15274f052SJens Axboe /* 25274f052SJens Axboe * "splice": joining two ropes together by interweaving their strands. 35274f052SJens Axboe * 45274f052SJens Axboe * This is the "extended pipe" functionality, where a pipe is used as 55274f052SJens Axboe * an arbitrary in-memory buffer. Think of a pipe as a small kernel 65274f052SJens Axboe * buffer that you can use to transfer data from one end to the other. 75274f052SJens Axboe * 85274f052SJens Axboe * The traditional unix read/write is extended with a "splice()" operation 95274f052SJens Axboe * that transfers data buffers to or from a pipe buffer. 105274f052SJens Axboe * 115274f052SJens Axboe * Named by Larry McVoy, original implementation from Linus, extended by 12c2058e06SJens Axboe * Jens to support splicing to files, network, direct splicing, etc and 13c2058e06SJens Axboe * fixing lots of bugs. 145274f052SJens Axboe * 150fe23479SJens Axboe * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> 16c2058e06SJens Axboe * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> 17c2058e06SJens Axboe * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> 185274f052SJens Axboe * 195274f052SJens Axboe */ 205274f052SJens Axboe #include <linux/fs.h> 215274f052SJens Axboe #include <linux/file.h> 225274f052SJens Axboe #include <linux/pagemap.h> 23d6b29d7cSJens Axboe #include <linux/splice.h> 2408e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h> 255274f052SJens Axboe #include <linux/mm_inline.h> 265abc97aaSJens Axboe #include <linux/swap.h> 274f6f0bd2SJens Axboe #include <linux/writeback.h> 28630d9c47SPaul Gortmaker #include <linux/export.h> 294f6f0bd2SJens Axboe #include <linux/syscalls.h> 30912d35f8SJens Axboe #include <linux/uio.h> 3129ce2058SJames Morris #include <linux/security.h> 325a0e3ad6STejun Heo #include <linux/gfp.h> 3335f9c09fSEric Dumazet #include <linux/socket.h> 3476b021d0SAl Viro #include <linux/compat.h> 358d020765SAl Viro #include <linux/aio.h> 3606ae43f3SAl Viro #include "internal.h" 375274f052SJens Axboe 3883f9135bSJens Axboe /* 3983f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 4083f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 4183f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 4283f9135bSJens Axboe * attempt to reuse this page for another destination. 4383f9135bSJens Axboe */ 4476ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, 455abc97aaSJens Axboe struct pipe_buffer *buf) 465abc97aaSJens Axboe { 475abc97aaSJens Axboe struct page *page = buf->page; 489e94cd4fSJens Axboe struct address_space *mapping; 495abc97aaSJens Axboe 509e0267c2SJens Axboe lock_page(page); 519e0267c2SJens Axboe 529e94cd4fSJens Axboe mapping = page_mapping(page); 539e94cd4fSJens Axboe if (mapping) { 545abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 555abc97aaSJens Axboe 56ad8d6f0aSJens Axboe /* 579e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 589e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 599e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 609e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 619e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 629e94cd4fSJens Axboe * ensues. 63ad8d6f0aSJens Axboe */ 64ad8d6f0aSJens Axboe wait_on_page_writeback(page); 65ad8d6f0aSJens Axboe 66266cf658SDavid Howells if (page_has_private(page) && 67266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 68ca39d651SJens Axboe goto out_unlock; 694f6f0bd2SJens Axboe 709e94cd4fSJens Axboe /* 719e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 729e94cd4fSJens Axboe * and return good. 739e94cd4fSJens Axboe */ 749e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 751432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 765abc97aaSJens Axboe return 0; 775abc97aaSJens Axboe } 789e94cd4fSJens Axboe } 799e94cd4fSJens Axboe 809e94cd4fSJens Axboe /* 819e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 829e94cd4fSJens Axboe * address space, unlock and return failure. 839e94cd4fSJens Axboe */ 84ca39d651SJens Axboe out_unlock: 859e94cd4fSJens Axboe unlock_page(page); 869e94cd4fSJens Axboe return 1; 879e94cd4fSJens Axboe } 885abc97aaSJens Axboe 8976ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 905274f052SJens Axboe struct pipe_buffer *buf) 915274f052SJens Axboe { 925274f052SJens Axboe page_cache_release(buf->page); 931432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 945274f052SJens Axboe } 955274f052SJens Axboe 960845718dSJens Axboe /* 970845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 980845718dSJens Axboe * is a page cache page, IO may be in flight. 990845718dSJens Axboe */ 100cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 1015274f052SJens Axboe struct pipe_buffer *buf) 1025274f052SJens Axboe { 1035274f052SJens Axboe struct page *page = buf->page; 10449d0b21bSJens Axboe int err; 1055274f052SJens Axboe 1065274f052SJens Axboe if (!PageUptodate(page)) { 10749d0b21bSJens Axboe lock_page(page); 1085274f052SJens Axboe 10949d0b21bSJens Axboe /* 11049d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 11173d62d83SIngo Molnar * splice, if this is the first page. 11249d0b21bSJens Axboe */ 1135274f052SJens Axboe if (!page->mapping) { 11449d0b21bSJens Axboe err = -ENODATA; 11549d0b21bSJens Axboe goto error; 1165274f052SJens Axboe } 1175274f052SJens Axboe 11849d0b21bSJens Axboe /* 11973d62d83SIngo Molnar * Uh oh, read-error from disk. 12049d0b21bSJens Axboe */ 12149d0b21bSJens Axboe if (!PageUptodate(page)) { 12249d0b21bSJens Axboe err = -EIO; 12349d0b21bSJens Axboe goto error; 12449d0b21bSJens Axboe } 12549d0b21bSJens Axboe 12649d0b21bSJens Axboe /* 127f84d7519SJens Axboe * Page is ok afterall, we are done. 12849d0b21bSJens Axboe */ 12949d0b21bSJens Axboe unlock_page(page); 13049d0b21bSJens Axboe } 13149d0b21bSJens Axboe 132f84d7519SJens Axboe return 0; 13349d0b21bSJens Axboe error: 13449d0b21bSJens Axboe unlock_page(page); 135f84d7519SJens Axboe return err; 13670524490SJens Axboe } 13770524490SJens Axboe 138708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 1395274f052SJens Axboe .can_merge = 0, 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, 158cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 159912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 160912d35f8SJens Axboe .steal = user_page_pipe_buf_steal, 161f84d7519SJens Axboe .get = generic_pipe_buf_get, 162912d35f8SJens Axboe }; 163912d35f8SJens Axboe 164825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 165825cdcb1SNamhyung Kim { 166825cdcb1SNamhyung Kim smp_mb(); 167825cdcb1SNamhyung Kim if (waitqueue_active(&pipe->wait)) 168825cdcb1SNamhyung Kim wake_up_interruptible(&pipe->wait); 169825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 170825cdcb1SNamhyung Kim } 171825cdcb1SNamhyung Kim 172932cc6d4SJens Axboe /** 173932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 174932cc6d4SJens Axboe * @pipe: pipe to fill 175932cc6d4SJens Axboe * @spd: data to fill 176932cc6d4SJens Axboe * 177932cc6d4SJens Axboe * Description: 17879685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 179932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 180932cc6d4SJens Axboe * function will link that data to the pipe. 181932cc6d4SJens Axboe * 18283f9135bSJens Axboe */ 183d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 184912d35f8SJens Axboe struct splice_pipe_desc *spd) 1855274f052SJens Axboe { 18600de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 187912d35f8SJens Axboe int ret, do_wakeup, page_nr; 1885274f052SJens Axboe 1895274f052SJens Axboe ret = 0; 1905274f052SJens Axboe do_wakeup = 0; 191912d35f8SJens Axboe page_nr = 0; 1925274f052SJens Axboe 19361e0d47cSMiklos Szeredi pipe_lock(pipe); 1945274f052SJens Axboe 1955274f052SJens Axboe for (;;) { 1963a326a2cSIngo Molnar if (!pipe->readers) { 1975274f052SJens Axboe send_sig(SIGPIPE, current, 0); 1985274f052SJens Axboe if (!ret) 1995274f052SJens Axboe ret = -EPIPE; 2005274f052SJens Axboe break; 2015274f052SJens Axboe } 2025274f052SJens Axboe 20335f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) { 20435f3d14dSJens Axboe int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1); 2053a326a2cSIngo Molnar struct pipe_buffer *buf = pipe->bufs + newbuf; 2065274f052SJens Axboe 207912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 208912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 209912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 210497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 211912d35f8SJens Axboe buf->ops = spd->ops; 2127afa6fd0SJens Axboe if (spd->flags & SPLICE_F_GIFT) 2137afa6fd0SJens Axboe buf->flags |= PIPE_BUF_FLAG_GIFT; 2147afa6fd0SJens Axboe 2156f767b04SJens Axboe pipe->nrbufs++; 216912d35f8SJens Axboe page_nr++; 217912d35f8SJens Axboe ret += buf->len; 218912d35f8SJens Axboe 2196447a3cfSAl Viro if (pipe->files) 2205274f052SJens Axboe do_wakeup = 1; 2215274f052SJens Axboe 222912d35f8SJens Axboe if (!--spd->nr_pages) 2235274f052SJens Axboe break; 22435f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 2255274f052SJens Axboe continue; 2265274f052SJens Axboe 2275274f052SJens Axboe break; 2285274f052SJens Axboe } 2295274f052SJens Axboe 230912d35f8SJens Axboe if (spd->flags & SPLICE_F_NONBLOCK) { 23129e35094SLinus Torvalds if (!ret) 23229e35094SLinus Torvalds ret = -EAGAIN; 23329e35094SLinus Torvalds break; 23429e35094SLinus Torvalds } 23529e35094SLinus Torvalds 2365274f052SJens Axboe if (signal_pending(current)) { 2375274f052SJens Axboe if (!ret) 2385274f052SJens Axboe ret = -ERESTARTSYS; 2395274f052SJens Axboe break; 2405274f052SJens Axboe } 2415274f052SJens Axboe 2425274f052SJens Axboe if (do_wakeup) { 243c0bd1f65SJens Axboe smp_mb(); 2443a326a2cSIngo Molnar if (waitqueue_active(&pipe->wait)) 2453a326a2cSIngo Molnar wake_up_interruptible_sync(&pipe->wait); 2463a326a2cSIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 2475274f052SJens Axboe do_wakeup = 0; 2485274f052SJens Axboe } 2495274f052SJens Axboe 2503a326a2cSIngo Molnar pipe->waiting_writers++; 2513a326a2cSIngo Molnar pipe_wait(pipe); 2523a326a2cSIngo Molnar pipe->waiting_writers--; 2535274f052SJens Axboe } 2545274f052SJens Axboe 25561e0d47cSMiklos Szeredi pipe_unlock(pipe); 2565274f052SJens Axboe 257825cdcb1SNamhyung Kim if (do_wakeup) 258825cdcb1SNamhyung Kim wakeup_pipe_readers(pipe); 2595274f052SJens Axboe 26000de00bdSJens Axboe while (page_nr < spd_pages) 261bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2625274f052SJens Axboe 2635274f052SJens Axboe return ret; 2645274f052SJens Axboe } 2655274f052SJens Axboe 266708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) 267bbdfc2f7SJens Axboe { 268bbdfc2f7SJens Axboe page_cache_release(spd->pages[i]); 269bbdfc2f7SJens Axboe } 270bbdfc2f7SJens Axboe 27135f3d14dSJens Axboe /* 27235f3d14dSJens Axboe * Check if we need to grow the arrays holding pages and partial page 27335f3d14dSJens Axboe * descriptions. 27435f3d14dSJens Axboe */ 275047fe360SEric Dumazet int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 27635f3d14dSJens Axboe { 277047fe360SEric Dumazet unsigned int buffers = ACCESS_ONCE(pipe->buffers); 278047fe360SEric Dumazet 279047fe360SEric Dumazet spd->nr_pages_max = buffers; 280047fe360SEric Dumazet if (buffers <= PIPE_DEF_BUFFERS) 28135f3d14dSJens Axboe return 0; 28235f3d14dSJens Axboe 283047fe360SEric Dumazet spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL); 284047fe360SEric Dumazet spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL); 28535f3d14dSJens Axboe 28635f3d14dSJens Axboe if (spd->pages && spd->partial) 28735f3d14dSJens Axboe return 0; 28835f3d14dSJens Axboe 28935f3d14dSJens Axboe kfree(spd->pages); 29035f3d14dSJens Axboe kfree(spd->partial); 29135f3d14dSJens Axboe return -ENOMEM; 29235f3d14dSJens Axboe } 29335f3d14dSJens Axboe 294047fe360SEric Dumazet void splice_shrink_spd(struct splice_pipe_desc *spd) 29535f3d14dSJens Axboe { 296047fe360SEric Dumazet if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 29735f3d14dSJens Axboe return; 29835f3d14dSJens Axboe 29935f3d14dSJens Axboe kfree(spd->pages); 30035f3d14dSJens Axboe kfree(spd->partial); 30135f3d14dSJens Axboe } 30235f3d14dSJens Axboe 3033a326a2cSIngo Molnar static int 304cbb7e577SJens Axboe __generic_file_splice_read(struct file *in, loff_t *ppos, 305cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 306cbb7e577SJens Axboe unsigned int flags) 3075274f052SJens Axboe { 3085274f052SJens Axboe struct address_space *mapping = in->f_mapping; 309d8983910SFengguang Wu unsigned int loff, nr_pages, req_pages; 31035f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 31135f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 3125274f052SJens Axboe struct page *page; 31391ad66efSJens Axboe pgoff_t index, end_index; 31491ad66efSJens Axboe loff_t isize; 315eb20796bSJens Axboe int error, page_nr; 316912d35f8SJens Axboe struct splice_pipe_desc spd = { 317912d35f8SJens Axboe .pages = pages, 318912d35f8SJens Axboe .partial = partial, 319047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 320912d35f8SJens Axboe .flags = flags, 321912d35f8SJens Axboe .ops = &page_cache_pipe_buf_ops, 322bbdfc2f7SJens Axboe .spd_release = spd_release_page, 323912d35f8SJens Axboe }; 3245274f052SJens Axboe 32535f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 32635f3d14dSJens Axboe return -ENOMEM; 32735f3d14dSJens Axboe 328cbb7e577SJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 329912d35f8SJens Axboe loff = *ppos & ~PAGE_CACHE_MASK; 330d8983910SFengguang Wu req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 331047fe360SEric Dumazet nr_pages = min(req_pages, spd.nr_pages_max); 3325274f052SJens Axboe 3335274f052SJens Axboe /* 334eb20796bSJens Axboe * Lookup the (hopefully) full range of pages we need. 33582aa5d61SJens Axboe */ 33635f3d14dSJens Axboe spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages); 337431a4820SFengguang Wu index += spd.nr_pages; 338eb20796bSJens Axboe 3395274f052SJens Axboe /* 340eb20796bSJens Axboe * If find_get_pages_contig() returned fewer pages than we needed, 341431a4820SFengguang Wu * readahead/allocate the rest and fill in the holes. 342eb20796bSJens Axboe */ 343431a4820SFengguang Wu if (spd.nr_pages < nr_pages) 344cf914a7dSRusty Russell page_cache_sync_readahead(mapping, &in->f_ra, in, 345cf914a7dSRusty Russell index, req_pages - spd.nr_pages); 346431a4820SFengguang Wu 347932cc6d4SJens Axboe error = 0; 348eb20796bSJens Axboe while (spd.nr_pages < nr_pages) { 349eb20796bSJens Axboe /* 350eb20796bSJens Axboe * Page could be there, find_get_pages_contig() breaks on 351eb20796bSJens Axboe * the first hole. 3525274f052SJens Axboe */ 3537480a904SJens Axboe page = find_get_page(mapping, index); 3547480a904SJens Axboe if (!page) { 355e27dedd8SJens Axboe /* 356eb20796bSJens Axboe * page didn't exist, allocate one. 3577480a904SJens Axboe */ 3587480a904SJens Axboe page = page_cache_alloc_cold(mapping); 3595274f052SJens Axboe if (!page) 3605274f052SJens Axboe break; 3615274f052SJens Axboe 3627480a904SJens Axboe error = add_to_page_cache_lru(page, mapping, index, 3630ae0b5d0SNick Piggin GFP_KERNEL); 3645274f052SJens Axboe if (unlikely(error)) { 3655274f052SJens Axboe page_cache_release(page); 366a0548871SJens Axboe if (error == -EEXIST) 367a0548871SJens Axboe continue; 3685274f052SJens Axboe break; 3695274f052SJens Axboe } 370eb20796bSJens Axboe /* 371eb20796bSJens Axboe * add_to_page_cache() locks the page, unlock it 372eb20796bSJens Axboe * to avoid convoluting the logic below even more. 373eb20796bSJens Axboe */ 374eb20796bSJens Axboe unlock_page(page); 3755274f052SJens Axboe } 3767480a904SJens Axboe 37735f3d14dSJens Axboe spd.pages[spd.nr_pages++] = page; 378eb20796bSJens Axboe index++; 379eb20796bSJens Axboe } 380eb20796bSJens Axboe 381eb20796bSJens Axboe /* 382eb20796bSJens Axboe * Now loop over the map and see if we need to start IO on any 383eb20796bSJens Axboe * pages, fill in the partial map, etc. 384eb20796bSJens Axboe */ 385eb20796bSJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 386eb20796bSJens Axboe nr_pages = spd.nr_pages; 387eb20796bSJens Axboe spd.nr_pages = 0; 388eb20796bSJens Axboe for (page_nr = 0; page_nr < nr_pages; page_nr++) { 389eb20796bSJens Axboe unsigned int this_len; 390eb20796bSJens Axboe 391eb20796bSJens Axboe if (!len) 392eb20796bSJens Axboe break; 393eb20796bSJens Axboe 394eb20796bSJens Axboe /* 395eb20796bSJens Axboe * this_len is the max we'll use from this page 396eb20796bSJens Axboe */ 397eb20796bSJens Axboe this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 39835f3d14dSJens Axboe page = spd.pages[page_nr]; 399eb20796bSJens Axboe 400a08a166fSFengguang Wu if (PageReadahead(page)) 401cf914a7dSRusty Russell page_cache_async_readahead(mapping, &in->f_ra, in, 402d8983910SFengguang Wu page, index, req_pages - page_nr); 403a08a166fSFengguang Wu 4047480a904SJens Axboe /* 4057480a904SJens Axboe * If the page isn't uptodate, we may need to start io on it 4067480a904SJens Axboe */ 4077480a904SJens Axboe if (!PageUptodate(page)) { 4087480a904SJens Axboe lock_page(page); 4097480a904SJens Axboe 4107480a904SJens Axboe /* 41132502b84SMiklos Szeredi * Page was truncated, or invalidated by the 41232502b84SMiklos Szeredi * filesystem. Redo the find/create, but this time the 41332502b84SMiklos Szeredi * page is kept locked, so there's no chance of another 41432502b84SMiklos Szeredi * race with truncate/invalidate. 4157480a904SJens Axboe */ 4167480a904SJens Axboe if (!page->mapping) { 4177480a904SJens Axboe unlock_page(page); 41832502b84SMiklos Szeredi page = find_or_create_page(mapping, index, 41932502b84SMiklos Szeredi mapping_gfp_mask(mapping)); 42032502b84SMiklos Szeredi 42132502b84SMiklos Szeredi if (!page) { 42232502b84SMiklos Szeredi error = -ENOMEM; 4237480a904SJens Axboe break; 4247480a904SJens Axboe } 42535f3d14dSJens Axboe page_cache_release(spd.pages[page_nr]); 42635f3d14dSJens Axboe spd.pages[page_nr] = page; 42732502b84SMiklos Szeredi } 4287480a904SJens Axboe /* 4297480a904SJens Axboe * page was already under io and is now done, great 4307480a904SJens Axboe */ 4317480a904SJens Axboe if (PageUptodate(page)) { 4327480a904SJens Axboe unlock_page(page); 4337480a904SJens Axboe goto fill_it; 4347480a904SJens Axboe } 4357480a904SJens Axboe 4367480a904SJens Axboe /* 4377480a904SJens Axboe * need to read in the page 4387480a904SJens Axboe */ 4397480a904SJens Axboe error = mapping->a_ops->readpage(in, page); 4407480a904SJens Axboe if (unlikely(error)) { 441eb20796bSJens Axboe /* 442eb20796bSJens Axboe * We really should re-lookup the page here, 443eb20796bSJens Axboe * but it complicates things a lot. Instead 444eb20796bSJens Axboe * lets just do what we already stored, and 445eb20796bSJens Axboe * we'll get it the next time we are called. 446eb20796bSJens Axboe */ 4477480a904SJens Axboe if (error == AOP_TRUNCATED_PAGE) 448eb20796bSJens Axboe error = 0; 449eb20796bSJens Axboe 4507480a904SJens Axboe break; 4517480a904SJens Axboe } 452620a324bSJens Axboe } 453620a324bSJens Axboe fill_it: 45491ad66efSJens Axboe /* 455620a324bSJens Axboe * i_size must be checked after PageUptodate. 45691ad66efSJens Axboe */ 45791ad66efSJens Axboe isize = i_size_read(mapping->host); 45891ad66efSJens Axboe end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 459eb20796bSJens Axboe if (unlikely(!isize || index > end_index)) 46091ad66efSJens Axboe break; 46191ad66efSJens Axboe 46291ad66efSJens Axboe /* 46391ad66efSJens Axboe * if this is the last page, see if we need to shrink 46491ad66efSJens Axboe * the length and stop 46591ad66efSJens Axboe */ 46691ad66efSJens Axboe if (end_index == index) { 467475ecadeSHugh Dickins unsigned int plen; 468475ecadeSHugh Dickins 469475ecadeSHugh Dickins /* 470475ecadeSHugh Dickins * max good bytes in this page 471475ecadeSHugh Dickins */ 472475ecadeSHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 473475ecadeSHugh Dickins if (plen <= loff) 47491ad66efSJens Axboe break; 475475ecadeSHugh Dickins 47691ad66efSJens Axboe /* 47791ad66efSJens Axboe * force quit after adding this page 47891ad66efSJens Axboe */ 479475ecadeSHugh Dickins this_len = min(this_len, plen - loff); 480eb20796bSJens Axboe len = this_len; 48191ad66efSJens Axboe } 482620a324bSJens Axboe 48335f3d14dSJens Axboe spd.partial[page_nr].offset = loff; 48435f3d14dSJens Axboe spd.partial[page_nr].len = this_len; 48582aa5d61SJens Axboe len -= this_len; 48691ad66efSJens Axboe loff = 0; 487eb20796bSJens Axboe spd.nr_pages++; 488eb20796bSJens Axboe index++; 4895274f052SJens Axboe } 4905274f052SJens Axboe 491eb20796bSJens Axboe /* 492475ecadeSHugh Dickins * Release any pages at the end, if we quit early. 'page_nr' is how far 493eb20796bSJens Axboe * we got, 'nr_pages' is how many pages are in the map. 494eb20796bSJens Axboe */ 495eb20796bSJens Axboe while (page_nr < nr_pages) 49635f3d14dSJens Axboe page_cache_release(spd.pages[page_nr++]); 497f4e6b498SFengguang Wu in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT; 498eb20796bSJens Axboe 499912d35f8SJens Axboe if (spd.nr_pages) 50035f3d14dSJens Axboe error = splice_to_pipe(pipe, &spd); 50116c523ddSJens Axboe 502047fe360SEric Dumazet splice_shrink_spd(&spd); 5037480a904SJens Axboe return error; 5045274f052SJens Axboe } 5055274f052SJens Axboe 50683f9135bSJens Axboe /** 50783f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 50883f9135bSJens Axboe * @in: file to splice from 509932cc6d4SJens Axboe * @ppos: position in @in 51083f9135bSJens Axboe * @pipe: pipe to splice to 51183f9135bSJens Axboe * @len: number of bytes to splice 51283f9135bSJens Axboe * @flags: splice modifier flags 51383f9135bSJens Axboe * 514932cc6d4SJens Axboe * Description: 515932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 516932cc6d4SJens Axboe * used as long as the address_space operations for the source implements 517932cc6d4SJens Axboe * a readpage() hook. 518932cc6d4SJens Axboe * 51983f9135bSJens Axboe */ 520cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 521cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 522cbb7e577SJens Axboe unsigned int flags) 5235274f052SJens Axboe { 524d366d398SJens Axboe loff_t isize, left; 5258191ecd1SJens Axboe int ret; 526d366d398SJens Axboe 527d366d398SJens Axboe isize = i_size_read(in->f_mapping->host); 528d366d398SJens Axboe if (unlikely(*ppos >= isize)) 529d366d398SJens Axboe return 0; 530d366d398SJens Axboe 531d366d398SJens Axboe left = isize - *ppos; 532d366d398SJens Axboe if (unlikely(left < len)) 533d366d398SJens Axboe len = left; 5345274f052SJens Axboe 535cbb7e577SJens Axboe ret = __generic_file_splice_read(in, ppos, pipe, len, flags); 536723590edSMiklos Szeredi if (ret > 0) { 537cbb7e577SJens Axboe *ppos += ret; 538723590edSMiklos Szeredi file_accessed(in); 539723590edSMiklos Szeredi } 5405274f052SJens Axboe 5415274f052SJens Axboe return ret; 5425274f052SJens Axboe } 543059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 544059a8f37SJens Axboe 5456818173bSMiklos Szeredi static const struct pipe_buf_operations default_pipe_buf_ops = { 5466818173bSMiklos Szeredi .can_merge = 0, 5476818173bSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 5486818173bSMiklos Szeredi .release = generic_pipe_buf_release, 5496818173bSMiklos Szeredi .steal = generic_pipe_buf_steal, 5506818173bSMiklos Szeredi .get = generic_pipe_buf_get, 5516818173bSMiklos Szeredi }; 5526818173bSMiklos Szeredi 55328a625cbSMiklos Szeredi static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe, 55428a625cbSMiklos Szeredi struct pipe_buffer *buf) 55528a625cbSMiklos Szeredi { 55628a625cbSMiklos Szeredi return 1; 55728a625cbSMiklos Szeredi } 55828a625cbSMiklos Szeredi 55928a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 56028a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 56128a625cbSMiklos Szeredi .can_merge = 0, 56228a625cbSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 56328a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 56428a625cbSMiklos Szeredi .steal = generic_pipe_buf_nosteal, 56528a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 56628a625cbSMiklos Szeredi }; 56728a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 56828a625cbSMiklos Szeredi 5696818173bSMiklos Szeredi static ssize_t kernel_readv(struct file *file, const struct iovec *vec, 5706818173bSMiklos Szeredi unsigned long vlen, loff_t offset) 5716818173bSMiklos Szeredi { 5726818173bSMiklos Szeredi mm_segment_t old_fs; 5736818173bSMiklos Szeredi loff_t pos = offset; 5746818173bSMiklos Szeredi ssize_t res; 5756818173bSMiklos Szeredi 5766818173bSMiklos Szeredi old_fs = get_fs(); 5776818173bSMiklos Szeredi set_fs(get_ds()); 5786818173bSMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 5796818173bSMiklos Szeredi res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos); 5806818173bSMiklos Szeredi set_fs(old_fs); 5816818173bSMiklos Szeredi 5826818173bSMiklos Szeredi return res; 5836818173bSMiklos Szeredi } 5846818173bSMiklos Szeredi 5857bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count, 586b2858d7dSMiklos Szeredi loff_t pos) 5870b0a47f5SMiklos Szeredi { 5880b0a47f5SMiklos Szeredi mm_segment_t old_fs; 5890b0a47f5SMiklos Szeredi ssize_t res; 5900b0a47f5SMiklos Szeredi 5910b0a47f5SMiklos Szeredi old_fs = get_fs(); 5920b0a47f5SMiklos Szeredi set_fs(get_ds()); 5930b0a47f5SMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 5947bb307e8SAl Viro res = vfs_write(file, (__force const char __user *)buf, count, &pos); 5950b0a47f5SMiklos Szeredi set_fs(old_fs); 5960b0a47f5SMiklos Szeredi 5970b0a47f5SMiklos Szeredi return res; 5980b0a47f5SMiklos Szeredi } 5997bb307e8SAl Viro EXPORT_SYMBOL(kernel_write); 6000b0a47f5SMiklos Szeredi 6016818173bSMiklos Szeredi ssize_t default_file_splice_read(struct file *in, loff_t *ppos, 6026818173bSMiklos Szeredi struct pipe_inode_info *pipe, size_t len, 6036818173bSMiklos Szeredi unsigned int flags) 6046818173bSMiklos Szeredi { 6056818173bSMiklos Szeredi unsigned int nr_pages; 6066818173bSMiklos Szeredi unsigned int nr_freed; 6076818173bSMiklos Szeredi size_t offset; 60835f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 60935f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 61035f3d14dSJens Axboe struct iovec *vec, __vec[PIPE_DEF_BUFFERS]; 6116818173bSMiklos Szeredi ssize_t res; 6126818173bSMiklos Szeredi size_t this_len; 6136818173bSMiklos Szeredi int error; 6146818173bSMiklos Szeredi int i; 6156818173bSMiklos Szeredi struct splice_pipe_desc spd = { 6166818173bSMiklos Szeredi .pages = pages, 6176818173bSMiklos Szeredi .partial = partial, 618047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 6196818173bSMiklos Szeredi .flags = flags, 6206818173bSMiklos Szeredi .ops = &default_pipe_buf_ops, 6216818173bSMiklos Szeredi .spd_release = spd_release_page, 6226818173bSMiklos Szeredi }; 6236818173bSMiklos Szeredi 62435f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 62535f3d14dSJens Axboe return -ENOMEM; 62635f3d14dSJens Axboe 62735f3d14dSJens Axboe res = -ENOMEM; 62835f3d14dSJens Axboe vec = __vec; 629047fe360SEric Dumazet if (spd.nr_pages_max > PIPE_DEF_BUFFERS) { 630047fe360SEric Dumazet vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL); 63135f3d14dSJens Axboe if (!vec) 63235f3d14dSJens Axboe goto shrink_ret; 63335f3d14dSJens Axboe } 63435f3d14dSJens Axboe 6356818173bSMiklos Szeredi offset = *ppos & ~PAGE_CACHE_MASK; 6366818173bSMiklos Szeredi nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 6376818173bSMiklos Szeredi 638047fe360SEric Dumazet for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) { 6396818173bSMiklos Szeredi struct page *page; 6406818173bSMiklos Szeredi 6414f231228SJens Axboe page = alloc_page(GFP_USER); 6426818173bSMiklos Szeredi error = -ENOMEM; 6436818173bSMiklos Szeredi if (!page) 6446818173bSMiklos Szeredi goto err; 6456818173bSMiklos Szeredi 6466818173bSMiklos Szeredi this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset); 6474f231228SJens Axboe vec[i].iov_base = (void __user *) page_address(page); 6486818173bSMiklos Szeredi vec[i].iov_len = this_len; 64935f3d14dSJens Axboe spd.pages[i] = page; 6506818173bSMiklos Szeredi spd.nr_pages++; 6516818173bSMiklos Szeredi len -= this_len; 6526818173bSMiklos Szeredi offset = 0; 6536818173bSMiklos Szeredi } 6546818173bSMiklos Szeredi 6556818173bSMiklos Szeredi res = kernel_readv(in, vec, spd.nr_pages, *ppos); 65677f6bf57SAndrew Morton if (res < 0) { 65777f6bf57SAndrew Morton error = res; 6586818173bSMiklos Szeredi goto err; 65977f6bf57SAndrew Morton } 6606818173bSMiklos Szeredi 6616818173bSMiklos Szeredi error = 0; 6626818173bSMiklos Szeredi if (!res) 6636818173bSMiklos Szeredi goto err; 6646818173bSMiklos Szeredi 6656818173bSMiklos Szeredi nr_freed = 0; 6666818173bSMiklos Szeredi for (i = 0; i < spd.nr_pages; i++) { 6676818173bSMiklos Szeredi this_len = min_t(size_t, vec[i].iov_len, res); 66835f3d14dSJens Axboe spd.partial[i].offset = 0; 66935f3d14dSJens Axboe spd.partial[i].len = this_len; 6706818173bSMiklos Szeredi if (!this_len) { 67135f3d14dSJens Axboe __free_page(spd.pages[i]); 67235f3d14dSJens Axboe spd.pages[i] = NULL; 6736818173bSMiklos Szeredi nr_freed++; 6746818173bSMiklos Szeredi } 6756818173bSMiklos Szeredi res -= this_len; 6766818173bSMiklos Szeredi } 6776818173bSMiklos Szeredi spd.nr_pages -= nr_freed; 6786818173bSMiklos Szeredi 6796818173bSMiklos Szeredi res = splice_to_pipe(pipe, &spd); 6806818173bSMiklos Szeredi if (res > 0) 6816818173bSMiklos Szeredi *ppos += res; 6826818173bSMiklos Szeredi 68335f3d14dSJens Axboe shrink_ret: 68435f3d14dSJens Axboe if (vec != __vec) 68535f3d14dSJens Axboe kfree(vec); 686047fe360SEric Dumazet splice_shrink_spd(&spd); 6876818173bSMiklos Szeredi return res; 6886818173bSMiklos Szeredi 6896818173bSMiklos Szeredi err: 6904f231228SJens Axboe for (i = 0; i < spd.nr_pages; i++) 69135f3d14dSJens Axboe __free_page(spd.pages[i]); 6924f231228SJens Axboe 69335f3d14dSJens Axboe res = error; 69435f3d14dSJens Axboe goto shrink_ret; 6956818173bSMiklos Szeredi } 6966818173bSMiklos Szeredi EXPORT_SYMBOL(default_file_splice_read); 6976818173bSMiklos Szeredi 6985274f052SJens Axboe /* 6994f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 700016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 7015274f052SJens Axboe */ 70276ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 7035274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 7045274f052SJens Axboe { 7056a14b90bSJens Axboe struct file *file = sd->u.file; 7065274f052SJens Axboe loff_t pos = sd->pos; 707a8adbe37SMichał Mirosław int more; 7085274f052SJens Axboe 70972c2d531SAl Viro if (!likely(file->f_op->sendpage)) 710a8adbe37SMichał Mirosław return -EINVAL; 711a8adbe37SMichał Mirosław 71235f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 713ae62ca7bSEric Dumazet 714ae62ca7bSEric Dumazet if (sd->len < sd->total_len && pipe->nrbufs > 1) 71535f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 716ae62ca7bSEric Dumazet 717a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 718f84d7519SJens Axboe sd->len, &pos, more); 7195274f052SJens Axboe } 7205274f052SJens Axboe 7215274f052SJens Axboe /* 7225274f052SJens Axboe * This is a little more tricky than the file -> pipe splicing. There are 7235274f052SJens Axboe * basically three cases: 7245274f052SJens Axboe * 7255274f052SJens Axboe * - Destination page already exists in the address space and there 7265274f052SJens Axboe * are users of it. For that case we have no other option that 7275274f052SJens Axboe * copying the data. Tough luck. 7285274f052SJens Axboe * - Destination page already exists in the address space, but there 7295274f052SJens Axboe * are no users of it. Make sure it's uptodate, then drop it. Fall 7305274f052SJens Axboe * through to last case. 7315274f052SJens Axboe * - Destination page does not exist, we can add the pipe page to 7325274f052SJens Axboe * the page cache and avoid the copy. 7335274f052SJens Axboe * 73483f9135bSJens Axboe * If asked to move pages to the output file (SPLICE_F_MOVE is set in 73583f9135bSJens Axboe * sd->flags), we attempt to migrate pages from the pipe to the output 73683f9135bSJens Axboe * file address space page cache. This is possible if no one else has 73783f9135bSJens Axboe * the pipe page referenced outside of the pipe and page cache. If 73883f9135bSJens Axboe * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create 73983f9135bSJens Axboe * a new page in the output file page cache and fill/dirty that. 7405274f052SJens Axboe */ 74196f9bc8fSAl Viro static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 7425274f052SJens Axboe struct splice_desc *sd) 7435274f052SJens Axboe { 7446a14b90bSJens Axboe struct file *file = sd->u.file; 7455274f052SJens Axboe struct address_space *mapping = file->f_mapping; 746016b661eSJens Axboe unsigned int offset, this_len; 7475274f052SJens Axboe struct page *page; 748afddba49SNick Piggin void *fsdata; 7493e7ee3e7SJens Axboe int ret; 7505274f052SJens Axboe 7515274f052SJens Axboe offset = sd->pos & ~PAGE_CACHE_MASK; 7525274f052SJens Axboe 753016b661eSJens Axboe this_len = sd->len; 754016b661eSJens Axboe if (this_len + offset > PAGE_CACHE_SIZE) 755016b661eSJens Axboe this_len = PAGE_CACHE_SIZE - offset; 756016b661eSJens Axboe 757afddba49SNick Piggin ret = pagecache_write_begin(file, mapping, sd->pos, this_len, 758afddba49SNick Piggin AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 7599e0267c2SJens Axboe if (unlikely(ret)) 760afddba49SNick Piggin goto out; 7615274f052SJens Axboe 7620568b409SJens Axboe if (buf->page != page) { 763fbb32750SAl Viro char *src = kmap_atomic(buf->page); 764e8e3c3d6SCong Wang char *dst = kmap_atomic(page); 7655abc97aaSJens Axboe 766016b661eSJens Axboe memcpy(dst + offset, src + buf->offset, this_len); 7675274f052SJens Axboe flush_dcache_page(page); 768e8e3c3d6SCong Wang kunmap_atomic(dst); 769fbb32750SAl Viro kunmap_atomic(src); 7705abc97aaSJens Axboe } 771afddba49SNick Piggin ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len, 772afddba49SNick Piggin page, fsdata); 7730568b409SJens Axboe out: 7745274f052SJens Axboe return ret; 7755274f052SJens Axboe } 7765274f052SJens Axboe 777b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 778b3c2d2ddSMiklos Szeredi { 779b3c2d2ddSMiklos Szeredi smp_mb(); 780b3c2d2ddSMiklos Szeredi if (waitqueue_active(&pipe->wait)) 781b3c2d2ddSMiklos Szeredi wake_up_interruptible(&pipe->wait); 782b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 783b3c2d2ddSMiklos Szeredi } 784b3c2d2ddSMiklos Szeredi 785b3c2d2ddSMiklos Szeredi /** 786b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 787b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 788b3c2d2ddSMiklos Szeredi * @sd: information to @actor 789b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 790b3c2d2ddSMiklos Szeredi * 791b3c2d2ddSMiklos Szeredi * Description: 792b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 793b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 794b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 795b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 796b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 797b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 798b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 799b3c2d2ddSMiklos Szeredi * 800b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 801b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 802b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 803b3c2d2ddSMiklos Szeredi * destination. 804b3c2d2ddSMiklos Szeredi */ 80596f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 806b3c2d2ddSMiklos Szeredi splice_actor *actor) 807b3c2d2ddSMiklos Szeredi { 808b3c2d2ddSMiklos Szeredi int ret; 809b3c2d2ddSMiklos Szeredi 810b3c2d2ddSMiklos Szeredi while (pipe->nrbufs) { 811b3c2d2ddSMiklos Szeredi struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 812b3c2d2ddSMiklos Szeredi const struct pipe_buf_operations *ops = buf->ops; 813b3c2d2ddSMiklos Szeredi 814b3c2d2ddSMiklos Szeredi sd->len = buf->len; 815b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 816b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 817b3c2d2ddSMiklos Szeredi 818a8adbe37SMichał Mirosław ret = buf->ops->confirm(pipe, buf); 819a8adbe37SMichał Mirosław if (unlikely(ret)) { 820b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 821b3c2d2ddSMiklos Szeredi ret = 0; 822b3c2d2ddSMiklos Szeredi return ret; 823b3c2d2ddSMiklos Szeredi } 824a8adbe37SMichał Mirosław 825a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 826a8adbe37SMichał Mirosław if (ret <= 0) 827a8adbe37SMichał Mirosław return ret; 828a8adbe37SMichał Mirosław 829b3c2d2ddSMiklos Szeredi buf->offset += ret; 830b3c2d2ddSMiklos Szeredi buf->len -= ret; 831b3c2d2ddSMiklos Szeredi 832b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 833b3c2d2ddSMiklos Szeredi sd->len -= ret; 834b3c2d2ddSMiklos Szeredi sd->pos += ret; 835b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 836b3c2d2ddSMiklos Szeredi 837b3c2d2ddSMiklos Szeredi if (!buf->len) { 838b3c2d2ddSMiklos Szeredi buf->ops = NULL; 839b3c2d2ddSMiklos Szeredi ops->release(pipe, buf); 84035f3d14dSJens Axboe pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); 841b3c2d2ddSMiklos Szeredi pipe->nrbufs--; 8426447a3cfSAl Viro if (pipe->files) 843b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 844b3c2d2ddSMiklos Szeredi } 845b3c2d2ddSMiklos Szeredi 846b3c2d2ddSMiklos Szeredi if (!sd->total_len) 847b3c2d2ddSMiklos Szeredi return 0; 848b3c2d2ddSMiklos Szeredi } 849b3c2d2ddSMiklos Szeredi 850b3c2d2ddSMiklos Szeredi return 1; 851b3c2d2ddSMiklos Szeredi } 852b3c2d2ddSMiklos Szeredi 853b3c2d2ddSMiklos Szeredi /** 854b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 855b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 856b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 857b3c2d2ddSMiklos Szeredi * 858b3c2d2ddSMiklos Szeredi * Description: 859b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 860b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 861b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 862b3c2d2ddSMiklos Szeredi */ 86396f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 864b3c2d2ddSMiklos Szeredi { 865b3c2d2ddSMiklos Szeredi while (!pipe->nrbufs) { 866b3c2d2ddSMiklos Szeredi if (!pipe->writers) 867b3c2d2ddSMiklos Szeredi return 0; 868b3c2d2ddSMiklos Szeredi 869b3c2d2ddSMiklos Szeredi if (!pipe->waiting_writers && sd->num_spliced) 870b3c2d2ddSMiklos Szeredi return 0; 871b3c2d2ddSMiklos Szeredi 872b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 873b3c2d2ddSMiklos Szeredi return -EAGAIN; 874b3c2d2ddSMiklos Szeredi 875b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 876b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 877b3c2d2ddSMiklos Szeredi 878b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 879b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 880b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 881b3c2d2ddSMiklos Szeredi } 882b3c2d2ddSMiklos Szeredi 883b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 884b3c2d2ddSMiklos Szeredi } 885b3c2d2ddSMiklos Szeredi 886b3c2d2ddSMiklos Szeredi return 1; 887b3c2d2ddSMiklos Szeredi } 888b3c2d2ddSMiklos Szeredi 889b3c2d2ddSMiklos Szeredi /** 890b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 891b80901bbSRandy Dunlap * @sd: information about the splice operation 892b3c2d2ddSMiklos Szeredi * 893b3c2d2ddSMiklos Szeredi * Description: 894b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 895b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 896b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 897b3c2d2ddSMiklos Szeredi */ 89896f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd) 899b3c2d2ddSMiklos Szeredi { 900b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 901b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 902b3c2d2ddSMiklos Szeredi } 903b3c2d2ddSMiklos Szeredi 904b3c2d2ddSMiklos Szeredi /** 905b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 906b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 907b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 908b3c2d2ddSMiklos Szeredi * 909b3c2d2ddSMiklos Szeredi * Description: 910b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 911b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 912b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 913b3c2d2ddSMiklos Szeredi */ 91496f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 915b3c2d2ddSMiklos Szeredi { 916b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 917b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 918b3c2d2ddSMiklos Szeredi } 919b3c2d2ddSMiklos Szeredi 920932cc6d4SJens Axboe /** 921932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 922932cc6d4SJens Axboe * @pipe: pipe to splice from 923932cc6d4SJens Axboe * @sd: information to @actor 924932cc6d4SJens Axboe * @actor: handler that splices the data 925932cc6d4SJens Axboe * 926932cc6d4SJens Axboe * Description: 927932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 928932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 929932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 930932cc6d4SJens Axboe * pipe_to_user. 931932cc6d4SJens Axboe * 93283f9135bSJens Axboe */ 933c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 934c66ab6faSJens Axboe splice_actor *actor) 9355274f052SJens Axboe { 936b3c2d2ddSMiklos Szeredi int ret; 9375274f052SJens Axboe 938b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 939b3c2d2ddSMiklos Szeredi do { 940b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 941b3c2d2ddSMiklos Szeredi if (ret > 0) 942b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 943b3c2d2ddSMiklos Szeredi } while (ret > 0); 944b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 9455274f052SJens Axboe 946b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 9475274f052SJens Axboe } 94840bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 9495274f052SJens Axboe 950932cc6d4SJens Axboe /** 951932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 952932cc6d4SJens Axboe * @pipe: pipe to splice from 953932cc6d4SJens Axboe * @out: file to splice to 954932cc6d4SJens Axboe * @ppos: position in @out 955932cc6d4SJens Axboe * @len: how many bytes to splice 956932cc6d4SJens Axboe * @flags: splice modifier flags 957932cc6d4SJens Axboe * @actor: handler that splices the data 958932cc6d4SJens Axboe * 959932cc6d4SJens Axboe * Description: 9602933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 961932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 962932cc6d4SJens Axboe * 963932cc6d4SJens Axboe */ 9646da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 9656da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 9666da61809SMark Fasheh splice_actor *actor) 9676da61809SMark Fasheh { 9686da61809SMark Fasheh ssize_t ret; 969c66ab6faSJens Axboe struct splice_desc sd = { 970c66ab6faSJens Axboe .total_len = len, 971c66ab6faSJens Axboe .flags = flags, 972c66ab6faSJens Axboe .pos = *ppos, 9736a14b90bSJens Axboe .u.file = out, 974c66ab6faSJens Axboe }; 9756da61809SMark Fasheh 97661e0d47cSMiklos Szeredi pipe_lock(pipe); 977c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 97861e0d47cSMiklos Szeredi pipe_unlock(pipe); 9796da61809SMark Fasheh 9806da61809SMark Fasheh return ret; 9816da61809SMark Fasheh } 9826da61809SMark Fasheh 9836da61809SMark Fasheh /** 98483f9135bSJens Axboe * generic_file_splice_write - splice data from a pipe to a file 9853a326a2cSIngo Molnar * @pipe: pipe info 98683f9135bSJens Axboe * @out: file to write to 987932cc6d4SJens Axboe * @ppos: position in @out 98883f9135bSJens Axboe * @len: number of bytes to splice 98983f9135bSJens Axboe * @flags: splice modifier flags 99083f9135bSJens Axboe * 991932cc6d4SJens Axboe * Description: 99283f9135bSJens Axboe * Will either move or copy pages (determined by @flags options) from 99383f9135bSJens Axboe * the given pipe inode to the given file. 99483f9135bSJens Axboe * 99583f9135bSJens Axboe */ 9963a326a2cSIngo Molnar ssize_t 9973a326a2cSIngo Molnar generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 998cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 9995274f052SJens Axboe { 10004f6f0bd2SJens Axboe struct address_space *mapping = out->f_mapping; 10018c34e2d6SJens Axboe struct inode *inode = mapping->host; 10027f3d4ee1SMiklos Szeredi struct splice_desc sd = { 10037f3d4ee1SMiklos Szeredi .total_len = len, 10047f3d4ee1SMiklos Szeredi .flags = flags, 10057f3d4ee1SMiklos Szeredi .pos = *ppos, 10067f3d4ee1SMiklos Szeredi .u.file = out, 10077f3d4ee1SMiklos Szeredi }; 10083a326a2cSIngo Molnar ssize_t ret; 10098c34e2d6SJens Axboe 101061e0d47cSMiklos Szeredi pipe_lock(pipe); 1011eb443e5aSMiklos Szeredi 1012eb443e5aSMiklos Szeredi splice_from_pipe_begin(&sd); 1013eb443e5aSMiklos Szeredi do { 1014eb443e5aSMiklos Szeredi ret = splice_from_pipe_next(pipe, &sd); 1015eb443e5aSMiklos Szeredi if (ret <= 0) 1016eb443e5aSMiklos Szeredi break; 1017eb443e5aSMiklos Szeredi 1018eb443e5aSMiklos Szeredi mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 1019eb443e5aSMiklos Szeredi ret = file_remove_suid(out); 1020723590edSMiklos Szeredi if (!ret) { 1021c3b2da31SJosef Bacik ret = file_update_time(out); 1022c3b2da31SJosef Bacik if (!ret) 1023c3b2da31SJosef Bacik ret = splice_from_pipe_feed(pipe, &sd, 1024c3b2da31SJosef Bacik pipe_to_file); 1025723590edSMiklos Szeredi } 1026eb443e5aSMiklos Szeredi mutex_unlock(&inode->i_mutex); 1027eb443e5aSMiklos Szeredi } while (ret > 0); 1028eb443e5aSMiklos Szeredi splice_from_pipe_end(pipe, &sd); 1029eb443e5aSMiklos Szeredi 103061e0d47cSMiklos Szeredi pipe_unlock(pipe); 1031eb443e5aSMiklos Szeredi 1032eb443e5aSMiklos Szeredi if (sd.num_spliced) 1033eb443e5aSMiklos Szeredi ret = sd.num_spliced; 1034eb443e5aSMiklos Szeredi 1035a4514ebdSJens Axboe if (ret > 0) { 10367f3d4ee1SMiklos Szeredi int err; 10377f3d4ee1SMiklos Szeredi 1038148f948bSJan Kara err = generic_write_sync(out, *ppos, ret); 10394f6f0bd2SJens Axboe if (err) 10404f6f0bd2SJens Axboe ret = err; 1041148f948bSJan Kara else 1042148f948bSJan Kara *ppos += ret; 1043d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(mapping); 1044a4514ebdSJens Axboe } 10454f6f0bd2SJens Axboe 10464f6f0bd2SJens Axboe return ret; 10475274f052SJens Axboe } 10485274f052SJens Axboe 1049059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_write); 1050059a8f37SJens Axboe 10518d020765SAl Viro /** 10528d020765SAl Viro * iter_file_splice_write - splice data from a pipe to a file 10538d020765SAl Viro * @pipe: pipe info 10548d020765SAl Viro * @out: file to write to 10558d020765SAl Viro * @ppos: position in @out 10568d020765SAl Viro * @len: number of bytes to splice 10578d020765SAl Viro * @flags: splice modifier flags 10588d020765SAl Viro * 10598d020765SAl Viro * Description: 10608d020765SAl Viro * Will either move or copy pages (determined by @flags options) from 10618d020765SAl Viro * the given pipe inode to the given file. 10628d020765SAl Viro * This one is ->write_iter-based. 10638d020765SAl Viro * 10648d020765SAl Viro */ 10658d020765SAl Viro ssize_t 10668d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 10678d020765SAl Viro loff_t *ppos, size_t len, unsigned int flags) 10688d020765SAl Viro { 10698d020765SAl Viro struct splice_desc sd = { 10708d020765SAl Viro .total_len = len, 10718d020765SAl Viro .flags = flags, 10728d020765SAl Viro .pos = *ppos, 10738d020765SAl Viro .u.file = out, 10748d020765SAl Viro }; 10758d020765SAl Viro int nbufs = pipe->buffers; 10768d020765SAl Viro struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 10778d020765SAl Viro GFP_KERNEL); 10788d020765SAl Viro ssize_t ret; 10798d020765SAl Viro 10808d020765SAl Viro if (unlikely(!array)) 10818d020765SAl Viro return -ENOMEM; 10828d020765SAl Viro 10838d020765SAl Viro pipe_lock(pipe); 10848d020765SAl Viro 10858d020765SAl Viro splice_from_pipe_begin(&sd); 10868d020765SAl Viro while (sd.total_len) { 10878d020765SAl Viro struct iov_iter from; 10888d020765SAl Viro struct kiocb kiocb; 10898d020765SAl Viro size_t left; 10908d020765SAl Viro int n, idx; 10918d020765SAl Viro 10928d020765SAl Viro ret = splice_from_pipe_next(pipe, &sd); 10938d020765SAl Viro if (ret <= 0) 10948d020765SAl Viro break; 10958d020765SAl Viro 10968d020765SAl Viro if (unlikely(nbufs < pipe->buffers)) { 10978d020765SAl Viro kfree(array); 10988d020765SAl Viro nbufs = pipe->buffers; 10998d020765SAl Viro array = kcalloc(nbufs, sizeof(struct bio_vec), 11008d020765SAl Viro GFP_KERNEL); 11018d020765SAl Viro if (!array) { 11028d020765SAl Viro ret = -ENOMEM; 11038d020765SAl Viro break; 11048d020765SAl Viro } 11058d020765SAl Viro } 11068d020765SAl Viro 11078d020765SAl Viro /* build the vector */ 11088d020765SAl Viro left = sd.total_len; 11098d020765SAl Viro for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) { 11108d020765SAl Viro struct pipe_buffer *buf = pipe->bufs + idx; 11118d020765SAl Viro size_t this_len = buf->len; 11128d020765SAl Viro 11138d020765SAl Viro if (this_len > left) 11148d020765SAl Viro this_len = left; 11158d020765SAl Viro 11168d020765SAl Viro if (idx == pipe->buffers - 1) 11178d020765SAl Viro idx = -1; 11188d020765SAl Viro 11198d020765SAl Viro ret = buf->ops->confirm(pipe, buf); 11208d020765SAl Viro if (unlikely(ret)) { 11218d020765SAl Viro if (ret == -ENODATA) 11228d020765SAl Viro ret = 0; 11238d020765SAl Viro goto done; 11248d020765SAl Viro } 11258d020765SAl Viro 11268d020765SAl Viro array[n].bv_page = buf->page; 11278d020765SAl Viro array[n].bv_len = this_len; 11288d020765SAl Viro array[n].bv_offset = buf->offset; 11298d020765SAl Viro left -= this_len; 11308d020765SAl Viro } 11318d020765SAl Viro 11328d020765SAl Viro /* ... iov_iter */ 11338d020765SAl Viro from.type = ITER_BVEC | WRITE; 11348d020765SAl Viro from.bvec = array; 11358d020765SAl Viro from.nr_segs = n; 11368d020765SAl Viro from.count = sd.total_len - left; 11378d020765SAl Viro from.iov_offset = 0; 11388d020765SAl Viro 11398d020765SAl Viro /* ... and iocb */ 11408d020765SAl Viro init_sync_kiocb(&kiocb, out); 11418d020765SAl Viro kiocb.ki_pos = sd.pos; 11428d020765SAl Viro kiocb.ki_nbytes = sd.total_len - left; 11438d020765SAl Viro 11448d020765SAl Viro /* now, send it */ 11458d020765SAl Viro ret = out->f_op->write_iter(&kiocb, &from); 11468d020765SAl Viro if (-EIOCBQUEUED == ret) 11478d020765SAl Viro ret = wait_on_sync_kiocb(&kiocb); 11488d020765SAl Viro 11498d020765SAl Viro if (ret <= 0) 11508d020765SAl Viro break; 11518d020765SAl Viro 11528d020765SAl Viro sd.num_spliced += ret; 11538d020765SAl Viro sd.total_len -= ret; 11548d020765SAl Viro *ppos = sd.pos = kiocb.ki_pos; 11558d020765SAl Viro 11568d020765SAl Viro /* dismiss the fully eaten buffers, adjust the partial one */ 11578d020765SAl Viro while (ret) { 11588d020765SAl Viro struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 11598d020765SAl Viro if (ret >= buf->len) { 11608d020765SAl Viro const struct pipe_buf_operations *ops = buf->ops; 11618d020765SAl Viro ret -= buf->len; 11628d020765SAl Viro buf->len = 0; 11638d020765SAl Viro buf->ops = NULL; 11648d020765SAl Viro ops->release(pipe, buf); 11658d020765SAl Viro pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); 11668d020765SAl Viro pipe->nrbufs--; 11678d020765SAl Viro if (pipe->files) 11688d020765SAl Viro sd.need_wakeup = true; 11698d020765SAl Viro } else { 11708d020765SAl Viro buf->offset += ret; 11718d020765SAl Viro buf->len -= ret; 11728d020765SAl Viro ret = 0; 11738d020765SAl Viro } 11748d020765SAl Viro } 11758d020765SAl Viro } 11768d020765SAl Viro done: 11778d020765SAl Viro kfree(array); 11788d020765SAl Viro splice_from_pipe_end(pipe, &sd); 11798d020765SAl Viro 11808d020765SAl Viro pipe_unlock(pipe); 11818d020765SAl Viro 11828d020765SAl Viro if (sd.num_spliced) 11838d020765SAl Viro ret = sd.num_spliced; 11848d020765SAl Viro 11858d020765SAl Viro return ret; 11868d020765SAl Viro } 11878d020765SAl Viro 11888d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write); 11898d020765SAl Viro 1190b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 1191b2858d7dSMiklos Szeredi struct splice_desc *sd) 11920b0a47f5SMiklos Szeredi { 1193b2858d7dSMiklos Szeredi int ret; 1194b2858d7dSMiklos Szeredi void *data; 119506ae43f3SAl Viro loff_t tmp = sd->pos; 1196b2858d7dSMiklos Szeredi 1197fbb32750SAl Viro data = kmap(buf->page); 119806ae43f3SAl Viro ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); 1199fbb32750SAl Viro kunmap(buf->page); 1200b2858d7dSMiklos Szeredi 1201b2858d7dSMiklos Szeredi return ret; 12020b0a47f5SMiklos Szeredi } 12030b0a47f5SMiklos Szeredi 12040b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, 12050b0a47f5SMiklos Szeredi struct file *out, loff_t *ppos, 12060b0a47f5SMiklos Szeredi size_t len, unsigned int flags) 12070b0a47f5SMiklos Szeredi { 1208b2858d7dSMiklos Szeredi ssize_t ret; 12090b0a47f5SMiklos Szeredi 1210b2858d7dSMiklos Szeredi ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); 1211b2858d7dSMiklos Szeredi if (ret > 0) 1212b2858d7dSMiklos Szeredi *ppos += ret; 12130b0a47f5SMiklos Szeredi 1214b2858d7dSMiklos Szeredi return ret; 12150b0a47f5SMiklos Szeredi } 12160b0a47f5SMiklos Szeredi 121783f9135bSJens Axboe /** 121883f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 1219932cc6d4SJens Axboe * @pipe: pipe to splice from 122083f9135bSJens Axboe * @out: socket to write to 1221932cc6d4SJens Axboe * @ppos: position in @out 122283f9135bSJens Axboe * @len: number of bytes to splice 122383f9135bSJens Axboe * @flags: splice modifier flags 122483f9135bSJens Axboe * 1225932cc6d4SJens Axboe * Description: 122683f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 122783f9135bSJens Axboe * is involved. 122883f9135bSJens Axboe * 122983f9135bSJens Axboe */ 12303a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 1231cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 12325274f052SJens Axboe { 123300522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 12345274f052SJens Axboe } 12355274f052SJens Axboe 1236059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 1237a0f06780SJeff Garzik 123883f9135bSJens Axboe /* 123983f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 124083f9135bSJens Axboe */ 12413a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 1242cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 12435274f052SJens Axboe { 12440b0a47f5SMiklos Szeredi ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 12450b0a47f5SMiklos Szeredi loff_t *, size_t, unsigned int); 12465274f052SJens Axboe 124772c2d531SAl Viro if (out->f_op->splice_write) 12480b0a47f5SMiklos Szeredi splice_write = out->f_op->splice_write; 1249cc56f7deSChangli Gao else 12500b0a47f5SMiklos Szeredi splice_write = default_file_splice_write; 12510b0a47f5SMiklos Szeredi 1252500368f7SAl Viro return splice_write(pipe, out, ppos, len, flags); 12535274f052SJens Axboe } 12545274f052SJens Axboe 125583f9135bSJens Axboe /* 125683f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 125783f9135bSJens Axboe */ 1258cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 1259cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 1260cbb7e577SJens Axboe unsigned int flags) 12615274f052SJens Axboe { 12626818173bSMiklos Szeredi ssize_t (*splice_read)(struct file *, loff_t *, 12636818173bSMiklos Szeredi struct pipe_inode_info *, size_t, unsigned int); 12645274f052SJens Axboe int ret; 12655274f052SJens Axboe 126649570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 12675274f052SJens Axboe return -EBADF; 12685274f052SJens Axboe 1269cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 12705274f052SJens Axboe if (unlikely(ret < 0)) 12715274f052SJens Axboe return ret; 12725274f052SJens Axboe 127372c2d531SAl Viro if (in->f_op->splice_read) 12746818173bSMiklos Szeredi splice_read = in->f_op->splice_read; 1275cc56f7deSChangli Gao else 12766818173bSMiklos Szeredi splice_read = default_file_splice_read; 12776818173bSMiklos Szeredi 12786818173bSMiklos Szeredi return splice_read(in, ppos, pipe, len, flags); 12795274f052SJens Axboe } 12805274f052SJens Axboe 1281932cc6d4SJens Axboe /** 1282932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 1283932cc6d4SJens Axboe * @in: file to splice from 1284932cc6d4SJens Axboe * @sd: actor information on where to splice to 1285932cc6d4SJens Axboe * @actor: handles the data splicing 1286932cc6d4SJens Axboe * 1287932cc6d4SJens Axboe * Description: 1288932cc6d4SJens Axboe * This is a special case helper to splice directly between two 1289932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 1290932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 1291932cc6d4SJens Axboe * that process. 1292932cc6d4SJens Axboe * 1293c66ab6faSJens Axboe */ 1294c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 1295c66ab6faSJens Axboe splice_direct_actor *actor) 1296b92ce558SJens Axboe { 1297b92ce558SJens Axboe struct pipe_inode_info *pipe; 1298b92ce558SJens Axboe long ret, bytes; 1299b92ce558SJens Axboe umode_t i_mode; 1300c66ab6faSJens Axboe size_t len; 1301c66ab6faSJens Axboe int i, flags; 1302b92ce558SJens Axboe 1303b92ce558SJens Axboe /* 1304b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 1305b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 1306b92ce558SJens Axboe * piped splicing for that! 1307b92ce558SJens Axboe */ 1308496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 1309b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 1310b92ce558SJens Axboe return -EINVAL; 1311b92ce558SJens Axboe 1312b92ce558SJens Axboe /* 1313b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 1314b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 1315b92ce558SJens Axboe */ 1316b92ce558SJens Axboe pipe = current->splice_pipe; 131749570e9bSJens Axboe if (unlikely(!pipe)) { 13187bee130eSAl Viro pipe = alloc_pipe_info(); 1319b92ce558SJens Axboe if (!pipe) 1320b92ce558SJens Axboe return -ENOMEM; 1321b92ce558SJens Axboe 1322b92ce558SJens Axboe /* 1323b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 132400522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 1325b92ce558SJens Axboe * PIPE_READERS appropriately. 1326b92ce558SJens Axboe */ 1327b92ce558SJens Axboe pipe->readers = 1; 1328b92ce558SJens Axboe 1329b92ce558SJens Axboe current->splice_pipe = pipe; 1330b92ce558SJens Axboe } 1331b92ce558SJens Axboe 1332b92ce558SJens Axboe /* 133373d62d83SIngo Molnar * Do the splice. 1334b92ce558SJens Axboe */ 1335b92ce558SJens Axboe ret = 0; 1336b92ce558SJens Axboe bytes = 0; 1337c66ab6faSJens Axboe len = sd->total_len; 1338c66ab6faSJens Axboe flags = sd->flags; 1339c66ab6faSJens Axboe 1340c66ab6faSJens Axboe /* 1341c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 1342c66ab6faSJens Axboe */ 1343c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 1344b92ce558SJens Axboe 1345b92ce558SJens Axboe while (len) { 134651a92c0fSJens Axboe size_t read_len; 1347a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 1348b92ce558SJens Axboe 1349bcd4f3acSJens Axboe ret = do_splice_to(in, &pos, pipe, len, flags); 135051a92c0fSJens Axboe if (unlikely(ret <= 0)) 1351b92ce558SJens Axboe goto out_release; 1352b92ce558SJens Axboe 1353b92ce558SJens Axboe read_len = ret; 1354c66ab6faSJens Axboe sd->total_len = read_len; 1355b92ce558SJens Axboe 1356b92ce558SJens Axboe /* 1357b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 1358b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 1359b92ce558SJens Axboe * could get stuck data in the internal pipe: 1360b92ce558SJens Axboe */ 1361c66ab6faSJens Axboe ret = actor(pipe, sd); 1362a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 1363a82c53a0STom Zanussi sd->pos = prev_pos; 1364b92ce558SJens Axboe goto out_release; 1365a82c53a0STom Zanussi } 1366b92ce558SJens Axboe 1367b92ce558SJens Axboe bytes += ret; 1368b92ce558SJens Axboe len -= ret; 1369bcd4f3acSJens Axboe sd->pos = pos; 1370b92ce558SJens Axboe 1371a82c53a0STom Zanussi if (ret < read_len) { 1372a82c53a0STom Zanussi sd->pos = prev_pos + ret; 137351a92c0fSJens Axboe goto out_release; 1374b92ce558SJens Axboe } 1375a82c53a0STom Zanussi } 1376b92ce558SJens Axboe 13779e97198dSJens Axboe done: 1378b92ce558SJens Axboe pipe->nrbufs = pipe->curbuf = 0; 13799e97198dSJens Axboe file_accessed(in); 1380b92ce558SJens Axboe return bytes; 1381b92ce558SJens Axboe 1382b92ce558SJens Axboe out_release: 1383b92ce558SJens Axboe /* 1384b92ce558SJens Axboe * If we did an incomplete transfer we must release 1385b92ce558SJens Axboe * the pipe buffers in question: 1386b92ce558SJens Axboe */ 138735f3d14dSJens Axboe for (i = 0; i < pipe->buffers; i++) { 1388b92ce558SJens Axboe struct pipe_buffer *buf = pipe->bufs + i; 1389b92ce558SJens Axboe 1390b92ce558SJens Axboe if (buf->ops) { 1391b92ce558SJens Axboe buf->ops->release(pipe, buf); 1392b92ce558SJens Axboe buf->ops = NULL; 1393b92ce558SJens Axboe } 1394b92ce558SJens Axboe } 1395b92ce558SJens Axboe 13969e97198dSJens Axboe if (!bytes) 13979e97198dSJens Axboe bytes = ret; 1398b92ce558SJens Axboe 13999e97198dSJens Axboe goto done; 1400c66ab6faSJens Axboe } 1401c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1402c66ab6faSJens Axboe 1403c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1404c66ab6faSJens Axboe struct splice_desc *sd) 1405c66ab6faSJens Axboe { 14066a14b90bSJens Axboe struct file *file = sd->u.file; 1407c66ab6faSJens Axboe 14087995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 14092cb4b05eSChangli Gao sd->flags); 1410c66ab6faSJens Axboe } 1411c66ab6faSJens Axboe 1412932cc6d4SJens Axboe /** 1413932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1414932cc6d4SJens Axboe * @in: file to splice from 1415932cc6d4SJens Axboe * @ppos: input file offset 1416932cc6d4SJens Axboe * @out: file to splice to 1417acdb37c3SRandy Dunlap * @opos: output file offset 1418932cc6d4SJens Axboe * @len: number of bytes to splice 1419932cc6d4SJens Axboe * @flags: splice modifier flags 1420932cc6d4SJens Axboe * 1421932cc6d4SJens Axboe * Description: 1422932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1423932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1424932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1425932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1426932cc6d4SJens Axboe * 1427932cc6d4SJens Axboe */ 1428c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 14297995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 1430c66ab6faSJens Axboe { 1431c66ab6faSJens Axboe struct splice_desc sd = { 1432c66ab6faSJens Axboe .len = len, 1433c66ab6faSJens Axboe .total_len = len, 1434c66ab6faSJens Axboe .flags = flags, 1435c66ab6faSJens Axboe .pos = *ppos, 14366a14b90bSJens Axboe .u.file = out, 14377995bd28SAl Viro .opos = opos, 1438c66ab6faSJens Axboe }; 143951a92c0fSJens Axboe long ret; 1440c66ab6faSJens Axboe 144118c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 144218c67cb9SAl Viro return -EBADF; 144318c67cb9SAl Viro 144418c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 144518c67cb9SAl Viro return -EINVAL; 144618c67cb9SAl Viro 144718c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 144818c67cb9SAl Viro if (unlikely(ret < 0)) 144918c67cb9SAl Viro return ret; 145018c67cb9SAl Viro 1451c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 145251a92c0fSJens Axboe if (ret > 0) 1453a82c53a0STom Zanussi *ppos = sd.pos; 145451a92c0fSJens Axboe 1455c66ab6faSJens Axboe return ret; 1456b92ce558SJens Axboe } 1457b92ce558SJens Axboe 14587c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 14597c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 14607c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1461ddac0d39SJens Axboe 1462ddac0d39SJens Axboe /* 146383f9135bSJens Axboe * Determine where to splice to/from. 146483f9135bSJens Axboe */ 1465529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in, 1466529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 1467529565dcSIngo Molnar size_t len, unsigned int flags) 14685274f052SJens Axboe { 14697c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 14707c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 14717995bd28SAl Viro loff_t offset; 1472a4514ebdSJens Axboe long ret; 14735274f052SJens Axboe 147471993e62SLinus Torvalds ipipe = get_pipe_info(in); 147571993e62SLinus Torvalds opipe = get_pipe_info(out); 14767c77f0b3SMiklos Szeredi 14777c77f0b3SMiklos Szeredi if (ipipe && opipe) { 14787c77f0b3SMiklos Szeredi if (off_in || off_out) 14797c77f0b3SMiklos Szeredi return -ESPIPE; 14807c77f0b3SMiklos Szeredi 14817c77f0b3SMiklos Szeredi if (!(in->f_mode & FMODE_READ)) 14827c77f0b3SMiklos Szeredi return -EBADF; 14837c77f0b3SMiklos Szeredi 14847c77f0b3SMiklos Szeredi if (!(out->f_mode & FMODE_WRITE)) 14857c77f0b3SMiklos Szeredi return -EBADF; 14867c77f0b3SMiklos Szeredi 14877c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 14887c77f0b3SMiklos Szeredi if (ipipe == opipe) 14897c77f0b3SMiklos Szeredi return -EINVAL; 14907c77f0b3SMiklos Szeredi 14917c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 14927c77f0b3SMiklos Szeredi } 14937c77f0b3SMiklos Szeredi 14947c77f0b3SMiklos Szeredi if (ipipe) { 1495529565dcSIngo Molnar if (off_in) 1496529565dcSIngo Molnar return -ESPIPE; 1497b92ce558SJens Axboe if (off_out) { 149819c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1499b92ce558SJens Axboe return -EINVAL; 1500cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1501b92ce558SJens Axboe return -EFAULT; 15027995bd28SAl Viro } else { 15037995bd28SAl Viro offset = out->f_pos; 15047995bd28SAl Viro } 1505529565dcSIngo Molnar 150618c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 150718c67cb9SAl Viro return -EBADF; 150818c67cb9SAl Viro 150918c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 151018c67cb9SAl Viro return -EINVAL; 151118c67cb9SAl Viro 151218c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 151318c67cb9SAl Viro if (unlikely(ret < 0)) 151418c67cb9SAl Viro return ret; 151518c67cb9SAl Viro 1516500368f7SAl Viro file_start_write(out); 15177995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1518500368f7SAl Viro file_end_write(out); 1519a4514ebdSJens Axboe 15207995bd28SAl Viro if (!off_out) 15217995bd28SAl Viro out->f_pos = offset; 15227995bd28SAl Viro else if (copy_to_user(off_out, &offset, sizeof(loff_t))) 1523a4514ebdSJens Axboe ret = -EFAULT; 1524a4514ebdSJens Axboe 1525a4514ebdSJens Axboe return ret; 1526529565dcSIngo Molnar } 15275274f052SJens Axboe 15287c77f0b3SMiklos Szeredi if (opipe) { 1529529565dcSIngo Molnar if (off_out) 1530529565dcSIngo Molnar return -ESPIPE; 1531b92ce558SJens Axboe if (off_in) { 153219c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1533b92ce558SJens Axboe return -EINVAL; 1534cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1535b92ce558SJens Axboe return -EFAULT; 15367995bd28SAl Viro } else { 15377995bd28SAl Viro offset = in->f_pos; 15387995bd28SAl Viro } 1539529565dcSIngo Molnar 15407995bd28SAl Viro ret = do_splice_to(in, &offset, opipe, len, flags); 1541a4514ebdSJens Axboe 15427995bd28SAl Viro if (!off_in) 15437995bd28SAl Viro in->f_pos = offset; 15447995bd28SAl Viro else if (copy_to_user(off_in, &offset, sizeof(loff_t))) 1545a4514ebdSJens Axboe ret = -EFAULT; 1546a4514ebdSJens Axboe 1547a4514ebdSJens Axboe return ret; 1548529565dcSIngo Molnar } 15495274f052SJens Axboe 15505274f052SJens Axboe return -EINVAL; 15515274f052SJens Axboe } 15525274f052SJens Axboe 1553912d35f8SJens Axboe /* 1554912d35f8SJens Axboe * Map an iov into an array of pages and offset/length tupples. With the 1555912d35f8SJens Axboe * partial_page structure, we can map several non-contiguous ranges into 1556912d35f8SJens Axboe * our ones pages[] map instead of splitting that operation into pieces. 1557912d35f8SJens Axboe * Could easily be exported as a generic helper for other users, in which 1558912d35f8SJens Axboe * case one would probably want to add a 'max_nr_pages' parameter as well. 1559912d35f8SJens Axboe */ 1560912d35f8SJens Axboe static int get_iovec_page_array(const struct iovec __user *iov, 1561912d35f8SJens Axboe unsigned int nr_vecs, struct page **pages, 1562bd1a68b5SEric Dumazet struct partial_page *partial, bool aligned, 156335f3d14dSJens Axboe unsigned int pipe_buffers) 1564912d35f8SJens Axboe { 1565912d35f8SJens Axboe int buffers = 0, error = 0; 1566912d35f8SJens Axboe 1567912d35f8SJens Axboe while (nr_vecs) { 1568912d35f8SJens Axboe unsigned long off, npages; 156975723957SLinus Torvalds struct iovec entry; 1570912d35f8SJens Axboe void __user *base; 1571912d35f8SJens Axboe size_t len; 1572912d35f8SJens Axboe int i; 1573912d35f8SJens Axboe 157475723957SLinus Torvalds error = -EFAULT; 1575bc40d73cSNick Piggin if (copy_from_user(&entry, iov, sizeof(entry))) 1576912d35f8SJens Axboe break; 157775723957SLinus Torvalds 157875723957SLinus Torvalds base = entry.iov_base; 157975723957SLinus Torvalds len = entry.iov_len; 1580912d35f8SJens Axboe 1581912d35f8SJens Axboe /* 1582912d35f8SJens Axboe * Sanity check this iovec. 0 read succeeds. 1583912d35f8SJens Axboe */ 158475723957SLinus Torvalds error = 0; 1585912d35f8SJens Axboe if (unlikely(!len)) 1586912d35f8SJens Axboe break; 1587912d35f8SJens Axboe error = -EFAULT; 1588712a30e6SBastian Blank if (!access_ok(VERIFY_READ, base, len)) 1589912d35f8SJens Axboe break; 1590912d35f8SJens Axboe 1591912d35f8SJens Axboe /* 1592912d35f8SJens Axboe * Get this base offset and number of pages, then map 1593912d35f8SJens Axboe * in the user pages. 1594912d35f8SJens Axboe */ 1595912d35f8SJens Axboe off = (unsigned long) base & ~PAGE_MASK; 15967afa6fd0SJens Axboe 15977afa6fd0SJens Axboe /* 15987afa6fd0SJens Axboe * If asked for alignment, the offset must be zero and the 15997afa6fd0SJens Axboe * length a multiple of the PAGE_SIZE. 16007afa6fd0SJens Axboe */ 16017afa6fd0SJens Axboe error = -EINVAL; 16027afa6fd0SJens Axboe if (aligned && (off || len & ~PAGE_MASK)) 16037afa6fd0SJens Axboe break; 16047afa6fd0SJens Axboe 1605912d35f8SJens Axboe npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 160635f3d14dSJens Axboe if (npages > pipe_buffers - buffers) 160735f3d14dSJens Axboe npages = pipe_buffers - buffers; 1608912d35f8SJens Axboe 1609bc40d73cSNick Piggin error = get_user_pages_fast((unsigned long)base, npages, 1610bc40d73cSNick Piggin 0, &pages[buffers]); 1611912d35f8SJens Axboe 1612912d35f8SJens Axboe if (unlikely(error <= 0)) 1613912d35f8SJens Axboe break; 1614912d35f8SJens Axboe 1615912d35f8SJens Axboe /* 1616912d35f8SJens Axboe * Fill this contiguous range into the partial page map. 1617912d35f8SJens Axboe */ 1618912d35f8SJens Axboe for (i = 0; i < error; i++) { 16197591489aSJens Axboe const int plen = min_t(size_t, len, PAGE_SIZE - off); 1620912d35f8SJens Axboe 1621912d35f8SJens Axboe partial[buffers].offset = off; 1622912d35f8SJens Axboe partial[buffers].len = plen; 1623912d35f8SJens Axboe 1624912d35f8SJens Axboe off = 0; 1625912d35f8SJens Axboe len -= plen; 1626912d35f8SJens Axboe buffers++; 1627912d35f8SJens Axboe } 1628912d35f8SJens Axboe 1629912d35f8SJens Axboe /* 1630912d35f8SJens Axboe * We didn't complete this iov, stop here since it probably 1631912d35f8SJens Axboe * means we have to move some of this into a pipe to 1632912d35f8SJens Axboe * be able to continue. 1633912d35f8SJens Axboe */ 1634912d35f8SJens Axboe if (len) 1635912d35f8SJens Axboe break; 1636912d35f8SJens Axboe 1637912d35f8SJens Axboe /* 1638912d35f8SJens Axboe * Don't continue if we mapped fewer pages than we asked for, 1639912d35f8SJens Axboe * or if we mapped the max number of pages that we have 1640912d35f8SJens Axboe * room for. 1641912d35f8SJens Axboe */ 164235f3d14dSJens Axboe if (error < npages || buffers == pipe_buffers) 1643912d35f8SJens Axboe break; 1644912d35f8SJens Axboe 1645912d35f8SJens Axboe nr_vecs--; 1646912d35f8SJens Axboe iov++; 1647912d35f8SJens Axboe } 1648912d35f8SJens Axboe 1649912d35f8SJens Axboe if (buffers) 1650912d35f8SJens Axboe return buffers; 1651912d35f8SJens Axboe 1652912d35f8SJens Axboe return error; 1653912d35f8SJens Axboe } 1654912d35f8SJens Axboe 16556a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 16566a14b90bSJens Axboe struct splice_desc *sd) 16576a14b90bSJens Axboe { 16586130f531SAl Viro int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 16596130f531SAl Viro return n == sd->len ? n : -EFAULT; 16606a14b90bSJens Axboe } 16616a14b90bSJens Axboe 16626a14b90bSJens Axboe /* 16636a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 16646a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 16656a14b90bSJens Axboe */ 16666130f531SAl Viro static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov, 16676a14b90bSJens Axboe unsigned long nr_segs, unsigned int flags) 16686a14b90bSJens Axboe { 16696a14b90bSJens Axboe struct pipe_inode_info *pipe; 16706a14b90bSJens Axboe struct splice_desc sd; 16716a14b90bSJens Axboe long ret; 16726130f531SAl Viro struct iovec iovstack[UIO_FASTIOV]; 16736130f531SAl Viro struct iovec *iov = iovstack; 16746130f531SAl Viro struct iov_iter iter; 16756130f531SAl Viro ssize_t count = 0; 16766a14b90bSJens Axboe 167771993e62SLinus Torvalds pipe = get_pipe_info(file); 16786a14b90bSJens Axboe if (!pipe) 16796a14b90bSJens Axboe return -EBADF; 16806a14b90bSJens Axboe 16816130f531SAl Viro ret = rw_copy_check_uvector(READ, uiov, nr_segs, 16826130f531SAl Viro ARRAY_SIZE(iovstack), iovstack, &iov); 16836130f531SAl Viro if (ret <= 0) 16846130f531SAl Viro return ret; 16856a14b90bSJens Axboe 168671d8e532SAl Viro iov_iter_init(&iter, READ, iov, nr_segs, count); 16878811930dSJens Axboe 16886a14b90bSJens Axboe sd.len = 0; 16896130f531SAl Viro sd.total_len = count; 16906a14b90bSJens Axboe sd.flags = flags; 16916130f531SAl Viro sd.u.data = &iter; 16926a14b90bSJens Axboe sd.pos = 0; 16936a14b90bSJens Axboe 16946130f531SAl Viro pipe_lock(pipe); 16956130f531SAl Viro ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 169661e0d47cSMiklos Szeredi pipe_unlock(pipe); 16976a14b90bSJens Axboe 16986130f531SAl Viro if (iov != iovstack) 16996130f531SAl Viro kfree(iov); 17006a14b90bSJens Axboe 17016a14b90bSJens Axboe return ret; 17026a14b90bSJens Axboe } 17036a14b90bSJens Axboe 1704912d35f8SJens Axboe /* 1705912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1706912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1707912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1708912d35f8SJens Axboe */ 17096a14b90bSJens Axboe static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov, 1710912d35f8SJens Axboe unsigned long nr_segs, unsigned int flags) 1711912d35f8SJens Axboe { 1712ddac0d39SJens Axboe struct pipe_inode_info *pipe; 171335f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 171435f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 1715912d35f8SJens Axboe struct splice_pipe_desc spd = { 1716912d35f8SJens Axboe .pages = pages, 1717912d35f8SJens Axboe .partial = partial, 1718047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1719912d35f8SJens Axboe .flags = flags, 1720912d35f8SJens Axboe .ops = &user_page_pipe_buf_ops, 1721bbdfc2f7SJens Axboe .spd_release = spd_release_page, 1722912d35f8SJens Axboe }; 172335f3d14dSJens Axboe long ret; 1724912d35f8SJens Axboe 172571993e62SLinus Torvalds pipe = get_pipe_info(file); 1726ddac0d39SJens Axboe if (!pipe) 1727912d35f8SJens Axboe return -EBADF; 1728912d35f8SJens Axboe 172935f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 173035f3d14dSJens Axboe return -ENOMEM; 1731912d35f8SJens Axboe 173235f3d14dSJens Axboe spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages, 1733bd1a68b5SEric Dumazet spd.partial, false, 1734047fe360SEric Dumazet spd.nr_pages_max); 173535f3d14dSJens Axboe if (spd.nr_pages <= 0) 173635f3d14dSJens Axboe ret = spd.nr_pages; 173735f3d14dSJens Axboe else 173835f3d14dSJens Axboe ret = splice_to_pipe(pipe, &spd); 173935f3d14dSJens Axboe 1740047fe360SEric Dumazet splice_shrink_spd(&spd); 174135f3d14dSJens Axboe return ret; 1742912d35f8SJens Axboe } 1743912d35f8SJens Axboe 17446a14b90bSJens Axboe /* 17456a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 17466a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 17476a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 17486a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 17496a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 17506a14b90bSJens Axboe * solutions for that: 17516a14b90bSJens Axboe * 17526a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 17536a14b90bSJens Axboe * do a regular read() on the buffer anyway. 17546a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 17556a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 17566a14b90bSJens Axboe * 17576a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 17586a14b90bSJens Axboe * 17596a14b90bSJens Axboe */ 1760836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, 1761836f92adSHeiko Carstens unsigned long, nr_segs, unsigned int, flags) 1762912d35f8SJens Axboe { 17632903ff01SAl Viro struct fd f; 1764912d35f8SJens Axboe long error; 1765912d35f8SJens Axboe 17666a14b90bSJens Axboe if (unlikely(nr_segs > UIO_MAXIOV)) 17676a14b90bSJens Axboe return -EINVAL; 17686a14b90bSJens Axboe else if (unlikely(!nr_segs)) 17696a14b90bSJens Axboe return 0; 17706a14b90bSJens Axboe 1771912d35f8SJens Axboe error = -EBADF; 17722903ff01SAl Viro f = fdget(fd); 17732903ff01SAl Viro if (f.file) { 17742903ff01SAl Viro if (f.file->f_mode & FMODE_WRITE) 17752903ff01SAl Viro error = vmsplice_to_pipe(f.file, iov, nr_segs, flags); 17762903ff01SAl Viro else if (f.file->f_mode & FMODE_READ) 17772903ff01SAl Viro error = vmsplice_to_user(f.file, iov, nr_segs, flags); 1778912d35f8SJens Axboe 17792903ff01SAl Viro fdput(f); 1780912d35f8SJens Axboe } 1781912d35f8SJens Axboe 1782912d35f8SJens Axboe return error; 1783912d35f8SJens Axboe } 1784912d35f8SJens Axboe 178576b021d0SAl Viro #ifdef CONFIG_COMPAT 178676b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32, 178776b021d0SAl Viro unsigned int, nr_segs, unsigned int, flags) 178876b021d0SAl Viro { 178976b021d0SAl Viro unsigned i; 179076b021d0SAl Viro struct iovec __user *iov; 179176b021d0SAl Viro if (nr_segs > UIO_MAXIOV) 179276b021d0SAl Viro return -EINVAL; 179376b021d0SAl Viro iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec)); 179476b021d0SAl Viro for (i = 0; i < nr_segs; i++) { 179576b021d0SAl Viro struct compat_iovec v; 179676b021d0SAl Viro if (get_user(v.iov_base, &iov32[i].iov_base) || 179776b021d0SAl Viro get_user(v.iov_len, &iov32[i].iov_len) || 179876b021d0SAl Viro put_user(compat_ptr(v.iov_base), &iov[i].iov_base) || 179976b021d0SAl Viro put_user(v.iov_len, &iov[i].iov_len)) 180076b021d0SAl Viro return -EFAULT; 180176b021d0SAl Viro } 180276b021d0SAl Viro return sys_vmsplice(fd, iov, nr_segs, flags); 180376b021d0SAl Viro } 180476b021d0SAl Viro #endif 180576b021d0SAl Viro 1806836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1807836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1808836f92adSHeiko Carstens size_t, len, unsigned int, flags) 18095274f052SJens Axboe { 18102903ff01SAl Viro struct fd in, out; 18115274f052SJens Axboe long error; 18125274f052SJens Axboe 18135274f052SJens Axboe if (unlikely(!len)) 18145274f052SJens Axboe return 0; 18155274f052SJens Axboe 18165274f052SJens Axboe error = -EBADF; 18172903ff01SAl Viro in = fdget(fd_in); 18182903ff01SAl Viro if (in.file) { 18192903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 18202903ff01SAl Viro out = fdget(fd_out); 18212903ff01SAl Viro if (out.file) { 18222903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 18232903ff01SAl Viro error = do_splice(in.file, off_in, 18242903ff01SAl Viro out.file, off_out, 1825529565dcSIngo Molnar len, flags); 18262903ff01SAl Viro fdput(out); 18275274f052SJens Axboe } 18285274f052SJens Axboe } 18292903ff01SAl Viro fdput(in); 18305274f052SJens Axboe } 18315274f052SJens Axboe return error; 18325274f052SJens Axboe } 183370524490SJens Axboe 183470524490SJens Axboe /* 1835aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1836aadd06e5SJens Axboe * return an appropriate error. 1837aadd06e5SJens Axboe */ 18387c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1839aadd06e5SJens Axboe { 1840aadd06e5SJens Axboe int ret; 1841aadd06e5SJens Axboe 1842aadd06e5SJens Axboe /* 1843aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1844aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1845aadd06e5SJens Axboe */ 1846aadd06e5SJens Axboe if (pipe->nrbufs) 1847aadd06e5SJens Axboe return 0; 1848aadd06e5SJens Axboe 1849aadd06e5SJens Axboe ret = 0; 185061e0d47cSMiklos Szeredi pipe_lock(pipe); 1851aadd06e5SJens Axboe 1852aadd06e5SJens Axboe while (!pipe->nrbufs) { 1853aadd06e5SJens Axboe if (signal_pending(current)) { 1854aadd06e5SJens Axboe ret = -ERESTARTSYS; 1855aadd06e5SJens Axboe break; 1856aadd06e5SJens Axboe } 1857aadd06e5SJens Axboe if (!pipe->writers) 1858aadd06e5SJens Axboe break; 1859aadd06e5SJens Axboe if (!pipe->waiting_writers) { 1860aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1861aadd06e5SJens Axboe ret = -EAGAIN; 1862aadd06e5SJens Axboe break; 1863aadd06e5SJens Axboe } 1864aadd06e5SJens Axboe } 1865aadd06e5SJens Axboe pipe_wait(pipe); 1866aadd06e5SJens Axboe } 1867aadd06e5SJens Axboe 186861e0d47cSMiklos Szeredi pipe_unlock(pipe); 1869aadd06e5SJens Axboe return ret; 1870aadd06e5SJens Axboe } 1871aadd06e5SJens Axboe 1872aadd06e5SJens Axboe /* 1873aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1874aadd06e5SJens Axboe * return an appropriate error. 1875aadd06e5SJens Axboe */ 18767c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1877aadd06e5SJens Axboe { 1878aadd06e5SJens Axboe int ret; 1879aadd06e5SJens Axboe 1880aadd06e5SJens Axboe /* 1881aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1882aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1883aadd06e5SJens Axboe */ 188435f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 1885aadd06e5SJens Axboe return 0; 1886aadd06e5SJens Axboe 1887aadd06e5SJens Axboe ret = 0; 188861e0d47cSMiklos Szeredi pipe_lock(pipe); 1889aadd06e5SJens Axboe 189035f3d14dSJens Axboe while (pipe->nrbufs >= pipe->buffers) { 1891aadd06e5SJens Axboe if (!pipe->readers) { 1892aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1893aadd06e5SJens Axboe ret = -EPIPE; 1894aadd06e5SJens Axboe break; 1895aadd06e5SJens Axboe } 1896aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1897aadd06e5SJens Axboe ret = -EAGAIN; 1898aadd06e5SJens Axboe break; 1899aadd06e5SJens Axboe } 1900aadd06e5SJens Axboe if (signal_pending(current)) { 1901aadd06e5SJens Axboe ret = -ERESTARTSYS; 1902aadd06e5SJens Axboe break; 1903aadd06e5SJens Axboe } 1904aadd06e5SJens Axboe pipe->waiting_writers++; 1905aadd06e5SJens Axboe pipe_wait(pipe); 1906aadd06e5SJens Axboe pipe->waiting_writers--; 1907aadd06e5SJens Axboe } 1908aadd06e5SJens Axboe 190961e0d47cSMiklos Szeredi pipe_unlock(pipe); 1910aadd06e5SJens Axboe return ret; 1911aadd06e5SJens Axboe } 1912aadd06e5SJens Axboe 1913aadd06e5SJens Axboe /* 19147c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 19157c77f0b3SMiklos Szeredi */ 19167c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 19177c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 19187c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 19197c77f0b3SMiklos Szeredi { 19207c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 19217c77f0b3SMiklos Szeredi int ret = 0, nbuf; 19227c77f0b3SMiklos Szeredi bool input_wakeup = false; 19237c77f0b3SMiklos Szeredi 19247c77f0b3SMiklos Szeredi 19257c77f0b3SMiklos Szeredi retry: 19267c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 19277c77f0b3SMiklos Szeredi if (ret) 19287c77f0b3SMiklos Szeredi return ret; 19297c77f0b3SMiklos Szeredi 19307c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 19317c77f0b3SMiklos Szeredi if (ret) 19327c77f0b3SMiklos Szeredi return ret; 19337c77f0b3SMiklos Szeredi 19347c77f0b3SMiklos Szeredi /* 19357c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 19367c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 19377c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 19387c77f0b3SMiklos Szeredi */ 19397c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 19407c77f0b3SMiklos Szeredi 19417c77f0b3SMiklos Szeredi do { 19427c77f0b3SMiklos Szeredi if (!opipe->readers) { 19437c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 19447c77f0b3SMiklos Szeredi if (!ret) 19457c77f0b3SMiklos Szeredi ret = -EPIPE; 19467c77f0b3SMiklos Szeredi break; 19477c77f0b3SMiklos Szeredi } 19487c77f0b3SMiklos Szeredi 19497c77f0b3SMiklos Szeredi if (!ipipe->nrbufs && !ipipe->writers) 19507c77f0b3SMiklos Szeredi break; 19517c77f0b3SMiklos Szeredi 19527c77f0b3SMiklos Szeredi /* 19537c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 19547c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 19557c77f0b3SMiklos Szeredi */ 195635f3d14dSJens Axboe if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) { 19577c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 19587c77f0b3SMiklos Szeredi if (ret) 19597c77f0b3SMiklos Szeredi break; 19607c77f0b3SMiklos Szeredi 19617c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 19627c77f0b3SMiklos Szeredi ret = -EAGAIN; 19637c77f0b3SMiklos Szeredi break; 19647c77f0b3SMiklos Szeredi } 19657c77f0b3SMiklos Szeredi 19667c77f0b3SMiklos Szeredi /* 19677c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 19687c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 19697c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 19707c77f0b3SMiklos Szeredi */ 19717c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 19727c77f0b3SMiklos Szeredi pipe_unlock(opipe); 19737c77f0b3SMiklos Szeredi goto retry; 19747c77f0b3SMiklos Szeredi } 19757c77f0b3SMiklos Szeredi 19767c77f0b3SMiklos Szeredi ibuf = ipipe->bufs + ipipe->curbuf; 197735f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 19787c77f0b3SMiklos Szeredi obuf = opipe->bufs + nbuf; 19797c77f0b3SMiklos Szeredi 19807c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 19817c77f0b3SMiklos Szeredi /* 19827c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 19837c77f0b3SMiklos Szeredi */ 19847c77f0b3SMiklos Szeredi *obuf = *ibuf; 19857c77f0b3SMiklos Szeredi ibuf->ops = NULL; 19867c77f0b3SMiklos Szeredi opipe->nrbufs++; 198735f3d14dSJens Axboe ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1); 19887c77f0b3SMiklos Szeredi ipipe->nrbufs--; 19897c77f0b3SMiklos Szeredi input_wakeup = true; 19907c77f0b3SMiklos Szeredi } else { 19917c77f0b3SMiklos Szeredi /* 19927c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 19937c77f0b3SMiklos Szeredi * so we can copy the contents over. 19947c77f0b3SMiklos Szeredi */ 19957c77f0b3SMiklos Szeredi ibuf->ops->get(ipipe, ibuf); 19967c77f0b3SMiklos Szeredi *obuf = *ibuf; 19977c77f0b3SMiklos Szeredi 19987c77f0b3SMiklos Szeredi /* 19997c77f0b3SMiklos Szeredi * Don't inherit the gift flag, we need to 20007c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 20017c77f0b3SMiklos Szeredi */ 20027c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 20037c77f0b3SMiklos Szeredi 20047c77f0b3SMiklos Szeredi obuf->len = len; 20057c77f0b3SMiklos Szeredi opipe->nrbufs++; 20067c77f0b3SMiklos Szeredi ibuf->offset += obuf->len; 20077c77f0b3SMiklos Szeredi ibuf->len -= obuf->len; 20087c77f0b3SMiklos Szeredi } 20097c77f0b3SMiklos Szeredi ret += obuf->len; 20107c77f0b3SMiklos Szeredi len -= obuf->len; 20117c77f0b3SMiklos Szeredi } while (len); 20127c77f0b3SMiklos Szeredi 20137c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 20147c77f0b3SMiklos Szeredi pipe_unlock(opipe); 20157c77f0b3SMiklos Szeredi 20167c77f0b3SMiklos Szeredi /* 20177c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 20187c77f0b3SMiklos Szeredi */ 2019825cdcb1SNamhyung Kim if (ret > 0) 2020825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 2021825cdcb1SNamhyung Kim 20227c77f0b3SMiklos Szeredi if (input_wakeup) 20237c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 20247c77f0b3SMiklos Szeredi 20257c77f0b3SMiklos Szeredi return ret; 20267c77f0b3SMiklos Szeredi } 20277c77f0b3SMiklos Szeredi 20287c77f0b3SMiklos Szeredi /* 202970524490SJens Axboe * Link contents of ipipe to opipe. 203070524490SJens Axboe */ 203170524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 203270524490SJens Axboe struct pipe_inode_info *opipe, 203370524490SJens Axboe size_t len, unsigned int flags) 203470524490SJens Axboe { 203570524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 2036aadd06e5SJens Axboe int ret = 0, i = 0, nbuf; 203770524490SJens Axboe 203870524490SJens Axboe /* 203970524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 204061e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 204170524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 204270524490SJens Axboe */ 204361e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 204470524490SJens Axboe 2045aadd06e5SJens Axboe do { 204670524490SJens Axboe if (!opipe->readers) { 204770524490SJens Axboe send_sig(SIGPIPE, current, 0); 204870524490SJens Axboe if (!ret) 204970524490SJens Axboe ret = -EPIPE; 205070524490SJens Axboe break; 205170524490SJens Axboe } 205270524490SJens Axboe 205370524490SJens Axboe /* 2054aadd06e5SJens Axboe * If we have iterated all input buffers or ran out of 2055aadd06e5SJens Axboe * output room, break. 205670524490SJens Axboe */ 205735f3d14dSJens Axboe if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) 2058aadd06e5SJens Axboe break; 2059aadd06e5SJens Axboe 206035f3d14dSJens Axboe ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1)); 206135f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 206270524490SJens Axboe 206370524490SJens Axboe /* 206470524490SJens Axboe * Get a reference to this pipe buffer, 206570524490SJens Axboe * so we can copy the contents over. 206670524490SJens Axboe */ 206770524490SJens Axboe ibuf->ops->get(ipipe, ibuf); 206870524490SJens Axboe 206970524490SJens Axboe obuf = opipe->bufs + nbuf; 207070524490SJens Axboe *obuf = *ibuf; 207170524490SJens Axboe 20727afa6fd0SJens Axboe /* 20737afa6fd0SJens Axboe * Don't inherit the gift flag, we need to 20747afa6fd0SJens Axboe * prevent multiple steals of this page. 20757afa6fd0SJens Axboe */ 20767afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 20777afa6fd0SJens Axboe 207870524490SJens Axboe if (obuf->len > len) 207970524490SJens Axboe obuf->len = len; 208070524490SJens Axboe 208170524490SJens Axboe opipe->nrbufs++; 208270524490SJens Axboe ret += obuf->len; 208370524490SJens Axboe len -= obuf->len; 2084aadd06e5SJens Axboe i++; 2085aadd06e5SJens Axboe } while (len); 208670524490SJens Axboe 208702cf01aeSJens Axboe /* 208802cf01aeSJens Axboe * return EAGAIN if we have the potential of some data in the 208902cf01aeSJens Axboe * future, otherwise just return 0 209002cf01aeSJens Axboe */ 209102cf01aeSJens Axboe if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) 209202cf01aeSJens Axboe ret = -EAGAIN; 209302cf01aeSJens Axboe 209461e0d47cSMiklos Szeredi pipe_unlock(ipipe); 209561e0d47cSMiklos Szeredi pipe_unlock(opipe); 209670524490SJens Axboe 2097aadd06e5SJens Axboe /* 2098aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 2099aadd06e5SJens Axboe */ 2100825cdcb1SNamhyung Kim if (ret > 0) 2101825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 210270524490SJens Axboe 210370524490SJens Axboe return ret; 210470524490SJens Axboe } 210570524490SJens Axboe 210670524490SJens Axboe /* 210770524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 210870524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 210970524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 211070524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 211170524490SJens Axboe */ 211270524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len, 211370524490SJens Axboe unsigned int flags) 211470524490SJens Axboe { 211571993e62SLinus Torvalds struct pipe_inode_info *ipipe = get_pipe_info(in); 211671993e62SLinus Torvalds struct pipe_inode_info *opipe = get_pipe_info(out); 2117aadd06e5SJens Axboe int ret = -EINVAL; 211870524490SJens Axboe 211970524490SJens Axboe /* 2120aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 2121aadd06e5SJens Axboe * copying the data. 212270524490SJens Axboe */ 2123aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 2124aadd06e5SJens Axboe /* 2125aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 2126aadd06e5SJens Axboe * ordering doesn't really matter. 2127aadd06e5SJens Axboe */ 21287c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 2129aadd06e5SJens Axboe if (!ret) { 21307c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 213102cf01aeSJens Axboe if (!ret) 2132aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 2133aadd06e5SJens Axboe } 2134aadd06e5SJens Axboe } 213570524490SJens Axboe 2136aadd06e5SJens Axboe return ret; 213770524490SJens Axboe } 213870524490SJens Axboe 2139836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 214070524490SJens Axboe { 21412903ff01SAl Viro struct fd in; 21422903ff01SAl Viro int error; 214370524490SJens Axboe 214470524490SJens Axboe if (unlikely(!len)) 214570524490SJens Axboe return 0; 214670524490SJens Axboe 214770524490SJens Axboe error = -EBADF; 21482903ff01SAl Viro in = fdget(fdin); 21492903ff01SAl Viro if (in.file) { 21502903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 21512903ff01SAl Viro struct fd out = fdget(fdout); 21522903ff01SAl Viro if (out.file) { 21532903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 21542903ff01SAl Viro error = do_tee(in.file, out.file, 21552903ff01SAl Viro len, flags); 21562903ff01SAl Viro fdput(out); 215770524490SJens Axboe } 215870524490SJens Axboe } 21592903ff01SAl Viro fdput(in); 216070524490SJens Axboe } 216170524490SJens Axboe 216270524490SJens Axboe return error; 216370524490SJens Axboe } 2164