1 // SPDX-License-Identifier: GPL-2.0-only
3 * "splice": joining two ropes together by interweaving their strands.
5 * This is the "extended pipe" functionality, where a pipe is used as
6 * an arbitrary in-memory buffer. Think of a pipe as a small kernel
7 * buffer that you can use to transfer data from one end to the other.
9 * The traditional unix read/write is extended with a "splice()" operation
10 * that transfers data buffers to or from a pipe buffer.
12 * Named by Larry McVoy, original implementation from Linus, extended by
13 * Jens to support splicing to files, network, direct splicing, etc and
14 * fixing lots of bugs.
16 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
17 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
21 #include <linux/bvec.h>
23 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/splice.h>
26 #include <linux/memcontrol.h>
27 #include <linux/mm_inline.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/export.h>
31 #include <linux/syscalls.h>
32 #include <linux/uio.h>
33 #include <linux/security.h>
34 #include <linux/gfp.h>
35 #include <linux/socket.h>
36 #include <linux/sched/signal.h>
41 * Attempt to steal a page from a pipe buffer. This should perhaps go into
42 * a vm helper function, it's already simplified quite a bit by the
43 * addition of remove_mapping(). If success is returned, the caller may
44 * attempt to reuse this page for another destination.
46 static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info
*pipe
,
47 struct pipe_buffer
*buf
)
49 struct page
*page
= buf
->page
;
50 struct address_space
*mapping
;
54 mapping
= page_mapping(page
);
56 WARN_ON(!PageUptodate(page
));
59 * At least for ext2 with nobh option, we need to wait on
60 * writeback completing on this page, since we'll remove it
61 * from the pagecache. Otherwise truncate wont wait on the
62 * page, allowing the disk blocks to be reused by someone else
63 * before we actually wrote our data to them. fs corruption
66 wait_on_page_writeback(page
);
68 if (page_has_private(page
) &&
69 !try_to_release_page(page
, GFP_KERNEL
))
73 * If we succeeded in removing the mapping, set LRU flag
76 if (remove_mapping(mapping
, page
)) {
77 buf
->flags
|= PIPE_BUF_FLAG_LRU
;
83 * Raced with truncate or failed to remove page from current
84 * address space, unlock and return failure.
91 static void page_cache_pipe_buf_release(struct pipe_inode_info
*pipe
,
92 struct pipe_buffer
*buf
)
95 buf
->flags
&= ~PIPE_BUF_FLAG_LRU
;
99 * Check whether the contents of buf is OK to access. Since the content
100 * is a page cache page, IO may be in flight.
102 static int page_cache_pipe_buf_confirm(struct pipe_inode_info
*pipe
,
103 struct pipe_buffer
*buf
)
105 struct page
*page
= buf
->page
;
108 if (!PageUptodate(page
)) {
112 * Page got truncated/unhashed. This will cause a 0-byte
113 * splice, if this is the first page.
115 if (!page
->mapping
) {
121 * Uh oh, read-error from disk.
123 if (!PageUptodate(page
)) {
129 * Page is ok afterall, we are done.
140 const struct pipe_buf_operations page_cache_pipe_buf_ops
= {
141 .confirm
= page_cache_pipe_buf_confirm
,
142 .release
= page_cache_pipe_buf_release
,
143 .try_steal
= page_cache_pipe_buf_try_steal
,
144 .get
= generic_pipe_buf_get
,
147 static bool user_page_pipe_buf_try_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_try_steal(pipe
, buf
);
157 static const struct pipe_buf_operations user_page_pipe_buf_ops
= {
158 .release
= page_cache_pipe_buf_release
,
159 .try_steal
= user_page_pipe_buf_try_steal
,
160 .get
= generic_pipe_buf_get
,
163 static void wakeup_pipe_readers(struct pipe_inode_info
*pipe
)
166 if (waitqueue_active(&pipe
->rd_wait
))
167 wake_up_interruptible(&pipe
->rd_wait
);
168 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
172 * splice_to_pipe - fill passed data into a pipe
173 * @pipe: pipe to fill
177 * @spd contains a map of pages and len/offset tuples, along with
178 * the struct pipe_buf_operations associated with these pages. This
179 * function will link that data to the pipe.
182 ssize_t
splice_to_pipe(struct pipe_inode_info
*pipe
,
183 struct splice_pipe_desc
*spd
)
185 unsigned int spd_pages
= spd
->nr_pages
;
186 unsigned int tail
= pipe
->tail
;
187 unsigned int head
= pipe
->head
;
188 unsigned int mask
= pipe
->ring_size
- 1;
189 int ret
= 0, page_nr
= 0;
194 if (unlikely(!pipe
->readers
)) {
195 send_sig(SIGPIPE
, current
, 0);
200 while (!pipe_full(head
, tail
, pipe
->max_usage
)) {
201 struct pipe_buffer
*buf
= &pipe
->bufs
[head
& mask
];
203 buf
->page
= spd
->pages
[page_nr
];
204 buf
->offset
= spd
->partial
[page_nr
].offset
;
205 buf
->len
= spd
->partial
[page_nr
].len
;
206 buf
->private = spd
->partial
[page_nr
].private;
215 if (!--spd
->nr_pages
)
223 while (page_nr
< spd_pages
)
224 spd
->spd_release(spd
, page_nr
++);
228 EXPORT_SYMBOL_GPL(splice_to_pipe
);
230 ssize_t
add_to_pipe(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
)
232 unsigned int head
= pipe
->head
;
233 unsigned int tail
= pipe
->tail
;
234 unsigned int mask
= pipe
->ring_size
- 1;
237 if (unlikely(!pipe
->readers
)) {
238 send_sig(SIGPIPE
, current
, 0);
240 } else if (pipe_full(head
, tail
, pipe
->max_usage
)) {
243 pipe
->bufs
[head
& mask
] = *buf
;
244 pipe
->head
= head
+ 1;
247 pipe_buf_release(pipe
, buf
);
250 EXPORT_SYMBOL(add_to_pipe
);
253 * Check if we need to grow the arrays holding pages and partial page
256 int splice_grow_spd(const struct pipe_inode_info
*pipe
, struct splice_pipe_desc
*spd
)
258 unsigned int max_usage
= READ_ONCE(pipe
->max_usage
);
260 spd
->nr_pages_max
= max_usage
;
261 if (max_usage
<= PIPE_DEF_BUFFERS
)
264 spd
->pages
= kmalloc_array(max_usage
, sizeof(struct page
*), GFP_KERNEL
);
265 spd
->partial
= kmalloc_array(max_usage
, sizeof(struct partial_page
),
268 if (spd
->pages
&& spd
->partial
)
276 void splice_shrink_spd(struct splice_pipe_desc
*spd
)
278 if (spd
->nr_pages_max
<= PIPE_DEF_BUFFERS
)
286 * generic_file_splice_read - splice data from file to a pipe
287 * @in: file to splice from
288 * @ppos: position in @in
289 * @pipe: pipe to splice to
290 * @len: number of bytes to splice
291 * @flags: splice modifier flags
294 * Will read pages from given file and fill them into a pipe. Can be
295 * used as long as it has more or less sane ->read_iter().
298 ssize_t
generic_file_splice_read(struct file
*in
, loff_t
*ppos
,
299 struct pipe_inode_info
*pipe
, size_t len
,
307 iov_iter_pipe(&to
, READ
, pipe
, len
);
309 init_sync_kiocb(&kiocb
, in
);
310 kiocb
.ki_pos
= *ppos
;
311 ret
= call_read_iter(in
, &kiocb
, &to
);
313 *ppos
= kiocb
.ki_pos
;
315 } else if (ret
< 0) {
318 iov_iter_advance(&to
, 0); /* to free what was emitted */
320 * callers of ->splice_read() expect -EAGAIN on
321 * "can't put anything in there", rather than -EFAULT.
329 EXPORT_SYMBOL(generic_file_splice_read
);
331 const struct pipe_buf_operations default_pipe_buf_ops
= {
332 .release
= generic_pipe_buf_release
,
333 .try_steal
= generic_pipe_buf_try_steal
,
334 .get
= generic_pipe_buf_get
,
337 /* Pipe buffer operations for a socket and similar. */
338 const struct pipe_buf_operations nosteal_pipe_buf_ops
= {
339 .release
= generic_pipe_buf_release
,
340 .get
= generic_pipe_buf_get
,
342 EXPORT_SYMBOL(nosteal_pipe_buf_ops
);
344 static ssize_t
kernel_readv(struct file
*file
, const struct kvec
*vec
,
345 unsigned long vlen
, loff_t offset
)
353 /* The cast to a user pointer is valid due to the set_fs() */
354 res
= vfs_readv(file
, (const struct iovec __user
*)vec
, vlen
, &pos
, 0);
360 static ssize_t
default_file_splice_read(struct file
*in
, loff_t
*ppos
,
361 struct pipe_inode_info
*pipe
, size_t len
,
364 struct kvec
*vec
, __vec
[PIPE_DEF_BUFFERS
];
367 unsigned int nr_pages
;
369 size_t offset
, base
, copied
= 0;
373 if (pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
))
377 * Try to keep page boundaries matching to source pagecache ones -
378 * it probably won't be much help, but...
380 offset
= *ppos
& ~PAGE_MASK
;
382 iov_iter_pipe(&to
, READ
, pipe
, len
+ offset
);
384 res
= iov_iter_get_pages_alloc(&to
, &pages
, len
+ offset
, &base
);
388 nr_pages
= DIV_ROUND_UP(res
+ base
, PAGE_SIZE
);
391 if (nr_pages
> PIPE_DEF_BUFFERS
) {
392 vec
= kmalloc_array(nr_pages
, sizeof(struct kvec
), GFP_KERNEL
);
393 if (unlikely(!vec
)) {
399 mask
= pipe
->ring_size
- 1;
400 pipe
->bufs
[to
.head
& mask
].offset
= offset
;
401 pipe
->bufs
[to
.head
& mask
].len
-= offset
;
403 for (i
= 0; i
< nr_pages
; i
++) {
404 size_t this_len
= min_t(size_t, len
, PAGE_SIZE
- offset
);
405 vec
[i
].iov_base
= page_address(pages
[i
]) + offset
;
406 vec
[i
].iov_len
= this_len
;
411 res
= kernel_readv(in
, vec
, nr_pages
, *ppos
);
420 for (i
= 0; i
< nr_pages
; i
++)
423 iov_iter_advance(&to
, copied
); /* truncates and discards */
428 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
429 * using sendpage(). Return the number of bytes sent.
431 static int pipe_to_sendpage(struct pipe_inode_info
*pipe
,
432 struct pipe_buffer
*buf
, struct splice_desc
*sd
)
434 struct file
*file
= sd
->u
.file
;
435 loff_t pos
= sd
->pos
;
438 if (!likely(file
->f_op
->sendpage
))
441 more
= (sd
->flags
& SPLICE_F_MORE
) ? MSG_MORE
: 0;
443 if (sd
->len
< sd
->total_len
&&
444 pipe_occupancy(pipe
->head
, pipe
->tail
) > 1)
445 more
|= MSG_SENDPAGE_NOTLAST
;
447 return file
->f_op
->sendpage(file
, buf
->page
, buf
->offset
,
448 sd
->len
, &pos
, more
);
451 static void wakeup_pipe_writers(struct pipe_inode_info
*pipe
)
454 if (waitqueue_active(&pipe
->wr_wait
))
455 wake_up_interruptible(&pipe
->wr_wait
);
456 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
460 * splice_from_pipe_feed - feed available data from a pipe to a file
461 * @pipe: pipe to splice from
462 * @sd: information to @actor
463 * @actor: handler that splices the data
466 * This function loops over the pipe and calls @actor to do the
467 * actual moving of a single struct pipe_buffer to the desired
468 * destination. It returns when there's no more buffers left in
469 * the pipe or if the requested number of bytes (@sd->total_len)
470 * have been copied. It returns a positive number (one) if the
471 * pipe needs to be filled with more data, zero if the required
472 * number of bytes have been copied and -errno on error.
474 * This, together with splice_from_pipe_{begin,end,next}, may be
475 * used to implement the functionality of __splice_from_pipe() when
476 * locking is required around copying the pipe buffers to the
479 static int splice_from_pipe_feed(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
,
482 unsigned int head
= pipe
->head
;
483 unsigned int tail
= pipe
->tail
;
484 unsigned int mask
= pipe
->ring_size
- 1;
487 while (!pipe_empty(head
, tail
)) {
488 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
491 if (sd
->len
> sd
->total_len
)
492 sd
->len
= sd
->total_len
;
494 ret
= pipe_buf_confirm(pipe
, buf
);
501 ret
= actor(pipe
, buf
, sd
);
508 sd
->num_spliced
+= ret
;
511 sd
->total_len
-= ret
;
514 pipe_buf_release(pipe
, buf
);
518 sd
->need_wakeup
= true;
528 /* We know we have a pipe buffer, but maybe it's empty? */
529 static inline bool eat_empty_buffer(struct pipe_inode_info
*pipe
)
531 unsigned int tail
= pipe
->tail
;
532 unsigned int mask
= pipe
->ring_size
- 1;
533 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
535 if (unlikely(!buf
->len
)) {
536 pipe_buf_release(pipe
, buf
);
545 * splice_from_pipe_next - wait for some data to splice from
546 * @pipe: pipe to splice from
547 * @sd: information about the splice operation
550 * This function will wait for some data and return a positive
551 * value (one) if pipe buffers are available. It will return zero
552 * or -errno if no more data needs to be spliced.
554 static int splice_from_pipe_next(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
)
557 * Check for signal early to make process killable when there are
558 * always buffers available
560 if (signal_pending(current
))
564 while (pipe_empty(pipe
->head
, pipe
->tail
)) {
571 if (sd
->flags
& SPLICE_F_NONBLOCK
)
574 if (signal_pending(current
))
577 if (sd
->need_wakeup
) {
578 wakeup_pipe_writers(pipe
);
579 sd
->need_wakeup
= false;
582 pipe_wait_readable(pipe
);
585 if (eat_empty_buffer(pipe
))
592 * splice_from_pipe_begin - start splicing from pipe
593 * @sd: information about the splice operation
596 * This function should be called before a loop containing
597 * splice_from_pipe_next() and splice_from_pipe_feed() to
598 * initialize the necessary fields of @sd.
600 static void splice_from_pipe_begin(struct splice_desc
*sd
)
603 sd
->need_wakeup
= false;
607 * splice_from_pipe_end - finish splicing from pipe
608 * @pipe: pipe to splice from
609 * @sd: information about the splice operation
612 * This function will wake up pipe writers if necessary. It should
613 * be called after a loop containing splice_from_pipe_next() and
614 * splice_from_pipe_feed().
616 static void splice_from_pipe_end(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
)
619 wakeup_pipe_writers(pipe
);
623 * __splice_from_pipe - splice data from a pipe to given actor
624 * @pipe: pipe to splice from
625 * @sd: information to @actor
626 * @actor: handler that splices the data
629 * This function does little more than loop over the pipe and call
630 * @actor to do the actual moving of a single struct pipe_buffer to
631 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
635 ssize_t
__splice_from_pipe(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
,
640 splice_from_pipe_begin(sd
);
643 ret
= splice_from_pipe_next(pipe
, sd
);
645 ret
= splice_from_pipe_feed(pipe
, sd
, actor
);
647 splice_from_pipe_end(pipe
, sd
);
649 return sd
->num_spliced
? sd
->num_spliced
: ret
;
651 EXPORT_SYMBOL(__splice_from_pipe
);
654 * splice_from_pipe - splice data from a pipe to a file
655 * @pipe: pipe to splice from
656 * @out: file to splice to
657 * @ppos: position in @out
658 * @len: how many bytes to splice
659 * @flags: splice modifier flags
660 * @actor: handler that splices the data
663 * See __splice_from_pipe. This function locks the pipe inode,
664 * otherwise it's identical to __splice_from_pipe().
667 ssize_t
splice_from_pipe(struct pipe_inode_info
*pipe
, struct file
*out
,
668 loff_t
*ppos
, size_t len
, unsigned int flags
,
672 struct splice_desc sd
= {
680 ret
= __splice_from_pipe(pipe
, &sd
, actor
);
687 * iter_file_splice_write - splice data from a pipe to a file
689 * @out: file to write to
690 * @ppos: position in @out
691 * @len: number of bytes to splice
692 * @flags: splice modifier flags
695 * Will either move or copy pages (determined by @flags options) from
696 * the given pipe inode to the given file.
697 * This one is ->write_iter-based.
701 iter_file_splice_write(struct pipe_inode_info
*pipe
, struct file
*out
,
702 loff_t
*ppos
, size_t len
, unsigned int flags
)
704 struct splice_desc sd
= {
710 int nbufs
= pipe
->max_usage
;
711 struct bio_vec
*array
= kcalloc(nbufs
, sizeof(struct bio_vec
),
715 if (unlikely(!array
))
720 splice_from_pipe_begin(&sd
);
721 while (sd
.total_len
) {
722 struct iov_iter from
;
723 unsigned int head
, tail
, mask
;
727 ret
= splice_from_pipe_next(pipe
, &sd
);
731 if (unlikely(nbufs
< pipe
->max_usage
)) {
733 nbufs
= pipe
->max_usage
;
734 array
= kcalloc(nbufs
, sizeof(struct bio_vec
),
744 mask
= pipe
->ring_size
- 1;
746 /* build the vector */
748 for (n
= 0; !pipe_empty(head
, tail
) && left
&& n
< nbufs
; tail
++, n
++) {
749 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
750 size_t this_len
= buf
->len
;
755 ret
= pipe_buf_confirm(pipe
, buf
);
762 array
[n
].bv_page
= buf
->page
;
763 array
[n
].bv_len
= this_len
;
764 array
[n
].bv_offset
= buf
->offset
;
768 iov_iter_bvec(&from
, WRITE
, array
, n
, sd
.total_len
- left
);
769 ret
= vfs_iter_write(out
, &from
, &sd
.pos
, 0);
773 sd
.num_spliced
+= ret
;
777 /* dismiss the fully eaten buffers, adjust the partial one */
780 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
781 if (ret
>= buf
->len
) {
784 pipe_buf_release(pipe
, buf
);
788 sd
.need_wakeup
= true;
798 splice_from_pipe_end(pipe
, &sd
);
803 ret
= sd
.num_spliced
;
808 EXPORT_SYMBOL(iter_file_splice_write
);
810 static int write_pipe_buf(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
811 struct splice_desc
*sd
)
815 loff_t tmp
= sd
->pos
;
817 data
= kmap(buf
->page
);
818 ret
= __kernel_write(sd
->u
.file
, data
+ buf
->offset
, sd
->len
, &tmp
);
824 static ssize_t
default_file_splice_write(struct pipe_inode_info
*pipe
,
825 struct file
*out
, loff_t
*ppos
,
826 size_t len
, unsigned int flags
)
830 ret
= splice_from_pipe(pipe
, out
, ppos
, len
, flags
, write_pipe_buf
);
838 * generic_splice_sendpage - splice data from a pipe to a socket
839 * @pipe: pipe to splice from
840 * @out: socket to write to
841 * @ppos: position in @out
842 * @len: number of bytes to splice
843 * @flags: splice modifier flags
846 * Will send @len bytes from the pipe to a network socket. No data copying
850 ssize_t
generic_splice_sendpage(struct pipe_inode_info
*pipe
, struct file
*out
,
851 loff_t
*ppos
, size_t len
, unsigned int flags
)
853 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_sendpage
);
856 EXPORT_SYMBOL(generic_splice_sendpage
);
859 * Attempt to initiate a splice from pipe to file.
861 static long do_splice_from(struct pipe_inode_info
*pipe
, struct file
*out
,
862 loff_t
*ppos
, size_t len
, unsigned int flags
)
864 if (out
->f_op
->splice_write
)
865 return out
->f_op
->splice_write(pipe
, out
, ppos
, len
, flags
);
866 return default_file_splice_write(pipe
, out
, ppos
, len
, flags
);
870 * Attempt to initiate a splice from a file to a pipe.
872 static long do_splice_to(struct file
*in
, loff_t
*ppos
,
873 struct pipe_inode_info
*pipe
, size_t len
,
878 if (unlikely(!(in
->f_mode
& FMODE_READ
)))
881 ret
= rw_verify_area(READ
, in
, ppos
, len
);
882 if (unlikely(ret
< 0))
885 if (unlikely(len
> MAX_RW_COUNT
))
888 if (in
->f_op
->splice_read
)
889 return in
->f_op
->splice_read(in
, ppos
, pipe
, len
, flags
);
890 return default_file_splice_read(in
, ppos
, pipe
, len
, flags
);
894 * splice_direct_to_actor - splices data directly between two non-pipes
895 * @in: file to splice from
896 * @sd: actor information on where to splice to
897 * @actor: handles the data splicing
900 * This is a special case helper to splice directly between two
901 * points, without requiring an explicit pipe. Internally an allocated
902 * pipe is cached in the process, and reused during the lifetime of
906 ssize_t
splice_direct_to_actor(struct file
*in
, struct splice_desc
*sd
,
907 splice_direct_actor
*actor
)
909 struct pipe_inode_info
*pipe
;
916 * We require the input being a regular file, as we don't want to
917 * randomly drop data for eg socket -> socket splicing. Use the
918 * piped splicing for that!
920 i_mode
= file_inode(in
)->i_mode
;
921 if (unlikely(!S_ISREG(i_mode
) && !S_ISBLK(i_mode
)))
925 * neither in nor out is a pipe, setup an internal pipe attached to
926 * 'out' and transfer the wanted data from 'in' to 'out' through that
928 pipe
= current
->splice_pipe
;
929 if (unlikely(!pipe
)) {
930 pipe
= alloc_pipe_info();
935 * We don't have an immediate reader, but we'll read the stuff
936 * out of the pipe right after the splice_to_pipe(). So set
937 * PIPE_READERS appropriately.
941 current
->splice_pipe
= pipe
;
953 * Don't block on output, we have to drain the direct pipe.
955 sd
->flags
&= ~SPLICE_F_NONBLOCK
;
956 more
= sd
->flags
& SPLICE_F_MORE
;
958 WARN_ON_ONCE(!pipe_empty(pipe
->head
, pipe
->tail
));
961 unsigned int p_space
;
963 loff_t pos
= sd
->pos
, prev_pos
= pos
;
965 /* Don't try to read more the pipe has space for. */
966 p_space
= pipe
->max_usage
-
967 pipe_occupancy(pipe
->head
, pipe
->tail
);
968 read_len
= min_t(size_t, len
, p_space
<< PAGE_SHIFT
);
969 ret
= do_splice_to(in
, &pos
, pipe
, read_len
, flags
);
970 if (unlikely(ret
<= 0))
974 sd
->total_len
= read_len
;
977 * If more data is pending, set SPLICE_F_MORE
978 * If this is the last data and SPLICE_F_MORE was not set
979 * initially, clears it.
982 sd
->flags
|= SPLICE_F_MORE
;
984 sd
->flags
&= ~SPLICE_F_MORE
;
986 * NOTE: nonblocking mode only applies to the input. We
987 * must not do the output in nonblocking mode as then we
988 * could get stuck data in the internal pipe:
990 ret
= actor(pipe
, sd
);
991 if (unlikely(ret
<= 0)) {
1000 if (ret
< read_len
) {
1001 sd
->pos
= prev_pos
+ ret
;
1007 pipe
->tail
= pipe
->head
= 0;
1013 * If we did an incomplete transfer we must release
1014 * the pipe buffers in question:
1016 for (i
= 0; i
< pipe
->ring_size
; i
++) {
1017 struct pipe_buffer
*buf
= &pipe
->bufs
[i
];
1020 pipe_buf_release(pipe
, buf
);
1028 EXPORT_SYMBOL(splice_direct_to_actor
);
1030 static int direct_splice_actor(struct pipe_inode_info
*pipe
,
1031 struct splice_desc
*sd
)
1033 struct file
*file
= sd
->u
.file
;
1035 return do_splice_from(pipe
, file
, sd
->opos
, sd
->total_len
,
1040 * do_splice_direct - splices data directly between two files
1041 * @in: file to splice from
1042 * @ppos: input file offset
1043 * @out: file to splice to
1044 * @opos: output file offset
1045 * @len: number of bytes to splice
1046 * @flags: splice modifier flags
1049 * For use by do_sendfile(). splice can easily emulate sendfile, but
1050 * doing it in the application would incur an extra system call
1051 * (splice in + splice out, as compared to just sendfile()). So this helper
1052 * can splice directly through a process-private pipe.
1055 long do_splice_direct(struct file
*in
, loff_t
*ppos
, struct file
*out
,
1056 loff_t
*opos
, size_t len
, unsigned int flags
)
1058 struct splice_desc sd
= {
1068 if (unlikely(!(out
->f_mode
& FMODE_WRITE
)))
1071 if (unlikely(out
->f_flags
& O_APPEND
))
1074 ret
= rw_verify_area(WRITE
, out
, opos
, len
);
1075 if (unlikely(ret
< 0))
1078 ret
= splice_direct_to_actor(in
, &sd
, direct_splice_actor
);
1084 EXPORT_SYMBOL(do_splice_direct
);
1086 static int wait_for_space(struct pipe_inode_info
*pipe
, unsigned flags
)
1089 if (unlikely(!pipe
->readers
)) {
1090 send_sig(SIGPIPE
, current
, 0);
1093 if (!pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
))
1095 if (flags
& SPLICE_F_NONBLOCK
)
1097 if (signal_pending(current
))
1098 return -ERESTARTSYS
;
1099 pipe_wait_writable(pipe
);
1103 static int splice_pipe_to_pipe(struct pipe_inode_info
*ipipe
,
1104 struct pipe_inode_info
*opipe
,
1105 size_t len
, unsigned int flags
);
1108 * Determine where to splice to/from.
1110 long do_splice(struct file
*in
, loff_t __user
*off_in
,
1111 struct file
*out
, loff_t __user
*off_out
,
1112 size_t len
, unsigned int flags
)
1114 struct pipe_inode_info
*ipipe
;
1115 struct pipe_inode_info
*opipe
;
1119 if (unlikely(!(in
->f_mode
& FMODE_READ
) ||
1120 !(out
->f_mode
& FMODE_WRITE
)))
1123 ipipe
= get_pipe_info(in
, true);
1124 opipe
= get_pipe_info(out
, true);
1126 if (ipipe
&& opipe
) {
1127 if (off_in
|| off_out
)
1130 /* Splicing to self would be fun, but... */
1134 if ((in
->f_flags
| out
->f_flags
) & O_NONBLOCK
)
1135 flags
|= SPLICE_F_NONBLOCK
;
1137 return splice_pipe_to_pipe(ipipe
, opipe
, len
, flags
);
1144 if (!(out
->f_mode
& FMODE_PWRITE
))
1146 if (copy_from_user(&offset
, off_out
, sizeof(loff_t
)))
1149 offset
= out
->f_pos
;
1152 if (unlikely(out
->f_flags
& O_APPEND
))
1155 ret
= rw_verify_area(WRITE
, out
, &offset
, len
);
1156 if (unlikely(ret
< 0))
1159 if (in
->f_flags
& O_NONBLOCK
)
1160 flags
|= SPLICE_F_NONBLOCK
;
1162 file_start_write(out
);
1163 ret
= do_splice_from(ipipe
, out
, &offset
, len
, flags
);
1164 file_end_write(out
);
1167 out
->f_pos
= offset
;
1168 else if (copy_to_user(off_out
, &offset
, sizeof(loff_t
)))
1178 if (!(in
->f_mode
& FMODE_PREAD
))
1180 if (copy_from_user(&offset
, off_in
, sizeof(loff_t
)))
1186 if (out
->f_flags
& O_NONBLOCK
)
1187 flags
|= SPLICE_F_NONBLOCK
;
1190 ret
= wait_for_space(opipe
, flags
);
1192 unsigned int p_space
;
1194 /* Don't try to read more the pipe has space for. */
1195 p_space
= opipe
->max_usage
- pipe_occupancy(opipe
->head
, opipe
->tail
);
1196 len
= min_t(size_t, len
, p_space
<< PAGE_SHIFT
);
1198 ret
= do_splice_to(in
, &offset
, opipe
, len
, flags
);
1202 wakeup_pipe_readers(opipe
);
1205 else if (copy_to_user(off_in
, &offset
, sizeof(loff_t
)))
1214 static int iter_to_pipe(struct iov_iter
*from
,
1215 struct pipe_inode_info
*pipe
,
1218 struct pipe_buffer buf
= {
1219 .ops
= &user_page_pipe_buf_ops
,
1224 bool failed
= false;
1226 while (iov_iter_count(from
) && !failed
) {
1227 struct page
*pages
[16];
1232 copied
= iov_iter_get_pages(from
, pages
, ~0UL, 16, &start
);
1238 for (n
= 0; copied
; n
++, start
= 0) {
1239 int size
= min_t(int, copied
, PAGE_SIZE
- start
);
1241 buf
.page
= pages
[n
];
1244 ret
= add_to_pipe(pipe
, &buf
);
1245 if (unlikely(ret
< 0)) {
1248 iov_iter_advance(from
, ret
);
1257 return total
? total
: ret
;
1260 static int pipe_to_user(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
1261 struct splice_desc
*sd
)
1263 int n
= copy_page_to_iter(buf
->page
, buf
->offset
, sd
->len
, sd
->u
.data
);
1264 return n
== sd
->len
? n
: -EFAULT
;
1268 * For lack of a better implementation, implement vmsplice() to userspace
1269 * as a simple copy of the pipes pages to the user iov.
1271 static long vmsplice_to_user(struct file
*file
, struct iov_iter
*iter
,
1274 struct pipe_inode_info
*pipe
= get_pipe_info(file
, true);
1275 struct splice_desc sd
= {
1276 .total_len
= iov_iter_count(iter
),
1287 ret
= __splice_from_pipe(pipe
, &sd
, pipe_to_user
);
1295 * vmsplice splices a user address range into a pipe. It can be thought of
1296 * as splice-from-memory, where the regular splice is splice-from-file (or
1297 * to file). In both cases the output is a pipe, naturally.
1299 static long vmsplice_to_pipe(struct file
*file
, struct iov_iter
*iter
,
1302 struct pipe_inode_info
*pipe
;
1304 unsigned buf_flag
= 0;
1306 if (flags
& SPLICE_F_GIFT
)
1307 buf_flag
= PIPE_BUF_FLAG_GIFT
;
1309 pipe
= get_pipe_info(file
, true);
1314 ret
= wait_for_space(pipe
, flags
);
1316 ret
= iter_to_pipe(iter
, pipe
, buf_flag
);
1319 wakeup_pipe_readers(pipe
);
1323 static int vmsplice_type(struct fd f
, int *type
)
1327 if (f
.file
->f_mode
& FMODE_WRITE
) {
1329 } else if (f
.file
->f_mode
& FMODE_READ
) {
1339 * Note that vmsplice only really supports true splicing _from_ user memory
1340 * to a pipe, not the other way around. Splicing from user memory is a simple
1341 * operation that can be supported without any funky alignment restrictions
1342 * or nasty vm tricks. We simply map in the user memory and fill them into
1343 * a pipe. The reverse isn't quite as easy, though. There are two possible
1344 * solutions for that:
1346 * - memcpy() the data internally, at which point we might as well just
1347 * do a regular read() on the buffer anyway.
1348 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1349 * has restriction limitations on both ends of the pipe).
1351 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1354 SYSCALL_DEFINE4(vmsplice
, int, fd
, const struct iovec __user
*, uiov
,
1355 unsigned long, nr_segs
, unsigned int, flags
)
1357 struct iovec iovstack
[UIO_FASTIOV
];
1358 struct iovec
*iov
= iovstack
;
1359 struct iov_iter iter
;
1364 if (unlikely(flags
& ~SPLICE_F_ALL
))
1368 error
= vmsplice_type(f
, &type
);
1372 error
= import_iovec(type
, uiov
, nr_segs
,
1373 ARRAY_SIZE(iovstack
), &iov
, &iter
);
1377 if (!iov_iter_count(&iter
))
1379 else if (iov_iter_rw(&iter
) == WRITE
)
1380 error
= vmsplice_to_pipe(f
.file
, &iter
, flags
);
1382 error
= vmsplice_to_user(f
.file
, &iter
, flags
);
1390 SYSCALL_DEFINE6(splice
, int, fd_in
, loff_t __user
*, off_in
,
1391 int, fd_out
, loff_t __user
*, off_out
,
1392 size_t, len
, unsigned int, flags
)
1400 if (unlikely(flags
& ~SPLICE_F_ALL
))
1406 out
= fdget(fd_out
);
1408 error
= do_splice(in
.file
, off_in
, out
.file
, off_out
,
1418 * Make sure there's data to read. Wait for input if we can, otherwise
1419 * return an appropriate error.
1421 static int ipipe_prep(struct pipe_inode_info
*pipe
, unsigned int flags
)
1426 * Check the pipe occupancy without the inode lock first. This function
1427 * is speculative anyways, so missing one is ok.
1429 if (!pipe_empty(pipe
->head
, pipe
->tail
))
1435 while (pipe_empty(pipe
->head
, pipe
->tail
)) {
1436 if (signal_pending(current
)) {
1442 if (flags
& SPLICE_F_NONBLOCK
) {
1446 pipe_wait_readable(pipe
);
1454 * Make sure there's writeable room. Wait for room if we can, otherwise
1455 * return an appropriate error.
1457 static int opipe_prep(struct pipe_inode_info
*pipe
, unsigned int flags
)
1462 * Check pipe occupancy without the inode lock first. This function
1463 * is speculative anyways, so missing one is ok.
1465 if (!pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
))
1471 while (pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
)) {
1472 if (!pipe
->readers
) {
1473 send_sig(SIGPIPE
, current
, 0);
1477 if (flags
& SPLICE_F_NONBLOCK
) {
1481 if (signal_pending(current
)) {
1485 pipe_wait_writable(pipe
);
1493 * Splice contents of ipipe to opipe.
1495 static int splice_pipe_to_pipe(struct pipe_inode_info
*ipipe
,
1496 struct pipe_inode_info
*opipe
,
1497 size_t len
, unsigned int flags
)
1499 struct pipe_buffer
*ibuf
, *obuf
;
1500 unsigned int i_head
, o_head
;
1501 unsigned int i_tail
, o_tail
;
1502 unsigned int i_mask
, o_mask
;
1504 bool input_wakeup
= false;
1508 ret
= ipipe_prep(ipipe
, flags
);
1512 ret
= opipe_prep(opipe
, flags
);
1517 * Potential ABBA deadlock, work around it by ordering lock
1518 * grabbing by pipe info address. Otherwise two different processes
1519 * could deadlock (one doing tee from A -> B, the other from B -> A).
1521 pipe_double_lock(ipipe
, opipe
);
1523 i_tail
= ipipe
->tail
;
1524 i_mask
= ipipe
->ring_size
- 1;
1525 o_head
= opipe
->head
;
1526 o_mask
= opipe
->ring_size
- 1;
1531 if (!opipe
->readers
) {
1532 send_sig(SIGPIPE
, current
, 0);
1538 i_head
= ipipe
->head
;
1539 o_tail
= opipe
->tail
;
1541 if (pipe_empty(i_head
, i_tail
) && !ipipe
->writers
)
1545 * Cannot make any progress, because either the input
1546 * pipe is empty or the output pipe is full.
1548 if (pipe_empty(i_head
, i_tail
) ||
1549 pipe_full(o_head
, o_tail
, opipe
->max_usage
)) {
1550 /* Already processed some buffers, break */
1554 if (flags
& SPLICE_F_NONBLOCK
) {
1560 * We raced with another reader/writer and haven't
1561 * managed to process any buffers. A zero return
1562 * value means EOF, so retry instead.
1569 ibuf
= &ipipe
->bufs
[i_tail
& i_mask
];
1570 obuf
= &opipe
->bufs
[o_head
& o_mask
];
1572 if (len
>= ibuf
->len
) {
1574 * Simply move the whole buffer from ipipe to opipe
1579 ipipe
->tail
= i_tail
;
1580 input_wakeup
= true;
1583 opipe
->head
= o_head
;
1586 * Get a reference to this pipe buffer,
1587 * so we can copy the contents over.
1589 if (!pipe_buf_get(ipipe
, ibuf
)) {
1597 * Don't inherit the gift and merge flags, we need to
1598 * prevent multiple steals of this page.
1600 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1601 obuf
->flags
&= ~PIPE_BUF_FLAG_CAN_MERGE
;
1604 ibuf
->offset
+= len
;
1608 opipe
->head
= o_head
;
1618 * If we put data in the output pipe, wakeup any potential readers.
1621 wakeup_pipe_readers(opipe
);
1624 wakeup_pipe_writers(ipipe
);
1630 * Link contents of ipipe to opipe.
1632 static int link_pipe(struct pipe_inode_info
*ipipe
,
1633 struct pipe_inode_info
*opipe
,
1634 size_t len
, unsigned int flags
)
1636 struct pipe_buffer
*ibuf
, *obuf
;
1637 unsigned int i_head
, o_head
;
1638 unsigned int i_tail
, o_tail
;
1639 unsigned int i_mask
, o_mask
;
1643 * Potential ABBA deadlock, work around it by ordering lock
1644 * grabbing by pipe info address. Otherwise two different processes
1645 * could deadlock (one doing tee from A -> B, the other from B -> A).
1647 pipe_double_lock(ipipe
, opipe
);
1649 i_tail
= ipipe
->tail
;
1650 i_mask
= ipipe
->ring_size
- 1;
1651 o_head
= opipe
->head
;
1652 o_mask
= opipe
->ring_size
- 1;
1655 if (!opipe
->readers
) {
1656 send_sig(SIGPIPE
, current
, 0);
1662 i_head
= ipipe
->head
;
1663 o_tail
= opipe
->tail
;
1666 * If we have iterated all input buffers or run out of
1667 * output room, break.
1669 if (pipe_empty(i_head
, i_tail
) ||
1670 pipe_full(o_head
, o_tail
, opipe
->max_usage
))
1673 ibuf
= &ipipe
->bufs
[i_tail
& i_mask
];
1674 obuf
= &opipe
->bufs
[o_head
& o_mask
];
1677 * Get a reference to this pipe buffer,
1678 * so we can copy the contents over.
1680 if (!pipe_buf_get(ipipe
, ibuf
)) {
1689 * Don't inherit the gift and merge flag, we need to prevent
1690 * multiple steals of this page.
1692 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1693 obuf
->flags
&= ~PIPE_BUF_FLAG_CAN_MERGE
;
1695 if (obuf
->len
> len
)
1701 opipe
->head
= o_head
;
1709 * If we put data in the output pipe, wakeup any potential readers.
1712 wakeup_pipe_readers(opipe
);
1718 * This is a tee(1) implementation that works on pipes. It doesn't copy
1719 * any data, it simply references the 'in' pages on the 'out' pipe.
1720 * The 'flags' used are the SPLICE_F_* variants, currently the only
1721 * applicable one is SPLICE_F_NONBLOCK.
1723 long do_tee(struct file
*in
, struct file
*out
, size_t len
, unsigned int flags
)
1725 struct pipe_inode_info
*ipipe
= get_pipe_info(in
, true);
1726 struct pipe_inode_info
*opipe
= get_pipe_info(out
, true);
1729 if (unlikely(!(in
->f_mode
& FMODE_READ
) ||
1730 !(out
->f_mode
& FMODE_WRITE
)))
1734 * Duplicate the contents of ipipe to opipe without actually
1737 if (ipipe
&& opipe
&& ipipe
!= opipe
) {
1738 if ((in
->f_flags
| out
->f_flags
) & O_NONBLOCK
)
1739 flags
|= SPLICE_F_NONBLOCK
;
1742 * Keep going, unless we encounter an error. The ipipe/opipe
1743 * ordering doesn't really matter.
1745 ret
= ipipe_prep(ipipe
, flags
);
1747 ret
= opipe_prep(opipe
, flags
);
1749 ret
= link_pipe(ipipe
, opipe
, len
, flags
);
1756 SYSCALL_DEFINE4(tee
, int, fdin
, int, fdout
, size_t, len
, unsigned int, flags
)
1761 if (unlikely(flags
& ~SPLICE_F_ALL
))
1772 error
= do_tee(in
.file
, out
.file
, len
, flags
);