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