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 | c2058e0 | 2006-04-11 13:56:34 +0200 | [diff] [blame] | 15 | * Copyright (C) 2005-2006 Jens Axboe <axboe@suse.de> |
| 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> |
| 23 | #include <linux/pipe_fs_i.h> |
| 24 | #include <linux/mm_inline.h> |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 25 | #include <linux/swap.h> |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 26 | #include <linux/writeback.h> |
| 27 | #include <linux/buffer_head.h> |
Jeff Garzik | a0f0678 | 2006-03-30 23:06:13 -0500 | [diff] [blame] | 28 | #include <linux/module.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> |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Passed to the actors |
| 34 | */ |
| 35 | struct splice_desc { |
| 36 | unsigned int len, total_len; /* current and remaining length */ |
| 37 | unsigned int flags; /* splice flags */ |
| 38 | struct file *file; /* file to read/write */ |
| 39 | loff_t pos; /* file position */ |
| 40 | }; |
| 41 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 42 | struct partial_page { |
| 43 | unsigned int offset; |
| 44 | unsigned int len; |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * Passed to move_to_pipe |
| 49 | */ |
| 50 | struct splice_pipe_desc { |
| 51 | struct page **pages; /* page map */ |
| 52 | struct partial_page *partial; /* pages[] may not be contig */ |
| 53 | int nr_pages; /* number of pages in map */ |
| 54 | unsigned int flags; /* splice flags */ |
| 55 | struct pipe_buf_operations *ops;/* ops associated with output pipe */ |
| 56 | }; |
| 57 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Attempt to steal a page from a pipe buffer. This should perhaps go into |
| 60 | * a vm helper function, it's already simplified quite a bit by the |
| 61 | * addition of remove_mapping(). If success is returned, the caller may |
| 62 | * attempt to reuse this page for another destination. |
| 63 | */ |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 64 | static int page_cache_pipe_buf_steal(struct pipe_inode_info *info, |
| 65 | struct pipe_buffer *buf) |
| 66 | { |
| 67 | struct page *page = buf->page; |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 68 | struct address_space *mapping = page_mapping(page); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 69 | |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 70 | lock_page(page); |
| 71 | |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 72 | WARN_ON(!PageUptodate(page)); |
| 73 | |
Jens Axboe | ad8d6f0 | 2006-04-02 23:10:32 +0200 | [diff] [blame] | 74 | /* |
| 75 | * At least for ext2 with nobh option, we need to wait on writeback |
| 76 | * completing on this page, since we'll remove it from the pagecache. |
| 77 | * Otherwise truncate wont wait on the page, allowing the disk |
| 78 | * blocks to be reused by someone else before we actually wrote our |
| 79 | * data to them. fs corruption ensues. |
| 80 | */ |
| 81 | wait_on_page_writeback(page); |
| 82 | |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 83 | if (PagePrivate(page)) |
| 84 | try_to_release_page(page, mapping_gfp_mask(mapping)); |
| 85 | |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 86 | if (!remove_mapping(mapping, page)) { |
| 87 | unlock_page(page); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 88 | return 1; |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 89 | } |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 90 | |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 91 | buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 95 | static void page_cache_pipe_buf_release(struct pipe_inode_info *info, |
| 96 | struct pipe_buffer *buf) |
| 97 | { |
| 98 | page_cache_release(buf->page); |
| 99 | buf->page = NULL; |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 100 | buf->flags &= ~(PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void *page_cache_pipe_buf_map(struct file *file, |
| 104 | struct pipe_inode_info *info, |
| 105 | struct pipe_buffer *buf) |
| 106 | { |
| 107 | struct page *page = buf->page; |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 108 | int err; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 109 | |
| 110 | if (!PageUptodate(page)) { |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 111 | lock_page(page); |
| 112 | |
| 113 | /* |
| 114 | * Page got truncated/unhashed. This will cause a 0-byte |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 115 | * splice, if this is the first page. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 116 | */ |
| 117 | if (!page->mapping) { |
| 118 | err = -ENODATA; |
| 119 | goto error; |
| 120 | } |
| 121 | |
| 122 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 123 | * Uh oh, read-error from disk. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 124 | */ |
| 125 | if (!PageUptodate(page)) { |
| 126 | err = -EIO; |
| 127 | goto error; |
| 128 | } |
| 129 | |
| 130 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 131 | * Page is ok afterall, fall through to mapping. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 132 | */ |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 133 | unlock_page(page); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 136 | return kmap(page); |
| 137 | error: |
| 138 | unlock_page(page); |
| 139 | return ERR_PTR(err); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info, |
| 143 | struct pipe_buffer *buf) |
| 144 | { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 145 | kunmap(buf->page); |
| 146 | } |
| 147 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 148 | static void *user_page_pipe_buf_map(struct file *file, |
| 149 | struct pipe_inode_info *pipe, |
| 150 | struct pipe_buffer *buf) |
| 151 | { |
| 152 | return kmap(buf->page); |
| 153 | } |
| 154 | |
| 155 | static void user_page_pipe_buf_unmap(struct pipe_inode_info *pipe, |
| 156 | struct pipe_buffer *buf) |
| 157 | { |
| 158 | kunmap(buf->page); |
| 159 | } |
| 160 | |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 161 | static void page_cache_pipe_buf_get(struct pipe_inode_info *info, |
| 162 | struct pipe_buffer *buf) |
| 163 | { |
| 164 | page_cache_get(buf->page); |
| 165 | } |
| 166 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 167 | static struct pipe_buf_operations page_cache_pipe_buf_ops = { |
| 168 | .can_merge = 0, |
| 169 | .map = page_cache_pipe_buf_map, |
| 170 | .unmap = page_cache_pipe_buf_unmap, |
| 171 | .release = page_cache_pipe_buf_release, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 172 | .steal = page_cache_pipe_buf_steal, |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 173 | .get = page_cache_pipe_buf_get, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 174 | }; |
| 175 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 176 | static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, |
| 177 | struct pipe_buffer *buf) |
| 178 | { |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | static struct pipe_buf_operations user_page_pipe_buf_ops = { |
| 183 | .can_merge = 0, |
| 184 | .map = user_page_pipe_buf_map, |
| 185 | .unmap = user_page_pipe_buf_unmap, |
| 186 | .release = page_cache_pipe_buf_release, |
| 187 | .steal = user_page_pipe_buf_steal, |
| 188 | .get = page_cache_pipe_buf_get, |
| 189 | }; |
| 190 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 191 | /* |
| 192 | * Pipe output worker. This sets up our pipe format with the page cache |
| 193 | * pipe buffer operations. Otherwise very similar to the regular pipe_writev(). |
| 194 | */ |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 195 | static ssize_t move_to_pipe(struct pipe_inode_info *pipe, |
| 196 | struct splice_pipe_desc *spd) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 197 | { |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 198 | int ret, do_wakeup, page_nr; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 199 | |
| 200 | ret = 0; |
| 201 | do_wakeup = 0; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 202 | page_nr = 0; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 203 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 204 | if (pipe->inode) |
| 205 | mutex_lock(&pipe->inode->i_mutex); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 206 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 207 | for (;;) { |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 208 | if (!pipe->readers) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 209 | send_sig(SIGPIPE, current, 0); |
| 210 | if (!ret) |
| 211 | ret = -EPIPE; |
| 212 | break; |
| 213 | } |
| 214 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 215 | if (pipe->nrbufs < PIPE_BUFFERS) { |
| 216 | int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 217 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 218 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 219 | buf->page = spd->pages[page_nr]; |
| 220 | buf->offset = spd->partial[page_nr].offset; |
| 221 | buf->len = spd->partial[page_nr].len; |
| 222 | buf->ops = spd->ops; |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 223 | pipe->nrbufs++; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 224 | page_nr++; |
| 225 | ret += buf->len; |
| 226 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 227 | if (pipe->inode) |
| 228 | do_wakeup = 1; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 229 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 230 | if (!--spd->nr_pages) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 231 | break; |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 232 | if (pipe->nrbufs < PIPE_BUFFERS) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 233 | continue; |
| 234 | |
| 235 | break; |
| 236 | } |
| 237 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 238 | if (spd->flags & SPLICE_F_NONBLOCK) { |
Linus Torvalds | 29e3509 | 2006-04-02 12:46:35 -0700 | [diff] [blame] | 239 | if (!ret) |
| 240 | ret = -EAGAIN; |
| 241 | break; |
| 242 | } |
| 243 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 244 | if (signal_pending(current)) { |
| 245 | if (!ret) |
| 246 | ret = -ERESTARTSYS; |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | if (do_wakeup) { |
Jens Axboe | c0bd1f6 | 2006-04-10 09:03:32 +0200 | [diff] [blame] | 251 | smp_mb(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 252 | if (waitqueue_active(&pipe->wait)) |
| 253 | wake_up_interruptible_sync(&pipe->wait); |
| 254 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 255 | do_wakeup = 0; |
| 256 | } |
| 257 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 258 | pipe->waiting_writers++; |
| 259 | pipe_wait(pipe); |
| 260 | pipe->waiting_writers--; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 263 | if (pipe->inode) |
| 264 | mutex_unlock(&pipe->inode->i_mutex); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 265 | |
| 266 | if (do_wakeup) { |
Jens Axboe | c0bd1f6 | 2006-04-10 09:03:32 +0200 | [diff] [blame] | 267 | smp_mb(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 268 | if (waitqueue_active(&pipe->wait)) |
| 269 | wake_up_interruptible(&pipe->wait); |
| 270 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 273 | while (page_nr < spd->nr_pages) |
| 274 | page_cache_release(spd->pages[page_nr++]); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 279 | static int |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 280 | __generic_file_splice_read(struct file *in, loff_t *ppos, |
| 281 | struct pipe_inode_info *pipe, size_t len, |
| 282 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 283 | { |
| 284 | struct address_space *mapping = in->f_mapping; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 285 | unsigned int loff, nr_pages; |
Jens Axboe | 16c523d | 2006-04-10 09:03:58 +0200 | [diff] [blame] | 286 | struct page *pages[PIPE_BUFFERS]; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 287 | struct partial_page partial[PIPE_BUFFERS]; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 288 | struct page *page; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 289 | pgoff_t index, end_index; |
| 290 | loff_t isize; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 291 | size_t total_len; |
| 292 | int error; |
| 293 | struct splice_pipe_desc spd = { |
| 294 | .pages = pages, |
| 295 | .partial = partial, |
| 296 | .flags = flags, |
| 297 | .ops = &page_cache_pipe_buf_ops, |
| 298 | }; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 299 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 300 | index = *ppos >> PAGE_CACHE_SHIFT; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 301 | loff = *ppos & ~PAGE_CACHE_MASK; |
| 302 | nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 303 | |
| 304 | if (nr_pages > PIPE_BUFFERS) |
| 305 | nr_pages = PIPE_BUFFERS; |
| 306 | |
| 307 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 308 | * Initiate read-ahead on this page range. however, don't call into |
Jens Axboe | 0b749ce | 2006-04-10 09:05:04 +0200 | [diff] [blame] | 309 | * read-ahead if this is a non-zero offset (we are likely doing small |
| 310 | * chunk splice and the page is already there) for a single page. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 311 | */ |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 312 | if (!loff || spd.nr_pages > 1) |
| 313 | do_page_cache_readahead(mapping, in, index, spd.nr_pages); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 314 | |
| 315 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 316 | * Now fill in the holes: |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 317 | */ |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 318 | error = 0; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 319 | total_len = 0; |
| 320 | for (spd.nr_pages = 0; spd.nr_pages < nr_pages; spd.nr_pages++, index++) { |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 321 | unsigned int this_len; |
| 322 | |
| 323 | if (!len) |
| 324 | break; |
| 325 | |
| 326 | /* |
| 327 | * this_len is the max we'll use from this page |
| 328 | */ |
Andrew Morton | ba5f5d9 | 2006-04-25 15:33:34 +0200 | [diff] [blame] | 329 | this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 330 | find_page: |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 331 | /* |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 332 | * lookup the page for this index |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 333 | */ |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 334 | page = find_get_page(mapping, index); |
| 335 | if (!page) { |
| 336 | /* |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 337 | * page didn't exist, allocate one |
| 338 | */ |
| 339 | page = page_cache_alloc_cold(mapping); |
| 340 | if (!page) |
| 341 | break; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 342 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 343 | error = add_to_page_cache_lru(page, mapping, index, |
| 344 | mapping_gfp_mask(mapping)); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 345 | if (unlikely(error)) { |
| 346 | page_cache_release(page); |
| 347 | break; |
| 348 | } |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 349 | |
| 350 | goto readpage; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 351 | } |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * If the page isn't uptodate, we may need to start io on it |
| 355 | */ |
| 356 | if (!PageUptodate(page)) { |
Jens Axboe | c4f895c | 2006-04-19 15:56:12 +0200 | [diff] [blame] | 357 | /* |
| 358 | * If in nonblock mode then dont block on waiting |
| 359 | * for an in-flight io page |
| 360 | */ |
| 361 | if (flags & SPLICE_F_NONBLOCK) |
| 362 | break; |
| 363 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 364 | lock_page(page); |
| 365 | |
| 366 | /* |
| 367 | * page was truncated, stop here. if this isn't the |
| 368 | * first page, we'll just complete what we already |
| 369 | * added |
| 370 | */ |
| 371 | if (!page->mapping) { |
| 372 | unlock_page(page); |
| 373 | page_cache_release(page); |
| 374 | break; |
| 375 | } |
| 376 | /* |
| 377 | * page was already under io and is now done, great |
| 378 | */ |
| 379 | if (PageUptodate(page)) { |
| 380 | unlock_page(page); |
| 381 | goto fill_it; |
| 382 | } |
| 383 | |
| 384 | readpage: |
| 385 | /* |
| 386 | * need to read in the page |
| 387 | */ |
| 388 | error = mapping->a_ops->readpage(in, page); |
| 389 | |
| 390 | if (unlikely(error)) { |
| 391 | page_cache_release(page); |
| 392 | if (error == AOP_TRUNCATED_PAGE) |
| 393 | goto find_page; |
| 394 | break; |
| 395 | } |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * i_size must be checked after ->readpage(). |
| 399 | */ |
| 400 | isize = i_size_read(mapping->host); |
| 401 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 402 | if (unlikely(!isize || index > end_index)) { |
| 403 | page_cache_release(page); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * if this is the last page, see if we need to shrink |
| 409 | * the length and stop |
| 410 | */ |
| 411 | if (end_index == index) { |
| 412 | loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 413 | if (total_len + loff > isize) { |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 414 | page_cache_release(page); |
| 415 | break; |
| 416 | } |
| 417 | /* |
| 418 | * force quit after adding this page |
| 419 | */ |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 420 | nr_pages = spd.nr_pages; |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 421 | this_len = min(this_len, loff); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 422 | loff = 0; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 423 | } |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 424 | } |
| 425 | fill_it: |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 426 | pages[spd.nr_pages] = page; |
| 427 | partial[spd.nr_pages].offset = loff; |
| 428 | partial[spd.nr_pages].len = this_len; |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 429 | len -= this_len; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 430 | total_len += this_len; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 431 | loff = 0; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 434 | if (spd.nr_pages) |
| 435 | return move_to_pipe(pipe, &spd); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 436 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 437 | return error; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 440 | /** |
| 441 | * generic_file_splice_read - splice data from file to a pipe |
| 442 | * @in: file to splice from |
| 443 | * @pipe: pipe to splice to |
| 444 | * @len: number of bytes to splice |
| 445 | * @flags: splice modifier flags |
| 446 | * |
| 447 | * Will read pages from given file and fill them into a pipe. |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 448 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 449 | ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, |
| 450 | struct pipe_inode_info *pipe, size_t len, |
| 451 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 452 | { |
| 453 | ssize_t spliced; |
| 454 | int ret; |
| 455 | |
| 456 | ret = 0; |
| 457 | spliced = 0; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 458 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 459 | while (len) { |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 460 | ret = __generic_file_splice_read(in, ppos, pipe, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 461 | |
Jens Axboe | c4f895c | 2006-04-19 15:56:12 +0200 | [diff] [blame] | 462 | if (ret < 0) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 463 | break; |
Jens Axboe | c4f895c | 2006-04-19 15:56:12 +0200 | [diff] [blame] | 464 | else if (!ret) { |
| 465 | if (spliced) |
| 466 | break; |
| 467 | if (flags & SPLICE_F_NONBLOCK) { |
| 468 | ret = -EAGAIN; |
| 469 | break; |
| 470 | } |
| 471 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 472 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 473 | *ppos += ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 474 | len -= ret; |
| 475 | spliced += ret; |
| 476 | } |
| 477 | |
| 478 | if (spliced) |
| 479 | return spliced; |
| 480 | |
| 481 | return ret; |
| 482 | } |
| 483 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 484 | EXPORT_SYMBOL(generic_file_splice_read); |
| 485 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 486 | /* |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 487 | * 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] | 488 | * using sendpage(). Return the number of bytes sent. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 489 | */ |
| 490 | static int pipe_to_sendpage(struct pipe_inode_info *info, |
| 491 | struct pipe_buffer *buf, struct splice_desc *sd) |
| 492 | { |
| 493 | struct file *file = sd->file; |
| 494 | loff_t pos = sd->pos; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 495 | ssize_t ret; |
| 496 | void *ptr; |
Jens Axboe | b2b39fa | 2006-04-02 23:05:41 +0200 | [diff] [blame] | 497 | int more; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 498 | |
| 499 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 500 | * Sub-optimal, but we are limited by the pipe ->map. We don't |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 501 | * need a kmap'ed buffer here, we just want to make sure we |
| 502 | * have the page pinned if the pipe page originates from the |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 503 | * page cache. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 504 | */ |
| 505 | ptr = buf->ops->map(file, info, buf); |
| 506 | if (IS_ERR(ptr)) |
| 507 | return PTR_ERR(ptr); |
| 508 | |
Jens Axboe | b2b39fa | 2006-04-02 23:05:41 +0200 | [diff] [blame] | 509 | more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 510 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 511 | ret = file->f_op->sendpage(file, buf->page, buf->offset, sd->len, |
| 512 | &pos, more); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 513 | |
| 514 | buf->ops->unmap(info, buf); |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 515 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
| 519 | * This is a little more tricky than the file -> pipe splicing. There are |
| 520 | * basically three cases: |
| 521 | * |
| 522 | * - Destination page already exists in the address space and there |
| 523 | * are users of it. For that case we have no other option that |
| 524 | * copying the data. Tough luck. |
| 525 | * - Destination page already exists in the address space, but there |
| 526 | * are no users of it. Make sure it's uptodate, then drop it. Fall |
| 527 | * through to last case. |
| 528 | * - Destination page does not exist, we can add the pipe page to |
| 529 | * the page cache and avoid the copy. |
| 530 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 531 | * If asked to move pages to the output file (SPLICE_F_MOVE is set in |
| 532 | * sd->flags), we attempt to migrate pages from the pipe to the output |
| 533 | * file address space page cache. This is possible if no one else has |
| 534 | * the pipe page referenced outside of the pipe and page cache. If |
| 535 | * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create |
| 536 | * a new page in the output file page cache and fill/dirty that. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 537 | */ |
| 538 | static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf, |
| 539 | struct splice_desc *sd) |
| 540 | { |
| 541 | struct file *file = sd->file; |
| 542 | struct address_space *mapping = file->f_mapping; |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 543 | gfp_t gfp_mask = mapping_gfp_mask(mapping); |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 544 | unsigned int offset, this_len; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 545 | struct page *page; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 546 | pgoff_t index; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 547 | char *src; |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 548 | int ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 549 | |
| 550 | /* |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 551 | * make sure the data in this buffer is uptodate |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 552 | */ |
| 553 | src = buf->ops->map(file, info, buf); |
| 554 | if (IS_ERR(src)) |
| 555 | return PTR_ERR(src); |
| 556 | |
| 557 | index = sd->pos >> PAGE_CACHE_SHIFT; |
| 558 | offset = sd->pos & ~PAGE_CACHE_MASK; |
| 559 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 560 | this_len = sd->len; |
| 561 | if (this_len + offset > PAGE_CACHE_SIZE) |
| 562 | this_len = PAGE_CACHE_SIZE - offset; |
| 563 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 564 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 565 | * Reuse buf page, if SPLICE_F_MOVE is set. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 566 | */ |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 567 | if (sd->flags & SPLICE_F_MOVE) { |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 568 | /* |
| 569 | * If steal succeeds, buf->page is now pruned from the vm |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 570 | * side (LRU and page cache) and we can reuse it. The page |
| 571 | * will also be looked on successful return. |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 572 | */ |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 573 | if (buf->ops->steal(info, buf)) |
| 574 | goto find_page; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 575 | |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 576 | page = buf->page; |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 577 | if (add_to_page_cache(page, mapping, index, gfp_mask)) |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 578 | goto find_page; |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 579 | |
| 580 | if (!(buf->flags & PIPE_BUF_FLAG_LRU)) |
| 581 | lru_cache_add(page); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 582 | } else { |
| 583 | find_page: |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 584 | page = find_lock_page(mapping, index); |
| 585 | if (!page) { |
| 586 | ret = -ENOMEM; |
| 587 | page = page_cache_alloc_cold(mapping); |
| 588 | if (unlikely(!page)) |
| 589 | goto out_nomem; |
| 590 | |
| 591 | /* |
| 592 | * This will also lock the page |
| 593 | */ |
| 594 | ret = add_to_page_cache_lru(page, mapping, index, |
| 595 | gfp_mask); |
| 596 | if (unlikely(ret)) |
| 597 | goto out; |
| 598 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 599 | |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 600 | /* |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 601 | * We get here with the page locked. If the page is also |
| 602 | * uptodate, we don't need to do more. If it isn't, we |
| 603 | * may need to bring it in if we are not going to overwrite |
| 604 | * the full page. |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 605 | */ |
| 606 | if (!PageUptodate(page)) { |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 607 | if (this_len < PAGE_CACHE_SIZE) { |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 608 | ret = mapping->a_ops->readpage(file, page); |
| 609 | if (unlikely(ret)) |
| 610 | goto out; |
| 611 | |
| 612 | lock_page(page); |
| 613 | |
| 614 | if (!PageUptodate(page)) { |
| 615 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 616 | * Page got invalidated, repeat. |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 617 | */ |
| 618 | if (!page->mapping) { |
| 619 | unlock_page(page); |
| 620 | page_cache_release(page); |
| 621 | goto find_page; |
| 622 | } |
| 623 | ret = -EIO; |
| 624 | goto out; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 625 | } |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 626 | } else |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 627 | SetPageUptodate(page); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 631 | ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len); |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 632 | if (ret == AOP_TRUNCATED_PAGE) { |
| 633 | page_cache_release(page); |
| 634 | goto find_page; |
| 635 | } else if (ret) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 636 | goto out; |
| 637 | |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 638 | if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) { |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 639 | char *dst = kmap_atomic(page, KM_USER0); |
| 640 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 641 | memcpy(dst + offset, src + buf->offset, this_len); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 642 | flush_dcache_page(page); |
| 643 | kunmap_atomic(dst, KM_USER0); |
| 644 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 645 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 646 | ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len); |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 647 | if (ret == AOP_TRUNCATED_PAGE) { |
| 648 | page_cache_release(page); |
| 649 | goto find_page; |
| 650 | } else if (ret) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 651 | goto out; |
| 652 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 653 | /* |
| 654 | * Return the number of bytes written. |
| 655 | */ |
| 656 | ret = this_len; |
Jens Axboe | c7f21e4 | 2006-04-10 09:01:01 +0200 | [diff] [blame] | 657 | mark_page_accessed(page); |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 658 | balance_dirty_pages_ratelimited(mapping); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 659 | out: |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 660 | if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 661 | page_cache_release(page); |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 662 | |
| 663 | unlock_page(page); |
Dave Jones | 9aefe43 | 2006-04-10 09:02:40 +0200 | [diff] [blame] | 664 | out_nomem: |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 665 | buf->ops->unmap(info, buf); |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *, |
| 670 | struct splice_desc *); |
| 671 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 672 | /* |
| 673 | * Pipe input worker. Most of this logic works like a regular pipe, the |
| 674 | * key here is the 'actor' worker passed in that actually moves the data |
| 675 | * to the wanted destination. See pipe_to_file/pipe_to_sendpage above. |
| 676 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 677 | static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 678 | loff_t *ppos, size_t len, unsigned int flags, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 679 | splice_actor *actor) |
| 680 | { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 681 | int ret, do_wakeup, err; |
| 682 | struct splice_desc sd; |
| 683 | |
| 684 | ret = 0; |
| 685 | do_wakeup = 0; |
| 686 | |
| 687 | sd.total_len = len; |
| 688 | sd.flags = flags; |
| 689 | sd.file = out; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 690 | sd.pos = *ppos; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 691 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 692 | if (pipe->inode) |
| 693 | mutex_lock(&pipe->inode->i_mutex); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 694 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 695 | for (;;) { |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 696 | if (pipe->nrbufs) { |
| 697 | struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 698 | struct pipe_buf_operations *ops = buf->ops; |
| 699 | |
| 700 | sd.len = buf->len; |
| 701 | if (sd.len > sd.total_len) |
| 702 | sd.len = sd.total_len; |
| 703 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 704 | err = actor(pipe, buf, &sd); |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 705 | if (err <= 0) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 706 | if (!ret && err != -ENODATA) |
| 707 | ret = err; |
| 708 | |
| 709 | break; |
| 710 | } |
| 711 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 712 | ret += err; |
| 713 | buf->offset += err; |
| 714 | buf->len -= err; |
| 715 | |
| 716 | sd.len -= err; |
| 717 | sd.pos += err; |
| 718 | sd.total_len -= err; |
| 719 | if (sd.len) |
| 720 | continue; |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 721 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 722 | if (!buf->len) { |
| 723 | buf->ops = NULL; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 724 | ops->release(pipe, buf); |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 725 | pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); |
| 726 | pipe->nrbufs--; |
| 727 | if (pipe->inode) |
| 728 | do_wakeup = 1; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 731 | if (!sd.total_len) |
| 732 | break; |
| 733 | } |
| 734 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 735 | if (pipe->nrbufs) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 736 | continue; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 737 | if (!pipe->writers) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 738 | break; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 739 | if (!pipe->waiting_writers) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 740 | if (ret) |
| 741 | break; |
| 742 | } |
| 743 | |
Linus Torvalds | 29e3509 | 2006-04-02 12:46:35 -0700 | [diff] [blame] | 744 | if (flags & SPLICE_F_NONBLOCK) { |
| 745 | if (!ret) |
| 746 | ret = -EAGAIN; |
| 747 | break; |
| 748 | } |
| 749 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 750 | if (signal_pending(current)) { |
| 751 | if (!ret) |
| 752 | ret = -ERESTARTSYS; |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | if (do_wakeup) { |
Jens Axboe | c0bd1f6 | 2006-04-10 09:03:32 +0200 | [diff] [blame] | 757 | smp_mb(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 758 | if (waitqueue_active(&pipe->wait)) |
| 759 | wake_up_interruptible_sync(&pipe->wait); |
| 760 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 761 | do_wakeup = 0; |
| 762 | } |
| 763 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 764 | pipe_wait(pipe); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 767 | if (pipe->inode) |
| 768 | mutex_unlock(&pipe->inode->i_mutex); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 769 | |
| 770 | if (do_wakeup) { |
Jens Axboe | c0bd1f6 | 2006-04-10 09:03:32 +0200 | [diff] [blame] | 771 | smp_mb(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 772 | if (waitqueue_active(&pipe->wait)) |
| 773 | wake_up_interruptible(&pipe->wait); |
| 774 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 777 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 780 | /** |
| 781 | * generic_file_splice_write - splice data from a pipe to a file |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 782 | * @pipe: pipe info |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 783 | * @out: file to write to |
| 784 | * @len: number of bytes to splice |
| 785 | * @flags: splice modifier flags |
| 786 | * |
| 787 | * Will either move or copy pages (determined by @flags options) from |
| 788 | * the given pipe inode to the given file. |
| 789 | * |
| 790 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 791 | ssize_t |
| 792 | generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 793 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 794 | { |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 795 | struct address_space *mapping = out->f_mapping; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 796 | ssize_t ret; |
| 797 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 798 | ret = move_from_pipe(pipe, out, ppos, len, flags, pipe_to_file); |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 799 | if (ret > 0) { |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 800 | struct inode *inode = mapping->host; |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 801 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 802 | *ppos += ret; |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 803 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 804 | /* |
| 805 | * If file or inode is SYNC and we actually wrote some data, |
| 806 | * sync it. |
| 807 | */ |
| 808 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 809 | int err; |
| 810 | |
| 811 | mutex_lock(&inode->i_mutex); |
| 812 | err = generic_osync_inode(inode, mapping, |
| 813 | OSYNC_METADATA|OSYNC_DATA); |
| 814 | mutex_unlock(&inode->i_mutex); |
| 815 | |
| 816 | if (err) |
| 817 | ret = err; |
| 818 | } |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 824 | EXPORT_SYMBOL(generic_file_splice_write); |
| 825 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 826 | /** |
| 827 | * generic_splice_sendpage - splice data from a pipe to a socket |
| 828 | * @inode: pipe inode |
| 829 | * @out: socket to write to |
| 830 | * @len: number of bytes to splice |
| 831 | * @flags: splice modifier flags |
| 832 | * |
| 833 | * Will send @len bytes from the pipe to a network socket. No data copying |
| 834 | * is involved. |
| 835 | * |
| 836 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 837 | 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] | 838 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 839 | { |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 840 | return move_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 843 | EXPORT_SYMBOL(generic_splice_sendpage); |
Jeff Garzik | a0f0678 | 2006-03-30 23:06:13 -0500 | [diff] [blame] | 844 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 845 | /* |
| 846 | * Attempt to initiate a splice from pipe to file. |
| 847 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 848 | 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] | 849 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 850 | { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 851 | int ret; |
| 852 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 853 | if (unlikely(!out->f_op || !out->f_op->splice_write)) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 854 | return -EINVAL; |
| 855 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 856 | if (unlikely(!(out->f_mode & FMODE_WRITE))) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 857 | return -EBADF; |
| 858 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 859 | ret = rw_verify_area(WRITE, out, ppos, len); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 860 | if (unlikely(ret < 0)) |
| 861 | return ret; |
| 862 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 863 | return out->f_op->splice_write(pipe, out, ppos, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 866 | /* |
| 867 | * Attempt to initiate a splice from a file to a pipe. |
| 868 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 869 | static long do_splice_to(struct file *in, loff_t *ppos, |
| 870 | struct pipe_inode_info *pipe, size_t len, |
| 871 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 872 | { |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 873 | loff_t isize, left; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 874 | int ret; |
| 875 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 876 | if (unlikely(!in->f_op || !in->f_op->splice_read)) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 877 | return -EINVAL; |
| 878 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 879 | if (unlikely(!(in->f_mode & FMODE_READ))) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 880 | return -EBADF; |
| 881 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 882 | ret = rw_verify_area(READ, in, ppos, len); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 883 | if (unlikely(ret < 0)) |
| 884 | return ret; |
| 885 | |
| 886 | isize = i_size_read(in->f_mapping->host); |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 887 | if (unlikely(*ppos >= isize)) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 888 | return 0; |
| 889 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 890 | left = isize - *ppos; |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 891 | if (unlikely(left < len)) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 892 | len = left; |
| 893 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 894 | return in->f_op->splice_read(in, ppos, pipe, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 897 | long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, |
| 898 | size_t len, unsigned int flags) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 899 | { |
| 900 | struct pipe_inode_info *pipe; |
| 901 | long ret, bytes; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 902 | loff_t out_off; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 903 | umode_t i_mode; |
| 904 | int i; |
| 905 | |
| 906 | /* |
| 907 | * We require the input being a regular file, as we don't want to |
| 908 | * randomly drop data for eg socket -> socket splicing. Use the |
| 909 | * piped splicing for that! |
| 910 | */ |
| 911 | i_mode = in->f_dentry->d_inode->i_mode; |
| 912 | if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) |
| 913 | return -EINVAL; |
| 914 | |
| 915 | /* |
| 916 | * neither in nor out is a pipe, setup an internal pipe attached to |
| 917 | * 'out' and transfer the wanted data from 'in' to 'out' through that |
| 918 | */ |
| 919 | pipe = current->splice_pipe; |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 920 | if (unlikely(!pipe)) { |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 921 | pipe = alloc_pipe_info(NULL); |
| 922 | if (!pipe) |
| 923 | return -ENOMEM; |
| 924 | |
| 925 | /* |
| 926 | * We don't have an immediate reader, but we'll read the stuff |
| 927 | * out of the pipe right after the move_to_pipe(). So set |
| 928 | * PIPE_READERS appropriately. |
| 929 | */ |
| 930 | pipe->readers = 1; |
| 931 | |
| 932 | current->splice_pipe = pipe; |
| 933 | } |
| 934 | |
| 935 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 936 | * Do the splice. |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 937 | */ |
| 938 | ret = 0; |
| 939 | bytes = 0; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 940 | out_off = 0; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 941 | |
| 942 | while (len) { |
| 943 | size_t read_len, max_read_len; |
| 944 | |
| 945 | /* |
| 946 | * Do at most PIPE_BUFFERS pages worth of transfer: |
| 947 | */ |
| 948 | max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE)); |
| 949 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 950 | ret = do_splice_to(in, ppos, pipe, max_read_len, flags); |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 951 | if (unlikely(ret < 0)) |
| 952 | goto out_release; |
| 953 | |
| 954 | read_len = ret; |
| 955 | |
| 956 | /* |
| 957 | * NOTE: nonblocking mode only applies to the input. We |
| 958 | * must not do the output in nonblocking mode as then we |
| 959 | * could get stuck data in the internal pipe: |
| 960 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 961 | ret = do_splice_from(pipe, out, &out_off, read_len, |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 962 | flags & ~SPLICE_F_NONBLOCK); |
| 963 | if (unlikely(ret < 0)) |
| 964 | goto out_release; |
| 965 | |
| 966 | bytes += ret; |
| 967 | len -= ret; |
| 968 | |
| 969 | /* |
| 970 | * In nonblocking mode, if we got back a short read then |
| 971 | * that was due to either an IO error or due to the |
| 972 | * pagecache entry not being there. In the IO error case |
| 973 | * the _next_ splice attempt will produce a clean IO error |
| 974 | * return value (not a short read), so in both cases it's |
| 975 | * correct to break out of the loop here: |
| 976 | */ |
| 977 | if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len)) |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | pipe->nrbufs = pipe->curbuf = 0; |
| 982 | |
| 983 | return bytes; |
| 984 | |
| 985 | out_release: |
| 986 | /* |
| 987 | * If we did an incomplete transfer we must release |
| 988 | * the pipe buffers in question: |
| 989 | */ |
| 990 | for (i = 0; i < PIPE_BUFFERS; i++) { |
| 991 | struct pipe_buffer *buf = pipe->bufs + i; |
| 992 | |
| 993 | if (buf->ops) { |
| 994 | buf->ops->release(pipe, buf); |
| 995 | buf->ops = NULL; |
| 996 | } |
| 997 | } |
| 998 | pipe->nrbufs = pipe->curbuf = 0; |
| 999 | |
| 1000 | /* |
| 1001 | * If we transferred some data, return the number of bytes: |
| 1002 | */ |
| 1003 | if (bytes > 0) |
| 1004 | return bytes; |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | EXPORT_SYMBOL(do_splice_direct); |
| 1010 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1011 | /* |
| 1012 | * Determine where to splice to/from. |
| 1013 | */ |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1014 | static long do_splice(struct file *in, loff_t __user *off_in, |
| 1015 | struct file *out, loff_t __user *off_out, |
| 1016 | size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1017 | { |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1018 | struct pipe_inode_info *pipe; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1019 | loff_t offset, *off; |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1020 | long ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1021 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1022 | pipe = in->f_dentry->d_inode->i_pipe; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1023 | if (pipe) { |
| 1024 | if (off_in) |
| 1025 | return -ESPIPE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1026 | if (off_out) { |
| 1027 | if (out->f_op->llseek == no_llseek) |
| 1028 | return -EINVAL; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1029 | if (copy_from_user(&offset, off_out, sizeof(loff_t))) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1030 | return -EFAULT; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1031 | off = &offset; |
| 1032 | } else |
| 1033 | off = &out->f_pos; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1034 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1035 | ret = do_splice_from(pipe, out, off, len, flags); |
| 1036 | |
| 1037 | if (off_out && copy_to_user(off_out, off, sizeof(loff_t))) |
| 1038 | ret = -EFAULT; |
| 1039 | |
| 1040 | return ret; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1041 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1042 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1043 | pipe = out->f_dentry->d_inode->i_pipe; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1044 | if (pipe) { |
| 1045 | if (off_out) |
| 1046 | return -ESPIPE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1047 | if (off_in) { |
| 1048 | if (in->f_op->llseek == no_llseek) |
| 1049 | return -EINVAL; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1050 | if (copy_from_user(&offset, off_in, sizeof(loff_t))) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1051 | return -EFAULT; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1052 | off = &offset; |
| 1053 | } else |
| 1054 | off = &in->f_pos; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1055 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1056 | ret = do_splice_to(in, off, pipe, len, flags); |
| 1057 | |
| 1058 | if (off_in && copy_to_user(off_in, off, sizeof(loff_t))) |
| 1059 | ret = -EFAULT; |
| 1060 | |
| 1061 | return ret; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1062 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1063 | |
| 1064 | return -EINVAL; |
| 1065 | } |
| 1066 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame^] | 1067 | /* |
| 1068 | * Map an iov into an array of pages and offset/length tupples. With the |
| 1069 | * partial_page structure, we can map several non-contiguous ranges into |
| 1070 | * our ones pages[] map instead of splitting that operation into pieces. |
| 1071 | * Could easily be exported as a generic helper for other users, in which |
| 1072 | * case one would probably want to add a 'max_nr_pages' parameter as well. |
| 1073 | */ |
| 1074 | static int get_iovec_page_array(const struct iovec __user *iov, |
| 1075 | unsigned int nr_vecs, struct page **pages, |
| 1076 | struct partial_page *partial) |
| 1077 | { |
| 1078 | int buffers = 0, error = 0; |
| 1079 | |
| 1080 | /* |
| 1081 | * It's ok to take the mmap_sem for reading, even |
| 1082 | * across a "get_user()". |
| 1083 | */ |
| 1084 | down_read(¤t->mm->mmap_sem); |
| 1085 | |
| 1086 | while (nr_vecs) { |
| 1087 | unsigned long off, npages; |
| 1088 | void __user *base; |
| 1089 | size_t len; |
| 1090 | int i; |
| 1091 | |
| 1092 | /* |
| 1093 | * Get user address base and length for this iovec. |
| 1094 | */ |
| 1095 | error = get_user(base, &iov->iov_base); |
| 1096 | if (unlikely(error)) |
| 1097 | break; |
| 1098 | error = get_user(len, &iov->iov_len); |
| 1099 | if (unlikely(error)) |
| 1100 | break; |
| 1101 | |
| 1102 | /* |
| 1103 | * Sanity check this iovec. 0 read succeeds. |
| 1104 | */ |
| 1105 | if (unlikely(!len)) |
| 1106 | break; |
| 1107 | error = -EFAULT; |
| 1108 | if (unlikely(!base)) |
| 1109 | break; |
| 1110 | |
| 1111 | /* |
| 1112 | * Get this base offset and number of pages, then map |
| 1113 | * in the user pages. |
| 1114 | */ |
| 1115 | off = (unsigned long) base & ~PAGE_MASK; |
| 1116 | npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1117 | if (npages > PIPE_BUFFERS - buffers) |
| 1118 | npages = PIPE_BUFFERS - buffers; |
| 1119 | |
| 1120 | error = get_user_pages(current, current->mm, |
| 1121 | (unsigned long) base, npages, 0, 0, |
| 1122 | &pages[buffers], NULL); |
| 1123 | |
| 1124 | if (unlikely(error <= 0)) |
| 1125 | break; |
| 1126 | |
| 1127 | /* |
| 1128 | * Fill this contiguous range into the partial page map. |
| 1129 | */ |
| 1130 | for (i = 0; i < error; i++) { |
| 1131 | const int plen = min_t(size_t, len, PAGE_SIZE) - off; |
| 1132 | |
| 1133 | partial[buffers].offset = off; |
| 1134 | partial[buffers].len = plen; |
| 1135 | |
| 1136 | off = 0; |
| 1137 | len -= plen; |
| 1138 | buffers++; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * We didn't complete this iov, stop here since it probably |
| 1143 | * means we have to move some of this into a pipe to |
| 1144 | * be able to continue. |
| 1145 | */ |
| 1146 | if (len) |
| 1147 | break; |
| 1148 | |
| 1149 | /* |
| 1150 | * Don't continue if we mapped fewer pages than we asked for, |
| 1151 | * or if we mapped the max number of pages that we have |
| 1152 | * room for. |
| 1153 | */ |
| 1154 | if (error < npages || buffers == PIPE_BUFFERS) |
| 1155 | break; |
| 1156 | |
| 1157 | nr_vecs--; |
| 1158 | iov++; |
| 1159 | } |
| 1160 | |
| 1161 | up_read(¤t->mm->mmap_sem); |
| 1162 | |
| 1163 | if (buffers) |
| 1164 | return buffers; |
| 1165 | |
| 1166 | return error; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * vmsplice splices a user address range into a pipe. It can be thought of |
| 1171 | * as splice-from-memory, where the regular splice is splice-from-file (or |
| 1172 | * to file). In both cases the output is a pipe, naturally. |
| 1173 | * |
| 1174 | * Note that vmsplice only supports splicing _from_ user memory to a pipe, |
| 1175 | * not the other way around. Splicing from user memory is a simple operation |
| 1176 | * that can be supported without any funky alignment restrictions or nasty |
| 1177 | * vm tricks. We simply map in the user memory and fill them into a pipe. |
| 1178 | * The reverse isn't quite as easy, though. There are two possible solutions |
| 1179 | * for that: |
| 1180 | * |
| 1181 | * - memcpy() the data internally, at which point we might as well just |
| 1182 | * do a regular read() on the buffer anyway. |
| 1183 | * - Lots of nasty vm tricks, that are neither fast nor flexible (it |
| 1184 | * has restriction limitations on both ends of the pipe). |
| 1185 | * |
| 1186 | * Alas, it isn't here. |
| 1187 | * |
| 1188 | */ |
| 1189 | static long do_vmsplice(struct file *file, const struct iovec __user *iov, |
| 1190 | unsigned long nr_segs, unsigned int flags) |
| 1191 | { |
| 1192 | struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe; |
| 1193 | struct page *pages[PIPE_BUFFERS]; |
| 1194 | struct partial_page partial[PIPE_BUFFERS]; |
| 1195 | struct splice_pipe_desc spd = { |
| 1196 | .pages = pages, |
| 1197 | .partial = partial, |
| 1198 | .flags = flags, |
| 1199 | .ops = &user_page_pipe_buf_ops, |
| 1200 | }; |
| 1201 | |
| 1202 | if (unlikely(!pipe)) |
| 1203 | return -EBADF; |
| 1204 | if (unlikely(nr_segs > UIO_MAXIOV)) |
| 1205 | return -EINVAL; |
| 1206 | else if (unlikely(!nr_segs)) |
| 1207 | return 0; |
| 1208 | |
| 1209 | spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial); |
| 1210 | if (spd.nr_pages <= 0) |
| 1211 | return spd.nr_pages; |
| 1212 | |
| 1213 | return move_to_pipe(pipe, &spd); |
| 1214 | } |
| 1215 | |
| 1216 | asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov, |
| 1217 | unsigned long nr_segs, unsigned int flags) |
| 1218 | { |
| 1219 | struct file *file; |
| 1220 | long error; |
| 1221 | int fput; |
| 1222 | |
| 1223 | error = -EBADF; |
| 1224 | file = fget_light(fd, &fput); |
| 1225 | if (file) { |
| 1226 | if (file->f_mode & FMODE_WRITE) |
| 1227 | error = do_vmsplice(file, iov, nr_segs, flags); |
| 1228 | |
| 1229 | fput_light(file, fput); |
| 1230 | } |
| 1231 | |
| 1232 | return error; |
| 1233 | } |
| 1234 | |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1235 | asmlinkage long sys_splice(int fd_in, loff_t __user *off_in, |
| 1236 | int fd_out, loff_t __user *off_out, |
| 1237 | size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1238 | { |
| 1239 | long error; |
| 1240 | struct file *in, *out; |
| 1241 | int fput_in, fput_out; |
| 1242 | |
| 1243 | if (unlikely(!len)) |
| 1244 | return 0; |
| 1245 | |
| 1246 | error = -EBADF; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1247 | in = fget_light(fd_in, &fput_in); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1248 | if (in) { |
| 1249 | if (in->f_mode & FMODE_READ) { |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1250 | out = fget_light(fd_out, &fput_out); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1251 | if (out) { |
| 1252 | if (out->f_mode & FMODE_WRITE) |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1253 | error = do_splice(in, off_in, |
| 1254 | out, off_out, |
| 1255 | len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1256 | fput_light(out, fput_out); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | fput_light(in, fput_in); |
| 1261 | } |
| 1262 | |
| 1263 | return error; |
| 1264 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1265 | |
| 1266 | /* |
| 1267 | * Link contents of ipipe to opipe. |
| 1268 | */ |
| 1269 | static int link_pipe(struct pipe_inode_info *ipipe, |
| 1270 | struct pipe_inode_info *opipe, |
| 1271 | size_t len, unsigned int flags) |
| 1272 | { |
| 1273 | struct pipe_buffer *ibuf, *obuf; |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1274 | int ret, do_wakeup, i, ipipe_first; |
| 1275 | |
| 1276 | ret = do_wakeup = ipipe_first = 0; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1277 | |
| 1278 | /* |
| 1279 | * Potential ABBA deadlock, work around it by ordering lock |
| 1280 | * grabbing by inode address. Otherwise two different processes |
| 1281 | * could deadlock (one doing tee from A -> B, the other from B -> A). |
| 1282 | */ |
| 1283 | if (ipipe->inode < opipe->inode) { |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1284 | ipipe_first = 1; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1285 | mutex_lock(&ipipe->inode->i_mutex); |
| 1286 | mutex_lock(&opipe->inode->i_mutex); |
| 1287 | } else { |
| 1288 | mutex_lock(&opipe->inode->i_mutex); |
| 1289 | mutex_lock(&ipipe->inode->i_mutex); |
| 1290 | } |
| 1291 | |
| 1292 | for (i = 0;; i++) { |
| 1293 | if (!opipe->readers) { |
| 1294 | send_sig(SIGPIPE, current, 0); |
| 1295 | if (!ret) |
| 1296 | ret = -EPIPE; |
| 1297 | break; |
| 1298 | } |
| 1299 | if (ipipe->nrbufs - i) { |
| 1300 | ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1)); |
| 1301 | |
| 1302 | /* |
| 1303 | * If we have room, fill this buffer |
| 1304 | */ |
| 1305 | if (opipe->nrbufs < PIPE_BUFFERS) { |
| 1306 | int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1); |
| 1307 | |
| 1308 | /* |
| 1309 | * Get a reference to this pipe buffer, |
| 1310 | * so we can copy the contents over. |
| 1311 | */ |
| 1312 | ibuf->ops->get(ipipe, ibuf); |
| 1313 | |
| 1314 | obuf = opipe->bufs + nbuf; |
| 1315 | *obuf = *ibuf; |
| 1316 | |
| 1317 | if (obuf->len > len) |
| 1318 | obuf->len = len; |
| 1319 | |
| 1320 | opipe->nrbufs++; |
| 1321 | do_wakeup = 1; |
| 1322 | ret += obuf->len; |
| 1323 | len -= obuf->len; |
| 1324 | |
| 1325 | if (!len) |
| 1326 | break; |
| 1327 | if (opipe->nrbufs < PIPE_BUFFERS) |
| 1328 | continue; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * We have input available, but no output room. |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1333 | * If we already copied data, return that. If we |
| 1334 | * need to drop the opipe lock, it must be ordered |
| 1335 | * last to avoid deadlocks. |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1336 | */ |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1337 | if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) { |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1338 | if (!ret) |
| 1339 | ret = -EAGAIN; |
| 1340 | break; |
| 1341 | } |
| 1342 | if (signal_pending(current)) { |
| 1343 | if (!ret) |
| 1344 | ret = -ERESTARTSYS; |
| 1345 | break; |
| 1346 | } |
| 1347 | if (do_wakeup) { |
| 1348 | smp_mb(); |
| 1349 | if (waitqueue_active(&opipe->wait)) |
| 1350 | wake_up_interruptible(&opipe->wait); |
| 1351 | kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); |
| 1352 | do_wakeup = 0; |
| 1353 | } |
| 1354 | |
| 1355 | opipe->waiting_writers++; |
| 1356 | pipe_wait(opipe); |
| 1357 | opipe->waiting_writers--; |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * No input buffers, do the usual checks for available |
| 1363 | * writers and blocking and wait if necessary |
| 1364 | */ |
| 1365 | if (!ipipe->writers) |
| 1366 | break; |
| 1367 | if (!ipipe->waiting_writers) { |
| 1368 | if (ret) |
| 1369 | break; |
| 1370 | } |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1371 | /* |
| 1372 | * pipe_wait() drops the ipipe mutex. To avoid deadlocks |
| 1373 | * with another process, we can only safely do that if |
| 1374 | * the ipipe lock is ordered last. |
| 1375 | */ |
| 1376 | if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) { |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1377 | if (!ret) |
| 1378 | ret = -EAGAIN; |
| 1379 | break; |
| 1380 | } |
| 1381 | if (signal_pending(current)) { |
| 1382 | if (!ret) |
| 1383 | ret = -ERESTARTSYS; |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | if (waitqueue_active(&ipipe->wait)) |
| 1388 | wake_up_interruptible_sync(&ipipe->wait); |
| 1389 | kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT); |
| 1390 | |
| 1391 | pipe_wait(ipipe); |
| 1392 | } |
| 1393 | |
| 1394 | mutex_unlock(&ipipe->inode->i_mutex); |
| 1395 | mutex_unlock(&opipe->inode->i_mutex); |
| 1396 | |
| 1397 | if (do_wakeup) { |
| 1398 | smp_mb(); |
| 1399 | if (waitqueue_active(&opipe->wait)) |
| 1400 | wake_up_interruptible(&opipe->wait); |
| 1401 | kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); |
| 1402 | } |
| 1403 | |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * This is a tee(1) implementation that works on pipes. It doesn't copy |
| 1409 | * any data, it simply references the 'in' pages on the 'out' pipe. |
| 1410 | * The 'flags' used are the SPLICE_F_* variants, currently the only |
| 1411 | * applicable one is SPLICE_F_NONBLOCK. |
| 1412 | */ |
| 1413 | static long do_tee(struct file *in, struct file *out, size_t len, |
| 1414 | unsigned int flags) |
| 1415 | { |
| 1416 | struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe; |
| 1417 | struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe; |
| 1418 | |
| 1419 | /* |
| 1420 | * Link ipipe to the two output pipes, consuming as we go along. |
| 1421 | */ |
| 1422 | if (ipipe && opipe) |
| 1423 | return link_pipe(ipipe, opipe, len, flags); |
| 1424 | |
| 1425 | return -EINVAL; |
| 1426 | } |
| 1427 | |
| 1428 | asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags) |
| 1429 | { |
| 1430 | struct file *in; |
| 1431 | int error, fput_in; |
| 1432 | |
| 1433 | if (unlikely(!len)) |
| 1434 | return 0; |
| 1435 | |
| 1436 | error = -EBADF; |
| 1437 | in = fget_light(fdin, &fput_in); |
| 1438 | if (in) { |
| 1439 | if (in->f_mode & FMODE_READ) { |
| 1440 | int fput_out; |
| 1441 | struct file *out = fget_light(fdout, &fput_out); |
| 1442 | |
| 1443 | if (out) { |
| 1444 | if (out->f_mode & FMODE_WRITE) |
| 1445 | error = do_tee(in, out, len, flags); |
| 1446 | fput_light(out, fput_out); |
| 1447 | } |
| 1448 | } |
| 1449 | fput_light(in, fput_in); |
| 1450 | } |
| 1451 | |
| 1452 | return error; |
| 1453 | } |