xref: /openbmc/linux/fs/splice.c (revision 35f9c09f)
15274f052SJens Axboe /*
25274f052SJens Axboe  * "splice": joining two ropes together by interweaving their strands.
35274f052SJens Axboe  *
45274f052SJens Axboe  * This is the "extended pipe" functionality, where a pipe is used as
55274f052SJens Axboe  * an arbitrary in-memory buffer. Think of a pipe as a small kernel
65274f052SJens Axboe  * buffer that you can use to transfer data from one end to the other.
75274f052SJens Axboe  *
85274f052SJens Axboe  * The traditional unix read/write is extended with a "splice()" operation
95274f052SJens Axboe  * that transfers data buffers to or from a pipe buffer.
105274f052SJens Axboe  *
115274f052SJens Axboe  * Named by Larry McVoy, original implementation from Linus, extended by
12c2058e06SJens Axboe  * Jens to support splicing to files, network, direct splicing, etc and
13c2058e06SJens Axboe  * fixing lots of bugs.
145274f052SJens Axboe  *
150fe23479SJens Axboe  * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
16c2058e06SJens Axboe  * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17c2058e06SJens Axboe  * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
185274f052SJens Axboe  *
195274f052SJens Axboe  */
205274f052SJens Axboe #include <linux/fs.h>
215274f052SJens Axboe #include <linux/file.h>
225274f052SJens Axboe #include <linux/pagemap.h>
23d6b29d7cSJens Axboe #include <linux/splice.h>
2408e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h>
255274f052SJens Axboe #include <linux/mm_inline.h>
265abc97aaSJens Axboe #include <linux/swap.h>
274f6f0bd2SJens Axboe #include <linux/writeback.h>
28630d9c47SPaul Gortmaker #include <linux/export.h>
294f6f0bd2SJens Axboe #include <linux/syscalls.h>
30912d35f8SJens Axboe #include <linux/uio.h>
3129ce2058SJames Morris #include <linux/security.h>
325a0e3ad6STejun Heo #include <linux/gfp.h>
3335f9c09fSEric Dumazet #include <linux/socket.h>
345274f052SJens Axboe 
3583f9135bSJens Axboe /*
3683f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
3783f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
3883f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
3983f9135bSJens Axboe  * attempt to reuse this page for another destination.
4083f9135bSJens Axboe  */
4176ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
425abc97aaSJens Axboe 				     struct pipe_buffer *buf)
435abc97aaSJens Axboe {
445abc97aaSJens Axboe 	struct page *page = buf->page;
459e94cd4fSJens Axboe 	struct address_space *mapping;
465abc97aaSJens Axboe 
479e0267c2SJens Axboe 	lock_page(page);
489e0267c2SJens Axboe 
499e94cd4fSJens Axboe 	mapping = page_mapping(page);
509e94cd4fSJens Axboe 	if (mapping) {
515abc97aaSJens Axboe 		WARN_ON(!PageUptodate(page));
525abc97aaSJens Axboe 
53ad8d6f0aSJens Axboe 		/*
549e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
559e94cd4fSJens Axboe 		 * writeback completing on this page, since we'll remove it
569e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
579e94cd4fSJens Axboe 		 * page, allowing the disk blocks to be reused by someone else
589e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
599e94cd4fSJens Axboe 		 * ensues.
60ad8d6f0aSJens Axboe 		 */
61ad8d6f0aSJens Axboe 		wait_on_page_writeback(page);
62ad8d6f0aSJens Axboe 
63266cf658SDavid Howells 		if (page_has_private(page) &&
64266cf658SDavid Howells 		    !try_to_release_page(page, GFP_KERNEL))
65ca39d651SJens Axboe 			goto out_unlock;
664f6f0bd2SJens Axboe 
679e94cd4fSJens Axboe 		/*
689e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
699e94cd4fSJens Axboe 		 * and return good.
709e94cd4fSJens Axboe 		 */
719e94cd4fSJens Axboe 		if (remove_mapping(mapping, page)) {
721432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
735abc97aaSJens Axboe 			return 0;
745abc97aaSJens Axboe 		}
759e94cd4fSJens Axboe 	}
769e94cd4fSJens Axboe 
779e94cd4fSJens Axboe 	/*
789e94cd4fSJens Axboe 	 * Raced with truncate or failed to remove page from current
799e94cd4fSJens Axboe 	 * address space, unlock and return failure.
809e94cd4fSJens Axboe 	 */
81ca39d651SJens Axboe out_unlock:
829e94cd4fSJens Axboe 	unlock_page(page);
839e94cd4fSJens Axboe 	return 1;
849e94cd4fSJens Axboe }
855abc97aaSJens Axboe 
8676ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
875274f052SJens Axboe 					struct pipe_buffer *buf)
885274f052SJens Axboe {
895274f052SJens Axboe 	page_cache_release(buf->page);
901432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
915274f052SJens Axboe }
925274f052SJens Axboe 
930845718dSJens Axboe /*
940845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
950845718dSJens Axboe  * is a page cache page, IO may be in flight.
960845718dSJens Axboe  */
97cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
985274f052SJens Axboe 				       struct pipe_buffer *buf)
995274f052SJens Axboe {
1005274f052SJens Axboe 	struct page *page = buf->page;
10149d0b21bSJens Axboe 	int err;
1025274f052SJens Axboe 
1035274f052SJens Axboe 	if (!PageUptodate(page)) {
10449d0b21bSJens Axboe 		lock_page(page);
1055274f052SJens Axboe 
10649d0b21bSJens Axboe 		/*
10749d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
10873d62d83SIngo Molnar 		 * splice, if this is the first page.
10949d0b21bSJens Axboe 		 */
1105274f052SJens Axboe 		if (!page->mapping) {
11149d0b21bSJens Axboe 			err = -ENODATA;
11249d0b21bSJens Axboe 			goto error;
1135274f052SJens Axboe 		}
1145274f052SJens Axboe 
11549d0b21bSJens Axboe 		/*
11673d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
11749d0b21bSJens Axboe 		 */
11849d0b21bSJens Axboe 		if (!PageUptodate(page)) {
11949d0b21bSJens Axboe 			err = -EIO;
12049d0b21bSJens Axboe 			goto error;
12149d0b21bSJens Axboe 		}
12249d0b21bSJens Axboe 
12349d0b21bSJens Axboe 		/*
124f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
12549d0b21bSJens Axboe 		 */
12649d0b21bSJens Axboe 		unlock_page(page);
12749d0b21bSJens Axboe 	}
12849d0b21bSJens Axboe 
129f84d7519SJens Axboe 	return 0;
13049d0b21bSJens Axboe error:
13149d0b21bSJens Axboe 	unlock_page(page);
132f84d7519SJens Axboe 	return err;
13370524490SJens Axboe }
13470524490SJens Axboe 
135708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
1365274f052SJens Axboe 	.can_merge = 0,
137f84d7519SJens Axboe 	.map = generic_pipe_buf_map,
138f84d7519SJens Axboe 	.unmap = generic_pipe_buf_unmap,
139cac36bb0SJens Axboe 	.confirm = page_cache_pipe_buf_confirm,
1405274f052SJens Axboe 	.release = page_cache_pipe_buf_release,
1415abc97aaSJens Axboe 	.steal = page_cache_pipe_buf_steal,
142f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
1435274f052SJens Axboe };
1445274f052SJens Axboe 
145912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
146912d35f8SJens Axboe 				    struct pipe_buffer *buf)
147912d35f8SJens Axboe {
1487afa6fd0SJens Axboe 	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
149912d35f8SJens Axboe 		return 1;
1507afa6fd0SJens Axboe 
1511432873aSJens Axboe 	buf->flags |= PIPE_BUF_FLAG_LRU;
152330ab716SJens Axboe 	return generic_pipe_buf_steal(pipe, buf);
153912d35f8SJens Axboe }
154912d35f8SJens Axboe 
155d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = {
156912d35f8SJens Axboe 	.can_merge = 0,
157f84d7519SJens Axboe 	.map = generic_pipe_buf_map,
158f84d7519SJens Axboe 	.unmap = generic_pipe_buf_unmap,
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;
188912d35f8SJens Axboe 	int ret, do_wakeup, page_nr;
1895274f052SJens Axboe 
1905274f052SJens Axboe 	ret = 0;
1915274f052SJens Axboe 	do_wakeup = 0;
192912d35f8SJens Axboe 	page_nr = 0;
1935274f052SJens Axboe 
19461e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1955274f052SJens Axboe 
1965274f052SJens Axboe 	for (;;) {
1973a326a2cSIngo Molnar 		if (!pipe->readers) {
1985274f052SJens Axboe 			send_sig(SIGPIPE, current, 0);
1995274f052SJens Axboe 			if (!ret)
2005274f052SJens Axboe 				ret = -EPIPE;
2015274f052SJens Axboe 			break;
2025274f052SJens Axboe 		}
2035274f052SJens Axboe 
20435f3d14dSJens Axboe 		if (pipe->nrbufs < pipe->buffers) {
20535f3d14dSJens Axboe 			int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
2063a326a2cSIngo Molnar 			struct pipe_buffer *buf = pipe->bufs + newbuf;
2075274f052SJens Axboe 
208912d35f8SJens Axboe 			buf->page = spd->pages[page_nr];
209912d35f8SJens Axboe 			buf->offset = spd->partial[page_nr].offset;
210912d35f8SJens Axboe 			buf->len = spd->partial[page_nr].len;
211497f9625SJens Axboe 			buf->private = spd->partial[page_nr].private;
212912d35f8SJens Axboe 			buf->ops = spd->ops;
2137afa6fd0SJens Axboe 			if (spd->flags & SPLICE_F_GIFT)
2147afa6fd0SJens Axboe 				buf->flags |= PIPE_BUF_FLAG_GIFT;
2157afa6fd0SJens Axboe 
2166f767b04SJens Axboe 			pipe->nrbufs++;
217912d35f8SJens Axboe 			page_nr++;
218912d35f8SJens Axboe 			ret += buf->len;
219912d35f8SJens Axboe 
2206f767b04SJens Axboe 			if (pipe->inode)
2215274f052SJens Axboe 				do_wakeup = 1;
2225274f052SJens Axboe 
223912d35f8SJens Axboe 			if (!--spd->nr_pages)
2245274f052SJens Axboe 				break;
22535f3d14dSJens Axboe 			if (pipe->nrbufs < pipe->buffers)
2265274f052SJens Axboe 				continue;
2275274f052SJens Axboe 
2285274f052SJens Axboe 			break;
2295274f052SJens Axboe 		}
2305274f052SJens Axboe 
231912d35f8SJens Axboe 		if (spd->flags & SPLICE_F_NONBLOCK) {
23229e35094SLinus Torvalds 			if (!ret)
23329e35094SLinus Torvalds 				ret = -EAGAIN;
23429e35094SLinus Torvalds 			break;
23529e35094SLinus Torvalds 		}
23629e35094SLinus Torvalds 
2375274f052SJens Axboe 		if (signal_pending(current)) {
2385274f052SJens Axboe 			if (!ret)
2395274f052SJens Axboe 				ret = -ERESTARTSYS;
2405274f052SJens Axboe 			break;
2415274f052SJens Axboe 		}
2425274f052SJens Axboe 
2435274f052SJens Axboe 		if (do_wakeup) {
244c0bd1f65SJens Axboe 			smp_mb();
2453a326a2cSIngo Molnar 			if (waitqueue_active(&pipe->wait))
2463a326a2cSIngo Molnar 				wake_up_interruptible_sync(&pipe->wait);
2473a326a2cSIngo Molnar 			kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
2485274f052SJens Axboe 			do_wakeup = 0;
2495274f052SJens Axboe 		}
2505274f052SJens Axboe 
2513a326a2cSIngo Molnar 		pipe->waiting_writers++;
2523a326a2cSIngo Molnar 		pipe_wait(pipe);
2533a326a2cSIngo Molnar 		pipe->waiting_writers--;
2545274f052SJens Axboe 	}
2555274f052SJens Axboe 
25661e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
2575274f052SJens Axboe 
258825cdcb1SNamhyung Kim 	if (do_wakeup)
259825cdcb1SNamhyung Kim 		wakeup_pipe_readers(pipe);
2605274f052SJens Axboe 
26100de00bdSJens Axboe 	while (page_nr < spd_pages)
262bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2635274f052SJens Axboe 
2645274f052SJens Axboe 	return ret;
2655274f052SJens Axboe }
2665274f052SJens Axboe 
267708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
268bbdfc2f7SJens Axboe {
269bbdfc2f7SJens Axboe 	page_cache_release(spd->pages[i]);
270bbdfc2f7SJens Axboe }
271bbdfc2f7SJens Axboe 
27235f3d14dSJens Axboe /*
27335f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
27435f3d14dSJens Axboe  * descriptions.
27535f3d14dSJens Axboe  */
27635f3d14dSJens Axboe int splice_grow_spd(struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
27735f3d14dSJens Axboe {
27835f3d14dSJens Axboe 	if (pipe->buffers <= PIPE_DEF_BUFFERS)
27935f3d14dSJens Axboe 		return 0;
28035f3d14dSJens Axboe 
28135f3d14dSJens Axboe 	spd->pages = kmalloc(pipe->buffers * sizeof(struct page *), GFP_KERNEL);
28235f3d14dSJens Axboe 	spd->partial = kmalloc(pipe->buffers * sizeof(struct partial_page), GFP_KERNEL);
28335f3d14dSJens Axboe 
28435f3d14dSJens Axboe 	if (spd->pages && spd->partial)
28535f3d14dSJens Axboe 		return 0;
28635f3d14dSJens Axboe 
28735f3d14dSJens Axboe 	kfree(spd->pages);
28835f3d14dSJens Axboe 	kfree(spd->partial);
28935f3d14dSJens Axboe 	return -ENOMEM;
29035f3d14dSJens Axboe }
29135f3d14dSJens Axboe 
29235f3d14dSJens Axboe void splice_shrink_spd(struct pipe_inode_info *pipe,
29335f3d14dSJens Axboe 		       struct splice_pipe_desc *spd)
29435f3d14dSJens Axboe {
29535f3d14dSJens Axboe 	if (pipe->buffers <= PIPE_DEF_BUFFERS)
29635f3d14dSJens Axboe 		return;
29735f3d14dSJens Axboe 
29835f3d14dSJens Axboe 	kfree(spd->pages);
29935f3d14dSJens Axboe 	kfree(spd->partial);
30035f3d14dSJens Axboe }
30135f3d14dSJens Axboe 
3023a326a2cSIngo Molnar static int
303cbb7e577SJens Axboe __generic_file_splice_read(struct file *in, loff_t *ppos,
304cbb7e577SJens Axboe 			   struct pipe_inode_info *pipe, size_t len,
305cbb7e577SJens Axboe 			   unsigned int flags)
3065274f052SJens Axboe {
3075274f052SJens Axboe 	struct address_space *mapping = in->f_mapping;
308d8983910SFengguang Wu 	unsigned int loff, nr_pages, req_pages;
30935f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
31035f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
3115274f052SJens Axboe 	struct page *page;
31291ad66efSJens Axboe 	pgoff_t index, end_index;
31391ad66efSJens Axboe 	loff_t isize;
314eb20796bSJens Axboe 	int error, page_nr;
315912d35f8SJens Axboe 	struct splice_pipe_desc spd = {
316912d35f8SJens Axboe 		.pages = pages,
317912d35f8SJens Axboe 		.partial = partial,
318912d35f8SJens Axboe 		.flags = flags,
319912d35f8SJens Axboe 		.ops = &page_cache_pipe_buf_ops,
320bbdfc2f7SJens Axboe 		.spd_release = spd_release_page,
321912d35f8SJens Axboe 	};
3225274f052SJens Axboe 
32335f3d14dSJens Axboe 	if (splice_grow_spd(pipe, &spd))
32435f3d14dSJens Axboe 		return -ENOMEM;
32535f3d14dSJens Axboe 
326cbb7e577SJens Axboe 	index = *ppos >> PAGE_CACHE_SHIFT;
327912d35f8SJens Axboe 	loff = *ppos & ~PAGE_CACHE_MASK;
328d8983910SFengguang Wu 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
32935f3d14dSJens Axboe 	nr_pages = min(req_pages, pipe->buffers);
3305274f052SJens Axboe 
3315274f052SJens Axboe 	/*
332eb20796bSJens Axboe 	 * Lookup the (hopefully) full range of pages we need.
33382aa5d61SJens Axboe 	 */
33435f3d14dSJens Axboe 	spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
335431a4820SFengguang Wu 	index += spd.nr_pages;
336eb20796bSJens Axboe 
3375274f052SJens Axboe 	/*
338eb20796bSJens Axboe 	 * If find_get_pages_contig() returned fewer pages than we needed,
339431a4820SFengguang Wu 	 * readahead/allocate the rest and fill in the holes.
340eb20796bSJens Axboe 	 */
341431a4820SFengguang Wu 	if (spd.nr_pages < nr_pages)
342cf914a7dSRusty Russell 		page_cache_sync_readahead(mapping, &in->f_ra, in,
343cf914a7dSRusty Russell 				index, req_pages - spd.nr_pages);
344431a4820SFengguang Wu 
345932cc6d4SJens Axboe 	error = 0;
346eb20796bSJens Axboe 	while (spd.nr_pages < nr_pages) {
347eb20796bSJens Axboe 		/*
348eb20796bSJens Axboe 		 * Page could be there, find_get_pages_contig() breaks on
349eb20796bSJens Axboe 		 * the first hole.
3505274f052SJens Axboe 		 */
3517480a904SJens Axboe 		page = find_get_page(mapping, index);
3527480a904SJens Axboe 		if (!page) {
353e27dedd8SJens Axboe 			/*
354eb20796bSJens Axboe 			 * page didn't exist, allocate one.
3557480a904SJens Axboe 			 */
3567480a904SJens Axboe 			page = page_cache_alloc_cold(mapping);
3575274f052SJens Axboe 			if (!page)
3585274f052SJens Axboe 				break;
3595274f052SJens Axboe 
3607480a904SJens Axboe 			error = add_to_page_cache_lru(page, mapping, index,
3610ae0b5d0SNick Piggin 						GFP_KERNEL);
3625274f052SJens Axboe 			if (unlikely(error)) {
3635274f052SJens Axboe 				page_cache_release(page);
364a0548871SJens Axboe 				if (error == -EEXIST)
365a0548871SJens Axboe 					continue;
3665274f052SJens Axboe 				break;
3675274f052SJens Axboe 			}
368eb20796bSJens Axboe 			/*
369eb20796bSJens Axboe 			 * add_to_page_cache() locks the page, unlock it
370eb20796bSJens Axboe 			 * to avoid convoluting the logic below even more.
371eb20796bSJens Axboe 			 */
372eb20796bSJens Axboe 			unlock_page(page);
3735274f052SJens Axboe 		}
3747480a904SJens Axboe 
37535f3d14dSJens Axboe 		spd.pages[spd.nr_pages++] = page;
376eb20796bSJens Axboe 		index++;
377eb20796bSJens Axboe 	}
378eb20796bSJens Axboe 
379eb20796bSJens Axboe 	/*
380eb20796bSJens Axboe 	 * Now loop over the map and see if we need to start IO on any
381eb20796bSJens Axboe 	 * pages, fill in the partial map, etc.
382eb20796bSJens Axboe 	 */
383eb20796bSJens Axboe 	index = *ppos >> PAGE_CACHE_SHIFT;
384eb20796bSJens Axboe 	nr_pages = spd.nr_pages;
385eb20796bSJens Axboe 	spd.nr_pages = 0;
386eb20796bSJens Axboe 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
387eb20796bSJens Axboe 		unsigned int this_len;
388eb20796bSJens Axboe 
389eb20796bSJens Axboe 		if (!len)
390eb20796bSJens Axboe 			break;
391eb20796bSJens Axboe 
392eb20796bSJens Axboe 		/*
393eb20796bSJens Axboe 		 * this_len is the max we'll use from this page
394eb20796bSJens Axboe 		 */
395eb20796bSJens Axboe 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
39635f3d14dSJens Axboe 		page = spd.pages[page_nr];
397eb20796bSJens Axboe 
398a08a166fSFengguang Wu 		if (PageReadahead(page))
399cf914a7dSRusty Russell 			page_cache_async_readahead(mapping, &in->f_ra, in,
400d8983910SFengguang Wu 					page, index, req_pages - page_nr);
401a08a166fSFengguang Wu 
4027480a904SJens Axboe 		/*
4037480a904SJens Axboe 		 * If the page isn't uptodate, we may need to start io on it
4047480a904SJens Axboe 		 */
4057480a904SJens Axboe 		if (!PageUptodate(page)) {
4067480a904SJens Axboe 			lock_page(page);
4077480a904SJens Axboe 
4087480a904SJens Axboe 			/*
40932502b84SMiklos Szeredi 			 * Page was truncated, or invalidated by the
41032502b84SMiklos Szeredi 			 * filesystem.  Redo the find/create, but this time the
41132502b84SMiklos Szeredi 			 * page is kept locked, so there's no chance of another
41232502b84SMiklos Szeredi 			 * race with truncate/invalidate.
4137480a904SJens Axboe 			 */
4147480a904SJens Axboe 			if (!page->mapping) {
4157480a904SJens Axboe 				unlock_page(page);
41632502b84SMiklos Szeredi 				page = find_or_create_page(mapping, index,
41732502b84SMiklos Szeredi 						mapping_gfp_mask(mapping));
41832502b84SMiklos Szeredi 
41932502b84SMiklos Szeredi 				if (!page) {
42032502b84SMiklos Szeredi 					error = -ENOMEM;
4217480a904SJens Axboe 					break;
4227480a904SJens Axboe 				}
42335f3d14dSJens Axboe 				page_cache_release(spd.pages[page_nr]);
42435f3d14dSJens Axboe 				spd.pages[page_nr] = page;
42532502b84SMiklos Szeredi 			}
4267480a904SJens Axboe 			/*
4277480a904SJens Axboe 			 * page was already under io and is now done, great
4287480a904SJens Axboe 			 */
4297480a904SJens Axboe 			if (PageUptodate(page)) {
4307480a904SJens Axboe 				unlock_page(page);
4317480a904SJens Axboe 				goto fill_it;
4327480a904SJens Axboe 			}
4337480a904SJens Axboe 
4347480a904SJens Axboe 			/*
4357480a904SJens Axboe 			 * need to read in the page
4367480a904SJens Axboe 			 */
4377480a904SJens Axboe 			error = mapping->a_ops->readpage(in, page);
4387480a904SJens Axboe 			if (unlikely(error)) {
439eb20796bSJens Axboe 				/*
440eb20796bSJens Axboe 				 * We really should re-lookup the page here,
441eb20796bSJens Axboe 				 * but it complicates things a lot. Instead
442eb20796bSJens Axboe 				 * lets just do what we already stored, and
443eb20796bSJens Axboe 				 * we'll get it the next time we are called.
444eb20796bSJens Axboe 				 */
4457480a904SJens Axboe 				if (error == AOP_TRUNCATED_PAGE)
446eb20796bSJens Axboe 					error = 0;
447eb20796bSJens Axboe 
4487480a904SJens Axboe 				break;
4497480a904SJens Axboe 			}
450620a324bSJens Axboe 		}
451620a324bSJens Axboe fill_it:
45291ad66efSJens Axboe 		/*
453620a324bSJens Axboe 		 * i_size must be checked after PageUptodate.
45491ad66efSJens Axboe 		 */
45591ad66efSJens Axboe 		isize = i_size_read(mapping->host);
45691ad66efSJens Axboe 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
457eb20796bSJens Axboe 		if (unlikely(!isize || index > end_index))
45891ad66efSJens Axboe 			break;
45991ad66efSJens Axboe 
46091ad66efSJens Axboe 		/*
46191ad66efSJens Axboe 		 * if this is the last page, see if we need to shrink
46291ad66efSJens Axboe 		 * the length and stop
46391ad66efSJens Axboe 		 */
46491ad66efSJens Axboe 		if (end_index == index) {
465475ecadeSHugh Dickins 			unsigned int plen;
466475ecadeSHugh Dickins 
467475ecadeSHugh Dickins 			/*
468475ecadeSHugh Dickins 			 * max good bytes in this page
469475ecadeSHugh Dickins 			 */
470475ecadeSHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
471475ecadeSHugh Dickins 			if (plen <= loff)
47291ad66efSJens Axboe 				break;
473475ecadeSHugh Dickins 
47491ad66efSJens Axboe 			/*
47591ad66efSJens Axboe 			 * force quit after adding this page
47691ad66efSJens Axboe 			 */
477475ecadeSHugh Dickins 			this_len = min(this_len, plen - loff);
478eb20796bSJens Axboe 			len = this_len;
47991ad66efSJens Axboe 		}
480620a324bSJens Axboe 
48135f3d14dSJens Axboe 		spd.partial[page_nr].offset = loff;
48235f3d14dSJens Axboe 		spd.partial[page_nr].len = this_len;
48382aa5d61SJens Axboe 		len -= this_len;
48491ad66efSJens Axboe 		loff = 0;
485eb20796bSJens Axboe 		spd.nr_pages++;
486eb20796bSJens Axboe 		index++;
4875274f052SJens Axboe 	}
4885274f052SJens Axboe 
489eb20796bSJens Axboe 	/*
490475ecadeSHugh Dickins 	 * Release any pages at the end, if we quit early. 'page_nr' is how far
491eb20796bSJens Axboe 	 * we got, 'nr_pages' is how many pages are in the map.
492eb20796bSJens Axboe 	 */
493eb20796bSJens Axboe 	while (page_nr < nr_pages)
49435f3d14dSJens Axboe 		page_cache_release(spd.pages[page_nr++]);
495f4e6b498SFengguang Wu 	in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
496eb20796bSJens Axboe 
497912d35f8SJens Axboe 	if (spd.nr_pages)
49835f3d14dSJens Axboe 		error = splice_to_pipe(pipe, &spd);
49916c523ddSJens Axboe 
50035f3d14dSJens Axboe 	splice_shrink_spd(pipe, &spd);
5017480a904SJens Axboe 	return error;
5025274f052SJens Axboe }
5035274f052SJens Axboe 
50483f9135bSJens Axboe /**
50583f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
50683f9135bSJens Axboe  * @in:		file to splice from
507932cc6d4SJens Axboe  * @ppos:	position in @in
50883f9135bSJens Axboe  * @pipe:	pipe to splice to
50983f9135bSJens Axboe  * @len:	number of bytes to splice
51083f9135bSJens Axboe  * @flags:	splice modifier flags
51183f9135bSJens Axboe  *
512932cc6d4SJens Axboe  * Description:
513932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
514932cc6d4SJens Axboe  *    used as long as the address_space operations for the source implements
515932cc6d4SJens Axboe  *    a readpage() hook.
516932cc6d4SJens Axboe  *
51783f9135bSJens Axboe  */
518cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
519cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
520cbb7e577SJens Axboe 				 unsigned int flags)
5215274f052SJens Axboe {
522d366d398SJens Axboe 	loff_t isize, left;
5238191ecd1SJens Axboe 	int ret;
524d366d398SJens Axboe 
525d366d398SJens Axboe 	isize = i_size_read(in->f_mapping->host);
526d366d398SJens Axboe 	if (unlikely(*ppos >= isize))
527d366d398SJens Axboe 		return 0;
528d366d398SJens Axboe 
529d366d398SJens Axboe 	left = isize - *ppos;
530d366d398SJens Axboe 	if (unlikely(left < len))
531d366d398SJens Axboe 		len = left;
5325274f052SJens Axboe 
533cbb7e577SJens Axboe 	ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
534723590edSMiklos Szeredi 	if (ret > 0) {
535cbb7e577SJens Axboe 		*ppos += ret;
536723590edSMiklos Szeredi 		file_accessed(in);
537723590edSMiklos Szeredi 	}
5385274f052SJens Axboe 
5395274f052SJens Axboe 	return ret;
5405274f052SJens Axboe }
541059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
542059a8f37SJens Axboe 
5436818173bSMiklos Szeredi static const struct pipe_buf_operations default_pipe_buf_ops = {
5446818173bSMiklos Szeredi 	.can_merge = 0,
5456818173bSMiklos Szeredi 	.map = generic_pipe_buf_map,
5466818173bSMiklos Szeredi 	.unmap = generic_pipe_buf_unmap,
5476818173bSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
5486818173bSMiklos Szeredi 	.release = generic_pipe_buf_release,
5496818173bSMiklos Szeredi 	.steal = generic_pipe_buf_steal,
5506818173bSMiklos Szeredi 	.get = generic_pipe_buf_get,
5516818173bSMiklos Szeredi };
5526818173bSMiklos Szeredi 
5536818173bSMiklos Szeredi static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
5546818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
5556818173bSMiklos Szeredi {
5566818173bSMiklos Szeredi 	mm_segment_t old_fs;
5576818173bSMiklos Szeredi 	loff_t pos = offset;
5586818173bSMiklos Szeredi 	ssize_t res;
5596818173bSMiklos Szeredi 
5606818173bSMiklos Szeredi 	old_fs = get_fs();
5616818173bSMiklos Szeredi 	set_fs(get_ds());
5626818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
5636818173bSMiklos Szeredi 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
5646818173bSMiklos Szeredi 	set_fs(old_fs);
5656818173bSMiklos Szeredi 
5666818173bSMiklos Szeredi 	return res;
5676818173bSMiklos Szeredi }
5686818173bSMiklos Szeredi 
569b2858d7dSMiklos Szeredi static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
570b2858d7dSMiklos Szeredi 			    loff_t pos)
5710b0a47f5SMiklos Szeredi {
5720b0a47f5SMiklos Szeredi 	mm_segment_t old_fs;
5730b0a47f5SMiklos Szeredi 	ssize_t res;
5740b0a47f5SMiklos Szeredi 
5750b0a47f5SMiklos Szeredi 	old_fs = get_fs();
5760b0a47f5SMiklos Szeredi 	set_fs(get_ds());
5770b0a47f5SMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
578b2858d7dSMiklos Szeredi 	res = vfs_write(file, (const char __user *)buf, count, &pos);
5790b0a47f5SMiklos Szeredi 	set_fs(old_fs);
5800b0a47f5SMiklos Szeredi 
5810b0a47f5SMiklos Szeredi 	return res;
5820b0a47f5SMiklos Szeredi }
5830b0a47f5SMiklos Szeredi 
5846818173bSMiklos Szeredi ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
5856818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
5866818173bSMiklos Szeredi 				 unsigned int flags)
5876818173bSMiklos Szeredi {
5886818173bSMiklos Szeredi 	unsigned int nr_pages;
5896818173bSMiklos Szeredi 	unsigned int nr_freed;
5906818173bSMiklos Szeredi 	size_t offset;
59135f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
59235f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
59335f3d14dSJens Axboe 	struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
5946818173bSMiklos Szeredi 	ssize_t res;
5956818173bSMiklos Szeredi 	size_t this_len;
5966818173bSMiklos Szeredi 	int error;
5976818173bSMiklos Szeredi 	int i;
5986818173bSMiklos Szeredi 	struct splice_pipe_desc spd = {
5996818173bSMiklos Szeredi 		.pages = pages,
6006818173bSMiklos Szeredi 		.partial = partial,
6016818173bSMiklos Szeredi 		.flags = flags,
6026818173bSMiklos Szeredi 		.ops = &default_pipe_buf_ops,
6036818173bSMiklos Szeredi 		.spd_release = spd_release_page,
6046818173bSMiklos Szeredi 	};
6056818173bSMiklos Szeredi 
60635f3d14dSJens Axboe 	if (splice_grow_spd(pipe, &spd))
60735f3d14dSJens Axboe 		return -ENOMEM;
60835f3d14dSJens Axboe 
60935f3d14dSJens Axboe 	res = -ENOMEM;
61035f3d14dSJens Axboe 	vec = __vec;
61135f3d14dSJens Axboe 	if (pipe->buffers > PIPE_DEF_BUFFERS) {
61235f3d14dSJens Axboe 		vec = kmalloc(pipe->buffers * sizeof(struct iovec), GFP_KERNEL);
61335f3d14dSJens Axboe 		if (!vec)
61435f3d14dSJens Axboe 			goto shrink_ret;
61535f3d14dSJens Axboe 	}
61635f3d14dSJens Axboe 
6176818173bSMiklos Szeredi 	offset = *ppos & ~PAGE_CACHE_MASK;
6186818173bSMiklos Szeredi 	nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
6196818173bSMiklos Szeredi 
62035f3d14dSJens Axboe 	for (i = 0; i < nr_pages && i < pipe->buffers && len; i++) {
6216818173bSMiklos Szeredi 		struct page *page;
6226818173bSMiklos Szeredi 
6234f231228SJens Axboe 		page = alloc_page(GFP_USER);
6246818173bSMiklos Szeredi 		error = -ENOMEM;
6256818173bSMiklos Szeredi 		if (!page)
6266818173bSMiklos Szeredi 			goto err;
6276818173bSMiklos Szeredi 
6286818173bSMiklos Szeredi 		this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
6294f231228SJens Axboe 		vec[i].iov_base = (void __user *) page_address(page);
6306818173bSMiklos Szeredi 		vec[i].iov_len = this_len;
63135f3d14dSJens Axboe 		spd.pages[i] = page;
6326818173bSMiklos Szeredi 		spd.nr_pages++;
6336818173bSMiklos Szeredi 		len -= this_len;
6346818173bSMiklos Szeredi 		offset = 0;
6356818173bSMiklos Szeredi 	}
6366818173bSMiklos Szeredi 
6376818173bSMiklos Szeredi 	res = kernel_readv(in, vec, spd.nr_pages, *ppos);
63877f6bf57SAndrew Morton 	if (res < 0) {
63977f6bf57SAndrew Morton 		error = res;
6406818173bSMiklos Szeredi 		goto err;
64177f6bf57SAndrew Morton 	}
6426818173bSMiklos Szeredi 
6436818173bSMiklos Szeredi 	error = 0;
6446818173bSMiklos Szeredi 	if (!res)
6456818173bSMiklos Szeredi 		goto err;
6466818173bSMiklos Szeredi 
6476818173bSMiklos Szeredi 	nr_freed = 0;
6486818173bSMiklos Szeredi 	for (i = 0; i < spd.nr_pages; i++) {
6496818173bSMiklos Szeredi 		this_len = min_t(size_t, vec[i].iov_len, res);
65035f3d14dSJens Axboe 		spd.partial[i].offset = 0;
65135f3d14dSJens Axboe 		spd.partial[i].len = this_len;
6526818173bSMiklos Szeredi 		if (!this_len) {
65335f3d14dSJens Axboe 			__free_page(spd.pages[i]);
65435f3d14dSJens Axboe 			spd.pages[i] = NULL;
6556818173bSMiklos Szeredi 			nr_freed++;
6566818173bSMiklos Szeredi 		}
6576818173bSMiklos Szeredi 		res -= this_len;
6586818173bSMiklos Szeredi 	}
6596818173bSMiklos Szeredi 	spd.nr_pages -= nr_freed;
6606818173bSMiklos Szeredi 
6616818173bSMiklos Szeredi 	res = splice_to_pipe(pipe, &spd);
6626818173bSMiklos Szeredi 	if (res > 0)
6636818173bSMiklos Szeredi 		*ppos += res;
6646818173bSMiklos Szeredi 
66535f3d14dSJens Axboe shrink_ret:
66635f3d14dSJens Axboe 	if (vec != __vec)
66735f3d14dSJens Axboe 		kfree(vec);
66835f3d14dSJens Axboe 	splice_shrink_spd(pipe, &spd);
6696818173bSMiklos Szeredi 	return res;
6706818173bSMiklos Szeredi 
6716818173bSMiklos Szeredi err:
6724f231228SJens Axboe 	for (i = 0; i < spd.nr_pages; i++)
67335f3d14dSJens Axboe 		__free_page(spd.pages[i]);
6744f231228SJens Axboe 
67535f3d14dSJens Axboe 	res = error;
67635f3d14dSJens Axboe 	goto shrink_ret;
6776818173bSMiklos Szeredi }
6786818173bSMiklos Szeredi EXPORT_SYMBOL(default_file_splice_read);
6796818173bSMiklos Szeredi 
6805274f052SJens Axboe /*
6814f6f0bd2SJens Axboe  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
682016b661eSJens Axboe  * using sendpage(). Return the number of bytes sent.
6835274f052SJens Axboe  */
68476ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe,
6855274f052SJens Axboe 			    struct pipe_buffer *buf, struct splice_desc *sd)
6865274f052SJens Axboe {
6876a14b90bSJens Axboe 	struct file *file = sd->u.file;
6885274f052SJens Axboe 	loff_t pos = sd->pos;
689a8adbe37SMichał Mirosław 	int more;
6905274f052SJens Axboe 
691a8adbe37SMichał Mirosław 	if (!likely(file->f_op && file->f_op->sendpage))
692a8adbe37SMichał Mirosław 		return -EINVAL;
693a8adbe37SMichał Mirosław 
69435f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
69535f9c09fSEric Dumazet 	if (sd->len < sd->total_len)
69635f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
697a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
698f84d7519SJens Axboe 				    sd->len, &pos, more);
6995274f052SJens Axboe }
7005274f052SJens Axboe 
7015274f052SJens Axboe /*
7025274f052SJens Axboe  * This is a little more tricky than the file -> pipe splicing. There are
7035274f052SJens Axboe  * basically three cases:
7045274f052SJens Axboe  *
7055274f052SJens Axboe  *	- Destination page already exists in the address space and there
7065274f052SJens Axboe  *	  are users of it. For that case we have no other option that
7075274f052SJens Axboe  *	  copying the data. Tough luck.
7085274f052SJens Axboe  *	- Destination page already exists in the address space, but there
7095274f052SJens Axboe  *	  are no users of it. Make sure it's uptodate, then drop it. Fall
7105274f052SJens Axboe  *	  through to last case.
7115274f052SJens Axboe  *	- Destination page does not exist, we can add the pipe page to
7125274f052SJens Axboe  *	  the page cache and avoid the copy.
7135274f052SJens Axboe  *
71483f9135bSJens Axboe  * If asked to move pages to the output file (SPLICE_F_MOVE is set in
71583f9135bSJens Axboe  * sd->flags), we attempt to migrate pages from the pipe to the output
71683f9135bSJens Axboe  * file address space page cache. This is possible if no one else has
71783f9135bSJens Axboe  * the pipe page referenced outside of the pipe and page cache. If
71883f9135bSJens Axboe  * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
71983f9135bSJens Axboe  * a new page in the output file page cache and fill/dirty that.
7205274f052SJens Axboe  */
721328eaabaSMiklos Szeredi int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
7225274f052SJens Axboe 		 struct splice_desc *sd)
7235274f052SJens Axboe {
7246a14b90bSJens Axboe 	struct file *file = sd->u.file;
7255274f052SJens Axboe 	struct address_space *mapping = file->f_mapping;
726016b661eSJens Axboe 	unsigned int offset, this_len;
7275274f052SJens Axboe 	struct page *page;
728afddba49SNick Piggin 	void *fsdata;
7293e7ee3e7SJens Axboe 	int ret;
7305274f052SJens Axboe 
7315274f052SJens Axboe 	offset = sd->pos & ~PAGE_CACHE_MASK;
7325274f052SJens Axboe 
733016b661eSJens Axboe 	this_len = sd->len;
734016b661eSJens Axboe 	if (this_len + offset > PAGE_CACHE_SIZE)
735016b661eSJens Axboe 		this_len = PAGE_CACHE_SIZE - offset;
736016b661eSJens Axboe 
737afddba49SNick Piggin 	ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
738afddba49SNick Piggin 				AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
7399e0267c2SJens Axboe 	if (unlikely(ret))
740afddba49SNick Piggin 		goto out;
7415274f052SJens Axboe 
7420568b409SJens Axboe 	if (buf->page != page) {
74376ad4d11SJens Axboe 		char *src = buf->ops->map(pipe, buf, 1);
744e8e3c3d6SCong Wang 		char *dst = kmap_atomic(page);
7455abc97aaSJens Axboe 
746016b661eSJens Axboe 		memcpy(dst + offset, src + buf->offset, this_len);
7475274f052SJens Axboe 		flush_dcache_page(page);
748e8e3c3d6SCong Wang 		kunmap_atomic(dst);
74976ad4d11SJens Axboe 		buf->ops->unmap(pipe, buf, src);
7505abc97aaSJens Axboe 	}
751afddba49SNick Piggin 	ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
752afddba49SNick Piggin 				page, fsdata);
7530568b409SJens Axboe out:
7545274f052SJens Axboe 	return ret;
7555274f052SJens Axboe }
756328eaabaSMiklos Szeredi EXPORT_SYMBOL(pipe_to_file);
7575274f052SJens Axboe 
758b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
759b3c2d2ddSMiklos Szeredi {
760b3c2d2ddSMiklos Szeredi 	smp_mb();
761b3c2d2ddSMiklos Szeredi 	if (waitqueue_active(&pipe->wait))
762b3c2d2ddSMiklos Szeredi 		wake_up_interruptible(&pipe->wait);
763b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
764b3c2d2ddSMiklos Szeredi }
765b3c2d2ddSMiklos Szeredi 
766b3c2d2ddSMiklos Szeredi /**
767b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
768b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
769b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
770b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
771b3c2d2ddSMiklos Szeredi  *
772b3c2d2ddSMiklos Szeredi  * Description:
773b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
774b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
775b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
776b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
777b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
778b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
779b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
780b3c2d2ddSMiklos Szeredi  *
781b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
782b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
783b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
784b3c2d2ddSMiklos Szeredi  *    destination.
785b3c2d2ddSMiklos Szeredi  */
786b3c2d2ddSMiklos Szeredi int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
787b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
788b3c2d2ddSMiklos Szeredi {
789b3c2d2ddSMiklos Szeredi 	int ret;
790b3c2d2ddSMiklos Szeredi 
791b3c2d2ddSMiklos Szeredi 	while (pipe->nrbufs) {
792b3c2d2ddSMiklos Szeredi 		struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
793b3c2d2ddSMiklos Szeredi 		const struct pipe_buf_operations *ops = buf->ops;
794b3c2d2ddSMiklos Szeredi 
795b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
796b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
797b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
798b3c2d2ddSMiklos Szeredi 
799a8adbe37SMichał Mirosław 		ret = buf->ops->confirm(pipe, buf);
800a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
801b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
802b3c2d2ddSMiklos Szeredi 				ret = 0;
803b3c2d2ddSMiklos Szeredi 			return ret;
804b3c2d2ddSMiklos Szeredi 		}
805a8adbe37SMichał Mirosław 
806a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
807a8adbe37SMichał Mirosław 		if (ret <= 0)
808a8adbe37SMichał Mirosław 			return ret;
809a8adbe37SMichał Mirosław 
810b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
811b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
812b3c2d2ddSMiklos Szeredi 
813b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
814b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
815b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
816b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
817b3c2d2ddSMiklos Szeredi 
818b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
819b3c2d2ddSMiklos Szeredi 			buf->ops = NULL;
820b3c2d2ddSMiklos Szeredi 			ops->release(pipe, buf);
82135f3d14dSJens Axboe 			pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
822b3c2d2ddSMiklos Szeredi 			pipe->nrbufs--;
823b3c2d2ddSMiklos Szeredi 			if (pipe->inode)
824b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
825b3c2d2ddSMiklos Szeredi 		}
826b3c2d2ddSMiklos Szeredi 
827b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
828b3c2d2ddSMiklos Szeredi 			return 0;
829b3c2d2ddSMiklos Szeredi 	}
830b3c2d2ddSMiklos Szeredi 
831b3c2d2ddSMiklos Szeredi 	return 1;
832b3c2d2ddSMiklos Szeredi }
833b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_feed);
834b3c2d2ddSMiklos Szeredi 
835b3c2d2ddSMiklos Szeredi /**
836b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
837b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
838b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
839b3c2d2ddSMiklos Szeredi  *
840b3c2d2ddSMiklos Szeredi  * Description:
841b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
842b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
843b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
844b3c2d2ddSMiklos Szeredi  */
845b3c2d2ddSMiklos Szeredi int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
846b3c2d2ddSMiklos Szeredi {
847b3c2d2ddSMiklos Szeredi 	while (!pipe->nrbufs) {
848b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
849b3c2d2ddSMiklos Szeredi 			return 0;
850b3c2d2ddSMiklos Szeredi 
851b3c2d2ddSMiklos Szeredi 		if (!pipe->waiting_writers && sd->num_spliced)
852b3c2d2ddSMiklos Szeredi 			return 0;
853b3c2d2ddSMiklos Szeredi 
854b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
855b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
856b3c2d2ddSMiklos Szeredi 
857b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
858b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
859b3c2d2ddSMiklos Szeredi 
860b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
861b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
862b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
863b3c2d2ddSMiklos Szeredi 		}
864b3c2d2ddSMiklos Szeredi 
865b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
866b3c2d2ddSMiklos Szeredi 	}
867b3c2d2ddSMiklos Szeredi 
868b3c2d2ddSMiklos Szeredi 	return 1;
869b3c2d2ddSMiklos Szeredi }
870b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_next);
871b3c2d2ddSMiklos Szeredi 
872b3c2d2ddSMiklos Szeredi /**
873b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
874b80901bbSRandy Dunlap  * @sd:		information about the splice operation
875b3c2d2ddSMiklos Szeredi  *
876b3c2d2ddSMiklos Szeredi  * Description:
877b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
878b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
879b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
880b3c2d2ddSMiklos Szeredi  */
881b3c2d2ddSMiklos Szeredi void splice_from_pipe_begin(struct splice_desc *sd)
882b3c2d2ddSMiklos Szeredi {
883b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
884b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
885b3c2d2ddSMiklos Szeredi }
886b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_begin);
887b3c2d2ddSMiklos Szeredi 
888b3c2d2ddSMiklos Szeredi /**
889b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
890b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
891b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
892b3c2d2ddSMiklos Szeredi  *
893b3c2d2ddSMiklos Szeredi  * Description:
894b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
895b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
896b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
897b3c2d2ddSMiklos Szeredi  */
898b3c2d2ddSMiklos Szeredi void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
899b3c2d2ddSMiklos Szeredi {
900b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
901b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
902b3c2d2ddSMiklos Szeredi }
903b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_end);
904b3c2d2ddSMiklos Szeredi 
905932cc6d4SJens Axboe /**
906932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
907932cc6d4SJens Axboe  * @pipe:	pipe to splice from
908932cc6d4SJens Axboe  * @sd:		information to @actor
909932cc6d4SJens Axboe  * @actor:	handler that splices the data
910932cc6d4SJens Axboe  *
911932cc6d4SJens Axboe  * Description:
912932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
913932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
914932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
915932cc6d4SJens Axboe  *    pipe_to_user.
916932cc6d4SJens Axboe  *
91783f9135bSJens Axboe  */
918c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
919c66ab6faSJens Axboe 			   splice_actor *actor)
9205274f052SJens Axboe {
921b3c2d2ddSMiklos Szeredi 	int ret;
9225274f052SJens Axboe 
923b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
924b3c2d2ddSMiklos Szeredi 	do {
925b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
926b3c2d2ddSMiklos Szeredi 		if (ret > 0)
927b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
928b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
929b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
9305274f052SJens Axboe 
931b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
9325274f052SJens Axboe }
93340bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
9345274f052SJens Axboe 
935932cc6d4SJens Axboe /**
936932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
937932cc6d4SJens Axboe  * @pipe:	pipe to splice from
938932cc6d4SJens Axboe  * @out:	file to splice to
939932cc6d4SJens Axboe  * @ppos:	position in @out
940932cc6d4SJens Axboe  * @len:	how many bytes to splice
941932cc6d4SJens Axboe  * @flags:	splice modifier flags
942932cc6d4SJens Axboe  * @actor:	handler that splices the data
943932cc6d4SJens Axboe  *
944932cc6d4SJens Axboe  * Description:
9452933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
946932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
947932cc6d4SJens Axboe  *
948932cc6d4SJens Axboe  */
9496da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
9506da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
9516da61809SMark Fasheh 			 splice_actor *actor)
9526da61809SMark Fasheh {
9536da61809SMark Fasheh 	ssize_t ret;
954c66ab6faSJens Axboe 	struct splice_desc sd = {
955c66ab6faSJens Axboe 		.total_len = len,
956c66ab6faSJens Axboe 		.flags = flags,
957c66ab6faSJens Axboe 		.pos = *ppos,
9586a14b90bSJens Axboe 		.u.file = out,
959c66ab6faSJens Axboe 	};
9606da61809SMark Fasheh 
96161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
962c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
96361e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
9646da61809SMark Fasheh 
9656da61809SMark Fasheh 	return ret;
9666da61809SMark Fasheh }
9676da61809SMark Fasheh 
9686da61809SMark Fasheh /**
96983f9135bSJens Axboe  * generic_file_splice_write - splice data from a pipe to a file
9703a326a2cSIngo Molnar  * @pipe:	pipe info
97183f9135bSJens Axboe  * @out:	file to write to
972932cc6d4SJens Axboe  * @ppos:	position in @out
97383f9135bSJens Axboe  * @len:	number of bytes to splice
97483f9135bSJens Axboe  * @flags:	splice modifier flags
97583f9135bSJens Axboe  *
976932cc6d4SJens Axboe  * Description:
97783f9135bSJens Axboe  *    Will either move or copy pages (determined by @flags options) from
97883f9135bSJens Axboe  *    the given pipe inode to the given file.
97983f9135bSJens Axboe  *
98083f9135bSJens Axboe  */
9813a326a2cSIngo Molnar ssize_t
9823a326a2cSIngo Molnar generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
983cbb7e577SJens Axboe 			  loff_t *ppos, size_t len, unsigned int flags)
9845274f052SJens Axboe {
9854f6f0bd2SJens Axboe 	struct address_space *mapping = out->f_mapping;
9868c34e2d6SJens Axboe 	struct inode *inode = mapping->host;
9877f3d4ee1SMiklos Szeredi 	struct splice_desc sd = {
9887f3d4ee1SMiklos Szeredi 		.total_len = len,
9897f3d4ee1SMiklos Szeredi 		.flags = flags,
9907f3d4ee1SMiklos Szeredi 		.pos = *ppos,
9917f3d4ee1SMiklos Szeredi 		.u.file = out,
9927f3d4ee1SMiklos Szeredi 	};
9933a326a2cSIngo Molnar 	ssize_t ret;
9948c34e2d6SJens Axboe 
99561e0d47cSMiklos Szeredi 	pipe_lock(pipe);
996eb443e5aSMiklos Szeredi 
997eb443e5aSMiklos Szeredi 	splice_from_pipe_begin(&sd);
998eb443e5aSMiklos Szeredi 	do {
999eb443e5aSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, &sd);
1000eb443e5aSMiklos Szeredi 		if (ret <= 0)
1001eb443e5aSMiklos Szeredi 			break;
1002eb443e5aSMiklos Szeredi 
1003eb443e5aSMiklos Szeredi 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1004eb443e5aSMiklos Szeredi 		ret = file_remove_suid(out);
1005723590edSMiklos Szeredi 		if (!ret) {
1006723590edSMiklos Szeredi 			file_update_time(out);
1007eb443e5aSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
1008723590edSMiklos Szeredi 		}
1009eb443e5aSMiklos Szeredi 		mutex_unlock(&inode->i_mutex);
1010eb443e5aSMiklos Szeredi 	} while (ret > 0);
1011eb443e5aSMiklos Szeredi 	splice_from_pipe_end(pipe, &sd);
1012eb443e5aSMiklos Szeredi 
101361e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1014eb443e5aSMiklos Szeredi 
1015eb443e5aSMiklos Szeredi 	if (sd.num_spliced)
1016eb443e5aSMiklos Szeredi 		ret = sd.num_spliced;
1017eb443e5aSMiklos Szeredi 
1018a4514ebdSJens Axboe 	if (ret > 0) {
101917ee4f49SJens Axboe 		unsigned long nr_pages;
10207f3d4ee1SMiklos Szeredi 		int err;
10217f3d4ee1SMiklos Szeredi 
1022148f948bSJan Kara 		nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
10234f6f0bd2SJens Axboe 
1024148f948bSJan Kara 		err = generic_write_sync(out, *ppos, ret);
10254f6f0bd2SJens Axboe 		if (err)
10264f6f0bd2SJens Axboe 			ret = err;
1027148f948bSJan Kara 		else
1028148f948bSJan Kara 			*ppos += ret;
102917ee4f49SJens Axboe 		balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
1030a4514ebdSJens Axboe 	}
10314f6f0bd2SJens Axboe 
10324f6f0bd2SJens Axboe 	return ret;
10335274f052SJens Axboe }
10345274f052SJens Axboe 
1035059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_write);
1036059a8f37SJens Axboe 
1037b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1038b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
10390b0a47f5SMiklos Szeredi {
1040b2858d7dSMiklos Szeredi 	int ret;
1041b2858d7dSMiklos Szeredi 	void *data;
1042b2858d7dSMiklos Szeredi 
1043b2858d7dSMiklos Szeredi 	data = buf->ops->map(pipe, buf, 0);
1044b2858d7dSMiklos Szeredi 	ret = kernel_write(sd->u.file, data + buf->offset, sd->len, sd->pos);
1045b2858d7dSMiklos Szeredi 	buf->ops->unmap(pipe, buf, data);
1046b2858d7dSMiklos Szeredi 
1047b2858d7dSMiklos Szeredi 	return ret;
10480b0a47f5SMiklos Szeredi }
10490b0a47f5SMiklos Szeredi 
10500b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
10510b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
10520b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
10530b0a47f5SMiklos Szeredi {
1054b2858d7dSMiklos Szeredi 	ssize_t ret;
10550b0a47f5SMiklos Szeredi 
1056b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1057b2858d7dSMiklos Szeredi 	if (ret > 0)
1058b2858d7dSMiklos Szeredi 		*ppos += ret;
10590b0a47f5SMiklos Szeredi 
1060b2858d7dSMiklos Szeredi 	return ret;
10610b0a47f5SMiklos Szeredi }
10620b0a47f5SMiklos Szeredi 
106383f9135bSJens Axboe /**
106483f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
1065932cc6d4SJens Axboe  * @pipe:	pipe to splice from
106683f9135bSJens Axboe  * @out:	socket to write to
1067932cc6d4SJens Axboe  * @ppos:	position in @out
106883f9135bSJens Axboe  * @len:	number of bytes to splice
106983f9135bSJens Axboe  * @flags:	splice modifier flags
107083f9135bSJens Axboe  *
1071932cc6d4SJens Axboe  * Description:
107283f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
107383f9135bSJens Axboe  *    is involved.
107483f9135bSJens Axboe  *
107583f9135bSJens Axboe  */
10763a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
1077cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
10785274f052SJens Axboe {
107900522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
10805274f052SJens Axboe }
10815274f052SJens Axboe 
1082059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
1083a0f06780SJeff Garzik 
108483f9135bSJens Axboe /*
108583f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
108683f9135bSJens Axboe  */
10873a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1088cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
10895274f052SJens Axboe {
10900b0a47f5SMiklos Szeredi 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
10910b0a47f5SMiklos Szeredi 				loff_t *, size_t, unsigned int);
10925274f052SJens Axboe 	int ret;
10935274f052SJens Axboe 
109449570e9bSJens Axboe 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
10955274f052SJens Axboe 		return -EBADF;
10965274f052SJens Axboe 
1097efc968d4SLinus Torvalds 	if (unlikely(out->f_flags & O_APPEND))
1098efc968d4SLinus Torvalds 		return -EINVAL;
1099efc968d4SLinus Torvalds 
1100cbb7e577SJens Axboe 	ret = rw_verify_area(WRITE, out, ppos, len);
11015274f052SJens Axboe 	if (unlikely(ret < 0))
11025274f052SJens Axboe 		return ret;
11035274f052SJens Axboe 
1104cc56f7deSChangli Gao 	if (out->f_op && out->f_op->splice_write)
11050b0a47f5SMiklos Szeredi 		splice_write = out->f_op->splice_write;
1106cc56f7deSChangli Gao 	else
11070b0a47f5SMiklos Szeredi 		splice_write = default_file_splice_write;
11080b0a47f5SMiklos Szeredi 
11090b0a47f5SMiklos Szeredi 	return splice_write(pipe, out, ppos, len, flags);
11105274f052SJens Axboe }
11115274f052SJens Axboe 
111283f9135bSJens Axboe /*
111383f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
111483f9135bSJens Axboe  */
1115cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
1116cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
1117cbb7e577SJens Axboe 			 unsigned int flags)
11185274f052SJens Axboe {
11196818173bSMiklos Szeredi 	ssize_t (*splice_read)(struct file *, loff_t *,
11206818173bSMiklos Szeredi 			       struct pipe_inode_info *, size_t, unsigned int);
11215274f052SJens Axboe 	int ret;
11225274f052SJens Axboe 
112349570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
11245274f052SJens Axboe 		return -EBADF;
11255274f052SJens Axboe 
1126cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
11275274f052SJens Axboe 	if (unlikely(ret < 0))
11285274f052SJens Axboe 		return ret;
11295274f052SJens Axboe 
1130cc56f7deSChangli Gao 	if (in->f_op && in->f_op->splice_read)
11316818173bSMiklos Szeredi 		splice_read = in->f_op->splice_read;
1132cc56f7deSChangli Gao 	else
11336818173bSMiklos Szeredi 		splice_read = default_file_splice_read;
11346818173bSMiklos Szeredi 
11356818173bSMiklos Szeredi 	return splice_read(in, ppos, pipe, len, flags);
11365274f052SJens Axboe }
11375274f052SJens Axboe 
1138932cc6d4SJens Axboe /**
1139932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
1140932cc6d4SJens Axboe  * @in:		file to splice from
1141932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
1142932cc6d4SJens Axboe  * @actor:	handles the data splicing
1143932cc6d4SJens Axboe  *
1144932cc6d4SJens Axboe  * Description:
1145932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
1146932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
1147932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
1148932cc6d4SJens Axboe  *    that process.
1149932cc6d4SJens Axboe  *
1150c66ab6faSJens Axboe  */
1151c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1152c66ab6faSJens Axboe 			       splice_direct_actor *actor)
1153b92ce558SJens Axboe {
1154b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
1155b92ce558SJens Axboe 	long ret, bytes;
1156b92ce558SJens Axboe 	umode_t i_mode;
1157c66ab6faSJens Axboe 	size_t len;
1158c66ab6faSJens Axboe 	int i, flags;
1159b92ce558SJens Axboe 
1160b92ce558SJens Axboe 	/*
1161b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
1162b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
1163b92ce558SJens Axboe 	 * piped splicing for that!
1164b92ce558SJens Axboe 	 */
11650f7fc9e4SJosef "Jeff" Sipek 	i_mode = in->f_path.dentry->d_inode->i_mode;
1166b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1167b92ce558SJens Axboe 		return -EINVAL;
1168b92ce558SJens Axboe 
1169b92ce558SJens Axboe 	/*
1170b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
1171b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
1172b92ce558SJens Axboe 	 */
1173b92ce558SJens Axboe 	pipe = current->splice_pipe;
117449570e9bSJens Axboe 	if (unlikely(!pipe)) {
1175b92ce558SJens Axboe 		pipe = alloc_pipe_info(NULL);
1176b92ce558SJens Axboe 		if (!pipe)
1177b92ce558SJens Axboe 			return -ENOMEM;
1178b92ce558SJens Axboe 
1179b92ce558SJens Axboe 		/*
1180b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
118100522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
1182b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
1183b92ce558SJens Axboe 		 */
1184b92ce558SJens Axboe 		pipe->readers = 1;
1185b92ce558SJens Axboe 
1186b92ce558SJens Axboe 		current->splice_pipe = pipe;
1187b92ce558SJens Axboe 	}
1188b92ce558SJens Axboe 
1189b92ce558SJens Axboe 	/*
119073d62d83SIngo Molnar 	 * Do the splice.
1191b92ce558SJens Axboe 	 */
1192b92ce558SJens Axboe 	ret = 0;
1193b92ce558SJens Axboe 	bytes = 0;
1194c66ab6faSJens Axboe 	len = sd->total_len;
1195c66ab6faSJens Axboe 	flags = sd->flags;
1196c66ab6faSJens Axboe 
1197c66ab6faSJens Axboe 	/*
1198c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
1199c66ab6faSJens Axboe 	 */
1200c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
1201b92ce558SJens Axboe 
1202b92ce558SJens Axboe 	while (len) {
120351a92c0fSJens Axboe 		size_t read_len;
1204a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
1205b92ce558SJens Axboe 
1206bcd4f3acSJens Axboe 		ret = do_splice_to(in, &pos, pipe, len, flags);
120751a92c0fSJens Axboe 		if (unlikely(ret <= 0))
1208b92ce558SJens Axboe 			goto out_release;
1209b92ce558SJens Axboe 
1210b92ce558SJens Axboe 		read_len = ret;
1211c66ab6faSJens Axboe 		sd->total_len = read_len;
1212b92ce558SJens Axboe 
1213b92ce558SJens Axboe 		/*
1214b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
1215b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
1216b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
1217b92ce558SJens Axboe 		 */
1218c66ab6faSJens Axboe 		ret = actor(pipe, sd);
1219a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
1220a82c53a0STom Zanussi 			sd->pos = prev_pos;
1221b92ce558SJens Axboe 			goto out_release;
1222a82c53a0STom Zanussi 		}
1223b92ce558SJens Axboe 
1224b92ce558SJens Axboe 		bytes += ret;
1225b92ce558SJens Axboe 		len -= ret;
1226bcd4f3acSJens Axboe 		sd->pos = pos;
1227b92ce558SJens Axboe 
1228a82c53a0STom Zanussi 		if (ret < read_len) {
1229a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
123051a92c0fSJens Axboe 			goto out_release;
1231b92ce558SJens Axboe 		}
1232a82c53a0STom Zanussi 	}
1233b92ce558SJens Axboe 
12349e97198dSJens Axboe done:
1235b92ce558SJens Axboe 	pipe->nrbufs = pipe->curbuf = 0;
12369e97198dSJens Axboe 	file_accessed(in);
1237b92ce558SJens Axboe 	return bytes;
1238b92ce558SJens Axboe 
1239b92ce558SJens Axboe out_release:
1240b92ce558SJens Axboe 	/*
1241b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1242b92ce558SJens Axboe 	 * the pipe buffers in question:
1243b92ce558SJens Axboe 	 */
124435f3d14dSJens Axboe 	for (i = 0; i < pipe->buffers; i++) {
1245b92ce558SJens Axboe 		struct pipe_buffer *buf = pipe->bufs + i;
1246b92ce558SJens Axboe 
1247b92ce558SJens Axboe 		if (buf->ops) {
1248b92ce558SJens Axboe 			buf->ops->release(pipe, buf);
1249b92ce558SJens Axboe 			buf->ops = NULL;
1250b92ce558SJens Axboe 		}
1251b92ce558SJens Axboe 	}
1252b92ce558SJens Axboe 
12539e97198dSJens Axboe 	if (!bytes)
12549e97198dSJens Axboe 		bytes = ret;
1255b92ce558SJens Axboe 
12569e97198dSJens Axboe 	goto done;
1257c66ab6faSJens Axboe }
1258c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1259c66ab6faSJens Axboe 
1260c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1261c66ab6faSJens Axboe 			       struct splice_desc *sd)
1262c66ab6faSJens Axboe {
12636a14b90bSJens Axboe 	struct file *file = sd->u.file;
1264c66ab6faSJens Axboe 
12652cb4b05eSChangli Gao 	return do_splice_from(pipe, file, &file->f_pos, sd->total_len,
12662cb4b05eSChangli Gao 			      sd->flags);
1267c66ab6faSJens Axboe }
1268c66ab6faSJens Axboe 
1269932cc6d4SJens Axboe /**
1270932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1271932cc6d4SJens Axboe  * @in:		file to splice from
1272932cc6d4SJens Axboe  * @ppos:	input file offset
1273932cc6d4SJens Axboe  * @out:	file to splice to
1274932cc6d4SJens Axboe  * @len:	number of bytes to splice
1275932cc6d4SJens Axboe  * @flags:	splice modifier flags
1276932cc6d4SJens Axboe  *
1277932cc6d4SJens Axboe  * Description:
1278932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1279932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1280932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1281932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1282932cc6d4SJens Axboe  *
1283932cc6d4SJens Axboe  */
1284c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1285c66ab6faSJens Axboe 		      size_t len, unsigned int flags)
1286c66ab6faSJens Axboe {
1287c66ab6faSJens Axboe 	struct splice_desc sd = {
1288c66ab6faSJens Axboe 		.len		= len,
1289c66ab6faSJens Axboe 		.total_len	= len,
1290c66ab6faSJens Axboe 		.flags		= flags,
1291c66ab6faSJens Axboe 		.pos		= *ppos,
12926a14b90bSJens Axboe 		.u.file		= out,
1293c66ab6faSJens Axboe 	};
129451a92c0fSJens Axboe 	long ret;
1295c66ab6faSJens Axboe 
1296c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
129751a92c0fSJens Axboe 	if (ret > 0)
1298a82c53a0STom Zanussi 		*ppos = sd.pos;
129951a92c0fSJens Axboe 
1300c66ab6faSJens Axboe 	return ret;
1301b92ce558SJens Axboe }
1302b92ce558SJens Axboe 
13037c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
13047c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
13057c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1306ddac0d39SJens Axboe 
1307ddac0d39SJens Axboe /*
130883f9135bSJens Axboe  * Determine where to splice to/from.
130983f9135bSJens Axboe  */
1310529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in,
1311529565dcSIngo Molnar 		      struct file *out, loff_t __user *off_out,
1312529565dcSIngo Molnar 		      size_t len, unsigned int flags)
13135274f052SJens Axboe {
13147c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
13157c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
1316cbb7e577SJens Axboe 	loff_t offset, *off;
1317a4514ebdSJens Axboe 	long ret;
13185274f052SJens Axboe 
131971993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
132071993e62SLinus Torvalds 	opipe = get_pipe_info(out);
13217c77f0b3SMiklos Szeredi 
13227c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
13237c77f0b3SMiklos Szeredi 		if (off_in || off_out)
13247c77f0b3SMiklos Szeredi 			return -ESPIPE;
13257c77f0b3SMiklos Szeredi 
13267c77f0b3SMiklos Szeredi 		if (!(in->f_mode & FMODE_READ))
13277c77f0b3SMiklos Szeredi 			return -EBADF;
13287c77f0b3SMiklos Szeredi 
13297c77f0b3SMiklos Szeredi 		if (!(out->f_mode & FMODE_WRITE))
13307c77f0b3SMiklos Szeredi 			return -EBADF;
13317c77f0b3SMiklos Szeredi 
13327c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
13337c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
13347c77f0b3SMiklos Szeredi 			return -EINVAL;
13357c77f0b3SMiklos Szeredi 
13367c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
13377c77f0b3SMiklos Szeredi 	}
13387c77f0b3SMiklos Szeredi 
13397c77f0b3SMiklos Szeredi 	if (ipipe) {
1340529565dcSIngo Molnar 		if (off_in)
1341529565dcSIngo Molnar 			return -ESPIPE;
1342b92ce558SJens Axboe 		if (off_out) {
134319c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1344b92ce558SJens Axboe 				return -EINVAL;
1345cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1346b92ce558SJens Axboe 				return -EFAULT;
1347cbb7e577SJens Axboe 			off = &offset;
1348cbb7e577SJens Axboe 		} else
1349cbb7e577SJens Axboe 			off = &out->f_pos;
1350529565dcSIngo Molnar 
13517c77f0b3SMiklos Szeredi 		ret = do_splice_from(ipipe, out, off, len, flags);
1352a4514ebdSJens Axboe 
1353a4514ebdSJens Axboe 		if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1354a4514ebdSJens Axboe 			ret = -EFAULT;
1355a4514ebdSJens Axboe 
1356a4514ebdSJens Axboe 		return ret;
1357529565dcSIngo Molnar 	}
13585274f052SJens Axboe 
13597c77f0b3SMiklos Szeredi 	if (opipe) {
1360529565dcSIngo Molnar 		if (off_out)
1361529565dcSIngo Molnar 			return -ESPIPE;
1362b92ce558SJens Axboe 		if (off_in) {
136319c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1364b92ce558SJens Axboe 				return -EINVAL;
1365cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1366b92ce558SJens Axboe 				return -EFAULT;
1367cbb7e577SJens Axboe 			off = &offset;
1368cbb7e577SJens Axboe 		} else
1369cbb7e577SJens Axboe 			off = &in->f_pos;
1370529565dcSIngo Molnar 
13717c77f0b3SMiklos Szeredi 		ret = do_splice_to(in, off, opipe, len, flags);
1372a4514ebdSJens Axboe 
1373a4514ebdSJens Axboe 		if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1374a4514ebdSJens Axboe 			ret = -EFAULT;
1375a4514ebdSJens Axboe 
1376a4514ebdSJens Axboe 		return ret;
1377529565dcSIngo Molnar 	}
13785274f052SJens Axboe 
13795274f052SJens Axboe 	return -EINVAL;
13805274f052SJens Axboe }
13815274f052SJens Axboe 
1382912d35f8SJens Axboe /*
1383912d35f8SJens Axboe  * Map an iov into an array of pages and offset/length tupples. With the
1384912d35f8SJens Axboe  * partial_page structure, we can map several non-contiguous ranges into
1385912d35f8SJens Axboe  * our ones pages[] map instead of splitting that operation into pieces.
1386912d35f8SJens Axboe  * Could easily be exported as a generic helper for other users, in which
1387912d35f8SJens Axboe  * case one would probably want to add a 'max_nr_pages' parameter as well.
1388912d35f8SJens Axboe  */
1389912d35f8SJens Axboe static int get_iovec_page_array(const struct iovec __user *iov,
1390912d35f8SJens Axboe 				unsigned int nr_vecs, struct page **pages,
139135f3d14dSJens Axboe 				struct partial_page *partial, int aligned,
139235f3d14dSJens Axboe 				unsigned int pipe_buffers)
1393912d35f8SJens Axboe {
1394912d35f8SJens Axboe 	int buffers = 0, error = 0;
1395912d35f8SJens Axboe 
1396912d35f8SJens Axboe 	while (nr_vecs) {
1397912d35f8SJens Axboe 		unsigned long off, npages;
139875723957SLinus Torvalds 		struct iovec entry;
1399912d35f8SJens Axboe 		void __user *base;
1400912d35f8SJens Axboe 		size_t len;
1401912d35f8SJens Axboe 		int i;
1402912d35f8SJens Axboe 
140375723957SLinus Torvalds 		error = -EFAULT;
1404bc40d73cSNick Piggin 		if (copy_from_user(&entry, iov, sizeof(entry)))
1405912d35f8SJens Axboe 			break;
140675723957SLinus Torvalds 
140775723957SLinus Torvalds 		base = entry.iov_base;
140875723957SLinus Torvalds 		len = entry.iov_len;
1409912d35f8SJens Axboe 
1410912d35f8SJens Axboe 		/*
1411912d35f8SJens Axboe 		 * Sanity check this iovec. 0 read succeeds.
1412912d35f8SJens Axboe 		 */
141375723957SLinus Torvalds 		error = 0;
1414912d35f8SJens Axboe 		if (unlikely(!len))
1415912d35f8SJens Axboe 			break;
1416912d35f8SJens Axboe 		error = -EFAULT;
1417712a30e6SBastian Blank 		if (!access_ok(VERIFY_READ, base, len))
1418912d35f8SJens Axboe 			break;
1419912d35f8SJens Axboe 
1420912d35f8SJens Axboe 		/*
1421912d35f8SJens Axboe 		 * Get this base offset and number of pages, then map
1422912d35f8SJens Axboe 		 * in the user pages.
1423912d35f8SJens Axboe 		 */
1424912d35f8SJens Axboe 		off = (unsigned long) base & ~PAGE_MASK;
14257afa6fd0SJens Axboe 
14267afa6fd0SJens Axboe 		/*
14277afa6fd0SJens Axboe 		 * If asked for alignment, the offset must be zero and the
14287afa6fd0SJens Axboe 		 * length a multiple of the PAGE_SIZE.
14297afa6fd0SJens Axboe 		 */
14307afa6fd0SJens Axboe 		error = -EINVAL;
14317afa6fd0SJens Axboe 		if (aligned && (off || len & ~PAGE_MASK))
14327afa6fd0SJens Axboe 			break;
14337afa6fd0SJens Axboe 
1434912d35f8SJens Axboe 		npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
143535f3d14dSJens Axboe 		if (npages > pipe_buffers - buffers)
143635f3d14dSJens Axboe 			npages = pipe_buffers - buffers;
1437912d35f8SJens Axboe 
1438bc40d73cSNick Piggin 		error = get_user_pages_fast((unsigned long)base, npages,
1439bc40d73cSNick Piggin 					0, &pages[buffers]);
1440912d35f8SJens Axboe 
1441912d35f8SJens Axboe 		if (unlikely(error <= 0))
1442912d35f8SJens Axboe 			break;
1443912d35f8SJens Axboe 
1444912d35f8SJens Axboe 		/*
1445912d35f8SJens Axboe 		 * Fill this contiguous range into the partial page map.
1446912d35f8SJens Axboe 		 */
1447912d35f8SJens Axboe 		for (i = 0; i < error; i++) {
14487591489aSJens Axboe 			const int plen = min_t(size_t, len, PAGE_SIZE - off);
1449912d35f8SJens Axboe 
1450912d35f8SJens Axboe 			partial[buffers].offset = off;
1451912d35f8SJens Axboe 			partial[buffers].len = plen;
1452912d35f8SJens Axboe 
1453912d35f8SJens Axboe 			off = 0;
1454912d35f8SJens Axboe 			len -= plen;
1455912d35f8SJens Axboe 			buffers++;
1456912d35f8SJens Axboe 		}
1457912d35f8SJens Axboe 
1458912d35f8SJens Axboe 		/*
1459912d35f8SJens Axboe 		 * We didn't complete this iov, stop here since it probably
1460912d35f8SJens Axboe 		 * means we have to move some of this into a pipe to
1461912d35f8SJens Axboe 		 * be able to continue.
1462912d35f8SJens Axboe 		 */
1463912d35f8SJens Axboe 		if (len)
1464912d35f8SJens Axboe 			break;
1465912d35f8SJens Axboe 
1466912d35f8SJens Axboe 		/*
1467912d35f8SJens Axboe 		 * Don't continue if we mapped fewer pages than we asked for,
1468912d35f8SJens Axboe 		 * or if we mapped the max number of pages that we have
1469912d35f8SJens Axboe 		 * room for.
1470912d35f8SJens Axboe 		 */
147135f3d14dSJens Axboe 		if (error < npages || buffers == pipe_buffers)
1472912d35f8SJens Axboe 			break;
1473912d35f8SJens Axboe 
1474912d35f8SJens Axboe 		nr_vecs--;
1475912d35f8SJens Axboe 		iov++;
1476912d35f8SJens Axboe 	}
1477912d35f8SJens Axboe 
1478912d35f8SJens Axboe 	if (buffers)
1479912d35f8SJens Axboe 		return buffers;
1480912d35f8SJens Axboe 
1481912d35f8SJens Axboe 	return error;
1482912d35f8SJens Axboe }
1483912d35f8SJens Axboe 
14846a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
14856a14b90bSJens Axboe 			struct splice_desc *sd)
14866a14b90bSJens Axboe {
14876a14b90bSJens Axboe 	char *src;
14886a14b90bSJens Axboe 	int ret;
14896a14b90bSJens Axboe 
14906a14b90bSJens Axboe 	/*
14916a14b90bSJens Axboe 	 * See if we can use the atomic maps, by prefaulting in the
14926a14b90bSJens Axboe 	 * pages and doing an atomic copy
14936a14b90bSJens Axboe 	 */
14946a14b90bSJens Axboe 	if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
14956a14b90bSJens Axboe 		src = buf->ops->map(pipe, buf, 1);
14966a14b90bSJens Axboe 		ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
14976a14b90bSJens Axboe 							sd->len);
14986a14b90bSJens Axboe 		buf->ops->unmap(pipe, buf, src);
14996a14b90bSJens Axboe 		if (!ret) {
15006a14b90bSJens Axboe 			ret = sd->len;
15016a14b90bSJens Axboe 			goto out;
15026a14b90bSJens Axboe 		}
15036a14b90bSJens Axboe 	}
15046a14b90bSJens Axboe 
15056a14b90bSJens Axboe 	/*
15066a14b90bSJens Axboe 	 * No dice, use slow non-atomic map and copy
15076a14b90bSJens Axboe  	 */
15086a14b90bSJens Axboe 	src = buf->ops->map(pipe, buf, 0);
15096a14b90bSJens Axboe 
15106a14b90bSJens Axboe 	ret = sd->len;
15116a14b90bSJens Axboe 	if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
15126a14b90bSJens Axboe 		ret = -EFAULT;
15136a14b90bSJens Axboe 
15146866bef4SJens Axboe 	buf->ops->unmap(pipe, buf, src);
15156a14b90bSJens Axboe out:
15166a14b90bSJens Axboe 	if (ret > 0)
15176a14b90bSJens Axboe 		sd->u.userptr += ret;
15186a14b90bSJens Axboe 	return ret;
15196a14b90bSJens Axboe }
15206a14b90bSJens Axboe 
15216a14b90bSJens Axboe /*
15226a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
15236a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
15246a14b90bSJens Axboe  */
15256a14b90bSJens Axboe static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
15266a14b90bSJens Axboe 			     unsigned long nr_segs, unsigned int flags)
15276a14b90bSJens Axboe {
15286a14b90bSJens Axboe 	struct pipe_inode_info *pipe;
15296a14b90bSJens Axboe 	struct splice_desc sd;
15306a14b90bSJens Axboe 	ssize_t size;
15316a14b90bSJens Axboe 	int error;
15326a14b90bSJens Axboe 	long ret;
15336a14b90bSJens Axboe 
153471993e62SLinus Torvalds 	pipe = get_pipe_info(file);
15356a14b90bSJens Axboe 	if (!pipe)
15366a14b90bSJens Axboe 		return -EBADF;
15376a14b90bSJens Axboe 
153861e0d47cSMiklos Szeredi 	pipe_lock(pipe);
15396a14b90bSJens Axboe 
15406a14b90bSJens Axboe 	error = ret = 0;
15416a14b90bSJens Axboe 	while (nr_segs) {
15426a14b90bSJens Axboe 		void __user *base;
15436a14b90bSJens Axboe 		size_t len;
15446a14b90bSJens Axboe 
15456a14b90bSJens Axboe 		/*
15466a14b90bSJens Axboe 		 * Get user address base and length for this iovec.
15476a14b90bSJens Axboe 		 */
15486a14b90bSJens Axboe 		error = get_user(base, &iov->iov_base);
15496a14b90bSJens Axboe 		if (unlikely(error))
15506a14b90bSJens Axboe 			break;
15516a14b90bSJens Axboe 		error = get_user(len, &iov->iov_len);
15526a14b90bSJens Axboe 		if (unlikely(error))
15536a14b90bSJens Axboe 			break;
15546a14b90bSJens Axboe 
15556a14b90bSJens Axboe 		/*
15566a14b90bSJens Axboe 		 * Sanity check this iovec. 0 read succeeds.
15576a14b90bSJens Axboe 		 */
15586a14b90bSJens Axboe 		if (unlikely(!len))
15596a14b90bSJens Axboe 			break;
15606a14b90bSJens Axboe 		if (unlikely(!base)) {
15616a14b90bSJens Axboe 			error = -EFAULT;
15626a14b90bSJens Axboe 			break;
15636a14b90bSJens Axboe 		}
15646a14b90bSJens Axboe 
15658811930dSJens Axboe 		if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
15668811930dSJens Axboe 			error = -EFAULT;
15678811930dSJens Axboe 			break;
15688811930dSJens Axboe 		}
15698811930dSJens Axboe 
15706a14b90bSJens Axboe 		sd.len = 0;
15716a14b90bSJens Axboe 		sd.total_len = len;
15726a14b90bSJens Axboe 		sd.flags = flags;
15736a14b90bSJens Axboe 		sd.u.userptr = base;
15746a14b90bSJens Axboe 		sd.pos = 0;
15756a14b90bSJens Axboe 
15766a14b90bSJens Axboe 		size = __splice_from_pipe(pipe, &sd, pipe_to_user);
15776a14b90bSJens Axboe 		if (size < 0) {
15786a14b90bSJens Axboe 			if (!ret)
15796a14b90bSJens Axboe 				ret = size;
15806a14b90bSJens Axboe 
15816a14b90bSJens Axboe 			break;
15826a14b90bSJens Axboe 		}
15836a14b90bSJens Axboe 
15846a14b90bSJens Axboe 		ret += size;
15856a14b90bSJens Axboe 
15866a14b90bSJens Axboe 		if (size < len)
15876a14b90bSJens Axboe 			break;
15886a14b90bSJens Axboe 
15896a14b90bSJens Axboe 		nr_segs--;
15906a14b90bSJens Axboe 		iov++;
15916a14b90bSJens Axboe 	}
15926a14b90bSJens Axboe 
159361e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
15946a14b90bSJens Axboe 
15956a14b90bSJens Axboe 	if (!ret)
15966a14b90bSJens Axboe 		ret = error;
15976a14b90bSJens Axboe 
15986a14b90bSJens Axboe 	return ret;
15996a14b90bSJens Axboe }
16006a14b90bSJens Axboe 
1601912d35f8SJens Axboe /*
1602912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1603912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1604912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1605912d35f8SJens Axboe  */
16066a14b90bSJens Axboe static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1607912d35f8SJens Axboe 			     unsigned long nr_segs, unsigned int flags)
1608912d35f8SJens Axboe {
1609ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
161035f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
161135f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
1612912d35f8SJens Axboe 	struct splice_pipe_desc spd = {
1613912d35f8SJens Axboe 		.pages = pages,
1614912d35f8SJens Axboe 		.partial = partial,
1615912d35f8SJens Axboe 		.flags = flags,
1616912d35f8SJens Axboe 		.ops = &user_page_pipe_buf_ops,
1617bbdfc2f7SJens Axboe 		.spd_release = spd_release_page,
1618912d35f8SJens Axboe 	};
161935f3d14dSJens Axboe 	long ret;
1620912d35f8SJens Axboe 
162171993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1622ddac0d39SJens Axboe 	if (!pipe)
1623912d35f8SJens Axboe 		return -EBADF;
1624912d35f8SJens Axboe 
162535f3d14dSJens Axboe 	if (splice_grow_spd(pipe, &spd))
162635f3d14dSJens Axboe 		return -ENOMEM;
1627912d35f8SJens Axboe 
162835f3d14dSJens Axboe 	spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
162935f3d14dSJens Axboe 					    spd.partial, flags & SPLICE_F_GIFT,
163035f3d14dSJens Axboe 					    pipe->buffers);
163135f3d14dSJens Axboe 	if (spd.nr_pages <= 0)
163235f3d14dSJens Axboe 		ret = spd.nr_pages;
163335f3d14dSJens Axboe 	else
163435f3d14dSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
163535f3d14dSJens Axboe 
163635f3d14dSJens Axboe 	splice_shrink_spd(pipe, &spd);
163735f3d14dSJens Axboe 	return ret;
1638912d35f8SJens Axboe }
1639912d35f8SJens Axboe 
16406a14b90bSJens Axboe /*
16416a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
16426a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
16436a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
16446a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
16456a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
16466a14b90bSJens Axboe  * solutions for that:
16476a14b90bSJens Axboe  *
16486a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
16496a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
16506a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
16516a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
16526a14b90bSJens Axboe  *
16536a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
16546a14b90bSJens Axboe  *
16556a14b90bSJens Axboe  */
1656836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1657836f92adSHeiko Carstens 		unsigned long, nr_segs, unsigned int, flags)
1658912d35f8SJens Axboe {
1659912d35f8SJens Axboe 	struct file *file;
1660912d35f8SJens Axboe 	long error;
1661912d35f8SJens Axboe 	int fput;
1662912d35f8SJens Axboe 
16636a14b90bSJens Axboe 	if (unlikely(nr_segs > UIO_MAXIOV))
16646a14b90bSJens Axboe 		return -EINVAL;
16656a14b90bSJens Axboe 	else if (unlikely(!nr_segs))
16666a14b90bSJens Axboe 		return 0;
16676a14b90bSJens Axboe 
1668912d35f8SJens Axboe 	error = -EBADF;
1669912d35f8SJens Axboe 	file = fget_light(fd, &fput);
1670912d35f8SJens Axboe 	if (file) {
1671912d35f8SJens Axboe 		if (file->f_mode & FMODE_WRITE)
16726a14b90bSJens Axboe 			error = vmsplice_to_pipe(file, iov, nr_segs, flags);
16736a14b90bSJens Axboe 		else if (file->f_mode & FMODE_READ)
16746a14b90bSJens Axboe 			error = vmsplice_to_user(file, iov, nr_segs, flags);
1675912d35f8SJens Axboe 
1676912d35f8SJens Axboe 		fput_light(file, fput);
1677912d35f8SJens Axboe 	}
1678912d35f8SJens Axboe 
1679912d35f8SJens Axboe 	return error;
1680912d35f8SJens Axboe }
1681912d35f8SJens Axboe 
1682836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1683836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1684836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
16855274f052SJens Axboe {
16865274f052SJens Axboe 	long error;
16875274f052SJens Axboe 	struct file *in, *out;
16885274f052SJens Axboe 	int fput_in, fput_out;
16895274f052SJens Axboe 
16905274f052SJens Axboe 	if (unlikely(!len))
16915274f052SJens Axboe 		return 0;
16925274f052SJens Axboe 
16935274f052SJens Axboe 	error = -EBADF;
1694529565dcSIngo Molnar 	in = fget_light(fd_in, &fput_in);
16955274f052SJens Axboe 	if (in) {
16965274f052SJens Axboe 		if (in->f_mode & FMODE_READ) {
1697529565dcSIngo Molnar 			out = fget_light(fd_out, &fput_out);
16985274f052SJens Axboe 			if (out) {
16995274f052SJens Axboe 				if (out->f_mode & FMODE_WRITE)
1700529565dcSIngo Molnar 					error = do_splice(in, off_in,
1701529565dcSIngo Molnar 							  out, off_out,
1702529565dcSIngo Molnar 							  len, flags);
17035274f052SJens Axboe 				fput_light(out, fput_out);
17045274f052SJens Axboe 			}
17055274f052SJens Axboe 		}
17065274f052SJens Axboe 
17075274f052SJens Axboe 		fput_light(in, fput_in);
17085274f052SJens Axboe 	}
17095274f052SJens Axboe 
17105274f052SJens Axboe 	return error;
17115274f052SJens Axboe }
171270524490SJens Axboe 
171370524490SJens Axboe /*
1714aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1715aadd06e5SJens Axboe  * return an appropriate error.
1716aadd06e5SJens Axboe  */
17177c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1718aadd06e5SJens Axboe {
1719aadd06e5SJens Axboe 	int ret;
1720aadd06e5SJens Axboe 
1721aadd06e5SJens Axboe 	/*
1722aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1723aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1724aadd06e5SJens Axboe 	 */
1725aadd06e5SJens Axboe 	if (pipe->nrbufs)
1726aadd06e5SJens Axboe 		return 0;
1727aadd06e5SJens Axboe 
1728aadd06e5SJens Axboe 	ret = 0;
172961e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1730aadd06e5SJens Axboe 
1731aadd06e5SJens Axboe 	while (!pipe->nrbufs) {
1732aadd06e5SJens Axboe 		if (signal_pending(current)) {
1733aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1734aadd06e5SJens Axboe 			break;
1735aadd06e5SJens Axboe 		}
1736aadd06e5SJens Axboe 		if (!pipe->writers)
1737aadd06e5SJens Axboe 			break;
1738aadd06e5SJens Axboe 		if (!pipe->waiting_writers) {
1739aadd06e5SJens Axboe 			if (flags & SPLICE_F_NONBLOCK) {
1740aadd06e5SJens Axboe 				ret = -EAGAIN;
1741aadd06e5SJens Axboe 				break;
1742aadd06e5SJens Axboe 			}
1743aadd06e5SJens Axboe 		}
1744aadd06e5SJens Axboe 		pipe_wait(pipe);
1745aadd06e5SJens Axboe 	}
1746aadd06e5SJens Axboe 
174761e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1748aadd06e5SJens Axboe 	return ret;
1749aadd06e5SJens Axboe }
1750aadd06e5SJens Axboe 
1751aadd06e5SJens Axboe /*
1752aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1753aadd06e5SJens Axboe  * return an appropriate error.
1754aadd06e5SJens Axboe  */
17557c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1756aadd06e5SJens Axboe {
1757aadd06e5SJens Axboe 	int ret;
1758aadd06e5SJens Axboe 
1759aadd06e5SJens Axboe 	/*
1760aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1761aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1762aadd06e5SJens Axboe 	 */
176335f3d14dSJens Axboe 	if (pipe->nrbufs < pipe->buffers)
1764aadd06e5SJens Axboe 		return 0;
1765aadd06e5SJens Axboe 
1766aadd06e5SJens Axboe 	ret = 0;
176761e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1768aadd06e5SJens Axboe 
176935f3d14dSJens Axboe 	while (pipe->nrbufs >= pipe->buffers) {
1770aadd06e5SJens Axboe 		if (!pipe->readers) {
1771aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1772aadd06e5SJens Axboe 			ret = -EPIPE;
1773aadd06e5SJens Axboe 			break;
1774aadd06e5SJens Axboe 		}
1775aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1776aadd06e5SJens Axboe 			ret = -EAGAIN;
1777aadd06e5SJens Axboe 			break;
1778aadd06e5SJens Axboe 		}
1779aadd06e5SJens Axboe 		if (signal_pending(current)) {
1780aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1781aadd06e5SJens Axboe 			break;
1782aadd06e5SJens Axboe 		}
1783aadd06e5SJens Axboe 		pipe->waiting_writers++;
1784aadd06e5SJens Axboe 		pipe_wait(pipe);
1785aadd06e5SJens Axboe 		pipe->waiting_writers--;
1786aadd06e5SJens Axboe 	}
1787aadd06e5SJens Axboe 
178861e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1789aadd06e5SJens Axboe 	return ret;
1790aadd06e5SJens Axboe }
1791aadd06e5SJens Axboe 
1792aadd06e5SJens Axboe /*
17937c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
17947c77f0b3SMiklos Szeredi  */
17957c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
17967c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
17977c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
17987c77f0b3SMiklos Szeredi {
17997c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
18007c77f0b3SMiklos Szeredi 	int ret = 0, nbuf;
18017c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
18027c77f0b3SMiklos Szeredi 
18037c77f0b3SMiklos Szeredi 
18047c77f0b3SMiklos Szeredi retry:
18057c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
18067c77f0b3SMiklos Szeredi 	if (ret)
18077c77f0b3SMiklos Szeredi 		return ret;
18087c77f0b3SMiklos Szeredi 
18097c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
18107c77f0b3SMiklos Szeredi 	if (ret)
18117c77f0b3SMiklos Szeredi 		return ret;
18127c77f0b3SMiklos Szeredi 
18137c77f0b3SMiklos Szeredi 	/*
18147c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
18157c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
18167c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
18177c77f0b3SMiklos Szeredi 	 */
18187c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
18197c77f0b3SMiklos Szeredi 
18207c77f0b3SMiklos Szeredi 	do {
18217c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
18227c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
18237c77f0b3SMiklos Szeredi 			if (!ret)
18247c77f0b3SMiklos Szeredi 				ret = -EPIPE;
18257c77f0b3SMiklos Szeredi 			break;
18267c77f0b3SMiklos Szeredi 		}
18277c77f0b3SMiklos Szeredi 
18287c77f0b3SMiklos Szeredi 		if (!ipipe->nrbufs && !ipipe->writers)
18297c77f0b3SMiklos Szeredi 			break;
18307c77f0b3SMiklos Szeredi 
18317c77f0b3SMiklos Szeredi 		/*
18327c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
18337c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
18347c77f0b3SMiklos Szeredi 		 */
183535f3d14dSJens Axboe 		if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
18367c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
18377c77f0b3SMiklos Szeredi 			if (ret)
18387c77f0b3SMiklos Szeredi 				break;
18397c77f0b3SMiklos Szeredi 
18407c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
18417c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
18427c77f0b3SMiklos Szeredi 				break;
18437c77f0b3SMiklos Szeredi 			}
18447c77f0b3SMiklos Szeredi 
18457c77f0b3SMiklos Szeredi 			/*
18467c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
18477c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
18487c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
18497c77f0b3SMiklos Szeredi 			 */
18507c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
18517c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
18527c77f0b3SMiklos Szeredi 			goto retry;
18537c77f0b3SMiklos Szeredi 		}
18547c77f0b3SMiklos Szeredi 
18557c77f0b3SMiklos Szeredi 		ibuf = ipipe->bufs + ipipe->curbuf;
185635f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
18577c77f0b3SMiklos Szeredi 		obuf = opipe->bufs + nbuf;
18587c77f0b3SMiklos Szeredi 
18597c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
18607c77f0b3SMiklos Szeredi 			/*
18617c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
18627c77f0b3SMiklos Szeredi 			 */
18637c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
18647c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
18657c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
186635f3d14dSJens Axboe 			ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
18677c77f0b3SMiklos Szeredi 			ipipe->nrbufs--;
18687c77f0b3SMiklos Szeredi 			input_wakeup = true;
18697c77f0b3SMiklos Szeredi 		} else {
18707c77f0b3SMiklos Szeredi 			/*
18717c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
18727c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
18737c77f0b3SMiklos Szeredi 			 */
18747c77f0b3SMiklos Szeredi 			ibuf->ops->get(ipipe, ibuf);
18757c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
18767c77f0b3SMiklos Szeredi 
18777c77f0b3SMiklos Szeredi 			/*
18787c77f0b3SMiklos Szeredi 			 * Don't inherit the gift flag, we need to
18797c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
18807c77f0b3SMiklos Szeredi 			 */
18817c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
18827c77f0b3SMiklos Szeredi 
18837c77f0b3SMiklos Szeredi 			obuf->len = len;
18847c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
18857c77f0b3SMiklos Szeredi 			ibuf->offset += obuf->len;
18867c77f0b3SMiklos Szeredi 			ibuf->len -= obuf->len;
18877c77f0b3SMiklos Szeredi 		}
18887c77f0b3SMiklos Szeredi 		ret += obuf->len;
18897c77f0b3SMiklos Szeredi 		len -= obuf->len;
18907c77f0b3SMiklos Szeredi 	} while (len);
18917c77f0b3SMiklos Szeredi 
18927c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
18937c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
18947c77f0b3SMiklos Szeredi 
18957c77f0b3SMiklos Szeredi 	/*
18967c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
18977c77f0b3SMiklos Szeredi 	 */
1898825cdcb1SNamhyung Kim 	if (ret > 0)
1899825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1900825cdcb1SNamhyung Kim 
19017c77f0b3SMiklos Szeredi 	if (input_wakeup)
19027c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
19037c77f0b3SMiklos Szeredi 
19047c77f0b3SMiklos Szeredi 	return ret;
19057c77f0b3SMiklos Szeredi }
19067c77f0b3SMiklos Szeredi 
19077c77f0b3SMiklos Szeredi /*
190870524490SJens Axboe  * Link contents of ipipe to opipe.
190970524490SJens Axboe  */
191070524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
191170524490SJens Axboe 		     struct pipe_inode_info *opipe,
191270524490SJens Axboe 		     size_t len, unsigned int flags)
191370524490SJens Axboe {
191470524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
1915aadd06e5SJens Axboe 	int ret = 0, i = 0, nbuf;
191670524490SJens Axboe 
191770524490SJens Axboe 	/*
191870524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
191961e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
192070524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
192170524490SJens Axboe 	 */
192261e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
192370524490SJens Axboe 
1924aadd06e5SJens Axboe 	do {
192570524490SJens Axboe 		if (!opipe->readers) {
192670524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
192770524490SJens Axboe 			if (!ret)
192870524490SJens Axboe 				ret = -EPIPE;
192970524490SJens Axboe 			break;
193070524490SJens Axboe 		}
193170524490SJens Axboe 
193270524490SJens Axboe 		/*
1933aadd06e5SJens Axboe 		 * If we have iterated all input buffers or ran out of
1934aadd06e5SJens Axboe 		 * output room, break.
193570524490SJens Axboe 		 */
193635f3d14dSJens Axboe 		if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1937aadd06e5SJens Axboe 			break;
1938aadd06e5SJens Axboe 
193935f3d14dSJens Axboe 		ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
194035f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
194170524490SJens Axboe 
194270524490SJens Axboe 		/*
194370524490SJens Axboe 		 * Get a reference to this pipe buffer,
194470524490SJens Axboe 		 * so we can copy the contents over.
194570524490SJens Axboe 		 */
194670524490SJens Axboe 		ibuf->ops->get(ipipe, ibuf);
194770524490SJens Axboe 
194870524490SJens Axboe 		obuf = opipe->bufs + nbuf;
194970524490SJens Axboe 		*obuf = *ibuf;
195070524490SJens Axboe 
19517afa6fd0SJens Axboe 		/*
19527afa6fd0SJens Axboe 		 * Don't inherit the gift flag, we need to
19537afa6fd0SJens Axboe 		 * prevent multiple steals of this page.
19547afa6fd0SJens Axboe 		 */
19557afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
19567afa6fd0SJens Axboe 
195770524490SJens Axboe 		if (obuf->len > len)
195870524490SJens Axboe 			obuf->len = len;
195970524490SJens Axboe 
196070524490SJens Axboe 		opipe->nrbufs++;
196170524490SJens Axboe 		ret += obuf->len;
196270524490SJens Axboe 		len -= obuf->len;
1963aadd06e5SJens Axboe 		i++;
1964aadd06e5SJens Axboe 	} while (len);
196570524490SJens Axboe 
196602cf01aeSJens Axboe 	/*
196702cf01aeSJens Axboe 	 * return EAGAIN if we have the potential of some data in the
196802cf01aeSJens Axboe 	 * future, otherwise just return 0
196902cf01aeSJens Axboe 	 */
197002cf01aeSJens Axboe 	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
197102cf01aeSJens Axboe 		ret = -EAGAIN;
197202cf01aeSJens Axboe 
197361e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
197461e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
197570524490SJens Axboe 
1976aadd06e5SJens Axboe 	/*
1977aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1978aadd06e5SJens Axboe 	 */
1979825cdcb1SNamhyung Kim 	if (ret > 0)
1980825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
198170524490SJens Axboe 
198270524490SJens Axboe 	return ret;
198370524490SJens Axboe }
198470524490SJens Axboe 
198570524490SJens Axboe /*
198670524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
198770524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
198870524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
198970524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
199070524490SJens Axboe  */
199170524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
199270524490SJens Axboe 		   unsigned int flags)
199370524490SJens Axboe {
199471993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
199571993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1996aadd06e5SJens Axboe 	int ret = -EINVAL;
199770524490SJens Axboe 
199870524490SJens Axboe 	/*
1999aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
2000aadd06e5SJens Axboe 	 * copying the data.
200170524490SJens Axboe 	 */
2002aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
2003aadd06e5SJens Axboe 		/*
2004aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
2005aadd06e5SJens Axboe 		 * ordering doesn't really matter.
2006aadd06e5SJens Axboe 		 */
20077c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
2008aadd06e5SJens Axboe 		if (!ret) {
20097c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
201002cf01aeSJens Axboe 			if (!ret)
2011aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
2012aadd06e5SJens Axboe 		}
2013aadd06e5SJens Axboe 	}
201470524490SJens Axboe 
2015aadd06e5SJens Axboe 	return ret;
201670524490SJens Axboe }
201770524490SJens Axboe 
2018836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
201970524490SJens Axboe {
202070524490SJens Axboe 	struct file *in;
202170524490SJens Axboe 	int error, fput_in;
202270524490SJens Axboe 
202370524490SJens Axboe 	if (unlikely(!len))
202470524490SJens Axboe 		return 0;
202570524490SJens Axboe 
202670524490SJens Axboe 	error = -EBADF;
202770524490SJens Axboe 	in = fget_light(fdin, &fput_in);
202870524490SJens Axboe 	if (in) {
202970524490SJens Axboe 		if (in->f_mode & FMODE_READ) {
203070524490SJens Axboe 			int fput_out;
203170524490SJens Axboe 			struct file *out = fget_light(fdout, &fput_out);
203270524490SJens Axboe 
203370524490SJens Axboe 			if (out) {
203470524490SJens Axboe 				if (out->f_mode & FMODE_WRITE)
203570524490SJens Axboe 					error = do_tee(in, out, len, flags);
203670524490SJens Axboe 				fput_light(out, fput_out);
203770524490SJens Axboe 			}
203870524490SJens Axboe 		}
203970524490SJens Axboe  		fput_light(in, fput_in);
204070524490SJens Axboe  	}
204170524490SJens Axboe 
204270524490SJens Axboe 	return error;
204370524490SJens Axboe }
2044