splice: introduce FMODE_SPLICE_READ and FMODE_SPLICE_WRITE
authorJohannes Thumshirn <jthumshirn@suse.de>
Thu, 8 Dec 2016 13:04:57 +0000 (14:04 +0100)
committerStricted <info@stricted.net>
Thu, 11 Oct 2018 16:03:58 +0000 (18:03 +0200)
Introduce FMODE_SPLICE_READ and FMODE_SPLICE_WRITE. These modes check
whether it is legal to read or write a file using splice. Both get
automatically set on regular files and are not checked when a 'struct
fileoperations' includes the splice_{read,write} methods.

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
fs/open.c
fs/splice.c
include/linux/fs.h

index 142b177d037977d632264fb2d2ec67fc49d345b7..6f554f40214f34275fdd792610685c986a4616ca 100644 (file)
--- a/fs/open.c
+++ b/fs/open.c
@@ -694,6 +694,10 @@ static int do_dentry_open(struct file *f,
                return 0;
        }
 
+       if (S_ISREG(inode->i_mode))
+               f->f_mode |= FMODE_SPLICE_WRITE | FMODE_SPLICE_READ;
+
+
        f->f_op = fops_get(inode->i_fop);
 
        error = security_file_open(f, cred);
index 4b5a5fac33832f0463320af1287b495f3857d0a3..d20e9abaee5a59368f7819892db6cbd3e1ad19e4 100644 (file)
@@ -628,6 +628,10 @@ ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
                .spd_release = spd_release_page,
        };
 
+       if (unlikely(!(in->f_mode & FMODE_SPLICE_READ)))
+               return -EINVAL;
+
+
        if (splice_grow_spd(pipe, &spd))
                return -ENOMEM;
 
@@ -1080,6 +1084,9 @@ static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
 {
        ssize_t ret;
 
+       if (unlikely(!(out->f_mode & FMODE_SPLICE_WRITE)))
+               return -EINVAL;
+
        ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
        if (ret > 0)
                *ppos += ret;
index 5226c3d793e13784071d1401c4160e39c5db6737..4acc1829c7626b5a2754b0b5a8bd3e5332639cb7 100644 (file)
@@ -132,6 +132,11 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
 /* File was opened by fanotify and shouldn't generate fanotify events */
 #define FMODE_NONOTIFY         ((__force fmode_t)0x1000000)
 
+/* File can be read using splice */
+#define FMODE_SPLICE_READ       ((__force fmode_t)0x8000000)
+/* File can be written using splice */
+#define FMODE_SPLICE_WRITE      ((__force fmode_t)0x10000000)
+
 /*
  * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector
  * that indicates that they should check the contents of the iovec are