xref: /openbmc/linux/fs/splice.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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>
362dc334f1SDavid Howells #include <linux/net.h>
3735f9c09fSEric Dumazet #include <linux/socket.h>
38174cd4b1SIngo Molnar #include <linux/sched/signal.h>
39174cd4b1SIngo Molnar 
4006ae43f3SAl Viro #include "internal.h"
415274f052SJens Axboe 
4283f9135bSJens Axboe /*
430f99fc51SJens Axboe  * Splice doesn't support FMODE_NOWAIT. Since pipes may set this flag to
440f99fc51SJens Axboe  * indicate they support non-blocking reads or writes, we must clear it
450f99fc51SJens Axboe  * here if set to avoid blocking other users of this pipe if splice is
460f99fc51SJens Axboe  * being done on it.
470f99fc51SJens Axboe  */
pipe_clear_nowait(struct file * file)480f99fc51SJens Axboe static noinline void noinline pipe_clear_nowait(struct file *file)
490f99fc51SJens Axboe {
500f99fc51SJens Axboe 	fmode_t fmode = READ_ONCE(file->f_mode);
510f99fc51SJens Axboe 
520f99fc51SJens Axboe 	do {
530f99fc51SJens Axboe 		if (!(fmode & FMODE_NOWAIT))
540f99fc51SJens Axboe 			break;
550f99fc51SJens Axboe 	} while (!try_cmpxchg(&file->f_mode, &fmode, fmode & ~FMODE_NOWAIT));
560f99fc51SJens Axboe }
570f99fc51SJens Axboe 
580f99fc51SJens Axboe /*
5983f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
6083f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
6183f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
6283f9135bSJens Axboe  * attempt to reuse this page for another destination.
6383f9135bSJens Axboe  */
page_cache_pipe_buf_try_steal(struct pipe_inode_info * pipe,struct pipe_buffer * buf)64c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
655abc97aaSJens Axboe 		struct pipe_buffer *buf)
665abc97aaSJens Axboe {
675100da38SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(buf->page);
689e94cd4fSJens Axboe 	struct address_space *mapping;
695abc97aaSJens Axboe 
70b9ccad2eSMatthew Wilcox (Oracle) 	folio_lock(folio);
719e0267c2SJens Axboe 
72b9ccad2eSMatthew Wilcox (Oracle) 	mapping = folio_mapping(folio);
739e94cd4fSJens Axboe 	if (mapping) {
74b9ccad2eSMatthew Wilcox (Oracle) 		WARN_ON(!folio_test_uptodate(folio));
755abc97aaSJens Axboe 
76ad8d6f0aSJens Axboe 		/*
779e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
78b9ccad2eSMatthew Wilcox (Oracle) 		 * writeback completing on this folio, since we'll remove it
799e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
80b9ccad2eSMatthew Wilcox (Oracle) 		 * folio, allowing the disk blocks to be reused by someone else
819e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
829e94cd4fSJens Axboe 		 * ensues.
83ad8d6f0aSJens Axboe 		 */
84b9ccad2eSMatthew Wilcox (Oracle) 		folio_wait_writeback(folio);
85ad8d6f0aSJens Axboe 
860201ebf2SDavid Howells 		if (!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 
page_cache_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)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  */
page_cache_pipe_buf_confirm(struct pipe_inode_info * pipe,struct pipe_buffer * buf)119cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1205274f052SJens Axboe 				       struct pipe_buffer *buf)
1215274f052SJens Axboe {
122*781ca602SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(buf->page);
12349d0b21bSJens Axboe 	int err;
1245274f052SJens Axboe 
125*781ca602SMatthew Wilcox (Oracle) 	if (!folio_test_uptodate(folio)) {
126*781ca602SMatthew Wilcox (Oracle) 		folio_lock(folio);
1275274f052SJens Axboe 
12849d0b21bSJens Axboe 		/*
129*781ca602SMatthew Wilcox (Oracle) 		 * Folio got truncated/unhashed. This will cause a 0-byte
13073d62d83SIngo Molnar 		 * splice, if this is the first page.
13149d0b21bSJens Axboe 		 */
132*781ca602SMatthew Wilcox (Oracle) 		if (!folio->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 		 */
140*781ca602SMatthew Wilcox (Oracle) 		if (!folio_test_uptodate(folio)) {
14149d0b21bSJens Axboe 			err = -EIO;
14249d0b21bSJens Axboe 			goto error;
14349d0b21bSJens Axboe 		}
14449d0b21bSJens Axboe 
145*781ca602SMatthew Wilcox (Oracle) 		/* Folio is ok after all, we are done */
146*781ca602SMatthew Wilcox (Oracle) 		folio_unlock(folio);
14749d0b21bSJens Axboe 	}
14849d0b21bSJens Axboe 
149f84d7519SJens Axboe 	return 0;
15049d0b21bSJens Axboe error:
151*781ca602SMatthew Wilcox (Oracle) 	folio_unlock(folio);
152f84d7519SJens Axboe 	return err;
15370524490SJens Axboe }
15470524490SJens Axboe 
155708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
156cac36bb0SJens Axboe 	.confirm	= page_cache_pipe_buf_confirm,
1575274f052SJens Axboe 	.release	= page_cache_pipe_buf_release,
158c928f642SChristoph Hellwig 	.try_steal	= page_cache_pipe_buf_try_steal,
159f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
1605274f052SJens Axboe };
1615274f052SJens Axboe 
user_page_pipe_buf_try_steal(struct pipe_inode_info * pipe,struct pipe_buffer * buf)162c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
163912d35f8SJens Axboe 		struct pipe_buffer *buf)
164912d35f8SJens Axboe {
1657afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
166c928f642SChristoph Hellwig 		return false;
1677afa6fd0SJens Axboe 
1681432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
169c928f642SChristoph Hellwig 	return generic_pipe_buf_try_steal(pipe, buf);
170912d35f8SJens Axboe }
171912d35f8SJens Axboe 
172d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
173912d35f8SJens Axboe 	.release	= page_cache_pipe_buf_release,
174c928f642SChristoph Hellwig 	.try_steal	= user_page_pipe_buf_try_steal,
175f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
176912d35f8SJens Axboe };
177912d35f8SJens Axboe 
wakeup_pipe_readers(struct pipe_inode_info * pipe)178825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
179825cdcb1SNamhyung Kim {
180825cdcb1SNamhyung Kim 	smp_mb();
1810ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1820ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_wait);
183825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
184825cdcb1SNamhyung Kim }
185825cdcb1SNamhyung Kim 
186932cc6d4SJens Axboe /**
187932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
188932cc6d4SJens Axboe  * @pipe:	pipe to fill
189932cc6d4SJens Axboe  * @spd:	data to fill
190932cc6d4SJens Axboe  *
191932cc6d4SJens Axboe  * Description:
19279685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
193932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
194932cc6d4SJens Axboe  *    function will link that data to the pipe.
195932cc6d4SJens Axboe  *
19683f9135bSJens Axboe  */
splice_to_pipe(struct pipe_inode_info * pipe,struct splice_pipe_desc * spd)197d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
198912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1995274f052SJens Axboe {
20000de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
2018cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2028cefc107SDavid Howells 	unsigned int head = pipe->head;
2038cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
2048924feffSAl Viro 	int ret = 0, page_nr = 0;
2055274f052SJens Axboe 
206d6785d91SRabin Vincent 	if (!spd_pages)
207d6785d91SRabin Vincent 		return 0;
208d6785d91SRabin Vincent 
2098924feffSAl Viro 	if (unlikely(!pipe->readers)) {
2105274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
2115274f052SJens Axboe 		ret = -EPIPE;
2128924feffSAl Viro 		goto out;
2135274f052SJens Axboe 	}
2145274f052SJens Axboe 
2156718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2168cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
2175274f052SJens Axboe 
218912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
219912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
220912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
221497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
222912d35f8SJens Axboe 		buf->ops = spd->ops;
2235a81e6a1SMiklos Szeredi 		buf->flags = 0;
2247afa6fd0SJens Axboe 
2258cefc107SDavid Howells 		head++;
2268cefc107SDavid Howells 		pipe->head = head;
227912d35f8SJens Axboe 		page_nr++;
228912d35f8SJens Axboe 		ret += buf->len;
229912d35f8SJens Axboe 
230912d35f8SJens Axboe 		if (!--spd->nr_pages)
2315274f052SJens Axboe 			break;
2325274f052SJens Axboe 	}
2335274f052SJens Axboe 
23429e35094SLinus Torvalds 	if (!ret)
23529e35094SLinus Torvalds 		ret = -EAGAIN;
23629e35094SLinus Torvalds 
2378924feffSAl Viro out:
23800de00bdSJens Axboe 	while (page_nr < spd_pages)
239bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2405274f052SJens Axboe 
2415274f052SJens Axboe 	return ret;
2425274f052SJens Axboe }
2432b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2445274f052SJens Axboe 
add_to_pipe(struct pipe_inode_info * pipe,struct pipe_buffer * buf)24579fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
24679fddc4eSAl Viro {
2478cefc107SDavid Howells 	unsigned int head = pipe->head;
2488cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2498cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
25079fddc4eSAl Viro 	int ret;
25179fddc4eSAl Viro 
25279fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
25379fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
25479fddc4eSAl Viro 		ret = -EPIPE;
2556718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
25679fddc4eSAl Viro 		ret = -EAGAIN;
25779fddc4eSAl Viro 	} else {
2588cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2598cefc107SDavid Howells 		pipe->head = head + 1;
26079fddc4eSAl Viro 		return buf->len;
26179fddc4eSAl Viro 	}
262a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
26379fddc4eSAl Viro 	return ret;
26479fddc4eSAl Viro }
26579fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
26679fddc4eSAl Viro 
26735f3d14dSJens Axboe /*
26835f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
26935f3d14dSJens Axboe  * descriptions.
27035f3d14dSJens Axboe  */
splice_grow_spd(const struct pipe_inode_info * pipe,struct splice_pipe_desc * spd)271047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
27235f3d14dSJens Axboe {
2736718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
274047fe360SEric Dumazet 
2758cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2768cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
27735f3d14dSJens Axboe 		return 0;
27835f3d14dSJens Axboe 
2798cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2808cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2816da2ec56SKees Cook 				     GFP_KERNEL);
28235f3d14dSJens Axboe 
28335f3d14dSJens Axboe 	if (spd->pages && spd->partial)
28435f3d14dSJens Axboe 		return 0;
28535f3d14dSJens Axboe 
28635f3d14dSJens Axboe 	kfree(spd->pages);
28735f3d14dSJens Axboe 	kfree(spd->partial);
28835f3d14dSJens Axboe 	return -ENOMEM;
28935f3d14dSJens Axboe }
29035f3d14dSJens Axboe 
splice_shrink_spd(struct splice_pipe_desc * spd)291047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
29235f3d14dSJens Axboe {
293047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
29435f3d14dSJens Axboe 		return;
29535f3d14dSJens Axboe 
29635f3d14dSJens Axboe 	kfree(spd->pages);
29735f3d14dSJens Axboe 	kfree(spd->partial);
29835f3d14dSJens Axboe }
29935f3d14dSJens Axboe 
3009eee8bd8SDavid Howells /**
3019eee8bd8SDavid Howells  * copy_splice_read -  Copy data from a file and splice the copy into a pipe
3029eee8bd8SDavid Howells  * @in: The file to read from
3039eee8bd8SDavid Howells  * @ppos: Pointer to the file position to read from
3049eee8bd8SDavid Howells  * @pipe: The pipe to splice into
3059eee8bd8SDavid Howells  * @len: The amount to splice
3069eee8bd8SDavid Howells  * @flags: The SPLICE_F_* flags
3079eee8bd8SDavid Howells  *
3089eee8bd8SDavid Howells  * This function allocates a bunch of pages sufficient to hold the requested
3099eee8bd8SDavid Howells  * amount of data (but limited by the remaining pipe capacity), passes it to
3109eee8bd8SDavid Howells  * the file's ->read_iter() to read into and then splices the used pages into
3119eee8bd8SDavid Howells  * the pipe.
3129eee8bd8SDavid Howells  *
3139eee8bd8SDavid Howells  * Return: On success, the number of bytes read will be returned and *@ppos
3149eee8bd8SDavid Howells  * will be updated if appropriate; 0 will be returned if there is no more data
3159eee8bd8SDavid Howells  * to be read; -EAGAIN will be returned if the pipe had no space, and some
3169eee8bd8SDavid Howells  * other negative error code will be returned on error.  A short read may occur
3179eee8bd8SDavid Howells  * if the pipe has insufficient space, we reach the end of the data or we hit a
3189eee8bd8SDavid Howells  * hole.
31933b3b041SDavid Howells  */
copy_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)32069df79a4SDavid Howells ssize_t copy_splice_read(struct file *in, loff_t *ppos,
32133b3b041SDavid Howells 			 struct pipe_inode_info *pipe,
32233b3b041SDavid Howells 			 size_t len, unsigned int flags)
32333b3b041SDavid Howells {
32433b3b041SDavid Howells 	struct iov_iter to;
32533b3b041SDavid Howells 	struct bio_vec *bv;
32633b3b041SDavid Howells 	struct kiocb kiocb;
32733b3b041SDavid Howells 	struct page **pages;
32833b3b041SDavid Howells 	ssize_t ret;
329e69f37bcSDavid Howells 	size_t used, npages, chunk, remain, keep = 0;
33033b3b041SDavid Howells 	int i;
33133b3b041SDavid Howells 
33233b3b041SDavid Howells 	/* Work out how much data we can actually add into the pipe */
33333b3b041SDavid Howells 	used = pipe_occupancy(pipe->head, pipe->tail);
33433b3b041SDavid Howells 	npages = max_t(ssize_t, pipe->max_usage - used, 0);
33533b3b041SDavid Howells 	len = min_t(size_t, len, npages * PAGE_SIZE);
33633b3b041SDavid Howells 	npages = DIV_ROUND_UP(len, PAGE_SIZE);
33733b3b041SDavid Howells 
33833b3b041SDavid Howells 	bv = kzalloc(array_size(npages, sizeof(bv[0])) +
33933b3b041SDavid Howells 		     array_size(npages, sizeof(struct page *)), GFP_KERNEL);
34033b3b041SDavid Howells 	if (!bv)
34133b3b041SDavid Howells 		return -ENOMEM;
34233b3b041SDavid Howells 
343e69f37bcSDavid Howells 	pages = (struct page **)(bv + npages);
34433b3b041SDavid Howells 	npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
34533b3b041SDavid Howells 	if (!npages) {
34633b3b041SDavid Howells 		kfree(bv);
34733b3b041SDavid Howells 		return -ENOMEM;
34833b3b041SDavid Howells 	}
34933b3b041SDavid Howells 
35033b3b041SDavid Howells 	remain = len = min_t(size_t, len, npages * PAGE_SIZE);
35133b3b041SDavid Howells 
35233b3b041SDavid Howells 	for (i = 0; i < npages; i++) {
35333b3b041SDavid Howells 		chunk = min_t(size_t, PAGE_SIZE, remain);
35433b3b041SDavid Howells 		bv[i].bv_page = pages[i];
35533b3b041SDavid Howells 		bv[i].bv_offset = 0;
35633b3b041SDavid Howells 		bv[i].bv_len = chunk;
35733b3b041SDavid Howells 		remain -= chunk;
35833b3b041SDavid Howells 	}
35933b3b041SDavid Howells 
36033b3b041SDavid Howells 	/* Do the I/O */
36133b3b041SDavid Howells 	iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
36233b3b041SDavid Howells 	init_sync_kiocb(&kiocb, in);
36333b3b041SDavid Howells 	kiocb.ki_pos = *ppos;
36433b3b041SDavid Howells 	ret = call_read_iter(in, &kiocb, &to);
36533b3b041SDavid Howells 
36633b3b041SDavid Howells 	if (ret > 0) {
367e69f37bcSDavid Howells 		keep = DIV_ROUND_UP(ret, PAGE_SIZE);
36833b3b041SDavid Howells 		*ppos = kiocb.ki_pos;
3692e82f6c3SChristoph Hellwig 	}
3702e82f6c3SChristoph Hellwig 
37133b3b041SDavid Howells 	/*
3722e82f6c3SChristoph Hellwig 	 * Callers of ->splice_read() expect -EAGAIN on "can't put anything in
3732e82f6c3SChristoph Hellwig 	 * there", rather than -EFAULT.
37433b3b041SDavid Howells 	 */
37533b3b041SDavid Howells 	if (ret == -EFAULT)
37633b3b041SDavid Howells 		ret = -EAGAIN;
37733b3b041SDavid Howells 
37833b3b041SDavid Howells 	/* Free any pages that didn't get touched at all. */
379e69f37bcSDavid Howells 	if (keep < npages)
380e69f37bcSDavid Howells 		release_pages(pages + keep, npages - keep);
38133b3b041SDavid Howells 
38233b3b041SDavid Howells 	/* Push the remaining pages into the pipe. */
383e69f37bcSDavid Howells 	remain = ret;
384e69f37bcSDavid Howells 	for (i = 0; i < keep; i++) {
38533b3b041SDavid Howells 		struct pipe_buffer *buf = pipe_head_buf(pipe);
38633b3b041SDavid Howells 
38733b3b041SDavid Howells 		chunk = min_t(size_t, remain, PAGE_SIZE);
38833b3b041SDavid Howells 		*buf = (struct pipe_buffer) {
38933b3b041SDavid Howells 			.ops	= &default_pipe_buf_ops,
39033b3b041SDavid Howells 			.page	= bv[i].bv_page,
39133b3b041SDavid Howells 			.offset	= 0,
39233b3b041SDavid Howells 			.len	= chunk,
39333b3b041SDavid Howells 		};
39433b3b041SDavid Howells 		pipe->head++;
39533b3b041SDavid Howells 		remain -= chunk;
39633b3b041SDavid Howells 	}
39733b3b041SDavid Howells 
39833b3b041SDavid Howells 	kfree(bv);
39933b3b041SDavid Howells 	return ret;
40033b3b041SDavid Howells }
40169df79a4SDavid Howells EXPORT_SYMBOL(copy_splice_read);
40233b3b041SDavid Howells 
403241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
4046818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
405c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
4066818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
4076818173bSMiklos Szeredi };
4086818173bSMiklos Szeredi 
40928a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
41028a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
41128a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
41228a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
41328a625cbSMiklos Szeredi };
41428a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
41528a625cbSMiklos Szeredi 
wakeup_pipe_writers(struct pipe_inode_info * pipe)416b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
417b3c2d2ddSMiklos Szeredi {
418b3c2d2ddSMiklos Szeredi 	smp_mb();
4190ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4200ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
421b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
422b3c2d2ddSMiklos Szeredi }
423b3c2d2ddSMiklos Szeredi 
424b3c2d2ddSMiklos Szeredi /**
425b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
426b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
427b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
428b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
429b3c2d2ddSMiklos Szeredi  *
430b3c2d2ddSMiklos Szeredi  * Description:
431b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
432b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
433b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
434b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
435b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
436b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
437b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
438b3c2d2ddSMiklos Szeredi  *
439b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
440b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
441b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
442b3c2d2ddSMiklos Szeredi  *    destination.
443b3c2d2ddSMiklos Szeredi  */
splice_from_pipe_feed(struct pipe_inode_info * pipe,struct splice_desc * sd,splice_actor * actor)44496f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
445b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
446b3c2d2ddSMiklos Szeredi {
4478cefc107SDavid Howells 	unsigned int head = pipe->head;
4488cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4498cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
450b3c2d2ddSMiklos Szeredi 	int ret;
451b3c2d2ddSMiklos Szeredi 
452ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4538cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
454b3c2d2ddSMiklos Szeredi 
455b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
456b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
457b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
458b3c2d2ddSMiklos Szeredi 
459fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
460a8adbe37SMichaÅ‚ MirosÅ‚aw 		if (unlikely(ret)) {
461b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
462b3c2d2ddSMiklos Szeredi 				ret = 0;
463b3c2d2ddSMiklos Szeredi 			return ret;
464b3c2d2ddSMiklos Szeredi 		}
465a8adbe37SMichaÅ‚ MirosÅ‚aw 
466a8adbe37SMichaÅ‚ MirosÅ‚aw 		ret = actor(pipe, buf, sd);
467a8adbe37SMichaÅ‚ MirosÅ‚aw 		if (ret <= 0)
468a8adbe37SMichaÅ‚ MirosÅ‚aw 			return ret;
469a8adbe37SMichaÅ‚ MirosÅ‚aw 
470b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
471b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
472b3c2d2ddSMiklos Szeredi 
473b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
474b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
475b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
476b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
477b3c2d2ddSMiklos Szeredi 
478b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
479a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
4808cefc107SDavid Howells 			tail++;
4818cefc107SDavid Howells 			pipe->tail = tail;
4826447a3cfSAl Viro 			if (pipe->files)
483b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
484b3c2d2ddSMiklos Szeredi 		}
485b3c2d2ddSMiklos Szeredi 
486b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
487b3c2d2ddSMiklos Szeredi 			return 0;
488b3c2d2ddSMiklos Szeredi 	}
489b3c2d2ddSMiklos Szeredi 
490b3c2d2ddSMiklos Szeredi 	return 1;
491b3c2d2ddSMiklos Szeredi }
492b3c2d2ddSMiklos Szeredi 
493d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
eat_empty_buffer(struct pipe_inode_info * pipe)494d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
495d1a819a2SLinus Torvalds {
496d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
497d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
498d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
499d1a819a2SLinus Torvalds 
500d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
501d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
502d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
503d1a819a2SLinus Torvalds 		return true;
504d1a819a2SLinus Torvalds 	}
505d1a819a2SLinus Torvalds 
506d1a819a2SLinus Torvalds 	return false;
507d1a819a2SLinus Torvalds }
508d1a819a2SLinus Torvalds 
509b3c2d2ddSMiklos Szeredi /**
510b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
511b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
512b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
513b3c2d2ddSMiklos Szeredi  *
514b3c2d2ddSMiklos Szeredi  * Description:
515b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
516b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
517b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
518b3c2d2ddSMiklos Szeredi  */
splice_from_pipe_next(struct pipe_inode_info * pipe,struct splice_desc * sd)51996f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
520b3c2d2ddSMiklos Szeredi {
521c725bfceSJan Kara 	/*
522c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
523c725bfceSJan Kara 	 * always buffers available
524c725bfceSJan Kara 	 */
525c725bfceSJan Kara 	if (signal_pending(current))
526c725bfceSJan Kara 		return -ERESTARTSYS;
527c725bfceSJan Kara 
528d1a819a2SLinus Torvalds repeat:
5298cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
530b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
531b3c2d2ddSMiklos Szeredi 			return 0;
532b3c2d2ddSMiklos Szeredi 
533a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
534b3c2d2ddSMiklos Szeredi 			return 0;
535b3c2d2ddSMiklos Szeredi 
536b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
537b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
538b3c2d2ddSMiklos Szeredi 
539b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
540b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
541b3c2d2ddSMiklos Szeredi 
542b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
543b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
544b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
545b3c2d2ddSMiklos Szeredi 		}
546b3c2d2ddSMiklos Szeredi 
547472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
548b3c2d2ddSMiklos Szeredi 	}
549b3c2d2ddSMiklos Szeredi 
550d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
551d1a819a2SLinus Torvalds 		goto repeat;
552d1a819a2SLinus Torvalds 
553b3c2d2ddSMiklos Szeredi 	return 1;
554b3c2d2ddSMiklos Szeredi }
555b3c2d2ddSMiklos Szeredi 
556b3c2d2ddSMiklos Szeredi /**
557b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
558b80901bbSRandy Dunlap  * @sd:		information about the splice operation
559b3c2d2ddSMiklos Szeredi  *
560b3c2d2ddSMiklos Szeredi  * Description:
561b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
562b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
563b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
564b3c2d2ddSMiklos Szeredi  */
splice_from_pipe_begin(struct splice_desc * sd)56596f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
566b3c2d2ddSMiklos Szeredi {
567b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
568b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
569b3c2d2ddSMiklos Szeredi }
570b3c2d2ddSMiklos Szeredi 
571b3c2d2ddSMiklos Szeredi /**
572b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
573b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
574b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
575b3c2d2ddSMiklos Szeredi  *
576b3c2d2ddSMiklos Szeredi  * Description:
577b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
578b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
579b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
580b3c2d2ddSMiklos Szeredi  */
splice_from_pipe_end(struct pipe_inode_info * pipe,struct splice_desc * sd)58196f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
582b3c2d2ddSMiklos Szeredi {
583b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
584b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
585b3c2d2ddSMiklos Szeredi }
586b3c2d2ddSMiklos Szeredi 
587932cc6d4SJens Axboe /**
588932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
589932cc6d4SJens Axboe  * @pipe:	pipe to splice from
590932cc6d4SJens Axboe  * @sd:		information to @actor
591932cc6d4SJens Axboe  * @actor:	handler that splices the data
592932cc6d4SJens Axboe  *
593932cc6d4SJens Axboe  * Description:
594932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
595932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
5962dc334f1SDavid Howells  *    the desired destination. See pipe_to_file, pipe_to_sendmsg, or
597932cc6d4SJens Axboe  *    pipe_to_user.
598932cc6d4SJens Axboe  *
59983f9135bSJens Axboe  */
__splice_from_pipe(struct pipe_inode_info * pipe,struct splice_desc * sd,splice_actor * actor)600c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
601c66ab6faSJens Axboe 			   splice_actor *actor)
6025274f052SJens Axboe {
603b3c2d2ddSMiklos Szeredi 	int ret;
6045274f052SJens Axboe 
605b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
606b3c2d2ddSMiklos Szeredi 	do {
607c2489e07SJan Kara 		cond_resched();
608b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
609b3c2d2ddSMiklos Szeredi 		if (ret > 0)
610b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
611b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
612b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6135274f052SJens Axboe 
614b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6155274f052SJens Axboe }
61640bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6175274f052SJens Axboe 
618932cc6d4SJens Axboe /**
619932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
620932cc6d4SJens Axboe  * @pipe:	pipe to splice from
621932cc6d4SJens Axboe  * @out:	file to splice to
622932cc6d4SJens Axboe  * @ppos:	position in @out
623932cc6d4SJens Axboe  * @len:	how many bytes to splice
624932cc6d4SJens Axboe  * @flags:	splice modifier flags
625932cc6d4SJens Axboe  * @actor:	handler that splices the data
626932cc6d4SJens Axboe  *
627932cc6d4SJens Axboe  * Description:
6282933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
629932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
630932cc6d4SJens Axboe  *
631932cc6d4SJens Axboe  */
splice_from_pipe(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags,splice_actor * actor)6326da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6336da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6346da61809SMark Fasheh 			 splice_actor *actor)
6356da61809SMark Fasheh {
6366da61809SMark Fasheh 	ssize_t ret;
637c66ab6faSJens Axboe 	struct splice_desc sd = {
638c66ab6faSJens Axboe 		.total_len = len,
639c66ab6faSJens Axboe 		.flags = flags,
640c66ab6faSJens Axboe 		.pos = *ppos,
6416a14b90bSJens Axboe 		.u.file = out,
642c66ab6faSJens Axboe 	};
6436da61809SMark Fasheh 
64461e0d47cSMiklos Szeredi 	pipe_lock(pipe);
645c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
64661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6476da61809SMark Fasheh 
6486da61809SMark Fasheh 	return ret;
6496da61809SMark Fasheh }
6506da61809SMark Fasheh 
6516da61809SMark Fasheh /**
6528d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6538d020765SAl Viro  * @pipe:	pipe info
6548d020765SAl Viro  * @out:	file to write to
6558d020765SAl Viro  * @ppos:	position in @out
6568d020765SAl Viro  * @len:	number of bytes to splice
6578d020765SAl Viro  * @flags:	splice modifier flags
6588d020765SAl Viro  *
6598d020765SAl Viro  * Description:
6608d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6618d020765SAl Viro  *    the given pipe inode to the given file.
6628d020765SAl Viro  *    This one is ->write_iter-based.
6638d020765SAl Viro  *
6648d020765SAl Viro  */
6658d020765SAl Viro ssize_t
iter_file_splice_write(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)6668d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6678d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
6688d020765SAl Viro {
6698d020765SAl Viro 	struct splice_desc sd = {
6708d020765SAl Viro 		.total_len = len,
6718d020765SAl Viro 		.flags = flags,
6728d020765SAl Viro 		.pos = *ppos,
6738d020765SAl Viro 		.u.file = out,
6748d020765SAl Viro 	};
6756718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
6768d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
6778d020765SAl Viro 					GFP_KERNEL);
6788d020765SAl Viro 	ssize_t ret;
6798d020765SAl Viro 
6808d020765SAl Viro 	if (unlikely(!array))
6818d020765SAl Viro 		return -ENOMEM;
6828d020765SAl Viro 
6838d020765SAl Viro 	pipe_lock(pipe);
6848d020765SAl Viro 
6858d020765SAl Viro 	splice_from_pipe_begin(&sd);
6868d020765SAl Viro 	while (sd.total_len) {
6878d020765SAl Viro 		struct iov_iter from;
688ec057595SLinus Torvalds 		unsigned int head, tail, mask;
6898d020765SAl Viro 		size_t left;
6908cefc107SDavid Howells 		int n;
6918d020765SAl Viro 
6928d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
6938d020765SAl Viro 		if (ret <= 0)
6948d020765SAl Viro 			break;
6958d020765SAl Viro 
6966718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
6978d020765SAl Viro 			kfree(array);
6986718b6f8SDavid Howells 			nbufs = pipe->max_usage;
6998d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7008d020765SAl Viro 					GFP_KERNEL);
7018d020765SAl Viro 			if (!array) {
7028d020765SAl Viro 				ret = -ENOMEM;
7038d020765SAl Viro 				break;
7048d020765SAl Viro 			}
7058d020765SAl Viro 		}
7068d020765SAl Viro 
707ec057595SLinus Torvalds 		head = pipe->head;
708ec057595SLinus Torvalds 		tail = pipe->tail;
709ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
710ec057595SLinus Torvalds 
7118d020765SAl Viro 		/* build the vector */
7128d020765SAl Viro 		left = sd.total_len;
7130f1d344fSPavel Begunkov 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
7148cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7158d020765SAl Viro 			size_t this_len = buf->len;
7168d020765SAl Viro 
7170f1d344fSPavel Begunkov 			/* zero-length bvecs are not supported, skip them */
7180f1d344fSPavel Begunkov 			if (!this_len)
7190f1d344fSPavel Begunkov 				continue;
7200f1d344fSPavel Begunkov 			this_len = min(this_len, left);
7218d020765SAl Viro 
722fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7238d020765SAl Viro 			if (unlikely(ret)) {
7248d020765SAl Viro 				if (ret == -ENODATA)
7258d020765SAl Viro 					ret = 0;
7268d020765SAl Viro 				goto done;
7278d020765SAl Viro 			}
7288d020765SAl Viro 
729664e4078SChristoph Hellwig 			bvec_set_page(&array[n], buf->page, this_len,
730664e4078SChristoph Hellwig 				      buf->offset);
7318d020765SAl Viro 			left -= this_len;
7320f1d344fSPavel Begunkov 			n++;
7338d020765SAl Viro 		}
7348d020765SAl Viro 
735de4eda9dSAl Viro 		iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
736abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7378d020765SAl Viro 		if (ret <= 0)
7388d020765SAl Viro 			break;
7398d020765SAl Viro 
7408d020765SAl Viro 		sd.num_spliced += ret;
7418d020765SAl Viro 		sd.total_len -= ret;
742dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7438d020765SAl Viro 
7448d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7458cefc107SDavid Howells 		tail = pipe->tail;
7468d020765SAl Viro 		while (ret) {
7478cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7488d020765SAl Viro 			if (ret >= buf->len) {
7498d020765SAl Viro 				ret -= buf->len;
7508d020765SAl Viro 				buf->len = 0;
751a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7528cefc107SDavid Howells 				tail++;
7538cefc107SDavid Howells 				pipe->tail = tail;
7548d020765SAl Viro 				if (pipe->files)
7558d020765SAl Viro 					sd.need_wakeup = true;
7568d020765SAl Viro 			} else {
7578d020765SAl Viro 				buf->offset += ret;
7588d020765SAl Viro 				buf->len -= ret;
7598d020765SAl Viro 				ret = 0;
7608d020765SAl Viro 			}
7618d020765SAl Viro 		}
7628d020765SAl Viro 	}
7638d020765SAl Viro done:
7648d020765SAl Viro 	kfree(array);
7658d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7668d020765SAl Viro 
7678d020765SAl Viro 	pipe_unlock(pipe);
7688d020765SAl Viro 
7698d020765SAl Viro 	if (sd.num_spliced)
7708d020765SAl Viro 		ret = sd.num_spliced;
7718d020765SAl Viro 
7728d020765SAl Viro 	return ret;
7738d020765SAl Viro }
7748d020765SAl Viro 
7758d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
7768d020765SAl Viro 
7772dc334f1SDavid Howells #ifdef CONFIG_NET
77883f9135bSJens Axboe /**
7792dc334f1SDavid Howells  * splice_to_socket - splice data from a pipe to a socket
780932cc6d4SJens Axboe  * @pipe:	pipe to splice from
78183f9135bSJens Axboe  * @out:	socket to write to
782932cc6d4SJens Axboe  * @ppos:	position in @out
78383f9135bSJens Axboe  * @len:	number of bytes to splice
78483f9135bSJens Axboe  * @flags:	splice modifier flags
78583f9135bSJens Axboe  *
786932cc6d4SJens Axboe  * Description:
78783f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
78883f9135bSJens Axboe  *    is involved.
78983f9135bSJens Axboe  *
79083f9135bSJens Axboe  */
splice_to_socket(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)7912dc334f1SDavid Howells ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
792cbb7e577SJens Axboe 			 loff_t *ppos, size_t len, unsigned int flags)
7935274f052SJens Axboe {
7942dc334f1SDavid Howells 	struct socket *sock = sock_from_file(out);
7952dc334f1SDavid Howells 	struct bio_vec bvec[16];
7962dc334f1SDavid Howells 	struct msghdr msg = {};
7972dc334f1SDavid Howells 	ssize_t ret = 0;
7982dc334f1SDavid Howells 	size_t spliced = 0;
7992dc334f1SDavid Howells 	bool need_wakeup = false;
8002dc334f1SDavid Howells 
8012dc334f1SDavid Howells 	pipe_lock(pipe);
8022dc334f1SDavid Howells 
8032dc334f1SDavid Howells 	while (len > 0) {
8042dc334f1SDavid Howells 		unsigned int head, tail, mask, bc = 0;
8052dc334f1SDavid Howells 		size_t remain = len;
8062dc334f1SDavid Howells 
8072dc334f1SDavid Howells 		/*
8082dc334f1SDavid Howells 		 * Check for signal early to make process killable when there
8092dc334f1SDavid Howells 		 * are always buffers available
8102dc334f1SDavid Howells 		 */
8112dc334f1SDavid Howells 		ret = -ERESTARTSYS;
8122dc334f1SDavid Howells 		if (signal_pending(current))
8132dc334f1SDavid Howells 			break;
8142dc334f1SDavid Howells 
8152dc334f1SDavid Howells 		while (pipe_empty(pipe->head, pipe->tail)) {
8162dc334f1SDavid Howells 			ret = 0;
8172dc334f1SDavid Howells 			if (!pipe->writers)
8182dc334f1SDavid Howells 				goto out;
8192dc334f1SDavid Howells 
8202dc334f1SDavid Howells 			if (spliced)
8212dc334f1SDavid Howells 				goto out;
8222dc334f1SDavid Howells 
8232dc334f1SDavid Howells 			ret = -EAGAIN;
8242dc334f1SDavid Howells 			if (flags & SPLICE_F_NONBLOCK)
8252dc334f1SDavid Howells 				goto out;
8262dc334f1SDavid Howells 
8272dc334f1SDavid Howells 			ret = -ERESTARTSYS;
8282dc334f1SDavid Howells 			if (signal_pending(current))
8292dc334f1SDavid Howells 				goto out;
8302dc334f1SDavid Howells 
8312dc334f1SDavid Howells 			if (need_wakeup) {
8322dc334f1SDavid Howells 				wakeup_pipe_writers(pipe);
8332dc334f1SDavid Howells 				need_wakeup = false;
8345274f052SJens Axboe 			}
8355274f052SJens Axboe 
8362dc334f1SDavid Howells 			pipe_wait_readable(pipe);
8372dc334f1SDavid Howells 		}
8382dc334f1SDavid Howells 
8392dc334f1SDavid Howells 		head = pipe->head;
8402dc334f1SDavid Howells 		tail = pipe->tail;
8412dc334f1SDavid Howells 		mask = pipe->ring_size - 1;
8422dc334f1SDavid Howells 
8432dc334f1SDavid Howells 		while (!pipe_empty(head, tail)) {
8442dc334f1SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
8452dc334f1SDavid Howells 			size_t seg;
8462dc334f1SDavid Howells 
8472dc334f1SDavid Howells 			if (!buf->len) {
8482dc334f1SDavid Howells 				tail++;
8492dc334f1SDavid Howells 				continue;
8502dc334f1SDavid Howells 			}
8512dc334f1SDavid Howells 
8522dc334f1SDavid Howells 			seg = min_t(size_t, remain, buf->len);
8532dc334f1SDavid Howells 
8542dc334f1SDavid Howells 			ret = pipe_buf_confirm(pipe, buf);
8552dc334f1SDavid Howells 			if (unlikely(ret)) {
8562dc334f1SDavid Howells 				if (ret == -ENODATA)
8572dc334f1SDavid Howells 					ret = 0;
8582dc334f1SDavid Howells 				break;
8592dc334f1SDavid Howells 			}
8602dc334f1SDavid Howells 
8612dc334f1SDavid Howells 			bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset);
8622dc334f1SDavid Howells 			remain -= seg;
863ca2d49f7SDavid Howells 			if (remain == 0 || bc >= ARRAY_SIZE(bvec))
8642dc334f1SDavid Howells 				break;
865ca2d49f7SDavid Howells 			tail++;
8662dc334f1SDavid Howells 		}
8672dc334f1SDavid Howells 
8682dc334f1SDavid Howells 		if (!bc)
8692dc334f1SDavid Howells 			break;
8702dc334f1SDavid Howells 
8712dc334f1SDavid Howells 		msg.msg_flags = MSG_SPLICE_PAGES;
8722dc334f1SDavid Howells 		if (flags & SPLICE_F_MORE)
8732dc334f1SDavid Howells 			msg.msg_flags |= MSG_MORE;
8742dc334f1SDavid Howells 		if (remain && pipe_occupancy(pipe->head, tail) > 0)
8752dc334f1SDavid Howells 			msg.msg_flags |= MSG_MORE;
8760f0fa27bSJan Stancek 		if (out->f_flags & O_NONBLOCK)
8770f0fa27bSJan Stancek 			msg.msg_flags |= MSG_DONTWAIT;
8782dc334f1SDavid Howells 
8792dc334f1SDavid Howells 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc,
8802dc334f1SDavid Howells 			      len - remain);
8812dc334f1SDavid Howells 		ret = sock_sendmsg(sock, &msg);
8822dc334f1SDavid Howells 		if (ret <= 0)
8832dc334f1SDavid Howells 			break;
8842dc334f1SDavid Howells 
8852dc334f1SDavid Howells 		spliced += ret;
8862dc334f1SDavid Howells 		len -= ret;
8872dc334f1SDavid Howells 		tail = pipe->tail;
8882dc334f1SDavid Howells 		while (ret > 0) {
8892dc334f1SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
8902dc334f1SDavid Howells 			size_t seg = min_t(size_t, ret, buf->len);
8912dc334f1SDavid Howells 
8922dc334f1SDavid Howells 			buf->offset += seg;
8932dc334f1SDavid Howells 			buf->len -= seg;
8942dc334f1SDavid Howells 			ret -= seg;
8952dc334f1SDavid Howells 
8962dc334f1SDavid Howells 			if (!buf->len) {
8972dc334f1SDavid Howells 				pipe_buf_release(pipe, buf);
8982dc334f1SDavid Howells 				tail++;
8992dc334f1SDavid Howells 			}
9002dc334f1SDavid Howells 		}
9012dc334f1SDavid Howells 
9022dc334f1SDavid Howells 		if (tail != pipe->tail) {
9032dc334f1SDavid Howells 			pipe->tail = tail;
9042dc334f1SDavid Howells 			if (pipe->files)
9052dc334f1SDavid Howells 				need_wakeup = true;
9062dc334f1SDavid Howells 		}
9072dc334f1SDavid Howells 	}
9082dc334f1SDavid Howells 
9092dc334f1SDavid Howells out:
9102dc334f1SDavid Howells 	pipe_unlock(pipe);
9112dc334f1SDavid Howells 	if (need_wakeup)
9122dc334f1SDavid Howells 		wakeup_pipe_writers(pipe);
9132dc334f1SDavid Howells 	return spliced ?: ret;
9142dc334f1SDavid Howells }
9152dc334f1SDavid Howells #endif
916a0f06780SJeff Garzik 
warn_unsupported(struct file * file,const char * op)91736e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op)
91836e2c742SChristoph Hellwig {
91936e2c742SChristoph Hellwig 	pr_debug_ratelimited(
92036e2c742SChristoph Hellwig 		"splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
92136e2c742SChristoph Hellwig 		op, file, current->pid, current->comm);
92236e2c742SChristoph Hellwig 	return -EINVAL;
92336e2c742SChristoph Hellwig }
92436e2c742SChristoph Hellwig 
92583f9135bSJens Axboe /*
92683f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
92783f9135bSJens Axboe  */
do_splice_from(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)9283a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
929cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
9305274f052SJens Axboe {
93136e2c742SChristoph Hellwig 	if (unlikely(!out->f_op->splice_write))
93236e2c742SChristoph Hellwig 		return warn_unsupported(out, "write");
93300c285d0SChristoph Hellwig 	return out->f_op->splice_write(pipe, out, ppos, len, flags);
9345274f052SJens Axboe }
9355274f052SJens Axboe 
93683f9135bSJens Axboe /*
9372bfc6685SDavid Howells  * Indicate to the caller that there was a premature EOF when reading from the
9382bfc6685SDavid Howells  * source and the caller didn't indicate they would be sending more data after
9392bfc6685SDavid Howells  * this.
9402bfc6685SDavid Howells  */
do_splice_eof(struct splice_desc * sd)9412bfc6685SDavid Howells static void do_splice_eof(struct splice_desc *sd)
9422bfc6685SDavid Howells {
9432bfc6685SDavid Howells 	if (sd->splice_eof)
9442bfc6685SDavid Howells 		sd->splice_eof(sd);
9452bfc6685SDavid Howells }
9462bfc6685SDavid Howells 
9476a3f30b8SDavid Howells /**
9486a3f30b8SDavid Howells  * vfs_splice_read - Read data from a file and splice it into a pipe
9496a3f30b8SDavid Howells  * @in:		File to splice from
9506a3f30b8SDavid Howells  * @ppos:	Input file offset
9516a3f30b8SDavid Howells  * @pipe:	Pipe to splice to
9526a3f30b8SDavid Howells  * @len:	Number of bytes to splice
9536a3f30b8SDavid Howells  * @flags:	Splice modifier flags (SPLICE_F_*)
9546a3f30b8SDavid Howells  *
9556a3f30b8SDavid Howells  * Splice the requested amount of data from the input file to the pipe.  This
9566a3f30b8SDavid Howells  * is synchronous as the caller must hold the pipe lock across the entire
9576a3f30b8SDavid Howells  * operation.
9586a3f30b8SDavid Howells  *
9596a3f30b8SDavid Howells  * If successful, it returns the amount of data spliced, 0 if it hit the EOF or
9606a3f30b8SDavid Howells  * a hole and a negative error code otherwise.
96183f9135bSJens Axboe  */
vfs_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)9626a3f30b8SDavid Howells long vfs_splice_read(struct file *in, loff_t *ppos,
963cbb7e577SJens Axboe 		     struct pipe_inode_info *pipe, size_t len,
964cbb7e577SJens Axboe 		     unsigned int flags)
9655274f052SJens Axboe {
966313d64a3SAl Viro 	unsigned int p_space;
9675274f052SJens Axboe 	int ret;
9685274f052SJens Axboe 
96949570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
9705274f052SJens Axboe 		return -EBADF;
971123856f0SDavid Howells 	if (!len)
972123856f0SDavid Howells 		return 0;
9735274f052SJens Axboe 
974313d64a3SAl Viro 	/* Don't try to read more the pipe has space for. */
975313d64a3SAl Viro 	p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
976313d64a3SAl Viro 	len = min_t(size_t, len, p_space << PAGE_SHIFT);
977313d64a3SAl Viro 
978cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
9795274f052SJens Axboe 	if (unlikely(ret < 0))
9805274f052SJens Axboe 		return ret;
9815274f052SJens Axboe 
98203cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
98303cc0789SAl Viro 		len = MAX_RW_COUNT;
98403cc0789SAl Viro 
98536e2c742SChristoph Hellwig 	if (unlikely(!in->f_op->splice_read))
98636e2c742SChristoph Hellwig 		return warn_unsupported(in, "read");
987aa3dbde8SDavid Howells 	/*
988b85930a0SDavid Howells 	 * O_DIRECT and DAX don't deal with the pagecache, so we allocate a
989b85930a0SDavid Howells 	 * buffer, copy into it and splice that into the pipe.
990aa3dbde8SDavid Howells 	 */
991b85930a0SDavid Howells 	if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host))
992aa3dbde8SDavid Howells 		return copy_splice_read(in, ppos, pipe, len, flags);
9932bc01060SChristoph Hellwig 	return in->f_op->splice_read(in, ppos, pipe, len, flags);
9945274f052SJens Axboe }
9956a3f30b8SDavid Howells EXPORT_SYMBOL_GPL(vfs_splice_read);
9965274f052SJens Axboe 
997932cc6d4SJens Axboe /**
998932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
999932cc6d4SJens Axboe  * @in:		file to splice from
1000932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
1001932cc6d4SJens Axboe  * @actor:	handles the data splicing
1002932cc6d4SJens Axboe  *
1003932cc6d4SJens Axboe  * Description:
1004932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
1005932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
1006932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
1007932cc6d4SJens Axboe  *    that process.
1008932cc6d4SJens Axboe  *
1009c66ab6faSJens Axboe  */
splice_direct_to_actor(struct file * in,struct splice_desc * sd,splice_direct_actor * actor)1010c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1011c66ab6faSJens Axboe 			       splice_direct_actor *actor)
1012b92ce558SJens Axboe {
1013b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
1014b92ce558SJens Axboe 	long ret, bytes;
1015c66ab6faSJens Axboe 	size_t len;
10160ff28d9fSChristophe Leroy 	int i, flags, more;
1017b92ce558SJens Axboe 
1018b92ce558SJens Axboe 	/*
101997ef77c5SJason A. Donenfeld 	 * We require the input to be seekable, as we don't want to randomly
102097ef77c5SJason A. Donenfeld 	 * drop data for eg socket -> socket splicing. Use the piped splicing
102197ef77c5SJason A. Donenfeld 	 * for that!
1022b92ce558SJens Axboe 	 */
102397ef77c5SJason A. Donenfeld 	if (unlikely(!(in->f_mode & FMODE_LSEEK)))
1024b92ce558SJens Axboe 		return -EINVAL;
1025b92ce558SJens Axboe 
1026b92ce558SJens Axboe 	/*
1027b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
1028b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
1029b92ce558SJens Axboe 	 */
1030b92ce558SJens Axboe 	pipe = current->splice_pipe;
103149570e9bSJens Axboe 	if (unlikely(!pipe)) {
10327bee130eSAl Viro 		pipe = alloc_pipe_info();
1033b92ce558SJens Axboe 		if (!pipe)
1034b92ce558SJens Axboe 			return -ENOMEM;
1035b92ce558SJens Axboe 
1036b92ce558SJens Axboe 		/*
1037b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
103800522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
1039b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
1040b92ce558SJens Axboe 		 */
1041b92ce558SJens Axboe 		pipe->readers = 1;
1042b92ce558SJens Axboe 
1043b92ce558SJens Axboe 		current->splice_pipe = pipe;
1044b92ce558SJens Axboe 	}
1045b92ce558SJens Axboe 
1046b92ce558SJens Axboe 	/*
104773d62d83SIngo Molnar 	 * Do the splice.
1048b92ce558SJens Axboe 	 */
1049b92ce558SJens Axboe 	bytes = 0;
1050c66ab6faSJens Axboe 	len = sd->total_len;
1051219d9205SDavid Howells 
1052219d9205SDavid Howells 	/* Don't block on output, we have to drain the direct pipe. */
1053c66ab6faSJens Axboe 	flags = sd->flags;
1054219d9205SDavid Howells 	sd->flags &= ~SPLICE_F_NONBLOCK;
1055c66ab6faSJens Axboe 
1056c66ab6faSJens Axboe 	/*
1057219d9205SDavid Howells 	 * We signal MORE until we've read sufficient data to fulfill the
1058219d9205SDavid Howells 	 * request and we keep signalling it if the caller set it.
1059c66ab6faSJens Axboe 	 */
10600ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
1061219d9205SDavid Howells 	sd->flags |= SPLICE_F_MORE;
1062b92ce558SJens Axboe 
10638cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
106417614445SDarrick J. Wong 
1065b92ce558SJens Axboe 	while (len) {
106651a92c0fSJens Axboe 		size_t read_len;
1067a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
1068b92ce558SJens Axboe 
10696a3f30b8SDavid Howells 		ret = vfs_splice_read(in, &pos, pipe, len, flags);
107051a92c0fSJens Axboe 		if (unlikely(ret <= 0))
10712bfc6685SDavid Howells 			goto read_failure;
1072b92ce558SJens Axboe 
1073b92ce558SJens Axboe 		read_len = ret;
1074c66ab6faSJens Axboe 		sd->total_len = read_len;
1075b92ce558SJens Axboe 
1076b92ce558SJens Axboe 		/*
1077219d9205SDavid Howells 		 * If we now have sufficient data to fulfill the request then
1078219d9205SDavid Howells 		 * we clear SPLICE_F_MORE if it was not set initially.
10790ff28d9fSChristophe Leroy 		 */
1080219d9205SDavid Howells 		if (read_len >= len && !more)
10810ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
1082219d9205SDavid Howells 
10830ff28d9fSChristophe Leroy 		/*
1084b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
1085b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
1086b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
1087b92ce558SJens Axboe 		 */
1088c66ab6faSJens Axboe 		ret = actor(pipe, sd);
1089a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
1090a82c53a0STom Zanussi 			sd->pos = prev_pos;
1091b92ce558SJens Axboe 			goto out_release;
1092a82c53a0STom Zanussi 		}
1093b92ce558SJens Axboe 
1094b92ce558SJens Axboe 		bytes += ret;
1095b92ce558SJens Axboe 		len -= ret;
1096bcd4f3acSJens Axboe 		sd->pos = pos;
1097b92ce558SJens Axboe 
1098a82c53a0STom Zanussi 		if (ret < read_len) {
1099a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
110051a92c0fSJens Axboe 			goto out_release;
1101b92ce558SJens Axboe 		}
1102a82c53a0STom Zanussi 	}
1103b92ce558SJens Axboe 
11049e97198dSJens Axboe done:
11058cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
11069e97198dSJens Axboe 	file_accessed(in);
1107b92ce558SJens Axboe 	return bytes;
1108b92ce558SJens Axboe 
11092bfc6685SDavid Howells read_failure:
11102bfc6685SDavid Howells 	/*
11112bfc6685SDavid Howells 	 * If the user did *not* set SPLICE_F_MORE *and* we didn't hit that
11122bfc6685SDavid Howells 	 * "use all of len" case that cleared SPLICE_F_MORE, *and* we did a
11132bfc6685SDavid Howells 	 * "->splice_in()" that returned EOF (ie zero) *and* we have sent at
11142bfc6685SDavid Howells 	 * least 1 byte *then* we will also do the ->splice_eof() call.
11152bfc6685SDavid Howells 	 */
11162bfc6685SDavid Howells 	if (ret == 0 && !more && len > 0 && bytes)
11172bfc6685SDavid Howells 		do_splice_eof(sd);
1118b92ce558SJens Axboe out_release:
1119b92ce558SJens Axboe 	/*
1120b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1121b92ce558SJens Axboe 	 * the pipe buffers in question:
1122b92ce558SJens Axboe 	 */
11238cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
11248cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1125b92ce558SJens Axboe 
1126a779638cSMiklos Szeredi 		if (buf->ops)
1127a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1128b92ce558SJens Axboe 	}
1129b92ce558SJens Axboe 
11309e97198dSJens Axboe 	if (!bytes)
11319e97198dSJens Axboe 		bytes = ret;
1132b92ce558SJens Axboe 
11339e97198dSJens Axboe 	goto done;
1134c66ab6faSJens Axboe }
1135c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1136c66ab6faSJens Axboe 
direct_splice_actor(struct pipe_inode_info * pipe,struct splice_desc * sd)1137c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1138c66ab6faSJens Axboe 			       struct splice_desc *sd)
1139c66ab6faSJens Axboe {
11406a14b90bSJens Axboe 	struct file *file = sd->u.file;
1141c66ab6faSJens Axboe 
11427995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
11432cb4b05eSChangli Gao 			      sd->flags);
1144c66ab6faSJens Axboe }
1145c66ab6faSJens Axboe 
direct_file_splice_eof(struct splice_desc * sd)11462bfc6685SDavid Howells static void direct_file_splice_eof(struct splice_desc *sd)
11472bfc6685SDavid Howells {
11482bfc6685SDavid Howells 	struct file *file = sd->u.file;
11492bfc6685SDavid Howells 
11502bfc6685SDavid Howells 	if (file->f_op->splice_eof)
11512bfc6685SDavid Howells 		file->f_op->splice_eof(file);
11522bfc6685SDavid Howells }
11532bfc6685SDavid Howells 
1154932cc6d4SJens Axboe /**
1155932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1156932cc6d4SJens Axboe  * @in:		file to splice from
1157932cc6d4SJens Axboe  * @ppos:	input file offset
1158932cc6d4SJens Axboe  * @out:	file to splice to
1159acdb37c3SRandy Dunlap  * @opos:	output file offset
1160932cc6d4SJens Axboe  * @len:	number of bytes to splice
1161932cc6d4SJens Axboe  * @flags:	splice modifier flags
1162932cc6d4SJens Axboe  *
1163932cc6d4SJens Axboe  * Description:
1164932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1165932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1166932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1167932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1168932cc6d4SJens Axboe  *
1169932cc6d4SJens Axboe  */
do_splice_direct(struct file * in,loff_t * ppos,struct file * out,loff_t * opos,size_t len,unsigned int flags)1170c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
11717995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1172c66ab6faSJens Axboe {
1173c66ab6faSJens Axboe 	struct splice_desc sd = {
1174c66ab6faSJens Axboe 		.len		= len,
1175c66ab6faSJens Axboe 		.total_len	= len,
1176c66ab6faSJens Axboe 		.flags		= flags,
1177c66ab6faSJens Axboe 		.pos		= *ppos,
11786a14b90bSJens Axboe 		.u.file		= out,
11792bfc6685SDavid Howells 		.splice_eof	= direct_file_splice_eof,
11807995bd28SAl Viro 		.opos		= opos,
1181c66ab6faSJens Axboe 	};
118251a92c0fSJens Axboe 	long ret;
1183c66ab6faSJens Axboe 
118418c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
118518c67cb9SAl Viro 		return -EBADF;
118618c67cb9SAl Viro 
118718c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
118818c67cb9SAl Viro 		return -EINVAL;
118918c67cb9SAl Viro 
119018c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
119118c67cb9SAl Viro 	if (unlikely(ret < 0))
119218c67cb9SAl Viro 		return ret;
119318c67cb9SAl Viro 
1194c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
119551a92c0fSJens Axboe 	if (ret > 0)
1196a82c53a0STom Zanussi 		*ppos = sd.pos;
119751a92c0fSJens Axboe 
1198c66ab6faSJens Axboe 	return ret;
1199b92ce558SJens Axboe }
12001c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1201b92ce558SJens Axboe 
wait_for_space(struct pipe_inode_info * pipe,unsigned flags)12028924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
12038924feffSAl Viro {
120452bce911SLinus Torvalds 	for (;;) {
120552bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
120652bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
120752bce911SLinus Torvalds 			return -EPIPE;
120852bce911SLinus Torvalds 		}
12096718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
121052bce911SLinus Torvalds 			return 0;
12118924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
12128924feffSAl Viro 			return -EAGAIN;
12138924feffSAl Viro 		if (signal_pending(current))
12148924feffSAl Viro 			return -ERESTARTSYS;
1215472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
12168924feffSAl Viro 	}
12178924feffSAl Viro }
12188924feffSAl Viro 
12197c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
12207c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
12217c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1222ddac0d39SJens Axboe 
splice_file_to_pipe(struct file * in,struct pipe_inode_info * opipe,loff_t * offset,size_t len,unsigned int flags)1223b964bf53SAl Viro long splice_file_to_pipe(struct file *in,
1224faa97c48SAl Viro 			 struct pipe_inode_info *opipe,
1225faa97c48SAl Viro 			 loff_t *offset,
1226faa97c48SAl Viro 			 size_t len, unsigned int flags)
1227faa97c48SAl Viro {
1228faa97c48SAl Viro 	long ret;
1229faa97c48SAl Viro 
1230faa97c48SAl Viro 	pipe_lock(opipe);
1231faa97c48SAl Viro 	ret = wait_for_space(opipe, flags);
1232faa97c48SAl Viro 	if (!ret)
12336a3f30b8SDavid Howells 		ret = vfs_splice_read(in, offset, opipe, len, flags);
1234faa97c48SAl Viro 	pipe_unlock(opipe);
1235faa97c48SAl Viro 	if (ret > 0)
1236faa97c48SAl Viro 		wakeup_pipe_readers(opipe);
1237faa97c48SAl Viro 	return ret;
1238faa97c48SAl Viro }
1239faa97c48SAl Viro 
1240ddac0d39SJens Axboe /*
124183f9135bSJens Axboe  * Determine where to splice to/from.
124283f9135bSJens Axboe  */
do_splice(struct file * in,loff_t * off_in,struct file * out,loff_t * off_out,size_t len,unsigned int flags)1243ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1244ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
12455274f052SJens Axboe {
12467c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
12477c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
12487995bd28SAl Viro 	loff_t offset;
1249a4514ebdSJens Axboe 	long ret;
12505274f052SJens Axboe 
125190da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
125290da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
125390da2e3fSPavel Begunkov 		return -EBADF;
125490da2e3fSPavel Begunkov 
1255c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1256c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
12577c77f0b3SMiklos Szeredi 
12587c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
12597c77f0b3SMiklos Szeredi 		if (off_in || off_out)
12607c77f0b3SMiklos Szeredi 			return -ESPIPE;
12617c77f0b3SMiklos Szeredi 
12627c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
12637c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
12647c77f0b3SMiklos Szeredi 			return -EINVAL;
12657c77f0b3SMiklos Szeredi 
1266ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1267ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1268ee5e0011SSlavomir Kaslev 
126912ee4b66SAhelenia ZiemiaÅ„ska 		ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
127012ee4b66SAhelenia ZiemiaÅ„ska 	} else if (ipipe) {
1271529565dcSIngo Molnar 		if (off_in)
1272529565dcSIngo Molnar 			return -ESPIPE;
1273b92ce558SJens Axboe 		if (off_out) {
127419c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1275b92ce558SJens Axboe 				return -EINVAL;
1276ee6e00c8SJens Axboe 			offset = *off_out;
12777995bd28SAl Viro 		} else {
12787995bd28SAl Viro 			offset = out->f_pos;
12797995bd28SAl Viro 		}
1280529565dcSIngo Molnar 
128118c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
128218c67cb9SAl Viro 			return -EINVAL;
128318c67cb9SAl Viro 
128418c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
128518c67cb9SAl Viro 		if (unlikely(ret < 0))
128618c67cb9SAl Viro 			return ret;
128718c67cb9SAl Viro 
1288ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1289ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1290ee5e0011SSlavomir Kaslev 
1291500368f7SAl Viro 		file_start_write(out);
12927995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1293500368f7SAl Viro 		file_end_write(out);
1294a4514ebdSJens Axboe 
12957995bd28SAl Viro 		if (!off_out)
12967995bd28SAl Viro 			out->f_pos = offset;
1297ee6e00c8SJens Axboe 		else
1298ee6e00c8SJens Axboe 			*off_out = offset;
129912ee4b66SAhelenia ZiemiaÅ„ska 	} else if (opipe) {
1300529565dcSIngo Molnar 		if (off_out)
1301529565dcSIngo Molnar 			return -ESPIPE;
1302b92ce558SJens Axboe 		if (off_in) {
130319c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1304b92ce558SJens Axboe 				return -EINVAL;
1305ee6e00c8SJens Axboe 			offset = *off_in;
13067995bd28SAl Viro 		} else {
13077995bd28SAl Viro 			offset = in->f_pos;
13087995bd28SAl Viro 		}
1309529565dcSIngo Molnar 
1310ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1311ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1312ee5e0011SSlavomir Kaslev 
1313faa97c48SAl Viro 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1314983652c6SChung-Chiang Cheng 
13157995bd28SAl Viro 		if (!off_in)
13167995bd28SAl Viro 			in->f_pos = offset;
1317ee6e00c8SJens Axboe 		else
1318ee6e00c8SJens Axboe 			*off_in = offset;
131912ee4b66SAhelenia ZiemiaÅ„ska 	} else {
132012ee4b66SAhelenia ZiemiaÅ„ska 		ret = -EINVAL;
1321529565dcSIngo Molnar 	}
13225274f052SJens Axboe 
132312ee4b66SAhelenia ZiemiaÅ„ska 	if (ret > 0) {
132412ee4b66SAhelenia ZiemiaÅ„ska 		/*
132512ee4b66SAhelenia ZiemiaÅ„ska 		 * Generate modify out before access in:
132612ee4b66SAhelenia ZiemiaÅ„ska 		 * do_splice_from() may've already sent modify out,
132712ee4b66SAhelenia ZiemiaÅ„ska 		 * and this ensures the events get merged.
132812ee4b66SAhelenia ZiemiaÅ„ska 		 */
132912ee4b66SAhelenia ZiemiaÅ„ska 		fsnotify_modify(out);
133012ee4b66SAhelenia ZiemiaÅ„ska 		fsnotify_access(in);
133112ee4b66SAhelenia ZiemiaÅ„ska 	}
133212ee4b66SAhelenia ZiemiaÅ„ska 
133312ee4b66SAhelenia ZiemiaÅ„ska 	return ret;
13345274f052SJens Axboe }
13355274f052SJens Axboe 
__do_splice(struct file * in,loff_t __user * off_in,struct file * out,loff_t __user * off_out,size_t len,unsigned int flags)1336ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1337ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1338ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1339ee6e00c8SJens Axboe {
1340ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1341ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1342ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1343ee6e00c8SJens Axboe 	long ret;
1344ee6e00c8SJens Axboe 
1345ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1346ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1347ee6e00c8SJens Axboe 
13480f99fc51SJens Axboe 	if (ipipe) {
13490f99fc51SJens Axboe 		if (off_in)
1350ee6e00c8SJens Axboe 			return -ESPIPE;
13510f99fc51SJens Axboe 		pipe_clear_nowait(in);
13520f99fc51SJens Axboe 	}
13530f99fc51SJens Axboe 	if (opipe) {
13540f99fc51SJens Axboe 		if (off_out)
1355ee6e00c8SJens Axboe 			return -ESPIPE;
13560f99fc51SJens Axboe 		pipe_clear_nowait(out);
13570f99fc51SJens Axboe 	}
1358ee6e00c8SJens Axboe 
1359ee6e00c8SJens Axboe 	if (off_out) {
1360ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1361ee6e00c8SJens Axboe 			return -EFAULT;
1362ee6e00c8SJens Axboe 		__off_out = &offset;
1363ee6e00c8SJens Axboe 	}
1364ee6e00c8SJens Axboe 	if (off_in) {
1365ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1366ee6e00c8SJens Axboe 			return -EFAULT;
1367ee6e00c8SJens Axboe 		__off_in = &offset;
1368ee6e00c8SJens Axboe 	}
1369ee6e00c8SJens Axboe 
1370ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1371ee6e00c8SJens Axboe 	if (ret < 0)
1372ee6e00c8SJens Axboe 		return ret;
1373ee6e00c8SJens Axboe 
1374ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1375ee6e00c8SJens Axboe 		return -EFAULT;
1376ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1377ee6e00c8SJens Axboe 		return -EFAULT;
1378ee6e00c8SJens Axboe 
1379ee6e00c8SJens Axboe 	return ret;
1380ee6e00c8SJens Axboe }
1381ee6e00c8SJens Axboe 
iter_to_pipe(struct iov_iter * from,struct pipe_inode_info * pipe,unsigned flags)138279fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
138379fddc4eSAl Viro 			struct pipe_inode_info *pipe,
138479fddc4eSAl Viro 			unsigned flags)
1385912d35f8SJens Axboe {
138679fddc4eSAl Viro 	struct pipe_buffer buf = {
138779fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
138879fddc4eSAl Viro 		.flags = flags
138979fddc4eSAl Viro 	};
139079fddc4eSAl Viro 	size_t total = 0;
139179fddc4eSAl Viro 	int ret = 0;
139279fddc4eSAl Viro 
13937d690c15SAl Viro 	while (iov_iter_count(from)) {
139479fddc4eSAl Viro 		struct page *pages[16];
13957d690c15SAl Viro 		ssize_t left;
1396db85a9ebSAl Viro 		size_t start;
13977d690c15SAl Viro 		int i, n;
1398912d35f8SJens Axboe 
13997d690c15SAl Viro 		left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
14007d690c15SAl Viro 		if (left <= 0) {
14017d690c15SAl Viro 			ret = left;
140279fddc4eSAl Viro 			break;
140379fddc4eSAl Viro 		}
1404912d35f8SJens Axboe 
14057d690c15SAl Viro 		n = DIV_ROUND_UP(left + start, PAGE_SIZE);
14067d690c15SAl Viro 		for (i = 0; i < n; i++) {
14077d690c15SAl Viro 			int size = min_t(int, left, PAGE_SIZE - start);
14087d690c15SAl Viro 
14097d690c15SAl Viro 			buf.page = pages[i];
141079fddc4eSAl Viro 			buf.offset = start;
141179fddc4eSAl Viro 			buf.len = size;
141279fddc4eSAl Viro 			ret = add_to_pipe(pipe, &buf);
141379fddc4eSAl Viro 			if (unlikely(ret < 0)) {
14147d690c15SAl Viro 				iov_iter_revert(from, left);
14157d690c15SAl Viro 				// this one got dropped by add_to_pipe()
14167d690c15SAl Viro 				while (++i < n)
14177d690c15SAl Viro 					put_page(pages[i]);
14187d690c15SAl Viro 				goto out;
14197d690c15SAl Viro 			}
142079fddc4eSAl Viro 			total += ret;
14217d690c15SAl Viro 			left -= size;
14227d690c15SAl Viro 			start = 0;
1423912d35f8SJens Axboe 		}
1424912d35f8SJens Axboe 	}
14257d690c15SAl Viro out:
142679fddc4eSAl Viro 	return total ? total : ret;
1427912d35f8SJens Axboe }
1428912d35f8SJens Axboe 
pipe_to_user(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)14296a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
14306a14b90bSJens Axboe 			struct splice_desc *sd)
14316a14b90bSJens Axboe {
14326130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
14336130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
14346a14b90bSJens Axboe }
14356a14b90bSJens Axboe 
14366a14b90bSJens Axboe /*
14376a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
14386a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
14396a14b90bSJens Axboe  */
vmsplice_to_user(struct file * file,struct iov_iter * iter,unsigned int flags)144087a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
144187a3002aSAl Viro 			     unsigned int flags)
14426a14b90bSJens Axboe {
1443c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
144487a3002aSAl Viro 	struct splice_desc sd = {
144587a3002aSAl Viro 		.total_len = iov_iter_count(iter),
144687a3002aSAl Viro 		.flags = flags,
144787a3002aSAl Viro 		.u.data = iter
144887a3002aSAl Viro 	};
144987a3002aSAl Viro 	long ret = 0;
14506a14b90bSJens Axboe 
14516a14b90bSJens Axboe 	if (!pipe)
14526a14b90bSJens Axboe 		return -EBADF;
14536a14b90bSJens Axboe 
14540f99fc51SJens Axboe 	pipe_clear_nowait(file);
14550f99fc51SJens Axboe 
1456345995faSAl Viro 	if (sd.total_len) {
14576130f531SAl Viro 		pipe_lock(pipe);
14586130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
145961e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1460345995faSAl Viro 	}
14616a14b90bSJens Axboe 
14627f0f1ea0SAhelenia ZiemiaÅ„ska 	if (ret > 0)
14637f0f1ea0SAhelenia ZiemiaÅ„ska 		fsnotify_access(file);
14647f0f1ea0SAhelenia ZiemiaÅ„ska 
14656a14b90bSJens Axboe 	return ret;
14666a14b90bSJens Axboe }
14676a14b90bSJens Axboe 
1468912d35f8SJens Axboe /*
1469912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1470912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1471912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1472912d35f8SJens Axboe  */
vmsplice_to_pipe(struct file * file,struct iov_iter * iter,unsigned int flags)147387a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
147487a3002aSAl Viro 			     unsigned int flags)
1475912d35f8SJens Axboe {
1476ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
147787a3002aSAl Viro 	long ret = 0;
147879fddc4eSAl Viro 	unsigned buf_flag = 0;
147979fddc4eSAl Viro 
148079fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
148179fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1482912d35f8SJens Axboe 
1483c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1484ddac0d39SJens Axboe 	if (!pipe)
1485912d35f8SJens Axboe 		return -EBADF;
1486912d35f8SJens Axboe 
14870f99fc51SJens Axboe 	pipe_clear_nowait(file);
14880f99fc51SJens Axboe 
14898924feffSAl Viro 	pipe_lock(pipe);
14908924feffSAl Viro 	ret = wait_for_space(pipe, flags);
149179fddc4eSAl Viro 	if (!ret)
149287a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
14938924feffSAl Viro 	pipe_unlock(pipe);
14947f0f1ea0SAhelenia ZiemiaÅ„ska 	if (ret > 0) {
14958924feffSAl Viro 		wakeup_pipe_readers(pipe);
14967f0f1ea0SAhelenia ZiemiaÅ„ska 		fsnotify_modify(file);
14977f0f1ea0SAhelenia ZiemiaÅ„ska 	}
149835f3d14dSJens Axboe 	return ret;
1499912d35f8SJens Axboe }
1500912d35f8SJens Axboe 
vmsplice_type(struct fd f,int * type)150187a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
150287a3002aSAl Viro {
150387a3002aSAl Viro 	if (!f.file)
150487a3002aSAl Viro 		return -EBADF;
150587a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
1506de4eda9dSAl Viro 		*type = ITER_SOURCE;
150787a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
1508de4eda9dSAl Viro 		*type = ITER_DEST;
150987a3002aSAl Viro 	} else {
151087a3002aSAl Viro 		fdput(f);
151187a3002aSAl Viro 		return -EBADF;
151287a3002aSAl Viro 	}
151387a3002aSAl Viro 	return 0;
151487a3002aSAl Viro }
151587a3002aSAl Viro 
15166a14b90bSJens Axboe /*
15176a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
15186a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
15196a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
15206a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
15216a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
15226a14b90bSJens Axboe  * solutions for that:
15236a14b90bSJens Axboe  *
15246a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
15256a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
15266a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
15276a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
15286a14b90bSJens Axboe  *
15296a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
15306a14b90bSJens Axboe  *
15316a14b90bSJens Axboe  */
SYSCALL_DEFINE4(vmsplice,int,fd,const struct iovec __user *,uiov,unsigned long,nr_segs,unsigned int,flags)153287a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
153330cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
153430cfe4efSDominik Brodowski {
153587a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
153687a3002aSAl Viro 	struct iovec *iov = iovstack;
153787a3002aSAl Viro 	struct iov_iter iter;
153887e5e6daSJens Axboe 	ssize_t error;
153987a3002aSAl Viro 	struct fd f;
154087a3002aSAl Viro 	int type;
154187a3002aSAl Viro 
1542598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1543598b3cecSChristoph Hellwig 		return -EINVAL;
1544598b3cecSChristoph Hellwig 
154587a3002aSAl Viro 	f = fdget(fd);
154687a3002aSAl Viro 	error = vmsplice_type(f, &type);
154787a3002aSAl Viro 	if (error)
154887a3002aSAl Viro 		return error;
154987a3002aSAl Viro 
155087a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
155187a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1552598b3cecSChristoph Hellwig 	if (error < 0)
1553598b3cecSChristoph Hellwig 		goto out_fdput;
1554598b3cecSChristoph Hellwig 
1555598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1556598b3cecSChristoph Hellwig 		error = 0;
1557de4eda9dSAl Viro 	else if (type == ITER_SOURCE)
1558598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1559598b3cecSChristoph Hellwig 	else
1560598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1561598b3cecSChristoph Hellwig 
156287a3002aSAl Viro 	kfree(iov);
1563598b3cecSChristoph Hellwig out_fdput:
156487a3002aSAl Viro 	fdput(f);
156587a3002aSAl Viro 	return error;
156630cfe4efSDominik Brodowski }
156730cfe4efSDominik Brodowski 
SYSCALL_DEFINE6(splice,int,fd_in,loff_t __user *,off_in,int,fd_out,loff_t __user *,off_out,size_t,len,unsigned int,flags)1568836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1569836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1570836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
15715274f052SJens Axboe {
15722903ff01SAl Viro 	struct fd in, out;
15735274f052SJens Axboe 	long error;
15745274f052SJens Axboe 
15755274f052SJens Axboe 	if (unlikely(!len))
15765274f052SJens Axboe 		return 0;
15775274f052SJens Axboe 
15783d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
15793d6ea290SAl Viro 		return -EINVAL;
15803d6ea290SAl Viro 
15815274f052SJens Axboe 	error = -EBADF;
15822903ff01SAl Viro 	in = fdget(fd_in);
15832903ff01SAl Viro 	if (in.file) {
15842903ff01SAl Viro 		out = fdget(fd_out);
15852903ff01SAl Viro 		if (out.file) {
1586ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1587529565dcSIngo Molnar 						len, flags);
15882903ff01SAl Viro 			fdput(out);
15895274f052SJens Axboe 		}
15902903ff01SAl Viro 		fdput(in);
15915274f052SJens Axboe 	}
15925274f052SJens Axboe 	return error;
15935274f052SJens Axboe }
159470524490SJens Axboe 
159570524490SJens Axboe /*
1596aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1597aadd06e5SJens Axboe  * return an appropriate error.
1598aadd06e5SJens Axboe  */
ipipe_prep(struct pipe_inode_info * pipe,unsigned int flags)15997c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1600aadd06e5SJens Axboe {
1601aadd06e5SJens Axboe 	int ret;
1602aadd06e5SJens Axboe 
1603aadd06e5SJens Axboe 	/*
16048cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1605aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1606aadd06e5SJens Axboe 	 */
16078cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1608aadd06e5SJens Axboe 		return 0;
1609aadd06e5SJens Axboe 
1610aadd06e5SJens Axboe 	ret = 0;
161161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1612aadd06e5SJens Axboe 
16138cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1614aadd06e5SJens Axboe 		if (signal_pending(current)) {
1615aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1616aadd06e5SJens Axboe 			break;
1617aadd06e5SJens Axboe 		}
1618aadd06e5SJens Axboe 		if (!pipe->writers)
1619aadd06e5SJens Axboe 			break;
1620aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1621aadd06e5SJens Axboe 			ret = -EAGAIN;
1622aadd06e5SJens Axboe 			break;
1623aadd06e5SJens Axboe 		}
1624472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1625aadd06e5SJens Axboe 	}
1626aadd06e5SJens Axboe 
162761e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1628aadd06e5SJens Axboe 	return ret;
1629aadd06e5SJens Axboe }
1630aadd06e5SJens Axboe 
1631aadd06e5SJens Axboe /*
1632aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1633aadd06e5SJens Axboe  * return an appropriate error.
1634aadd06e5SJens Axboe  */
opipe_prep(struct pipe_inode_info * pipe,unsigned int flags)16357c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1636aadd06e5SJens Axboe {
1637aadd06e5SJens Axboe 	int ret;
1638aadd06e5SJens Axboe 
1639aadd06e5SJens Axboe 	/*
16408cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1641aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1642aadd06e5SJens Axboe 	 */
1643566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1644aadd06e5SJens Axboe 		return 0;
1645aadd06e5SJens Axboe 
1646aadd06e5SJens Axboe 	ret = 0;
164761e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1648aadd06e5SJens Axboe 
16496718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1650aadd06e5SJens Axboe 		if (!pipe->readers) {
1651aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1652aadd06e5SJens Axboe 			ret = -EPIPE;
1653aadd06e5SJens Axboe 			break;
1654aadd06e5SJens Axboe 		}
1655aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1656aadd06e5SJens Axboe 			ret = -EAGAIN;
1657aadd06e5SJens Axboe 			break;
1658aadd06e5SJens Axboe 		}
1659aadd06e5SJens Axboe 		if (signal_pending(current)) {
1660aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1661aadd06e5SJens Axboe 			break;
1662aadd06e5SJens Axboe 		}
1663472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1664aadd06e5SJens Axboe 	}
1665aadd06e5SJens Axboe 
166661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1667aadd06e5SJens Axboe 	return ret;
1668aadd06e5SJens Axboe }
1669aadd06e5SJens Axboe 
1670aadd06e5SJens Axboe /*
16717c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
16727c77f0b3SMiklos Szeredi  */
splice_pipe_to_pipe(struct pipe_inode_info * ipipe,struct pipe_inode_info * opipe,size_t len,unsigned int flags)16737c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
16747c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
16757c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
16767c77f0b3SMiklos Szeredi {
16777c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
16788cefc107SDavid Howells 	unsigned int i_head, o_head;
16798cefc107SDavid Howells 	unsigned int i_tail, o_tail;
16808cefc107SDavid Howells 	unsigned int i_mask, o_mask;
16818cefc107SDavid Howells 	int ret = 0;
16827c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
16837c77f0b3SMiklos Szeredi 
16847c77f0b3SMiklos Szeredi 
16857c77f0b3SMiklos Szeredi retry:
16867c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
16877c77f0b3SMiklos Szeredi 	if (ret)
16887c77f0b3SMiklos Szeredi 		return ret;
16897c77f0b3SMiklos Szeredi 
16907c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
16917c77f0b3SMiklos Szeredi 	if (ret)
16927c77f0b3SMiklos Szeredi 		return ret;
16937c77f0b3SMiklos Szeredi 
16947c77f0b3SMiklos Szeredi 	/*
16957c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
16967c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
16977c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
16987c77f0b3SMiklos Szeredi 	 */
16997c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
17007c77f0b3SMiklos Szeredi 
17018cefc107SDavid Howells 	i_tail = ipipe->tail;
17028cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
17038cefc107SDavid Howells 	o_head = opipe->head;
17048cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
17058cefc107SDavid Howells 
17067c77f0b3SMiklos Szeredi 	do {
17078cefc107SDavid Howells 		size_t o_len;
17088cefc107SDavid Howells 
17097c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
17107c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
17117c77f0b3SMiklos Szeredi 			if (!ret)
17127c77f0b3SMiklos Szeredi 				ret = -EPIPE;
17137c77f0b3SMiklos Szeredi 			break;
17147c77f0b3SMiklos Szeredi 		}
17157c77f0b3SMiklos Szeredi 
17168cefc107SDavid Howells 		i_head = ipipe->head;
17178cefc107SDavid Howells 		o_tail = opipe->tail;
17188cefc107SDavid Howells 
17198cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
17207c77f0b3SMiklos Szeredi 			break;
17217c77f0b3SMiklos Szeredi 
17227c77f0b3SMiklos Szeredi 		/*
17237c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
17247c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
17257c77f0b3SMiklos Szeredi 		 */
17268cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17276718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
17287c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
17297c77f0b3SMiklos Szeredi 			if (ret)
17307c77f0b3SMiklos Szeredi 				break;
17317c77f0b3SMiklos Szeredi 
17327c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
17337c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
17347c77f0b3SMiklos Szeredi 				break;
17357c77f0b3SMiklos Szeredi 			}
17367c77f0b3SMiklos Szeredi 
17377c77f0b3SMiklos Szeredi 			/*
17387c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
17397c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
17407c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
17417c77f0b3SMiklos Szeredi 			 */
17427c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
17437c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
17447c77f0b3SMiklos Szeredi 			goto retry;
17457c77f0b3SMiklos Szeredi 		}
17467c77f0b3SMiklos Szeredi 
17478cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17488cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
17497c77f0b3SMiklos Szeredi 
17507c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
17517c77f0b3SMiklos Szeredi 			/*
17527c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
17537c77f0b3SMiklos Szeredi 			 */
17547c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
17557c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
17568cefc107SDavid Howells 			i_tail++;
17578cefc107SDavid Howells 			ipipe->tail = i_tail;
17587c77f0b3SMiklos Szeredi 			input_wakeup = true;
17598cefc107SDavid Howells 			o_len = obuf->len;
17608cefc107SDavid Howells 			o_head++;
17618cefc107SDavid Howells 			opipe->head = o_head;
17627c77f0b3SMiklos Szeredi 		} else {
17637c77f0b3SMiklos Szeredi 			/*
17647c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
17657c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
17667c77f0b3SMiklos Szeredi 			 */
176715fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
176815fab63eSMatthew Wilcox 				if (ret == 0)
176915fab63eSMatthew Wilcox 					ret = -EFAULT;
177015fab63eSMatthew Wilcox 				break;
177115fab63eSMatthew Wilcox 			}
17727c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
17737c77f0b3SMiklos Szeredi 
17747c77f0b3SMiklos Szeredi 			/*
1775f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
17767c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
17777c77f0b3SMiklos Szeredi 			 */
17787c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1779f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1780a0ce2f0aSJann Horn 
17817c77f0b3SMiklos Szeredi 			obuf->len = len;
17828cefc107SDavid Howells 			ibuf->offset += len;
17838cefc107SDavid Howells 			ibuf->len -= len;
17848cefc107SDavid Howells 			o_len = len;
17858cefc107SDavid Howells 			o_head++;
17868cefc107SDavid Howells 			opipe->head = o_head;
17877c77f0b3SMiklos Szeredi 		}
17888cefc107SDavid Howells 		ret += o_len;
17898cefc107SDavid Howells 		len -= o_len;
17907c77f0b3SMiklos Szeredi 	} while (len);
17917c77f0b3SMiklos Szeredi 
17927c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
17937c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
17947c77f0b3SMiklos Szeredi 
17957c77f0b3SMiklos Szeredi 	/*
17967c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
17977c77f0b3SMiklos Szeredi 	 */
1798825cdcb1SNamhyung Kim 	if (ret > 0)
1799825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1800825cdcb1SNamhyung Kim 
18017c77f0b3SMiklos Szeredi 	if (input_wakeup)
18027c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
18037c77f0b3SMiklos Szeredi 
18047c77f0b3SMiklos Szeredi 	return ret;
18057c77f0b3SMiklos Szeredi }
18067c77f0b3SMiklos Szeredi 
18077c77f0b3SMiklos Szeredi /*
180870524490SJens Axboe  * Link contents of ipipe to opipe.
180970524490SJens Axboe  */
link_pipe(struct pipe_inode_info * ipipe,struct pipe_inode_info * opipe,size_t len,unsigned int flags)181070524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
181170524490SJens Axboe 		     struct pipe_inode_info *opipe,
181270524490SJens Axboe 		     size_t len, unsigned int flags)
181370524490SJens Axboe {
181470524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
18158cefc107SDavid Howells 	unsigned int i_head, o_head;
18168cefc107SDavid Howells 	unsigned int i_tail, o_tail;
18178cefc107SDavid Howells 	unsigned int i_mask, o_mask;
18188cefc107SDavid Howells 	int ret = 0;
181970524490SJens Axboe 
182070524490SJens Axboe 	/*
182170524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
182261e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
182370524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
182470524490SJens Axboe 	 */
182561e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
182670524490SJens Axboe 
18278cefc107SDavid Howells 	i_tail = ipipe->tail;
18288cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
18298cefc107SDavid Howells 	o_head = opipe->head;
18308cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
18318cefc107SDavid Howells 
1832aadd06e5SJens Axboe 	do {
183370524490SJens Axboe 		if (!opipe->readers) {
183470524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
183570524490SJens Axboe 			if (!ret)
183670524490SJens Axboe 				ret = -EPIPE;
183770524490SJens Axboe 			break;
183870524490SJens Axboe 		}
183970524490SJens Axboe 
18408cefc107SDavid Howells 		i_head = ipipe->head;
18418cefc107SDavid Howells 		o_tail = opipe->tail;
18428cefc107SDavid Howells 
184370524490SJens Axboe 		/*
18448cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1845aadd06e5SJens Axboe 		 * output room, break.
184670524490SJens Axboe 		 */
18478cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
18486718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1849aadd06e5SJens Axboe 			break;
1850aadd06e5SJens Axboe 
18518cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
18528cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
185370524490SJens Axboe 
185470524490SJens Axboe 		/*
185570524490SJens Axboe 		 * Get a reference to this pipe buffer,
185670524490SJens Axboe 		 * so we can copy the contents over.
185770524490SJens Axboe 		 */
185815fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
185915fab63eSMatthew Wilcox 			if (ret == 0)
186015fab63eSMatthew Wilcox 				ret = -EFAULT;
186115fab63eSMatthew Wilcox 			break;
186215fab63eSMatthew Wilcox 		}
186370524490SJens Axboe 
186470524490SJens Axboe 		*obuf = *ibuf;
186570524490SJens Axboe 
18667afa6fd0SJens Axboe 		/*
1867f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1868f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
18697afa6fd0SJens Axboe 		 */
18707afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1871f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1872a0ce2f0aSJann Horn 
187370524490SJens Axboe 		if (obuf->len > len)
187470524490SJens Axboe 			obuf->len = len;
187570524490SJens Axboe 		ret += obuf->len;
187670524490SJens Axboe 		len -= obuf->len;
18778cefc107SDavid Howells 
18788cefc107SDavid Howells 		o_head++;
18798cefc107SDavid Howells 		opipe->head = o_head;
18808cefc107SDavid Howells 		i_tail++;
1881aadd06e5SJens Axboe 	} while (len);
188270524490SJens Axboe 
188361e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
188461e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
188570524490SJens Axboe 
1886aadd06e5SJens Axboe 	/*
1887aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1888aadd06e5SJens Axboe 	 */
1889825cdcb1SNamhyung Kim 	if (ret > 0)
1890825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
189170524490SJens Axboe 
189270524490SJens Axboe 	return ret;
189370524490SJens Axboe }
189470524490SJens Axboe 
189570524490SJens Axboe /*
189670524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
189770524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
189870524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
189970524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
190070524490SJens Axboe  */
do_tee(struct file * in,struct file * out,size_t len,unsigned int flags)19019dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
190270524490SJens Axboe {
1903c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1904c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1905aadd06e5SJens Axboe 	int ret = -EINVAL;
190670524490SJens Axboe 
190790da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
190890da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
190990da2e3fSPavel Begunkov 		return -EBADF;
191090da2e3fSPavel Begunkov 
191170524490SJens Axboe 	/*
1912aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1913aadd06e5SJens Axboe 	 * copying the data.
191470524490SJens Axboe 	 */
1915aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1916ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1917ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1918ee5e0011SSlavomir Kaslev 
1919aadd06e5SJens Axboe 		/*
1920aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1921aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1922aadd06e5SJens Axboe 		 */
19237c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1924aadd06e5SJens Axboe 		if (!ret) {
19257c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
192602cf01aeSJens Axboe 			if (!ret)
1927aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1928aadd06e5SJens Axboe 		}
1929aadd06e5SJens Axboe 	}
193070524490SJens Axboe 
1931576d498eSAhelenia ZiemiaÅ„ska 	if (ret > 0) {
1932576d498eSAhelenia ZiemiaÅ„ska 		fsnotify_access(in);
1933576d498eSAhelenia ZiemiaÅ„ska 		fsnotify_modify(out);
1934576d498eSAhelenia ZiemiaÅ„ska 	}
1935576d498eSAhelenia ZiemiaÅ„ska 
1936aadd06e5SJens Axboe 	return ret;
193770524490SJens Axboe }
193870524490SJens Axboe 
SYSCALL_DEFINE4(tee,int,fdin,int,fdout,size_t,len,unsigned int,flags)1939836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
194070524490SJens Axboe {
194190da2e3fSPavel Begunkov 	struct fd in, out;
19422903ff01SAl Viro 	int error;
194370524490SJens Axboe 
19443d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
19453d6ea290SAl Viro 		return -EINVAL;
19463d6ea290SAl Viro 
194770524490SJens Axboe 	if (unlikely(!len))
194870524490SJens Axboe 		return 0;
194970524490SJens Axboe 
195070524490SJens Axboe 	error = -EBADF;
19512903ff01SAl Viro 	in = fdget(fdin);
19522903ff01SAl Viro 	if (in.file) {
195390da2e3fSPavel Begunkov 		out = fdget(fdout);
19542903ff01SAl Viro 		if (out.file) {
195590da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
19562903ff01SAl Viro 			fdput(out);
195770524490SJens Axboe 		}
19582903ff01SAl Viro  		fdput(in);
195970524490SJens Axboe  	}
196070524490SJens Axboe 
196170524490SJens Axboe 	return error;
196270524490SJens Axboe }
1963