xref: /openbmc/linux/fs/splice.c (revision 457c8996)
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();
168825cdcb1SNamhyung Kim 	if (waitqueue_active(&pipe->wait))
169825cdcb1SNamhyung Kim 		wake_up_interruptible(&pipe->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;
1888924feffSAl Viro 	int ret = 0, page_nr = 0;
1895274f052SJens Axboe 
190d6785d91SRabin Vincent 	if (!spd_pages)
191d6785d91SRabin Vincent 		return 0;
192d6785d91SRabin Vincent 
1938924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1945274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1955274f052SJens Axboe 		ret = -EPIPE;
1968924feffSAl Viro 		goto out;
1975274f052SJens Axboe 	}
1985274f052SJens Axboe 
1998924feffSAl Viro 	while (pipe->nrbufs < pipe->buffers) {
20035f3d14dSJens Axboe 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
2013a326a2cSIngo Molnar 		struct pipe_buffer *buf = pipe->bufs + newbuf;
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 
2106f767b04SJens Axboe 		pipe->nrbufs++;
211912d35f8SJens Axboe 		page_nr++;
212912d35f8SJens Axboe 		ret += buf->len;
213912d35f8SJens Axboe 
214912d35f8SJens Axboe 		if (!--spd->nr_pages)
2155274f052SJens Axboe 			break;
2165274f052SJens Axboe 	}
2175274f052SJens Axboe 
21829e35094SLinus Torvalds 	if (!ret)
21929e35094SLinus Torvalds 		ret = -EAGAIN;
22029e35094SLinus Torvalds 
2218924feffSAl Viro out:
22200de00bdSJens Axboe 	while (page_nr < spd_pages)
223bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2245274f052SJens Axboe 
2255274f052SJens Axboe 	return ret;
2265274f052SJens Axboe }
2272b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2285274f052SJens Axboe 
22979fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
23079fddc4eSAl Viro {
23179fddc4eSAl Viro 	int ret;
23279fddc4eSAl Viro 
23379fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23479fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
23579fddc4eSAl Viro 		ret = -EPIPE;
23679fddc4eSAl Viro 	} else if (pipe->nrbufs == pipe->buffers) {
23779fddc4eSAl Viro 		ret = -EAGAIN;
23879fddc4eSAl Viro 	} else {
23979fddc4eSAl Viro 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
24079fddc4eSAl Viro 		pipe->bufs[newbuf] = *buf;
24179fddc4eSAl Viro 		pipe->nrbufs++;
24279fddc4eSAl Viro 		return buf->len;
24379fddc4eSAl Viro 	}
244a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24579fddc4eSAl Viro 	return ret;
24679fddc4eSAl Viro }
24779fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
24879fddc4eSAl Viro 
24935f3d14dSJens Axboe /*
25035f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25135f3d14dSJens Axboe  * descriptions.
25235f3d14dSJens Axboe  */
253047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25435f3d14dSJens Axboe {
2556aa7de05SMark Rutland 	unsigned int buffers = READ_ONCE(pipe->buffers);
256047fe360SEric Dumazet 
257047fe360SEric Dumazet 	spd->nr_pages_max = buffers;
258047fe360SEric Dumazet 	if (buffers <= PIPE_DEF_BUFFERS)
25935f3d14dSJens Axboe 		return 0;
26035f3d14dSJens Axboe 
2616da2ec56SKees Cook 	spd->pages = kmalloc_array(buffers, sizeof(struct page *), GFP_KERNEL);
2626da2ec56SKees Cook 	spd->partial = kmalloc_array(buffers, sizeof(struct partial_page),
2636da2ec56SKees Cook 				     GFP_KERNEL);
26435f3d14dSJens Axboe 
26535f3d14dSJens Axboe 	if (spd->pages && spd->partial)
26635f3d14dSJens Axboe 		return 0;
26735f3d14dSJens Axboe 
26835f3d14dSJens Axboe 	kfree(spd->pages);
26935f3d14dSJens Axboe 	kfree(spd->partial);
27035f3d14dSJens Axboe 	return -ENOMEM;
27135f3d14dSJens Axboe }
27235f3d14dSJens Axboe 
273047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27435f3d14dSJens Axboe {
275047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
27635f3d14dSJens Axboe 		return;
27735f3d14dSJens Axboe 
27835f3d14dSJens Axboe 	kfree(spd->pages);
27935f3d14dSJens Axboe 	kfree(spd->partial);
28035f3d14dSJens Axboe }
28135f3d14dSJens Axboe 
28283f9135bSJens Axboe /**
28383f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
28483f9135bSJens Axboe  * @in:		file to splice from
285932cc6d4SJens Axboe  * @ppos:	position in @in
28683f9135bSJens Axboe  * @pipe:	pipe to splice to
28783f9135bSJens Axboe  * @len:	number of bytes to splice
28883f9135bSJens Axboe  * @flags:	splice modifier flags
28983f9135bSJens Axboe  *
290932cc6d4SJens Axboe  * Description:
291932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
29282c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
293932cc6d4SJens Axboe  *
29483f9135bSJens Axboe  */
295cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
296cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
297cbb7e577SJens Axboe 				 unsigned int flags)
2985274f052SJens Axboe {
29982c156f8SAl Viro 	struct iov_iter to;
30082c156f8SAl Viro 	struct kiocb kiocb;
30182c156f8SAl Viro 	int idx, ret;
302be64f884SBoaz Harrosh 
303aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len);
30482c156f8SAl Viro 	idx = to.idx;
30582c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
30682c156f8SAl Viro 	kiocb.ki_pos = *ppos;
307bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
308723590edSMiklos Szeredi 	if (ret > 0) {
30982c156f8SAl Viro 		*ppos = kiocb.ki_pos;
310723590edSMiklos Szeredi 		file_accessed(in);
31182c156f8SAl Viro 	} else if (ret < 0) {
31282c156f8SAl Viro 		to.idx = idx;
31382c156f8SAl Viro 		to.iov_offset = 0;
31482c156f8SAl Viro 		iov_iter_advance(&to, 0); /* to free what was emitted */
31582c156f8SAl Viro 		/*
31682c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
31782c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
31882c156f8SAl Viro 		 */
31982c156f8SAl Viro 		if (ret == -EFAULT)
32082c156f8SAl Viro 			ret = -EAGAIN;
321723590edSMiklos Szeredi 	}
3225274f052SJens Axboe 
3235274f052SJens Axboe 	return ret;
3245274f052SJens Axboe }
325059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
326059a8f37SJens Axboe 
327241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
3286818173bSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
3296818173bSMiklos Szeredi 	.release = generic_pipe_buf_release,
3306818173bSMiklos Szeredi 	.steal = generic_pipe_buf_steal,
3316818173bSMiklos Szeredi 	.get = generic_pipe_buf_get,
3326818173bSMiklos Szeredi };
3336818173bSMiklos Szeredi 
334b9872226SJann Horn int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
33528a625cbSMiklos Szeredi 			     struct pipe_buffer *buf)
33628a625cbSMiklos Szeredi {
33728a625cbSMiklos Szeredi 	return 1;
33828a625cbSMiklos Szeredi }
33928a625cbSMiklos Szeredi 
34028a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
34128a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
34228a625cbSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
34328a625cbSMiklos Szeredi 	.release = generic_pipe_buf_release,
34428a625cbSMiklos Szeredi 	.steal = generic_pipe_buf_nosteal,
34528a625cbSMiklos Szeredi 	.get = generic_pipe_buf_get,
34628a625cbSMiklos Szeredi };
34728a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
34828a625cbSMiklos Szeredi 
349523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
3506818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
3516818173bSMiklos Szeredi {
3526818173bSMiklos Szeredi 	mm_segment_t old_fs;
3536818173bSMiklos Szeredi 	loff_t pos = offset;
3546818173bSMiklos Szeredi 	ssize_t res;
3556818173bSMiklos Szeredi 
3566818173bSMiklos Szeredi 	old_fs = get_fs();
357736706beSLinus Torvalds 	set_fs(KERNEL_DS);
3586818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
359793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
3606818173bSMiklos Szeredi 	set_fs(old_fs);
3616818173bSMiklos Szeredi 
3626818173bSMiklos Szeredi 	return res;
3636818173bSMiklos Szeredi }
3646818173bSMiklos Szeredi 
36582c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
3666818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
3676818173bSMiklos Szeredi 				 unsigned int flags)
3686818173bSMiklos Szeredi {
369523ac9afSAl Viro 	struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
370523ac9afSAl Viro 	struct iov_iter to;
371523ac9afSAl Viro 	struct page **pages;
3726818173bSMiklos Szeredi 	unsigned int nr_pages;
37313c0f52bSAl Viro 	size_t offset, base, copied = 0;
3746818173bSMiklos Szeredi 	ssize_t res;
3756818173bSMiklos Szeredi 	int i;
3766818173bSMiklos Szeredi 
377523ac9afSAl Viro 	if (pipe->nrbufs == pipe->buffers)
378523ac9afSAl Viro 		return -EAGAIN;
379523ac9afSAl Viro 
380523ac9afSAl Viro 	/*
381523ac9afSAl Viro 	 * Try to keep page boundaries matching to source pagecache ones -
382523ac9afSAl Viro 	 * it probably won't be much help, but...
383523ac9afSAl Viro 	 */
384523ac9afSAl Viro 	offset = *ppos & ~PAGE_MASK;
385523ac9afSAl Viro 
386aa563d7bSDavid Howells 	iov_iter_pipe(&to, READ, pipe, len + offset);
387523ac9afSAl Viro 
38813c0f52bSAl Viro 	res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
389523ac9afSAl Viro 	if (res <= 0)
39035f3d14dSJens Axboe 		return -ENOMEM;
39135f3d14dSJens Axboe 
39213c0f52bSAl Viro 	nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
393523ac9afSAl Viro 
39435f3d14dSJens Axboe 	vec = __vec;
395523ac9afSAl Viro 	if (nr_pages > PIPE_DEF_BUFFERS) {
3966da2ec56SKees Cook 		vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
397523ac9afSAl Viro 		if (unlikely(!vec)) {
398523ac9afSAl Viro 			res = -ENOMEM;
399523ac9afSAl Viro 			goto out;
400523ac9afSAl Viro 		}
40135f3d14dSJens Axboe 	}
40235f3d14dSJens Axboe 
403523ac9afSAl Viro 	pipe->bufs[to.idx].offset = offset;
404523ac9afSAl Viro 	pipe->bufs[to.idx].len -= offset;
4056818173bSMiklos Szeredi 
406523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++) {
407523ac9afSAl Viro 		size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
408523ac9afSAl Viro 		vec[i].iov_base = page_address(pages[i]) + offset;
4096818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
4106818173bSMiklos Szeredi 		len -= this_len;
4116818173bSMiklos Szeredi 		offset = 0;
4126818173bSMiklos Szeredi 	}
4136818173bSMiklos Szeredi 
414523ac9afSAl Viro 	res = kernel_readv(in, vec, nr_pages, *ppos);
415523ac9afSAl Viro 	if (res > 0) {
416523ac9afSAl Viro 		copied = res;
4176818173bSMiklos Szeredi 		*ppos += res;
418523ac9afSAl Viro 	}
4196818173bSMiklos Szeredi 
42035f3d14dSJens Axboe 	if (vec != __vec)
42135f3d14dSJens Axboe 		kfree(vec);
422523ac9afSAl Viro out:
423523ac9afSAl Viro 	for (i = 0; i < nr_pages; i++)
424523ac9afSAl Viro 		put_page(pages[i]);
425523ac9afSAl Viro 	kvfree(pages);
426523ac9afSAl Viro 	iov_iter_advance(&to, copied);	/* truncates and discards */
4276818173bSMiklos Szeredi 	return res;
4286818173bSMiklos Szeredi }
4296818173bSMiklos Szeredi 
4305274f052SJens Axboe /*
4314f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
432016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4335274f052SJens Axboe  */
43476ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4355274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4365274f052SJens Axboe {
4376a14b90bSJens Axboe 	struct file *file = sd->u.file;
4385274f052SJens Axboe 	loff_t pos = sd->pos;
439a8adbe37SMichał Mirosław 	int more;
4405274f052SJens Axboe 
44172c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
442a8adbe37SMichał Mirosław 		return -EINVAL;
443a8adbe37SMichał Mirosław 
44435f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
445ae62ca7bSEric Dumazet 
446ae62ca7bSEric Dumazet 	if (sd->len < sd->total_len && pipe->nrbufs > 1)
44735f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
448ae62ca7bSEric Dumazet 
449a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
450f84d7519SJens Axboe 				    sd->len, &pos, more);
4515274f052SJens Axboe }
4525274f052SJens Axboe 
453b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
454b3c2d2ddSMiklos Szeredi {
455b3c2d2ddSMiklos Szeredi 	smp_mb();
456b3c2d2ddSMiklos Szeredi 	if (waitqueue_active(&pipe->wait))
457b3c2d2ddSMiklos Szeredi 		wake_up_interruptible(&pipe->wait);
458b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
459b3c2d2ddSMiklos Szeredi }
460b3c2d2ddSMiklos Szeredi 
461b3c2d2ddSMiklos Szeredi /**
462b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
463b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
464b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
465b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
466b3c2d2ddSMiklos Szeredi  *
467b3c2d2ddSMiklos Szeredi  * Description:
468b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
469b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
470b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
471b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
472b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
473b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
474b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
475b3c2d2ddSMiklos Szeredi  *
476b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
477b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
478b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
479b3c2d2ddSMiklos Szeredi  *    destination.
480b3c2d2ddSMiklos Szeredi  */
48196f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
482b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
483b3c2d2ddSMiklos Szeredi {
484b3c2d2ddSMiklos Szeredi 	int ret;
485b3c2d2ddSMiklos Szeredi 
486b3c2d2ddSMiklos Szeredi 	while (pipe->nrbufs) {
487b3c2d2ddSMiklos Szeredi 		struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
488b3c2d2ddSMiklos Szeredi 
489b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
490b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
491b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
492b3c2d2ddSMiklos Szeredi 
493fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
494a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
495b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
496b3c2d2ddSMiklos Szeredi 				ret = 0;
497b3c2d2ddSMiklos Szeredi 			return ret;
498b3c2d2ddSMiklos Szeredi 		}
499a8adbe37SMichał Mirosław 
500a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
501a8adbe37SMichał Mirosław 		if (ret <= 0)
502a8adbe37SMichał Mirosław 			return ret;
503a8adbe37SMichał Mirosław 
504b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
505b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
506b3c2d2ddSMiklos Szeredi 
507b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
508b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
509b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
510b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
511b3c2d2ddSMiklos Szeredi 
512b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
513a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
51435f3d14dSJens Axboe 			pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
515b3c2d2ddSMiklos Szeredi 			pipe->nrbufs--;
5166447a3cfSAl Viro 			if (pipe->files)
517b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
518b3c2d2ddSMiklos Szeredi 		}
519b3c2d2ddSMiklos Szeredi 
520b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
521b3c2d2ddSMiklos Szeredi 			return 0;
522b3c2d2ddSMiklos Szeredi 	}
523b3c2d2ddSMiklos Szeredi 
524b3c2d2ddSMiklos Szeredi 	return 1;
525b3c2d2ddSMiklos Szeredi }
526b3c2d2ddSMiklos Szeredi 
527b3c2d2ddSMiklos Szeredi /**
528b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
529b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
530b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
531b3c2d2ddSMiklos Szeredi  *
532b3c2d2ddSMiklos Szeredi  * Description:
533b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
534b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
535b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
536b3c2d2ddSMiklos Szeredi  */
53796f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
538b3c2d2ddSMiklos Szeredi {
539c725bfceSJan Kara 	/*
540c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
541c725bfceSJan Kara 	 * always buffers available
542c725bfceSJan Kara 	 */
543c725bfceSJan Kara 	if (signal_pending(current))
544c725bfceSJan Kara 		return -ERESTARTSYS;
545c725bfceSJan Kara 
546b3c2d2ddSMiklos Szeredi 	while (!pipe->nrbufs) {
547b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
548b3c2d2ddSMiklos Szeredi 			return 0;
549b3c2d2ddSMiklos Szeredi 
550b3c2d2ddSMiklos Szeredi 		if (!pipe->waiting_writers && sd->num_spliced)
551b3c2d2ddSMiklos Szeredi 			return 0;
552b3c2d2ddSMiklos Szeredi 
553b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
554b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
555b3c2d2ddSMiklos Szeredi 
556b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
557b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
558b3c2d2ddSMiklos Szeredi 
559b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
560b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
561b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
562b3c2d2ddSMiklos Szeredi 		}
563b3c2d2ddSMiklos Szeredi 
564b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
565b3c2d2ddSMiklos Szeredi 	}
566b3c2d2ddSMiklos Szeredi 
567b3c2d2ddSMiklos Szeredi 	return 1;
568b3c2d2ddSMiklos Szeredi }
569b3c2d2ddSMiklos Szeredi 
570b3c2d2ddSMiklos Szeredi /**
571b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
572b80901bbSRandy Dunlap  * @sd:		information about the splice operation
573b3c2d2ddSMiklos Szeredi  *
574b3c2d2ddSMiklos Szeredi  * Description:
575b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
576b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
577b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
578b3c2d2ddSMiklos Szeredi  */
57996f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
580b3c2d2ddSMiklos Szeredi {
581b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
582b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
583b3c2d2ddSMiklos Szeredi }
584b3c2d2ddSMiklos Szeredi 
585b3c2d2ddSMiklos Szeredi /**
586b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
587b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
588b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
589b3c2d2ddSMiklos Szeredi  *
590b3c2d2ddSMiklos Szeredi  * Description:
591b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
592b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
593b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
594b3c2d2ddSMiklos Szeredi  */
59596f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
596b3c2d2ddSMiklos Szeredi {
597b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
598b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
599b3c2d2ddSMiklos Szeredi }
600b3c2d2ddSMiklos Szeredi 
601932cc6d4SJens Axboe /**
602932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
603932cc6d4SJens Axboe  * @pipe:	pipe to splice from
604932cc6d4SJens Axboe  * @sd:		information to @actor
605932cc6d4SJens Axboe  * @actor:	handler that splices the data
606932cc6d4SJens Axboe  *
607932cc6d4SJens Axboe  * Description:
608932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
609932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
610932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
611932cc6d4SJens Axboe  *    pipe_to_user.
612932cc6d4SJens Axboe  *
61383f9135bSJens Axboe  */
614c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
615c66ab6faSJens Axboe 			   splice_actor *actor)
6165274f052SJens Axboe {
617b3c2d2ddSMiklos Szeredi 	int ret;
6185274f052SJens Axboe 
619b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
620b3c2d2ddSMiklos Szeredi 	do {
621c2489e07SJan Kara 		cond_resched();
622b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
623b3c2d2ddSMiklos Szeredi 		if (ret > 0)
624b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
625b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
626b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6275274f052SJens Axboe 
628b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6295274f052SJens Axboe }
63040bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6315274f052SJens Axboe 
632932cc6d4SJens Axboe /**
633932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
634932cc6d4SJens Axboe  * @pipe:	pipe to splice from
635932cc6d4SJens Axboe  * @out:	file to splice to
636932cc6d4SJens Axboe  * @ppos:	position in @out
637932cc6d4SJens Axboe  * @len:	how many bytes to splice
638932cc6d4SJens Axboe  * @flags:	splice modifier flags
639932cc6d4SJens Axboe  * @actor:	handler that splices the data
640932cc6d4SJens Axboe  *
641932cc6d4SJens Axboe  * Description:
6422933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
643932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
644932cc6d4SJens Axboe  *
645932cc6d4SJens Axboe  */
6466da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6476da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6486da61809SMark Fasheh 			 splice_actor *actor)
6496da61809SMark Fasheh {
6506da61809SMark Fasheh 	ssize_t ret;
651c66ab6faSJens Axboe 	struct splice_desc sd = {
652c66ab6faSJens Axboe 		.total_len = len,
653c66ab6faSJens Axboe 		.flags = flags,
654c66ab6faSJens Axboe 		.pos = *ppos,
6556a14b90bSJens Axboe 		.u.file = out,
656c66ab6faSJens Axboe 	};
6576da61809SMark Fasheh 
65861e0d47cSMiklos Szeredi 	pipe_lock(pipe);
659c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
66061e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6616da61809SMark Fasheh 
6626da61809SMark Fasheh 	return ret;
6636da61809SMark Fasheh }
6646da61809SMark Fasheh 
6656da61809SMark Fasheh /**
6668d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6678d020765SAl Viro  * @pipe:	pipe info
6688d020765SAl Viro  * @out:	file to write to
6698d020765SAl Viro  * @ppos:	position in @out
6708d020765SAl Viro  * @len:	number of bytes to splice
6718d020765SAl Viro  * @flags:	splice modifier flags
6728d020765SAl Viro  *
6738d020765SAl Viro  * Description:
6748d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
6758d020765SAl Viro  *    the given pipe inode to the given file.
6768d020765SAl Viro  *    This one is ->write_iter-based.
6778d020765SAl Viro  *
6788d020765SAl Viro  */
6798d020765SAl Viro ssize_t
6808d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
6818d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
6828d020765SAl Viro {
6838d020765SAl Viro 	struct splice_desc sd = {
6848d020765SAl Viro 		.total_len = len,
6858d020765SAl Viro 		.flags = flags,
6868d020765SAl Viro 		.pos = *ppos,
6878d020765SAl Viro 		.u.file = out,
6888d020765SAl Viro 	};
6898d020765SAl Viro 	int nbufs = pipe->buffers;
6908d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
6918d020765SAl Viro 					GFP_KERNEL);
6928d020765SAl Viro 	ssize_t ret;
6938d020765SAl Viro 
6948d020765SAl Viro 	if (unlikely(!array))
6958d020765SAl Viro 		return -ENOMEM;
6968d020765SAl Viro 
6978d020765SAl Viro 	pipe_lock(pipe);
6988d020765SAl Viro 
6998d020765SAl Viro 	splice_from_pipe_begin(&sd);
7008d020765SAl Viro 	while (sd.total_len) {
7018d020765SAl Viro 		struct iov_iter from;
7028d020765SAl Viro 		size_t left;
7038d020765SAl Viro 		int n, idx;
7048d020765SAl Viro 
7058d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7068d020765SAl Viro 		if (ret <= 0)
7078d020765SAl Viro 			break;
7088d020765SAl Viro 
7098d020765SAl Viro 		if (unlikely(nbufs < pipe->buffers)) {
7108d020765SAl Viro 			kfree(array);
7118d020765SAl Viro 			nbufs = pipe->buffers;
7128d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7138d020765SAl Viro 					GFP_KERNEL);
7148d020765SAl Viro 			if (!array) {
7158d020765SAl Viro 				ret = -ENOMEM;
7168d020765SAl Viro 				break;
7178d020765SAl Viro 			}
7188d020765SAl Viro 		}
7198d020765SAl Viro 
7208d020765SAl Viro 		/* build the vector */
7218d020765SAl Viro 		left = sd.total_len;
7228d020765SAl Viro 		for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
7238d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + idx;
7248d020765SAl Viro 			size_t this_len = buf->len;
7258d020765SAl Viro 
7268d020765SAl Viro 			if (this_len > left)
7278d020765SAl Viro 				this_len = left;
7288d020765SAl Viro 
7298d020765SAl Viro 			if (idx == pipe->buffers - 1)
7308d020765SAl Viro 				idx = -1;
7318d020765SAl Viro 
732fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7338d020765SAl Viro 			if (unlikely(ret)) {
7348d020765SAl Viro 				if (ret == -ENODATA)
7358d020765SAl Viro 					ret = 0;
7368d020765SAl Viro 				goto done;
7378d020765SAl Viro 			}
7388d020765SAl Viro 
7398d020765SAl Viro 			array[n].bv_page = buf->page;
7408d020765SAl Viro 			array[n].bv_len = this_len;
7418d020765SAl Viro 			array[n].bv_offset = buf->offset;
7428d020765SAl Viro 			left -= this_len;
7438d020765SAl Viro 		}
7448d020765SAl Viro 
745aa563d7bSDavid Howells 		iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
746abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7478d020765SAl Viro 		if (ret <= 0)
7488d020765SAl Viro 			break;
7498d020765SAl Viro 
7508d020765SAl Viro 		sd.num_spliced += ret;
7518d020765SAl Viro 		sd.total_len -= ret;
752dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7538d020765SAl Viro 
7548d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7558d020765SAl Viro 		while (ret) {
7568d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
7578d020765SAl Viro 			if (ret >= buf->len) {
7588d020765SAl Viro 				ret -= buf->len;
7598d020765SAl Viro 				buf->len = 0;
760a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7618d020765SAl Viro 				pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
7628d020765SAl Viro 				pipe->nrbufs--;
7638d020765SAl Viro 				if (pipe->files)
7648d020765SAl Viro 					sd.need_wakeup = true;
7658d020765SAl Viro 			} else {
7668d020765SAl Viro 				buf->offset += ret;
7678d020765SAl Viro 				buf->len -= ret;
7688d020765SAl Viro 				ret = 0;
7698d020765SAl Viro 			}
7708d020765SAl Viro 		}
7718d020765SAl Viro 	}
7728d020765SAl Viro done:
7738d020765SAl Viro 	kfree(array);
7748d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
7758d020765SAl Viro 
7768d020765SAl Viro 	pipe_unlock(pipe);
7778d020765SAl Viro 
7788d020765SAl Viro 	if (sd.num_spliced)
7798d020765SAl Viro 		ret = sd.num_spliced;
7808d020765SAl Viro 
7818d020765SAl Viro 	return ret;
7828d020765SAl Viro }
7838d020765SAl Viro 
7848d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
7858d020765SAl Viro 
786b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
787b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
7880b0a47f5SMiklos Szeredi {
789b2858d7dSMiklos Szeredi 	int ret;
790b2858d7dSMiklos Szeredi 	void *data;
79106ae43f3SAl Viro 	loff_t tmp = sd->pos;
792b2858d7dSMiklos Szeredi 
793fbb32750SAl Viro 	data = kmap(buf->page);
79406ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
795fbb32750SAl Viro 	kunmap(buf->page);
796b2858d7dSMiklos Szeredi 
797b2858d7dSMiklos Szeredi 	return ret;
7980b0a47f5SMiklos Szeredi }
7990b0a47f5SMiklos Szeredi 
8000b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
8010b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
8020b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
8030b0a47f5SMiklos Szeredi {
804b2858d7dSMiklos Szeredi 	ssize_t ret;
8050b0a47f5SMiklos Szeredi 
806b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
807b2858d7dSMiklos Szeredi 	if (ret > 0)
808b2858d7dSMiklos Szeredi 		*ppos += ret;
8090b0a47f5SMiklos Szeredi 
810b2858d7dSMiklos Szeredi 	return ret;
8110b0a47f5SMiklos Szeredi }
8120b0a47f5SMiklos Szeredi 
81383f9135bSJens Axboe /**
81483f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
815932cc6d4SJens Axboe  * @pipe:	pipe to splice from
81683f9135bSJens Axboe  * @out:	socket to write to
817932cc6d4SJens Axboe  * @ppos:	position in @out
81883f9135bSJens Axboe  * @len:	number of bytes to splice
81983f9135bSJens Axboe  * @flags:	splice modifier flags
82083f9135bSJens Axboe  *
821932cc6d4SJens Axboe  * Description:
82283f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
82383f9135bSJens Axboe  *    is involved.
82483f9135bSJens Axboe  *
82583f9135bSJens Axboe  */
8263a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
827cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8285274f052SJens Axboe {
82900522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8305274f052SJens Axboe }
8315274f052SJens Axboe 
832059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
833a0f06780SJeff Garzik 
83483f9135bSJens Axboe /*
83583f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
83683f9135bSJens Axboe  */
8373a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
838cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8395274f052SJens Axboe {
8400b0a47f5SMiklos Szeredi 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
8410b0a47f5SMiklos Szeredi 				loff_t *, size_t, unsigned int);
8425274f052SJens Axboe 
84372c2d531SAl Viro 	if (out->f_op->splice_write)
8440b0a47f5SMiklos Szeredi 		splice_write = out->f_op->splice_write;
845cc56f7deSChangli Gao 	else
8460b0a47f5SMiklos Szeredi 		splice_write = default_file_splice_write;
8470b0a47f5SMiklos Szeredi 
848500368f7SAl Viro 	return splice_write(pipe, out, ppos, len, flags);
8495274f052SJens Axboe }
8505274f052SJens Axboe 
85183f9135bSJens Axboe /*
85283f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
85383f9135bSJens Axboe  */
854cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
855cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
856cbb7e577SJens Axboe 			 unsigned int flags)
8575274f052SJens Axboe {
8586818173bSMiklos Szeredi 	ssize_t (*splice_read)(struct file *, loff_t *,
8596818173bSMiklos Szeredi 			       struct pipe_inode_info *, size_t, unsigned int);
8605274f052SJens Axboe 	int ret;
8615274f052SJens Axboe 
86249570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8635274f052SJens Axboe 		return -EBADF;
8645274f052SJens Axboe 
865cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8665274f052SJens Axboe 	if (unlikely(ret < 0))
8675274f052SJens Axboe 		return ret;
8685274f052SJens Axboe 
86903cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
87003cc0789SAl Viro 		len = MAX_RW_COUNT;
87103cc0789SAl Viro 
87272c2d531SAl Viro 	if (in->f_op->splice_read)
8736818173bSMiklos Szeredi 		splice_read = in->f_op->splice_read;
874cc56f7deSChangli Gao 	else
8756818173bSMiklos Szeredi 		splice_read = default_file_splice_read;
8766818173bSMiklos Szeredi 
8776818173bSMiklos Szeredi 	return splice_read(in, ppos, pipe, len, flags);
8785274f052SJens Axboe }
8795274f052SJens Axboe 
880932cc6d4SJens Axboe /**
881932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
882932cc6d4SJens Axboe  * @in:		file to splice from
883932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
884932cc6d4SJens Axboe  * @actor:	handles the data splicing
885932cc6d4SJens Axboe  *
886932cc6d4SJens Axboe  * Description:
887932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
888932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
889932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
890932cc6d4SJens Axboe  *    that process.
891932cc6d4SJens Axboe  *
892c66ab6faSJens Axboe  */
893c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
894c66ab6faSJens Axboe 			       splice_direct_actor *actor)
895b92ce558SJens Axboe {
896b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
897b92ce558SJens Axboe 	long ret, bytes;
898b92ce558SJens Axboe 	umode_t i_mode;
899c66ab6faSJens Axboe 	size_t len;
9000ff28d9fSChristophe Leroy 	int i, flags, more;
901b92ce558SJens Axboe 
902b92ce558SJens Axboe 	/*
903b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
904b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
905b92ce558SJens Axboe 	 * piped splicing for that!
906b92ce558SJens Axboe 	 */
907496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
908b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
909b92ce558SJens Axboe 		return -EINVAL;
910b92ce558SJens Axboe 
911b92ce558SJens Axboe 	/*
912b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
913b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
914b92ce558SJens Axboe 	 */
915b92ce558SJens Axboe 	pipe = current->splice_pipe;
91649570e9bSJens Axboe 	if (unlikely(!pipe)) {
9177bee130eSAl Viro 		pipe = alloc_pipe_info();
918b92ce558SJens Axboe 		if (!pipe)
919b92ce558SJens Axboe 			return -ENOMEM;
920b92ce558SJens Axboe 
921b92ce558SJens Axboe 		/*
922b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
92300522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
924b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
925b92ce558SJens Axboe 		 */
926b92ce558SJens Axboe 		pipe->readers = 1;
927b92ce558SJens Axboe 
928b92ce558SJens Axboe 		current->splice_pipe = pipe;
929b92ce558SJens Axboe 	}
930b92ce558SJens Axboe 
931b92ce558SJens Axboe 	/*
93273d62d83SIngo Molnar 	 * Do the splice.
933b92ce558SJens Axboe 	 */
934b92ce558SJens Axboe 	ret = 0;
935b92ce558SJens Axboe 	bytes = 0;
936c66ab6faSJens Axboe 	len = sd->total_len;
937c66ab6faSJens Axboe 	flags = sd->flags;
938c66ab6faSJens Axboe 
939c66ab6faSJens Axboe 	/*
940c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
941c66ab6faSJens Axboe 	 */
942c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9430ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
944b92ce558SJens Axboe 
94517614445SDarrick J. Wong 	WARN_ON_ONCE(pipe->nrbufs != 0);
94617614445SDarrick J. Wong 
947b92ce558SJens Axboe 	while (len) {
94851a92c0fSJens Axboe 		size_t read_len;
949a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
950b92ce558SJens Axboe 
95117614445SDarrick J. Wong 		/* Don't try to read more the pipe has space for. */
95217614445SDarrick J. Wong 		read_len = min_t(size_t, len,
95317614445SDarrick J. Wong 				 (pipe->buffers - pipe->nrbufs) << PAGE_SHIFT);
95417614445SDarrick J. Wong 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
95551a92c0fSJens Axboe 		if (unlikely(ret <= 0))
956b92ce558SJens Axboe 			goto out_release;
957b92ce558SJens Axboe 
958b92ce558SJens Axboe 		read_len = ret;
959c66ab6faSJens Axboe 		sd->total_len = read_len;
960b92ce558SJens Axboe 
961b92ce558SJens Axboe 		/*
9620ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9630ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9640ff28d9fSChristophe Leroy 		 * initially, clears it.
9650ff28d9fSChristophe Leroy 		 */
9660ff28d9fSChristophe Leroy 		if (read_len < len)
9670ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9680ff28d9fSChristophe Leroy 		else if (!more)
9690ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9700ff28d9fSChristophe Leroy 		/*
971b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
972b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
973b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
974b92ce558SJens Axboe 		 */
975c66ab6faSJens Axboe 		ret = actor(pipe, sd);
976a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
977a82c53a0STom Zanussi 			sd->pos = prev_pos;
978b92ce558SJens Axboe 			goto out_release;
979a82c53a0STom Zanussi 		}
980b92ce558SJens Axboe 
981b92ce558SJens Axboe 		bytes += ret;
982b92ce558SJens Axboe 		len -= ret;
983bcd4f3acSJens Axboe 		sd->pos = pos;
984b92ce558SJens Axboe 
985a82c53a0STom Zanussi 		if (ret < read_len) {
986a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
98751a92c0fSJens Axboe 			goto out_release;
988b92ce558SJens Axboe 		}
989a82c53a0STom Zanussi 	}
990b92ce558SJens Axboe 
9919e97198dSJens Axboe done:
992b92ce558SJens Axboe 	pipe->nrbufs = pipe->curbuf = 0;
9939e97198dSJens Axboe 	file_accessed(in);
994b92ce558SJens Axboe 	return bytes;
995b92ce558SJens Axboe 
996b92ce558SJens Axboe out_release:
997b92ce558SJens Axboe 	/*
998b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
999b92ce558SJens Axboe 	 * the pipe buffers in question:
1000b92ce558SJens Axboe 	 */
100135f3d14dSJens Axboe 	for (i = 0; i < pipe->buffers; i++) {
1002b92ce558SJens Axboe 		struct pipe_buffer *buf = pipe->bufs + i;
1003b92ce558SJens Axboe 
1004a779638cSMiklos Szeredi 		if (buf->ops)
1005a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1006b92ce558SJens Axboe 	}
1007b92ce558SJens Axboe 
10089e97198dSJens Axboe 	if (!bytes)
10099e97198dSJens Axboe 		bytes = ret;
1010b92ce558SJens Axboe 
10119e97198dSJens Axboe 	goto done;
1012c66ab6faSJens Axboe }
1013c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1014c66ab6faSJens Axboe 
1015c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1016c66ab6faSJens Axboe 			       struct splice_desc *sd)
1017c66ab6faSJens Axboe {
10186a14b90bSJens Axboe 	struct file *file = sd->u.file;
1019c66ab6faSJens Axboe 
10207995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10212cb4b05eSChangli Gao 			      sd->flags);
1022c66ab6faSJens Axboe }
1023c66ab6faSJens Axboe 
1024932cc6d4SJens Axboe /**
1025932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1026932cc6d4SJens Axboe  * @in:		file to splice from
1027932cc6d4SJens Axboe  * @ppos:	input file offset
1028932cc6d4SJens Axboe  * @out:	file to splice to
1029acdb37c3SRandy Dunlap  * @opos:	output file offset
1030932cc6d4SJens Axboe  * @len:	number of bytes to splice
1031932cc6d4SJens Axboe  * @flags:	splice modifier flags
1032932cc6d4SJens Axboe  *
1033932cc6d4SJens Axboe  * Description:
1034932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1035932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1036932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1037932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1038932cc6d4SJens Axboe  *
1039932cc6d4SJens Axboe  */
1040c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10417995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1042c66ab6faSJens Axboe {
1043c66ab6faSJens Axboe 	struct splice_desc sd = {
1044c66ab6faSJens Axboe 		.len		= len,
1045c66ab6faSJens Axboe 		.total_len	= len,
1046c66ab6faSJens Axboe 		.flags		= flags,
1047c66ab6faSJens Axboe 		.pos		= *ppos,
10486a14b90bSJens Axboe 		.u.file		= out,
10497995bd28SAl Viro 		.opos		= opos,
1050c66ab6faSJens Axboe 	};
105151a92c0fSJens Axboe 	long ret;
1052c66ab6faSJens Axboe 
105318c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
105418c67cb9SAl Viro 		return -EBADF;
105518c67cb9SAl Viro 
105618c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
105718c67cb9SAl Viro 		return -EINVAL;
105818c67cb9SAl Viro 
105918c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
106018c67cb9SAl Viro 	if (unlikely(ret < 0))
106118c67cb9SAl Viro 		return ret;
106218c67cb9SAl Viro 
1063c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
106451a92c0fSJens Axboe 	if (ret > 0)
1065a82c53a0STom Zanussi 		*ppos = sd.pos;
106651a92c0fSJens Axboe 
1067c66ab6faSJens Axboe 	return ret;
1068b92ce558SJens Axboe }
10691c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1070b92ce558SJens Axboe 
10718924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10728924feffSAl Viro {
107352bce911SLinus Torvalds 	for (;;) {
107452bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
107552bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
107652bce911SLinus Torvalds 			return -EPIPE;
107752bce911SLinus Torvalds 		}
107852bce911SLinus Torvalds 		if (pipe->nrbufs != pipe->buffers)
107952bce911SLinus Torvalds 			return 0;
10808924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10818924feffSAl Viro 			return -EAGAIN;
10828924feffSAl Viro 		if (signal_pending(current))
10838924feffSAl Viro 			return -ERESTARTSYS;
10848924feffSAl Viro 		pipe->waiting_writers++;
10858924feffSAl Viro 		pipe_wait(pipe);
10868924feffSAl Viro 		pipe->waiting_writers--;
10878924feffSAl Viro 	}
10888924feffSAl Viro }
10898924feffSAl Viro 
10907c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
10917c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
10927c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1093ddac0d39SJens Axboe 
1094ddac0d39SJens Axboe /*
109583f9135bSJens Axboe  * Determine where to splice to/from.
109683f9135bSJens Axboe  */
1097529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in,
1098529565dcSIngo Molnar 		      struct file *out, loff_t __user *off_out,
1099529565dcSIngo Molnar 		      size_t len, unsigned int flags)
11005274f052SJens Axboe {
11017c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11027c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11037995bd28SAl Viro 	loff_t offset;
1104a4514ebdSJens Axboe 	long ret;
11055274f052SJens Axboe 
110671993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
110771993e62SLinus Torvalds 	opipe = get_pipe_info(out);
11087c77f0b3SMiklos Szeredi 
11097c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11107c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11117c77f0b3SMiklos Szeredi 			return -ESPIPE;
11127c77f0b3SMiklos Szeredi 
11137c77f0b3SMiklos Szeredi 		if (!(in->f_mode & FMODE_READ))
11147c77f0b3SMiklos Szeredi 			return -EBADF;
11157c77f0b3SMiklos Szeredi 
11167c77f0b3SMiklos Szeredi 		if (!(out->f_mode & FMODE_WRITE))
11177c77f0b3SMiklos Szeredi 			return -EBADF;
11187c77f0b3SMiklos Szeredi 
11197c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11207c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11217c77f0b3SMiklos Szeredi 			return -EINVAL;
11227c77f0b3SMiklos Szeredi 
1123ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1124ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1125ee5e0011SSlavomir Kaslev 
11267c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11277c77f0b3SMiklos Szeredi 	}
11287c77f0b3SMiklos Szeredi 
11297c77f0b3SMiklos Szeredi 	if (ipipe) {
1130529565dcSIngo Molnar 		if (off_in)
1131529565dcSIngo Molnar 			return -ESPIPE;
1132b92ce558SJens Axboe 		if (off_out) {
113319c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1134b92ce558SJens Axboe 				return -EINVAL;
1135cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1136b92ce558SJens Axboe 				return -EFAULT;
11377995bd28SAl Viro 		} else {
11387995bd28SAl Viro 			offset = out->f_pos;
11397995bd28SAl Viro 		}
1140529565dcSIngo Molnar 
114118c67cb9SAl Viro 		if (unlikely(!(out->f_mode & FMODE_WRITE)))
114218c67cb9SAl Viro 			return -EBADF;
114318c67cb9SAl Viro 
114418c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
114518c67cb9SAl Viro 			return -EINVAL;
114618c67cb9SAl Viro 
114718c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
114818c67cb9SAl Viro 		if (unlikely(ret < 0))
114918c67cb9SAl Viro 			return ret;
115018c67cb9SAl Viro 
1151ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1152ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1153ee5e0011SSlavomir Kaslev 
1154500368f7SAl Viro 		file_start_write(out);
11557995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1156500368f7SAl Viro 		file_end_write(out);
1157a4514ebdSJens Axboe 
11587995bd28SAl Viro 		if (!off_out)
11597995bd28SAl Viro 			out->f_pos = offset;
11607995bd28SAl Viro 		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1161a4514ebdSJens Axboe 			ret = -EFAULT;
1162a4514ebdSJens Axboe 
1163a4514ebdSJens Axboe 		return ret;
1164529565dcSIngo Molnar 	}
11655274f052SJens Axboe 
11667c77f0b3SMiklos Szeredi 	if (opipe) {
1167529565dcSIngo Molnar 		if (off_out)
1168529565dcSIngo Molnar 			return -ESPIPE;
1169b92ce558SJens Axboe 		if (off_in) {
117019c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1171b92ce558SJens Axboe 				return -EINVAL;
1172cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1173b92ce558SJens Axboe 				return -EFAULT;
11747995bd28SAl Viro 		} else {
11757995bd28SAl Viro 			offset = in->f_pos;
11767995bd28SAl Viro 		}
1177529565dcSIngo Molnar 
1178ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1179ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1180ee5e0011SSlavomir Kaslev 
11818924feffSAl Viro 		pipe_lock(opipe);
11828924feffSAl Viro 		ret = wait_for_space(opipe, flags);
11838924feffSAl Viro 		if (!ret)
11847995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
11858924feffSAl Viro 		pipe_unlock(opipe);
11868924feffSAl Viro 		if (ret > 0)
11878924feffSAl Viro 			wakeup_pipe_readers(opipe);
11887995bd28SAl Viro 		if (!off_in)
11897995bd28SAl Viro 			in->f_pos = offset;
11907995bd28SAl Viro 		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1191a4514ebdSJens Axboe 			ret = -EFAULT;
1192a4514ebdSJens Axboe 
1193a4514ebdSJens Axboe 		return ret;
1194529565dcSIngo Molnar 	}
11955274f052SJens Axboe 
11965274f052SJens Axboe 	return -EINVAL;
11975274f052SJens Axboe }
11985274f052SJens Axboe 
119979fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
120079fddc4eSAl Viro 			struct pipe_inode_info *pipe,
120179fddc4eSAl Viro 			unsigned flags)
1202912d35f8SJens Axboe {
120379fddc4eSAl Viro 	struct pipe_buffer buf = {
120479fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
120579fddc4eSAl Viro 		.flags = flags
120679fddc4eSAl Viro 	};
120779fddc4eSAl Viro 	size_t total = 0;
120879fddc4eSAl Viro 	int ret = 0;
120979fddc4eSAl Viro 	bool failed = false;
121079fddc4eSAl Viro 
121179fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
121279fddc4eSAl Viro 		struct page *pages[16];
1213db85a9ebSAl Viro 		ssize_t copied;
1214db85a9ebSAl Viro 		size_t start;
121579fddc4eSAl Viro 		int n;
1216912d35f8SJens Axboe 
121779fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
121879fddc4eSAl Viro 		if (copied <= 0) {
121979fddc4eSAl Viro 			ret = copied;
122079fddc4eSAl Viro 			break;
122179fddc4eSAl Viro 		}
1222912d35f8SJens Axboe 
122379fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1224db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
122579fddc4eSAl Viro 			if (!failed) {
122679fddc4eSAl Viro 				buf.page = pages[n];
122779fddc4eSAl Viro 				buf.offset = start;
122879fddc4eSAl Viro 				buf.len = size;
122979fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
123079fddc4eSAl Viro 				if (unlikely(ret < 0)) {
123179fddc4eSAl Viro 					failed = true;
123279fddc4eSAl Viro 				} else {
123379fddc4eSAl Viro 					iov_iter_advance(from, ret);
123479fddc4eSAl Viro 					total += ret;
123579fddc4eSAl Viro 				}
123679fddc4eSAl Viro 			} else {
123779fddc4eSAl Viro 				put_page(pages[n]);
123879fddc4eSAl Viro 			}
1239db85a9ebSAl Viro 			copied -= size;
1240912d35f8SJens Axboe 		}
1241912d35f8SJens Axboe 	}
124279fddc4eSAl Viro 	return total ? total : ret;
1243912d35f8SJens Axboe }
1244912d35f8SJens Axboe 
12456a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12466a14b90bSJens Axboe 			struct splice_desc *sd)
12476a14b90bSJens Axboe {
12486130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
12496130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
12506a14b90bSJens Axboe }
12516a14b90bSJens Axboe 
12526a14b90bSJens Axboe /*
12536a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
12546a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
12556a14b90bSJens Axboe  */
125687a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
125787a3002aSAl Viro 			     unsigned int flags)
12586a14b90bSJens Axboe {
125987a3002aSAl Viro 	struct pipe_inode_info *pipe = get_pipe_info(file);
126087a3002aSAl Viro 	struct splice_desc sd = {
126187a3002aSAl Viro 		.total_len = iov_iter_count(iter),
126287a3002aSAl Viro 		.flags = flags,
126387a3002aSAl Viro 		.u.data = iter
126487a3002aSAl Viro 	};
126587a3002aSAl Viro 	long ret = 0;
12666a14b90bSJens Axboe 
12676a14b90bSJens Axboe 	if (!pipe)
12686a14b90bSJens Axboe 		return -EBADF;
12696a14b90bSJens Axboe 
1270345995faSAl Viro 	if (sd.total_len) {
12716130f531SAl Viro 		pipe_lock(pipe);
12726130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
127361e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1274345995faSAl Viro 	}
12756a14b90bSJens Axboe 
12766a14b90bSJens Axboe 	return ret;
12776a14b90bSJens Axboe }
12786a14b90bSJens Axboe 
1279912d35f8SJens Axboe /*
1280912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1281912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1282912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1283912d35f8SJens Axboe  */
128487a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
128587a3002aSAl Viro 			     unsigned int flags)
1286912d35f8SJens Axboe {
1287ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
128887a3002aSAl Viro 	long ret = 0;
128979fddc4eSAl Viro 	unsigned buf_flag = 0;
129079fddc4eSAl Viro 
129179fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
129279fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1293912d35f8SJens Axboe 
129471993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1295ddac0d39SJens Axboe 	if (!pipe)
1296912d35f8SJens Axboe 		return -EBADF;
1297912d35f8SJens Axboe 
12988924feffSAl Viro 	pipe_lock(pipe);
12998924feffSAl Viro 	ret = wait_for_space(pipe, flags);
130079fddc4eSAl Viro 	if (!ret)
130187a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13028924feffSAl Viro 	pipe_unlock(pipe);
13038924feffSAl Viro 	if (ret > 0)
13048924feffSAl Viro 		wakeup_pipe_readers(pipe);
130535f3d14dSJens Axboe 	return ret;
1306912d35f8SJens Axboe }
1307912d35f8SJens Axboe 
130887a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
130987a3002aSAl Viro {
131087a3002aSAl Viro 	if (!f.file)
131187a3002aSAl Viro 		return -EBADF;
131287a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
131387a3002aSAl Viro 		*type = WRITE;
131487a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
131587a3002aSAl Viro 		*type = READ;
131687a3002aSAl Viro 	} else {
131787a3002aSAl Viro 		fdput(f);
131887a3002aSAl Viro 		return -EBADF;
131987a3002aSAl Viro 	}
132087a3002aSAl Viro 	return 0;
132187a3002aSAl Viro }
132287a3002aSAl Viro 
13236a14b90bSJens Axboe /*
13246a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13256a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13266a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13276a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13286a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13296a14b90bSJens Axboe  * solutions for that:
13306a14b90bSJens Axboe  *
13316a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13326a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13336a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13346a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13356a14b90bSJens Axboe  *
13366a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13376a14b90bSJens Axboe  *
13386a14b90bSJens Axboe  */
133987a3002aSAl Viro static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
1340912d35f8SJens Axboe {
13413d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
13423d6ea290SAl Viro 		return -EINVAL;
134387a3002aSAl Viro 
134487a3002aSAl Viro 	if (!iov_iter_count(iter))
13456a14b90bSJens Axboe 		return 0;
13466a14b90bSJens Axboe 
134787a3002aSAl Viro 	if (iov_iter_rw(iter) == WRITE)
134887a3002aSAl Viro 		return vmsplice_to_pipe(f, iter, flags);
134987a3002aSAl Viro 	else
135087a3002aSAl Viro 		return vmsplice_to_user(f, iter, flags);
1351912d35f8SJens Axboe }
1352912d35f8SJens Axboe 
135387a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
135430cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
135530cfe4efSDominik Brodowski {
135687a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
135787a3002aSAl Viro 	struct iovec *iov = iovstack;
135887a3002aSAl Viro 	struct iov_iter iter;
135987a3002aSAl Viro 	long error;
136087a3002aSAl Viro 	struct fd f;
136187a3002aSAl Viro 	int type;
136287a3002aSAl Viro 
136387a3002aSAl Viro 	f = fdget(fd);
136487a3002aSAl Viro 	error = vmsplice_type(f, &type);
136587a3002aSAl Viro 	if (error)
136687a3002aSAl Viro 		return error;
136787a3002aSAl Viro 
136887a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
136987a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
137087a3002aSAl Viro 	if (!error) {
137187a3002aSAl Viro 		error = do_vmsplice(f.file, &iter, flags);
137287a3002aSAl Viro 		kfree(iov);
137387a3002aSAl Viro 	}
137487a3002aSAl Viro 	fdput(f);
137587a3002aSAl Viro 	return error;
137630cfe4efSDominik Brodowski }
137730cfe4efSDominik Brodowski 
137876b021d0SAl Viro #ifdef CONFIG_COMPAT
137976b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
138076b021d0SAl Viro 		    unsigned int, nr_segs, unsigned int, flags)
138176b021d0SAl Viro {
138287a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
138387a3002aSAl Viro 	struct iovec *iov = iovstack;
138487a3002aSAl Viro 	struct iov_iter iter;
138587a3002aSAl Viro 	long error;
138687a3002aSAl Viro 	struct fd f;
138787a3002aSAl Viro 	int type;
138887a3002aSAl Viro 
138987a3002aSAl Viro 	f = fdget(fd);
139087a3002aSAl Viro 	error = vmsplice_type(f, &type);
139187a3002aSAl Viro 	if (error)
139287a3002aSAl Viro 		return error;
139387a3002aSAl Viro 
139487a3002aSAl Viro 	error = compat_import_iovec(type, iov32, nr_segs,
139587a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
139687a3002aSAl Viro 	if (!error) {
139787a3002aSAl Viro 		error = do_vmsplice(f.file, &iter, flags);
139887a3002aSAl Viro 		kfree(iov);
139976b021d0SAl Viro 	}
140087a3002aSAl Viro 	fdput(f);
140187a3002aSAl Viro 	return error;
140276b021d0SAl Viro }
140376b021d0SAl Viro #endif
140476b021d0SAl Viro 
1405836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1406836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1407836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14085274f052SJens Axboe {
14092903ff01SAl Viro 	struct fd in, out;
14105274f052SJens Axboe 	long error;
14115274f052SJens Axboe 
14125274f052SJens Axboe 	if (unlikely(!len))
14135274f052SJens Axboe 		return 0;
14145274f052SJens Axboe 
14153d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14163d6ea290SAl Viro 		return -EINVAL;
14173d6ea290SAl Viro 
14185274f052SJens Axboe 	error = -EBADF;
14192903ff01SAl Viro 	in = fdget(fd_in);
14202903ff01SAl Viro 	if (in.file) {
14212903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
14222903ff01SAl Viro 			out = fdget(fd_out);
14232903ff01SAl Viro 			if (out.file) {
14242903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
14252903ff01SAl Viro 					error = do_splice(in.file, off_in,
14262903ff01SAl Viro 							  out.file, off_out,
1427529565dcSIngo Molnar 							  len, flags);
14282903ff01SAl Viro 				fdput(out);
14295274f052SJens Axboe 			}
14305274f052SJens Axboe 		}
14312903ff01SAl Viro 		fdput(in);
14325274f052SJens Axboe 	}
14335274f052SJens Axboe 	return error;
14345274f052SJens Axboe }
143570524490SJens Axboe 
143670524490SJens Axboe /*
1437aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1438aadd06e5SJens Axboe  * return an appropriate error.
1439aadd06e5SJens Axboe  */
14407c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1441aadd06e5SJens Axboe {
1442aadd06e5SJens Axboe 	int ret;
1443aadd06e5SJens Axboe 
1444aadd06e5SJens Axboe 	/*
1445aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1446aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1447aadd06e5SJens Axboe 	 */
1448aadd06e5SJens Axboe 	if (pipe->nrbufs)
1449aadd06e5SJens Axboe 		return 0;
1450aadd06e5SJens Axboe 
1451aadd06e5SJens Axboe 	ret = 0;
145261e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1453aadd06e5SJens Axboe 
1454aadd06e5SJens Axboe 	while (!pipe->nrbufs) {
1455aadd06e5SJens Axboe 		if (signal_pending(current)) {
1456aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1457aadd06e5SJens Axboe 			break;
1458aadd06e5SJens Axboe 		}
1459aadd06e5SJens Axboe 		if (!pipe->writers)
1460aadd06e5SJens Axboe 			break;
1461aadd06e5SJens Axboe 		if (!pipe->waiting_writers) {
1462aadd06e5SJens Axboe 			if (flags & SPLICE_F_NONBLOCK) {
1463aadd06e5SJens Axboe 				ret = -EAGAIN;
1464aadd06e5SJens Axboe 				break;
1465aadd06e5SJens Axboe 			}
1466aadd06e5SJens Axboe 		}
1467aadd06e5SJens Axboe 		pipe_wait(pipe);
1468aadd06e5SJens Axboe 	}
1469aadd06e5SJens Axboe 
147061e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1471aadd06e5SJens Axboe 	return ret;
1472aadd06e5SJens Axboe }
1473aadd06e5SJens Axboe 
1474aadd06e5SJens Axboe /*
1475aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1476aadd06e5SJens Axboe  * return an appropriate error.
1477aadd06e5SJens Axboe  */
14787c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1479aadd06e5SJens Axboe {
1480aadd06e5SJens Axboe 	int ret;
1481aadd06e5SJens Axboe 
1482aadd06e5SJens Axboe 	/*
1483aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1484aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1485aadd06e5SJens Axboe 	 */
148635f3d14dSJens Axboe 	if (pipe->nrbufs < pipe->buffers)
1487aadd06e5SJens Axboe 		return 0;
1488aadd06e5SJens Axboe 
1489aadd06e5SJens Axboe 	ret = 0;
149061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1491aadd06e5SJens Axboe 
149235f3d14dSJens Axboe 	while (pipe->nrbufs >= pipe->buffers) {
1493aadd06e5SJens Axboe 		if (!pipe->readers) {
1494aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1495aadd06e5SJens Axboe 			ret = -EPIPE;
1496aadd06e5SJens Axboe 			break;
1497aadd06e5SJens Axboe 		}
1498aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1499aadd06e5SJens Axboe 			ret = -EAGAIN;
1500aadd06e5SJens Axboe 			break;
1501aadd06e5SJens Axboe 		}
1502aadd06e5SJens Axboe 		if (signal_pending(current)) {
1503aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1504aadd06e5SJens Axboe 			break;
1505aadd06e5SJens Axboe 		}
1506aadd06e5SJens Axboe 		pipe->waiting_writers++;
1507aadd06e5SJens Axboe 		pipe_wait(pipe);
1508aadd06e5SJens Axboe 		pipe->waiting_writers--;
1509aadd06e5SJens Axboe 	}
1510aadd06e5SJens Axboe 
151161e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1512aadd06e5SJens Axboe 	return ret;
1513aadd06e5SJens Axboe }
1514aadd06e5SJens Axboe 
1515aadd06e5SJens Axboe /*
15167c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15177c77f0b3SMiklos Szeredi  */
15187c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15197c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15207c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15217c77f0b3SMiklos Szeredi {
15227c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15237c77f0b3SMiklos Szeredi 	int ret = 0, nbuf;
15247c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15257c77f0b3SMiklos Szeredi 
15267c77f0b3SMiklos Szeredi 
15277c77f0b3SMiklos Szeredi retry:
15287c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15297c77f0b3SMiklos Szeredi 	if (ret)
15307c77f0b3SMiklos Szeredi 		return ret;
15317c77f0b3SMiklos Szeredi 
15327c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15337c77f0b3SMiklos Szeredi 	if (ret)
15347c77f0b3SMiklos Szeredi 		return ret;
15357c77f0b3SMiklos Szeredi 
15367c77f0b3SMiklos Szeredi 	/*
15377c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15387c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15397c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15407c77f0b3SMiklos Szeredi 	 */
15417c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15427c77f0b3SMiklos Szeredi 
15437c77f0b3SMiklos Szeredi 	do {
15447c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15457c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15467c77f0b3SMiklos Szeredi 			if (!ret)
15477c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15487c77f0b3SMiklos Szeredi 			break;
15497c77f0b3SMiklos Szeredi 		}
15507c77f0b3SMiklos Szeredi 
15517c77f0b3SMiklos Szeredi 		if (!ipipe->nrbufs && !ipipe->writers)
15527c77f0b3SMiklos Szeredi 			break;
15537c77f0b3SMiklos Szeredi 
15547c77f0b3SMiklos Szeredi 		/*
15557c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15567c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15577c77f0b3SMiklos Szeredi 		 */
155835f3d14dSJens Axboe 		if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
15597c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15607c77f0b3SMiklos Szeredi 			if (ret)
15617c77f0b3SMiklos Szeredi 				break;
15627c77f0b3SMiklos Szeredi 
15637c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15647c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15657c77f0b3SMiklos Szeredi 				break;
15667c77f0b3SMiklos Szeredi 			}
15677c77f0b3SMiklos Szeredi 
15687c77f0b3SMiklos Szeredi 			/*
15697c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15707c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15717c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
15727c77f0b3SMiklos Szeredi 			 */
15737c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
15747c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
15757c77f0b3SMiklos Szeredi 			goto retry;
15767c77f0b3SMiklos Szeredi 		}
15777c77f0b3SMiklos Szeredi 
15787c77f0b3SMiklos Szeredi 		ibuf = ipipe->bufs + ipipe->curbuf;
157935f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
15807c77f0b3SMiklos Szeredi 		obuf = opipe->bufs + nbuf;
15817c77f0b3SMiklos Szeredi 
15827c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
15837c77f0b3SMiklos Szeredi 			/*
15847c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
15857c77f0b3SMiklos Szeredi 			 */
15867c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
15877c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
15887c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
158935f3d14dSJens Axboe 			ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
15907c77f0b3SMiklos Szeredi 			ipipe->nrbufs--;
15917c77f0b3SMiklos Szeredi 			input_wakeup = true;
15927c77f0b3SMiklos Szeredi 		} else {
15937c77f0b3SMiklos Szeredi 			/*
15947c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
15957c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
15967c77f0b3SMiklos Szeredi 			 */
159715fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
159815fab63eSMatthew Wilcox 				if (ret == 0)
159915fab63eSMatthew Wilcox 					ret = -EFAULT;
160015fab63eSMatthew Wilcox 				break;
160115fab63eSMatthew Wilcox 			}
16027c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16037c77f0b3SMiklos Szeredi 
16047c77f0b3SMiklos Szeredi 			/*
16057c77f0b3SMiklos Szeredi 			 * Don't inherit the gift flag, we need to
16067c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16077c77f0b3SMiklos Szeredi 			 */
16087c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
16097c77f0b3SMiklos Szeredi 
1610a0ce2f0aSJann Horn 			pipe_buf_mark_unmergeable(obuf);
1611a0ce2f0aSJann Horn 
16127c77f0b3SMiklos Szeredi 			obuf->len = len;
16137c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
16147c77f0b3SMiklos Szeredi 			ibuf->offset += obuf->len;
16157c77f0b3SMiklos Szeredi 			ibuf->len -= obuf->len;
16167c77f0b3SMiklos Szeredi 		}
16177c77f0b3SMiklos Szeredi 		ret += obuf->len;
16187c77f0b3SMiklos Szeredi 		len -= obuf->len;
16197c77f0b3SMiklos Szeredi 	} while (len);
16207c77f0b3SMiklos Szeredi 
16217c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16227c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16237c77f0b3SMiklos Szeredi 
16247c77f0b3SMiklos Szeredi 	/*
16257c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16267c77f0b3SMiklos Szeredi 	 */
1627825cdcb1SNamhyung Kim 	if (ret > 0)
1628825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1629825cdcb1SNamhyung Kim 
16307c77f0b3SMiklos Szeredi 	if (input_wakeup)
16317c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16327c77f0b3SMiklos Szeredi 
16337c77f0b3SMiklos Szeredi 	return ret;
16347c77f0b3SMiklos Szeredi }
16357c77f0b3SMiklos Szeredi 
16367c77f0b3SMiklos Szeredi /*
163770524490SJens Axboe  * Link contents of ipipe to opipe.
163870524490SJens Axboe  */
163970524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
164070524490SJens Axboe 		     struct pipe_inode_info *opipe,
164170524490SJens Axboe 		     size_t len, unsigned int flags)
164270524490SJens Axboe {
164370524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
1644aadd06e5SJens Axboe 	int ret = 0, i = 0, nbuf;
164570524490SJens Axboe 
164670524490SJens Axboe 	/*
164770524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
164861e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
164970524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
165070524490SJens Axboe 	 */
165161e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
165270524490SJens Axboe 
1653aadd06e5SJens Axboe 	do {
165470524490SJens Axboe 		if (!opipe->readers) {
165570524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
165670524490SJens Axboe 			if (!ret)
165770524490SJens Axboe 				ret = -EPIPE;
165870524490SJens Axboe 			break;
165970524490SJens Axboe 		}
166070524490SJens Axboe 
166170524490SJens Axboe 		/*
1662aadd06e5SJens Axboe 		 * If we have iterated all input buffers or ran out of
1663aadd06e5SJens Axboe 		 * output room, break.
166470524490SJens Axboe 		 */
166535f3d14dSJens Axboe 		if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1666aadd06e5SJens Axboe 			break;
1667aadd06e5SJens Axboe 
166835f3d14dSJens Axboe 		ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
166935f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
167070524490SJens Axboe 
167170524490SJens Axboe 		/*
167270524490SJens Axboe 		 * Get a reference to this pipe buffer,
167370524490SJens Axboe 		 * so we can copy the contents over.
167470524490SJens Axboe 		 */
167515fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
167615fab63eSMatthew Wilcox 			if (ret == 0)
167715fab63eSMatthew Wilcox 				ret = -EFAULT;
167815fab63eSMatthew Wilcox 			break;
167915fab63eSMatthew Wilcox 		}
168070524490SJens Axboe 
168170524490SJens Axboe 		obuf = opipe->bufs + nbuf;
168270524490SJens Axboe 		*obuf = *ibuf;
168370524490SJens Axboe 
16847afa6fd0SJens Axboe 		/*
16857afa6fd0SJens Axboe 		 * Don't inherit the gift flag, we need to
16867afa6fd0SJens Axboe 		 * prevent multiple steals of this page.
16877afa6fd0SJens Axboe 		 */
16887afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
16897afa6fd0SJens Axboe 
1690a0ce2f0aSJann Horn 		pipe_buf_mark_unmergeable(obuf);
1691a0ce2f0aSJann Horn 
169270524490SJens Axboe 		if (obuf->len > len)
169370524490SJens Axboe 			obuf->len = len;
169470524490SJens Axboe 
169570524490SJens Axboe 		opipe->nrbufs++;
169670524490SJens Axboe 		ret += obuf->len;
169770524490SJens Axboe 		len -= obuf->len;
1698aadd06e5SJens Axboe 		i++;
1699aadd06e5SJens Axboe 	} while (len);
170070524490SJens Axboe 
170102cf01aeSJens Axboe 	/*
170202cf01aeSJens Axboe 	 * return EAGAIN if we have the potential of some data in the
170302cf01aeSJens Axboe 	 * future, otherwise just return 0
170402cf01aeSJens Axboe 	 */
170502cf01aeSJens Axboe 	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
170602cf01aeSJens Axboe 		ret = -EAGAIN;
170702cf01aeSJens Axboe 
170861e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
170961e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
171070524490SJens Axboe 
1711aadd06e5SJens Axboe 	/*
1712aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1713aadd06e5SJens Axboe 	 */
1714825cdcb1SNamhyung Kim 	if (ret > 0)
1715825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
171670524490SJens Axboe 
171770524490SJens Axboe 	return ret;
171870524490SJens Axboe }
171970524490SJens Axboe 
172070524490SJens Axboe /*
172170524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
172270524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
172370524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
172470524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
172570524490SJens Axboe  */
172670524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
172770524490SJens Axboe 		   unsigned int flags)
172870524490SJens Axboe {
172971993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
173071993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1731aadd06e5SJens Axboe 	int ret = -EINVAL;
173270524490SJens Axboe 
173370524490SJens Axboe 	/*
1734aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1735aadd06e5SJens Axboe 	 * copying the data.
173670524490SJens Axboe 	 */
1737aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1738ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1739ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1740ee5e0011SSlavomir Kaslev 
1741aadd06e5SJens Axboe 		/*
1742aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1743aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1744aadd06e5SJens Axboe 		 */
17457c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1746aadd06e5SJens Axboe 		if (!ret) {
17477c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
174802cf01aeSJens Axboe 			if (!ret)
1749aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1750aadd06e5SJens Axboe 		}
1751aadd06e5SJens Axboe 	}
175270524490SJens Axboe 
1753aadd06e5SJens Axboe 	return ret;
175470524490SJens Axboe }
175570524490SJens Axboe 
1756836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
175770524490SJens Axboe {
17582903ff01SAl Viro 	struct fd in;
17592903ff01SAl Viro 	int error;
176070524490SJens Axboe 
17613d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
17623d6ea290SAl Viro 		return -EINVAL;
17633d6ea290SAl Viro 
176470524490SJens Axboe 	if (unlikely(!len))
176570524490SJens Axboe 		return 0;
176670524490SJens Axboe 
176770524490SJens Axboe 	error = -EBADF;
17682903ff01SAl Viro 	in = fdget(fdin);
17692903ff01SAl Viro 	if (in.file) {
17702903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
17712903ff01SAl Viro 			struct fd out = fdget(fdout);
17722903ff01SAl Viro 			if (out.file) {
17732903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
17742903ff01SAl Viro 					error = do_tee(in.file, out.file,
17752903ff01SAl Viro 							len, flags);
17762903ff01SAl Viro 				fdput(out);
177770524490SJens Axboe 			}
177870524490SJens Axboe 		}
17792903ff01SAl Viro  		fdput(in);
178070524490SJens Axboe  	}
178170524490SJens Axboe 
178270524490SJens Axboe 	return error;
178370524490SJens Axboe }
1784