xref: /openbmc/linux/fs/splice.c (revision c928f642)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25274f052SJens Axboe /*
35274f052SJens Axboe  * "splice": joining two ropes together by interweaving their strands.
45274f052SJens Axboe  *
55274f052SJens Axboe  * This is the "extended pipe" functionality, where a pipe is used as
65274f052SJens Axboe  * an arbitrary in-memory buffer. Think of a pipe as a small kernel
75274f052SJens Axboe  * buffer that you can use to transfer data from one end to the other.
85274f052SJens Axboe  *
95274f052SJens Axboe  * The traditional unix read/write is extended with a "splice()" operation
105274f052SJens Axboe  * that transfers data buffers to or from a pipe buffer.
115274f052SJens Axboe  *
125274f052SJens Axboe  * Named by Larry McVoy, original implementation from Linus, extended by
13c2058e06SJens Axboe  * Jens to support splicing to files, network, direct splicing, etc and
14c2058e06SJens Axboe  * fixing lots of bugs.
155274f052SJens Axboe  *
160fe23479SJens Axboe  * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
17c2058e06SJens Axboe  * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18c2058e06SJens Axboe  * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
195274f052SJens Axboe  *
205274f052SJens Axboe  */
21be297968SChristoph Hellwig #include <linux/bvec.h>
225274f052SJens Axboe #include <linux/fs.h>
235274f052SJens Axboe #include <linux/file.h>
245274f052SJens Axboe #include <linux/pagemap.h>
25d6b29d7cSJens Axboe #include <linux/splice.h>
2608e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h>
275274f052SJens Axboe #include <linux/mm_inline.h>
285abc97aaSJens Axboe #include <linux/swap.h>
294f6f0bd2SJens Axboe #include <linux/writeback.h>
30630d9c47SPaul Gortmaker #include <linux/export.h>
314f6f0bd2SJens Axboe #include <linux/syscalls.h>
32912d35f8SJens Axboe #include <linux/uio.h>
3329ce2058SJames Morris #include <linux/security.h>
345a0e3ad6STejun Heo #include <linux/gfp.h>
3535f9c09fSEric Dumazet #include <linux/socket.h>
3676b021d0SAl Viro #include <linux/compat.h>
37174cd4b1SIngo Molnar #include <linux/sched/signal.h>
38174cd4b1SIngo Molnar 
3906ae43f3SAl Viro #include "internal.h"
405274f052SJens Axboe 
4183f9135bSJens Axboe /*
4283f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
4383f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
4483f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
4583f9135bSJens Axboe  * attempt to reuse this page for another destination.
4683f9135bSJens Axboe  */
47c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
485abc97aaSJens Axboe 		struct pipe_buffer *buf)
495abc97aaSJens Axboe {
505abc97aaSJens Axboe 	struct page *page = buf->page;
519e94cd4fSJens Axboe 	struct address_space *mapping;
525abc97aaSJens Axboe 
539e0267c2SJens Axboe 	lock_page(page);
549e0267c2SJens Axboe 
559e94cd4fSJens Axboe 	mapping = page_mapping(page);
569e94cd4fSJens Axboe 	if (mapping) {
575abc97aaSJens Axboe 		WARN_ON(!PageUptodate(page));
585abc97aaSJens Axboe 
59ad8d6f0aSJens Axboe 		/*
609e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
619e94cd4fSJens Axboe 		 * writeback completing on this page, since we'll remove it
629e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
639e94cd4fSJens Axboe 		 * page, allowing the disk blocks to be reused by someone else
649e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
659e94cd4fSJens Axboe 		 * ensues.
66ad8d6f0aSJens Axboe 		 */
67ad8d6f0aSJens Axboe 		wait_on_page_writeback(page);
68ad8d6f0aSJens Axboe 
69266cf658SDavid Howells 		if (page_has_private(page) &&
70266cf658SDavid Howells 		    !try_to_release_page(page, GFP_KERNEL))
71ca39d651SJens Axboe 			goto out_unlock;
724f6f0bd2SJens Axboe 
739e94cd4fSJens Axboe 		/*
749e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
759e94cd4fSJens Axboe 		 * and return good.
769e94cd4fSJens Axboe 		 */
779e94cd4fSJens Axboe 		if (remove_mapping(mapping, page)) {
781432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
79c928f642SChristoph Hellwig 			return true;
805abc97aaSJens Axboe 		}
819e94cd4fSJens Axboe 	}
829e94cd4fSJens Axboe 
839e94cd4fSJens Axboe 	/*
849e94cd4fSJens Axboe 	 * Raced with truncate or failed to remove page from current
859e94cd4fSJens Axboe 	 * address space, unlock and return failure.
869e94cd4fSJens Axboe 	 */
87ca39d651SJens Axboe out_unlock:
889e94cd4fSJens Axboe 	unlock_page(page);
89c928f642SChristoph Hellwig 	return false;
909e94cd4fSJens Axboe }
915abc97aaSJens Axboe 
9276ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
935274f052SJens Axboe 					struct pipe_buffer *buf)
945274f052SJens Axboe {
9509cbfeafSKirill A. Shutemov 	put_page(buf->page);
961432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
975274f052SJens Axboe }
985274f052SJens Axboe 
990845718dSJens Axboe /*
1000845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
1010845718dSJens Axboe  * is a page cache page, IO may be in flight.
1020845718dSJens Axboe  */
103cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1045274f052SJens Axboe 				       struct pipe_buffer *buf)
1055274f052SJens Axboe {
1065274f052SJens Axboe 	struct page *page = buf->page;
10749d0b21bSJens Axboe 	int err;
1085274f052SJens Axboe 
1095274f052SJens Axboe 	if (!PageUptodate(page)) {
11049d0b21bSJens Axboe 		lock_page(page);
1115274f052SJens Axboe 
11249d0b21bSJens Axboe 		/*
11349d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
11473d62d83SIngo Molnar 		 * splice, if this is the first page.
11549d0b21bSJens Axboe 		 */
1165274f052SJens Axboe 		if (!page->mapping) {
11749d0b21bSJens Axboe 			err = -ENODATA;
11849d0b21bSJens Axboe 			goto error;
1195274f052SJens Axboe 		}
1205274f052SJens Axboe 
12149d0b21bSJens Axboe 		/*
12273d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
12349d0b21bSJens Axboe 		 */
12449d0b21bSJens Axboe 		if (!PageUptodate(page)) {
12549d0b21bSJens Axboe 			err = -EIO;
12649d0b21bSJens Axboe 			goto error;
12749d0b21bSJens Axboe 		}
12849d0b21bSJens Axboe 
12949d0b21bSJens Axboe 		/*
130f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
13149d0b21bSJens Axboe 		 */
13249d0b21bSJens Axboe 		unlock_page(page);
13349d0b21bSJens Axboe 	}
13449d0b21bSJens Axboe 
135f84d7519SJens Axboe 	return 0;
13649d0b21bSJens Axboe error:
13749d0b21bSJens Axboe 	unlock_page(page);
138f84d7519SJens Axboe 	return err;
13970524490SJens Axboe }
14070524490SJens Axboe 
141708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
142cac36bb0SJens Axboe 	.confirm	= page_cache_pipe_buf_confirm,
1435274f052SJens Axboe 	.release	= page_cache_pipe_buf_release,
144c928f642SChristoph Hellwig 	.try_steal	= page_cache_pipe_buf_try_steal,
145f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
1465274f052SJens Axboe };
1475274f052SJens Axboe 
148c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
149912d35f8SJens Axboe 		struct pipe_buffer *buf)
150912d35f8SJens Axboe {
1517afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152c928f642SChristoph Hellwig 		return false;
1537afa6fd0SJens Axboe 
1541432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
155c928f642SChristoph Hellwig 	return generic_pipe_buf_try_steal(pipe, buf);
156912d35f8SJens Axboe }
157912d35f8SJens Axboe 
158d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159912d35f8SJens Axboe 	.release	= page_cache_pipe_buf_release,
160c928f642SChristoph Hellwig 	.try_steal	= user_page_pipe_buf_try_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();
1670ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1680ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_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;
1878cefc107SDavid Howells 	unsigned int tail = pipe->tail;
1888cefc107SDavid Howells 	unsigned int head = pipe->head;
1898cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
1908924feffSAl Viro 	int ret = 0, page_nr = 0;
1915274f052SJens Axboe 
192d6785d91SRabin Vincent 	if (!spd_pages)
193d6785d91SRabin Vincent 		return 0;
194d6785d91SRabin Vincent 
1958924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1965274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1975274f052SJens Axboe 		ret = -EPIPE;
1988924feffSAl Viro 		goto out;
1995274f052SJens Axboe 	}
2005274f052SJens Axboe 
2016718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2028cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
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 
2118cefc107SDavid Howells 		head++;
2128cefc107SDavid Howells 		pipe->head = head;
213912d35f8SJens Axboe 		page_nr++;
214912d35f8SJens Axboe 		ret += buf->len;
215912d35f8SJens Axboe 
216912d35f8SJens Axboe 		if (!--spd->nr_pages)
2175274f052SJens Axboe 			break;
2185274f052SJens Axboe 	}
2195274f052SJens Axboe 
22029e35094SLinus Torvalds 	if (!ret)
22129e35094SLinus Torvalds 		ret = -EAGAIN;
22229e35094SLinus Torvalds 
2238924feffSAl Viro out:
22400de00bdSJens Axboe 	while (page_nr < spd_pages)
225bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2265274f052SJens Axboe 
2275274f052SJens Axboe 	return ret;
2285274f052SJens Axboe }
2292b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2305274f052SJens Axboe 
23179fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
23279fddc4eSAl Viro {
2338cefc107SDavid Howells 	unsigned int head = pipe->head;
2348cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2358cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
23679fddc4eSAl Viro 	int ret;
23779fddc4eSAl Viro 
23879fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23979fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
24079fddc4eSAl Viro 		ret = -EPIPE;
2416718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
24279fddc4eSAl Viro 		ret = -EAGAIN;
24379fddc4eSAl Viro 	} else {
2448cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2458cefc107SDavid Howells 		pipe->head = head + 1;
24679fddc4eSAl Viro 		return buf->len;
24779fddc4eSAl Viro 	}
248a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24979fddc4eSAl Viro 	return ret;
25079fddc4eSAl Viro }
25179fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
25279fddc4eSAl Viro 
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 {
2596718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
260047fe360SEric Dumazet 
2618cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2628cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
26335f3d14dSJens Axboe 		return 0;
26435f3d14dSJens Axboe 
2658cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2668cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2676da2ec56SKees Cook 				     GFP_KERNEL);
26835f3d14dSJens Axboe 
26935f3d14dSJens Axboe 	if (spd->pages && spd->partial)
27035f3d14dSJens Axboe 		return 0;
27135f3d14dSJens Axboe 
27235f3d14dSJens Axboe 	kfree(spd->pages);
27335f3d14dSJens Axboe 	kfree(spd->partial);
27435f3d14dSJens Axboe 	return -ENOMEM;
27535f3d14dSJens Axboe }
27635f3d14dSJens Axboe 
277047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27835f3d14dSJens Axboe {
279047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
28035f3d14dSJens Axboe 		return;
28135f3d14dSJens Axboe 
28235f3d14dSJens Axboe 	kfree(spd->pages);
28335f3d14dSJens Axboe 	kfree(spd->partial);
28435f3d14dSJens Axboe }
28535f3d14dSJens Axboe 
28683f9135bSJens Axboe /**
28783f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
28883f9135bSJens Axboe  * @in:		file to splice from
289932cc6d4SJens Axboe  * @ppos:	position in @in
29083f9135bSJens Axboe  * @pipe:	pipe to splice to
29183f9135bSJens Axboe  * @len:	number of bytes to splice
29283f9135bSJens Axboe  * @flags:	splice modifier flags
29383f9135bSJens Axboe  *
294932cc6d4SJens Axboe  * Description:
295932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
29682c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
297932cc6d4SJens Axboe  *
29883f9135bSJens Axboe  */
299cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
300cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
301cbb7e577SJens Axboe 				 unsigned int flags)
3025274f052SJens Axboe {
30382c156f8SAl Viro 	struct iov_iter to;
30482c156f8SAl Viro 	struct kiocb kiocb;
3058cefc107SDavid Howells 	unsigned int i_head;
3068cefc107SDavid Howells 	int ret;
307be64f884SBoaz Harrosh 
308aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len);
3098cefc107SDavid Howells 	i_head = to.head;
31082c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
31182c156f8SAl Viro 	kiocb.ki_pos = *ppos;
312bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
313723590edSMiklos Szeredi 	if (ret > 0) {
31482c156f8SAl Viro 		*ppos = kiocb.ki_pos;
315723590edSMiklos Szeredi 		file_accessed(in);
31682c156f8SAl Viro 	} else if (ret < 0) {
3178cefc107SDavid Howells 		to.head = i_head;
31882c156f8SAl Viro 		to.iov_offset = 0;
31982c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
32082c156f8SAl Viro 		/*
32182c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
32282c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
32382c156f8SAl Viro 		 */
32482c156f8SAl Viro 		if (ret == -EFAULT)
32582c156f8SAl Viro 			ret = -EAGAIN;
326723590edSMiklos Szeredi 	}
3275274f052SJens Axboe 
3285274f052SJens Axboe 	return ret;
3295274f052SJens Axboe }
330059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
331059a8f37SJens Axboe 
332241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3336818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
334c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
3356818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
3366818173bSMiklos Szeredi };
3376818173bSMiklos Szeredi 
33828a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
33928a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
34028a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
34128a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
34228a625cbSMiklos Szeredi };
34328a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
34428a625cbSMiklos Szeredi 
345523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
3466818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
3476818173bSMiklos Szeredi {
3486818173bSMiklos Szeredi 	mm_segment_t old_fs;
3496818173bSMiklos Szeredi 	loff_t pos = offset;
3506818173bSMiklos Szeredi 	ssize_t res;
3516818173bSMiklos Szeredi 
3526818173bSMiklos Szeredi 	old_fs = get_fs();
353736706beSLinus Torvalds 	set_fs(KERNEL_DS);
3546818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
355793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
3566818173bSMiklos Szeredi 	set_fs(old_fs);
3576818173bSMiklos Szeredi 
3586818173bSMiklos Szeredi 	return res;
3596818173bSMiklos Szeredi }
3606818173bSMiklos Szeredi 
36182c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
3626818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
3636818173bSMiklos Szeredi 				 unsigned int flags)
3646818173bSMiklos Szeredi {
365523ac9afSAl Viro 	struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
366523ac9afSAl Viro 	struct iov_iter to;
367523ac9afSAl Viro 	struct page **pages;
3686818173bSMiklos Szeredi 	unsigned int nr_pages;
3698cefc107SDavid Howells 	unsigned int mask;
37013c0f52bSAl Viro 	size_t offset, base, copied = 0;
3716818173bSMiklos Szeredi 	ssize_t res;
3726818173bSMiklos Szeredi 	int i;
3736818173bSMiklos Szeredi 
3746718b6f8SDavid Howells 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
375523ac9afSAl Viro 		return -EAGAIN;
376523ac9afSAl Viro 
377523ac9afSAl Viro 	/*
378523ac9afSAl Viro 	 * Try to keep page boundaries matching to source pagecache ones -
379523ac9afSAl Viro 	 * it probably won't be much help, but...
380523ac9afSAl Viro 	 */
381523ac9afSAl Viro 	offset = *ppos & ~PAGE_MASK;
382523ac9afSAl Viro 
383aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len + offset);
384523ac9afSAl Viro 
38513c0f52bSAl Viro 	res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
386523ac9afSAl Viro 	if (res <= 0)
38735f3d14dSJens Axboe 		return -ENOMEM;
38835f3d14dSJens Axboe 
38913c0f52bSAl Viro 	nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
390523ac9afSAl Viro 
39135f3d14dSJens Axboe 	vec = __vec;
392523ac9afSAl Viro 	if (nr_pages > PIPE_DEF_BUFFERS) {
3936da2ec56SKees Cook 		vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
394523ac9afSAl Viro 		if (unlikely(!vec)) {
395523ac9afSAl Viro 			res = -ENOMEM;
396523ac9afSAl Viro 			goto out;
397523ac9afSAl Viro 		}
39835f3d14dSJens Axboe 	}
39935f3d14dSJens Axboe 
4008cefc107SDavid Howells 	mask = pipe->ring_size - 1;
4018cefc107SDavid Howells 	pipe->bufs[to.head & mask].offset = offset;
4028cefc107SDavid Howells 	pipe->bufs[to.head & mask].len -= offset;
4036818173bSMiklos Szeredi 
404523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++) {
405523ac9afSAl Viro 		size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
406523ac9afSAl Viro 		vec[i].iov_base = page_address(pages[i]) + offset;
4076818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
4086818173bSMiklos Szeredi 		len -= this_len;
4096818173bSMiklos Szeredi 		offset = 0;
4106818173bSMiklos Szeredi 	}
4116818173bSMiklos Szeredi 
412523ac9afSAl Viro 	res = kernel_readv(in, vec, nr_pages, *ppos);
413523ac9afSAl Viro 	if (res > 0) {
414523ac9afSAl Viro 		copied = res;
4156818173bSMiklos Szeredi 		*ppos += res;
416523ac9afSAl Viro 	}
4176818173bSMiklos Szeredi 
41835f3d14dSJens Axboe 	if (vec != __vec)
41935f3d14dSJens Axboe 		kfree(vec);
420523ac9afSAl Viro out:
421523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++)
422523ac9afSAl Viro 		put_page(pages[i]);
423523ac9afSAl Viro 	kvfree(pages);
424523ac9afSAl Viro 	iov_iter_advance(&to, copied);	/* truncates and discards */
4256818173bSMiklos Szeredi 	return res;
4266818173bSMiklos Szeredi }
4276818173bSMiklos Szeredi 
4285274f052SJens Axboe /*
4294f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
430016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4315274f052SJens Axboe  */
43276ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4335274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4345274f052SJens Axboe {
4356a14b90bSJens Axboe 	struct file *file = sd->u.file;
4365274f052SJens Axboe 	loff_t pos = sd->pos;
437a8adbe37SMichał Mirosław 	int more;
4385274f052SJens Axboe 
43972c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
440a8adbe37SMichał Mirosław 		return -EINVAL;
441a8adbe37SMichał Mirosław 
44235f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
443ae62ca7bSEric Dumazet 
4448cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4458cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
44635f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
447ae62ca7bSEric Dumazet 
448a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
449f84d7519SJens Axboe 				    sd->len, &pos, more);
4505274f052SJens Axboe }
4515274f052SJens Axboe 
452b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
453b3c2d2ddSMiklos Szeredi {
454b3c2d2ddSMiklos Szeredi 	smp_mb();
4550ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4560ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
457b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
458b3c2d2ddSMiklos Szeredi }
459b3c2d2ddSMiklos Szeredi 
460b3c2d2ddSMiklos Szeredi /**
461b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
462b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
463b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
464b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
465b3c2d2ddSMiklos Szeredi  *
466b3c2d2ddSMiklos Szeredi  * Description:
467b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
468b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
469b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
470b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
471b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
472b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
473b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
474b3c2d2ddSMiklos Szeredi  *
475b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
476b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
477b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
478b3c2d2ddSMiklos Szeredi  *    destination.
479b3c2d2ddSMiklos Szeredi  */
48096f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
481b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
482b3c2d2ddSMiklos Szeredi {
4838cefc107SDavid Howells 	unsigned int head = pipe->head;
4848cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4858cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
486b3c2d2ddSMiklos Szeredi 	int ret;
487b3c2d2ddSMiklos Szeredi 
488ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4898cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
490b3c2d2ddSMiklos Szeredi 
491b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
492b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
493b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
494b3c2d2ddSMiklos Szeredi 
495fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
496a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
497b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
498b3c2d2ddSMiklos Szeredi 				ret = 0;
499b3c2d2ddSMiklos Szeredi 			return ret;
500b3c2d2ddSMiklos Szeredi 		}
501a8adbe37SMichał Mirosław 
502a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
503a8adbe37SMichał Mirosław 		if (ret <= 0)
504a8adbe37SMichał Mirosław 			return ret;
505a8adbe37SMichał Mirosław 
506b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
507b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
508b3c2d2ddSMiklos Szeredi 
509b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
510b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
511b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
512b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
513b3c2d2ddSMiklos Szeredi 
514b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
515a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5168cefc107SDavid Howells 			tail++;
5178cefc107SDavid Howells 			pipe->tail = tail;
5186447a3cfSAl Viro 			if (pipe->files)
519b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
520b3c2d2ddSMiklos Szeredi 		}
521b3c2d2ddSMiklos Szeredi 
522b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
523b3c2d2ddSMiklos Szeredi 			return 0;
524b3c2d2ddSMiklos Szeredi 	}
525b3c2d2ddSMiklos Szeredi 
526b3c2d2ddSMiklos Szeredi 	return 1;
527b3c2d2ddSMiklos Szeredi }
528b3c2d2ddSMiklos Szeredi 
529b3c2d2ddSMiklos Szeredi /**
530b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
531b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
532b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
533b3c2d2ddSMiklos Szeredi  *
534b3c2d2ddSMiklos Szeredi  * Description:
535b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
536b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
537b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
538b3c2d2ddSMiklos Szeredi  */
53996f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
540b3c2d2ddSMiklos Szeredi {
541c725bfceSJan Kara 	/*
542c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
543c725bfceSJan Kara 	 * always buffers available
544c725bfceSJan Kara 	 */
545c725bfceSJan Kara 	if (signal_pending(current))
546c725bfceSJan Kara 		return -ERESTARTSYS;
547c725bfceSJan Kara 
5488cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
549b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
550b3c2d2ddSMiklos Szeredi 			return 0;
551b3c2d2ddSMiklos Szeredi 
552a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
553b3c2d2ddSMiklos Szeredi 			return 0;
554b3c2d2ddSMiklos Szeredi 
555b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
556b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
557b3c2d2ddSMiklos Szeredi 
558b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
559b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
560b3c2d2ddSMiklos Szeredi 
561b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
562b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
563b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
564b3c2d2ddSMiklos Szeredi 		}
565b3c2d2ddSMiklos Szeredi 
566b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
567b3c2d2ddSMiklos Szeredi 	}
568b3c2d2ddSMiklos Szeredi 
569b3c2d2ddSMiklos Szeredi 	return 1;
570b3c2d2ddSMiklos Szeredi }
571b3c2d2ddSMiklos Szeredi 
572b3c2d2ddSMiklos Szeredi /**
573b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
574b80901bbSRandy Dunlap  * @sd:		information about the splice operation
575b3c2d2ddSMiklos Szeredi  *
576b3c2d2ddSMiklos Szeredi  * Description:
577b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
578b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
579b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
580b3c2d2ddSMiklos Szeredi  */
58196f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
582b3c2d2ddSMiklos Szeredi {
583b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
584b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
585b3c2d2ddSMiklos Szeredi }
586b3c2d2ddSMiklos Szeredi 
587b3c2d2ddSMiklos Szeredi /**
588b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
589b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
590b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
591b3c2d2ddSMiklos Szeredi  *
592b3c2d2ddSMiklos Szeredi  * Description:
593b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
594b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
595b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
596b3c2d2ddSMiklos Szeredi  */
59796f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
598b3c2d2ddSMiklos Szeredi {
599b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
600b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
601b3c2d2ddSMiklos Szeredi }
602b3c2d2ddSMiklos Szeredi 
603932cc6d4SJens Axboe /**
604932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
605932cc6d4SJens Axboe  * @pipe:	pipe to splice from
606932cc6d4SJens Axboe  * @sd:		information to @actor
607932cc6d4SJens Axboe  * @actor:	handler that splices the data
608932cc6d4SJens Axboe  *
609932cc6d4SJens Axboe  * Description:
610932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
611932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
612932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
613932cc6d4SJens Axboe  *    pipe_to_user.
614932cc6d4SJens Axboe  *
61583f9135bSJens Axboe  */
616c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
617c66ab6faSJens Axboe 			   splice_actor *actor)
6185274f052SJens Axboe {
619b3c2d2ddSMiklos Szeredi 	int ret;
6205274f052SJens Axboe 
621b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
622b3c2d2ddSMiklos Szeredi 	do {
623c2489e07SJan Kara 		cond_resched();
624b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
625b3c2d2ddSMiklos Szeredi 		if (ret > 0)
626b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
627b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
628b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6295274f052SJens Axboe 
630b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6315274f052SJens Axboe }
63240bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6335274f052SJens Axboe 
634932cc6d4SJens Axboe /**
635932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
636932cc6d4SJens Axboe  * @pipe:	pipe to splice from
637932cc6d4SJens Axboe  * @out:	file to splice to
638932cc6d4SJens Axboe  * @ppos:	position in @out
639932cc6d4SJens Axboe  * @len:	how many bytes to splice
640932cc6d4SJens Axboe  * @flags:	splice modifier flags
641932cc6d4SJens Axboe  * @actor:	handler that splices the data
642932cc6d4SJens Axboe  *
643932cc6d4SJens Axboe  * Description:
6442933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
645932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
646932cc6d4SJens Axboe  *
647932cc6d4SJens Axboe  */
6486da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6496da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6506da61809SMark Fasheh 			 splice_actor *actor)
6516da61809SMark Fasheh {
6526da61809SMark Fasheh 	ssize_t ret;
653c66ab6faSJens Axboe 	struct splice_desc sd = {
654c66ab6faSJens Axboe 		.total_len = len,
655c66ab6faSJens Axboe 		.flags = flags,
656c66ab6faSJens Axboe 		.pos = *ppos,
6576a14b90bSJens Axboe 		.u.file = out,
658c66ab6faSJens Axboe 	};
6596da61809SMark Fasheh 
66061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
661c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
66261e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6636da61809SMark Fasheh 
6646da61809SMark Fasheh 	return ret;
6656da61809SMark Fasheh }
6666da61809SMark Fasheh 
6676da61809SMark Fasheh /**
6688d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6698d020765SAl Viro  * @pipe:	pipe info
6708d020765SAl Viro  * @out:	file to write to
6718d020765SAl Viro  * @ppos:	position in @out
6728d020765SAl Viro  * @len:	number of bytes to splice
6738d020765SAl Viro  * @flags:	splice modifier flags
6748d020765SAl Viro  *
6758d020765SAl Viro  * Description:
6768d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6778d020765SAl Viro  *    the given pipe inode to the given file.
6788d020765SAl Viro  *    This one is ->write_iter-based.
6798d020765SAl Viro  *
6808d020765SAl Viro  */
6818d020765SAl Viro ssize_t
6828d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6838d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
6848d020765SAl Viro {
6858d020765SAl Viro 	struct splice_desc sd = {
6868d020765SAl Viro 		.total_len = len,
6878d020765SAl Viro 		.flags = flags,
6888d020765SAl Viro 		.pos = *ppos,
6898d020765SAl Viro 		.u.file = out,
6908d020765SAl Viro 	};
6916718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
6928d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
6938d020765SAl Viro 					GFP_KERNEL);
6948d020765SAl Viro 	ssize_t ret;
6958d020765SAl Viro 
6968d020765SAl Viro 	if (unlikely(!array))
6978d020765SAl Viro 		return -ENOMEM;
6988d020765SAl Viro 
6998d020765SAl Viro 	pipe_lock(pipe);
7008d020765SAl Viro 
7018d020765SAl Viro 	splice_from_pipe_begin(&sd);
7028d020765SAl Viro 	while (sd.total_len) {
7038d020765SAl Viro 		struct iov_iter from;
704ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7058d020765SAl Viro 		size_t left;
7068cefc107SDavid Howells 		int n;
7078d020765SAl Viro 
7088d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7098d020765SAl Viro 		if (ret <= 0)
7108d020765SAl Viro 			break;
7118d020765SAl Viro 
7126718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7138d020765SAl Viro 			kfree(array);
7146718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7158d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7168d020765SAl Viro 					GFP_KERNEL);
7178d020765SAl Viro 			if (!array) {
7188d020765SAl Viro 				ret = -ENOMEM;
7198d020765SAl Viro 				break;
7208d020765SAl Viro 			}
7218d020765SAl Viro 		}
7228d020765SAl Viro 
723ec057595SLinus Torvalds 		head = pipe->head;
724ec057595SLinus Torvalds 		tail = pipe->tail;
725ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
726ec057595SLinus Torvalds 
7278d020765SAl Viro 		/* build the vector */
7288d020765SAl Viro 		left = sd.total_len;
7298cefc107SDavid Howells 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
7308cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7318d020765SAl Viro 			size_t this_len = buf->len;
7328d020765SAl Viro 
7338d020765SAl Viro 			if (this_len > left)
7348d020765SAl Viro 				this_len = left;
7358d020765SAl Viro 
736fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7378d020765SAl Viro 			if (unlikely(ret)) {
7388d020765SAl Viro 				if (ret == -ENODATA)
7398d020765SAl Viro 					ret = 0;
7408d020765SAl Viro 				goto done;
7418d020765SAl Viro 			}
7428d020765SAl Viro 
7438d020765SAl Viro 			array[n].bv_page = buf->page;
7448d020765SAl Viro 			array[n].bv_len = this_len;
7458d020765SAl Viro 			array[n].bv_offset = buf->offset;
7468d020765SAl Viro 			left -= this_len;
7478d020765SAl Viro 		}
7488d020765SAl Viro 
749aa563d7bSDavid Howells 		iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
750abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7518d020765SAl Viro 		if (ret <= 0)
7528d020765SAl Viro 			break;
7538d020765SAl Viro 
7548d020765SAl Viro 		sd.num_spliced += ret;
7558d020765SAl Viro 		sd.total_len -= ret;
756dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7578d020765SAl Viro 
7588d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7598cefc107SDavid Howells 		tail = pipe->tail;
7608d020765SAl Viro 		while (ret) {
7618cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7628d020765SAl Viro 			if (ret >= buf->len) {
7638d020765SAl Viro 				ret -= buf->len;
7648d020765SAl Viro 				buf->len = 0;
765a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7668cefc107SDavid Howells 				tail++;
7678cefc107SDavid Howells 				pipe->tail = tail;
7688d020765SAl Viro 				if (pipe->files)
7698d020765SAl Viro 					sd.need_wakeup = true;
7708d020765SAl Viro 			} else {
7718d020765SAl Viro 				buf->offset += ret;
7728d020765SAl Viro 				buf->len -= ret;
7738d020765SAl Viro 				ret = 0;
7748d020765SAl Viro 			}
7758d020765SAl Viro 		}
7768d020765SAl Viro 	}
7778d020765SAl Viro done:
7788d020765SAl Viro 	kfree(array);
7798d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7808d020765SAl Viro 
7818d020765SAl Viro 	pipe_unlock(pipe);
7828d020765SAl Viro 
7838d020765SAl Viro 	if (sd.num_spliced)
7848d020765SAl Viro 		ret = sd.num_spliced;
7858d020765SAl Viro 
7868d020765SAl Viro 	return ret;
7878d020765SAl Viro }
7888d020765SAl Viro 
7898d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
7908d020765SAl Viro 
791b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
792b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
7930b0a47f5SMiklos Szeredi {
794b2858d7dSMiklos Szeredi 	int ret;
795b2858d7dSMiklos Szeredi 	void *data;
79606ae43f3SAl Viro 	loff_t tmp = sd->pos;
797b2858d7dSMiklos Szeredi 
798fbb32750SAl Viro 	data = kmap(buf->page);
79906ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
800fbb32750SAl Viro 	kunmap(buf->page);
801b2858d7dSMiklos Szeredi 
802b2858d7dSMiklos Szeredi 	return ret;
8030b0a47f5SMiklos Szeredi }
8040b0a47f5SMiklos Szeredi 
8050b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
8060b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
8070b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
8080b0a47f5SMiklos Szeredi {
809b2858d7dSMiklos Szeredi 	ssize_t ret;
8100b0a47f5SMiklos Szeredi 
811b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
812b2858d7dSMiklos Szeredi 	if (ret > 0)
813b2858d7dSMiklos Szeredi 		*ppos += ret;
8140b0a47f5SMiklos Szeredi 
815b2858d7dSMiklos Szeredi 	return ret;
8160b0a47f5SMiklos Szeredi }
8170b0a47f5SMiklos Szeredi 
81883f9135bSJens Axboe /**
81983f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
820932cc6d4SJens Axboe  * @pipe:	pipe to splice from
82183f9135bSJens Axboe  * @out:	socket to write to
822932cc6d4SJens Axboe  * @ppos:	position in @out
82383f9135bSJens Axboe  * @len:	number of bytes to splice
82483f9135bSJens Axboe  * @flags:	splice modifier flags
82583f9135bSJens Axboe  *
826932cc6d4SJens Axboe  * Description:
82783f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
82883f9135bSJens Axboe  *    is involved.
82983f9135bSJens Axboe  *
83083f9135bSJens Axboe  */
8313a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
832cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8335274f052SJens Axboe {
83400522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8355274f052SJens Axboe }
8365274f052SJens Axboe 
837059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
838a0f06780SJeff Garzik 
83983f9135bSJens Axboe /*
84083f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
84183f9135bSJens Axboe  */
8423a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
843cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8445274f052SJens Axboe {
84572c2d531SAl Viro 	if (out->f_op->splice_write)
84600c285d0SChristoph Hellwig 		return out->f_op->splice_write(pipe, out, ppos, len, flags);
84700c285d0SChristoph Hellwig 	return default_file_splice_write(pipe, out, ppos, len, flags);
8485274f052SJens Axboe }
8495274f052SJens Axboe 
85083f9135bSJens Axboe /*
85183f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
85283f9135bSJens Axboe  */
853cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
854cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
855cbb7e577SJens Axboe 			 unsigned int flags)
8565274f052SJens Axboe {
8575274f052SJens Axboe 	int ret;
8585274f052SJens Axboe 
85949570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8605274f052SJens Axboe 		return -EBADF;
8615274f052SJens Axboe 
862cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8635274f052SJens Axboe 	if (unlikely(ret < 0))
8645274f052SJens Axboe 		return ret;
8655274f052SJens Axboe 
86603cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
86703cc0789SAl Viro 		len = MAX_RW_COUNT;
86803cc0789SAl Viro 
86972c2d531SAl Viro 	if (in->f_op->splice_read)
8702bc01060SChristoph Hellwig 		return in->f_op->splice_read(in, ppos, pipe, len, flags);
8712bc01060SChristoph Hellwig 	return default_file_splice_read(in, ppos, pipe, len, flags);
8725274f052SJens Axboe }
8735274f052SJens Axboe 
874932cc6d4SJens Axboe /**
875932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
876932cc6d4SJens Axboe  * @in:		file to splice from
877932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
878932cc6d4SJens Axboe  * @actor:	handles the data splicing
879932cc6d4SJens Axboe  *
880932cc6d4SJens Axboe  * Description:
881932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
882932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
883932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
884932cc6d4SJens Axboe  *    that process.
885932cc6d4SJens Axboe  *
886c66ab6faSJens Axboe  */
887c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
888c66ab6faSJens Axboe 			       splice_direct_actor *actor)
889b92ce558SJens Axboe {
890b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
891b92ce558SJens Axboe 	long ret, bytes;
892b92ce558SJens Axboe 	umode_t i_mode;
893c66ab6faSJens Axboe 	size_t len;
8940ff28d9fSChristophe Leroy 	int i, flags, more;
895b92ce558SJens Axboe 
896b92ce558SJens Axboe 	/*
897b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
898b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
899b92ce558SJens Axboe 	 * piped splicing for that!
900b92ce558SJens Axboe 	 */
901496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
902b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
903b92ce558SJens Axboe 		return -EINVAL;
904b92ce558SJens Axboe 
905b92ce558SJens Axboe 	/*
906b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
907b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
908b92ce558SJens Axboe 	 */
909b92ce558SJens Axboe 	pipe = current->splice_pipe;
91049570e9bSJens Axboe 	if (unlikely(!pipe)) {
9117bee130eSAl Viro 		pipe = alloc_pipe_info();
912b92ce558SJens Axboe 		if (!pipe)
913b92ce558SJens Axboe 			return -ENOMEM;
914b92ce558SJens Axboe 
915b92ce558SJens Axboe 		/*
916b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
91700522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
918b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
919b92ce558SJens Axboe 		 */
920b92ce558SJens Axboe 		pipe->readers = 1;
921b92ce558SJens Axboe 
922b92ce558SJens Axboe 		current->splice_pipe = pipe;
923b92ce558SJens Axboe 	}
924b92ce558SJens Axboe 
925b92ce558SJens Axboe 	/*
92673d62d83SIngo Molnar 	 * Do the splice.
927b92ce558SJens Axboe 	 */
928b92ce558SJens Axboe 	ret = 0;
929b92ce558SJens Axboe 	bytes = 0;
930c66ab6faSJens Axboe 	len = sd->total_len;
931c66ab6faSJens Axboe 	flags = sd->flags;
932c66ab6faSJens Axboe 
933c66ab6faSJens Axboe 	/*
934c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
935c66ab6faSJens Axboe 	 */
936c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9370ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
938b92ce558SJens Axboe 
9398cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
94017614445SDarrick J. Wong 
941b92ce558SJens Axboe 	while (len) {
9428cefc107SDavid Howells 		unsigned int p_space;
94351a92c0fSJens Axboe 		size_t read_len;
944a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
945b92ce558SJens Axboe 
94617614445SDarrick J. Wong 		/* Don't try to read more the pipe has space for. */
9476718b6f8SDavid Howells 		p_space = pipe->max_usage -
9488cefc107SDavid Howells 			pipe_occupancy(pipe->head, pipe->tail);
9498cefc107SDavid Howells 		read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
95017614445SDarrick J. Wong 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
95151a92c0fSJens Axboe 		if (unlikely(ret <= 0))
952b92ce558SJens Axboe 			goto out_release;
953b92ce558SJens Axboe 
954b92ce558SJens Axboe 		read_len = ret;
955c66ab6faSJens Axboe 		sd->total_len = read_len;
956b92ce558SJens Axboe 
957b92ce558SJens Axboe 		/*
9580ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9590ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9600ff28d9fSChristophe Leroy 		 * initially, clears it.
9610ff28d9fSChristophe Leroy 		 */
9620ff28d9fSChristophe Leroy 		if (read_len < len)
9630ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9640ff28d9fSChristophe Leroy 		else if (!more)
9650ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9660ff28d9fSChristophe Leroy 		/*
967b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
968b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
969b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
970b92ce558SJens Axboe 		 */
971c66ab6faSJens Axboe 		ret = actor(pipe, sd);
972a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
973a82c53a0STom Zanussi 			sd->pos = prev_pos;
974b92ce558SJens Axboe 			goto out_release;
975a82c53a0STom Zanussi 		}
976b92ce558SJens Axboe 
977b92ce558SJens Axboe 		bytes += ret;
978b92ce558SJens Axboe 		len -= ret;
979bcd4f3acSJens Axboe 		sd->pos = pos;
980b92ce558SJens Axboe 
981a82c53a0STom Zanussi 		if (ret < read_len) {
982a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
98351a92c0fSJens Axboe 			goto out_release;
984b92ce558SJens Axboe 		}
985a82c53a0STom Zanussi 	}
986b92ce558SJens Axboe 
9879e97198dSJens Axboe done:
9888cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
9899e97198dSJens Axboe 	file_accessed(in);
990b92ce558SJens Axboe 	return bytes;
991b92ce558SJens Axboe 
992b92ce558SJens Axboe out_release:
993b92ce558SJens Axboe 	/*
994b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
995b92ce558SJens Axboe 	 * the pipe buffers in question:
996b92ce558SJens Axboe 	 */
9978cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
9988cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
999b92ce558SJens Axboe 
1000a779638cSMiklos Szeredi 		if (buf->ops)
1001a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1002b92ce558SJens Axboe 	}
1003b92ce558SJens Axboe 
10049e97198dSJens Axboe 	if (!bytes)
10059e97198dSJens Axboe 		bytes = ret;
1006b92ce558SJens Axboe 
10079e97198dSJens Axboe 	goto done;
1008c66ab6faSJens Axboe }
1009c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1010c66ab6faSJens Axboe 
1011c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1012c66ab6faSJens Axboe 			       struct splice_desc *sd)
1013c66ab6faSJens Axboe {
10146a14b90bSJens Axboe 	struct file *file = sd->u.file;
1015c66ab6faSJens Axboe 
10167995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10172cb4b05eSChangli Gao 			      sd->flags);
1018c66ab6faSJens Axboe }
1019c66ab6faSJens Axboe 
1020932cc6d4SJens Axboe /**
1021932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1022932cc6d4SJens Axboe  * @in:		file to splice from
1023932cc6d4SJens Axboe  * @ppos:	input file offset
1024932cc6d4SJens Axboe  * @out:	file to splice to
1025acdb37c3SRandy Dunlap  * @opos:	output file offset
1026932cc6d4SJens Axboe  * @len:	number of bytes to splice
1027932cc6d4SJens Axboe  * @flags:	splice modifier flags
1028932cc6d4SJens Axboe  *
1029932cc6d4SJens Axboe  * Description:
1030932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1031932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1032932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1033932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1034932cc6d4SJens Axboe  *
1035932cc6d4SJens Axboe  */
1036c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10377995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1038c66ab6faSJens Axboe {
1039c66ab6faSJens Axboe 	struct splice_desc sd = {
1040c66ab6faSJens Axboe 		.len		= len,
1041c66ab6faSJens Axboe 		.total_len	= len,
1042c66ab6faSJens Axboe 		.flags		= flags,
1043c66ab6faSJens Axboe 		.pos		= *ppos,
10446a14b90bSJens Axboe 		.u.file		= out,
10457995bd28SAl Viro 		.opos		= opos,
1046c66ab6faSJens Axboe 	};
104751a92c0fSJens Axboe 	long ret;
1048c66ab6faSJens Axboe 
104918c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
105018c67cb9SAl Viro 		return -EBADF;
105118c67cb9SAl Viro 
105218c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
105318c67cb9SAl Viro 		return -EINVAL;
105418c67cb9SAl Viro 
105518c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
105618c67cb9SAl Viro 	if (unlikely(ret < 0))
105718c67cb9SAl Viro 		return ret;
105818c67cb9SAl Viro 
1059c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
106051a92c0fSJens Axboe 	if (ret > 0)
1061a82c53a0STom Zanussi 		*ppos = sd.pos;
106251a92c0fSJens Axboe 
1063c66ab6faSJens Axboe 	return ret;
1064b92ce558SJens Axboe }
10651c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1066b92ce558SJens Axboe 
10678924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10688924feffSAl Viro {
106952bce911SLinus Torvalds 	for (;;) {
107052bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
107152bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
107252bce911SLinus Torvalds 			return -EPIPE;
107352bce911SLinus Torvalds 		}
10746718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
107552bce911SLinus Torvalds 			return 0;
10768924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10778924feffSAl Viro 			return -EAGAIN;
10788924feffSAl Viro 		if (signal_pending(current))
10798924feffSAl Viro 			return -ERESTARTSYS;
10808924feffSAl Viro 		pipe_wait(pipe);
10818924feffSAl Viro 	}
10828924feffSAl Viro }
10838924feffSAl Viro 
10847c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
10857c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
10867c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1087ddac0d39SJens Axboe 
1088ddac0d39SJens Axboe /*
108983f9135bSJens Axboe  * Determine where to splice to/from.
109083f9135bSJens Axboe  */
1091444ebb57SPavel Begunkov long do_splice(struct file *in, loff_t __user *off_in,
1092529565dcSIngo Molnar 		struct file *out, loff_t __user *off_out,
1093529565dcSIngo Molnar 		size_t len, unsigned int flags)
10945274f052SJens Axboe {
10957c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
10967c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
10977995bd28SAl Viro 	loff_t offset;
1098a4514ebdSJens Axboe 	long ret;
10995274f052SJens Axboe 
110071993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
110171993e62SLinus Torvalds 	opipe = get_pipe_info(out);
11027c77f0b3SMiklos Szeredi 
11037c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11047c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11057c77f0b3SMiklos Szeredi 			return -ESPIPE;
11067c77f0b3SMiklos Szeredi 
11077c77f0b3SMiklos Szeredi 		if (!(in->f_mode & FMODE_READ))
11087c77f0b3SMiklos Szeredi 			return -EBADF;
11097c77f0b3SMiklos Szeredi 
11107c77f0b3SMiklos Szeredi 		if (!(out->f_mode & FMODE_WRITE))
11117c77f0b3SMiklos Szeredi 			return -EBADF;
11127c77f0b3SMiklos Szeredi 
11137c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11147c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11157c77f0b3SMiklos Szeredi 			return -EINVAL;
11167c77f0b3SMiklos Szeredi 
1117ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1118ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1119ee5e0011SSlavomir Kaslev 
11207c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11217c77f0b3SMiklos Szeredi 	}
11227c77f0b3SMiklos Szeredi 
11237c77f0b3SMiklos Szeredi 	if (ipipe) {
1124529565dcSIngo Molnar 		if (off_in)
1125529565dcSIngo Molnar 			return -ESPIPE;
1126b92ce558SJens Axboe 		if (off_out) {
112719c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1128b92ce558SJens Axboe 				return -EINVAL;
1129cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1130b92ce558SJens Axboe 				return -EFAULT;
11317995bd28SAl Viro 		} else {
11327995bd28SAl Viro 			offset = out->f_pos;
11337995bd28SAl Viro 		}
1134529565dcSIngo Molnar 
113518c67cb9SAl Viro 		if (unlikely(!(out->f_mode & FMODE_WRITE)))
113618c67cb9SAl Viro 			return -EBADF;
113718c67cb9SAl Viro 
113818c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
113918c67cb9SAl Viro 			return -EINVAL;
114018c67cb9SAl Viro 
114118c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
114218c67cb9SAl Viro 		if (unlikely(ret < 0))
114318c67cb9SAl Viro 			return ret;
114418c67cb9SAl Viro 
1145ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1146ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1147ee5e0011SSlavomir Kaslev 
1148500368f7SAl Viro 		file_start_write(out);
11497995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1150500368f7SAl Viro 		file_end_write(out);
1151a4514ebdSJens Axboe 
11527995bd28SAl Viro 		if (!off_out)
11537995bd28SAl Viro 			out->f_pos = offset;
11547995bd28SAl Viro 		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1155a4514ebdSJens Axboe 			ret = -EFAULT;
1156a4514ebdSJens Axboe 
1157a4514ebdSJens Axboe 		return ret;
1158529565dcSIngo Molnar 	}
11595274f052SJens Axboe 
11607c77f0b3SMiklos Szeredi 	if (opipe) {
1161529565dcSIngo Molnar 		if (off_out)
1162529565dcSIngo Molnar 			return -ESPIPE;
1163b92ce558SJens Axboe 		if (off_in) {
116419c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1165b92ce558SJens Axboe 				return -EINVAL;
1166cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1167b92ce558SJens Axboe 				return -EFAULT;
11687995bd28SAl Viro 		} else {
11697995bd28SAl Viro 			offset = in->f_pos;
11707995bd28SAl Viro 		}
1171529565dcSIngo Molnar 
1172ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1173ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1174ee5e0011SSlavomir Kaslev 
11758924feffSAl Viro 		pipe_lock(opipe);
11768924feffSAl Viro 		ret = wait_for_space(opipe, flags);
11773253d9d0SDarrick J. Wong 		if (!ret) {
11786a965666SLinus Torvalds 			unsigned int p_space;
11793253d9d0SDarrick J. Wong 
11803253d9d0SDarrick J. Wong 			/* Don't try to read more the pipe has space for. */
11816a965666SLinus Torvalds 			p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail);
11826a965666SLinus Torvalds 			len = min_t(size_t, len, p_space << PAGE_SHIFT);
11833253d9d0SDarrick J. Wong 
11847995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
11853253d9d0SDarrick J. Wong 		}
11868924feffSAl Viro 		pipe_unlock(opipe);
11878924feffSAl Viro 		if (ret > 0)
11888924feffSAl Viro 			wakeup_pipe_readers(opipe);
11897995bd28SAl Viro 		if (!off_in)
11907995bd28SAl Viro 			in->f_pos = offset;
11917995bd28SAl Viro 		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1192a4514ebdSJens Axboe 			ret = -EFAULT;
1193a4514ebdSJens Axboe 
1194a4514ebdSJens Axboe 		return ret;
1195529565dcSIngo Molnar 	}
11965274f052SJens Axboe 
11975274f052SJens Axboe 	return -EINVAL;
11985274f052SJens Axboe }
11995274f052SJens Axboe 
120079fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
120179fddc4eSAl Viro 			struct pipe_inode_info *pipe,
120279fddc4eSAl Viro 			unsigned flags)
1203912d35f8SJens Axboe {
120479fddc4eSAl Viro 	struct pipe_buffer buf = {
120579fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
120679fddc4eSAl Viro 		.flags = flags
120779fddc4eSAl Viro 	};
120879fddc4eSAl Viro 	size_t total = 0;
120979fddc4eSAl Viro 	int ret = 0;
121079fddc4eSAl Viro 	bool failed = false;
121179fddc4eSAl Viro 
121279fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
121379fddc4eSAl Viro 		struct page *pages[16];
1214db85a9ebSAl Viro 		ssize_t copied;
1215db85a9ebSAl Viro 		size_t start;
121679fddc4eSAl Viro 		int n;
1217912d35f8SJens Axboe 
121879fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
121979fddc4eSAl Viro 		if (copied <= 0) {
122079fddc4eSAl Viro 			ret = copied;
122179fddc4eSAl Viro 			break;
122279fddc4eSAl Viro 		}
1223912d35f8SJens Axboe 
122479fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1225db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
122679fddc4eSAl Viro 			if (!failed) {
122779fddc4eSAl Viro 				buf.page = pages[n];
122879fddc4eSAl Viro 				buf.offset = start;
122979fddc4eSAl Viro 				buf.len = size;
123079fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
123179fddc4eSAl Viro 				if (unlikely(ret < 0)) {
123279fddc4eSAl Viro 					failed = true;
123379fddc4eSAl Viro 				} else {
123479fddc4eSAl Viro 					iov_iter_advance(from, ret);
123579fddc4eSAl Viro 					total += ret;
123679fddc4eSAl Viro 				}
123779fddc4eSAl Viro 			} else {
123879fddc4eSAl Viro 				put_page(pages[n]);
123979fddc4eSAl Viro 			}
1240db85a9ebSAl Viro 			copied -= size;
1241912d35f8SJens Axboe 		}
1242912d35f8SJens Axboe 	}
124379fddc4eSAl Viro 	return total ? total : ret;
1244912d35f8SJens Axboe }
1245912d35f8SJens Axboe 
12466a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12476a14b90bSJens Axboe 			struct splice_desc *sd)
12486a14b90bSJens Axboe {
12496130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
12506130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
12516a14b90bSJens Axboe }
12526a14b90bSJens Axboe 
12536a14b90bSJens Axboe /*
12546a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
12556a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
12566a14b90bSJens Axboe  */
125787a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
125887a3002aSAl Viro 			     unsigned int flags)
12596a14b90bSJens Axboe {
126087a3002aSAl Viro 	struct pipe_inode_info *pipe = get_pipe_info(file);
126187a3002aSAl Viro 	struct splice_desc sd = {
126287a3002aSAl Viro 		.total_len = iov_iter_count(iter),
126387a3002aSAl Viro 		.flags = flags,
126487a3002aSAl Viro 		.u.data = iter
126587a3002aSAl Viro 	};
126687a3002aSAl Viro 	long ret = 0;
12676a14b90bSJens Axboe 
12686a14b90bSJens Axboe 	if (!pipe)
12696a14b90bSJens Axboe 		return -EBADF;
12706a14b90bSJens Axboe 
1271345995faSAl Viro 	if (sd.total_len) {
12726130f531SAl Viro 		pipe_lock(pipe);
12736130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
127461e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1275345995faSAl Viro 	}
12766a14b90bSJens Axboe 
12776a14b90bSJens Axboe 	return ret;
12786a14b90bSJens Axboe }
12796a14b90bSJens Axboe 
1280912d35f8SJens Axboe /*
1281912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1282912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1283912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1284912d35f8SJens Axboe  */
128587a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
128687a3002aSAl Viro 			     unsigned int flags)
1287912d35f8SJens Axboe {
1288ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
128987a3002aSAl Viro 	long ret = 0;
129079fddc4eSAl Viro 	unsigned buf_flag = 0;
129179fddc4eSAl Viro 
129279fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
129379fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1294912d35f8SJens Axboe 
129571993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1296ddac0d39SJens Axboe 	if (!pipe)
1297912d35f8SJens Axboe 		return -EBADF;
1298912d35f8SJens Axboe 
12998924feffSAl Viro 	pipe_lock(pipe);
13008924feffSAl Viro 	ret = wait_for_space(pipe, flags);
130179fddc4eSAl Viro 	if (!ret)
130287a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13038924feffSAl Viro 	pipe_unlock(pipe);
13048924feffSAl Viro 	if (ret > 0)
13058924feffSAl Viro 		wakeup_pipe_readers(pipe);
130635f3d14dSJens Axboe 	return ret;
1307912d35f8SJens Axboe }
1308912d35f8SJens Axboe 
130987a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
131087a3002aSAl Viro {
131187a3002aSAl Viro 	if (!f.file)
131287a3002aSAl Viro 		return -EBADF;
131387a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
131487a3002aSAl Viro 		*type = WRITE;
131587a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
131687a3002aSAl Viro 		*type = READ;
131787a3002aSAl Viro 	} else {
131887a3002aSAl Viro 		fdput(f);
131987a3002aSAl Viro 		return -EBADF;
132087a3002aSAl Viro 	}
132187a3002aSAl Viro 	return 0;
132287a3002aSAl Viro }
132387a3002aSAl Viro 
13246a14b90bSJens Axboe /*
13256a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13266a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13276a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13286a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13296a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13306a14b90bSJens Axboe  * solutions for that:
13316a14b90bSJens Axboe  *
13326a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13336a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13346a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13356a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13366a14b90bSJens Axboe  *
13376a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13386a14b90bSJens Axboe  *
13396a14b90bSJens Axboe  */
134087a3002aSAl Viro static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
1341912d35f8SJens Axboe {
13423d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
13433d6ea290SAl Viro 		return -EINVAL;
134487a3002aSAl Viro 
134587a3002aSAl Viro 	if (!iov_iter_count(iter))
13466a14b90bSJens Axboe 		return 0;
13476a14b90bSJens Axboe 
134887a3002aSAl Viro 	if (iov_iter_rw(iter) == WRITE)
134987a3002aSAl Viro 		return vmsplice_to_pipe(f, iter, flags);
135087a3002aSAl Viro 	else
135187a3002aSAl Viro 		return vmsplice_to_user(f, iter, flags);
1352912d35f8SJens Axboe }
1353912d35f8SJens Axboe 
135487a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
135530cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
135630cfe4efSDominik Brodowski {
135787a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
135887a3002aSAl Viro 	struct iovec *iov = iovstack;
135987a3002aSAl Viro 	struct iov_iter iter;
136087e5e6daSJens Axboe 	ssize_t error;
136187a3002aSAl Viro 	struct fd f;
136287a3002aSAl Viro 	int type;
136387a3002aSAl Viro 
136487a3002aSAl Viro 	f = fdget(fd);
136587a3002aSAl Viro 	error = vmsplice_type(f, &type);
136687a3002aSAl Viro 	if (error)
136787a3002aSAl Viro 		return error;
136887a3002aSAl Viro 
136987a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
137087a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
137187e5e6daSJens Axboe 	if (error >= 0) {
137287a3002aSAl Viro 		error = do_vmsplice(f.file, &iter, flags);
137387a3002aSAl Viro 		kfree(iov);
137487a3002aSAl Viro 	}
137587a3002aSAl Viro 	fdput(f);
137687a3002aSAl Viro 	return error;
137730cfe4efSDominik Brodowski }
137830cfe4efSDominik Brodowski 
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 {
138387a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
138487a3002aSAl Viro 	struct iovec *iov = iovstack;
138587a3002aSAl Viro 	struct iov_iter iter;
138687e5e6daSJens Axboe 	ssize_t error;
138787a3002aSAl Viro 	struct fd f;
138887a3002aSAl Viro 	int type;
138987a3002aSAl Viro 
139087a3002aSAl Viro 	f = fdget(fd);
139187a3002aSAl Viro 	error = vmsplice_type(f, &type);
139287a3002aSAl Viro 	if (error)
139387a3002aSAl Viro 		return error;
139487a3002aSAl Viro 
139587a3002aSAl Viro 	error = compat_import_iovec(type, iov32, nr_segs,
139687a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
139787e5e6daSJens Axboe 	if (error >= 0) {
139887a3002aSAl Viro 		error = do_vmsplice(f.file, &iter, flags);
139987a3002aSAl Viro 		kfree(iov);
140076b021d0SAl Viro 	}
140187a3002aSAl Viro 	fdput(f);
140287a3002aSAl Viro 	return error;
140376b021d0SAl Viro }
140476b021d0SAl Viro #endif
140576b021d0SAl Viro 
1406836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1407836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1408836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14095274f052SJens Axboe {
14102903ff01SAl Viro 	struct fd in, out;
14115274f052SJens Axboe 	long error;
14125274f052SJens Axboe 
14135274f052SJens Axboe 	if (unlikely(!len))
14145274f052SJens Axboe 		return 0;
14155274f052SJens Axboe 
14163d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14173d6ea290SAl Viro 		return -EINVAL;
14183d6ea290SAl Viro 
14195274f052SJens Axboe 	error = -EBADF;
14202903ff01SAl Viro 	in = fdget(fd_in);
14212903ff01SAl Viro 	if (in.file) {
14222903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
14232903ff01SAl Viro 			out = fdget(fd_out);
14242903ff01SAl Viro 			if (out.file) {
14252903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
14262903ff01SAl Viro 					error = do_splice(in.file, off_in,
14272903ff01SAl Viro 							  out.file, off_out,
1428529565dcSIngo Molnar 							  len, flags);
14292903ff01SAl Viro 				fdput(out);
14305274f052SJens Axboe 			}
14315274f052SJens Axboe 		}
14322903ff01SAl Viro 		fdput(in);
14335274f052SJens Axboe 	}
14345274f052SJens Axboe 	return error;
14355274f052SJens Axboe }
143670524490SJens Axboe 
143770524490SJens Axboe /*
1438aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1439aadd06e5SJens Axboe  * return an appropriate error.
1440aadd06e5SJens Axboe  */
14417c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1442aadd06e5SJens Axboe {
1443aadd06e5SJens Axboe 	int ret;
1444aadd06e5SJens Axboe 
1445aadd06e5SJens Axboe 	/*
14468cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1447aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1448aadd06e5SJens Axboe 	 */
14498cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1450aadd06e5SJens Axboe 		return 0;
1451aadd06e5SJens Axboe 
1452aadd06e5SJens Axboe 	ret = 0;
145361e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1454aadd06e5SJens Axboe 
14558cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1456aadd06e5SJens Axboe 		if (signal_pending(current)) {
1457aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1458aadd06e5SJens Axboe 			break;
1459aadd06e5SJens Axboe 		}
1460aadd06e5SJens Axboe 		if (!pipe->writers)
1461aadd06e5SJens Axboe 			break;
1462aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1463aadd06e5SJens Axboe 			ret = -EAGAIN;
1464aadd06e5SJens Axboe 			break;
1465aadd06e5SJens Axboe 		}
1466aadd06e5SJens Axboe 		pipe_wait(pipe);
1467aadd06e5SJens Axboe 	}
1468aadd06e5SJens Axboe 
146961e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1470aadd06e5SJens Axboe 	return ret;
1471aadd06e5SJens Axboe }
1472aadd06e5SJens Axboe 
1473aadd06e5SJens Axboe /*
1474aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1475aadd06e5SJens Axboe  * return an appropriate error.
1476aadd06e5SJens Axboe  */
14777c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1478aadd06e5SJens Axboe {
1479aadd06e5SJens Axboe 	int ret;
1480aadd06e5SJens Axboe 
1481aadd06e5SJens Axboe 	/*
14828cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1483aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1484aadd06e5SJens Axboe 	 */
14856718b6f8SDavid Howells 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1486aadd06e5SJens Axboe 		return 0;
1487aadd06e5SJens Axboe 
1488aadd06e5SJens Axboe 	ret = 0;
148961e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1490aadd06e5SJens Axboe 
14916718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1492aadd06e5SJens Axboe 		if (!pipe->readers) {
1493aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1494aadd06e5SJens Axboe 			ret = -EPIPE;
1495aadd06e5SJens Axboe 			break;
1496aadd06e5SJens Axboe 		}
1497aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1498aadd06e5SJens Axboe 			ret = -EAGAIN;
1499aadd06e5SJens Axboe 			break;
1500aadd06e5SJens Axboe 		}
1501aadd06e5SJens Axboe 		if (signal_pending(current)) {
1502aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1503aadd06e5SJens Axboe 			break;
1504aadd06e5SJens Axboe 		}
1505aadd06e5SJens Axboe 		pipe_wait(pipe);
1506aadd06e5SJens Axboe 	}
1507aadd06e5SJens Axboe 
150861e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1509aadd06e5SJens Axboe 	return ret;
1510aadd06e5SJens Axboe }
1511aadd06e5SJens Axboe 
1512aadd06e5SJens Axboe /*
15137c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15147c77f0b3SMiklos Szeredi  */
15157c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15167c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15177c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15187c77f0b3SMiklos Szeredi {
15197c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15208cefc107SDavid Howells 	unsigned int i_head, o_head;
15218cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15228cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15238cefc107SDavid Howells 	int ret = 0;
15247c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15257c77f0b3SMiklos Szeredi 
15267c77f0b3SMiklos Szeredi 
15277c77f0b3SMiklos Szeredi retry:
15287c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15297c77f0b3SMiklos Szeredi 	if (ret)
15307c77f0b3SMiklos Szeredi 		return ret;
15317c77f0b3SMiklos Szeredi 
15327c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15337c77f0b3SMiklos Szeredi 	if (ret)
15347c77f0b3SMiklos Szeredi 		return ret;
15357c77f0b3SMiklos Szeredi 
15367c77f0b3SMiklos Szeredi 	/*
15377c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15387c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15397c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15407c77f0b3SMiklos Szeredi 	 */
15417c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15427c77f0b3SMiklos Szeredi 
15438cefc107SDavid Howells 	i_tail = ipipe->tail;
15448cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15458cefc107SDavid Howells 	o_head = opipe->head;
15468cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15478cefc107SDavid Howells 
15487c77f0b3SMiklos Szeredi 	do {
15498cefc107SDavid Howells 		size_t o_len;
15508cefc107SDavid Howells 
15517c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15527c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15537c77f0b3SMiklos Szeredi 			if (!ret)
15547c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15557c77f0b3SMiklos Szeredi 			break;
15567c77f0b3SMiklos Szeredi 		}
15577c77f0b3SMiklos Szeredi 
15588cefc107SDavid Howells 		i_head = ipipe->head;
15598cefc107SDavid Howells 		o_tail = opipe->tail;
15608cefc107SDavid Howells 
15618cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
15627c77f0b3SMiklos Szeredi 			break;
15637c77f0b3SMiklos Szeredi 
15647c77f0b3SMiklos Szeredi 		/*
15657c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15667c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15677c77f0b3SMiklos Szeredi 		 */
15688cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
15696718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
15707c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15717c77f0b3SMiklos Szeredi 			if (ret)
15727c77f0b3SMiklos Szeredi 				break;
15737c77f0b3SMiklos Szeredi 
15747c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15757c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15767c77f0b3SMiklos Szeredi 				break;
15777c77f0b3SMiklos Szeredi 			}
15787c77f0b3SMiklos Szeredi 
15797c77f0b3SMiklos Szeredi 			/*
15807c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15817c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15827c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
15837c77f0b3SMiklos Szeredi 			 */
15847c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
15857c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
15867c77f0b3SMiklos Szeredi 			goto retry;
15877c77f0b3SMiklos Szeredi 		}
15887c77f0b3SMiklos Szeredi 
15898cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
15908cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
15917c77f0b3SMiklos Szeredi 
15927c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
15937c77f0b3SMiklos Szeredi 			/*
15947c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
15957c77f0b3SMiklos Szeredi 			 */
15967c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15977c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
15988cefc107SDavid Howells 			i_tail++;
15998cefc107SDavid Howells 			ipipe->tail = i_tail;
16007c77f0b3SMiklos Szeredi 			input_wakeup = true;
16018cefc107SDavid Howells 			o_len = obuf->len;
16028cefc107SDavid Howells 			o_head++;
16038cefc107SDavid Howells 			opipe->head = o_head;
16047c77f0b3SMiklos Szeredi 		} else {
16057c77f0b3SMiklos Szeredi 			/*
16067c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16077c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16087c77f0b3SMiklos Szeredi 			 */
160915fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
161015fab63eSMatthew Wilcox 				if (ret == 0)
161115fab63eSMatthew Wilcox 					ret = -EFAULT;
161215fab63eSMatthew Wilcox 				break;
161315fab63eSMatthew Wilcox 			}
16147c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16157c77f0b3SMiklos Szeredi 
16167c77f0b3SMiklos Szeredi 			/*
1617f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
16187c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16197c77f0b3SMiklos Szeredi 			 */
16207c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1621f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1622a0ce2f0aSJann Horn 
16237c77f0b3SMiklos Szeredi 			obuf->len = len;
16248cefc107SDavid Howells 			ibuf->offset += len;
16258cefc107SDavid Howells 			ibuf->len -= len;
16268cefc107SDavid Howells 			o_len = len;
16278cefc107SDavid Howells 			o_head++;
16288cefc107SDavid Howells 			opipe->head = o_head;
16297c77f0b3SMiklos Szeredi 		}
16308cefc107SDavid Howells 		ret += o_len;
16318cefc107SDavid Howells 		len -= o_len;
16327c77f0b3SMiklos Szeredi 	} while (len);
16337c77f0b3SMiklos Szeredi 
16347c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16357c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16367c77f0b3SMiklos Szeredi 
16377c77f0b3SMiklos Szeredi 	/*
16387c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16397c77f0b3SMiklos Szeredi 	 */
1640825cdcb1SNamhyung Kim 	if (ret > 0)
1641825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1642825cdcb1SNamhyung Kim 
16437c77f0b3SMiklos Szeredi 	if (input_wakeup)
16447c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16457c77f0b3SMiklos Szeredi 
16467c77f0b3SMiklos Szeredi 	return ret;
16477c77f0b3SMiklos Szeredi }
16487c77f0b3SMiklos Szeredi 
16497c77f0b3SMiklos Szeredi /*
165070524490SJens Axboe  * Link contents of ipipe to opipe.
165170524490SJens Axboe  */
165270524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
165370524490SJens Axboe 		     struct pipe_inode_info *opipe,
165470524490SJens Axboe 		     size_t len, unsigned int flags)
165570524490SJens Axboe {
165670524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
16578cefc107SDavid Howells 	unsigned int i_head, o_head;
16588cefc107SDavid Howells 	unsigned int i_tail, o_tail;
16598cefc107SDavid Howells 	unsigned int i_mask, o_mask;
16608cefc107SDavid Howells 	int ret = 0;
166170524490SJens Axboe 
166270524490SJens Axboe 	/*
166370524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
166461e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
166570524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
166670524490SJens Axboe 	 */
166761e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
166870524490SJens Axboe 
16698cefc107SDavid Howells 	i_tail = ipipe->tail;
16708cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
16718cefc107SDavid Howells 	o_head = opipe->head;
16728cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
16738cefc107SDavid Howells 
1674aadd06e5SJens Axboe 	do {
167570524490SJens Axboe 		if (!opipe->readers) {
167670524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
167770524490SJens Axboe 			if (!ret)
167870524490SJens Axboe 				ret = -EPIPE;
167970524490SJens Axboe 			break;
168070524490SJens Axboe 		}
168170524490SJens Axboe 
16828cefc107SDavid Howells 		i_head = ipipe->head;
16838cefc107SDavid Howells 		o_tail = opipe->tail;
16848cefc107SDavid Howells 
168570524490SJens Axboe 		/*
16868cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1687aadd06e5SJens Axboe 		 * output room, break.
168870524490SJens Axboe 		 */
16898cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
16906718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1691aadd06e5SJens Axboe 			break;
1692aadd06e5SJens Axboe 
16938cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16948cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
169570524490SJens Axboe 
169670524490SJens Axboe 		/*
169770524490SJens Axboe 		 * Get a reference to this pipe buffer,
169870524490SJens Axboe 		 * so we can copy the contents over.
169970524490SJens Axboe 		 */
170015fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
170115fab63eSMatthew Wilcox 			if (ret == 0)
170215fab63eSMatthew Wilcox 				ret = -EFAULT;
170315fab63eSMatthew Wilcox 			break;
170415fab63eSMatthew Wilcox 		}
170570524490SJens Axboe 
170670524490SJens Axboe 		*obuf = *ibuf;
170770524490SJens Axboe 
17087afa6fd0SJens Axboe 		/*
1709f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1710f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
17117afa6fd0SJens Axboe 		 */
17127afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1713f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1714a0ce2f0aSJann Horn 
171570524490SJens Axboe 		if (obuf->len > len)
171670524490SJens Axboe 			obuf->len = len;
171770524490SJens Axboe 		ret += obuf->len;
171870524490SJens Axboe 		len -= obuf->len;
17198cefc107SDavid Howells 
17208cefc107SDavid Howells 		o_head++;
17218cefc107SDavid Howells 		opipe->head = o_head;
17228cefc107SDavid Howells 		i_tail++;
1723aadd06e5SJens Axboe 	} while (len);
172470524490SJens Axboe 
172561e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
172661e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
172770524490SJens Axboe 
1728aadd06e5SJens Axboe 	/*
1729aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1730aadd06e5SJens Axboe 	 */
1731825cdcb1SNamhyung Kim 	if (ret > 0)
1732825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
173370524490SJens Axboe 
173470524490SJens Axboe 	return ret;
173570524490SJens Axboe }
173670524490SJens Axboe 
173770524490SJens Axboe /*
173870524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
173970524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
174070524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
174170524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
174270524490SJens Axboe  */
174370524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
174470524490SJens Axboe 		   unsigned int flags)
174570524490SJens Axboe {
174671993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
174771993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1748aadd06e5SJens Axboe 	int ret = -EINVAL;
174970524490SJens Axboe 
175070524490SJens Axboe 	/*
1751aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1752aadd06e5SJens Axboe 	 * copying the data.
175370524490SJens Axboe 	 */
1754aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1755ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1756ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1757ee5e0011SSlavomir Kaslev 
1758aadd06e5SJens Axboe 		/*
1759aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1760aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1761aadd06e5SJens Axboe 		 */
17627c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1763aadd06e5SJens Axboe 		if (!ret) {
17647c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
176502cf01aeSJens Axboe 			if (!ret)
1766aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1767aadd06e5SJens Axboe 		}
1768aadd06e5SJens Axboe 	}
176970524490SJens Axboe 
1770aadd06e5SJens Axboe 	return ret;
177170524490SJens Axboe }
177270524490SJens Axboe 
1773836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
177470524490SJens Axboe {
17752903ff01SAl Viro 	struct fd in;
17762903ff01SAl Viro 	int error;
177770524490SJens Axboe 
17783d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
17793d6ea290SAl Viro 		return -EINVAL;
17803d6ea290SAl Viro 
178170524490SJens Axboe 	if (unlikely(!len))
178270524490SJens Axboe 		return 0;
178370524490SJens Axboe 
178470524490SJens Axboe 	error = -EBADF;
17852903ff01SAl Viro 	in = fdget(fdin);
17862903ff01SAl Viro 	if (in.file) {
17872903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
17882903ff01SAl Viro 			struct fd out = fdget(fdout);
17892903ff01SAl Viro 			if (out.file) {
17902903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
17912903ff01SAl Viro 					error = do_tee(in.file, out.file,
17922903ff01SAl Viro 							len, flags);
17932903ff01SAl Viro 				fdput(out);
179470524490SJens Axboe 			}
179570524490SJens Axboe 		}
17962903ff01SAl Viro  		fdput(in);
179770524490SJens Axboe  	}
179870524490SJens Axboe 
179970524490SJens Axboe 	return error;
180070524490SJens Axboe }
1801