]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
file: prepare for new helper
authorChristian Brauner <brauner@kernel.org>
Thu, 8 Feb 2024 18:08:37 +0000 (19:08 +0100)
committerChristian Brauner <brauner@kernel.org>
Sun, 25 Feb 2024 11:04:13 +0000 (12:04 +0100)
In order to add a helper to open files that aren't accounted split
alloc_file() and parts of alloc_file_pseudo() into helpers. One to
prepare a path, another one to setup the file.

Suggested-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20240129160241.GA2793@lst.de
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/file_table.c

index b991f90571b4d3089a0c00884fea3e38f317b1da..a94adac3d907591694f64b23b02c604de35ea914 100644 (file)
@@ -276,21 +276,15 @@ struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
 }
 
 /**
- * alloc_file - allocate and initialize a 'struct file'
+ * file_init_path - initialize a 'struct file' based on path
  *
+ * @file: the file to set up
  * @path: the (dentry, vfsmount) pair for the new file
- * @flags: O_... flags with which the new file will be opened
  * @fop: the 'struct file_operations' for the new file
  */
-static struct file *alloc_file(const struct path *path, int flags,
-               const struct file_operations *fop)
+static void file_init_path(struct file *file, const struct path *path,
+                          const struct file_operations *fop)
 {
-       struct file *file;
-
-       file = alloc_empty_file(flags, current_cred());
-       if (IS_ERR(file))
-               return file;
-
        file->f_path = *path;
        file->f_inode = path->dentry->d_inode;
        file->f_mapping = path->dentry->d_inode->i_mapping;
@@ -309,22 +303,51 @@ static struct file *alloc_file(const struct path *path, int flags,
        file->f_op = fop;
        if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
                i_readcount_inc(path->dentry->d_inode);
+}
+
+/**
+ * alloc_file - allocate and initialize a 'struct file'
+ *
+ * @path: the (dentry, vfsmount) pair for the new file
+ * @flags: O_... flags with which the new file will be opened
+ * @fop: the 'struct file_operations' for the new file
+ */
+static struct file *alloc_file(const struct path *path, int flags,
+               const struct file_operations *fop)
+{
+       struct file *file;
+
+       file = alloc_empty_file(flags, current_cred());
+       if (!IS_ERR(file))
+               file_init_path(file, path, fop);
        return file;
 }
 
-struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
-                               const char *name, int flags,
-                               const struct file_operations *fops)
+static inline int alloc_path_pseudo(const char *name, struct inode *inode,
+                                   struct vfsmount *mnt, struct path *path)
 {
        struct qstr this = QSTR_INIT(name, strlen(name));
+
+       path->dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
+       if (!path->dentry)
+               return -ENOMEM;
+       path->mnt = mntget(mnt);
+       d_instantiate(path->dentry, inode);
+       return 0;
+}
+
+struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
+                              const char *name, int flags,
+                              const struct file_operations *fops)
+{
+       int ret;
        struct path path;
        struct file *file;
 
-       path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
-       if (!path.dentry)
-               return ERR_PTR(-ENOMEM);
-       path.mnt = mntget(mnt);
-       d_instantiate(path.dentry, inode);
+       ret = alloc_path_pseudo(name, inode, mnt, &path);
+       if (ret)
+               return ERR_PTR(ret);
+
        file = alloc_file(&path, flags, fops);
        if (IS_ERR(file)) {
                ihold(inode);