xref: /openbmc/linux/fs/splice.c (revision 313d64a3)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25274f052SJens Axboe /*
35274f052SJens Axboe  * "splice": joining two ropes together by interweaving their strands.
45274f052SJens Axboe  *
55274f052SJens Axboe  * This is the "extended pipe" functionality, where a pipe is used as
65274f052SJens Axboe  * an arbitrary in-memory buffer. Think of a pipe as a small kernel
75274f052SJens Axboe  * buffer that you can use to transfer data from one end to the other.
85274f052SJens Axboe  *
95274f052SJens Axboe  * The traditional unix read/write is extended with a "splice()" operation
105274f052SJens Axboe  * that transfers data buffers to or from a pipe buffer.
115274f052SJens Axboe  *
125274f052SJens Axboe  * Named by Larry McVoy, original implementation from Linus, extended by
13c2058e06SJens Axboe  * Jens to support splicing to files, network, direct splicing, etc and
14c2058e06SJens Axboe  * fixing lots of bugs.
155274f052SJens Axboe  *
160fe23479SJens Axboe  * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
17c2058e06SJens Axboe  * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18c2058e06SJens Axboe  * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
195274f052SJens Axboe  *
205274f052SJens Axboe  */
21be297968SChristoph Hellwig #include <linux/bvec.h>
225274f052SJens Axboe #include <linux/fs.h>
235274f052SJens Axboe #include <linux/file.h>
245274f052SJens Axboe #include <linux/pagemap.h>
25d6b29d7cSJens Axboe #include <linux/splice.h>
2608e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h>
275274f052SJens Axboe #include <linux/mm_inline.h>
285abc97aaSJens Axboe #include <linux/swap.h>
294f6f0bd2SJens Axboe #include <linux/writeback.h>
30630d9c47SPaul Gortmaker #include <linux/export.h>
314f6f0bd2SJens Axboe #include <linux/syscalls.h>
32912d35f8SJens Axboe #include <linux/uio.h>
3329ce2058SJames Morris #include <linux/security.h>
345a0e3ad6STejun Heo #include <linux/gfp.h>
3535f9c09fSEric Dumazet #include <linux/socket.h>
36174cd4b1SIngo Molnar #include <linux/sched/signal.h>
37174cd4b1SIngo Molnar 
3806ae43f3SAl Viro #include "internal.h"
395274f052SJens Axboe 
4083f9135bSJens Axboe /*
4183f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
4283f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
4383f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
4483f9135bSJens Axboe  * attempt to reuse this page for another destination.
4583f9135bSJens Axboe  */
46c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
475abc97aaSJens Axboe 		struct pipe_buffer *buf)
485abc97aaSJens Axboe {
495abc97aaSJens Axboe 	struct page *page = buf->page;
509e94cd4fSJens Axboe 	struct address_space *mapping;
515abc97aaSJens Axboe 
529e0267c2SJens Axboe 	lock_page(page);
539e0267c2SJens Axboe 
549e94cd4fSJens Axboe 	mapping = page_mapping(page);
559e94cd4fSJens Axboe 	if (mapping) {
565abc97aaSJens Axboe 		WARN_ON(!PageUptodate(page));
575abc97aaSJens Axboe 
58ad8d6f0aSJens Axboe 		/*
599e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
609e94cd4fSJens Axboe 		 * writeback completing on this page, since we'll remove it
619e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
629e94cd4fSJens Axboe 		 * page, allowing the disk blocks to be reused by someone else
639e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
649e94cd4fSJens Axboe 		 * ensues.
65ad8d6f0aSJens Axboe 		 */
66ad8d6f0aSJens Axboe 		wait_on_page_writeback(page);
67ad8d6f0aSJens Axboe 
68266cf658SDavid Howells 		if (page_has_private(page) &&
69266cf658SDavid Howells 		    !try_to_release_page(page, GFP_KERNEL))
70ca39d651SJens Axboe 			goto out_unlock;
714f6f0bd2SJens Axboe 
729e94cd4fSJens Axboe 		/*
739e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
749e94cd4fSJens Axboe 		 * and return good.
759e94cd4fSJens Axboe 		 */
769e94cd4fSJens Axboe 		if (remove_mapping(mapping, page)) {
771432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
78c928f642SChristoph Hellwig 			return true;
795abc97aaSJens Axboe 		}
809e94cd4fSJens Axboe 	}
819e94cd4fSJens Axboe 
829e94cd4fSJens Axboe 	/*
839e94cd4fSJens Axboe 	 * Raced with truncate or failed to remove page from current
849e94cd4fSJens Axboe 	 * address space, unlock and return failure.
859e94cd4fSJens Axboe 	 */
86ca39d651SJens Axboe out_unlock:
879e94cd4fSJens Axboe 	unlock_page(page);
88c928f642SChristoph Hellwig 	return false;
899e94cd4fSJens Axboe }
905abc97aaSJens Axboe 
9176ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
925274f052SJens Axboe 					struct pipe_buffer *buf)
935274f052SJens Axboe {
9409cbfeafSKirill A. Shutemov 	put_page(buf->page);
951432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
965274f052SJens Axboe }
975274f052SJens Axboe 
980845718dSJens Axboe /*
990845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
1000845718dSJens Axboe  * is a page cache page, IO may be in flight.
1010845718dSJens Axboe  */
102cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1035274f052SJens Axboe 				       struct pipe_buffer *buf)
1045274f052SJens Axboe {
1055274f052SJens Axboe 	struct page *page = buf->page;
10649d0b21bSJens Axboe 	int err;
1075274f052SJens Axboe 
1085274f052SJens Axboe 	if (!PageUptodate(page)) {
10949d0b21bSJens Axboe 		lock_page(page);
1105274f052SJens Axboe 
11149d0b21bSJens Axboe 		/*
11249d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
11373d62d83SIngo Molnar 		 * splice, if this is the first page.
11449d0b21bSJens Axboe 		 */
1155274f052SJens Axboe 		if (!page->mapping) {
11649d0b21bSJens Axboe 			err = -ENODATA;
11749d0b21bSJens Axboe 			goto error;
1185274f052SJens Axboe 		}
1195274f052SJens Axboe 
12049d0b21bSJens Axboe 		/*
12173d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
12249d0b21bSJens Axboe 		 */
12349d0b21bSJens Axboe 		if (!PageUptodate(page)) {
12449d0b21bSJens Axboe 			err = -EIO;
12549d0b21bSJens Axboe 			goto error;
12649d0b21bSJens Axboe 		}
12749d0b21bSJens Axboe 
12849d0b21bSJens Axboe 		/*
129f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
13049d0b21bSJens Axboe 		 */
13149d0b21bSJens Axboe 		unlock_page(page);
13249d0b21bSJens Axboe 	}
13349d0b21bSJens Axboe 
134f84d7519SJens Axboe 	return 0;
13549d0b21bSJens Axboe error:
13649d0b21bSJens Axboe 	unlock_page(page);
137f84d7519SJens Axboe 	return err;
13870524490SJens Axboe }
13970524490SJens Axboe 
140708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
141cac36bb0SJens Axboe 	.confirm	= page_cache_pipe_buf_confirm,
1425274f052SJens Axboe 	.release	= page_cache_pipe_buf_release,
143c928f642SChristoph Hellwig 	.try_steal	= page_cache_pipe_buf_try_steal,
144f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
1455274f052SJens Axboe };
1465274f052SJens Axboe 
147c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
148912d35f8SJens Axboe 		struct pipe_buffer *buf)
149912d35f8SJens Axboe {
1507afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
151c928f642SChristoph Hellwig 		return false;
1527afa6fd0SJens Axboe 
1531432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
154c928f642SChristoph Hellwig 	return generic_pipe_buf_try_steal(pipe, buf);
155912d35f8SJens Axboe }
156912d35f8SJens Axboe 
157d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
158912d35f8SJens Axboe 	.release	= page_cache_pipe_buf_release,
159c928f642SChristoph Hellwig 	.try_steal	= user_page_pipe_buf_try_steal,
160f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
161912d35f8SJens Axboe };
162912d35f8SJens Axboe 
163825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
164825cdcb1SNamhyung Kim {
165825cdcb1SNamhyung Kim 	smp_mb();
1660ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1670ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_wait);
168825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
169825cdcb1SNamhyung Kim }
170825cdcb1SNamhyung Kim 
171932cc6d4SJens Axboe /**
172932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
173932cc6d4SJens Axboe  * @pipe:	pipe to fill
174932cc6d4SJens Axboe  * @spd:	data to fill
175932cc6d4SJens Axboe  *
176932cc6d4SJens Axboe  * Description:
17779685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
178932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
179932cc6d4SJens Axboe  *    function will link that data to the pipe.
180932cc6d4SJens Axboe  *
18183f9135bSJens Axboe  */
182d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1845274f052SJens Axboe {
18500de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
1868cefc107SDavid Howells 	unsigned int tail = pipe->tail;
1878cefc107SDavid Howells 	unsigned int head = pipe->head;
1888cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
1898924feffSAl Viro 	int ret = 0, page_nr = 0;
1905274f052SJens Axboe 
191d6785d91SRabin Vincent 	if (!spd_pages)
192d6785d91SRabin Vincent 		return 0;
193d6785d91SRabin Vincent 
1948924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1955274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1965274f052SJens Axboe 		ret = -EPIPE;
1978924feffSAl Viro 		goto out;
1985274f052SJens Axboe 	}
1995274f052SJens Axboe 
2006718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2018cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
2025274f052SJens Axboe 
203912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
204912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
205912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
206497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
207912d35f8SJens Axboe 		buf->ops = spd->ops;
2085a81e6a1SMiklos Szeredi 		buf->flags = 0;
2097afa6fd0SJens Axboe 
2108cefc107SDavid Howells 		head++;
2118cefc107SDavid Howells 		pipe->head = head;
212912d35f8SJens Axboe 		page_nr++;
213912d35f8SJens Axboe 		ret += buf->len;
214912d35f8SJens Axboe 
215912d35f8SJens Axboe 		if (!--spd->nr_pages)
2165274f052SJens Axboe 			break;
2175274f052SJens Axboe 	}
2185274f052SJens Axboe 
21929e35094SLinus Torvalds 	if (!ret)
22029e35094SLinus Torvalds 		ret = -EAGAIN;
22129e35094SLinus Torvalds 
2228924feffSAl Viro out:
22300de00bdSJens Axboe 	while (page_nr < spd_pages)
224bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2255274f052SJens Axboe 
2265274f052SJens Axboe 	return ret;
2275274f052SJens Axboe }
2282b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2295274f052SJens Axboe 
23079fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
23179fddc4eSAl Viro {
2328cefc107SDavid Howells 	unsigned int head = pipe->head;
2338cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2348cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
23579fddc4eSAl Viro 	int ret;
23679fddc4eSAl Viro 
23779fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23879fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
23979fddc4eSAl Viro 		ret = -EPIPE;
2406718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
24179fddc4eSAl Viro 		ret = -EAGAIN;
24279fddc4eSAl Viro 	} else {
2438cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2448cefc107SDavid Howells 		pipe->head = head + 1;
24579fddc4eSAl Viro 		return buf->len;
24679fddc4eSAl Viro 	}
247a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24879fddc4eSAl Viro 	return ret;
24979fddc4eSAl Viro }
25079fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
25179fddc4eSAl Viro 
25235f3d14dSJens Axboe /*
25335f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25435f3d14dSJens Axboe  * descriptions.
25535f3d14dSJens Axboe  */
256047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25735f3d14dSJens Axboe {
2586718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
259047fe360SEric Dumazet 
2608cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2618cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
26235f3d14dSJens Axboe 		return 0;
26335f3d14dSJens Axboe 
2648cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2658cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2666da2ec56SKees Cook 				     GFP_KERNEL);
26735f3d14dSJens Axboe 
26835f3d14dSJens Axboe 	if (spd->pages && spd->partial)
26935f3d14dSJens Axboe 		return 0;
27035f3d14dSJens Axboe 
27135f3d14dSJens Axboe 	kfree(spd->pages);
27235f3d14dSJens Axboe 	kfree(spd->partial);
27335f3d14dSJens Axboe 	return -ENOMEM;
27435f3d14dSJens Axboe }
27535f3d14dSJens Axboe 
276047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27735f3d14dSJens Axboe {
278047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
27935f3d14dSJens Axboe 		return;
28035f3d14dSJens Axboe 
28135f3d14dSJens Axboe 	kfree(spd->pages);
28235f3d14dSJens Axboe 	kfree(spd->partial);
28335f3d14dSJens Axboe }
28435f3d14dSJens Axboe 
28583f9135bSJens Axboe /**
28683f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
28783f9135bSJens Axboe  * @in:		file to splice from
288932cc6d4SJens Axboe  * @ppos:	position in @in
28983f9135bSJens Axboe  * @pipe:	pipe to splice to
29083f9135bSJens Axboe  * @len:	number of bytes to splice
29183f9135bSJens Axboe  * @flags:	splice modifier flags
29283f9135bSJens Axboe  *
293932cc6d4SJens Axboe  * Description:
294932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
29582c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
296932cc6d4SJens Axboe  *
29783f9135bSJens Axboe  */
298cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
299cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
300cbb7e577SJens Axboe 				 unsigned int flags)
3015274f052SJens Axboe {
30282c156f8SAl Viro 	struct iov_iter to;
30382c156f8SAl Viro 	struct kiocb kiocb;
3048cefc107SDavid Howells 	unsigned int i_head;
3058cefc107SDavid Howells 	int ret;
306be64f884SBoaz Harrosh 
307aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len);
3088cefc107SDavid Howells 	i_head = to.head;
30982c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
31082c156f8SAl Viro 	kiocb.ki_pos = *ppos;
311bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
312723590edSMiklos Szeredi 	if (ret > 0) {
31382c156f8SAl Viro 		*ppos = kiocb.ki_pos;
314723590edSMiklos Szeredi 		file_accessed(in);
31582c156f8SAl Viro 	} else if (ret < 0) {
3168cefc107SDavid Howells 		to.head = i_head;
31782c156f8SAl Viro 		to.iov_offset = 0;
31882c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
31982c156f8SAl Viro 		/*
32082c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
32182c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
32282c156f8SAl Viro 		 */
32382c156f8SAl Viro 		if (ret == -EFAULT)
32482c156f8SAl Viro 			ret = -EAGAIN;
325723590edSMiklos Szeredi 	}
3265274f052SJens Axboe 
3275274f052SJens Axboe 	return ret;
3285274f052SJens Axboe }
329059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
330059a8f37SJens Axboe 
331241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3326818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
333c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
3346818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
3356818173bSMiklos Szeredi };
3366818173bSMiklos Szeredi 
33728a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
33828a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
33928a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
34028a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
34128a625cbSMiklos Szeredi };
34228a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
34328a625cbSMiklos Szeredi 
3445274f052SJens Axboe /*
3454f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
346016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
3475274f052SJens Axboe  */
34876ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
3495274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
3505274f052SJens Axboe {
3516a14b90bSJens Axboe 	struct file *file = sd->u.file;
3525274f052SJens Axboe 	loff_t pos = sd->pos;
353a8adbe37SMichał Mirosław 	int more;
3545274f052SJens Axboe 
35572c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
356a8adbe37SMichał Mirosław 		return -EINVAL;
357a8adbe37SMichał Mirosław 
35835f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
359ae62ca7bSEric Dumazet 
3608cefc107SDavid Howells 	if (sd->len < sd->total_len &&
3618cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
36235f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
363ae62ca7bSEric Dumazet 
364a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
365f84d7519SJens Axboe 				    sd->len, &pos, more);
3665274f052SJens Axboe }
3675274f052SJens Axboe 
368b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
369b3c2d2ddSMiklos Szeredi {
370b3c2d2ddSMiklos Szeredi 	smp_mb();
3710ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
3720ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
373b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
374b3c2d2ddSMiklos Szeredi }
375b3c2d2ddSMiklos Szeredi 
376b3c2d2ddSMiklos Szeredi /**
377b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
378b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
379b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
380b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
381b3c2d2ddSMiklos Szeredi  *
382b3c2d2ddSMiklos Szeredi  * Description:
383b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
384b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
385b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
386b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
387b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
388b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
389b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
390b3c2d2ddSMiklos Szeredi  *
391b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
392b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
393b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
394b3c2d2ddSMiklos Szeredi  *    destination.
395b3c2d2ddSMiklos Szeredi  */
39696f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
397b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
398b3c2d2ddSMiklos Szeredi {
3998cefc107SDavid Howells 	unsigned int head = pipe->head;
4008cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4018cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
402b3c2d2ddSMiklos Szeredi 	int ret;
403b3c2d2ddSMiklos Szeredi 
404ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4058cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
406b3c2d2ddSMiklos Szeredi 
407b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
408b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
409b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
410b3c2d2ddSMiklos Szeredi 
411fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
412a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
413b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
414b3c2d2ddSMiklos Szeredi 				ret = 0;
415b3c2d2ddSMiklos Szeredi 			return ret;
416b3c2d2ddSMiklos Szeredi 		}
417a8adbe37SMichał Mirosław 
418a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
419a8adbe37SMichał Mirosław 		if (ret <= 0)
420a8adbe37SMichał Mirosław 			return ret;
421a8adbe37SMichał Mirosław 
422b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
423b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
424b3c2d2ddSMiklos Szeredi 
425b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
426b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
427b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
428b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
429b3c2d2ddSMiklos Szeredi 
430b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
431a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
4328cefc107SDavid Howells 			tail++;
4338cefc107SDavid Howells 			pipe->tail = tail;
4346447a3cfSAl Viro 			if (pipe->files)
435b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
436b3c2d2ddSMiklos Szeredi 		}
437b3c2d2ddSMiklos Szeredi 
438b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
439b3c2d2ddSMiklos Szeredi 			return 0;
440b3c2d2ddSMiklos Szeredi 	}
441b3c2d2ddSMiklos Szeredi 
442b3c2d2ddSMiklos Szeredi 	return 1;
443b3c2d2ddSMiklos Szeredi }
444b3c2d2ddSMiklos Szeredi 
445d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
446d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
447d1a819a2SLinus Torvalds {
448d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
449d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
450d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
451d1a819a2SLinus Torvalds 
452d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
453d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
454d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
455d1a819a2SLinus Torvalds 		return true;
456d1a819a2SLinus Torvalds 	}
457d1a819a2SLinus Torvalds 
458d1a819a2SLinus Torvalds 	return false;
459d1a819a2SLinus Torvalds }
460d1a819a2SLinus Torvalds 
461b3c2d2ddSMiklos Szeredi /**
462b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
463b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
464b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
465b3c2d2ddSMiklos Szeredi  *
466b3c2d2ddSMiklos Szeredi  * Description:
467b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
468b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
469b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
470b3c2d2ddSMiklos Szeredi  */
47196f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
472b3c2d2ddSMiklos Szeredi {
473c725bfceSJan Kara 	/*
474c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
475c725bfceSJan Kara 	 * always buffers available
476c725bfceSJan Kara 	 */
477c725bfceSJan Kara 	if (signal_pending(current))
478c725bfceSJan Kara 		return -ERESTARTSYS;
479c725bfceSJan Kara 
480d1a819a2SLinus Torvalds repeat:
4818cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
482b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
483b3c2d2ddSMiklos Szeredi 			return 0;
484b3c2d2ddSMiklos Szeredi 
485a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
486b3c2d2ddSMiklos Szeredi 			return 0;
487b3c2d2ddSMiklos Szeredi 
488b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
489b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
490b3c2d2ddSMiklos Szeredi 
491b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
492b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
493b3c2d2ddSMiklos Szeredi 
494b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
495b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
496b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
497b3c2d2ddSMiklos Szeredi 		}
498b3c2d2ddSMiklos Szeredi 
499472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
500b3c2d2ddSMiklos Szeredi 	}
501b3c2d2ddSMiklos Szeredi 
502d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
503d1a819a2SLinus Torvalds 		goto repeat;
504d1a819a2SLinus Torvalds 
505b3c2d2ddSMiklos Szeredi 	return 1;
506b3c2d2ddSMiklos Szeredi }
507b3c2d2ddSMiklos Szeredi 
508b3c2d2ddSMiklos Szeredi /**
509b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
510b80901bbSRandy Dunlap  * @sd:		information about the splice operation
511b3c2d2ddSMiklos Szeredi  *
512b3c2d2ddSMiklos Szeredi  * Description:
513b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
514b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
515b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
516b3c2d2ddSMiklos Szeredi  */
51796f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
518b3c2d2ddSMiklos Szeredi {
519b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
520b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
521b3c2d2ddSMiklos Szeredi }
522b3c2d2ddSMiklos Szeredi 
523b3c2d2ddSMiklos Szeredi /**
524b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
525b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
526b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
527b3c2d2ddSMiklos Szeredi  *
528b3c2d2ddSMiklos Szeredi  * Description:
529b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
530b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
531b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
532b3c2d2ddSMiklos Szeredi  */
53396f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
534b3c2d2ddSMiklos Szeredi {
535b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
536b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
537b3c2d2ddSMiklos Szeredi }
538b3c2d2ddSMiklos Szeredi 
539932cc6d4SJens Axboe /**
540932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
541932cc6d4SJens Axboe  * @pipe:	pipe to splice from
542932cc6d4SJens Axboe  * @sd:		information to @actor
543932cc6d4SJens Axboe  * @actor:	handler that splices the data
544932cc6d4SJens Axboe  *
545932cc6d4SJens Axboe  * Description:
546932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
547932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
548932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
549932cc6d4SJens Axboe  *    pipe_to_user.
550932cc6d4SJens Axboe  *
55183f9135bSJens Axboe  */
552c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
553c66ab6faSJens Axboe 			   splice_actor *actor)
5545274f052SJens Axboe {
555b3c2d2ddSMiklos Szeredi 	int ret;
5565274f052SJens Axboe 
557b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
558b3c2d2ddSMiklos Szeredi 	do {
559c2489e07SJan Kara 		cond_resched();
560b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
561b3c2d2ddSMiklos Szeredi 		if (ret > 0)
562b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
563b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
564b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
5655274f052SJens Axboe 
566b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
5675274f052SJens Axboe }
56840bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
5695274f052SJens Axboe 
570932cc6d4SJens Axboe /**
571932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
572932cc6d4SJens Axboe  * @pipe:	pipe to splice from
573932cc6d4SJens Axboe  * @out:	file to splice to
574932cc6d4SJens Axboe  * @ppos:	position in @out
575932cc6d4SJens Axboe  * @len:	how many bytes to splice
576932cc6d4SJens Axboe  * @flags:	splice modifier flags
577932cc6d4SJens Axboe  * @actor:	handler that splices the data
578932cc6d4SJens Axboe  *
579932cc6d4SJens Axboe  * Description:
5802933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
581932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
582932cc6d4SJens Axboe  *
583932cc6d4SJens Axboe  */
5846da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
5856da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
5866da61809SMark Fasheh 			 splice_actor *actor)
5876da61809SMark Fasheh {
5886da61809SMark Fasheh 	ssize_t ret;
589c66ab6faSJens Axboe 	struct splice_desc sd = {
590c66ab6faSJens Axboe 		.total_len = len,
591c66ab6faSJens Axboe 		.flags = flags,
592c66ab6faSJens Axboe 		.pos = *ppos,
5936a14b90bSJens Axboe 		.u.file = out,
594c66ab6faSJens Axboe 	};
5956da61809SMark Fasheh 
59661e0d47cSMiklos Szeredi 	pipe_lock(pipe);
597c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
59861e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
5996da61809SMark Fasheh 
6006da61809SMark Fasheh 	return ret;
6016da61809SMark Fasheh }
6026da61809SMark Fasheh 
6036da61809SMark Fasheh /**
6048d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6058d020765SAl Viro  * @pipe:	pipe info
6068d020765SAl Viro  * @out:	file to write to
6078d020765SAl Viro  * @ppos:	position in @out
6088d020765SAl Viro  * @len:	number of bytes to splice
6098d020765SAl Viro  * @flags:	splice modifier flags
6108d020765SAl Viro  *
6118d020765SAl Viro  * Description:
6128d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6138d020765SAl Viro  *    the given pipe inode to the given file.
6148d020765SAl Viro  *    This one is ->write_iter-based.
6158d020765SAl Viro  *
6168d020765SAl Viro  */
6178d020765SAl Viro ssize_t
6188d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6198d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
6208d020765SAl Viro {
6218d020765SAl Viro 	struct splice_desc sd = {
6228d020765SAl Viro 		.total_len = len,
6238d020765SAl Viro 		.flags = flags,
6248d020765SAl Viro 		.pos = *ppos,
6258d020765SAl Viro 		.u.file = out,
6268d020765SAl Viro 	};
6276718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
6288d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
6298d020765SAl Viro 					GFP_KERNEL);
6308d020765SAl Viro 	ssize_t ret;
6318d020765SAl Viro 
6328d020765SAl Viro 	if (unlikely(!array))
6338d020765SAl Viro 		return -ENOMEM;
6348d020765SAl Viro 
6358d020765SAl Viro 	pipe_lock(pipe);
6368d020765SAl Viro 
6378d020765SAl Viro 	splice_from_pipe_begin(&sd);
6388d020765SAl Viro 	while (sd.total_len) {
6398d020765SAl Viro 		struct iov_iter from;
640ec057595SLinus Torvalds 		unsigned int head, tail, mask;
6418d020765SAl Viro 		size_t left;
6428cefc107SDavid Howells 		int n;
6438d020765SAl Viro 
6448d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
6458d020765SAl Viro 		if (ret <= 0)
6468d020765SAl Viro 			break;
6478d020765SAl Viro 
6486718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
6498d020765SAl Viro 			kfree(array);
6506718b6f8SDavid Howells 			nbufs = pipe->max_usage;
6518d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
6528d020765SAl Viro 					GFP_KERNEL);
6538d020765SAl Viro 			if (!array) {
6548d020765SAl Viro 				ret = -ENOMEM;
6558d020765SAl Viro 				break;
6568d020765SAl Viro 			}
6578d020765SAl Viro 		}
6588d020765SAl Viro 
659ec057595SLinus Torvalds 		head = pipe->head;
660ec057595SLinus Torvalds 		tail = pipe->tail;
661ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
662ec057595SLinus Torvalds 
6638d020765SAl Viro 		/* build the vector */
6648d020765SAl Viro 		left = sd.total_len;
6658cefc107SDavid Howells 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
6668cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
6678d020765SAl Viro 			size_t this_len = buf->len;
6688d020765SAl Viro 
6698d020765SAl Viro 			if (this_len > left)
6708d020765SAl Viro 				this_len = left;
6718d020765SAl Viro 
672fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
6738d020765SAl Viro 			if (unlikely(ret)) {
6748d020765SAl Viro 				if (ret == -ENODATA)
6758d020765SAl Viro 					ret = 0;
6768d020765SAl Viro 				goto done;
6778d020765SAl Viro 			}
6788d020765SAl Viro 
6798d020765SAl Viro 			array[n].bv_page = buf->page;
6808d020765SAl Viro 			array[n].bv_len = this_len;
6818d020765SAl Viro 			array[n].bv_offset = buf->offset;
6828d020765SAl Viro 			left -= this_len;
6838d020765SAl Viro 		}
6848d020765SAl Viro 
685aa563d7bSDavid Howells 		iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
686abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
6878d020765SAl Viro 		if (ret <= 0)
6888d020765SAl Viro 			break;
6898d020765SAl Viro 
6908d020765SAl Viro 		sd.num_spliced += ret;
6918d020765SAl Viro 		sd.total_len -= ret;
692dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
6938d020765SAl Viro 
6948d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
6958cefc107SDavid Howells 		tail = pipe->tail;
6968d020765SAl Viro 		while (ret) {
6978cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
6988d020765SAl Viro 			if (ret >= buf->len) {
6998d020765SAl Viro 				ret -= buf->len;
7008d020765SAl Viro 				buf->len = 0;
701a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7028cefc107SDavid Howells 				tail++;
7038cefc107SDavid Howells 				pipe->tail = tail;
7048d020765SAl Viro 				if (pipe->files)
7058d020765SAl Viro 					sd.need_wakeup = true;
7068d020765SAl Viro 			} else {
7078d020765SAl Viro 				buf->offset += ret;
7088d020765SAl Viro 				buf->len -= ret;
7098d020765SAl Viro 				ret = 0;
7108d020765SAl Viro 			}
7118d020765SAl Viro 		}
7128d020765SAl Viro 	}
7138d020765SAl Viro done:
7148d020765SAl Viro 	kfree(array);
7158d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7168d020765SAl Viro 
7178d020765SAl Viro 	pipe_unlock(pipe);
7188d020765SAl Viro 
7198d020765SAl Viro 	if (sd.num_spliced)
7208d020765SAl Viro 		ret = sd.num_spliced;
7218d020765SAl Viro 
7228d020765SAl Viro 	return ret;
7238d020765SAl Viro }
7248d020765SAl Viro 
7258d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
7268d020765SAl Viro 
72783f9135bSJens Axboe /**
72883f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
729932cc6d4SJens Axboe  * @pipe:	pipe to splice from
73083f9135bSJens Axboe  * @out:	socket to write to
731932cc6d4SJens Axboe  * @ppos:	position in @out
73283f9135bSJens Axboe  * @len:	number of bytes to splice
73383f9135bSJens Axboe  * @flags:	splice modifier flags
73483f9135bSJens Axboe  *
735932cc6d4SJens Axboe  * Description:
73683f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
73783f9135bSJens Axboe  *    is involved.
73883f9135bSJens Axboe  *
73983f9135bSJens Axboe  */
7403a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
741cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
7425274f052SJens Axboe {
74300522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
7445274f052SJens Axboe }
7455274f052SJens Axboe 
746059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
747a0f06780SJeff Garzik 
74836e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op)
74936e2c742SChristoph Hellwig {
75036e2c742SChristoph Hellwig 	pr_debug_ratelimited(
75136e2c742SChristoph Hellwig 		"splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
75236e2c742SChristoph Hellwig 		op, file, current->pid, current->comm);
75336e2c742SChristoph Hellwig 	return -EINVAL;
75436e2c742SChristoph Hellwig }
75536e2c742SChristoph Hellwig 
75683f9135bSJens Axboe /*
75783f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
75883f9135bSJens Axboe  */
7593a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
760cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
7615274f052SJens Axboe {
76236e2c742SChristoph Hellwig 	if (unlikely(!out->f_op->splice_write))
76336e2c742SChristoph Hellwig 		return warn_unsupported(out, "write");
76400c285d0SChristoph Hellwig 	return out->f_op->splice_write(pipe, out, ppos, len, flags);
7655274f052SJens Axboe }
7665274f052SJens Axboe 
76783f9135bSJens Axboe /*
76883f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
76983f9135bSJens Axboe  */
770cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
771cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
772cbb7e577SJens Axboe 			 unsigned int flags)
7735274f052SJens Axboe {
774*313d64a3SAl Viro 	unsigned int p_space;
7755274f052SJens Axboe 	int ret;
7765274f052SJens Axboe 
77749570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
7785274f052SJens Axboe 		return -EBADF;
7795274f052SJens Axboe 
780*313d64a3SAl Viro 	/* Don't try to read more the pipe has space for. */
781*313d64a3SAl Viro 	p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
782*313d64a3SAl Viro 	len = min_t(size_t, len, p_space << PAGE_SHIFT);
783*313d64a3SAl Viro 
784cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
7855274f052SJens Axboe 	if (unlikely(ret < 0))
7865274f052SJens Axboe 		return ret;
7875274f052SJens Axboe 
78803cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
78903cc0789SAl Viro 		len = MAX_RW_COUNT;
79003cc0789SAl Viro 
79136e2c742SChristoph Hellwig 	if (unlikely(!in->f_op->splice_read))
79236e2c742SChristoph Hellwig 		return warn_unsupported(in, "read");
7932bc01060SChristoph Hellwig 	return in->f_op->splice_read(in, ppos, pipe, len, flags);
7945274f052SJens Axboe }
7955274f052SJens Axboe 
796932cc6d4SJens Axboe /**
797932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
798932cc6d4SJens Axboe  * @in:		file to splice from
799932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
800932cc6d4SJens Axboe  * @actor:	handles the data splicing
801932cc6d4SJens Axboe  *
802932cc6d4SJens Axboe  * Description:
803932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
804932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
805932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
806932cc6d4SJens Axboe  *    that process.
807932cc6d4SJens Axboe  *
808c66ab6faSJens Axboe  */
809c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
810c66ab6faSJens Axboe 			       splice_direct_actor *actor)
811b92ce558SJens Axboe {
812b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
813b92ce558SJens Axboe 	long ret, bytes;
814b92ce558SJens Axboe 	umode_t i_mode;
815c66ab6faSJens Axboe 	size_t len;
8160ff28d9fSChristophe Leroy 	int i, flags, more;
817b92ce558SJens Axboe 
818b92ce558SJens Axboe 	/*
819b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
820b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
821b92ce558SJens Axboe 	 * piped splicing for that!
822b92ce558SJens Axboe 	 */
823496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
824b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
825b92ce558SJens Axboe 		return -EINVAL;
826b92ce558SJens Axboe 
827b92ce558SJens Axboe 	/*
828b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
829b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
830b92ce558SJens Axboe 	 */
831b92ce558SJens Axboe 	pipe = current->splice_pipe;
83249570e9bSJens Axboe 	if (unlikely(!pipe)) {
8337bee130eSAl Viro 		pipe = alloc_pipe_info();
834b92ce558SJens Axboe 		if (!pipe)
835b92ce558SJens Axboe 			return -ENOMEM;
836b92ce558SJens Axboe 
837b92ce558SJens Axboe 		/*
838b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
83900522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
840b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
841b92ce558SJens Axboe 		 */
842b92ce558SJens Axboe 		pipe->readers = 1;
843b92ce558SJens Axboe 
844b92ce558SJens Axboe 		current->splice_pipe = pipe;
845b92ce558SJens Axboe 	}
846b92ce558SJens Axboe 
847b92ce558SJens Axboe 	/*
84873d62d83SIngo Molnar 	 * Do the splice.
849b92ce558SJens Axboe 	 */
850b92ce558SJens Axboe 	ret = 0;
851b92ce558SJens Axboe 	bytes = 0;
852c66ab6faSJens Axboe 	len = sd->total_len;
853c66ab6faSJens Axboe 	flags = sd->flags;
854c66ab6faSJens Axboe 
855c66ab6faSJens Axboe 	/*
856c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
857c66ab6faSJens Axboe 	 */
858c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
8590ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
860b92ce558SJens Axboe 
8618cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
86217614445SDarrick J. Wong 
863b92ce558SJens Axboe 	while (len) {
86451a92c0fSJens Axboe 		size_t read_len;
865a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
866b92ce558SJens Axboe 
867*313d64a3SAl Viro 		ret = do_splice_to(in, &pos, pipe, len, flags);
86851a92c0fSJens Axboe 		if (unlikely(ret <= 0))
869b92ce558SJens Axboe 			goto out_release;
870b92ce558SJens Axboe 
871b92ce558SJens Axboe 		read_len = ret;
872c66ab6faSJens Axboe 		sd->total_len = read_len;
873b92ce558SJens Axboe 
874b92ce558SJens Axboe 		/*
8750ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
8760ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
8770ff28d9fSChristophe Leroy 		 * initially, clears it.
8780ff28d9fSChristophe Leroy 		 */
8790ff28d9fSChristophe Leroy 		if (read_len < len)
8800ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
8810ff28d9fSChristophe Leroy 		else if (!more)
8820ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
8830ff28d9fSChristophe Leroy 		/*
884b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
885b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
886b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
887b92ce558SJens Axboe 		 */
888c66ab6faSJens Axboe 		ret = actor(pipe, sd);
889a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
890a82c53a0STom Zanussi 			sd->pos = prev_pos;
891b92ce558SJens Axboe 			goto out_release;
892a82c53a0STom Zanussi 		}
893b92ce558SJens Axboe 
894b92ce558SJens Axboe 		bytes += ret;
895b92ce558SJens Axboe 		len -= ret;
896bcd4f3acSJens Axboe 		sd->pos = pos;
897b92ce558SJens Axboe 
898a82c53a0STom Zanussi 		if (ret < read_len) {
899a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
90051a92c0fSJens Axboe 			goto out_release;
901b92ce558SJens Axboe 		}
902a82c53a0STom Zanussi 	}
903b92ce558SJens Axboe 
9049e97198dSJens Axboe done:
9058cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
9069e97198dSJens Axboe 	file_accessed(in);
907b92ce558SJens Axboe 	return bytes;
908b92ce558SJens Axboe 
909b92ce558SJens Axboe out_release:
910b92ce558SJens Axboe 	/*
911b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
912b92ce558SJens Axboe 	 * the pipe buffers in question:
913b92ce558SJens Axboe 	 */
9148cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
9158cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
916b92ce558SJens Axboe 
917a779638cSMiklos Szeredi 		if (buf->ops)
918a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
919b92ce558SJens Axboe 	}
920b92ce558SJens Axboe 
9219e97198dSJens Axboe 	if (!bytes)
9229e97198dSJens Axboe 		bytes = ret;
923b92ce558SJens Axboe 
9249e97198dSJens Axboe 	goto done;
925c66ab6faSJens Axboe }
926c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
927c66ab6faSJens Axboe 
928c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
929c66ab6faSJens Axboe 			       struct splice_desc *sd)
930c66ab6faSJens Axboe {
9316a14b90bSJens Axboe 	struct file *file = sd->u.file;
932c66ab6faSJens Axboe 
9337995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
9342cb4b05eSChangli Gao 			      sd->flags);
935c66ab6faSJens Axboe }
936c66ab6faSJens Axboe 
937932cc6d4SJens Axboe /**
938932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
939932cc6d4SJens Axboe  * @in:		file to splice from
940932cc6d4SJens Axboe  * @ppos:	input file offset
941932cc6d4SJens Axboe  * @out:	file to splice to
942acdb37c3SRandy Dunlap  * @opos:	output file offset
943932cc6d4SJens Axboe  * @len:	number of bytes to splice
944932cc6d4SJens Axboe  * @flags:	splice modifier flags
945932cc6d4SJens Axboe  *
946932cc6d4SJens Axboe  * Description:
947932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
948932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
949932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
950932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
951932cc6d4SJens Axboe  *
952932cc6d4SJens Axboe  */
953c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
9547995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
955c66ab6faSJens Axboe {
956c66ab6faSJens Axboe 	struct splice_desc sd = {
957c66ab6faSJens Axboe 		.len		= len,
958c66ab6faSJens Axboe 		.total_len	= len,
959c66ab6faSJens Axboe 		.flags		= flags,
960c66ab6faSJens Axboe 		.pos		= *ppos,
9616a14b90bSJens Axboe 		.u.file		= out,
9627995bd28SAl Viro 		.opos		= opos,
963c66ab6faSJens Axboe 	};
96451a92c0fSJens Axboe 	long ret;
965c66ab6faSJens Axboe 
96618c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
96718c67cb9SAl Viro 		return -EBADF;
96818c67cb9SAl Viro 
96918c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
97018c67cb9SAl Viro 		return -EINVAL;
97118c67cb9SAl Viro 
97218c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
97318c67cb9SAl Viro 	if (unlikely(ret < 0))
97418c67cb9SAl Viro 		return ret;
97518c67cb9SAl Viro 
976c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
97751a92c0fSJens Axboe 	if (ret > 0)
978a82c53a0STom Zanussi 		*ppos = sd.pos;
97951a92c0fSJens Axboe 
980c66ab6faSJens Axboe 	return ret;
981b92ce558SJens Axboe }
9821c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
983b92ce558SJens Axboe 
9848924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
9858924feffSAl Viro {
98652bce911SLinus Torvalds 	for (;;) {
98752bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
98852bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
98952bce911SLinus Torvalds 			return -EPIPE;
99052bce911SLinus Torvalds 		}
9916718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
99252bce911SLinus Torvalds 			return 0;
9938924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
9948924feffSAl Viro 			return -EAGAIN;
9958924feffSAl Viro 		if (signal_pending(current))
9968924feffSAl Viro 			return -ERESTARTSYS;
997472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
9988924feffSAl Viro 	}
9998924feffSAl Viro }
10008924feffSAl Viro 
10017c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
10027c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
10037c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1004ddac0d39SJens Axboe 
1005ddac0d39SJens Axboe /*
100683f9135bSJens Axboe  * Determine where to splice to/from.
100783f9135bSJens Axboe  */
1008ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1009ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
10105274f052SJens Axboe {
10117c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
10127c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
10137995bd28SAl Viro 	loff_t offset;
1014a4514ebdSJens Axboe 	long ret;
10155274f052SJens Axboe 
101690da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
101790da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
101890da2e3fSPavel Begunkov 		return -EBADF;
101990da2e3fSPavel Begunkov 
1020c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1021c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
10227c77f0b3SMiklos Szeredi 
10237c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
10247c77f0b3SMiklos Szeredi 		if (off_in || off_out)
10257c77f0b3SMiklos Szeredi 			return -ESPIPE;
10267c77f0b3SMiklos Szeredi 
10277c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
10287c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
10297c77f0b3SMiklos Szeredi 			return -EINVAL;
10307c77f0b3SMiklos Szeredi 
1031ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1032ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1033ee5e0011SSlavomir Kaslev 
10347c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
10357c77f0b3SMiklos Szeredi 	}
10367c77f0b3SMiklos Szeredi 
10377c77f0b3SMiklos Szeredi 	if (ipipe) {
1038529565dcSIngo Molnar 		if (off_in)
1039529565dcSIngo Molnar 			return -ESPIPE;
1040b92ce558SJens Axboe 		if (off_out) {
104119c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1042b92ce558SJens Axboe 				return -EINVAL;
1043ee6e00c8SJens Axboe 			offset = *off_out;
10447995bd28SAl Viro 		} else {
10457995bd28SAl Viro 			offset = out->f_pos;
10467995bd28SAl Viro 		}
1047529565dcSIngo Molnar 
104818c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
104918c67cb9SAl Viro 			return -EINVAL;
105018c67cb9SAl Viro 
105118c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
105218c67cb9SAl Viro 		if (unlikely(ret < 0))
105318c67cb9SAl Viro 			return ret;
105418c67cb9SAl Viro 
1055ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1056ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1057ee5e0011SSlavomir Kaslev 
1058500368f7SAl Viro 		file_start_write(out);
10597995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1060500368f7SAl Viro 		file_end_write(out);
1061a4514ebdSJens Axboe 
10627995bd28SAl Viro 		if (!off_out)
10637995bd28SAl Viro 			out->f_pos = offset;
1064ee6e00c8SJens Axboe 		else
1065ee6e00c8SJens Axboe 			*off_out = offset;
1066a4514ebdSJens Axboe 
1067a4514ebdSJens Axboe 		return ret;
1068529565dcSIngo Molnar 	}
10695274f052SJens Axboe 
10707c77f0b3SMiklos Szeredi 	if (opipe) {
1071529565dcSIngo Molnar 		if (off_out)
1072529565dcSIngo Molnar 			return -ESPIPE;
1073b92ce558SJens Axboe 		if (off_in) {
107419c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1075b92ce558SJens Axboe 				return -EINVAL;
1076ee6e00c8SJens Axboe 			offset = *off_in;
10777995bd28SAl Viro 		} else {
10787995bd28SAl Viro 			offset = in->f_pos;
10797995bd28SAl Viro 		}
1080529565dcSIngo Molnar 
1081ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1082ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1083ee5e0011SSlavomir Kaslev 
10848924feffSAl Viro 		pipe_lock(opipe);
10858924feffSAl Viro 		ret = wait_for_space(opipe, flags);
1086*313d64a3SAl Viro 		if (!ret)
10877995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
10888924feffSAl Viro 		pipe_unlock(opipe);
10898924feffSAl Viro 		if (ret > 0)
10908924feffSAl Viro 			wakeup_pipe_readers(opipe);
10917995bd28SAl Viro 		if (!off_in)
10927995bd28SAl Viro 			in->f_pos = offset;
1093ee6e00c8SJens Axboe 		else
1094ee6e00c8SJens Axboe 			*off_in = offset;
1095a4514ebdSJens Axboe 
1096a4514ebdSJens Axboe 		return ret;
1097529565dcSIngo Molnar 	}
10985274f052SJens Axboe 
10995274f052SJens Axboe 	return -EINVAL;
11005274f052SJens Axboe }
11015274f052SJens Axboe 
1102ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1103ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1104ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1105ee6e00c8SJens Axboe {
1106ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1107ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1108ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1109ee6e00c8SJens Axboe 	long ret;
1110ee6e00c8SJens Axboe 
1111ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1112ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1113ee6e00c8SJens Axboe 
1114ee6e00c8SJens Axboe 	if (ipipe && off_in)
1115ee6e00c8SJens Axboe 		return -ESPIPE;
1116ee6e00c8SJens Axboe 	if (opipe && off_out)
1117ee6e00c8SJens Axboe 		return -ESPIPE;
1118ee6e00c8SJens Axboe 
1119ee6e00c8SJens Axboe 	if (off_out) {
1120ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1121ee6e00c8SJens Axboe 			return -EFAULT;
1122ee6e00c8SJens Axboe 		__off_out = &offset;
1123ee6e00c8SJens Axboe 	}
1124ee6e00c8SJens Axboe 	if (off_in) {
1125ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1126ee6e00c8SJens Axboe 			return -EFAULT;
1127ee6e00c8SJens Axboe 		__off_in = &offset;
1128ee6e00c8SJens Axboe 	}
1129ee6e00c8SJens Axboe 
1130ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1131ee6e00c8SJens Axboe 	if (ret < 0)
1132ee6e00c8SJens Axboe 		return ret;
1133ee6e00c8SJens Axboe 
1134ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1135ee6e00c8SJens Axboe 		return -EFAULT;
1136ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1137ee6e00c8SJens Axboe 		return -EFAULT;
1138ee6e00c8SJens Axboe 
1139ee6e00c8SJens Axboe 	return ret;
1140ee6e00c8SJens Axboe }
1141ee6e00c8SJens Axboe 
114279fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
114379fddc4eSAl Viro 			struct pipe_inode_info *pipe,
114479fddc4eSAl Viro 			unsigned flags)
1145912d35f8SJens Axboe {
114679fddc4eSAl Viro 	struct pipe_buffer buf = {
114779fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
114879fddc4eSAl Viro 		.flags = flags
114979fddc4eSAl Viro 	};
115079fddc4eSAl Viro 	size_t total = 0;
115179fddc4eSAl Viro 	int ret = 0;
115279fddc4eSAl Viro 	bool failed = false;
115379fddc4eSAl Viro 
115479fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
115579fddc4eSAl Viro 		struct page *pages[16];
1156db85a9ebSAl Viro 		ssize_t copied;
1157db85a9ebSAl Viro 		size_t start;
115879fddc4eSAl Viro 		int n;
1159912d35f8SJens Axboe 
116079fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
116179fddc4eSAl Viro 		if (copied <= 0) {
116279fddc4eSAl Viro 			ret = copied;
116379fddc4eSAl Viro 			break;
116479fddc4eSAl Viro 		}
1165912d35f8SJens Axboe 
116679fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1167db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
116879fddc4eSAl Viro 			if (!failed) {
116979fddc4eSAl Viro 				buf.page = pages[n];
117079fddc4eSAl Viro 				buf.offset = start;
117179fddc4eSAl Viro 				buf.len = size;
117279fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
117379fddc4eSAl Viro 				if (unlikely(ret < 0)) {
117479fddc4eSAl Viro 					failed = true;
117579fddc4eSAl Viro 				} else {
117679fddc4eSAl Viro 					iov_iter_advance(from, ret);
117779fddc4eSAl Viro 					total += ret;
117879fddc4eSAl Viro 				}
117979fddc4eSAl Viro 			} else {
118079fddc4eSAl Viro 				put_page(pages[n]);
118179fddc4eSAl Viro 			}
1182db85a9ebSAl Viro 			copied -= size;
1183912d35f8SJens Axboe 		}
1184912d35f8SJens Axboe 	}
118579fddc4eSAl Viro 	return total ? total : ret;
1186912d35f8SJens Axboe }
1187912d35f8SJens Axboe 
11886a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
11896a14b90bSJens Axboe 			struct splice_desc *sd)
11906a14b90bSJens Axboe {
11916130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
11926130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
11936a14b90bSJens Axboe }
11946a14b90bSJens Axboe 
11956a14b90bSJens Axboe /*
11966a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
11976a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
11986a14b90bSJens Axboe  */
119987a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
120087a3002aSAl Viro 			     unsigned int flags)
12016a14b90bSJens Axboe {
1202c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
120387a3002aSAl Viro 	struct splice_desc sd = {
120487a3002aSAl Viro 		.total_len = iov_iter_count(iter),
120587a3002aSAl Viro 		.flags = flags,
120687a3002aSAl Viro 		.u.data = iter
120787a3002aSAl Viro 	};
120887a3002aSAl Viro 	long ret = 0;
12096a14b90bSJens Axboe 
12106a14b90bSJens Axboe 	if (!pipe)
12116a14b90bSJens Axboe 		return -EBADF;
12126a14b90bSJens Axboe 
1213345995faSAl Viro 	if (sd.total_len) {
12146130f531SAl Viro 		pipe_lock(pipe);
12156130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
121661e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1217345995faSAl Viro 	}
12186a14b90bSJens Axboe 
12196a14b90bSJens Axboe 	return ret;
12206a14b90bSJens Axboe }
12216a14b90bSJens Axboe 
1222912d35f8SJens Axboe /*
1223912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1224912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1225912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1226912d35f8SJens Axboe  */
122787a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
122887a3002aSAl Viro 			     unsigned int flags)
1229912d35f8SJens Axboe {
1230ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
123187a3002aSAl Viro 	long ret = 0;
123279fddc4eSAl Viro 	unsigned buf_flag = 0;
123379fddc4eSAl Viro 
123479fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
123579fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1236912d35f8SJens Axboe 
1237c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1238ddac0d39SJens Axboe 	if (!pipe)
1239912d35f8SJens Axboe 		return -EBADF;
1240912d35f8SJens Axboe 
12418924feffSAl Viro 	pipe_lock(pipe);
12428924feffSAl Viro 	ret = wait_for_space(pipe, flags);
124379fddc4eSAl Viro 	if (!ret)
124487a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
12458924feffSAl Viro 	pipe_unlock(pipe);
12468924feffSAl Viro 	if (ret > 0)
12478924feffSAl Viro 		wakeup_pipe_readers(pipe);
124835f3d14dSJens Axboe 	return ret;
1249912d35f8SJens Axboe }
1250912d35f8SJens Axboe 
125187a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
125287a3002aSAl Viro {
125387a3002aSAl Viro 	if (!f.file)
125487a3002aSAl Viro 		return -EBADF;
125587a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
125687a3002aSAl Viro 		*type = WRITE;
125787a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
125887a3002aSAl Viro 		*type = READ;
125987a3002aSAl Viro 	} else {
126087a3002aSAl Viro 		fdput(f);
126187a3002aSAl Viro 		return -EBADF;
126287a3002aSAl Viro 	}
126387a3002aSAl Viro 	return 0;
126487a3002aSAl Viro }
126587a3002aSAl Viro 
12666a14b90bSJens Axboe /*
12676a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
12686a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
12696a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
12706a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
12716a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
12726a14b90bSJens Axboe  * solutions for that:
12736a14b90bSJens Axboe  *
12746a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
12756a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
12766a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
12776a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
12786a14b90bSJens Axboe  *
12796a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
12806a14b90bSJens Axboe  *
12816a14b90bSJens Axboe  */
128287a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
128330cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
128430cfe4efSDominik Brodowski {
128587a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
128687a3002aSAl Viro 	struct iovec *iov = iovstack;
128787a3002aSAl Viro 	struct iov_iter iter;
128887e5e6daSJens Axboe 	ssize_t error;
128987a3002aSAl Viro 	struct fd f;
129087a3002aSAl Viro 	int type;
129187a3002aSAl Viro 
1292598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1293598b3cecSChristoph Hellwig 		return -EINVAL;
1294598b3cecSChristoph Hellwig 
129587a3002aSAl Viro 	f = fdget(fd);
129687a3002aSAl Viro 	error = vmsplice_type(f, &type);
129787a3002aSAl Viro 	if (error)
129887a3002aSAl Viro 		return error;
129987a3002aSAl Viro 
130087a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
130187a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1302598b3cecSChristoph Hellwig 	if (error < 0)
1303598b3cecSChristoph Hellwig 		goto out_fdput;
1304598b3cecSChristoph Hellwig 
1305598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1306598b3cecSChristoph Hellwig 		error = 0;
1307598b3cecSChristoph Hellwig 	else if (iov_iter_rw(&iter) == WRITE)
1308598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1309598b3cecSChristoph Hellwig 	else
1310598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1311598b3cecSChristoph Hellwig 
131287a3002aSAl Viro 	kfree(iov);
1313598b3cecSChristoph Hellwig out_fdput:
131487a3002aSAl Viro 	fdput(f);
131587a3002aSAl Viro 	return error;
131630cfe4efSDominik Brodowski }
131730cfe4efSDominik Brodowski 
1318836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1319836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1320836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
13215274f052SJens Axboe {
13222903ff01SAl Viro 	struct fd in, out;
13235274f052SJens Axboe 	long error;
13245274f052SJens Axboe 
13255274f052SJens Axboe 	if (unlikely(!len))
13265274f052SJens Axboe 		return 0;
13275274f052SJens Axboe 
13283d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
13293d6ea290SAl Viro 		return -EINVAL;
13303d6ea290SAl Viro 
13315274f052SJens Axboe 	error = -EBADF;
13322903ff01SAl Viro 	in = fdget(fd_in);
13332903ff01SAl Viro 	if (in.file) {
13342903ff01SAl Viro 		out = fdget(fd_out);
13352903ff01SAl Viro 		if (out.file) {
1336ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1337529565dcSIngo Molnar 						len, flags);
13382903ff01SAl Viro 			fdput(out);
13395274f052SJens Axboe 		}
13402903ff01SAl Viro 		fdput(in);
13415274f052SJens Axboe 	}
13425274f052SJens Axboe 	return error;
13435274f052SJens Axboe }
134470524490SJens Axboe 
134570524490SJens Axboe /*
1346aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1347aadd06e5SJens Axboe  * return an appropriate error.
1348aadd06e5SJens Axboe  */
13497c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1350aadd06e5SJens Axboe {
1351aadd06e5SJens Axboe 	int ret;
1352aadd06e5SJens Axboe 
1353aadd06e5SJens Axboe 	/*
13548cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1355aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1356aadd06e5SJens Axboe 	 */
13578cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1358aadd06e5SJens Axboe 		return 0;
1359aadd06e5SJens Axboe 
1360aadd06e5SJens Axboe 	ret = 0;
136161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1362aadd06e5SJens Axboe 
13638cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1364aadd06e5SJens Axboe 		if (signal_pending(current)) {
1365aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1366aadd06e5SJens Axboe 			break;
1367aadd06e5SJens Axboe 		}
1368aadd06e5SJens Axboe 		if (!pipe->writers)
1369aadd06e5SJens Axboe 			break;
1370aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1371aadd06e5SJens Axboe 			ret = -EAGAIN;
1372aadd06e5SJens Axboe 			break;
1373aadd06e5SJens Axboe 		}
1374472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1375aadd06e5SJens Axboe 	}
1376aadd06e5SJens Axboe 
137761e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1378aadd06e5SJens Axboe 	return ret;
1379aadd06e5SJens Axboe }
1380aadd06e5SJens Axboe 
1381aadd06e5SJens Axboe /*
1382aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1383aadd06e5SJens Axboe  * return an appropriate error.
1384aadd06e5SJens Axboe  */
13857c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1386aadd06e5SJens Axboe {
1387aadd06e5SJens Axboe 	int ret;
1388aadd06e5SJens Axboe 
1389aadd06e5SJens Axboe 	/*
13908cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1391aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1392aadd06e5SJens Axboe 	 */
1393566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1394aadd06e5SJens Axboe 		return 0;
1395aadd06e5SJens Axboe 
1396aadd06e5SJens Axboe 	ret = 0;
139761e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1398aadd06e5SJens Axboe 
13996718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1400aadd06e5SJens Axboe 		if (!pipe->readers) {
1401aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1402aadd06e5SJens Axboe 			ret = -EPIPE;
1403aadd06e5SJens Axboe 			break;
1404aadd06e5SJens Axboe 		}
1405aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1406aadd06e5SJens Axboe 			ret = -EAGAIN;
1407aadd06e5SJens Axboe 			break;
1408aadd06e5SJens Axboe 		}
1409aadd06e5SJens Axboe 		if (signal_pending(current)) {
1410aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1411aadd06e5SJens Axboe 			break;
1412aadd06e5SJens Axboe 		}
1413472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1414aadd06e5SJens Axboe 	}
1415aadd06e5SJens Axboe 
141661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1417aadd06e5SJens Axboe 	return ret;
1418aadd06e5SJens Axboe }
1419aadd06e5SJens Axboe 
1420aadd06e5SJens Axboe /*
14217c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
14227c77f0b3SMiklos Szeredi  */
14237c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
14247c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
14257c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
14267c77f0b3SMiklos Szeredi {
14277c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
14288cefc107SDavid Howells 	unsigned int i_head, o_head;
14298cefc107SDavid Howells 	unsigned int i_tail, o_tail;
14308cefc107SDavid Howells 	unsigned int i_mask, o_mask;
14318cefc107SDavid Howells 	int ret = 0;
14327c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
14337c77f0b3SMiklos Szeredi 
14347c77f0b3SMiklos Szeredi 
14357c77f0b3SMiklos Szeredi retry:
14367c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
14377c77f0b3SMiklos Szeredi 	if (ret)
14387c77f0b3SMiklos Szeredi 		return ret;
14397c77f0b3SMiklos Szeredi 
14407c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
14417c77f0b3SMiklos Szeredi 	if (ret)
14427c77f0b3SMiklos Szeredi 		return ret;
14437c77f0b3SMiklos Szeredi 
14447c77f0b3SMiklos Szeredi 	/*
14457c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
14467c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
14477c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
14487c77f0b3SMiklos Szeredi 	 */
14497c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
14507c77f0b3SMiklos Szeredi 
14518cefc107SDavid Howells 	i_tail = ipipe->tail;
14528cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
14538cefc107SDavid Howells 	o_head = opipe->head;
14548cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
14558cefc107SDavid Howells 
14567c77f0b3SMiklos Szeredi 	do {
14578cefc107SDavid Howells 		size_t o_len;
14588cefc107SDavid Howells 
14597c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
14607c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
14617c77f0b3SMiklos Szeredi 			if (!ret)
14627c77f0b3SMiklos Szeredi 				ret = -EPIPE;
14637c77f0b3SMiklos Szeredi 			break;
14647c77f0b3SMiklos Szeredi 		}
14657c77f0b3SMiklos Szeredi 
14668cefc107SDavid Howells 		i_head = ipipe->head;
14678cefc107SDavid Howells 		o_tail = opipe->tail;
14688cefc107SDavid Howells 
14698cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
14707c77f0b3SMiklos Szeredi 			break;
14717c77f0b3SMiklos Szeredi 
14727c77f0b3SMiklos Szeredi 		/*
14737c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
14747c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
14757c77f0b3SMiklos Szeredi 		 */
14768cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
14776718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
14787c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
14797c77f0b3SMiklos Szeredi 			if (ret)
14807c77f0b3SMiklos Szeredi 				break;
14817c77f0b3SMiklos Szeredi 
14827c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
14837c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
14847c77f0b3SMiklos Szeredi 				break;
14857c77f0b3SMiklos Szeredi 			}
14867c77f0b3SMiklos Szeredi 
14877c77f0b3SMiklos Szeredi 			/*
14887c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
14897c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
14907c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
14917c77f0b3SMiklos Szeredi 			 */
14927c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
14937c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
14947c77f0b3SMiklos Szeredi 			goto retry;
14957c77f0b3SMiklos Szeredi 		}
14967c77f0b3SMiklos Szeredi 
14978cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
14988cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
14997c77f0b3SMiklos Szeredi 
15007c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
15017c77f0b3SMiklos Szeredi 			/*
15027c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
15037c77f0b3SMiklos Szeredi 			 */
15047c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15057c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
15068cefc107SDavid Howells 			i_tail++;
15078cefc107SDavid Howells 			ipipe->tail = i_tail;
15087c77f0b3SMiklos Szeredi 			input_wakeup = true;
15098cefc107SDavid Howells 			o_len = obuf->len;
15108cefc107SDavid Howells 			o_head++;
15118cefc107SDavid Howells 			opipe->head = o_head;
15127c77f0b3SMiklos Szeredi 		} else {
15137c77f0b3SMiklos Szeredi 			/*
15147c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
15157c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
15167c77f0b3SMiklos Szeredi 			 */
151715fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
151815fab63eSMatthew Wilcox 				if (ret == 0)
151915fab63eSMatthew Wilcox 					ret = -EFAULT;
152015fab63eSMatthew Wilcox 				break;
152115fab63eSMatthew Wilcox 			}
15227c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15237c77f0b3SMiklos Szeredi 
15247c77f0b3SMiklos Szeredi 			/*
1525f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
15267c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
15277c77f0b3SMiklos Szeredi 			 */
15287c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1529f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1530a0ce2f0aSJann Horn 
15317c77f0b3SMiklos Szeredi 			obuf->len = len;
15328cefc107SDavid Howells 			ibuf->offset += len;
15338cefc107SDavid Howells 			ibuf->len -= len;
15348cefc107SDavid Howells 			o_len = len;
15358cefc107SDavid Howells 			o_head++;
15368cefc107SDavid Howells 			opipe->head = o_head;
15377c77f0b3SMiklos Szeredi 		}
15388cefc107SDavid Howells 		ret += o_len;
15398cefc107SDavid Howells 		len -= o_len;
15407c77f0b3SMiklos Szeredi 	} while (len);
15417c77f0b3SMiklos Szeredi 
15427c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
15437c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
15447c77f0b3SMiklos Szeredi 
15457c77f0b3SMiklos Szeredi 	/*
15467c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
15477c77f0b3SMiklos Szeredi 	 */
1548825cdcb1SNamhyung Kim 	if (ret > 0)
1549825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1550825cdcb1SNamhyung Kim 
15517c77f0b3SMiklos Szeredi 	if (input_wakeup)
15527c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
15537c77f0b3SMiklos Szeredi 
15547c77f0b3SMiklos Szeredi 	return ret;
15557c77f0b3SMiklos Szeredi }
15567c77f0b3SMiklos Szeredi 
15577c77f0b3SMiklos Szeredi /*
155870524490SJens Axboe  * Link contents of ipipe to opipe.
155970524490SJens Axboe  */
156070524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
156170524490SJens Axboe 		     struct pipe_inode_info *opipe,
156270524490SJens Axboe 		     size_t len, unsigned int flags)
156370524490SJens Axboe {
156470524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
15658cefc107SDavid Howells 	unsigned int i_head, o_head;
15668cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15678cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15688cefc107SDavid Howells 	int ret = 0;
156970524490SJens Axboe 
157070524490SJens Axboe 	/*
157170524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
157261e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
157370524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
157470524490SJens Axboe 	 */
157561e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
157670524490SJens Axboe 
15778cefc107SDavid Howells 	i_tail = ipipe->tail;
15788cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15798cefc107SDavid Howells 	o_head = opipe->head;
15808cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15818cefc107SDavid Howells 
1582aadd06e5SJens Axboe 	do {
158370524490SJens Axboe 		if (!opipe->readers) {
158470524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
158570524490SJens Axboe 			if (!ret)
158670524490SJens Axboe 				ret = -EPIPE;
158770524490SJens Axboe 			break;
158870524490SJens Axboe 		}
158970524490SJens Axboe 
15908cefc107SDavid Howells 		i_head = ipipe->head;
15918cefc107SDavid Howells 		o_tail = opipe->tail;
15928cefc107SDavid Howells 
159370524490SJens Axboe 		/*
15948cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1595aadd06e5SJens Axboe 		 * output room, break.
159670524490SJens Axboe 		 */
15978cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
15986718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1599aadd06e5SJens Axboe 			break;
1600aadd06e5SJens Axboe 
16018cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16028cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
160370524490SJens Axboe 
160470524490SJens Axboe 		/*
160570524490SJens Axboe 		 * Get a reference to this pipe buffer,
160670524490SJens Axboe 		 * so we can copy the contents over.
160770524490SJens Axboe 		 */
160815fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
160915fab63eSMatthew Wilcox 			if (ret == 0)
161015fab63eSMatthew Wilcox 				ret = -EFAULT;
161115fab63eSMatthew Wilcox 			break;
161215fab63eSMatthew Wilcox 		}
161370524490SJens Axboe 
161470524490SJens Axboe 		*obuf = *ibuf;
161570524490SJens Axboe 
16167afa6fd0SJens Axboe 		/*
1617f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1618f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
16197afa6fd0SJens Axboe 		 */
16207afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1621f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1622a0ce2f0aSJann Horn 
162370524490SJens Axboe 		if (obuf->len > len)
162470524490SJens Axboe 			obuf->len = len;
162570524490SJens Axboe 		ret += obuf->len;
162670524490SJens Axboe 		len -= obuf->len;
16278cefc107SDavid Howells 
16288cefc107SDavid Howells 		o_head++;
16298cefc107SDavid Howells 		opipe->head = o_head;
16308cefc107SDavid Howells 		i_tail++;
1631aadd06e5SJens Axboe 	} while (len);
163270524490SJens Axboe 
163361e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
163461e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
163570524490SJens Axboe 
1636aadd06e5SJens Axboe 	/*
1637aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1638aadd06e5SJens Axboe 	 */
1639825cdcb1SNamhyung Kim 	if (ret > 0)
1640825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
164170524490SJens Axboe 
164270524490SJens Axboe 	return ret;
164370524490SJens Axboe }
164470524490SJens Axboe 
164570524490SJens Axboe /*
164670524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
164770524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
164870524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
164970524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
165070524490SJens Axboe  */
16519dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
165270524490SJens Axboe {
1653c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1654c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1655aadd06e5SJens Axboe 	int ret = -EINVAL;
165670524490SJens Axboe 
165790da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
165890da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
165990da2e3fSPavel Begunkov 		return -EBADF;
166090da2e3fSPavel Begunkov 
166170524490SJens Axboe 	/*
1662aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1663aadd06e5SJens Axboe 	 * copying the data.
166470524490SJens Axboe 	 */
1665aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1666ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1667ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1668ee5e0011SSlavomir Kaslev 
1669aadd06e5SJens Axboe 		/*
1670aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1671aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1672aadd06e5SJens Axboe 		 */
16737c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1674aadd06e5SJens Axboe 		if (!ret) {
16757c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
167602cf01aeSJens Axboe 			if (!ret)
1677aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1678aadd06e5SJens Axboe 		}
1679aadd06e5SJens Axboe 	}
168070524490SJens Axboe 
1681aadd06e5SJens Axboe 	return ret;
168270524490SJens Axboe }
168370524490SJens Axboe 
1684836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
168570524490SJens Axboe {
168690da2e3fSPavel Begunkov 	struct fd in, out;
16872903ff01SAl Viro 	int error;
168870524490SJens Axboe 
16893d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
16903d6ea290SAl Viro 		return -EINVAL;
16913d6ea290SAl Viro 
169270524490SJens Axboe 	if (unlikely(!len))
169370524490SJens Axboe 		return 0;
169470524490SJens Axboe 
169570524490SJens Axboe 	error = -EBADF;
16962903ff01SAl Viro 	in = fdget(fdin);
16972903ff01SAl Viro 	if (in.file) {
169890da2e3fSPavel Begunkov 		out = fdget(fdout);
16992903ff01SAl Viro 		if (out.file) {
170090da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
17012903ff01SAl Viro 			fdput(out);
170270524490SJens Axboe 		}
17032903ff01SAl Viro  		fdput(in);
170470524490SJens Axboe  	}
170570524490SJens Axboe 
170670524490SJens Axboe 	return error;
170770524490SJens Axboe }
1708