]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
NFS: Label the dentry with a verifier in nfs_rmdir() and nfs_unlink()
authorTrond Myklebust <trond.myklebust@hammerspace.com>
Thu, 8 Jul 2021 01:43:09 +0000 (21:43 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 19 Jan 2026 12:09:36 +0000 (13:09 +0100)
[ Upstream commit 9019fb391de02cbff422090768b73afe9f6174df ]

After the success of an operation such as rmdir() or unlink(), we expect
to add the dentry back to the dcache as an ordinary negative dentry.
However in NFS, unless it is labelled with the appropriate verifier for
the parent directory state, then nfs_lookup_revalidate will end up
discarding that dentry and forcing a new lookup.

The fix is to ensure that we relabel the dentry appropriately on
success.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
Stable-dep-of: bd4928ec799b ("NFS: Avoid changing nlink when file removes and attribute updates race")
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/nfs/dir.c

index 32c3d0c454b19119d8915ec2727adb24954e487f..9dceb6cb10417cd891f0df6ac30e8925c93a4bcd 100644 (file)
@@ -2212,6 +2212,18 @@ static void nfs_dentry_handle_enoent(struct dentry *dentry)
                d_delete(dentry);
 }
 
+static void nfs_dentry_remove_handle_error(struct inode *dir,
+                                          struct dentry *dentry, int error)
+{
+       switch (error) {
+       case -ENOENT:
+               d_delete(dentry);
+               fallthrough;
+       case 0:
+               nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
+       }
+}
+
 int nfs_rmdir(struct inode *dir, struct dentry *dentry)
 {
        int error;
@@ -2234,6 +2246,7 @@ int nfs_rmdir(struct inode *dir, struct dentry *dentry)
                up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
        } else
                error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
+       nfs_dentry_remove_handle_error(dir, dentry, error);
        trace_nfs_rmdir_exit(dir, dentry, error);
 
        return error;
@@ -2303,9 +2316,8 @@ int nfs_unlink(struct inode *dir, struct dentry *dentry)
        }
        spin_unlock(&dentry->d_lock);
        error = nfs_safe_remove(dentry);
-       if (!error || error == -ENOENT) {
-               nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
-       } else if (need_rehash)
+       nfs_dentry_remove_handle_error(dir, dentry, error);
+       if (need_rehash)
                d_rehash(dentry);
 out:
        trace_nfs_unlink_exit(dir, dentry, error);