15274f052SJens Axboe /* 25274f052SJens Axboe * "splice": joining two ropes together by interweaving their strands. 35274f052SJens Axboe * 45274f052SJens Axboe * This is the "extended pipe" functionality, where a pipe is used as 55274f052SJens Axboe * an arbitrary in-memory buffer. Think of a pipe as a small kernel 65274f052SJens Axboe * buffer that you can use to transfer data from one end to the other. 75274f052SJens Axboe * 85274f052SJens Axboe * The traditional unix read/write is extended with a "splice()" operation 95274f052SJens Axboe * that transfers data buffers to or from a pipe buffer. 105274f052SJens Axboe * 115274f052SJens Axboe * Named by Larry McVoy, original implementation from Linus, extended by 12c2058e06SJens Axboe * Jens to support splicing to files, network, direct splicing, etc and 13c2058e06SJens Axboe * fixing lots of bugs. 145274f052SJens Axboe * 150fe23479SJens Axboe * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> 16c2058e06SJens Axboe * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> 17c2058e06SJens Axboe * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> 185274f052SJens Axboe * 195274f052SJens Axboe */ 205274f052SJens Axboe #include <linux/fs.h> 215274f052SJens Axboe #include <linux/file.h> 225274f052SJens Axboe #include <linux/pagemap.h> 23d6b29d7cSJens Axboe #include <linux/splice.h> 2408e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h> 255274f052SJens Axboe #include <linux/mm_inline.h> 265abc97aaSJens Axboe #include <linux/swap.h> 274f6f0bd2SJens Axboe #include <linux/writeback.h> 28630d9c47SPaul Gortmaker #include <linux/export.h> 294f6f0bd2SJens Axboe #include <linux/syscalls.h> 30912d35f8SJens Axboe #include <linux/uio.h> 3129ce2058SJames Morris #include <linux/security.h> 325a0e3ad6STejun Heo #include <linux/gfp.h> 3335f9c09fSEric Dumazet #include <linux/socket.h> 3476b021d0SAl Viro #include <linux/compat.h> 3506ae43f3SAl Viro #include "internal.h" 365274f052SJens Axboe 3783f9135bSJens Axboe /* 3883f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 3983f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 4083f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 4183f9135bSJens Axboe * attempt to reuse this page for another destination. 4283f9135bSJens Axboe */ 4376ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, 445abc97aaSJens Axboe struct pipe_buffer *buf) 455abc97aaSJens Axboe { 465abc97aaSJens Axboe struct page *page = buf->page; 479e94cd4fSJens Axboe struct address_space *mapping; 485abc97aaSJens Axboe 499e0267c2SJens Axboe lock_page(page); 509e0267c2SJens Axboe 519e94cd4fSJens Axboe mapping = page_mapping(page); 529e94cd4fSJens Axboe if (mapping) { 535abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 545abc97aaSJens Axboe 55ad8d6f0aSJens Axboe /* 569e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 579e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 589e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 599e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 609e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 619e94cd4fSJens Axboe * ensues. 62ad8d6f0aSJens Axboe */ 63ad8d6f0aSJens Axboe wait_on_page_writeback(page); 64ad8d6f0aSJens Axboe 65266cf658SDavid Howells if (page_has_private(page) && 66266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 67ca39d651SJens Axboe goto out_unlock; 684f6f0bd2SJens Axboe 699e94cd4fSJens Axboe /* 709e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 719e94cd4fSJens Axboe * and return good. 729e94cd4fSJens Axboe */ 739e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 741432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 755abc97aaSJens Axboe return 0; 765abc97aaSJens Axboe } 779e94cd4fSJens Axboe } 789e94cd4fSJens Axboe 799e94cd4fSJens Axboe /* 809e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 819e94cd4fSJens Axboe * address space, unlock and return failure. 829e94cd4fSJens Axboe */ 83ca39d651SJens Axboe out_unlock: 849e94cd4fSJens Axboe unlock_page(page); 859e94cd4fSJens Axboe return 1; 869e94cd4fSJens Axboe } 875abc97aaSJens Axboe 8876ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 895274f052SJens Axboe struct pipe_buffer *buf) 905274f052SJens Axboe { 915274f052SJens Axboe page_cache_release(buf->page); 921432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 935274f052SJens Axboe } 945274f052SJens Axboe 950845718dSJens Axboe /* 960845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 970845718dSJens Axboe * is a page cache page, IO may be in flight. 980845718dSJens Axboe */ 99cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 1005274f052SJens Axboe struct pipe_buffer *buf) 1015274f052SJens Axboe { 1025274f052SJens Axboe struct page *page = buf->page; 10349d0b21bSJens Axboe int err; 1045274f052SJens Axboe 1055274f052SJens Axboe if (!PageUptodate(page)) { 10649d0b21bSJens Axboe lock_page(page); 1075274f052SJens Axboe 10849d0b21bSJens Axboe /* 10949d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 11073d62d83SIngo Molnar * splice, if this is the first page. 11149d0b21bSJens Axboe */ 1125274f052SJens Axboe if (!page->mapping) { 11349d0b21bSJens Axboe err = -ENODATA; 11449d0b21bSJens Axboe goto error; 1155274f052SJens Axboe } 1165274f052SJens Axboe 11749d0b21bSJens Axboe /* 11873d62d83SIngo Molnar * Uh oh, read-error from disk. 11949d0b21bSJens Axboe */ 12049d0b21bSJens Axboe if (!PageUptodate(page)) { 12149d0b21bSJens Axboe err = -EIO; 12249d0b21bSJens Axboe goto error; 12349d0b21bSJens Axboe } 12449d0b21bSJens Axboe 12549d0b21bSJens Axboe /* 126f84d7519SJens Axboe * Page is ok afterall, we are done. 12749d0b21bSJens Axboe */ 12849d0b21bSJens Axboe unlock_page(page); 12949d0b21bSJens Axboe } 13049d0b21bSJens Axboe 131f84d7519SJens Axboe return 0; 13249d0b21bSJens Axboe error: 13349d0b21bSJens Axboe unlock_page(page); 134f84d7519SJens Axboe return err; 13570524490SJens Axboe } 13670524490SJens Axboe 137708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 1385274f052SJens Axboe .can_merge = 0, 139f84d7519SJens Axboe .map = generic_pipe_buf_map, 140f84d7519SJens Axboe .unmap = generic_pipe_buf_unmap, 141cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1425274f052SJens Axboe .release = page_cache_pipe_buf_release, 1435abc97aaSJens Axboe .steal = page_cache_pipe_buf_steal, 144f84d7519SJens Axboe .get = generic_pipe_buf_get, 1455274f052SJens Axboe }; 1465274f052SJens Axboe 147912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, 148912d35f8SJens Axboe struct pipe_buffer *buf) 149912d35f8SJens Axboe { 1507afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 151912d35f8SJens Axboe return 1; 1527afa6fd0SJens Axboe 1531432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 154330ab716SJens Axboe return generic_pipe_buf_steal(pipe, buf); 155912d35f8SJens Axboe } 156912d35f8SJens Axboe 157d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 158912d35f8SJens Axboe .can_merge = 0, 159f84d7519SJens Axboe .map = generic_pipe_buf_map, 160f84d7519SJens Axboe .unmap = generic_pipe_buf_unmap, 161cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 162912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 163912d35f8SJens Axboe .steal = user_page_pipe_buf_steal, 164f84d7519SJens Axboe .get = generic_pipe_buf_get, 165912d35f8SJens Axboe }; 166912d35f8SJens Axboe 167825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 168825cdcb1SNamhyung Kim { 169825cdcb1SNamhyung Kim smp_mb(); 170825cdcb1SNamhyung Kim if (waitqueue_active(&pipe->wait)) 171825cdcb1SNamhyung Kim wake_up_interruptible(&pipe->wait); 172825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 173825cdcb1SNamhyung Kim } 174825cdcb1SNamhyung Kim 175932cc6d4SJens Axboe /** 176932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 177932cc6d4SJens Axboe * @pipe: pipe to fill 178932cc6d4SJens Axboe * @spd: data to fill 179932cc6d4SJens Axboe * 180932cc6d4SJens Axboe * Description: 18179685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 182932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 183932cc6d4SJens Axboe * function will link that data to the pipe. 184932cc6d4SJens Axboe * 18583f9135bSJens Axboe */ 186d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 187912d35f8SJens Axboe struct splice_pipe_desc *spd) 1885274f052SJens Axboe { 18900de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 190912d35f8SJens Axboe int ret, do_wakeup, page_nr; 1915274f052SJens Axboe 1925274f052SJens Axboe ret = 0; 1935274f052SJens Axboe do_wakeup = 0; 194912d35f8SJens Axboe page_nr = 0; 1955274f052SJens Axboe 19661e0d47cSMiklos Szeredi pipe_lock(pipe); 1975274f052SJens Axboe 1985274f052SJens Axboe for (;;) { 1993a326a2cSIngo Molnar if (!pipe->readers) { 2005274f052SJens Axboe send_sig(SIGPIPE, current, 0); 2015274f052SJens Axboe if (!ret) 2025274f052SJens Axboe ret = -EPIPE; 2035274f052SJens Axboe break; 2045274f052SJens Axboe } 2055274f052SJens Axboe 20635f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) { 20735f3d14dSJens Axboe int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1); 2083a326a2cSIngo Molnar struct pipe_buffer *buf = pipe->bufs + newbuf; 2095274f052SJens Axboe 210912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 211912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 212912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 213497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 214912d35f8SJens Axboe buf->ops = spd->ops; 2157afa6fd0SJens Axboe if (spd->flags & SPLICE_F_GIFT) 2167afa6fd0SJens Axboe buf->flags |= PIPE_BUF_FLAG_GIFT; 2177afa6fd0SJens Axboe 2186f767b04SJens Axboe pipe->nrbufs++; 219912d35f8SJens Axboe page_nr++; 220912d35f8SJens Axboe ret += buf->len; 221912d35f8SJens Axboe 2226447a3cfSAl Viro if (pipe->files) 2235274f052SJens Axboe do_wakeup = 1; 2245274f052SJens Axboe 225912d35f8SJens Axboe if (!--spd->nr_pages) 2265274f052SJens Axboe break; 22735f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 2285274f052SJens Axboe continue; 2295274f052SJens Axboe 2305274f052SJens Axboe break; 2315274f052SJens Axboe } 2325274f052SJens Axboe 233912d35f8SJens Axboe if (spd->flags & SPLICE_F_NONBLOCK) { 23429e35094SLinus Torvalds if (!ret) 23529e35094SLinus Torvalds ret = -EAGAIN; 23629e35094SLinus Torvalds break; 23729e35094SLinus Torvalds } 23829e35094SLinus Torvalds 2395274f052SJens Axboe if (signal_pending(current)) { 2405274f052SJens Axboe if (!ret) 2415274f052SJens Axboe ret = -ERESTARTSYS; 2425274f052SJens Axboe break; 2435274f052SJens Axboe } 2445274f052SJens Axboe 2455274f052SJens Axboe if (do_wakeup) { 246c0bd1f65SJens Axboe smp_mb(); 2473a326a2cSIngo Molnar if (waitqueue_active(&pipe->wait)) 2483a326a2cSIngo Molnar wake_up_interruptible_sync(&pipe->wait); 2493a326a2cSIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 2505274f052SJens Axboe do_wakeup = 0; 2515274f052SJens Axboe } 2525274f052SJens Axboe 2533a326a2cSIngo Molnar pipe->waiting_writers++; 2543a326a2cSIngo Molnar pipe_wait(pipe); 2553a326a2cSIngo Molnar pipe->waiting_writers--; 2565274f052SJens Axboe } 2575274f052SJens Axboe 25861e0d47cSMiklos Szeredi pipe_unlock(pipe); 2595274f052SJens Axboe 260825cdcb1SNamhyung Kim if (do_wakeup) 261825cdcb1SNamhyung Kim wakeup_pipe_readers(pipe); 2625274f052SJens Axboe 26300de00bdSJens Axboe while (page_nr < spd_pages) 264bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2655274f052SJens Axboe 2665274f052SJens Axboe return ret; 2675274f052SJens Axboe } 2685274f052SJens Axboe 269708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) 270bbdfc2f7SJens Axboe { 271bbdfc2f7SJens Axboe page_cache_release(spd->pages[i]); 272bbdfc2f7SJens Axboe } 273bbdfc2f7SJens Axboe 27435f3d14dSJens Axboe /* 27535f3d14dSJens Axboe * Check if we need to grow the arrays holding pages and partial page 27635f3d14dSJens Axboe * descriptions. 27735f3d14dSJens Axboe */ 278047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 27935f3d14dSJens Axboe { 280047fe360SEric Dumazet unsigned int buffers = ACCESS_ONCE(pipe->buffers); 281047fe360SEric Dumazet 282047fe360SEric Dumazet spd->nr_pages_max = buffers; 283047fe360SEric Dumazet if (buffers <= PIPE_DEF_BUFFERS) 28435f3d14dSJens Axboe return 0; 28535f3d14dSJens Axboe 286047fe360SEric Dumazet spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL); 287047fe360SEric Dumazet spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL); 28835f3d14dSJens Axboe 28935f3d14dSJens Axboe if (spd->pages && spd->partial) 29035f3d14dSJens Axboe return 0; 29135f3d14dSJens Axboe 29235f3d14dSJens Axboe kfree(spd->pages); 29335f3d14dSJens Axboe kfree(spd->partial); 29435f3d14dSJens Axboe return -ENOMEM; 29535f3d14dSJens Axboe } 29635f3d14dSJens Axboe 297047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd) 29835f3d14dSJens Axboe { 299047fe360SEric Dumazet if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 30035f3d14dSJens Axboe return; 30135f3d14dSJens Axboe 30235f3d14dSJens Axboe kfree(spd->pages); 30335f3d14dSJens Axboe kfree(spd->partial); 30435f3d14dSJens Axboe } 30535f3d14dSJens Axboe 3063a326a2cSIngo Molnar static int 307cbb7e577SJens Axboe __generic_file_splice_read(struct file *in, loff_t *ppos, 308cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 309cbb7e577SJens Axboe unsigned int flags) 3105274f052SJens Axboe { 3115274f052SJens Axboe struct address_space *mapping = in->f_mapping; 312d8983910SFengguang Wu unsigned int loff, nr_pages, req_pages; 31335f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 31435f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 3155274f052SJens Axboe struct page *page; 31691ad66efSJens Axboe pgoff_t index, end_index; 31791ad66efSJens Axboe loff_t isize; 318eb20796bSJens Axboe int error, page_nr; 319912d35f8SJens Axboe struct splice_pipe_desc spd = { 320912d35f8SJens Axboe .pages = pages, 321912d35f8SJens Axboe .partial = partial, 322047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 323912d35f8SJens Axboe .flags = flags, 324912d35f8SJens Axboe .ops = &page_cache_pipe_buf_ops, 325bbdfc2f7SJens Axboe .spd_release = spd_release_page, 326912d35f8SJens Axboe }; 3275274f052SJens Axboe 32835f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 32935f3d14dSJens Axboe return -ENOMEM; 33035f3d14dSJens Axboe 331cbb7e577SJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 332912d35f8SJens Axboe loff = *ppos & ~PAGE_CACHE_MASK; 333d8983910SFengguang Wu req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 334047fe360SEric Dumazet nr_pages = min(req_pages, spd.nr_pages_max); 3355274f052SJens Axboe 3365274f052SJens Axboe /* 337eb20796bSJens Axboe * Lookup the (hopefully) full range of pages we need. 33882aa5d61SJens Axboe */ 33935f3d14dSJens Axboe spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages); 340431a4820SFengguang Wu index += spd.nr_pages; 341eb20796bSJens Axboe 3425274f052SJens Axboe /* 343eb20796bSJens Axboe * If find_get_pages_contig() returned fewer pages than we needed, 344431a4820SFengguang Wu * readahead/allocate the rest and fill in the holes. 345eb20796bSJens Axboe */ 346431a4820SFengguang Wu if (spd.nr_pages < nr_pages) 347cf914a7dSRusty Russell page_cache_sync_readahead(mapping, &in->f_ra, in, 348cf914a7dSRusty Russell index, req_pages - spd.nr_pages); 349431a4820SFengguang Wu 350932cc6d4SJens Axboe error = 0; 351eb20796bSJens Axboe while (spd.nr_pages < nr_pages) { 352eb20796bSJens Axboe /* 353eb20796bSJens Axboe * Page could be there, find_get_pages_contig() breaks on 354eb20796bSJens Axboe * the first hole. 3555274f052SJens Axboe */ 3567480a904SJens Axboe page = find_get_page(mapping, index); 3577480a904SJens Axboe if (!page) { 358e27dedd8SJens Axboe /* 359eb20796bSJens Axboe * page didn't exist, allocate one. 3607480a904SJens Axboe */ 3617480a904SJens Axboe page = page_cache_alloc_cold(mapping); 3625274f052SJens Axboe if (!page) 3635274f052SJens Axboe break; 3645274f052SJens Axboe 3657480a904SJens Axboe error = add_to_page_cache_lru(page, mapping, index, 3660ae0b5d0SNick Piggin GFP_KERNEL); 3675274f052SJens Axboe if (unlikely(error)) { 3685274f052SJens Axboe page_cache_release(page); 369a0548871SJens Axboe if (error == -EEXIST) 370a0548871SJens Axboe continue; 3715274f052SJens Axboe break; 3725274f052SJens Axboe } 373eb20796bSJens Axboe /* 374eb20796bSJens Axboe * add_to_page_cache() locks the page, unlock it 375eb20796bSJens Axboe * to avoid convoluting the logic below even more. 376eb20796bSJens Axboe */ 377eb20796bSJens Axboe unlock_page(page); 3785274f052SJens Axboe } 3797480a904SJens Axboe 38035f3d14dSJens Axboe spd.pages[spd.nr_pages++] = page; 381eb20796bSJens Axboe index++; 382eb20796bSJens Axboe } 383eb20796bSJens Axboe 384eb20796bSJens Axboe /* 385eb20796bSJens Axboe * Now loop over the map and see if we need to start IO on any 386eb20796bSJens Axboe * pages, fill in the partial map, etc. 387eb20796bSJens Axboe */ 388eb20796bSJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 389eb20796bSJens Axboe nr_pages = spd.nr_pages; 390eb20796bSJens Axboe spd.nr_pages = 0; 391eb20796bSJens Axboe for (page_nr = 0; page_nr < nr_pages; page_nr++) { 392eb20796bSJens Axboe unsigned int this_len; 393eb20796bSJens Axboe 394eb20796bSJens Axboe if (!len) 395eb20796bSJens Axboe break; 396eb20796bSJens Axboe 397eb20796bSJens Axboe /* 398eb20796bSJens Axboe * this_len is the max we'll use from this page 399eb20796bSJens Axboe */ 400eb20796bSJens Axboe this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 40135f3d14dSJens Axboe page = spd.pages[page_nr]; 402eb20796bSJens Axboe 403a08a166fSFengguang Wu if (PageReadahead(page)) 404cf914a7dSRusty Russell page_cache_async_readahead(mapping, &in->f_ra, in, 405d8983910SFengguang Wu page, index, req_pages - page_nr); 406a08a166fSFengguang Wu 4077480a904SJens Axboe /* 4087480a904SJens Axboe * If the page isn't uptodate, we may need to start io on it 4097480a904SJens Axboe */ 4107480a904SJens Axboe if (!PageUptodate(page)) { 4117480a904SJens Axboe lock_page(page); 4127480a904SJens Axboe 4137480a904SJens Axboe /* 41432502b84SMiklos Szeredi * Page was truncated, or invalidated by the 41532502b84SMiklos Szeredi * filesystem. Redo the find/create, but this time the 41632502b84SMiklos Szeredi * page is kept locked, so there's no chance of another 41732502b84SMiklos Szeredi * race with truncate/invalidate. 4187480a904SJens Axboe */ 4197480a904SJens Axboe if (!page->mapping) { 4207480a904SJens Axboe unlock_page(page); 42132502b84SMiklos Szeredi page = find_or_create_page(mapping, index, 42232502b84SMiklos Szeredi mapping_gfp_mask(mapping)); 42332502b84SMiklos Szeredi 42432502b84SMiklos Szeredi if (!page) { 42532502b84SMiklos Szeredi error = -ENOMEM; 4267480a904SJens Axboe break; 4277480a904SJens Axboe } 42835f3d14dSJens Axboe page_cache_release(spd.pages[page_nr]); 42935f3d14dSJens Axboe spd.pages[page_nr] = page; 43032502b84SMiklos Szeredi } 4317480a904SJens Axboe /* 4327480a904SJens Axboe * page was already under io and is now done, great 4337480a904SJens Axboe */ 4347480a904SJens Axboe if (PageUptodate(page)) { 4357480a904SJens Axboe unlock_page(page); 4367480a904SJens Axboe goto fill_it; 4377480a904SJens Axboe } 4387480a904SJens Axboe 4397480a904SJens Axboe /* 4407480a904SJens Axboe * need to read in the page 4417480a904SJens Axboe */ 4427480a904SJens Axboe error = mapping->a_ops->readpage(in, page); 4437480a904SJens Axboe if (unlikely(error)) { 444eb20796bSJens Axboe /* 445eb20796bSJens Axboe * We really should re-lookup the page here, 446eb20796bSJens Axboe * but it complicates things a lot. Instead 447eb20796bSJens Axboe * lets just do what we already stored, and 448eb20796bSJens Axboe * we'll get it the next time we are called. 449eb20796bSJens Axboe */ 4507480a904SJens Axboe if (error == AOP_TRUNCATED_PAGE) 451eb20796bSJens Axboe error = 0; 452eb20796bSJens Axboe 4537480a904SJens Axboe break; 4547480a904SJens Axboe } 455620a324bSJens Axboe } 456620a324bSJens Axboe fill_it: 45791ad66efSJens Axboe /* 458620a324bSJens Axboe * i_size must be checked after PageUptodate. 45991ad66efSJens Axboe */ 46091ad66efSJens Axboe isize = i_size_read(mapping->host); 46191ad66efSJens Axboe end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 462eb20796bSJens Axboe if (unlikely(!isize || index > end_index)) 46391ad66efSJens Axboe break; 46491ad66efSJens Axboe 46591ad66efSJens Axboe /* 46691ad66efSJens Axboe * if this is the last page, see if we need to shrink 46791ad66efSJens Axboe * the length and stop 46891ad66efSJens Axboe */ 46991ad66efSJens Axboe if (end_index == index) { 470475ecadeSHugh Dickins unsigned int plen; 471475ecadeSHugh Dickins 472475ecadeSHugh Dickins /* 473475ecadeSHugh Dickins * max good bytes in this page 474475ecadeSHugh Dickins */ 475475ecadeSHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 476475ecadeSHugh Dickins if (plen <= loff) 47791ad66efSJens Axboe break; 478475ecadeSHugh Dickins 47991ad66efSJens Axboe /* 48091ad66efSJens Axboe * force quit after adding this page 48191ad66efSJens Axboe */ 482475ecadeSHugh Dickins this_len = min(this_len, plen - loff); 483eb20796bSJens Axboe len = this_len; 48491ad66efSJens Axboe } 485620a324bSJens Axboe 48635f3d14dSJens Axboe spd.partial[page_nr].offset = loff; 48735f3d14dSJens Axboe spd.partial[page_nr].len = this_len; 48882aa5d61SJens Axboe len -= this_len; 48991ad66efSJens Axboe loff = 0; 490eb20796bSJens Axboe spd.nr_pages++; 491eb20796bSJens Axboe index++; 4925274f052SJens Axboe } 4935274f052SJens Axboe 494eb20796bSJens Axboe /* 495475ecadeSHugh Dickins * Release any pages at the end, if we quit early. 'page_nr' is how far 496eb20796bSJens Axboe * we got, 'nr_pages' is how many pages are in the map. 497eb20796bSJens Axboe */ 498eb20796bSJens Axboe while (page_nr < nr_pages) 49935f3d14dSJens Axboe page_cache_release(spd.pages[page_nr++]); 500f4e6b498SFengguang Wu in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT; 501eb20796bSJens Axboe 502912d35f8SJens Axboe if (spd.nr_pages) 50335f3d14dSJens Axboe error = splice_to_pipe(pipe, &spd); 50416c523ddSJens Axboe 505047fe360SEric Dumazet splice_shrink_spd(&spd); 5067480a904SJens Axboe return error; 5075274f052SJens Axboe } 5085274f052SJens Axboe 50983f9135bSJens Axboe /** 51083f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 51183f9135bSJens Axboe * @in: file to splice from 512932cc6d4SJens Axboe * @ppos: position in @in 51383f9135bSJens Axboe * @pipe: pipe to splice to 51483f9135bSJens Axboe * @len: number of bytes to splice 51583f9135bSJens Axboe * @flags: splice modifier flags 51683f9135bSJens Axboe * 517932cc6d4SJens Axboe * Description: 518932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 519932cc6d4SJens Axboe * used as long as the address_space operations for the source implements 520932cc6d4SJens Axboe * a readpage() hook. 521932cc6d4SJens Axboe * 52283f9135bSJens Axboe */ 523cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 524cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 525cbb7e577SJens Axboe unsigned int flags) 5265274f052SJens Axboe { 527d366d398SJens Axboe loff_t isize, left; 5288191ecd1SJens Axboe int ret; 529d366d398SJens Axboe 530d366d398SJens Axboe isize = i_size_read(in->f_mapping->host); 531d366d398SJens Axboe if (unlikely(*ppos >= isize)) 532d366d398SJens Axboe return 0; 533d366d398SJens Axboe 534d366d398SJens Axboe left = isize - *ppos; 535d366d398SJens Axboe if (unlikely(left < len)) 536d366d398SJens Axboe len = left; 5375274f052SJens Axboe 538cbb7e577SJens Axboe ret = __generic_file_splice_read(in, ppos, pipe, len, flags); 539723590edSMiklos Szeredi if (ret > 0) { 540cbb7e577SJens Axboe *ppos += ret; 541723590edSMiklos Szeredi file_accessed(in); 542723590edSMiklos Szeredi } 5435274f052SJens Axboe 5445274f052SJens Axboe return ret; 5455274f052SJens Axboe } 546059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 547059a8f37SJens Axboe 5486818173bSMiklos Szeredi static const struct pipe_buf_operations default_pipe_buf_ops = { 5496818173bSMiklos Szeredi .can_merge = 0, 5506818173bSMiklos Szeredi .map = generic_pipe_buf_map, 5516818173bSMiklos Szeredi .unmap = generic_pipe_buf_unmap, 5526818173bSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 5536818173bSMiklos Szeredi .release = generic_pipe_buf_release, 5546818173bSMiklos Szeredi .steal = generic_pipe_buf_steal, 5556818173bSMiklos Szeredi .get = generic_pipe_buf_get, 5566818173bSMiklos Szeredi }; 5576818173bSMiklos Szeredi 558*28a625cbSMiklos Szeredi static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe, 559*28a625cbSMiklos Szeredi struct pipe_buffer *buf) 560*28a625cbSMiklos Szeredi { 561*28a625cbSMiklos Szeredi return 1; 562*28a625cbSMiklos Szeredi } 563*28a625cbSMiklos Szeredi 564*28a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 565*28a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 566*28a625cbSMiklos Szeredi .can_merge = 0, 567*28a625cbSMiklos Szeredi .map = generic_pipe_buf_map, 568*28a625cbSMiklos Szeredi .unmap = generic_pipe_buf_unmap, 569*28a625cbSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 570*28a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 571*28a625cbSMiklos Szeredi .steal = generic_pipe_buf_nosteal, 572*28a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 573*28a625cbSMiklos Szeredi }; 574*28a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 575*28a625cbSMiklos Szeredi 5766818173bSMiklos Szeredi static ssize_t kernel_readv(struct file *file, const struct iovec *vec, 5776818173bSMiklos Szeredi unsigned long vlen, loff_t offset) 5786818173bSMiklos Szeredi { 5796818173bSMiklos Szeredi mm_segment_t old_fs; 5806818173bSMiklos Szeredi loff_t pos = offset; 5816818173bSMiklos Szeredi ssize_t res; 5826818173bSMiklos Szeredi 5836818173bSMiklos Szeredi old_fs = get_fs(); 5846818173bSMiklos Szeredi set_fs(get_ds()); 5856818173bSMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 5866818173bSMiklos Szeredi res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos); 5876818173bSMiklos Szeredi set_fs(old_fs); 5886818173bSMiklos Szeredi 5896818173bSMiklos Szeredi return res; 5906818173bSMiklos Szeredi } 5916818173bSMiklos Szeredi 5927bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count, 593b2858d7dSMiklos Szeredi loff_t pos) 5940b0a47f5SMiklos Szeredi { 5950b0a47f5SMiklos Szeredi mm_segment_t old_fs; 5960b0a47f5SMiklos Szeredi ssize_t res; 5970b0a47f5SMiklos Szeredi 5980b0a47f5SMiklos Szeredi old_fs = get_fs(); 5990b0a47f5SMiklos Szeredi set_fs(get_ds()); 6000b0a47f5SMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 6017bb307e8SAl Viro res = vfs_write(file, (__force const char __user *)buf, count, &pos); 6020b0a47f5SMiklos Szeredi set_fs(old_fs); 6030b0a47f5SMiklos Szeredi 6040b0a47f5SMiklos Szeredi return res; 6050b0a47f5SMiklos Szeredi } 6067bb307e8SAl Viro EXPORT_SYMBOL(kernel_write); 6070b0a47f5SMiklos Szeredi 6086818173bSMiklos Szeredi ssize_t default_file_splice_read(struct file *in, loff_t *ppos, 6096818173bSMiklos Szeredi struct pipe_inode_info *pipe, size_t len, 6106818173bSMiklos Szeredi unsigned int flags) 6116818173bSMiklos Szeredi { 6126818173bSMiklos Szeredi unsigned int nr_pages; 6136818173bSMiklos Szeredi unsigned int nr_freed; 6146818173bSMiklos Szeredi size_t offset; 61535f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 61635f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 61735f3d14dSJens Axboe struct iovec *vec, __vec[PIPE_DEF_BUFFERS]; 6186818173bSMiklos Szeredi ssize_t res; 6196818173bSMiklos Szeredi size_t this_len; 6206818173bSMiklos Szeredi int error; 6216818173bSMiklos Szeredi int i; 6226818173bSMiklos Szeredi struct splice_pipe_desc spd = { 6236818173bSMiklos Szeredi .pages = pages, 6246818173bSMiklos Szeredi .partial = partial, 625047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 6266818173bSMiklos Szeredi .flags = flags, 6276818173bSMiklos Szeredi .ops = &default_pipe_buf_ops, 6286818173bSMiklos Szeredi .spd_release = spd_release_page, 6296818173bSMiklos Szeredi }; 6306818173bSMiklos Szeredi 63135f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 63235f3d14dSJens Axboe return -ENOMEM; 63335f3d14dSJens Axboe 63435f3d14dSJens Axboe res = -ENOMEM; 63535f3d14dSJens Axboe vec = __vec; 636047fe360SEric Dumazet if (spd.nr_pages_max > PIPE_DEF_BUFFERS) { 637047fe360SEric Dumazet vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL); 63835f3d14dSJens Axboe if (!vec) 63935f3d14dSJens Axboe goto shrink_ret; 64035f3d14dSJens Axboe } 64135f3d14dSJens Axboe 6426818173bSMiklos Szeredi offset = *ppos & ~PAGE_CACHE_MASK; 6436818173bSMiklos Szeredi nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 6446818173bSMiklos Szeredi 645047fe360SEric Dumazet for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) { 6466818173bSMiklos Szeredi struct page *page; 6476818173bSMiklos Szeredi 6484f231228SJens Axboe page = alloc_page(GFP_USER); 6496818173bSMiklos Szeredi error = -ENOMEM; 6506818173bSMiklos Szeredi if (!page) 6516818173bSMiklos Szeredi goto err; 6526818173bSMiklos Szeredi 6536818173bSMiklos Szeredi this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset); 6544f231228SJens Axboe vec[i].iov_base = (void __user *) page_address(page); 6556818173bSMiklos Szeredi vec[i].iov_len = this_len; 65635f3d14dSJens Axboe spd.pages[i] = page; 6576818173bSMiklos Szeredi spd.nr_pages++; 6586818173bSMiklos Szeredi len -= this_len; 6596818173bSMiklos Szeredi offset = 0; 6606818173bSMiklos Szeredi } 6616818173bSMiklos Szeredi 6626818173bSMiklos Szeredi res = kernel_readv(in, vec, spd.nr_pages, *ppos); 66377f6bf57SAndrew Morton if (res < 0) { 66477f6bf57SAndrew Morton error = res; 6656818173bSMiklos Szeredi goto err; 66677f6bf57SAndrew Morton } 6676818173bSMiklos Szeredi 6686818173bSMiklos Szeredi error = 0; 6696818173bSMiklos Szeredi if (!res) 6706818173bSMiklos Szeredi goto err; 6716818173bSMiklos Szeredi 6726818173bSMiklos Szeredi nr_freed = 0; 6736818173bSMiklos Szeredi for (i = 0; i < spd.nr_pages; i++) { 6746818173bSMiklos Szeredi this_len = min_t(size_t, vec[i].iov_len, res); 67535f3d14dSJens Axboe spd.partial[i].offset = 0; 67635f3d14dSJens Axboe spd.partial[i].len = this_len; 6776818173bSMiklos Szeredi if (!this_len) { 67835f3d14dSJens Axboe __free_page(spd.pages[i]); 67935f3d14dSJens Axboe spd.pages[i] = NULL; 6806818173bSMiklos Szeredi nr_freed++; 6816818173bSMiklos Szeredi } 6826818173bSMiklos Szeredi res -= this_len; 6836818173bSMiklos Szeredi } 6846818173bSMiklos Szeredi spd.nr_pages -= nr_freed; 6856818173bSMiklos Szeredi 6866818173bSMiklos Szeredi res = splice_to_pipe(pipe, &spd); 6876818173bSMiklos Szeredi if (res > 0) 6886818173bSMiklos Szeredi *ppos += res; 6896818173bSMiklos Szeredi 69035f3d14dSJens Axboe shrink_ret: 69135f3d14dSJens Axboe if (vec != __vec) 69235f3d14dSJens Axboe kfree(vec); 693047fe360SEric Dumazet splice_shrink_spd(&spd); 6946818173bSMiklos Szeredi return res; 6956818173bSMiklos Szeredi 6966818173bSMiklos Szeredi err: 6974f231228SJens Axboe for (i = 0; i < spd.nr_pages; i++) 69835f3d14dSJens Axboe __free_page(spd.pages[i]); 6994f231228SJens Axboe 70035f3d14dSJens Axboe res = error; 70135f3d14dSJens Axboe goto shrink_ret; 7026818173bSMiklos Szeredi } 7036818173bSMiklos Szeredi EXPORT_SYMBOL(default_file_splice_read); 7046818173bSMiklos Szeredi 7055274f052SJens Axboe /* 7064f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 707016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 7085274f052SJens Axboe */ 70976ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 7105274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 7115274f052SJens Axboe { 7126a14b90bSJens Axboe struct file *file = sd->u.file; 7135274f052SJens Axboe loff_t pos = sd->pos; 714a8adbe37SMichał Mirosław int more; 7155274f052SJens Axboe 71672c2d531SAl Viro if (!likely(file->f_op->sendpage)) 717a8adbe37SMichał Mirosław return -EINVAL; 718a8adbe37SMichał Mirosław 71935f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 720ae62ca7bSEric Dumazet 721ae62ca7bSEric Dumazet if (sd->len < sd->total_len && pipe->nrbufs > 1) 72235f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 723ae62ca7bSEric Dumazet 724a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 725f84d7519SJens Axboe sd->len, &pos, more); 7265274f052SJens Axboe } 7275274f052SJens Axboe 7285274f052SJens Axboe /* 7295274f052SJens Axboe * This is a little more tricky than the file -> pipe splicing. There are 7305274f052SJens Axboe * basically three cases: 7315274f052SJens Axboe * 7325274f052SJens Axboe * - Destination page already exists in the address space and there 7335274f052SJens Axboe * are users of it. For that case we have no other option that 7345274f052SJens Axboe * copying the data. Tough luck. 7355274f052SJens Axboe * - Destination page already exists in the address space, but there 7365274f052SJens Axboe * are no users of it. Make sure it's uptodate, then drop it. Fall 7375274f052SJens Axboe * through to last case. 7385274f052SJens Axboe * - Destination page does not exist, we can add the pipe page to 7395274f052SJens Axboe * the page cache and avoid the copy. 7405274f052SJens Axboe * 74183f9135bSJens Axboe * If asked to move pages to the output file (SPLICE_F_MOVE is set in 74283f9135bSJens Axboe * sd->flags), we attempt to migrate pages from the pipe to the output 74383f9135bSJens Axboe * file address space page cache. This is possible if no one else has 74483f9135bSJens Axboe * the pipe page referenced outside of the pipe and page cache. If 74583f9135bSJens Axboe * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create 74683f9135bSJens Axboe * a new page in the output file page cache and fill/dirty that. 7475274f052SJens Axboe */ 748328eaabaSMiklos Szeredi int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 7495274f052SJens Axboe struct splice_desc *sd) 7505274f052SJens Axboe { 7516a14b90bSJens Axboe struct file *file = sd->u.file; 7525274f052SJens Axboe struct address_space *mapping = file->f_mapping; 753016b661eSJens Axboe unsigned int offset, this_len; 7545274f052SJens Axboe struct page *page; 755afddba49SNick Piggin void *fsdata; 7563e7ee3e7SJens Axboe int ret; 7575274f052SJens Axboe 7585274f052SJens Axboe offset = sd->pos & ~PAGE_CACHE_MASK; 7595274f052SJens Axboe 760016b661eSJens Axboe this_len = sd->len; 761016b661eSJens Axboe if (this_len + offset > PAGE_CACHE_SIZE) 762016b661eSJens Axboe this_len = PAGE_CACHE_SIZE - offset; 763016b661eSJens Axboe 764afddba49SNick Piggin ret = pagecache_write_begin(file, mapping, sd->pos, this_len, 765afddba49SNick Piggin AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 7669e0267c2SJens Axboe if (unlikely(ret)) 767afddba49SNick Piggin goto out; 7685274f052SJens Axboe 7690568b409SJens Axboe if (buf->page != page) { 77076ad4d11SJens Axboe char *src = buf->ops->map(pipe, buf, 1); 771e8e3c3d6SCong Wang char *dst = kmap_atomic(page); 7725abc97aaSJens Axboe 773016b661eSJens Axboe memcpy(dst + offset, src + buf->offset, this_len); 7745274f052SJens Axboe flush_dcache_page(page); 775e8e3c3d6SCong Wang kunmap_atomic(dst); 77676ad4d11SJens Axboe buf->ops->unmap(pipe, buf, src); 7775abc97aaSJens Axboe } 778afddba49SNick Piggin ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len, 779afddba49SNick Piggin page, fsdata); 7800568b409SJens Axboe out: 7815274f052SJens Axboe return ret; 7825274f052SJens Axboe } 783328eaabaSMiklos Szeredi EXPORT_SYMBOL(pipe_to_file); 7845274f052SJens Axboe 785b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 786b3c2d2ddSMiklos Szeredi { 787b3c2d2ddSMiklos Szeredi smp_mb(); 788b3c2d2ddSMiklos Szeredi if (waitqueue_active(&pipe->wait)) 789b3c2d2ddSMiklos Szeredi wake_up_interruptible(&pipe->wait); 790b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 791b3c2d2ddSMiklos Szeredi } 792b3c2d2ddSMiklos Szeredi 793b3c2d2ddSMiklos Szeredi /** 794b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 795b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 796b3c2d2ddSMiklos Szeredi * @sd: information to @actor 797b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 798b3c2d2ddSMiklos Szeredi * 799b3c2d2ddSMiklos Szeredi * Description: 800b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 801b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 802b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 803b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 804b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 805b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 806b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 807b3c2d2ddSMiklos Szeredi * 808b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 809b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 810b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 811b3c2d2ddSMiklos Szeredi * destination. 812b3c2d2ddSMiklos Szeredi */ 813b3c2d2ddSMiklos Szeredi int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 814b3c2d2ddSMiklos Szeredi splice_actor *actor) 815b3c2d2ddSMiklos Szeredi { 816b3c2d2ddSMiklos Szeredi int ret; 817b3c2d2ddSMiklos Szeredi 818b3c2d2ddSMiklos Szeredi while (pipe->nrbufs) { 819b3c2d2ddSMiklos Szeredi struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 820b3c2d2ddSMiklos Szeredi const struct pipe_buf_operations *ops = buf->ops; 821b3c2d2ddSMiklos Szeredi 822b3c2d2ddSMiklos Szeredi sd->len = buf->len; 823b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 824b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 825b3c2d2ddSMiklos Szeredi 826a8adbe37SMichał Mirosław ret = buf->ops->confirm(pipe, buf); 827a8adbe37SMichał Mirosław if (unlikely(ret)) { 828b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 829b3c2d2ddSMiklos Szeredi ret = 0; 830b3c2d2ddSMiklos Szeredi return ret; 831b3c2d2ddSMiklos Szeredi } 832a8adbe37SMichał Mirosław 833a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 834a8adbe37SMichał Mirosław if (ret <= 0) 835a8adbe37SMichał Mirosław return ret; 836a8adbe37SMichał Mirosław 837b3c2d2ddSMiklos Szeredi buf->offset += ret; 838b3c2d2ddSMiklos Szeredi buf->len -= ret; 839b3c2d2ddSMiklos Szeredi 840b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 841b3c2d2ddSMiklos Szeredi sd->len -= ret; 842b3c2d2ddSMiklos Szeredi sd->pos += ret; 843b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 844b3c2d2ddSMiklos Szeredi 845b3c2d2ddSMiklos Szeredi if (!buf->len) { 846b3c2d2ddSMiklos Szeredi buf->ops = NULL; 847b3c2d2ddSMiklos Szeredi ops->release(pipe, buf); 84835f3d14dSJens Axboe pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); 849b3c2d2ddSMiklos Szeredi pipe->nrbufs--; 8506447a3cfSAl Viro if (pipe->files) 851b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 852b3c2d2ddSMiklos Szeredi } 853b3c2d2ddSMiklos Szeredi 854b3c2d2ddSMiklos Szeredi if (!sd->total_len) 855b3c2d2ddSMiklos Szeredi return 0; 856b3c2d2ddSMiklos Szeredi } 857b3c2d2ddSMiklos Szeredi 858b3c2d2ddSMiklos Szeredi return 1; 859b3c2d2ddSMiklos Szeredi } 860b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_feed); 861b3c2d2ddSMiklos Szeredi 862b3c2d2ddSMiklos Szeredi /** 863b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 864b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 865b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 866b3c2d2ddSMiklos Szeredi * 867b3c2d2ddSMiklos Szeredi * Description: 868b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 869b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 870b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 871b3c2d2ddSMiklos Szeredi */ 872b3c2d2ddSMiklos Szeredi int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 873b3c2d2ddSMiklos Szeredi { 874b3c2d2ddSMiklos Szeredi while (!pipe->nrbufs) { 875b3c2d2ddSMiklos Szeredi if (!pipe->writers) 876b3c2d2ddSMiklos Szeredi return 0; 877b3c2d2ddSMiklos Szeredi 878b3c2d2ddSMiklos Szeredi if (!pipe->waiting_writers && sd->num_spliced) 879b3c2d2ddSMiklos Szeredi return 0; 880b3c2d2ddSMiklos Szeredi 881b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 882b3c2d2ddSMiklos Szeredi return -EAGAIN; 883b3c2d2ddSMiklos Szeredi 884b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 885b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 886b3c2d2ddSMiklos Szeredi 887b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 888b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 889b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 890b3c2d2ddSMiklos Szeredi } 891b3c2d2ddSMiklos Szeredi 892b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 893b3c2d2ddSMiklos Szeredi } 894b3c2d2ddSMiklos Szeredi 895b3c2d2ddSMiklos Szeredi return 1; 896b3c2d2ddSMiklos Szeredi } 897b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_next); 898b3c2d2ddSMiklos Szeredi 899b3c2d2ddSMiklos Szeredi /** 900b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 901b80901bbSRandy Dunlap * @sd: information about the splice operation 902b3c2d2ddSMiklos Szeredi * 903b3c2d2ddSMiklos Szeredi * Description: 904b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 905b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 906b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 907b3c2d2ddSMiklos Szeredi */ 908b3c2d2ddSMiklos Szeredi void splice_from_pipe_begin(struct splice_desc *sd) 909b3c2d2ddSMiklos Szeredi { 910b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 911b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 912b3c2d2ddSMiklos Szeredi } 913b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_begin); 914b3c2d2ddSMiklos Szeredi 915b3c2d2ddSMiklos Szeredi /** 916b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 917b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 918b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 919b3c2d2ddSMiklos Szeredi * 920b3c2d2ddSMiklos Szeredi * Description: 921b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 922b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 923b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 924b3c2d2ddSMiklos Szeredi */ 925b3c2d2ddSMiklos Szeredi void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 926b3c2d2ddSMiklos Szeredi { 927b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 928b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 929b3c2d2ddSMiklos Szeredi } 930b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_end); 931b3c2d2ddSMiklos Szeredi 932932cc6d4SJens Axboe /** 933932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 934932cc6d4SJens Axboe * @pipe: pipe to splice from 935932cc6d4SJens Axboe * @sd: information to @actor 936932cc6d4SJens Axboe * @actor: handler that splices the data 937932cc6d4SJens Axboe * 938932cc6d4SJens Axboe * Description: 939932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 940932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 941932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 942932cc6d4SJens Axboe * pipe_to_user. 943932cc6d4SJens Axboe * 94483f9135bSJens Axboe */ 945c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 946c66ab6faSJens Axboe splice_actor *actor) 9475274f052SJens Axboe { 948b3c2d2ddSMiklos Szeredi int ret; 9495274f052SJens Axboe 950b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 951b3c2d2ddSMiklos Szeredi do { 952b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 953b3c2d2ddSMiklos Szeredi if (ret > 0) 954b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 955b3c2d2ddSMiklos Szeredi } while (ret > 0); 956b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 9575274f052SJens Axboe 958b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 9595274f052SJens Axboe } 96040bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 9615274f052SJens Axboe 962932cc6d4SJens Axboe /** 963932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 964932cc6d4SJens Axboe * @pipe: pipe to splice from 965932cc6d4SJens Axboe * @out: file to splice to 966932cc6d4SJens Axboe * @ppos: position in @out 967932cc6d4SJens Axboe * @len: how many bytes to splice 968932cc6d4SJens Axboe * @flags: splice modifier flags 969932cc6d4SJens Axboe * @actor: handler that splices the data 970932cc6d4SJens Axboe * 971932cc6d4SJens Axboe * Description: 9722933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 973932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 974932cc6d4SJens Axboe * 975932cc6d4SJens Axboe */ 9766da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 9776da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 9786da61809SMark Fasheh splice_actor *actor) 9796da61809SMark Fasheh { 9806da61809SMark Fasheh ssize_t ret; 981c66ab6faSJens Axboe struct splice_desc sd = { 982c66ab6faSJens Axboe .total_len = len, 983c66ab6faSJens Axboe .flags = flags, 984c66ab6faSJens Axboe .pos = *ppos, 9856a14b90bSJens Axboe .u.file = out, 986c66ab6faSJens Axboe }; 9876da61809SMark Fasheh 98861e0d47cSMiklos Szeredi pipe_lock(pipe); 989c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 99061e0d47cSMiklos Szeredi pipe_unlock(pipe); 9916da61809SMark Fasheh 9926da61809SMark Fasheh return ret; 9936da61809SMark Fasheh } 9946da61809SMark Fasheh 9956da61809SMark Fasheh /** 99683f9135bSJens Axboe * generic_file_splice_write - splice data from a pipe to a file 9973a326a2cSIngo Molnar * @pipe: pipe info 99883f9135bSJens Axboe * @out: file to write to 999932cc6d4SJens Axboe * @ppos: position in @out 100083f9135bSJens Axboe * @len: number of bytes to splice 100183f9135bSJens Axboe * @flags: splice modifier flags 100283f9135bSJens Axboe * 1003932cc6d4SJens Axboe * Description: 100483f9135bSJens Axboe * Will either move or copy pages (determined by @flags options) from 100583f9135bSJens Axboe * the given pipe inode to the given file. 100683f9135bSJens Axboe * 100783f9135bSJens Axboe */ 10083a326a2cSIngo Molnar ssize_t 10093a326a2cSIngo Molnar generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 1010cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 10115274f052SJens Axboe { 10124f6f0bd2SJens Axboe struct address_space *mapping = out->f_mapping; 10138c34e2d6SJens Axboe struct inode *inode = mapping->host; 10147f3d4ee1SMiklos Szeredi struct splice_desc sd = { 10157f3d4ee1SMiklos Szeredi .total_len = len, 10167f3d4ee1SMiklos Szeredi .flags = flags, 10177f3d4ee1SMiklos Szeredi .pos = *ppos, 10187f3d4ee1SMiklos Szeredi .u.file = out, 10197f3d4ee1SMiklos Szeredi }; 10203a326a2cSIngo Molnar ssize_t ret; 10218c34e2d6SJens Axboe 102261e0d47cSMiklos Szeredi pipe_lock(pipe); 1023eb443e5aSMiklos Szeredi 1024eb443e5aSMiklos Szeredi splice_from_pipe_begin(&sd); 1025eb443e5aSMiklos Szeredi do { 1026eb443e5aSMiklos Szeredi ret = splice_from_pipe_next(pipe, &sd); 1027eb443e5aSMiklos Szeredi if (ret <= 0) 1028eb443e5aSMiklos Szeredi break; 1029eb443e5aSMiklos Szeredi 1030eb443e5aSMiklos Szeredi mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 1031eb443e5aSMiklos Szeredi ret = file_remove_suid(out); 1032723590edSMiklos Szeredi if (!ret) { 1033c3b2da31SJosef Bacik ret = file_update_time(out); 1034c3b2da31SJosef Bacik if (!ret) 1035c3b2da31SJosef Bacik ret = splice_from_pipe_feed(pipe, &sd, 1036c3b2da31SJosef Bacik pipe_to_file); 1037723590edSMiklos Szeredi } 1038eb443e5aSMiklos Szeredi mutex_unlock(&inode->i_mutex); 1039eb443e5aSMiklos Szeredi } while (ret > 0); 1040eb443e5aSMiklos Szeredi splice_from_pipe_end(pipe, &sd); 1041eb443e5aSMiklos Szeredi 104261e0d47cSMiklos Szeredi pipe_unlock(pipe); 1043eb443e5aSMiklos Szeredi 1044eb443e5aSMiklos Szeredi if (sd.num_spliced) 1045eb443e5aSMiklos Szeredi ret = sd.num_spliced; 1046eb443e5aSMiklos Szeredi 1047a4514ebdSJens Axboe if (ret > 0) { 10487f3d4ee1SMiklos Szeredi int err; 10497f3d4ee1SMiklos Szeredi 1050148f948bSJan Kara err = generic_write_sync(out, *ppos, ret); 10514f6f0bd2SJens Axboe if (err) 10524f6f0bd2SJens Axboe ret = err; 1053148f948bSJan Kara else 1054148f948bSJan Kara *ppos += ret; 1055d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(mapping); 1056a4514ebdSJens Axboe } 10574f6f0bd2SJens Axboe 10584f6f0bd2SJens Axboe return ret; 10595274f052SJens Axboe } 10605274f052SJens Axboe 1061059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_write); 1062059a8f37SJens Axboe 1063b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 1064b2858d7dSMiklos Szeredi struct splice_desc *sd) 10650b0a47f5SMiklos Szeredi { 1066b2858d7dSMiklos Szeredi int ret; 1067b2858d7dSMiklos Szeredi void *data; 106806ae43f3SAl Viro loff_t tmp = sd->pos; 1069b2858d7dSMiklos Szeredi 1070b2858d7dSMiklos Szeredi data = buf->ops->map(pipe, buf, 0); 107106ae43f3SAl Viro ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); 1072b2858d7dSMiklos Szeredi buf->ops->unmap(pipe, buf, data); 1073b2858d7dSMiklos Szeredi 1074b2858d7dSMiklos Szeredi return ret; 10750b0a47f5SMiklos Szeredi } 10760b0a47f5SMiklos Szeredi 10770b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, 10780b0a47f5SMiklos Szeredi struct file *out, loff_t *ppos, 10790b0a47f5SMiklos Szeredi size_t len, unsigned int flags) 10800b0a47f5SMiklos Szeredi { 1081b2858d7dSMiklos Szeredi ssize_t ret; 10820b0a47f5SMiklos Szeredi 1083b2858d7dSMiklos Szeredi ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); 1084b2858d7dSMiklos Szeredi if (ret > 0) 1085b2858d7dSMiklos Szeredi *ppos += ret; 10860b0a47f5SMiklos Szeredi 1087b2858d7dSMiklos Szeredi return ret; 10880b0a47f5SMiklos Szeredi } 10890b0a47f5SMiklos Szeredi 109083f9135bSJens Axboe /** 109183f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 1092932cc6d4SJens Axboe * @pipe: pipe to splice from 109383f9135bSJens Axboe * @out: socket to write to 1094932cc6d4SJens Axboe * @ppos: position in @out 109583f9135bSJens Axboe * @len: number of bytes to splice 109683f9135bSJens Axboe * @flags: splice modifier flags 109783f9135bSJens Axboe * 1098932cc6d4SJens Axboe * Description: 109983f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 110083f9135bSJens Axboe * is involved. 110183f9135bSJens Axboe * 110283f9135bSJens Axboe */ 11033a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 1104cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 11055274f052SJens Axboe { 110600522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 11075274f052SJens Axboe } 11085274f052SJens Axboe 1109059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 1110a0f06780SJeff Garzik 111183f9135bSJens Axboe /* 111283f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 111383f9135bSJens Axboe */ 11143a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 1115cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 11165274f052SJens Axboe { 11170b0a47f5SMiklos Szeredi ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 11180b0a47f5SMiklos Szeredi loff_t *, size_t, unsigned int); 11195274f052SJens Axboe 112072c2d531SAl Viro if (out->f_op->splice_write) 11210b0a47f5SMiklos Szeredi splice_write = out->f_op->splice_write; 1122cc56f7deSChangli Gao else 11230b0a47f5SMiklos Szeredi splice_write = default_file_splice_write; 11240b0a47f5SMiklos Szeredi 1125500368f7SAl Viro return splice_write(pipe, out, ppos, len, flags); 11265274f052SJens Axboe } 11275274f052SJens Axboe 112883f9135bSJens Axboe /* 112983f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 113083f9135bSJens Axboe */ 1131cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 1132cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 1133cbb7e577SJens Axboe unsigned int flags) 11345274f052SJens Axboe { 11356818173bSMiklos Szeredi ssize_t (*splice_read)(struct file *, loff_t *, 11366818173bSMiklos Szeredi struct pipe_inode_info *, size_t, unsigned int); 11375274f052SJens Axboe int ret; 11385274f052SJens Axboe 113949570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 11405274f052SJens Axboe return -EBADF; 11415274f052SJens Axboe 1142cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 11435274f052SJens Axboe if (unlikely(ret < 0)) 11445274f052SJens Axboe return ret; 11455274f052SJens Axboe 114672c2d531SAl Viro if (in->f_op->splice_read) 11476818173bSMiklos Szeredi splice_read = in->f_op->splice_read; 1148cc56f7deSChangli Gao else 11496818173bSMiklos Szeredi splice_read = default_file_splice_read; 11506818173bSMiklos Szeredi 11516818173bSMiklos Szeredi return splice_read(in, ppos, pipe, len, flags); 11525274f052SJens Axboe } 11535274f052SJens Axboe 1154932cc6d4SJens Axboe /** 1155932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 1156932cc6d4SJens Axboe * @in: file to splice from 1157932cc6d4SJens Axboe * @sd: actor information on where to splice to 1158932cc6d4SJens Axboe * @actor: handles the data splicing 1159932cc6d4SJens Axboe * 1160932cc6d4SJens Axboe * Description: 1161932cc6d4SJens Axboe * This is a special case helper to splice directly between two 1162932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 1163932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 1164932cc6d4SJens Axboe * that process. 1165932cc6d4SJens Axboe * 1166c66ab6faSJens Axboe */ 1167c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 1168c66ab6faSJens Axboe splice_direct_actor *actor) 1169b92ce558SJens Axboe { 1170b92ce558SJens Axboe struct pipe_inode_info *pipe; 1171b92ce558SJens Axboe long ret, bytes; 1172b92ce558SJens Axboe umode_t i_mode; 1173c66ab6faSJens Axboe size_t len; 1174c66ab6faSJens Axboe int i, flags; 1175b92ce558SJens Axboe 1176b92ce558SJens Axboe /* 1177b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 1178b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 1179b92ce558SJens Axboe * piped splicing for that! 1180b92ce558SJens Axboe */ 1181496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 1182b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 1183b92ce558SJens Axboe return -EINVAL; 1184b92ce558SJens Axboe 1185b92ce558SJens Axboe /* 1186b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 1187b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 1188b92ce558SJens Axboe */ 1189b92ce558SJens Axboe pipe = current->splice_pipe; 119049570e9bSJens Axboe if (unlikely(!pipe)) { 11917bee130eSAl Viro pipe = alloc_pipe_info(); 1192b92ce558SJens Axboe if (!pipe) 1193b92ce558SJens Axboe return -ENOMEM; 1194b92ce558SJens Axboe 1195b92ce558SJens Axboe /* 1196b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 119700522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 1198b92ce558SJens Axboe * PIPE_READERS appropriately. 1199b92ce558SJens Axboe */ 1200b92ce558SJens Axboe pipe->readers = 1; 1201b92ce558SJens Axboe 1202b92ce558SJens Axboe current->splice_pipe = pipe; 1203b92ce558SJens Axboe } 1204b92ce558SJens Axboe 1205b92ce558SJens Axboe /* 120673d62d83SIngo Molnar * Do the splice. 1207b92ce558SJens Axboe */ 1208b92ce558SJens Axboe ret = 0; 1209b92ce558SJens Axboe bytes = 0; 1210c66ab6faSJens Axboe len = sd->total_len; 1211c66ab6faSJens Axboe flags = sd->flags; 1212c66ab6faSJens Axboe 1213c66ab6faSJens Axboe /* 1214c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 1215c66ab6faSJens Axboe */ 1216c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 1217b92ce558SJens Axboe 1218b92ce558SJens Axboe while (len) { 121951a92c0fSJens Axboe size_t read_len; 1220a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 1221b92ce558SJens Axboe 1222bcd4f3acSJens Axboe ret = do_splice_to(in, &pos, pipe, len, flags); 122351a92c0fSJens Axboe if (unlikely(ret <= 0)) 1224b92ce558SJens Axboe goto out_release; 1225b92ce558SJens Axboe 1226b92ce558SJens Axboe read_len = ret; 1227c66ab6faSJens Axboe sd->total_len = read_len; 1228b92ce558SJens Axboe 1229b92ce558SJens Axboe /* 1230b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 1231b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 1232b92ce558SJens Axboe * could get stuck data in the internal pipe: 1233b92ce558SJens Axboe */ 1234c66ab6faSJens Axboe ret = actor(pipe, sd); 1235a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 1236a82c53a0STom Zanussi sd->pos = prev_pos; 1237b92ce558SJens Axboe goto out_release; 1238a82c53a0STom Zanussi } 1239b92ce558SJens Axboe 1240b92ce558SJens Axboe bytes += ret; 1241b92ce558SJens Axboe len -= ret; 1242bcd4f3acSJens Axboe sd->pos = pos; 1243b92ce558SJens Axboe 1244a82c53a0STom Zanussi if (ret < read_len) { 1245a82c53a0STom Zanussi sd->pos = prev_pos + ret; 124651a92c0fSJens Axboe goto out_release; 1247b92ce558SJens Axboe } 1248a82c53a0STom Zanussi } 1249b92ce558SJens Axboe 12509e97198dSJens Axboe done: 1251b92ce558SJens Axboe pipe->nrbufs = pipe->curbuf = 0; 12529e97198dSJens Axboe file_accessed(in); 1253b92ce558SJens Axboe return bytes; 1254b92ce558SJens Axboe 1255b92ce558SJens Axboe out_release: 1256b92ce558SJens Axboe /* 1257b92ce558SJens Axboe * If we did an incomplete transfer we must release 1258b92ce558SJens Axboe * the pipe buffers in question: 1259b92ce558SJens Axboe */ 126035f3d14dSJens Axboe for (i = 0; i < pipe->buffers; i++) { 1261b92ce558SJens Axboe struct pipe_buffer *buf = pipe->bufs + i; 1262b92ce558SJens Axboe 1263b92ce558SJens Axboe if (buf->ops) { 1264b92ce558SJens Axboe buf->ops->release(pipe, buf); 1265b92ce558SJens Axboe buf->ops = NULL; 1266b92ce558SJens Axboe } 1267b92ce558SJens Axboe } 1268b92ce558SJens Axboe 12699e97198dSJens Axboe if (!bytes) 12709e97198dSJens Axboe bytes = ret; 1271b92ce558SJens Axboe 12729e97198dSJens Axboe goto done; 1273c66ab6faSJens Axboe } 1274c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1275c66ab6faSJens Axboe 1276c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1277c66ab6faSJens Axboe struct splice_desc *sd) 1278c66ab6faSJens Axboe { 12796a14b90bSJens Axboe struct file *file = sd->u.file; 1280c66ab6faSJens Axboe 12817995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 12822cb4b05eSChangli Gao sd->flags); 1283c66ab6faSJens Axboe } 1284c66ab6faSJens Axboe 1285932cc6d4SJens Axboe /** 1286932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1287932cc6d4SJens Axboe * @in: file to splice from 1288932cc6d4SJens Axboe * @ppos: input file offset 1289932cc6d4SJens Axboe * @out: file to splice to 1290acdb37c3SRandy Dunlap * @opos: output file offset 1291932cc6d4SJens Axboe * @len: number of bytes to splice 1292932cc6d4SJens Axboe * @flags: splice modifier flags 1293932cc6d4SJens Axboe * 1294932cc6d4SJens Axboe * Description: 1295932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1296932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1297932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1298932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1299932cc6d4SJens Axboe * 1300932cc6d4SJens Axboe */ 1301c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 13027995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 1303c66ab6faSJens Axboe { 1304c66ab6faSJens Axboe struct splice_desc sd = { 1305c66ab6faSJens Axboe .len = len, 1306c66ab6faSJens Axboe .total_len = len, 1307c66ab6faSJens Axboe .flags = flags, 1308c66ab6faSJens Axboe .pos = *ppos, 13096a14b90bSJens Axboe .u.file = out, 13107995bd28SAl Viro .opos = opos, 1311c66ab6faSJens Axboe }; 131251a92c0fSJens Axboe long ret; 1313c66ab6faSJens Axboe 131418c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 131518c67cb9SAl Viro return -EBADF; 131618c67cb9SAl Viro 131718c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 131818c67cb9SAl Viro return -EINVAL; 131918c67cb9SAl Viro 132018c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 132118c67cb9SAl Viro if (unlikely(ret < 0)) 132218c67cb9SAl Viro return ret; 132318c67cb9SAl Viro 1324c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 132551a92c0fSJens Axboe if (ret > 0) 1326a82c53a0STom Zanussi *ppos = sd.pos; 132751a92c0fSJens Axboe 1328c66ab6faSJens Axboe return ret; 1329b92ce558SJens Axboe } 1330b92ce558SJens Axboe 13317c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 13327c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 13337c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1334ddac0d39SJens Axboe 1335ddac0d39SJens Axboe /* 133683f9135bSJens Axboe * Determine where to splice to/from. 133783f9135bSJens Axboe */ 1338529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in, 1339529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 1340529565dcSIngo Molnar size_t len, unsigned int flags) 13415274f052SJens Axboe { 13427c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 13437c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 13447995bd28SAl Viro loff_t offset; 1345a4514ebdSJens Axboe long ret; 13465274f052SJens Axboe 134771993e62SLinus Torvalds ipipe = get_pipe_info(in); 134871993e62SLinus Torvalds opipe = get_pipe_info(out); 13497c77f0b3SMiklos Szeredi 13507c77f0b3SMiklos Szeredi if (ipipe && opipe) { 13517c77f0b3SMiklos Szeredi if (off_in || off_out) 13527c77f0b3SMiklos Szeredi return -ESPIPE; 13537c77f0b3SMiklos Szeredi 13547c77f0b3SMiklos Szeredi if (!(in->f_mode & FMODE_READ)) 13557c77f0b3SMiklos Szeredi return -EBADF; 13567c77f0b3SMiklos Szeredi 13577c77f0b3SMiklos Szeredi if (!(out->f_mode & FMODE_WRITE)) 13587c77f0b3SMiklos Szeredi return -EBADF; 13597c77f0b3SMiklos Szeredi 13607c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 13617c77f0b3SMiklos Szeredi if (ipipe == opipe) 13627c77f0b3SMiklos Szeredi return -EINVAL; 13637c77f0b3SMiklos Szeredi 13647c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 13657c77f0b3SMiklos Szeredi } 13667c77f0b3SMiklos Szeredi 13677c77f0b3SMiklos Szeredi if (ipipe) { 1368529565dcSIngo Molnar if (off_in) 1369529565dcSIngo Molnar return -ESPIPE; 1370b92ce558SJens Axboe if (off_out) { 137119c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1372b92ce558SJens Axboe return -EINVAL; 1373cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1374b92ce558SJens Axboe return -EFAULT; 13757995bd28SAl Viro } else { 13767995bd28SAl Viro offset = out->f_pos; 13777995bd28SAl Viro } 1378529565dcSIngo Molnar 137918c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 138018c67cb9SAl Viro return -EBADF; 138118c67cb9SAl Viro 138218c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 138318c67cb9SAl Viro return -EINVAL; 138418c67cb9SAl Viro 138518c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 138618c67cb9SAl Viro if (unlikely(ret < 0)) 138718c67cb9SAl Viro return ret; 138818c67cb9SAl Viro 1389500368f7SAl Viro file_start_write(out); 13907995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1391500368f7SAl Viro file_end_write(out); 1392a4514ebdSJens Axboe 13937995bd28SAl Viro if (!off_out) 13947995bd28SAl Viro out->f_pos = offset; 13957995bd28SAl Viro else if (copy_to_user(off_out, &offset, sizeof(loff_t))) 1396a4514ebdSJens Axboe ret = -EFAULT; 1397a4514ebdSJens Axboe 1398a4514ebdSJens Axboe return ret; 1399529565dcSIngo Molnar } 14005274f052SJens Axboe 14017c77f0b3SMiklos Szeredi if (opipe) { 1402529565dcSIngo Molnar if (off_out) 1403529565dcSIngo Molnar return -ESPIPE; 1404b92ce558SJens Axboe if (off_in) { 140519c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1406b92ce558SJens Axboe return -EINVAL; 1407cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1408b92ce558SJens Axboe return -EFAULT; 14097995bd28SAl Viro } else { 14107995bd28SAl Viro offset = in->f_pos; 14117995bd28SAl Viro } 1412529565dcSIngo Molnar 14137995bd28SAl Viro ret = do_splice_to(in, &offset, opipe, len, flags); 1414a4514ebdSJens Axboe 14157995bd28SAl Viro if (!off_in) 14167995bd28SAl Viro in->f_pos = offset; 14177995bd28SAl Viro else if (copy_to_user(off_in, &offset, sizeof(loff_t))) 1418a4514ebdSJens Axboe ret = -EFAULT; 1419a4514ebdSJens Axboe 1420a4514ebdSJens Axboe return ret; 1421529565dcSIngo Molnar } 14225274f052SJens Axboe 14235274f052SJens Axboe return -EINVAL; 14245274f052SJens Axboe } 14255274f052SJens Axboe 1426912d35f8SJens Axboe /* 1427912d35f8SJens Axboe * Map an iov into an array of pages and offset/length tupples. With the 1428912d35f8SJens Axboe * partial_page structure, we can map several non-contiguous ranges into 1429912d35f8SJens Axboe * our ones pages[] map instead of splitting that operation into pieces. 1430912d35f8SJens Axboe * Could easily be exported as a generic helper for other users, in which 1431912d35f8SJens Axboe * case one would probably want to add a 'max_nr_pages' parameter as well. 1432912d35f8SJens Axboe */ 1433912d35f8SJens Axboe static int get_iovec_page_array(const struct iovec __user *iov, 1434912d35f8SJens Axboe unsigned int nr_vecs, struct page **pages, 1435bd1a68b5SEric Dumazet struct partial_page *partial, bool aligned, 143635f3d14dSJens Axboe unsigned int pipe_buffers) 1437912d35f8SJens Axboe { 1438912d35f8SJens Axboe int buffers = 0, error = 0; 1439912d35f8SJens Axboe 1440912d35f8SJens Axboe while (nr_vecs) { 1441912d35f8SJens Axboe unsigned long off, npages; 144275723957SLinus Torvalds struct iovec entry; 1443912d35f8SJens Axboe void __user *base; 1444912d35f8SJens Axboe size_t len; 1445912d35f8SJens Axboe int i; 1446912d35f8SJens Axboe 144775723957SLinus Torvalds error = -EFAULT; 1448bc40d73cSNick Piggin if (copy_from_user(&entry, iov, sizeof(entry))) 1449912d35f8SJens Axboe break; 145075723957SLinus Torvalds 145175723957SLinus Torvalds base = entry.iov_base; 145275723957SLinus Torvalds len = entry.iov_len; 1453912d35f8SJens Axboe 1454912d35f8SJens Axboe /* 1455912d35f8SJens Axboe * Sanity check this iovec. 0 read succeeds. 1456912d35f8SJens Axboe */ 145775723957SLinus Torvalds error = 0; 1458912d35f8SJens Axboe if (unlikely(!len)) 1459912d35f8SJens Axboe break; 1460912d35f8SJens Axboe error = -EFAULT; 1461712a30e6SBastian Blank if (!access_ok(VERIFY_READ, base, len)) 1462912d35f8SJens Axboe break; 1463912d35f8SJens Axboe 1464912d35f8SJens Axboe /* 1465912d35f8SJens Axboe * Get this base offset and number of pages, then map 1466912d35f8SJens Axboe * in the user pages. 1467912d35f8SJens Axboe */ 1468912d35f8SJens Axboe off = (unsigned long) base & ~PAGE_MASK; 14697afa6fd0SJens Axboe 14707afa6fd0SJens Axboe /* 14717afa6fd0SJens Axboe * If asked for alignment, the offset must be zero and the 14727afa6fd0SJens Axboe * length a multiple of the PAGE_SIZE. 14737afa6fd0SJens Axboe */ 14747afa6fd0SJens Axboe error = -EINVAL; 14757afa6fd0SJens Axboe if (aligned && (off || len & ~PAGE_MASK)) 14767afa6fd0SJens Axboe break; 14777afa6fd0SJens Axboe 1478912d35f8SJens Axboe npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 147935f3d14dSJens Axboe if (npages > pipe_buffers - buffers) 148035f3d14dSJens Axboe npages = pipe_buffers - buffers; 1481912d35f8SJens Axboe 1482bc40d73cSNick Piggin error = get_user_pages_fast((unsigned long)base, npages, 1483bc40d73cSNick Piggin 0, &pages[buffers]); 1484912d35f8SJens Axboe 1485912d35f8SJens Axboe if (unlikely(error <= 0)) 1486912d35f8SJens Axboe break; 1487912d35f8SJens Axboe 1488912d35f8SJens Axboe /* 1489912d35f8SJens Axboe * Fill this contiguous range into the partial page map. 1490912d35f8SJens Axboe */ 1491912d35f8SJens Axboe for (i = 0; i < error; i++) { 14927591489aSJens Axboe const int plen = min_t(size_t, len, PAGE_SIZE - off); 1493912d35f8SJens Axboe 1494912d35f8SJens Axboe partial[buffers].offset = off; 1495912d35f8SJens Axboe partial[buffers].len = plen; 1496912d35f8SJens Axboe 1497912d35f8SJens Axboe off = 0; 1498912d35f8SJens Axboe len -= plen; 1499912d35f8SJens Axboe buffers++; 1500912d35f8SJens Axboe } 1501912d35f8SJens Axboe 1502912d35f8SJens Axboe /* 1503912d35f8SJens Axboe * We didn't complete this iov, stop here since it probably 1504912d35f8SJens Axboe * means we have to move some of this into a pipe to 1505912d35f8SJens Axboe * be able to continue. 1506912d35f8SJens Axboe */ 1507912d35f8SJens Axboe if (len) 1508912d35f8SJens Axboe break; 1509912d35f8SJens Axboe 1510912d35f8SJens Axboe /* 1511912d35f8SJens Axboe * Don't continue if we mapped fewer pages than we asked for, 1512912d35f8SJens Axboe * or if we mapped the max number of pages that we have 1513912d35f8SJens Axboe * room for. 1514912d35f8SJens Axboe */ 151535f3d14dSJens Axboe if (error < npages || buffers == pipe_buffers) 1516912d35f8SJens Axboe break; 1517912d35f8SJens Axboe 1518912d35f8SJens Axboe nr_vecs--; 1519912d35f8SJens Axboe iov++; 1520912d35f8SJens Axboe } 1521912d35f8SJens Axboe 1522912d35f8SJens Axboe if (buffers) 1523912d35f8SJens Axboe return buffers; 1524912d35f8SJens Axboe 1525912d35f8SJens Axboe return error; 1526912d35f8SJens Axboe } 1527912d35f8SJens Axboe 15286a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 15296a14b90bSJens Axboe struct splice_desc *sd) 15306a14b90bSJens Axboe { 15316a14b90bSJens Axboe char *src; 15326a14b90bSJens Axboe int ret; 15336a14b90bSJens Axboe 15346a14b90bSJens Axboe /* 15356a14b90bSJens Axboe * See if we can use the atomic maps, by prefaulting in the 15366a14b90bSJens Axboe * pages and doing an atomic copy 15376a14b90bSJens Axboe */ 15386a14b90bSJens Axboe if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) { 15396a14b90bSJens Axboe src = buf->ops->map(pipe, buf, 1); 15406a14b90bSJens Axboe ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset, 15416a14b90bSJens Axboe sd->len); 15426a14b90bSJens Axboe buf->ops->unmap(pipe, buf, src); 15436a14b90bSJens Axboe if (!ret) { 15446a14b90bSJens Axboe ret = sd->len; 15456a14b90bSJens Axboe goto out; 15466a14b90bSJens Axboe } 15476a14b90bSJens Axboe } 15486a14b90bSJens Axboe 15496a14b90bSJens Axboe /* 15506a14b90bSJens Axboe * No dice, use slow non-atomic map and copy 15516a14b90bSJens Axboe */ 15526a14b90bSJens Axboe src = buf->ops->map(pipe, buf, 0); 15536a14b90bSJens Axboe 15546a14b90bSJens Axboe ret = sd->len; 15556a14b90bSJens Axboe if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len)) 15566a14b90bSJens Axboe ret = -EFAULT; 15576a14b90bSJens Axboe 15586866bef4SJens Axboe buf->ops->unmap(pipe, buf, src); 15596a14b90bSJens Axboe out: 15606a14b90bSJens Axboe if (ret > 0) 15616a14b90bSJens Axboe sd->u.userptr += ret; 15626a14b90bSJens Axboe return ret; 15636a14b90bSJens Axboe } 15646a14b90bSJens Axboe 15656a14b90bSJens Axboe /* 15666a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 15676a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 15686a14b90bSJens Axboe */ 15696a14b90bSJens Axboe static long vmsplice_to_user(struct file *file, const struct iovec __user *iov, 15706a14b90bSJens Axboe unsigned long nr_segs, unsigned int flags) 15716a14b90bSJens Axboe { 15726a14b90bSJens Axboe struct pipe_inode_info *pipe; 15736a14b90bSJens Axboe struct splice_desc sd; 15746a14b90bSJens Axboe ssize_t size; 15756a14b90bSJens Axboe int error; 15766a14b90bSJens Axboe long ret; 15776a14b90bSJens Axboe 157871993e62SLinus Torvalds pipe = get_pipe_info(file); 15796a14b90bSJens Axboe if (!pipe) 15806a14b90bSJens Axboe return -EBADF; 15816a14b90bSJens Axboe 158261e0d47cSMiklos Szeredi pipe_lock(pipe); 15836a14b90bSJens Axboe 15846a14b90bSJens Axboe error = ret = 0; 15856a14b90bSJens Axboe while (nr_segs) { 15866a14b90bSJens Axboe void __user *base; 15876a14b90bSJens Axboe size_t len; 15886a14b90bSJens Axboe 15896a14b90bSJens Axboe /* 15906a14b90bSJens Axboe * Get user address base and length for this iovec. 15916a14b90bSJens Axboe */ 15926a14b90bSJens Axboe error = get_user(base, &iov->iov_base); 15936a14b90bSJens Axboe if (unlikely(error)) 15946a14b90bSJens Axboe break; 15956a14b90bSJens Axboe error = get_user(len, &iov->iov_len); 15966a14b90bSJens Axboe if (unlikely(error)) 15976a14b90bSJens Axboe break; 15986a14b90bSJens Axboe 15996a14b90bSJens Axboe /* 16006a14b90bSJens Axboe * Sanity check this iovec. 0 read succeeds. 16016a14b90bSJens Axboe */ 16026a14b90bSJens Axboe if (unlikely(!len)) 16036a14b90bSJens Axboe break; 16046a14b90bSJens Axboe if (unlikely(!base)) { 16056a14b90bSJens Axboe error = -EFAULT; 16066a14b90bSJens Axboe break; 16076a14b90bSJens Axboe } 16086a14b90bSJens Axboe 16098811930dSJens Axboe if (unlikely(!access_ok(VERIFY_WRITE, base, len))) { 16108811930dSJens Axboe error = -EFAULT; 16118811930dSJens Axboe break; 16128811930dSJens Axboe } 16138811930dSJens Axboe 16146a14b90bSJens Axboe sd.len = 0; 16156a14b90bSJens Axboe sd.total_len = len; 16166a14b90bSJens Axboe sd.flags = flags; 16176a14b90bSJens Axboe sd.u.userptr = base; 16186a14b90bSJens Axboe sd.pos = 0; 16196a14b90bSJens Axboe 16206a14b90bSJens Axboe size = __splice_from_pipe(pipe, &sd, pipe_to_user); 16216a14b90bSJens Axboe if (size < 0) { 16226a14b90bSJens Axboe if (!ret) 16236a14b90bSJens Axboe ret = size; 16246a14b90bSJens Axboe 16256a14b90bSJens Axboe break; 16266a14b90bSJens Axboe } 16276a14b90bSJens Axboe 16286a14b90bSJens Axboe ret += size; 16296a14b90bSJens Axboe 16306a14b90bSJens Axboe if (size < len) 16316a14b90bSJens Axboe break; 16326a14b90bSJens Axboe 16336a14b90bSJens Axboe nr_segs--; 16346a14b90bSJens Axboe iov++; 16356a14b90bSJens Axboe } 16366a14b90bSJens Axboe 163761e0d47cSMiklos Szeredi pipe_unlock(pipe); 16386a14b90bSJens Axboe 16396a14b90bSJens Axboe if (!ret) 16406a14b90bSJens Axboe ret = error; 16416a14b90bSJens Axboe 16426a14b90bSJens Axboe return ret; 16436a14b90bSJens Axboe } 16446a14b90bSJens Axboe 1645912d35f8SJens Axboe /* 1646912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1647912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1648912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1649912d35f8SJens Axboe */ 16506a14b90bSJens Axboe static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov, 1651912d35f8SJens Axboe unsigned long nr_segs, unsigned int flags) 1652912d35f8SJens Axboe { 1653ddac0d39SJens Axboe struct pipe_inode_info *pipe; 165435f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 165535f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 1656912d35f8SJens Axboe struct splice_pipe_desc spd = { 1657912d35f8SJens Axboe .pages = pages, 1658912d35f8SJens Axboe .partial = partial, 1659047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1660912d35f8SJens Axboe .flags = flags, 1661912d35f8SJens Axboe .ops = &user_page_pipe_buf_ops, 1662bbdfc2f7SJens Axboe .spd_release = spd_release_page, 1663912d35f8SJens Axboe }; 166435f3d14dSJens Axboe long ret; 1665912d35f8SJens Axboe 166671993e62SLinus Torvalds pipe = get_pipe_info(file); 1667ddac0d39SJens Axboe if (!pipe) 1668912d35f8SJens Axboe return -EBADF; 1669912d35f8SJens Axboe 167035f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 167135f3d14dSJens Axboe return -ENOMEM; 1672912d35f8SJens Axboe 167335f3d14dSJens Axboe spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages, 1674bd1a68b5SEric Dumazet spd.partial, false, 1675047fe360SEric Dumazet spd.nr_pages_max); 167635f3d14dSJens Axboe if (spd.nr_pages <= 0) 167735f3d14dSJens Axboe ret = spd.nr_pages; 167835f3d14dSJens Axboe else 167935f3d14dSJens Axboe ret = splice_to_pipe(pipe, &spd); 168035f3d14dSJens Axboe 1681047fe360SEric Dumazet splice_shrink_spd(&spd); 168235f3d14dSJens Axboe return ret; 1683912d35f8SJens Axboe } 1684912d35f8SJens Axboe 16856a14b90bSJens Axboe /* 16866a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 16876a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 16886a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 16896a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 16906a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 16916a14b90bSJens Axboe * solutions for that: 16926a14b90bSJens Axboe * 16936a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 16946a14b90bSJens Axboe * do a regular read() on the buffer anyway. 16956a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 16966a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 16976a14b90bSJens Axboe * 16986a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 16996a14b90bSJens Axboe * 17006a14b90bSJens Axboe */ 1701836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, 1702836f92adSHeiko Carstens unsigned long, nr_segs, unsigned int, flags) 1703912d35f8SJens Axboe { 17042903ff01SAl Viro struct fd f; 1705912d35f8SJens Axboe long error; 1706912d35f8SJens Axboe 17076a14b90bSJens Axboe if (unlikely(nr_segs > UIO_MAXIOV)) 17086a14b90bSJens Axboe return -EINVAL; 17096a14b90bSJens Axboe else if (unlikely(!nr_segs)) 17106a14b90bSJens Axboe return 0; 17116a14b90bSJens Axboe 1712912d35f8SJens Axboe error = -EBADF; 17132903ff01SAl Viro f = fdget(fd); 17142903ff01SAl Viro if (f.file) { 17152903ff01SAl Viro if (f.file->f_mode & FMODE_WRITE) 17162903ff01SAl Viro error = vmsplice_to_pipe(f.file, iov, nr_segs, flags); 17172903ff01SAl Viro else if (f.file->f_mode & FMODE_READ) 17182903ff01SAl Viro error = vmsplice_to_user(f.file, iov, nr_segs, flags); 1719912d35f8SJens Axboe 17202903ff01SAl Viro fdput(f); 1721912d35f8SJens Axboe } 1722912d35f8SJens Axboe 1723912d35f8SJens Axboe return error; 1724912d35f8SJens Axboe } 1725912d35f8SJens Axboe 172676b021d0SAl Viro #ifdef CONFIG_COMPAT 172776b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32, 172876b021d0SAl Viro unsigned int, nr_segs, unsigned int, flags) 172976b021d0SAl Viro { 173076b021d0SAl Viro unsigned i; 173176b021d0SAl Viro struct iovec __user *iov; 173276b021d0SAl Viro if (nr_segs > UIO_MAXIOV) 173376b021d0SAl Viro return -EINVAL; 173476b021d0SAl Viro iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec)); 173576b021d0SAl Viro for (i = 0; i < nr_segs; i++) { 173676b021d0SAl Viro struct compat_iovec v; 173776b021d0SAl Viro if (get_user(v.iov_base, &iov32[i].iov_base) || 173876b021d0SAl Viro get_user(v.iov_len, &iov32[i].iov_len) || 173976b021d0SAl Viro put_user(compat_ptr(v.iov_base), &iov[i].iov_base) || 174076b021d0SAl Viro put_user(v.iov_len, &iov[i].iov_len)) 174176b021d0SAl Viro return -EFAULT; 174276b021d0SAl Viro } 174376b021d0SAl Viro return sys_vmsplice(fd, iov, nr_segs, flags); 174476b021d0SAl Viro } 174576b021d0SAl Viro #endif 174676b021d0SAl Viro 1747836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1748836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1749836f92adSHeiko Carstens size_t, len, unsigned int, flags) 17505274f052SJens Axboe { 17512903ff01SAl Viro struct fd in, out; 17525274f052SJens Axboe long error; 17535274f052SJens Axboe 17545274f052SJens Axboe if (unlikely(!len)) 17555274f052SJens Axboe return 0; 17565274f052SJens Axboe 17575274f052SJens Axboe error = -EBADF; 17582903ff01SAl Viro in = fdget(fd_in); 17592903ff01SAl Viro if (in.file) { 17602903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 17612903ff01SAl Viro out = fdget(fd_out); 17622903ff01SAl Viro if (out.file) { 17632903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 17642903ff01SAl Viro error = do_splice(in.file, off_in, 17652903ff01SAl Viro out.file, off_out, 1766529565dcSIngo Molnar len, flags); 17672903ff01SAl Viro fdput(out); 17685274f052SJens Axboe } 17695274f052SJens Axboe } 17702903ff01SAl Viro fdput(in); 17715274f052SJens Axboe } 17725274f052SJens Axboe return error; 17735274f052SJens Axboe } 177470524490SJens Axboe 177570524490SJens Axboe /* 1776aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1777aadd06e5SJens Axboe * return an appropriate error. 1778aadd06e5SJens Axboe */ 17797c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1780aadd06e5SJens Axboe { 1781aadd06e5SJens Axboe int ret; 1782aadd06e5SJens Axboe 1783aadd06e5SJens Axboe /* 1784aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1785aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1786aadd06e5SJens Axboe */ 1787aadd06e5SJens Axboe if (pipe->nrbufs) 1788aadd06e5SJens Axboe return 0; 1789aadd06e5SJens Axboe 1790aadd06e5SJens Axboe ret = 0; 179161e0d47cSMiklos Szeredi pipe_lock(pipe); 1792aadd06e5SJens Axboe 1793aadd06e5SJens Axboe while (!pipe->nrbufs) { 1794aadd06e5SJens Axboe if (signal_pending(current)) { 1795aadd06e5SJens Axboe ret = -ERESTARTSYS; 1796aadd06e5SJens Axboe break; 1797aadd06e5SJens Axboe } 1798aadd06e5SJens Axboe if (!pipe->writers) 1799aadd06e5SJens Axboe break; 1800aadd06e5SJens Axboe if (!pipe->waiting_writers) { 1801aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1802aadd06e5SJens Axboe ret = -EAGAIN; 1803aadd06e5SJens Axboe break; 1804aadd06e5SJens Axboe } 1805aadd06e5SJens Axboe } 1806aadd06e5SJens Axboe pipe_wait(pipe); 1807aadd06e5SJens Axboe } 1808aadd06e5SJens Axboe 180961e0d47cSMiklos Szeredi pipe_unlock(pipe); 1810aadd06e5SJens Axboe return ret; 1811aadd06e5SJens Axboe } 1812aadd06e5SJens Axboe 1813aadd06e5SJens Axboe /* 1814aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1815aadd06e5SJens Axboe * return an appropriate error. 1816aadd06e5SJens Axboe */ 18177c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1818aadd06e5SJens Axboe { 1819aadd06e5SJens Axboe int ret; 1820aadd06e5SJens Axboe 1821aadd06e5SJens Axboe /* 1822aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1823aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1824aadd06e5SJens Axboe */ 182535f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 1826aadd06e5SJens Axboe return 0; 1827aadd06e5SJens Axboe 1828aadd06e5SJens Axboe ret = 0; 182961e0d47cSMiklos Szeredi pipe_lock(pipe); 1830aadd06e5SJens Axboe 183135f3d14dSJens Axboe while (pipe->nrbufs >= pipe->buffers) { 1832aadd06e5SJens Axboe if (!pipe->readers) { 1833aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1834aadd06e5SJens Axboe ret = -EPIPE; 1835aadd06e5SJens Axboe break; 1836aadd06e5SJens Axboe } 1837aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1838aadd06e5SJens Axboe ret = -EAGAIN; 1839aadd06e5SJens Axboe break; 1840aadd06e5SJens Axboe } 1841aadd06e5SJens Axboe if (signal_pending(current)) { 1842aadd06e5SJens Axboe ret = -ERESTARTSYS; 1843aadd06e5SJens Axboe break; 1844aadd06e5SJens Axboe } 1845aadd06e5SJens Axboe pipe->waiting_writers++; 1846aadd06e5SJens Axboe pipe_wait(pipe); 1847aadd06e5SJens Axboe pipe->waiting_writers--; 1848aadd06e5SJens Axboe } 1849aadd06e5SJens Axboe 185061e0d47cSMiklos Szeredi pipe_unlock(pipe); 1851aadd06e5SJens Axboe return ret; 1852aadd06e5SJens Axboe } 1853aadd06e5SJens Axboe 1854aadd06e5SJens Axboe /* 18557c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 18567c77f0b3SMiklos Szeredi */ 18577c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 18587c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 18597c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 18607c77f0b3SMiklos Szeredi { 18617c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 18627c77f0b3SMiklos Szeredi int ret = 0, nbuf; 18637c77f0b3SMiklos Szeredi bool input_wakeup = false; 18647c77f0b3SMiklos Szeredi 18657c77f0b3SMiklos Szeredi 18667c77f0b3SMiklos Szeredi retry: 18677c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 18687c77f0b3SMiklos Szeredi if (ret) 18697c77f0b3SMiklos Szeredi return ret; 18707c77f0b3SMiklos Szeredi 18717c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 18727c77f0b3SMiklos Szeredi if (ret) 18737c77f0b3SMiklos Szeredi return ret; 18747c77f0b3SMiklos Szeredi 18757c77f0b3SMiklos Szeredi /* 18767c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 18777c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 18787c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 18797c77f0b3SMiklos Szeredi */ 18807c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 18817c77f0b3SMiklos Szeredi 18827c77f0b3SMiklos Szeredi do { 18837c77f0b3SMiklos Szeredi if (!opipe->readers) { 18847c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 18857c77f0b3SMiklos Szeredi if (!ret) 18867c77f0b3SMiklos Szeredi ret = -EPIPE; 18877c77f0b3SMiklos Szeredi break; 18887c77f0b3SMiklos Szeredi } 18897c77f0b3SMiklos Szeredi 18907c77f0b3SMiklos Szeredi if (!ipipe->nrbufs && !ipipe->writers) 18917c77f0b3SMiklos Szeredi break; 18927c77f0b3SMiklos Szeredi 18937c77f0b3SMiklos Szeredi /* 18947c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 18957c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 18967c77f0b3SMiklos Szeredi */ 189735f3d14dSJens Axboe if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) { 18987c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 18997c77f0b3SMiklos Szeredi if (ret) 19007c77f0b3SMiklos Szeredi break; 19017c77f0b3SMiklos Szeredi 19027c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 19037c77f0b3SMiklos Szeredi ret = -EAGAIN; 19047c77f0b3SMiklos Szeredi break; 19057c77f0b3SMiklos Szeredi } 19067c77f0b3SMiklos Szeredi 19077c77f0b3SMiklos Szeredi /* 19087c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 19097c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 19107c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 19117c77f0b3SMiklos Szeredi */ 19127c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 19137c77f0b3SMiklos Szeredi pipe_unlock(opipe); 19147c77f0b3SMiklos Szeredi goto retry; 19157c77f0b3SMiklos Szeredi } 19167c77f0b3SMiklos Szeredi 19177c77f0b3SMiklos Szeredi ibuf = ipipe->bufs + ipipe->curbuf; 191835f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 19197c77f0b3SMiklos Szeredi obuf = opipe->bufs + nbuf; 19207c77f0b3SMiklos Szeredi 19217c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 19227c77f0b3SMiklos Szeredi /* 19237c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 19247c77f0b3SMiklos Szeredi */ 19257c77f0b3SMiklos Szeredi *obuf = *ibuf; 19267c77f0b3SMiklos Szeredi ibuf->ops = NULL; 19277c77f0b3SMiklos Szeredi opipe->nrbufs++; 192835f3d14dSJens Axboe ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1); 19297c77f0b3SMiklos Szeredi ipipe->nrbufs--; 19307c77f0b3SMiklos Szeredi input_wakeup = true; 19317c77f0b3SMiklos Szeredi } else { 19327c77f0b3SMiklos Szeredi /* 19337c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 19347c77f0b3SMiklos Szeredi * so we can copy the contents over. 19357c77f0b3SMiklos Szeredi */ 19367c77f0b3SMiklos Szeredi ibuf->ops->get(ipipe, ibuf); 19377c77f0b3SMiklos Szeredi *obuf = *ibuf; 19387c77f0b3SMiklos Szeredi 19397c77f0b3SMiklos Szeredi /* 19407c77f0b3SMiklos Szeredi * Don't inherit the gift flag, we need to 19417c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 19427c77f0b3SMiklos Szeredi */ 19437c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 19447c77f0b3SMiklos Szeredi 19457c77f0b3SMiklos Szeredi obuf->len = len; 19467c77f0b3SMiklos Szeredi opipe->nrbufs++; 19477c77f0b3SMiklos Szeredi ibuf->offset += obuf->len; 19487c77f0b3SMiklos Szeredi ibuf->len -= obuf->len; 19497c77f0b3SMiklos Szeredi } 19507c77f0b3SMiklos Szeredi ret += obuf->len; 19517c77f0b3SMiklos Szeredi len -= obuf->len; 19527c77f0b3SMiklos Szeredi } while (len); 19537c77f0b3SMiklos Szeredi 19547c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 19557c77f0b3SMiklos Szeredi pipe_unlock(opipe); 19567c77f0b3SMiklos Szeredi 19577c77f0b3SMiklos Szeredi /* 19587c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 19597c77f0b3SMiklos Szeredi */ 1960825cdcb1SNamhyung Kim if (ret > 0) 1961825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1962825cdcb1SNamhyung Kim 19637c77f0b3SMiklos Szeredi if (input_wakeup) 19647c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 19657c77f0b3SMiklos Szeredi 19667c77f0b3SMiklos Szeredi return ret; 19677c77f0b3SMiklos Szeredi } 19687c77f0b3SMiklos Szeredi 19697c77f0b3SMiklos Szeredi /* 197070524490SJens Axboe * Link contents of ipipe to opipe. 197170524490SJens Axboe */ 197270524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 197370524490SJens Axboe struct pipe_inode_info *opipe, 197470524490SJens Axboe size_t len, unsigned int flags) 197570524490SJens Axboe { 197670524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 1977aadd06e5SJens Axboe int ret = 0, i = 0, nbuf; 197870524490SJens Axboe 197970524490SJens Axboe /* 198070524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 198161e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 198270524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 198370524490SJens Axboe */ 198461e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 198570524490SJens Axboe 1986aadd06e5SJens Axboe do { 198770524490SJens Axboe if (!opipe->readers) { 198870524490SJens Axboe send_sig(SIGPIPE, current, 0); 198970524490SJens Axboe if (!ret) 199070524490SJens Axboe ret = -EPIPE; 199170524490SJens Axboe break; 199270524490SJens Axboe } 199370524490SJens Axboe 199470524490SJens Axboe /* 1995aadd06e5SJens Axboe * If we have iterated all input buffers or ran out of 1996aadd06e5SJens Axboe * output room, break. 199770524490SJens Axboe */ 199835f3d14dSJens Axboe if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) 1999aadd06e5SJens Axboe break; 2000aadd06e5SJens Axboe 200135f3d14dSJens Axboe ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1)); 200235f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 200370524490SJens Axboe 200470524490SJens Axboe /* 200570524490SJens Axboe * Get a reference to this pipe buffer, 200670524490SJens Axboe * so we can copy the contents over. 200770524490SJens Axboe */ 200870524490SJens Axboe ibuf->ops->get(ipipe, ibuf); 200970524490SJens Axboe 201070524490SJens Axboe obuf = opipe->bufs + nbuf; 201170524490SJens Axboe *obuf = *ibuf; 201270524490SJens Axboe 20137afa6fd0SJens Axboe /* 20147afa6fd0SJens Axboe * Don't inherit the gift flag, we need to 20157afa6fd0SJens Axboe * prevent multiple steals of this page. 20167afa6fd0SJens Axboe */ 20177afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 20187afa6fd0SJens Axboe 201970524490SJens Axboe if (obuf->len > len) 202070524490SJens Axboe obuf->len = len; 202170524490SJens Axboe 202270524490SJens Axboe opipe->nrbufs++; 202370524490SJens Axboe ret += obuf->len; 202470524490SJens Axboe len -= obuf->len; 2025aadd06e5SJens Axboe i++; 2026aadd06e5SJens Axboe } while (len); 202770524490SJens Axboe 202802cf01aeSJens Axboe /* 202902cf01aeSJens Axboe * return EAGAIN if we have the potential of some data in the 203002cf01aeSJens Axboe * future, otherwise just return 0 203102cf01aeSJens Axboe */ 203202cf01aeSJens Axboe if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) 203302cf01aeSJens Axboe ret = -EAGAIN; 203402cf01aeSJens Axboe 203561e0d47cSMiklos Szeredi pipe_unlock(ipipe); 203661e0d47cSMiklos Szeredi pipe_unlock(opipe); 203770524490SJens Axboe 2038aadd06e5SJens Axboe /* 2039aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 2040aadd06e5SJens Axboe */ 2041825cdcb1SNamhyung Kim if (ret > 0) 2042825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 204370524490SJens Axboe 204470524490SJens Axboe return ret; 204570524490SJens Axboe } 204670524490SJens Axboe 204770524490SJens Axboe /* 204870524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 204970524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 205070524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 205170524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 205270524490SJens Axboe */ 205370524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len, 205470524490SJens Axboe unsigned int flags) 205570524490SJens Axboe { 205671993e62SLinus Torvalds struct pipe_inode_info *ipipe = get_pipe_info(in); 205771993e62SLinus Torvalds struct pipe_inode_info *opipe = get_pipe_info(out); 2058aadd06e5SJens Axboe int ret = -EINVAL; 205970524490SJens Axboe 206070524490SJens Axboe /* 2061aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 2062aadd06e5SJens Axboe * copying the data. 206370524490SJens Axboe */ 2064aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 2065aadd06e5SJens Axboe /* 2066aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 2067aadd06e5SJens Axboe * ordering doesn't really matter. 2068aadd06e5SJens Axboe */ 20697c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 2070aadd06e5SJens Axboe if (!ret) { 20717c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 207202cf01aeSJens Axboe if (!ret) 2073aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 2074aadd06e5SJens Axboe } 2075aadd06e5SJens Axboe } 207670524490SJens Axboe 2077aadd06e5SJens Axboe return ret; 207870524490SJens Axboe } 207970524490SJens Axboe 2080836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 208170524490SJens Axboe { 20822903ff01SAl Viro struct fd in; 20832903ff01SAl Viro int error; 208470524490SJens Axboe 208570524490SJens Axboe if (unlikely(!len)) 208670524490SJens Axboe return 0; 208770524490SJens Axboe 208870524490SJens Axboe error = -EBADF; 20892903ff01SAl Viro in = fdget(fdin); 20902903ff01SAl Viro if (in.file) { 20912903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 20922903ff01SAl Viro struct fd out = fdget(fdout); 20932903ff01SAl Viro if (out.file) { 20942903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 20952903ff01SAl Viro error = do_tee(in.file, out.file, 20962903ff01SAl Viro len, flags); 20972903ff01SAl Viro fdput(out); 209870524490SJens Axboe } 209970524490SJens Axboe } 21002903ff01SAl Viro fdput(in); 210170524490SJens Axboe } 210270524490SJens Axboe 210370524490SJens Axboe return error; 210470524490SJens Axboe } 2105