]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
new helper: simple_start_creating()
authorAl Viro <viro@zeniv.linux.org.uk>
Sun, 23 Mar 2025 01:06:11 +0000 (21:06 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 3 Jul 2025 02:44:38 +0000 (22:44 -0400)
Set the things up for kernel-initiated creation of object in
a tree-in-dcache filesystem.  With respect to locking it's
an equivalent of filename_create() - we either get a negative
dentry with locked parent, or ERR_PTR() and no locks taken.

tracefs and debugfs had that open-coded as part of their
object creation machinery; switched to calling new helper.

Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/debugfs/inode.c
fs/libfs.c
fs/tracefs/inode.c
include/linux/fs.h

index 30c4944e18622dcccee5c4dbc845ddcaecae5012..52befd94aceeb5b10add88fb0dafae3dc93532c4 100644 (file)
@@ -384,27 +384,12 @@ static struct dentry *start_creating(const char *name, struct dentry *parent)
        if (!parent)
                parent = debugfs_mount->mnt_root;
 
-       inode_lock(d_inode(parent));
-       if (unlikely(IS_DEADDIR(d_inode(parent))))
-               dentry = ERR_PTR(-ENOENT);
-       else
-               dentry = lookup_noperm(&QSTR(name), parent);
-       if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
-               if (d_is_dir(dentry))
-                       pr_err("Directory '%s' with parent '%s' already present!\n",
-                              name, parent->d_name.name);
-               else
-                       pr_err("File '%s' in directory '%s' already present!\n",
-                              name, parent->d_name.name);
-               dput(dentry);
-               dentry = ERR_PTR(-EEXIST);
-       }
-
+       dentry = simple_start_creating(parent, name);
        if (IS_ERR(dentry)) {
-               inode_unlock(d_inode(parent));
+               if (dentry == ERR_PTR(-EEXIST))
+                       pr_err("'%s' already exists in '%pd'\n", name, parent);
                simple_release_fs(&debugfs_mount, &debugfs_mount_count);
        }
-
        return dentry;
 }
 
index 20b05a6db7701b70048a45377d8f1924a7c4da6a..dbbfa3703937a9e1dd0d6fdf20e53925015d88ee 100644 (file)
@@ -2260,3 +2260,28 @@ void stashed_dentry_prune(struct dentry *dentry)
         */
        cmpxchg(stashed, dentry, NULL);
 }
+
+/* parent must be held exclusive */
+struct dentry *simple_start_creating(struct dentry *parent, const char *name)
+{
+       struct dentry *dentry;
+       struct inode *dir = d_inode(parent);
+
+       inode_lock(dir);
+       if (unlikely(IS_DEADDIR(dir))) {
+               inode_unlock(dir);
+               return ERR_PTR(-ENOENT);
+       }
+       dentry = lookup_noperm(&QSTR(name), parent);
+       if (IS_ERR(dentry)) {
+               inode_unlock(dir);
+               return dentry;
+       }
+       if (dentry->d_inode) {
+               dput(dentry);
+               inode_unlock(dir);
+               return ERR_PTR(-EEXIST);
+       }
+       return dentry;
+}
+EXPORT_SYMBOL(simple_start_creating);
index a3fd3cc591bd8884de4c05e6cd569331dc69df5b..4e5d091e926374e6bed4899d73bb2785bdef94fc 100644 (file)
@@ -551,20 +551,9 @@ struct dentry *tracefs_start_creating(const char *name, struct dentry *parent)
        if (!parent)
                parent = tracefs_mount->mnt_root;
 
-       inode_lock(d_inode(parent));
-       if (unlikely(IS_DEADDIR(d_inode(parent))))
-               dentry = ERR_PTR(-ENOENT);
-       else
-               dentry = lookup_noperm(&QSTR(name), parent);
-       if (!IS_ERR(dentry) && d_inode(dentry)) {
-               dput(dentry);
-               dentry = ERR_PTR(-EEXIST);
-       }
-
-       if (IS_ERR(dentry)) {
-               inode_unlock(d_inode(parent));
+       dentry = simple_start_creating(parent, name);
+       if (IS_ERR(dentry))
                simple_release_fs(&tracefs_mount, &tracefs_mount_count);
-       }
 
        return dentry;
 }
index 96c7925a655199ebdfefafa587ea5ae3e3665867..9f75f8836bbd8238b3307fc00678d4d2e646bbb9 100644 (file)
@@ -3619,6 +3619,7 @@ extern int simple_fill_super(struct super_block *, unsigned long,
                             const struct tree_descr *);
 extern int simple_pin_fs(struct file_system_type *, struct vfsmount **mount, int *count);
 extern void simple_release_fs(struct vfsmount **mount, int *count);
+struct dentry *simple_start_creating(struct dentry *, const char *);
 
 extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
                        loff_t *ppos, const void *from, size_t available);