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> 3406ae43f3SAl Viro #include "internal.h" 355274f052SJens Axboe 3683f9135bSJens Axboe /* 3783f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 3883f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 3983f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 4083f9135bSJens Axboe * attempt to reuse this page for another destination. 4183f9135bSJens Axboe */ 4276ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, 435abc97aaSJens Axboe struct pipe_buffer *buf) 445abc97aaSJens Axboe { 455abc97aaSJens Axboe struct page *page = buf->page; 469e94cd4fSJens Axboe struct address_space *mapping; 475abc97aaSJens Axboe 489e0267c2SJens Axboe lock_page(page); 499e0267c2SJens Axboe 509e94cd4fSJens Axboe mapping = page_mapping(page); 519e94cd4fSJens Axboe if (mapping) { 525abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 535abc97aaSJens Axboe 54ad8d6f0aSJens Axboe /* 559e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 569e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 579e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 589e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 599e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 609e94cd4fSJens Axboe * ensues. 61ad8d6f0aSJens Axboe */ 62ad8d6f0aSJens Axboe wait_on_page_writeback(page); 63ad8d6f0aSJens Axboe 64266cf658SDavid Howells if (page_has_private(page) && 65266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 66ca39d651SJens Axboe goto out_unlock; 674f6f0bd2SJens Axboe 689e94cd4fSJens Axboe /* 699e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 709e94cd4fSJens Axboe * and return good. 719e94cd4fSJens Axboe */ 729e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 731432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 745abc97aaSJens Axboe return 0; 755abc97aaSJens Axboe } 769e94cd4fSJens Axboe } 779e94cd4fSJens Axboe 789e94cd4fSJens Axboe /* 799e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 809e94cd4fSJens Axboe * address space, unlock and return failure. 819e94cd4fSJens Axboe */ 82ca39d651SJens Axboe out_unlock: 839e94cd4fSJens Axboe unlock_page(page); 849e94cd4fSJens Axboe return 1; 859e94cd4fSJens Axboe } 865abc97aaSJens Axboe 8776ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 885274f052SJens Axboe struct pipe_buffer *buf) 895274f052SJens Axboe { 905274f052SJens Axboe page_cache_release(buf->page); 911432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 925274f052SJens Axboe } 935274f052SJens Axboe 940845718dSJens Axboe /* 950845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 960845718dSJens Axboe * is a page cache page, IO may be in flight. 970845718dSJens Axboe */ 98cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 995274f052SJens Axboe struct pipe_buffer *buf) 1005274f052SJens Axboe { 1015274f052SJens Axboe struct page *page = buf->page; 10249d0b21bSJens Axboe int err; 1035274f052SJens Axboe 1045274f052SJens Axboe if (!PageUptodate(page)) { 10549d0b21bSJens Axboe lock_page(page); 1065274f052SJens Axboe 10749d0b21bSJens Axboe /* 10849d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 10973d62d83SIngo Molnar * splice, if this is the first page. 11049d0b21bSJens Axboe */ 1115274f052SJens Axboe if (!page->mapping) { 11249d0b21bSJens Axboe err = -ENODATA; 11349d0b21bSJens Axboe goto error; 1145274f052SJens Axboe } 1155274f052SJens Axboe 11649d0b21bSJens Axboe /* 11773d62d83SIngo Molnar * Uh oh, read-error from disk. 11849d0b21bSJens Axboe */ 11949d0b21bSJens Axboe if (!PageUptodate(page)) { 12049d0b21bSJens Axboe err = -EIO; 12149d0b21bSJens Axboe goto error; 12249d0b21bSJens Axboe } 12349d0b21bSJens Axboe 12449d0b21bSJens Axboe /* 125f84d7519SJens Axboe * Page is ok afterall, we are done. 12649d0b21bSJens Axboe */ 12749d0b21bSJens Axboe unlock_page(page); 12849d0b21bSJens Axboe } 12949d0b21bSJens Axboe 130f84d7519SJens Axboe return 0; 13149d0b21bSJens Axboe error: 13249d0b21bSJens Axboe unlock_page(page); 133f84d7519SJens Axboe return err; 13470524490SJens Axboe } 13570524490SJens Axboe 136708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 1375274f052SJens Axboe .can_merge = 0, 138f84d7519SJens Axboe .map = generic_pipe_buf_map, 139f84d7519SJens Axboe .unmap = generic_pipe_buf_unmap, 140cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1415274f052SJens Axboe .release = page_cache_pipe_buf_release, 1425abc97aaSJens Axboe .steal = page_cache_pipe_buf_steal, 143f84d7519SJens Axboe .get = generic_pipe_buf_get, 1445274f052SJens Axboe }; 1455274f052SJens Axboe 146912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, 147912d35f8SJens Axboe struct pipe_buffer *buf) 148912d35f8SJens Axboe { 1497afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 150912d35f8SJens Axboe return 1; 1517afa6fd0SJens Axboe 1521432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 153330ab716SJens Axboe return generic_pipe_buf_steal(pipe, buf); 154912d35f8SJens Axboe } 155912d35f8SJens Axboe 156d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 157912d35f8SJens Axboe .can_merge = 0, 158f84d7519SJens Axboe .map = generic_pipe_buf_map, 159f84d7519SJens Axboe .unmap = generic_pipe_buf_unmap, 160cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 161912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 162912d35f8SJens Axboe .steal = user_page_pipe_buf_steal, 163f84d7519SJens Axboe .get = generic_pipe_buf_get, 164912d35f8SJens Axboe }; 165912d35f8SJens Axboe 166825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 167825cdcb1SNamhyung Kim { 168825cdcb1SNamhyung Kim smp_mb(); 169825cdcb1SNamhyung Kim if (waitqueue_active(&pipe->wait)) 170825cdcb1SNamhyung Kim wake_up_interruptible(&pipe->wait); 171825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 172825cdcb1SNamhyung Kim } 173825cdcb1SNamhyung Kim 174932cc6d4SJens Axboe /** 175932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 176932cc6d4SJens Axboe * @pipe: pipe to fill 177932cc6d4SJens Axboe * @spd: data to fill 178932cc6d4SJens Axboe * 179932cc6d4SJens Axboe * Description: 18079685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 181932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 182932cc6d4SJens Axboe * function will link that data to the pipe. 183932cc6d4SJens Axboe * 18483f9135bSJens Axboe */ 185d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 186912d35f8SJens Axboe struct splice_pipe_desc *spd) 1875274f052SJens Axboe { 18800de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 189912d35f8SJens Axboe int ret, do_wakeup, page_nr; 1905274f052SJens Axboe 1915274f052SJens Axboe ret = 0; 1925274f052SJens Axboe do_wakeup = 0; 193912d35f8SJens Axboe page_nr = 0; 1945274f052SJens Axboe 19561e0d47cSMiklos Szeredi pipe_lock(pipe); 1965274f052SJens Axboe 1975274f052SJens Axboe for (;;) { 1983a326a2cSIngo Molnar if (!pipe->readers) { 1995274f052SJens Axboe send_sig(SIGPIPE, current, 0); 2005274f052SJens Axboe if (!ret) 2015274f052SJens Axboe ret = -EPIPE; 2025274f052SJens Axboe break; 2035274f052SJens Axboe } 2045274f052SJens Axboe 20535f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) { 20635f3d14dSJens Axboe int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1); 2073a326a2cSIngo Molnar struct pipe_buffer *buf = pipe->bufs + newbuf; 2085274f052SJens Axboe 209912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 210912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 211912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 212497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 213912d35f8SJens Axboe buf->ops = spd->ops; 2147afa6fd0SJens Axboe if (spd->flags & SPLICE_F_GIFT) 2157afa6fd0SJens Axboe buf->flags |= PIPE_BUF_FLAG_GIFT; 2167afa6fd0SJens Axboe 2176f767b04SJens Axboe pipe->nrbufs++; 218912d35f8SJens Axboe page_nr++; 219912d35f8SJens Axboe ret += buf->len; 220912d35f8SJens Axboe 2216447a3cfSAl Viro if (pipe->files) 2225274f052SJens Axboe do_wakeup = 1; 2235274f052SJens Axboe 224912d35f8SJens Axboe if (!--spd->nr_pages) 2255274f052SJens Axboe break; 22635f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 2275274f052SJens Axboe continue; 2285274f052SJens Axboe 2295274f052SJens Axboe break; 2305274f052SJens Axboe } 2315274f052SJens Axboe 232912d35f8SJens Axboe if (spd->flags & SPLICE_F_NONBLOCK) { 23329e35094SLinus Torvalds if (!ret) 23429e35094SLinus Torvalds ret = -EAGAIN; 23529e35094SLinus Torvalds break; 23629e35094SLinus Torvalds } 23729e35094SLinus Torvalds 2385274f052SJens Axboe if (signal_pending(current)) { 2395274f052SJens Axboe if (!ret) 2405274f052SJens Axboe ret = -ERESTARTSYS; 2415274f052SJens Axboe break; 2425274f052SJens Axboe } 2435274f052SJens Axboe 2445274f052SJens Axboe if (do_wakeup) { 245c0bd1f65SJens Axboe smp_mb(); 2463a326a2cSIngo Molnar if (waitqueue_active(&pipe->wait)) 2473a326a2cSIngo Molnar wake_up_interruptible_sync(&pipe->wait); 2483a326a2cSIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 2495274f052SJens Axboe do_wakeup = 0; 2505274f052SJens Axboe } 2515274f052SJens Axboe 2523a326a2cSIngo Molnar pipe->waiting_writers++; 2533a326a2cSIngo Molnar pipe_wait(pipe); 2543a326a2cSIngo Molnar pipe->waiting_writers--; 2555274f052SJens Axboe } 2565274f052SJens Axboe 25761e0d47cSMiklos Szeredi pipe_unlock(pipe); 2585274f052SJens Axboe 259825cdcb1SNamhyung Kim if (do_wakeup) 260825cdcb1SNamhyung Kim wakeup_pipe_readers(pipe); 2615274f052SJens Axboe 26200de00bdSJens Axboe while (page_nr < spd_pages) 263bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2645274f052SJens Axboe 2655274f052SJens Axboe return ret; 2665274f052SJens Axboe } 2675274f052SJens Axboe 268708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) 269bbdfc2f7SJens Axboe { 270bbdfc2f7SJens Axboe page_cache_release(spd->pages[i]); 271bbdfc2f7SJens Axboe } 272bbdfc2f7SJens Axboe 27335f3d14dSJens Axboe /* 27435f3d14dSJens Axboe * Check if we need to grow the arrays holding pages and partial page 27535f3d14dSJens Axboe * descriptions. 27635f3d14dSJens Axboe */ 277047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 27835f3d14dSJens Axboe { 279047fe360SEric Dumazet unsigned int buffers = ACCESS_ONCE(pipe->buffers); 280047fe360SEric Dumazet 281047fe360SEric Dumazet spd->nr_pages_max = buffers; 282047fe360SEric Dumazet if (buffers <= PIPE_DEF_BUFFERS) 28335f3d14dSJens Axboe return 0; 28435f3d14dSJens Axboe 285047fe360SEric Dumazet spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL); 286047fe360SEric Dumazet spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL); 28735f3d14dSJens Axboe 28835f3d14dSJens Axboe if (spd->pages && spd->partial) 28935f3d14dSJens Axboe return 0; 29035f3d14dSJens Axboe 29135f3d14dSJens Axboe kfree(spd->pages); 29235f3d14dSJens Axboe kfree(spd->partial); 29335f3d14dSJens Axboe return -ENOMEM; 29435f3d14dSJens Axboe } 29535f3d14dSJens Axboe 296047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd) 29735f3d14dSJens Axboe { 298047fe360SEric Dumazet if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 29935f3d14dSJens Axboe return; 30035f3d14dSJens Axboe 30135f3d14dSJens Axboe kfree(spd->pages); 30235f3d14dSJens Axboe kfree(spd->partial); 30335f3d14dSJens Axboe } 30435f3d14dSJens Axboe 3053a326a2cSIngo Molnar static int 306cbb7e577SJens Axboe __generic_file_splice_read(struct file *in, loff_t *ppos, 307cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 308cbb7e577SJens Axboe unsigned int flags) 3095274f052SJens Axboe { 3105274f052SJens Axboe struct address_space *mapping = in->f_mapping; 311d8983910SFengguang Wu unsigned int loff, nr_pages, req_pages; 31235f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 31335f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 3145274f052SJens Axboe struct page *page; 31591ad66efSJens Axboe pgoff_t index, end_index; 31691ad66efSJens Axboe loff_t isize; 317eb20796bSJens Axboe int error, page_nr; 318912d35f8SJens Axboe struct splice_pipe_desc spd = { 319912d35f8SJens Axboe .pages = pages, 320912d35f8SJens Axboe .partial = partial, 321047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 322912d35f8SJens Axboe .flags = flags, 323912d35f8SJens Axboe .ops = &page_cache_pipe_buf_ops, 324bbdfc2f7SJens Axboe .spd_release = spd_release_page, 325912d35f8SJens Axboe }; 3265274f052SJens Axboe 32735f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 32835f3d14dSJens Axboe return -ENOMEM; 32935f3d14dSJens Axboe 330cbb7e577SJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 331912d35f8SJens Axboe loff = *ppos & ~PAGE_CACHE_MASK; 332d8983910SFengguang Wu req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 333047fe360SEric Dumazet nr_pages = min(req_pages, spd.nr_pages_max); 3345274f052SJens Axboe 3355274f052SJens Axboe /* 336eb20796bSJens Axboe * Lookup the (hopefully) full range of pages we need. 33782aa5d61SJens Axboe */ 33835f3d14dSJens Axboe spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages); 339431a4820SFengguang Wu index += spd.nr_pages; 340eb20796bSJens Axboe 3415274f052SJens Axboe /* 342eb20796bSJens Axboe * If find_get_pages_contig() returned fewer pages than we needed, 343431a4820SFengguang Wu * readahead/allocate the rest and fill in the holes. 344eb20796bSJens Axboe */ 345431a4820SFengguang Wu if (spd.nr_pages < nr_pages) 346cf914a7dSRusty Russell page_cache_sync_readahead(mapping, &in->f_ra, in, 347cf914a7dSRusty Russell index, req_pages - spd.nr_pages); 348431a4820SFengguang Wu 349932cc6d4SJens Axboe error = 0; 350eb20796bSJens Axboe while (spd.nr_pages < nr_pages) { 351eb20796bSJens Axboe /* 352eb20796bSJens Axboe * Page could be there, find_get_pages_contig() breaks on 353eb20796bSJens Axboe * the first hole. 3545274f052SJens Axboe */ 3557480a904SJens Axboe page = find_get_page(mapping, index); 3567480a904SJens Axboe if (!page) { 357e27dedd8SJens Axboe /* 358eb20796bSJens Axboe * page didn't exist, allocate one. 3597480a904SJens Axboe */ 3607480a904SJens Axboe page = page_cache_alloc_cold(mapping); 3615274f052SJens Axboe if (!page) 3625274f052SJens Axboe break; 3635274f052SJens Axboe 3647480a904SJens Axboe error = add_to_page_cache_lru(page, mapping, index, 3650ae0b5d0SNick Piggin GFP_KERNEL); 3665274f052SJens Axboe if (unlikely(error)) { 3675274f052SJens Axboe page_cache_release(page); 368a0548871SJens Axboe if (error == -EEXIST) 369a0548871SJens Axboe continue; 3705274f052SJens Axboe break; 3715274f052SJens Axboe } 372eb20796bSJens Axboe /* 373eb20796bSJens Axboe * add_to_page_cache() locks the page, unlock it 374eb20796bSJens Axboe * to avoid convoluting the logic below even more. 375eb20796bSJens Axboe */ 376eb20796bSJens Axboe unlock_page(page); 3775274f052SJens Axboe } 3787480a904SJens Axboe 37935f3d14dSJens Axboe spd.pages[spd.nr_pages++] = page; 380eb20796bSJens Axboe index++; 381eb20796bSJens Axboe } 382eb20796bSJens Axboe 383eb20796bSJens Axboe /* 384eb20796bSJens Axboe * Now loop over the map and see if we need to start IO on any 385eb20796bSJens Axboe * pages, fill in the partial map, etc. 386eb20796bSJens Axboe */ 387eb20796bSJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 388eb20796bSJens Axboe nr_pages = spd.nr_pages; 389eb20796bSJens Axboe spd.nr_pages = 0; 390eb20796bSJens Axboe for (page_nr = 0; page_nr < nr_pages; page_nr++) { 391eb20796bSJens Axboe unsigned int this_len; 392eb20796bSJens Axboe 393eb20796bSJens Axboe if (!len) 394eb20796bSJens Axboe break; 395eb20796bSJens Axboe 396eb20796bSJens Axboe /* 397eb20796bSJens Axboe * this_len is the max we'll use from this page 398eb20796bSJens Axboe */ 399eb20796bSJens Axboe this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 40035f3d14dSJens Axboe page = spd.pages[page_nr]; 401eb20796bSJens Axboe 402a08a166fSFengguang Wu if (PageReadahead(page)) 403cf914a7dSRusty Russell page_cache_async_readahead(mapping, &in->f_ra, in, 404d8983910SFengguang Wu page, index, req_pages - page_nr); 405a08a166fSFengguang Wu 4067480a904SJens Axboe /* 4077480a904SJens Axboe * If the page isn't uptodate, we may need to start io on it 4087480a904SJens Axboe */ 4097480a904SJens Axboe if (!PageUptodate(page)) { 4107480a904SJens Axboe lock_page(page); 4117480a904SJens Axboe 4127480a904SJens Axboe /* 41332502b84SMiklos Szeredi * Page was truncated, or invalidated by the 41432502b84SMiklos Szeredi * filesystem. Redo the find/create, but this time the 41532502b84SMiklos Szeredi * page is kept locked, so there's no chance of another 41632502b84SMiklos Szeredi * race with truncate/invalidate. 4177480a904SJens Axboe */ 4187480a904SJens Axboe if (!page->mapping) { 4197480a904SJens Axboe unlock_page(page); 42032502b84SMiklos Szeredi page = find_or_create_page(mapping, index, 42132502b84SMiklos Szeredi mapping_gfp_mask(mapping)); 42232502b84SMiklos Szeredi 42332502b84SMiklos Szeredi if (!page) { 42432502b84SMiklos Szeredi error = -ENOMEM; 4257480a904SJens Axboe break; 4267480a904SJens Axboe } 42735f3d14dSJens Axboe page_cache_release(spd.pages[page_nr]); 42835f3d14dSJens Axboe spd.pages[page_nr] = page; 42932502b84SMiklos Szeredi } 4307480a904SJens Axboe /* 4317480a904SJens Axboe * page was already under io and is now done, great 4327480a904SJens Axboe */ 4337480a904SJens Axboe if (PageUptodate(page)) { 4347480a904SJens Axboe unlock_page(page); 4357480a904SJens Axboe goto fill_it; 4367480a904SJens Axboe } 4377480a904SJens Axboe 4387480a904SJens Axboe /* 4397480a904SJens Axboe * need to read in the page 4407480a904SJens Axboe */ 4417480a904SJens Axboe error = mapping->a_ops->readpage(in, page); 4427480a904SJens Axboe if (unlikely(error)) { 443eb20796bSJens Axboe /* 444eb20796bSJens Axboe * We really should re-lookup the page here, 445eb20796bSJens Axboe * but it complicates things a lot. Instead 446eb20796bSJens Axboe * lets just do what we already stored, and 447eb20796bSJens Axboe * we'll get it the next time we are called. 448eb20796bSJens Axboe */ 4497480a904SJens Axboe if (error == AOP_TRUNCATED_PAGE) 450eb20796bSJens Axboe error = 0; 451eb20796bSJens Axboe 4527480a904SJens Axboe break; 4537480a904SJens Axboe } 454620a324bSJens Axboe } 455620a324bSJens Axboe fill_it: 45691ad66efSJens Axboe /* 457620a324bSJens Axboe * i_size must be checked after PageUptodate. 45891ad66efSJens Axboe */ 45991ad66efSJens Axboe isize = i_size_read(mapping->host); 46091ad66efSJens Axboe end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 461eb20796bSJens Axboe if (unlikely(!isize || index > end_index)) 46291ad66efSJens Axboe break; 46391ad66efSJens Axboe 46491ad66efSJens Axboe /* 46591ad66efSJens Axboe * if this is the last page, see if we need to shrink 46691ad66efSJens Axboe * the length and stop 46791ad66efSJens Axboe */ 46891ad66efSJens Axboe if (end_index == index) { 469475ecadeSHugh Dickins unsigned int plen; 470475ecadeSHugh Dickins 471475ecadeSHugh Dickins /* 472475ecadeSHugh Dickins * max good bytes in this page 473475ecadeSHugh Dickins */ 474475ecadeSHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 475475ecadeSHugh Dickins if (plen <= loff) 47691ad66efSJens Axboe break; 477475ecadeSHugh Dickins 47891ad66efSJens Axboe /* 47991ad66efSJens Axboe * force quit after adding this page 48091ad66efSJens Axboe */ 481475ecadeSHugh Dickins this_len = min(this_len, plen - loff); 482eb20796bSJens Axboe len = this_len; 48391ad66efSJens Axboe } 484620a324bSJens Axboe 48535f3d14dSJens Axboe spd.partial[page_nr].offset = loff; 48635f3d14dSJens Axboe spd.partial[page_nr].len = this_len; 48782aa5d61SJens Axboe len -= this_len; 48891ad66efSJens Axboe loff = 0; 489eb20796bSJens Axboe spd.nr_pages++; 490eb20796bSJens Axboe index++; 4915274f052SJens Axboe } 4925274f052SJens Axboe 493eb20796bSJens Axboe /* 494475ecadeSHugh Dickins * Release any pages at the end, if we quit early. 'page_nr' is how far 495eb20796bSJens Axboe * we got, 'nr_pages' is how many pages are in the map. 496eb20796bSJens Axboe */ 497eb20796bSJens Axboe while (page_nr < nr_pages) 49835f3d14dSJens Axboe page_cache_release(spd.pages[page_nr++]); 499f4e6b498SFengguang Wu in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT; 500eb20796bSJens Axboe 501912d35f8SJens Axboe if (spd.nr_pages) 50235f3d14dSJens Axboe error = splice_to_pipe(pipe, &spd); 50316c523ddSJens Axboe 504047fe360SEric Dumazet splice_shrink_spd(&spd); 5057480a904SJens Axboe return error; 5065274f052SJens Axboe } 5075274f052SJens Axboe 50883f9135bSJens Axboe /** 50983f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 51083f9135bSJens Axboe * @in: file to splice from 511932cc6d4SJens Axboe * @ppos: position in @in 51283f9135bSJens Axboe * @pipe: pipe to splice to 51383f9135bSJens Axboe * @len: number of bytes to splice 51483f9135bSJens Axboe * @flags: splice modifier flags 51583f9135bSJens Axboe * 516932cc6d4SJens Axboe * Description: 517932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 518932cc6d4SJens Axboe * used as long as the address_space operations for the source implements 519932cc6d4SJens Axboe * a readpage() hook. 520932cc6d4SJens Axboe * 52183f9135bSJens Axboe */ 522cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 523cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 524cbb7e577SJens Axboe unsigned int flags) 5255274f052SJens Axboe { 526d366d398SJens Axboe loff_t isize, left; 5278191ecd1SJens Axboe int ret; 528d366d398SJens Axboe 529d366d398SJens Axboe isize = i_size_read(in->f_mapping->host); 530d366d398SJens Axboe if (unlikely(*ppos >= isize)) 531d366d398SJens Axboe return 0; 532d366d398SJens Axboe 533d366d398SJens Axboe left = isize - *ppos; 534d366d398SJens Axboe if (unlikely(left < len)) 535d366d398SJens Axboe len = left; 5365274f052SJens Axboe 537cbb7e577SJens Axboe ret = __generic_file_splice_read(in, ppos, pipe, len, flags); 538723590edSMiklos Szeredi if (ret > 0) { 539cbb7e577SJens Axboe *ppos += ret; 540723590edSMiklos Szeredi file_accessed(in); 541723590edSMiklos Szeredi } 5425274f052SJens Axboe 5435274f052SJens Axboe return ret; 5445274f052SJens Axboe } 545059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 546059a8f37SJens Axboe 5476818173bSMiklos Szeredi static const struct pipe_buf_operations default_pipe_buf_ops = { 5486818173bSMiklos Szeredi .can_merge = 0, 5496818173bSMiklos Szeredi .map = generic_pipe_buf_map, 5506818173bSMiklos Szeredi .unmap = generic_pipe_buf_unmap, 5516818173bSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 5526818173bSMiklos Szeredi .release = generic_pipe_buf_release, 5536818173bSMiklos Szeredi .steal = generic_pipe_buf_steal, 5546818173bSMiklos Szeredi .get = generic_pipe_buf_get, 5556818173bSMiklos Szeredi }; 5566818173bSMiklos Szeredi 5576818173bSMiklos Szeredi static ssize_t kernel_readv(struct file *file, const struct iovec *vec, 5586818173bSMiklos Szeredi unsigned long vlen, loff_t offset) 5596818173bSMiklos Szeredi { 5606818173bSMiklos Szeredi mm_segment_t old_fs; 5616818173bSMiklos Szeredi loff_t pos = offset; 5626818173bSMiklos Szeredi ssize_t res; 5636818173bSMiklos Szeredi 5646818173bSMiklos Szeredi old_fs = get_fs(); 5656818173bSMiklos Szeredi set_fs(get_ds()); 5666818173bSMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 5676818173bSMiklos Szeredi res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos); 5686818173bSMiklos Szeredi set_fs(old_fs); 5696818173bSMiklos Szeredi 5706818173bSMiklos Szeredi return res; 5716818173bSMiklos Szeredi } 5726818173bSMiklos Szeredi 5737bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count, 574b2858d7dSMiklos Szeredi loff_t pos) 5750b0a47f5SMiklos Szeredi { 5760b0a47f5SMiklos Szeredi mm_segment_t old_fs; 5770b0a47f5SMiklos Szeredi ssize_t res; 5780b0a47f5SMiklos Szeredi 5790b0a47f5SMiklos Szeredi old_fs = get_fs(); 5800b0a47f5SMiklos Szeredi set_fs(get_ds()); 5810b0a47f5SMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 5827bb307e8SAl Viro res = vfs_write(file, (__force const char __user *)buf, count, &pos); 5830b0a47f5SMiklos Szeredi set_fs(old_fs); 5840b0a47f5SMiklos Szeredi 5850b0a47f5SMiklos Szeredi return res; 5860b0a47f5SMiklos Szeredi } 5877bb307e8SAl Viro EXPORT_SYMBOL(kernel_write); 5880b0a47f5SMiklos Szeredi 5896818173bSMiklos Szeredi ssize_t default_file_splice_read(struct file *in, loff_t *ppos, 5906818173bSMiklos Szeredi struct pipe_inode_info *pipe, size_t len, 5916818173bSMiklos Szeredi unsigned int flags) 5926818173bSMiklos Szeredi { 5936818173bSMiklos Szeredi unsigned int nr_pages; 5946818173bSMiklos Szeredi unsigned int nr_freed; 5956818173bSMiklos Szeredi size_t offset; 59635f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 59735f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 59835f3d14dSJens Axboe struct iovec *vec, __vec[PIPE_DEF_BUFFERS]; 5996818173bSMiklos Szeredi ssize_t res; 6006818173bSMiklos Szeredi size_t this_len; 6016818173bSMiklos Szeredi int error; 6026818173bSMiklos Szeredi int i; 6036818173bSMiklos Szeredi struct splice_pipe_desc spd = { 6046818173bSMiklos Szeredi .pages = pages, 6056818173bSMiklos Szeredi .partial = partial, 606047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 6076818173bSMiklos Szeredi .flags = flags, 6086818173bSMiklos Szeredi .ops = &default_pipe_buf_ops, 6096818173bSMiklos Szeredi .spd_release = spd_release_page, 6106818173bSMiklos Szeredi }; 6116818173bSMiklos Szeredi 61235f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 61335f3d14dSJens Axboe return -ENOMEM; 61435f3d14dSJens Axboe 61535f3d14dSJens Axboe res = -ENOMEM; 61635f3d14dSJens Axboe vec = __vec; 617047fe360SEric Dumazet if (spd.nr_pages_max > PIPE_DEF_BUFFERS) { 618047fe360SEric Dumazet vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL); 61935f3d14dSJens Axboe if (!vec) 62035f3d14dSJens Axboe goto shrink_ret; 62135f3d14dSJens Axboe } 62235f3d14dSJens Axboe 6236818173bSMiklos Szeredi offset = *ppos & ~PAGE_CACHE_MASK; 6246818173bSMiklos Szeredi nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 6256818173bSMiklos Szeredi 626047fe360SEric Dumazet for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) { 6276818173bSMiklos Szeredi struct page *page; 6286818173bSMiklos Szeredi 6294f231228SJens Axboe page = alloc_page(GFP_USER); 6306818173bSMiklos Szeredi error = -ENOMEM; 6316818173bSMiklos Szeredi if (!page) 6326818173bSMiklos Szeredi goto err; 6336818173bSMiklos Szeredi 6346818173bSMiklos Szeredi this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset); 6354f231228SJens Axboe vec[i].iov_base = (void __user *) page_address(page); 6366818173bSMiklos Szeredi vec[i].iov_len = this_len; 63735f3d14dSJens Axboe spd.pages[i] = page; 6386818173bSMiklos Szeredi spd.nr_pages++; 6396818173bSMiklos Szeredi len -= this_len; 6406818173bSMiklos Szeredi offset = 0; 6416818173bSMiklos Szeredi } 6426818173bSMiklos Szeredi 6436818173bSMiklos Szeredi res = kernel_readv(in, vec, spd.nr_pages, *ppos); 64477f6bf57SAndrew Morton if (res < 0) { 64577f6bf57SAndrew Morton error = res; 6466818173bSMiklos Szeredi goto err; 64777f6bf57SAndrew Morton } 6486818173bSMiklos Szeredi 6496818173bSMiklos Szeredi error = 0; 6506818173bSMiklos Szeredi if (!res) 6516818173bSMiklos Szeredi goto err; 6526818173bSMiklos Szeredi 6536818173bSMiklos Szeredi nr_freed = 0; 6546818173bSMiklos Szeredi for (i = 0; i < spd.nr_pages; i++) { 6556818173bSMiklos Szeredi this_len = min_t(size_t, vec[i].iov_len, res); 65635f3d14dSJens Axboe spd.partial[i].offset = 0; 65735f3d14dSJens Axboe spd.partial[i].len = this_len; 6586818173bSMiklos Szeredi if (!this_len) { 65935f3d14dSJens Axboe __free_page(spd.pages[i]); 66035f3d14dSJens Axboe spd.pages[i] = NULL; 6616818173bSMiklos Szeredi nr_freed++; 6626818173bSMiklos Szeredi } 6636818173bSMiklos Szeredi res -= this_len; 6646818173bSMiklos Szeredi } 6656818173bSMiklos Szeredi spd.nr_pages -= nr_freed; 6666818173bSMiklos Szeredi 6676818173bSMiklos Szeredi res = splice_to_pipe(pipe, &spd); 6686818173bSMiklos Szeredi if (res > 0) 6696818173bSMiklos Szeredi *ppos += res; 6706818173bSMiklos Szeredi 67135f3d14dSJens Axboe shrink_ret: 67235f3d14dSJens Axboe if (vec != __vec) 67335f3d14dSJens Axboe kfree(vec); 674047fe360SEric Dumazet splice_shrink_spd(&spd); 6756818173bSMiklos Szeredi return res; 6766818173bSMiklos Szeredi 6776818173bSMiklos Szeredi err: 6784f231228SJens Axboe for (i = 0; i < spd.nr_pages; i++) 67935f3d14dSJens Axboe __free_page(spd.pages[i]); 6804f231228SJens Axboe 68135f3d14dSJens Axboe res = error; 68235f3d14dSJens Axboe goto shrink_ret; 6836818173bSMiklos Szeredi } 6846818173bSMiklos Szeredi EXPORT_SYMBOL(default_file_splice_read); 6856818173bSMiklos Szeredi 6865274f052SJens Axboe /* 6874f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 688016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 6895274f052SJens Axboe */ 69076ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 6915274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 6925274f052SJens Axboe { 6936a14b90bSJens Axboe struct file *file = sd->u.file; 6945274f052SJens Axboe loff_t pos = sd->pos; 695a8adbe37SMichał Mirosław int more; 6965274f052SJens Axboe 697a8adbe37SMichał Mirosław if (!likely(file->f_op && file->f_op->sendpage)) 698a8adbe37SMichał Mirosław return -EINVAL; 699a8adbe37SMichał Mirosław 70035f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 701ae62ca7bSEric Dumazet 702ae62ca7bSEric Dumazet if (sd->len < sd->total_len && pipe->nrbufs > 1) 70335f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 704ae62ca7bSEric Dumazet 705a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 706f84d7519SJens Axboe sd->len, &pos, more); 7075274f052SJens Axboe } 7085274f052SJens Axboe 7095274f052SJens Axboe /* 7105274f052SJens Axboe * This is a little more tricky than the file -> pipe splicing. There are 7115274f052SJens Axboe * basically three cases: 7125274f052SJens Axboe * 7135274f052SJens Axboe * - Destination page already exists in the address space and there 7145274f052SJens Axboe * are users of it. For that case we have no other option that 7155274f052SJens Axboe * copying the data. Tough luck. 7165274f052SJens Axboe * - Destination page already exists in the address space, but there 7175274f052SJens Axboe * are no users of it. Make sure it's uptodate, then drop it. Fall 7185274f052SJens Axboe * through to last case. 7195274f052SJens Axboe * - Destination page does not exist, we can add the pipe page to 7205274f052SJens Axboe * the page cache and avoid the copy. 7215274f052SJens Axboe * 72283f9135bSJens Axboe * If asked to move pages to the output file (SPLICE_F_MOVE is set in 72383f9135bSJens Axboe * sd->flags), we attempt to migrate pages from the pipe to the output 72483f9135bSJens Axboe * file address space page cache. This is possible if no one else has 72583f9135bSJens Axboe * the pipe page referenced outside of the pipe and page cache. If 72683f9135bSJens Axboe * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create 72783f9135bSJens Axboe * a new page in the output file page cache and fill/dirty that. 7285274f052SJens Axboe */ 729328eaabaSMiklos Szeredi int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 7305274f052SJens Axboe struct splice_desc *sd) 7315274f052SJens Axboe { 7326a14b90bSJens Axboe struct file *file = sd->u.file; 7335274f052SJens Axboe struct address_space *mapping = file->f_mapping; 734016b661eSJens Axboe unsigned int offset, this_len; 7355274f052SJens Axboe struct page *page; 736afddba49SNick Piggin void *fsdata; 7373e7ee3e7SJens Axboe int ret; 7385274f052SJens Axboe 7395274f052SJens Axboe offset = sd->pos & ~PAGE_CACHE_MASK; 7405274f052SJens Axboe 741016b661eSJens Axboe this_len = sd->len; 742016b661eSJens Axboe if (this_len + offset > PAGE_CACHE_SIZE) 743016b661eSJens Axboe this_len = PAGE_CACHE_SIZE - offset; 744016b661eSJens Axboe 745afddba49SNick Piggin ret = pagecache_write_begin(file, mapping, sd->pos, this_len, 746afddba49SNick Piggin AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 7479e0267c2SJens Axboe if (unlikely(ret)) 748afddba49SNick Piggin goto out; 7495274f052SJens Axboe 7500568b409SJens Axboe if (buf->page != page) { 75176ad4d11SJens Axboe char *src = buf->ops->map(pipe, buf, 1); 752e8e3c3d6SCong Wang char *dst = kmap_atomic(page); 7535abc97aaSJens Axboe 754016b661eSJens Axboe memcpy(dst + offset, src + buf->offset, this_len); 7555274f052SJens Axboe flush_dcache_page(page); 756e8e3c3d6SCong Wang kunmap_atomic(dst); 75776ad4d11SJens Axboe buf->ops->unmap(pipe, buf, src); 7585abc97aaSJens Axboe } 759afddba49SNick Piggin ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len, 760afddba49SNick Piggin page, fsdata); 7610568b409SJens Axboe out: 7625274f052SJens Axboe return ret; 7635274f052SJens Axboe } 764328eaabaSMiklos Szeredi EXPORT_SYMBOL(pipe_to_file); 7655274f052SJens Axboe 766b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 767b3c2d2ddSMiklos Szeredi { 768b3c2d2ddSMiklos Szeredi smp_mb(); 769b3c2d2ddSMiklos Szeredi if (waitqueue_active(&pipe->wait)) 770b3c2d2ddSMiklos Szeredi wake_up_interruptible(&pipe->wait); 771b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 772b3c2d2ddSMiklos Szeredi } 773b3c2d2ddSMiklos Szeredi 774b3c2d2ddSMiklos Szeredi /** 775b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 776b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 777b3c2d2ddSMiklos Szeredi * @sd: information to @actor 778b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 779b3c2d2ddSMiklos Szeredi * 780b3c2d2ddSMiklos Szeredi * Description: 781b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 782b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 783b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 784b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 785b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 786b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 787b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 788b3c2d2ddSMiklos Szeredi * 789b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 790b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 791b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 792b3c2d2ddSMiklos Szeredi * destination. 793b3c2d2ddSMiklos Szeredi */ 794b3c2d2ddSMiklos Szeredi int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 795b3c2d2ddSMiklos Szeredi splice_actor *actor) 796b3c2d2ddSMiklos Szeredi { 797b3c2d2ddSMiklos Szeredi int ret; 798b3c2d2ddSMiklos Szeredi 799b3c2d2ddSMiklos Szeredi while (pipe->nrbufs) { 800b3c2d2ddSMiklos Szeredi struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 801b3c2d2ddSMiklos Szeredi const struct pipe_buf_operations *ops = buf->ops; 802b3c2d2ddSMiklos Szeredi 803b3c2d2ddSMiklos Szeredi sd->len = buf->len; 804b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 805b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 806b3c2d2ddSMiklos Szeredi 807a8adbe37SMichał Mirosław ret = buf->ops->confirm(pipe, buf); 808a8adbe37SMichał Mirosław if (unlikely(ret)) { 809b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 810b3c2d2ddSMiklos Szeredi ret = 0; 811b3c2d2ddSMiklos Szeredi return ret; 812b3c2d2ddSMiklos Szeredi } 813a8adbe37SMichał Mirosław 814a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 815a8adbe37SMichał Mirosław if (ret <= 0) 816a8adbe37SMichał Mirosław return ret; 817a8adbe37SMichał Mirosław 818b3c2d2ddSMiklos Szeredi buf->offset += ret; 819b3c2d2ddSMiklos Szeredi buf->len -= ret; 820b3c2d2ddSMiklos Szeredi 821b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 822b3c2d2ddSMiklos Szeredi sd->len -= ret; 823b3c2d2ddSMiklos Szeredi sd->pos += ret; 824b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 825b3c2d2ddSMiklos Szeredi 826b3c2d2ddSMiklos Szeredi if (!buf->len) { 827b3c2d2ddSMiklos Szeredi buf->ops = NULL; 828b3c2d2ddSMiklos Szeredi ops->release(pipe, buf); 82935f3d14dSJens Axboe pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); 830b3c2d2ddSMiklos Szeredi pipe->nrbufs--; 8316447a3cfSAl Viro if (pipe->files) 832b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 833b3c2d2ddSMiklos Szeredi } 834b3c2d2ddSMiklos Szeredi 835b3c2d2ddSMiklos Szeredi if (!sd->total_len) 836b3c2d2ddSMiklos Szeredi return 0; 837b3c2d2ddSMiklos Szeredi } 838b3c2d2ddSMiklos Szeredi 839b3c2d2ddSMiklos Szeredi return 1; 840b3c2d2ddSMiklos Szeredi } 841b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_feed); 842b3c2d2ddSMiklos Szeredi 843b3c2d2ddSMiklos Szeredi /** 844b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 845b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 846b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 847b3c2d2ddSMiklos Szeredi * 848b3c2d2ddSMiklos Szeredi * Description: 849b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 850b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 851b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 852b3c2d2ddSMiklos Szeredi */ 853b3c2d2ddSMiklos Szeredi int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 854b3c2d2ddSMiklos Szeredi { 855b3c2d2ddSMiklos Szeredi while (!pipe->nrbufs) { 856b3c2d2ddSMiklos Szeredi if (!pipe->writers) 857b3c2d2ddSMiklos Szeredi return 0; 858b3c2d2ddSMiklos Szeredi 859b3c2d2ddSMiklos Szeredi if (!pipe->waiting_writers && sd->num_spliced) 860b3c2d2ddSMiklos Szeredi return 0; 861b3c2d2ddSMiklos Szeredi 862b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 863b3c2d2ddSMiklos Szeredi return -EAGAIN; 864b3c2d2ddSMiklos Szeredi 865b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 866b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 867b3c2d2ddSMiklos Szeredi 868b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 869b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 870b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 871b3c2d2ddSMiklos Szeredi } 872b3c2d2ddSMiklos Szeredi 873b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 874b3c2d2ddSMiklos Szeredi } 875b3c2d2ddSMiklos Szeredi 876b3c2d2ddSMiklos Szeredi return 1; 877b3c2d2ddSMiklos Szeredi } 878b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_next); 879b3c2d2ddSMiklos Szeredi 880b3c2d2ddSMiklos Szeredi /** 881b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 882b80901bbSRandy Dunlap * @sd: information about the splice operation 883b3c2d2ddSMiklos Szeredi * 884b3c2d2ddSMiklos Szeredi * Description: 885b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 886b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 887b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 888b3c2d2ddSMiklos Szeredi */ 889b3c2d2ddSMiklos Szeredi void splice_from_pipe_begin(struct splice_desc *sd) 890b3c2d2ddSMiklos Szeredi { 891b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 892b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 893b3c2d2ddSMiklos Szeredi } 894b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_begin); 895b3c2d2ddSMiklos Szeredi 896b3c2d2ddSMiklos Szeredi /** 897b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 898b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 899b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 900b3c2d2ddSMiklos Szeredi * 901b3c2d2ddSMiklos Szeredi * Description: 902b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 903b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 904b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 905b3c2d2ddSMiklos Szeredi */ 906b3c2d2ddSMiklos Szeredi void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 907b3c2d2ddSMiklos Szeredi { 908b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 909b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 910b3c2d2ddSMiklos Szeredi } 911b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_end); 912b3c2d2ddSMiklos Szeredi 913932cc6d4SJens Axboe /** 914932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 915932cc6d4SJens Axboe * @pipe: pipe to splice from 916932cc6d4SJens Axboe * @sd: information to @actor 917932cc6d4SJens Axboe * @actor: handler that splices the data 918932cc6d4SJens Axboe * 919932cc6d4SJens Axboe * Description: 920932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 921932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 922932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 923932cc6d4SJens Axboe * pipe_to_user. 924932cc6d4SJens Axboe * 92583f9135bSJens Axboe */ 926c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 927c66ab6faSJens Axboe splice_actor *actor) 9285274f052SJens Axboe { 929b3c2d2ddSMiklos Szeredi int ret; 9305274f052SJens Axboe 931b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 932b3c2d2ddSMiklos Szeredi do { 933b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 934b3c2d2ddSMiklos Szeredi if (ret > 0) 935b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 936b3c2d2ddSMiklos Szeredi } while (ret > 0); 937b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 9385274f052SJens Axboe 939b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 9405274f052SJens Axboe } 94140bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 9425274f052SJens Axboe 943932cc6d4SJens Axboe /** 944932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 945932cc6d4SJens Axboe * @pipe: pipe to splice from 946932cc6d4SJens Axboe * @out: file to splice to 947932cc6d4SJens Axboe * @ppos: position in @out 948932cc6d4SJens Axboe * @len: how many bytes to splice 949932cc6d4SJens Axboe * @flags: splice modifier flags 950932cc6d4SJens Axboe * @actor: handler that splices the data 951932cc6d4SJens Axboe * 952932cc6d4SJens Axboe * Description: 9532933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 954932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 955932cc6d4SJens Axboe * 956932cc6d4SJens Axboe */ 9576da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 9586da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 9596da61809SMark Fasheh splice_actor *actor) 9606da61809SMark Fasheh { 9616da61809SMark Fasheh ssize_t ret; 962c66ab6faSJens Axboe struct splice_desc sd = { 963c66ab6faSJens Axboe .total_len = len, 964c66ab6faSJens Axboe .flags = flags, 965c66ab6faSJens Axboe .pos = *ppos, 9666a14b90bSJens Axboe .u.file = out, 967c66ab6faSJens Axboe }; 9686da61809SMark Fasheh 96961e0d47cSMiklos Szeredi pipe_lock(pipe); 970c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 97161e0d47cSMiklos Szeredi pipe_unlock(pipe); 9726da61809SMark Fasheh 9736da61809SMark Fasheh return ret; 9746da61809SMark Fasheh } 9756da61809SMark Fasheh 9766da61809SMark Fasheh /** 97783f9135bSJens Axboe * generic_file_splice_write - splice data from a pipe to a file 9783a326a2cSIngo Molnar * @pipe: pipe info 97983f9135bSJens Axboe * @out: file to write to 980932cc6d4SJens Axboe * @ppos: position in @out 98183f9135bSJens Axboe * @len: number of bytes to splice 98283f9135bSJens Axboe * @flags: splice modifier flags 98383f9135bSJens Axboe * 984932cc6d4SJens Axboe * Description: 98583f9135bSJens Axboe * Will either move or copy pages (determined by @flags options) from 98683f9135bSJens Axboe * the given pipe inode to the given file. 98783f9135bSJens Axboe * 98883f9135bSJens Axboe */ 9893a326a2cSIngo Molnar ssize_t 9903a326a2cSIngo Molnar generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 991cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 9925274f052SJens Axboe { 9934f6f0bd2SJens Axboe struct address_space *mapping = out->f_mapping; 9948c34e2d6SJens Axboe struct inode *inode = mapping->host; 9957f3d4ee1SMiklos Szeredi struct splice_desc sd = { 9967f3d4ee1SMiklos Szeredi .total_len = len, 9977f3d4ee1SMiklos Szeredi .flags = flags, 9987f3d4ee1SMiklos Szeredi .pos = *ppos, 9997f3d4ee1SMiklos Szeredi .u.file = out, 10007f3d4ee1SMiklos Szeredi }; 10013a326a2cSIngo Molnar ssize_t ret; 10028c34e2d6SJens Axboe 100361e0d47cSMiklos Szeredi pipe_lock(pipe); 1004eb443e5aSMiklos Szeredi 1005eb443e5aSMiklos Szeredi splice_from_pipe_begin(&sd); 1006eb443e5aSMiklos Szeredi do { 1007eb443e5aSMiklos Szeredi ret = splice_from_pipe_next(pipe, &sd); 1008eb443e5aSMiklos Szeredi if (ret <= 0) 1009eb443e5aSMiklos Szeredi break; 1010eb443e5aSMiklos Szeredi 1011eb443e5aSMiklos Szeredi mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 1012eb443e5aSMiklos Szeredi ret = file_remove_suid(out); 1013723590edSMiklos Szeredi if (!ret) { 1014c3b2da31SJosef Bacik ret = file_update_time(out); 1015c3b2da31SJosef Bacik if (!ret) 1016c3b2da31SJosef Bacik ret = splice_from_pipe_feed(pipe, &sd, 1017c3b2da31SJosef Bacik pipe_to_file); 1018723590edSMiklos Szeredi } 1019eb443e5aSMiklos Szeredi mutex_unlock(&inode->i_mutex); 1020eb443e5aSMiklos Szeredi } while (ret > 0); 1021eb443e5aSMiklos Szeredi splice_from_pipe_end(pipe, &sd); 1022eb443e5aSMiklos Szeredi 102361e0d47cSMiklos Szeredi pipe_unlock(pipe); 1024eb443e5aSMiklos Szeredi 1025eb443e5aSMiklos Szeredi if (sd.num_spliced) 1026eb443e5aSMiklos Szeredi ret = sd.num_spliced; 1027eb443e5aSMiklos Szeredi 1028a4514ebdSJens Axboe if (ret > 0) { 10297f3d4ee1SMiklos Szeredi int err; 10307f3d4ee1SMiklos Szeredi 1031148f948bSJan Kara err = generic_write_sync(out, *ppos, ret); 10324f6f0bd2SJens Axboe if (err) 10334f6f0bd2SJens Axboe ret = err; 1034148f948bSJan Kara else 1035148f948bSJan Kara *ppos += ret; 1036d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(mapping); 1037a4514ebdSJens Axboe } 10384f6f0bd2SJens Axboe 10394f6f0bd2SJens Axboe return ret; 10405274f052SJens Axboe } 10415274f052SJens Axboe 1042059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_write); 1043059a8f37SJens Axboe 1044b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 1045b2858d7dSMiklos Szeredi struct splice_desc *sd) 10460b0a47f5SMiklos Szeredi { 1047b2858d7dSMiklos Szeredi int ret; 1048b2858d7dSMiklos Szeredi void *data; 104906ae43f3SAl Viro loff_t tmp = sd->pos; 1050b2858d7dSMiklos Szeredi 1051b2858d7dSMiklos Szeredi data = buf->ops->map(pipe, buf, 0); 105206ae43f3SAl Viro ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); 1053b2858d7dSMiklos Szeredi buf->ops->unmap(pipe, buf, data); 1054b2858d7dSMiklos Szeredi 1055b2858d7dSMiklos Szeredi return ret; 10560b0a47f5SMiklos Szeredi } 10570b0a47f5SMiklos Szeredi 10580b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, 10590b0a47f5SMiklos Szeredi struct file *out, loff_t *ppos, 10600b0a47f5SMiklos Szeredi size_t len, unsigned int flags) 10610b0a47f5SMiklos Szeredi { 1062b2858d7dSMiklos Szeredi ssize_t ret; 10630b0a47f5SMiklos Szeredi 1064b2858d7dSMiklos Szeredi ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); 1065b2858d7dSMiklos Szeredi if (ret > 0) 1066b2858d7dSMiklos Szeredi *ppos += ret; 10670b0a47f5SMiklos Szeredi 1068b2858d7dSMiklos Szeredi return ret; 10690b0a47f5SMiklos Szeredi } 10700b0a47f5SMiklos Szeredi 107183f9135bSJens Axboe /** 107283f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 1073932cc6d4SJens Axboe * @pipe: pipe to splice from 107483f9135bSJens Axboe * @out: socket to write to 1075932cc6d4SJens Axboe * @ppos: position in @out 107683f9135bSJens Axboe * @len: number of bytes to splice 107783f9135bSJens Axboe * @flags: splice modifier flags 107883f9135bSJens Axboe * 1079932cc6d4SJens Axboe * Description: 108083f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 108183f9135bSJens Axboe * is involved. 108283f9135bSJens Axboe * 108383f9135bSJens Axboe */ 10843a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 1085cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 10865274f052SJens Axboe { 108700522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 10885274f052SJens Axboe } 10895274f052SJens Axboe 1090059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 1091a0f06780SJeff Garzik 109283f9135bSJens Axboe /* 109383f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 109483f9135bSJens Axboe */ 10953a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 1096cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 10975274f052SJens Axboe { 10980b0a47f5SMiklos Szeredi ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 10990b0a47f5SMiklos Szeredi loff_t *, size_t, unsigned int); 11005274f052SJens Axboe int ret; 11015274f052SJens Axboe 110249570e9bSJens Axboe if (unlikely(!(out->f_mode & FMODE_WRITE))) 11035274f052SJens Axboe return -EBADF; 11045274f052SJens Axboe 1105efc968d4SLinus Torvalds if (unlikely(out->f_flags & O_APPEND)) 1106efc968d4SLinus Torvalds return -EINVAL; 1107efc968d4SLinus Torvalds 1108cbb7e577SJens Axboe ret = rw_verify_area(WRITE, out, ppos, len); 11095274f052SJens Axboe if (unlikely(ret < 0)) 11105274f052SJens Axboe return ret; 11115274f052SJens Axboe 1112cc56f7deSChangli Gao if (out->f_op && out->f_op->splice_write) 11130b0a47f5SMiklos Szeredi splice_write = out->f_op->splice_write; 1114cc56f7deSChangli Gao else 11150b0a47f5SMiklos Szeredi splice_write = default_file_splice_write; 11160b0a47f5SMiklos Szeredi 11172dd8c9adSAl Viro file_start_write(out); 11182dd8c9adSAl Viro ret = splice_write(pipe, out, ppos, len, flags); 11192dd8c9adSAl Viro file_end_write(out); 11202dd8c9adSAl Viro return ret; 11215274f052SJens Axboe } 11225274f052SJens Axboe 112383f9135bSJens Axboe /* 112483f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 112583f9135bSJens Axboe */ 1126cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 1127cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 1128cbb7e577SJens Axboe unsigned int flags) 11295274f052SJens Axboe { 11306818173bSMiklos Szeredi ssize_t (*splice_read)(struct file *, loff_t *, 11316818173bSMiklos Szeredi struct pipe_inode_info *, size_t, unsigned int); 11325274f052SJens Axboe int ret; 11335274f052SJens Axboe 113449570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 11355274f052SJens Axboe return -EBADF; 11365274f052SJens Axboe 1137cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 11385274f052SJens Axboe if (unlikely(ret < 0)) 11395274f052SJens Axboe return ret; 11405274f052SJens Axboe 1141cc56f7deSChangli Gao if (in->f_op && in->f_op->splice_read) 11426818173bSMiklos Szeredi splice_read = in->f_op->splice_read; 1143cc56f7deSChangli Gao else 11446818173bSMiklos Szeredi splice_read = default_file_splice_read; 11456818173bSMiklos Szeredi 11466818173bSMiklos Szeredi return splice_read(in, ppos, pipe, len, flags); 11475274f052SJens Axboe } 11485274f052SJens Axboe 1149932cc6d4SJens Axboe /** 1150932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 1151932cc6d4SJens Axboe * @in: file to splice from 1152932cc6d4SJens Axboe * @sd: actor information on where to splice to 1153932cc6d4SJens Axboe * @actor: handles the data splicing 1154932cc6d4SJens Axboe * 1155932cc6d4SJens Axboe * Description: 1156932cc6d4SJens Axboe * This is a special case helper to splice directly between two 1157932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 1158932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 1159932cc6d4SJens Axboe * that process. 1160932cc6d4SJens Axboe * 1161c66ab6faSJens Axboe */ 1162c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 1163c66ab6faSJens Axboe splice_direct_actor *actor) 1164b92ce558SJens Axboe { 1165b92ce558SJens Axboe struct pipe_inode_info *pipe; 1166b92ce558SJens Axboe long ret, bytes; 1167b92ce558SJens Axboe umode_t i_mode; 1168c66ab6faSJens Axboe size_t len; 1169c66ab6faSJens Axboe int i, flags; 1170b92ce558SJens Axboe 1171b92ce558SJens Axboe /* 1172b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 1173b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 1174b92ce558SJens Axboe * piped splicing for that! 1175b92ce558SJens Axboe */ 1176496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 1177b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 1178b92ce558SJens Axboe return -EINVAL; 1179b92ce558SJens Axboe 1180b92ce558SJens Axboe /* 1181b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 1182b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 1183b92ce558SJens Axboe */ 1184b92ce558SJens Axboe pipe = current->splice_pipe; 118549570e9bSJens Axboe if (unlikely(!pipe)) { 11867bee130eSAl Viro pipe = alloc_pipe_info(); 1187b92ce558SJens Axboe if (!pipe) 1188b92ce558SJens Axboe return -ENOMEM; 1189b92ce558SJens Axboe 1190b92ce558SJens Axboe /* 1191b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 119200522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 1193b92ce558SJens Axboe * PIPE_READERS appropriately. 1194b92ce558SJens Axboe */ 1195b92ce558SJens Axboe pipe->readers = 1; 1196b92ce558SJens Axboe 1197b92ce558SJens Axboe current->splice_pipe = pipe; 1198b92ce558SJens Axboe } 1199b92ce558SJens Axboe 1200b92ce558SJens Axboe /* 120173d62d83SIngo Molnar * Do the splice. 1202b92ce558SJens Axboe */ 1203b92ce558SJens Axboe ret = 0; 1204b92ce558SJens Axboe bytes = 0; 1205c66ab6faSJens Axboe len = sd->total_len; 1206c66ab6faSJens Axboe flags = sd->flags; 1207c66ab6faSJens Axboe 1208c66ab6faSJens Axboe /* 1209c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 1210c66ab6faSJens Axboe */ 1211c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 1212b92ce558SJens Axboe 1213b92ce558SJens Axboe while (len) { 121451a92c0fSJens Axboe size_t read_len; 1215a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 1216b92ce558SJens Axboe 1217bcd4f3acSJens Axboe ret = do_splice_to(in, &pos, pipe, len, flags); 121851a92c0fSJens Axboe if (unlikely(ret <= 0)) 1219b92ce558SJens Axboe goto out_release; 1220b92ce558SJens Axboe 1221b92ce558SJens Axboe read_len = ret; 1222c66ab6faSJens Axboe sd->total_len = read_len; 1223b92ce558SJens Axboe 1224b92ce558SJens Axboe /* 1225b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 1226b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 1227b92ce558SJens Axboe * could get stuck data in the internal pipe: 1228b92ce558SJens Axboe */ 1229c66ab6faSJens Axboe ret = actor(pipe, sd); 1230a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 1231a82c53a0STom Zanussi sd->pos = prev_pos; 1232b92ce558SJens Axboe goto out_release; 1233a82c53a0STom Zanussi } 1234b92ce558SJens Axboe 1235b92ce558SJens Axboe bytes += ret; 1236b92ce558SJens Axboe len -= ret; 1237bcd4f3acSJens Axboe sd->pos = pos; 1238b92ce558SJens Axboe 1239a82c53a0STom Zanussi if (ret < read_len) { 1240a82c53a0STom Zanussi sd->pos = prev_pos + ret; 124151a92c0fSJens Axboe goto out_release; 1242b92ce558SJens Axboe } 1243a82c53a0STom Zanussi } 1244b92ce558SJens Axboe 12459e97198dSJens Axboe done: 1246b92ce558SJens Axboe pipe->nrbufs = pipe->curbuf = 0; 12479e97198dSJens Axboe file_accessed(in); 1248b92ce558SJens Axboe return bytes; 1249b92ce558SJens Axboe 1250b92ce558SJens Axboe out_release: 1251b92ce558SJens Axboe /* 1252b92ce558SJens Axboe * If we did an incomplete transfer we must release 1253b92ce558SJens Axboe * the pipe buffers in question: 1254b92ce558SJens Axboe */ 125535f3d14dSJens Axboe for (i = 0; i < pipe->buffers; i++) { 1256b92ce558SJens Axboe struct pipe_buffer *buf = pipe->bufs + i; 1257b92ce558SJens Axboe 1258b92ce558SJens Axboe if (buf->ops) { 1259b92ce558SJens Axboe buf->ops->release(pipe, buf); 1260b92ce558SJens Axboe buf->ops = NULL; 1261b92ce558SJens Axboe } 1262b92ce558SJens Axboe } 1263b92ce558SJens Axboe 12649e97198dSJens Axboe if (!bytes) 12659e97198dSJens Axboe bytes = ret; 1266b92ce558SJens Axboe 12679e97198dSJens Axboe goto done; 1268c66ab6faSJens Axboe } 1269c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1270c66ab6faSJens Axboe 1271c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1272c66ab6faSJens Axboe struct splice_desc *sd) 1273c66ab6faSJens Axboe { 12746a14b90bSJens Axboe struct file *file = sd->u.file; 1275c66ab6faSJens Axboe 12762cb4b05eSChangli Gao return do_splice_from(pipe, file, &file->f_pos, sd->total_len, 12772cb4b05eSChangli Gao sd->flags); 1278c66ab6faSJens Axboe } 1279c66ab6faSJens Axboe 1280932cc6d4SJens Axboe /** 1281932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1282932cc6d4SJens Axboe * @in: file to splice from 1283932cc6d4SJens Axboe * @ppos: input file offset 1284932cc6d4SJens Axboe * @out: file to splice to 1285932cc6d4SJens Axboe * @len: number of bytes to splice 1286932cc6d4SJens Axboe * @flags: splice modifier flags 1287932cc6d4SJens Axboe * 1288932cc6d4SJens Axboe * Description: 1289932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1290932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1291932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1292932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1293932cc6d4SJens Axboe * 1294932cc6d4SJens Axboe */ 1295c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 1296c66ab6faSJens Axboe size_t len, unsigned int flags) 1297c66ab6faSJens Axboe { 1298c66ab6faSJens Axboe struct splice_desc sd = { 1299c66ab6faSJens Axboe .len = len, 1300c66ab6faSJens Axboe .total_len = len, 1301c66ab6faSJens Axboe .flags = flags, 1302c66ab6faSJens Axboe .pos = *ppos, 13036a14b90bSJens Axboe .u.file = out, 1304c66ab6faSJens Axboe }; 130551a92c0fSJens Axboe long ret; 1306c66ab6faSJens Axboe 1307c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 130851a92c0fSJens Axboe if (ret > 0) 1309a82c53a0STom Zanussi *ppos = sd.pos; 131051a92c0fSJens Axboe 1311c66ab6faSJens Axboe return ret; 1312b92ce558SJens Axboe } 1313b92ce558SJens Axboe 13147c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 13157c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 13167c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1317ddac0d39SJens Axboe 1318ddac0d39SJens Axboe /* 131983f9135bSJens Axboe * Determine where to splice to/from. 132083f9135bSJens Axboe */ 1321529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in, 1322529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 1323529565dcSIngo Molnar size_t len, unsigned int flags) 13245274f052SJens Axboe { 13257c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 13267c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 1327cbb7e577SJens Axboe loff_t offset, *off; 1328a4514ebdSJens Axboe long ret; 13295274f052SJens Axboe 133071993e62SLinus Torvalds ipipe = get_pipe_info(in); 133171993e62SLinus Torvalds opipe = get_pipe_info(out); 13327c77f0b3SMiklos Szeredi 13337c77f0b3SMiklos Szeredi if (ipipe && opipe) { 13347c77f0b3SMiklos Szeredi if (off_in || off_out) 13357c77f0b3SMiklos Szeredi return -ESPIPE; 13367c77f0b3SMiklos Szeredi 13377c77f0b3SMiklos Szeredi if (!(in->f_mode & FMODE_READ)) 13387c77f0b3SMiklos Szeredi return -EBADF; 13397c77f0b3SMiklos Szeredi 13407c77f0b3SMiklos Szeredi if (!(out->f_mode & FMODE_WRITE)) 13417c77f0b3SMiklos Szeredi return -EBADF; 13427c77f0b3SMiklos Szeredi 13437c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 13447c77f0b3SMiklos Szeredi if (ipipe == opipe) 13457c77f0b3SMiklos Szeredi return -EINVAL; 13467c77f0b3SMiklos Szeredi 13477c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 13487c77f0b3SMiklos Szeredi } 13497c77f0b3SMiklos Szeredi 13507c77f0b3SMiklos Szeredi if (ipipe) { 1351529565dcSIngo Molnar if (off_in) 1352529565dcSIngo Molnar return -ESPIPE; 1353b92ce558SJens Axboe if (off_out) { 135419c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1355b92ce558SJens Axboe return -EINVAL; 1356cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1357b92ce558SJens Axboe return -EFAULT; 1358cbb7e577SJens Axboe off = &offset; 1359cbb7e577SJens Axboe } else 1360cbb7e577SJens Axboe off = &out->f_pos; 1361529565dcSIngo Molnar 13627c77f0b3SMiklos Szeredi ret = do_splice_from(ipipe, out, off, len, flags); 1363a4514ebdSJens Axboe 1364a4514ebdSJens Axboe if (off_out && copy_to_user(off_out, off, sizeof(loff_t))) 1365a4514ebdSJens Axboe ret = -EFAULT; 1366a4514ebdSJens Axboe 1367a4514ebdSJens Axboe return ret; 1368529565dcSIngo Molnar } 13695274f052SJens Axboe 13707c77f0b3SMiklos Szeredi if (opipe) { 1371529565dcSIngo Molnar if (off_out) 1372529565dcSIngo Molnar return -ESPIPE; 1373b92ce558SJens Axboe if (off_in) { 137419c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1375b92ce558SJens Axboe return -EINVAL; 1376cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1377b92ce558SJens Axboe return -EFAULT; 1378cbb7e577SJens Axboe off = &offset; 1379cbb7e577SJens Axboe } else 1380cbb7e577SJens Axboe off = &in->f_pos; 1381529565dcSIngo Molnar 13827c77f0b3SMiklos Szeredi ret = do_splice_to(in, off, opipe, len, flags); 1383a4514ebdSJens Axboe 1384a4514ebdSJens Axboe if (off_in && copy_to_user(off_in, off, sizeof(loff_t))) 1385a4514ebdSJens Axboe ret = -EFAULT; 1386a4514ebdSJens Axboe 1387a4514ebdSJens Axboe return ret; 1388529565dcSIngo Molnar } 13895274f052SJens Axboe 13905274f052SJens Axboe return -EINVAL; 13915274f052SJens Axboe } 13925274f052SJens Axboe 1393912d35f8SJens Axboe /* 1394912d35f8SJens Axboe * Map an iov into an array of pages and offset/length tupples. With the 1395912d35f8SJens Axboe * partial_page structure, we can map several non-contiguous ranges into 1396912d35f8SJens Axboe * our ones pages[] map instead of splitting that operation into pieces. 1397912d35f8SJens Axboe * Could easily be exported as a generic helper for other users, in which 1398912d35f8SJens Axboe * case one would probably want to add a 'max_nr_pages' parameter as well. 1399912d35f8SJens Axboe */ 1400912d35f8SJens Axboe static int get_iovec_page_array(const struct iovec __user *iov, 1401912d35f8SJens Axboe unsigned int nr_vecs, struct page **pages, 1402bd1a68b5SEric Dumazet struct partial_page *partial, bool aligned, 140335f3d14dSJens Axboe unsigned int pipe_buffers) 1404912d35f8SJens Axboe { 1405912d35f8SJens Axboe int buffers = 0, error = 0; 1406912d35f8SJens Axboe 1407912d35f8SJens Axboe while (nr_vecs) { 1408912d35f8SJens Axboe unsigned long off, npages; 140975723957SLinus Torvalds struct iovec entry; 1410912d35f8SJens Axboe void __user *base; 1411912d35f8SJens Axboe size_t len; 1412912d35f8SJens Axboe int i; 1413912d35f8SJens Axboe 141475723957SLinus Torvalds error = -EFAULT; 1415bc40d73cSNick Piggin if (copy_from_user(&entry, iov, sizeof(entry))) 1416912d35f8SJens Axboe break; 141775723957SLinus Torvalds 141875723957SLinus Torvalds base = entry.iov_base; 141975723957SLinus Torvalds len = entry.iov_len; 1420912d35f8SJens Axboe 1421912d35f8SJens Axboe /* 1422912d35f8SJens Axboe * Sanity check this iovec. 0 read succeeds. 1423912d35f8SJens Axboe */ 142475723957SLinus Torvalds error = 0; 1425912d35f8SJens Axboe if (unlikely(!len)) 1426912d35f8SJens Axboe break; 1427912d35f8SJens Axboe error = -EFAULT; 1428712a30e6SBastian Blank if (!access_ok(VERIFY_READ, base, len)) 1429912d35f8SJens Axboe break; 1430912d35f8SJens Axboe 1431912d35f8SJens Axboe /* 1432912d35f8SJens Axboe * Get this base offset and number of pages, then map 1433912d35f8SJens Axboe * in the user pages. 1434912d35f8SJens Axboe */ 1435912d35f8SJens Axboe off = (unsigned long) base & ~PAGE_MASK; 14367afa6fd0SJens Axboe 14377afa6fd0SJens Axboe /* 14387afa6fd0SJens Axboe * If asked for alignment, the offset must be zero and the 14397afa6fd0SJens Axboe * length a multiple of the PAGE_SIZE. 14407afa6fd0SJens Axboe */ 14417afa6fd0SJens Axboe error = -EINVAL; 14427afa6fd0SJens Axboe if (aligned && (off || len & ~PAGE_MASK)) 14437afa6fd0SJens Axboe break; 14447afa6fd0SJens Axboe 1445912d35f8SJens Axboe npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 144635f3d14dSJens Axboe if (npages > pipe_buffers - buffers) 144735f3d14dSJens Axboe npages = pipe_buffers - buffers; 1448912d35f8SJens Axboe 1449bc40d73cSNick Piggin error = get_user_pages_fast((unsigned long)base, npages, 1450bc40d73cSNick Piggin 0, &pages[buffers]); 1451912d35f8SJens Axboe 1452912d35f8SJens Axboe if (unlikely(error <= 0)) 1453912d35f8SJens Axboe break; 1454912d35f8SJens Axboe 1455912d35f8SJens Axboe /* 1456912d35f8SJens Axboe * Fill this contiguous range into the partial page map. 1457912d35f8SJens Axboe */ 1458912d35f8SJens Axboe for (i = 0; i < error; i++) { 14597591489aSJens Axboe const int plen = min_t(size_t, len, PAGE_SIZE - off); 1460912d35f8SJens Axboe 1461912d35f8SJens Axboe partial[buffers].offset = off; 1462912d35f8SJens Axboe partial[buffers].len = plen; 1463912d35f8SJens Axboe 1464912d35f8SJens Axboe off = 0; 1465912d35f8SJens Axboe len -= plen; 1466912d35f8SJens Axboe buffers++; 1467912d35f8SJens Axboe } 1468912d35f8SJens Axboe 1469912d35f8SJens Axboe /* 1470912d35f8SJens Axboe * We didn't complete this iov, stop here since it probably 1471912d35f8SJens Axboe * means we have to move some of this into a pipe to 1472912d35f8SJens Axboe * be able to continue. 1473912d35f8SJens Axboe */ 1474912d35f8SJens Axboe if (len) 1475912d35f8SJens Axboe break; 1476912d35f8SJens Axboe 1477912d35f8SJens Axboe /* 1478912d35f8SJens Axboe * Don't continue if we mapped fewer pages than we asked for, 1479912d35f8SJens Axboe * or if we mapped the max number of pages that we have 1480912d35f8SJens Axboe * room for. 1481912d35f8SJens Axboe */ 148235f3d14dSJens Axboe if (error < npages || buffers == pipe_buffers) 1483912d35f8SJens Axboe break; 1484912d35f8SJens Axboe 1485912d35f8SJens Axboe nr_vecs--; 1486912d35f8SJens Axboe iov++; 1487912d35f8SJens Axboe } 1488912d35f8SJens Axboe 1489912d35f8SJens Axboe if (buffers) 1490912d35f8SJens Axboe return buffers; 1491912d35f8SJens Axboe 1492912d35f8SJens Axboe return error; 1493912d35f8SJens Axboe } 1494912d35f8SJens Axboe 14956a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 14966a14b90bSJens Axboe struct splice_desc *sd) 14976a14b90bSJens Axboe { 14986a14b90bSJens Axboe char *src; 14996a14b90bSJens Axboe int ret; 15006a14b90bSJens Axboe 15016a14b90bSJens Axboe /* 15026a14b90bSJens Axboe * See if we can use the atomic maps, by prefaulting in the 15036a14b90bSJens Axboe * pages and doing an atomic copy 15046a14b90bSJens Axboe */ 15056a14b90bSJens Axboe if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) { 15066a14b90bSJens Axboe src = buf->ops->map(pipe, buf, 1); 15076a14b90bSJens Axboe ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset, 15086a14b90bSJens Axboe sd->len); 15096a14b90bSJens Axboe buf->ops->unmap(pipe, buf, src); 15106a14b90bSJens Axboe if (!ret) { 15116a14b90bSJens Axboe ret = sd->len; 15126a14b90bSJens Axboe goto out; 15136a14b90bSJens Axboe } 15146a14b90bSJens Axboe } 15156a14b90bSJens Axboe 15166a14b90bSJens Axboe /* 15176a14b90bSJens Axboe * No dice, use slow non-atomic map and copy 15186a14b90bSJens Axboe */ 15196a14b90bSJens Axboe src = buf->ops->map(pipe, buf, 0); 15206a14b90bSJens Axboe 15216a14b90bSJens Axboe ret = sd->len; 15226a14b90bSJens Axboe if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len)) 15236a14b90bSJens Axboe ret = -EFAULT; 15246a14b90bSJens Axboe 15256866bef4SJens Axboe buf->ops->unmap(pipe, buf, src); 15266a14b90bSJens Axboe out: 15276a14b90bSJens Axboe if (ret > 0) 15286a14b90bSJens Axboe sd->u.userptr += ret; 15296a14b90bSJens Axboe return ret; 15306a14b90bSJens Axboe } 15316a14b90bSJens Axboe 15326a14b90bSJens Axboe /* 15336a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 15346a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 15356a14b90bSJens Axboe */ 15366a14b90bSJens Axboe static long vmsplice_to_user(struct file *file, const struct iovec __user *iov, 15376a14b90bSJens Axboe unsigned long nr_segs, unsigned int flags) 15386a14b90bSJens Axboe { 15396a14b90bSJens Axboe struct pipe_inode_info *pipe; 15406a14b90bSJens Axboe struct splice_desc sd; 15416a14b90bSJens Axboe ssize_t size; 15426a14b90bSJens Axboe int error; 15436a14b90bSJens Axboe long ret; 15446a14b90bSJens Axboe 154571993e62SLinus Torvalds pipe = get_pipe_info(file); 15466a14b90bSJens Axboe if (!pipe) 15476a14b90bSJens Axboe return -EBADF; 15486a14b90bSJens Axboe 154961e0d47cSMiklos Szeredi pipe_lock(pipe); 15506a14b90bSJens Axboe 15516a14b90bSJens Axboe error = ret = 0; 15526a14b90bSJens Axboe while (nr_segs) { 15536a14b90bSJens Axboe void __user *base; 15546a14b90bSJens Axboe size_t len; 15556a14b90bSJens Axboe 15566a14b90bSJens Axboe /* 15576a14b90bSJens Axboe * Get user address base and length for this iovec. 15586a14b90bSJens Axboe */ 15596a14b90bSJens Axboe error = get_user(base, &iov->iov_base); 15606a14b90bSJens Axboe if (unlikely(error)) 15616a14b90bSJens Axboe break; 15626a14b90bSJens Axboe error = get_user(len, &iov->iov_len); 15636a14b90bSJens Axboe if (unlikely(error)) 15646a14b90bSJens Axboe break; 15656a14b90bSJens Axboe 15666a14b90bSJens Axboe /* 15676a14b90bSJens Axboe * Sanity check this iovec. 0 read succeeds. 15686a14b90bSJens Axboe */ 15696a14b90bSJens Axboe if (unlikely(!len)) 15706a14b90bSJens Axboe break; 15716a14b90bSJens Axboe if (unlikely(!base)) { 15726a14b90bSJens Axboe error = -EFAULT; 15736a14b90bSJens Axboe break; 15746a14b90bSJens Axboe } 15756a14b90bSJens Axboe 15768811930dSJens Axboe if (unlikely(!access_ok(VERIFY_WRITE, base, len))) { 15778811930dSJens Axboe error = -EFAULT; 15788811930dSJens Axboe break; 15798811930dSJens Axboe } 15808811930dSJens Axboe 15816a14b90bSJens Axboe sd.len = 0; 15826a14b90bSJens Axboe sd.total_len = len; 15836a14b90bSJens Axboe sd.flags = flags; 15846a14b90bSJens Axboe sd.u.userptr = base; 15856a14b90bSJens Axboe sd.pos = 0; 15866a14b90bSJens Axboe 15876a14b90bSJens Axboe size = __splice_from_pipe(pipe, &sd, pipe_to_user); 15886a14b90bSJens Axboe if (size < 0) { 15896a14b90bSJens Axboe if (!ret) 15906a14b90bSJens Axboe ret = size; 15916a14b90bSJens Axboe 15926a14b90bSJens Axboe break; 15936a14b90bSJens Axboe } 15946a14b90bSJens Axboe 15956a14b90bSJens Axboe ret += size; 15966a14b90bSJens Axboe 15976a14b90bSJens Axboe if (size < len) 15986a14b90bSJens Axboe break; 15996a14b90bSJens Axboe 16006a14b90bSJens Axboe nr_segs--; 16016a14b90bSJens Axboe iov++; 16026a14b90bSJens Axboe } 16036a14b90bSJens Axboe 160461e0d47cSMiklos Szeredi pipe_unlock(pipe); 16056a14b90bSJens Axboe 16066a14b90bSJens Axboe if (!ret) 16076a14b90bSJens Axboe ret = error; 16086a14b90bSJens Axboe 16096a14b90bSJens Axboe return ret; 16106a14b90bSJens Axboe } 16116a14b90bSJens Axboe 1612912d35f8SJens Axboe /* 1613912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1614912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1615912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1616912d35f8SJens Axboe */ 16176a14b90bSJens Axboe static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov, 1618912d35f8SJens Axboe unsigned long nr_segs, unsigned int flags) 1619912d35f8SJens Axboe { 1620ddac0d39SJens Axboe struct pipe_inode_info *pipe; 162135f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 162235f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 1623912d35f8SJens Axboe struct splice_pipe_desc spd = { 1624912d35f8SJens Axboe .pages = pages, 1625912d35f8SJens Axboe .partial = partial, 1626047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1627912d35f8SJens Axboe .flags = flags, 1628912d35f8SJens Axboe .ops = &user_page_pipe_buf_ops, 1629bbdfc2f7SJens Axboe .spd_release = spd_release_page, 1630912d35f8SJens Axboe }; 163135f3d14dSJens Axboe long ret; 1632912d35f8SJens Axboe 163371993e62SLinus Torvalds pipe = get_pipe_info(file); 1634ddac0d39SJens Axboe if (!pipe) 1635912d35f8SJens Axboe return -EBADF; 1636912d35f8SJens Axboe 163735f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 163835f3d14dSJens Axboe return -ENOMEM; 1639912d35f8SJens Axboe 164035f3d14dSJens Axboe spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages, 1641bd1a68b5SEric Dumazet spd.partial, false, 1642047fe360SEric Dumazet spd.nr_pages_max); 164335f3d14dSJens Axboe if (spd.nr_pages <= 0) 164435f3d14dSJens Axboe ret = spd.nr_pages; 164535f3d14dSJens Axboe else 164635f3d14dSJens Axboe ret = splice_to_pipe(pipe, &spd); 164735f3d14dSJens Axboe 1648047fe360SEric Dumazet splice_shrink_spd(&spd); 164935f3d14dSJens Axboe return ret; 1650912d35f8SJens Axboe } 1651912d35f8SJens Axboe 16526a14b90bSJens Axboe /* 16536a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 16546a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 16556a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 16566a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 16576a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 16586a14b90bSJens Axboe * solutions for that: 16596a14b90bSJens Axboe * 16606a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 16616a14b90bSJens Axboe * do a regular read() on the buffer anyway. 16626a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 16636a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 16646a14b90bSJens Axboe * 16656a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 16666a14b90bSJens Axboe * 16676a14b90bSJens Axboe */ 1668836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, 1669836f92adSHeiko Carstens unsigned long, nr_segs, unsigned int, flags) 1670912d35f8SJens Axboe { 16712903ff01SAl Viro struct fd f; 1672912d35f8SJens Axboe long error; 1673912d35f8SJens Axboe 16746a14b90bSJens Axboe if (unlikely(nr_segs > UIO_MAXIOV)) 16756a14b90bSJens Axboe return -EINVAL; 16766a14b90bSJens Axboe else if (unlikely(!nr_segs)) 16776a14b90bSJens Axboe return 0; 16786a14b90bSJens Axboe 1679912d35f8SJens Axboe error = -EBADF; 16802903ff01SAl Viro f = fdget(fd); 16812903ff01SAl Viro if (f.file) { 16822903ff01SAl Viro if (f.file->f_mode & FMODE_WRITE) 16832903ff01SAl Viro error = vmsplice_to_pipe(f.file, iov, nr_segs, flags); 16842903ff01SAl Viro else if (f.file->f_mode & FMODE_READ) 16852903ff01SAl Viro error = vmsplice_to_user(f.file, iov, nr_segs, flags); 1686912d35f8SJens Axboe 16872903ff01SAl Viro fdput(f); 1688912d35f8SJens Axboe } 1689912d35f8SJens Axboe 1690912d35f8SJens Axboe return error; 1691912d35f8SJens Axboe } 1692912d35f8SJens Axboe 1693836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1694836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1695836f92adSHeiko Carstens size_t, len, unsigned int, flags) 16965274f052SJens Axboe { 16972903ff01SAl Viro struct fd in, out; 16985274f052SJens Axboe long error; 16995274f052SJens Axboe 17005274f052SJens Axboe if (unlikely(!len)) 17015274f052SJens Axboe return 0; 17025274f052SJens Axboe 17035274f052SJens Axboe error = -EBADF; 17042903ff01SAl Viro in = fdget(fd_in); 17052903ff01SAl Viro if (in.file) { 17062903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 17072903ff01SAl Viro out = fdget(fd_out); 17082903ff01SAl Viro if (out.file) { 17092903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 17102903ff01SAl Viro error = do_splice(in.file, off_in, 17112903ff01SAl Viro out.file, off_out, 1712529565dcSIngo Molnar len, flags); 17132903ff01SAl Viro fdput(out); 17145274f052SJens Axboe } 17155274f052SJens Axboe } 17162903ff01SAl Viro fdput(in); 17175274f052SJens Axboe } 17185274f052SJens Axboe return error; 17195274f052SJens Axboe } 172070524490SJens Axboe 172170524490SJens Axboe /* 1722aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1723aadd06e5SJens Axboe * return an appropriate error. 1724aadd06e5SJens Axboe */ 17257c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1726aadd06e5SJens Axboe { 1727aadd06e5SJens Axboe int ret; 1728aadd06e5SJens Axboe 1729aadd06e5SJens Axboe /* 1730aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1731aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1732aadd06e5SJens Axboe */ 1733aadd06e5SJens Axboe if (pipe->nrbufs) 1734aadd06e5SJens Axboe return 0; 1735aadd06e5SJens Axboe 1736aadd06e5SJens Axboe ret = 0; 173761e0d47cSMiklos Szeredi pipe_lock(pipe); 1738aadd06e5SJens Axboe 1739aadd06e5SJens Axboe while (!pipe->nrbufs) { 1740aadd06e5SJens Axboe if (signal_pending(current)) { 1741aadd06e5SJens Axboe ret = -ERESTARTSYS; 1742aadd06e5SJens Axboe break; 1743aadd06e5SJens Axboe } 1744aadd06e5SJens Axboe if (!pipe->writers) 1745aadd06e5SJens Axboe break; 1746aadd06e5SJens Axboe if (!pipe->waiting_writers) { 1747aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1748aadd06e5SJens Axboe ret = -EAGAIN; 1749aadd06e5SJens Axboe break; 1750aadd06e5SJens Axboe } 1751aadd06e5SJens Axboe } 1752aadd06e5SJens Axboe pipe_wait(pipe); 1753aadd06e5SJens Axboe } 1754aadd06e5SJens Axboe 175561e0d47cSMiklos Szeredi pipe_unlock(pipe); 1756aadd06e5SJens Axboe return ret; 1757aadd06e5SJens Axboe } 1758aadd06e5SJens Axboe 1759aadd06e5SJens Axboe /* 1760aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1761aadd06e5SJens Axboe * return an appropriate error. 1762aadd06e5SJens Axboe */ 17637c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1764aadd06e5SJens Axboe { 1765aadd06e5SJens Axboe int ret; 1766aadd06e5SJens Axboe 1767aadd06e5SJens Axboe /* 1768aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1769aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1770aadd06e5SJens Axboe */ 177135f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 1772aadd06e5SJens Axboe return 0; 1773aadd06e5SJens Axboe 1774aadd06e5SJens Axboe ret = 0; 177561e0d47cSMiklos Szeredi pipe_lock(pipe); 1776aadd06e5SJens Axboe 177735f3d14dSJens Axboe while (pipe->nrbufs >= pipe->buffers) { 1778aadd06e5SJens Axboe if (!pipe->readers) { 1779aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1780aadd06e5SJens Axboe ret = -EPIPE; 1781aadd06e5SJens Axboe break; 1782aadd06e5SJens Axboe } 1783aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1784aadd06e5SJens Axboe ret = -EAGAIN; 1785aadd06e5SJens Axboe break; 1786aadd06e5SJens Axboe } 1787aadd06e5SJens Axboe if (signal_pending(current)) { 1788aadd06e5SJens Axboe ret = -ERESTARTSYS; 1789aadd06e5SJens Axboe break; 1790aadd06e5SJens Axboe } 1791aadd06e5SJens Axboe pipe->waiting_writers++; 1792aadd06e5SJens Axboe pipe_wait(pipe); 1793aadd06e5SJens Axboe pipe->waiting_writers--; 1794aadd06e5SJens Axboe } 1795aadd06e5SJens Axboe 179661e0d47cSMiklos Szeredi pipe_unlock(pipe); 1797aadd06e5SJens Axboe return ret; 1798aadd06e5SJens Axboe } 1799aadd06e5SJens Axboe 1800aadd06e5SJens Axboe /* 18017c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 18027c77f0b3SMiklos Szeredi */ 18037c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 18047c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 18057c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 18067c77f0b3SMiklos Szeredi { 18077c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 18087c77f0b3SMiklos Szeredi int ret = 0, nbuf; 18097c77f0b3SMiklos Szeredi bool input_wakeup = false; 18107c77f0b3SMiklos Szeredi 18117c77f0b3SMiklos Szeredi 18127c77f0b3SMiklos Szeredi retry: 18137c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 18147c77f0b3SMiklos Szeredi if (ret) 18157c77f0b3SMiklos Szeredi return ret; 18167c77f0b3SMiklos Szeredi 18177c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 18187c77f0b3SMiklos Szeredi if (ret) 18197c77f0b3SMiklos Szeredi return ret; 18207c77f0b3SMiklos Szeredi 18217c77f0b3SMiklos Szeredi /* 18227c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 18237c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 18247c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 18257c77f0b3SMiklos Szeredi */ 18267c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 18277c77f0b3SMiklos Szeredi 18287c77f0b3SMiklos Szeredi do { 18297c77f0b3SMiklos Szeredi if (!opipe->readers) { 18307c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 18317c77f0b3SMiklos Szeredi if (!ret) 18327c77f0b3SMiklos Szeredi ret = -EPIPE; 18337c77f0b3SMiklos Szeredi break; 18347c77f0b3SMiklos Szeredi } 18357c77f0b3SMiklos Szeredi 18367c77f0b3SMiklos Szeredi if (!ipipe->nrbufs && !ipipe->writers) 18377c77f0b3SMiklos Szeredi break; 18387c77f0b3SMiklos Szeredi 18397c77f0b3SMiklos Szeredi /* 18407c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 18417c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 18427c77f0b3SMiklos Szeredi */ 184335f3d14dSJens Axboe if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) { 18447c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 18457c77f0b3SMiklos Szeredi if (ret) 18467c77f0b3SMiklos Szeredi break; 18477c77f0b3SMiklos Szeredi 18487c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 18497c77f0b3SMiklos Szeredi ret = -EAGAIN; 18507c77f0b3SMiklos Szeredi break; 18517c77f0b3SMiklos Szeredi } 18527c77f0b3SMiklos Szeredi 18537c77f0b3SMiklos Szeredi /* 18547c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 18557c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 18567c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 18577c77f0b3SMiklos Szeredi */ 18587c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 18597c77f0b3SMiklos Szeredi pipe_unlock(opipe); 18607c77f0b3SMiklos Szeredi goto retry; 18617c77f0b3SMiklos Szeredi } 18627c77f0b3SMiklos Szeredi 18637c77f0b3SMiklos Szeredi ibuf = ipipe->bufs + ipipe->curbuf; 186435f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 18657c77f0b3SMiklos Szeredi obuf = opipe->bufs + nbuf; 18667c77f0b3SMiklos Szeredi 18677c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 18687c77f0b3SMiklos Szeredi /* 18697c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 18707c77f0b3SMiklos Szeredi */ 18717c77f0b3SMiklos Szeredi *obuf = *ibuf; 18727c77f0b3SMiklos Szeredi ibuf->ops = NULL; 18737c77f0b3SMiklos Szeredi opipe->nrbufs++; 187435f3d14dSJens Axboe ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1); 18757c77f0b3SMiklos Szeredi ipipe->nrbufs--; 18767c77f0b3SMiklos Szeredi input_wakeup = true; 18777c77f0b3SMiklos Szeredi } else { 18787c77f0b3SMiklos Szeredi /* 18797c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 18807c77f0b3SMiklos Szeredi * so we can copy the contents over. 18817c77f0b3SMiklos Szeredi */ 18827c77f0b3SMiklos Szeredi ibuf->ops->get(ipipe, ibuf); 18837c77f0b3SMiklos Szeredi *obuf = *ibuf; 18847c77f0b3SMiklos Szeredi 18857c77f0b3SMiklos Szeredi /* 18867c77f0b3SMiklos Szeredi * Don't inherit the gift flag, we need to 18877c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 18887c77f0b3SMiklos Szeredi */ 18897c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 18907c77f0b3SMiklos Szeredi 18917c77f0b3SMiklos Szeredi obuf->len = len; 18927c77f0b3SMiklos Szeredi opipe->nrbufs++; 18937c77f0b3SMiklos Szeredi ibuf->offset += obuf->len; 18947c77f0b3SMiklos Szeredi ibuf->len -= obuf->len; 18957c77f0b3SMiklos Szeredi } 18967c77f0b3SMiklos Szeredi ret += obuf->len; 18977c77f0b3SMiklos Szeredi len -= obuf->len; 18987c77f0b3SMiklos Szeredi } while (len); 18997c77f0b3SMiklos Szeredi 19007c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 19017c77f0b3SMiklos Szeredi pipe_unlock(opipe); 19027c77f0b3SMiklos Szeredi 19037c77f0b3SMiklos Szeredi /* 19047c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 19057c77f0b3SMiklos Szeredi */ 1906825cdcb1SNamhyung Kim if (ret > 0) 1907825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1908825cdcb1SNamhyung Kim 19097c77f0b3SMiklos Szeredi if (input_wakeup) 19107c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 19117c77f0b3SMiklos Szeredi 19127c77f0b3SMiklos Szeredi return ret; 19137c77f0b3SMiklos Szeredi } 19147c77f0b3SMiklos Szeredi 19157c77f0b3SMiklos Szeredi /* 191670524490SJens Axboe * Link contents of ipipe to opipe. 191770524490SJens Axboe */ 191870524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 191970524490SJens Axboe struct pipe_inode_info *opipe, 192070524490SJens Axboe size_t len, unsigned int flags) 192170524490SJens Axboe { 192270524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 1923aadd06e5SJens Axboe int ret = 0, i = 0, nbuf; 192470524490SJens Axboe 192570524490SJens Axboe /* 192670524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 192761e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 192870524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 192970524490SJens Axboe */ 193061e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 193170524490SJens Axboe 1932aadd06e5SJens Axboe do { 193370524490SJens Axboe if (!opipe->readers) { 193470524490SJens Axboe send_sig(SIGPIPE, current, 0); 193570524490SJens Axboe if (!ret) 193670524490SJens Axboe ret = -EPIPE; 193770524490SJens Axboe break; 193870524490SJens Axboe } 193970524490SJens Axboe 194070524490SJens Axboe /* 1941aadd06e5SJens Axboe * If we have iterated all input buffers or ran out of 1942aadd06e5SJens Axboe * output room, break. 194370524490SJens Axboe */ 194435f3d14dSJens Axboe if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) 1945aadd06e5SJens Axboe break; 1946aadd06e5SJens Axboe 194735f3d14dSJens Axboe ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1)); 194835f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 194970524490SJens Axboe 195070524490SJens Axboe /* 195170524490SJens Axboe * Get a reference to this pipe buffer, 195270524490SJens Axboe * so we can copy the contents over. 195370524490SJens Axboe */ 195470524490SJens Axboe ibuf->ops->get(ipipe, ibuf); 195570524490SJens Axboe 195670524490SJens Axboe obuf = opipe->bufs + nbuf; 195770524490SJens Axboe *obuf = *ibuf; 195870524490SJens Axboe 19597afa6fd0SJens Axboe /* 19607afa6fd0SJens Axboe * Don't inherit the gift flag, we need to 19617afa6fd0SJens Axboe * prevent multiple steals of this page. 19627afa6fd0SJens Axboe */ 19637afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 19647afa6fd0SJens Axboe 196570524490SJens Axboe if (obuf->len > len) 196670524490SJens Axboe obuf->len = len; 196770524490SJens Axboe 196870524490SJens Axboe opipe->nrbufs++; 196970524490SJens Axboe ret += obuf->len; 197070524490SJens Axboe len -= obuf->len; 1971aadd06e5SJens Axboe i++; 1972aadd06e5SJens Axboe } while (len); 197370524490SJens Axboe 197402cf01aeSJens Axboe /* 197502cf01aeSJens Axboe * return EAGAIN if we have the potential of some data in the 197602cf01aeSJens Axboe * future, otherwise just return 0 197702cf01aeSJens Axboe */ 197802cf01aeSJens Axboe if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) 197902cf01aeSJens Axboe ret = -EAGAIN; 198002cf01aeSJens Axboe 198161e0d47cSMiklos Szeredi pipe_unlock(ipipe); 198261e0d47cSMiklos Szeredi pipe_unlock(opipe); 198370524490SJens Axboe 1984aadd06e5SJens Axboe /* 1985aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1986aadd06e5SJens Axboe */ 1987825cdcb1SNamhyung Kim if (ret > 0) 1988825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 198970524490SJens Axboe 199070524490SJens Axboe return ret; 199170524490SJens Axboe } 199270524490SJens Axboe 199370524490SJens Axboe /* 199470524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 199570524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 199670524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 199770524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 199870524490SJens Axboe */ 199970524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len, 200070524490SJens Axboe unsigned int flags) 200170524490SJens Axboe { 200271993e62SLinus Torvalds struct pipe_inode_info *ipipe = get_pipe_info(in); 200371993e62SLinus Torvalds struct pipe_inode_info *opipe = get_pipe_info(out); 2004aadd06e5SJens Axboe int ret = -EINVAL; 200570524490SJens Axboe 200670524490SJens Axboe /* 2007aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 2008aadd06e5SJens Axboe * copying the data. 200970524490SJens Axboe */ 2010aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 2011aadd06e5SJens Axboe /* 2012aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 2013aadd06e5SJens Axboe * ordering doesn't really matter. 2014aadd06e5SJens Axboe */ 20157c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 2016aadd06e5SJens Axboe if (!ret) { 20177c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 201802cf01aeSJens Axboe if (!ret) 2019aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 2020aadd06e5SJens Axboe } 2021aadd06e5SJens Axboe } 202270524490SJens Axboe 2023aadd06e5SJens Axboe return ret; 202470524490SJens Axboe } 202570524490SJens Axboe 2026836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 202770524490SJens Axboe { 20282903ff01SAl Viro struct fd in; 20292903ff01SAl Viro int error; 203070524490SJens Axboe 203170524490SJens Axboe if (unlikely(!len)) 203270524490SJens Axboe return 0; 203370524490SJens Axboe 203470524490SJens Axboe error = -EBADF; 20352903ff01SAl Viro in = fdget(fdin); 20362903ff01SAl Viro if (in.file) { 20372903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 20382903ff01SAl Viro struct fd out = fdget(fdout); 20392903ff01SAl Viro if (out.file) { 20402903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 20412903ff01SAl Viro error = do_tee(in.file, out.file, 20422903ff01SAl Viro len, flags); 20432903ff01SAl Viro fdput(out); 204470524490SJens Axboe } 204570524490SJens Axboe } 20462903ff01SAl Viro fdput(in); 204770524490SJens Axboe } 204870524490SJens Axboe 204970524490SJens Axboe return error; 205070524490SJens Axboe } 2051