[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250721084412.370258-6-neil@brown.name>
Date: Mon, 21 Jul 2025 18:00:01 +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 5/7] VFS: add dentry_lookup_killable()
btrfs/ioctl.c uses a "killable" lock on the directory when creating an
destroying subvols. overlayfs also does this.
This patch adds dentry_lookup_killable() for these users.
Possibly all dentry_lookup should be killable as there is no down-side,
but that can come in a later patch.
Signed-off-by: NeilBrown <neil@...wn.name>
---
fs/namei.c | 37 +++++++++++++++++++++++++++++++++++++
include/linux/namei.h | 3 +++
2 files changed, 40 insertions(+)
diff --git a/fs/namei.c b/fs/namei.c
index f292df61565a..46657736c7a0 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1799,6 +1799,43 @@ struct dentry *dentry_lookup(struct mnt_idmap *idmap,
}
EXPORT_SYMBOL(dentry_lookup);
+/**
+ * dentry_lookup_killable - lookup and lock a name prior to dir ops
+ * @last: the name in the given directory
+ * @base: the directory in which the name is to be found
+ * @lookup_flags: %LOOKUP_xxx flags
+ *
+ * The name is looked up and necessary locks are taken so that
+ * the name can be created or removed.
+ * The "necessary locks" are currently the inode node lock on @base.
+ * If a fatal signal arrives or is already pending the operation is aborted.
+ * The name @last is NOT expected to already have the hash calculated.
+ * Permission checks are performed to ensure %MAY_EXEC access to @base.
+ * Returns: the dentry, suitably locked, or an ERR_PTR().
+ */
+struct dentry *dentry_lookup_killable(struct mnt_idmap *idmap,
+ struct qstr *last,
+ struct dentry *base,
+ unsigned int lookup_flags)
+{
+ struct dentry *dentry;
+ int err;
+
+ err = lookup_one_common(idmap, last, base);
+ if (err < 0)
+ return ERR_PTR(err);
+
+ err = down_write_killable_nested(&base->d_inode->i_rwsem, I_MUTEX_PARENT);
+ if (err)
+ return ERR_PTR(err);
+
+ dentry = lookup_one_qstr_excl(last, base, lookup_flags);
+ if (IS_ERR(dentry))
+ inode_unlock(base->d_inode);
+ return dentry;
+}
+EXPORT_SYMBOL(dentry_lookup_killable);
+
/**
* 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 6d2cf4af5f16..8eade32623d8 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -83,6 +83,9 @@ struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
struct dentry *dentry_lookup(struct mnt_idmap *idmap,
struct qstr *last, struct dentry *base,
unsigned int lookup_flags);
+struct dentry *dentry_lookup_killable(struct mnt_idmap *idmap,
+ struct qstr *last, struct dentry *base,
+ unsigned int lookup_flags);
struct dentry *dentry_lookup_noperm(struct qstr *name, struct dentry *base,
unsigned int lookup_flags);
struct dentry *dentry_lookup_hashed(struct qstr *last, struct dentry *base,
--
2.49.0
Powered by blists - more mailing lists