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