162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * "splice": joining two ropes together by interweaving their strands. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This is the "extended pipe" functionality, where a pipe is used as 662306a36Sopenharmony_ci * an arbitrary in-memory buffer. Think of a pipe as a small kernel 762306a36Sopenharmony_ci * buffer that you can use to transfer data from one end to the other. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * The traditional unix read/write is extended with a "splice()" operation 1062306a36Sopenharmony_ci * that transfers data buffers to or from a pipe buffer. 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Named by Larry McVoy, original implementation from Linus, extended by 1362306a36Sopenharmony_ci * Jens to support splicing to files, network, direct splicing, etc and 1462306a36Sopenharmony_ci * fixing lots of bugs. 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> 1762306a36Sopenharmony_ci * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> 1862306a36Sopenharmony_ci * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci */ 2162306a36Sopenharmony_ci#include <linux/bvec.h> 2262306a36Sopenharmony_ci#include <linux/fs.h> 2362306a36Sopenharmony_ci#include <linux/file.h> 2462306a36Sopenharmony_ci#include <linux/pagemap.h> 2562306a36Sopenharmony_ci#include <linux/splice.h> 2662306a36Sopenharmony_ci#include <linux/memcontrol.h> 2762306a36Sopenharmony_ci#include <linux/mm_inline.h> 2862306a36Sopenharmony_ci#include <linux/swap.h> 2962306a36Sopenharmony_ci#include <linux/writeback.h> 3062306a36Sopenharmony_ci#include <linux/export.h> 3162306a36Sopenharmony_ci#include <linux/syscalls.h> 3262306a36Sopenharmony_ci#include <linux/uio.h> 3362306a36Sopenharmony_ci#include <linux/fsnotify.h> 3462306a36Sopenharmony_ci#include <linux/security.h> 3562306a36Sopenharmony_ci#include <linux/gfp.h> 3662306a36Sopenharmony_ci#include <linux/net.h> 3762306a36Sopenharmony_ci#include <linux/socket.h> 3862306a36Sopenharmony_ci#include <linux/sched/signal.h> 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci#include "internal.h" 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci/* 4362306a36Sopenharmony_ci * Splice doesn't support FMODE_NOWAIT. Since pipes may set this flag to 4462306a36Sopenharmony_ci * indicate they support non-blocking reads or writes, we must clear it 4562306a36Sopenharmony_ci * here if set to avoid blocking other users of this pipe if splice is 4662306a36Sopenharmony_ci * being done on it. 4762306a36Sopenharmony_ci */ 4862306a36Sopenharmony_cistatic noinline void noinline pipe_clear_nowait(struct file *file) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci fmode_t fmode = READ_ONCE(file->f_mode); 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci do { 5362306a36Sopenharmony_ci if (!(fmode & FMODE_NOWAIT)) 5462306a36Sopenharmony_ci break; 5562306a36Sopenharmony_ci } while (!try_cmpxchg(&file->f_mode, &fmode, fmode & ~FMODE_NOWAIT)); 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* 5962306a36Sopenharmony_ci * Attempt to steal a page from a pipe buffer. This should perhaps go into 6062306a36Sopenharmony_ci * a vm helper function, it's already simplified quite a bit by the 6162306a36Sopenharmony_ci * addition of remove_mapping(). If success is returned, the caller may 6262306a36Sopenharmony_ci * attempt to reuse this page for another destination. 6362306a36Sopenharmony_ci */ 6462306a36Sopenharmony_cistatic bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe, 6562306a36Sopenharmony_ci struct pipe_buffer *buf) 6662306a36Sopenharmony_ci{ 6762306a36Sopenharmony_ci struct folio *folio = page_folio(buf->page); 6862306a36Sopenharmony_ci struct address_space *mapping; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci folio_lock(folio); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci mapping = folio_mapping(folio); 7362306a36Sopenharmony_ci if (mapping) { 7462306a36Sopenharmony_ci WARN_ON(!folio_test_uptodate(folio)); 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci /* 7762306a36Sopenharmony_ci * At least for ext2 with nobh option, we need to wait on 7862306a36Sopenharmony_ci * writeback completing on this folio, since we'll remove it 7962306a36Sopenharmony_ci * from the pagecache. Otherwise truncate wont wait on the 8062306a36Sopenharmony_ci * folio, allowing the disk blocks to be reused by someone else 8162306a36Sopenharmony_ci * before we actually wrote our data to them. fs corruption 8262306a36Sopenharmony_ci * ensues. 8362306a36Sopenharmony_ci */ 8462306a36Sopenharmony_ci folio_wait_writeback(folio); 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci if (!filemap_release_folio(folio, GFP_KERNEL)) 8762306a36Sopenharmony_ci goto out_unlock; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci /* 9062306a36Sopenharmony_ci * If we succeeded in removing the mapping, set LRU flag 9162306a36Sopenharmony_ci * and return good. 9262306a36Sopenharmony_ci */ 9362306a36Sopenharmony_ci if (remove_mapping(mapping, folio)) { 9462306a36Sopenharmony_ci buf->flags |= PIPE_BUF_FLAG_LRU; 9562306a36Sopenharmony_ci return true; 9662306a36Sopenharmony_ci } 9762306a36Sopenharmony_ci } 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci /* 10062306a36Sopenharmony_ci * Raced with truncate or failed to remove folio from current 10162306a36Sopenharmony_ci * address space, unlock and return failure. 10262306a36Sopenharmony_ci */ 10362306a36Sopenharmony_ciout_unlock: 10462306a36Sopenharmony_ci folio_unlock(folio); 10562306a36Sopenharmony_ci return false; 10662306a36Sopenharmony_ci} 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_cistatic void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, 10962306a36Sopenharmony_ci struct pipe_buffer *buf) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci put_page(buf->page); 11262306a36Sopenharmony_ci buf->flags &= ~PIPE_BUF_FLAG_LRU; 11362306a36Sopenharmony_ci} 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci/* 11662306a36Sopenharmony_ci * Check whether the contents of buf is OK to access. Since the content 11762306a36Sopenharmony_ci * is a page cache page, IO may be in flight. 11862306a36Sopenharmony_ci */ 11962306a36Sopenharmony_cistatic int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, 12062306a36Sopenharmony_ci struct pipe_buffer *buf) 12162306a36Sopenharmony_ci{ 12262306a36Sopenharmony_ci struct folio *folio = page_folio(buf->page); 12362306a36Sopenharmony_ci int err; 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci if (!folio_test_uptodate(folio)) { 12662306a36Sopenharmony_ci folio_lock(folio); 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci /* 12962306a36Sopenharmony_ci * Folio got truncated/unhashed. This will cause a 0-byte 13062306a36Sopenharmony_ci * splice, if this is the first page. 13162306a36Sopenharmony_ci */ 13262306a36Sopenharmony_ci if (!folio->mapping) { 13362306a36Sopenharmony_ci err = -ENODATA; 13462306a36Sopenharmony_ci goto error; 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci /* 13862306a36Sopenharmony_ci * Uh oh, read-error from disk. 13962306a36Sopenharmony_ci */ 14062306a36Sopenharmony_ci if (!folio_test_uptodate(folio)) { 14162306a36Sopenharmony_ci err = -EIO; 14262306a36Sopenharmony_ci goto error; 14362306a36Sopenharmony_ci } 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci /* Folio is ok after all, we are done */ 14662306a36Sopenharmony_ci folio_unlock(folio); 14762306a36Sopenharmony_ci } 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci return 0; 15062306a36Sopenharmony_cierror: 15162306a36Sopenharmony_ci folio_unlock(folio); 15262306a36Sopenharmony_ci return err; 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ciconst struct pipe_buf_operations page_cache_pipe_buf_ops = { 15662306a36Sopenharmony_ci .confirm = page_cache_pipe_buf_confirm, 15762306a36Sopenharmony_ci .release = page_cache_pipe_buf_release, 15862306a36Sopenharmony_ci .try_steal = page_cache_pipe_buf_try_steal, 15962306a36Sopenharmony_ci .get = generic_pipe_buf_get, 16062306a36Sopenharmony_ci}; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_cistatic bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe, 16362306a36Sopenharmony_ci struct pipe_buffer *buf) 16462306a36Sopenharmony_ci{ 16562306a36Sopenharmony_ci if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) 16662306a36Sopenharmony_ci return false; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci buf->flags |= PIPE_BUF_FLAG_LRU; 16962306a36Sopenharmony_ci return generic_pipe_buf_try_steal(pipe, buf); 17062306a36Sopenharmony_ci} 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_cistatic const struct pipe_buf_operations user_page_pipe_buf_ops = { 17362306a36Sopenharmony_ci .release = page_cache_pipe_buf_release, 17462306a36Sopenharmony_ci .try_steal = user_page_pipe_buf_try_steal, 17562306a36Sopenharmony_ci .get = generic_pipe_buf_get, 17662306a36Sopenharmony_ci}; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_cistatic void wakeup_pipe_readers(struct pipe_inode_info *pipe) 17962306a36Sopenharmony_ci{ 18062306a36Sopenharmony_ci smp_mb(); 18162306a36Sopenharmony_ci if (waitqueue_active(&pipe->rd_wait)) 18262306a36Sopenharmony_ci wake_up_interruptible(&pipe->rd_wait); 18362306a36Sopenharmony_ci kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 18462306a36Sopenharmony_ci} 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci/** 18762306a36Sopenharmony_ci * splice_to_pipe - fill passed data into a pipe 18862306a36Sopenharmony_ci * @pipe: pipe to fill 18962306a36Sopenharmony_ci * @spd: data to fill 19062306a36Sopenharmony_ci * 19162306a36Sopenharmony_ci * Description: 19262306a36Sopenharmony_ci * @spd contains a map of pages and len/offset tuples, along with 19362306a36Sopenharmony_ci * the struct pipe_buf_operations associated with these pages. This 19462306a36Sopenharmony_ci * function will link that data to the pipe. 19562306a36Sopenharmony_ci * 19662306a36Sopenharmony_ci */ 19762306a36Sopenharmony_cissize_t splice_to_pipe(struct pipe_inode_info *pipe, 19862306a36Sopenharmony_ci struct splice_pipe_desc *spd) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci unsigned int spd_pages = spd->nr_pages; 20162306a36Sopenharmony_ci unsigned int tail = pipe->tail; 20262306a36Sopenharmony_ci unsigned int head = pipe->head; 20362306a36Sopenharmony_ci unsigned int mask = pipe->ring_size - 1; 20462306a36Sopenharmony_ci int ret = 0, page_nr = 0; 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci if (!spd_pages) 20762306a36Sopenharmony_ci return 0; 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci if (unlikely(!pipe->readers)) { 21062306a36Sopenharmony_ci send_sig(SIGPIPE, current, 0); 21162306a36Sopenharmony_ci ret = -EPIPE; 21262306a36Sopenharmony_ci goto out; 21362306a36Sopenharmony_ci } 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci while (!pipe_full(head, tail, pipe->max_usage)) { 21662306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[head & mask]; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci buf->page = spd->pages[page_nr]; 21962306a36Sopenharmony_ci buf->offset = spd->partial[page_nr].offset; 22062306a36Sopenharmony_ci buf->len = spd->partial[page_nr].len; 22162306a36Sopenharmony_ci buf->private = spd->partial[page_nr].private; 22262306a36Sopenharmony_ci buf->ops = spd->ops; 22362306a36Sopenharmony_ci buf->flags = 0; 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci head++; 22662306a36Sopenharmony_ci pipe->head = head; 22762306a36Sopenharmony_ci page_nr++; 22862306a36Sopenharmony_ci ret += buf->len; 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci if (!--spd->nr_pages) 23162306a36Sopenharmony_ci break; 23262306a36Sopenharmony_ci } 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci if (!ret) 23562306a36Sopenharmony_ci ret = -EAGAIN; 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ciout: 23862306a36Sopenharmony_ci while (page_nr < spd_pages) 23962306a36Sopenharmony_ci spd->spd_release(spd, page_nr++); 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci return ret; 24262306a36Sopenharmony_ci} 24362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(splice_to_pipe); 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_cissize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) 24662306a36Sopenharmony_ci{ 24762306a36Sopenharmony_ci unsigned int head = pipe->head; 24862306a36Sopenharmony_ci unsigned int tail = pipe->tail; 24962306a36Sopenharmony_ci unsigned int mask = pipe->ring_size - 1; 25062306a36Sopenharmony_ci int ret; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci if (unlikely(!pipe->readers)) { 25362306a36Sopenharmony_ci send_sig(SIGPIPE, current, 0); 25462306a36Sopenharmony_ci ret = -EPIPE; 25562306a36Sopenharmony_ci } else if (pipe_full(head, tail, pipe->max_usage)) { 25662306a36Sopenharmony_ci ret = -EAGAIN; 25762306a36Sopenharmony_ci } else { 25862306a36Sopenharmony_ci pipe->bufs[head & mask] = *buf; 25962306a36Sopenharmony_ci pipe->head = head + 1; 26062306a36Sopenharmony_ci return buf->len; 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci pipe_buf_release(pipe, buf); 26362306a36Sopenharmony_ci return ret; 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ciEXPORT_SYMBOL(add_to_pipe); 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci/* 26862306a36Sopenharmony_ci * Check if we need to grow the arrays holding pages and partial page 26962306a36Sopenharmony_ci * descriptions. 27062306a36Sopenharmony_ci */ 27162306a36Sopenharmony_ciint splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) 27262306a36Sopenharmony_ci{ 27362306a36Sopenharmony_ci unsigned int max_usage = READ_ONCE(pipe->max_usage); 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci spd->nr_pages_max = max_usage; 27662306a36Sopenharmony_ci if (max_usage <= PIPE_DEF_BUFFERS) 27762306a36Sopenharmony_ci return 0; 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL); 28062306a36Sopenharmony_ci spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page), 28162306a36Sopenharmony_ci GFP_KERNEL); 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci if (spd->pages && spd->partial) 28462306a36Sopenharmony_ci return 0; 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci kfree(spd->pages); 28762306a36Sopenharmony_ci kfree(spd->partial); 28862306a36Sopenharmony_ci return -ENOMEM; 28962306a36Sopenharmony_ci} 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_civoid splice_shrink_spd(struct splice_pipe_desc *spd) 29262306a36Sopenharmony_ci{ 29362306a36Sopenharmony_ci if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) 29462306a36Sopenharmony_ci return; 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci kfree(spd->pages); 29762306a36Sopenharmony_ci kfree(spd->partial); 29862306a36Sopenharmony_ci} 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci/** 30162306a36Sopenharmony_ci * copy_splice_read - Copy data from a file and splice the copy into a pipe 30262306a36Sopenharmony_ci * @in: The file to read from 30362306a36Sopenharmony_ci * @ppos: Pointer to the file position to read from 30462306a36Sopenharmony_ci * @pipe: The pipe to splice into 30562306a36Sopenharmony_ci * @len: The amount to splice 30662306a36Sopenharmony_ci * @flags: The SPLICE_F_* flags 30762306a36Sopenharmony_ci * 30862306a36Sopenharmony_ci * This function allocates a bunch of pages sufficient to hold the requested 30962306a36Sopenharmony_ci * amount of data (but limited by the remaining pipe capacity), passes it to 31062306a36Sopenharmony_ci * the file's ->read_iter() to read into and then splices the used pages into 31162306a36Sopenharmony_ci * the pipe. 31262306a36Sopenharmony_ci * 31362306a36Sopenharmony_ci * Return: On success, the number of bytes read will be returned and *@ppos 31462306a36Sopenharmony_ci * will be updated if appropriate; 0 will be returned if there is no more data 31562306a36Sopenharmony_ci * to be read; -EAGAIN will be returned if the pipe had no space, and some 31662306a36Sopenharmony_ci * other negative error code will be returned on error. A short read may occur 31762306a36Sopenharmony_ci * if the pipe has insufficient space, we reach the end of the data or we hit a 31862306a36Sopenharmony_ci * hole. 31962306a36Sopenharmony_ci */ 32062306a36Sopenharmony_cissize_t copy_splice_read(struct file *in, loff_t *ppos, 32162306a36Sopenharmony_ci struct pipe_inode_info *pipe, 32262306a36Sopenharmony_ci size_t len, unsigned int flags) 32362306a36Sopenharmony_ci{ 32462306a36Sopenharmony_ci struct iov_iter to; 32562306a36Sopenharmony_ci struct bio_vec *bv; 32662306a36Sopenharmony_ci struct kiocb kiocb; 32762306a36Sopenharmony_ci struct page **pages; 32862306a36Sopenharmony_ci ssize_t ret; 32962306a36Sopenharmony_ci size_t used, npages, chunk, remain, keep = 0; 33062306a36Sopenharmony_ci int i; 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci /* Work out how much data we can actually add into the pipe */ 33362306a36Sopenharmony_ci used = pipe_occupancy(pipe->head, pipe->tail); 33462306a36Sopenharmony_ci npages = max_t(ssize_t, pipe->max_usage - used, 0); 33562306a36Sopenharmony_ci len = min_t(size_t, len, npages * PAGE_SIZE); 33662306a36Sopenharmony_ci npages = DIV_ROUND_UP(len, PAGE_SIZE); 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci bv = kzalloc(array_size(npages, sizeof(bv[0])) + 33962306a36Sopenharmony_ci array_size(npages, sizeof(struct page *)), GFP_KERNEL); 34062306a36Sopenharmony_ci if (!bv) 34162306a36Sopenharmony_ci return -ENOMEM; 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci pages = (struct page **)(bv + npages); 34462306a36Sopenharmony_ci npages = alloc_pages_bulk_array(GFP_USER, npages, pages); 34562306a36Sopenharmony_ci if (!npages) { 34662306a36Sopenharmony_ci kfree(bv); 34762306a36Sopenharmony_ci return -ENOMEM; 34862306a36Sopenharmony_ci } 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci remain = len = min_t(size_t, len, npages * PAGE_SIZE); 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci for (i = 0; i < npages; i++) { 35362306a36Sopenharmony_ci chunk = min_t(size_t, PAGE_SIZE, remain); 35462306a36Sopenharmony_ci bv[i].bv_page = pages[i]; 35562306a36Sopenharmony_ci bv[i].bv_offset = 0; 35662306a36Sopenharmony_ci bv[i].bv_len = chunk; 35762306a36Sopenharmony_ci remain -= chunk; 35862306a36Sopenharmony_ci } 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci /* Do the I/O */ 36162306a36Sopenharmony_ci iov_iter_bvec(&to, ITER_DEST, bv, npages, len); 36262306a36Sopenharmony_ci init_sync_kiocb(&kiocb, in); 36362306a36Sopenharmony_ci kiocb.ki_pos = *ppos; 36462306a36Sopenharmony_ci ret = call_read_iter(in, &kiocb, &to); 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci if (ret > 0) { 36762306a36Sopenharmony_ci keep = DIV_ROUND_UP(ret, PAGE_SIZE); 36862306a36Sopenharmony_ci *ppos = kiocb.ki_pos; 36962306a36Sopenharmony_ci } 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci /* 37262306a36Sopenharmony_ci * Callers of ->splice_read() expect -EAGAIN on "can't put anything in 37362306a36Sopenharmony_ci * there", rather than -EFAULT. 37462306a36Sopenharmony_ci */ 37562306a36Sopenharmony_ci if (ret == -EFAULT) 37662306a36Sopenharmony_ci ret = -EAGAIN; 37762306a36Sopenharmony_ci 37862306a36Sopenharmony_ci /* Free any pages that didn't get touched at all. */ 37962306a36Sopenharmony_ci if (keep < npages) 38062306a36Sopenharmony_ci release_pages(pages + keep, npages - keep); 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci /* Push the remaining pages into the pipe. */ 38362306a36Sopenharmony_ci remain = ret; 38462306a36Sopenharmony_ci for (i = 0; i < keep; i++) { 38562306a36Sopenharmony_ci struct pipe_buffer *buf = pipe_head_buf(pipe); 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci chunk = min_t(size_t, remain, PAGE_SIZE); 38862306a36Sopenharmony_ci *buf = (struct pipe_buffer) { 38962306a36Sopenharmony_ci .ops = &default_pipe_buf_ops, 39062306a36Sopenharmony_ci .page = bv[i].bv_page, 39162306a36Sopenharmony_ci .offset = 0, 39262306a36Sopenharmony_ci .len = chunk, 39362306a36Sopenharmony_ci }; 39462306a36Sopenharmony_ci pipe->head++; 39562306a36Sopenharmony_ci remain -= chunk; 39662306a36Sopenharmony_ci } 39762306a36Sopenharmony_ci 39862306a36Sopenharmony_ci kfree(bv); 39962306a36Sopenharmony_ci return ret; 40062306a36Sopenharmony_ci} 40162306a36Sopenharmony_ciEXPORT_SYMBOL(copy_splice_read); 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ciconst struct pipe_buf_operations default_pipe_buf_ops = { 40462306a36Sopenharmony_ci .release = generic_pipe_buf_release, 40562306a36Sopenharmony_ci .try_steal = generic_pipe_buf_try_steal, 40662306a36Sopenharmony_ci .get = generic_pipe_buf_get, 40762306a36Sopenharmony_ci}; 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci/* Pipe buffer operations for a socket and similar. */ 41062306a36Sopenharmony_ciconst struct pipe_buf_operations nosteal_pipe_buf_ops = { 41162306a36Sopenharmony_ci .release = generic_pipe_buf_release, 41262306a36Sopenharmony_ci .get = generic_pipe_buf_get, 41362306a36Sopenharmony_ci}; 41462306a36Sopenharmony_ciEXPORT_SYMBOL(nosteal_pipe_buf_ops); 41562306a36Sopenharmony_ci 41662306a36Sopenharmony_cistatic void wakeup_pipe_writers(struct pipe_inode_info *pipe) 41762306a36Sopenharmony_ci{ 41862306a36Sopenharmony_ci smp_mb(); 41962306a36Sopenharmony_ci if (waitqueue_active(&pipe->wr_wait)) 42062306a36Sopenharmony_ci wake_up_interruptible(&pipe->wr_wait); 42162306a36Sopenharmony_ci kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 42262306a36Sopenharmony_ci} 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci/** 42562306a36Sopenharmony_ci * splice_from_pipe_feed - feed available data from a pipe to a file 42662306a36Sopenharmony_ci * @pipe: pipe to splice from 42762306a36Sopenharmony_ci * @sd: information to @actor 42862306a36Sopenharmony_ci * @actor: handler that splices the data 42962306a36Sopenharmony_ci * 43062306a36Sopenharmony_ci * Description: 43162306a36Sopenharmony_ci * This function loops over the pipe and calls @actor to do the 43262306a36Sopenharmony_ci * actual moving of a single struct pipe_buffer to the desired 43362306a36Sopenharmony_ci * destination. It returns when there's no more buffers left in 43462306a36Sopenharmony_ci * the pipe or if the requested number of bytes (@sd->total_len) 43562306a36Sopenharmony_ci * have been copied. It returns a positive number (one) if the 43662306a36Sopenharmony_ci * pipe needs to be filled with more data, zero if the required 43762306a36Sopenharmony_ci * number of bytes have been copied and -errno on error. 43862306a36Sopenharmony_ci * 43962306a36Sopenharmony_ci * This, together with splice_from_pipe_{begin,end,next}, may be 44062306a36Sopenharmony_ci * used to implement the functionality of __splice_from_pipe() when 44162306a36Sopenharmony_ci * locking is required around copying the pipe buffers to the 44262306a36Sopenharmony_ci * destination. 44362306a36Sopenharmony_ci */ 44462306a36Sopenharmony_cistatic int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 44562306a36Sopenharmony_ci splice_actor *actor) 44662306a36Sopenharmony_ci{ 44762306a36Sopenharmony_ci unsigned int head = pipe->head; 44862306a36Sopenharmony_ci unsigned int tail = pipe->tail; 44962306a36Sopenharmony_ci unsigned int mask = pipe->ring_size - 1; 45062306a36Sopenharmony_ci int ret; 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci while (!pipe_empty(head, tail)) { 45362306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci sd->len = buf->len; 45662306a36Sopenharmony_ci if (sd->len > sd->total_len) 45762306a36Sopenharmony_ci sd->len = sd->total_len; 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci ret = pipe_buf_confirm(pipe, buf); 46062306a36Sopenharmony_ci if (unlikely(ret)) { 46162306a36Sopenharmony_ci if (ret == -ENODATA) 46262306a36Sopenharmony_ci ret = 0; 46362306a36Sopenharmony_ci return ret; 46462306a36Sopenharmony_ci } 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci ret = actor(pipe, buf, sd); 46762306a36Sopenharmony_ci if (ret <= 0) 46862306a36Sopenharmony_ci return ret; 46962306a36Sopenharmony_ci 47062306a36Sopenharmony_ci buf->offset += ret; 47162306a36Sopenharmony_ci buf->len -= ret; 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci sd->num_spliced += ret; 47462306a36Sopenharmony_ci sd->len -= ret; 47562306a36Sopenharmony_ci sd->pos += ret; 47662306a36Sopenharmony_ci sd->total_len -= ret; 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci if (!buf->len) { 47962306a36Sopenharmony_ci pipe_buf_release(pipe, buf); 48062306a36Sopenharmony_ci tail++; 48162306a36Sopenharmony_ci pipe->tail = tail; 48262306a36Sopenharmony_ci if (pipe->files) 48362306a36Sopenharmony_ci sd->need_wakeup = true; 48462306a36Sopenharmony_ci } 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci if (!sd->total_len) 48762306a36Sopenharmony_ci return 0; 48862306a36Sopenharmony_ci } 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci return 1; 49162306a36Sopenharmony_ci} 49262306a36Sopenharmony_ci 49362306a36Sopenharmony_ci/* We know we have a pipe buffer, but maybe it's empty? */ 49462306a36Sopenharmony_cistatic inline bool eat_empty_buffer(struct pipe_inode_info *pipe) 49562306a36Sopenharmony_ci{ 49662306a36Sopenharmony_ci unsigned int tail = pipe->tail; 49762306a36Sopenharmony_ci unsigned int mask = pipe->ring_size - 1; 49862306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_ci if (unlikely(!buf->len)) { 50162306a36Sopenharmony_ci pipe_buf_release(pipe, buf); 50262306a36Sopenharmony_ci pipe->tail = tail+1; 50362306a36Sopenharmony_ci return true; 50462306a36Sopenharmony_ci } 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci return false; 50762306a36Sopenharmony_ci} 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci/** 51062306a36Sopenharmony_ci * splice_from_pipe_next - wait for some data to splice from 51162306a36Sopenharmony_ci * @pipe: pipe to splice from 51262306a36Sopenharmony_ci * @sd: information about the splice operation 51362306a36Sopenharmony_ci * 51462306a36Sopenharmony_ci * Description: 51562306a36Sopenharmony_ci * This function will wait for some data and return a positive 51662306a36Sopenharmony_ci * value (one) if pipe buffers are available. It will return zero 51762306a36Sopenharmony_ci * or -errno if no more data needs to be spliced. 51862306a36Sopenharmony_ci */ 51962306a36Sopenharmony_cistatic int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 52062306a36Sopenharmony_ci{ 52162306a36Sopenharmony_ci /* 52262306a36Sopenharmony_ci * Check for signal early to make process killable when there are 52362306a36Sopenharmony_ci * always buffers available 52462306a36Sopenharmony_ci */ 52562306a36Sopenharmony_ci if (signal_pending(current)) 52662306a36Sopenharmony_ci return -ERESTARTSYS; 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_cirepeat: 52962306a36Sopenharmony_ci while (pipe_empty(pipe->head, pipe->tail)) { 53062306a36Sopenharmony_ci if (!pipe->writers) 53162306a36Sopenharmony_ci return 0; 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci if (sd->num_spliced) 53462306a36Sopenharmony_ci return 0; 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci if (sd->flags & SPLICE_F_NONBLOCK) 53762306a36Sopenharmony_ci return -EAGAIN; 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci if (signal_pending(current)) 54062306a36Sopenharmony_ci return -ERESTARTSYS; 54162306a36Sopenharmony_ci 54262306a36Sopenharmony_ci if (sd->need_wakeup) { 54362306a36Sopenharmony_ci wakeup_pipe_writers(pipe); 54462306a36Sopenharmony_ci sd->need_wakeup = false; 54562306a36Sopenharmony_ci } 54662306a36Sopenharmony_ci 54762306a36Sopenharmony_ci pipe_wait_readable(pipe); 54862306a36Sopenharmony_ci } 54962306a36Sopenharmony_ci 55062306a36Sopenharmony_ci if (eat_empty_buffer(pipe)) 55162306a36Sopenharmony_ci goto repeat; 55262306a36Sopenharmony_ci 55362306a36Sopenharmony_ci return 1; 55462306a36Sopenharmony_ci} 55562306a36Sopenharmony_ci 55662306a36Sopenharmony_ci/** 55762306a36Sopenharmony_ci * splice_from_pipe_begin - start splicing from pipe 55862306a36Sopenharmony_ci * @sd: information about the splice operation 55962306a36Sopenharmony_ci * 56062306a36Sopenharmony_ci * Description: 56162306a36Sopenharmony_ci * This function should be called before a loop containing 56262306a36Sopenharmony_ci * splice_from_pipe_next() and splice_from_pipe_feed() to 56362306a36Sopenharmony_ci * initialize the necessary fields of @sd. 56462306a36Sopenharmony_ci */ 56562306a36Sopenharmony_cistatic void splice_from_pipe_begin(struct splice_desc *sd) 56662306a36Sopenharmony_ci{ 56762306a36Sopenharmony_ci sd->num_spliced = 0; 56862306a36Sopenharmony_ci sd->need_wakeup = false; 56962306a36Sopenharmony_ci} 57062306a36Sopenharmony_ci 57162306a36Sopenharmony_ci/** 57262306a36Sopenharmony_ci * splice_from_pipe_end - finish splicing from pipe 57362306a36Sopenharmony_ci * @pipe: pipe to splice from 57462306a36Sopenharmony_ci * @sd: information about the splice operation 57562306a36Sopenharmony_ci * 57662306a36Sopenharmony_ci * Description: 57762306a36Sopenharmony_ci * This function will wake up pipe writers if necessary. It should 57862306a36Sopenharmony_ci * be called after a loop containing splice_from_pipe_next() and 57962306a36Sopenharmony_ci * splice_from_pipe_feed(). 58062306a36Sopenharmony_ci */ 58162306a36Sopenharmony_cistatic void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 58262306a36Sopenharmony_ci{ 58362306a36Sopenharmony_ci if (sd->need_wakeup) 58462306a36Sopenharmony_ci wakeup_pipe_writers(pipe); 58562306a36Sopenharmony_ci} 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci/** 58862306a36Sopenharmony_ci * __splice_from_pipe - splice data from a pipe to given actor 58962306a36Sopenharmony_ci * @pipe: pipe to splice from 59062306a36Sopenharmony_ci * @sd: information to @actor 59162306a36Sopenharmony_ci * @actor: handler that splices the data 59262306a36Sopenharmony_ci * 59362306a36Sopenharmony_ci * Description: 59462306a36Sopenharmony_ci * This function does little more than loop over the pipe and call 59562306a36Sopenharmony_ci * @actor to do the actual moving of a single struct pipe_buffer to 59662306a36Sopenharmony_ci * the desired destination. See pipe_to_file, pipe_to_sendmsg, or 59762306a36Sopenharmony_ci * pipe_to_user. 59862306a36Sopenharmony_ci * 59962306a36Sopenharmony_ci */ 60062306a36Sopenharmony_cissize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 60162306a36Sopenharmony_ci splice_actor *actor) 60262306a36Sopenharmony_ci{ 60362306a36Sopenharmony_ci int ret; 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci splice_from_pipe_begin(sd); 60662306a36Sopenharmony_ci do { 60762306a36Sopenharmony_ci cond_resched(); 60862306a36Sopenharmony_ci ret = splice_from_pipe_next(pipe, sd); 60962306a36Sopenharmony_ci if (ret > 0) 61062306a36Sopenharmony_ci ret = splice_from_pipe_feed(pipe, sd, actor); 61162306a36Sopenharmony_ci } while (ret > 0); 61262306a36Sopenharmony_ci splice_from_pipe_end(pipe, sd); 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci return sd->num_spliced ? sd->num_spliced : ret; 61562306a36Sopenharmony_ci} 61662306a36Sopenharmony_ciEXPORT_SYMBOL(__splice_from_pipe); 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_ci/** 61962306a36Sopenharmony_ci * splice_from_pipe - splice data from a pipe to a file 62062306a36Sopenharmony_ci * @pipe: pipe to splice from 62162306a36Sopenharmony_ci * @out: file to splice to 62262306a36Sopenharmony_ci * @ppos: position in @out 62362306a36Sopenharmony_ci * @len: how many bytes to splice 62462306a36Sopenharmony_ci * @flags: splice modifier flags 62562306a36Sopenharmony_ci * @actor: handler that splices the data 62662306a36Sopenharmony_ci * 62762306a36Sopenharmony_ci * Description: 62862306a36Sopenharmony_ci * See __splice_from_pipe. This function locks the pipe inode, 62962306a36Sopenharmony_ci * otherwise it's identical to __splice_from_pipe(). 63062306a36Sopenharmony_ci * 63162306a36Sopenharmony_ci */ 63262306a36Sopenharmony_cissize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, 63362306a36Sopenharmony_ci loff_t *ppos, size_t len, unsigned int flags, 63462306a36Sopenharmony_ci splice_actor *actor) 63562306a36Sopenharmony_ci{ 63662306a36Sopenharmony_ci ssize_t ret; 63762306a36Sopenharmony_ci struct splice_desc sd = { 63862306a36Sopenharmony_ci .total_len = len, 63962306a36Sopenharmony_ci .flags = flags, 64062306a36Sopenharmony_ci .pos = *ppos, 64162306a36Sopenharmony_ci .u.file = out, 64262306a36Sopenharmony_ci }; 64362306a36Sopenharmony_ci 64462306a36Sopenharmony_ci pipe_lock(pipe); 64562306a36Sopenharmony_ci ret = __splice_from_pipe(pipe, &sd, actor); 64662306a36Sopenharmony_ci pipe_unlock(pipe); 64762306a36Sopenharmony_ci 64862306a36Sopenharmony_ci return ret; 64962306a36Sopenharmony_ci} 65062306a36Sopenharmony_ci 65162306a36Sopenharmony_ci/** 65262306a36Sopenharmony_ci * iter_file_splice_write - splice data from a pipe to a file 65362306a36Sopenharmony_ci * @pipe: pipe info 65462306a36Sopenharmony_ci * @out: file to write to 65562306a36Sopenharmony_ci * @ppos: position in @out 65662306a36Sopenharmony_ci * @len: number of bytes to splice 65762306a36Sopenharmony_ci * @flags: splice modifier flags 65862306a36Sopenharmony_ci * 65962306a36Sopenharmony_ci * Description: 66062306a36Sopenharmony_ci * Will either move or copy pages (determined by @flags options) from 66162306a36Sopenharmony_ci * the given pipe inode to the given file. 66262306a36Sopenharmony_ci * This one is ->write_iter-based. 66362306a36Sopenharmony_ci * 66462306a36Sopenharmony_ci */ 66562306a36Sopenharmony_cissize_t 66662306a36Sopenharmony_ciiter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 66762306a36Sopenharmony_ci loff_t *ppos, size_t len, unsigned int flags) 66862306a36Sopenharmony_ci{ 66962306a36Sopenharmony_ci struct splice_desc sd = { 67062306a36Sopenharmony_ci .total_len = len, 67162306a36Sopenharmony_ci .flags = flags, 67262306a36Sopenharmony_ci .pos = *ppos, 67362306a36Sopenharmony_ci .u.file = out, 67462306a36Sopenharmony_ci }; 67562306a36Sopenharmony_ci int nbufs = pipe->max_usage; 67662306a36Sopenharmony_ci struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), 67762306a36Sopenharmony_ci GFP_KERNEL); 67862306a36Sopenharmony_ci ssize_t ret; 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci if (unlikely(!array)) 68162306a36Sopenharmony_ci return -ENOMEM; 68262306a36Sopenharmony_ci 68362306a36Sopenharmony_ci pipe_lock(pipe); 68462306a36Sopenharmony_ci 68562306a36Sopenharmony_ci splice_from_pipe_begin(&sd); 68662306a36Sopenharmony_ci while (sd.total_len) { 68762306a36Sopenharmony_ci struct iov_iter from; 68862306a36Sopenharmony_ci unsigned int head, tail, mask; 68962306a36Sopenharmony_ci size_t left; 69062306a36Sopenharmony_ci int n; 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci ret = splice_from_pipe_next(pipe, &sd); 69362306a36Sopenharmony_ci if (ret <= 0) 69462306a36Sopenharmony_ci break; 69562306a36Sopenharmony_ci 69662306a36Sopenharmony_ci if (unlikely(nbufs < pipe->max_usage)) { 69762306a36Sopenharmony_ci kfree(array); 69862306a36Sopenharmony_ci nbufs = pipe->max_usage; 69962306a36Sopenharmony_ci array = kcalloc(nbufs, sizeof(struct bio_vec), 70062306a36Sopenharmony_ci GFP_KERNEL); 70162306a36Sopenharmony_ci if (!array) { 70262306a36Sopenharmony_ci ret = -ENOMEM; 70362306a36Sopenharmony_ci break; 70462306a36Sopenharmony_ci } 70562306a36Sopenharmony_ci } 70662306a36Sopenharmony_ci 70762306a36Sopenharmony_ci head = pipe->head; 70862306a36Sopenharmony_ci tail = pipe->tail; 70962306a36Sopenharmony_ci mask = pipe->ring_size - 1; 71062306a36Sopenharmony_ci 71162306a36Sopenharmony_ci /* build the vector */ 71262306a36Sopenharmony_ci left = sd.total_len; 71362306a36Sopenharmony_ci for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) { 71462306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 71562306a36Sopenharmony_ci size_t this_len = buf->len; 71662306a36Sopenharmony_ci 71762306a36Sopenharmony_ci /* zero-length bvecs are not supported, skip them */ 71862306a36Sopenharmony_ci if (!this_len) 71962306a36Sopenharmony_ci continue; 72062306a36Sopenharmony_ci this_len = min(this_len, left); 72162306a36Sopenharmony_ci 72262306a36Sopenharmony_ci ret = pipe_buf_confirm(pipe, buf); 72362306a36Sopenharmony_ci if (unlikely(ret)) { 72462306a36Sopenharmony_ci if (ret == -ENODATA) 72562306a36Sopenharmony_ci ret = 0; 72662306a36Sopenharmony_ci goto done; 72762306a36Sopenharmony_ci } 72862306a36Sopenharmony_ci 72962306a36Sopenharmony_ci bvec_set_page(&array[n], buf->page, this_len, 73062306a36Sopenharmony_ci buf->offset); 73162306a36Sopenharmony_ci left -= this_len; 73262306a36Sopenharmony_ci n++; 73362306a36Sopenharmony_ci } 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_ci iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left); 73662306a36Sopenharmony_ci ret = vfs_iter_write(out, &from, &sd.pos, 0); 73762306a36Sopenharmony_ci if (ret <= 0) 73862306a36Sopenharmony_ci break; 73962306a36Sopenharmony_ci 74062306a36Sopenharmony_ci sd.num_spliced += ret; 74162306a36Sopenharmony_ci sd.total_len -= ret; 74262306a36Sopenharmony_ci *ppos = sd.pos; 74362306a36Sopenharmony_ci 74462306a36Sopenharmony_ci /* dismiss the fully eaten buffers, adjust the partial one */ 74562306a36Sopenharmony_ci tail = pipe->tail; 74662306a36Sopenharmony_ci while (ret) { 74762306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 74862306a36Sopenharmony_ci if (ret >= buf->len) { 74962306a36Sopenharmony_ci ret -= buf->len; 75062306a36Sopenharmony_ci buf->len = 0; 75162306a36Sopenharmony_ci pipe_buf_release(pipe, buf); 75262306a36Sopenharmony_ci tail++; 75362306a36Sopenharmony_ci pipe->tail = tail; 75462306a36Sopenharmony_ci if (pipe->files) 75562306a36Sopenharmony_ci sd.need_wakeup = true; 75662306a36Sopenharmony_ci } else { 75762306a36Sopenharmony_ci buf->offset += ret; 75862306a36Sopenharmony_ci buf->len -= ret; 75962306a36Sopenharmony_ci ret = 0; 76062306a36Sopenharmony_ci } 76162306a36Sopenharmony_ci } 76262306a36Sopenharmony_ci } 76362306a36Sopenharmony_cidone: 76462306a36Sopenharmony_ci kfree(array); 76562306a36Sopenharmony_ci splice_from_pipe_end(pipe, &sd); 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_ci pipe_unlock(pipe); 76862306a36Sopenharmony_ci 76962306a36Sopenharmony_ci if (sd.num_spliced) 77062306a36Sopenharmony_ci ret = sd.num_spliced; 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ci return ret; 77362306a36Sopenharmony_ci} 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_ciEXPORT_SYMBOL(iter_file_splice_write); 77662306a36Sopenharmony_ci 77762306a36Sopenharmony_ci#ifdef CONFIG_NET 77862306a36Sopenharmony_ci/** 77962306a36Sopenharmony_ci * splice_to_socket - splice data from a pipe to a socket 78062306a36Sopenharmony_ci * @pipe: pipe to splice from 78162306a36Sopenharmony_ci * @out: socket to write to 78262306a36Sopenharmony_ci * @ppos: position in @out 78362306a36Sopenharmony_ci * @len: number of bytes to splice 78462306a36Sopenharmony_ci * @flags: splice modifier flags 78562306a36Sopenharmony_ci * 78662306a36Sopenharmony_ci * Description: 78762306a36Sopenharmony_ci * Will send @len bytes from the pipe to a network socket. No data copying 78862306a36Sopenharmony_ci * is involved. 78962306a36Sopenharmony_ci * 79062306a36Sopenharmony_ci */ 79162306a36Sopenharmony_cissize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out, 79262306a36Sopenharmony_ci loff_t *ppos, size_t len, unsigned int flags) 79362306a36Sopenharmony_ci{ 79462306a36Sopenharmony_ci struct socket *sock = sock_from_file(out); 79562306a36Sopenharmony_ci struct bio_vec bvec[16]; 79662306a36Sopenharmony_ci struct msghdr msg = {}; 79762306a36Sopenharmony_ci ssize_t ret = 0; 79862306a36Sopenharmony_ci size_t spliced = 0; 79962306a36Sopenharmony_ci bool need_wakeup = false; 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci pipe_lock(pipe); 80262306a36Sopenharmony_ci 80362306a36Sopenharmony_ci while (len > 0) { 80462306a36Sopenharmony_ci unsigned int head, tail, mask, bc = 0; 80562306a36Sopenharmony_ci size_t remain = len; 80662306a36Sopenharmony_ci 80762306a36Sopenharmony_ci /* 80862306a36Sopenharmony_ci * Check for signal early to make process killable when there 80962306a36Sopenharmony_ci * are always buffers available 81062306a36Sopenharmony_ci */ 81162306a36Sopenharmony_ci ret = -ERESTARTSYS; 81262306a36Sopenharmony_ci if (signal_pending(current)) 81362306a36Sopenharmony_ci break; 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci while (pipe_empty(pipe->head, pipe->tail)) { 81662306a36Sopenharmony_ci ret = 0; 81762306a36Sopenharmony_ci if (!pipe->writers) 81862306a36Sopenharmony_ci goto out; 81962306a36Sopenharmony_ci 82062306a36Sopenharmony_ci if (spliced) 82162306a36Sopenharmony_ci goto out; 82262306a36Sopenharmony_ci 82362306a36Sopenharmony_ci ret = -EAGAIN; 82462306a36Sopenharmony_ci if (flags & SPLICE_F_NONBLOCK) 82562306a36Sopenharmony_ci goto out; 82662306a36Sopenharmony_ci 82762306a36Sopenharmony_ci ret = -ERESTARTSYS; 82862306a36Sopenharmony_ci if (signal_pending(current)) 82962306a36Sopenharmony_ci goto out; 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci if (need_wakeup) { 83262306a36Sopenharmony_ci wakeup_pipe_writers(pipe); 83362306a36Sopenharmony_ci need_wakeup = false; 83462306a36Sopenharmony_ci } 83562306a36Sopenharmony_ci 83662306a36Sopenharmony_ci pipe_wait_readable(pipe); 83762306a36Sopenharmony_ci } 83862306a36Sopenharmony_ci 83962306a36Sopenharmony_ci head = pipe->head; 84062306a36Sopenharmony_ci tail = pipe->tail; 84162306a36Sopenharmony_ci mask = pipe->ring_size - 1; 84262306a36Sopenharmony_ci 84362306a36Sopenharmony_ci while (!pipe_empty(head, tail)) { 84462306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 84562306a36Sopenharmony_ci size_t seg; 84662306a36Sopenharmony_ci 84762306a36Sopenharmony_ci if (!buf->len) { 84862306a36Sopenharmony_ci tail++; 84962306a36Sopenharmony_ci continue; 85062306a36Sopenharmony_ci } 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci seg = min_t(size_t, remain, buf->len); 85362306a36Sopenharmony_ci 85462306a36Sopenharmony_ci ret = pipe_buf_confirm(pipe, buf); 85562306a36Sopenharmony_ci if (unlikely(ret)) { 85662306a36Sopenharmony_ci if (ret == -ENODATA) 85762306a36Sopenharmony_ci ret = 0; 85862306a36Sopenharmony_ci break; 85962306a36Sopenharmony_ci } 86062306a36Sopenharmony_ci 86162306a36Sopenharmony_ci bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset); 86262306a36Sopenharmony_ci remain -= seg; 86362306a36Sopenharmony_ci if (remain == 0 || bc >= ARRAY_SIZE(bvec)) 86462306a36Sopenharmony_ci break; 86562306a36Sopenharmony_ci tail++; 86662306a36Sopenharmony_ci } 86762306a36Sopenharmony_ci 86862306a36Sopenharmony_ci if (!bc) 86962306a36Sopenharmony_ci break; 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci msg.msg_flags = MSG_SPLICE_PAGES; 87262306a36Sopenharmony_ci if (flags & SPLICE_F_MORE) 87362306a36Sopenharmony_ci msg.msg_flags |= MSG_MORE; 87462306a36Sopenharmony_ci if (remain && pipe_occupancy(pipe->head, tail) > 0) 87562306a36Sopenharmony_ci msg.msg_flags |= MSG_MORE; 87662306a36Sopenharmony_ci if (out->f_flags & O_NONBLOCK) 87762306a36Sopenharmony_ci msg.msg_flags |= MSG_DONTWAIT; 87862306a36Sopenharmony_ci 87962306a36Sopenharmony_ci iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc, 88062306a36Sopenharmony_ci len - remain); 88162306a36Sopenharmony_ci ret = sock_sendmsg(sock, &msg); 88262306a36Sopenharmony_ci if (ret <= 0) 88362306a36Sopenharmony_ci break; 88462306a36Sopenharmony_ci 88562306a36Sopenharmony_ci spliced += ret; 88662306a36Sopenharmony_ci len -= ret; 88762306a36Sopenharmony_ci tail = pipe->tail; 88862306a36Sopenharmony_ci while (ret > 0) { 88962306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[tail & mask]; 89062306a36Sopenharmony_ci size_t seg = min_t(size_t, ret, buf->len); 89162306a36Sopenharmony_ci 89262306a36Sopenharmony_ci buf->offset += seg; 89362306a36Sopenharmony_ci buf->len -= seg; 89462306a36Sopenharmony_ci ret -= seg; 89562306a36Sopenharmony_ci 89662306a36Sopenharmony_ci if (!buf->len) { 89762306a36Sopenharmony_ci pipe_buf_release(pipe, buf); 89862306a36Sopenharmony_ci tail++; 89962306a36Sopenharmony_ci } 90062306a36Sopenharmony_ci } 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_ci if (tail != pipe->tail) { 90362306a36Sopenharmony_ci pipe->tail = tail; 90462306a36Sopenharmony_ci if (pipe->files) 90562306a36Sopenharmony_ci need_wakeup = true; 90662306a36Sopenharmony_ci } 90762306a36Sopenharmony_ci } 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ciout: 91062306a36Sopenharmony_ci pipe_unlock(pipe); 91162306a36Sopenharmony_ci if (need_wakeup) 91262306a36Sopenharmony_ci wakeup_pipe_writers(pipe); 91362306a36Sopenharmony_ci return spliced ?: ret; 91462306a36Sopenharmony_ci} 91562306a36Sopenharmony_ci#endif 91662306a36Sopenharmony_ci 91762306a36Sopenharmony_cistatic int warn_unsupported(struct file *file, const char *op) 91862306a36Sopenharmony_ci{ 91962306a36Sopenharmony_ci pr_debug_ratelimited( 92062306a36Sopenharmony_ci "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n", 92162306a36Sopenharmony_ci op, file, current->pid, current->comm); 92262306a36Sopenharmony_ci return -EINVAL; 92362306a36Sopenharmony_ci} 92462306a36Sopenharmony_ci 92562306a36Sopenharmony_ci/* 92662306a36Sopenharmony_ci * Attempt to initiate a splice from pipe to file. 92762306a36Sopenharmony_ci */ 92862306a36Sopenharmony_cistatic long do_splice_from(struct pipe_inode_info *pipe, struct file *out, 92962306a36Sopenharmony_ci loff_t *ppos, size_t len, unsigned int flags) 93062306a36Sopenharmony_ci{ 93162306a36Sopenharmony_ci if (unlikely(!out->f_op->splice_write)) 93262306a36Sopenharmony_ci return warn_unsupported(out, "write"); 93362306a36Sopenharmony_ci return out->f_op->splice_write(pipe, out, ppos, len, flags); 93462306a36Sopenharmony_ci} 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci/* 93762306a36Sopenharmony_ci * Indicate to the caller that there was a premature EOF when reading from the 93862306a36Sopenharmony_ci * source and the caller didn't indicate they would be sending more data after 93962306a36Sopenharmony_ci * this. 94062306a36Sopenharmony_ci */ 94162306a36Sopenharmony_cistatic void do_splice_eof(struct splice_desc *sd) 94262306a36Sopenharmony_ci{ 94362306a36Sopenharmony_ci if (sd->splice_eof) 94462306a36Sopenharmony_ci sd->splice_eof(sd); 94562306a36Sopenharmony_ci} 94662306a36Sopenharmony_ci 94762306a36Sopenharmony_ci/** 94862306a36Sopenharmony_ci * vfs_splice_read - Read data from a file and splice it into a pipe 94962306a36Sopenharmony_ci * @in: File to splice from 95062306a36Sopenharmony_ci * @ppos: Input file offset 95162306a36Sopenharmony_ci * @pipe: Pipe to splice to 95262306a36Sopenharmony_ci * @len: Number of bytes to splice 95362306a36Sopenharmony_ci * @flags: Splice modifier flags (SPLICE_F_*) 95462306a36Sopenharmony_ci * 95562306a36Sopenharmony_ci * Splice the requested amount of data from the input file to the pipe. This 95662306a36Sopenharmony_ci * is synchronous as the caller must hold the pipe lock across the entire 95762306a36Sopenharmony_ci * operation. 95862306a36Sopenharmony_ci * 95962306a36Sopenharmony_ci * If successful, it returns the amount of data spliced, 0 if it hit the EOF or 96062306a36Sopenharmony_ci * a hole and a negative error code otherwise. 96162306a36Sopenharmony_ci */ 96262306a36Sopenharmony_cilong vfs_splice_read(struct file *in, loff_t *ppos, 96362306a36Sopenharmony_ci struct pipe_inode_info *pipe, size_t len, 96462306a36Sopenharmony_ci unsigned int flags) 96562306a36Sopenharmony_ci{ 96662306a36Sopenharmony_ci unsigned int p_space; 96762306a36Sopenharmony_ci int ret; 96862306a36Sopenharmony_ci 96962306a36Sopenharmony_ci if (unlikely(!(in->f_mode & FMODE_READ))) 97062306a36Sopenharmony_ci return -EBADF; 97162306a36Sopenharmony_ci if (!len) 97262306a36Sopenharmony_ci return 0; 97362306a36Sopenharmony_ci 97462306a36Sopenharmony_ci /* Don't try to read more the pipe has space for. */ 97562306a36Sopenharmony_ci p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail); 97662306a36Sopenharmony_ci len = min_t(size_t, len, p_space << PAGE_SHIFT); 97762306a36Sopenharmony_ci 97862306a36Sopenharmony_ci ret = rw_verify_area(READ, in, ppos, len); 97962306a36Sopenharmony_ci if (unlikely(ret < 0)) 98062306a36Sopenharmony_ci return ret; 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci if (unlikely(len > MAX_RW_COUNT)) 98362306a36Sopenharmony_ci len = MAX_RW_COUNT; 98462306a36Sopenharmony_ci 98562306a36Sopenharmony_ci if (unlikely(!in->f_op->splice_read)) 98662306a36Sopenharmony_ci return warn_unsupported(in, "read"); 98762306a36Sopenharmony_ci /* 98862306a36Sopenharmony_ci * O_DIRECT and DAX don't deal with the pagecache, so we allocate a 98962306a36Sopenharmony_ci * buffer, copy into it and splice that into the pipe. 99062306a36Sopenharmony_ci */ 99162306a36Sopenharmony_ci if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host)) 99262306a36Sopenharmony_ci return copy_splice_read(in, ppos, pipe, len, flags); 99362306a36Sopenharmony_ci return in->f_op->splice_read(in, ppos, pipe, len, flags); 99462306a36Sopenharmony_ci} 99562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(vfs_splice_read); 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ci/** 99862306a36Sopenharmony_ci * splice_direct_to_actor - splices data directly between two non-pipes 99962306a36Sopenharmony_ci * @in: file to splice from 100062306a36Sopenharmony_ci * @sd: actor information on where to splice to 100162306a36Sopenharmony_ci * @actor: handles the data splicing 100262306a36Sopenharmony_ci * 100362306a36Sopenharmony_ci * Description: 100462306a36Sopenharmony_ci * This is a special case helper to splice directly between two 100562306a36Sopenharmony_ci * points, without requiring an explicit pipe. Internally an allocated 100662306a36Sopenharmony_ci * pipe is cached in the process, and reused during the lifetime of 100762306a36Sopenharmony_ci * that process. 100862306a36Sopenharmony_ci * 100962306a36Sopenharmony_ci */ 101062306a36Sopenharmony_cissize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, 101162306a36Sopenharmony_ci splice_direct_actor *actor) 101262306a36Sopenharmony_ci{ 101362306a36Sopenharmony_ci struct pipe_inode_info *pipe; 101462306a36Sopenharmony_ci long ret, bytes; 101562306a36Sopenharmony_ci size_t len; 101662306a36Sopenharmony_ci int i, flags, more; 101762306a36Sopenharmony_ci 101862306a36Sopenharmony_ci /* 101962306a36Sopenharmony_ci * We require the input to be seekable, as we don't want to randomly 102062306a36Sopenharmony_ci * drop data for eg socket -> socket splicing. Use the piped splicing 102162306a36Sopenharmony_ci * for that! 102262306a36Sopenharmony_ci */ 102362306a36Sopenharmony_ci if (unlikely(!(in->f_mode & FMODE_LSEEK))) 102462306a36Sopenharmony_ci return -EINVAL; 102562306a36Sopenharmony_ci 102662306a36Sopenharmony_ci /* 102762306a36Sopenharmony_ci * neither in nor out is a pipe, setup an internal pipe attached to 102862306a36Sopenharmony_ci * 'out' and transfer the wanted data from 'in' to 'out' through that 102962306a36Sopenharmony_ci */ 103062306a36Sopenharmony_ci pipe = current->splice_pipe; 103162306a36Sopenharmony_ci if (unlikely(!pipe)) { 103262306a36Sopenharmony_ci pipe = alloc_pipe_info(); 103362306a36Sopenharmony_ci if (!pipe) 103462306a36Sopenharmony_ci return -ENOMEM; 103562306a36Sopenharmony_ci 103662306a36Sopenharmony_ci /* 103762306a36Sopenharmony_ci * We don't have an immediate reader, but we'll read the stuff 103862306a36Sopenharmony_ci * out of the pipe right after the splice_to_pipe(). So set 103962306a36Sopenharmony_ci * PIPE_READERS appropriately. 104062306a36Sopenharmony_ci */ 104162306a36Sopenharmony_ci pipe->readers = 1; 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci current->splice_pipe = pipe; 104462306a36Sopenharmony_ci } 104562306a36Sopenharmony_ci 104662306a36Sopenharmony_ci /* 104762306a36Sopenharmony_ci * Do the splice. 104862306a36Sopenharmony_ci */ 104962306a36Sopenharmony_ci bytes = 0; 105062306a36Sopenharmony_ci len = sd->total_len; 105162306a36Sopenharmony_ci 105262306a36Sopenharmony_ci /* Don't block on output, we have to drain the direct pipe. */ 105362306a36Sopenharmony_ci flags = sd->flags; 105462306a36Sopenharmony_ci sd->flags &= ~SPLICE_F_NONBLOCK; 105562306a36Sopenharmony_ci 105662306a36Sopenharmony_ci /* 105762306a36Sopenharmony_ci * We signal MORE until we've read sufficient data to fulfill the 105862306a36Sopenharmony_ci * request and we keep signalling it if the caller set it. 105962306a36Sopenharmony_ci */ 106062306a36Sopenharmony_ci more = sd->flags & SPLICE_F_MORE; 106162306a36Sopenharmony_ci sd->flags |= SPLICE_F_MORE; 106262306a36Sopenharmony_ci 106362306a36Sopenharmony_ci WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); 106462306a36Sopenharmony_ci 106562306a36Sopenharmony_ci while (len) { 106662306a36Sopenharmony_ci size_t read_len; 106762306a36Sopenharmony_ci loff_t pos = sd->pos, prev_pos = pos; 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_ci ret = vfs_splice_read(in, &pos, pipe, len, flags); 107062306a36Sopenharmony_ci if (unlikely(ret <= 0)) 107162306a36Sopenharmony_ci goto read_failure; 107262306a36Sopenharmony_ci 107362306a36Sopenharmony_ci read_len = ret; 107462306a36Sopenharmony_ci sd->total_len = read_len; 107562306a36Sopenharmony_ci 107662306a36Sopenharmony_ci /* 107762306a36Sopenharmony_ci * If we now have sufficient data to fulfill the request then 107862306a36Sopenharmony_ci * we clear SPLICE_F_MORE if it was not set initially. 107962306a36Sopenharmony_ci */ 108062306a36Sopenharmony_ci if (read_len >= len && !more) 108162306a36Sopenharmony_ci sd->flags &= ~SPLICE_F_MORE; 108262306a36Sopenharmony_ci 108362306a36Sopenharmony_ci /* 108462306a36Sopenharmony_ci * NOTE: nonblocking mode only applies to the input. We 108562306a36Sopenharmony_ci * must not do the output in nonblocking mode as then we 108662306a36Sopenharmony_ci * could get stuck data in the internal pipe: 108762306a36Sopenharmony_ci */ 108862306a36Sopenharmony_ci ret = actor(pipe, sd); 108962306a36Sopenharmony_ci if (unlikely(ret <= 0)) { 109062306a36Sopenharmony_ci sd->pos = prev_pos; 109162306a36Sopenharmony_ci goto out_release; 109262306a36Sopenharmony_ci } 109362306a36Sopenharmony_ci 109462306a36Sopenharmony_ci bytes += ret; 109562306a36Sopenharmony_ci len -= ret; 109662306a36Sopenharmony_ci sd->pos = pos; 109762306a36Sopenharmony_ci 109862306a36Sopenharmony_ci if (ret < read_len) { 109962306a36Sopenharmony_ci sd->pos = prev_pos + ret; 110062306a36Sopenharmony_ci goto out_release; 110162306a36Sopenharmony_ci } 110262306a36Sopenharmony_ci } 110362306a36Sopenharmony_ci 110462306a36Sopenharmony_cidone: 110562306a36Sopenharmony_ci pipe->tail = pipe->head = 0; 110662306a36Sopenharmony_ci file_accessed(in); 110762306a36Sopenharmony_ci return bytes; 110862306a36Sopenharmony_ci 110962306a36Sopenharmony_ciread_failure: 111062306a36Sopenharmony_ci /* 111162306a36Sopenharmony_ci * If the user did *not* set SPLICE_F_MORE *and* we didn't hit that 111262306a36Sopenharmony_ci * "use all of len" case that cleared SPLICE_F_MORE, *and* we did a 111362306a36Sopenharmony_ci * "->splice_in()" that returned EOF (ie zero) *and* we have sent at 111462306a36Sopenharmony_ci * least 1 byte *then* we will also do the ->splice_eof() call. 111562306a36Sopenharmony_ci */ 111662306a36Sopenharmony_ci if (ret == 0 && !more && len > 0 && bytes) 111762306a36Sopenharmony_ci do_splice_eof(sd); 111862306a36Sopenharmony_ciout_release: 111962306a36Sopenharmony_ci /* 112062306a36Sopenharmony_ci * If we did an incomplete transfer we must release 112162306a36Sopenharmony_ci * the pipe buffers in question: 112262306a36Sopenharmony_ci */ 112362306a36Sopenharmony_ci for (i = 0; i < pipe->ring_size; i++) { 112462306a36Sopenharmony_ci struct pipe_buffer *buf = &pipe->bufs[i]; 112562306a36Sopenharmony_ci 112662306a36Sopenharmony_ci if (buf->ops) 112762306a36Sopenharmony_ci pipe_buf_release(pipe, buf); 112862306a36Sopenharmony_ci } 112962306a36Sopenharmony_ci 113062306a36Sopenharmony_ci if (!bytes) 113162306a36Sopenharmony_ci bytes = ret; 113262306a36Sopenharmony_ci 113362306a36Sopenharmony_ci goto done; 113462306a36Sopenharmony_ci} 113562306a36Sopenharmony_ciEXPORT_SYMBOL(splice_direct_to_actor); 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_cistatic int direct_splice_actor(struct pipe_inode_info *pipe, 113862306a36Sopenharmony_ci struct splice_desc *sd) 113962306a36Sopenharmony_ci{ 114062306a36Sopenharmony_ci struct file *file = sd->u.file; 114162306a36Sopenharmony_ci 114262306a36Sopenharmony_ci return do_splice_from(pipe, file, sd->opos, sd->total_len, 114362306a36Sopenharmony_ci sd->flags); 114462306a36Sopenharmony_ci} 114562306a36Sopenharmony_ci 114662306a36Sopenharmony_cistatic void direct_file_splice_eof(struct splice_desc *sd) 114762306a36Sopenharmony_ci{ 114862306a36Sopenharmony_ci struct file *file = sd->u.file; 114962306a36Sopenharmony_ci 115062306a36Sopenharmony_ci if (file->f_op->splice_eof) 115162306a36Sopenharmony_ci file->f_op->splice_eof(file); 115262306a36Sopenharmony_ci} 115362306a36Sopenharmony_ci 115462306a36Sopenharmony_ci/** 115562306a36Sopenharmony_ci * do_splice_direct - splices data directly between two files 115662306a36Sopenharmony_ci * @in: file to splice from 115762306a36Sopenharmony_ci * @ppos: input file offset 115862306a36Sopenharmony_ci * @out: file to splice to 115962306a36Sopenharmony_ci * @opos: output file offset 116062306a36Sopenharmony_ci * @len: number of bytes to splice 116162306a36Sopenharmony_ci * @flags: splice modifier flags 116262306a36Sopenharmony_ci * 116362306a36Sopenharmony_ci * Description: 116462306a36Sopenharmony_ci * For use by do_sendfile(). splice can easily emulate sendfile, but 116562306a36Sopenharmony_ci * doing it in the application would incur an extra system call 116662306a36Sopenharmony_ci * (splice in + splice out, as compared to just sendfile()). So this helper 116762306a36Sopenharmony_ci * can splice directly through a process-private pipe. 116862306a36Sopenharmony_ci * 116962306a36Sopenharmony_ci */ 117062306a36Sopenharmony_cilong do_splice_direct(struct file *in, loff_t *ppos, struct file *out, 117162306a36Sopenharmony_ci loff_t *opos, size_t len, unsigned int flags) 117262306a36Sopenharmony_ci{ 117362306a36Sopenharmony_ci struct splice_desc sd = { 117462306a36Sopenharmony_ci .len = len, 117562306a36Sopenharmony_ci .total_len = len, 117662306a36Sopenharmony_ci .flags = flags, 117762306a36Sopenharmony_ci .pos = *ppos, 117862306a36Sopenharmony_ci .u.file = out, 117962306a36Sopenharmony_ci .splice_eof = direct_file_splice_eof, 118062306a36Sopenharmony_ci .opos = opos, 118162306a36Sopenharmony_ci }; 118262306a36Sopenharmony_ci long ret; 118362306a36Sopenharmony_ci 118462306a36Sopenharmony_ci if (unlikely(!(out->f_mode & FMODE_WRITE))) 118562306a36Sopenharmony_ci return -EBADF; 118662306a36Sopenharmony_ci 118762306a36Sopenharmony_ci if (unlikely(out->f_flags & O_APPEND)) 118862306a36Sopenharmony_ci return -EINVAL; 118962306a36Sopenharmony_ci 119062306a36Sopenharmony_ci ret = rw_verify_area(WRITE, out, opos, len); 119162306a36Sopenharmony_ci if (unlikely(ret < 0)) 119262306a36Sopenharmony_ci return ret; 119362306a36Sopenharmony_ci 119462306a36Sopenharmony_ci ret = splice_direct_to_actor(in, &sd, direct_splice_actor); 119562306a36Sopenharmony_ci if (ret > 0) 119662306a36Sopenharmony_ci *ppos = sd.pos; 119762306a36Sopenharmony_ci 119862306a36Sopenharmony_ci return ret; 119962306a36Sopenharmony_ci} 120062306a36Sopenharmony_ciEXPORT_SYMBOL(do_splice_direct); 120162306a36Sopenharmony_ci 120262306a36Sopenharmony_cistatic int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) 120362306a36Sopenharmony_ci{ 120462306a36Sopenharmony_ci for (;;) { 120562306a36Sopenharmony_ci if (unlikely(!pipe->readers)) { 120662306a36Sopenharmony_ci send_sig(SIGPIPE, current, 0); 120762306a36Sopenharmony_ci return -EPIPE; 120862306a36Sopenharmony_ci } 120962306a36Sopenharmony_ci if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 121062306a36Sopenharmony_ci return 0; 121162306a36Sopenharmony_ci if (flags & SPLICE_F_NONBLOCK) 121262306a36Sopenharmony_ci return -EAGAIN; 121362306a36Sopenharmony_ci if (signal_pending(current)) 121462306a36Sopenharmony_ci return -ERESTARTSYS; 121562306a36Sopenharmony_ci pipe_wait_writable(pipe); 121662306a36Sopenharmony_ci } 121762306a36Sopenharmony_ci} 121862306a36Sopenharmony_ci 121962306a36Sopenharmony_cistatic int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 122062306a36Sopenharmony_ci struct pipe_inode_info *opipe, 122162306a36Sopenharmony_ci size_t len, unsigned int flags); 122262306a36Sopenharmony_ci 122362306a36Sopenharmony_cilong splice_file_to_pipe(struct file *in, 122462306a36Sopenharmony_ci struct pipe_inode_info *opipe, 122562306a36Sopenharmony_ci loff_t *offset, 122662306a36Sopenharmony_ci size_t len, unsigned int flags) 122762306a36Sopenharmony_ci{ 122862306a36Sopenharmony_ci long ret; 122962306a36Sopenharmony_ci 123062306a36Sopenharmony_ci pipe_lock(opipe); 123162306a36Sopenharmony_ci ret = wait_for_space(opipe, flags); 123262306a36Sopenharmony_ci if (!ret) 123362306a36Sopenharmony_ci ret = vfs_splice_read(in, offset, opipe, len, flags); 123462306a36Sopenharmony_ci pipe_unlock(opipe); 123562306a36Sopenharmony_ci if (ret > 0) 123662306a36Sopenharmony_ci wakeup_pipe_readers(opipe); 123762306a36Sopenharmony_ci return ret; 123862306a36Sopenharmony_ci} 123962306a36Sopenharmony_ci 124062306a36Sopenharmony_ci/* 124162306a36Sopenharmony_ci * Determine where to splice to/from. 124262306a36Sopenharmony_ci */ 124362306a36Sopenharmony_cilong do_splice(struct file *in, loff_t *off_in, struct file *out, 124462306a36Sopenharmony_ci loff_t *off_out, size_t len, unsigned int flags) 124562306a36Sopenharmony_ci{ 124662306a36Sopenharmony_ci struct pipe_inode_info *ipipe; 124762306a36Sopenharmony_ci struct pipe_inode_info *opipe; 124862306a36Sopenharmony_ci loff_t offset; 124962306a36Sopenharmony_ci long ret; 125062306a36Sopenharmony_ci 125162306a36Sopenharmony_ci if (unlikely(!(in->f_mode & FMODE_READ) || 125262306a36Sopenharmony_ci !(out->f_mode & FMODE_WRITE))) 125362306a36Sopenharmony_ci return -EBADF; 125462306a36Sopenharmony_ci 125562306a36Sopenharmony_ci ipipe = get_pipe_info(in, true); 125662306a36Sopenharmony_ci opipe = get_pipe_info(out, true); 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci if (ipipe && opipe) { 125962306a36Sopenharmony_ci if (off_in || off_out) 126062306a36Sopenharmony_ci return -ESPIPE; 126162306a36Sopenharmony_ci 126262306a36Sopenharmony_ci /* Splicing to self would be fun, but... */ 126362306a36Sopenharmony_ci if (ipipe == opipe) 126462306a36Sopenharmony_ci return -EINVAL; 126562306a36Sopenharmony_ci 126662306a36Sopenharmony_ci if ((in->f_flags | out->f_flags) & O_NONBLOCK) 126762306a36Sopenharmony_ci flags |= SPLICE_F_NONBLOCK; 126862306a36Sopenharmony_ci 126962306a36Sopenharmony_ci ret = splice_pipe_to_pipe(ipipe, opipe, len, flags); 127062306a36Sopenharmony_ci } else if (ipipe) { 127162306a36Sopenharmony_ci if (off_in) 127262306a36Sopenharmony_ci return -ESPIPE; 127362306a36Sopenharmony_ci if (off_out) { 127462306a36Sopenharmony_ci if (!(out->f_mode & FMODE_PWRITE)) 127562306a36Sopenharmony_ci return -EINVAL; 127662306a36Sopenharmony_ci offset = *off_out; 127762306a36Sopenharmony_ci } else { 127862306a36Sopenharmony_ci offset = out->f_pos; 127962306a36Sopenharmony_ci } 128062306a36Sopenharmony_ci 128162306a36Sopenharmony_ci if (unlikely(out->f_flags & O_APPEND)) 128262306a36Sopenharmony_ci return -EINVAL; 128362306a36Sopenharmony_ci 128462306a36Sopenharmony_ci ret = rw_verify_area(WRITE, out, &offset, len); 128562306a36Sopenharmony_ci if (unlikely(ret < 0)) 128662306a36Sopenharmony_ci return ret; 128762306a36Sopenharmony_ci 128862306a36Sopenharmony_ci if (in->f_flags & O_NONBLOCK) 128962306a36Sopenharmony_ci flags |= SPLICE_F_NONBLOCK; 129062306a36Sopenharmony_ci 129162306a36Sopenharmony_ci file_start_write(out); 129262306a36Sopenharmony_ci ret = do_splice_from(ipipe, out, &offset, len, flags); 129362306a36Sopenharmony_ci file_end_write(out); 129462306a36Sopenharmony_ci 129562306a36Sopenharmony_ci if (!off_out) 129662306a36Sopenharmony_ci out->f_pos = offset; 129762306a36Sopenharmony_ci else 129862306a36Sopenharmony_ci *off_out = offset; 129962306a36Sopenharmony_ci } else if (opipe) { 130062306a36Sopenharmony_ci if (off_out) 130162306a36Sopenharmony_ci return -ESPIPE; 130262306a36Sopenharmony_ci if (off_in) { 130362306a36Sopenharmony_ci if (!(in->f_mode & FMODE_PREAD)) 130462306a36Sopenharmony_ci return -EINVAL; 130562306a36Sopenharmony_ci offset = *off_in; 130662306a36Sopenharmony_ci } else { 130762306a36Sopenharmony_ci offset = in->f_pos; 130862306a36Sopenharmony_ci } 130962306a36Sopenharmony_ci 131062306a36Sopenharmony_ci if (out->f_flags & O_NONBLOCK) 131162306a36Sopenharmony_ci flags |= SPLICE_F_NONBLOCK; 131262306a36Sopenharmony_ci 131362306a36Sopenharmony_ci ret = splice_file_to_pipe(in, opipe, &offset, len, flags); 131462306a36Sopenharmony_ci 131562306a36Sopenharmony_ci if (!off_in) 131662306a36Sopenharmony_ci in->f_pos = offset; 131762306a36Sopenharmony_ci else 131862306a36Sopenharmony_ci *off_in = offset; 131962306a36Sopenharmony_ci } else { 132062306a36Sopenharmony_ci ret = -EINVAL; 132162306a36Sopenharmony_ci } 132262306a36Sopenharmony_ci 132362306a36Sopenharmony_ci if (ret > 0) { 132462306a36Sopenharmony_ci /* 132562306a36Sopenharmony_ci * Generate modify out before access in: 132662306a36Sopenharmony_ci * do_splice_from() may've already sent modify out, 132762306a36Sopenharmony_ci * and this ensures the events get merged. 132862306a36Sopenharmony_ci */ 132962306a36Sopenharmony_ci fsnotify_modify(out); 133062306a36Sopenharmony_ci fsnotify_access(in); 133162306a36Sopenharmony_ci } 133262306a36Sopenharmony_ci 133362306a36Sopenharmony_ci return ret; 133462306a36Sopenharmony_ci} 133562306a36Sopenharmony_ci 133662306a36Sopenharmony_cistatic long __do_splice(struct file *in, loff_t __user *off_in, 133762306a36Sopenharmony_ci struct file *out, loff_t __user *off_out, 133862306a36Sopenharmony_ci size_t len, unsigned int flags) 133962306a36Sopenharmony_ci{ 134062306a36Sopenharmony_ci struct pipe_inode_info *ipipe; 134162306a36Sopenharmony_ci struct pipe_inode_info *opipe; 134262306a36Sopenharmony_ci loff_t offset, *__off_in = NULL, *__off_out = NULL; 134362306a36Sopenharmony_ci long ret; 134462306a36Sopenharmony_ci 134562306a36Sopenharmony_ci ipipe = get_pipe_info(in, true); 134662306a36Sopenharmony_ci opipe = get_pipe_info(out, true); 134762306a36Sopenharmony_ci 134862306a36Sopenharmony_ci if (ipipe) { 134962306a36Sopenharmony_ci if (off_in) 135062306a36Sopenharmony_ci return -ESPIPE; 135162306a36Sopenharmony_ci pipe_clear_nowait(in); 135262306a36Sopenharmony_ci } 135362306a36Sopenharmony_ci if (opipe) { 135462306a36Sopenharmony_ci if (off_out) 135562306a36Sopenharmony_ci return -ESPIPE; 135662306a36Sopenharmony_ci pipe_clear_nowait(out); 135762306a36Sopenharmony_ci } 135862306a36Sopenharmony_ci 135962306a36Sopenharmony_ci if (off_out) { 136062306a36Sopenharmony_ci if (copy_from_user(&offset, off_out, sizeof(loff_t))) 136162306a36Sopenharmony_ci return -EFAULT; 136262306a36Sopenharmony_ci __off_out = &offset; 136362306a36Sopenharmony_ci } 136462306a36Sopenharmony_ci if (off_in) { 136562306a36Sopenharmony_ci if (copy_from_user(&offset, off_in, sizeof(loff_t))) 136662306a36Sopenharmony_ci return -EFAULT; 136762306a36Sopenharmony_ci __off_in = &offset; 136862306a36Sopenharmony_ci } 136962306a36Sopenharmony_ci 137062306a36Sopenharmony_ci ret = do_splice(in, __off_in, out, __off_out, len, flags); 137162306a36Sopenharmony_ci if (ret < 0) 137262306a36Sopenharmony_ci return ret; 137362306a36Sopenharmony_ci 137462306a36Sopenharmony_ci if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t))) 137562306a36Sopenharmony_ci return -EFAULT; 137662306a36Sopenharmony_ci if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t))) 137762306a36Sopenharmony_ci return -EFAULT; 137862306a36Sopenharmony_ci 137962306a36Sopenharmony_ci return ret; 138062306a36Sopenharmony_ci} 138162306a36Sopenharmony_ci 138262306a36Sopenharmony_cistatic int iter_to_pipe(struct iov_iter *from, 138362306a36Sopenharmony_ci struct pipe_inode_info *pipe, 138462306a36Sopenharmony_ci unsigned flags) 138562306a36Sopenharmony_ci{ 138662306a36Sopenharmony_ci struct pipe_buffer buf = { 138762306a36Sopenharmony_ci .ops = &user_page_pipe_buf_ops, 138862306a36Sopenharmony_ci .flags = flags 138962306a36Sopenharmony_ci }; 139062306a36Sopenharmony_ci size_t total = 0; 139162306a36Sopenharmony_ci int ret = 0; 139262306a36Sopenharmony_ci 139362306a36Sopenharmony_ci while (iov_iter_count(from)) { 139462306a36Sopenharmony_ci struct page *pages[16]; 139562306a36Sopenharmony_ci ssize_t left; 139662306a36Sopenharmony_ci size_t start; 139762306a36Sopenharmony_ci int i, n; 139862306a36Sopenharmony_ci 139962306a36Sopenharmony_ci left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start); 140062306a36Sopenharmony_ci if (left <= 0) { 140162306a36Sopenharmony_ci ret = left; 140262306a36Sopenharmony_ci break; 140362306a36Sopenharmony_ci } 140462306a36Sopenharmony_ci 140562306a36Sopenharmony_ci n = DIV_ROUND_UP(left + start, PAGE_SIZE); 140662306a36Sopenharmony_ci for (i = 0; i < n; i++) { 140762306a36Sopenharmony_ci int size = min_t(int, left, PAGE_SIZE - start); 140862306a36Sopenharmony_ci 140962306a36Sopenharmony_ci buf.page = pages[i]; 141062306a36Sopenharmony_ci buf.offset = start; 141162306a36Sopenharmony_ci buf.len = size; 141262306a36Sopenharmony_ci ret = add_to_pipe(pipe, &buf); 141362306a36Sopenharmony_ci if (unlikely(ret < 0)) { 141462306a36Sopenharmony_ci iov_iter_revert(from, left); 141562306a36Sopenharmony_ci // this one got dropped by add_to_pipe() 141662306a36Sopenharmony_ci while (++i < n) 141762306a36Sopenharmony_ci put_page(pages[i]); 141862306a36Sopenharmony_ci goto out; 141962306a36Sopenharmony_ci } 142062306a36Sopenharmony_ci total += ret; 142162306a36Sopenharmony_ci left -= size; 142262306a36Sopenharmony_ci start = 0; 142362306a36Sopenharmony_ci } 142462306a36Sopenharmony_ci } 142562306a36Sopenharmony_ciout: 142662306a36Sopenharmony_ci return total ? total : ret; 142762306a36Sopenharmony_ci} 142862306a36Sopenharmony_ci 142962306a36Sopenharmony_cistatic int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 143062306a36Sopenharmony_ci struct splice_desc *sd) 143162306a36Sopenharmony_ci{ 143262306a36Sopenharmony_ci int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); 143362306a36Sopenharmony_ci return n == sd->len ? n : -EFAULT; 143462306a36Sopenharmony_ci} 143562306a36Sopenharmony_ci 143662306a36Sopenharmony_ci/* 143762306a36Sopenharmony_ci * For lack of a better implementation, implement vmsplice() to userspace 143862306a36Sopenharmony_ci * as a simple copy of the pipes pages to the user iov. 143962306a36Sopenharmony_ci */ 144062306a36Sopenharmony_cistatic long vmsplice_to_user(struct file *file, struct iov_iter *iter, 144162306a36Sopenharmony_ci unsigned int flags) 144262306a36Sopenharmony_ci{ 144362306a36Sopenharmony_ci struct pipe_inode_info *pipe = get_pipe_info(file, true); 144462306a36Sopenharmony_ci struct splice_desc sd = { 144562306a36Sopenharmony_ci .total_len = iov_iter_count(iter), 144662306a36Sopenharmony_ci .flags = flags, 144762306a36Sopenharmony_ci .u.data = iter 144862306a36Sopenharmony_ci }; 144962306a36Sopenharmony_ci long ret = 0; 145062306a36Sopenharmony_ci 145162306a36Sopenharmony_ci if (!pipe) 145262306a36Sopenharmony_ci return -EBADF; 145362306a36Sopenharmony_ci 145462306a36Sopenharmony_ci pipe_clear_nowait(file); 145562306a36Sopenharmony_ci 145662306a36Sopenharmony_ci if (sd.total_len) { 145762306a36Sopenharmony_ci pipe_lock(pipe); 145862306a36Sopenharmony_ci ret = __splice_from_pipe(pipe, &sd, pipe_to_user); 145962306a36Sopenharmony_ci pipe_unlock(pipe); 146062306a36Sopenharmony_ci } 146162306a36Sopenharmony_ci 146262306a36Sopenharmony_ci if (ret > 0) 146362306a36Sopenharmony_ci fsnotify_access(file); 146462306a36Sopenharmony_ci 146562306a36Sopenharmony_ci return ret; 146662306a36Sopenharmony_ci} 146762306a36Sopenharmony_ci 146862306a36Sopenharmony_ci/* 146962306a36Sopenharmony_ci * vmsplice splices a user address range into a pipe. It can be thought of 147062306a36Sopenharmony_ci * as splice-from-memory, where the regular splice is splice-from-file (or 147162306a36Sopenharmony_ci * to file). In both cases the output is a pipe, naturally. 147262306a36Sopenharmony_ci */ 147362306a36Sopenharmony_cistatic long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, 147462306a36Sopenharmony_ci unsigned int flags) 147562306a36Sopenharmony_ci{ 147662306a36Sopenharmony_ci struct pipe_inode_info *pipe; 147762306a36Sopenharmony_ci long ret = 0; 147862306a36Sopenharmony_ci unsigned buf_flag = 0; 147962306a36Sopenharmony_ci 148062306a36Sopenharmony_ci if (flags & SPLICE_F_GIFT) 148162306a36Sopenharmony_ci buf_flag = PIPE_BUF_FLAG_GIFT; 148262306a36Sopenharmony_ci 148362306a36Sopenharmony_ci pipe = get_pipe_info(file, true); 148462306a36Sopenharmony_ci if (!pipe) 148562306a36Sopenharmony_ci return -EBADF; 148662306a36Sopenharmony_ci 148762306a36Sopenharmony_ci pipe_clear_nowait(file); 148862306a36Sopenharmony_ci 148962306a36Sopenharmony_ci pipe_lock(pipe); 149062306a36Sopenharmony_ci ret = wait_for_space(pipe, flags); 149162306a36Sopenharmony_ci if (!ret) 149262306a36Sopenharmony_ci ret = iter_to_pipe(iter, pipe, buf_flag); 149362306a36Sopenharmony_ci pipe_unlock(pipe); 149462306a36Sopenharmony_ci if (ret > 0) { 149562306a36Sopenharmony_ci wakeup_pipe_readers(pipe); 149662306a36Sopenharmony_ci fsnotify_modify(file); 149762306a36Sopenharmony_ci } 149862306a36Sopenharmony_ci return ret; 149962306a36Sopenharmony_ci} 150062306a36Sopenharmony_ci 150162306a36Sopenharmony_cistatic int vmsplice_type(struct fd f, int *type) 150262306a36Sopenharmony_ci{ 150362306a36Sopenharmony_ci if (!f.file) 150462306a36Sopenharmony_ci return -EBADF; 150562306a36Sopenharmony_ci if (f.file->f_mode & FMODE_WRITE) { 150662306a36Sopenharmony_ci *type = ITER_SOURCE; 150762306a36Sopenharmony_ci } else if (f.file->f_mode & FMODE_READ) { 150862306a36Sopenharmony_ci *type = ITER_DEST; 150962306a36Sopenharmony_ci } else { 151062306a36Sopenharmony_ci fdput(f); 151162306a36Sopenharmony_ci return -EBADF; 151262306a36Sopenharmony_ci } 151362306a36Sopenharmony_ci return 0; 151462306a36Sopenharmony_ci} 151562306a36Sopenharmony_ci 151662306a36Sopenharmony_ci/* 151762306a36Sopenharmony_ci * Note that vmsplice only really supports true splicing _from_ user memory 151862306a36Sopenharmony_ci * to a pipe, not the other way around. Splicing from user memory is a simple 151962306a36Sopenharmony_ci * operation that can be supported without any funky alignment restrictions 152062306a36Sopenharmony_ci * or nasty vm tricks. We simply map in the user memory and fill them into 152162306a36Sopenharmony_ci * a pipe. The reverse isn't quite as easy, though. There are two possible 152262306a36Sopenharmony_ci * solutions for that: 152362306a36Sopenharmony_ci * 152462306a36Sopenharmony_ci * - memcpy() the data internally, at which point we might as well just 152562306a36Sopenharmony_ci * do a regular read() on the buffer anyway. 152662306a36Sopenharmony_ci * - Lots of nasty vm tricks, that are neither fast nor flexible (it 152762306a36Sopenharmony_ci * has restriction limitations on both ends of the pipe). 152862306a36Sopenharmony_ci * 152962306a36Sopenharmony_ci * Currently we punt and implement it as a normal copy, see pipe_to_user(). 153062306a36Sopenharmony_ci * 153162306a36Sopenharmony_ci */ 153262306a36Sopenharmony_ciSYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, 153362306a36Sopenharmony_ci unsigned long, nr_segs, unsigned int, flags) 153462306a36Sopenharmony_ci{ 153562306a36Sopenharmony_ci struct iovec iovstack[UIO_FASTIOV]; 153662306a36Sopenharmony_ci struct iovec *iov = iovstack; 153762306a36Sopenharmony_ci struct iov_iter iter; 153862306a36Sopenharmony_ci ssize_t error; 153962306a36Sopenharmony_ci struct fd f; 154062306a36Sopenharmony_ci int type; 154162306a36Sopenharmony_ci 154262306a36Sopenharmony_ci if (unlikely(flags & ~SPLICE_F_ALL)) 154362306a36Sopenharmony_ci return -EINVAL; 154462306a36Sopenharmony_ci 154562306a36Sopenharmony_ci f = fdget(fd); 154662306a36Sopenharmony_ci error = vmsplice_type(f, &type); 154762306a36Sopenharmony_ci if (error) 154862306a36Sopenharmony_ci return error; 154962306a36Sopenharmony_ci 155062306a36Sopenharmony_ci error = import_iovec(type, uiov, nr_segs, 155162306a36Sopenharmony_ci ARRAY_SIZE(iovstack), &iov, &iter); 155262306a36Sopenharmony_ci if (error < 0) 155362306a36Sopenharmony_ci goto out_fdput; 155462306a36Sopenharmony_ci 155562306a36Sopenharmony_ci if (!iov_iter_count(&iter)) 155662306a36Sopenharmony_ci error = 0; 155762306a36Sopenharmony_ci else if (type == ITER_SOURCE) 155862306a36Sopenharmony_ci error = vmsplice_to_pipe(f.file, &iter, flags); 155962306a36Sopenharmony_ci else 156062306a36Sopenharmony_ci error = vmsplice_to_user(f.file, &iter, flags); 156162306a36Sopenharmony_ci 156262306a36Sopenharmony_ci kfree(iov); 156362306a36Sopenharmony_ciout_fdput: 156462306a36Sopenharmony_ci fdput(f); 156562306a36Sopenharmony_ci return error; 156662306a36Sopenharmony_ci} 156762306a36Sopenharmony_ci 156862306a36Sopenharmony_ciSYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, 156962306a36Sopenharmony_ci int, fd_out, loff_t __user *, off_out, 157062306a36Sopenharmony_ci size_t, len, unsigned int, flags) 157162306a36Sopenharmony_ci{ 157262306a36Sopenharmony_ci struct fd in, out; 157362306a36Sopenharmony_ci long error; 157462306a36Sopenharmony_ci 157562306a36Sopenharmony_ci if (unlikely(!len)) 157662306a36Sopenharmony_ci return 0; 157762306a36Sopenharmony_ci 157862306a36Sopenharmony_ci if (unlikely(flags & ~SPLICE_F_ALL)) 157962306a36Sopenharmony_ci return -EINVAL; 158062306a36Sopenharmony_ci 158162306a36Sopenharmony_ci error = -EBADF; 158262306a36Sopenharmony_ci in = fdget(fd_in); 158362306a36Sopenharmony_ci if (in.file) { 158462306a36Sopenharmony_ci out = fdget(fd_out); 158562306a36Sopenharmony_ci if (out.file) { 158662306a36Sopenharmony_ci error = __do_splice(in.file, off_in, out.file, off_out, 158762306a36Sopenharmony_ci len, flags); 158862306a36Sopenharmony_ci fdput(out); 158962306a36Sopenharmony_ci } 159062306a36Sopenharmony_ci fdput(in); 159162306a36Sopenharmony_ci } 159262306a36Sopenharmony_ci return error; 159362306a36Sopenharmony_ci} 159462306a36Sopenharmony_ci 159562306a36Sopenharmony_ci/* 159662306a36Sopenharmony_ci * Make sure there's data to read. Wait for input if we can, otherwise 159762306a36Sopenharmony_ci * return an appropriate error. 159862306a36Sopenharmony_ci */ 159962306a36Sopenharmony_cistatic int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 160062306a36Sopenharmony_ci{ 160162306a36Sopenharmony_ci int ret; 160262306a36Sopenharmony_ci 160362306a36Sopenharmony_ci /* 160462306a36Sopenharmony_ci * Check the pipe occupancy without the inode lock first. This function 160562306a36Sopenharmony_ci * is speculative anyways, so missing one is ok. 160662306a36Sopenharmony_ci */ 160762306a36Sopenharmony_ci if (!pipe_empty(pipe->head, pipe->tail)) 160862306a36Sopenharmony_ci return 0; 160962306a36Sopenharmony_ci 161062306a36Sopenharmony_ci ret = 0; 161162306a36Sopenharmony_ci pipe_lock(pipe); 161262306a36Sopenharmony_ci 161362306a36Sopenharmony_ci while (pipe_empty(pipe->head, pipe->tail)) { 161462306a36Sopenharmony_ci if (signal_pending(current)) { 161562306a36Sopenharmony_ci ret = -ERESTARTSYS; 161662306a36Sopenharmony_ci break; 161762306a36Sopenharmony_ci } 161862306a36Sopenharmony_ci if (!pipe->writers) 161962306a36Sopenharmony_ci break; 162062306a36Sopenharmony_ci if (flags & SPLICE_F_NONBLOCK) { 162162306a36Sopenharmony_ci ret = -EAGAIN; 162262306a36Sopenharmony_ci break; 162362306a36Sopenharmony_ci } 162462306a36Sopenharmony_ci pipe_wait_readable(pipe); 162562306a36Sopenharmony_ci } 162662306a36Sopenharmony_ci 162762306a36Sopenharmony_ci pipe_unlock(pipe); 162862306a36Sopenharmony_ci return ret; 162962306a36Sopenharmony_ci} 163062306a36Sopenharmony_ci 163162306a36Sopenharmony_ci/* 163262306a36Sopenharmony_ci * Make sure there's writeable room. Wait for room if we can, otherwise 163362306a36Sopenharmony_ci * return an appropriate error. 163462306a36Sopenharmony_ci */ 163562306a36Sopenharmony_cistatic int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) 163662306a36Sopenharmony_ci{ 163762306a36Sopenharmony_ci int ret; 163862306a36Sopenharmony_ci 163962306a36Sopenharmony_ci /* 164062306a36Sopenharmony_ci * Check pipe occupancy without the inode lock first. This function 164162306a36Sopenharmony_ci * is speculative anyways, so missing one is ok. 164262306a36Sopenharmony_ci */ 164362306a36Sopenharmony_ci if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 164462306a36Sopenharmony_ci return 0; 164562306a36Sopenharmony_ci 164662306a36Sopenharmony_ci ret = 0; 164762306a36Sopenharmony_ci pipe_lock(pipe); 164862306a36Sopenharmony_ci 164962306a36Sopenharmony_ci while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 165062306a36Sopenharmony_ci if (!pipe->readers) { 165162306a36Sopenharmony_ci send_sig(SIGPIPE, current, 0); 165262306a36Sopenharmony_ci ret = -EPIPE; 165362306a36Sopenharmony_ci break; 165462306a36Sopenharmony_ci } 165562306a36Sopenharmony_ci if (flags & SPLICE_F_NONBLOCK) { 165662306a36Sopenharmony_ci ret = -EAGAIN; 165762306a36Sopenharmony_ci break; 165862306a36Sopenharmony_ci } 165962306a36Sopenharmony_ci if (signal_pending(current)) { 166062306a36Sopenharmony_ci ret = -ERESTARTSYS; 166162306a36Sopenharmony_ci break; 166262306a36Sopenharmony_ci } 166362306a36Sopenharmony_ci pipe_wait_writable(pipe); 166462306a36Sopenharmony_ci } 166562306a36Sopenharmony_ci 166662306a36Sopenharmony_ci pipe_unlock(pipe); 166762306a36Sopenharmony_ci return ret; 166862306a36Sopenharmony_ci} 166962306a36Sopenharmony_ci 167062306a36Sopenharmony_ci/* 167162306a36Sopenharmony_ci * Splice contents of ipipe to opipe. 167262306a36Sopenharmony_ci */ 167362306a36Sopenharmony_cistatic int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, 167462306a36Sopenharmony_ci struct pipe_inode_info *opipe, 167562306a36Sopenharmony_ci size_t len, unsigned int flags) 167662306a36Sopenharmony_ci{ 167762306a36Sopenharmony_ci struct pipe_buffer *ibuf, *obuf; 167862306a36Sopenharmony_ci unsigned int i_head, o_head; 167962306a36Sopenharmony_ci unsigned int i_tail, o_tail; 168062306a36Sopenharmony_ci unsigned int i_mask, o_mask; 168162306a36Sopenharmony_ci int ret = 0; 168262306a36Sopenharmony_ci bool input_wakeup = false; 168362306a36Sopenharmony_ci 168462306a36Sopenharmony_ci 168562306a36Sopenharmony_ciretry: 168662306a36Sopenharmony_ci ret = ipipe_prep(ipipe, flags); 168762306a36Sopenharmony_ci if (ret) 168862306a36Sopenharmony_ci return ret; 168962306a36Sopenharmony_ci 169062306a36Sopenharmony_ci ret = opipe_prep(opipe, flags); 169162306a36Sopenharmony_ci if (ret) 169262306a36Sopenharmony_ci return ret; 169362306a36Sopenharmony_ci 169462306a36Sopenharmony_ci /* 169562306a36Sopenharmony_ci * Potential ABBA deadlock, work around it by ordering lock 169662306a36Sopenharmony_ci * grabbing by pipe info address. Otherwise two different processes 169762306a36Sopenharmony_ci * could deadlock (one doing tee from A -> B, the other from B -> A). 169862306a36Sopenharmony_ci */ 169962306a36Sopenharmony_ci pipe_double_lock(ipipe, opipe); 170062306a36Sopenharmony_ci 170162306a36Sopenharmony_ci i_tail = ipipe->tail; 170262306a36Sopenharmony_ci i_mask = ipipe->ring_size - 1; 170362306a36Sopenharmony_ci o_head = opipe->head; 170462306a36Sopenharmony_ci o_mask = opipe->ring_size - 1; 170562306a36Sopenharmony_ci 170662306a36Sopenharmony_ci do { 170762306a36Sopenharmony_ci size_t o_len; 170862306a36Sopenharmony_ci 170962306a36Sopenharmony_ci if (!opipe->readers) { 171062306a36Sopenharmony_ci send_sig(SIGPIPE, current, 0); 171162306a36Sopenharmony_ci if (!ret) 171262306a36Sopenharmony_ci ret = -EPIPE; 171362306a36Sopenharmony_ci break; 171462306a36Sopenharmony_ci } 171562306a36Sopenharmony_ci 171662306a36Sopenharmony_ci i_head = ipipe->head; 171762306a36Sopenharmony_ci o_tail = opipe->tail; 171862306a36Sopenharmony_ci 171962306a36Sopenharmony_ci if (pipe_empty(i_head, i_tail) && !ipipe->writers) 172062306a36Sopenharmony_ci break; 172162306a36Sopenharmony_ci 172262306a36Sopenharmony_ci /* 172362306a36Sopenharmony_ci * Cannot make any progress, because either the input 172462306a36Sopenharmony_ci * pipe is empty or the output pipe is full. 172562306a36Sopenharmony_ci */ 172662306a36Sopenharmony_ci if (pipe_empty(i_head, i_tail) || 172762306a36Sopenharmony_ci pipe_full(o_head, o_tail, opipe->max_usage)) { 172862306a36Sopenharmony_ci /* Already processed some buffers, break */ 172962306a36Sopenharmony_ci if (ret) 173062306a36Sopenharmony_ci break; 173162306a36Sopenharmony_ci 173262306a36Sopenharmony_ci if (flags & SPLICE_F_NONBLOCK) { 173362306a36Sopenharmony_ci ret = -EAGAIN; 173462306a36Sopenharmony_ci break; 173562306a36Sopenharmony_ci } 173662306a36Sopenharmony_ci 173762306a36Sopenharmony_ci /* 173862306a36Sopenharmony_ci * We raced with another reader/writer and haven't 173962306a36Sopenharmony_ci * managed to process any buffers. A zero return 174062306a36Sopenharmony_ci * value means EOF, so retry instead. 174162306a36Sopenharmony_ci */ 174262306a36Sopenharmony_ci pipe_unlock(ipipe); 174362306a36Sopenharmony_ci pipe_unlock(opipe); 174462306a36Sopenharmony_ci goto retry; 174562306a36Sopenharmony_ci } 174662306a36Sopenharmony_ci 174762306a36Sopenharmony_ci ibuf = &ipipe->bufs[i_tail & i_mask]; 174862306a36Sopenharmony_ci obuf = &opipe->bufs[o_head & o_mask]; 174962306a36Sopenharmony_ci 175062306a36Sopenharmony_ci if (len >= ibuf->len) { 175162306a36Sopenharmony_ci /* 175262306a36Sopenharmony_ci * Simply move the whole buffer from ipipe to opipe 175362306a36Sopenharmony_ci */ 175462306a36Sopenharmony_ci *obuf = *ibuf; 175562306a36Sopenharmony_ci ibuf->ops = NULL; 175662306a36Sopenharmony_ci i_tail++; 175762306a36Sopenharmony_ci ipipe->tail = i_tail; 175862306a36Sopenharmony_ci input_wakeup = true; 175962306a36Sopenharmony_ci o_len = obuf->len; 176062306a36Sopenharmony_ci o_head++; 176162306a36Sopenharmony_ci opipe->head = o_head; 176262306a36Sopenharmony_ci } else { 176362306a36Sopenharmony_ci /* 176462306a36Sopenharmony_ci * Get a reference to this pipe buffer, 176562306a36Sopenharmony_ci * so we can copy the contents over. 176662306a36Sopenharmony_ci */ 176762306a36Sopenharmony_ci if (!pipe_buf_get(ipipe, ibuf)) { 176862306a36Sopenharmony_ci if (ret == 0) 176962306a36Sopenharmony_ci ret = -EFAULT; 177062306a36Sopenharmony_ci break; 177162306a36Sopenharmony_ci } 177262306a36Sopenharmony_ci *obuf = *ibuf; 177362306a36Sopenharmony_ci 177462306a36Sopenharmony_ci /* 177562306a36Sopenharmony_ci * Don't inherit the gift and merge flags, we need to 177662306a36Sopenharmony_ci * prevent multiple steals of this page. 177762306a36Sopenharmony_ci */ 177862306a36Sopenharmony_ci obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 177962306a36Sopenharmony_ci obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 178062306a36Sopenharmony_ci 178162306a36Sopenharmony_ci obuf->len = len; 178262306a36Sopenharmony_ci ibuf->offset += len; 178362306a36Sopenharmony_ci ibuf->len -= len; 178462306a36Sopenharmony_ci o_len = len; 178562306a36Sopenharmony_ci o_head++; 178662306a36Sopenharmony_ci opipe->head = o_head; 178762306a36Sopenharmony_ci } 178862306a36Sopenharmony_ci ret += o_len; 178962306a36Sopenharmony_ci len -= o_len; 179062306a36Sopenharmony_ci } while (len); 179162306a36Sopenharmony_ci 179262306a36Sopenharmony_ci pipe_unlock(ipipe); 179362306a36Sopenharmony_ci pipe_unlock(opipe); 179462306a36Sopenharmony_ci 179562306a36Sopenharmony_ci /* 179662306a36Sopenharmony_ci * If we put data in the output pipe, wakeup any potential readers. 179762306a36Sopenharmony_ci */ 179862306a36Sopenharmony_ci if (ret > 0) 179962306a36Sopenharmony_ci wakeup_pipe_readers(opipe); 180062306a36Sopenharmony_ci 180162306a36Sopenharmony_ci if (input_wakeup) 180262306a36Sopenharmony_ci wakeup_pipe_writers(ipipe); 180362306a36Sopenharmony_ci 180462306a36Sopenharmony_ci return ret; 180562306a36Sopenharmony_ci} 180662306a36Sopenharmony_ci 180762306a36Sopenharmony_ci/* 180862306a36Sopenharmony_ci * Link contents of ipipe to opipe. 180962306a36Sopenharmony_ci */ 181062306a36Sopenharmony_cistatic int link_pipe(struct pipe_inode_info *ipipe, 181162306a36Sopenharmony_ci struct pipe_inode_info *opipe, 181262306a36Sopenharmony_ci size_t len, unsigned int flags) 181362306a36Sopenharmony_ci{ 181462306a36Sopenharmony_ci struct pipe_buffer *ibuf, *obuf; 181562306a36Sopenharmony_ci unsigned int i_head, o_head; 181662306a36Sopenharmony_ci unsigned int i_tail, o_tail; 181762306a36Sopenharmony_ci unsigned int i_mask, o_mask; 181862306a36Sopenharmony_ci int ret = 0; 181962306a36Sopenharmony_ci 182062306a36Sopenharmony_ci /* 182162306a36Sopenharmony_ci * Potential ABBA deadlock, work around it by ordering lock 182262306a36Sopenharmony_ci * grabbing by pipe info address. Otherwise two different processes 182362306a36Sopenharmony_ci * could deadlock (one doing tee from A -> B, the other from B -> A). 182462306a36Sopenharmony_ci */ 182562306a36Sopenharmony_ci pipe_double_lock(ipipe, opipe); 182662306a36Sopenharmony_ci 182762306a36Sopenharmony_ci i_tail = ipipe->tail; 182862306a36Sopenharmony_ci i_mask = ipipe->ring_size - 1; 182962306a36Sopenharmony_ci o_head = opipe->head; 183062306a36Sopenharmony_ci o_mask = opipe->ring_size - 1; 183162306a36Sopenharmony_ci 183262306a36Sopenharmony_ci do { 183362306a36Sopenharmony_ci if (!opipe->readers) { 183462306a36Sopenharmony_ci send_sig(SIGPIPE, current, 0); 183562306a36Sopenharmony_ci if (!ret) 183662306a36Sopenharmony_ci ret = -EPIPE; 183762306a36Sopenharmony_ci break; 183862306a36Sopenharmony_ci } 183962306a36Sopenharmony_ci 184062306a36Sopenharmony_ci i_head = ipipe->head; 184162306a36Sopenharmony_ci o_tail = opipe->tail; 184262306a36Sopenharmony_ci 184362306a36Sopenharmony_ci /* 184462306a36Sopenharmony_ci * If we have iterated all input buffers or run out of 184562306a36Sopenharmony_ci * output room, break. 184662306a36Sopenharmony_ci */ 184762306a36Sopenharmony_ci if (pipe_empty(i_head, i_tail) || 184862306a36Sopenharmony_ci pipe_full(o_head, o_tail, opipe->max_usage)) 184962306a36Sopenharmony_ci break; 185062306a36Sopenharmony_ci 185162306a36Sopenharmony_ci ibuf = &ipipe->bufs[i_tail & i_mask]; 185262306a36Sopenharmony_ci obuf = &opipe->bufs[o_head & o_mask]; 185362306a36Sopenharmony_ci 185462306a36Sopenharmony_ci /* 185562306a36Sopenharmony_ci * Get a reference to this pipe buffer, 185662306a36Sopenharmony_ci * so we can copy the contents over. 185762306a36Sopenharmony_ci */ 185862306a36Sopenharmony_ci if (!pipe_buf_get(ipipe, ibuf)) { 185962306a36Sopenharmony_ci if (ret == 0) 186062306a36Sopenharmony_ci ret = -EFAULT; 186162306a36Sopenharmony_ci break; 186262306a36Sopenharmony_ci } 186362306a36Sopenharmony_ci 186462306a36Sopenharmony_ci *obuf = *ibuf; 186562306a36Sopenharmony_ci 186662306a36Sopenharmony_ci /* 186762306a36Sopenharmony_ci * Don't inherit the gift and merge flag, we need to prevent 186862306a36Sopenharmony_ci * multiple steals of this page. 186962306a36Sopenharmony_ci */ 187062306a36Sopenharmony_ci obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 187162306a36Sopenharmony_ci obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; 187262306a36Sopenharmony_ci 187362306a36Sopenharmony_ci if (obuf->len > len) 187462306a36Sopenharmony_ci obuf->len = len; 187562306a36Sopenharmony_ci ret += obuf->len; 187662306a36Sopenharmony_ci len -= obuf->len; 187762306a36Sopenharmony_ci 187862306a36Sopenharmony_ci o_head++; 187962306a36Sopenharmony_ci opipe->head = o_head; 188062306a36Sopenharmony_ci i_tail++; 188162306a36Sopenharmony_ci } while (len); 188262306a36Sopenharmony_ci 188362306a36Sopenharmony_ci pipe_unlock(ipipe); 188462306a36Sopenharmony_ci pipe_unlock(opipe); 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci /* 188762306a36Sopenharmony_ci * If we put data in the output pipe, wakeup any potential readers. 188862306a36Sopenharmony_ci */ 188962306a36Sopenharmony_ci if (ret > 0) 189062306a36Sopenharmony_ci wakeup_pipe_readers(opipe); 189162306a36Sopenharmony_ci 189262306a36Sopenharmony_ci return ret; 189362306a36Sopenharmony_ci} 189462306a36Sopenharmony_ci 189562306a36Sopenharmony_ci/* 189662306a36Sopenharmony_ci * This is a tee(1) implementation that works on pipes. It doesn't copy 189762306a36Sopenharmony_ci * any data, it simply references the 'in' pages on the 'out' pipe. 189862306a36Sopenharmony_ci * The 'flags' used are the SPLICE_F_* variants, currently the only 189962306a36Sopenharmony_ci * applicable one is SPLICE_F_NONBLOCK. 190062306a36Sopenharmony_ci */ 190162306a36Sopenharmony_cilong do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) 190262306a36Sopenharmony_ci{ 190362306a36Sopenharmony_ci struct pipe_inode_info *ipipe = get_pipe_info(in, true); 190462306a36Sopenharmony_ci struct pipe_inode_info *opipe = get_pipe_info(out, true); 190562306a36Sopenharmony_ci int ret = -EINVAL; 190662306a36Sopenharmony_ci 190762306a36Sopenharmony_ci if (unlikely(!(in->f_mode & FMODE_READ) || 190862306a36Sopenharmony_ci !(out->f_mode & FMODE_WRITE))) 190962306a36Sopenharmony_ci return -EBADF; 191062306a36Sopenharmony_ci 191162306a36Sopenharmony_ci /* 191262306a36Sopenharmony_ci * Duplicate the contents of ipipe to opipe without actually 191362306a36Sopenharmony_ci * copying the data. 191462306a36Sopenharmony_ci */ 191562306a36Sopenharmony_ci if (ipipe && opipe && ipipe != opipe) { 191662306a36Sopenharmony_ci if ((in->f_flags | out->f_flags) & O_NONBLOCK) 191762306a36Sopenharmony_ci flags |= SPLICE_F_NONBLOCK; 191862306a36Sopenharmony_ci 191962306a36Sopenharmony_ci /* 192062306a36Sopenharmony_ci * Keep going, unless we encounter an error. The ipipe/opipe 192162306a36Sopenharmony_ci * ordering doesn't really matter. 192262306a36Sopenharmony_ci */ 192362306a36Sopenharmony_ci ret = ipipe_prep(ipipe, flags); 192462306a36Sopenharmony_ci if (!ret) { 192562306a36Sopenharmony_ci ret = opipe_prep(opipe, flags); 192662306a36Sopenharmony_ci if (!ret) 192762306a36Sopenharmony_ci ret = link_pipe(ipipe, opipe, len, flags); 192862306a36Sopenharmony_ci } 192962306a36Sopenharmony_ci } 193062306a36Sopenharmony_ci 193162306a36Sopenharmony_ci if (ret > 0) { 193262306a36Sopenharmony_ci fsnotify_access(in); 193362306a36Sopenharmony_ci fsnotify_modify(out); 193462306a36Sopenharmony_ci } 193562306a36Sopenharmony_ci 193662306a36Sopenharmony_ci return ret; 193762306a36Sopenharmony_ci} 193862306a36Sopenharmony_ci 193962306a36Sopenharmony_ciSYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) 194062306a36Sopenharmony_ci{ 194162306a36Sopenharmony_ci struct fd in, out; 194262306a36Sopenharmony_ci int error; 194362306a36Sopenharmony_ci 194462306a36Sopenharmony_ci if (unlikely(flags & ~SPLICE_F_ALL)) 194562306a36Sopenharmony_ci return -EINVAL; 194662306a36Sopenharmony_ci 194762306a36Sopenharmony_ci if (unlikely(!len)) 194862306a36Sopenharmony_ci return 0; 194962306a36Sopenharmony_ci 195062306a36Sopenharmony_ci error = -EBADF; 195162306a36Sopenharmony_ci in = fdget(fdin); 195262306a36Sopenharmony_ci if (in.file) { 195362306a36Sopenharmony_ci out = fdget(fdout); 195462306a36Sopenharmony_ci if (out.file) { 195562306a36Sopenharmony_ci error = do_tee(in.file, out.file, len, flags); 195662306a36Sopenharmony_ci fdput(out); 195762306a36Sopenharmony_ci } 195862306a36Sopenharmony_ci fdput(in); 195962306a36Sopenharmony_ci } 196062306a36Sopenharmony_ci 196162306a36Sopenharmony_ci return error; 196262306a36Sopenharmony_ci} 1963