xref: /openbmc/linux/fs/splice.c (revision 90da2e3f)
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  */
4776ad4d11SJens Axboe static int page_cache_pipe_buf_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;
795abc97aaSJens Axboe 			return 0;
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);
899e94cd4fSJens Axboe 	return 1;
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,
1445abc97aaSJens Axboe 	.steal = page_cache_pipe_buf_steal,
145f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
1465274f052SJens Axboe };
1475274f052SJens Axboe 
148912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
149912d35f8SJens Axboe 				    struct pipe_buffer *buf)
150912d35f8SJens Axboe {
1517afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152912d35f8SJens Axboe 		return 1;
1537afa6fd0SJens Axboe 
1541432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
155330ab716SJens Axboe 	return generic_pipe_buf_steal(pipe, buf);
156912d35f8SJens Axboe }
157912d35f8SJens Axboe 
158d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159cac36bb0SJens Axboe 	.confirm = generic_pipe_buf_confirm,
160912d35f8SJens Axboe 	.release = page_cache_pipe_buf_release,
161912d35f8SJens Axboe 	.steal = user_page_pipe_buf_steal,
162f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
163912d35f8SJens Axboe };
164912d35f8SJens Axboe 
165825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
166825cdcb1SNamhyung Kim {
167825cdcb1SNamhyung Kim 	smp_mb();
1680ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1690ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_wait);
170825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
171825cdcb1SNamhyung Kim }
172825cdcb1SNamhyung Kim 
173932cc6d4SJens Axboe /**
174932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
175932cc6d4SJens Axboe  * @pipe:	pipe to fill
176932cc6d4SJens Axboe  * @spd:	data to fill
177932cc6d4SJens Axboe  *
178932cc6d4SJens Axboe  * Description:
17979685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
180932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
181932cc6d4SJens Axboe  *    function will link that data to the pipe.
182932cc6d4SJens Axboe  *
18383f9135bSJens Axboe  */
184d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
185912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1865274f052SJens Axboe {
18700de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
1888cefc107SDavid Howells 	unsigned int tail = pipe->tail;
1898cefc107SDavid Howells 	unsigned int head = pipe->head;
1908cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
1918924feffSAl Viro 	int ret = 0, page_nr = 0;
1925274f052SJens Axboe 
193d6785d91SRabin Vincent 	if (!spd_pages)
194d6785d91SRabin Vincent 		return 0;
195d6785d91SRabin Vincent 
1968924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1975274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1985274f052SJens Axboe 		ret = -EPIPE;
1998924feffSAl Viro 		goto out;
2005274f052SJens Axboe 	}
2015274f052SJens Axboe 
2026718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2038cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
2045274f052SJens Axboe 
205912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
206912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
207912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
208497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
209912d35f8SJens Axboe 		buf->ops = spd->ops;
2105a81e6a1SMiklos Szeredi 		buf->flags = 0;
2117afa6fd0SJens Axboe 
2128cefc107SDavid Howells 		head++;
2138cefc107SDavid Howells 		pipe->head = head;
214912d35f8SJens Axboe 		page_nr++;
215912d35f8SJens Axboe 		ret += buf->len;
216912d35f8SJens Axboe 
217912d35f8SJens Axboe 		if (!--spd->nr_pages)
2185274f052SJens Axboe 			break;
2195274f052SJens Axboe 	}
2205274f052SJens Axboe 
22129e35094SLinus Torvalds 	if (!ret)
22229e35094SLinus Torvalds 		ret = -EAGAIN;
22329e35094SLinus Torvalds 
2248924feffSAl Viro out:
22500de00bdSJens Axboe 	while (page_nr < spd_pages)
226bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2275274f052SJens Axboe 
2285274f052SJens Axboe 	return ret;
2295274f052SJens Axboe }
2302b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2315274f052SJens Axboe 
23279fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
23379fddc4eSAl Viro {
2348cefc107SDavid Howells 	unsigned int head = pipe->head;
2358cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2368cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
23779fddc4eSAl Viro 	int ret;
23879fddc4eSAl Viro 
23979fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
24079fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
24179fddc4eSAl Viro 		ret = -EPIPE;
2426718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
24379fddc4eSAl Viro 		ret = -EAGAIN;
24479fddc4eSAl Viro 	} else {
2458cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2468cefc107SDavid Howells 		pipe->head = head + 1;
24779fddc4eSAl Viro 		return buf->len;
24879fddc4eSAl Viro 	}
249a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
25079fddc4eSAl Viro 	return ret;
25179fddc4eSAl Viro }
25279fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
25379fddc4eSAl Viro 
25435f3d14dSJens Axboe /*
25535f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25635f3d14dSJens Axboe  * descriptions.
25735f3d14dSJens Axboe  */
258047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25935f3d14dSJens Axboe {
2606718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
261047fe360SEric Dumazet 
2628cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2638cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
26435f3d14dSJens Axboe 		return 0;
26535f3d14dSJens Axboe 
2668cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2678cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2686da2ec56SKees Cook 				     GFP_KERNEL);
26935f3d14dSJens Axboe 
27035f3d14dSJens Axboe 	if (spd->pages && spd->partial)
27135f3d14dSJens Axboe 		return 0;
27235f3d14dSJens Axboe 
27335f3d14dSJens Axboe 	kfree(spd->pages);
27435f3d14dSJens Axboe 	kfree(spd->partial);
27535f3d14dSJens Axboe 	return -ENOMEM;
27635f3d14dSJens Axboe }
27735f3d14dSJens Axboe 
278047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27935f3d14dSJens Axboe {
280047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
28135f3d14dSJens Axboe 		return;
28235f3d14dSJens Axboe 
28335f3d14dSJens Axboe 	kfree(spd->pages);
28435f3d14dSJens Axboe 	kfree(spd->partial);
28535f3d14dSJens Axboe }
28635f3d14dSJens Axboe 
28783f9135bSJens Axboe /**
28883f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
28983f9135bSJens Axboe  * @in:		file to splice from
290932cc6d4SJens Axboe  * @ppos:	position in @in
29183f9135bSJens Axboe  * @pipe:	pipe to splice to
29283f9135bSJens Axboe  * @len:	number of bytes to splice
29383f9135bSJens Axboe  * @flags:	splice modifier flags
29483f9135bSJens Axboe  *
295932cc6d4SJens Axboe  * Description:
296932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
29782c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
298932cc6d4SJens Axboe  *
29983f9135bSJens Axboe  */
300cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
301cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
302cbb7e577SJens Axboe 				 unsigned int flags)
3035274f052SJens Axboe {
30482c156f8SAl Viro 	struct iov_iter to;
30582c156f8SAl Viro 	struct kiocb kiocb;
3068cefc107SDavid Howells 	unsigned int i_head;
3078cefc107SDavid Howells 	int ret;
308be64f884SBoaz Harrosh 
309aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len);
3108cefc107SDavid Howells 	i_head = to.head;
31182c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
31282c156f8SAl Viro 	kiocb.ki_pos = *ppos;
313bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
314723590edSMiklos Szeredi 	if (ret > 0) {
31582c156f8SAl Viro 		*ppos = kiocb.ki_pos;
316723590edSMiklos Szeredi 		file_accessed(in);
31782c156f8SAl Viro 	} else if (ret < 0) {
3188cefc107SDavid Howells 		to.head = i_head;
31982c156f8SAl Viro 		to.iov_offset = 0;
32082c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
32182c156f8SAl Viro 		/*
32282c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
32382c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
32482c156f8SAl Viro 		 */
32582c156f8SAl Viro 		if (ret == -EFAULT)
32682c156f8SAl Viro 			ret = -EAGAIN;
327723590edSMiklos Szeredi 	}
3285274f052SJens Axboe 
3295274f052SJens Axboe 	return ret;
3305274f052SJens Axboe }
331059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
332059a8f37SJens Axboe 
333241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3346818173bSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
3356818173bSMiklos Szeredi 	.release = generic_pipe_buf_release,
3366818173bSMiklos Szeredi 	.steal = generic_pipe_buf_steal,
3376818173bSMiklos Szeredi 	.get = generic_pipe_buf_get,
3386818173bSMiklos Szeredi };
3396818173bSMiklos Szeredi 
340b9872226SJann Horn int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
34128a625cbSMiklos Szeredi 			     struct pipe_buffer *buf)
34228a625cbSMiklos Szeredi {
34328a625cbSMiklos Szeredi 	return 1;
34428a625cbSMiklos Szeredi }
34528a625cbSMiklos Szeredi 
34628a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
34728a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
34828a625cbSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
34928a625cbSMiklos Szeredi 	.release = generic_pipe_buf_release,
35028a625cbSMiklos Szeredi 	.steal = generic_pipe_buf_nosteal,
35128a625cbSMiklos Szeredi 	.get = generic_pipe_buf_get,
35228a625cbSMiklos Szeredi };
35328a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
35428a625cbSMiklos Szeredi 
355523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
3566818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
3576818173bSMiklos Szeredi {
3586818173bSMiklos Szeredi 	mm_segment_t old_fs;
3596818173bSMiklos Szeredi 	loff_t pos = offset;
3606818173bSMiklos Szeredi 	ssize_t res;
3616818173bSMiklos Szeredi 
3626818173bSMiklos Szeredi 	old_fs = get_fs();
363736706beSLinus Torvalds 	set_fs(KERNEL_DS);
3646818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
365793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
3666818173bSMiklos Szeredi 	set_fs(old_fs);
3676818173bSMiklos Szeredi 
3686818173bSMiklos Szeredi 	return res;
3696818173bSMiklos Szeredi }
3706818173bSMiklos Szeredi 
37182c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
3726818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
3736818173bSMiklos Szeredi 				 unsigned int flags)
3746818173bSMiklos Szeredi {
375523ac9afSAl Viro 	struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
376523ac9afSAl Viro 	struct iov_iter to;
377523ac9afSAl Viro 	struct page **pages;
3786818173bSMiklos Szeredi 	unsigned int nr_pages;
3798cefc107SDavid Howells 	unsigned int mask;
38013c0f52bSAl Viro 	size_t offset, base, copied = 0;
3816818173bSMiklos Szeredi 	ssize_t res;
3826818173bSMiklos Szeredi 	int i;
3836818173bSMiklos Szeredi 
3846718b6f8SDavid Howells 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
385523ac9afSAl Viro 		return -EAGAIN;
386523ac9afSAl Viro 
387523ac9afSAl Viro 	/*
388523ac9afSAl Viro 	 * Try to keep page boundaries matching to source pagecache ones -
389523ac9afSAl Viro 	 * it probably won't be much help, but...
390523ac9afSAl Viro 	 */
391523ac9afSAl Viro 	offset = *ppos & ~PAGE_MASK;
392523ac9afSAl Viro 
393aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len + offset);
394523ac9afSAl Viro 
39513c0f52bSAl Viro 	res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
396523ac9afSAl Viro 	if (res <= 0)
39735f3d14dSJens Axboe 		return -ENOMEM;
39835f3d14dSJens Axboe 
39913c0f52bSAl Viro 	nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
400523ac9afSAl Viro 
40135f3d14dSJens Axboe 	vec = __vec;
402523ac9afSAl Viro 	if (nr_pages > PIPE_DEF_BUFFERS) {
4036da2ec56SKees Cook 		vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
404523ac9afSAl Viro 		if (unlikely(!vec)) {
405523ac9afSAl Viro 			res = -ENOMEM;
406523ac9afSAl Viro 			goto out;
407523ac9afSAl Viro 		}
40835f3d14dSJens Axboe 	}
40935f3d14dSJens Axboe 
4108cefc107SDavid Howells 	mask = pipe->ring_size - 1;
4118cefc107SDavid Howells 	pipe->bufs[to.head & mask].offset = offset;
4128cefc107SDavid Howells 	pipe->bufs[to.head & mask].len -= offset;
4136818173bSMiklos Szeredi 
414523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++) {
415523ac9afSAl Viro 		size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
416523ac9afSAl Viro 		vec[i].iov_base = page_address(pages[i]) + offset;
4176818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
4186818173bSMiklos Szeredi 		len -= this_len;
4196818173bSMiklos Szeredi 		offset = 0;
4206818173bSMiklos Szeredi 	}
4216818173bSMiklos Szeredi 
422523ac9afSAl Viro 	res = kernel_readv(in, vec, nr_pages, *ppos);
423523ac9afSAl Viro 	if (res > 0) {
424523ac9afSAl Viro 		copied = res;
4256818173bSMiklos Szeredi 		*ppos += res;
426523ac9afSAl Viro 	}
4276818173bSMiklos Szeredi 
42835f3d14dSJens Axboe 	if (vec != __vec)
42935f3d14dSJens Axboe 		kfree(vec);
430523ac9afSAl Viro out:
431523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++)
432523ac9afSAl Viro 		put_page(pages[i]);
433523ac9afSAl Viro 	kvfree(pages);
434523ac9afSAl Viro 	iov_iter_advance(&to, copied);	/* truncates and discards */
4356818173bSMiklos Szeredi 	return res;
4366818173bSMiklos Szeredi }
4376818173bSMiklos Szeredi 
4385274f052SJens Axboe /*
4394f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
440016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4415274f052SJens Axboe  */
44276ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4435274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4445274f052SJens Axboe {
4456a14b90bSJens Axboe 	struct file *file = sd->u.file;
4465274f052SJens Axboe 	loff_t pos = sd->pos;
447a8adbe37SMichał Mirosław 	int more;
4485274f052SJens Axboe 
44972c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
450a8adbe37SMichał Mirosław 		return -EINVAL;
451a8adbe37SMichał Mirosław 
45235f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
453ae62ca7bSEric Dumazet 
4548cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4558cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
45635f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
457ae62ca7bSEric Dumazet 
458a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
459f84d7519SJens Axboe 				    sd->len, &pos, more);
4605274f052SJens Axboe }
4615274f052SJens Axboe 
462b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
463b3c2d2ddSMiklos Szeredi {
464b3c2d2ddSMiklos Szeredi 	smp_mb();
4650ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4660ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
467b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
468b3c2d2ddSMiklos Szeredi }
469b3c2d2ddSMiklos Szeredi 
470b3c2d2ddSMiklos Szeredi /**
471b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
472b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
473b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
474b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
475b3c2d2ddSMiklos Szeredi  *
476b3c2d2ddSMiklos Szeredi  * Description:
477b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
478b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
479b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
480b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
481b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
482b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
483b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
484b3c2d2ddSMiklos Szeredi  *
485b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
486b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
487b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
488b3c2d2ddSMiklos Szeredi  *    destination.
489b3c2d2ddSMiklos Szeredi  */
49096f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
491b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
492b3c2d2ddSMiklos Szeredi {
4938cefc107SDavid Howells 	unsigned int head = pipe->head;
4948cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4958cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
496b3c2d2ddSMiklos Szeredi 	int ret;
497b3c2d2ddSMiklos Szeredi 
498ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4998cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
500b3c2d2ddSMiklos Szeredi 
501b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
502b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
503b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
504b3c2d2ddSMiklos Szeredi 
505fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
506a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
507b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
508b3c2d2ddSMiklos Szeredi 				ret = 0;
509b3c2d2ddSMiklos Szeredi 			return ret;
510b3c2d2ddSMiklos Szeredi 		}
511a8adbe37SMichał Mirosław 
512a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
513a8adbe37SMichał Mirosław 		if (ret <= 0)
514a8adbe37SMichał Mirosław 			return ret;
515a8adbe37SMichał Mirosław 
516b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
517b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
518b3c2d2ddSMiklos Szeredi 
519b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
520b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
521b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
522b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
523b3c2d2ddSMiklos Szeredi 
524b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
525a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5268cefc107SDavid Howells 			tail++;
5278cefc107SDavid Howells 			pipe->tail = tail;
5286447a3cfSAl Viro 			if (pipe->files)
529b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
530b3c2d2ddSMiklos Szeredi 		}
531b3c2d2ddSMiklos Szeredi 
532b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
533b3c2d2ddSMiklos Szeredi 			return 0;
534b3c2d2ddSMiklos Szeredi 	}
535b3c2d2ddSMiklos Szeredi 
536b3c2d2ddSMiklos Szeredi 	return 1;
537b3c2d2ddSMiklos Szeredi }
538b3c2d2ddSMiklos Szeredi 
539b3c2d2ddSMiklos Szeredi /**
540b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
541b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
542b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
543b3c2d2ddSMiklos Szeredi  *
544b3c2d2ddSMiklos Szeredi  * Description:
545b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
546b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
547b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
548b3c2d2ddSMiklos Szeredi  */
54996f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
550b3c2d2ddSMiklos Szeredi {
551c725bfceSJan Kara 	/*
552c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
553c725bfceSJan Kara 	 * always buffers available
554c725bfceSJan Kara 	 */
555c725bfceSJan Kara 	if (signal_pending(current))
556c725bfceSJan Kara 		return -ERESTARTSYS;
557c725bfceSJan Kara 
5588cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
559b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
560b3c2d2ddSMiklos Szeredi 			return 0;
561b3c2d2ddSMiklos Szeredi 
562a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
563b3c2d2ddSMiklos Szeredi 			return 0;
564b3c2d2ddSMiklos Szeredi 
565b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
566b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
567b3c2d2ddSMiklos Szeredi 
568b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
569b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
570b3c2d2ddSMiklos Szeredi 
571b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
572b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
573b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
574b3c2d2ddSMiklos Szeredi 		}
575b3c2d2ddSMiklos Szeredi 
576b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
577b3c2d2ddSMiklos Szeredi 	}
578b3c2d2ddSMiklos Szeredi 
579b3c2d2ddSMiklos Szeredi 	return 1;
580b3c2d2ddSMiklos Szeredi }
581b3c2d2ddSMiklos Szeredi 
582b3c2d2ddSMiklos Szeredi /**
583b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
584b80901bbSRandy Dunlap  * @sd:		information about the splice operation
585b3c2d2ddSMiklos Szeredi  *
586b3c2d2ddSMiklos Szeredi  * Description:
587b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
588b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
589b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
590b3c2d2ddSMiklos Szeredi  */
59196f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
592b3c2d2ddSMiklos Szeredi {
593b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
594b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
595b3c2d2ddSMiklos Szeredi }
596b3c2d2ddSMiklos Szeredi 
597b3c2d2ddSMiklos Szeredi /**
598b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
599b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
600b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
601b3c2d2ddSMiklos Szeredi  *
602b3c2d2ddSMiklos Szeredi  * Description:
603b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
604b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
605b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
606b3c2d2ddSMiklos Szeredi  */
60796f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
608b3c2d2ddSMiklos Szeredi {
609b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
610b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
611b3c2d2ddSMiklos Szeredi }
612b3c2d2ddSMiklos Szeredi 
613932cc6d4SJens Axboe /**
614932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
615932cc6d4SJens Axboe  * @pipe:	pipe to splice from
616932cc6d4SJens Axboe  * @sd:		information to @actor
617932cc6d4SJens Axboe  * @actor:	handler that splices the data
618932cc6d4SJens Axboe  *
619932cc6d4SJens Axboe  * Description:
620932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
621932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
622932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
623932cc6d4SJens Axboe  *    pipe_to_user.
624932cc6d4SJens Axboe  *
62583f9135bSJens Axboe  */
626c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
627c66ab6faSJens Axboe 			   splice_actor *actor)
6285274f052SJens Axboe {
629b3c2d2ddSMiklos Szeredi 	int ret;
6305274f052SJens Axboe 
631b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
632b3c2d2ddSMiklos Szeredi 	do {
633c2489e07SJan Kara 		cond_resched();
634b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
635b3c2d2ddSMiklos Szeredi 		if (ret > 0)
636b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
637b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
638b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6395274f052SJens Axboe 
640b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6415274f052SJens Axboe }
64240bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6435274f052SJens Axboe 
644932cc6d4SJens Axboe /**
645932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
646932cc6d4SJens Axboe  * @pipe:	pipe to splice from
647932cc6d4SJens Axboe  * @out:	file to splice to
648932cc6d4SJens Axboe  * @ppos:	position in @out
649932cc6d4SJens Axboe  * @len:	how many bytes to splice
650932cc6d4SJens Axboe  * @flags:	splice modifier flags
651932cc6d4SJens Axboe  * @actor:	handler that splices the data
652932cc6d4SJens Axboe  *
653932cc6d4SJens Axboe  * Description:
6542933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
655932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
656932cc6d4SJens Axboe  *
657932cc6d4SJens Axboe  */
6586da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6596da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6606da61809SMark Fasheh 			 splice_actor *actor)
6616da61809SMark Fasheh {
6626da61809SMark Fasheh 	ssize_t ret;
663c66ab6faSJens Axboe 	struct splice_desc sd = {
664c66ab6faSJens Axboe 		.total_len = len,
665c66ab6faSJens Axboe 		.flags = flags,
666c66ab6faSJens Axboe 		.pos = *ppos,
6676a14b90bSJens Axboe 		.u.file = out,
668c66ab6faSJens Axboe 	};
6696da61809SMark Fasheh 
67061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
671c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
67261e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6736da61809SMark Fasheh 
6746da61809SMark Fasheh 	return ret;
6756da61809SMark Fasheh }
6766da61809SMark Fasheh 
6776da61809SMark Fasheh /**
6788d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6798d020765SAl Viro  * @pipe:	pipe info
6808d020765SAl Viro  * @out:	file to write to
6818d020765SAl Viro  * @ppos:	position in @out
6828d020765SAl Viro  * @len:	number of bytes to splice
6838d020765SAl Viro  * @flags:	splice modifier flags
6848d020765SAl Viro  *
6858d020765SAl Viro  * Description:
6868d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6878d020765SAl Viro  *    the given pipe inode to the given file.
6888d020765SAl Viro  *    This one is ->write_iter-based.
6898d020765SAl Viro  *
6908d020765SAl Viro  */
6918d020765SAl Viro ssize_t
6928d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6938d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
6948d020765SAl Viro {
6958d020765SAl Viro 	struct splice_desc sd = {
6968d020765SAl Viro 		.total_len = len,
6978d020765SAl Viro 		.flags = flags,
6988d020765SAl Viro 		.pos = *ppos,
6998d020765SAl Viro 		.u.file = out,
7008d020765SAl Viro 	};
7016718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
7028d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7038d020765SAl Viro 					GFP_KERNEL);
7048d020765SAl Viro 	ssize_t ret;
7058d020765SAl Viro 
7068d020765SAl Viro 	if (unlikely(!array))
7078d020765SAl Viro 		return -ENOMEM;
7088d020765SAl Viro 
7098d020765SAl Viro 	pipe_lock(pipe);
7108d020765SAl Viro 
7118d020765SAl Viro 	splice_from_pipe_begin(&sd);
7128d020765SAl Viro 	while (sd.total_len) {
7138d020765SAl Viro 		struct iov_iter from;
714ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7158d020765SAl Viro 		size_t left;
7168cefc107SDavid Howells 		int n;
7178d020765SAl Viro 
7188d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7198d020765SAl Viro 		if (ret <= 0)
7208d020765SAl Viro 			break;
7218d020765SAl Viro 
7226718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7238d020765SAl Viro 			kfree(array);
7246718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7258d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7268d020765SAl Viro 					GFP_KERNEL);
7278d020765SAl Viro 			if (!array) {
7288d020765SAl Viro 				ret = -ENOMEM;
7298d020765SAl Viro 				break;
7308d020765SAl Viro 			}
7318d020765SAl Viro 		}
7328d020765SAl Viro 
733ec057595SLinus Torvalds 		head = pipe->head;
734ec057595SLinus Torvalds 		tail = pipe->tail;
735ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
736ec057595SLinus Torvalds 
7378d020765SAl Viro 		/* build the vector */
7388d020765SAl Viro 		left = sd.total_len;
7398cefc107SDavid Howells 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
7408cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7418d020765SAl Viro 			size_t this_len = buf->len;
7428d020765SAl Viro 
7438d020765SAl Viro 			if (this_len > left)
7448d020765SAl Viro 				this_len = left;
7458d020765SAl Viro 
746fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7478d020765SAl Viro 			if (unlikely(ret)) {
7488d020765SAl Viro 				if (ret == -ENODATA)
7498d020765SAl Viro 					ret = 0;
7508d020765SAl Viro 				goto done;
7518d020765SAl Viro 			}
7528d020765SAl Viro 
7538d020765SAl Viro 			array[n].bv_page = buf->page;
7548d020765SAl Viro 			array[n].bv_len = this_len;
7558d020765SAl Viro 			array[n].bv_offset = buf->offset;
7568d020765SAl Viro 			left -= this_len;
7578d020765SAl Viro 		}
7588d020765SAl Viro 
759aa563d7bSDavid Howells 		iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
760abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7618d020765SAl Viro 		if (ret <= 0)
7628d020765SAl Viro 			break;
7638d020765SAl Viro 
7648d020765SAl Viro 		sd.num_spliced += ret;
7658d020765SAl Viro 		sd.total_len -= ret;
766dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7678d020765SAl Viro 
7688d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7698cefc107SDavid Howells 		tail = pipe->tail;
7708d020765SAl Viro 		while (ret) {
7718cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7728d020765SAl Viro 			if (ret >= buf->len) {
7738d020765SAl Viro 				ret -= buf->len;
7748d020765SAl Viro 				buf->len = 0;
775a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7768cefc107SDavid Howells 				tail++;
7778cefc107SDavid Howells 				pipe->tail = tail;
7788d020765SAl Viro 				if (pipe->files)
7798d020765SAl Viro 					sd.need_wakeup = true;
7808d020765SAl Viro 			} else {
7818d020765SAl Viro 				buf->offset += ret;
7828d020765SAl Viro 				buf->len -= ret;
7838d020765SAl Viro 				ret = 0;
7848d020765SAl Viro 			}
7858d020765SAl Viro 		}
7868d020765SAl Viro 	}
7878d020765SAl Viro done:
7888d020765SAl Viro 	kfree(array);
7898d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7908d020765SAl Viro 
7918d020765SAl Viro 	pipe_unlock(pipe);
7928d020765SAl Viro 
7938d020765SAl Viro 	if (sd.num_spliced)
7948d020765SAl Viro 		ret = sd.num_spliced;
7958d020765SAl Viro 
7968d020765SAl Viro 	return ret;
7978d020765SAl Viro }
7988d020765SAl Viro 
7998d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8008d020765SAl Viro 
801b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
802b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
8030b0a47f5SMiklos Szeredi {
804b2858d7dSMiklos Szeredi 	int ret;
805b2858d7dSMiklos Szeredi 	void *data;
80606ae43f3SAl Viro 	loff_t tmp = sd->pos;
807b2858d7dSMiklos Szeredi 
808fbb32750SAl Viro 	data = kmap(buf->page);
80906ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
810fbb32750SAl Viro 	kunmap(buf->page);
811b2858d7dSMiklos Szeredi 
812b2858d7dSMiklos Szeredi 	return ret;
8130b0a47f5SMiklos Szeredi }
8140b0a47f5SMiklos Szeredi 
8150b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
8160b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
8170b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
8180b0a47f5SMiklos Szeredi {
819b2858d7dSMiklos Szeredi 	ssize_t ret;
8200b0a47f5SMiklos Szeredi 
821b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
822b2858d7dSMiklos Szeredi 	if (ret > 0)
823b2858d7dSMiklos Szeredi 		*ppos += ret;
8240b0a47f5SMiklos Szeredi 
825b2858d7dSMiklos Szeredi 	return ret;
8260b0a47f5SMiklos Szeredi }
8270b0a47f5SMiklos Szeredi 
82883f9135bSJens Axboe /**
82983f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
830932cc6d4SJens Axboe  * @pipe:	pipe to splice from
83183f9135bSJens Axboe  * @out:	socket to write to
832932cc6d4SJens Axboe  * @ppos:	position in @out
83383f9135bSJens Axboe  * @len:	number of bytes to splice
83483f9135bSJens Axboe  * @flags:	splice modifier flags
83583f9135bSJens Axboe  *
836932cc6d4SJens Axboe  * Description:
83783f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
83883f9135bSJens Axboe  *    is involved.
83983f9135bSJens Axboe  *
84083f9135bSJens Axboe  */
8413a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
842cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8435274f052SJens Axboe {
84400522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8455274f052SJens Axboe }
8465274f052SJens Axboe 
847059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
848a0f06780SJeff Garzik 
84983f9135bSJens Axboe /*
85083f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
85183f9135bSJens Axboe  */
8523a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
853cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8545274f052SJens Axboe {
8550b0a47f5SMiklos Szeredi 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
8560b0a47f5SMiklos Szeredi 				loff_t *, size_t, unsigned int);
8575274f052SJens Axboe 
85872c2d531SAl Viro 	if (out->f_op->splice_write)
8590b0a47f5SMiklos Szeredi 		splice_write = out->f_op->splice_write;
860cc56f7deSChangli Gao 	else
8610b0a47f5SMiklos Szeredi 		splice_write = default_file_splice_write;
8620b0a47f5SMiklos Szeredi 
863500368f7SAl Viro 	return splice_write(pipe, out, ppos, len, flags);
8645274f052SJens Axboe }
8655274f052SJens Axboe 
86683f9135bSJens Axboe /*
86783f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
86883f9135bSJens Axboe  */
869cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
870cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
871cbb7e577SJens Axboe 			 unsigned int flags)
8725274f052SJens Axboe {
8736818173bSMiklos Szeredi 	ssize_t (*splice_read)(struct file *, loff_t *,
8746818173bSMiklos Szeredi 			       struct pipe_inode_info *, size_t, unsigned int);
8755274f052SJens Axboe 	int ret;
8765274f052SJens Axboe 
87749570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8785274f052SJens Axboe 		return -EBADF;
8795274f052SJens Axboe 
880cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8815274f052SJens Axboe 	if (unlikely(ret < 0))
8825274f052SJens Axboe 		return ret;
8835274f052SJens Axboe 
88403cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
88503cc0789SAl Viro 		len = MAX_RW_COUNT;
88603cc0789SAl Viro 
88772c2d531SAl Viro 	if (in->f_op->splice_read)
8886818173bSMiklos Szeredi 		splice_read = in->f_op->splice_read;
889cc56f7deSChangli Gao 	else
8906818173bSMiklos Szeredi 		splice_read = default_file_splice_read;
8916818173bSMiklos Szeredi 
8926818173bSMiklos Szeredi 	return splice_read(in, ppos, pipe, len, flags);
8935274f052SJens Axboe }
8945274f052SJens Axboe 
895932cc6d4SJens Axboe /**
896932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
897932cc6d4SJens Axboe  * @in:		file to splice from
898932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
899932cc6d4SJens Axboe  * @actor:	handles the data splicing
900932cc6d4SJens Axboe  *
901932cc6d4SJens Axboe  * Description:
902932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
903932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
904932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
905932cc6d4SJens Axboe  *    that process.
906932cc6d4SJens Axboe  *
907c66ab6faSJens Axboe  */
908c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
909c66ab6faSJens Axboe 			       splice_direct_actor *actor)
910b92ce558SJens Axboe {
911b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
912b92ce558SJens Axboe 	long ret, bytes;
913b92ce558SJens Axboe 	umode_t i_mode;
914c66ab6faSJens Axboe 	size_t len;
9150ff28d9fSChristophe Leroy 	int i, flags, more;
916b92ce558SJens Axboe 
917b92ce558SJens Axboe 	/*
918b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
919b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
920b92ce558SJens Axboe 	 * piped splicing for that!
921b92ce558SJens Axboe 	 */
922496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
923b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
924b92ce558SJens Axboe 		return -EINVAL;
925b92ce558SJens Axboe 
926b92ce558SJens Axboe 	/*
927b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
928b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
929b92ce558SJens Axboe 	 */
930b92ce558SJens Axboe 	pipe = current->splice_pipe;
93149570e9bSJens Axboe 	if (unlikely(!pipe)) {
9327bee130eSAl Viro 		pipe = alloc_pipe_info();
933b92ce558SJens Axboe 		if (!pipe)
934b92ce558SJens Axboe 			return -ENOMEM;
935b92ce558SJens Axboe 
936b92ce558SJens Axboe 		/*
937b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
93800522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
939b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
940b92ce558SJens Axboe 		 */
941b92ce558SJens Axboe 		pipe->readers = 1;
942b92ce558SJens Axboe 
943b92ce558SJens Axboe 		current->splice_pipe = pipe;
944b92ce558SJens Axboe 	}
945b92ce558SJens Axboe 
946b92ce558SJens Axboe 	/*
94773d62d83SIngo Molnar 	 * Do the splice.
948b92ce558SJens Axboe 	 */
949b92ce558SJens Axboe 	ret = 0;
950b92ce558SJens Axboe 	bytes = 0;
951c66ab6faSJens Axboe 	len = sd->total_len;
952c66ab6faSJens Axboe 	flags = sd->flags;
953c66ab6faSJens Axboe 
954c66ab6faSJens Axboe 	/*
955c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
956c66ab6faSJens Axboe 	 */
957c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9580ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
959b92ce558SJens Axboe 
9608cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
96117614445SDarrick J. Wong 
962b92ce558SJens Axboe 	while (len) {
9638cefc107SDavid Howells 		unsigned int p_space;
96451a92c0fSJens Axboe 		size_t read_len;
965a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
966b92ce558SJens Axboe 
96717614445SDarrick J. Wong 		/* Don't try to read more the pipe has space for. */
9686718b6f8SDavid Howells 		p_space = pipe->max_usage -
9698cefc107SDavid Howells 			pipe_occupancy(pipe->head, pipe->tail);
9708cefc107SDavid Howells 		read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
97117614445SDarrick J. Wong 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
97251a92c0fSJens Axboe 		if (unlikely(ret <= 0))
973b92ce558SJens Axboe 			goto out_release;
974b92ce558SJens Axboe 
975b92ce558SJens Axboe 		read_len = ret;
976c66ab6faSJens Axboe 		sd->total_len = read_len;
977b92ce558SJens Axboe 
978b92ce558SJens Axboe 		/*
9790ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9800ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9810ff28d9fSChristophe Leroy 		 * initially, clears it.
9820ff28d9fSChristophe Leroy 		 */
9830ff28d9fSChristophe Leroy 		if (read_len < len)
9840ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9850ff28d9fSChristophe Leroy 		else if (!more)
9860ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9870ff28d9fSChristophe Leroy 		/*
988b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
989b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
990b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
991b92ce558SJens Axboe 		 */
992c66ab6faSJens Axboe 		ret = actor(pipe, sd);
993a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
994a82c53a0STom Zanussi 			sd->pos = prev_pos;
995b92ce558SJens Axboe 			goto out_release;
996a82c53a0STom Zanussi 		}
997b92ce558SJens Axboe 
998b92ce558SJens Axboe 		bytes += ret;
999b92ce558SJens Axboe 		len -= ret;
1000bcd4f3acSJens Axboe 		sd->pos = pos;
1001b92ce558SJens Axboe 
1002a82c53a0STom Zanussi 		if (ret < read_len) {
1003a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
100451a92c0fSJens Axboe 			goto out_release;
1005b92ce558SJens Axboe 		}
1006a82c53a0STom Zanussi 	}
1007b92ce558SJens Axboe 
10089e97198dSJens Axboe done:
10098cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
10109e97198dSJens Axboe 	file_accessed(in);
1011b92ce558SJens Axboe 	return bytes;
1012b92ce558SJens Axboe 
1013b92ce558SJens Axboe out_release:
1014b92ce558SJens Axboe 	/*
1015b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1016b92ce558SJens Axboe 	 * the pipe buffers in question:
1017b92ce558SJens Axboe 	 */
10188cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
10198cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1020b92ce558SJens Axboe 
1021a779638cSMiklos Szeredi 		if (buf->ops)
1022a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1023b92ce558SJens Axboe 	}
1024b92ce558SJens Axboe 
10259e97198dSJens Axboe 	if (!bytes)
10269e97198dSJens Axboe 		bytes = ret;
1027b92ce558SJens Axboe 
10289e97198dSJens Axboe 	goto done;
1029c66ab6faSJens Axboe }
1030c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1031c66ab6faSJens Axboe 
1032c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1033c66ab6faSJens Axboe 			       struct splice_desc *sd)
1034c66ab6faSJens Axboe {
10356a14b90bSJens Axboe 	struct file *file = sd->u.file;
1036c66ab6faSJens Axboe 
10377995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10382cb4b05eSChangli Gao 			      sd->flags);
1039c66ab6faSJens Axboe }
1040c66ab6faSJens Axboe 
1041932cc6d4SJens Axboe /**
1042932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1043932cc6d4SJens Axboe  * @in:		file to splice from
1044932cc6d4SJens Axboe  * @ppos:	input file offset
1045932cc6d4SJens Axboe  * @out:	file to splice to
1046acdb37c3SRandy Dunlap  * @opos:	output file offset
1047932cc6d4SJens Axboe  * @len:	number of bytes to splice
1048932cc6d4SJens Axboe  * @flags:	splice modifier flags
1049932cc6d4SJens Axboe  *
1050932cc6d4SJens Axboe  * Description:
1051932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1052932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1053932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1054932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1055932cc6d4SJens Axboe  *
1056932cc6d4SJens Axboe  */
1057c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10587995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1059c66ab6faSJens Axboe {
1060c66ab6faSJens Axboe 	struct splice_desc sd = {
1061c66ab6faSJens Axboe 		.len		= len,
1062c66ab6faSJens Axboe 		.total_len	= len,
1063c66ab6faSJens Axboe 		.flags		= flags,
1064c66ab6faSJens Axboe 		.pos		= *ppos,
10656a14b90bSJens Axboe 		.u.file		= out,
10667995bd28SAl Viro 		.opos		= opos,
1067c66ab6faSJens Axboe 	};
106851a92c0fSJens Axboe 	long ret;
1069c66ab6faSJens Axboe 
107018c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
107118c67cb9SAl Viro 		return -EBADF;
107218c67cb9SAl Viro 
107318c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
107418c67cb9SAl Viro 		return -EINVAL;
107518c67cb9SAl Viro 
107618c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
107718c67cb9SAl Viro 	if (unlikely(ret < 0))
107818c67cb9SAl Viro 		return ret;
107918c67cb9SAl Viro 
1080c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
108151a92c0fSJens Axboe 	if (ret > 0)
1082a82c53a0STom Zanussi 		*ppos = sd.pos;
108351a92c0fSJens Axboe 
1084c66ab6faSJens Axboe 	return ret;
1085b92ce558SJens Axboe }
10861c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1087b92ce558SJens Axboe 
10888924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10898924feffSAl Viro {
109052bce911SLinus Torvalds 	for (;;) {
109152bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
109252bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
109352bce911SLinus Torvalds 			return -EPIPE;
109452bce911SLinus Torvalds 		}
10956718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
109652bce911SLinus Torvalds 			return 0;
10978924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10988924feffSAl Viro 			return -EAGAIN;
10998924feffSAl Viro 		if (signal_pending(current))
11008924feffSAl Viro 			return -ERESTARTSYS;
11018924feffSAl Viro 		pipe_wait(pipe);
11028924feffSAl Viro 	}
11038924feffSAl Viro }
11048924feffSAl Viro 
11057c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
11067c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
11077c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1108ddac0d39SJens Axboe 
1109ddac0d39SJens Axboe /*
111083f9135bSJens Axboe  * Determine where to splice to/from.
111183f9135bSJens Axboe  */
1112444ebb57SPavel Begunkov long do_splice(struct file *in, loff_t __user *off_in,
1113529565dcSIngo Molnar 		struct file *out, loff_t __user *off_out,
1114529565dcSIngo Molnar 		size_t len, unsigned int flags)
11155274f052SJens Axboe {
11167c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11177c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11187995bd28SAl Viro 	loff_t offset;
1119a4514ebdSJens Axboe 	long ret;
11205274f052SJens Axboe 
112190da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
112290da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
112390da2e3fSPavel Begunkov 		return -EBADF;
112490da2e3fSPavel Begunkov 
112571993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
112671993e62SLinus Torvalds 	opipe = get_pipe_info(out);
11277c77f0b3SMiklos Szeredi 
11287c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11297c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11307c77f0b3SMiklos Szeredi 			return -ESPIPE;
11317c77f0b3SMiklos Szeredi 
11327c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11337c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11347c77f0b3SMiklos Szeredi 			return -EINVAL;
11357c77f0b3SMiklos Szeredi 
1136ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1137ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1138ee5e0011SSlavomir Kaslev 
11397c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11407c77f0b3SMiklos Szeredi 	}
11417c77f0b3SMiklos Szeredi 
11427c77f0b3SMiklos Szeredi 	if (ipipe) {
1143529565dcSIngo Molnar 		if (off_in)
1144529565dcSIngo Molnar 			return -ESPIPE;
1145b92ce558SJens Axboe 		if (off_out) {
114619c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1147b92ce558SJens Axboe 				return -EINVAL;
1148cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1149b92ce558SJens Axboe 				return -EFAULT;
11507995bd28SAl Viro 		} else {
11517995bd28SAl Viro 			offset = out->f_pos;
11527995bd28SAl Viro 		}
1153529565dcSIngo Molnar 
115418c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
115518c67cb9SAl Viro 			return -EINVAL;
115618c67cb9SAl Viro 
115718c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
115818c67cb9SAl Viro 		if (unlikely(ret < 0))
115918c67cb9SAl Viro 			return ret;
116018c67cb9SAl Viro 
1161ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1162ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1163ee5e0011SSlavomir Kaslev 
1164500368f7SAl Viro 		file_start_write(out);
11657995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1166500368f7SAl Viro 		file_end_write(out);
1167a4514ebdSJens Axboe 
11687995bd28SAl Viro 		if (!off_out)
11697995bd28SAl Viro 			out->f_pos = offset;
11707995bd28SAl Viro 		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1171a4514ebdSJens Axboe 			ret = -EFAULT;
1172a4514ebdSJens Axboe 
1173a4514ebdSJens Axboe 		return ret;
1174529565dcSIngo Molnar 	}
11755274f052SJens Axboe 
11767c77f0b3SMiklos Szeredi 	if (opipe) {
1177529565dcSIngo Molnar 		if (off_out)
1178529565dcSIngo Molnar 			return -ESPIPE;
1179b92ce558SJens Axboe 		if (off_in) {
118019c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1181b92ce558SJens Axboe 				return -EINVAL;
1182cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1183b92ce558SJens Axboe 				return -EFAULT;
11847995bd28SAl Viro 		} else {
11857995bd28SAl Viro 			offset = in->f_pos;
11867995bd28SAl Viro 		}
1187529565dcSIngo Molnar 
1188ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1189ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1190ee5e0011SSlavomir Kaslev 
11918924feffSAl Viro 		pipe_lock(opipe);
11928924feffSAl Viro 		ret = wait_for_space(opipe, flags);
11933253d9d0SDarrick J. Wong 		if (!ret) {
11946a965666SLinus Torvalds 			unsigned int p_space;
11953253d9d0SDarrick J. Wong 
11963253d9d0SDarrick J. Wong 			/* Don't try to read more the pipe has space for. */
11976a965666SLinus Torvalds 			p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail);
11986a965666SLinus Torvalds 			len = min_t(size_t, len, p_space << PAGE_SHIFT);
11993253d9d0SDarrick J. Wong 
12007995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
12013253d9d0SDarrick J. Wong 		}
12028924feffSAl Viro 		pipe_unlock(opipe);
12038924feffSAl Viro 		if (ret > 0)
12048924feffSAl Viro 			wakeup_pipe_readers(opipe);
12057995bd28SAl Viro 		if (!off_in)
12067995bd28SAl Viro 			in->f_pos = offset;
12077995bd28SAl Viro 		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1208a4514ebdSJens Axboe 			ret = -EFAULT;
1209a4514ebdSJens Axboe 
1210a4514ebdSJens Axboe 		return ret;
1211529565dcSIngo Molnar 	}
12125274f052SJens Axboe 
12135274f052SJens Axboe 	return -EINVAL;
12145274f052SJens Axboe }
12155274f052SJens Axboe 
121679fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
121779fddc4eSAl Viro 			struct pipe_inode_info *pipe,
121879fddc4eSAl Viro 			unsigned flags)
1219912d35f8SJens Axboe {
122079fddc4eSAl Viro 	struct pipe_buffer buf = {
122179fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
122279fddc4eSAl Viro 		.flags = flags
122379fddc4eSAl Viro 	};
122479fddc4eSAl Viro 	size_t total = 0;
122579fddc4eSAl Viro 	int ret = 0;
122679fddc4eSAl Viro 	bool failed = false;
122779fddc4eSAl Viro 
122879fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
122979fddc4eSAl Viro 		struct page *pages[16];
1230db85a9ebSAl Viro 		ssize_t copied;
1231db85a9ebSAl Viro 		size_t start;
123279fddc4eSAl Viro 		int n;
1233912d35f8SJens Axboe 
123479fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
123579fddc4eSAl Viro 		if (copied <= 0) {
123679fddc4eSAl Viro 			ret = copied;
123779fddc4eSAl Viro 			break;
123879fddc4eSAl Viro 		}
1239912d35f8SJens Axboe 
124079fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1241db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
124279fddc4eSAl Viro 			if (!failed) {
124379fddc4eSAl Viro 				buf.page = pages[n];
124479fddc4eSAl Viro 				buf.offset = start;
124579fddc4eSAl Viro 				buf.len = size;
124679fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
124779fddc4eSAl Viro 				if (unlikely(ret < 0)) {
124879fddc4eSAl Viro 					failed = true;
124979fddc4eSAl Viro 				} else {
125079fddc4eSAl Viro 					iov_iter_advance(from, ret);
125179fddc4eSAl Viro 					total += ret;
125279fddc4eSAl Viro 				}
125379fddc4eSAl Viro 			} else {
125479fddc4eSAl Viro 				put_page(pages[n]);
125579fddc4eSAl Viro 			}
1256db85a9ebSAl Viro 			copied -= size;
1257912d35f8SJens Axboe 		}
1258912d35f8SJens Axboe 	}
125979fddc4eSAl Viro 	return total ? total : ret;
1260912d35f8SJens Axboe }
1261912d35f8SJens Axboe 
12626a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12636a14b90bSJens Axboe 			struct splice_desc *sd)
12646a14b90bSJens Axboe {
12656130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
12666130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
12676a14b90bSJens Axboe }
12686a14b90bSJens Axboe 
12696a14b90bSJens Axboe /*
12706a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
12716a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
12726a14b90bSJens Axboe  */
127387a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
127487a3002aSAl Viro 			     unsigned int flags)
12756a14b90bSJens Axboe {
127687a3002aSAl Viro 	struct pipe_inode_info *pipe = get_pipe_info(file);
127787a3002aSAl Viro 	struct splice_desc sd = {
127887a3002aSAl Viro 		.total_len = iov_iter_count(iter),
127987a3002aSAl Viro 		.flags = flags,
128087a3002aSAl Viro 		.u.data = iter
128187a3002aSAl Viro 	};
128287a3002aSAl Viro 	long ret = 0;
12836a14b90bSJens Axboe 
12846a14b90bSJens Axboe 	if (!pipe)
12856a14b90bSJens Axboe 		return -EBADF;
12866a14b90bSJens Axboe 
1287345995faSAl Viro 	if (sd.total_len) {
12886130f531SAl Viro 		pipe_lock(pipe);
12896130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
129061e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1291345995faSAl Viro 	}
12926a14b90bSJens Axboe 
12936a14b90bSJens Axboe 	return ret;
12946a14b90bSJens Axboe }
12956a14b90bSJens Axboe 
1296912d35f8SJens Axboe /*
1297912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1298912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1299912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1300912d35f8SJens Axboe  */
130187a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
130287a3002aSAl Viro 			     unsigned int flags)
1303912d35f8SJens Axboe {
1304ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
130587a3002aSAl Viro 	long ret = 0;
130679fddc4eSAl Viro 	unsigned buf_flag = 0;
130779fddc4eSAl Viro 
130879fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
130979fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1310912d35f8SJens Axboe 
131171993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1312ddac0d39SJens Axboe 	if (!pipe)
1313912d35f8SJens Axboe 		return -EBADF;
1314912d35f8SJens Axboe 
13158924feffSAl Viro 	pipe_lock(pipe);
13168924feffSAl Viro 	ret = wait_for_space(pipe, flags);
131779fddc4eSAl Viro 	if (!ret)
131887a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13198924feffSAl Viro 	pipe_unlock(pipe);
13208924feffSAl Viro 	if (ret > 0)
13218924feffSAl Viro 		wakeup_pipe_readers(pipe);
132235f3d14dSJens Axboe 	return ret;
1323912d35f8SJens Axboe }
1324912d35f8SJens Axboe 
132587a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
132687a3002aSAl Viro {
132787a3002aSAl Viro 	if (!f.file)
132887a3002aSAl Viro 		return -EBADF;
132987a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
133087a3002aSAl Viro 		*type = WRITE;
133187a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
133287a3002aSAl Viro 		*type = READ;
133387a3002aSAl Viro 	} else {
133487a3002aSAl Viro 		fdput(f);
133587a3002aSAl Viro 		return -EBADF;
133687a3002aSAl Viro 	}
133787a3002aSAl Viro 	return 0;
133887a3002aSAl Viro }
133987a3002aSAl Viro 
13406a14b90bSJens Axboe /*
13416a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13426a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13436a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13446a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13456a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13466a14b90bSJens Axboe  * solutions for that:
13476a14b90bSJens Axboe  *
13486a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13496a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13506a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13516a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13526a14b90bSJens Axboe  *
13536a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13546a14b90bSJens Axboe  *
13556a14b90bSJens Axboe  */
135687a3002aSAl Viro static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
1357912d35f8SJens Axboe {
13583d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
13593d6ea290SAl Viro 		return -EINVAL;
136087a3002aSAl Viro 
136187a3002aSAl Viro 	if (!iov_iter_count(iter))
13626a14b90bSJens Axboe 		return 0;
13636a14b90bSJens Axboe 
136487a3002aSAl Viro 	if (iov_iter_rw(iter) == WRITE)
136587a3002aSAl Viro 		return vmsplice_to_pipe(f, iter, flags);
136687a3002aSAl Viro 	else
136787a3002aSAl Viro 		return vmsplice_to_user(f, iter, flags);
1368912d35f8SJens Axboe }
1369912d35f8SJens Axboe 
137087a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
137130cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
137230cfe4efSDominik Brodowski {
137387a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
137487a3002aSAl Viro 	struct iovec *iov = iovstack;
137587a3002aSAl Viro 	struct iov_iter iter;
137687e5e6daSJens Axboe 	ssize_t error;
137787a3002aSAl Viro 	struct fd f;
137887a3002aSAl Viro 	int type;
137987a3002aSAl Viro 
138087a3002aSAl Viro 	f = fdget(fd);
138187a3002aSAl Viro 	error = vmsplice_type(f, &type);
138287a3002aSAl Viro 	if (error)
138387a3002aSAl Viro 		return error;
138487a3002aSAl Viro 
138587a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
138687a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
138787e5e6daSJens Axboe 	if (error >= 0) {
138887a3002aSAl Viro 		error = do_vmsplice(f.file, &iter, flags);
138987a3002aSAl Viro 		kfree(iov);
139087a3002aSAl Viro 	}
139187a3002aSAl Viro 	fdput(f);
139287a3002aSAl Viro 	return error;
139330cfe4efSDominik Brodowski }
139430cfe4efSDominik Brodowski 
139576b021d0SAl Viro #ifdef CONFIG_COMPAT
139676b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
139776b021d0SAl Viro 		    unsigned int, nr_segs, unsigned int, flags)
139876b021d0SAl Viro {
139987a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
140087a3002aSAl Viro 	struct iovec *iov = iovstack;
140187a3002aSAl Viro 	struct iov_iter iter;
140287e5e6daSJens Axboe 	ssize_t error;
140387a3002aSAl Viro 	struct fd f;
140487a3002aSAl Viro 	int type;
140587a3002aSAl Viro 
140687a3002aSAl Viro 	f = fdget(fd);
140787a3002aSAl Viro 	error = vmsplice_type(f, &type);
140887a3002aSAl Viro 	if (error)
140987a3002aSAl Viro 		return error;
141087a3002aSAl Viro 
141187a3002aSAl Viro 	error = compat_import_iovec(type, iov32, nr_segs,
141287a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
141387e5e6daSJens Axboe 	if (error >= 0) {
141487a3002aSAl Viro 		error = do_vmsplice(f.file, &iter, flags);
141587a3002aSAl Viro 		kfree(iov);
141676b021d0SAl Viro 	}
141787a3002aSAl Viro 	fdput(f);
141887a3002aSAl Viro 	return error;
141976b021d0SAl Viro }
142076b021d0SAl Viro #endif
142176b021d0SAl Viro 
1422836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1423836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1424836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14255274f052SJens Axboe {
14262903ff01SAl Viro 	struct fd in, out;
14275274f052SJens Axboe 	long error;
14285274f052SJens Axboe 
14295274f052SJens Axboe 	if (unlikely(!len))
14305274f052SJens Axboe 		return 0;
14315274f052SJens Axboe 
14323d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14333d6ea290SAl Viro 		return -EINVAL;
14343d6ea290SAl Viro 
14355274f052SJens Axboe 	error = -EBADF;
14362903ff01SAl Viro 	in = fdget(fd_in);
14372903ff01SAl Viro 	if (in.file) {
14382903ff01SAl Viro 		out = fdget(fd_out);
14392903ff01SAl Viro 		if (out.file) {
144090da2e3fSPavel Begunkov 			error = do_splice(in.file, off_in, out.file, off_out,
1441529565dcSIngo Molnar 					  len, flags);
14422903ff01SAl Viro 			fdput(out);
14435274f052SJens Axboe 		}
14442903ff01SAl Viro 		fdput(in);
14455274f052SJens Axboe 	}
14465274f052SJens Axboe 	return error;
14475274f052SJens Axboe }
144870524490SJens Axboe 
144970524490SJens Axboe /*
1450aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1451aadd06e5SJens Axboe  * return an appropriate error.
1452aadd06e5SJens Axboe  */
14537c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1454aadd06e5SJens Axboe {
1455aadd06e5SJens Axboe 	int ret;
1456aadd06e5SJens Axboe 
1457aadd06e5SJens Axboe 	/*
14588cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1459aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1460aadd06e5SJens Axboe 	 */
14618cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1462aadd06e5SJens Axboe 		return 0;
1463aadd06e5SJens Axboe 
1464aadd06e5SJens Axboe 	ret = 0;
146561e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1466aadd06e5SJens Axboe 
14678cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1468aadd06e5SJens Axboe 		if (signal_pending(current)) {
1469aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1470aadd06e5SJens Axboe 			break;
1471aadd06e5SJens Axboe 		}
1472aadd06e5SJens Axboe 		if (!pipe->writers)
1473aadd06e5SJens Axboe 			break;
1474aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1475aadd06e5SJens Axboe 			ret = -EAGAIN;
1476aadd06e5SJens Axboe 			break;
1477aadd06e5SJens Axboe 		}
1478aadd06e5SJens Axboe 		pipe_wait(pipe);
1479aadd06e5SJens Axboe 	}
1480aadd06e5SJens Axboe 
148161e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1482aadd06e5SJens Axboe 	return ret;
1483aadd06e5SJens Axboe }
1484aadd06e5SJens Axboe 
1485aadd06e5SJens Axboe /*
1486aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1487aadd06e5SJens Axboe  * return an appropriate error.
1488aadd06e5SJens Axboe  */
14897c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1490aadd06e5SJens Axboe {
1491aadd06e5SJens Axboe 	int ret;
1492aadd06e5SJens Axboe 
1493aadd06e5SJens Axboe 	/*
14948cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1495aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1496aadd06e5SJens Axboe 	 */
14976718b6f8SDavid Howells 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1498aadd06e5SJens Axboe 		return 0;
1499aadd06e5SJens Axboe 
1500aadd06e5SJens Axboe 	ret = 0;
150161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1502aadd06e5SJens Axboe 
15036718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1504aadd06e5SJens Axboe 		if (!pipe->readers) {
1505aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1506aadd06e5SJens Axboe 			ret = -EPIPE;
1507aadd06e5SJens Axboe 			break;
1508aadd06e5SJens Axboe 		}
1509aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1510aadd06e5SJens Axboe 			ret = -EAGAIN;
1511aadd06e5SJens Axboe 			break;
1512aadd06e5SJens Axboe 		}
1513aadd06e5SJens Axboe 		if (signal_pending(current)) {
1514aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1515aadd06e5SJens Axboe 			break;
1516aadd06e5SJens Axboe 		}
1517aadd06e5SJens Axboe 		pipe_wait(pipe);
1518aadd06e5SJens Axboe 	}
1519aadd06e5SJens Axboe 
152061e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1521aadd06e5SJens Axboe 	return ret;
1522aadd06e5SJens Axboe }
1523aadd06e5SJens Axboe 
1524aadd06e5SJens Axboe /*
15257c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15267c77f0b3SMiklos Szeredi  */
15277c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15287c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15297c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15307c77f0b3SMiklos Szeredi {
15317c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15328cefc107SDavid Howells 	unsigned int i_head, o_head;
15338cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15348cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15358cefc107SDavid Howells 	int ret = 0;
15367c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15377c77f0b3SMiklos Szeredi 
15387c77f0b3SMiklos Szeredi 
15397c77f0b3SMiklos Szeredi retry:
15407c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15417c77f0b3SMiklos Szeredi 	if (ret)
15427c77f0b3SMiklos Szeredi 		return ret;
15437c77f0b3SMiklos Szeredi 
15447c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15457c77f0b3SMiklos Szeredi 	if (ret)
15467c77f0b3SMiklos Szeredi 		return ret;
15477c77f0b3SMiklos Szeredi 
15487c77f0b3SMiklos Szeredi 	/*
15497c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15507c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15517c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15527c77f0b3SMiklos Szeredi 	 */
15537c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15547c77f0b3SMiklos Szeredi 
15558cefc107SDavid Howells 	i_tail = ipipe->tail;
15568cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15578cefc107SDavid Howells 	o_head = opipe->head;
15588cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15598cefc107SDavid Howells 
15607c77f0b3SMiklos Szeredi 	do {
15618cefc107SDavid Howells 		size_t o_len;
15628cefc107SDavid Howells 
15637c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15647c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15657c77f0b3SMiklos Szeredi 			if (!ret)
15667c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15677c77f0b3SMiklos Szeredi 			break;
15687c77f0b3SMiklos Szeredi 		}
15697c77f0b3SMiklos Szeredi 
15708cefc107SDavid Howells 		i_head = ipipe->head;
15718cefc107SDavid Howells 		o_tail = opipe->tail;
15728cefc107SDavid Howells 
15738cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
15747c77f0b3SMiklos Szeredi 			break;
15757c77f0b3SMiklos Szeredi 
15767c77f0b3SMiklos Szeredi 		/*
15777c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15787c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15797c77f0b3SMiklos Szeredi 		 */
15808cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
15816718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
15827c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15837c77f0b3SMiklos Szeredi 			if (ret)
15847c77f0b3SMiklos Szeredi 				break;
15857c77f0b3SMiklos Szeredi 
15867c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15877c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15887c77f0b3SMiklos Szeredi 				break;
15897c77f0b3SMiklos Szeredi 			}
15907c77f0b3SMiklos Szeredi 
15917c77f0b3SMiklos Szeredi 			/*
15927c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15937c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15947c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
15957c77f0b3SMiklos Szeredi 			 */
15967c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
15977c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
15987c77f0b3SMiklos Szeredi 			goto retry;
15997c77f0b3SMiklos Szeredi 		}
16007c77f0b3SMiklos Szeredi 
16018cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16028cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
16037c77f0b3SMiklos Szeredi 
16047c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
16057c77f0b3SMiklos Szeredi 			/*
16067c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
16077c77f0b3SMiklos Szeredi 			 */
16087c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16097c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
16108cefc107SDavid Howells 			i_tail++;
16118cefc107SDavid Howells 			ipipe->tail = i_tail;
16127c77f0b3SMiklos Szeredi 			input_wakeup = true;
16138cefc107SDavid Howells 			o_len = obuf->len;
16148cefc107SDavid Howells 			o_head++;
16158cefc107SDavid Howells 			opipe->head = o_head;
16167c77f0b3SMiklos Szeredi 		} else {
16177c77f0b3SMiklos Szeredi 			/*
16187c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16197c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16207c77f0b3SMiklos Szeredi 			 */
162115fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
162215fab63eSMatthew Wilcox 				if (ret == 0)
162315fab63eSMatthew Wilcox 					ret = -EFAULT;
162415fab63eSMatthew Wilcox 				break;
162515fab63eSMatthew Wilcox 			}
16267c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16277c77f0b3SMiklos Szeredi 
16287c77f0b3SMiklos Szeredi 			/*
16297c77f0b3SMiklos Szeredi 			 * Don't inherit the gift flag, we need to
16307c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16317c77f0b3SMiklos Szeredi 			 */
16327c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
16337c77f0b3SMiklos Szeredi 
1634a0ce2f0aSJann Horn 			pipe_buf_mark_unmergeable(obuf);
1635a0ce2f0aSJann Horn 
16367c77f0b3SMiklos Szeredi 			obuf->len = len;
16378cefc107SDavid Howells 			ibuf->offset += len;
16388cefc107SDavid Howells 			ibuf->len -= len;
16398cefc107SDavid Howells 			o_len = len;
16408cefc107SDavid Howells 			o_head++;
16418cefc107SDavid Howells 			opipe->head = o_head;
16427c77f0b3SMiklos Szeredi 		}
16438cefc107SDavid Howells 		ret += o_len;
16448cefc107SDavid Howells 		len -= o_len;
16457c77f0b3SMiklos Szeredi 	} while (len);
16467c77f0b3SMiklos Szeredi 
16477c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16487c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16497c77f0b3SMiklos Szeredi 
16507c77f0b3SMiklos Szeredi 	/*
16517c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16527c77f0b3SMiklos Szeredi 	 */
1653825cdcb1SNamhyung Kim 	if (ret > 0)
1654825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1655825cdcb1SNamhyung Kim 
16567c77f0b3SMiklos Szeredi 	if (input_wakeup)
16577c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16587c77f0b3SMiklos Szeredi 
16597c77f0b3SMiklos Szeredi 	return ret;
16607c77f0b3SMiklos Szeredi }
16617c77f0b3SMiklos Szeredi 
16627c77f0b3SMiklos Szeredi /*
166370524490SJens Axboe  * Link contents of ipipe to opipe.
166470524490SJens Axboe  */
166570524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
166670524490SJens Axboe 		     struct pipe_inode_info *opipe,
166770524490SJens Axboe 		     size_t len, unsigned int flags)
166870524490SJens Axboe {
166970524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
16708cefc107SDavid Howells 	unsigned int i_head, o_head;
16718cefc107SDavid Howells 	unsigned int i_tail, o_tail;
16728cefc107SDavid Howells 	unsigned int i_mask, o_mask;
16738cefc107SDavid Howells 	int ret = 0;
167470524490SJens Axboe 
167570524490SJens Axboe 	/*
167670524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
167761e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
167870524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
167970524490SJens Axboe 	 */
168061e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
168170524490SJens Axboe 
16828cefc107SDavid Howells 	i_tail = ipipe->tail;
16838cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
16848cefc107SDavid Howells 	o_head = opipe->head;
16858cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
16868cefc107SDavid Howells 
1687aadd06e5SJens Axboe 	do {
168870524490SJens Axboe 		if (!opipe->readers) {
168970524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
169070524490SJens Axboe 			if (!ret)
169170524490SJens Axboe 				ret = -EPIPE;
169270524490SJens Axboe 			break;
169370524490SJens Axboe 		}
169470524490SJens Axboe 
16958cefc107SDavid Howells 		i_head = ipipe->head;
16968cefc107SDavid Howells 		o_tail = opipe->tail;
16978cefc107SDavid Howells 
169870524490SJens Axboe 		/*
16998cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1700aadd06e5SJens Axboe 		 * output room, break.
170170524490SJens Axboe 		 */
17028cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17036718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1704aadd06e5SJens Axboe 			break;
1705aadd06e5SJens Axboe 
17068cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17078cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
170870524490SJens Axboe 
170970524490SJens Axboe 		/*
171070524490SJens Axboe 		 * Get a reference to this pipe buffer,
171170524490SJens Axboe 		 * so we can copy the contents over.
171270524490SJens Axboe 		 */
171315fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
171415fab63eSMatthew Wilcox 			if (ret == 0)
171515fab63eSMatthew Wilcox 				ret = -EFAULT;
171615fab63eSMatthew Wilcox 			break;
171715fab63eSMatthew Wilcox 		}
171870524490SJens Axboe 
171970524490SJens Axboe 		*obuf = *ibuf;
172070524490SJens Axboe 
17217afa6fd0SJens Axboe 		/*
17227afa6fd0SJens Axboe 		 * Don't inherit the gift flag, we need to
17237afa6fd0SJens Axboe 		 * prevent multiple steals of this page.
17247afa6fd0SJens Axboe 		 */
17257afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
17267afa6fd0SJens Axboe 
1727a0ce2f0aSJann Horn 		pipe_buf_mark_unmergeable(obuf);
1728a0ce2f0aSJann Horn 
172970524490SJens Axboe 		if (obuf->len > len)
173070524490SJens Axboe 			obuf->len = len;
173170524490SJens Axboe 		ret += obuf->len;
173270524490SJens Axboe 		len -= obuf->len;
17338cefc107SDavid Howells 
17348cefc107SDavid Howells 		o_head++;
17358cefc107SDavid Howells 		opipe->head = o_head;
17368cefc107SDavid Howells 		i_tail++;
1737aadd06e5SJens Axboe 	} while (len);
173870524490SJens Axboe 
173961e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
174061e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
174170524490SJens Axboe 
1742aadd06e5SJens Axboe 	/*
1743aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1744aadd06e5SJens Axboe 	 */
1745825cdcb1SNamhyung Kim 	if (ret > 0)
1746825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
174770524490SJens Axboe 
174870524490SJens Axboe 	return ret;
174970524490SJens Axboe }
175070524490SJens Axboe 
175170524490SJens Axboe /*
175270524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
175370524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
175470524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
175570524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
175670524490SJens Axboe  */
175770524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
175870524490SJens Axboe 		   unsigned int flags)
175970524490SJens Axboe {
176071993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
176171993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1762aadd06e5SJens Axboe 	int ret = -EINVAL;
176370524490SJens Axboe 
176490da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
176590da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
176690da2e3fSPavel Begunkov 		return -EBADF;
176790da2e3fSPavel Begunkov 
176870524490SJens Axboe 	/*
1769aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1770aadd06e5SJens Axboe 	 * copying the data.
177170524490SJens Axboe 	 */
1772aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1773ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1774ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1775ee5e0011SSlavomir Kaslev 
1776aadd06e5SJens Axboe 		/*
1777aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1778aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1779aadd06e5SJens Axboe 		 */
17807c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1781aadd06e5SJens Axboe 		if (!ret) {
17827c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
178302cf01aeSJens Axboe 			if (!ret)
1784aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1785aadd06e5SJens Axboe 		}
1786aadd06e5SJens Axboe 	}
178770524490SJens Axboe 
1788aadd06e5SJens Axboe 	return ret;
178970524490SJens Axboe }
179070524490SJens Axboe 
1791836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
179270524490SJens Axboe {
179390da2e3fSPavel Begunkov 	struct fd in, out;
17942903ff01SAl Viro 	int error;
179570524490SJens Axboe 
17963d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
17973d6ea290SAl Viro 		return -EINVAL;
17983d6ea290SAl Viro 
179970524490SJens Axboe 	if (unlikely(!len))
180070524490SJens Axboe 		return 0;
180170524490SJens Axboe 
180270524490SJens Axboe 	error = -EBADF;
18032903ff01SAl Viro 	in = fdget(fdin);
18042903ff01SAl Viro 	if (in.file) {
180590da2e3fSPavel Begunkov 		out = fdget(fdout);
18062903ff01SAl Viro 		if (out.file) {
180790da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
18082903ff01SAl Viro 			fdput(out);
180970524490SJens Axboe 		}
18102903ff01SAl Viro  		fdput(in);
181170524490SJens Axboe  	}
181270524490SJens Axboe 
181370524490SJens Axboe 	return error;
181470524490SJens Axboe }
1815