xref: /openbmc/linux/fs/splice.c (revision 9eee8bd8)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25274f052SJens Axboe /*
35274f052SJens Axboe  * "splice": joining two ropes together by interweaving their strands.
45274f052SJens Axboe  *
55274f052SJens Axboe  * This is the "extended pipe" functionality, where a pipe is used as
65274f052SJens Axboe  * an arbitrary in-memory buffer. Think of a pipe as a small kernel
75274f052SJens Axboe  * buffer that you can use to transfer data from one end to the other.
85274f052SJens Axboe  *
95274f052SJens Axboe  * The traditional unix read/write is extended with a "splice()" operation
105274f052SJens Axboe  * that transfers data buffers to or from a pipe buffer.
115274f052SJens Axboe  *
125274f052SJens Axboe  * Named by Larry McVoy, original implementation from Linus, extended by
13c2058e06SJens Axboe  * Jens to support splicing to files, network, direct splicing, etc and
14c2058e06SJens Axboe  * fixing lots of bugs.
155274f052SJens Axboe  *
160fe23479SJens Axboe  * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
17c2058e06SJens Axboe  * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18c2058e06SJens Axboe  * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
195274f052SJens Axboe  *
205274f052SJens Axboe  */
21be297968SChristoph Hellwig #include <linux/bvec.h>
225274f052SJens Axboe #include <linux/fs.h>
235274f052SJens Axboe #include <linux/file.h>
245274f052SJens Axboe #include <linux/pagemap.h>
25d6b29d7cSJens Axboe #include <linux/splice.h>
2608e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h>
275274f052SJens Axboe #include <linux/mm_inline.h>
285abc97aaSJens Axboe #include <linux/swap.h>
294f6f0bd2SJens Axboe #include <linux/writeback.h>
30630d9c47SPaul Gortmaker #include <linux/export.h>
314f6f0bd2SJens Axboe #include <linux/syscalls.h>
32912d35f8SJens Axboe #include <linux/uio.h>
33983652c6SChung-Chiang Cheng #include <linux/fsnotify.h>
3429ce2058SJames Morris #include <linux/security.h>
355a0e3ad6STejun Heo #include <linux/gfp.h>
3635f9c09fSEric Dumazet #include <linux/socket.h>
37174cd4b1SIngo Molnar #include <linux/sched/signal.h>
38174cd4b1SIngo Molnar 
3906ae43f3SAl Viro #include "internal.h"
405274f052SJens Axboe 
4183f9135bSJens Axboe /*
420f99fc51SJens Axboe  * Splice doesn't support FMODE_NOWAIT. Since pipes may set this flag to
430f99fc51SJens Axboe  * indicate they support non-blocking reads or writes, we must clear it
440f99fc51SJens Axboe  * here if set to avoid blocking other users of this pipe if splice is
450f99fc51SJens Axboe  * being done on it.
460f99fc51SJens Axboe  */
470f99fc51SJens Axboe static noinline void noinline pipe_clear_nowait(struct file *file)
480f99fc51SJens Axboe {
490f99fc51SJens Axboe 	fmode_t fmode = READ_ONCE(file->f_mode);
500f99fc51SJens Axboe 
510f99fc51SJens Axboe 	do {
520f99fc51SJens Axboe 		if (!(fmode & FMODE_NOWAIT))
530f99fc51SJens Axboe 			break;
540f99fc51SJens Axboe 	} while (!try_cmpxchg(&file->f_mode, &fmode, fmode & ~FMODE_NOWAIT));
550f99fc51SJens Axboe }
560f99fc51SJens Axboe 
570f99fc51SJens Axboe /*
5883f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
5983f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
6083f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
6183f9135bSJens Axboe  * attempt to reuse this page for another destination.
6283f9135bSJens Axboe  */
63c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
645abc97aaSJens Axboe 		struct pipe_buffer *buf)
655abc97aaSJens Axboe {
665100da38SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(buf->page);
679e94cd4fSJens Axboe 	struct address_space *mapping;
685abc97aaSJens Axboe 
69b9ccad2eSMatthew Wilcox (Oracle) 	folio_lock(folio);
709e0267c2SJens Axboe 
71b9ccad2eSMatthew Wilcox (Oracle) 	mapping = folio_mapping(folio);
729e94cd4fSJens Axboe 	if (mapping) {
73b9ccad2eSMatthew Wilcox (Oracle) 		WARN_ON(!folio_test_uptodate(folio));
745abc97aaSJens Axboe 
75ad8d6f0aSJens Axboe 		/*
769e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
77b9ccad2eSMatthew Wilcox (Oracle) 		 * writeback completing on this folio, since we'll remove it
789e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
79b9ccad2eSMatthew Wilcox (Oracle) 		 * folio, allowing the disk blocks to be reused by someone else
809e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
819e94cd4fSJens Axboe 		 * ensues.
82ad8d6f0aSJens Axboe 		 */
83b9ccad2eSMatthew Wilcox (Oracle) 		folio_wait_writeback(folio);
84ad8d6f0aSJens Axboe 
85b9ccad2eSMatthew Wilcox (Oracle) 		if (folio_has_private(folio) &&
86b9ccad2eSMatthew Wilcox (Oracle) 		    !filemap_release_folio(folio, GFP_KERNEL))
87ca39d651SJens Axboe 			goto out_unlock;
884f6f0bd2SJens Axboe 
899e94cd4fSJens Axboe 		/*
909e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
919e94cd4fSJens Axboe 		 * and return good.
929e94cd4fSJens Axboe 		 */
935100da38SMatthew Wilcox (Oracle) 		if (remove_mapping(mapping, folio)) {
941432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
95c928f642SChristoph Hellwig 			return true;
965abc97aaSJens Axboe 		}
979e94cd4fSJens Axboe 	}
989e94cd4fSJens Axboe 
999e94cd4fSJens Axboe 	/*
100b9ccad2eSMatthew Wilcox (Oracle) 	 * Raced with truncate or failed to remove folio from current
1019e94cd4fSJens Axboe 	 * address space, unlock and return failure.
1029e94cd4fSJens Axboe 	 */
103ca39d651SJens Axboe out_unlock:
104b9ccad2eSMatthew Wilcox (Oracle) 	folio_unlock(folio);
105c928f642SChristoph Hellwig 	return false;
1069e94cd4fSJens Axboe }
1075abc97aaSJens Axboe 
10876ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
1095274f052SJens Axboe 					struct pipe_buffer *buf)
1105274f052SJens Axboe {
11109cbfeafSKirill A. Shutemov 	put_page(buf->page);
1121432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
1135274f052SJens Axboe }
1145274f052SJens Axboe 
1150845718dSJens Axboe /*
1160845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
1170845718dSJens Axboe  * is a page cache page, IO may be in flight.
1180845718dSJens Axboe  */
119cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1205274f052SJens Axboe 				       struct pipe_buffer *buf)
1215274f052SJens Axboe {
1225274f052SJens Axboe 	struct page *page = buf->page;
12349d0b21bSJens Axboe 	int err;
1245274f052SJens Axboe 
1255274f052SJens Axboe 	if (!PageUptodate(page)) {
12649d0b21bSJens Axboe 		lock_page(page);
1275274f052SJens Axboe 
12849d0b21bSJens Axboe 		/*
12949d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
13073d62d83SIngo Molnar 		 * splice, if this is the first page.
13149d0b21bSJens Axboe 		 */
1325274f052SJens Axboe 		if (!page->mapping) {
13349d0b21bSJens Axboe 			err = -ENODATA;
13449d0b21bSJens Axboe 			goto error;
1355274f052SJens Axboe 		}
1365274f052SJens Axboe 
13749d0b21bSJens Axboe 		/*
13873d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
13949d0b21bSJens Axboe 		 */
14049d0b21bSJens Axboe 		if (!PageUptodate(page)) {
14149d0b21bSJens Axboe 			err = -EIO;
14249d0b21bSJens Axboe 			goto error;
14349d0b21bSJens Axboe 		}
14449d0b21bSJens Axboe 
14549d0b21bSJens Axboe 		/*
146f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
14749d0b21bSJens Axboe 		 */
14849d0b21bSJens Axboe 		unlock_page(page);
14949d0b21bSJens Axboe 	}
15049d0b21bSJens Axboe 
151f84d7519SJens Axboe 	return 0;
15249d0b21bSJens Axboe error:
15349d0b21bSJens Axboe 	unlock_page(page);
154f84d7519SJens Axboe 	return err;
15570524490SJens Axboe }
15670524490SJens Axboe 
157708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
158cac36bb0SJens Axboe 	.confirm	= page_cache_pipe_buf_confirm,
1595274f052SJens Axboe 	.release	= page_cache_pipe_buf_release,
160c928f642SChristoph Hellwig 	.try_steal	= page_cache_pipe_buf_try_steal,
161f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
1625274f052SJens Axboe };
1635274f052SJens Axboe 
164c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
165912d35f8SJens Axboe 		struct pipe_buffer *buf)
166912d35f8SJens Axboe {
1677afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
168c928f642SChristoph Hellwig 		return false;
1697afa6fd0SJens Axboe 
1701432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
171c928f642SChristoph Hellwig 	return generic_pipe_buf_try_steal(pipe, buf);
172912d35f8SJens Axboe }
173912d35f8SJens Axboe 
174d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
175912d35f8SJens Axboe 	.release	= page_cache_pipe_buf_release,
176c928f642SChristoph Hellwig 	.try_steal	= user_page_pipe_buf_try_steal,
177f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
178912d35f8SJens Axboe };
179912d35f8SJens Axboe 
180825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
181825cdcb1SNamhyung Kim {
182825cdcb1SNamhyung Kim 	smp_mb();
1830ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1840ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_wait);
185825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
186825cdcb1SNamhyung Kim }
187825cdcb1SNamhyung Kim 
188932cc6d4SJens Axboe /**
189932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
190932cc6d4SJens Axboe  * @pipe:	pipe to fill
191932cc6d4SJens Axboe  * @spd:	data to fill
192932cc6d4SJens Axboe  *
193932cc6d4SJens Axboe  * Description:
19479685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
195932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
196932cc6d4SJens Axboe  *    function will link that data to the pipe.
197932cc6d4SJens Axboe  *
19883f9135bSJens Axboe  */
199d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
200912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
2015274f052SJens Axboe {
20200de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
2038cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2048cefc107SDavid Howells 	unsigned int head = pipe->head;
2058cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
2068924feffSAl Viro 	int ret = 0, page_nr = 0;
2075274f052SJens Axboe 
208d6785d91SRabin Vincent 	if (!spd_pages)
209d6785d91SRabin Vincent 		return 0;
210d6785d91SRabin Vincent 
2118924feffSAl Viro 	if (unlikely(!pipe->readers)) {
2125274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
2135274f052SJens Axboe 		ret = -EPIPE;
2148924feffSAl Viro 		goto out;
2155274f052SJens Axboe 	}
2165274f052SJens Axboe 
2176718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2188cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
2195274f052SJens Axboe 
220912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
221912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
222912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
223497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
224912d35f8SJens Axboe 		buf->ops = spd->ops;
2255a81e6a1SMiklos Szeredi 		buf->flags = 0;
2267afa6fd0SJens Axboe 
2278cefc107SDavid Howells 		head++;
2288cefc107SDavid Howells 		pipe->head = head;
229912d35f8SJens Axboe 		page_nr++;
230912d35f8SJens Axboe 		ret += buf->len;
231912d35f8SJens Axboe 
232912d35f8SJens Axboe 		if (!--spd->nr_pages)
2335274f052SJens Axboe 			break;
2345274f052SJens Axboe 	}
2355274f052SJens Axboe 
23629e35094SLinus Torvalds 	if (!ret)
23729e35094SLinus Torvalds 		ret = -EAGAIN;
23829e35094SLinus Torvalds 
2398924feffSAl Viro out:
24000de00bdSJens Axboe 	while (page_nr < spd_pages)
241bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2425274f052SJens Axboe 
2435274f052SJens Axboe 	return ret;
2445274f052SJens Axboe }
2452b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2465274f052SJens Axboe 
24779fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
24879fddc4eSAl Viro {
2498cefc107SDavid Howells 	unsigned int head = pipe->head;
2508cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2518cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
25279fddc4eSAl Viro 	int ret;
25379fddc4eSAl Viro 
25479fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
25579fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
25679fddc4eSAl Viro 		ret = -EPIPE;
2576718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
25879fddc4eSAl Viro 		ret = -EAGAIN;
25979fddc4eSAl Viro 	} else {
2608cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2618cefc107SDavid Howells 		pipe->head = head + 1;
26279fddc4eSAl Viro 		return buf->len;
26379fddc4eSAl Viro 	}
264a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
26579fddc4eSAl Viro 	return ret;
26679fddc4eSAl Viro }
26779fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
26879fddc4eSAl Viro 
26935f3d14dSJens Axboe /*
27035f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
27135f3d14dSJens Axboe  * descriptions.
27235f3d14dSJens Axboe  */
273047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
27435f3d14dSJens Axboe {
2756718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
276047fe360SEric Dumazet 
2778cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2788cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
27935f3d14dSJens Axboe 		return 0;
28035f3d14dSJens Axboe 
2818cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2828cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2836da2ec56SKees Cook 				     GFP_KERNEL);
28435f3d14dSJens Axboe 
28535f3d14dSJens Axboe 	if (spd->pages && spd->partial)
28635f3d14dSJens Axboe 		return 0;
28735f3d14dSJens Axboe 
28835f3d14dSJens Axboe 	kfree(spd->pages);
28935f3d14dSJens Axboe 	kfree(spd->partial);
29035f3d14dSJens Axboe 	return -ENOMEM;
29135f3d14dSJens Axboe }
29235f3d14dSJens Axboe 
293047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
29435f3d14dSJens Axboe {
295047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
29635f3d14dSJens Axboe 		return;
29735f3d14dSJens Axboe 
29835f3d14dSJens Axboe 	kfree(spd->pages);
29935f3d14dSJens Axboe 	kfree(spd->partial);
30035f3d14dSJens Axboe }
30135f3d14dSJens Axboe 
302*9eee8bd8SDavid Howells /**
303*9eee8bd8SDavid Howells  * copy_splice_read -  Copy data from a file and splice the copy into a pipe
304*9eee8bd8SDavid Howells  * @in: The file to read from
305*9eee8bd8SDavid Howells  * @ppos: Pointer to the file position to read from
306*9eee8bd8SDavid Howells  * @pipe: The pipe to splice into
307*9eee8bd8SDavid Howells  * @len: The amount to splice
308*9eee8bd8SDavid Howells  * @flags: The SPLICE_F_* flags
309*9eee8bd8SDavid Howells  *
310*9eee8bd8SDavid Howells  * This function allocates a bunch of pages sufficient to hold the requested
311*9eee8bd8SDavid Howells  * amount of data (but limited by the remaining pipe capacity), passes it to
312*9eee8bd8SDavid Howells  * the file's ->read_iter() to read into and then splices the used pages into
313*9eee8bd8SDavid Howells  * the pipe.
314*9eee8bd8SDavid Howells  *
315*9eee8bd8SDavid Howells  * Return: On success, the number of bytes read will be returned and *@ppos
316*9eee8bd8SDavid Howells  * will be updated if appropriate; 0 will be returned if there is no more data
317*9eee8bd8SDavid Howells  * to be read; -EAGAIN will be returned if the pipe had no space, and some
318*9eee8bd8SDavid Howells  * other negative error code will be returned on error.  A short read may occur
319*9eee8bd8SDavid Howells  * if the pipe has insufficient space, we reach the end of the data or we hit a
320*9eee8bd8SDavid Howells  * hole.
32133b3b041SDavid Howells  */
32269df79a4SDavid Howells ssize_t copy_splice_read(struct file *in, loff_t *ppos,
32333b3b041SDavid Howells 			 struct pipe_inode_info *pipe,
32433b3b041SDavid Howells 			 size_t len, unsigned int flags)
32533b3b041SDavid Howells {
32633b3b041SDavid Howells 	struct iov_iter to;
32733b3b041SDavid Howells 	struct bio_vec *bv;
32833b3b041SDavid Howells 	struct kiocb kiocb;
32933b3b041SDavid Howells 	struct page **pages;
33033b3b041SDavid Howells 	ssize_t ret;
331e69f37bcSDavid Howells 	size_t used, npages, chunk, remain, keep = 0;
33233b3b041SDavid Howells 	int i;
33333b3b041SDavid Howells 
33433b3b041SDavid Howells 	/* Work out how much data we can actually add into the pipe */
33533b3b041SDavid Howells 	used = pipe_occupancy(pipe->head, pipe->tail);
33633b3b041SDavid Howells 	npages = max_t(ssize_t, pipe->max_usage - used, 0);
33733b3b041SDavid Howells 	len = min_t(size_t, len, npages * PAGE_SIZE);
33833b3b041SDavid Howells 	npages = DIV_ROUND_UP(len, PAGE_SIZE);
33933b3b041SDavid Howells 
34033b3b041SDavid Howells 	bv = kzalloc(array_size(npages, sizeof(bv[0])) +
34133b3b041SDavid Howells 		     array_size(npages, sizeof(struct page *)), GFP_KERNEL);
34233b3b041SDavid Howells 	if (!bv)
34333b3b041SDavid Howells 		return -ENOMEM;
34433b3b041SDavid Howells 
345e69f37bcSDavid Howells 	pages = (struct page **)(bv + npages);
34633b3b041SDavid Howells 	npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
34733b3b041SDavid Howells 	if (!npages) {
34833b3b041SDavid Howells 		kfree(bv);
34933b3b041SDavid Howells 		return -ENOMEM;
35033b3b041SDavid Howells 	}
35133b3b041SDavid Howells 
35233b3b041SDavid Howells 	remain = len = min_t(size_t, len, npages * PAGE_SIZE);
35333b3b041SDavid Howells 
35433b3b041SDavid Howells 	for (i = 0; i < npages; i++) {
35533b3b041SDavid Howells 		chunk = min_t(size_t, PAGE_SIZE, remain);
35633b3b041SDavid Howells 		bv[i].bv_page = pages[i];
35733b3b041SDavid Howells 		bv[i].bv_offset = 0;
35833b3b041SDavid Howells 		bv[i].bv_len = chunk;
35933b3b041SDavid Howells 		remain -= chunk;
36033b3b041SDavid Howells 	}
36133b3b041SDavid Howells 
36233b3b041SDavid Howells 	/* Do the I/O */
36333b3b041SDavid Howells 	iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
36433b3b041SDavid Howells 	init_sync_kiocb(&kiocb, in);
36533b3b041SDavid Howells 	kiocb.ki_pos = *ppos;
36633b3b041SDavid Howells 	ret = call_read_iter(in, &kiocb, &to);
36733b3b041SDavid Howells 
36833b3b041SDavid Howells 	if (ret > 0) {
369e69f37bcSDavid Howells 		keep = DIV_ROUND_UP(ret, PAGE_SIZE);
37033b3b041SDavid Howells 		*ppos = kiocb.ki_pos;
37133b3b041SDavid Howells 		file_accessed(in);
37233b3b041SDavid Howells 	} else if (ret < 0) {
37333b3b041SDavid Howells 		/*
37433b3b041SDavid Howells 		 * callers of ->splice_read() expect -EAGAIN on
37533b3b041SDavid Howells 		 * "can't put anything in there", rather than -EFAULT.
37633b3b041SDavid Howells 		 */
37733b3b041SDavid Howells 		if (ret == -EFAULT)
37833b3b041SDavid Howells 			ret = -EAGAIN;
37933b3b041SDavid Howells 	}
38033b3b041SDavid Howells 
38133b3b041SDavid Howells 	/* Free any pages that didn't get touched at all. */
382e69f37bcSDavid Howells 	if (keep < npages)
383e69f37bcSDavid Howells 		release_pages(pages + keep, npages - keep);
38433b3b041SDavid Howells 
38533b3b041SDavid Howells 	/* Push the remaining pages into the pipe. */
386e69f37bcSDavid Howells 	remain = ret;
387e69f37bcSDavid Howells 	for (i = 0; i < keep; i++) {
38833b3b041SDavid Howells 		struct pipe_buffer *buf = pipe_head_buf(pipe);
38933b3b041SDavid Howells 
39033b3b041SDavid Howells 		chunk = min_t(size_t, remain, PAGE_SIZE);
39133b3b041SDavid Howells 		*buf = (struct pipe_buffer) {
39233b3b041SDavid Howells 			.ops	= &default_pipe_buf_ops,
39333b3b041SDavid Howells 			.page	= bv[i].bv_page,
39433b3b041SDavid Howells 			.offset	= 0,
39533b3b041SDavid Howells 			.len	= chunk,
39633b3b041SDavid Howells 		};
39733b3b041SDavid Howells 		pipe->head++;
39833b3b041SDavid Howells 		remain -= chunk;
39933b3b041SDavid Howells 	}
40033b3b041SDavid Howells 
40133b3b041SDavid Howells 	kfree(bv);
40233b3b041SDavid Howells 	return ret;
40333b3b041SDavid Howells }
40469df79a4SDavid Howells EXPORT_SYMBOL(copy_splice_read);
40533b3b041SDavid Howells 
406241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
4076818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
408c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
4096818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
4106818173bSMiklos Szeredi };
4116818173bSMiklos Szeredi 
41228a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
41328a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
41428a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
41528a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
41628a625cbSMiklos Szeredi };
41728a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
41828a625cbSMiklos Szeredi 
4195274f052SJens Axboe /*
4204f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
421016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4225274f052SJens Axboe  */
42376ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4245274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4255274f052SJens Axboe {
4266a14b90bSJens Axboe 	struct file *file = sd->u.file;
4275274f052SJens Axboe 	loff_t pos = sd->pos;
428a8adbe37SMichał Mirosław 	int more;
4295274f052SJens Axboe 
43072c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
431a8adbe37SMichał Mirosław 		return -EINVAL;
432a8adbe37SMichał Mirosław 
43335f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
434ae62ca7bSEric Dumazet 
4358cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4368cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
43735f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
438ae62ca7bSEric Dumazet 
439a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
440f84d7519SJens Axboe 				    sd->len, &pos, more);
4415274f052SJens Axboe }
4425274f052SJens Axboe 
443b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
444b3c2d2ddSMiklos Szeredi {
445b3c2d2ddSMiklos Szeredi 	smp_mb();
4460ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4470ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
448b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
449b3c2d2ddSMiklos Szeredi }
450b3c2d2ddSMiklos Szeredi 
451b3c2d2ddSMiklos Szeredi /**
452b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
453b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
454b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
455b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
456b3c2d2ddSMiklos Szeredi  *
457b3c2d2ddSMiklos Szeredi  * Description:
458b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
459b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
460b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
461b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
462b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
463b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
464b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
465b3c2d2ddSMiklos Szeredi  *
466b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
467b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
468b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
469b3c2d2ddSMiklos Szeredi  *    destination.
470b3c2d2ddSMiklos Szeredi  */
47196f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
472b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
473b3c2d2ddSMiklos Szeredi {
4748cefc107SDavid Howells 	unsigned int head = pipe->head;
4758cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4768cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
477b3c2d2ddSMiklos Szeredi 	int ret;
478b3c2d2ddSMiklos Szeredi 
479ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4808cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
481b3c2d2ddSMiklos Szeredi 
482b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
483b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
484b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
485b3c2d2ddSMiklos Szeredi 
486fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
487a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
488b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
489b3c2d2ddSMiklos Szeredi 				ret = 0;
490b3c2d2ddSMiklos Szeredi 			return ret;
491b3c2d2ddSMiklos Szeredi 		}
492a8adbe37SMichał Mirosław 
493a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
494a8adbe37SMichał Mirosław 		if (ret <= 0)
495a8adbe37SMichał Mirosław 			return ret;
496a8adbe37SMichał Mirosław 
497b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
498b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
499b3c2d2ddSMiklos Szeredi 
500b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
501b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
502b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
503b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
504b3c2d2ddSMiklos Szeredi 
505b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
506a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5078cefc107SDavid Howells 			tail++;
5088cefc107SDavid Howells 			pipe->tail = tail;
5096447a3cfSAl Viro 			if (pipe->files)
510b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
511b3c2d2ddSMiklos Szeredi 		}
512b3c2d2ddSMiklos Szeredi 
513b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
514b3c2d2ddSMiklos Szeredi 			return 0;
515b3c2d2ddSMiklos Szeredi 	}
516b3c2d2ddSMiklos Szeredi 
517b3c2d2ddSMiklos Szeredi 	return 1;
518b3c2d2ddSMiklos Szeredi }
519b3c2d2ddSMiklos Szeredi 
520d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
521d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
522d1a819a2SLinus Torvalds {
523d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
524d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
525d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
526d1a819a2SLinus Torvalds 
527d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
528d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
529d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
530d1a819a2SLinus Torvalds 		return true;
531d1a819a2SLinus Torvalds 	}
532d1a819a2SLinus Torvalds 
533d1a819a2SLinus Torvalds 	return false;
534d1a819a2SLinus Torvalds }
535d1a819a2SLinus Torvalds 
536b3c2d2ddSMiklos Szeredi /**
537b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
538b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
539b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
540b3c2d2ddSMiklos Szeredi  *
541b3c2d2ddSMiklos Szeredi  * Description:
542b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
543b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
544b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
545b3c2d2ddSMiklos Szeredi  */
54696f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
547b3c2d2ddSMiklos Szeredi {
548c725bfceSJan Kara 	/*
549c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
550c725bfceSJan Kara 	 * always buffers available
551c725bfceSJan Kara 	 */
552c725bfceSJan Kara 	if (signal_pending(current))
553c725bfceSJan Kara 		return -ERESTARTSYS;
554c725bfceSJan Kara 
555d1a819a2SLinus Torvalds repeat:
5568cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
557b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
558b3c2d2ddSMiklos Szeredi 			return 0;
559b3c2d2ddSMiklos Szeredi 
560a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
561b3c2d2ddSMiklos Szeredi 			return 0;
562b3c2d2ddSMiklos Szeredi 
563b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
564b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
565b3c2d2ddSMiklos Szeredi 
566b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
567b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
568b3c2d2ddSMiklos Szeredi 
569b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
570b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
571b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
572b3c2d2ddSMiklos Szeredi 		}
573b3c2d2ddSMiklos Szeredi 
574472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
575b3c2d2ddSMiklos Szeredi 	}
576b3c2d2ddSMiklos Szeredi 
577d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
578d1a819a2SLinus Torvalds 		goto repeat;
579d1a819a2SLinus Torvalds 
580b3c2d2ddSMiklos Szeredi 	return 1;
581b3c2d2ddSMiklos Szeredi }
582b3c2d2ddSMiklos Szeredi 
583b3c2d2ddSMiklos Szeredi /**
584b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
585b80901bbSRandy Dunlap  * @sd:		information about the splice operation
586b3c2d2ddSMiklos Szeredi  *
587b3c2d2ddSMiklos Szeredi  * Description:
588b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
589b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
590b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
591b3c2d2ddSMiklos Szeredi  */
59296f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
593b3c2d2ddSMiklos Szeredi {
594b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
595b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
596b3c2d2ddSMiklos Szeredi }
597b3c2d2ddSMiklos Szeredi 
598b3c2d2ddSMiklos Szeredi /**
599b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
600b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
601b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
602b3c2d2ddSMiklos Szeredi  *
603b3c2d2ddSMiklos Szeredi  * Description:
604b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
605b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
606b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
607b3c2d2ddSMiklos Szeredi  */
60896f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
609b3c2d2ddSMiklos Szeredi {
610b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
611b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
612b3c2d2ddSMiklos Szeredi }
613b3c2d2ddSMiklos Szeredi 
614932cc6d4SJens Axboe /**
615932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
616932cc6d4SJens Axboe  * @pipe:	pipe to splice from
617932cc6d4SJens Axboe  * @sd:		information to @actor
618932cc6d4SJens Axboe  * @actor:	handler that splices the data
619932cc6d4SJens Axboe  *
620932cc6d4SJens Axboe  * Description:
621932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
622932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
623932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
624932cc6d4SJens Axboe  *    pipe_to_user.
625932cc6d4SJens Axboe  *
62683f9135bSJens Axboe  */
627c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
628c66ab6faSJens Axboe 			   splice_actor *actor)
6295274f052SJens Axboe {
630b3c2d2ddSMiklos Szeredi 	int ret;
6315274f052SJens Axboe 
632b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
633b3c2d2ddSMiklos Szeredi 	do {
634c2489e07SJan Kara 		cond_resched();
635b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
636b3c2d2ddSMiklos Szeredi 		if (ret > 0)
637b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
638b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
639b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6405274f052SJens Axboe 
641b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6425274f052SJens Axboe }
64340bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6445274f052SJens Axboe 
645932cc6d4SJens Axboe /**
646932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
647932cc6d4SJens Axboe  * @pipe:	pipe to splice from
648932cc6d4SJens Axboe  * @out:	file to splice to
649932cc6d4SJens Axboe  * @ppos:	position in @out
650932cc6d4SJens Axboe  * @len:	how many bytes to splice
651932cc6d4SJens Axboe  * @flags:	splice modifier flags
652932cc6d4SJens Axboe  * @actor:	handler that splices the data
653932cc6d4SJens Axboe  *
654932cc6d4SJens Axboe  * Description:
6552933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
656932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
657932cc6d4SJens Axboe  *
658932cc6d4SJens Axboe  */
6596da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6606da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6616da61809SMark Fasheh 			 splice_actor *actor)
6626da61809SMark Fasheh {
6636da61809SMark Fasheh 	ssize_t ret;
664c66ab6faSJens Axboe 	struct splice_desc sd = {
665c66ab6faSJens Axboe 		.total_len = len,
666c66ab6faSJens Axboe 		.flags = flags,
667c66ab6faSJens Axboe 		.pos = *ppos,
6686a14b90bSJens Axboe 		.u.file = out,
669c66ab6faSJens Axboe 	};
6706da61809SMark Fasheh 
67161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
672c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
67361e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6746da61809SMark Fasheh 
6756da61809SMark Fasheh 	return ret;
6766da61809SMark Fasheh }
6776da61809SMark Fasheh 
6786da61809SMark Fasheh /**
6798d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6808d020765SAl Viro  * @pipe:	pipe info
6818d020765SAl Viro  * @out:	file to write to
6828d020765SAl Viro  * @ppos:	position in @out
6838d020765SAl Viro  * @len:	number of bytes to splice
6848d020765SAl Viro  * @flags:	splice modifier flags
6858d020765SAl Viro  *
6868d020765SAl Viro  * Description:
6878d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6888d020765SAl Viro  *    the given pipe inode to the given file.
6898d020765SAl Viro  *    This one is ->write_iter-based.
6908d020765SAl Viro  *
6918d020765SAl Viro  */
6928d020765SAl Viro ssize_t
6938d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6948d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
6958d020765SAl Viro {
6968d020765SAl Viro 	struct splice_desc sd = {
6978d020765SAl Viro 		.total_len = len,
6988d020765SAl Viro 		.flags = flags,
6998d020765SAl Viro 		.pos = *ppos,
7008d020765SAl Viro 		.u.file = out,
7018d020765SAl Viro 	};
7026718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
7038d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7048d020765SAl Viro 					GFP_KERNEL);
7058d020765SAl Viro 	ssize_t ret;
7068d020765SAl Viro 
7078d020765SAl Viro 	if (unlikely(!array))
7088d020765SAl Viro 		return -ENOMEM;
7098d020765SAl Viro 
7108d020765SAl Viro 	pipe_lock(pipe);
7118d020765SAl Viro 
7128d020765SAl Viro 	splice_from_pipe_begin(&sd);
7138d020765SAl Viro 	while (sd.total_len) {
7148d020765SAl Viro 		struct iov_iter from;
715ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7168d020765SAl Viro 		size_t left;
7178cefc107SDavid Howells 		int n;
7188d020765SAl Viro 
7198d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7208d020765SAl Viro 		if (ret <= 0)
7218d020765SAl Viro 			break;
7228d020765SAl Viro 
7236718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7248d020765SAl Viro 			kfree(array);
7256718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7268d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7278d020765SAl Viro 					GFP_KERNEL);
7288d020765SAl Viro 			if (!array) {
7298d020765SAl Viro 				ret = -ENOMEM;
7308d020765SAl Viro 				break;
7318d020765SAl Viro 			}
7328d020765SAl Viro 		}
7338d020765SAl Viro 
734ec057595SLinus Torvalds 		head = pipe->head;
735ec057595SLinus Torvalds 		tail = pipe->tail;
736ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
737ec057595SLinus Torvalds 
7388d020765SAl Viro 		/* build the vector */
7398d020765SAl Viro 		left = sd.total_len;
7400f1d344fSPavel Begunkov 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
7418cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7428d020765SAl Viro 			size_t this_len = buf->len;
7438d020765SAl Viro 
7440f1d344fSPavel Begunkov 			/* zero-length bvecs are not supported, skip them */
7450f1d344fSPavel Begunkov 			if (!this_len)
7460f1d344fSPavel Begunkov 				continue;
7470f1d344fSPavel Begunkov 			this_len = min(this_len, left);
7488d020765SAl Viro 
749fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7508d020765SAl Viro 			if (unlikely(ret)) {
7518d020765SAl Viro 				if (ret == -ENODATA)
7528d020765SAl Viro 					ret = 0;
7538d020765SAl Viro 				goto done;
7548d020765SAl Viro 			}
7558d020765SAl Viro 
756664e4078SChristoph Hellwig 			bvec_set_page(&array[n], buf->page, this_len,
757664e4078SChristoph Hellwig 				      buf->offset);
7588d020765SAl Viro 			left -= this_len;
7590f1d344fSPavel Begunkov 			n++;
7608d020765SAl Viro 		}
7618d020765SAl Viro 
762de4eda9dSAl Viro 		iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
763abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7648d020765SAl Viro 		if (ret <= 0)
7658d020765SAl Viro 			break;
7668d020765SAl Viro 
7678d020765SAl Viro 		sd.num_spliced += ret;
7688d020765SAl Viro 		sd.total_len -= ret;
769dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7708d020765SAl Viro 
7718d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7728cefc107SDavid Howells 		tail = pipe->tail;
7738d020765SAl Viro 		while (ret) {
7748cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7758d020765SAl Viro 			if (ret >= buf->len) {
7768d020765SAl Viro 				ret -= buf->len;
7778d020765SAl Viro 				buf->len = 0;
778a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7798cefc107SDavid Howells 				tail++;
7808cefc107SDavid Howells 				pipe->tail = tail;
7818d020765SAl Viro 				if (pipe->files)
7828d020765SAl Viro 					sd.need_wakeup = true;
7838d020765SAl Viro 			} else {
7848d020765SAl Viro 				buf->offset += ret;
7858d020765SAl Viro 				buf->len -= ret;
7868d020765SAl Viro 				ret = 0;
7878d020765SAl Viro 			}
7888d020765SAl Viro 		}
7898d020765SAl Viro 	}
7908d020765SAl Viro done:
7918d020765SAl Viro 	kfree(array);
7928d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7938d020765SAl Viro 
7948d020765SAl Viro 	pipe_unlock(pipe);
7958d020765SAl Viro 
7968d020765SAl Viro 	if (sd.num_spliced)
7978d020765SAl Viro 		ret = sd.num_spliced;
7988d020765SAl Viro 
7998d020765SAl Viro 	return ret;
8008d020765SAl Viro }
8018d020765SAl Viro 
8028d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8038d020765SAl Viro 
80483f9135bSJens Axboe /**
80583f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
806932cc6d4SJens Axboe  * @pipe:	pipe to splice from
80783f9135bSJens Axboe  * @out:	socket to write to
808932cc6d4SJens Axboe  * @ppos:	position in @out
80983f9135bSJens Axboe  * @len:	number of bytes to splice
81083f9135bSJens Axboe  * @flags:	splice modifier flags
81183f9135bSJens Axboe  *
812932cc6d4SJens Axboe  * Description:
81383f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
81483f9135bSJens Axboe  *    is involved.
81583f9135bSJens Axboe  *
81683f9135bSJens Axboe  */
8173a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
818cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8195274f052SJens Axboe {
82000522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8215274f052SJens Axboe }
8225274f052SJens Axboe 
823059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
824a0f06780SJeff Garzik 
82536e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op)
82636e2c742SChristoph Hellwig {
82736e2c742SChristoph Hellwig 	pr_debug_ratelimited(
82836e2c742SChristoph Hellwig 		"splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
82936e2c742SChristoph Hellwig 		op, file, current->pid, current->comm);
83036e2c742SChristoph Hellwig 	return -EINVAL;
83136e2c742SChristoph Hellwig }
83236e2c742SChristoph Hellwig 
83383f9135bSJens Axboe /*
83483f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
83583f9135bSJens Axboe  */
8363a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
837cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8385274f052SJens Axboe {
83936e2c742SChristoph Hellwig 	if (unlikely(!out->f_op->splice_write))
84036e2c742SChristoph Hellwig 		return warn_unsupported(out, "write");
84100c285d0SChristoph Hellwig 	return out->f_op->splice_write(pipe, out, ppos, len, flags);
8425274f052SJens Axboe }
8435274f052SJens Axboe 
8446a3f30b8SDavid Howells /**
8456a3f30b8SDavid Howells  * vfs_splice_read - Read data from a file and splice it into a pipe
8466a3f30b8SDavid Howells  * @in:		File to splice from
8476a3f30b8SDavid Howells  * @ppos:	Input file offset
8486a3f30b8SDavid Howells  * @pipe:	Pipe to splice to
8496a3f30b8SDavid Howells  * @len:	Number of bytes to splice
8506a3f30b8SDavid Howells  * @flags:	Splice modifier flags (SPLICE_F_*)
8516a3f30b8SDavid Howells  *
8526a3f30b8SDavid Howells  * Splice the requested amount of data from the input file to the pipe.  This
8536a3f30b8SDavid Howells  * is synchronous as the caller must hold the pipe lock across the entire
8546a3f30b8SDavid Howells  * operation.
8556a3f30b8SDavid Howells  *
8566a3f30b8SDavid Howells  * If successful, it returns the amount of data spliced, 0 if it hit the EOF or
8576a3f30b8SDavid Howells  * a hole and a negative error code otherwise.
85883f9135bSJens Axboe  */
8596a3f30b8SDavid Howells long vfs_splice_read(struct file *in, loff_t *ppos,
860cbb7e577SJens Axboe 		     struct pipe_inode_info *pipe, size_t len,
861cbb7e577SJens Axboe 		     unsigned int flags)
8625274f052SJens Axboe {
863313d64a3SAl Viro 	unsigned int p_space;
8645274f052SJens Axboe 	int ret;
8655274f052SJens Axboe 
86649570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8675274f052SJens Axboe 		return -EBADF;
868123856f0SDavid Howells 	if (!len)
869123856f0SDavid Howells 		return 0;
8705274f052SJens Axboe 
871313d64a3SAl Viro 	/* Don't try to read more the pipe has space for. */
872313d64a3SAl Viro 	p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
873313d64a3SAl Viro 	len = min_t(size_t, len, p_space << PAGE_SHIFT);
874313d64a3SAl Viro 
875cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8765274f052SJens Axboe 	if (unlikely(ret < 0))
8775274f052SJens Axboe 		return ret;
8785274f052SJens Axboe 
87903cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
88003cc0789SAl Viro 		len = MAX_RW_COUNT;
88103cc0789SAl Viro 
88236e2c742SChristoph Hellwig 	if (unlikely(!in->f_op->splice_read))
88336e2c742SChristoph Hellwig 		return warn_unsupported(in, "read");
884aa3dbde8SDavid Howells 	/*
885b85930a0SDavid Howells 	 * O_DIRECT and DAX don't deal with the pagecache, so we allocate a
886b85930a0SDavid Howells 	 * buffer, copy into it and splice that into the pipe.
887aa3dbde8SDavid Howells 	 */
888b85930a0SDavid Howells 	if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host))
889aa3dbde8SDavid Howells 		return copy_splice_read(in, ppos, pipe, len, flags);
8902bc01060SChristoph Hellwig 	return in->f_op->splice_read(in, ppos, pipe, len, flags);
8915274f052SJens Axboe }
8926a3f30b8SDavid Howells EXPORT_SYMBOL_GPL(vfs_splice_read);
8935274f052SJens Axboe 
894932cc6d4SJens Axboe /**
895932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
896932cc6d4SJens Axboe  * @in:		file to splice from
897932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
898932cc6d4SJens Axboe  * @actor:	handles the data splicing
899932cc6d4SJens Axboe  *
900932cc6d4SJens Axboe  * Description:
901932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
902932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
903932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
904932cc6d4SJens Axboe  *    that process.
905932cc6d4SJens Axboe  *
906c66ab6faSJens Axboe  */
907c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
908c66ab6faSJens Axboe 			       splice_direct_actor *actor)
909b92ce558SJens Axboe {
910b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
911b92ce558SJens Axboe 	long ret, bytes;
912c66ab6faSJens Axboe 	size_t len;
9130ff28d9fSChristophe Leroy 	int i, flags, more;
914b92ce558SJens Axboe 
915b92ce558SJens Axboe 	/*
91697ef77c5SJason A. Donenfeld 	 * We require the input to be seekable, as we don't want to randomly
91797ef77c5SJason A. Donenfeld 	 * drop data for eg socket -> socket splicing. Use the piped splicing
91897ef77c5SJason A. Donenfeld 	 * for that!
919b92ce558SJens Axboe 	 */
92097ef77c5SJason A. Donenfeld 	if (unlikely(!(in->f_mode & FMODE_LSEEK)))
921b92ce558SJens Axboe 		return -EINVAL;
922b92ce558SJens Axboe 
923b92ce558SJens Axboe 	/*
924b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
925b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
926b92ce558SJens Axboe 	 */
927b92ce558SJens Axboe 	pipe = current->splice_pipe;
92849570e9bSJens Axboe 	if (unlikely(!pipe)) {
9297bee130eSAl Viro 		pipe = alloc_pipe_info();
930b92ce558SJens Axboe 		if (!pipe)
931b92ce558SJens Axboe 			return -ENOMEM;
932b92ce558SJens Axboe 
933b92ce558SJens Axboe 		/*
934b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
93500522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
936b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
937b92ce558SJens Axboe 		 */
938b92ce558SJens Axboe 		pipe->readers = 1;
939b92ce558SJens Axboe 
940b92ce558SJens Axboe 		current->splice_pipe = pipe;
941b92ce558SJens Axboe 	}
942b92ce558SJens Axboe 
943b92ce558SJens Axboe 	/*
94473d62d83SIngo Molnar 	 * Do the splice.
945b92ce558SJens Axboe 	 */
946b92ce558SJens Axboe 	bytes = 0;
947c66ab6faSJens Axboe 	len = sd->total_len;
948c66ab6faSJens Axboe 	flags = sd->flags;
949c66ab6faSJens Axboe 
950c66ab6faSJens Axboe 	/*
951c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
952c66ab6faSJens Axboe 	 */
953c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9540ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
955b92ce558SJens Axboe 
9568cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
95717614445SDarrick J. Wong 
958b92ce558SJens Axboe 	while (len) {
95951a92c0fSJens Axboe 		size_t read_len;
960a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
961b92ce558SJens Axboe 
9626a3f30b8SDavid Howells 		ret = vfs_splice_read(in, &pos, pipe, len, flags);
96351a92c0fSJens Axboe 		if (unlikely(ret <= 0))
964b92ce558SJens Axboe 			goto out_release;
965b92ce558SJens Axboe 
966b92ce558SJens Axboe 		read_len = ret;
967c66ab6faSJens Axboe 		sd->total_len = read_len;
968b92ce558SJens Axboe 
969b92ce558SJens Axboe 		/*
9700ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9710ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9720ff28d9fSChristophe Leroy 		 * initially, clears it.
9730ff28d9fSChristophe Leroy 		 */
9740ff28d9fSChristophe Leroy 		if (read_len < len)
9750ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9760ff28d9fSChristophe Leroy 		else if (!more)
9770ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9780ff28d9fSChristophe Leroy 		/*
979b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
980b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
981b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
982b92ce558SJens Axboe 		 */
983c66ab6faSJens Axboe 		ret = actor(pipe, sd);
984a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
985a82c53a0STom Zanussi 			sd->pos = prev_pos;
986b92ce558SJens Axboe 			goto out_release;
987a82c53a0STom Zanussi 		}
988b92ce558SJens Axboe 
989b92ce558SJens Axboe 		bytes += ret;
990b92ce558SJens Axboe 		len -= ret;
991bcd4f3acSJens Axboe 		sd->pos = pos;
992b92ce558SJens Axboe 
993a82c53a0STom Zanussi 		if (ret < read_len) {
994a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
99551a92c0fSJens Axboe 			goto out_release;
996b92ce558SJens Axboe 		}
997a82c53a0STom Zanussi 	}
998b92ce558SJens Axboe 
9999e97198dSJens Axboe done:
10008cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
10019e97198dSJens Axboe 	file_accessed(in);
1002b92ce558SJens Axboe 	return bytes;
1003b92ce558SJens Axboe 
1004b92ce558SJens Axboe out_release:
1005b92ce558SJens Axboe 	/*
1006b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1007b92ce558SJens Axboe 	 * the pipe buffers in question:
1008b92ce558SJens Axboe 	 */
10098cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
10108cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1011b92ce558SJens Axboe 
1012a779638cSMiklos Szeredi 		if (buf->ops)
1013a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1014b92ce558SJens Axboe 	}
1015b92ce558SJens Axboe 
10169e97198dSJens Axboe 	if (!bytes)
10179e97198dSJens Axboe 		bytes = ret;
1018b92ce558SJens Axboe 
10199e97198dSJens Axboe 	goto done;
1020c66ab6faSJens Axboe }
1021c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1022c66ab6faSJens Axboe 
1023c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1024c66ab6faSJens Axboe 			       struct splice_desc *sd)
1025c66ab6faSJens Axboe {
10266a14b90bSJens Axboe 	struct file *file = sd->u.file;
1027c66ab6faSJens Axboe 
10287995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10292cb4b05eSChangli Gao 			      sd->flags);
1030c66ab6faSJens Axboe }
1031c66ab6faSJens Axboe 
1032932cc6d4SJens Axboe /**
1033932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1034932cc6d4SJens Axboe  * @in:		file to splice from
1035932cc6d4SJens Axboe  * @ppos:	input file offset
1036932cc6d4SJens Axboe  * @out:	file to splice to
1037acdb37c3SRandy Dunlap  * @opos:	output file offset
1038932cc6d4SJens Axboe  * @len:	number of bytes to splice
1039932cc6d4SJens Axboe  * @flags:	splice modifier flags
1040932cc6d4SJens Axboe  *
1041932cc6d4SJens Axboe  * Description:
1042932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1043932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1044932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1045932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1046932cc6d4SJens Axboe  *
1047932cc6d4SJens Axboe  */
1048c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10497995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1050c66ab6faSJens Axboe {
1051c66ab6faSJens Axboe 	struct splice_desc sd = {
1052c66ab6faSJens Axboe 		.len		= len,
1053c66ab6faSJens Axboe 		.total_len	= len,
1054c66ab6faSJens Axboe 		.flags		= flags,
1055c66ab6faSJens Axboe 		.pos		= *ppos,
10566a14b90bSJens Axboe 		.u.file		= out,
10577995bd28SAl Viro 		.opos		= opos,
1058c66ab6faSJens Axboe 	};
105951a92c0fSJens Axboe 	long ret;
1060c66ab6faSJens Axboe 
106118c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
106218c67cb9SAl Viro 		return -EBADF;
106318c67cb9SAl Viro 
106418c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
106518c67cb9SAl Viro 		return -EINVAL;
106618c67cb9SAl Viro 
106718c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
106818c67cb9SAl Viro 	if (unlikely(ret < 0))
106918c67cb9SAl Viro 		return ret;
107018c67cb9SAl Viro 
1071c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
107251a92c0fSJens Axboe 	if (ret > 0)
1073a82c53a0STom Zanussi 		*ppos = sd.pos;
107451a92c0fSJens Axboe 
1075c66ab6faSJens Axboe 	return ret;
1076b92ce558SJens Axboe }
10771c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1078b92ce558SJens Axboe 
10798924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10808924feffSAl Viro {
108152bce911SLinus Torvalds 	for (;;) {
108252bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
108352bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
108452bce911SLinus Torvalds 			return -EPIPE;
108552bce911SLinus Torvalds 		}
10866718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
108752bce911SLinus Torvalds 			return 0;
10888924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10898924feffSAl Viro 			return -EAGAIN;
10908924feffSAl Viro 		if (signal_pending(current))
10918924feffSAl Viro 			return -ERESTARTSYS;
1092472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
10938924feffSAl Viro 	}
10948924feffSAl Viro }
10958924feffSAl Viro 
10967c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
10977c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
10987c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1099ddac0d39SJens Axboe 
1100b964bf53SAl Viro long splice_file_to_pipe(struct file *in,
1101faa97c48SAl Viro 			 struct pipe_inode_info *opipe,
1102faa97c48SAl Viro 			 loff_t *offset,
1103faa97c48SAl Viro 			 size_t len, unsigned int flags)
1104faa97c48SAl Viro {
1105faa97c48SAl Viro 	long ret;
1106faa97c48SAl Viro 
1107faa97c48SAl Viro 	pipe_lock(opipe);
1108faa97c48SAl Viro 	ret = wait_for_space(opipe, flags);
1109faa97c48SAl Viro 	if (!ret)
11106a3f30b8SDavid Howells 		ret = vfs_splice_read(in, offset, opipe, len, flags);
1111faa97c48SAl Viro 	pipe_unlock(opipe);
1112faa97c48SAl Viro 	if (ret > 0)
1113faa97c48SAl Viro 		wakeup_pipe_readers(opipe);
1114faa97c48SAl Viro 	return ret;
1115faa97c48SAl Viro }
1116faa97c48SAl Viro 
1117ddac0d39SJens Axboe /*
111883f9135bSJens Axboe  * Determine where to splice to/from.
111983f9135bSJens Axboe  */
1120ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1121ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
11225274f052SJens Axboe {
11237c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11247c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11257995bd28SAl Viro 	loff_t offset;
1126a4514ebdSJens Axboe 	long ret;
11275274f052SJens Axboe 
112890da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
112990da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
113090da2e3fSPavel Begunkov 		return -EBADF;
113190da2e3fSPavel Begunkov 
1132c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1133c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
11347c77f0b3SMiklos Szeredi 
11357c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11367c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11377c77f0b3SMiklos Szeredi 			return -ESPIPE;
11387c77f0b3SMiklos Szeredi 
11397c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11407c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11417c77f0b3SMiklos Szeredi 			return -EINVAL;
11427c77f0b3SMiklos Szeredi 
1143ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1144ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1145ee5e0011SSlavomir Kaslev 
11467c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11477c77f0b3SMiklos Szeredi 	}
11487c77f0b3SMiklos Szeredi 
11497c77f0b3SMiklos Szeredi 	if (ipipe) {
1150529565dcSIngo Molnar 		if (off_in)
1151529565dcSIngo Molnar 			return -ESPIPE;
1152b92ce558SJens Axboe 		if (off_out) {
115319c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1154b92ce558SJens Axboe 				return -EINVAL;
1155ee6e00c8SJens Axboe 			offset = *off_out;
11567995bd28SAl Viro 		} else {
11577995bd28SAl Viro 			offset = out->f_pos;
11587995bd28SAl Viro 		}
1159529565dcSIngo Molnar 
116018c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
116118c67cb9SAl Viro 			return -EINVAL;
116218c67cb9SAl Viro 
116318c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
116418c67cb9SAl Viro 		if (unlikely(ret < 0))
116518c67cb9SAl Viro 			return ret;
116618c67cb9SAl Viro 
1167ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1168ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1169ee5e0011SSlavomir Kaslev 
1170500368f7SAl Viro 		file_start_write(out);
11717995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1172500368f7SAl Viro 		file_end_write(out);
1173a4514ebdSJens Axboe 
1174983652c6SChung-Chiang Cheng 		if (ret > 0)
1175983652c6SChung-Chiang Cheng 			fsnotify_modify(out);
1176983652c6SChung-Chiang Cheng 
11777995bd28SAl Viro 		if (!off_out)
11787995bd28SAl Viro 			out->f_pos = offset;
1179ee6e00c8SJens Axboe 		else
1180ee6e00c8SJens Axboe 			*off_out = offset;
1181a4514ebdSJens Axboe 
1182a4514ebdSJens Axboe 		return ret;
1183529565dcSIngo Molnar 	}
11845274f052SJens Axboe 
11857c77f0b3SMiklos Szeredi 	if (opipe) {
1186529565dcSIngo Molnar 		if (off_out)
1187529565dcSIngo Molnar 			return -ESPIPE;
1188b92ce558SJens Axboe 		if (off_in) {
118919c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1190b92ce558SJens Axboe 				return -EINVAL;
1191ee6e00c8SJens Axboe 			offset = *off_in;
11927995bd28SAl Viro 		} else {
11937995bd28SAl Viro 			offset = in->f_pos;
11947995bd28SAl Viro 		}
1195529565dcSIngo Molnar 
1196ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1197ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1198ee5e0011SSlavomir Kaslev 
1199faa97c48SAl Viro 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1200983652c6SChung-Chiang Cheng 
1201983652c6SChung-Chiang Cheng 		if (ret > 0)
1202983652c6SChung-Chiang Cheng 			fsnotify_access(in);
1203983652c6SChung-Chiang Cheng 
12047995bd28SAl Viro 		if (!off_in)
12057995bd28SAl Viro 			in->f_pos = offset;
1206ee6e00c8SJens Axboe 		else
1207ee6e00c8SJens Axboe 			*off_in = offset;
1208a4514ebdSJens Axboe 
1209a4514ebdSJens Axboe 		return ret;
1210529565dcSIngo Molnar 	}
12115274f052SJens Axboe 
12125274f052SJens Axboe 	return -EINVAL;
12135274f052SJens Axboe }
12145274f052SJens Axboe 
1215ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1216ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1217ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1218ee6e00c8SJens Axboe {
1219ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1220ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1221ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1222ee6e00c8SJens Axboe 	long ret;
1223ee6e00c8SJens Axboe 
1224ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1225ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1226ee6e00c8SJens Axboe 
12270f99fc51SJens Axboe 	if (ipipe) {
12280f99fc51SJens Axboe 		if (off_in)
1229ee6e00c8SJens Axboe 			return -ESPIPE;
12300f99fc51SJens Axboe 		pipe_clear_nowait(in);
12310f99fc51SJens Axboe 	}
12320f99fc51SJens Axboe 	if (opipe) {
12330f99fc51SJens Axboe 		if (off_out)
1234ee6e00c8SJens Axboe 			return -ESPIPE;
12350f99fc51SJens Axboe 		pipe_clear_nowait(out);
12360f99fc51SJens Axboe 	}
1237ee6e00c8SJens Axboe 
1238ee6e00c8SJens Axboe 	if (off_out) {
1239ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1240ee6e00c8SJens Axboe 			return -EFAULT;
1241ee6e00c8SJens Axboe 		__off_out = &offset;
1242ee6e00c8SJens Axboe 	}
1243ee6e00c8SJens Axboe 	if (off_in) {
1244ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1245ee6e00c8SJens Axboe 			return -EFAULT;
1246ee6e00c8SJens Axboe 		__off_in = &offset;
1247ee6e00c8SJens Axboe 	}
1248ee6e00c8SJens Axboe 
1249ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1250ee6e00c8SJens Axboe 	if (ret < 0)
1251ee6e00c8SJens Axboe 		return ret;
1252ee6e00c8SJens Axboe 
1253ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1254ee6e00c8SJens Axboe 		return -EFAULT;
1255ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1256ee6e00c8SJens Axboe 		return -EFAULT;
1257ee6e00c8SJens Axboe 
1258ee6e00c8SJens Axboe 	return ret;
1259ee6e00c8SJens Axboe }
1260ee6e00c8SJens Axboe 
126179fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
126279fddc4eSAl Viro 			struct pipe_inode_info *pipe,
126379fddc4eSAl Viro 			unsigned flags)
1264912d35f8SJens Axboe {
126579fddc4eSAl Viro 	struct pipe_buffer buf = {
126679fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
126779fddc4eSAl Viro 		.flags = flags
126879fddc4eSAl Viro 	};
126979fddc4eSAl Viro 	size_t total = 0;
127079fddc4eSAl Viro 	int ret = 0;
127179fddc4eSAl Viro 
12727d690c15SAl Viro 	while (iov_iter_count(from)) {
127379fddc4eSAl Viro 		struct page *pages[16];
12747d690c15SAl Viro 		ssize_t left;
1275db85a9ebSAl Viro 		size_t start;
12767d690c15SAl Viro 		int i, n;
1277912d35f8SJens Axboe 
12787d690c15SAl Viro 		left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
12797d690c15SAl Viro 		if (left <= 0) {
12807d690c15SAl Viro 			ret = left;
128179fddc4eSAl Viro 			break;
128279fddc4eSAl Viro 		}
1283912d35f8SJens Axboe 
12847d690c15SAl Viro 		n = DIV_ROUND_UP(left + start, PAGE_SIZE);
12857d690c15SAl Viro 		for (i = 0; i < n; i++) {
12867d690c15SAl Viro 			int size = min_t(int, left, PAGE_SIZE - start);
12877d690c15SAl Viro 
12887d690c15SAl Viro 			buf.page = pages[i];
128979fddc4eSAl Viro 			buf.offset = start;
129079fddc4eSAl Viro 			buf.len = size;
129179fddc4eSAl Viro 			ret = add_to_pipe(pipe, &buf);
129279fddc4eSAl Viro 			if (unlikely(ret < 0)) {
12937d690c15SAl Viro 				iov_iter_revert(from, left);
12947d690c15SAl Viro 				// this one got dropped by add_to_pipe()
12957d690c15SAl Viro 				while (++i < n)
12967d690c15SAl Viro 					put_page(pages[i]);
12977d690c15SAl Viro 				goto out;
12987d690c15SAl Viro 			}
129979fddc4eSAl Viro 			total += ret;
13007d690c15SAl Viro 			left -= size;
13017d690c15SAl Viro 			start = 0;
1302912d35f8SJens Axboe 		}
1303912d35f8SJens Axboe 	}
13047d690c15SAl Viro out:
130579fddc4eSAl Viro 	return total ? total : ret;
1306912d35f8SJens Axboe }
1307912d35f8SJens Axboe 
13086a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
13096a14b90bSJens Axboe 			struct splice_desc *sd)
13106a14b90bSJens Axboe {
13116130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
13126130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
13136a14b90bSJens Axboe }
13146a14b90bSJens Axboe 
13156a14b90bSJens Axboe /*
13166a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
13176a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
13186a14b90bSJens Axboe  */
131987a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
132087a3002aSAl Viro 			     unsigned int flags)
13216a14b90bSJens Axboe {
1322c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
132387a3002aSAl Viro 	struct splice_desc sd = {
132487a3002aSAl Viro 		.total_len = iov_iter_count(iter),
132587a3002aSAl Viro 		.flags = flags,
132687a3002aSAl Viro 		.u.data = iter
132787a3002aSAl Viro 	};
132887a3002aSAl Viro 	long ret = 0;
13296a14b90bSJens Axboe 
13306a14b90bSJens Axboe 	if (!pipe)
13316a14b90bSJens Axboe 		return -EBADF;
13326a14b90bSJens Axboe 
13330f99fc51SJens Axboe 	pipe_clear_nowait(file);
13340f99fc51SJens Axboe 
1335345995faSAl Viro 	if (sd.total_len) {
13366130f531SAl Viro 		pipe_lock(pipe);
13376130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
133861e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1339345995faSAl Viro 	}
13406a14b90bSJens Axboe 
13416a14b90bSJens Axboe 	return ret;
13426a14b90bSJens Axboe }
13436a14b90bSJens Axboe 
1344912d35f8SJens Axboe /*
1345912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1346912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1347912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1348912d35f8SJens Axboe  */
134987a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
135087a3002aSAl Viro 			     unsigned int flags)
1351912d35f8SJens Axboe {
1352ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
135387a3002aSAl Viro 	long ret = 0;
135479fddc4eSAl Viro 	unsigned buf_flag = 0;
135579fddc4eSAl Viro 
135679fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
135779fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1358912d35f8SJens Axboe 
1359c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1360ddac0d39SJens Axboe 	if (!pipe)
1361912d35f8SJens Axboe 		return -EBADF;
1362912d35f8SJens Axboe 
13630f99fc51SJens Axboe 	pipe_clear_nowait(file);
13640f99fc51SJens Axboe 
13658924feffSAl Viro 	pipe_lock(pipe);
13668924feffSAl Viro 	ret = wait_for_space(pipe, flags);
136779fddc4eSAl Viro 	if (!ret)
136887a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13698924feffSAl Viro 	pipe_unlock(pipe);
13708924feffSAl Viro 	if (ret > 0)
13718924feffSAl Viro 		wakeup_pipe_readers(pipe);
137235f3d14dSJens Axboe 	return ret;
1373912d35f8SJens Axboe }
1374912d35f8SJens Axboe 
137587a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
137687a3002aSAl Viro {
137787a3002aSAl Viro 	if (!f.file)
137887a3002aSAl Viro 		return -EBADF;
137987a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
1380de4eda9dSAl Viro 		*type = ITER_SOURCE;
138187a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
1382de4eda9dSAl Viro 		*type = ITER_DEST;
138387a3002aSAl Viro 	} else {
138487a3002aSAl Viro 		fdput(f);
138587a3002aSAl Viro 		return -EBADF;
138687a3002aSAl Viro 	}
138787a3002aSAl Viro 	return 0;
138887a3002aSAl Viro }
138987a3002aSAl Viro 
13906a14b90bSJens Axboe /*
13916a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13926a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13936a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13946a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13956a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13966a14b90bSJens Axboe  * solutions for that:
13976a14b90bSJens Axboe  *
13986a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13996a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
14006a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
14016a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
14026a14b90bSJens Axboe  *
14036a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
14046a14b90bSJens Axboe  *
14056a14b90bSJens Axboe  */
140687a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
140730cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
140830cfe4efSDominik Brodowski {
140987a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
141087a3002aSAl Viro 	struct iovec *iov = iovstack;
141187a3002aSAl Viro 	struct iov_iter iter;
141287e5e6daSJens Axboe 	ssize_t error;
141387a3002aSAl Viro 	struct fd f;
141487a3002aSAl Viro 	int type;
141587a3002aSAl Viro 
1416598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1417598b3cecSChristoph Hellwig 		return -EINVAL;
1418598b3cecSChristoph Hellwig 
141987a3002aSAl Viro 	f = fdget(fd);
142087a3002aSAl Viro 	error = vmsplice_type(f, &type);
142187a3002aSAl Viro 	if (error)
142287a3002aSAl Viro 		return error;
142387a3002aSAl Viro 
142487a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
142587a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1426598b3cecSChristoph Hellwig 	if (error < 0)
1427598b3cecSChristoph Hellwig 		goto out_fdput;
1428598b3cecSChristoph Hellwig 
1429598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1430598b3cecSChristoph Hellwig 		error = 0;
1431de4eda9dSAl Viro 	else if (type == ITER_SOURCE)
1432598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1433598b3cecSChristoph Hellwig 	else
1434598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1435598b3cecSChristoph Hellwig 
143687a3002aSAl Viro 	kfree(iov);
1437598b3cecSChristoph Hellwig out_fdput:
143887a3002aSAl Viro 	fdput(f);
143987a3002aSAl Viro 	return error;
144030cfe4efSDominik Brodowski }
144130cfe4efSDominik Brodowski 
1442836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1443836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1444836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14455274f052SJens Axboe {
14462903ff01SAl Viro 	struct fd in, out;
14475274f052SJens Axboe 	long error;
14485274f052SJens Axboe 
14495274f052SJens Axboe 	if (unlikely(!len))
14505274f052SJens Axboe 		return 0;
14515274f052SJens Axboe 
14523d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14533d6ea290SAl Viro 		return -EINVAL;
14543d6ea290SAl Viro 
14555274f052SJens Axboe 	error = -EBADF;
14562903ff01SAl Viro 	in = fdget(fd_in);
14572903ff01SAl Viro 	if (in.file) {
14582903ff01SAl Viro 		out = fdget(fd_out);
14592903ff01SAl Viro 		if (out.file) {
1460ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1461529565dcSIngo Molnar 						len, flags);
14622903ff01SAl Viro 			fdput(out);
14635274f052SJens Axboe 		}
14642903ff01SAl Viro 		fdput(in);
14655274f052SJens Axboe 	}
14665274f052SJens Axboe 	return error;
14675274f052SJens Axboe }
146870524490SJens Axboe 
146970524490SJens Axboe /*
1470aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1471aadd06e5SJens Axboe  * return an appropriate error.
1472aadd06e5SJens Axboe  */
14737c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1474aadd06e5SJens Axboe {
1475aadd06e5SJens Axboe 	int ret;
1476aadd06e5SJens Axboe 
1477aadd06e5SJens Axboe 	/*
14788cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1479aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1480aadd06e5SJens Axboe 	 */
14818cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1482aadd06e5SJens Axboe 		return 0;
1483aadd06e5SJens Axboe 
1484aadd06e5SJens Axboe 	ret = 0;
148561e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1486aadd06e5SJens Axboe 
14878cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1488aadd06e5SJens Axboe 		if (signal_pending(current)) {
1489aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1490aadd06e5SJens Axboe 			break;
1491aadd06e5SJens Axboe 		}
1492aadd06e5SJens Axboe 		if (!pipe->writers)
1493aadd06e5SJens Axboe 			break;
1494aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1495aadd06e5SJens Axboe 			ret = -EAGAIN;
1496aadd06e5SJens Axboe 			break;
1497aadd06e5SJens Axboe 		}
1498472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1499aadd06e5SJens Axboe 	}
1500aadd06e5SJens Axboe 
150161e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1502aadd06e5SJens Axboe 	return ret;
1503aadd06e5SJens Axboe }
1504aadd06e5SJens Axboe 
1505aadd06e5SJens Axboe /*
1506aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1507aadd06e5SJens Axboe  * return an appropriate error.
1508aadd06e5SJens Axboe  */
15097c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1510aadd06e5SJens Axboe {
1511aadd06e5SJens Axboe 	int ret;
1512aadd06e5SJens Axboe 
1513aadd06e5SJens Axboe 	/*
15148cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1515aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1516aadd06e5SJens Axboe 	 */
1517566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1518aadd06e5SJens Axboe 		return 0;
1519aadd06e5SJens Axboe 
1520aadd06e5SJens Axboe 	ret = 0;
152161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1522aadd06e5SJens Axboe 
15236718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1524aadd06e5SJens Axboe 		if (!pipe->readers) {
1525aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1526aadd06e5SJens Axboe 			ret = -EPIPE;
1527aadd06e5SJens Axboe 			break;
1528aadd06e5SJens Axboe 		}
1529aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1530aadd06e5SJens Axboe 			ret = -EAGAIN;
1531aadd06e5SJens Axboe 			break;
1532aadd06e5SJens Axboe 		}
1533aadd06e5SJens Axboe 		if (signal_pending(current)) {
1534aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1535aadd06e5SJens Axboe 			break;
1536aadd06e5SJens Axboe 		}
1537472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1538aadd06e5SJens Axboe 	}
1539aadd06e5SJens Axboe 
154061e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1541aadd06e5SJens Axboe 	return ret;
1542aadd06e5SJens Axboe }
1543aadd06e5SJens Axboe 
1544aadd06e5SJens Axboe /*
15457c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15467c77f0b3SMiklos Szeredi  */
15477c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15487c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15497c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15507c77f0b3SMiklos Szeredi {
15517c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15528cefc107SDavid Howells 	unsigned int i_head, o_head;
15538cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15548cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15558cefc107SDavid Howells 	int ret = 0;
15567c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15577c77f0b3SMiklos Szeredi 
15587c77f0b3SMiklos Szeredi 
15597c77f0b3SMiklos Szeredi retry:
15607c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15617c77f0b3SMiklos Szeredi 	if (ret)
15627c77f0b3SMiklos Szeredi 		return ret;
15637c77f0b3SMiklos Szeredi 
15647c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15657c77f0b3SMiklos Szeredi 	if (ret)
15667c77f0b3SMiklos Szeredi 		return ret;
15677c77f0b3SMiklos Szeredi 
15687c77f0b3SMiklos Szeredi 	/*
15697c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15707c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15717c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15727c77f0b3SMiklos Szeredi 	 */
15737c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15747c77f0b3SMiklos Szeredi 
15758cefc107SDavid Howells 	i_tail = ipipe->tail;
15768cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15778cefc107SDavid Howells 	o_head = opipe->head;
15788cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15798cefc107SDavid Howells 
15807c77f0b3SMiklos Szeredi 	do {
15818cefc107SDavid Howells 		size_t o_len;
15828cefc107SDavid Howells 
15837c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15847c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15857c77f0b3SMiklos Szeredi 			if (!ret)
15867c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15877c77f0b3SMiklos Szeredi 			break;
15887c77f0b3SMiklos Szeredi 		}
15897c77f0b3SMiklos Szeredi 
15908cefc107SDavid Howells 		i_head = ipipe->head;
15918cefc107SDavid Howells 		o_tail = opipe->tail;
15928cefc107SDavid Howells 
15938cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
15947c77f0b3SMiklos Szeredi 			break;
15957c77f0b3SMiklos Szeredi 
15967c77f0b3SMiklos Szeredi 		/*
15977c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15987c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15997c77f0b3SMiklos Szeredi 		 */
16008cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
16016718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
16027c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
16037c77f0b3SMiklos Szeredi 			if (ret)
16047c77f0b3SMiklos Szeredi 				break;
16057c77f0b3SMiklos Szeredi 
16067c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
16077c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
16087c77f0b3SMiklos Szeredi 				break;
16097c77f0b3SMiklos Szeredi 			}
16107c77f0b3SMiklos Szeredi 
16117c77f0b3SMiklos Szeredi 			/*
16127c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
16137c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
16147c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
16157c77f0b3SMiklos Szeredi 			 */
16167c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
16177c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
16187c77f0b3SMiklos Szeredi 			goto retry;
16197c77f0b3SMiklos Szeredi 		}
16207c77f0b3SMiklos Szeredi 
16218cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16228cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
16237c77f0b3SMiklos Szeredi 
16247c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
16257c77f0b3SMiklos Szeredi 			/*
16267c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
16277c77f0b3SMiklos Szeredi 			 */
16287c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16297c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
16308cefc107SDavid Howells 			i_tail++;
16318cefc107SDavid Howells 			ipipe->tail = i_tail;
16327c77f0b3SMiklos Szeredi 			input_wakeup = true;
16338cefc107SDavid Howells 			o_len = obuf->len;
16348cefc107SDavid Howells 			o_head++;
16358cefc107SDavid Howells 			opipe->head = o_head;
16367c77f0b3SMiklos Szeredi 		} else {
16377c77f0b3SMiklos Szeredi 			/*
16387c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16397c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16407c77f0b3SMiklos Szeredi 			 */
164115fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
164215fab63eSMatthew Wilcox 				if (ret == 0)
164315fab63eSMatthew Wilcox 					ret = -EFAULT;
164415fab63eSMatthew Wilcox 				break;
164515fab63eSMatthew Wilcox 			}
16467c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16477c77f0b3SMiklos Szeredi 
16487c77f0b3SMiklos Szeredi 			/*
1649f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
16507c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16517c77f0b3SMiklos Szeredi 			 */
16527c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1653f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1654a0ce2f0aSJann Horn 
16557c77f0b3SMiklos Szeredi 			obuf->len = len;
16568cefc107SDavid Howells 			ibuf->offset += len;
16578cefc107SDavid Howells 			ibuf->len -= len;
16588cefc107SDavid Howells 			o_len = len;
16598cefc107SDavid Howells 			o_head++;
16608cefc107SDavid Howells 			opipe->head = o_head;
16617c77f0b3SMiklos Szeredi 		}
16628cefc107SDavid Howells 		ret += o_len;
16638cefc107SDavid Howells 		len -= o_len;
16647c77f0b3SMiklos Szeredi 	} while (len);
16657c77f0b3SMiklos Szeredi 
16667c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16677c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16687c77f0b3SMiklos Szeredi 
16697c77f0b3SMiklos Szeredi 	/*
16707c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16717c77f0b3SMiklos Szeredi 	 */
1672825cdcb1SNamhyung Kim 	if (ret > 0)
1673825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1674825cdcb1SNamhyung Kim 
16757c77f0b3SMiklos Szeredi 	if (input_wakeup)
16767c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16777c77f0b3SMiklos Szeredi 
16787c77f0b3SMiklos Szeredi 	return ret;
16797c77f0b3SMiklos Szeredi }
16807c77f0b3SMiklos Szeredi 
16817c77f0b3SMiklos Szeredi /*
168270524490SJens Axboe  * Link contents of ipipe to opipe.
168370524490SJens Axboe  */
168470524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
168570524490SJens Axboe 		     struct pipe_inode_info *opipe,
168670524490SJens Axboe 		     size_t len, unsigned int flags)
168770524490SJens Axboe {
168870524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
16898cefc107SDavid Howells 	unsigned int i_head, o_head;
16908cefc107SDavid Howells 	unsigned int i_tail, o_tail;
16918cefc107SDavid Howells 	unsigned int i_mask, o_mask;
16928cefc107SDavid Howells 	int ret = 0;
169370524490SJens Axboe 
169470524490SJens Axboe 	/*
169570524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
169661e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
169770524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
169870524490SJens Axboe 	 */
169961e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
170070524490SJens Axboe 
17018cefc107SDavid Howells 	i_tail = ipipe->tail;
17028cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
17038cefc107SDavid Howells 	o_head = opipe->head;
17048cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
17058cefc107SDavid Howells 
1706aadd06e5SJens Axboe 	do {
170770524490SJens Axboe 		if (!opipe->readers) {
170870524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
170970524490SJens Axboe 			if (!ret)
171070524490SJens Axboe 				ret = -EPIPE;
171170524490SJens Axboe 			break;
171270524490SJens Axboe 		}
171370524490SJens Axboe 
17148cefc107SDavid Howells 		i_head = ipipe->head;
17158cefc107SDavid Howells 		o_tail = opipe->tail;
17168cefc107SDavid Howells 
171770524490SJens Axboe 		/*
17188cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1719aadd06e5SJens Axboe 		 * output room, break.
172070524490SJens Axboe 		 */
17218cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17226718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1723aadd06e5SJens Axboe 			break;
1724aadd06e5SJens Axboe 
17258cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17268cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
172770524490SJens Axboe 
172870524490SJens Axboe 		/*
172970524490SJens Axboe 		 * Get a reference to this pipe buffer,
173070524490SJens Axboe 		 * so we can copy the contents over.
173170524490SJens Axboe 		 */
173215fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
173315fab63eSMatthew Wilcox 			if (ret == 0)
173415fab63eSMatthew Wilcox 				ret = -EFAULT;
173515fab63eSMatthew Wilcox 			break;
173615fab63eSMatthew Wilcox 		}
173770524490SJens Axboe 
173870524490SJens Axboe 		*obuf = *ibuf;
173970524490SJens Axboe 
17407afa6fd0SJens Axboe 		/*
1741f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1742f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
17437afa6fd0SJens Axboe 		 */
17447afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1745f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1746a0ce2f0aSJann Horn 
174770524490SJens Axboe 		if (obuf->len > len)
174870524490SJens Axboe 			obuf->len = len;
174970524490SJens Axboe 		ret += obuf->len;
175070524490SJens Axboe 		len -= obuf->len;
17518cefc107SDavid Howells 
17528cefc107SDavid Howells 		o_head++;
17538cefc107SDavid Howells 		opipe->head = o_head;
17548cefc107SDavid Howells 		i_tail++;
1755aadd06e5SJens Axboe 	} while (len);
175670524490SJens Axboe 
175761e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
175861e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
175970524490SJens Axboe 
1760aadd06e5SJens Axboe 	/*
1761aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1762aadd06e5SJens Axboe 	 */
1763825cdcb1SNamhyung Kim 	if (ret > 0)
1764825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
176570524490SJens Axboe 
176670524490SJens Axboe 	return ret;
176770524490SJens Axboe }
176870524490SJens Axboe 
176970524490SJens Axboe /*
177070524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
177170524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
177270524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
177370524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
177470524490SJens Axboe  */
17759dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
177670524490SJens Axboe {
1777c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1778c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1779aadd06e5SJens Axboe 	int ret = -EINVAL;
178070524490SJens Axboe 
178190da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
178290da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
178390da2e3fSPavel Begunkov 		return -EBADF;
178490da2e3fSPavel Begunkov 
178570524490SJens Axboe 	/*
1786aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1787aadd06e5SJens Axboe 	 * copying the data.
178870524490SJens Axboe 	 */
1789aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1790ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1791ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1792ee5e0011SSlavomir Kaslev 
1793aadd06e5SJens Axboe 		/*
1794aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1795aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1796aadd06e5SJens Axboe 		 */
17977c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1798aadd06e5SJens Axboe 		if (!ret) {
17997c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
180002cf01aeSJens Axboe 			if (!ret)
1801aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1802aadd06e5SJens Axboe 		}
1803aadd06e5SJens Axboe 	}
180470524490SJens Axboe 
1805aadd06e5SJens Axboe 	return ret;
180670524490SJens Axboe }
180770524490SJens Axboe 
1808836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
180970524490SJens Axboe {
181090da2e3fSPavel Begunkov 	struct fd in, out;
18112903ff01SAl Viro 	int error;
181270524490SJens Axboe 
18133d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
18143d6ea290SAl Viro 		return -EINVAL;
18153d6ea290SAl Viro 
181670524490SJens Axboe 	if (unlikely(!len))
181770524490SJens Axboe 		return 0;
181870524490SJens Axboe 
181970524490SJens Axboe 	error = -EBADF;
18202903ff01SAl Viro 	in = fdget(fdin);
18212903ff01SAl Viro 	if (in.file) {
182290da2e3fSPavel Begunkov 		out = fdget(fdout);
18232903ff01SAl Viro 		if (out.file) {
182490da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
18252903ff01SAl Viro 			fdput(out);
182670524490SJens Axboe 		}
18272903ff01SAl Viro  		fdput(in);
182870524490SJens Axboe  	}
182970524490SJens Axboe 
183070524490SJens Axboe 	return error;
183170524490SJens Axboe }
1832