Merge tag 'regmap-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-microblaze.git] / fs / splice.c
index ce75aec..5dbce4d 100644 (file)
@@ -33,7 +33,6 @@
 #include <linux/security.h>
 #include <linux/gfp.h>
 #include <linux/socket.h>
-#include <linux/compat.h>
 #include <linux/sched/signal.h>
 
 #include "internal.h"
@@ -342,89 +341,6 @@ const struct pipe_buf_operations nosteal_pipe_buf_ops = {
 };
 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
 
-static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
-                           unsigned long vlen, loff_t offset)
-{
-       mm_segment_t old_fs;
-       loff_t pos = offset;
-       ssize_t res;
-
-       old_fs = get_fs();
-       set_fs(KERNEL_DS);
-       /* The cast to a user pointer is valid due to the set_fs() */
-       res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
-       set_fs(old_fs);
-
-       return res;
-}
-
-static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
-                                struct pipe_inode_info *pipe, size_t len,
-                                unsigned int flags)
-{
-       struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
-       struct iov_iter to;
-       struct page **pages;
-       unsigned int nr_pages;
-       unsigned int mask;
-       size_t offset, base, copied = 0;
-       ssize_t res;
-       int i;
-
-       if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
-               return -EAGAIN;
-
-       /*
-        * Try to keep page boundaries matching to source pagecache ones -
-        * it probably won't be much help, but...
-        */
-       offset = *ppos & ~PAGE_MASK;
-
-       iov_iter_pipe(&to, READ, pipe, len + offset);
-
-       res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
-       if (res <= 0)
-               return -ENOMEM;
-
-       nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
-
-       vec = __vec;
-       if (nr_pages > PIPE_DEF_BUFFERS) {
-               vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
-               if (unlikely(!vec)) {
-                       res = -ENOMEM;
-                       goto out;
-               }
-       }
-
-       mask = pipe->ring_size - 1;
-       pipe->bufs[to.head & mask].offset = offset;
-       pipe->bufs[to.head & mask].len -= offset;
-
-       for (i = 0; i < nr_pages; i++) {
-               size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
-               vec[i].iov_base = page_address(pages[i]) + offset;
-               vec[i].iov_len = this_len;
-               len -= this_len;
-               offset = 0;
-       }
-
-       res = kernel_readv(in, vec, nr_pages, *ppos);
-       if (res > 0) {
-               copied = res;
-               *ppos += res;
-       }
-
-       if (vec != __vec)
-               kfree(vec);
-out:
-       for (i = 0; i < nr_pages; i++)
-               put_page(pages[i]);
-       kvfree(pages);
-       iov_iter_advance(&to, copied);  /* truncates and discards */
-       return res;
-}
-
 /*
  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  * using sendpage(). Return the number of bytes sent.
@@ -746,12 +662,14 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
 
                /* build the vector */
                left = sd.total_len;
-               for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
+               for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
                        struct pipe_buffer *buf = &pipe->bufs[tail & mask];
                        size_t this_len = buf->len;
 
-                       if (this_len > left)
-                               this_len = left;
+                       /* zero-length bvecs are not supported, skip them */
+                       if (!this_len)
+                               continue;
+                       this_len = min(this_len, left);
 
                        ret = pipe_buf_confirm(pipe, buf);
                        if (unlikely(ret)) {
@@ -764,6 +682,7 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
                        array[n].bv_len = this_len;
                        array[n].bv_offset = buf->offset;
                        left -= this_len;
+                       n++;
                }
 
                iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
@@ -808,33 +727,6 @@ done:
 
 EXPORT_SYMBOL(iter_file_splice_write);
 
-static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
-                         struct splice_desc *sd)
-{
-       int ret;
-       void *data;
-       loff_t tmp = sd->pos;
-
-       data = kmap(buf->page);
-       ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
-       kunmap(buf->page);
-
-       return ret;
-}
-
-static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
-                                        struct file *out, loff_t *ppos,
-                                        size_t len, unsigned int flags)
-{
-       ssize_t ret;
-
-       ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
-       if (ret > 0)
-               *ppos += ret;
-
-       return ret;
-}
-
 /**
  * generic_splice_sendpage - splice data from a pipe to a socket
  * @pipe:      pipe to splice from
@@ -856,15 +748,23 @@ ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
 
 EXPORT_SYMBOL(generic_splice_sendpage);
 
+static int warn_unsupported(struct file *file, const char *op)
+{
+       pr_debug_ratelimited(
+               "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
+               op, file, current->pid, current->comm);
+       return -EINVAL;
+}
+
 /*
  * Attempt to initiate a splice from pipe to file.
  */
 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
                           loff_t *ppos, size_t len, unsigned int flags)
 {
-       if (out->f_op->splice_write)
-               return out->f_op->splice_write(pipe, out, ppos, len, flags);
-       return default_file_splice_write(pipe, out, ppos, len, flags);
+       if (unlikely(!out->f_op->splice_write))
+               return warn_unsupported(out, "write");
+       return out->f_op->splice_write(pipe, out, ppos, len, flags);
 }
 
 /*
@@ -874,11 +774,16 @@ static long do_splice_to(struct file *in, loff_t *ppos,
                         struct pipe_inode_info *pipe, size_t len,
                         unsigned int flags)
 {
+       unsigned int p_space;
        int ret;
 
        if (unlikely(!(in->f_mode & FMODE_READ)))
                return -EBADF;
 
+       /* Don't try to read more the pipe has space for. */
+       p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
+       len = min_t(size_t, len, p_space << PAGE_SHIFT);
+
        ret = rw_verify_area(READ, in, ppos, len);
        if (unlikely(ret < 0))
                return ret;
@@ -886,9 +791,9 @@ static long do_splice_to(struct file *in, loff_t *ppos,
        if (unlikely(len > MAX_RW_COUNT))
                len = MAX_RW_COUNT;
 
-       if (in->f_op->splice_read)
-               return in->f_op->splice_read(in, ppos, pipe, len, flags);
-       return default_file_splice_read(in, ppos, pipe, len, flags);
+       if (unlikely(!in->f_op->splice_read))
+               return warn_unsupported(in, "read");
+       return in->f_op->splice_read(in, ppos, pipe, len, flags);
 }
 
 /**
@@ -959,15 +864,10 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
        WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
 
        while (len) {
-               unsigned int p_space;
                size_t read_len;
                loff_t pos = sd->pos, prev_pos = pos;
 
-               /* Don't try to read more the pipe has space for. */
-               p_space = pipe->max_usage -
-                       pipe_occupancy(pipe->head, pipe->tail);
-               read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
-               ret = do_splice_to(in, &pos, pipe, read_len, flags);
+               ret = do_splice_to(in, &pos, pipe, len, flags);
                if (unlikely(ret <= 0))
                        goto out_release;
 
@@ -1105,12 +1005,28 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
                               struct pipe_inode_info *opipe,
                               size_t len, unsigned int flags);
 
+long splice_file_to_pipe(struct file *in,
+                        struct pipe_inode_info *opipe,
+                        loff_t *offset,
+                        size_t len, unsigned int flags)
+{
+       long ret;
+
+       pipe_lock(opipe);
+       ret = wait_for_space(opipe, flags);
+       if (!ret)
+               ret = do_splice_to(in, offset, opipe, len, flags);
+       pipe_unlock(opipe);
+       if (ret > 0)
+               wakeup_pipe_readers(opipe);
+       return ret;
+}
+
 /*
  * Determine where to splice to/from.
  */
-long do_splice(struct file *in, loff_t __user *off_in,
-               struct file *out, loff_t __user *off_out,
-               size_t len, unsigned int flags)
+long do_splice(struct file *in, loff_t *off_in, struct file *out,
+              loff_t *off_out, size_t len, unsigned int flags)
 {
        struct pipe_inode_info *ipipe;
        struct pipe_inode_info *opipe;
@@ -1144,8 +1060,7 @@ long do_splice(struct file *in, loff_t __user *off_in,
                if (off_out) {
                        if (!(out->f_mode & FMODE_PWRITE))
                                return -EINVAL;
-                       if (copy_from_user(&offset, off_out, sizeof(loff_t)))
-                               return -EFAULT;
+                       offset = *off_out;
                } else {
                        offset = out->f_pos;
                }
@@ -1166,8 +1081,8 @@ long do_splice(struct file *in, loff_t __user *off_in,
 
                if (!off_out)
                        out->f_pos = offset;
-               else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
-                       ret = -EFAULT;
+               else
+                       *off_out = offset;
 
                return ret;
        }
@@ -1178,8 +1093,7 @@ long do_splice(struct file *in, loff_t __user *off_in,
                if (off_in) {
                        if (!(in->f_mode & FMODE_PREAD))
                                return -EINVAL;
-                       if (copy_from_user(&offset, off_in, sizeof(loff_t)))
-                               return -EFAULT;
+                       offset = *off_in;
                } else {
                        offset = in->f_pos;
                }
@@ -1187,24 +1101,11 @@ long do_splice(struct file *in, loff_t __user *off_in,
                if (out->f_flags & O_NONBLOCK)
                        flags |= SPLICE_F_NONBLOCK;
 
-               pipe_lock(opipe);
-               ret = wait_for_space(opipe, flags);
-               if (!ret) {
-                       unsigned int p_space;
-
-                       /* Don't try to read more the pipe has space for. */
-                       p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail);
-                       len = min_t(size_t, len, p_space << PAGE_SHIFT);
-
-                       ret = do_splice_to(in, &offset, opipe, len, flags);
-               }
-               pipe_unlock(opipe);
-               if (ret > 0)
-                       wakeup_pipe_readers(opipe);
+               ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
                if (!off_in)
                        in->f_pos = offset;
-               else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
-                       ret = -EFAULT;
+               else
+                       *off_in = offset;
 
                return ret;
        }
@@ -1212,6 +1113,46 @@ long do_splice(struct file *in, loff_t __user *off_in,
        return -EINVAL;
 }
 
+static long __do_splice(struct file *in, loff_t __user *off_in,
+                       struct file *out, loff_t __user *off_out,
+                       size_t len, unsigned int flags)
+{
+       struct pipe_inode_info *ipipe;
+       struct pipe_inode_info *opipe;
+       loff_t offset, *__off_in = NULL, *__off_out = NULL;
+       long ret;
+
+       ipipe = get_pipe_info(in, true);
+       opipe = get_pipe_info(out, true);
+
+       if (ipipe && off_in)
+               return -ESPIPE;
+       if (opipe && off_out)
+               return -ESPIPE;
+
+       if (off_out) {
+               if (copy_from_user(&offset, off_out, sizeof(loff_t)))
+                       return -EFAULT;
+               __off_out = &offset;
+       }
+       if (off_in) {
+               if (copy_from_user(&offset, off_in, sizeof(loff_t)))
+                       return -EFAULT;
+               __off_in = &offset;
+       }
+
+       ret = do_splice(in, __off_in, out, __off_out, len, flags);
+       if (ret < 0)
+               return ret;
+
+       if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
+               return -EFAULT;
+       if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
+               return -EFAULT;
+
+       return ret;
+}
+
 static int iter_to_pipe(struct iov_iter *from,
                        struct pipe_inode_info *pipe,
                        unsigned flags)
@@ -1352,20 +1293,6 @@ static int vmsplice_type(struct fd f, int *type)
  * Currently we punt and implement it as a normal copy, see pipe_to_user().
  *
  */
-static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
-{
-       if (unlikely(flags & ~SPLICE_F_ALL))
-               return -EINVAL;
-
-       if (!iov_iter_count(iter))
-               return 0;
-
-       if (iov_iter_rw(iter) == WRITE)
-               return vmsplice_to_pipe(f, iter, flags);
-       else
-               return vmsplice_to_user(f, iter, flags);
-}
-
 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
                unsigned long, nr_segs, unsigned int, flags)
 {
@@ -1376,6 +1303,9 @@ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
        struct fd f;
        int type;
 
+       if (unlikely(flags & ~SPLICE_F_ALL))
+               return -EINVAL;
+
        f = fdget(fd);
        error = vmsplice_type(f, &type);
        if (error)
@@ -1383,40 +1313,21 @@ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
 
        error = import_iovec(type, uiov, nr_segs,
                             ARRAY_SIZE(iovstack), &iov, &iter);
-       if (error >= 0) {
-               error = do_vmsplice(f.file, &iter, flags);
-               kfree(iov);
-       }
-       fdput(f);
-       return error;
-}
-
-#ifdef CONFIG_COMPAT
-COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
-                   unsigned int, nr_segs, unsigned int, flags)
-{
-       struct iovec iovstack[UIO_FASTIOV];
-       struct iovec *iov = iovstack;
-       struct iov_iter iter;
-       ssize_t error;
-       struct fd f;
-       int type;
+       if (error < 0)
+               goto out_fdput;
 
-       f = fdget(fd);
-       error = vmsplice_type(f, &type);
-       if (error)
-               return error;
+       if (!iov_iter_count(&iter))
+               error = 0;
+       else if (iov_iter_rw(&iter) == WRITE)
+               error = vmsplice_to_pipe(f.file, &iter, flags);
+       else
+               error = vmsplice_to_user(f.file, &iter, flags);
 
-       error = compat_import_iovec(type, iov32, nr_segs,
-                            ARRAY_SIZE(iovstack), &iov, &iter);
-       if (error >= 0) {
-               error = do_vmsplice(f.file, &iter, flags);
-               kfree(iov);
-       }
+       kfree(iov);
+out_fdput:
        fdput(f);
        return error;
 }
-#endif
 
 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
                int, fd_out, loff_t __user *, off_out,
@@ -1436,8 +1347,8 @@ SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
        if (in.file) {
                out = fdget(fd_out);
                if (out.file) {
-                       error = do_splice(in.file, off_in, out.file, off_out,
-                                         len, flags);
+                       error = __do_splice(in.file, off_in, out.file, off_out,
+                                               len, flags);
                        fdput(out);
                }
                fdput(in);