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 .map
= generic_pipe_buf_map
,
140 .unmap
= generic_pipe_buf_unmap
,
141 .confirm
= page_cache_pipe_buf_confirm
,
142 .release
= page_cache_pipe_buf_release
,
143 .steal
= page_cache_pipe_buf_steal
,
144 .get
= generic_pipe_buf_get
,
147 static int user_page_pipe_buf_steal(struct pipe_inode_info
*pipe
,
148 struct pipe_buffer
*buf
)
150 if (!(buf
->flags
& PIPE_BUF_FLAG_GIFT
))
153 buf
->flags
|= PIPE_BUF_FLAG_LRU
;
154 return generic_pipe_buf_steal(pipe
, buf
);
157 static const struct pipe_buf_operations user_page_pipe_buf_ops
= {
159 .map
= generic_pipe_buf_map
,
160 .unmap
= generic_pipe_buf_unmap
,
161 .confirm
= generic_pipe_buf_confirm
,
162 .release
= page_cache_pipe_buf_release
,
163 .steal
= user_page_pipe_buf_steal
,
164 .get
= generic_pipe_buf_get
,
167 static void wakeup_pipe_readers(struct pipe_inode_info
*pipe
)
170 if (waitqueue_active(&pipe
->wait
))
171 wake_up_interruptible(&pipe
->wait
);
172 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
176 * splice_to_pipe - fill passed data into a pipe
177 * @pipe: pipe to fill
181 * @spd contains a map of pages and len/offset tuples, along with
182 * the struct pipe_buf_operations associated with these pages. This
183 * function will link that data to the pipe.
186 ssize_t
splice_to_pipe(struct pipe_inode_info
*pipe
,
187 struct splice_pipe_desc
*spd
)
189 unsigned int spd_pages
= spd
->nr_pages
;
190 int ret
, do_wakeup
, page_nr
;
202 if (!pipe
->readers
) {
203 send_sig(SIGPIPE
, current
, 0);
209 if (pipe
->nrbufs
< pipe
->buffers
) {
210 int newbuf
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
211 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
213 buf
->page
= spd
->pages
[page_nr
];
214 buf
->offset
= spd
->partial
[page_nr
].offset
;
215 buf
->len
= spd
->partial
[page_nr
].len
;
216 buf
->private = spd
->partial
[page_nr
].private;
219 if (spd
->flags
& SPLICE_F_GIFT
)
220 buf
->flags
|= PIPE_BUF_FLAG_GIFT
;
229 if (!--spd
->nr_pages
)
231 if (pipe
->nrbufs
< pipe
->buffers
)
237 if (spd
->flags
& SPLICE_F_NONBLOCK
) {
243 if (signal_pending(current
)) {
251 if (waitqueue_active(&pipe
->wait
))
252 wake_up_interruptible_sync(&pipe
->wait
);
253 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
257 pipe
->waiting_writers
++;
259 pipe
->waiting_writers
--;
265 wakeup_pipe_readers(pipe
);
267 while (page_nr
< spd_pages
)
268 spd
->spd_release(spd
, page_nr
++);
273 void spd_release_page(struct splice_pipe_desc
*spd
, unsigned int i
)
275 page_cache_release(spd
->pages
[i
]);
279 * Check if we need to grow the arrays holding pages and partial page
282 int splice_grow_spd(const struct pipe_inode_info
*pipe
, struct splice_pipe_desc
*spd
)
284 unsigned int buffers
= ACCESS_ONCE(pipe
->buffers
);
286 spd
->nr_pages_max
= buffers
;
287 if (buffers
<= PIPE_DEF_BUFFERS
)
290 spd
->pages
= kmalloc(buffers
* sizeof(struct page
*), GFP_KERNEL
);
291 spd
->partial
= kmalloc(buffers
* sizeof(struct partial_page
), GFP_KERNEL
);
293 if (spd
->pages
&& spd
->partial
)
301 void splice_shrink_spd(struct splice_pipe_desc
*spd
)
303 if (spd
->nr_pages_max
<= PIPE_DEF_BUFFERS
)
311 __generic_file_splice_read(struct file
*in
, loff_t
*ppos
,
312 struct pipe_inode_info
*pipe
, size_t len
,
315 struct address_space
*mapping
= in
->f_mapping
;
316 unsigned int loff
, nr_pages
, req_pages
;
317 struct page
*pages
[PIPE_DEF_BUFFERS
];
318 struct partial_page partial
[PIPE_DEF_BUFFERS
];
320 pgoff_t index
, end_index
;
323 struct splice_pipe_desc spd
= {
326 .nr_pages_max
= PIPE_DEF_BUFFERS
,
328 .ops
= &page_cache_pipe_buf_ops
,
329 .spd_release
= spd_release_page
,
332 if (splice_grow_spd(pipe
, &spd
))
335 index
= *ppos
>> PAGE_CACHE_SHIFT
;
336 loff
= *ppos
& ~PAGE_CACHE_MASK
;
337 req_pages
= (len
+ loff
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
338 nr_pages
= min(req_pages
, spd
.nr_pages_max
);
341 * Lookup the (hopefully) full range of pages we need.
343 spd
.nr_pages
= find_get_pages_contig(mapping
, index
, nr_pages
, spd
.pages
);
344 index
+= spd
.nr_pages
;
347 * If find_get_pages_contig() returned fewer pages than we needed,
348 * readahead/allocate the rest and fill in the holes.
350 if (spd
.nr_pages
< nr_pages
)
351 page_cache_sync_readahead(mapping
, &in
->f_ra
, in
,
352 index
, req_pages
- spd
.nr_pages
);
355 while (spd
.nr_pages
< nr_pages
) {
357 * Page could be there, find_get_pages_contig() breaks on
360 page
= find_get_page(mapping
, index
);
363 * page didn't exist, allocate one.
365 page
= page_cache_alloc_cold(mapping
);
369 error
= add_to_page_cache_lru(page
, mapping
, index
,
371 if (unlikely(error
)) {
372 page_cache_release(page
);
373 if (error
== -EEXIST
)
378 * add_to_page_cache() locks the page, unlock it
379 * to avoid convoluting the logic below even more.
384 spd
.pages
[spd
.nr_pages
++] = page
;
389 * Now loop over the map and see if we need to start IO on any
390 * pages, fill in the partial map, etc.
392 index
= *ppos
>> PAGE_CACHE_SHIFT
;
393 nr_pages
= spd
.nr_pages
;
395 for (page_nr
= 0; page_nr
< nr_pages
; page_nr
++) {
396 unsigned int this_len
;
402 * this_len is the max we'll use from this page
404 this_len
= min_t(unsigned long, len
, PAGE_CACHE_SIZE
- loff
);
405 page
= spd
.pages
[page_nr
];
407 if (PageReadahead(page
))
408 page_cache_async_readahead(mapping
, &in
->f_ra
, in
,
409 page
, index
, req_pages
- page_nr
);
412 * If the page isn't uptodate, we may need to start io on it
414 if (!PageUptodate(page
)) {
418 * Page was truncated, or invalidated by the
419 * filesystem. Redo the find/create, but this time the
420 * page is kept locked, so there's no chance of another
421 * race with truncate/invalidate.
423 if (!page
->mapping
) {
425 page
= find_or_create_page(mapping
, index
,
426 mapping_gfp_mask(mapping
));
432 page_cache_release(spd
.pages
[page_nr
]);
433 spd
.pages
[page_nr
] = page
;
436 * page was already under io and is now done, great
438 if (PageUptodate(page
)) {
444 * need to read in the page
446 error
= mapping
->a_ops
->readpage(in
, page
);
447 if (unlikely(error
)) {
449 * We really should re-lookup the page here,
450 * but it complicates things a lot. Instead
451 * lets just do what we already stored, and
452 * we'll get it the next time we are called.
454 if (error
== AOP_TRUNCATED_PAGE
)
462 * i_size must be checked after PageUptodate.
464 isize
= i_size_read(mapping
->host
);
465 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
466 if (unlikely(!isize
|| index
> end_index
))
470 * if this is the last page, see if we need to shrink
471 * the length and stop
473 if (end_index
== index
) {
477 * max good bytes in this page
479 plen
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
484 * force quit after adding this page
486 this_len
= min(this_len
, plen
- loff
);
490 spd
.partial
[page_nr
].offset
= loff
;
491 spd
.partial
[page_nr
].len
= this_len
;
499 * Release any pages at the end, if we quit early. 'page_nr' is how far
500 * we got, 'nr_pages' is how many pages are in the map.
502 while (page_nr
< nr_pages
)
503 page_cache_release(spd
.pages
[page_nr
++]);
504 in
->f_ra
.prev_pos
= (loff_t
)index
<< PAGE_CACHE_SHIFT
;
507 error
= splice_to_pipe(pipe
, &spd
);
509 splice_shrink_spd(&spd
);
514 * generic_file_splice_read - splice data from file to a pipe
515 * @in: file to splice from
516 * @ppos: position in @in
517 * @pipe: pipe to splice to
518 * @len: number of bytes to splice
519 * @flags: splice modifier flags
522 * Will read pages from given file and fill them into a pipe. Can be
523 * used as long as the address_space operations for the source implements
527 ssize_t
generic_file_splice_read(struct file
*in
, loff_t
*ppos
,
528 struct pipe_inode_info
*pipe
, size_t len
,
534 isize
= i_size_read(in
->f_mapping
->host
);
535 if (unlikely(*ppos
>= isize
))
538 left
= isize
- *ppos
;
539 if (unlikely(left
< len
))
542 ret
= __generic_file_splice_read(in
, ppos
, pipe
, len
, flags
);
550 EXPORT_SYMBOL(generic_file_splice_read
);
552 static const struct pipe_buf_operations default_pipe_buf_ops
= {
554 .map
= generic_pipe_buf_map
,
555 .unmap
= generic_pipe_buf_unmap
,
556 .confirm
= generic_pipe_buf_confirm
,
557 .release
= generic_pipe_buf_release
,
558 .steal
= generic_pipe_buf_steal
,
559 .get
= generic_pipe_buf_get
,
562 static int generic_pipe_buf_nosteal(struct pipe_inode_info
*pipe
,
563 struct pipe_buffer
*buf
)
568 /* Pipe buffer operations for a socket and similar. */
569 const struct pipe_buf_operations nosteal_pipe_buf_ops
= {
571 .map
= generic_pipe_buf_map
,
572 .unmap
= generic_pipe_buf_unmap
,
573 .confirm
= generic_pipe_buf_confirm
,
574 .release
= generic_pipe_buf_release
,
575 .steal
= generic_pipe_buf_nosteal
,
576 .get
= generic_pipe_buf_get
,
578 EXPORT_SYMBOL(nosteal_pipe_buf_ops
);
580 static ssize_t
kernel_readv(struct file
*file
, const struct iovec
*vec
,
581 unsigned long vlen
, loff_t offset
)
589 /* The cast to a user pointer is valid due to the set_fs() */
590 res
= vfs_readv(file
, (const struct iovec __user
*)vec
, vlen
, &pos
);
596 ssize_t
kernel_write(struct file
*file
, const char *buf
, size_t count
,
604 /* The cast to a user pointer is valid due to the set_fs() */
605 res
= vfs_write(file
, (__force
const char __user
*)buf
, count
, &pos
);
610 EXPORT_SYMBOL(kernel_write
);
612 ssize_t
default_file_splice_read(struct file
*in
, loff_t
*ppos
,
613 struct pipe_inode_info
*pipe
, size_t len
,
616 unsigned int nr_pages
;
617 unsigned int nr_freed
;
619 struct page
*pages
[PIPE_DEF_BUFFERS
];
620 struct partial_page partial
[PIPE_DEF_BUFFERS
];
621 struct iovec
*vec
, __vec
[PIPE_DEF_BUFFERS
];
626 struct splice_pipe_desc spd
= {
629 .nr_pages_max
= PIPE_DEF_BUFFERS
,
631 .ops
= &default_pipe_buf_ops
,
632 .spd_release
= spd_release_page
,
635 if (splice_grow_spd(pipe
, &spd
))
640 if (spd
.nr_pages_max
> PIPE_DEF_BUFFERS
) {
641 vec
= kmalloc(spd
.nr_pages_max
* sizeof(struct iovec
), GFP_KERNEL
);
646 offset
= *ppos
& ~PAGE_CACHE_MASK
;
647 nr_pages
= (len
+ offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
649 for (i
= 0; i
< nr_pages
&& i
< spd
.nr_pages_max
&& len
; i
++) {
652 page
= alloc_page(GFP_USER
);
657 this_len
= min_t(size_t, len
, PAGE_CACHE_SIZE
- offset
);
658 vec
[i
].iov_base
= (void __user
*) page_address(page
);
659 vec
[i
].iov_len
= this_len
;
666 res
= kernel_readv(in
, vec
, spd
.nr_pages
, *ppos
);
677 for (i
= 0; i
< spd
.nr_pages
; i
++) {
678 this_len
= min_t(size_t, vec
[i
].iov_len
, res
);
679 spd
.partial
[i
].offset
= 0;
680 spd
.partial
[i
].len
= this_len
;
682 __free_page(spd
.pages
[i
]);
688 spd
.nr_pages
-= nr_freed
;
690 res
= splice_to_pipe(pipe
, &spd
);
697 splice_shrink_spd(&spd
);
701 for (i
= 0; i
< spd
.nr_pages
; i
++)
702 __free_page(spd
.pages
[i
]);
707 EXPORT_SYMBOL(default_file_splice_read
);
710 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
711 * using sendpage(). Return the number of bytes sent.
713 static int pipe_to_sendpage(struct pipe_inode_info
*pipe
,
714 struct pipe_buffer
*buf
, struct splice_desc
*sd
)
716 struct file
*file
= sd
->u
.file
;
717 loff_t pos
= sd
->pos
;
720 if (!likely(file
->f_op
&& file
->f_op
->sendpage
))
723 more
= (sd
->flags
& SPLICE_F_MORE
) ? MSG_MORE
: 0;
725 if (sd
->len
< sd
->total_len
&& pipe
->nrbufs
> 1)
726 more
|= MSG_SENDPAGE_NOTLAST
;
728 return file
->f_op
->sendpage(file
, buf
->page
, buf
->offset
,
729 sd
->len
, &pos
, more
);
733 * This is a little more tricky than the file -> pipe splicing. There are
734 * basically three cases:
736 * - Destination page already exists in the address space and there
737 * are users of it. For that case we have no other option that
738 * copying the data. Tough luck.
739 * - Destination page already exists in the address space, but there
740 * are no users of it. Make sure it's uptodate, then drop it. Fall
741 * through to last case.
742 * - Destination page does not exist, we can add the pipe page to
743 * the page cache and avoid the copy.
745 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
746 * sd->flags), we attempt to migrate pages from the pipe to the output
747 * file address space page cache. This is possible if no one else has
748 * the pipe page referenced outside of the pipe and page cache. If
749 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
750 * a new page in the output file page cache and fill/dirty that.
752 int pipe_to_file(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
753 struct splice_desc
*sd
)
755 struct file
*file
= sd
->u
.file
;
756 struct address_space
*mapping
= file
->f_mapping
;
757 unsigned int offset
, this_len
;
762 offset
= sd
->pos
& ~PAGE_CACHE_MASK
;
765 if (this_len
+ offset
> PAGE_CACHE_SIZE
)
766 this_len
= PAGE_CACHE_SIZE
- offset
;
768 ret
= pagecache_write_begin(file
, mapping
, sd
->pos
, this_len
,
769 AOP_FLAG_UNINTERRUPTIBLE
, &page
, &fsdata
);
773 if (buf
->page
!= page
) {
774 char *src
= buf
->ops
->map(pipe
, buf
, 1);
775 char *dst
= kmap_atomic(page
);
777 memcpy(dst
+ offset
, src
+ buf
->offset
, this_len
);
778 flush_dcache_page(page
);
780 buf
->ops
->unmap(pipe
, buf
, src
);
782 ret
= pagecache_write_end(file
, mapping
, sd
->pos
, this_len
, this_len
,
787 EXPORT_SYMBOL(pipe_to_file
);
789 static void wakeup_pipe_writers(struct pipe_inode_info
*pipe
)
792 if (waitqueue_active(&pipe
->wait
))
793 wake_up_interruptible(&pipe
->wait
);
794 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
798 * splice_from_pipe_feed - feed available data from a pipe to a file
799 * @pipe: pipe to splice from
800 * @sd: information to @actor
801 * @actor: handler that splices the data
804 * This function loops over the pipe and calls @actor to do the
805 * actual moving of a single struct pipe_buffer to the desired
806 * destination. It returns when there's no more buffers left in
807 * the pipe or if the requested number of bytes (@sd->total_len)
808 * have been copied. It returns a positive number (one) if the
809 * pipe needs to be filled with more data, zero if the required
810 * number of bytes have been copied and -errno on error.
812 * This, together with splice_from_pipe_{begin,end,next}, may be
813 * used to implement the functionality of __splice_from_pipe() when
814 * locking is required around copying the pipe buffers to the
817 int splice_from_pipe_feed(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
,
822 while (pipe
->nrbufs
) {
823 struct pipe_buffer
*buf
= pipe
->bufs
+ pipe
->curbuf
;
824 const struct pipe_buf_operations
*ops
= buf
->ops
;
827 if (sd
->len
> sd
->total_len
)
828 sd
->len
= sd
->total_len
;
830 ret
= buf
->ops
->confirm(pipe
, buf
);
837 ret
= actor(pipe
, buf
, sd
);
844 sd
->num_spliced
+= ret
;
847 sd
->total_len
-= ret
;
851 ops
->release(pipe
, buf
);
852 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
855 sd
->need_wakeup
= true;
864 EXPORT_SYMBOL(splice_from_pipe_feed
);
867 * splice_from_pipe_next - wait for some data to splice from
868 * @pipe: pipe to splice from
869 * @sd: information about the splice operation
872 * This function will wait for some data and return a positive
873 * value (one) if pipe buffers are available. It will return zero
874 * or -errno if no more data needs to be spliced.
876 int splice_from_pipe_next(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
)
878 while (!pipe
->nrbufs
) {
882 if (!pipe
->waiting_writers
&& sd
->num_spliced
)
885 if (sd
->flags
& SPLICE_F_NONBLOCK
)
888 if (signal_pending(current
))
891 if (sd
->need_wakeup
) {
892 wakeup_pipe_writers(pipe
);
893 sd
->need_wakeup
= false;
901 EXPORT_SYMBOL(splice_from_pipe_next
);
904 * splice_from_pipe_begin - start splicing from pipe
905 * @sd: information about the splice operation
908 * This function should be called before a loop containing
909 * splice_from_pipe_next() and splice_from_pipe_feed() to
910 * initialize the necessary fields of @sd.
912 void splice_from_pipe_begin(struct splice_desc
*sd
)
915 sd
->need_wakeup
= false;
917 EXPORT_SYMBOL(splice_from_pipe_begin
);
920 * splice_from_pipe_end - finish splicing from pipe
921 * @pipe: pipe to splice from
922 * @sd: information about the splice operation
925 * This function will wake up pipe writers if necessary. It should
926 * be called after a loop containing splice_from_pipe_next() and
927 * splice_from_pipe_feed().
929 void splice_from_pipe_end(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
)
932 wakeup_pipe_writers(pipe
);
934 EXPORT_SYMBOL(splice_from_pipe_end
);
937 * __splice_from_pipe - splice data from a pipe to given actor
938 * @pipe: pipe to splice from
939 * @sd: information to @actor
940 * @actor: handler that splices the data
943 * This function does little more than loop over the pipe and call
944 * @actor to do the actual moving of a single struct pipe_buffer to
945 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
949 ssize_t
__splice_from_pipe(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
,
954 splice_from_pipe_begin(sd
);
957 ret
= splice_from_pipe_next(pipe
, sd
);
959 ret
= splice_from_pipe_feed(pipe
, sd
, actor
);
961 splice_from_pipe_end(pipe
, sd
);
963 return sd
->num_spliced
? sd
->num_spliced
: ret
;
965 EXPORT_SYMBOL(__splice_from_pipe
);
968 * splice_from_pipe - splice data from a pipe to a file
969 * @pipe: pipe to splice from
970 * @out: file to splice to
971 * @ppos: position in @out
972 * @len: how many bytes to splice
973 * @flags: splice modifier flags
974 * @actor: handler that splices the data
977 * See __splice_from_pipe. This function locks the pipe inode,
978 * otherwise it's identical to __splice_from_pipe().
981 ssize_t
splice_from_pipe(struct pipe_inode_info
*pipe
, struct file
*out
,
982 loff_t
*ppos
, size_t len
, unsigned int flags
,
986 struct splice_desc sd
= {
994 ret
= __splice_from_pipe(pipe
, &sd
, actor
);
1001 * generic_file_splice_write - splice data from a pipe to a file
1003 * @out: file to write to
1004 * @ppos: position in @out
1005 * @len: number of bytes to splice
1006 * @flags: splice modifier flags
1009 * Will either move or copy pages (determined by @flags options) from
1010 * the given pipe inode to the given file.
1014 generic_file_splice_write(struct pipe_inode_info
*pipe
, struct file
*out
,
1015 loff_t
*ppos
, size_t len
, unsigned int flags
)
1017 struct address_space
*mapping
= out
->f_mapping
;
1018 struct inode
*inode
= mapping
->host
;
1019 struct splice_desc sd
= {
1025 ret
= generic_write_checks(out
, ppos
, &len
, S_ISBLK(inode
->i_mode
));
1033 splice_from_pipe_begin(&sd
);
1035 ret
= splice_from_pipe_next(pipe
, &sd
);
1039 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1040 ret
= file_remove_suid(out
);
1042 ret
= file_update_time(out
);
1044 ret
= splice_from_pipe_feed(pipe
, &sd
,
1047 mutex_unlock(&inode
->i_mutex
);
1049 splice_from_pipe_end(pipe
, &sd
);
1054 ret
= sd
.num_spliced
;
1059 err
= generic_write_sync(out
, *ppos
, ret
);
1064 balance_dirty_pages_ratelimited(mapping
);
1070 EXPORT_SYMBOL(generic_file_splice_write
);
1072 static int write_pipe_buf(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
1073 struct splice_desc
*sd
)
1077 loff_t tmp
= sd
->pos
;
1079 data
= buf
->ops
->map(pipe
, buf
, 0);
1080 ret
= __kernel_write(sd
->u
.file
, data
+ buf
->offset
, sd
->len
, &tmp
);
1081 buf
->ops
->unmap(pipe
, buf
, data
);
1086 static ssize_t
default_file_splice_write(struct pipe_inode_info
*pipe
,
1087 struct file
*out
, loff_t
*ppos
,
1088 size_t len
, unsigned int flags
)
1092 ret
= splice_from_pipe(pipe
, out
, ppos
, len
, flags
, write_pipe_buf
);
1100 * generic_splice_sendpage - splice data from a pipe to a socket
1101 * @pipe: pipe to splice from
1102 * @out: socket to write to
1103 * @ppos: position in @out
1104 * @len: number of bytes to splice
1105 * @flags: splice modifier flags
1108 * Will send @len bytes from the pipe to a network socket. No data copying
1112 ssize_t
generic_splice_sendpage(struct pipe_inode_info
*pipe
, struct file
*out
,
1113 loff_t
*ppos
, size_t len
, unsigned int flags
)
1115 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_sendpage
);
1118 EXPORT_SYMBOL(generic_splice_sendpage
);
1121 * Attempt to initiate a splice from pipe to file.
1123 static long do_splice_from(struct pipe_inode_info
*pipe
, struct file
*out
,
1124 loff_t
*ppos
, size_t len
, unsigned int flags
)
1126 ssize_t (*splice_write
)(struct pipe_inode_info
*, struct file
*,
1127 loff_t
*, size_t, unsigned int);
1130 if (unlikely(!(out
->f_mode
& FMODE_WRITE
)))
1133 if (unlikely(out
->f_flags
& O_APPEND
))
1136 ret
= rw_verify_area(WRITE
, out
, ppos
, len
);
1137 if (unlikely(ret
< 0))
1140 if (out
->f_op
&& out
->f_op
->splice_write
)
1141 splice_write
= out
->f_op
->splice_write
;
1143 splice_write
= default_file_splice_write
;
1145 file_start_write(out
);
1146 ret
= splice_write(pipe
, out
, ppos
, len
, flags
);
1147 file_end_write(out
);
1152 * Attempt to initiate a splice from a file to a pipe.
1154 static long do_splice_to(struct file
*in
, loff_t
*ppos
,
1155 struct pipe_inode_info
*pipe
, size_t len
,
1158 ssize_t (*splice_read
)(struct file
*, loff_t
*,
1159 struct pipe_inode_info
*, size_t, unsigned int);
1162 if (unlikely(!(in
->f_mode
& FMODE_READ
)))
1165 ret
= rw_verify_area(READ
, in
, ppos
, len
);
1166 if (unlikely(ret
< 0))
1169 if (in
->f_op
&& in
->f_op
->splice_read
)
1170 splice_read
= in
->f_op
->splice_read
;
1172 splice_read
= default_file_splice_read
;
1174 return splice_read(in
, ppos
, pipe
, len
, flags
);
1178 * splice_direct_to_actor - splices data directly between two non-pipes
1179 * @in: file to splice from
1180 * @sd: actor information on where to splice to
1181 * @actor: handles the data splicing
1184 * This is a special case helper to splice directly between two
1185 * points, without requiring an explicit pipe. Internally an allocated
1186 * pipe is cached in the process, and reused during the lifetime of
1190 ssize_t
splice_direct_to_actor(struct file
*in
, struct splice_desc
*sd
,
1191 splice_direct_actor
*actor
)
1193 struct pipe_inode_info
*pipe
;
1200 * We require the input being a regular file, as we don't want to
1201 * randomly drop data for eg socket -> socket splicing. Use the
1202 * piped splicing for that!
1204 i_mode
= file_inode(in
)->i_mode
;
1205 if (unlikely(!S_ISREG(i_mode
) && !S_ISBLK(i_mode
)))
1209 * neither in nor out is a pipe, setup an internal pipe attached to
1210 * 'out' and transfer the wanted data from 'in' to 'out' through that
1212 pipe
= current
->splice_pipe
;
1213 if (unlikely(!pipe
)) {
1214 pipe
= alloc_pipe_info();
1219 * We don't have an immediate reader, but we'll read the stuff
1220 * out of the pipe right after the splice_to_pipe(). So set
1221 * PIPE_READERS appropriately.
1225 current
->splice_pipe
= pipe
;
1233 len
= sd
->total_len
;
1237 * Don't block on output, we have to drain the direct pipe.
1239 sd
->flags
&= ~SPLICE_F_NONBLOCK
;
1240 more
= sd
->flags
& SPLICE_F_MORE
;
1244 loff_t pos
= sd
->pos
, prev_pos
= pos
;
1246 ret
= do_splice_to(in
, &pos
, pipe
, len
, flags
);
1247 if (unlikely(ret
<= 0))
1251 sd
->total_len
= read_len
;
1254 * If more data is pending, set SPLICE_F_MORE
1255 * If this is the last data and SPLICE_F_MORE was not set
1256 * initially, clears it.
1259 sd
->flags
|= SPLICE_F_MORE
;
1261 sd
->flags
&= ~SPLICE_F_MORE
;
1263 * NOTE: nonblocking mode only applies to the input. We
1264 * must not do the output in nonblocking mode as then we
1265 * could get stuck data in the internal pipe:
1267 ret
= actor(pipe
, sd
);
1268 if (unlikely(ret
<= 0)) {
1277 if (ret
< read_len
) {
1278 sd
->pos
= prev_pos
+ ret
;
1284 pipe
->nrbufs
= pipe
->curbuf
= 0;
1290 * If we did an incomplete transfer we must release
1291 * the pipe buffers in question:
1293 for (i
= 0; i
< pipe
->buffers
; i
++) {
1294 struct pipe_buffer
*buf
= pipe
->bufs
+ i
;
1297 buf
->ops
->release(pipe
, buf
);
1307 EXPORT_SYMBOL(splice_direct_to_actor
);
1309 static int direct_splice_actor(struct pipe_inode_info
*pipe
,
1310 struct splice_desc
*sd
)
1312 struct file
*file
= sd
->u
.file
;
1314 return do_splice_from(pipe
, file
, sd
->opos
, sd
->total_len
,
1319 * do_splice_direct - splices data directly between two files
1320 * @in: file to splice from
1321 * @ppos: input file offset
1322 * @out: file to splice to
1323 * @opos: output file offset
1324 * @len: number of bytes to splice
1325 * @flags: splice modifier flags
1328 * For use by do_sendfile(). splice can easily emulate sendfile, but
1329 * doing it in the application would incur an extra system call
1330 * (splice in + splice out, as compared to just sendfile()). So this helper
1331 * can splice directly through a process-private pipe.
1334 long do_splice_direct(struct file
*in
, loff_t
*ppos
, struct file
*out
,
1335 loff_t
*opos
, size_t len
, unsigned int flags
)
1337 struct splice_desc sd
= {
1347 ret
= splice_direct_to_actor(in
, &sd
, direct_splice_actor
);
1354 static int splice_pipe_to_pipe(struct pipe_inode_info
*ipipe
,
1355 struct pipe_inode_info
*opipe
,
1356 size_t len
, unsigned int flags
);
1359 * Determine where to splice to/from.
1361 static long do_splice(struct file
*in
, loff_t __user
*off_in
,
1362 struct file
*out
, loff_t __user
*off_out
,
1363 size_t len
, unsigned int flags
)
1365 struct pipe_inode_info
*ipipe
;
1366 struct pipe_inode_info
*opipe
;
1370 ipipe
= get_pipe_info(in
);
1371 opipe
= get_pipe_info(out
);
1373 if (ipipe
&& opipe
) {
1374 if (off_in
|| off_out
)
1377 if (!(in
->f_mode
& FMODE_READ
))
1380 if (!(out
->f_mode
& FMODE_WRITE
))
1383 /* Splicing to self would be fun, but... */
1387 return splice_pipe_to_pipe(ipipe
, opipe
, len
, flags
);
1394 if (!(out
->f_mode
& FMODE_PWRITE
))
1396 if (copy_from_user(&offset
, off_out
, sizeof(loff_t
)))
1399 offset
= out
->f_pos
;
1402 ret
= do_splice_from(ipipe
, out
, &offset
, len
, flags
);
1405 out
->f_pos
= offset
;
1406 else if (copy_to_user(off_out
, &offset
, sizeof(loff_t
)))
1416 if (!(in
->f_mode
& FMODE_PREAD
))
1418 if (copy_from_user(&offset
, off_in
, sizeof(loff_t
)))
1424 ret
= do_splice_to(in
, &offset
, opipe
, len
, flags
);
1428 else if (copy_to_user(off_in
, &offset
, sizeof(loff_t
)))
1438 * Map an iov into an array of pages and offset/length tupples. With the
1439 * partial_page structure, we can map several non-contiguous ranges into
1440 * our ones pages[] map instead of splitting that operation into pieces.
1441 * Could easily be exported as a generic helper for other users, in which
1442 * case one would probably want to add a 'max_nr_pages' parameter as well.
1444 static int get_iovec_page_array(const struct iovec __user
*iov
,
1445 unsigned int nr_vecs
, struct page
**pages
,
1446 struct partial_page
*partial
, bool aligned
,
1447 unsigned int pipe_buffers
)
1449 int buffers
= 0, error
= 0;
1452 unsigned long off
, npages
;
1459 if (copy_from_user(&entry
, iov
, sizeof(entry
)))
1462 base
= entry
.iov_base
;
1463 len
= entry
.iov_len
;
1466 * Sanity check this iovec. 0 read succeeds.
1472 if (!access_ok(VERIFY_READ
, base
, len
))
1476 * Get this base offset and number of pages, then map
1477 * in the user pages.
1479 off
= (unsigned long) base
& ~PAGE_MASK
;
1482 * If asked for alignment, the offset must be zero and the
1483 * length a multiple of the PAGE_SIZE.
1486 if (aligned
&& (off
|| len
& ~PAGE_MASK
))
1489 npages
= (off
+ len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1490 if (npages
> pipe_buffers
- buffers
)
1491 npages
= pipe_buffers
- buffers
;
1493 error
= get_user_pages_fast((unsigned long)base
, npages
,
1494 0, &pages
[buffers
]);
1496 if (unlikely(error
<= 0))
1500 * Fill this contiguous range into the partial page map.
1502 for (i
= 0; i
< error
; i
++) {
1503 const int plen
= min_t(size_t, len
, PAGE_SIZE
- off
);
1505 partial
[buffers
].offset
= off
;
1506 partial
[buffers
].len
= plen
;
1514 * We didn't complete this iov, stop here since it probably
1515 * means we have to move some of this into a pipe to
1516 * be able to continue.
1522 * Don't continue if we mapped fewer pages than we asked for,
1523 * or if we mapped the max number of pages that we have
1526 if (error
< npages
|| buffers
== pipe_buffers
)
1539 static int pipe_to_user(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
1540 struct splice_desc
*sd
)
1546 * See if we can use the atomic maps, by prefaulting in the
1547 * pages and doing an atomic copy
1549 if (!fault_in_pages_writeable(sd
->u
.userptr
, sd
->len
)) {
1550 src
= buf
->ops
->map(pipe
, buf
, 1);
1551 ret
= __copy_to_user_inatomic(sd
->u
.userptr
, src
+ buf
->offset
,
1553 buf
->ops
->unmap(pipe
, buf
, src
);
1561 * No dice, use slow non-atomic map and copy
1563 src
= buf
->ops
->map(pipe
, buf
, 0);
1566 if (copy_to_user(sd
->u
.userptr
, src
+ buf
->offset
, sd
->len
))
1569 buf
->ops
->unmap(pipe
, buf
, src
);
1572 sd
->u
.userptr
+= ret
;
1577 * For lack of a better implementation, implement vmsplice() to userspace
1578 * as a simple copy of the pipes pages to the user iov.
1580 static long vmsplice_to_user(struct file
*file
, const struct iovec __user
*iov
,
1581 unsigned long nr_segs
, unsigned int flags
)
1583 struct pipe_inode_info
*pipe
;
1584 struct splice_desc sd
;
1589 pipe
= get_pipe_info(file
);
1601 * Get user address base and length for this iovec.
1603 error
= get_user(base
, &iov
->iov_base
);
1604 if (unlikely(error
))
1606 error
= get_user(len
, &iov
->iov_len
);
1607 if (unlikely(error
))
1611 * Sanity check this iovec. 0 read succeeds.
1615 if (unlikely(!base
)) {
1620 if (unlikely(!access_ok(VERIFY_WRITE
, base
, len
))) {
1628 sd
.u
.userptr
= base
;
1631 size
= __splice_from_pipe(pipe
, &sd
, pipe_to_user
);
1657 * vmsplice splices a user address range into a pipe. It can be thought of
1658 * as splice-from-memory, where the regular splice is splice-from-file (or
1659 * to file). In both cases the output is a pipe, naturally.
1661 static long vmsplice_to_pipe(struct file
*file
, const struct iovec __user
*iov
,
1662 unsigned long nr_segs
, unsigned int flags
)
1664 struct pipe_inode_info
*pipe
;
1665 struct page
*pages
[PIPE_DEF_BUFFERS
];
1666 struct partial_page partial
[PIPE_DEF_BUFFERS
];
1667 struct splice_pipe_desc spd
= {
1670 .nr_pages_max
= PIPE_DEF_BUFFERS
,
1672 .ops
= &user_page_pipe_buf_ops
,
1673 .spd_release
= spd_release_page
,
1677 pipe
= get_pipe_info(file
);
1681 if (splice_grow_spd(pipe
, &spd
))
1684 spd
.nr_pages
= get_iovec_page_array(iov
, nr_segs
, spd
.pages
,
1687 if (spd
.nr_pages
<= 0)
1690 ret
= splice_to_pipe(pipe
, &spd
);
1692 splice_shrink_spd(&spd
);
1697 * Note that vmsplice only really supports true splicing _from_ user memory
1698 * to a pipe, not the other way around. Splicing from user memory is a simple
1699 * operation that can be supported without any funky alignment restrictions
1700 * or nasty vm tricks. We simply map in the user memory and fill them into
1701 * a pipe. The reverse isn't quite as easy, though. There are two possible
1702 * solutions for that:
1704 * - memcpy() the data internally, at which point we might as well just
1705 * do a regular read() on the buffer anyway.
1706 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1707 * has restriction limitations on both ends of the pipe).
1709 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1712 SYSCALL_DEFINE4(vmsplice
, int, fd
, const struct iovec __user
*, iov
,
1713 unsigned long, nr_segs
, unsigned int, flags
)
1718 if (unlikely(nr_segs
> UIO_MAXIOV
))
1720 else if (unlikely(!nr_segs
))
1726 if (f
.file
->f_mode
& FMODE_WRITE
)
1727 error
= vmsplice_to_pipe(f
.file
, iov
, nr_segs
, flags
);
1728 else if (f
.file
->f_mode
& FMODE_READ
)
1729 error
= vmsplice_to_user(f
.file
, iov
, nr_segs
, flags
);
1737 #ifdef CONFIG_COMPAT
1738 COMPAT_SYSCALL_DEFINE4(vmsplice
, int, fd
, const struct compat_iovec __user
*, iov32
,
1739 unsigned int, nr_segs
, unsigned int, flags
)
1742 struct iovec __user
*iov
;
1743 if (nr_segs
> UIO_MAXIOV
)
1745 iov
= compat_alloc_user_space(nr_segs
* sizeof(struct iovec
));
1746 for (i
= 0; i
< nr_segs
; i
++) {
1747 struct compat_iovec v
;
1748 if (get_user(v
.iov_base
, &iov32
[i
].iov_base
) ||
1749 get_user(v
.iov_len
, &iov32
[i
].iov_len
) ||
1750 put_user(compat_ptr(v
.iov_base
), &iov
[i
].iov_base
) ||
1751 put_user(v
.iov_len
, &iov
[i
].iov_len
))
1754 return sys_vmsplice(fd
, iov
, nr_segs
, flags
);
1758 SYSCALL_DEFINE6(splice
, int, fd_in
, loff_t __user
*, off_in
,
1759 int, fd_out
, loff_t __user
*, off_out
,
1760 size_t, len
, unsigned int, flags
)
1771 if (in
.file
->f_mode
& FMODE_READ
) {
1772 out
= fdget(fd_out
);
1774 if (out
.file
->f_mode
& FMODE_WRITE
)
1775 error
= do_splice(in
.file
, off_in
,
1787 * Make sure there's data to read. Wait for input if we can, otherwise
1788 * return an appropriate error.
1790 static int ipipe_prep(struct pipe_inode_info
*pipe
, unsigned int flags
)
1795 * Check ->nrbufs without the inode lock first. This function
1796 * is speculative anyways, so missing one is ok.
1804 while (!pipe
->nrbufs
) {
1805 if (signal_pending(current
)) {
1811 if (!pipe
->waiting_writers
) {
1812 if (flags
& SPLICE_F_NONBLOCK
) {
1825 * Make sure there's writeable room. Wait for room if we can, otherwise
1826 * return an appropriate error.
1828 static int opipe_prep(struct pipe_inode_info
*pipe
, unsigned int flags
)
1833 * Check ->nrbufs without the inode lock first. This function
1834 * is speculative anyways, so missing one is ok.
1836 if (pipe
->nrbufs
< pipe
->buffers
)
1842 while (pipe
->nrbufs
>= pipe
->buffers
) {
1843 if (!pipe
->readers
) {
1844 send_sig(SIGPIPE
, current
, 0);
1848 if (flags
& SPLICE_F_NONBLOCK
) {
1852 if (signal_pending(current
)) {
1856 pipe
->waiting_writers
++;
1858 pipe
->waiting_writers
--;
1866 * Splice contents of ipipe to opipe.
1868 static int splice_pipe_to_pipe(struct pipe_inode_info
*ipipe
,
1869 struct pipe_inode_info
*opipe
,
1870 size_t len
, unsigned int flags
)
1872 struct pipe_buffer
*ibuf
, *obuf
;
1874 bool input_wakeup
= false;
1878 ret
= ipipe_prep(ipipe
, flags
);
1882 ret
= opipe_prep(opipe
, flags
);
1887 * Potential ABBA deadlock, work around it by ordering lock
1888 * grabbing by pipe info address. Otherwise two different processes
1889 * could deadlock (one doing tee from A -> B, the other from B -> A).
1891 pipe_double_lock(ipipe
, opipe
);
1894 if (!opipe
->readers
) {
1895 send_sig(SIGPIPE
, current
, 0);
1901 if (!ipipe
->nrbufs
&& !ipipe
->writers
)
1905 * Cannot make any progress, because either the input
1906 * pipe is empty or the output pipe is full.
1908 if (!ipipe
->nrbufs
|| opipe
->nrbufs
>= opipe
->buffers
) {
1909 /* Already processed some buffers, break */
1913 if (flags
& SPLICE_F_NONBLOCK
) {
1919 * We raced with another reader/writer and haven't
1920 * managed to process any buffers. A zero return
1921 * value means EOF, so retry instead.
1928 ibuf
= ipipe
->bufs
+ ipipe
->curbuf
;
1929 nbuf
= (opipe
->curbuf
+ opipe
->nrbufs
) & (opipe
->buffers
- 1);
1930 obuf
= opipe
->bufs
+ nbuf
;
1932 if (len
>= ibuf
->len
) {
1934 * Simply move the whole buffer from ipipe to opipe
1939 ipipe
->curbuf
= (ipipe
->curbuf
+ 1) & (ipipe
->buffers
- 1);
1941 input_wakeup
= true;
1944 * Get a reference to this pipe buffer,
1945 * so we can copy the contents over.
1947 ibuf
->ops
->get(ipipe
, ibuf
);
1951 * Don't inherit the gift flag, we need to
1952 * prevent multiple steals of this page.
1954 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1958 ibuf
->offset
+= obuf
->len
;
1959 ibuf
->len
-= obuf
->len
;
1969 * If we put data in the output pipe, wakeup any potential readers.
1972 wakeup_pipe_readers(opipe
);
1975 wakeup_pipe_writers(ipipe
);
1981 * Link contents of ipipe to opipe.
1983 static int link_pipe(struct pipe_inode_info
*ipipe
,
1984 struct pipe_inode_info
*opipe
,
1985 size_t len
, unsigned int flags
)
1987 struct pipe_buffer
*ibuf
, *obuf
;
1988 int ret
= 0, i
= 0, nbuf
;
1991 * Potential ABBA deadlock, work around it by ordering lock
1992 * grabbing by pipe info address. Otherwise two different processes
1993 * could deadlock (one doing tee from A -> B, the other from B -> A).
1995 pipe_double_lock(ipipe
, opipe
);
1998 if (!opipe
->readers
) {
1999 send_sig(SIGPIPE
, current
, 0);
2006 * If we have iterated all input buffers or ran out of
2007 * output room, break.
2009 if (i
>= ipipe
->nrbufs
|| opipe
->nrbufs
>= opipe
->buffers
)
2012 ibuf
= ipipe
->bufs
+ ((ipipe
->curbuf
+ i
) & (ipipe
->buffers
-1));
2013 nbuf
= (opipe
->curbuf
+ opipe
->nrbufs
) & (opipe
->buffers
- 1);
2016 * Get a reference to this pipe buffer,
2017 * so we can copy the contents over.
2019 ibuf
->ops
->get(ipipe
, ibuf
);
2021 obuf
= opipe
->bufs
+ nbuf
;
2025 * Don't inherit the gift flag, we need to
2026 * prevent multiple steals of this page.
2028 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
2030 if (obuf
->len
> len
)
2040 * return EAGAIN if we have the potential of some data in the
2041 * future, otherwise just return 0
2043 if (!ret
&& ipipe
->waiting_writers
&& (flags
& SPLICE_F_NONBLOCK
))
2050 * If we put data in the output pipe, wakeup any potential readers.
2053 wakeup_pipe_readers(opipe
);
2059 * This is a tee(1) implementation that works on pipes. It doesn't copy
2060 * any data, it simply references the 'in' pages on the 'out' pipe.
2061 * The 'flags' used are the SPLICE_F_* variants, currently the only
2062 * applicable one is SPLICE_F_NONBLOCK.
2064 static long do_tee(struct file
*in
, struct file
*out
, size_t len
,
2067 struct pipe_inode_info
*ipipe
= get_pipe_info(in
);
2068 struct pipe_inode_info
*opipe
= get_pipe_info(out
);
2072 * Duplicate the contents of ipipe to opipe without actually
2075 if (ipipe
&& opipe
&& ipipe
!= opipe
) {
2077 * Keep going, unless we encounter an error. The ipipe/opipe
2078 * ordering doesn't really matter.
2080 ret
= ipipe_prep(ipipe
, flags
);
2082 ret
= opipe_prep(opipe
, flags
);
2084 ret
= link_pipe(ipipe
, opipe
, len
, flags
);
2091 SYSCALL_DEFINE4(tee
, int, fdin
, int, fdout
, size_t, len
, unsigned int, flags
)
2102 if (in
.file
->f_mode
& FMODE_READ
) {
2103 struct fd out
= fdget(fdout
);
2105 if (out
.file
->f_mode
& FMODE_WRITE
)
2106 error
= do_tee(in
.file
, out
.file
,