[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250721084412.370258-8-neil@brown.name>
Date: Mon, 21 Jul 2025 18:00:03 +1000
From: NeilBrown <neil@...wn.name>
To: Linus Torvalds <torvalds@...ux-foundation.org>,
Alexander Viro <viro@...iv.linux.org.uk>,
Christian Brauner <brauner@...nel.org>,
Jan Kara <jack@...e.cz>
Cc: linux-fsdevel@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH 7/7] VFS: introduce dentry_lock_in()
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@...wn.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
Powered by blists - more mailing lists