xref: /openbmc/linux/fs/splice.c (revision ee6e00c8)
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>
3329ce2058SJames Morris #include <linux/security.h>
345a0e3ad6STejun Heo #include <linux/gfp.h>
3535f9c09fSEric Dumazet #include <linux/socket.h>
36174cd4b1SIngo Molnar #include <linux/sched/signal.h>
37174cd4b1SIngo Molnar 
3806ae43f3SAl Viro #include "internal.h"
395274f052SJens Axboe 
4083f9135bSJens Axboe /*
4183f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
4283f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
4383f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
4483f9135bSJens Axboe  * attempt to reuse this page for another destination.
4583f9135bSJens Axboe  */
46c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
475abc97aaSJens Axboe 		struct pipe_buffer *buf)
485abc97aaSJens Axboe {
495abc97aaSJens Axboe 	struct page *page = buf->page;
509e94cd4fSJens Axboe 	struct address_space *mapping;
515abc97aaSJens Axboe 
529e0267c2SJens Axboe 	lock_page(page);
539e0267c2SJens Axboe 
549e94cd4fSJens Axboe 	mapping = page_mapping(page);
559e94cd4fSJens Axboe 	if (mapping) {
565abc97aaSJens Axboe 		WARN_ON(!PageUptodate(page));
575abc97aaSJens Axboe 
58ad8d6f0aSJens Axboe 		/*
599e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
609e94cd4fSJens Axboe 		 * writeback completing on this page, since we'll remove it
619e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
629e94cd4fSJens Axboe 		 * page, allowing the disk blocks to be reused by someone else
639e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
649e94cd4fSJens Axboe 		 * ensues.
65ad8d6f0aSJens Axboe 		 */
66ad8d6f0aSJens Axboe 		wait_on_page_writeback(page);
67ad8d6f0aSJens Axboe 
68266cf658SDavid Howells 		if (page_has_private(page) &&
69266cf658SDavid Howells 		    !try_to_release_page(page, GFP_KERNEL))
70ca39d651SJens Axboe 			goto out_unlock;
714f6f0bd2SJens Axboe 
729e94cd4fSJens Axboe 		/*
739e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
749e94cd4fSJens Axboe 		 * and return good.
759e94cd4fSJens Axboe 		 */
769e94cd4fSJens Axboe 		if (remove_mapping(mapping, page)) {
771432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
78c928f642SChristoph Hellwig 			return true;
795abc97aaSJens Axboe 		}
809e94cd4fSJens Axboe 	}
819e94cd4fSJens Axboe 
829e94cd4fSJens Axboe 	/*
839e94cd4fSJens Axboe 	 * Raced with truncate or failed to remove page from current
849e94cd4fSJens Axboe 	 * address space, unlock and return failure.
859e94cd4fSJens Axboe 	 */
86ca39d651SJens Axboe out_unlock:
879e94cd4fSJens Axboe 	unlock_page(page);
88c928f642SChristoph Hellwig 	return false;
899e94cd4fSJens Axboe }
905abc97aaSJens Axboe 
9176ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
925274f052SJens Axboe 					struct pipe_buffer *buf)
935274f052SJens Axboe {
9409cbfeafSKirill A. Shutemov 	put_page(buf->page);
951432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
965274f052SJens Axboe }
975274f052SJens Axboe 
980845718dSJens Axboe /*
990845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
1000845718dSJens Axboe  * is a page cache page, IO may be in flight.
1010845718dSJens Axboe  */
102cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1035274f052SJens Axboe 				       struct pipe_buffer *buf)
1045274f052SJens Axboe {
1055274f052SJens Axboe 	struct page *page = buf->page;
10649d0b21bSJens Axboe 	int err;
1075274f052SJens Axboe 
1085274f052SJens Axboe 	if (!PageUptodate(page)) {
10949d0b21bSJens Axboe 		lock_page(page);
1105274f052SJens Axboe 
11149d0b21bSJens Axboe 		/*
11249d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
11373d62d83SIngo Molnar 		 * splice, if this is the first page.
11449d0b21bSJens Axboe 		 */
1155274f052SJens Axboe 		if (!page->mapping) {
11649d0b21bSJens Axboe 			err = -ENODATA;
11749d0b21bSJens Axboe 			goto error;
1185274f052SJens Axboe 		}
1195274f052SJens Axboe 
12049d0b21bSJens Axboe 		/*
12173d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
12249d0b21bSJens Axboe 		 */
12349d0b21bSJens Axboe 		if (!PageUptodate(page)) {
12449d0b21bSJens Axboe 			err = -EIO;
12549d0b21bSJens Axboe 			goto error;
12649d0b21bSJens Axboe 		}
12749d0b21bSJens Axboe 
12849d0b21bSJens Axboe 		/*
129f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
13049d0b21bSJens Axboe 		 */
13149d0b21bSJens Axboe 		unlock_page(page);
13249d0b21bSJens Axboe 	}
13349d0b21bSJens Axboe 
134f84d7519SJens Axboe 	return 0;
13549d0b21bSJens Axboe error:
13649d0b21bSJens Axboe 	unlock_page(page);
137f84d7519SJens Axboe 	return err;
13870524490SJens Axboe }
13970524490SJens Axboe 
140708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
141cac36bb0SJens Axboe 	.confirm	= page_cache_pipe_buf_confirm,
1425274f052SJens Axboe 	.release	= page_cache_pipe_buf_release,
143c928f642SChristoph Hellwig 	.try_steal	= page_cache_pipe_buf_try_steal,
144f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
1455274f052SJens Axboe };
1465274f052SJens Axboe 
147c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
148912d35f8SJens Axboe 		struct pipe_buffer *buf)
149912d35f8SJens Axboe {
1507afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
151c928f642SChristoph Hellwig 		return false;
1527afa6fd0SJens Axboe 
1531432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
154c928f642SChristoph Hellwig 	return generic_pipe_buf_try_steal(pipe, buf);
155912d35f8SJens Axboe }
156912d35f8SJens Axboe 
157d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
158912d35f8SJens Axboe 	.release	= page_cache_pipe_buf_release,
159c928f642SChristoph Hellwig 	.try_steal	= user_page_pipe_buf_try_steal,
160f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
161912d35f8SJens Axboe };
162912d35f8SJens Axboe 
163825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
164825cdcb1SNamhyung Kim {
165825cdcb1SNamhyung Kim 	smp_mb();
1660ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1670ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_wait);
168825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
169825cdcb1SNamhyung Kim }
170825cdcb1SNamhyung Kim 
171932cc6d4SJens Axboe /**
172932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
173932cc6d4SJens Axboe  * @pipe:	pipe to fill
174932cc6d4SJens Axboe  * @spd:	data to fill
175932cc6d4SJens Axboe  *
176932cc6d4SJens Axboe  * Description:
17779685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
178932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
179932cc6d4SJens Axboe  *    function will link that data to the pipe.
180932cc6d4SJens Axboe  *
18183f9135bSJens Axboe  */
182d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1845274f052SJens Axboe {
18500de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
1868cefc107SDavid Howells 	unsigned int tail = pipe->tail;
1878cefc107SDavid Howells 	unsigned int head = pipe->head;
1888cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
1898924feffSAl Viro 	int ret = 0, page_nr = 0;
1905274f052SJens Axboe 
191d6785d91SRabin Vincent 	if (!spd_pages)
192d6785d91SRabin Vincent 		return 0;
193d6785d91SRabin Vincent 
1948924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1955274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1965274f052SJens Axboe 		ret = -EPIPE;
1978924feffSAl Viro 		goto out;
1985274f052SJens Axboe 	}
1995274f052SJens Axboe 
2006718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2018cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
2025274f052SJens Axboe 
203912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
204912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
205912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
206497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
207912d35f8SJens Axboe 		buf->ops = spd->ops;
2085a81e6a1SMiklos Szeredi 		buf->flags = 0;
2097afa6fd0SJens Axboe 
2108cefc107SDavid Howells 		head++;
2118cefc107SDavid Howells 		pipe->head = head;
212912d35f8SJens Axboe 		page_nr++;
213912d35f8SJens Axboe 		ret += buf->len;
214912d35f8SJens Axboe 
215912d35f8SJens Axboe 		if (!--spd->nr_pages)
2165274f052SJens Axboe 			break;
2175274f052SJens Axboe 	}
2185274f052SJens Axboe 
21929e35094SLinus Torvalds 	if (!ret)
22029e35094SLinus Torvalds 		ret = -EAGAIN;
22129e35094SLinus Torvalds 
2228924feffSAl Viro out:
22300de00bdSJens Axboe 	while (page_nr < spd_pages)
224bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2255274f052SJens Axboe 
2265274f052SJens Axboe 	return ret;
2275274f052SJens Axboe }
2282b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2295274f052SJens Axboe 
23079fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
23179fddc4eSAl Viro {
2328cefc107SDavid Howells 	unsigned int head = pipe->head;
2338cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2348cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
23579fddc4eSAl Viro 	int ret;
23679fddc4eSAl Viro 
23779fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23879fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
23979fddc4eSAl Viro 		ret = -EPIPE;
2406718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
24179fddc4eSAl Viro 		ret = -EAGAIN;
24279fddc4eSAl Viro 	} else {
2438cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2448cefc107SDavid Howells 		pipe->head = head + 1;
24579fddc4eSAl Viro 		return buf->len;
24679fddc4eSAl Viro 	}
247a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24879fddc4eSAl Viro 	return ret;
24979fddc4eSAl Viro }
25079fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
25179fddc4eSAl Viro 
25235f3d14dSJens Axboe /*
25335f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25435f3d14dSJens Axboe  * descriptions.
25535f3d14dSJens Axboe  */
256047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25735f3d14dSJens Axboe {
2586718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
259047fe360SEric Dumazet 
2608cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2618cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
26235f3d14dSJens Axboe 		return 0;
26335f3d14dSJens Axboe 
2648cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2658cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2666da2ec56SKees Cook 				     GFP_KERNEL);
26735f3d14dSJens Axboe 
26835f3d14dSJens Axboe 	if (spd->pages && spd->partial)
26935f3d14dSJens Axboe 		return 0;
27035f3d14dSJens Axboe 
27135f3d14dSJens Axboe 	kfree(spd->pages);
27235f3d14dSJens Axboe 	kfree(spd->partial);
27335f3d14dSJens Axboe 	return -ENOMEM;
27435f3d14dSJens Axboe }
27535f3d14dSJens Axboe 
276047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27735f3d14dSJens Axboe {
278047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
27935f3d14dSJens Axboe 		return;
28035f3d14dSJens Axboe 
28135f3d14dSJens Axboe 	kfree(spd->pages);
28235f3d14dSJens Axboe 	kfree(spd->partial);
28335f3d14dSJens Axboe }
28435f3d14dSJens Axboe 
28583f9135bSJens Axboe /**
28683f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
28783f9135bSJens Axboe  * @in:		file to splice from
288932cc6d4SJens Axboe  * @ppos:	position in @in
28983f9135bSJens Axboe  * @pipe:	pipe to splice to
29083f9135bSJens Axboe  * @len:	number of bytes to splice
29183f9135bSJens Axboe  * @flags:	splice modifier flags
29283f9135bSJens Axboe  *
293932cc6d4SJens Axboe  * Description:
294932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
29582c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
296932cc6d4SJens Axboe  *
29783f9135bSJens Axboe  */
298cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
299cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
300cbb7e577SJens Axboe 				 unsigned int flags)
3015274f052SJens Axboe {
30282c156f8SAl Viro 	struct iov_iter to;
30382c156f8SAl Viro 	struct kiocb kiocb;
3048cefc107SDavid Howells 	unsigned int i_head;
3058cefc107SDavid Howells 	int ret;
306be64f884SBoaz Harrosh 
307aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len);
3088cefc107SDavid Howells 	i_head = to.head;
30982c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
31082c156f8SAl Viro 	kiocb.ki_pos = *ppos;
311bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
312723590edSMiklos Szeredi 	if (ret > 0) {
31382c156f8SAl Viro 		*ppos = kiocb.ki_pos;
314723590edSMiklos Szeredi 		file_accessed(in);
31582c156f8SAl Viro 	} else if (ret < 0) {
3168cefc107SDavid Howells 		to.head = i_head;
31782c156f8SAl Viro 		to.iov_offset = 0;
31882c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
31982c156f8SAl Viro 		/*
32082c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
32182c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
32282c156f8SAl Viro 		 */
32382c156f8SAl Viro 		if (ret == -EFAULT)
32482c156f8SAl Viro 			ret = -EAGAIN;
325723590edSMiklos Szeredi 	}
3265274f052SJens Axboe 
3275274f052SJens Axboe 	return ret;
3285274f052SJens Axboe }
329059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
330059a8f37SJens Axboe 
331241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3326818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
333c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
3346818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
3356818173bSMiklos Szeredi };
3366818173bSMiklos Szeredi 
33728a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
33828a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
33928a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
34028a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
34128a625cbSMiklos Szeredi };
34228a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
34328a625cbSMiklos Szeredi 
344523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
3456818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
3466818173bSMiklos Szeredi {
3476818173bSMiklos Szeredi 	mm_segment_t old_fs;
3486818173bSMiklos Szeredi 	loff_t pos = offset;
3496818173bSMiklos Szeredi 	ssize_t res;
3506818173bSMiklos Szeredi 
3516818173bSMiklos Szeredi 	old_fs = get_fs();
352736706beSLinus Torvalds 	set_fs(KERNEL_DS);
3536818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
354793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
3556818173bSMiklos Szeredi 	set_fs(old_fs);
3566818173bSMiklos Szeredi 
3576818173bSMiklos Szeredi 	return res;
3586818173bSMiklos Szeredi }
3596818173bSMiklos Szeredi 
36082c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
3616818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
3626818173bSMiklos Szeredi 				 unsigned int flags)
3636818173bSMiklos Szeredi {
364523ac9afSAl Viro 	struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
365523ac9afSAl Viro 	struct iov_iter to;
366523ac9afSAl Viro 	struct page **pages;
3676818173bSMiklos Szeredi 	unsigned int nr_pages;
3688cefc107SDavid Howells 	unsigned int mask;
36913c0f52bSAl Viro 	size_t offset, base, copied = 0;
3706818173bSMiklos Szeredi 	ssize_t res;
3716818173bSMiklos Szeredi 	int i;
3726818173bSMiklos Szeredi 
3736718b6f8SDavid Howells 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
374523ac9afSAl Viro 		return -EAGAIN;
375523ac9afSAl Viro 
376523ac9afSAl Viro 	/*
377523ac9afSAl Viro 	 * Try to keep page boundaries matching to source pagecache ones -
378523ac9afSAl Viro 	 * it probably won't be much help, but...
379523ac9afSAl Viro 	 */
380523ac9afSAl Viro 	offset = *ppos & ~PAGE_MASK;
381523ac9afSAl Viro 
382aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len + offset);
383523ac9afSAl Viro 
38413c0f52bSAl Viro 	res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
385523ac9afSAl Viro 	if (res <= 0)
38635f3d14dSJens Axboe 		return -ENOMEM;
38735f3d14dSJens Axboe 
38813c0f52bSAl Viro 	nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
389523ac9afSAl Viro 
39035f3d14dSJens Axboe 	vec = __vec;
391523ac9afSAl Viro 	if (nr_pages > PIPE_DEF_BUFFERS) {
3926da2ec56SKees Cook 		vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
393523ac9afSAl Viro 		if (unlikely(!vec)) {
394523ac9afSAl Viro 			res = -ENOMEM;
395523ac9afSAl Viro 			goto out;
396523ac9afSAl Viro 		}
39735f3d14dSJens Axboe 	}
39835f3d14dSJens Axboe 
3998cefc107SDavid Howells 	mask = pipe->ring_size - 1;
4008cefc107SDavid Howells 	pipe->bufs[to.head & mask].offset = offset;
4018cefc107SDavid Howells 	pipe->bufs[to.head & mask].len -= offset;
4026818173bSMiklos Szeredi 
403523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++) {
404523ac9afSAl Viro 		size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
405523ac9afSAl Viro 		vec[i].iov_base = page_address(pages[i]) + offset;
4066818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
4076818173bSMiklos Szeredi 		len -= this_len;
4086818173bSMiklos Szeredi 		offset = 0;
4096818173bSMiklos Szeredi 	}
4106818173bSMiklos Szeredi 
411523ac9afSAl Viro 	res = kernel_readv(in, vec, nr_pages, *ppos);
412523ac9afSAl Viro 	if (res > 0) {
413523ac9afSAl Viro 		copied = res;
4146818173bSMiklos Szeredi 		*ppos += res;
415523ac9afSAl Viro 	}
4166818173bSMiklos Szeredi 
41735f3d14dSJens Axboe 	if (vec != __vec)
41835f3d14dSJens Axboe 		kfree(vec);
419523ac9afSAl Viro out:
420523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++)
421523ac9afSAl Viro 		put_page(pages[i]);
422523ac9afSAl Viro 	kvfree(pages);
423523ac9afSAl Viro 	iov_iter_advance(&to, copied);	/* truncates and discards */
4246818173bSMiklos Szeredi 	return res;
4256818173bSMiklos Szeredi }
4266818173bSMiklos Szeredi 
4275274f052SJens Axboe /*
4284f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
429016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4305274f052SJens Axboe  */
43176ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4325274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4335274f052SJens Axboe {
4346a14b90bSJens Axboe 	struct file *file = sd->u.file;
4355274f052SJens Axboe 	loff_t pos = sd->pos;
436a8adbe37SMichał Mirosław 	int more;
4375274f052SJens Axboe 
43872c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
439a8adbe37SMichał Mirosław 		return -EINVAL;
440a8adbe37SMichał Mirosław 
44135f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
442ae62ca7bSEric Dumazet 
4438cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4448cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
44535f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
446ae62ca7bSEric Dumazet 
447a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
448f84d7519SJens Axboe 				    sd->len, &pos, more);
4495274f052SJens Axboe }
4505274f052SJens Axboe 
451b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
452b3c2d2ddSMiklos Szeredi {
453b3c2d2ddSMiklos Szeredi 	smp_mb();
4540ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4550ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
456b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
457b3c2d2ddSMiklos Szeredi }
458b3c2d2ddSMiklos Szeredi 
459b3c2d2ddSMiklos Szeredi /**
460b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
461b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
462b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
463b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
464b3c2d2ddSMiklos Szeredi  *
465b3c2d2ddSMiklos Szeredi  * Description:
466b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
467b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
468b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
469b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
470b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
471b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
472b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
473b3c2d2ddSMiklos Szeredi  *
474b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
475b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
476b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
477b3c2d2ddSMiklos Szeredi  *    destination.
478b3c2d2ddSMiklos Szeredi  */
47996f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
480b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
481b3c2d2ddSMiklos Szeredi {
4828cefc107SDavid Howells 	unsigned int head = pipe->head;
4838cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4848cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
485b3c2d2ddSMiklos Szeredi 	int ret;
486b3c2d2ddSMiklos Szeredi 
487ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4888cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
489b3c2d2ddSMiklos Szeredi 
490b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
491b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
492b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
493b3c2d2ddSMiklos Szeredi 
494fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
495a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
496b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
497b3c2d2ddSMiklos Szeredi 				ret = 0;
498b3c2d2ddSMiklos Szeredi 			return ret;
499b3c2d2ddSMiklos Szeredi 		}
500a8adbe37SMichał Mirosław 
501a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
502a8adbe37SMichał Mirosław 		if (ret <= 0)
503a8adbe37SMichał Mirosław 			return ret;
504a8adbe37SMichał Mirosław 
505b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
506b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
507b3c2d2ddSMiklos Szeredi 
508b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
509b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
510b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
511b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
512b3c2d2ddSMiklos Szeredi 
513b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
514a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5158cefc107SDavid Howells 			tail++;
5168cefc107SDavid Howells 			pipe->tail = tail;
5176447a3cfSAl Viro 			if (pipe->files)
518b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
519b3c2d2ddSMiklos Szeredi 		}
520b3c2d2ddSMiklos Szeredi 
521b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
522b3c2d2ddSMiklos Szeredi 			return 0;
523b3c2d2ddSMiklos Szeredi 	}
524b3c2d2ddSMiklos Szeredi 
525b3c2d2ddSMiklos Szeredi 	return 1;
526b3c2d2ddSMiklos Szeredi }
527b3c2d2ddSMiklos Szeredi 
528d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
529d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
530d1a819a2SLinus Torvalds {
531d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
532d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
533d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
534d1a819a2SLinus Torvalds 
535d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
536d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
537d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
538d1a819a2SLinus Torvalds 		return true;
539d1a819a2SLinus Torvalds 	}
540d1a819a2SLinus Torvalds 
541d1a819a2SLinus Torvalds 	return false;
542d1a819a2SLinus Torvalds }
543d1a819a2SLinus Torvalds 
544b3c2d2ddSMiklos Szeredi /**
545b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
546b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
547b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
548b3c2d2ddSMiklos Szeredi  *
549b3c2d2ddSMiklos Szeredi  * Description:
550b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
551b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
552b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
553b3c2d2ddSMiklos Szeredi  */
55496f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
555b3c2d2ddSMiklos Szeredi {
556c725bfceSJan Kara 	/*
557c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
558c725bfceSJan Kara 	 * always buffers available
559c725bfceSJan Kara 	 */
560c725bfceSJan Kara 	if (signal_pending(current))
561c725bfceSJan Kara 		return -ERESTARTSYS;
562c725bfceSJan Kara 
563d1a819a2SLinus Torvalds repeat:
5648cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
565b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
566b3c2d2ddSMiklos Szeredi 			return 0;
567b3c2d2ddSMiklos Szeredi 
568a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
569b3c2d2ddSMiklos Szeredi 			return 0;
570b3c2d2ddSMiklos Szeredi 
571b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
572b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
573b3c2d2ddSMiklos Szeredi 
574b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
575b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
576b3c2d2ddSMiklos Szeredi 
577b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
578b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
579b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
580b3c2d2ddSMiklos Szeredi 		}
581b3c2d2ddSMiklos Szeredi 
582472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
583b3c2d2ddSMiklos Szeredi 	}
584b3c2d2ddSMiklos Szeredi 
585d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
586d1a819a2SLinus Torvalds 		goto repeat;
587d1a819a2SLinus Torvalds 
588b3c2d2ddSMiklos Szeredi 	return 1;
589b3c2d2ddSMiklos Szeredi }
590b3c2d2ddSMiklos Szeredi 
591b3c2d2ddSMiklos Szeredi /**
592b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
593b80901bbSRandy Dunlap  * @sd:		information about the splice operation
594b3c2d2ddSMiklos Szeredi  *
595b3c2d2ddSMiklos Szeredi  * Description:
596b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
597b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
598b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
599b3c2d2ddSMiklos Szeredi  */
60096f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
601b3c2d2ddSMiklos Szeredi {
602b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
603b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
604b3c2d2ddSMiklos Szeredi }
605b3c2d2ddSMiklos Szeredi 
606b3c2d2ddSMiklos Szeredi /**
607b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
608b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
609b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
610b3c2d2ddSMiklos Szeredi  *
611b3c2d2ddSMiklos Szeredi  * Description:
612b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
613b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
614b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
615b3c2d2ddSMiklos Szeredi  */
61696f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
617b3c2d2ddSMiklos Szeredi {
618b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
619b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
620b3c2d2ddSMiklos Szeredi }
621b3c2d2ddSMiklos Szeredi 
622932cc6d4SJens Axboe /**
623932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
624932cc6d4SJens Axboe  * @pipe:	pipe to splice from
625932cc6d4SJens Axboe  * @sd:		information to @actor
626932cc6d4SJens Axboe  * @actor:	handler that splices the data
627932cc6d4SJens Axboe  *
628932cc6d4SJens Axboe  * Description:
629932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
630932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
631932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
632932cc6d4SJens Axboe  *    pipe_to_user.
633932cc6d4SJens Axboe  *
63483f9135bSJens Axboe  */
635c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
636c66ab6faSJens Axboe 			   splice_actor *actor)
6375274f052SJens Axboe {
638b3c2d2ddSMiklos Szeredi 	int ret;
6395274f052SJens Axboe 
640b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
641b3c2d2ddSMiklos Szeredi 	do {
642c2489e07SJan Kara 		cond_resched();
643b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
644b3c2d2ddSMiklos Szeredi 		if (ret > 0)
645b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
646b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
647b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6485274f052SJens Axboe 
649b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6505274f052SJens Axboe }
65140bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6525274f052SJens Axboe 
653932cc6d4SJens Axboe /**
654932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
655932cc6d4SJens Axboe  * @pipe:	pipe to splice from
656932cc6d4SJens Axboe  * @out:	file to splice to
657932cc6d4SJens Axboe  * @ppos:	position in @out
658932cc6d4SJens Axboe  * @len:	how many bytes to splice
659932cc6d4SJens Axboe  * @flags:	splice modifier flags
660932cc6d4SJens Axboe  * @actor:	handler that splices the data
661932cc6d4SJens Axboe  *
662932cc6d4SJens Axboe  * Description:
6632933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
664932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
665932cc6d4SJens Axboe  *
666932cc6d4SJens Axboe  */
6676da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6686da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6696da61809SMark Fasheh 			 splice_actor *actor)
6706da61809SMark Fasheh {
6716da61809SMark Fasheh 	ssize_t ret;
672c66ab6faSJens Axboe 	struct splice_desc sd = {
673c66ab6faSJens Axboe 		.total_len = len,
674c66ab6faSJens Axboe 		.flags = flags,
675c66ab6faSJens Axboe 		.pos = *ppos,
6766a14b90bSJens Axboe 		.u.file = out,
677c66ab6faSJens Axboe 	};
6786da61809SMark Fasheh 
67961e0d47cSMiklos Szeredi 	pipe_lock(pipe);
680c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
68161e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6826da61809SMark Fasheh 
6836da61809SMark Fasheh 	return ret;
6846da61809SMark Fasheh }
6856da61809SMark Fasheh 
6866da61809SMark Fasheh /**
6878d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6888d020765SAl Viro  * @pipe:	pipe info
6898d020765SAl Viro  * @out:	file to write to
6908d020765SAl Viro  * @ppos:	position in @out
6918d020765SAl Viro  * @len:	number of bytes to splice
6928d020765SAl Viro  * @flags:	splice modifier flags
6938d020765SAl Viro  *
6948d020765SAl Viro  * Description:
6958d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6968d020765SAl Viro  *    the given pipe inode to the given file.
6978d020765SAl Viro  *    This one is ->write_iter-based.
6988d020765SAl Viro  *
6998d020765SAl Viro  */
7008d020765SAl Viro ssize_t
7018d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
7028d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
7038d020765SAl Viro {
7048d020765SAl Viro 	struct splice_desc sd = {
7058d020765SAl Viro 		.total_len = len,
7068d020765SAl Viro 		.flags = flags,
7078d020765SAl Viro 		.pos = *ppos,
7088d020765SAl Viro 		.u.file = out,
7098d020765SAl Viro 	};
7106718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
7118d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7128d020765SAl Viro 					GFP_KERNEL);
7138d020765SAl Viro 	ssize_t ret;
7148d020765SAl Viro 
7158d020765SAl Viro 	if (unlikely(!array))
7168d020765SAl Viro 		return -ENOMEM;
7178d020765SAl Viro 
7188d020765SAl Viro 	pipe_lock(pipe);
7198d020765SAl Viro 
7208d020765SAl Viro 	splice_from_pipe_begin(&sd);
7218d020765SAl Viro 	while (sd.total_len) {
7228d020765SAl Viro 		struct iov_iter from;
723ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7248d020765SAl Viro 		size_t left;
7258cefc107SDavid Howells 		int n;
7268d020765SAl Viro 
7278d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7288d020765SAl Viro 		if (ret <= 0)
7298d020765SAl Viro 			break;
7308d020765SAl Viro 
7316718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7328d020765SAl Viro 			kfree(array);
7336718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7348d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7358d020765SAl Viro 					GFP_KERNEL);
7368d020765SAl Viro 			if (!array) {
7378d020765SAl Viro 				ret = -ENOMEM;
7388d020765SAl Viro 				break;
7398d020765SAl Viro 			}
7408d020765SAl Viro 		}
7418d020765SAl Viro 
742ec057595SLinus Torvalds 		head = pipe->head;
743ec057595SLinus Torvalds 		tail = pipe->tail;
744ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
745ec057595SLinus Torvalds 
7468d020765SAl Viro 		/* build the vector */
7478d020765SAl Viro 		left = sd.total_len;
7488cefc107SDavid Howells 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
7498cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7508d020765SAl Viro 			size_t this_len = buf->len;
7518d020765SAl Viro 
7528d020765SAl Viro 			if (this_len > left)
7538d020765SAl Viro 				this_len = left;
7548d020765SAl Viro 
755fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7568d020765SAl Viro 			if (unlikely(ret)) {
7578d020765SAl Viro 				if (ret == -ENODATA)
7588d020765SAl Viro 					ret = 0;
7598d020765SAl Viro 				goto done;
7608d020765SAl Viro 			}
7618d020765SAl Viro 
7628d020765SAl Viro 			array[n].bv_page = buf->page;
7638d020765SAl Viro 			array[n].bv_len = this_len;
7648d020765SAl Viro 			array[n].bv_offset = buf->offset;
7658d020765SAl Viro 			left -= this_len;
7668d020765SAl Viro 		}
7678d020765SAl Viro 
768aa563d7bSDavid Howells 		iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
769abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7708d020765SAl Viro 		if (ret <= 0)
7718d020765SAl Viro 			break;
7728d020765SAl Viro 
7738d020765SAl Viro 		sd.num_spliced += ret;
7748d020765SAl Viro 		sd.total_len -= ret;
775dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7768d020765SAl Viro 
7778d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7788cefc107SDavid Howells 		tail = pipe->tail;
7798d020765SAl Viro 		while (ret) {
7808cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7818d020765SAl Viro 			if (ret >= buf->len) {
7828d020765SAl Viro 				ret -= buf->len;
7838d020765SAl Viro 				buf->len = 0;
784a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7858cefc107SDavid Howells 				tail++;
7868cefc107SDavid Howells 				pipe->tail = tail;
7878d020765SAl Viro 				if (pipe->files)
7888d020765SAl Viro 					sd.need_wakeup = true;
7898d020765SAl Viro 			} else {
7908d020765SAl Viro 				buf->offset += ret;
7918d020765SAl Viro 				buf->len -= ret;
7928d020765SAl Viro 				ret = 0;
7938d020765SAl Viro 			}
7948d020765SAl Viro 		}
7958d020765SAl Viro 	}
7968d020765SAl Viro done:
7978d020765SAl Viro 	kfree(array);
7988d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7998d020765SAl Viro 
8008d020765SAl Viro 	pipe_unlock(pipe);
8018d020765SAl Viro 
8028d020765SAl Viro 	if (sd.num_spliced)
8038d020765SAl Viro 		ret = sd.num_spliced;
8048d020765SAl Viro 
8058d020765SAl Viro 	return ret;
8068d020765SAl Viro }
8078d020765SAl Viro 
8088d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8098d020765SAl Viro 
810b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
811b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
8120b0a47f5SMiklos Szeredi {
813b2858d7dSMiklos Szeredi 	int ret;
814b2858d7dSMiklos Szeredi 	void *data;
81506ae43f3SAl Viro 	loff_t tmp = sd->pos;
816b2858d7dSMiklos Szeredi 
817fbb32750SAl Viro 	data = kmap(buf->page);
81806ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
819fbb32750SAl Viro 	kunmap(buf->page);
820b2858d7dSMiklos Szeredi 
821b2858d7dSMiklos Szeredi 	return ret;
8220b0a47f5SMiklos Szeredi }
8230b0a47f5SMiklos Szeredi 
8240b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
8250b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
8260b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
8270b0a47f5SMiklos Szeredi {
828b2858d7dSMiklos Szeredi 	ssize_t ret;
8290b0a47f5SMiklos Szeredi 
830b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
831b2858d7dSMiklos Szeredi 	if (ret > 0)
832b2858d7dSMiklos Szeredi 		*ppos += ret;
8330b0a47f5SMiklos Szeredi 
834b2858d7dSMiklos Szeredi 	return ret;
8350b0a47f5SMiklos Szeredi }
8360b0a47f5SMiklos Szeredi 
83783f9135bSJens Axboe /**
83883f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
839932cc6d4SJens Axboe  * @pipe:	pipe to splice from
84083f9135bSJens Axboe  * @out:	socket to write to
841932cc6d4SJens Axboe  * @ppos:	position in @out
84283f9135bSJens Axboe  * @len:	number of bytes to splice
84383f9135bSJens Axboe  * @flags:	splice modifier flags
84483f9135bSJens Axboe  *
845932cc6d4SJens Axboe  * Description:
84683f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
84783f9135bSJens Axboe  *    is involved.
84883f9135bSJens Axboe  *
84983f9135bSJens Axboe  */
8503a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
851cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8525274f052SJens Axboe {
85300522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8545274f052SJens Axboe }
8555274f052SJens Axboe 
856059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
857a0f06780SJeff Garzik 
85883f9135bSJens Axboe /*
85983f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
86083f9135bSJens Axboe  */
8613a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
862cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8635274f052SJens Axboe {
86472c2d531SAl Viro 	if (out->f_op->splice_write)
86500c285d0SChristoph Hellwig 		return out->f_op->splice_write(pipe, out, ppos, len, flags);
86600c285d0SChristoph Hellwig 	return default_file_splice_write(pipe, out, ppos, len, flags);
8675274f052SJens Axboe }
8685274f052SJens Axboe 
86983f9135bSJens Axboe /*
87083f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
87183f9135bSJens Axboe  */
872cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
873cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
874cbb7e577SJens Axboe 			 unsigned int flags)
8755274f052SJens Axboe {
8765274f052SJens Axboe 	int ret;
8775274f052SJens Axboe 
87849570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8795274f052SJens Axboe 		return -EBADF;
8805274f052SJens Axboe 
881cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8825274f052SJens Axboe 	if (unlikely(ret < 0))
8835274f052SJens Axboe 		return ret;
8845274f052SJens Axboe 
88503cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
88603cc0789SAl Viro 		len = MAX_RW_COUNT;
88703cc0789SAl Viro 
88872c2d531SAl Viro 	if (in->f_op->splice_read)
8892bc01060SChristoph Hellwig 		return in->f_op->splice_read(in, ppos, pipe, len, flags);
8902bc01060SChristoph Hellwig 	return default_file_splice_read(in, ppos, pipe, len, flags);
8915274f052SJens Axboe }
8925274f052SJens Axboe 
893932cc6d4SJens Axboe /**
894932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
895932cc6d4SJens Axboe  * @in:		file to splice from
896932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
897932cc6d4SJens Axboe  * @actor:	handles the data splicing
898932cc6d4SJens Axboe  *
899932cc6d4SJens Axboe  * Description:
900932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
901932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
902932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
903932cc6d4SJens Axboe  *    that process.
904932cc6d4SJens Axboe  *
905c66ab6faSJens Axboe  */
906c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
907c66ab6faSJens Axboe 			       splice_direct_actor *actor)
908b92ce558SJens Axboe {
909b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
910b92ce558SJens Axboe 	long ret, bytes;
911b92ce558SJens Axboe 	umode_t i_mode;
912c66ab6faSJens Axboe 	size_t len;
9130ff28d9fSChristophe Leroy 	int i, flags, more;
914b92ce558SJens Axboe 
915b92ce558SJens Axboe 	/*
916b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
917b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
918b92ce558SJens Axboe 	 * piped splicing for that!
919b92ce558SJens Axboe 	 */
920496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
921b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
922b92ce558SJens Axboe 		return -EINVAL;
923b92ce558SJens Axboe 
924b92ce558SJens Axboe 	/*
925b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
926b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
927b92ce558SJens Axboe 	 */
928b92ce558SJens Axboe 	pipe = current->splice_pipe;
92949570e9bSJens Axboe 	if (unlikely(!pipe)) {
9307bee130eSAl Viro 		pipe = alloc_pipe_info();
931b92ce558SJens Axboe 		if (!pipe)
932b92ce558SJens Axboe 			return -ENOMEM;
933b92ce558SJens Axboe 
934b92ce558SJens Axboe 		/*
935b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
93600522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
937b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
938b92ce558SJens Axboe 		 */
939b92ce558SJens Axboe 		pipe->readers = 1;
940b92ce558SJens Axboe 
941b92ce558SJens Axboe 		current->splice_pipe = pipe;
942b92ce558SJens Axboe 	}
943b92ce558SJens Axboe 
944b92ce558SJens Axboe 	/*
94573d62d83SIngo Molnar 	 * Do the splice.
946b92ce558SJens Axboe 	 */
947b92ce558SJens Axboe 	ret = 0;
948b92ce558SJens Axboe 	bytes = 0;
949c66ab6faSJens Axboe 	len = sd->total_len;
950c66ab6faSJens Axboe 	flags = sd->flags;
951c66ab6faSJens Axboe 
952c66ab6faSJens Axboe 	/*
953c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
954c66ab6faSJens Axboe 	 */
955c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9560ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
957b92ce558SJens Axboe 
9588cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
95917614445SDarrick J. Wong 
960b92ce558SJens Axboe 	while (len) {
9618cefc107SDavid Howells 		unsigned int p_space;
96251a92c0fSJens Axboe 		size_t read_len;
963a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
964b92ce558SJens Axboe 
96517614445SDarrick J. Wong 		/* Don't try to read more the pipe has space for. */
9666718b6f8SDavid Howells 		p_space = pipe->max_usage -
9678cefc107SDavid Howells 			pipe_occupancy(pipe->head, pipe->tail);
9688cefc107SDavid Howells 		read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
96917614445SDarrick J. Wong 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
97051a92c0fSJens Axboe 		if (unlikely(ret <= 0))
971b92ce558SJens Axboe 			goto out_release;
972b92ce558SJens Axboe 
973b92ce558SJens Axboe 		read_len = ret;
974c66ab6faSJens Axboe 		sd->total_len = read_len;
975b92ce558SJens Axboe 
976b92ce558SJens Axboe 		/*
9770ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9780ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9790ff28d9fSChristophe Leroy 		 * initially, clears it.
9800ff28d9fSChristophe Leroy 		 */
9810ff28d9fSChristophe Leroy 		if (read_len < len)
9820ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9830ff28d9fSChristophe Leroy 		else if (!more)
9840ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9850ff28d9fSChristophe Leroy 		/*
986b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
987b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
988b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
989b92ce558SJens Axboe 		 */
990c66ab6faSJens Axboe 		ret = actor(pipe, sd);
991a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
992a82c53a0STom Zanussi 			sd->pos = prev_pos;
993b92ce558SJens Axboe 			goto out_release;
994a82c53a0STom Zanussi 		}
995b92ce558SJens Axboe 
996b92ce558SJens Axboe 		bytes += ret;
997b92ce558SJens Axboe 		len -= ret;
998bcd4f3acSJens Axboe 		sd->pos = pos;
999b92ce558SJens Axboe 
1000a82c53a0STom Zanussi 		if (ret < read_len) {
1001a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
100251a92c0fSJens Axboe 			goto out_release;
1003b92ce558SJens Axboe 		}
1004a82c53a0STom Zanussi 	}
1005b92ce558SJens Axboe 
10069e97198dSJens Axboe done:
10078cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
10089e97198dSJens Axboe 	file_accessed(in);
1009b92ce558SJens Axboe 	return bytes;
1010b92ce558SJens Axboe 
1011b92ce558SJens Axboe out_release:
1012b92ce558SJens Axboe 	/*
1013b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1014b92ce558SJens Axboe 	 * the pipe buffers in question:
1015b92ce558SJens Axboe 	 */
10168cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
10178cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1018b92ce558SJens Axboe 
1019a779638cSMiklos Szeredi 		if (buf->ops)
1020a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1021b92ce558SJens Axboe 	}
1022b92ce558SJens Axboe 
10239e97198dSJens Axboe 	if (!bytes)
10249e97198dSJens Axboe 		bytes = ret;
1025b92ce558SJens Axboe 
10269e97198dSJens Axboe 	goto done;
1027c66ab6faSJens Axboe }
1028c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1029c66ab6faSJens Axboe 
1030c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1031c66ab6faSJens Axboe 			       struct splice_desc *sd)
1032c66ab6faSJens Axboe {
10336a14b90bSJens Axboe 	struct file *file = sd->u.file;
1034c66ab6faSJens Axboe 
10357995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10362cb4b05eSChangli Gao 			      sd->flags);
1037c66ab6faSJens Axboe }
1038c66ab6faSJens Axboe 
1039932cc6d4SJens Axboe /**
1040932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1041932cc6d4SJens Axboe  * @in:		file to splice from
1042932cc6d4SJens Axboe  * @ppos:	input file offset
1043932cc6d4SJens Axboe  * @out:	file to splice to
1044acdb37c3SRandy Dunlap  * @opos:	output file offset
1045932cc6d4SJens Axboe  * @len:	number of bytes to splice
1046932cc6d4SJens Axboe  * @flags:	splice modifier flags
1047932cc6d4SJens Axboe  *
1048932cc6d4SJens Axboe  * Description:
1049932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1050932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1051932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1052932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1053932cc6d4SJens Axboe  *
1054932cc6d4SJens Axboe  */
1055c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10567995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1057c66ab6faSJens Axboe {
1058c66ab6faSJens Axboe 	struct splice_desc sd = {
1059c66ab6faSJens Axboe 		.len		= len,
1060c66ab6faSJens Axboe 		.total_len	= len,
1061c66ab6faSJens Axboe 		.flags		= flags,
1062c66ab6faSJens Axboe 		.pos		= *ppos,
10636a14b90bSJens Axboe 		.u.file		= out,
10647995bd28SAl Viro 		.opos		= opos,
1065c66ab6faSJens Axboe 	};
106651a92c0fSJens Axboe 	long ret;
1067c66ab6faSJens Axboe 
106818c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
106918c67cb9SAl Viro 		return -EBADF;
107018c67cb9SAl Viro 
107118c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
107218c67cb9SAl Viro 		return -EINVAL;
107318c67cb9SAl Viro 
107418c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
107518c67cb9SAl Viro 	if (unlikely(ret < 0))
107618c67cb9SAl Viro 		return ret;
107718c67cb9SAl Viro 
1078c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
107951a92c0fSJens Axboe 	if (ret > 0)
1080a82c53a0STom Zanussi 		*ppos = sd.pos;
108151a92c0fSJens Axboe 
1082c66ab6faSJens Axboe 	return ret;
1083b92ce558SJens Axboe }
10841c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1085b92ce558SJens Axboe 
10868924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10878924feffSAl Viro {
108852bce911SLinus Torvalds 	for (;;) {
108952bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
109052bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
109152bce911SLinus Torvalds 			return -EPIPE;
109252bce911SLinus Torvalds 		}
10936718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
109452bce911SLinus Torvalds 			return 0;
10958924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10968924feffSAl Viro 			return -EAGAIN;
10978924feffSAl Viro 		if (signal_pending(current))
10988924feffSAl Viro 			return -ERESTARTSYS;
1099472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
11008924feffSAl Viro 	}
11018924feffSAl Viro }
11028924feffSAl Viro 
11037c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
11047c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
11057c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1106ddac0d39SJens Axboe 
1107ddac0d39SJens Axboe /*
110883f9135bSJens Axboe  * Determine where to splice to/from.
110983f9135bSJens Axboe  */
1110ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1111ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
11125274f052SJens Axboe {
11137c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11147c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11157995bd28SAl Viro 	loff_t offset;
1116a4514ebdSJens Axboe 	long ret;
11175274f052SJens Axboe 
111890da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
111990da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
112090da2e3fSPavel Begunkov 		return -EBADF;
112190da2e3fSPavel Begunkov 
1122c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1123c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
11247c77f0b3SMiklos Szeredi 
11257c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11267c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11277c77f0b3SMiklos Szeredi 			return -ESPIPE;
11287c77f0b3SMiklos Szeredi 
11297c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11307c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11317c77f0b3SMiklos Szeredi 			return -EINVAL;
11327c77f0b3SMiklos Szeredi 
1133ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1134ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1135ee5e0011SSlavomir Kaslev 
11367c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11377c77f0b3SMiklos Szeredi 	}
11387c77f0b3SMiklos Szeredi 
11397c77f0b3SMiklos Szeredi 	if (ipipe) {
1140529565dcSIngo Molnar 		if (off_in)
1141529565dcSIngo Molnar 			return -ESPIPE;
1142b92ce558SJens Axboe 		if (off_out) {
114319c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1144b92ce558SJens Axboe 				return -EINVAL;
1145ee6e00c8SJens Axboe 			offset = *off_out;
11467995bd28SAl Viro 		} else {
11477995bd28SAl Viro 			offset = out->f_pos;
11487995bd28SAl Viro 		}
1149529565dcSIngo Molnar 
115018c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
115118c67cb9SAl Viro 			return -EINVAL;
115218c67cb9SAl Viro 
115318c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
115418c67cb9SAl Viro 		if (unlikely(ret < 0))
115518c67cb9SAl Viro 			return ret;
115618c67cb9SAl Viro 
1157ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1158ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1159ee5e0011SSlavomir Kaslev 
1160500368f7SAl Viro 		file_start_write(out);
11617995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1162500368f7SAl Viro 		file_end_write(out);
1163a4514ebdSJens Axboe 
11647995bd28SAl Viro 		if (!off_out)
11657995bd28SAl Viro 			out->f_pos = offset;
1166ee6e00c8SJens Axboe 		else
1167ee6e00c8SJens Axboe 			*off_out = offset;
1168a4514ebdSJens Axboe 
1169a4514ebdSJens Axboe 		return ret;
1170529565dcSIngo Molnar 	}
11715274f052SJens Axboe 
11727c77f0b3SMiklos Szeredi 	if (opipe) {
1173529565dcSIngo Molnar 		if (off_out)
1174529565dcSIngo Molnar 			return -ESPIPE;
1175b92ce558SJens Axboe 		if (off_in) {
117619c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1177b92ce558SJens Axboe 				return -EINVAL;
1178ee6e00c8SJens Axboe 			offset = *off_in;
11797995bd28SAl Viro 		} else {
11807995bd28SAl Viro 			offset = in->f_pos;
11817995bd28SAl Viro 		}
1182529565dcSIngo Molnar 
1183ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1184ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1185ee5e0011SSlavomir Kaslev 
11868924feffSAl Viro 		pipe_lock(opipe);
11878924feffSAl Viro 		ret = wait_for_space(opipe, flags);
11883253d9d0SDarrick J. Wong 		if (!ret) {
11896a965666SLinus Torvalds 			unsigned int p_space;
11903253d9d0SDarrick J. Wong 
11913253d9d0SDarrick J. Wong 			/* Don't try to read more the pipe has space for. */
11926a965666SLinus Torvalds 			p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail);
11936a965666SLinus Torvalds 			len = min_t(size_t, len, p_space << PAGE_SHIFT);
11943253d9d0SDarrick J. Wong 
11957995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
11963253d9d0SDarrick J. Wong 		}
11978924feffSAl Viro 		pipe_unlock(opipe);
11988924feffSAl Viro 		if (ret > 0)
11998924feffSAl Viro 			wakeup_pipe_readers(opipe);
12007995bd28SAl Viro 		if (!off_in)
12017995bd28SAl Viro 			in->f_pos = offset;
1202ee6e00c8SJens Axboe 		else
1203ee6e00c8SJens Axboe 			*off_in = offset;
1204a4514ebdSJens Axboe 
1205a4514ebdSJens Axboe 		return ret;
1206529565dcSIngo Molnar 	}
12075274f052SJens Axboe 
12085274f052SJens Axboe 	return -EINVAL;
12095274f052SJens Axboe }
12105274f052SJens Axboe 
1211ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1212ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1213ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1214ee6e00c8SJens Axboe {
1215ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1216ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1217ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1218ee6e00c8SJens Axboe 	long ret;
1219ee6e00c8SJens Axboe 
1220ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1221ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1222ee6e00c8SJens Axboe 
1223ee6e00c8SJens Axboe 	if (ipipe && off_in)
1224ee6e00c8SJens Axboe 		return -ESPIPE;
1225ee6e00c8SJens Axboe 	if (opipe && off_out)
1226ee6e00c8SJens Axboe 		return -ESPIPE;
1227ee6e00c8SJens Axboe 
1228ee6e00c8SJens Axboe 	if (off_out) {
1229ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1230ee6e00c8SJens Axboe 			return -EFAULT;
1231ee6e00c8SJens Axboe 		__off_out = &offset;
1232ee6e00c8SJens Axboe 	}
1233ee6e00c8SJens Axboe 	if (off_in) {
1234ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1235ee6e00c8SJens Axboe 			return -EFAULT;
1236ee6e00c8SJens Axboe 		__off_in = &offset;
1237ee6e00c8SJens Axboe 	}
1238ee6e00c8SJens Axboe 
1239ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1240ee6e00c8SJens Axboe 	if (ret < 0)
1241ee6e00c8SJens Axboe 		return ret;
1242ee6e00c8SJens Axboe 
1243ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1244ee6e00c8SJens Axboe 		return -EFAULT;
1245ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1246ee6e00c8SJens Axboe 		return -EFAULT;
1247ee6e00c8SJens Axboe 
1248ee6e00c8SJens Axboe 	return ret;
1249ee6e00c8SJens Axboe }
1250ee6e00c8SJens Axboe 
125179fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
125279fddc4eSAl Viro 			struct pipe_inode_info *pipe,
125379fddc4eSAl Viro 			unsigned flags)
1254912d35f8SJens Axboe {
125579fddc4eSAl Viro 	struct pipe_buffer buf = {
125679fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
125779fddc4eSAl Viro 		.flags = flags
125879fddc4eSAl Viro 	};
125979fddc4eSAl Viro 	size_t total = 0;
126079fddc4eSAl Viro 	int ret = 0;
126179fddc4eSAl Viro 	bool failed = false;
126279fddc4eSAl Viro 
126379fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
126479fddc4eSAl Viro 		struct page *pages[16];
1265db85a9ebSAl Viro 		ssize_t copied;
1266db85a9ebSAl Viro 		size_t start;
126779fddc4eSAl Viro 		int n;
1268912d35f8SJens Axboe 
126979fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
127079fddc4eSAl Viro 		if (copied <= 0) {
127179fddc4eSAl Viro 			ret = copied;
127279fddc4eSAl Viro 			break;
127379fddc4eSAl Viro 		}
1274912d35f8SJens Axboe 
127579fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1276db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
127779fddc4eSAl Viro 			if (!failed) {
127879fddc4eSAl Viro 				buf.page = pages[n];
127979fddc4eSAl Viro 				buf.offset = start;
128079fddc4eSAl Viro 				buf.len = size;
128179fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
128279fddc4eSAl Viro 				if (unlikely(ret < 0)) {
128379fddc4eSAl Viro 					failed = true;
128479fddc4eSAl Viro 				} else {
128579fddc4eSAl Viro 					iov_iter_advance(from, ret);
128679fddc4eSAl Viro 					total += ret;
128779fddc4eSAl Viro 				}
128879fddc4eSAl Viro 			} else {
128979fddc4eSAl Viro 				put_page(pages[n]);
129079fddc4eSAl Viro 			}
1291db85a9ebSAl Viro 			copied -= size;
1292912d35f8SJens Axboe 		}
1293912d35f8SJens Axboe 	}
129479fddc4eSAl Viro 	return total ? total : ret;
1295912d35f8SJens Axboe }
1296912d35f8SJens Axboe 
12976a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12986a14b90bSJens Axboe 			struct splice_desc *sd)
12996a14b90bSJens Axboe {
13006130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
13016130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
13026a14b90bSJens Axboe }
13036a14b90bSJens Axboe 
13046a14b90bSJens Axboe /*
13056a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
13066a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
13076a14b90bSJens Axboe  */
130887a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
130987a3002aSAl Viro 			     unsigned int flags)
13106a14b90bSJens Axboe {
1311c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
131287a3002aSAl Viro 	struct splice_desc sd = {
131387a3002aSAl Viro 		.total_len = iov_iter_count(iter),
131487a3002aSAl Viro 		.flags = flags,
131587a3002aSAl Viro 		.u.data = iter
131687a3002aSAl Viro 	};
131787a3002aSAl Viro 	long ret = 0;
13186a14b90bSJens Axboe 
13196a14b90bSJens Axboe 	if (!pipe)
13206a14b90bSJens Axboe 		return -EBADF;
13216a14b90bSJens Axboe 
1322345995faSAl Viro 	if (sd.total_len) {
13236130f531SAl Viro 		pipe_lock(pipe);
13246130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
132561e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1326345995faSAl Viro 	}
13276a14b90bSJens Axboe 
13286a14b90bSJens Axboe 	return ret;
13296a14b90bSJens Axboe }
13306a14b90bSJens Axboe 
1331912d35f8SJens Axboe /*
1332912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1333912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1334912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1335912d35f8SJens Axboe  */
133687a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
133787a3002aSAl Viro 			     unsigned int flags)
1338912d35f8SJens Axboe {
1339ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
134087a3002aSAl Viro 	long ret = 0;
134179fddc4eSAl Viro 	unsigned buf_flag = 0;
134279fddc4eSAl Viro 
134379fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
134479fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1345912d35f8SJens Axboe 
1346c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1347ddac0d39SJens Axboe 	if (!pipe)
1348912d35f8SJens Axboe 		return -EBADF;
1349912d35f8SJens Axboe 
13508924feffSAl Viro 	pipe_lock(pipe);
13518924feffSAl Viro 	ret = wait_for_space(pipe, flags);
135279fddc4eSAl Viro 	if (!ret)
135387a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13548924feffSAl Viro 	pipe_unlock(pipe);
13558924feffSAl Viro 	if (ret > 0)
13568924feffSAl Viro 		wakeup_pipe_readers(pipe);
135735f3d14dSJens Axboe 	return ret;
1358912d35f8SJens Axboe }
1359912d35f8SJens Axboe 
136087a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
136187a3002aSAl Viro {
136287a3002aSAl Viro 	if (!f.file)
136387a3002aSAl Viro 		return -EBADF;
136487a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
136587a3002aSAl Viro 		*type = WRITE;
136687a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
136787a3002aSAl Viro 		*type = READ;
136887a3002aSAl Viro 	} else {
136987a3002aSAl Viro 		fdput(f);
137087a3002aSAl Viro 		return -EBADF;
137187a3002aSAl Viro 	}
137287a3002aSAl Viro 	return 0;
137387a3002aSAl Viro }
137487a3002aSAl Viro 
13756a14b90bSJens Axboe /*
13766a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13776a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13786a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13796a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13806a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13816a14b90bSJens Axboe  * solutions for that:
13826a14b90bSJens Axboe  *
13836a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13846a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13856a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13866a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13876a14b90bSJens Axboe  *
13886a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13896a14b90bSJens Axboe  *
13906a14b90bSJens Axboe  */
139187a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
139230cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
139330cfe4efSDominik Brodowski {
139487a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
139587a3002aSAl Viro 	struct iovec *iov = iovstack;
139687a3002aSAl Viro 	struct iov_iter iter;
139787e5e6daSJens Axboe 	ssize_t error;
139887a3002aSAl Viro 	struct fd f;
139987a3002aSAl Viro 	int type;
140087a3002aSAl Viro 
1401598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1402598b3cecSChristoph Hellwig 		return -EINVAL;
1403598b3cecSChristoph Hellwig 
140487a3002aSAl Viro 	f = fdget(fd);
140587a3002aSAl Viro 	error = vmsplice_type(f, &type);
140687a3002aSAl Viro 	if (error)
140787a3002aSAl Viro 		return error;
140887a3002aSAl Viro 
140987a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
141087a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1411598b3cecSChristoph Hellwig 	if (error < 0)
1412598b3cecSChristoph Hellwig 		goto out_fdput;
1413598b3cecSChristoph Hellwig 
1414598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1415598b3cecSChristoph Hellwig 		error = 0;
1416598b3cecSChristoph Hellwig 	else if (iov_iter_rw(&iter) == WRITE)
1417598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1418598b3cecSChristoph Hellwig 	else
1419598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1420598b3cecSChristoph Hellwig 
142187a3002aSAl Viro 	kfree(iov);
1422598b3cecSChristoph Hellwig out_fdput:
142387a3002aSAl Viro 	fdput(f);
142487a3002aSAl Viro 	return error;
142530cfe4efSDominik Brodowski }
142630cfe4efSDominik Brodowski 
1427836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1428836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1429836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14305274f052SJens Axboe {
14312903ff01SAl Viro 	struct fd in, out;
14325274f052SJens Axboe 	long error;
14335274f052SJens Axboe 
14345274f052SJens Axboe 	if (unlikely(!len))
14355274f052SJens Axboe 		return 0;
14365274f052SJens Axboe 
14373d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14383d6ea290SAl Viro 		return -EINVAL;
14393d6ea290SAl Viro 
14405274f052SJens Axboe 	error = -EBADF;
14412903ff01SAl Viro 	in = fdget(fd_in);
14422903ff01SAl Viro 	if (in.file) {
14432903ff01SAl Viro 		out = fdget(fd_out);
14442903ff01SAl Viro 		if (out.file) {
1445ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1446529565dcSIngo Molnar 						len, flags);
14472903ff01SAl Viro 			fdput(out);
14485274f052SJens Axboe 		}
14492903ff01SAl Viro 		fdput(in);
14505274f052SJens Axboe 	}
14515274f052SJens Axboe 	return error;
14525274f052SJens Axboe }
145370524490SJens Axboe 
145470524490SJens Axboe /*
1455aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1456aadd06e5SJens Axboe  * return an appropriate error.
1457aadd06e5SJens Axboe  */
14587c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1459aadd06e5SJens Axboe {
1460aadd06e5SJens Axboe 	int ret;
1461aadd06e5SJens Axboe 
1462aadd06e5SJens Axboe 	/*
14638cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1464aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1465aadd06e5SJens Axboe 	 */
14668cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1467aadd06e5SJens Axboe 		return 0;
1468aadd06e5SJens Axboe 
1469aadd06e5SJens Axboe 	ret = 0;
147061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1471aadd06e5SJens Axboe 
14728cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1473aadd06e5SJens Axboe 		if (signal_pending(current)) {
1474aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1475aadd06e5SJens Axboe 			break;
1476aadd06e5SJens Axboe 		}
1477aadd06e5SJens Axboe 		if (!pipe->writers)
1478aadd06e5SJens Axboe 			break;
1479aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1480aadd06e5SJens Axboe 			ret = -EAGAIN;
1481aadd06e5SJens Axboe 			break;
1482aadd06e5SJens Axboe 		}
1483472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1484aadd06e5SJens Axboe 	}
1485aadd06e5SJens Axboe 
148661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1487aadd06e5SJens Axboe 	return ret;
1488aadd06e5SJens Axboe }
1489aadd06e5SJens Axboe 
1490aadd06e5SJens Axboe /*
1491aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1492aadd06e5SJens Axboe  * return an appropriate error.
1493aadd06e5SJens Axboe  */
14947c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1495aadd06e5SJens Axboe {
1496aadd06e5SJens Axboe 	int ret;
1497aadd06e5SJens Axboe 
1498aadd06e5SJens Axboe 	/*
14998cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1500aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1501aadd06e5SJens Axboe 	 */
1502566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1503aadd06e5SJens Axboe 		return 0;
1504aadd06e5SJens Axboe 
1505aadd06e5SJens Axboe 	ret = 0;
150661e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1507aadd06e5SJens Axboe 
15086718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1509aadd06e5SJens Axboe 		if (!pipe->readers) {
1510aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1511aadd06e5SJens Axboe 			ret = -EPIPE;
1512aadd06e5SJens Axboe 			break;
1513aadd06e5SJens Axboe 		}
1514aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1515aadd06e5SJens Axboe 			ret = -EAGAIN;
1516aadd06e5SJens Axboe 			break;
1517aadd06e5SJens Axboe 		}
1518aadd06e5SJens Axboe 		if (signal_pending(current)) {
1519aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1520aadd06e5SJens Axboe 			break;
1521aadd06e5SJens Axboe 		}
1522472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1523aadd06e5SJens Axboe 	}
1524aadd06e5SJens Axboe 
152561e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1526aadd06e5SJens Axboe 	return ret;
1527aadd06e5SJens Axboe }
1528aadd06e5SJens Axboe 
1529aadd06e5SJens Axboe /*
15307c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15317c77f0b3SMiklos Szeredi  */
15327c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15337c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15347c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15357c77f0b3SMiklos Szeredi {
15367c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15378cefc107SDavid Howells 	unsigned int i_head, o_head;
15388cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15398cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15408cefc107SDavid Howells 	int ret = 0;
15417c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15427c77f0b3SMiklos Szeredi 
15437c77f0b3SMiklos Szeredi 
15447c77f0b3SMiklos Szeredi retry:
15457c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15467c77f0b3SMiklos Szeredi 	if (ret)
15477c77f0b3SMiklos Szeredi 		return ret;
15487c77f0b3SMiklos Szeredi 
15497c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15507c77f0b3SMiklos Szeredi 	if (ret)
15517c77f0b3SMiklos Szeredi 		return ret;
15527c77f0b3SMiklos Szeredi 
15537c77f0b3SMiklos Szeredi 	/*
15547c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15557c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15567c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15577c77f0b3SMiklos Szeredi 	 */
15587c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15597c77f0b3SMiklos Szeredi 
15608cefc107SDavid Howells 	i_tail = ipipe->tail;
15618cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15628cefc107SDavid Howells 	o_head = opipe->head;
15638cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15648cefc107SDavid Howells 
15657c77f0b3SMiklos Szeredi 	do {
15668cefc107SDavid Howells 		size_t o_len;
15678cefc107SDavid Howells 
15687c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15697c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15707c77f0b3SMiklos Szeredi 			if (!ret)
15717c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15727c77f0b3SMiklos Szeredi 			break;
15737c77f0b3SMiklos Szeredi 		}
15747c77f0b3SMiklos Szeredi 
15758cefc107SDavid Howells 		i_head = ipipe->head;
15768cefc107SDavid Howells 		o_tail = opipe->tail;
15778cefc107SDavid Howells 
15788cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
15797c77f0b3SMiklos Szeredi 			break;
15807c77f0b3SMiklos Szeredi 
15817c77f0b3SMiklos Szeredi 		/*
15827c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15837c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15847c77f0b3SMiklos Szeredi 		 */
15858cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
15866718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
15877c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15887c77f0b3SMiklos Szeredi 			if (ret)
15897c77f0b3SMiklos Szeredi 				break;
15907c77f0b3SMiklos Szeredi 
15917c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15927c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15937c77f0b3SMiklos Szeredi 				break;
15947c77f0b3SMiklos Szeredi 			}
15957c77f0b3SMiklos Szeredi 
15967c77f0b3SMiklos Szeredi 			/*
15977c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15987c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15997c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
16007c77f0b3SMiklos Szeredi 			 */
16017c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
16027c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
16037c77f0b3SMiklos Szeredi 			goto retry;
16047c77f0b3SMiklos Szeredi 		}
16057c77f0b3SMiklos Szeredi 
16068cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16078cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
16087c77f0b3SMiklos Szeredi 
16097c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
16107c77f0b3SMiklos Szeredi 			/*
16117c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
16127c77f0b3SMiklos Szeredi 			 */
16137c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16147c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
16158cefc107SDavid Howells 			i_tail++;
16168cefc107SDavid Howells 			ipipe->tail = i_tail;
16177c77f0b3SMiklos Szeredi 			input_wakeup = true;
16188cefc107SDavid Howells 			o_len = obuf->len;
16198cefc107SDavid Howells 			o_head++;
16208cefc107SDavid Howells 			opipe->head = o_head;
16217c77f0b3SMiklos Szeredi 		} else {
16227c77f0b3SMiklos Szeredi 			/*
16237c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16247c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16257c77f0b3SMiklos Szeredi 			 */
162615fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
162715fab63eSMatthew Wilcox 				if (ret == 0)
162815fab63eSMatthew Wilcox 					ret = -EFAULT;
162915fab63eSMatthew Wilcox 				break;
163015fab63eSMatthew Wilcox 			}
16317c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16327c77f0b3SMiklos Szeredi 
16337c77f0b3SMiklos Szeredi 			/*
1634f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
16357c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16367c77f0b3SMiklos Szeredi 			 */
16377c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1638f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1639a0ce2f0aSJann Horn 
16407c77f0b3SMiklos Szeredi 			obuf->len = len;
16418cefc107SDavid Howells 			ibuf->offset += len;
16428cefc107SDavid Howells 			ibuf->len -= len;
16438cefc107SDavid Howells 			o_len = len;
16448cefc107SDavid Howells 			o_head++;
16458cefc107SDavid Howells 			opipe->head = o_head;
16467c77f0b3SMiklos Szeredi 		}
16478cefc107SDavid Howells 		ret += o_len;
16488cefc107SDavid Howells 		len -= o_len;
16497c77f0b3SMiklos Szeredi 	} while (len);
16507c77f0b3SMiklos Szeredi 
16517c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16527c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16537c77f0b3SMiklos Szeredi 
16547c77f0b3SMiklos Szeredi 	/*
16557c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16567c77f0b3SMiklos Szeredi 	 */
1657825cdcb1SNamhyung Kim 	if (ret > 0)
1658825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1659825cdcb1SNamhyung Kim 
16607c77f0b3SMiklos Szeredi 	if (input_wakeup)
16617c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16627c77f0b3SMiklos Szeredi 
16637c77f0b3SMiklos Szeredi 	return ret;
16647c77f0b3SMiklos Szeredi }
16657c77f0b3SMiklos Szeredi 
16667c77f0b3SMiklos Szeredi /*
166770524490SJens Axboe  * Link contents of ipipe to opipe.
166870524490SJens Axboe  */
166970524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
167070524490SJens Axboe 		     struct pipe_inode_info *opipe,
167170524490SJens Axboe 		     size_t len, unsigned int flags)
167270524490SJens Axboe {
167370524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
16748cefc107SDavid Howells 	unsigned int i_head, o_head;
16758cefc107SDavid Howells 	unsigned int i_tail, o_tail;
16768cefc107SDavid Howells 	unsigned int i_mask, o_mask;
16778cefc107SDavid Howells 	int ret = 0;
167870524490SJens Axboe 
167970524490SJens Axboe 	/*
168070524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
168161e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
168270524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
168370524490SJens Axboe 	 */
168461e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
168570524490SJens Axboe 
16868cefc107SDavid Howells 	i_tail = ipipe->tail;
16878cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
16888cefc107SDavid Howells 	o_head = opipe->head;
16898cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
16908cefc107SDavid Howells 
1691aadd06e5SJens Axboe 	do {
169270524490SJens Axboe 		if (!opipe->readers) {
169370524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
169470524490SJens Axboe 			if (!ret)
169570524490SJens Axboe 				ret = -EPIPE;
169670524490SJens Axboe 			break;
169770524490SJens Axboe 		}
169870524490SJens Axboe 
16998cefc107SDavid Howells 		i_head = ipipe->head;
17008cefc107SDavid Howells 		o_tail = opipe->tail;
17018cefc107SDavid Howells 
170270524490SJens Axboe 		/*
17038cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1704aadd06e5SJens Axboe 		 * output room, break.
170570524490SJens Axboe 		 */
17068cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17076718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1708aadd06e5SJens Axboe 			break;
1709aadd06e5SJens Axboe 
17108cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17118cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
171270524490SJens Axboe 
171370524490SJens Axboe 		/*
171470524490SJens Axboe 		 * Get a reference to this pipe buffer,
171570524490SJens Axboe 		 * so we can copy the contents over.
171670524490SJens Axboe 		 */
171715fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
171815fab63eSMatthew Wilcox 			if (ret == 0)
171915fab63eSMatthew Wilcox 				ret = -EFAULT;
172015fab63eSMatthew Wilcox 			break;
172115fab63eSMatthew Wilcox 		}
172270524490SJens Axboe 
172370524490SJens Axboe 		*obuf = *ibuf;
172470524490SJens Axboe 
17257afa6fd0SJens Axboe 		/*
1726f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1727f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
17287afa6fd0SJens Axboe 		 */
17297afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1730f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1731a0ce2f0aSJann Horn 
173270524490SJens Axboe 		if (obuf->len > len)
173370524490SJens Axboe 			obuf->len = len;
173470524490SJens Axboe 		ret += obuf->len;
173570524490SJens Axboe 		len -= obuf->len;
17368cefc107SDavid Howells 
17378cefc107SDavid Howells 		o_head++;
17388cefc107SDavid Howells 		opipe->head = o_head;
17398cefc107SDavid Howells 		i_tail++;
1740aadd06e5SJens Axboe 	} while (len);
174170524490SJens Axboe 
174261e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
174361e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
174470524490SJens Axboe 
1745aadd06e5SJens Axboe 	/*
1746aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1747aadd06e5SJens Axboe 	 */
1748825cdcb1SNamhyung Kim 	if (ret > 0)
1749825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
175070524490SJens Axboe 
175170524490SJens Axboe 	return ret;
175270524490SJens Axboe }
175370524490SJens Axboe 
175470524490SJens Axboe /*
175570524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
175670524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
175770524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
175870524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
175970524490SJens Axboe  */
17609dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
176170524490SJens Axboe {
1762c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1763c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1764aadd06e5SJens Axboe 	int ret = -EINVAL;
176570524490SJens Axboe 
176690da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
176790da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
176890da2e3fSPavel Begunkov 		return -EBADF;
176990da2e3fSPavel Begunkov 
177070524490SJens Axboe 	/*
1771aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1772aadd06e5SJens Axboe 	 * copying the data.
177370524490SJens Axboe 	 */
1774aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1775ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1776ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1777ee5e0011SSlavomir Kaslev 
1778aadd06e5SJens Axboe 		/*
1779aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1780aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1781aadd06e5SJens Axboe 		 */
17827c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1783aadd06e5SJens Axboe 		if (!ret) {
17847c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
178502cf01aeSJens Axboe 			if (!ret)
1786aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1787aadd06e5SJens Axboe 		}
1788aadd06e5SJens Axboe 	}
178970524490SJens Axboe 
1790aadd06e5SJens Axboe 	return ret;
179170524490SJens Axboe }
179270524490SJens Axboe 
1793836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
179470524490SJens Axboe {
179590da2e3fSPavel Begunkov 	struct fd in, out;
17962903ff01SAl Viro 	int error;
179770524490SJens Axboe 
17983d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
17993d6ea290SAl Viro 		return -EINVAL;
18003d6ea290SAl Viro 
180170524490SJens Axboe 	if (unlikely(!len))
180270524490SJens Axboe 		return 0;
180370524490SJens Axboe 
180470524490SJens Axboe 	error = -EBADF;
18052903ff01SAl Viro 	in = fdget(fdin);
18062903ff01SAl Viro 	if (in.file) {
180790da2e3fSPavel Begunkov 		out = fdget(fdout);
18082903ff01SAl Viro 		if (out.file) {
180990da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
18102903ff01SAl Viro 			fdput(out);
181170524490SJens Axboe 		}
18122903ff01SAl Viro  		fdput(in);
181370524490SJens Axboe  	}
181470524490SJens Axboe 
181570524490SJens Axboe 	return error;
181670524490SJens Axboe }
1817