xref: /openbmc/linux/fs/splice.c (revision 79fddc4e)
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>
3476b021d0SAl Viro #include <linux/compat.h>
3506ae43f3SAl Viro #include "internal.h"
365274f052SJens Axboe 
3783f9135bSJens Axboe /*
3883f9135bSJens Axboe  * Attempt to steal a page from a pipe buffer. This should perhaps go into
3983f9135bSJens Axboe  * a vm helper function, it's already simplified quite a bit by the
4083f9135bSJens Axboe  * addition of remove_mapping(). If success is returned, the caller may
4183f9135bSJens Axboe  * attempt to reuse this page for another destination.
4283f9135bSJens Axboe  */
4376ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
445abc97aaSJens Axboe 				     struct pipe_buffer *buf)
455abc97aaSJens Axboe {
465abc97aaSJens Axboe 	struct page *page = buf->page;
479e94cd4fSJens Axboe 	struct address_space *mapping;
485abc97aaSJens Axboe 
499e0267c2SJens Axboe 	lock_page(page);
509e0267c2SJens Axboe 
519e94cd4fSJens Axboe 	mapping = page_mapping(page);
529e94cd4fSJens Axboe 	if (mapping) {
535abc97aaSJens Axboe 		WARN_ON(!PageUptodate(page));
545abc97aaSJens Axboe 
55ad8d6f0aSJens Axboe 		/*
569e94cd4fSJens Axboe 		 * At least for ext2 with nobh option, we need to wait on
579e94cd4fSJens Axboe 		 * writeback completing on this page, since we'll remove it
589e94cd4fSJens Axboe 		 * from the pagecache.  Otherwise truncate wont wait on the
599e94cd4fSJens Axboe 		 * page, allowing the disk blocks to be reused by someone else
609e94cd4fSJens Axboe 		 * before we actually wrote our data to them. fs corruption
619e94cd4fSJens Axboe 		 * ensues.
62ad8d6f0aSJens Axboe 		 */
63ad8d6f0aSJens Axboe 		wait_on_page_writeback(page);
64ad8d6f0aSJens Axboe 
65266cf658SDavid Howells 		if (page_has_private(page) &&
66266cf658SDavid Howells 		    !try_to_release_page(page, GFP_KERNEL))
67ca39d651SJens Axboe 			goto out_unlock;
684f6f0bd2SJens Axboe 
699e94cd4fSJens Axboe 		/*
709e94cd4fSJens Axboe 		 * If we succeeded in removing the mapping, set LRU flag
719e94cd4fSJens Axboe 		 * and return good.
729e94cd4fSJens Axboe 		 */
739e94cd4fSJens Axboe 		if (remove_mapping(mapping, page)) {
741432873aSJens Axboe 			buf->flags |= PIPE_BUF_FLAG_LRU;
755abc97aaSJens Axboe 			return 0;
765abc97aaSJens Axboe 		}
779e94cd4fSJens Axboe 	}
789e94cd4fSJens Axboe 
799e94cd4fSJens Axboe 	/*
809e94cd4fSJens Axboe 	 * Raced with truncate or failed to remove page from current
819e94cd4fSJens Axboe 	 * address space, unlock and return failure.
829e94cd4fSJens Axboe 	 */
83ca39d651SJens Axboe out_unlock:
849e94cd4fSJens Axboe 	unlock_page(page);
859e94cd4fSJens Axboe 	return 1;
869e94cd4fSJens Axboe }
875abc97aaSJens Axboe 
8876ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
895274f052SJens Axboe 					struct pipe_buffer *buf)
905274f052SJens Axboe {
9109cbfeafSKirill A. Shutemov 	put_page(buf->page);
921432873aSJens Axboe 	buf->flags &= ~PIPE_BUF_FLAG_LRU;
935274f052SJens Axboe }
945274f052SJens Axboe 
950845718dSJens Axboe /*
960845718dSJens Axboe  * Check whether the contents of buf is OK to access. Since the content
970845718dSJens Axboe  * is a page cache page, IO may be in flight.
980845718dSJens Axboe  */
99cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
1005274f052SJens Axboe 				       struct pipe_buffer *buf)
1015274f052SJens Axboe {
1025274f052SJens Axboe 	struct page *page = buf->page;
10349d0b21bSJens Axboe 	int err;
1045274f052SJens Axboe 
1055274f052SJens Axboe 	if (!PageUptodate(page)) {
10649d0b21bSJens Axboe 		lock_page(page);
1075274f052SJens Axboe 
10849d0b21bSJens Axboe 		/*
10949d0b21bSJens Axboe 		 * Page got truncated/unhashed. This will cause a 0-byte
11073d62d83SIngo Molnar 		 * splice, if this is the first page.
11149d0b21bSJens Axboe 		 */
1125274f052SJens Axboe 		if (!page->mapping) {
11349d0b21bSJens Axboe 			err = -ENODATA;
11449d0b21bSJens Axboe 			goto error;
1155274f052SJens Axboe 		}
1165274f052SJens Axboe 
11749d0b21bSJens Axboe 		/*
11873d62d83SIngo Molnar 		 * Uh oh, read-error from disk.
11949d0b21bSJens Axboe 		 */
12049d0b21bSJens Axboe 		if (!PageUptodate(page)) {
12149d0b21bSJens Axboe 			err = -EIO;
12249d0b21bSJens Axboe 			goto error;
12349d0b21bSJens Axboe 		}
12449d0b21bSJens Axboe 
12549d0b21bSJens Axboe 		/*
126f84d7519SJens Axboe 		 * Page is ok afterall, we are done.
12749d0b21bSJens Axboe 		 */
12849d0b21bSJens Axboe 		unlock_page(page);
12949d0b21bSJens Axboe 	}
13049d0b21bSJens Axboe 
131f84d7519SJens Axboe 	return 0;
13249d0b21bSJens Axboe error:
13349d0b21bSJens Axboe 	unlock_page(page);
134f84d7519SJens Axboe 	return err;
13570524490SJens Axboe }
13670524490SJens Axboe 
137708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = {
1385274f052SJens Axboe 	.can_merge = 0,
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,
157cac36bb0SJens Axboe 	.confirm = generic_pipe_buf_confirm,
158912d35f8SJens Axboe 	.release = page_cache_pipe_buf_release,
159912d35f8SJens Axboe 	.steal = user_page_pipe_buf_steal,
160f84d7519SJens Axboe 	.get = generic_pipe_buf_get,
161912d35f8SJens Axboe };
162912d35f8SJens Axboe 
163825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
164825cdcb1SNamhyung Kim {
165825cdcb1SNamhyung Kim 	smp_mb();
166825cdcb1SNamhyung Kim 	if (waitqueue_active(&pipe->wait))
167825cdcb1SNamhyung Kim 		wake_up_interruptible(&pipe->wait);
168825cdcb1SNamhyung Kim 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
169825cdcb1SNamhyung Kim }
170825cdcb1SNamhyung Kim 
171932cc6d4SJens Axboe /**
172932cc6d4SJens Axboe  * splice_to_pipe - fill passed data into a pipe
173932cc6d4SJens Axboe  * @pipe:	pipe to fill
174932cc6d4SJens Axboe  * @spd:	data to fill
175932cc6d4SJens Axboe  *
176932cc6d4SJens Axboe  * Description:
17779685b8dSRandy Dunlap  *    @spd contains a map of pages and len/offset tuples, along with
178932cc6d4SJens Axboe  *    the struct pipe_buf_operations associated with these pages. This
179932cc6d4SJens Axboe  *    function will link that data to the pipe.
180932cc6d4SJens Axboe  *
18183f9135bSJens Axboe  */
182d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183912d35f8SJens Axboe 		       struct splice_pipe_desc *spd)
1845274f052SJens Axboe {
18500de00bdSJens Axboe 	unsigned int spd_pages = spd->nr_pages;
1868924feffSAl Viro 	int ret = 0, page_nr = 0;
1875274f052SJens Axboe 
188d6785d91SRabin Vincent 	if (!spd_pages)
189d6785d91SRabin Vincent 		return 0;
190d6785d91SRabin Vincent 
1918924feffSAl Viro 	if (unlikely(!pipe->readers)) {
1925274f052SJens Axboe 		send_sig(SIGPIPE, current, 0);
1935274f052SJens Axboe 		ret = -EPIPE;
1948924feffSAl Viro 		goto out;
1955274f052SJens Axboe 	}
1965274f052SJens Axboe 
1978924feffSAl Viro 	while (pipe->nrbufs < pipe->buffers) {
19835f3d14dSJens Axboe 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1993a326a2cSIngo Molnar 		struct pipe_buffer *buf = pipe->bufs + newbuf;
2005274f052SJens Axboe 
201912d35f8SJens Axboe 		buf->page = spd->pages[page_nr];
202912d35f8SJens Axboe 		buf->offset = spd->partial[page_nr].offset;
203912d35f8SJens Axboe 		buf->len = spd->partial[page_nr].len;
204497f9625SJens Axboe 		buf->private = spd->partial[page_nr].private;
205912d35f8SJens Axboe 		buf->ops = spd->ops;
2067afa6fd0SJens Axboe 
2076f767b04SJens Axboe 		pipe->nrbufs++;
208912d35f8SJens Axboe 		page_nr++;
209912d35f8SJens Axboe 		ret += buf->len;
210912d35f8SJens Axboe 
211912d35f8SJens Axboe 		if (!--spd->nr_pages)
2125274f052SJens Axboe 			break;
2135274f052SJens Axboe 	}
2145274f052SJens Axboe 
21529e35094SLinus Torvalds 	if (!ret)
21629e35094SLinus Torvalds 		ret = -EAGAIN;
21729e35094SLinus Torvalds 
2188924feffSAl Viro out:
21900de00bdSJens Axboe 	while (page_nr < spd_pages)
220bbdfc2f7SJens Axboe 		spd->spd_release(spd, page_nr++);
2215274f052SJens Axboe 
2225274f052SJens Axboe 	return ret;
2235274f052SJens Axboe }
2242b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe);
2255274f052SJens Axboe 
22679fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
22779fddc4eSAl Viro {
22879fddc4eSAl Viro 	int ret;
22979fddc4eSAl Viro 
23079fddc4eSAl Viro 	if (unlikely(!pipe->readers)) {
23179fddc4eSAl Viro 		send_sig(SIGPIPE, current, 0);
23279fddc4eSAl Viro 		ret = -EPIPE;
23379fddc4eSAl Viro 	} else if (pipe->nrbufs == pipe->buffers) {
23479fddc4eSAl Viro 		ret = -EAGAIN;
23579fddc4eSAl Viro 	} else {
23679fddc4eSAl Viro 		int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
23779fddc4eSAl Viro 		pipe->bufs[newbuf] = *buf;
23879fddc4eSAl Viro 		pipe->nrbufs++;
23979fddc4eSAl Viro 		return buf->len;
24079fddc4eSAl Viro 	}
24179fddc4eSAl Viro 	buf->ops->release(pipe, buf);
24279fddc4eSAl Viro 	buf->ops = NULL;
24379fddc4eSAl Viro 	return ret;
24479fddc4eSAl Viro }
24579fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe);
24679fddc4eSAl Viro 
247708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
248bbdfc2f7SJens Axboe {
24909cbfeafSKirill A. Shutemov 	put_page(spd->pages[i]);
250bbdfc2f7SJens Axboe }
251bbdfc2f7SJens Axboe 
25235f3d14dSJens Axboe /*
25335f3d14dSJens Axboe  * Check if we need to grow the arrays holding pages and partial page
25435f3d14dSJens Axboe  * descriptions.
25535f3d14dSJens Axboe  */
256047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
25735f3d14dSJens Axboe {
258047fe360SEric Dumazet 	unsigned int buffers = ACCESS_ONCE(pipe->buffers);
259047fe360SEric Dumazet 
260047fe360SEric Dumazet 	spd->nr_pages_max = buffers;
261047fe360SEric Dumazet 	if (buffers <= PIPE_DEF_BUFFERS)
26235f3d14dSJens Axboe 		return 0;
26335f3d14dSJens Axboe 
264047fe360SEric Dumazet 	spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
265047fe360SEric Dumazet 	spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
26635f3d14dSJens Axboe 
26735f3d14dSJens Axboe 	if (spd->pages && spd->partial)
26835f3d14dSJens Axboe 		return 0;
26935f3d14dSJens Axboe 
27035f3d14dSJens Axboe 	kfree(spd->pages);
27135f3d14dSJens Axboe 	kfree(spd->partial);
27235f3d14dSJens Axboe 	return -ENOMEM;
27335f3d14dSJens Axboe }
27435f3d14dSJens Axboe 
275047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd)
27635f3d14dSJens Axboe {
277047fe360SEric Dumazet 	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
27835f3d14dSJens Axboe 		return;
27935f3d14dSJens Axboe 
28035f3d14dSJens Axboe 	kfree(spd->pages);
28135f3d14dSJens Axboe 	kfree(spd->partial);
28235f3d14dSJens Axboe }
28335f3d14dSJens Axboe 
2843a326a2cSIngo Molnar static int
285cbb7e577SJens Axboe __generic_file_splice_read(struct file *in, loff_t *ppos,
286cbb7e577SJens Axboe 			   struct pipe_inode_info *pipe, size_t len,
287cbb7e577SJens Axboe 			   unsigned int flags)
2885274f052SJens Axboe {
2895274f052SJens Axboe 	struct address_space *mapping = in->f_mapping;
290d8983910SFengguang Wu 	unsigned int loff, nr_pages, req_pages;
29135f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
29235f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
2935274f052SJens Axboe 	struct page *page;
29491ad66efSJens Axboe 	pgoff_t index, end_index;
29591ad66efSJens Axboe 	loff_t isize;
296eb20796bSJens Axboe 	int error, page_nr;
297912d35f8SJens Axboe 	struct splice_pipe_desc spd = {
298912d35f8SJens Axboe 		.pages = pages,
299912d35f8SJens Axboe 		.partial = partial,
300047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
301912d35f8SJens Axboe 		.flags = flags,
302912d35f8SJens Axboe 		.ops = &page_cache_pipe_buf_ops,
303bbdfc2f7SJens Axboe 		.spd_release = spd_release_page,
304912d35f8SJens Axboe 	};
3055274f052SJens Axboe 
30635f3d14dSJens Axboe 	if (splice_grow_spd(pipe, &spd))
30735f3d14dSJens Axboe 		return -ENOMEM;
30835f3d14dSJens Axboe 
30909cbfeafSKirill A. Shutemov 	index = *ppos >> PAGE_SHIFT;
31009cbfeafSKirill A. Shutemov 	loff = *ppos & ~PAGE_MASK;
31109cbfeafSKirill A. Shutemov 	req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
312047fe360SEric Dumazet 	nr_pages = min(req_pages, spd.nr_pages_max);
3135274f052SJens Axboe 
3145274f052SJens Axboe 	/*
315eb20796bSJens Axboe 	 * Lookup the (hopefully) full range of pages we need.
31682aa5d61SJens Axboe 	 */
31735f3d14dSJens Axboe 	spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
318431a4820SFengguang Wu 	index += spd.nr_pages;
319eb20796bSJens Axboe 
3205274f052SJens Axboe 	/*
321eb20796bSJens Axboe 	 * If find_get_pages_contig() returned fewer pages than we needed,
322431a4820SFengguang Wu 	 * readahead/allocate the rest and fill in the holes.
323eb20796bSJens Axboe 	 */
324431a4820SFengguang Wu 	if (spd.nr_pages < nr_pages)
325cf914a7dSRusty Russell 		page_cache_sync_readahead(mapping, &in->f_ra, in,
326cf914a7dSRusty Russell 				index, req_pages - spd.nr_pages);
327431a4820SFengguang Wu 
328932cc6d4SJens Axboe 	error = 0;
329eb20796bSJens Axboe 	while (spd.nr_pages < nr_pages) {
330eb20796bSJens Axboe 		/*
331eb20796bSJens Axboe 		 * Page could be there, find_get_pages_contig() breaks on
332eb20796bSJens Axboe 		 * the first hole.
3335274f052SJens Axboe 		 */
3347480a904SJens Axboe 		page = find_get_page(mapping, index);
3357480a904SJens Axboe 		if (!page) {
336e27dedd8SJens Axboe 			/*
337eb20796bSJens Axboe 			 * page didn't exist, allocate one.
3387480a904SJens Axboe 			 */
3397480a904SJens Axboe 			page = page_cache_alloc_cold(mapping);
3405274f052SJens Axboe 			if (!page)
3415274f052SJens Axboe 				break;
3425274f052SJens Axboe 
3437480a904SJens Axboe 			error = add_to_page_cache_lru(page, mapping, index,
344c62d2555SMichal Hocko 				   mapping_gfp_constraint(mapping, GFP_KERNEL));
3455274f052SJens Axboe 			if (unlikely(error)) {
34609cbfeafSKirill A. Shutemov 				put_page(page);
347a0548871SJens Axboe 				if (error == -EEXIST)
348a0548871SJens Axboe 					continue;
3495274f052SJens Axboe 				break;
3505274f052SJens Axboe 			}
351eb20796bSJens Axboe 			/*
352eb20796bSJens Axboe 			 * add_to_page_cache() locks the page, unlock it
353eb20796bSJens Axboe 			 * to avoid convoluting the logic below even more.
354eb20796bSJens Axboe 			 */
355eb20796bSJens Axboe 			unlock_page(page);
3565274f052SJens Axboe 		}
3577480a904SJens Axboe 
35835f3d14dSJens Axboe 		spd.pages[spd.nr_pages++] = page;
359eb20796bSJens Axboe 		index++;
360eb20796bSJens Axboe 	}
361eb20796bSJens Axboe 
362eb20796bSJens Axboe 	/*
363eb20796bSJens Axboe 	 * Now loop over the map and see if we need to start IO on any
364eb20796bSJens Axboe 	 * pages, fill in the partial map, etc.
365eb20796bSJens Axboe 	 */
36609cbfeafSKirill A. Shutemov 	index = *ppos >> PAGE_SHIFT;
367eb20796bSJens Axboe 	nr_pages = spd.nr_pages;
368eb20796bSJens Axboe 	spd.nr_pages = 0;
369eb20796bSJens Axboe 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
370eb20796bSJens Axboe 		unsigned int this_len;
371eb20796bSJens Axboe 
372eb20796bSJens Axboe 		if (!len)
373eb20796bSJens Axboe 			break;
374eb20796bSJens Axboe 
375eb20796bSJens Axboe 		/*
376eb20796bSJens Axboe 		 * this_len is the max we'll use from this page
377eb20796bSJens Axboe 		 */
37809cbfeafSKirill A. Shutemov 		this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
37935f3d14dSJens Axboe 		page = spd.pages[page_nr];
380eb20796bSJens Axboe 
381a08a166fSFengguang Wu 		if (PageReadahead(page))
382cf914a7dSRusty Russell 			page_cache_async_readahead(mapping, &in->f_ra, in,
383d8983910SFengguang Wu 					page, index, req_pages - page_nr);
384a08a166fSFengguang Wu 
3857480a904SJens Axboe 		/*
3867480a904SJens Axboe 		 * If the page isn't uptodate, we may need to start io on it
3877480a904SJens Axboe 		 */
3887480a904SJens Axboe 		if (!PageUptodate(page)) {
3897480a904SJens Axboe 			lock_page(page);
3907480a904SJens Axboe 
3917480a904SJens Axboe 			/*
39232502b84SMiklos Szeredi 			 * Page was truncated, or invalidated by the
39332502b84SMiklos Szeredi 			 * filesystem.  Redo the find/create, but this time the
39432502b84SMiklos Szeredi 			 * page is kept locked, so there's no chance of another
39532502b84SMiklos Szeredi 			 * race with truncate/invalidate.
3967480a904SJens Axboe 			 */
3977480a904SJens Axboe 			if (!page->mapping) {
3987480a904SJens Axboe 				unlock_page(page);
39990330e68SAbhi Das retry_lookup:
40032502b84SMiklos Szeredi 				page = find_or_create_page(mapping, index,
40132502b84SMiklos Szeredi 						mapping_gfp_mask(mapping));
40232502b84SMiklos Szeredi 
40332502b84SMiklos Szeredi 				if (!page) {
40432502b84SMiklos Szeredi 					error = -ENOMEM;
4057480a904SJens Axboe 					break;
4067480a904SJens Axboe 				}
40709cbfeafSKirill A. Shutemov 				put_page(spd.pages[page_nr]);
40835f3d14dSJens Axboe 				spd.pages[page_nr] = page;
40932502b84SMiklos Szeredi 			}
4107480a904SJens Axboe 			/*
4117480a904SJens Axboe 			 * page was already under io and is now done, great
4127480a904SJens Axboe 			 */
4137480a904SJens Axboe 			if (PageUptodate(page)) {
4147480a904SJens Axboe 				unlock_page(page);
4157480a904SJens Axboe 				goto fill_it;
4167480a904SJens Axboe 			}
4177480a904SJens Axboe 
4187480a904SJens Axboe 			/*
4197480a904SJens Axboe 			 * need to read in the page
4207480a904SJens Axboe 			 */
4217480a904SJens Axboe 			error = mapping->a_ops->readpage(in, page);
4227480a904SJens Axboe 			if (unlikely(error)) {
423eb20796bSJens Axboe 				/*
42490330e68SAbhi Das 				 * Re-lookup the page
425eb20796bSJens Axboe 				 */
4267480a904SJens Axboe 				if (error == AOP_TRUNCATED_PAGE)
42790330e68SAbhi Das 					goto retry_lookup;
428eb20796bSJens Axboe 
4297480a904SJens Axboe 				break;
4307480a904SJens Axboe 			}
431620a324bSJens Axboe 		}
432620a324bSJens Axboe fill_it:
43391ad66efSJens Axboe 		/*
434620a324bSJens Axboe 		 * i_size must be checked after PageUptodate.
43591ad66efSJens Axboe 		 */
43691ad66efSJens Axboe 		isize = i_size_read(mapping->host);
43709cbfeafSKirill A. Shutemov 		end_index = (isize - 1) >> PAGE_SHIFT;
438eb20796bSJens Axboe 		if (unlikely(!isize || index > end_index))
43991ad66efSJens Axboe 			break;
44091ad66efSJens Axboe 
44191ad66efSJens Axboe 		/*
44291ad66efSJens Axboe 		 * if this is the last page, see if we need to shrink
44391ad66efSJens Axboe 		 * the length and stop
44491ad66efSJens Axboe 		 */
44591ad66efSJens Axboe 		if (end_index == index) {
446475ecadeSHugh Dickins 			unsigned int plen;
447475ecadeSHugh Dickins 
448475ecadeSHugh Dickins 			/*
449475ecadeSHugh Dickins 			 * max good bytes in this page
450475ecadeSHugh Dickins 			 */
45109cbfeafSKirill A. Shutemov 			plen = ((isize - 1) & ~PAGE_MASK) + 1;
452475ecadeSHugh Dickins 			if (plen <= loff)
45391ad66efSJens Axboe 				break;
454475ecadeSHugh Dickins 
45591ad66efSJens Axboe 			/*
45691ad66efSJens Axboe 			 * force quit after adding this page
45791ad66efSJens Axboe 			 */
458475ecadeSHugh Dickins 			this_len = min(this_len, plen - loff);
459eb20796bSJens Axboe 			len = this_len;
46091ad66efSJens Axboe 		}
461620a324bSJens Axboe 
46235f3d14dSJens Axboe 		spd.partial[page_nr].offset = loff;
46335f3d14dSJens Axboe 		spd.partial[page_nr].len = this_len;
46482aa5d61SJens Axboe 		len -= this_len;
46591ad66efSJens Axboe 		loff = 0;
466eb20796bSJens Axboe 		spd.nr_pages++;
467eb20796bSJens Axboe 		index++;
4685274f052SJens Axboe 	}
4695274f052SJens Axboe 
470eb20796bSJens Axboe 	/*
471475ecadeSHugh Dickins 	 * Release any pages at the end, if we quit early. 'page_nr' is how far
472eb20796bSJens Axboe 	 * we got, 'nr_pages' is how many pages are in the map.
473eb20796bSJens Axboe 	 */
474eb20796bSJens Axboe 	while (page_nr < nr_pages)
47509cbfeafSKirill A. Shutemov 		put_page(spd.pages[page_nr++]);
47609cbfeafSKirill A. Shutemov 	in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
477eb20796bSJens Axboe 
478912d35f8SJens Axboe 	if (spd.nr_pages)
47935f3d14dSJens Axboe 		error = splice_to_pipe(pipe, &spd);
48016c523ddSJens Axboe 
481047fe360SEric Dumazet 	splice_shrink_spd(&spd);
4827480a904SJens Axboe 	return error;
4835274f052SJens Axboe }
4845274f052SJens Axboe 
48583f9135bSJens Axboe /**
48683f9135bSJens Axboe  * generic_file_splice_read - splice data from file to a pipe
48783f9135bSJens Axboe  * @in:		file to splice from
488932cc6d4SJens Axboe  * @ppos:	position in @in
48983f9135bSJens Axboe  * @pipe:	pipe to splice to
49083f9135bSJens Axboe  * @len:	number of bytes to splice
49183f9135bSJens Axboe  * @flags:	splice modifier flags
49283f9135bSJens Axboe  *
493932cc6d4SJens Axboe  * Description:
494932cc6d4SJens Axboe  *    Will read pages from given file and fill them into a pipe. Can be
495932cc6d4SJens Axboe  *    used as long as the address_space operations for the source implements
496932cc6d4SJens Axboe  *    a readpage() hook.
497932cc6d4SJens Axboe  *
49883f9135bSJens Axboe  */
499cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
500cbb7e577SJens Axboe 				 struct pipe_inode_info *pipe, size_t len,
501cbb7e577SJens Axboe 				 unsigned int flags)
5025274f052SJens Axboe {
503d366d398SJens Axboe 	loff_t isize, left;
5048191ecd1SJens Axboe 	int ret;
505d366d398SJens Axboe 
506be64f884SBoaz Harrosh 	if (IS_DAX(in->f_mapping->host))
507be64f884SBoaz Harrosh 		return default_file_splice_read(in, ppos, pipe, len, flags);
508be64f884SBoaz Harrosh 
509d366d398SJens Axboe 	isize = i_size_read(in->f_mapping->host);
510d366d398SJens Axboe 	if (unlikely(*ppos >= isize))
511d366d398SJens Axboe 		return 0;
512d366d398SJens Axboe 
513d366d398SJens Axboe 	left = isize - *ppos;
514d366d398SJens Axboe 	if (unlikely(left < len))
515d366d398SJens Axboe 		len = left;
5165274f052SJens Axboe 
517cbb7e577SJens Axboe 	ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
518723590edSMiklos Szeredi 	if (ret > 0) {
519cbb7e577SJens Axboe 		*ppos += ret;
520723590edSMiklos Szeredi 		file_accessed(in);
521723590edSMiklos Szeredi 	}
5225274f052SJens Axboe 
5235274f052SJens Axboe 	return ret;
5245274f052SJens Axboe }
525059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read);
526059a8f37SJens Axboe 
5276818173bSMiklos Szeredi static const struct pipe_buf_operations default_pipe_buf_ops = {
5286818173bSMiklos Szeredi 	.can_merge = 0,
5296818173bSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
5306818173bSMiklos Szeredi 	.release = generic_pipe_buf_release,
5316818173bSMiklos Szeredi 	.steal = generic_pipe_buf_steal,
5326818173bSMiklos Szeredi 	.get = generic_pipe_buf_get,
5336818173bSMiklos Szeredi };
5346818173bSMiklos Szeredi 
53528a625cbSMiklos Szeredi static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
53628a625cbSMiklos Szeredi 				    struct pipe_buffer *buf)
53728a625cbSMiklos Szeredi {
53828a625cbSMiklos Szeredi 	return 1;
53928a625cbSMiklos Szeredi }
54028a625cbSMiklos Szeredi 
54128a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */
54228a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = {
54328a625cbSMiklos Szeredi 	.can_merge = 0,
54428a625cbSMiklos Szeredi 	.confirm = generic_pipe_buf_confirm,
54528a625cbSMiklos Szeredi 	.release = generic_pipe_buf_release,
54628a625cbSMiklos Szeredi 	.steal = generic_pipe_buf_nosteal,
54728a625cbSMiklos Szeredi 	.get = generic_pipe_buf_get,
54828a625cbSMiklos Szeredi };
54928a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops);
55028a625cbSMiklos Szeredi 
5516818173bSMiklos Szeredi static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
5526818173bSMiklos Szeredi 			    unsigned long vlen, loff_t offset)
5536818173bSMiklos Szeredi {
5546818173bSMiklos Szeredi 	mm_segment_t old_fs;
5556818173bSMiklos Szeredi 	loff_t pos = offset;
5566818173bSMiklos Szeredi 	ssize_t res;
5576818173bSMiklos Szeredi 
5586818173bSMiklos Szeredi 	old_fs = get_fs();
5596818173bSMiklos Szeredi 	set_fs(get_ds());
5606818173bSMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
561793b80efSChristoph Hellwig 	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
5626818173bSMiklos Szeredi 	set_fs(old_fs);
5636818173bSMiklos Szeredi 
5646818173bSMiklos Szeredi 	return res;
5656818173bSMiklos Szeredi }
5666818173bSMiklos Szeredi 
5677bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count,
568b2858d7dSMiklos Szeredi 			    loff_t pos)
5690b0a47f5SMiklos Szeredi {
5700b0a47f5SMiklos Szeredi 	mm_segment_t old_fs;
5710b0a47f5SMiklos Szeredi 	ssize_t res;
5720b0a47f5SMiklos Szeredi 
5730b0a47f5SMiklos Szeredi 	old_fs = get_fs();
5740b0a47f5SMiklos Szeredi 	set_fs(get_ds());
5750b0a47f5SMiklos Szeredi 	/* The cast to a user pointer is valid due to the set_fs() */
5767bb307e8SAl Viro 	res = vfs_write(file, (__force const char __user *)buf, count, &pos);
5770b0a47f5SMiklos Szeredi 	set_fs(old_fs);
5780b0a47f5SMiklos Szeredi 
5790b0a47f5SMiklos Szeredi 	return res;
5800b0a47f5SMiklos Szeredi }
5817bb307e8SAl Viro EXPORT_SYMBOL(kernel_write);
5820b0a47f5SMiklos Szeredi 
5836818173bSMiklos Szeredi ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
5846818173bSMiklos Szeredi 				 struct pipe_inode_info *pipe, size_t len,
5856818173bSMiklos Szeredi 				 unsigned int flags)
5866818173bSMiklos Szeredi {
5876818173bSMiklos Szeredi 	unsigned int nr_pages;
5886818173bSMiklos Szeredi 	unsigned int nr_freed;
5896818173bSMiklos Szeredi 	size_t offset;
59035f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
59135f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
59235f3d14dSJens Axboe 	struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
5936818173bSMiklos Szeredi 	ssize_t res;
5946818173bSMiklos Szeredi 	size_t this_len;
5956818173bSMiklos Szeredi 	int error;
5966818173bSMiklos Szeredi 	int i;
5976818173bSMiklos Szeredi 	struct splice_pipe_desc spd = {
5986818173bSMiklos Szeredi 		.pages = pages,
5996818173bSMiklos Szeredi 		.partial = partial,
600047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
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;
611047fe360SEric Dumazet 	if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
612047fe360SEric Dumazet 		vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
61335f3d14dSJens Axboe 		if (!vec)
61435f3d14dSJens Axboe 			goto shrink_ret;
61535f3d14dSJens Axboe 	}
61635f3d14dSJens Axboe 
61709cbfeafSKirill A. Shutemov 	offset = *ppos & ~PAGE_MASK;
61809cbfeafSKirill A. Shutemov 	nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
6196818173bSMiklos Szeredi 
620047fe360SEric Dumazet 	for (i = 0; i < nr_pages && i < spd.nr_pages_max && 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 
62809cbfeafSKirill A. Shutemov 		this_len = min_t(size_t, len, PAGE_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);
668047fe360SEric Dumazet 	splice_shrink_spd(&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 
69172c2d531SAl Viro 	if (!likely(file->f_op->sendpage))
692a8adbe37SMichał Mirosław 		return -EINVAL;
693a8adbe37SMichał Mirosław 
69435f9c09fSEric Dumazet 	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
695ae62ca7bSEric Dumazet 
696ae62ca7bSEric Dumazet 	if (sd->len < sd->total_len && pipe->nrbufs > 1)
69735f9c09fSEric Dumazet 		more |= MSG_SENDPAGE_NOTLAST;
698ae62ca7bSEric Dumazet 
699a8adbe37SMichał Mirosław 	return file->f_op->sendpage(file, buf->page, buf->offset,
700f84d7519SJens Axboe 				    sd->len, &pos, more);
7015274f052SJens Axboe }
7025274f052SJens Axboe 
703b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
704b3c2d2ddSMiklos Szeredi {
705b3c2d2ddSMiklos Szeredi 	smp_mb();
706b3c2d2ddSMiklos Szeredi 	if (waitqueue_active(&pipe->wait))
707b3c2d2ddSMiklos Szeredi 		wake_up_interruptible(&pipe->wait);
708b3c2d2ddSMiklos Szeredi 	kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
709b3c2d2ddSMiklos Szeredi }
710b3c2d2ddSMiklos Szeredi 
711b3c2d2ddSMiklos Szeredi /**
712b3c2d2ddSMiklos Szeredi  * splice_from_pipe_feed - feed available data from a pipe to a file
713b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
714b3c2d2ddSMiklos Szeredi  * @sd:		information to @actor
715b3c2d2ddSMiklos Szeredi  * @actor:	handler that splices the data
716b3c2d2ddSMiklos Szeredi  *
717b3c2d2ddSMiklos Szeredi  * Description:
718b3c2d2ddSMiklos Szeredi  *    This function loops over the pipe and calls @actor to do the
719b3c2d2ddSMiklos Szeredi  *    actual moving of a single struct pipe_buffer to the desired
720b3c2d2ddSMiklos Szeredi  *    destination.  It returns when there's no more buffers left in
721b3c2d2ddSMiklos Szeredi  *    the pipe or if the requested number of bytes (@sd->total_len)
722b3c2d2ddSMiklos Szeredi  *    have been copied.  It returns a positive number (one) if the
723b3c2d2ddSMiklos Szeredi  *    pipe needs to be filled with more data, zero if the required
724b3c2d2ddSMiklos Szeredi  *    number of bytes have been copied and -errno on error.
725b3c2d2ddSMiklos Szeredi  *
726b3c2d2ddSMiklos Szeredi  *    This, together with splice_from_pipe_{begin,end,next}, may be
727b3c2d2ddSMiklos Szeredi  *    used to implement the functionality of __splice_from_pipe() when
728b3c2d2ddSMiklos Szeredi  *    locking is required around copying the pipe buffers to the
729b3c2d2ddSMiklos Szeredi  *    destination.
730b3c2d2ddSMiklos Szeredi  */
73196f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
732b3c2d2ddSMiklos Szeredi 			  splice_actor *actor)
733b3c2d2ddSMiklos Szeredi {
734b3c2d2ddSMiklos Szeredi 	int ret;
735b3c2d2ddSMiklos Szeredi 
736b3c2d2ddSMiklos Szeredi 	while (pipe->nrbufs) {
737b3c2d2ddSMiklos Szeredi 		struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
738b3c2d2ddSMiklos Szeredi 		const struct pipe_buf_operations *ops = buf->ops;
739b3c2d2ddSMiklos Szeredi 
740b3c2d2ddSMiklos Szeredi 		sd->len = buf->len;
741b3c2d2ddSMiklos Szeredi 		if (sd->len > sd->total_len)
742b3c2d2ddSMiklos Szeredi 			sd->len = sd->total_len;
743b3c2d2ddSMiklos Szeredi 
744a8adbe37SMichał Mirosław 		ret = buf->ops->confirm(pipe, buf);
745a8adbe37SMichał Mirosław 		if (unlikely(ret)) {
746b3c2d2ddSMiklos Szeredi 			if (ret == -ENODATA)
747b3c2d2ddSMiklos Szeredi 				ret = 0;
748b3c2d2ddSMiklos Szeredi 			return ret;
749b3c2d2ddSMiklos Szeredi 		}
750a8adbe37SMichał Mirosław 
751a8adbe37SMichał Mirosław 		ret = actor(pipe, buf, sd);
752a8adbe37SMichał Mirosław 		if (ret <= 0)
753a8adbe37SMichał Mirosław 			return ret;
754a8adbe37SMichał Mirosław 
755b3c2d2ddSMiklos Szeredi 		buf->offset += ret;
756b3c2d2ddSMiklos Szeredi 		buf->len -= ret;
757b3c2d2ddSMiklos Szeredi 
758b3c2d2ddSMiklos Szeredi 		sd->num_spliced += ret;
759b3c2d2ddSMiklos Szeredi 		sd->len -= ret;
760b3c2d2ddSMiklos Szeredi 		sd->pos += ret;
761b3c2d2ddSMiklos Szeredi 		sd->total_len -= ret;
762b3c2d2ddSMiklos Szeredi 
763b3c2d2ddSMiklos Szeredi 		if (!buf->len) {
764b3c2d2ddSMiklos Szeredi 			buf->ops = NULL;
765b3c2d2ddSMiklos Szeredi 			ops->release(pipe, buf);
76635f3d14dSJens Axboe 			pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
767b3c2d2ddSMiklos Szeredi 			pipe->nrbufs--;
7686447a3cfSAl Viro 			if (pipe->files)
769b3c2d2ddSMiklos Szeredi 				sd->need_wakeup = true;
770b3c2d2ddSMiklos Szeredi 		}
771b3c2d2ddSMiklos Szeredi 
772b3c2d2ddSMiklos Szeredi 		if (!sd->total_len)
773b3c2d2ddSMiklos Szeredi 			return 0;
774b3c2d2ddSMiklos Szeredi 	}
775b3c2d2ddSMiklos Szeredi 
776b3c2d2ddSMiklos Szeredi 	return 1;
777b3c2d2ddSMiklos Szeredi }
778b3c2d2ddSMiklos Szeredi 
779b3c2d2ddSMiklos Szeredi /**
780b3c2d2ddSMiklos Szeredi  * splice_from_pipe_next - wait for some data to splice from
781b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
782b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
783b3c2d2ddSMiklos Szeredi  *
784b3c2d2ddSMiklos Szeredi  * Description:
785b3c2d2ddSMiklos Szeredi  *    This function will wait for some data and return a positive
786b3c2d2ddSMiklos Szeredi  *    value (one) if pipe buffers are available.  It will return zero
787b3c2d2ddSMiklos Szeredi  *    or -errno if no more data needs to be spliced.
788b3c2d2ddSMiklos Szeredi  */
78996f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
790b3c2d2ddSMiklos Szeredi {
791c725bfceSJan Kara 	/*
792c725bfceSJan Kara 	 * Check for signal early to make process killable when there are
793c725bfceSJan Kara 	 * always buffers available
794c725bfceSJan Kara 	 */
795c725bfceSJan Kara 	if (signal_pending(current))
796c725bfceSJan Kara 		return -ERESTARTSYS;
797c725bfceSJan Kara 
798b3c2d2ddSMiklos Szeredi 	while (!pipe->nrbufs) {
799b3c2d2ddSMiklos Szeredi 		if (!pipe->writers)
800b3c2d2ddSMiklos Szeredi 			return 0;
801b3c2d2ddSMiklos Szeredi 
802b3c2d2ddSMiklos Szeredi 		if (!pipe->waiting_writers && sd->num_spliced)
803b3c2d2ddSMiklos Szeredi 			return 0;
804b3c2d2ddSMiklos Szeredi 
805b3c2d2ddSMiklos Szeredi 		if (sd->flags & SPLICE_F_NONBLOCK)
806b3c2d2ddSMiklos Szeredi 			return -EAGAIN;
807b3c2d2ddSMiklos Szeredi 
808b3c2d2ddSMiklos Szeredi 		if (signal_pending(current))
809b3c2d2ddSMiklos Szeredi 			return -ERESTARTSYS;
810b3c2d2ddSMiklos Szeredi 
811b3c2d2ddSMiklos Szeredi 		if (sd->need_wakeup) {
812b3c2d2ddSMiklos Szeredi 			wakeup_pipe_writers(pipe);
813b3c2d2ddSMiklos Szeredi 			sd->need_wakeup = false;
814b3c2d2ddSMiklos Szeredi 		}
815b3c2d2ddSMiklos Szeredi 
816b3c2d2ddSMiklos Szeredi 		pipe_wait(pipe);
817b3c2d2ddSMiklos Szeredi 	}
818b3c2d2ddSMiklos Szeredi 
819b3c2d2ddSMiklos Szeredi 	return 1;
820b3c2d2ddSMiklos Szeredi }
821b3c2d2ddSMiklos Szeredi 
822b3c2d2ddSMiklos Szeredi /**
823b3c2d2ddSMiklos Szeredi  * splice_from_pipe_begin - start splicing from pipe
824b80901bbSRandy Dunlap  * @sd:		information about the splice operation
825b3c2d2ddSMiklos Szeredi  *
826b3c2d2ddSMiklos Szeredi  * Description:
827b3c2d2ddSMiklos Szeredi  *    This function should be called before a loop containing
828b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_next() and splice_from_pipe_feed() to
829b3c2d2ddSMiklos Szeredi  *    initialize the necessary fields of @sd.
830b3c2d2ddSMiklos Szeredi  */
83196f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd)
832b3c2d2ddSMiklos Szeredi {
833b3c2d2ddSMiklos Szeredi 	sd->num_spliced = 0;
834b3c2d2ddSMiklos Szeredi 	sd->need_wakeup = false;
835b3c2d2ddSMiklos Szeredi }
836b3c2d2ddSMiklos Szeredi 
837b3c2d2ddSMiklos Szeredi /**
838b3c2d2ddSMiklos Szeredi  * splice_from_pipe_end - finish splicing from pipe
839b3c2d2ddSMiklos Szeredi  * @pipe:	pipe to splice from
840b3c2d2ddSMiklos Szeredi  * @sd:		information about the splice operation
841b3c2d2ddSMiklos Szeredi  *
842b3c2d2ddSMiklos Szeredi  * Description:
843b3c2d2ddSMiklos Szeredi  *    This function will wake up pipe writers if necessary.  It should
844b3c2d2ddSMiklos Szeredi  *    be called after a loop containing splice_from_pipe_next() and
845b3c2d2ddSMiklos Szeredi  *    splice_from_pipe_feed().
846b3c2d2ddSMiklos Szeredi  */
84796f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
848b3c2d2ddSMiklos Szeredi {
849b3c2d2ddSMiklos Szeredi 	if (sd->need_wakeup)
850b3c2d2ddSMiklos Szeredi 		wakeup_pipe_writers(pipe);
851b3c2d2ddSMiklos Szeredi }
852b3c2d2ddSMiklos Szeredi 
853932cc6d4SJens Axboe /**
854932cc6d4SJens Axboe  * __splice_from_pipe - splice data from a pipe to given actor
855932cc6d4SJens Axboe  * @pipe:	pipe to splice from
856932cc6d4SJens Axboe  * @sd:		information to @actor
857932cc6d4SJens Axboe  * @actor:	handler that splices the data
858932cc6d4SJens Axboe  *
859932cc6d4SJens Axboe  * Description:
860932cc6d4SJens Axboe  *    This function does little more than loop over the pipe and call
861932cc6d4SJens Axboe  *    @actor to do the actual moving of a single struct pipe_buffer to
862932cc6d4SJens Axboe  *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
863932cc6d4SJens Axboe  *    pipe_to_user.
864932cc6d4SJens Axboe  *
86583f9135bSJens Axboe  */
866c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
867c66ab6faSJens Axboe 			   splice_actor *actor)
8685274f052SJens Axboe {
869b3c2d2ddSMiklos Szeredi 	int ret;
8705274f052SJens Axboe 
871b3c2d2ddSMiklos Szeredi 	splice_from_pipe_begin(sd);
872b3c2d2ddSMiklos Szeredi 	do {
873c2489e07SJan Kara 		cond_resched();
874b3c2d2ddSMiklos Szeredi 		ret = splice_from_pipe_next(pipe, sd);
875b3c2d2ddSMiklos Szeredi 		if (ret > 0)
876b3c2d2ddSMiklos Szeredi 			ret = splice_from_pipe_feed(pipe, sd, actor);
877b3c2d2ddSMiklos Szeredi 	} while (ret > 0);
878b3c2d2ddSMiklos Szeredi 	splice_from_pipe_end(pipe, sd);
8795274f052SJens Axboe 
880b3c2d2ddSMiklos Szeredi 	return sd->num_spliced ? sd->num_spliced : ret;
8815274f052SJens Axboe }
88240bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe);
8835274f052SJens Axboe 
884932cc6d4SJens Axboe /**
885932cc6d4SJens Axboe  * splice_from_pipe - splice data from a pipe to a file
886932cc6d4SJens Axboe  * @pipe:	pipe to splice from
887932cc6d4SJens Axboe  * @out:	file to splice to
888932cc6d4SJens Axboe  * @ppos:	position in @out
889932cc6d4SJens Axboe  * @len:	how many bytes to splice
890932cc6d4SJens Axboe  * @flags:	splice modifier flags
891932cc6d4SJens Axboe  * @actor:	handler that splices the data
892932cc6d4SJens Axboe  *
893932cc6d4SJens Axboe  * Description:
8942933970bSMiklos Szeredi  *    See __splice_from_pipe. This function locks the pipe inode,
895932cc6d4SJens Axboe  *    otherwise it's identical to __splice_from_pipe().
896932cc6d4SJens Axboe  *
897932cc6d4SJens Axboe  */
8986da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
8996da61809SMark Fasheh 			 loff_t *ppos, size_t len, unsigned int flags,
9006da61809SMark Fasheh 			 splice_actor *actor)
9016da61809SMark Fasheh {
9026da61809SMark Fasheh 	ssize_t ret;
903c66ab6faSJens Axboe 	struct splice_desc sd = {
904c66ab6faSJens Axboe 		.total_len = len,
905c66ab6faSJens Axboe 		.flags = flags,
906c66ab6faSJens Axboe 		.pos = *ppos,
9076a14b90bSJens Axboe 		.u.file = out,
908c66ab6faSJens Axboe 	};
9096da61809SMark Fasheh 
91061e0d47cSMiklos Szeredi 	pipe_lock(pipe);
911c66ab6faSJens Axboe 	ret = __splice_from_pipe(pipe, &sd, actor);
91261e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
9136da61809SMark Fasheh 
9146da61809SMark Fasheh 	return ret;
9156da61809SMark Fasheh }
9166da61809SMark Fasheh 
9176da61809SMark Fasheh /**
9188d020765SAl Viro  * iter_file_splice_write - splice data from a pipe to a file
9198d020765SAl Viro  * @pipe:	pipe info
9208d020765SAl Viro  * @out:	file to write to
9218d020765SAl Viro  * @ppos:	position in @out
9228d020765SAl Viro  * @len:	number of bytes to splice
9238d020765SAl Viro  * @flags:	splice modifier flags
9248d020765SAl Viro  *
9258d020765SAl Viro  * Description:
9268d020765SAl Viro  *    Will either move or copy pages (determined by @flags options) from
9278d020765SAl Viro  *    the given pipe inode to the given file.
9288d020765SAl Viro  *    This one is ->write_iter-based.
9298d020765SAl Viro  *
9308d020765SAl Viro  */
9318d020765SAl Viro ssize_t
9328d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
9338d020765SAl Viro 			  loff_t *ppos, size_t len, unsigned int flags)
9348d020765SAl Viro {
9358d020765SAl Viro 	struct splice_desc sd = {
9368d020765SAl Viro 		.total_len = len,
9378d020765SAl Viro 		.flags = flags,
9388d020765SAl Viro 		.pos = *ppos,
9398d020765SAl Viro 		.u.file = out,
9408d020765SAl Viro 	};
9418d020765SAl Viro 	int nbufs = pipe->buffers;
9428d020765SAl Viro 	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
9438d020765SAl Viro 					GFP_KERNEL);
9448d020765SAl Viro 	ssize_t ret;
9458d020765SAl Viro 
9468d020765SAl Viro 	if (unlikely(!array))
9478d020765SAl Viro 		return -ENOMEM;
9488d020765SAl Viro 
9498d020765SAl Viro 	pipe_lock(pipe);
9508d020765SAl Viro 
9518d020765SAl Viro 	splice_from_pipe_begin(&sd);
9528d020765SAl Viro 	while (sd.total_len) {
9538d020765SAl Viro 		struct iov_iter from;
9548d020765SAl Viro 		size_t left;
9558d020765SAl Viro 		int n, idx;
9568d020765SAl Viro 
9578d020765SAl Viro 		ret = splice_from_pipe_next(pipe, &sd);
9588d020765SAl Viro 		if (ret <= 0)
9598d020765SAl Viro 			break;
9608d020765SAl Viro 
9618d020765SAl Viro 		if (unlikely(nbufs < pipe->buffers)) {
9628d020765SAl Viro 			kfree(array);
9638d020765SAl Viro 			nbufs = pipe->buffers;
9648d020765SAl Viro 			array = kcalloc(nbufs, sizeof(struct bio_vec),
9658d020765SAl Viro 					GFP_KERNEL);
9668d020765SAl Viro 			if (!array) {
9678d020765SAl Viro 				ret = -ENOMEM;
9688d020765SAl Viro 				break;
9698d020765SAl Viro 			}
9708d020765SAl Viro 		}
9718d020765SAl Viro 
9728d020765SAl Viro 		/* build the vector */
9738d020765SAl Viro 		left = sd.total_len;
9748d020765SAl Viro 		for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
9758d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + idx;
9768d020765SAl Viro 			size_t this_len = buf->len;
9778d020765SAl Viro 
9788d020765SAl Viro 			if (this_len > left)
9798d020765SAl Viro 				this_len = left;
9808d020765SAl Viro 
9818d020765SAl Viro 			if (idx == pipe->buffers - 1)
9828d020765SAl Viro 				idx = -1;
9838d020765SAl Viro 
9848d020765SAl Viro 			ret = buf->ops->confirm(pipe, buf);
9858d020765SAl Viro 			if (unlikely(ret)) {
9868d020765SAl Viro 				if (ret == -ENODATA)
9878d020765SAl Viro 					ret = 0;
9888d020765SAl Viro 				goto done;
9898d020765SAl Viro 			}
9908d020765SAl Viro 
9918d020765SAl Viro 			array[n].bv_page = buf->page;
9928d020765SAl Viro 			array[n].bv_len = this_len;
9938d020765SAl Viro 			array[n].bv_offset = buf->offset;
9948d020765SAl Viro 			left -= this_len;
9958d020765SAl Viro 		}
9968d020765SAl Viro 
99705afcb77SAl Viro 		iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
99805afcb77SAl Viro 			      sd.total_len - left);
999dbe4e192SChristoph Hellwig 		ret = vfs_iter_write(out, &from, &sd.pos);
10008d020765SAl Viro 		if (ret <= 0)
10018d020765SAl Viro 			break;
10028d020765SAl Viro 
10038d020765SAl Viro 		sd.num_spliced += ret;
10048d020765SAl Viro 		sd.total_len -= ret;
1005dbe4e192SChristoph Hellwig 		*ppos = sd.pos;
10068d020765SAl Viro 
10078d020765SAl Viro 		/* dismiss the fully eaten buffers, adjust the partial one */
10088d020765SAl Viro 		while (ret) {
10098d020765SAl Viro 			struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
10108d020765SAl Viro 			if (ret >= buf->len) {
10118d020765SAl Viro 				const struct pipe_buf_operations *ops = buf->ops;
10128d020765SAl Viro 				ret -= buf->len;
10138d020765SAl Viro 				buf->len = 0;
10148d020765SAl Viro 				buf->ops = NULL;
10158d020765SAl Viro 				ops->release(pipe, buf);
10168d020765SAl Viro 				pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
10178d020765SAl Viro 				pipe->nrbufs--;
10188d020765SAl Viro 				if (pipe->files)
10198d020765SAl Viro 					sd.need_wakeup = true;
10208d020765SAl Viro 			} else {
10218d020765SAl Viro 				buf->offset += ret;
10228d020765SAl Viro 				buf->len -= ret;
10238d020765SAl Viro 				ret = 0;
10248d020765SAl Viro 			}
10258d020765SAl Viro 		}
10268d020765SAl Viro 	}
10278d020765SAl Viro done:
10288d020765SAl Viro 	kfree(array);
10298d020765SAl Viro 	splice_from_pipe_end(pipe, &sd);
10308d020765SAl Viro 
10318d020765SAl Viro 	pipe_unlock(pipe);
10328d020765SAl Viro 
10338d020765SAl Viro 	if (sd.num_spliced)
10348d020765SAl Viro 		ret = sd.num_spliced;
10358d020765SAl Viro 
10368d020765SAl Viro 	return ret;
10378d020765SAl Viro }
10388d020765SAl Viro 
10398d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write);
10408d020765SAl Viro 
1041b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1042b2858d7dSMiklos Szeredi 			  struct splice_desc *sd)
10430b0a47f5SMiklos Szeredi {
1044b2858d7dSMiklos Szeredi 	int ret;
1045b2858d7dSMiklos Szeredi 	void *data;
104606ae43f3SAl Viro 	loff_t tmp = sd->pos;
1047b2858d7dSMiklos Szeredi 
1048fbb32750SAl Viro 	data = kmap(buf->page);
104906ae43f3SAl Viro 	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
1050fbb32750SAl Viro 	kunmap(buf->page);
1051b2858d7dSMiklos Szeredi 
1052b2858d7dSMiklos Szeredi 	return ret;
10530b0a47f5SMiklos Szeredi }
10540b0a47f5SMiklos Szeredi 
10550b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
10560b0a47f5SMiklos Szeredi 					 struct file *out, loff_t *ppos,
10570b0a47f5SMiklos Szeredi 					 size_t len, unsigned int flags)
10580b0a47f5SMiklos Szeredi {
1059b2858d7dSMiklos Szeredi 	ssize_t ret;
10600b0a47f5SMiklos Szeredi 
1061b2858d7dSMiklos Szeredi 	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1062b2858d7dSMiklos Szeredi 	if (ret > 0)
1063b2858d7dSMiklos Szeredi 		*ppos += ret;
10640b0a47f5SMiklos Szeredi 
1065b2858d7dSMiklos Szeredi 	return ret;
10660b0a47f5SMiklos Szeredi }
10670b0a47f5SMiklos Szeredi 
106883f9135bSJens Axboe /**
106983f9135bSJens Axboe  * generic_splice_sendpage - splice data from a pipe to a socket
1070932cc6d4SJens Axboe  * @pipe:	pipe to splice from
107183f9135bSJens Axboe  * @out:	socket to write to
1072932cc6d4SJens Axboe  * @ppos:	position in @out
107383f9135bSJens Axboe  * @len:	number of bytes to splice
107483f9135bSJens Axboe  * @flags:	splice modifier flags
107583f9135bSJens Axboe  *
1076932cc6d4SJens Axboe  * Description:
107783f9135bSJens Axboe  *    Will send @len bytes from the pipe to a network socket. No data copying
107883f9135bSJens Axboe  *    is involved.
107983f9135bSJens Axboe  *
108083f9135bSJens Axboe  */
10813a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
1082cbb7e577SJens Axboe 				loff_t *ppos, size_t len, unsigned int flags)
10835274f052SJens Axboe {
108400522fb4SJens Axboe 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
10855274f052SJens Axboe }
10865274f052SJens Axboe 
1087059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage);
1088a0f06780SJeff Garzik 
108983f9135bSJens Axboe /*
109083f9135bSJens Axboe  * Attempt to initiate a splice from pipe to file.
109183f9135bSJens Axboe  */
10923a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1093cbb7e577SJens Axboe 			   loff_t *ppos, size_t len, unsigned int flags)
10945274f052SJens Axboe {
10950b0a47f5SMiklos Szeredi 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
10960b0a47f5SMiklos Szeredi 				loff_t *, size_t, unsigned int);
10975274f052SJens Axboe 
109872c2d531SAl Viro 	if (out->f_op->splice_write)
10990b0a47f5SMiklos Szeredi 		splice_write = out->f_op->splice_write;
1100cc56f7deSChangli Gao 	else
11010b0a47f5SMiklos Szeredi 		splice_write = default_file_splice_write;
11020b0a47f5SMiklos Szeredi 
1103500368f7SAl Viro 	return splice_write(pipe, out, ppos, len, flags);
11045274f052SJens Axboe }
11055274f052SJens Axboe 
110683f9135bSJens Axboe /*
110783f9135bSJens Axboe  * Attempt to initiate a splice from a file to a pipe.
110883f9135bSJens Axboe  */
1109cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos,
1110cbb7e577SJens Axboe 			 struct pipe_inode_info *pipe, size_t len,
1111cbb7e577SJens Axboe 			 unsigned int flags)
11125274f052SJens Axboe {
11136818173bSMiklos Szeredi 	ssize_t (*splice_read)(struct file *, loff_t *,
11146818173bSMiklos Szeredi 			       struct pipe_inode_info *, size_t, unsigned int);
11155274f052SJens Axboe 	int ret;
11165274f052SJens Axboe 
111749570e9bSJens Axboe 	if (unlikely(!(in->f_mode & FMODE_READ)))
11185274f052SJens Axboe 		return -EBADF;
11195274f052SJens Axboe 
1120cbb7e577SJens Axboe 	ret = rw_verify_area(READ, in, ppos, len);
11215274f052SJens Axboe 	if (unlikely(ret < 0))
11225274f052SJens Axboe 		return ret;
11235274f052SJens Axboe 
112403cc0789SAl Viro 	if (unlikely(len > MAX_RW_COUNT))
112503cc0789SAl Viro 		len = MAX_RW_COUNT;
112603cc0789SAl Viro 
112772c2d531SAl Viro 	if (in->f_op->splice_read)
11286818173bSMiklos Szeredi 		splice_read = in->f_op->splice_read;
1129cc56f7deSChangli Gao 	else
11306818173bSMiklos Szeredi 		splice_read = default_file_splice_read;
11316818173bSMiklos Szeredi 
11326818173bSMiklos Szeredi 	return splice_read(in, ppos, pipe, len, flags);
11335274f052SJens Axboe }
11345274f052SJens Axboe 
1135932cc6d4SJens Axboe /**
1136932cc6d4SJens Axboe  * splice_direct_to_actor - splices data directly between two non-pipes
1137932cc6d4SJens Axboe  * @in:		file to splice from
1138932cc6d4SJens Axboe  * @sd:		actor information on where to splice to
1139932cc6d4SJens Axboe  * @actor:	handles the data splicing
1140932cc6d4SJens Axboe  *
1141932cc6d4SJens Axboe  * Description:
1142932cc6d4SJens Axboe  *    This is a special case helper to splice directly between two
1143932cc6d4SJens Axboe  *    points, without requiring an explicit pipe. Internally an allocated
1144932cc6d4SJens Axboe  *    pipe is cached in the process, and reused during the lifetime of
1145932cc6d4SJens Axboe  *    that process.
1146932cc6d4SJens Axboe  *
1147c66ab6faSJens Axboe  */
1148c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1149c66ab6faSJens Axboe 			       splice_direct_actor *actor)
1150b92ce558SJens Axboe {
1151b92ce558SJens Axboe 	struct pipe_inode_info *pipe;
1152b92ce558SJens Axboe 	long ret, bytes;
1153b92ce558SJens Axboe 	umode_t i_mode;
1154c66ab6faSJens Axboe 	size_t len;
11550ff28d9fSChristophe Leroy 	int i, flags, more;
1156b92ce558SJens Axboe 
1157b92ce558SJens Axboe 	/*
1158b92ce558SJens Axboe 	 * We require the input being a regular file, as we don't want to
1159b92ce558SJens Axboe 	 * randomly drop data for eg socket -> socket splicing. Use the
1160b92ce558SJens Axboe 	 * piped splicing for that!
1161b92ce558SJens Axboe 	 */
1162496ad9aaSAl Viro 	i_mode = file_inode(in)->i_mode;
1163b92ce558SJens Axboe 	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1164b92ce558SJens Axboe 		return -EINVAL;
1165b92ce558SJens Axboe 
1166b92ce558SJens Axboe 	/*
1167b92ce558SJens Axboe 	 * neither in nor out is a pipe, setup an internal pipe attached to
1168b92ce558SJens Axboe 	 * 'out' and transfer the wanted data from 'in' to 'out' through that
1169b92ce558SJens Axboe 	 */
1170b92ce558SJens Axboe 	pipe = current->splice_pipe;
117149570e9bSJens Axboe 	if (unlikely(!pipe)) {
11727bee130eSAl Viro 		pipe = alloc_pipe_info();
1173b92ce558SJens Axboe 		if (!pipe)
1174b92ce558SJens Axboe 			return -ENOMEM;
1175b92ce558SJens Axboe 
1176b92ce558SJens Axboe 		/*
1177b92ce558SJens Axboe 		 * We don't have an immediate reader, but we'll read the stuff
117800522fb4SJens Axboe 		 * out of the pipe right after the splice_to_pipe(). So set
1179b92ce558SJens Axboe 		 * PIPE_READERS appropriately.
1180b92ce558SJens Axboe 		 */
1181b92ce558SJens Axboe 		pipe->readers = 1;
1182b92ce558SJens Axboe 
1183b92ce558SJens Axboe 		current->splice_pipe = pipe;
1184b92ce558SJens Axboe 	}
1185b92ce558SJens Axboe 
1186b92ce558SJens Axboe 	/*
118773d62d83SIngo Molnar 	 * Do the splice.
1188b92ce558SJens Axboe 	 */
1189b92ce558SJens Axboe 	ret = 0;
1190b92ce558SJens Axboe 	bytes = 0;
1191c66ab6faSJens Axboe 	len = sd->total_len;
1192c66ab6faSJens Axboe 	flags = sd->flags;
1193c66ab6faSJens Axboe 
1194c66ab6faSJens Axboe 	/*
1195c66ab6faSJens Axboe 	 * Don't block on output, we have to drain the direct pipe.
1196c66ab6faSJens Axboe 	 */
1197c66ab6faSJens Axboe 	sd->flags &= ~SPLICE_F_NONBLOCK;
11980ff28d9fSChristophe Leroy 	more = sd->flags & SPLICE_F_MORE;
1199b92ce558SJens Axboe 
1200b92ce558SJens Axboe 	while (len) {
120151a92c0fSJens Axboe 		size_t read_len;
1202a82c53a0STom Zanussi 		loff_t pos = sd->pos, prev_pos = pos;
1203b92ce558SJens Axboe 
1204bcd4f3acSJens Axboe 		ret = do_splice_to(in, &pos, pipe, len, flags);
120551a92c0fSJens Axboe 		if (unlikely(ret <= 0))
1206b92ce558SJens Axboe 			goto out_release;
1207b92ce558SJens Axboe 
1208b92ce558SJens Axboe 		read_len = ret;
1209c66ab6faSJens Axboe 		sd->total_len = read_len;
1210b92ce558SJens Axboe 
1211b92ce558SJens Axboe 		/*
12120ff28d9fSChristophe Leroy 		 * If more data is pending, set SPLICE_F_MORE
12130ff28d9fSChristophe Leroy 		 * If this is the last data and SPLICE_F_MORE was not set
12140ff28d9fSChristophe Leroy 		 * initially, clears it.
12150ff28d9fSChristophe Leroy 		 */
12160ff28d9fSChristophe Leroy 		if (read_len < len)
12170ff28d9fSChristophe Leroy 			sd->flags |= SPLICE_F_MORE;
12180ff28d9fSChristophe Leroy 		else if (!more)
12190ff28d9fSChristophe Leroy 			sd->flags &= ~SPLICE_F_MORE;
12200ff28d9fSChristophe Leroy 		/*
1221b92ce558SJens Axboe 		 * NOTE: nonblocking mode only applies to the input. We
1222b92ce558SJens Axboe 		 * must not do the output in nonblocking mode as then we
1223b92ce558SJens Axboe 		 * could get stuck data in the internal pipe:
1224b92ce558SJens Axboe 		 */
1225c66ab6faSJens Axboe 		ret = actor(pipe, sd);
1226a82c53a0STom Zanussi 		if (unlikely(ret <= 0)) {
1227a82c53a0STom Zanussi 			sd->pos = prev_pos;
1228b92ce558SJens Axboe 			goto out_release;
1229a82c53a0STom Zanussi 		}
1230b92ce558SJens Axboe 
1231b92ce558SJens Axboe 		bytes += ret;
1232b92ce558SJens Axboe 		len -= ret;
1233bcd4f3acSJens Axboe 		sd->pos = pos;
1234b92ce558SJens Axboe 
1235a82c53a0STom Zanussi 		if (ret < read_len) {
1236a82c53a0STom Zanussi 			sd->pos = prev_pos + ret;
123751a92c0fSJens Axboe 			goto out_release;
1238b92ce558SJens Axboe 		}
1239a82c53a0STom Zanussi 	}
1240b92ce558SJens Axboe 
12419e97198dSJens Axboe done:
1242b92ce558SJens Axboe 	pipe->nrbufs = pipe->curbuf = 0;
12439e97198dSJens Axboe 	file_accessed(in);
1244b92ce558SJens Axboe 	return bytes;
1245b92ce558SJens Axboe 
1246b92ce558SJens Axboe out_release:
1247b92ce558SJens Axboe 	/*
1248b92ce558SJens Axboe 	 * If we did an incomplete transfer we must release
1249b92ce558SJens Axboe 	 * the pipe buffers in question:
1250b92ce558SJens Axboe 	 */
125135f3d14dSJens Axboe 	for (i = 0; i < pipe->buffers; i++) {
1252b92ce558SJens Axboe 		struct pipe_buffer *buf = pipe->bufs + i;
1253b92ce558SJens Axboe 
1254b92ce558SJens Axboe 		if (buf->ops) {
1255b92ce558SJens Axboe 			buf->ops->release(pipe, buf);
1256b92ce558SJens Axboe 			buf->ops = NULL;
1257b92ce558SJens Axboe 		}
1258b92ce558SJens Axboe 	}
1259b92ce558SJens Axboe 
12609e97198dSJens Axboe 	if (!bytes)
12619e97198dSJens Axboe 		bytes = ret;
1262b92ce558SJens Axboe 
12639e97198dSJens Axboe 	goto done;
1264c66ab6faSJens Axboe }
1265c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor);
1266c66ab6faSJens Axboe 
1267c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe,
1268c66ab6faSJens Axboe 			       struct splice_desc *sd)
1269c66ab6faSJens Axboe {
12706a14b90bSJens Axboe 	struct file *file = sd->u.file;
1271c66ab6faSJens Axboe 
12727995bd28SAl Viro 	return do_splice_from(pipe, file, sd->opos, sd->total_len,
12732cb4b05eSChangli Gao 			      sd->flags);
1274c66ab6faSJens Axboe }
1275c66ab6faSJens Axboe 
1276932cc6d4SJens Axboe /**
1277932cc6d4SJens Axboe  * do_splice_direct - splices data directly between two files
1278932cc6d4SJens Axboe  * @in:		file to splice from
1279932cc6d4SJens Axboe  * @ppos:	input file offset
1280932cc6d4SJens Axboe  * @out:	file to splice to
1281acdb37c3SRandy Dunlap  * @opos:	output file offset
1282932cc6d4SJens Axboe  * @len:	number of bytes to splice
1283932cc6d4SJens Axboe  * @flags:	splice modifier flags
1284932cc6d4SJens Axboe  *
1285932cc6d4SJens Axboe  * Description:
1286932cc6d4SJens Axboe  *    For use by do_sendfile(). splice can easily emulate sendfile, but
1287932cc6d4SJens Axboe  *    doing it in the application would incur an extra system call
1288932cc6d4SJens Axboe  *    (splice in + splice out, as compared to just sendfile()). So this helper
1289932cc6d4SJens Axboe  *    can splice directly through a process-private pipe.
1290932cc6d4SJens Axboe  *
1291932cc6d4SJens Axboe  */
1292c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
12937995bd28SAl Viro 		      loff_t *opos, size_t len, unsigned int flags)
1294c66ab6faSJens Axboe {
1295c66ab6faSJens Axboe 	struct splice_desc sd = {
1296c66ab6faSJens Axboe 		.len		= len,
1297c66ab6faSJens Axboe 		.total_len	= len,
1298c66ab6faSJens Axboe 		.flags		= flags,
1299c66ab6faSJens Axboe 		.pos		= *ppos,
13006a14b90bSJens Axboe 		.u.file		= out,
13017995bd28SAl Viro 		.opos		= opos,
1302c66ab6faSJens Axboe 	};
130351a92c0fSJens Axboe 	long ret;
1304c66ab6faSJens Axboe 
130518c67cb9SAl Viro 	if (unlikely(!(out->f_mode & FMODE_WRITE)))
130618c67cb9SAl Viro 		return -EBADF;
130718c67cb9SAl Viro 
130818c67cb9SAl Viro 	if (unlikely(out->f_flags & O_APPEND))
130918c67cb9SAl Viro 		return -EINVAL;
131018c67cb9SAl Viro 
131118c67cb9SAl Viro 	ret = rw_verify_area(WRITE, out, opos, len);
131218c67cb9SAl Viro 	if (unlikely(ret < 0))
131318c67cb9SAl Viro 		return ret;
131418c67cb9SAl Viro 
1315c66ab6faSJens Axboe 	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
131651a92c0fSJens Axboe 	if (ret > 0)
1317a82c53a0STom Zanussi 		*ppos = sd.pos;
131851a92c0fSJens Axboe 
1319c66ab6faSJens Axboe 	return ret;
1320b92ce558SJens Axboe }
13211c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct);
1322b92ce558SJens Axboe 
13238924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
13248924feffSAl Viro {
13258924feffSAl Viro 	while (pipe->nrbufs == pipe->buffers) {
13268924feffSAl Viro 		if (flags & SPLICE_F_NONBLOCK)
13278924feffSAl Viro 			return -EAGAIN;
13288924feffSAl Viro 		if (signal_pending(current))
13298924feffSAl Viro 			return -ERESTARTSYS;
13308924feffSAl Viro 		pipe->waiting_writers++;
13318924feffSAl Viro 		pipe_wait(pipe);
13328924feffSAl Viro 		pipe->waiting_writers--;
13338924feffSAl Viro 	}
13348924feffSAl Viro 	return 0;
13358924feffSAl Viro }
13368924feffSAl Viro 
13377c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
13387c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
13397c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags);
1340ddac0d39SJens Axboe 
1341ddac0d39SJens Axboe /*
134283f9135bSJens Axboe  * Determine where to splice to/from.
134383f9135bSJens Axboe  */
1344529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in,
1345529565dcSIngo Molnar 		      struct file *out, loff_t __user *off_out,
1346529565dcSIngo Molnar 		      size_t len, unsigned int flags)
13475274f052SJens Axboe {
13487c77f0b3SMiklos Szeredi 	struct pipe_inode_info *ipipe;
13497c77f0b3SMiklos Szeredi 	struct pipe_inode_info *opipe;
13507995bd28SAl Viro 	loff_t offset;
1351a4514ebdSJens Axboe 	long ret;
13525274f052SJens Axboe 
135371993e62SLinus Torvalds 	ipipe = get_pipe_info(in);
135471993e62SLinus Torvalds 	opipe = get_pipe_info(out);
13557c77f0b3SMiklos Szeredi 
13567c77f0b3SMiklos Szeredi 	if (ipipe && opipe) {
13577c77f0b3SMiklos Szeredi 		if (off_in || off_out)
13587c77f0b3SMiklos Szeredi 			return -ESPIPE;
13597c77f0b3SMiklos Szeredi 
13607c77f0b3SMiklos Szeredi 		if (!(in->f_mode & FMODE_READ))
13617c77f0b3SMiklos Szeredi 			return -EBADF;
13627c77f0b3SMiklos Szeredi 
13637c77f0b3SMiklos Szeredi 		if (!(out->f_mode & FMODE_WRITE))
13647c77f0b3SMiklos Szeredi 			return -EBADF;
13657c77f0b3SMiklos Szeredi 
13667c77f0b3SMiklos Szeredi 		/* Splicing to self would be fun, but... */
13677c77f0b3SMiklos Szeredi 		if (ipipe == opipe)
13687c77f0b3SMiklos Szeredi 			return -EINVAL;
13697c77f0b3SMiklos Szeredi 
13707c77f0b3SMiklos Szeredi 		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
13717c77f0b3SMiklos Szeredi 	}
13727c77f0b3SMiklos Szeredi 
13737c77f0b3SMiklos Szeredi 	if (ipipe) {
1374529565dcSIngo Molnar 		if (off_in)
1375529565dcSIngo Molnar 			return -ESPIPE;
1376b92ce558SJens Axboe 		if (off_out) {
137719c9a49bSChangli Gao 			if (!(out->f_mode & FMODE_PWRITE))
1378b92ce558SJens Axboe 				return -EINVAL;
1379cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1380b92ce558SJens Axboe 				return -EFAULT;
13817995bd28SAl Viro 		} else {
13827995bd28SAl Viro 			offset = out->f_pos;
13837995bd28SAl Viro 		}
1384529565dcSIngo Molnar 
138518c67cb9SAl Viro 		if (unlikely(!(out->f_mode & FMODE_WRITE)))
138618c67cb9SAl Viro 			return -EBADF;
138718c67cb9SAl Viro 
138818c67cb9SAl Viro 		if (unlikely(out->f_flags & O_APPEND))
138918c67cb9SAl Viro 			return -EINVAL;
139018c67cb9SAl Viro 
139118c67cb9SAl Viro 		ret = rw_verify_area(WRITE, out, &offset, len);
139218c67cb9SAl Viro 		if (unlikely(ret < 0))
139318c67cb9SAl Viro 			return ret;
139418c67cb9SAl Viro 
1395500368f7SAl Viro 		file_start_write(out);
13967995bd28SAl Viro 		ret = do_splice_from(ipipe, out, &offset, len, flags);
1397500368f7SAl Viro 		file_end_write(out);
1398a4514ebdSJens Axboe 
13997995bd28SAl Viro 		if (!off_out)
14007995bd28SAl Viro 			out->f_pos = offset;
14017995bd28SAl Viro 		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1402a4514ebdSJens Axboe 			ret = -EFAULT;
1403a4514ebdSJens Axboe 
1404a4514ebdSJens Axboe 		return ret;
1405529565dcSIngo Molnar 	}
14065274f052SJens Axboe 
14077c77f0b3SMiklos Szeredi 	if (opipe) {
1408529565dcSIngo Molnar 		if (off_out)
1409529565dcSIngo Molnar 			return -ESPIPE;
1410b92ce558SJens Axboe 		if (off_in) {
141119c9a49bSChangli Gao 			if (!(in->f_mode & FMODE_PREAD))
1412b92ce558SJens Axboe 				return -EINVAL;
1413cbb7e577SJens Axboe 			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1414b92ce558SJens Axboe 				return -EFAULT;
14157995bd28SAl Viro 		} else {
14167995bd28SAl Viro 			offset = in->f_pos;
14177995bd28SAl Viro 		}
1418529565dcSIngo Molnar 
14198924feffSAl Viro 		pipe_lock(opipe);
14208924feffSAl Viro 		ret = wait_for_space(opipe, flags);
14218924feffSAl Viro 		if (!ret)
14227995bd28SAl Viro 			ret = do_splice_to(in, &offset, opipe, len, flags);
14238924feffSAl Viro 		pipe_unlock(opipe);
14248924feffSAl Viro 		if (ret > 0)
14258924feffSAl Viro 			wakeup_pipe_readers(opipe);
14267995bd28SAl Viro 		if (!off_in)
14277995bd28SAl Viro 			in->f_pos = offset;
14287995bd28SAl Viro 		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1429a4514ebdSJens Axboe 			ret = -EFAULT;
1430a4514ebdSJens Axboe 
1431a4514ebdSJens Axboe 		return ret;
1432529565dcSIngo Molnar 	}
14335274f052SJens Axboe 
14345274f052SJens Axboe 	return -EINVAL;
14355274f052SJens Axboe }
14365274f052SJens Axboe 
143779fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from,
143879fddc4eSAl Viro 			struct pipe_inode_info *pipe,
143979fddc4eSAl Viro 			unsigned flags)
1440912d35f8SJens Axboe {
144179fddc4eSAl Viro 	struct pipe_buffer buf = {
144279fddc4eSAl Viro 		.ops = &user_page_pipe_buf_ops,
144379fddc4eSAl Viro 		.flags = flags
144479fddc4eSAl Viro 	};
144579fddc4eSAl Viro 	size_t total = 0;
144679fddc4eSAl Viro 	int ret = 0;
144779fddc4eSAl Viro 	bool failed = false;
144879fddc4eSAl Viro 
144979fddc4eSAl Viro 	while (iov_iter_count(from) && !failed) {
145079fddc4eSAl Viro 		struct page *pages[16];
1451db85a9ebSAl Viro 		ssize_t copied;
1452db85a9ebSAl Viro 		size_t start;
145379fddc4eSAl Viro 		int n;
1454912d35f8SJens Axboe 
145579fddc4eSAl Viro 		copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
145679fddc4eSAl Viro 		if (copied <= 0) {
145779fddc4eSAl Viro 			ret = copied;
145879fddc4eSAl Viro 			break;
145979fddc4eSAl Viro 		}
1460912d35f8SJens Axboe 
146179fddc4eSAl Viro 		for (n = 0; copied; n++, start = 0) {
1462db85a9ebSAl Viro 			int size = min_t(int, copied, PAGE_SIZE - start);
146379fddc4eSAl Viro 			if (!failed) {
146479fddc4eSAl Viro 				buf.page = pages[n];
146579fddc4eSAl Viro 				buf.offset = start;
146679fddc4eSAl Viro 				buf.len = size;
146779fddc4eSAl Viro 				ret = add_to_pipe(pipe, &buf);
146879fddc4eSAl Viro 				if (unlikely(ret < 0)) {
146979fddc4eSAl Viro 					failed = true;
147079fddc4eSAl Viro 				} else {
147179fddc4eSAl Viro 					iov_iter_advance(from, ret);
147279fddc4eSAl Viro 					total += ret;
147379fddc4eSAl Viro 				}
147479fddc4eSAl Viro 			} else {
147579fddc4eSAl Viro 				put_page(pages[n]);
147679fddc4eSAl Viro 			}
1477db85a9ebSAl Viro 			copied -= size;
1478912d35f8SJens Axboe 		}
1479912d35f8SJens Axboe 	}
148079fddc4eSAl Viro 	return total ? total : ret;
1481912d35f8SJens Axboe }
1482912d35f8SJens Axboe 
14836a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
14846a14b90bSJens Axboe 			struct splice_desc *sd)
14856a14b90bSJens Axboe {
14866130f531SAl Viro 	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
14876130f531SAl Viro 	return n == sd->len ? n : -EFAULT;
14886a14b90bSJens Axboe }
14896a14b90bSJens Axboe 
14906a14b90bSJens Axboe /*
14916a14b90bSJens Axboe  * For lack of a better implementation, implement vmsplice() to userspace
14926a14b90bSJens Axboe  * as a simple copy of the pipes pages to the user iov.
14936a14b90bSJens Axboe  */
14946130f531SAl Viro static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
14956a14b90bSJens Axboe 			     unsigned long nr_segs, unsigned int flags)
14966a14b90bSJens Axboe {
14976a14b90bSJens Axboe 	struct pipe_inode_info *pipe;
14986a14b90bSJens Axboe 	struct splice_desc sd;
14996a14b90bSJens Axboe 	long ret;
15006130f531SAl Viro 	struct iovec iovstack[UIO_FASTIOV];
15016130f531SAl Viro 	struct iovec *iov = iovstack;
15026130f531SAl Viro 	struct iov_iter iter;
15036a14b90bSJens Axboe 
150471993e62SLinus Torvalds 	pipe = get_pipe_info(file);
15056a14b90bSJens Axboe 	if (!pipe)
15066a14b90bSJens Axboe 		return -EBADF;
15076a14b90bSJens Axboe 
1508345995faSAl Viro 	ret = import_iovec(READ, uiov, nr_segs,
1509345995faSAl Viro 			   ARRAY_SIZE(iovstack), &iov, &iter);
1510345995faSAl Viro 	if (ret < 0)
1511345995faSAl Viro 		return ret;
15126a14b90bSJens Axboe 
1513345995faSAl Viro 	sd.total_len = iov_iter_count(&iter);
15146a14b90bSJens Axboe 	sd.len = 0;
15156a14b90bSJens Axboe 	sd.flags = flags;
15166130f531SAl Viro 	sd.u.data = &iter;
15176a14b90bSJens Axboe 	sd.pos = 0;
15186a14b90bSJens Axboe 
1519345995faSAl Viro 	if (sd.total_len) {
15206130f531SAl Viro 		pipe_lock(pipe);
15216130f531SAl Viro 		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
152261e0d47cSMiklos Szeredi 		pipe_unlock(pipe);
1523345995faSAl Viro 	}
15246a14b90bSJens Axboe 
15256130f531SAl Viro 	kfree(iov);
15266a14b90bSJens Axboe 	return ret;
15276a14b90bSJens Axboe }
15286a14b90bSJens Axboe 
1529912d35f8SJens Axboe /*
1530912d35f8SJens Axboe  * vmsplice splices a user address range into a pipe. It can be thought of
1531912d35f8SJens Axboe  * as splice-from-memory, where the regular splice is splice-from-file (or
1532912d35f8SJens Axboe  * to file). In both cases the output is a pipe, naturally.
1533912d35f8SJens Axboe  */
1534db85a9ebSAl Viro static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1535912d35f8SJens Axboe 			     unsigned long nr_segs, unsigned int flags)
1536912d35f8SJens Axboe {
1537ddac0d39SJens Axboe 	struct pipe_inode_info *pipe;
1538db85a9ebSAl Viro 	struct iovec iovstack[UIO_FASTIOV];
1539db85a9ebSAl Viro 	struct iovec *iov = iovstack;
1540db85a9ebSAl Viro 	struct iov_iter from;
154135f3d14dSJens Axboe 	long ret;
154279fddc4eSAl Viro 	unsigned buf_flag = 0;
154379fddc4eSAl Viro 
154479fddc4eSAl Viro 	if (flags & SPLICE_F_GIFT)
154579fddc4eSAl Viro 		buf_flag = PIPE_BUF_FLAG_GIFT;
1546912d35f8SJens Axboe 
154771993e62SLinus Torvalds 	pipe = get_pipe_info(file);
1548ddac0d39SJens Axboe 	if (!pipe)
1549912d35f8SJens Axboe 		return -EBADF;
1550912d35f8SJens Axboe 
1551db85a9ebSAl Viro 	ret = import_iovec(WRITE, uiov, nr_segs,
1552db85a9ebSAl Viro 			   ARRAY_SIZE(iovstack), &iov, &from);
1553db85a9ebSAl Viro 	if (ret < 0)
1554db85a9ebSAl Viro 		return ret;
1555912d35f8SJens Axboe 
15568924feffSAl Viro 	pipe_lock(pipe);
15578924feffSAl Viro 	ret = wait_for_space(pipe, flags);
155879fddc4eSAl Viro 	if (!ret)
155979fddc4eSAl Viro 		ret = iter_to_pipe(&from, pipe, buf_flag);
15608924feffSAl Viro 	pipe_unlock(pipe);
15618924feffSAl Viro 	if (ret > 0)
15628924feffSAl Viro 		wakeup_pipe_readers(pipe);
1563db85a9ebSAl Viro 	kfree(iov);
156435f3d14dSJens Axboe 	return ret;
1565912d35f8SJens Axboe }
1566912d35f8SJens Axboe 
15676a14b90bSJens Axboe /*
15686a14b90bSJens Axboe  * Note that vmsplice only really supports true splicing _from_ user memory
15696a14b90bSJens Axboe  * to a pipe, not the other way around. Splicing from user memory is a simple
15706a14b90bSJens Axboe  * operation that can be supported without any funky alignment restrictions
15716a14b90bSJens Axboe  * or nasty vm tricks. We simply map in the user memory and fill them into
15726a14b90bSJens Axboe  * a pipe. The reverse isn't quite as easy, though. There are two possible
15736a14b90bSJens Axboe  * solutions for that:
15746a14b90bSJens Axboe  *
15756a14b90bSJens Axboe  *	- memcpy() the data internally, at which point we might as well just
15766a14b90bSJens Axboe  *	  do a regular read() on the buffer anyway.
15776a14b90bSJens Axboe  *	- Lots of nasty vm tricks, that are neither fast nor flexible (it
15786a14b90bSJens Axboe  *	  has restriction limitations on both ends of the pipe).
15796a14b90bSJens Axboe  *
15806a14b90bSJens Axboe  * Currently we punt and implement it as a normal copy, see pipe_to_user().
15816a14b90bSJens Axboe  *
15826a14b90bSJens Axboe  */
1583836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1584836f92adSHeiko Carstens 		unsigned long, nr_segs, unsigned int, flags)
1585912d35f8SJens Axboe {
15862903ff01SAl Viro 	struct fd f;
1587912d35f8SJens Axboe 	long error;
1588912d35f8SJens Axboe 
15896a14b90bSJens Axboe 	if (unlikely(nr_segs > UIO_MAXIOV))
15906a14b90bSJens Axboe 		return -EINVAL;
15916a14b90bSJens Axboe 	else if (unlikely(!nr_segs))
15926a14b90bSJens Axboe 		return 0;
15936a14b90bSJens Axboe 
1594912d35f8SJens Axboe 	error = -EBADF;
15952903ff01SAl Viro 	f = fdget(fd);
15962903ff01SAl Viro 	if (f.file) {
15972903ff01SAl Viro 		if (f.file->f_mode & FMODE_WRITE)
15982903ff01SAl Viro 			error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
15992903ff01SAl Viro 		else if (f.file->f_mode & FMODE_READ)
16002903ff01SAl Viro 			error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1601912d35f8SJens Axboe 
16022903ff01SAl Viro 		fdput(f);
1603912d35f8SJens Axboe 	}
1604912d35f8SJens Axboe 
1605912d35f8SJens Axboe 	return error;
1606912d35f8SJens Axboe }
1607912d35f8SJens Axboe 
160876b021d0SAl Viro #ifdef CONFIG_COMPAT
160976b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
161076b021d0SAl Viro 		    unsigned int, nr_segs, unsigned int, flags)
161176b021d0SAl Viro {
161276b021d0SAl Viro 	unsigned i;
161376b021d0SAl Viro 	struct iovec __user *iov;
161476b021d0SAl Viro 	if (nr_segs > UIO_MAXIOV)
161576b021d0SAl Viro 		return -EINVAL;
161676b021d0SAl Viro 	iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
161776b021d0SAl Viro 	for (i = 0; i < nr_segs; i++) {
161876b021d0SAl Viro 		struct compat_iovec v;
161976b021d0SAl Viro 		if (get_user(v.iov_base, &iov32[i].iov_base) ||
162076b021d0SAl Viro 		    get_user(v.iov_len, &iov32[i].iov_len) ||
162176b021d0SAl Viro 		    put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
162276b021d0SAl Viro 		    put_user(v.iov_len, &iov[i].iov_len))
162376b021d0SAl Viro 			return -EFAULT;
162476b021d0SAl Viro 	}
162576b021d0SAl Viro 	return sys_vmsplice(fd, iov, nr_segs, flags);
162676b021d0SAl Viro }
162776b021d0SAl Viro #endif
162876b021d0SAl Viro 
1629836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1630836f92adSHeiko Carstens 		int, fd_out, loff_t __user *, off_out,
1631836f92adSHeiko Carstens 		size_t, len, unsigned int, flags)
16325274f052SJens Axboe {
16332903ff01SAl Viro 	struct fd in, out;
16345274f052SJens Axboe 	long error;
16355274f052SJens Axboe 
16365274f052SJens Axboe 	if (unlikely(!len))
16375274f052SJens Axboe 		return 0;
16385274f052SJens Axboe 
16395274f052SJens Axboe 	error = -EBADF;
16402903ff01SAl Viro 	in = fdget(fd_in);
16412903ff01SAl Viro 	if (in.file) {
16422903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
16432903ff01SAl Viro 			out = fdget(fd_out);
16442903ff01SAl Viro 			if (out.file) {
16452903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
16462903ff01SAl Viro 					error = do_splice(in.file, off_in,
16472903ff01SAl Viro 							  out.file, off_out,
1648529565dcSIngo Molnar 							  len, flags);
16492903ff01SAl Viro 				fdput(out);
16505274f052SJens Axboe 			}
16515274f052SJens Axboe 		}
16522903ff01SAl Viro 		fdput(in);
16535274f052SJens Axboe 	}
16545274f052SJens Axboe 	return error;
16555274f052SJens Axboe }
165670524490SJens Axboe 
165770524490SJens Axboe /*
1658aadd06e5SJens Axboe  * Make sure there's data to read. Wait for input if we can, otherwise
1659aadd06e5SJens Axboe  * return an appropriate error.
1660aadd06e5SJens Axboe  */
16617c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1662aadd06e5SJens Axboe {
1663aadd06e5SJens Axboe 	int ret;
1664aadd06e5SJens Axboe 
1665aadd06e5SJens Axboe 	/*
1666aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1667aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1668aadd06e5SJens Axboe 	 */
1669aadd06e5SJens Axboe 	if (pipe->nrbufs)
1670aadd06e5SJens Axboe 		return 0;
1671aadd06e5SJens Axboe 
1672aadd06e5SJens Axboe 	ret = 0;
167361e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1674aadd06e5SJens Axboe 
1675aadd06e5SJens Axboe 	while (!pipe->nrbufs) {
1676aadd06e5SJens Axboe 		if (signal_pending(current)) {
1677aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1678aadd06e5SJens Axboe 			break;
1679aadd06e5SJens Axboe 		}
1680aadd06e5SJens Axboe 		if (!pipe->writers)
1681aadd06e5SJens Axboe 			break;
1682aadd06e5SJens Axboe 		if (!pipe->waiting_writers) {
1683aadd06e5SJens Axboe 			if (flags & SPLICE_F_NONBLOCK) {
1684aadd06e5SJens Axboe 				ret = -EAGAIN;
1685aadd06e5SJens Axboe 				break;
1686aadd06e5SJens Axboe 			}
1687aadd06e5SJens Axboe 		}
1688aadd06e5SJens Axboe 		pipe_wait(pipe);
1689aadd06e5SJens Axboe 	}
1690aadd06e5SJens Axboe 
169161e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1692aadd06e5SJens Axboe 	return ret;
1693aadd06e5SJens Axboe }
1694aadd06e5SJens Axboe 
1695aadd06e5SJens Axboe /*
1696aadd06e5SJens Axboe  * Make sure there's writeable room. Wait for room if we can, otherwise
1697aadd06e5SJens Axboe  * return an appropriate error.
1698aadd06e5SJens Axboe  */
16997c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1700aadd06e5SJens Axboe {
1701aadd06e5SJens Axboe 	int ret;
1702aadd06e5SJens Axboe 
1703aadd06e5SJens Axboe 	/*
1704aadd06e5SJens Axboe 	 * Check ->nrbufs without the inode lock first. This function
1705aadd06e5SJens Axboe 	 * is speculative anyways, so missing one is ok.
1706aadd06e5SJens Axboe 	 */
170735f3d14dSJens Axboe 	if (pipe->nrbufs < pipe->buffers)
1708aadd06e5SJens Axboe 		return 0;
1709aadd06e5SJens Axboe 
1710aadd06e5SJens Axboe 	ret = 0;
171161e0d47cSMiklos Szeredi 	pipe_lock(pipe);
1712aadd06e5SJens Axboe 
171335f3d14dSJens Axboe 	while (pipe->nrbufs >= pipe->buffers) {
1714aadd06e5SJens Axboe 		if (!pipe->readers) {
1715aadd06e5SJens Axboe 			send_sig(SIGPIPE, current, 0);
1716aadd06e5SJens Axboe 			ret = -EPIPE;
1717aadd06e5SJens Axboe 			break;
1718aadd06e5SJens Axboe 		}
1719aadd06e5SJens Axboe 		if (flags & SPLICE_F_NONBLOCK) {
1720aadd06e5SJens Axboe 			ret = -EAGAIN;
1721aadd06e5SJens Axboe 			break;
1722aadd06e5SJens Axboe 		}
1723aadd06e5SJens Axboe 		if (signal_pending(current)) {
1724aadd06e5SJens Axboe 			ret = -ERESTARTSYS;
1725aadd06e5SJens Axboe 			break;
1726aadd06e5SJens Axboe 		}
1727aadd06e5SJens Axboe 		pipe->waiting_writers++;
1728aadd06e5SJens Axboe 		pipe_wait(pipe);
1729aadd06e5SJens Axboe 		pipe->waiting_writers--;
1730aadd06e5SJens Axboe 	}
1731aadd06e5SJens Axboe 
173261e0d47cSMiklos Szeredi 	pipe_unlock(pipe);
1733aadd06e5SJens Axboe 	return ret;
1734aadd06e5SJens Axboe }
1735aadd06e5SJens Axboe 
1736aadd06e5SJens Axboe /*
17377c77f0b3SMiklos Szeredi  * Splice contents of ipipe to opipe.
17387c77f0b3SMiklos Szeredi  */
17397c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
17407c77f0b3SMiklos Szeredi 			       struct pipe_inode_info *opipe,
17417c77f0b3SMiklos Szeredi 			       size_t len, unsigned int flags)
17427c77f0b3SMiklos Szeredi {
17437c77f0b3SMiklos Szeredi 	struct pipe_buffer *ibuf, *obuf;
17447c77f0b3SMiklos Szeredi 	int ret = 0, nbuf;
17457c77f0b3SMiklos Szeredi 	bool input_wakeup = false;
17467c77f0b3SMiklos Szeredi 
17477c77f0b3SMiklos Szeredi 
17487c77f0b3SMiklos Szeredi retry:
17497c77f0b3SMiklos Szeredi 	ret = ipipe_prep(ipipe, flags);
17507c77f0b3SMiklos Szeredi 	if (ret)
17517c77f0b3SMiklos Szeredi 		return ret;
17527c77f0b3SMiklos Szeredi 
17537c77f0b3SMiklos Szeredi 	ret = opipe_prep(opipe, flags);
17547c77f0b3SMiklos Szeredi 	if (ret)
17557c77f0b3SMiklos Szeredi 		return ret;
17567c77f0b3SMiklos Szeredi 
17577c77f0b3SMiklos Szeredi 	/*
17587c77f0b3SMiklos Szeredi 	 * Potential ABBA deadlock, work around it by ordering lock
17597c77f0b3SMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
17607c77f0b3SMiklos Szeredi 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
17617c77f0b3SMiklos Szeredi 	 */
17627c77f0b3SMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
17637c77f0b3SMiklos Szeredi 
17647c77f0b3SMiklos Szeredi 	do {
17657c77f0b3SMiklos Szeredi 		if (!opipe->readers) {
17667c77f0b3SMiklos Szeredi 			send_sig(SIGPIPE, current, 0);
17677c77f0b3SMiklos Szeredi 			if (!ret)
17687c77f0b3SMiklos Szeredi 				ret = -EPIPE;
17697c77f0b3SMiklos Szeredi 			break;
17707c77f0b3SMiklos Szeredi 		}
17717c77f0b3SMiklos Szeredi 
17727c77f0b3SMiklos Szeredi 		if (!ipipe->nrbufs && !ipipe->writers)
17737c77f0b3SMiklos Szeredi 			break;
17747c77f0b3SMiklos Szeredi 
17757c77f0b3SMiklos Szeredi 		/*
17767c77f0b3SMiklos Szeredi 		 * Cannot make any progress, because either the input
17777c77f0b3SMiklos Szeredi 		 * pipe is empty or the output pipe is full.
17787c77f0b3SMiklos Szeredi 		 */
177935f3d14dSJens Axboe 		if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
17807c77f0b3SMiklos Szeredi 			/* Already processed some buffers, break */
17817c77f0b3SMiklos Szeredi 			if (ret)
17827c77f0b3SMiklos Szeredi 				break;
17837c77f0b3SMiklos Szeredi 
17847c77f0b3SMiklos Szeredi 			if (flags & SPLICE_F_NONBLOCK) {
17857c77f0b3SMiklos Szeredi 				ret = -EAGAIN;
17867c77f0b3SMiklos Szeredi 				break;
17877c77f0b3SMiklos Szeredi 			}
17887c77f0b3SMiklos Szeredi 
17897c77f0b3SMiklos Szeredi 			/*
17907c77f0b3SMiklos Szeredi 			 * We raced with another reader/writer and haven't
17917c77f0b3SMiklos Szeredi 			 * managed to process any buffers.  A zero return
17927c77f0b3SMiklos Szeredi 			 * value means EOF, so retry instead.
17937c77f0b3SMiklos Szeredi 			 */
17947c77f0b3SMiklos Szeredi 			pipe_unlock(ipipe);
17957c77f0b3SMiklos Szeredi 			pipe_unlock(opipe);
17967c77f0b3SMiklos Szeredi 			goto retry;
17977c77f0b3SMiklos Szeredi 		}
17987c77f0b3SMiklos Szeredi 
17997c77f0b3SMiklos Szeredi 		ibuf = ipipe->bufs + ipipe->curbuf;
180035f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
18017c77f0b3SMiklos Szeredi 		obuf = opipe->bufs + nbuf;
18027c77f0b3SMiklos Szeredi 
18037c77f0b3SMiklos Szeredi 		if (len >= ibuf->len) {
18047c77f0b3SMiklos Szeredi 			/*
18057c77f0b3SMiklos Szeredi 			 * Simply move the whole buffer from ipipe to opipe
18067c77f0b3SMiklos Szeredi 			 */
18077c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
18087c77f0b3SMiklos Szeredi 			ibuf->ops = NULL;
18097c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
181035f3d14dSJens Axboe 			ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
18117c77f0b3SMiklos Szeredi 			ipipe->nrbufs--;
18127c77f0b3SMiklos Szeredi 			input_wakeup = true;
18137c77f0b3SMiklos Szeredi 		} else {
18147c77f0b3SMiklos Szeredi 			/*
18157c77f0b3SMiklos Szeredi 			 * Get a reference to this pipe buffer,
18167c77f0b3SMiklos Szeredi 			 * so we can copy the contents over.
18177c77f0b3SMiklos Szeredi 			 */
18187c77f0b3SMiklos Szeredi 			ibuf->ops->get(ipipe, ibuf);
18197c77f0b3SMiklos Szeredi 			*obuf = *ibuf;
18207c77f0b3SMiklos Szeredi 
18217c77f0b3SMiklos Szeredi 			/*
18227c77f0b3SMiklos Szeredi 			 * Don't inherit the gift flag, we need to
18237c77f0b3SMiklos Szeredi 			 * prevent multiple steals of this page.
18247c77f0b3SMiklos Szeredi 			 */
18257c77f0b3SMiklos Szeredi 			obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
18267c77f0b3SMiklos Szeredi 
18277c77f0b3SMiklos Szeredi 			obuf->len = len;
18287c77f0b3SMiklos Szeredi 			opipe->nrbufs++;
18297c77f0b3SMiklos Szeredi 			ibuf->offset += obuf->len;
18307c77f0b3SMiklos Szeredi 			ibuf->len -= obuf->len;
18317c77f0b3SMiklos Szeredi 		}
18327c77f0b3SMiklos Szeredi 		ret += obuf->len;
18337c77f0b3SMiklos Szeredi 		len -= obuf->len;
18347c77f0b3SMiklos Szeredi 	} while (len);
18357c77f0b3SMiklos Szeredi 
18367c77f0b3SMiklos Szeredi 	pipe_unlock(ipipe);
18377c77f0b3SMiklos Szeredi 	pipe_unlock(opipe);
18387c77f0b3SMiklos Szeredi 
18397c77f0b3SMiklos Szeredi 	/*
18407c77f0b3SMiklos Szeredi 	 * If we put data in the output pipe, wakeup any potential readers.
18417c77f0b3SMiklos Szeredi 	 */
1842825cdcb1SNamhyung Kim 	if (ret > 0)
1843825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
1844825cdcb1SNamhyung Kim 
18457c77f0b3SMiklos Szeredi 	if (input_wakeup)
18467c77f0b3SMiklos Szeredi 		wakeup_pipe_writers(ipipe);
18477c77f0b3SMiklos Szeredi 
18487c77f0b3SMiklos Szeredi 	return ret;
18497c77f0b3SMiklos Szeredi }
18507c77f0b3SMiklos Szeredi 
18517c77f0b3SMiklos Szeredi /*
185270524490SJens Axboe  * Link contents of ipipe to opipe.
185370524490SJens Axboe  */
185470524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe,
185570524490SJens Axboe 		     struct pipe_inode_info *opipe,
185670524490SJens Axboe 		     size_t len, unsigned int flags)
185770524490SJens Axboe {
185870524490SJens Axboe 	struct pipe_buffer *ibuf, *obuf;
1859aadd06e5SJens Axboe 	int ret = 0, i = 0, nbuf;
186070524490SJens Axboe 
186170524490SJens Axboe 	/*
186270524490SJens Axboe 	 * Potential ABBA deadlock, work around it by ordering lock
186361e0d47cSMiklos Szeredi 	 * grabbing by pipe info address. Otherwise two different processes
186470524490SJens Axboe 	 * could deadlock (one doing tee from A -> B, the other from B -> A).
186570524490SJens Axboe 	 */
186661e0d47cSMiklos Szeredi 	pipe_double_lock(ipipe, opipe);
186770524490SJens Axboe 
1868aadd06e5SJens Axboe 	do {
186970524490SJens Axboe 		if (!opipe->readers) {
187070524490SJens Axboe 			send_sig(SIGPIPE, current, 0);
187170524490SJens Axboe 			if (!ret)
187270524490SJens Axboe 				ret = -EPIPE;
187370524490SJens Axboe 			break;
187470524490SJens Axboe 		}
187570524490SJens Axboe 
187670524490SJens Axboe 		/*
1877aadd06e5SJens Axboe 		 * If we have iterated all input buffers or ran out of
1878aadd06e5SJens Axboe 		 * output room, break.
187970524490SJens Axboe 		 */
188035f3d14dSJens Axboe 		if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1881aadd06e5SJens Axboe 			break;
1882aadd06e5SJens Axboe 
188335f3d14dSJens Axboe 		ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
188435f3d14dSJens Axboe 		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
188570524490SJens Axboe 
188670524490SJens Axboe 		/*
188770524490SJens Axboe 		 * Get a reference to this pipe buffer,
188870524490SJens Axboe 		 * so we can copy the contents over.
188970524490SJens Axboe 		 */
189070524490SJens Axboe 		ibuf->ops->get(ipipe, ibuf);
189170524490SJens Axboe 
189270524490SJens Axboe 		obuf = opipe->bufs + nbuf;
189370524490SJens Axboe 		*obuf = *ibuf;
189470524490SJens Axboe 
18957afa6fd0SJens Axboe 		/*
18967afa6fd0SJens Axboe 		 * Don't inherit the gift flag, we need to
18977afa6fd0SJens Axboe 		 * prevent multiple steals of this page.
18987afa6fd0SJens Axboe 		 */
18997afa6fd0SJens Axboe 		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
19007afa6fd0SJens Axboe 
190170524490SJens Axboe 		if (obuf->len > len)
190270524490SJens Axboe 			obuf->len = len;
190370524490SJens Axboe 
190470524490SJens Axboe 		opipe->nrbufs++;
190570524490SJens Axboe 		ret += obuf->len;
190670524490SJens Axboe 		len -= obuf->len;
1907aadd06e5SJens Axboe 		i++;
1908aadd06e5SJens Axboe 	} while (len);
190970524490SJens Axboe 
191002cf01aeSJens Axboe 	/*
191102cf01aeSJens Axboe 	 * return EAGAIN if we have the potential of some data in the
191202cf01aeSJens Axboe 	 * future, otherwise just return 0
191302cf01aeSJens Axboe 	 */
191402cf01aeSJens Axboe 	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
191502cf01aeSJens Axboe 		ret = -EAGAIN;
191602cf01aeSJens Axboe 
191761e0d47cSMiklos Szeredi 	pipe_unlock(ipipe);
191861e0d47cSMiklos Szeredi 	pipe_unlock(opipe);
191970524490SJens Axboe 
1920aadd06e5SJens Axboe 	/*
1921aadd06e5SJens Axboe 	 * If we put data in the output pipe, wakeup any potential readers.
1922aadd06e5SJens Axboe 	 */
1923825cdcb1SNamhyung Kim 	if (ret > 0)
1924825cdcb1SNamhyung Kim 		wakeup_pipe_readers(opipe);
192570524490SJens Axboe 
192670524490SJens Axboe 	return ret;
192770524490SJens Axboe }
192870524490SJens Axboe 
192970524490SJens Axboe /*
193070524490SJens Axboe  * This is a tee(1) implementation that works on pipes. It doesn't copy
193170524490SJens Axboe  * any data, it simply references the 'in' pages on the 'out' pipe.
193270524490SJens Axboe  * The 'flags' used are the SPLICE_F_* variants, currently the only
193370524490SJens Axboe  * applicable one is SPLICE_F_NONBLOCK.
193470524490SJens Axboe  */
193570524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len,
193670524490SJens Axboe 		   unsigned int flags)
193770524490SJens Axboe {
193871993e62SLinus Torvalds 	struct pipe_inode_info *ipipe = get_pipe_info(in);
193971993e62SLinus Torvalds 	struct pipe_inode_info *opipe = get_pipe_info(out);
1940aadd06e5SJens Axboe 	int ret = -EINVAL;
194170524490SJens Axboe 
194270524490SJens Axboe 	/*
1943aadd06e5SJens Axboe 	 * Duplicate the contents of ipipe to opipe without actually
1944aadd06e5SJens Axboe 	 * copying the data.
194570524490SJens Axboe 	 */
1946aadd06e5SJens Axboe 	if (ipipe && opipe && ipipe != opipe) {
1947aadd06e5SJens Axboe 		/*
1948aadd06e5SJens Axboe 		 * Keep going, unless we encounter an error. The ipipe/opipe
1949aadd06e5SJens Axboe 		 * ordering doesn't really matter.
1950aadd06e5SJens Axboe 		 */
19517c77f0b3SMiklos Szeredi 		ret = ipipe_prep(ipipe, flags);
1952aadd06e5SJens Axboe 		if (!ret) {
19537c77f0b3SMiklos Szeredi 			ret = opipe_prep(opipe, flags);
195402cf01aeSJens Axboe 			if (!ret)
1955aadd06e5SJens Axboe 				ret = link_pipe(ipipe, opipe, len, flags);
1956aadd06e5SJens Axboe 		}
1957aadd06e5SJens Axboe 	}
195870524490SJens Axboe 
1959aadd06e5SJens Axboe 	return ret;
196070524490SJens Axboe }
196170524490SJens Axboe 
1962836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
196370524490SJens Axboe {
19642903ff01SAl Viro 	struct fd in;
19652903ff01SAl Viro 	int error;
196670524490SJens Axboe 
196770524490SJens Axboe 	if (unlikely(!len))
196870524490SJens Axboe 		return 0;
196970524490SJens Axboe 
197070524490SJens Axboe 	error = -EBADF;
19712903ff01SAl Viro 	in = fdget(fdin);
19722903ff01SAl Viro 	if (in.file) {
19732903ff01SAl Viro 		if (in.file->f_mode & FMODE_READ) {
19742903ff01SAl Viro 			struct fd out = fdget(fdout);
19752903ff01SAl Viro 			if (out.file) {
19762903ff01SAl Viro 				if (out.file->f_mode & FMODE_WRITE)
19772903ff01SAl Viro 					error = do_tee(in.file, out.file,
19782903ff01SAl Viro 							len, flags);
19792903ff01SAl Viro 				fdput(out);
198070524490SJens Axboe 			}
198170524490SJens Axboe 		}
19822903ff01SAl Viro  		fdput(in);
198370524490SJens Axboe  	}
198470524490SJens Axboe 
198570524490SJens Axboe 	return error;
198670524490SJens Axboe }
1987