xref: /openbmc/linux/fs/splice.c (revision aa3dbde8)
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 /*
30369df79a4SDavid Howells  * Copy data from a file into pages and then splice those into the output pipe.
30433b3b041SDavid Howells  */
30569df79a4SDavid 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;
314e69f37bcSDavid Howells 	size_t used, npages, chunk, remain, keep = 0;
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 
328e69f37bcSDavid Howells 	pages = (struct page **)(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 	if (ret > 0) {
352e69f37bcSDavid Howells 		keep = DIV_ROUND_UP(ret, PAGE_SIZE);
35333b3b041SDavid Howells 		*ppos = kiocb.ki_pos;
35433b3b041SDavid Howells 		file_accessed(in);
35533b3b041SDavid Howells 	} else if (ret < 0) {
35633b3b041SDavid Howells 		/*
35733b3b041SDavid Howells 		 * callers of ->splice_read() expect -EAGAIN on
35833b3b041SDavid Howells 		 * "can't put anything in there", rather than -EFAULT.
35933b3b041SDavid Howells 		 */
36033b3b041SDavid Howells 		if (ret == -EFAULT)
36133b3b041SDavid Howells 			ret = -EAGAIN;
36233b3b041SDavid Howells 	}
36333b3b041SDavid Howells 
36433b3b041SDavid Howells 	/* Free any pages that didn't get touched at all. */
365e69f37bcSDavid Howells 	if (keep < npages)
366e69f37bcSDavid Howells 		release_pages(pages + keep, npages - keep);
36733b3b041SDavid Howells 
36833b3b041SDavid Howells 	/* Push the remaining pages into the pipe. */
369e69f37bcSDavid Howells 	remain = ret;
370e69f37bcSDavid Howells 	for (i = 0; i < keep; i++) {
37133b3b041SDavid Howells 		struct pipe_buffer *buf = pipe_head_buf(pipe);
37233b3b041SDavid Howells 
37333b3b041SDavid Howells 		chunk = min_t(size_t, remain, PAGE_SIZE);
37433b3b041SDavid Howells 		*buf = (struct pipe_buffer) {
37533b3b041SDavid Howells 			.ops	= &default_pipe_buf_ops,
37633b3b041SDavid Howells 			.page	= bv[i].bv_page,
37733b3b041SDavid Howells 			.offset	= 0,
37833b3b041SDavid Howells 			.len	= chunk,
37933b3b041SDavid Howells 		};
38033b3b041SDavid Howells 		pipe->head++;
38133b3b041SDavid Howells 		remain -= chunk;
38233b3b041SDavid Howells 	}
38333b3b041SDavid Howells 
38433b3b041SDavid Howells 	kfree(bv);
38533b3b041SDavid Howells 	return ret;
38633b3b041SDavid Howells }
38769df79a4SDavid Howells EXPORT_SYMBOL(copy_splice_read);
38833b3b041SDavid Howells 
38983f9135bSJens Axboe /**
39083f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
39183f9135bSJens Axboe  * @in:		file to splice from
392932cc6d4SJens Axboe  * @ppos:	position in @in
39383f9135bSJens Axboe  * @pipe:	pipe to splice to
39483f9135bSJens Axboe  * @len:	number of bytes to splice
39583f9135bSJens Axboe  * @flags:	splice modifier flags
39683f9135bSJens Axboe  *
397932cc6d4SJens Axboe  * Description:
398932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
39982c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
400932cc6d4SJens Axboe  *
40183f9135bSJens Axboe  */
402cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
403cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
404cbb7e577SJens Axboe 				 unsigned int flags)
4055274f052SJens Axboe {
40682c156f8SAl Viro 	struct iov_iter to;
40782c156f8SAl Viro 	struct kiocb kiocb;
4088cefc107SDavid Howells 	int ret;
409be64f884SBoaz Harrosh 
410de4eda9dSAl Viro 	iov_iter_pipe(&to, ITER_DEST, pipe, len);
41182c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
41282c156f8SAl Viro 	kiocb.ki_pos = *ppos;
413bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
414723590edSMiklos Szeredi 	if (ret > 0) {
41582c156f8SAl Viro 		*ppos = kiocb.ki_pos;
416723590edSMiklos Szeredi 		file_accessed(in);
41782c156f8SAl Viro 	} else if (ret < 0) {
4180d964934SAl Viro 		/* free what was emitted */
4190d964934SAl Viro 		pipe_discard_from(pipe, to.start_head);
42082c156f8SAl Viro 		/*
42182c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
42282c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
42382c156f8SAl Viro 		 */
42482c156f8SAl Viro 		if (ret == -EFAULT)
42582c156f8SAl Viro 			ret = -EAGAIN;
426723590edSMiklos Szeredi 	}
4275274f052SJens Axboe 
4285274f052SJens Axboe 	return ret;
4295274f052SJens Axboe }
430059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
431059a8f37SJens Axboe 
432241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
4336818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
434c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
4356818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
4366818173bSMiklos Szeredi };
4376818173bSMiklos Szeredi 
43828a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
43928a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
44028a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
44128a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
44228a625cbSMiklos Szeredi };
44328a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
44428a625cbSMiklos Szeredi 
4455274f052SJens Axboe /*
4464f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
447016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4485274f052SJens Axboe  */
44976ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4505274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4515274f052SJens Axboe {
4526a14b90bSJens Axboe 	struct file *file = sd->u.file;
4535274f052SJens Axboe 	loff_t pos = sd->pos;
454a8adbe37SMichał Mirosław 	int more;
4555274f052SJens Axboe 
45672c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
457a8adbe37SMichał Mirosław 		return -EINVAL;
458a8adbe37SMichał Mirosław 
45935f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
460ae62ca7bSEric Dumazet 
4618cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4628cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
46335f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
464ae62ca7bSEric Dumazet 
465a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
466f84d7519SJens Axboe 				    sd->len, &pos, more);
4675274f052SJens Axboe }
4685274f052SJens Axboe 
469b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
470b3c2d2ddSMiklos Szeredi {
471b3c2d2ddSMiklos Szeredi 	smp_mb();
4720ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4730ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
474b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
475b3c2d2ddSMiklos Szeredi }
476b3c2d2ddSMiklos Szeredi 
477b3c2d2ddSMiklos Szeredi /**
478b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
479b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
480b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
481b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
482b3c2d2ddSMiklos Szeredi  *
483b3c2d2ddSMiklos Szeredi  * Description:
484b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
485b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
486b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
487b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
488b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
489b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
490b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
491b3c2d2ddSMiklos Szeredi  *
492b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
493b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
494b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
495b3c2d2ddSMiklos Szeredi  *    destination.
496b3c2d2ddSMiklos Szeredi  */
49796f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
498b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
499b3c2d2ddSMiklos Szeredi {
5008cefc107SDavid Howells 	unsigned int head = pipe->head;
5018cefc107SDavid Howells 	unsigned int tail = pipe->tail;
5028cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
503b3c2d2ddSMiklos Szeredi 	int ret;
504b3c2d2ddSMiklos Szeredi 
505ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
5068cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
507b3c2d2ddSMiklos Szeredi 
508b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
509b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
510b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
511b3c2d2ddSMiklos Szeredi 
512fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
513a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
514b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
515b3c2d2ddSMiklos Szeredi 				ret = 0;
516b3c2d2ddSMiklos Szeredi 			return ret;
517b3c2d2ddSMiklos Szeredi 		}
518a8adbe37SMichał Mirosław 
519a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
520a8adbe37SMichał Mirosław 		if (ret <= 0)
521a8adbe37SMichał Mirosław 			return ret;
522a8adbe37SMichał Mirosław 
523b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
524b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
525b3c2d2ddSMiklos Szeredi 
526b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
527b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
528b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
529b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
530b3c2d2ddSMiklos Szeredi 
531b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
532a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5338cefc107SDavid Howells 			tail++;
5348cefc107SDavid Howells 			pipe->tail = tail;
5356447a3cfSAl Viro 			if (pipe->files)
536b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
537b3c2d2ddSMiklos Szeredi 		}
538b3c2d2ddSMiklos Szeredi 
539b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
540b3c2d2ddSMiklos Szeredi 			return 0;
541b3c2d2ddSMiklos Szeredi 	}
542b3c2d2ddSMiklos Szeredi 
543b3c2d2ddSMiklos Szeredi 	return 1;
544b3c2d2ddSMiklos Szeredi }
545b3c2d2ddSMiklos Szeredi 
546d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
547d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
548d1a819a2SLinus Torvalds {
549d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
550d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
551d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
552d1a819a2SLinus Torvalds 
553d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
554d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
555d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
556d1a819a2SLinus Torvalds 		return true;
557d1a819a2SLinus Torvalds 	}
558d1a819a2SLinus Torvalds 
559d1a819a2SLinus Torvalds 	return false;
560d1a819a2SLinus Torvalds }
561d1a819a2SLinus Torvalds 
562b3c2d2ddSMiklos Szeredi /**
563b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
564b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
565b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
566b3c2d2ddSMiklos Szeredi  *
567b3c2d2ddSMiklos Szeredi  * Description:
568b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
569b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
570b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
571b3c2d2ddSMiklos Szeredi  */
57296f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
573b3c2d2ddSMiklos Szeredi {
574c725bfceSJan Kara 	/*
575c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
576c725bfceSJan Kara 	 * always buffers available
577c725bfceSJan Kara 	 */
578c725bfceSJan Kara 	if (signal_pending(current))
579c725bfceSJan Kara 		return -ERESTARTSYS;
580c725bfceSJan Kara 
581d1a819a2SLinus Torvalds repeat:
5828cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
583b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
584b3c2d2ddSMiklos Szeredi 			return 0;
585b3c2d2ddSMiklos Szeredi 
586a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
587b3c2d2ddSMiklos Szeredi 			return 0;
588b3c2d2ddSMiklos Szeredi 
589b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
590b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
591b3c2d2ddSMiklos Szeredi 
592b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
593b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
594b3c2d2ddSMiklos Szeredi 
595b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
596b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
597b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
598b3c2d2ddSMiklos Szeredi 		}
599b3c2d2ddSMiklos Szeredi 
600472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
601b3c2d2ddSMiklos Szeredi 	}
602b3c2d2ddSMiklos Szeredi 
603d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
604d1a819a2SLinus Torvalds 		goto repeat;
605d1a819a2SLinus Torvalds 
606b3c2d2ddSMiklos Szeredi 	return 1;
607b3c2d2ddSMiklos Szeredi }
608b3c2d2ddSMiklos Szeredi 
609b3c2d2ddSMiklos Szeredi /**
610b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
611b80901bbSRandy Dunlap  * @sd:		information about the splice operation
612b3c2d2ddSMiklos Szeredi  *
613b3c2d2ddSMiklos Szeredi  * Description:
614b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
615b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
616b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
617b3c2d2ddSMiklos Szeredi  */
61896f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
619b3c2d2ddSMiklos Szeredi {
620b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
621b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
622b3c2d2ddSMiklos Szeredi }
623b3c2d2ddSMiklos Szeredi 
624b3c2d2ddSMiklos Szeredi /**
625b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
626b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
627b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
628b3c2d2ddSMiklos Szeredi  *
629b3c2d2ddSMiklos Szeredi  * Description:
630b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
631b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
632b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
633b3c2d2ddSMiklos Szeredi  */
63496f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
635b3c2d2ddSMiklos Szeredi {
636b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
637b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
638b3c2d2ddSMiklos Szeredi }
639b3c2d2ddSMiklos Szeredi 
640932cc6d4SJens Axboe /**
641932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
642932cc6d4SJens Axboe  * @pipe:	pipe to splice from
643932cc6d4SJens Axboe  * @sd:		information to @actor
644932cc6d4SJens Axboe  * @actor:	handler that splices the data
645932cc6d4SJens Axboe  *
646932cc6d4SJens Axboe  * Description:
647932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
648932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
649932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
650932cc6d4SJens Axboe  *    pipe_to_user.
651932cc6d4SJens Axboe  *
65283f9135bSJens Axboe  */
653c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
654c66ab6faSJens Axboe 			   splice_actor *actor)
6555274f052SJens Axboe {
656b3c2d2ddSMiklos Szeredi 	int ret;
6575274f052SJens Axboe 
658b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
659b3c2d2ddSMiklos Szeredi 	do {
660c2489e07SJan Kara 		cond_resched();
661b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
662b3c2d2ddSMiklos Szeredi 		if (ret > 0)
663b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
664b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
665b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6665274f052SJens Axboe 
667b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6685274f052SJens Axboe }
66940bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6705274f052SJens Axboe 
671932cc6d4SJens Axboe /**
672932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
673932cc6d4SJens Axboe  * @pipe:	pipe to splice from
674932cc6d4SJens Axboe  * @out:	file to splice to
675932cc6d4SJens Axboe  * @ppos:	position in @out
676932cc6d4SJens Axboe  * @len:	how many bytes to splice
677932cc6d4SJens Axboe  * @flags:	splice modifier flags
678932cc6d4SJens Axboe  * @actor:	handler that splices the data
679932cc6d4SJens Axboe  *
680932cc6d4SJens Axboe  * Description:
6812933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
682932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
683932cc6d4SJens Axboe  *
684932cc6d4SJens Axboe  */
6856da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6866da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6876da61809SMark Fasheh 			 splice_actor *actor)
6886da61809SMark Fasheh {
6896da61809SMark Fasheh 	ssize_t ret;
690c66ab6faSJens Axboe 	struct splice_desc sd = {
691c66ab6faSJens Axboe 		.total_len = len,
692c66ab6faSJens Axboe 		.flags = flags,
693c66ab6faSJens Axboe 		.pos = *ppos,
6946a14b90bSJens Axboe 		.u.file = out,
695c66ab6faSJens Axboe 	};
6966da61809SMark Fasheh 
69761e0d47cSMiklos Szeredi 	pipe_lock(pipe);
698c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
69961e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
7006da61809SMark Fasheh 
7016da61809SMark Fasheh 	return ret;
7026da61809SMark Fasheh }
7036da61809SMark Fasheh 
7046da61809SMark Fasheh /**
7058d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
7068d020765SAl Viro  * @pipe:	pipe info
7078d020765SAl Viro  * @out:	file to write to
7088d020765SAl Viro  * @ppos:	position in @out
7098d020765SAl Viro  * @len:	number of bytes to splice
7108d020765SAl Viro  * @flags:	splice modifier flags
7118d020765SAl Viro  *
7128d020765SAl Viro  * Description:
7138d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
7148d020765SAl Viro  *    the given pipe inode to the given file.
7158d020765SAl Viro  *    This one is ->write_iter-based.
7168d020765SAl Viro  *
7178d020765SAl Viro  */
7188d020765SAl Viro ssize_t
7198d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
7208d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
7218d020765SAl Viro {
7228d020765SAl Viro 	struct splice_desc sd = {
7238d020765SAl Viro 		.total_len = len,
7248d020765SAl Viro 		.flags = flags,
7258d020765SAl Viro 		.pos = *ppos,
7268d020765SAl Viro 		.u.file = out,
7278d020765SAl Viro 	};
7286718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
7298d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7308d020765SAl Viro 					GFP_KERNEL);
7318d020765SAl Viro 	ssize_t ret;
7328d020765SAl Viro 
7338d020765SAl Viro 	if (unlikely(!array))
7348d020765SAl Viro 		return -ENOMEM;
7358d020765SAl Viro 
7368d020765SAl Viro 	pipe_lock(pipe);
7378d020765SAl Viro 
7388d020765SAl Viro 	splice_from_pipe_begin(&sd);
7398d020765SAl Viro 	while (sd.total_len) {
7408d020765SAl Viro 		struct iov_iter from;
741ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7428d020765SAl Viro 		size_t left;
7438cefc107SDavid Howells 		int n;
7448d020765SAl Viro 
7458d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7468d020765SAl Viro 		if (ret <= 0)
7478d020765SAl Viro 			break;
7488d020765SAl Viro 
7496718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7508d020765SAl Viro 			kfree(array);
7516718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7528d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7538d020765SAl Viro 					GFP_KERNEL);
7548d020765SAl Viro 			if (!array) {
7558d020765SAl Viro 				ret = -ENOMEM;
7568d020765SAl Viro 				break;
7578d020765SAl Viro 			}
7588d020765SAl Viro 		}
7598d020765SAl Viro 
760ec057595SLinus Torvalds 		head = pipe->head;
761ec057595SLinus Torvalds 		tail = pipe->tail;
762ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
763ec057595SLinus Torvalds 
7648d020765SAl Viro 		/* build the vector */
7658d020765SAl Viro 		left = sd.total_len;
7660f1d344fSPavel Begunkov 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
7678cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7688d020765SAl Viro 			size_t this_len = buf->len;
7698d020765SAl Viro 
7700f1d344fSPavel Begunkov 			/* zero-length bvecs are not supported, skip them */
7710f1d344fSPavel Begunkov 			if (!this_len)
7720f1d344fSPavel Begunkov 				continue;
7730f1d344fSPavel Begunkov 			this_len = min(this_len, left);
7748d020765SAl Viro 
775fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7768d020765SAl Viro 			if (unlikely(ret)) {
7778d020765SAl Viro 				if (ret == -ENODATA)
7788d020765SAl Viro 					ret = 0;
7798d020765SAl Viro 				goto done;
7808d020765SAl Viro 			}
7818d020765SAl Viro 
782664e4078SChristoph Hellwig 			bvec_set_page(&array[n], buf->page, this_len,
783664e4078SChristoph Hellwig 				      buf->offset);
7848d020765SAl Viro 			left -= this_len;
7850f1d344fSPavel Begunkov 			n++;
7868d020765SAl Viro 		}
7878d020765SAl Viro 
788de4eda9dSAl Viro 		iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
789abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7908d020765SAl Viro 		if (ret <= 0)
7918d020765SAl Viro 			break;
7928d020765SAl Viro 
7938d020765SAl Viro 		sd.num_spliced += ret;
7948d020765SAl Viro 		sd.total_len -= ret;
795dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7968d020765SAl Viro 
7978d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7988cefc107SDavid Howells 		tail = pipe->tail;
7998d020765SAl Viro 		while (ret) {
8008cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
8018d020765SAl Viro 			if (ret >= buf->len) {
8028d020765SAl Viro 				ret -= buf->len;
8038d020765SAl Viro 				buf->len = 0;
804a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
8058cefc107SDavid Howells 				tail++;
8068cefc107SDavid Howells 				pipe->tail = tail;
8078d020765SAl Viro 				if (pipe->files)
8088d020765SAl Viro 					sd.need_wakeup = true;
8098d020765SAl Viro 			} else {
8108d020765SAl Viro 				buf->offset += ret;
8118d020765SAl Viro 				buf->len -= ret;
8128d020765SAl Viro 				ret = 0;
8138d020765SAl Viro 			}
8148d020765SAl Viro 		}
8158d020765SAl Viro 	}
8168d020765SAl Viro done:
8178d020765SAl Viro 	kfree(array);
8188d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
8198d020765SAl Viro 
8208d020765SAl Viro 	pipe_unlock(pipe);
8218d020765SAl Viro 
8228d020765SAl Viro 	if (sd.num_spliced)
8238d020765SAl Viro 		ret = sd.num_spliced;
8248d020765SAl Viro 
8258d020765SAl Viro 	return ret;
8268d020765SAl Viro }
8278d020765SAl Viro 
8288d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8298d020765SAl Viro 
83083f9135bSJens Axboe /**
83183f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
832932cc6d4SJens Axboe  * @pipe:	pipe to splice from
83383f9135bSJens Axboe  * @out:	socket to write to
834932cc6d4SJens Axboe  * @ppos:	position in @out
83583f9135bSJens Axboe  * @len:	number of bytes to splice
83683f9135bSJens Axboe  * @flags:	splice modifier flags
83783f9135bSJens Axboe  *
838932cc6d4SJens Axboe  * Description:
83983f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
84083f9135bSJens Axboe  *    is involved.
84183f9135bSJens Axboe  *
84283f9135bSJens Axboe  */
8433a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
844cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8455274f052SJens Axboe {
84600522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8475274f052SJens Axboe }
8485274f052SJens Axboe 
849059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
850a0f06780SJeff Garzik 
85136e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op)
85236e2c742SChristoph Hellwig {
85336e2c742SChristoph Hellwig 	pr_debug_ratelimited(
85436e2c742SChristoph Hellwig 		"splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
85536e2c742SChristoph Hellwig 		op, file, current->pid, current->comm);
85636e2c742SChristoph Hellwig 	return -EINVAL;
85736e2c742SChristoph Hellwig }
85836e2c742SChristoph Hellwig 
85983f9135bSJens Axboe /*
86083f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
86183f9135bSJens Axboe  */
8623a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
863cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8645274f052SJens Axboe {
86536e2c742SChristoph Hellwig 	if (unlikely(!out->f_op->splice_write))
86636e2c742SChristoph Hellwig 		return warn_unsupported(out, "write");
86700c285d0SChristoph Hellwig 	return out->f_op->splice_write(pipe, out, ppos, len, flags);
8685274f052SJens Axboe }
8695274f052SJens Axboe 
8706a3f30b8SDavid Howells /**
8716a3f30b8SDavid Howells  * vfs_splice_read - Read data from a file and splice it into a pipe
8726a3f30b8SDavid Howells  * @in:		File to splice from
8736a3f30b8SDavid Howells  * @ppos:	Input file offset
8746a3f30b8SDavid Howells  * @pipe:	Pipe to splice to
8756a3f30b8SDavid Howells  * @len:	Number of bytes to splice
8766a3f30b8SDavid Howells  * @flags:	Splice modifier flags (SPLICE_F_*)
8776a3f30b8SDavid Howells  *
8786a3f30b8SDavid Howells  * Splice the requested amount of data from the input file to the pipe.  This
8796a3f30b8SDavid Howells  * is synchronous as the caller must hold the pipe lock across the entire
8806a3f30b8SDavid Howells  * operation.
8816a3f30b8SDavid Howells  *
8826a3f30b8SDavid Howells  * If successful, it returns the amount of data spliced, 0 if it hit the EOF or
8836a3f30b8SDavid Howells  * a hole and a negative error code otherwise.
88483f9135bSJens Axboe  */
8856a3f30b8SDavid Howells long vfs_splice_read(struct file *in, loff_t *ppos,
886cbb7e577SJens Axboe 		     struct pipe_inode_info *pipe, size_t len,
887cbb7e577SJens Axboe 		     unsigned int flags)
8885274f052SJens Axboe {
889313d64a3SAl Viro 	unsigned int p_space;
8905274f052SJens Axboe 	int ret;
8915274f052SJens Axboe 
89249570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8935274f052SJens Axboe 		return -EBADF;
894123856f0SDavid Howells 	if (!len)
895123856f0SDavid Howells 		return 0;
8965274f052SJens Axboe 
897313d64a3SAl Viro 	/* Don't try to read more the pipe has space for. */
898313d64a3SAl Viro 	p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
899313d64a3SAl Viro 	len = min_t(size_t, len, p_space << PAGE_SHIFT);
900313d64a3SAl Viro 
901cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
9025274f052SJens Axboe 	if (unlikely(ret < 0))
9035274f052SJens Axboe 		return ret;
9045274f052SJens Axboe 
90503cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
90603cc0789SAl Viro 		len = MAX_RW_COUNT;
90703cc0789SAl Viro 
90836e2c742SChristoph Hellwig 	if (unlikely(!in->f_op->splice_read))
90936e2c742SChristoph Hellwig 		return warn_unsupported(in, "read");
910*aa3dbde8SDavid Howells 	/*
911*aa3dbde8SDavid Howells 	 * O_DIRECT doesn't deal with the pagecache, so we allocate a buffer,
912*aa3dbde8SDavid Howells 	 * copy into it and splice that into the pipe.
913*aa3dbde8SDavid Howells 	 */
914*aa3dbde8SDavid Howells 	if ((in->f_flags & O_DIRECT))
915*aa3dbde8SDavid Howells 		return copy_splice_read(in, ppos, pipe, len, flags);
9162bc01060SChristoph Hellwig 	return in->f_op->splice_read(in, ppos, pipe, len, flags);
9175274f052SJens Axboe }
9186a3f30b8SDavid Howells EXPORT_SYMBOL_GPL(vfs_splice_read);
9195274f052SJens Axboe 
920932cc6d4SJens Axboe /**
921932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
922932cc6d4SJens Axboe  * @in:		file to splice from
923932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
924932cc6d4SJens Axboe  * @actor:	handles the data splicing
925932cc6d4SJens Axboe  *
926932cc6d4SJens Axboe  * Description:
927932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
928932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
929932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
930932cc6d4SJens Axboe  *    that process.
931932cc6d4SJens Axboe  *
932c66ab6faSJens Axboe  */
933c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
934c66ab6faSJens Axboe 			       splice_direct_actor *actor)
935b92ce558SJens Axboe {
936b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
937b92ce558SJens Axboe 	long ret, bytes;
938c66ab6faSJens Axboe 	size_t len;
9390ff28d9fSChristophe Leroy 	int i, flags, more;
940b92ce558SJens Axboe 
941b92ce558SJens Axboe 	/*
94297ef77c5SJason A. Donenfeld 	 * We require the input to be seekable, as we don't want to randomly
94397ef77c5SJason A. Donenfeld 	 * drop data for eg socket -> socket splicing. Use the piped splicing
94497ef77c5SJason A. Donenfeld 	 * for that!
945b92ce558SJens Axboe 	 */
94697ef77c5SJason A. Donenfeld 	if (unlikely(!(in->f_mode & FMODE_LSEEK)))
947b92ce558SJens Axboe 		return -EINVAL;
948b92ce558SJens Axboe 
949b92ce558SJens Axboe 	/*
950b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
951b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
952b92ce558SJens Axboe 	 */
953b92ce558SJens Axboe 	pipe = current->splice_pipe;
95449570e9bSJens Axboe 	if (unlikely(!pipe)) {
9557bee130eSAl Viro 		pipe = alloc_pipe_info();
956b92ce558SJens Axboe 		if (!pipe)
957b92ce558SJens Axboe 			return -ENOMEM;
958b92ce558SJens Axboe 
959b92ce558SJens Axboe 		/*
960b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
96100522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
962b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
963b92ce558SJens Axboe 		 */
964b92ce558SJens Axboe 		pipe->readers = 1;
965b92ce558SJens Axboe 
966b92ce558SJens Axboe 		current->splice_pipe = pipe;
967b92ce558SJens Axboe 	}
968b92ce558SJens Axboe 
969b92ce558SJens Axboe 	/*
97073d62d83SIngo Molnar 	 * Do the splice.
971b92ce558SJens Axboe 	 */
972b92ce558SJens Axboe 	bytes = 0;
973c66ab6faSJens Axboe 	len = sd->total_len;
974c66ab6faSJens Axboe 	flags = sd->flags;
975c66ab6faSJens Axboe 
976c66ab6faSJens Axboe 	/*
977c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
978c66ab6faSJens Axboe 	 */
979c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9800ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
981b92ce558SJens Axboe 
9828cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
98317614445SDarrick J. Wong 
984b92ce558SJens Axboe 	while (len) {
98551a92c0fSJens Axboe 		size_t read_len;
986a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
987b92ce558SJens Axboe 
9886a3f30b8SDavid Howells 		ret = vfs_splice_read(in, &pos, pipe, len, flags);
98951a92c0fSJens Axboe 		if (unlikely(ret <= 0))
990b92ce558SJens Axboe 			goto out_release;
991b92ce558SJens Axboe 
992b92ce558SJens Axboe 		read_len = ret;
993c66ab6faSJens Axboe 		sd->total_len = read_len;
994b92ce558SJens Axboe 
995b92ce558SJens Axboe 		/*
9960ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9970ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9980ff28d9fSChristophe Leroy 		 * initially, clears it.
9990ff28d9fSChristophe Leroy 		 */
10000ff28d9fSChristophe Leroy 		if (read_len < len)
10010ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
10020ff28d9fSChristophe Leroy 		else if (!more)
10030ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
10040ff28d9fSChristophe Leroy 		/*
1005b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
1006b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
1007b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
1008b92ce558SJens Axboe 		 */
1009c66ab6faSJens Axboe 		ret = actor(pipe, sd);
1010a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
1011a82c53a0STom Zanussi 			sd->pos = prev_pos;
1012b92ce558SJens Axboe 			goto out_release;
1013a82c53a0STom Zanussi 		}
1014b92ce558SJens Axboe 
1015b92ce558SJens Axboe 		bytes += ret;
1016b92ce558SJens Axboe 		len -= ret;
1017bcd4f3acSJens Axboe 		sd->pos = pos;
1018b92ce558SJens Axboe 
1019a82c53a0STom Zanussi 		if (ret < read_len) {
1020a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
102151a92c0fSJens Axboe 			goto out_release;
1022b92ce558SJens Axboe 		}
1023a82c53a0STom Zanussi 	}
1024b92ce558SJens Axboe 
10259e97198dSJens Axboe done:
10268cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
10279e97198dSJens Axboe 	file_accessed(in);
1028b92ce558SJens Axboe 	return bytes;
1029b92ce558SJens Axboe 
1030b92ce558SJens Axboe out_release:
1031b92ce558SJens Axboe 	/*
1032b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1033b92ce558SJens Axboe 	 * the pipe buffers in question:
1034b92ce558SJens Axboe 	 */
10358cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
10368cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1037b92ce558SJens Axboe 
1038a779638cSMiklos Szeredi 		if (buf->ops)
1039a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1040b92ce558SJens Axboe 	}
1041b92ce558SJens Axboe 
10429e97198dSJens Axboe 	if (!bytes)
10439e97198dSJens Axboe 		bytes = ret;
1044b92ce558SJens Axboe 
10459e97198dSJens Axboe 	goto done;
1046c66ab6faSJens Axboe }
1047c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1048c66ab6faSJens Axboe 
1049c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1050c66ab6faSJens Axboe 			       struct splice_desc *sd)
1051c66ab6faSJens Axboe {
10526a14b90bSJens Axboe 	struct file *file = sd->u.file;
1053c66ab6faSJens Axboe 
10547995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10552cb4b05eSChangli Gao 			      sd->flags);
1056c66ab6faSJens Axboe }
1057c66ab6faSJens Axboe 
1058932cc6d4SJens Axboe /**
1059932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1060932cc6d4SJens Axboe  * @in:		file to splice from
1061932cc6d4SJens Axboe  * @ppos:	input file offset
1062932cc6d4SJens Axboe  * @out:	file to splice to
1063acdb37c3SRandy Dunlap  * @opos:	output file offset
1064932cc6d4SJens Axboe  * @len:	number of bytes to splice
1065932cc6d4SJens Axboe  * @flags:	splice modifier flags
1066932cc6d4SJens Axboe  *
1067932cc6d4SJens Axboe  * Description:
1068932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1069932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1070932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1071932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1072932cc6d4SJens Axboe  *
1073932cc6d4SJens Axboe  */
1074c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10757995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1076c66ab6faSJens Axboe {
1077c66ab6faSJens Axboe 	struct splice_desc sd = {
1078c66ab6faSJens Axboe 		.len		= len,
1079c66ab6faSJens Axboe 		.total_len	= len,
1080c66ab6faSJens Axboe 		.flags		= flags,
1081c66ab6faSJens Axboe 		.pos		= *ppos,
10826a14b90bSJens Axboe 		.u.file		= out,
10837995bd28SAl Viro 		.opos		= opos,
1084c66ab6faSJens Axboe 	};
108551a92c0fSJens Axboe 	long ret;
1086c66ab6faSJens Axboe 
108718c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
108818c67cb9SAl Viro 		return -EBADF;
108918c67cb9SAl Viro 
109018c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
109118c67cb9SAl Viro 		return -EINVAL;
109218c67cb9SAl Viro 
109318c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
109418c67cb9SAl Viro 	if (unlikely(ret < 0))
109518c67cb9SAl Viro 		return ret;
109618c67cb9SAl Viro 
1097c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
109851a92c0fSJens Axboe 	if (ret > 0)
1099a82c53a0STom Zanussi 		*ppos = sd.pos;
110051a92c0fSJens Axboe 
1101c66ab6faSJens Axboe 	return ret;
1102b92ce558SJens Axboe }
11031c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1104b92ce558SJens Axboe 
11058924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
11068924feffSAl Viro {
110752bce911SLinus Torvalds 	for (;;) {
110852bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
110952bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
111052bce911SLinus Torvalds 			return -EPIPE;
111152bce911SLinus Torvalds 		}
11126718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
111352bce911SLinus Torvalds 			return 0;
11148924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
11158924feffSAl Viro 			return -EAGAIN;
11168924feffSAl Viro 		if (signal_pending(current))
11178924feffSAl Viro 			return -ERESTARTSYS;
1118472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
11198924feffSAl Viro 	}
11208924feffSAl Viro }
11218924feffSAl Viro 
11227c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
11237c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
11247c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1125ddac0d39SJens Axboe 
1126b964bf53SAl Viro long splice_file_to_pipe(struct file *in,
1127faa97c48SAl Viro 			 struct pipe_inode_info *opipe,
1128faa97c48SAl Viro 			 loff_t *offset,
1129faa97c48SAl Viro 			 size_t len, unsigned int flags)
1130faa97c48SAl Viro {
1131faa97c48SAl Viro 	long ret;
1132faa97c48SAl Viro 
1133faa97c48SAl Viro 	pipe_lock(opipe);
1134faa97c48SAl Viro 	ret = wait_for_space(opipe, flags);
1135faa97c48SAl Viro 	if (!ret)
11366a3f30b8SDavid Howells 		ret = vfs_splice_read(in, offset, opipe, len, flags);
1137faa97c48SAl Viro 	pipe_unlock(opipe);
1138faa97c48SAl Viro 	if (ret > 0)
1139faa97c48SAl Viro 		wakeup_pipe_readers(opipe);
1140faa97c48SAl Viro 	return ret;
1141faa97c48SAl Viro }
1142faa97c48SAl Viro 
1143ddac0d39SJens Axboe /*
114483f9135bSJens Axboe  * Determine where to splice to/from.
114583f9135bSJens Axboe  */
1146ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1147ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
11485274f052SJens Axboe {
11497c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11507c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11517995bd28SAl Viro 	loff_t offset;
1152a4514ebdSJens Axboe 	long ret;
11535274f052SJens Axboe 
115490da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
115590da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
115690da2e3fSPavel Begunkov 		return -EBADF;
115790da2e3fSPavel Begunkov 
1158c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1159c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
11607c77f0b3SMiklos Szeredi 
11617c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11627c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11637c77f0b3SMiklos Szeredi 			return -ESPIPE;
11647c77f0b3SMiklos Szeredi 
11657c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11667c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11677c77f0b3SMiklos Szeredi 			return -EINVAL;
11687c77f0b3SMiklos Szeredi 
1169ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1170ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1171ee5e0011SSlavomir Kaslev 
11727c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11737c77f0b3SMiklos Szeredi 	}
11747c77f0b3SMiklos Szeredi 
11757c77f0b3SMiklos Szeredi 	if (ipipe) {
1176529565dcSIngo Molnar 		if (off_in)
1177529565dcSIngo Molnar 			return -ESPIPE;
1178b92ce558SJens Axboe 		if (off_out) {
117919c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1180b92ce558SJens Axboe 				return -EINVAL;
1181ee6e00c8SJens Axboe 			offset = *off_out;
11827995bd28SAl Viro 		} else {
11837995bd28SAl Viro 			offset = out->f_pos;
11847995bd28SAl Viro 		}
1185529565dcSIngo Molnar 
118618c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
118718c67cb9SAl Viro 			return -EINVAL;
118818c67cb9SAl Viro 
118918c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
119018c67cb9SAl Viro 		if (unlikely(ret < 0))
119118c67cb9SAl Viro 			return ret;
119218c67cb9SAl Viro 
1193ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1194ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1195ee5e0011SSlavomir Kaslev 
1196500368f7SAl Viro 		file_start_write(out);
11977995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1198500368f7SAl Viro 		file_end_write(out);
1199a4514ebdSJens Axboe 
1200983652c6SChung-Chiang Cheng 		if (ret > 0)
1201983652c6SChung-Chiang Cheng 			fsnotify_modify(out);
1202983652c6SChung-Chiang Cheng 
12037995bd28SAl Viro 		if (!off_out)
12047995bd28SAl Viro 			out->f_pos = offset;
1205ee6e00c8SJens Axboe 		else
1206ee6e00c8SJens Axboe 			*off_out = offset;
1207a4514ebdSJens Axboe 
1208a4514ebdSJens Axboe 		return ret;
1209529565dcSIngo Molnar 	}
12105274f052SJens Axboe 
12117c77f0b3SMiklos Szeredi 	if (opipe) {
1212529565dcSIngo Molnar 		if (off_out)
1213529565dcSIngo Molnar 			return -ESPIPE;
1214b92ce558SJens Axboe 		if (off_in) {
121519c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1216b92ce558SJens Axboe 				return -EINVAL;
1217ee6e00c8SJens Axboe 			offset = *off_in;
12187995bd28SAl Viro 		} else {
12197995bd28SAl Viro 			offset = in->f_pos;
12207995bd28SAl Viro 		}
1221529565dcSIngo Molnar 
1222ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1223ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1224ee5e0011SSlavomir Kaslev 
1225faa97c48SAl Viro 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1226983652c6SChung-Chiang Cheng 
1227983652c6SChung-Chiang Cheng 		if (ret > 0)
1228983652c6SChung-Chiang Cheng 			fsnotify_access(in);
1229983652c6SChung-Chiang Cheng 
12307995bd28SAl Viro 		if (!off_in)
12317995bd28SAl Viro 			in->f_pos = offset;
1232ee6e00c8SJens Axboe 		else
1233ee6e00c8SJens Axboe 			*off_in = offset;
1234a4514ebdSJens Axboe 
1235a4514ebdSJens Axboe 		return ret;
1236529565dcSIngo Molnar 	}
12375274f052SJens Axboe 
12385274f052SJens Axboe 	return -EINVAL;
12395274f052SJens Axboe }
12405274f052SJens Axboe 
1241ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1242ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1243ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1244ee6e00c8SJens Axboe {
1245ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1246ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1247ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1248ee6e00c8SJens Axboe 	long ret;
1249ee6e00c8SJens Axboe 
1250ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1251ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1252ee6e00c8SJens Axboe 
12530f99fc51SJens Axboe 	if (ipipe) {
12540f99fc51SJens Axboe 		if (off_in)
1255ee6e00c8SJens Axboe 			return -ESPIPE;
12560f99fc51SJens Axboe 		pipe_clear_nowait(in);
12570f99fc51SJens Axboe 	}
12580f99fc51SJens Axboe 	if (opipe) {
12590f99fc51SJens Axboe 		if (off_out)
1260ee6e00c8SJens Axboe 			return -ESPIPE;
12610f99fc51SJens Axboe 		pipe_clear_nowait(out);
12620f99fc51SJens Axboe 	}
1263ee6e00c8SJens Axboe 
1264ee6e00c8SJens Axboe 	if (off_out) {
1265ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1266ee6e00c8SJens Axboe 			return -EFAULT;
1267ee6e00c8SJens Axboe 		__off_out = &offset;
1268ee6e00c8SJens Axboe 	}
1269ee6e00c8SJens Axboe 	if (off_in) {
1270ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1271ee6e00c8SJens Axboe 			return -EFAULT;
1272ee6e00c8SJens Axboe 		__off_in = &offset;
1273ee6e00c8SJens Axboe 	}
1274ee6e00c8SJens Axboe 
1275ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1276ee6e00c8SJens Axboe 	if (ret < 0)
1277ee6e00c8SJens Axboe 		return ret;
1278ee6e00c8SJens Axboe 
1279ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1280ee6e00c8SJens Axboe 		return -EFAULT;
1281ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1282ee6e00c8SJens Axboe 		return -EFAULT;
1283ee6e00c8SJens Axboe 
1284ee6e00c8SJens Axboe 	return ret;
1285ee6e00c8SJens Axboe }
1286ee6e00c8SJens Axboe 
128779fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
128879fddc4eSAl Viro 			struct pipe_inode_info *pipe,
128979fddc4eSAl Viro 			unsigned flags)
1290912d35f8SJens Axboe {
129179fddc4eSAl Viro 	struct pipe_buffer buf = {
129279fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
129379fddc4eSAl Viro 		.flags = flags
129479fddc4eSAl Viro 	};
129579fddc4eSAl Viro 	size_t total = 0;
129679fddc4eSAl Viro 	int ret = 0;
129779fddc4eSAl Viro 
12987d690c15SAl Viro 	while (iov_iter_count(from)) {
129979fddc4eSAl Viro 		struct page *pages[16];
13007d690c15SAl Viro 		ssize_t left;
1301db85a9ebSAl Viro 		size_t start;
13027d690c15SAl Viro 		int i, n;
1303912d35f8SJens Axboe 
13047d690c15SAl Viro 		left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
13057d690c15SAl Viro 		if (left <= 0) {
13067d690c15SAl Viro 			ret = left;
130779fddc4eSAl Viro 			break;
130879fddc4eSAl Viro 		}
1309912d35f8SJens Axboe 
13107d690c15SAl Viro 		n = DIV_ROUND_UP(left + start, PAGE_SIZE);
13117d690c15SAl Viro 		for (i = 0; i < n; i++) {
13127d690c15SAl Viro 			int size = min_t(int, left, PAGE_SIZE - start);
13137d690c15SAl Viro 
13147d690c15SAl Viro 			buf.page = pages[i];
131579fddc4eSAl Viro 			buf.offset = start;
131679fddc4eSAl Viro 			buf.len = size;
131779fddc4eSAl Viro 			ret = add_to_pipe(pipe, &buf);
131879fddc4eSAl Viro 			if (unlikely(ret < 0)) {
13197d690c15SAl Viro 				iov_iter_revert(from, left);
13207d690c15SAl Viro 				// this one got dropped by add_to_pipe()
13217d690c15SAl Viro 				while (++i < n)
13227d690c15SAl Viro 					put_page(pages[i]);
13237d690c15SAl Viro 				goto out;
13247d690c15SAl Viro 			}
132579fddc4eSAl Viro 			total += ret;
13267d690c15SAl Viro 			left -= size;
13277d690c15SAl Viro 			start = 0;
1328912d35f8SJens Axboe 		}
1329912d35f8SJens Axboe 	}
13307d690c15SAl Viro out:
133179fddc4eSAl Viro 	return total ? total : ret;
1332912d35f8SJens Axboe }
1333912d35f8SJens Axboe 
13346a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
13356a14b90bSJens Axboe 			struct splice_desc *sd)
13366a14b90bSJens Axboe {
13376130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
13386130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
13396a14b90bSJens Axboe }
13406a14b90bSJens Axboe 
13416a14b90bSJens Axboe /*
13426a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
13436a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
13446a14b90bSJens Axboe  */
134587a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
134687a3002aSAl Viro 			     unsigned int flags)
13476a14b90bSJens Axboe {
1348c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
134987a3002aSAl Viro 	struct splice_desc sd = {
135087a3002aSAl Viro 		.total_len = iov_iter_count(iter),
135187a3002aSAl Viro 		.flags = flags,
135287a3002aSAl Viro 		.u.data = iter
135387a3002aSAl Viro 	};
135487a3002aSAl Viro 	long ret = 0;
13556a14b90bSJens Axboe 
13566a14b90bSJens Axboe 	if (!pipe)
13576a14b90bSJens Axboe 		return -EBADF;
13586a14b90bSJens Axboe 
13590f99fc51SJens Axboe 	pipe_clear_nowait(file);
13600f99fc51SJens Axboe 
1361345995faSAl Viro 	if (sd.total_len) {
13626130f531SAl Viro 		pipe_lock(pipe);
13636130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
136461e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1365345995faSAl Viro 	}
13666a14b90bSJens Axboe 
13676a14b90bSJens Axboe 	return ret;
13686a14b90bSJens Axboe }
13696a14b90bSJens Axboe 
1370912d35f8SJens Axboe /*
1371912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1372912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1373912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1374912d35f8SJens Axboe  */
137587a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
137687a3002aSAl Viro 			     unsigned int flags)
1377912d35f8SJens Axboe {
1378ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
137987a3002aSAl Viro 	long ret = 0;
138079fddc4eSAl Viro 	unsigned buf_flag = 0;
138179fddc4eSAl Viro 
138279fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
138379fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1384912d35f8SJens Axboe 
1385c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1386ddac0d39SJens Axboe 	if (!pipe)
1387912d35f8SJens Axboe 		return -EBADF;
1388912d35f8SJens Axboe 
13890f99fc51SJens Axboe 	pipe_clear_nowait(file);
13900f99fc51SJens Axboe 
13918924feffSAl Viro 	pipe_lock(pipe);
13928924feffSAl Viro 	ret = wait_for_space(pipe, flags);
139379fddc4eSAl Viro 	if (!ret)
139487a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13958924feffSAl Viro 	pipe_unlock(pipe);
13968924feffSAl Viro 	if (ret > 0)
13978924feffSAl Viro 		wakeup_pipe_readers(pipe);
139835f3d14dSJens Axboe 	return ret;
1399912d35f8SJens Axboe }
1400912d35f8SJens Axboe 
140187a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
140287a3002aSAl Viro {
140387a3002aSAl Viro 	if (!f.file)
140487a3002aSAl Viro 		return -EBADF;
140587a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
1406de4eda9dSAl Viro 		*type = ITER_SOURCE;
140787a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
1408de4eda9dSAl Viro 		*type = ITER_DEST;
140987a3002aSAl Viro 	} else {
141087a3002aSAl Viro 		fdput(f);
141187a3002aSAl Viro 		return -EBADF;
141287a3002aSAl Viro 	}
141387a3002aSAl Viro 	return 0;
141487a3002aSAl Viro }
141587a3002aSAl Viro 
14166a14b90bSJens Axboe /*
14176a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
14186a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
14196a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
14206a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
14216a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
14226a14b90bSJens Axboe  * solutions for that:
14236a14b90bSJens Axboe  *
14246a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
14256a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
14266a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
14276a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
14286a14b90bSJens Axboe  *
14296a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
14306a14b90bSJens Axboe  *
14316a14b90bSJens Axboe  */
143287a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
143330cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
143430cfe4efSDominik Brodowski {
143587a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
143687a3002aSAl Viro 	struct iovec *iov = iovstack;
143787a3002aSAl Viro 	struct iov_iter iter;
143887e5e6daSJens Axboe 	ssize_t error;
143987a3002aSAl Viro 	struct fd f;
144087a3002aSAl Viro 	int type;
144187a3002aSAl Viro 
1442598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1443598b3cecSChristoph Hellwig 		return -EINVAL;
1444598b3cecSChristoph Hellwig 
144587a3002aSAl Viro 	f = fdget(fd);
144687a3002aSAl Viro 	error = vmsplice_type(f, &type);
144787a3002aSAl Viro 	if (error)
144887a3002aSAl Viro 		return error;
144987a3002aSAl Viro 
145087a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
145187a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1452598b3cecSChristoph Hellwig 	if (error < 0)
1453598b3cecSChristoph Hellwig 		goto out_fdput;
1454598b3cecSChristoph Hellwig 
1455598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1456598b3cecSChristoph Hellwig 		error = 0;
1457de4eda9dSAl Viro 	else if (type == ITER_SOURCE)
1458598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1459598b3cecSChristoph Hellwig 	else
1460598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1461598b3cecSChristoph Hellwig 
146287a3002aSAl Viro 	kfree(iov);
1463598b3cecSChristoph Hellwig out_fdput:
146487a3002aSAl Viro 	fdput(f);
146587a3002aSAl Viro 	return error;
146630cfe4efSDominik Brodowski }
146730cfe4efSDominik Brodowski 
1468836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1469836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1470836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14715274f052SJens Axboe {
14722903ff01SAl Viro 	struct fd in, out;
14735274f052SJens Axboe 	long error;
14745274f052SJens Axboe 
14755274f052SJens Axboe 	if (unlikely(!len))
14765274f052SJens Axboe 		return 0;
14775274f052SJens Axboe 
14783d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14793d6ea290SAl Viro 		return -EINVAL;
14803d6ea290SAl Viro 
14815274f052SJens Axboe 	error = -EBADF;
14822903ff01SAl Viro 	in = fdget(fd_in);
14832903ff01SAl Viro 	if (in.file) {
14842903ff01SAl Viro 		out = fdget(fd_out);
14852903ff01SAl Viro 		if (out.file) {
1486ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1487529565dcSIngo Molnar 						len, flags);
14882903ff01SAl Viro 			fdput(out);
14895274f052SJens Axboe 		}
14902903ff01SAl Viro 		fdput(in);
14915274f052SJens Axboe 	}
14925274f052SJens Axboe 	return error;
14935274f052SJens Axboe }
149470524490SJens Axboe 
149570524490SJens Axboe /*
1496aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1497aadd06e5SJens Axboe  * return an appropriate error.
1498aadd06e5SJens Axboe  */
14997c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1500aadd06e5SJens Axboe {
1501aadd06e5SJens Axboe 	int ret;
1502aadd06e5SJens Axboe 
1503aadd06e5SJens Axboe 	/*
15048cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1505aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1506aadd06e5SJens Axboe 	 */
15078cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1508aadd06e5SJens Axboe 		return 0;
1509aadd06e5SJens Axboe 
1510aadd06e5SJens Axboe 	ret = 0;
151161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1512aadd06e5SJens Axboe 
15138cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1514aadd06e5SJens Axboe 		if (signal_pending(current)) {
1515aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1516aadd06e5SJens Axboe 			break;
1517aadd06e5SJens Axboe 		}
1518aadd06e5SJens Axboe 		if (!pipe->writers)
1519aadd06e5SJens Axboe 			break;
1520aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1521aadd06e5SJens Axboe 			ret = -EAGAIN;
1522aadd06e5SJens Axboe 			break;
1523aadd06e5SJens Axboe 		}
1524472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1525aadd06e5SJens Axboe 	}
1526aadd06e5SJens Axboe 
152761e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1528aadd06e5SJens Axboe 	return ret;
1529aadd06e5SJens Axboe }
1530aadd06e5SJens Axboe 
1531aadd06e5SJens Axboe /*
1532aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1533aadd06e5SJens Axboe  * return an appropriate error.
1534aadd06e5SJens Axboe  */
15357c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1536aadd06e5SJens Axboe {
1537aadd06e5SJens Axboe 	int ret;
1538aadd06e5SJens Axboe 
1539aadd06e5SJens Axboe 	/*
15408cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1541aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1542aadd06e5SJens Axboe 	 */
1543566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1544aadd06e5SJens Axboe 		return 0;
1545aadd06e5SJens Axboe 
1546aadd06e5SJens Axboe 	ret = 0;
154761e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1548aadd06e5SJens Axboe 
15496718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1550aadd06e5SJens Axboe 		if (!pipe->readers) {
1551aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1552aadd06e5SJens Axboe 			ret = -EPIPE;
1553aadd06e5SJens Axboe 			break;
1554aadd06e5SJens Axboe 		}
1555aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1556aadd06e5SJens Axboe 			ret = -EAGAIN;
1557aadd06e5SJens Axboe 			break;
1558aadd06e5SJens Axboe 		}
1559aadd06e5SJens Axboe 		if (signal_pending(current)) {
1560aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1561aadd06e5SJens Axboe 			break;
1562aadd06e5SJens Axboe 		}
1563472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1564aadd06e5SJens Axboe 	}
1565aadd06e5SJens Axboe 
156661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1567aadd06e5SJens Axboe 	return ret;
1568aadd06e5SJens Axboe }
1569aadd06e5SJens Axboe 
1570aadd06e5SJens Axboe /*
15717c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15727c77f0b3SMiklos Szeredi  */
15737c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15747c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15757c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15767c77f0b3SMiklos Szeredi {
15777c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15788cefc107SDavid Howells 	unsigned int i_head, o_head;
15798cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15808cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15818cefc107SDavid Howells 	int ret = 0;
15827c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15837c77f0b3SMiklos Szeredi 
15847c77f0b3SMiklos Szeredi 
15857c77f0b3SMiklos Szeredi retry:
15867c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15877c77f0b3SMiklos Szeredi 	if (ret)
15887c77f0b3SMiklos Szeredi 		return ret;
15897c77f0b3SMiklos Szeredi 
15907c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15917c77f0b3SMiklos Szeredi 	if (ret)
15927c77f0b3SMiklos Szeredi 		return ret;
15937c77f0b3SMiklos Szeredi 
15947c77f0b3SMiklos Szeredi 	/*
15957c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15967c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15977c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15987c77f0b3SMiklos Szeredi 	 */
15997c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
16007c77f0b3SMiklos Szeredi 
16018cefc107SDavid Howells 	i_tail = ipipe->tail;
16028cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
16038cefc107SDavid Howells 	o_head = opipe->head;
16048cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
16058cefc107SDavid Howells 
16067c77f0b3SMiklos Szeredi 	do {
16078cefc107SDavid Howells 		size_t o_len;
16088cefc107SDavid Howells 
16097c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
16107c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
16117c77f0b3SMiklos Szeredi 			if (!ret)
16127c77f0b3SMiklos Szeredi 				ret = -EPIPE;
16137c77f0b3SMiklos Szeredi 			break;
16147c77f0b3SMiklos Szeredi 		}
16157c77f0b3SMiklos Szeredi 
16168cefc107SDavid Howells 		i_head = ipipe->head;
16178cefc107SDavid Howells 		o_tail = opipe->tail;
16188cefc107SDavid Howells 
16198cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
16207c77f0b3SMiklos Szeredi 			break;
16217c77f0b3SMiklos Szeredi 
16227c77f0b3SMiklos Szeredi 		/*
16237c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
16247c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
16257c77f0b3SMiklos Szeredi 		 */
16268cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
16276718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
16287c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
16297c77f0b3SMiklos Szeredi 			if (ret)
16307c77f0b3SMiklos Szeredi 				break;
16317c77f0b3SMiklos Szeredi 
16327c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
16337c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
16347c77f0b3SMiklos Szeredi 				break;
16357c77f0b3SMiklos Szeredi 			}
16367c77f0b3SMiklos Szeredi 
16377c77f0b3SMiklos Szeredi 			/*
16387c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
16397c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
16407c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
16417c77f0b3SMiklos Szeredi 			 */
16427c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
16437c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
16447c77f0b3SMiklos Szeredi 			goto retry;
16457c77f0b3SMiklos Szeredi 		}
16467c77f0b3SMiklos Szeredi 
16478cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16488cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
16497c77f0b3SMiklos Szeredi 
16507c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
16517c77f0b3SMiklos Szeredi 			/*
16527c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
16537c77f0b3SMiklos Szeredi 			 */
16547c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16557c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
16568cefc107SDavid Howells 			i_tail++;
16578cefc107SDavid Howells 			ipipe->tail = i_tail;
16587c77f0b3SMiklos Szeredi 			input_wakeup = true;
16598cefc107SDavid Howells 			o_len = obuf->len;
16608cefc107SDavid Howells 			o_head++;
16618cefc107SDavid Howells 			opipe->head = o_head;
16627c77f0b3SMiklos Szeredi 		} else {
16637c77f0b3SMiklos Szeredi 			/*
16647c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16657c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16667c77f0b3SMiklos Szeredi 			 */
166715fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
166815fab63eSMatthew Wilcox 				if (ret == 0)
166915fab63eSMatthew Wilcox 					ret = -EFAULT;
167015fab63eSMatthew Wilcox 				break;
167115fab63eSMatthew Wilcox 			}
16727c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16737c77f0b3SMiklos Szeredi 
16747c77f0b3SMiklos Szeredi 			/*
1675f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
16767c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16777c77f0b3SMiklos Szeredi 			 */
16787c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1679f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1680a0ce2f0aSJann Horn 
16817c77f0b3SMiklos Szeredi 			obuf->len = len;
16828cefc107SDavid Howells 			ibuf->offset += len;
16838cefc107SDavid Howells 			ibuf->len -= len;
16848cefc107SDavid Howells 			o_len = len;
16858cefc107SDavid Howells 			o_head++;
16868cefc107SDavid Howells 			opipe->head = o_head;
16877c77f0b3SMiklos Szeredi 		}
16888cefc107SDavid Howells 		ret += o_len;
16898cefc107SDavid Howells 		len -= o_len;
16907c77f0b3SMiklos Szeredi 	} while (len);
16917c77f0b3SMiklos Szeredi 
16927c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16937c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16947c77f0b3SMiklos Szeredi 
16957c77f0b3SMiklos Szeredi 	/*
16967c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16977c77f0b3SMiklos Szeredi 	 */
1698825cdcb1SNamhyung Kim 	if (ret > 0)
1699825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1700825cdcb1SNamhyung Kim 
17017c77f0b3SMiklos Szeredi 	if (input_wakeup)
17027c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
17037c77f0b3SMiklos Szeredi 
17047c77f0b3SMiklos Szeredi 	return ret;
17057c77f0b3SMiklos Szeredi }
17067c77f0b3SMiklos Szeredi 
17077c77f0b3SMiklos Szeredi /*
170870524490SJens Axboe  * Link contents of ipipe to opipe.
170970524490SJens Axboe  */
171070524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
171170524490SJens Axboe 		     struct pipe_inode_info *opipe,
171270524490SJens Axboe 		     size_t len, unsigned int flags)
171370524490SJens Axboe {
171470524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
17158cefc107SDavid Howells 	unsigned int i_head, o_head;
17168cefc107SDavid Howells 	unsigned int i_tail, o_tail;
17178cefc107SDavid Howells 	unsigned int i_mask, o_mask;
17188cefc107SDavid Howells 	int ret = 0;
171970524490SJens Axboe 
172070524490SJens Axboe 	/*
172170524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
172261e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
172370524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
172470524490SJens Axboe 	 */
172561e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
172670524490SJens Axboe 
17278cefc107SDavid Howells 	i_tail = ipipe->tail;
17288cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
17298cefc107SDavid Howells 	o_head = opipe->head;
17308cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
17318cefc107SDavid Howells 
1732aadd06e5SJens Axboe 	do {
173370524490SJens Axboe 		if (!opipe->readers) {
173470524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
173570524490SJens Axboe 			if (!ret)
173670524490SJens Axboe 				ret = -EPIPE;
173770524490SJens Axboe 			break;
173870524490SJens Axboe 		}
173970524490SJens Axboe 
17408cefc107SDavid Howells 		i_head = ipipe->head;
17418cefc107SDavid Howells 		o_tail = opipe->tail;
17428cefc107SDavid Howells 
174370524490SJens Axboe 		/*
17448cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1745aadd06e5SJens Axboe 		 * output room, break.
174670524490SJens Axboe 		 */
17478cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17486718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1749aadd06e5SJens Axboe 			break;
1750aadd06e5SJens Axboe 
17518cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17528cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
175370524490SJens Axboe 
175470524490SJens Axboe 		/*
175570524490SJens Axboe 		 * Get a reference to this pipe buffer,
175670524490SJens Axboe 		 * so we can copy the contents over.
175770524490SJens Axboe 		 */
175815fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
175915fab63eSMatthew Wilcox 			if (ret == 0)
176015fab63eSMatthew Wilcox 				ret = -EFAULT;
176115fab63eSMatthew Wilcox 			break;
176215fab63eSMatthew Wilcox 		}
176370524490SJens Axboe 
176470524490SJens Axboe 		*obuf = *ibuf;
176570524490SJens Axboe 
17667afa6fd0SJens Axboe 		/*
1767f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1768f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
17697afa6fd0SJens Axboe 		 */
17707afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1771f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1772a0ce2f0aSJann Horn 
177370524490SJens Axboe 		if (obuf->len > len)
177470524490SJens Axboe 			obuf->len = len;
177570524490SJens Axboe 		ret += obuf->len;
177670524490SJens Axboe 		len -= obuf->len;
17778cefc107SDavid Howells 
17788cefc107SDavid Howells 		o_head++;
17798cefc107SDavid Howells 		opipe->head = o_head;
17808cefc107SDavid Howells 		i_tail++;
1781aadd06e5SJens Axboe 	} while (len);
178270524490SJens Axboe 
178361e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
178461e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
178570524490SJens Axboe 
1786aadd06e5SJens Axboe 	/*
1787aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1788aadd06e5SJens Axboe 	 */
1789825cdcb1SNamhyung Kim 	if (ret > 0)
1790825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
179170524490SJens Axboe 
179270524490SJens Axboe 	return ret;
179370524490SJens Axboe }
179470524490SJens Axboe 
179570524490SJens Axboe /*
179670524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
179770524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
179870524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
179970524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
180070524490SJens Axboe  */
18019dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
180270524490SJens Axboe {
1803c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1804c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1805aadd06e5SJens Axboe 	int ret = -EINVAL;
180670524490SJens Axboe 
180790da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
180890da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
180990da2e3fSPavel Begunkov 		return -EBADF;
181090da2e3fSPavel Begunkov 
181170524490SJens Axboe 	/*
1812aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1813aadd06e5SJens Axboe 	 * copying the data.
181470524490SJens Axboe 	 */
1815aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1816ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1817ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1818ee5e0011SSlavomir Kaslev 
1819aadd06e5SJens Axboe 		/*
1820aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1821aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1822aadd06e5SJens Axboe 		 */
18237c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1824aadd06e5SJens Axboe 		if (!ret) {
18257c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
182602cf01aeSJens Axboe 			if (!ret)
1827aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1828aadd06e5SJens Axboe 		}
1829aadd06e5SJens Axboe 	}
183070524490SJens Axboe 
1831aadd06e5SJens Axboe 	return ret;
183270524490SJens Axboe }
183370524490SJens Axboe 
1834836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
183570524490SJens Axboe {
183690da2e3fSPavel Begunkov 	struct fd in, out;
18372903ff01SAl Viro 	int error;
183870524490SJens Axboe 
18393d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
18403d6ea290SAl Viro 		return -EINVAL;
18413d6ea290SAl Viro 
184270524490SJens Axboe 	if (unlikely(!len))
184370524490SJens Axboe 		return 0;
184470524490SJens Axboe 
184570524490SJens Axboe 	error = -EBADF;
18462903ff01SAl Viro 	in = fdget(fdin);
18472903ff01SAl Viro 	if (in.file) {
184890da2e3fSPavel Begunkov 		out = fdget(fdout);
18492903ff01SAl Viro 		if (out.file) {
185090da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
18512903ff01SAl Viro 			fdput(out);
185270524490SJens Axboe 		}
18532903ff01SAl Viro  		fdput(in);
185470524490SJens Axboe  	}
185570524490SJens Axboe 
185670524490SJens Axboe 	return error;
185770524490SJens Axboe }
1858