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 */ 47c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_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; 79c928f642SChristoph Hellwig return true; 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); 89c928f642SChristoph Hellwig return false; 909e94cd4fSJens Axboe } 915abc97aaSJens Axboe 9276ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 935274f052SJens Axboe struct pipe_buffer *buf) 945274f052SJens Axboe { 9509cbfeafSKirill A. Shutemov put_page(buf->page); 961432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 975274f052SJens Axboe } 985274f052SJens Axboe 990845718dSJens Axboe /* 1000845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 1010845718dSJens Axboe * is a page cache page, IO may be in flight. 1020845718dSJens Axboe */ 103cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 1045274f052SJens Axboe struct pipe_buffer *buf) 1055274f052SJens Axboe { 1065274f052SJens Axboe struct page *page = buf->page; 10749d0b21bSJens Axboe int err; 1085274f052SJens Axboe 1095274f052SJens Axboe if (!PageUptodate(page)) { 11049d0b21bSJens Axboe lock_page(page); 1115274f052SJens Axboe 11249d0b21bSJens Axboe /* 11349d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 11473d62d83SIngo Molnar * splice, if this is the first page. 11549d0b21bSJens Axboe */ 1165274f052SJens Axboe if (!page->mapping) { 11749d0b21bSJens Axboe err = -ENODATA; 11849d0b21bSJens Axboe goto error; 1195274f052SJens Axboe } 1205274f052SJens Axboe 12149d0b21bSJens Axboe /* 12273d62d83SIngo Molnar * Uh oh, read-error from disk. 12349d0b21bSJens Axboe */ 12449d0b21bSJens Axboe if (!PageUptodate(page)) { 12549d0b21bSJens Axboe err = -EIO; 12649d0b21bSJens Axboe goto error; 12749d0b21bSJens Axboe } 12849d0b21bSJens Axboe 12949d0b21bSJens Axboe /* 130f84d7519SJens Axboe * Page is ok afterall, we are done. 13149d0b21bSJens Axboe */ 13249d0b21bSJens Axboe unlock_page(page); 13349d0b21bSJens Axboe } 13449d0b21bSJens Axboe 135f84d7519SJens Axboe return 0; 13649d0b21bSJens Axboe error: 13749d0b21bSJens Axboe unlock_page(page); 138f84d7519SJens Axboe return err; 13970524490SJens Axboe } 14070524490SJens Axboe 141708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 142cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1435274f052SJens Axboe .release = page_cache_pipe_buf_release, 144c928f642SChristoph Hellwig .try_steal = page_cache_pipe_buf_try_steal, 145f84d7519SJens Axboe .get = generic_pipe_buf_get, 1465274f052SJens Axboe }; 1475274f052SJens Axboe 148c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe, 149912d35f8SJens Axboe struct pipe_buffer *buf) 150912d35f8SJens Axboe { 1517afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 152c928f642SChristoph Hellwig return false; 1537afa6fd0SJens Axboe 1541432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 155c928f642SChristoph Hellwig return generic_pipe_buf_try_steal(pipe, buf); 156912d35f8SJens Axboe } 157912d35f8SJens Axboe 158d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 159912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 160c928f642SChristoph Hellwig .try_steal = user_page_pipe_buf_try_steal, 161f84d7519SJens Axboe .get = generic_pipe_buf_get, 162912d35f8SJens Axboe }; 163912d35f8SJens Axboe 164825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 165825cdcb1SNamhyung Kim { 166825cdcb1SNamhyung Kim smp_mb(); 1670ddad21dSLinus Torvalds if (waitqueue_active(&pipe->rd_wait)) 1680ddad21dSLinus Torvalds wake_up_interruptible(&pipe->rd_wait); 169825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 170825cdcb1SNamhyung Kim } 171825cdcb1SNamhyung Kim 172932cc6d4SJens Axboe /** 173932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 174932cc6d4SJens Axboe * @pipe: pipe to fill 175932cc6d4SJens Axboe * @spd: data to fill 176932cc6d4SJens Axboe * 177932cc6d4SJens Axboe * Description: 17879685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 179932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 180932cc6d4SJens Axboe * function will link that data to the pipe. 181932cc6d4SJens Axboe * 18283f9135bSJens Axboe */ 183d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 184912d35f8SJens Axboe struct splice_pipe_desc *spd) 1855274f052SJens Axboe { 18600de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 1878cefc107SDavid Howells unsigned int tail = pipe->tail; 1888cefc107SDavid Howells unsigned int head = pipe->head; 1898cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 1908924feffSAl Viro int ret = 0, page_nr = 0; 1915274f052SJens Axboe 192d6785d91SRabin Vincent if (!spd_pages) 193d6785d91SRabin Vincent return 0; 194d6785d91SRabin Vincent 1958924feffSAl Viro if (unlikely(!pipe->readers)) { 1965274f052SJens Axboe send_sig(SIGPIPE, current, 0); 1975274f052SJens Axboe ret = -EPIPE; 1988924feffSAl Viro goto out; 1995274f052SJens Axboe } 2005274f052SJens Axboe 2016718b6f8SDavid Howells while (!pipe_full(head, tail, pipe->max_usage)) { 2028cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[head & mask]; 2035274f052SJens Axboe 204912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 205912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 206912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 207497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 208912d35f8SJens Axboe buf->ops = spd->ops; 2095a81e6a1SMiklos Szeredi buf->flags = 0; 2107afa6fd0SJens Axboe 2118cefc107SDavid Howells head++; 2128cefc107SDavid Howells pipe->head = head; 213912d35f8SJens Axboe page_nr++; 214912d35f8SJens Axboe ret += buf->len; 215912d35f8SJens Axboe 216912d35f8SJens Axboe if (!--spd->nr_pages) 2175274f052SJens Axboe break; 2185274f052SJens Axboe } 2195274f052SJens Axboe 22029e35094SLinus Torvalds if (!ret) 22129e35094SLinus Torvalds ret = -EAGAIN; 22229e35094SLinus Torvalds 2238924feffSAl Viro out: 22400de00bdSJens Axboe while (page_nr < spd_pages) 225bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2265274f052SJens Axboe 2275274f052SJens Axboe return ret; 2285274f052SJens Axboe } 2292b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe); 2305274f052SJens Axboe 23179fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) 23279fddc4eSAl Viro { 2338cefc107SDavid Howells unsigned int head = pipe->head; 2348cefc107SDavid Howells unsigned int tail = pipe->tail; 2358cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 23679fddc4eSAl Viro int ret; 23779fddc4eSAl Viro 23879fddc4eSAl Viro if (unlikely(!pipe->readers)) { 23979fddc4eSAl Viro send_sig(SIGPIPE, current, 0); 24079fddc4eSAl Viro ret = -EPIPE; 2416718b6f8SDavid Howells } else if (pipe_full(head, tail, pipe->max_usage)) { 24279fddc4eSAl Viro ret = -EAGAIN; 24379fddc4eSAl Viro } else { 2448cefc107SDavid Howells pipe->bufs[head & mask] = *buf; 2458cefc107SDavid Howells pipe->head = head + 1; 24679fddc4eSAl Viro return buf->len; 24779fddc4eSAl Viro } 248a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 24979fddc4eSAl Viro return ret; 25079fddc4eSAl Viro } 25179fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe); 25279fddc4eSAl Viro 25335f3d14dSJens Axboe /* 25435f3d14dSJens Axboe * Check if we need to grow the arrays holding pages and partial page 25535f3d14dSJens Axboe * descriptions. 25635f3d14dSJens Axboe */ 257047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 25835f3d14dSJens Axboe { 2596718b6f8SDavid Howells unsigned int max_usage = READ_ONCE(pipe->max_usage); 260047fe360SEric Dumazet 2618cefc107SDavid Howells spd->nr_pages_max = max_usage; 2628cefc107SDavid Howells if (max_usage <= PIPE_DEF_BUFFERS) 26335f3d14dSJens Axboe return 0; 26435f3d14dSJens Axboe 2658cefc107SDavid Howells spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL); 2668cefc107SDavid Howells spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page), 2676da2ec56SKees Cook GFP_KERNEL); 26835f3d14dSJens Axboe 26935f3d14dSJens Axboe if (spd->pages && spd->partial) 27035f3d14dSJens Axboe return 0; 27135f3d14dSJens Axboe 27235f3d14dSJens Axboe kfree(spd->pages); 27335f3d14dSJens Axboe kfree(spd->partial); 27435f3d14dSJens Axboe return -ENOMEM; 27535f3d14dSJens Axboe } 27635f3d14dSJens Axboe 277047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd) 27835f3d14dSJens Axboe { 279047fe360SEric Dumazet if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 28035f3d14dSJens Axboe return; 28135f3d14dSJens Axboe 28235f3d14dSJens Axboe kfree(spd->pages); 28335f3d14dSJens Axboe kfree(spd->partial); 28435f3d14dSJens Axboe } 28535f3d14dSJens Axboe 28683f9135bSJens Axboe /** 28783f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 28883f9135bSJens Axboe * @in: file to splice from 289932cc6d4SJens Axboe * @ppos: position in @in 29083f9135bSJens Axboe * @pipe: pipe to splice to 29183f9135bSJens Axboe * @len: number of bytes to splice 29283f9135bSJens Axboe * @flags: splice modifier flags 29383f9135bSJens Axboe * 294932cc6d4SJens Axboe * Description: 295932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 29682c156f8SAl Viro * used as long as it has more or less sane ->read_iter(). 297932cc6d4SJens Axboe * 29883f9135bSJens Axboe */ 299cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 300cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 301cbb7e577SJens Axboe unsigned int flags) 3025274f052SJens Axboe { 30382c156f8SAl Viro struct iov_iter to; 30482c156f8SAl Viro struct kiocb kiocb; 3058cefc107SDavid Howells unsigned int i_head; 3068cefc107SDavid Howells int ret; 307be64f884SBoaz Harrosh 308aa563d7bSDavid Howells iov_iter_pipe(&to, READ, pipe, len); 3098cefc107SDavid Howells i_head = to.head; 31082c156f8SAl Viro init_sync_kiocb(&kiocb, in); 31182c156f8SAl Viro kiocb.ki_pos = *ppos; 312bb7462b6SMiklos Szeredi ret = call_read_iter(in, &kiocb, &to); 313723590edSMiklos Szeredi if (ret > 0) { 31482c156f8SAl Viro *ppos = kiocb.ki_pos; 315723590edSMiklos Szeredi file_accessed(in); 31682c156f8SAl Viro } else if (ret < 0) { 3178cefc107SDavid Howells to.head = i_head; 31882c156f8SAl Viro to.iov_offset = 0; 31982c156f8SAl Viro iov_iter_advance(&to, 0); /* to free what was emitted */ 32082c156f8SAl Viro /* 32182c156f8SAl Viro * callers of ->splice_read() expect -EAGAIN on 32282c156f8SAl Viro * "can't put anything in there", rather than -EFAULT. 32382c156f8SAl Viro */ 32482c156f8SAl Viro if (ret == -EFAULT) 32582c156f8SAl Viro ret = -EAGAIN; 326723590edSMiklos Szeredi } 3275274f052SJens Axboe 3285274f052SJens Axboe return ret; 3295274f052SJens Axboe } 330059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 331059a8f37SJens Axboe 332241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = { 3336818173bSMiklos Szeredi .release = generic_pipe_buf_release, 334c928f642SChristoph Hellwig .try_steal = generic_pipe_buf_try_steal, 3356818173bSMiklos Szeredi .get = generic_pipe_buf_get, 3366818173bSMiklos Szeredi }; 3376818173bSMiklos Szeredi 33828a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 33928a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 34028a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 34128a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 34228a625cbSMiklos Szeredi }; 34328a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 34428a625cbSMiklos Szeredi 3455274f052SJens Axboe /* 3464f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 347016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 3485274f052SJens Axboe */ 34976ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 3505274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 3515274f052SJens Axboe { 3526a14b90bSJens Axboe struct file *file = sd->u.file; 3535274f052SJens Axboe loff_t pos = sd->pos; 354a8adbe37SMichał Mirosław int more; 3555274f052SJens Axboe 35672c2d531SAl Viro if (!likely(file->f_op->sendpage)) 357a8adbe37SMichał Mirosław return -EINVAL; 358a8adbe37SMichał Mirosław 35935f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 360ae62ca7bSEric Dumazet 3618cefc107SDavid Howells if (sd->len < sd->total_len && 3628cefc107SDavid Howells pipe_occupancy(pipe->head, pipe->tail) > 1) 36335f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 364ae62ca7bSEric Dumazet 365a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 366f84d7519SJens Axboe sd->len, &pos, more); 3675274f052SJens Axboe } 3685274f052SJens Axboe 369b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 370b3c2d2ddSMiklos Szeredi { 371b3c2d2ddSMiklos Szeredi smp_mb(); 3720ddad21dSLinus Torvalds if (waitqueue_active(&pipe->wr_wait)) 3730ddad21dSLinus Torvalds wake_up_interruptible(&pipe->wr_wait); 374b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 375b3c2d2ddSMiklos Szeredi } 376b3c2d2ddSMiklos Szeredi 377b3c2d2ddSMiklos Szeredi /** 378b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 379b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 380b3c2d2ddSMiklos Szeredi * @sd: information to @actor 381b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 382b3c2d2ddSMiklos Szeredi * 383b3c2d2ddSMiklos Szeredi * Description: 384b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 385b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 386b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 387b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 388b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 389b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 390b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 391b3c2d2ddSMiklos Szeredi * 392b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 393b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 394b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 395b3c2d2ddSMiklos Szeredi * destination. 396b3c2d2ddSMiklos Szeredi */ 39796f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 398b3c2d2ddSMiklos Szeredi splice_actor *actor) 399b3c2d2ddSMiklos Szeredi { 4008cefc107SDavid Howells unsigned int head = pipe->head; 4018cefc107SDavid Howells unsigned int tail = pipe->tail; 4028cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 403b3c2d2ddSMiklos Szeredi int ret; 404b3c2d2ddSMiklos Szeredi 405ec057595SLinus Torvalds while (!pipe_empty(head, tail)) { 4068cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 407b3c2d2ddSMiklos Szeredi 408b3c2d2ddSMiklos Szeredi sd->len = buf->len; 409b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 410b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 411b3c2d2ddSMiklos Szeredi 412fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 413a8adbe37SMichał Mirosław if (unlikely(ret)) { 414b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 415b3c2d2ddSMiklos Szeredi ret = 0; 416b3c2d2ddSMiklos Szeredi return ret; 417b3c2d2ddSMiklos Szeredi } 418a8adbe37SMichał Mirosław 419a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 420a8adbe37SMichał Mirosław if (ret <= 0) 421a8adbe37SMichał Mirosław return ret; 422a8adbe37SMichał Mirosław 423b3c2d2ddSMiklos Szeredi buf->offset += ret; 424b3c2d2ddSMiklos Szeredi buf->len -= ret; 425b3c2d2ddSMiklos Szeredi 426b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 427b3c2d2ddSMiklos Szeredi sd->len -= ret; 428b3c2d2ddSMiklos Szeredi sd->pos += ret; 429b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 430b3c2d2ddSMiklos Szeredi 431b3c2d2ddSMiklos Szeredi if (!buf->len) { 432a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 4338cefc107SDavid Howells tail++; 4348cefc107SDavid Howells pipe->tail = tail; 4356447a3cfSAl Viro if (pipe->files) 436b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 437b3c2d2ddSMiklos Szeredi } 438b3c2d2ddSMiklos Szeredi 439b3c2d2ddSMiklos Szeredi if (!sd->total_len) 440b3c2d2ddSMiklos Szeredi return 0; 441b3c2d2ddSMiklos Szeredi } 442b3c2d2ddSMiklos Szeredi 443b3c2d2ddSMiklos Szeredi return 1; 444b3c2d2ddSMiklos Szeredi } 445b3c2d2ddSMiklos Szeredi 446b3c2d2ddSMiklos Szeredi /** 447b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 448b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 449b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 450b3c2d2ddSMiklos Szeredi * 451b3c2d2ddSMiklos Szeredi * Description: 452b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 453b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 454b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 455b3c2d2ddSMiklos Szeredi */ 45696f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 457b3c2d2ddSMiklos Szeredi { 458c725bfceSJan Kara /* 459c725bfceSJan Kara * Check for signal early to make process killable when there are 460c725bfceSJan Kara * always buffers available 461c725bfceSJan Kara */ 462c725bfceSJan Kara if (signal_pending(current)) 463c725bfceSJan Kara return -ERESTARTSYS; 464c725bfceSJan Kara 4658cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 466b3c2d2ddSMiklos Szeredi if (!pipe->writers) 467b3c2d2ddSMiklos Szeredi return 0; 468b3c2d2ddSMiklos Szeredi 469a28c8b9dSLinus Torvalds if (sd->num_spliced) 470b3c2d2ddSMiklos Szeredi return 0; 471b3c2d2ddSMiklos Szeredi 472b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 473b3c2d2ddSMiklos Szeredi return -EAGAIN; 474b3c2d2ddSMiklos Szeredi 475b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 476b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 477b3c2d2ddSMiklos Szeredi 478b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 479b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 480b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 481b3c2d2ddSMiklos Szeredi } 482b3c2d2ddSMiklos Szeredi 483b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 484b3c2d2ddSMiklos Szeredi } 485b3c2d2ddSMiklos Szeredi 486b3c2d2ddSMiklos Szeredi return 1; 487b3c2d2ddSMiklos Szeredi } 488b3c2d2ddSMiklos Szeredi 489b3c2d2ddSMiklos Szeredi /** 490b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 491b80901bbSRandy Dunlap * @sd: information about the splice operation 492b3c2d2ddSMiklos Szeredi * 493b3c2d2ddSMiklos Szeredi * Description: 494b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 495b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 496b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 497b3c2d2ddSMiklos Szeredi */ 49896f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd) 499b3c2d2ddSMiklos Szeredi { 500b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 501b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 502b3c2d2ddSMiklos Szeredi } 503b3c2d2ddSMiklos Szeredi 504b3c2d2ddSMiklos Szeredi /** 505b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 506b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 507b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 508b3c2d2ddSMiklos Szeredi * 509b3c2d2ddSMiklos Szeredi * Description: 510b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 511b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 512b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 513b3c2d2ddSMiklos Szeredi */ 51496f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 515b3c2d2ddSMiklos Szeredi { 516b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 517b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 518b3c2d2ddSMiklos Szeredi } 519b3c2d2ddSMiklos Szeredi 520932cc6d4SJens Axboe /** 521932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 522932cc6d4SJens Axboe * @pipe: pipe to splice from 523932cc6d4SJens Axboe * @sd: information to @actor 524932cc6d4SJens Axboe * @actor: handler that splices the data 525932cc6d4SJens Axboe * 526932cc6d4SJens Axboe * Description: 527932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 528932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 529932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 530932cc6d4SJens Axboe * pipe_to_user. 531932cc6d4SJens Axboe * 53283f9135bSJens Axboe */ 533c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 534c66ab6faSJens Axboe splice_actor *actor) 5355274f052SJens Axboe { 536b3c2d2ddSMiklos Szeredi int ret; 5375274f052SJens Axboe 538b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 539b3c2d2ddSMiklos Szeredi do { 540c2489e07SJan Kara cond_resched(); 541b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 542b3c2d2ddSMiklos Szeredi if (ret > 0) 543b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 544b3c2d2ddSMiklos Szeredi } while (ret > 0); 545b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 5465274f052SJens Axboe 547b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 5485274f052SJens Axboe } 54940bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 5505274f052SJens Axboe 551932cc6d4SJens Axboe /** 552932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 553932cc6d4SJens Axboe * @pipe: pipe to splice from 554932cc6d4SJens Axboe * @out: file to splice to 555932cc6d4SJens Axboe * @ppos: position in @out 556932cc6d4SJens Axboe * @len: how many bytes to splice 557932cc6d4SJens Axboe * @flags: splice modifier flags 558932cc6d4SJens Axboe * @actor: handler that splices the data 559932cc6d4SJens Axboe * 560932cc6d4SJens Axboe * Description: 5612933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 562932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 563932cc6d4SJens Axboe * 564932cc6d4SJens Axboe */ 5656da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 5666da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 5676da61809SMark Fasheh splice_actor *actor) 5686da61809SMark Fasheh { 5696da61809SMark Fasheh ssize_t ret; 570c66ab6faSJens Axboe struct splice_desc sd = { 571c66ab6faSJens Axboe .total_len = len, 572c66ab6faSJens Axboe .flags = flags, 573c66ab6faSJens Axboe .pos = *ppos, 5746a14b90bSJens Axboe .u.file = out, 575c66ab6faSJens Axboe }; 5766da61809SMark Fasheh 57761e0d47cSMiklos Szeredi pipe_lock(pipe); 578c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 57961e0d47cSMiklos Szeredi pipe_unlock(pipe); 5806da61809SMark Fasheh 5816da61809SMark Fasheh return ret; 5826da61809SMark Fasheh } 5836da61809SMark Fasheh 5846da61809SMark Fasheh /** 5858d020765SAl Viro * iter_file_splice_write - splice data from a pipe to a file 5868d020765SAl Viro * @pipe: pipe info 5878d020765SAl Viro * @out: file to write to 5888d020765SAl Viro * @ppos: position in @out 5898d020765SAl Viro * @len: number of bytes to splice 5908d020765SAl Viro * @flags: splice modifier flags 5918d020765SAl Viro * 5928d020765SAl Viro * Description: 5938d020765SAl Viro * Will either move or copy pages (determined by @flags options) from 5948d020765SAl Viro * the given pipe inode to the given file. 5958d020765SAl Viro * This one is ->write_iter-based. 5968d020765SAl Viro * 5978d020765SAl Viro */ 5988d020765SAl Viro ssize_t 5998d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 6008d020765SAl Viro loff_t *ppos, size_t len, unsigned int flags) 6018d020765SAl Viro { 6028d020765SAl Viro struct splice_desc sd = { 6038d020765SAl Viro .total_len = len, 6048d020765SAl Viro .flags = flags, 6058d020765SAl Viro .pos = *ppos, 6068d020765SAl Viro .u.file = out, 6078d020765SAl Viro }; 6086718b6f8SDavid Howells int nbufs = pipe->max_usage; 6098d020765SAl Viro struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 6108d020765SAl Viro GFP_KERNEL); 6118d020765SAl Viro ssize_t ret; 6128d020765SAl Viro 6138d020765SAl Viro if (unlikely(!array)) 6148d020765SAl Viro return -ENOMEM; 6158d020765SAl Viro 6168d020765SAl Viro pipe_lock(pipe); 6178d020765SAl Viro 6188d020765SAl Viro splice_from_pipe_begin(&sd); 6198d020765SAl Viro while (sd.total_len) { 6208d020765SAl Viro struct iov_iter from; 621ec057595SLinus Torvalds unsigned int head, tail, mask; 6228d020765SAl Viro size_t left; 6238cefc107SDavid Howells int n; 6248d020765SAl Viro 6258d020765SAl Viro ret = splice_from_pipe_next(pipe, &sd); 6268d020765SAl Viro if (ret <= 0) 6278d020765SAl Viro break; 6288d020765SAl Viro 6296718b6f8SDavid Howells if (unlikely(nbufs < pipe->max_usage)) { 6308d020765SAl Viro kfree(array); 6316718b6f8SDavid Howells nbufs = pipe->max_usage; 6328d020765SAl Viro array = kcalloc(nbufs, sizeof(struct bio_vec), 6338d020765SAl Viro GFP_KERNEL); 6348d020765SAl Viro if (!array) { 6358d020765SAl Viro ret = -ENOMEM; 6368d020765SAl Viro break; 6378d020765SAl Viro } 6388d020765SAl Viro } 6398d020765SAl Viro 640ec057595SLinus Torvalds head = pipe->head; 641ec057595SLinus Torvalds tail = pipe->tail; 642ec057595SLinus Torvalds mask = pipe->ring_size - 1; 643ec057595SLinus Torvalds 6448d020765SAl Viro /* build the vector */ 6458d020765SAl Viro left = sd.total_len; 6468cefc107SDavid Howells for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) { 6478cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 6488d020765SAl Viro size_t this_len = buf->len; 6498d020765SAl Viro 6508d020765SAl Viro if (this_len > left) 6518d020765SAl Viro this_len = left; 6528d020765SAl Viro 653fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 6548d020765SAl Viro if (unlikely(ret)) { 6558d020765SAl Viro if (ret == -ENODATA) 6568d020765SAl Viro ret = 0; 6578d020765SAl Viro goto done; 6588d020765SAl Viro } 6598d020765SAl Viro 6608d020765SAl Viro array[n].bv_page = buf->page; 6618d020765SAl Viro array[n].bv_len = this_len; 6628d020765SAl Viro array[n].bv_offset = buf->offset; 6638d020765SAl Viro left -= this_len; 6648d020765SAl Viro } 6658d020765SAl Viro 666aa563d7bSDavid Howells iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left); 667abbb6589SChristoph Hellwig ret = vfs_iter_write(out, &from, &sd.pos, 0); 6688d020765SAl Viro if (ret <= 0) 6698d020765SAl Viro break; 6708d020765SAl Viro 6718d020765SAl Viro sd.num_spliced += ret; 6728d020765SAl Viro sd.total_len -= ret; 673dbe4e192SChristoph Hellwig *ppos = sd.pos; 6748d020765SAl Viro 6758d020765SAl Viro /* dismiss the fully eaten buffers, adjust the partial one */ 6768cefc107SDavid Howells tail = pipe->tail; 6778d020765SAl Viro while (ret) { 6788cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 6798d020765SAl Viro if (ret >= buf->len) { 6808d020765SAl Viro ret -= buf->len; 6818d020765SAl Viro buf->len = 0; 682a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 6838cefc107SDavid Howells tail++; 6848cefc107SDavid Howells pipe->tail = tail; 6858d020765SAl Viro if (pipe->files) 6868d020765SAl Viro sd.need_wakeup = true; 6878d020765SAl Viro } else { 6888d020765SAl Viro buf->offset += ret; 6898d020765SAl Viro buf->len -= ret; 6908d020765SAl Viro ret = 0; 6918d020765SAl Viro } 6928d020765SAl Viro } 6938d020765SAl Viro } 6948d020765SAl Viro done: 6958d020765SAl Viro kfree(array); 6968d020765SAl Viro splice_from_pipe_end(pipe, &sd); 6978d020765SAl Viro 6988d020765SAl Viro pipe_unlock(pipe); 6998d020765SAl Viro 7008d020765SAl Viro if (sd.num_spliced) 7018d020765SAl Viro ret = sd.num_spliced; 7028d020765SAl Viro 7038d020765SAl Viro return ret; 7048d020765SAl Viro } 7058d020765SAl Viro 7068d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write); 7078d020765SAl Viro 70883f9135bSJens Axboe /** 70983f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 710932cc6d4SJens Axboe * @pipe: pipe to splice from 71183f9135bSJens Axboe * @out: socket to write to 712932cc6d4SJens Axboe * @ppos: position in @out 71383f9135bSJens Axboe * @len: number of bytes to splice 71483f9135bSJens Axboe * @flags: splice modifier flags 71583f9135bSJens Axboe * 716932cc6d4SJens Axboe * Description: 71783f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 71883f9135bSJens Axboe * is involved. 71983f9135bSJens Axboe * 72083f9135bSJens Axboe */ 7213a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 722cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 7235274f052SJens Axboe { 72400522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 7255274f052SJens Axboe } 7265274f052SJens Axboe 727059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 728a0f06780SJeff Garzik 72936e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op) 73036e2c742SChristoph Hellwig { 73136e2c742SChristoph Hellwig pr_debug_ratelimited( 73236e2c742SChristoph Hellwig "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n", 73336e2c742SChristoph Hellwig op, file, current->pid, current->comm); 73436e2c742SChristoph Hellwig return -EINVAL; 73536e2c742SChristoph Hellwig } 73636e2c742SChristoph Hellwig 73783f9135bSJens Axboe /* 73883f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 73983f9135bSJens Axboe */ 7403a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 741cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 7425274f052SJens Axboe { 74336e2c742SChristoph Hellwig if (unlikely(!out->f_op->splice_write)) 74436e2c742SChristoph Hellwig return warn_unsupported(out, "write"); 74500c285d0SChristoph Hellwig return out->f_op->splice_write(pipe, out, ppos, len, flags); 7465274f052SJens Axboe } 7475274f052SJens Axboe 74883f9135bSJens Axboe /* 74983f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 75083f9135bSJens Axboe */ 751cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 752cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 753cbb7e577SJens Axboe unsigned int flags) 7545274f052SJens Axboe { 7555274f052SJens Axboe int ret; 7565274f052SJens Axboe 75749570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 7585274f052SJens Axboe return -EBADF; 7595274f052SJens Axboe 760cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 7615274f052SJens Axboe if (unlikely(ret < 0)) 7625274f052SJens Axboe return ret; 7635274f052SJens Axboe 76403cc0789SAl Viro if (unlikely(len > MAX_RW_COUNT)) 76503cc0789SAl Viro len = MAX_RW_COUNT; 76603cc0789SAl Viro 76736e2c742SChristoph Hellwig if (unlikely(!in->f_op->splice_read)) 76836e2c742SChristoph Hellwig return warn_unsupported(in, "read"); 7692bc01060SChristoph Hellwig return in->f_op->splice_read(in, ppos, pipe, len, flags); 7705274f052SJens Axboe } 7715274f052SJens Axboe 772932cc6d4SJens Axboe /** 773932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 774932cc6d4SJens Axboe * @in: file to splice from 775932cc6d4SJens Axboe * @sd: actor information on where to splice to 776932cc6d4SJens Axboe * @actor: handles the data splicing 777932cc6d4SJens Axboe * 778932cc6d4SJens Axboe * Description: 779932cc6d4SJens Axboe * This is a special case helper to splice directly between two 780932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 781932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 782932cc6d4SJens Axboe * that process. 783932cc6d4SJens Axboe * 784c66ab6faSJens Axboe */ 785c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 786c66ab6faSJens Axboe splice_direct_actor *actor) 787b92ce558SJens Axboe { 788b92ce558SJens Axboe struct pipe_inode_info *pipe; 789b92ce558SJens Axboe long ret, bytes; 790b92ce558SJens Axboe umode_t i_mode; 791c66ab6faSJens Axboe size_t len; 7920ff28d9fSChristophe Leroy int i, flags, more; 793b92ce558SJens Axboe 794b92ce558SJens Axboe /* 795b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 796b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 797b92ce558SJens Axboe * piped splicing for that! 798b92ce558SJens Axboe */ 799496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 800b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 801b92ce558SJens Axboe return -EINVAL; 802b92ce558SJens Axboe 803b92ce558SJens Axboe /* 804b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 805b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 806b92ce558SJens Axboe */ 807b92ce558SJens Axboe pipe = current->splice_pipe; 80849570e9bSJens Axboe if (unlikely(!pipe)) { 8097bee130eSAl Viro pipe = alloc_pipe_info(); 810b92ce558SJens Axboe if (!pipe) 811b92ce558SJens Axboe return -ENOMEM; 812b92ce558SJens Axboe 813b92ce558SJens Axboe /* 814b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 81500522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 816b92ce558SJens Axboe * PIPE_READERS appropriately. 817b92ce558SJens Axboe */ 818b92ce558SJens Axboe pipe->readers = 1; 819b92ce558SJens Axboe 820b92ce558SJens Axboe current->splice_pipe = pipe; 821b92ce558SJens Axboe } 822b92ce558SJens Axboe 823b92ce558SJens Axboe /* 82473d62d83SIngo Molnar * Do the splice. 825b92ce558SJens Axboe */ 826b92ce558SJens Axboe ret = 0; 827b92ce558SJens Axboe bytes = 0; 828c66ab6faSJens Axboe len = sd->total_len; 829c66ab6faSJens Axboe flags = sd->flags; 830c66ab6faSJens Axboe 831c66ab6faSJens Axboe /* 832c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 833c66ab6faSJens Axboe */ 834c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 8350ff28d9fSChristophe Leroy more = sd->flags & SPLICE_F_MORE; 836b92ce558SJens Axboe 8378cefc107SDavid Howells WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); 83817614445SDarrick J. Wong 839b92ce558SJens Axboe while (len) { 8408cefc107SDavid Howells unsigned int p_space; 84151a92c0fSJens Axboe size_t read_len; 842a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 843b92ce558SJens Axboe 84417614445SDarrick J. Wong /* Don't try to read more the pipe has space for. */ 8456718b6f8SDavid Howells p_space = pipe->max_usage - 8468cefc107SDavid Howells pipe_occupancy(pipe->head, pipe->tail); 8478cefc107SDavid Howells read_len = min_t(size_t, len, p_space << PAGE_SHIFT); 84817614445SDarrick J. Wong ret = do_splice_to(in, &pos, pipe, read_len, flags); 84951a92c0fSJens Axboe if (unlikely(ret <= 0)) 850b92ce558SJens Axboe goto out_release; 851b92ce558SJens Axboe 852b92ce558SJens Axboe read_len = ret; 853c66ab6faSJens Axboe sd->total_len = read_len; 854b92ce558SJens Axboe 855b92ce558SJens Axboe /* 8560ff28d9fSChristophe Leroy * If more data is pending, set SPLICE_F_MORE 8570ff28d9fSChristophe Leroy * If this is the last data and SPLICE_F_MORE was not set 8580ff28d9fSChristophe Leroy * initially, clears it. 8590ff28d9fSChristophe Leroy */ 8600ff28d9fSChristophe Leroy if (read_len < len) 8610ff28d9fSChristophe Leroy sd->flags |= SPLICE_F_MORE; 8620ff28d9fSChristophe Leroy else if (!more) 8630ff28d9fSChristophe Leroy sd->flags &= ~SPLICE_F_MORE; 8640ff28d9fSChristophe Leroy /* 865b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 866b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 867b92ce558SJens Axboe * could get stuck data in the internal pipe: 868b92ce558SJens Axboe */ 869c66ab6faSJens Axboe ret = actor(pipe, sd); 870a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 871a82c53a0STom Zanussi sd->pos = prev_pos; 872b92ce558SJens Axboe goto out_release; 873a82c53a0STom Zanussi } 874b92ce558SJens Axboe 875b92ce558SJens Axboe bytes += ret; 876b92ce558SJens Axboe len -= ret; 877bcd4f3acSJens Axboe sd->pos = pos; 878b92ce558SJens Axboe 879a82c53a0STom Zanussi if (ret < read_len) { 880a82c53a0STom Zanussi sd->pos = prev_pos + ret; 88151a92c0fSJens Axboe goto out_release; 882b92ce558SJens Axboe } 883a82c53a0STom Zanussi } 884b92ce558SJens Axboe 8859e97198dSJens Axboe done: 8868cefc107SDavid Howells pipe->tail = pipe->head = 0; 8879e97198dSJens Axboe file_accessed(in); 888b92ce558SJens Axboe return bytes; 889b92ce558SJens Axboe 890b92ce558SJens Axboe out_release: 891b92ce558SJens Axboe /* 892b92ce558SJens Axboe * If we did an incomplete transfer we must release 893b92ce558SJens Axboe * the pipe buffers in question: 894b92ce558SJens Axboe */ 8958cefc107SDavid Howells for (i = 0; i < pipe->ring_size; i++) { 8968cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[i]; 897b92ce558SJens Axboe 898a779638cSMiklos Szeredi if (buf->ops) 899a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 900b92ce558SJens Axboe } 901b92ce558SJens Axboe 9029e97198dSJens Axboe if (!bytes) 9039e97198dSJens Axboe bytes = ret; 904b92ce558SJens Axboe 9059e97198dSJens Axboe goto done; 906c66ab6faSJens Axboe } 907c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 908c66ab6faSJens Axboe 909c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 910c66ab6faSJens Axboe struct splice_desc *sd) 911c66ab6faSJens Axboe { 9126a14b90bSJens Axboe struct file *file = sd->u.file; 913c66ab6faSJens Axboe 9147995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 9152cb4b05eSChangli Gao sd->flags); 916c66ab6faSJens Axboe } 917c66ab6faSJens Axboe 918932cc6d4SJens Axboe /** 919932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 920932cc6d4SJens Axboe * @in: file to splice from 921932cc6d4SJens Axboe * @ppos: input file offset 922932cc6d4SJens Axboe * @out: file to splice to 923acdb37c3SRandy Dunlap * @opos: output file offset 924932cc6d4SJens Axboe * @len: number of bytes to splice 925932cc6d4SJens Axboe * @flags: splice modifier flags 926932cc6d4SJens Axboe * 927932cc6d4SJens Axboe * Description: 928932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 929932cc6d4SJens Axboe * doing it in the application would incur an extra system call 930932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 931932cc6d4SJens Axboe * can splice directly through a process-private pipe. 932932cc6d4SJens Axboe * 933932cc6d4SJens Axboe */ 934c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 9357995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 936c66ab6faSJens Axboe { 937c66ab6faSJens Axboe struct splice_desc sd = { 938c66ab6faSJens Axboe .len = len, 939c66ab6faSJens Axboe .total_len = len, 940c66ab6faSJens Axboe .flags = flags, 941c66ab6faSJens Axboe .pos = *ppos, 9426a14b90bSJens Axboe .u.file = out, 9437995bd28SAl Viro .opos = opos, 944c66ab6faSJens Axboe }; 94551a92c0fSJens Axboe long ret; 946c66ab6faSJens Axboe 94718c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 94818c67cb9SAl Viro return -EBADF; 94918c67cb9SAl Viro 95018c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 95118c67cb9SAl Viro return -EINVAL; 95218c67cb9SAl Viro 95318c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 95418c67cb9SAl Viro if (unlikely(ret < 0)) 95518c67cb9SAl Viro return ret; 95618c67cb9SAl Viro 957c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 95851a92c0fSJens Axboe if (ret > 0) 959a82c53a0STom Zanussi *ppos = sd.pos; 96051a92c0fSJens Axboe 961c66ab6faSJens Axboe return ret; 962b92ce558SJens Axboe } 9631c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct); 964b92ce558SJens Axboe 9658924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) 9668924feffSAl Viro { 96752bce911SLinus Torvalds for (;;) { 96852bce911SLinus Torvalds if (unlikely(!pipe->readers)) { 96952bce911SLinus Torvalds send_sig(SIGPIPE, current, 0); 97052bce911SLinus Torvalds return -EPIPE; 97152bce911SLinus Torvalds } 9726718b6f8SDavid Howells if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 97352bce911SLinus Torvalds return 0; 9748924feffSAl Viro if (flags & SPLICE_F_NONBLOCK) 9758924feffSAl Viro return -EAGAIN; 9768924feffSAl Viro if (signal_pending(current)) 9778924feffSAl Viro return -ERESTARTSYS; 9788924feffSAl Viro pipe_wait(pipe); 9798924feffSAl Viro } 9808924feffSAl Viro } 9818924feffSAl Viro 9827c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 9837c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 9847c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 985ddac0d39SJens Axboe 986ddac0d39SJens Axboe /* 98783f9135bSJens Axboe * Determine where to splice to/from. 98883f9135bSJens Axboe */ 989444ebb57SPavel Begunkov long do_splice(struct file *in, loff_t __user *off_in, 990529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 991529565dcSIngo Molnar size_t len, unsigned int flags) 9925274f052SJens Axboe { 9937c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 9947c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 9957995bd28SAl Viro loff_t offset; 996a4514ebdSJens Axboe long ret; 9975274f052SJens Axboe 99890da2e3fSPavel Begunkov if (unlikely(!(in->f_mode & FMODE_READ) || 99990da2e3fSPavel Begunkov !(out->f_mode & FMODE_WRITE))) 100090da2e3fSPavel Begunkov return -EBADF; 100190da2e3fSPavel Begunkov 1002c73be61cSDavid Howells ipipe = get_pipe_info(in, true); 1003c73be61cSDavid Howells opipe = get_pipe_info(out, true); 10047c77f0b3SMiklos Szeredi 10057c77f0b3SMiklos Szeredi if (ipipe && opipe) { 10067c77f0b3SMiklos Szeredi if (off_in || off_out) 10077c77f0b3SMiklos Szeredi return -ESPIPE; 10087c77f0b3SMiklos Szeredi 10097c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 10107c77f0b3SMiklos Szeredi if (ipipe == opipe) 10117c77f0b3SMiklos Szeredi return -EINVAL; 10127c77f0b3SMiklos Szeredi 1013ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1014ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1015ee5e0011SSlavomir Kaslev 10167c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 10177c77f0b3SMiklos Szeredi } 10187c77f0b3SMiklos Szeredi 10197c77f0b3SMiklos Szeredi if (ipipe) { 1020529565dcSIngo Molnar if (off_in) 1021529565dcSIngo Molnar return -ESPIPE; 1022b92ce558SJens Axboe if (off_out) { 102319c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1024b92ce558SJens Axboe return -EINVAL; 1025cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1026b92ce558SJens Axboe return -EFAULT; 10277995bd28SAl Viro } else { 10287995bd28SAl Viro offset = out->f_pos; 10297995bd28SAl Viro } 1030529565dcSIngo Molnar 103118c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 103218c67cb9SAl Viro return -EINVAL; 103318c67cb9SAl Viro 103418c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 103518c67cb9SAl Viro if (unlikely(ret < 0)) 103618c67cb9SAl Viro return ret; 103718c67cb9SAl Viro 1038ee5e0011SSlavomir Kaslev if (in->f_flags & O_NONBLOCK) 1039ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1040ee5e0011SSlavomir Kaslev 1041500368f7SAl Viro file_start_write(out); 10427995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1043500368f7SAl Viro file_end_write(out); 1044a4514ebdSJens Axboe 10457995bd28SAl Viro if (!off_out) 10467995bd28SAl Viro out->f_pos = offset; 10477995bd28SAl Viro else if (copy_to_user(off_out, &offset, sizeof(loff_t))) 1048a4514ebdSJens Axboe ret = -EFAULT; 1049a4514ebdSJens Axboe 1050a4514ebdSJens Axboe return ret; 1051529565dcSIngo Molnar } 10525274f052SJens Axboe 10537c77f0b3SMiklos Szeredi if (opipe) { 1054529565dcSIngo Molnar if (off_out) 1055529565dcSIngo Molnar return -ESPIPE; 1056b92ce558SJens Axboe if (off_in) { 105719c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1058b92ce558SJens Axboe return -EINVAL; 1059cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1060b92ce558SJens Axboe return -EFAULT; 10617995bd28SAl Viro } else { 10627995bd28SAl Viro offset = in->f_pos; 10637995bd28SAl Viro } 1064529565dcSIngo Molnar 1065ee5e0011SSlavomir Kaslev if (out->f_flags & O_NONBLOCK) 1066ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1067ee5e0011SSlavomir Kaslev 10688924feffSAl Viro pipe_lock(opipe); 10698924feffSAl Viro ret = wait_for_space(opipe, flags); 10703253d9d0SDarrick J. Wong if (!ret) { 10716a965666SLinus Torvalds unsigned int p_space; 10723253d9d0SDarrick J. Wong 10733253d9d0SDarrick J. Wong /* Don't try to read more the pipe has space for. */ 10746a965666SLinus Torvalds p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail); 10756a965666SLinus Torvalds len = min_t(size_t, len, p_space << PAGE_SHIFT); 10763253d9d0SDarrick J. Wong 10777995bd28SAl Viro ret = do_splice_to(in, &offset, opipe, len, flags); 10783253d9d0SDarrick J. Wong } 10798924feffSAl Viro pipe_unlock(opipe); 10808924feffSAl Viro if (ret > 0) 10818924feffSAl Viro wakeup_pipe_readers(opipe); 10827995bd28SAl Viro if (!off_in) 10837995bd28SAl Viro in->f_pos = offset; 10847995bd28SAl Viro else if (copy_to_user(off_in, &offset, sizeof(loff_t))) 1085a4514ebdSJens Axboe ret = -EFAULT; 1086a4514ebdSJens Axboe 1087a4514ebdSJens Axboe return ret; 1088529565dcSIngo Molnar } 10895274f052SJens Axboe 10905274f052SJens Axboe return -EINVAL; 10915274f052SJens Axboe } 10925274f052SJens Axboe 109379fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from, 109479fddc4eSAl Viro struct pipe_inode_info *pipe, 109579fddc4eSAl Viro unsigned flags) 1096912d35f8SJens Axboe { 109779fddc4eSAl Viro struct pipe_buffer buf = { 109879fddc4eSAl Viro .ops = &user_page_pipe_buf_ops, 109979fddc4eSAl Viro .flags = flags 110079fddc4eSAl Viro }; 110179fddc4eSAl Viro size_t total = 0; 110279fddc4eSAl Viro int ret = 0; 110379fddc4eSAl Viro bool failed = false; 110479fddc4eSAl Viro 110579fddc4eSAl Viro while (iov_iter_count(from) && !failed) { 110679fddc4eSAl Viro struct page *pages[16]; 1107db85a9ebSAl Viro ssize_t copied; 1108db85a9ebSAl Viro size_t start; 110979fddc4eSAl Viro int n; 1110912d35f8SJens Axboe 111179fddc4eSAl Viro copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start); 111279fddc4eSAl Viro if (copied <= 0) { 111379fddc4eSAl Viro ret = copied; 111479fddc4eSAl Viro break; 111579fddc4eSAl Viro } 1116912d35f8SJens Axboe 111779fddc4eSAl Viro for (n = 0; copied; n++, start = 0) { 1118db85a9ebSAl Viro int size = min_t(int, copied, PAGE_SIZE - start); 111979fddc4eSAl Viro if (!failed) { 112079fddc4eSAl Viro buf.page = pages[n]; 112179fddc4eSAl Viro buf.offset = start; 112279fddc4eSAl Viro buf.len = size; 112379fddc4eSAl Viro ret = add_to_pipe(pipe, &buf); 112479fddc4eSAl Viro if (unlikely(ret < 0)) { 112579fddc4eSAl Viro failed = true; 112679fddc4eSAl Viro } else { 112779fddc4eSAl Viro iov_iter_advance(from, ret); 112879fddc4eSAl Viro total += ret; 112979fddc4eSAl Viro } 113079fddc4eSAl Viro } else { 113179fddc4eSAl Viro put_page(pages[n]); 113279fddc4eSAl Viro } 1133db85a9ebSAl Viro copied -= size; 1134912d35f8SJens Axboe } 1135912d35f8SJens Axboe } 113679fddc4eSAl Viro return total ? total : ret; 1137912d35f8SJens Axboe } 1138912d35f8SJens Axboe 11396a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 11406a14b90bSJens Axboe struct splice_desc *sd) 11416a14b90bSJens Axboe { 11426130f531SAl Viro int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 11436130f531SAl Viro return n == sd->len ? n : -EFAULT; 11446a14b90bSJens Axboe } 11456a14b90bSJens Axboe 11466a14b90bSJens Axboe /* 11476a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 11486a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 11496a14b90bSJens Axboe */ 115087a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter, 115187a3002aSAl Viro unsigned int flags) 11526a14b90bSJens Axboe { 1153c73be61cSDavid Howells struct pipe_inode_info *pipe = get_pipe_info(file, true); 115487a3002aSAl Viro struct splice_desc sd = { 115587a3002aSAl Viro .total_len = iov_iter_count(iter), 115687a3002aSAl Viro .flags = flags, 115787a3002aSAl Viro .u.data = iter 115887a3002aSAl Viro }; 115987a3002aSAl Viro long ret = 0; 11606a14b90bSJens Axboe 11616a14b90bSJens Axboe if (!pipe) 11626a14b90bSJens Axboe return -EBADF; 11636a14b90bSJens Axboe 1164345995faSAl Viro if (sd.total_len) { 11656130f531SAl Viro pipe_lock(pipe); 11666130f531SAl Viro ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 116761e0d47cSMiklos Szeredi pipe_unlock(pipe); 1168345995faSAl Viro } 11696a14b90bSJens Axboe 11706a14b90bSJens Axboe return ret; 11716a14b90bSJens Axboe } 11726a14b90bSJens Axboe 1173912d35f8SJens Axboe /* 1174912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1175912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1176912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1177912d35f8SJens Axboe */ 117887a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, 117987a3002aSAl Viro unsigned int flags) 1180912d35f8SJens Axboe { 1181ddac0d39SJens Axboe struct pipe_inode_info *pipe; 118287a3002aSAl Viro long ret = 0; 118379fddc4eSAl Viro unsigned buf_flag = 0; 118479fddc4eSAl Viro 118579fddc4eSAl Viro if (flags & SPLICE_F_GIFT) 118679fddc4eSAl Viro buf_flag = PIPE_BUF_FLAG_GIFT; 1187912d35f8SJens Axboe 1188c73be61cSDavid Howells pipe = get_pipe_info(file, true); 1189ddac0d39SJens Axboe if (!pipe) 1190912d35f8SJens Axboe return -EBADF; 1191912d35f8SJens Axboe 11928924feffSAl Viro pipe_lock(pipe); 11938924feffSAl Viro ret = wait_for_space(pipe, flags); 119479fddc4eSAl Viro if (!ret) 119587a3002aSAl Viro ret = iter_to_pipe(iter, pipe, buf_flag); 11968924feffSAl Viro pipe_unlock(pipe); 11978924feffSAl Viro if (ret > 0) 11988924feffSAl Viro wakeup_pipe_readers(pipe); 119935f3d14dSJens Axboe return ret; 1200912d35f8SJens Axboe } 1201912d35f8SJens Axboe 120287a3002aSAl Viro static int vmsplice_type(struct fd f, int *type) 120387a3002aSAl Viro { 120487a3002aSAl Viro if (!f.file) 120587a3002aSAl Viro return -EBADF; 120687a3002aSAl Viro if (f.file->f_mode & FMODE_WRITE) { 120787a3002aSAl Viro *type = WRITE; 120887a3002aSAl Viro } else if (f.file->f_mode & FMODE_READ) { 120987a3002aSAl Viro *type = READ; 121087a3002aSAl Viro } else { 121187a3002aSAl Viro fdput(f); 121287a3002aSAl Viro return -EBADF; 121387a3002aSAl Viro } 121487a3002aSAl Viro return 0; 121587a3002aSAl Viro } 121687a3002aSAl Viro 12176a14b90bSJens Axboe /* 12186a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 12196a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 12206a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 12216a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 12226a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 12236a14b90bSJens Axboe * solutions for that: 12246a14b90bSJens Axboe * 12256a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 12266a14b90bSJens Axboe * do a regular read() on the buffer anyway. 12276a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 12286a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 12296a14b90bSJens Axboe * 12306a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 12316a14b90bSJens Axboe * 12326a14b90bSJens Axboe */ 123387a3002aSAl Viro static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags) 1234912d35f8SJens Axboe { 12353d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 12363d6ea290SAl Viro return -EINVAL; 123787a3002aSAl Viro 123887a3002aSAl Viro if (!iov_iter_count(iter)) 12396a14b90bSJens Axboe return 0; 12406a14b90bSJens Axboe 124187a3002aSAl Viro if (iov_iter_rw(iter) == WRITE) 124287a3002aSAl Viro return vmsplice_to_pipe(f, iter, flags); 124387a3002aSAl Viro else 124487a3002aSAl Viro return vmsplice_to_user(f, iter, flags); 1245912d35f8SJens Axboe } 1246912d35f8SJens Axboe 124787a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, 124830cfe4efSDominik Brodowski unsigned long, nr_segs, unsigned int, flags) 124930cfe4efSDominik Brodowski { 125087a3002aSAl Viro struct iovec iovstack[UIO_FASTIOV]; 125187a3002aSAl Viro struct iovec *iov = iovstack; 125287a3002aSAl Viro struct iov_iter iter; 125387e5e6daSJens Axboe ssize_t error; 125487a3002aSAl Viro struct fd f; 125587a3002aSAl Viro int type; 125687a3002aSAl Viro 125787a3002aSAl Viro f = fdget(fd); 125887a3002aSAl Viro error = vmsplice_type(f, &type); 125987a3002aSAl Viro if (error) 126087a3002aSAl Viro return error; 126187a3002aSAl Viro 126287a3002aSAl Viro error = import_iovec(type, uiov, nr_segs, 126387a3002aSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 126487e5e6daSJens Axboe if (error >= 0) { 126587a3002aSAl Viro error = do_vmsplice(f.file, &iter, flags); 126687a3002aSAl Viro kfree(iov); 126787a3002aSAl Viro } 126887a3002aSAl Viro fdput(f); 126987a3002aSAl Viro return error; 127030cfe4efSDominik Brodowski } 127130cfe4efSDominik Brodowski 127276b021d0SAl Viro #ifdef CONFIG_COMPAT 127376b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32, 127476b021d0SAl Viro unsigned int, nr_segs, unsigned int, flags) 127576b021d0SAl Viro { 127687a3002aSAl Viro struct iovec iovstack[UIO_FASTIOV]; 127787a3002aSAl Viro struct iovec *iov = iovstack; 127887a3002aSAl Viro struct iov_iter iter; 127987e5e6daSJens Axboe ssize_t error; 128087a3002aSAl Viro struct fd f; 128187a3002aSAl Viro int type; 128287a3002aSAl Viro 128387a3002aSAl Viro f = fdget(fd); 128487a3002aSAl Viro error = vmsplice_type(f, &type); 128587a3002aSAl Viro if (error) 128687a3002aSAl Viro return error; 128787a3002aSAl Viro 128887a3002aSAl Viro error = compat_import_iovec(type, iov32, nr_segs, 128987a3002aSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 129087e5e6daSJens Axboe if (error >= 0) { 129187a3002aSAl Viro error = do_vmsplice(f.file, &iter, flags); 129287a3002aSAl Viro kfree(iov); 129376b021d0SAl Viro } 129487a3002aSAl Viro fdput(f); 129587a3002aSAl Viro return error; 129676b021d0SAl Viro } 129776b021d0SAl Viro #endif 129876b021d0SAl Viro 1299836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1300836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1301836f92adSHeiko Carstens size_t, len, unsigned int, flags) 13025274f052SJens Axboe { 13032903ff01SAl Viro struct fd in, out; 13045274f052SJens Axboe long error; 13055274f052SJens Axboe 13065274f052SJens Axboe if (unlikely(!len)) 13075274f052SJens Axboe return 0; 13085274f052SJens Axboe 13093d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 13103d6ea290SAl Viro return -EINVAL; 13113d6ea290SAl Viro 13125274f052SJens Axboe error = -EBADF; 13132903ff01SAl Viro in = fdget(fd_in); 13142903ff01SAl Viro if (in.file) { 13152903ff01SAl Viro out = fdget(fd_out); 13162903ff01SAl Viro if (out.file) { 131790da2e3fSPavel Begunkov error = do_splice(in.file, off_in, out.file, off_out, 1318529565dcSIngo Molnar len, flags); 13192903ff01SAl Viro fdput(out); 13205274f052SJens Axboe } 13212903ff01SAl Viro fdput(in); 13225274f052SJens Axboe } 13235274f052SJens Axboe return error; 13245274f052SJens Axboe } 132570524490SJens Axboe 132670524490SJens Axboe /* 1327aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1328aadd06e5SJens Axboe * return an appropriate error. 1329aadd06e5SJens Axboe */ 13307c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1331aadd06e5SJens Axboe { 1332aadd06e5SJens Axboe int ret; 1333aadd06e5SJens Axboe 1334aadd06e5SJens Axboe /* 13358cefc107SDavid Howells * Check the pipe occupancy without the inode lock first. This function 1336aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1337aadd06e5SJens Axboe */ 13388cefc107SDavid Howells if (!pipe_empty(pipe->head, pipe->tail)) 1339aadd06e5SJens Axboe return 0; 1340aadd06e5SJens Axboe 1341aadd06e5SJens Axboe ret = 0; 134261e0d47cSMiklos Szeredi pipe_lock(pipe); 1343aadd06e5SJens Axboe 13448cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 1345aadd06e5SJens Axboe if (signal_pending(current)) { 1346aadd06e5SJens Axboe ret = -ERESTARTSYS; 1347aadd06e5SJens Axboe break; 1348aadd06e5SJens Axboe } 1349aadd06e5SJens Axboe if (!pipe->writers) 1350aadd06e5SJens Axboe break; 1351aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1352aadd06e5SJens Axboe ret = -EAGAIN; 1353aadd06e5SJens Axboe break; 1354aadd06e5SJens Axboe } 1355aadd06e5SJens Axboe pipe_wait(pipe); 1356aadd06e5SJens Axboe } 1357aadd06e5SJens Axboe 135861e0d47cSMiklos Szeredi pipe_unlock(pipe); 1359aadd06e5SJens Axboe return ret; 1360aadd06e5SJens Axboe } 1361aadd06e5SJens Axboe 1362aadd06e5SJens Axboe /* 1363aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1364aadd06e5SJens Axboe * return an appropriate error. 1365aadd06e5SJens Axboe */ 13667c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1367aadd06e5SJens Axboe { 1368aadd06e5SJens Axboe int ret; 1369aadd06e5SJens Axboe 1370aadd06e5SJens Axboe /* 13718cefc107SDavid Howells * Check pipe occupancy without the inode lock first. This function 1372aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1373aadd06e5SJens Axboe */ 1374566d1362STetsuo Handa if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 1375aadd06e5SJens Axboe return 0; 1376aadd06e5SJens Axboe 1377aadd06e5SJens Axboe ret = 0; 137861e0d47cSMiklos Szeredi pipe_lock(pipe); 1379aadd06e5SJens Axboe 13806718b6f8SDavid Howells while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 1381aadd06e5SJens Axboe if (!pipe->readers) { 1382aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1383aadd06e5SJens Axboe ret = -EPIPE; 1384aadd06e5SJens Axboe break; 1385aadd06e5SJens Axboe } 1386aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1387aadd06e5SJens Axboe ret = -EAGAIN; 1388aadd06e5SJens Axboe break; 1389aadd06e5SJens Axboe } 1390aadd06e5SJens Axboe if (signal_pending(current)) { 1391aadd06e5SJens Axboe ret = -ERESTARTSYS; 1392aadd06e5SJens Axboe break; 1393aadd06e5SJens Axboe } 1394aadd06e5SJens Axboe pipe_wait(pipe); 1395aadd06e5SJens Axboe } 1396aadd06e5SJens Axboe 139761e0d47cSMiklos Szeredi pipe_unlock(pipe); 1398aadd06e5SJens Axboe return ret; 1399aadd06e5SJens Axboe } 1400aadd06e5SJens Axboe 1401aadd06e5SJens Axboe /* 14027c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 14037c77f0b3SMiklos Szeredi */ 14047c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 14057c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 14067c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 14077c77f0b3SMiklos Szeredi { 14087c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 14098cefc107SDavid Howells unsigned int i_head, o_head; 14108cefc107SDavid Howells unsigned int i_tail, o_tail; 14118cefc107SDavid Howells unsigned int i_mask, o_mask; 14128cefc107SDavid Howells int ret = 0; 14137c77f0b3SMiklos Szeredi bool input_wakeup = false; 14147c77f0b3SMiklos Szeredi 14157c77f0b3SMiklos Szeredi 14167c77f0b3SMiklos Szeredi retry: 14177c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 14187c77f0b3SMiklos Szeredi if (ret) 14197c77f0b3SMiklos Szeredi return ret; 14207c77f0b3SMiklos Szeredi 14217c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 14227c77f0b3SMiklos Szeredi if (ret) 14237c77f0b3SMiklos Szeredi return ret; 14247c77f0b3SMiklos Szeredi 14257c77f0b3SMiklos Szeredi /* 14267c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 14277c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 14287c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 14297c77f0b3SMiklos Szeredi */ 14307c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 14317c77f0b3SMiklos Szeredi 14328cefc107SDavid Howells i_tail = ipipe->tail; 14338cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 14348cefc107SDavid Howells o_head = opipe->head; 14358cefc107SDavid Howells o_mask = opipe->ring_size - 1; 14368cefc107SDavid Howells 14377c77f0b3SMiklos Szeredi do { 14388cefc107SDavid Howells size_t o_len; 14398cefc107SDavid Howells 14407c77f0b3SMiklos Szeredi if (!opipe->readers) { 14417c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 14427c77f0b3SMiklos Szeredi if (!ret) 14437c77f0b3SMiklos Szeredi ret = -EPIPE; 14447c77f0b3SMiklos Szeredi break; 14457c77f0b3SMiklos Szeredi } 14467c77f0b3SMiklos Szeredi 14478cefc107SDavid Howells i_head = ipipe->head; 14488cefc107SDavid Howells o_tail = opipe->tail; 14498cefc107SDavid Howells 14508cefc107SDavid Howells if (pipe_empty(i_head, i_tail) && !ipipe->writers) 14517c77f0b3SMiklos Szeredi break; 14527c77f0b3SMiklos Szeredi 14537c77f0b3SMiklos Szeredi /* 14547c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 14557c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 14567c77f0b3SMiklos Szeredi */ 14578cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 14586718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) { 14597c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 14607c77f0b3SMiklos Szeredi if (ret) 14617c77f0b3SMiklos Szeredi break; 14627c77f0b3SMiklos Szeredi 14637c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 14647c77f0b3SMiklos Szeredi ret = -EAGAIN; 14657c77f0b3SMiklos Szeredi break; 14667c77f0b3SMiklos Szeredi } 14677c77f0b3SMiklos Szeredi 14687c77f0b3SMiklos Szeredi /* 14697c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 14707c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 14717c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 14727c77f0b3SMiklos Szeredi */ 14737c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 14747c77f0b3SMiklos Szeredi pipe_unlock(opipe); 14757c77f0b3SMiklos Szeredi goto retry; 14767c77f0b3SMiklos Szeredi } 14777c77f0b3SMiklos Szeredi 14788cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 14798cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 14807c77f0b3SMiklos Szeredi 14817c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 14827c77f0b3SMiklos Szeredi /* 14837c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 14847c77f0b3SMiklos Szeredi */ 14857c77f0b3SMiklos Szeredi *obuf = *ibuf; 14867c77f0b3SMiklos Szeredi ibuf->ops = NULL; 14878cefc107SDavid Howells i_tail++; 14888cefc107SDavid Howells ipipe->tail = i_tail; 14897c77f0b3SMiklos Szeredi input_wakeup = true; 14908cefc107SDavid Howells o_len = obuf->len; 14918cefc107SDavid Howells o_head++; 14928cefc107SDavid Howells opipe->head = o_head; 14937c77f0b3SMiklos Szeredi } else { 14947c77f0b3SMiklos Szeredi /* 14957c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 14967c77f0b3SMiklos Szeredi * so we can copy the contents over. 14977c77f0b3SMiklos Szeredi */ 149815fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 149915fab63eSMatthew Wilcox if (ret == 0) 150015fab63eSMatthew Wilcox ret = -EFAULT; 150115fab63eSMatthew Wilcox break; 150215fab63eSMatthew Wilcox } 15037c77f0b3SMiklos Szeredi *obuf = *ibuf; 15047c77f0b3SMiklos Szeredi 15057c77f0b3SMiklos Szeredi /* 1506f6dd9755SChristoph Hellwig * Don't inherit the gift and merge flags, we need to 15077c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 15087c77f0b3SMiklos Szeredi */ 15097c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 1510f6dd9755SChristoph Hellwig obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 1511a0ce2f0aSJann Horn 15127c77f0b3SMiklos Szeredi obuf->len = len; 15138cefc107SDavid Howells ibuf->offset += len; 15148cefc107SDavid Howells ibuf->len -= len; 15158cefc107SDavid Howells o_len = len; 15168cefc107SDavid Howells o_head++; 15178cefc107SDavid Howells opipe->head = o_head; 15187c77f0b3SMiklos Szeredi } 15198cefc107SDavid Howells ret += o_len; 15208cefc107SDavid Howells len -= o_len; 15217c77f0b3SMiklos Szeredi } while (len); 15227c77f0b3SMiklos Szeredi 15237c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 15247c77f0b3SMiklos Szeredi pipe_unlock(opipe); 15257c77f0b3SMiklos Szeredi 15267c77f0b3SMiklos Szeredi /* 15277c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 15287c77f0b3SMiklos Szeredi */ 1529825cdcb1SNamhyung Kim if (ret > 0) 1530825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1531825cdcb1SNamhyung Kim 15327c77f0b3SMiklos Szeredi if (input_wakeup) 15337c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 15347c77f0b3SMiklos Szeredi 15357c77f0b3SMiklos Szeredi return ret; 15367c77f0b3SMiklos Szeredi } 15377c77f0b3SMiklos Szeredi 15387c77f0b3SMiklos Szeredi /* 153970524490SJens Axboe * Link contents of ipipe to opipe. 154070524490SJens Axboe */ 154170524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 154270524490SJens Axboe struct pipe_inode_info *opipe, 154370524490SJens Axboe size_t len, unsigned int flags) 154470524490SJens Axboe { 154570524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 15468cefc107SDavid Howells unsigned int i_head, o_head; 15478cefc107SDavid Howells unsigned int i_tail, o_tail; 15488cefc107SDavid Howells unsigned int i_mask, o_mask; 15498cefc107SDavid Howells int ret = 0; 155070524490SJens Axboe 155170524490SJens Axboe /* 155270524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 155361e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 155470524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 155570524490SJens Axboe */ 155661e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 155770524490SJens Axboe 15588cefc107SDavid Howells i_tail = ipipe->tail; 15598cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 15608cefc107SDavid Howells o_head = opipe->head; 15618cefc107SDavid Howells o_mask = opipe->ring_size - 1; 15628cefc107SDavid Howells 1563aadd06e5SJens Axboe do { 156470524490SJens Axboe if (!opipe->readers) { 156570524490SJens Axboe send_sig(SIGPIPE, current, 0); 156670524490SJens Axboe if (!ret) 156770524490SJens Axboe ret = -EPIPE; 156870524490SJens Axboe break; 156970524490SJens Axboe } 157070524490SJens Axboe 15718cefc107SDavid Howells i_head = ipipe->head; 15728cefc107SDavid Howells o_tail = opipe->tail; 15738cefc107SDavid Howells 157470524490SJens Axboe /* 15758cefc107SDavid Howells * If we have iterated all input buffers or run out of 1576aadd06e5SJens Axboe * output room, break. 157770524490SJens Axboe */ 15788cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 15796718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) 1580aadd06e5SJens Axboe break; 1581aadd06e5SJens Axboe 15828cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 15838cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 158470524490SJens Axboe 158570524490SJens Axboe /* 158670524490SJens Axboe * Get a reference to this pipe buffer, 158770524490SJens Axboe * so we can copy the contents over. 158870524490SJens Axboe */ 158915fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 159015fab63eSMatthew Wilcox if (ret == 0) 159115fab63eSMatthew Wilcox ret = -EFAULT; 159215fab63eSMatthew Wilcox break; 159315fab63eSMatthew Wilcox } 159470524490SJens Axboe 159570524490SJens Axboe *obuf = *ibuf; 159670524490SJens Axboe 15977afa6fd0SJens Axboe /* 1598f6dd9755SChristoph Hellwig * Don't inherit the gift and merge flag, we need to prevent 1599f6dd9755SChristoph Hellwig * multiple steals of this page. 16007afa6fd0SJens Axboe */ 16017afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 1602f6dd9755SChristoph Hellwig obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 1603a0ce2f0aSJann Horn 160470524490SJens Axboe if (obuf->len > len) 160570524490SJens Axboe obuf->len = len; 160670524490SJens Axboe ret += obuf->len; 160770524490SJens Axboe len -= obuf->len; 16088cefc107SDavid Howells 16098cefc107SDavid Howells o_head++; 16108cefc107SDavid Howells opipe->head = o_head; 16118cefc107SDavid Howells i_tail++; 1612aadd06e5SJens Axboe } while (len); 161370524490SJens Axboe 161461e0d47cSMiklos Szeredi pipe_unlock(ipipe); 161561e0d47cSMiklos Szeredi pipe_unlock(opipe); 161670524490SJens Axboe 1617aadd06e5SJens Axboe /* 1618aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1619aadd06e5SJens Axboe */ 1620825cdcb1SNamhyung Kim if (ret > 0) 1621825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 162270524490SJens Axboe 162370524490SJens Axboe return ret; 162470524490SJens Axboe } 162570524490SJens Axboe 162670524490SJens Axboe /* 162770524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 162870524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 162970524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 163070524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 163170524490SJens Axboe */ 16329dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) 163370524490SJens Axboe { 1634c73be61cSDavid Howells struct pipe_inode_info *ipipe = get_pipe_info(in, true); 1635c73be61cSDavid Howells struct pipe_inode_info *opipe = get_pipe_info(out, true); 1636aadd06e5SJens Axboe int ret = -EINVAL; 163770524490SJens Axboe 163890da2e3fSPavel Begunkov if (unlikely(!(in->f_mode & FMODE_READ) || 163990da2e3fSPavel Begunkov !(out->f_mode & FMODE_WRITE))) 164090da2e3fSPavel Begunkov return -EBADF; 164190da2e3fSPavel Begunkov 164270524490SJens Axboe /* 1643aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 1644aadd06e5SJens Axboe * copying the data. 164570524490SJens Axboe */ 1646aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 1647ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1648ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1649ee5e0011SSlavomir Kaslev 1650aadd06e5SJens Axboe /* 1651aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 1652aadd06e5SJens Axboe * ordering doesn't really matter. 1653aadd06e5SJens Axboe */ 16547c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 1655aadd06e5SJens Axboe if (!ret) { 16567c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 165702cf01aeSJens Axboe if (!ret) 1658aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 1659aadd06e5SJens Axboe } 1660aadd06e5SJens Axboe } 166170524490SJens Axboe 1662aadd06e5SJens Axboe return ret; 166370524490SJens Axboe } 166470524490SJens Axboe 1665836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 166670524490SJens Axboe { 166790da2e3fSPavel Begunkov struct fd in, out; 16682903ff01SAl Viro int error; 166970524490SJens Axboe 16703d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 16713d6ea290SAl Viro return -EINVAL; 16723d6ea290SAl Viro 167370524490SJens Axboe if (unlikely(!len)) 167470524490SJens Axboe return 0; 167570524490SJens Axboe 167670524490SJens Axboe error = -EBADF; 16772903ff01SAl Viro in = fdget(fdin); 16782903ff01SAl Viro if (in.file) { 167990da2e3fSPavel Begunkov out = fdget(fdout); 16802903ff01SAl Viro if (out.file) { 168190da2e3fSPavel Begunkov error = do_tee(in.file, out.file, len, flags); 16822903ff01SAl Viro fdput(out); 168370524490SJens Axboe } 16842903ff01SAl Viro fdput(in); 168570524490SJens Axboe } 168670524490SJens Axboe 168770524490SJens Axboe return error; 168870524490SJens Axboe } 1689