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> 284f6f0bd2SJens Axboe #include <linux/buffer_head.h> 29a0f06780SJeff Garzik #include <linux/module.h> 304f6f0bd2SJens Axboe #include <linux/syscalls.h> 31912d35f8SJens Axboe #include <linux/uio.h> 3229ce2058SJames Morris #include <linux/security.h> 335274f052SJens Axboe 3483f9135bSJens Axboe /* 3583f9135bSJens Axboe * Attempt to steal a page from a pipe buffer. This should perhaps go into 3683f9135bSJens Axboe * a vm helper function, it's already simplified quite a bit by the 3783f9135bSJens Axboe * addition of remove_mapping(). If success is returned, the caller may 3883f9135bSJens Axboe * attempt to reuse this page for another destination. 3983f9135bSJens Axboe */ 4076ad4d11SJens Axboe static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, 415abc97aaSJens Axboe struct pipe_buffer *buf) 425abc97aaSJens Axboe { 435abc97aaSJens Axboe struct page *page = buf->page; 449e94cd4fSJens Axboe struct address_space *mapping; 455abc97aaSJens Axboe 469e0267c2SJens Axboe lock_page(page); 479e0267c2SJens Axboe 489e94cd4fSJens Axboe mapping = page_mapping(page); 499e94cd4fSJens Axboe if (mapping) { 505abc97aaSJens Axboe WARN_ON(!PageUptodate(page)); 515abc97aaSJens Axboe 52ad8d6f0aSJens Axboe /* 539e94cd4fSJens Axboe * At least for ext2 with nobh option, we need to wait on 549e94cd4fSJens Axboe * writeback completing on this page, since we'll remove it 559e94cd4fSJens Axboe * from the pagecache. Otherwise truncate wont wait on the 569e94cd4fSJens Axboe * page, allowing the disk blocks to be reused by someone else 579e94cd4fSJens Axboe * before we actually wrote our data to them. fs corruption 589e94cd4fSJens Axboe * ensues. 59ad8d6f0aSJens Axboe */ 60ad8d6f0aSJens Axboe wait_on_page_writeback(page); 61ad8d6f0aSJens Axboe 62266cf658SDavid Howells if (page_has_private(page) && 63266cf658SDavid Howells !try_to_release_page(page, GFP_KERNEL)) 64ca39d651SJens Axboe goto out_unlock; 654f6f0bd2SJens Axboe 669e94cd4fSJens Axboe /* 679e94cd4fSJens Axboe * If we succeeded in removing the mapping, set LRU flag 689e94cd4fSJens Axboe * and return good. 699e94cd4fSJens Axboe */ 709e94cd4fSJens Axboe if (remove_mapping(mapping, page)) { 711432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 725abc97aaSJens Axboe return 0; 735abc97aaSJens Axboe } 749e94cd4fSJens Axboe } 759e94cd4fSJens Axboe 769e94cd4fSJens Axboe /* 779e94cd4fSJens Axboe * Raced with truncate or failed to remove page from current 789e94cd4fSJens Axboe * address space, unlock and return failure. 799e94cd4fSJens Axboe */ 80ca39d651SJens Axboe out_unlock: 819e94cd4fSJens Axboe unlock_page(page); 829e94cd4fSJens Axboe return 1; 839e94cd4fSJens Axboe } 845abc97aaSJens Axboe 8576ad4d11SJens Axboe static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 865274f052SJens Axboe struct pipe_buffer *buf) 875274f052SJens Axboe { 885274f052SJens Axboe page_cache_release(buf->page); 891432873aSJens Axboe buf->flags &= ~PIPE_BUF_FLAG_LRU; 905274f052SJens Axboe } 915274f052SJens Axboe 920845718dSJens Axboe /* 930845718dSJens Axboe * Check whether the contents of buf is OK to access. Since the content 940845718dSJens Axboe * is a page cache page, IO may be in flight. 950845718dSJens Axboe */ 96cac36bb0SJens Axboe static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 975274f052SJens Axboe struct pipe_buffer *buf) 985274f052SJens Axboe { 995274f052SJens Axboe struct page *page = buf->page; 10049d0b21bSJens Axboe int err; 1015274f052SJens Axboe 1025274f052SJens Axboe if (!PageUptodate(page)) { 10349d0b21bSJens Axboe lock_page(page); 1045274f052SJens Axboe 10549d0b21bSJens Axboe /* 10649d0b21bSJens Axboe * Page got truncated/unhashed. This will cause a 0-byte 10773d62d83SIngo Molnar * splice, if this is the first page. 10849d0b21bSJens Axboe */ 1095274f052SJens Axboe if (!page->mapping) { 11049d0b21bSJens Axboe err = -ENODATA; 11149d0b21bSJens Axboe goto error; 1125274f052SJens Axboe } 1135274f052SJens Axboe 11449d0b21bSJens Axboe /* 11573d62d83SIngo Molnar * Uh oh, read-error from disk. 11649d0b21bSJens Axboe */ 11749d0b21bSJens Axboe if (!PageUptodate(page)) { 11849d0b21bSJens Axboe err = -EIO; 11949d0b21bSJens Axboe goto error; 12049d0b21bSJens Axboe } 12149d0b21bSJens Axboe 12249d0b21bSJens Axboe /* 123f84d7519SJens Axboe * Page is ok afterall, we are done. 12449d0b21bSJens Axboe */ 12549d0b21bSJens Axboe unlock_page(page); 12649d0b21bSJens Axboe } 12749d0b21bSJens Axboe 128f84d7519SJens Axboe return 0; 12949d0b21bSJens Axboe error: 13049d0b21bSJens Axboe unlock_page(page); 131f84d7519SJens Axboe return err; 13270524490SJens Axboe } 13370524490SJens Axboe 134d4c3cca9SEric Dumazet static const struct pipe_buf_operations page_cache_pipe_buf_ops = { 1355274f052SJens Axboe .can_merge = 0, 136f84d7519SJens Axboe .map = generic_pipe_buf_map, 137f84d7519SJens Axboe .unmap = generic_pipe_buf_unmap, 138cac36bb0SJens Axboe .confirm = page_cache_pipe_buf_confirm, 1395274f052SJens Axboe .release = page_cache_pipe_buf_release, 1405abc97aaSJens Axboe .steal = page_cache_pipe_buf_steal, 141f84d7519SJens Axboe .get = generic_pipe_buf_get, 1425274f052SJens Axboe }; 1435274f052SJens Axboe 144912d35f8SJens Axboe static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, 145912d35f8SJens Axboe struct pipe_buffer *buf) 146912d35f8SJens Axboe { 1477afa6fd0SJens Axboe if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 148912d35f8SJens Axboe return 1; 1497afa6fd0SJens Axboe 1501432873aSJens Axboe buf->flags |= PIPE_BUF_FLAG_LRU; 151330ab716SJens Axboe return generic_pipe_buf_steal(pipe, buf); 152912d35f8SJens Axboe } 153912d35f8SJens Axboe 154d4c3cca9SEric Dumazet static const struct pipe_buf_operations user_page_pipe_buf_ops = { 155912d35f8SJens Axboe .can_merge = 0, 156f84d7519SJens Axboe .map = generic_pipe_buf_map, 157f84d7519SJens Axboe .unmap = generic_pipe_buf_unmap, 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 164932cc6d4SJens Axboe /** 165932cc6d4SJens Axboe * splice_to_pipe - fill passed data into a pipe 166932cc6d4SJens Axboe * @pipe: pipe to fill 167932cc6d4SJens Axboe * @spd: data to fill 168932cc6d4SJens Axboe * 169932cc6d4SJens Axboe * Description: 17079685b8dSRandy Dunlap * @spd contains a map of pages and len/offset tuples, along with 171932cc6d4SJens Axboe * the struct pipe_buf_operations associated with these pages. This 172932cc6d4SJens Axboe * function will link that data to the pipe. 173932cc6d4SJens Axboe * 17483f9135bSJens Axboe */ 175d6b29d7cSJens Axboe ssize_t splice_to_pipe(struct pipe_inode_info *pipe, 176912d35f8SJens Axboe struct splice_pipe_desc *spd) 1775274f052SJens Axboe { 17800de00bdSJens Axboe unsigned int spd_pages = spd->nr_pages; 179912d35f8SJens Axboe int ret, do_wakeup, page_nr; 1805274f052SJens Axboe 1815274f052SJens Axboe ret = 0; 1825274f052SJens Axboe do_wakeup = 0; 183912d35f8SJens Axboe page_nr = 0; 1845274f052SJens Axboe 18561e0d47cSMiklos Szeredi pipe_lock(pipe); 1865274f052SJens Axboe 1875274f052SJens Axboe for (;;) { 1883a326a2cSIngo Molnar if (!pipe->readers) { 1895274f052SJens Axboe send_sig(SIGPIPE, current, 0); 1905274f052SJens Axboe if (!ret) 1915274f052SJens Axboe ret = -EPIPE; 1925274f052SJens Axboe break; 1935274f052SJens Axboe } 1945274f052SJens Axboe 1956f767b04SJens Axboe if (pipe->nrbufs < PIPE_BUFFERS) { 1966f767b04SJens Axboe int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); 1973a326a2cSIngo Molnar struct pipe_buffer *buf = pipe->bufs + newbuf; 1985274f052SJens Axboe 199912d35f8SJens Axboe buf->page = spd->pages[page_nr]; 200912d35f8SJens Axboe buf->offset = spd->partial[page_nr].offset; 201912d35f8SJens Axboe buf->len = spd->partial[page_nr].len; 202497f9625SJens Axboe buf->private = spd->partial[page_nr].private; 203912d35f8SJens Axboe buf->ops = spd->ops; 2047afa6fd0SJens Axboe if (spd->flags & SPLICE_F_GIFT) 2057afa6fd0SJens Axboe buf->flags |= PIPE_BUF_FLAG_GIFT; 2067afa6fd0SJens Axboe 2076f767b04SJens Axboe pipe->nrbufs++; 208912d35f8SJens Axboe page_nr++; 209912d35f8SJens Axboe ret += buf->len; 210912d35f8SJens Axboe 2116f767b04SJens Axboe if (pipe->inode) 2125274f052SJens Axboe do_wakeup = 1; 2135274f052SJens Axboe 214912d35f8SJens Axboe if (!--spd->nr_pages) 2155274f052SJens Axboe break; 2166f767b04SJens Axboe if (pipe->nrbufs < PIPE_BUFFERS) 2175274f052SJens Axboe continue; 2185274f052SJens Axboe 2195274f052SJens Axboe break; 2205274f052SJens Axboe } 2215274f052SJens Axboe 222912d35f8SJens Axboe if (spd->flags & SPLICE_F_NONBLOCK) { 22329e35094SLinus Torvalds if (!ret) 22429e35094SLinus Torvalds ret = -EAGAIN; 22529e35094SLinus Torvalds break; 22629e35094SLinus Torvalds } 22729e35094SLinus Torvalds 2285274f052SJens Axboe if (signal_pending(current)) { 2295274f052SJens Axboe if (!ret) 2305274f052SJens Axboe ret = -ERESTARTSYS; 2315274f052SJens Axboe break; 2325274f052SJens Axboe } 2335274f052SJens Axboe 2345274f052SJens Axboe if (do_wakeup) { 235c0bd1f65SJens Axboe smp_mb(); 2363a326a2cSIngo Molnar if (waitqueue_active(&pipe->wait)) 2373a326a2cSIngo Molnar wake_up_interruptible_sync(&pipe->wait); 2383a326a2cSIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 2395274f052SJens Axboe do_wakeup = 0; 2405274f052SJens Axboe } 2415274f052SJens Axboe 2423a326a2cSIngo Molnar pipe->waiting_writers++; 2433a326a2cSIngo Molnar pipe_wait(pipe); 2443a326a2cSIngo Molnar pipe->waiting_writers--; 2455274f052SJens Axboe } 2465274f052SJens Axboe 24761e0d47cSMiklos Szeredi pipe_unlock(pipe); 2485274f052SJens Axboe 2495274f052SJens Axboe if (do_wakeup) { 250c0bd1f65SJens Axboe smp_mb(); 2513a326a2cSIngo Molnar if (waitqueue_active(&pipe->wait)) 2523a326a2cSIngo Molnar wake_up_interruptible(&pipe->wait); 2533a326a2cSIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 2545274f052SJens Axboe } 2555274f052SJens Axboe 25600de00bdSJens Axboe while (page_nr < spd_pages) 257bbdfc2f7SJens Axboe spd->spd_release(spd, page_nr++); 2585274f052SJens Axboe 2595274f052SJens Axboe return ret; 2605274f052SJens Axboe } 2615274f052SJens Axboe 262bbdfc2f7SJens Axboe static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) 263bbdfc2f7SJens Axboe { 264bbdfc2f7SJens Axboe page_cache_release(spd->pages[i]); 265bbdfc2f7SJens Axboe } 266bbdfc2f7SJens Axboe 2673a326a2cSIngo Molnar static int 268cbb7e577SJens Axboe __generic_file_splice_read(struct file *in, loff_t *ppos, 269cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 270cbb7e577SJens Axboe unsigned int flags) 2715274f052SJens Axboe { 2725274f052SJens Axboe struct address_space *mapping = in->f_mapping; 273d8983910SFengguang Wu unsigned int loff, nr_pages, req_pages; 27416c523ddSJens Axboe struct page *pages[PIPE_BUFFERS]; 275912d35f8SJens Axboe struct partial_page partial[PIPE_BUFFERS]; 2765274f052SJens Axboe struct page *page; 27791ad66efSJens Axboe pgoff_t index, end_index; 27891ad66efSJens Axboe loff_t isize; 279eb20796bSJens Axboe int error, page_nr; 280912d35f8SJens Axboe struct splice_pipe_desc spd = { 281912d35f8SJens Axboe .pages = pages, 282912d35f8SJens Axboe .partial = partial, 283912d35f8SJens Axboe .flags = flags, 284912d35f8SJens Axboe .ops = &page_cache_pipe_buf_ops, 285bbdfc2f7SJens Axboe .spd_release = spd_release_page, 286912d35f8SJens Axboe }; 2875274f052SJens Axboe 288cbb7e577SJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 289912d35f8SJens Axboe loff = *ppos & ~PAGE_CACHE_MASK; 290d8983910SFengguang Wu req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 291d8983910SFengguang Wu nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS); 2925274f052SJens Axboe 2935274f052SJens Axboe /* 294eb20796bSJens Axboe * Lookup the (hopefully) full range of pages we need. 29582aa5d61SJens Axboe */ 296eb20796bSJens Axboe spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages); 297431a4820SFengguang Wu index += spd.nr_pages; 298eb20796bSJens Axboe 2995274f052SJens Axboe /* 300eb20796bSJens Axboe * If find_get_pages_contig() returned fewer pages than we needed, 301431a4820SFengguang Wu * readahead/allocate the rest and fill in the holes. 302eb20796bSJens Axboe */ 303431a4820SFengguang Wu if (spd.nr_pages < nr_pages) 304cf914a7dSRusty Russell page_cache_sync_readahead(mapping, &in->f_ra, in, 305cf914a7dSRusty Russell index, req_pages - spd.nr_pages); 306431a4820SFengguang Wu 307932cc6d4SJens Axboe error = 0; 308eb20796bSJens Axboe while (spd.nr_pages < nr_pages) { 309eb20796bSJens Axboe /* 310eb20796bSJens Axboe * Page could be there, find_get_pages_contig() breaks on 311eb20796bSJens Axboe * the first hole. 3125274f052SJens Axboe */ 3137480a904SJens Axboe page = find_get_page(mapping, index); 3147480a904SJens Axboe if (!page) { 315e27dedd8SJens Axboe /* 316eb20796bSJens Axboe * page didn't exist, allocate one. 3177480a904SJens Axboe */ 3187480a904SJens Axboe page = page_cache_alloc_cold(mapping); 3195274f052SJens Axboe if (!page) 3205274f052SJens Axboe break; 3215274f052SJens Axboe 3227480a904SJens Axboe error = add_to_page_cache_lru(page, mapping, index, 3234cd13504SHugh Dickins mapping_gfp_mask(mapping)); 3245274f052SJens Axboe if (unlikely(error)) { 3255274f052SJens Axboe page_cache_release(page); 326a0548871SJens Axboe if (error == -EEXIST) 327a0548871SJens Axboe continue; 3285274f052SJens Axboe break; 3295274f052SJens Axboe } 330eb20796bSJens Axboe /* 331eb20796bSJens Axboe * add_to_page_cache() locks the page, unlock it 332eb20796bSJens Axboe * to avoid convoluting the logic below even more. 333eb20796bSJens Axboe */ 334eb20796bSJens Axboe unlock_page(page); 3355274f052SJens Axboe } 3367480a904SJens Axboe 337eb20796bSJens Axboe pages[spd.nr_pages++] = page; 338eb20796bSJens Axboe index++; 339eb20796bSJens Axboe } 340eb20796bSJens Axboe 341eb20796bSJens Axboe /* 342eb20796bSJens Axboe * Now loop over the map and see if we need to start IO on any 343eb20796bSJens Axboe * pages, fill in the partial map, etc. 344eb20796bSJens Axboe */ 345eb20796bSJens Axboe index = *ppos >> PAGE_CACHE_SHIFT; 346eb20796bSJens Axboe nr_pages = spd.nr_pages; 347eb20796bSJens Axboe spd.nr_pages = 0; 348eb20796bSJens Axboe for (page_nr = 0; page_nr < nr_pages; page_nr++) { 349eb20796bSJens Axboe unsigned int this_len; 350eb20796bSJens Axboe 351eb20796bSJens Axboe if (!len) 352eb20796bSJens Axboe break; 353eb20796bSJens Axboe 354eb20796bSJens Axboe /* 355eb20796bSJens Axboe * this_len is the max we'll use from this page 356eb20796bSJens Axboe */ 357eb20796bSJens Axboe this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 358eb20796bSJens Axboe page = pages[page_nr]; 359eb20796bSJens Axboe 360a08a166fSFengguang Wu if (PageReadahead(page)) 361cf914a7dSRusty Russell page_cache_async_readahead(mapping, &in->f_ra, in, 362d8983910SFengguang Wu page, index, req_pages - page_nr); 363a08a166fSFengguang Wu 3647480a904SJens Axboe /* 3657480a904SJens Axboe * If the page isn't uptodate, we may need to start io on it 3667480a904SJens Axboe */ 3677480a904SJens Axboe if (!PageUptodate(page)) { 368c4f895cbSJens Axboe /* 369c4f895cbSJens Axboe * If in nonblock mode then dont block on waiting 370c4f895cbSJens Axboe * for an in-flight io page 371c4f895cbSJens Axboe */ 3729ae9d68cSFengguang Wu if (flags & SPLICE_F_NONBLOCK) { 373529ae9aaSNick Piggin if (!trylock_page(page)) { 3748191ecd1SJens Axboe error = -EAGAIN; 375c4f895cbSJens Axboe break; 3768191ecd1SJens Axboe } 3779ae9d68cSFengguang Wu } else 3787480a904SJens Axboe lock_page(page); 3797480a904SJens Axboe 3807480a904SJens Axboe /* 38132502b84SMiklos Szeredi * Page was truncated, or invalidated by the 38232502b84SMiklos Szeredi * filesystem. Redo the find/create, but this time the 38332502b84SMiklos Szeredi * page is kept locked, so there's no chance of another 38432502b84SMiklos Szeredi * race with truncate/invalidate. 3857480a904SJens Axboe */ 3867480a904SJens Axboe if (!page->mapping) { 3877480a904SJens Axboe unlock_page(page); 38832502b84SMiklos Szeredi page = find_or_create_page(mapping, index, 38932502b84SMiklos Szeredi mapping_gfp_mask(mapping)); 39032502b84SMiklos Szeredi 39132502b84SMiklos Szeredi if (!page) { 39232502b84SMiklos Szeredi error = -ENOMEM; 3937480a904SJens Axboe break; 3947480a904SJens Axboe } 39532502b84SMiklos Szeredi page_cache_release(pages[page_nr]); 39632502b84SMiklos Szeredi pages[page_nr] = page; 39732502b84SMiklos Szeredi } 3987480a904SJens Axboe /* 3997480a904SJens Axboe * page was already under io and is now done, great 4007480a904SJens Axboe */ 4017480a904SJens Axboe if (PageUptodate(page)) { 4027480a904SJens Axboe unlock_page(page); 4037480a904SJens Axboe goto fill_it; 4047480a904SJens Axboe } 4057480a904SJens Axboe 4067480a904SJens Axboe /* 4077480a904SJens Axboe * need to read in the page 4087480a904SJens Axboe */ 4097480a904SJens Axboe error = mapping->a_ops->readpage(in, page); 4107480a904SJens Axboe if (unlikely(error)) { 411eb20796bSJens Axboe /* 412eb20796bSJens Axboe * We really should re-lookup the page here, 413eb20796bSJens Axboe * but it complicates things a lot. Instead 414eb20796bSJens Axboe * lets just do what we already stored, and 415eb20796bSJens Axboe * we'll get it the next time we are called. 416eb20796bSJens Axboe */ 4177480a904SJens Axboe if (error == AOP_TRUNCATED_PAGE) 418eb20796bSJens Axboe error = 0; 419eb20796bSJens Axboe 4207480a904SJens Axboe break; 4217480a904SJens Axboe } 422620a324bSJens Axboe } 423620a324bSJens Axboe fill_it: 42491ad66efSJens Axboe /* 425620a324bSJens Axboe * i_size must be checked after PageUptodate. 42691ad66efSJens Axboe */ 42791ad66efSJens Axboe isize = i_size_read(mapping->host); 42891ad66efSJens Axboe end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 429eb20796bSJens Axboe if (unlikely(!isize || index > end_index)) 43091ad66efSJens Axboe break; 43191ad66efSJens Axboe 43291ad66efSJens Axboe /* 43391ad66efSJens Axboe * if this is the last page, see if we need to shrink 43491ad66efSJens Axboe * the length and stop 43591ad66efSJens Axboe */ 43691ad66efSJens Axboe if (end_index == index) { 437475ecadeSHugh Dickins unsigned int plen; 438475ecadeSHugh Dickins 439475ecadeSHugh Dickins /* 440475ecadeSHugh Dickins * max good bytes in this page 441475ecadeSHugh Dickins */ 442475ecadeSHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 443475ecadeSHugh Dickins if (plen <= loff) 44491ad66efSJens Axboe break; 445475ecadeSHugh Dickins 44691ad66efSJens Axboe /* 44791ad66efSJens Axboe * force quit after adding this page 44891ad66efSJens Axboe */ 449475ecadeSHugh Dickins this_len = min(this_len, plen - loff); 450eb20796bSJens Axboe len = this_len; 45191ad66efSJens Axboe } 452620a324bSJens Axboe 453eb20796bSJens Axboe partial[page_nr].offset = loff; 454eb20796bSJens Axboe partial[page_nr].len = this_len; 45582aa5d61SJens Axboe len -= this_len; 45691ad66efSJens Axboe loff = 0; 457eb20796bSJens Axboe spd.nr_pages++; 458eb20796bSJens Axboe index++; 4595274f052SJens Axboe } 4605274f052SJens Axboe 461eb20796bSJens Axboe /* 462475ecadeSHugh Dickins * Release any pages at the end, if we quit early. 'page_nr' is how far 463eb20796bSJens Axboe * we got, 'nr_pages' is how many pages are in the map. 464eb20796bSJens Axboe */ 465eb20796bSJens Axboe while (page_nr < nr_pages) 466eb20796bSJens Axboe page_cache_release(pages[page_nr++]); 467f4e6b498SFengguang Wu in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT; 468eb20796bSJens Axboe 469912d35f8SJens Axboe if (spd.nr_pages) 47000522fb4SJens Axboe return splice_to_pipe(pipe, &spd); 47116c523ddSJens Axboe 4727480a904SJens Axboe return error; 4735274f052SJens Axboe } 4745274f052SJens Axboe 47583f9135bSJens Axboe /** 47683f9135bSJens Axboe * generic_file_splice_read - splice data from file to a pipe 47783f9135bSJens Axboe * @in: file to splice from 478932cc6d4SJens Axboe * @ppos: position in @in 47983f9135bSJens Axboe * @pipe: pipe to splice to 48083f9135bSJens Axboe * @len: number of bytes to splice 48183f9135bSJens Axboe * @flags: splice modifier flags 48283f9135bSJens Axboe * 483932cc6d4SJens Axboe * Description: 484932cc6d4SJens Axboe * Will read pages from given file and fill them into a pipe. Can be 485932cc6d4SJens Axboe * used as long as the address_space operations for the source implements 486932cc6d4SJens Axboe * a readpage() hook. 487932cc6d4SJens Axboe * 48883f9135bSJens Axboe */ 489cbb7e577SJens Axboe ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, 490cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 491cbb7e577SJens Axboe unsigned int flags) 4925274f052SJens Axboe { 493d366d398SJens Axboe loff_t isize, left; 4948191ecd1SJens Axboe int ret; 495d366d398SJens Axboe 496d366d398SJens Axboe isize = i_size_read(in->f_mapping->host); 497d366d398SJens Axboe if (unlikely(*ppos >= isize)) 498d366d398SJens Axboe return 0; 499d366d398SJens Axboe 500d366d398SJens Axboe left = isize - *ppos; 501d366d398SJens Axboe if (unlikely(left < len)) 502d366d398SJens Axboe len = left; 5035274f052SJens Axboe 504cbb7e577SJens Axboe ret = __generic_file_splice_read(in, ppos, pipe, len, flags); 5058191ecd1SJens Axboe if (ret > 0) 506cbb7e577SJens Axboe *ppos += ret; 5075274f052SJens Axboe 5085274f052SJens Axboe return ret; 5095274f052SJens Axboe } 5105274f052SJens Axboe 511059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_read); 512059a8f37SJens Axboe 5135274f052SJens Axboe /* 5144f6f0bd2SJens Axboe * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 515016b661eSJens Axboe * using sendpage(). Return the number of bytes sent. 5165274f052SJens Axboe */ 51776ad4d11SJens Axboe static int pipe_to_sendpage(struct pipe_inode_info *pipe, 5185274f052SJens Axboe struct pipe_buffer *buf, struct splice_desc *sd) 5195274f052SJens Axboe { 5206a14b90bSJens Axboe struct file *file = sd->u.file; 5215274f052SJens Axboe loff_t pos = sd->pos; 522f84d7519SJens Axboe int ret, more; 5235274f052SJens Axboe 524cac36bb0SJens Axboe ret = buf->ops->confirm(pipe, buf); 525f84d7519SJens Axboe if (!ret) { 526b2b39fa4SJens Axboe more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len; 5275274f052SJens Axboe 528f84d7519SJens Axboe ret = file->f_op->sendpage(file, buf->page, buf->offset, 529f84d7519SJens Axboe sd->len, &pos, more); 530f84d7519SJens Axboe } 5315274f052SJens Axboe 532016b661eSJens Axboe return ret; 5335274f052SJens Axboe } 5345274f052SJens Axboe 5355274f052SJens Axboe /* 5365274f052SJens Axboe * This is a little more tricky than the file -> pipe splicing. There are 5375274f052SJens Axboe * basically three cases: 5385274f052SJens Axboe * 5395274f052SJens Axboe * - Destination page already exists in the address space and there 5405274f052SJens Axboe * are users of it. For that case we have no other option that 5415274f052SJens Axboe * copying the data. Tough luck. 5425274f052SJens Axboe * - Destination page already exists in the address space, but there 5435274f052SJens Axboe * are no users of it. Make sure it's uptodate, then drop it. Fall 5445274f052SJens Axboe * through to last case. 5455274f052SJens Axboe * - Destination page does not exist, we can add the pipe page to 5465274f052SJens Axboe * the page cache and avoid the copy. 5475274f052SJens Axboe * 54883f9135bSJens Axboe * If asked to move pages to the output file (SPLICE_F_MOVE is set in 54983f9135bSJens Axboe * sd->flags), we attempt to migrate pages from the pipe to the output 55083f9135bSJens Axboe * file address space page cache. This is possible if no one else has 55183f9135bSJens Axboe * the pipe page referenced outside of the pipe and page cache. If 55283f9135bSJens Axboe * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create 55383f9135bSJens Axboe * a new page in the output file page cache and fill/dirty that. 5545274f052SJens Axboe */ 555328eaabaSMiklos Szeredi int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 5565274f052SJens Axboe struct splice_desc *sd) 5575274f052SJens Axboe { 5586a14b90bSJens Axboe struct file *file = sd->u.file; 5595274f052SJens Axboe struct address_space *mapping = file->f_mapping; 560016b661eSJens Axboe unsigned int offset, this_len; 5615274f052SJens Axboe struct page *page; 562afddba49SNick Piggin void *fsdata; 5633e7ee3e7SJens Axboe int ret; 5645274f052SJens Axboe 5655274f052SJens Axboe /* 56649d0b21bSJens Axboe * make sure the data in this buffer is uptodate 5675274f052SJens Axboe */ 568cac36bb0SJens Axboe ret = buf->ops->confirm(pipe, buf); 569f84d7519SJens Axboe if (unlikely(ret)) 570f84d7519SJens Axboe return ret; 5715274f052SJens Axboe 5725274f052SJens Axboe offset = sd->pos & ~PAGE_CACHE_MASK; 5735274f052SJens Axboe 574016b661eSJens Axboe this_len = sd->len; 575016b661eSJens Axboe if (this_len + offset > PAGE_CACHE_SIZE) 576016b661eSJens Axboe this_len = PAGE_CACHE_SIZE - offset; 577016b661eSJens Axboe 578afddba49SNick Piggin ret = pagecache_write_begin(file, mapping, sd->pos, this_len, 579afddba49SNick Piggin AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 5809e0267c2SJens Axboe if (unlikely(ret)) 581afddba49SNick Piggin goto out; 5825274f052SJens Axboe 5830568b409SJens Axboe if (buf->page != page) { 584f84d7519SJens Axboe /* 585f84d7519SJens Axboe * Careful, ->map() uses KM_USER0! 586f84d7519SJens Axboe */ 58776ad4d11SJens Axboe char *src = buf->ops->map(pipe, buf, 1); 588f84d7519SJens Axboe char *dst = kmap_atomic(page, KM_USER1); 5895abc97aaSJens Axboe 590016b661eSJens Axboe memcpy(dst + offset, src + buf->offset, this_len); 5915274f052SJens Axboe flush_dcache_page(page); 592f84d7519SJens Axboe kunmap_atomic(dst, KM_USER1); 59376ad4d11SJens Axboe buf->ops->unmap(pipe, buf, src); 5945abc97aaSJens Axboe } 595afddba49SNick Piggin ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len, 596afddba49SNick Piggin page, fsdata); 5970568b409SJens Axboe out: 5985274f052SJens Axboe return ret; 5995274f052SJens Axboe } 600328eaabaSMiklos Szeredi EXPORT_SYMBOL(pipe_to_file); 6015274f052SJens Axboe 602b3c2d2ddSMiklos Szeredi static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 603b3c2d2ddSMiklos Szeredi { 604b3c2d2ddSMiklos Szeredi smp_mb(); 605b3c2d2ddSMiklos Szeredi if (waitqueue_active(&pipe->wait)) 606b3c2d2ddSMiklos Szeredi wake_up_interruptible(&pipe->wait); 607b3c2d2ddSMiklos Szeredi kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 608b3c2d2ddSMiklos Szeredi } 609b3c2d2ddSMiklos Szeredi 610b3c2d2ddSMiklos Szeredi /** 611b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed - feed available data from a pipe to a file 612b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 613b3c2d2ddSMiklos Szeredi * @sd: information to @actor 614b3c2d2ddSMiklos Szeredi * @actor: handler that splices the data 615b3c2d2ddSMiklos Szeredi * 616b3c2d2ddSMiklos Szeredi * Description: 617b3c2d2ddSMiklos Szeredi * This function loops over the pipe and calls @actor to do the 618b3c2d2ddSMiklos Szeredi * actual moving of a single struct pipe_buffer to the desired 619b3c2d2ddSMiklos Szeredi * destination. It returns when there's no more buffers left in 620b3c2d2ddSMiklos Szeredi * the pipe or if the requested number of bytes (@sd->total_len) 621b3c2d2ddSMiklos Szeredi * have been copied. It returns a positive number (one) if the 622b3c2d2ddSMiklos Szeredi * pipe needs to be filled with more data, zero if the required 623b3c2d2ddSMiklos Szeredi * number of bytes have been copied and -errno on error. 624b3c2d2ddSMiklos Szeredi * 625b3c2d2ddSMiklos Szeredi * This, together with splice_from_pipe_{begin,end,next}, may be 626b3c2d2ddSMiklos Szeredi * used to implement the functionality of __splice_from_pipe() when 627b3c2d2ddSMiklos Szeredi * locking is required around copying the pipe buffers to the 628b3c2d2ddSMiklos Szeredi * destination. 629b3c2d2ddSMiklos Szeredi */ 630b3c2d2ddSMiklos Szeredi int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 631b3c2d2ddSMiklos Szeredi splice_actor *actor) 632b3c2d2ddSMiklos Szeredi { 633b3c2d2ddSMiklos Szeredi int ret; 634b3c2d2ddSMiklos Szeredi 635b3c2d2ddSMiklos Szeredi while (pipe->nrbufs) { 636b3c2d2ddSMiklos Szeredi struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; 637b3c2d2ddSMiklos Szeredi const struct pipe_buf_operations *ops = buf->ops; 638b3c2d2ddSMiklos Szeredi 639b3c2d2ddSMiklos Szeredi sd->len = buf->len; 640b3c2d2ddSMiklos Szeredi if (sd->len > sd->total_len) 641b3c2d2ddSMiklos Szeredi sd->len = sd->total_len; 642b3c2d2ddSMiklos Szeredi 643b3c2d2ddSMiklos Szeredi ret = actor(pipe, buf, sd); 644b3c2d2ddSMiklos Szeredi if (ret <= 0) { 645b3c2d2ddSMiklos Szeredi if (ret == -ENODATA) 646b3c2d2ddSMiklos Szeredi ret = 0; 647b3c2d2ddSMiklos Szeredi return ret; 648b3c2d2ddSMiklos Szeredi } 649b3c2d2ddSMiklos Szeredi buf->offset += ret; 650b3c2d2ddSMiklos Szeredi buf->len -= ret; 651b3c2d2ddSMiklos Szeredi 652b3c2d2ddSMiklos Szeredi sd->num_spliced += ret; 653b3c2d2ddSMiklos Szeredi sd->len -= ret; 654b3c2d2ddSMiklos Szeredi sd->pos += ret; 655b3c2d2ddSMiklos Szeredi sd->total_len -= ret; 656b3c2d2ddSMiklos Szeredi 657b3c2d2ddSMiklos Szeredi if (!buf->len) { 658b3c2d2ddSMiklos Szeredi buf->ops = NULL; 659b3c2d2ddSMiklos Szeredi ops->release(pipe, buf); 660b3c2d2ddSMiklos Szeredi pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); 661b3c2d2ddSMiklos Szeredi pipe->nrbufs--; 662b3c2d2ddSMiklos Szeredi if (pipe->inode) 663b3c2d2ddSMiklos Szeredi sd->need_wakeup = true; 664b3c2d2ddSMiklos Szeredi } 665b3c2d2ddSMiklos Szeredi 666b3c2d2ddSMiklos Szeredi if (!sd->total_len) 667b3c2d2ddSMiklos Szeredi return 0; 668b3c2d2ddSMiklos Szeredi } 669b3c2d2ddSMiklos Szeredi 670b3c2d2ddSMiklos Szeredi return 1; 671b3c2d2ddSMiklos Szeredi } 672b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_feed); 673b3c2d2ddSMiklos Szeredi 674b3c2d2ddSMiklos Szeredi /** 675b3c2d2ddSMiklos Szeredi * splice_from_pipe_next - wait for some data to splice from 676b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 677b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 678b3c2d2ddSMiklos Szeredi * 679b3c2d2ddSMiklos Szeredi * Description: 680b3c2d2ddSMiklos Szeredi * This function will wait for some data and return a positive 681b3c2d2ddSMiklos Szeredi * value (one) if pipe buffers are available. It will return zero 682b3c2d2ddSMiklos Szeredi * or -errno if no more data needs to be spliced. 683b3c2d2ddSMiklos Szeredi */ 684b3c2d2ddSMiklos Szeredi int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 685b3c2d2ddSMiklos Szeredi { 686b3c2d2ddSMiklos Szeredi while (!pipe->nrbufs) { 687b3c2d2ddSMiklos Szeredi if (!pipe->writers) 688b3c2d2ddSMiklos Szeredi return 0; 689b3c2d2ddSMiklos Szeredi 690b3c2d2ddSMiklos Szeredi if (!pipe->waiting_writers && sd->num_spliced) 691b3c2d2ddSMiklos Szeredi return 0; 692b3c2d2ddSMiklos Szeredi 693b3c2d2ddSMiklos Szeredi if (sd->flags & SPLICE_F_NONBLOCK) 694b3c2d2ddSMiklos Szeredi return -EAGAIN; 695b3c2d2ddSMiklos Szeredi 696b3c2d2ddSMiklos Szeredi if (signal_pending(current)) 697b3c2d2ddSMiklos Szeredi return -ERESTARTSYS; 698b3c2d2ddSMiklos Szeredi 699b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) { 700b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 701b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 702b3c2d2ddSMiklos Szeredi } 703b3c2d2ddSMiklos Szeredi 704b3c2d2ddSMiklos Szeredi pipe_wait(pipe); 705b3c2d2ddSMiklos Szeredi } 706b3c2d2ddSMiklos Szeredi 707b3c2d2ddSMiklos Szeredi return 1; 708b3c2d2ddSMiklos Szeredi } 709b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_next); 710b3c2d2ddSMiklos Szeredi 711b3c2d2ddSMiklos Szeredi /** 712b3c2d2ddSMiklos Szeredi * splice_from_pipe_begin - start splicing from pipe 713b80901bbSRandy Dunlap * @sd: information about the splice operation 714b3c2d2ddSMiklos Szeredi * 715b3c2d2ddSMiklos Szeredi * Description: 716b3c2d2ddSMiklos Szeredi * This function should be called before a loop containing 717b3c2d2ddSMiklos Szeredi * splice_from_pipe_next() and splice_from_pipe_feed() to 718b3c2d2ddSMiklos Szeredi * initialize the necessary fields of @sd. 719b3c2d2ddSMiklos Szeredi */ 720b3c2d2ddSMiklos Szeredi void splice_from_pipe_begin(struct splice_desc *sd) 721b3c2d2ddSMiklos Szeredi { 722b3c2d2ddSMiklos Szeredi sd->num_spliced = 0; 723b3c2d2ddSMiklos Szeredi sd->need_wakeup = false; 724b3c2d2ddSMiklos Szeredi } 725b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_begin); 726b3c2d2ddSMiklos Szeredi 727b3c2d2ddSMiklos Szeredi /** 728b3c2d2ddSMiklos Szeredi * splice_from_pipe_end - finish splicing from pipe 729b3c2d2ddSMiklos Szeredi * @pipe: pipe to splice from 730b3c2d2ddSMiklos Szeredi * @sd: information about the splice operation 731b3c2d2ddSMiklos Szeredi * 732b3c2d2ddSMiklos Szeredi * Description: 733b3c2d2ddSMiklos Szeredi * This function will wake up pipe writers if necessary. It should 734b3c2d2ddSMiklos Szeredi * be called after a loop containing splice_from_pipe_next() and 735b3c2d2ddSMiklos Szeredi * splice_from_pipe_feed(). 736b3c2d2ddSMiklos Szeredi */ 737b3c2d2ddSMiklos Szeredi void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 738b3c2d2ddSMiklos Szeredi { 739b3c2d2ddSMiklos Szeredi if (sd->need_wakeup) 740b3c2d2ddSMiklos Szeredi wakeup_pipe_writers(pipe); 741b3c2d2ddSMiklos Szeredi } 742b3c2d2ddSMiklos Szeredi EXPORT_SYMBOL(splice_from_pipe_end); 743b3c2d2ddSMiklos Szeredi 744932cc6d4SJens Axboe /** 745932cc6d4SJens Axboe * __splice_from_pipe - splice data from a pipe to given actor 746932cc6d4SJens Axboe * @pipe: pipe to splice from 747932cc6d4SJens Axboe * @sd: information to @actor 748932cc6d4SJens Axboe * @actor: handler that splices the data 749932cc6d4SJens Axboe * 750932cc6d4SJens Axboe * Description: 751932cc6d4SJens Axboe * This function does little more than loop over the pipe and call 752932cc6d4SJens Axboe * @actor to do the actual moving of a single struct pipe_buffer to 753932cc6d4SJens Axboe * the desired destination. See pipe_to_file, pipe_to_sendpage, or 754932cc6d4SJens Axboe * pipe_to_user. 755932cc6d4SJens Axboe * 75683f9135bSJens Axboe */ 757c66ab6faSJens Axboe ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 758c66ab6faSJens Axboe splice_actor *actor) 7595274f052SJens Axboe { 760b3c2d2ddSMiklos Szeredi int ret; 7615274f052SJens Axboe 762b3c2d2ddSMiklos Szeredi splice_from_pipe_begin(sd); 763b3c2d2ddSMiklos Szeredi do { 764b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_next(pipe, sd); 765b3c2d2ddSMiklos Szeredi if (ret > 0) 766b3c2d2ddSMiklos Szeredi ret = splice_from_pipe_feed(pipe, sd, actor); 767b3c2d2ddSMiklos Szeredi } while (ret > 0); 768b3c2d2ddSMiklos Szeredi splice_from_pipe_end(pipe, sd); 7695274f052SJens Axboe 770b3c2d2ddSMiklos Szeredi return sd->num_spliced ? sd->num_spliced : ret; 7715274f052SJens Axboe } 77240bee44eSMark Fasheh EXPORT_SYMBOL(__splice_from_pipe); 7735274f052SJens Axboe 774932cc6d4SJens Axboe /** 775932cc6d4SJens Axboe * splice_from_pipe - splice data from a pipe to a file 776932cc6d4SJens Axboe * @pipe: pipe to splice from 777932cc6d4SJens Axboe * @out: file to splice to 778932cc6d4SJens Axboe * @ppos: position in @out 779932cc6d4SJens Axboe * @len: how many bytes to splice 780932cc6d4SJens Axboe * @flags: splice modifier flags 781932cc6d4SJens Axboe * @actor: handler that splices the data 782932cc6d4SJens Axboe * 783932cc6d4SJens Axboe * Description: 7842933970bSMiklos Szeredi * See __splice_from_pipe. This function locks the pipe inode, 785932cc6d4SJens Axboe * otherwise it's identical to __splice_from_pipe(). 786932cc6d4SJens Axboe * 787932cc6d4SJens Axboe */ 7886da61809SMark Fasheh ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 7896da61809SMark Fasheh loff_t *ppos, size_t len, unsigned int flags, 7906da61809SMark Fasheh splice_actor *actor) 7916da61809SMark Fasheh { 7926da61809SMark Fasheh ssize_t ret; 793c66ab6faSJens Axboe struct splice_desc sd = { 794c66ab6faSJens Axboe .total_len = len, 795c66ab6faSJens Axboe .flags = flags, 796c66ab6faSJens Axboe .pos = *ppos, 7976a14b90bSJens Axboe .u.file = out, 798c66ab6faSJens Axboe }; 7996da61809SMark Fasheh 80061e0d47cSMiklos Szeredi pipe_lock(pipe); 801c66ab6faSJens Axboe ret = __splice_from_pipe(pipe, &sd, actor); 80261e0d47cSMiklos Szeredi pipe_unlock(pipe); 8036da61809SMark Fasheh 8046da61809SMark Fasheh return ret; 8056da61809SMark Fasheh } 8066da61809SMark Fasheh 8076da61809SMark Fasheh /** 80883f9135bSJens Axboe * generic_file_splice_write - splice data from a pipe to a file 8093a326a2cSIngo Molnar * @pipe: pipe info 81083f9135bSJens Axboe * @out: file to write to 811932cc6d4SJens Axboe * @ppos: position in @out 81283f9135bSJens Axboe * @len: number of bytes to splice 81383f9135bSJens Axboe * @flags: splice modifier flags 81483f9135bSJens Axboe * 815932cc6d4SJens Axboe * Description: 81683f9135bSJens Axboe * Will either move or copy pages (determined by @flags options) from 81783f9135bSJens Axboe * the given pipe inode to the given file. 81883f9135bSJens Axboe * 81983f9135bSJens Axboe */ 8203a326a2cSIngo Molnar ssize_t 8213a326a2cSIngo Molnar generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 822cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 8235274f052SJens Axboe { 8244f6f0bd2SJens Axboe struct address_space *mapping = out->f_mapping; 8258c34e2d6SJens Axboe struct inode *inode = mapping->host; 8267f3d4ee1SMiklos Szeredi struct splice_desc sd = { 8277f3d4ee1SMiklos Szeredi .total_len = len, 8287f3d4ee1SMiklos Szeredi .flags = flags, 8297f3d4ee1SMiklos Szeredi .pos = *ppos, 8307f3d4ee1SMiklos Szeredi .u.file = out, 8317f3d4ee1SMiklos Szeredi }; 8323a326a2cSIngo Molnar ssize_t ret; 8338c34e2d6SJens Axboe 83461e0d47cSMiklos Szeredi pipe_lock(pipe); 835eb443e5aSMiklos Szeredi 836eb443e5aSMiklos Szeredi splice_from_pipe_begin(&sd); 837eb443e5aSMiklos Szeredi do { 838eb443e5aSMiklos Szeredi ret = splice_from_pipe_next(pipe, &sd); 839eb443e5aSMiklos Szeredi if (ret <= 0) 840eb443e5aSMiklos Szeredi break; 841eb443e5aSMiklos Szeredi 842eb443e5aSMiklos Szeredi mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 843eb443e5aSMiklos Szeredi ret = file_remove_suid(out); 844eb443e5aSMiklos Szeredi if (!ret) 845eb443e5aSMiklos Szeredi ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file); 846eb443e5aSMiklos Szeredi mutex_unlock(&inode->i_mutex); 847eb443e5aSMiklos Szeredi } while (ret > 0); 848eb443e5aSMiklos Szeredi splice_from_pipe_end(pipe, &sd); 849eb443e5aSMiklos Szeredi 85061e0d47cSMiklos Szeredi pipe_unlock(pipe); 851eb443e5aSMiklos Szeredi 852eb443e5aSMiklos Szeredi if (sd.num_spliced) 853eb443e5aSMiklos Szeredi ret = sd.num_spliced; 854eb443e5aSMiklos Szeredi 855a4514ebdSJens Axboe if (ret > 0) { 85617ee4f49SJens Axboe unsigned long nr_pages; 85717ee4f49SJens Axboe 858a4514ebdSJens Axboe *ppos += ret; 85917ee4f49SJens Axboe nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 8604f6f0bd2SJens Axboe 8614f6f0bd2SJens Axboe /* 862a4514ebdSJens Axboe * If file or inode is SYNC and we actually wrote some data, 863a4514ebdSJens Axboe * sync it. 8644f6f0bd2SJens Axboe */ 865a4514ebdSJens Axboe if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { 8667f3d4ee1SMiklos Szeredi int err; 8677f3d4ee1SMiklos Szeredi 8684f6f0bd2SJens Axboe mutex_lock(&inode->i_mutex); 869a4514ebdSJens Axboe err = generic_osync_inode(inode, mapping, 8704f6f0bd2SJens Axboe OSYNC_METADATA|OSYNC_DATA); 8714f6f0bd2SJens Axboe mutex_unlock(&inode->i_mutex); 8724f6f0bd2SJens Axboe 8734f6f0bd2SJens Axboe if (err) 8744f6f0bd2SJens Axboe ret = err; 8754f6f0bd2SJens Axboe } 87617ee4f49SJens Axboe balance_dirty_pages_ratelimited_nr(mapping, nr_pages); 877a4514ebdSJens Axboe } 8784f6f0bd2SJens Axboe 8794f6f0bd2SJens Axboe return ret; 8805274f052SJens Axboe } 8815274f052SJens Axboe 882059a8f37SJens Axboe EXPORT_SYMBOL(generic_file_splice_write); 883059a8f37SJens Axboe 88483f9135bSJens Axboe /** 88583f9135bSJens Axboe * generic_splice_sendpage - splice data from a pipe to a socket 886932cc6d4SJens Axboe * @pipe: pipe to splice from 88783f9135bSJens Axboe * @out: socket to write to 888932cc6d4SJens Axboe * @ppos: position in @out 88983f9135bSJens Axboe * @len: number of bytes to splice 89083f9135bSJens Axboe * @flags: splice modifier flags 89183f9135bSJens Axboe * 892932cc6d4SJens Axboe * Description: 89383f9135bSJens Axboe * Will send @len bytes from the pipe to a network socket. No data copying 89483f9135bSJens Axboe * is involved. 89583f9135bSJens Axboe * 89683f9135bSJens Axboe */ 8973a326a2cSIngo Molnar ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 898cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 8995274f052SJens Axboe { 90000522fb4SJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 9015274f052SJens Axboe } 9025274f052SJens Axboe 903059a8f37SJens Axboe EXPORT_SYMBOL(generic_splice_sendpage); 904a0f06780SJeff Garzik 90583f9135bSJens Axboe /* 90683f9135bSJens Axboe * Attempt to initiate a splice from pipe to file. 90783f9135bSJens Axboe */ 9083a326a2cSIngo Molnar static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 909cbb7e577SJens Axboe loff_t *ppos, size_t len, unsigned int flags) 9105274f052SJens Axboe { 9115274f052SJens Axboe int ret; 9125274f052SJens Axboe 91349570e9bSJens Axboe if (unlikely(!out->f_op || !out->f_op->splice_write)) 9145274f052SJens Axboe return -EINVAL; 9155274f052SJens Axboe 91649570e9bSJens Axboe if (unlikely(!(out->f_mode & FMODE_WRITE))) 9175274f052SJens Axboe return -EBADF; 9185274f052SJens Axboe 919efc968d4SLinus Torvalds if (unlikely(out->f_flags & O_APPEND)) 920efc968d4SLinus Torvalds return -EINVAL; 921efc968d4SLinus Torvalds 922cbb7e577SJens Axboe ret = rw_verify_area(WRITE, out, ppos, len); 9235274f052SJens Axboe if (unlikely(ret < 0)) 9245274f052SJens Axboe return ret; 9255274f052SJens Axboe 926cbb7e577SJens Axboe return out->f_op->splice_write(pipe, out, ppos, len, flags); 9275274f052SJens Axboe } 9285274f052SJens Axboe 92983f9135bSJens Axboe /* 93083f9135bSJens Axboe * Attempt to initiate a splice from a file to a pipe. 93183f9135bSJens Axboe */ 932cbb7e577SJens Axboe static long do_splice_to(struct file *in, loff_t *ppos, 933cbb7e577SJens Axboe struct pipe_inode_info *pipe, size_t len, 934cbb7e577SJens Axboe unsigned int flags) 9355274f052SJens Axboe { 9365274f052SJens Axboe int ret; 9375274f052SJens Axboe 93849570e9bSJens Axboe if (unlikely(!in->f_op || !in->f_op->splice_read)) 9395274f052SJens Axboe return -EINVAL; 9405274f052SJens Axboe 94149570e9bSJens Axboe if (unlikely(!(in->f_mode & FMODE_READ))) 9425274f052SJens Axboe return -EBADF; 9435274f052SJens Axboe 944cbb7e577SJens Axboe ret = rw_verify_area(READ, in, ppos, len); 9455274f052SJens Axboe if (unlikely(ret < 0)) 9465274f052SJens Axboe return ret; 9475274f052SJens Axboe 948cbb7e577SJens Axboe return in->f_op->splice_read(in, ppos, pipe, len, flags); 9495274f052SJens Axboe } 9505274f052SJens Axboe 951932cc6d4SJens Axboe /** 952932cc6d4SJens Axboe * splice_direct_to_actor - splices data directly between two non-pipes 953932cc6d4SJens Axboe * @in: file to splice from 954932cc6d4SJens Axboe * @sd: actor information on where to splice to 955932cc6d4SJens Axboe * @actor: handles the data splicing 956932cc6d4SJens Axboe * 957932cc6d4SJens Axboe * Description: 958932cc6d4SJens Axboe * This is a special case helper to splice directly between two 959932cc6d4SJens Axboe * points, without requiring an explicit pipe. Internally an allocated 960932cc6d4SJens Axboe * pipe is cached in the process, and reused during the lifetime of 961932cc6d4SJens Axboe * that process. 962932cc6d4SJens Axboe * 963c66ab6faSJens Axboe */ 964c66ab6faSJens Axboe ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 965c66ab6faSJens Axboe splice_direct_actor *actor) 966b92ce558SJens Axboe { 967b92ce558SJens Axboe struct pipe_inode_info *pipe; 968b92ce558SJens Axboe long ret, bytes; 969b92ce558SJens Axboe umode_t i_mode; 970c66ab6faSJens Axboe size_t len; 971c66ab6faSJens Axboe int i, flags; 972b92ce558SJens Axboe 973b92ce558SJens Axboe /* 974b92ce558SJens Axboe * We require the input being a regular file, as we don't want to 975b92ce558SJens Axboe * randomly drop data for eg socket -> socket splicing. Use the 976b92ce558SJens Axboe * piped splicing for that! 977b92ce558SJens Axboe */ 9780f7fc9e4SJosef "Jeff" Sipek i_mode = in->f_path.dentry->d_inode->i_mode; 979b92ce558SJens Axboe if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) 980b92ce558SJens Axboe return -EINVAL; 981b92ce558SJens Axboe 982b92ce558SJens Axboe /* 983b92ce558SJens Axboe * neither in nor out is a pipe, setup an internal pipe attached to 984b92ce558SJens Axboe * 'out' and transfer the wanted data from 'in' to 'out' through that 985b92ce558SJens Axboe */ 986b92ce558SJens Axboe pipe = current->splice_pipe; 98749570e9bSJens Axboe if (unlikely(!pipe)) { 988b92ce558SJens Axboe pipe = alloc_pipe_info(NULL); 989b92ce558SJens Axboe if (!pipe) 990b92ce558SJens Axboe return -ENOMEM; 991b92ce558SJens Axboe 992b92ce558SJens Axboe /* 993b92ce558SJens Axboe * We don't have an immediate reader, but we'll read the stuff 99400522fb4SJens Axboe * out of the pipe right after the splice_to_pipe(). So set 995b92ce558SJens Axboe * PIPE_READERS appropriately. 996b92ce558SJens Axboe */ 997b92ce558SJens Axboe pipe->readers = 1; 998b92ce558SJens Axboe 999b92ce558SJens Axboe current->splice_pipe = pipe; 1000b92ce558SJens Axboe } 1001b92ce558SJens Axboe 1002b92ce558SJens Axboe /* 100373d62d83SIngo Molnar * Do the splice. 1004b92ce558SJens Axboe */ 1005b92ce558SJens Axboe ret = 0; 1006b92ce558SJens Axboe bytes = 0; 1007c66ab6faSJens Axboe len = sd->total_len; 1008c66ab6faSJens Axboe flags = sd->flags; 1009c66ab6faSJens Axboe 1010c66ab6faSJens Axboe /* 1011c66ab6faSJens Axboe * Don't block on output, we have to drain the direct pipe. 1012c66ab6faSJens Axboe */ 1013c66ab6faSJens Axboe sd->flags &= ~SPLICE_F_NONBLOCK; 1014b92ce558SJens Axboe 1015b92ce558SJens Axboe while (len) { 101651a92c0fSJens Axboe size_t read_len; 1017a82c53a0STom Zanussi loff_t pos = sd->pos, prev_pos = pos; 1018b92ce558SJens Axboe 1019bcd4f3acSJens Axboe ret = do_splice_to(in, &pos, pipe, len, flags); 102051a92c0fSJens Axboe if (unlikely(ret <= 0)) 1021b92ce558SJens Axboe goto out_release; 1022b92ce558SJens Axboe 1023b92ce558SJens Axboe read_len = ret; 1024c66ab6faSJens Axboe sd->total_len = read_len; 1025b92ce558SJens Axboe 1026b92ce558SJens Axboe /* 1027b92ce558SJens Axboe * NOTE: nonblocking mode only applies to the input. We 1028b92ce558SJens Axboe * must not do the output in nonblocking mode as then we 1029b92ce558SJens Axboe * could get stuck data in the internal pipe: 1030b92ce558SJens Axboe */ 1031c66ab6faSJens Axboe ret = actor(pipe, sd); 1032a82c53a0STom Zanussi if (unlikely(ret <= 0)) { 1033a82c53a0STom Zanussi sd->pos = prev_pos; 1034b92ce558SJens Axboe goto out_release; 1035a82c53a0STom Zanussi } 1036b92ce558SJens Axboe 1037b92ce558SJens Axboe bytes += ret; 1038b92ce558SJens Axboe len -= ret; 1039bcd4f3acSJens Axboe sd->pos = pos; 1040b92ce558SJens Axboe 1041a82c53a0STom Zanussi if (ret < read_len) { 1042a82c53a0STom Zanussi sd->pos = prev_pos + ret; 104351a92c0fSJens Axboe goto out_release; 1044b92ce558SJens Axboe } 1045a82c53a0STom Zanussi } 1046b92ce558SJens Axboe 10479e97198dSJens Axboe done: 1048b92ce558SJens Axboe pipe->nrbufs = pipe->curbuf = 0; 10499e97198dSJens Axboe file_accessed(in); 1050b92ce558SJens Axboe return bytes; 1051b92ce558SJens Axboe 1052b92ce558SJens Axboe out_release: 1053b92ce558SJens Axboe /* 1054b92ce558SJens Axboe * If we did an incomplete transfer we must release 1055b92ce558SJens Axboe * the pipe buffers in question: 1056b92ce558SJens Axboe */ 1057b92ce558SJens Axboe for (i = 0; i < PIPE_BUFFERS; i++) { 1058b92ce558SJens Axboe struct pipe_buffer *buf = pipe->bufs + i; 1059b92ce558SJens Axboe 1060b92ce558SJens Axboe if (buf->ops) { 1061b92ce558SJens Axboe buf->ops->release(pipe, buf); 1062b92ce558SJens Axboe buf->ops = NULL; 1063b92ce558SJens Axboe } 1064b92ce558SJens Axboe } 1065b92ce558SJens Axboe 10669e97198dSJens Axboe if (!bytes) 10679e97198dSJens Axboe bytes = ret; 1068b92ce558SJens Axboe 10699e97198dSJens Axboe goto done; 1070c66ab6faSJens Axboe } 1071c66ab6faSJens Axboe EXPORT_SYMBOL(splice_direct_to_actor); 1072c66ab6faSJens Axboe 1073c66ab6faSJens Axboe static int direct_splice_actor(struct pipe_inode_info *pipe, 1074c66ab6faSJens Axboe struct splice_desc *sd) 1075c66ab6faSJens Axboe { 10766a14b90bSJens Axboe struct file *file = sd->u.file; 1077c66ab6faSJens Axboe 1078c66ab6faSJens Axboe return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags); 1079c66ab6faSJens Axboe } 1080c66ab6faSJens Axboe 1081932cc6d4SJens Axboe /** 1082932cc6d4SJens Axboe * do_splice_direct - splices data directly between two files 1083932cc6d4SJens Axboe * @in: file to splice from 1084932cc6d4SJens Axboe * @ppos: input file offset 1085932cc6d4SJens Axboe * @out: file to splice to 1086932cc6d4SJens Axboe * @len: number of bytes to splice 1087932cc6d4SJens Axboe * @flags: splice modifier flags 1088932cc6d4SJens Axboe * 1089932cc6d4SJens Axboe * Description: 1090932cc6d4SJens Axboe * For use by do_sendfile(). splice can easily emulate sendfile, but 1091932cc6d4SJens Axboe * doing it in the application would incur an extra system call 1092932cc6d4SJens Axboe * (splice in + splice out, as compared to just sendfile()). So this helper 1093932cc6d4SJens Axboe * can splice directly through a process-private pipe. 1094932cc6d4SJens Axboe * 1095932cc6d4SJens Axboe */ 1096c66ab6faSJens Axboe long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 1097c66ab6faSJens Axboe size_t len, unsigned int flags) 1098c66ab6faSJens Axboe { 1099c66ab6faSJens Axboe struct splice_desc sd = { 1100c66ab6faSJens Axboe .len = len, 1101c66ab6faSJens Axboe .total_len = len, 1102c66ab6faSJens Axboe .flags = flags, 1103c66ab6faSJens Axboe .pos = *ppos, 11046a14b90bSJens Axboe .u.file = out, 1105c66ab6faSJens Axboe }; 110651a92c0fSJens Axboe long ret; 1107c66ab6faSJens Axboe 1108c66ab6faSJens Axboe ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 110951a92c0fSJens Axboe if (ret > 0) 1110a82c53a0STom Zanussi *ppos = sd.pos; 111151a92c0fSJens Axboe 1112c66ab6faSJens Axboe return ret; 1113b92ce558SJens Axboe } 1114b92ce558SJens Axboe 11157c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 11167c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 11177c77f0b3SMiklos Szeredi size_t len, unsigned int flags); 111883f9135bSJens Axboe /* 1119ddac0d39SJens Axboe * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same 1120ddac0d39SJens Axboe * location, so checking ->i_pipe is not enough to verify that this is a 1121ddac0d39SJens Axboe * pipe. 1122ddac0d39SJens Axboe */ 1123ddac0d39SJens Axboe static inline struct pipe_inode_info *pipe_info(struct inode *inode) 1124ddac0d39SJens Axboe { 1125ddac0d39SJens Axboe if (S_ISFIFO(inode->i_mode)) 1126ddac0d39SJens Axboe return inode->i_pipe; 1127ddac0d39SJens Axboe 1128ddac0d39SJens Axboe return NULL; 1129ddac0d39SJens Axboe } 1130ddac0d39SJens Axboe 1131ddac0d39SJens Axboe /* 113283f9135bSJens Axboe * Determine where to splice to/from. 113383f9135bSJens Axboe */ 1134529565dcSIngo Molnar static long do_splice(struct file *in, loff_t __user *off_in, 1135529565dcSIngo Molnar struct file *out, loff_t __user *off_out, 1136529565dcSIngo Molnar size_t len, unsigned int flags) 11375274f052SJens Axboe { 11387c77f0b3SMiklos Szeredi struct pipe_inode_info *ipipe; 11397c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe; 1140cbb7e577SJens Axboe loff_t offset, *off; 1141a4514ebdSJens Axboe long ret; 11425274f052SJens Axboe 11437c77f0b3SMiklos Szeredi ipipe = pipe_info(in->f_path.dentry->d_inode); 11447c77f0b3SMiklos Szeredi opipe = pipe_info(out->f_path.dentry->d_inode); 11457c77f0b3SMiklos Szeredi 11467c77f0b3SMiklos Szeredi if (ipipe && opipe) { 11477c77f0b3SMiklos Szeredi if (off_in || off_out) 11487c77f0b3SMiklos Szeredi return -ESPIPE; 11497c77f0b3SMiklos Szeredi 11507c77f0b3SMiklos Szeredi if (!(in->f_mode & FMODE_READ)) 11517c77f0b3SMiklos Szeredi return -EBADF; 11527c77f0b3SMiklos Szeredi 11537c77f0b3SMiklos Szeredi if (!(out->f_mode & FMODE_WRITE)) 11547c77f0b3SMiklos Szeredi return -EBADF; 11557c77f0b3SMiklos Szeredi 11567c77f0b3SMiklos Szeredi /* Splicing to self would be fun, but... */ 11577c77f0b3SMiklos Szeredi if (ipipe == opipe) 11587c77f0b3SMiklos Szeredi return -EINVAL; 11597c77f0b3SMiklos Szeredi 11607c77f0b3SMiklos Szeredi return splice_pipe_to_pipe(ipipe, opipe, len, flags); 11617c77f0b3SMiklos Szeredi } 11627c77f0b3SMiklos Szeredi 11637c77f0b3SMiklos Szeredi if (ipipe) { 1164529565dcSIngo Molnar if (off_in) 1165529565dcSIngo Molnar return -ESPIPE; 1166b92ce558SJens Axboe if (off_out) { 1167b92ce558SJens Axboe if (out->f_op->llseek == no_llseek) 1168b92ce558SJens Axboe return -EINVAL; 1169cbb7e577SJens Axboe if (copy_from_user(&offset, off_out, sizeof(loff_t))) 1170b92ce558SJens Axboe return -EFAULT; 1171cbb7e577SJens Axboe off = &offset; 1172cbb7e577SJens Axboe } else 1173cbb7e577SJens Axboe off = &out->f_pos; 1174529565dcSIngo Molnar 11757c77f0b3SMiklos Szeredi ret = do_splice_from(ipipe, out, off, len, flags); 1176a4514ebdSJens Axboe 1177a4514ebdSJens Axboe if (off_out && copy_to_user(off_out, off, sizeof(loff_t))) 1178a4514ebdSJens Axboe ret = -EFAULT; 1179a4514ebdSJens Axboe 1180a4514ebdSJens Axboe return ret; 1181529565dcSIngo Molnar } 11825274f052SJens Axboe 11837c77f0b3SMiklos Szeredi if (opipe) { 1184529565dcSIngo Molnar if (off_out) 1185529565dcSIngo Molnar return -ESPIPE; 1186b92ce558SJens Axboe if (off_in) { 1187b92ce558SJens Axboe if (in->f_op->llseek == no_llseek) 1188b92ce558SJens Axboe return -EINVAL; 1189cbb7e577SJens Axboe if (copy_from_user(&offset, off_in, sizeof(loff_t))) 1190b92ce558SJens Axboe return -EFAULT; 1191cbb7e577SJens Axboe off = &offset; 1192cbb7e577SJens Axboe } else 1193cbb7e577SJens Axboe off = &in->f_pos; 1194529565dcSIngo Molnar 11957c77f0b3SMiklos Szeredi ret = do_splice_to(in, off, opipe, len, flags); 1196a4514ebdSJens Axboe 1197a4514ebdSJens Axboe if (off_in && copy_to_user(off_in, off, sizeof(loff_t))) 1198a4514ebdSJens Axboe ret = -EFAULT; 1199a4514ebdSJens Axboe 1200a4514ebdSJens Axboe return ret; 1201529565dcSIngo Molnar } 12025274f052SJens Axboe 12035274f052SJens Axboe return -EINVAL; 12045274f052SJens Axboe } 12055274f052SJens Axboe 1206912d35f8SJens Axboe /* 1207912d35f8SJens Axboe * Map an iov into an array of pages and offset/length tupples. With the 1208912d35f8SJens Axboe * partial_page structure, we can map several non-contiguous ranges into 1209912d35f8SJens Axboe * our ones pages[] map instead of splitting that operation into pieces. 1210912d35f8SJens Axboe * Could easily be exported as a generic helper for other users, in which 1211912d35f8SJens Axboe * case one would probably want to add a 'max_nr_pages' parameter as well. 1212912d35f8SJens Axboe */ 1213912d35f8SJens Axboe static int get_iovec_page_array(const struct iovec __user *iov, 1214912d35f8SJens Axboe unsigned int nr_vecs, struct page **pages, 12157afa6fd0SJens Axboe struct partial_page *partial, int aligned) 1216912d35f8SJens Axboe { 1217912d35f8SJens Axboe int buffers = 0, error = 0; 1218912d35f8SJens Axboe 1219912d35f8SJens Axboe while (nr_vecs) { 1220912d35f8SJens Axboe unsigned long off, npages; 122175723957SLinus Torvalds struct iovec entry; 1222912d35f8SJens Axboe void __user *base; 1223912d35f8SJens Axboe size_t len; 1224912d35f8SJens Axboe int i; 1225912d35f8SJens Axboe 122675723957SLinus Torvalds error = -EFAULT; 1227bc40d73cSNick Piggin if (copy_from_user(&entry, iov, sizeof(entry))) 1228912d35f8SJens Axboe break; 122975723957SLinus Torvalds 123075723957SLinus Torvalds base = entry.iov_base; 123175723957SLinus Torvalds len = entry.iov_len; 1232912d35f8SJens Axboe 1233912d35f8SJens Axboe /* 1234912d35f8SJens Axboe * Sanity check this iovec. 0 read succeeds. 1235912d35f8SJens Axboe */ 123675723957SLinus Torvalds error = 0; 1237912d35f8SJens Axboe if (unlikely(!len)) 1238912d35f8SJens Axboe break; 1239912d35f8SJens Axboe error = -EFAULT; 1240712a30e6SBastian Blank if (!access_ok(VERIFY_READ, base, len)) 1241912d35f8SJens Axboe break; 1242912d35f8SJens Axboe 1243912d35f8SJens Axboe /* 1244912d35f8SJens Axboe * Get this base offset and number of pages, then map 1245912d35f8SJens Axboe * in the user pages. 1246912d35f8SJens Axboe */ 1247912d35f8SJens Axboe off = (unsigned long) base & ~PAGE_MASK; 12487afa6fd0SJens Axboe 12497afa6fd0SJens Axboe /* 12507afa6fd0SJens Axboe * If asked for alignment, the offset must be zero and the 12517afa6fd0SJens Axboe * length a multiple of the PAGE_SIZE. 12527afa6fd0SJens Axboe */ 12537afa6fd0SJens Axboe error = -EINVAL; 12547afa6fd0SJens Axboe if (aligned && (off || len & ~PAGE_MASK)) 12557afa6fd0SJens Axboe break; 12567afa6fd0SJens Axboe 1257912d35f8SJens Axboe npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1258912d35f8SJens Axboe if (npages > PIPE_BUFFERS - buffers) 1259912d35f8SJens Axboe npages = PIPE_BUFFERS - buffers; 1260912d35f8SJens Axboe 1261bc40d73cSNick Piggin error = get_user_pages_fast((unsigned long)base, npages, 1262bc40d73cSNick Piggin 0, &pages[buffers]); 1263912d35f8SJens Axboe 1264912d35f8SJens Axboe if (unlikely(error <= 0)) 1265912d35f8SJens Axboe break; 1266912d35f8SJens Axboe 1267912d35f8SJens Axboe /* 1268912d35f8SJens Axboe * Fill this contiguous range into the partial page map. 1269912d35f8SJens Axboe */ 1270912d35f8SJens Axboe for (i = 0; i < error; i++) { 12717591489aSJens Axboe const int plen = min_t(size_t, len, PAGE_SIZE - off); 1272912d35f8SJens Axboe 1273912d35f8SJens Axboe partial[buffers].offset = off; 1274912d35f8SJens Axboe partial[buffers].len = plen; 1275912d35f8SJens Axboe 1276912d35f8SJens Axboe off = 0; 1277912d35f8SJens Axboe len -= plen; 1278912d35f8SJens Axboe buffers++; 1279912d35f8SJens Axboe } 1280912d35f8SJens Axboe 1281912d35f8SJens Axboe /* 1282912d35f8SJens Axboe * We didn't complete this iov, stop here since it probably 1283912d35f8SJens Axboe * means we have to move some of this into a pipe to 1284912d35f8SJens Axboe * be able to continue. 1285912d35f8SJens Axboe */ 1286912d35f8SJens Axboe if (len) 1287912d35f8SJens Axboe break; 1288912d35f8SJens Axboe 1289912d35f8SJens Axboe /* 1290912d35f8SJens Axboe * Don't continue if we mapped fewer pages than we asked for, 1291912d35f8SJens Axboe * or if we mapped the max number of pages that we have 1292912d35f8SJens Axboe * room for. 1293912d35f8SJens Axboe */ 1294912d35f8SJens Axboe if (error < npages || buffers == PIPE_BUFFERS) 1295912d35f8SJens Axboe break; 1296912d35f8SJens Axboe 1297912d35f8SJens Axboe nr_vecs--; 1298912d35f8SJens Axboe iov++; 1299912d35f8SJens Axboe } 1300912d35f8SJens Axboe 1301912d35f8SJens Axboe if (buffers) 1302912d35f8SJens Axboe return buffers; 1303912d35f8SJens Axboe 1304912d35f8SJens Axboe return error; 1305912d35f8SJens Axboe } 1306912d35f8SJens Axboe 13076a14b90bSJens Axboe static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 13086a14b90bSJens Axboe struct splice_desc *sd) 13096a14b90bSJens Axboe { 13106a14b90bSJens Axboe char *src; 13116a14b90bSJens Axboe int ret; 13126a14b90bSJens Axboe 1313cac36bb0SJens Axboe ret = buf->ops->confirm(pipe, buf); 13146a14b90bSJens Axboe if (unlikely(ret)) 13156a14b90bSJens Axboe return ret; 13166a14b90bSJens Axboe 13176a14b90bSJens Axboe /* 13186a14b90bSJens Axboe * See if we can use the atomic maps, by prefaulting in the 13196a14b90bSJens Axboe * pages and doing an atomic copy 13206a14b90bSJens Axboe */ 13216a14b90bSJens Axboe if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) { 13226a14b90bSJens Axboe src = buf->ops->map(pipe, buf, 1); 13236a14b90bSJens Axboe ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset, 13246a14b90bSJens Axboe sd->len); 13256a14b90bSJens Axboe buf->ops->unmap(pipe, buf, src); 13266a14b90bSJens Axboe if (!ret) { 13276a14b90bSJens Axboe ret = sd->len; 13286a14b90bSJens Axboe goto out; 13296a14b90bSJens Axboe } 13306a14b90bSJens Axboe } 13316a14b90bSJens Axboe 13326a14b90bSJens Axboe /* 13336a14b90bSJens Axboe * No dice, use slow non-atomic map and copy 13346a14b90bSJens Axboe */ 13356a14b90bSJens Axboe src = buf->ops->map(pipe, buf, 0); 13366a14b90bSJens Axboe 13376a14b90bSJens Axboe ret = sd->len; 13386a14b90bSJens Axboe if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len)) 13396a14b90bSJens Axboe ret = -EFAULT; 13406a14b90bSJens Axboe 13416866bef4SJens Axboe buf->ops->unmap(pipe, buf, src); 13426a14b90bSJens Axboe out: 13436a14b90bSJens Axboe if (ret > 0) 13446a14b90bSJens Axboe sd->u.userptr += ret; 13456a14b90bSJens Axboe return ret; 13466a14b90bSJens Axboe } 13476a14b90bSJens Axboe 13486a14b90bSJens Axboe /* 13496a14b90bSJens Axboe * For lack of a better implementation, implement vmsplice() to userspace 13506a14b90bSJens Axboe * as a simple copy of the pipes pages to the user iov. 13516a14b90bSJens Axboe */ 13526a14b90bSJens Axboe static long vmsplice_to_user(struct file *file, const struct iovec __user *iov, 13536a14b90bSJens Axboe unsigned long nr_segs, unsigned int flags) 13546a14b90bSJens Axboe { 13556a14b90bSJens Axboe struct pipe_inode_info *pipe; 13566a14b90bSJens Axboe struct splice_desc sd; 13576a14b90bSJens Axboe ssize_t size; 13586a14b90bSJens Axboe int error; 13596a14b90bSJens Axboe long ret; 13606a14b90bSJens Axboe 13616a14b90bSJens Axboe pipe = pipe_info(file->f_path.dentry->d_inode); 13626a14b90bSJens Axboe if (!pipe) 13636a14b90bSJens Axboe return -EBADF; 13646a14b90bSJens Axboe 136561e0d47cSMiklos Szeredi pipe_lock(pipe); 13666a14b90bSJens Axboe 13676a14b90bSJens Axboe error = ret = 0; 13686a14b90bSJens Axboe while (nr_segs) { 13696a14b90bSJens Axboe void __user *base; 13706a14b90bSJens Axboe size_t len; 13716a14b90bSJens Axboe 13726a14b90bSJens Axboe /* 13736a14b90bSJens Axboe * Get user address base and length for this iovec. 13746a14b90bSJens Axboe */ 13756a14b90bSJens Axboe error = get_user(base, &iov->iov_base); 13766a14b90bSJens Axboe if (unlikely(error)) 13776a14b90bSJens Axboe break; 13786a14b90bSJens Axboe error = get_user(len, &iov->iov_len); 13796a14b90bSJens Axboe if (unlikely(error)) 13806a14b90bSJens Axboe break; 13816a14b90bSJens Axboe 13826a14b90bSJens Axboe /* 13836a14b90bSJens Axboe * Sanity check this iovec. 0 read succeeds. 13846a14b90bSJens Axboe */ 13856a14b90bSJens Axboe if (unlikely(!len)) 13866a14b90bSJens Axboe break; 13876a14b90bSJens Axboe if (unlikely(!base)) { 13886a14b90bSJens Axboe error = -EFAULT; 13896a14b90bSJens Axboe break; 13906a14b90bSJens Axboe } 13916a14b90bSJens Axboe 13928811930dSJens Axboe if (unlikely(!access_ok(VERIFY_WRITE, base, len))) { 13938811930dSJens Axboe error = -EFAULT; 13948811930dSJens Axboe break; 13958811930dSJens Axboe } 13968811930dSJens Axboe 13976a14b90bSJens Axboe sd.len = 0; 13986a14b90bSJens Axboe sd.total_len = len; 13996a14b90bSJens Axboe sd.flags = flags; 14006a14b90bSJens Axboe sd.u.userptr = base; 14016a14b90bSJens Axboe sd.pos = 0; 14026a14b90bSJens Axboe 14036a14b90bSJens Axboe size = __splice_from_pipe(pipe, &sd, pipe_to_user); 14046a14b90bSJens Axboe if (size < 0) { 14056a14b90bSJens Axboe if (!ret) 14066a14b90bSJens Axboe ret = size; 14076a14b90bSJens Axboe 14086a14b90bSJens Axboe break; 14096a14b90bSJens Axboe } 14106a14b90bSJens Axboe 14116a14b90bSJens Axboe ret += size; 14126a14b90bSJens Axboe 14136a14b90bSJens Axboe if (size < len) 14146a14b90bSJens Axboe break; 14156a14b90bSJens Axboe 14166a14b90bSJens Axboe nr_segs--; 14176a14b90bSJens Axboe iov++; 14186a14b90bSJens Axboe } 14196a14b90bSJens Axboe 142061e0d47cSMiklos Szeredi pipe_unlock(pipe); 14216a14b90bSJens Axboe 14226a14b90bSJens Axboe if (!ret) 14236a14b90bSJens Axboe ret = error; 14246a14b90bSJens Axboe 14256a14b90bSJens Axboe return ret; 14266a14b90bSJens Axboe } 14276a14b90bSJens Axboe 1428912d35f8SJens Axboe /* 1429912d35f8SJens Axboe * vmsplice splices a user address range into a pipe. It can be thought of 1430912d35f8SJens Axboe * as splice-from-memory, where the regular splice is splice-from-file (or 1431912d35f8SJens Axboe * to file). In both cases the output is a pipe, naturally. 1432912d35f8SJens Axboe */ 14336a14b90bSJens Axboe static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov, 1434912d35f8SJens Axboe unsigned long nr_segs, unsigned int flags) 1435912d35f8SJens Axboe { 1436ddac0d39SJens Axboe struct pipe_inode_info *pipe; 1437912d35f8SJens Axboe struct page *pages[PIPE_BUFFERS]; 1438912d35f8SJens Axboe struct partial_page partial[PIPE_BUFFERS]; 1439912d35f8SJens Axboe struct splice_pipe_desc spd = { 1440912d35f8SJens Axboe .pages = pages, 1441912d35f8SJens Axboe .partial = partial, 1442912d35f8SJens Axboe .flags = flags, 1443912d35f8SJens Axboe .ops = &user_page_pipe_buf_ops, 1444bbdfc2f7SJens Axboe .spd_release = spd_release_page, 1445912d35f8SJens Axboe }; 1446912d35f8SJens Axboe 14470f7fc9e4SJosef "Jeff" Sipek pipe = pipe_info(file->f_path.dentry->d_inode); 1448ddac0d39SJens Axboe if (!pipe) 1449912d35f8SJens Axboe return -EBADF; 1450912d35f8SJens Axboe 14517afa6fd0SJens Axboe spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial, 14527afa6fd0SJens Axboe flags & SPLICE_F_GIFT); 1453912d35f8SJens Axboe if (spd.nr_pages <= 0) 1454912d35f8SJens Axboe return spd.nr_pages; 1455912d35f8SJens Axboe 145600522fb4SJens Axboe return splice_to_pipe(pipe, &spd); 1457912d35f8SJens Axboe } 1458912d35f8SJens Axboe 14596a14b90bSJens Axboe /* 14606a14b90bSJens Axboe * Note that vmsplice only really supports true splicing _from_ user memory 14616a14b90bSJens Axboe * to a pipe, not the other way around. Splicing from user memory is a simple 14626a14b90bSJens Axboe * operation that can be supported without any funky alignment restrictions 14636a14b90bSJens Axboe * or nasty vm tricks. We simply map in the user memory and fill them into 14646a14b90bSJens Axboe * a pipe. The reverse isn't quite as easy, though. There are two possible 14656a14b90bSJens Axboe * solutions for that: 14666a14b90bSJens Axboe * 14676a14b90bSJens Axboe * - memcpy() the data internally, at which point we might as well just 14686a14b90bSJens Axboe * do a regular read() on the buffer anyway. 14696a14b90bSJens Axboe * - Lots of nasty vm tricks, that are neither fast nor flexible (it 14706a14b90bSJens Axboe * has restriction limitations on both ends of the pipe). 14716a14b90bSJens Axboe * 14726a14b90bSJens Axboe * Currently we punt and implement it as a normal copy, see pipe_to_user(). 14736a14b90bSJens Axboe * 14746a14b90bSJens Axboe */ 1475836f92adSHeiko Carstens SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, 1476836f92adSHeiko Carstens unsigned long, nr_segs, unsigned int, flags) 1477912d35f8SJens Axboe { 1478912d35f8SJens Axboe struct file *file; 1479912d35f8SJens Axboe long error; 1480912d35f8SJens Axboe int fput; 1481912d35f8SJens Axboe 14826a14b90bSJens Axboe if (unlikely(nr_segs > UIO_MAXIOV)) 14836a14b90bSJens Axboe return -EINVAL; 14846a14b90bSJens Axboe else if (unlikely(!nr_segs)) 14856a14b90bSJens Axboe return 0; 14866a14b90bSJens Axboe 1487912d35f8SJens Axboe error = -EBADF; 1488912d35f8SJens Axboe file = fget_light(fd, &fput); 1489912d35f8SJens Axboe if (file) { 1490912d35f8SJens Axboe if (file->f_mode & FMODE_WRITE) 14916a14b90bSJens Axboe error = vmsplice_to_pipe(file, iov, nr_segs, flags); 14926a14b90bSJens Axboe else if (file->f_mode & FMODE_READ) 14936a14b90bSJens Axboe error = vmsplice_to_user(file, iov, nr_segs, flags); 1494912d35f8SJens Axboe 1495912d35f8SJens Axboe fput_light(file, fput); 1496912d35f8SJens Axboe } 1497912d35f8SJens Axboe 1498912d35f8SJens Axboe return error; 1499912d35f8SJens Axboe } 1500912d35f8SJens Axboe 1501836f92adSHeiko Carstens SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 1502836f92adSHeiko Carstens int, fd_out, loff_t __user *, off_out, 1503836f92adSHeiko Carstens size_t, len, unsigned int, flags) 15045274f052SJens Axboe { 15055274f052SJens Axboe long error; 15065274f052SJens Axboe struct file *in, *out; 15075274f052SJens Axboe int fput_in, fput_out; 15085274f052SJens Axboe 15095274f052SJens Axboe if (unlikely(!len)) 15105274f052SJens Axboe return 0; 15115274f052SJens Axboe 15125274f052SJens Axboe error = -EBADF; 1513529565dcSIngo Molnar in = fget_light(fd_in, &fput_in); 15145274f052SJens Axboe if (in) { 15155274f052SJens Axboe if (in->f_mode & FMODE_READ) { 1516529565dcSIngo Molnar out = fget_light(fd_out, &fput_out); 15175274f052SJens Axboe if (out) { 15185274f052SJens Axboe if (out->f_mode & FMODE_WRITE) 1519529565dcSIngo Molnar error = do_splice(in, off_in, 1520529565dcSIngo Molnar out, off_out, 1521529565dcSIngo Molnar len, flags); 15225274f052SJens Axboe fput_light(out, fput_out); 15235274f052SJens Axboe } 15245274f052SJens Axboe } 15255274f052SJens Axboe 15265274f052SJens Axboe fput_light(in, fput_in); 15275274f052SJens Axboe } 15285274f052SJens Axboe 15295274f052SJens Axboe return error; 15305274f052SJens Axboe } 153170524490SJens Axboe 153270524490SJens Axboe /* 1533aadd06e5SJens Axboe * Make sure there's data to read. Wait for input if we can, otherwise 1534aadd06e5SJens Axboe * return an appropriate error. 1535aadd06e5SJens Axboe */ 15367c77f0b3SMiklos Szeredi static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1537aadd06e5SJens Axboe { 1538aadd06e5SJens Axboe int ret; 1539aadd06e5SJens Axboe 1540aadd06e5SJens Axboe /* 1541aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1542aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1543aadd06e5SJens Axboe */ 1544aadd06e5SJens Axboe if (pipe->nrbufs) 1545aadd06e5SJens Axboe return 0; 1546aadd06e5SJens Axboe 1547aadd06e5SJens Axboe ret = 0; 154861e0d47cSMiklos Szeredi pipe_lock(pipe); 1549aadd06e5SJens Axboe 1550aadd06e5SJens Axboe while (!pipe->nrbufs) { 1551aadd06e5SJens Axboe if (signal_pending(current)) { 1552aadd06e5SJens Axboe ret = -ERESTARTSYS; 1553aadd06e5SJens Axboe break; 1554aadd06e5SJens Axboe } 1555aadd06e5SJens Axboe if (!pipe->writers) 1556aadd06e5SJens Axboe break; 1557aadd06e5SJens Axboe if (!pipe->waiting_writers) { 1558aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1559aadd06e5SJens Axboe ret = -EAGAIN; 1560aadd06e5SJens Axboe break; 1561aadd06e5SJens Axboe } 1562aadd06e5SJens Axboe } 1563aadd06e5SJens Axboe pipe_wait(pipe); 1564aadd06e5SJens Axboe } 1565aadd06e5SJens Axboe 156661e0d47cSMiklos Szeredi pipe_unlock(pipe); 1567aadd06e5SJens Axboe return ret; 1568aadd06e5SJens Axboe } 1569aadd06e5SJens Axboe 1570aadd06e5SJens Axboe /* 1571aadd06e5SJens Axboe * Make sure there's writeable room. Wait for room if we can, otherwise 1572aadd06e5SJens Axboe * return an appropriate error. 1573aadd06e5SJens Axboe */ 15747c77f0b3SMiklos Szeredi static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 1575aadd06e5SJens Axboe { 1576aadd06e5SJens Axboe int ret; 1577aadd06e5SJens Axboe 1578aadd06e5SJens Axboe /* 1579aadd06e5SJens Axboe * Check ->nrbufs without the inode lock first. This function 1580aadd06e5SJens Axboe * is speculative anyways, so missing one is ok. 1581aadd06e5SJens Axboe */ 1582aadd06e5SJens Axboe if (pipe->nrbufs < PIPE_BUFFERS) 1583aadd06e5SJens Axboe return 0; 1584aadd06e5SJens Axboe 1585aadd06e5SJens Axboe ret = 0; 158661e0d47cSMiklos Szeredi pipe_lock(pipe); 1587aadd06e5SJens Axboe 1588aadd06e5SJens Axboe while (pipe->nrbufs >= PIPE_BUFFERS) { 1589aadd06e5SJens Axboe if (!pipe->readers) { 1590aadd06e5SJens Axboe send_sig(SIGPIPE, current, 0); 1591aadd06e5SJens Axboe ret = -EPIPE; 1592aadd06e5SJens Axboe break; 1593aadd06e5SJens Axboe } 1594aadd06e5SJens Axboe if (flags & SPLICE_F_NONBLOCK) { 1595aadd06e5SJens Axboe ret = -EAGAIN; 1596aadd06e5SJens Axboe break; 1597aadd06e5SJens Axboe } 1598aadd06e5SJens Axboe if (signal_pending(current)) { 1599aadd06e5SJens Axboe ret = -ERESTARTSYS; 1600aadd06e5SJens Axboe break; 1601aadd06e5SJens Axboe } 1602aadd06e5SJens Axboe pipe->waiting_writers++; 1603aadd06e5SJens Axboe pipe_wait(pipe); 1604aadd06e5SJens Axboe pipe->waiting_writers--; 1605aadd06e5SJens Axboe } 1606aadd06e5SJens Axboe 160761e0d47cSMiklos Szeredi pipe_unlock(pipe); 1608aadd06e5SJens Axboe return ret; 1609aadd06e5SJens Axboe } 1610aadd06e5SJens Axboe 1611aadd06e5SJens Axboe /* 16127c77f0b3SMiklos Szeredi * Splice contents of ipipe to opipe. 16137c77f0b3SMiklos Szeredi */ 16147c77f0b3SMiklos Szeredi static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 16157c77f0b3SMiklos Szeredi struct pipe_inode_info *opipe, 16167c77f0b3SMiklos Szeredi size_t len, unsigned int flags) 16177c77f0b3SMiklos Szeredi { 16187c77f0b3SMiklos Szeredi struct pipe_buffer *ibuf, *obuf; 16197c77f0b3SMiklos Szeredi int ret = 0, nbuf; 16207c77f0b3SMiklos Szeredi bool input_wakeup = false; 16217c77f0b3SMiklos Szeredi 16227c77f0b3SMiklos Szeredi 16237c77f0b3SMiklos Szeredi retry: 16247c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 16257c77f0b3SMiklos Szeredi if (ret) 16267c77f0b3SMiklos Szeredi return ret; 16277c77f0b3SMiklos Szeredi 16287c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 16297c77f0b3SMiklos Szeredi if (ret) 16307c77f0b3SMiklos Szeredi return ret; 16317c77f0b3SMiklos Szeredi 16327c77f0b3SMiklos Szeredi /* 16337c77f0b3SMiklos Szeredi * Potential ABBA deadlock, work around it by ordering lock 16347c77f0b3SMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 16357c77f0b3SMiklos Szeredi * could deadlock (one doing tee from A -> B, the other from B -> A). 16367c77f0b3SMiklos Szeredi */ 16377c77f0b3SMiklos Szeredi pipe_double_lock(ipipe, opipe); 16387c77f0b3SMiklos Szeredi 16397c77f0b3SMiklos Szeredi do { 16407c77f0b3SMiklos Szeredi if (!opipe->readers) { 16417c77f0b3SMiklos Szeredi send_sig(SIGPIPE, current, 0); 16427c77f0b3SMiklos Szeredi if (!ret) 16437c77f0b3SMiklos Szeredi ret = -EPIPE; 16447c77f0b3SMiklos Szeredi break; 16457c77f0b3SMiklos Szeredi } 16467c77f0b3SMiklos Szeredi 16477c77f0b3SMiklos Szeredi if (!ipipe->nrbufs && !ipipe->writers) 16487c77f0b3SMiklos Szeredi break; 16497c77f0b3SMiklos Szeredi 16507c77f0b3SMiklos Szeredi /* 16517c77f0b3SMiklos Szeredi * Cannot make any progress, because either the input 16527c77f0b3SMiklos Szeredi * pipe is empty or the output pipe is full. 16537c77f0b3SMiklos Szeredi */ 16547c77f0b3SMiklos Szeredi if (!ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) { 16557c77f0b3SMiklos Szeredi /* Already processed some buffers, break */ 16567c77f0b3SMiklos Szeredi if (ret) 16577c77f0b3SMiklos Szeredi break; 16587c77f0b3SMiklos Szeredi 16597c77f0b3SMiklos Szeredi if (flags & SPLICE_F_NONBLOCK) { 16607c77f0b3SMiklos Szeredi ret = -EAGAIN; 16617c77f0b3SMiklos Szeredi break; 16627c77f0b3SMiklos Szeredi } 16637c77f0b3SMiklos Szeredi 16647c77f0b3SMiklos Szeredi /* 16657c77f0b3SMiklos Szeredi * We raced with another reader/writer and haven't 16667c77f0b3SMiklos Szeredi * managed to process any buffers. A zero return 16677c77f0b3SMiklos Szeredi * value means EOF, so retry instead. 16687c77f0b3SMiklos Szeredi */ 16697c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 16707c77f0b3SMiklos Szeredi pipe_unlock(opipe); 16717c77f0b3SMiklos Szeredi goto retry; 16727c77f0b3SMiklos Szeredi } 16737c77f0b3SMiklos Szeredi 16747c77f0b3SMiklos Szeredi ibuf = ipipe->bufs + ipipe->curbuf; 16757c77f0b3SMiklos Szeredi nbuf = (opipe->curbuf + opipe->nrbufs) % PIPE_BUFFERS; 16767c77f0b3SMiklos Szeredi obuf = opipe->bufs + nbuf; 16777c77f0b3SMiklos Szeredi 16787c77f0b3SMiklos Szeredi if (len >= ibuf->len) { 16797c77f0b3SMiklos Szeredi /* 16807c77f0b3SMiklos Szeredi * Simply move the whole buffer from ipipe to opipe 16817c77f0b3SMiklos Szeredi */ 16827c77f0b3SMiklos Szeredi *obuf = *ibuf; 16837c77f0b3SMiklos Szeredi ibuf->ops = NULL; 16847c77f0b3SMiklos Szeredi opipe->nrbufs++; 16857c77f0b3SMiklos Szeredi ipipe->curbuf = (ipipe->curbuf + 1) % PIPE_BUFFERS; 16867c77f0b3SMiklos Szeredi ipipe->nrbufs--; 16877c77f0b3SMiklos Szeredi input_wakeup = true; 16887c77f0b3SMiklos Szeredi } else { 16897c77f0b3SMiklos Szeredi /* 16907c77f0b3SMiklos Szeredi * Get a reference to this pipe buffer, 16917c77f0b3SMiklos Szeredi * so we can copy the contents over. 16927c77f0b3SMiklos Szeredi */ 16937c77f0b3SMiklos Szeredi ibuf->ops->get(ipipe, ibuf); 16947c77f0b3SMiklos Szeredi *obuf = *ibuf; 16957c77f0b3SMiklos Szeredi 16967c77f0b3SMiklos Szeredi /* 16977c77f0b3SMiklos Szeredi * Don't inherit the gift flag, we need to 16987c77f0b3SMiklos Szeredi * prevent multiple steals of this page. 16997c77f0b3SMiklos Szeredi */ 17007c77f0b3SMiklos Szeredi obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 17017c77f0b3SMiklos Szeredi 17027c77f0b3SMiklos Szeredi obuf->len = len; 17037c77f0b3SMiklos Szeredi opipe->nrbufs++; 17047c77f0b3SMiklos Szeredi ibuf->offset += obuf->len; 17057c77f0b3SMiklos Szeredi ibuf->len -= obuf->len; 17067c77f0b3SMiklos Szeredi } 17077c77f0b3SMiklos Szeredi ret += obuf->len; 17087c77f0b3SMiklos Szeredi len -= obuf->len; 17097c77f0b3SMiklos Szeredi } while (len); 17107c77f0b3SMiklos Szeredi 17117c77f0b3SMiklos Szeredi pipe_unlock(ipipe); 17127c77f0b3SMiklos Szeredi pipe_unlock(opipe); 17137c77f0b3SMiklos Szeredi 17147c77f0b3SMiklos Szeredi /* 17157c77f0b3SMiklos Szeredi * If we put data in the output pipe, wakeup any potential readers. 17167c77f0b3SMiklos Szeredi */ 17177c77f0b3SMiklos Szeredi if (ret > 0) { 17187c77f0b3SMiklos Szeredi smp_mb(); 17197c77f0b3SMiklos Szeredi if (waitqueue_active(&opipe->wait)) 17207c77f0b3SMiklos Szeredi wake_up_interruptible(&opipe->wait); 17217c77f0b3SMiklos Szeredi kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); 17227c77f0b3SMiklos Szeredi } 17237c77f0b3SMiklos Szeredi if (input_wakeup) 17247c77f0b3SMiklos Szeredi wakeup_pipe_writers(ipipe); 17257c77f0b3SMiklos Szeredi 17267c77f0b3SMiklos Szeredi return ret; 17277c77f0b3SMiklos Szeredi } 17287c77f0b3SMiklos Szeredi 17297c77f0b3SMiklos Szeredi /* 173070524490SJens Axboe * Link contents of ipipe to opipe. 173170524490SJens Axboe */ 173270524490SJens Axboe static int link_pipe(struct pipe_inode_info *ipipe, 173370524490SJens Axboe struct pipe_inode_info *opipe, 173470524490SJens Axboe size_t len, unsigned int flags) 173570524490SJens Axboe { 173670524490SJens Axboe struct pipe_buffer *ibuf, *obuf; 1737aadd06e5SJens Axboe int ret = 0, i = 0, nbuf; 173870524490SJens Axboe 173970524490SJens Axboe /* 174070524490SJens Axboe * Potential ABBA deadlock, work around it by ordering lock 174161e0d47cSMiklos Szeredi * grabbing by pipe info address. Otherwise two different processes 174270524490SJens Axboe * could deadlock (one doing tee from A -> B, the other from B -> A). 174370524490SJens Axboe */ 174461e0d47cSMiklos Szeredi pipe_double_lock(ipipe, opipe); 174570524490SJens Axboe 1746aadd06e5SJens Axboe do { 174770524490SJens Axboe if (!opipe->readers) { 174870524490SJens Axboe send_sig(SIGPIPE, current, 0); 174970524490SJens Axboe if (!ret) 175070524490SJens Axboe ret = -EPIPE; 175170524490SJens Axboe break; 175270524490SJens Axboe } 175370524490SJens Axboe 175470524490SJens Axboe /* 1755aadd06e5SJens Axboe * If we have iterated all input buffers or ran out of 1756aadd06e5SJens Axboe * output room, break. 175770524490SJens Axboe */ 1758aadd06e5SJens Axboe if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) 1759aadd06e5SJens Axboe break; 1760aadd06e5SJens Axboe 1761aadd06e5SJens Axboe ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1)); 1762aadd06e5SJens Axboe nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1); 176370524490SJens Axboe 176470524490SJens Axboe /* 176570524490SJens Axboe * Get a reference to this pipe buffer, 176670524490SJens Axboe * so we can copy the contents over. 176770524490SJens Axboe */ 176870524490SJens Axboe ibuf->ops->get(ipipe, ibuf); 176970524490SJens Axboe 177070524490SJens Axboe obuf = opipe->bufs + nbuf; 177170524490SJens Axboe *obuf = *ibuf; 177270524490SJens Axboe 17737afa6fd0SJens Axboe /* 17747afa6fd0SJens Axboe * Don't inherit the gift flag, we need to 17757afa6fd0SJens Axboe * prevent multiple steals of this page. 17767afa6fd0SJens Axboe */ 17777afa6fd0SJens Axboe obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 17787afa6fd0SJens Axboe 177970524490SJens Axboe if (obuf->len > len) 178070524490SJens Axboe obuf->len = len; 178170524490SJens Axboe 178270524490SJens Axboe opipe->nrbufs++; 178370524490SJens Axboe ret += obuf->len; 178470524490SJens Axboe len -= obuf->len; 1785aadd06e5SJens Axboe i++; 1786aadd06e5SJens Axboe } while (len); 178770524490SJens Axboe 178802cf01aeSJens Axboe /* 178902cf01aeSJens Axboe * return EAGAIN if we have the potential of some data in the 179002cf01aeSJens Axboe * future, otherwise just return 0 179102cf01aeSJens Axboe */ 179202cf01aeSJens Axboe if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) 179302cf01aeSJens Axboe ret = -EAGAIN; 179402cf01aeSJens Axboe 179561e0d47cSMiklos Szeredi pipe_unlock(ipipe); 179661e0d47cSMiklos Szeredi pipe_unlock(opipe); 179770524490SJens Axboe 1798aadd06e5SJens Axboe /* 1799aadd06e5SJens Axboe * If we put data in the output pipe, wakeup any potential readers. 1800aadd06e5SJens Axboe */ 1801aadd06e5SJens Axboe if (ret > 0) { 180270524490SJens Axboe smp_mb(); 180370524490SJens Axboe if (waitqueue_active(&opipe->wait)) 180470524490SJens Axboe wake_up_interruptible(&opipe->wait); 180570524490SJens Axboe kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); 180670524490SJens Axboe } 180770524490SJens Axboe 180870524490SJens Axboe return ret; 180970524490SJens Axboe } 181070524490SJens Axboe 181170524490SJens Axboe /* 181270524490SJens Axboe * This is a tee(1) implementation that works on pipes. It doesn't copy 181370524490SJens Axboe * any data, it simply references the 'in' pages on the 'out' pipe. 181470524490SJens Axboe * The 'flags' used are the SPLICE_F_* variants, currently the only 181570524490SJens Axboe * applicable one is SPLICE_F_NONBLOCK. 181670524490SJens Axboe */ 181770524490SJens Axboe static long do_tee(struct file *in, struct file *out, size_t len, 181870524490SJens Axboe unsigned int flags) 181970524490SJens Axboe { 18200f7fc9e4SJosef "Jeff" Sipek struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode); 18210f7fc9e4SJosef "Jeff" Sipek struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode); 1822aadd06e5SJens Axboe int ret = -EINVAL; 182370524490SJens Axboe 182470524490SJens Axboe /* 1825aadd06e5SJens Axboe * Duplicate the contents of ipipe to opipe without actually 1826aadd06e5SJens Axboe * copying the data. 182770524490SJens Axboe */ 1828aadd06e5SJens Axboe if (ipipe && opipe && ipipe != opipe) { 1829aadd06e5SJens Axboe /* 1830aadd06e5SJens Axboe * Keep going, unless we encounter an error. The ipipe/opipe 1831aadd06e5SJens Axboe * ordering doesn't really matter. 1832aadd06e5SJens Axboe */ 18337c77f0b3SMiklos Szeredi ret = ipipe_prep(ipipe, flags); 1834aadd06e5SJens Axboe if (!ret) { 18357c77f0b3SMiklos Szeredi ret = opipe_prep(opipe, flags); 183602cf01aeSJens Axboe if (!ret) 1837aadd06e5SJens Axboe ret = link_pipe(ipipe, opipe, len, flags); 1838aadd06e5SJens Axboe } 1839aadd06e5SJens Axboe } 184070524490SJens Axboe 1841aadd06e5SJens Axboe return ret; 184270524490SJens Axboe } 184370524490SJens Axboe 1844836f92adSHeiko Carstens SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 184570524490SJens Axboe { 184670524490SJens Axboe struct file *in; 184770524490SJens Axboe int error, fput_in; 184870524490SJens Axboe 184970524490SJens Axboe if (unlikely(!len)) 185070524490SJens Axboe return 0; 185170524490SJens Axboe 185270524490SJens Axboe error = -EBADF; 185370524490SJens Axboe in = fget_light(fdin, &fput_in); 185470524490SJens Axboe if (in) { 185570524490SJens Axboe if (in->f_mode & FMODE_READ) { 185670524490SJens Axboe int fput_out; 185770524490SJens Axboe struct file *out = fget_light(fdout, &fput_out); 185870524490SJens Axboe 185970524490SJens Axboe if (out) { 186070524490SJens Axboe if (out->f_mode & FMODE_WRITE) 186170524490SJens Axboe error = do_tee(in, out, len, flags); 186270524490SJens Axboe fput_light(out, fput_out); 186370524490SJens Axboe } 186470524490SJens Axboe } 186570524490SJens Axboe fput_light(in, fput_in); 186670524490SJens Axboe } 186770524490SJens Axboe 186870524490SJens Axboe return error; 186970524490SJens Axboe } 1870