2 * "splice": joining two ropes together by interweaving their strands.
4 * This is the "extended pipe" functionality, where a pipe is used as
5 * an arbitrary in-memory buffer. Think of a pipe as a small kernel
6 * buffer that you can use to transfer data from one end to the other.
8 * The traditional unix read/write is extended with a "splice()" operation
9 * that transfers data buffers to or from a pipe buffer.
11 * Named by Larry McVoy, original implementation from Linus, extended by
12 * Jens to support splicing to files, network, direct splicing, etc and
13 * fixing lots of bugs.
15 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
16 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
21 #include <linux/file.h>
22 #include <linux/pagemap.h>
23 #include <linux/splice.h>
24 #include <linux/memcontrol.h>
25 #include <linux/mm_inline.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/export.h>
29 #include <linux/syscalls.h>
30 #include <linux/uio.h>
31 #include <linux/security.h>
32 #include <linux/gfp.h>
33 #include <linux/socket.h>
34 #include <linux/compat.h>
38 * Attempt to steal a page from a pipe buffer. This should perhaps go into
39 * a vm helper function, it's already simplified quite a bit by the
40 * addition of remove_mapping(). If success is returned, the caller may
41 * attempt to reuse this page for another destination.
43 static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
44 struct pipe_buffer *buf)
46 struct page *page = buf->page;
47 struct address_space *mapping;
51 mapping = page_mapping(page);
53 WARN_ON(!PageUptodate(page));
56 * At least for ext2 with nobh option, we need to wait on
57 * writeback completing on this page, since we'll remove it
58 * from the pagecache. Otherwise truncate wont wait on the
59 * page, allowing the disk blocks to be reused by someone else
60 * before we actually wrote our data to them. fs corruption
63 wait_on_page_writeback(page);
65 if (page_has_private(page) &&
66 !try_to_release_page(page, GFP_KERNEL))
70 * If we succeeded in removing the mapping, set LRU flag
73 if (remove_mapping(mapping, page)) {
74 buf->flags |= PIPE_BUF_FLAG_LRU;
80 * Raced with truncate or failed to remove page from current
81 * address space, unlock and return failure.
88 static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
89 struct pipe_buffer *buf)
92 buf->flags &= ~PIPE_BUF_FLAG_LRU;
96 * Check whether the contents of buf is OK to access. Since the content
97 * is a page cache page, IO may be in flight.
99 static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
100 struct pipe_buffer *buf)
102 struct page *page = buf->page;
105 if (!PageUptodate(page)) {
109 * Page got truncated/unhashed. This will cause a 0-byte
110 * splice, if this is the first page.
112 if (!page->mapping) {
118 * Uh oh, read-error from disk.
120 if (!PageUptodate(page)) {
126 * Page is ok afterall, we are done.
137 const struct pipe_buf_operations page_cache_pipe_buf_ops = {
139 .confirm = page_cache_pipe_buf_confirm,
140 .release = page_cache_pipe_buf_release,
141 .steal = page_cache_pipe_buf_steal,
142 .get = generic_pipe_buf_get,
145 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
146 struct pipe_buffer *buf)
148 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
151 buf->flags |= PIPE_BUF_FLAG_LRU;
152 return generic_pipe_buf_steal(pipe, buf);
155 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
157 .confirm = generic_pipe_buf_confirm,
158 .release = page_cache_pipe_buf_release,
159 .steal = user_page_pipe_buf_steal,
160 .get = generic_pipe_buf_get,
163 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
166 if (waitqueue_active(&pipe->wait))
167 wake_up_interruptible(&pipe->wait);
168 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
172 * splice_to_pipe - fill passed data into a pipe
173 * @pipe: pipe to fill
177 * @spd contains a map of pages and len/offset tuples, along with
178 * the struct pipe_buf_operations associated with these pages. This
179 * function will link that data to the pipe.
182 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183 struct splice_pipe_desc *spd)
185 unsigned int spd_pages = spd->nr_pages;
186 int ret = 0, page_nr = 0;
191 if (unlikely(!pipe->readers)) {
192 send_sig(SIGPIPE, current, 0);
197 while (pipe->nrbufs < pipe->buffers) {
198 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
199 struct pipe_buffer *buf = pipe->bufs + newbuf;
201 buf->page = spd->pages[page_nr];
202 buf->offset = spd->partial[page_nr].offset;
203 buf->len = spd->partial[page_nr].len;
204 buf->private = spd->partial[page_nr].private;
211 if (!--spd->nr_pages)
219 while (page_nr < spd_pages)
220 spd->spd_release(spd, page_nr++);
224 EXPORT_SYMBOL_GPL(splice_to_pipe);
226 ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
230 if (unlikely(!pipe->readers)) {
231 send_sig(SIGPIPE, current, 0);
233 } else if (pipe->nrbufs == pipe->buffers) {
236 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
237 pipe->bufs[newbuf] = *buf;
241 buf->ops->release(pipe, buf);
245 EXPORT_SYMBOL(add_to_pipe);
247 void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
249 put_page(spd->pages[i]);
253 * Check if we need to grow the arrays holding pages and partial page
256 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
258 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
260 spd->nr_pages_max = buffers;
261 if (buffers <= PIPE_DEF_BUFFERS)
264 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
265 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
267 if (spd->pages && spd->partial)
275 void splice_shrink_spd(struct splice_pipe_desc *spd)
277 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
285 * generic_file_splice_read - splice data from file to a pipe
286 * @in: file to splice from
287 * @ppos: position in @in
288 * @pipe: pipe to splice to
289 * @len: number of bytes to splice
290 * @flags: splice modifier flags
293 * Will read pages from given file and fill them into a pipe. Can be
294 * used as long as it has more or less sane ->read_iter().
297 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
298 struct pipe_inode_info *pipe, size_t len,
306 isize = i_size_read(in->f_mapping->host);
307 if (unlikely(*ppos >= isize))
310 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
312 init_sync_kiocb(&kiocb, in);
313 kiocb.ki_pos = *ppos;
314 ret = in->f_op->read_iter(&kiocb, &to);
316 *ppos = kiocb.ki_pos;
318 } else if (ret < 0) {
319 if (WARN_ON(to.idx != idx || to.iov_offset)) {
321 * a bogus ->read_iter() has copied something and still
322 * returned an error instead of a short read.
326 iov_iter_advance(&to, 0); /* to free what was emitted */
329 * callers of ->splice_read() expect -EAGAIN on
330 * "can't put anything in there", rather than -EFAULT.
338 EXPORT_SYMBOL(generic_file_splice_read);
340 const struct pipe_buf_operations default_pipe_buf_ops = {
342 .confirm = generic_pipe_buf_confirm,
343 .release = generic_pipe_buf_release,
344 .steal = generic_pipe_buf_steal,
345 .get = generic_pipe_buf_get,
348 static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
349 struct pipe_buffer *buf)
354 /* Pipe buffer operations for a socket and similar. */
355 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
357 .confirm = generic_pipe_buf_confirm,
358 .release = generic_pipe_buf_release,
359 .steal = generic_pipe_buf_nosteal,
360 .get = generic_pipe_buf_get,
362 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
364 static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
365 unsigned long vlen, loff_t offset)
373 /* The cast to a user pointer is valid due to the set_fs() */
374 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
380 ssize_t kernel_write(struct file *file, const char *buf, size_t count,
388 /* The cast to a user pointer is valid due to the set_fs() */
389 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
394 EXPORT_SYMBOL(kernel_write);
396 static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
397 struct pipe_inode_info *pipe, size_t len,
400 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
403 unsigned int nr_pages;
404 size_t offset, dummy, copied = 0;
408 if (pipe->nrbufs == pipe->buffers)
412 * Try to keep page boundaries matching to source pagecache ones -
413 * it probably won't be much help, but...
415 offset = *ppos & ~PAGE_MASK;
417 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
419 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &dummy);
423 nr_pages = res / PAGE_SIZE;
426 if (nr_pages > PIPE_DEF_BUFFERS) {
427 vec = kmalloc(nr_pages * sizeof(struct kvec), GFP_KERNEL);
428 if (unlikely(!vec)) {
434 pipe->bufs[to.idx].offset = offset;
435 pipe->bufs[to.idx].len -= offset;
437 for (i = 0; i < nr_pages; i++) {
438 size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
439 vec[i].iov_base = page_address(pages[i]) + offset;
440 vec[i].iov_len = this_len;
445 res = kernel_readv(in, vec, nr_pages, *ppos);
454 for (i = 0; i < nr_pages; i++)
457 iov_iter_advance(&to, copied); /* truncates and discards */
462 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
463 * using sendpage(). Return the number of bytes sent.
465 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
466 struct pipe_buffer *buf, struct splice_desc *sd)
468 struct file *file = sd->u.file;
469 loff_t pos = sd->pos;
472 if (!likely(file->f_op->sendpage))
475 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
477 if (sd->len < sd->total_len && pipe->nrbufs > 1)
478 more |= MSG_SENDPAGE_NOTLAST;
480 return file->f_op->sendpage(file, buf->page, buf->offset,
481 sd->len, &pos, more);
484 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
487 if (waitqueue_active(&pipe->wait))
488 wake_up_interruptible(&pipe->wait);
489 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
493 * splice_from_pipe_feed - feed available data from a pipe to a file
494 * @pipe: pipe to splice from
495 * @sd: information to @actor
496 * @actor: handler that splices the data
499 * This function loops over the pipe and calls @actor to do the
500 * actual moving of a single struct pipe_buffer to the desired
501 * destination. It returns when there's no more buffers left in
502 * the pipe or if the requested number of bytes (@sd->total_len)
503 * have been copied. It returns a positive number (one) if the
504 * pipe needs to be filled with more data, zero if the required
505 * number of bytes have been copied and -errno on error.
507 * This, together with splice_from_pipe_{begin,end,next}, may be
508 * used to implement the functionality of __splice_from_pipe() when
509 * locking is required around copying the pipe buffers to the
512 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
517 while (pipe->nrbufs) {
518 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
519 const struct pipe_buf_operations *ops = buf->ops;
522 if (sd->len > sd->total_len)
523 sd->len = sd->total_len;
525 ret = buf->ops->confirm(pipe, buf);
532 ret = actor(pipe, buf, sd);
539 sd->num_spliced += ret;
542 sd->total_len -= ret;
546 ops->release(pipe, buf);
547 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
550 sd->need_wakeup = true;
561 * splice_from_pipe_next - wait for some data to splice from
562 * @pipe: pipe to splice from
563 * @sd: information about the splice operation
566 * This function will wait for some data and return a positive
567 * value (one) if pipe buffers are available. It will return zero
568 * or -errno if no more data needs to be spliced.
570 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
573 * Check for signal early to make process killable when there are
574 * always buffers available
576 if (signal_pending(current))
579 while (!pipe->nrbufs) {
583 if (!pipe->waiting_writers && sd->num_spliced)
586 if (sd->flags & SPLICE_F_NONBLOCK)
589 if (signal_pending(current))
592 if (sd->need_wakeup) {
593 wakeup_pipe_writers(pipe);
594 sd->need_wakeup = false;
604 * splice_from_pipe_begin - start splicing from pipe
605 * @sd: information about the splice operation
608 * This function should be called before a loop containing
609 * splice_from_pipe_next() and splice_from_pipe_feed() to
610 * initialize the necessary fields of @sd.
612 static void splice_from_pipe_begin(struct splice_desc *sd)
615 sd->need_wakeup = false;
619 * splice_from_pipe_end - finish splicing from pipe
620 * @pipe: pipe to splice from
621 * @sd: information about the splice operation
624 * This function will wake up pipe writers if necessary. It should
625 * be called after a loop containing splice_from_pipe_next() and
626 * splice_from_pipe_feed().
628 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
631 wakeup_pipe_writers(pipe);
635 * __splice_from_pipe - splice data from a pipe to given actor
636 * @pipe: pipe to splice from
637 * @sd: information to @actor
638 * @actor: handler that splices the data
641 * This function does little more than loop over the pipe and call
642 * @actor to do the actual moving of a single struct pipe_buffer to
643 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
647 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
652 splice_from_pipe_begin(sd);
655 ret = splice_from_pipe_next(pipe, sd);
657 ret = splice_from_pipe_feed(pipe, sd, actor);
659 splice_from_pipe_end(pipe, sd);
661 return sd->num_spliced ? sd->num_spliced : ret;
663 EXPORT_SYMBOL(__splice_from_pipe);
666 * splice_from_pipe - splice data from a pipe to a file
667 * @pipe: pipe to splice from
668 * @out: file to splice to
669 * @ppos: position in @out
670 * @len: how many bytes to splice
671 * @flags: splice modifier flags
672 * @actor: handler that splices the data
675 * See __splice_from_pipe. This function locks the pipe inode,
676 * otherwise it's identical to __splice_from_pipe().
679 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
680 loff_t *ppos, size_t len, unsigned int flags,
684 struct splice_desc sd = {
692 ret = __splice_from_pipe(pipe, &sd, actor);
699 * iter_file_splice_write - splice data from a pipe to a file
701 * @out: file to write to
702 * @ppos: position in @out
703 * @len: number of bytes to splice
704 * @flags: splice modifier flags
707 * Will either move or copy pages (determined by @flags options) from
708 * the given pipe inode to the given file.
709 * This one is ->write_iter-based.
713 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
714 loff_t *ppos, size_t len, unsigned int flags)
716 struct splice_desc sd = {
722 int nbufs = pipe->buffers;
723 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
727 if (unlikely(!array))
732 splice_from_pipe_begin(&sd);
733 while (sd.total_len) {
734 struct iov_iter from;
738 ret = splice_from_pipe_next(pipe, &sd);
742 if (unlikely(nbufs < pipe->buffers)) {
744 nbufs = pipe->buffers;
745 array = kcalloc(nbufs, sizeof(struct bio_vec),
753 /* build the vector */
755 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
756 struct pipe_buffer *buf = pipe->bufs + idx;
757 size_t this_len = buf->len;
762 if (idx == pipe->buffers - 1)
765 ret = buf->ops->confirm(pipe, buf);
772 array[n].bv_page = buf->page;
773 array[n].bv_len = this_len;
774 array[n].bv_offset = buf->offset;
778 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
779 sd.total_len - left);
780 ret = vfs_iter_write(out, &from, &sd.pos);
784 sd.num_spliced += ret;
788 /* dismiss the fully eaten buffers, adjust the partial one */
790 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
791 if (ret >= buf->len) {
792 const struct pipe_buf_operations *ops = buf->ops;
796 ops->release(pipe, buf);
797 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
800 sd.need_wakeup = true;
810 splice_from_pipe_end(pipe, &sd);
815 ret = sd.num_spliced;
820 EXPORT_SYMBOL(iter_file_splice_write);
822 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
823 struct splice_desc *sd)
827 loff_t tmp = sd->pos;
829 data = kmap(buf->page);
830 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
836 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
837 struct file *out, loff_t *ppos,
838 size_t len, unsigned int flags)
842 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
850 * generic_splice_sendpage - splice data from a pipe to a socket
851 * @pipe: pipe to splice from
852 * @out: socket to write to
853 * @ppos: position in @out
854 * @len: number of bytes to splice
855 * @flags: splice modifier flags
858 * Will send @len bytes from the pipe to a network socket. No data copying
862 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
863 loff_t *ppos, size_t len, unsigned int flags)
865 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
868 EXPORT_SYMBOL(generic_splice_sendpage);
871 * Attempt to initiate a splice from pipe to file.
873 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
874 loff_t *ppos, size_t len, unsigned int flags)
876 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
877 loff_t *, size_t, unsigned int);
879 if (out->f_op->splice_write)
880 splice_write = out->f_op->splice_write;
882 splice_write = default_file_splice_write;
884 return splice_write(pipe, out, ppos, len, flags);
888 * Attempt to initiate a splice from a file to a pipe.
890 static long do_splice_to(struct file *in, loff_t *ppos,
891 struct pipe_inode_info *pipe, size_t len,
894 ssize_t (*splice_read)(struct file *, loff_t *,
895 struct pipe_inode_info *, size_t, unsigned int);
898 if (unlikely(!(in->f_mode & FMODE_READ)))
901 ret = rw_verify_area(READ, in, ppos, len);
902 if (unlikely(ret < 0))
905 if (unlikely(len > MAX_RW_COUNT))
908 if (in->f_op->splice_read)
909 splice_read = in->f_op->splice_read;
911 splice_read = default_file_splice_read;
913 return splice_read(in, ppos, pipe, len, flags);
917 * splice_direct_to_actor - splices data directly between two non-pipes
918 * @in: file to splice from
919 * @sd: actor information on where to splice to
920 * @actor: handles the data splicing
923 * This is a special case helper to splice directly between two
924 * points, without requiring an explicit pipe. Internally an allocated
925 * pipe is cached in the process, and reused during the lifetime of
929 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
930 splice_direct_actor *actor)
932 struct pipe_inode_info *pipe;
939 * We require the input being a regular file, as we don't want to
940 * randomly drop data for eg socket -> socket splicing. Use the
941 * piped splicing for that!
943 i_mode = file_inode(in)->i_mode;
944 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
948 * neither in nor out is a pipe, setup an internal pipe attached to
949 * 'out' and transfer the wanted data from 'in' to 'out' through that
951 pipe = current->splice_pipe;
952 if (unlikely(!pipe)) {
953 pipe = alloc_pipe_info();
958 * We don't have an immediate reader, but we'll read the stuff
959 * out of the pipe right after the splice_to_pipe(). So set
960 * PIPE_READERS appropriately.
964 current->splice_pipe = pipe;
976 * Don't block on output, we have to drain the direct pipe.
978 sd->flags &= ~SPLICE_F_NONBLOCK;
979 more = sd->flags & SPLICE_F_MORE;
983 loff_t pos = sd->pos, prev_pos = pos;
985 ret = do_splice_to(in, &pos, pipe, len, flags);
986 if (unlikely(ret <= 0))
990 sd->total_len = read_len;
993 * If more data is pending, set SPLICE_F_MORE
994 * If this is the last data and SPLICE_F_MORE was not set
995 * initially, clears it.
998 sd->flags |= SPLICE_F_MORE;
1000 sd->flags &= ~SPLICE_F_MORE;
1002 * NOTE: nonblocking mode only applies to the input. We
1003 * must not do the output in nonblocking mode as then we
1004 * could get stuck data in the internal pipe:
1006 ret = actor(pipe, sd);
1007 if (unlikely(ret <= 0)) {
1016 if (ret < read_len) {
1017 sd->pos = prev_pos + ret;
1023 pipe->nrbufs = pipe->curbuf = 0;
1029 * If we did an incomplete transfer we must release
1030 * the pipe buffers in question:
1032 for (i = 0; i < pipe->buffers; i++) {
1033 struct pipe_buffer *buf = pipe->bufs + i;
1036 buf->ops->release(pipe, buf);
1046 EXPORT_SYMBOL(splice_direct_to_actor);
1048 static int direct_splice_actor(struct pipe_inode_info *pipe,
1049 struct splice_desc *sd)
1051 struct file *file = sd->u.file;
1053 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1058 * do_splice_direct - splices data directly between two files
1059 * @in: file to splice from
1060 * @ppos: input file offset
1061 * @out: file to splice to
1062 * @opos: output file offset
1063 * @len: number of bytes to splice
1064 * @flags: splice modifier flags
1067 * For use by do_sendfile(). splice can easily emulate sendfile, but
1068 * doing it in the application would incur an extra system call
1069 * (splice in + splice out, as compared to just sendfile()). So this helper
1070 * can splice directly through a process-private pipe.
1073 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1074 loff_t *opos, size_t len, unsigned int flags)
1076 struct splice_desc sd = {
1086 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1089 if (unlikely(out->f_flags & O_APPEND))
1092 ret = rw_verify_area(WRITE, out, opos, len);
1093 if (unlikely(ret < 0))
1096 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1102 EXPORT_SYMBOL(do_splice_direct);
1104 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1106 while (pipe->nrbufs == pipe->buffers) {
1107 if (flags & SPLICE_F_NONBLOCK)
1109 if (signal_pending(current))
1110 return -ERESTARTSYS;
1111 pipe->waiting_writers++;
1113 pipe->waiting_writers--;
1118 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1119 struct pipe_inode_info *opipe,
1120 size_t len, unsigned int flags);
1123 * Determine where to splice to/from.
1125 static long do_splice(struct file *in, loff_t __user *off_in,
1126 struct file *out, loff_t __user *off_out,
1127 size_t len, unsigned int flags)
1129 struct pipe_inode_info *ipipe;
1130 struct pipe_inode_info *opipe;
1134 ipipe = get_pipe_info(in);
1135 opipe = get_pipe_info(out);
1137 if (ipipe && opipe) {
1138 if (off_in || off_out)
1141 if (!(in->f_mode & FMODE_READ))
1144 if (!(out->f_mode & FMODE_WRITE))
1147 /* Splicing to self would be fun, but... */
1151 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1158 if (!(out->f_mode & FMODE_PWRITE))
1160 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1163 offset = out->f_pos;
1166 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1169 if (unlikely(out->f_flags & O_APPEND))
1172 ret = rw_verify_area(WRITE, out, &offset, len);
1173 if (unlikely(ret < 0))
1176 file_start_write(out);
1177 ret = do_splice_from(ipipe, out, &offset, len, flags);
1178 file_end_write(out);
1181 out->f_pos = offset;
1182 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1192 if (!(in->f_mode & FMODE_PREAD))
1194 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1201 ret = wait_for_space(opipe, flags);
1203 ret = do_splice_to(in, &offset, opipe, len, flags);
1206 wakeup_pipe_readers(opipe);
1209 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1218 static int iter_to_pipe(struct iov_iter *from,
1219 struct pipe_inode_info *pipe,
1222 struct pipe_buffer buf = {
1223 .ops = &user_page_pipe_buf_ops,
1228 bool failed = false;
1230 while (iov_iter_count(from) && !failed) {
1231 struct page *pages[16];
1236 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1242 for (n = 0; copied; n++, start = 0) {
1243 int size = min_t(int, copied, PAGE_SIZE - start);
1245 buf.page = pages[n];
1248 ret = add_to_pipe(pipe, &buf);
1249 if (unlikely(ret < 0)) {
1252 iov_iter_advance(from, ret);
1261 return total ? total : ret;
1264 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1265 struct splice_desc *sd)
1267 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1268 return n == sd->len ? n : -EFAULT;
1272 * For lack of a better implementation, implement vmsplice() to userspace
1273 * as a simple copy of the pipes pages to the user iov.
1275 static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
1276 unsigned long nr_segs, unsigned int flags)
1278 struct pipe_inode_info *pipe;
1279 struct splice_desc sd;
1281 struct iovec iovstack[UIO_FASTIOV];
1282 struct iovec *iov = iovstack;
1283 struct iov_iter iter;
1285 pipe = get_pipe_info(file);
1289 ret = import_iovec(READ, uiov, nr_segs,
1290 ARRAY_SIZE(iovstack), &iov, &iter);
1294 sd.total_len = iov_iter_count(&iter);
1302 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1311 * vmsplice splices a user address range into a pipe. It can be thought of
1312 * as splice-from-memory, where the regular splice is splice-from-file (or
1313 * to file). In both cases the output is a pipe, naturally.
1315 static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1316 unsigned long nr_segs, unsigned int flags)
1318 struct pipe_inode_info *pipe;
1319 struct iovec iovstack[UIO_FASTIOV];
1320 struct iovec *iov = iovstack;
1321 struct iov_iter from;
1323 unsigned buf_flag = 0;
1325 if (flags & SPLICE_F_GIFT)
1326 buf_flag = PIPE_BUF_FLAG_GIFT;
1328 pipe = get_pipe_info(file);
1332 ret = import_iovec(WRITE, uiov, nr_segs,
1333 ARRAY_SIZE(iovstack), &iov, &from);
1338 ret = wait_for_space(pipe, flags);
1340 ret = iter_to_pipe(&from, pipe, buf_flag);
1343 wakeup_pipe_readers(pipe);
1349 * Note that vmsplice only really supports true splicing _from_ user memory
1350 * to a pipe, not the other way around. Splicing from user memory is a simple
1351 * operation that can be supported without any funky alignment restrictions
1352 * or nasty vm tricks. We simply map in the user memory and fill them into
1353 * a pipe. The reverse isn't quite as easy, though. There are two possible
1354 * solutions for that:
1356 * - memcpy() the data internally, at which point we might as well just
1357 * do a regular read() on the buffer anyway.
1358 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1359 * has restriction limitations on both ends of the pipe).
1361 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1364 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1365 unsigned long, nr_segs, unsigned int, flags)
1370 if (unlikely(nr_segs > UIO_MAXIOV))
1372 else if (unlikely(!nr_segs))
1378 if (f.file->f_mode & FMODE_WRITE)
1379 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1380 else if (f.file->f_mode & FMODE_READ)
1381 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1389 #ifdef CONFIG_COMPAT
1390 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1391 unsigned int, nr_segs, unsigned int, flags)
1394 struct iovec __user *iov;
1395 if (nr_segs > UIO_MAXIOV)
1397 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1398 for (i = 0; i < nr_segs; i++) {
1399 struct compat_iovec v;
1400 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1401 get_user(v.iov_len, &iov32[i].iov_len) ||
1402 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1403 put_user(v.iov_len, &iov[i].iov_len))
1406 return sys_vmsplice(fd, iov, nr_segs, flags);
1410 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1411 int, fd_out, loff_t __user *, off_out,
1412 size_t, len, unsigned int, flags)
1423 if (in.file->f_mode & FMODE_READ) {
1424 out = fdget(fd_out);
1426 if (out.file->f_mode & FMODE_WRITE)
1427 error = do_splice(in.file, off_in,
1439 * Make sure there's data to read. Wait for input if we can, otherwise
1440 * return an appropriate error.
1442 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1447 * Check ->nrbufs without the inode lock first. This function
1448 * is speculative anyways, so missing one is ok.
1456 while (!pipe->nrbufs) {
1457 if (signal_pending(current)) {
1463 if (!pipe->waiting_writers) {
1464 if (flags & SPLICE_F_NONBLOCK) {
1477 * Make sure there's writeable room. Wait for room if we can, otherwise
1478 * return an appropriate error.
1480 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1485 * Check ->nrbufs without the inode lock first. This function
1486 * is speculative anyways, so missing one is ok.
1488 if (pipe->nrbufs < pipe->buffers)
1494 while (pipe->nrbufs >= pipe->buffers) {
1495 if (!pipe->readers) {
1496 send_sig(SIGPIPE, current, 0);
1500 if (flags & SPLICE_F_NONBLOCK) {
1504 if (signal_pending(current)) {
1508 pipe->waiting_writers++;
1510 pipe->waiting_writers--;
1518 * Splice contents of ipipe to opipe.
1520 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1521 struct pipe_inode_info *opipe,
1522 size_t len, unsigned int flags)
1524 struct pipe_buffer *ibuf, *obuf;
1526 bool input_wakeup = false;
1530 ret = ipipe_prep(ipipe, flags);
1534 ret = opipe_prep(opipe, flags);
1539 * Potential ABBA deadlock, work around it by ordering lock
1540 * grabbing by pipe info address. Otherwise two different processes
1541 * could deadlock (one doing tee from A -> B, the other from B -> A).
1543 pipe_double_lock(ipipe, opipe);
1546 if (!opipe->readers) {
1547 send_sig(SIGPIPE, current, 0);
1553 if (!ipipe->nrbufs && !ipipe->writers)
1557 * Cannot make any progress, because either the input
1558 * pipe is empty or the output pipe is full.
1560 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
1561 /* Already processed some buffers, break */
1565 if (flags & SPLICE_F_NONBLOCK) {
1571 * We raced with another reader/writer and haven't
1572 * managed to process any buffers. A zero return
1573 * value means EOF, so retry instead.
1580 ibuf = ipipe->bufs + ipipe->curbuf;
1581 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1582 obuf = opipe->bufs + nbuf;
1584 if (len >= ibuf->len) {
1586 * Simply move the whole buffer from ipipe to opipe
1591 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
1593 input_wakeup = true;
1596 * Get a reference to this pipe buffer,
1597 * so we can copy the contents over.
1599 pipe_buf_get(ipipe, ibuf);
1603 * Don't inherit the gift flag, we need to
1604 * prevent multiple steals of this page.
1606 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1610 ibuf->offset += obuf->len;
1611 ibuf->len -= obuf->len;
1621 * If we put data in the output pipe, wakeup any potential readers.
1624 wakeup_pipe_readers(opipe);
1627 wakeup_pipe_writers(ipipe);
1633 * Link contents of ipipe to opipe.
1635 static int link_pipe(struct pipe_inode_info *ipipe,
1636 struct pipe_inode_info *opipe,
1637 size_t len, unsigned int flags)
1639 struct pipe_buffer *ibuf, *obuf;
1640 int ret = 0, i = 0, nbuf;
1643 * Potential ABBA deadlock, work around it by ordering lock
1644 * grabbing by pipe info address. Otherwise two different processes
1645 * could deadlock (one doing tee from A -> B, the other from B -> A).
1647 pipe_double_lock(ipipe, opipe);
1650 if (!opipe->readers) {
1651 send_sig(SIGPIPE, current, 0);
1658 * If we have iterated all input buffers or ran out of
1659 * output room, break.
1661 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1664 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1665 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1668 * Get a reference to this pipe buffer,
1669 * so we can copy the contents over.
1671 pipe_buf_get(ipipe, ibuf);
1673 obuf = opipe->bufs + nbuf;
1677 * Don't inherit the gift flag, we need to
1678 * prevent multiple steals of this page.
1680 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1682 if (obuf->len > len)
1692 * return EAGAIN if we have the potential of some data in the
1693 * future, otherwise just return 0
1695 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1702 * If we put data in the output pipe, wakeup any potential readers.
1705 wakeup_pipe_readers(opipe);
1711 * This is a tee(1) implementation that works on pipes. It doesn't copy
1712 * any data, it simply references the 'in' pages on the 'out' pipe.
1713 * The 'flags' used are the SPLICE_F_* variants, currently the only
1714 * applicable one is SPLICE_F_NONBLOCK.
1716 static long do_tee(struct file *in, struct file *out, size_t len,
1719 struct pipe_inode_info *ipipe = get_pipe_info(in);
1720 struct pipe_inode_info *opipe = get_pipe_info(out);
1724 * Duplicate the contents of ipipe to opipe without actually
1727 if (ipipe && opipe && ipipe != opipe) {
1729 * Keep going, unless we encounter an error. The ipipe/opipe
1730 * ordering doesn't really matter.
1732 ret = ipipe_prep(ipipe, flags);
1734 ret = opipe_prep(opipe, flags);
1736 ret = link_pipe(ipipe, opipe, len, flags);
1743 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1754 if (in.file->f_mode & FMODE_READ) {
1755 struct fd out = fdget(fdout);
1757 if (out.file->f_mode & FMODE_WRITE)
1758 error = do_tee(in.file, out.file,