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)
91 page_cache_release(buf->page);
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, do_wakeup, page_nr;
195 if (!pipe->readers) {
196 send_sig(SIGPIPE, current, 0);
202 if (pipe->nrbufs < pipe->buffers) {
203 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
204 struct pipe_buffer *buf = pipe->bufs + newbuf;
206 buf->page = spd->pages[page_nr];
207 buf->offset = spd->partial[page_nr].offset;
208 buf->len = spd->partial[page_nr].len;
209 buf->private = spd->partial[page_nr].private;
211 if (spd->flags & SPLICE_F_GIFT)
212 buf->flags |= PIPE_BUF_FLAG_GIFT;
221 if (!--spd->nr_pages)
223 if (pipe->nrbufs < pipe->buffers)
229 if (spd->flags & SPLICE_F_NONBLOCK) {
235 if (signal_pending(current)) {
243 if (waitqueue_active(&pipe->wait))
244 wake_up_interruptible_sync(&pipe->wait);
245 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
249 pipe->waiting_writers++;
251 pipe->waiting_writers--;
257 wakeup_pipe_readers(pipe);
259 while (page_nr < spd_pages)
260 spd->spd_release(spd, page_nr++);
265 void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
267 page_cache_release(spd->pages[i]);
271 * Check if we need to grow the arrays holding pages and partial page
274 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
276 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
278 spd->nr_pages_max = buffers;
279 if (buffers <= PIPE_DEF_BUFFERS)
282 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
283 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
285 if (spd->pages && spd->partial)
293 void splice_shrink_spd(struct splice_pipe_desc *spd)
295 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
303 __generic_file_splice_read(struct file *in, loff_t *ppos,
304 struct pipe_inode_info *pipe, size_t len,
307 struct address_space *mapping = in->f_mapping;
308 unsigned int loff, nr_pages, req_pages;
309 struct page *pages[PIPE_DEF_BUFFERS];
310 struct partial_page partial[PIPE_DEF_BUFFERS];
312 pgoff_t index, end_index;
315 struct splice_pipe_desc spd = {
318 .nr_pages_max = PIPE_DEF_BUFFERS,
320 .ops = &page_cache_pipe_buf_ops,
321 .spd_release = spd_release_page,
324 if (splice_grow_spd(pipe, &spd))
327 index = *ppos >> PAGE_CACHE_SHIFT;
328 loff = *ppos & ~PAGE_CACHE_MASK;
329 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
330 nr_pages = min(req_pages, spd.nr_pages_max);
333 * Lookup the (hopefully) full range of pages we need.
335 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
336 index += spd.nr_pages;
339 * If find_get_pages_contig() returned fewer pages than we needed,
340 * readahead/allocate the rest and fill in the holes.
342 if (spd.nr_pages < nr_pages)
343 page_cache_sync_readahead(mapping, &in->f_ra, in,
344 index, req_pages - spd.nr_pages);
347 while (spd.nr_pages < nr_pages) {
349 * Page could be there, find_get_pages_contig() breaks on
352 page = find_get_page(mapping, index);
355 * page didn't exist, allocate one.
357 page = page_cache_alloc_cold(mapping);
361 error = add_to_page_cache_lru(page, mapping, index,
363 if (unlikely(error)) {
364 page_cache_release(page);
365 if (error == -EEXIST)
370 * add_to_page_cache() locks the page, unlock it
371 * to avoid convoluting the logic below even more.
376 spd.pages[spd.nr_pages++] = page;
381 * Now loop over the map and see if we need to start IO on any
382 * pages, fill in the partial map, etc.
384 index = *ppos >> PAGE_CACHE_SHIFT;
385 nr_pages = spd.nr_pages;
387 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
388 unsigned int this_len;
394 * this_len is the max we'll use from this page
396 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
397 page = spd.pages[page_nr];
399 if (PageReadahead(page))
400 page_cache_async_readahead(mapping, &in->f_ra, in,
401 page, index, req_pages - page_nr);
404 * If the page isn't uptodate, we may need to start io on it
406 if (!PageUptodate(page)) {
410 * Page was truncated, or invalidated by the
411 * filesystem. Redo the find/create, but this time the
412 * page is kept locked, so there's no chance of another
413 * race with truncate/invalidate.
415 if (!page->mapping) {
417 page = find_or_create_page(mapping, index,
418 mapping_gfp_mask(mapping));
424 page_cache_release(spd.pages[page_nr]);
425 spd.pages[page_nr] = page;
428 * page was already under io and is now done, great
430 if (PageUptodate(page)) {
436 * need to read in the page
438 error = mapping->a_ops->readpage(in, page);
439 if (unlikely(error)) {
441 * We really should re-lookup the page here,
442 * but it complicates things a lot. Instead
443 * lets just do what we already stored, and
444 * we'll get it the next time we are called.
446 if (error == AOP_TRUNCATED_PAGE)
454 * i_size must be checked after PageUptodate.
456 isize = i_size_read(mapping->host);
457 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
458 if (unlikely(!isize || index > end_index))
462 * if this is the last page, see if we need to shrink
463 * the length and stop
465 if (end_index == index) {
469 * max good bytes in this page
471 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
476 * force quit after adding this page
478 this_len = min(this_len, plen - loff);
482 spd.partial[page_nr].offset = loff;
483 spd.partial[page_nr].len = this_len;
491 * Release any pages at the end, if we quit early. 'page_nr' is how far
492 * we got, 'nr_pages' is how many pages are in the map.
494 while (page_nr < nr_pages)
495 page_cache_release(spd.pages[page_nr++]);
496 in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
499 error = splice_to_pipe(pipe, &spd);
501 splice_shrink_spd(&spd);
506 * generic_file_splice_read - splice data from file to a pipe
507 * @in: file to splice from
508 * @ppos: position in @in
509 * @pipe: pipe to splice to
510 * @len: number of bytes to splice
511 * @flags: splice modifier flags
514 * Will read pages from given file and fill them into a pipe. Can be
515 * used as long as the address_space operations for the source implements
519 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
520 struct pipe_inode_info *pipe, size_t len,
526 isize = i_size_read(in->f_mapping->host);
527 if (unlikely(*ppos >= isize))
530 left = isize - *ppos;
531 if (unlikely(left < len))
534 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
542 EXPORT_SYMBOL(generic_file_splice_read);
544 static const struct pipe_buf_operations default_pipe_buf_ops = {
546 .confirm = generic_pipe_buf_confirm,
547 .release = generic_pipe_buf_release,
548 .steal = generic_pipe_buf_steal,
549 .get = generic_pipe_buf_get,
552 static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
553 struct pipe_buffer *buf)
558 /* Pipe buffer operations for a socket and similar. */
559 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
561 .confirm = generic_pipe_buf_confirm,
562 .release = generic_pipe_buf_release,
563 .steal = generic_pipe_buf_nosteal,
564 .get = generic_pipe_buf_get,
566 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
568 static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
569 unsigned long vlen, loff_t offset)
577 /* The cast to a user pointer is valid due to the set_fs() */
578 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
584 ssize_t kernel_write(struct file *file, const char *buf, size_t count,
592 /* The cast to a user pointer is valid due to the set_fs() */
593 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
598 EXPORT_SYMBOL(kernel_write);
600 ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
601 struct pipe_inode_info *pipe, size_t len,
604 unsigned int nr_pages;
605 unsigned int nr_freed;
607 struct page *pages[PIPE_DEF_BUFFERS];
608 struct partial_page partial[PIPE_DEF_BUFFERS];
609 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
614 struct splice_pipe_desc spd = {
617 .nr_pages_max = PIPE_DEF_BUFFERS,
619 .ops = &default_pipe_buf_ops,
620 .spd_release = spd_release_page,
623 if (splice_grow_spd(pipe, &spd))
628 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
629 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
634 offset = *ppos & ~PAGE_CACHE_MASK;
635 nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
637 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
640 page = alloc_page(GFP_USER);
645 this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
646 vec[i].iov_base = (void __user *) page_address(page);
647 vec[i].iov_len = this_len;
654 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
665 for (i = 0; i < spd.nr_pages; i++) {
666 this_len = min_t(size_t, vec[i].iov_len, res);
667 spd.partial[i].offset = 0;
668 spd.partial[i].len = this_len;
670 __free_page(spd.pages[i]);
676 spd.nr_pages -= nr_freed;
678 res = splice_to_pipe(pipe, &spd);
685 splice_shrink_spd(&spd);
689 for (i = 0; i < spd.nr_pages; i++)
690 __free_page(spd.pages[i]);
695 EXPORT_SYMBOL(default_file_splice_read);
698 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
699 * using sendpage(). Return the number of bytes sent.
701 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
702 struct pipe_buffer *buf, struct splice_desc *sd)
704 struct file *file = sd->u.file;
705 loff_t pos = sd->pos;
708 if (!likely(file->f_op->sendpage))
711 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
713 if (sd->len < sd->total_len && pipe->nrbufs > 1)
714 more |= MSG_SENDPAGE_NOTLAST;
716 return file->f_op->sendpage(file, buf->page, buf->offset,
717 sd->len, &pos, more);
720 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
723 if (waitqueue_active(&pipe->wait))
724 wake_up_interruptible(&pipe->wait);
725 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
729 * splice_from_pipe_feed - feed available data from a pipe to a file
730 * @pipe: pipe to splice from
731 * @sd: information to @actor
732 * @actor: handler that splices the data
735 * This function loops over the pipe and calls @actor to do the
736 * actual moving of a single struct pipe_buffer to the desired
737 * destination. It returns when there's no more buffers left in
738 * the pipe or if the requested number of bytes (@sd->total_len)
739 * have been copied. It returns a positive number (one) if the
740 * pipe needs to be filled with more data, zero if the required
741 * number of bytes have been copied and -errno on error.
743 * This, together with splice_from_pipe_{begin,end,next}, may be
744 * used to implement the functionality of __splice_from_pipe() when
745 * locking is required around copying the pipe buffers to the
748 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
753 while (pipe->nrbufs) {
754 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
755 const struct pipe_buf_operations *ops = buf->ops;
758 if (sd->len > sd->total_len)
759 sd->len = sd->total_len;
761 ret = buf->ops->confirm(pipe, buf);
768 ret = actor(pipe, buf, sd);
775 sd->num_spliced += ret;
778 sd->total_len -= ret;
782 ops->release(pipe, buf);
783 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
786 sd->need_wakeup = true;
797 * splice_from_pipe_next - wait for some data to splice from
798 * @pipe: pipe to splice from
799 * @sd: information about the splice operation
802 * This function will wait for some data and return a positive
803 * value (one) if pipe buffers are available. It will return zero
804 * or -errno if no more data needs to be spliced.
806 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
808 while (!pipe->nrbufs) {
812 if (!pipe->waiting_writers && sd->num_spliced)
815 if (sd->flags & SPLICE_F_NONBLOCK)
818 if (signal_pending(current))
821 if (sd->need_wakeup) {
822 wakeup_pipe_writers(pipe);
823 sd->need_wakeup = false;
833 * splice_from_pipe_begin - start splicing from pipe
834 * @sd: information about the splice operation
837 * This function should be called before a loop containing
838 * splice_from_pipe_next() and splice_from_pipe_feed() to
839 * initialize the necessary fields of @sd.
841 static void splice_from_pipe_begin(struct splice_desc *sd)
844 sd->need_wakeup = false;
848 * splice_from_pipe_end - finish splicing from pipe
849 * @pipe: pipe to splice from
850 * @sd: information about the splice operation
853 * This function will wake up pipe writers if necessary. It should
854 * be called after a loop containing splice_from_pipe_next() and
855 * splice_from_pipe_feed().
857 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
860 wakeup_pipe_writers(pipe);
864 * __splice_from_pipe - splice data from a pipe to given actor
865 * @pipe: pipe to splice from
866 * @sd: information to @actor
867 * @actor: handler that splices the data
870 * This function does little more than loop over the pipe and call
871 * @actor to do the actual moving of a single struct pipe_buffer to
872 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
876 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
881 splice_from_pipe_begin(sd);
883 ret = splice_from_pipe_next(pipe, sd);
885 ret = splice_from_pipe_feed(pipe, sd, actor);
887 splice_from_pipe_end(pipe, sd);
889 return sd->num_spliced ? sd->num_spliced : ret;
891 EXPORT_SYMBOL(__splice_from_pipe);
894 * splice_from_pipe - splice data from a pipe to a file
895 * @pipe: pipe to splice from
896 * @out: file to splice to
897 * @ppos: position in @out
898 * @len: how many bytes to splice
899 * @flags: splice modifier flags
900 * @actor: handler that splices the data
903 * See __splice_from_pipe. This function locks the pipe inode,
904 * otherwise it's identical to __splice_from_pipe().
907 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
908 loff_t *ppos, size_t len, unsigned int flags,
912 struct splice_desc sd = {
920 ret = __splice_from_pipe(pipe, &sd, actor);
927 * iter_file_splice_write - splice data from a pipe to a file
929 * @out: file to write to
930 * @ppos: position in @out
931 * @len: number of bytes to splice
932 * @flags: splice modifier flags
935 * Will either move or copy pages (determined by @flags options) from
936 * the given pipe inode to the given file.
937 * This one is ->write_iter-based.
941 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
942 loff_t *ppos, size_t len, unsigned int flags)
944 struct splice_desc sd = {
950 int nbufs = pipe->buffers;
951 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
955 if (unlikely(!array))
960 splice_from_pipe_begin(&sd);
961 while (sd.total_len) {
962 struct iov_iter from;
966 ret = splice_from_pipe_next(pipe, &sd);
970 if (unlikely(nbufs < pipe->buffers)) {
972 nbufs = pipe->buffers;
973 array = kcalloc(nbufs, sizeof(struct bio_vec),
981 /* build the vector */
983 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
984 struct pipe_buffer *buf = pipe->bufs + idx;
985 size_t this_len = buf->len;
990 if (idx == pipe->buffers - 1)
993 ret = buf->ops->confirm(pipe, buf);
1000 array[n].bv_page = buf->page;
1001 array[n].bv_len = this_len;
1002 array[n].bv_offset = buf->offset;
1006 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
1007 sd.total_len - left);
1008 ret = vfs_iter_write(out, &from, &sd.pos);
1012 sd.num_spliced += ret;
1013 sd.total_len -= ret;
1016 /* dismiss the fully eaten buffers, adjust the partial one */
1018 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
1019 if (ret >= buf->len) {
1020 const struct pipe_buf_operations *ops = buf->ops;
1024 ops->release(pipe, buf);
1025 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1028 sd.need_wakeup = true;
1038 splice_from_pipe_end(pipe, &sd);
1043 ret = sd.num_spliced;
1048 EXPORT_SYMBOL(iter_file_splice_write);
1050 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1051 struct splice_desc *sd)
1055 loff_t tmp = sd->pos;
1057 data = kmap(buf->page);
1058 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
1064 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
1065 struct file *out, loff_t *ppos,
1066 size_t len, unsigned int flags)
1070 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1078 * generic_splice_sendpage - splice data from a pipe to a socket
1079 * @pipe: pipe to splice from
1080 * @out: socket to write to
1081 * @ppos: position in @out
1082 * @len: number of bytes to splice
1083 * @flags: splice modifier flags
1086 * Will send @len bytes from the pipe to a network socket. No data copying
1090 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
1091 loff_t *ppos, size_t len, unsigned int flags)
1093 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
1096 EXPORT_SYMBOL(generic_splice_sendpage);
1099 * Attempt to initiate a splice from pipe to file.
1101 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1102 loff_t *ppos, size_t len, unsigned int flags)
1104 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1105 loff_t *, size_t, unsigned int);
1107 if (out->f_op->splice_write)
1108 splice_write = out->f_op->splice_write;
1110 splice_write = default_file_splice_write;
1112 return splice_write(pipe, out, ppos, len, flags);
1116 * Attempt to initiate a splice from a file to a pipe.
1118 static long do_splice_to(struct file *in, loff_t *ppos,
1119 struct pipe_inode_info *pipe, size_t len,
1122 ssize_t (*splice_read)(struct file *, loff_t *,
1123 struct pipe_inode_info *, size_t, unsigned int);
1126 if (unlikely(!(in->f_mode & FMODE_READ)))
1129 ret = rw_verify_area(READ, in, ppos, len);
1130 if (unlikely(ret < 0))
1133 if (in->f_op->splice_read)
1134 splice_read = in->f_op->splice_read;
1136 splice_read = default_file_splice_read;
1138 return splice_read(in, ppos, pipe, len, flags);
1142 * splice_direct_to_actor - splices data directly between two non-pipes
1143 * @in: file to splice from
1144 * @sd: actor information on where to splice to
1145 * @actor: handles the data splicing
1148 * This is a special case helper to splice directly between two
1149 * points, without requiring an explicit pipe. Internally an allocated
1150 * pipe is cached in the process, and reused during the lifetime of
1154 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1155 splice_direct_actor *actor)
1157 struct pipe_inode_info *pipe;
1164 * We require the input being a regular file, as we don't want to
1165 * randomly drop data for eg socket -> socket splicing. Use the
1166 * piped splicing for that!
1168 i_mode = file_inode(in)->i_mode;
1169 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1173 * neither in nor out is a pipe, setup an internal pipe attached to
1174 * 'out' and transfer the wanted data from 'in' to 'out' through that
1176 pipe = current->splice_pipe;
1177 if (unlikely(!pipe)) {
1178 pipe = alloc_pipe_info();
1183 * We don't have an immediate reader, but we'll read the stuff
1184 * out of the pipe right after the splice_to_pipe(). So set
1185 * PIPE_READERS appropriately.
1189 current->splice_pipe = pipe;
1197 len = sd->total_len;
1201 * Don't block on output, we have to drain the direct pipe.
1203 sd->flags &= ~SPLICE_F_NONBLOCK;
1207 loff_t pos = sd->pos, prev_pos = pos;
1209 ret = do_splice_to(in, &pos, pipe, len, flags);
1210 if (unlikely(ret <= 0))
1214 sd->total_len = read_len;
1217 * NOTE: nonblocking mode only applies to the input. We
1218 * must not do the output in nonblocking mode as then we
1219 * could get stuck data in the internal pipe:
1221 ret = actor(pipe, sd);
1222 if (unlikely(ret <= 0)) {
1231 if (ret < read_len) {
1232 sd->pos = prev_pos + ret;
1238 pipe->nrbufs = pipe->curbuf = 0;
1244 * If we did an incomplete transfer we must release
1245 * the pipe buffers in question:
1247 for (i = 0; i < pipe->buffers; i++) {
1248 struct pipe_buffer *buf = pipe->bufs + i;
1251 buf->ops->release(pipe, buf);
1261 EXPORT_SYMBOL(splice_direct_to_actor);
1263 static int direct_splice_actor(struct pipe_inode_info *pipe,
1264 struct splice_desc *sd)
1266 struct file *file = sd->u.file;
1268 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1273 * do_splice_direct - splices data directly between two files
1274 * @in: file to splice from
1275 * @ppos: input file offset
1276 * @out: file to splice to
1277 * @opos: output file offset
1278 * @len: number of bytes to splice
1279 * @flags: splice modifier flags
1282 * For use by do_sendfile(). splice can easily emulate sendfile, but
1283 * doing it in the application would incur an extra system call
1284 * (splice in + splice out, as compared to just sendfile()). So this helper
1285 * can splice directly through a process-private pipe.
1288 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1289 loff_t *opos, size_t len, unsigned int flags)
1291 struct splice_desc sd = {
1301 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1304 if (unlikely(out->f_flags & O_APPEND))
1307 ret = rw_verify_area(WRITE, out, opos, len);
1308 if (unlikely(ret < 0))
1311 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1317 EXPORT_SYMBOL(do_splice_direct);
1319 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1320 struct pipe_inode_info *opipe,
1321 size_t len, unsigned int flags);
1324 * Determine where to splice to/from.
1326 static long do_splice(struct file *in, loff_t __user *off_in,
1327 struct file *out, loff_t __user *off_out,
1328 size_t len, unsigned int flags)
1330 struct pipe_inode_info *ipipe;
1331 struct pipe_inode_info *opipe;
1335 ipipe = get_pipe_info(in);
1336 opipe = get_pipe_info(out);
1338 if (ipipe && opipe) {
1339 if (off_in || off_out)
1342 if (!(in->f_mode & FMODE_READ))
1345 if (!(out->f_mode & FMODE_WRITE))
1348 /* Splicing to self would be fun, but... */
1352 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1359 if (!(out->f_mode & FMODE_PWRITE))
1361 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1364 offset = out->f_pos;
1367 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1370 if (unlikely(out->f_flags & O_APPEND))
1373 ret = rw_verify_area(WRITE, out, &offset, len);
1374 if (unlikely(ret < 0))
1377 file_start_write(out);
1378 ret = do_splice_from(ipipe, out, &offset, len, flags);
1379 file_end_write(out);
1382 out->f_pos = offset;
1383 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1393 if (!(in->f_mode & FMODE_PREAD))
1395 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1401 ret = do_splice_to(in, &offset, opipe, len, flags);
1405 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1415 * Map an iov into an array of pages and offset/length tupples. With the
1416 * partial_page structure, we can map several non-contiguous ranges into
1417 * our ones pages[] map instead of splitting that operation into pieces.
1418 * Could easily be exported as a generic helper for other users, in which
1419 * case one would probably want to add a 'max_nr_pages' parameter as well.
1421 static int get_iovec_page_array(const struct iovec __user *iov,
1422 unsigned int nr_vecs, struct page **pages,
1423 struct partial_page *partial, bool aligned,
1424 unsigned int pipe_buffers)
1426 int buffers = 0, error = 0;
1429 unsigned long off, npages;
1436 if (copy_from_user(&entry, iov, sizeof(entry)))
1439 base = entry.iov_base;
1440 len = entry.iov_len;
1443 * Sanity check this iovec. 0 read succeeds.
1449 if (!access_ok(VERIFY_READ, base, len))
1453 * Get this base offset and number of pages, then map
1454 * in the user pages.
1456 off = (unsigned long) base & ~PAGE_MASK;
1459 * If asked for alignment, the offset must be zero and the
1460 * length a multiple of the PAGE_SIZE.
1463 if (aligned && (off || len & ~PAGE_MASK))
1466 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1467 if (npages > pipe_buffers - buffers)
1468 npages = pipe_buffers - buffers;
1470 error = get_user_pages_fast((unsigned long)base, npages,
1471 0, &pages[buffers]);
1473 if (unlikely(error <= 0))
1477 * Fill this contiguous range into the partial page map.
1479 for (i = 0; i < error; i++) {
1480 const int plen = min_t(size_t, len, PAGE_SIZE - off);
1482 partial[buffers].offset = off;
1483 partial[buffers].len = plen;
1491 * We didn't complete this iov, stop here since it probably
1492 * means we have to move some of this into a pipe to
1493 * be able to continue.
1499 * Don't continue if we mapped fewer pages than we asked for,
1500 * or if we mapped the max number of pages that we have
1503 if (error < npages || buffers == pipe_buffers)
1516 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1517 struct splice_desc *sd)
1519 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1520 return n == sd->len ? n : -EFAULT;
1524 * For lack of a better implementation, implement vmsplice() to userspace
1525 * as a simple copy of the pipes pages to the user iov.
1527 static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
1528 unsigned long nr_segs, unsigned int flags)
1530 struct pipe_inode_info *pipe;
1531 struct splice_desc sd;
1533 struct iovec iovstack[UIO_FASTIOV];
1534 struct iovec *iov = iovstack;
1535 struct iov_iter iter;
1537 pipe = get_pipe_info(file);
1541 ret = import_iovec(READ, uiov, nr_segs,
1542 ARRAY_SIZE(iovstack), &iov, &iter);
1546 sd.total_len = iov_iter_count(&iter);
1554 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1563 * vmsplice splices a user address range into a pipe. It can be thought of
1564 * as splice-from-memory, where the regular splice is splice-from-file (or
1565 * to file). In both cases the output is a pipe, naturally.
1567 static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1568 unsigned long nr_segs, unsigned int flags)
1570 struct pipe_inode_info *pipe;
1571 struct page *pages[PIPE_DEF_BUFFERS];
1572 struct partial_page partial[PIPE_DEF_BUFFERS];
1573 struct splice_pipe_desc spd = {
1576 .nr_pages_max = PIPE_DEF_BUFFERS,
1578 .ops = &user_page_pipe_buf_ops,
1579 .spd_release = spd_release_page,
1583 pipe = get_pipe_info(file);
1587 if (splice_grow_spd(pipe, &spd))
1590 spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
1593 if (spd.nr_pages <= 0)
1596 ret = splice_to_pipe(pipe, &spd);
1598 splice_shrink_spd(&spd);
1603 * Note that vmsplice only really supports true splicing _from_ user memory
1604 * to a pipe, not the other way around. Splicing from user memory is a simple
1605 * operation that can be supported without any funky alignment restrictions
1606 * or nasty vm tricks. We simply map in the user memory and fill them into
1607 * a pipe. The reverse isn't quite as easy, though. There are two possible
1608 * solutions for that:
1610 * - memcpy() the data internally, at which point we might as well just
1611 * do a regular read() on the buffer anyway.
1612 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1613 * has restriction limitations on both ends of the pipe).
1615 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1618 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1619 unsigned long, nr_segs, unsigned int, flags)
1624 if (unlikely(nr_segs > UIO_MAXIOV))
1626 else if (unlikely(!nr_segs))
1632 if (f.file->f_mode & FMODE_WRITE)
1633 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1634 else if (f.file->f_mode & FMODE_READ)
1635 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1643 #ifdef CONFIG_COMPAT
1644 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1645 unsigned int, nr_segs, unsigned int, flags)
1648 struct iovec __user *iov;
1649 if (nr_segs > UIO_MAXIOV)
1651 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1652 for (i = 0; i < nr_segs; i++) {
1653 struct compat_iovec v;
1654 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1655 get_user(v.iov_len, &iov32[i].iov_len) ||
1656 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1657 put_user(v.iov_len, &iov[i].iov_len))
1660 return sys_vmsplice(fd, iov, nr_segs, flags);
1664 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1665 int, fd_out, loff_t __user *, off_out,
1666 size_t, len, unsigned int, flags)
1677 if (in.file->f_mode & FMODE_READ) {
1678 out = fdget(fd_out);
1680 if (out.file->f_mode & FMODE_WRITE)
1681 error = do_splice(in.file, off_in,
1693 * Make sure there's data to read. Wait for input if we can, otherwise
1694 * return an appropriate error.
1696 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1701 * Check ->nrbufs without the inode lock first. This function
1702 * is speculative anyways, so missing one is ok.
1710 while (!pipe->nrbufs) {
1711 if (signal_pending(current)) {
1717 if (!pipe->waiting_writers) {
1718 if (flags & SPLICE_F_NONBLOCK) {
1731 * Make sure there's writeable room. Wait for room if we can, otherwise
1732 * return an appropriate error.
1734 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1739 * Check ->nrbufs without the inode lock first. This function
1740 * is speculative anyways, so missing one is ok.
1742 if (pipe->nrbufs < pipe->buffers)
1748 while (pipe->nrbufs >= pipe->buffers) {
1749 if (!pipe->readers) {
1750 send_sig(SIGPIPE, current, 0);
1754 if (flags & SPLICE_F_NONBLOCK) {
1758 if (signal_pending(current)) {
1762 pipe->waiting_writers++;
1764 pipe->waiting_writers--;
1772 * Splice contents of ipipe to opipe.
1774 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1775 struct pipe_inode_info *opipe,
1776 size_t len, unsigned int flags)
1778 struct pipe_buffer *ibuf, *obuf;
1780 bool input_wakeup = false;
1784 ret = ipipe_prep(ipipe, flags);
1788 ret = opipe_prep(opipe, flags);
1793 * Potential ABBA deadlock, work around it by ordering lock
1794 * grabbing by pipe info address. Otherwise two different processes
1795 * could deadlock (one doing tee from A -> B, the other from B -> A).
1797 pipe_double_lock(ipipe, opipe);
1800 if (!opipe->readers) {
1801 send_sig(SIGPIPE, current, 0);
1807 if (!ipipe->nrbufs && !ipipe->writers)
1811 * Cannot make any progress, because either the input
1812 * pipe is empty or the output pipe is full.
1814 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
1815 /* Already processed some buffers, break */
1819 if (flags & SPLICE_F_NONBLOCK) {
1825 * We raced with another reader/writer and haven't
1826 * managed to process any buffers. A zero return
1827 * value means EOF, so retry instead.
1834 ibuf = ipipe->bufs + ipipe->curbuf;
1835 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1836 obuf = opipe->bufs + nbuf;
1838 if (len >= ibuf->len) {
1840 * Simply move the whole buffer from ipipe to opipe
1845 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
1847 input_wakeup = true;
1850 * Get a reference to this pipe buffer,
1851 * so we can copy the contents over.
1853 ibuf->ops->get(ipipe, ibuf);
1857 * Don't inherit the gift flag, we need to
1858 * prevent multiple steals of this page.
1860 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1864 ibuf->offset += obuf->len;
1865 ibuf->len -= obuf->len;
1875 * If we put data in the output pipe, wakeup any potential readers.
1878 wakeup_pipe_readers(opipe);
1881 wakeup_pipe_writers(ipipe);
1887 * Link contents of ipipe to opipe.
1889 static int link_pipe(struct pipe_inode_info *ipipe,
1890 struct pipe_inode_info *opipe,
1891 size_t len, unsigned int flags)
1893 struct pipe_buffer *ibuf, *obuf;
1894 int ret = 0, i = 0, nbuf;
1897 * Potential ABBA deadlock, work around it by ordering lock
1898 * grabbing by pipe info address. Otherwise two different processes
1899 * could deadlock (one doing tee from A -> B, the other from B -> A).
1901 pipe_double_lock(ipipe, opipe);
1904 if (!opipe->readers) {
1905 send_sig(SIGPIPE, current, 0);
1912 * If we have iterated all input buffers or ran out of
1913 * output room, break.
1915 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1918 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1919 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1922 * Get a reference to this pipe buffer,
1923 * so we can copy the contents over.
1925 ibuf->ops->get(ipipe, ibuf);
1927 obuf = opipe->bufs + nbuf;
1931 * Don't inherit the gift flag, we need to
1932 * prevent multiple steals of this page.
1934 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1936 if (obuf->len > len)
1946 * return EAGAIN if we have the potential of some data in the
1947 * future, otherwise just return 0
1949 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1956 * If we put data in the output pipe, wakeup any potential readers.
1959 wakeup_pipe_readers(opipe);
1965 * This is a tee(1) implementation that works on pipes. It doesn't copy
1966 * any data, it simply references the 'in' pages on the 'out' pipe.
1967 * The 'flags' used are the SPLICE_F_* variants, currently the only
1968 * applicable one is SPLICE_F_NONBLOCK.
1970 static long do_tee(struct file *in, struct file *out, size_t len,
1973 struct pipe_inode_info *ipipe = get_pipe_info(in);
1974 struct pipe_inode_info *opipe = get_pipe_info(out);
1978 * Duplicate the contents of ipipe to opipe without actually
1981 if (ipipe && opipe && ipipe != opipe) {
1983 * Keep going, unless we encounter an error. The ipipe/opipe
1984 * ordering doesn't really matter.
1986 ret = ipipe_prep(ipipe, flags);
1988 ret = opipe_prep(opipe, flags);
1990 ret = link_pipe(ipipe, opipe, len, flags);
1997 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
2008 if (in.file->f_mode & FMODE_READ) {
2009 struct fd out = fdget(fdout);
2011 if (out.file->f_mode & FMODE_WRITE)
2012 error = do_tee(in.file, out.file,