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.
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)) {
81 buf->flags |= PIPE_BUF_FLAG_LRU;
85 static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
86 struct pipe_buffer *buf)
88 page_cache_release(buf->page);
90 buf->flags &= ~PIPE_BUF_FLAG_LRU;
93 static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
94 struct pipe_buffer *buf)
96 struct page *page = buf->page;
99 if (!PageUptodate(page)) {
103 * Page got truncated/unhashed. This will cause a 0-byte
104 * splice, if this is the first page.
106 if (!page->mapping) {
112 * Uh oh, read-error from disk.
114 if (!PageUptodate(page)) {
120 * Page is ok afterall, we are done.
131 static struct pipe_buf_operations page_cache_pipe_buf_ops = {
133 .map = generic_pipe_buf_map,
134 .unmap = generic_pipe_buf_unmap,
135 .pin = page_cache_pipe_buf_pin,
136 .release = page_cache_pipe_buf_release,
137 .steal = page_cache_pipe_buf_steal,
138 .get = generic_pipe_buf_get,
141 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
142 struct pipe_buffer *buf)
144 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
150 static struct pipe_buf_operations user_page_pipe_buf_ops = {
152 .map = generic_pipe_buf_map,
153 .unmap = generic_pipe_buf_unmap,
154 .pin = generic_pipe_buf_pin,
155 .release = page_cache_pipe_buf_release,
156 .steal = user_page_pipe_buf_steal,
157 .get = generic_pipe_buf_get,
161 * Pipe output worker. This sets up our pipe format with the page cache
162 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
164 static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
165 struct splice_pipe_desc *spd)
167 int ret, do_wakeup, page_nr;
174 mutex_lock(&pipe->inode->i_mutex);
177 if (!pipe->readers) {
178 send_sig(SIGPIPE, current, 0);
184 if (pipe->nrbufs < PIPE_BUFFERS) {
185 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
186 struct pipe_buffer *buf = pipe->bufs + newbuf;
188 buf->page = spd->pages[page_nr];
189 buf->offset = spd->partial[page_nr].offset;
190 buf->len = spd->partial[page_nr].len;
192 if (spd->flags & SPLICE_F_GIFT)
193 buf->flags |= PIPE_BUF_FLAG_GIFT;
202 if (!--spd->nr_pages)
204 if (pipe->nrbufs < PIPE_BUFFERS)
210 if (spd->flags & SPLICE_F_NONBLOCK) {
216 if (signal_pending(current)) {
224 if (waitqueue_active(&pipe->wait))
225 wake_up_interruptible_sync(&pipe->wait);
226 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
230 pipe->waiting_writers++;
232 pipe->waiting_writers--;
236 mutex_unlock(&pipe->inode->i_mutex);
240 if (waitqueue_active(&pipe->wait))
241 wake_up_interruptible(&pipe->wait);
242 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
245 while (page_nr < spd->nr_pages)
246 page_cache_release(spd->pages[page_nr++]);
252 __generic_file_splice_read(struct file *in, loff_t *ppos,
253 struct pipe_inode_info *pipe, size_t len,
256 struct address_space *mapping = in->f_mapping;
257 unsigned int loff, nr_pages;
258 struct page *pages[PIPE_BUFFERS];
259 struct partial_page partial[PIPE_BUFFERS];
261 pgoff_t index, end_index;
265 struct splice_pipe_desc spd = {
269 .ops = &page_cache_pipe_buf_ops,
272 index = *ppos >> PAGE_CACHE_SHIFT;
273 loff = *ppos & ~PAGE_CACHE_MASK;
274 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
276 if (nr_pages > PIPE_BUFFERS)
277 nr_pages = PIPE_BUFFERS;
280 * Initiate read-ahead on this page range. however, don't call into
281 * read-ahead if this is a non-zero offset (we are likely doing small
282 * chunk splice and the page is already there) for a single page.
284 if (!loff || nr_pages > 1)
285 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
288 * Now fill in the holes:
294 * Lookup the (hopefully) full range of pages we need.
296 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
299 * If find_get_pages_contig() returned fewer pages than we needed,
302 index += spd.nr_pages;
303 while (spd.nr_pages < nr_pages) {
305 * Page could be there, find_get_pages_contig() breaks on
308 page = find_get_page(mapping, index);
311 * Make sure the read-ahead engine is notified
312 * about this failure.
314 handle_ra_miss(mapping, &in->f_ra, index);
317 * page didn't exist, allocate one.
319 page = page_cache_alloc_cold(mapping);
323 error = add_to_page_cache_lru(page, mapping, index,
324 mapping_gfp_mask(mapping));
325 if (unlikely(error)) {
326 page_cache_release(page);
330 * add_to_page_cache() locks the page, unlock it
331 * to avoid convoluting the logic below even more.
336 pages[spd.nr_pages++] = page;
341 * Now loop over the map and see if we need to start IO on any
342 * pages, fill in the partial map, etc.
344 index = *ppos >> PAGE_CACHE_SHIFT;
345 nr_pages = spd.nr_pages;
347 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
348 unsigned int this_len;
354 * this_len is the max we'll use from this page
356 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
357 page = pages[page_nr];
360 * If the page isn't uptodate, we may need to start io on it
362 if (!PageUptodate(page)) {
364 * If in nonblock mode then dont block on waiting
365 * for an in-flight io page
367 if (flags & SPLICE_F_NONBLOCK)
373 * page was truncated, stop here. if this isn't the
374 * first page, we'll just complete what we already
377 if (!page->mapping) {
382 * page was already under io and is now done, great
384 if (PageUptodate(page)) {
390 * need to read in the page
392 error = mapping->a_ops->readpage(in, page);
393 if (unlikely(error)) {
395 * We really should re-lookup the page here,
396 * but it complicates things a lot. Instead
397 * lets just do what we already stored, and
398 * we'll get it the next time we are called.
400 if (error == AOP_TRUNCATED_PAGE)
407 * i_size must be checked after ->readpage().
409 isize = i_size_read(mapping->host);
410 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
411 if (unlikely(!isize || index > end_index))
415 * if this is the last page, see if we need to shrink
416 * the length and stop
418 if (end_index == index) {
419 loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
420 if (total_len + loff > isize)
423 * force quit after adding this page
426 this_len = min(this_len, loff);
431 partial[page_nr].offset = loff;
432 partial[page_nr].len = this_len;
434 total_len += this_len;
441 * Release any pages at the end, if we quit early. 'i' is how far
442 * we got, 'nr_pages' is how many pages are in the map.
444 while (page_nr < nr_pages)
445 page_cache_release(pages[page_nr++]);
448 return splice_to_pipe(pipe, &spd);
454 * generic_file_splice_read - splice data from file to a pipe
455 * @in: file to splice from
456 * @pipe: pipe to splice to
457 * @len: number of bytes to splice
458 * @flags: splice modifier flags
460 * Will read pages from given file and fill them into a pipe.
462 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
463 struct pipe_inode_info *pipe, size_t len,
473 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
480 if (flags & SPLICE_F_NONBLOCK) {
497 EXPORT_SYMBOL(generic_file_splice_read);
500 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
501 * using sendpage(). Return the number of bytes sent.
503 static int pipe_to_sendpage(struct pipe_inode_info *info,
504 struct pipe_buffer *buf, struct splice_desc *sd)
506 struct file *file = sd->file;
507 loff_t pos = sd->pos;
510 ret = buf->ops->pin(info, buf);
512 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
514 ret = file->f_op->sendpage(file, buf->page, buf->offset,
515 sd->len, &pos, more);
522 * This is a little more tricky than the file -> pipe splicing. There are
523 * basically three cases:
525 * - Destination page already exists in the address space and there
526 * are users of it. For that case we have no other option that
527 * copying the data. Tough luck.
528 * - Destination page already exists in the address space, but there
529 * are no users of it. Make sure it's uptodate, then drop it. Fall
530 * through to last case.
531 * - Destination page does not exist, we can add the pipe page to
532 * the page cache and avoid the copy.
534 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
535 * sd->flags), we attempt to migrate pages from the pipe to the output
536 * file address space page cache. This is possible if no one else has
537 * the pipe page referenced outside of the pipe and page cache. If
538 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
539 * a new page in the output file page cache and fill/dirty that.
541 static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
542 struct splice_desc *sd)
544 struct file *file = sd->file;
545 struct address_space *mapping = file->f_mapping;
546 gfp_t gfp_mask = mapping_gfp_mask(mapping);
547 unsigned int offset, this_len;
553 * make sure the data in this buffer is uptodate
555 ret = buf->ops->pin(info, buf);
559 index = sd->pos >> PAGE_CACHE_SHIFT;
560 offset = sd->pos & ~PAGE_CACHE_MASK;
563 if (this_len + offset > PAGE_CACHE_SIZE)
564 this_len = PAGE_CACHE_SIZE - offset;
567 * Reuse buf page, if SPLICE_F_MOVE is set and we are doing a full
570 if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
572 * If steal succeeds, buf->page is now pruned from the vm
573 * side (LRU and page cache) and we can reuse it. The page
574 * will also be looked on successful return.
576 if (buf->ops->steal(info, buf))
580 if (add_to_page_cache(page, mapping, index, gfp_mask)) {
585 page_cache_get(page);
587 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
591 page = find_lock_page(mapping, index);
594 page = page_cache_alloc_cold(mapping);
599 * This will also lock the page
601 ret = add_to_page_cache_lru(page, mapping, index,
608 * We get here with the page locked. If the page is also
609 * uptodate, we don't need to do more. If it isn't, we
610 * may need to bring it in if we are not going to overwrite
613 if (!PageUptodate(page)) {
614 if (this_len < PAGE_CACHE_SIZE) {
615 ret = mapping->a_ops->readpage(file, page);
621 if (!PageUptodate(page)) {
623 * Page got invalidated, repeat.
625 if (!page->mapping) {
627 page_cache_release(page);
634 SetPageUptodate(page);
638 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
639 if (ret == AOP_TRUNCATED_PAGE) {
640 page_cache_release(page);
645 if (buf->page != page) {
647 * Careful, ->map() uses KM_USER0!
649 char *src = buf->ops->map(info, buf, 1);
650 char *dst = kmap_atomic(page, KM_USER1);
652 memcpy(dst + offset, src + buf->offset, this_len);
653 flush_dcache_page(page);
654 kunmap_atomic(dst, KM_USER1);
655 buf->ops->unmap(info, buf, src);
658 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
661 * Return the number of bytes written and mark page as
662 * accessed, we are now done!
665 mark_page_accessed(page);
666 balance_dirty_pages_ratelimited(mapping);
667 } else if (ret == AOP_TRUNCATED_PAGE) {
668 page_cache_release(page);
672 page_cache_release(page);
679 * Pipe input worker. Most of this logic works like a regular pipe, the
680 * key here is the 'actor' worker passed in that actually moves the data
681 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
683 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
684 loff_t *ppos, size_t len, unsigned int flags,
687 int ret, do_wakeup, err;
688 struct splice_desc sd;
699 mutex_lock(&pipe->inode->i_mutex);
703 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
704 struct pipe_buf_operations *ops = buf->ops;
707 if (sd.len > sd.total_len)
708 sd.len = sd.total_len;
710 err = actor(pipe, buf, &sd);
712 if (!ret && err != -ENODATA)
730 ops->release(pipe, buf);
731 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
745 if (!pipe->waiting_writers) {
750 if (flags & SPLICE_F_NONBLOCK) {
756 if (signal_pending(current)) {
764 if (waitqueue_active(&pipe->wait))
765 wake_up_interruptible_sync(&pipe->wait);
766 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
774 mutex_unlock(&pipe->inode->i_mutex);
778 if (waitqueue_active(&pipe->wait))
779 wake_up_interruptible(&pipe->wait);
780 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
787 * generic_file_splice_write - splice data from a pipe to a file
789 * @out: file to write to
790 * @len: number of bytes to splice
791 * @flags: splice modifier flags
793 * Will either move or copy pages (determined by @flags options) from
794 * the given pipe inode to the given file.
798 generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
799 loff_t *ppos, size_t len, unsigned int flags)
801 struct address_space *mapping = out->f_mapping;
804 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
806 struct inode *inode = mapping->host;
811 * If file or inode is SYNC and we actually wrote some data,
814 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
817 mutex_lock(&inode->i_mutex);
818 err = generic_osync_inode(inode, mapping,
819 OSYNC_METADATA|OSYNC_DATA);
820 mutex_unlock(&inode->i_mutex);
830 EXPORT_SYMBOL(generic_file_splice_write);
833 * generic_splice_sendpage - splice data from a pipe to a socket
835 * @out: socket to write to
836 * @len: number of bytes to splice
837 * @flags: splice modifier flags
839 * Will send @len bytes from the pipe to a network socket. No data copying
843 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
844 loff_t *ppos, size_t len, unsigned int flags)
846 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
849 EXPORT_SYMBOL(generic_splice_sendpage);
852 * Attempt to initiate a splice from pipe to file.
854 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
855 loff_t *ppos, size_t len, unsigned int flags)
859 if (unlikely(!out->f_op || !out->f_op->splice_write))
862 if (unlikely(!(out->f_mode & FMODE_WRITE)))
865 ret = rw_verify_area(WRITE, out, ppos, len);
866 if (unlikely(ret < 0))
869 return out->f_op->splice_write(pipe, out, ppos, len, flags);
873 * Attempt to initiate a splice from a file to a pipe.
875 static long do_splice_to(struct file *in, loff_t *ppos,
876 struct pipe_inode_info *pipe, size_t len,
882 if (unlikely(!in->f_op || !in->f_op->splice_read))
885 if (unlikely(!(in->f_mode & FMODE_READ)))
888 ret = rw_verify_area(READ, in, ppos, len);
889 if (unlikely(ret < 0))
892 isize = i_size_read(in->f_mapping->host);
893 if (unlikely(*ppos >= isize))
896 left = isize - *ppos;
897 if (unlikely(left < len))
900 return in->f_op->splice_read(in, ppos, pipe, len, flags);
903 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
904 size_t len, unsigned int flags)
906 struct pipe_inode_info *pipe;
913 * We require the input being a regular file, as we don't want to
914 * randomly drop data for eg socket -> socket splicing. Use the
915 * piped splicing for that!
917 i_mode = in->f_dentry->d_inode->i_mode;
918 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
922 * neither in nor out is a pipe, setup an internal pipe attached to
923 * 'out' and transfer the wanted data from 'in' to 'out' through that
925 pipe = current->splice_pipe;
926 if (unlikely(!pipe)) {
927 pipe = alloc_pipe_info(NULL);
932 * We don't have an immediate reader, but we'll read the stuff
933 * out of the pipe right after the splice_to_pipe(). So set
934 * PIPE_READERS appropriately.
938 current->splice_pipe = pipe;
949 size_t read_len, max_read_len;
952 * Do at most PIPE_BUFFERS pages worth of transfer:
954 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
956 ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
957 if (unlikely(ret < 0))
963 * NOTE: nonblocking mode only applies to the input. We
964 * must not do the output in nonblocking mode as then we
965 * could get stuck data in the internal pipe:
967 ret = do_splice_from(pipe, out, &out_off, read_len,
968 flags & ~SPLICE_F_NONBLOCK);
969 if (unlikely(ret < 0))
976 * In nonblocking mode, if we got back a short read then
977 * that was due to either an IO error or due to the
978 * pagecache entry not being there. In the IO error case
979 * the _next_ splice attempt will produce a clean IO error
980 * return value (not a short read), so in both cases it's
981 * correct to break out of the loop here:
983 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
987 pipe->nrbufs = pipe->curbuf = 0;
993 * If we did an incomplete transfer we must release
994 * the pipe buffers in question:
996 for (i = 0; i < PIPE_BUFFERS; i++) {
997 struct pipe_buffer *buf = pipe->bufs + i;
1000 buf->ops->release(pipe, buf);
1004 pipe->nrbufs = pipe->curbuf = 0;
1007 * If we transferred some data, return the number of bytes:
1015 EXPORT_SYMBOL(do_splice_direct);
1018 * Determine where to splice to/from.
1020 static long do_splice(struct file *in, loff_t __user *off_in,
1021 struct file *out, loff_t __user *off_out,
1022 size_t len, unsigned int flags)
1024 struct pipe_inode_info *pipe;
1025 loff_t offset, *off;
1028 pipe = in->f_dentry->d_inode->i_pipe;
1033 if (out->f_op->llseek == no_llseek)
1035 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1041 ret = do_splice_from(pipe, out, off, len, flags);
1043 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1049 pipe = out->f_dentry->d_inode->i_pipe;
1054 if (in->f_op->llseek == no_llseek)
1056 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1062 ret = do_splice_to(in, off, pipe, len, flags);
1064 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1074 * Map an iov into an array of pages and offset/length tupples. With the
1075 * partial_page structure, we can map several non-contiguous ranges into
1076 * our ones pages[] map instead of splitting that operation into pieces.
1077 * Could easily be exported as a generic helper for other users, in which
1078 * case one would probably want to add a 'max_nr_pages' parameter as well.
1080 static int get_iovec_page_array(const struct iovec __user *iov,
1081 unsigned int nr_vecs, struct page **pages,
1082 struct partial_page *partial, int aligned)
1084 int buffers = 0, error = 0;
1087 * It's ok to take the mmap_sem for reading, even
1088 * across a "get_user()".
1090 down_read(¤t->mm->mmap_sem);
1093 unsigned long off, npages;
1099 * Get user address base and length for this iovec.
1101 error = get_user(base, &iov->iov_base);
1102 if (unlikely(error))
1104 error = get_user(len, &iov->iov_len);
1105 if (unlikely(error))
1109 * Sanity check this iovec. 0 read succeeds.
1114 if (unlikely(!base))
1118 * Get this base offset and number of pages, then map
1119 * in the user pages.
1121 off = (unsigned long) base & ~PAGE_MASK;
1124 * If asked for alignment, the offset must be zero and the
1125 * length a multiple of the PAGE_SIZE.
1128 if (aligned && (off || len & ~PAGE_MASK))
1131 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1132 if (npages > PIPE_BUFFERS - buffers)
1133 npages = PIPE_BUFFERS - buffers;
1135 error = get_user_pages(current, current->mm,
1136 (unsigned long) base, npages, 0, 0,
1137 &pages[buffers], NULL);
1139 if (unlikely(error <= 0))
1143 * Fill this contiguous range into the partial page map.
1145 for (i = 0; i < error; i++) {
1146 const int plen = min_t(size_t, len, PAGE_SIZE) - off;
1148 partial[buffers].offset = off;
1149 partial[buffers].len = plen;
1157 * We didn't complete this iov, stop here since it probably
1158 * means we have to move some of this into a pipe to
1159 * be able to continue.
1165 * Don't continue if we mapped fewer pages than we asked for,
1166 * or if we mapped the max number of pages that we have
1169 if (error < npages || buffers == PIPE_BUFFERS)
1176 up_read(¤t->mm->mmap_sem);
1185 * vmsplice splices a user address range into a pipe. It can be thought of
1186 * as splice-from-memory, where the regular splice is splice-from-file (or
1187 * to file). In both cases the output is a pipe, naturally.
1189 * Note that vmsplice only supports splicing _from_ user memory to a pipe,
1190 * not the other way around. Splicing from user memory is a simple operation
1191 * that can be supported without any funky alignment restrictions or nasty
1192 * vm tricks. We simply map in the user memory and fill them into a pipe.
1193 * The reverse isn't quite as easy, though. There are two possible solutions
1196 * - memcpy() the data internally, at which point we might as well just
1197 * do a regular read() on the buffer anyway.
1198 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1199 * has restriction limitations on both ends of the pipe).
1201 * Alas, it isn't here.
1204 static long do_vmsplice(struct file *file, const struct iovec __user *iov,
1205 unsigned long nr_segs, unsigned int flags)
1207 struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
1208 struct page *pages[PIPE_BUFFERS];
1209 struct partial_page partial[PIPE_BUFFERS];
1210 struct splice_pipe_desc spd = {
1214 .ops = &user_page_pipe_buf_ops,
1217 if (unlikely(!pipe))
1219 if (unlikely(nr_segs > UIO_MAXIOV))
1221 else if (unlikely(!nr_segs))
1224 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1225 flags & SPLICE_F_GIFT);
1226 if (spd.nr_pages <= 0)
1227 return spd.nr_pages;
1229 return splice_to_pipe(pipe, &spd);
1232 asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1233 unsigned long nr_segs, unsigned int flags)
1240 file = fget_light(fd, &fput);
1242 if (file->f_mode & FMODE_WRITE)
1243 error = do_vmsplice(file, iov, nr_segs, flags);
1245 fput_light(file, fput);
1251 asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1252 int fd_out, loff_t __user *off_out,
1253 size_t len, unsigned int flags)
1256 struct file *in, *out;
1257 int fput_in, fput_out;
1263 in = fget_light(fd_in, &fput_in);
1265 if (in->f_mode & FMODE_READ) {
1266 out = fget_light(fd_out, &fput_out);
1268 if (out->f_mode & FMODE_WRITE)
1269 error = do_splice(in, off_in,
1272 fput_light(out, fput_out);
1276 fput_light(in, fput_in);
1283 * Link contents of ipipe to opipe.
1285 static int link_pipe(struct pipe_inode_info *ipipe,
1286 struct pipe_inode_info *opipe,
1287 size_t len, unsigned int flags)
1289 struct pipe_buffer *ibuf, *obuf;
1290 int ret, do_wakeup, i, ipipe_first;
1292 ret = do_wakeup = ipipe_first = 0;
1295 * Potential ABBA deadlock, work around it by ordering lock
1296 * grabbing by inode address. Otherwise two different processes
1297 * could deadlock (one doing tee from A -> B, the other from B -> A).
1299 if (ipipe->inode < opipe->inode) {
1301 mutex_lock(&ipipe->inode->i_mutex);
1302 mutex_lock(&opipe->inode->i_mutex);
1304 mutex_lock(&opipe->inode->i_mutex);
1305 mutex_lock(&ipipe->inode->i_mutex);
1309 if (!opipe->readers) {
1310 send_sig(SIGPIPE, current, 0);
1315 if (ipipe->nrbufs - i) {
1316 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1319 * If we have room, fill this buffer
1321 if (opipe->nrbufs < PIPE_BUFFERS) {
1322 int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1325 * Get a reference to this pipe buffer,
1326 * so we can copy the contents over.
1328 ibuf->ops->get(ipipe, ibuf);
1330 obuf = opipe->bufs + nbuf;
1334 * Don't inherit the gift flag, we need to
1335 * prevent multiple steals of this page.
1337 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1339 if (obuf->len > len)
1349 if (opipe->nrbufs < PIPE_BUFFERS)
1354 * We have input available, but no output room.
1355 * If we already copied data, return that. If we
1356 * need to drop the opipe lock, it must be ordered
1357 * last to avoid deadlocks.
1359 if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
1364 if (signal_pending(current)) {
1371 if (waitqueue_active(&opipe->wait))
1372 wake_up_interruptible(&opipe->wait);
1373 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1377 opipe->waiting_writers++;
1379 opipe->waiting_writers--;
1384 * No input buffers, do the usual checks for available
1385 * writers and blocking and wait if necessary
1387 if (!ipipe->writers)
1389 if (!ipipe->waiting_writers) {
1394 * pipe_wait() drops the ipipe mutex. To avoid deadlocks
1395 * with another process, we can only safely do that if
1396 * the ipipe lock is ordered last.
1398 if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
1403 if (signal_pending(current)) {
1409 if (waitqueue_active(&ipipe->wait))
1410 wake_up_interruptible_sync(&ipipe->wait);
1411 kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
1416 mutex_unlock(&ipipe->inode->i_mutex);
1417 mutex_unlock(&opipe->inode->i_mutex);
1421 if (waitqueue_active(&opipe->wait))
1422 wake_up_interruptible(&opipe->wait);
1423 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1430 * This is a tee(1) implementation that works on pipes. It doesn't copy
1431 * any data, it simply references the 'in' pages on the 'out' pipe.
1432 * The 'flags' used are the SPLICE_F_* variants, currently the only
1433 * applicable one is SPLICE_F_NONBLOCK.
1435 static long do_tee(struct file *in, struct file *out, size_t len,
1438 struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
1439 struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
1442 * Link ipipe to the two output pipes, consuming as we go along.
1445 return link_pipe(ipipe, opipe, len, flags);
1450 asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1459 in = fget_light(fdin, &fput_in);
1461 if (in->f_mode & FMODE_READ) {
1463 struct file *out = fget_light(fdout, &fput_out);
1466 if (out->f_mode & FMODE_WRITE)
1467 error = do_tee(in, out, len, flags);
1468 fput_light(out, fput_out);
1471 fput_light(in, fput_in);