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