15274f052SJens Axboe /* 25274f052SJens Axboe * "splice": joining two ropes together by interweaving their strands. 35274f052SJens Axboe * 45274f052SJens Axboe * This is the "extended pipe" functionality, where a pipe is used as 55274f052SJens Axboe * an arbitrary in-memory buffer. Think of a pipe as a small kernel 65274f052SJens Axboe * buffer that you can use to transfer data from one end to the other. 75274f052SJens Axboe * 85274f052SJens Axboe * The traditional unix read/write is extended with a "splice()" operation 95274f052SJens Axboe * that transfers data buffers to or from a pipe buffer. 105274f052SJens Axboe * 115274f052SJens Axboe * Named by Larry McVoy, original implementation from Linus, extended by 12c2058e06SJens Axboe * Jens to support splicing to files, network, direct splicing, etc and 13c2058e06SJens Axboe * fixing lots of bugs. 145274f052SJens Axboe * 150fe23479SJens Axboe * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> 16c2058e06SJens Axboe * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> 17c2058e06SJens Axboe * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> 185274f052SJens Axboe * 195274f052SJens Axboe */ 205274f052SJens Axboe #include <linux/fs.h> 215274f052SJens Axboe #include <linux/file.h> 225274f052SJens Axboe #include <linux/pagemap.h> 23d6b29d7cSJens Axboe #include <linux/splice.h> 2408e552c6SKAMEZAWA Hiroyuki #include <linux/memcontrol.h> 255274f052SJens Axboe #include <linux/mm_inline.h> 265abc97aaSJens Axboe #include <linux/swap.h> 274f6f0bd2SJens Axboe #include <linux/writeback.h> 28630d9c47SPaul Gortmaker #include <linux/export.h> 294f6f0bd2SJens Axboe #include <linux/syscalls.h> 30912d35f8SJens Axboe #include <linux/uio.h> 3129ce2058SJames Morris #include <linux/security.h> 325a0e3ad6STejun Heo #include <linux/gfp.h> 3335f9c09fSEric Dumazet #include <linux/socket.h> 3476b021d0SAl Viro #include <linux/compat.h> 3506ae43f3SAl Viro #include "internal.h" 365274f052SJens Axboe 3783f9135bSJens Axboe /* 3883f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 3983f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 4083f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 4183f9135bSJens Axboe * attempt to reuse this page for another destination. 4283f9135bSJens Axboe */ 4376ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, 445abc97aaSJens Axboe struct pipe_buffer *buf) 455abc97aaSJens Axboe { 465abc97aaSJens Axboe struct page *page = buf->page; 479e94cd4fSJens Axboe struct address_space *mapping; 485abc97aaSJens Axboe 499e0267c2SJens Axboe lock_page(page); 509e0267c2SJens Axboe 519e94cd4fSJens Axboe mapping = page_mapping(page); 529e94cd4fSJens Axboe if (mapping) { 535abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 545abc97aaSJens Axboe 55ad8d6f0aSJens Axboe /* 569e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 579e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 589e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 599e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 609e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 619e94cd4fSJens Axboe * ensues. 62ad8d6f0aSJens Axboe */ 63ad8d6f0aSJens Axboe wait_on_page_writeback(page); 64ad8d6f0aSJens Axboe 65266cf658SDavid Howells if (page_has_private(page) && 66266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 67ca39d651SJens Axboe goto out_unlock; 684f6f0bd2SJens Axboe 699e94cd4fSJens Axboe /* 709e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 719e94cd4fSJens Axboe * and return good. 729e94cd4fSJens Axboe */ 739e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 741432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 755abc97aaSJens Axboe return 0; 765abc97aaSJens Axboe } 779e94cd4fSJens Axboe } 789e94cd4fSJens Axboe 799e94cd4fSJens Axboe /* 809e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 819e94cd4fSJens Axboe * address space, unlock and return failure. 829e94cd4fSJens Axboe */ 83ca39d651SJens Axboe out_unlock: 849e94cd4fSJens Axboe unlock_page(page); 859e94cd4fSJens Axboe return 1; 869e94cd4fSJens Axboe } 875abc97aaSJens Axboe 8876ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 895274f052SJens Axboe struct pipe_buffer *buf) 905274f052SJens Axboe { 9109cbfeafSKirill A. Shutemov put_page(buf->page); 921432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 935274f052SJens Axboe } 945274f052SJens Axboe 950845718dSJens Axboe /* 960845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 970845718dSJens Axboe * is a page cache page, IO may be in flight. 980845718dSJens Axboe */ 99cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 1005274f052SJens Axboe struct pipe_buffer *buf) 1015274f052SJens Axboe { 1025274f052SJens Axboe struct page *page = buf->page; 10349d0b21bSJens Axboe int err; 1045274f052SJens Axboe 1055274f052SJens Axboe if (!PageUptodate(page)) { 10649d0b21bSJens Axboe lock_page(page); 1075274f052SJens Axboe 10849d0b21bSJens Axboe /* 10949d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 11073d62d83SIngo Molnar * splice, if this is the first page. 11149d0b21bSJens Axboe */ 1125274f052SJens Axboe if (!page->mapping) { 11349d0b21bSJens Axboe err = -ENODATA; 11449d0b21bSJens Axboe goto error; 1155274f052SJens Axboe } 1165274f052SJens Axboe 11749d0b21bSJens Axboe /* 11873d62d83SIngo Molnar * Uh oh, read-error from disk. 11949d0b21bSJens Axboe */ 12049d0b21bSJens Axboe if (!PageUptodate(page)) { 12149d0b21bSJens Axboe err = -EIO; 12249d0b21bSJens Axboe goto error; 12349d0b21bSJens Axboe } 12449d0b21bSJens Axboe 12549d0b21bSJens Axboe /* 126f84d7519SJens Axboe * Page is ok afterall, we are done. 12749d0b21bSJens Axboe */ 12849d0b21bSJens Axboe unlock_page(page); 12949d0b21bSJens Axboe } 13049d0b21bSJens Axboe 131f84d7519SJens Axboe return 0; 13249d0b21bSJens Axboe error: 13349d0b21bSJens Axboe unlock_page(page); 134f84d7519SJens Axboe return err; 13570524490SJens Axboe } 13670524490SJens Axboe 137708e3508SHugh Dickins const struct pipe_buf_operations page_cache_pipe_buf_ops = { 1385274f052SJens Axboe .can_merge = 0, 139cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1405274f052SJens Axboe .release = page_cache_pipe_buf_release, 1415abc97aaSJens Axboe .steal = page_cache_pipe_buf_steal, 142f84d7519SJens Axboe .get = generic_pipe_buf_get, 1435274f052SJens Axboe }; 1445274f052SJens Axboe 145912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, 146912d35f8SJens Axboe struct pipe_buffer *buf) 147912d35f8SJens Axboe { 1487afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 149912d35f8SJens Axboe return 1; 1507afa6fd0SJens Axboe 1511432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 152330ab716SJens Axboe return generic_pipe_buf_steal(pipe, buf); 153912d35f8SJens Axboe } 154912d35f8SJens Axboe 155d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 156912d35f8SJens Axboe .can_merge = 0, 157cac36bb0SJens Axboe .confirm = generic_pipe_buf_confirm, 158912d35f8SJens Axboe .release = page_cache_pipe_buf_release, 159912d35f8SJens Axboe .steal = user_page_pipe_buf_steal, 160f84d7519SJens Axboe .get = generic_pipe_buf_get, 161912d35f8SJens Axboe }; 162912d35f8SJens Axboe 163825cdcb1SNamhyung Kim static void wakeup_pipe_readers(struct pipe_inode_info *pipe) 164825cdcb1SNamhyung Kim { 165825cdcb1SNamhyung Kim smp_mb(); 166825cdcb1SNamhyung Kim if (waitqueue_active(&pipe->wait)) 167825cdcb1SNamhyung Kim wake_up_interruptible(&pipe->wait); 168825cdcb1SNamhyung Kim kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 169825cdcb1SNamhyung Kim } 170825cdcb1SNamhyung Kim 171932cc6d4SJens Axboe /** 172932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 173932cc6d4SJens Axboe * @pipe: pipe to fill 174932cc6d4SJens Axboe * @spd: data to fill 175932cc6d4SJens Axboe * 176932cc6d4SJens Axboe * Description: 17779685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 178932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 179932cc6d4SJens Axboe * function will link that data to the pipe. 180932cc6d4SJens Axboe * 18183f9135bSJens Axboe */ 182d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 183912d35f8SJens Axboe struct splice_pipe_desc *spd) 1845274f052SJens Axboe { 18500de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 186912d35f8SJens Axboe int ret, do_wakeup, page_nr; 1875274f052SJens Axboe 188d6785d91SRabin Vincent if (!spd_pages) 189d6785d91SRabin Vincent return 0; 190d6785d91SRabin Vincent 1915274f052SJens Axboe ret = 0; 1925274f052SJens Axboe do_wakeup = 0; 193912d35f8SJens Axboe page_nr = 0; 1945274f052SJens Axboe 19561e0d47cSMiklos Szeredi pipe_lock(pipe); 1965274f052SJens Axboe 1975274f052SJens Axboe for (;;) { 1983a326a2cSIngo Molnar if (!pipe->readers) { 1995274f052SJens Axboe send_sig(SIGPIPE, current, 0); 2005274f052SJens Axboe if (!ret) 2015274f052SJens Axboe ret = -EPIPE; 2025274f052SJens Axboe break; 2035274f052SJens Axboe } 2045274f052SJens Axboe 20535f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) { 20635f3d14dSJens Axboe int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1); 2073a326a2cSIngo Molnar struct pipe_buffer *buf = pipe->bufs + newbuf; 2085274f052SJens Axboe 209912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 210912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 211912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 212497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 213912d35f8SJens Axboe buf->ops = spd->ops; 2147afa6fd0SJens Axboe if (spd->flags & SPLICE_F_GIFT) 2157afa6fd0SJens Axboe buf->flags |= PIPE_BUF_FLAG_GIFT; 2167afa6fd0SJens Axboe 2176f767b04SJens Axboe pipe->nrbufs++; 218912d35f8SJens Axboe page_nr++; 219912d35f8SJens Axboe ret += buf->len; 220912d35f8SJens Axboe 2216447a3cfSAl Viro if (pipe->files) 2225274f052SJens Axboe do_wakeup = 1; 2235274f052SJens Axboe 224912d35f8SJens Axboe if (!--spd->nr_pages) 2255274f052SJens Axboe break; 22635f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 2275274f052SJens Axboe continue; 2285274f052SJens Axboe 2295274f052SJens Axboe break; 2305274f052SJens Axboe } 2315274f052SJens Axboe 232912d35f8SJens Axboe if (spd->flags & SPLICE_F_NONBLOCK) { 23329e35094SLinus Torvalds if (!ret) 23429e35094SLinus Torvalds ret = -EAGAIN; 23529e35094SLinus Torvalds break; 23629e35094SLinus Torvalds } 23729e35094SLinus Torvalds 2385274f052SJens Axboe if (signal_pending(current)) { 2395274f052SJens Axboe if (!ret) 2405274f052SJens Axboe ret = -ERESTARTSYS; 2415274f052SJens Axboe break; 2425274f052SJens Axboe } 2435274f052SJens Axboe 2445274f052SJens Axboe if (do_wakeup) { 245e7c3c646SAl Viro wakeup_pipe_readers(pipe); 2465274f052SJens Axboe do_wakeup = 0; 2475274f052SJens Axboe } 2485274f052SJens Axboe 2493a326a2cSIngo Molnar pipe->waiting_writers++; 2503a326a2cSIngo Molnar pipe_wait(pipe); 2513a326a2cSIngo Molnar pipe->waiting_writers--; 2525274f052SJens Axboe } 2535274f052SJens Axboe 25461e0d47cSMiklos Szeredi pipe_unlock(pipe); 2555274f052SJens Axboe 256825cdcb1SNamhyung Kim if (do_wakeup) 257825cdcb1SNamhyung Kim wakeup_pipe_readers(pipe); 2585274f052SJens Axboe 25900de00bdSJens Axboe while (page_nr < spd_pages) 260bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2615274f052SJens Axboe 2625274f052SJens Axboe return ret; 2635274f052SJens Axboe } 2642b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(splice_to_pipe); 2655274f052SJens Axboe 266708e3508SHugh Dickins void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) 267bbdfc2f7SJens Axboe { 26809cbfeafSKirill A. Shutemov put_page(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 32809cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 32909cbfeafSKirill A. Shutemov loff = *ppos & ~PAGE_MASK; 33009cbfeafSKirill A. Shutemov req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_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, 363c62d2555SMichal Hocko mapping_gfp_constraint(mapping, GFP_KERNEL)); 3645274f052SJens Axboe if (unlikely(error)) { 36509cbfeafSKirill A. Shutemov put_page(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 */ 38509cbfeafSKirill A. Shutemov index = *ppos >> PAGE_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 */ 39709cbfeafSKirill A. Shutemov this_len = min_t(unsigned long, len, PAGE_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); 41890330e68SAbhi Das retry_lookup: 41932502b84SMiklos Szeredi page = find_or_create_page(mapping, index, 42032502b84SMiklos Szeredi mapping_gfp_mask(mapping)); 42132502b84SMiklos Szeredi 42232502b84SMiklos Szeredi if (!page) { 42332502b84SMiklos Szeredi error = -ENOMEM; 4247480a904SJens Axboe break; 4257480a904SJens Axboe } 42609cbfeafSKirill A. Shutemov put_page(spd.pages[page_nr]); 42735f3d14dSJens Axboe spd.pages[page_nr] = page; 42832502b84SMiklos Szeredi } 4297480a904SJens Axboe /* 4307480a904SJens Axboe * page was already under io and is now done, great 4317480a904SJens Axboe */ 4327480a904SJens Axboe if (PageUptodate(page)) { 4337480a904SJens Axboe unlock_page(page); 4347480a904SJens Axboe goto fill_it; 4357480a904SJens Axboe } 4367480a904SJens Axboe 4377480a904SJens Axboe /* 4387480a904SJens Axboe * need to read in the page 4397480a904SJens Axboe */ 4407480a904SJens Axboe error = mapping->a_ops->readpage(in, page); 4417480a904SJens Axboe if (unlikely(error)) { 442eb20796bSJens Axboe /* 44390330e68SAbhi Das * Re-lookup the page 444eb20796bSJens Axboe */ 4457480a904SJens Axboe if (error == AOP_TRUNCATED_PAGE) 44690330e68SAbhi Das goto retry_lookup; 447eb20796bSJens Axboe 4487480a904SJens Axboe break; 4497480a904SJens Axboe } 450620a324bSJens Axboe } 451620a324bSJens Axboe fill_it: 45291ad66efSJens Axboe /* 453620a324bSJens Axboe * i_size must be checked after PageUptodate. 45491ad66efSJens Axboe */ 45591ad66efSJens Axboe isize = i_size_read(mapping->host); 45609cbfeafSKirill A. Shutemov end_index = (isize - 1) >> PAGE_SHIFT; 457eb20796bSJens Axboe if (unlikely(!isize || index > end_index)) 45891ad66efSJens Axboe break; 45991ad66efSJens Axboe 46091ad66efSJens Axboe /* 46191ad66efSJens Axboe * if this is the last page, see if we need to shrink 46291ad66efSJens Axboe * the length and stop 46391ad66efSJens Axboe */ 46491ad66efSJens Axboe if (end_index == index) { 465475ecadeSHugh Dickins unsigned int plen; 466475ecadeSHugh Dickins 467475ecadeSHugh Dickins /* 468475ecadeSHugh Dickins * max good bytes in this page 469475ecadeSHugh Dickins */ 47009cbfeafSKirill A. Shutemov plen = ((isize - 1) & ~PAGE_MASK) + 1; 471475ecadeSHugh Dickins if (plen <= loff) 47291ad66efSJens Axboe break; 473475ecadeSHugh Dickins 47491ad66efSJens Axboe /* 47591ad66efSJens Axboe * force quit after adding this page 47691ad66efSJens Axboe */ 477475ecadeSHugh Dickins this_len = min(this_len, plen - loff); 478eb20796bSJens Axboe len = this_len; 47991ad66efSJens Axboe } 480620a324bSJens Axboe 48135f3d14dSJens Axboe spd.partial[page_nr].offset = loff; 48235f3d14dSJens Axboe spd.partial[page_nr].len = this_len; 48382aa5d61SJens Axboe len -= this_len; 48491ad66efSJens Axboe loff = 0; 485eb20796bSJens Axboe spd.nr_pages++; 486eb20796bSJens Axboe index++; 4875274f052SJens Axboe } 4885274f052SJens Axboe 489eb20796bSJens Axboe /* 490475ecadeSHugh Dickins * Release any pages at the end, if we quit early. 'page_nr' is how far 491eb20796bSJens Axboe * we got, 'nr_pages' is how many pages are in the map. 492eb20796bSJens Axboe */ 493eb20796bSJens Axboe while (page_nr < nr_pages) 49409cbfeafSKirill A. Shutemov put_page(spd.pages[page_nr++]); 49509cbfeafSKirill A. Shutemov in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT; 496eb20796bSJens Axboe 497912d35f8SJens Axboe if (spd.nr_pages) 49835f3d14dSJens Axboe error = splice_to_pipe(pipe, &spd); 49916c523ddSJens Axboe 500047fe360SEric Dumazet splice_shrink_spd(&spd); 5017480a904SJens Axboe return error; 5025274f052SJens Axboe } 5035274f052SJens Axboe 50483f9135bSJens Axboe /** 50583f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 50683f9135bSJens Axboe * @in: file to splice from 507932cc6d4SJens Axboe * @ppos: position in @in 50883f9135bSJens Axboe * @pipe: pipe to splice to 50983f9135bSJens Axboe * @len: number of bytes to splice 51083f9135bSJens Axboe * @flags: splice modifier flags 51183f9135bSJens Axboe * 512932cc6d4SJens Axboe * Description: 513932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 514932cc6d4SJens Axboe * used as long as the address_space operations for the source implements 515932cc6d4SJens Axboe * a readpage() hook. 516932cc6d4SJens Axboe * 51783f9135bSJens Axboe */ 518cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 519cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 520cbb7e577SJens Axboe unsigned int flags) 5215274f052SJens Axboe { 522d366d398SJens Axboe loff_t isize, left; 5238191ecd1SJens Axboe int ret; 524d366d398SJens Axboe 525be64f884SBoaz Harrosh if (IS_DAX(in->f_mapping->host)) 526be64f884SBoaz Harrosh return default_file_splice_read(in, ppos, pipe, len, flags); 527be64f884SBoaz Harrosh 528d366d398SJens Axboe isize = i_size_read(in->f_mapping->host); 529d366d398SJens Axboe if (unlikely(*ppos >= isize)) 530d366d398SJens Axboe return 0; 531d366d398SJens Axboe 532d366d398SJens Axboe left = isize - *ppos; 533d366d398SJens Axboe if (unlikely(left < len)) 534d366d398SJens Axboe len = left; 5355274f052SJens Axboe 536cbb7e577SJens Axboe ret = __generic_file_splice_read(in, ppos, pipe, len, flags); 537723590edSMiklos Szeredi if (ret > 0) { 538cbb7e577SJens Axboe *ppos += ret; 539723590edSMiklos Szeredi file_accessed(in); 540723590edSMiklos Szeredi } 5415274f052SJens Axboe 5425274f052SJens Axboe return ret; 5435274f052SJens Axboe } 544059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 545059a8f37SJens Axboe 5466818173bSMiklos Szeredi static const struct pipe_buf_operations default_pipe_buf_ops = { 5476818173bSMiklos Szeredi .can_merge = 0, 5486818173bSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 5496818173bSMiklos Szeredi .release = generic_pipe_buf_release, 5506818173bSMiklos Szeredi .steal = generic_pipe_buf_steal, 5516818173bSMiklos Szeredi .get = generic_pipe_buf_get, 5526818173bSMiklos Szeredi }; 5536818173bSMiklos Szeredi 55428a625cbSMiklos Szeredi static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe, 55528a625cbSMiklos Szeredi struct pipe_buffer *buf) 55628a625cbSMiklos Szeredi { 55728a625cbSMiklos Szeredi return 1; 55828a625cbSMiklos Szeredi } 55928a625cbSMiklos Szeredi 56028a625cbSMiklos Szeredi /* Pipe buffer operations for a socket and similar. */ 56128a625cbSMiklos Szeredi const struct pipe_buf_operations nosteal_pipe_buf_ops = { 56228a625cbSMiklos Szeredi .can_merge = 0, 56328a625cbSMiklos Szeredi .confirm = generic_pipe_buf_confirm, 56428a625cbSMiklos Szeredi .release = generic_pipe_buf_release, 56528a625cbSMiklos Szeredi .steal = generic_pipe_buf_nosteal, 56628a625cbSMiklos Szeredi .get = generic_pipe_buf_get, 56728a625cbSMiklos Szeredi }; 56828a625cbSMiklos Szeredi EXPORT_SYMBOL(nosteal_pipe_buf_ops); 56928a625cbSMiklos Szeredi 5706818173bSMiklos Szeredi static ssize_t kernel_readv(struct file *file, const struct iovec *vec, 5716818173bSMiklos Szeredi unsigned long vlen, loff_t offset) 5726818173bSMiklos Szeredi { 5736818173bSMiklos Szeredi mm_segment_t old_fs; 5746818173bSMiklos Szeredi loff_t pos = offset; 5756818173bSMiklos Szeredi ssize_t res; 5766818173bSMiklos Szeredi 5776818173bSMiklos Szeredi old_fs = get_fs(); 5786818173bSMiklos Szeredi set_fs(get_ds()); 5796818173bSMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 580793b80efSChristoph Hellwig res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0); 5816818173bSMiklos Szeredi set_fs(old_fs); 5826818173bSMiklos Szeredi 5836818173bSMiklos Szeredi return res; 5846818173bSMiklos Szeredi } 5856818173bSMiklos Szeredi 5867bb307e8SAl Viro ssize_t kernel_write(struct file *file, const char *buf, size_t count, 587b2858d7dSMiklos Szeredi loff_t pos) 5880b0a47f5SMiklos Szeredi { 5890b0a47f5SMiklos Szeredi mm_segment_t old_fs; 5900b0a47f5SMiklos Szeredi ssize_t res; 5910b0a47f5SMiklos Szeredi 5920b0a47f5SMiklos Szeredi old_fs = get_fs(); 5930b0a47f5SMiklos Szeredi set_fs(get_ds()); 5940b0a47f5SMiklos Szeredi /* The cast to a user pointer is valid due to the set_fs() */ 5957bb307e8SAl Viro res = vfs_write(file, (__force const char __user *)buf, count, &pos); 5960b0a47f5SMiklos Szeredi set_fs(old_fs); 5970b0a47f5SMiklos Szeredi 5980b0a47f5SMiklos Szeredi return res; 5990b0a47f5SMiklos Szeredi } 6007bb307e8SAl Viro EXPORT_SYMBOL(kernel_write); 6010b0a47f5SMiklos Szeredi 6026818173bSMiklos Szeredi ssize_t default_file_splice_read(struct file *in, loff_t *ppos, 6036818173bSMiklos Szeredi struct pipe_inode_info *pipe, size_t len, 6046818173bSMiklos Szeredi unsigned int flags) 6056818173bSMiklos Szeredi { 6066818173bSMiklos Szeredi unsigned int nr_pages; 6076818173bSMiklos Szeredi unsigned int nr_freed; 6086818173bSMiklos Szeredi size_t offset; 60935f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 61035f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 61135f3d14dSJens Axboe struct iovec *vec, __vec[PIPE_DEF_BUFFERS]; 6126818173bSMiklos Szeredi ssize_t res; 6136818173bSMiklos Szeredi size_t this_len; 6146818173bSMiklos Szeredi int error; 6156818173bSMiklos Szeredi int i; 6166818173bSMiklos Szeredi struct splice_pipe_desc spd = { 6176818173bSMiklos Szeredi .pages = pages, 6186818173bSMiklos Szeredi .partial = partial, 619047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 6206818173bSMiklos Szeredi .flags = flags, 6216818173bSMiklos Szeredi .ops = &default_pipe_buf_ops, 6226818173bSMiklos Szeredi .spd_release = spd_release_page, 6236818173bSMiklos Szeredi }; 6246818173bSMiklos Szeredi 62535f3d14dSJens Axboe if (splice_grow_spd(pipe, &spd)) 62635f3d14dSJens Axboe return -ENOMEM; 62735f3d14dSJens Axboe 62835f3d14dSJens Axboe res = -ENOMEM; 62935f3d14dSJens Axboe vec = __vec; 630047fe360SEric Dumazet if (spd.nr_pages_max > PIPE_DEF_BUFFERS) { 631047fe360SEric Dumazet vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL); 63235f3d14dSJens Axboe if (!vec) 63335f3d14dSJens Axboe goto shrink_ret; 63435f3d14dSJens Axboe } 63535f3d14dSJens Axboe 63609cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 63709cbfeafSKirill A. Shutemov nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; 6386818173bSMiklos Szeredi 639047fe360SEric Dumazet for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) { 6406818173bSMiklos Szeredi struct page *page; 6416818173bSMiklos Szeredi 6424f231228SJens Axboe page = alloc_page(GFP_USER); 6436818173bSMiklos Szeredi error = -ENOMEM; 6446818173bSMiklos Szeredi if (!page) 6456818173bSMiklos Szeredi goto err; 6466818173bSMiklos Szeredi 64709cbfeafSKirill A. Shutemov this_len = min_t(size_t, len, PAGE_SIZE - offset); 6484f231228SJens Axboe vec[i].iov_base = (void __user *) page_address(page); 6496818173bSMiklos Szeredi vec[i].iov_len = this_len; 65035f3d14dSJens Axboe spd.pages[i] = page; 6516818173bSMiklos Szeredi spd.nr_pages++; 6526818173bSMiklos Szeredi len -= this_len; 6536818173bSMiklos Szeredi offset = 0; 6546818173bSMiklos Szeredi } 6556818173bSMiklos Szeredi 6566818173bSMiklos Szeredi res = kernel_readv(in, vec, spd.nr_pages, *ppos); 65777f6bf57SAndrew Morton if (res < 0) { 65877f6bf57SAndrew Morton error = res; 6596818173bSMiklos Szeredi goto err; 66077f6bf57SAndrew Morton } 6616818173bSMiklos Szeredi 6626818173bSMiklos Szeredi error = 0; 6636818173bSMiklos Szeredi if (!res) 6646818173bSMiklos Szeredi goto err; 6656818173bSMiklos Szeredi 6666818173bSMiklos Szeredi nr_freed = 0; 6676818173bSMiklos Szeredi for (i = 0; i < spd.nr_pages; i++) { 6686818173bSMiklos Szeredi this_len = min_t(size_t, vec[i].iov_len, res); 66935f3d14dSJens Axboe spd.partial[i].offset = 0; 67035f3d14dSJens Axboe spd.partial[i].len = this_len; 6716818173bSMiklos Szeredi if (!this_len) { 67235f3d14dSJens Axboe __free_page(spd.pages[i]); 67335f3d14dSJens Axboe spd.pages[i] = NULL; 6746818173bSMiklos Szeredi nr_freed++; 6756818173bSMiklos Szeredi } 6766818173bSMiklos Szeredi res -= this_len; 6776818173bSMiklos Szeredi } 6786818173bSMiklos Szeredi spd.nr_pages -= nr_freed; 6796818173bSMiklos Szeredi 6806818173bSMiklos Szeredi res = splice_to_pipe(pipe, &spd); 6816818173bSMiklos Szeredi if (res > 0) 6826818173bSMiklos Szeredi *ppos += res; 6836818173bSMiklos Szeredi 68435f3d14dSJens Axboe shrink_ret: 68535f3d14dSJens Axboe if (vec != __vec) 68635f3d14dSJens Axboe kfree(vec); 687047fe360SEric Dumazet splice_shrink_spd(&spd); 6886818173bSMiklos Szeredi return res; 6896818173bSMiklos Szeredi 6906818173bSMiklos Szeredi err: 6914f231228SJens Axboe for (i = 0; i < spd.nr_pages; i++) 69235f3d14dSJens Axboe __free_page(spd.pages[i]); 6934f231228SJens Axboe 69435f3d14dSJens Axboe res = error; 69535f3d14dSJens Axboe goto shrink_ret; 6966818173bSMiklos Szeredi } 6976818173bSMiklos Szeredi EXPORT_SYMBOL(default_file_splice_read); 6986818173bSMiklos Szeredi 6995274f052SJens Axboe /* 7004f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 701016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 7025274f052SJens Axboe */ 70376ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 7045274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 7055274f052SJens Axboe { 7066a14b90bSJens Axboe struct file *file = sd->u.file; 7075274f052SJens Axboe loff_t pos = sd->pos; 708a8adbe37SMichał Mirosław int more; 7095274f052SJens Axboe 71072c2d531SAl Viro if (!likely(file->f_op->sendpage)) 711a8adbe37SMichał Mirosław return -EINVAL; 712a8adbe37SMichał Mirosław 71335f9c09fSEric Dumazet more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; 714ae62ca7bSEric Dumazet 715ae62ca7bSEric Dumazet if (sd->len < sd->total_len && pipe->nrbufs > 1) 71635f9c09fSEric Dumazet more |= MSG_SENDPAGE_NOTLAST; 717ae62ca7bSEric Dumazet 718a8adbe37SMichał Mirosław return file->f_op->sendpage(file, buf->page, buf->offset, 719f84d7519SJens Axboe sd->len, &pos, more); 7205274f052SJens Axboe } 7215274f052SJens Axboe 722b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 723b3c2d2ddSMiklos Szeredi { 724b3c2d2ddSMiklos Szeredi smp_mb(); 725b3c2d2ddSMiklos Szeredi if (waitqueue_active(&pipe->wait)) 726b3c2d2ddSMiklos Szeredi wake_up_interruptible(&pipe->wait); 727b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 728b3c2d2ddSMiklos Szeredi } 729b3c2d2ddSMiklos Szeredi 730b3c2d2ddSMiklos Szeredi /** 731b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 732b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 733b3c2d2ddSMiklos Szeredi * @sd: information to @actor 734b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 735b3c2d2ddSMiklos Szeredi * 736b3c2d2ddSMiklos Szeredi * Description: 737b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 738b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 739b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 740b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 741b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 742b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 743b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 744b3c2d2ddSMiklos Szeredi * 745b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 746b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 747b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 748b3c2d2ddSMiklos Szeredi * destination. 749b3c2d2ddSMiklos Szeredi */ 75096f9bc8fSAl Viro static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 751b3c2d2ddSMiklos Szeredi splice_actor *actor) 752b3c2d2ddSMiklos Szeredi { 753b3c2d2ddSMiklos Szeredi int ret; 754b3c2d2ddSMiklos Szeredi 755b3c2d2ddSMiklos Szeredi while (pipe->nrbufs) { 756b3c2d2ddSMiklos Szeredi struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 757b3c2d2ddSMiklos Szeredi const struct pipe_buf_operations *ops = buf->ops; 758b3c2d2ddSMiklos Szeredi 759b3c2d2ddSMiklos Szeredi sd->len = buf->len; 760b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 761b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 762b3c2d2ddSMiklos Szeredi 763a8adbe37SMichał Mirosław ret = buf->ops->confirm(pipe, buf); 764a8adbe37SMichał Mirosław if (unlikely(ret)) { 765b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 766b3c2d2ddSMiklos Szeredi ret = 0; 767b3c2d2ddSMiklos Szeredi return ret; 768b3c2d2ddSMiklos Szeredi } 769a8adbe37SMichał Mirosław 770a8adbe37SMichał Mirosław ret = actor(pipe, buf, sd); 771a8adbe37SMichał Mirosław if (ret <= 0) 772a8adbe37SMichał Mirosław return ret; 773a8adbe37SMichał Mirosław 774b3c2d2ddSMiklos Szeredi buf->offset += ret; 775b3c2d2ddSMiklos Szeredi buf->len -= ret; 776b3c2d2ddSMiklos Szeredi 777b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 778b3c2d2ddSMiklos Szeredi sd->len -= ret; 779b3c2d2ddSMiklos Szeredi sd->pos += ret; 780b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 781b3c2d2ddSMiklos Szeredi 782b3c2d2ddSMiklos Szeredi if (!buf->len) { 783b3c2d2ddSMiklos Szeredi buf->ops = NULL; 784b3c2d2ddSMiklos Szeredi ops->release(pipe, buf); 78535f3d14dSJens Axboe pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); 786b3c2d2ddSMiklos Szeredi pipe->nrbufs--; 7876447a3cfSAl Viro if (pipe->files) 788b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 789b3c2d2ddSMiklos Szeredi } 790b3c2d2ddSMiklos Szeredi 791b3c2d2ddSMiklos Szeredi if (!sd->total_len) 792b3c2d2ddSMiklos Szeredi return 0; 793b3c2d2ddSMiklos Szeredi } 794b3c2d2ddSMiklos Szeredi 795b3c2d2ddSMiklos Szeredi return 1; 796b3c2d2ddSMiklos Szeredi } 797b3c2d2ddSMiklos Szeredi 798b3c2d2ddSMiklos Szeredi /** 799b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 800b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 801b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 802b3c2d2ddSMiklos Szeredi * 803b3c2d2ddSMiklos Szeredi * Description: 804b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 805b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 806b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 807b3c2d2ddSMiklos Szeredi */ 80896f9bc8fSAl Viro static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 809b3c2d2ddSMiklos Szeredi { 810c725bfceSJan Kara /* 811c725bfceSJan Kara * Check for signal early to make process killable when there are 812c725bfceSJan Kara * always buffers available 813c725bfceSJan Kara */ 814c725bfceSJan Kara if (signal_pending(current)) 815c725bfceSJan Kara return -ERESTARTSYS; 816c725bfceSJan Kara 817b3c2d2ddSMiklos Szeredi while (!pipe->nrbufs) { 818b3c2d2ddSMiklos Szeredi if (!pipe->writers) 819b3c2d2ddSMiklos Szeredi return 0; 820b3c2d2ddSMiklos Szeredi 821b3c2d2ddSMiklos Szeredi if (!pipe->waiting_writers && sd->num_spliced) 822b3c2d2ddSMiklos Szeredi return 0; 823b3c2d2ddSMiklos Szeredi 824b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 825b3c2d2ddSMiklos Szeredi return -EAGAIN; 826b3c2d2ddSMiklos Szeredi 827b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 828b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 829b3c2d2ddSMiklos Szeredi 830b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 831b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 832b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 833b3c2d2ddSMiklos Szeredi } 834b3c2d2ddSMiklos Szeredi 835b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 836b3c2d2ddSMiklos Szeredi } 837b3c2d2ddSMiklos Szeredi 838b3c2d2ddSMiklos Szeredi return 1; 839b3c2d2ddSMiklos Szeredi } 840b3c2d2ddSMiklos Szeredi 841b3c2d2ddSMiklos Szeredi /** 842b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 843b80901bbSRandy Dunlap * @sd: information about the splice operation 844b3c2d2ddSMiklos Szeredi * 845b3c2d2ddSMiklos Szeredi * Description: 846b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 847b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 848b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 849b3c2d2ddSMiklos Szeredi */ 85096f9bc8fSAl Viro static void splice_from_pipe_begin(struct splice_desc *sd) 851b3c2d2ddSMiklos Szeredi { 852b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 853b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 854b3c2d2ddSMiklos Szeredi } 855b3c2d2ddSMiklos Szeredi 856b3c2d2ddSMiklos Szeredi /** 857b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 858b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 859b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 860b3c2d2ddSMiklos Szeredi * 861b3c2d2ddSMiklos Szeredi * Description: 862b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 863b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 864b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 865b3c2d2ddSMiklos Szeredi */ 86696f9bc8fSAl Viro static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 867b3c2d2ddSMiklos Szeredi { 868b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 869b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 870b3c2d2ddSMiklos Szeredi } 871b3c2d2ddSMiklos Szeredi 872932cc6d4SJens Axboe /** 873932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 874932cc6d4SJens Axboe * @pipe: pipe to splice from 875932cc6d4SJens Axboe * @sd: information to @actor 876932cc6d4SJens Axboe * @actor: handler that splices the data 877932cc6d4SJens Axboe * 878932cc6d4SJens Axboe * Description: 879932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 880932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 881932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 882932cc6d4SJens Axboe * pipe_to_user. 883932cc6d4SJens Axboe * 88483f9135bSJens Axboe */ 885c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 886c66ab6faSJens Axboe splice_actor *actor) 8875274f052SJens Axboe { 888b3c2d2ddSMiklos Szeredi int ret; 8895274f052SJens Axboe 890b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 891b3c2d2ddSMiklos Szeredi do { 892c2489e07SJan Kara cond_resched(); 893b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 894b3c2d2ddSMiklos Szeredi if (ret > 0) 895b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 896b3c2d2ddSMiklos Szeredi } while (ret > 0); 897b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 8985274f052SJens Axboe 899b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 9005274f052SJens Axboe } 90140bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 9025274f052SJens Axboe 903932cc6d4SJens Axboe /** 904932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 905932cc6d4SJens Axboe * @pipe: pipe to splice from 906932cc6d4SJens Axboe * @out: file to splice to 907932cc6d4SJens Axboe * @ppos: position in @out 908932cc6d4SJens Axboe * @len: how many bytes to splice 909932cc6d4SJens Axboe * @flags: splice modifier flags 910932cc6d4SJens Axboe * @actor: handler that splices the data 911932cc6d4SJens Axboe * 912932cc6d4SJens Axboe * Description: 9132933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 914932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 915932cc6d4SJens Axboe * 916932cc6d4SJens Axboe */ 9176da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 9186da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 9196da61809SMark Fasheh splice_actor *actor) 9206da61809SMark Fasheh { 9216da61809SMark Fasheh ssize_t ret; 922c66ab6faSJens Axboe struct splice_desc sd = { 923c66ab6faSJens Axboe .total_len = len, 924c66ab6faSJens Axboe .flags = flags, 925c66ab6faSJens Axboe .pos = *ppos, 9266a14b90bSJens Axboe .u.file = out, 927c66ab6faSJens Axboe }; 9286da61809SMark Fasheh 92961e0d47cSMiklos Szeredi pipe_lock(pipe); 930c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 93161e0d47cSMiklos Szeredi pipe_unlock(pipe); 9326da61809SMark Fasheh 9336da61809SMark Fasheh return ret; 9346da61809SMark Fasheh } 9356da61809SMark Fasheh 9366da61809SMark Fasheh /** 9378d020765SAl Viro * iter_file_splice_write - splice data from a pipe to a file 9388d020765SAl Viro * @pipe: pipe info 9398d020765SAl Viro * @out: file to write to 9408d020765SAl Viro * @ppos: position in @out 9418d020765SAl Viro * @len: number of bytes to splice 9428d020765SAl Viro * @flags: splice modifier flags 9438d020765SAl Viro * 9448d020765SAl Viro * Description: 9458d020765SAl Viro * Will either move or copy pages (determined by @flags options) from 9468d020765SAl Viro * the given pipe inode to the given file. 9478d020765SAl Viro * This one is ->write_iter-based. 9488d020765SAl Viro * 9498d020765SAl Viro */ 9508d020765SAl Viro ssize_t 9518d020765SAl Viro iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 9528d020765SAl Viro loff_t *ppos, size_t len, unsigned int flags) 9538d020765SAl Viro { 9548d020765SAl Viro struct splice_desc sd = { 9558d020765SAl Viro .total_len = len, 9568d020765SAl Viro .flags = flags, 9578d020765SAl Viro .pos = *ppos, 9588d020765SAl Viro .u.file = out, 9598d020765SAl Viro }; 9608d020765SAl Viro int nbufs = pipe->buffers; 9618d020765SAl Viro struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 9628d020765SAl Viro GFP_KERNEL); 9638d020765SAl Viro ssize_t ret; 9648d020765SAl Viro 9658d020765SAl Viro if (unlikely(!array)) 9668d020765SAl Viro return -ENOMEM; 9678d020765SAl Viro 9688d020765SAl Viro pipe_lock(pipe); 9698d020765SAl Viro 9708d020765SAl Viro splice_from_pipe_begin(&sd); 9718d020765SAl Viro while (sd.total_len) { 9728d020765SAl Viro struct iov_iter from; 9738d020765SAl Viro size_t left; 9748d020765SAl Viro int n, idx; 9758d020765SAl Viro 9768d020765SAl Viro ret = splice_from_pipe_next(pipe, &sd); 9778d020765SAl Viro if (ret <= 0) 9788d020765SAl Viro break; 9798d020765SAl Viro 9808d020765SAl Viro if (unlikely(nbufs < pipe->buffers)) { 9818d020765SAl Viro kfree(array); 9828d020765SAl Viro nbufs = pipe->buffers; 9838d020765SAl Viro array = kcalloc(nbufs, sizeof(struct bio_vec), 9848d020765SAl Viro GFP_KERNEL); 9858d020765SAl Viro if (!array) { 9868d020765SAl Viro ret = -ENOMEM; 9878d020765SAl Viro break; 9888d020765SAl Viro } 9898d020765SAl Viro } 9908d020765SAl Viro 9918d020765SAl Viro /* build the vector */ 9928d020765SAl Viro left = sd.total_len; 9938d020765SAl Viro for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) { 9948d020765SAl Viro struct pipe_buffer *buf = pipe->bufs + idx; 9958d020765SAl Viro size_t this_len = buf->len; 9968d020765SAl Viro 9978d020765SAl Viro if (this_len > left) 9988d020765SAl Viro this_len = left; 9998d020765SAl Viro 10008d020765SAl Viro if (idx == pipe->buffers - 1) 10018d020765SAl Viro idx = -1; 10028d020765SAl Viro 10038d020765SAl Viro ret = buf->ops->confirm(pipe, buf); 10048d020765SAl Viro if (unlikely(ret)) { 10058d020765SAl Viro if (ret == -ENODATA) 10068d020765SAl Viro ret = 0; 10078d020765SAl Viro goto done; 10088d020765SAl Viro } 10098d020765SAl Viro 10108d020765SAl Viro array[n].bv_page = buf->page; 10118d020765SAl Viro array[n].bv_len = this_len; 10128d020765SAl Viro array[n].bv_offset = buf->offset; 10138d020765SAl Viro left -= this_len; 10148d020765SAl Viro } 10158d020765SAl Viro 101605afcb77SAl Viro iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n, 101705afcb77SAl Viro sd.total_len - left); 1018dbe4e192SChristoph Hellwig ret = vfs_iter_write(out, &from, &sd.pos); 10198d020765SAl Viro if (ret <= 0) 10208d020765SAl Viro break; 10218d020765SAl Viro 10228d020765SAl Viro sd.num_spliced += ret; 10238d020765SAl Viro sd.total_len -= ret; 1024dbe4e192SChristoph Hellwig *ppos = sd.pos; 10258d020765SAl Viro 10268d020765SAl Viro /* dismiss the fully eaten buffers, adjust the partial one */ 10278d020765SAl Viro while (ret) { 10288d020765SAl Viro struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 10298d020765SAl Viro if (ret >= buf->len) { 10308d020765SAl Viro const struct pipe_buf_operations *ops = buf->ops; 10318d020765SAl Viro ret -= buf->len; 10328d020765SAl Viro buf->len = 0; 10338d020765SAl Viro buf->ops = NULL; 10348d020765SAl Viro ops->release(pipe, buf); 10358d020765SAl Viro pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); 10368d020765SAl Viro pipe->nrbufs--; 10378d020765SAl Viro if (pipe->files) 10388d020765SAl Viro sd.need_wakeup = true; 10398d020765SAl Viro } else { 10408d020765SAl Viro buf->offset += ret; 10418d020765SAl Viro buf->len -= ret; 10428d020765SAl Viro ret = 0; 10438d020765SAl Viro } 10448d020765SAl Viro } 10458d020765SAl Viro } 10468d020765SAl Viro done: 10478d020765SAl Viro kfree(array); 10488d020765SAl Viro splice_from_pipe_end(pipe, &sd); 10498d020765SAl Viro 10508d020765SAl Viro pipe_unlock(pipe); 10518d020765SAl Viro 10528d020765SAl Viro if (sd.num_spliced) 10538d020765SAl Viro ret = sd.num_spliced; 10548d020765SAl Viro 10558d020765SAl Viro return ret; 10568d020765SAl Viro } 10578d020765SAl Viro 10588d020765SAl Viro EXPORT_SYMBOL(iter_file_splice_write); 10598d020765SAl Viro 1060b2858d7dSMiklos Szeredi static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 1061b2858d7dSMiklos Szeredi struct splice_desc *sd) 10620b0a47f5SMiklos Szeredi { 1063b2858d7dSMiklos Szeredi int ret; 1064b2858d7dSMiklos Szeredi void *data; 106506ae43f3SAl Viro loff_t tmp = sd->pos; 1066b2858d7dSMiklos Szeredi 1067fbb32750SAl Viro data = kmap(buf->page); 106806ae43f3SAl Viro ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); 1069fbb32750SAl Viro kunmap(buf->page); 1070b2858d7dSMiklos Szeredi 1071b2858d7dSMiklos Szeredi return ret; 10720b0a47f5SMiklos Szeredi } 10730b0a47f5SMiklos Szeredi 10740b0a47f5SMiklos Szeredi static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, 10750b0a47f5SMiklos Szeredi struct file *out, loff_t *ppos, 10760b0a47f5SMiklos Szeredi size_t len, unsigned int flags) 10770b0a47f5SMiklos Szeredi { 1078b2858d7dSMiklos Szeredi ssize_t ret; 10790b0a47f5SMiklos Szeredi 1080b2858d7dSMiklos Szeredi ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); 1081b2858d7dSMiklos Szeredi if (ret > 0) 1082b2858d7dSMiklos Szeredi *ppos += ret; 10830b0a47f5SMiklos Szeredi 1084b2858d7dSMiklos Szeredi return ret; 10850b0a47f5SMiklos Szeredi } 10860b0a47f5SMiklos Szeredi 108783f9135bSJens Axboe /** 108883f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 1089932cc6d4SJens Axboe * @pipe: pipe to splice from 109083f9135bSJens Axboe * @out: socket to write to 1091932cc6d4SJens Axboe * @ppos: position in @out 109283f9135bSJens Axboe * @len: number of bytes to splice 109383f9135bSJens Axboe * @flags: splice modifier flags 109483f9135bSJens Axboe * 1095932cc6d4SJens Axboe * Description: 109683f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 109783f9135bSJens Axboe * is involved. 109883f9135bSJens Axboe * 109983f9135bSJens Axboe */ 11003a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 1101cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 11025274f052SJens Axboe { 110300522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 11045274f052SJens Axboe } 11055274f052SJens Axboe 1106059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 1107a0f06780SJeff Garzik 110883f9135bSJens Axboe /* 110983f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 111083f9135bSJens Axboe */ 11113a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 1112cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 11135274f052SJens Axboe { 11140b0a47f5SMiklos Szeredi ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 11150b0a47f5SMiklos Szeredi loff_t *, size_t, unsigned int); 11165274f052SJens Axboe 111772c2d531SAl Viro if (out->f_op->splice_write) 11180b0a47f5SMiklos Szeredi splice_write = out->f_op->splice_write; 1119cc56f7deSChangli Gao else 11200b0a47f5SMiklos Szeredi splice_write = default_file_splice_write; 11210b0a47f5SMiklos Szeredi 1122500368f7SAl Viro return splice_write(pipe, out, ppos, len, flags); 11235274f052SJens Axboe } 11245274f052SJens Axboe 112583f9135bSJens Axboe /* 112683f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 112783f9135bSJens Axboe */ 1128cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 1129cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 1130cbb7e577SJens Axboe unsigned int flags) 11315274f052SJens Axboe { 11326818173bSMiklos Szeredi ssize_t (*splice_read)(struct file *, loff_t *, 11336818173bSMiklos Szeredi struct pipe_inode_info *, size_t, unsigned int); 11345274f052SJens Axboe int ret; 11355274f052SJens Axboe 113649570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 11375274f052SJens Axboe return -EBADF; 11385274f052SJens Axboe 1139cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 11405274f052SJens Axboe if (unlikely(ret < 0)) 11415274f052SJens Axboe return ret; 11425274f052SJens Axboe 114303cc0789SAl Viro if (unlikely(len > MAX_RW_COUNT)) 114403cc0789SAl Viro len = MAX_RW_COUNT; 114503cc0789SAl Viro 114672c2d531SAl Viro if (in->f_op->splice_read) 11476818173bSMiklos Szeredi splice_read = in->f_op->splice_read; 1148cc56f7deSChangli Gao else 11496818173bSMiklos Szeredi splice_read = default_file_splice_read; 11506818173bSMiklos Szeredi 11516818173bSMiklos Szeredi return splice_read(in, ppos, pipe, len, flags); 11525274f052SJens Axboe } 11535274f052SJens Axboe 1154932cc6d4SJens Axboe /** 1155932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 1156932cc6d4SJens Axboe * @in: file to splice from 1157932cc6d4SJens Axboe * @sd: actor information on where to splice to 1158932cc6d4SJens Axboe * @actor: handles the data splicing 1159932cc6d4SJens Axboe * 1160932cc6d4SJens Axboe * Description: 1161932cc6d4SJens Axboe * This is a special case helper to splice directly between two 1162932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 1163932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 1164932cc6d4SJens Axboe * that process. 1165932cc6d4SJens Axboe * 1166c66ab6faSJens Axboe */ 1167c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 1168c66ab6faSJens Axboe splice_direct_actor *actor) 1169b92ce558SJens Axboe { 1170b92ce558SJens Axboe struct pipe_inode_info *pipe; 1171b92ce558SJens Axboe long ret, bytes; 1172b92ce558SJens Axboe umode_t i_mode; 1173c66ab6faSJens Axboe size_t len; 11740ff28d9fSChristophe Leroy int i, flags, more; 1175b92ce558SJens Axboe 1176b92ce558SJens Axboe /* 1177b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 1178b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 1179b92ce558SJens Axboe * piped splicing for that! 1180b92ce558SJens Axboe */ 1181496ad9aaSAl Viro i_mode = file_inode(in)->i_mode; 1182b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 1183b92ce558SJens Axboe return -EINVAL; 1184b92ce558SJens Axboe 1185b92ce558SJens Axboe /* 1186b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 1187b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 1188b92ce558SJens Axboe */ 1189b92ce558SJens Axboe pipe = current->splice_pipe; 119049570e9bSJens Axboe if (unlikely(!pipe)) { 11917bee130eSAl Viro pipe = alloc_pipe_info(); 1192b92ce558SJens Axboe if (!pipe) 1193b92ce558SJens Axboe return -ENOMEM; 1194b92ce558SJens Axboe 1195b92ce558SJens Axboe /* 1196b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 119700522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 1198b92ce558SJens Axboe * PIPE_READERS appropriately. 1199b92ce558SJens Axboe */ 1200b92ce558SJens Axboe pipe->readers = 1; 1201b92ce558SJens Axboe 1202b92ce558SJens Axboe current->splice_pipe = pipe; 1203b92ce558SJens Axboe } 1204b92ce558SJens Axboe 1205b92ce558SJens Axboe /* 120673d62d83SIngo Molnar * Do the splice. 1207b92ce558SJens Axboe */ 1208b92ce558SJens Axboe ret = 0; 1209b92ce558SJens Axboe bytes = 0; 1210c66ab6faSJens Axboe len = sd->total_len; 1211c66ab6faSJens Axboe flags = sd->flags; 1212c66ab6faSJens Axboe 1213c66ab6faSJens Axboe /* 1214c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 1215c66ab6faSJens Axboe */ 1216c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 12170ff28d9fSChristophe Leroy more = sd->flags & SPLICE_F_MORE; 1218b92ce558SJens Axboe 1219b92ce558SJens Axboe while (len) { 122051a92c0fSJens Axboe size_t read_len; 1221a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 1222b92ce558SJens Axboe 1223bcd4f3acSJens Axboe ret = do_splice_to(in, &pos, pipe, len, flags); 122451a92c0fSJens Axboe if (unlikely(ret <= 0)) 1225b92ce558SJens Axboe goto out_release; 1226b92ce558SJens Axboe 1227b92ce558SJens Axboe read_len = ret; 1228c66ab6faSJens Axboe sd->total_len = read_len; 1229b92ce558SJens Axboe 1230b92ce558SJens Axboe /* 12310ff28d9fSChristophe Leroy * If more data is pending, set SPLICE_F_MORE 12320ff28d9fSChristophe Leroy * If this is the last data and SPLICE_F_MORE was not set 12330ff28d9fSChristophe Leroy * initially, clears it. 12340ff28d9fSChristophe Leroy */ 12350ff28d9fSChristophe Leroy if (read_len < len) 12360ff28d9fSChristophe Leroy sd->flags |= SPLICE_F_MORE; 12370ff28d9fSChristophe Leroy else if (!more) 12380ff28d9fSChristophe Leroy sd->flags &= ~SPLICE_F_MORE; 12390ff28d9fSChristophe Leroy /* 1240b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 1241b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 1242b92ce558SJens Axboe * could get stuck data in the internal pipe: 1243b92ce558SJens Axboe */ 1244c66ab6faSJens Axboe ret = actor(pipe, sd); 1245a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 1246a82c53a0STom Zanussi sd->pos = prev_pos; 1247b92ce558SJens Axboe goto out_release; 1248a82c53a0STom Zanussi } 1249b92ce558SJens Axboe 1250b92ce558SJens Axboe bytes += ret; 1251b92ce558SJens Axboe len -= ret; 1252bcd4f3acSJens Axboe sd->pos = pos; 1253b92ce558SJens Axboe 1254a82c53a0STom Zanussi if (ret < read_len) { 1255a82c53a0STom Zanussi sd->pos = prev_pos + ret; 125651a92c0fSJens Axboe goto out_release; 1257b92ce558SJens Axboe } 1258a82c53a0STom Zanussi } 1259b92ce558SJens Axboe 12609e97198dSJens Axboe done: 1261b92ce558SJens Axboe pipe->nrbufs = pipe->curbuf = 0; 12629e97198dSJens Axboe file_accessed(in); 1263b92ce558SJens Axboe return bytes; 1264b92ce558SJens Axboe 1265b92ce558SJens Axboe out_release: 1266b92ce558SJens Axboe /* 1267b92ce558SJens Axboe * If we did an incomplete transfer we must release 1268b92ce558SJens Axboe * the pipe buffers in question: 1269b92ce558SJens Axboe */ 127035f3d14dSJens Axboe for (i = 0; i < pipe->buffers; i++) { 1271b92ce558SJens Axboe struct pipe_buffer *buf = pipe->bufs + i; 1272b92ce558SJens Axboe 1273b92ce558SJens Axboe if (buf->ops) { 1274b92ce558SJens Axboe buf->ops->release(pipe, buf); 1275b92ce558SJens Axboe buf->ops = NULL; 1276b92ce558SJens Axboe } 1277b92ce558SJens Axboe } 1278b92ce558SJens Axboe 12799e97198dSJens Axboe if (!bytes) 12809e97198dSJens Axboe bytes = ret; 1281b92ce558SJens Axboe 12829e97198dSJens Axboe goto done; 1283c66ab6faSJens Axboe } 1284c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1285c66ab6faSJens Axboe 1286c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1287c66ab6faSJens Axboe struct splice_desc *sd) 1288c66ab6faSJens Axboe { 12896a14b90bSJens Axboe struct file *file = sd->u.file; 1290c66ab6faSJens Axboe 12917995bd28SAl Viro return do_splice_from(pipe, file, sd->opos, sd->total_len, 12922cb4b05eSChangli Gao sd->flags); 1293c66ab6faSJens Axboe } 1294c66ab6faSJens Axboe 1295932cc6d4SJens Axboe /** 1296932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1297932cc6d4SJens Axboe * @in: file to splice from 1298932cc6d4SJens Axboe * @ppos: input file offset 1299932cc6d4SJens Axboe * @out: file to splice to 1300acdb37c3SRandy Dunlap * @opos: output file offset 1301932cc6d4SJens Axboe * @len: number of bytes to splice 1302932cc6d4SJens Axboe * @flags: splice modifier flags 1303932cc6d4SJens Axboe * 1304932cc6d4SJens Axboe * Description: 1305932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1306932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1307932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1308932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1309932cc6d4SJens Axboe * 1310932cc6d4SJens Axboe */ 1311c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 13127995bd28SAl Viro loff_t *opos, size_t len, unsigned int flags) 1313c66ab6faSJens Axboe { 1314c66ab6faSJens Axboe struct splice_desc sd = { 1315c66ab6faSJens Axboe .len = len, 1316c66ab6faSJens Axboe .total_len = len, 1317c66ab6faSJens Axboe .flags = flags, 1318c66ab6faSJens Axboe .pos = *ppos, 13196a14b90bSJens Axboe .u.file = out, 13207995bd28SAl Viro .opos = opos, 1321c66ab6faSJens Axboe }; 132251a92c0fSJens Axboe long ret; 1323c66ab6faSJens Axboe 132418c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 132518c67cb9SAl Viro return -EBADF; 132618c67cb9SAl Viro 132718c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 132818c67cb9SAl Viro return -EINVAL; 132918c67cb9SAl Viro 133018c67cb9SAl Viro ret = rw_verify_area(WRITE, out, opos, len); 133118c67cb9SAl Viro if (unlikely(ret < 0)) 133218c67cb9SAl Viro return ret; 133318c67cb9SAl Viro 1334c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 133551a92c0fSJens Axboe if (ret > 0) 1336a82c53a0STom Zanussi *ppos = sd.pos; 133751a92c0fSJens Axboe 1338c66ab6faSJens Axboe return ret; 1339b92ce558SJens Axboe } 13401c118596SMiklos Szeredi EXPORT_SYMBOL(do_splice_direct); 1341b92ce558SJens Axboe 13427c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 13437c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 13447c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 1345ddac0d39SJens Axboe 1346ddac0d39SJens Axboe /* 134783f9135bSJens Axboe * Determine where to splice to/from. 134883f9135bSJens Axboe */ 1349529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in, 1350529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 1351529565dcSIngo Molnar size_t len, unsigned int flags) 13525274f052SJens Axboe { 13537c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 13547c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 13557995bd28SAl Viro loff_t offset; 1356a4514ebdSJens Axboe long ret; 13575274f052SJens Axboe 135871993e62SLinus Torvalds ipipe = get_pipe_info(in); 135971993e62SLinus Torvalds opipe = get_pipe_info(out); 13607c77f0b3SMiklos Szeredi 13617c77f0b3SMiklos Szeredi if (ipipe && opipe) { 13627c77f0b3SMiklos Szeredi if (off_in || off_out) 13637c77f0b3SMiklos Szeredi return -ESPIPE; 13647c77f0b3SMiklos Szeredi 13657c77f0b3SMiklos Szeredi if (!(in->f_mode & FMODE_READ)) 13667c77f0b3SMiklos Szeredi return -EBADF; 13677c77f0b3SMiklos Szeredi 13687c77f0b3SMiklos Szeredi if (!(out->f_mode & FMODE_WRITE)) 13697c77f0b3SMiklos Szeredi return -EBADF; 13707c77f0b3SMiklos Szeredi 13717c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 13727c77f0b3SMiklos Szeredi if (ipipe == opipe) 13737c77f0b3SMiklos Szeredi return -EINVAL; 13747c77f0b3SMiklos Szeredi 13757c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 13767c77f0b3SMiklos Szeredi } 13777c77f0b3SMiklos Szeredi 13787c77f0b3SMiklos Szeredi if (ipipe) { 1379529565dcSIngo Molnar if (off_in) 1380529565dcSIngo Molnar return -ESPIPE; 1381b92ce558SJens Axboe if (off_out) { 138219c9a49bSChangli Gao if (!(out->f_mode & FMODE_PWRITE)) 1383b92ce558SJens Axboe return -EINVAL; 1384cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1385b92ce558SJens Axboe return -EFAULT; 13867995bd28SAl Viro } else { 13877995bd28SAl Viro offset = out->f_pos; 13887995bd28SAl Viro } 1389529565dcSIngo Molnar 139018c67cb9SAl Viro if (unlikely(!(out->f_mode & FMODE_WRITE))) 139118c67cb9SAl Viro return -EBADF; 139218c67cb9SAl Viro 139318c67cb9SAl Viro if (unlikely(out->f_flags & O_APPEND)) 139418c67cb9SAl Viro return -EINVAL; 139518c67cb9SAl Viro 139618c67cb9SAl Viro ret = rw_verify_area(WRITE, out, &offset, len); 139718c67cb9SAl Viro if (unlikely(ret < 0)) 139818c67cb9SAl Viro return ret; 139918c67cb9SAl Viro 1400500368f7SAl Viro file_start_write(out); 14017995bd28SAl Viro ret = do_splice_from(ipipe, out, &offset, len, flags); 1402500368f7SAl Viro file_end_write(out); 1403a4514ebdSJens Axboe 14047995bd28SAl Viro if (!off_out) 14057995bd28SAl Viro out->f_pos = offset; 14067995bd28SAl Viro else if (copy_to_user(off_out, &offset, sizeof(loff_t))) 1407a4514ebdSJens Axboe ret = -EFAULT; 1408a4514ebdSJens Axboe 1409a4514ebdSJens Axboe return ret; 1410529565dcSIngo Molnar } 14115274f052SJens Axboe 14127c77f0b3SMiklos Szeredi if (opipe) { 1413529565dcSIngo Molnar if (off_out) 1414529565dcSIngo Molnar return -ESPIPE; 1415b92ce558SJens Axboe if (off_in) { 141619c9a49bSChangli Gao if (!(in->f_mode & FMODE_PREAD)) 1417b92ce558SJens Axboe return -EINVAL; 1418cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1419b92ce558SJens Axboe return -EFAULT; 14207995bd28SAl Viro } else { 14217995bd28SAl Viro offset = in->f_pos; 14227995bd28SAl Viro } 1423529565dcSIngo Molnar 14247995bd28SAl Viro ret = do_splice_to(in, &offset, opipe, len, flags); 1425a4514ebdSJens Axboe 14267995bd28SAl Viro if (!off_in) 14277995bd28SAl Viro in->f_pos = offset; 14287995bd28SAl Viro else if (copy_to_user(off_in, &offset, sizeof(loff_t))) 1429a4514ebdSJens Axboe ret = -EFAULT; 1430a4514ebdSJens Axboe 1431a4514ebdSJens Axboe return ret; 1432529565dcSIngo Molnar } 14335274f052SJens Axboe 14345274f052SJens Axboe return -EINVAL; 14355274f052SJens Axboe } 14365274f052SJens Axboe 1437db85a9ebSAl Viro static int get_iovec_page_array(struct iov_iter *from, 1438db85a9ebSAl Viro struct page **pages, 1439db85a9ebSAl Viro struct partial_page *partial, 144035f3d14dSJens Axboe unsigned int pipe_buffers) 1441912d35f8SJens Axboe { 1442db85a9ebSAl Viro int buffers = 0; 1443db85a9ebSAl Viro while (iov_iter_count(from)) { 1444db85a9ebSAl Viro ssize_t copied; 1445db85a9ebSAl Viro size_t start; 1446912d35f8SJens Axboe 1447db85a9ebSAl Viro copied = iov_iter_get_pages(from, pages + buffers, ~0UL, 1448db85a9ebSAl Viro pipe_buffers - buffers, &start); 1449db85a9ebSAl Viro if (copied <= 0) 1450db85a9ebSAl Viro return buffers ? buffers : copied; 1451912d35f8SJens Axboe 1452db85a9ebSAl Viro iov_iter_advance(from, copied); 1453db85a9ebSAl Viro while (copied) { 1454db85a9ebSAl Viro int size = min_t(int, copied, PAGE_SIZE - start); 1455db85a9ebSAl Viro partial[buffers].offset = start; 1456db85a9ebSAl Viro partial[buffers].len = size; 1457db85a9ebSAl Viro copied -= size; 1458db85a9ebSAl Viro start = 0; 1459912d35f8SJens Axboe buffers++; 1460912d35f8SJens Axboe } 1461912d35f8SJens Axboe } 1462912d35f8SJens Axboe return buffers; 1463912d35f8SJens Axboe } 1464912d35f8SJens Axboe 14656a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 14666a14b90bSJens Axboe struct splice_desc *sd) 14676a14b90bSJens Axboe { 14686130f531SAl Viro int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 14696130f531SAl Viro return n == sd->len ? n : -EFAULT; 14706a14b90bSJens Axboe } 14716a14b90bSJens Axboe 14726a14b90bSJens Axboe /* 14736a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 14746a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 14756a14b90bSJens Axboe */ 14766130f531SAl Viro static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov, 14776a14b90bSJens Axboe unsigned long nr_segs, unsigned int flags) 14786a14b90bSJens Axboe { 14796a14b90bSJens Axboe struct pipe_inode_info *pipe; 14806a14b90bSJens Axboe struct splice_desc sd; 14816a14b90bSJens Axboe long ret; 14826130f531SAl Viro struct iovec iovstack[UIO_FASTIOV]; 14836130f531SAl Viro struct iovec *iov = iovstack; 14846130f531SAl Viro struct iov_iter iter; 14856a14b90bSJens Axboe 148671993e62SLinus Torvalds pipe = get_pipe_info(file); 14876a14b90bSJens Axboe if (!pipe) 14886a14b90bSJens Axboe return -EBADF; 14896a14b90bSJens Axboe 1490345995faSAl Viro ret = import_iovec(READ, uiov, nr_segs, 1491345995faSAl Viro ARRAY_SIZE(iovstack), &iov, &iter); 1492345995faSAl Viro if (ret < 0) 1493345995faSAl Viro return ret; 14946a14b90bSJens Axboe 1495345995faSAl Viro sd.total_len = iov_iter_count(&iter); 14966a14b90bSJens Axboe sd.len = 0; 14976a14b90bSJens Axboe sd.flags = flags; 14986130f531SAl Viro sd.u.data = &iter; 14996a14b90bSJens Axboe sd.pos = 0; 15006a14b90bSJens Axboe 1501345995faSAl Viro if (sd.total_len) { 15026130f531SAl Viro pipe_lock(pipe); 15036130f531SAl Viro ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 150461e0d47cSMiklos Szeredi pipe_unlock(pipe); 1505345995faSAl Viro } 15066a14b90bSJens Axboe 15076130f531SAl Viro kfree(iov); 15086a14b90bSJens Axboe return ret; 15096a14b90bSJens Axboe } 15106a14b90bSJens Axboe 1511912d35f8SJens Axboe /* 1512912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1513912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1514912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1515912d35f8SJens Axboe */ 1516db85a9ebSAl Viro static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov, 1517912d35f8SJens Axboe unsigned long nr_segs, unsigned int flags) 1518912d35f8SJens Axboe { 1519ddac0d39SJens Axboe struct pipe_inode_info *pipe; 1520db85a9ebSAl Viro struct iovec iovstack[UIO_FASTIOV]; 1521db85a9ebSAl Viro struct iovec *iov = iovstack; 1522db85a9ebSAl Viro struct iov_iter from; 152335f3d14dSJens Axboe struct page *pages[PIPE_DEF_BUFFERS]; 152435f3d14dSJens Axboe struct partial_page partial[PIPE_DEF_BUFFERS]; 1525912d35f8SJens Axboe struct splice_pipe_desc spd = { 1526912d35f8SJens Axboe .pages = pages, 1527912d35f8SJens Axboe .partial = partial, 1528047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1529912d35f8SJens Axboe .flags = flags, 1530912d35f8SJens Axboe .ops = &user_page_pipe_buf_ops, 1531bbdfc2f7SJens Axboe .spd_release = spd_release_page, 1532912d35f8SJens Axboe }; 153335f3d14dSJens Axboe long ret; 1534912d35f8SJens Axboe 153571993e62SLinus Torvalds pipe = get_pipe_info(file); 1536ddac0d39SJens Axboe if (!pipe) 1537912d35f8SJens Axboe return -EBADF; 1538912d35f8SJens Axboe 1539db85a9ebSAl Viro ret = import_iovec(WRITE, uiov, nr_segs, 1540db85a9ebSAl Viro ARRAY_SIZE(iovstack), &iov, &from); 1541db85a9ebSAl Viro if (ret < 0) 1542db85a9ebSAl Viro return ret; 1543912d35f8SJens Axboe 1544db85a9ebSAl Viro if (splice_grow_spd(pipe, &spd)) { 1545db85a9ebSAl Viro kfree(iov); 1546db85a9ebSAl Viro return -ENOMEM; 1547db85a9ebSAl Viro } 1548db85a9ebSAl Viro 1549db85a9ebSAl Viro spd.nr_pages = get_iovec_page_array(&from, spd.pages, 1550db85a9ebSAl Viro spd.partial, 1551047fe360SEric Dumazet spd.nr_pages_max); 155235f3d14dSJens Axboe if (spd.nr_pages <= 0) 155335f3d14dSJens Axboe ret = spd.nr_pages; 155435f3d14dSJens Axboe else 155535f3d14dSJens Axboe ret = splice_to_pipe(pipe, &spd); 155635f3d14dSJens Axboe 1557047fe360SEric Dumazet splice_shrink_spd(&spd); 1558db85a9ebSAl Viro kfree(iov); 155935f3d14dSJens Axboe return ret; 1560912d35f8SJens Axboe } 1561912d35f8SJens Axboe 15626a14b90bSJens Axboe /* 15636a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 15646a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 15656a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 15666a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 15676a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 15686a14b90bSJens Axboe * solutions for that: 15696a14b90bSJens Axboe * 15706a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 15716a14b90bSJens Axboe * do a regular read() on the buffer anyway. 15726a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 15736a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 15746a14b90bSJens Axboe * 15756a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 15766a14b90bSJens Axboe * 15776a14b90bSJens Axboe */ 1578836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, 1579836f92adSHeiko Carstens unsigned long, nr_segs, unsigned int, flags) 1580912d35f8SJens Axboe { 15812903ff01SAl Viro struct fd f; 1582912d35f8SJens Axboe long error; 1583912d35f8SJens Axboe 15846a14b90bSJens Axboe if (unlikely(nr_segs > UIO_MAXIOV)) 15856a14b90bSJens Axboe return -EINVAL; 15866a14b90bSJens Axboe else if (unlikely(!nr_segs)) 15876a14b90bSJens Axboe return 0; 15886a14b90bSJens Axboe 1589912d35f8SJens Axboe error = -EBADF; 15902903ff01SAl Viro f = fdget(fd); 15912903ff01SAl Viro if (f.file) { 15922903ff01SAl Viro if (f.file->f_mode & FMODE_WRITE) 15932903ff01SAl Viro error = vmsplice_to_pipe(f.file, iov, nr_segs, flags); 15942903ff01SAl Viro else if (f.file->f_mode & FMODE_READ) 15952903ff01SAl Viro error = vmsplice_to_user(f.file, iov, nr_segs, flags); 1596912d35f8SJens Axboe 15972903ff01SAl Viro fdput(f); 1598912d35f8SJens Axboe } 1599912d35f8SJens Axboe 1600912d35f8SJens Axboe return error; 1601912d35f8SJens Axboe } 1602912d35f8SJens Axboe 160376b021d0SAl Viro #ifdef CONFIG_COMPAT 160476b021d0SAl Viro COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32, 160576b021d0SAl Viro unsigned int, nr_segs, unsigned int, flags) 160676b021d0SAl Viro { 160776b021d0SAl Viro unsigned i; 160876b021d0SAl Viro struct iovec __user *iov; 160976b021d0SAl Viro if (nr_segs > UIO_MAXIOV) 161076b021d0SAl Viro return -EINVAL; 161176b021d0SAl Viro iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec)); 161276b021d0SAl Viro for (i = 0; i < nr_segs; i++) { 161376b021d0SAl Viro struct compat_iovec v; 161476b021d0SAl Viro if (get_user(v.iov_base, &iov32[i].iov_base) || 161576b021d0SAl Viro get_user(v.iov_len, &iov32[i].iov_len) || 161676b021d0SAl Viro put_user(compat_ptr(v.iov_base), &iov[i].iov_base) || 161776b021d0SAl Viro put_user(v.iov_len, &iov[i].iov_len)) 161876b021d0SAl Viro return -EFAULT; 161976b021d0SAl Viro } 162076b021d0SAl Viro return sys_vmsplice(fd, iov, nr_segs, flags); 162176b021d0SAl Viro } 162276b021d0SAl Viro #endif 162376b021d0SAl Viro 1624836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1625836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1626836f92adSHeiko Carstens size_t, len, unsigned int, flags) 16275274f052SJens Axboe { 16282903ff01SAl Viro struct fd in, out; 16295274f052SJens Axboe long error; 16305274f052SJens Axboe 16315274f052SJens Axboe if (unlikely(!len)) 16325274f052SJens Axboe return 0; 16335274f052SJens Axboe 16345274f052SJens Axboe error = -EBADF; 16352903ff01SAl Viro in = fdget(fd_in); 16362903ff01SAl Viro if (in.file) { 16372903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 16382903ff01SAl Viro out = fdget(fd_out); 16392903ff01SAl Viro if (out.file) { 16402903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 16412903ff01SAl Viro error = do_splice(in.file, off_in, 16422903ff01SAl Viro out.file, off_out, 1643529565dcSIngo Molnar len, flags); 16442903ff01SAl Viro fdput(out); 16455274f052SJens Axboe } 16465274f052SJens Axboe } 16472903ff01SAl Viro fdput(in); 16485274f052SJens Axboe } 16495274f052SJens Axboe return error; 16505274f052SJens Axboe } 165170524490SJens Axboe 165270524490SJens Axboe /* 1653aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1654aadd06e5SJens Axboe * return an appropriate error. 1655aadd06e5SJens Axboe */ 16567c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1657aadd06e5SJens Axboe { 1658aadd06e5SJens Axboe int ret; 1659aadd06e5SJens Axboe 1660aadd06e5SJens Axboe /* 1661aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1662aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1663aadd06e5SJens Axboe */ 1664aadd06e5SJens Axboe if (pipe->nrbufs) 1665aadd06e5SJens Axboe return 0; 1666aadd06e5SJens Axboe 1667aadd06e5SJens Axboe ret = 0; 166861e0d47cSMiklos Szeredi pipe_lock(pipe); 1669aadd06e5SJens Axboe 1670aadd06e5SJens Axboe while (!pipe->nrbufs) { 1671aadd06e5SJens Axboe if (signal_pending(current)) { 1672aadd06e5SJens Axboe ret = -ERESTARTSYS; 1673aadd06e5SJens Axboe break; 1674aadd06e5SJens Axboe } 1675aadd06e5SJens Axboe if (!pipe->writers) 1676aadd06e5SJens Axboe break; 1677aadd06e5SJens Axboe if (!pipe->waiting_writers) { 1678aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1679aadd06e5SJens Axboe ret = -EAGAIN; 1680aadd06e5SJens Axboe break; 1681aadd06e5SJens Axboe } 1682aadd06e5SJens Axboe } 1683aadd06e5SJens Axboe pipe_wait(pipe); 1684aadd06e5SJens Axboe } 1685aadd06e5SJens Axboe 168661e0d47cSMiklos Szeredi pipe_unlock(pipe); 1687aadd06e5SJens Axboe return ret; 1688aadd06e5SJens Axboe } 1689aadd06e5SJens Axboe 1690aadd06e5SJens Axboe /* 1691aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1692aadd06e5SJens Axboe * return an appropriate error. 1693aadd06e5SJens Axboe */ 16947c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1695aadd06e5SJens Axboe { 1696aadd06e5SJens Axboe int ret; 1697aadd06e5SJens Axboe 1698aadd06e5SJens Axboe /* 1699aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1700aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1701aadd06e5SJens Axboe */ 170235f3d14dSJens Axboe if (pipe->nrbufs < pipe->buffers) 1703aadd06e5SJens Axboe return 0; 1704aadd06e5SJens Axboe 1705aadd06e5SJens Axboe ret = 0; 170661e0d47cSMiklos Szeredi pipe_lock(pipe); 1707aadd06e5SJens Axboe 170835f3d14dSJens Axboe while (pipe->nrbufs >= pipe->buffers) { 1709aadd06e5SJens Axboe if (!pipe->readers) { 1710aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1711aadd06e5SJens Axboe ret = -EPIPE; 1712aadd06e5SJens Axboe break; 1713aadd06e5SJens Axboe } 1714aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1715aadd06e5SJens Axboe ret = -EAGAIN; 1716aadd06e5SJens Axboe break; 1717aadd06e5SJens Axboe } 1718aadd06e5SJens Axboe if (signal_pending(current)) { 1719aadd06e5SJens Axboe ret = -ERESTARTSYS; 1720aadd06e5SJens Axboe break; 1721aadd06e5SJens Axboe } 1722aadd06e5SJens Axboe pipe->waiting_writers++; 1723aadd06e5SJens Axboe pipe_wait(pipe); 1724aadd06e5SJens Axboe pipe->waiting_writers--; 1725aadd06e5SJens Axboe } 1726aadd06e5SJens Axboe 172761e0d47cSMiklos Szeredi pipe_unlock(pipe); 1728aadd06e5SJens Axboe return ret; 1729aadd06e5SJens Axboe } 1730aadd06e5SJens Axboe 1731aadd06e5SJens Axboe /* 17327c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 17337c77f0b3SMiklos Szeredi */ 17347c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 17357c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 17367c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 17377c77f0b3SMiklos Szeredi { 17387c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 17397c77f0b3SMiklos Szeredi int ret = 0, nbuf; 17407c77f0b3SMiklos Szeredi bool input_wakeup = false; 17417c77f0b3SMiklos Szeredi 17427c77f0b3SMiklos Szeredi 17437c77f0b3SMiklos Szeredi retry: 17447c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 17457c77f0b3SMiklos Szeredi if (ret) 17467c77f0b3SMiklos Szeredi return ret; 17477c77f0b3SMiklos Szeredi 17487c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 17497c77f0b3SMiklos Szeredi if (ret) 17507c77f0b3SMiklos Szeredi return ret; 17517c77f0b3SMiklos Szeredi 17527c77f0b3SMiklos Szeredi /* 17537c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 17547c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 17557c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 17567c77f0b3SMiklos Szeredi */ 17577c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 17587c77f0b3SMiklos Szeredi 17597c77f0b3SMiklos Szeredi do { 17607c77f0b3SMiklos Szeredi if (!opipe->readers) { 17617c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 17627c77f0b3SMiklos Szeredi if (!ret) 17637c77f0b3SMiklos Szeredi ret = -EPIPE; 17647c77f0b3SMiklos Szeredi break; 17657c77f0b3SMiklos Szeredi } 17667c77f0b3SMiklos Szeredi 17677c77f0b3SMiklos Szeredi if (!ipipe->nrbufs && !ipipe->writers) 17687c77f0b3SMiklos Szeredi break; 17697c77f0b3SMiklos Szeredi 17707c77f0b3SMiklos Szeredi /* 17717c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 17727c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 17737c77f0b3SMiklos Szeredi */ 177435f3d14dSJens Axboe if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) { 17757c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 17767c77f0b3SMiklos Szeredi if (ret) 17777c77f0b3SMiklos Szeredi break; 17787c77f0b3SMiklos Szeredi 17797c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 17807c77f0b3SMiklos Szeredi ret = -EAGAIN; 17817c77f0b3SMiklos Szeredi break; 17827c77f0b3SMiklos Szeredi } 17837c77f0b3SMiklos Szeredi 17847c77f0b3SMiklos Szeredi /* 17857c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 17867c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 17877c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 17887c77f0b3SMiklos Szeredi */ 17897c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 17907c77f0b3SMiklos Szeredi pipe_unlock(opipe); 17917c77f0b3SMiklos Szeredi goto retry; 17927c77f0b3SMiklos Szeredi } 17937c77f0b3SMiklos Szeredi 17947c77f0b3SMiklos Szeredi ibuf = ipipe->bufs + ipipe->curbuf; 179535f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 17967c77f0b3SMiklos Szeredi obuf = opipe->bufs + nbuf; 17977c77f0b3SMiklos Szeredi 17987c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 17997c77f0b3SMiklos Szeredi /* 18007c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 18017c77f0b3SMiklos Szeredi */ 18027c77f0b3SMiklos Szeredi *obuf = *ibuf; 18037c77f0b3SMiklos Szeredi ibuf->ops = NULL; 18047c77f0b3SMiklos Szeredi opipe->nrbufs++; 180535f3d14dSJens Axboe ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1); 18067c77f0b3SMiklos Szeredi ipipe->nrbufs--; 18077c77f0b3SMiklos Szeredi input_wakeup = true; 18087c77f0b3SMiklos Szeredi } else { 18097c77f0b3SMiklos Szeredi /* 18107c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 18117c77f0b3SMiklos Szeredi * so we can copy the contents over. 18127c77f0b3SMiklos Szeredi */ 18137c77f0b3SMiklos Szeredi ibuf->ops->get(ipipe, ibuf); 18147c77f0b3SMiklos Szeredi *obuf = *ibuf; 18157c77f0b3SMiklos Szeredi 18167c77f0b3SMiklos Szeredi /* 18177c77f0b3SMiklos Szeredi * Don't inherit the gift flag, we need to 18187c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 18197c77f0b3SMiklos Szeredi */ 18207c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 18217c77f0b3SMiklos Szeredi 18227c77f0b3SMiklos Szeredi obuf->len = len; 18237c77f0b3SMiklos Szeredi opipe->nrbufs++; 18247c77f0b3SMiklos Szeredi ibuf->offset += obuf->len; 18257c77f0b3SMiklos Szeredi ibuf->len -= obuf->len; 18267c77f0b3SMiklos Szeredi } 18277c77f0b3SMiklos Szeredi ret += obuf->len; 18287c77f0b3SMiklos Szeredi len -= obuf->len; 18297c77f0b3SMiklos Szeredi } while (len); 18307c77f0b3SMiklos Szeredi 18317c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 18327c77f0b3SMiklos Szeredi pipe_unlock(opipe); 18337c77f0b3SMiklos Szeredi 18347c77f0b3SMiklos Szeredi /* 18357c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 18367c77f0b3SMiklos Szeredi */ 1837825cdcb1SNamhyung Kim if (ret > 0) 1838825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 1839825cdcb1SNamhyung Kim 18407c77f0b3SMiklos Szeredi if (input_wakeup) 18417c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 18427c77f0b3SMiklos Szeredi 18437c77f0b3SMiklos Szeredi return ret; 18447c77f0b3SMiklos Szeredi } 18457c77f0b3SMiklos Szeredi 18467c77f0b3SMiklos Szeredi /* 184770524490SJens Axboe * Link contents of ipipe to opipe. 184870524490SJens Axboe */ 184970524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 185070524490SJens Axboe struct pipe_inode_info *opipe, 185170524490SJens Axboe size_t len, unsigned int flags) 185270524490SJens Axboe { 185370524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 1854aadd06e5SJens Axboe int ret = 0, i = 0, nbuf; 185570524490SJens Axboe 185670524490SJens Axboe /* 185770524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 185861e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 185970524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 186070524490SJens Axboe */ 186161e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 186270524490SJens Axboe 1863aadd06e5SJens Axboe do { 186470524490SJens Axboe if (!opipe->readers) { 186570524490SJens Axboe send_sig(SIGPIPE, current, 0); 186670524490SJens Axboe if (!ret) 186770524490SJens Axboe ret = -EPIPE; 186870524490SJens Axboe break; 186970524490SJens Axboe } 187070524490SJens Axboe 187170524490SJens Axboe /* 1872aadd06e5SJens Axboe * If we have iterated all input buffers or ran out of 1873aadd06e5SJens Axboe * output room, break. 187470524490SJens Axboe */ 187535f3d14dSJens Axboe if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) 1876aadd06e5SJens Axboe break; 1877aadd06e5SJens Axboe 187835f3d14dSJens Axboe ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1)); 187935f3d14dSJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); 188070524490SJens Axboe 188170524490SJens Axboe /* 188270524490SJens Axboe * Get a reference to this pipe buffer, 188370524490SJens Axboe * so we can copy the contents over. 188470524490SJens Axboe */ 188570524490SJens Axboe ibuf->ops->get(ipipe, ibuf); 188670524490SJens Axboe 188770524490SJens Axboe obuf = opipe->bufs + nbuf; 188870524490SJens Axboe *obuf = *ibuf; 188970524490SJens Axboe 18907afa6fd0SJens Axboe /* 18917afa6fd0SJens Axboe * Don't inherit the gift flag, we need to 18927afa6fd0SJens Axboe * prevent multiple steals of this page. 18937afa6fd0SJens Axboe */ 18947afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 18957afa6fd0SJens Axboe 189670524490SJens Axboe if (obuf->len > len) 189770524490SJens Axboe obuf->len = len; 189870524490SJens Axboe 189970524490SJens Axboe opipe->nrbufs++; 190070524490SJens Axboe ret += obuf->len; 190170524490SJens Axboe len -= obuf->len; 1902aadd06e5SJens Axboe i++; 1903aadd06e5SJens Axboe } while (len); 190470524490SJens Axboe 190502cf01aeSJens Axboe /* 190602cf01aeSJens Axboe * return EAGAIN if we have the potential of some data in the 190702cf01aeSJens Axboe * future, otherwise just return 0 190802cf01aeSJens Axboe */ 190902cf01aeSJens Axboe if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) 191002cf01aeSJens Axboe ret = -EAGAIN; 191102cf01aeSJens Axboe 191261e0d47cSMiklos Szeredi pipe_unlock(ipipe); 191361e0d47cSMiklos Szeredi pipe_unlock(opipe); 191470524490SJens Axboe 1915aadd06e5SJens Axboe /* 1916aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1917aadd06e5SJens Axboe */ 1918825cdcb1SNamhyung Kim if (ret > 0) 1919825cdcb1SNamhyung Kim wakeup_pipe_readers(opipe); 192070524490SJens Axboe 192170524490SJens Axboe return ret; 192270524490SJens Axboe } 192370524490SJens Axboe 192470524490SJens Axboe /* 192570524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 192670524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 192770524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 192870524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 192970524490SJens Axboe */ 193070524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len, 193170524490SJens Axboe unsigned int flags) 193270524490SJens Axboe { 193371993e62SLinus Torvalds struct pipe_inode_info *ipipe = get_pipe_info(in); 193471993e62SLinus Torvalds struct pipe_inode_info *opipe = get_pipe_info(out); 1935aadd06e5SJens Axboe int ret = -EINVAL; 193670524490SJens Axboe 193770524490SJens Axboe /* 1938aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 1939aadd06e5SJens Axboe * copying the data. 194070524490SJens Axboe */ 1941aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 1942aadd06e5SJens Axboe /* 1943aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 1944aadd06e5SJens Axboe * ordering doesn't really matter. 1945aadd06e5SJens Axboe */ 19467c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 1947aadd06e5SJens Axboe if (!ret) { 19487c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 194902cf01aeSJens Axboe if (!ret) 1950aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 1951aadd06e5SJens Axboe } 1952aadd06e5SJens Axboe } 195370524490SJens Axboe 1954aadd06e5SJens Axboe return ret; 195570524490SJens Axboe } 195670524490SJens Axboe 1957836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 195870524490SJens Axboe { 19592903ff01SAl Viro struct fd in; 19602903ff01SAl Viro int error; 196170524490SJens Axboe 196270524490SJens Axboe if (unlikely(!len)) 196370524490SJens Axboe return 0; 196470524490SJens Axboe 196570524490SJens Axboe error = -EBADF; 19662903ff01SAl Viro in = fdget(fdin); 19672903ff01SAl Viro if (in.file) { 19682903ff01SAl Viro if (in.file->f_mode & FMODE_READ) { 19692903ff01SAl Viro struct fd out = fdget(fdout); 19702903ff01SAl Viro if (out.file) { 19712903ff01SAl Viro if (out.file->f_mode & FMODE_WRITE) 19722903ff01SAl Viro error = do_tee(in.file, out.file, 19732903ff01SAl Viro len, flags); 19742903ff01SAl Viro fdput(out); 197570524490SJens Axboe } 197670524490SJens Axboe } 19772903ff01SAl Viro fdput(in); 197870524490SJens Axboe } 197970524490SJens Axboe 198070524490SJens Axboe return error; 198170524490SJens Axboe } 1982