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