Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * "splice": joining two ropes together by interweaving their strands. |
| 3 | * |
| 4 | * This is the "extended pipe" functionality, where a pipe is used as |
| 5 | * an arbitrary in-memory buffer. Think of a pipe as a small kernel |
| 6 | * buffer that you can use to transfer data from one end to the other. |
| 7 | * |
| 8 | * The traditional unix read/write is extended with a "splice()" operation |
| 9 | * that transfers data buffers to or from a pipe buffer. |
| 10 | * |
| 11 | * Named by Larry McVoy, original implementation from Linus, extended by |
Jens Axboe | c2058e0 | 2006-04-11 13:56:34 +0200 | [diff] [blame] | 12 | * Jens to support splicing to files, network, direct splicing, etc and |
| 13 | * fixing lots of bugs. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 14 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 15 | * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | c2058e0 | 2006-04-11 13:56:34 +0200 | [diff] [blame] | 16 | * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> |
| 17 | * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/file.h> |
| 22 | #include <linux/pagemap.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 23 | #include <linux/splice.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 24 | #include <linux/memcontrol.h> |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 25 | #include <linux/mm_inline.h> |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 26 | #include <linux/swap.h> |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 27 | #include <linux/writeback.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 28 | #include <linux/export.h> |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 29 | #include <linux/syscalls.h> |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 30 | #include <linux/uio.h> |
James Morris | 29ce205 | 2007-07-13 11:44:32 +0200 | [diff] [blame] | 31 | #include <linux/security.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/gfp.h> |
Eric Dumazet | 35f9c09 | 2012-04-05 03:05:35 +0000 | [diff] [blame] | 33 | #include <linux/socket.h> |
Al Viro | 76b021d | 2013-03-02 10:19:56 -0500 | [diff] [blame] | 34 | #include <linux/compat.h> |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 35 | #include "internal.h" |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 36 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Attempt to steal a page from a pipe buffer. This should perhaps go into |
| 39 | * a vm helper function, it's already simplified quite a bit by the |
| 40 | * addition of remove_mapping(). If success is returned, the caller may |
| 41 | * attempt to reuse this page for another destination. |
| 42 | */ |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 43 | static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 44 | struct pipe_buffer *buf) |
| 45 | { |
| 46 | struct page *page = buf->page; |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 47 | struct address_space *mapping; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 48 | |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 49 | lock_page(page); |
| 50 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 51 | mapping = page_mapping(page); |
| 52 | if (mapping) { |
| 53 | WARN_ON(!PageUptodate(page)); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 54 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 55 | /* |
| 56 | * At least for ext2 with nobh option, we need to wait on |
| 57 | * writeback completing on this page, since we'll remove it |
| 58 | * from the pagecache. Otherwise truncate wont wait on the |
| 59 | * page, allowing the disk blocks to be reused by someone else |
| 60 | * before we actually wrote our data to them. fs corruption |
| 61 | * ensues. |
| 62 | */ |
| 63 | wait_on_page_writeback(page); |
Jens Axboe | ad8d6f0 | 2006-04-02 23:10:32 +0200 | [diff] [blame] | 64 | |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 65 | if (page_has_private(page) && |
| 66 | !try_to_release_page(page, GFP_KERNEL)) |
Jens Axboe | ca39d65 | 2008-05-20 21:27:41 +0200 | [diff] [blame] | 67 | goto out_unlock; |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 68 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 69 | /* |
| 70 | * If we succeeded in removing the mapping, set LRU flag |
| 71 | * and return good. |
| 72 | */ |
| 73 | if (remove_mapping(mapping, page)) { |
| 74 | buf->flags |= PIPE_BUF_FLAG_LRU; |
| 75 | return 0; |
| 76 | } |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 77 | } |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 78 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 79 | /* |
| 80 | * Raced with truncate or failed to remove page from current |
| 81 | * address space, unlock and return failure. |
| 82 | */ |
Jens Axboe | ca39d65 | 2008-05-20 21:27:41 +0200 | [diff] [blame] | 83 | out_unlock: |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 84 | unlock_page(page); |
| 85 | return 1; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 88 | static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 89 | struct pipe_buffer *buf) |
| 90 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 91 | put_page(buf->page); |
Jens Axboe | 1432873 | 2006-05-03 10:35:26 +0200 | [diff] [blame] | 92 | buf->flags &= ~PIPE_BUF_FLAG_LRU; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 95 | /* |
| 96 | * Check whether the contents of buf is OK to access. Since the content |
| 97 | * is a page cache page, IO may be in flight. |
| 98 | */ |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 99 | static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, |
| 100 | struct pipe_buffer *buf) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 101 | { |
| 102 | struct page *page = buf->page; |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 103 | int err; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 104 | |
| 105 | if (!PageUptodate(page)) { |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 106 | lock_page(page); |
| 107 | |
| 108 | /* |
| 109 | * Page got truncated/unhashed. This will cause a 0-byte |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 110 | * splice, if this is the first page. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 111 | */ |
| 112 | if (!page->mapping) { |
| 113 | err = -ENODATA; |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 118 | * Uh oh, read-error from disk. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 119 | */ |
| 120 | if (!PageUptodate(page)) { |
| 121 | err = -EIO; |
| 122 | goto error; |
| 123 | } |
| 124 | |
| 125 | /* |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 126 | * Page is ok afterall, we are done. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 127 | */ |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 128 | unlock_page(page); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 131 | return 0; |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 132 | error: |
| 133 | unlock_page(page); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 134 | return err; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 137 | const struct pipe_buf_operations page_cache_pipe_buf_ops = { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 138 | .can_merge = 0, |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 139 | .confirm = page_cache_pipe_buf_confirm, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 140 | .release = page_cache_pipe_buf_release, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 141 | .steal = page_cache_pipe_buf_steal, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 142 | .get = generic_pipe_buf_get, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 143 | }; |
| 144 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 145 | static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, |
| 146 | struct pipe_buffer *buf) |
| 147 | { |
Jens Axboe | 7afa6fd | 2006-05-01 20:02:33 +0200 | [diff] [blame] | 148 | if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) |
| 149 | return 1; |
| 150 | |
Jens Axboe | 1432873 | 2006-05-03 10:35:26 +0200 | [diff] [blame] | 151 | buf->flags |= PIPE_BUF_FLAG_LRU; |
Jens Axboe | 330ab71 | 2006-05-02 15:29:57 +0200 | [diff] [blame] | 152 | return generic_pipe_buf_steal(pipe, buf); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 155 | static const struct pipe_buf_operations user_page_pipe_buf_ops = { |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 156 | .can_merge = 0, |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 157 | .confirm = generic_pipe_buf_confirm, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 158 | .release = page_cache_pipe_buf_release, |
| 159 | .steal = user_page_pipe_buf_steal, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 160 | .get = generic_pipe_buf_get, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
Namhyung Kim | 825cdcb | 2011-05-23 19:58:53 +0200 | [diff] [blame] | 163 | static void wakeup_pipe_readers(struct pipe_inode_info *pipe) |
| 164 | { |
| 165 | smp_mb(); |
| 166 | if (waitqueue_active(&pipe->wait)) |
| 167 | wake_up_interruptible(&pipe->wait); |
| 168 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
| 169 | } |
| 170 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 171 | /** |
| 172 | * splice_to_pipe - fill passed data into a pipe |
| 173 | * @pipe: pipe to fill |
| 174 | * @spd: data to fill |
| 175 | * |
| 176 | * Description: |
Randy Dunlap | 79685b8 | 2007-07-27 08:08:51 +0200 | [diff] [blame] | 177 | * @spd contains a map of pages and len/offset tuples, along with |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 178 | * the struct pipe_buf_operations associated with these pages. This |
| 179 | * function will link that data to the pipe. |
| 180 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 181 | */ |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 182 | ssize_t splice_to_pipe(struct pipe_inode_info *pipe, |
| 183 | struct splice_pipe_desc *spd) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 184 | { |
Jens Axboe | 00de00b | 2007-06-15 13:14:22 +0200 | [diff] [blame] | 185 | unsigned int spd_pages = spd->nr_pages; |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 186 | int ret = 0, page_nr = 0; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 187 | |
Rabin Vincent | d6785d9 | 2016-03-10 21:19:06 +0100 | [diff] [blame] | 188 | if (!spd_pages) |
| 189 | return 0; |
| 190 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 191 | if (unlikely(!pipe->readers)) { |
| 192 | send_sig(SIGPIPE, current, 0); |
| 193 | ret = -EPIPE; |
| 194 | goto out; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 197 | while (pipe->nrbufs < pipe->buffers) { |
| 198 | int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1); |
| 199 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 200 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 201 | buf->page = spd->pages[page_nr]; |
| 202 | buf->offset = spd->partial[page_nr].offset; |
| 203 | buf->len = spd->partial[page_nr].len; |
| 204 | buf->private = spd->partial[page_nr].private; |
| 205 | buf->ops = spd->ops; |
| 206 | if (spd->flags & SPLICE_F_GIFT) |
| 207 | buf->flags |= PIPE_BUF_FLAG_GIFT; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 208 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 209 | pipe->nrbufs++; |
| 210 | page_nr++; |
| 211 | ret += buf->len; |
| 212 | |
| 213 | if (!--spd->nr_pages) |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | if (!ret) |
| 218 | ret = -EAGAIN; |
| 219 | |
| 220 | out: |
Jens Axboe | 00de00b | 2007-06-15 13:14:22 +0200 | [diff] [blame] | 221 | while (page_nr < spd_pages) |
Jens Axboe | bbdfc2f | 2007-11-06 23:29:47 -0800 | [diff] [blame] | 222 | spd->spd_release(spd, page_nr++); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 223 | |
| 224 | return ret; |
| 225 | } |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(splice_to_pipe); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 227 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 228 | void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) |
Jens Axboe | bbdfc2f | 2007-11-06 23:29:47 -0800 | [diff] [blame] | 229 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 230 | put_page(spd->pages[i]); |
Jens Axboe | bbdfc2f | 2007-11-06 23:29:47 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 233 | /* |
| 234 | * Check if we need to grow the arrays holding pages and partial page |
| 235 | * descriptions. |
| 236 | */ |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 237 | int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 238 | { |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 239 | unsigned int buffers = ACCESS_ONCE(pipe->buffers); |
| 240 | |
| 241 | spd->nr_pages_max = buffers; |
| 242 | if (buffers <= PIPE_DEF_BUFFERS) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 245 | spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL); |
| 246 | spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 247 | |
| 248 | if (spd->pages && spd->partial) |
| 249 | return 0; |
| 250 | |
| 251 | kfree(spd->pages); |
| 252 | kfree(spd->partial); |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 256 | void splice_shrink_spd(struct splice_pipe_desc *spd) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 257 | { |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 258 | if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 259 | return; |
| 260 | |
| 261 | kfree(spd->pages); |
| 262 | kfree(spd->partial); |
| 263 | } |
| 264 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 265 | static int |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 266 | __generic_file_splice_read(struct file *in, loff_t *ppos, |
| 267 | struct pipe_inode_info *pipe, size_t len, |
| 268 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 269 | { |
| 270 | struct address_space *mapping = in->f_mapping; |
Fengguang Wu | d898391 | 2007-07-19 01:48:06 -0700 | [diff] [blame] | 271 | unsigned int loff, nr_pages, req_pages; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 272 | struct page *pages[PIPE_DEF_BUFFERS]; |
| 273 | struct partial_page partial[PIPE_DEF_BUFFERS]; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 274 | struct page *page; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 275 | pgoff_t index, end_index; |
| 276 | loff_t isize; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 277 | int error, page_nr; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 278 | struct splice_pipe_desc spd = { |
| 279 | .pages = pages, |
| 280 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 281 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 282 | .flags = flags, |
| 283 | .ops = &page_cache_pipe_buf_ops, |
Jens Axboe | bbdfc2f | 2007-11-06 23:29:47 -0800 | [diff] [blame] | 284 | .spd_release = spd_release_page, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 285 | }; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 286 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 287 | if (splice_grow_spd(pipe, &spd)) |
| 288 | return -ENOMEM; |
| 289 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 290 | index = *ppos >> PAGE_SHIFT; |
| 291 | loff = *ppos & ~PAGE_MASK; |
| 292 | req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 293 | nr_pages = min(req_pages, spd.nr_pages_max); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 294 | |
| 295 | /* |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 296 | * Lookup the (hopefully) full range of pages we need. |
| 297 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 298 | spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages); |
Fengguang Wu | 431a482 | 2007-07-19 01:48:05 -0700 | [diff] [blame] | 299 | index += spd.nr_pages; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * If find_get_pages_contig() returned fewer pages than we needed, |
Fengguang Wu | 431a482 | 2007-07-19 01:48:05 -0700 | [diff] [blame] | 303 | * readahead/allocate the rest and fill in the holes. |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 304 | */ |
Fengguang Wu | 431a482 | 2007-07-19 01:48:05 -0700 | [diff] [blame] | 305 | if (spd.nr_pages < nr_pages) |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 306 | page_cache_sync_readahead(mapping, &in->f_ra, in, |
| 307 | index, req_pages - spd.nr_pages); |
Fengguang Wu | 431a482 | 2007-07-19 01:48:05 -0700 | [diff] [blame] | 308 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 309 | error = 0; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 310 | while (spd.nr_pages < nr_pages) { |
| 311 | /* |
| 312 | * Page could be there, find_get_pages_contig() breaks on |
| 313 | * the first hole. |
| 314 | */ |
| 315 | page = find_get_page(mapping, index); |
| 316 | if (!page) { |
Jens Axboe | e27dedd | 2006-05-01 19:59:54 +0200 | [diff] [blame] | 317 | /* |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 318 | * page didn't exist, allocate one. |
| 319 | */ |
| 320 | page = page_cache_alloc_cold(mapping); |
| 321 | if (!page) |
| 322 | break; |
| 323 | |
| 324 | error = add_to_page_cache_lru(page, mapping, index, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 325 | mapping_gfp_constraint(mapping, GFP_KERNEL)); |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 326 | if (unlikely(error)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 327 | put_page(page); |
Jens Axboe | a054887 | 2006-05-03 10:58:22 +0200 | [diff] [blame] | 328 | if (error == -EEXIST) |
| 329 | continue; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | /* |
| 333 | * add_to_page_cache() locks the page, unlock it |
| 334 | * to avoid convoluting the logic below even more. |
| 335 | */ |
| 336 | unlock_page(page); |
| 337 | } |
| 338 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 339 | spd.pages[spd.nr_pages++] = page; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 340 | index++; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Now loop over the map and see if we need to start IO on any |
| 345 | * pages, fill in the partial map, etc. |
| 346 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 347 | index = *ppos >> PAGE_SHIFT; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 348 | nr_pages = spd.nr_pages; |
| 349 | spd.nr_pages = 0; |
| 350 | for (page_nr = 0; page_nr < nr_pages; page_nr++) { |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 351 | unsigned int this_len; |
| 352 | |
| 353 | if (!len) |
| 354 | break; |
| 355 | |
| 356 | /* |
| 357 | * this_len is the max we'll use from this page |
| 358 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 359 | this_len = min_t(unsigned long, len, PAGE_SIZE - loff); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 360 | page = spd.pages[page_nr]; |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 361 | |
Fengguang Wu | a08a166 | 2007-07-19 01:48:03 -0700 | [diff] [blame] | 362 | if (PageReadahead(page)) |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 363 | page_cache_async_readahead(mapping, &in->f_ra, in, |
Fengguang Wu | d898391 | 2007-07-19 01:48:06 -0700 | [diff] [blame] | 364 | page, index, req_pages - page_nr); |
Fengguang Wu | a08a166 | 2007-07-19 01:48:03 -0700 | [diff] [blame] | 365 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 366 | /* |
| 367 | * If the page isn't uptodate, we may need to start io on it |
| 368 | */ |
| 369 | if (!PageUptodate(page)) { |
Miklos Szeredi | 6965031 | 2010-08-03 12:48:50 +0200 | [diff] [blame] | 370 | lock_page(page); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 371 | |
| 372 | /* |
Miklos Szeredi | 32502b8 | 2008-07-04 09:35:17 +0200 | [diff] [blame] | 373 | * Page was truncated, or invalidated by the |
| 374 | * filesystem. Redo the find/create, but this time the |
| 375 | * page is kept locked, so there's no chance of another |
| 376 | * race with truncate/invalidate. |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 377 | */ |
| 378 | if (!page->mapping) { |
| 379 | unlock_page(page); |
Abhi Das | 90330e6 | 2015-12-18 14:11:36 -0600 | [diff] [blame] | 380 | retry_lookup: |
Miklos Szeredi | 32502b8 | 2008-07-04 09:35:17 +0200 | [diff] [blame] | 381 | page = find_or_create_page(mapping, index, |
| 382 | mapping_gfp_mask(mapping)); |
| 383 | |
| 384 | if (!page) { |
| 385 | error = -ENOMEM; |
| 386 | break; |
| 387 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 388 | put_page(spd.pages[page_nr]); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 389 | spd.pages[page_nr] = page; |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 390 | } |
| 391 | /* |
| 392 | * page was already under io and is now done, great |
| 393 | */ |
| 394 | if (PageUptodate(page)) { |
| 395 | unlock_page(page); |
| 396 | goto fill_it; |
| 397 | } |
| 398 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 399 | /* |
| 400 | * need to read in the page |
| 401 | */ |
| 402 | error = mapping->a_ops->readpage(in, page); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 403 | if (unlikely(error)) { |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 404 | /* |
Abhi Das | 90330e6 | 2015-12-18 14:11:36 -0600 | [diff] [blame] | 405 | * Re-lookup the page |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 406 | */ |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 407 | if (error == AOP_TRUNCATED_PAGE) |
Abhi Das | 90330e6 | 2015-12-18 14:11:36 -0600 | [diff] [blame] | 408 | goto retry_lookup; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 409 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 410 | break; |
| 411 | } |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 412 | } |
| 413 | fill_it: |
| 414 | /* |
| 415 | * i_size must be checked after PageUptodate. |
| 416 | */ |
| 417 | isize = i_size_read(mapping->host); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 418 | end_index = (isize - 1) >> PAGE_SHIFT; |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 419 | if (unlikely(!isize || index > end_index)) |
| 420 | break; |
| 421 | |
| 422 | /* |
| 423 | * if this is the last page, see if we need to shrink |
| 424 | * the length and stop |
| 425 | */ |
| 426 | if (end_index == index) { |
| 427 | unsigned int plen; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 430 | * max good bytes in this page |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 431 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 432 | plen = ((isize - 1) & ~PAGE_MASK) + 1; |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 433 | if (plen <= loff) |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 434 | break; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 435 | |
| 436 | /* |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 437 | * force quit after adding this page |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 438 | */ |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 439 | this_len = min(this_len, plen - loff); |
| 440 | len = this_len; |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 441 | } |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 442 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 443 | spd.partial[page_nr].offset = loff; |
| 444 | spd.partial[page_nr].len = this_len; |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 445 | len -= this_len; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 446 | loff = 0; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 447 | spd.nr_pages++; |
| 448 | index++; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 451 | /* |
Hugh Dickins | 475ecad | 2007-06-07 09:36:00 +0200 | [diff] [blame] | 452 | * Release any pages at the end, if we quit early. 'page_nr' is how far |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 453 | * we got, 'nr_pages' is how many pages are in the map. |
| 454 | */ |
| 455 | while (page_nr < nr_pages) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 456 | put_page(spd.pages[page_nr++]); |
| 457 | in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 458 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 459 | if (spd.nr_pages) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 460 | error = splice_to_pipe(pipe, &spd); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 461 | |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 462 | splice_shrink_spd(&spd); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 463 | return error; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 466 | /** |
| 467 | * generic_file_splice_read - splice data from file to a pipe |
| 468 | * @in: file to splice from |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 469 | * @ppos: position in @in |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 470 | * @pipe: pipe to splice to |
| 471 | * @len: number of bytes to splice |
| 472 | * @flags: splice modifier flags |
| 473 | * |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 474 | * Description: |
| 475 | * Will read pages from given file and fill them into a pipe. Can be |
| 476 | * used as long as the address_space operations for the source implements |
| 477 | * a readpage() hook. |
| 478 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 479 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 480 | ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, |
| 481 | struct pipe_inode_info *pipe, size_t len, |
| 482 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 483 | { |
Jens Axboe | d366d398 | 2007-06-01 14:54:11 +0200 | [diff] [blame] | 484 | loff_t isize, left; |
Jens Axboe | 8191ecd | 2008-04-10 08:24:25 +0200 | [diff] [blame] | 485 | int ret; |
Jens Axboe | d366d398 | 2007-06-01 14:54:11 +0200 | [diff] [blame] | 486 | |
Boaz Harrosh | be64f88 | 2015-04-15 16:15:17 -0700 | [diff] [blame] | 487 | if (IS_DAX(in->f_mapping->host)) |
| 488 | return default_file_splice_read(in, ppos, pipe, len, flags); |
| 489 | |
Jens Axboe | d366d398 | 2007-06-01 14:54:11 +0200 | [diff] [blame] | 490 | isize = i_size_read(in->f_mapping->host); |
| 491 | if (unlikely(*ppos >= isize)) |
| 492 | return 0; |
| 493 | |
| 494 | left = isize - *ppos; |
| 495 | if (unlikely(left < len)) |
| 496 | len = left; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 497 | |
Jens Axboe | 8191ecd | 2008-04-10 08:24:25 +0200 | [diff] [blame] | 498 | ret = __generic_file_splice_read(in, ppos, pipe, len, flags); |
Miklos Szeredi | 723590e | 2009-08-15 08:43:22 +0200 | [diff] [blame] | 499 | if (ret > 0) { |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 500 | *ppos += ret; |
Miklos Szeredi | 723590e | 2009-08-15 08:43:22 +0200 | [diff] [blame] | 501 | file_accessed(in); |
| 502 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 503 | |
| 504 | return ret; |
| 505 | } |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 506 | EXPORT_SYMBOL(generic_file_splice_read); |
| 507 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 508 | static const struct pipe_buf_operations default_pipe_buf_ops = { |
| 509 | .can_merge = 0, |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 510 | .confirm = generic_pipe_buf_confirm, |
| 511 | .release = generic_pipe_buf_release, |
| 512 | .steal = generic_pipe_buf_steal, |
| 513 | .get = generic_pipe_buf_get, |
| 514 | }; |
| 515 | |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 516 | static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe, |
| 517 | struct pipe_buffer *buf) |
| 518 | { |
| 519 | return 1; |
| 520 | } |
| 521 | |
| 522 | /* Pipe buffer operations for a socket and similar. */ |
| 523 | const struct pipe_buf_operations nosteal_pipe_buf_ops = { |
| 524 | .can_merge = 0, |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 525 | .confirm = generic_pipe_buf_confirm, |
| 526 | .release = generic_pipe_buf_release, |
| 527 | .steal = generic_pipe_buf_nosteal, |
| 528 | .get = generic_pipe_buf_get, |
| 529 | }; |
| 530 | EXPORT_SYMBOL(nosteal_pipe_buf_ops); |
| 531 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 532 | static ssize_t kernel_readv(struct file *file, const struct iovec *vec, |
| 533 | unsigned long vlen, loff_t offset) |
| 534 | { |
| 535 | mm_segment_t old_fs; |
| 536 | loff_t pos = offset; |
| 537 | ssize_t res; |
| 538 | |
| 539 | old_fs = get_fs(); |
| 540 | set_fs(get_ds()); |
| 541 | /* The cast to a user pointer is valid due to the set_fs() */ |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 542 | res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0); |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 543 | set_fs(old_fs); |
| 544 | |
| 545 | return res; |
| 546 | } |
| 547 | |
Al Viro | 7bb307e | 2013-02-23 14:51:48 -0500 | [diff] [blame] | 548 | ssize_t kernel_write(struct file *file, const char *buf, size_t count, |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 549 | loff_t pos) |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 550 | { |
| 551 | mm_segment_t old_fs; |
| 552 | ssize_t res; |
| 553 | |
| 554 | old_fs = get_fs(); |
| 555 | set_fs(get_ds()); |
| 556 | /* The cast to a user pointer is valid due to the set_fs() */ |
Al Viro | 7bb307e | 2013-02-23 14:51:48 -0500 | [diff] [blame] | 557 | res = vfs_write(file, (__force const char __user *)buf, count, &pos); |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 558 | set_fs(old_fs); |
| 559 | |
| 560 | return res; |
| 561 | } |
Al Viro | 7bb307e | 2013-02-23 14:51:48 -0500 | [diff] [blame] | 562 | EXPORT_SYMBOL(kernel_write); |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 563 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 564 | ssize_t default_file_splice_read(struct file *in, loff_t *ppos, |
| 565 | struct pipe_inode_info *pipe, size_t len, |
| 566 | unsigned int flags) |
| 567 | { |
| 568 | unsigned int nr_pages; |
| 569 | unsigned int nr_freed; |
| 570 | size_t offset; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 571 | struct page *pages[PIPE_DEF_BUFFERS]; |
| 572 | struct partial_page partial[PIPE_DEF_BUFFERS]; |
| 573 | struct iovec *vec, __vec[PIPE_DEF_BUFFERS]; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 574 | ssize_t res; |
| 575 | size_t this_len; |
| 576 | int error; |
| 577 | int i; |
| 578 | struct splice_pipe_desc spd = { |
| 579 | .pages = pages, |
| 580 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 581 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 582 | .flags = flags, |
| 583 | .ops = &default_pipe_buf_ops, |
| 584 | .spd_release = spd_release_page, |
| 585 | }; |
| 586 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 587 | if (splice_grow_spd(pipe, &spd)) |
| 588 | return -ENOMEM; |
| 589 | |
| 590 | res = -ENOMEM; |
| 591 | vec = __vec; |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 592 | if (spd.nr_pages_max > PIPE_DEF_BUFFERS) { |
| 593 | vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 594 | if (!vec) |
| 595 | goto shrink_ret; |
| 596 | } |
| 597 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 598 | offset = *ppos & ~PAGE_MASK; |
| 599 | nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 600 | |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 601 | for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) { |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 602 | struct page *page; |
| 603 | |
Jens Axboe | 4f23122 | 2009-05-13 08:35:35 +0200 | [diff] [blame] | 604 | page = alloc_page(GFP_USER); |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 605 | error = -ENOMEM; |
| 606 | if (!page) |
| 607 | goto err; |
| 608 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 609 | this_len = min_t(size_t, len, PAGE_SIZE - offset); |
Jens Axboe | 4f23122 | 2009-05-13 08:35:35 +0200 | [diff] [blame] | 610 | vec[i].iov_base = (void __user *) page_address(page); |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 611 | vec[i].iov_len = this_len; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 612 | spd.pages[i] = page; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 613 | spd.nr_pages++; |
| 614 | len -= this_len; |
| 615 | offset = 0; |
| 616 | } |
| 617 | |
| 618 | res = kernel_readv(in, vec, spd.nr_pages, *ppos); |
Andrew Morton | 77f6bf5 | 2009-05-14 09:49:44 +0200 | [diff] [blame] | 619 | if (res < 0) { |
| 620 | error = res; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 621 | goto err; |
Andrew Morton | 77f6bf5 | 2009-05-14 09:49:44 +0200 | [diff] [blame] | 622 | } |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 623 | |
| 624 | error = 0; |
| 625 | if (!res) |
| 626 | goto err; |
| 627 | |
| 628 | nr_freed = 0; |
| 629 | for (i = 0; i < spd.nr_pages; i++) { |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 630 | this_len = min_t(size_t, vec[i].iov_len, res); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 631 | spd.partial[i].offset = 0; |
| 632 | spd.partial[i].len = this_len; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 633 | if (!this_len) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 634 | __free_page(spd.pages[i]); |
| 635 | spd.pages[i] = NULL; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 636 | nr_freed++; |
| 637 | } |
| 638 | res -= this_len; |
| 639 | } |
| 640 | spd.nr_pages -= nr_freed; |
| 641 | |
| 642 | res = splice_to_pipe(pipe, &spd); |
| 643 | if (res > 0) |
| 644 | *ppos += res; |
| 645 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 646 | shrink_ret: |
| 647 | if (vec != __vec) |
| 648 | kfree(vec); |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 649 | splice_shrink_spd(&spd); |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 650 | return res; |
| 651 | |
| 652 | err: |
Jens Axboe | 4f23122 | 2009-05-13 08:35:35 +0200 | [diff] [blame] | 653 | for (i = 0; i < spd.nr_pages; i++) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 654 | __free_page(spd.pages[i]); |
Jens Axboe | 4f23122 | 2009-05-13 08:35:35 +0200 | [diff] [blame] | 655 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 656 | res = error; |
| 657 | goto shrink_ret; |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 658 | } |
| 659 | EXPORT_SYMBOL(default_file_splice_read); |
| 660 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 661 | /* |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 662 | * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 663 | * using sendpage(). Return the number of bytes sent. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 664 | */ |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 665 | static int pipe_to_sendpage(struct pipe_inode_info *pipe, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 666 | struct pipe_buffer *buf, struct splice_desc *sd) |
| 667 | { |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 668 | struct file *file = sd->u.file; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 669 | loff_t pos = sd->pos; |
Michał Mirosław | a8adbe3 | 2010-12-17 08:56:44 +0100 | [diff] [blame] | 670 | int more; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 671 | |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 672 | if (!likely(file->f_op->sendpage)) |
Michał Mirosław | a8adbe3 | 2010-12-17 08:56:44 +0100 | [diff] [blame] | 673 | return -EINVAL; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 674 | |
Eric Dumazet | 35f9c09 | 2012-04-05 03:05:35 +0000 | [diff] [blame] | 675 | more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; |
Eric Dumazet | ae62ca7 | 2013-01-06 18:21:49 +0000 | [diff] [blame] | 676 | |
| 677 | if (sd->len < sd->total_len && pipe->nrbufs > 1) |
Eric Dumazet | 35f9c09 | 2012-04-05 03:05:35 +0000 | [diff] [blame] | 678 | more |= MSG_SENDPAGE_NOTLAST; |
Eric Dumazet | ae62ca7 | 2013-01-06 18:21:49 +0000 | [diff] [blame] | 679 | |
Michał Mirosław | a8adbe3 | 2010-12-17 08:56:44 +0100 | [diff] [blame] | 680 | return file->f_op->sendpage(file, buf->page, buf->offset, |
| 681 | sd->len, &pos, more); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 684 | static void wakeup_pipe_writers(struct pipe_inode_info *pipe) |
| 685 | { |
| 686 | smp_mb(); |
| 687 | if (waitqueue_active(&pipe->wait)) |
| 688 | wake_up_interruptible(&pipe->wait); |
| 689 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * splice_from_pipe_feed - feed available data from a pipe to a file |
| 694 | * @pipe: pipe to splice from |
| 695 | * @sd: information to @actor |
| 696 | * @actor: handler that splices the data |
| 697 | * |
| 698 | * Description: |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 699 | * This function loops over the pipe and calls @actor to do the |
| 700 | * actual moving of a single struct pipe_buffer to the desired |
| 701 | * destination. It returns when there's no more buffers left in |
| 702 | * the pipe or if the requested number of bytes (@sd->total_len) |
| 703 | * have been copied. It returns a positive number (one) if the |
| 704 | * pipe needs to be filled with more data, zero if the required |
| 705 | * number of bytes have been copied and -errno on error. |
| 706 | * |
| 707 | * This, together with splice_from_pipe_{begin,end,next}, may be |
| 708 | * used to implement the functionality of __splice_from_pipe() when |
| 709 | * locking is required around copying the pipe buffers to the |
| 710 | * destination. |
| 711 | */ |
Al Viro | 96f9bc8 | 2014-04-05 04:35:49 -0400 | [diff] [blame] | 712 | static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 713 | splice_actor *actor) |
| 714 | { |
| 715 | int ret; |
| 716 | |
| 717 | while (pipe->nrbufs) { |
| 718 | struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; |
| 719 | const struct pipe_buf_operations *ops = buf->ops; |
| 720 | |
| 721 | sd->len = buf->len; |
| 722 | if (sd->len > sd->total_len) |
| 723 | sd->len = sd->total_len; |
| 724 | |
Michał Mirosław | a8adbe3 | 2010-12-17 08:56:44 +0100 | [diff] [blame] | 725 | ret = buf->ops->confirm(pipe, buf); |
| 726 | if (unlikely(ret)) { |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 727 | if (ret == -ENODATA) |
| 728 | ret = 0; |
| 729 | return ret; |
| 730 | } |
Michał Mirosław | a8adbe3 | 2010-12-17 08:56:44 +0100 | [diff] [blame] | 731 | |
| 732 | ret = actor(pipe, buf, sd); |
| 733 | if (ret <= 0) |
| 734 | return ret; |
| 735 | |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 736 | buf->offset += ret; |
| 737 | buf->len -= ret; |
| 738 | |
| 739 | sd->num_spliced += ret; |
| 740 | sd->len -= ret; |
| 741 | sd->pos += ret; |
| 742 | sd->total_len -= ret; |
| 743 | |
| 744 | if (!buf->len) { |
| 745 | buf->ops = NULL; |
| 746 | ops->release(pipe, buf); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 747 | pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 748 | pipe->nrbufs--; |
Al Viro | 6447a3c | 2013-03-21 11:01:38 -0400 | [diff] [blame] | 749 | if (pipe->files) |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 750 | sd->need_wakeup = true; |
| 751 | } |
| 752 | |
| 753 | if (!sd->total_len) |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | return 1; |
| 758 | } |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 759 | |
| 760 | /** |
| 761 | * splice_from_pipe_next - wait for some data to splice from |
| 762 | * @pipe: pipe to splice from |
| 763 | * @sd: information about the splice operation |
| 764 | * |
| 765 | * Description: |
| 766 | * This function will wait for some data and return a positive |
| 767 | * value (one) if pipe buffers are available. It will return zero |
| 768 | * or -errno if no more data needs to be spliced. |
| 769 | */ |
Al Viro | 96f9bc8 | 2014-04-05 04:35:49 -0400 | [diff] [blame] | 770 | static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 771 | { |
Jan Kara | c725bfc | 2015-11-23 13:09:50 +0100 | [diff] [blame] | 772 | /* |
| 773 | * Check for signal early to make process killable when there are |
| 774 | * always buffers available |
| 775 | */ |
| 776 | if (signal_pending(current)) |
| 777 | return -ERESTARTSYS; |
| 778 | |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 779 | while (!pipe->nrbufs) { |
| 780 | if (!pipe->writers) |
| 781 | return 0; |
| 782 | |
| 783 | if (!pipe->waiting_writers && sd->num_spliced) |
| 784 | return 0; |
| 785 | |
| 786 | if (sd->flags & SPLICE_F_NONBLOCK) |
| 787 | return -EAGAIN; |
| 788 | |
| 789 | if (signal_pending(current)) |
| 790 | return -ERESTARTSYS; |
| 791 | |
| 792 | if (sd->need_wakeup) { |
| 793 | wakeup_pipe_writers(pipe); |
| 794 | sd->need_wakeup = false; |
| 795 | } |
| 796 | |
| 797 | pipe_wait(pipe); |
| 798 | } |
| 799 | |
| 800 | return 1; |
| 801 | } |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 802 | |
| 803 | /** |
| 804 | * splice_from_pipe_begin - start splicing from pipe |
Randy Dunlap | b80901b | 2009-04-16 19:09:55 -0700 | [diff] [blame] | 805 | * @sd: information about the splice operation |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 806 | * |
| 807 | * Description: |
| 808 | * This function should be called before a loop containing |
| 809 | * splice_from_pipe_next() and splice_from_pipe_feed() to |
| 810 | * initialize the necessary fields of @sd. |
| 811 | */ |
Al Viro | 96f9bc8 | 2014-04-05 04:35:49 -0400 | [diff] [blame] | 812 | static void splice_from_pipe_begin(struct splice_desc *sd) |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 813 | { |
| 814 | sd->num_spliced = 0; |
| 815 | sd->need_wakeup = false; |
| 816 | } |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 817 | |
| 818 | /** |
| 819 | * splice_from_pipe_end - finish splicing from pipe |
| 820 | * @pipe: pipe to splice from |
| 821 | * @sd: information about the splice operation |
| 822 | * |
| 823 | * Description: |
| 824 | * This function will wake up pipe writers if necessary. It should |
| 825 | * be called after a loop containing splice_from_pipe_next() and |
| 826 | * splice_from_pipe_feed(). |
| 827 | */ |
Al Viro | 96f9bc8 | 2014-04-05 04:35:49 -0400 | [diff] [blame] | 828 | static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 829 | { |
| 830 | if (sd->need_wakeup) |
| 831 | wakeup_pipe_writers(pipe); |
| 832 | } |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 833 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 834 | /** |
| 835 | * __splice_from_pipe - splice data from a pipe to given actor |
| 836 | * @pipe: pipe to splice from |
| 837 | * @sd: information to @actor |
| 838 | * @actor: handler that splices the data |
| 839 | * |
| 840 | * Description: |
| 841 | * This function does little more than loop over the pipe and call |
| 842 | * @actor to do the actual moving of a single struct pipe_buffer to |
| 843 | * the desired destination. See pipe_to_file, pipe_to_sendpage, or |
| 844 | * pipe_to_user. |
| 845 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 846 | */ |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 847 | ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, |
| 848 | splice_actor *actor) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 849 | { |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 850 | int ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 851 | |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 852 | splice_from_pipe_begin(sd); |
| 853 | do { |
Jan Kara | c2489e0 | 2015-11-23 13:09:51 +0100 | [diff] [blame] | 854 | cond_resched(); |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 855 | ret = splice_from_pipe_next(pipe, sd); |
| 856 | if (ret > 0) |
| 857 | ret = splice_from_pipe_feed(pipe, sd, actor); |
| 858 | } while (ret > 0); |
| 859 | splice_from_pipe_end(pipe, sd); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 860 | |
Miklos Szeredi | b3c2d2d | 2009-04-14 19:48:36 +0200 | [diff] [blame] | 861 | return sd->num_spliced ? sd->num_spliced : ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 862 | } |
Mark Fasheh | 40bee44e | 2007-03-21 13:11:02 +0100 | [diff] [blame] | 863 | EXPORT_SYMBOL(__splice_from_pipe); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 864 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 865 | /** |
| 866 | * splice_from_pipe - splice data from a pipe to a file |
| 867 | * @pipe: pipe to splice from |
| 868 | * @out: file to splice to |
| 869 | * @ppos: position in @out |
| 870 | * @len: how many bytes to splice |
| 871 | * @flags: splice modifier flags |
| 872 | * @actor: handler that splices the data |
| 873 | * |
| 874 | * Description: |
Miklos Szeredi | 2933970 | 2009-04-14 19:48:37 +0200 | [diff] [blame] | 875 | * See __splice_from_pipe. This function locks the pipe inode, |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 876 | * otherwise it's identical to __splice_from_pipe(). |
| 877 | * |
| 878 | */ |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 879 | ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, |
| 880 | loff_t *ppos, size_t len, unsigned int flags, |
| 881 | splice_actor *actor) |
| 882 | { |
| 883 | ssize_t ret; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 884 | struct splice_desc sd = { |
| 885 | .total_len = len, |
| 886 | .flags = flags, |
| 887 | .pos = *ppos, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 888 | .u.file = out, |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 889 | }; |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 890 | |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 891 | pipe_lock(pipe); |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 892 | ret = __splice_from_pipe(pipe, &sd, actor); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 893 | pipe_unlock(pipe); |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 894 | |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | /** |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 899 | * iter_file_splice_write - splice data from a pipe to a file |
| 900 | * @pipe: pipe info |
| 901 | * @out: file to write to |
| 902 | * @ppos: position in @out |
| 903 | * @len: number of bytes to splice |
| 904 | * @flags: splice modifier flags |
| 905 | * |
| 906 | * Description: |
| 907 | * Will either move or copy pages (determined by @flags options) from |
| 908 | * the given pipe inode to the given file. |
| 909 | * This one is ->write_iter-based. |
| 910 | * |
| 911 | */ |
| 912 | ssize_t |
| 913 | iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, |
| 914 | loff_t *ppos, size_t len, unsigned int flags) |
| 915 | { |
| 916 | struct splice_desc sd = { |
| 917 | .total_len = len, |
| 918 | .flags = flags, |
| 919 | .pos = *ppos, |
| 920 | .u.file = out, |
| 921 | }; |
| 922 | int nbufs = pipe->buffers; |
| 923 | struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), |
| 924 | GFP_KERNEL); |
| 925 | ssize_t ret; |
| 926 | |
| 927 | if (unlikely(!array)) |
| 928 | return -ENOMEM; |
| 929 | |
| 930 | pipe_lock(pipe); |
| 931 | |
| 932 | splice_from_pipe_begin(&sd); |
| 933 | while (sd.total_len) { |
| 934 | struct iov_iter from; |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 935 | size_t left; |
| 936 | int n, idx; |
| 937 | |
| 938 | ret = splice_from_pipe_next(pipe, &sd); |
| 939 | if (ret <= 0) |
| 940 | break; |
| 941 | |
| 942 | if (unlikely(nbufs < pipe->buffers)) { |
| 943 | kfree(array); |
| 944 | nbufs = pipe->buffers; |
| 945 | array = kcalloc(nbufs, sizeof(struct bio_vec), |
| 946 | GFP_KERNEL); |
| 947 | if (!array) { |
| 948 | ret = -ENOMEM; |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* build the vector */ |
| 954 | left = sd.total_len; |
| 955 | for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) { |
| 956 | struct pipe_buffer *buf = pipe->bufs + idx; |
| 957 | size_t this_len = buf->len; |
| 958 | |
| 959 | if (this_len > left) |
| 960 | this_len = left; |
| 961 | |
| 962 | if (idx == pipe->buffers - 1) |
| 963 | idx = -1; |
| 964 | |
| 965 | ret = buf->ops->confirm(pipe, buf); |
| 966 | if (unlikely(ret)) { |
| 967 | if (ret == -ENODATA) |
| 968 | ret = 0; |
| 969 | goto done; |
| 970 | } |
| 971 | |
| 972 | array[n].bv_page = buf->page; |
| 973 | array[n].bv_len = this_len; |
| 974 | array[n].bv_offset = buf->offset; |
| 975 | left -= this_len; |
| 976 | } |
| 977 | |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 978 | iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n, |
| 979 | sd.total_len - left); |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 980 | ret = vfs_iter_write(out, &from, &sd.pos); |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 981 | if (ret <= 0) |
| 982 | break; |
| 983 | |
| 984 | sd.num_spliced += ret; |
| 985 | sd.total_len -= ret; |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 986 | *ppos = sd.pos; |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 987 | |
| 988 | /* dismiss the fully eaten buffers, adjust the partial one */ |
| 989 | while (ret) { |
| 990 | struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; |
| 991 | if (ret >= buf->len) { |
| 992 | const struct pipe_buf_operations *ops = buf->ops; |
| 993 | ret -= buf->len; |
| 994 | buf->len = 0; |
| 995 | buf->ops = NULL; |
| 996 | ops->release(pipe, buf); |
| 997 | pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); |
| 998 | pipe->nrbufs--; |
| 999 | if (pipe->files) |
| 1000 | sd.need_wakeup = true; |
| 1001 | } else { |
| 1002 | buf->offset += ret; |
| 1003 | buf->len -= ret; |
| 1004 | ret = 0; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | done: |
| 1009 | kfree(array); |
| 1010 | splice_from_pipe_end(pipe, &sd); |
| 1011 | |
| 1012 | pipe_unlock(pipe); |
| 1013 | |
| 1014 | if (sd.num_spliced) |
| 1015 | ret = sd.num_spliced; |
| 1016 | |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | EXPORT_SYMBOL(iter_file_splice_write); |
| 1021 | |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1022 | static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
| 1023 | struct splice_desc *sd) |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1024 | { |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1025 | int ret; |
| 1026 | void *data; |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 1027 | loff_t tmp = sd->pos; |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1028 | |
Al Viro | fbb3275 | 2014-02-02 21:09:54 -0500 | [diff] [blame] | 1029 | data = kmap(buf->page); |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 1030 | ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); |
Al Viro | fbb3275 | 2014-02-02 21:09:54 -0500 | [diff] [blame] | 1031 | kunmap(buf->page); |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1032 | |
| 1033 | return ret; |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, |
| 1037 | struct file *out, loff_t *ppos, |
| 1038 | size_t len, unsigned int flags) |
| 1039 | { |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1040 | ssize_t ret; |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1041 | |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1042 | ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); |
| 1043 | if (ret > 0) |
| 1044 | *ppos += ret; |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1045 | |
Miklos Szeredi | b2858d7 | 2009-05-19 11:37:46 +0200 | [diff] [blame] | 1046 | return ret; |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1049 | /** |
| 1050 | * generic_splice_sendpage - splice data from a pipe to a socket |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1051 | * @pipe: pipe to splice from |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1052 | * @out: socket to write to |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1053 | * @ppos: position in @out |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1054 | * @len: number of bytes to splice |
| 1055 | * @flags: splice modifier flags |
| 1056 | * |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1057 | * Description: |
| 1058 | * Will send @len bytes from the pipe to a network socket. No data copying |
| 1059 | * is involved. |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1060 | * |
| 1061 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1062 | ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1063 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1064 | { |
Jens Axboe | 00522fb | 2006-04-26 14:39:29 +0200 | [diff] [blame] | 1065 | return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 1068 | EXPORT_SYMBOL(generic_splice_sendpage); |
Jeff Garzik | a0f0678 | 2006-03-30 23:06:13 -0500 | [diff] [blame] | 1069 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1070 | /* |
| 1071 | * Attempt to initiate a splice from pipe to file. |
| 1072 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1073 | static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1074 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1075 | { |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1076 | ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, |
| 1077 | loff_t *, size_t, unsigned int); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1078 | |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 1079 | if (out->f_op->splice_write) |
Changli Gao | cc56f7d | 2009-11-04 09:09:52 +0100 | [diff] [blame] | 1080 | splice_write = out->f_op->splice_write; |
| 1081 | else |
Miklos Szeredi | 0b0a47f | 2009-05-07 15:37:37 +0200 | [diff] [blame] | 1082 | splice_write = default_file_splice_write; |
| 1083 | |
Al Viro | 500368f | 2013-05-23 20:07:11 -0400 | [diff] [blame] | 1084 | return splice_write(pipe, out, ppos, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1087 | /* |
| 1088 | * Attempt to initiate a splice from a file to a pipe. |
| 1089 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1090 | static long do_splice_to(struct file *in, loff_t *ppos, |
| 1091 | struct pipe_inode_info *pipe, size_t len, |
| 1092 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1093 | { |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 1094 | ssize_t (*splice_read)(struct file *, loff_t *, |
| 1095 | struct pipe_inode_info *, size_t, unsigned int); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1096 | int ret; |
| 1097 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 1098 | if (unlikely(!(in->f_mode & FMODE_READ))) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1099 | return -EBADF; |
| 1100 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1101 | ret = rw_verify_area(READ, in, ppos, len); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1102 | if (unlikely(ret < 0)) |
| 1103 | return ret; |
| 1104 | |
Al Viro | 03cc078 | 2016-04-02 14:56:58 -0400 | [diff] [blame] | 1105 | if (unlikely(len > MAX_RW_COUNT)) |
| 1106 | len = MAX_RW_COUNT; |
| 1107 | |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 1108 | if (in->f_op->splice_read) |
Changli Gao | cc56f7d | 2009-11-04 09:09:52 +0100 | [diff] [blame] | 1109 | splice_read = in->f_op->splice_read; |
| 1110 | else |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 1111 | splice_read = default_file_splice_read; |
| 1112 | |
| 1113 | return splice_read(in, ppos, pipe, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1116 | /** |
| 1117 | * splice_direct_to_actor - splices data directly between two non-pipes |
| 1118 | * @in: file to splice from |
| 1119 | * @sd: actor information on where to splice to |
| 1120 | * @actor: handles the data splicing |
| 1121 | * |
| 1122 | * Description: |
| 1123 | * This is a special case helper to splice directly between two |
| 1124 | * points, without requiring an explicit pipe. Internally an allocated |
Randy Dunlap | 79685b8 | 2007-07-27 08:08:51 +0200 | [diff] [blame] | 1125 | * pipe is cached in the process, and reused during the lifetime of |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1126 | * that process. |
| 1127 | * |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1128 | */ |
| 1129 | ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, |
| 1130 | splice_direct_actor *actor) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1131 | { |
| 1132 | struct pipe_inode_info *pipe; |
| 1133 | long ret, bytes; |
| 1134 | umode_t i_mode; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1135 | size_t len; |
Christophe Leroy | 0ff28d9 | 2015-05-06 17:26:47 +0200 | [diff] [blame] | 1136 | int i, flags, more; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | * We require the input being a regular file, as we don't want to |
| 1140 | * randomly drop data for eg socket -> socket splicing. Use the |
| 1141 | * piped splicing for that! |
| 1142 | */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1143 | i_mode = file_inode(in)->i_mode; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1144 | if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) |
| 1145 | return -EINVAL; |
| 1146 | |
| 1147 | /* |
| 1148 | * neither in nor out is a pipe, setup an internal pipe attached to |
| 1149 | * 'out' and transfer the wanted data from 'in' to 'out' through that |
| 1150 | */ |
| 1151 | pipe = current->splice_pipe; |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 1152 | if (unlikely(!pipe)) { |
Al Viro | 7bee130 | 2013-03-21 11:04:15 -0400 | [diff] [blame] | 1153 | pipe = alloc_pipe_info(); |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1154 | if (!pipe) |
| 1155 | return -ENOMEM; |
| 1156 | |
| 1157 | /* |
| 1158 | * We don't have an immediate reader, but we'll read the stuff |
Jens Axboe | 00522fb | 2006-04-26 14:39:29 +0200 | [diff] [blame] | 1159 | * out of the pipe right after the splice_to_pipe(). So set |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1160 | * PIPE_READERS appropriately. |
| 1161 | */ |
| 1162 | pipe->readers = 1; |
| 1163 | |
| 1164 | current->splice_pipe = pipe; |
| 1165 | } |
| 1166 | |
| 1167 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 1168 | * Do the splice. |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1169 | */ |
| 1170 | ret = 0; |
| 1171 | bytes = 0; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1172 | len = sd->total_len; |
| 1173 | flags = sd->flags; |
| 1174 | |
| 1175 | /* |
| 1176 | * Don't block on output, we have to drain the direct pipe. |
| 1177 | */ |
| 1178 | sd->flags &= ~SPLICE_F_NONBLOCK; |
Christophe Leroy | 0ff28d9 | 2015-05-06 17:26:47 +0200 | [diff] [blame] | 1179 | more = sd->flags & SPLICE_F_MORE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1180 | |
| 1181 | while (len) { |
Jens Axboe | 51a92c0 | 2007-07-13 14:11:43 +0200 | [diff] [blame] | 1182 | size_t read_len; |
Tom Zanussi | a82c53a | 2008-05-09 13:28:36 +0200 | [diff] [blame] | 1183 | loff_t pos = sd->pos, prev_pos = pos; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1184 | |
Jens Axboe | bcd4f3a | 2007-07-16 14:41:49 +0200 | [diff] [blame] | 1185 | ret = do_splice_to(in, &pos, pipe, len, flags); |
Jens Axboe | 51a92c0 | 2007-07-13 14:11:43 +0200 | [diff] [blame] | 1186 | if (unlikely(ret <= 0)) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1187 | goto out_release; |
| 1188 | |
| 1189 | read_len = ret; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1190 | sd->total_len = read_len; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1191 | |
| 1192 | /* |
Christophe Leroy | 0ff28d9 | 2015-05-06 17:26:47 +0200 | [diff] [blame] | 1193 | * If more data is pending, set SPLICE_F_MORE |
| 1194 | * If this is the last data and SPLICE_F_MORE was not set |
| 1195 | * initially, clears it. |
| 1196 | */ |
| 1197 | if (read_len < len) |
| 1198 | sd->flags |= SPLICE_F_MORE; |
| 1199 | else if (!more) |
| 1200 | sd->flags &= ~SPLICE_F_MORE; |
| 1201 | /* |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1202 | * NOTE: nonblocking mode only applies to the input. We |
| 1203 | * must not do the output in nonblocking mode as then we |
| 1204 | * could get stuck data in the internal pipe: |
| 1205 | */ |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1206 | ret = actor(pipe, sd); |
Tom Zanussi | a82c53a | 2008-05-09 13:28:36 +0200 | [diff] [blame] | 1207 | if (unlikely(ret <= 0)) { |
| 1208 | sd->pos = prev_pos; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1209 | goto out_release; |
Tom Zanussi | a82c53a | 2008-05-09 13:28:36 +0200 | [diff] [blame] | 1210 | } |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1211 | |
| 1212 | bytes += ret; |
| 1213 | len -= ret; |
Jens Axboe | bcd4f3a | 2007-07-16 14:41:49 +0200 | [diff] [blame] | 1214 | sd->pos = pos; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1215 | |
Tom Zanussi | a82c53a | 2008-05-09 13:28:36 +0200 | [diff] [blame] | 1216 | if (ret < read_len) { |
| 1217 | sd->pos = prev_pos + ret; |
Jens Axboe | 51a92c0 | 2007-07-13 14:11:43 +0200 | [diff] [blame] | 1218 | goto out_release; |
Tom Zanussi | a82c53a | 2008-05-09 13:28:36 +0200 | [diff] [blame] | 1219 | } |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Jens Axboe | 9e97198 | 2008-01-29 21:05:57 +0100 | [diff] [blame] | 1222 | done: |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1223 | pipe->nrbufs = pipe->curbuf = 0; |
Jens Axboe | 8084870 | 2008-01-30 12:24:48 +0100 | [diff] [blame] | 1224 | file_accessed(in); |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1225 | return bytes; |
| 1226 | |
| 1227 | out_release: |
| 1228 | /* |
| 1229 | * If we did an incomplete transfer we must release |
| 1230 | * the pipe buffers in question: |
| 1231 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1232 | for (i = 0; i < pipe->buffers; i++) { |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1233 | struct pipe_buffer *buf = pipe->bufs + i; |
| 1234 | |
| 1235 | if (buf->ops) { |
| 1236 | buf->ops->release(pipe, buf); |
| 1237 | buf->ops = NULL; |
| 1238 | } |
| 1239 | } |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1240 | |
Jens Axboe | 9e97198 | 2008-01-29 21:05:57 +0100 | [diff] [blame] | 1241 | if (!bytes) |
| 1242 | bytes = ret; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1243 | |
Jens Axboe | 9e97198 | 2008-01-29 21:05:57 +0100 | [diff] [blame] | 1244 | goto done; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1245 | } |
| 1246 | EXPORT_SYMBOL(splice_direct_to_actor); |
| 1247 | |
| 1248 | static int direct_splice_actor(struct pipe_inode_info *pipe, |
| 1249 | struct splice_desc *sd) |
| 1250 | { |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1251 | struct file *file = sd->u.file; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1252 | |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1253 | return do_splice_from(pipe, file, sd->opos, sd->total_len, |
Changli Gao | 2cb4b05 | 2010-06-29 13:09:18 +0200 | [diff] [blame] | 1254 | sd->flags); |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1257 | /** |
| 1258 | * do_splice_direct - splices data directly between two files |
| 1259 | * @in: file to splice from |
| 1260 | * @ppos: input file offset |
| 1261 | * @out: file to splice to |
Randy Dunlap | acdb37c | 2013-06-22 19:44:08 -0700 | [diff] [blame] | 1262 | * @opos: output file offset |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1263 | * @len: number of bytes to splice |
| 1264 | * @flags: splice modifier flags |
| 1265 | * |
| 1266 | * Description: |
| 1267 | * For use by do_sendfile(). splice can easily emulate sendfile, but |
| 1268 | * doing it in the application would incur an extra system call |
| 1269 | * (splice in + splice out, as compared to just sendfile()). So this helper |
| 1270 | * can splice directly through a process-private pipe. |
| 1271 | * |
| 1272 | */ |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1273 | long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1274 | loff_t *opos, size_t len, unsigned int flags) |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1275 | { |
| 1276 | struct splice_desc sd = { |
| 1277 | .len = len, |
| 1278 | .total_len = len, |
| 1279 | .flags = flags, |
| 1280 | .pos = *ppos, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1281 | .u.file = out, |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1282 | .opos = opos, |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1283 | }; |
Jens Axboe | 51a92c0 | 2007-07-13 14:11:43 +0200 | [diff] [blame] | 1284 | long ret; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1285 | |
Al Viro | 18c67cb | 2013-06-19 15:41:54 +0400 | [diff] [blame] | 1286 | if (unlikely(!(out->f_mode & FMODE_WRITE))) |
| 1287 | return -EBADF; |
| 1288 | |
| 1289 | if (unlikely(out->f_flags & O_APPEND)) |
| 1290 | return -EINVAL; |
| 1291 | |
| 1292 | ret = rw_verify_area(WRITE, out, opos, len); |
| 1293 | if (unlikely(ret < 0)) |
| 1294 | return ret; |
| 1295 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1296 | ret = splice_direct_to_actor(in, &sd, direct_splice_actor); |
Jens Axboe | 51a92c0 | 2007-07-13 14:11:43 +0200 | [diff] [blame] | 1297 | if (ret > 0) |
Tom Zanussi | a82c53a | 2008-05-09 13:28:36 +0200 | [diff] [blame] | 1298 | *ppos = sd.pos; |
Jens Axboe | 51a92c0 | 2007-07-13 14:11:43 +0200 | [diff] [blame] | 1299 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1300 | return ret; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1301 | } |
Miklos Szeredi | 1c11859 | 2014-10-24 00:14:35 +0200 | [diff] [blame] | 1302 | EXPORT_SYMBOL(do_splice_direct); |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1303 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 1304 | static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) |
| 1305 | { |
| 1306 | while (pipe->nrbufs == pipe->buffers) { |
| 1307 | if (flags & SPLICE_F_NONBLOCK) |
| 1308 | return -EAGAIN; |
| 1309 | if (signal_pending(current)) |
| 1310 | return -ERESTARTSYS; |
| 1311 | pipe->waiting_writers++; |
| 1312 | pipe_wait(pipe); |
| 1313 | pipe->waiting_writers--; |
| 1314 | } |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1318 | static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, |
| 1319 | struct pipe_inode_info *opipe, |
| 1320 | size_t len, unsigned int flags); |
Jens Axboe | ddac0d3 | 2006-11-04 12:49:32 +0100 | [diff] [blame] | 1321 | |
| 1322 | /* |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1323 | * Determine where to splice to/from. |
| 1324 | */ |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1325 | static long do_splice(struct file *in, loff_t __user *off_in, |
| 1326 | struct file *out, loff_t __user *off_out, |
| 1327 | size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1328 | { |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1329 | struct pipe_inode_info *ipipe; |
| 1330 | struct pipe_inode_info *opipe; |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1331 | loff_t offset; |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1332 | long ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1333 | |
Linus Torvalds | 71993e6 | 2010-11-28 13:56:09 -0800 | [diff] [blame] | 1334 | ipipe = get_pipe_info(in); |
| 1335 | opipe = get_pipe_info(out); |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1336 | |
| 1337 | if (ipipe && opipe) { |
| 1338 | if (off_in || off_out) |
| 1339 | return -ESPIPE; |
| 1340 | |
| 1341 | if (!(in->f_mode & FMODE_READ)) |
| 1342 | return -EBADF; |
| 1343 | |
| 1344 | if (!(out->f_mode & FMODE_WRITE)) |
| 1345 | return -EBADF; |
| 1346 | |
| 1347 | /* Splicing to self would be fun, but... */ |
| 1348 | if (ipipe == opipe) |
| 1349 | return -EINVAL; |
| 1350 | |
| 1351 | return splice_pipe_to_pipe(ipipe, opipe, len, flags); |
| 1352 | } |
| 1353 | |
| 1354 | if (ipipe) { |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1355 | if (off_in) |
| 1356 | return -ESPIPE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1357 | if (off_out) { |
Changli Gao | 19c9a49 | 2010-06-29 13:10:36 +0200 | [diff] [blame] | 1358 | if (!(out->f_mode & FMODE_PWRITE)) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1359 | return -EINVAL; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1360 | if (copy_from_user(&offset, off_out, sizeof(loff_t))) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1361 | return -EFAULT; |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1362 | } else { |
| 1363 | offset = out->f_pos; |
| 1364 | } |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1365 | |
Al Viro | 18c67cb | 2013-06-19 15:41:54 +0400 | [diff] [blame] | 1366 | if (unlikely(!(out->f_mode & FMODE_WRITE))) |
| 1367 | return -EBADF; |
| 1368 | |
| 1369 | if (unlikely(out->f_flags & O_APPEND)) |
| 1370 | return -EINVAL; |
| 1371 | |
| 1372 | ret = rw_verify_area(WRITE, out, &offset, len); |
| 1373 | if (unlikely(ret < 0)) |
| 1374 | return ret; |
| 1375 | |
Al Viro | 500368f | 2013-05-23 20:07:11 -0400 | [diff] [blame] | 1376 | file_start_write(out); |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1377 | ret = do_splice_from(ipipe, out, &offset, len, flags); |
Al Viro | 500368f | 2013-05-23 20:07:11 -0400 | [diff] [blame] | 1378 | file_end_write(out); |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1379 | |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1380 | if (!off_out) |
| 1381 | out->f_pos = offset; |
| 1382 | else if (copy_to_user(off_out, &offset, sizeof(loff_t))) |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1383 | ret = -EFAULT; |
| 1384 | |
| 1385 | return ret; |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1386 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1387 | |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1388 | if (opipe) { |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1389 | if (off_out) |
| 1390 | return -ESPIPE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1391 | if (off_in) { |
Changli Gao | 19c9a49 | 2010-06-29 13:10:36 +0200 | [diff] [blame] | 1392 | if (!(in->f_mode & FMODE_PREAD)) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1393 | return -EINVAL; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1394 | if (copy_from_user(&offset, off_in, sizeof(loff_t))) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1395 | return -EFAULT; |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1396 | } else { |
| 1397 | offset = in->f_pos; |
| 1398 | } |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1399 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 1400 | pipe_lock(opipe); |
| 1401 | ret = wait_for_space(opipe, flags); |
| 1402 | if (!ret) |
| 1403 | ret = do_splice_to(in, &offset, opipe, len, flags); |
| 1404 | pipe_unlock(opipe); |
| 1405 | if (ret > 0) |
| 1406 | wakeup_pipe_readers(opipe); |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1407 | if (!off_in) |
| 1408 | in->f_pos = offset; |
| 1409 | else if (copy_to_user(off_in, &offset, sizeof(loff_t))) |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1410 | ret = -EFAULT; |
| 1411 | |
| 1412 | return ret; |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1413 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1414 | |
| 1415 | return -EINVAL; |
| 1416 | } |
| 1417 | |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1418 | static int get_iovec_page_array(struct iov_iter *from, |
| 1419 | struct page **pages, |
| 1420 | struct partial_page *partial, |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1421 | unsigned int pipe_buffers) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1422 | { |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1423 | int buffers = 0; |
| 1424 | while (iov_iter_count(from)) { |
| 1425 | ssize_t copied; |
| 1426 | size_t start; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1427 | |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1428 | copied = iov_iter_get_pages(from, pages + buffers, ~0UL, |
| 1429 | pipe_buffers - buffers, &start); |
| 1430 | if (copied <= 0) |
| 1431 | return buffers ? buffers : copied; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1432 | |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1433 | iov_iter_advance(from, copied); |
| 1434 | while (copied) { |
| 1435 | int size = min_t(int, copied, PAGE_SIZE - start); |
| 1436 | partial[buffers].offset = start; |
| 1437 | partial[buffers].len = size; |
| 1438 | copied -= size; |
| 1439 | start = 0; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1440 | buffers++; |
| 1441 | } |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1442 | } |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1443 | return buffers; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1446 | static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
| 1447 | struct splice_desc *sd) |
| 1448 | { |
Al Viro | 6130f53 | 2014-02-03 18:19:51 -0500 | [diff] [blame] | 1449 | int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); |
| 1450 | return n == sd->len ? n : -EFAULT; |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * For lack of a better implementation, implement vmsplice() to userspace |
| 1455 | * as a simple copy of the pipes pages to the user iov. |
| 1456 | */ |
Al Viro | 6130f53 | 2014-02-03 18:19:51 -0500 | [diff] [blame] | 1457 | static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1458 | unsigned long nr_segs, unsigned int flags) |
| 1459 | { |
| 1460 | struct pipe_inode_info *pipe; |
| 1461 | struct splice_desc sd; |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1462 | long ret; |
Al Viro | 6130f53 | 2014-02-03 18:19:51 -0500 | [diff] [blame] | 1463 | struct iovec iovstack[UIO_FASTIOV]; |
| 1464 | struct iovec *iov = iovstack; |
| 1465 | struct iov_iter iter; |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1466 | |
Linus Torvalds | 71993e6 | 2010-11-28 13:56:09 -0800 | [diff] [blame] | 1467 | pipe = get_pipe_info(file); |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1468 | if (!pipe) |
| 1469 | return -EBADF; |
| 1470 | |
Al Viro | 345995f | 2015-03-21 19:17:55 -0400 | [diff] [blame] | 1471 | ret = import_iovec(READ, uiov, nr_segs, |
| 1472 | ARRAY_SIZE(iovstack), &iov, &iter); |
| 1473 | if (ret < 0) |
| 1474 | return ret; |
Al Viro | 6130f53 | 2014-02-03 18:19:51 -0500 | [diff] [blame] | 1475 | |
Al Viro | 345995f | 2015-03-21 19:17:55 -0400 | [diff] [blame] | 1476 | sd.total_len = iov_iter_count(&iter); |
Al Viro | 6130f53 | 2014-02-03 18:19:51 -0500 | [diff] [blame] | 1477 | sd.len = 0; |
Al Viro | 6130f53 | 2014-02-03 18:19:51 -0500 | [diff] [blame] | 1478 | sd.flags = flags; |
| 1479 | sd.u.data = &iter; |
| 1480 | sd.pos = 0; |
| 1481 | |
Al Viro | 345995f | 2015-03-21 19:17:55 -0400 | [diff] [blame] | 1482 | if (sd.total_len) { |
| 1483 | pipe_lock(pipe); |
| 1484 | ret = __splice_from_pipe(pipe, &sd, pipe_to_user); |
| 1485 | pipe_unlock(pipe); |
| 1486 | } |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1487 | |
Al Viro | 345995f | 2015-03-21 19:17:55 -0400 | [diff] [blame] | 1488 | kfree(iov); |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1489 | return ret; |
| 1490 | } |
| 1491 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1492 | /* |
| 1493 | * vmsplice splices a user address range into a pipe. It can be thought of |
| 1494 | * as splice-from-memory, where the regular splice is splice-from-file (or |
| 1495 | * to file). In both cases the output is a pipe, naturally. |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1496 | */ |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1497 | static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1498 | unsigned long nr_segs, unsigned int flags) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1499 | { |
Jens Axboe | ddac0d3 | 2006-11-04 12:49:32 +0100 | [diff] [blame] | 1500 | struct pipe_inode_info *pipe; |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1501 | struct iovec iovstack[UIO_FASTIOV]; |
| 1502 | struct iovec *iov = iovstack; |
| 1503 | struct iov_iter from; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1504 | struct page *pages[PIPE_DEF_BUFFERS]; |
| 1505 | struct partial_page partial[PIPE_DEF_BUFFERS]; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1506 | struct splice_pipe_desc spd = { |
| 1507 | .pages = pages, |
| 1508 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 1509 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1510 | .flags = flags, |
| 1511 | .ops = &user_page_pipe_buf_ops, |
Jens Axboe | bbdfc2f | 2007-11-06 23:29:47 -0800 | [diff] [blame] | 1512 | .spd_release = spd_release_page, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1513 | }; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1514 | long ret; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1515 | |
Linus Torvalds | 71993e6 | 2010-11-28 13:56:09 -0800 | [diff] [blame] | 1516 | pipe = get_pipe_info(file); |
Jens Axboe | ddac0d3 | 2006-11-04 12:49:32 +0100 | [diff] [blame] | 1517 | if (!pipe) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1518 | return -EBADF; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1519 | |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1520 | ret = import_iovec(WRITE, uiov, nr_segs, |
| 1521 | ARRAY_SIZE(iovstack), &iov, &from); |
| 1522 | if (ret < 0) |
| 1523 | return ret; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1524 | |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1525 | if (splice_grow_spd(pipe, &spd)) { |
| 1526 | kfree(iov); |
| 1527 | return -ENOMEM; |
| 1528 | } |
| 1529 | |
Al Viro | 8924fef | 2016-09-17 20:44:45 -0400 | [diff] [blame^] | 1530 | pipe_lock(pipe); |
| 1531 | ret = wait_for_space(pipe, flags); |
| 1532 | if (!ret) { |
| 1533 | spd.nr_pages = get_iovec_page_array(&from, spd.pages, |
| 1534 | spd.partial, |
| 1535 | spd.nr_pages_max); |
| 1536 | if (spd.nr_pages <= 0) |
| 1537 | ret = spd.nr_pages; |
| 1538 | else |
| 1539 | ret = splice_to_pipe(pipe, &spd); |
| 1540 | } |
| 1541 | pipe_unlock(pipe); |
| 1542 | if (ret > 0) |
| 1543 | wakeup_pipe_readers(pipe); |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 1544 | splice_shrink_spd(&spd); |
Al Viro | db85a9e | 2016-09-17 20:25:06 -0400 | [diff] [blame] | 1545 | kfree(iov); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1546 | return ret; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1549 | /* |
| 1550 | * Note that vmsplice only really supports true splicing _from_ user memory |
| 1551 | * to a pipe, not the other way around. Splicing from user memory is a simple |
| 1552 | * operation that can be supported without any funky alignment restrictions |
| 1553 | * or nasty vm tricks. We simply map in the user memory and fill them into |
| 1554 | * a pipe. The reverse isn't quite as easy, though. There are two possible |
| 1555 | * solutions for that: |
| 1556 | * |
| 1557 | * - memcpy() the data internally, at which point we might as well just |
| 1558 | * do a regular read() on the buffer anyway. |
| 1559 | * - Lots of nasty vm tricks, that are neither fast nor flexible (it |
| 1560 | * has restriction limitations on both ends of the pipe). |
| 1561 | * |
| 1562 | * Currently we punt and implement it as a normal copy, see pipe_to_user(). |
| 1563 | * |
| 1564 | */ |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 1565 | SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, |
| 1566 | unsigned long, nr_segs, unsigned int, flags) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1567 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1568 | struct fd f; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1569 | long error; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1570 | |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1571 | if (unlikely(nr_segs > UIO_MAXIOV)) |
| 1572 | return -EINVAL; |
| 1573 | else if (unlikely(!nr_segs)) |
| 1574 | return 0; |
| 1575 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1576 | error = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1577 | f = fdget(fd); |
| 1578 | if (f.file) { |
| 1579 | if (f.file->f_mode & FMODE_WRITE) |
| 1580 | error = vmsplice_to_pipe(f.file, iov, nr_segs, flags); |
| 1581 | else if (f.file->f_mode & FMODE_READ) |
| 1582 | error = vmsplice_to_user(f.file, iov, nr_segs, flags); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1583 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1584 | fdput(f); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | return error; |
| 1588 | } |
| 1589 | |
Al Viro | 76b021d | 2013-03-02 10:19:56 -0500 | [diff] [blame] | 1590 | #ifdef CONFIG_COMPAT |
| 1591 | COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32, |
| 1592 | unsigned int, nr_segs, unsigned int, flags) |
| 1593 | { |
| 1594 | unsigned i; |
| 1595 | struct iovec __user *iov; |
| 1596 | if (nr_segs > UIO_MAXIOV) |
| 1597 | return -EINVAL; |
| 1598 | iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec)); |
| 1599 | for (i = 0; i < nr_segs; i++) { |
| 1600 | struct compat_iovec v; |
| 1601 | if (get_user(v.iov_base, &iov32[i].iov_base) || |
| 1602 | get_user(v.iov_len, &iov32[i].iov_len) || |
| 1603 | put_user(compat_ptr(v.iov_base), &iov[i].iov_base) || |
| 1604 | put_user(v.iov_len, &iov[i].iov_len)) |
| 1605 | return -EFAULT; |
| 1606 | } |
| 1607 | return sys_vmsplice(fd, iov, nr_segs, flags); |
| 1608 | } |
| 1609 | #endif |
| 1610 | |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 1611 | SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, |
| 1612 | int, fd_out, loff_t __user *, off_out, |
| 1613 | size_t, len, unsigned int, flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1614 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1615 | struct fd in, out; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1616 | long error; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1617 | |
| 1618 | if (unlikely(!len)) |
| 1619 | return 0; |
| 1620 | |
| 1621 | error = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1622 | in = fdget(fd_in); |
| 1623 | if (in.file) { |
| 1624 | if (in.file->f_mode & FMODE_READ) { |
| 1625 | out = fdget(fd_out); |
| 1626 | if (out.file) { |
| 1627 | if (out.file->f_mode & FMODE_WRITE) |
| 1628 | error = do_splice(in.file, off_in, |
| 1629 | out.file, off_out, |
Ingo Molnar | 529565dc | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1630 | len, flags); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1631 | fdput(out); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1632 | } |
| 1633 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1634 | fdput(in); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1635 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1636 | return error; |
| 1637 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1638 | |
| 1639 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1640 | * Make sure there's data to read. Wait for input if we can, otherwise |
| 1641 | * return an appropriate error. |
| 1642 | */ |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1643 | static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1644 | { |
| 1645 | int ret; |
| 1646 | |
| 1647 | /* |
| 1648 | * Check ->nrbufs without the inode lock first. This function |
| 1649 | * is speculative anyways, so missing one is ok. |
| 1650 | */ |
| 1651 | if (pipe->nrbufs) |
| 1652 | return 0; |
| 1653 | |
| 1654 | ret = 0; |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1655 | pipe_lock(pipe); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1656 | |
| 1657 | while (!pipe->nrbufs) { |
| 1658 | if (signal_pending(current)) { |
| 1659 | ret = -ERESTARTSYS; |
| 1660 | break; |
| 1661 | } |
| 1662 | if (!pipe->writers) |
| 1663 | break; |
| 1664 | if (!pipe->waiting_writers) { |
| 1665 | if (flags & SPLICE_F_NONBLOCK) { |
| 1666 | ret = -EAGAIN; |
| 1667 | break; |
| 1668 | } |
| 1669 | } |
| 1670 | pipe_wait(pipe); |
| 1671 | } |
| 1672 | |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1673 | pipe_unlock(pipe); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1674 | return ret; |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * Make sure there's writeable room. Wait for room if we can, otherwise |
| 1679 | * return an appropriate error. |
| 1680 | */ |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1681 | static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1682 | { |
| 1683 | int ret; |
| 1684 | |
| 1685 | /* |
| 1686 | * Check ->nrbufs without the inode lock first. This function |
| 1687 | * is speculative anyways, so missing one is ok. |
| 1688 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1689 | if (pipe->nrbufs < pipe->buffers) |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1690 | return 0; |
| 1691 | |
| 1692 | ret = 0; |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1693 | pipe_lock(pipe); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1694 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1695 | while (pipe->nrbufs >= pipe->buffers) { |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1696 | if (!pipe->readers) { |
| 1697 | send_sig(SIGPIPE, current, 0); |
| 1698 | ret = -EPIPE; |
| 1699 | break; |
| 1700 | } |
| 1701 | if (flags & SPLICE_F_NONBLOCK) { |
| 1702 | ret = -EAGAIN; |
| 1703 | break; |
| 1704 | } |
| 1705 | if (signal_pending(current)) { |
| 1706 | ret = -ERESTARTSYS; |
| 1707 | break; |
| 1708 | } |
| 1709 | pipe->waiting_writers++; |
| 1710 | pipe_wait(pipe); |
| 1711 | pipe->waiting_writers--; |
| 1712 | } |
| 1713 | |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1714 | pipe_unlock(pipe); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1715 | return ret; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1719 | * Splice contents of ipipe to opipe. |
| 1720 | */ |
| 1721 | static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, |
| 1722 | struct pipe_inode_info *opipe, |
| 1723 | size_t len, unsigned int flags) |
| 1724 | { |
| 1725 | struct pipe_buffer *ibuf, *obuf; |
| 1726 | int ret = 0, nbuf; |
| 1727 | bool input_wakeup = false; |
| 1728 | |
| 1729 | |
| 1730 | retry: |
| 1731 | ret = ipipe_prep(ipipe, flags); |
| 1732 | if (ret) |
| 1733 | return ret; |
| 1734 | |
| 1735 | ret = opipe_prep(opipe, flags); |
| 1736 | if (ret) |
| 1737 | return ret; |
| 1738 | |
| 1739 | /* |
| 1740 | * Potential ABBA deadlock, work around it by ordering lock |
| 1741 | * grabbing by pipe info address. Otherwise two different processes |
| 1742 | * could deadlock (one doing tee from A -> B, the other from B -> A). |
| 1743 | */ |
| 1744 | pipe_double_lock(ipipe, opipe); |
| 1745 | |
| 1746 | do { |
| 1747 | if (!opipe->readers) { |
| 1748 | send_sig(SIGPIPE, current, 0); |
| 1749 | if (!ret) |
| 1750 | ret = -EPIPE; |
| 1751 | break; |
| 1752 | } |
| 1753 | |
| 1754 | if (!ipipe->nrbufs && !ipipe->writers) |
| 1755 | break; |
| 1756 | |
| 1757 | /* |
| 1758 | * Cannot make any progress, because either the input |
| 1759 | * pipe is empty or the output pipe is full. |
| 1760 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1761 | if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) { |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1762 | /* Already processed some buffers, break */ |
| 1763 | if (ret) |
| 1764 | break; |
| 1765 | |
| 1766 | if (flags & SPLICE_F_NONBLOCK) { |
| 1767 | ret = -EAGAIN; |
| 1768 | break; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
| 1772 | * We raced with another reader/writer and haven't |
| 1773 | * managed to process any buffers. A zero return |
| 1774 | * value means EOF, so retry instead. |
| 1775 | */ |
| 1776 | pipe_unlock(ipipe); |
| 1777 | pipe_unlock(opipe); |
| 1778 | goto retry; |
| 1779 | } |
| 1780 | |
| 1781 | ibuf = ipipe->bufs + ipipe->curbuf; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1782 | nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1783 | obuf = opipe->bufs + nbuf; |
| 1784 | |
| 1785 | if (len >= ibuf->len) { |
| 1786 | /* |
| 1787 | * Simply move the whole buffer from ipipe to opipe |
| 1788 | */ |
| 1789 | *obuf = *ibuf; |
| 1790 | ibuf->ops = NULL; |
| 1791 | opipe->nrbufs++; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1792 | ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1); |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1793 | ipipe->nrbufs--; |
| 1794 | input_wakeup = true; |
| 1795 | } else { |
| 1796 | /* |
| 1797 | * Get a reference to this pipe buffer, |
| 1798 | * so we can copy the contents over. |
| 1799 | */ |
| 1800 | ibuf->ops->get(ipipe, ibuf); |
| 1801 | *obuf = *ibuf; |
| 1802 | |
| 1803 | /* |
| 1804 | * Don't inherit the gift flag, we need to |
| 1805 | * prevent multiple steals of this page. |
| 1806 | */ |
| 1807 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
| 1808 | |
| 1809 | obuf->len = len; |
| 1810 | opipe->nrbufs++; |
| 1811 | ibuf->offset += obuf->len; |
| 1812 | ibuf->len -= obuf->len; |
| 1813 | } |
| 1814 | ret += obuf->len; |
| 1815 | len -= obuf->len; |
| 1816 | } while (len); |
| 1817 | |
| 1818 | pipe_unlock(ipipe); |
| 1819 | pipe_unlock(opipe); |
| 1820 | |
| 1821 | /* |
| 1822 | * If we put data in the output pipe, wakeup any potential readers. |
| 1823 | */ |
Namhyung Kim | 825cdcb | 2011-05-23 19:58:53 +0200 | [diff] [blame] | 1824 | if (ret > 0) |
| 1825 | wakeup_pipe_readers(opipe); |
| 1826 | |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1827 | if (input_wakeup) |
| 1828 | wakeup_pipe_writers(ipipe); |
| 1829 | |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1834 | * Link contents of ipipe to opipe. |
| 1835 | */ |
| 1836 | static int link_pipe(struct pipe_inode_info *ipipe, |
| 1837 | struct pipe_inode_info *opipe, |
| 1838 | size_t len, unsigned int flags) |
| 1839 | { |
| 1840 | struct pipe_buffer *ibuf, *obuf; |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1841 | int ret = 0, i = 0, nbuf; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1842 | |
| 1843 | /* |
| 1844 | * Potential ABBA deadlock, work around it by ordering lock |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1845 | * grabbing by pipe info address. Otherwise two different processes |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1846 | * could deadlock (one doing tee from A -> B, the other from B -> A). |
| 1847 | */ |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1848 | pipe_double_lock(ipipe, opipe); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1849 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1850 | do { |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1851 | if (!opipe->readers) { |
| 1852 | send_sig(SIGPIPE, current, 0); |
| 1853 | if (!ret) |
| 1854 | ret = -EPIPE; |
| 1855 | break; |
| 1856 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1857 | |
| 1858 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1859 | * If we have iterated all input buffers or ran out of |
| 1860 | * output room, break. |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1861 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1862 | if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1863 | break; |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1864 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1865 | ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1)); |
| 1866 | nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1867 | |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1868 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1869 | * Get a reference to this pipe buffer, |
| 1870 | * so we can copy the contents over. |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1871 | */ |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1872 | ibuf->ops->get(ipipe, ibuf); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1873 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1874 | obuf = opipe->bufs + nbuf; |
| 1875 | *obuf = *ibuf; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1876 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1877 | /* |
| 1878 | * Don't inherit the gift flag, we need to |
| 1879 | * prevent multiple steals of this page. |
| 1880 | */ |
| 1881 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
| 1882 | |
| 1883 | if (obuf->len > len) |
| 1884 | obuf->len = len; |
| 1885 | |
| 1886 | opipe->nrbufs++; |
| 1887 | ret += obuf->len; |
| 1888 | len -= obuf->len; |
| 1889 | i++; |
| 1890 | } while (len); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1891 | |
Jens Axboe | 02cf01a | 2008-02-20 10:34:51 +0100 | [diff] [blame] | 1892 | /* |
| 1893 | * return EAGAIN if we have the potential of some data in the |
| 1894 | * future, otherwise just return 0 |
| 1895 | */ |
| 1896 | if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) |
| 1897 | ret = -EAGAIN; |
| 1898 | |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 1899 | pipe_unlock(ipipe); |
| 1900 | pipe_unlock(opipe); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1901 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1902 | /* |
| 1903 | * If we put data in the output pipe, wakeup any potential readers. |
| 1904 | */ |
Namhyung Kim | 825cdcb | 2011-05-23 19:58:53 +0200 | [diff] [blame] | 1905 | if (ret > 0) |
| 1906 | wakeup_pipe_readers(opipe); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1907 | |
| 1908 | return ret; |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * This is a tee(1) implementation that works on pipes. It doesn't copy |
| 1913 | * any data, it simply references the 'in' pages on the 'out' pipe. |
| 1914 | * The 'flags' used are the SPLICE_F_* variants, currently the only |
| 1915 | * applicable one is SPLICE_F_NONBLOCK. |
| 1916 | */ |
| 1917 | static long do_tee(struct file *in, struct file *out, size_t len, |
| 1918 | unsigned int flags) |
| 1919 | { |
Linus Torvalds | 71993e6 | 2010-11-28 13:56:09 -0800 | [diff] [blame] | 1920 | struct pipe_inode_info *ipipe = get_pipe_info(in); |
| 1921 | struct pipe_inode_info *opipe = get_pipe_info(out); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1922 | int ret = -EINVAL; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1923 | |
| 1924 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1925 | * Duplicate the contents of ipipe to opipe without actually |
| 1926 | * copying the data. |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1927 | */ |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1928 | if (ipipe && opipe && ipipe != opipe) { |
| 1929 | /* |
| 1930 | * Keep going, unless we encounter an error. The ipipe/opipe |
| 1931 | * ordering doesn't really matter. |
| 1932 | */ |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1933 | ret = ipipe_prep(ipipe, flags); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1934 | if (!ret) { |
Miklos Szeredi | 7c77f0b | 2009-05-07 15:37:35 +0200 | [diff] [blame] | 1935 | ret = opipe_prep(opipe, flags); |
Jens Axboe | 02cf01a | 2008-02-20 10:34:51 +0100 | [diff] [blame] | 1936 | if (!ret) |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1937 | ret = link_pipe(ipipe, opipe, len, flags); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1938 | } |
| 1939 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1940 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1941 | return ret; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 1944 | SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1945 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1946 | struct fd in; |
| 1947 | int error; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1948 | |
| 1949 | if (unlikely(!len)) |
| 1950 | return 0; |
| 1951 | |
| 1952 | error = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1953 | in = fdget(fdin); |
| 1954 | if (in.file) { |
| 1955 | if (in.file->f_mode & FMODE_READ) { |
| 1956 | struct fd out = fdget(fdout); |
| 1957 | if (out.file) { |
| 1958 | if (out.file->f_mode & FMODE_WRITE) |
| 1959 | error = do_tee(in.file, out.file, |
| 1960 | len, flags); |
| 1961 | fdput(out); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1962 | } |
| 1963 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1964 | fdput(in); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | return error; |
| 1968 | } |