Merge tag 'lsm-pr-20240312' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm
[sfrench/cifs-2.6.git] / fs / file_table.c
index 1a0962981b48abc4b03de4e825c26da5022d1126..4f03beed47379d7bb60ca18231e8742c8182b391 100644 (file)
@@ -275,21 +275,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;
@@ -308,22 +302,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);
@@ -333,6 +356,30 @@ struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
 }
 EXPORT_SYMBOL(alloc_file_pseudo);
 
+struct file *alloc_file_pseudo_noaccount(struct inode *inode,
+                                        struct vfsmount *mnt, const char *name,
+                                        int flags,
+                                        const struct file_operations *fops)
+{
+       int ret;
+       struct path path;
+       struct file *file;
+
+       ret = alloc_path_pseudo(name, inode, mnt, &path);
+       if (ret)
+               return ERR_PTR(ret);
+
+       file = alloc_empty_file_noaccount(flags, current_cred());
+       if (IS_ERR(file)) {
+               ihold(inode);
+               path_put(&path);
+               return file;
+       }
+       file_init_path(file, &path, fops);
+       return file;
+}
+EXPORT_SYMBOL_GPL(alloc_file_pseudo_noaccount);
+
 struct file *alloc_file_clone(struct file *base, int flags,
                                const struct file_operations *fops)
 {