xref: /openbmc/linux/fs/splice.c (revision 983652c6)
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>
33*983652c6SChung-Chiang Cheng #include <linux/fsnotify.h>
3429ce2058SJames Morris #include <linux/security.h>
355a0e3ad6STejun Heo #include <linux/gfp.h>
3635f9c09fSEric Dumazet #include <linux/socket.h>
37174cd4b1SIngo Molnar #include <linux/sched/signal.h>
38174cd4b1SIngo Molnar 
3906ae43f3SAl Viro #include "internal.h"
405274f052SJens Axboe 
4183f9135bSJens Axboe /*
4283f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
4383f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
4483f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
4583f9135bSJens Axboe  * attempt to reuse this page for another destination.
4683f9135bSJens Axboe  */
47c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
485abc97aaSJens Axboe 		struct pipe_buffer *buf)
495abc97aaSJens Axboe {
505100da38SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(buf->page);
519e94cd4fSJens Axboe 	struct address_space *mapping;
525abc97aaSJens Axboe 
53b9ccad2eSMatthew Wilcox (Oracle) 	folio_lock(folio);
549e0267c2SJens Axboe 
55b9ccad2eSMatthew Wilcox (Oracle) 	mapping = folio_mapping(folio);
569e94cd4fSJens Axboe 	if (mapping) {
57b9ccad2eSMatthew Wilcox (Oracle) 		WARN_ON(!folio_test_uptodate(folio));
585abc97aaSJens Axboe 
59ad8d6f0aSJens Axboe 		/*
609e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
61b9ccad2eSMatthew Wilcox (Oracle) 		 * writeback completing on this folio, since we'll remove it
629e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
63b9ccad2eSMatthew Wilcox (Oracle) 		 * folio, 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 		 */
67b9ccad2eSMatthew Wilcox (Oracle) 		folio_wait_writeback(folio);
68ad8d6f0aSJens Axboe 
69b9ccad2eSMatthew Wilcox (Oracle) 		if (folio_has_private(folio) &&
70b9ccad2eSMatthew Wilcox (Oracle) 		    !filemap_release_folio(folio, 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 		 */
775100da38SMatthew Wilcox (Oracle) 		if (remove_mapping(mapping, folio)) {
781432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
79c928f642SChristoph Hellwig 			return true;
805abc97aaSJens Axboe 		}
819e94cd4fSJens Axboe 	}
829e94cd4fSJens Axboe 
839e94cd4fSJens Axboe 	/*
84b9ccad2eSMatthew Wilcox (Oracle) 	 * Raced with truncate or failed to remove folio from current
859e94cd4fSJens Axboe 	 * address space, unlock and return failure.
869e94cd4fSJens Axboe 	 */
87ca39d651SJens Axboe out_unlock:
88b9ccad2eSMatthew Wilcox (Oracle) 	folio_unlock(folio);
89c928f642SChristoph Hellwig 	return false;
909e94cd4fSJens Axboe }
915abc97aaSJens Axboe 
9276ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
935274f052SJens Axboe 					struct pipe_buffer *buf)
945274f052SJens Axboe {
9509cbfeafSKirill A. Shutemov 	put_page(buf->page);
961432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
975274f052SJens Axboe }
985274f052SJens Axboe 
990845718dSJens Axboe /*
1000845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
1010845718dSJens Axboe  * is a page cache page, IO may be in flight.
1020845718dSJens Axboe  */
103cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1045274f052SJens Axboe 				       struct pipe_buffer *buf)
1055274f052SJens Axboe {
1065274f052SJens Axboe 	struct page *page = buf->page;
10749d0b21bSJens Axboe 	int err;
1085274f052SJens Axboe 
1095274f052SJens Axboe 	if (!PageUptodate(page)) {
11049d0b21bSJens Axboe 		lock_page(page);
1115274f052SJens Axboe 
11249d0b21bSJens Axboe 		/*
11349d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
11473d62d83SIngo Molnar 		 * splice, if this is the first page.
11549d0b21bSJens Axboe 		 */
1165274f052SJens Axboe 		if (!page->mapping) {
11749d0b21bSJens Axboe 			err = -ENODATA;
11849d0b21bSJens Axboe 			goto error;
1195274f052SJens Axboe 		}
1205274f052SJens Axboe 
12149d0b21bSJens Axboe 		/*
12273d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
12349d0b21bSJens Axboe 		 */
12449d0b21bSJens Axboe 		if (!PageUptodate(page)) {
12549d0b21bSJens Axboe 			err = -EIO;
12649d0b21bSJens Axboe 			goto error;
12749d0b21bSJens Axboe 		}
12849d0b21bSJens Axboe 
12949d0b21bSJens Axboe 		/*
130f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
13149d0b21bSJens Axboe 		 */
13249d0b21bSJens Axboe 		unlock_page(page);
13349d0b21bSJens Axboe 	}
13449d0b21bSJens Axboe 
135f84d7519SJens Axboe 	return 0;
13649d0b21bSJens Axboe error:
13749d0b21bSJens Axboe 	unlock_page(page);
138f84d7519SJens Axboe 	return err;
13970524490SJens Axboe }
14070524490SJens Axboe 
141708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
142cac36bb0SJens Axboe 	.confirm	= page_cache_pipe_buf_confirm,
1435274f052SJens Axboe 	.release	= page_cache_pipe_buf_release,
144c928f642SChristoph Hellwig 	.try_steal	= page_cache_pipe_buf_try_steal,
145f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
1465274f052SJens Axboe };
1475274f052SJens Axboe 
148c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
149912d35f8SJens Axboe 		struct pipe_buffer *buf)
150912d35f8SJens Axboe {
1517afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152c928f642SChristoph Hellwig 		return false;
1537afa6fd0SJens Axboe 
1541432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
155c928f642SChristoph Hellwig 	return generic_pipe_buf_try_steal(pipe, buf);
156912d35f8SJens Axboe }
157912d35f8SJens Axboe 
158d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159912d35f8SJens Axboe 	.release	= page_cache_pipe_buf_release,
160c928f642SChristoph Hellwig 	.try_steal	= user_page_pipe_buf_try_steal,
161f84d7519SJens Axboe 	.get		= generic_pipe_buf_get,
162912d35f8SJens Axboe };
163912d35f8SJens Axboe 
164825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
165825cdcb1SNamhyung Kim {
166825cdcb1SNamhyung Kim 	smp_mb();
1670ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->rd_wait))
1680ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->rd_wait);
169825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
170825cdcb1SNamhyung Kim }
171825cdcb1SNamhyung Kim 
172932cc6d4SJens Axboe /**
173932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
174932cc6d4SJens Axboe  * @pipe:	pipe to fill
175932cc6d4SJens Axboe  * @spd:	data to fill
176932cc6d4SJens Axboe  *
177932cc6d4SJens Axboe  * Description:
17879685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
179932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
180932cc6d4SJens Axboe  *    function will link that data to the pipe.
181932cc6d4SJens Axboe  *
18283f9135bSJens Axboe  */
183d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
184912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1855274f052SJens Axboe {
18600de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
1878cefc107SDavid Howells 	unsigned int tail = pipe->tail;
1888cefc107SDavid Howells 	unsigned int head = pipe->head;
1898cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
1908924feffSAl Viro 	int ret = 0, page_nr = 0;
1915274f052SJens Axboe 
192d6785d91SRabin Vincent 	if (!spd_pages)
193d6785d91SRabin Vincent 		return 0;
194d6785d91SRabin Vincent 
1958924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1965274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1975274f052SJens Axboe 		ret = -EPIPE;
1988924feffSAl Viro 		goto out;
1995274f052SJens Axboe 	}
2005274f052SJens Axboe 
2016718b6f8SDavid Howells 	while (!pipe_full(head, tail, pipe->max_usage)) {
2028cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[head & mask];
2035274f052SJens Axboe 
204912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
205912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
206912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
207497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
208912d35f8SJens Axboe 		buf->ops = spd->ops;
2095a81e6a1SMiklos Szeredi 		buf->flags = 0;
2107afa6fd0SJens Axboe 
2118cefc107SDavid Howells 		head++;
2128cefc107SDavid Howells 		pipe->head = head;
213912d35f8SJens Axboe 		page_nr++;
214912d35f8SJens Axboe 		ret += buf->len;
215912d35f8SJens Axboe 
216912d35f8SJens Axboe 		if (!--spd->nr_pages)
2175274f052SJens Axboe 			break;
2185274f052SJens Axboe 	}
2195274f052SJens Axboe 
22029e35094SLinus Torvalds 	if (!ret)
22129e35094SLinus Torvalds 		ret = -EAGAIN;
22229e35094SLinus Torvalds 
2238924feffSAl Viro out:
22400de00bdSJens Axboe 	while (page_nr < spd_pages)
225bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2265274f052SJens Axboe 
2275274f052SJens Axboe 	return ret;
2285274f052SJens Axboe }
2292b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2305274f052SJens Axboe 
23179fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
23279fddc4eSAl Viro {
2338cefc107SDavid Howells 	unsigned int head = pipe->head;
2348cefc107SDavid Howells 	unsigned int tail = pipe->tail;
2358cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
23679fddc4eSAl Viro 	int ret;
23779fddc4eSAl Viro 
23879fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23979fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
24079fddc4eSAl Viro 		ret = -EPIPE;
2416718b6f8SDavid Howells 	} else if (pipe_full(head, tail, pipe->max_usage)) {
24279fddc4eSAl Viro 		ret = -EAGAIN;
24379fddc4eSAl Viro 	} else {
2448cefc107SDavid Howells 		pipe->bufs[head & mask] = *buf;
2458cefc107SDavid Howells 		pipe->head = head + 1;
24679fddc4eSAl Viro 		return buf->len;
24779fddc4eSAl Viro 	}
248a779638cSMiklos Szeredi 	pipe_buf_release(pipe, buf);
24979fddc4eSAl Viro 	return ret;
25079fddc4eSAl Viro }
25179fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
25279fddc4eSAl Viro 
25335f3d14dSJens Axboe /*
25435f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25535f3d14dSJens Axboe  * descriptions.
25635f3d14dSJens Axboe  */
257047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25835f3d14dSJens Axboe {
2596718b6f8SDavid Howells 	unsigned int max_usage = READ_ONCE(pipe->max_usage);
260047fe360SEric Dumazet 
2618cefc107SDavid Howells 	spd->nr_pages_max = max_usage;
2628cefc107SDavid Howells 	if (max_usage <= PIPE_DEF_BUFFERS)
26335f3d14dSJens Axboe 		return 0;
26435f3d14dSJens Axboe 
2658cefc107SDavid Howells 	spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
2668cefc107SDavid Howells 	spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
2676da2ec56SKees Cook 				     GFP_KERNEL);
26835f3d14dSJens Axboe 
26935f3d14dSJens Axboe 	if (spd->pages && spd->partial)
27035f3d14dSJens Axboe 		return 0;
27135f3d14dSJens Axboe 
27235f3d14dSJens Axboe 	kfree(spd->pages);
27335f3d14dSJens Axboe 	kfree(spd->partial);
27435f3d14dSJens Axboe 	return -ENOMEM;
27535f3d14dSJens Axboe }
27635f3d14dSJens Axboe 
277047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27835f3d14dSJens Axboe {
279047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
28035f3d14dSJens Axboe 		return;
28135f3d14dSJens Axboe 
28235f3d14dSJens Axboe 	kfree(spd->pages);
28335f3d14dSJens Axboe 	kfree(spd->partial);
28435f3d14dSJens Axboe }
28535f3d14dSJens Axboe 
28633b3b041SDavid Howells /*
28733b3b041SDavid Howells  * Splice data from an O_DIRECT file into pages and then add them to the output
28833b3b041SDavid Howells  * pipe.
28933b3b041SDavid Howells  */
29033b3b041SDavid Howells ssize_t direct_splice_read(struct file *in, loff_t *ppos,
29133b3b041SDavid Howells 			   struct pipe_inode_info *pipe,
29233b3b041SDavid Howells 			   size_t len, unsigned int flags)
29333b3b041SDavid Howells {
29433b3b041SDavid Howells 	struct iov_iter to;
29533b3b041SDavid Howells 	struct bio_vec *bv;
29633b3b041SDavid Howells 	struct kiocb kiocb;
29733b3b041SDavid Howells 	struct page **pages;
29833b3b041SDavid Howells 	ssize_t ret;
29933b3b041SDavid Howells 	size_t used, npages, chunk, remain, reclaim;
30033b3b041SDavid Howells 	int i;
30133b3b041SDavid Howells 
30233b3b041SDavid Howells 	/* Work out how much data we can actually add into the pipe */
30333b3b041SDavid Howells 	used = pipe_occupancy(pipe->head, pipe->tail);
30433b3b041SDavid Howells 	npages = max_t(ssize_t, pipe->max_usage - used, 0);
30533b3b041SDavid Howells 	len = min_t(size_t, len, npages * PAGE_SIZE);
30633b3b041SDavid Howells 	npages = DIV_ROUND_UP(len, PAGE_SIZE);
30733b3b041SDavid Howells 
30833b3b041SDavid Howells 	bv = kzalloc(array_size(npages, sizeof(bv[0])) +
30933b3b041SDavid Howells 		     array_size(npages, sizeof(struct page *)), GFP_KERNEL);
31033b3b041SDavid Howells 	if (!bv)
31133b3b041SDavid Howells 		return -ENOMEM;
31233b3b041SDavid Howells 
31333b3b041SDavid Howells 	pages = (void *)(bv + npages);
31433b3b041SDavid Howells 	npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
31533b3b041SDavid Howells 	if (!npages) {
31633b3b041SDavid Howells 		kfree(bv);
31733b3b041SDavid Howells 		return -ENOMEM;
31833b3b041SDavid Howells 	}
31933b3b041SDavid Howells 
32033b3b041SDavid Howells 	remain = len = min_t(size_t, len, npages * PAGE_SIZE);
32133b3b041SDavid Howells 
32233b3b041SDavid Howells 	for (i = 0; i < npages; i++) {
32333b3b041SDavid Howells 		chunk = min_t(size_t, PAGE_SIZE, remain);
32433b3b041SDavid Howells 		bv[i].bv_page = pages[i];
32533b3b041SDavid Howells 		bv[i].bv_offset = 0;
32633b3b041SDavid Howells 		bv[i].bv_len = chunk;
32733b3b041SDavid Howells 		remain -= chunk;
32833b3b041SDavid Howells 	}
32933b3b041SDavid Howells 
33033b3b041SDavid Howells 	/* Do the I/O */
33133b3b041SDavid Howells 	iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
33233b3b041SDavid Howells 	init_sync_kiocb(&kiocb, in);
33333b3b041SDavid Howells 	kiocb.ki_pos = *ppos;
33433b3b041SDavid Howells 	ret = call_read_iter(in, &kiocb, &to);
33533b3b041SDavid Howells 
33633b3b041SDavid Howells 	reclaim = npages * PAGE_SIZE;
33733b3b041SDavid Howells 	remain = 0;
33833b3b041SDavid Howells 	if (ret > 0) {
33933b3b041SDavid Howells 		reclaim -= ret;
34033b3b041SDavid Howells 		remain = ret;
34133b3b041SDavid Howells 		*ppos = kiocb.ki_pos;
34233b3b041SDavid Howells 		file_accessed(in);
34333b3b041SDavid Howells 	} else if (ret < 0) {
34433b3b041SDavid Howells 		/*
34533b3b041SDavid Howells 		 * callers of ->splice_read() expect -EAGAIN on
34633b3b041SDavid Howells 		 * "can't put anything in there", rather than -EFAULT.
34733b3b041SDavid Howells 		 */
34833b3b041SDavid Howells 		if (ret == -EFAULT)
34933b3b041SDavid Howells 			ret = -EAGAIN;
35033b3b041SDavid Howells 	}
35133b3b041SDavid Howells 
35233b3b041SDavid Howells 	/* Free any pages that didn't get touched at all. */
35333b3b041SDavid Howells 	reclaim /= PAGE_SIZE;
35433b3b041SDavid Howells 	if (reclaim) {
35533b3b041SDavid Howells 		npages -= reclaim;
35633b3b041SDavid Howells 		release_pages(pages + npages, reclaim);
35733b3b041SDavid Howells 	}
35833b3b041SDavid Howells 
35933b3b041SDavid Howells 	/* Push the remaining pages into the pipe. */
36033b3b041SDavid Howells 	for (i = 0; i < npages; i++) {
36133b3b041SDavid Howells 		struct pipe_buffer *buf = pipe_head_buf(pipe);
36233b3b041SDavid Howells 
36333b3b041SDavid Howells 		chunk = min_t(size_t, remain, PAGE_SIZE);
36433b3b041SDavid Howells 		*buf = (struct pipe_buffer) {
36533b3b041SDavid Howells 			.ops	= &default_pipe_buf_ops,
36633b3b041SDavid Howells 			.page	= bv[i].bv_page,
36733b3b041SDavid Howells 			.offset	= 0,
36833b3b041SDavid Howells 			.len	= chunk,
36933b3b041SDavid Howells 		};
37033b3b041SDavid Howells 		pipe->head++;
37133b3b041SDavid Howells 		remain -= chunk;
37233b3b041SDavid Howells 	}
37333b3b041SDavid Howells 
37433b3b041SDavid Howells 	kfree(bv);
37533b3b041SDavid Howells 	return ret;
37633b3b041SDavid Howells }
3777c8e01ebSDavid Howells EXPORT_SYMBOL(direct_splice_read);
37833b3b041SDavid Howells 
37983f9135bSJens Axboe /**
38083f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
38183f9135bSJens Axboe  * @in:		file to splice from
382932cc6d4SJens Axboe  * @ppos:	position in @in
38383f9135bSJens Axboe  * @pipe:	pipe to splice to
38483f9135bSJens Axboe  * @len:	number of bytes to splice
38583f9135bSJens Axboe  * @flags:	splice modifier flags
38683f9135bSJens Axboe  *
387932cc6d4SJens Axboe  * Description:
388932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
38982c156f8SAl Viro  *    used as long as it has more or less sane ->read_iter().
390932cc6d4SJens Axboe  *
39183f9135bSJens Axboe  */
392cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
393cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
394cbb7e577SJens Axboe 				 unsigned int flags)
3955274f052SJens Axboe {
39682c156f8SAl Viro 	struct iov_iter to;
39782c156f8SAl Viro 	struct kiocb kiocb;
3988cefc107SDavid Howells 	int ret;
399be64f884SBoaz Harrosh 
400de4eda9dSAl Viro 	iov_iter_pipe(&to, ITER_DEST, pipe, len);
40182c156f8SAl Viro 	init_sync_kiocb(&kiocb, in);
40282c156f8SAl Viro 	kiocb.ki_pos = *ppos;
403bb7462b6SMiklos Szeredi 	ret = call_read_iter(in, &kiocb, &to);
404723590edSMiklos Szeredi 	if (ret > 0) {
40582c156f8SAl Viro 		*ppos = kiocb.ki_pos;
406723590edSMiklos Szeredi 		file_accessed(in);
40782c156f8SAl Viro 	} else if (ret < 0) {
4080d964934SAl Viro 		/* free what was emitted */
4090d964934SAl Viro 		pipe_discard_from(pipe, to.start_head);
41082c156f8SAl Viro 		/*
41182c156f8SAl Viro 		 * callers of ->splice_read() expect -EAGAIN on
41282c156f8SAl Viro 		 * "can't put anything in there", rather than -EFAULT.
41382c156f8SAl Viro 		 */
41482c156f8SAl Viro 		if (ret == -EFAULT)
41582c156f8SAl Viro 			ret = -EAGAIN;
416723590edSMiklos Szeredi 	}
4175274f052SJens Axboe 
4185274f052SJens Axboe 	return ret;
4195274f052SJens Axboe }
420059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
421059a8f37SJens Axboe 
422241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = {
4236818173bSMiklos Szeredi 	.release	= generic_pipe_buf_release,
424c928f642SChristoph Hellwig 	.try_steal	= generic_pipe_buf_try_steal,
4256818173bSMiklos Szeredi 	.get		= generic_pipe_buf_get,
4266818173bSMiklos Szeredi };
4276818173bSMiklos Szeredi 
42828a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
42928a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
43028a625cbSMiklos Szeredi 	.release	= generic_pipe_buf_release,
43128a625cbSMiklos Szeredi 	.get		= generic_pipe_buf_get,
43228a625cbSMiklos Szeredi };
43328a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
43428a625cbSMiklos Szeredi 
4355274f052SJens Axboe /*
4364f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
437016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
4385274f052SJens Axboe  */
43976ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
4405274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
4415274f052SJens Axboe {
4426a14b90bSJens Axboe 	struct file *file = sd->u.file;
4435274f052SJens Axboe 	loff_t pos = sd->pos;
444a8adbe37SMichał Mirosław 	int more;
4455274f052SJens Axboe 
44672c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
447a8adbe37SMichał Mirosław 		return -EINVAL;
448a8adbe37SMichał Mirosław 
44935f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
450ae62ca7bSEric Dumazet 
4518cefc107SDavid Howells 	if (sd->len < sd->total_len &&
4528cefc107SDavid Howells 	    pipe_occupancy(pipe->head, pipe->tail) > 1)
45335f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
454ae62ca7bSEric Dumazet 
455a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
456f84d7519SJens Axboe 				    sd->len, &pos, more);
4575274f052SJens Axboe }
4585274f052SJens Axboe 
459b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
460b3c2d2ddSMiklos Szeredi {
461b3c2d2ddSMiklos Szeredi 	smp_mb();
4620ddad21dSLinus Torvalds 	if (waitqueue_active(&pipe->wr_wait))
4630ddad21dSLinus Torvalds 		wake_up_interruptible(&pipe->wr_wait);
464b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
465b3c2d2ddSMiklos Szeredi }
466b3c2d2ddSMiklos Szeredi 
467b3c2d2ddSMiklos Szeredi /**
468b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
469b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
470b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
471b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
472b3c2d2ddSMiklos Szeredi  *
473b3c2d2ddSMiklos Szeredi  * Description:
474b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
475b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
476b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
477b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
478b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
479b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
480b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
481b3c2d2ddSMiklos Szeredi  *
482b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
483b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
484b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
485b3c2d2ddSMiklos Szeredi  *    destination.
486b3c2d2ddSMiklos Szeredi  */
48796f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
488b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
489b3c2d2ddSMiklos Szeredi {
4908cefc107SDavid Howells 	unsigned int head = pipe->head;
4918cefc107SDavid Howells 	unsigned int tail = pipe->tail;
4928cefc107SDavid Howells 	unsigned int mask = pipe->ring_size - 1;
493b3c2d2ddSMiklos Szeredi 	int ret;
494b3c2d2ddSMiklos Szeredi 
495ec057595SLinus Torvalds 	while (!pipe_empty(head, tail)) {
4968cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[tail & mask];
497b3c2d2ddSMiklos Szeredi 
498b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
499b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
500b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
501b3c2d2ddSMiklos Szeredi 
502fba597dbSMiklos Szeredi 		ret = pipe_buf_confirm(pipe, buf);
503a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
504b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
505b3c2d2ddSMiklos Szeredi 				ret = 0;
506b3c2d2ddSMiklos Szeredi 			return ret;
507b3c2d2ddSMiklos Szeredi 		}
508a8adbe37SMichał Mirosław 
509a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
510a8adbe37SMichał Mirosław 		if (ret <= 0)
511a8adbe37SMichał Mirosław 			return ret;
512a8adbe37SMichał Mirosław 
513b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
514b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
515b3c2d2ddSMiklos Szeredi 
516b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
517b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
518b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
519b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
520b3c2d2ddSMiklos Szeredi 
521b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
522a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
5238cefc107SDavid Howells 			tail++;
5248cefc107SDavid Howells 			pipe->tail = tail;
5256447a3cfSAl Viro 			if (pipe->files)
526b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
527b3c2d2ddSMiklos Szeredi 		}
528b3c2d2ddSMiklos Szeredi 
529b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
530b3c2d2ddSMiklos Szeredi 			return 0;
531b3c2d2ddSMiklos Szeredi 	}
532b3c2d2ddSMiklos Szeredi 
533b3c2d2ddSMiklos Szeredi 	return 1;
534b3c2d2ddSMiklos Szeredi }
535b3c2d2ddSMiklos Szeredi 
536d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */
537d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
538d1a819a2SLinus Torvalds {
539d1a819a2SLinus Torvalds 	unsigned int tail = pipe->tail;
540d1a819a2SLinus Torvalds 	unsigned int mask = pipe->ring_size - 1;
541d1a819a2SLinus Torvalds 	struct pipe_buffer *buf = &pipe->bufs[tail & mask];
542d1a819a2SLinus Torvalds 
543d1a819a2SLinus Torvalds 	if (unlikely(!buf->len)) {
544d1a819a2SLinus Torvalds 		pipe_buf_release(pipe, buf);
545d1a819a2SLinus Torvalds 		pipe->tail = tail+1;
546d1a819a2SLinus Torvalds 		return true;
547d1a819a2SLinus Torvalds 	}
548d1a819a2SLinus Torvalds 
549d1a819a2SLinus Torvalds 	return false;
550d1a819a2SLinus Torvalds }
551d1a819a2SLinus Torvalds 
552b3c2d2ddSMiklos Szeredi /**
553b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
554b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
555b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
556b3c2d2ddSMiklos Szeredi  *
557b3c2d2ddSMiklos Szeredi  * Description:
558b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
559b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
560b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
561b3c2d2ddSMiklos Szeredi  */
56296f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
563b3c2d2ddSMiklos Szeredi {
564c725bfceSJan Kara 	/*
565c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
566c725bfceSJan Kara 	 * always buffers available
567c725bfceSJan Kara 	 */
568c725bfceSJan Kara 	if (signal_pending(current))
569c725bfceSJan Kara 		return -ERESTARTSYS;
570c725bfceSJan Kara 
571d1a819a2SLinus Torvalds repeat:
5728cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
573b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
574b3c2d2ddSMiklos Szeredi 			return 0;
575b3c2d2ddSMiklos Szeredi 
576a28c8b9dSLinus Torvalds 		if (sd->num_spliced)
577b3c2d2ddSMiklos Szeredi 			return 0;
578b3c2d2ddSMiklos Szeredi 
579b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
580b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
581b3c2d2ddSMiklos Szeredi 
582b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
583b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
584b3c2d2ddSMiklos Szeredi 
585b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
586b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
587b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
588b3c2d2ddSMiklos Szeredi 		}
589b3c2d2ddSMiklos Szeredi 
590472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
591b3c2d2ddSMiklos Szeredi 	}
592b3c2d2ddSMiklos Szeredi 
593d1a819a2SLinus Torvalds 	if (eat_empty_buffer(pipe))
594d1a819a2SLinus Torvalds 		goto repeat;
595d1a819a2SLinus Torvalds 
596b3c2d2ddSMiklos Szeredi 	return 1;
597b3c2d2ddSMiklos Szeredi }
598b3c2d2ddSMiklos Szeredi 
599b3c2d2ddSMiklos Szeredi /**
600b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
601b80901bbSRandy Dunlap  * @sd:		information about the splice operation
602b3c2d2ddSMiklos Szeredi  *
603b3c2d2ddSMiklos Szeredi  * Description:
604b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
605b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
606b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
607b3c2d2ddSMiklos Szeredi  */
60896f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
609b3c2d2ddSMiklos Szeredi {
610b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
611b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
612b3c2d2ddSMiklos Szeredi }
613b3c2d2ddSMiklos Szeredi 
614b3c2d2ddSMiklos Szeredi /**
615b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
616b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
617b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
618b3c2d2ddSMiklos Szeredi  *
619b3c2d2ddSMiklos Szeredi  * Description:
620b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
621b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
622b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
623b3c2d2ddSMiklos Szeredi  */
62496f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
625b3c2d2ddSMiklos Szeredi {
626b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
627b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
628b3c2d2ddSMiklos Szeredi }
629b3c2d2ddSMiklos Szeredi 
630932cc6d4SJens Axboe /**
631932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
632932cc6d4SJens Axboe  * @pipe:	pipe to splice from
633932cc6d4SJens Axboe  * @sd:		information to @actor
634932cc6d4SJens Axboe  * @actor:	handler that splices the data
635932cc6d4SJens Axboe  *
636932cc6d4SJens Axboe  * Description:
637932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
638932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
639932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
640932cc6d4SJens Axboe  *    pipe_to_user.
641932cc6d4SJens Axboe  *
64283f9135bSJens Axboe  */
643c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
644c66ab6faSJens Axboe 			   splice_actor *actor)
6455274f052SJens Axboe {
646b3c2d2ddSMiklos Szeredi 	int ret;
6475274f052SJens Axboe 
648b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
649b3c2d2ddSMiklos Szeredi 	do {
650c2489e07SJan Kara 		cond_resched();
651b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
652b3c2d2ddSMiklos Szeredi 		if (ret > 0)
653b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
654b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
655b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
6565274f052SJens Axboe 
657b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
6585274f052SJens Axboe }
65940bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
6605274f052SJens Axboe 
661932cc6d4SJens Axboe /**
662932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
663932cc6d4SJens Axboe  * @pipe:	pipe to splice from
664932cc6d4SJens Axboe  * @out:	file to splice to
665932cc6d4SJens Axboe  * @ppos:	position in @out
666932cc6d4SJens Axboe  * @len:	how many bytes to splice
667932cc6d4SJens Axboe  * @flags:	splice modifier flags
668932cc6d4SJens Axboe  * @actor:	handler that splices the data
669932cc6d4SJens Axboe  *
670932cc6d4SJens Axboe  * Description:
6712933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
672932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
673932cc6d4SJens Axboe  *
674932cc6d4SJens Axboe  */
6756da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
6766da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
6776da61809SMark Fasheh 			 splice_actor *actor)
6786da61809SMark Fasheh {
6796da61809SMark Fasheh 	ssize_t ret;
680c66ab6faSJens Axboe 	struct splice_desc sd = {
681c66ab6faSJens Axboe 		.total_len = len,
682c66ab6faSJens Axboe 		.flags = flags,
683c66ab6faSJens Axboe 		.pos = *ppos,
6846a14b90bSJens Axboe 		.u.file = out,
685c66ab6faSJens Axboe 	};
6866da61809SMark Fasheh 
68761e0d47cSMiklos Szeredi 	pipe_lock(pipe);
688c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
68961e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
6906da61809SMark Fasheh 
6916da61809SMark Fasheh 	return ret;
6926da61809SMark Fasheh }
6936da61809SMark Fasheh 
6946da61809SMark Fasheh /**
6958d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
6968d020765SAl Viro  * @pipe:	pipe info
6978d020765SAl Viro  * @out:	file to write to
6988d020765SAl Viro  * @ppos:	position in @out
6998d020765SAl Viro  * @len:	number of bytes to splice
7008d020765SAl Viro  * @flags:	splice modifier flags
7018d020765SAl Viro  *
7028d020765SAl Viro  * Description:
7038d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
7048d020765SAl Viro  *    the given pipe inode to the given file.
7058d020765SAl Viro  *    This one is ->write_iter-based.
7068d020765SAl Viro  *
7078d020765SAl Viro  */
7088d020765SAl Viro ssize_t
7098d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
7108d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
7118d020765SAl Viro {
7128d020765SAl Viro 	struct splice_desc sd = {
7138d020765SAl Viro 		.total_len = len,
7148d020765SAl Viro 		.flags = flags,
7158d020765SAl Viro 		.pos = *ppos,
7168d020765SAl Viro 		.u.file = out,
7178d020765SAl Viro 	};
7186718b6f8SDavid Howells 	int nbufs = pipe->max_usage;
7198d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
7208d020765SAl Viro 					GFP_KERNEL);
7218d020765SAl Viro 	ssize_t ret;
7228d020765SAl Viro 
7238d020765SAl Viro 	if (unlikely(!array))
7248d020765SAl Viro 		return -ENOMEM;
7258d020765SAl Viro 
7268d020765SAl Viro 	pipe_lock(pipe);
7278d020765SAl Viro 
7288d020765SAl Viro 	splice_from_pipe_begin(&sd);
7298d020765SAl Viro 	while (sd.total_len) {
7308d020765SAl Viro 		struct iov_iter from;
731ec057595SLinus Torvalds 		unsigned int head, tail, mask;
7328d020765SAl Viro 		size_t left;
7338cefc107SDavid Howells 		int n;
7348d020765SAl Viro 
7358d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
7368d020765SAl Viro 		if (ret <= 0)
7378d020765SAl Viro 			break;
7388d020765SAl Viro 
7396718b6f8SDavid Howells 		if (unlikely(nbufs < pipe->max_usage)) {
7408d020765SAl Viro 			kfree(array);
7416718b6f8SDavid Howells 			nbufs = pipe->max_usage;
7428d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
7438d020765SAl Viro 					GFP_KERNEL);
7448d020765SAl Viro 			if (!array) {
7458d020765SAl Viro 				ret = -ENOMEM;
7468d020765SAl Viro 				break;
7478d020765SAl Viro 			}
7488d020765SAl Viro 		}
7498d020765SAl Viro 
750ec057595SLinus Torvalds 		head = pipe->head;
751ec057595SLinus Torvalds 		tail = pipe->tail;
752ec057595SLinus Torvalds 		mask = pipe->ring_size - 1;
753ec057595SLinus Torvalds 
7548d020765SAl Viro 		/* build the vector */
7558d020765SAl Viro 		left = sd.total_len;
7560f1d344fSPavel Begunkov 		for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
7578cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7588d020765SAl Viro 			size_t this_len = buf->len;
7598d020765SAl Viro 
7600f1d344fSPavel Begunkov 			/* zero-length bvecs are not supported, skip them */
7610f1d344fSPavel Begunkov 			if (!this_len)
7620f1d344fSPavel Begunkov 				continue;
7630f1d344fSPavel Begunkov 			this_len = min(this_len, left);
7648d020765SAl Viro 
765fba597dbSMiklos Szeredi 			ret = pipe_buf_confirm(pipe, buf);
7668d020765SAl Viro 			if (unlikely(ret)) {
7678d020765SAl Viro 				if (ret == -ENODATA)
7688d020765SAl Viro 					ret = 0;
7698d020765SAl Viro 				goto done;
7708d020765SAl Viro 			}
7718d020765SAl Viro 
772664e4078SChristoph Hellwig 			bvec_set_page(&array[n], buf->page, this_len,
773664e4078SChristoph Hellwig 				      buf->offset);
7748d020765SAl Viro 			left -= this_len;
7750f1d344fSPavel Begunkov 			n++;
7768d020765SAl Viro 		}
7778d020765SAl Viro 
778de4eda9dSAl Viro 		iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
779abbb6589SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos, 0);
7808d020765SAl Viro 		if (ret <= 0)
7818d020765SAl Viro 			break;
7828d020765SAl Viro 
7838d020765SAl Viro 		sd.num_spliced += ret;
7848d020765SAl Viro 		sd.total_len -= ret;
785dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
7868d020765SAl Viro 
7878d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
7888cefc107SDavid Howells 		tail = pipe->tail;
7898d020765SAl Viro 		while (ret) {
7908cefc107SDavid Howells 			struct pipe_buffer *buf = &pipe->bufs[tail & mask];
7918d020765SAl Viro 			if (ret >= buf->len) {
7928d020765SAl Viro 				ret -= buf->len;
7938d020765SAl Viro 				buf->len = 0;
794a779638cSMiklos Szeredi 				pipe_buf_release(pipe, buf);
7958cefc107SDavid Howells 				tail++;
7968cefc107SDavid Howells 				pipe->tail = tail;
7978d020765SAl Viro 				if (pipe->files)
7988d020765SAl Viro 					sd.need_wakeup = true;
7998d020765SAl Viro 			} else {
8008d020765SAl Viro 				buf->offset += ret;
8018d020765SAl Viro 				buf->len -= ret;
8028d020765SAl Viro 				ret = 0;
8038d020765SAl Viro 			}
8048d020765SAl Viro 		}
8058d020765SAl Viro 	}
8068d020765SAl Viro done:
8078d020765SAl Viro 	kfree(array);
8088d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
8098d020765SAl Viro 
8108d020765SAl Viro 	pipe_unlock(pipe);
8118d020765SAl Viro 
8128d020765SAl Viro 	if (sd.num_spliced)
8138d020765SAl Viro 		ret = sd.num_spliced;
8148d020765SAl Viro 
8158d020765SAl Viro 	return ret;
8168d020765SAl Viro }
8178d020765SAl Viro 
8188d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
8198d020765SAl Viro 
82083f9135bSJens Axboe /**
82183f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
822932cc6d4SJens Axboe  * @pipe:	pipe to splice from
82383f9135bSJens Axboe  * @out:	socket to write to
824932cc6d4SJens Axboe  * @ppos:	position in @out
82583f9135bSJens Axboe  * @len:	number of bytes to splice
82683f9135bSJens Axboe  * @flags:	splice modifier flags
82783f9135bSJens Axboe  *
828932cc6d4SJens Axboe  * Description:
82983f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
83083f9135bSJens Axboe  *    is involved.
83183f9135bSJens Axboe  *
83283f9135bSJens Axboe  */
8333a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
834cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
8355274f052SJens Axboe {
83600522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
8375274f052SJens Axboe }
8385274f052SJens Axboe 
839059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
840a0f06780SJeff Garzik 
84136e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op)
84236e2c742SChristoph Hellwig {
84336e2c742SChristoph Hellwig 	pr_debug_ratelimited(
84436e2c742SChristoph Hellwig 		"splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
84536e2c742SChristoph Hellwig 		op, file, current->pid, current->comm);
84636e2c742SChristoph Hellwig 	return -EINVAL;
84736e2c742SChristoph Hellwig }
84836e2c742SChristoph Hellwig 
84983f9135bSJens Axboe /*
85083f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
85183f9135bSJens Axboe  */
8523a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
853cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
8545274f052SJens Axboe {
85536e2c742SChristoph Hellwig 	if (unlikely(!out->f_op->splice_write))
85636e2c742SChristoph Hellwig 		return warn_unsupported(out, "write");
85700c285d0SChristoph Hellwig 	return out->f_op->splice_write(pipe, out, ppos, len, flags);
8585274f052SJens Axboe }
8595274f052SJens Axboe 
86083f9135bSJens Axboe /*
86183f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
86283f9135bSJens Axboe  */
863cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
864cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
865cbb7e577SJens Axboe 			 unsigned int flags)
8665274f052SJens Axboe {
867313d64a3SAl Viro 	unsigned int p_space;
8685274f052SJens Axboe 	int ret;
8695274f052SJens Axboe 
87049570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
8715274f052SJens Axboe 		return -EBADF;
8725274f052SJens Axboe 
873313d64a3SAl Viro 	/* Don't try to read more the pipe has space for. */
874313d64a3SAl Viro 	p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
875313d64a3SAl Viro 	len = min_t(size_t, len, p_space << PAGE_SHIFT);
876313d64a3SAl Viro 
877cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
8785274f052SJens Axboe 	if (unlikely(ret < 0))
8795274f052SJens Axboe 		return ret;
8805274f052SJens Axboe 
88103cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
88203cc0789SAl Viro 		len = MAX_RW_COUNT;
88303cc0789SAl Viro 
88436e2c742SChristoph Hellwig 	if (unlikely(!in->f_op->splice_read))
88536e2c742SChristoph Hellwig 		return warn_unsupported(in, "read");
8862bc01060SChristoph Hellwig 	return in->f_op->splice_read(in, ppos, pipe, len, flags);
8875274f052SJens Axboe }
8885274f052SJens Axboe 
889932cc6d4SJens Axboe /**
890932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
891932cc6d4SJens Axboe  * @in:		file to splice from
892932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
893932cc6d4SJens Axboe  * @actor:	handles the data splicing
894932cc6d4SJens Axboe  *
895932cc6d4SJens Axboe  * Description:
896932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
897932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
898932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
899932cc6d4SJens Axboe  *    that process.
900932cc6d4SJens Axboe  *
901c66ab6faSJens Axboe  */
902c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
903c66ab6faSJens Axboe 			       splice_direct_actor *actor)
904b92ce558SJens Axboe {
905b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
906b92ce558SJens Axboe 	long ret, bytes;
907c66ab6faSJens Axboe 	size_t len;
9080ff28d9fSChristophe Leroy 	int i, flags, more;
909b92ce558SJens Axboe 
910b92ce558SJens Axboe 	/*
91197ef77c5SJason A. Donenfeld 	 * We require the input to be seekable, as we don't want to randomly
91297ef77c5SJason A. Donenfeld 	 * drop data for eg socket -> socket splicing. Use the piped splicing
91397ef77c5SJason A. Donenfeld 	 * for that!
914b92ce558SJens Axboe 	 */
91597ef77c5SJason A. Donenfeld 	if (unlikely(!(in->f_mode & FMODE_LSEEK)))
916b92ce558SJens Axboe 		return -EINVAL;
917b92ce558SJens Axboe 
918b92ce558SJens Axboe 	/*
919b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
920b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
921b92ce558SJens Axboe 	 */
922b92ce558SJens Axboe 	pipe = current->splice_pipe;
92349570e9bSJens Axboe 	if (unlikely(!pipe)) {
9247bee130eSAl Viro 		pipe = alloc_pipe_info();
925b92ce558SJens Axboe 		if (!pipe)
926b92ce558SJens Axboe 			return -ENOMEM;
927b92ce558SJens Axboe 
928b92ce558SJens Axboe 		/*
929b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
93000522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
931b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
932b92ce558SJens Axboe 		 */
933b92ce558SJens Axboe 		pipe->readers = 1;
934b92ce558SJens Axboe 
935b92ce558SJens Axboe 		current->splice_pipe = pipe;
936b92ce558SJens Axboe 	}
937b92ce558SJens Axboe 
938b92ce558SJens Axboe 	/*
93973d62d83SIngo Molnar 	 * Do the splice.
940b92ce558SJens Axboe 	 */
941b92ce558SJens Axboe 	bytes = 0;
942c66ab6faSJens Axboe 	len = sd->total_len;
943c66ab6faSJens Axboe 	flags = sd->flags;
944c66ab6faSJens Axboe 
945c66ab6faSJens Axboe 	/*
946c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
947c66ab6faSJens Axboe 	 */
948c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
9490ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
950b92ce558SJens Axboe 
9518cefc107SDavid Howells 	WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
95217614445SDarrick J. Wong 
953b92ce558SJens Axboe 	while (len) {
95451a92c0fSJens Axboe 		size_t read_len;
955a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
956b92ce558SJens Axboe 
957313d64a3SAl Viro 		ret = do_splice_to(in, &pos, pipe, len, flags);
95851a92c0fSJens Axboe 		if (unlikely(ret <= 0))
959b92ce558SJens Axboe 			goto out_release;
960b92ce558SJens Axboe 
961b92ce558SJens Axboe 		read_len = ret;
962c66ab6faSJens Axboe 		sd->total_len = read_len;
963b92ce558SJens Axboe 
964b92ce558SJens Axboe 		/*
9650ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
9660ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
9670ff28d9fSChristophe Leroy 		 * initially, clears it.
9680ff28d9fSChristophe Leroy 		 */
9690ff28d9fSChristophe Leroy 		if (read_len < len)
9700ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
9710ff28d9fSChristophe Leroy 		else if (!more)
9720ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
9730ff28d9fSChristophe Leroy 		/*
974b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
975b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
976b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
977b92ce558SJens Axboe 		 */
978c66ab6faSJens Axboe 		ret = actor(pipe, sd);
979a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
980a82c53a0STom Zanussi 			sd->pos = prev_pos;
981b92ce558SJens Axboe 			goto out_release;
982a82c53a0STom Zanussi 		}
983b92ce558SJens Axboe 
984b92ce558SJens Axboe 		bytes += ret;
985b92ce558SJens Axboe 		len -= ret;
986bcd4f3acSJens Axboe 		sd->pos = pos;
987b92ce558SJens Axboe 
988a82c53a0STom Zanussi 		if (ret < read_len) {
989a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
99051a92c0fSJens Axboe 			goto out_release;
991b92ce558SJens Axboe 		}
992a82c53a0STom Zanussi 	}
993b92ce558SJens Axboe 
9949e97198dSJens Axboe done:
9958cefc107SDavid Howells 	pipe->tail = pipe->head = 0;
9969e97198dSJens Axboe 	file_accessed(in);
997b92ce558SJens Axboe 	return bytes;
998b92ce558SJens Axboe 
999b92ce558SJens Axboe out_release:
1000b92ce558SJens Axboe 	/*
1001b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1002b92ce558SJens Axboe 	 * the pipe buffers in question:
1003b92ce558SJens Axboe 	 */
10048cefc107SDavid Howells 	for (i = 0; i < pipe->ring_size; i++) {
10058cefc107SDavid Howells 		struct pipe_buffer *buf = &pipe->bufs[i];
1006b92ce558SJens Axboe 
1007a779638cSMiklos Szeredi 		if (buf->ops)
1008a779638cSMiklos Szeredi 			pipe_buf_release(pipe, buf);
1009b92ce558SJens Axboe 	}
1010b92ce558SJens Axboe 
10119e97198dSJens Axboe 	if (!bytes)
10129e97198dSJens Axboe 		bytes = ret;
1013b92ce558SJens Axboe 
10149e97198dSJens Axboe 	goto done;
1015c66ab6faSJens Axboe }
1016c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1017c66ab6faSJens Axboe 
1018c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1019c66ab6faSJens Axboe 			       struct splice_desc *sd)
1020c66ab6faSJens Axboe {
10216a14b90bSJens Axboe 	struct file *file = sd->u.file;
1022c66ab6faSJens Axboe 
10237995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
10242cb4b05eSChangli Gao 			      sd->flags);
1025c66ab6faSJens Axboe }
1026c66ab6faSJens Axboe 
1027932cc6d4SJens Axboe /**
1028932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1029932cc6d4SJens Axboe  * @in:		file to splice from
1030932cc6d4SJens Axboe  * @ppos:	input file offset
1031932cc6d4SJens Axboe  * @out:	file to splice to
1032acdb37c3SRandy Dunlap  * @opos:	output file offset
1033932cc6d4SJens Axboe  * @len:	number of bytes to splice
1034932cc6d4SJens Axboe  * @flags:	splice modifier flags
1035932cc6d4SJens Axboe  *
1036932cc6d4SJens Axboe  * Description:
1037932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1038932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1039932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1040932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1041932cc6d4SJens Axboe  *
1042932cc6d4SJens Axboe  */
1043c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
10447995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1045c66ab6faSJens Axboe {
1046c66ab6faSJens Axboe 	struct splice_desc sd = {
1047c66ab6faSJens Axboe 		.len		= len,
1048c66ab6faSJens Axboe 		.total_len	= len,
1049c66ab6faSJens Axboe 		.flags		= flags,
1050c66ab6faSJens Axboe 		.pos		= *ppos,
10516a14b90bSJens Axboe 		.u.file		= out,
10527995bd28SAl Viro 		.opos		= opos,
1053c66ab6faSJens Axboe 	};
105451a92c0fSJens Axboe 	long ret;
1055c66ab6faSJens Axboe 
105618c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
105718c67cb9SAl Viro 		return -EBADF;
105818c67cb9SAl Viro 
105918c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
106018c67cb9SAl Viro 		return -EINVAL;
106118c67cb9SAl Viro 
106218c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
106318c67cb9SAl Viro 	if (unlikely(ret < 0))
106418c67cb9SAl Viro 		return ret;
106518c67cb9SAl Viro 
1066c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
106751a92c0fSJens Axboe 	if (ret > 0)
1068a82c53a0STom Zanussi 		*ppos = sd.pos;
106951a92c0fSJens Axboe 
1070c66ab6faSJens Axboe 	return ret;
1071b92ce558SJens Axboe }
10721c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1073b92ce558SJens Axboe 
10748924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
10758924feffSAl Viro {
107652bce911SLinus Torvalds 	for (;;) {
107752bce911SLinus Torvalds 		if (unlikely(!pipe->readers)) {
107852bce911SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
107952bce911SLinus Torvalds 			return -EPIPE;
108052bce911SLinus Torvalds 		}
10816718b6f8SDavid Howells 		if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
108252bce911SLinus Torvalds 			return 0;
10838924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
10848924feffSAl Viro 			return -EAGAIN;
10858924feffSAl Viro 		if (signal_pending(current))
10868924feffSAl Viro 			return -ERESTARTSYS;
1087472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
10888924feffSAl Viro 	}
10898924feffSAl Viro }
10908924feffSAl Viro 
10917c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
10927c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
10937c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1094ddac0d39SJens Axboe 
1095b964bf53SAl Viro long splice_file_to_pipe(struct file *in,
1096faa97c48SAl Viro 			 struct pipe_inode_info *opipe,
1097faa97c48SAl Viro 			 loff_t *offset,
1098faa97c48SAl Viro 			 size_t len, unsigned int flags)
1099faa97c48SAl Viro {
1100faa97c48SAl Viro 	long ret;
1101faa97c48SAl Viro 
1102faa97c48SAl Viro 	pipe_lock(opipe);
1103faa97c48SAl Viro 	ret = wait_for_space(opipe, flags);
1104faa97c48SAl Viro 	if (!ret)
1105faa97c48SAl Viro 		ret = do_splice_to(in, offset, opipe, len, flags);
1106faa97c48SAl Viro 	pipe_unlock(opipe);
1107faa97c48SAl Viro 	if (ret > 0)
1108faa97c48SAl Viro 		wakeup_pipe_readers(opipe);
1109faa97c48SAl Viro 	return ret;
1110faa97c48SAl Viro }
1111faa97c48SAl Viro 
1112ddac0d39SJens Axboe /*
111383f9135bSJens Axboe  * Determine where to splice to/from.
111483f9135bSJens Axboe  */
1115ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out,
1116ee6e00c8SJens Axboe 	       loff_t *off_out, size_t len, unsigned int flags)
11175274f052SJens Axboe {
11187c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
11197c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
11207995bd28SAl Viro 	loff_t offset;
1121a4514ebdSJens Axboe 	long ret;
11225274f052SJens Axboe 
112390da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
112490da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
112590da2e3fSPavel Begunkov 		return -EBADF;
112690da2e3fSPavel Begunkov 
1127c73be61cSDavid Howells 	ipipe = get_pipe_info(in, true);
1128c73be61cSDavid Howells 	opipe = get_pipe_info(out, true);
11297c77f0b3SMiklos Szeredi 
11307c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
11317c77f0b3SMiklos Szeredi 		if (off_in || off_out)
11327c77f0b3SMiklos Szeredi 			return -ESPIPE;
11337c77f0b3SMiklos Szeredi 
11347c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
11357c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
11367c77f0b3SMiklos Szeredi 			return -EINVAL;
11377c77f0b3SMiklos Szeredi 
1138ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1139ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1140ee5e0011SSlavomir Kaslev 
11417c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
11427c77f0b3SMiklos Szeredi 	}
11437c77f0b3SMiklos Szeredi 
11447c77f0b3SMiklos Szeredi 	if (ipipe) {
1145529565dcSIngo Molnar 		if (off_in)
1146529565dcSIngo Molnar 			return -ESPIPE;
1147b92ce558SJens Axboe 		if (off_out) {
114819c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1149b92ce558SJens Axboe 				return -EINVAL;
1150ee6e00c8SJens Axboe 			offset = *off_out;
11517995bd28SAl Viro 		} else {
11527995bd28SAl Viro 			offset = out->f_pos;
11537995bd28SAl Viro 		}
1154529565dcSIngo Molnar 
115518c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
115618c67cb9SAl Viro 			return -EINVAL;
115718c67cb9SAl Viro 
115818c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
115918c67cb9SAl Viro 		if (unlikely(ret < 0))
116018c67cb9SAl Viro 			return ret;
116118c67cb9SAl Viro 
1162ee5e0011SSlavomir Kaslev 		if (in->f_flags & O_NONBLOCK)
1163ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1164ee5e0011SSlavomir Kaslev 
1165500368f7SAl Viro 		file_start_write(out);
11667995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1167500368f7SAl Viro 		file_end_write(out);
1168a4514ebdSJens Axboe 
1169*983652c6SChung-Chiang Cheng 		if (ret > 0)
1170*983652c6SChung-Chiang Cheng 			fsnotify_modify(out);
1171*983652c6SChung-Chiang Cheng 
11727995bd28SAl Viro 		if (!off_out)
11737995bd28SAl Viro 			out->f_pos = offset;
1174ee6e00c8SJens Axboe 		else
1175ee6e00c8SJens Axboe 			*off_out = offset;
1176a4514ebdSJens Axboe 
1177a4514ebdSJens Axboe 		return ret;
1178529565dcSIngo Molnar 	}
11795274f052SJens Axboe 
11807c77f0b3SMiklos Szeredi 	if (opipe) {
1181529565dcSIngo Molnar 		if (off_out)
1182529565dcSIngo Molnar 			return -ESPIPE;
1183b92ce558SJens Axboe 		if (off_in) {
118419c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1185b92ce558SJens Axboe 				return -EINVAL;
1186ee6e00c8SJens Axboe 			offset = *off_in;
11877995bd28SAl Viro 		} else {
11887995bd28SAl Viro 			offset = in->f_pos;
11897995bd28SAl Viro 		}
1190529565dcSIngo Molnar 
1191ee5e0011SSlavomir Kaslev 		if (out->f_flags & O_NONBLOCK)
1192ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1193ee5e0011SSlavomir Kaslev 
1194faa97c48SAl Viro 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1195*983652c6SChung-Chiang Cheng 
1196*983652c6SChung-Chiang Cheng 		if (ret > 0)
1197*983652c6SChung-Chiang Cheng 			fsnotify_access(in);
1198*983652c6SChung-Chiang Cheng 
11997995bd28SAl Viro 		if (!off_in)
12007995bd28SAl Viro 			in->f_pos = offset;
1201ee6e00c8SJens Axboe 		else
1202ee6e00c8SJens Axboe 			*off_in = offset;
1203a4514ebdSJens Axboe 
1204a4514ebdSJens Axboe 		return ret;
1205529565dcSIngo Molnar 	}
12065274f052SJens Axboe 
12075274f052SJens Axboe 	return -EINVAL;
12085274f052SJens Axboe }
12095274f052SJens Axboe 
1210ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in,
1211ee6e00c8SJens Axboe 			struct file *out, loff_t __user *off_out,
1212ee6e00c8SJens Axboe 			size_t len, unsigned int flags)
1213ee6e00c8SJens Axboe {
1214ee6e00c8SJens Axboe 	struct pipe_inode_info *ipipe;
1215ee6e00c8SJens Axboe 	struct pipe_inode_info *opipe;
1216ee6e00c8SJens Axboe 	loff_t offset, *__off_in = NULL, *__off_out = NULL;
1217ee6e00c8SJens Axboe 	long ret;
1218ee6e00c8SJens Axboe 
1219ee6e00c8SJens Axboe 	ipipe = get_pipe_info(in, true);
1220ee6e00c8SJens Axboe 	opipe = get_pipe_info(out, true);
1221ee6e00c8SJens Axboe 
1222ee6e00c8SJens Axboe 	if (ipipe && off_in)
1223ee6e00c8SJens Axboe 		return -ESPIPE;
1224ee6e00c8SJens Axboe 	if (opipe && off_out)
1225ee6e00c8SJens Axboe 		return -ESPIPE;
1226ee6e00c8SJens Axboe 
1227ee6e00c8SJens Axboe 	if (off_out) {
1228ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1229ee6e00c8SJens Axboe 			return -EFAULT;
1230ee6e00c8SJens Axboe 		__off_out = &offset;
1231ee6e00c8SJens Axboe 	}
1232ee6e00c8SJens Axboe 	if (off_in) {
1233ee6e00c8SJens Axboe 		if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1234ee6e00c8SJens Axboe 			return -EFAULT;
1235ee6e00c8SJens Axboe 		__off_in = &offset;
1236ee6e00c8SJens Axboe 	}
1237ee6e00c8SJens Axboe 
1238ee6e00c8SJens Axboe 	ret = do_splice(in, __off_in, out, __off_out, len, flags);
1239ee6e00c8SJens Axboe 	if (ret < 0)
1240ee6e00c8SJens Axboe 		return ret;
1241ee6e00c8SJens Axboe 
1242ee6e00c8SJens Axboe 	if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1243ee6e00c8SJens Axboe 		return -EFAULT;
1244ee6e00c8SJens Axboe 	if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1245ee6e00c8SJens Axboe 		return -EFAULT;
1246ee6e00c8SJens Axboe 
1247ee6e00c8SJens Axboe 	return ret;
1248ee6e00c8SJens Axboe }
1249ee6e00c8SJens Axboe 
125079fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
125179fddc4eSAl Viro 			struct pipe_inode_info *pipe,
125279fddc4eSAl Viro 			unsigned flags)
1253912d35f8SJens Axboe {
125479fddc4eSAl Viro 	struct pipe_buffer buf = {
125579fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
125679fddc4eSAl Viro 		.flags = flags
125779fddc4eSAl Viro 	};
125879fddc4eSAl Viro 	size_t total = 0;
125979fddc4eSAl Viro 	int ret = 0;
126079fddc4eSAl Viro 
12617d690c15SAl Viro 	while (iov_iter_count(from)) {
126279fddc4eSAl Viro 		struct page *pages[16];
12637d690c15SAl Viro 		ssize_t left;
1264db85a9ebSAl Viro 		size_t start;
12657d690c15SAl Viro 		int i, n;
1266912d35f8SJens Axboe 
12677d690c15SAl Viro 		left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
12687d690c15SAl Viro 		if (left <= 0) {
12697d690c15SAl Viro 			ret = left;
127079fddc4eSAl Viro 			break;
127179fddc4eSAl Viro 		}
1272912d35f8SJens Axboe 
12737d690c15SAl Viro 		n = DIV_ROUND_UP(left + start, PAGE_SIZE);
12747d690c15SAl Viro 		for (i = 0; i < n; i++) {
12757d690c15SAl Viro 			int size = min_t(int, left, PAGE_SIZE - start);
12767d690c15SAl Viro 
12777d690c15SAl Viro 			buf.page = pages[i];
127879fddc4eSAl Viro 			buf.offset = start;
127979fddc4eSAl Viro 			buf.len = size;
128079fddc4eSAl Viro 			ret = add_to_pipe(pipe, &buf);
128179fddc4eSAl Viro 			if (unlikely(ret < 0)) {
12827d690c15SAl Viro 				iov_iter_revert(from, left);
12837d690c15SAl Viro 				// this one got dropped by add_to_pipe()
12847d690c15SAl Viro 				while (++i < n)
12857d690c15SAl Viro 					put_page(pages[i]);
12867d690c15SAl Viro 				goto out;
12877d690c15SAl Viro 			}
128879fddc4eSAl Viro 			total += ret;
12897d690c15SAl Viro 			left -= size;
12907d690c15SAl Viro 			start = 0;
1291912d35f8SJens Axboe 		}
1292912d35f8SJens Axboe 	}
12937d690c15SAl Viro out:
129479fddc4eSAl Viro 	return total ? total : ret;
1295912d35f8SJens Axboe }
1296912d35f8SJens Axboe 
12976a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
12986a14b90bSJens Axboe 			struct splice_desc *sd)
12996a14b90bSJens Axboe {
13006130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
13016130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
13026a14b90bSJens Axboe }
13036a14b90bSJens Axboe 
13046a14b90bSJens Axboe /*
13056a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
13066a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
13076a14b90bSJens Axboe  */
130887a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
130987a3002aSAl Viro 			     unsigned int flags)
13106a14b90bSJens Axboe {
1311c73be61cSDavid Howells 	struct pipe_inode_info *pipe = get_pipe_info(file, true);
131287a3002aSAl Viro 	struct splice_desc sd = {
131387a3002aSAl Viro 		.total_len = iov_iter_count(iter),
131487a3002aSAl Viro 		.flags = flags,
131587a3002aSAl Viro 		.u.data = iter
131687a3002aSAl Viro 	};
131787a3002aSAl Viro 	long ret = 0;
13186a14b90bSJens Axboe 
13196a14b90bSJens Axboe 	if (!pipe)
13206a14b90bSJens Axboe 		return -EBADF;
13216a14b90bSJens Axboe 
1322345995faSAl Viro 	if (sd.total_len) {
13236130f531SAl Viro 		pipe_lock(pipe);
13246130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
132561e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1326345995faSAl Viro 	}
13276a14b90bSJens Axboe 
13286a14b90bSJens Axboe 	return ret;
13296a14b90bSJens Axboe }
13306a14b90bSJens Axboe 
1331912d35f8SJens Axboe /*
1332912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1333912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1334912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1335912d35f8SJens Axboe  */
133687a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
133787a3002aSAl Viro 			     unsigned int flags)
1338912d35f8SJens Axboe {
1339ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
134087a3002aSAl Viro 	long ret = 0;
134179fddc4eSAl Viro 	unsigned buf_flag = 0;
134279fddc4eSAl Viro 
134379fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
134479fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1345912d35f8SJens Axboe 
1346c73be61cSDavid Howells 	pipe = get_pipe_info(file, true);
1347ddac0d39SJens Axboe 	if (!pipe)
1348912d35f8SJens Axboe 		return -EBADF;
1349912d35f8SJens Axboe 
13508924feffSAl Viro 	pipe_lock(pipe);
13518924feffSAl Viro 	ret = wait_for_space(pipe, flags);
135279fddc4eSAl Viro 	if (!ret)
135387a3002aSAl Viro 		ret = iter_to_pipe(iter, pipe, buf_flag);
13548924feffSAl Viro 	pipe_unlock(pipe);
13558924feffSAl Viro 	if (ret > 0)
13568924feffSAl Viro 		wakeup_pipe_readers(pipe);
135735f3d14dSJens Axboe 	return ret;
1358912d35f8SJens Axboe }
1359912d35f8SJens Axboe 
136087a3002aSAl Viro static int vmsplice_type(struct fd f, int *type)
136187a3002aSAl Viro {
136287a3002aSAl Viro 	if (!f.file)
136387a3002aSAl Viro 		return -EBADF;
136487a3002aSAl Viro 	if (f.file->f_mode & FMODE_WRITE) {
1365de4eda9dSAl Viro 		*type = ITER_SOURCE;
136687a3002aSAl Viro 	} else if (f.file->f_mode & FMODE_READ) {
1367de4eda9dSAl Viro 		*type = ITER_DEST;
136887a3002aSAl Viro 	} else {
136987a3002aSAl Viro 		fdput(f);
137087a3002aSAl Viro 		return -EBADF;
137187a3002aSAl Viro 	}
137287a3002aSAl Viro 	return 0;
137387a3002aSAl Viro }
137487a3002aSAl Viro 
13756a14b90bSJens Axboe /*
13766a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
13776a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
13786a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
13796a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
13806a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
13816a14b90bSJens Axboe  * solutions for that:
13826a14b90bSJens Axboe  *
13836a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
13846a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
13856a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
13866a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
13876a14b90bSJens Axboe  *
13886a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
13896a14b90bSJens Axboe  *
13906a14b90bSJens Axboe  */
139187a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
139230cfe4efSDominik Brodowski 		unsigned long, nr_segs, unsigned int, flags)
139330cfe4efSDominik Brodowski {
139487a3002aSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
139587a3002aSAl Viro 	struct iovec *iov = iovstack;
139687a3002aSAl Viro 	struct iov_iter iter;
139787e5e6daSJens Axboe 	ssize_t error;
139887a3002aSAl Viro 	struct fd f;
139987a3002aSAl Viro 	int type;
140087a3002aSAl Viro 
1401598b3cecSChristoph Hellwig 	if (unlikely(flags & ~SPLICE_F_ALL))
1402598b3cecSChristoph Hellwig 		return -EINVAL;
1403598b3cecSChristoph Hellwig 
140487a3002aSAl Viro 	f = fdget(fd);
140587a3002aSAl Viro 	error = vmsplice_type(f, &type);
140687a3002aSAl Viro 	if (error)
140787a3002aSAl Viro 		return error;
140887a3002aSAl Viro 
140987a3002aSAl Viro 	error = import_iovec(type, uiov, nr_segs,
141087a3002aSAl Viro 			     ARRAY_SIZE(iovstack), &iov, &iter);
1411598b3cecSChristoph Hellwig 	if (error < 0)
1412598b3cecSChristoph Hellwig 		goto out_fdput;
1413598b3cecSChristoph Hellwig 
1414598b3cecSChristoph Hellwig 	if (!iov_iter_count(&iter))
1415598b3cecSChristoph Hellwig 		error = 0;
1416de4eda9dSAl Viro 	else if (type == ITER_SOURCE)
1417598b3cecSChristoph Hellwig 		error = vmsplice_to_pipe(f.file, &iter, flags);
1418598b3cecSChristoph Hellwig 	else
1419598b3cecSChristoph Hellwig 		error = vmsplice_to_user(f.file, &iter, flags);
1420598b3cecSChristoph Hellwig 
142187a3002aSAl Viro 	kfree(iov);
1422598b3cecSChristoph Hellwig out_fdput:
142387a3002aSAl Viro 	fdput(f);
142487a3002aSAl Viro 	return error;
142530cfe4efSDominik Brodowski }
142630cfe4efSDominik Brodowski 
1427836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1428836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1429836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
14305274f052SJens Axboe {
14312903ff01SAl Viro 	struct fd in, out;
14325274f052SJens Axboe 	long error;
14335274f052SJens Axboe 
14345274f052SJens Axboe 	if (unlikely(!len))
14355274f052SJens Axboe 		return 0;
14365274f052SJens Axboe 
14373d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
14383d6ea290SAl Viro 		return -EINVAL;
14393d6ea290SAl Viro 
14405274f052SJens Axboe 	error = -EBADF;
14412903ff01SAl Viro 	in = fdget(fd_in);
14422903ff01SAl Viro 	if (in.file) {
14432903ff01SAl Viro 		out = fdget(fd_out);
14442903ff01SAl Viro 		if (out.file) {
1445ee6e00c8SJens Axboe 			error = __do_splice(in.file, off_in, out.file, off_out,
1446529565dcSIngo Molnar 						len, flags);
14472903ff01SAl Viro 			fdput(out);
14485274f052SJens Axboe 		}
14492903ff01SAl Viro 		fdput(in);
14505274f052SJens Axboe 	}
14515274f052SJens Axboe 	return error;
14525274f052SJens Axboe }
145370524490SJens Axboe 
145470524490SJens Axboe /*
1455aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1456aadd06e5SJens Axboe  * return an appropriate error.
1457aadd06e5SJens Axboe  */
14587c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1459aadd06e5SJens Axboe {
1460aadd06e5SJens Axboe 	int ret;
1461aadd06e5SJens Axboe 
1462aadd06e5SJens Axboe 	/*
14638cefc107SDavid Howells 	 * Check the pipe occupancy without the inode lock first. This function
1464aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1465aadd06e5SJens Axboe 	 */
14668cefc107SDavid Howells 	if (!pipe_empty(pipe->head, pipe->tail))
1467aadd06e5SJens Axboe 		return 0;
1468aadd06e5SJens Axboe 
1469aadd06e5SJens Axboe 	ret = 0;
147061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1471aadd06e5SJens Axboe 
14728cefc107SDavid Howells 	while (pipe_empty(pipe->head, pipe->tail)) {
1473aadd06e5SJens Axboe 		if (signal_pending(current)) {
1474aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1475aadd06e5SJens Axboe 			break;
1476aadd06e5SJens Axboe 		}
1477aadd06e5SJens Axboe 		if (!pipe->writers)
1478aadd06e5SJens Axboe 			break;
1479aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1480aadd06e5SJens Axboe 			ret = -EAGAIN;
1481aadd06e5SJens Axboe 			break;
1482aadd06e5SJens Axboe 		}
1483472e5b05SLinus Torvalds 		pipe_wait_readable(pipe);
1484aadd06e5SJens Axboe 	}
1485aadd06e5SJens Axboe 
148661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1487aadd06e5SJens Axboe 	return ret;
1488aadd06e5SJens Axboe }
1489aadd06e5SJens Axboe 
1490aadd06e5SJens Axboe /*
1491aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1492aadd06e5SJens Axboe  * return an appropriate error.
1493aadd06e5SJens Axboe  */
14947c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1495aadd06e5SJens Axboe {
1496aadd06e5SJens Axboe 	int ret;
1497aadd06e5SJens Axboe 
1498aadd06e5SJens Axboe 	/*
14998cefc107SDavid Howells 	 * Check pipe occupancy without the inode lock first. This function
1500aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1501aadd06e5SJens Axboe 	 */
1502566d1362STetsuo Handa 	if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1503aadd06e5SJens Axboe 		return 0;
1504aadd06e5SJens Axboe 
1505aadd06e5SJens Axboe 	ret = 0;
150661e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1507aadd06e5SJens Axboe 
15086718b6f8SDavid Howells 	while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1509aadd06e5SJens Axboe 		if (!pipe->readers) {
1510aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1511aadd06e5SJens Axboe 			ret = -EPIPE;
1512aadd06e5SJens Axboe 			break;
1513aadd06e5SJens Axboe 		}
1514aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1515aadd06e5SJens Axboe 			ret = -EAGAIN;
1516aadd06e5SJens Axboe 			break;
1517aadd06e5SJens Axboe 		}
1518aadd06e5SJens Axboe 		if (signal_pending(current)) {
1519aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1520aadd06e5SJens Axboe 			break;
1521aadd06e5SJens Axboe 		}
1522472e5b05SLinus Torvalds 		pipe_wait_writable(pipe);
1523aadd06e5SJens Axboe 	}
1524aadd06e5SJens Axboe 
152561e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1526aadd06e5SJens Axboe 	return ret;
1527aadd06e5SJens Axboe }
1528aadd06e5SJens Axboe 
1529aadd06e5SJens Axboe /*
15307c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
15317c77f0b3SMiklos Szeredi  */
15327c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
15337c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
15347c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
15357c77f0b3SMiklos Szeredi {
15367c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
15378cefc107SDavid Howells 	unsigned int i_head, o_head;
15388cefc107SDavid Howells 	unsigned int i_tail, o_tail;
15398cefc107SDavid Howells 	unsigned int i_mask, o_mask;
15408cefc107SDavid Howells 	int ret = 0;
15417c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
15427c77f0b3SMiklos Szeredi 
15437c77f0b3SMiklos Szeredi 
15447c77f0b3SMiklos Szeredi retry:
15457c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
15467c77f0b3SMiklos Szeredi 	if (ret)
15477c77f0b3SMiklos Szeredi 		return ret;
15487c77f0b3SMiklos Szeredi 
15497c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
15507c77f0b3SMiklos Szeredi 	if (ret)
15517c77f0b3SMiklos Szeredi 		return ret;
15527c77f0b3SMiklos Szeredi 
15537c77f0b3SMiklos Szeredi 	/*
15547c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
15557c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
15567c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
15577c77f0b3SMiklos Szeredi 	 */
15587c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
15597c77f0b3SMiklos Szeredi 
15608cefc107SDavid Howells 	i_tail = ipipe->tail;
15618cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
15628cefc107SDavid Howells 	o_head = opipe->head;
15638cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
15648cefc107SDavid Howells 
15657c77f0b3SMiklos Szeredi 	do {
15668cefc107SDavid Howells 		size_t o_len;
15678cefc107SDavid Howells 
15687c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
15697c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
15707c77f0b3SMiklos Szeredi 			if (!ret)
15717c77f0b3SMiklos Szeredi 				ret = -EPIPE;
15727c77f0b3SMiklos Szeredi 			break;
15737c77f0b3SMiklos Szeredi 		}
15747c77f0b3SMiklos Szeredi 
15758cefc107SDavid Howells 		i_head = ipipe->head;
15768cefc107SDavid Howells 		o_tail = opipe->tail;
15778cefc107SDavid Howells 
15788cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) && !ipipe->writers)
15797c77f0b3SMiklos Szeredi 			break;
15807c77f0b3SMiklos Szeredi 
15817c77f0b3SMiklos Szeredi 		/*
15827c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
15837c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
15847c77f0b3SMiklos Szeredi 		 */
15858cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
15866718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage)) {
15877c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
15887c77f0b3SMiklos Szeredi 			if (ret)
15897c77f0b3SMiklos Szeredi 				break;
15907c77f0b3SMiklos Szeredi 
15917c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
15927c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
15937c77f0b3SMiklos Szeredi 				break;
15947c77f0b3SMiklos Szeredi 			}
15957c77f0b3SMiklos Szeredi 
15967c77f0b3SMiklos Szeredi 			/*
15977c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
15987c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
15997c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
16007c77f0b3SMiklos Szeredi 			 */
16017c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
16027c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
16037c77f0b3SMiklos Szeredi 			goto retry;
16047c77f0b3SMiklos Szeredi 		}
16057c77f0b3SMiklos Szeredi 
16068cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
16078cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
16087c77f0b3SMiklos Szeredi 
16097c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
16107c77f0b3SMiklos Szeredi 			/*
16117c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
16127c77f0b3SMiklos Szeredi 			 */
16137c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16147c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
16158cefc107SDavid Howells 			i_tail++;
16168cefc107SDavid Howells 			ipipe->tail = i_tail;
16177c77f0b3SMiklos Szeredi 			input_wakeup = true;
16188cefc107SDavid Howells 			o_len = obuf->len;
16198cefc107SDavid Howells 			o_head++;
16208cefc107SDavid Howells 			opipe->head = o_head;
16217c77f0b3SMiklos Szeredi 		} else {
16227c77f0b3SMiklos Szeredi 			/*
16237c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
16247c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
16257c77f0b3SMiklos Szeredi 			 */
162615fab63eSMatthew Wilcox 			if (!pipe_buf_get(ipipe, ibuf)) {
162715fab63eSMatthew Wilcox 				if (ret == 0)
162815fab63eSMatthew Wilcox 					ret = -EFAULT;
162915fab63eSMatthew Wilcox 				break;
163015fab63eSMatthew Wilcox 			}
16317c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
16327c77f0b3SMiklos Szeredi 
16337c77f0b3SMiklos Szeredi 			/*
1634f6dd9755SChristoph Hellwig 			 * Don't inherit the gift and merge flags, we need to
16357c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
16367c77f0b3SMiklos Szeredi 			 */
16377c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1638f6dd9755SChristoph Hellwig 			obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1639a0ce2f0aSJann Horn 
16407c77f0b3SMiklos Szeredi 			obuf->len = len;
16418cefc107SDavid Howells 			ibuf->offset += len;
16428cefc107SDavid Howells 			ibuf->len -= len;
16438cefc107SDavid Howells 			o_len = len;
16448cefc107SDavid Howells 			o_head++;
16458cefc107SDavid Howells 			opipe->head = o_head;
16467c77f0b3SMiklos Szeredi 		}
16478cefc107SDavid Howells 		ret += o_len;
16488cefc107SDavid Howells 		len -= o_len;
16497c77f0b3SMiklos Szeredi 	} while (len);
16507c77f0b3SMiklos Szeredi 
16517c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
16527c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
16537c77f0b3SMiklos Szeredi 
16547c77f0b3SMiklos Szeredi 	/*
16557c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
16567c77f0b3SMiklos Szeredi 	 */
1657825cdcb1SNamhyung Kim 	if (ret > 0)
1658825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1659825cdcb1SNamhyung Kim 
16607c77f0b3SMiklos Szeredi 	if (input_wakeup)
16617c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
16627c77f0b3SMiklos Szeredi 
16637c77f0b3SMiklos Szeredi 	return ret;
16647c77f0b3SMiklos Szeredi }
16657c77f0b3SMiklos Szeredi 
16667c77f0b3SMiklos Szeredi /*
166770524490SJens Axboe  * Link contents of ipipe to opipe.
166870524490SJens Axboe  */
166970524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
167070524490SJens Axboe 		     struct pipe_inode_info *opipe,
167170524490SJens Axboe 		     size_t len, unsigned int flags)
167270524490SJens Axboe {
167370524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
16748cefc107SDavid Howells 	unsigned int i_head, o_head;
16758cefc107SDavid Howells 	unsigned int i_tail, o_tail;
16768cefc107SDavid Howells 	unsigned int i_mask, o_mask;
16778cefc107SDavid Howells 	int ret = 0;
167870524490SJens Axboe 
167970524490SJens Axboe 	/*
168070524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
168161e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
168270524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
168370524490SJens Axboe 	 */
168461e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
168570524490SJens Axboe 
16868cefc107SDavid Howells 	i_tail = ipipe->tail;
16878cefc107SDavid Howells 	i_mask = ipipe->ring_size - 1;
16888cefc107SDavid Howells 	o_head = opipe->head;
16898cefc107SDavid Howells 	o_mask = opipe->ring_size - 1;
16908cefc107SDavid Howells 
1691aadd06e5SJens Axboe 	do {
169270524490SJens Axboe 		if (!opipe->readers) {
169370524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
169470524490SJens Axboe 			if (!ret)
169570524490SJens Axboe 				ret = -EPIPE;
169670524490SJens Axboe 			break;
169770524490SJens Axboe 		}
169870524490SJens Axboe 
16998cefc107SDavid Howells 		i_head = ipipe->head;
17008cefc107SDavid Howells 		o_tail = opipe->tail;
17018cefc107SDavid Howells 
170270524490SJens Axboe 		/*
17038cefc107SDavid Howells 		 * If we have iterated all input buffers or run out of
1704aadd06e5SJens Axboe 		 * output room, break.
170570524490SJens Axboe 		 */
17068cefc107SDavid Howells 		if (pipe_empty(i_head, i_tail) ||
17076718b6f8SDavid Howells 		    pipe_full(o_head, o_tail, opipe->max_usage))
1708aadd06e5SJens Axboe 			break;
1709aadd06e5SJens Axboe 
17108cefc107SDavid Howells 		ibuf = &ipipe->bufs[i_tail & i_mask];
17118cefc107SDavid Howells 		obuf = &opipe->bufs[o_head & o_mask];
171270524490SJens Axboe 
171370524490SJens Axboe 		/*
171470524490SJens Axboe 		 * Get a reference to this pipe buffer,
171570524490SJens Axboe 		 * so we can copy the contents over.
171670524490SJens Axboe 		 */
171715fab63eSMatthew Wilcox 		if (!pipe_buf_get(ipipe, ibuf)) {
171815fab63eSMatthew Wilcox 			if (ret == 0)
171915fab63eSMatthew Wilcox 				ret = -EFAULT;
172015fab63eSMatthew Wilcox 			break;
172115fab63eSMatthew Wilcox 		}
172270524490SJens Axboe 
172370524490SJens Axboe 		*obuf = *ibuf;
172470524490SJens Axboe 
17257afa6fd0SJens Axboe 		/*
1726f6dd9755SChristoph Hellwig 		 * Don't inherit the gift and merge flag, we need to prevent
1727f6dd9755SChristoph Hellwig 		 * multiple steals of this page.
17287afa6fd0SJens Axboe 		 */
17297afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1730f6dd9755SChristoph Hellwig 		obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1731a0ce2f0aSJann Horn 
173270524490SJens Axboe 		if (obuf->len > len)
173370524490SJens Axboe 			obuf->len = len;
173470524490SJens Axboe 		ret += obuf->len;
173570524490SJens Axboe 		len -= obuf->len;
17368cefc107SDavid Howells 
17378cefc107SDavid Howells 		o_head++;
17388cefc107SDavid Howells 		opipe->head = o_head;
17398cefc107SDavid Howells 		i_tail++;
1740aadd06e5SJens Axboe 	} while (len);
174170524490SJens Axboe 
174261e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
174361e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
174470524490SJens Axboe 
1745aadd06e5SJens Axboe 	/*
1746aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1747aadd06e5SJens Axboe 	 */
1748825cdcb1SNamhyung Kim 	if (ret > 0)
1749825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
175070524490SJens Axboe 
175170524490SJens Axboe 	return ret;
175270524490SJens Axboe }
175370524490SJens Axboe 
175470524490SJens Axboe /*
175570524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
175670524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
175770524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
175870524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
175970524490SJens Axboe  */
17609dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
176170524490SJens Axboe {
1762c73be61cSDavid Howells 	struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1763c73be61cSDavid Howells 	struct pipe_inode_info *opipe = get_pipe_info(out, true);
1764aadd06e5SJens Axboe 	int ret = -EINVAL;
176570524490SJens Axboe 
176690da2e3fSPavel Begunkov 	if (unlikely(!(in->f_mode & FMODE_READ) ||
176790da2e3fSPavel Begunkov 		     !(out->f_mode & FMODE_WRITE)))
176890da2e3fSPavel Begunkov 		return -EBADF;
176990da2e3fSPavel Begunkov 
177070524490SJens Axboe 	/*
1771aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1772aadd06e5SJens Axboe 	 * copying the data.
177370524490SJens Axboe 	 */
1774aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1775ee5e0011SSlavomir Kaslev 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1776ee5e0011SSlavomir Kaslev 			flags |= SPLICE_F_NONBLOCK;
1777ee5e0011SSlavomir Kaslev 
1778aadd06e5SJens Axboe 		/*
1779aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1780aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1781aadd06e5SJens Axboe 		 */
17827c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1783aadd06e5SJens Axboe 		if (!ret) {
17847c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
178502cf01aeSJens Axboe 			if (!ret)
1786aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1787aadd06e5SJens Axboe 		}
1788aadd06e5SJens Axboe 	}
178970524490SJens Axboe 
1790aadd06e5SJens Axboe 	return ret;
179170524490SJens Axboe }
179270524490SJens Axboe 
1793836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
179470524490SJens Axboe {
179590da2e3fSPavel Begunkov 	struct fd in, out;
17962903ff01SAl Viro 	int error;
179770524490SJens Axboe 
17983d6ea290SAl Viro 	if (unlikely(flags & ~SPLICE_F_ALL))
17993d6ea290SAl Viro 		return -EINVAL;
18003d6ea290SAl Viro 
180170524490SJens Axboe 	if (unlikely(!len))
180270524490SJens Axboe 		return 0;
180370524490SJens Axboe 
180470524490SJens Axboe 	error = -EBADF;
18052903ff01SAl Viro 	in = fdget(fdin);
18062903ff01SAl Viro 	if (in.file) {
180790da2e3fSPavel Begunkov 		out = fdget(fdout);
18082903ff01SAl Viro 		if (out.file) {
180990da2e3fSPavel Begunkov 			error = do_tee(in.file, out.file, len, flags);
18102903ff01SAl Viro 			fdput(out);
181170524490SJens Axboe 		}
18122903ff01SAl Viro  		fdput(in);
181370524490SJens Axboe  	}
181470524490SJens Axboe 
181570524490SJens Axboe 	return error;
181670524490SJens Axboe }
1817