new helper: alloc_file_clone()
authorAl Viro <viro@zeniv.linux.org.uk>
Sun, 17 Jun 2018 18:15:10 +0000 (14:15 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 12 Jul 2018 14:04:28 +0000 (10:04 -0400)
alloc_file_clone(old_file, mode, ops): create a new struct file with
->f_path equal to that of old_file.  pipe converted.

Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/file_table.c
fs/pipe.c
include/linux/file.h

index 6b3723909342fc9eb95138390e78dc0f0ef2255f..78b067ddb3868d303cd52010ebeb70eb31857b83 100644 (file)
@@ -211,6 +211,17 @@ struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
 }
 EXPORT_SYMBOL(alloc_file_pseudo);
 
+struct file *alloc_file_clone(struct file *base, int flags,
+                               const struct file_operations *fops)
+{
+       struct file *f = alloc_file(&base->f_path, flags, fops);
+       if (!IS_ERR(f)) {
+               path_get(&f->f_path);
+               f->f_mapping = base->f_mapping;
+       }
+       return f;
+}
+
 /* the real guts of fput() - releasing the last reference to file
  */
 static void __fput(struct file *file)
index 9701bd3458d146ecfaf9cf343f09729e3dead0ec..5f50070774bcead11930e19f87bf574d0ab42131 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -761,15 +761,13 @@ int create_pipe_files(struct file **res, int flags)
 
        f->private_data = inode->i_pipe;
 
-       res[0] = alloc_file(&f->f_path, O_RDONLY | (flags & O_NONBLOCK),
-                       &pipefifo_fops);
+       res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
+                                 &pipefifo_fops);
        if (IS_ERR(res[0])) {
                put_pipe_info(inode, inode->i_pipe);
                fput(f);
                return PTR_ERR(res[0]);
        }
-
-       path_get(&f->f_path);
        res[0]->private_data = inode->i_pipe;
        res[1] = f;
        return 0;
index 5b25388f2f79820334f389618f7d7d08b15b4986..60914843c737992a04a0caa844d1428a4318ab73 100644 (file)
@@ -23,6 +23,8 @@ extern struct file *alloc_file(const struct path *, int flags,
        const struct file_operations *fop);
 extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
        const char *, int flags, const struct file_operations *);
+extern struct file *alloc_file_clone(struct file *, int flags,
+       const struct file_operations *);
 
 static inline void fput_light(struct file *file, int fput_needed)
 {