xref: /openbmc/linux/fs/splice.c (revision abbb6589)
15274f052SJens Axboe /*
25274f052SJens Axboe  * "splice": joining two ropes together by interweaving their strands.
35274f052SJens Axboe  *
45274f052SJens Axboe  * This is the "extended pipe" functionality, where a pipe is used as
55274f052SJens Axboe  * an arbitrary in-memory buffer. Think of a pipe as a small kernel
65274f052SJens Axboe  * buffer that you can use to transfer data from one end to the other.
75274f052SJens Axboe  *
85274f052SJens Axboe  * The traditional unix read/write is extended with a "splice()" operation
95274f052SJens Axboe  * that transfers data buffers to or from a pipe buffer.
105274f052SJens Axboe  *
115274f052SJens Axboe  * Named by Larry McVoy, original implementation from Linus, extended by
12c2058e06SJens Axboe  * Jens to support splicing to files, network, direct splicing, etc and
13c2058e06SJens Axboe  * fixing lots of bugs.
145274f052SJens Axboe  *
150fe23479SJens Axboe  * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
16c2058e06SJens Axboe  * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17c2058e06SJens Axboe  * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
185274f052SJens Axboe  *
195274f052SJens Axboe  */
20be297968SChristoph Hellwig #include <linux/bvec.h>
215274f052SJens Axboe #include <linux/fs.h>
225274f052SJens Axboe #include <linux/file.h>
235274f052SJens Axboe #include <linux/pagemap.h>
24d6b29d7cSJens Axboe #include <linux/splice.h>
2508e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h>
265274f052SJens Axboe #include <linux/mm_inline.h>
275abc97aaSJens Axboe #include <linux/swap.h>
284f6f0bd2SJens Axboe #include <linux/writeback.h>
29630d9c47SPaul Gortmaker #include <linux/export.h>
304f6f0bd2SJens Axboe #include <linux/syscalls.h>
31912d35f8SJens Axboe #include <linux/uio.h>
3229ce2058SJames Morris #include <linux/security.h>
335a0e3ad6STejun Heo #include <linux/gfp.h>
3435f9c09fSEric Dumazet #include <linux/socket.h>
3576b021d0SAl Viro #include <linux/compat.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  */
4676ad4d11SJens Axboe static int page_cache_pipe_buf_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;
785abc97aaSJens Axboe 			return 0;
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);
889e94cd4fSJens Axboe 	return 1;
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 = {
1415274f052SJens Axboe 	.can_merge = 0,
142cac36bb0SJens Axboe 	.confirm = page_cache_pipe_buf_confirm,
1435274f052SJens Axboe 	.release = page_cache_pipe_buf_release,
1445abc97aaSJens Axboe 	.steal = page_cache_pipe_buf_steal,
145f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
1465274f052SJens Axboe };
1475274f052SJens Axboe 
148912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
149912d35f8SJens Axboe 				    struct pipe_buffer *buf)
150912d35f8SJens Axboe {
1517afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152912d35f8SJens Axboe 		return 1;
1537afa6fd0SJens Axboe 
1541432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
155330ab716SJens Axboe 	return generic_pipe_buf_steal(pipe, buf);
156912d35f8SJens Axboe }
157912d35f8SJens Axboe 
158d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159912d35f8SJens Axboe 	.can_merge = 0,
160cac36bb0SJens Axboe 	.confirm = generic_pipe_buf_confirm,
161912d35f8SJens Axboe 	.release = page_cache_pipe_buf_release,
162912d35f8SJens Axboe 	.steal = user_page_pipe_buf_steal,
163f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
164912d35f8SJens Axboe };
165912d35f8SJens Axboe 
166825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
167825cdcb1SNamhyung Kim {
168825cdcb1SNamhyung Kim 	smp_mb();
169825cdcb1SNamhyung Kim 	if (waitqueue_active(&pipe->wait))
170825cdcb1SNamhyung Kim 		wake_up_interruptible(&pipe->wait);
171825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
172825cdcb1SNamhyung Kim }
173825cdcb1SNamhyung Kim 
174932cc6d4SJens Axboe /**
175932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
176932cc6d4SJens Axboe  * @pipe:	pipe to fill
177932cc6d4SJens Axboe  * @spd:	data to fill
178932cc6d4SJens Axboe  *
179932cc6d4SJens Axboe  * Description:
18079685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
181932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
182932cc6d4SJens Axboe  *    function will link that data to the pipe.
183932cc6d4SJens Axboe  *
18483f9135bSJens Axboe  */
185d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
186912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1875274f052SJens Axboe {
18800de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
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 
2008924feffSAl Viro 	while (pipe->nrbufs < pipe->buffers) {
20135f3d14dSJens Axboe 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
2023a326a2cSIngo Molnar 		struct pipe_buffer *buf = pipe->bufs + newbuf;
2035274f052SJens Axboe 
204912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
205912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
206912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
207497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
208912d35f8SJens Axboe 		buf->ops = spd->ops;
2095a81e6a1SMiklos Szeredi 		buf->flags = 0;
2107afa6fd0SJens Axboe 
2116f767b04SJens Axboe 		pipe->nrbufs++;
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 {
23279fddc4eSAl Viro 	int ret;
23379fddc4eSAl Viro 
23479fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23579fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
23679fddc4eSAl Viro 		ret = -EPIPE;
23779fddc4eSAl Viro 	} else if (pipe->nrbufs == pipe->buffers) {
23879fddc4eSAl Viro 		ret = -EAGAIN;
23979fddc4eSAl Viro 	} else {
24079fddc4eSAl Viro 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
24179fddc4eSAl Viro 		pipe->bufs[newbuf] = *buf;
24279fddc4eSAl Viro 		pipe->nrbufs++;
24379fddc4eSAl Viro 		return buf->len;
24479fddc4eSAl Viro 	}
245a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24679fddc4eSAl Viro 	return ret;
24779fddc4eSAl Viro }
24879fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
24979fddc4eSAl Viro 
25035f3d14dSJens Axboe /*
25135f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25235f3d14dSJens Axboe  * descriptions.
25335f3d14dSJens Axboe  */
254047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25535f3d14dSJens Axboe {
256047fe360SEric Dumazet 	unsigned int buffers = ACCESS_ONCE(pipe->buffers);
257047fe360SEric Dumazet 
258047fe360SEric Dumazet 	spd->nr_pages_max = buffers;
259047fe360SEric Dumazet 	if (buffers <= PIPE_DEF_BUFFERS)
26035f3d14dSJens Axboe 		return 0;
26135f3d14dSJens Axboe 
262047fe360SEric Dumazet 	spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
263047fe360SEric Dumazet 	spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
26435f3d14dSJens Axboe 
26535f3d14dSJens Axboe 	if (spd->pages && spd->partial)
26635f3d14dSJens Axboe 		return 0;
26735f3d14dSJens Axboe 
26835f3d14dSJens Axboe 	kfree(spd->pages);
26935f3d14dSJens Axboe 	kfree(spd->partial);
27035f3d14dSJens Axboe 	return -ENOMEM;
27135f3d14dSJens Axboe }
27235f3d14dSJens Axboe 
273047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27435f3d14dSJens Axboe {
275047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
27635f3d14dSJens Axboe 		return;
27735f3d14dSJens Axboe 
27835f3d14dSJens Axboe 	kfree(spd->pages);
27935f3d14dSJens Axboe 	kfree(spd->partial);
28035f3d14dSJens Axboe }
28135f3d14dSJens Axboe 
28283f9135bSJens Axboe /**
28383f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
28483f9135bSJens Axboe  * @in:		file to splice from
285932cc6d4SJens Axboe  * @ppos:	position in @in
28683f9135bSJens Axboe  * @pipe:	pipe to splice to
28783f9135bSJens Axboe  * @len:	number of bytes to splice
28883f9135bSJens Axboe  * @flags:	splice modifier flags
28983f9135bSJens Axboe  *
290932cc6d4SJens Axboe  * Description:
291932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
29282c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
293932cc6d4SJens Axboe  *
29483f9135bSJens Axboe  */
295cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
296cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
297cbb7e577SJens Axboe 				 unsigned int flags)
2985274f052SJens Axboe {
29982c156f8SAl Viro 	struct iov_iter to;
30082c156f8SAl Viro 	struct kiocb kiocb;
30182c156f8SAl Viro 	int idx, ret;
302be64f884SBoaz Harrosh 
30382c156f8SAl Viro 	iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
30482c156f8SAl Viro 	idx = to.idx;
30582c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
30682c156f8SAl Viro 	kiocb.ki_pos = *ppos;
307bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
308723590edSMiklos Szeredi 	if (ret > 0) {
30982c156f8SAl Viro 		*ppos = kiocb.ki_pos;
310723590edSMiklos Szeredi 		file_accessed(in);
31182c156f8SAl Viro 	} else if (ret < 0) {
31282c156f8SAl Viro 		to.idx = idx;
31382c156f8SAl Viro 		to.iov_offset = 0;
31482c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
31582c156f8SAl Viro 		/*
31682c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
31782c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
31882c156f8SAl Viro 		 */
31982c156f8SAl Viro 		if (ret == -EFAULT)
32082c156f8SAl Viro 			ret = -EAGAIN;
321723590edSMiklos Szeredi 	}
3225274f052SJens Axboe 
3235274f052SJens Axboe 	return ret;
3245274f052SJens Axboe }
325059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
326059a8f37SJens Axboe 
327241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3286818173bSMiklos Szeredi 	.can_merge = 0,
3296818173bSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
3306818173bSMiklos Szeredi 	.release = generic_pipe_buf_release,
3316818173bSMiklos Szeredi 	.steal = generic_pipe_buf_steal,
3326818173bSMiklos Szeredi 	.get = generic_pipe_buf_get,
3336818173bSMiklos Szeredi };
3346818173bSMiklos Szeredi 
33528a625cbSMiklos Szeredi static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
33628a625cbSMiklos Szeredi 				    struct pipe_buffer *buf)
33728a625cbSMiklos Szeredi {
33828a625cbSMiklos Szeredi 	return 1;
33928a625cbSMiklos Szeredi }
34028a625cbSMiklos Szeredi 
34128a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
34228a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
34328a625cbSMiklos Szeredi 	.can_merge = 0,
34428a625cbSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
34528a625cbSMiklos Szeredi 	.release = generic_pipe_buf_release,
34628a625cbSMiklos Szeredi 	.steal = generic_pipe_buf_nosteal,
34728a625cbSMiklos Szeredi 	.get = generic_pipe_buf_get,
34828a625cbSMiklos Szeredi };
34928a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
35028a625cbSMiklos Szeredi 
351523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
3526818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
3536818173bSMiklos Szeredi {
3546818173bSMiklos Szeredi 	mm_segment_t old_fs;
3556818173bSMiklos Szeredi 	loff_t pos = offset;
3566818173bSMiklos Szeredi 	ssize_t res;
3576818173bSMiklos Szeredi 
3586818173bSMiklos Szeredi 	old_fs = get_fs();
3596818173bSMiklos Szeredi 	set_fs(get_ds());
3606818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
361793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
3626818173bSMiklos Szeredi 	set_fs(old_fs);
3636818173bSMiklos Szeredi 
3646818173bSMiklos Szeredi 	return res;
3656818173bSMiklos Szeredi }
3666818173bSMiklos Szeredi 
3677bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count,
368b2858d7dSMiklos Szeredi 			    loff_t pos)
3690b0a47f5SMiklos Szeredi {
3700b0a47f5SMiklos Szeredi 	mm_segment_t old_fs;
3710b0a47f5SMiklos Szeredi 	ssize_t res;
3720b0a47f5SMiklos Szeredi 
3730b0a47f5SMiklos Szeredi 	old_fs = get_fs();
3740b0a47f5SMiklos Szeredi 	set_fs(get_ds());
3750b0a47f5SMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
3767bb307e8SAl Viro 	res = vfs_write(file, (__force const char __user *)buf, count, &pos);
3770b0a47f5SMiklos Szeredi 	set_fs(old_fs);
3780b0a47f5SMiklos Szeredi 
3790b0a47f5SMiklos Szeredi 	return res;
3800b0a47f5SMiklos Szeredi }
3817bb307e8SAl Viro EXPORT_SYMBOL(kernel_write);
3820b0a47f5SMiklos Szeredi 
38382c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
3846818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
3856818173bSMiklos Szeredi 				 unsigned int flags)
3866818173bSMiklos Szeredi {
387523ac9afSAl Viro 	struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
388523ac9afSAl Viro 	struct iov_iter to;
389523ac9afSAl Viro 	struct page **pages;
3906818173bSMiklos Szeredi 	unsigned int nr_pages;
39113c0f52bSAl Viro 	size_t offset, base, copied = 0;
3926818173bSMiklos Szeredi 	ssize_t res;
3936818173bSMiklos Szeredi 	int i;
3946818173bSMiklos Szeredi 
395523ac9afSAl Viro 	if (pipe->nrbufs == pipe->buffers)
396523ac9afSAl Viro 		return -EAGAIN;
397523ac9afSAl Viro 
398523ac9afSAl Viro 	/*
399523ac9afSAl Viro 	 * Try to keep page boundaries matching to source pagecache ones -
400523ac9afSAl Viro 	 * it probably won't be much help, but...
401523ac9afSAl Viro 	 */
402523ac9afSAl Viro 	offset = *ppos & ~PAGE_MASK;
403523ac9afSAl Viro 
404523ac9afSAl Viro 	iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
405523ac9afSAl Viro 
40613c0f52bSAl Viro 	res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
407523ac9afSAl Viro 	if (res <= 0)
40835f3d14dSJens Axboe 		return -ENOMEM;
40935f3d14dSJens Axboe 
41013c0f52bSAl Viro 	nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
411523ac9afSAl Viro 
41235f3d14dSJens Axboe 	vec = __vec;
413523ac9afSAl Viro 	if (nr_pages > PIPE_DEF_BUFFERS) {
414523ac9afSAl Viro 		vec = kmalloc(nr_pages * sizeof(struct kvec), GFP_KERNEL);
415523ac9afSAl Viro 		if (unlikely(!vec)) {
416523ac9afSAl Viro 			res = -ENOMEM;
417523ac9afSAl Viro 			goto out;
418523ac9afSAl Viro 		}
41935f3d14dSJens Axboe 	}
42035f3d14dSJens Axboe 
421523ac9afSAl Viro 	pipe->bufs[to.idx].offset = offset;
422523ac9afSAl Viro 	pipe->bufs[to.idx].len -= offset;
4236818173bSMiklos Szeredi 
424523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++) {
425523ac9afSAl Viro 		size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
426523ac9afSAl Viro 		vec[i].iov_base = page_address(pages[i]) + offset;
4276818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
4286818173bSMiklos Szeredi 		len -= this_len;
4296818173bSMiklos Szeredi 		offset = 0;
4306818173bSMiklos Szeredi 	}
4316818173bSMiklos Szeredi 
432523ac9afSAl Viro 	res = kernel_readv(in, vec, nr_pages, *ppos);
433523ac9afSAl Viro 	if (res > 0) {
434523ac9afSAl Viro 		copied = res;
4356818173bSMiklos Szeredi 		*ppos += res;
436523ac9afSAl Viro 	}
4376818173bSMiklos Szeredi 
43835f3d14dSJens Axboe 	if (vec != __vec)
43935f3d14dSJens Axboe 		kfree(vec);
440523ac9afSAl Viro out:
441523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++)
442523ac9afSAl Viro 		put_page(pages[i]);
443523ac9afSAl Viro 	kvfree(pages);
444523ac9afSAl Viro 	iov_iter_advance(&to, copied);	/* truncates and discards */
4456818173bSMiklos Szeredi 	return res;
4466818173bSMiklos Szeredi }
4476818173bSMiklos Szeredi 
4485274f052SJens Axboe /*
4494f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
450016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4515274f052SJens Axboe  */
45276ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4535274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4545274f052SJens Axboe {
4556a14b90bSJens Axboe 	struct file *file = sd->u.file;
4565274f052SJens Axboe 	loff_t pos = sd->pos;
457a8adbe37SMichał Mirosław 	int more;
4585274f052SJens Axboe 
45972c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
460a8adbe37SMichał Mirosław 		return -EINVAL;
461a8adbe37SMichał Mirosław 
46235f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
463ae62ca7bSEric Dumazet 
464ae62ca7bSEric Dumazet 	if (sd->len < sd->total_len && pipe->nrbufs > 1)
46535f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
466ae62ca7bSEric Dumazet 
467a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
468f84d7519SJens Axboe 				    sd->len, &pos, more);
4695274f052SJens Axboe }
4705274f052SJens Axboe 
471b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
472b3c2d2ddSMiklos Szeredi {
473b3c2d2ddSMiklos Szeredi 	smp_mb();
474b3c2d2ddSMiklos Szeredi 	if (waitqueue_active(&pipe->wait))
475b3c2d2ddSMiklos Szeredi 		wake_up_interruptible(&pipe->wait);
476b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
477b3c2d2ddSMiklos Szeredi }
478b3c2d2ddSMiklos Szeredi 
479b3c2d2ddSMiklos Szeredi /**
480b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
481b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
482b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
483b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
484b3c2d2ddSMiklos Szeredi  *
485b3c2d2ddSMiklos Szeredi  * Description:
486b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
487b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
488b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
489b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
490b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
491b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
492b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
493b3c2d2ddSMiklos Szeredi  *
494b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
495b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
496b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
497b3c2d2ddSMiklos Szeredi  *    destination.
498b3c2d2ddSMiklos Szeredi  */
49996f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
500b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
501b3c2d2ddSMiklos Szeredi {
502b3c2d2ddSMiklos Szeredi 	int ret;
503b3c2d2ddSMiklos Szeredi 
504b3c2d2ddSMiklos Szeredi 	while (pipe->nrbufs) {
505b3c2d2ddSMiklos Szeredi 		struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
506b3c2d2ddSMiklos Szeredi 
507b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
508b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
509b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
510b3c2d2ddSMiklos Szeredi 
511fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
512a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
513b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
514b3c2d2ddSMiklos Szeredi 				ret = 0;
515b3c2d2ddSMiklos Szeredi 			return ret;
516b3c2d2ddSMiklos Szeredi 		}
517a8adbe37SMichał Mirosław 
518a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
519a8adbe37SMichał Mirosław 		if (ret <= 0)
520a8adbe37SMichał Mirosław 			return ret;
521a8adbe37SMichał Mirosław 
522b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
523b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
524b3c2d2ddSMiklos Szeredi 
525b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
526b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
527b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
528b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
529b3c2d2ddSMiklos Szeredi 
530b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
531a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
53235f3d14dSJens Axboe 			pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
533b3c2d2ddSMiklos Szeredi 			pipe->nrbufs--;
5346447a3cfSAl Viro 			if (pipe->files)
535b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
536b3c2d2ddSMiklos Szeredi 		}
537b3c2d2ddSMiklos Szeredi 
538b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
539b3c2d2ddSMiklos Szeredi 			return 0;
540b3c2d2ddSMiklos Szeredi 	}
541b3c2d2ddSMiklos Szeredi 
542b3c2d2ddSMiklos Szeredi 	return 1;
543b3c2d2ddSMiklos Szeredi }
544b3c2d2ddSMiklos Szeredi 
545b3c2d2ddSMiklos Szeredi /**
546b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
547b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
548b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
549b3c2d2ddSMiklos Szeredi  *
550b3c2d2ddSMiklos Szeredi  * Description:
551b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
552b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
553b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
554b3c2d2ddSMiklos Szeredi  */
55596f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
556b3c2d2ddSMiklos Szeredi {
557c725bfceSJan Kara 	/*
558c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
559c725bfceSJan Kara 	 * always buffers available
560c725bfceSJan Kara 	 */
561c725bfceSJan Kara 	if (signal_pending(current))
562c725bfceSJan Kara 		return -ERESTARTSYS;
563c725bfceSJan Kara 
564b3c2d2ddSMiklos Szeredi 	while (!pipe->nrbufs) {
565b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
566b3c2d2ddSMiklos Szeredi 			return 0;
567b3c2d2ddSMiklos Szeredi 
568b3c2d2ddSMiklos Szeredi 		if (!pipe->waiting_writers && 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 
582b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
583b3c2d2ddSMiklos Szeredi 	}
584b3c2d2ddSMiklos Szeredi 
585b3c2d2ddSMiklos Szeredi 	return 1;
586b3c2d2ddSMiklos Szeredi }
587b3c2d2ddSMiklos Szeredi 
588b3c2d2ddSMiklos Szeredi /**
589b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
590b80901bbSRandy Dunlap  * @sd:		information about the splice operation
591b3c2d2ddSMiklos Szeredi  *
592b3c2d2ddSMiklos Szeredi  * Description:
593b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
594b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
595b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
596b3c2d2ddSMiklos Szeredi  */
59796f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
598b3c2d2ddSMiklos Szeredi {
599b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
600b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
601b3c2d2ddSMiklos Szeredi }
602b3c2d2ddSMiklos Szeredi 
603b3c2d2ddSMiklos Szeredi /**
604b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
605b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
606b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
607b3c2d2ddSMiklos Szeredi  *
608b3c2d2ddSMiklos Szeredi  * Description:
609b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
610b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
611b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
612b3c2d2ddSMiklos Szeredi  */
61396f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
614b3c2d2ddSMiklos Szeredi {
615b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
616b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
617b3c2d2ddSMiklos Szeredi }
618b3c2d2ddSMiklos Szeredi 
619932cc6d4SJens Axboe /**
620932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
621932cc6d4SJens Axboe  * @pipe:	pipe to splice from
622932cc6d4SJens Axboe  * @sd:		information to @actor
623932cc6d4SJens Axboe  * @actor:	handler that splices the data
624932cc6d4SJens Axboe  *
625932cc6d4SJens Axboe  * Description:
626932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
627932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
628932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
629932cc6d4SJens Axboe  *    pipe_to_user.
630932cc6d4SJens Axboe  *
63183f9135bSJens Axboe  */
632c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
633c66ab6faSJens Axboe 			   splice_actor *actor)
6345274f052SJens Axboe {
635b3c2d2ddSMiklos Szeredi 	int ret;
6365274f052SJens Axboe 
637b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
638b3c2d2ddSMiklos Szeredi 	do {
639c2489e07SJan Kara 		cond_resched();
640b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
641b3c2d2ddSMiklos Szeredi 		if (ret > 0)
642b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
643b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
644b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6455274f052SJens Axboe 
646b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6475274f052SJens Axboe }
64840bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6495274f052SJens Axboe 
650932cc6d4SJens Axboe /**
651932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
652932cc6d4SJens Axboe  * @pipe:	pipe to splice from
653932cc6d4SJens Axboe  * @out:	file to splice to
654932cc6d4SJens Axboe  * @ppos:	position in @out
655932cc6d4SJens Axboe  * @len:	how many bytes to splice
656932cc6d4SJens Axboe  * @flags:	splice modifier flags
657932cc6d4SJens Axboe  * @actor:	handler that splices the data
658932cc6d4SJens Axboe  *
659932cc6d4SJens Axboe  * Description:
6602933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
661932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
662932cc6d4SJens Axboe  *
663932cc6d4SJens Axboe  */
6646da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6656da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6666da61809SMark Fasheh 			 splice_actor *actor)
6676da61809SMark Fasheh {
6686da61809SMark Fasheh 	ssize_t ret;
669c66ab6faSJens Axboe 	struct splice_desc sd = {
670c66ab6faSJens Axboe 		.total_len = len,
671c66ab6faSJens Axboe 		.flags = flags,
672c66ab6faSJens Axboe 		.pos = *ppos,
6736a14b90bSJens Axboe 		.u.file = out,
674c66ab6faSJens Axboe 	};
6756da61809SMark Fasheh 
67661e0d47cSMiklos Szeredi 	pipe_lock(pipe);
677c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
67861e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6796da61809SMark Fasheh 
6806da61809SMark Fasheh 	return ret;
6816da61809SMark Fasheh }
6826da61809SMark Fasheh 
6836da61809SMark Fasheh /**
6848d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6858d020765SAl Viro  * @pipe:	pipe info
6868d020765SAl Viro  * @out:	file to write to
6878d020765SAl Viro  * @ppos:	position in @out
6888d020765SAl Viro  * @len:	number of bytes to splice
6898d020765SAl Viro  * @flags:	splice modifier flags
6908d020765SAl Viro  *
6918d020765SAl Viro  * Description:
6928d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6938d020765SAl Viro  *    the given pipe inode to the given file.
6948d020765SAl Viro  *    This one is ->write_iter-based.
6958d020765SAl Viro  *
6968d020765SAl Viro  */
6978d020765SAl Viro ssize_t
6988d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6998d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
7008d020765SAl Viro {
7018d020765SAl Viro 	struct splice_desc sd = {
7028d020765SAl Viro 		.total_len = len,
7038d020765SAl Viro 		.flags = flags,
7048d020765SAl Viro 		.pos = *ppos,
7058d020765SAl Viro 		.u.file = out,
7068d020765SAl Viro 	};
7078d020765SAl Viro 	int nbufs = pipe->buffers;
7088d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7098d020765SAl Viro 					GFP_KERNEL);
7108d020765SAl Viro 	ssize_t ret;
7118d020765SAl Viro 
7128d020765SAl Viro 	if (unlikely(!array))
7138d020765SAl Viro 		return -ENOMEM;
7148d020765SAl Viro 
7158d020765SAl Viro 	pipe_lock(pipe);
7168d020765SAl Viro 
7178d020765SAl Viro 	splice_from_pipe_begin(&sd);
7188d020765SAl Viro 	while (sd.total_len) {
7198d020765SAl Viro 		struct iov_iter from;
7208d020765SAl Viro 		size_t left;
7218d020765SAl Viro 		int n, idx;
7228d020765SAl Viro 
7238d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7248d020765SAl Viro 		if (ret <= 0)
7258d020765SAl Viro 			break;
7268d020765SAl Viro 
7278d020765SAl Viro 		if (unlikely(nbufs < pipe->buffers)) {
7288d020765SAl Viro 			kfree(array);
7298d020765SAl Viro 			nbufs = pipe->buffers;
7308d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7318d020765SAl Viro 					GFP_KERNEL);
7328d020765SAl Viro 			if (!array) {
7338d020765SAl Viro 				ret = -ENOMEM;
7348d020765SAl Viro 				break;
7358d020765SAl Viro 			}
7368d020765SAl Viro 		}
7378d020765SAl Viro 
7388d020765SAl Viro 		/* build the vector */
7398d020765SAl Viro 		left = sd.total_len;
7408d020765SAl Viro 		for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
7418d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + idx;
7428d020765SAl Viro 			size_t this_len = buf->len;
7438d020765SAl Viro 
7448d020765SAl Viro 			if (this_len > left)
7458d020765SAl Viro 				this_len = left;
7468d020765SAl Viro 
7478d020765SAl Viro 			if (idx == pipe->buffers - 1)
7488d020765SAl Viro 				idx = -1;
7498d020765SAl Viro 
750fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7518d020765SAl Viro 			if (unlikely(ret)) {
7528d020765SAl Viro 				if (ret == -ENODATA)
7538d020765SAl Viro 					ret = 0;
7548d020765SAl Viro 				goto done;
7558d020765SAl Viro 			}
7568d020765SAl Viro 
7578d020765SAl Viro 			array[n].bv_page = buf->page;
7588d020765SAl Viro 			array[n].bv_len = this_len;
7598d020765SAl Viro 			array[n].bv_offset = buf->offset;
7608d020765SAl Viro 			left -= this_len;
7618d020765SAl Viro 		}
7628d020765SAl Viro 
76305afcb77SAl Viro 		iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
76405afcb77SAl Viro 			      sd.total_len - left);
765abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7668d020765SAl Viro 		if (ret <= 0)
7678d020765SAl Viro 			break;
7688d020765SAl Viro 
7698d020765SAl Viro 		sd.num_spliced += ret;
7708d020765SAl Viro 		sd.total_len -= ret;
771dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7728d020765SAl Viro 
7738d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7748d020765SAl Viro 		while (ret) {
7758d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
7768d020765SAl Viro 			if (ret >= buf->len) {
7778d020765SAl Viro 				ret -= buf->len;
7788d020765SAl Viro 				buf->len = 0;
779a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7808d020765SAl Viro 				pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
7818d020765SAl Viro 				pipe->nrbufs--;
7828d020765SAl Viro 				if (pipe->files)
7838d020765SAl Viro 					sd.need_wakeup = true;
7848d020765SAl Viro 			} else {
7858d020765SAl Viro 				buf->offset += ret;
7868d020765SAl Viro 				buf->len -= ret;
7878d020765SAl Viro 				ret = 0;
7888d020765SAl Viro 			}
7898d020765SAl Viro 		}
7908d020765SAl Viro 	}
7918d020765SAl Viro done:
7928d020765SAl Viro 	kfree(array);
7938d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7948d020765SAl Viro 
7958d020765SAl Viro 	pipe_unlock(pipe);
7968d020765SAl Viro 
7978d020765SAl Viro 	if (sd.num_spliced)
7988d020765SAl Viro 		ret = sd.num_spliced;
7998d020765SAl Viro 
8008d020765SAl Viro 	return ret;
8018d020765SAl Viro }
8028d020765SAl Viro 
8038d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8048d020765SAl Viro 
805b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
806b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
8070b0a47f5SMiklos Szeredi {
808b2858d7dSMiklos Szeredi 	int ret;
809b2858d7dSMiklos Szeredi 	void *data;
81006ae43f3SAl Viro 	loff_t tmp = sd->pos;
811b2858d7dSMiklos Szeredi 
812fbb32750SAl Viro 	data = kmap(buf->page);
81306ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
814fbb32750SAl Viro 	kunmap(buf->page);
815b2858d7dSMiklos Szeredi 
816b2858d7dSMiklos Szeredi 	return ret;
8170b0a47f5SMiklos Szeredi }
8180b0a47f5SMiklos Szeredi 
8190b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
8200b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
8210b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
8220b0a47f5SMiklos Szeredi {
823b2858d7dSMiklos Szeredi 	ssize_t ret;
8240b0a47f5SMiklos Szeredi 
825b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
826b2858d7dSMiklos Szeredi 	if (ret > 0)
827b2858d7dSMiklos Szeredi 		*ppos += ret;
8280b0a47f5SMiklos Szeredi 
829b2858d7dSMiklos Szeredi 	return ret;
8300b0a47f5SMiklos Szeredi }
8310b0a47f5SMiklos Szeredi 
83283f9135bSJens Axboe /**
83383f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
834932cc6d4SJens Axboe  * @pipe:	pipe to splice from
83583f9135bSJens Axboe  * @out:	socket to write to
836932cc6d4SJens Axboe  * @ppos:	position in @out
83783f9135bSJens Axboe  * @len:	number of bytes to splice
83883f9135bSJens Axboe  * @flags:	splice modifier flags
83983f9135bSJens Axboe  *
840932cc6d4SJens Axboe  * Description:
84183f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
84283f9135bSJens Axboe  *    is involved.
84383f9135bSJens Axboe  *
84483f9135bSJens Axboe  */
8453a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
846cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8475274f052SJens Axboe {
84800522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8495274f052SJens Axboe }
8505274f052SJens Axboe 
851059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
852a0f06780SJeff Garzik 
85383f9135bSJens Axboe /*
85483f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
85583f9135bSJens Axboe  */
8563a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
857cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8585274f052SJens Axboe {
8590b0a47f5SMiklos Szeredi 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
8600b0a47f5SMiklos Szeredi 				loff_t *, size_t, unsigned int);
8615274f052SJens Axboe 
86272c2d531SAl Viro 	if (out->f_op->splice_write)
8630b0a47f5SMiklos Szeredi 		splice_write = out->f_op->splice_write;
864cc56f7deSChangli Gao 	else
8650b0a47f5SMiklos Szeredi 		splice_write = default_file_splice_write;
8660b0a47f5SMiklos Szeredi 
867500368f7SAl Viro 	return splice_write(pipe, out, ppos, len, flags);
8685274f052SJens Axboe }
8695274f052SJens Axboe 
87083f9135bSJens Axboe /*
87183f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
87283f9135bSJens Axboe  */
873cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
874cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
875cbb7e577SJens Axboe 			 unsigned int flags)
8765274f052SJens Axboe {
8776818173bSMiklos Szeredi 	ssize_t (*splice_read)(struct file *, loff_t *,
8786818173bSMiklos Szeredi 			       struct pipe_inode_info *, size_t, unsigned int);
8795274f052SJens Axboe 	int ret;
8805274f052SJens Axboe 
88149570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8825274f052SJens Axboe 		return -EBADF;
8835274f052SJens Axboe 
884cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8855274f052SJens Axboe 	if (unlikely(ret < 0))
8865274f052SJens Axboe 		return ret;
8875274f052SJens Axboe 
88803cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
88903cc0789SAl Viro 		len = MAX_RW_COUNT;
89003cc0789SAl Viro 
89172c2d531SAl Viro 	if (in->f_op->splice_read)
8926818173bSMiklos Szeredi 		splice_read = in->f_op->splice_read;
893cc56f7deSChangli Gao 	else
8946818173bSMiklos Szeredi 		splice_read = default_file_splice_read;
8956818173bSMiklos Szeredi 
8966818173bSMiklos Szeredi 	return splice_read(in, ppos, pipe, len, flags);
8975274f052SJens Axboe }
8985274f052SJens Axboe 
899932cc6d4SJens Axboe /**
900932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
901932cc6d4SJens Axboe  * @in:		file to splice from
902932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
903932cc6d4SJens Axboe  * @actor:	handles the data splicing
904932cc6d4SJens Axboe  *
905932cc6d4SJens Axboe  * Description:
906932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
907932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
908932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
909932cc6d4SJens Axboe  *    that process.
910932cc6d4SJens Axboe  *
911c66ab6faSJens Axboe  */
912c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
913c66ab6faSJens Axboe 			       splice_direct_actor *actor)
914b92ce558SJens Axboe {
915b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
916b92ce558SJens Axboe 	long ret, bytes;
917b92ce558SJens Axboe 	umode_t i_mode;
918c66ab6faSJens Axboe 	size_t len;
9190ff28d9fSChristophe Leroy 	int i, flags, more;
920b92ce558SJens Axboe 
921b92ce558SJens Axboe 	/*
922b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
923b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
924b92ce558SJens Axboe 	 * piped splicing for that!
925b92ce558SJens Axboe 	 */
926496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
927b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
928b92ce558SJens Axboe 		return -EINVAL;
929b92ce558SJens Axboe 
930b92ce558SJens Axboe 	/*
931b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
932b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
933b92ce558SJens Axboe 	 */
934b92ce558SJens Axboe 	pipe = current->splice_pipe;
93549570e9bSJens Axboe 	if (unlikely(!pipe)) {
9367bee130eSAl Viro 		pipe = alloc_pipe_info();
937b92ce558SJens Axboe 		if (!pipe)
938b92ce558SJens Axboe 			return -ENOMEM;
939b92ce558SJens Axboe 
940b92ce558SJens Axboe 		/*
941b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
94200522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
943b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
944b92ce558SJens Axboe 		 */
945b92ce558SJens Axboe 		pipe->readers = 1;
946b92ce558SJens Axboe 
947b92ce558SJens Axboe 		current->splice_pipe = pipe;
948b92ce558SJens Axboe 	}
949b92ce558SJens Axboe 
950b92ce558SJens Axboe 	/*
95173d62d83SIngo Molnar 	 * Do the splice.
952b92ce558SJens Axboe 	 */
953b92ce558SJens Axboe 	ret = 0;
954b92ce558SJens Axboe 	bytes = 0;
955c66ab6faSJens Axboe 	len = sd->total_len;
956c66ab6faSJens Axboe 	flags = sd->flags;
957c66ab6faSJens Axboe 
958c66ab6faSJens Axboe 	/*
959c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
960c66ab6faSJens Axboe 	 */
961c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9620ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
963b92ce558SJens Axboe 
964b92ce558SJens Axboe 	while (len) {
96551a92c0fSJens Axboe 		size_t read_len;
966a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
967b92ce558SJens Axboe 
968bcd4f3acSJens Axboe 		ret = do_splice_to(in, &pos, pipe, len, flags);
96951a92c0fSJens Axboe 		if (unlikely(ret <= 0))
970b92ce558SJens Axboe 			goto out_release;
971b92ce558SJens Axboe 
972b92ce558SJens Axboe 		read_len = ret;
973c66ab6faSJens Axboe 		sd->total_len = read_len;
974b92ce558SJens Axboe 
975b92ce558SJens Axboe 		/*
9760ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9770ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9780ff28d9fSChristophe Leroy 		 * initially, clears it.
9790ff28d9fSChristophe Leroy 		 */
9800ff28d9fSChristophe Leroy 		if (read_len < len)
9810ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9820ff28d9fSChristophe Leroy 		else if (!more)
9830ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9840ff28d9fSChristophe Leroy 		/*
985b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
986b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
987b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
988b92ce558SJens Axboe 		 */
989c66ab6faSJens Axboe 		ret = actor(pipe, sd);
990a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
991a82c53a0STom Zanussi 			sd->pos = prev_pos;
992b92ce558SJens Axboe 			goto out_release;
993a82c53a0STom Zanussi 		}
994b92ce558SJens Axboe 
995b92ce558SJens Axboe 		bytes += ret;
996b92ce558SJens Axboe 		len -= ret;
997bcd4f3acSJens Axboe 		sd->pos = pos;
998b92ce558SJens Axboe 
999a82c53a0STom Zanussi 		if (ret < read_len) {
1000a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
100151a92c0fSJens Axboe 			goto out_release;
1002b92ce558SJens Axboe 		}
1003a82c53a0STom Zanussi 	}
1004b92ce558SJens Axboe 
10059e97198dSJens Axboe done:
1006b92ce558SJens Axboe 	pipe->nrbufs = pipe->curbuf = 0;
10079e97198dSJens Axboe 	file_accessed(in);
1008b92ce558SJens Axboe 	return bytes;
1009b92ce558SJens Axboe 
1010b92ce558SJens Axboe out_release:
1011b92ce558SJens Axboe 	/*
1012b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1013b92ce558SJens Axboe 	 * the pipe buffers in question:
1014b92ce558SJens Axboe 	 */
101535f3d14dSJens Axboe 	for (i = 0; i < pipe->buffers; i++) {
1016b92ce558SJens Axboe 		struct pipe_buffer *buf = pipe->bufs + i;
1017b92ce558SJens Axboe 
1018a779638cSMiklos Szeredi 		if (buf->ops)
1019a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1020b92ce558SJens Axboe 	}
1021b92ce558SJens Axboe 
10229e97198dSJens Axboe 	if (!bytes)
10239e97198dSJens Axboe 		bytes = ret;
1024b92ce558SJens Axboe 
10259e97198dSJens Axboe 	goto done;
1026c66ab6faSJens Axboe }
1027c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1028c66ab6faSJens Axboe 
1029c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1030c66ab6faSJens Axboe 			       struct splice_desc *sd)
1031c66ab6faSJens Axboe {
10326a14b90bSJens Axboe 	struct file *file = sd->u.file;
1033c66ab6faSJens Axboe 
10347995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10352cb4b05eSChangli Gao 			      sd->flags);
1036c66ab6faSJens Axboe }
1037c66ab6faSJens Axboe 
1038932cc6d4SJens Axboe /**
1039932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1040932cc6d4SJens Axboe  * @in:		file to splice from
1041932cc6d4SJens Axboe  * @ppos:	input file offset
1042932cc6d4SJens Axboe  * @out:	file to splice to
1043acdb37c3SRandy Dunlap  * @opos:	output file offset
1044932cc6d4SJens Axboe  * @len:	number of bytes to splice
1045932cc6d4SJens Axboe  * @flags:	splice modifier flags
1046932cc6d4SJens Axboe  *
1047932cc6d4SJens Axboe  * Description:
1048932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1049932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1050932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1051932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1052932cc6d4SJens Axboe  *
1053932cc6d4SJens Axboe  */
1054c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10557995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1056c66ab6faSJens Axboe {
1057c66ab6faSJens Axboe 	struct splice_desc sd = {
1058c66ab6faSJens Axboe 		.len		= len,
1059c66ab6faSJens Axboe 		.total_len	= len,
1060c66ab6faSJens Axboe 		.flags		= flags,
1061c66ab6faSJens Axboe 		.pos		= *ppos,
10626a14b90bSJens Axboe 		.u.file		= out,
10637995bd28SAl Viro 		.opos		= opos,
1064c66ab6faSJens Axboe 	};
106551a92c0fSJens Axboe 	long ret;
1066c66ab6faSJens Axboe 
106718c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
106818c67cb9SAl Viro 		return -EBADF;
106918c67cb9SAl Viro 
107018c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
107118c67cb9SAl Viro 		return -EINVAL;
107218c67cb9SAl Viro 
107318c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
107418c67cb9SAl Viro 	if (unlikely(ret < 0))
107518c67cb9SAl Viro 		return ret;
107618c67cb9SAl Viro 
1077c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
107851a92c0fSJens Axboe 	if (ret > 0)
1079a82c53a0STom Zanussi 		*ppos = sd.pos;
108051a92c0fSJens Axboe 
1081c66ab6faSJens Axboe 	return ret;
1082b92ce558SJens Axboe }
10831c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1084b92ce558SJens Axboe 
10858924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10868924feffSAl Viro {
108752bce911SLinus Torvalds 	for (;;) {
108852bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
108952bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
109052bce911SLinus Torvalds 			return -EPIPE;
109152bce911SLinus Torvalds 		}
109252bce911SLinus Torvalds 		if (pipe->nrbufs != pipe->buffers)
109352bce911SLinus Torvalds 			return 0;
10948924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10958924feffSAl Viro 			return -EAGAIN;
10968924feffSAl Viro 		if (signal_pending(current))
10978924feffSAl Viro 			return -ERESTARTSYS;
10988924feffSAl Viro 		pipe->waiting_writers++;
10998924feffSAl Viro 		pipe_wait(pipe);
11008924feffSAl Viro 		pipe->waiting_writers--;
11018924feffSAl Viro 	}
11028924feffSAl Viro }
11038924feffSAl Viro 
11047c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
11057c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
11067c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1107ddac0d39SJens Axboe 
1108ddac0d39SJens Axboe /*
110983f9135bSJens Axboe  * Determine where to splice to/from.
111083f9135bSJens Axboe  */
1111529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in,
1112529565dcSIngo Molnar 		      struct file *out, loff_t __user *off_out,
1113529565dcSIngo Molnar 		      size_t len, unsigned int flags)
11145274f052SJens Axboe {
11157c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11167c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11177995bd28SAl Viro 	loff_t offset;
1118a4514ebdSJens Axboe 	long ret;
11195274f052SJens Axboe 
112071993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
112171993e62SLinus Torvalds 	opipe = get_pipe_info(out);
11227c77f0b3SMiklos Szeredi 
11237c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11247c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11257c77f0b3SMiklos Szeredi 			return -ESPIPE;
11267c77f0b3SMiklos Szeredi 
11277c77f0b3SMiklos Szeredi 		if (!(in->f_mode & FMODE_READ))
11287c77f0b3SMiklos Szeredi 			return -EBADF;
11297c77f0b3SMiklos Szeredi 
11307c77f0b3SMiklos Szeredi 		if (!(out->f_mode & FMODE_WRITE))
11317c77f0b3SMiklos Szeredi 			return -EBADF;
11327c77f0b3SMiklos Szeredi 
11337c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11347c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11357c77f0b3SMiklos Szeredi 			return -EINVAL;
11367c77f0b3SMiklos Szeredi 
11377c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11387c77f0b3SMiklos Szeredi 	}
11397c77f0b3SMiklos Szeredi 
11407c77f0b3SMiklos Szeredi 	if (ipipe) {
1141529565dcSIngo Molnar 		if (off_in)
1142529565dcSIngo Molnar 			return -ESPIPE;
1143b92ce558SJens Axboe 		if (off_out) {
114419c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1145b92ce558SJens Axboe 				return -EINVAL;
1146cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1147b92ce558SJens Axboe 				return -EFAULT;
11487995bd28SAl Viro 		} else {
11497995bd28SAl Viro 			offset = out->f_pos;
11507995bd28SAl Viro 		}
1151529565dcSIngo Molnar 
115218c67cb9SAl Viro 		if (unlikely(!(out->f_mode & FMODE_WRITE)))
115318c67cb9SAl Viro 			return -EBADF;
115418c67cb9SAl Viro 
115518c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
115618c67cb9SAl Viro 			return -EINVAL;
115718c67cb9SAl Viro 
115818c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
115918c67cb9SAl Viro 		if (unlikely(ret < 0))
116018c67cb9SAl Viro 			return ret;
116118c67cb9SAl Viro 
1162500368f7SAl Viro 		file_start_write(out);
11637995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1164500368f7SAl Viro 		file_end_write(out);
1165a4514ebdSJens Axboe 
11667995bd28SAl Viro 		if (!off_out)
11677995bd28SAl Viro 			out->f_pos = offset;
11687995bd28SAl Viro 		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1169a4514ebdSJens Axboe 			ret = -EFAULT;
1170a4514ebdSJens Axboe 
1171a4514ebdSJens Axboe 		return ret;
1172529565dcSIngo Molnar 	}
11735274f052SJens Axboe 
11747c77f0b3SMiklos Szeredi 	if (opipe) {
1175529565dcSIngo Molnar 		if (off_out)
1176529565dcSIngo Molnar 			return -ESPIPE;
1177b92ce558SJens Axboe 		if (off_in) {
117819c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1179b92ce558SJens Axboe 				return -EINVAL;
1180cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1181b92ce558SJens Axboe 				return -EFAULT;
11827995bd28SAl Viro 		} else {
11837995bd28SAl Viro 			offset = in->f_pos;
11847995bd28SAl Viro 		}
1185529565dcSIngo Molnar 
11868924feffSAl Viro 		pipe_lock(opipe);
11878924feffSAl Viro 		ret = wait_for_space(opipe, flags);
11888924feffSAl Viro 		if (!ret)
11897995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
11908924feffSAl Viro 		pipe_unlock(opipe);
11918924feffSAl Viro 		if (ret > 0)
11928924feffSAl Viro 			wakeup_pipe_readers(opipe);
11937995bd28SAl Viro 		if (!off_in)
11947995bd28SAl Viro 			in->f_pos = offset;
11957995bd28SAl Viro 		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1196a4514ebdSJens Axboe 			ret = -EFAULT;
1197a4514ebdSJens Axboe 
1198a4514ebdSJens Axboe 		return ret;
1199529565dcSIngo Molnar 	}
12005274f052SJens Axboe 
12015274f052SJens Axboe 	return -EINVAL;
12025274f052SJens Axboe }
12035274f052SJens Axboe 
120479fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
120579fddc4eSAl Viro 			struct pipe_inode_info *pipe,
120679fddc4eSAl Viro 			unsigned flags)
1207912d35f8SJens Axboe {
120879fddc4eSAl Viro 	struct pipe_buffer buf = {
120979fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
121079fddc4eSAl Viro 		.flags = flags
121179fddc4eSAl Viro 	};
121279fddc4eSAl Viro 	size_t total = 0;
121379fddc4eSAl Viro 	int ret = 0;
121479fddc4eSAl Viro 	bool failed = false;
121579fddc4eSAl Viro 
121679fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
121779fddc4eSAl Viro 		struct page *pages[16];
1218db85a9ebSAl Viro 		ssize_t copied;
1219db85a9ebSAl Viro 		size_t start;
122079fddc4eSAl Viro 		int n;
1221912d35f8SJens Axboe 
122279fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
122379fddc4eSAl Viro 		if (copied <= 0) {
122479fddc4eSAl Viro 			ret = copied;
122579fddc4eSAl Viro 			break;
122679fddc4eSAl Viro 		}
1227912d35f8SJens Axboe 
122879fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1229db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
123079fddc4eSAl Viro 			if (!failed) {
123179fddc4eSAl Viro 				buf.page = pages[n];
123279fddc4eSAl Viro 				buf.offset = start;
123379fddc4eSAl Viro 				buf.len = size;
123479fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
123579fddc4eSAl Viro 				if (unlikely(ret < 0)) {
123679fddc4eSAl Viro 					failed = true;
123779fddc4eSAl Viro 				} else {
123879fddc4eSAl Viro 					iov_iter_advance(from, ret);
123979fddc4eSAl Viro 					total += ret;
124079fddc4eSAl Viro 				}
124179fddc4eSAl Viro 			} else {
124279fddc4eSAl Viro 				put_page(pages[n]);
124379fddc4eSAl Viro 			}
1244db85a9ebSAl Viro 			copied -= size;
1245912d35f8SJens Axboe 		}
1246912d35f8SJens Axboe 	}
124779fddc4eSAl Viro 	return total ? total : ret;
1248912d35f8SJens Axboe }
1249912d35f8SJens Axboe 
12506a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12516a14b90bSJens Axboe 			struct splice_desc *sd)
12526a14b90bSJens Axboe {
12536130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
12546130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
12556a14b90bSJens Axboe }
12566a14b90bSJens Axboe 
12576a14b90bSJens Axboe /*
12586a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
12596a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
12606a14b90bSJens Axboe  */
12616130f531SAl Viro static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
12626a14b90bSJens Axboe 			     unsigned long nr_segs, unsigned int flags)
12636a14b90bSJens Axboe {
12646a14b90bSJens Axboe 	struct pipe_inode_info *pipe;
12656a14b90bSJens Axboe 	struct splice_desc sd;
12666a14b90bSJens Axboe 	long ret;
12676130f531SAl Viro 	struct iovec iovstack[UIO_FASTIOV];
12686130f531SAl Viro 	struct iovec *iov = iovstack;
12696130f531SAl Viro 	struct iov_iter iter;
12706a14b90bSJens Axboe 
127171993e62SLinus Torvalds 	pipe = get_pipe_info(file);
12726a14b90bSJens Axboe 	if (!pipe)
12736a14b90bSJens Axboe 		return -EBADF;
12746a14b90bSJens Axboe 
1275345995faSAl Viro 	ret = import_iovec(READ, uiov, nr_segs,
1276345995faSAl Viro 			   ARRAY_SIZE(iovstack), &iov, &iter);
1277345995faSAl Viro 	if (ret < 0)
1278345995faSAl Viro 		return ret;
12796a14b90bSJens Axboe 
1280345995faSAl Viro 	sd.total_len = iov_iter_count(&iter);
12816a14b90bSJens Axboe 	sd.len = 0;
12826a14b90bSJens Axboe 	sd.flags = flags;
12836130f531SAl Viro 	sd.u.data = &iter;
12846a14b90bSJens Axboe 	sd.pos = 0;
12856a14b90bSJens Axboe 
1286345995faSAl Viro 	if (sd.total_len) {
12876130f531SAl Viro 		pipe_lock(pipe);
12886130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
128961e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1290345995faSAl Viro 	}
12916a14b90bSJens Axboe 
12926130f531SAl Viro 	kfree(iov);
12936a14b90bSJens Axboe 	return ret;
12946a14b90bSJens Axboe }
12956a14b90bSJens Axboe 
1296912d35f8SJens Axboe /*
1297912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1298912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1299912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1300912d35f8SJens Axboe  */
1301db85a9ebSAl Viro static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1302912d35f8SJens Axboe 			     unsigned long nr_segs, unsigned int flags)
1303912d35f8SJens Axboe {
1304ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
1305db85a9ebSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
1306db85a9ebSAl Viro 	struct iovec *iov = iovstack;
1307db85a9ebSAl Viro 	struct iov_iter from;
130835f3d14dSJens Axboe 	long ret;
130979fddc4eSAl Viro 	unsigned buf_flag = 0;
131079fddc4eSAl Viro 
131179fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
131279fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1313912d35f8SJens Axboe 
131471993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1315ddac0d39SJens Axboe 	if (!pipe)
1316912d35f8SJens Axboe 		return -EBADF;
1317912d35f8SJens Axboe 
1318db85a9ebSAl Viro 	ret = import_iovec(WRITE, uiov, nr_segs,
1319db85a9ebSAl Viro 			   ARRAY_SIZE(iovstack), &iov, &from);
1320db85a9ebSAl Viro 	if (ret < 0)
1321db85a9ebSAl Viro 		return ret;
1322912d35f8SJens Axboe 
13238924feffSAl Viro 	pipe_lock(pipe);
13248924feffSAl Viro 	ret = wait_for_space(pipe, flags);
132579fddc4eSAl Viro 	if (!ret)
132679fddc4eSAl Viro 		ret = iter_to_pipe(&from, pipe, buf_flag);
13278924feffSAl Viro 	pipe_unlock(pipe);
13288924feffSAl Viro 	if (ret > 0)
13298924feffSAl Viro 		wakeup_pipe_readers(pipe);
1330db85a9ebSAl Viro 	kfree(iov);
133135f3d14dSJens Axboe 	return ret;
1332912d35f8SJens Axboe }
1333912d35f8SJens Axboe 
13346a14b90bSJens Axboe /*
13356a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13366a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13376a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13386a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13396a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13406a14b90bSJens Axboe  * solutions for that:
13416a14b90bSJens Axboe  *
13426a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13436a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13446a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13456a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13466a14b90bSJens Axboe  *
13476a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13486a14b90bSJens Axboe  *
13496a14b90bSJens Axboe  */
1350836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1351836f92adSHeiko Carstens 		unsigned long, nr_segs, unsigned int, flags)
1352912d35f8SJens Axboe {
13532903ff01SAl Viro 	struct fd f;
1354912d35f8SJens Axboe 	long error;
1355912d35f8SJens Axboe 
13563d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
13573d6ea290SAl Viro 		return -EINVAL;
13586a14b90bSJens Axboe 	if (unlikely(nr_segs > UIO_MAXIOV))
13596a14b90bSJens Axboe 		return -EINVAL;
13606a14b90bSJens Axboe 	else if (unlikely(!nr_segs))
13616a14b90bSJens Axboe 		return 0;
13626a14b90bSJens Axboe 
1363912d35f8SJens Axboe 	error = -EBADF;
13642903ff01SAl Viro 	f = fdget(fd);
13652903ff01SAl Viro 	if (f.file) {
13662903ff01SAl Viro 		if (f.file->f_mode & FMODE_WRITE)
13672903ff01SAl Viro 			error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
13682903ff01SAl Viro 		else if (f.file->f_mode & FMODE_READ)
13692903ff01SAl Viro 			error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1370912d35f8SJens Axboe 
13712903ff01SAl Viro 		fdput(f);
1372912d35f8SJens Axboe 	}
1373912d35f8SJens Axboe 
1374912d35f8SJens Axboe 	return error;
1375912d35f8SJens Axboe }
1376912d35f8SJens Axboe 
137776b021d0SAl Viro #ifdef CONFIG_COMPAT
137876b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
137976b021d0SAl Viro 		    unsigned int, nr_segs, unsigned int, flags)
138076b021d0SAl Viro {
138176b021d0SAl Viro 	unsigned i;
138276b021d0SAl Viro 	struct iovec __user *iov;
138376b021d0SAl Viro 	if (nr_segs > UIO_MAXIOV)
138476b021d0SAl Viro 		return -EINVAL;
138576b021d0SAl Viro 	iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
138676b021d0SAl Viro 	for (i = 0; i < nr_segs; i++) {
138776b021d0SAl Viro 		struct compat_iovec v;
138876b021d0SAl Viro 		if (get_user(v.iov_base, &iov32[i].iov_base) ||
138976b021d0SAl Viro 		    get_user(v.iov_len, &iov32[i].iov_len) ||
139076b021d0SAl Viro 		    put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
139176b021d0SAl Viro 		    put_user(v.iov_len, &iov[i].iov_len))
139276b021d0SAl Viro 			return -EFAULT;
139376b021d0SAl Viro 	}
139476b021d0SAl Viro 	return sys_vmsplice(fd, iov, nr_segs, flags);
139576b021d0SAl Viro }
139676b021d0SAl Viro #endif
139776b021d0SAl Viro 
1398836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1399836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1400836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14015274f052SJens Axboe {
14022903ff01SAl Viro 	struct fd in, out;
14035274f052SJens Axboe 	long error;
14045274f052SJens Axboe 
14055274f052SJens Axboe 	if (unlikely(!len))
14065274f052SJens Axboe 		return 0;
14075274f052SJens Axboe 
14083d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14093d6ea290SAl Viro 		return -EINVAL;
14103d6ea290SAl Viro 
14115274f052SJens Axboe 	error = -EBADF;
14122903ff01SAl Viro 	in = fdget(fd_in);
14132903ff01SAl Viro 	if (in.file) {
14142903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
14152903ff01SAl Viro 			out = fdget(fd_out);
14162903ff01SAl Viro 			if (out.file) {
14172903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
14182903ff01SAl Viro 					error = do_splice(in.file, off_in,
14192903ff01SAl Viro 							  out.file, off_out,
1420529565dcSIngo Molnar 							  len, flags);
14212903ff01SAl Viro 				fdput(out);
14225274f052SJens Axboe 			}
14235274f052SJens Axboe 		}
14242903ff01SAl Viro 		fdput(in);
14255274f052SJens Axboe 	}
14265274f052SJens Axboe 	return error;
14275274f052SJens Axboe }
142870524490SJens Axboe 
142970524490SJens Axboe /*
1430aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1431aadd06e5SJens Axboe  * return an appropriate error.
1432aadd06e5SJens Axboe  */
14337c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1434aadd06e5SJens Axboe {
1435aadd06e5SJens Axboe 	int ret;
1436aadd06e5SJens Axboe 
1437aadd06e5SJens Axboe 	/*
1438aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1439aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1440aadd06e5SJens Axboe 	 */
1441aadd06e5SJens Axboe 	if (pipe->nrbufs)
1442aadd06e5SJens Axboe 		return 0;
1443aadd06e5SJens Axboe 
1444aadd06e5SJens Axboe 	ret = 0;
144561e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1446aadd06e5SJens Axboe 
1447aadd06e5SJens Axboe 	while (!pipe->nrbufs) {
1448aadd06e5SJens Axboe 		if (signal_pending(current)) {
1449aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1450aadd06e5SJens Axboe 			break;
1451aadd06e5SJens Axboe 		}
1452aadd06e5SJens Axboe 		if (!pipe->writers)
1453aadd06e5SJens Axboe 			break;
1454aadd06e5SJens Axboe 		if (!pipe->waiting_writers) {
1455aadd06e5SJens Axboe 			if (flags & SPLICE_F_NONBLOCK) {
1456aadd06e5SJens Axboe 				ret = -EAGAIN;
1457aadd06e5SJens Axboe 				break;
1458aadd06e5SJens Axboe 			}
1459aadd06e5SJens Axboe 		}
1460aadd06e5SJens Axboe 		pipe_wait(pipe);
1461aadd06e5SJens Axboe 	}
1462aadd06e5SJens Axboe 
146361e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1464aadd06e5SJens Axboe 	return ret;
1465aadd06e5SJens Axboe }
1466aadd06e5SJens Axboe 
1467aadd06e5SJens Axboe /*
1468aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1469aadd06e5SJens Axboe  * return an appropriate error.
1470aadd06e5SJens Axboe  */
14717c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1472aadd06e5SJens Axboe {
1473aadd06e5SJens Axboe 	int ret;
1474aadd06e5SJens Axboe 
1475aadd06e5SJens Axboe 	/*
1476aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1477aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1478aadd06e5SJens Axboe 	 */
147935f3d14dSJens Axboe 	if (pipe->nrbufs < pipe->buffers)
1480aadd06e5SJens Axboe 		return 0;
1481aadd06e5SJens Axboe 
1482aadd06e5SJens Axboe 	ret = 0;
148361e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1484aadd06e5SJens Axboe 
148535f3d14dSJens Axboe 	while (pipe->nrbufs >= pipe->buffers) {
1486aadd06e5SJens Axboe 		if (!pipe->readers) {
1487aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1488aadd06e5SJens Axboe 			ret = -EPIPE;
1489aadd06e5SJens Axboe 			break;
1490aadd06e5SJens Axboe 		}
1491aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1492aadd06e5SJens Axboe 			ret = -EAGAIN;
1493aadd06e5SJens Axboe 			break;
1494aadd06e5SJens Axboe 		}
1495aadd06e5SJens Axboe 		if (signal_pending(current)) {
1496aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1497aadd06e5SJens Axboe 			break;
1498aadd06e5SJens Axboe 		}
1499aadd06e5SJens Axboe 		pipe->waiting_writers++;
1500aadd06e5SJens Axboe 		pipe_wait(pipe);
1501aadd06e5SJens Axboe 		pipe->waiting_writers--;
1502aadd06e5SJens Axboe 	}
1503aadd06e5SJens Axboe 
150461e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1505aadd06e5SJens Axboe 	return ret;
1506aadd06e5SJens Axboe }
1507aadd06e5SJens Axboe 
1508aadd06e5SJens Axboe /*
15097c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15107c77f0b3SMiklos Szeredi  */
15117c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15127c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15137c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15147c77f0b3SMiklos Szeredi {
15157c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15167c77f0b3SMiklos Szeredi 	int ret = 0, nbuf;
15177c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15187c77f0b3SMiklos Szeredi 
15197c77f0b3SMiklos Szeredi 
15207c77f0b3SMiklos Szeredi retry:
15217c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15227c77f0b3SMiklos Szeredi 	if (ret)
15237c77f0b3SMiklos Szeredi 		return ret;
15247c77f0b3SMiklos Szeredi 
15257c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15267c77f0b3SMiklos Szeredi 	if (ret)
15277c77f0b3SMiklos Szeredi 		return ret;
15287c77f0b3SMiklos Szeredi 
15297c77f0b3SMiklos Szeredi 	/*
15307c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15317c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15327c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15337c77f0b3SMiklos Szeredi 	 */
15347c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15357c77f0b3SMiklos Szeredi 
15367c77f0b3SMiklos Szeredi 	do {
15377c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15387c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15397c77f0b3SMiklos Szeredi 			if (!ret)
15407c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15417c77f0b3SMiklos Szeredi 			break;
15427c77f0b3SMiklos Szeredi 		}
15437c77f0b3SMiklos Szeredi 
15447c77f0b3SMiklos Szeredi 		if (!ipipe->nrbufs && !ipipe->writers)
15457c77f0b3SMiklos Szeredi 			break;
15467c77f0b3SMiklos Szeredi 
15477c77f0b3SMiklos Szeredi 		/*
15487c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15497c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15507c77f0b3SMiklos Szeredi 		 */
155135f3d14dSJens Axboe 		if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
15527c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15537c77f0b3SMiklos Szeredi 			if (ret)
15547c77f0b3SMiklos Szeredi 				break;
15557c77f0b3SMiklos Szeredi 
15567c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15577c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15587c77f0b3SMiklos Szeredi 				break;
15597c77f0b3SMiklos Szeredi 			}
15607c77f0b3SMiklos Szeredi 
15617c77f0b3SMiklos Szeredi 			/*
15627c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15637c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15647c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
15657c77f0b3SMiklos Szeredi 			 */
15667c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
15677c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
15687c77f0b3SMiklos Szeredi 			goto retry;
15697c77f0b3SMiklos Szeredi 		}
15707c77f0b3SMiklos Szeredi 
15717c77f0b3SMiklos Szeredi 		ibuf = ipipe->bufs + ipipe->curbuf;
157235f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
15737c77f0b3SMiklos Szeredi 		obuf = opipe->bufs + nbuf;
15747c77f0b3SMiklos Szeredi 
15757c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
15767c77f0b3SMiklos Szeredi 			/*
15777c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
15787c77f0b3SMiklos Szeredi 			 */
15797c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15807c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
15817c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
158235f3d14dSJens Axboe 			ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
15837c77f0b3SMiklos Szeredi 			ipipe->nrbufs--;
15847c77f0b3SMiklos Szeredi 			input_wakeup = true;
15857c77f0b3SMiklos Szeredi 		} else {
15867c77f0b3SMiklos Szeredi 			/*
15877c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
15887c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
15897c77f0b3SMiklos Szeredi 			 */
15907bf2d1dfSMiklos Szeredi 			pipe_buf_get(ipipe, ibuf);
15917c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15927c77f0b3SMiklos Szeredi 
15937c77f0b3SMiklos Szeredi 			/*
15947c77f0b3SMiklos Szeredi 			 * Don't inherit the gift flag, we need to
15957c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
15967c77f0b3SMiklos Szeredi 			 */
15977c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
15987c77f0b3SMiklos Szeredi 
15997c77f0b3SMiklos Szeredi 			obuf->len = len;
16007c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
16017c77f0b3SMiklos Szeredi 			ibuf->offset += obuf->len;
16027c77f0b3SMiklos Szeredi 			ibuf->len -= obuf->len;
16037c77f0b3SMiklos Szeredi 		}
16047c77f0b3SMiklos Szeredi 		ret += obuf->len;
16057c77f0b3SMiklos Szeredi 		len -= obuf->len;
16067c77f0b3SMiklos Szeredi 	} while (len);
16077c77f0b3SMiklos Szeredi 
16087c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16097c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16107c77f0b3SMiklos Szeredi 
16117c77f0b3SMiklos Szeredi 	/*
16127c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16137c77f0b3SMiklos Szeredi 	 */
1614825cdcb1SNamhyung Kim 	if (ret > 0)
1615825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1616825cdcb1SNamhyung Kim 
16177c77f0b3SMiklos Szeredi 	if (input_wakeup)
16187c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16197c77f0b3SMiklos Szeredi 
16207c77f0b3SMiklos Szeredi 	return ret;
16217c77f0b3SMiklos Szeredi }
16227c77f0b3SMiklos Szeredi 
16237c77f0b3SMiklos Szeredi /*
162470524490SJens Axboe  * Link contents of ipipe to opipe.
162570524490SJens Axboe  */
162670524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
162770524490SJens Axboe 		     struct pipe_inode_info *opipe,
162870524490SJens Axboe 		     size_t len, unsigned int flags)
162970524490SJens Axboe {
163070524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
1631aadd06e5SJens Axboe 	int ret = 0, i = 0, nbuf;
163270524490SJens Axboe 
163370524490SJens Axboe 	/*
163470524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
163561e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
163670524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
163770524490SJens Axboe 	 */
163861e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
163970524490SJens Axboe 
1640aadd06e5SJens Axboe 	do {
164170524490SJens Axboe 		if (!opipe->readers) {
164270524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
164370524490SJens Axboe 			if (!ret)
164470524490SJens Axboe 				ret = -EPIPE;
164570524490SJens Axboe 			break;
164670524490SJens Axboe 		}
164770524490SJens Axboe 
164870524490SJens Axboe 		/*
1649aadd06e5SJens Axboe 		 * If we have iterated all input buffers or ran out of
1650aadd06e5SJens Axboe 		 * output room, break.
165170524490SJens Axboe 		 */
165235f3d14dSJens Axboe 		if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1653aadd06e5SJens Axboe 			break;
1654aadd06e5SJens Axboe 
165535f3d14dSJens Axboe 		ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
165635f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
165770524490SJens Axboe 
165870524490SJens Axboe 		/*
165970524490SJens Axboe 		 * Get a reference to this pipe buffer,
166070524490SJens Axboe 		 * so we can copy the contents over.
166170524490SJens Axboe 		 */
16627bf2d1dfSMiklos Szeredi 		pipe_buf_get(ipipe, ibuf);
166370524490SJens Axboe 
166470524490SJens Axboe 		obuf = opipe->bufs + nbuf;
166570524490SJens Axboe 		*obuf = *ibuf;
166670524490SJens Axboe 
16677afa6fd0SJens Axboe 		/*
16687afa6fd0SJens Axboe 		 * Don't inherit the gift flag, we need to
16697afa6fd0SJens Axboe 		 * prevent multiple steals of this page.
16707afa6fd0SJens Axboe 		 */
16717afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
16727afa6fd0SJens Axboe 
167370524490SJens Axboe 		if (obuf->len > len)
167470524490SJens Axboe 			obuf->len = len;
167570524490SJens Axboe 
167670524490SJens Axboe 		opipe->nrbufs++;
167770524490SJens Axboe 		ret += obuf->len;
167870524490SJens Axboe 		len -= obuf->len;
1679aadd06e5SJens Axboe 		i++;
1680aadd06e5SJens Axboe 	} while (len);
168170524490SJens Axboe 
168202cf01aeSJens Axboe 	/*
168302cf01aeSJens Axboe 	 * return EAGAIN if we have the potential of some data in the
168402cf01aeSJens Axboe 	 * future, otherwise just return 0
168502cf01aeSJens Axboe 	 */
168602cf01aeSJens Axboe 	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
168702cf01aeSJens Axboe 		ret = -EAGAIN;
168802cf01aeSJens Axboe 
168961e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
169061e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
169170524490SJens Axboe 
1692aadd06e5SJens Axboe 	/*
1693aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1694aadd06e5SJens Axboe 	 */
1695825cdcb1SNamhyung Kim 	if (ret > 0)
1696825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
169770524490SJens Axboe 
169870524490SJens Axboe 	return ret;
169970524490SJens Axboe }
170070524490SJens Axboe 
170170524490SJens Axboe /*
170270524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
170370524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
170470524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
170570524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
170670524490SJens Axboe  */
170770524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
170870524490SJens Axboe 		   unsigned int flags)
170970524490SJens Axboe {
171071993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
171171993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1712aadd06e5SJens Axboe 	int ret = -EINVAL;
171370524490SJens Axboe 
171470524490SJens Axboe 	/*
1715aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1716aadd06e5SJens Axboe 	 * copying the data.
171770524490SJens Axboe 	 */
1718aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1719aadd06e5SJens Axboe 		/*
1720aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1721aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1722aadd06e5SJens Axboe 		 */
17237c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1724aadd06e5SJens Axboe 		if (!ret) {
17257c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
172602cf01aeSJens Axboe 			if (!ret)
1727aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1728aadd06e5SJens Axboe 		}
1729aadd06e5SJens Axboe 	}
173070524490SJens Axboe 
1731aadd06e5SJens Axboe 	return ret;
173270524490SJens Axboe }
173370524490SJens Axboe 
1734836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
173570524490SJens Axboe {
17362903ff01SAl Viro 	struct fd in;
17372903ff01SAl Viro 	int error;
173870524490SJens Axboe 
17393d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
17403d6ea290SAl Viro 		return -EINVAL;
17413d6ea290SAl Viro 
174270524490SJens Axboe 	if (unlikely(!len))
174370524490SJens Axboe 		return 0;
174470524490SJens Axboe 
174570524490SJens Axboe 	error = -EBADF;
17462903ff01SAl Viro 	in = fdget(fdin);
17472903ff01SAl Viro 	if (in.file) {
17482903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
17492903ff01SAl Viro 			struct fd out = fdget(fdout);
17502903ff01SAl Viro 			if (out.file) {
17512903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
17522903ff01SAl Viro 					error = do_tee(in.file, out.file,
17532903ff01SAl Viro 							len, flags);
17542903ff01SAl Viro 				fdput(out);
175570524490SJens Axboe 			}
175670524490SJens Axboe 		}
17572903ff01SAl Viro  		fdput(in);
175870524490SJens Axboe  	}
175970524490SJens Axboe 
176070524490SJens Axboe 	return error;
176170524490SJens Axboe }
1762