From: Jonathan Flynn <jonathan.flynn@hammerspace.com>
To: NeilBrown <neil@brown.name>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
Trond Myklebust <trondmy@kernel.org>,
Anna Schumaker <anna@kernel.org>
Cc: Jeff Layton <jlayton@kernel.org>, Chuck Lever <cel@kernel.org>,
Jan Kara <jack@suse.cz>, Jonathan Corbet <corbet@lwn.net>,
linux-doc@vger.kernel.org, linux-nfs@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH RFC 07/12] nfs: add a second mark for a removal made with the parent locked shared
Date: Thu, 29 Oct 2026 12:09:52 -0600 [thread overview]
Message-ID: <20261029180957.387138-8-jonathan.flynn@hammerspace.com> (raw)
In-Reply-To: <20261029180957.387138-1-jonathan.flynn@hammerspace.com>
Prepare the client to remove a file while its directory is locked
shared. While a REMOVE of a cached name, or a RENAME onto it, is in
flight, the client stores NFS_FSDATA_BLOCKED in the dentry's
->d_fsdata, and ->d_revalidate() waits for it to clear. Add a second
mark, NFS_FSDATA_BLOCKED_SHARED, for a removal made with the directory
locked shared. ->d_revalidate() waits on either, and nfs_call_unlink()
does not hand a pending silly-delete to a dentry carrying either. The
WARN_ON()s in nfs_unlink() and nfs_rename() still test for
NFS_FSDATA_BLOCKED alone: they run with the directory locked exclusive,
where the new mark cannot be present. Document every value ->d_fsdata
can hold.
Nothing stores the new mark yet, so nothing changes. The lookup check
is shared with NFSv2 and NFSv3, whose behavior is unchanged.
Signed-off-by: Jonathan Flynn <jonathan.flynn@hammerspace.com>
---
fs/nfs/dir.c | 11 +++++---
fs/nfs/internal.h | 58 ++++++++++++++++++++++++++++++++++++++++++
fs/nfs/unlink.c | 16 ++++++------
include/linux/nfs_fs.h | 21 +++++++++++++++
4 files changed, 94 insertions(+), 12 deletions(-)
diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index dc7784b666..68a1e578e1 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -1839,17 +1839,20 @@ static int
__nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
{
if (flags & LOOKUP_RCU) {
- if (dentry->d_fsdata == NFS_FSDATA_BLOCKED)
+ /* One load: no lock is held and there are two values */
+ if (nfs_fsdata_blocked(READ_ONCE(dentry->d_fsdata)))
return -ECHILD;
} else {
+ void **fsdata = &dentry->d_fsdata;
+
/* Wait for unlink to complete - see unblock_revalidate().
* smp_load_acquire() ensures ->d_fsdata is read after
* ->d_seq was read, matching block_revalidate() which
* sets ->d_fsdata before ->d_seq.
*/
- wait_var_event(&dentry->d_fsdata,
- smp_load_acquire(&dentry->d_fsdata)
- != NFS_FSDATA_BLOCKED);
+ wait_var_event(fsdata,
+ /* one load: there are two values to test */
+ !nfs_fsdata_blocked(smp_load_acquire(fsdata)));
}
return 0;
}
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index 34b8aff4e0..7cda8b7b3f 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -44,6 +44,64 @@ static inline bool nfs_lookup_is_soft_revalidate(const struct dentry *dentry)
return true;
}
+/*
+ * ->d_fsdata holds NFS_FSDATA_BLOCKED or NFS_FSDATA_BLOCKED_SHARED while a
+ * removal of the dentry's name is in flight: the first when the remover
+ * holds the parent's i_rwsem exclusive, the second when it holds it shared,
+ * and the second until the VFS ends the removal, after any delete event for
+ * it (see the client's ->unlink_shared_done()).
+ * ->d_revalidate() waits for either to clear. The two differ in what the
+ * dentry is to the removal.
+ *
+ * Under an exclusive parent no resolver of the name can make a new dentry
+ * for it, so a victim that gets unhashed is harmless. (That holds while
+ * the VFS calls d_alloc_parallel() under the parent's i_rwsem, as it does
+ * today; a VFS that allocates in-lookup dentries before taking i_rwsem
+ * would end it. nfs_call_unlink() calls it without i_rwsem, for a
+ * silly-renamed name, but the dentry it makes there is never hashed, and
+ * goes once its REMOVE is done.) Under a shared parent the hashed dentry
+ * is what every resolver of the name is handed - d_alloc_parallel() looks
+ * in the hash before it creates in-lookup state - and so it is what routes
+ * a lookup or open of the name through ->d_revalidate(), where a resolver
+ * that consults it after the sentinel is stored waits. Unhash it, and a
+ * lookup or open of the name allocates a fresh dentry, finds no removal in
+ * flight and can reach the server ahead of the REMOVE: an open then gets
+ * the file being removed, which the REMOVE goes on to delete while this
+ * client holds it open, without the silly-rename that would otherwise have
+ * kept it. Such a dentry is the removal's anchor. A path in the client
+ * that unhashes a dentry it does not own, or that reaches one without
+ * ->d_revalidate(), has to leave an anchor hashed or wait for its sentinel
+ * to clear.
+ *
+ * The shared sentinel is stored and cleared within one shared hold of the
+ * parent, so it is never present while the parent is held exclusive. That
+ * is why the two tests made with the parent exclusive, the WARN_ON()s in
+ * nfs_unlink() and nfs_rename(), still compare against NFS_FSDATA_BLOCKED
+ * alone. They are the only bare compares against a sentinel; every other
+ * test uses the helpers below.
+ */
+static inline bool nfs_fsdata_blocked(const void *fsdata)
+{
+ return fsdata == NFS_FSDATA_BLOCKED ||
+ fsdata == NFS_FSDATA_BLOCKED_SHARED;
+}
+
+static inline bool nfs_fsdata_blocked_shared(const void *fsdata)
+{
+ return fsdata == NFS_FSDATA_BLOCKED_SHARED;
+}
+
+/*
+ * Is a removal of this name in flight? The caller holds ->d_lock, which
+ * the sentinels are stored under but not cleared under:
+ * unblock_revalidate() clears them without it, hence READ_ONCE().
+ */
+static inline bool nfs_dentry_is_blocked(struct dentry *dentry)
+{
+ lockdep_assert_held(&dentry->d_lock);
+ return nfs_fsdata_blocked(READ_ONCE(dentry->d_fsdata));
+}
+
static inline fmode_t flags_to_mode(int flags)
{
fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
diff --git a/fs/nfs/unlink.c b/fs/nfs/unlink.c
index 679ef22637..a2845198d6 100644
--- a/fs/nfs/unlink.c
+++ b/fs/nfs/unlink.c
@@ -152,13 +152,13 @@ static int nfs_call_unlink(struct dentry *dentry, struct inode *inode, struct nf
*/
spin_lock(&alias->d_lock);
/*
- * Not if a removal of this name already owns the alias:
- * ->d_fsdata == NFS_FSDATA_BLOCKED means nfs_unlink() has a
- * REMOVE of exactly this name, or nfs_rename() a RENAME onto
- * it, in flight and ->d_revalidate() is waiting on that field,
- * so storing over it would lift the block early and the later
- * unblock_revalidate() would store NULL over @data. Tested
- * under the same ->d_lock block_revalidate() stores under.
+ * Not if a removal of this name already owns the alias: a
+ * sentinel in ->d_fsdata means that a REMOVE of exactly this
+ * name, or a RENAME onto it, is in flight, whichever way its
+ * parent is locked, and ->d_revalidate() is waiting on that
+ * field, so storing over it would lift the block early and
+ * the later unblock_revalidate() would store NULL over @data.
+ * Tested under the same ->d_lock the sentinel is stored under.
* Skip rather than wait: that removal makes this sillydelete
* redundant if it succeeds, and a wait here would sleep in a
* dput() path -- the nfsiod callback that releases a
@@ -170,7 +170,7 @@ static int nfs_call_unlink(struct dentry *dentry, struct inode *inode, struct nf
if (d_really_is_positive(alias) &&
!nfs_compare_fh(NFS_FH(inode), NFS_FH(d_inode(alias))) &&
!(alias->d_flags & DCACHE_NFSFS_RENAMED) &&
- alias->d_fsdata != NFS_FSDATA_BLOCKED) {
+ !nfs_dentry_is_blocked(alias)) {
devname_garbage = alias->d_fsdata;
alias->d_fsdata = data;
alias->d_flags |= DCACHE_NFSFS_RENAMED;
diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
index d2c716322c..b45952727c 100644
--- a/include/linux/nfs_fs.h
+++ b/include/linux/nfs_fs.h
@@ -678,8 +678,29 @@ static inline bool nfs_ooo_test(struct nfs_inode *nfsi)
* but deleted on the server resulting in ESTALE.
* So use ->d_fsdata to record when the unlink is happening
* and block dentry revalidation while it is set.
+ *
+ * ->d_fsdata of an NFS dentry holds one of:
+ * - NULL;
+ * - a mount's device name, a string kept on the mount's root dentry.
+ * Whatever stores over it frees it, and ->d_release() frees it
+ * otherwise;
+ * - a pending silly-delete, a struct nfs_unlinkdata, exactly while
+ * DCACHE_NFSFS_RENAMED is set, which is what tells it apart;
+ * - NFS_FSDATA_BLOCKED, while a REMOVE of the name, or a RENAME onto it,
+ * is in flight with the parent's i_rwsem held exclusive. It outlives
+ * that hold only when a rename is killed, until its RENAME completes;
+ * - NFS_FSDATA_BLOCKED_SHARED, from the store in the client's
+ * ->unlink_shared(), with the parent's i_rwsem held shared, until its
+ * ->unlink_shared_done(), which the VFS calls after any delete event
+ * for the removal, whether or not it succeeded, and before it drops
+ * that hold. The dentry may be negative, or positive and unhashed,
+ * by then.
+ * Both sentinels are stored under ->d_lock and cleared by
+ * unblock_revalidate(), and ->d_revalidate() waits while either is set.
+ * fs/nfs/internal.h says what else has to respect them.
*/
#define NFS_FSDATA_BLOCKED ((void*)1)
+#define NFS_FSDATA_BLOCKED_SHARED ((void *)2)
# undef ifdebug
# ifdef NFS_DEBUG
--
2.43.0
next prev parent reply other threads:[~2026-10-29 18:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261028041023.3738553-1-neilb@ownmail.net>
2026-10-29 18:09 ` [PATCH RFC 00/12] vfs, nfs: concurrent create and unlink in one directory Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 01/12] nfs: update a server's capabilities in one step Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 02/12] vfs: allow the parent lock to be shared for O_CREAT Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 03/12] nfs: tell the VFS when two names can be one entry on the server Jonathan Flynn
2026-10-29 22:23 ` NeilBrown
2026-10-29 18:09 ` [PATCH RFC 04/12] nfs: concurrent O_CREAT in one NFSv4 directory Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 05/12] vfs: factor file removal into a helper, no functional change Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 06/12] vfs: let a filesystem remove files with the parent directory locked shared Jonathan Flynn
2026-10-29 18:09 ` Jonathan Flynn [this message]
2026-10-29 18:09 ` [PATCH RFC 08/12] nfs: keep the dentry of a shared-parent removal hashed Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 09/12] nfs: do not cache a name from a listing that a removal overlapped Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 10/12] nfs: add the removal method for a parent directory locked shared Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 11/12] nfs: look a name up again once its removal has unhashed it Jonathan Flynn
2026-10-29 18:09 ` [PATCH RFC 12/12] nfs: remove files with the parent directory locked shared on NFSv4 Jonathan Flynn
2026-10-29 22:18 ` [PATCH RFC 00/12] vfs, nfs: concurrent create and unlink in one directory NeilBrown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029180957.387138-8-jonathan.flynn@hammerspace.com \
--to=jonathan.flynn@hammerspace.com \
--cc=anna@kernel.org \
--cc=brauner@kernel.org \
--cc=cel@kernel.org \
--cc=corbet@lwn.net \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=neil@brown.name \
--cc=trondmy@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox