diff mbox series

[RFC,2/4] fs/splice: add helper of splice_dismiss_pipe()

Message ID 20221103085004.1029763-3-ming.lei@redhat.com (mailing list archive)
State New, archived
Headers show
Series io_uring/splice: extend splice for supporting ublk zero copy | expand

Commit Message

Ming Lei Nov. 3, 2022, 8:50 a.m. UTC
Add helper of splice_dismiss_pipe(), so cleanup iter_file_splice_write
a bit.

And this helper will be reused in the following patch for supporting
to consume pipe by ->splice_read().

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 fs/splice.c | 47 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/fs/splice.c b/fs/splice.c
index 0878b852b355..f8999ffe6215 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -282,6 +282,34 @@  void splice_shrink_spd(struct splice_pipe_desc *spd)
 	kfree(spd->partial);
 }
 
+/* return if wakeup is needed */
+static bool splice_dismiss_pipe(struct pipe_inode_info *pipe, ssize_t bytes)
+{
+	unsigned int mask = pipe->ring_size - 1;
+	unsigned int tail = pipe->tail;
+	bool need_wakeup = false;
+
+	while (bytes) {
+		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
+
+		if (bytes >= buf->len) {
+			bytes -= buf->len;
+			buf->len = 0;
+			pipe_buf_release(pipe, buf);
+			tail++;
+			pipe->tail = tail;
+			if (pipe->files)
+				need_wakeup = true;
+		} else {
+			buf->offset += bytes;
+			buf->len -= bytes;
+			bytes = 0;
+		}
+	}
+
+	return need_wakeup;
+}
+
 /**
  * generic_file_splice_read - splice data from file to a pipe
  * @in:		file to splice from
@@ -692,23 +720,8 @@  iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
 		*ppos = sd.pos;
 
 		/* dismiss the fully eaten buffers, adjust the partial one */
-		tail = pipe->tail;
-		while (ret) {
-			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
-			if (ret >= buf->len) {
-				ret -= buf->len;
-				buf->len = 0;
-				pipe_buf_release(pipe, buf);
-				tail++;
-				pipe->tail = tail;
-				if (pipe->files)
-					sd.need_wakeup = true;
-			} else {
-				buf->offset += ret;
-				buf->len -= ret;
-				ret = 0;
-			}
-		}
+		if (splice_dismiss_pipe(pipe, ret))
+			sd.need_wakeup = true;
 	}
 done:
 	kfree(array);