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