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