From: NeilBrown <neil@brown.name>
To: Linus Torvalds <torvalds@linux-foundation.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 7/7] VFS: introduce dentry_lock_in()
Date: Mon, 21 Jul 2025 18:00:03 +1000 [thread overview]
Message-ID: <20250721084412.370258-8-neil@brown.name> (raw)
In-Reply-To: <20250721084412.370258-1-neil@brown.name>
A few callers operate on a dentry which they already have - unlike the
normal case where a lookup proceeds an operation.
For these callers dentry_lock_in() is provided where other callers would
use dentry_lookup(). The call will fail if, after the lock was
gained, the child is no longer a child of the given parent.
When the operation completes done_dentry_lookup() must be called. An
extra reference is taken when the dentry_lock_in() call succeeds
and will be dropped by done_dentry_lookup().
This will be used in smb/server, ecryptfs, and overlayfs, each of which
have their own lock_parent() or parent_lock() or similar; and a few
other places which lock the parent but don't check if the parent is
still correct (often because rename isn't supported so parent cannot be
incorrect).
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 26 ++++++++++++++++++++++++++
include/linux/namei.h | 1 +
2 files changed, 27 insertions(+)
diff --git a/fs/namei.c b/fs/namei.c
index ae8079916ac6..ed656a1e458c 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1836,6 +1836,32 @@ struct dentry *dentry_lookup_killable(struct mnt_idmap *idmap,
}
EXPORT_SYMBOL(dentry_lookup_killable);
+/**
+ * dentry_lock_in: lock a dentry in given parent prior to dir ops
+ * @child: the dentry to lock
+ * @parent: the dentry of the assumed parent
+ *
+ * The child is locked - currently by taking i_rwsem on the parent - to
+ * prepare for create/remove operations. If the given parent is no longer
+ * the parent of the dentry after the lock is gained, the lock is released
+ * and the call fails (returns %false).
+ *
+ * A reference is taken to the child on success. The lock and reference
+ * must both be dropped by done_dentry_lookup() after the operation completes.
+ */
+bool dentry_lock_in(struct dentry *child, struct dentry *parent)
+{
+ inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
+ if (child->d_parent == parent) {
+ /* get the child to balance with done_dentry_lookup() which puts it. */
+ dget(child);
+ return true;
+ }
+ inode_unlock(d_inode(parent));
+ return false;
+}
+EXPORT_SYMBOL(dentry_lock_in);
+
/**
* done_dentry_lookup - finish a lookup used for create/delete
* @dentry: the target dentry
diff --git a/include/linux/namei.h b/include/linux/namei.h
index c86d9683563c..61ab251237e4 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -104,6 +104,7 @@ int rename_lookup(struct renamedata *rd, int lookup_flags);
int rename_lookup_noperm(struct renamedata *rd, int lookup_flags);
int rename_lookup_hashed(struct renamedata *rd, int lookup_flags);
void done_rename_lookup(struct renamedata *rd);
+bool dentry_lock_in(struct dentry *child, struct dentry *parent);
/**
* mode_strip_umask - handle vfs umask stripping
--
2.49.0
next prev parent reply other threads:[~2025-07-21 8:45 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-21 7:59 [PATCH 0/7 RFC] New APIs for name lookup and lock for directory operations NeilBrown
2025-07-21 7:59 ` [PATCH 1/7] VFS: unify old_mnt_idmap and new_mnt_idmap in renamedata NeilBrown
2025-07-21 13:06 ` Jeff Layton
2025-07-21 7:59 ` [PATCH 2/7] VFS: introduce done_dentry_lookup() NeilBrown
2025-07-21 13:39 ` Jeff Layton
2025-07-21 23:04 ` NeilBrown
2025-07-21 7:59 ` [PATCH 3/7] VFS: Change vfs_mkdir() to unlock on failure NeilBrown
2025-07-21 14:46 ` Jeff Layton
2025-07-21 23:08 ` NeilBrown
2025-07-21 8:00 ` [PATCH 4/7] VFS: introduce dentry_lookup() and friends NeilBrown
2025-07-21 10:20 ` Amir Goldstein
2025-07-21 23:27 ` NeilBrown
2025-07-23 15:17 ` Amir Goldstein
2025-07-21 8:00 ` [PATCH 5/7] VFS: add dentry_lookup_killable() NeilBrown
2025-07-21 8:00 ` [PATCH 6/7] VFS: add rename_lookup() NeilBrown
2025-07-21 8:00 ` NeilBrown [this message]
2025-07-21 11:32 ` [PATCH 0/7 RFC] New APIs for name lookup and lock for directory operations Amir Goldstein
2025-07-21 23:48 ` 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=20250721084412.370258-8-neil@brown.name \
--to=neil@brown.name \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.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;
as well as URLs for NNTP newsgroup(s).