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@suse.de>
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/pipe_fs_i.h>
24 #include <linux/mm_inline.h>
25 #include <linux/swap.h>
26 #include <linux/writeback.h>
27 #include <linux/buffer_head.h>
28 #include <linux/module.h>
29 #include <linux/syscalls.h>
30 #include <linux/uio.h>
38 * Passed to splice_to_pipe
40 struct splice_pipe_desc {
41 struct page **pages; /* page map */
42 struct partial_page *partial; /* pages[] may not be contig */
43 int nr_pages; /* number of pages in map */
44 unsigned int flags; /* splice flags */
45 struct pipe_buf_operations *ops;/* ops associated with output pipe */
49 * Attempt to steal a page from a pipe buffer. This should perhaps go into
50 * a vm helper function, it's already simplified quite a bit by the
51 * addition of remove_mapping(). If success is returned, the caller may
52 * attempt to reuse this page for another destination.
54 static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
55 struct pipe_buffer *buf)
57 struct page *page = buf->page;
58 struct address_space *mapping = page_mapping(page);
62 WARN_ON(!PageUptodate(page));
65 * At least for ext2 with nobh option, we need to wait on writeback
66 * completing on this page, since we'll remove it from the pagecache.
67 * Otherwise truncate wont wait on the page, allowing the disk
68 * blocks to be reused by someone else before we actually wrote our
69 * data to them. fs corruption ensues.
71 wait_on_page_writeback(page);
73 if (PagePrivate(page))
74 try_to_release_page(page, mapping_gfp_mask(mapping));
76 if (!remove_mapping(mapping, page)) {
84 static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
85 struct pipe_buffer *buf)
87 page_cache_release(buf->page);
90 static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
91 struct pipe_buffer *buf)
93 struct page *page = buf->page;
96 if (!PageUptodate(page)) {
100 * Page got truncated/unhashed. This will cause a 0-byte
101 * splice, if this is the first page.
103 if (!page->mapping) {
109 * Uh oh, read-error from disk.
111 if (!PageUptodate(page)) {
117 * Page is ok afterall, we are done.
128 static struct pipe_buf_operations page_cache_pipe_buf_ops = {
130 .map = generic_pipe_buf_map,
131 .unmap = generic_pipe_buf_unmap,
132 .pin = page_cache_pipe_buf_pin,
133 .release = page_cache_pipe_buf_release,
134 .steal = page_cache_pipe_buf_steal,
135 .get = generic_pipe_buf_get,
138 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
139 struct pipe_buffer *buf)
141 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
144 return generic_pipe_buf_steal(pipe, buf);
147 static struct pipe_buf_operations user_page_pipe_buf_ops = {
149 .map = generic_pipe_buf_map,
150 .unmap = generic_pipe_buf_unmap,
151 .pin = generic_pipe_buf_pin,
152 .release = page_cache_pipe_buf_release,
153 .steal = user_page_pipe_buf_steal,
154 .get = generic_pipe_buf_get,
158 * Pipe output worker. This sets up our pipe format with the page cache
159 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
161 static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
162 struct splice_pipe_desc *spd)
164 int ret, do_wakeup, page_nr;
171 mutex_lock(&pipe->inode->i_mutex);
174 if (!pipe->readers) {
175 send_sig(SIGPIPE, current, 0);
181 if (pipe->nrbufs < PIPE_BUFFERS) {
182 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
183 struct pipe_buffer *buf = pipe->bufs + newbuf;
185 buf->page = spd->pages[page_nr];
186 buf->offset = spd->partial[page_nr].offset;
187 buf->len = spd->partial[page_nr].len;
189 if (spd->flags & SPLICE_F_GIFT)
190 buf->flags |= PIPE_BUF_FLAG_GIFT;
199 if (!--spd->nr_pages)
201 if (pipe->nrbufs < PIPE_BUFFERS)
207 if (spd->flags & SPLICE_F_NONBLOCK) {
213 if (signal_pending(current)) {
221 if (waitqueue_active(&pipe->wait))
222 wake_up_interruptible_sync(&pipe->wait);
223 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
227 pipe->waiting_writers++;
229 pipe->waiting_writers--;
233 mutex_unlock(&pipe->inode->i_mutex);
237 if (waitqueue_active(&pipe->wait))
238 wake_up_interruptible(&pipe->wait);
239 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
242 while (page_nr < spd->nr_pages)
243 page_cache_release(spd->pages[page_nr++]);
249 __generic_file_splice_read(struct file *in, loff_t *ppos,
250 struct pipe_inode_info *pipe, size_t len,
253 struct address_space *mapping = in->f_mapping;
254 unsigned int loff, nr_pages;
255 struct page *pages[PIPE_BUFFERS];
256 struct partial_page partial[PIPE_BUFFERS];
258 pgoff_t index, end_index;
262 struct splice_pipe_desc spd = {
266 .ops = &page_cache_pipe_buf_ops,
269 index = *ppos >> PAGE_CACHE_SHIFT;
270 loff = *ppos & ~PAGE_CACHE_MASK;
271 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
273 if (nr_pages > PIPE_BUFFERS)
274 nr_pages = PIPE_BUFFERS;
277 * Initiate read-ahead on this page range. however, don't call into
278 * read-ahead if this is a non-zero offset (we are likely doing small
279 * chunk splice and the page is already there) for a single page.
281 if (!loff || nr_pages > 1)
282 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
285 * Now fill in the holes:
291 * Lookup the (hopefully) full range of pages we need.
293 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
296 * If find_get_pages_contig() returned fewer pages than we needed,
299 index += spd.nr_pages;
300 while (spd.nr_pages < nr_pages) {
302 * Page could be there, find_get_pages_contig() breaks on
305 page = find_get_page(mapping, index);
308 * Make sure the read-ahead engine is notified
309 * about this failure.
311 handle_ra_miss(mapping, &in->f_ra, index);
314 * page didn't exist, allocate one.
316 page = page_cache_alloc_cold(mapping);
320 error = add_to_page_cache_lru(page, mapping, index,
321 mapping_gfp_mask(mapping));
322 if (unlikely(error)) {
323 page_cache_release(page);
327 * add_to_page_cache() locks the page, unlock it
328 * to avoid convoluting the logic below even more.
333 pages[spd.nr_pages++] = page;
338 * Now loop over the map and see if we need to start IO on any
339 * pages, fill in the partial map, etc.
341 index = *ppos >> PAGE_CACHE_SHIFT;
342 nr_pages = spd.nr_pages;
344 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
345 unsigned int this_len;
351 * this_len is the max we'll use from this page
353 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
354 page = pages[page_nr];
357 * If the page isn't uptodate, we may need to start io on it
359 if (!PageUptodate(page)) {
361 * If in nonblock mode then dont block on waiting
362 * for an in-flight io page
364 if (flags & SPLICE_F_NONBLOCK)
370 * page was truncated, stop here. if this isn't the
371 * first page, we'll just complete what we already
374 if (!page->mapping) {
379 * page was already under io and is now done, great
381 if (PageUptodate(page)) {
387 * need to read in the page
389 error = mapping->a_ops->readpage(in, page);
390 if (unlikely(error)) {
392 * We really should re-lookup the page here,
393 * but it complicates things a lot. Instead
394 * lets just do what we already stored, and
395 * we'll get it the next time we are called.
397 if (error == AOP_TRUNCATED_PAGE)
404 * i_size must be checked after ->readpage().
406 isize = i_size_read(mapping->host);
407 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
408 if (unlikely(!isize || index > end_index))
412 * if this is the last page, see if we need to shrink
413 * the length and stop
415 if (end_index == index) {
416 loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
417 if (total_len + loff > isize)
420 * force quit after adding this page
423 this_len = min(this_len, loff);
428 partial[page_nr].offset = loff;
429 partial[page_nr].len = this_len;
431 total_len += this_len;
438 * Release any pages at the end, if we quit early. 'i' is how far
439 * we got, 'nr_pages' is how many pages are in the map.
441 while (page_nr < nr_pages)
442 page_cache_release(pages[page_nr++]);
445 return splice_to_pipe(pipe, &spd);
451 * generic_file_splice_read - splice data from file to a pipe
452 * @in: file to splice from
453 * @pipe: pipe to splice to
454 * @len: number of bytes to splice
455 * @flags: splice modifier flags
457 * Will read pages from given file and fill them into a pipe.
459 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
460 struct pipe_inode_info *pipe, size_t len,
470 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
477 if (flags & SPLICE_F_NONBLOCK) {
494 EXPORT_SYMBOL(generic_file_splice_read);
497 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
498 * using sendpage(). Return the number of bytes sent.
500 static int pipe_to_sendpage(struct pipe_inode_info *info,
501 struct pipe_buffer *buf, struct splice_desc *sd)
503 struct file *file = sd->file;
504 loff_t pos = sd->pos;
507 ret = buf->ops->pin(info, buf);
509 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
511 ret = file->f_op->sendpage(file, buf->page, buf->offset,
512 sd->len, &pos, more);
519 * This is a little more tricky than the file -> pipe splicing. There are
520 * basically three cases:
522 * - Destination page already exists in the address space and there
523 * are users of it. For that case we have no other option that
524 * copying the data. Tough luck.
525 * - Destination page already exists in the address space, but there
526 * are no users of it. Make sure it's uptodate, then drop it. Fall
527 * through to last case.
528 * - Destination page does not exist, we can add the pipe page to
529 * the page cache and avoid the copy.
531 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
532 * sd->flags), we attempt to migrate pages from the pipe to the output
533 * file address space page cache. This is possible if no one else has
534 * the pipe page referenced outside of the pipe and page cache. If
535 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
536 * a new page in the output file page cache and fill/dirty that.
538 static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
539 struct splice_desc *sd)
541 struct file *file = sd->file;
542 struct address_space *mapping = file->f_mapping;
543 gfp_t gfp_mask = mapping_gfp_mask(mapping);
544 unsigned int offset, this_len;
550 * make sure the data in this buffer is uptodate
552 ret = buf->ops->pin(info, buf);
556 index = sd->pos >> PAGE_CACHE_SHIFT;
557 offset = sd->pos & ~PAGE_CACHE_MASK;
560 if (this_len + offset > PAGE_CACHE_SIZE)
561 this_len = PAGE_CACHE_SIZE - offset;
564 * Reuse buf page, if SPLICE_F_MOVE is set and we are doing a full
567 if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
569 * If steal succeeds, buf->page is now pruned from the vm
570 * side (page cache) and we can reuse it. The page will also
571 * be locked on successful return.
573 if (buf->ops->steal(info, buf))
577 page_cache_get(page);
580 * page must be on the LRU for adding to the pagecache.
581 * Check this without grabbing the zone lock, if it isn't
582 * the do grab the zone lock, recheck, and add if necessary.
584 if (!PageLRU(page)) {
585 struct zone *zone = page_zone(page);
587 spin_lock_irq(&zone->lru_lock);
588 if (!PageLRU(page)) {
590 add_page_to_inactive_list(zone, page);
592 spin_unlock_irq(&zone->lru_lock);
595 if (add_to_page_cache(page, mapping, index, gfp_mask)) {
596 page_cache_release(page);
602 page = find_lock_page(mapping, index);
605 page = page_cache_alloc_cold(mapping);
610 * This will also lock the page
612 ret = add_to_page_cache_lru(page, mapping, index,
619 * We get here with the page locked. If the page is also
620 * uptodate, we don't need to do more. If it isn't, we
621 * may need to bring it in if we are not going to overwrite
624 if (!PageUptodate(page)) {
625 if (this_len < PAGE_CACHE_SIZE) {
626 ret = mapping->a_ops->readpage(file, page);
632 if (!PageUptodate(page)) {
634 * Page got invalidated, repeat.
636 if (!page->mapping) {
638 page_cache_release(page);
645 SetPageUptodate(page);
649 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
650 if (ret == AOP_TRUNCATED_PAGE) {
651 page_cache_release(page);
656 if (buf->page != page) {
658 * Careful, ->map() uses KM_USER0!
660 char *src = buf->ops->map(info, buf, 1);
661 char *dst = kmap_atomic(page, KM_USER1);
663 memcpy(dst + offset, src + buf->offset, this_len);
664 flush_dcache_page(page);
665 kunmap_atomic(dst, KM_USER1);
666 buf->ops->unmap(info, buf, src);
669 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
672 * Return the number of bytes written and mark page as
673 * accessed, we are now done!
676 mark_page_accessed(page);
677 balance_dirty_pages_ratelimited(mapping);
678 } else if (ret == AOP_TRUNCATED_PAGE) {
679 page_cache_release(page);
683 page_cache_release(page);
690 * Pipe input worker. Most of this logic works like a regular pipe, the
691 * key here is the 'actor' worker passed in that actually moves the data
692 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
694 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
695 loff_t *ppos, size_t len, unsigned int flags,
698 int ret, do_wakeup, err;
699 struct splice_desc sd;
710 mutex_lock(&pipe->inode->i_mutex);
714 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
715 struct pipe_buf_operations *ops = buf->ops;
718 if (sd.len > sd.total_len)
719 sd.len = sd.total_len;
721 err = actor(pipe, buf, &sd);
723 if (!ret && err != -ENODATA)
741 ops->release(pipe, buf);
742 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
756 if (!pipe->waiting_writers) {
761 if (flags & SPLICE_F_NONBLOCK) {
767 if (signal_pending(current)) {
775 if (waitqueue_active(&pipe->wait))
776 wake_up_interruptible_sync(&pipe->wait);
777 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
785 mutex_unlock(&pipe->inode->i_mutex);
789 if (waitqueue_active(&pipe->wait))
790 wake_up_interruptible(&pipe->wait);
791 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
798 * generic_file_splice_write - splice data from a pipe to a file
800 * @out: file to write to
801 * @len: number of bytes to splice
802 * @flags: splice modifier flags
804 * Will either move or copy pages (determined by @flags options) from
805 * the given pipe inode to the given file.
809 generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
810 loff_t *ppos, size_t len, unsigned int flags)
812 struct address_space *mapping = out->f_mapping;
815 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
817 struct inode *inode = mapping->host;
822 * If file or inode is SYNC and we actually wrote some data,
825 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
828 mutex_lock(&inode->i_mutex);
829 err = generic_osync_inode(inode, mapping,
830 OSYNC_METADATA|OSYNC_DATA);
831 mutex_unlock(&inode->i_mutex);
841 EXPORT_SYMBOL(generic_file_splice_write);
844 * generic_splice_sendpage - splice data from a pipe to a socket
846 * @out: socket to write to
847 * @len: number of bytes to splice
848 * @flags: splice modifier flags
850 * Will send @len bytes from the pipe to a network socket. No data copying
854 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
855 loff_t *ppos, size_t len, unsigned int flags)
857 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
860 EXPORT_SYMBOL(generic_splice_sendpage);
863 * Attempt to initiate a splice from pipe to file.
865 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
866 loff_t *ppos, size_t len, unsigned int flags)
870 if (unlikely(!out->f_op || !out->f_op->splice_write))
873 if (unlikely(!(out->f_mode & FMODE_WRITE)))
876 ret = rw_verify_area(WRITE, out, ppos, len);
877 if (unlikely(ret < 0))
880 return out->f_op->splice_write(pipe, out, ppos, len, flags);
884 * Attempt to initiate a splice from a file to a pipe.
886 static long do_splice_to(struct file *in, loff_t *ppos,
887 struct pipe_inode_info *pipe, size_t len,
893 if (unlikely(!in->f_op || !in->f_op->splice_read))
896 if (unlikely(!(in->f_mode & FMODE_READ)))
899 ret = rw_verify_area(READ, in, ppos, len);
900 if (unlikely(ret < 0))
903 isize = i_size_read(in->f_mapping->host);
904 if (unlikely(*ppos >= isize))
907 left = isize - *ppos;
908 if (unlikely(left < len))
911 return in->f_op->splice_read(in, ppos, pipe, len, flags);
914 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
915 size_t len, unsigned int flags)
917 struct pipe_inode_info *pipe;
924 * We require the input being a regular file, as we don't want to
925 * randomly drop data for eg socket -> socket splicing. Use the
926 * piped splicing for that!
928 i_mode = in->f_dentry->d_inode->i_mode;
929 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
933 * neither in nor out is a pipe, setup an internal pipe attached to
934 * 'out' and transfer the wanted data from 'in' to 'out' through that
936 pipe = current->splice_pipe;
937 if (unlikely(!pipe)) {
938 pipe = alloc_pipe_info(NULL);
943 * We don't have an immediate reader, but we'll read the stuff
944 * out of the pipe right after the splice_to_pipe(). So set
945 * PIPE_READERS appropriately.
949 current->splice_pipe = pipe;
960 size_t read_len, max_read_len;
963 * Do at most PIPE_BUFFERS pages worth of transfer:
965 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
967 ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
968 if (unlikely(ret < 0))
974 * NOTE: nonblocking mode only applies to the input. We
975 * must not do the output in nonblocking mode as then we
976 * could get stuck data in the internal pipe:
978 ret = do_splice_from(pipe, out, &out_off, read_len,
979 flags & ~SPLICE_F_NONBLOCK);
980 if (unlikely(ret < 0))
987 * In nonblocking mode, if we got back a short read then
988 * that was due to either an IO error or due to the
989 * pagecache entry not being there. In the IO error case
990 * the _next_ splice attempt will produce a clean IO error
991 * return value (not a short read), so in both cases it's
992 * correct to break out of the loop here:
994 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
998 pipe->nrbufs = pipe->curbuf = 0;
1004 * If we did an incomplete transfer we must release
1005 * the pipe buffers in question:
1007 for (i = 0; i < PIPE_BUFFERS; i++) {
1008 struct pipe_buffer *buf = pipe->bufs + i;
1011 buf->ops->release(pipe, buf);
1015 pipe->nrbufs = pipe->curbuf = 0;
1018 * If we transferred some data, return the number of bytes:
1026 EXPORT_SYMBOL(do_splice_direct);
1029 * Determine where to splice to/from.
1031 static long do_splice(struct file *in, loff_t __user *off_in,
1032 struct file *out, loff_t __user *off_out,
1033 size_t len, unsigned int flags)
1035 struct pipe_inode_info *pipe;
1036 loff_t offset, *off;
1039 pipe = in->f_dentry->d_inode->i_pipe;
1044 if (out->f_op->llseek == no_llseek)
1046 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1052 ret = do_splice_from(pipe, out, off, len, flags);
1054 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1060 pipe = out->f_dentry->d_inode->i_pipe;
1065 if (in->f_op->llseek == no_llseek)
1067 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1073 ret = do_splice_to(in, off, pipe, len, flags);
1075 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1085 * Map an iov into an array of pages and offset/length tupples. With the
1086 * partial_page structure, we can map several non-contiguous ranges into
1087 * our ones pages[] map instead of splitting that operation into pieces.
1088 * Could easily be exported as a generic helper for other users, in which
1089 * case one would probably want to add a 'max_nr_pages' parameter as well.
1091 static int get_iovec_page_array(const struct iovec __user *iov,
1092 unsigned int nr_vecs, struct page **pages,
1093 struct partial_page *partial, int aligned)
1095 int buffers = 0, error = 0;
1098 * It's ok to take the mmap_sem for reading, even
1099 * across a "get_user()".
1101 down_read(¤t->mm->mmap_sem);
1104 unsigned long off, npages;
1110 * Get user address base and length for this iovec.
1112 error = get_user(base, &iov->iov_base);
1113 if (unlikely(error))
1115 error = get_user(len, &iov->iov_len);
1116 if (unlikely(error))
1120 * Sanity check this iovec. 0 read succeeds.
1125 if (unlikely(!base))
1129 * Get this base offset and number of pages, then map
1130 * in the user pages.
1132 off = (unsigned long) base & ~PAGE_MASK;
1135 * If asked for alignment, the offset must be zero and the
1136 * length a multiple of the PAGE_SIZE.
1139 if (aligned && (off || len & ~PAGE_MASK))
1142 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1143 if (npages > PIPE_BUFFERS - buffers)
1144 npages = PIPE_BUFFERS - buffers;
1146 error = get_user_pages(current, current->mm,
1147 (unsigned long) base, npages, 0, 0,
1148 &pages[buffers], NULL);
1150 if (unlikely(error <= 0))
1154 * Fill this contiguous range into the partial page map.
1156 for (i = 0; i < error; i++) {
1157 const int plen = min_t(size_t, len, PAGE_SIZE - off);
1159 partial[buffers].offset = off;
1160 partial[buffers].len = plen;
1168 * We didn't complete this iov, stop here since it probably
1169 * means we have to move some of this into a pipe to
1170 * be able to continue.
1176 * Don't continue if we mapped fewer pages than we asked for,
1177 * or if we mapped the max number of pages that we have
1180 if (error < npages || buffers == PIPE_BUFFERS)
1187 up_read(¤t->mm->mmap_sem);
1196 * vmsplice splices a user address range into a pipe. It can be thought of
1197 * as splice-from-memory, where the regular splice is splice-from-file (or
1198 * to file). In both cases the output is a pipe, naturally.
1200 * Note that vmsplice only supports splicing _from_ user memory to a pipe,
1201 * not the other way around. Splicing from user memory is a simple operation
1202 * that can be supported without any funky alignment restrictions or nasty
1203 * vm tricks. We simply map in the user memory and fill them into a pipe.
1204 * The reverse isn't quite as easy, though. There are two possible solutions
1207 * - memcpy() the data internally, at which point we might as well just
1208 * do a regular read() on the buffer anyway.
1209 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1210 * has restriction limitations on both ends of the pipe).
1212 * Alas, it isn't here.
1215 static long do_vmsplice(struct file *file, const struct iovec __user *iov,
1216 unsigned long nr_segs, unsigned int flags)
1218 struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
1219 struct page *pages[PIPE_BUFFERS];
1220 struct partial_page partial[PIPE_BUFFERS];
1221 struct splice_pipe_desc spd = {
1225 .ops = &user_page_pipe_buf_ops,
1228 if (unlikely(!pipe))
1230 if (unlikely(nr_segs > UIO_MAXIOV))
1232 else if (unlikely(!nr_segs))
1235 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1236 flags & SPLICE_F_GIFT);
1237 if (spd.nr_pages <= 0)
1238 return spd.nr_pages;
1240 return splice_to_pipe(pipe, &spd);
1243 asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1244 unsigned long nr_segs, unsigned int flags)
1251 file = fget_light(fd, &fput);
1253 if (file->f_mode & FMODE_WRITE)
1254 error = do_vmsplice(file, iov, nr_segs, flags);
1256 fput_light(file, fput);
1262 asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1263 int fd_out, loff_t __user *off_out,
1264 size_t len, unsigned int flags)
1267 struct file *in, *out;
1268 int fput_in, fput_out;
1274 in = fget_light(fd_in, &fput_in);
1276 if (in->f_mode & FMODE_READ) {
1277 out = fget_light(fd_out, &fput_out);
1279 if (out->f_mode & FMODE_WRITE)
1280 error = do_splice(in, off_in,
1283 fput_light(out, fput_out);
1287 fput_light(in, fput_in);
1294 * Link contents of ipipe to opipe.
1296 static int link_pipe(struct pipe_inode_info *ipipe,
1297 struct pipe_inode_info *opipe,
1298 size_t len, unsigned int flags)
1300 struct pipe_buffer *ibuf, *obuf;
1301 int ret, do_wakeup, i, ipipe_first;
1303 ret = do_wakeup = ipipe_first = 0;
1306 * Potential ABBA deadlock, work around it by ordering lock
1307 * grabbing by inode address. Otherwise two different processes
1308 * could deadlock (one doing tee from A -> B, the other from B -> A).
1310 if (ipipe->inode < opipe->inode) {
1312 mutex_lock(&ipipe->inode->i_mutex);
1313 mutex_lock(&opipe->inode->i_mutex);
1315 mutex_lock(&opipe->inode->i_mutex);
1316 mutex_lock(&ipipe->inode->i_mutex);
1320 if (!opipe->readers) {
1321 send_sig(SIGPIPE, current, 0);
1326 if (ipipe->nrbufs - i) {
1327 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1330 * If we have room, fill this buffer
1332 if (opipe->nrbufs < PIPE_BUFFERS) {
1333 int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1336 * Get a reference to this pipe buffer,
1337 * so we can copy the contents over.
1339 ibuf->ops->get(ipipe, ibuf);
1341 obuf = opipe->bufs + nbuf;
1345 * Don't inherit the gift flag, we need to
1346 * prevent multiple steals of this page.
1348 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1350 if (obuf->len > len)
1360 if (opipe->nrbufs < PIPE_BUFFERS)
1365 * We have input available, but no output room.
1366 * If we already copied data, return that. If we
1367 * need to drop the opipe lock, it must be ordered
1368 * last to avoid deadlocks.
1370 if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
1375 if (signal_pending(current)) {
1382 if (waitqueue_active(&opipe->wait))
1383 wake_up_interruptible(&opipe->wait);
1384 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1388 opipe->waiting_writers++;
1390 opipe->waiting_writers--;
1395 * No input buffers, do the usual checks for available
1396 * writers and blocking and wait if necessary
1398 if (!ipipe->writers)
1400 if (!ipipe->waiting_writers) {
1405 * pipe_wait() drops the ipipe mutex. To avoid deadlocks
1406 * with another process, we can only safely do that if
1407 * the ipipe lock is ordered last.
1409 if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
1414 if (signal_pending(current)) {
1420 if (waitqueue_active(&ipipe->wait))
1421 wake_up_interruptible_sync(&ipipe->wait);
1422 kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
1427 mutex_unlock(&ipipe->inode->i_mutex);
1428 mutex_unlock(&opipe->inode->i_mutex);
1432 if (waitqueue_active(&opipe->wait))
1433 wake_up_interruptible(&opipe->wait);
1434 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1441 * This is a tee(1) implementation that works on pipes. It doesn't copy
1442 * any data, it simply references the 'in' pages on the 'out' pipe.
1443 * The 'flags' used are the SPLICE_F_* variants, currently the only
1444 * applicable one is SPLICE_F_NONBLOCK.
1446 static long do_tee(struct file *in, struct file *out, size_t len,
1449 struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
1450 struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
1453 * Link ipipe to the two output pipes, consuming as we go along.
1456 return link_pipe(ipipe, opipe, len, flags);
1461 asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1470 in = fget_light(fdin, &fput_in);
1472 if (in->f_mode & FMODE_READ) {
1474 struct file *out = fget_light(fdout, &fput_out);
1477 if (out->f_mode & FMODE_WRITE)
1478 error = do_tee(in, out, len, flags);
1479 fput_light(out, fput_out);
1482 fput_light(in, fput_in);