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