1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 25274f052SJens Axboe /* 35274f052SJens Axboe * "splice": joining two ropes together by interweaving their strands. 45274f052SJens Axboe * 55274f052SJens Axboe * This is the "extended pipe" functionality, where a pipe is used as 65274f052SJens Axboe * an arbitrary in-memory buffer. Think of a pipe as a small kernel 75274f052SJens Axboe * buffer that you can use to transfer data from one end to the other. 85274f052SJens Axboe * 95274f052SJens Axboe * The traditional unix read/write is extended with a "splice()" operation 105274f052SJens Axboe * that transfers data buffers to or from a pipe buffer. 115274f052SJens Axboe * 125274f052SJens Axboe * Named by Larry McVoy, original implementation from Linus, extended by 13c2058e06SJens Axboe * Jens to support splicing to files, network, direct splicing, etc and 14c2058e06SJens Axboe * fixing lots of bugs. 155274f052SJens Axboe * 160fe23479SJens Axboe * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> 17c2058e06SJens Axboe * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> 18c2058e06SJens Axboe * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> 195274f052SJens Axboe * 205274f052SJens Axboe */ 21be297968SChristoph Hellwig #include <linux/bvec.h> 225274f052SJens Axboe #include <linux/fs.h> 235274f052SJens Axboe #include <linux/file.h> 245274f052SJens Axboe #include <linux/pagemap.h> 25d6b29d7cSJens Axboe #include <linux/splice.h> 2608e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h> 275274f052SJens Axboe #include <linux/mm_inline.h> 285abc97aaSJens Axboe #include <linux/swap.h> 294f6f0bd2SJens Axboe #include <linux/writeback.h> 30630d9c47SPaul Gortmaker #include <linux/export.h> 314f6f0bd2SJens Axboe #include <linux/syscalls.h> 32912d35f8SJens Axboe #include <linux/uio.h> 3329ce2058SJames Morris #include <linux/security.h> 345a0e3ad6STejun Heo #include <linux/gfp.h> 3535f9c09fSEric Dumazet #include <linux/socket.h> 3676b021d0SAl Viro #include <linux/compat.h> 37174cd4b1SIngo Molnar #include <linux/sched/signal.h> 38174cd4b1SIngo Molnar 3906ae43f3SAl Viro #include "internal.h" 405274f052SJens Axboe 4183f9135bSJens Axboe /* 4283f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 4383f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 4483f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 4583f9135bSJens Axboe * attempt to reuse this page for another destination. 4683f9135bSJens Axboe */ 4776ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, 485abc97aaSJens Axboe struct pipe_buffer *buf) 495abc97aaSJens Axboe { 505abc97aaSJens Axboe struct page *page = buf->page; 519e94cd4fSJens Axboe struct address_space *mapping; 525abc97aaSJens Axboe 539e0267c2SJens Axboe lock_page(page); 549e0267c2SJens Axboe 559e94cd4fSJens Axboe mapping = page_mapping(page); 569e94cd4fSJens Axboe if (mapping) { 575abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 585abc97aaSJens Axboe 59ad8d6f0aSJens Axboe /* 609e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 619e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 629e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 639e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 649e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 659e94cd4fSJens Axboe * ensues. 66ad8d6f0aSJens Axboe */ 67ad8d6f0aSJens Axboe wait_on_page_writeback(page); 68ad8d6f0aSJens Axboe 69266cf658SDavid Howells if (page_has_private(page) && 70266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 71ca39d651SJens Axboe goto out_unlock; 724f6f0bd2SJens Axboe 739e94cd4fSJens Axboe /* 749e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 759e94cd4fSJens Axboe * and return good. 769e94cd4fSJens Axboe */ 779e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 781432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 795abc97aaSJens Axboe return 0; 805abc97aaSJens Axboe } 819e94cd4fSJens Axboe } 829e94cd4fSJens Axboe 839e94cd4fSJens Axboe /* 849e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 859e94cd4fSJens Axboe * address space, unlock and return failure. 869e94cd4fSJens Axboe */ 87ca39d651SJens Axboe out_unlock: 889e94cd4fSJens Axboe unlock_page(page); 899e94cd4fSJens Axboe return 1; 909e94cd4fSJens Axboe } 915abc97aaSJens Axboe 9276ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 935274f052SJens Axboe struct pipe_buffer *buf) 945274f052SJens Axboe { 9509cbfeafSKirill A. Shutemov put_page(buf->page); 961432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 975274f052SJens Axboe } 985274f052SJens Axboe 990845718dSJens Axboe /* 1000845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 1010845718dSJens Axboe * is a page cache page, IO may be in flight. 1020845718dSJens Axboe */ 103cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 1045274f052SJens Axboe struct pipe_buffer *buf) 1055274f052SJens Axboe { 1065274f052SJens Axboe struct page *page = buf->page; 10749d0b21bSJens Axboe int err; 1085274f052SJens Axboe 1095274f052SJens Axboe if (!PageUptodate(page)) { 11049d0b21bSJens Axboe lock_page(page); 1115274f052SJens Axboe 11249d0b21bSJens Axboe /* 11349d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 11473d62d83SIngo Molnar * splice, if this is the first page. 11549d0b21bSJens Axboe */ 1165274f052SJens Axboe if (!page->mapping) { 11749d0b21bSJens Axboe err = -ENODATA; 11849d0b21bSJens Axboe goto error; 1195274f052SJens Axboe } 1205274f052SJens Axboe 12149d0b21bSJens Axboe /* 12273d62d83SIngo Molnar * Uh oh, read-error from disk. 12349d0b21bSJens Axboe */ 12449d0b21bSJens Axboe if (!PageUptodate(page)) { 12549d0b21bSJens Axboe err = -EIO; 12649d0b21bSJens Axboe goto error; 12749d0b21bSJens Axboe } 12849d0b21bSJens Axboe 12949d0b21bSJens Axboe /* 130f84d7519SJens Axboe * Page is ok afterall, we are done. 13149d0b21bSJens Axboe */ 13249d0b21bSJens Axboe unlock_page(page); 13349d0b21bSJens Axboe } 13449d0b21bSJens Axboe 135f84d7519SJens Axboe return 0; 13649d0b21bSJens Axboe error: 13749d0b21bSJens Axboe unlock_page(page); 138f84d7519SJens Axboe return err; 13970524490SJens Axboe } 14070524490SJens Axboe 141708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 142cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1435274f052SJens Axboe .release = page_cache_pipe_buf_release, 1445abc97aaSJens Axboe .steal = page_cache_pipe_buf_steal, 145f84d7519SJens Axboe .get = generic_pipe_buf_get, 1465274f052SJens Axboe }; 1475274f052SJens Axboe 148912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, 149912d35f8SJens Axboe struct pipe_buffer *buf) 150912d35f8SJens Axboe { 1517afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 152912d35f8SJens Axboe return 1; 1537afa6fd0SJens Axboe 1541432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 155330ab716SJens Axboe return generic_pipe_buf_steal(pipe, buf); 156912d35f8SJens Axboe } 157912d35f8SJens Axboe 158d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 159cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 160912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 161912d35f8SJens Axboe .steal = user_page_pipe_buf_steal, 162f84d7519SJens Axboe .get = generic_pipe_buf_get, 163912d35f8SJens Axboe }; 164912d35f8SJens Axboe 165825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 166825cdcb1SNamhyung Kim { 167825cdcb1SNamhyung Kim smp_mb(); 1680ddad21dSLinus Torvalds if (waitqueue_active(&pipe->rd_wait)) 1690ddad21dSLinus Torvalds wake_up_interruptible(&pipe->rd_wait); 170825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 171825cdcb1SNamhyung Kim } 172825cdcb1SNamhyung Kim 173932cc6d4SJens Axboe /** 174932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 175932cc6d4SJens Axboe * @pipe: pipe to fill 176932cc6d4SJens Axboe * @spd: data to fill 177932cc6d4SJens Axboe * 178932cc6d4SJens Axboe * Description: 17979685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 180932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 181932cc6d4SJens Axboe * function will link that data to the pipe. 182932cc6d4SJens Axboe * 18383f9135bSJens Axboe */ 184d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 185912d35f8SJens Axboe struct splice_pipe_desc *spd) 1865274f052SJens Axboe { 18700de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 1888cefc107SDavid Howells unsigned int tail = pipe->tail; 1898cefc107SDavid Howells unsigned int head = pipe->head; 1908cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 1918924feffSAl Viro int ret = 0, page_nr = 0; 1925274f052SJens Axboe 193d6785d91SRabin Vincent if (!spd_pages) 194d6785d91SRabin Vincent return 0; 195d6785d91SRabin Vincent 1968924feffSAl Viro if (unlikely(!pipe->readers)) { 1975274f052SJens Axboe send_sig(SIGPIPE, current, 0); 1985274f052SJens Axboe ret = -EPIPE; 1998924feffSAl Viro goto out; 2005274f052SJens Axboe } 2015274f052SJens Axboe 2026718b6f8SDavid Howells while (!pipe_full(head, tail, pipe->max_usage)) { 2038cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[head & mask]; 2045274f052SJens Axboe 205912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 206912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 207912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 208497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 209912d35f8SJens Axboe buf->ops = spd->ops; 2105a81e6a1SMiklos Szeredi buf->flags = 0; 2117afa6fd0SJens Axboe 2128cefc107SDavid Howells head++; 2138cefc107SDavid Howells pipe->head = head; 214912d35f8SJens Axboe page_nr++; 215912d35f8SJens Axboe ret += buf->len; 216912d35f8SJens Axboe 217912d35f8SJens Axboe if (!--spd->nr_pages) 2185274f052SJens Axboe break; 2195274f052SJens Axboe } 2205274f052SJens Axboe 22129e35094SLinus Torvalds if (!ret) 22229e35094SLinus Torvalds ret = -EAGAIN; 22329e35094SLinus Torvalds 2248924feffSAl Viro out: 22500de00bdSJens Axboe while (page_nr < spd_pages) 226bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2275274f052SJens Axboe 2285274f052SJens Axboe return ret; 2295274f052SJens Axboe } 2302b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe); 2315274f052SJens Axboe 23279fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) 23379fddc4eSAl Viro { 2348cefc107SDavid Howells unsigned int head = pipe->head; 2358cefc107SDavid Howells unsigned int tail = pipe->tail; 2368cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 23779fddc4eSAl Viro int ret; 23879fddc4eSAl Viro 23979fddc4eSAl Viro if (unlikely(!pipe->readers)) { 24079fddc4eSAl Viro send_sig(SIGPIPE, current, 0); 24179fddc4eSAl Viro ret = -EPIPE; 2426718b6f8SDavid Howells } else if (pipe_full(head, tail, pipe->max_usage)) { 24379fddc4eSAl Viro ret = -EAGAIN; 24479fddc4eSAl Viro } else { 2458cefc107SDavid Howells pipe->bufs[head & mask] = *buf; 2468cefc107SDavid Howells pipe->head = head + 1; 24779fddc4eSAl Viro return buf->len; 24879fddc4eSAl Viro } 249a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 25079fddc4eSAl Viro return ret; 25179fddc4eSAl Viro } 25279fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe); 25379fddc4eSAl Viro 25435f3d14dSJens Axboe /* 25535f3d14dSJens Axboe * Check if we need to grow the arrays holding pages and partial page 25635f3d14dSJens Axboe * descriptions. 25735f3d14dSJens Axboe */ 258047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 25935f3d14dSJens Axboe { 2606718b6f8SDavid Howells unsigned int max_usage = READ_ONCE(pipe->max_usage); 261047fe360SEric Dumazet 2628cefc107SDavid Howells spd->nr_pages_max = max_usage; 2638cefc107SDavid Howells if (max_usage <= PIPE_DEF_BUFFERS) 26435f3d14dSJens Axboe return 0; 26535f3d14dSJens Axboe 2668cefc107SDavid Howells spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL); 2678cefc107SDavid Howells spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page), 2686da2ec56SKees Cook GFP_KERNEL); 26935f3d14dSJens Axboe 27035f3d14dSJens Axboe if (spd->pages && spd->partial) 27135f3d14dSJens Axboe return 0; 27235f3d14dSJens Axboe 27335f3d14dSJens Axboe kfree(spd->pages); 27435f3d14dSJens Axboe kfree(spd->partial); 27535f3d14dSJens Axboe return -ENOMEM; 27635f3d14dSJens Axboe } 27735f3d14dSJens Axboe 278047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd) 27935f3d14dSJens Axboe { 280047fe360SEric Dumazet if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 28135f3d14dSJens Axboe return; 28235f3d14dSJens Axboe 28335f3d14dSJens Axboe kfree(spd->pages); 28435f3d14dSJens Axboe kfree(spd->partial); 28535f3d14dSJens Axboe } 28635f3d14dSJens Axboe 28783f9135bSJens Axboe /** 28883f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 28983f9135bSJens Axboe * @in: file to splice from 290932cc6d4SJens Axboe * @ppos: position in @in 29183f9135bSJens Axboe * @pipe: pipe to splice to 29283f9135bSJens Axboe * @len: number of bytes to splice 29383f9135bSJens Axboe * @flags: splice modifier flags 29483f9135bSJens Axboe * 295932cc6d4SJens Axboe * Description: 296932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 29782c156f8SAl Viro * used as long as it has more or less sane ->read_iter(). 298932cc6d4SJens Axboe * 29983f9135bSJens Axboe */ 300cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 301cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 302cbb7e577SJens Axboe unsigned int flags) 3035274f052SJens Axboe { 30482c156f8SAl Viro struct iov_iter to; 30582c156f8SAl Viro struct kiocb kiocb; 3068cefc107SDavid Howells unsigned int i_head; 3078cefc107SDavid Howells int ret; 308be64f884SBoaz Harrosh 309aa563d7bSDavid Howells iov_iter_pipe(&to, READ, pipe, len); 3108cefc107SDavid Howells i_head = to.head; 31182c156f8SAl Viro init_sync_kiocb(&kiocb, in); 31282c156f8SAl Viro kiocb.ki_pos = *ppos; 313bb7462b6SMiklos Szeredi ret = call_read_iter(in, &kiocb, &to); 314723590edSMiklos Szeredi if (ret > 0) { 31582c156f8SAl Viro *ppos = kiocb.ki_pos; 316723590edSMiklos Szeredi file_accessed(in); 31782c156f8SAl Viro } else if (ret < 0) { 3188cefc107SDavid Howells to.head = i_head; 31982c156f8SAl Viro to.iov_offset = 0; 32082c156f8SAl Viro iov_iter_advance(&to, 0); /* to free what was emitted */ 32182c156f8SAl Viro /* 32282c156f8SAl Viro * callers of ->splice_read() expect -EAGAIN on 32382c156f8SAl Viro * "can't put anything in there", rather than -EFAULT. 32482c156f8SAl Viro */ 32582c156f8SAl Viro if (ret == -EFAULT) 32682c156f8SAl Viro ret = -EAGAIN; 327723590edSMiklos Szeredi } 3285274f052SJens Axboe 3295274f052SJens Axboe return ret; 3305274f052SJens Axboe } 331059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 332059a8f37SJens Axboe 333241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = { 3346818173bSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 3356818173bSMiklos Szeredi .release = generic_pipe_buf_release, 3366818173bSMiklos Szeredi .steal = generic_pipe_buf_steal, 3376818173bSMiklos Szeredi .get = generic_pipe_buf_get, 3386818173bSMiklos Szeredi }; 3396818173bSMiklos Szeredi 340b9872226SJann Horn int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe, 34128a625cbSMiklos Szeredi struct pipe_buffer *buf) 34228a625cbSMiklos Szeredi { 34328a625cbSMiklos Szeredi return 1; 34428a625cbSMiklos Szeredi } 34528a625cbSMiklos Szeredi 34628a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 34728a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 34828a625cbSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 34928a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 35028a625cbSMiklos Szeredi .steal = generic_pipe_buf_nosteal, 35128a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 35228a625cbSMiklos Szeredi }; 35328a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 35428a625cbSMiklos Szeredi 355523ac9afSAl Viro static ssize_t kernel_readv(struct file *file, const struct kvec *vec, 3566818173bSMiklos Szeredi unsigned long vlen, loff_t offset) 3576818173bSMiklos Szeredi { 3586818173bSMiklos Szeredi mm_segment_t old_fs; 3596818173bSMiklos Szeredi loff_t pos = offset; 3606818173bSMiklos Szeredi ssize_t res; 3616818173bSMiklos Szeredi 3626818173bSMiklos Szeredi old_fs = get_fs(); 363736706beSLinus Torvalds set_fs(KERNEL_DS); 3646818173bSMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 365793b80efSChristoph Hellwig res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0); 3666818173bSMiklos Szeredi set_fs(old_fs); 3676818173bSMiklos Szeredi 3686818173bSMiklos Szeredi return res; 3696818173bSMiklos Szeredi } 3706818173bSMiklos Szeredi 37182c156f8SAl Viro static ssize_t default_file_splice_read(struct file *in, loff_t *ppos, 3726818173bSMiklos Szeredi struct pipe_inode_info *pipe, size_t len, 3736818173bSMiklos Szeredi unsigned int flags) 3746818173bSMiklos Szeredi { 375523ac9afSAl Viro struct kvec *vec, __vec[PIPE_DEF_BUFFERS]; 376523ac9afSAl Viro struct iov_iter to; 377523ac9afSAl Viro struct page **pages; 3786818173bSMiklos Szeredi unsigned int nr_pages; 3798cefc107SDavid Howells unsigned int mask; 38013c0f52bSAl Viro size_t offset, base, copied = 0; 3816818173bSMiklos Szeredi ssize_t res; 3826818173bSMiklos Szeredi int i; 3836818173bSMiklos Szeredi 3846718b6f8SDavid Howells if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 385523ac9afSAl Viro return -EAGAIN; 386523ac9afSAl Viro 387523ac9afSAl Viro /* 388523ac9afSAl Viro * Try to keep page boundaries matching to source pagecache ones - 389523ac9afSAl Viro * it probably won't be much help, but... 390523ac9afSAl Viro */ 391523ac9afSAl Viro offset = *ppos & ~PAGE_MASK; 392523ac9afSAl Viro 393aa563d7bSDavid Howells iov_iter_pipe(&to, READ, pipe, len + offset); 394523ac9afSAl Viro 39513c0f52bSAl Viro res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base); 396523ac9afSAl Viro if (res <= 0) 39735f3d14dSJens Axboe return -ENOMEM; 39835f3d14dSJens Axboe 39913c0f52bSAl Viro nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE); 400523ac9afSAl Viro 40135f3d14dSJens Axboe vec = __vec; 402523ac9afSAl Viro if (nr_pages > PIPE_DEF_BUFFERS) { 4036da2ec56SKees Cook vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL); 404523ac9afSAl Viro if (unlikely(!vec)) { 405523ac9afSAl Viro res = -ENOMEM; 406523ac9afSAl Viro goto out; 407523ac9afSAl Viro } 40835f3d14dSJens Axboe } 40935f3d14dSJens Axboe 4108cefc107SDavid Howells mask = pipe->ring_size - 1; 4118cefc107SDavid Howells pipe->bufs[to.head & mask].offset = offset; 4128cefc107SDavid Howells pipe->bufs[to.head & mask].len -= offset; 4136818173bSMiklos Szeredi 414523ac9afSAl Viro for (i = 0; i < nr_pages; i++) { 415523ac9afSAl Viro size_t this_len = min_t(size_t, len, PAGE_SIZE - offset); 416523ac9afSAl Viro vec[i].iov_base = page_address(pages[i]) + offset; 4176818173bSMiklos Szeredi vec[i].iov_len = this_len; 4186818173bSMiklos Szeredi len -= this_len; 4196818173bSMiklos Szeredi offset = 0; 4206818173bSMiklos Szeredi } 4216818173bSMiklos Szeredi 422523ac9afSAl Viro res = kernel_readv(in, vec, nr_pages, *ppos); 423523ac9afSAl Viro if (res > 0) { 424523ac9afSAl Viro copied = res; 4256818173bSMiklos Szeredi *ppos += res; 426523ac9afSAl Viro } 4276818173bSMiklos Szeredi 42835f3d14dSJens Axboe if (vec != __vec) 42935f3d14dSJens Axboe kfree(vec); 430523ac9afSAl Viro out: 431523ac9afSAl Viro for (i = 0; i < nr_pages; i++) 432523ac9afSAl Viro put_page(pages[i]); 433523ac9afSAl Viro kvfree(pages); 434523ac9afSAl Viro iov_iter_advance(&to, copied); /* truncates and discards */ 4356818173bSMiklos Szeredi return res; 4366818173bSMiklos Szeredi } 4376818173bSMiklos Szeredi 4385274f052SJens Axboe /* 4394f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 440016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 4415274f052SJens Axboe */ 44276ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 4435274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 4445274f052SJens Axboe { 4456a14b90bSJens Axboe struct file *file = sd->u.file; 4465274f052SJens Axboe loff_t pos = sd->pos; 447a8adbe37SMichał Mirosław int more; 4485274f052SJens Axboe 44972c2d531SAl Viro if (!likely(file->f_op->sendpage)) 450a8adbe37SMichał Mirosław return -EINVAL; 451a8adbe37SMichał Mirosław 45235f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 453ae62ca7bSEric Dumazet 4548cefc107SDavid Howells if (sd->len < sd->total_len && 4558cefc107SDavid Howells pipe_occupancy(pipe->head, pipe->tail) > 1) 45635f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 457ae62ca7bSEric Dumazet 458a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 459f84d7519SJens Axboe sd->len, &pos, more); 4605274f052SJens Axboe } 4615274f052SJens Axboe 462b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 463b3c2d2ddSMiklos Szeredi { 464b3c2d2ddSMiklos Szeredi smp_mb(); 4650ddad21dSLinus Torvalds if (waitqueue_active(&pipe->wr_wait)) 4660ddad21dSLinus Torvalds wake_up_interruptible(&pipe->wr_wait); 467b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 468b3c2d2ddSMiklos Szeredi } 469b3c2d2ddSMiklos Szeredi 470b3c2d2ddSMiklos Szeredi /** 471b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 472b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 473b3c2d2ddSMiklos Szeredi * @sd: information to @actor 474b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 475b3c2d2ddSMiklos Szeredi * 476b3c2d2ddSMiklos Szeredi * Description: 477b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 478b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 479b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 480b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 481b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 482b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 483b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 484b3c2d2ddSMiklos Szeredi * 485b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 486b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 487b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 488b3c2d2ddSMiklos Szeredi * destination. 489b3c2d2ddSMiklos Szeredi */ 49096f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 491b3c2d2ddSMiklos Szeredi splice_actor *actor) 492b3c2d2ddSMiklos Szeredi { 4938cefc107SDavid Howells unsigned int head = pipe->head; 4948cefc107SDavid Howells unsigned int tail = pipe->tail; 4958cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 496b3c2d2ddSMiklos Szeredi int ret; 497b3c2d2ddSMiklos Szeredi 498ec057595SLinus Torvalds while (!pipe_empty(head, tail)) { 4998cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 500b3c2d2ddSMiklos Szeredi 501b3c2d2ddSMiklos Szeredi sd->len = buf->len; 502b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 503b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 504b3c2d2ddSMiklos Szeredi 505fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 506a8adbe37SMichał Mirosław if (unlikely(ret)) { 507b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 508b3c2d2ddSMiklos Szeredi ret = 0; 509b3c2d2ddSMiklos Szeredi return ret; 510b3c2d2ddSMiklos Szeredi } 511a8adbe37SMichał Mirosław 512a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 513a8adbe37SMichał Mirosław if (ret <= 0) 514a8adbe37SMichał Mirosław return ret; 515a8adbe37SMichał Mirosław 516b3c2d2ddSMiklos Szeredi buf->offset += ret; 517b3c2d2ddSMiklos Szeredi buf->len -= ret; 518b3c2d2ddSMiklos Szeredi 519b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 520b3c2d2ddSMiklos Szeredi sd->len -= ret; 521b3c2d2ddSMiklos Szeredi sd->pos += ret; 522b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 523b3c2d2ddSMiklos Szeredi 524b3c2d2ddSMiklos Szeredi if (!buf->len) { 525a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 5268cefc107SDavid Howells tail++; 5278cefc107SDavid Howells pipe->tail = tail; 5286447a3cfSAl Viro if (pipe->files) 529b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 530b3c2d2ddSMiklos Szeredi } 531b3c2d2ddSMiklos Szeredi 532b3c2d2ddSMiklos Szeredi if (!sd->total_len) 533b3c2d2ddSMiklos Szeredi return 0; 534b3c2d2ddSMiklos Szeredi } 535b3c2d2ddSMiklos Szeredi 536b3c2d2ddSMiklos Szeredi return 1; 537b3c2d2ddSMiklos Szeredi } 538b3c2d2ddSMiklos Szeredi 539b3c2d2ddSMiklos Szeredi /** 540b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 541b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 542b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 543b3c2d2ddSMiklos Szeredi * 544b3c2d2ddSMiklos Szeredi * Description: 545b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 546b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 547b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 548b3c2d2ddSMiklos Szeredi */ 54996f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 550b3c2d2ddSMiklos Szeredi { 551c725bfceSJan Kara /* 552c725bfceSJan Kara * Check for signal early to make process killable when there are 553c725bfceSJan Kara * always buffers available 554c725bfceSJan Kara */ 555c725bfceSJan Kara if (signal_pending(current)) 556c725bfceSJan Kara return -ERESTARTSYS; 557c725bfceSJan Kara 5588cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 559b3c2d2ddSMiklos Szeredi if (!pipe->writers) 560b3c2d2ddSMiklos Szeredi return 0; 561b3c2d2ddSMiklos Szeredi 562a28c8b9dSLinus Torvalds if (sd->num_spliced) 563b3c2d2ddSMiklos Szeredi return 0; 564b3c2d2ddSMiklos Szeredi 565b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 566b3c2d2ddSMiklos Szeredi return -EAGAIN; 567b3c2d2ddSMiklos Szeredi 568b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 569b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 570b3c2d2ddSMiklos Szeredi 571b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 572b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 573b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 574b3c2d2ddSMiklos Szeredi } 575b3c2d2ddSMiklos Szeredi 576b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 577b3c2d2ddSMiklos Szeredi } 578b3c2d2ddSMiklos Szeredi 579b3c2d2ddSMiklos Szeredi return 1; 580b3c2d2ddSMiklos Szeredi } 581b3c2d2ddSMiklos Szeredi 582b3c2d2ddSMiklos Szeredi /** 583b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 584b80901bbSRandy Dunlap * @sd: information about the splice operation 585b3c2d2ddSMiklos Szeredi * 586b3c2d2ddSMiklos Szeredi * Description: 587b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 588b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 589b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 590b3c2d2ddSMiklos Szeredi */ 59196f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd) 592b3c2d2ddSMiklos Szeredi { 593b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 594b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 595b3c2d2ddSMiklos Szeredi } 596b3c2d2ddSMiklos Szeredi 597b3c2d2ddSMiklos Szeredi /** 598b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 599b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 600b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 601b3c2d2ddSMiklos Szeredi * 602b3c2d2ddSMiklos Szeredi * Description: 603b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 604b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 605b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 606b3c2d2ddSMiklos Szeredi */ 60796f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 608b3c2d2ddSMiklos Szeredi { 609b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 610b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 611b3c2d2ddSMiklos Szeredi } 612b3c2d2ddSMiklos Szeredi 613932cc6d4SJens Axboe /** 614932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 615932cc6d4SJens Axboe * @pipe: pipe to splice from 616932cc6d4SJens Axboe * @sd: information to @actor 617932cc6d4SJens Axboe * @actor: handler that splices the data 618932cc6d4SJens Axboe * 619932cc6d4SJens Axboe * Description: 620932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 621932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 622932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 623932cc6d4SJens Axboe * pipe_to_user. 624932cc6d4SJens Axboe * 62583f9135bSJens Axboe */ 626c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 627c66ab6faSJens Axboe splice_actor *actor) 6285274f052SJens Axboe { 629b3c2d2ddSMiklos Szeredi int ret; 6305274f052SJens Axboe 631b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 632b3c2d2ddSMiklos Szeredi do { 633c2489e07SJan Kara cond_resched(); 634b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 635b3c2d2ddSMiklos Szeredi if (ret > 0) 636b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 637b3c2d2ddSMiklos Szeredi } while (ret > 0); 638b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 6395274f052SJens Axboe 640b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 6415274f052SJens Axboe } 64240bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 6435274f052SJens Axboe 644932cc6d4SJens Axboe /** 645932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 646932cc6d4SJens Axboe * @pipe: pipe to splice from 647932cc6d4SJens Axboe * @out: file to splice to 648932cc6d4SJens Axboe * @ppos: position in @out 649932cc6d4SJens Axboe * @len: how many bytes to splice 650932cc6d4SJens Axboe * @flags: splice modifier flags 651932cc6d4SJens Axboe * @actor: handler that splices the data 652932cc6d4SJens Axboe * 653932cc6d4SJens Axboe * Description: 6542933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 655932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 656932cc6d4SJens Axboe * 657932cc6d4SJens Axboe */ 6586da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 6596da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 6606da61809SMark Fasheh splice_actor *actor) 6616da61809SMark Fasheh { 6626da61809SMark Fasheh ssize_t ret; 663c66ab6faSJens Axboe struct splice_desc sd = { 664c66ab6faSJens Axboe .total_len = len, 665c66ab6faSJens Axboe .flags = flags, 666c66ab6faSJens Axboe .pos = *ppos, 6676a14b90bSJens Axboe .u.file = out, 668c66ab6faSJens Axboe }; 6696da61809SMark Fasheh 67061e0d47cSMiklos Szeredi pipe_lock(pipe); 671c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 67261e0d47cSMiklos Szeredi pipe_unlock(pipe); 6736da61809SMark Fasheh 6746da61809SMark Fasheh return ret; 6756da61809SMark Fasheh } 6766da61809SMark Fasheh 6776da61809SMark Fasheh /** 6788d020765SAl Viro * iter_file_splice_write - splice data from a pipe to a file 6798d020765SAl Viro * @pipe: pipe info 6808d020765SAl Viro * @out: file to write to 6818d020765SAl Viro * @ppos: position in @out 6828d020765SAl Viro * @len: number of bytes to splice 6838d020765SAl Viro * @flags: splice modifier flags 6848d020765SAl Viro * 6858d020765SAl Viro * Description: 6868d020765SAl Viro * Will either move or copy pages (determined by @flags options) from 6878d020765SAl Viro * the given pipe inode to the given file. 6888d020765SAl Viro * This one is ->write_iter-based. 6898d020765SAl Viro * 6908d020765SAl Viro */ 6918d020765SAl Viro ssize_t 6928d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 6938d020765SAl Viro loff_t *ppos, size_t len, unsigned int flags) 6948d020765SAl Viro { 6958d020765SAl Viro struct splice_desc sd = { 6968d020765SAl Viro .total_len = len, 6978d020765SAl Viro .flags = flags, 6988d020765SAl Viro .pos = *ppos, 6998d020765SAl Viro .u.file = out, 7008d020765SAl Viro }; 7016718b6f8SDavid Howells int nbufs = pipe->max_usage; 7028d020765SAl Viro struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 7038d020765SAl Viro GFP_KERNEL); 7048d020765SAl Viro ssize_t ret; 7058d020765SAl Viro 7068d020765SAl Viro if (unlikely(!array)) 7078d020765SAl Viro return -ENOMEM; 7088d020765SAl Viro 7098d020765SAl Viro pipe_lock(pipe); 7108d020765SAl Viro 7118d020765SAl Viro splice_from_pipe_begin(&sd); 7128d020765SAl Viro while (sd.total_len) { 7138d020765SAl Viro struct iov_iter from; 714ec057595SLinus Torvalds unsigned int head, tail, mask; 7158d020765SAl Viro size_t left; 7168cefc107SDavid Howells int n; 7178d020765SAl Viro 7188d020765SAl Viro ret = splice_from_pipe_next(pipe, &sd); 7198d020765SAl Viro if (ret <= 0) 7208d020765SAl Viro break; 7218d020765SAl Viro 7226718b6f8SDavid Howells if (unlikely(nbufs < pipe->max_usage)) { 7238d020765SAl Viro kfree(array); 7246718b6f8SDavid Howells nbufs = pipe->max_usage; 7258d020765SAl Viro array = kcalloc(nbufs, sizeof(struct bio_vec), 7268d020765SAl Viro GFP_KERNEL); 7278d020765SAl Viro if (!array) { 7288d020765SAl Viro ret = -ENOMEM; 7298d020765SAl Viro break; 7308d020765SAl Viro } 7318d020765SAl Viro } 7328d020765SAl Viro 733ec057595SLinus Torvalds head = pipe->head; 734ec057595SLinus Torvalds tail = pipe->tail; 735ec057595SLinus Torvalds mask = pipe->ring_size - 1; 736ec057595SLinus Torvalds 7378d020765SAl Viro /* build the vector */ 7388d020765SAl Viro left = sd.total_len; 7398cefc107SDavid Howells for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) { 7408cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 7418d020765SAl Viro size_t this_len = buf->len; 7428d020765SAl Viro 7438d020765SAl Viro if (this_len > left) 7448d020765SAl Viro this_len = left; 7458d020765SAl Viro 746fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 7478d020765SAl Viro if (unlikely(ret)) { 7488d020765SAl Viro if (ret == -ENODATA) 7498d020765SAl Viro ret = 0; 7508d020765SAl Viro goto done; 7518d020765SAl Viro } 7528d020765SAl Viro 7538d020765SAl Viro array[n].bv_page = buf->page; 7548d020765SAl Viro array[n].bv_len = this_len; 7558d020765SAl Viro array[n].bv_offset = buf->offset; 7568d020765SAl Viro left -= this_len; 7578d020765SAl Viro } 7588d020765SAl Viro 759aa563d7bSDavid Howells iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left); 760abbb6589SChristoph Hellwig ret = vfs_iter_write(out, &from, &sd.pos, 0); 7618d020765SAl Viro if (ret <= 0) 7628d020765SAl Viro break; 7638d020765SAl Viro 7648d020765SAl Viro sd.num_spliced += ret; 7658d020765SAl Viro sd.total_len -= ret; 766dbe4e192SChristoph Hellwig *ppos = sd.pos; 7678d020765SAl Viro 7688d020765SAl Viro /* dismiss the fully eaten buffers, adjust the partial one */ 7698cefc107SDavid Howells tail = pipe->tail; 7708d020765SAl Viro while (ret) { 7718cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 7728d020765SAl Viro if (ret >= buf->len) { 7738d020765SAl Viro ret -= buf->len; 7748d020765SAl Viro buf->len = 0; 775a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 7768cefc107SDavid Howells tail++; 7778cefc107SDavid Howells pipe->tail = tail; 7788d020765SAl Viro if (pipe->files) 7798d020765SAl Viro sd.need_wakeup = true; 7808d020765SAl Viro } else { 7818d020765SAl Viro buf->offset += ret; 7828d020765SAl Viro buf->len -= ret; 7838d020765SAl Viro ret = 0; 7848d020765SAl Viro } 7858d020765SAl Viro } 7868d020765SAl Viro } 7878d020765SAl Viro done: 7888d020765SAl Viro kfree(array); 7898d020765SAl Viro splice_from_pipe_end(pipe, &sd); 7908d020765SAl Viro 7918d020765SAl Viro pipe_unlock(pipe); 7928d020765SAl Viro 7938d020765SAl Viro if (sd.num_spliced) 7948d020765SAl Viro ret = sd.num_spliced; 7958d020765SAl Viro 7968d020765SAl Viro return ret; 7978d020765SAl Viro } 7988d020765SAl Viro 7998d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write); 8008d020765SAl Viro 801b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 802b2858d7dSMiklos Szeredi struct splice_desc *sd) 8030b0a47f5SMiklos Szeredi { 804b2858d7dSMiklos Szeredi int ret; 805b2858d7dSMiklos Szeredi void *data; 80606ae43f3SAl Viro loff_t tmp = sd->pos; 807b2858d7dSMiklos Szeredi 808fbb32750SAl Viro data = kmap(buf->page); 80906ae43f3SAl Viro ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); 810fbb32750SAl Viro kunmap(buf->page); 811b2858d7dSMiklos Szeredi 812b2858d7dSMiklos Szeredi return ret; 8130b0a47f5SMiklos Szeredi } 8140b0a47f5SMiklos Szeredi 8150b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, 8160b0a47f5SMiklos Szeredi struct file *out, loff_t *ppos, 8170b0a47f5SMiklos Szeredi size_t len, unsigned int flags) 8180b0a47f5SMiklos Szeredi { 819b2858d7dSMiklos Szeredi ssize_t ret; 8200b0a47f5SMiklos Szeredi 821b2858d7dSMiklos Szeredi ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); 822b2858d7dSMiklos Szeredi if (ret > 0) 823b2858d7dSMiklos Szeredi *ppos += ret; 8240b0a47f5SMiklos Szeredi 825b2858d7dSMiklos Szeredi return ret; 8260b0a47f5SMiklos Szeredi } 8270b0a47f5SMiklos Szeredi 82883f9135bSJens Axboe /** 82983f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 830932cc6d4SJens Axboe * @pipe: pipe to splice from 83183f9135bSJens Axboe * @out: socket to write to 832932cc6d4SJens Axboe * @ppos: position in @out 83383f9135bSJens Axboe * @len: number of bytes to splice 83483f9135bSJens Axboe * @flags: splice modifier flags 83583f9135bSJens Axboe * 836932cc6d4SJens Axboe * Description: 83783f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 83883f9135bSJens Axboe * is involved. 83983f9135bSJens Axboe * 84083f9135bSJens Axboe */ 8413a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 842cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 8435274f052SJens Axboe { 84400522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 8455274f052SJens Axboe } 8465274f052SJens Axboe 847059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 848a0f06780SJeff Garzik 84983f9135bSJens Axboe /* 85083f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 85183f9135bSJens Axboe */ 8523a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 853cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 8545274f052SJens Axboe { 8550b0a47f5SMiklos Szeredi ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 8560b0a47f5SMiklos Szeredi loff_t *, size_t, unsigned int); 8575274f052SJens Axboe 85872c2d531SAl Viro if (out->f_op->splice_write) 8590b0a47f5SMiklos Szeredi splice_write = out->f_op->splice_write; 860cc56f7deSChangli Gao else 8610b0a47f5SMiklos Szeredi splice_write = default_file_splice_write; 8620b0a47f5SMiklos Szeredi 863500368f7SAl Viro return splice_write(pipe, out, ppos, len, flags); 8645274f052SJens Axboe } 8655274f052SJens Axboe 86683f9135bSJens Axboe /* 86783f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 86883f9135bSJens Axboe */ 869cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 870cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 871cbb7e577SJens Axboe unsigned int flags) 8725274f052SJens Axboe { 8736818173bSMiklos Szeredi ssize_t (*splice_read)(struct file *, loff_t *, 8746818173bSMiklos Szeredi struct pipe_inode_info *, size_t, unsigned int); 8755274f052SJens Axboe int ret; 8765274f052SJens Axboe 87749570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 8785274f052SJens Axboe return -EBADF; 8795274f052SJens Axboe 880cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 8815274f052SJens Axboe if (unlikely(ret < 0)) 8825274f052SJens Axboe return ret; 8835274f052SJens Axboe 88403cc0789SAl Viro if (unlikely(len > MAX_RW_COUNT)) 88503cc0789SAl Viro len = MAX_RW_COUNT; 88603cc0789SAl Viro 88772c2d531SAl Viro if (in->f_op->splice_read) 8886818173bSMiklos Szeredi splice_read = in->f_op->splice_read; 889cc56f7deSChangli Gao else 8906818173bSMiklos Szeredi splice_read = default_file_splice_read; 8916818173bSMiklos Szeredi 8926818173bSMiklos Szeredi return splice_read(in, ppos, pipe, len, flags); 8935274f052SJens Axboe } 8945274f052SJens Axboe 895932cc6d4SJens Axboe /** 896932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 897932cc6d4SJens Axboe * @in: file to splice from 898932cc6d4SJens Axboe * @sd: actor information on where to splice to 899932cc6d4SJens Axboe * @actor: handles the data splicing 900932cc6d4SJens Axboe * 901932cc6d4SJens Axboe * Description: 902932cc6d4SJens Axboe * This is a special case helper to splice directly between two 903932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 904932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 905932cc6d4SJens Axboe * that process. 906932cc6d4SJens Axboe * 907c66ab6faSJens Axboe */ 908c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 909c66ab6faSJens Axboe splice_direct_actor *actor) 910b92ce558SJens Axboe { 911b92ce558SJens Axboe struct pipe_inode_info *pipe; 912b92ce558SJens Axboe long ret, bytes; 913b92ce558SJens Axboe umode_t i_mode; 914c66ab6faSJens Axboe size_t len; 9150ff28d9fSChristophe Leroy int i, flags, more; 916b92ce558SJens Axboe 917b92ce558SJens Axboe /* 918b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 919b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 920b92ce558SJens Axboe * piped splicing for that! 921b92ce558SJens Axboe */ 922496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 923b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 924b92ce558SJens Axboe return -EINVAL; 925b92ce558SJens Axboe 926b92ce558SJens Axboe /* 927b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 928b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 929b92ce558SJens Axboe */ 930b92ce558SJens Axboe pipe = current->splice_pipe; 93149570e9bSJens Axboe if (unlikely(!pipe)) { 9327bee130eSAl Viro pipe = alloc_pipe_info(); 933b92ce558SJens Axboe if (!pipe) 934b92ce558SJens Axboe return -ENOMEM; 935b92ce558SJens Axboe 936b92ce558SJens Axboe /* 937b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 93800522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 939b92ce558SJens Axboe * PIPE_READERS appropriately. 940b92ce558SJens Axboe */ 941b92ce558SJens Axboe pipe->readers = 1; 942b92ce558SJens Axboe 943b92ce558SJens Axboe current->splice_pipe = pipe; 944b92ce558SJens Axboe } 945b92ce558SJens Axboe 946b92ce558SJens Axboe /* 94773d62d83SIngo Molnar * Do the splice. 948b92ce558SJens Axboe */ 949b92ce558SJens Axboe ret = 0; 950b92ce558SJens Axboe bytes = 0; 951c66ab6faSJens Axboe len = sd->total_len; 952c66ab6faSJens Axboe flags = sd->flags; 953c66ab6faSJens Axboe 954c66ab6faSJens Axboe /* 955c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 956c66ab6faSJens Axboe */ 957c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 9580ff28d9fSChristophe Leroy more = sd->flags & SPLICE_F_MORE; 959b92ce558SJens Axboe 9608cefc107SDavid Howells WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); 96117614445SDarrick J. Wong 962b92ce558SJens Axboe while (len) { 9638cefc107SDavid Howells unsigned int p_space; 96451a92c0fSJens Axboe size_t read_len; 965a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 966b92ce558SJens Axboe 96717614445SDarrick J. Wong /* Don't try to read more the pipe has space for. */ 9686718b6f8SDavid Howells p_space = pipe->max_usage - 9698cefc107SDavid Howells pipe_occupancy(pipe->head, pipe->tail); 9708cefc107SDavid Howells read_len = min_t(size_t, len, p_space << PAGE_SHIFT); 97117614445SDarrick J. Wong ret = do_splice_to(in, &pos, pipe, read_len, flags); 97251a92c0fSJens Axboe if (unlikely(ret <= 0)) 973b92ce558SJens Axboe goto out_release; 974b92ce558SJens Axboe 975b92ce558SJens Axboe read_len = ret; 976c66ab6faSJens Axboe sd->total_len = read_len; 977b92ce558SJens Axboe 978b92ce558SJens Axboe /* 9790ff28d9fSChristophe Leroy * If more data is pending, set SPLICE_F_MORE 9800ff28d9fSChristophe Leroy * If this is the last data and SPLICE_F_MORE was not set 9810ff28d9fSChristophe Leroy * initially, clears it. 9820ff28d9fSChristophe Leroy */ 9830ff28d9fSChristophe Leroy if (read_len < len) 9840ff28d9fSChristophe Leroy sd->flags |= SPLICE_F_MORE; 9850ff28d9fSChristophe Leroy else if (!more) 9860ff28d9fSChristophe Leroy sd->flags &= ~SPLICE_F_MORE; 9870ff28d9fSChristophe Leroy /* 988b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 989b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 990b92ce558SJens Axboe * could get stuck data in the internal pipe: 991b92ce558SJens Axboe */ 992c66ab6faSJens Axboe ret = actor(pipe, sd); 993a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 994a82c53a0STom Zanussi sd->pos = prev_pos; 995b92ce558SJens Axboe goto out_release; 996a82c53a0STom Zanussi } 997b92ce558SJens Axboe 998b92ce558SJens Axboe bytes += ret; 999b92ce558SJens Axboe len -= ret; 1000bcd4f3acSJens Axboe sd->pos = pos; 1001b92ce558SJens Axboe 1002a82c53a0STom Zanussi if (ret < read_len) { 1003a82c53a0STom Zanussi sd->pos = prev_pos + ret; 100451a92c0fSJens Axboe goto out_release; 1005b92ce558SJens Axboe } 1006a82c53a0STom Zanussi } 1007b92ce558SJens Axboe 10089e97198dSJens Axboe done: 10098cefc107SDavid Howells pipe->tail = pipe->head = 0; 10109e97198dSJens Axboe file_accessed(in); 1011b92ce558SJens Axboe return bytes; 1012b92ce558SJens Axboe 1013b92ce558SJens Axboe out_release: 1014b92ce558SJens Axboe /* 1015b92ce558SJens Axboe * If we did an incomplete transfer we must release 1016b92ce558SJens Axboe * the pipe buffers in question: 1017b92ce558SJens Axboe */ 10188cefc107SDavid Howells for (i = 0; i < pipe->ring_size; i++) { 10198cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[i]; 1020b92ce558SJens Axboe 1021a779638cSMiklos Szeredi if (buf->ops) 1022a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 1023b92ce558SJens Axboe } 1024b92ce558SJens Axboe 10259e97198dSJens Axboe if (!bytes) 10269e97198dSJens Axboe bytes = ret; 1027b92ce558SJens Axboe 10289e97198dSJens Axboe goto done; 1029c66ab6faSJens Axboe } 1030c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1031c66ab6faSJens Axboe 1032c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1033c66ab6faSJens Axboe struct splice_desc *sd) 1034c66ab6faSJens Axboe { 10356a14b90bSJens Axboe struct file *file = sd->u.file; 1036c66ab6faSJens Axboe 10377995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 10382cb4b05eSChangli Gao sd->flags); 1039c66ab6faSJens Axboe } 1040c66ab6faSJens Axboe 1041932cc6d4SJens Axboe /** 1042932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1043932cc6d4SJens Axboe * @in: file to splice from 1044932cc6d4SJens Axboe * @ppos: input file offset 1045932cc6d4SJens Axboe * @out: file to splice to 1046acdb37c3SRandy Dunlap * @opos: output file offset 1047932cc6d4SJens Axboe * @len: number of bytes to splice 1048932cc6d4SJens Axboe * @flags: splice modifier flags 1049932cc6d4SJens Axboe * 1050932cc6d4SJens Axboe * Description: 1051932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1052932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1053932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1054932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1055932cc6d4SJens Axboe * 1056932cc6d4SJens Axboe */ 1057c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 10587995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 1059c66ab6faSJens Axboe { 1060c66ab6faSJens Axboe struct splice_desc sd = { 1061c66ab6faSJens Axboe .len = len, 1062c66ab6faSJens Axboe .total_len = len, 1063c66ab6faSJens Axboe .flags = flags, 1064c66ab6faSJens Axboe .pos = *ppos, 10656a14b90bSJens Axboe .u.file = out, 10667995bd28SAl Viro .opos = opos, 1067c66ab6faSJens Axboe }; 106851a92c0fSJens Axboe long ret; 1069c66ab6faSJens Axboe 107018c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 107118c67cb9SAl Viro return -EBADF; 107218c67cb9SAl Viro 107318c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 107418c67cb9SAl Viro return -EINVAL; 107518c67cb9SAl Viro 107618c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 107718c67cb9SAl Viro if (unlikely(ret < 0)) 107818c67cb9SAl Viro return ret; 107918c67cb9SAl Viro 1080c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 108151a92c0fSJens Axboe if (ret > 0) 1082a82c53a0STom Zanussi *ppos = sd.pos; 108351a92c0fSJens Axboe 1084c66ab6faSJens Axboe return ret; 1085b92ce558SJens Axboe } 10861c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct); 1087b92ce558SJens Axboe 10888924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) 10898924feffSAl Viro { 109052bce911SLinus Torvalds for (;;) { 109152bce911SLinus Torvalds if (unlikely(!pipe->readers)) { 109252bce911SLinus Torvalds send_sig(SIGPIPE, current, 0); 109352bce911SLinus Torvalds return -EPIPE; 109452bce911SLinus Torvalds } 10956718b6f8SDavid Howells if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 109652bce911SLinus Torvalds return 0; 10978924feffSAl Viro if (flags & SPLICE_F_NONBLOCK) 10988924feffSAl Viro return -EAGAIN; 10998924feffSAl Viro if (signal_pending(current)) 11008924feffSAl Viro return -ERESTARTSYS; 11018924feffSAl Viro pipe_wait(pipe); 11028924feffSAl Viro } 11038924feffSAl Viro } 11048924feffSAl Viro 11057c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 11067c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 11077c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1108ddac0d39SJens Axboe 1109ddac0d39SJens Axboe /* 111083f9135bSJens Axboe * Determine where to splice to/from. 111183f9135bSJens Axboe */ 1112529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in, 1113529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 1114529565dcSIngo Molnar size_t len, unsigned int flags) 11155274f052SJens Axboe { 11167c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 11177c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 11187995bd28SAl Viro loff_t offset; 1119a4514ebdSJens Axboe long ret; 11205274f052SJens Axboe 112171993e62SLinus Torvalds ipipe = get_pipe_info(in); 112271993e62SLinus Torvalds opipe = get_pipe_info(out); 11237c77f0b3SMiklos Szeredi 11247c77f0b3SMiklos Szeredi if (ipipe && opipe) { 11257c77f0b3SMiklos Szeredi if (off_in || off_out) 11267c77f0b3SMiklos Szeredi return -ESPIPE; 11277c77f0b3SMiklos Szeredi 11287c77f0b3SMiklos Szeredi if (!(in->f_mode & FMODE_READ)) 11297c77f0b3SMiklos Szeredi return -EBADF; 11307c77f0b3SMiklos Szeredi 11317c77f0b3SMiklos Szeredi if (!(out->f_mode & FMODE_WRITE)) 11327c77f0b3SMiklos Szeredi return -EBADF; 11337c77f0b3SMiklos Szeredi 11347c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 11357c77f0b3SMiklos Szeredi if (ipipe == opipe) 11367c77f0b3SMiklos Szeredi return -EINVAL; 11377c77f0b3SMiklos Szeredi 1138ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1139ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1140ee5e0011SSlavomir Kaslev 11417c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 11427c77f0b3SMiklos Szeredi } 11437c77f0b3SMiklos Szeredi 11447c77f0b3SMiklos Szeredi if (ipipe) { 1145529565dcSIngo Molnar if (off_in) 1146529565dcSIngo Molnar return -ESPIPE; 1147b92ce558SJens Axboe if (off_out) { 114819c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1149b92ce558SJens Axboe return -EINVAL; 1150cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1151b92ce558SJens Axboe return -EFAULT; 11527995bd28SAl Viro } else { 11537995bd28SAl Viro offset = out->f_pos; 11547995bd28SAl Viro } 1155529565dcSIngo Molnar 115618c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 115718c67cb9SAl Viro return -EBADF; 115818c67cb9SAl Viro 115918c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 116018c67cb9SAl Viro return -EINVAL; 116118c67cb9SAl Viro 116218c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 116318c67cb9SAl Viro if (unlikely(ret < 0)) 116418c67cb9SAl Viro return ret; 116518c67cb9SAl Viro 1166ee5e0011SSlavomir Kaslev if (in->f_flags & O_NONBLOCK) 1167ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1168ee5e0011SSlavomir Kaslev 1169500368f7SAl Viro file_start_write(out); 11707995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1171500368f7SAl Viro file_end_write(out); 1172a4514ebdSJens Axboe 11737995bd28SAl Viro if (!off_out) 11747995bd28SAl Viro out->f_pos = offset; 11757995bd28SAl Viro else if (copy_to_user(off_out, &offset, sizeof(loff_t))) 1176a4514ebdSJens Axboe ret = -EFAULT; 1177a4514ebdSJens Axboe 1178a4514ebdSJens Axboe return ret; 1179529565dcSIngo Molnar } 11805274f052SJens Axboe 11817c77f0b3SMiklos Szeredi if (opipe) { 1182529565dcSIngo Molnar if (off_out) 1183529565dcSIngo Molnar return -ESPIPE; 1184b92ce558SJens Axboe if (off_in) { 118519c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1186b92ce558SJens Axboe return -EINVAL; 1187cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1188b92ce558SJens Axboe return -EFAULT; 11897995bd28SAl Viro } else { 11907995bd28SAl Viro offset = in->f_pos; 11917995bd28SAl Viro } 1192529565dcSIngo Molnar 1193ee5e0011SSlavomir Kaslev if (out->f_flags & O_NONBLOCK) 1194ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1195ee5e0011SSlavomir Kaslev 11968924feffSAl Viro pipe_lock(opipe); 11978924feffSAl Viro ret = wait_for_space(opipe, flags); 11983253d9d0SDarrick J. Wong if (!ret) { 11996a965666SLinus Torvalds unsigned int p_space; 12003253d9d0SDarrick J. Wong 12013253d9d0SDarrick J. Wong /* Don't try to read more the pipe has space for. */ 12026a965666SLinus Torvalds p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail); 12036a965666SLinus Torvalds len = min_t(size_t, len, p_space << PAGE_SHIFT); 12043253d9d0SDarrick J. Wong 12057995bd28SAl Viro ret = do_splice_to(in, &offset, opipe, len, flags); 12063253d9d0SDarrick J. Wong } 12078924feffSAl Viro pipe_unlock(opipe); 12088924feffSAl Viro if (ret > 0) 12098924feffSAl Viro wakeup_pipe_readers(opipe); 12107995bd28SAl Viro if (!off_in) 12117995bd28SAl Viro in->f_pos = offset; 12127995bd28SAl Viro else if (copy_to_user(off_in, &offset, sizeof(loff_t))) 1213a4514ebdSJens Axboe ret = -EFAULT; 1214a4514ebdSJens Axboe 1215a4514ebdSJens Axboe return ret; 1216529565dcSIngo Molnar } 12175274f052SJens Axboe 12185274f052SJens Axboe return -EINVAL; 12195274f052SJens Axboe } 12205274f052SJens Axboe 122179fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from, 122279fddc4eSAl Viro struct pipe_inode_info *pipe, 122379fddc4eSAl Viro unsigned flags) 1224912d35f8SJens Axboe { 122579fddc4eSAl Viro struct pipe_buffer buf = { 122679fddc4eSAl Viro .ops = &user_page_pipe_buf_ops, 122779fddc4eSAl Viro .flags = flags 122879fddc4eSAl Viro }; 122979fddc4eSAl Viro size_t total = 0; 123079fddc4eSAl Viro int ret = 0; 123179fddc4eSAl Viro bool failed = false; 123279fddc4eSAl Viro 123379fddc4eSAl Viro while (iov_iter_count(from) && !failed) { 123479fddc4eSAl Viro struct page *pages[16]; 1235db85a9ebSAl Viro ssize_t copied; 1236db85a9ebSAl Viro size_t start; 123779fddc4eSAl Viro int n; 1238912d35f8SJens Axboe 123979fddc4eSAl Viro copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start); 124079fddc4eSAl Viro if (copied <= 0) { 124179fddc4eSAl Viro ret = copied; 124279fddc4eSAl Viro break; 124379fddc4eSAl Viro } 1244912d35f8SJens Axboe 124579fddc4eSAl Viro for (n = 0; copied; n++, start = 0) { 1246db85a9ebSAl Viro int size = min_t(int, copied, PAGE_SIZE - start); 124779fddc4eSAl Viro if (!failed) { 124879fddc4eSAl Viro buf.page = pages[n]; 124979fddc4eSAl Viro buf.offset = start; 125079fddc4eSAl Viro buf.len = size; 125179fddc4eSAl Viro ret = add_to_pipe(pipe, &buf); 125279fddc4eSAl Viro if (unlikely(ret < 0)) { 125379fddc4eSAl Viro failed = true; 125479fddc4eSAl Viro } else { 125579fddc4eSAl Viro iov_iter_advance(from, ret); 125679fddc4eSAl Viro total += ret; 125779fddc4eSAl Viro } 125879fddc4eSAl Viro } else { 125979fddc4eSAl Viro put_page(pages[n]); 126079fddc4eSAl Viro } 1261db85a9ebSAl Viro copied -= size; 1262912d35f8SJens Axboe } 1263912d35f8SJens Axboe } 126479fddc4eSAl Viro return total ? total : ret; 1265912d35f8SJens Axboe } 1266912d35f8SJens Axboe 12676a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 12686a14b90bSJens Axboe struct splice_desc *sd) 12696a14b90bSJens Axboe { 12706130f531SAl Viro int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 12716130f531SAl Viro return n == sd->len ? n : -EFAULT; 12726a14b90bSJens Axboe } 12736a14b90bSJens Axboe 12746a14b90bSJens Axboe /* 12756a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 12766a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 12776a14b90bSJens Axboe */ 127887a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter, 127987a3002aSAl Viro unsigned int flags) 12806a14b90bSJens Axboe { 128187a3002aSAl Viro struct pipe_inode_info *pipe = get_pipe_info(file); 128287a3002aSAl Viro struct splice_desc sd = { 128387a3002aSAl Viro .total_len = iov_iter_count(iter), 128487a3002aSAl Viro .flags = flags, 128587a3002aSAl Viro .u.data = iter 128687a3002aSAl Viro }; 128787a3002aSAl Viro long ret = 0; 12886a14b90bSJens Axboe 12896a14b90bSJens Axboe if (!pipe) 12906a14b90bSJens Axboe return -EBADF; 12916a14b90bSJens Axboe 1292345995faSAl Viro if (sd.total_len) { 12936130f531SAl Viro pipe_lock(pipe); 12946130f531SAl Viro ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 129561e0d47cSMiklos Szeredi pipe_unlock(pipe); 1296345995faSAl Viro } 12976a14b90bSJens Axboe 12986a14b90bSJens Axboe return ret; 12996a14b90bSJens Axboe } 13006a14b90bSJens Axboe 1301912d35f8SJens Axboe /* 1302912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1303912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1304912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1305912d35f8SJens Axboe */ 130687a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, 130787a3002aSAl Viro unsigned int flags) 1308912d35f8SJens Axboe { 1309ddac0d39SJens Axboe struct pipe_inode_info *pipe; 131087a3002aSAl Viro long ret = 0; 131179fddc4eSAl Viro unsigned buf_flag = 0; 131279fddc4eSAl Viro 131379fddc4eSAl Viro if (flags & SPLICE_F_GIFT) 131479fddc4eSAl Viro buf_flag = PIPE_BUF_FLAG_GIFT; 1315912d35f8SJens Axboe 131671993e62SLinus Torvalds pipe = get_pipe_info(file); 1317ddac0d39SJens Axboe if (!pipe) 1318912d35f8SJens Axboe return -EBADF; 1319912d35f8SJens Axboe 13208924feffSAl Viro pipe_lock(pipe); 13218924feffSAl Viro ret = wait_for_space(pipe, flags); 132279fddc4eSAl Viro if (!ret) 132387a3002aSAl Viro ret = iter_to_pipe(iter, pipe, buf_flag); 13248924feffSAl Viro pipe_unlock(pipe); 13258924feffSAl Viro if (ret > 0) 13268924feffSAl Viro wakeup_pipe_readers(pipe); 132735f3d14dSJens Axboe return ret; 1328912d35f8SJens Axboe } 1329912d35f8SJens Axboe 133087a3002aSAl Viro static int vmsplice_type(struct fd f, int *type) 133187a3002aSAl Viro { 133287a3002aSAl Viro if (!f.file) 133387a3002aSAl Viro return -EBADF; 133487a3002aSAl Viro if (f.file->f_mode & FMODE_WRITE) { 133587a3002aSAl Viro *type = WRITE; 133687a3002aSAl Viro } else if (f.file->f_mode & FMODE_READ) { 133787a3002aSAl Viro *type = READ; 133887a3002aSAl Viro } else { 133987a3002aSAl Viro fdput(f); 134087a3002aSAl Viro return -EBADF; 134187a3002aSAl Viro } 134287a3002aSAl Viro return 0; 134387a3002aSAl Viro } 134487a3002aSAl Viro 13456a14b90bSJens Axboe /* 13466a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 13476a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 13486a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 13496a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 13506a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 13516a14b90bSJens Axboe * solutions for that: 13526a14b90bSJens Axboe * 13536a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 13546a14b90bSJens Axboe * do a regular read() on the buffer anyway. 13556a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 13566a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 13576a14b90bSJens Axboe * 13586a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 13596a14b90bSJens Axboe * 13606a14b90bSJens Axboe */ 136187a3002aSAl Viro static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags) 1362912d35f8SJens Axboe { 13633d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 13643d6ea290SAl Viro return -EINVAL; 136587a3002aSAl Viro 136687a3002aSAl Viro if (!iov_iter_count(iter)) 13676a14b90bSJens Axboe return 0; 13686a14b90bSJens Axboe 136987a3002aSAl Viro if (iov_iter_rw(iter) == WRITE) 137087a3002aSAl Viro return vmsplice_to_pipe(f, iter, flags); 137187a3002aSAl Viro else 137287a3002aSAl Viro return vmsplice_to_user(f, iter, flags); 1373912d35f8SJens Axboe } 1374912d35f8SJens Axboe 137587a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, 137630cfe4efSDominik Brodowski unsigned long, nr_segs, unsigned int, flags) 137730cfe4efSDominik Brodowski { 137887a3002aSAl Viro struct iovec iovstack[UIO_FASTIOV]; 137987a3002aSAl Viro struct iovec *iov = iovstack; 138087a3002aSAl Viro struct iov_iter iter; 138187e5e6daSJens Axboe ssize_t error; 138287a3002aSAl Viro struct fd f; 138387a3002aSAl Viro int type; 138487a3002aSAl Viro 138587a3002aSAl Viro f = fdget(fd); 138687a3002aSAl Viro error = vmsplice_type(f, &type); 138787a3002aSAl Viro if (error) 138887a3002aSAl Viro return error; 138987a3002aSAl Viro 139087a3002aSAl Viro error = import_iovec(type, uiov, nr_segs, 139187a3002aSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 139287e5e6daSJens Axboe if (error >= 0) { 139387a3002aSAl Viro error = do_vmsplice(f.file, &iter, flags); 139487a3002aSAl Viro kfree(iov); 139587a3002aSAl Viro } 139687a3002aSAl Viro fdput(f); 139787a3002aSAl Viro return error; 139830cfe4efSDominik Brodowski } 139930cfe4efSDominik Brodowski 140076b021d0SAl Viro #ifdef CONFIG_COMPAT 140176b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32, 140276b021d0SAl Viro unsigned int, nr_segs, unsigned int, flags) 140376b021d0SAl Viro { 140487a3002aSAl Viro struct iovec iovstack[UIO_FASTIOV]; 140587a3002aSAl Viro struct iovec *iov = iovstack; 140687a3002aSAl Viro struct iov_iter iter; 140787e5e6daSJens Axboe ssize_t error; 140887a3002aSAl Viro struct fd f; 140987a3002aSAl Viro int type; 141087a3002aSAl Viro 141187a3002aSAl Viro f = fdget(fd); 141287a3002aSAl Viro error = vmsplice_type(f, &type); 141387a3002aSAl Viro if (error) 141487a3002aSAl Viro return error; 141587a3002aSAl Viro 141687a3002aSAl Viro error = compat_import_iovec(type, iov32, nr_segs, 141787a3002aSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 141887e5e6daSJens Axboe if (error >= 0) { 141987a3002aSAl Viro error = do_vmsplice(f.file, &iter, flags); 142087a3002aSAl Viro kfree(iov); 142176b021d0SAl Viro } 142287a3002aSAl Viro fdput(f); 142387a3002aSAl Viro return error; 142476b021d0SAl Viro } 142576b021d0SAl Viro #endif 142676b021d0SAl Viro 1427836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1428836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1429836f92adSHeiko Carstens size_t, len, unsigned int, flags) 14305274f052SJens Axboe { 14312903ff01SAl Viro struct fd in, out; 14325274f052SJens Axboe long error; 14335274f052SJens Axboe 14345274f052SJens Axboe if (unlikely(!len)) 14355274f052SJens Axboe return 0; 14365274f052SJens Axboe 14373d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 14383d6ea290SAl Viro return -EINVAL; 14393d6ea290SAl Viro 14405274f052SJens Axboe error = -EBADF; 14412903ff01SAl Viro in = fdget(fd_in); 14422903ff01SAl Viro if (in.file) { 14432903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 14442903ff01SAl Viro out = fdget(fd_out); 14452903ff01SAl Viro if (out.file) { 14462903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 14472903ff01SAl Viro error = do_splice(in.file, off_in, 14482903ff01SAl Viro out.file, off_out, 1449529565dcSIngo Molnar len, flags); 14502903ff01SAl Viro fdput(out); 14515274f052SJens Axboe } 14525274f052SJens Axboe } 14532903ff01SAl Viro fdput(in); 14545274f052SJens Axboe } 14555274f052SJens Axboe return error; 14565274f052SJens Axboe } 145770524490SJens Axboe 145870524490SJens Axboe /* 1459aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1460aadd06e5SJens Axboe * return an appropriate error. 1461aadd06e5SJens Axboe */ 14627c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1463aadd06e5SJens Axboe { 1464aadd06e5SJens Axboe int ret; 1465aadd06e5SJens Axboe 1466aadd06e5SJens Axboe /* 14678cefc107SDavid Howells * Check the pipe occupancy without the inode lock first. This function 1468aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1469aadd06e5SJens Axboe */ 14708cefc107SDavid Howells if (!pipe_empty(pipe->head, pipe->tail)) 1471aadd06e5SJens Axboe return 0; 1472aadd06e5SJens Axboe 1473aadd06e5SJens Axboe ret = 0; 147461e0d47cSMiklos Szeredi pipe_lock(pipe); 1475aadd06e5SJens Axboe 14768cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 1477aadd06e5SJens Axboe if (signal_pending(current)) { 1478aadd06e5SJens Axboe ret = -ERESTARTSYS; 1479aadd06e5SJens Axboe break; 1480aadd06e5SJens Axboe } 1481aadd06e5SJens Axboe if (!pipe->writers) 1482aadd06e5SJens Axboe break; 1483aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1484aadd06e5SJens Axboe ret = -EAGAIN; 1485aadd06e5SJens Axboe break; 1486aadd06e5SJens Axboe } 1487aadd06e5SJens Axboe pipe_wait(pipe); 1488aadd06e5SJens Axboe } 1489aadd06e5SJens Axboe 149061e0d47cSMiklos Szeredi pipe_unlock(pipe); 1491aadd06e5SJens Axboe return ret; 1492aadd06e5SJens Axboe } 1493aadd06e5SJens Axboe 1494aadd06e5SJens Axboe /* 1495aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1496aadd06e5SJens Axboe * return an appropriate error. 1497aadd06e5SJens Axboe */ 14987c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1499aadd06e5SJens Axboe { 1500aadd06e5SJens Axboe int ret; 1501aadd06e5SJens Axboe 1502aadd06e5SJens Axboe /* 15038cefc107SDavid Howells * Check pipe occupancy without the inode lock first. This function 1504aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1505aadd06e5SJens Axboe */ 15066718b6f8SDavid Howells if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 1507aadd06e5SJens Axboe return 0; 1508aadd06e5SJens Axboe 1509aadd06e5SJens Axboe ret = 0; 151061e0d47cSMiklos Szeredi pipe_lock(pipe); 1511aadd06e5SJens Axboe 15126718b6f8SDavid Howells while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 1513aadd06e5SJens Axboe if (!pipe->readers) { 1514aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1515aadd06e5SJens Axboe ret = -EPIPE; 1516aadd06e5SJens Axboe break; 1517aadd06e5SJens Axboe } 1518aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1519aadd06e5SJens Axboe ret = -EAGAIN; 1520aadd06e5SJens Axboe break; 1521aadd06e5SJens Axboe } 1522aadd06e5SJens Axboe if (signal_pending(current)) { 1523aadd06e5SJens Axboe ret = -ERESTARTSYS; 1524aadd06e5SJens Axboe break; 1525aadd06e5SJens Axboe } 1526aadd06e5SJens Axboe pipe_wait(pipe); 1527aadd06e5SJens Axboe } 1528aadd06e5SJens Axboe 152961e0d47cSMiklos Szeredi pipe_unlock(pipe); 1530aadd06e5SJens Axboe return ret; 1531aadd06e5SJens Axboe } 1532aadd06e5SJens Axboe 1533aadd06e5SJens Axboe /* 15347c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 15357c77f0b3SMiklos Szeredi */ 15367c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 15377c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 15387c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 15397c77f0b3SMiklos Szeredi { 15407c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 15418cefc107SDavid Howells unsigned int i_head, o_head; 15428cefc107SDavid Howells unsigned int i_tail, o_tail; 15438cefc107SDavid Howells unsigned int i_mask, o_mask; 15448cefc107SDavid Howells int ret = 0; 15457c77f0b3SMiklos Szeredi bool input_wakeup = false; 15467c77f0b3SMiklos Szeredi 15477c77f0b3SMiklos Szeredi 15487c77f0b3SMiklos Szeredi retry: 15497c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 15507c77f0b3SMiklos Szeredi if (ret) 15517c77f0b3SMiklos Szeredi return ret; 15527c77f0b3SMiklos Szeredi 15537c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 15547c77f0b3SMiklos Szeredi if (ret) 15557c77f0b3SMiklos Szeredi return ret; 15567c77f0b3SMiklos Szeredi 15577c77f0b3SMiklos Szeredi /* 15587c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 15597c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 15607c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 15617c77f0b3SMiklos Szeredi */ 15627c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 15637c77f0b3SMiklos Szeredi 15648cefc107SDavid Howells i_tail = ipipe->tail; 15658cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 15668cefc107SDavid Howells o_head = opipe->head; 15678cefc107SDavid Howells o_mask = opipe->ring_size - 1; 15688cefc107SDavid Howells 15697c77f0b3SMiklos Szeredi do { 15708cefc107SDavid Howells size_t o_len; 15718cefc107SDavid Howells 15727c77f0b3SMiklos Szeredi if (!opipe->readers) { 15737c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 15747c77f0b3SMiklos Szeredi if (!ret) 15757c77f0b3SMiklos Szeredi ret = -EPIPE; 15767c77f0b3SMiklos Szeredi break; 15777c77f0b3SMiklos Szeredi } 15787c77f0b3SMiklos Szeredi 15798cefc107SDavid Howells i_head = ipipe->head; 15808cefc107SDavid Howells o_tail = opipe->tail; 15818cefc107SDavid Howells 15828cefc107SDavid Howells if (pipe_empty(i_head, i_tail) && !ipipe->writers) 15837c77f0b3SMiklos Szeredi break; 15847c77f0b3SMiklos Szeredi 15857c77f0b3SMiklos Szeredi /* 15867c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 15877c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 15887c77f0b3SMiklos Szeredi */ 15898cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 15906718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) { 15917c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 15927c77f0b3SMiklos Szeredi if (ret) 15937c77f0b3SMiklos Szeredi break; 15947c77f0b3SMiklos Szeredi 15957c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 15967c77f0b3SMiklos Szeredi ret = -EAGAIN; 15977c77f0b3SMiklos Szeredi break; 15987c77f0b3SMiklos Szeredi } 15997c77f0b3SMiklos Szeredi 16007c77f0b3SMiklos Szeredi /* 16017c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 16027c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 16037c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 16047c77f0b3SMiklos Szeredi */ 16057c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 16067c77f0b3SMiklos Szeredi pipe_unlock(opipe); 16077c77f0b3SMiklos Szeredi goto retry; 16087c77f0b3SMiklos Szeredi } 16097c77f0b3SMiklos Szeredi 16108cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 16118cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 16127c77f0b3SMiklos Szeredi 16137c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 16147c77f0b3SMiklos Szeredi /* 16157c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 16167c77f0b3SMiklos Szeredi */ 16177c77f0b3SMiklos Szeredi *obuf = *ibuf; 16187c77f0b3SMiklos Szeredi ibuf->ops = NULL; 16198cefc107SDavid Howells i_tail++; 16208cefc107SDavid Howells ipipe->tail = i_tail; 16217c77f0b3SMiklos Szeredi input_wakeup = true; 16228cefc107SDavid Howells o_len = obuf->len; 16238cefc107SDavid Howells o_head++; 16248cefc107SDavid Howells opipe->head = o_head; 16257c77f0b3SMiklos Szeredi } else { 16267c77f0b3SMiklos Szeredi /* 16277c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 16287c77f0b3SMiklos Szeredi * so we can copy the contents over. 16297c77f0b3SMiklos Szeredi */ 163015fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 163115fab63eSMatthew Wilcox if (ret == 0) 163215fab63eSMatthew Wilcox ret = -EFAULT; 163315fab63eSMatthew Wilcox break; 163415fab63eSMatthew Wilcox } 16357c77f0b3SMiklos Szeredi *obuf = *ibuf; 16367c77f0b3SMiklos Szeredi 16377c77f0b3SMiklos Szeredi /* 16387c77f0b3SMiklos Szeredi * Don't inherit the gift flag, we need to 16397c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 16407c77f0b3SMiklos Szeredi */ 16417c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 16427c77f0b3SMiklos Szeredi 1643a0ce2f0aSJann Horn pipe_buf_mark_unmergeable(obuf); 1644a0ce2f0aSJann Horn 16457c77f0b3SMiklos Szeredi obuf->len = len; 16468cefc107SDavid Howells ibuf->offset += len; 16478cefc107SDavid Howells ibuf->len -= len; 16488cefc107SDavid Howells o_len = len; 16498cefc107SDavid Howells o_head++; 16508cefc107SDavid Howells opipe->head = o_head; 16517c77f0b3SMiklos Szeredi } 16528cefc107SDavid Howells ret += o_len; 16538cefc107SDavid Howells len -= o_len; 16547c77f0b3SMiklos Szeredi } while (len); 16557c77f0b3SMiklos Szeredi 16567c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 16577c77f0b3SMiklos Szeredi pipe_unlock(opipe); 16587c77f0b3SMiklos Szeredi 16597c77f0b3SMiklos Szeredi /* 16607c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 16617c77f0b3SMiklos Szeredi */ 1662825cdcb1SNamhyung Kim if (ret > 0) 1663825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1664825cdcb1SNamhyung Kim 16657c77f0b3SMiklos Szeredi if (input_wakeup) 16667c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 16677c77f0b3SMiklos Szeredi 16687c77f0b3SMiklos Szeredi return ret; 16697c77f0b3SMiklos Szeredi } 16707c77f0b3SMiklos Szeredi 16717c77f0b3SMiklos Szeredi /* 167270524490SJens Axboe * Link contents of ipipe to opipe. 167370524490SJens Axboe */ 167470524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 167570524490SJens Axboe struct pipe_inode_info *opipe, 167670524490SJens Axboe size_t len, unsigned int flags) 167770524490SJens Axboe { 167870524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 16798cefc107SDavid Howells unsigned int i_head, o_head; 16808cefc107SDavid Howells unsigned int i_tail, o_tail; 16818cefc107SDavid Howells unsigned int i_mask, o_mask; 16828cefc107SDavid Howells int ret = 0; 168370524490SJens Axboe 168470524490SJens Axboe /* 168570524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 168661e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 168770524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 168870524490SJens Axboe */ 168961e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 169070524490SJens Axboe 16918cefc107SDavid Howells i_tail = ipipe->tail; 16928cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 16938cefc107SDavid Howells o_head = opipe->head; 16948cefc107SDavid Howells o_mask = opipe->ring_size - 1; 16958cefc107SDavid Howells 1696aadd06e5SJens Axboe do { 169770524490SJens Axboe if (!opipe->readers) { 169870524490SJens Axboe send_sig(SIGPIPE, current, 0); 169970524490SJens Axboe if (!ret) 170070524490SJens Axboe ret = -EPIPE; 170170524490SJens Axboe break; 170270524490SJens Axboe } 170370524490SJens Axboe 17048cefc107SDavid Howells i_head = ipipe->head; 17058cefc107SDavid Howells o_tail = opipe->tail; 17068cefc107SDavid Howells 170770524490SJens Axboe /* 17088cefc107SDavid Howells * If we have iterated all input buffers or run out of 1709aadd06e5SJens Axboe * output room, break. 171070524490SJens Axboe */ 17118cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 17126718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) 1713aadd06e5SJens Axboe break; 1714aadd06e5SJens Axboe 17158cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 17168cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 171770524490SJens Axboe 171870524490SJens Axboe /* 171970524490SJens Axboe * Get a reference to this pipe buffer, 172070524490SJens Axboe * so we can copy the contents over. 172170524490SJens Axboe */ 172215fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 172315fab63eSMatthew Wilcox if (ret == 0) 172415fab63eSMatthew Wilcox ret = -EFAULT; 172515fab63eSMatthew Wilcox break; 172615fab63eSMatthew Wilcox } 172770524490SJens Axboe 172870524490SJens Axboe *obuf = *ibuf; 172970524490SJens Axboe 17307afa6fd0SJens Axboe /* 17317afa6fd0SJens Axboe * Don't inherit the gift flag, we need to 17327afa6fd0SJens Axboe * prevent multiple steals of this page. 17337afa6fd0SJens Axboe */ 17347afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 17357afa6fd0SJens Axboe 1736a0ce2f0aSJann Horn pipe_buf_mark_unmergeable(obuf); 1737a0ce2f0aSJann Horn 173870524490SJens Axboe if (obuf->len > len) 173970524490SJens Axboe obuf->len = len; 174070524490SJens Axboe ret += obuf->len; 174170524490SJens Axboe len -= obuf->len; 17428cefc107SDavid Howells 17438cefc107SDavid Howells o_head++; 17448cefc107SDavid Howells opipe->head = o_head; 17458cefc107SDavid Howells i_tail++; 1746aadd06e5SJens Axboe } while (len); 174770524490SJens Axboe 174861e0d47cSMiklos Szeredi pipe_unlock(ipipe); 174961e0d47cSMiklos Szeredi pipe_unlock(opipe); 175070524490SJens Axboe 1751aadd06e5SJens Axboe /* 1752aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1753aadd06e5SJens Axboe */ 1754825cdcb1SNamhyung Kim if (ret > 0) 1755825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 175670524490SJens Axboe 175770524490SJens Axboe return ret; 175870524490SJens Axboe } 175970524490SJens Axboe 176070524490SJens Axboe /* 176170524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 176270524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 176370524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 176470524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 176570524490SJens Axboe */ 176670524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len, 176770524490SJens Axboe unsigned int flags) 176870524490SJens Axboe { 176971993e62SLinus Torvalds struct pipe_inode_info *ipipe = get_pipe_info(in); 177071993e62SLinus Torvalds struct pipe_inode_info *opipe = get_pipe_info(out); 1771aadd06e5SJens Axboe int ret = -EINVAL; 177270524490SJens Axboe 177370524490SJens Axboe /* 1774aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 1775aadd06e5SJens Axboe * copying the data. 177670524490SJens Axboe */ 1777aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 1778ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1779ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1780ee5e0011SSlavomir Kaslev 1781aadd06e5SJens Axboe /* 1782aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 1783aadd06e5SJens Axboe * ordering doesn't really matter. 1784aadd06e5SJens Axboe */ 17857c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 1786aadd06e5SJens Axboe if (!ret) { 17877c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 178802cf01aeSJens Axboe if (!ret) 1789aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 1790aadd06e5SJens Axboe } 1791aadd06e5SJens Axboe } 179270524490SJens Axboe 1793aadd06e5SJens Axboe return ret; 179470524490SJens Axboe } 179570524490SJens Axboe 1796836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 179770524490SJens Axboe { 17982903ff01SAl Viro struct fd in; 17992903ff01SAl Viro int error; 180070524490SJens Axboe 18013d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 18023d6ea290SAl Viro return -EINVAL; 18033d6ea290SAl Viro 180470524490SJens Axboe if (unlikely(!len)) 180570524490SJens Axboe return 0; 180670524490SJens Axboe 180770524490SJens Axboe error = -EBADF; 18082903ff01SAl Viro in = fdget(fdin); 18092903ff01SAl Viro if (in.file) { 18102903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 18112903ff01SAl Viro struct fd out = fdget(fdout); 18122903ff01SAl Viro if (out.file) { 18132903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 18142903ff01SAl Viro error = do_tee(in.file, out.file, 18152903ff01SAl Viro len, flags); 18162903ff01SAl Viro fdput(out); 181770524490SJens Axboe } 181870524490SJens Axboe } 18192903ff01SAl Viro fdput(in); 182070524490SJens Axboe } 182170524490SJens Axboe 182270524490SJens Axboe return error; 182370524490SJens Axboe } 1824