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(struct file *in, loff_t *ppos,
286 struct pipe_inode_info *pipe, size_t len,
289 struct address_space *mapping = in->f_mapping;
290 unsigned int loff, nr_pages, req_pages;
291 struct page *pages[PIPE_DEF_BUFFERS];
292 struct partial_page partial[PIPE_DEF_BUFFERS];
294 pgoff_t index, end_index;
297 struct splice_pipe_desc spd = {
300 .nr_pages_max = PIPE_DEF_BUFFERS,
302 .ops = &page_cache_pipe_buf_ops,
303 .spd_release = spd_release_page,
306 if (splice_grow_spd(pipe, &spd))
309 index = *ppos >> PAGE_SHIFT;
310 loff = *ppos & ~PAGE_MASK;
311 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
312 nr_pages = min(req_pages, spd.nr_pages_max);
315 * Lookup the (hopefully) full range of pages we need.
317 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
318 index += spd.nr_pages;
321 * If find_get_pages_contig() returned fewer pages than we needed,
322 * readahead/allocate the rest and fill in the holes.
324 if (spd.nr_pages < nr_pages)
325 page_cache_sync_readahead(mapping, &in->f_ra, in,
326 index, req_pages - spd.nr_pages);
329 while (spd.nr_pages < nr_pages) {
331 * Page could be there, find_get_pages_contig() breaks on
334 page = find_get_page(mapping, index);
337 * page didn't exist, allocate one.
339 page = page_cache_alloc_cold(mapping);
343 error = add_to_page_cache_lru(page, mapping, index,
344 mapping_gfp_constraint(mapping, GFP_KERNEL));
345 if (unlikely(error)) {
347 if (error == -EEXIST)
352 * add_to_page_cache() locks the page, unlock it
353 * to avoid convoluting the logic below even more.
358 spd.pages[spd.nr_pages++] = page;
363 * Now loop over the map and see if we need to start IO on any
364 * pages, fill in the partial map, etc.
366 index = *ppos >> PAGE_SHIFT;
367 nr_pages = spd.nr_pages;
369 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
370 unsigned int this_len;
376 * this_len is the max we'll use from this page
378 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
379 page = spd.pages[page_nr];
381 if (PageReadahead(page))
382 page_cache_async_readahead(mapping, &in->f_ra, in,
383 page, index, req_pages - page_nr);
386 * If the page isn't uptodate, we may need to start io on it
388 if (!PageUptodate(page)) {
392 * Page was truncated, or invalidated by the
393 * filesystem. Redo the find/create, but this time the
394 * page is kept locked, so there's no chance of another
395 * race with truncate/invalidate.
397 if (!page->mapping) {
400 page = find_or_create_page(mapping, index,
401 mapping_gfp_mask(mapping));
407 put_page(spd.pages[page_nr]);
408 spd.pages[page_nr] = page;
411 * page was already under io and is now done, great
413 if (PageUptodate(page)) {
419 * need to read in the page
421 error = mapping->a_ops->readpage(in, page);
422 if (unlikely(error)) {
426 if (error == AOP_TRUNCATED_PAGE)
434 * i_size must be checked after PageUptodate.
436 isize = i_size_read(mapping->host);
437 end_index = (isize - 1) >> PAGE_SHIFT;
438 if (unlikely(!isize || index > end_index))
442 * if this is the last page, see if we need to shrink
443 * the length and stop
445 if (end_index == index) {
449 * max good bytes in this page
451 plen = ((isize - 1) & ~PAGE_MASK) + 1;
456 * force quit after adding this page
458 this_len = min(this_len, plen - loff);
462 spd.partial[page_nr].offset = loff;
463 spd.partial[page_nr].len = this_len;
471 * Release any pages at the end, if we quit early. 'page_nr' is how far
472 * we got, 'nr_pages' is how many pages are in the map.
474 while (page_nr < nr_pages)
475 put_page(spd.pages[page_nr++]);
476 in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
479 error = splice_to_pipe(pipe, &spd);
481 splice_shrink_spd(&spd);
486 * generic_file_splice_read - splice data from file to a pipe
487 * @in: file to splice from
488 * @ppos: position in @in
489 * @pipe: pipe to splice to
490 * @len: number of bytes to splice
491 * @flags: splice modifier flags
494 * Will read pages from given file and fill them into a pipe. Can be
495 * used as long as the address_space operations for the source implements
499 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
500 struct pipe_inode_info *pipe, size_t len,
506 if (IS_DAX(in->f_mapping->host))
507 return default_file_splice_read(in, ppos, pipe, len, flags);
509 isize = i_size_read(in->f_mapping->host);
510 if (unlikely(*ppos >= isize))
513 left = isize - *ppos;
514 if (unlikely(left < len))
517 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
525 EXPORT_SYMBOL(generic_file_splice_read);
527 const struct pipe_buf_operations default_pipe_buf_ops = {
529 .confirm = generic_pipe_buf_confirm,
530 .release = generic_pipe_buf_release,
531 .steal = generic_pipe_buf_steal,
532 .get = generic_pipe_buf_get,
535 static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
536 struct pipe_buffer *buf)
541 /* Pipe buffer operations for a socket and similar. */
542 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
544 .confirm = generic_pipe_buf_confirm,
545 .release = generic_pipe_buf_release,
546 .steal = generic_pipe_buf_nosteal,
547 .get = generic_pipe_buf_get,
549 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
551 static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
552 unsigned long vlen, loff_t offset)
560 /* The cast to a user pointer is valid due to the set_fs() */
561 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
567 ssize_t kernel_write(struct file *file, const char *buf, size_t count,
575 /* The cast to a user pointer is valid due to the set_fs() */
576 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
581 EXPORT_SYMBOL(kernel_write);
583 ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
584 struct pipe_inode_info *pipe, size_t len,
587 unsigned int nr_pages;
588 unsigned int nr_freed;
590 struct page *pages[PIPE_DEF_BUFFERS];
591 struct partial_page partial[PIPE_DEF_BUFFERS];
592 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
597 struct splice_pipe_desc spd = {
600 .nr_pages_max = PIPE_DEF_BUFFERS,
602 .ops = &default_pipe_buf_ops,
603 .spd_release = spd_release_page,
606 if (splice_grow_spd(pipe, &spd))
611 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
612 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
617 offset = *ppos & ~PAGE_MASK;
618 nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
620 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
623 page = alloc_page(GFP_USER);
628 this_len = min_t(size_t, len, PAGE_SIZE - offset);
629 vec[i].iov_base = (void __user *) page_address(page);
630 vec[i].iov_len = this_len;
637 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
648 for (i = 0; i < spd.nr_pages; i++) {
649 this_len = min_t(size_t, vec[i].iov_len, res);
650 spd.partial[i].offset = 0;
651 spd.partial[i].len = this_len;
653 __free_page(spd.pages[i]);
659 spd.nr_pages -= nr_freed;
661 res = splice_to_pipe(pipe, &spd);
668 splice_shrink_spd(&spd);
672 for (i = 0; i < spd.nr_pages; i++)
673 __free_page(spd.pages[i]);
678 EXPORT_SYMBOL(default_file_splice_read);
681 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
682 * using sendpage(). Return the number of bytes sent.
684 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
685 struct pipe_buffer *buf, struct splice_desc *sd)
687 struct file *file = sd->u.file;
688 loff_t pos = sd->pos;
691 if (!likely(file->f_op->sendpage))
694 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
696 if (sd->len < sd->total_len && pipe->nrbufs > 1)
697 more |= MSG_SENDPAGE_NOTLAST;
699 return file->f_op->sendpage(file, buf->page, buf->offset,
700 sd->len, &pos, more);
703 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
706 if (waitqueue_active(&pipe->wait))
707 wake_up_interruptible(&pipe->wait);
708 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
712 * splice_from_pipe_feed - feed available data from a pipe to a file
713 * @pipe: pipe to splice from
714 * @sd: information to @actor
715 * @actor: handler that splices the data
718 * This function loops over the pipe and calls @actor to do the
719 * actual moving of a single struct pipe_buffer to the desired
720 * destination. It returns when there's no more buffers left in
721 * the pipe or if the requested number of bytes (@sd->total_len)
722 * have been copied. It returns a positive number (one) if the
723 * pipe needs to be filled with more data, zero if the required
724 * number of bytes have been copied and -errno on error.
726 * This, together with splice_from_pipe_{begin,end,next}, may be
727 * used to implement the functionality of __splice_from_pipe() when
728 * locking is required around copying the pipe buffers to the
731 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
736 while (pipe->nrbufs) {
737 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
738 const struct pipe_buf_operations *ops = buf->ops;
741 if (sd->len > sd->total_len)
742 sd->len = sd->total_len;
744 ret = buf->ops->confirm(pipe, buf);
751 ret = actor(pipe, buf, sd);
758 sd->num_spliced += ret;
761 sd->total_len -= ret;
765 ops->release(pipe, buf);
766 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
769 sd->need_wakeup = true;
780 * splice_from_pipe_next - wait for some data to splice from
781 * @pipe: pipe to splice from
782 * @sd: information about the splice operation
785 * This function will wait for some data and return a positive
786 * value (one) if pipe buffers are available. It will return zero
787 * or -errno if no more data needs to be spliced.
789 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
792 * Check for signal early to make process killable when there are
793 * always buffers available
795 if (signal_pending(current))
798 while (!pipe->nrbufs) {
802 if (!pipe->waiting_writers && sd->num_spliced)
805 if (sd->flags & SPLICE_F_NONBLOCK)
808 if (signal_pending(current))
811 if (sd->need_wakeup) {
812 wakeup_pipe_writers(pipe);
813 sd->need_wakeup = false;
823 * splice_from_pipe_begin - start splicing from pipe
824 * @sd: information about the splice operation
827 * This function should be called before a loop containing
828 * splice_from_pipe_next() and splice_from_pipe_feed() to
829 * initialize the necessary fields of @sd.
831 static void splice_from_pipe_begin(struct splice_desc *sd)
834 sd->need_wakeup = false;
838 * splice_from_pipe_end - finish splicing from pipe
839 * @pipe: pipe to splice from
840 * @sd: information about the splice operation
843 * This function will wake up pipe writers if necessary. It should
844 * be called after a loop containing splice_from_pipe_next() and
845 * splice_from_pipe_feed().
847 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
850 wakeup_pipe_writers(pipe);
854 * __splice_from_pipe - splice data from a pipe to given actor
855 * @pipe: pipe to splice from
856 * @sd: information to @actor
857 * @actor: handler that splices the data
860 * This function does little more than loop over the pipe and call
861 * @actor to do the actual moving of a single struct pipe_buffer to
862 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
866 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
871 splice_from_pipe_begin(sd);
874 ret = splice_from_pipe_next(pipe, sd);
876 ret = splice_from_pipe_feed(pipe, sd, actor);
878 splice_from_pipe_end(pipe, sd);
880 return sd->num_spliced ? sd->num_spliced : ret;
882 EXPORT_SYMBOL(__splice_from_pipe);
885 * splice_from_pipe - splice data from a pipe to a file
886 * @pipe: pipe to splice from
887 * @out: file to splice to
888 * @ppos: position in @out
889 * @len: how many bytes to splice
890 * @flags: splice modifier flags
891 * @actor: handler that splices the data
894 * See __splice_from_pipe. This function locks the pipe inode,
895 * otherwise it's identical to __splice_from_pipe().
898 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
899 loff_t *ppos, size_t len, unsigned int flags,
903 struct splice_desc sd = {
911 ret = __splice_from_pipe(pipe, &sd, actor);
918 * iter_file_splice_write - splice data from a pipe to a file
920 * @out: file to write to
921 * @ppos: position in @out
922 * @len: number of bytes to splice
923 * @flags: splice modifier flags
926 * Will either move or copy pages (determined by @flags options) from
927 * the given pipe inode to the given file.
928 * This one is ->write_iter-based.
932 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
933 loff_t *ppos, size_t len, unsigned int flags)
935 struct splice_desc sd = {
941 int nbufs = pipe->buffers;
942 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
946 if (unlikely(!array))
951 splice_from_pipe_begin(&sd);
952 while (sd.total_len) {
953 struct iov_iter from;
957 ret = splice_from_pipe_next(pipe, &sd);
961 if (unlikely(nbufs < pipe->buffers)) {
963 nbufs = pipe->buffers;
964 array = kcalloc(nbufs, sizeof(struct bio_vec),
972 /* build the vector */
974 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
975 struct pipe_buffer *buf = pipe->bufs + idx;
976 size_t this_len = buf->len;
981 if (idx == pipe->buffers - 1)
984 ret = buf->ops->confirm(pipe, buf);
991 array[n].bv_page = buf->page;
992 array[n].bv_len = this_len;
993 array[n].bv_offset = buf->offset;
997 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
998 sd.total_len - left);
999 ret = vfs_iter_write(out, &from, &sd.pos);
1003 sd.num_spliced += ret;
1004 sd.total_len -= ret;
1007 /* dismiss the fully eaten buffers, adjust the partial one */
1009 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
1010 if (ret >= buf->len) {
1011 const struct pipe_buf_operations *ops = buf->ops;
1015 ops->release(pipe, buf);
1016 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1019 sd.need_wakeup = true;
1029 splice_from_pipe_end(pipe, &sd);
1034 ret = sd.num_spliced;
1039 EXPORT_SYMBOL(iter_file_splice_write);
1041 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1042 struct splice_desc *sd)
1046 loff_t tmp = sd->pos;
1048 data = kmap(buf->page);
1049 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
1055 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
1056 struct file *out, loff_t *ppos,
1057 size_t len, unsigned int flags)
1061 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1069 * generic_splice_sendpage - splice data from a pipe to a socket
1070 * @pipe: pipe to splice from
1071 * @out: socket to write to
1072 * @ppos: position in @out
1073 * @len: number of bytes to splice
1074 * @flags: splice modifier flags
1077 * Will send @len bytes from the pipe to a network socket. No data copying
1081 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
1082 loff_t *ppos, size_t len, unsigned int flags)
1084 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
1087 EXPORT_SYMBOL(generic_splice_sendpage);
1090 * Attempt to initiate a splice from pipe to file.
1092 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1093 loff_t *ppos, size_t len, unsigned int flags)
1095 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1096 loff_t *, size_t, unsigned int);
1098 if (out->f_op->splice_write)
1099 splice_write = out->f_op->splice_write;
1101 splice_write = default_file_splice_write;
1103 return splice_write(pipe, out, ppos, len, flags);
1107 * Attempt to initiate a splice from a file to a pipe.
1109 static long do_splice_to(struct file *in, loff_t *ppos,
1110 struct pipe_inode_info *pipe, size_t len,
1113 ssize_t (*splice_read)(struct file *, loff_t *,
1114 struct pipe_inode_info *, size_t, unsigned int);
1117 if (unlikely(!(in->f_mode & FMODE_READ)))
1120 ret = rw_verify_area(READ, in, ppos, len);
1121 if (unlikely(ret < 0))
1124 if (unlikely(len > MAX_RW_COUNT))
1127 if (in->f_op->splice_read)
1128 splice_read = in->f_op->splice_read;
1130 splice_read = default_file_splice_read;
1132 return splice_read(in, ppos, pipe, len, flags);
1136 * splice_direct_to_actor - splices data directly between two non-pipes
1137 * @in: file to splice from
1138 * @sd: actor information on where to splice to
1139 * @actor: handles the data splicing
1142 * This is a special case helper to splice directly between two
1143 * points, without requiring an explicit pipe. Internally an allocated
1144 * pipe is cached in the process, and reused during the lifetime of
1148 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1149 splice_direct_actor *actor)
1151 struct pipe_inode_info *pipe;
1158 * We require the input being a regular file, as we don't want to
1159 * randomly drop data for eg socket -> socket splicing. Use the
1160 * piped splicing for that!
1162 i_mode = file_inode(in)->i_mode;
1163 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1167 * neither in nor out is a pipe, setup an internal pipe attached to
1168 * 'out' and transfer the wanted data from 'in' to 'out' through that
1170 pipe = current->splice_pipe;
1171 if (unlikely(!pipe)) {
1172 pipe = alloc_pipe_info();
1177 * We don't have an immediate reader, but we'll read the stuff
1178 * out of the pipe right after the splice_to_pipe(). So set
1179 * PIPE_READERS appropriately.
1183 current->splice_pipe = pipe;
1191 len = sd->total_len;
1195 * Don't block on output, we have to drain the direct pipe.
1197 sd->flags &= ~SPLICE_F_NONBLOCK;
1198 more = sd->flags & SPLICE_F_MORE;
1202 loff_t pos = sd->pos, prev_pos = pos;
1204 ret = do_splice_to(in, &pos, pipe, len, flags);
1205 if (unlikely(ret <= 0))
1209 sd->total_len = read_len;
1212 * If more data is pending, set SPLICE_F_MORE
1213 * If this is the last data and SPLICE_F_MORE was not set
1214 * initially, clears it.
1217 sd->flags |= SPLICE_F_MORE;
1219 sd->flags &= ~SPLICE_F_MORE;
1221 * NOTE: nonblocking mode only applies to the input. We
1222 * must not do the output in nonblocking mode as then we
1223 * could get stuck data in the internal pipe:
1225 ret = actor(pipe, sd);
1226 if (unlikely(ret <= 0)) {
1235 if (ret < read_len) {
1236 sd->pos = prev_pos + ret;
1242 pipe->nrbufs = pipe->curbuf = 0;
1248 * If we did an incomplete transfer we must release
1249 * the pipe buffers in question:
1251 for (i = 0; i < pipe->buffers; i++) {
1252 struct pipe_buffer *buf = pipe->bufs + i;
1255 buf->ops->release(pipe, buf);
1265 EXPORT_SYMBOL(splice_direct_to_actor);
1267 static int direct_splice_actor(struct pipe_inode_info *pipe,
1268 struct splice_desc *sd)
1270 struct file *file = sd->u.file;
1272 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1277 * do_splice_direct - splices data directly between two files
1278 * @in: file to splice from
1279 * @ppos: input file offset
1280 * @out: file to splice to
1281 * @opos: output file offset
1282 * @len: number of bytes to splice
1283 * @flags: splice modifier flags
1286 * For use by do_sendfile(). splice can easily emulate sendfile, but
1287 * doing it in the application would incur an extra system call
1288 * (splice in + splice out, as compared to just sendfile()). So this helper
1289 * can splice directly through a process-private pipe.
1292 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1293 loff_t *opos, size_t len, unsigned int flags)
1295 struct splice_desc sd = {
1305 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1308 if (unlikely(out->f_flags & O_APPEND))
1311 ret = rw_verify_area(WRITE, out, opos, len);
1312 if (unlikely(ret < 0))
1315 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1321 EXPORT_SYMBOL(do_splice_direct);
1323 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1325 while (pipe->nrbufs == pipe->buffers) {
1326 if (flags & SPLICE_F_NONBLOCK)
1328 if (signal_pending(current))
1329 return -ERESTARTSYS;
1330 pipe->waiting_writers++;
1332 pipe->waiting_writers--;
1337 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1338 struct pipe_inode_info *opipe,
1339 size_t len, unsigned int flags);
1342 * Determine where to splice to/from.
1344 static long do_splice(struct file *in, loff_t __user *off_in,
1345 struct file *out, loff_t __user *off_out,
1346 size_t len, unsigned int flags)
1348 struct pipe_inode_info *ipipe;
1349 struct pipe_inode_info *opipe;
1353 ipipe = get_pipe_info(in);
1354 opipe = get_pipe_info(out);
1356 if (ipipe && opipe) {
1357 if (off_in || off_out)
1360 if (!(in->f_mode & FMODE_READ))
1363 if (!(out->f_mode & FMODE_WRITE))
1366 /* Splicing to self would be fun, but... */
1370 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1377 if (!(out->f_mode & FMODE_PWRITE))
1379 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1382 offset = out->f_pos;
1385 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1388 if (unlikely(out->f_flags & O_APPEND))
1391 ret = rw_verify_area(WRITE, out, &offset, len);
1392 if (unlikely(ret < 0))
1395 file_start_write(out);
1396 ret = do_splice_from(ipipe, out, &offset, len, flags);
1397 file_end_write(out);
1400 out->f_pos = offset;
1401 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1411 if (!(in->f_mode & FMODE_PREAD))
1413 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1420 ret = wait_for_space(opipe, flags);
1422 ret = do_splice_to(in, &offset, opipe, len, flags);
1425 wakeup_pipe_readers(opipe);
1428 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1437 static int iter_to_pipe(struct iov_iter *from,
1438 struct pipe_inode_info *pipe,
1441 struct pipe_buffer buf = {
1442 .ops = &user_page_pipe_buf_ops,
1447 bool failed = false;
1449 while (iov_iter_count(from) && !failed) {
1450 struct page *pages[16];
1455 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1461 for (n = 0; copied; n++, start = 0) {
1462 int size = min_t(int, copied, PAGE_SIZE - start);
1464 buf.page = pages[n];
1467 ret = add_to_pipe(pipe, &buf);
1468 if (unlikely(ret < 0)) {
1471 iov_iter_advance(from, ret);
1480 return total ? total : ret;
1483 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1484 struct splice_desc *sd)
1486 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1487 return n == sd->len ? n : -EFAULT;
1491 * For lack of a better implementation, implement vmsplice() to userspace
1492 * as a simple copy of the pipes pages to the user iov.
1494 static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
1495 unsigned long nr_segs, unsigned int flags)
1497 struct pipe_inode_info *pipe;
1498 struct splice_desc sd;
1500 struct iovec iovstack[UIO_FASTIOV];
1501 struct iovec *iov = iovstack;
1502 struct iov_iter iter;
1504 pipe = get_pipe_info(file);
1508 ret = import_iovec(READ, uiov, nr_segs,
1509 ARRAY_SIZE(iovstack), &iov, &iter);
1513 sd.total_len = iov_iter_count(&iter);
1521 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1530 * vmsplice splices a user address range into a pipe. It can be thought of
1531 * as splice-from-memory, where the regular splice is splice-from-file (or
1532 * to file). In both cases the output is a pipe, naturally.
1534 static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1535 unsigned long nr_segs, unsigned int flags)
1537 struct pipe_inode_info *pipe;
1538 struct iovec iovstack[UIO_FASTIOV];
1539 struct iovec *iov = iovstack;
1540 struct iov_iter from;
1542 unsigned buf_flag = 0;
1544 if (flags & SPLICE_F_GIFT)
1545 buf_flag = PIPE_BUF_FLAG_GIFT;
1547 pipe = get_pipe_info(file);
1551 ret = import_iovec(WRITE, uiov, nr_segs,
1552 ARRAY_SIZE(iovstack), &iov, &from);
1557 ret = wait_for_space(pipe, flags);
1559 ret = iter_to_pipe(&from, pipe, buf_flag);
1562 wakeup_pipe_readers(pipe);
1568 * Note that vmsplice only really supports true splicing _from_ user memory
1569 * to a pipe, not the other way around. Splicing from user memory is a simple
1570 * operation that can be supported without any funky alignment restrictions
1571 * or nasty vm tricks. We simply map in the user memory and fill them into
1572 * a pipe. The reverse isn't quite as easy, though. There are two possible
1573 * solutions for that:
1575 * - memcpy() the data internally, at which point we might as well just
1576 * do a regular read() on the buffer anyway.
1577 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1578 * has restriction limitations on both ends of the pipe).
1580 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1583 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1584 unsigned long, nr_segs, unsigned int, flags)
1589 if (unlikely(nr_segs > UIO_MAXIOV))
1591 else if (unlikely(!nr_segs))
1597 if (f.file->f_mode & FMODE_WRITE)
1598 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1599 else if (f.file->f_mode & FMODE_READ)
1600 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1608 #ifdef CONFIG_COMPAT
1609 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1610 unsigned int, nr_segs, unsigned int, flags)
1613 struct iovec __user *iov;
1614 if (nr_segs > UIO_MAXIOV)
1616 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1617 for (i = 0; i < nr_segs; i++) {
1618 struct compat_iovec v;
1619 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1620 get_user(v.iov_len, &iov32[i].iov_len) ||
1621 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1622 put_user(v.iov_len, &iov[i].iov_len))
1625 return sys_vmsplice(fd, iov, nr_segs, flags);
1629 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1630 int, fd_out, loff_t __user *, off_out,
1631 size_t, len, unsigned int, flags)
1642 if (in.file->f_mode & FMODE_READ) {
1643 out = fdget(fd_out);
1645 if (out.file->f_mode & FMODE_WRITE)
1646 error = do_splice(in.file, off_in,
1658 * Make sure there's data to read. Wait for input if we can, otherwise
1659 * return an appropriate error.
1661 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1666 * Check ->nrbufs without the inode lock first. This function
1667 * is speculative anyways, so missing one is ok.
1675 while (!pipe->nrbufs) {
1676 if (signal_pending(current)) {
1682 if (!pipe->waiting_writers) {
1683 if (flags & SPLICE_F_NONBLOCK) {
1696 * Make sure there's writeable room. Wait for room if we can, otherwise
1697 * return an appropriate error.
1699 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1704 * Check ->nrbufs without the inode lock first. This function
1705 * is speculative anyways, so missing one is ok.
1707 if (pipe->nrbufs < pipe->buffers)
1713 while (pipe->nrbufs >= pipe->buffers) {
1714 if (!pipe->readers) {
1715 send_sig(SIGPIPE, current, 0);
1719 if (flags & SPLICE_F_NONBLOCK) {
1723 if (signal_pending(current)) {
1727 pipe->waiting_writers++;
1729 pipe->waiting_writers--;
1737 * Splice contents of ipipe to opipe.
1739 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1740 struct pipe_inode_info *opipe,
1741 size_t len, unsigned int flags)
1743 struct pipe_buffer *ibuf, *obuf;
1745 bool input_wakeup = false;
1749 ret = ipipe_prep(ipipe, flags);
1753 ret = opipe_prep(opipe, flags);
1758 * Potential ABBA deadlock, work around it by ordering lock
1759 * grabbing by pipe info address. Otherwise two different processes
1760 * could deadlock (one doing tee from A -> B, the other from B -> A).
1762 pipe_double_lock(ipipe, opipe);
1765 if (!opipe->readers) {
1766 send_sig(SIGPIPE, current, 0);
1772 if (!ipipe->nrbufs && !ipipe->writers)
1776 * Cannot make any progress, because either the input
1777 * pipe is empty or the output pipe is full.
1779 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
1780 /* Already processed some buffers, break */
1784 if (flags & SPLICE_F_NONBLOCK) {
1790 * We raced with another reader/writer and haven't
1791 * managed to process any buffers. A zero return
1792 * value means EOF, so retry instead.
1799 ibuf = ipipe->bufs + ipipe->curbuf;
1800 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1801 obuf = opipe->bufs + nbuf;
1803 if (len >= ibuf->len) {
1805 * Simply move the whole buffer from ipipe to opipe
1810 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
1812 input_wakeup = true;
1815 * Get a reference to this pipe buffer,
1816 * so we can copy the contents over.
1818 ibuf->ops->get(ipipe, ibuf);
1822 * Don't inherit the gift flag, we need to
1823 * prevent multiple steals of this page.
1825 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1829 ibuf->offset += obuf->len;
1830 ibuf->len -= obuf->len;
1840 * If we put data in the output pipe, wakeup any potential readers.
1843 wakeup_pipe_readers(opipe);
1846 wakeup_pipe_writers(ipipe);
1852 * Link contents of ipipe to opipe.
1854 static int link_pipe(struct pipe_inode_info *ipipe,
1855 struct pipe_inode_info *opipe,
1856 size_t len, unsigned int flags)
1858 struct pipe_buffer *ibuf, *obuf;
1859 int ret = 0, i = 0, nbuf;
1862 * Potential ABBA deadlock, work around it by ordering lock
1863 * grabbing by pipe info address. Otherwise two different processes
1864 * could deadlock (one doing tee from A -> B, the other from B -> A).
1866 pipe_double_lock(ipipe, opipe);
1869 if (!opipe->readers) {
1870 send_sig(SIGPIPE, current, 0);
1877 * If we have iterated all input buffers or ran out of
1878 * output room, break.
1880 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1883 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1884 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1887 * Get a reference to this pipe buffer,
1888 * so we can copy the contents over.
1890 ibuf->ops->get(ipipe, ibuf);
1892 obuf = opipe->bufs + nbuf;
1896 * Don't inherit the gift flag, we need to
1897 * prevent multiple steals of this page.
1899 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1901 if (obuf->len > len)
1911 * return EAGAIN if we have the potential of some data in the
1912 * future, otherwise just return 0
1914 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1921 * If we put data in the output pipe, wakeup any potential readers.
1924 wakeup_pipe_readers(opipe);
1930 * This is a tee(1) implementation that works on pipes. It doesn't copy
1931 * any data, it simply references the 'in' pages on the 'out' pipe.
1932 * The 'flags' used are the SPLICE_F_* variants, currently the only
1933 * applicable one is SPLICE_F_NONBLOCK.
1935 static long do_tee(struct file *in, struct file *out, size_t len,
1938 struct pipe_inode_info *ipipe = get_pipe_info(in);
1939 struct pipe_inode_info *opipe = get_pipe_info(out);
1943 * Duplicate the contents of ipipe to opipe without actually
1946 if (ipipe && opipe && ipipe != opipe) {
1948 * Keep going, unless we encounter an error. The ipipe/opipe
1949 * ordering doesn't really matter.
1951 ret = ipipe_prep(ipipe, flags);
1953 ret = opipe_prep(opipe, flags);
1955 ret = link_pipe(ipipe, opipe, len, flags);
1962 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1973 if (in.file->f_mode & FMODE_READ) {
1974 struct fd out = fdget(fdout);
1976 if (out.file->f_mode & FMODE_WRITE)
1977 error = do_tee(in.file, out.file,