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