xref: /openbmc/linux/fs/splice.c (revision 5a81e6a1)
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>
3606ae43f3SAl Viro #include "internal.h"
375274f052SJens Axboe 
3883f9135bSJens Axboe /*
3983f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
4083f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
4183f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
4283f9135bSJens Axboe  * attempt to reuse this page for another destination.
4383f9135bSJens Axboe  */
4476ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
455abc97aaSJens Axboe 				     struct pipe_buffer *buf)
465abc97aaSJens Axboe {
475abc97aaSJens Axboe 	struct page *page = buf->page;
489e94cd4fSJens Axboe 	struct address_space *mapping;
495abc97aaSJens Axboe 
509e0267c2SJens Axboe 	lock_page(page);
519e0267c2SJens Axboe 
529e94cd4fSJens Axboe 	mapping = page_mapping(page);
539e94cd4fSJens Axboe 	if (mapping) {
545abc97aaSJens Axboe 		WARN_ON(!PageUptodate(page));
555abc97aaSJens Axboe 
56ad8d6f0aSJens Axboe 		/*
579e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
589e94cd4fSJens Axboe 		 * writeback completing on this page, since we'll remove it
599e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
609e94cd4fSJens Axboe 		 * page, allowing the disk blocks to be reused by someone else
619e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
629e94cd4fSJens Axboe 		 * ensues.
63ad8d6f0aSJens Axboe 		 */
64ad8d6f0aSJens Axboe 		wait_on_page_writeback(page);
65ad8d6f0aSJens Axboe 
66266cf658SDavid Howells 		if (page_has_private(page) &&
67266cf658SDavid Howells 		    !try_to_release_page(page, GFP_KERNEL))
68ca39d651SJens Axboe 			goto out_unlock;
694f6f0bd2SJens Axboe 
709e94cd4fSJens Axboe 		/*
719e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
729e94cd4fSJens Axboe 		 * and return good.
739e94cd4fSJens Axboe 		 */
749e94cd4fSJens Axboe 		if (remove_mapping(mapping, page)) {
751432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
765abc97aaSJens Axboe 			return 0;
775abc97aaSJens Axboe 		}
789e94cd4fSJens Axboe 	}
799e94cd4fSJens Axboe 
809e94cd4fSJens Axboe 	/*
819e94cd4fSJens Axboe 	 * Raced with truncate or failed to remove page from current
829e94cd4fSJens Axboe 	 * address space, unlock and return failure.
839e94cd4fSJens Axboe 	 */
84ca39d651SJens Axboe out_unlock:
859e94cd4fSJens Axboe 	unlock_page(page);
869e94cd4fSJens Axboe 	return 1;
879e94cd4fSJens Axboe }
885abc97aaSJens Axboe 
8976ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
905274f052SJens Axboe 					struct pipe_buffer *buf)
915274f052SJens Axboe {
9209cbfeafSKirill A. Shutemov 	put_page(buf->page);
931432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
945274f052SJens Axboe }
955274f052SJens Axboe 
960845718dSJens Axboe /*
970845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
980845718dSJens Axboe  * is a page cache page, IO may be in flight.
990845718dSJens Axboe  */
100cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1015274f052SJens Axboe 				       struct pipe_buffer *buf)
1025274f052SJens Axboe {
1035274f052SJens Axboe 	struct page *page = buf->page;
10449d0b21bSJens Axboe 	int err;
1055274f052SJens Axboe 
1065274f052SJens Axboe 	if (!PageUptodate(page)) {
10749d0b21bSJens Axboe 		lock_page(page);
1085274f052SJens Axboe 
10949d0b21bSJens Axboe 		/*
11049d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
11173d62d83SIngo Molnar 		 * splice, if this is the first page.
11249d0b21bSJens Axboe 		 */
1135274f052SJens Axboe 		if (!page->mapping) {
11449d0b21bSJens Axboe 			err = -ENODATA;
11549d0b21bSJens Axboe 			goto error;
1165274f052SJens Axboe 		}
1175274f052SJens Axboe 
11849d0b21bSJens Axboe 		/*
11973d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
12049d0b21bSJens Axboe 		 */
12149d0b21bSJens Axboe 		if (!PageUptodate(page)) {
12249d0b21bSJens Axboe 			err = -EIO;
12349d0b21bSJens Axboe 			goto error;
12449d0b21bSJens Axboe 		}
12549d0b21bSJens Axboe 
12649d0b21bSJens Axboe 		/*
127f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
12849d0b21bSJens Axboe 		 */
12949d0b21bSJens Axboe 		unlock_page(page);
13049d0b21bSJens Axboe 	}
13149d0b21bSJens Axboe 
132f84d7519SJens Axboe 	return 0;
13349d0b21bSJens Axboe error:
13449d0b21bSJens Axboe 	unlock_page(page);
135f84d7519SJens Axboe 	return err;
13670524490SJens Axboe }
13770524490SJens Axboe 
138708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
1395274f052SJens Axboe 	.can_merge = 0,
140cac36bb0SJens Axboe 	.confirm = page_cache_pipe_buf_confirm,
1415274f052SJens Axboe 	.release = page_cache_pipe_buf_release,
1425abc97aaSJens Axboe 	.steal = page_cache_pipe_buf_steal,
143f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
1445274f052SJens Axboe };
1455274f052SJens Axboe 
146912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
147912d35f8SJens Axboe 				    struct pipe_buffer *buf)
148912d35f8SJens Axboe {
1497afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
150912d35f8SJens Axboe 		return 1;
1517afa6fd0SJens Axboe 
1521432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
153330ab716SJens Axboe 	return generic_pipe_buf_steal(pipe, buf);
154912d35f8SJens Axboe }
155912d35f8SJens Axboe 
156d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
157912d35f8SJens Axboe 	.can_merge = 0,
158cac36bb0SJens Axboe 	.confirm = generic_pipe_buf_confirm,
159912d35f8SJens Axboe 	.release = page_cache_pipe_buf_release,
160912d35f8SJens Axboe 	.steal = user_page_pipe_buf_steal,
161f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
162912d35f8SJens Axboe };
163912d35f8SJens Axboe 
164825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
165825cdcb1SNamhyung Kim {
166825cdcb1SNamhyung Kim 	smp_mb();
167825cdcb1SNamhyung Kim 	if (waitqueue_active(&pipe->wait))
168825cdcb1SNamhyung Kim 		wake_up_interruptible(&pipe->wait);
169825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
170825cdcb1SNamhyung Kim }
171825cdcb1SNamhyung Kim 
172932cc6d4SJens Axboe /**
173932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
174932cc6d4SJens Axboe  * @pipe:	pipe to fill
175932cc6d4SJens Axboe  * @spd:	data to fill
176932cc6d4SJens Axboe  *
177932cc6d4SJens Axboe  * Description:
17879685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
179932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
180932cc6d4SJens Axboe  *    function will link that data to the pipe.
181932cc6d4SJens Axboe  *
18283f9135bSJens Axboe  */
183d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
184912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1855274f052SJens Axboe {
18600de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
1878924feffSAl Viro 	int ret = 0, page_nr = 0;
1885274f052SJens Axboe 
189d6785d91SRabin Vincent 	if (!spd_pages)
190d6785d91SRabin Vincent 		return 0;
191d6785d91SRabin Vincent 
1928924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1935274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1945274f052SJens Axboe 		ret = -EPIPE;
1958924feffSAl Viro 		goto out;
1965274f052SJens Axboe 	}
1975274f052SJens Axboe 
1988924feffSAl Viro 	while (pipe->nrbufs < pipe->buffers) {
19935f3d14dSJens Axboe 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
2003a326a2cSIngo Molnar 		struct pipe_buffer *buf = pipe->bufs + newbuf;
2015274f052SJens Axboe 
202912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
203912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
204912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
205497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
206912d35f8SJens Axboe 		buf->ops = spd->ops;
2075a81e6a1SMiklos Szeredi 		buf->flags = 0;
2087afa6fd0SJens Axboe 
2096f767b04SJens Axboe 		pipe->nrbufs++;
210912d35f8SJens Axboe 		page_nr++;
211912d35f8SJens Axboe 		ret += buf->len;
212912d35f8SJens Axboe 
213912d35f8SJens Axboe 		if (!--spd->nr_pages)
2145274f052SJens Axboe 			break;
2155274f052SJens Axboe 	}
2165274f052SJens Axboe 
21729e35094SLinus Torvalds 	if (!ret)
21829e35094SLinus Torvalds 		ret = -EAGAIN;
21929e35094SLinus Torvalds 
2208924feffSAl Viro out:
22100de00bdSJens Axboe 	while (page_nr < spd_pages)
222bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2235274f052SJens Axboe 
2245274f052SJens Axboe 	return ret;
2255274f052SJens Axboe }
2262b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2275274f052SJens Axboe 
22879fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
22979fddc4eSAl Viro {
23079fddc4eSAl Viro 	int ret;
23179fddc4eSAl Viro 
23279fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23379fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
23479fddc4eSAl Viro 		ret = -EPIPE;
23579fddc4eSAl Viro 	} else if (pipe->nrbufs == pipe->buffers) {
23679fddc4eSAl Viro 		ret = -EAGAIN;
23779fddc4eSAl Viro 	} else {
23879fddc4eSAl Viro 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
23979fddc4eSAl Viro 		pipe->bufs[newbuf] = *buf;
24079fddc4eSAl Viro 		pipe->nrbufs++;
24179fddc4eSAl Viro 		return buf->len;
24279fddc4eSAl Viro 	}
243a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24479fddc4eSAl Viro 	return ret;
24579fddc4eSAl Viro }
24679fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
24779fddc4eSAl Viro 
248708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
249bbdfc2f7SJens Axboe {
25009cbfeafSKirill A. Shutemov 	put_page(spd->pages[i]);
251bbdfc2f7SJens Axboe }
252bbdfc2f7SJens Axboe 
25335f3d14dSJens Axboe /*
25435f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25535f3d14dSJens Axboe  * descriptions.
25635f3d14dSJens Axboe  */
257047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25835f3d14dSJens Axboe {
259047fe360SEric Dumazet 	unsigned int buffers = ACCESS_ONCE(pipe->buffers);
260047fe360SEric Dumazet 
261047fe360SEric Dumazet 	spd->nr_pages_max = buffers;
262047fe360SEric Dumazet 	if (buffers <= PIPE_DEF_BUFFERS)
26335f3d14dSJens Axboe 		return 0;
26435f3d14dSJens Axboe 
265047fe360SEric Dumazet 	spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
266047fe360SEric Dumazet 	spd->partial = kmalloc(buffers * sizeof(struct partial_page), 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;
30482c156f8SAl Viro 	int idx, ret;
305be64f884SBoaz Harrosh 
30682c156f8SAl Viro 	iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
30782c156f8SAl Viro 	idx = to.idx;
30882c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
30982c156f8SAl Viro 	kiocb.ki_pos = *ppos;
31082c156f8SAl Viro 	ret = in->f_op->read_iter(&kiocb, &to);
311723590edSMiklos Szeredi 	if (ret > 0) {
31282c156f8SAl Viro 		*ppos = kiocb.ki_pos;
313723590edSMiklos Szeredi 		file_accessed(in);
31482c156f8SAl Viro 	} else if (ret < 0) {
31582c156f8SAl Viro 		to.idx = idx;
31682c156f8SAl Viro 		to.iov_offset = 0;
31782c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
31882c156f8SAl Viro 		/*
31982c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
32082c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
32182c156f8SAl Viro 		 */
32282c156f8SAl Viro 		if (ret == -EFAULT)
32382c156f8SAl Viro 			ret = -EAGAIN;
324723590edSMiklos Szeredi 	}
3255274f052SJens Axboe 
3265274f052SJens Axboe 	return ret;
3275274f052SJens Axboe }
328059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
329059a8f37SJens Axboe 
330241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3316818173bSMiklos Szeredi 	.can_merge = 0,
3326818173bSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
3336818173bSMiklos Szeredi 	.release = generic_pipe_buf_release,
3346818173bSMiklos Szeredi 	.steal = generic_pipe_buf_steal,
3356818173bSMiklos Szeredi 	.get = generic_pipe_buf_get,
3366818173bSMiklos Szeredi };
3376818173bSMiklos Szeredi 
33828a625cbSMiklos Szeredi static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
33928a625cbSMiklos Szeredi 				    struct pipe_buffer *buf)
34028a625cbSMiklos Szeredi {
34128a625cbSMiklos Szeredi 	return 1;
34228a625cbSMiklos Szeredi }
34328a625cbSMiklos Szeredi 
34428a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
34528a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
34628a625cbSMiklos Szeredi 	.can_merge = 0,
34728a625cbSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
34828a625cbSMiklos Szeredi 	.release = generic_pipe_buf_release,
34928a625cbSMiklos Szeredi 	.steal = generic_pipe_buf_nosteal,
35028a625cbSMiklos Szeredi 	.get = generic_pipe_buf_get,
35128a625cbSMiklos Szeredi };
35228a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
35328a625cbSMiklos Szeredi 
354523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
3556818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
3566818173bSMiklos Szeredi {
3576818173bSMiklos Szeredi 	mm_segment_t old_fs;
3586818173bSMiklos Szeredi 	loff_t pos = offset;
3596818173bSMiklos Szeredi 	ssize_t res;
3606818173bSMiklos Szeredi 
3616818173bSMiklos Szeredi 	old_fs = get_fs();
3626818173bSMiklos Szeredi 	set_fs(get_ds());
3636818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
364793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
3656818173bSMiklos Szeredi 	set_fs(old_fs);
3666818173bSMiklos Szeredi 
3676818173bSMiklos Szeredi 	return res;
3686818173bSMiklos Szeredi }
3696818173bSMiklos Szeredi 
3707bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count,
371b2858d7dSMiklos Szeredi 			    loff_t pos)
3720b0a47f5SMiklos Szeredi {
3730b0a47f5SMiklos Szeredi 	mm_segment_t old_fs;
3740b0a47f5SMiklos Szeredi 	ssize_t res;
3750b0a47f5SMiklos Szeredi 
3760b0a47f5SMiklos Szeredi 	old_fs = get_fs();
3770b0a47f5SMiklos Szeredi 	set_fs(get_ds());
3780b0a47f5SMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
3797bb307e8SAl Viro 	res = vfs_write(file, (__force const char __user *)buf, count, &pos);
3800b0a47f5SMiklos Szeredi 	set_fs(old_fs);
3810b0a47f5SMiklos Szeredi 
3820b0a47f5SMiklos Szeredi 	return res;
3830b0a47f5SMiklos Szeredi }
3847bb307e8SAl Viro EXPORT_SYMBOL(kernel_write);
3850b0a47f5SMiklos Szeredi 
38682c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
3876818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
3886818173bSMiklos Szeredi 				 unsigned int flags)
3896818173bSMiklos Szeredi {
390523ac9afSAl Viro 	struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
391523ac9afSAl Viro 	struct iov_iter to;
392523ac9afSAl Viro 	struct page **pages;
3936818173bSMiklos Szeredi 	unsigned int nr_pages;
394523ac9afSAl Viro 	size_t offset, dummy, copied = 0;
3956818173bSMiklos Szeredi 	ssize_t res;
3966818173bSMiklos Szeredi 	int i;
3976818173bSMiklos Szeredi 
398523ac9afSAl Viro 	if (pipe->nrbufs == pipe->buffers)
399523ac9afSAl Viro 		return -EAGAIN;
400523ac9afSAl Viro 
401523ac9afSAl Viro 	/*
402523ac9afSAl Viro 	 * Try to keep page boundaries matching to source pagecache ones -
403523ac9afSAl Viro 	 * it probably won't be much help, but...
404523ac9afSAl Viro 	 */
405523ac9afSAl Viro 	offset = *ppos & ~PAGE_MASK;
406523ac9afSAl Viro 
407523ac9afSAl Viro 	iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
408523ac9afSAl Viro 
409523ac9afSAl Viro 	res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &dummy);
410523ac9afSAl Viro 	if (res <= 0)
41135f3d14dSJens Axboe 		return -ENOMEM;
41235f3d14dSJens Axboe 
4138e54cadaSAl Viro 	BUG_ON(dummy);
4148e54cadaSAl Viro 	nr_pages = DIV_ROUND_UP(res, PAGE_SIZE);
415523ac9afSAl Viro 
41635f3d14dSJens Axboe 	vec = __vec;
417523ac9afSAl Viro 	if (nr_pages > PIPE_DEF_BUFFERS) {
418523ac9afSAl Viro 		vec = kmalloc(nr_pages * sizeof(struct kvec), GFP_KERNEL);
419523ac9afSAl Viro 		if (unlikely(!vec)) {
420523ac9afSAl Viro 			res = -ENOMEM;
421523ac9afSAl Viro 			goto out;
422523ac9afSAl Viro 		}
42335f3d14dSJens Axboe 	}
42435f3d14dSJens Axboe 
425523ac9afSAl Viro 	pipe->bufs[to.idx].offset = offset;
426523ac9afSAl Viro 	pipe->bufs[to.idx].len -= offset;
4276818173bSMiklos Szeredi 
428523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++) {
429523ac9afSAl Viro 		size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
430523ac9afSAl Viro 		vec[i].iov_base = page_address(pages[i]) + offset;
4316818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
4326818173bSMiklos Szeredi 		len -= this_len;
4336818173bSMiklos Szeredi 		offset = 0;
4346818173bSMiklos Szeredi 	}
4356818173bSMiklos Szeredi 
436523ac9afSAl Viro 	res = kernel_readv(in, vec, nr_pages, *ppos);
437523ac9afSAl Viro 	if (res > 0) {
438523ac9afSAl Viro 		copied = res;
4396818173bSMiklos Szeredi 		*ppos += res;
440523ac9afSAl Viro 	}
4416818173bSMiklos Szeredi 
44235f3d14dSJens Axboe 	if (vec != __vec)
44335f3d14dSJens Axboe 		kfree(vec);
444523ac9afSAl Viro out:
445523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++)
446523ac9afSAl Viro 		put_page(pages[i]);
447523ac9afSAl Viro 	kvfree(pages);
448523ac9afSAl Viro 	iov_iter_advance(&to, copied);	/* truncates and discards */
4496818173bSMiklos Szeredi 	return res;
4506818173bSMiklos Szeredi }
4516818173bSMiklos Szeredi 
4525274f052SJens Axboe /*
4534f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
454016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4555274f052SJens Axboe  */
45676ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4575274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4585274f052SJens Axboe {
4596a14b90bSJens Axboe 	struct file *file = sd->u.file;
4605274f052SJens Axboe 	loff_t pos = sd->pos;
461a8adbe37SMichał Mirosław 	int more;
4625274f052SJens Axboe 
46372c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
464a8adbe37SMichał Mirosław 		return -EINVAL;
465a8adbe37SMichał Mirosław 
46635f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
467ae62ca7bSEric Dumazet 
468ae62ca7bSEric Dumazet 	if (sd->len < sd->total_len && pipe->nrbufs > 1)
46935f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
470ae62ca7bSEric Dumazet 
471a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
472f84d7519SJens Axboe 				    sd->len, &pos, more);
4735274f052SJens Axboe }
4745274f052SJens Axboe 
475b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
476b3c2d2ddSMiklos Szeredi {
477b3c2d2ddSMiklos Szeredi 	smp_mb();
478b3c2d2ddSMiklos Szeredi 	if (waitqueue_active(&pipe->wait))
479b3c2d2ddSMiklos Szeredi 		wake_up_interruptible(&pipe->wait);
480b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
481b3c2d2ddSMiklos Szeredi }
482b3c2d2ddSMiklos Szeredi 
483b3c2d2ddSMiklos Szeredi /**
484b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
485b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
486b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
487b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
488b3c2d2ddSMiklos Szeredi  *
489b3c2d2ddSMiklos Szeredi  * Description:
490b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
491b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
492b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
493b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
494b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
495b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
496b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
497b3c2d2ddSMiklos Szeredi  *
498b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
499b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
500b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
501b3c2d2ddSMiklos Szeredi  *    destination.
502b3c2d2ddSMiklos Szeredi  */
50396f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
504b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
505b3c2d2ddSMiklos Szeredi {
506b3c2d2ddSMiklos Szeredi 	int ret;
507b3c2d2ddSMiklos Szeredi 
508b3c2d2ddSMiklos Szeredi 	while (pipe->nrbufs) {
509b3c2d2ddSMiklos Szeredi 		struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
510b3c2d2ddSMiklos Szeredi 
511b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
512b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
513b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
514b3c2d2ddSMiklos Szeredi 
515fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
516a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
517b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
518b3c2d2ddSMiklos Szeredi 				ret = 0;
519b3c2d2ddSMiklos Szeredi 			return ret;
520b3c2d2ddSMiklos Szeredi 		}
521a8adbe37SMichał Mirosław 
522a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
523a8adbe37SMichał Mirosław 		if (ret <= 0)
524a8adbe37SMichał Mirosław 			return ret;
525a8adbe37SMichał Mirosław 
526b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
527b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
528b3c2d2ddSMiklos Szeredi 
529b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
530b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
531b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
532b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
533b3c2d2ddSMiklos Szeredi 
534b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
535a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
53635f3d14dSJens Axboe 			pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
537b3c2d2ddSMiklos Szeredi 			pipe->nrbufs--;
5386447a3cfSAl Viro 			if (pipe->files)
539b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
540b3c2d2ddSMiklos Szeredi 		}
541b3c2d2ddSMiklos Szeredi 
542b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
543b3c2d2ddSMiklos Szeredi 			return 0;
544b3c2d2ddSMiklos Szeredi 	}
545b3c2d2ddSMiklos Szeredi 
546b3c2d2ddSMiklos Szeredi 	return 1;
547b3c2d2ddSMiklos Szeredi }
548b3c2d2ddSMiklos Szeredi 
549b3c2d2ddSMiklos Szeredi /**
550b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
551b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
552b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
553b3c2d2ddSMiklos Szeredi  *
554b3c2d2ddSMiklos Szeredi  * Description:
555b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
556b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
557b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
558b3c2d2ddSMiklos Szeredi  */
55996f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
560b3c2d2ddSMiklos Szeredi {
561c725bfceSJan Kara 	/*
562c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
563c725bfceSJan Kara 	 * always buffers available
564c725bfceSJan Kara 	 */
565c725bfceSJan Kara 	if (signal_pending(current))
566c725bfceSJan Kara 		return -ERESTARTSYS;
567c725bfceSJan Kara 
568b3c2d2ddSMiklos Szeredi 	while (!pipe->nrbufs) {
569b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
570b3c2d2ddSMiklos Szeredi 			return 0;
571b3c2d2ddSMiklos Szeredi 
572b3c2d2ddSMiklos Szeredi 		if (!pipe->waiting_writers && sd->num_spliced)
573b3c2d2ddSMiklos Szeredi 			return 0;
574b3c2d2ddSMiklos Szeredi 
575b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
576b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
577b3c2d2ddSMiklos Szeredi 
578b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
579b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
580b3c2d2ddSMiklos Szeredi 
581b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
582b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
583b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
584b3c2d2ddSMiklos Szeredi 		}
585b3c2d2ddSMiklos Szeredi 
586b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
587b3c2d2ddSMiklos Szeredi 	}
588b3c2d2ddSMiklos Szeredi 
589b3c2d2ddSMiklos Szeredi 	return 1;
590b3c2d2ddSMiklos Szeredi }
591b3c2d2ddSMiklos Szeredi 
592b3c2d2ddSMiklos Szeredi /**
593b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
594b80901bbSRandy Dunlap  * @sd:		information about the splice operation
595b3c2d2ddSMiklos Szeredi  *
596b3c2d2ddSMiklos Szeredi  * Description:
597b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
598b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
599b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
600b3c2d2ddSMiklos Szeredi  */
60196f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
602b3c2d2ddSMiklos Szeredi {
603b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
604b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
605b3c2d2ddSMiklos Szeredi }
606b3c2d2ddSMiklos Szeredi 
607b3c2d2ddSMiklos Szeredi /**
608b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
609b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
610b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
611b3c2d2ddSMiklos Szeredi  *
612b3c2d2ddSMiklos Szeredi  * Description:
613b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
614b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
615b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
616b3c2d2ddSMiklos Szeredi  */
61796f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
618b3c2d2ddSMiklos Szeredi {
619b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
620b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
621b3c2d2ddSMiklos Szeredi }
622b3c2d2ddSMiklos Szeredi 
623932cc6d4SJens Axboe /**
624932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
625932cc6d4SJens Axboe  * @pipe:	pipe to splice from
626932cc6d4SJens Axboe  * @sd:		information to @actor
627932cc6d4SJens Axboe  * @actor:	handler that splices the data
628932cc6d4SJens Axboe  *
629932cc6d4SJens Axboe  * Description:
630932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
631932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
632932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
633932cc6d4SJens Axboe  *    pipe_to_user.
634932cc6d4SJens Axboe  *
63583f9135bSJens Axboe  */
636c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
637c66ab6faSJens Axboe 			   splice_actor *actor)
6385274f052SJens Axboe {
639b3c2d2ddSMiklos Szeredi 	int ret;
6405274f052SJens Axboe 
641b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
642b3c2d2ddSMiklos Szeredi 	do {
643c2489e07SJan Kara 		cond_resched();
644b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
645b3c2d2ddSMiklos Szeredi 		if (ret > 0)
646b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
647b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
648b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6495274f052SJens Axboe 
650b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6515274f052SJens Axboe }
65240bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6535274f052SJens Axboe 
654932cc6d4SJens Axboe /**
655932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
656932cc6d4SJens Axboe  * @pipe:	pipe to splice from
657932cc6d4SJens Axboe  * @out:	file to splice to
658932cc6d4SJens Axboe  * @ppos:	position in @out
659932cc6d4SJens Axboe  * @len:	how many bytes to splice
660932cc6d4SJens Axboe  * @flags:	splice modifier flags
661932cc6d4SJens Axboe  * @actor:	handler that splices the data
662932cc6d4SJens Axboe  *
663932cc6d4SJens Axboe  * Description:
6642933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
665932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
666932cc6d4SJens Axboe  *
667932cc6d4SJens Axboe  */
6686da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6696da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6706da61809SMark Fasheh 			 splice_actor *actor)
6716da61809SMark Fasheh {
6726da61809SMark Fasheh 	ssize_t ret;
673c66ab6faSJens Axboe 	struct splice_desc sd = {
674c66ab6faSJens Axboe 		.total_len = len,
675c66ab6faSJens Axboe 		.flags = flags,
676c66ab6faSJens Axboe 		.pos = *ppos,
6776a14b90bSJens Axboe 		.u.file = out,
678c66ab6faSJens Axboe 	};
6796da61809SMark Fasheh 
68061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
681c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
68261e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6836da61809SMark Fasheh 
6846da61809SMark Fasheh 	return ret;
6856da61809SMark Fasheh }
6866da61809SMark Fasheh 
6876da61809SMark Fasheh /**
6888d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6898d020765SAl Viro  * @pipe:	pipe info
6908d020765SAl Viro  * @out:	file to write to
6918d020765SAl Viro  * @ppos:	position in @out
6928d020765SAl Viro  * @len:	number of bytes to splice
6938d020765SAl Viro  * @flags:	splice modifier flags
6948d020765SAl Viro  *
6958d020765SAl Viro  * Description:
6968d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6978d020765SAl Viro  *    the given pipe inode to the given file.
6988d020765SAl Viro  *    This one is ->write_iter-based.
6998d020765SAl Viro  *
7008d020765SAl Viro  */
7018d020765SAl Viro ssize_t
7028d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
7038d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
7048d020765SAl Viro {
7058d020765SAl Viro 	struct splice_desc sd = {
7068d020765SAl Viro 		.total_len = len,
7078d020765SAl Viro 		.flags = flags,
7088d020765SAl Viro 		.pos = *ppos,
7098d020765SAl Viro 		.u.file = out,
7108d020765SAl Viro 	};
7118d020765SAl Viro 	int nbufs = pipe->buffers;
7128d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7138d020765SAl Viro 					GFP_KERNEL);
7148d020765SAl Viro 	ssize_t ret;
7158d020765SAl Viro 
7168d020765SAl Viro 	if (unlikely(!array))
7178d020765SAl Viro 		return -ENOMEM;
7188d020765SAl Viro 
7198d020765SAl Viro 	pipe_lock(pipe);
7208d020765SAl Viro 
7218d020765SAl Viro 	splice_from_pipe_begin(&sd);
7228d020765SAl Viro 	while (sd.total_len) {
7238d020765SAl Viro 		struct iov_iter from;
7248d020765SAl Viro 		size_t left;
7258d020765SAl Viro 		int n, idx;
7268d020765SAl Viro 
7278d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7288d020765SAl Viro 		if (ret <= 0)
7298d020765SAl Viro 			break;
7308d020765SAl Viro 
7318d020765SAl Viro 		if (unlikely(nbufs < pipe->buffers)) {
7328d020765SAl Viro 			kfree(array);
7338d020765SAl Viro 			nbufs = pipe->buffers;
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 
7428d020765SAl Viro 		/* build the vector */
7438d020765SAl Viro 		left = sd.total_len;
7448d020765SAl Viro 		for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
7458d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + idx;
7468d020765SAl Viro 			size_t this_len = buf->len;
7478d020765SAl Viro 
7488d020765SAl Viro 			if (this_len > left)
7498d020765SAl Viro 				this_len = left;
7508d020765SAl Viro 
7518d020765SAl Viro 			if (idx == pipe->buffers - 1)
7528d020765SAl Viro 				idx = -1;
7538d020765SAl Viro 
754fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7558d020765SAl Viro 			if (unlikely(ret)) {
7568d020765SAl Viro 				if (ret == -ENODATA)
7578d020765SAl Viro 					ret = 0;
7588d020765SAl Viro 				goto done;
7598d020765SAl Viro 			}
7608d020765SAl Viro 
7618d020765SAl Viro 			array[n].bv_page = buf->page;
7628d020765SAl Viro 			array[n].bv_len = this_len;
7638d020765SAl Viro 			array[n].bv_offset = buf->offset;
7648d020765SAl Viro 			left -= this_len;
7658d020765SAl Viro 		}
7668d020765SAl Viro 
76705afcb77SAl Viro 		iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
76805afcb77SAl Viro 			      sd.total_len - left);
769dbe4e192SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos);
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 */
7788d020765SAl Viro 		while (ret) {
7798d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
7808d020765SAl Viro 			if (ret >= buf->len) {
7818d020765SAl Viro 				ret -= buf->len;
7828d020765SAl Viro 				buf->len = 0;
783a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7848d020765SAl Viro 				pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
7858d020765SAl Viro 				pipe->nrbufs--;
7868d020765SAl Viro 				if (pipe->files)
7878d020765SAl Viro 					sd.need_wakeup = true;
7888d020765SAl Viro 			} else {
7898d020765SAl Viro 				buf->offset += ret;
7908d020765SAl Viro 				buf->len -= ret;
7918d020765SAl Viro 				ret = 0;
7928d020765SAl Viro 			}
7938d020765SAl Viro 		}
7948d020765SAl Viro 	}
7958d020765SAl Viro done:
7968d020765SAl Viro 	kfree(array);
7978d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7988d020765SAl Viro 
7998d020765SAl Viro 	pipe_unlock(pipe);
8008d020765SAl Viro 
8018d020765SAl Viro 	if (sd.num_spliced)
8028d020765SAl Viro 		ret = sd.num_spliced;
8038d020765SAl Viro 
8048d020765SAl Viro 	return ret;
8058d020765SAl Viro }
8068d020765SAl Viro 
8078d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8088d020765SAl Viro 
809b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
810b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
8110b0a47f5SMiklos Szeredi {
812b2858d7dSMiklos Szeredi 	int ret;
813b2858d7dSMiklos Szeredi 	void *data;
81406ae43f3SAl Viro 	loff_t tmp = sd->pos;
815b2858d7dSMiklos Szeredi 
816fbb32750SAl Viro 	data = kmap(buf->page);
81706ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
818fbb32750SAl Viro 	kunmap(buf->page);
819b2858d7dSMiklos Szeredi 
820b2858d7dSMiklos Szeredi 	return ret;
8210b0a47f5SMiklos Szeredi }
8220b0a47f5SMiklos Szeredi 
8230b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
8240b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
8250b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
8260b0a47f5SMiklos Szeredi {
827b2858d7dSMiklos Szeredi 	ssize_t ret;
8280b0a47f5SMiklos Szeredi 
829b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
830b2858d7dSMiklos Szeredi 	if (ret > 0)
831b2858d7dSMiklos Szeredi 		*ppos += ret;
8320b0a47f5SMiklos Szeredi 
833b2858d7dSMiklos Szeredi 	return ret;
8340b0a47f5SMiklos Szeredi }
8350b0a47f5SMiklos Szeredi 
83683f9135bSJens Axboe /**
83783f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
838932cc6d4SJens Axboe  * @pipe:	pipe to splice from
83983f9135bSJens Axboe  * @out:	socket to write to
840932cc6d4SJens Axboe  * @ppos:	position in @out
84183f9135bSJens Axboe  * @len:	number of bytes to splice
84283f9135bSJens Axboe  * @flags:	splice modifier flags
84383f9135bSJens Axboe  *
844932cc6d4SJens Axboe  * Description:
84583f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
84683f9135bSJens Axboe  *    is involved.
84783f9135bSJens Axboe  *
84883f9135bSJens Axboe  */
8493a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
850cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8515274f052SJens Axboe {
85200522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8535274f052SJens Axboe }
8545274f052SJens Axboe 
855059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
856a0f06780SJeff Garzik 
85783f9135bSJens Axboe /*
85883f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
85983f9135bSJens Axboe  */
8603a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
861cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8625274f052SJens Axboe {
8630b0a47f5SMiklos Szeredi 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
8640b0a47f5SMiklos Szeredi 				loff_t *, size_t, unsigned int);
8655274f052SJens Axboe 
86672c2d531SAl Viro 	if (out->f_op->splice_write)
8670b0a47f5SMiklos Szeredi 		splice_write = out->f_op->splice_write;
868cc56f7deSChangli Gao 	else
8690b0a47f5SMiklos Szeredi 		splice_write = default_file_splice_write;
8700b0a47f5SMiklos Szeredi 
871500368f7SAl Viro 	return splice_write(pipe, out, ppos, len, flags);
8725274f052SJens Axboe }
8735274f052SJens Axboe 
87483f9135bSJens Axboe /*
87583f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
87683f9135bSJens Axboe  */
877cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
878cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
879cbb7e577SJens Axboe 			 unsigned int flags)
8805274f052SJens Axboe {
8816818173bSMiklos Szeredi 	ssize_t (*splice_read)(struct file *, loff_t *,
8826818173bSMiklos Szeredi 			       struct pipe_inode_info *, size_t, unsigned int);
8835274f052SJens Axboe 	int ret;
8845274f052SJens Axboe 
88549570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8865274f052SJens Axboe 		return -EBADF;
8875274f052SJens Axboe 
888cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8895274f052SJens Axboe 	if (unlikely(ret < 0))
8905274f052SJens Axboe 		return ret;
8915274f052SJens Axboe 
89203cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
89303cc0789SAl Viro 		len = MAX_RW_COUNT;
89403cc0789SAl Viro 
89572c2d531SAl Viro 	if (in->f_op->splice_read)
8966818173bSMiklos Szeredi 		splice_read = in->f_op->splice_read;
897cc56f7deSChangli Gao 	else
8986818173bSMiklos Szeredi 		splice_read = default_file_splice_read;
8996818173bSMiklos Szeredi 
9006818173bSMiklos Szeredi 	return splice_read(in, ppos, pipe, len, flags);
9015274f052SJens Axboe }
9025274f052SJens Axboe 
903932cc6d4SJens Axboe /**
904932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
905932cc6d4SJens Axboe  * @in:		file to splice from
906932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
907932cc6d4SJens Axboe  * @actor:	handles the data splicing
908932cc6d4SJens Axboe  *
909932cc6d4SJens Axboe  * Description:
910932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
911932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
912932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
913932cc6d4SJens Axboe  *    that process.
914932cc6d4SJens Axboe  *
915c66ab6faSJens Axboe  */
916c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
917c66ab6faSJens Axboe 			       splice_direct_actor *actor)
918b92ce558SJens Axboe {
919b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
920b92ce558SJens Axboe 	long ret, bytes;
921b92ce558SJens Axboe 	umode_t i_mode;
922c66ab6faSJens Axboe 	size_t len;
9230ff28d9fSChristophe Leroy 	int i, flags, more;
924b92ce558SJens Axboe 
925b92ce558SJens Axboe 	/*
926b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
927b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
928b92ce558SJens Axboe 	 * piped splicing for that!
929b92ce558SJens Axboe 	 */
930496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
931b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
932b92ce558SJens Axboe 		return -EINVAL;
933b92ce558SJens Axboe 
934b92ce558SJens Axboe 	/*
935b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
936b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
937b92ce558SJens Axboe 	 */
938b92ce558SJens Axboe 	pipe = current->splice_pipe;
93949570e9bSJens Axboe 	if (unlikely(!pipe)) {
9407bee130eSAl Viro 		pipe = alloc_pipe_info();
941b92ce558SJens Axboe 		if (!pipe)
942b92ce558SJens Axboe 			return -ENOMEM;
943b92ce558SJens Axboe 
944b92ce558SJens Axboe 		/*
945b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
94600522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
947b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
948b92ce558SJens Axboe 		 */
949b92ce558SJens Axboe 		pipe->readers = 1;
950b92ce558SJens Axboe 
951b92ce558SJens Axboe 		current->splice_pipe = pipe;
952b92ce558SJens Axboe 	}
953b92ce558SJens Axboe 
954b92ce558SJens Axboe 	/*
95573d62d83SIngo Molnar 	 * Do the splice.
956b92ce558SJens Axboe 	 */
957b92ce558SJens Axboe 	ret = 0;
958b92ce558SJens Axboe 	bytes = 0;
959c66ab6faSJens Axboe 	len = sd->total_len;
960c66ab6faSJens Axboe 	flags = sd->flags;
961c66ab6faSJens Axboe 
962c66ab6faSJens Axboe 	/*
963c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
964c66ab6faSJens Axboe 	 */
965c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9660ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
967b92ce558SJens Axboe 
968b92ce558SJens Axboe 	while (len) {
96951a92c0fSJens Axboe 		size_t read_len;
970a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
971b92ce558SJens Axboe 
972bcd4f3acSJens Axboe 		ret = do_splice_to(in, &pos, pipe, len, flags);
97351a92c0fSJens Axboe 		if (unlikely(ret <= 0))
974b92ce558SJens Axboe 			goto out_release;
975b92ce558SJens Axboe 
976b92ce558SJens Axboe 		read_len = ret;
977c66ab6faSJens Axboe 		sd->total_len = read_len;
978b92ce558SJens Axboe 
979b92ce558SJens Axboe 		/*
9800ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9810ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9820ff28d9fSChristophe Leroy 		 * initially, clears it.
9830ff28d9fSChristophe Leroy 		 */
9840ff28d9fSChristophe Leroy 		if (read_len < len)
9850ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9860ff28d9fSChristophe Leroy 		else if (!more)
9870ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9880ff28d9fSChristophe Leroy 		/*
989b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
990b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
991b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
992b92ce558SJens Axboe 		 */
993c66ab6faSJens Axboe 		ret = actor(pipe, sd);
994a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
995a82c53a0STom Zanussi 			sd->pos = prev_pos;
996b92ce558SJens Axboe 			goto out_release;
997a82c53a0STom Zanussi 		}
998b92ce558SJens Axboe 
999b92ce558SJens Axboe 		bytes += ret;
1000b92ce558SJens Axboe 		len -= ret;
1001bcd4f3acSJens Axboe 		sd->pos = pos;
1002b92ce558SJens Axboe 
1003a82c53a0STom Zanussi 		if (ret < read_len) {
1004a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
100551a92c0fSJens Axboe 			goto out_release;
1006b92ce558SJens Axboe 		}
1007a82c53a0STom Zanussi 	}
1008b92ce558SJens Axboe 
10099e97198dSJens Axboe done:
1010b92ce558SJens Axboe 	pipe->nrbufs = pipe->curbuf = 0;
10119e97198dSJens Axboe 	file_accessed(in);
1012b92ce558SJens Axboe 	return bytes;
1013b92ce558SJens Axboe 
1014b92ce558SJens Axboe out_release:
1015b92ce558SJens Axboe 	/*
1016b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1017b92ce558SJens Axboe 	 * the pipe buffers in question:
1018b92ce558SJens Axboe 	 */
101935f3d14dSJens Axboe 	for (i = 0; i < pipe->buffers; i++) {
1020b92ce558SJens Axboe 		struct pipe_buffer *buf = pipe->bufs + i;
1021b92ce558SJens Axboe 
1022a779638cSMiklos Szeredi 		if (buf->ops)
1023a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1024b92ce558SJens Axboe 	}
1025b92ce558SJens Axboe 
10269e97198dSJens Axboe 	if (!bytes)
10279e97198dSJens Axboe 		bytes = ret;
1028b92ce558SJens Axboe 
10299e97198dSJens Axboe 	goto done;
1030c66ab6faSJens Axboe }
1031c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1032c66ab6faSJens Axboe 
1033c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1034c66ab6faSJens Axboe 			       struct splice_desc *sd)
1035c66ab6faSJens Axboe {
10366a14b90bSJens Axboe 	struct file *file = sd->u.file;
1037c66ab6faSJens Axboe 
10387995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10392cb4b05eSChangli Gao 			      sd->flags);
1040c66ab6faSJens Axboe }
1041c66ab6faSJens Axboe 
1042932cc6d4SJens Axboe /**
1043932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1044932cc6d4SJens Axboe  * @in:		file to splice from
1045932cc6d4SJens Axboe  * @ppos:	input file offset
1046932cc6d4SJens Axboe  * @out:	file to splice to
1047acdb37c3SRandy Dunlap  * @opos:	output file offset
1048932cc6d4SJens Axboe  * @len:	number of bytes to splice
1049932cc6d4SJens Axboe  * @flags:	splice modifier flags
1050932cc6d4SJens Axboe  *
1051932cc6d4SJens Axboe  * Description:
1052932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1053932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1054932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1055932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1056932cc6d4SJens Axboe  *
1057932cc6d4SJens Axboe  */
1058c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10597995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1060c66ab6faSJens Axboe {
1061c66ab6faSJens Axboe 	struct splice_desc sd = {
1062c66ab6faSJens Axboe 		.len		= len,
1063c66ab6faSJens Axboe 		.total_len	= len,
1064c66ab6faSJens Axboe 		.flags		= flags,
1065c66ab6faSJens Axboe 		.pos		= *ppos,
10666a14b90bSJens Axboe 		.u.file		= out,
10677995bd28SAl Viro 		.opos		= opos,
1068c66ab6faSJens Axboe 	};
106951a92c0fSJens Axboe 	long ret;
1070c66ab6faSJens Axboe 
107118c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
107218c67cb9SAl Viro 		return -EBADF;
107318c67cb9SAl Viro 
107418c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
107518c67cb9SAl Viro 		return -EINVAL;
107618c67cb9SAl Viro 
107718c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
107818c67cb9SAl Viro 	if (unlikely(ret < 0))
107918c67cb9SAl Viro 		return ret;
108018c67cb9SAl Viro 
1081c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
108251a92c0fSJens Axboe 	if (ret > 0)
1083a82c53a0STom Zanussi 		*ppos = sd.pos;
108451a92c0fSJens Axboe 
1085c66ab6faSJens Axboe 	return ret;
1086b92ce558SJens Axboe }
10871c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1088b92ce558SJens Axboe 
10898924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10908924feffSAl Viro {
109152bce911SLinus Torvalds 	for (;;) {
109252bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
109352bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
109452bce911SLinus Torvalds 			return -EPIPE;
109552bce911SLinus Torvalds 		}
109652bce911SLinus Torvalds 		if (pipe->nrbufs != pipe->buffers)
109752bce911SLinus Torvalds 			return 0;
10988924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10998924feffSAl Viro 			return -EAGAIN;
11008924feffSAl Viro 		if (signal_pending(current))
11018924feffSAl Viro 			return -ERESTARTSYS;
11028924feffSAl Viro 		pipe->waiting_writers++;
11038924feffSAl Viro 		pipe_wait(pipe);
11048924feffSAl Viro 		pipe->waiting_writers--;
11058924feffSAl Viro 	}
11068924feffSAl Viro }
11078924feffSAl Viro 
11087c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
11097c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
11107c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1111ddac0d39SJens Axboe 
1112ddac0d39SJens Axboe /*
111383f9135bSJens Axboe  * Determine where to splice to/from.
111483f9135bSJens Axboe  */
1115529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in,
1116529565dcSIngo Molnar 		      struct file *out, loff_t __user *off_out,
1117529565dcSIngo Molnar 		      size_t len, unsigned int flags)
11185274f052SJens Axboe {
11197c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11207c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11217995bd28SAl Viro 	loff_t offset;
1122a4514ebdSJens Axboe 	long ret;
11235274f052SJens Axboe 
112471993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
112571993e62SLinus Torvalds 	opipe = get_pipe_info(out);
11267c77f0b3SMiklos Szeredi 
11277c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11287c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11297c77f0b3SMiklos Szeredi 			return -ESPIPE;
11307c77f0b3SMiklos Szeredi 
11317c77f0b3SMiklos Szeredi 		if (!(in->f_mode & FMODE_READ))
11327c77f0b3SMiklos Szeredi 			return -EBADF;
11337c77f0b3SMiklos Szeredi 
11347c77f0b3SMiklos Szeredi 		if (!(out->f_mode & FMODE_WRITE))
11357c77f0b3SMiklos Szeredi 			return -EBADF;
11367c77f0b3SMiklos Szeredi 
11377c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11387c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11397c77f0b3SMiklos Szeredi 			return -EINVAL;
11407c77f0b3SMiklos Szeredi 
11417c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11427c77f0b3SMiklos Szeredi 	}
11437c77f0b3SMiklos Szeredi 
11447c77f0b3SMiklos Szeredi 	if (ipipe) {
1145529565dcSIngo Molnar 		if (off_in)
1146529565dcSIngo Molnar 			return -ESPIPE;
1147b92ce558SJens Axboe 		if (off_out) {
114819c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1149b92ce558SJens Axboe 				return -EINVAL;
1150cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1151b92ce558SJens Axboe 				return -EFAULT;
11527995bd28SAl Viro 		} else {
11537995bd28SAl Viro 			offset = out->f_pos;
11547995bd28SAl Viro 		}
1155529565dcSIngo Molnar 
115618c67cb9SAl Viro 		if (unlikely(!(out->f_mode & FMODE_WRITE)))
115718c67cb9SAl Viro 			return -EBADF;
115818c67cb9SAl Viro 
115918c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
116018c67cb9SAl Viro 			return -EINVAL;
116118c67cb9SAl Viro 
116218c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
116318c67cb9SAl Viro 		if (unlikely(ret < 0))
116418c67cb9SAl Viro 			return ret;
116518c67cb9SAl Viro 
1166500368f7SAl Viro 		file_start_write(out);
11677995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1168500368f7SAl Viro 		file_end_write(out);
1169a4514ebdSJens Axboe 
11707995bd28SAl Viro 		if (!off_out)
11717995bd28SAl Viro 			out->f_pos = offset;
11727995bd28SAl Viro 		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1173a4514ebdSJens Axboe 			ret = -EFAULT;
1174a4514ebdSJens Axboe 
1175a4514ebdSJens Axboe 		return ret;
1176529565dcSIngo Molnar 	}
11775274f052SJens Axboe 
11787c77f0b3SMiklos Szeredi 	if (opipe) {
1179529565dcSIngo Molnar 		if (off_out)
1180529565dcSIngo Molnar 			return -ESPIPE;
1181b92ce558SJens Axboe 		if (off_in) {
118219c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1183b92ce558SJens Axboe 				return -EINVAL;
1184cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1185b92ce558SJens Axboe 				return -EFAULT;
11867995bd28SAl Viro 		} else {
11877995bd28SAl Viro 			offset = in->f_pos;
11887995bd28SAl Viro 		}
1189529565dcSIngo Molnar 
11908924feffSAl Viro 		pipe_lock(opipe);
11918924feffSAl Viro 		ret = wait_for_space(opipe, flags);
11928924feffSAl Viro 		if (!ret)
11937995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
11948924feffSAl Viro 		pipe_unlock(opipe);
11958924feffSAl Viro 		if (ret > 0)
11968924feffSAl Viro 			wakeup_pipe_readers(opipe);
11977995bd28SAl Viro 		if (!off_in)
11987995bd28SAl Viro 			in->f_pos = offset;
11997995bd28SAl Viro 		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1200a4514ebdSJens Axboe 			ret = -EFAULT;
1201a4514ebdSJens Axboe 
1202a4514ebdSJens Axboe 		return ret;
1203529565dcSIngo Molnar 	}
12045274f052SJens Axboe 
12055274f052SJens Axboe 	return -EINVAL;
12065274f052SJens Axboe }
12075274f052SJens Axboe 
120879fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
120979fddc4eSAl Viro 			struct pipe_inode_info *pipe,
121079fddc4eSAl Viro 			unsigned flags)
1211912d35f8SJens Axboe {
121279fddc4eSAl Viro 	struct pipe_buffer buf = {
121379fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
121479fddc4eSAl Viro 		.flags = flags
121579fddc4eSAl Viro 	};
121679fddc4eSAl Viro 	size_t total = 0;
121779fddc4eSAl Viro 	int ret = 0;
121879fddc4eSAl Viro 	bool failed = false;
121979fddc4eSAl Viro 
122079fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
122179fddc4eSAl Viro 		struct page *pages[16];
1222db85a9ebSAl Viro 		ssize_t copied;
1223db85a9ebSAl Viro 		size_t start;
122479fddc4eSAl Viro 		int n;
1225912d35f8SJens Axboe 
122679fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
122779fddc4eSAl Viro 		if (copied <= 0) {
122879fddc4eSAl Viro 			ret = copied;
122979fddc4eSAl Viro 			break;
123079fddc4eSAl Viro 		}
1231912d35f8SJens Axboe 
123279fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1233db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
123479fddc4eSAl Viro 			if (!failed) {
123579fddc4eSAl Viro 				buf.page = pages[n];
123679fddc4eSAl Viro 				buf.offset = start;
123779fddc4eSAl Viro 				buf.len = size;
123879fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
123979fddc4eSAl Viro 				if (unlikely(ret < 0)) {
124079fddc4eSAl Viro 					failed = true;
124179fddc4eSAl Viro 				} else {
124279fddc4eSAl Viro 					iov_iter_advance(from, ret);
124379fddc4eSAl Viro 					total += ret;
124479fddc4eSAl Viro 				}
124579fddc4eSAl Viro 			} else {
124679fddc4eSAl Viro 				put_page(pages[n]);
124779fddc4eSAl Viro 			}
1248db85a9ebSAl Viro 			copied -= size;
1249912d35f8SJens Axboe 		}
1250912d35f8SJens Axboe 	}
125179fddc4eSAl Viro 	return total ? total : ret;
1252912d35f8SJens Axboe }
1253912d35f8SJens Axboe 
12546a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12556a14b90bSJens Axboe 			struct splice_desc *sd)
12566a14b90bSJens Axboe {
12576130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
12586130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
12596a14b90bSJens Axboe }
12606a14b90bSJens Axboe 
12616a14b90bSJens Axboe /*
12626a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
12636a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
12646a14b90bSJens Axboe  */
12656130f531SAl Viro static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
12666a14b90bSJens Axboe 			     unsigned long nr_segs, unsigned int flags)
12676a14b90bSJens Axboe {
12686a14b90bSJens Axboe 	struct pipe_inode_info *pipe;
12696a14b90bSJens Axboe 	struct splice_desc sd;
12706a14b90bSJens Axboe 	long ret;
12716130f531SAl Viro 	struct iovec iovstack[UIO_FASTIOV];
12726130f531SAl Viro 	struct iovec *iov = iovstack;
12736130f531SAl Viro 	struct iov_iter iter;
12746a14b90bSJens Axboe 
127571993e62SLinus Torvalds 	pipe = get_pipe_info(file);
12766a14b90bSJens Axboe 	if (!pipe)
12776a14b90bSJens Axboe 		return -EBADF;
12786a14b90bSJens Axboe 
1279345995faSAl Viro 	ret = import_iovec(READ, uiov, nr_segs,
1280345995faSAl Viro 			   ARRAY_SIZE(iovstack), &iov, &iter);
1281345995faSAl Viro 	if (ret < 0)
1282345995faSAl Viro 		return ret;
12836a14b90bSJens Axboe 
1284345995faSAl Viro 	sd.total_len = iov_iter_count(&iter);
12856a14b90bSJens Axboe 	sd.len = 0;
12866a14b90bSJens Axboe 	sd.flags = flags;
12876130f531SAl Viro 	sd.u.data = &iter;
12886a14b90bSJens Axboe 	sd.pos = 0;
12896a14b90bSJens Axboe 
1290345995faSAl Viro 	if (sd.total_len) {
12916130f531SAl Viro 		pipe_lock(pipe);
12926130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
129361e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1294345995faSAl Viro 	}
12956a14b90bSJens Axboe 
12966130f531SAl Viro 	kfree(iov);
12976a14b90bSJens Axboe 	return ret;
12986a14b90bSJens Axboe }
12996a14b90bSJens Axboe 
1300912d35f8SJens Axboe /*
1301912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1302912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1303912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1304912d35f8SJens Axboe  */
1305db85a9ebSAl Viro static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1306912d35f8SJens Axboe 			     unsigned long nr_segs, unsigned int flags)
1307912d35f8SJens Axboe {
1308ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
1309db85a9ebSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
1310db85a9ebSAl Viro 	struct iovec *iov = iovstack;
1311db85a9ebSAl Viro 	struct iov_iter from;
131235f3d14dSJens Axboe 	long ret;
131379fddc4eSAl Viro 	unsigned buf_flag = 0;
131479fddc4eSAl Viro 
131579fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
131679fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1317912d35f8SJens Axboe 
131871993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1319ddac0d39SJens Axboe 	if (!pipe)
1320912d35f8SJens Axboe 		return -EBADF;
1321912d35f8SJens Axboe 
1322db85a9ebSAl Viro 	ret = import_iovec(WRITE, uiov, nr_segs,
1323db85a9ebSAl Viro 			   ARRAY_SIZE(iovstack), &iov, &from);
1324db85a9ebSAl Viro 	if (ret < 0)
1325db85a9ebSAl Viro 		return ret;
1326912d35f8SJens Axboe 
13278924feffSAl Viro 	pipe_lock(pipe);
13288924feffSAl Viro 	ret = wait_for_space(pipe, flags);
132979fddc4eSAl Viro 	if (!ret)
133079fddc4eSAl Viro 		ret = iter_to_pipe(&from, pipe, buf_flag);
13318924feffSAl Viro 	pipe_unlock(pipe);
13328924feffSAl Viro 	if (ret > 0)
13338924feffSAl Viro 		wakeup_pipe_readers(pipe);
1334db85a9ebSAl Viro 	kfree(iov);
133535f3d14dSJens Axboe 	return ret;
1336912d35f8SJens Axboe }
1337912d35f8SJens Axboe 
13386a14b90bSJens Axboe /*
13396a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13406a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13416a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13426a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13436a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13446a14b90bSJens Axboe  * solutions for that:
13456a14b90bSJens Axboe  *
13466a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13476a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13486a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13496a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13506a14b90bSJens Axboe  *
13516a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13526a14b90bSJens Axboe  *
13536a14b90bSJens Axboe  */
1354836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1355836f92adSHeiko Carstens 		unsigned long, nr_segs, unsigned int, flags)
1356912d35f8SJens Axboe {
13572903ff01SAl Viro 	struct fd f;
1358912d35f8SJens Axboe 	long error;
1359912d35f8SJens Axboe 
13606a14b90bSJens Axboe 	if (unlikely(nr_segs > UIO_MAXIOV))
13616a14b90bSJens Axboe 		return -EINVAL;
13626a14b90bSJens Axboe 	else if (unlikely(!nr_segs))
13636a14b90bSJens Axboe 		return 0;
13646a14b90bSJens Axboe 
1365912d35f8SJens Axboe 	error = -EBADF;
13662903ff01SAl Viro 	f = fdget(fd);
13672903ff01SAl Viro 	if (f.file) {
13682903ff01SAl Viro 		if (f.file->f_mode & FMODE_WRITE)
13692903ff01SAl Viro 			error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
13702903ff01SAl Viro 		else if (f.file->f_mode & FMODE_READ)
13712903ff01SAl Viro 			error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1372912d35f8SJens Axboe 
13732903ff01SAl Viro 		fdput(f);
1374912d35f8SJens Axboe 	}
1375912d35f8SJens Axboe 
1376912d35f8SJens Axboe 	return error;
1377912d35f8SJens Axboe }
1378912d35f8SJens Axboe 
137976b021d0SAl Viro #ifdef CONFIG_COMPAT
138076b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
138176b021d0SAl Viro 		    unsigned int, nr_segs, unsigned int, flags)
138276b021d0SAl Viro {
138376b021d0SAl Viro 	unsigned i;
138476b021d0SAl Viro 	struct iovec __user *iov;
138576b021d0SAl Viro 	if (nr_segs > UIO_MAXIOV)
138676b021d0SAl Viro 		return -EINVAL;
138776b021d0SAl Viro 	iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
138876b021d0SAl Viro 	for (i = 0; i < nr_segs; i++) {
138976b021d0SAl Viro 		struct compat_iovec v;
139076b021d0SAl Viro 		if (get_user(v.iov_base, &iov32[i].iov_base) ||
139176b021d0SAl Viro 		    get_user(v.iov_len, &iov32[i].iov_len) ||
139276b021d0SAl Viro 		    put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
139376b021d0SAl Viro 		    put_user(v.iov_len, &iov[i].iov_len))
139476b021d0SAl Viro 			return -EFAULT;
139576b021d0SAl Viro 	}
139676b021d0SAl Viro 	return sys_vmsplice(fd, iov, nr_segs, flags);
139776b021d0SAl Viro }
139876b021d0SAl Viro #endif
139976b021d0SAl Viro 
1400836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1401836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1402836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14035274f052SJens Axboe {
14042903ff01SAl Viro 	struct fd in, out;
14055274f052SJens Axboe 	long error;
14065274f052SJens Axboe 
14075274f052SJens Axboe 	if (unlikely(!len))
14085274f052SJens Axboe 		return 0;
14095274f052SJens Axboe 
14105274f052SJens Axboe 	error = -EBADF;
14112903ff01SAl Viro 	in = fdget(fd_in);
14122903ff01SAl Viro 	if (in.file) {
14132903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
14142903ff01SAl Viro 			out = fdget(fd_out);
14152903ff01SAl Viro 			if (out.file) {
14162903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
14172903ff01SAl Viro 					error = do_splice(in.file, off_in,
14182903ff01SAl Viro 							  out.file, off_out,
1419529565dcSIngo Molnar 							  len, flags);
14202903ff01SAl Viro 				fdput(out);
14215274f052SJens Axboe 			}
14225274f052SJens Axboe 		}
14232903ff01SAl Viro 		fdput(in);
14245274f052SJens Axboe 	}
14255274f052SJens Axboe 	return error;
14265274f052SJens Axboe }
142770524490SJens Axboe 
142870524490SJens Axboe /*
1429aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1430aadd06e5SJens Axboe  * return an appropriate error.
1431aadd06e5SJens Axboe  */
14327c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1433aadd06e5SJens Axboe {
1434aadd06e5SJens Axboe 	int ret;
1435aadd06e5SJens Axboe 
1436aadd06e5SJens Axboe 	/*
1437aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1438aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1439aadd06e5SJens Axboe 	 */
1440aadd06e5SJens Axboe 	if (pipe->nrbufs)
1441aadd06e5SJens Axboe 		return 0;
1442aadd06e5SJens Axboe 
1443aadd06e5SJens Axboe 	ret = 0;
144461e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1445aadd06e5SJens Axboe 
1446aadd06e5SJens Axboe 	while (!pipe->nrbufs) {
1447aadd06e5SJens Axboe 		if (signal_pending(current)) {
1448aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1449aadd06e5SJens Axboe 			break;
1450aadd06e5SJens Axboe 		}
1451aadd06e5SJens Axboe 		if (!pipe->writers)
1452aadd06e5SJens Axboe 			break;
1453aadd06e5SJens Axboe 		if (!pipe->waiting_writers) {
1454aadd06e5SJens Axboe 			if (flags & SPLICE_F_NONBLOCK) {
1455aadd06e5SJens Axboe 				ret = -EAGAIN;
1456aadd06e5SJens Axboe 				break;
1457aadd06e5SJens Axboe 			}
1458aadd06e5SJens Axboe 		}
1459aadd06e5SJens Axboe 		pipe_wait(pipe);
1460aadd06e5SJens Axboe 	}
1461aadd06e5SJens Axboe 
146261e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1463aadd06e5SJens Axboe 	return ret;
1464aadd06e5SJens Axboe }
1465aadd06e5SJens Axboe 
1466aadd06e5SJens Axboe /*
1467aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1468aadd06e5SJens Axboe  * return an appropriate error.
1469aadd06e5SJens Axboe  */
14707c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1471aadd06e5SJens Axboe {
1472aadd06e5SJens Axboe 	int ret;
1473aadd06e5SJens Axboe 
1474aadd06e5SJens Axboe 	/*
1475aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1476aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1477aadd06e5SJens Axboe 	 */
147835f3d14dSJens Axboe 	if (pipe->nrbufs < pipe->buffers)
1479aadd06e5SJens Axboe 		return 0;
1480aadd06e5SJens Axboe 
1481aadd06e5SJens Axboe 	ret = 0;
148261e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1483aadd06e5SJens Axboe 
148435f3d14dSJens Axboe 	while (pipe->nrbufs >= pipe->buffers) {
1485aadd06e5SJens Axboe 		if (!pipe->readers) {
1486aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1487aadd06e5SJens Axboe 			ret = -EPIPE;
1488aadd06e5SJens Axboe 			break;
1489aadd06e5SJens Axboe 		}
1490aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1491aadd06e5SJens Axboe 			ret = -EAGAIN;
1492aadd06e5SJens Axboe 			break;
1493aadd06e5SJens Axboe 		}
1494aadd06e5SJens Axboe 		if (signal_pending(current)) {
1495aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1496aadd06e5SJens Axboe 			break;
1497aadd06e5SJens Axboe 		}
1498aadd06e5SJens Axboe 		pipe->waiting_writers++;
1499aadd06e5SJens Axboe 		pipe_wait(pipe);
1500aadd06e5SJens Axboe 		pipe->waiting_writers--;
1501aadd06e5SJens Axboe 	}
1502aadd06e5SJens Axboe 
150361e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1504aadd06e5SJens Axboe 	return ret;
1505aadd06e5SJens Axboe }
1506aadd06e5SJens Axboe 
1507aadd06e5SJens Axboe /*
15087c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15097c77f0b3SMiklos Szeredi  */
15107c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15117c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15127c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15137c77f0b3SMiklos Szeredi {
15147c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15157c77f0b3SMiklos Szeredi 	int ret = 0, nbuf;
15167c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15177c77f0b3SMiklos Szeredi 
15187c77f0b3SMiklos Szeredi 
15197c77f0b3SMiklos Szeredi retry:
15207c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15217c77f0b3SMiklos Szeredi 	if (ret)
15227c77f0b3SMiklos Szeredi 		return ret;
15237c77f0b3SMiklos Szeredi 
15247c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15257c77f0b3SMiklos Szeredi 	if (ret)
15267c77f0b3SMiklos Szeredi 		return ret;
15277c77f0b3SMiklos Szeredi 
15287c77f0b3SMiklos Szeredi 	/*
15297c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15307c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15317c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15327c77f0b3SMiklos Szeredi 	 */
15337c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15347c77f0b3SMiklos Szeredi 
15357c77f0b3SMiklos Szeredi 	do {
15367c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15377c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15387c77f0b3SMiklos Szeredi 			if (!ret)
15397c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15407c77f0b3SMiklos Szeredi 			break;
15417c77f0b3SMiklos Szeredi 		}
15427c77f0b3SMiklos Szeredi 
15437c77f0b3SMiklos Szeredi 		if (!ipipe->nrbufs && !ipipe->writers)
15447c77f0b3SMiklos Szeredi 			break;
15457c77f0b3SMiklos Szeredi 
15467c77f0b3SMiklos Szeredi 		/*
15477c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15487c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15497c77f0b3SMiklos Szeredi 		 */
155035f3d14dSJens Axboe 		if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
15517c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15527c77f0b3SMiklos Szeredi 			if (ret)
15537c77f0b3SMiklos Szeredi 				break;
15547c77f0b3SMiklos Szeredi 
15557c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15567c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15577c77f0b3SMiklos Szeredi 				break;
15587c77f0b3SMiklos Szeredi 			}
15597c77f0b3SMiklos Szeredi 
15607c77f0b3SMiklos Szeredi 			/*
15617c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15627c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15637c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
15647c77f0b3SMiklos Szeredi 			 */
15657c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
15667c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
15677c77f0b3SMiklos Szeredi 			goto retry;
15687c77f0b3SMiklos Szeredi 		}
15697c77f0b3SMiklos Szeredi 
15707c77f0b3SMiklos Szeredi 		ibuf = ipipe->bufs + ipipe->curbuf;
157135f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
15727c77f0b3SMiklos Szeredi 		obuf = opipe->bufs + nbuf;
15737c77f0b3SMiklos Szeredi 
15747c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
15757c77f0b3SMiklos Szeredi 			/*
15767c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
15777c77f0b3SMiklos Szeredi 			 */
15787c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15797c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
15807c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
158135f3d14dSJens Axboe 			ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
15827c77f0b3SMiklos Szeredi 			ipipe->nrbufs--;
15837c77f0b3SMiklos Szeredi 			input_wakeup = true;
15847c77f0b3SMiklos Szeredi 		} else {
15857c77f0b3SMiklos Szeredi 			/*
15867c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
15877c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
15887c77f0b3SMiklos Szeredi 			 */
15897bf2d1dfSMiklos Szeredi 			pipe_buf_get(ipipe, ibuf);
15907c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15917c77f0b3SMiklos Szeredi 
15927c77f0b3SMiklos Szeredi 			/*
15937c77f0b3SMiklos Szeredi 			 * Don't inherit the gift flag, we need to
15947c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
15957c77f0b3SMiklos Szeredi 			 */
15967c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
15977c77f0b3SMiklos Szeredi 
15987c77f0b3SMiklos Szeredi 			obuf->len = len;
15997c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
16007c77f0b3SMiklos Szeredi 			ibuf->offset += obuf->len;
16017c77f0b3SMiklos Szeredi 			ibuf->len -= obuf->len;
16027c77f0b3SMiklos Szeredi 		}
16037c77f0b3SMiklos Szeredi 		ret += obuf->len;
16047c77f0b3SMiklos Szeredi 		len -= obuf->len;
16057c77f0b3SMiklos Szeredi 	} while (len);
16067c77f0b3SMiklos Szeredi 
16077c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16087c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16097c77f0b3SMiklos Szeredi 
16107c77f0b3SMiklos Szeredi 	/*
16117c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16127c77f0b3SMiklos Szeredi 	 */
1613825cdcb1SNamhyung Kim 	if (ret > 0)
1614825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1615825cdcb1SNamhyung Kim 
16167c77f0b3SMiklos Szeredi 	if (input_wakeup)
16177c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16187c77f0b3SMiklos Szeredi 
16197c77f0b3SMiklos Szeredi 	return ret;
16207c77f0b3SMiklos Szeredi }
16217c77f0b3SMiklos Szeredi 
16227c77f0b3SMiklos Szeredi /*
162370524490SJens Axboe  * Link contents of ipipe to opipe.
162470524490SJens Axboe  */
162570524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
162670524490SJens Axboe 		     struct pipe_inode_info *opipe,
162770524490SJens Axboe 		     size_t len, unsigned int flags)
162870524490SJens Axboe {
162970524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
1630aadd06e5SJens Axboe 	int ret = 0, i = 0, nbuf;
163170524490SJens Axboe 
163270524490SJens Axboe 	/*
163370524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
163461e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
163570524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
163670524490SJens Axboe 	 */
163761e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
163870524490SJens Axboe 
1639aadd06e5SJens Axboe 	do {
164070524490SJens Axboe 		if (!opipe->readers) {
164170524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
164270524490SJens Axboe 			if (!ret)
164370524490SJens Axboe 				ret = -EPIPE;
164470524490SJens Axboe 			break;
164570524490SJens Axboe 		}
164670524490SJens Axboe 
164770524490SJens Axboe 		/*
1648aadd06e5SJens Axboe 		 * If we have iterated all input buffers or ran out of
1649aadd06e5SJens Axboe 		 * output room, break.
165070524490SJens Axboe 		 */
165135f3d14dSJens Axboe 		if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1652aadd06e5SJens Axboe 			break;
1653aadd06e5SJens Axboe 
165435f3d14dSJens Axboe 		ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
165535f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
165670524490SJens Axboe 
165770524490SJens Axboe 		/*
165870524490SJens Axboe 		 * Get a reference to this pipe buffer,
165970524490SJens Axboe 		 * so we can copy the contents over.
166070524490SJens Axboe 		 */
16617bf2d1dfSMiklos Szeredi 		pipe_buf_get(ipipe, ibuf);
166270524490SJens Axboe 
166370524490SJens Axboe 		obuf = opipe->bufs + nbuf;
166470524490SJens Axboe 		*obuf = *ibuf;
166570524490SJens Axboe 
16667afa6fd0SJens Axboe 		/*
16677afa6fd0SJens Axboe 		 * Don't inherit the gift flag, we need to
16687afa6fd0SJens Axboe 		 * prevent multiple steals of this page.
16697afa6fd0SJens Axboe 		 */
16707afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
16717afa6fd0SJens Axboe 
167270524490SJens Axboe 		if (obuf->len > len)
167370524490SJens Axboe 			obuf->len = len;
167470524490SJens Axboe 
167570524490SJens Axboe 		opipe->nrbufs++;
167670524490SJens Axboe 		ret += obuf->len;
167770524490SJens Axboe 		len -= obuf->len;
1678aadd06e5SJens Axboe 		i++;
1679aadd06e5SJens Axboe 	} while (len);
168070524490SJens Axboe 
168102cf01aeSJens Axboe 	/*
168202cf01aeSJens Axboe 	 * return EAGAIN if we have the potential of some data in the
168302cf01aeSJens Axboe 	 * future, otherwise just return 0
168402cf01aeSJens Axboe 	 */
168502cf01aeSJens Axboe 	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
168602cf01aeSJens Axboe 		ret = -EAGAIN;
168702cf01aeSJens Axboe 
168861e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
168961e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
169070524490SJens Axboe 
1691aadd06e5SJens Axboe 	/*
1692aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1693aadd06e5SJens Axboe 	 */
1694825cdcb1SNamhyung Kim 	if (ret > 0)
1695825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
169670524490SJens Axboe 
169770524490SJens Axboe 	return ret;
169870524490SJens Axboe }
169970524490SJens Axboe 
170070524490SJens Axboe /*
170170524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
170270524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
170370524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
170470524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
170570524490SJens Axboe  */
170670524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
170770524490SJens Axboe 		   unsigned int flags)
170870524490SJens Axboe {
170971993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
171071993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1711aadd06e5SJens Axboe 	int ret = -EINVAL;
171270524490SJens Axboe 
171370524490SJens Axboe 	/*
1714aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1715aadd06e5SJens Axboe 	 * copying the data.
171670524490SJens Axboe 	 */
1717aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1718aadd06e5SJens Axboe 		/*
1719aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1720aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1721aadd06e5SJens Axboe 		 */
17227c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1723aadd06e5SJens Axboe 		if (!ret) {
17247c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
172502cf01aeSJens Axboe 			if (!ret)
1726aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1727aadd06e5SJens Axboe 		}
1728aadd06e5SJens Axboe 	}
172970524490SJens Axboe 
1730aadd06e5SJens Axboe 	return ret;
173170524490SJens Axboe }
173270524490SJens Axboe 
1733836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
173470524490SJens Axboe {
17352903ff01SAl Viro 	struct fd in;
17362903ff01SAl Viro 	int error;
173770524490SJens Axboe 
173870524490SJens Axboe 	if (unlikely(!len))
173970524490SJens Axboe 		return 0;
174070524490SJens Axboe 
174170524490SJens Axboe 	error = -EBADF;
17422903ff01SAl Viro 	in = fdget(fdin);
17432903ff01SAl Viro 	if (in.file) {
17442903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
17452903ff01SAl Viro 			struct fd out = fdget(fdout);
17462903ff01SAl Viro 			if (out.file) {
17472903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
17482903ff01SAl Viro 					error = do_tee(in.file, out.file,
17492903ff01SAl Viro 							len, flags);
17502903ff01SAl Viro 				fdput(out);
175170524490SJens Axboe 			}
175270524490SJens Axboe 		}
17532903ff01SAl Viro  		fdput(in);
175470524490SJens Axboe  	}
175570524490SJens Axboe 
175670524490SJens Axboe 	return error;
175770524490SJens Axboe }
1758