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