Merge tag 'net-next-6.5' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev...
[platform/kernel/linux-starfive.git] / fs / splice.c
index 7a9565d..004eb1c 100644 (file)
@@ -33,6 +33,7 @@
 #include <linux/fsnotify.h>
 #include <linux/security.h>
 #include <linux/gfp.h>
+#include <linux/net.h>
 #include <linux/socket.h>
 #include <linux/sched/signal.h>
 
@@ -415,30 +416,6 @@ const struct pipe_buf_operations nosteal_pipe_buf_ops = {
 };
 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
 
-/*
- * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
- * using sendpage(). Return the number of bytes sent.
- */
-static int pipe_to_sendpage(struct pipe_inode_info *pipe,
-                           struct pipe_buffer *buf, struct splice_desc *sd)
-{
-       struct file *file = sd->u.file;
-       loff_t pos = sd->pos;
-       int more;
-
-       if (!likely(file->f_op->sendpage))
-               return -EINVAL;
-
-       more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
-
-       if (sd->len < sd->total_len &&
-           pipe_occupancy(pipe->head, pipe->tail) > 1)
-               more |= MSG_SENDPAGE_NOTLAST;
-
-       return file->f_op->sendpage(file, buf->page, buf->offset,
-                                   sd->len, &pos, more);
-}
-
 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
 {
        smp_mb();
@@ -619,7 +596,7 @@ static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_des
  * Description:
  *    This function does little more than loop over the pipe and call
  *    @actor to do the actual moving of a single struct pipe_buffer to
- *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
+ *    the desired destination. See pipe_to_file, pipe_to_sendmsg, or
  *    pipe_to_user.
  *
  */
@@ -800,8 +777,9 @@ done:
 
 EXPORT_SYMBOL(iter_file_splice_write);
 
+#ifdef CONFIG_NET
 /**
- * generic_splice_sendpage - splice data from a pipe to a socket
+ * splice_to_socket - splice data from a pipe to a socket
  * @pipe:      pipe to splice from
  * @out:       socket to write to
  * @ppos:      position in @out
@@ -813,13 +791,129 @@ EXPORT_SYMBOL(iter_file_splice_write);
  *    is involved.
  *
  */
-ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
-                               loff_t *ppos, size_t len, unsigned int flags)
+ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
+                        loff_t *ppos, size_t len, unsigned int flags)
 {
-       return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
-}
+       struct socket *sock = sock_from_file(out);
+       struct bio_vec bvec[16];
+       struct msghdr msg = {};
+       ssize_t ret = 0;
+       size_t spliced = 0;
+       bool need_wakeup = false;
+
+       pipe_lock(pipe);
+
+       while (len > 0) {
+               unsigned int head, tail, mask, bc = 0;
+               size_t remain = len;
+
+               /*
+                * Check for signal early to make process killable when there
+                * are always buffers available
+                */
+               ret = -ERESTARTSYS;
+               if (signal_pending(current))
+                       break;
+
+               while (pipe_empty(pipe->head, pipe->tail)) {
+                       ret = 0;
+                       if (!pipe->writers)
+                               goto out;
+
+                       if (spliced)
+                               goto out;
+
+                       ret = -EAGAIN;
+                       if (flags & SPLICE_F_NONBLOCK)
+                               goto out;
+
+                       ret = -ERESTARTSYS;
+                       if (signal_pending(current))
+                               goto out;
+
+                       if (need_wakeup) {
+                               wakeup_pipe_writers(pipe);
+                               need_wakeup = false;
+                       }
+
+                       pipe_wait_readable(pipe);
+               }
+
+               head = pipe->head;
+               tail = pipe->tail;
+               mask = pipe->ring_size - 1;
+
+               while (!pipe_empty(head, tail)) {
+                       struct pipe_buffer *buf = &pipe->bufs[tail & mask];
+                       size_t seg;
+
+                       if (!buf->len) {
+                               tail++;
+                               continue;
+                       }
+
+                       seg = min_t(size_t, remain, buf->len);
+
+                       ret = pipe_buf_confirm(pipe, buf);
+                       if (unlikely(ret)) {
+                               if (ret == -ENODATA)
+                                       ret = 0;
+                               break;
+                       }
 
-EXPORT_SYMBOL(generic_splice_sendpage);
+                       bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset);
+                       remain -= seg;
+                       if (remain == 0 || bc >= ARRAY_SIZE(bvec))
+                               break;
+                       tail++;
+               }
+
+               if (!bc)
+                       break;
+
+               msg.msg_flags = MSG_SPLICE_PAGES;
+               if (flags & SPLICE_F_MORE)
+                       msg.msg_flags |= MSG_MORE;
+               if (remain && pipe_occupancy(pipe->head, tail) > 0)
+                       msg.msg_flags |= MSG_MORE;
+
+               iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc,
+                             len - remain);
+               ret = sock_sendmsg(sock, &msg);
+               if (ret <= 0)
+                       break;
+
+               spliced += ret;
+               len -= ret;
+               tail = pipe->tail;
+               while (ret > 0) {
+                       struct pipe_buffer *buf = &pipe->bufs[tail & mask];
+                       size_t seg = min_t(size_t, ret, buf->len);
+
+                       buf->offset += seg;
+                       buf->len -= seg;
+                       ret -= seg;
+
+                       if (!buf->len) {
+                               pipe_buf_release(pipe, buf);
+                               tail++;
+                       }
+               }
+
+               if (tail != pipe->tail) {
+                       pipe->tail = tail;
+                       if (pipe->files)
+                               need_wakeup = true;
+               }
+       }
+
+out:
+       pipe_unlock(pipe);
+       if (need_wakeup)
+               wakeup_pipe_writers(pipe);
+       return spliced ?: ret;
+}
+#endif
 
 static int warn_unsupported(struct file *file, const char *op)
 {
@@ -840,6 +934,17 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
        return out->f_op->splice_write(pipe, out, ppos, len, flags);
 }
 
+/*
+ * Indicate to the caller that there was a premature EOF when reading from the
+ * source and the caller didn't indicate they would be sending more data after
+ * this.
+ */
+static void do_splice_eof(struct splice_desc *sd)
+{
+       if (sd->splice_eof)
+               sd->splice_eof(sd);
+}
+
 /**
  * vfs_splice_read - Read data from a file and splice it into a pipe
  * @in:                File to splice from
@@ -944,13 +1049,17 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
         */
        bytes = 0;
        len = sd->total_len;
+
+       /* Don't block on output, we have to drain the direct pipe. */
        flags = sd->flags;
+       sd->flags &= ~SPLICE_F_NONBLOCK;
 
        /*
-        * Don't block on output, we have to drain the direct pipe.
+        * We signal MORE until we've read sufficient data to fulfill the
+        * request and we keep signalling it if the caller set it.
         */
-       sd->flags &= ~SPLICE_F_NONBLOCK;
        more = sd->flags & SPLICE_F_MORE;
+       sd->flags |= SPLICE_F_MORE;
 
        WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
 
@@ -960,20 +1069,18 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
 
                ret = vfs_splice_read(in, &pos, pipe, len, flags);
                if (unlikely(ret <= 0))
-                       goto out_release;
+                       goto read_failure;
 
                read_len = ret;
                sd->total_len = read_len;
 
                /*
-                * If more data is pending, set SPLICE_F_MORE
-                * If this is the last data and SPLICE_F_MORE was not set
-                * initially, clears it.
+                * If we now have sufficient data to fulfill the request then
+                * we clear SPLICE_F_MORE if it was not set initially.
                 */
-               if (read_len < len)
-                       sd->flags |= SPLICE_F_MORE;
-               else if (!more)
+               if (read_len >= len && !more)
                        sd->flags &= ~SPLICE_F_MORE;
+
                /*
                 * NOTE: nonblocking mode only applies to the input. We
                 * must not do the output in nonblocking mode as then we
@@ -1000,6 +1107,15 @@ done:
        file_accessed(in);
        return bytes;
 
+read_failure:
+       /*
+        * If the user did *not* set SPLICE_F_MORE *and* we didn't hit that
+        * "use all of len" case that cleared SPLICE_F_MORE, *and* we did a
+        * "->splice_in()" that returned EOF (ie zero) *and* we have sent at
+        * least 1 byte *then* we will also do the ->splice_eof() call.
+        */
+       if (ret == 0 && !more && len > 0 && bytes)
+               do_splice_eof(sd);
 out_release:
        /*
         * If we did an incomplete transfer we must release
@@ -1028,6 +1144,14 @@ static int direct_splice_actor(struct pipe_inode_info *pipe,
                              sd->flags);
 }
 
+static void direct_file_splice_eof(struct splice_desc *sd)
+{
+       struct file *file = sd->u.file;
+
+       if (file->f_op->splice_eof)
+               file->f_op->splice_eof(file);
+}
+
 /**
  * do_splice_direct - splices data directly between two files
  * @in:                file to splice from
@@ -1053,6 +1177,7 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
                .flags          = flags,
                .pos            = *ppos,
                .u.file         = out,
+               .splice_eof     = direct_file_splice_eof,
                .opos           = opos,
        };
        long ret;