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> 36174cd4b1SIngo Molnar #include <linux/sched/signal.h> 37174cd4b1SIngo Molnar 3806ae43f3SAl Viro #include "internal.h" 395274f052SJens Axboe 4083f9135bSJens Axboe /* 4183f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 4283f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 4383f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 4483f9135bSJens Axboe * attempt to reuse this page for another destination. 4583f9135bSJens Axboe */ 46c928f642SChristoph Hellwig static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe, 475abc97aaSJens Axboe struct pipe_buffer *buf) 485abc97aaSJens Axboe { 495abc97aaSJens Axboe struct page *page = buf->page; 509e94cd4fSJens Axboe struct address_space *mapping; 515abc97aaSJens Axboe 529e0267c2SJens Axboe lock_page(page); 539e0267c2SJens Axboe 549e94cd4fSJens Axboe mapping = page_mapping(page); 559e94cd4fSJens Axboe if (mapping) { 565abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 575abc97aaSJens Axboe 58ad8d6f0aSJens Axboe /* 599e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 609e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 619e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 629e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 639e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 649e94cd4fSJens Axboe * ensues. 65ad8d6f0aSJens Axboe */ 66ad8d6f0aSJens Axboe wait_on_page_writeback(page); 67ad8d6f0aSJens Axboe 68266cf658SDavid Howells if (page_has_private(page) && 69266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 70ca39d651SJens Axboe goto out_unlock; 714f6f0bd2SJens Axboe 729e94cd4fSJens Axboe /* 739e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 749e94cd4fSJens Axboe * and return good. 759e94cd4fSJens Axboe */ 769e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 771432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 78c928f642SChristoph Hellwig return true; 795abc97aaSJens Axboe } 809e94cd4fSJens Axboe } 819e94cd4fSJens Axboe 829e94cd4fSJens Axboe /* 839e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 849e94cd4fSJens Axboe * address space, unlock and return failure. 859e94cd4fSJens Axboe */ 86ca39d651SJens Axboe out_unlock: 879e94cd4fSJens Axboe unlock_page(page); 88c928f642SChristoph Hellwig return false; 899e94cd4fSJens Axboe } 905abc97aaSJens Axboe 9176ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 925274f052SJens Axboe struct pipe_buffer *buf) 935274f052SJens Axboe { 9409cbfeafSKirill A. Shutemov put_page(buf->page); 951432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 965274f052SJens Axboe } 975274f052SJens Axboe 980845718dSJens Axboe /* 990845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 1000845718dSJens Axboe * is a page cache page, IO may be in flight. 1010845718dSJens Axboe */ 102cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 1035274f052SJens Axboe struct pipe_buffer *buf) 1045274f052SJens Axboe { 1055274f052SJens Axboe struct page *page = buf->page; 10649d0b21bSJens Axboe int err; 1075274f052SJens Axboe 1085274f052SJens Axboe if (!PageUptodate(page)) { 10949d0b21bSJens Axboe lock_page(page); 1105274f052SJens Axboe 11149d0b21bSJens Axboe /* 11249d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 11373d62d83SIngo Molnar * splice, if this is the first page. 11449d0b21bSJens Axboe */ 1155274f052SJens Axboe if (!page->mapping) { 11649d0b21bSJens Axboe err = -ENODATA; 11749d0b21bSJens Axboe goto error; 1185274f052SJens Axboe } 1195274f052SJens Axboe 12049d0b21bSJens Axboe /* 12173d62d83SIngo Molnar * Uh oh, read-error from disk. 12249d0b21bSJens Axboe */ 12349d0b21bSJens Axboe if (!PageUptodate(page)) { 12449d0b21bSJens Axboe err = -EIO; 12549d0b21bSJens Axboe goto error; 12649d0b21bSJens Axboe } 12749d0b21bSJens Axboe 12849d0b21bSJens Axboe /* 129f84d7519SJens Axboe * Page is ok afterall, we are done. 13049d0b21bSJens Axboe */ 13149d0b21bSJens Axboe unlock_page(page); 13249d0b21bSJens Axboe } 13349d0b21bSJens Axboe 134f84d7519SJens Axboe return 0; 13549d0b21bSJens Axboe error: 13649d0b21bSJens Axboe unlock_page(page); 137f84d7519SJens Axboe return err; 13870524490SJens Axboe } 13970524490SJens Axboe 140708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 141cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1425274f052SJens Axboe .release = page_cache_pipe_buf_release, 143c928f642SChristoph Hellwig .try_steal = page_cache_pipe_buf_try_steal, 144f84d7519SJens Axboe .get = generic_pipe_buf_get, 1455274f052SJens Axboe }; 1465274f052SJens Axboe 147c928f642SChristoph Hellwig static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe, 148912d35f8SJens Axboe struct pipe_buffer *buf) 149912d35f8SJens Axboe { 1507afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 151c928f642SChristoph Hellwig return false; 1527afa6fd0SJens Axboe 1531432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 154c928f642SChristoph Hellwig return generic_pipe_buf_try_steal(pipe, buf); 155912d35f8SJens Axboe } 156912d35f8SJens Axboe 157d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 158912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 159c928f642SChristoph Hellwig .try_steal = user_page_pipe_buf_try_steal, 160f84d7519SJens Axboe .get = generic_pipe_buf_get, 161912d35f8SJens Axboe }; 162912d35f8SJens Axboe 163825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 164825cdcb1SNamhyung Kim { 165825cdcb1SNamhyung Kim smp_mb(); 1660ddad21dSLinus Torvalds if (waitqueue_active(&pipe->rd_wait)) 1670ddad21dSLinus Torvalds wake_up_interruptible(&pipe->rd_wait); 168825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 169825cdcb1SNamhyung Kim } 170825cdcb1SNamhyung Kim 171932cc6d4SJens Axboe /** 172932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 173932cc6d4SJens Axboe * @pipe: pipe to fill 174932cc6d4SJens Axboe * @spd: data to fill 175932cc6d4SJens Axboe * 176932cc6d4SJens Axboe * Description: 17779685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 178932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 179932cc6d4SJens Axboe * function will link that data to the pipe. 180932cc6d4SJens Axboe * 18183f9135bSJens Axboe */ 182d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 183912d35f8SJens Axboe struct splice_pipe_desc *spd) 1845274f052SJens Axboe { 18500de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 1868cefc107SDavid Howells unsigned int tail = pipe->tail; 1878cefc107SDavid Howells unsigned int head = pipe->head; 1888cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 1898924feffSAl Viro int ret = 0, page_nr = 0; 1905274f052SJens Axboe 191d6785d91SRabin Vincent if (!spd_pages) 192d6785d91SRabin Vincent return 0; 193d6785d91SRabin Vincent 1948924feffSAl Viro if (unlikely(!pipe->readers)) { 1955274f052SJens Axboe send_sig(SIGPIPE, current, 0); 1965274f052SJens Axboe ret = -EPIPE; 1978924feffSAl Viro goto out; 1985274f052SJens Axboe } 1995274f052SJens Axboe 2006718b6f8SDavid Howells while (!pipe_full(head, tail, pipe->max_usage)) { 2018cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[head & mask]; 2025274f052SJens Axboe 203912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 204912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 205912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 206497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 207912d35f8SJens Axboe buf->ops = spd->ops; 2085a81e6a1SMiklos Szeredi buf->flags = 0; 2097afa6fd0SJens Axboe 2108cefc107SDavid Howells head++; 2118cefc107SDavid Howells pipe->head = head; 212912d35f8SJens Axboe page_nr++; 213912d35f8SJens Axboe ret += buf->len; 214912d35f8SJens Axboe 215912d35f8SJens Axboe if (!--spd->nr_pages) 2165274f052SJens Axboe break; 2175274f052SJens Axboe } 2185274f052SJens Axboe 21929e35094SLinus Torvalds if (!ret) 22029e35094SLinus Torvalds ret = -EAGAIN; 22129e35094SLinus Torvalds 2228924feffSAl Viro out: 22300de00bdSJens Axboe while (page_nr < spd_pages) 224bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2255274f052SJens Axboe 2265274f052SJens Axboe return ret; 2275274f052SJens Axboe } 2282b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe); 2295274f052SJens Axboe 23079fddc4eSAl Viro ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) 23179fddc4eSAl Viro { 2328cefc107SDavid Howells unsigned int head = pipe->head; 2338cefc107SDavid Howells unsigned int tail = pipe->tail; 2348cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 23579fddc4eSAl Viro int ret; 23679fddc4eSAl Viro 23779fddc4eSAl Viro if (unlikely(!pipe->readers)) { 23879fddc4eSAl Viro send_sig(SIGPIPE, current, 0); 23979fddc4eSAl Viro ret = -EPIPE; 2406718b6f8SDavid Howells } else if (pipe_full(head, tail, pipe->max_usage)) { 24179fddc4eSAl Viro ret = -EAGAIN; 24279fddc4eSAl Viro } else { 2438cefc107SDavid Howells pipe->bufs[head & mask] = *buf; 2448cefc107SDavid Howells pipe->head = head + 1; 24579fddc4eSAl Viro return buf->len; 24679fddc4eSAl Viro } 247a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 24879fddc4eSAl Viro return ret; 24979fddc4eSAl Viro } 25079fddc4eSAl Viro EXPORT_SYMBOL(add_to_pipe); 25179fddc4eSAl Viro 25235f3d14dSJens Axboe /* 25335f3d14dSJens Axboe * Check if we need to grow the arrays holding pages and partial page 25435f3d14dSJens Axboe * descriptions. 25535f3d14dSJens Axboe */ 256047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 25735f3d14dSJens Axboe { 2586718b6f8SDavid Howells unsigned int max_usage = READ_ONCE(pipe->max_usage); 259047fe360SEric Dumazet 2608cefc107SDavid Howells spd->nr_pages_max = max_usage; 2618cefc107SDavid Howells if (max_usage <= PIPE_DEF_BUFFERS) 26235f3d14dSJens Axboe return 0; 26335f3d14dSJens Axboe 2648cefc107SDavid Howells spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL); 2658cefc107SDavid Howells spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page), 2666da2ec56SKees Cook GFP_KERNEL); 26735f3d14dSJens Axboe 26835f3d14dSJens Axboe if (spd->pages && spd->partial) 26935f3d14dSJens Axboe return 0; 27035f3d14dSJens Axboe 27135f3d14dSJens Axboe kfree(spd->pages); 27235f3d14dSJens Axboe kfree(spd->partial); 27335f3d14dSJens Axboe return -ENOMEM; 27435f3d14dSJens Axboe } 27535f3d14dSJens Axboe 276047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd) 27735f3d14dSJens Axboe { 278047fe360SEric Dumazet if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 27935f3d14dSJens Axboe return; 28035f3d14dSJens Axboe 28135f3d14dSJens Axboe kfree(spd->pages); 28235f3d14dSJens Axboe kfree(spd->partial); 28335f3d14dSJens Axboe } 28435f3d14dSJens Axboe 28583f9135bSJens Axboe /** 28683f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 28783f9135bSJens Axboe * @in: file to splice from 288932cc6d4SJens Axboe * @ppos: position in @in 28983f9135bSJens Axboe * @pipe: pipe to splice to 29083f9135bSJens Axboe * @len: number of bytes to splice 29183f9135bSJens Axboe * @flags: splice modifier flags 29283f9135bSJens Axboe * 293932cc6d4SJens Axboe * Description: 294932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 29582c156f8SAl Viro * used as long as it has more or less sane ->read_iter(). 296932cc6d4SJens Axboe * 29783f9135bSJens Axboe */ 298cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 299cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 300cbb7e577SJens Axboe unsigned int flags) 3015274f052SJens Axboe { 30282c156f8SAl Viro struct iov_iter to; 30382c156f8SAl Viro struct kiocb kiocb; 3048cefc107SDavid Howells unsigned int i_head; 3058cefc107SDavid Howells int ret; 306be64f884SBoaz Harrosh 307aa563d7bSDavid Howells iov_iter_pipe(&to, READ, pipe, len); 3088cefc107SDavid Howells i_head = to.head; 30982c156f8SAl Viro init_sync_kiocb(&kiocb, in); 31082c156f8SAl Viro kiocb.ki_pos = *ppos; 311bb7462b6SMiklos Szeredi ret = call_read_iter(in, &kiocb, &to); 312723590edSMiklos Szeredi if (ret > 0) { 31382c156f8SAl Viro *ppos = kiocb.ki_pos; 314723590edSMiklos Szeredi file_accessed(in); 31582c156f8SAl Viro } else if (ret < 0) { 3168cefc107SDavid Howells to.head = i_head; 31782c156f8SAl Viro to.iov_offset = 0; 31882c156f8SAl Viro iov_iter_advance(&to, 0); /* to free what was emitted */ 31982c156f8SAl Viro /* 32082c156f8SAl Viro * callers of ->splice_read() expect -EAGAIN on 32182c156f8SAl Viro * "can't put anything in there", rather than -EFAULT. 32282c156f8SAl Viro */ 32382c156f8SAl Viro if (ret == -EFAULT) 32482c156f8SAl Viro ret = -EAGAIN; 325723590edSMiklos Szeredi } 3265274f052SJens Axboe 3275274f052SJens Axboe return ret; 3285274f052SJens Axboe } 329059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 330059a8f37SJens Axboe 331241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = { 3326818173bSMiklos Szeredi .release = generic_pipe_buf_release, 333c928f642SChristoph Hellwig .try_steal = generic_pipe_buf_try_steal, 3346818173bSMiklos Szeredi .get = generic_pipe_buf_get, 3356818173bSMiklos Szeredi }; 3366818173bSMiklos Szeredi 33728a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 33828a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 33928a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 34028a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 34128a625cbSMiklos Szeredi }; 34228a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 34328a625cbSMiklos Szeredi 3445274f052SJens Axboe /* 3454f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 346016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 3475274f052SJens Axboe */ 34876ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 3495274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 3505274f052SJens Axboe { 3516a14b90bSJens Axboe struct file *file = sd->u.file; 3525274f052SJens Axboe loff_t pos = sd->pos; 353a8adbe37SMichał Mirosław int more; 3545274f052SJens Axboe 35572c2d531SAl Viro if (!likely(file->f_op->sendpage)) 356a8adbe37SMichał Mirosław return -EINVAL; 357a8adbe37SMichał Mirosław 35835f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 359ae62ca7bSEric Dumazet 3608cefc107SDavid Howells if (sd->len < sd->total_len && 3618cefc107SDavid Howells pipe_occupancy(pipe->head, pipe->tail) > 1) 36235f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 363ae62ca7bSEric Dumazet 364a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 365f84d7519SJens Axboe sd->len, &pos, more); 3665274f052SJens Axboe } 3675274f052SJens Axboe 368b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 369b3c2d2ddSMiklos Szeredi { 370b3c2d2ddSMiklos Szeredi smp_mb(); 3710ddad21dSLinus Torvalds if (waitqueue_active(&pipe->wr_wait)) 3720ddad21dSLinus Torvalds wake_up_interruptible(&pipe->wr_wait); 373b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 374b3c2d2ddSMiklos Szeredi } 375b3c2d2ddSMiklos Szeredi 376b3c2d2ddSMiklos Szeredi /** 377b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 378b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 379b3c2d2ddSMiklos Szeredi * @sd: information to @actor 380b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 381b3c2d2ddSMiklos Szeredi * 382b3c2d2ddSMiklos Szeredi * Description: 383b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 384b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 385b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 386b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 387b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 388b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 389b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 390b3c2d2ddSMiklos Szeredi * 391b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 392b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 393b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 394b3c2d2ddSMiklos Szeredi * destination. 395b3c2d2ddSMiklos Szeredi */ 39696f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 397b3c2d2ddSMiklos Szeredi splice_actor *actor) 398b3c2d2ddSMiklos Szeredi { 3998cefc107SDavid Howells unsigned int head = pipe->head; 4008cefc107SDavid Howells unsigned int tail = pipe->tail; 4018cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 402b3c2d2ddSMiklos Szeredi int ret; 403b3c2d2ddSMiklos Szeredi 404ec057595SLinus Torvalds while (!pipe_empty(head, tail)) { 4058cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 406b3c2d2ddSMiklos Szeredi 407b3c2d2ddSMiklos Szeredi sd->len = buf->len; 408b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 409b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 410b3c2d2ddSMiklos Szeredi 411fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 412a8adbe37SMichał Mirosław if (unlikely(ret)) { 413b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 414b3c2d2ddSMiklos Szeredi ret = 0; 415b3c2d2ddSMiklos Szeredi return ret; 416b3c2d2ddSMiklos Szeredi } 417a8adbe37SMichał Mirosław 418a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 419a8adbe37SMichał Mirosław if (ret <= 0) 420a8adbe37SMichał Mirosław return ret; 421a8adbe37SMichał Mirosław 422b3c2d2ddSMiklos Szeredi buf->offset += ret; 423b3c2d2ddSMiklos Szeredi buf->len -= ret; 424b3c2d2ddSMiklos Szeredi 425b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 426b3c2d2ddSMiklos Szeredi sd->len -= ret; 427b3c2d2ddSMiklos Szeredi sd->pos += ret; 428b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 429b3c2d2ddSMiklos Szeredi 430b3c2d2ddSMiklos Szeredi if (!buf->len) { 431a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 4328cefc107SDavid Howells tail++; 4338cefc107SDavid Howells pipe->tail = tail; 4346447a3cfSAl Viro if (pipe->files) 435b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 436b3c2d2ddSMiklos Szeredi } 437b3c2d2ddSMiklos Szeredi 438b3c2d2ddSMiklos Szeredi if (!sd->total_len) 439b3c2d2ddSMiklos Szeredi return 0; 440b3c2d2ddSMiklos Szeredi } 441b3c2d2ddSMiklos Szeredi 442b3c2d2ddSMiklos Szeredi return 1; 443b3c2d2ddSMiklos Szeredi } 444b3c2d2ddSMiklos Szeredi 445d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */ 446d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe) 447d1a819a2SLinus Torvalds { 448d1a819a2SLinus Torvalds unsigned int tail = pipe->tail; 449d1a819a2SLinus Torvalds unsigned int mask = pipe->ring_size - 1; 450d1a819a2SLinus Torvalds struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 451d1a819a2SLinus Torvalds 452d1a819a2SLinus Torvalds if (unlikely(!buf->len)) { 453d1a819a2SLinus Torvalds pipe_buf_release(pipe, buf); 454d1a819a2SLinus Torvalds pipe->tail = tail+1; 455d1a819a2SLinus Torvalds return true; 456d1a819a2SLinus Torvalds } 457d1a819a2SLinus Torvalds 458d1a819a2SLinus Torvalds return false; 459d1a819a2SLinus Torvalds } 460d1a819a2SLinus Torvalds 461b3c2d2ddSMiklos Szeredi /** 462b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 463b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 464b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 465b3c2d2ddSMiklos Szeredi * 466b3c2d2ddSMiklos Szeredi * Description: 467b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 468b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 469b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 470b3c2d2ddSMiklos Szeredi */ 47196f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 472b3c2d2ddSMiklos Szeredi { 473c725bfceSJan Kara /* 474c725bfceSJan Kara * Check for signal early to make process killable when there are 475c725bfceSJan Kara * always buffers available 476c725bfceSJan Kara */ 477c725bfceSJan Kara if (signal_pending(current)) 478c725bfceSJan Kara return -ERESTARTSYS; 479c725bfceSJan Kara 480d1a819a2SLinus Torvalds repeat: 4818cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 482b3c2d2ddSMiklos Szeredi if (!pipe->writers) 483b3c2d2ddSMiklos Szeredi return 0; 484b3c2d2ddSMiklos Szeredi 485a28c8b9dSLinus Torvalds if (sd->num_spliced) 486b3c2d2ddSMiklos Szeredi return 0; 487b3c2d2ddSMiklos Szeredi 488b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 489b3c2d2ddSMiklos Szeredi return -EAGAIN; 490b3c2d2ddSMiklos Szeredi 491b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 492b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 493b3c2d2ddSMiklos Szeredi 494b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 495b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 496b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 497b3c2d2ddSMiklos Szeredi } 498b3c2d2ddSMiklos Szeredi 499472e5b05SLinus Torvalds pipe_wait_readable(pipe); 500b3c2d2ddSMiklos Szeredi } 501b3c2d2ddSMiklos Szeredi 502d1a819a2SLinus Torvalds if (eat_empty_buffer(pipe)) 503d1a819a2SLinus Torvalds goto repeat; 504d1a819a2SLinus Torvalds 505b3c2d2ddSMiklos Szeredi return 1; 506b3c2d2ddSMiklos Szeredi } 507b3c2d2ddSMiklos Szeredi 508b3c2d2ddSMiklos Szeredi /** 509b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 510b80901bbSRandy Dunlap * @sd: information about the splice operation 511b3c2d2ddSMiklos Szeredi * 512b3c2d2ddSMiklos Szeredi * Description: 513b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 514b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 515b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 516b3c2d2ddSMiklos Szeredi */ 51796f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd) 518b3c2d2ddSMiklos Szeredi { 519b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 520b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 521b3c2d2ddSMiklos Szeredi } 522b3c2d2ddSMiklos Szeredi 523b3c2d2ddSMiklos Szeredi /** 524b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 525b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 526b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 527b3c2d2ddSMiklos Szeredi * 528b3c2d2ddSMiklos Szeredi * Description: 529b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 530b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 531b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 532b3c2d2ddSMiklos Szeredi */ 53396f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 534b3c2d2ddSMiklos Szeredi { 535b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 536b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 537b3c2d2ddSMiklos Szeredi } 538b3c2d2ddSMiklos Szeredi 539932cc6d4SJens Axboe /** 540932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 541932cc6d4SJens Axboe * @pipe: pipe to splice from 542932cc6d4SJens Axboe * @sd: information to @actor 543932cc6d4SJens Axboe * @actor: handler that splices the data 544932cc6d4SJens Axboe * 545932cc6d4SJens Axboe * Description: 546932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 547932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 548932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 549932cc6d4SJens Axboe * pipe_to_user. 550932cc6d4SJens Axboe * 55183f9135bSJens Axboe */ 552c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 553c66ab6faSJens Axboe splice_actor *actor) 5545274f052SJens Axboe { 555b3c2d2ddSMiklos Szeredi int ret; 5565274f052SJens Axboe 557b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 558b3c2d2ddSMiklos Szeredi do { 559c2489e07SJan Kara cond_resched(); 560b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 561b3c2d2ddSMiklos Szeredi if (ret > 0) 562b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 563b3c2d2ddSMiklos Szeredi } while (ret > 0); 564b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 5655274f052SJens Axboe 566b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 5675274f052SJens Axboe } 56840bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 5695274f052SJens Axboe 570932cc6d4SJens Axboe /** 571932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 572932cc6d4SJens Axboe * @pipe: pipe to splice from 573932cc6d4SJens Axboe * @out: file to splice to 574932cc6d4SJens Axboe * @ppos: position in @out 575932cc6d4SJens Axboe * @len: how many bytes to splice 576932cc6d4SJens Axboe * @flags: splice modifier flags 577932cc6d4SJens Axboe * @actor: handler that splices the data 578932cc6d4SJens Axboe * 579932cc6d4SJens Axboe * Description: 5802933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 581932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 582932cc6d4SJens Axboe * 583932cc6d4SJens Axboe */ 5846da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 5856da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 5866da61809SMark Fasheh splice_actor *actor) 5876da61809SMark Fasheh { 5886da61809SMark Fasheh ssize_t ret; 589c66ab6faSJens Axboe struct splice_desc sd = { 590c66ab6faSJens Axboe .total_len = len, 591c66ab6faSJens Axboe .flags = flags, 592c66ab6faSJens Axboe .pos = *ppos, 5936a14b90bSJens Axboe .u.file = out, 594c66ab6faSJens Axboe }; 5956da61809SMark Fasheh 59661e0d47cSMiklos Szeredi pipe_lock(pipe); 597c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 59861e0d47cSMiklos Szeredi pipe_unlock(pipe); 5996da61809SMark Fasheh 6006da61809SMark Fasheh return ret; 6016da61809SMark Fasheh } 6026da61809SMark Fasheh 6036da61809SMark Fasheh /** 6048d020765SAl Viro * iter_file_splice_write - splice data from a pipe to a file 6058d020765SAl Viro * @pipe: pipe info 6068d020765SAl Viro * @out: file to write to 6078d020765SAl Viro * @ppos: position in @out 6088d020765SAl Viro * @len: number of bytes to splice 6098d020765SAl Viro * @flags: splice modifier flags 6108d020765SAl Viro * 6118d020765SAl Viro * Description: 6128d020765SAl Viro * Will either move or copy pages (determined by @flags options) from 6138d020765SAl Viro * the given pipe inode to the given file. 6148d020765SAl Viro * This one is ->write_iter-based. 6158d020765SAl Viro * 6168d020765SAl Viro */ 6178d020765SAl Viro ssize_t 6188d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 6198d020765SAl Viro loff_t *ppos, size_t len, unsigned int flags) 6208d020765SAl Viro { 6218d020765SAl Viro struct splice_desc sd = { 6228d020765SAl Viro .total_len = len, 6238d020765SAl Viro .flags = flags, 6248d020765SAl Viro .pos = *ppos, 6258d020765SAl Viro .u.file = out, 6268d020765SAl Viro }; 6276718b6f8SDavid Howells int nbufs = pipe->max_usage; 6288d020765SAl Viro struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 6298d020765SAl Viro GFP_KERNEL); 6308d020765SAl Viro ssize_t ret; 6318d020765SAl Viro 6328d020765SAl Viro if (unlikely(!array)) 6338d020765SAl Viro return -ENOMEM; 6348d020765SAl Viro 6358d020765SAl Viro pipe_lock(pipe); 6368d020765SAl Viro 6378d020765SAl Viro splice_from_pipe_begin(&sd); 6388d020765SAl Viro while (sd.total_len) { 6398d020765SAl Viro struct iov_iter from; 640ec057595SLinus Torvalds unsigned int head, tail, mask; 6418d020765SAl Viro size_t left; 6428cefc107SDavid Howells int n; 6438d020765SAl Viro 6448d020765SAl Viro ret = splice_from_pipe_next(pipe, &sd); 6458d020765SAl Viro if (ret <= 0) 6468d020765SAl Viro break; 6478d020765SAl Viro 6486718b6f8SDavid Howells if (unlikely(nbufs < pipe->max_usage)) { 6498d020765SAl Viro kfree(array); 6506718b6f8SDavid Howells nbufs = pipe->max_usage; 6518d020765SAl Viro array = kcalloc(nbufs, sizeof(struct bio_vec), 6528d020765SAl Viro GFP_KERNEL); 6538d020765SAl Viro if (!array) { 6548d020765SAl Viro ret = -ENOMEM; 6558d020765SAl Viro break; 6568d020765SAl Viro } 6578d020765SAl Viro } 6588d020765SAl Viro 659ec057595SLinus Torvalds head = pipe->head; 660ec057595SLinus Torvalds tail = pipe->tail; 661ec057595SLinus Torvalds mask = pipe->ring_size - 1; 662ec057595SLinus Torvalds 6638d020765SAl Viro /* build the vector */ 6648d020765SAl Viro left = sd.total_len; 6658cefc107SDavid Howells for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) { 6668cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 6678d020765SAl Viro size_t this_len = buf->len; 6688d020765SAl Viro 6698d020765SAl Viro if (this_len > left) 6708d020765SAl Viro this_len = left; 6718d020765SAl Viro 672fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 6738d020765SAl Viro if (unlikely(ret)) { 6748d020765SAl Viro if (ret == -ENODATA) 6758d020765SAl Viro ret = 0; 6768d020765SAl Viro goto done; 6778d020765SAl Viro } 6788d020765SAl Viro 6798d020765SAl Viro array[n].bv_page = buf->page; 6808d020765SAl Viro array[n].bv_len = this_len; 6818d020765SAl Viro array[n].bv_offset = buf->offset; 6828d020765SAl Viro left -= this_len; 6838d020765SAl Viro } 6848d020765SAl Viro 685aa563d7bSDavid Howells iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left); 686abbb6589SChristoph Hellwig ret = vfs_iter_write(out, &from, &sd.pos, 0); 6878d020765SAl Viro if (ret <= 0) 6888d020765SAl Viro break; 6898d020765SAl Viro 6908d020765SAl Viro sd.num_spliced += ret; 6918d020765SAl Viro sd.total_len -= ret; 692dbe4e192SChristoph Hellwig *ppos = sd.pos; 6938d020765SAl Viro 6948d020765SAl Viro /* dismiss the fully eaten buffers, adjust the partial one */ 6958cefc107SDavid Howells tail = pipe->tail; 6968d020765SAl Viro while (ret) { 6978cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 6988d020765SAl Viro if (ret >= buf->len) { 6998d020765SAl Viro ret -= buf->len; 7008d020765SAl Viro buf->len = 0; 701a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 7028cefc107SDavid Howells tail++; 7038cefc107SDavid Howells pipe->tail = tail; 7048d020765SAl Viro if (pipe->files) 7058d020765SAl Viro sd.need_wakeup = true; 7068d020765SAl Viro } else { 7078d020765SAl Viro buf->offset += ret; 7088d020765SAl Viro buf->len -= ret; 7098d020765SAl Viro ret = 0; 7108d020765SAl Viro } 7118d020765SAl Viro } 7128d020765SAl Viro } 7138d020765SAl Viro done: 7148d020765SAl Viro kfree(array); 7158d020765SAl Viro splice_from_pipe_end(pipe, &sd); 7168d020765SAl Viro 7178d020765SAl Viro pipe_unlock(pipe); 7188d020765SAl Viro 7198d020765SAl Viro if (sd.num_spliced) 7208d020765SAl Viro ret = sd.num_spliced; 7218d020765SAl Viro 7228d020765SAl Viro return ret; 7238d020765SAl Viro } 7248d020765SAl Viro 7258d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write); 7268d020765SAl Viro 72783f9135bSJens Axboe /** 72883f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 729932cc6d4SJens Axboe * @pipe: pipe to splice from 73083f9135bSJens Axboe * @out: socket to write to 731932cc6d4SJens Axboe * @ppos: position in @out 73283f9135bSJens Axboe * @len: number of bytes to splice 73383f9135bSJens Axboe * @flags: splice modifier flags 73483f9135bSJens Axboe * 735932cc6d4SJens Axboe * Description: 73683f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 73783f9135bSJens Axboe * is involved. 73883f9135bSJens Axboe * 73983f9135bSJens Axboe */ 7403a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 741cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 7425274f052SJens Axboe { 74300522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 7445274f052SJens Axboe } 7455274f052SJens Axboe 746059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 747a0f06780SJeff Garzik 74836e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op) 74936e2c742SChristoph Hellwig { 75036e2c742SChristoph Hellwig pr_debug_ratelimited( 75136e2c742SChristoph Hellwig "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n", 75236e2c742SChristoph Hellwig op, file, current->pid, current->comm); 75336e2c742SChristoph Hellwig return -EINVAL; 75436e2c742SChristoph Hellwig } 75536e2c742SChristoph Hellwig 75683f9135bSJens Axboe /* 75783f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 75883f9135bSJens Axboe */ 7593a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 760cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 7615274f052SJens Axboe { 76236e2c742SChristoph Hellwig if (unlikely(!out->f_op->splice_write)) 76336e2c742SChristoph Hellwig return warn_unsupported(out, "write"); 76400c285d0SChristoph Hellwig return out->f_op->splice_write(pipe, out, ppos, len, flags); 7655274f052SJens Axboe } 7665274f052SJens Axboe 76783f9135bSJens Axboe /* 76883f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 76983f9135bSJens Axboe */ 770cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 771cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 772cbb7e577SJens Axboe unsigned int flags) 7735274f052SJens Axboe { 774313d64a3SAl Viro unsigned int p_space; 7755274f052SJens Axboe int ret; 7765274f052SJens Axboe 77749570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 7785274f052SJens Axboe return -EBADF; 7795274f052SJens Axboe 780313d64a3SAl Viro /* Don't try to read more the pipe has space for. */ 781313d64a3SAl Viro p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail); 782313d64a3SAl Viro len = min_t(size_t, len, p_space << PAGE_SHIFT); 783313d64a3SAl Viro 784cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 7855274f052SJens Axboe if (unlikely(ret < 0)) 7865274f052SJens Axboe return ret; 7875274f052SJens Axboe 78803cc0789SAl Viro if (unlikely(len > MAX_RW_COUNT)) 78903cc0789SAl Viro len = MAX_RW_COUNT; 79003cc0789SAl Viro 79136e2c742SChristoph Hellwig if (unlikely(!in->f_op->splice_read)) 79236e2c742SChristoph Hellwig return warn_unsupported(in, "read"); 7932bc01060SChristoph Hellwig return in->f_op->splice_read(in, ppos, pipe, len, flags); 7945274f052SJens Axboe } 7955274f052SJens Axboe 796932cc6d4SJens Axboe /** 797932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 798932cc6d4SJens Axboe * @in: file to splice from 799932cc6d4SJens Axboe * @sd: actor information on where to splice to 800932cc6d4SJens Axboe * @actor: handles the data splicing 801932cc6d4SJens Axboe * 802932cc6d4SJens Axboe * Description: 803932cc6d4SJens Axboe * This is a special case helper to splice directly between two 804932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 805932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 806932cc6d4SJens Axboe * that process. 807932cc6d4SJens Axboe * 808c66ab6faSJens Axboe */ 809c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 810c66ab6faSJens Axboe splice_direct_actor *actor) 811b92ce558SJens Axboe { 812b92ce558SJens Axboe struct pipe_inode_info *pipe; 813b92ce558SJens Axboe long ret, bytes; 814b92ce558SJens Axboe umode_t i_mode; 815c66ab6faSJens Axboe size_t len; 8160ff28d9fSChristophe Leroy int i, flags, more; 817b92ce558SJens Axboe 818b92ce558SJens Axboe /* 819b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 820b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 821b92ce558SJens Axboe * piped splicing for that! 822b92ce558SJens Axboe */ 823496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 824b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 825b92ce558SJens Axboe return -EINVAL; 826b92ce558SJens Axboe 827b92ce558SJens Axboe /* 828b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 829b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 830b92ce558SJens Axboe */ 831b92ce558SJens Axboe pipe = current->splice_pipe; 83249570e9bSJens Axboe if (unlikely(!pipe)) { 8337bee130eSAl Viro pipe = alloc_pipe_info(); 834b92ce558SJens Axboe if (!pipe) 835b92ce558SJens Axboe return -ENOMEM; 836b92ce558SJens Axboe 837b92ce558SJens Axboe /* 838b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 83900522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 840b92ce558SJens Axboe * PIPE_READERS appropriately. 841b92ce558SJens Axboe */ 842b92ce558SJens Axboe pipe->readers = 1; 843b92ce558SJens Axboe 844b92ce558SJens Axboe current->splice_pipe = pipe; 845b92ce558SJens Axboe } 846b92ce558SJens Axboe 847b92ce558SJens Axboe /* 84873d62d83SIngo Molnar * Do the splice. 849b92ce558SJens Axboe */ 850b92ce558SJens Axboe ret = 0; 851b92ce558SJens Axboe bytes = 0; 852c66ab6faSJens Axboe len = sd->total_len; 853c66ab6faSJens Axboe flags = sd->flags; 854c66ab6faSJens Axboe 855c66ab6faSJens Axboe /* 856c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 857c66ab6faSJens Axboe */ 858c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 8590ff28d9fSChristophe Leroy more = sd->flags & SPLICE_F_MORE; 860b92ce558SJens Axboe 8618cefc107SDavid Howells WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); 86217614445SDarrick J. Wong 863b92ce558SJens Axboe while (len) { 86451a92c0fSJens Axboe size_t read_len; 865a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 866b92ce558SJens Axboe 867313d64a3SAl Viro ret = do_splice_to(in, &pos, pipe, len, flags); 86851a92c0fSJens Axboe if (unlikely(ret <= 0)) 869b92ce558SJens Axboe goto out_release; 870b92ce558SJens Axboe 871b92ce558SJens Axboe read_len = ret; 872c66ab6faSJens Axboe sd->total_len = read_len; 873b92ce558SJens Axboe 874b92ce558SJens Axboe /* 8750ff28d9fSChristophe Leroy * If more data is pending, set SPLICE_F_MORE 8760ff28d9fSChristophe Leroy * If this is the last data and SPLICE_F_MORE was not set 8770ff28d9fSChristophe Leroy * initially, clears it. 8780ff28d9fSChristophe Leroy */ 8790ff28d9fSChristophe Leroy if (read_len < len) 8800ff28d9fSChristophe Leroy sd->flags |= SPLICE_F_MORE; 8810ff28d9fSChristophe Leroy else if (!more) 8820ff28d9fSChristophe Leroy sd->flags &= ~SPLICE_F_MORE; 8830ff28d9fSChristophe Leroy /* 884b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 885b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 886b92ce558SJens Axboe * could get stuck data in the internal pipe: 887b92ce558SJens Axboe */ 888c66ab6faSJens Axboe ret = actor(pipe, sd); 889a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 890a82c53a0STom Zanussi sd->pos = prev_pos; 891b92ce558SJens Axboe goto out_release; 892a82c53a0STom Zanussi } 893b92ce558SJens Axboe 894b92ce558SJens Axboe bytes += ret; 895b92ce558SJens Axboe len -= ret; 896bcd4f3acSJens Axboe sd->pos = pos; 897b92ce558SJens Axboe 898a82c53a0STom Zanussi if (ret < read_len) { 899a82c53a0STom Zanussi sd->pos = prev_pos + ret; 90051a92c0fSJens Axboe goto out_release; 901b92ce558SJens Axboe } 902a82c53a0STom Zanussi } 903b92ce558SJens Axboe 9049e97198dSJens Axboe done: 9058cefc107SDavid Howells pipe->tail = pipe->head = 0; 9069e97198dSJens Axboe file_accessed(in); 907b92ce558SJens Axboe return bytes; 908b92ce558SJens Axboe 909b92ce558SJens Axboe out_release: 910b92ce558SJens Axboe /* 911b92ce558SJens Axboe * If we did an incomplete transfer we must release 912b92ce558SJens Axboe * the pipe buffers in question: 913b92ce558SJens Axboe */ 9148cefc107SDavid Howells for (i = 0; i < pipe->ring_size; i++) { 9158cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[i]; 916b92ce558SJens Axboe 917a779638cSMiklos Szeredi if (buf->ops) 918a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 919b92ce558SJens Axboe } 920b92ce558SJens Axboe 9219e97198dSJens Axboe if (!bytes) 9229e97198dSJens Axboe bytes = ret; 923b92ce558SJens Axboe 9249e97198dSJens Axboe goto done; 925c66ab6faSJens Axboe } 926c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 927c66ab6faSJens Axboe 928c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 929c66ab6faSJens Axboe struct splice_desc *sd) 930c66ab6faSJens Axboe { 9316a14b90bSJens Axboe struct file *file = sd->u.file; 932c66ab6faSJens Axboe 9337995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 9342cb4b05eSChangli Gao sd->flags); 935c66ab6faSJens Axboe } 936c66ab6faSJens Axboe 937932cc6d4SJens Axboe /** 938932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 939932cc6d4SJens Axboe * @in: file to splice from 940932cc6d4SJens Axboe * @ppos: input file offset 941932cc6d4SJens Axboe * @out: file to splice to 942acdb37c3SRandy Dunlap * @opos: output file offset 943932cc6d4SJens Axboe * @len: number of bytes to splice 944932cc6d4SJens Axboe * @flags: splice modifier flags 945932cc6d4SJens Axboe * 946932cc6d4SJens Axboe * Description: 947932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 948932cc6d4SJens Axboe * doing it in the application would incur an extra system call 949932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 950932cc6d4SJens Axboe * can splice directly through a process-private pipe. 951932cc6d4SJens Axboe * 952932cc6d4SJens Axboe */ 953c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 9547995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 955c66ab6faSJens Axboe { 956c66ab6faSJens Axboe struct splice_desc sd = { 957c66ab6faSJens Axboe .len = len, 958c66ab6faSJens Axboe .total_len = len, 959c66ab6faSJens Axboe .flags = flags, 960c66ab6faSJens Axboe .pos = *ppos, 9616a14b90bSJens Axboe .u.file = out, 9627995bd28SAl Viro .opos = opos, 963c66ab6faSJens Axboe }; 96451a92c0fSJens Axboe long ret; 965c66ab6faSJens Axboe 96618c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 96718c67cb9SAl Viro return -EBADF; 96818c67cb9SAl Viro 96918c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 97018c67cb9SAl Viro return -EINVAL; 97118c67cb9SAl Viro 97218c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 97318c67cb9SAl Viro if (unlikely(ret < 0)) 97418c67cb9SAl Viro return ret; 97518c67cb9SAl Viro 976c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 97751a92c0fSJens Axboe if (ret > 0) 978a82c53a0STom Zanussi *ppos = sd.pos; 97951a92c0fSJens Axboe 980c66ab6faSJens Axboe return ret; 981b92ce558SJens Axboe } 9821c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct); 983b92ce558SJens Axboe 9848924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) 9858924feffSAl Viro { 98652bce911SLinus Torvalds for (;;) { 98752bce911SLinus Torvalds if (unlikely(!pipe->readers)) { 98852bce911SLinus Torvalds send_sig(SIGPIPE, current, 0); 98952bce911SLinus Torvalds return -EPIPE; 99052bce911SLinus Torvalds } 9916718b6f8SDavid Howells if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 99252bce911SLinus Torvalds return 0; 9938924feffSAl Viro if (flags & SPLICE_F_NONBLOCK) 9948924feffSAl Viro return -EAGAIN; 9958924feffSAl Viro if (signal_pending(current)) 9968924feffSAl Viro return -ERESTARTSYS; 997472e5b05SLinus Torvalds pipe_wait_writable(pipe); 9988924feffSAl Viro } 9998924feffSAl Viro } 10008924feffSAl Viro 10017c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 10027c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 10037c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1004ddac0d39SJens Axboe 1005*b964bf53SAl Viro long splice_file_to_pipe(struct file *in, 1006faa97c48SAl Viro struct pipe_inode_info *opipe, 1007faa97c48SAl Viro loff_t *offset, 1008faa97c48SAl Viro size_t len, unsigned int flags) 1009faa97c48SAl Viro { 1010faa97c48SAl Viro long ret; 1011faa97c48SAl Viro 1012faa97c48SAl Viro pipe_lock(opipe); 1013faa97c48SAl Viro ret = wait_for_space(opipe, flags); 1014faa97c48SAl Viro if (!ret) 1015faa97c48SAl Viro ret = do_splice_to(in, offset, opipe, len, flags); 1016faa97c48SAl Viro pipe_unlock(opipe); 1017faa97c48SAl Viro if (ret > 0) 1018faa97c48SAl Viro wakeup_pipe_readers(opipe); 1019faa97c48SAl Viro return ret; 1020faa97c48SAl Viro } 1021faa97c48SAl Viro 1022ddac0d39SJens Axboe /* 102383f9135bSJens Axboe * Determine where to splice to/from. 102483f9135bSJens Axboe */ 1025ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out, 1026ee6e00c8SJens Axboe loff_t *off_out, size_t len, unsigned int flags) 10275274f052SJens Axboe { 10287c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 10297c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 10307995bd28SAl Viro loff_t offset; 1031a4514ebdSJens Axboe long ret; 10325274f052SJens Axboe 103390da2e3fSPavel Begunkov if (unlikely(!(in->f_mode & FMODE_READ) || 103490da2e3fSPavel Begunkov !(out->f_mode & FMODE_WRITE))) 103590da2e3fSPavel Begunkov return -EBADF; 103690da2e3fSPavel Begunkov 1037c73be61cSDavid Howells ipipe = get_pipe_info(in, true); 1038c73be61cSDavid Howells opipe = get_pipe_info(out, true); 10397c77f0b3SMiklos Szeredi 10407c77f0b3SMiklos Szeredi if (ipipe && opipe) { 10417c77f0b3SMiklos Szeredi if (off_in || off_out) 10427c77f0b3SMiklos Szeredi return -ESPIPE; 10437c77f0b3SMiklos Szeredi 10447c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 10457c77f0b3SMiklos Szeredi if (ipipe == opipe) 10467c77f0b3SMiklos Szeredi return -EINVAL; 10477c77f0b3SMiklos Szeredi 1048ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1049ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1050ee5e0011SSlavomir Kaslev 10517c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 10527c77f0b3SMiklos Szeredi } 10537c77f0b3SMiklos Szeredi 10547c77f0b3SMiklos Szeredi if (ipipe) { 1055529565dcSIngo Molnar if (off_in) 1056529565dcSIngo Molnar return -ESPIPE; 1057b92ce558SJens Axboe if (off_out) { 105819c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1059b92ce558SJens Axboe return -EINVAL; 1060ee6e00c8SJens Axboe offset = *off_out; 10617995bd28SAl Viro } else { 10627995bd28SAl Viro offset = out->f_pos; 10637995bd28SAl Viro } 1064529565dcSIngo Molnar 106518c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 106618c67cb9SAl Viro return -EINVAL; 106718c67cb9SAl Viro 106818c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 106918c67cb9SAl Viro if (unlikely(ret < 0)) 107018c67cb9SAl Viro return ret; 107118c67cb9SAl Viro 1072ee5e0011SSlavomir Kaslev if (in->f_flags & O_NONBLOCK) 1073ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1074ee5e0011SSlavomir Kaslev 1075500368f7SAl Viro file_start_write(out); 10767995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1077500368f7SAl Viro file_end_write(out); 1078a4514ebdSJens Axboe 10797995bd28SAl Viro if (!off_out) 10807995bd28SAl Viro out->f_pos = offset; 1081ee6e00c8SJens Axboe else 1082ee6e00c8SJens Axboe *off_out = offset; 1083a4514ebdSJens Axboe 1084a4514ebdSJens Axboe return ret; 1085529565dcSIngo Molnar } 10865274f052SJens Axboe 10877c77f0b3SMiklos Szeredi if (opipe) { 1088529565dcSIngo Molnar if (off_out) 1089529565dcSIngo Molnar return -ESPIPE; 1090b92ce558SJens Axboe if (off_in) { 109119c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1092b92ce558SJens Axboe return -EINVAL; 1093ee6e00c8SJens Axboe offset = *off_in; 10947995bd28SAl Viro } else { 10957995bd28SAl Viro offset = in->f_pos; 10967995bd28SAl Viro } 1097529565dcSIngo Molnar 1098ee5e0011SSlavomir Kaslev if (out->f_flags & O_NONBLOCK) 1099ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1100ee5e0011SSlavomir Kaslev 1101faa97c48SAl Viro ret = splice_file_to_pipe(in, opipe, &offset, len, flags); 11027995bd28SAl Viro if (!off_in) 11037995bd28SAl Viro in->f_pos = offset; 1104ee6e00c8SJens Axboe else 1105ee6e00c8SJens Axboe *off_in = offset; 1106a4514ebdSJens Axboe 1107a4514ebdSJens Axboe return ret; 1108529565dcSIngo Molnar } 11095274f052SJens Axboe 11105274f052SJens Axboe return -EINVAL; 11115274f052SJens Axboe } 11125274f052SJens Axboe 1113ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in, 1114ee6e00c8SJens Axboe struct file *out, loff_t __user *off_out, 1115ee6e00c8SJens Axboe size_t len, unsigned int flags) 1116ee6e00c8SJens Axboe { 1117ee6e00c8SJens Axboe struct pipe_inode_info *ipipe; 1118ee6e00c8SJens Axboe struct pipe_inode_info *opipe; 1119ee6e00c8SJens Axboe loff_t offset, *__off_in = NULL, *__off_out = NULL; 1120ee6e00c8SJens Axboe long ret; 1121ee6e00c8SJens Axboe 1122ee6e00c8SJens Axboe ipipe = get_pipe_info(in, true); 1123ee6e00c8SJens Axboe opipe = get_pipe_info(out, true); 1124ee6e00c8SJens Axboe 1125ee6e00c8SJens Axboe if (ipipe && off_in) 1126ee6e00c8SJens Axboe return -ESPIPE; 1127ee6e00c8SJens Axboe if (opipe && off_out) 1128ee6e00c8SJens Axboe return -ESPIPE; 1129ee6e00c8SJens Axboe 1130ee6e00c8SJens Axboe if (off_out) { 1131ee6e00c8SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1132ee6e00c8SJens Axboe return -EFAULT; 1133ee6e00c8SJens Axboe __off_out = &offset; 1134ee6e00c8SJens Axboe } 1135ee6e00c8SJens Axboe if (off_in) { 1136ee6e00c8SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1137ee6e00c8SJens Axboe return -EFAULT; 1138ee6e00c8SJens Axboe __off_in = &offset; 1139ee6e00c8SJens Axboe } 1140ee6e00c8SJens Axboe 1141ee6e00c8SJens Axboe ret = do_splice(in, __off_in, out, __off_out, len, flags); 1142ee6e00c8SJens Axboe if (ret < 0) 1143ee6e00c8SJens Axboe return ret; 1144ee6e00c8SJens Axboe 1145ee6e00c8SJens Axboe if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t))) 1146ee6e00c8SJens Axboe return -EFAULT; 1147ee6e00c8SJens Axboe if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t))) 1148ee6e00c8SJens Axboe return -EFAULT; 1149ee6e00c8SJens Axboe 1150ee6e00c8SJens Axboe return ret; 1151ee6e00c8SJens Axboe } 1152ee6e00c8SJens Axboe 115379fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from, 115479fddc4eSAl Viro struct pipe_inode_info *pipe, 115579fddc4eSAl Viro unsigned flags) 1156912d35f8SJens Axboe { 115779fddc4eSAl Viro struct pipe_buffer buf = { 115879fddc4eSAl Viro .ops = &user_page_pipe_buf_ops, 115979fddc4eSAl Viro .flags = flags 116079fddc4eSAl Viro }; 116179fddc4eSAl Viro size_t total = 0; 116279fddc4eSAl Viro int ret = 0; 116379fddc4eSAl Viro bool failed = false; 116479fddc4eSAl Viro 116579fddc4eSAl Viro while (iov_iter_count(from) && !failed) { 116679fddc4eSAl Viro struct page *pages[16]; 1167db85a9ebSAl Viro ssize_t copied; 1168db85a9ebSAl Viro size_t start; 116979fddc4eSAl Viro int n; 1170912d35f8SJens Axboe 117179fddc4eSAl Viro copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start); 117279fddc4eSAl Viro if (copied <= 0) { 117379fddc4eSAl Viro ret = copied; 117479fddc4eSAl Viro break; 117579fddc4eSAl Viro } 1176912d35f8SJens Axboe 117779fddc4eSAl Viro for (n = 0; copied; n++, start = 0) { 1178db85a9ebSAl Viro int size = min_t(int, copied, PAGE_SIZE - start); 117979fddc4eSAl Viro if (!failed) { 118079fddc4eSAl Viro buf.page = pages[n]; 118179fddc4eSAl Viro buf.offset = start; 118279fddc4eSAl Viro buf.len = size; 118379fddc4eSAl Viro ret = add_to_pipe(pipe, &buf); 118479fddc4eSAl Viro if (unlikely(ret < 0)) { 118579fddc4eSAl Viro failed = true; 118679fddc4eSAl Viro } else { 118779fddc4eSAl Viro iov_iter_advance(from, ret); 118879fddc4eSAl Viro total += ret; 118979fddc4eSAl Viro } 119079fddc4eSAl Viro } else { 119179fddc4eSAl Viro put_page(pages[n]); 119279fddc4eSAl Viro } 1193db85a9ebSAl Viro copied -= size; 1194912d35f8SJens Axboe } 1195912d35f8SJens Axboe } 119679fddc4eSAl Viro return total ? total : ret; 1197912d35f8SJens Axboe } 1198912d35f8SJens Axboe 11996a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 12006a14b90bSJens Axboe struct splice_desc *sd) 12016a14b90bSJens Axboe { 12026130f531SAl Viro int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 12036130f531SAl Viro return n == sd->len ? n : -EFAULT; 12046a14b90bSJens Axboe } 12056a14b90bSJens Axboe 12066a14b90bSJens Axboe /* 12076a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 12086a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 12096a14b90bSJens Axboe */ 121087a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter, 121187a3002aSAl Viro unsigned int flags) 12126a14b90bSJens Axboe { 1213c73be61cSDavid Howells struct pipe_inode_info *pipe = get_pipe_info(file, true); 121487a3002aSAl Viro struct splice_desc sd = { 121587a3002aSAl Viro .total_len = iov_iter_count(iter), 121687a3002aSAl Viro .flags = flags, 121787a3002aSAl Viro .u.data = iter 121887a3002aSAl Viro }; 121987a3002aSAl Viro long ret = 0; 12206a14b90bSJens Axboe 12216a14b90bSJens Axboe if (!pipe) 12226a14b90bSJens Axboe return -EBADF; 12236a14b90bSJens Axboe 1224345995faSAl Viro if (sd.total_len) { 12256130f531SAl Viro pipe_lock(pipe); 12266130f531SAl Viro ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 122761e0d47cSMiklos Szeredi pipe_unlock(pipe); 1228345995faSAl Viro } 12296a14b90bSJens Axboe 12306a14b90bSJens Axboe return ret; 12316a14b90bSJens Axboe } 12326a14b90bSJens Axboe 1233912d35f8SJens Axboe /* 1234912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1235912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1236912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1237912d35f8SJens Axboe */ 123887a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, 123987a3002aSAl Viro unsigned int flags) 1240912d35f8SJens Axboe { 1241ddac0d39SJens Axboe struct pipe_inode_info *pipe; 124287a3002aSAl Viro long ret = 0; 124379fddc4eSAl Viro unsigned buf_flag = 0; 124479fddc4eSAl Viro 124579fddc4eSAl Viro if (flags & SPLICE_F_GIFT) 124679fddc4eSAl Viro buf_flag = PIPE_BUF_FLAG_GIFT; 1247912d35f8SJens Axboe 1248c73be61cSDavid Howells pipe = get_pipe_info(file, true); 1249ddac0d39SJens Axboe if (!pipe) 1250912d35f8SJens Axboe return -EBADF; 1251912d35f8SJens Axboe 12528924feffSAl Viro pipe_lock(pipe); 12538924feffSAl Viro ret = wait_for_space(pipe, flags); 125479fddc4eSAl Viro if (!ret) 125587a3002aSAl Viro ret = iter_to_pipe(iter, pipe, buf_flag); 12568924feffSAl Viro pipe_unlock(pipe); 12578924feffSAl Viro if (ret > 0) 12588924feffSAl Viro wakeup_pipe_readers(pipe); 125935f3d14dSJens Axboe return ret; 1260912d35f8SJens Axboe } 1261912d35f8SJens Axboe 126287a3002aSAl Viro static int vmsplice_type(struct fd f, int *type) 126387a3002aSAl Viro { 126487a3002aSAl Viro if (!f.file) 126587a3002aSAl Viro return -EBADF; 126687a3002aSAl Viro if (f.file->f_mode & FMODE_WRITE) { 126787a3002aSAl Viro *type = WRITE; 126887a3002aSAl Viro } else if (f.file->f_mode & FMODE_READ) { 126987a3002aSAl Viro *type = READ; 127087a3002aSAl Viro } else { 127187a3002aSAl Viro fdput(f); 127287a3002aSAl Viro return -EBADF; 127387a3002aSAl Viro } 127487a3002aSAl Viro return 0; 127587a3002aSAl Viro } 127687a3002aSAl Viro 12776a14b90bSJens Axboe /* 12786a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 12796a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 12806a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 12816a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 12826a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 12836a14b90bSJens Axboe * solutions for that: 12846a14b90bSJens Axboe * 12856a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 12866a14b90bSJens Axboe * do a regular read() on the buffer anyway. 12876a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 12886a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 12896a14b90bSJens Axboe * 12906a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 12916a14b90bSJens Axboe * 12926a14b90bSJens Axboe */ 129387a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, 129430cfe4efSDominik Brodowski unsigned long, nr_segs, unsigned int, flags) 129530cfe4efSDominik Brodowski { 129687a3002aSAl Viro struct iovec iovstack[UIO_FASTIOV]; 129787a3002aSAl Viro struct iovec *iov = iovstack; 129887a3002aSAl Viro struct iov_iter iter; 129987e5e6daSJens Axboe ssize_t error; 130087a3002aSAl Viro struct fd f; 130187a3002aSAl Viro int type; 130287a3002aSAl Viro 1303598b3cecSChristoph Hellwig if (unlikely(flags & ~SPLICE_F_ALL)) 1304598b3cecSChristoph Hellwig return -EINVAL; 1305598b3cecSChristoph Hellwig 130687a3002aSAl Viro f = fdget(fd); 130787a3002aSAl Viro error = vmsplice_type(f, &type); 130887a3002aSAl Viro if (error) 130987a3002aSAl Viro return error; 131087a3002aSAl Viro 131187a3002aSAl Viro error = import_iovec(type, uiov, nr_segs, 131287a3002aSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 1313598b3cecSChristoph Hellwig if (error < 0) 1314598b3cecSChristoph Hellwig goto out_fdput; 1315598b3cecSChristoph Hellwig 1316598b3cecSChristoph Hellwig if (!iov_iter_count(&iter)) 1317598b3cecSChristoph Hellwig error = 0; 1318598b3cecSChristoph Hellwig else if (iov_iter_rw(&iter) == WRITE) 1319598b3cecSChristoph Hellwig error = vmsplice_to_pipe(f.file, &iter, flags); 1320598b3cecSChristoph Hellwig else 1321598b3cecSChristoph Hellwig error = vmsplice_to_user(f.file, &iter, flags); 1322598b3cecSChristoph Hellwig 132387a3002aSAl Viro kfree(iov); 1324598b3cecSChristoph Hellwig out_fdput: 132587a3002aSAl Viro fdput(f); 132687a3002aSAl Viro return error; 132730cfe4efSDominik Brodowski } 132830cfe4efSDominik Brodowski 1329836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1330836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1331836f92adSHeiko Carstens size_t, len, unsigned int, flags) 13325274f052SJens Axboe { 13332903ff01SAl Viro struct fd in, out; 13345274f052SJens Axboe long error; 13355274f052SJens Axboe 13365274f052SJens Axboe if (unlikely(!len)) 13375274f052SJens Axboe return 0; 13385274f052SJens Axboe 13393d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 13403d6ea290SAl Viro return -EINVAL; 13413d6ea290SAl Viro 13425274f052SJens Axboe error = -EBADF; 13432903ff01SAl Viro in = fdget(fd_in); 13442903ff01SAl Viro if (in.file) { 13452903ff01SAl Viro out = fdget(fd_out); 13462903ff01SAl Viro if (out.file) { 1347ee6e00c8SJens Axboe error = __do_splice(in.file, off_in, out.file, off_out, 1348529565dcSIngo Molnar len, flags); 13492903ff01SAl Viro fdput(out); 13505274f052SJens Axboe } 13512903ff01SAl Viro fdput(in); 13525274f052SJens Axboe } 13535274f052SJens Axboe return error; 13545274f052SJens Axboe } 135570524490SJens Axboe 135670524490SJens Axboe /* 1357aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1358aadd06e5SJens Axboe * return an appropriate error. 1359aadd06e5SJens Axboe */ 13607c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1361aadd06e5SJens Axboe { 1362aadd06e5SJens Axboe int ret; 1363aadd06e5SJens Axboe 1364aadd06e5SJens Axboe /* 13658cefc107SDavid Howells * Check the pipe occupancy without the inode lock first. This function 1366aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1367aadd06e5SJens Axboe */ 13688cefc107SDavid Howells if (!pipe_empty(pipe->head, pipe->tail)) 1369aadd06e5SJens Axboe return 0; 1370aadd06e5SJens Axboe 1371aadd06e5SJens Axboe ret = 0; 137261e0d47cSMiklos Szeredi pipe_lock(pipe); 1373aadd06e5SJens Axboe 13748cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 1375aadd06e5SJens Axboe if (signal_pending(current)) { 1376aadd06e5SJens Axboe ret = -ERESTARTSYS; 1377aadd06e5SJens Axboe break; 1378aadd06e5SJens Axboe } 1379aadd06e5SJens Axboe if (!pipe->writers) 1380aadd06e5SJens Axboe break; 1381aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1382aadd06e5SJens Axboe ret = -EAGAIN; 1383aadd06e5SJens Axboe break; 1384aadd06e5SJens Axboe } 1385472e5b05SLinus Torvalds pipe_wait_readable(pipe); 1386aadd06e5SJens Axboe } 1387aadd06e5SJens Axboe 138861e0d47cSMiklos Szeredi pipe_unlock(pipe); 1389aadd06e5SJens Axboe return ret; 1390aadd06e5SJens Axboe } 1391aadd06e5SJens Axboe 1392aadd06e5SJens Axboe /* 1393aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1394aadd06e5SJens Axboe * return an appropriate error. 1395aadd06e5SJens Axboe */ 13967c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1397aadd06e5SJens Axboe { 1398aadd06e5SJens Axboe int ret; 1399aadd06e5SJens Axboe 1400aadd06e5SJens Axboe /* 14018cefc107SDavid Howells * Check pipe occupancy without the inode lock first. This function 1402aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1403aadd06e5SJens Axboe */ 1404566d1362STetsuo Handa if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 1405aadd06e5SJens Axboe return 0; 1406aadd06e5SJens Axboe 1407aadd06e5SJens Axboe ret = 0; 140861e0d47cSMiklos Szeredi pipe_lock(pipe); 1409aadd06e5SJens Axboe 14106718b6f8SDavid Howells while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 1411aadd06e5SJens Axboe if (!pipe->readers) { 1412aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1413aadd06e5SJens Axboe ret = -EPIPE; 1414aadd06e5SJens Axboe break; 1415aadd06e5SJens Axboe } 1416aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1417aadd06e5SJens Axboe ret = -EAGAIN; 1418aadd06e5SJens Axboe break; 1419aadd06e5SJens Axboe } 1420aadd06e5SJens Axboe if (signal_pending(current)) { 1421aadd06e5SJens Axboe ret = -ERESTARTSYS; 1422aadd06e5SJens Axboe break; 1423aadd06e5SJens Axboe } 1424472e5b05SLinus Torvalds pipe_wait_writable(pipe); 1425aadd06e5SJens Axboe } 1426aadd06e5SJens Axboe 142761e0d47cSMiklos Szeredi pipe_unlock(pipe); 1428aadd06e5SJens Axboe return ret; 1429aadd06e5SJens Axboe } 1430aadd06e5SJens Axboe 1431aadd06e5SJens Axboe /* 14327c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 14337c77f0b3SMiklos Szeredi */ 14347c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 14357c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 14367c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 14377c77f0b3SMiklos Szeredi { 14387c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 14398cefc107SDavid Howells unsigned int i_head, o_head; 14408cefc107SDavid Howells unsigned int i_tail, o_tail; 14418cefc107SDavid Howells unsigned int i_mask, o_mask; 14428cefc107SDavid Howells int ret = 0; 14437c77f0b3SMiklos Szeredi bool input_wakeup = false; 14447c77f0b3SMiklos Szeredi 14457c77f0b3SMiklos Szeredi 14467c77f0b3SMiklos Szeredi retry: 14477c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 14487c77f0b3SMiklos Szeredi if (ret) 14497c77f0b3SMiklos Szeredi return ret; 14507c77f0b3SMiklos Szeredi 14517c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 14527c77f0b3SMiklos Szeredi if (ret) 14537c77f0b3SMiklos Szeredi return ret; 14547c77f0b3SMiklos Szeredi 14557c77f0b3SMiklos Szeredi /* 14567c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 14577c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 14587c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 14597c77f0b3SMiklos Szeredi */ 14607c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 14617c77f0b3SMiklos Szeredi 14628cefc107SDavid Howells i_tail = ipipe->tail; 14638cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 14648cefc107SDavid Howells o_head = opipe->head; 14658cefc107SDavid Howells o_mask = opipe->ring_size - 1; 14668cefc107SDavid Howells 14677c77f0b3SMiklos Szeredi do { 14688cefc107SDavid Howells size_t o_len; 14698cefc107SDavid Howells 14707c77f0b3SMiklos Szeredi if (!opipe->readers) { 14717c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 14727c77f0b3SMiklos Szeredi if (!ret) 14737c77f0b3SMiklos Szeredi ret = -EPIPE; 14747c77f0b3SMiklos Szeredi break; 14757c77f0b3SMiklos Szeredi } 14767c77f0b3SMiklos Szeredi 14778cefc107SDavid Howells i_head = ipipe->head; 14788cefc107SDavid Howells o_tail = opipe->tail; 14798cefc107SDavid Howells 14808cefc107SDavid Howells if (pipe_empty(i_head, i_tail) && !ipipe->writers) 14817c77f0b3SMiklos Szeredi break; 14827c77f0b3SMiklos Szeredi 14837c77f0b3SMiklos Szeredi /* 14847c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 14857c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 14867c77f0b3SMiklos Szeredi */ 14878cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 14886718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) { 14897c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 14907c77f0b3SMiklos Szeredi if (ret) 14917c77f0b3SMiklos Szeredi break; 14927c77f0b3SMiklos Szeredi 14937c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 14947c77f0b3SMiklos Szeredi ret = -EAGAIN; 14957c77f0b3SMiklos Szeredi break; 14967c77f0b3SMiklos Szeredi } 14977c77f0b3SMiklos Szeredi 14987c77f0b3SMiklos Szeredi /* 14997c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 15007c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 15017c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 15027c77f0b3SMiklos Szeredi */ 15037c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 15047c77f0b3SMiklos Szeredi pipe_unlock(opipe); 15057c77f0b3SMiklos Szeredi goto retry; 15067c77f0b3SMiklos Szeredi } 15077c77f0b3SMiklos Szeredi 15088cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 15098cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 15107c77f0b3SMiklos Szeredi 15117c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 15127c77f0b3SMiklos Szeredi /* 15137c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 15147c77f0b3SMiklos Szeredi */ 15157c77f0b3SMiklos Szeredi *obuf = *ibuf; 15167c77f0b3SMiklos Szeredi ibuf->ops = NULL; 15178cefc107SDavid Howells i_tail++; 15188cefc107SDavid Howells ipipe->tail = i_tail; 15197c77f0b3SMiklos Szeredi input_wakeup = true; 15208cefc107SDavid Howells o_len = obuf->len; 15218cefc107SDavid Howells o_head++; 15228cefc107SDavid Howells opipe->head = o_head; 15237c77f0b3SMiklos Szeredi } else { 15247c77f0b3SMiklos Szeredi /* 15257c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 15267c77f0b3SMiklos Szeredi * so we can copy the contents over. 15277c77f0b3SMiklos Szeredi */ 152815fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 152915fab63eSMatthew Wilcox if (ret == 0) 153015fab63eSMatthew Wilcox ret = -EFAULT; 153115fab63eSMatthew Wilcox break; 153215fab63eSMatthew Wilcox } 15337c77f0b3SMiklos Szeredi *obuf = *ibuf; 15347c77f0b3SMiklos Szeredi 15357c77f0b3SMiklos Szeredi /* 1536f6dd9755SChristoph Hellwig * Don't inherit the gift and merge flags, we need to 15377c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 15387c77f0b3SMiklos Szeredi */ 15397c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 1540f6dd9755SChristoph Hellwig obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 1541a0ce2f0aSJann Horn 15427c77f0b3SMiklos Szeredi obuf->len = len; 15438cefc107SDavid Howells ibuf->offset += len; 15448cefc107SDavid Howells ibuf->len -= len; 15458cefc107SDavid Howells o_len = len; 15468cefc107SDavid Howells o_head++; 15478cefc107SDavid Howells opipe->head = o_head; 15487c77f0b3SMiklos Szeredi } 15498cefc107SDavid Howells ret += o_len; 15508cefc107SDavid Howells len -= o_len; 15517c77f0b3SMiklos Szeredi } while (len); 15527c77f0b3SMiklos Szeredi 15537c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 15547c77f0b3SMiklos Szeredi pipe_unlock(opipe); 15557c77f0b3SMiklos Szeredi 15567c77f0b3SMiklos Szeredi /* 15577c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 15587c77f0b3SMiklos Szeredi */ 1559825cdcb1SNamhyung Kim if (ret > 0) 1560825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1561825cdcb1SNamhyung Kim 15627c77f0b3SMiklos Szeredi if (input_wakeup) 15637c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 15647c77f0b3SMiklos Szeredi 15657c77f0b3SMiklos Szeredi return ret; 15667c77f0b3SMiklos Szeredi } 15677c77f0b3SMiklos Szeredi 15687c77f0b3SMiklos Szeredi /* 156970524490SJens Axboe * Link contents of ipipe to opipe. 157070524490SJens Axboe */ 157170524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 157270524490SJens Axboe struct pipe_inode_info *opipe, 157370524490SJens Axboe size_t len, unsigned int flags) 157470524490SJens Axboe { 157570524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 15768cefc107SDavid Howells unsigned int i_head, o_head; 15778cefc107SDavid Howells unsigned int i_tail, o_tail; 15788cefc107SDavid Howells unsigned int i_mask, o_mask; 15798cefc107SDavid Howells int ret = 0; 158070524490SJens Axboe 158170524490SJens Axboe /* 158270524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 158361e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 158470524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 158570524490SJens Axboe */ 158661e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 158770524490SJens Axboe 15888cefc107SDavid Howells i_tail = ipipe->tail; 15898cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 15908cefc107SDavid Howells o_head = opipe->head; 15918cefc107SDavid Howells o_mask = opipe->ring_size - 1; 15928cefc107SDavid Howells 1593aadd06e5SJens Axboe do { 159470524490SJens Axboe if (!opipe->readers) { 159570524490SJens Axboe send_sig(SIGPIPE, current, 0); 159670524490SJens Axboe if (!ret) 159770524490SJens Axboe ret = -EPIPE; 159870524490SJens Axboe break; 159970524490SJens Axboe } 160070524490SJens Axboe 16018cefc107SDavid Howells i_head = ipipe->head; 16028cefc107SDavid Howells o_tail = opipe->tail; 16038cefc107SDavid Howells 160470524490SJens Axboe /* 16058cefc107SDavid Howells * If we have iterated all input buffers or run out of 1606aadd06e5SJens Axboe * output room, break. 160770524490SJens Axboe */ 16088cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 16096718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) 1610aadd06e5SJens Axboe break; 1611aadd06e5SJens Axboe 16128cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 16138cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 161470524490SJens Axboe 161570524490SJens Axboe /* 161670524490SJens Axboe * Get a reference to this pipe buffer, 161770524490SJens Axboe * so we can copy the contents over. 161870524490SJens Axboe */ 161915fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 162015fab63eSMatthew Wilcox if (ret == 0) 162115fab63eSMatthew Wilcox ret = -EFAULT; 162215fab63eSMatthew Wilcox break; 162315fab63eSMatthew Wilcox } 162470524490SJens Axboe 162570524490SJens Axboe *obuf = *ibuf; 162670524490SJens Axboe 16277afa6fd0SJens Axboe /* 1628f6dd9755SChristoph Hellwig * Don't inherit the gift and merge flag, we need to prevent 1629f6dd9755SChristoph Hellwig * multiple steals of this page. 16307afa6fd0SJens Axboe */ 16317afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 1632f6dd9755SChristoph Hellwig obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 1633a0ce2f0aSJann Horn 163470524490SJens Axboe if (obuf->len > len) 163570524490SJens Axboe obuf->len = len; 163670524490SJens Axboe ret += obuf->len; 163770524490SJens Axboe len -= obuf->len; 16388cefc107SDavid Howells 16398cefc107SDavid Howells o_head++; 16408cefc107SDavid Howells opipe->head = o_head; 16418cefc107SDavid Howells i_tail++; 1642aadd06e5SJens Axboe } while (len); 164370524490SJens Axboe 164461e0d47cSMiklos Szeredi pipe_unlock(ipipe); 164561e0d47cSMiklos Szeredi pipe_unlock(opipe); 164670524490SJens Axboe 1647aadd06e5SJens Axboe /* 1648aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1649aadd06e5SJens Axboe */ 1650825cdcb1SNamhyung Kim if (ret > 0) 1651825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 165270524490SJens Axboe 165370524490SJens Axboe return ret; 165470524490SJens Axboe } 165570524490SJens Axboe 165670524490SJens Axboe /* 165770524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 165870524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 165970524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 166070524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 166170524490SJens Axboe */ 16629dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) 166370524490SJens Axboe { 1664c73be61cSDavid Howells struct pipe_inode_info *ipipe = get_pipe_info(in, true); 1665c73be61cSDavid Howells struct pipe_inode_info *opipe = get_pipe_info(out, true); 1666aadd06e5SJens Axboe int ret = -EINVAL; 166770524490SJens Axboe 166890da2e3fSPavel Begunkov if (unlikely(!(in->f_mode & FMODE_READ) || 166990da2e3fSPavel Begunkov !(out->f_mode & FMODE_WRITE))) 167090da2e3fSPavel Begunkov return -EBADF; 167190da2e3fSPavel Begunkov 167270524490SJens Axboe /* 1673aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 1674aadd06e5SJens Axboe * copying the data. 167570524490SJens Axboe */ 1676aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 1677ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1678ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1679ee5e0011SSlavomir Kaslev 1680aadd06e5SJens Axboe /* 1681aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 1682aadd06e5SJens Axboe * ordering doesn't really matter. 1683aadd06e5SJens Axboe */ 16847c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 1685aadd06e5SJens Axboe if (!ret) { 16867c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 168702cf01aeSJens Axboe if (!ret) 1688aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 1689aadd06e5SJens Axboe } 1690aadd06e5SJens Axboe } 169170524490SJens Axboe 1692aadd06e5SJens Axboe return ret; 169370524490SJens Axboe } 169470524490SJens Axboe 1695836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 169670524490SJens Axboe { 169790da2e3fSPavel Begunkov struct fd in, out; 16982903ff01SAl Viro int error; 169970524490SJens Axboe 17003d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 17013d6ea290SAl Viro return -EINVAL; 17023d6ea290SAl Viro 170370524490SJens Axboe if (unlikely(!len)) 170470524490SJens Axboe return 0; 170570524490SJens Axboe 170670524490SJens Axboe error = -EBADF; 17072903ff01SAl Viro in = fdget(fdin); 17082903ff01SAl Viro if (in.file) { 170990da2e3fSPavel Begunkov out = fdget(fdout); 17102903ff01SAl Viro if (out.file) { 171190da2e3fSPavel Begunkov error = do_tee(in.file, out.file, len, flags); 17122903ff01SAl Viro fdput(out); 171370524490SJens Axboe } 17142903ff01SAl Viro fdput(in); 171570524490SJens Axboe } 171670524490SJens Axboe 171770524490SJens Axboe return error; 171870524490SJens Axboe } 1719