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