xref: /openbmc/linux/fs/splice.c (revision 69df79a4)
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 
30233b3b041SDavid Howells /*
303*69df79a4SDavid Howells  * Copy data from a file into pages and then splice those into the output pipe.
30433b3b041SDavid Howells  */
305*69df79a4SDavid Howells ssize_t copy_splice_read(struct file *in, loff_t *ppos,
30633b3b041SDavid Howells 			 struct pipe_inode_info *pipe,
30733b3b041SDavid Howells 			 size_t len, unsigned int flags)
30833b3b041SDavid Howells {
30933b3b041SDavid Howells 	struct iov_iter to;
31033b3b041SDavid Howells 	struct bio_vec *bv;
31133b3b041SDavid Howells 	struct kiocb kiocb;
31233b3b041SDavid Howells 	struct page **pages;
31333b3b041SDavid Howells 	ssize_t ret;
31433b3b041SDavid Howells 	size_t used, npages, chunk, remain, reclaim;
31533b3b041SDavid Howells 	int i;
31633b3b041SDavid Howells 
31733b3b041SDavid Howells 	/* Work out how much data we can actually add into the pipe */
31833b3b041SDavid Howells 	used = pipe_occupancy(pipe->head, pipe->tail);
31933b3b041SDavid Howells 	npages = max_t(ssize_t, pipe->max_usage - used, 0);
32033b3b041SDavid Howells 	len = min_t(size_t, len, npages * PAGE_SIZE);
32133b3b041SDavid Howells 	npages = DIV_ROUND_UP(len, PAGE_SIZE);
32233b3b041SDavid Howells 
32333b3b041SDavid Howells 	bv = kzalloc(array_size(npages, sizeof(bv[0])) +
32433b3b041SDavid Howells 		     array_size(npages, sizeof(struct page *)), GFP_KERNEL);
32533b3b041SDavid Howells 	if (!bv)
32633b3b041SDavid Howells 		return -ENOMEM;
32733b3b041SDavid Howells 
32833b3b041SDavid Howells 	pages = (void *)(bv + npages);
32933b3b041SDavid Howells 	npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
33033b3b041SDavid Howells 	if (!npages) {
33133b3b041SDavid Howells 		kfree(bv);
33233b3b041SDavid Howells 		return -ENOMEM;
33333b3b041SDavid Howells 	}
33433b3b041SDavid Howells 
33533b3b041SDavid Howells 	remain = len = min_t(size_t, len, npages * PAGE_SIZE);
33633b3b041SDavid Howells 
33733b3b041SDavid Howells 	for (i = 0; i < npages; i++) {
33833b3b041SDavid Howells 		chunk = min_t(size_t, PAGE_SIZE, remain);
33933b3b041SDavid Howells 		bv[i].bv_page = pages[i];
34033b3b041SDavid Howells 		bv[i].bv_offset = 0;
34133b3b041SDavid Howells 		bv[i].bv_len = chunk;
34233b3b041SDavid Howells 		remain -= chunk;
34333b3b041SDavid Howells 	}
34433b3b041SDavid Howells 
34533b3b041SDavid Howells 	/* Do the I/O */
34633b3b041SDavid Howells 	iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
34733b3b041SDavid Howells 	init_sync_kiocb(&kiocb, in);
34833b3b041SDavid Howells 	kiocb.ki_pos = *ppos;
34933b3b041SDavid Howells 	ret = call_read_iter(in, &kiocb, &to);
35033b3b041SDavid Howells 
35133b3b041SDavid Howells 	reclaim = npages * PAGE_SIZE;
35233b3b041SDavid Howells 	remain = 0;
35333b3b041SDavid Howells 	if (ret > 0) {
35433b3b041SDavid Howells 		reclaim -= ret;
35533b3b041SDavid Howells 		remain = ret;
35633b3b041SDavid Howells 		*ppos = kiocb.ki_pos;
35733b3b041SDavid Howells 		file_accessed(in);
35833b3b041SDavid Howells 	} else if (ret < 0) {
35933b3b041SDavid Howells 		/*
36033b3b041SDavid Howells 		 * callers of ->splice_read() expect -EAGAIN on
36133b3b041SDavid Howells 		 * "can't put anything in there", rather than -EFAULT.
36233b3b041SDavid Howells 		 */
36333b3b041SDavid Howells 		if (ret == -EFAULT)
36433b3b041SDavid Howells 			ret = -EAGAIN;
36533b3b041SDavid Howells 	}
36633b3b041SDavid Howells 
36733b3b041SDavid Howells 	/* Free any pages that didn't get touched at all. */
36833b3b041SDavid Howells 	reclaim /= PAGE_SIZE;
36933b3b041SDavid Howells 	if (reclaim) {
37033b3b041SDavid Howells 		npages -= reclaim;
37133b3b041SDavid Howells 		release_pages(pages + npages, reclaim);
37233b3b041SDavid Howells 	}
37333b3b041SDavid Howells 
37433b3b041SDavid Howells 	/* Push the remaining pages into the pipe. */
37533b3b041SDavid Howells 	for (i = 0; i < npages; i++) {
37633b3b041SDavid Howells 		struct pipe_buffer *buf = pipe_head_buf(pipe);
37733b3b041SDavid Howells 
37833b3b041SDavid Howells 		chunk = min_t(size_t, remain, PAGE_SIZE);
37933b3b041SDavid Howells 		*buf = (struct pipe_buffer) {
38033b3b041SDavid Howells 			.ops	= &default_pipe_buf_ops,
38133b3b041SDavid Howells 			.page	= bv[i].bv_page,
38233b3b041SDavid Howells 			.offset	= 0,
38333b3b041SDavid Howells 			.len	= chunk,
38433b3b041SDavid Howells 		};
38533b3b041SDavid Howells 		pipe->head++;
38633b3b041SDavid Howells 		remain -= chunk;
38733b3b041SDavid Howells 	}
38833b3b041SDavid Howells 
38933b3b041SDavid Howells 	kfree(bv);
39033b3b041SDavid Howells 	return ret;
39133b3b041SDavid Howells }
392*69df79a4SDavid Howells EXPORT_SYMBOL(copy_splice_read);
39333b3b041SDavid Howells 
39483f9135bSJens Axboe /**
39583f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
39683f9135bSJens Axboe  * @in:		file to splice from
397932cc6d4SJens Axboe  * @ppos:	position in @in
39883f9135bSJens Axboe  * @pipe:	pipe to splice to
39983f9135bSJens Axboe  * @len:	number of bytes to splice
40083f9135bSJens Axboe  * @flags:	splice modifier flags
40183f9135bSJens Axboe  *
402932cc6d4SJens Axboe  * Description:
403932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
40482c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
405932cc6d4SJens Axboe  *
40683f9135bSJens Axboe  */
407cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
408cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
409cbb7e577SJens Axboe 				 unsigned int flags)
4105274f052SJens Axboe {
41182c156f8SAl Viro 	struct iov_iter to;
41282c156f8SAl Viro 	struct kiocb kiocb;
4138cefc107SDavid Howells 	int ret;
414be64f884SBoaz Harrosh 
415de4eda9dSAl Viro 	iov_iter_pipe(&to, ITER_DEST, pipe, len);
41682c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
41782c156f8SAl Viro 	kiocb.ki_pos = *ppos;
418bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
419723590edSMiklos Szeredi 	if (ret > 0) {
42082c156f8SAl Viro 		*ppos = kiocb.ki_pos;
421723590edSMiklos Szeredi 		file_accessed(in);
42282c156f8SAl Viro 	} else if (ret < 0) {
4230d964934SAl Viro 		/* free what was emitted */
4240d964934SAl Viro 		pipe_discard_from(pipe, to.start_head);
42582c156f8SAl Viro 		/*
42682c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
42782c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
42882c156f8SAl Viro 		 */
42982c156f8SAl Viro 		if (ret == -EFAULT)
43082c156f8SAl Viro 			ret = -EAGAIN;
431723590edSMiklos Szeredi 	}
4325274f052SJens Axboe 
4335274f052SJens Axboe 	return ret;
4345274f052SJens Axboe }
435059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
436059a8f37SJens Axboe 
437241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
4386818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
439c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
4406818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
4416818173bSMiklos Szeredi };
4426818173bSMiklos Szeredi 
44328a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
44428a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
44528a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
44628a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
44728a625cbSMiklos Szeredi };
44828a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
44928a625cbSMiklos Szeredi 
4505274f052SJens Axboe /*
4514f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
452016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4535274f052SJens Axboe  */
45476ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4555274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4565274f052SJens Axboe {
4576a14b90bSJens Axboe 	struct file *file = sd->u.file;
4585274f052SJens Axboe 	loff_t pos = sd->pos;
459a8adbe37SMichał Mirosław 	int more;
4605274f052SJens Axboe 
46172c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
462a8adbe37SMichał Mirosław 		return -EINVAL;
463a8adbe37SMichał Mirosław 
46435f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
465ae62ca7bSEric Dumazet 
4668cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4678cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
46835f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
469ae62ca7bSEric Dumazet 
470a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
471f84d7519SJens Axboe 				    sd->len, &pos, more);
4725274f052SJens Axboe }
4735274f052SJens Axboe 
474b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
475b3c2d2ddSMiklos Szeredi {
476b3c2d2ddSMiklos Szeredi 	smp_mb();
4770ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4780ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
479b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
480b3c2d2ddSMiklos Szeredi }
481b3c2d2ddSMiklos Szeredi 
482b3c2d2ddSMiklos Szeredi /**
483b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
484b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
485b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
486b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
487b3c2d2ddSMiklos Szeredi  *
488b3c2d2ddSMiklos Szeredi  * Description:
489b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
490b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
491b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
492b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
493b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
494b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
495b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
496b3c2d2ddSMiklos Szeredi  *
497b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
498b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
499b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
500b3c2d2ddSMiklos Szeredi  *    destination.
501b3c2d2ddSMiklos Szeredi  */
50296f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
503b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
504b3c2d2ddSMiklos Szeredi {
5058cefc107SDavid Howells 	unsigned int head = pipe->head;
5068cefc107SDavid Howells 	unsigned int tail = pipe->tail;
5078cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
508b3c2d2ddSMiklos Szeredi 	int ret;
509b3c2d2ddSMiklos Szeredi 
510ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
5118cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
512b3c2d2ddSMiklos Szeredi 
513b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
514b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
515b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
516b3c2d2ddSMiklos Szeredi 
517fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
518a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
519b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
520b3c2d2ddSMiklos Szeredi 				ret = 0;
521b3c2d2ddSMiklos Szeredi 			return ret;
522b3c2d2ddSMiklos Szeredi 		}
523a8adbe37SMichał Mirosław 
524a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
525a8adbe37SMichał Mirosław 		if (ret <= 0)
526a8adbe37SMichał Mirosław 			return ret;
527a8adbe37SMichał Mirosław 
528b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
529b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
530b3c2d2ddSMiklos Szeredi 
531b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
532b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
533b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
534b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
535b3c2d2ddSMiklos Szeredi 
536b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
537a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5388cefc107SDavid Howells 			tail++;
5398cefc107SDavid Howells 			pipe->tail = tail;
5406447a3cfSAl Viro 			if (pipe->files)
541b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
542b3c2d2ddSMiklos Szeredi 		}
543b3c2d2ddSMiklos Szeredi 
544b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
545b3c2d2ddSMiklos Szeredi 			return 0;
546b3c2d2ddSMiklos Szeredi 	}
547b3c2d2ddSMiklos Szeredi 
548b3c2d2ddSMiklos Szeredi 	return 1;
549b3c2d2ddSMiklos Szeredi }
550b3c2d2ddSMiklos Szeredi 
551d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
552d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
553d1a819a2SLinus Torvalds {
554d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
555d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
556d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
557d1a819a2SLinus Torvalds 
558d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
559d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
560d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
561d1a819a2SLinus Torvalds 		return true;
562d1a819a2SLinus Torvalds 	}
563d1a819a2SLinus Torvalds 
564d1a819a2SLinus Torvalds 	return false;
565d1a819a2SLinus Torvalds }
566d1a819a2SLinus Torvalds 
567b3c2d2ddSMiklos Szeredi /**
568b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
569b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
570b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
571b3c2d2ddSMiklos Szeredi  *
572b3c2d2ddSMiklos Szeredi  * Description:
573b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
574b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
575b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
576b3c2d2ddSMiklos Szeredi  */
57796f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
578b3c2d2ddSMiklos Szeredi {
579c725bfceSJan Kara 	/*
580c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
581c725bfceSJan Kara 	 * always buffers available
582c725bfceSJan Kara 	 */
583c725bfceSJan Kara 	if (signal_pending(current))
584c725bfceSJan Kara 		return -ERESTARTSYS;
585c725bfceSJan Kara 
586d1a819a2SLinus Torvalds repeat:
5878cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
588b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
589b3c2d2ddSMiklos Szeredi 			return 0;
590b3c2d2ddSMiklos Szeredi 
591a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
592b3c2d2ddSMiklos Szeredi 			return 0;
593b3c2d2ddSMiklos Szeredi 
594b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
595b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
596b3c2d2ddSMiklos Szeredi 
597b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
598b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
599b3c2d2ddSMiklos Szeredi 
600b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
601b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
602b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
603b3c2d2ddSMiklos Szeredi 		}
604b3c2d2ddSMiklos Szeredi 
605472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
606b3c2d2ddSMiklos Szeredi 	}
607b3c2d2ddSMiklos Szeredi 
608d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
609d1a819a2SLinus Torvalds 		goto repeat;
610d1a819a2SLinus Torvalds 
611b3c2d2ddSMiklos Szeredi 	return 1;
612b3c2d2ddSMiklos Szeredi }
613b3c2d2ddSMiklos Szeredi 
614b3c2d2ddSMiklos Szeredi /**
615b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
616b80901bbSRandy Dunlap  * @sd:		information about the splice operation
617b3c2d2ddSMiklos Szeredi  *
618b3c2d2ddSMiklos Szeredi  * Description:
619b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
620b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
621b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
622b3c2d2ddSMiklos Szeredi  */
62396f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
624b3c2d2ddSMiklos Szeredi {
625b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
626b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
627b3c2d2ddSMiklos Szeredi }
628b3c2d2ddSMiklos Szeredi 
629b3c2d2ddSMiklos Szeredi /**
630b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
631b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
632b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
633b3c2d2ddSMiklos Szeredi  *
634b3c2d2ddSMiklos Szeredi  * Description:
635b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
636b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
637b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
638b3c2d2ddSMiklos Szeredi  */
63996f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
640b3c2d2ddSMiklos Szeredi {
641b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
642b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
643b3c2d2ddSMiklos Szeredi }
644b3c2d2ddSMiklos Szeredi 
645932cc6d4SJens Axboe /**
646932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
647932cc6d4SJens Axboe  * @pipe:	pipe to splice from
648932cc6d4SJens Axboe  * @sd:		information to @actor
649932cc6d4SJens Axboe  * @actor:	handler that splices the data
650932cc6d4SJens Axboe  *
651932cc6d4SJens Axboe  * Description:
652932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
653932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
654932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
655932cc6d4SJens Axboe  *    pipe_to_user.
656932cc6d4SJens Axboe  *
65783f9135bSJens Axboe  */
658c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
659c66ab6faSJens Axboe 			   splice_actor *actor)
6605274f052SJens Axboe {
661b3c2d2ddSMiklos Szeredi 	int ret;
6625274f052SJens Axboe 
663b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
664b3c2d2ddSMiklos Szeredi 	do {
665c2489e07SJan Kara 		cond_resched();
666b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
667b3c2d2ddSMiklos Szeredi 		if (ret > 0)
668b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
669b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
670b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6715274f052SJens Axboe 
672b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6735274f052SJens Axboe }
67440bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6755274f052SJens Axboe 
676932cc6d4SJens Axboe /**
677932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
678932cc6d4SJens Axboe  * @pipe:	pipe to splice from
679932cc6d4SJens Axboe  * @out:	file to splice to
680932cc6d4SJens Axboe  * @ppos:	position in @out
681932cc6d4SJens Axboe  * @len:	how many bytes to splice
682932cc6d4SJens Axboe  * @flags:	splice modifier flags
683932cc6d4SJens Axboe  * @actor:	handler that splices the data
684932cc6d4SJens Axboe  *
685932cc6d4SJens Axboe  * Description:
6862933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
687932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
688932cc6d4SJens Axboe  *
689932cc6d4SJens Axboe  */
6906da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6916da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6926da61809SMark Fasheh 			 splice_actor *actor)
6936da61809SMark Fasheh {
6946da61809SMark Fasheh 	ssize_t ret;
695c66ab6faSJens Axboe 	struct splice_desc sd = {
696c66ab6faSJens Axboe 		.total_len = len,
697c66ab6faSJens Axboe 		.flags = flags,
698c66ab6faSJens Axboe 		.pos = *ppos,
6996a14b90bSJens Axboe 		.u.file = out,
700c66ab6faSJens Axboe 	};
7016da61809SMark Fasheh 
70261e0d47cSMiklos Szeredi 	pipe_lock(pipe);
703c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
70461e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
7056da61809SMark Fasheh 
7066da61809SMark Fasheh 	return ret;
7076da61809SMark Fasheh }
7086da61809SMark Fasheh 
7096da61809SMark Fasheh /**
7108d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
7118d020765SAl Viro  * @pipe:	pipe info
7128d020765SAl Viro  * @out:	file to write to
7138d020765SAl Viro  * @ppos:	position in @out
7148d020765SAl Viro  * @len:	number of bytes to splice
7158d020765SAl Viro  * @flags:	splice modifier flags
7168d020765SAl Viro  *
7178d020765SAl Viro  * Description:
7188d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
7198d020765SAl Viro  *    the given pipe inode to the given file.
7208d020765SAl Viro  *    This one is ->write_iter-based.
7218d020765SAl Viro  *
7228d020765SAl Viro  */
7238d020765SAl Viro ssize_t
7248d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
7258d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
7268d020765SAl Viro {
7278d020765SAl Viro 	struct splice_desc sd = {
7288d020765SAl Viro 		.total_len = len,
7298d020765SAl Viro 		.flags = flags,
7308d020765SAl Viro 		.pos = *ppos,
7318d020765SAl Viro 		.u.file = out,
7328d020765SAl Viro 	};
7336718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
7348d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7358d020765SAl Viro 					GFP_KERNEL);
7368d020765SAl Viro 	ssize_t ret;
7378d020765SAl Viro 
7388d020765SAl Viro 	if (unlikely(!array))
7398d020765SAl Viro 		return -ENOMEM;
7408d020765SAl Viro 
7418d020765SAl Viro 	pipe_lock(pipe);
7428d020765SAl Viro 
7438d020765SAl Viro 	splice_from_pipe_begin(&sd);
7448d020765SAl Viro 	while (sd.total_len) {
7458d020765SAl Viro 		struct iov_iter from;
746ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7478d020765SAl Viro 		size_t left;
7488cefc107SDavid Howells 		int n;
7498d020765SAl Viro 
7508d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7518d020765SAl Viro 		if (ret <= 0)
7528d020765SAl Viro 			break;
7538d020765SAl Viro 
7546718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7558d020765SAl Viro 			kfree(array);
7566718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7578d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7588d020765SAl Viro 					GFP_KERNEL);
7598d020765SAl Viro 			if (!array) {
7608d020765SAl Viro 				ret = -ENOMEM;
7618d020765SAl Viro 				break;
7628d020765SAl Viro 			}
7638d020765SAl Viro 		}
7648d020765SAl Viro 
765ec057595SLinus Torvalds 		head = pipe->head;
766ec057595SLinus Torvalds 		tail = pipe->tail;
767ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
768ec057595SLinus Torvalds 
7698d020765SAl Viro 		/* build the vector */
7708d020765SAl Viro 		left = sd.total_len;
7710f1d344fSPavel Begunkov 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
7728cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7738d020765SAl Viro 			size_t this_len = buf->len;
7748d020765SAl Viro 
7750f1d344fSPavel Begunkov 			/* zero-length bvecs are not supported, skip them */
7760f1d344fSPavel Begunkov 			if (!this_len)
7770f1d344fSPavel Begunkov 				continue;
7780f1d344fSPavel Begunkov 			this_len = min(this_len, left);
7798d020765SAl Viro 
780fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7818d020765SAl Viro 			if (unlikely(ret)) {
7828d020765SAl Viro 				if (ret == -ENODATA)
7838d020765SAl Viro 					ret = 0;
7848d020765SAl Viro 				goto done;
7858d020765SAl Viro 			}
7868d020765SAl Viro 
787664e4078SChristoph Hellwig 			bvec_set_page(&array[n], buf->page, this_len,
788664e4078SChristoph Hellwig 				      buf->offset);
7898d020765SAl Viro 			left -= this_len;
7900f1d344fSPavel Begunkov 			n++;
7918d020765SAl Viro 		}
7928d020765SAl Viro 
793de4eda9dSAl Viro 		iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
794abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7958d020765SAl Viro 		if (ret <= 0)
7968d020765SAl Viro 			break;
7978d020765SAl Viro 
7988d020765SAl Viro 		sd.num_spliced += ret;
7998d020765SAl Viro 		sd.total_len -= ret;
800dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
8018d020765SAl Viro 
8028d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
8038cefc107SDavid Howells 		tail = pipe->tail;
8048d020765SAl Viro 		while (ret) {
8058cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
8068d020765SAl Viro 			if (ret >= buf->len) {
8078d020765SAl Viro 				ret -= buf->len;
8088d020765SAl Viro 				buf->len = 0;
809a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
8108cefc107SDavid Howells 				tail++;
8118cefc107SDavid Howells 				pipe->tail = tail;
8128d020765SAl Viro 				if (pipe->files)
8138d020765SAl Viro 					sd.need_wakeup = true;
8148d020765SAl Viro 			} else {
8158d020765SAl Viro 				buf->offset += ret;
8168d020765SAl Viro 				buf->len -= ret;
8178d020765SAl Viro 				ret = 0;
8188d020765SAl Viro 			}
8198d020765SAl Viro 		}
8208d020765SAl Viro 	}
8218d020765SAl Viro done:
8228d020765SAl Viro 	kfree(array);
8238d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
8248d020765SAl Viro 
8258d020765SAl Viro 	pipe_unlock(pipe);
8268d020765SAl Viro 
8278d020765SAl Viro 	if (sd.num_spliced)
8288d020765SAl Viro 		ret = sd.num_spliced;
8298d020765SAl Viro 
8308d020765SAl Viro 	return ret;
8318d020765SAl Viro }
8328d020765SAl Viro 
8338d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8348d020765SAl Viro 
83583f9135bSJens Axboe /**
83683f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
837932cc6d4SJens Axboe  * @pipe:	pipe to splice from
83883f9135bSJens Axboe  * @out:	socket to write to
839932cc6d4SJens Axboe  * @ppos:	position in @out
84083f9135bSJens Axboe  * @len:	number of bytes to splice
84183f9135bSJens Axboe  * @flags:	splice modifier flags
84283f9135bSJens Axboe  *
843932cc6d4SJens Axboe  * Description:
84483f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
84583f9135bSJens Axboe  *    is involved.
84683f9135bSJens Axboe  *
84783f9135bSJens Axboe  */
8483a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
849cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8505274f052SJens Axboe {
85100522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8525274f052SJens Axboe }
8535274f052SJens Axboe 
854059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
855a0f06780SJeff Garzik 
85636e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op)
85736e2c742SChristoph Hellwig {
85836e2c742SChristoph Hellwig 	pr_debug_ratelimited(
85936e2c742SChristoph Hellwig 		"splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
86036e2c742SChristoph Hellwig 		op, file, current->pid, current->comm);
86136e2c742SChristoph Hellwig 	return -EINVAL;
86236e2c742SChristoph Hellwig }
86336e2c742SChristoph Hellwig 
86483f9135bSJens Axboe /*
86583f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
86683f9135bSJens Axboe  */
8673a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
868cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8695274f052SJens Axboe {
87036e2c742SChristoph Hellwig 	if (unlikely(!out->f_op->splice_write))
87136e2c742SChristoph Hellwig 		return warn_unsupported(out, "write");
87200c285d0SChristoph Hellwig 	return out->f_op->splice_write(pipe, out, ppos, len, flags);
8735274f052SJens Axboe }
8745274f052SJens Axboe 
87583f9135bSJens Axboe /*
87683f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
87783f9135bSJens Axboe  */
878cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
879cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
880cbb7e577SJens Axboe 			 unsigned int flags)
8815274f052SJens Axboe {
882313d64a3SAl Viro 	unsigned int p_space;
8835274f052SJens Axboe 	int ret;
8845274f052SJens Axboe 
88549570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8865274f052SJens Axboe 		return -EBADF;
8875274f052SJens Axboe 
888313d64a3SAl Viro 	/* Don't try to read more the pipe has space for. */
889313d64a3SAl Viro 	p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
890313d64a3SAl Viro 	len = min_t(size_t, len, p_space << PAGE_SHIFT);
891313d64a3SAl Viro 
892cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8935274f052SJens Axboe 	if (unlikely(ret < 0))
8945274f052SJens Axboe 		return ret;
8955274f052SJens Axboe 
89603cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
89703cc0789SAl Viro 		len = MAX_RW_COUNT;
89803cc0789SAl Viro 
89936e2c742SChristoph Hellwig 	if (unlikely(!in->f_op->splice_read))
90036e2c742SChristoph Hellwig 		return warn_unsupported(in, "read");
9012bc01060SChristoph Hellwig 	return in->f_op->splice_read(in, ppos, pipe, len, flags);
9025274f052SJens Axboe }
9035274f052SJens Axboe 
904932cc6d4SJens Axboe /**
905932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
906932cc6d4SJens Axboe  * @in:		file to splice from
907932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
908932cc6d4SJens Axboe  * @actor:	handles the data splicing
909932cc6d4SJens Axboe  *
910932cc6d4SJens Axboe  * Description:
911932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
912932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
913932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
914932cc6d4SJens Axboe  *    that process.
915932cc6d4SJens Axboe  *
916c66ab6faSJens Axboe  */
917c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
918c66ab6faSJens Axboe 			       splice_direct_actor *actor)
919b92ce558SJens Axboe {
920b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
921b92ce558SJens Axboe 	long ret, bytes;
922c66ab6faSJens Axboe 	size_t len;
9230ff28d9fSChristophe Leroy 	int i, flags, more;
924b92ce558SJens Axboe 
925b92ce558SJens Axboe 	/*
92697ef77c5SJason A. Donenfeld 	 * We require the input to be seekable, as we don't want to randomly
92797ef77c5SJason A. Donenfeld 	 * drop data for eg socket -> socket splicing. Use the piped splicing
92897ef77c5SJason A. Donenfeld 	 * for that!
929b92ce558SJens Axboe 	 */
93097ef77c5SJason A. Donenfeld 	if (unlikely(!(in->f_mode & FMODE_LSEEK)))
931b92ce558SJens Axboe 		return -EINVAL;
932b92ce558SJens Axboe 
933b92ce558SJens Axboe 	/*
934b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
935b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
936b92ce558SJens Axboe 	 */
937b92ce558SJens Axboe 	pipe = current->splice_pipe;
93849570e9bSJens Axboe 	if (unlikely(!pipe)) {
9397bee130eSAl Viro 		pipe = alloc_pipe_info();
940b92ce558SJens Axboe 		if (!pipe)
941b92ce558SJens Axboe 			return -ENOMEM;
942b92ce558SJens Axboe 
943b92ce558SJens Axboe 		/*
944b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
94500522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
946b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
947b92ce558SJens Axboe 		 */
948b92ce558SJens Axboe 		pipe->readers = 1;
949b92ce558SJens Axboe 
950b92ce558SJens Axboe 		current->splice_pipe = pipe;
951b92ce558SJens Axboe 	}
952b92ce558SJens Axboe 
953b92ce558SJens Axboe 	/*
95473d62d83SIngo Molnar 	 * Do the splice.
955b92ce558SJens Axboe 	 */
956b92ce558SJens Axboe 	bytes = 0;
957c66ab6faSJens Axboe 	len = sd->total_len;
958c66ab6faSJens Axboe 	flags = sd->flags;
959c66ab6faSJens Axboe 
960c66ab6faSJens Axboe 	/*
961c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
962c66ab6faSJens Axboe 	 */
963c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9640ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
965b92ce558SJens Axboe 
9668cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
96717614445SDarrick J. Wong 
968b92ce558SJens Axboe 	while (len) {
96951a92c0fSJens Axboe 		size_t read_len;
970a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
971b92ce558SJens Axboe 
972313d64a3SAl Viro 		ret = do_splice_to(in, &pos, pipe, len, flags);
97351a92c0fSJens Axboe 		if (unlikely(ret <= 0))
974b92ce558SJens Axboe 			goto out_release;
975b92ce558SJens Axboe 
976b92ce558SJens Axboe 		read_len = ret;
977c66ab6faSJens Axboe 		sd->total_len = read_len;
978b92ce558SJens Axboe 
979b92ce558SJens Axboe 		/*
9800ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9810ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9820ff28d9fSChristophe Leroy 		 * initially, clears it.
9830ff28d9fSChristophe Leroy 		 */
9840ff28d9fSChristophe Leroy 		if (read_len < len)
9850ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9860ff28d9fSChristophe Leroy 		else if (!more)
9870ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9880ff28d9fSChristophe Leroy 		/*
989b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
990b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
991b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
992b92ce558SJens Axboe 		 */
993c66ab6faSJens Axboe 		ret = actor(pipe, sd);
994a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
995a82c53a0STom Zanussi 			sd->pos = prev_pos;
996b92ce558SJens Axboe 			goto out_release;
997a82c53a0STom Zanussi 		}
998b92ce558SJens Axboe 
999b92ce558SJens Axboe 		bytes += ret;
1000b92ce558SJens Axboe 		len -= ret;
1001bcd4f3acSJens Axboe 		sd->pos = pos;
1002b92ce558SJens Axboe 
1003a82c53a0STom Zanussi 		if (ret < read_len) {
1004a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
100551a92c0fSJens Axboe 			goto out_release;
1006b92ce558SJens Axboe 		}
1007a82c53a0STom Zanussi 	}
1008b92ce558SJens Axboe 
10099e97198dSJens Axboe done:
10108cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
10119e97198dSJens Axboe 	file_accessed(in);
1012b92ce558SJens Axboe 	return bytes;
1013b92ce558SJens Axboe 
1014b92ce558SJens Axboe out_release:
1015b92ce558SJens Axboe 	/*
1016b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1017b92ce558SJens Axboe 	 * the pipe buffers in question:
1018b92ce558SJens Axboe 	 */
10198cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
10208cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1021b92ce558SJens Axboe 
1022a779638cSMiklos Szeredi 		if (buf->ops)
1023a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1024b92ce558SJens Axboe 	}
1025b92ce558SJens Axboe 
10269e97198dSJens Axboe 	if (!bytes)
10279e97198dSJens Axboe 		bytes = ret;
1028b92ce558SJens Axboe 
10299e97198dSJens Axboe 	goto done;
1030c66ab6faSJens Axboe }
1031c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1032c66ab6faSJens Axboe 
1033c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1034c66ab6faSJens Axboe 			       struct splice_desc *sd)
1035c66ab6faSJens Axboe {
10366a14b90bSJens Axboe 	struct file *file = sd->u.file;
1037c66ab6faSJens Axboe 
10387995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10392cb4b05eSChangli Gao 			      sd->flags);
1040c66ab6faSJens Axboe }
1041c66ab6faSJens Axboe 
1042932cc6d4SJens Axboe /**
1043932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1044932cc6d4SJens Axboe  * @in:		file to splice from
1045932cc6d4SJens Axboe  * @ppos:	input file offset
1046932cc6d4SJens Axboe  * @out:	file to splice to
1047acdb37c3SRandy Dunlap  * @opos:	output file offset
1048932cc6d4SJens Axboe  * @len:	number of bytes to splice
1049932cc6d4SJens Axboe  * @flags:	splice modifier flags
1050932cc6d4SJens Axboe  *
1051932cc6d4SJens Axboe  * Description:
1052932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1053932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1054932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1055932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1056932cc6d4SJens Axboe  *
1057932cc6d4SJens Axboe  */
1058c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10597995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1060c66ab6faSJens Axboe {
1061c66ab6faSJens Axboe 	struct splice_desc sd = {
1062c66ab6faSJens Axboe 		.len		= len,
1063c66ab6faSJens Axboe 		.total_len	= len,
1064c66ab6faSJens Axboe 		.flags		= flags,
1065c66ab6faSJens Axboe 		.pos		= *ppos,
10666a14b90bSJens Axboe 		.u.file		= out,
10677995bd28SAl Viro 		.opos		= opos,
1068c66ab6faSJens Axboe 	};
106951a92c0fSJens Axboe 	long ret;
1070c66ab6faSJens Axboe 
107118c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
107218c67cb9SAl Viro 		return -EBADF;
107318c67cb9SAl Viro 
107418c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
107518c67cb9SAl Viro 		return -EINVAL;
107618c67cb9SAl Viro 
107718c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
107818c67cb9SAl Viro 	if (unlikely(ret < 0))
107918c67cb9SAl Viro 		return ret;
108018c67cb9SAl Viro 
1081c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
108251a92c0fSJens Axboe 	if (ret > 0)
1083a82c53a0STom Zanussi 		*ppos = sd.pos;
108451a92c0fSJens Axboe 
1085c66ab6faSJens Axboe 	return ret;
1086b92ce558SJens Axboe }
10871c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1088b92ce558SJens Axboe 
10898924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10908924feffSAl Viro {
109152bce911SLinus Torvalds 	for (;;) {
109252bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
109352bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
109452bce911SLinus Torvalds 			return -EPIPE;
109552bce911SLinus Torvalds 		}
10966718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
109752bce911SLinus Torvalds 			return 0;
10988924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10998924feffSAl Viro 			return -EAGAIN;
11008924feffSAl Viro 		if (signal_pending(current))
11018924feffSAl Viro 			return -ERESTARTSYS;
1102472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
11038924feffSAl Viro 	}
11048924feffSAl Viro }
11058924feffSAl Viro 
11067c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
11077c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
11087c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1109ddac0d39SJens Axboe 
1110b964bf53SAl Viro long splice_file_to_pipe(struct file *in,
1111faa97c48SAl Viro 			 struct pipe_inode_info *opipe,
1112faa97c48SAl Viro 			 loff_t *offset,
1113faa97c48SAl Viro 			 size_t len, unsigned int flags)
1114faa97c48SAl Viro {
1115faa97c48SAl Viro 	long ret;
1116faa97c48SAl Viro 
1117faa97c48SAl Viro 	pipe_lock(opipe);
1118faa97c48SAl Viro 	ret = wait_for_space(opipe, flags);
1119faa97c48SAl Viro 	if (!ret)
1120faa97c48SAl Viro 		ret = do_splice_to(in, offset, opipe, len, flags);
1121faa97c48SAl Viro 	pipe_unlock(opipe);
1122faa97c48SAl Viro 	if (ret > 0)
1123faa97c48SAl Viro 		wakeup_pipe_readers(opipe);
1124faa97c48SAl Viro 	return ret;
1125faa97c48SAl Viro }
1126faa97c48SAl Viro 
1127ddac0d39SJens Axboe /*
112883f9135bSJens Axboe  * Determine where to splice to/from.
112983f9135bSJens Axboe  */
1130ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1131ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
11325274f052SJens Axboe {
11337c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11347c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11357995bd28SAl Viro 	loff_t offset;
1136a4514ebdSJens Axboe 	long ret;
11375274f052SJens Axboe 
113890da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
113990da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
114090da2e3fSPavel Begunkov 		return -EBADF;
114190da2e3fSPavel Begunkov 
1142c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1143c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
11447c77f0b3SMiklos Szeredi 
11457c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11467c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11477c77f0b3SMiklos Szeredi 			return -ESPIPE;
11487c77f0b3SMiklos Szeredi 
11497c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11507c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11517c77f0b3SMiklos Szeredi 			return -EINVAL;
11527c77f0b3SMiklos Szeredi 
1153ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1154ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1155ee5e0011SSlavomir Kaslev 
11567c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11577c77f0b3SMiklos Szeredi 	}
11587c77f0b3SMiklos Szeredi 
11597c77f0b3SMiklos Szeredi 	if (ipipe) {
1160529565dcSIngo Molnar 		if (off_in)
1161529565dcSIngo Molnar 			return -ESPIPE;
1162b92ce558SJens Axboe 		if (off_out) {
116319c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1164b92ce558SJens Axboe 				return -EINVAL;
1165ee6e00c8SJens Axboe 			offset = *off_out;
11667995bd28SAl Viro 		} else {
11677995bd28SAl Viro 			offset = out->f_pos;
11687995bd28SAl Viro 		}
1169529565dcSIngo Molnar 
117018c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
117118c67cb9SAl Viro 			return -EINVAL;
117218c67cb9SAl Viro 
117318c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
117418c67cb9SAl Viro 		if (unlikely(ret < 0))
117518c67cb9SAl Viro 			return ret;
117618c67cb9SAl Viro 
1177ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1178ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1179ee5e0011SSlavomir Kaslev 
1180500368f7SAl Viro 		file_start_write(out);
11817995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1182500368f7SAl Viro 		file_end_write(out);
1183a4514ebdSJens Axboe 
1184983652c6SChung-Chiang Cheng 		if (ret > 0)
1185983652c6SChung-Chiang Cheng 			fsnotify_modify(out);
1186983652c6SChung-Chiang Cheng 
11877995bd28SAl Viro 		if (!off_out)
11887995bd28SAl Viro 			out->f_pos = offset;
1189ee6e00c8SJens Axboe 		else
1190ee6e00c8SJens Axboe 			*off_out = offset;
1191a4514ebdSJens Axboe 
1192a4514ebdSJens Axboe 		return ret;
1193529565dcSIngo Molnar 	}
11945274f052SJens Axboe 
11957c77f0b3SMiklos Szeredi 	if (opipe) {
1196529565dcSIngo Molnar 		if (off_out)
1197529565dcSIngo Molnar 			return -ESPIPE;
1198b92ce558SJens Axboe 		if (off_in) {
119919c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1200b92ce558SJens Axboe 				return -EINVAL;
1201ee6e00c8SJens Axboe 			offset = *off_in;
12027995bd28SAl Viro 		} else {
12037995bd28SAl Viro 			offset = in->f_pos;
12047995bd28SAl Viro 		}
1205529565dcSIngo Molnar 
1206ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1207ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1208ee5e0011SSlavomir Kaslev 
1209faa97c48SAl Viro 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1210983652c6SChung-Chiang Cheng 
1211983652c6SChung-Chiang Cheng 		if (ret > 0)
1212983652c6SChung-Chiang Cheng 			fsnotify_access(in);
1213983652c6SChung-Chiang Cheng 
12147995bd28SAl Viro 		if (!off_in)
12157995bd28SAl Viro 			in->f_pos = offset;
1216ee6e00c8SJens Axboe 		else
1217ee6e00c8SJens Axboe 			*off_in = offset;
1218a4514ebdSJens Axboe 
1219a4514ebdSJens Axboe 		return ret;
1220529565dcSIngo Molnar 	}
12215274f052SJens Axboe 
12225274f052SJens Axboe 	return -EINVAL;
12235274f052SJens Axboe }
12245274f052SJens Axboe 
1225ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1226ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1227ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1228ee6e00c8SJens Axboe {
1229ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1230ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1231ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1232ee6e00c8SJens Axboe 	long ret;
1233ee6e00c8SJens Axboe 
1234ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1235ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1236ee6e00c8SJens Axboe 
12370f99fc51SJens Axboe 	if (ipipe) {
12380f99fc51SJens Axboe 		if (off_in)
1239ee6e00c8SJens Axboe 			return -ESPIPE;
12400f99fc51SJens Axboe 		pipe_clear_nowait(in);
12410f99fc51SJens Axboe 	}
12420f99fc51SJens Axboe 	if (opipe) {
12430f99fc51SJens Axboe 		if (off_out)
1244ee6e00c8SJens Axboe 			return -ESPIPE;
12450f99fc51SJens Axboe 		pipe_clear_nowait(out);
12460f99fc51SJens Axboe 	}
1247ee6e00c8SJens Axboe 
1248ee6e00c8SJens Axboe 	if (off_out) {
1249ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1250ee6e00c8SJens Axboe 			return -EFAULT;
1251ee6e00c8SJens Axboe 		__off_out = &offset;
1252ee6e00c8SJens Axboe 	}
1253ee6e00c8SJens Axboe 	if (off_in) {
1254ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1255ee6e00c8SJens Axboe 			return -EFAULT;
1256ee6e00c8SJens Axboe 		__off_in = &offset;
1257ee6e00c8SJens Axboe 	}
1258ee6e00c8SJens Axboe 
1259ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1260ee6e00c8SJens Axboe 	if (ret < 0)
1261ee6e00c8SJens Axboe 		return ret;
1262ee6e00c8SJens Axboe 
1263ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1264ee6e00c8SJens Axboe 		return -EFAULT;
1265ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1266ee6e00c8SJens Axboe 		return -EFAULT;
1267ee6e00c8SJens Axboe 
1268ee6e00c8SJens Axboe 	return ret;
1269ee6e00c8SJens Axboe }
1270ee6e00c8SJens Axboe 
127179fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
127279fddc4eSAl Viro 			struct pipe_inode_info *pipe,
127379fddc4eSAl Viro 			unsigned flags)
1274912d35f8SJens Axboe {
127579fddc4eSAl Viro 	struct pipe_buffer buf = {
127679fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
127779fddc4eSAl Viro 		.flags = flags
127879fddc4eSAl Viro 	};
127979fddc4eSAl Viro 	size_t total = 0;
128079fddc4eSAl Viro 	int ret = 0;
128179fddc4eSAl Viro 
12827d690c15SAl Viro 	while (iov_iter_count(from)) {
128379fddc4eSAl Viro 		struct page *pages[16];
12847d690c15SAl Viro 		ssize_t left;
1285db85a9ebSAl Viro 		size_t start;
12867d690c15SAl Viro 		int i, n;
1287912d35f8SJens Axboe 
12887d690c15SAl Viro 		left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
12897d690c15SAl Viro 		if (left <= 0) {
12907d690c15SAl Viro 			ret = left;
129179fddc4eSAl Viro 			break;
129279fddc4eSAl Viro 		}
1293912d35f8SJens Axboe 
12947d690c15SAl Viro 		n = DIV_ROUND_UP(left + start, PAGE_SIZE);
12957d690c15SAl Viro 		for (i = 0; i < n; i++) {
12967d690c15SAl Viro 			int size = min_t(int, left, PAGE_SIZE - start);
12977d690c15SAl Viro 
12987d690c15SAl Viro 			buf.page = pages[i];
129979fddc4eSAl Viro 			buf.offset = start;
130079fddc4eSAl Viro 			buf.len = size;
130179fddc4eSAl Viro 			ret = add_to_pipe(pipe, &buf);
130279fddc4eSAl Viro 			if (unlikely(ret < 0)) {
13037d690c15SAl Viro 				iov_iter_revert(from, left);
13047d690c15SAl Viro 				// this one got dropped by add_to_pipe()
13057d690c15SAl Viro 				while (++i < n)
13067d690c15SAl Viro 					put_page(pages[i]);
13077d690c15SAl Viro 				goto out;
13087d690c15SAl Viro 			}
130979fddc4eSAl Viro 			total += ret;
13107d690c15SAl Viro 			left -= size;
13117d690c15SAl Viro 			start = 0;
1312912d35f8SJens Axboe 		}
1313912d35f8SJens Axboe 	}
13147d690c15SAl Viro out:
131579fddc4eSAl Viro 	return total ? total : ret;
1316912d35f8SJens Axboe }
1317912d35f8SJens Axboe 
13186a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
13196a14b90bSJens Axboe 			struct splice_desc *sd)
13206a14b90bSJens Axboe {
13216130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
13226130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
13236a14b90bSJens Axboe }
13246a14b90bSJens Axboe 
13256a14b90bSJens Axboe /*
13266a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
13276a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
13286a14b90bSJens Axboe  */
132987a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
133087a3002aSAl Viro 			     unsigned int flags)
13316a14b90bSJens Axboe {
1332c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
133387a3002aSAl Viro 	struct splice_desc sd = {
133487a3002aSAl Viro 		.total_len = iov_iter_count(iter),
133587a3002aSAl Viro 		.flags = flags,
133687a3002aSAl Viro 		.u.data = iter
133787a3002aSAl Viro 	};
133887a3002aSAl Viro 	long ret = 0;
13396a14b90bSJens Axboe 
13406a14b90bSJens Axboe 	if (!pipe)
13416a14b90bSJens Axboe 		return -EBADF;
13426a14b90bSJens Axboe 
13430f99fc51SJens Axboe 	pipe_clear_nowait(file);
13440f99fc51SJens Axboe 
1345345995faSAl Viro 	if (sd.total_len) {
13466130f531SAl Viro 		pipe_lock(pipe);
13476130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
134861e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1349345995faSAl Viro 	}
13506a14b90bSJens Axboe 
13516a14b90bSJens Axboe 	return ret;
13526a14b90bSJens Axboe }
13536a14b90bSJens Axboe 
1354912d35f8SJens Axboe /*
1355912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1356912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1357912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1358912d35f8SJens Axboe  */
135987a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
136087a3002aSAl Viro 			     unsigned int flags)
1361912d35f8SJens Axboe {
1362ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
136387a3002aSAl Viro 	long ret = 0;
136479fddc4eSAl Viro 	unsigned buf_flag = 0;
136579fddc4eSAl Viro 
136679fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
136779fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1368912d35f8SJens Axboe 
1369c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1370ddac0d39SJens Axboe 	if (!pipe)
1371912d35f8SJens Axboe 		return -EBADF;
1372912d35f8SJens Axboe 
13730f99fc51SJens Axboe 	pipe_clear_nowait(file);
13740f99fc51SJens Axboe 
13758924feffSAl Viro 	pipe_lock(pipe);
13768924feffSAl Viro 	ret = wait_for_space(pipe, flags);
137779fddc4eSAl Viro 	if (!ret)
137887a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13798924feffSAl Viro 	pipe_unlock(pipe);
13808924feffSAl Viro 	if (ret > 0)
13818924feffSAl Viro 		wakeup_pipe_readers(pipe);
138235f3d14dSJens Axboe 	return ret;
1383912d35f8SJens Axboe }
1384912d35f8SJens Axboe 
138587a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
138687a3002aSAl Viro {
138787a3002aSAl Viro 	if (!f.file)
138887a3002aSAl Viro 		return -EBADF;
138987a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
1390de4eda9dSAl Viro 		*type = ITER_SOURCE;
139187a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
1392de4eda9dSAl Viro 		*type = ITER_DEST;
139387a3002aSAl Viro 	} else {
139487a3002aSAl Viro 		fdput(f);
139587a3002aSAl Viro 		return -EBADF;
139687a3002aSAl Viro 	}
139787a3002aSAl Viro 	return 0;
139887a3002aSAl Viro }
139987a3002aSAl Viro 
14006a14b90bSJens Axboe /*
14016a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
14026a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
14036a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
14046a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
14056a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
14066a14b90bSJens Axboe  * solutions for that:
14076a14b90bSJens Axboe  *
14086a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
14096a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
14106a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
14116a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
14126a14b90bSJens Axboe  *
14136a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
14146a14b90bSJens Axboe  *
14156a14b90bSJens Axboe  */
141687a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
141730cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
141830cfe4efSDominik Brodowski {
141987a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
142087a3002aSAl Viro 	struct iovec *iov = iovstack;
142187a3002aSAl Viro 	struct iov_iter iter;
142287e5e6daSJens Axboe 	ssize_t error;
142387a3002aSAl Viro 	struct fd f;
142487a3002aSAl Viro 	int type;
142587a3002aSAl Viro 
1426598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1427598b3cecSChristoph Hellwig 		return -EINVAL;
1428598b3cecSChristoph Hellwig 
142987a3002aSAl Viro 	f = fdget(fd);
143087a3002aSAl Viro 	error = vmsplice_type(f, &type);
143187a3002aSAl Viro 	if (error)
143287a3002aSAl Viro 		return error;
143387a3002aSAl Viro 
143487a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
143587a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1436598b3cecSChristoph Hellwig 	if (error < 0)
1437598b3cecSChristoph Hellwig 		goto out_fdput;
1438598b3cecSChristoph Hellwig 
1439598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1440598b3cecSChristoph Hellwig 		error = 0;
1441de4eda9dSAl Viro 	else if (type == ITER_SOURCE)
1442598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1443598b3cecSChristoph Hellwig 	else
1444598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1445598b3cecSChristoph Hellwig 
144687a3002aSAl Viro 	kfree(iov);
1447598b3cecSChristoph Hellwig out_fdput:
144887a3002aSAl Viro 	fdput(f);
144987a3002aSAl Viro 	return error;
145030cfe4efSDominik Brodowski }
145130cfe4efSDominik Brodowski 
1452836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1453836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1454836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14555274f052SJens Axboe {
14562903ff01SAl Viro 	struct fd in, out;
14575274f052SJens Axboe 	long error;
14585274f052SJens Axboe 
14595274f052SJens Axboe 	if (unlikely(!len))
14605274f052SJens Axboe 		return 0;
14615274f052SJens Axboe 
14623d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14633d6ea290SAl Viro 		return -EINVAL;
14643d6ea290SAl Viro 
14655274f052SJens Axboe 	error = -EBADF;
14662903ff01SAl Viro 	in = fdget(fd_in);
14672903ff01SAl Viro 	if (in.file) {
14682903ff01SAl Viro 		out = fdget(fd_out);
14692903ff01SAl Viro 		if (out.file) {
1470ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1471529565dcSIngo Molnar 						len, flags);
14722903ff01SAl Viro 			fdput(out);
14735274f052SJens Axboe 		}
14742903ff01SAl Viro 		fdput(in);
14755274f052SJens Axboe 	}
14765274f052SJens Axboe 	return error;
14775274f052SJens Axboe }
147870524490SJens Axboe 
147970524490SJens Axboe /*
1480aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1481aadd06e5SJens Axboe  * return an appropriate error.
1482aadd06e5SJens Axboe  */
14837c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1484aadd06e5SJens Axboe {
1485aadd06e5SJens Axboe 	int ret;
1486aadd06e5SJens Axboe 
1487aadd06e5SJens Axboe 	/*
14888cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1489aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1490aadd06e5SJens Axboe 	 */
14918cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1492aadd06e5SJens Axboe 		return 0;
1493aadd06e5SJens Axboe 
1494aadd06e5SJens Axboe 	ret = 0;
149561e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1496aadd06e5SJens Axboe 
14978cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1498aadd06e5SJens Axboe 		if (signal_pending(current)) {
1499aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1500aadd06e5SJens Axboe 			break;
1501aadd06e5SJens Axboe 		}
1502aadd06e5SJens Axboe 		if (!pipe->writers)
1503aadd06e5SJens Axboe 			break;
1504aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1505aadd06e5SJens Axboe 			ret = -EAGAIN;
1506aadd06e5SJens Axboe 			break;
1507aadd06e5SJens Axboe 		}
1508472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1509aadd06e5SJens Axboe 	}
1510aadd06e5SJens Axboe 
151161e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1512aadd06e5SJens Axboe 	return ret;
1513aadd06e5SJens Axboe }
1514aadd06e5SJens Axboe 
1515aadd06e5SJens Axboe /*
1516aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1517aadd06e5SJens Axboe  * return an appropriate error.
1518aadd06e5SJens Axboe  */
15197c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1520aadd06e5SJens Axboe {
1521aadd06e5SJens Axboe 	int ret;
1522aadd06e5SJens Axboe 
1523aadd06e5SJens Axboe 	/*
15248cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1525aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1526aadd06e5SJens Axboe 	 */
1527566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1528aadd06e5SJens Axboe 		return 0;
1529aadd06e5SJens Axboe 
1530aadd06e5SJens Axboe 	ret = 0;
153161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1532aadd06e5SJens Axboe 
15336718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1534aadd06e5SJens Axboe 		if (!pipe->readers) {
1535aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1536aadd06e5SJens Axboe 			ret = -EPIPE;
1537aadd06e5SJens Axboe 			break;
1538aadd06e5SJens Axboe 		}
1539aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1540aadd06e5SJens Axboe 			ret = -EAGAIN;
1541aadd06e5SJens Axboe 			break;
1542aadd06e5SJens Axboe 		}
1543aadd06e5SJens Axboe 		if (signal_pending(current)) {
1544aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1545aadd06e5SJens Axboe 			break;
1546aadd06e5SJens Axboe 		}
1547472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1548aadd06e5SJens Axboe 	}
1549aadd06e5SJens Axboe 
155061e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1551aadd06e5SJens Axboe 	return ret;
1552aadd06e5SJens Axboe }
1553aadd06e5SJens Axboe 
1554aadd06e5SJens Axboe /*
15557c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15567c77f0b3SMiklos Szeredi  */
15577c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15587c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15597c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15607c77f0b3SMiklos Szeredi {
15617c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15628cefc107SDavid Howells 	unsigned int i_head, o_head;
15638cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15648cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15658cefc107SDavid Howells 	int ret = 0;
15667c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15677c77f0b3SMiklos Szeredi 
15687c77f0b3SMiklos Szeredi 
15697c77f0b3SMiklos Szeredi retry:
15707c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15717c77f0b3SMiklos Szeredi 	if (ret)
15727c77f0b3SMiklos Szeredi 		return ret;
15737c77f0b3SMiklos Szeredi 
15747c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15757c77f0b3SMiklos Szeredi 	if (ret)
15767c77f0b3SMiklos Szeredi 		return ret;
15777c77f0b3SMiklos Szeredi 
15787c77f0b3SMiklos Szeredi 	/*
15797c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15807c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15817c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15827c77f0b3SMiklos Szeredi 	 */
15837c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15847c77f0b3SMiklos Szeredi 
15858cefc107SDavid Howells 	i_tail = ipipe->tail;
15868cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15878cefc107SDavid Howells 	o_head = opipe->head;
15888cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15898cefc107SDavid Howells 
15907c77f0b3SMiklos Szeredi 	do {
15918cefc107SDavid Howells 		size_t o_len;
15928cefc107SDavid Howells 
15937c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15947c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15957c77f0b3SMiklos Szeredi 			if (!ret)
15967c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15977c77f0b3SMiklos Szeredi 			break;
15987c77f0b3SMiklos Szeredi 		}
15997c77f0b3SMiklos Szeredi 
16008cefc107SDavid Howells 		i_head = ipipe->head;
16018cefc107SDavid Howells 		o_tail = opipe->tail;
16028cefc107SDavid Howells 
16038cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
16047c77f0b3SMiklos Szeredi 			break;
16057c77f0b3SMiklos Szeredi 
16067c77f0b3SMiklos Szeredi 		/*
16077c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
16087c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
16097c77f0b3SMiklos Szeredi 		 */
16108cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
16116718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
16127c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
16137c77f0b3SMiklos Szeredi 			if (ret)
16147c77f0b3SMiklos Szeredi 				break;
16157c77f0b3SMiklos Szeredi 
16167c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
16177c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
16187c77f0b3SMiklos Szeredi 				break;
16197c77f0b3SMiklos Szeredi 			}
16207c77f0b3SMiklos Szeredi 
16217c77f0b3SMiklos Szeredi 			/*
16227c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
16237c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
16247c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
16257c77f0b3SMiklos Szeredi 			 */
16267c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
16277c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
16287c77f0b3SMiklos Szeredi 			goto retry;
16297c77f0b3SMiklos Szeredi 		}
16307c77f0b3SMiklos Szeredi 
16318cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16328cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
16337c77f0b3SMiklos Szeredi 
16347c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
16357c77f0b3SMiklos Szeredi 			/*
16367c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
16377c77f0b3SMiklos Szeredi 			 */
16387c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16397c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
16408cefc107SDavid Howells 			i_tail++;
16418cefc107SDavid Howells 			ipipe->tail = i_tail;
16427c77f0b3SMiklos Szeredi 			input_wakeup = true;
16438cefc107SDavid Howells 			o_len = obuf->len;
16448cefc107SDavid Howells 			o_head++;
16458cefc107SDavid Howells 			opipe->head = o_head;
16467c77f0b3SMiklos Szeredi 		} else {
16477c77f0b3SMiklos Szeredi 			/*
16487c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16497c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16507c77f0b3SMiklos Szeredi 			 */
165115fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
165215fab63eSMatthew Wilcox 				if (ret == 0)
165315fab63eSMatthew Wilcox 					ret = -EFAULT;
165415fab63eSMatthew Wilcox 				break;
165515fab63eSMatthew Wilcox 			}
16567c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16577c77f0b3SMiklos Szeredi 
16587c77f0b3SMiklos Szeredi 			/*
1659f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
16607c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16617c77f0b3SMiklos Szeredi 			 */
16627c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1663f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1664a0ce2f0aSJann Horn 
16657c77f0b3SMiklos Szeredi 			obuf->len = len;
16668cefc107SDavid Howells 			ibuf->offset += len;
16678cefc107SDavid Howells 			ibuf->len -= len;
16688cefc107SDavid Howells 			o_len = len;
16698cefc107SDavid Howells 			o_head++;
16708cefc107SDavid Howells 			opipe->head = o_head;
16717c77f0b3SMiklos Szeredi 		}
16728cefc107SDavid Howells 		ret += o_len;
16738cefc107SDavid Howells 		len -= o_len;
16747c77f0b3SMiklos Szeredi 	} while (len);
16757c77f0b3SMiklos Szeredi 
16767c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16777c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16787c77f0b3SMiklos Szeredi 
16797c77f0b3SMiklos Szeredi 	/*
16807c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16817c77f0b3SMiklos Szeredi 	 */
1682825cdcb1SNamhyung Kim 	if (ret > 0)
1683825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1684825cdcb1SNamhyung Kim 
16857c77f0b3SMiklos Szeredi 	if (input_wakeup)
16867c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16877c77f0b3SMiklos Szeredi 
16887c77f0b3SMiklos Szeredi 	return ret;
16897c77f0b3SMiklos Szeredi }
16907c77f0b3SMiklos Szeredi 
16917c77f0b3SMiklos Szeredi /*
169270524490SJens Axboe  * Link contents of ipipe to opipe.
169370524490SJens Axboe  */
169470524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
169570524490SJens Axboe 		     struct pipe_inode_info *opipe,
169670524490SJens Axboe 		     size_t len, unsigned int flags)
169770524490SJens Axboe {
169870524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
16998cefc107SDavid Howells 	unsigned int i_head, o_head;
17008cefc107SDavid Howells 	unsigned int i_tail, o_tail;
17018cefc107SDavid Howells 	unsigned int i_mask, o_mask;
17028cefc107SDavid Howells 	int ret = 0;
170370524490SJens Axboe 
170470524490SJens Axboe 	/*
170570524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
170661e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
170770524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
170870524490SJens Axboe 	 */
170961e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
171070524490SJens Axboe 
17118cefc107SDavid Howells 	i_tail = ipipe->tail;
17128cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
17138cefc107SDavid Howells 	o_head = opipe->head;
17148cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
17158cefc107SDavid Howells 
1716aadd06e5SJens Axboe 	do {
171770524490SJens Axboe 		if (!opipe->readers) {
171870524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
171970524490SJens Axboe 			if (!ret)
172070524490SJens Axboe 				ret = -EPIPE;
172170524490SJens Axboe 			break;
172270524490SJens Axboe 		}
172370524490SJens Axboe 
17248cefc107SDavid Howells 		i_head = ipipe->head;
17258cefc107SDavid Howells 		o_tail = opipe->tail;
17268cefc107SDavid Howells 
172770524490SJens Axboe 		/*
17288cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1729aadd06e5SJens Axboe 		 * output room, break.
173070524490SJens Axboe 		 */
17318cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17326718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1733aadd06e5SJens Axboe 			break;
1734aadd06e5SJens Axboe 
17358cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17368cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
173770524490SJens Axboe 
173870524490SJens Axboe 		/*
173970524490SJens Axboe 		 * Get a reference to this pipe buffer,
174070524490SJens Axboe 		 * so we can copy the contents over.
174170524490SJens Axboe 		 */
174215fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
174315fab63eSMatthew Wilcox 			if (ret == 0)
174415fab63eSMatthew Wilcox 				ret = -EFAULT;
174515fab63eSMatthew Wilcox 			break;
174615fab63eSMatthew Wilcox 		}
174770524490SJens Axboe 
174870524490SJens Axboe 		*obuf = *ibuf;
174970524490SJens Axboe 
17507afa6fd0SJens Axboe 		/*
1751f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1752f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
17537afa6fd0SJens Axboe 		 */
17547afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1755f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1756a0ce2f0aSJann Horn 
175770524490SJens Axboe 		if (obuf->len > len)
175870524490SJens Axboe 			obuf->len = len;
175970524490SJens Axboe 		ret += obuf->len;
176070524490SJens Axboe 		len -= obuf->len;
17618cefc107SDavid Howells 
17628cefc107SDavid Howells 		o_head++;
17638cefc107SDavid Howells 		opipe->head = o_head;
17648cefc107SDavid Howells 		i_tail++;
1765aadd06e5SJens Axboe 	} while (len);
176670524490SJens Axboe 
176761e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
176861e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
176970524490SJens Axboe 
1770aadd06e5SJens Axboe 	/*
1771aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1772aadd06e5SJens Axboe 	 */
1773825cdcb1SNamhyung Kim 	if (ret > 0)
1774825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
177570524490SJens Axboe 
177670524490SJens Axboe 	return ret;
177770524490SJens Axboe }
177870524490SJens Axboe 
177970524490SJens Axboe /*
178070524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
178170524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
178270524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
178370524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
178470524490SJens Axboe  */
17859dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
178670524490SJens Axboe {
1787c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1788c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1789aadd06e5SJens Axboe 	int ret = -EINVAL;
179070524490SJens Axboe 
179190da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
179290da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
179390da2e3fSPavel Begunkov 		return -EBADF;
179490da2e3fSPavel Begunkov 
179570524490SJens Axboe 	/*
1796aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1797aadd06e5SJens Axboe 	 * copying the data.
179870524490SJens Axboe 	 */
1799aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1800ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1801ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1802ee5e0011SSlavomir Kaslev 
1803aadd06e5SJens Axboe 		/*
1804aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1805aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1806aadd06e5SJens Axboe 		 */
18077c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1808aadd06e5SJens Axboe 		if (!ret) {
18097c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
181002cf01aeSJens Axboe 			if (!ret)
1811aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1812aadd06e5SJens Axboe 		}
1813aadd06e5SJens Axboe 	}
181470524490SJens Axboe 
1815aadd06e5SJens Axboe 	return ret;
181670524490SJens Axboe }
181770524490SJens Axboe 
1818836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
181970524490SJens Axboe {
182090da2e3fSPavel Begunkov 	struct fd in, out;
18212903ff01SAl Viro 	int error;
182270524490SJens Axboe 
18233d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
18243d6ea290SAl Viro 		return -EINVAL;
18253d6ea290SAl Viro 
182670524490SJens Axboe 	if (unlikely(!len))
182770524490SJens Axboe 		return 0;
182870524490SJens Axboe 
182970524490SJens Axboe 	error = -EBADF;
18302903ff01SAl Viro 	in = fdget(fdin);
18312903ff01SAl Viro 	if (in.file) {
183290da2e3fSPavel Begunkov 		out = fdget(fdout);
18332903ff01SAl Viro 		if (out.file) {
183490da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
18352903ff01SAl Viro 			fdput(out);
183670524490SJens Axboe 		}
18372903ff01SAl Viro  		fdput(in);
183870524490SJens Axboe  	}
183970524490SJens Axboe 
184070524490SJens Axboe 	return error;
184170524490SJens Axboe }
1842