• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_killable(int mode)75 static int nfs_wait_killable(int mode)
76 {
77 	freezable_schedule_unsafe();
78 	if (signal_pending_state(mode, current))
79 		return -ERESTARTSYS;
80 	return 0;
81 }
82 
nfs_wait_bit_killable(struct wait_bit_key * key,int mode)83 int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
84 {
85 	return nfs_wait_killable(mode);
86 }
87 EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
88 
89 /**
90  * nfs_compat_user_ino64 - returns the user-visible inode number
91  * @fileid: 64-bit fileid
92  *
93  * This function returns a 32-bit inode number if the boot parameter
94  * nfs.enable_ino64 is zero.
95  */
nfs_compat_user_ino64(u64 fileid)96 u64 nfs_compat_user_ino64(u64 fileid)
97 {
98 #ifdef CONFIG_COMPAT
99 	compat_ulong_t ino;
100 #else
101 	unsigned long ino;
102 #endif
103 
104 	if (enable_ino64)
105 		return fileid;
106 	ino = fileid;
107 	if (sizeof(ino) < sizeof(fileid))
108 		ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
109 	return ino;
110 }
111 
nfs_drop_inode(struct inode * inode)112 int nfs_drop_inode(struct inode *inode)
113 {
114 	return NFS_STALE(inode) || generic_drop_inode(inode);
115 }
116 EXPORT_SYMBOL_GPL(nfs_drop_inode);
117 
nfs_clear_inode(struct inode * inode)118 void nfs_clear_inode(struct inode *inode)
119 {
120 	/*
121 	 * The following should never happen...
122 	 */
123 	WARN_ON_ONCE(nfs_have_writebacks(inode));
124 	WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
125 	nfs_zap_acl_cache(inode);
126 	nfs_access_zap_cache(inode);
127 	nfs_fscache_clear_inode(inode);
128 }
129 EXPORT_SYMBOL_GPL(nfs_clear_inode);
130 
nfs_evict_inode(struct inode * inode)131 void nfs_evict_inode(struct inode *inode)
132 {
133 	truncate_inode_pages_final(&inode->i_data);
134 	clear_inode(inode);
135 	nfs_clear_inode(inode);
136 }
137 
nfs_sync_inode(struct inode * inode)138 int nfs_sync_inode(struct inode *inode)
139 {
140 	inode_dio_wait(inode);
141 	return nfs_wb_all(inode);
142 }
143 EXPORT_SYMBOL_GPL(nfs_sync_inode);
144 
145 /**
146  * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
147  * @mapping: pointer to struct address_space
148  */
nfs_sync_mapping(struct address_space * mapping)149 int nfs_sync_mapping(struct address_space *mapping)
150 {
151 	int ret = 0;
152 
153 	if (mapping->nrpages != 0) {
154 		unmap_mapping_range(mapping, 0, 0, 0);
155 		ret = nfs_wb_all(mapping->host);
156 	}
157 	return ret;
158 }
159 
nfs_attribute_timeout(struct inode * inode)160 static int nfs_attribute_timeout(struct inode *inode)
161 {
162 	struct nfs_inode *nfsi = NFS_I(inode);
163 
164 	return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
165 }
166 
nfs_check_cache_invalid_delegated(struct inode * inode,unsigned long flags)167 static bool nfs_check_cache_invalid_delegated(struct inode *inode, unsigned long flags)
168 {
169 	unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
170 
171 	/* Special case for the pagecache or access cache */
172 	if (flags == NFS_INO_REVAL_PAGECACHE &&
173 	    !(cache_validity & NFS_INO_REVAL_FORCED))
174 		return false;
175 	return (cache_validity & flags) != 0;
176 }
177 
nfs_check_cache_invalid_not_delegated(struct inode * inode,unsigned long flags)178 static bool nfs_check_cache_invalid_not_delegated(struct inode *inode, unsigned long flags)
179 {
180 	unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
181 
182 	if ((cache_validity & flags) != 0)
183 		return true;
184 	if (nfs_attribute_timeout(inode))
185 		return true;
186 	return false;
187 }
188 
nfs_check_cache_invalid(struct inode * inode,unsigned long flags)189 bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
190 {
191 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
192 		return nfs_check_cache_invalid_delegated(inode, flags);
193 
194 	return nfs_check_cache_invalid_not_delegated(inode, flags);
195 }
196 EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
197 
nfs_set_cache_invalid(struct inode * inode,unsigned long flags)198 static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
199 {
200 	struct nfs_inode *nfsi = NFS_I(inode);
201 	bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
202 
203 	if (have_delegation) {
204 		if (!(flags & NFS_INO_REVAL_FORCED))
205 			flags &= ~NFS_INO_INVALID_OTHER;
206 		flags &= ~(NFS_INO_INVALID_CHANGE
207 				| NFS_INO_INVALID_SIZE
208 				| NFS_INO_REVAL_PAGECACHE
209 				| NFS_INO_INVALID_XATTR);
210 	} else if (flags & NFS_INO_REVAL_PAGECACHE)
211 		flags |= NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE;
212 
213 	if (inode->i_mapping->nrpages == 0)
214 		flags &= ~(NFS_INO_INVALID_DATA|NFS_INO_DATA_INVAL_DEFER);
215 	nfsi->cache_validity |= flags;
216 	if (flags & NFS_INO_INVALID_DATA)
217 		nfs_fscache_invalidate(inode);
218 }
219 
220 /*
221  * Invalidate the local caches
222  */
nfs_zap_caches_locked(struct inode * inode)223 static void nfs_zap_caches_locked(struct inode *inode)
224 {
225 	struct nfs_inode *nfsi = NFS_I(inode);
226 	int mode = inode->i_mode;
227 
228 	nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
229 
230 	nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
231 	nfsi->attrtimeo_timestamp = jiffies;
232 
233 	memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
234 	if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
235 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
236 					| NFS_INO_INVALID_DATA
237 					| NFS_INO_INVALID_ACCESS
238 					| NFS_INO_INVALID_ACL
239 					| NFS_INO_INVALID_XATTR
240 					| NFS_INO_REVAL_PAGECACHE);
241 	} else
242 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
243 					| NFS_INO_INVALID_ACCESS
244 					| NFS_INO_INVALID_ACL
245 					| NFS_INO_INVALID_XATTR
246 					| NFS_INO_REVAL_PAGECACHE);
247 	nfs_zap_label_cache_locked(nfsi);
248 }
249 
nfs_zap_caches(struct inode * inode)250 void nfs_zap_caches(struct inode *inode)
251 {
252 	spin_lock(&inode->i_lock);
253 	nfs_zap_caches_locked(inode);
254 	spin_unlock(&inode->i_lock);
255 }
256 
nfs_zap_mapping(struct inode * inode,struct address_space * mapping)257 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
258 {
259 	if (mapping->nrpages != 0) {
260 		spin_lock(&inode->i_lock);
261 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
262 		spin_unlock(&inode->i_lock);
263 	}
264 }
265 
nfs_zap_acl_cache(struct inode * inode)266 void nfs_zap_acl_cache(struct inode *inode)
267 {
268 	void (*clear_acl_cache)(struct inode *);
269 
270 	clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
271 	if (clear_acl_cache != NULL)
272 		clear_acl_cache(inode);
273 	spin_lock(&inode->i_lock);
274 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
275 	spin_unlock(&inode->i_lock);
276 }
277 EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
278 
nfs_invalidate_atime(struct inode * inode)279 void nfs_invalidate_atime(struct inode *inode)
280 {
281 	spin_lock(&inode->i_lock);
282 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
283 	spin_unlock(&inode->i_lock);
284 }
285 EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
286 
287 /*
288  * Invalidate, but do not unhash, the inode.
289  * NB: must be called with inode->i_lock held!
290  */
nfs_set_inode_stale_locked(struct inode * inode)291 static void nfs_set_inode_stale_locked(struct inode *inode)
292 {
293 	set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
294 	nfs_zap_caches_locked(inode);
295 	trace_nfs_set_inode_stale(inode);
296 }
297 
nfs_set_inode_stale(struct inode * inode)298 void nfs_set_inode_stale(struct inode *inode)
299 {
300 	spin_lock(&inode->i_lock);
301 	nfs_set_inode_stale_locked(inode);
302 	spin_unlock(&inode->i_lock);
303 }
304 
305 struct nfs_find_desc {
306 	struct nfs_fh		*fh;
307 	struct nfs_fattr	*fattr;
308 };
309 
310 /*
311  * In NFSv3 we can have 64bit inode numbers. In order to support
312  * this, and re-exported directories (also seen in NFSv2)
313  * we are forced to allow 2 different inodes to have the same
314  * i_ino.
315  */
316 static int
nfs_find_actor(struct inode * inode,void * opaque)317 nfs_find_actor(struct inode *inode, void *opaque)
318 {
319 	struct nfs_find_desc	*desc = (struct nfs_find_desc *)opaque;
320 	struct nfs_fh		*fh = desc->fh;
321 	struct nfs_fattr	*fattr = desc->fattr;
322 
323 	if (NFS_FILEID(inode) != fattr->fileid)
324 		return 0;
325 	if (inode_wrong_type(inode, fattr->mode))
326 		return 0;
327 	if (nfs_compare_fh(NFS_FH(inode), fh))
328 		return 0;
329 	if (is_bad_inode(inode) || NFS_STALE(inode))
330 		return 0;
331 	return 1;
332 }
333 
334 static int
nfs_init_locked(struct inode * inode,void * opaque)335 nfs_init_locked(struct inode *inode, void *opaque)
336 {
337 	struct nfs_find_desc	*desc = (struct nfs_find_desc *)opaque;
338 	struct nfs_fattr	*fattr = desc->fattr;
339 
340 	set_nfs_fileid(inode, fattr->fileid);
341 	inode->i_mode = fattr->mode;
342 	nfs_copy_fh(NFS_FH(inode), desc->fh);
343 	return 0;
344 }
345 
346 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
nfs_clear_label_invalid(struct inode * inode)347 static void nfs_clear_label_invalid(struct inode *inode)
348 {
349 	spin_lock(&inode->i_lock);
350 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
351 	spin_unlock(&inode->i_lock);
352 }
353 
nfs_setsecurity(struct inode * inode,struct nfs_fattr * fattr,struct nfs4_label * label)354 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
355 					struct nfs4_label *label)
356 {
357 	int error;
358 
359 	if (label == NULL)
360 		return;
361 
362 	if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
363 		error = security_inode_notifysecctx(inode, label->label,
364 				label->len);
365 		if (error)
366 			printk(KERN_ERR "%s() %s %d "
367 					"security_inode_notifysecctx() %d\n",
368 					__func__,
369 					(char *)label->label,
370 					label->len, error);
371 		nfs_clear_label_invalid(inode);
372 	}
373 }
374 
nfs4_label_alloc(struct nfs_server * server,gfp_t flags)375 struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
376 {
377 	struct nfs4_label *label = NULL;
378 	int minor_version = server->nfs_client->cl_minorversion;
379 
380 	if (minor_version < 2)
381 		return label;
382 
383 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
384 		return label;
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,struct nfs4_label * label)401 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
402 					struct nfs4_label *label)
403 {
404 }
405 #endif
406 EXPORT_SYMBOL_GPL(nfs_setsecurity);
407 
408 /* Search for inode identified by fh, fileid and i_mode in inode cache. */
409 struct inode *
nfs_ilookup(struct super_block * sb,struct nfs_fattr * fattr,struct nfs_fh * fh)410 nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
411 {
412 	struct nfs_find_desc desc = {
413 		.fh	= fh,
414 		.fattr	= fattr,
415 	};
416 	struct inode *inode;
417 	unsigned long hash;
418 
419 	if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
420 	    !(fattr->valid & NFS_ATTR_FATTR_TYPE))
421 		return NULL;
422 
423 	hash = nfs_fattr_to_ino_t(fattr);
424 	inode = ilookup5(sb, hash, nfs_find_actor, &desc);
425 
426 	dprintk("%s: returning %p\n", __func__, inode);
427 	return inode;
428 }
429 
430 /*
431  * This is our front-end to iget that looks up inodes by file handle
432  * instead of inode number.
433  */
434 struct inode *
nfs_fhget(struct super_block * sb,struct nfs_fh * fh,struct nfs_fattr * fattr,struct nfs4_label * label)435 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
436 {
437 	struct nfs_find_desc desc = {
438 		.fh	= fh,
439 		.fattr	= fattr
440 	};
441 	struct inode *inode = ERR_PTR(-ENOENT);
442 	unsigned long hash;
443 
444 	nfs_attr_check_mountpoint(sb, fattr);
445 
446 	if (nfs_attr_use_mounted_on_fileid(fattr))
447 		fattr->fileid = fattr->mounted_on_fileid;
448 	else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
449 		goto out_no_inode;
450 	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
451 		goto out_no_inode;
452 
453 	hash = nfs_fattr_to_ino_t(fattr);
454 
455 	inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
456 	if (inode == NULL) {
457 		inode = ERR_PTR(-ENOMEM);
458 		goto out_no_inode;
459 	}
460 
461 	if (inode->i_state & I_NEW) {
462 		struct nfs_inode *nfsi = NFS_I(inode);
463 		unsigned long now = jiffies;
464 
465 		/* We set i_ino for the few things that still rely on it,
466 		 * such as stat(2) */
467 		inode->i_ino = hash;
468 
469 		/* We can't support update_atime(), since the server will reset it */
470 		inode->i_flags |= S_NOATIME|S_NOCMTIME;
471 		inode->i_mode = fattr->mode;
472 		nfsi->cache_validity = 0;
473 		if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
474 				&& nfs_server_capable(inode, NFS_CAP_MODE))
475 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
476 		/* Why so? Because we want revalidate for devices/FIFOs, and
477 		 * that's precisely what we have in nfs_file_inode_operations.
478 		 */
479 		inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
480 		if (S_ISREG(inode->i_mode)) {
481 			inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
482 			inode->i_data.a_ops = &nfs_file_aops;
483 		} else if (S_ISDIR(inode->i_mode)) {
484 			inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
485 			inode->i_fop = &nfs_dir_operations;
486 			inode->i_data.a_ops = &nfs_dir_aops;
487 			/* Deal with crossing mountpoints */
488 			if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
489 					fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
490 				if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
491 					inode->i_op = &nfs_referral_inode_operations;
492 				else
493 					inode->i_op = &nfs_mountpoint_inode_operations;
494 				inode->i_fop = NULL;
495 				inode->i_flags |= S_AUTOMOUNT;
496 			}
497 		} else if (S_ISLNK(inode->i_mode)) {
498 			inode->i_op = &nfs_symlink_inode_operations;
499 			inode_nohighmem(inode);
500 		} else
501 			init_special_inode(inode, inode->i_mode, fattr->rdev);
502 
503 		memset(&inode->i_atime, 0, sizeof(inode->i_atime));
504 		memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
505 		memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
506 		inode_set_iversion_raw(inode, 0);
507 		inode->i_size = 0;
508 		clear_nlink(inode);
509 		inode->i_uid = make_kuid(&init_user_ns, -2);
510 		inode->i_gid = make_kgid(&init_user_ns, -2);
511 		inode->i_blocks = 0;
512 		memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
513 		nfsi->write_io = 0;
514 		nfsi->read_io = 0;
515 
516 		nfsi->read_cache_jiffies = fattr->time_start;
517 		nfsi->attr_gencount = fattr->gencount;
518 		if (fattr->valid & NFS_ATTR_FATTR_ATIME)
519 			inode->i_atime = fattr->atime;
520 		else if (nfs_server_capable(inode, NFS_CAP_ATIME))
521 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
522 		if (fattr->valid & NFS_ATTR_FATTR_MTIME)
523 			inode->i_mtime = fattr->mtime;
524 		else if (nfs_server_capable(inode, NFS_CAP_MTIME))
525 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
526 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
527 			inode->i_ctime = fattr->ctime;
528 		else if (nfs_server_capable(inode, NFS_CAP_CTIME))
529 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
530 		if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
531 			inode_set_iversion_raw(inode, fattr->change_attr);
532 		else
533 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
534 		if (fattr->valid & NFS_ATTR_FATTR_SIZE)
535 			inode->i_size = nfs_size_to_loff_t(fattr->size);
536 		else
537 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
538 		if (fattr->valid & NFS_ATTR_FATTR_NLINK)
539 			set_nlink(inode, fattr->nlink);
540 		else if (nfs_server_capable(inode, NFS_CAP_NLINK))
541 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
542 		if (fattr->valid & NFS_ATTR_FATTR_OWNER)
543 			inode->i_uid = fattr->uid;
544 		else if (nfs_server_capable(inode, NFS_CAP_OWNER))
545 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
546 		if (fattr->valid & NFS_ATTR_FATTR_GROUP)
547 			inode->i_gid = fattr->gid;
548 		else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
549 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
550 		if (nfs_server_capable(inode, NFS_CAP_XATTR))
551 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
552 		if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
553 			inode->i_blocks = fattr->du.nfs2.blocks;
554 		if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
555 			/*
556 			 * report the blocks in 512byte units
557 			 */
558 			inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
559 		}
560 
561 		if (nfsi->cache_validity != 0)
562 			nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
563 
564 		nfs_setsecurity(inode, fattr, label);
565 
566 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
567 		nfsi->attrtimeo_timestamp = now;
568 		nfsi->access_cache = RB_ROOT;
569 
570 		nfs_fscache_init_inode(inode);
571 
572 		unlock_new_inode(inode);
573 	} else {
574 		int err = nfs_refresh_inode(inode, fattr);
575 		if (err < 0) {
576 			iput(inode);
577 			inode = ERR_PTR(err);
578 			goto out_no_inode;
579 		}
580 	}
581 	dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
582 		inode->i_sb->s_id,
583 		(unsigned long long)NFS_FILEID(inode),
584 		nfs_display_fhandle_hash(fh),
585 		atomic_read(&inode->i_count));
586 
587 out:
588 	return inode;
589 
590 out_no_inode:
591 	dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
592 	goto out;
593 }
594 EXPORT_SYMBOL_GPL(nfs_fhget);
595 
596 #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)
597 
598 int
nfs_setattr(struct dentry * dentry,struct iattr * attr)599 nfs_setattr(struct dentry *dentry, struct iattr *attr)
600 {
601 	struct inode *inode = d_inode(dentry);
602 	struct nfs_fattr *fattr;
603 	int error = 0;
604 
605 	nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
606 
607 	/* skip mode change if it's just for clearing setuid/setgid */
608 	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
609 		attr->ia_valid &= ~ATTR_MODE;
610 
611 	if (attr->ia_valid & ATTR_SIZE) {
612 		BUG_ON(!S_ISREG(inode->i_mode));
613 
614 		error = inode_newsize_ok(inode, attr->ia_size);
615 		if (error)
616 			return error;
617 
618 		if (attr->ia_size == i_size_read(inode))
619 			attr->ia_valid &= ~ATTR_SIZE;
620 	}
621 
622 	/* Optimization: if the end result is no change, don't RPC */
623 	attr->ia_valid &= NFS_VALID_ATTRS;
624 	if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
625 		return 0;
626 
627 	trace_nfs_setattr_enter(inode);
628 
629 	/* Write all dirty data */
630 	if (S_ISREG(inode->i_mode))
631 		nfs_sync_inode(inode);
632 
633 	fattr = nfs_alloc_fattr();
634 	if (fattr == NULL) {
635 		error = -ENOMEM;
636 		goto out;
637 	}
638 
639 	error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
640 	if (error == 0)
641 		error = nfs_refresh_inode(inode, fattr);
642 	nfs_free_fattr(fattr);
643 out:
644 	trace_nfs_setattr_exit(inode, error);
645 	return error;
646 }
647 EXPORT_SYMBOL_GPL(nfs_setattr);
648 
649 /**
650  * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
651  * @inode: inode of the file used
652  * @offset: file offset to start truncating
653  *
654  * This is a copy of the common vmtruncate, but with the locking
655  * corrected to take into account the fact that NFS requires
656  * inode->i_size to be updated under the inode->i_lock.
657  * Note: must be called with inode->i_lock held!
658  */
nfs_vmtruncate(struct inode * inode,loff_t offset)659 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
660 {
661 	int err;
662 
663 	err = inode_newsize_ok(inode, offset);
664 	if (err)
665 		goto out;
666 
667 	i_size_write(inode, offset);
668 	/* Optimisation */
669 	if (offset == 0)
670 		NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_DATA |
671 				NFS_INO_DATA_INVAL_DEFER);
672 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
673 
674 	spin_unlock(&inode->i_lock);
675 	truncate_pagecache(inode, offset);
676 	spin_lock(&inode->i_lock);
677 out:
678 	return err;
679 }
680 
681 /**
682  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
683  * @inode: pointer to struct inode
684  * @attr: pointer to struct iattr
685  * @fattr: pointer to struct nfs_fattr
686  *
687  * Note: we do this in the *proc.c in order to ensure that
688  *       it works for things like exclusive creates too.
689  */
nfs_setattr_update_inode(struct inode * inode,struct iattr * attr,struct nfs_fattr * fattr)690 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
691 		struct nfs_fattr *fattr)
692 {
693 	/* Barrier: bump the attribute generation count. */
694 	nfs_fattr_set_barrier(fattr);
695 
696 	spin_lock(&inode->i_lock);
697 	NFS_I(inode)->attr_gencount = fattr->gencount;
698 	if ((attr->ia_valid & ATTR_SIZE) != 0) {
699 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
700 		nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
701 		nfs_vmtruncate(inode, attr->ia_size);
702 	}
703 	if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
704 		NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
705 		if ((attr->ia_valid & ATTR_MODE) != 0) {
706 			int mode = attr->ia_mode & S_IALLUGO;
707 			mode |= inode->i_mode & ~S_IALLUGO;
708 			inode->i_mode = mode;
709 		}
710 		if ((attr->ia_valid & ATTR_UID) != 0)
711 			inode->i_uid = attr->ia_uid;
712 		if ((attr->ia_valid & ATTR_GID) != 0)
713 			inode->i_gid = attr->ia_gid;
714 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
715 			inode->i_ctime = fattr->ctime;
716 		else
717 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
718 					| NFS_INO_INVALID_CTIME);
719 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
720 				| NFS_INO_INVALID_ACL);
721 	}
722 	if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
723 		NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
724 				| NFS_INO_INVALID_CTIME);
725 		if (fattr->valid & NFS_ATTR_FATTR_ATIME)
726 			inode->i_atime = fattr->atime;
727 		else if (attr->ia_valid & ATTR_ATIME_SET)
728 			inode->i_atime = attr->ia_atime;
729 		else
730 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
731 
732 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
733 			inode->i_ctime = fattr->ctime;
734 		else
735 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
736 					| NFS_INO_INVALID_CTIME);
737 	}
738 	if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
739 		NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
740 				| NFS_INO_INVALID_CTIME);
741 		if (fattr->valid & NFS_ATTR_FATTR_MTIME)
742 			inode->i_mtime = fattr->mtime;
743 		else if (attr->ia_valid & ATTR_MTIME_SET)
744 			inode->i_mtime = attr->ia_mtime;
745 		else
746 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
747 
748 		if (fattr->valid & NFS_ATTR_FATTR_CTIME)
749 			inode->i_ctime = fattr->ctime;
750 		else
751 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
752 					| NFS_INO_INVALID_CTIME);
753 	}
754 	if (fattr->valid)
755 		nfs_update_inode(inode, fattr);
756 	spin_unlock(&inode->i_lock);
757 }
758 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
759 
nfs_readdirplus_parent_cache_miss(struct dentry * dentry)760 static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
761 {
762 	struct dentry *parent;
763 
764 	if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
765 		return;
766 	parent = dget_parent(dentry);
767 	nfs_force_use_readdirplus(d_inode(parent));
768 	dput(parent);
769 }
770 
nfs_readdirplus_parent_cache_hit(struct dentry * dentry)771 static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
772 {
773 	struct dentry *parent;
774 
775 	if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
776 		return;
777 	parent = dget_parent(dentry);
778 	nfs_advise_use_readdirplus(d_inode(parent));
779 	dput(parent);
780 }
781 
nfs_need_revalidate_inode(struct inode * inode)782 static bool nfs_need_revalidate_inode(struct inode *inode)
783 {
784 	if (NFS_I(inode)->cache_validity &
785 			(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
786 		return true;
787 	if (nfs_attribute_cache_expired(inode))
788 		return true;
789 	return false;
790 }
791 
nfs_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)792 int nfs_getattr(const struct path *path, struct kstat *stat,
793 		u32 request_mask, unsigned int query_flags)
794 {
795 	struct inode *inode = d_inode(path->dentry);
796 	struct nfs_server *server = NFS_SERVER(inode);
797 	unsigned long cache_validity;
798 	int err = 0;
799 	bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
800 	bool do_update = false;
801 
802 	trace_nfs_getattr_enter(inode);
803 
804 	if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
805 		nfs_readdirplus_parent_cache_hit(path->dentry);
806 		goto out_no_update;
807 	}
808 
809 	/* Flush out writes to the server in order to update c/mtime.  */
810 	if ((request_mask & (STATX_CTIME|STATX_MTIME)) &&
811 			S_ISREG(inode->i_mode)) {
812 		err = filemap_write_and_wait(inode->i_mapping);
813 		if (err)
814 			goto out;
815 	}
816 
817 	/*
818 	 * We may force a getattr if the user cares about atime.
819 	 *
820 	 * Note that we only have to check the vfsmount flags here:
821 	 *  - NFS always sets S_NOATIME by so checking it would give a
822 	 *    bogus result
823 	 *  - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
824 	 *    no point in checking those.
825 	 */
826 	if ((path->mnt->mnt_flags & MNT_NOATIME) ||
827 	    ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
828 		request_mask &= ~STATX_ATIME;
829 
830 	/* Is the user requesting attributes that might need revalidation? */
831 	if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
832 					STATX_MTIME|STATX_UID|STATX_GID|
833 					STATX_SIZE|STATX_BLOCKS)))
834 		goto out_no_revalidate;
835 
836 	/* Check whether the cached attributes are stale */
837 	do_update |= force_sync || nfs_attribute_cache_expired(inode);
838 	cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
839 	do_update |= cache_validity &
840 		(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL);
841 	if (request_mask & STATX_ATIME)
842 		do_update |= cache_validity & NFS_INO_INVALID_ATIME;
843 	if (request_mask & (STATX_CTIME|STATX_MTIME))
844 		do_update |= cache_validity & NFS_INO_REVAL_PAGECACHE;
845 	if (request_mask & STATX_BLOCKS)
846 		do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
847 	if (do_update) {
848 		/* Update the attribute cache */
849 		if (!(server->flags & NFS_MOUNT_NOAC))
850 			nfs_readdirplus_parent_cache_miss(path->dentry);
851 		else
852 			nfs_readdirplus_parent_cache_hit(path->dentry);
853 		err = __nfs_revalidate_inode(server, inode);
854 		if (err)
855 			goto out;
856 	} else
857 		nfs_readdirplus_parent_cache_hit(path->dentry);
858 out_no_revalidate:
859 	/* Only return attributes that were revalidated. */
860 	stat->result_mask &= request_mask;
861 out_no_update:
862 	generic_fillattr(inode, stat);
863 	stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
864 	if (S_ISDIR(inode->i_mode))
865 		stat->blksize = NFS_SERVER(inode)->dtsize;
866 out:
867 	trace_nfs_getattr_exit(inode, err);
868 	return err;
869 }
870 EXPORT_SYMBOL_GPL(nfs_getattr);
871 
nfs_init_lock_context(struct nfs_lock_context * l_ctx)872 static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
873 {
874 	refcount_set(&l_ctx->count, 1);
875 	l_ctx->lockowner = current->files;
876 	INIT_LIST_HEAD(&l_ctx->list);
877 	atomic_set(&l_ctx->io_count, 0);
878 }
879 
__nfs_find_lock_context(struct nfs_open_context * ctx)880 static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
881 {
882 	struct nfs_lock_context *pos;
883 
884 	list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
885 		if (pos->lockowner != current->files)
886 			continue;
887 		if (refcount_inc_not_zero(&pos->count))
888 			return pos;
889 	}
890 	return NULL;
891 }
892 
nfs_get_lock_context(struct nfs_open_context * ctx)893 struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
894 {
895 	struct nfs_lock_context *res, *new = NULL;
896 	struct inode *inode = d_inode(ctx->dentry);
897 
898 	rcu_read_lock();
899 	res = __nfs_find_lock_context(ctx);
900 	rcu_read_unlock();
901 	if (res == NULL) {
902 		new = kmalloc(sizeof(*new), GFP_KERNEL);
903 		if (new == NULL)
904 			return ERR_PTR(-ENOMEM);
905 		nfs_init_lock_context(new);
906 		spin_lock(&inode->i_lock);
907 		res = __nfs_find_lock_context(ctx);
908 		if (res == NULL) {
909 			new->open_context = get_nfs_open_context(ctx);
910 			if (new->open_context) {
911 				list_add_tail_rcu(&new->list,
912 						&ctx->lock_context.list);
913 				res = new;
914 				new = NULL;
915 			} else
916 				res = ERR_PTR(-EBADF);
917 		}
918 		spin_unlock(&inode->i_lock);
919 		kfree(new);
920 	}
921 	return res;
922 }
923 EXPORT_SYMBOL_GPL(nfs_get_lock_context);
924 
nfs_put_lock_context(struct nfs_lock_context * l_ctx)925 void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
926 {
927 	struct nfs_open_context *ctx = l_ctx->open_context;
928 	struct inode *inode = d_inode(ctx->dentry);
929 
930 	if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
931 		return;
932 	list_del_rcu(&l_ctx->list);
933 	spin_unlock(&inode->i_lock);
934 	put_nfs_open_context(ctx);
935 	kfree_rcu(l_ctx, rcu_head);
936 }
937 EXPORT_SYMBOL_GPL(nfs_put_lock_context);
938 
939 /**
940  * nfs_close_context - Common close_context() routine NFSv2/v3
941  * @ctx: pointer to context
942  * @is_sync: is this a synchronous close
943  *
944  * Ensure that the attributes are up to date if we're mounted
945  * with close-to-open semantics and we have cached data that will
946  * need to be revalidated on open.
947  */
nfs_close_context(struct nfs_open_context * ctx,int is_sync)948 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
949 {
950 	struct nfs_inode *nfsi;
951 	struct inode *inode;
952 	struct nfs_server *server;
953 
954 	if (!(ctx->mode & FMODE_WRITE))
955 		return;
956 	if (!is_sync)
957 		return;
958 	inode = d_inode(ctx->dentry);
959 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
960 		return;
961 	nfsi = NFS_I(inode);
962 	if (inode->i_mapping->nrpages == 0)
963 		return;
964 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
965 		return;
966 	if (!list_empty(&nfsi->open_files))
967 		return;
968 	server = NFS_SERVER(inode);
969 	if (server->flags & NFS_MOUNT_NOCTO)
970 		return;
971 	nfs_revalidate_inode(server, inode);
972 }
973 EXPORT_SYMBOL_GPL(nfs_close_context);
974 
alloc_nfs_open_context(struct dentry * dentry,fmode_t f_mode,struct file * filp)975 struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
976 						fmode_t f_mode,
977 						struct file *filp)
978 {
979 	struct nfs_open_context *ctx;
980 
981 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
982 	if (!ctx)
983 		return ERR_PTR(-ENOMEM);
984 	nfs_sb_active(dentry->d_sb);
985 	ctx->dentry = dget(dentry);
986 	if (filp)
987 		ctx->cred = get_cred(filp->f_cred);
988 	else
989 		ctx->cred = get_current_cred();
990 	ctx->ll_cred = NULL;
991 	ctx->state = NULL;
992 	ctx->mode = f_mode;
993 	ctx->flags = 0;
994 	ctx->error = 0;
995 	ctx->flock_owner = (fl_owner_t)filp;
996 	nfs_init_lock_context(&ctx->lock_context);
997 	ctx->lock_context.open_context = ctx;
998 	INIT_LIST_HEAD(&ctx->list);
999 	ctx->mdsthreshold = NULL;
1000 	return ctx;
1001 }
1002 EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
1003 
get_nfs_open_context(struct nfs_open_context * ctx)1004 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1005 {
1006 	if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1007 		return ctx;
1008 	return NULL;
1009 }
1010 EXPORT_SYMBOL_GPL(get_nfs_open_context);
1011 
__put_nfs_open_context(struct nfs_open_context * ctx,int is_sync)1012 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1013 {
1014 	struct inode *inode = d_inode(ctx->dentry);
1015 	struct super_block *sb = ctx->dentry->d_sb;
1016 
1017 	if (!refcount_dec_and_test(&ctx->lock_context.count))
1018 		return;
1019 	if (!list_empty(&ctx->list)) {
1020 		spin_lock(&inode->i_lock);
1021 		list_del_rcu(&ctx->list);
1022 		spin_unlock(&inode->i_lock);
1023 	}
1024 	if (inode != NULL)
1025 		NFS_PROTO(inode)->close_context(ctx, is_sync);
1026 	put_cred(ctx->cred);
1027 	dput(ctx->dentry);
1028 	nfs_sb_deactive(sb);
1029 	put_rpccred(ctx->ll_cred);
1030 	kfree(ctx->mdsthreshold);
1031 	kfree_rcu(ctx, rcu_head);
1032 }
1033 
put_nfs_open_context(struct nfs_open_context * ctx)1034 void put_nfs_open_context(struct nfs_open_context *ctx)
1035 {
1036 	__put_nfs_open_context(ctx, 0);
1037 }
1038 EXPORT_SYMBOL_GPL(put_nfs_open_context);
1039 
put_nfs_open_context_sync(struct nfs_open_context * ctx)1040 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1041 {
1042 	__put_nfs_open_context(ctx, 1);
1043 }
1044 
1045 /*
1046  * Ensure that mmap has a recent RPC credential for use when writing out
1047  * shared pages
1048  */
nfs_inode_attach_open_context(struct nfs_open_context * ctx)1049 void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
1050 {
1051 	struct inode *inode = d_inode(ctx->dentry);
1052 	struct nfs_inode *nfsi = NFS_I(inode);
1053 
1054 	spin_lock(&inode->i_lock);
1055 	if (list_empty(&nfsi->open_files) &&
1056 	    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
1057 		nfsi->cache_validity |= NFS_INO_INVALID_DATA |
1058 			NFS_INO_REVAL_FORCED;
1059 	list_add_tail_rcu(&ctx->list, &nfsi->open_files);
1060 	spin_unlock(&inode->i_lock);
1061 }
1062 EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1063 
nfs_file_set_open_context(struct file * filp,struct nfs_open_context * ctx)1064 void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1065 {
1066 	filp->private_data = get_nfs_open_context(ctx);
1067 	set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1068 	if (list_empty(&ctx->list))
1069 		nfs_inode_attach_open_context(ctx);
1070 }
1071 EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
1072 
1073 /*
1074  * Given an inode, search for an open context with the desired characteristics
1075  */
nfs_find_open_context(struct inode * inode,const struct cred * cred,fmode_t mode)1076 struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
1077 {
1078 	struct nfs_inode *nfsi = NFS_I(inode);
1079 	struct nfs_open_context *pos, *ctx = NULL;
1080 
1081 	rcu_read_lock();
1082 	list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
1083 		if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
1084 			continue;
1085 		if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1086 			continue;
1087 		if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
1088 			continue;
1089 		ctx = get_nfs_open_context(pos);
1090 		if (ctx)
1091 			break;
1092 	}
1093 	rcu_read_unlock();
1094 	return ctx;
1095 }
1096 
nfs_file_clear_open_context(struct file * filp)1097 void nfs_file_clear_open_context(struct file *filp)
1098 {
1099 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1100 
1101 	if (ctx) {
1102 		struct inode *inode = d_inode(ctx->dentry);
1103 
1104 		clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1105 		/*
1106 		 * We fatal error on write before. Try to writeback
1107 		 * every page again.
1108 		 */
1109 		if (ctx->error < 0)
1110 			invalidate_inode_pages2(inode->i_mapping);
1111 		filp->private_data = NULL;
1112 		put_nfs_open_context_sync(ctx);
1113 	}
1114 }
1115 
1116 /*
1117  * These allocate and release file read/write context information.
1118  */
nfs_open(struct inode * inode,struct file * filp)1119 int nfs_open(struct inode *inode, struct file *filp)
1120 {
1121 	struct nfs_open_context *ctx;
1122 
1123 	ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
1124 	if (IS_ERR(ctx))
1125 		return PTR_ERR(ctx);
1126 	nfs_file_set_open_context(filp, ctx);
1127 	put_nfs_open_context(ctx);
1128 	nfs_fscache_open_file(inode, filp);
1129 	return 0;
1130 }
1131 EXPORT_SYMBOL_GPL(nfs_open);
1132 
1133 /*
1134  * This function is called whenever some part of NFS notices that
1135  * the cached attributes have to be refreshed.
1136  */
1137 int
__nfs_revalidate_inode(struct nfs_server * server,struct inode * inode)1138 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1139 {
1140 	int		 status = -ESTALE;
1141 	struct nfs4_label *label = NULL;
1142 	struct nfs_fattr *fattr = NULL;
1143 	struct nfs_inode *nfsi = NFS_I(inode);
1144 
1145 	dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1146 		inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
1147 
1148 	trace_nfs_revalidate_inode_enter(inode);
1149 
1150 	if (is_bad_inode(inode))
1151 		goto out;
1152 	if (NFS_STALE(inode))
1153 		goto out;
1154 
1155 	/* pNFS: Attributes aren't updated until we layoutcommit */
1156 	if (S_ISREG(inode->i_mode)) {
1157 		status = pnfs_sync_inode(inode, false);
1158 		if (status)
1159 			goto out;
1160 	}
1161 
1162 	status = -ENOMEM;
1163 	fattr = nfs_alloc_fattr();
1164 	if (fattr == NULL)
1165 		goto out;
1166 
1167 	nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1168 
1169 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1170 	if (IS_ERR(label)) {
1171 		status = PTR_ERR(label);
1172 		goto out;
1173 	}
1174 
1175 	status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
1176 			label, inode);
1177 	if (status != 0) {
1178 		dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
1179 			 inode->i_sb->s_id,
1180 			 (unsigned long long)NFS_FILEID(inode), status);
1181 		switch (status) {
1182 		case -ETIMEDOUT:
1183 			/* A soft timeout occurred. Use cached information? */
1184 			if (server->flags & NFS_MOUNT_SOFTREVAL)
1185 				status = 0;
1186 			break;
1187 		case -ESTALE:
1188 			if (!S_ISDIR(inode->i_mode))
1189 				nfs_set_inode_stale(inode);
1190 			else
1191 				nfs_zap_caches(inode);
1192 		}
1193 		goto err_out;
1194 	}
1195 
1196 	status = nfs_refresh_inode(inode, fattr);
1197 	if (status) {
1198 		dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
1199 			 inode->i_sb->s_id,
1200 			 (unsigned long long)NFS_FILEID(inode), status);
1201 		goto err_out;
1202 	}
1203 
1204 	if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
1205 		nfs_zap_acl_cache(inode);
1206 
1207 	nfs_setsecurity(inode, fattr, label);
1208 
1209 	dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
1210 		inode->i_sb->s_id,
1211 		(unsigned long long)NFS_FILEID(inode));
1212 
1213 err_out:
1214 	nfs4_label_free(label);
1215 out:
1216 	nfs_free_fattr(fattr);
1217 	trace_nfs_revalidate_inode_exit(inode, status);
1218 	return status;
1219 }
1220 
nfs_attribute_cache_expired(struct inode * inode)1221 int nfs_attribute_cache_expired(struct inode *inode)
1222 {
1223 	if (nfs_have_delegated_attributes(inode))
1224 		return 0;
1225 	return nfs_attribute_timeout(inode);
1226 }
1227 
1228 /**
1229  * nfs_revalidate_inode - Revalidate the inode attributes
1230  * @server: pointer to nfs_server struct
1231  * @inode: pointer to inode struct
1232  *
1233  * Updates inode attribute information by retrieving the data from the server.
1234  */
nfs_revalidate_inode(struct nfs_server * server,struct inode * inode)1235 int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1236 {
1237 	if (!nfs_need_revalidate_inode(inode))
1238 		return NFS_STALE(inode) ? -ESTALE : 0;
1239 	return __nfs_revalidate_inode(server, inode);
1240 }
1241 EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
1242 
nfs_invalidate_mapping(struct inode * inode,struct address_space * mapping)1243 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
1244 {
1245 	struct nfs_inode *nfsi = NFS_I(inode);
1246 	int ret;
1247 
1248 	if (mapping->nrpages != 0) {
1249 		if (S_ISREG(inode->i_mode)) {
1250 			ret = nfs_sync_mapping(mapping);
1251 			if (ret < 0)
1252 				return ret;
1253 		}
1254 		ret = invalidate_inode_pages2(mapping);
1255 		if (ret < 0)
1256 			return ret;
1257 	}
1258 	if (S_ISDIR(inode->i_mode)) {
1259 		spin_lock(&inode->i_lock);
1260 		memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
1261 		spin_unlock(&inode->i_lock);
1262 	}
1263 	nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
1264 	nfs_fscache_wait_on_invalidate(inode);
1265 
1266 	dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1267 			inode->i_sb->s_id,
1268 			(unsigned long long)NFS_FILEID(inode));
1269 	return 0;
1270 }
1271 
nfs_mapping_need_revalidate_inode(struct inode * inode)1272 bool nfs_mapping_need_revalidate_inode(struct inode *inode)
1273 {
1274 	return nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE) ||
1275 		NFS_STALE(inode);
1276 }
1277 
nfs_revalidate_mapping_rcu(struct inode * inode)1278 int nfs_revalidate_mapping_rcu(struct inode *inode)
1279 {
1280 	struct nfs_inode *nfsi = NFS_I(inode);
1281 	unsigned long *bitlock = &nfsi->flags;
1282 	int ret = 0;
1283 
1284 	if (IS_SWAPFILE(inode))
1285 		goto out;
1286 	if (nfs_mapping_need_revalidate_inode(inode)) {
1287 		ret = -ECHILD;
1288 		goto out;
1289 	}
1290 	spin_lock(&inode->i_lock);
1291 	if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1292 	    (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1293 		ret = -ECHILD;
1294 	spin_unlock(&inode->i_lock);
1295 out:
1296 	return ret;
1297 }
1298 
1299 /**
1300  * nfs_revalidate_mapping - Revalidate the pagecache
1301  * @inode: pointer to host inode
1302  * @mapping: pointer to mapping
1303  */
nfs_revalidate_mapping(struct inode * inode,struct address_space * mapping)1304 int nfs_revalidate_mapping(struct inode *inode,
1305 		struct address_space *mapping)
1306 {
1307 	struct nfs_inode *nfsi = NFS_I(inode);
1308 	unsigned long *bitlock = &nfsi->flags;
1309 	int ret = 0;
1310 
1311 	/* swapfiles are not supposed to be shared. */
1312 	if (IS_SWAPFILE(inode))
1313 		goto out;
1314 
1315 	if (nfs_mapping_need_revalidate_inode(inode)) {
1316 		ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1317 		if (ret < 0)
1318 			goto out;
1319 	}
1320 
1321 	/*
1322 	 * We must clear NFS_INO_INVALID_DATA first to ensure that
1323 	 * invalidations that come in while we're shooting down the mappings
1324 	 * are respected. But, that leaves a race window where one revalidator
1325 	 * can clear the flag, and then another checks it before the mapping
1326 	 * gets invalidated. Fix that by serializing access to this part of
1327 	 * the function.
1328 	 *
1329 	 * At the same time, we need to allow other tasks to see whether we
1330 	 * might be in the middle of invalidating the pages, so we only set
1331 	 * the bit lock here if it looks like we're going to be doing that.
1332 	 */
1333 	for (;;) {
1334 		ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1335 					 nfs_wait_bit_killable, TASK_KILLABLE);
1336 		if (ret)
1337 			goto out;
1338 		spin_lock(&inode->i_lock);
1339 		if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1340 			spin_unlock(&inode->i_lock);
1341 			continue;
1342 		}
1343 		if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1344 			break;
1345 		spin_unlock(&inode->i_lock);
1346 		goto out;
1347 	}
1348 
1349 	set_bit(NFS_INO_INVALIDATING, bitlock);
1350 	smp_wmb();
1351 	nfsi->cache_validity &= ~(NFS_INO_INVALID_DATA|
1352 			NFS_INO_DATA_INVAL_DEFER);
1353 	spin_unlock(&inode->i_lock);
1354 	trace_nfs_invalidate_mapping_enter(inode);
1355 	ret = nfs_invalidate_mapping(inode, mapping);
1356 	trace_nfs_invalidate_mapping_exit(inode, ret);
1357 
1358 	clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
1359 	smp_mb__after_atomic();
1360 	wake_up_bit(bitlock, NFS_INO_INVALIDATING);
1361 out:
1362 	return ret;
1363 }
1364 
nfs_file_has_writers(struct nfs_inode * nfsi)1365 static bool nfs_file_has_writers(struct nfs_inode *nfsi)
1366 {
1367 	struct inode *inode = &nfsi->vfs_inode;
1368 
1369 	if (!S_ISREG(inode->i_mode))
1370 		return false;
1371 	if (list_empty(&nfsi->open_files))
1372 		return false;
1373 	return inode_is_open_for_write(inode);
1374 }
1375 
nfs_file_has_buffered_writers(struct nfs_inode * nfsi)1376 static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
1377 {
1378 	return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
1379 }
1380 
nfs_wcc_update_inode(struct inode * inode,struct nfs_fattr * fattr)1381 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1382 {
1383 	struct timespec64 ts;
1384 
1385 	if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1386 			&& (fattr->valid & NFS_ATTR_FATTR_CHANGE)
1387 			&& inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
1388 		inode_set_iversion_raw(inode, fattr->change_attr);
1389 		if (S_ISDIR(inode->i_mode))
1390 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1391 		else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1392 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
1393 	}
1394 	/* If we have atomic WCC data, we may update some attributes */
1395 	ts = inode->i_ctime;
1396 	if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1397 			&& (fattr->valid & NFS_ATTR_FATTR_CTIME)
1398 			&& timespec64_equal(&ts, &fattr->pre_ctime)) {
1399 		inode->i_ctime = fattr->ctime;
1400 	}
1401 
1402 	ts = inode->i_mtime;
1403 	if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1404 			&& (fattr->valid & NFS_ATTR_FATTR_MTIME)
1405 			&& timespec64_equal(&ts, &fattr->pre_mtime)) {
1406 		inode->i_mtime = fattr->mtime;
1407 		if (S_ISDIR(inode->i_mode))
1408 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1409 	}
1410 	if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1411 			&& (fattr->valid & NFS_ATTR_FATTR_SIZE)
1412 			&& i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
1413 			&& !nfs_have_writebacks(inode)) {
1414 		i_size_write(inode, nfs_size_to_loff_t(fattr->size));
1415 	}
1416 }
1417 
1418 /**
1419  * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1420  * @inode: pointer to inode
1421  * @fattr: updated attributes
1422  *
1423  * Verifies the attribute cache. If we have just changed the attributes,
1424  * so that fattr carries weak cache consistency data, then it may
1425  * also update the ctime/mtime/change_attribute.
1426  */
nfs_check_inode_attributes(struct inode * inode,struct nfs_fattr * fattr)1427 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1428 {
1429 	struct nfs_inode *nfsi = NFS_I(inode);
1430 	loff_t cur_size, new_isize;
1431 	unsigned long invalid = 0;
1432 	struct timespec64 ts;
1433 
1434 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1435 		return 0;
1436 
1437 	if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1438 		/* Only a mounted-on-fileid? Just exit */
1439 		if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1440 			return 0;
1441 	/* Has the inode gone and changed behind our back? */
1442 	} else if (nfsi->fileid != fattr->fileid) {
1443 		/* Is this perhaps the mounted-on fileid? */
1444 		if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1445 		    nfsi->fileid == fattr->mounted_on_fileid)
1446 			return 0;
1447 		return -ESTALE;
1448 	}
1449 	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
1450 		return -ESTALE;
1451 
1452 
1453 	if (!nfs_file_has_buffered_writers(nfsi)) {
1454 		/* Verify a few of the more important attributes */
1455 		if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
1456 			invalid |= NFS_INO_INVALID_CHANGE
1457 				| NFS_INO_REVAL_PAGECACHE;
1458 
1459 		ts = inode->i_mtime;
1460 		if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
1461 			invalid |= NFS_INO_INVALID_MTIME;
1462 
1463 		ts = inode->i_ctime;
1464 		if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
1465 			invalid |= NFS_INO_INVALID_CTIME;
1466 
1467 		if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1468 			cur_size = i_size_read(inode);
1469 			new_isize = nfs_size_to_loff_t(fattr->size);
1470 			if (cur_size != new_isize)
1471 				invalid |= NFS_INO_INVALID_SIZE
1472 					| NFS_INO_REVAL_PAGECACHE;
1473 		}
1474 	}
1475 
1476 	/* Have any file permissions changed? */
1477 	if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
1478 		invalid |= NFS_INO_INVALID_ACCESS
1479 			| NFS_INO_INVALID_ACL
1480 			| NFS_INO_INVALID_OTHER;
1481 	if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
1482 		invalid |= NFS_INO_INVALID_ACCESS
1483 			| NFS_INO_INVALID_ACL
1484 			| NFS_INO_INVALID_OTHER;
1485 	if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
1486 		invalid |= NFS_INO_INVALID_ACCESS
1487 			| NFS_INO_INVALID_ACL
1488 			| NFS_INO_INVALID_OTHER;
1489 
1490 	/* Has the link count changed? */
1491 	if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
1492 		invalid |= NFS_INO_INVALID_OTHER;
1493 
1494 	ts = inode->i_atime;
1495 	if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
1496 		invalid |= NFS_INO_INVALID_ATIME;
1497 
1498 	if (invalid != 0)
1499 		nfs_set_cache_invalid(inode, invalid);
1500 
1501 	nfsi->read_cache_jiffies = fattr->time_start;
1502 	return 0;
1503 }
1504 
1505 static atomic_long_t nfs_attr_generation_counter;
1506 
nfs_read_attr_generation_counter(void)1507 static unsigned long nfs_read_attr_generation_counter(void)
1508 {
1509 	return atomic_long_read(&nfs_attr_generation_counter);
1510 }
1511 
nfs_inc_attr_generation_counter(void)1512 unsigned long nfs_inc_attr_generation_counter(void)
1513 {
1514 	return atomic_long_inc_return(&nfs_attr_generation_counter);
1515 }
1516 EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
1517 
nfs_fattr_init(struct nfs_fattr * fattr)1518 void nfs_fattr_init(struct nfs_fattr *fattr)
1519 {
1520 	fattr->valid = 0;
1521 	fattr->time_start = jiffies;
1522 	fattr->gencount = nfs_inc_attr_generation_counter();
1523 	fattr->owner_name = NULL;
1524 	fattr->group_name = NULL;
1525 }
1526 EXPORT_SYMBOL_GPL(nfs_fattr_init);
1527 
1528 /**
1529  * nfs_fattr_set_barrier
1530  * @fattr: attributes
1531  *
1532  * Used to set a barrier after an attribute was updated. This
1533  * barrier ensures that older attributes from RPC calls that may
1534  * have raced with our update cannot clobber these new values.
1535  * Note that you are still responsible for ensuring that other
1536  * operations which change the attribute on the server do not
1537  * collide.
1538  */
nfs_fattr_set_barrier(struct nfs_fattr * fattr)1539 void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1540 {
1541 	fattr->gencount = nfs_inc_attr_generation_counter();
1542 }
1543 
nfs_alloc_fattr(void)1544 struct nfs_fattr *nfs_alloc_fattr(void)
1545 {
1546 	struct nfs_fattr *fattr;
1547 
1548 	fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
1549 	if (fattr != NULL)
1550 		nfs_fattr_init(fattr);
1551 	return fattr;
1552 }
1553 EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
1554 
nfs_alloc_fhandle(void)1555 struct nfs_fh *nfs_alloc_fhandle(void)
1556 {
1557 	struct nfs_fh *fh;
1558 
1559 	fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
1560 	if (fh != NULL)
1561 		fh->size = 0;
1562 	return fh;
1563 }
1564 EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
1565 
1566 #ifdef NFS_DEBUG
1567 /*
1568  * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1569  *                             in the same way that wireshark does
1570  *
1571  * @fh: file handle
1572  *
1573  * For debugging only.
1574  */
_nfs_display_fhandle_hash(const struct nfs_fh * fh)1575 u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1576 {
1577 	/* wireshark uses 32-bit AUTODIN crc and does a bitwise
1578 	 * not on the result */
1579 	return nfs_fhandle_hash(fh);
1580 }
1581 EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
1582 
1583 /*
1584  * _nfs_display_fhandle - display an NFS file handle on the console
1585  *
1586  * @fh: file handle to display
1587  * @caption: display caption
1588  *
1589  * For debugging only.
1590  */
_nfs_display_fhandle(const struct nfs_fh * fh,const char * caption)1591 void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1592 {
1593 	unsigned short i;
1594 
1595 	if (fh == NULL || fh->size == 0) {
1596 		printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1597 		return;
1598 	}
1599 
1600 	printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1601 	       caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
1602 	for (i = 0; i < fh->size; i += 16) {
1603 		__be32 *pos = (__be32 *)&fh->data[i];
1604 
1605 		switch ((fh->size - i - 1) >> 2) {
1606 		case 0:
1607 			printk(KERN_DEFAULT " %08x\n",
1608 				be32_to_cpup(pos));
1609 			break;
1610 		case 1:
1611 			printk(KERN_DEFAULT " %08x %08x\n",
1612 				be32_to_cpup(pos), be32_to_cpup(pos + 1));
1613 			break;
1614 		case 2:
1615 			printk(KERN_DEFAULT " %08x %08x %08x\n",
1616 				be32_to_cpup(pos), be32_to_cpup(pos + 1),
1617 				be32_to_cpup(pos + 2));
1618 			break;
1619 		default:
1620 			printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1621 				be32_to_cpup(pos), be32_to_cpup(pos + 1),
1622 				be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1623 		}
1624 	}
1625 }
1626 EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
1627 #endif
1628 
1629 /**
1630  * nfs_inode_attrs_need_update - check if the inode attributes need updating
1631  * @inode: pointer to inode
1632  * @fattr: attributes
1633  *
1634  * Attempt to divine whether or not an RPC call reply carrying stale
1635  * attributes got scheduled after another call carrying updated ones.
1636  *
1637  * To do so, the function first assumes that a more recent ctime means
1638  * that the attributes in fattr are newer, however it also attempt to
1639  * catch the case where ctime either didn't change, or went backwards
1640  * (if someone reset the clock on the server) by looking at whether
1641  * or not this RPC call was started after the inode was last updated.
1642  * Note also the check for wraparound of 'attr_gencount'
1643  *
1644  * The function returns 'true' if it thinks the attributes in 'fattr' are
1645  * more recent than the ones cached in the inode.
1646  *
1647  */
nfs_inode_attrs_need_update(const struct inode * inode,const struct nfs_fattr * fattr)1648 static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1649 {
1650 	unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
1651 
1652 	return (long)(fattr->gencount - attr_gencount) > 0 ||
1653 	       (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
1654 }
1655 
nfs_refresh_inode_locked(struct inode * inode,struct nfs_fattr * fattr)1656 static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1657 {
1658 	int ret;
1659 
1660 	trace_nfs_refresh_inode_enter(inode);
1661 
1662 	if (nfs_inode_attrs_need_update(inode, fattr))
1663 		ret = nfs_update_inode(inode, fattr);
1664 	else
1665 		ret = nfs_check_inode_attributes(inode, fattr);
1666 
1667 	trace_nfs_refresh_inode_exit(inode, ret);
1668 	return ret;
1669 }
1670 
1671 /**
1672  * nfs_refresh_inode - try to update the inode attribute cache
1673  * @inode: pointer to inode
1674  * @fattr: updated attributes
1675  *
1676  * Check that an RPC call that returned attributes has not overlapped with
1677  * other recent updates of the inode metadata, then decide whether it is
1678  * safe to do a full update of the inode attributes, or whether just to
1679  * call nfs_check_inode_attributes.
1680  */
nfs_refresh_inode(struct inode * inode,struct nfs_fattr * fattr)1681 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1682 {
1683 	int status;
1684 
1685 	if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1686 		return 0;
1687 	spin_lock(&inode->i_lock);
1688 	status = nfs_refresh_inode_locked(inode, fattr);
1689 	spin_unlock(&inode->i_lock);
1690 
1691 	return status;
1692 }
1693 EXPORT_SYMBOL_GPL(nfs_refresh_inode);
1694 
nfs_post_op_update_inode_locked(struct inode * inode,struct nfs_fattr * fattr,unsigned int invalid)1695 static int nfs_post_op_update_inode_locked(struct inode *inode,
1696 		struct nfs_fattr *fattr, unsigned int invalid)
1697 {
1698 	if (S_ISDIR(inode->i_mode))
1699 		invalid |= NFS_INO_INVALID_DATA;
1700 	nfs_set_cache_invalid(inode, invalid);
1701 	if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1702 		return 0;
1703 	return nfs_refresh_inode_locked(inode, fattr);
1704 }
1705 
1706 /**
1707  * nfs_post_op_update_inode - try to update the inode attribute cache
1708  * @inode: pointer to inode
1709  * @fattr: updated attributes
1710  *
1711  * After an operation that has changed the inode metadata, mark the
1712  * attribute cache as being invalid, then try to update it.
1713  *
1714  * NB: if the server didn't return any post op attributes, this
1715  * function will force the retrieval of attributes before the next
1716  * NFS request.  Thus it should be used only for operations that
1717  * are expected to change one or more attributes, to avoid
1718  * unnecessary NFS requests and trips through nfs_update_inode().
1719  */
nfs_post_op_update_inode(struct inode * inode,struct nfs_fattr * fattr)1720 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1721 {
1722 	int status;
1723 
1724 	spin_lock(&inode->i_lock);
1725 	nfs_fattr_set_barrier(fattr);
1726 	status = nfs_post_op_update_inode_locked(inode, fattr,
1727 			NFS_INO_INVALID_CHANGE
1728 			| NFS_INO_INVALID_CTIME
1729 			| NFS_INO_REVAL_FORCED);
1730 	spin_unlock(&inode->i_lock);
1731 
1732 	return status;
1733 }
1734 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
1735 
1736 /**
1737  * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
1738  * @inode: pointer to inode
1739  * @fattr: updated attributes
1740  *
1741  * After an operation that has changed the inode metadata, mark the
1742  * attribute cache as being invalid, then try to update it. Fake up
1743  * weak cache consistency data, if none exist.
1744  *
1745  * This function is mainly designed to be used by the ->write_done() functions.
1746  */
nfs_post_op_update_inode_force_wcc_locked(struct inode * inode,struct nfs_fattr * fattr)1747 int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
1748 {
1749 	int status;
1750 
1751 	/* Don't do a WCC update if these attributes are already stale */
1752 	if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1753 			!nfs_inode_attrs_need_update(inode, fattr)) {
1754 		fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1755 				| NFS_ATTR_FATTR_PRESIZE
1756 				| NFS_ATTR_FATTR_PREMTIME
1757 				| NFS_ATTR_FATTR_PRECTIME);
1758 		goto out_noforce;
1759 	}
1760 	if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1761 			(fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
1762 		fattr->pre_change_attr = inode_peek_iversion_raw(inode);
1763 		fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
1764 	}
1765 	if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1766 			(fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
1767 		fattr->pre_ctime = inode->i_ctime;
1768 		fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1769 	}
1770 	if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1771 			(fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
1772 		fattr->pre_mtime = inode->i_mtime;
1773 		fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1774 	}
1775 	if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1776 			(fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
1777 		fattr->pre_size = i_size_read(inode);
1778 		fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
1779 	}
1780 out_noforce:
1781 	status = nfs_post_op_update_inode_locked(inode, fattr,
1782 			NFS_INO_INVALID_CHANGE
1783 			| NFS_INO_INVALID_CTIME
1784 			| NFS_INO_INVALID_MTIME
1785 			| NFS_INO_INVALID_BLOCKS);
1786 	return status;
1787 }
1788 
1789 /**
1790  * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1791  * @inode: pointer to inode
1792  * @fattr: updated attributes
1793  *
1794  * After an operation that has changed the inode metadata, mark the
1795  * attribute cache as being invalid, then try to update it. Fake up
1796  * weak cache consistency data, if none exist.
1797  *
1798  * This function is mainly designed to be used by the ->write_done() functions.
1799  */
nfs_post_op_update_inode_force_wcc(struct inode * inode,struct nfs_fattr * fattr)1800 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1801 {
1802 	int status;
1803 
1804 	spin_lock(&inode->i_lock);
1805 	nfs_fattr_set_barrier(fattr);
1806 	status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1807 	spin_unlock(&inode->i_lock);
1808 	return status;
1809 }
1810 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
1811 
1812 
1813 /*
1814  * Many nfs protocol calls return the new file attributes after
1815  * an operation.  Here we update the inode to reflect the state
1816  * of the server's inode.
1817  *
1818  * This is a bit tricky because we have to make sure all dirty pages
1819  * have been sent off to the server before calling invalidate_inode_pages.
1820  * To make sure no other process adds more write requests while we try
1821  * our best to flush them, we make them sleep during the attribute refresh.
1822  *
1823  * A very similar scenario holds for the dir cache.
1824  */
nfs_update_inode(struct inode * inode,struct nfs_fattr * fattr)1825 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1826 {
1827 	struct nfs_server *server;
1828 	struct nfs_inode *nfsi = NFS_I(inode);
1829 	loff_t cur_isize, new_isize;
1830 	unsigned long invalid = 0;
1831 	unsigned long now = jiffies;
1832 	unsigned long save_cache_validity;
1833 	bool have_writers = nfs_file_has_buffered_writers(nfsi);
1834 	bool cache_revalidated = true;
1835 	bool attr_changed = false;
1836 	bool have_delegation;
1837 
1838 	dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
1839 			__func__, inode->i_sb->s_id, inode->i_ino,
1840 			nfs_display_fhandle_hash(NFS_FH(inode)),
1841 			atomic_read(&inode->i_count), fattr->valid);
1842 
1843 	if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1844 		/* Only a mounted-on-fileid? Just exit */
1845 		if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1846 			return 0;
1847 	/* Has the inode gone and changed behind our back? */
1848 	} else if (nfsi->fileid != fattr->fileid) {
1849 		/* Is this perhaps the mounted-on fileid? */
1850 		if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1851 		    nfsi->fileid == fattr->mounted_on_fileid)
1852 			return 0;
1853 		printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1854 			"fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1855 			NFS_SERVER(inode)->nfs_client->cl_hostname,
1856 			inode->i_sb->s_id, (long long)nfsi->fileid,
1857 			(long long)fattr->fileid);
1858 		goto out_err;
1859 	}
1860 
1861 	/*
1862 	 * Make sure the inode's type hasn't changed.
1863 	 */
1864 	if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
1865 		/*
1866 		* Big trouble! The inode has become a different object.
1867 		*/
1868 		printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
1869 				__func__, inode->i_ino, inode->i_mode, fattr->mode);
1870 		goto out_err;
1871 	}
1872 
1873 	server = NFS_SERVER(inode);
1874 	/* Update the fsid? */
1875 	if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
1876 			!nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1877 			!IS_AUTOMOUNT(inode))
1878 		server->fsid = fattr->fsid;
1879 
1880 	/* Save the delegation state before clearing cache_validity */
1881 	have_delegation = nfs_have_delegated_attributes(inode);
1882 
1883 	/*
1884 	 * Update the read time so we don't revalidate too often.
1885 	 */
1886 	nfsi->read_cache_jiffies = fattr->time_start;
1887 
1888 	save_cache_validity = nfsi->cache_validity;
1889 	nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1890 			| NFS_INO_INVALID_ATIME
1891 			| NFS_INO_REVAL_FORCED
1892 			| NFS_INO_REVAL_PAGECACHE
1893 			| NFS_INO_INVALID_BLOCKS);
1894 
1895 	/* Do atomic weak cache consistency updates */
1896 	nfs_wcc_update_inode(inode, fattr);
1897 
1898 	if (pnfs_layoutcommit_outstanding(inode)) {
1899 		nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
1900 		cache_revalidated = false;
1901 	}
1902 
1903 	/* More cache consistency checks */
1904 	if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1905 		if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
1906 			/* Could it be a race with writeback? */
1907 			if (!(have_writers || have_delegation)) {
1908 				invalid |= NFS_INO_INVALID_DATA
1909 					| NFS_INO_INVALID_ACCESS
1910 					| NFS_INO_INVALID_ACL
1911 					| NFS_INO_INVALID_XATTR;
1912 				/* Force revalidate of all attributes */
1913 				save_cache_validity |= NFS_INO_INVALID_CTIME
1914 					| NFS_INO_INVALID_MTIME
1915 					| NFS_INO_INVALID_SIZE
1916 					| NFS_INO_INVALID_OTHER;
1917 				if (S_ISDIR(inode->i_mode))
1918 					nfs_force_lookup_revalidate(inode);
1919 				dprintk("NFS: change_attr change on server for file %s/%ld\n",
1920 						inode->i_sb->s_id,
1921 						inode->i_ino);
1922 			} else if (!have_delegation)
1923 				nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
1924 			inode_set_iversion_raw(inode, fattr->change_attr);
1925 			attr_changed = true;
1926 		}
1927 	} else {
1928 		nfsi->cache_validity |= save_cache_validity &
1929 				(NFS_INO_INVALID_CHANGE
1930 				| NFS_INO_REVAL_PAGECACHE
1931 				| NFS_INO_REVAL_FORCED);
1932 		cache_revalidated = false;
1933 	}
1934 
1935 	if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
1936 		inode->i_mtime = fattr->mtime;
1937 	} else if (server->caps & NFS_CAP_MTIME) {
1938 		nfsi->cache_validity |= save_cache_validity &
1939 				(NFS_INO_INVALID_MTIME
1940 				| NFS_INO_REVAL_FORCED);
1941 		cache_revalidated = false;
1942 	}
1943 
1944 	if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
1945 		inode->i_ctime = fattr->ctime;
1946 	} else if (server->caps & NFS_CAP_CTIME) {
1947 		nfsi->cache_validity |= save_cache_validity &
1948 				(NFS_INO_INVALID_CTIME
1949 				| NFS_INO_REVAL_FORCED);
1950 		cache_revalidated = false;
1951 	}
1952 
1953 	/* Check if our cached file size is stale */
1954 	if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1955 		new_isize = nfs_size_to_loff_t(fattr->size);
1956 		cur_isize = i_size_read(inode);
1957 		if (new_isize != cur_isize && !have_delegation) {
1958 			/* Do we perhaps have any outstanding writes, or has
1959 			 * the file grown beyond our last write? */
1960 			if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
1961 				i_size_write(inode, new_isize);
1962 				if (!have_writers)
1963 					invalid |= NFS_INO_INVALID_DATA;
1964 				attr_changed = true;
1965 			}
1966 			dprintk("NFS: isize change on server for file %s/%ld "
1967 					"(%Ld to %Ld)\n",
1968 					inode->i_sb->s_id,
1969 					inode->i_ino,
1970 					(long long)cur_isize,
1971 					(long long)new_isize);
1972 		}
1973 	} else {
1974 		nfsi->cache_validity |= save_cache_validity &
1975 				(NFS_INO_INVALID_SIZE
1976 				| NFS_INO_REVAL_PAGECACHE
1977 				| NFS_INO_REVAL_FORCED);
1978 		cache_revalidated = false;
1979 	}
1980 
1981 
1982 	if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1983 		inode->i_atime = fattr->atime;
1984 	else if (server->caps & NFS_CAP_ATIME) {
1985 		nfsi->cache_validity |= save_cache_validity &
1986 				(NFS_INO_INVALID_ATIME
1987 				| NFS_INO_REVAL_FORCED);
1988 		cache_revalidated = false;
1989 	}
1990 
1991 	if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1992 		if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
1993 			umode_t newmode = inode->i_mode & S_IFMT;
1994 			newmode |= fattr->mode & S_IALLUGO;
1995 			inode->i_mode = newmode;
1996 			invalid |= NFS_INO_INVALID_ACCESS
1997 				| NFS_INO_INVALID_ACL;
1998 			attr_changed = true;
1999 		}
2000 	} else if (server->caps & NFS_CAP_MODE) {
2001 		nfsi->cache_validity |= save_cache_validity &
2002 				(NFS_INO_INVALID_OTHER
2003 				| NFS_INO_REVAL_FORCED);
2004 		cache_revalidated = false;
2005 	}
2006 
2007 	if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
2008 		if (!uid_eq(inode->i_uid, fattr->uid)) {
2009 			invalid |= NFS_INO_INVALID_ACCESS
2010 				| NFS_INO_INVALID_ACL;
2011 			inode->i_uid = fattr->uid;
2012 			attr_changed = true;
2013 		}
2014 	} else if (server->caps & NFS_CAP_OWNER) {
2015 		nfsi->cache_validity |= save_cache_validity &
2016 				(NFS_INO_INVALID_OTHER
2017 				| NFS_INO_REVAL_FORCED);
2018 		cache_revalidated = false;
2019 	}
2020 
2021 	if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
2022 		if (!gid_eq(inode->i_gid, fattr->gid)) {
2023 			invalid |= NFS_INO_INVALID_ACCESS
2024 				| NFS_INO_INVALID_ACL;
2025 			inode->i_gid = fattr->gid;
2026 			attr_changed = true;
2027 		}
2028 	} else if (server->caps & NFS_CAP_OWNER_GROUP) {
2029 		nfsi->cache_validity |= save_cache_validity &
2030 				(NFS_INO_INVALID_OTHER
2031 				| NFS_INO_REVAL_FORCED);
2032 		cache_revalidated = false;
2033 	}
2034 
2035 	if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2036 		if (inode->i_nlink != fattr->nlink) {
2037 			if (S_ISDIR(inode->i_mode))
2038 				invalid |= NFS_INO_INVALID_DATA;
2039 			set_nlink(inode, fattr->nlink);
2040 			attr_changed = true;
2041 		}
2042 	} else if (server->caps & NFS_CAP_NLINK) {
2043 		nfsi->cache_validity |= save_cache_validity &
2044 				(NFS_INO_INVALID_OTHER
2045 				| NFS_INO_REVAL_FORCED);
2046 		cache_revalidated = false;
2047 	}
2048 
2049 	if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
2050 		/*
2051 		 * report the blocks in 512byte units
2052 		 */
2053 		inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
2054 	} else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
2055 		inode->i_blocks = fattr->du.nfs2.blocks;
2056 	else {
2057 		nfsi->cache_validity |= save_cache_validity &
2058 				(NFS_INO_INVALID_BLOCKS
2059 				| NFS_INO_REVAL_FORCED);
2060 		cache_revalidated = false;
2061 	}
2062 
2063 	/* Update attrtimeo value if we're out of the unstable period */
2064 	if (attr_changed) {
2065 		invalid &= ~NFS_INO_INVALID_ATTR;
2066 		nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
2067 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
2068 		nfsi->attrtimeo_timestamp = now;
2069 		/* Set barrier to be more recent than all outstanding updates */
2070 		nfsi->attr_gencount = nfs_inc_attr_generation_counter();
2071 	} else {
2072 		if (cache_revalidated) {
2073 			if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2074 				nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2075 				nfsi->attrtimeo <<= 1;
2076 				if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2077 					nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2078 			}
2079 			nfsi->attrtimeo_timestamp = now;
2080 		}
2081 		/* Set the barrier to be more recent than this fattr */
2082 		if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
2083 			nfsi->attr_gencount = fattr->gencount;
2084 	}
2085 
2086 	/* Don't invalidate the data if we were to blame */
2087 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2088 				|| S_ISLNK(inode->i_mode)))
2089 		invalid &= ~NFS_INO_INVALID_DATA;
2090 	nfs_set_cache_invalid(inode, invalid);
2091 
2092 	return 0;
2093  out_err:
2094 	/*
2095 	 * No need to worry about unhashing the dentry, as the
2096 	 * lookup validation will know that the inode is bad.
2097 	 * (But we fall through to invalidate the caches.)
2098 	 */
2099 	nfs_set_inode_stale_locked(inode);
2100 	return -ESTALE;
2101 }
2102 
nfs_alloc_inode(struct super_block * sb)2103 struct inode *nfs_alloc_inode(struct super_block *sb)
2104 {
2105 	struct nfs_inode *nfsi;
2106 	nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
2107 	if (!nfsi)
2108 		return NULL;
2109 	nfsi->flags = 0UL;
2110 	nfsi->cache_validity = 0UL;
2111 #if IS_ENABLED(CONFIG_NFS_V4)
2112 	nfsi->nfs4_acl = NULL;
2113 #endif /* CONFIG_NFS_V4 */
2114 #ifdef CONFIG_NFS_V4_2
2115 	nfsi->xattr_cache = NULL;
2116 #endif
2117 	return &nfsi->vfs_inode;
2118 }
2119 EXPORT_SYMBOL_GPL(nfs_alloc_inode);
2120 
nfs_free_inode(struct inode * inode)2121 void nfs_free_inode(struct inode *inode)
2122 {
2123 	kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2124 }
2125 EXPORT_SYMBOL_GPL(nfs_free_inode);
2126 
nfs4_init_once(struct nfs_inode * nfsi)2127 static inline void nfs4_init_once(struct nfs_inode *nfsi)
2128 {
2129 #if IS_ENABLED(CONFIG_NFS_V4)
2130 	INIT_LIST_HEAD(&nfsi->open_states);
2131 	nfsi->delegation = NULL;
2132 	init_rwsem(&nfsi->rwsem);
2133 	nfsi->layout = NULL;
2134 #endif
2135 }
2136 
init_once(void * foo)2137 static void init_once(void *foo)
2138 {
2139 	struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2140 
2141 	inode_init_once(&nfsi->vfs_inode);
2142 	INIT_LIST_HEAD(&nfsi->open_files);
2143 	INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2144 	INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
2145 	INIT_LIST_HEAD(&nfsi->commit_info.list);
2146 	atomic_long_set(&nfsi->nrequests, 0);
2147 	atomic_long_set(&nfsi->commit_info.ncommit, 0);
2148 	atomic_set(&nfsi->commit_info.rpcs_out, 0);
2149 	init_rwsem(&nfsi->rmdir_sem);
2150 	mutex_init(&nfsi->commit_mutex);
2151 	nfs4_init_once(nfsi);
2152 	nfsi->cache_change_attribute = 0;
2153 }
2154 
nfs_init_inodecache(void)2155 static int __init nfs_init_inodecache(void)
2156 {
2157 	nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2158 					     sizeof(struct nfs_inode),
2159 					     0, (SLAB_RECLAIM_ACCOUNT|
2160 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2161 					     init_once);
2162 	if (nfs_inode_cachep == NULL)
2163 		return -ENOMEM;
2164 
2165 	return 0;
2166 }
2167 
nfs_destroy_inodecache(void)2168 static void nfs_destroy_inodecache(void)
2169 {
2170 	/*
2171 	 * Make sure all delayed rcu free inodes are flushed before we
2172 	 * destroy cache.
2173 	 */
2174 	rcu_barrier();
2175 	kmem_cache_destroy(nfs_inode_cachep);
2176 }
2177 
2178 struct workqueue_struct *nfsiod_workqueue;
2179 EXPORT_SYMBOL_GPL(nfsiod_workqueue);
2180 
2181 /*
2182  * start up the nfsiod workqueue
2183  */
nfsiod_start(void)2184 static int nfsiod_start(void)
2185 {
2186 	struct workqueue_struct *wq;
2187 	dprintk("RPC:       creating workqueue nfsiod\n");
2188 	wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
2189 	if (wq == NULL)
2190 		return -ENOMEM;
2191 	nfsiod_workqueue = wq;
2192 	return 0;
2193 }
2194 
2195 /*
2196  * Destroy the nfsiod workqueue
2197  */
nfsiod_stop(void)2198 static void nfsiod_stop(void)
2199 {
2200 	struct workqueue_struct *wq;
2201 
2202 	wq = nfsiod_workqueue;
2203 	if (wq == NULL)
2204 		return;
2205 	nfsiod_workqueue = NULL;
2206 	destroy_workqueue(wq);
2207 }
2208 
2209 unsigned int nfs_net_id;
2210 EXPORT_SYMBOL_GPL(nfs_net_id);
2211 
nfs_net_init(struct net * net)2212 static int nfs_net_init(struct net *net)
2213 {
2214 	nfs_clients_init(net);
2215 	return nfs_fs_proc_net_init(net);
2216 }
2217 
nfs_net_exit(struct net * net)2218 static void nfs_net_exit(struct net *net)
2219 {
2220 	nfs_fs_proc_net_exit(net);
2221 	nfs_clients_exit(net);
2222 }
2223 
2224 static struct pernet_operations nfs_net_ops = {
2225 	.init = nfs_net_init,
2226 	.exit = nfs_net_exit,
2227 	.id   = &nfs_net_id,
2228 	.size = sizeof(struct nfs_net),
2229 };
2230 
2231 /*
2232  * Initialize NFS
2233  */
init_nfs_fs(void)2234 static int __init init_nfs_fs(void)
2235 {
2236 	int err;
2237 
2238 	err = nfs_sysfs_init();
2239 	if (err < 0)
2240 		goto out10;
2241 
2242 	err = register_pernet_subsys(&nfs_net_ops);
2243 	if (err < 0)
2244 		goto out9;
2245 
2246 	err = nfs_fscache_register();
2247 	if (err < 0)
2248 		goto out8;
2249 
2250 	err = nfsiod_start();
2251 	if (err)
2252 		goto out7;
2253 
2254 	err = nfs_fs_proc_init();
2255 	if (err)
2256 		goto out6;
2257 
2258 	err = nfs_init_nfspagecache();
2259 	if (err)
2260 		goto out5;
2261 
2262 	err = nfs_init_inodecache();
2263 	if (err)
2264 		goto out4;
2265 
2266 	err = nfs_init_readpagecache();
2267 	if (err)
2268 		goto out3;
2269 
2270 	err = nfs_init_writepagecache();
2271 	if (err)
2272 		goto out2;
2273 
2274 	err = nfs_init_directcache();
2275 	if (err)
2276 		goto out1;
2277 
2278 	rpc_proc_register(&init_net, &nfs_rpcstat);
2279 
2280 	err = register_nfs_fs();
2281 	if (err)
2282 		goto out0;
2283 
2284 	return 0;
2285 out0:
2286 	rpc_proc_unregister(&init_net, "nfs");
2287 	nfs_destroy_directcache();
2288 out1:
2289 	nfs_destroy_writepagecache();
2290 out2:
2291 	nfs_destroy_readpagecache();
2292 out3:
2293 	nfs_destroy_inodecache();
2294 out4:
2295 	nfs_destroy_nfspagecache();
2296 out5:
2297 	nfs_fs_proc_exit();
2298 out6:
2299 	nfsiod_stop();
2300 out7:
2301 	nfs_fscache_unregister();
2302 out8:
2303 	unregister_pernet_subsys(&nfs_net_ops);
2304 out9:
2305 	nfs_sysfs_exit();
2306 out10:
2307 	return err;
2308 }
2309 
exit_nfs_fs(void)2310 static void __exit exit_nfs_fs(void)
2311 {
2312 	nfs_destroy_directcache();
2313 	nfs_destroy_writepagecache();
2314 	nfs_destroy_readpagecache();
2315 	nfs_destroy_inodecache();
2316 	nfs_destroy_nfspagecache();
2317 	nfs_fscache_unregister();
2318 	unregister_pernet_subsys(&nfs_net_ops);
2319 	rpc_proc_unregister(&init_net, "nfs");
2320 	unregister_nfs_fs();
2321 	nfs_fs_proc_exit();
2322 	nfsiod_stop();
2323 	nfs_sysfs_exit();
2324 }
2325 
2326 /* Not quite true; I just maintain it */
2327 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2328 MODULE_LICENSE("GPL");
2329 module_param(enable_ino64, bool, 0644);
2330 
2331 module_init(init_nfs_fs)
2332 module_exit(exit_nfs_fs)
2333