xref: /openbmc/linux/fs/nfs/inode.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/nfs/inode.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  *
7  *  nfs inode and superblock handling functions
8  *
9  *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
10  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
11  *
12  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
13  *  J.S.Peatfield@damtp.cam.ac.uk
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/sched/signal.h>
20 #include <linux/time.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/stat.h>
25 #include <linux/errno.h>
26 #include <linux/unistd.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/sunrpc/stats.h>
29 #include <linux/sunrpc/metrics.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_mount.h>
32 #include <linux/nfs4_mount.h>
33 #include <linux/lockd/bind.h>
34 #include <linux/seq_file.h>
35 #include <linux/mount.h>
36 #include <linux/vfs.h>
37 #include <linux/inet.h>
38 #include <linux/nfs_xdr.h>
39 #include <linux/slab.h>
40 #include <linux/compat.h>
41 #include <linux/freezer.h>
42 #include <linux/uaccess.h>
43 #include <linux/iversion.h>
44 
45 #include "nfs4_fs.h"
46 #include "callback.h"
47 #include "delegation.h"
48 #include "iostat.h"
49 #include "internal.h"
50 #include "fscache.h"
51 #include "pnfs.h"
52 #include "nfs.h"
53 #include "netns.h"
54 #include "sysfs.h"
55 
56 #include "nfstrace.h"
57 
58 #define NFSDBG_FACILITY		NFSDBG_VFS
59 
60 #define NFS_64_BIT_INODE_NUMBERS_ENABLED	1
61 
62 /* Default is to see 64-bit inode numbers */
63 static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
64 
65 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
66 
67 static struct kmem_cache * nfs_inode_cachep;
68 
69 static inline unsigned long
nfs_fattr_to_ino_t(struct nfs_fattr * fattr)70 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
71 {
72 	return nfs_fileid_to_ino_t(fattr->fileid);
73 }
74 
nfs_wait_bit_killable(struct wait_bit_key * key,int mode)75 int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
76 {
77 	if (unlikely(nfs_current_task_exiting()))
78 		return -EINTR;
79 	schedule();
80 	if (signal_pending_state(mode, current))
81 		return -ERESTARTSYS;
82 	return 0;
83 }
84 EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
85 
86 /**
87  * nfs_compat_user_ino64 - returns the user-visible inode number
88  * @fileid: 64-bit fileid
89  *
90  * This function returns a 32-bit inode number if the boot parameter
91  * nfs.enable_ino64 is zero.
92  */
nfs_compat_user_ino64(u64 fileid)93 u64 nfs_compat_user_ino64(u64 fileid)
94 {
95 #ifdef CONFIG_COMPAT
96 	compat_ulong_t ino;
97 #else
98 	unsigned long ino;
99 #endif
100 
101 	if (enable_ino64)
102 		return fileid;
103 	ino = fileid;
104 	if (sizeof(ino) < sizeof(fileid))
105 		ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
106 	return ino;
107 }
108 
nfs_drop_inode(struct inode * inode)109 int nfs_drop_inode(struct inode *inode)
110 {
111 	return NFS_STALE(inode) || generic_drop_inode(inode);
112 }
113 EXPORT_SYMBOL_GPL(nfs_drop_inode);
114 
nfs_clear_inode(struct inode * inode)115 void nfs_clear_inode(struct inode *inode)
116 {
117 	/*
118 	 * The following should never happen...
119 	 */
120 	WARN_ON_ONCE(nfs_have_writebacks(inode));
121 	WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
122 	nfs_zap_acl_cache(inode);
123 	nfs_access_zap_cache(inode);
124 	nfs_fscache_clear_inode(inode);
125 }
126 EXPORT_SYMBOL_GPL(nfs_clear_inode);
127 
nfs_evict_inode(struct inode * inode)128 void nfs_evict_inode(struct inode *inode)
129 {
130 	truncate_inode_pages_final(&inode->i_data);
131 	clear_inode(inode);
132 	nfs_clear_inode(inode);
133 }
134 
nfs_sync_inode(struct inode * inode)135 int nfs_sync_inode(struct inode *inode)
136 {
137 	inode_dio_wait(inode);
138 	return nfs_wb_all(inode);
139 }
140 EXPORT_SYMBOL_GPL(nfs_sync_inode);
141 
142 /**
143  * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
144  * @mapping: pointer to struct address_space
145  */
nfs_sync_mapping(struct address_space * mapping)146 int nfs_sync_mapping(struct address_space *mapping)
147 {
148 	int ret = 0;
149 
150 	if (mapping->nrpages != 0) {
151 		unmap_mapping_range(mapping, 0, 0, 0);
152 		ret = nfs_wb_all(mapping->host);
153 	}
154 	return ret;
155 }
156 
nfs_attribute_timeout(struct inode * inode)157 static int nfs_attribute_timeout(struct inode *inode)
158 {
159 	struct nfs_inode *nfsi = NFS_I(inode);
160 
161 	return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
162 }
163 
nfs_check_cache_flags_invalid(struct inode * inode,unsigned long flags)164 static bool nfs_check_cache_flags_invalid(struct inode *inode,
165 					  unsigned long flags)
166 {
167 	unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
168 
169 	return (cache_validity & flags) != 0;
170 }
171 
nfs_check_cache_invalid(struct inode * inode,unsigned long flags)172 bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
173 {
174 	if (nfs_check_cache_flags_invalid(inode, flags))
175 		return true;
176 	return nfs_attribute_cache_expired(inode);
177 }
178 EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
179 
180 #ifdef CONFIG_NFS_V4_2
nfs_has_xattr_cache(const struct nfs_inode * nfsi)181 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
182 {
183 	return nfsi->xattr_cache != NULL;
184 }
185 #else
nfs_has_xattr_cache(const struct nfs_inode * nfsi)186 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
187 {
188 	return false;
189 }
190 #endif
191 
nfs_set_cache_invalid(struct inode * inode,unsigned long flags)192 void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
193 {
194 	struct nfs_inode *nfsi = NFS_I(inode);
195 	bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
196 
197 	if (have_delegation) {
198 		if (!(flags & NFS_INO_REVAL_FORCED))
199 			flags &= ~(NFS_INO_INVALID_MODE |
200 				   NFS_INO_INVALID_OTHER |
201 				   NFS_INO_INVALID_XATTR);
202 		flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
203 	}
204 
205 	if (!nfs_has_xattr_cache(nfsi))
206 		flags &= ~NFS_INO_INVALID_XATTR;
207 	if (flags & NFS_INO_INVALID_DATA)
208 		nfs_fscache_invalidate(inode, 0);
209 	flags &= ~NFS_INO_REVAL_FORCED;
210 
211 	flags |= nfsi->cache_validity;
212 	if (inode->i_mapping->nrpages == 0)
213 		flags &= ~NFS_INO_INVALID_DATA;
214 
215 	/* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */
216 	smp_store_release(&nfsi->cache_validity, flags);
217 
218 	if (inode->i_mapping->nrpages == 0 ||
219 	    nfsi->cache_validity & NFS_INO_INVALID_DATA) {
220 		nfs_ooo_clear(nfsi);
221 	}
222 	trace_nfs_set_cache_invalid(inode, 0);
223 }
224 EXPORT_SYMBOL_GPL(nfs_set_cache_invalid);
225 
226 /*
227  * Invalidate the local caches
228  */
nfs_zap_caches_locked(struct inode * inode)229 static void nfs_zap_caches_locked(struct inode *inode)
230 {
231 	struct nfs_inode *nfsi = NFS_I(inode);
232 	int mode = inode->i_mode;
233 
234 	nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
235 
236 	nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
237 	nfsi->attrtimeo_timestamp = jiffies;
238 
239 	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
240 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
241 						     NFS_INO_INVALID_DATA |
242 						     NFS_INO_INVALID_ACCESS |
243 						     NFS_INO_INVALID_ACL |
244 						     NFS_INO_INVALID_XATTR);
245 	else
246 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
247 						     NFS_INO_INVALID_ACCESS |
248 						     NFS_INO_INVALID_ACL |
249 						     NFS_INO_INVALID_XATTR);
250 	nfs_zap_label_cache_locked(nfsi);
251 }
252 
nfs_zap_caches(struct inode * inode)253 void nfs_zap_caches(struct inode *inode)
254 {
255 	spin_lock(&inode->i_lock);
256 	nfs_zap_caches_locked(inode);
257 	spin_unlock(&inode->i_lock);
258 }
259 
nfs_zap_mapping(struct inode * inode,struct address_space * mapping)260 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
261 {
262 	if (mapping->nrpages != 0) {
263 		spin_lock(&inode->i_lock);
264 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
265 		spin_unlock(&inode->i_lock);
266 	}
267 }
268 
nfs_zap_acl_cache(struct inode * inode)269 void nfs_zap_acl_cache(struct inode *inode)
270 {
271 	void (*clear_acl_cache)(struct inode *);
272 
273 	clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
274 	if (clear_acl_cache != NULL)
275 		clear_acl_cache(inode);
276 	spin_lock(&inode->i_lock);
277 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
278 	spin_unlock(&inode->i_lock);
279 }
280 EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
281 
nfs_invalidate_atime(struct inode * inode)282 void nfs_invalidate_atime(struct inode *inode)
283 {
284 	spin_lock(&inode->i_lock);
285 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
286 	spin_unlock(&inode->i_lock);
287 }
288 EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
289 
290 /*
291  * Invalidate, but do not unhash, the inode.
292  * NB: must be called with inode->i_lock held!
293  */
nfs_set_inode_stale_locked(struct inode * inode)294 static void nfs_set_inode_stale_locked(struct inode *inode)
295 {
296 	set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
297 	nfs_zap_caches_locked(inode);
298 	trace_nfs_set_inode_stale(inode);
299 }
300 
nfs_set_inode_stale(struct inode * inode)301 void nfs_set_inode_stale(struct inode *inode)
302 {
303 	spin_lock(&inode->i_lock);
304 	nfs_set_inode_stale_locked(inode);
305 	spin_unlock(&inode->i_lock);
306 }
307 
308 struct nfs_find_desc {
309 	struct nfs_fh		*fh;
310 	struct nfs_fattr	*fattr;
311 };
312 
313 /*
314  * In NFSv3 we can have 64bit inode numbers. In order to support
315  * this, and re-exported directories (also seen in NFSv2)
316  * we are forced to allow 2 different inodes to have the same
317  * i_ino.
318  */
319 static int
nfs_find_actor(struct inode * inode,void * opaque)320 nfs_find_actor(struct inode *inode, void *opaque)
321 {
322 	struct nfs_find_desc	*desc = opaque;
323 	struct nfs_fh		*fh = desc->fh;
324 	struct nfs_fattr	*fattr = desc->fattr;
325 
326 	if (NFS_FILEID(inode) != fattr->fileid)
327 		return 0;
328 	if (inode_wrong_type(inode, fattr->mode))
329 		return 0;
330 	if (nfs_compare_fh(NFS_FH(inode), fh))
331 		return 0;
332 	if (is_bad_inode(inode) || NFS_STALE(inode))
333 		return 0;
334 	return 1;
335 }
336 
337 static int
nfs_init_locked(struct inode * inode,void * opaque)338 nfs_init_locked(struct inode *inode, void *opaque)
339 {
340 	struct nfs_find_desc	*desc = opaque;
341 	struct nfs_fattr	*fattr = desc->fattr;
342 
343 	set_nfs_fileid(inode, fattr->fileid);
344 	inode->i_mode = fattr->mode;
345 	nfs_copy_fh(NFS_FH(inode), desc->fh);
346 	return 0;
347 }
348 
349 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
nfs_clear_label_invalid(struct inode * inode)350 static void nfs_clear_label_invalid(struct inode *inode)
351 {
352 	spin_lock(&inode->i_lock);
353 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
354 	spin_unlock(&inode->i_lock);
355 }
356 
nfs_setsecurity(struct inode * inode,struct nfs_fattr * fattr)357 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
358 {
359 	int error;
360 
361 	if (fattr->label == NULL)
362 		return;
363 
364 	if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
365 		error = security_inode_notifysecctx(inode, fattr->label->label,
366 				fattr->label->len);
367 		if (error)
368 			printk(KERN_ERR "%s() %s %d "
369 					"security_inode_notifysecctx() %d\n",
370 					__func__,
371 					(char *)fattr->label->label,
372 					fattr->label->len, error);
373 		nfs_clear_label_invalid(inode);
374 	}
375 }
376 
nfs4_label_alloc(struct nfs_server * server,gfp_t flags)377 struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
378 {
379 	struct nfs4_label *label;
380 
381 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
382 		return NULL;
383 
384 	label = kzalloc(sizeof(struct nfs4_label), flags);
385 	if (label == NULL)
386 		return ERR_PTR(-ENOMEM);
387 
388 	label->label = kzalloc(NFS4_MAXLABELLEN, flags);
389 	if (label->label == NULL) {
390 		kfree(label);
391 		return ERR_PTR(-ENOMEM);
392 	}
393 	label->len = NFS4_MAXLABELLEN;
394 
395 	return label;
396 }
397 EXPORT_SYMBOL_GPL(nfs4_label_alloc);
398 #else
nfs_setsecurity(struct inode * inode,struct nfs_fattr * fattr)399 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
400 {
401 }
402 #endif
403 EXPORT_SYMBOL_GPL(nfs_setsecurity);
404 
405 /* Search for inode identified by fh, fileid and i_mode in inode cache. */
406 struct inode *
nfs_ilookup(struct super_block * sb,struct nfs_fattr * fattr,struct nfs_fh * fh)407 nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
408 {
409 	struct nfs_find_desc desc = {
410 		.fh	= fh,
411 		.fattr	= fattr,
412 	};
413 	struct inode *inode;
414 	unsigned long hash;
415 
416 	if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
417 	    !(fattr->valid & NFS_ATTR_FATTR_TYPE))
418 		return NULL;
419 
420 	hash = nfs_fattr_to_ino_t(fattr);
421 	inode = ilookup5(sb, hash, nfs_find_actor, &desc);
422 
423 	dprintk("%s: returning %p\n", __func__, inode);
424 	return inode;
425 }
426 
nfs_inode_init_regular(struct nfs_inode * nfsi)427 static void nfs_inode_init_regular(struct nfs_inode *nfsi)
428 {
429 	atomic_long_set(&nfsi->nrequests, 0);
430 	atomic_long_set(&nfsi->redirtied_pages, 0);
431 	INIT_LIST_HEAD(&nfsi->commit_info.list);
432 	atomic_long_set(&nfsi->commit_info.ncommit, 0);
433 	atomic_set(&nfsi->commit_info.rpcs_out, 0);
434 	mutex_init(&nfsi->commit_mutex);
435 }
436 
nfs_inode_init_dir(struct nfs_inode * nfsi)437 static void nfs_inode_init_dir(struct nfs_inode *nfsi)
438 {
439 	nfsi->cache_change_attribute = 0;
440 	memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
441 	init_rwsem(&nfsi->rmdir_sem);
442 }
443 
444 /*
445  * This is our front-end to iget that looks up inodes by file handle
446  * instead of inode number.
447  */
448 struct inode *
nfs_fhget(struct super_block * sb,struct nfs_fh * fh,struct nfs_fattr * fattr)449 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
450 {
451 	struct nfs_find_desc desc = {
452 		.fh	= fh,
453 		.fattr	= fattr
454 	};
455 	struct inode *inode = ERR_PTR(-ENOENT);
456 	u64 fattr_supported = NFS_SB(sb)->fattr_valid;
457 	unsigned long hash;
458 
459 	nfs_attr_check_mountpoint(sb, fattr);
460 
461 	if (nfs_attr_use_mounted_on_fileid(fattr))
462 		fattr->fileid = fattr->mounted_on_fileid;
463 	else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
464 		goto out_no_inode;
465 	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
466 		goto out_no_inode;
467 
468 	hash = nfs_fattr_to_ino_t(fattr);
469 
470 	inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
471 	if (inode == NULL) {
472 		inode = ERR_PTR(-ENOMEM);
473 		goto out_no_inode;
474 	}
475 
476 	if (inode->i_state & I_NEW) {
477 		struct nfs_inode *nfsi = NFS_I(inode);
478 		unsigned long now = jiffies;
479 
480 		/* We set i_ino for the few things that still rely on it,
481 		 * such as stat(2) */
482 		inode->i_ino = hash;
483 
484 		/* We can't support update_atime(), since the server will reset it */
485 		inode->i_flags |= S_NOATIME|S_NOCMTIME;
486 		inode->i_mode = fattr->mode;
487 		nfsi->cache_validity = 0;
488 		if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
489 				&& (fattr_supported & NFS_ATTR_FATTR_MODE))
490 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
491 		/* Why so? Because we want revalidate for devices/FIFOs, and
492 		 * that's precisely what we have in nfs_file_inode_operations.
493 		 */
494 		inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
495 		if (S_ISREG(inode->i_mode)) {
496 			inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
497 			inode->i_data.a_ops = &nfs_file_aops;
498 			nfs_inode_init_regular(nfsi);
499 		} else if (S_ISDIR(inode->i_mode)) {
500 			inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
501 			inode->i_fop = &nfs_dir_operations;
502 			inode->i_data.a_ops = &nfs_dir_aops;
503 			nfs_inode_init_dir(nfsi);
504 			/* Deal with crossing mountpoints */
505 			if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
506 					fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
507 				if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
508 					inode->i_op = &nfs_referral_inode_operations;
509 				else
510 					inode->i_op = &nfs_mountpoint_inode_operations;
511 				inode->i_fop = NULL;
512 				inode->i_flags |= S_AUTOMOUNT;
513 			}
514 		} else if (S_ISLNK(inode->i_mode)) {
515 			inode->i_op = &nfs_symlink_inode_operations;
516 			inode_nohighmem(inode);
517 		} else
518 			init_special_inode(inode, inode->i_mode, fattr->rdev);
519 
520 		memset(&inode->i_atime, 0, sizeof(inode->i_atime));
521 		memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
522 		inode_set_ctime(inode, 0, 0);
523 		inode_set_iversion_raw(inode, 0);
524 		inode->i_size = 0;
525 		clear_nlink(inode);
526 		inode->i_uid = make_kuid(&init_user_ns, -2);
527 		inode->i_gid = make_kgid(&init_user_ns, -2);
528 		inode->i_blocks = 0;
529 		nfsi->write_io = 0;
530 		nfsi->read_io = 0;
531 
532 		nfsi->read_cache_jiffies = fattr->time_start;
533 		nfsi->attr_gencount = fattr->gencount;
534 		if (fattr->valid & NFS_ATTR_FATTR_ATIME)
535 			inode->i_atime = fattr->atime;
536 		else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
537 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
538 		if (fattr->valid & NFS_ATTR_FATTR_MTIME)
539 			inode->i_mtime = fattr->mtime;
540 		else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
541 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
542 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
543 			inode_set_ctime_to_ts(inode, fattr->ctime);
544 		else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
545 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
546 		if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
547 			inode_set_iversion_raw(inode, fattr->change_attr);
548 		else
549 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
550 		if (fattr->valid & NFS_ATTR_FATTR_SIZE)
551 			inode->i_size = nfs_size_to_loff_t(fattr->size);
552 		else
553 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
554 		if (fattr->valid & NFS_ATTR_FATTR_NLINK)
555 			set_nlink(inode, fattr->nlink);
556 		else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
557 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
558 		if (fattr->valid & NFS_ATTR_FATTR_OWNER)
559 			inode->i_uid = fattr->uid;
560 		else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
561 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
562 		if (fattr->valid & NFS_ATTR_FATTR_GROUP)
563 			inode->i_gid = fattr->gid;
564 		else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
565 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
566 		if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
567 			inode->i_blocks = fattr->du.nfs2.blocks;
568 		else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED &&
569 			 fattr->size != 0)
570 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
571 		if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
572 			/*
573 			 * report the blocks in 512byte units
574 			 */
575 			inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
576 		} else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED &&
577 			   fattr->size != 0)
578 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
579 
580 		nfs_setsecurity(inode, fattr);
581 
582 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
583 		nfsi->attrtimeo_timestamp = now;
584 		nfsi->access_cache = RB_ROOT;
585 
586 		nfs_fscache_init_inode(inode);
587 
588 		unlock_new_inode(inode);
589 	} else {
590 		int err = nfs_refresh_inode(inode, fattr);
591 		if (err < 0) {
592 			iput(inode);
593 			inode = ERR_PTR(err);
594 			goto out_no_inode;
595 		}
596 	}
597 	dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
598 		inode->i_sb->s_id,
599 		(unsigned long long)NFS_FILEID(inode),
600 		nfs_display_fhandle_hash(fh),
601 		atomic_read(&inode->i_count));
602 
603 out:
604 	return inode;
605 
606 out_no_inode:
607 	dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
608 	goto out;
609 }
610 EXPORT_SYMBOL_GPL(nfs_fhget);
611 
612 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
613 
614 int
nfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)615 nfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
616 	    struct iattr *attr)
617 {
618 	struct inode *inode = d_inode(dentry);
619 	struct nfs_fattr *fattr;
620 	int error = 0;
621 
622 	nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
623 
624 	/* skip mode change if it's just for clearing setuid/setgid */
625 	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
626 		attr->ia_valid &= ~ATTR_MODE;
627 
628 	if (attr->ia_valid & ATTR_SIZE) {
629 		BUG_ON(!S_ISREG(inode->i_mode));
630 
631 		error = inode_newsize_ok(inode, attr->ia_size);
632 		if (error)
633 			return error;
634 
635 		if (attr->ia_size == i_size_read(inode))
636 			attr->ia_valid &= ~ATTR_SIZE;
637 	}
638 
639 	/* Optimization: if the end result is no change, don't RPC */
640 	if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0)
641 		return 0;
642 
643 	trace_nfs_setattr_enter(inode);
644 
645 	/* Write all dirty data */
646 	if (S_ISREG(inode->i_mode))
647 		nfs_sync_inode(inode);
648 
649 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
650 	if (fattr == NULL) {
651 		error = -ENOMEM;
652 		goto out;
653 	}
654 
655 	error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
656 	if (error == 0)
657 		error = nfs_refresh_inode(inode, fattr);
658 	nfs_free_fattr(fattr);
659 out:
660 	trace_nfs_setattr_exit(inode, error);
661 	return error;
662 }
663 EXPORT_SYMBOL_GPL(nfs_setattr);
664 
665 /**
666  * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
667  * @inode: inode of the file used
668  * @offset: file offset to start truncating
669  *
670  * This is a copy of the common vmtruncate, but with the locking
671  * corrected to take into account the fact that NFS requires
672  * inode->i_size to be updated under the inode->i_lock.
673  * Note: must be called with inode->i_lock held!
674  */
nfs_vmtruncate(struct inode * inode,loff_t offset)675 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
676 {
677 	int err;
678 
679 	err = inode_newsize_ok(inode, offset);
680 	if (err)
681 		goto out;
682 
683 	trace_nfs_size_truncate(inode, offset);
684 	i_size_write(inode, offset);
685 	/* Optimisation */
686 	if (offset == 0) {
687 		NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
688 		nfs_ooo_clear(NFS_I(inode));
689 	}
690 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
691 
692 	spin_unlock(&inode->i_lock);
693 	truncate_pagecache(inode, offset);
694 	spin_lock(&inode->i_lock);
695 out:
696 	return err;
697 }
698 
699 /**
700  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
701  * @inode: pointer to struct inode
702  * @attr: pointer to struct iattr
703  * @fattr: pointer to struct nfs_fattr
704  *
705  * Note: we do this in the *proc.c in order to ensure that
706  *       it works for things like exclusive creates too.
707  */
nfs_setattr_update_inode(struct inode * inode,struct iattr * attr,struct nfs_fattr * fattr)708 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
709 		struct nfs_fattr *fattr)
710 {
711 	/* Barrier: bump the attribute generation count. */
712 	nfs_fattr_set_barrier(fattr);
713 
714 	spin_lock(&inode->i_lock);
715 	NFS_I(inode)->attr_gencount = fattr->gencount;
716 	if ((attr->ia_valid & ATTR_SIZE) != 0) {
717 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME |
718 						     NFS_INO_INVALID_BLOCKS);
719 		nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
720 		nfs_vmtruncate(inode, attr->ia_size);
721 	}
722 	if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
723 		NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
724 		if ((attr->ia_valid & ATTR_KILL_SUID) != 0 &&
725 		    inode->i_mode & S_ISUID)
726 			inode->i_mode &= ~S_ISUID;
727 		if (setattr_should_drop_sgid(&nop_mnt_idmap, inode))
728 			inode->i_mode &= ~S_ISGID;
729 		if ((attr->ia_valid & ATTR_MODE) != 0) {
730 			int mode = attr->ia_mode & S_IALLUGO;
731 			mode |= inode->i_mode & ~S_IALLUGO;
732 			inode->i_mode = mode;
733 		}
734 		if ((attr->ia_valid & ATTR_UID) != 0)
735 			inode->i_uid = attr->ia_uid;
736 		if ((attr->ia_valid & ATTR_GID) != 0)
737 			inode->i_gid = attr->ia_gid;
738 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
739 			inode_set_ctime_to_ts(inode, fattr->ctime);
740 		else
741 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
742 					| NFS_INO_INVALID_CTIME);
743 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
744 				| NFS_INO_INVALID_ACL);
745 	}
746 	if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
747 		NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
748 				| NFS_INO_INVALID_CTIME);
749 		if (fattr->valid & NFS_ATTR_FATTR_ATIME)
750 			inode->i_atime = fattr->atime;
751 		else if (attr->ia_valid & ATTR_ATIME_SET)
752 			inode->i_atime = attr->ia_atime;
753 		else
754 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
755 
756 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
757 			inode_set_ctime_to_ts(inode, fattr->ctime);
758 		else
759 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
760 					| NFS_INO_INVALID_CTIME);
761 	}
762 	if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
763 		NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
764 				| NFS_INO_INVALID_CTIME);
765 		if (fattr->valid & NFS_ATTR_FATTR_MTIME)
766 			inode->i_mtime = fattr->mtime;
767 		else if (attr->ia_valid & ATTR_MTIME_SET)
768 			inode->i_mtime = attr->ia_mtime;
769 		else
770 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
771 
772 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
773 			inode_set_ctime_to_ts(inode, fattr->ctime);
774 		else
775 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
776 					| NFS_INO_INVALID_CTIME);
777 	}
778 	if (fattr->valid)
779 		nfs_update_inode(inode, fattr);
780 	spin_unlock(&inode->i_lock);
781 }
782 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
783 
784 /*
785  * Don't request help from readdirplus if the file is being written to,
786  * or if attribute caching is turned off
787  */
nfs_getattr_readdirplus_enable(const struct inode * inode)788 static bool nfs_getattr_readdirplus_enable(const struct inode *inode)
789 {
790 	return nfs_server_capable(inode, NFS_CAP_READDIRPLUS) &&
791 	       !nfs_have_writebacks(inode) && NFS_MAXATTRTIMEO(inode) > 5 * HZ;
792 }
793 
nfs_readdirplus_parent_cache_miss(struct dentry * dentry)794 static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
795 {
796 	if (!IS_ROOT(dentry)) {
797 		struct dentry *parent = dget_parent(dentry);
798 		nfs_readdir_record_entry_cache_miss(d_inode(parent));
799 		dput(parent);
800 	}
801 }
802 
nfs_readdirplus_parent_cache_hit(struct dentry * dentry)803 static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
804 {
805 	if (!IS_ROOT(dentry)) {
806 		struct dentry *parent = dget_parent(dentry);
807 		nfs_readdir_record_entry_cache_hit(d_inode(parent));
808 		dput(parent);
809 	}
810 }
811 
nfs_get_valid_attrmask(struct inode * inode)812 static u32 nfs_get_valid_attrmask(struct inode *inode)
813 {
814 	unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
815 	u32 reply_mask = STATX_INO | STATX_TYPE;
816 
817 	if (!(cache_validity & NFS_INO_INVALID_ATIME))
818 		reply_mask |= STATX_ATIME;
819 	if (!(cache_validity & NFS_INO_INVALID_CTIME))
820 		reply_mask |= STATX_CTIME;
821 	if (!(cache_validity & NFS_INO_INVALID_MTIME))
822 		reply_mask |= STATX_MTIME;
823 	if (!(cache_validity & NFS_INO_INVALID_SIZE))
824 		reply_mask |= STATX_SIZE;
825 	if (!(cache_validity & NFS_INO_INVALID_NLINK))
826 		reply_mask |= STATX_NLINK;
827 	if (!(cache_validity & NFS_INO_INVALID_MODE))
828 		reply_mask |= STATX_MODE;
829 	if (!(cache_validity & NFS_INO_INVALID_OTHER))
830 		reply_mask |= STATX_UID | STATX_GID;
831 	if (!(cache_validity & NFS_INO_INVALID_BLOCKS))
832 		reply_mask |= STATX_BLOCKS;
833 	if (!(cache_validity & NFS_INO_INVALID_CHANGE))
834 		reply_mask |= STATX_CHANGE_COOKIE;
835 	return reply_mask;
836 }
837 
nfs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)838 int nfs_getattr(struct mnt_idmap *idmap, const struct path *path,
839 		struct kstat *stat, u32 request_mask, unsigned int query_flags)
840 {
841 	struct inode *inode = d_inode(path->dentry);
842 	struct nfs_server *server = NFS_SERVER(inode);
843 	unsigned long cache_validity;
844 	int err = 0;
845 	bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
846 	bool do_update = false;
847 	bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode);
848 
849 	trace_nfs_getattr_enter(inode);
850 
851 	request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID |
852 			STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME |
853 			STATX_INO | STATX_SIZE | STATX_BLOCKS |
854 			STATX_CHANGE_COOKIE;
855 
856 	if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
857 		if (readdirplus_enabled)
858 			nfs_readdirplus_parent_cache_hit(path->dentry);
859 		goto out_no_revalidate;
860 	}
861 
862 	/* Flush out writes to the server in order to update c/mtime/version.  */
863 	if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_CHANGE_COOKIE)) &&
864 	    S_ISREG(inode->i_mode))
865 		filemap_write_and_wait(inode->i_mapping);
866 
867 	/*
868 	 * We may force a getattr if the user cares about atime.
869 	 *
870 	 * Note that we only have to check the vfsmount flags here:
871 	 *  - NFS always sets S_NOATIME by so checking it would give a
872 	 *    bogus result
873 	 *  - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
874 	 *    no point in checking those.
875 	 */
876 	if ((path->mnt->mnt_flags & MNT_NOATIME) ||
877 	    ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
878 		request_mask &= ~STATX_ATIME;
879 
880 	/* Is the user requesting attributes that might need revalidation? */
881 	if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
882 					STATX_MTIME|STATX_UID|STATX_GID|
883 					STATX_SIZE|STATX_BLOCKS|
884 					STATX_CHANGE_COOKIE)))
885 		goto out_no_revalidate;
886 
887 	/* Check whether the cached attributes are stale */
888 	do_update |= force_sync || nfs_attribute_cache_expired(inode);
889 	cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
890 	do_update |= cache_validity & NFS_INO_INVALID_CHANGE;
891 	if (request_mask & STATX_ATIME)
892 		do_update |= cache_validity & NFS_INO_INVALID_ATIME;
893 	if (request_mask & STATX_CTIME)
894 		do_update |= cache_validity & NFS_INO_INVALID_CTIME;
895 	if (request_mask & STATX_MTIME)
896 		do_update |= cache_validity & NFS_INO_INVALID_MTIME;
897 	if (request_mask & STATX_SIZE)
898 		do_update |= cache_validity & NFS_INO_INVALID_SIZE;
899 	if (request_mask & STATX_NLINK)
900 		do_update |= cache_validity & NFS_INO_INVALID_NLINK;
901 	if (request_mask & STATX_MODE)
902 		do_update |= cache_validity & NFS_INO_INVALID_MODE;
903 	if (request_mask & (STATX_UID | STATX_GID))
904 		do_update |= cache_validity & NFS_INO_INVALID_OTHER;
905 	if (request_mask & STATX_BLOCKS)
906 		do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
907 
908 	if (do_update) {
909 		if (readdirplus_enabled)
910 			nfs_readdirplus_parent_cache_miss(path->dentry);
911 		err = __nfs_revalidate_inode(server, inode);
912 		if (err)
913 			goto out;
914 	} else if (readdirplus_enabled)
915 		nfs_readdirplus_parent_cache_hit(path->dentry);
916 out_no_revalidate:
917 	/* Only return attributes that were revalidated. */
918 	stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask;
919 
920 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
921 	stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
922 	stat->change_cookie = inode_peek_iversion_raw(inode);
923 	stat->attributes_mask |= STATX_ATTR_CHANGE_MONOTONIC;
924 	if (server->change_attr_type != NFS4_CHANGE_TYPE_IS_UNDEFINED)
925 		stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
926 	if (S_ISDIR(inode->i_mode))
927 		stat->blksize = NFS_SERVER(inode)->dtsize;
928 out:
929 	trace_nfs_getattr_exit(inode, err);
930 	return err;
931 }
932 EXPORT_SYMBOL_GPL(nfs_getattr);
933 
nfs_init_lock_context(struct nfs_lock_context * l_ctx)934 static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
935 {
936 	refcount_set(&l_ctx->count, 1);
937 	l_ctx->lockowner = current->files;
938 	INIT_LIST_HEAD(&l_ctx->list);
939 	atomic_set(&l_ctx->io_count, 0);
940 }
941 
__nfs_find_lock_context(struct nfs_open_context * ctx)942 static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
943 {
944 	struct nfs_lock_context *pos;
945 
946 	list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
947 		if (pos->lockowner != current->files)
948 			continue;
949 		if (refcount_inc_not_zero(&pos->count))
950 			return pos;
951 	}
952 	return NULL;
953 }
954 
nfs_get_lock_context(struct nfs_open_context * ctx)955 struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
956 {
957 	struct nfs_lock_context *res, *new = NULL;
958 	struct inode *inode = d_inode(ctx->dentry);
959 
960 	rcu_read_lock();
961 	res = __nfs_find_lock_context(ctx);
962 	rcu_read_unlock();
963 	if (res == NULL) {
964 		new = kmalloc(sizeof(*new), GFP_KERNEL_ACCOUNT);
965 		if (new == NULL)
966 			return ERR_PTR(-ENOMEM);
967 		nfs_init_lock_context(new);
968 		spin_lock(&inode->i_lock);
969 		res = __nfs_find_lock_context(ctx);
970 		if (res == NULL) {
971 			new->open_context = get_nfs_open_context(ctx);
972 			if (new->open_context) {
973 				list_add_tail_rcu(&new->list,
974 						&ctx->lock_context.list);
975 				res = new;
976 				new = NULL;
977 			} else
978 				res = ERR_PTR(-EBADF);
979 		}
980 		spin_unlock(&inode->i_lock);
981 		kfree(new);
982 	}
983 	return res;
984 }
985 EXPORT_SYMBOL_GPL(nfs_get_lock_context);
986 
nfs_put_lock_context(struct nfs_lock_context * l_ctx)987 void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
988 {
989 	struct nfs_open_context *ctx = l_ctx->open_context;
990 	struct inode *inode = d_inode(ctx->dentry);
991 
992 	if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
993 		return;
994 	list_del_rcu(&l_ctx->list);
995 	spin_unlock(&inode->i_lock);
996 	put_nfs_open_context(ctx);
997 	kfree_rcu(l_ctx, rcu_head);
998 }
999 EXPORT_SYMBOL_GPL(nfs_put_lock_context);
1000 
1001 /**
1002  * nfs_close_context - Common close_context() routine NFSv2/v3
1003  * @ctx: pointer to context
1004  * @is_sync: is this a synchronous close
1005  *
1006  * Ensure that the attributes are up to date if we're mounted
1007  * with close-to-open semantics and we have cached data that will
1008  * need to be revalidated on open.
1009  */
nfs_close_context(struct nfs_open_context * ctx,int is_sync)1010 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
1011 {
1012 	struct nfs_inode *nfsi;
1013 	struct inode *inode;
1014 
1015 	if (!(ctx->mode & FMODE_WRITE))
1016 		return;
1017 	if (!is_sync)
1018 		return;
1019 	inode = d_inode(ctx->dentry);
1020 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1021 		return;
1022 	nfsi = NFS_I(inode);
1023 	if (inode->i_mapping->nrpages == 0)
1024 		return;
1025 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1026 		return;
1027 	if (!list_empty(&nfsi->open_files))
1028 		return;
1029 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO)
1030 		return;
1031 	nfs_revalidate_inode(inode,
1032 			     NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
1033 }
1034 EXPORT_SYMBOL_GPL(nfs_close_context);
1035 
alloc_nfs_open_context(struct dentry * dentry,fmode_t f_mode,struct file * filp)1036 struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
1037 						fmode_t f_mode,
1038 						struct file *filp)
1039 {
1040 	struct nfs_open_context *ctx;
1041 
1042 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
1043 	if (!ctx)
1044 		return ERR_PTR(-ENOMEM);
1045 	nfs_sb_active(dentry->d_sb);
1046 	ctx->dentry = dget(dentry);
1047 	if (filp)
1048 		ctx->cred = get_cred(filp->f_cred);
1049 	else
1050 		ctx->cred = get_current_cred();
1051 	rcu_assign_pointer(ctx->ll_cred, NULL);
1052 	ctx->state = NULL;
1053 	ctx->mode = f_mode;
1054 	ctx->flags = 0;
1055 	ctx->error = 0;
1056 	ctx->flock_owner = (fl_owner_t)filp;
1057 	nfs_init_lock_context(&ctx->lock_context);
1058 	ctx->lock_context.open_context = ctx;
1059 	INIT_LIST_HEAD(&ctx->list);
1060 	ctx->mdsthreshold = NULL;
1061 	return ctx;
1062 }
1063 EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
1064 
get_nfs_open_context(struct nfs_open_context * ctx)1065 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1066 {
1067 	if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1068 		return ctx;
1069 	return NULL;
1070 }
1071 EXPORT_SYMBOL_GPL(get_nfs_open_context);
1072 
__put_nfs_open_context(struct nfs_open_context * ctx,int is_sync)1073 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1074 {
1075 	struct inode *inode = d_inode(ctx->dentry);
1076 	struct super_block *sb = ctx->dentry->d_sb;
1077 
1078 	if (!refcount_dec_and_test(&ctx->lock_context.count))
1079 		return;
1080 	if (!list_empty(&ctx->list)) {
1081 		spin_lock(&inode->i_lock);
1082 		list_del_rcu(&ctx->list);
1083 		spin_unlock(&inode->i_lock);
1084 	}
1085 	if (inode != NULL)
1086 		NFS_PROTO(inode)->close_context(ctx, is_sync);
1087 	put_cred(ctx->cred);
1088 	dput(ctx->dentry);
1089 	nfs_sb_deactive(sb);
1090 	put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1));
1091 	kfree(ctx->mdsthreshold);
1092 	kfree_rcu(ctx, rcu_head);
1093 }
1094 
put_nfs_open_context(struct nfs_open_context * ctx)1095 void put_nfs_open_context(struct nfs_open_context *ctx)
1096 {
1097 	__put_nfs_open_context(ctx, 0);
1098 }
1099 EXPORT_SYMBOL_GPL(put_nfs_open_context);
1100 
put_nfs_open_context_sync(struct nfs_open_context * ctx)1101 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1102 {
1103 	__put_nfs_open_context(ctx, 1);
1104 }
1105 
1106 /*
1107  * Ensure that mmap has a recent RPC credential for use when writing out
1108  * shared pages
1109  */
nfs_inode_attach_open_context(struct nfs_open_context * ctx)1110 void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
1111 {
1112 	struct inode *inode = d_inode(ctx->dentry);
1113 	struct nfs_inode *nfsi = NFS_I(inode);
1114 
1115 	spin_lock(&inode->i_lock);
1116 	if (list_empty(&nfsi->open_files) &&
1117 	    nfs_ooo_test(nfsi))
1118 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA |
1119 						     NFS_INO_REVAL_FORCED);
1120 	list_add_tail_rcu(&ctx->list, &nfsi->open_files);
1121 	spin_unlock(&inode->i_lock);
1122 }
1123 EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1124 
nfs_file_set_open_context(struct file * filp,struct nfs_open_context * ctx)1125 void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1126 {
1127 	filp->private_data = get_nfs_open_context(ctx);
1128 	set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1129 	if (list_empty(&ctx->list))
1130 		nfs_inode_attach_open_context(ctx);
1131 }
1132 EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
1133 
1134 /*
1135  * Given an inode, search for an open context with the desired characteristics
1136  */
nfs_find_open_context(struct inode * inode,const struct cred * cred,fmode_t mode)1137 struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
1138 {
1139 	struct nfs_inode *nfsi = NFS_I(inode);
1140 	struct nfs_open_context *pos, *ctx = NULL;
1141 
1142 	rcu_read_lock();
1143 	list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
1144 		if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
1145 			continue;
1146 		if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1147 			continue;
1148 		if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
1149 			continue;
1150 		ctx = get_nfs_open_context(pos);
1151 		if (ctx)
1152 			break;
1153 	}
1154 	rcu_read_unlock();
1155 	return ctx;
1156 }
1157 
nfs_file_clear_open_context(struct file * filp)1158 void nfs_file_clear_open_context(struct file *filp)
1159 {
1160 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1161 
1162 	if (ctx) {
1163 		struct inode *inode = d_inode(ctx->dentry);
1164 
1165 		clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1166 		/*
1167 		 * We fatal error on write before. Try to writeback
1168 		 * every page again.
1169 		 */
1170 		if (ctx->error < 0)
1171 			invalidate_inode_pages2(inode->i_mapping);
1172 		filp->private_data = NULL;
1173 		put_nfs_open_context_sync(ctx);
1174 	}
1175 }
1176 
1177 /*
1178  * These allocate and release file read/write context information.
1179  */
nfs_open(struct inode * inode,struct file * filp)1180 int nfs_open(struct inode *inode, struct file *filp)
1181 {
1182 	struct nfs_open_context *ctx;
1183 
1184 	ctx = alloc_nfs_open_context(file_dentry(filp),
1185 				     flags_to_mode(filp->f_flags), filp);
1186 	if (IS_ERR(ctx))
1187 		return PTR_ERR(ctx);
1188 	nfs_file_set_open_context(filp, ctx);
1189 	put_nfs_open_context(ctx);
1190 	nfs_fscache_open_file(inode, filp);
1191 	return 0;
1192 }
1193 
1194 /*
1195  * This function is called whenever some part of NFS notices that
1196  * the cached attributes have to be refreshed.
1197  */
1198 int
__nfs_revalidate_inode(struct nfs_server * server,struct inode * inode)1199 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1200 {
1201 	int		 status = -ESTALE;
1202 	struct nfs_fattr *fattr = NULL;
1203 	struct nfs_inode *nfsi = NFS_I(inode);
1204 
1205 	dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1206 		inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
1207 
1208 	trace_nfs_revalidate_inode_enter(inode);
1209 
1210 	if (is_bad_inode(inode))
1211 		goto out;
1212 	if (NFS_STALE(inode))
1213 		goto out;
1214 
1215 	/* pNFS: Attributes aren't updated until we layoutcommit */
1216 	if (S_ISREG(inode->i_mode)) {
1217 		status = pnfs_sync_inode(inode, false);
1218 		if (status)
1219 			goto out;
1220 	}
1221 
1222 	status = -ENOMEM;
1223 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1224 	if (fattr == NULL)
1225 		goto out;
1226 
1227 	nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1228 
1229 	status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode);
1230 	if (status != 0) {
1231 		dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
1232 			 inode->i_sb->s_id,
1233 			 (unsigned long long)NFS_FILEID(inode), status);
1234 		switch (status) {
1235 		case -ETIMEDOUT:
1236 			/* A soft timeout occurred. Use cached information? */
1237 			if (server->flags & NFS_MOUNT_SOFTREVAL)
1238 				status = 0;
1239 			break;
1240 		case -ESTALE:
1241 			if (!S_ISDIR(inode->i_mode))
1242 				nfs_set_inode_stale(inode);
1243 			else
1244 				nfs_zap_caches(inode);
1245 		}
1246 		goto out;
1247 	}
1248 
1249 	status = nfs_refresh_inode(inode, fattr);
1250 	if (status) {
1251 		dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
1252 			 inode->i_sb->s_id,
1253 			 (unsigned long long)NFS_FILEID(inode), status);
1254 		goto out;
1255 	}
1256 
1257 	if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
1258 		nfs_zap_acl_cache(inode);
1259 
1260 	nfs_setsecurity(inode, fattr);
1261 
1262 	dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
1263 		inode->i_sb->s_id,
1264 		(unsigned long long)NFS_FILEID(inode));
1265 
1266 out:
1267 	nfs_free_fattr(fattr);
1268 	trace_nfs_revalidate_inode_exit(inode, status);
1269 	return status;
1270 }
1271 
nfs_attribute_cache_expired(struct inode * inode)1272 int nfs_attribute_cache_expired(struct inode *inode)
1273 {
1274 	if (nfs_have_delegated_attributes(inode))
1275 		return 0;
1276 	return nfs_attribute_timeout(inode);
1277 }
1278 
1279 /**
1280  * nfs_revalidate_inode - Revalidate the inode attributes
1281  * @inode: pointer to inode struct
1282  * @flags: cache flags to check
1283  *
1284  * Updates inode attribute information by retrieving the data from the server.
1285  */
nfs_revalidate_inode(struct inode * inode,unsigned long flags)1286 int nfs_revalidate_inode(struct inode *inode, unsigned long flags)
1287 {
1288 	if (!nfs_check_cache_invalid(inode, flags))
1289 		return NFS_STALE(inode) ? -ESTALE : 0;
1290 	return __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1291 }
1292 EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
1293 
nfs_invalidate_mapping(struct inode * inode,struct address_space * mapping)1294 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
1295 {
1296 	int ret;
1297 
1298 	nfs_fscache_invalidate(inode, 0);
1299 	if (mapping->nrpages != 0) {
1300 		if (S_ISREG(inode->i_mode)) {
1301 			ret = nfs_sync_mapping(mapping);
1302 			if (ret < 0)
1303 				return ret;
1304 		}
1305 		ret = invalidate_inode_pages2(mapping);
1306 		if (ret < 0)
1307 			return ret;
1308 	}
1309 	nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
1310 
1311 	dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1312 			inode->i_sb->s_id,
1313 			(unsigned long long)NFS_FILEID(inode));
1314 	return 0;
1315 }
1316 
1317 /**
1318  * nfs_clear_invalid_mapping - Conditionally clear a mapping
1319  * @mapping: pointer to mapping
1320  *
1321  * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping.
1322  */
nfs_clear_invalid_mapping(struct address_space * mapping)1323 int nfs_clear_invalid_mapping(struct address_space *mapping)
1324 {
1325 	struct inode *inode = mapping->host;
1326 	struct nfs_inode *nfsi = NFS_I(inode);
1327 	unsigned long *bitlock = &nfsi->flags;
1328 	int ret = 0;
1329 
1330 	/*
1331 	 * We must clear NFS_INO_INVALID_DATA first to ensure that
1332 	 * invalidations that come in while we're shooting down the mappings
1333 	 * are respected. But, that leaves a race window where one revalidator
1334 	 * can clear the flag, and then another checks it before the mapping
1335 	 * gets invalidated. Fix that by serializing access to this part of
1336 	 * the function.
1337 	 *
1338 	 * At the same time, we need to allow other tasks to see whether we
1339 	 * might be in the middle of invalidating the pages, so we only set
1340 	 * the bit lock here if it looks like we're going to be doing that.
1341 	 */
1342 	for (;;) {
1343 		ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1344 					 nfs_wait_bit_killable,
1345 					 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
1346 		if (ret)
1347 			goto out;
1348 		smp_rmb(); /* pairs with smp_wmb() below */
1349 		if (test_bit(NFS_INO_INVALIDATING, bitlock))
1350 			continue;
1351 		/* pairs with nfs_set_cache_invalid()'s smp_store_release() */
1352 		if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA))
1353 			goto out;
1354 		/* Slow-path that double-checks with spinlock held */
1355 		spin_lock(&inode->i_lock);
1356 		if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1357 			spin_unlock(&inode->i_lock);
1358 			continue;
1359 		}
1360 		if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1361 			break;
1362 		spin_unlock(&inode->i_lock);
1363 		goto out;
1364 	}
1365 
1366 	set_bit(NFS_INO_INVALIDATING, bitlock);
1367 	smp_wmb();
1368 	nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
1369 	nfs_ooo_clear(nfsi);
1370 	spin_unlock(&inode->i_lock);
1371 	trace_nfs_invalidate_mapping_enter(inode);
1372 	ret = nfs_invalidate_mapping(inode, mapping);
1373 	trace_nfs_invalidate_mapping_exit(inode, ret);
1374 
1375 	clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
1376 	smp_mb__after_atomic();
1377 	wake_up_bit(bitlock, NFS_INO_INVALIDATING);
1378 out:
1379 	return ret;
1380 }
1381 
nfs_mapping_need_revalidate_inode(struct inode * inode)1382 bool nfs_mapping_need_revalidate_inode(struct inode *inode)
1383 {
1384 	return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) ||
1385 		NFS_STALE(inode);
1386 }
1387 
nfs_revalidate_mapping_rcu(struct inode * inode)1388 int nfs_revalidate_mapping_rcu(struct inode *inode)
1389 {
1390 	struct nfs_inode *nfsi = NFS_I(inode);
1391 	unsigned long *bitlock = &nfsi->flags;
1392 	int ret = 0;
1393 
1394 	if (IS_SWAPFILE(inode))
1395 		goto out;
1396 	if (nfs_mapping_need_revalidate_inode(inode)) {
1397 		ret = -ECHILD;
1398 		goto out;
1399 	}
1400 	spin_lock(&inode->i_lock);
1401 	if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1402 	    (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1403 		ret = -ECHILD;
1404 	spin_unlock(&inode->i_lock);
1405 out:
1406 	return ret;
1407 }
1408 
1409 /**
1410  * nfs_revalidate_mapping - Revalidate the pagecache
1411  * @inode: pointer to host inode
1412  * @mapping: pointer to mapping
1413  */
nfs_revalidate_mapping(struct inode * inode,struct address_space * mapping)1414 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
1415 {
1416 	/* swapfiles are not supposed to be shared. */
1417 	if (IS_SWAPFILE(inode))
1418 		return 0;
1419 
1420 	if (nfs_mapping_need_revalidate_inode(inode)) {
1421 		int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1422 		if (ret < 0)
1423 			return ret;
1424 	}
1425 
1426 	return nfs_clear_invalid_mapping(mapping);
1427 }
1428 
nfs_file_has_writers(struct nfs_inode * nfsi)1429 static bool nfs_file_has_writers(struct nfs_inode *nfsi)
1430 {
1431 	struct inode *inode = &nfsi->vfs_inode;
1432 
1433 	if (!S_ISREG(inode->i_mode))
1434 		return false;
1435 	if (list_empty(&nfsi->open_files))
1436 		return false;
1437 	return inode_is_open_for_write(inode);
1438 }
1439 
nfs_file_has_buffered_writers(struct nfs_inode * nfsi)1440 static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
1441 {
1442 	return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
1443 }
1444 
nfs_wcc_update_inode(struct inode * inode,struct nfs_fattr * fattr)1445 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1446 {
1447 	struct timespec64 ts;
1448 
1449 	if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1450 			&& (fattr->valid & NFS_ATTR_FATTR_CHANGE)
1451 			&& inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
1452 		inode_set_iversion_raw(inode, fattr->change_attr);
1453 		if (S_ISDIR(inode->i_mode))
1454 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1455 		else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1456 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
1457 	}
1458 	/* If we have atomic WCC data, we may update some attributes */
1459 	ts = inode_get_ctime(inode);
1460 	if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1461 			&& (fattr->valid & NFS_ATTR_FATTR_CTIME)
1462 			&& timespec64_equal(&ts, &fattr->pre_ctime)) {
1463 		inode_set_ctime_to_ts(inode, fattr->ctime);
1464 	}
1465 
1466 	ts = inode->i_mtime;
1467 	if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1468 			&& (fattr->valid & NFS_ATTR_FATTR_MTIME)
1469 			&& timespec64_equal(&ts, &fattr->pre_mtime)) {
1470 		inode->i_mtime = fattr->mtime;
1471 	}
1472 	if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1473 			&& (fattr->valid & NFS_ATTR_FATTR_SIZE)
1474 			&& i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
1475 			&& !nfs_have_writebacks(inode)) {
1476 		trace_nfs_size_wcc(inode, fattr->size);
1477 		i_size_write(inode, nfs_size_to_loff_t(fattr->size));
1478 	}
1479 }
1480 
1481 /**
1482  * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1483  * @inode: pointer to inode
1484  * @fattr: updated attributes
1485  *
1486  * Verifies the attribute cache. If we have just changed the attributes,
1487  * so that fattr carries weak cache consistency data, then it may
1488  * also update the ctime/mtime/change_attribute.
1489  */
nfs_check_inode_attributes(struct inode * inode,struct nfs_fattr * fattr)1490 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1491 {
1492 	struct nfs_inode *nfsi = NFS_I(inode);
1493 	loff_t cur_size, new_isize;
1494 	unsigned long invalid = 0;
1495 	struct timespec64 ts;
1496 
1497 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1498 		return 0;
1499 
1500 	if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1501 		/* Only a mounted-on-fileid? Just exit */
1502 		if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1503 			return 0;
1504 	/* Has the inode gone and changed behind our back? */
1505 	} else if (nfsi->fileid != fattr->fileid) {
1506 		/* Is this perhaps the mounted-on fileid? */
1507 		if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1508 		    nfsi->fileid == fattr->mounted_on_fileid)
1509 			return 0;
1510 		return -ESTALE;
1511 	}
1512 	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
1513 		return -ESTALE;
1514 
1515 
1516 	if (!nfs_file_has_buffered_writers(nfsi)) {
1517 		/* Verify a few of the more important attributes */
1518 		if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
1519 			invalid |= NFS_INO_INVALID_CHANGE;
1520 
1521 		ts = inode->i_mtime;
1522 		if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
1523 			invalid |= NFS_INO_INVALID_MTIME;
1524 
1525 		ts = inode_get_ctime(inode);
1526 		if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
1527 			invalid |= NFS_INO_INVALID_CTIME;
1528 
1529 		if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1530 			cur_size = i_size_read(inode);
1531 			new_isize = nfs_size_to_loff_t(fattr->size);
1532 			if (cur_size != new_isize)
1533 				invalid |= NFS_INO_INVALID_SIZE;
1534 		}
1535 	}
1536 
1537 	/* Have any file permissions changed? */
1538 	if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
1539 		invalid |= NFS_INO_INVALID_MODE;
1540 	if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
1541 		invalid |= NFS_INO_INVALID_OTHER;
1542 	if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
1543 		invalid |= NFS_INO_INVALID_OTHER;
1544 
1545 	/* Has the link count changed? */
1546 	if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
1547 		invalid |= NFS_INO_INVALID_NLINK;
1548 
1549 	ts = inode->i_atime;
1550 	if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
1551 		invalid |= NFS_INO_INVALID_ATIME;
1552 
1553 	if (invalid != 0)
1554 		nfs_set_cache_invalid(inode, invalid);
1555 
1556 	nfsi->read_cache_jiffies = fattr->time_start;
1557 	return 0;
1558 }
1559 
1560 static atomic_long_t nfs_attr_generation_counter;
1561 
nfs_read_attr_generation_counter(void)1562 static unsigned long nfs_read_attr_generation_counter(void)
1563 {
1564 	return atomic_long_read(&nfs_attr_generation_counter);
1565 }
1566 
nfs_inc_attr_generation_counter(void)1567 unsigned long nfs_inc_attr_generation_counter(void)
1568 {
1569 	return atomic_long_inc_return(&nfs_attr_generation_counter);
1570 }
1571 EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
1572 
nfs_fattr_init(struct nfs_fattr * fattr)1573 void nfs_fattr_init(struct nfs_fattr *fattr)
1574 {
1575 	fattr->valid = 0;
1576 	fattr->time_start = jiffies;
1577 	fattr->gencount = nfs_inc_attr_generation_counter();
1578 	fattr->owner_name = NULL;
1579 	fattr->group_name = NULL;
1580 	fattr->mdsthreshold = NULL;
1581 }
1582 EXPORT_SYMBOL_GPL(nfs_fattr_init);
1583 
1584 /**
1585  * nfs_fattr_set_barrier
1586  * @fattr: attributes
1587  *
1588  * Used to set a barrier after an attribute was updated. This
1589  * barrier ensures that older attributes from RPC calls that may
1590  * have raced with our update cannot clobber these new values.
1591  * Note that you are still responsible for ensuring that other
1592  * operations which change the attribute on the server do not
1593  * collide.
1594  */
nfs_fattr_set_barrier(struct nfs_fattr * fattr)1595 void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1596 {
1597 	fattr->gencount = nfs_inc_attr_generation_counter();
1598 }
1599 
nfs_alloc_fattr(void)1600 struct nfs_fattr *nfs_alloc_fattr(void)
1601 {
1602 	struct nfs_fattr *fattr;
1603 
1604 	fattr = kmalloc(sizeof(*fattr), GFP_KERNEL);
1605 	if (fattr != NULL) {
1606 		nfs_fattr_init(fattr);
1607 		fattr->label = NULL;
1608 	}
1609 	return fattr;
1610 }
1611 EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
1612 
nfs_alloc_fattr_with_label(struct nfs_server * server)1613 struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server)
1614 {
1615 	struct nfs_fattr *fattr = nfs_alloc_fattr();
1616 
1617 	if (!fattr)
1618 		return NULL;
1619 
1620 	fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
1621 	if (IS_ERR(fattr->label)) {
1622 		kfree(fattr);
1623 		return NULL;
1624 	}
1625 
1626 	return fattr;
1627 }
1628 EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label);
1629 
nfs_alloc_fhandle(void)1630 struct nfs_fh *nfs_alloc_fhandle(void)
1631 {
1632 	struct nfs_fh *fh;
1633 
1634 	fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
1635 	if (fh != NULL)
1636 		fh->size = 0;
1637 	return fh;
1638 }
1639 EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
1640 
1641 #ifdef NFS_DEBUG
1642 /*
1643  * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1644  *                             in the same way that wireshark does
1645  *
1646  * @fh: file handle
1647  *
1648  * For debugging only.
1649  */
_nfs_display_fhandle_hash(const struct nfs_fh * fh)1650 u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1651 {
1652 	/* wireshark uses 32-bit AUTODIN crc and does a bitwise
1653 	 * not on the result */
1654 	return nfs_fhandle_hash(fh);
1655 }
1656 EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
1657 
1658 /*
1659  * _nfs_display_fhandle - display an NFS file handle on the console
1660  *
1661  * @fh: file handle to display
1662  * @caption: display caption
1663  *
1664  * For debugging only.
1665  */
_nfs_display_fhandle(const struct nfs_fh * fh,const char * caption)1666 void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1667 {
1668 	unsigned short i;
1669 
1670 	if (fh == NULL || fh->size == 0) {
1671 		printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1672 		return;
1673 	}
1674 
1675 	printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1676 	       caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
1677 	for (i = 0; i < fh->size; i += 16) {
1678 		__be32 *pos = (__be32 *)&fh->data[i];
1679 
1680 		switch ((fh->size - i - 1) >> 2) {
1681 		case 0:
1682 			printk(KERN_DEFAULT " %08x\n",
1683 				be32_to_cpup(pos));
1684 			break;
1685 		case 1:
1686 			printk(KERN_DEFAULT " %08x %08x\n",
1687 				be32_to_cpup(pos), be32_to_cpup(pos + 1));
1688 			break;
1689 		case 2:
1690 			printk(KERN_DEFAULT " %08x %08x %08x\n",
1691 				be32_to_cpup(pos), be32_to_cpup(pos + 1),
1692 				be32_to_cpup(pos + 2));
1693 			break;
1694 		default:
1695 			printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1696 				be32_to_cpup(pos), be32_to_cpup(pos + 1),
1697 				be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1698 		}
1699 	}
1700 }
1701 EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
1702 #endif
1703 
1704 /**
1705  * nfs_inode_attrs_cmp_generic - compare attributes
1706  * @fattr: attributes
1707  * @inode: pointer to inode
1708  *
1709  * Attempt to divine whether or not an RPC call reply carrying stale
1710  * attributes got scheduled after another call carrying updated ones.
1711  * Note also the check for wraparound of 'attr_gencount'
1712  *
1713  * The function returns '1' if it thinks the attributes in @fattr are
1714  * more recent than the ones cached in @inode. Otherwise it returns
1715  * the value '0'.
1716  */
nfs_inode_attrs_cmp_generic(const struct nfs_fattr * fattr,const struct inode * inode)1717 static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr,
1718 				       const struct inode *inode)
1719 {
1720 	unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
1721 
1722 	return (long)(fattr->gencount - attr_gencount) > 0 ||
1723 	       (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
1724 }
1725 
1726 /**
1727  * nfs_inode_attrs_cmp_monotonic - compare attributes
1728  * @fattr: attributes
1729  * @inode: pointer to inode
1730  *
1731  * Attempt to divine whether or not an RPC call reply carrying stale
1732  * attributes got scheduled after another call carrying updated ones.
1733  *
1734  * We assume that the server observes monotonic semantics for
1735  * the change attribute, so a larger value means that the attributes in
1736  * @fattr are more recent, in which case the function returns the
1737  * value '1'.
1738  * A return value of '0' indicates no measurable change
1739  * A return value of '-1' means that the attributes in @inode are
1740  * more recent.
1741  */
nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr * fattr,const struct inode * inode)1742 static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr,
1743 					 const struct inode *inode)
1744 {
1745 	s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode);
1746 	if (diff > 0)
1747 		return 1;
1748 	return diff == 0 ? 0 : -1;
1749 }
1750 
1751 /**
1752  * nfs_inode_attrs_cmp_strict_monotonic - compare attributes
1753  * @fattr: attributes
1754  * @inode: pointer to inode
1755  *
1756  * Attempt to divine whether or not an RPC call reply carrying stale
1757  * attributes got scheduled after another call carrying updated ones.
1758  *
1759  * We assume that the server observes strictly monotonic semantics for
1760  * the change attribute, so a larger value means that the attributes in
1761  * @fattr are more recent, in which case the function returns the
1762  * value '1'.
1763  * A return value of '-1' means that the attributes in @inode are
1764  * more recent or unchanged.
1765  */
nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr * fattr,const struct inode * inode)1766 static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr,
1767 						const struct inode *inode)
1768 {
1769 	return  nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1;
1770 }
1771 
1772 /**
1773  * nfs_inode_attrs_cmp - compare attributes
1774  * @fattr: attributes
1775  * @inode: pointer to inode
1776  *
1777  * This function returns '1' if it thinks the attributes in @fattr are
1778  * more recent than the ones cached in @inode. It returns '-1' if
1779  * the attributes in @inode are more recent than the ones in @fattr,
1780  * and it returns 0 if not sure.
1781  */
nfs_inode_attrs_cmp(const struct nfs_fattr * fattr,const struct inode * inode)1782 static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr,
1783 			       const struct inode *inode)
1784 {
1785 	if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0)
1786 		return 1;
1787 	switch (NFS_SERVER(inode)->change_attr_type) {
1788 	case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1789 		break;
1790 	case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1791 		if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
1792 			break;
1793 		return nfs_inode_attrs_cmp_monotonic(fattr, inode);
1794 	default:
1795 		if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
1796 			break;
1797 		return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode);
1798 	}
1799 	return 0;
1800 }
1801 
1802 /**
1803  * nfs_inode_finish_partial_attr_update - complete a previous inode update
1804  * @fattr: attributes
1805  * @inode: pointer to inode
1806  *
1807  * Returns '1' if the last attribute update left the inode cached
1808  * attributes in a partially unrevalidated state, and @fattr
1809  * matches the change attribute of that partial update.
1810  * Otherwise returns '0'.
1811  */
nfs_inode_finish_partial_attr_update(const struct nfs_fattr * fattr,const struct inode * inode)1812 static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr,
1813 						const struct inode *inode)
1814 {
1815 	const unsigned long check_valid =
1816 		NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
1817 		NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
1818 		NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER |
1819 		NFS_INO_INVALID_NLINK;
1820 	unsigned long cache_validity = NFS_I(inode)->cache_validity;
1821 	enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type;
1822 
1823 	if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED &&
1824 	    !(cache_validity & NFS_INO_INVALID_CHANGE) &&
1825 	    (cache_validity & check_valid) != 0 &&
1826 	    (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1827 	    nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0)
1828 		return 1;
1829 	return 0;
1830 }
1831 
nfs_ooo_merge(struct nfs_inode * nfsi,u64 start,u64 end)1832 static void nfs_ooo_merge(struct nfs_inode *nfsi,
1833 			  u64 start, u64 end)
1834 {
1835 	int i, cnt;
1836 
1837 	if (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)
1838 		/* No point merging anything */
1839 		return;
1840 
1841 	if (!nfsi->ooo) {
1842 		nfsi->ooo = kmalloc(sizeof(*nfsi->ooo), GFP_ATOMIC);
1843 		if (!nfsi->ooo) {
1844 			nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
1845 			return;
1846 		}
1847 		nfsi->ooo->cnt = 0;
1848 	}
1849 
1850 	/* add this range, merging if possible */
1851 	cnt = nfsi->ooo->cnt;
1852 	for (i = 0; i < cnt; i++) {
1853 		if (end == nfsi->ooo->gap[i].start)
1854 			end = nfsi->ooo->gap[i].end;
1855 		else if (start == nfsi->ooo->gap[i].end)
1856 			start = nfsi->ooo->gap[i].start;
1857 		else
1858 			continue;
1859 		/* Remove 'i' from table and loop to insert the new range */
1860 		cnt -= 1;
1861 		nfsi->ooo->gap[i] = nfsi->ooo->gap[cnt];
1862 		i = -1;
1863 	}
1864 	if (start != end) {
1865 		if (cnt >= ARRAY_SIZE(nfsi->ooo->gap)) {
1866 			nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
1867 			kfree(nfsi->ooo);
1868 			nfsi->ooo = NULL;
1869 			return;
1870 		}
1871 		nfsi->ooo->gap[cnt].start = start;
1872 		nfsi->ooo->gap[cnt].end = end;
1873 		cnt += 1;
1874 	}
1875 	nfsi->ooo->cnt = cnt;
1876 }
1877 
nfs_ooo_record(struct nfs_inode * nfsi,struct nfs_fattr * fattr)1878 static void nfs_ooo_record(struct nfs_inode *nfsi,
1879 			   struct nfs_fattr *fattr)
1880 {
1881 	/* This reply was out-of-order, so record in the
1882 	 * pre/post change id, possibly cancelling
1883 	 * gaps created when iversion was jumpped forward.
1884 	 */
1885 	if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) &&
1886 	    (fattr->valid & NFS_ATTR_FATTR_PRECHANGE))
1887 		nfs_ooo_merge(nfsi,
1888 			      fattr->change_attr,
1889 			      fattr->pre_change_attr);
1890 }
1891 
nfs_refresh_inode_locked(struct inode * inode,struct nfs_fattr * fattr)1892 static int nfs_refresh_inode_locked(struct inode *inode,
1893 				    struct nfs_fattr *fattr)
1894 {
1895 	int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
1896 	int ret = 0;
1897 
1898 	trace_nfs_refresh_inode_enter(inode);
1899 
1900 	if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode))
1901 		ret = nfs_update_inode(inode, fattr);
1902 	else {
1903 		nfs_ooo_record(NFS_I(inode), fattr);
1904 
1905 		if (attr_cmp == 0)
1906 			ret = nfs_check_inode_attributes(inode, fattr);
1907 	}
1908 
1909 	trace_nfs_refresh_inode_exit(inode, ret);
1910 	return ret;
1911 }
1912 
1913 /**
1914  * nfs_refresh_inode - try to update the inode attribute cache
1915  * @inode: pointer to inode
1916  * @fattr: updated attributes
1917  *
1918  * Check that an RPC call that returned attributes has not overlapped with
1919  * other recent updates of the inode metadata, then decide whether it is
1920  * safe to do a full update of the inode attributes, or whether just to
1921  * call nfs_check_inode_attributes.
1922  */
nfs_refresh_inode(struct inode * inode,struct nfs_fattr * fattr)1923 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1924 {
1925 	int status;
1926 
1927 	if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1928 		return 0;
1929 	spin_lock(&inode->i_lock);
1930 	status = nfs_refresh_inode_locked(inode, fattr);
1931 	spin_unlock(&inode->i_lock);
1932 
1933 	return status;
1934 }
1935 EXPORT_SYMBOL_GPL(nfs_refresh_inode);
1936 
nfs_post_op_update_inode_locked(struct inode * inode,struct nfs_fattr * fattr,unsigned int invalid)1937 static int nfs_post_op_update_inode_locked(struct inode *inode,
1938 		struct nfs_fattr *fattr, unsigned int invalid)
1939 {
1940 	if (S_ISDIR(inode->i_mode))
1941 		invalid |= NFS_INO_INVALID_DATA;
1942 	nfs_set_cache_invalid(inode, invalid);
1943 	if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1944 		return 0;
1945 	return nfs_refresh_inode_locked(inode, fattr);
1946 }
1947 
1948 /**
1949  * nfs_post_op_update_inode - try to update the inode attribute cache
1950  * @inode: pointer to inode
1951  * @fattr: updated attributes
1952  *
1953  * After an operation that has changed the inode metadata, mark the
1954  * attribute cache as being invalid, then try to update it.
1955  *
1956  * NB: if the server didn't return any post op attributes, this
1957  * function will force the retrieval of attributes before the next
1958  * NFS request.  Thus it should be used only for operations that
1959  * are expected to change one or more attributes, to avoid
1960  * unnecessary NFS requests and trips through nfs_update_inode().
1961  */
nfs_post_op_update_inode(struct inode * inode,struct nfs_fattr * fattr)1962 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1963 {
1964 	int status;
1965 
1966 	spin_lock(&inode->i_lock);
1967 	nfs_fattr_set_barrier(fattr);
1968 	status = nfs_post_op_update_inode_locked(inode, fattr,
1969 			NFS_INO_INVALID_CHANGE
1970 			| NFS_INO_INVALID_CTIME
1971 			| NFS_INO_REVAL_FORCED);
1972 	spin_unlock(&inode->i_lock);
1973 
1974 	return status;
1975 }
1976 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
1977 
1978 /**
1979  * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
1980  * @inode: pointer to inode
1981  * @fattr: updated attributes
1982  *
1983  * After an operation that has changed the inode metadata, mark the
1984  * attribute cache as being invalid, then try to update it. Fake up
1985  * weak cache consistency data, if none exist.
1986  *
1987  * This function is mainly designed to be used by the ->write_done() functions.
1988  */
nfs_post_op_update_inode_force_wcc_locked(struct inode * inode,struct nfs_fattr * fattr)1989 int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
1990 {
1991 	int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
1992 	int status;
1993 
1994 	/* Don't do a WCC update if these attributes are already stale */
1995 	if (attr_cmp < 0)
1996 		return 0;
1997 	if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) {
1998 		/* Record the pre/post change info before clearing PRECHANGE */
1999 		nfs_ooo_record(NFS_I(inode), fattr);
2000 		fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
2001 				| NFS_ATTR_FATTR_PRESIZE
2002 				| NFS_ATTR_FATTR_PREMTIME
2003 				| NFS_ATTR_FATTR_PRECTIME);
2004 		goto out_noforce;
2005 	}
2006 	if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
2007 			(fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
2008 		fattr->pre_change_attr = inode_peek_iversion_raw(inode);
2009 		fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
2010 	}
2011 	if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
2012 			(fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
2013 		fattr->pre_ctime = inode_get_ctime(inode);
2014 		fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
2015 	}
2016 	if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
2017 			(fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
2018 		fattr->pre_mtime = inode->i_mtime;
2019 		fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
2020 	}
2021 	if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
2022 			(fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
2023 		fattr->pre_size = i_size_read(inode);
2024 		fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
2025 	}
2026 out_noforce:
2027 	status = nfs_post_op_update_inode_locked(inode, fattr,
2028 			NFS_INO_INVALID_CHANGE
2029 			| NFS_INO_INVALID_CTIME
2030 			| NFS_INO_INVALID_MTIME
2031 			| NFS_INO_INVALID_BLOCKS);
2032 	return status;
2033 }
2034 
2035 /**
2036  * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
2037  * @inode: pointer to inode
2038  * @fattr: updated attributes
2039  *
2040  * After an operation that has changed the inode metadata, mark the
2041  * attribute cache as being invalid, then try to update it. Fake up
2042  * weak cache consistency data, if none exist.
2043  *
2044  * This function is mainly designed to be used by the ->write_done() functions.
2045  */
nfs_post_op_update_inode_force_wcc(struct inode * inode,struct nfs_fattr * fattr)2046 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
2047 {
2048 	int status;
2049 
2050 	spin_lock(&inode->i_lock);
2051 	nfs_fattr_set_barrier(fattr);
2052 	status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
2053 	spin_unlock(&inode->i_lock);
2054 	return status;
2055 }
2056 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
2057 
2058 
2059 /*
2060  * Many nfs protocol calls return the new file attributes after
2061  * an operation.  Here we update the inode to reflect the state
2062  * of the server's inode.
2063  *
2064  * This is a bit tricky because we have to make sure all dirty pages
2065  * have been sent off to the server before calling invalidate_inode_pages.
2066  * To make sure no other process adds more write requests while we try
2067  * our best to flush them, we make them sleep during the attribute refresh.
2068  *
2069  * A very similar scenario holds for the dir cache.
2070  */
nfs_update_inode(struct inode * inode,struct nfs_fattr * fattr)2071 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
2072 {
2073 	struct nfs_server *server = NFS_SERVER(inode);
2074 	struct nfs_inode *nfsi = NFS_I(inode);
2075 	loff_t cur_isize, new_isize;
2076 	u64 fattr_supported = server->fattr_valid;
2077 	unsigned long invalid = 0;
2078 	unsigned long now = jiffies;
2079 	unsigned long save_cache_validity;
2080 	bool have_writers = nfs_file_has_buffered_writers(nfsi);
2081 	bool cache_revalidated = true;
2082 	bool attr_changed = false;
2083 	bool have_delegation;
2084 
2085 	dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
2086 			__func__, inode->i_sb->s_id, inode->i_ino,
2087 			nfs_display_fhandle_hash(NFS_FH(inode)),
2088 			atomic_read(&inode->i_count), fattr->valid);
2089 
2090 	if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
2091 		/* Only a mounted-on-fileid? Just exit */
2092 		if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
2093 			return 0;
2094 	/* Has the inode gone and changed behind our back? */
2095 	} else if (nfsi->fileid != fattr->fileid) {
2096 		/* Is this perhaps the mounted-on fileid? */
2097 		if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
2098 		    nfsi->fileid == fattr->mounted_on_fileid)
2099 			return 0;
2100 		printk(KERN_ERR "NFS: server %s error: fileid changed\n"
2101 			"fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
2102 			NFS_SERVER(inode)->nfs_client->cl_hostname,
2103 			inode->i_sb->s_id, (long long)nfsi->fileid,
2104 			(long long)fattr->fileid);
2105 		goto out_err;
2106 	}
2107 
2108 	/*
2109 	 * Make sure the inode's type hasn't changed.
2110 	 */
2111 	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
2112 		/*
2113 		* Big trouble! The inode has become a different object.
2114 		*/
2115 		printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
2116 				__func__, inode->i_ino, inode->i_mode, fattr->mode);
2117 		goto out_err;
2118 	}
2119 
2120 	/* Update the fsid? */
2121 	if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
2122 			!nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
2123 			!IS_AUTOMOUNT(inode))
2124 		server->fsid = fattr->fsid;
2125 
2126 	/* Save the delegation state before clearing cache_validity */
2127 	have_delegation = nfs_have_delegated_attributes(inode);
2128 
2129 	/*
2130 	 * Update the read time so we don't revalidate too often.
2131 	 */
2132 	nfsi->read_cache_jiffies = fattr->time_start;
2133 
2134 	save_cache_validity = nfsi->cache_validity;
2135 	nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
2136 			| NFS_INO_INVALID_ATIME
2137 			| NFS_INO_REVAL_FORCED
2138 			| NFS_INO_INVALID_BLOCKS);
2139 
2140 	/* Do atomic weak cache consistency updates */
2141 	nfs_wcc_update_inode(inode, fattr);
2142 
2143 	if (pnfs_layoutcommit_outstanding(inode)) {
2144 		nfsi->cache_validity |=
2145 			save_cache_validity &
2146 			(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
2147 			 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
2148 			 NFS_INO_INVALID_BLOCKS);
2149 		cache_revalidated = false;
2150 	}
2151 
2152 	/* More cache consistency checks */
2153 	if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
2154 		if (!have_writers && nfsi->ooo && nfsi->ooo->cnt == 1 &&
2155 		    nfsi->ooo->gap[0].end == inode_peek_iversion_raw(inode)) {
2156 			/* There is one remaining gap that hasn't been
2157 			 * merged into iversion - do that now.
2158 			 */
2159 			inode_set_iversion_raw(inode, nfsi->ooo->gap[0].start);
2160 			kfree(nfsi->ooo);
2161 			nfsi->ooo = NULL;
2162 		}
2163 		if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
2164 			/* Could it be a race with writeback? */
2165 			if (!(have_writers || have_delegation)) {
2166 				invalid |= NFS_INO_INVALID_DATA
2167 					| NFS_INO_INVALID_ACCESS
2168 					| NFS_INO_INVALID_ACL
2169 					| NFS_INO_INVALID_XATTR;
2170 				/* Force revalidate of all attributes */
2171 				save_cache_validity |= NFS_INO_INVALID_CTIME
2172 					| NFS_INO_INVALID_MTIME
2173 					| NFS_INO_INVALID_SIZE
2174 					| NFS_INO_INVALID_BLOCKS
2175 					| NFS_INO_INVALID_NLINK
2176 					| NFS_INO_INVALID_MODE
2177 					| NFS_INO_INVALID_OTHER;
2178 				if (S_ISDIR(inode->i_mode))
2179 					nfs_force_lookup_revalidate(inode);
2180 				attr_changed = true;
2181 				dprintk("NFS: change_attr change on server for file %s/%ld\n",
2182 						inode->i_sb->s_id,
2183 						inode->i_ino);
2184 			} else if (!have_delegation) {
2185 				nfs_ooo_record(nfsi, fattr);
2186 				nfs_ooo_merge(nfsi, inode_peek_iversion_raw(inode),
2187 					      fattr->change_attr);
2188 			}
2189 			inode_set_iversion_raw(inode, fattr->change_attr);
2190 		}
2191 	} else {
2192 		nfsi->cache_validity |=
2193 			save_cache_validity & NFS_INO_INVALID_CHANGE;
2194 		if (!have_delegation ||
2195 		    (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0)
2196 			cache_revalidated = false;
2197 	}
2198 
2199 	if (fattr->valid & NFS_ATTR_FATTR_MTIME)
2200 		inode->i_mtime = fattr->mtime;
2201 	else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
2202 		nfsi->cache_validity |=
2203 			save_cache_validity & NFS_INO_INVALID_MTIME;
2204 
2205 	if (fattr->valid & NFS_ATTR_FATTR_CTIME)
2206 		inode_set_ctime_to_ts(inode, fattr->ctime);
2207 	else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
2208 		nfsi->cache_validity |=
2209 			save_cache_validity & NFS_INO_INVALID_CTIME;
2210 
2211 	/* Check if our cached file size is stale */
2212 	if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
2213 		new_isize = nfs_size_to_loff_t(fattr->size);
2214 		cur_isize = i_size_read(inode);
2215 		if (new_isize != cur_isize && !have_delegation) {
2216 			/* Do we perhaps have any outstanding writes, or has
2217 			 * the file grown beyond our last write? */
2218 			if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
2219 				trace_nfs_size_update(inode, new_isize);
2220 				i_size_write(inode, new_isize);
2221 				if (!have_writers)
2222 					invalid |= NFS_INO_INVALID_DATA;
2223 			}
2224 		}
2225 		if (new_isize == 0 &&
2226 		    !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED |
2227 				      NFS_ATTR_FATTR_BLOCKS_USED))) {
2228 			fattr->du.nfs3.used = 0;
2229 			fattr->valid |= NFS_ATTR_FATTR_SPACE_USED;
2230 		}
2231 	} else
2232 		nfsi->cache_validity |=
2233 			save_cache_validity & NFS_INO_INVALID_SIZE;
2234 
2235 	if (fattr->valid & NFS_ATTR_FATTR_ATIME)
2236 		inode->i_atime = fattr->atime;
2237 	else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
2238 		nfsi->cache_validity |=
2239 			save_cache_validity & NFS_INO_INVALID_ATIME;
2240 
2241 	if (fattr->valid & NFS_ATTR_FATTR_MODE) {
2242 		if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
2243 			umode_t newmode = inode->i_mode & S_IFMT;
2244 			newmode |= fattr->mode & S_IALLUGO;
2245 			inode->i_mode = newmode;
2246 			invalid |= NFS_INO_INVALID_ACCESS
2247 				| NFS_INO_INVALID_ACL;
2248 		}
2249 	} else if (fattr_supported & NFS_ATTR_FATTR_MODE)
2250 		nfsi->cache_validity |=
2251 			save_cache_validity & NFS_INO_INVALID_MODE;
2252 
2253 	if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
2254 		if (!uid_eq(inode->i_uid, fattr->uid)) {
2255 			invalid |= NFS_INO_INVALID_ACCESS
2256 				| NFS_INO_INVALID_ACL;
2257 			inode->i_uid = fattr->uid;
2258 		}
2259 	} else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
2260 		nfsi->cache_validity |=
2261 			save_cache_validity & NFS_INO_INVALID_OTHER;
2262 
2263 	if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
2264 		if (!gid_eq(inode->i_gid, fattr->gid)) {
2265 			invalid |= NFS_INO_INVALID_ACCESS
2266 				| NFS_INO_INVALID_ACL;
2267 			inode->i_gid = fattr->gid;
2268 		}
2269 	} else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
2270 		nfsi->cache_validity |=
2271 			save_cache_validity & NFS_INO_INVALID_OTHER;
2272 
2273 	if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2274 		if (inode->i_nlink != fattr->nlink)
2275 			set_nlink(inode, fattr->nlink);
2276 	} else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
2277 		nfsi->cache_validity |=
2278 			save_cache_validity & NFS_INO_INVALID_NLINK;
2279 
2280 	if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
2281 		/*
2282 		 * report the blocks in 512byte units
2283 		 */
2284 		inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
2285 	} else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED)
2286 		nfsi->cache_validity |=
2287 			save_cache_validity & NFS_INO_INVALID_BLOCKS;
2288 
2289 	if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
2290 		inode->i_blocks = fattr->du.nfs2.blocks;
2291 	else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED)
2292 		nfsi->cache_validity |=
2293 			save_cache_validity & NFS_INO_INVALID_BLOCKS;
2294 
2295 	/* Update attrtimeo value if we're out of the unstable period */
2296 	if (attr_changed) {
2297 		nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
2298 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
2299 		nfsi->attrtimeo_timestamp = now;
2300 		/* Set barrier to be more recent than all outstanding updates */
2301 		nfsi->attr_gencount = nfs_inc_attr_generation_counter();
2302 	} else {
2303 		if (cache_revalidated) {
2304 			if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2305 				nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2306 				nfsi->attrtimeo <<= 1;
2307 				if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2308 					nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2309 			}
2310 			nfsi->attrtimeo_timestamp = now;
2311 		}
2312 		/* Set the barrier to be more recent than this fattr */
2313 		if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
2314 			nfsi->attr_gencount = fattr->gencount;
2315 	}
2316 
2317 	/* Don't invalidate the data if we were to blame */
2318 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2319 				|| S_ISLNK(inode->i_mode)))
2320 		invalid &= ~NFS_INO_INVALID_DATA;
2321 	nfs_set_cache_invalid(inode, invalid);
2322 
2323 	return 0;
2324  out_err:
2325 	/*
2326 	 * No need to worry about unhashing the dentry, as the
2327 	 * lookup validation will know that the inode is bad.
2328 	 * (But we fall through to invalidate the caches.)
2329 	 */
2330 	nfs_set_inode_stale_locked(inode);
2331 	return -ESTALE;
2332 }
2333 
nfs_alloc_inode(struct super_block * sb)2334 struct inode *nfs_alloc_inode(struct super_block *sb)
2335 {
2336 	struct nfs_inode *nfsi;
2337 	nfsi = alloc_inode_sb(sb, nfs_inode_cachep, GFP_KERNEL);
2338 	if (!nfsi)
2339 		return NULL;
2340 	nfsi->flags = 0UL;
2341 	nfsi->cache_validity = 0UL;
2342 	nfsi->ooo = NULL;
2343 #if IS_ENABLED(CONFIG_NFS_V4)
2344 	nfsi->nfs4_acl = NULL;
2345 #endif /* CONFIG_NFS_V4 */
2346 #ifdef CONFIG_NFS_V4_2
2347 	nfsi->xattr_cache = NULL;
2348 #endif
2349 	nfs_netfs_inode_init(nfsi);
2350 
2351 	return &nfsi->vfs_inode;
2352 }
2353 EXPORT_SYMBOL_GPL(nfs_alloc_inode);
2354 
nfs_free_inode(struct inode * inode)2355 void nfs_free_inode(struct inode *inode)
2356 {
2357 	kfree(NFS_I(inode)->ooo);
2358 	kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2359 }
2360 EXPORT_SYMBOL_GPL(nfs_free_inode);
2361 
nfs4_init_once(struct nfs_inode * nfsi)2362 static inline void nfs4_init_once(struct nfs_inode *nfsi)
2363 {
2364 #if IS_ENABLED(CONFIG_NFS_V4)
2365 	INIT_LIST_HEAD(&nfsi->open_states);
2366 	nfsi->delegation = NULL;
2367 	init_rwsem(&nfsi->rwsem);
2368 	nfsi->layout = NULL;
2369 #endif
2370 }
2371 
init_once(void * foo)2372 static void init_once(void *foo)
2373 {
2374 	struct nfs_inode *nfsi = foo;
2375 
2376 	inode_init_once(&nfsi->vfs_inode);
2377 	INIT_LIST_HEAD(&nfsi->open_files);
2378 	INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2379 	INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
2380 	nfs4_init_once(nfsi);
2381 }
2382 
nfs_init_inodecache(void)2383 static int __init nfs_init_inodecache(void)
2384 {
2385 	nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2386 					     sizeof(struct nfs_inode),
2387 					     0, (SLAB_RECLAIM_ACCOUNT|
2388 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2389 					     init_once);
2390 	if (nfs_inode_cachep == NULL)
2391 		return -ENOMEM;
2392 
2393 	return 0;
2394 }
2395 
nfs_destroy_inodecache(void)2396 static void nfs_destroy_inodecache(void)
2397 {
2398 	/*
2399 	 * Make sure all delayed rcu free inodes are flushed before we
2400 	 * destroy cache.
2401 	 */
2402 	rcu_barrier();
2403 	kmem_cache_destroy(nfs_inode_cachep);
2404 }
2405 
2406 struct workqueue_struct *nfsiod_workqueue;
2407 EXPORT_SYMBOL_GPL(nfsiod_workqueue);
2408 
2409 /*
2410  * start up the nfsiod workqueue
2411  */
nfsiod_start(void)2412 static int nfsiod_start(void)
2413 {
2414 	struct workqueue_struct *wq;
2415 	dprintk("RPC:       creating workqueue nfsiod\n");
2416 	wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
2417 	if (wq == NULL)
2418 		return -ENOMEM;
2419 	nfsiod_workqueue = wq;
2420 	return 0;
2421 }
2422 
2423 /*
2424  * Destroy the nfsiod workqueue
2425  */
nfsiod_stop(void)2426 static void nfsiod_stop(void)
2427 {
2428 	struct workqueue_struct *wq;
2429 
2430 	wq = nfsiod_workqueue;
2431 	if (wq == NULL)
2432 		return;
2433 	nfsiod_workqueue = NULL;
2434 	destroy_workqueue(wq);
2435 }
2436 
2437 unsigned int nfs_net_id;
2438 EXPORT_SYMBOL_GPL(nfs_net_id);
2439 
nfs_net_init(struct net * net)2440 static int nfs_net_init(struct net *net)
2441 {
2442 	struct nfs_net *nn = net_generic(net, nfs_net_id);
2443 
2444 	nfs_clients_init(net);
2445 
2446 	if (!rpc_proc_register(net, &nn->rpcstats)) {
2447 		nfs_clients_exit(net);
2448 		return -ENOMEM;
2449 	}
2450 
2451 	return nfs_fs_proc_net_init(net);
2452 }
2453 
nfs_net_exit(struct net * net)2454 static void nfs_net_exit(struct net *net)
2455 {
2456 	rpc_proc_unregister(net, "nfs");
2457 	nfs_fs_proc_net_exit(net);
2458 	nfs_clients_exit(net);
2459 }
2460 
2461 static struct pernet_operations nfs_net_ops = {
2462 	.init = nfs_net_init,
2463 	.exit = nfs_net_exit,
2464 	.id   = &nfs_net_id,
2465 	.size = sizeof(struct nfs_net),
2466 };
2467 
2468 /*
2469  * Initialize NFS
2470  */
init_nfs_fs(void)2471 static int __init init_nfs_fs(void)
2472 {
2473 	int err;
2474 
2475 	err = nfs_sysfs_init();
2476 	if (err < 0)
2477 		goto out10;
2478 
2479 	err = register_pernet_subsys(&nfs_net_ops);
2480 	if (err < 0)
2481 		goto out9;
2482 
2483 	err = nfsiod_start();
2484 	if (err)
2485 		goto out7;
2486 
2487 	err = nfs_fs_proc_init();
2488 	if (err)
2489 		goto out6;
2490 
2491 	err = nfs_init_nfspagecache();
2492 	if (err)
2493 		goto out5;
2494 
2495 	err = nfs_init_inodecache();
2496 	if (err)
2497 		goto out4;
2498 
2499 	err = nfs_init_readpagecache();
2500 	if (err)
2501 		goto out3;
2502 
2503 	err = nfs_init_writepagecache();
2504 	if (err)
2505 		goto out2;
2506 
2507 	err = nfs_init_directcache();
2508 	if (err)
2509 		goto out1;
2510 
2511 	err = register_nfs_fs();
2512 	if (err)
2513 		goto out0;
2514 
2515 	return 0;
2516 out0:
2517 	nfs_destroy_directcache();
2518 out1:
2519 	nfs_destroy_writepagecache();
2520 out2:
2521 	nfs_destroy_readpagecache();
2522 out3:
2523 	nfs_destroy_inodecache();
2524 out4:
2525 	nfs_destroy_nfspagecache();
2526 out5:
2527 	nfs_fs_proc_exit();
2528 out6:
2529 	nfsiod_stop();
2530 out7:
2531 	unregister_pernet_subsys(&nfs_net_ops);
2532 out9:
2533 	nfs_sysfs_exit();
2534 out10:
2535 	return err;
2536 }
2537 
exit_nfs_fs(void)2538 static void __exit exit_nfs_fs(void)
2539 {
2540 	nfs_destroy_directcache();
2541 	nfs_destroy_writepagecache();
2542 	nfs_destroy_readpagecache();
2543 	nfs_destroy_inodecache();
2544 	nfs_destroy_nfspagecache();
2545 	unregister_pernet_subsys(&nfs_net_ops);
2546 	unregister_nfs_fs();
2547 	nfs_fs_proc_exit();
2548 	nfsiod_stop();
2549 	nfs_sysfs_exit();
2550 }
2551 
2552 /* Not quite true; I just maintain it */
2553 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2554 MODULE_LICENSE("GPL");
2555 module_param(enable_ino64, bool, 0644);
2556 
2557 module_init(init_nfs_fs)
2558 module_exit(exit_nfs_fs)
2559