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 { 495100da38SMatthew Wilcox (Oracle) struct folio *folio = page_folio(buf->page); 509e94cd4fSJens Axboe struct address_space *mapping; 515abc97aaSJens Axboe 52b9ccad2eSMatthew Wilcox (Oracle) folio_lock(folio); 539e0267c2SJens Axboe 54b9ccad2eSMatthew Wilcox (Oracle) mapping = folio_mapping(folio); 559e94cd4fSJens Axboe if (mapping) { 56b9ccad2eSMatthew Wilcox (Oracle) WARN_ON(!folio_test_uptodate(folio)); 575abc97aaSJens Axboe 58ad8d6f0aSJens Axboe /* 599e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 60b9ccad2eSMatthew Wilcox (Oracle) * writeback completing on this folio, since we'll remove it 619e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 62b9ccad2eSMatthew Wilcox (Oracle) * folio, 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 */ 66b9ccad2eSMatthew Wilcox (Oracle) folio_wait_writeback(folio); 67ad8d6f0aSJens Axboe 68b9ccad2eSMatthew Wilcox (Oracle) if (folio_has_private(folio) && 69b9ccad2eSMatthew Wilcox (Oracle) !filemap_release_folio(folio, 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 */ 765100da38SMatthew Wilcox (Oracle) if (remove_mapping(mapping, folio)) { 771432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 78c928f642SChristoph Hellwig return true; 795abc97aaSJens Axboe } 809e94cd4fSJens Axboe } 819e94cd4fSJens Axboe 829e94cd4fSJens Axboe /* 83b9ccad2eSMatthew Wilcox (Oracle) * Raced with truncate or failed to remove folio from current 849e94cd4fSJens Axboe * address space, unlock and return failure. 859e94cd4fSJens Axboe */ 86ca39d651SJens Axboe out_unlock: 87b9ccad2eSMatthew Wilcox (Oracle) folio_unlock(folio); 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 285*33b3b041SDavid Howells /* 286*33b3b041SDavid Howells * Splice data from an O_DIRECT file into pages and then add them to the output 287*33b3b041SDavid Howells * pipe. 288*33b3b041SDavid Howells */ 289*33b3b041SDavid Howells ssize_t direct_splice_read(struct file *in, loff_t *ppos, 290*33b3b041SDavid Howells struct pipe_inode_info *pipe, 291*33b3b041SDavid Howells size_t len, unsigned int flags) 292*33b3b041SDavid Howells { 293*33b3b041SDavid Howells struct iov_iter to; 294*33b3b041SDavid Howells struct bio_vec *bv; 295*33b3b041SDavid Howells struct kiocb kiocb; 296*33b3b041SDavid Howells struct page **pages; 297*33b3b041SDavid Howells ssize_t ret; 298*33b3b041SDavid Howells size_t used, npages, chunk, remain, reclaim; 299*33b3b041SDavid Howells int i; 300*33b3b041SDavid Howells 301*33b3b041SDavid Howells /* Work out how much data we can actually add into the pipe */ 302*33b3b041SDavid Howells used = pipe_occupancy(pipe->head, pipe->tail); 303*33b3b041SDavid Howells npages = max_t(ssize_t, pipe->max_usage - used, 0); 304*33b3b041SDavid Howells len = min_t(size_t, len, npages * PAGE_SIZE); 305*33b3b041SDavid Howells npages = DIV_ROUND_UP(len, PAGE_SIZE); 306*33b3b041SDavid Howells 307*33b3b041SDavid Howells bv = kzalloc(array_size(npages, sizeof(bv[0])) + 308*33b3b041SDavid Howells array_size(npages, sizeof(struct page *)), GFP_KERNEL); 309*33b3b041SDavid Howells if (!bv) 310*33b3b041SDavid Howells return -ENOMEM; 311*33b3b041SDavid Howells 312*33b3b041SDavid Howells pages = (void *)(bv + npages); 313*33b3b041SDavid Howells npages = alloc_pages_bulk_array(GFP_USER, npages, pages); 314*33b3b041SDavid Howells if (!npages) { 315*33b3b041SDavid Howells kfree(bv); 316*33b3b041SDavid Howells return -ENOMEM; 317*33b3b041SDavid Howells } 318*33b3b041SDavid Howells 319*33b3b041SDavid Howells remain = len = min_t(size_t, len, npages * PAGE_SIZE); 320*33b3b041SDavid Howells 321*33b3b041SDavid Howells for (i = 0; i < npages; i++) { 322*33b3b041SDavid Howells chunk = min_t(size_t, PAGE_SIZE, remain); 323*33b3b041SDavid Howells bv[i].bv_page = pages[i]; 324*33b3b041SDavid Howells bv[i].bv_offset = 0; 325*33b3b041SDavid Howells bv[i].bv_len = chunk; 326*33b3b041SDavid Howells remain -= chunk; 327*33b3b041SDavid Howells } 328*33b3b041SDavid Howells 329*33b3b041SDavid Howells /* Do the I/O */ 330*33b3b041SDavid Howells iov_iter_bvec(&to, ITER_DEST, bv, npages, len); 331*33b3b041SDavid Howells init_sync_kiocb(&kiocb, in); 332*33b3b041SDavid Howells kiocb.ki_pos = *ppos; 333*33b3b041SDavid Howells ret = call_read_iter(in, &kiocb, &to); 334*33b3b041SDavid Howells 335*33b3b041SDavid Howells reclaim = npages * PAGE_SIZE; 336*33b3b041SDavid Howells remain = 0; 337*33b3b041SDavid Howells if (ret > 0) { 338*33b3b041SDavid Howells reclaim -= ret; 339*33b3b041SDavid Howells remain = ret; 340*33b3b041SDavid Howells *ppos = kiocb.ki_pos; 341*33b3b041SDavid Howells file_accessed(in); 342*33b3b041SDavid Howells } else if (ret < 0) { 343*33b3b041SDavid Howells /* 344*33b3b041SDavid Howells * callers of ->splice_read() expect -EAGAIN on 345*33b3b041SDavid Howells * "can't put anything in there", rather than -EFAULT. 346*33b3b041SDavid Howells */ 347*33b3b041SDavid Howells if (ret == -EFAULT) 348*33b3b041SDavid Howells ret = -EAGAIN; 349*33b3b041SDavid Howells } 350*33b3b041SDavid Howells 351*33b3b041SDavid Howells /* Free any pages that didn't get touched at all. */ 352*33b3b041SDavid Howells reclaim /= PAGE_SIZE; 353*33b3b041SDavid Howells if (reclaim) { 354*33b3b041SDavid Howells npages -= reclaim; 355*33b3b041SDavid Howells release_pages(pages + npages, reclaim); 356*33b3b041SDavid Howells } 357*33b3b041SDavid Howells 358*33b3b041SDavid Howells /* Push the remaining pages into the pipe. */ 359*33b3b041SDavid Howells for (i = 0; i < npages; i++) { 360*33b3b041SDavid Howells struct pipe_buffer *buf = pipe_head_buf(pipe); 361*33b3b041SDavid Howells 362*33b3b041SDavid Howells chunk = min_t(size_t, remain, PAGE_SIZE); 363*33b3b041SDavid Howells *buf = (struct pipe_buffer) { 364*33b3b041SDavid Howells .ops = &default_pipe_buf_ops, 365*33b3b041SDavid Howells .page = bv[i].bv_page, 366*33b3b041SDavid Howells .offset = 0, 367*33b3b041SDavid Howells .len = chunk, 368*33b3b041SDavid Howells }; 369*33b3b041SDavid Howells pipe->head++; 370*33b3b041SDavid Howells remain -= chunk; 371*33b3b041SDavid Howells } 372*33b3b041SDavid Howells 373*33b3b041SDavid Howells kfree(bv); 374*33b3b041SDavid Howells return ret; 375*33b3b041SDavid Howells } 376*33b3b041SDavid Howells 37783f9135bSJens Axboe /** 37883f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 37983f9135bSJens Axboe * @in: file to splice from 380932cc6d4SJens Axboe * @ppos: position in @in 38183f9135bSJens Axboe * @pipe: pipe to splice to 38283f9135bSJens Axboe * @len: number of bytes to splice 38383f9135bSJens Axboe * @flags: splice modifier flags 38483f9135bSJens Axboe * 385932cc6d4SJens Axboe * Description: 386932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 38782c156f8SAl Viro * used as long as it has more or less sane ->read_iter(). 388932cc6d4SJens Axboe * 38983f9135bSJens Axboe */ 390cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 391cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 392cbb7e577SJens Axboe unsigned int flags) 3935274f052SJens Axboe { 39482c156f8SAl Viro struct iov_iter to; 39582c156f8SAl Viro struct kiocb kiocb; 3968cefc107SDavid Howells int ret; 397be64f884SBoaz Harrosh 398de4eda9dSAl Viro iov_iter_pipe(&to, ITER_DEST, pipe, len); 39982c156f8SAl Viro init_sync_kiocb(&kiocb, in); 40082c156f8SAl Viro kiocb.ki_pos = *ppos; 401bb7462b6SMiklos Szeredi ret = call_read_iter(in, &kiocb, &to); 402723590edSMiklos Szeredi if (ret > 0) { 40382c156f8SAl Viro *ppos = kiocb.ki_pos; 404723590edSMiklos Szeredi file_accessed(in); 40582c156f8SAl Viro } else if (ret < 0) { 4060d964934SAl Viro /* free what was emitted */ 4070d964934SAl Viro pipe_discard_from(pipe, to.start_head); 40882c156f8SAl Viro /* 40982c156f8SAl Viro * callers of ->splice_read() expect -EAGAIN on 41082c156f8SAl Viro * "can't put anything in there", rather than -EFAULT. 41182c156f8SAl Viro */ 41282c156f8SAl Viro if (ret == -EFAULT) 41382c156f8SAl Viro ret = -EAGAIN; 414723590edSMiklos Szeredi } 4155274f052SJens Axboe 4165274f052SJens Axboe return ret; 4175274f052SJens Axboe } 418059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 419059a8f37SJens Axboe 420241699cdSAl Viro const struct pipe_buf_operations default_pipe_buf_ops = { 4216818173bSMiklos Szeredi .release = generic_pipe_buf_release, 422c928f642SChristoph Hellwig .try_steal = generic_pipe_buf_try_steal, 4236818173bSMiklos Szeredi .get = generic_pipe_buf_get, 4246818173bSMiklos Szeredi }; 4256818173bSMiklos Szeredi 42628a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 42728a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 42828a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 42928a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 43028a625cbSMiklos Szeredi }; 43128a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 43228a625cbSMiklos Szeredi 4335274f052SJens Axboe /* 4344f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 435016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 4365274f052SJens Axboe */ 43776ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 4385274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 4395274f052SJens Axboe { 4406a14b90bSJens Axboe struct file *file = sd->u.file; 4415274f052SJens Axboe loff_t pos = sd->pos; 442a8adbe37SMichał Mirosław int more; 4435274f052SJens Axboe 44472c2d531SAl Viro if (!likely(file->f_op->sendpage)) 445a8adbe37SMichał Mirosław return -EINVAL; 446a8adbe37SMichał Mirosław 44735f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 448ae62ca7bSEric Dumazet 4498cefc107SDavid Howells if (sd->len < sd->total_len && 4508cefc107SDavid Howells pipe_occupancy(pipe->head, pipe->tail) > 1) 45135f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 452ae62ca7bSEric Dumazet 453a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 454f84d7519SJens Axboe sd->len, &pos, more); 4555274f052SJens Axboe } 4565274f052SJens Axboe 457b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 458b3c2d2ddSMiklos Szeredi { 459b3c2d2ddSMiklos Szeredi smp_mb(); 4600ddad21dSLinus Torvalds if (waitqueue_active(&pipe->wr_wait)) 4610ddad21dSLinus Torvalds wake_up_interruptible(&pipe->wr_wait); 462b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 463b3c2d2ddSMiklos Szeredi } 464b3c2d2ddSMiklos Szeredi 465b3c2d2ddSMiklos Szeredi /** 466b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 467b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 468b3c2d2ddSMiklos Szeredi * @sd: information to @actor 469b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 470b3c2d2ddSMiklos Szeredi * 471b3c2d2ddSMiklos Szeredi * Description: 472b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 473b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 474b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 475b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 476b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 477b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 478b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 479b3c2d2ddSMiklos Szeredi * 480b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 481b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 482b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 483b3c2d2ddSMiklos Szeredi * destination. 484b3c2d2ddSMiklos Szeredi */ 48596f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 486b3c2d2ddSMiklos Szeredi splice_actor *actor) 487b3c2d2ddSMiklos Szeredi { 4888cefc107SDavid Howells unsigned int head = pipe->head; 4898cefc107SDavid Howells unsigned int tail = pipe->tail; 4908cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1; 491b3c2d2ddSMiklos Szeredi int ret; 492b3c2d2ddSMiklos Szeredi 493ec057595SLinus Torvalds while (!pipe_empty(head, tail)) { 4948cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 495b3c2d2ddSMiklos Szeredi 496b3c2d2ddSMiklos Szeredi sd->len = buf->len; 497b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 498b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 499b3c2d2ddSMiklos Szeredi 500fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 501a8adbe37SMichał Mirosław if (unlikely(ret)) { 502b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 503b3c2d2ddSMiklos Szeredi ret = 0; 504b3c2d2ddSMiklos Szeredi return ret; 505b3c2d2ddSMiklos Szeredi } 506a8adbe37SMichał Mirosław 507a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 508a8adbe37SMichał Mirosław if (ret <= 0) 509a8adbe37SMichał Mirosław return ret; 510a8adbe37SMichał Mirosław 511b3c2d2ddSMiklos Szeredi buf->offset += ret; 512b3c2d2ddSMiklos Szeredi buf->len -= ret; 513b3c2d2ddSMiklos Szeredi 514b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 515b3c2d2ddSMiklos Szeredi sd->len -= ret; 516b3c2d2ddSMiklos Szeredi sd->pos += ret; 517b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 518b3c2d2ddSMiklos Szeredi 519b3c2d2ddSMiklos Szeredi if (!buf->len) { 520a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 5218cefc107SDavid Howells tail++; 5228cefc107SDavid Howells pipe->tail = tail; 5236447a3cfSAl Viro if (pipe->files) 524b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 525b3c2d2ddSMiklos Szeredi } 526b3c2d2ddSMiklos Szeredi 527b3c2d2ddSMiklos Szeredi if (!sd->total_len) 528b3c2d2ddSMiklos Szeredi return 0; 529b3c2d2ddSMiklos Szeredi } 530b3c2d2ddSMiklos Szeredi 531b3c2d2ddSMiklos Szeredi return 1; 532b3c2d2ddSMiklos Szeredi } 533b3c2d2ddSMiklos Szeredi 534d1a819a2SLinus Torvalds /* We know we have a pipe buffer, but maybe it's empty? */ 535d1a819a2SLinus Torvalds static inline bool eat_empty_buffer(struct pipe_inode_info *pipe) 536d1a819a2SLinus Torvalds { 537d1a819a2SLinus Torvalds unsigned int tail = pipe->tail; 538d1a819a2SLinus Torvalds unsigned int mask = pipe->ring_size - 1; 539d1a819a2SLinus Torvalds struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 540d1a819a2SLinus Torvalds 541d1a819a2SLinus Torvalds if (unlikely(!buf->len)) { 542d1a819a2SLinus Torvalds pipe_buf_release(pipe, buf); 543d1a819a2SLinus Torvalds pipe->tail = tail+1; 544d1a819a2SLinus Torvalds return true; 545d1a819a2SLinus Torvalds } 546d1a819a2SLinus Torvalds 547d1a819a2SLinus Torvalds return false; 548d1a819a2SLinus Torvalds } 549d1a819a2SLinus Torvalds 550b3c2d2ddSMiklos Szeredi /** 551b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 552b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 553b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 554b3c2d2ddSMiklos Szeredi * 555b3c2d2ddSMiklos Szeredi * Description: 556b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 557b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 558b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 559b3c2d2ddSMiklos Szeredi */ 56096f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 561b3c2d2ddSMiklos Szeredi { 562c725bfceSJan Kara /* 563c725bfceSJan Kara * Check for signal early to make process killable when there are 564c725bfceSJan Kara * always buffers available 565c725bfceSJan Kara */ 566c725bfceSJan Kara if (signal_pending(current)) 567c725bfceSJan Kara return -ERESTARTSYS; 568c725bfceSJan Kara 569d1a819a2SLinus Torvalds repeat: 5708cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 571b3c2d2ddSMiklos Szeredi if (!pipe->writers) 572b3c2d2ddSMiklos Szeredi return 0; 573b3c2d2ddSMiklos Szeredi 574a28c8b9dSLinus Torvalds if (sd->num_spliced) 575b3c2d2ddSMiklos Szeredi return 0; 576b3c2d2ddSMiklos Szeredi 577b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 578b3c2d2ddSMiklos Szeredi return -EAGAIN; 579b3c2d2ddSMiklos Szeredi 580b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 581b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 582b3c2d2ddSMiklos Szeredi 583b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 584b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 585b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 586b3c2d2ddSMiklos Szeredi } 587b3c2d2ddSMiklos Szeredi 588472e5b05SLinus Torvalds pipe_wait_readable(pipe); 589b3c2d2ddSMiklos Szeredi } 590b3c2d2ddSMiklos Szeredi 591d1a819a2SLinus Torvalds if (eat_empty_buffer(pipe)) 592d1a819a2SLinus Torvalds goto repeat; 593d1a819a2SLinus Torvalds 594b3c2d2ddSMiklos Szeredi return 1; 595b3c2d2ddSMiklos Szeredi } 596b3c2d2ddSMiklos Szeredi 597b3c2d2ddSMiklos Szeredi /** 598b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 599b80901bbSRandy Dunlap * @sd: information about the splice operation 600b3c2d2ddSMiklos Szeredi * 601b3c2d2ddSMiklos Szeredi * Description: 602b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 603b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 604b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 605b3c2d2ddSMiklos Szeredi */ 60696f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd) 607b3c2d2ddSMiklos Szeredi { 608b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 609b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 610b3c2d2ddSMiklos Szeredi } 611b3c2d2ddSMiklos Szeredi 612b3c2d2ddSMiklos Szeredi /** 613b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 614b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 615b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 616b3c2d2ddSMiklos Szeredi * 617b3c2d2ddSMiklos Szeredi * Description: 618b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 619b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 620b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 621b3c2d2ddSMiklos Szeredi */ 62296f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 623b3c2d2ddSMiklos Szeredi { 624b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 625b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 626b3c2d2ddSMiklos Szeredi } 627b3c2d2ddSMiklos Szeredi 628932cc6d4SJens Axboe /** 629932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 630932cc6d4SJens Axboe * @pipe: pipe to splice from 631932cc6d4SJens Axboe * @sd: information to @actor 632932cc6d4SJens Axboe * @actor: handler that splices the data 633932cc6d4SJens Axboe * 634932cc6d4SJens Axboe * Description: 635932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 636932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 637932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 638932cc6d4SJens Axboe * pipe_to_user. 639932cc6d4SJens Axboe * 64083f9135bSJens Axboe */ 641c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 642c66ab6faSJens Axboe splice_actor *actor) 6435274f052SJens Axboe { 644b3c2d2ddSMiklos Szeredi int ret; 6455274f052SJens Axboe 646b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 647b3c2d2ddSMiklos Szeredi do { 648c2489e07SJan Kara cond_resched(); 649b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 650b3c2d2ddSMiklos Szeredi if (ret > 0) 651b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 652b3c2d2ddSMiklos Szeredi } while (ret > 0); 653b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 6545274f052SJens Axboe 655b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 6565274f052SJens Axboe } 65740bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 6585274f052SJens Axboe 659932cc6d4SJens Axboe /** 660932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 661932cc6d4SJens Axboe * @pipe: pipe to splice from 662932cc6d4SJens Axboe * @out: file to splice to 663932cc6d4SJens Axboe * @ppos: position in @out 664932cc6d4SJens Axboe * @len: how many bytes to splice 665932cc6d4SJens Axboe * @flags: splice modifier flags 666932cc6d4SJens Axboe * @actor: handler that splices the data 667932cc6d4SJens Axboe * 668932cc6d4SJens Axboe * Description: 6692933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 670932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 671932cc6d4SJens Axboe * 672932cc6d4SJens Axboe */ 6736da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 6746da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 6756da61809SMark Fasheh splice_actor *actor) 6766da61809SMark Fasheh { 6776da61809SMark Fasheh ssize_t ret; 678c66ab6faSJens Axboe struct splice_desc sd = { 679c66ab6faSJens Axboe .total_len = len, 680c66ab6faSJens Axboe .flags = flags, 681c66ab6faSJens Axboe .pos = *ppos, 6826a14b90bSJens Axboe .u.file = out, 683c66ab6faSJens Axboe }; 6846da61809SMark Fasheh 68561e0d47cSMiklos Szeredi pipe_lock(pipe); 686c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 68761e0d47cSMiklos Szeredi pipe_unlock(pipe); 6886da61809SMark Fasheh 6896da61809SMark Fasheh return ret; 6906da61809SMark Fasheh } 6916da61809SMark Fasheh 6926da61809SMark Fasheh /** 6938d020765SAl Viro * iter_file_splice_write - splice data from a pipe to a file 6948d020765SAl Viro * @pipe: pipe info 6958d020765SAl Viro * @out: file to write to 6968d020765SAl Viro * @ppos: position in @out 6978d020765SAl Viro * @len: number of bytes to splice 6988d020765SAl Viro * @flags: splice modifier flags 6998d020765SAl Viro * 7008d020765SAl Viro * Description: 7018d020765SAl Viro * Will either move or copy pages (determined by @flags options) from 7028d020765SAl Viro * the given pipe inode to the given file. 7038d020765SAl Viro * This one is ->write_iter-based. 7048d020765SAl Viro * 7058d020765SAl Viro */ 7068d020765SAl Viro ssize_t 7078d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 7088d020765SAl Viro loff_t *ppos, size_t len, unsigned int flags) 7098d020765SAl Viro { 7108d020765SAl Viro struct splice_desc sd = { 7118d020765SAl Viro .total_len = len, 7128d020765SAl Viro .flags = flags, 7138d020765SAl Viro .pos = *ppos, 7148d020765SAl Viro .u.file = out, 7158d020765SAl Viro }; 7166718b6f8SDavid Howells int nbufs = pipe->max_usage; 7178d020765SAl Viro struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 7188d020765SAl Viro GFP_KERNEL); 7198d020765SAl Viro ssize_t ret; 7208d020765SAl Viro 7218d020765SAl Viro if (unlikely(!array)) 7228d020765SAl Viro return -ENOMEM; 7238d020765SAl Viro 7248d020765SAl Viro pipe_lock(pipe); 7258d020765SAl Viro 7268d020765SAl Viro splice_from_pipe_begin(&sd); 7278d020765SAl Viro while (sd.total_len) { 7288d020765SAl Viro struct iov_iter from; 729ec057595SLinus Torvalds unsigned int head, tail, mask; 7308d020765SAl Viro size_t left; 7318cefc107SDavid Howells int n; 7328d020765SAl Viro 7338d020765SAl Viro ret = splice_from_pipe_next(pipe, &sd); 7348d020765SAl Viro if (ret <= 0) 7358d020765SAl Viro break; 7368d020765SAl Viro 7376718b6f8SDavid Howells if (unlikely(nbufs < pipe->max_usage)) { 7388d020765SAl Viro kfree(array); 7396718b6f8SDavid Howells nbufs = pipe->max_usage; 7408d020765SAl Viro array = kcalloc(nbufs, sizeof(struct bio_vec), 7418d020765SAl Viro GFP_KERNEL); 7428d020765SAl Viro if (!array) { 7438d020765SAl Viro ret = -ENOMEM; 7448d020765SAl Viro break; 7458d020765SAl Viro } 7468d020765SAl Viro } 7478d020765SAl Viro 748ec057595SLinus Torvalds head = pipe->head; 749ec057595SLinus Torvalds tail = pipe->tail; 750ec057595SLinus Torvalds mask = pipe->ring_size - 1; 751ec057595SLinus Torvalds 7528d020765SAl Viro /* build the vector */ 7538d020765SAl Viro left = sd.total_len; 7540f1d344fSPavel Begunkov for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) { 7558cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 7568d020765SAl Viro size_t this_len = buf->len; 7578d020765SAl Viro 7580f1d344fSPavel Begunkov /* zero-length bvecs are not supported, skip them */ 7590f1d344fSPavel Begunkov if (!this_len) 7600f1d344fSPavel Begunkov continue; 7610f1d344fSPavel Begunkov this_len = min(this_len, left); 7628d020765SAl Viro 763fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf); 7648d020765SAl Viro if (unlikely(ret)) { 7658d020765SAl Viro if (ret == -ENODATA) 7668d020765SAl Viro ret = 0; 7678d020765SAl Viro goto done; 7688d020765SAl Viro } 7698d020765SAl Viro 7708d020765SAl Viro array[n].bv_page = buf->page; 7718d020765SAl Viro array[n].bv_len = this_len; 7728d020765SAl Viro array[n].bv_offset = buf->offset; 7738d020765SAl Viro left -= this_len; 7740f1d344fSPavel Begunkov n++; 7758d020765SAl Viro } 7768d020765SAl Viro 777de4eda9dSAl Viro iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left); 778abbb6589SChristoph Hellwig ret = vfs_iter_write(out, &from, &sd.pos, 0); 7798d020765SAl Viro if (ret <= 0) 7808d020765SAl Viro break; 7818d020765SAl Viro 7828d020765SAl Viro sd.num_spliced += ret; 7838d020765SAl Viro sd.total_len -= ret; 784dbe4e192SChristoph Hellwig *ppos = sd.pos; 7858d020765SAl Viro 7868d020765SAl Viro /* dismiss the fully eaten buffers, adjust the partial one */ 7878cefc107SDavid Howells tail = pipe->tail; 7888d020765SAl Viro while (ret) { 7898cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 7908d020765SAl Viro if (ret >= buf->len) { 7918d020765SAl Viro ret -= buf->len; 7928d020765SAl Viro buf->len = 0; 793a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 7948cefc107SDavid Howells tail++; 7958cefc107SDavid Howells pipe->tail = tail; 7968d020765SAl Viro if (pipe->files) 7978d020765SAl Viro sd.need_wakeup = true; 7988d020765SAl Viro } else { 7998d020765SAl Viro buf->offset += ret; 8008d020765SAl Viro buf->len -= ret; 8018d020765SAl Viro ret = 0; 8028d020765SAl Viro } 8038d020765SAl Viro } 8048d020765SAl Viro } 8058d020765SAl Viro done: 8068d020765SAl Viro kfree(array); 8078d020765SAl Viro splice_from_pipe_end(pipe, &sd); 8088d020765SAl Viro 8098d020765SAl Viro pipe_unlock(pipe); 8108d020765SAl Viro 8118d020765SAl Viro if (sd.num_spliced) 8128d020765SAl Viro ret = sd.num_spliced; 8138d020765SAl Viro 8148d020765SAl Viro return ret; 8158d020765SAl Viro } 8168d020765SAl Viro 8178d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write); 8188d020765SAl Viro 81983f9135bSJens Axboe /** 82083f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 821932cc6d4SJens Axboe * @pipe: pipe to splice from 82283f9135bSJens Axboe * @out: socket to write to 823932cc6d4SJens Axboe * @ppos: position in @out 82483f9135bSJens Axboe * @len: number of bytes to splice 82583f9135bSJens Axboe * @flags: splice modifier flags 82683f9135bSJens Axboe * 827932cc6d4SJens Axboe * Description: 82883f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 82983f9135bSJens Axboe * is involved. 83083f9135bSJens Axboe * 83183f9135bSJens Axboe */ 8323a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 833cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 8345274f052SJens Axboe { 83500522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 8365274f052SJens Axboe } 8375274f052SJens Axboe 838059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 839a0f06780SJeff Garzik 84036e2c742SChristoph Hellwig static int warn_unsupported(struct file *file, const char *op) 84136e2c742SChristoph Hellwig { 84236e2c742SChristoph Hellwig pr_debug_ratelimited( 84336e2c742SChristoph Hellwig "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n", 84436e2c742SChristoph Hellwig op, file, current->pid, current->comm); 84536e2c742SChristoph Hellwig return -EINVAL; 84636e2c742SChristoph Hellwig } 84736e2c742SChristoph Hellwig 84883f9135bSJens Axboe /* 84983f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 85083f9135bSJens Axboe */ 8513a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 852cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 8535274f052SJens Axboe { 85436e2c742SChristoph Hellwig if (unlikely(!out->f_op->splice_write)) 85536e2c742SChristoph Hellwig return warn_unsupported(out, "write"); 85600c285d0SChristoph Hellwig return out->f_op->splice_write(pipe, out, ppos, len, flags); 8575274f052SJens Axboe } 8585274f052SJens Axboe 85983f9135bSJens Axboe /* 86083f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 86183f9135bSJens Axboe */ 862cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 863cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 864cbb7e577SJens Axboe unsigned int flags) 8655274f052SJens Axboe { 866313d64a3SAl Viro unsigned int p_space; 8675274f052SJens Axboe int ret; 8685274f052SJens Axboe 86949570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 8705274f052SJens Axboe return -EBADF; 8715274f052SJens Axboe 872313d64a3SAl Viro /* Don't try to read more the pipe has space for. */ 873313d64a3SAl Viro p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail); 874313d64a3SAl Viro len = min_t(size_t, len, p_space << PAGE_SHIFT); 875313d64a3SAl Viro 876cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 8775274f052SJens Axboe if (unlikely(ret < 0)) 8785274f052SJens Axboe return ret; 8795274f052SJens Axboe 88003cc0789SAl Viro if (unlikely(len > MAX_RW_COUNT)) 88103cc0789SAl Viro len = MAX_RW_COUNT; 88203cc0789SAl Viro 88336e2c742SChristoph Hellwig if (unlikely(!in->f_op->splice_read)) 88436e2c742SChristoph Hellwig return warn_unsupported(in, "read"); 8852bc01060SChristoph Hellwig return in->f_op->splice_read(in, ppos, pipe, len, flags); 8865274f052SJens Axboe } 8875274f052SJens Axboe 888932cc6d4SJens Axboe /** 889932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 890932cc6d4SJens Axboe * @in: file to splice from 891932cc6d4SJens Axboe * @sd: actor information on where to splice to 892932cc6d4SJens Axboe * @actor: handles the data splicing 893932cc6d4SJens Axboe * 894932cc6d4SJens Axboe * Description: 895932cc6d4SJens Axboe * This is a special case helper to splice directly between two 896932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 897932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 898932cc6d4SJens Axboe * that process. 899932cc6d4SJens Axboe * 900c66ab6faSJens Axboe */ 901c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 902c66ab6faSJens Axboe splice_direct_actor *actor) 903b92ce558SJens Axboe { 904b92ce558SJens Axboe struct pipe_inode_info *pipe; 905b92ce558SJens Axboe long ret, bytes; 906c66ab6faSJens Axboe size_t len; 9070ff28d9fSChristophe Leroy int i, flags, more; 908b92ce558SJens Axboe 909b92ce558SJens Axboe /* 91097ef77c5SJason A. Donenfeld * We require the input to be seekable, as we don't want to randomly 91197ef77c5SJason A. Donenfeld * drop data for eg socket -> socket splicing. Use the piped splicing 91297ef77c5SJason A. Donenfeld * for that! 913b92ce558SJens Axboe */ 91497ef77c5SJason A. Donenfeld if (unlikely(!(in->f_mode & FMODE_LSEEK))) 915b92ce558SJens Axboe return -EINVAL; 916b92ce558SJens Axboe 917b92ce558SJens Axboe /* 918b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 919b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 920b92ce558SJens Axboe */ 921b92ce558SJens Axboe pipe = current->splice_pipe; 92249570e9bSJens Axboe if (unlikely(!pipe)) { 9237bee130eSAl Viro pipe = alloc_pipe_info(); 924b92ce558SJens Axboe if (!pipe) 925b92ce558SJens Axboe return -ENOMEM; 926b92ce558SJens Axboe 927b92ce558SJens Axboe /* 928b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 92900522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 930b92ce558SJens Axboe * PIPE_READERS appropriately. 931b92ce558SJens Axboe */ 932b92ce558SJens Axboe pipe->readers = 1; 933b92ce558SJens Axboe 934b92ce558SJens Axboe current->splice_pipe = pipe; 935b92ce558SJens Axboe } 936b92ce558SJens Axboe 937b92ce558SJens Axboe /* 93873d62d83SIngo Molnar * Do the splice. 939b92ce558SJens Axboe */ 940b92ce558SJens Axboe ret = 0; 941b92ce558SJens Axboe bytes = 0; 942c66ab6faSJens Axboe len = sd->total_len; 943c66ab6faSJens Axboe flags = sd->flags; 944c66ab6faSJens Axboe 945c66ab6faSJens Axboe /* 946c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 947c66ab6faSJens Axboe */ 948c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 9490ff28d9fSChristophe Leroy more = sd->flags & SPLICE_F_MORE; 950b92ce558SJens Axboe 9518cefc107SDavid Howells WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); 95217614445SDarrick J. Wong 953b92ce558SJens Axboe while (len) { 95451a92c0fSJens Axboe size_t read_len; 955a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 956b92ce558SJens Axboe 957313d64a3SAl Viro ret = do_splice_to(in, &pos, pipe, len, flags); 95851a92c0fSJens Axboe if (unlikely(ret <= 0)) 959b92ce558SJens Axboe goto out_release; 960b92ce558SJens Axboe 961b92ce558SJens Axboe read_len = ret; 962c66ab6faSJens Axboe sd->total_len = read_len; 963b92ce558SJens Axboe 964b92ce558SJens Axboe /* 9650ff28d9fSChristophe Leroy * If more data is pending, set SPLICE_F_MORE 9660ff28d9fSChristophe Leroy * If this is the last data and SPLICE_F_MORE was not set 9670ff28d9fSChristophe Leroy * initially, clears it. 9680ff28d9fSChristophe Leroy */ 9690ff28d9fSChristophe Leroy if (read_len < len) 9700ff28d9fSChristophe Leroy sd->flags |= SPLICE_F_MORE; 9710ff28d9fSChristophe Leroy else if (!more) 9720ff28d9fSChristophe Leroy sd->flags &= ~SPLICE_F_MORE; 9730ff28d9fSChristophe Leroy /* 974b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 975b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 976b92ce558SJens Axboe * could get stuck data in the internal pipe: 977b92ce558SJens Axboe */ 978c66ab6faSJens Axboe ret = actor(pipe, sd); 979a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 980a82c53a0STom Zanussi sd->pos = prev_pos; 981b92ce558SJens Axboe goto out_release; 982a82c53a0STom Zanussi } 983b92ce558SJens Axboe 984b92ce558SJens Axboe bytes += ret; 985b92ce558SJens Axboe len -= ret; 986bcd4f3acSJens Axboe sd->pos = pos; 987b92ce558SJens Axboe 988a82c53a0STom Zanussi if (ret < read_len) { 989a82c53a0STom Zanussi sd->pos = prev_pos + ret; 99051a92c0fSJens Axboe goto out_release; 991b92ce558SJens Axboe } 992a82c53a0STom Zanussi } 993b92ce558SJens Axboe 9949e97198dSJens Axboe done: 9958cefc107SDavid Howells pipe->tail = pipe->head = 0; 9969e97198dSJens Axboe file_accessed(in); 997b92ce558SJens Axboe return bytes; 998b92ce558SJens Axboe 999b92ce558SJens Axboe out_release: 1000b92ce558SJens Axboe /* 1001b92ce558SJens Axboe * If we did an incomplete transfer we must release 1002b92ce558SJens Axboe * the pipe buffers in question: 1003b92ce558SJens Axboe */ 10048cefc107SDavid Howells for (i = 0; i < pipe->ring_size; i++) { 10058cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[i]; 1006b92ce558SJens Axboe 1007a779638cSMiklos Szeredi if (buf->ops) 1008a779638cSMiklos Szeredi pipe_buf_release(pipe, buf); 1009b92ce558SJens Axboe } 1010b92ce558SJens Axboe 10119e97198dSJens Axboe if (!bytes) 10129e97198dSJens Axboe bytes = ret; 1013b92ce558SJens Axboe 10149e97198dSJens Axboe goto done; 1015c66ab6faSJens Axboe } 1016c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1017c66ab6faSJens Axboe 1018c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1019c66ab6faSJens Axboe struct splice_desc *sd) 1020c66ab6faSJens Axboe { 10216a14b90bSJens Axboe struct file *file = sd->u.file; 1022c66ab6faSJens Axboe 10237995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 10242cb4b05eSChangli Gao sd->flags); 1025c66ab6faSJens Axboe } 1026c66ab6faSJens Axboe 1027932cc6d4SJens Axboe /** 1028932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1029932cc6d4SJens Axboe * @in: file to splice from 1030932cc6d4SJens Axboe * @ppos: input file offset 1031932cc6d4SJens Axboe * @out: file to splice to 1032acdb37c3SRandy Dunlap * @opos: output file offset 1033932cc6d4SJens Axboe * @len: number of bytes to splice 1034932cc6d4SJens Axboe * @flags: splice modifier flags 1035932cc6d4SJens Axboe * 1036932cc6d4SJens Axboe * Description: 1037932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1038932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1039932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1040932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1041932cc6d4SJens Axboe * 1042932cc6d4SJens Axboe */ 1043c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 10447995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 1045c66ab6faSJens Axboe { 1046c66ab6faSJens Axboe struct splice_desc sd = { 1047c66ab6faSJens Axboe .len = len, 1048c66ab6faSJens Axboe .total_len = len, 1049c66ab6faSJens Axboe .flags = flags, 1050c66ab6faSJens Axboe .pos = *ppos, 10516a14b90bSJens Axboe .u.file = out, 10527995bd28SAl Viro .opos = opos, 1053c66ab6faSJens Axboe }; 105451a92c0fSJens Axboe long ret; 1055c66ab6faSJens Axboe 105618c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 105718c67cb9SAl Viro return -EBADF; 105818c67cb9SAl Viro 105918c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 106018c67cb9SAl Viro return -EINVAL; 106118c67cb9SAl Viro 106218c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 106318c67cb9SAl Viro if (unlikely(ret < 0)) 106418c67cb9SAl Viro return ret; 106518c67cb9SAl Viro 1066c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 106751a92c0fSJens Axboe if (ret > 0) 1068a82c53a0STom Zanussi *ppos = sd.pos; 106951a92c0fSJens Axboe 1070c66ab6faSJens Axboe return ret; 1071b92ce558SJens Axboe } 10721c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct); 1073b92ce558SJens Axboe 10748924feffSAl Viro static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) 10758924feffSAl Viro { 107652bce911SLinus Torvalds for (;;) { 107752bce911SLinus Torvalds if (unlikely(!pipe->readers)) { 107852bce911SLinus Torvalds send_sig(SIGPIPE, current, 0); 107952bce911SLinus Torvalds return -EPIPE; 108052bce911SLinus Torvalds } 10816718b6f8SDavid Howells if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 108252bce911SLinus Torvalds return 0; 10838924feffSAl Viro if (flags & SPLICE_F_NONBLOCK) 10848924feffSAl Viro return -EAGAIN; 10858924feffSAl Viro if (signal_pending(current)) 10868924feffSAl Viro return -ERESTARTSYS; 1087472e5b05SLinus Torvalds pipe_wait_writable(pipe); 10888924feffSAl Viro } 10898924feffSAl Viro } 10908924feffSAl Viro 10917c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 10927c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 10937c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1094ddac0d39SJens Axboe 1095b964bf53SAl Viro long splice_file_to_pipe(struct file *in, 1096faa97c48SAl Viro struct pipe_inode_info *opipe, 1097faa97c48SAl Viro loff_t *offset, 1098faa97c48SAl Viro size_t len, unsigned int flags) 1099faa97c48SAl Viro { 1100faa97c48SAl Viro long ret; 1101faa97c48SAl Viro 1102faa97c48SAl Viro pipe_lock(opipe); 1103faa97c48SAl Viro ret = wait_for_space(opipe, flags); 1104faa97c48SAl Viro if (!ret) 1105faa97c48SAl Viro ret = do_splice_to(in, offset, opipe, len, flags); 1106faa97c48SAl Viro pipe_unlock(opipe); 1107faa97c48SAl Viro if (ret > 0) 1108faa97c48SAl Viro wakeup_pipe_readers(opipe); 1109faa97c48SAl Viro return ret; 1110faa97c48SAl Viro } 1111faa97c48SAl Viro 1112ddac0d39SJens Axboe /* 111383f9135bSJens Axboe * Determine where to splice to/from. 111483f9135bSJens Axboe */ 1115ee6e00c8SJens Axboe long do_splice(struct file *in, loff_t *off_in, struct file *out, 1116ee6e00c8SJens Axboe loff_t *off_out, size_t len, unsigned int flags) 11175274f052SJens Axboe { 11187c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 11197c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 11207995bd28SAl Viro loff_t offset; 1121a4514ebdSJens Axboe long ret; 11225274f052SJens Axboe 112390da2e3fSPavel Begunkov if (unlikely(!(in->f_mode & FMODE_READ) || 112490da2e3fSPavel Begunkov !(out->f_mode & FMODE_WRITE))) 112590da2e3fSPavel Begunkov return -EBADF; 112690da2e3fSPavel Begunkov 1127c73be61cSDavid Howells ipipe = get_pipe_info(in, true); 1128c73be61cSDavid Howells opipe = get_pipe_info(out, true); 11297c77f0b3SMiklos Szeredi 11307c77f0b3SMiklos Szeredi if (ipipe && opipe) { 11317c77f0b3SMiklos Szeredi if (off_in || off_out) 11327c77f0b3SMiklos Szeredi return -ESPIPE; 11337c77f0b3SMiklos Szeredi 11347c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 11357c77f0b3SMiklos Szeredi if (ipipe == opipe) 11367c77f0b3SMiklos Szeredi return -EINVAL; 11377c77f0b3SMiklos Szeredi 1138ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1139ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1140ee5e0011SSlavomir Kaslev 11417c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 11427c77f0b3SMiklos Szeredi } 11437c77f0b3SMiklos Szeredi 11447c77f0b3SMiklos Szeredi if (ipipe) { 1145529565dcSIngo Molnar if (off_in) 1146529565dcSIngo Molnar return -ESPIPE; 1147b92ce558SJens Axboe if (off_out) { 114819c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1149b92ce558SJens Axboe return -EINVAL; 1150ee6e00c8SJens Axboe offset = *off_out; 11517995bd28SAl Viro } else { 11527995bd28SAl Viro offset = out->f_pos; 11537995bd28SAl Viro } 1154529565dcSIngo Molnar 115518c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 115618c67cb9SAl Viro return -EINVAL; 115718c67cb9SAl Viro 115818c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 115918c67cb9SAl Viro if (unlikely(ret < 0)) 116018c67cb9SAl Viro return ret; 116118c67cb9SAl Viro 1162ee5e0011SSlavomir Kaslev if (in->f_flags & O_NONBLOCK) 1163ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1164ee5e0011SSlavomir Kaslev 1165500368f7SAl Viro file_start_write(out); 11667995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1167500368f7SAl Viro file_end_write(out); 1168a4514ebdSJens Axboe 11697995bd28SAl Viro if (!off_out) 11707995bd28SAl Viro out->f_pos = offset; 1171ee6e00c8SJens Axboe else 1172ee6e00c8SJens Axboe *off_out = offset; 1173a4514ebdSJens Axboe 1174a4514ebdSJens Axboe return ret; 1175529565dcSIngo Molnar } 11765274f052SJens Axboe 11777c77f0b3SMiklos Szeredi if (opipe) { 1178529565dcSIngo Molnar if (off_out) 1179529565dcSIngo Molnar return -ESPIPE; 1180b92ce558SJens Axboe if (off_in) { 118119c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1182b92ce558SJens Axboe return -EINVAL; 1183ee6e00c8SJens Axboe offset = *off_in; 11847995bd28SAl Viro } else { 11857995bd28SAl Viro offset = in->f_pos; 11867995bd28SAl Viro } 1187529565dcSIngo Molnar 1188ee5e0011SSlavomir Kaslev if (out->f_flags & O_NONBLOCK) 1189ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1190ee5e0011SSlavomir Kaslev 1191faa97c48SAl Viro ret = splice_file_to_pipe(in, opipe, &offset, len, flags); 11927995bd28SAl Viro if (!off_in) 11937995bd28SAl Viro in->f_pos = offset; 1194ee6e00c8SJens Axboe else 1195ee6e00c8SJens Axboe *off_in = offset; 1196a4514ebdSJens Axboe 1197a4514ebdSJens Axboe return ret; 1198529565dcSIngo Molnar } 11995274f052SJens Axboe 12005274f052SJens Axboe return -EINVAL; 12015274f052SJens Axboe } 12025274f052SJens Axboe 1203ee6e00c8SJens Axboe static long __do_splice(struct file *in, loff_t __user *off_in, 1204ee6e00c8SJens Axboe struct file *out, loff_t __user *off_out, 1205ee6e00c8SJens Axboe size_t len, unsigned int flags) 1206ee6e00c8SJens Axboe { 1207ee6e00c8SJens Axboe struct pipe_inode_info *ipipe; 1208ee6e00c8SJens Axboe struct pipe_inode_info *opipe; 1209ee6e00c8SJens Axboe loff_t offset, *__off_in = NULL, *__off_out = NULL; 1210ee6e00c8SJens Axboe long ret; 1211ee6e00c8SJens Axboe 1212ee6e00c8SJens Axboe ipipe = get_pipe_info(in, true); 1213ee6e00c8SJens Axboe opipe = get_pipe_info(out, true); 1214ee6e00c8SJens Axboe 1215ee6e00c8SJens Axboe if (ipipe && off_in) 1216ee6e00c8SJens Axboe return -ESPIPE; 1217ee6e00c8SJens Axboe if (opipe && off_out) 1218ee6e00c8SJens Axboe return -ESPIPE; 1219ee6e00c8SJens Axboe 1220ee6e00c8SJens Axboe if (off_out) { 1221ee6e00c8SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1222ee6e00c8SJens Axboe return -EFAULT; 1223ee6e00c8SJens Axboe __off_out = &offset; 1224ee6e00c8SJens Axboe } 1225ee6e00c8SJens Axboe if (off_in) { 1226ee6e00c8SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1227ee6e00c8SJens Axboe return -EFAULT; 1228ee6e00c8SJens Axboe __off_in = &offset; 1229ee6e00c8SJens Axboe } 1230ee6e00c8SJens Axboe 1231ee6e00c8SJens Axboe ret = do_splice(in, __off_in, out, __off_out, len, flags); 1232ee6e00c8SJens Axboe if (ret < 0) 1233ee6e00c8SJens Axboe return ret; 1234ee6e00c8SJens Axboe 1235ee6e00c8SJens Axboe if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t))) 1236ee6e00c8SJens Axboe return -EFAULT; 1237ee6e00c8SJens Axboe if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t))) 1238ee6e00c8SJens Axboe return -EFAULT; 1239ee6e00c8SJens Axboe 1240ee6e00c8SJens Axboe return ret; 1241ee6e00c8SJens Axboe } 1242ee6e00c8SJens Axboe 124379fddc4eSAl Viro static int iter_to_pipe(struct iov_iter *from, 124479fddc4eSAl Viro struct pipe_inode_info *pipe, 124579fddc4eSAl Viro unsigned flags) 1246912d35f8SJens Axboe { 124779fddc4eSAl Viro struct pipe_buffer buf = { 124879fddc4eSAl Viro .ops = &user_page_pipe_buf_ops, 124979fddc4eSAl Viro .flags = flags 125079fddc4eSAl Viro }; 125179fddc4eSAl Viro size_t total = 0; 125279fddc4eSAl Viro int ret = 0; 125379fddc4eSAl Viro 12547d690c15SAl Viro while (iov_iter_count(from)) { 125579fddc4eSAl Viro struct page *pages[16]; 12567d690c15SAl Viro ssize_t left; 1257db85a9ebSAl Viro size_t start; 12587d690c15SAl Viro int i, n; 1259912d35f8SJens Axboe 12607d690c15SAl Viro left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start); 12617d690c15SAl Viro if (left <= 0) { 12627d690c15SAl Viro ret = left; 126379fddc4eSAl Viro break; 126479fddc4eSAl Viro } 1265912d35f8SJens Axboe 12667d690c15SAl Viro n = DIV_ROUND_UP(left + start, PAGE_SIZE); 12677d690c15SAl Viro for (i = 0; i < n; i++) { 12687d690c15SAl Viro int size = min_t(int, left, PAGE_SIZE - start); 12697d690c15SAl Viro 12707d690c15SAl Viro buf.page = pages[i]; 127179fddc4eSAl Viro buf.offset = start; 127279fddc4eSAl Viro buf.len = size; 127379fddc4eSAl Viro ret = add_to_pipe(pipe, &buf); 127479fddc4eSAl Viro if (unlikely(ret < 0)) { 12757d690c15SAl Viro iov_iter_revert(from, left); 12767d690c15SAl Viro // this one got dropped by add_to_pipe() 12777d690c15SAl Viro while (++i < n) 12787d690c15SAl Viro put_page(pages[i]); 12797d690c15SAl Viro goto out; 12807d690c15SAl Viro } 128179fddc4eSAl Viro total += ret; 12827d690c15SAl Viro left -= size; 12837d690c15SAl Viro start = 0; 1284912d35f8SJens Axboe } 1285912d35f8SJens Axboe } 12867d690c15SAl Viro out: 128779fddc4eSAl Viro return total ? total : ret; 1288912d35f8SJens Axboe } 1289912d35f8SJens Axboe 12906a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 12916a14b90bSJens Axboe struct splice_desc *sd) 12926a14b90bSJens Axboe { 12936130f531SAl Viro int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 12946130f531SAl Viro return n == sd->len ? n : -EFAULT; 12956a14b90bSJens Axboe } 12966a14b90bSJens Axboe 12976a14b90bSJens Axboe /* 12986a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 12996a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 13006a14b90bSJens Axboe */ 130187a3002aSAl Viro static long vmsplice_to_user(struct file *file, struct iov_iter *iter, 130287a3002aSAl Viro unsigned int flags) 13036a14b90bSJens Axboe { 1304c73be61cSDavid Howells struct pipe_inode_info *pipe = get_pipe_info(file, true); 130587a3002aSAl Viro struct splice_desc sd = { 130687a3002aSAl Viro .total_len = iov_iter_count(iter), 130787a3002aSAl Viro .flags = flags, 130887a3002aSAl Viro .u.data = iter 130987a3002aSAl Viro }; 131087a3002aSAl Viro long ret = 0; 13116a14b90bSJens Axboe 13126a14b90bSJens Axboe if (!pipe) 13136a14b90bSJens Axboe return -EBADF; 13146a14b90bSJens Axboe 1315345995faSAl Viro if (sd.total_len) { 13166130f531SAl Viro pipe_lock(pipe); 13176130f531SAl Viro ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 131861e0d47cSMiklos Szeredi pipe_unlock(pipe); 1319345995faSAl Viro } 13206a14b90bSJens Axboe 13216a14b90bSJens Axboe return ret; 13226a14b90bSJens Axboe } 13236a14b90bSJens Axboe 1324912d35f8SJens Axboe /* 1325912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1326912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1327912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1328912d35f8SJens Axboe */ 132987a3002aSAl Viro static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, 133087a3002aSAl Viro unsigned int flags) 1331912d35f8SJens Axboe { 1332ddac0d39SJens Axboe struct pipe_inode_info *pipe; 133387a3002aSAl Viro long ret = 0; 133479fddc4eSAl Viro unsigned buf_flag = 0; 133579fddc4eSAl Viro 133679fddc4eSAl Viro if (flags & SPLICE_F_GIFT) 133779fddc4eSAl Viro buf_flag = PIPE_BUF_FLAG_GIFT; 1338912d35f8SJens Axboe 1339c73be61cSDavid Howells pipe = get_pipe_info(file, true); 1340ddac0d39SJens Axboe if (!pipe) 1341912d35f8SJens Axboe return -EBADF; 1342912d35f8SJens Axboe 13438924feffSAl Viro pipe_lock(pipe); 13448924feffSAl Viro ret = wait_for_space(pipe, flags); 134579fddc4eSAl Viro if (!ret) 134687a3002aSAl Viro ret = iter_to_pipe(iter, pipe, buf_flag); 13478924feffSAl Viro pipe_unlock(pipe); 13488924feffSAl Viro if (ret > 0) 13498924feffSAl Viro wakeup_pipe_readers(pipe); 135035f3d14dSJens Axboe return ret; 1351912d35f8SJens Axboe } 1352912d35f8SJens Axboe 135387a3002aSAl Viro static int vmsplice_type(struct fd f, int *type) 135487a3002aSAl Viro { 135587a3002aSAl Viro if (!f.file) 135687a3002aSAl Viro return -EBADF; 135787a3002aSAl Viro if (f.file->f_mode & FMODE_WRITE) { 1358de4eda9dSAl Viro *type = ITER_SOURCE; 135987a3002aSAl Viro } else if (f.file->f_mode & FMODE_READ) { 1360de4eda9dSAl Viro *type = ITER_DEST; 136187a3002aSAl Viro } else { 136287a3002aSAl Viro fdput(f); 136387a3002aSAl Viro return -EBADF; 136487a3002aSAl Viro } 136587a3002aSAl Viro return 0; 136687a3002aSAl Viro } 136787a3002aSAl Viro 13686a14b90bSJens Axboe /* 13696a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 13706a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 13716a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 13726a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 13736a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 13746a14b90bSJens Axboe * solutions for that: 13756a14b90bSJens Axboe * 13766a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 13776a14b90bSJens Axboe * do a regular read() on the buffer anyway. 13786a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 13796a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 13806a14b90bSJens Axboe * 13816a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 13826a14b90bSJens Axboe * 13836a14b90bSJens Axboe */ 138487a3002aSAl Viro SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, 138530cfe4efSDominik Brodowski unsigned long, nr_segs, unsigned int, flags) 138630cfe4efSDominik Brodowski { 138787a3002aSAl Viro struct iovec iovstack[UIO_FASTIOV]; 138887a3002aSAl Viro struct iovec *iov = iovstack; 138987a3002aSAl Viro struct iov_iter iter; 139087e5e6daSJens Axboe ssize_t error; 139187a3002aSAl Viro struct fd f; 139287a3002aSAl Viro int type; 139387a3002aSAl Viro 1394598b3cecSChristoph Hellwig if (unlikely(flags & ~SPLICE_F_ALL)) 1395598b3cecSChristoph Hellwig return -EINVAL; 1396598b3cecSChristoph Hellwig 139787a3002aSAl Viro f = fdget(fd); 139887a3002aSAl Viro error = vmsplice_type(f, &type); 139987a3002aSAl Viro if (error) 140087a3002aSAl Viro return error; 140187a3002aSAl Viro 140287a3002aSAl Viro error = import_iovec(type, uiov, nr_segs, 140387a3002aSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 1404598b3cecSChristoph Hellwig if (error < 0) 1405598b3cecSChristoph Hellwig goto out_fdput; 1406598b3cecSChristoph Hellwig 1407598b3cecSChristoph Hellwig if (!iov_iter_count(&iter)) 1408598b3cecSChristoph Hellwig error = 0; 1409de4eda9dSAl Viro else if (type == ITER_SOURCE) 1410598b3cecSChristoph Hellwig error = vmsplice_to_pipe(f.file, &iter, flags); 1411598b3cecSChristoph Hellwig else 1412598b3cecSChristoph Hellwig error = vmsplice_to_user(f.file, &iter, flags); 1413598b3cecSChristoph Hellwig 141487a3002aSAl Viro kfree(iov); 1415598b3cecSChristoph Hellwig out_fdput: 141687a3002aSAl Viro fdput(f); 141787a3002aSAl Viro return error; 141830cfe4efSDominik Brodowski } 141930cfe4efSDominik Brodowski 1420836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1421836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1422836f92adSHeiko Carstens size_t, len, unsigned int, flags) 14235274f052SJens Axboe { 14242903ff01SAl Viro struct fd in, out; 14255274f052SJens Axboe long error; 14265274f052SJens Axboe 14275274f052SJens Axboe if (unlikely(!len)) 14285274f052SJens Axboe return 0; 14295274f052SJens Axboe 14303d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 14313d6ea290SAl Viro return -EINVAL; 14323d6ea290SAl Viro 14335274f052SJens Axboe error = -EBADF; 14342903ff01SAl Viro in = fdget(fd_in); 14352903ff01SAl Viro if (in.file) { 14362903ff01SAl Viro out = fdget(fd_out); 14372903ff01SAl Viro if (out.file) { 1438ee6e00c8SJens Axboe error = __do_splice(in.file, off_in, out.file, off_out, 1439529565dcSIngo Molnar len, flags); 14402903ff01SAl Viro fdput(out); 14415274f052SJens Axboe } 14422903ff01SAl Viro fdput(in); 14435274f052SJens Axboe } 14445274f052SJens Axboe return error; 14455274f052SJens Axboe } 144670524490SJens Axboe 144770524490SJens Axboe /* 1448aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1449aadd06e5SJens Axboe * return an appropriate error. 1450aadd06e5SJens Axboe */ 14517c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1452aadd06e5SJens Axboe { 1453aadd06e5SJens Axboe int ret; 1454aadd06e5SJens Axboe 1455aadd06e5SJens Axboe /* 14568cefc107SDavid Howells * Check the pipe occupancy without the inode lock first. This function 1457aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1458aadd06e5SJens Axboe */ 14598cefc107SDavid Howells if (!pipe_empty(pipe->head, pipe->tail)) 1460aadd06e5SJens Axboe return 0; 1461aadd06e5SJens Axboe 1462aadd06e5SJens Axboe ret = 0; 146361e0d47cSMiklos Szeredi pipe_lock(pipe); 1464aadd06e5SJens Axboe 14658cefc107SDavid Howells while (pipe_empty(pipe->head, pipe->tail)) { 1466aadd06e5SJens Axboe if (signal_pending(current)) { 1467aadd06e5SJens Axboe ret = -ERESTARTSYS; 1468aadd06e5SJens Axboe break; 1469aadd06e5SJens Axboe } 1470aadd06e5SJens Axboe if (!pipe->writers) 1471aadd06e5SJens Axboe break; 1472aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1473aadd06e5SJens Axboe ret = -EAGAIN; 1474aadd06e5SJens Axboe break; 1475aadd06e5SJens Axboe } 1476472e5b05SLinus Torvalds pipe_wait_readable(pipe); 1477aadd06e5SJens Axboe } 1478aadd06e5SJens Axboe 147961e0d47cSMiklos Szeredi pipe_unlock(pipe); 1480aadd06e5SJens Axboe return ret; 1481aadd06e5SJens Axboe } 1482aadd06e5SJens Axboe 1483aadd06e5SJens Axboe /* 1484aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1485aadd06e5SJens Axboe * return an appropriate error. 1486aadd06e5SJens Axboe */ 14877c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1488aadd06e5SJens Axboe { 1489aadd06e5SJens Axboe int ret; 1490aadd06e5SJens Axboe 1491aadd06e5SJens Axboe /* 14928cefc107SDavid Howells * Check pipe occupancy without the inode lock first. This function 1493aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1494aadd06e5SJens Axboe */ 1495566d1362STetsuo Handa if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 1496aadd06e5SJens Axboe return 0; 1497aadd06e5SJens Axboe 1498aadd06e5SJens Axboe ret = 0; 149961e0d47cSMiklos Szeredi pipe_lock(pipe); 1500aadd06e5SJens Axboe 15016718b6f8SDavid Howells while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 1502aadd06e5SJens Axboe if (!pipe->readers) { 1503aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1504aadd06e5SJens Axboe ret = -EPIPE; 1505aadd06e5SJens Axboe break; 1506aadd06e5SJens Axboe } 1507aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1508aadd06e5SJens Axboe ret = -EAGAIN; 1509aadd06e5SJens Axboe break; 1510aadd06e5SJens Axboe } 1511aadd06e5SJens Axboe if (signal_pending(current)) { 1512aadd06e5SJens Axboe ret = -ERESTARTSYS; 1513aadd06e5SJens Axboe break; 1514aadd06e5SJens Axboe } 1515472e5b05SLinus Torvalds pipe_wait_writable(pipe); 1516aadd06e5SJens Axboe } 1517aadd06e5SJens Axboe 151861e0d47cSMiklos Szeredi pipe_unlock(pipe); 1519aadd06e5SJens Axboe return ret; 1520aadd06e5SJens Axboe } 1521aadd06e5SJens Axboe 1522aadd06e5SJens Axboe /* 15237c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 15247c77f0b3SMiklos Szeredi */ 15257c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 15267c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 15277c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 15287c77f0b3SMiklos Szeredi { 15297c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 15308cefc107SDavid Howells unsigned int i_head, o_head; 15318cefc107SDavid Howells unsigned int i_tail, o_tail; 15328cefc107SDavid Howells unsigned int i_mask, o_mask; 15338cefc107SDavid Howells int ret = 0; 15347c77f0b3SMiklos Szeredi bool input_wakeup = false; 15357c77f0b3SMiklos Szeredi 15367c77f0b3SMiklos Szeredi 15377c77f0b3SMiklos Szeredi retry: 15387c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 15397c77f0b3SMiklos Szeredi if (ret) 15407c77f0b3SMiklos Szeredi return ret; 15417c77f0b3SMiklos Szeredi 15427c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 15437c77f0b3SMiklos Szeredi if (ret) 15447c77f0b3SMiklos Szeredi return ret; 15457c77f0b3SMiklos Szeredi 15467c77f0b3SMiklos Szeredi /* 15477c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 15487c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 15497c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 15507c77f0b3SMiklos Szeredi */ 15517c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 15527c77f0b3SMiklos Szeredi 15538cefc107SDavid Howells i_tail = ipipe->tail; 15548cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 15558cefc107SDavid Howells o_head = opipe->head; 15568cefc107SDavid Howells o_mask = opipe->ring_size - 1; 15578cefc107SDavid Howells 15587c77f0b3SMiklos Szeredi do { 15598cefc107SDavid Howells size_t o_len; 15608cefc107SDavid Howells 15617c77f0b3SMiklos Szeredi if (!opipe->readers) { 15627c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 15637c77f0b3SMiklos Szeredi if (!ret) 15647c77f0b3SMiklos Szeredi ret = -EPIPE; 15657c77f0b3SMiklos Szeredi break; 15667c77f0b3SMiklos Szeredi } 15677c77f0b3SMiklos Szeredi 15688cefc107SDavid Howells i_head = ipipe->head; 15698cefc107SDavid Howells o_tail = opipe->tail; 15708cefc107SDavid Howells 15718cefc107SDavid Howells if (pipe_empty(i_head, i_tail) && !ipipe->writers) 15727c77f0b3SMiklos Szeredi break; 15737c77f0b3SMiklos Szeredi 15747c77f0b3SMiklos Szeredi /* 15757c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 15767c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 15777c77f0b3SMiklos Szeredi */ 15788cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 15796718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) { 15807c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 15817c77f0b3SMiklos Szeredi if (ret) 15827c77f0b3SMiklos Szeredi break; 15837c77f0b3SMiklos Szeredi 15847c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 15857c77f0b3SMiklos Szeredi ret = -EAGAIN; 15867c77f0b3SMiklos Szeredi break; 15877c77f0b3SMiklos Szeredi } 15887c77f0b3SMiklos Szeredi 15897c77f0b3SMiklos Szeredi /* 15907c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 15917c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 15927c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 15937c77f0b3SMiklos Szeredi */ 15947c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 15957c77f0b3SMiklos Szeredi pipe_unlock(opipe); 15967c77f0b3SMiklos Szeredi goto retry; 15977c77f0b3SMiklos Szeredi } 15987c77f0b3SMiklos Szeredi 15998cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 16008cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 16017c77f0b3SMiklos Szeredi 16027c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 16037c77f0b3SMiklos Szeredi /* 16047c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 16057c77f0b3SMiklos Szeredi */ 16067c77f0b3SMiklos Szeredi *obuf = *ibuf; 16077c77f0b3SMiklos Szeredi ibuf->ops = NULL; 16088cefc107SDavid Howells i_tail++; 16098cefc107SDavid Howells ipipe->tail = i_tail; 16107c77f0b3SMiklos Szeredi input_wakeup = true; 16118cefc107SDavid Howells o_len = obuf->len; 16128cefc107SDavid Howells o_head++; 16138cefc107SDavid Howells opipe->head = o_head; 16147c77f0b3SMiklos Szeredi } else { 16157c77f0b3SMiklos Szeredi /* 16167c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 16177c77f0b3SMiklos Szeredi * so we can copy the contents over. 16187c77f0b3SMiklos Szeredi */ 161915fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 162015fab63eSMatthew Wilcox if (ret == 0) 162115fab63eSMatthew Wilcox ret = -EFAULT; 162215fab63eSMatthew Wilcox break; 162315fab63eSMatthew Wilcox } 16247c77f0b3SMiklos Szeredi *obuf = *ibuf; 16257c77f0b3SMiklos Szeredi 16267c77f0b3SMiklos Szeredi /* 1627f6dd9755SChristoph Hellwig * Don't inherit the gift and merge flags, we need to 16287c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 16297c77f0b3SMiklos Szeredi */ 16307c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 1631f6dd9755SChristoph Hellwig obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 1632a0ce2f0aSJann Horn 16337c77f0b3SMiklos Szeredi obuf->len = len; 16348cefc107SDavid Howells ibuf->offset += len; 16358cefc107SDavid Howells ibuf->len -= len; 16368cefc107SDavid Howells o_len = len; 16378cefc107SDavid Howells o_head++; 16388cefc107SDavid Howells opipe->head = o_head; 16397c77f0b3SMiklos Szeredi } 16408cefc107SDavid Howells ret += o_len; 16418cefc107SDavid Howells len -= o_len; 16427c77f0b3SMiklos Szeredi } while (len); 16437c77f0b3SMiklos Szeredi 16447c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 16457c77f0b3SMiklos Szeredi pipe_unlock(opipe); 16467c77f0b3SMiklos Szeredi 16477c77f0b3SMiklos Szeredi /* 16487c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 16497c77f0b3SMiklos Szeredi */ 1650825cdcb1SNamhyung Kim if (ret > 0) 1651825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1652825cdcb1SNamhyung Kim 16537c77f0b3SMiklos Szeredi if (input_wakeup) 16547c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 16557c77f0b3SMiklos Szeredi 16567c77f0b3SMiklos Szeredi return ret; 16577c77f0b3SMiklos Szeredi } 16587c77f0b3SMiklos Szeredi 16597c77f0b3SMiklos Szeredi /* 166070524490SJens Axboe * Link contents of ipipe to opipe. 166170524490SJens Axboe */ 166270524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 166370524490SJens Axboe struct pipe_inode_info *opipe, 166470524490SJens Axboe size_t len, unsigned int flags) 166570524490SJens Axboe { 166670524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 16678cefc107SDavid Howells unsigned int i_head, o_head; 16688cefc107SDavid Howells unsigned int i_tail, o_tail; 16698cefc107SDavid Howells unsigned int i_mask, o_mask; 16708cefc107SDavid Howells int ret = 0; 167170524490SJens Axboe 167270524490SJens Axboe /* 167370524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 167461e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 167570524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 167670524490SJens Axboe */ 167761e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 167870524490SJens Axboe 16798cefc107SDavid Howells i_tail = ipipe->tail; 16808cefc107SDavid Howells i_mask = ipipe->ring_size - 1; 16818cefc107SDavid Howells o_head = opipe->head; 16828cefc107SDavid Howells o_mask = opipe->ring_size - 1; 16838cefc107SDavid Howells 1684aadd06e5SJens Axboe do { 168570524490SJens Axboe if (!opipe->readers) { 168670524490SJens Axboe send_sig(SIGPIPE, current, 0); 168770524490SJens Axboe if (!ret) 168870524490SJens Axboe ret = -EPIPE; 168970524490SJens Axboe break; 169070524490SJens Axboe } 169170524490SJens Axboe 16928cefc107SDavid Howells i_head = ipipe->head; 16938cefc107SDavid Howells o_tail = opipe->tail; 16948cefc107SDavid Howells 169570524490SJens Axboe /* 16968cefc107SDavid Howells * If we have iterated all input buffers or run out of 1697aadd06e5SJens Axboe * output room, break. 169870524490SJens Axboe */ 16998cefc107SDavid Howells if (pipe_empty(i_head, i_tail) || 17006718b6f8SDavid Howells pipe_full(o_head, o_tail, opipe->max_usage)) 1701aadd06e5SJens Axboe break; 1702aadd06e5SJens Axboe 17038cefc107SDavid Howells ibuf = &ipipe->bufs[i_tail & i_mask]; 17048cefc107SDavid Howells obuf = &opipe->bufs[o_head & o_mask]; 170570524490SJens Axboe 170670524490SJens Axboe /* 170770524490SJens Axboe * Get a reference to this pipe buffer, 170870524490SJens Axboe * so we can copy the contents over. 170970524490SJens Axboe */ 171015fab63eSMatthew Wilcox if (!pipe_buf_get(ipipe, ibuf)) { 171115fab63eSMatthew Wilcox if (ret == 0) 171215fab63eSMatthew Wilcox ret = -EFAULT; 171315fab63eSMatthew Wilcox break; 171415fab63eSMatthew Wilcox } 171570524490SJens Axboe 171670524490SJens Axboe *obuf = *ibuf; 171770524490SJens Axboe 17187afa6fd0SJens Axboe /* 1719f6dd9755SChristoph Hellwig * Don't inherit the gift and merge flag, we need to prevent 1720f6dd9755SChristoph Hellwig * multiple steals of this page. 17217afa6fd0SJens Axboe */ 17227afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 1723f6dd9755SChristoph Hellwig obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 1724a0ce2f0aSJann Horn 172570524490SJens Axboe if (obuf->len > len) 172670524490SJens Axboe obuf->len = len; 172770524490SJens Axboe ret += obuf->len; 172870524490SJens Axboe len -= obuf->len; 17298cefc107SDavid Howells 17308cefc107SDavid Howells o_head++; 17318cefc107SDavid Howells opipe->head = o_head; 17328cefc107SDavid Howells i_tail++; 1733aadd06e5SJens Axboe } while (len); 173470524490SJens Axboe 173561e0d47cSMiklos Szeredi pipe_unlock(ipipe); 173661e0d47cSMiklos Szeredi pipe_unlock(opipe); 173770524490SJens Axboe 1738aadd06e5SJens Axboe /* 1739aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1740aadd06e5SJens Axboe */ 1741825cdcb1SNamhyung Kim if (ret > 0) 1742825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 174370524490SJens Axboe 174470524490SJens Axboe return ret; 174570524490SJens Axboe } 174670524490SJens Axboe 174770524490SJens Axboe /* 174870524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 174970524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 175070524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 175170524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 175270524490SJens Axboe */ 17539dafdfc2SPavel Begunkov long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) 175470524490SJens Axboe { 1755c73be61cSDavid Howells struct pipe_inode_info *ipipe = get_pipe_info(in, true); 1756c73be61cSDavid Howells struct pipe_inode_info *opipe = get_pipe_info(out, true); 1757aadd06e5SJens Axboe int ret = -EINVAL; 175870524490SJens Axboe 175990da2e3fSPavel Begunkov if (unlikely(!(in->f_mode & FMODE_READ) || 176090da2e3fSPavel Begunkov !(out->f_mode & FMODE_WRITE))) 176190da2e3fSPavel Begunkov return -EBADF; 176290da2e3fSPavel Begunkov 176370524490SJens Axboe /* 1764aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 1765aadd06e5SJens Axboe * copying the data. 176670524490SJens Axboe */ 1767aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 1768ee5e0011SSlavomir Kaslev if ((in->f_flags | out->f_flags) & O_NONBLOCK) 1769ee5e0011SSlavomir Kaslev flags |= SPLICE_F_NONBLOCK; 1770ee5e0011SSlavomir Kaslev 1771aadd06e5SJens Axboe /* 1772aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 1773aadd06e5SJens Axboe * ordering doesn't really matter. 1774aadd06e5SJens Axboe */ 17757c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 1776aadd06e5SJens Axboe if (!ret) { 17777c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 177802cf01aeSJens Axboe if (!ret) 1779aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 1780aadd06e5SJens Axboe } 1781aadd06e5SJens Axboe } 178270524490SJens Axboe 1783aadd06e5SJens Axboe return ret; 178470524490SJens Axboe } 178570524490SJens Axboe 1786836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 178770524490SJens Axboe { 178890da2e3fSPavel Begunkov struct fd in, out; 17892903ff01SAl Viro int error; 179070524490SJens Axboe 17913d6ea290SAl Viro if (unlikely(flags & ~SPLICE_F_ALL)) 17923d6ea290SAl Viro return -EINVAL; 17933d6ea290SAl Viro 179470524490SJens Axboe if (unlikely(!len)) 179570524490SJens Axboe return 0; 179670524490SJens Axboe 179770524490SJens Axboe error = -EBADF; 17982903ff01SAl Viro in = fdget(fdin); 17992903ff01SAl Viro if (in.file) { 180090da2e3fSPavel Begunkov out = fdget(fdout); 18012903ff01SAl Viro if (out.file) { 180290da2e3fSPavel Begunkov error = do_tee(in.file, out.file, len, flags); 18032903ff01SAl Viro fdput(out); 180470524490SJens Axboe } 18052903ff01SAl Viro fdput(in); 180670524490SJens Axboe } 180770524490SJens Axboe 180870524490SJens Axboe return error; 180970524490SJens Axboe } 1810