1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1992 Rick Sladkey
7 * nfs inode and superblock handling functions
10 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
12 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
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>
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>
47 #include "delegation.h"
58 #define NFSDBG_FACILITY NFSDBG_VFS
60 #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
62 /* Default is to see 64-bit inode numbers */
63 static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
65 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
67 static struct kmem_cache * nfs_inode_cachep;
69 static inline unsigned long
70 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
72 return nfs_fileid_to_ino_t(fattr->fileid);
75 int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
78 if (signal_pending_state(mode, current))
82 EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
85 * nfs_compat_user_ino64 - returns the user-visible inode number
86 * @fileid: 64-bit fileid
88 * This function returns a 32-bit inode number if the boot parameter
89 * nfs.enable_ino64 is zero.
91 u64 nfs_compat_user_ino64(u64 fileid)
102 if (sizeof(ino) < sizeof(fileid))
103 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
107 int nfs_drop_inode(struct inode *inode)
109 return NFS_STALE(inode) || generic_drop_inode(inode);
111 EXPORT_SYMBOL_GPL(nfs_drop_inode);
113 void nfs_clear_inode(struct inode *inode)
116 * The following should never happen...
118 WARN_ON_ONCE(nfs_have_writebacks(inode));
119 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
120 nfs_zap_acl_cache(inode);
121 nfs_access_zap_cache(inode);
122 nfs_fscache_clear_inode(inode);
124 EXPORT_SYMBOL_GPL(nfs_clear_inode);
126 void nfs_evict_inode(struct inode *inode)
128 truncate_inode_pages_final(&inode->i_data);
130 nfs_clear_inode(inode);
133 int nfs_sync_inode(struct inode *inode)
135 inode_dio_wait(inode);
136 return nfs_wb_all(inode);
138 EXPORT_SYMBOL_GPL(nfs_sync_inode);
141 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
142 * @mapping: pointer to struct address_space
144 int nfs_sync_mapping(struct address_space *mapping)
148 if (mapping->nrpages != 0) {
149 unmap_mapping_range(mapping, 0, 0, 0);
150 ret = nfs_wb_all(mapping->host);
155 static int nfs_attribute_timeout(struct inode *inode)
157 struct nfs_inode *nfsi = NFS_I(inode);
159 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
162 static bool nfs_check_cache_flags_invalid(struct inode *inode,
165 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
167 return (cache_validity & flags) != 0;
170 bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
172 if (nfs_check_cache_flags_invalid(inode, flags))
174 return nfs_attribute_cache_expired(inode);
176 EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
178 #ifdef CONFIG_NFS_V4_2
179 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
181 return nfsi->xattr_cache != NULL;
184 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
190 void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
192 struct nfs_inode *nfsi = NFS_I(inode);
194 if (nfs_have_delegated_attributes(inode)) {
195 if (!(flags & NFS_INO_REVAL_FORCED))
196 flags &= ~(NFS_INO_INVALID_MODE |
197 NFS_INO_INVALID_OTHER |
198 NFS_INO_INVALID_XATTR);
199 flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
202 if (!nfs_has_xattr_cache(nfsi))
203 flags &= ~NFS_INO_INVALID_XATTR;
204 if (flags & NFS_INO_INVALID_DATA)
205 nfs_fscache_invalidate(inode, 0);
206 flags &= ~NFS_INO_REVAL_FORCED;
208 nfsi->cache_validity |= flags;
210 if (inode->i_mapping->nrpages == 0) {
211 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
213 } else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
216 trace_nfs_set_cache_invalid(inode, 0);
218 EXPORT_SYMBOL_GPL(nfs_set_cache_invalid);
221 * Invalidate the local caches
223 static void nfs_zap_caches_locked(struct inode *inode)
225 struct nfs_inode *nfsi = NFS_I(inode);
226 int mode = inode->i_mode;
228 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
230 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
231 nfsi->attrtimeo_timestamp = jiffies;
233 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
234 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
235 NFS_INO_INVALID_DATA |
236 NFS_INO_INVALID_ACCESS |
237 NFS_INO_INVALID_ACL |
238 NFS_INO_INVALID_XATTR);
240 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
241 NFS_INO_INVALID_ACCESS |
242 NFS_INO_INVALID_ACL |
243 NFS_INO_INVALID_XATTR);
244 nfs_zap_label_cache_locked(nfsi);
247 void nfs_zap_caches(struct inode *inode)
249 spin_lock(&inode->i_lock);
250 nfs_zap_caches_locked(inode);
251 spin_unlock(&inode->i_lock);
254 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
256 if (mapping->nrpages != 0) {
257 spin_lock(&inode->i_lock);
258 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
259 spin_unlock(&inode->i_lock);
263 void nfs_zap_acl_cache(struct inode *inode)
265 void (*clear_acl_cache)(struct inode *);
267 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
268 if (clear_acl_cache != NULL)
269 clear_acl_cache(inode);
270 spin_lock(&inode->i_lock);
271 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
272 spin_unlock(&inode->i_lock);
274 EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
276 void nfs_invalidate_atime(struct inode *inode)
278 if (nfs_have_delegated_atime(inode))
280 spin_lock(&inode->i_lock);
281 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
282 spin_unlock(&inode->i_lock);
284 EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
287 * Invalidate, but do not unhash, the inode.
288 * NB: must be called with inode->i_lock held!
290 static void nfs_set_inode_stale_locked(struct inode *inode)
292 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
293 nfs_zap_caches_locked(inode);
294 trace_nfs_set_inode_stale(inode);
297 void nfs_set_inode_stale(struct inode *inode)
299 spin_lock(&inode->i_lock);
300 nfs_set_inode_stale_locked(inode);
301 spin_unlock(&inode->i_lock);
304 struct nfs_find_desc {
306 struct nfs_fattr *fattr;
310 * In NFSv3 we can have 64bit inode numbers. In order to support
311 * this, and re-exported directories (also seen in NFSv2)
312 * we are forced to allow 2 different inodes to have the same
316 nfs_find_actor(struct inode *inode, void *opaque)
318 struct nfs_find_desc *desc = opaque;
319 struct nfs_fh *fh = desc->fh;
320 struct nfs_fattr *fattr = desc->fattr;
322 if (NFS_FILEID(inode) != fattr->fileid)
324 if (inode_wrong_type(inode, fattr->mode))
326 if (nfs_compare_fh(NFS_FH(inode), fh))
328 if (is_bad_inode(inode) || NFS_STALE(inode))
334 nfs_init_locked(struct inode *inode, void *opaque)
336 struct nfs_find_desc *desc = opaque;
337 struct nfs_fattr *fattr = desc->fattr;
339 set_nfs_fileid(inode, fattr->fileid);
340 inode->i_mode = fattr->mode;
341 nfs_copy_fh(NFS_FH(inode), desc->fh);
345 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
346 static void nfs_clear_label_invalid(struct inode *inode)
348 spin_lock(&inode->i_lock);
349 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
350 spin_unlock(&inode->i_lock);
353 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
357 if (fattr->label == NULL)
360 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
361 error = security_inode_notifysecctx(inode, fattr->label->label,
364 printk(KERN_ERR "%s() %s %d "
365 "security_inode_notifysecctx() %d\n",
367 (char *)fattr->label->label,
368 fattr->label->len, error);
369 nfs_clear_label_invalid(inode);
373 struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
375 struct nfs4_label *label;
377 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
380 label = kzalloc(sizeof(struct nfs4_label), flags);
382 return ERR_PTR(-ENOMEM);
384 label->label = kzalloc(NFS4_MAXLABELLEN, flags);
385 if (label->label == NULL) {
387 return ERR_PTR(-ENOMEM);
389 label->len = NFS4_MAXLABELLEN;
393 EXPORT_SYMBOL_GPL(nfs4_label_alloc);
395 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
399 EXPORT_SYMBOL_GPL(nfs_setsecurity);
401 /* Search for inode identified by fh, fileid and i_mode in inode cache. */
403 nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
405 struct nfs_find_desc desc = {
412 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
413 !(fattr->valid & NFS_ATTR_FATTR_TYPE))
416 hash = nfs_fattr_to_ino_t(fattr);
417 inode = ilookup5(sb, hash, nfs_find_actor, &desc);
419 dprintk("%s: returning %p\n", __func__, inode);
423 static void nfs_inode_init_regular(struct nfs_inode *nfsi)
425 atomic_long_set(&nfsi->nrequests, 0);
426 atomic_long_set(&nfsi->redirtied_pages, 0);
427 INIT_LIST_HEAD(&nfsi->commit_info.list);
428 atomic_long_set(&nfsi->commit_info.ncommit, 0);
429 atomic_set(&nfsi->commit_info.rpcs_out, 0);
430 mutex_init(&nfsi->commit_mutex);
433 static void nfs_inode_init_dir(struct nfs_inode *nfsi)
435 nfsi->cache_change_attribute = 0;
436 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
437 init_rwsem(&nfsi->rmdir_sem);
441 * This is our front-end to iget that looks up inodes by file handle
442 * instead of inode number.
445 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
447 struct nfs_find_desc desc = {
451 struct inode *inode = ERR_PTR(-ENOENT);
452 u64 fattr_supported = NFS_SB(sb)->fattr_valid;
455 nfs_attr_check_mountpoint(sb, fattr);
457 if (nfs_attr_use_mounted_on_fileid(fattr))
458 fattr->fileid = fattr->mounted_on_fileid;
459 else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
461 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
464 hash = nfs_fattr_to_ino_t(fattr);
466 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
468 inode = ERR_PTR(-ENOMEM);
472 if (inode->i_state & I_NEW) {
473 struct nfs_inode *nfsi = NFS_I(inode);
474 unsigned long now = jiffies;
476 /* We set i_ino for the few things that still rely on it,
480 /* We can't support update_atime(), since the server will reset it */
481 inode->i_flags |= S_NOATIME|S_NOCMTIME;
482 inode->i_mode = fattr->mode;
483 nfsi->cache_validity = 0;
484 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
485 && (fattr_supported & NFS_ATTR_FATTR_MODE))
486 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
487 /* Why so? Because we want revalidate for devices/FIFOs, and
488 * that's precisely what we have in nfs_file_inode_operations.
490 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
491 if (S_ISREG(inode->i_mode)) {
492 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
493 inode->i_data.a_ops = &nfs_file_aops;
494 nfs_inode_init_regular(nfsi);
495 mapping_set_large_folios(inode->i_mapping);
496 } else if (S_ISDIR(inode->i_mode)) {
497 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
498 inode->i_fop = &nfs_dir_operations;
499 inode->i_data.a_ops = &nfs_dir_aops;
500 nfs_inode_init_dir(nfsi);
501 /* Deal with crossing mountpoints */
502 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
503 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
504 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
505 inode->i_op = &nfs_referral_inode_operations;
507 inode->i_op = &nfs_mountpoint_inode_operations;
509 inode->i_flags |= S_AUTOMOUNT;
511 } else if (S_ISLNK(inode->i_mode)) {
512 inode->i_op = &nfs_symlink_inode_operations;
513 inode_nohighmem(inode);
515 init_special_inode(inode, inode->i_mode, fattr->rdev);
517 inode_set_atime(inode, 0, 0);
518 inode_set_mtime(inode, 0, 0);
519 inode_set_ctime(inode, 0, 0);
520 inode_set_iversion_raw(inode, 0);
523 inode->i_uid = make_kuid(&init_user_ns, -2);
524 inode->i_gid = make_kgid(&init_user_ns, -2);
529 nfsi->read_cache_jiffies = fattr->time_start;
530 nfsi->attr_gencount = fattr->gencount;
531 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
532 inode_set_atime_to_ts(inode, fattr->atime);
533 else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
534 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
535 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
536 inode_set_mtime_to_ts(inode, fattr->mtime);
537 else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
538 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
539 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
540 inode_set_ctime_to_ts(inode, fattr->ctime);
541 else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
542 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
543 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
544 inode_set_iversion_raw(inode, fattr->change_attr);
546 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
547 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
548 inode->i_size = nfs_size_to_loff_t(fattr->size);
550 nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
551 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
552 set_nlink(inode, fattr->nlink);
553 else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
554 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
555 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
556 inode->i_uid = fattr->uid;
557 else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
558 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
559 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
560 inode->i_gid = fattr->gid;
561 else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
562 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
563 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
564 inode->i_blocks = fattr->du.nfs2.blocks;
565 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED &&
567 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
568 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
570 * report the blocks in 512byte units
572 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
573 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED &&
575 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
577 nfs_setsecurity(inode, fattr);
579 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
580 nfsi->attrtimeo_timestamp = now;
581 nfsi->access_cache = RB_ROOT;
583 nfs_fscache_init_inode(inode);
585 unlock_new_inode(inode);
587 int err = nfs_refresh_inode(inode, fattr);
590 inode = ERR_PTR(err);
594 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
596 (unsigned long long)NFS_FILEID(inode),
597 nfs_display_fhandle_hash(fh),
598 atomic_read(&inode->i_count));
604 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
607 EXPORT_SYMBOL_GPL(nfs_fhget);
610 nfs_fattr_fixup_delegated(struct inode *inode, struct nfs_fattr *fattr)
612 unsigned long cache_validity = NFS_I(inode)->cache_validity;
614 if (nfs_have_delegated_mtime(inode)) {
615 if (!(cache_validity & NFS_INO_INVALID_CTIME))
616 fattr->valid &= ~(NFS_ATTR_FATTR_PRECTIME |
617 NFS_ATTR_FATTR_CTIME);
619 if (!(cache_validity & NFS_INO_INVALID_MTIME))
620 fattr->valid &= ~(NFS_ATTR_FATTR_PREMTIME |
621 NFS_ATTR_FATTR_MTIME);
623 if (!(cache_validity & NFS_INO_INVALID_ATIME))
624 fattr->valid &= ~NFS_ATTR_FATTR_ATIME;
625 } else if (nfs_have_delegated_atime(inode)) {
626 if (!(cache_validity & NFS_INO_INVALID_ATIME))
627 fattr->valid &= ~NFS_ATTR_FATTR_ATIME;
631 void nfs_update_delegated_atime(struct inode *inode)
633 spin_lock(&inode->i_lock);
634 if (nfs_have_delegated_atime(inode)) {
635 inode_update_timestamps(inode, S_ATIME);
636 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ATIME;
638 spin_unlock(&inode->i_lock);
641 void nfs_update_delegated_mtime_locked(struct inode *inode)
643 if (nfs_have_delegated_mtime(inode)) {
644 inode_update_timestamps(inode, S_CTIME | S_MTIME);
645 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_CTIME |
646 NFS_INO_INVALID_MTIME);
650 void nfs_update_delegated_mtime(struct inode *inode)
652 spin_lock(&inode->i_lock);
653 nfs_update_delegated_mtime_locked(inode);
654 spin_unlock(&inode->i_lock);
656 EXPORT_SYMBOL_GPL(nfs_update_delegated_mtime);
658 #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)
661 nfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
664 struct inode *inode = d_inode(dentry);
665 struct nfs_fattr *fattr;
668 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
670 /* skip mode change if it's just for clearing setuid/setgid */
671 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
672 attr->ia_valid &= ~ATTR_MODE;
674 if (attr->ia_valid & ATTR_SIZE) {
675 BUG_ON(!S_ISREG(inode->i_mode));
677 error = inode_newsize_ok(inode, attr->ia_size);
681 if (attr->ia_size == i_size_read(inode))
682 attr->ia_valid &= ~ATTR_SIZE;
685 if (nfs_have_delegated_mtime(inode)) {
686 if (attr->ia_valid & ATTR_MTIME) {
687 nfs_update_delegated_mtime(inode);
688 attr->ia_valid &= ~ATTR_MTIME;
690 if (attr->ia_valid & ATTR_ATIME) {
691 nfs_update_delegated_atime(inode);
692 attr->ia_valid &= ~ATTR_ATIME;
696 /* Optimization: if the end result is no change, don't RPC */
697 if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0)
700 trace_nfs_setattr_enter(inode);
702 /* Write all dirty data */
703 if (S_ISREG(inode->i_mode))
704 nfs_sync_inode(inode);
706 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
712 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
714 error = nfs_refresh_inode(inode, fattr);
715 nfs_free_fattr(fattr);
717 trace_nfs_setattr_exit(inode, error);
720 EXPORT_SYMBOL_GPL(nfs_setattr);
723 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
724 * @inode: inode of the file used
725 * @offset: file offset to start truncating
727 * This is a copy of the common vmtruncate, but with the locking
728 * corrected to take into account the fact that NFS requires
729 * inode->i_size to be updated under the inode->i_lock.
730 * Note: must be called with inode->i_lock held!
732 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
736 err = inode_newsize_ok(inode, offset);
740 trace_nfs_size_truncate(inode, offset);
741 i_size_write(inode, offset);
744 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
745 nfs_ooo_clear(NFS_I(inode));
747 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
749 spin_unlock(&inode->i_lock);
750 truncate_pagecache(inode, offset);
751 nfs_update_delegated_mtime_locked(inode);
752 spin_lock(&inode->i_lock);
758 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
759 * @inode: pointer to struct inode
760 * @attr: pointer to struct iattr
761 * @fattr: pointer to struct nfs_fattr
763 * Note: we do this in the *proc.c in order to ensure that
764 * it works for things like exclusive creates too.
766 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
767 struct nfs_fattr *fattr)
769 /* Barrier: bump the attribute generation count. */
770 nfs_fattr_set_barrier(fattr);
772 spin_lock(&inode->i_lock);
773 NFS_I(inode)->attr_gencount = fattr->gencount;
774 if ((attr->ia_valid & ATTR_SIZE) != 0) {
775 if (!nfs_have_delegated_mtime(inode))
776 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
777 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
778 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
779 nfs_vmtruncate(inode, attr->ia_size);
781 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
782 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
783 if ((attr->ia_valid & ATTR_KILL_SUID) != 0 &&
784 inode->i_mode & S_ISUID)
785 inode->i_mode &= ~S_ISUID;
786 if (setattr_should_drop_sgid(&nop_mnt_idmap, inode))
787 inode->i_mode &= ~S_ISGID;
788 if ((attr->ia_valid & ATTR_MODE) != 0) {
789 int mode = attr->ia_mode & S_IALLUGO;
790 mode |= inode->i_mode & ~S_IALLUGO;
791 inode->i_mode = mode;
793 if ((attr->ia_valid & ATTR_UID) != 0)
794 inode->i_uid = attr->ia_uid;
795 if ((attr->ia_valid & ATTR_GID) != 0)
796 inode->i_gid = attr->ia_gid;
797 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
798 inode_set_ctime_to_ts(inode, fattr->ctime);
800 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
801 | NFS_INO_INVALID_CTIME);
802 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
803 | NFS_INO_INVALID_ACL);
805 if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
806 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
807 | NFS_INO_INVALID_CTIME);
808 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
809 inode_set_atime_to_ts(inode, fattr->atime);
810 else if (attr->ia_valid & ATTR_ATIME_SET)
811 inode_set_atime_to_ts(inode, attr->ia_atime);
813 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
815 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
816 inode_set_ctime_to_ts(inode, fattr->ctime);
818 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
819 | NFS_INO_INVALID_CTIME);
821 if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
822 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
823 | NFS_INO_INVALID_CTIME);
824 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
825 inode_set_mtime_to_ts(inode, fattr->mtime);
826 else if (attr->ia_valid & ATTR_MTIME_SET)
827 inode_set_mtime_to_ts(inode, attr->ia_mtime);
829 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
831 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
832 inode_set_ctime_to_ts(inode, fattr->ctime);
834 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
835 | NFS_INO_INVALID_CTIME);
838 nfs_update_inode(inode, fattr);
839 spin_unlock(&inode->i_lock);
841 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
844 * Don't request help from readdirplus if the file is being written to,
845 * or if attribute caching is turned off
847 static bool nfs_getattr_readdirplus_enable(const struct inode *inode)
849 return nfs_server_capable(inode, NFS_CAP_READDIRPLUS) &&
850 !nfs_have_writebacks(inode) && NFS_MAXATTRTIMEO(inode) > 5 * HZ;
853 static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
855 if (!IS_ROOT(dentry)) {
856 struct dentry *parent = dget_parent(dentry);
857 nfs_readdir_record_entry_cache_miss(d_inode(parent));
862 static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
864 if (!IS_ROOT(dentry)) {
865 struct dentry *parent = dget_parent(dentry);
866 nfs_readdir_record_entry_cache_hit(d_inode(parent));
871 static u32 nfs_get_valid_attrmask(struct inode *inode)
873 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
874 u32 reply_mask = STATX_INO | STATX_TYPE;
876 if (!(cache_validity & NFS_INO_INVALID_ATIME))
877 reply_mask |= STATX_ATIME;
878 if (!(cache_validity & NFS_INO_INVALID_CTIME))
879 reply_mask |= STATX_CTIME;
880 if (!(cache_validity & NFS_INO_INVALID_MTIME))
881 reply_mask |= STATX_MTIME;
882 if (!(cache_validity & NFS_INO_INVALID_SIZE))
883 reply_mask |= STATX_SIZE;
884 if (!(cache_validity & NFS_INO_INVALID_NLINK))
885 reply_mask |= STATX_NLINK;
886 if (!(cache_validity & NFS_INO_INVALID_MODE))
887 reply_mask |= STATX_MODE;
888 if (!(cache_validity & NFS_INO_INVALID_OTHER))
889 reply_mask |= STATX_UID | STATX_GID;
890 if (!(cache_validity & NFS_INO_INVALID_BLOCKS))
891 reply_mask |= STATX_BLOCKS;
892 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
893 reply_mask |= STATX_CHANGE_COOKIE;
897 int nfs_getattr(struct mnt_idmap *idmap, const struct path *path,
898 struct kstat *stat, u32 request_mask, unsigned int query_flags)
900 struct inode *inode = d_inode(path->dentry);
901 struct nfs_server *server = NFS_SERVER(inode);
902 unsigned long cache_validity;
904 bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
905 bool do_update = false;
906 bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode);
908 trace_nfs_getattr_enter(inode);
910 request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID |
911 STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME |
912 STATX_INO | STATX_SIZE | STATX_BLOCKS |
915 if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
916 if (readdirplus_enabled)
917 nfs_readdirplus_parent_cache_hit(path->dentry);
918 goto out_no_revalidate;
921 /* Flush out writes to the server in order to update c/mtime/version. */
922 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_CHANGE_COOKIE)) &&
923 S_ISREG(inode->i_mode)) {
924 if (nfs_have_delegated_mtime(inode))
925 filemap_fdatawrite(inode->i_mapping);
927 filemap_write_and_wait(inode->i_mapping);
931 * We may force a getattr if the user cares about atime.
933 * Note that we only have to check the vfsmount flags here:
934 * - NFS always sets S_NOATIME by so checking it would give a
936 * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
937 * no point in checking those.
939 if ((path->mnt->mnt_flags & MNT_NOATIME) ||
940 ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
941 request_mask &= ~STATX_ATIME;
943 /* Is the user requesting attributes that might need revalidation? */
944 if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
945 STATX_MTIME|STATX_UID|STATX_GID|
946 STATX_SIZE|STATX_BLOCKS|
947 STATX_CHANGE_COOKIE)))
948 goto out_no_revalidate;
950 /* Check whether the cached attributes are stale */
951 do_update |= force_sync || nfs_attribute_cache_expired(inode);
952 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
953 do_update |= cache_validity & NFS_INO_INVALID_CHANGE;
954 if (request_mask & STATX_ATIME)
955 do_update |= cache_validity & NFS_INO_INVALID_ATIME;
956 if (request_mask & STATX_CTIME)
957 do_update |= cache_validity & NFS_INO_INVALID_CTIME;
958 if (request_mask & STATX_MTIME)
959 do_update |= cache_validity & NFS_INO_INVALID_MTIME;
960 if (request_mask & STATX_SIZE)
961 do_update |= cache_validity & NFS_INO_INVALID_SIZE;
962 if (request_mask & STATX_NLINK)
963 do_update |= cache_validity & NFS_INO_INVALID_NLINK;
964 if (request_mask & STATX_MODE)
965 do_update |= cache_validity & NFS_INO_INVALID_MODE;
966 if (request_mask & (STATX_UID | STATX_GID))
967 do_update |= cache_validity & NFS_INO_INVALID_OTHER;
968 if (request_mask & STATX_BLOCKS)
969 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
972 if (readdirplus_enabled)
973 nfs_readdirplus_parent_cache_miss(path->dentry);
974 err = __nfs_revalidate_inode(server, inode);
977 } else if (readdirplus_enabled)
978 nfs_readdirplus_parent_cache_hit(path->dentry);
980 /* Only return attributes that were revalidated. */
981 stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask;
983 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
984 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
985 stat->change_cookie = inode_peek_iversion_raw(inode);
986 stat->attributes_mask |= STATX_ATTR_CHANGE_MONOTONIC;
987 if (server->change_attr_type != NFS4_CHANGE_TYPE_IS_UNDEFINED)
988 stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
989 if (S_ISDIR(inode->i_mode))
990 stat->blksize = NFS_SERVER(inode)->dtsize;
992 trace_nfs_getattr_exit(inode, err);
995 EXPORT_SYMBOL_GPL(nfs_getattr);
997 static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
999 refcount_set(&l_ctx->count, 1);
1000 l_ctx->lockowner = current->files;
1001 INIT_LIST_HEAD(&l_ctx->list);
1002 atomic_set(&l_ctx->io_count, 0);
1005 static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
1007 struct nfs_lock_context *pos;
1009 list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
1010 if (pos->lockowner != current->files)
1012 if (refcount_inc_not_zero(&pos->count))
1018 struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
1020 struct nfs_lock_context *res, *new = NULL;
1021 struct inode *inode = d_inode(ctx->dentry);
1024 res = __nfs_find_lock_context(ctx);
1027 new = kmalloc(sizeof(*new), GFP_KERNEL_ACCOUNT);
1029 return ERR_PTR(-ENOMEM);
1030 nfs_init_lock_context(new);
1031 spin_lock(&inode->i_lock);
1032 res = __nfs_find_lock_context(ctx);
1034 new->open_context = get_nfs_open_context(ctx);
1035 if (new->open_context) {
1036 list_add_tail_rcu(&new->list,
1037 &ctx->lock_context.list);
1041 res = ERR_PTR(-EBADF);
1043 spin_unlock(&inode->i_lock);
1048 EXPORT_SYMBOL_GPL(nfs_get_lock_context);
1050 void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
1052 struct nfs_open_context *ctx = l_ctx->open_context;
1053 struct inode *inode = d_inode(ctx->dentry);
1055 if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
1057 list_del_rcu(&l_ctx->list);
1058 spin_unlock(&inode->i_lock);
1059 put_nfs_open_context(ctx);
1060 kfree_rcu(l_ctx, rcu_head);
1062 EXPORT_SYMBOL_GPL(nfs_put_lock_context);
1065 * nfs_close_context - Common close_context() routine NFSv2/v3
1066 * @ctx: pointer to context
1067 * @is_sync: is this a synchronous close
1069 * Ensure that the attributes are up to date if we're mounted
1070 * with close-to-open semantics and we have cached data that will
1071 * need to be revalidated on open.
1073 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
1075 struct nfs_inode *nfsi;
1076 struct inode *inode;
1078 if (!(ctx->mode & FMODE_WRITE))
1082 inode = d_inode(ctx->dentry);
1083 if (nfs_have_read_or_write_delegation(inode))
1085 nfsi = NFS_I(inode);
1086 if (inode->i_mapping->nrpages == 0)
1088 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1090 if (!list_empty(&nfsi->open_files))
1092 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO)
1094 nfs_revalidate_inode(inode,
1095 NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
1097 EXPORT_SYMBOL_GPL(nfs_close_context);
1099 struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
1103 struct nfs_open_context *ctx;
1105 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
1107 return ERR_PTR(-ENOMEM);
1108 nfs_sb_active(dentry->d_sb);
1109 ctx->dentry = dget(dentry);
1111 ctx->cred = get_cred(filp->f_cred);
1113 ctx->cred = get_current_cred();
1114 rcu_assign_pointer(ctx->ll_cred, NULL);
1119 ctx->flock_owner = (fl_owner_t)filp;
1120 nfs_init_lock_context(&ctx->lock_context);
1121 ctx->lock_context.open_context = ctx;
1122 INIT_LIST_HEAD(&ctx->list);
1123 ctx->mdsthreshold = NULL;
1126 EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
1128 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1130 if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1134 EXPORT_SYMBOL_GPL(get_nfs_open_context);
1136 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1138 struct inode *inode = d_inode(ctx->dentry);
1139 struct super_block *sb = ctx->dentry->d_sb;
1141 if (!refcount_dec_and_test(&ctx->lock_context.count))
1143 if (!list_empty(&ctx->list)) {
1144 spin_lock(&inode->i_lock);
1145 list_del_rcu(&ctx->list);
1146 spin_unlock(&inode->i_lock);
1149 NFS_PROTO(inode)->close_context(ctx, is_sync);
1150 put_cred(ctx->cred);
1152 nfs_sb_deactive(sb);
1153 put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1));
1154 kfree(ctx->mdsthreshold);
1155 kfree_rcu(ctx, rcu_head);
1158 void put_nfs_open_context(struct nfs_open_context *ctx)
1160 __put_nfs_open_context(ctx, 0);
1162 EXPORT_SYMBOL_GPL(put_nfs_open_context);
1164 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1166 __put_nfs_open_context(ctx, 1);
1170 * Ensure that mmap has a recent RPC credential for use when writing out
1173 void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
1175 struct inode *inode = d_inode(ctx->dentry);
1176 struct nfs_inode *nfsi = NFS_I(inode);
1178 spin_lock(&inode->i_lock);
1179 if (list_empty(&nfsi->open_files) &&
1181 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA |
1182 NFS_INO_REVAL_FORCED);
1183 list_add_tail_rcu(&ctx->list, &nfsi->open_files);
1184 spin_unlock(&inode->i_lock);
1186 EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1188 void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1190 filp->private_data = get_nfs_open_context(ctx);
1191 set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1192 if (list_empty(&ctx->list))
1193 nfs_inode_attach_open_context(ctx);
1195 EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
1198 * Given an inode, search for an open context with the desired characteristics
1200 struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
1202 struct nfs_inode *nfsi = NFS_I(inode);
1203 struct nfs_open_context *pos, *ctx = NULL;
1206 list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
1207 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
1209 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1211 if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
1213 ctx = get_nfs_open_context(pos);
1221 void nfs_file_clear_open_context(struct file *filp)
1223 struct nfs_open_context *ctx = nfs_file_open_context(filp);
1226 struct inode *inode = d_inode(ctx->dentry);
1228 clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1230 * We fatal error on write before. Try to writeback
1234 invalidate_inode_pages2(inode->i_mapping);
1235 filp->private_data = NULL;
1236 put_nfs_open_context_sync(ctx);
1241 * These allocate and release file read/write context information.
1243 int nfs_open(struct inode *inode, struct file *filp)
1245 struct nfs_open_context *ctx;
1247 ctx = alloc_nfs_open_context(file_dentry(filp),
1248 flags_to_mode(filp->f_flags), filp);
1250 return PTR_ERR(ctx);
1251 nfs_file_set_open_context(filp, ctx);
1252 put_nfs_open_context(ctx);
1253 nfs_fscache_open_file(inode, filp);
1258 * This function is called whenever some part of NFS notices that
1259 * the cached attributes have to be refreshed.
1262 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1264 int status = -ESTALE;
1265 struct nfs_fattr *fattr = NULL;
1266 struct nfs_inode *nfsi = NFS_I(inode);
1268 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1269 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
1271 trace_nfs_revalidate_inode_enter(inode);
1273 if (is_bad_inode(inode))
1275 if (NFS_STALE(inode))
1278 /* pNFS: Attributes aren't updated until we layoutcommit */
1279 if (S_ISREG(inode->i_mode)) {
1280 status = pnfs_sync_inode(inode, false);
1286 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1290 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1292 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode);
1294 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
1296 (unsigned long long)NFS_FILEID(inode), status);
1299 /* A soft timeout occurred. Use cached information? */
1300 if (server->flags & NFS_MOUNT_SOFTREVAL)
1304 if (!S_ISDIR(inode->i_mode))
1305 nfs_set_inode_stale(inode);
1307 nfs_zap_caches(inode);
1312 status = nfs_refresh_inode(inode, fattr);
1314 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
1316 (unsigned long long)NFS_FILEID(inode), status);
1320 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
1321 nfs_zap_acl_cache(inode);
1323 nfs_setsecurity(inode, fattr);
1325 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
1327 (unsigned long long)NFS_FILEID(inode));
1330 nfs_free_fattr(fattr);
1331 trace_nfs_revalidate_inode_exit(inode, status);
1335 int nfs_attribute_cache_expired(struct inode *inode)
1337 if (nfs_have_delegated_attributes(inode))
1339 return nfs_attribute_timeout(inode);
1343 * nfs_revalidate_inode - Revalidate the inode attributes
1344 * @inode: pointer to inode struct
1345 * @flags: cache flags to check
1347 * Updates inode attribute information by retrieving the data from the server.
1349 int nfs_revalidate_inode(struct inode *inode, unsigned long flags)
1351 if (!nfs_check_cache_invalid(inode, flags))
1352 return NFS_STALE(inode) ? -ESTALE : 0;
1353 return __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1355 EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
1357 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
1361 nfs_fscache_invalidate(inode, 0);
1362 if (mapping->nrpages != 0) {
1363 if (S_ISREG(inode->i_mode)) {
1364 ret = nfs_sync_mapping(mapping);
1368 ret = invalidate_inode_pages2(mapping);
1372 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
1374 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1376 (unsigned long long)NFS_FILEID(inode));
1381 * nfs_clear_invalid_mapping - Conditionally clear a mapping
1382 * @mapping: pointer to mapping
1384 * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping.
1386 int nfs_clear_invalid_mapping(struct address_space *mapping)
1388 struct inode *inode = mapping->host;
1389 struct nfs_inode *nfsi = NFS_I(inode);
1390 unsigned long *bitlock = &nfsi->flags;
1394 * We must clear NFS_INO_INVALID_DATA first to ensure that
1395 * invalidations that come in while we're shooting down the mappings
1396 * are respected. But, that leaves a race window where one revalidator
1397 * can clear the flag, and then another checks it before the mapping
1398 * gets invalidated. Fix that by serializing access to this part of
1401 * At the same time, we need to allow other tasks to see whether we
1402 * might be in the middle of invalidating the pages, so we only set
1403 * the bit lock here if it looks like we're going to be doing that.
1406 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1407 nfs_wait_bit_killable,
1408 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
1411 spin_lock(&inode->i_lock);
1412 if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1413 spin_unlock(&inode->i_lock);
1416 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1418 spin_unlock(&inode->i_lock);
1422 set_bit(NFS_INO_INVALIDATING, bitlock);
1424 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
1425 nfs_ooo_clear(nfsi);
1426 spin_unlock(&inode->i_lock);
1427 trace_nfs_invalidate_mapping_enter(inode);
1428 ret = nfs_invalidate_mapping(inode, mapping);
1429 trace_nfs_invalidate_mapping_exit(inode, ret);
1431 clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
1432 smp_mb__after_atomic();
1433 wake_up_bit(bitlock, NFS_INO_INVALIDATING);
1438 bool nfs_mapping_need_revalidate_inode(struct inode *inode)
1440 return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) ||
1444 int nfs_revalidate_mapping_rcu(struct inode *inode)
1446 struct nfs_inode *nfsi = NFS_I(inode);
1447 unsigned long *bitlock = &nfsi->flags;
1450 if (IS_SWAPFILE(inode))
1452 if (nfs_mapping_need_revalidate_inode(inode)) {
1456 spin_lock(&inode->i_lock);
1457 if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1458 (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1460 spin_unlock(&inode->i_lock);
1466 * nfs_revalidate_mapping - Revalidate the pagecache
1467 * @inode: pointer to host inode
1468 * @mapping: pointer to mapping
1470 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
1472 /* swapfiles are not supposed to be shared. */
1473 if (IS_SWAPFILE(inode))
1476 if (nfs_mapping_need_revalidate_inode(inode)) {
1477 int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1482 return nfs_clear_invalid_mapping(mapping);
1485 static bool nfs_file_has_writers(struct nfs_inode *nfsi)
1487 struct inode *inode = &nfsi->vfs_inode;
1489 if (!S_ISREG(inode->i_mode))
1491 if (list_empty(&nfsi->open_files))
1493 return inode_is_open_for_write(inode);
1496 static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
1498 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
1501 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1503 struct timespec64 ts;
1505 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1506 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
1507 && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
1508 inode_set_iversion_raw(inode, fattr->change_attr);
1509 if (S_ISDIR(inode->i_mode))
1510 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1511 else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1512 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
1514 /* If we have atomic WCC data, we may update some attributes */
1515 ts = inode_get_ctime(inode);
1516 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1517 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
1518 && timespec64_equal(&ts, &fattr->pre_ctime)) {
1519 inode_set_ctime_to_ts(inode, fattr->ctime);
1522 ts = inode_get_mtime(inode);
1523 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1524 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
1525 && timespec64_equal(&ts, &fattr->pre_mtime)) {
1526 inode_set_mtime_to_ts(inode, fattr->mtime);
1528 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1529 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
1530 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
1531 && !nfs_have_writebacks(inode)) {
1532 trace_nfs_size_wcc(inode, fattr->size);
1533 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
1538 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1539 * @inode: pointer to inode
1540 * @fattr: updated attributes
1542 * Verifies the attribute cache. If we have just changed the attributes,
1543 * so that fattr carries weak cache consistency data, then it may
1544 * also update the ctime/mtime/change_attribute.
1546 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1548 struct nfs_inode *nfsi = NFS_I(inode);
1549 loff_t cur_size, new_isize;
1550 unsigned long invalid = 0;
1551 struct timespec64 ts;
1553 if (nfs_have_delegated_attributes(inode))
1556 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1557 /* Only a mounted-on-fileid? Just exit */
1558 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1560 /* Has the inode gone and changed behind our back? */
1561 } else if (nfsi->fileid != fattr->fileid) {
1562 /* Is this perhaps the mounted-on fileid? */
1563 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1564 nfsi->fileid == fattr->mounted_on_fileid)
1568 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
1572 if (!nfs_file_has_buffered_writers(nfsi)) {
1573 /* Verify a few of the more important attributes */
1574 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
1575 invalid |= NFS_INO_INVALID_CHANGE;
1577 ts = inode_get_mtime(inode);
1578 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
1579 invalid |= NFS_INO_INVALID_MTIME;
1581 ts = inode_get_ctime(inode);
1582 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
1583 invalid |= NFS_INO_INVALID_CTIME;
1585 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1586 cur_size = i_size_read(inode);
1587 new_isize = nfs_size_to_loff_t(fattr->size);
1588 if (cur_size != new_isize)
1589 invalid |= NFS_INO_INVALID_SIZE;
1593 /* Have any file permissions changed? */
1594 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
1595 invalid |= NFS_INO_INVALID_MODE;
1596 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
1597 invalid |= NFS_INO_INVALID_OTHER;
1598 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
1599 invalid |= NFS_INO_INVALID_OTHER;
1601 /* Has the link count changed? */
1602 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
1603 invalid |= NFS_INO_INVALID_NLINK;
1605 ts = inode_get_atime(inode);
1606 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
1607 invalid |= NFS_INO_INVALID_ATIME;
1610 nfs_set_cache_invalid(inode, invalid);
1612 nfsi->read_cache_jiffies = fattr->time_start;
1616 static atomic_long_t nfs_attr_generation_counter;
1618 static unsigned long nfs_read_attr_generation_counter(void)
1620 return atomic_long_read(&nfs_attr_generation_counter);
1623 unsigned long nfs_inc_attr_generation_counter(void)
1625 return atomic_long_inc_return(&nfs_attr_generation_counter);
1627 EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
1629 void nfs_fattr_init(struct nfs_fattr *fattr)
1632 fattr->time_start = jiffies;
1633 fattr->gencount = nfs_inc_attr_generation_counter();
1634 fattr->owner_name = NULL;
1635 fattr->group_name = NULL;
1636 fattr->mdsthreshold = NULL;
1638 EXPORT_SYMBOL_GPL(nfs_fattr_init);
1641 * nfs_fattr_set_barrier
1642 * @fattr: attributes
1644 * Used to set a barrier after an attribute was updated. This
1645 * barrier ensures that older attributes from RPC calls that may
1646 * have raced with our update cannot clobber these new values.
1647 * Note that you are still responsible for ensuring that other
1648 * operations which change the attribute on the server do not
1651 void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1653 fattr->gencount = nfs_inc_attr_generation_counter();
1656 struct nfs_fattr *nfs_alloc_fattr(void)
1658 struct nfs_fattr *fattr;
1660 fattr = kmalloc(sizeof(*fattr), GFP_KERNEL);
1661 if (fattr != NULL) {
1662 nfs_fattr_init(fattr);
1663 fattr->label = NULL;
1667 EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
1669 struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server)
1671 struct nfs_fattr *fattr = nfs_alloc_fattr();
1676 fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
1677 if (IS_ERR(fattr->label)) {
1684 EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label);
1686 struct nfs_fh *nfs_alloc_fhandle(void)
1690 fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
1695 EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
1699 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1700 * in the same way that wireshark does
1704 * For debugging only.
1706 u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1708 /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1709 * not on the result */
1710 return nfs_fhandle_hash(fh);
1712 EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
1715 * _nfs_display_fhandle - display an NFS file handle on the console
1717 * @fh: file handle to display
1718 * @caption: display caption
1720 * For debugging only.
1722 void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1726 if (fh == NULL || fh->size == 0) {
1727 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1731 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1732 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
1733 for (i = 0; i < fh->size; i += 16) {
1734 __be32 *pos = (__be32 *)&fh->data[i];
1736 switch ((fh->size - i - 1) >> 2) {
1738 printk(KERN_DEFAULT " %08x\n",
1742 printk(KERN_DEFAULT " %08x %08x\n",
1743 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1746 printk(KERN_DEFAULT " %08x %08x %08x\n",
1747 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1748 be32_to_cpup(pos + 2));
1751 printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1752 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1753 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1757 EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
1761 * nfs_inode_attrs_cmp_generic - compare attributes
1762 * @fattr: attributes
1763 * @inode: pointer to inode
1765 * Attempt to divine whether or not an RPC call reply carrying stale
1766 * attributes got scheduled after another call carrying updated ones.
1767 * Note also the check for wraparound of 'attr_gencount'
1769 * The function returns '1' if it thinks the attributes in @fattr are
1770 * more recent than the ones cached in @inode. Otherwise it returns
1773 static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr,
1774 const struct inode *inode)
1776 unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
1778 return (long)(fattr->gencount - attr_gencount) > 0 ||
1779 (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
1783 * nfs_inode_attrs_cmp_monotonic - compare attributes
1784 * @fattr: attributes
1785 * @inode: pointer to inode
1787 * Attempt to divine whether or not an RPC call reply carrying stale
1788 * attributes got scheduled after another call carrying updated ones.
1790 * We assume that the server observes monotonic semantics for
1791 * the change attribute, so a larger value means that the attributes in
1792 * @fattr are more recent, in which case the function returns the
1794 * A return value of '0' indicates no measurable change
1795 * A return value of '-1' means that the attributes in @inode are
1798 static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr,
1799 const struct inode *inode)
1801 s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode);
1804 return diff == 0 ? 0 : -1;
1808 * nfs_inode_attrs_cmp_strict_monotonic - compare attributes
1809 * @fattr: attributes
1810 * @inode: pointer to inode
1812 * Attempt to divine whether or not an RPC call reply carrying stale
1813 * attributes got scheduled after another call carrying updated ones.
1815 * We assume that the server observes strictly monotonic semantics for
1816 * the change attribute, so a larger value means that the attributes in
1817 * @fattr are more recent, in which case the function returns the
1819 * A return value of '-1' means that the attributes in @inode are
1820 * more recent or unchanged.
1822 static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr,
1823 const struct inode *inode)
1825 return nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1;
1829 * nfs_inode_attrs_cmp - compare attributes
1830 * @fattr: attributes
1831 * @inode: pointer to inode
1833 * This function returns '1' if it thinks the attributes in @fattr are
1834 * more recent than the ones cached in @inode. It returns '-1' if
1835 * the attributes in @inode are more recent than the ones in @fattr,
1836 * and it returns 0 if not sure.
1838 static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr,
1839 const struct inode *inode)
1841 if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0)
1843 switch (NFS_SERVER(inode)->change_attr_type) {
1844 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1846 case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1847 if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
1849 return nfs_inode_attrs_cmp_monotonic(fattr, inode);
1851 if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
1853 return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode);
1859 * nfs_inode_finish_partial_attr_update - complete a previous inode update
1860 * @fattr: attributes
1861 * @inode: pointer to inode
1863 * Returns '1' if the last attribute update left the inode cached
1864 * attributes in a partially unrevalidated state, and @fattr
1865 * matches the change attribute of that partial update.
1866 * Otherwise returns '0'.
1868 static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr,
1869 const struct inode *inode)
1871 const unsigned long check_valid =
1872 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
1873 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
1874 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER |
1875 NFS_INO_INVALID_NLINK;
1876 unsigned long cache_validity = NFS_I(inode)->cache_validity;
1877 enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type;
1879 if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED &&
1880 !(cache_validity & NFS_INO_INVALID_CHANGE) &&
1881 (cache_validity & check_valid) != 0 &&
1882 (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1883 nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0)
1888 static void nfs_ooo_merge(struct nfs_inode *nfsi,
1893 if (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)
1894 /* No point merging anything */
1898 nfsi->ooo = kmalloc(sizeof(*nfsi->ooo), GFP_ATOMIC);
1900 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
1906 /* add this range, merging if possible */
1907 cnt = nfsi->ooo->cnt;
1908 for (i = 0; i < cnt; i++) {
1909 if (end == nfsi->ooo->gap[i].start)
1910 end = nfsi->ooo->gap[i].end;
1911 else if (start == nfsi->ooo->gap[i].end)
1912 start = nfsi->ooo->gap[i].start;
1915 /* Remove 'i' from table and loop to insert the new range */
1917 nfsi->ooo->gap[i] = nfsi->ooo->gap[cnt];
1921 if (cnt >= ARRAY_SIZE(nfsi->ooo->gap)) {
1922 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
1927 nfsi->ooo->gap[cnt].start = start;
1928 nfsi->ooo->gap[cnt].end = end;
1931 nfsi->ooo->cnt = cnt;
1934 static void nfs_ooo_record(struct nfs_inode *nfsi,
1935 struct nfs_fattr *fattr)
1937 /* This reply was out-of-order, so record in the
1938 * pre/post change id, possibly cancelling
1939 * gaps created when iversion was jumpped forward.
1941 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) &&
1942 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE))
1945 fattr->pre_change_attr);
1948 static int nfs_refresh_inode_locked(struct inode *inode,
1949 struct nfs_fattr *fattr)
1951 int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
1954 trace_nfs_refresh_inode_enter(inode);
1956 if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode))
1957 ret = nfs_update_inode(inode, fattr);
1959 nfs_ooo_record(NFS_I(inode), fattr);
1962 ret = nfs_check_inode_attributes(inode, fattr);
1965 trace_nfs_refresh_inode_exit(inode, ret);
1970 * nfs_refresh_inode - try to update the inode attribute cache
1971 * @inode: pointer to inode
1972 * @fattr: updated attributes
1974 * Check that an RPC call that returned attributes has not overlapped with
1975 * other recent updates of the inode metadata, then decide whether it is
1976 * safe to do a full update of the inode attributes, or whether just to
1977 * call nfs_check_inode_attributes.
1979 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1983 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1985 spin_lock(&inode->i_lock);
1986 status = nfs_refresh_inode_locked(inode, fattr);
1987 spin_unlock(&inode->i_lock);
1991 EXPORT_SYMBOL_GPL(nfs_refresh_inode);
1993 static int nfs_post_op_update_inode_locked(struct inode *inode,
1994 struct nfs_fattr *fattr, unsigned int invalid)
1996 if (S_ISDIR(inode->i_mode))
1997 invalid |= NFS_INO_INVALID_DATA;
1998 nfs_set_cache_invalid(inode, invalid);
1999 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
2001 return nfs_refresh_inode_locked(inode, fattr);
2005 * nfs_post_op_update_inode - try to update the inode attribute cache
2006 * @inode: pointer to inode
2007 * @fattr: updated attributes
2009 * After an operation that has changed the inode metadata, mark the
2010 * attribute cache as being invalid, then try to update it.
2012 * NB: if the server didn't return any post op attributes, this
2013 * function will force the retrieval of attributes before the next
2014 * NFS request. Thus it should be used only for operations that
2015 * are expected to change one or more attributes, to avoid
2016 * unnecessary NFS requests and trips through nfs_update_inode().
2018 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
2022 spin_lock(&inode->i_lock);
2023 nfs_fattr_set_barrier(fattr);
2024 status = nfs_post_op_update_inode_locked(inode, fattr,
2025 NFS_INO_INVALID_CHANGE
2026 | NFS_INO_INVALID_CTIME
2027 | NFS_INO_REVAL_FORCED);
2028 spin_unlock(&inode->i_lock);
2032 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
2035 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
2036 * @inode: pointer to inode
2037 * @fattr: updated attributes
2039 * After an operation that has changed the inode metadata, mark the
2040 * attribute cache as being invalid, then try to update it. Fake up
2041 * weak cache consistency data, if none exist.
2043 * This function is mainly designed to be used by the ->write_done() functions.
2045 int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
2047 int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
2050 /* Don't do a WCC update if these attributes are already stale */
2053 if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) {
2054 /* Record the pre/post change info before clearing PRECHANGE */
2055 nfs_ooo_record(NFS_I(inode), fattr);
2056 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
2057 | NFS_ATTR_FATTR_PRESIZE
2058 | NFS_ATTR_FATTR_PREMTIME
2059 | NFS_ATTR_FATTR_PRECTIME);
2062 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
2063 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
2064 fattr->pre_change_attr = inode_peek_iversion_raw(inode);
2065 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
2067 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
2068 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
2069 fattr->pre_ctime = inode_get_ctime(inode);
2070 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
2072 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
2073 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
2074 fattr->pre_mtime = inode_get_mtime(inode);
2075 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
2077 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
2078 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
2079 fattr->pre_size = i_size_read(inode);
2080 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
2083 status = nfs_post_op_update_inode_locked(inode, fattr,
2084 NFS_INO_INVALID_CHANGE
2085 | NFS_INO_INVALID_CTIME
2086 | NFS_INO_INVALID_MTIME
2087 | NFS_INO_INVALID_BLOCKS);
2092 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
2093 * @inode: pointer to inode
2094 * @fattr: updated attributes
2096 * After an operation that has changed the inode metadata, mark the
2097 * attribute cache as being invalid, then try to update it. Fake up
2098 * weak cache consistency data, if none exist.
2100 * This function is mainly designed to be used by the ->write_done() functions.
2102 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
2106 spin_lock(&inode->i_lock);
2107 nfs_fattr_set_barrier(fattr);
2108 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
2109 spin_unlock(&inode->i_lock);
2112 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
2116 * Many nfs protocol calls return the new file attributes after
2117 * an operation. Here we update the inode to reflect the state
2118 * of the server's inode.
2120 * This is a bit tricky because we have to make sure all dirty pages
2121 * have been sent off to the server before calling invalidate_inode_pages.
2122 * To make sure no other process adds more write requests while we try
2123 * our best to flush them, we make them sleep during the attribute refresh.
2125 * A very similar scenario holds for the dir cache.
2127 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
2129 struct nfs_server *server = NFS_SERVER(inode);
2130 struct nfs_inode *nfsi = NFS_I(inode);
2131 loff_t cur_isize, new_isize;
2132 u64 fattr_supported = server->fattr_valid;
2133 unsigned long invalid = 0;
2134 unsigned long now = jiffies;
2135 unsigned long save_cache_validity;
2136 bool have_writers = nfs_file_has_buffered_writers(nfsi);
2137 bool cache_revalidated = true;
2138 bool attr_changed = false;
2139 bool have_delegation;
2141 dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
2142 __func__, inode->i_sb->s_id, inode->i_ino,
2143 nfs_display_fhandle_hash(NFS_FH(inode)),
2144 atomic_read(&inode->i_count), fattr->valid);
2146 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
2147 /* Only a mounted-on-fileid? Just exit */
2148 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
2150 /* Has the inode gone and changed behind our back? */
2151 } else if (nfsi->fileid != fattr->fileid) {
2152 /* Is this perhaps the mounted-on fileid? */
2153 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
2154 nfsi->fileid == fattr->mounted_on_fileid)
2156 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
2157 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
2158 NFS_SERVER(inode)->nfs_client->cl_hostname,
2159 inode->i_sb->s_id, (long long)nfsi->fileid,
2160 (long long)fattr->fileid);
2165 * Make sure the inode's type hasn't changed.
2167 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
2169 * Big trouble! The inode has become a different object.
2171 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
2172 __func__, inode->i_ino, inode->i_mode, fattr->mode);
2176 /* Update the fsid? */
2177 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
2178 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
2179 !IS_AUTOMOUNT(inode))
2180 server->fsid = fattr->fsid;
2182 /* Save the delegation state before clearing cache_validity */
2183 have_delegation = nfs_have_delegated_attributes(inode);
2186 * Update the read time so we don't revalidate too often.
2188 nfsi->read_cache_jiffies = fattr->time_start;
2190 /* Fix up any delegated attributes in the struct nfs_fattr */
2191 nfs_fattr_fixup_delegated(inode, fattr);
2193 save_cache_validity = nfsi->cache_validity;
2194 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
2195 | NFS_INO_INVALID_ATIME
2196 | NFS_INO_REVAL_FORCED
2197 | NFS_INO_INVALID_BLOCKS);
2199 /* Do atomic weak cache consistency updates */
2200 nfs_wcc_update_inode(inode, fattr);
2202 if (pnfs_layoutcommit_outstanding(inode)) {
2203 nfsi->cache_validity |=
2204 save_cache_validity &
2205 (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
2206 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
2207 NFS_INO_INVALID_BLOCKS);
2208 cache_revalidated = false;
2211 /* More cache consistency checks */
2212 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
2213 if (!have_writers && nfsi->ooo && nfsi->ooo->cnt == 1 &&
2214 nfsi->ooo->gap[0].end == inode_peek_iversion_raw(inode)) {
2215 /* There is one remaining gap that hasn't been
2216 * merged into iversion - do that now.
2218 inode_set_iversion_raw(inode, nfsi->ooo->gap[0].start);
2222 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
2223 /* Could it be a race with writeback? */
2224 if (!(have_writers || have_delegation)) {
2225 invalid |= NFS_INO_INVALID_DATA
2226 | NFS_INO_INVALID_ACCESS
2227 | NFS_INO_INVALID_ACL
2228 | NFS_INO_INVALID_XATTR;
2229 /* Force revalidate of all attributes */
2230 save_cache_validity |= NFS_INO_INVALID_CTIME
2231 | NFS_INO_INVALID_MTIME
2232 | NFS_INO_INVALID_SIZE
2233 | NFS_INO_INVALID_BLOCKS
2234 | NFS_INO_INVALID_NLINK
2235 | NFS_INO_INVALID_MODE
2236 | NFS_INO_INVALID_OTHER;
2237 if (S_ISDIR(inode->i_mode))
2238 nfs_force_lookup_revalidate(inode);
2239 attr_changed = true;
2240 dprintk("NFS: change_attr change on server for file %s/%ld\n",
2243 } else if (!have_delegation) {
2244 nfs_ooo_record(nfsi, fattr);
2245 nfs_ooo_merge(nfsi, inode_peek_iversion_raw(inode),
2246 fattr->change_attr);
2248 inode_set_iversion_raw(inode, fattr->change_attr);
2251 nfsi->cache_validity |=
2252 save_cache_validity & NFS_INO_INVALID_CHANGE;
2253 if (!have_delegation ||
2254 (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0)
2255 cache_revalidated = false;
2258 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
2259 inode_set_mtime_to_ts(inode, fattr->mtime);
2260 else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
2261 nfsi->cache_validity |=
2262 save_cache_validity & NFS_INO_INVALID_MTIME;
2264 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
2265 inode_set_ctime_to_ts(inode, fattr->ctime);
2266 else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
2267 nfsi->cache_validity |=
2268 save_cache_validity & NFS_INO_INVALID_CTIME;
2270 /* Check if our cached file size is stale */
2271 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
2272 new_isize = nfs_size_to_loff_t(fattr->size);
2273 cur_isize = i_size_read(inode);
2274 if (new_isize != cur_isize && !have_delegation) {
2275 /* Do we perhaps have any outstanding writes, or has
2276 * the file grown beyond our last write? */
2277 if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
2278 trace_nfs_size_update(inode, new_isize);
2279 i_size_write(inode, new_isize);
2281 invalid |= NFS_INO_INVALID_DATA;
2284 if (new_isize == 0 &&
2285 !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED |
2286 NFS_ATTR_FATTR_BLOCKS_USED))) {
2287 fattr->du.nfs3.used = 0;
2288 fattr->valid |= NFS_ATTR_FATTR_SPACE_USED;
2291 nfsi->cache_validity |=
2292 save_cache_validity & NFS_INO_INVALID_SIZE;
2294 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
2295 inode_set_atime_to_ts(inode, fattr->atime);
2296 else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
2297 nfsi->cache_validity |=
2298 save_cache_validity & NFS_INO_INVALID_ATIME;
2300 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
2301 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
2302 umode_t newmode = inode->i_mode & S_IFMT;
2303 newmode |= fattr->mode & S_IALLUGO;
2304 inode->i_mode = newmode;
2305 invalid |= NFS_INO_INVALID_ACCESS
2306 | NFS_INO_INVALID_ACL;
2308 } else if (fattr_supported & NFS_ATTR_FATTR_MODE)
2309 nfsi->cache_validity |=
2310 save_cache_validity & NFS_INO_INVALID_MODE;
2312 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
2313 if (!uid_eq(inode->i_uid, fattr->uid)) {
2314 invalid |= NFS_INO_INVALID_ACCESS
2315 | NFS_INO_INVALID_ACL;
2316 inode->i_uid = fattr->uid;
2318 } else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
2319 nfsi->cache_validity |=
2320 save_cache_validity & NFS_INO_INVALID_OTHER;
2322 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
2323 if (!gid_eq(inode->i_gid, fattr->gid)) {
2324 invalid |= NFS_INO_INVALID_ACCESS
2325 | NFS_INO_INVALID_ACL;
2326 inode->i_gid = fattr->gid;
2328 } else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
2329 nfsi->cache_validity |=
2330 save_cache_validity & NFS_INO_INVALID_OTHER;
2332 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2333 if (inode->i_nlink != fattr->nlink)
2334 set_nlink(inode, fattr->nlink);
2335 } else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
2336 nfsi->cache_validity |=
2337 save_cache_validity & NFS_INO_INVALID_NLINK;
2339 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
2341 * report the blocks in 512byte units
2343 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
2344 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED)
2345 nfsi->cache_validity |=
2346 save_cache_validity & NFS_INO_INVALID_BLOCKS;
2348 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
2349 inode->i_blocks = fattr->du.nfs2.blocks;
2350 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED)
2351 nfsi->cache_validity |=
2352 save_cache_validity & NFS_INO_INVALID_BLOCKS;
2354 /* Update attrtimeo value if we're out of the unstable period */
2356 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
2357 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
2358 nfsi->attrtimeo_timestamp = now;
2359 /* Set barrier to be more recent than all outstanding updates */
2360 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
2362 if (cache_revalidated) {
2363 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2364 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2365 nfsi->attrtimeo <<= 1;
2366 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2367 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2369 nfsi->attrtimeo_timestamp = now;
2371 /* Set the barrier to be more recent than this fattr */
2372 if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
2373 nfsi->attr_gencount = fattr->gencount;
2376 /* Don't invalidate the data if we were to blame */
2377 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2378 || S_ISLNK(inode->i_mode)))
2379 invalid &= ~NFS_INO_INVALID_DATA;
2380 nfs_set_cache_invalid(inode, invalid);
2385 * No need to worry about unhashing the dentry, as the
2386 * lookup validation will know that the inode is bad.
2387 * (But we fall through to invalidate the caches.)
2389 nfs_set_inode_stale_locked(inode);
2393 struct inode *nfs_alloc_inode(struct super_block *sb)
2395 struct nfs_inode *nfsi;
2396 nfsi = alloc_inode_sb(sb, nfs_inode_cachep, GFP_KERNEL);
2400 nfsi->cache_validity = 0UL;
2402 #if IS_ENABLED(CONFIG_NFS_V4)
2403 nfsi->nfs4_acl = NULL;
2404 #endif /* CONFIG_NFS_V4 */
2405 #ifdef CONFIG_NFS_V4_2
2406 nfsi->xattr_cache = NULL;
2408 nfs_netfs_inode_init(nfsi);
2410 return &nfsi->vfs_inode;
2412 EXPORT_SYMBOL_GPL(nfs_alloc_inode);
2414 void nfs_free_inode(struct inode *inode)
2416 kfree(NFS_I(inode)->ooo);
2417 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2419 EXPORT_SYMBOL_GPL(nfs_free_inode);
2421 static inline void nfs4_init_once(struct nfs_inode *nfsi)
2423 #if IS_ENABLED(CONFIG_NFS_V4)
2424 INIT_LIST_HEAD(&nfsi->open_states);
2425 nfsi->delegation = NULL;
2426 init_rwsem(&nfsi->rwsem);
2427 nfsi->layout = NULL;
2431 static void init_once(void *foo)
2433 struct nfs_inode *nfsi = foo;
2435 inode_init_once(&nfsi->vfs_inode);
2436 INIT_LIST_HEAD(&nfsi->open_files);
2437 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2438 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
2439 nfs4_init_once(nfsi);
2442 static int __init nfs_init_inodecache(void)
2444 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2445 sizeof(struct nfs_inode),
2446 0, (SLAB_RECLAIM_ACCOUNT|
2449 if (nfs_inode_cachep == NULL)
2455 static void nfs_destroy_inodecache(void)
2458 * Make sure all delayed rcu free inodes are flushed before we
2462 kmem_cache_destroy(nfs_inode_cachep);
2465 struct workqueue_struct *nfslocaliod_workqueue;
2466 struct workqueue_struct *nfsiod_workqueue;
2467 EXPORT_SYMBOL_GPL(nfsiod_workqueue);
2470 * Destroy the nfsiod workqueues
2472 static void nfsiod_stop(void)
2474 struct workqueue_struct *wq;
2476 wq = nfsiod_workqueue;
2478 nfsiod_workqueue = NULL;
2479 destroy_workqueue(wq);
2481 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2482 wq = nfslocaliod_workqueue;
2484 nfslocaliod_workqueue = NULL;
2485 destroy_workqueue(wq);
2487 #endif /* CONFIG_NFS_LOCALIO */
2491 * Start the nfsiod workqueues
2493 static int nfsiod_start(void)
2495 dprintk("RPC: creating workqueue nfsiod\n");
2496 nfsiod_workqueue = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
2497 if (nfsiod_workqueue == NULL)
2499 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2501 * localio writes need to use a normal (non-memreclaim) workqueue.
2502 * When we start getting low on space, XFS goes and calls flush_work() on
2503 * a non-memreclaim work queue, which causes a priority inversion problem.
2505 dprintk("RPC: creating workqueue nfslocaliod\n");
2506 nfslocaliod_workqueue = alloc_workqueue("nfslocaliod", WQ_UNBOUND, 0);
2507 if (unlikely(nfslocaliod_workqueue == NULL)) {
2511 #endif /* CONFIG_NFS_LOCALIO */
2515 unsigned int nfs_net_id;
2516 EXPORT_SYMBOL_GPL(nfs_net_id);
2518 static int nfs_net_init(struct net *net)
2520 struct nfs_net *nn = net_generic(net, nfs_net_id);
2522 nfs_clients_init(net);
2524 if (!rpc_proc_register(net, &nn->rpcstats)) {
2525 nfs_clients_exit(net);
2529 return nfs_fs_proc_net_init(net);
2532 static void nfs_net_exit(struct net *net)
2534 rpc_proc_unregister(net, "nfs");
2535 nfs_fs_proc_net_exit(net);
2536 nfs_clients_exit(net);
2539 static struct pernet_operations nfs_net_ops = {
2540 .init = nfs_net_init,
2541 .exit = nfs_net_exit,
2543 .size = sizeof(struct nfs_net),
2549 static int __init init_nfs_fs(void)
2553 err = nfs_sysfs_init();
2557 err = register_pernet_subsys(&nfs_net_ops);
2561 err = nfsiod_start();
2565 err = nfs_fs_proc_init();
2569 err = nfs_init_nfspagecache();
2573 err = nfs_init_inodecache();
2577 err = nfs_init_readpagecache();
2581 err = nfs_init_writepagecache();
2585 err = nfs_init_directcache();
2589 err = register_nfs_fs();
2595 nfs_destroy_directcache();
2597 nfs_destroy_writepagecache();
2599 nfs_destroy_readpagecache();
2601 nfs_destroy_inodecache();
2603 nfs_destroy_nfspagecache();
2609 unregister_pernet_subsys(&nfs_net_ops);
2616 static void __exit exit_nfs_fs(void)
2618 nfs_destroy_directcache();
2619 nfs_destroy_writepagecache();
2620 nfs_destroy_readpagecache();
2621 nfs_destroy_inodecache();
2622 nfs_destroy_nfspagecache();
2623 unregister_pernet_subsys(&nfs_net_ops);
2624 unregister_nfs_fs();
2630 /* Not quite true; I just maintain it */
2632 MODULE_DESCRIPTION("NFS client support");
2633 MODULE_LICENSE("GPL");
2634 module_param(enable_ino64, bool, 0644);
2636 module_init(init_nfs_fs)
2637 module_exit(exit_nfs_fs)