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