[PATCH 06/11] VFS: introduce done_lookup_and_lock()

From: NeilBrown
Date: Thu Dec 19 2024 - 22:10:54 EST


Callers of kern_path_locked() and user_path_locked_at() should now call
done_lookup_and_lock() to unlock the directory and dput() the
dentry.

This will allow the locking rules to be changed in a central place.

Signed-off-by: NeilBrown <neilb@xxxxxxx>
---
drivers/base/devtmpfs.c | 7 +++----
fs/bcachefs/fs-ioctl.c | 3 +--
fs/namei.c | 10 ++++++++--
include/linux/namei.h | 1 +
kernel/audit_fsnotify.c | 3 +--
kernel/audit_watch.c | 3 +--
6 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/drivers/base/devtmpfs.c b/drivers/base/devtmpfs.c
index c9e34842139f..bb6d26338b6c 100644
--- a/drivers/base/devtmpfs.c
+++ b/drivers/base/devtmpfs.c
@@ -251,8 +251,8 @@ static int dev_rmdir(const char *name)
else
err = -EPERM;

- dput(dentry);
- inode_unlock(d_inode(parent.dentry));
+ done_lookup_and_lock(parent.dentry, dentry);
+
path_put(&parent);
return err;
}
@@ -339,8 +339,7 @@ static int handle_remove(const char *nodename, struct device *dev)
if (!err || err == -ENOENT)
deleted = 1;
}
- dput(dentry);
- inode_unlock(d_inode(parent.dentry));
+ done_lookup_and_lock(parent.dentry, dentry);

path_put(&parent);
if (deleted && strchr(nodename, '/'))
diff --git a/fs/bcachefs/fs-ioctl.c b/fs/bcachefs/fs-ioctl.c
index c5464219b23f..d51c86e24bef 100644
--- a/fs/bcachefs/fs-ioctl.c
+++ b/fs/bcachefs/fs-ioctl.c
@@ -522,8 +522,7 @@ static long bch2_ioctl_subvolume_destroy(struct bch_fs *c, struct file *filp,
d_delete(victim);
}
err:
- inode_unlock(dir);
- dput(victim);
+ done_lookup_and_lock(path.dentry, victim);
path_put(&path);
return ret;
}
diff --git a/fs/namei.c b/fs/namei.c
index 8780406cb4d7..29f86df4b9dc 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2773,6 +2773,13 @@ struct dentry *user_path_locked_at(int dfd, const char __user *name, struct path
}
EXPORT_SYMBOL(user_path_locked_at);

+void done_lookup_and_lock(struct dentry *parent, struct dentry *child)
+{
+ dput(child);
+ inode_unlock(d_inode(parent));
+}
+EXPORT_SYMBOL(done_lookup_and_lock);
+
int kern_path(const char *name, unsigned int flags, struct path *path)
{
struct filename *filename = getname_kernel(name);
@@ -4146,8 +4153,7 @@ EXPORT_SYMBOL(kern_path_create);

void done_path_create(struct path *path, struct dentry *dentry)
{
- dput(dentry);
- inode_unlock(path->dentry->d_inode);
+ done_lookup_and_lock(path->dentry, dentry);
mnt_drop_write(path->mnt);
path_put(path);
}
diff --git a/include/linux/namei.h b/include/linux/namei.h
index 15118992f745..898fc8ba37e1 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -65,6 +65,7 @@ extern struct dentry *user_path_create(int, const char __user *, struct path *,
extern void done_path_create(struct path *, struct dentry *);
extern struct dentry *kern_path_locked(const char *, struct path *);
extern struct dentry *user_path_locked_at(int , const char __user *, struct path *);
+extern void done_lookup_and_lock(struct dentry *parent, struct dentry *child);
int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
struct path *parent, struct qstr *last, int *type,
const struct path *root);
diff --git a/kernel/audit_fsnotify.c b/kernel/audit_fsnotify.c
index c565fbf66ac8..db2c03caa74d 100644
--- a/kernel/audit_fsnotify.c
+++ b/kernel/audit_fsnotify.c
@@ -86,7 +86,6 @@ struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pa
if (IS_ERR(dentry))
return ERR_CAST(dentry); /* returning an error */
inode = path.dentry->d_inode;
- inode_unlock(inode);

audit_mark = kzalloc(sizeof(*audit_mark), GFP_KERNEL);
if (unlikely(!audit_mark)) {
@@ -107,7 +106,7 @@ struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pa
audit_mark = ERR_PTR(ret);
}
out:
- dput(dentry);
+ done_lookup_and_lock(path.dentry, dentry);
path_put(&path);
return audit_mark;
}
diff --git a/kernel/audit_watch.c b/kernel/audit_watch.c
index e3130675ee6b..e1137ea9294b 100644
--- a/kernel/audit_watch.c
+++ b/kernel/audit_watch.c
@@ -354,8 +354,7 @@ static int audit_get_nd(struct audit_watch *watch, struct path *parent)
watch->dev = d->d_sb->s_dev;
watch->ino = d_backing_inode(d)->i_ino;

- inode_unlock(d_backing_inode(parent->dentry));
- dput(d);
+ done_lookup_and_lock(parent->dentry, d);
return 0;
}

--
2.47.0