16 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/time.h>
20 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/stat.h>
24 #include <linux/errno.h>
29 #include <linux/nfs_fs.h>
38 #include <linux/slab.h>
43 #include <asm/uaccess.h>
56 #define NFSDBG_FACILITY NFSDBG_VFS
58 #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
63 static void nfs_invalidate_inode(
struct inode *);
68 static inline unsigned long
69 nfs_fattr_to_ino_t(
struct nfs_fattr *fattr)
71 return nfs_fileid_to_ino_t(fattr->
fileid);
80 if (fatal_signal_pending(
current))
105 if (
sizeof(ino) <
sizeof(fileid))
106 ino ^= fileid >> (
sizeof(fileid)-
sizeof(ino)) * 8;
115 BUG_ON(nfs_have_writebacks(inode));
116 BUG_ON(!list_empty(&NFS_I(inode)->open_files));
147 static void nfs_zap_caches_locked(
struct inode *
inode)
154 nfsi->
attrtimeo = NFS_MINATTRTIMEO(inode);
166 spin_lock(&inode->
i_lock);
167 nfs_zap_caches_locked(inode);
168 spin_unlock(&inode->
i_lock);
174 spin_lock(&inode->
i_lock);
176 spin_unlock(&inode->
i_lock);
182 void (*clear_acl_cache)(
struct inode *);
184 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
185 if (clear_acl_cache !=
NULL)
186 clear_acl_cache(inode);
187 spin_lock(&inode->
i_lock);
189 spin_unlock(&inode->
i_lock);
195 spin_lock(&inode->
i_lock);
197 spin_unlock(&inode->
i_lock);
205 static void nfs_invalidate_inode(
struct inode *inode)
208 nfs_zap_caches_locked(inode);
223 nfs_find_actor(
struct inode *inode,
void *opaque)
229 if (NFS_FILEID(inode) != fattr->
fileid)
231 if (nfs_compare_fh(NFS_FH(inode), fh))
239 nfs_init_locked(
struct inode *inode,
void *opaque)
244 set_nfs_fileid(inode, fattr->
fileid);
245 nfs_copy_fh(NFS_FH(inode), desc->
fh);
260 struct inode *inode = ERR_PTR(-
ENOENT);
263 nfs_attr_check_mountpoint(sb, fattr);
266 !nfs_attr_use_mounted_on_fileid(fattr))
271 hash = nfs_fattr_to_ino_t(fattr);
273 inode =
iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
296 inode->
i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
298 inode->
i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
300 inode->
i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
302 inode->
i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
352 inode->
i_size = nfs_size_to_loff_t(fattr->
size);
376 nfsi->
attrtimeo = NFS_MINATTRTIMEO(inode);
385 dprintk(
"NFS: nfs_fhget(%s/%Ld fh_crc=0x%08x ct=%d)\n",
387 (
long long)NFS_FILEID(inode),
388 nfs_display_fhandle_hash(fh),
395 dprintk(
"nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
400 #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)
405 struct inode *inode = dentry->
d_inode;
427 nfs_inode_dio_wait(inode);
438 NFS_PROTO(inode)->return_delegation(inode);
439 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
442 nfs_free_fattr(fattr);
457 static int nfs_vmtruncate(
struct inode * inode, loff_t
offset)
466 spin_lock(&inode->
i_lock);
468 i_size_write(inode, offset);
469 spin_unlock(&inode->
i_lock);
487 spin_lock(&inode->
i_lock);
498 spin_unlock(&inode->
i_lock);
502 nfs_vmtruncate(inode, attr->
ia_size);
509 struct inode *inode = dentry->
d_inode;
515 nfs_inode_dio_wait(inode);
552 INIT_LIST_HEAD(&l_ctx->
list);
573 struct inode *inode = ctx->
dentry->d_inode;
575 spin_lock(&inode->
i_lock);
576 res = __nfs_find_lock_context(ctx);
578 spin_unlock(&inode->
i_lock);
582 nfs_init_lock_context(
new);
583 spin_lock(&inode->
i_lock);
584 res = __nfs_find_lock_context(ctx);
587 new->open_context =
ctx;
592 spin_unlock(&inode->
i_lock);
600 struct inode *inode = ctx->
dentry->d_inode;
605 spin_unlock(&inode->
i_lock);
626 inode = ctx->
dentry->d_inode;
627 if (!list_empty(&NFS_I(inode)->open_files))
629 server = NFS_SERVER(inode);
641 return ERR_CAST(cred);
649 ctx->
dentry = dget(dentry);
657 INIT_LIST_HEAD(&ctx->
list);
671 static void __put_nfs_open_context(
struct nfs_open_context *ctx,
int is_sync)
673 struct inode *inode = ctx->
dentry->d_inode;
676 if (!list_empty(&ctx->
list)) {
680 spin_unlock(&inode->
i_lock);
684 NFS_PROTO(inode)->close_context(ctx, is_sync);
698 __put_nfs_open_context(ctx, 0);
708 struct inode *inode = filp->
f_path.dentry->d_inode;
712 spin_lock(&inode->
i_lock);
714 spin_unlock(&inode->
i_lock);
726 spin_lock(&inode->
i_lock);
728 if (cred !=
NULL && pos->
cred != cred)
735 spin_unlock(&inode->
i_lock);
739 static void nfs_file_clear_open_context(
struct file *filp)
741 struct inode *inode = filp->
f_path.dentry->d_inode;
746 spin_lock(&inode->
i_lock);
747 list_move_tail(&ctx->
list, &NFS_I(inode)->open_files);
748 spin_unlock(&inode->
i_lock);
771 nfs_file_clear_open_context(filp);
786 dfprintk(PAGECACHE,
"NFS: revalidating (%s/%Ld)\n",
787 inode->
i_sb->s_id, (
long long)NFS_FILEID(inode));
791 if (NFS_STALE(inode))
800 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr);
802 dfprintk(PAGECACHE,
"nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
804 (
long long)NFS_FILEID(inode), status);
815 dfprintk(PAGECACHE,
"nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
817 (
long long)NFS_FILEID(inode), status);
824 dfprintk(PAGECACHE,
"NFS: (%s/%Ld) revalidation complete\n",
826 (
long long)NFS_FILEID(inode));
829 nfs_free_fattr(fattr);
840 static int nfs_attribute_cache_expired(
struct inode *inode)
842 if (nfs_have_delegated_attributes(inode))
857 && !nfs_attribute_cache_expired(inode))
858 return NFS_STALE(inode) ? -
ESTALE : 0;
872 spin_lock(&inode->
i_lock);
876 spin_unlock(&inode->
i_lock);
879 dfprintk(PAGECACHE,
"NFS: (%s/%Ld) data cache invalidated\n",
880 inode->
i_sb->s_id, (
long long)NFS_FILEID(inode));
884 static bool nfs_mapping_need_revalidate_inode(
struct inode *inode)
886 if (nfs_have_delegated_attributes(inode))
907 if (nfs_mapping_need_revalidate_inode(inode)) {
913 ret = nfs_invalidate_mapping(inode, mapping);
918 static unsigned long nfs_wcc_update_inode(
struct inode *inode,
struct nfs_fattr *fattr)
921 unsigned long ret = 0;
949 && i_size_read(inode) == nfs_size_to_loff_t(fattr->
pre_size)
951 i_size_write(inode, nfs_size_to_loff_t(fattr->
size));
966 static int nfs_check_inode_attributes(
struct inode *inode,
struct nfs_fattr *fattr)
969 loff_t cur_size, new_isize;
973 if (nfs_have_delegated_attributes(inode))
990 cur_size = i_size_read(inode);
991 new_isize = nfs_size_to_loff_t(fattr->
size);
992 if (cur_size != new_isize && nfsi->
npages == 0)
1018 static int nfs_ctime_need_update(
const struct inode *inode,
const struct nfs_fattr *fattr)
1022 return timespec_compare(&fattr->
ctime, &inode->
i_ctime) > 0;
1025 static int nfs_size_need_update(
const struct inode *inode,
const struct nfs_fattr *fattr)
1029 return nfs_size_to_loff_t(fattr->
size) > i_size_read(inode);
1034 static unsigned long nfs_read_attr_generation_counter(
void)
1036 return atomic_long_read(&nfs_attr_generation_counter);
1041 return atomic_long_inc_return(&nfs_attr_generation_counter);
1085 u32 _nfs_display_fhandle_hash(
const struct nfs_fh *fh)
1100 void _nfs_display_fhandle(
const struct nfs_fh *fh,
const char *caption)
1110 caption, fh, fh->
size, _nfs_display_fhandle_hash(fh));
1111 for (i = 0; i < fh->
size; i += 16) {
1114 switch ((fh->
size - i - 1) >> 2) {
1156 static int nfs_inode_attrs_need_update(
const struct inode *inode,
const struct nfs_fattr *fattr)
1158 const struct nfs_inode *nfsi = NFS_I(inode);
1161 nfs_ctime_need_update(inode, fattr) ||
1162 nfs_size_need_update(inode, fattr) ||
1166 static int nfs_refresh_inode_locked(
struct inode *inode,
struct nfs_fattr *fattr)
1168 if (nfs_inode_attrs_need_update(inode, fattr))
1169 return nfs_update_inode(inode, fattr);
1170 return nfs_check_inode_attributes(inode, fattr);
1189 spin_lock(&inode->
i_lock);
1190 status = nfs_refresh_inode_locked(inode, fattr);
1191 spin_unlock(&inode->
i_lock);
1197 static int nfs_post_op_update_inode_locked(
struct inode *inode,
struct nfs_fattr *fattr)
1206 return nfs_refresh_inode_locked(inode, fattr);
1227 spin_lock(&inode->
i_lock);
1228 status = nfs_post_op_update_inode_locked(inode, fattr);
1229 spin_unlock(&inode->
i_lock);
1249 spin_lock(&inode->
i_lock);
1252 !nfs_inode_attrs_need_update(inode, fattr)) {
1276 fattr->
pre_size = i_size_read(inode);
1280 status = nfs_post_op_update_inode_locked(inode, fattr);
1281 spin_unlock(&inode->
i_lock);
1298 static int nfs_update_inode(
struct inode *inode,
struct nfs_fattr *fattr)
1302 loff_t cur_isize, new_isize;
1303 unsigned long invalid = 0;
1305 unsigned long save_cache_validity;
1307 dfprintk(VFS,
"NFS: %s(%s/%ld fh_crc=0x%08x ct=%d info=0x%x)\n",
1308 __func__, inode->
i_sb->s_id, inode->
i_ino,
1309 nfs_display_fhandle_hash(NFS_FH(inode)),
1314 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1317 (
long long)fattr->
fileid);
1333 server = NFS_SERVER(inode);
1336 !nfs_fsid_equal(&server->
fsid, &fattr->
fsid) &&
1352 invalid |= nfs_wcc_update_inode(inode, fattr);
1357 dprintk(
"NFS: change_attr change on server for file %s/%ld\n",
1369 invalid |= save_cache_validity;
1385 new_isize = nfs_size_to_loff_t(fattr->
size);
1386 cur_isize = i_size_read(inode);
1387 if (new_isize != cur_isize) {
1391 new_isize > cur_isize) {
1392 i_size_write(inode, new_isize);
1395 dprintk(
"NFS: isize change on server for file %s/%ld "
1399 (
long long)cur_isize,
1400 (
long long)new_isize);
1472 nfsi->
attrtimeo = NFS_MINATTRTIMEO(inode);
1477 if ((nfsi->
attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1478 nfsi->
attrtimeo = NFS_MAXATTRTIMEO(inode);
1482 invalid &= ~NFS_INO_INVALID_ATTR;
1487 if (!NFS_PROTO(inode)->have_delegation(inode,
FMODE_READ) ||
1498 nfs_invalidate_inode(inode);
1510 #ifdef CONFIG_NFS_V3_ACL
1511 nfsi->acl_access = ERR_PTR(-
EAGAIN);
1512 nfsi->acl_default = ERR_PTR(-
EAGAIN);
1514 #if IS_ENABLED(CONFIG_NFS_V4)
1515 nfsi->nfs4_acl =
NULL;
1533 static inline void nfs4_init_once(
struct nfs_inode *nfsi)
1535 #if IS_ENABLED(CONFIG_NFS_V4)
1536 INIT_LIST_HEAD(&nfsi->open_states);
1537 nfsi->delegation =
NULL;
1538 nfsi->delegation_state = 0;
1540 nfsi->layout =
NULL;
1544 static void init_once(
void *
foo)
1559 nfs4_init_once(nfsi);
1562 static int __init nfs_init_inodecache(
void)
1569 if (nfs_inode_cachep ==
NULL)
1575 static void nfs_destroy_inodecache(
void)
1591 static int nfsiod_start(
void)
1594 dprintk(
"RPC: creating workqueue nfsiod\n");
1598 nfsiod_workqueue = wq;
1605 static void nfsiod_stop(
void)
1612 nfsiod_workqueue =
NULL;
1619 static int nfs_net_init(
struct net *
net)
1625 static void nfs_net_exit(
struct net *
net)
1632 .init = nfs_net_init,
1633 .exit = nfs_net_exit,
1635 .size =
sizeof(
struct nfs_net),
1641 static int __init init_nfs_fs(
void)
1657 err = nfsiod_start();
1661 err = nfs_fs_proc_init();
1669 err = nfs_init_inodecache();
1685 #ifdef CONFIG_PROC_FS
1693 #ifdef CONFIG_PROC_FS
1702 nfs_destroy_inodecache();
1719 static void __exit exit_nfs_fs(
void)
1724 nfs_destroy_inodecache();
1729 #ifdef CONFIG_PROC_FS