Dentry revalidation fixups
Subsystems (2)
cover Dentry revalidation fixups
Trond's helped me out with my dentry revalidation woes. The first two patches fix races that may occur, and the third patch fixes a problem I've tried to solve in the past where the client can decide not to revalidate a dentry because we hold a delegation, but really it should be revalidated because we cached that dentry before the delegation. The most recent attempt and discussion of that problem was here:
That patch went to linux-next and should be dropped. Anna, it looks like you've already dropped it, but I just wanted to make a note of it.
Thanks for all the help, Trond!
Trond Myklebust (3):
NFS: Fix up directory verifier races
NFSv4: Fix races between open and dentry revalidation
NFSv4: Fix revalidation of dentries with delegations
fs/nfs/delegation.c | 6 ++ fs/nfs/dir.c | 124 +++++++++++++++++++++++++++++++++++++---- fs/nfs/inode.c | 1 + fs/nfs/nfs4file.c | 1 - fs/nfs/nfs4proc.c | 18 +++++- include/linux/nfs_fs.h | 26 ++------- 6 files changed, 143 insertions(+), 33 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/3 NFS: Fix up directory verifier races
From: Trond Myklebust <[email protected]>
In order to avoid having our dentry revalidation race with an update of the directory on the server, we need to store the verifier before the RPC calls to LOOKUP and READDIR.
--- fs/nfs/dir.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
fs/nfs/dir.c +14 -7
@@ -155,6 +155,7 @@ typedef struct {loff_t current_index;decode_dirent_t decode; +unsigned long dir_verifier;unsigned long timestamp;unsigned long gencount;unsigned int cache_entry_index;
@@ -353,6 +354,7 @@ int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, again: timestamp = jiffies; gencount =nfs_inc_attr_generation_counter (); + desc->dir_verifier =nfs_save_change_attribute (inode); error =NFS_PROTO (inode)->readdir (file_dentry (file), cred, entry->cookie, pages,NFS_SERVER (inode)->dtsize, desc->plus);if (error <0 ) {
@@ -455,13 +457,13 @@ void nfs_force_use_readdirplus(struct inode *dir) }static -void nfs_prime_dcache (struct dentry *parent,struct nfs_entry *entry) +void nfs_prime_dcache (struct dentry *parent,struct nfs_entry *entry, +unsigned long dir_verifier) {struct qstr filename =QSTR_INIT (entry->name, entry->len);DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);struct dentry *dentry;struct dentry *alias; -struct inode *dir =d_inode (parent);struct inode *inode;int status;
@@ -500,7 +502,7 @@ void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)if (nfs_same_file (dentry, entry)) {if (!entry->fh->size) goto out; -nfs_set_verifier (dentry,nfs_save_change_attribute (dir)); +nfs_set_verifier (dentry, dir_verifier); status =nfs_refresh_inode (d_inode (dentry), entry->fattr);if (!status)nfs_setsecurity (d_inode (dentry), entry->fattr, entry->label);
@@ -526,7 +528,7 @@ void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)dput (dentry); dentry = alias; } -nfs_set_verifier (dentry,nfs_save_change_attribute (dir)); +nfs_set_verifier (dentry, dir_verifier); out:dput (dentry); }
@@ -564,7 +566,8 @@ int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *en count++;if (desc->plus) -nfs_prime_dcache (file_dentry (desc->file), entry); +nfs_prime_dcache (file_dentry (desc->file), entry, + desc->dir_verifier); status =nfs_readdir_add_to_array (entry, page);if (status !=0 )
@@ -1159,6 +1162,7 @@ nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,struct nfs_fh *fhandle;struct nfs_fattr *fattr;struct nfs4_label *label; +unsigned long dir_verifier;int ret; ret = -ENOMEM ;
@@ -1168,6 +1172,7 @@ nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,if (fhandle ==NULL || fattr ==NULL ||IS_ERR (label)) goto out; + dir_verifier =nfs_save_change_attribute (dir); ret =NFS_PROTO (dir)->lookup (dir, dentry, fhandle, fattr, label);if (ret <0 ) {switch (ret) {
@@ -1188,7 +1193,7 @@ nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, goto out;nfs_setsecurity (inode, fattr, label); -nfs_set_verifier (dentry,nfs_save_change_attribute (dir)); +nfs_set_verifier (dentry, dir_verifier);/* set a readdirplus hint that we had a cache miss */ nfs_force_use_readdirplus (dir);
@@ -1415,6 +1420,7 @@ struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned instruct nfs_fh *fhandle =NULL ;struct nfs_fattr *fattr =NULL ;struct nfs4_label *label =NULL ; +unsigned long dir_verifier;int error;dfprintk (VFS ,"NFS: lookup(%pd2)\n" , dentry);
@@ -1440,6 +1446,7 @@ struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned inif (IS_ERR (label)) goto out; + dir_verifier =nfs_save_change_attribute (dir);trace_nfs_lookup_enter (dir, dentry, flags); error =NFS_PROTO (dir)->lookup (dir, dentry, fhandle, fattr, label);if (error == -ENOENT )
@@ -1463,7 +1470,7 @@ struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned in goto out_label; dentry = res; } -nfs_set_verifier (dentry,nfs_save_change_attribute (dir)); +nfs_set_verifier (dentry, dir_verifier); out_label:trace_nfs_lookup_exit (dir, dentry, flags, error);nfs4_label_free (label);
Signature · 1 line
2.20.1
PATCH 2/3 NFSv4: Fix races between open and dentry revalidation
From: Trond Myklebust <[email protected]>
We want to make sure that we revalidate the dentry if and only if we've done an OPEN by filename.
In order to avoid races with remote changes to the directory on the server, we want to save the verifier before calling OPEN. The exception is if the server returned a delegation with our OPEN, as we then know that the filename can't have changed on the server.
--- fs/nfs/nfs4file.c | 1 - fs/nfs/nfs4proc.c | 18 ++++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-)
fs/nfs/nfs4file.c +0 -1
@@ -87,7 +87,6 @@ nfs4_file_open(struct inode *inode, struct file *filp)if (inode !=d_inode (dentry)) goto out_drop; -nfs_set_verifier (dentry,nfs_save_change_attribute (dir));nfs_file_set_open_context (filp, ctx);nfs_fscache_open_file (inode, filp); err =0 ;
fs/nfs/nfs4proc.c +16 -2
@@ -2974,10 +2974,13 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,struct dentry *dentry;struct nfs4_state *state;fmode_t acc_mode =_nfs4_ctx_to_accessmode (ctx); +struct inode *dir =d_inode (opendata->dir); +unsigned long dir_verifier;unsigned int seq;int ret; seq =raw_seqcount_begin (&sp->so_reclaim_seqcount); + dir_verifier =nfs_save_change_attribute (dir); ret =_nfs4_proc_open (opendata, ctx);if (ret !=0 )
@@ -3005,8 +3008,19 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,dput (ctx->dentry); ctx->dentry = dentry = alias; } -nfs_set_verifier (dentry, -nfs_save_change_attribute (d_inode (opendata->dir))); + } + +switch (opendata->o_arg.claim) { +default : +break ; +case NFS4_OPEN_CLAIM_NULL : +case NFS4_OPEN_CLAIM_DELEGATE_CUR : +case NFS4_OPEN_CLAIM_DELEGATE_PREV : +if (!opendata->rpc_done) +break ; +if (opendata->o_res.delegation_type !=0 ) + dir_verifier =nfs_save_change_attribute (dir); +nfs_set_verifier (dentry, dir_verifier); }/* Parse layoutget results before we check for access */
Signature · 1 line
2.20.1
PATCH 3/3 NFSv4: Fix revalidation of dentries with delegations
From: Trond Myklebust <[email protected]>
If a dentry was not initially looked up while we were holding a delegation, then we do still need to revalidate that it still holds the same name. If there are multiple hard links to the same file, then all the hard links need validation.
--- fs/nfs/delegation.c | 6 +++ fs/nfs/dir.c | 103 +++++++++++++++++++++++++++++++++++++++-- fs/nfs/inode.c | 1 + include/linux/nfs_fs.h | 26 +++-------- 4 files changed, 113 insertions(+), 23 deletions(-)
fs/nfs/delegation.c +6 -0
@@ -42,6 +42,8 @@ static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)if (!test_and_set_bit (NFS_DELEGATION_REVOKED , &delegation->flags)) { delegation->stateid.type =NFS4_INVALID_STATEID_TYPE ;atomic_long_dec (&nfs_active_delegations); +if (!test_bit (NFS_DELEGATION_RETURNING , &delegation->flags)) +nfs_clear_verifier_delegated (delegation->inode); } }
@@ -276,6 +278,8 @@ nfs_start_delegation_return_locked(struct nfs_inode *nfsi)if (!test_and_set_bit (NFS_DELEGATION_RETURNING , &delegation->flags)) ret = delegation;spin_unlock (&delegation->lock); +if (ret) +nfs_clear_verifier_delegated (&nfsi->vfs_inode); out:return ret; }
@@ -689,6 +693,8 @@ void nfs4_inode_return_delegation_on_close(struct inode *inode) ret = delegation; }spin_unlock (&delegation->lock); +if (ret) +nfs_clear_verifier_delegated (inode); } out:rcu_read_unlock ();
fs/nfs/dir.c +100 -3
128 lines not on this page · raw message
fs/nfs/inode.c +1 -0
@@ -2114,6 +2114,7 @@ static void init_once(void *foo)init_rwsem (&nfsi->rmdir_sem);mutex_init (&nfsi->commit_mutex);nfs4_init_once (nfsi); + nfsi->cache_change_attribute =0 ; }static int __init nfs_init_inodecache (void )
include/linux/nfs_fs.h +6 -20
@@ -337,35 +337,17 @@ static inline int nfs_server_capable(struct inode *inode, int cap)return NFS_SERVER (inode)->caps & cap; } -static inline void nfs_set_verifier (struct dentry * dentry,unsigned long verf) -{ - dentry->d_time = verf; -} -/** * nfs_save_change_attribute - Returns the inode attribute change cookie * @dir - pointer to parent directory inode -* The "change attribute" is updated every time we finish an operation -* that will result in a metadata change on the server. +* The "cache change attribute" is updated when we need to revalidate +* our dentry cache after a directory was seen to change on the server. */ static inline unsigned long nfs_save_change_attribute (struct inode *dir) {return NFS_I (dir)->cache_change_attribute; } -/** -* nfs_verify_change_attribute - Detects NFS remote directory changes -* @dir - pointer to parent directory inode -* @chattr - previously saved change attribute -* Return "false" if the verifiers doesn't match the change attribute. -* This would usually indicate that the directory contents have changed on -* the server, and that any dentries need revalidating. -*/ -static inline int nfs_verify_change_attribute (struct inode *dir,unsigned long chattr) -{ -return chattr ==NFS_I (dir)->cache_change_attribute; -} -/* * linux/fs/nfs/inode.c */
@@ -495,6 +477,10 @@ extern const struct file_operations nfs_dir_operations;extern const struct dentry_operations nfs_dentry_operations;extern void nfs_force_lookup_revalidate (struct inode *dir); +extern void nfs_set_verifier (struct dentry * dentry,unsigned long verf); +#if IS_ENABLED (CONFIG_NFS_V4 ) +extern void nfs_clear_verifier_delegated (struct inode *inode); +#endif /* IS_ENABLED(CONFIG_NFS_V4) */ extern struct dentry *nfs_add_or_obtain (struct dentry *dentry,struct nfs_fh *fh,struct nfs_fattr *fattr,struct nfs4_label *label);
Signature · 1 line
2.20.1