]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
VFS: introduce end_creating_keep()
authorNeilBrown <neil@brown.name>
Thu, 13 Nov 2025 00:18:38 +0000 (11:18 +1100)
committerChristian Brauner <brauner@kernel.org>
Fri, 14 Nov 2025 12:15:58 +0000 (13:15 +0100)
Occasionally the caller of end_creating() wants to keep using the dentry.
Rather then requiring them to dget() the dentry (when not an error)
before calling end_creating(), provide end_creating_keep() which does
this.

cachefiles and overlayfs make use of this.

Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: NeilBrown <neil@brown.name>
Link: https://patch.msgid.link/20251113002050.676694-16-neilb@ownmail.net
Tested-by: syzbot@syzkaller.appspotmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/cachefiles/namei.c
fs/overlayfs/dir.c
fs/overlayfs/super.c
include/linux/namei.h

index 59327618ac429e657f4c6eb78ce7383272305ce2..ef22ac19545b98f6e7778879c79e2ed75680ee64 100644 (file)
@@ -155,8 +155,7 @@ retry:
 
        /* Tell rmdir() it's not allowed to delete the subdir */
        inode_lock(d_inode(subdir));
-       dget(subdir);
-       end_creating(subdir);
+       end_creating_keep(subdir);
 
        if (!__cachefiles_mark_inode_in_use(NULL, d_inode(subdir))) {
                pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n",
index e097ef4e79d2c0b3e79e66ca8a933e4a77269031..10e2b7e41a7aa5e326864c133e95a200cb449f27 100644 (file)
@@ -251,10 +251,7 @@ struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
        if (IS_ERR(ret))
                return ret;
        ret = ovl_create_real(ofs, workdir, ret, attr);
-       if (!IS_ERR(ret))
-               dget(ret);
-       end_creating(ret);
-       return ret;
+       return end_creating_keep(ret);
 }
 
 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
@@ -364,8 +361,7 @@ static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
        if (IS_ERR(newdentry))
                return PTR_ERR(newdentry);
 
-       dget(newdentry);
-       end_creating(newdentry);
+       end_creating_keep(newdentry);
 
        if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
            !ovl_allow_offline_changes(ofs)) {
index 3acda985c8a31f462cf06fcacf31dd78e61281a9..7b8fc1cab6eb856088739f8a416e413619c47972 100644 (file)
@@ -319,8 +319,7 @@ retry:
                };
 
                if (work->d_inode) {
-                       dget(work);
-                       end_creating(work);
+                       end_creating_keep(work);
                        if (persist)
                                return work;
                        err = -EEXIST;
@@ -336,9 +335,7 @@ retry:
                }
 
                work = ovl_do_mkdir(ofs, dir, work, attr.ia_mode);
-               if (!IS_ERR(work))
-                       dget(work);
-               end_creating(work);
+               end_creating_keep(work);
                err = PTR_ERR(work);
                if (IS_ERR(work))
                        goto out_err;
@@ -630,9 +627,7 @@ static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
                if (!child->d_inode)
                        child = ovl_create_real(ofs, parent, child,
                                                OVL_CATTR(mode));
-               if (!IS_ERR(child))
-                       dget(child);
-               end_creating(child);
+               end_creating_keep(child);
        }
        dput(parent);
 
index 0ef73d739a310d41183d634d43d68e9b9bf3d8db..3d82c6a1919737e53de5c96b41ab8e2ca28c854c 100644 (file)
@@ -125,6 +125,28 @@ static inline void end_creating(struct dentry *child)
        end_dirop(child);
 }
 
+/* end_creating_keep - finish action started with start_creating() and return result
+ * @child: dentry returned by start_creating() or vfs_mkdir()
+ *
+ * Unlock and return the child. This can be called after
+ * start_creating() whether that function succeeded or not,
+ * but it is not needed on failure.
+ *
+ * If vfs_mkdir() was called then the value returned from that function
+ * should be given for @child rather than the original dentry, as vfs_mkdir()
+ * may have provided a new dentry.
+ *
+ * Returns: @child, which may be a dentry or an error.
+ *
+ */
+static inline struct dentry *end_creating_keep(struct dentry *child)
+{
+       if (!IS_ERR(child))
+               dget(child);
+       end_dirop(child);
+       return child;
+}
+
 /**
  * end_removing - finish action started with start_removing
  * @child:  dentry returned by start_removing()