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