Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * "splice": joining two ropes together by interweaving their strands. |
| 3 | * |
| 4 | * This is the "extended pipe" functionality, where a pipe is used as |
| 5 | * an arbitrary in-memory buffer. Think of a pipe as a small kernel |
| 6 | * buffer that you can use to transfer data from one end to the other. |
| 7 | * |
| 8 | * The traditional unix read/write is extended with a "splice()" operation |
| 9 | * that transfers data buffers to or from a pipe buffer. |
| 10 | * |
| 11 | * Named by Larry McVoy, original implementation from Linus, extended by |
Jens Axboe | c2058e0 | 2006-04-11 13:56:34 +0200 | [diff] [blame] | 12 | * Jens to support splicing to files, network, direct splicing, etc and |
| 13 | * fixing lots of bugs. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 14 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 15 | * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | c2058e0 | 2006-04-11 13:56:34 +0200 | [diff] [blame] | 16 | * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> |
| 17 | * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/file.h> |
| 22 | #include <linux/pagemap.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 23 | #include <linux/splice.h> |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 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 | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 32 | /* |
| 33 | * Attempt to steal a page from a pipe buffer. This should perhaps go into |
| 34 | * a vm helper function, it's already simplified quite a bit by the |
| 35 | * addition of remove_mapping(). If success is returned, the caller may |
| 36 | * attempt to reuse this page for another destination. |
| 37 | */ |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 38 | static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 39 | struct pipe_buffer *buf) |
| 40 | { |
| 41 | struct page *page = buf->page; |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 42 | struct address_space *mapping; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 43 | |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 44 | lock_page(page); |
| 45 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 46 | mapping = page_mapping(page); |
| 47 | if (mapping) { |
| 48 | WARN_ON(!PageUptodate(page)); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 49 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 50 | /* |
| 51 | * At least for ext2 with nobh option, we need to wait on |
| 52 | * writeback completing on this page, since we'll remove it |
| 53 | * from the pagecache. Otherwise truncate wont wait on the |
| 54 | * page, allowing the disk blocks to be reused by someone else |
| 55 | * before we actually wrote our data to them. fs corruption |
| 56 | * ensues. |
| 57 | */ |
| 58 | wait_on_page_writeback(page); |
Jens Axboe | ad8d6f0 | 2006-04-02 23:10:32 +0200 | [diff] [blame] | 59 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 60 | if (PagePrivate(page)) |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 61 | try_to_release_page(page, GFP_KERNEL); |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 62 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 63 | /* |
| 64 | * If we succeeded in removing the mapping, set LRU flag |
| 65 | * and return good. |
| 66 | */ |
| 67 | if (remove_mapping(mapping, page)) { |
| 68 | buf->flags |= PIPE_BUF_FLAG_LRU; |
| 69 | return 0; |
| 70 | } |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 71 | } |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 72 | |
Jens Axboe | 9e94cd4 | 2006-06-20 15:01:12 +0200 | [diff] [blame] | 73 | /* |
| 74 | * Raced with truncate or failed to remove page from current |
| 75 | * address space, unlock and return failure. |
| 76 | */ |
| 77 | unlock_page(page); |
| 78 | return 1; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 81 | static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 82 | struct pipe_buffer *buf) |
| 83 | { |
| 84 | page_cache_release(buf->page); |
Jens Axboe | 1432873 | 2006-05-03 10:35:26 +0200 | [diff] [blame] | 85 | buf->flags &= ~PIPE_BUF_FLAG_LRU; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Check whether the contents of buf is OK to access. Since the content |
| 90 | * is a page cache page, IO may be in flight. |
| 91 | */ |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 92 | static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, |
| 93 | struct pipe_buffer *buf) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 94 | { |
| 95 | struct page *page = buf->page; |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 96 | int err; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 97 | |
| 98 | if (!PageUptodate(page)) { |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 99 | lock_page(page); |
| 100 | |
| 101 | /* |
| 102 | * Page got truncated/unhashed. This will cause a 0-byte |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 103 | * splice, if this is the first page. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 104 | */ |
| 105 | if (!page->mapping) { |
| 106 | err = -ENODATA; |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 111 | * Uh oh, read-error from disk. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 112 | */ |
| 113 | if (!PageUptodate(page)) { |
| 114 | err = -EIO; |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | /* |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 119 | * Page is ok afterall, we are done. |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 120 | */ |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 121 | unlock_page(page); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 124 | return 0; |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 125 | error: |
| 126 | unlock_page(page); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 127 | return err; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 130 | static const struct pipe_buf_operations page_cache_pipe_buf_ops = { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 131 | .can_merge = 0, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 132 | .map = generic_pipe_buf_map, |
| 133 | .unmap = generic_pipe_buf_unmap, |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 134 | .confirm = page_cache_pipe_buf_confirm, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 135 | .release = page_cache_pipe_buf_release, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 136 | .steal = page_cache_pipe_buf_steal, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 137 | .get = generic_pipe_buf_get, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 140 | static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, |
| 141 | struct pipe_buffer *buf) |
| 142 | { |
Jens Axboe | 7afa6fd | 2006-05-01 20:02:33 +0200 | [diff] [blame] | 143 | if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) |
| 144 | return 1; |
| 145 | |
Jens Axboe | 1432873 | 2006-05-03 10:35:26 +0200 | [diff] [blame] | 146 | buf->flags |= PIPE_BUF_FLAG_LRU; |
Jens Axboe | 330ab71 | 2006-05-02 15:29:57 +0200 | [diff] [blame] | 147 | return generic_pipe_buf_steal(pipe, buf); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 150 | static const struct pipe_buf_operations user_page_pipe_buf_ops = { |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 151 | .can_merge = 0, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 152 | .map = generic_pipe_buf_map, |
| 153 | .unmap = generic_pipe_buf_unmap, |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 154 | .confirm = generic_pipe_buf_confirm, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 155 | .release = page_cache_pipe_buf_release, |
| 156 | .steal = user_page_pipe_buf_steal, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 157 | .get = generic_pipe_buf_get, |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 158 | }; |
| 159 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 160 | /** |
| 161 | * splice_to_pipe - fill passed data into a pipe |
| 162 | * @pipe: pipe to fill |
| 163 | * @spd: data to fill |
| 164 | * |
| 165 | * Description: |
| 166 | * @spd contains a map of pages and len/offset tupples, a long with |
| 167 | * the struct pipe_buf_operations associated with these pages. This |
| 168 | * function will link that data to the pipe. |
| 169 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 170 | */ |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 171 | ssize_t splice_to_pipe(struct pipe_inode_info *pipe, |
| 172 | struct splice_pipe_desc *spd) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 173 | { |
Jens Axboe | 00de00b | 2007-06-15 13:14:22 +0200 | [diff] [blame] | 174 | unsigned int spd_pages = spd->nr_pages; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 175 | int ret, do_wakeup, page_nr; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 176 | |
| 177 | ret = 0; |
| 178 | do_wakeup = 0; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 179 | page_nr = 0; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 180 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 181 | if (pipe->inode) |
| 182 | mutex_lock(&pipe->inode->i_mutex); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 183 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 184 | for (;;) { |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 185 | if (!pipe->readers) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 186 | send_sig(SIGPIPE, current, 0); |
| 187 | if (!ret) |
| 188 | ret = -EPIPE; |
| 189 | break; |
| 190 | } |
| 191 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 192 | if (pipe->nrbufs < PIPE_BUFFERS) { |
| 193 | int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 194 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
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 | buf->page = spd->pages[page_nr]; |
| 197 | buf->offset = spd->partial[page_nr].offset; |
| 198 | buf->len = spd->partial[page_nr].len; |
Jens Axboe | 497f962 | 2007-06-11 12:00:45 +0200 | [diff] [blame] | 199 | buf->private = spd->partial[page_nr].private; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 200 | buf->ops = spd->ops; |
Jens Axboe | 7afa6fd | 2006-05-01 20:02:33 +0200 | [diff] [blame] | 201 | if (spd->flags & SPLICE_F_GIFT) |
| 202 | buf->flags |= PIPE_BUF_FLAG_GIFT; |
| 203 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 204 | pipe->nrbufs++; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 205 | page_nr++; |
| 206 | ret += buf->len; |
| 207 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 208 | if (pipe->inode) |
| 209 | do_wakeup = 1; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 210 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 211 | if (!--spd->nr_pages) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 212 | break; |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 213 | if (pipe->nrbufs < PIPE_BUFFERS) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 214 | continue; |
| 215 | |
| 216 | break; |
| 217 | } |
| 218 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 219 | if (spd->flags & SPLICE_F_NONBLOCK) { |
Linus Torvalds | 29e3509 | 2006-04-02 12:46:35 -0700 | [diff] [blame] | 220 | if (!ret) |
| 221 | ret = -EAGAIN; |
| 222 | break; |
| 223 | } |
| 224 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 225 | if (signal_pending(current)) { |
| 226 | if (!ret) |
| 227 | ret = -ERESTARTSYS; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | if (do_wakeup) { |
Jens Axboe | c0bd1f6 | 2006-04-10 09:03:32 +0200 | [diff] [blame] | 232 | smp_mb(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 233 | if (waitqueue_active(&pipe->wait)) |
| 234 | wake_up_interruptible_sync(&pipe->wait); |
| 235 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 236 | do_wakeup = 0; |
| 237 | } |
| 238 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 239 | pipe->waiting_writers++; |
| 240 | pipe_wait(pipe); |
| 241 | pipe->waiting_writers--; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Jens Axboe | 02676e5 | 2007-06-15 13:16:13 +0200 | [diff] [blame] | 244 | if (pipe->inode) { |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 245 | mutex_unlock(&pipe->inode->i_mutex); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 246 | |
Jens Axboe | 02676e5 | 2007-06-15 13:16:13 +0200 | [diff] [blame] | 247 | if (do_wakeup) { |
| 248 | smp_mb(); |
| 249 | if (waitqueue_active(&pipe->wait)) |
| 250 | wake_up_interruptible(&pipe->wait); |
| 251 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
| 252 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Jens Axboe | 00de00b | 2007-06-15 13:14:22 +0200 | [diff] [blame] | 255 | while (page_nr < spd_pages) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 256 | page_cache_release(spd->pages[page_nr++]); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 261 | static int |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 262 | __generic_file_splice_read(struct file *in, loff_t *ppos, |
| 263 | struct pipe_inode_info *pipe, size_t len, |
| 264 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 265 | { |
| 266 | struct address_space *mapping = in->f_mapping; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 267 | unsigned int loff, nr_pages; |
Jens Axboe | 16c523d | 2006-04-10 09:03:58 +0200 | [diff] [blame] | 268 | struct page *pages[PIPE_BUFFERS]; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 269 | struct partial_page partial[PIPE_BUFFERS]; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 270 | struct page *page; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 271 | pgoff_t index, end_index; |
| 272 | loff_t isize; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 273 | int error, page_nr; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 274 | struct splice_pipe_desc spd = { |
| 275 | .pages = pages, |
| 276 | .partial = partial, |
| 277 | .flags = flags, |
| 278 | .ops = &page_cache_pipe_buf_ops, |
| 279 | }; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 280 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 281 | index = *ppos >> PAGE_CACHE_SHIFT; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 282 | loff = *ppos & ~PAGE_CACHE_MASK; |
| 283 | nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 284 | |
| 285 | if (nr_pages > PIPE_BUFFERS) |
| 286 | nr_pages = PIPE_BUFFERS; |
| 287 | |
| 288 | /* |
Jens Axboe | 86aa5ac | 2007-05-08 08:46:19 +0200 | [diff] [blame] | 289 | * Don't try to 2nd guess the read-ahead logic, call into |
| 290 | * page_cache_readahead() like the page cache reads would do. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 291 | */ |
Jens Axboe | 86aa5ac | 2007-05-08 08:46:19 +0200 | [diff] [blame] | 292 | page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 293 | |
| 294 | /* |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 295 | * Lookup the (hopefully) full range of pages we need. |
| 296 | */ |
| 297 | spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages); |
| 298 | |
| 299 | /* |
| 300 | * If find_get_pages_contig() returned fewer pages than we needed, |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 301 | * allocate the rest and fill in the holes. |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 302 | */ |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 303 | error = 0; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 304 | index += spd.nr_pages; |
| 305 | while (spd.nr_pages < nr_pages) { |
| 306 | /* |
| 307 | * Page could be there, find_get_pages_contig() breaks on |
| 308 | * the first hole. |
| 309 | */ |
| 310 | page = find_get_page(mapping, index); |
| 311 | if (!page) { |
| 312 | /* |
Jens Axboe | e27dedd | 2006-05-01 19:59:54 +0200 | [diff] [blame] | 313 | * Make sure the read-ahead engine is notified |
| 314 | * about this failure. |
| 315 | */ |
| 316 | handle_ra_miss(mapping, &in->f_ra, index); |
| 317 | |
| 318 | /* |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 319 | * page didn't exist, allocate one. |
| 320 | */ |
| 321 | page = page_cache_alloc_cold(mapping); |
| 322 | if (!page) |
| 323 | break; |
| 324 | |
| 325 | error = add_to_page_cache_lru(page, mapping, index, |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 326 | GFP_KERNEL); |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 327 | if (unlikely(error)) { |
| 328 | page_cache_release(page); |
Jens Axboe | a054887 | 2006-05-03 10:58:22 +0200 | [diff] [blame] | 329 | if (error == -EEXIST) |
| 330 | continue; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | /* |
| 334 | * add_to_page_cache() locks the page, unlock it |
| 335 | * to avoid convoluting the logic below even more. |
| 336 | */ |
| 337 | unlock_page(page); |
| 338 | } |
| 339 | |
| 340 | pages[spd.nr_pages++] = page; |
| 341 | index++; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Now loop over the map and see if we need to start IO on any |
| 346 | * pages, fill in the partial map, etc. |
| 347 | */ |
| 348 | index = *ppos >> PAGE_CACHE_SHIFT; |
| 349 | nr_pages = spd.nr_pages; |
| 350 | spd.nr_pages = 0; |
| 351 | for (page_nr = 0; page_nr < nr_pages; page_nr++) { |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 352 | unsigned int this_len; |
| 353 | |
| 354 | if (!len) |
| 355 | break; |
| 356 | |
| 357 | /* |
| 358 | * this_len is the max we'll use from this page |
| 359 | */ |
Andrew Morton | ba5f5d9 | 2006-04-25 15:33:34 +0200 | [diff] [blame] | 360 | this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 361 | page = pages[page_nr]; |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * If the page isn't uptodate, we may need to start io on it |
| 365 | */ |
| 366 | if (!PageUptodate(page)) { |
Jens Axboe | c4f895c | 2006-04-19 15:56:12 +0200 | [diff] [blame] | 367 | /* |
| 368 | * If in nonblock mode then dont block on waiting |
| 369 | * for an in-flight io page |
| 370 | */ |
Fengguang Wu | 9ae9d68 | 2007-05-08 08:44:36 +0200 | [diff] [blame] | 371 | if (flags & SPLICE_F_NONBLOCK) { |
| 372 | if (TestSetPageLocked(page)) |
| 373 | break; |
| 374 | } else |
| 375 | lock_page(page); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * page was truncated, stop here. if this isn't the |
| 379 | * first page, we'll just complete what we already |
| 380 | * added |
| 381 | */ |
| 382 | if (!page->mapping) { |
| 383 | unlock_page(page); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | /* |
| 387 | * page was already under io and is now done, great |
| 388 | */ |
| 389 | if (PageUptodate(page)) { |
| 390 | unlock_page(page); |
| 391 | goto fill_it; |
| 392 | } |
| 393 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 394 | /* |
| 395 | * need to read in the page |
| 396 | */ |
| 397 | error = mapping->a_ops->readpage(in, page); |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 398 | if (unlikely(error)) { |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 399 | /* |
| 400 | * We really should re-lookup the page here, |
| 401 | * but it complicates things a lot. Instead |
| 402 | * lets just do what we already stored, and |
| 403 | * we'll get it the next time we are called. |
| 404 | */ |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 405 | if (error == AOP_TRUNCATED_PAGE) |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 406 | error = 0; |
| 407 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 408 | break; |
| 409 | } |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 410 | } |
| 411 | fill_it: |
| 412 | /* |
| 413 | * i_size must be checked after PageUptodate. |
| 414 | */ |
| 415 | isize = i_size_read(mapping->host); |
| 416 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 417 | if (unlikely(!isize || index > end_index)) |
| 418 | break; |
| 419 | |
| 420 | /* |
| 421 | * if this is the last page, see if we need to shrink |
| 422 | * the length and stop |
| 423 | */ |
| 424 | if (end_index == index) { |
| 425 | unsigned int plen; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 426 | |
| 427 | /* |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 428 | * max good bytes in this page |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 429 | */ |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 430 | plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 431 | if (plen <= loff) |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 432 | break; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 433 | |
| 434 | /* |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 435 | * force quit after adding this page |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 436 | */ |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 437 | this_len = min(this_len, plen - loff); |
| 438 | len = this_len; |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 439 | } |
Jens Axboe | 620a324 | 2007-06-07 09:39:42 +0200 | [diff] [blame] | 440 | |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 441 | partial[page_nr].offset = loff; |
| 442 | partial[page_nr].len = this_len; |
Jens Axboe | 82aa5d6 | 2006-04-20 13:05:48 +0200 | [diff] [blame] | 443 | len -= this_len; |
Jens Axboe | 91ad66e | 2006-04-19 15:55:10 +0200 | [diff] [blame] | 444 | loff = 0; |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 445 | spd.nr_pages++; |
| 446 | index++; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 449 | /* |
Hugh Dickins | 475ecad | 2007-06-07 09:36:00 +0200 | [diff] [blame] | 450 | * Release any pages at the end, if we quit early. 'page_nr' is how far |
Jens Axboe | eb20796 | 2006-04-27 11:05:22 +0200 | [diff] [blame] | 451 | * we got, 'nr_pages' is how many pages are in the map. |
| 452 | */ |
| 453 | while (page_nr < nr_pages) |
| 454 | page_cache_release(pages[page_nr++]); |
| 455 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 456 | if (spd.nr_pages) |
Jens Axboe | 00522fb | 2006-04-26 14:39:29 +0200 | [diff] [blame] | 457 | return splice_to_pipe(pipe, &spd); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 458 | |
Jens Axboe | 7480a90 | 2006-04-11 13:52:47 +0200 | [diff] [blame] | 459 | return error; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 462 | /** |
| 463 | * generic_file_splice_read - splice data from file to a pipe |
| 464 | * @in: file to splice from |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 465 | * @ppos: position in @in |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 466 | * @pipe: pipe to splice to |
| 467 | * @len: number of bytes to splice |
| 468 | * @flags: splice modifier flags |
| 469 | * |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 470 | * Description: |
| 471 | * Will read pages from given file and fill them into a pipe. Can be |
| 472 | * used as long as the address_space operations for the source implements |
| 473 | * a readpage() hook. |
| 474 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 475 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 476 | ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, |
| 477 | struct pipe_inode_info *pipe, size_t len, |
| 478 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 479 | { |
| 480 | ssize_t spliced; |
| 481 | int ret; |
Jens Axboe | d366d398 | 2007-06-01 14:54:11 +0200 | [diff] [blame] | 482 | loff_t isize, left; |
| 483 | |
| 484 | isize = i_size_read(in->f_mapping->host); |
| 485 | if (unlikely(*ppos >= isize)) |
| 486 | return 0; |
| 487 | |
| 488 | left = isize - *ppos; |
| 489 | if (unlikely(left < len)) |
| 490 | len = left; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 491 | |
| 492 | ret = 0; |
| 493 | spliced = 0; |
| 494 | while (len) { |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 495 | ret = __generic_file_splice_read(in, ppos, pipe, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 496 | |
Jens Axboe | c4f895c | 2006-04-19 15:56:12 +0200 | [diff] [blame] | 497 | if (ret < 0) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 498 | break; |
Jens Axboe | c4f895c | 2006-04-19 15:56:12 +0200 | [diff] [blame] | 499 | else if (!ret) { |
| 500 | if (spliced) |
| 501 | break; |
| 502 | if (flags & SPLICE_F_NONBLOCK) { |
| 503 | ret = -EAGAIN; |
| 504 | break; |
| 505 | } |
| 506 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 507 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 508 | *ppos += ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 509 | len -= ret; |
| 510 | spliced += ret; |
| 511 | } |
| 512 | |
| 513 | if (spliced) |
| 514 | return spliced; |
| 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 519 | EXPORT_SYMBOL(generic_file_splice_read); |
| 520 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 521 | /* |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 522 | * 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] | 523 | * using sendpage(). Return the number of bytes sent. |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 524 | */ |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 525 | static int pipe_to_sendpage(struct pipe_inode_info *pipe, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 526 | struct pipe_buffer *buf, struct splice_desc *sd) |
| 527 | { |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 528 | struct file *file = sd->u.file; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 529 | loff_t pos = sd->pos; |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 530 | int ret, more; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 531 | |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 532 | ret = buf->ops->confirm(pipe, buf); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 533 | if (!ret) { |
| 534 | more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 535 | |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 536 | ret = file->f_op->sendpage(file, buf->page, buf->offset, |
| 537 | sd->len, &pos, more); |
| 538 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 539 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 540 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| 544 | * This is a little more tricky than the file -> pipe splicing. There are |
| 545 | * basically three cases: |
| 546 | * |
| 547 | * - Destination page already exists in the address space and there |
| 548 | * are users of it. For that case we have no other option that |
| 549 | * copying the data. Tough luck. |
| 550 | * - Destination page already exists in the address space, but there |
| 551 | * are no users of it. Make sure it's uptodate, then drop it. Fall |
| 552 | * through to last case. |
| 553 | * - Destination page does not exist, we can add the pipe page to |
| 554 | * the page cache and avoid the copy. |
| 555 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 556 | * If asked to move pages to the output file (SPLICE_F_MOVE is set in |
| 557 | * sd->flags), we attempt to migrate pages from the pipe to the output |
| 558 | * file address space page cache. This is possible if no one else has |
| 559 | * the pipe page referenced outside of the pipe and page cache. If |
| 560 | * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create |
| 561 | * 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] | 562 | */ |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 563 | static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 564 | struct splice_desc *sd) |
| 565 | { |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 566 | struct file *file = sd->u.file; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 567 | struct address_space *mapping = file->f_mapping; |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 568 | unsigned int offset, this_len; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 569 | struct page *page; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 570 | pgoff_t index; |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 571 | int ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 572 | |
| 573 | /* |
Jens Axboe | 49d0b21 | 2006-04-10 09:04:41 +0200 | [diff] [blame] | 574 | * make sure the data in this buffer is uptodate |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 575 | */ |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 576 | ret = buf->ops->confirm(pipe, buf); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 577 | if (unlikely(ret)) |
| 578 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 579 | |
| 580 | index = sd->pos >> PAGE_CACHE_SHIFT; |
| 581 | offset = sd->pos & ~PAGE_CACHE_MASK; |
| 582 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 583 | this_len = sd->len; |
| 584 | if (this_len + offset > PAGE_CACHE_SIZE) |
| 585 | this_len = PAGE_CACHE_SIZE - offset; |
| 586 | |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 587 | find_page: |
Nick Piggin | 485ddb4 | 2007-03-27 08:55:08 +0200 | [diff] [blame] | 588 | page = find_lock_page(mapping, index); |
| 589 | if (!page) { |
| 590 | ret = -ENOMEM; |
| 591 | page = page_cache_alloc_cold(mapping); |
| 592 | if (unlikely(!page)) |
| 593 | goto out_ret; |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 594 | |
Nick Piggin | 485ddb4 | 2007-03-27 08:55:08 +0200 | [diff] [blame] | 595 | /* |
| 596 | * This will also lock the page |
| 597 | */ |
| 598 | ret = add_to_page_cache_lru(page, mapping, index, |
| 599 | GFP_KERNEL); |
| 600 | if (unlikely(ret)) |
| 601 | goto out; |
| 602 | } |
| 603 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 604 | ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len); |
Jens Axboe | bfc4ee3 | 2006-05-03 10:35:10 +0200 | [diff] [blame] | 605 | if (unlikely(ret)) { |
| 606 | loff_t isize = i_size_read(mapping->host); |
| 607 | |
| 608 | if (ret != AOP_TRUNCATED_PAGE) |
| 609 | unlock_page(page); |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 610 | page_cache_release(page); |
Jens Axboe | bfc4ee3 | 2006-05-03 10:35:10 +0200 | [diff] [blame] | 611 | if (ret == AOP_TRUNCATED_PAGE) |
| 612 | goto find_page; |
| 613 | |
| 614 | /* |
| 615 | * prepare_write() may have instantiated a few blocks |
| 616 | * outside i_size. Trim these off again. |
| 617 | */ |
| 618 | if (sd->pos + this_len > isize) |
| 619 | vmtruncate(mapping->host, isize); |
| 620 | |
Jens Axboe | e6e80f2 | 2006-10-11 10:03:09 +0200 | [diff] [blame] | 621 | goto out_ret; |
Jens Axboe | bfc4ee3 | 2006-05-03 10:35:10 +0200 | [diff] [blame] | 622 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 623 | |
Jens Axboe | 0568b40 | 2006-05-01 19:50:48 +0200 | [diff] [blame] | 624 | if (buf->page != page) { |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 625 | /* |
| 626 | * Careful, ->map() uses KM_USER0! |
| 627 | */ |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 628 | char *src = buf->ops->map(pipe, buf, 1); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 629 | char *dst = kmap_atomic(page, KM_USER1); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 630 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 631 | memcpy(dst + offset, src + buf->offset, this_len); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 632 | flush_dcache_page(page); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 633 | kunmap_atomic(dst, KM_USER1); |
Jens Axboe | 76ad4d1 | 2006-05-03 10:41:33 +0200 | [diff] [blame] | 634 | buf->ops->unmap(pipe, buf, src); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 635 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 636 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 637 | ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len); |
Dmitriy Monakhov | d9993c3 | 2007-03-29 14:24:09 +0200 | [diff] [blame] | 638 | if (ret) { |
| 639 | if (ret == AOP_TRUNCATED_PAGE) { |
| 640 | page_cache_release(page); |
| 641 | goto find_page; |
| 642 | } |
| 643 | if (ret < 0) |
| 644 | goto out; |
Jens Axboe | 0568b40 | 2006-05-01 19:50:48 +0200 | [diff] [blame] | 645 | /* |
Dmitriy Monakhov | d9993c3 | 2007-03-29 14:24:09 +0200 | [diff] [blame] | 646 | * Partial write has happened, so 'ret' already initialized by |
| 647 | * number of bytes written, Where is nothing we have to do here. |
Jens Axboe | 0568b40 | 2006-05-01 19:50:48 +0200 | [diff] [blame] | 648 | */ |
Dmitriy Monakhov | d9993c3 | 2007-03-29 14:24:09 +0200 | [diff] [blame] | 649 | } else |
Jens Axboe | 0568b40 | 2006-05-01 19:50:48 +0200 | [diff] [blame] | 650 | ret = this_len; |
Dmitriy Monakhov | d9993c3 | 2007-03-29 14:24:09 +0200 | [diff] [blame] | 651 | /* |
| 652 | * Return the number of bytes written and mark page as |
| 653 | * accessed, we are now done! |
| 654 | */ |
| 655 | mark_page_accessed(page); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 656 | out: |
Jens Axboe | 0568b40 | 2006-05-01 19:50:48 +0200 | [diff] [blame] | 657 | page_cache_release(page); |
Jens Axboe | 9e0267c | 2006-04-19 15:57:31 +0200 | [diff] [blame] | 658 | unlock_page(page); |
Jens Axboe | e6e80f2 | 2006-10-11 10:03:09 +0200 | [diff] [blame] | 659 | out_ret: |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 660 | return ret; |
| 661 | } |
| 662 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 663 | /** |
| 664 | * __splice_from_pipe - splice data from a pipe to given actor |
| 665 | * @pipe: pipe to splice from |
| 666 | * @sd: information to @actor |
| 667 | * @actor: handler that splices the data |
| 668 | * |
| 669 | * Description: |
| 670 | * This function does little more than loop over the pipe and call |
| 671 | * @actor to do the actual moving of a single struct pipe_buffer to |
| 672 | * the desired destination. See pipe_to_file, pipe_to_sendpage, or |
| 673 | * pipe_to_user. |
| 674 | * |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 675 | */ |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 676 | ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, |
| 677 | splice_actor *actor) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 678 | { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 679 | int ret, do_wakeup, err; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 680 | |
| 681 | ret = 0; |
| 682 | do_wakeup = 0; |
| 683 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 684 | for (;;) { |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 685 | if (pipe->nrbufs) { |
| 686 | struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 687 | const struct pipe_buf_operations *ops = buf->ops; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 688 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 689 | sd->len = buf->len; |
| 690 | if (sd->len > sd->total_len) |
| 691 | sd->len = sd->total_len; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 692 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 693 | err = actor(pipe, buf, sd); |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 694 | if (err <= 0) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 695 | if (!ret && err != -ENODATA) |
| 696 | ret = err; |
| 697 | |
| 698 | break; |
| 699 | } |
| 700 | |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 701 | ret += err; |
| 702 | buf->offset += err; |
| 703 | buf->len -= err; |
| 704 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 705 | sd->len -= err; |
| 706 | sd->pos += err; |
| 707 | sd->total_len -= err; |
| 708 | if (sd->len) |
Jens Axboe | 016b661 | 2006-04-25 15:42:00 +0200 | [diff] [blame] | 709 | continue; |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 710 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 711 | if (!buf->len) { |
| 712 | buf->ops = NULL; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 713 | ops->release(pipe, buf); |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 714 | pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); |
| 715 | pipe->nrbufs--; |
| 716 | if (pipe->inode) |
| 717 | do_wakeup = 1; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 720 | if (!sd->total_len) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | |
Jens Axboe | 6f767b0 | 2006-04-11 13:53:56 +0200 | [diff] [blame] | 724 | if (pipe->nrbufs) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 725 | continue; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 726 | if (!pipe->writers) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 727 | break; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 728 | if (!pipe->waiting_writers) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 729 | if (ret) |
| 730 | break; |
| 731 | } |
| 732 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 733 | if (sd->flags & SPLICE_F_NONBLOCK) { |
Linus Torvalds | 29e3509 | 2006-04-02 12:46:35 -0700 | [diff] [blame] | 734 | if (!ret) |
| 735 | ret = -EAGAIN; |
| 736 | break; |
| 737 | } |
| 738 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 739 | if (signal_pending(current)) { |
| 740 | if (!ret) |
| 741 | ret = -ERESTARTSYS; |
| 742 | break; |
| 743 | } |
| 744 | |
| 745 | if (do_wakeup) { |
Jens Axboe | c0bd1f6 | 2006-04-10 09:03:32 +0200 | [diff] [blame] | 746 | smp_mb(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 747 | if (waitqueue_active(&pipe->wait)) |
| 748 | wake_up_interruptible_sync(&pipe->wait); |
| 749 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 750 | do_wakeup = 0; |
| 751 | } |
| 752 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 753 | pipe_wait(pipe); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 754 | } |
| 755 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 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(&pipe->wait); |
| 760 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 761 | } |
| 762 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 763 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 764 | } |
Mark Fasheh | 40bee44e | 2007-03-21 13:11:02 +0100 | [diff] [blame] | 765 | EXPORT_SYMBOL(__splice_from_pipe); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 766 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 767 | /** |
| 768 | * splice_from_pipe - splice data from a pipe to a file |
| 769 | * @pipe: pipe to splice from |
| 770 | * @out: file to splice to |
| 771 | * @ppos: position in @out |
| 772 | * @len: how many bytes to splice |
| 773 | * @flags: splice modifier flags |
| 774 | * @actor: handler that splices the data |
| 775 | * |
| 776 | * Description: |
| 777 | * See __splice_from_pipe. This function locks the input and output inodes, |
| 778 | * otherwise it's identical to __splice_from_pipe(). |
| 779 | * |
| 780 | */ |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 781 | ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, |
| 782 | loff_t *ppos, size_t len, unsigned int flags, |
| 783 | splice_actor *actor) |
| 784 | { |
| 785 | ssize_t ret; |
| 786 | struct inode *inode = out->f_mapping->host; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 787 | struct splice_desc sd = { |
| 788 | .total_len = len, |
| 789 | .flags = flags, |
| 790 | .pos = *ppos, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 791 | .u.file = out, |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 792 | }; |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 793 | |
| 794 | /* |
| 795 | * The actor worker might be calling ->prepare_write and |
| 796 | * ->commit_write. Most of the time, these expect i_mutex to |
| 797 | * be held. Since this may result in an ABBA deadlock with |
| 798 | * pipe->inode, we have to order lock acquiry here. |
| 799 | */ |
| 800 | inode_double_lock(inode, pipe->inode); |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 801 | ret = __splice_from_pipe(pipe, &sd, actor); |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 802 | inode_double_unlock(inode, pipe->inode); |
| 803 | |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * generic_file_splice_write_nolock - generic_file_splice_write without mutexes |
| 809 | * @pipe: pipe info |
| 810 | * @out: file to write to |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 811 | * @ppos: position in @out |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 812 | * @len: number of bytes to splice |
| 813 | * @flags: splice modifier flags |
| 814 | * |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 815 | * Description: |
| 816 | * Will either move or copy pages (determined by @flags options) from |
| 817 | * the given pipe inode to the given file. The caller is responsible |
| 818 | * for acquiring i_mutex on both inodes. |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 819 | * |
| 820 | */ |
| 821 | ssize_t |
| 822 | generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out, |
| 823 | loff_t *ppos, size_t len, unsigned int flags) |
| 824 | { |
| 825 | struct address_space *mapping = out->f_mapping; |
| 826 | struct inode *inode = mapping->host; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 827 | struct splice_desc sd = { |
| 828 | .total_len = len, |
| 829 | .flags = flags, |
| 830 | .pos = *ppos, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 831 | .u.file = out, |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 832 | }; |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 833 | ssize_t ret; |
| 834 | int err; |
| 835 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 836 | err = remove_suid(out->f_path.dentry); |
Jens Axboe | 8c34e2d | 2006-10-17 19:43:22 +0200 | [diff] [blame] | 837 | if (unlikely(err)) |
| 838 | return err; |
| 839 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 840 | ret = __splice_from_pipe(pipe, &sd, pipe_to_file); |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 841 | if (ret > 0) { |
Jens Axboe | 17ee4f4 | 2007-06-15 13:10:37 +0200 | [diff] [blame] | 842 | unsigned long nr_pages; |
| 843 | |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 844 | *ppos += ret; |
Jens Axboe | 17ee4f4 | 2007-06-15 13:10:37 +0200 | [diff] [blame] | 845 | nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * If file or inode is SYNC and we actually wrote some data, |
| 849 | * sync it. |
| 850 | */ |
| 851 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 852 | err = generic_osync_inode(inode, mapping, |
| 853 | OSYNC_METADATA|OSYNC_DATA); |
| 854 | |
| 855 | if (err) |
| 856 | ret = err; |
| 857 | } |
Jens Axboe | 17ee4f4 | 2007-06-15 13:10:37 +0200 | [diff] [blame] | 858 | balance_dirty_pages_ratelimited_nr(mapping, nr_pages); |
Mark Fasheh | 6da6180 | 2006-10-17 18:43:07 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | EXPORT_SYMBOL(generic_file_splice_write_nolock); |
| 865 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 866 | /** |
| 867 | * generic_file_splice_write - splice data from a pipe to a file |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 868 | * @pipe: pipe info |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 869 | * @out: file to write to |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 870 | * @ppos: position in @out |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 871 | * @len: number of bytes to splice |
| 872 | * @flags: splice modifier flags |
| 873 | * |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 874 | * Description: |
| 875 | * Will either move or copy pages (determined by @flags options) from |
| 876 | * the given pipe inode to the given file. |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 877 | * |
| 878 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 879 | ssize_t |
| 880 | generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 881 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 882 | { |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 883 | struct address_space *mapping = out->f_mapping; |
Jens Axboe | 8c34e2d | 2006-10-17 19:43:22 +0200 | [diff] [blame] | 884 | struct inode *inode = mapping->host; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 885 | ssize_t ret; |
Jens Axboe | 8c34e2d | 2006-10-17 19:43:22 +0200 | [diff] [blame] | 886 | int err; |
| 887 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 888 | err = should_remove_suid(out->f_path.dentry); |
Jens Axboe | 8c34e2d | 2006-10-17 19:43:22 +0200 | [diff] [blame] | 889 | if (unlikely(err)) { |
| 890 | mutex_lock(&inode->i_mutex); |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 891 | err = __remove_suid(out->f_path.dentry, err); |
Jens Axboe | 8c34e2d | 2006-10-17 19:43:22 +0200 | [diff] [blame] | 892 | mutex_unlock(&inode->i_mutex); |
| 893 | if (err) |
| 894 | return err; |
| 895 | } |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 896 | |
Jens Axboe | 00522fb | 2006-04-26 14:39:29 +0200 | [diff] [blame] | 897 | ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file); |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 898 | if (ret > 0) { |
Jens Axboe | 17ee4f4 | 2007-06-15 13:10:37 +0200 | [diff] [blame] | 899 | unsigned long nr_pages; |
| 900 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 901 | *ppos += ret; |
Jens Axboe | 17ee4f4 | 2007-06-15 13:10:37 +0200 | [diff] [blame] | 902 | nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 903 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 904 | /* |
| 905 | * If file or inode is SYNC and we actually wrote some data, |
| 906 | * sync it. |
| 907 | */ |
| 908 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 909 | mutex_lock(&inode->i_mutex); |
| 910 | err = generic_osync_inode(inode, mapping, |
| 911 | OSYNC_METADATA|OSYNC_DATA); |
| 912 | mutex_unlock(&inode->i_mutex); |
| 913 | |
| 914 | if (err) |
| 915 | ret = err; |
| 916 | } |
Jens Axboe | 17ee4f4 | 2007-06-15 13:10:37 +0200 | [diff] [blame] | 917 | balance_dirty_pages_ratelimited_nr(mapping, nr_pages); |
Jens Axboe | 4f6f0bd | 2006-04-02 23:04:46 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | return ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 923 | EXPORT_SYMBOL(generic_file_splice_write); |
| 924 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 925 | /** |
| 926 | * generic_splice_sendpage - splice data from a pipe to a socket |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 927 | * @pipe: pipe to splice from |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 928 | * @out: socket to write to |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 929 | * @ppos: position in @out |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 930 | * @len: number of bytes to splice |
| 931 | * @flags: splice modifier flags |
| 932 | * |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 933 | * Description: |
| 934 | * Will send @len bytes from the pipe to a network socket. No data copying |
| 935 | * is involved. |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 936 | * |
| 937 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 938 | 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] | 939 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 940 | { |
Jens Axboe | 00522fb | 2006-04-26 14:39:29 +0200 | [diff] [blame] | 941 | return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Jens Axboe | 059a8f3 | 2006-04-02 23:06:05 +0200 | [diff] [blame] | 944 | EXPORT_SYMBOL(generic_splice_sendpage); |
Jeff Garzik | a0f0678 | 2006-03-30 23:06:13 -0500 | [diff] [blame] | 945 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 946 | /* |
| 947 | * Attempt to initiate a splice from pipe to file. |
| 948 | */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 949 | 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] | 950 | loff_t *ppos, size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 951 | { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 952 | int ret; |
| 953 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 954 | if (unlikely(!out->f_op || !out->f_op->splice_write)) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 955 | return -EINVAL; |
| 956 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 957 | if (unlikely(!(out->f_mode & FMODE_WRITE))) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 958 | return -EBADF; |
| 959 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 960 | ret = rw_verify_area(WRITE, out, ppos, len); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 961 | if (unlikely(ret < 0)) |
| 962 | return ret; |
| 963 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 964 | return out->f_op->splice_write(pipe, out, ppos, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 967 | /* |
| 968 | * Attempt to initiate a splice from a file to a pipe. |
| 969 | */ |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 970 | static long do_splice_to(struct file *in, loff_t *ppos, |
| 971 | struct pipe_inode_info *pipe, size_t len, |
| 972 | unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 973 | { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 974 | int ret; |
| 975 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 976 | if (unlikely(!in->f_op || !in->f_op->splice_read)) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 977 | return -EINVAL; |
| 978 | |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 979 | if (unlikely(!(in->f_mode & FMODE_READ))) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 980 | return -EBADF; |
| 981 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 982 | ret = rw_verify_area(READ, in, ppos, len); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 983 | if (unlikely(ret < 0)) |
| 984 | return ret; |
| 985 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 986 | return in->f_op->splice_read(in, ppos, pipe, len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 989 | /** |
| 990 | * splice_direct_to_actor - splices data directly between two non-pipes |
| 991 | * @in: file to splice from |
| 992 | * @sd: actor information on where to splice to |
| 993 | * @actor: handles the data splicing |
| 994 | * |
| 995 | * Description: |
| 996 | * This is a special case helper to splice directly between two |
| 997 | * points, without requiring an explicit pipe. Internally an allocated |
| 998 | * pipe is cached in the process, and reused during the life time of |
| 999 | * that process. |
| 1000 | * |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1001 | */ |
| 1002 | ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, |
| 1003 | splice_direct_actor *actor) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1004 | { |
| 1005 | struct pipe_inode_info *pipe; |
| 1006 | long ret, bytes; |
| 1007 | umode_t i_mode; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1008 | size_t len; |
| 1009 | int i, flags; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1010 | |
| 1011 | /* |
| 1012 | * We require the input being a regular file, as we don't want to |
| 1013 | * randomly drop data for eg socket -> socket splicing. Use the |
| 1014 | * piped splicing for that! |
| 1015 | */ |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1016 | i_mode = in->f_path.dentry->d_inode->i_mode; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1017 | if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) |
| 1018 | return -EINVAL; |
| 1019 | |
| 1020 | /* |
| 1021 | * neither in nor out is a pipe, setup an internal pipe attached to |
| 1022 | * 'out' and transfer the wanted data from 'in' to 'out' through that |
| 1023 | */ |
| 1024 | pipe = current->splice_pipe; |
Jens Axboe | 49570e9 | 2006-04-11 13:56:09 +0200 | [diff] [blame] | 1025 | if (unlikely(!pipe)) { |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1026 | pipe = alloc_pipe_info(NULL); |
| 1027 | if (!pipe) |
| 1028 | return -ENOMEM; |
| 1029 | |
| 1030 | /* |
| 1031 | * 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] | 1032 | * out of the pipe right after the splice_to_pipe(). So set |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1033 | * PIPE_READERS appropriately. |
| 1034 | */ |
| 1035 | pipe->readers = 1; |
| 1036 | |
| 1037 | current->splice_pipe = pipe; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
Ingo Molnar | 73d62d8 | 2006-04-11 13:57:21 +0200 | [diff] [blame] | 1041 | * Do the splice. |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1042 | */ |
| 1043 | ret = 0; |
| 1044 | bytes = 0; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1045 | len = sd->total_len; |
| 1046 | flags = sd->flags; |
| 1047 | |
| 1048 | /* |
| 1049 | * Don't block on output, we have to drain the direct pipe. |
| 1050 | */ |
| 1051 | sd->flags &= ~SPLICE_F_NONBLOCK; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1052 | |
| 1053 | while (len) { |
| 1054 | size_t read_len, max_read_len; |
| 1055 | |
| 1056 | /* |
| 1057 | * Do at most PIPE_BUFFERS pages worth of transfer: |
| 1058 | */ |
| 1059 | max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE)); |
| 1060 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1061 | ret = do_splice_to(in, &sd->pos, pipe, max_read_len, flags); |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1062 | if (unlikely(ret < 0)) |
| 1063 | goto out_release; |
| 1064 | |
| 1065 | read_len = ret; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1066 | sd->total_len = read_len; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1067 | |
| 1068 | /* |
| 1069 | * NOTE: nonblocking mode only applies to the input. We |
| 1070 | * must not do the output in nonblocking mode as then we |
| 1071 | * could get stuck data in the internal pipe: |
| 1072 | */ |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1073 | ret = actor(pipe, sd); |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1074 | if (unlikely(ret < 0)) |
| 1075 | goto out_release; |
| 1076 | |
| 1077 | bytes += ret; |
| 1078 | len -= ret; |
| 1079 | |
| 1080 | /* |
| 1081 | * In nonblocking mode, if we got back a short read then |
| 1082 | * that was due to either an IO error or due to the |
| 1083 | * pagecache entry not being there. In the IO error case |
| 1084 | * the _next_ splice attempt will produce a clean IO error |
| 1085 | * return value (not a short read), so in both cases it's |
| 1086 | * correct to break out of the loop here: |
| 1087 | */ |
| 1088 | if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len)) |
| 1089 | break; |
| 1090 | } |
| 1091 | |
| 1092 | pipe->nrbufs = pipe->curbuf = 0; |
| 1093 | |
| 1094 | return bytes; |
| 1095 | |
| 1096 | out_release: |
| 1097 | /* |
| 1098 | * If we did an incomplete transfer we must release |
| 1099 | * the pipe buffers in question: |
| 1100 | */ |
| 1101 | for (i = 0; i < PIPE_BUFFERS; i++) { |
| 1102 | struct pipe_buffer *buf = pipe->bufs + i; |
| 1103 | |
| 1104 | if (buf->ops) { |
| 1105 | buf->ops->release(pipe, buf); |
| 1106 | buf->ops = NULL; |
| 1107 | } |
| 1108 | } |
| 1109 | pipe->nrbufs = pipe->curbuf = 0; |
| 1110 | |
| 1111 | /* |
| 1112 | * If we transferred some data, return the number of bytes: |
| 1113 | */ |
| 1114 | if (bytes > 0) |
| 1115 | return bytes; |
| 1116 | |
| 1117 | return ret; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1118 | |
| 1119 | } |
| 1120 | EXPORT_SYMBOL(splice_direct_to_actor); |
| 1121 | |
| 1122 | static int direct_splice_actor(struct pipe_inode_info *pipe, |
| 1123 | struct splice_desc *sd) |
| 1124 | { |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1125 | struct file *file = sd->u.file; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1126 | |
| 1127 | return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags); |
| 1128 | } |
| 1129 | |
Jens Axboe | 932cc6d | 2007-06-21 13:10:21 +0200 | [diff] [blame] | 1130 | /** |
| 1131 | * do_splice_direct - splices data directly between two files |
| 1132 | * @in: file to splice from |
| 1133 | * @ppos: input file offset |
| 1134 | * @out: file to splice to |
| 1135 | * @len: number of bytes to splice |
| 1136 | * @flags: splice modifier flags |
| 1137 | * |
| 1138 | * Description: |
| 1139 | * For use by do_sendfile(). splice can easily emulate sendfile, but |
| 1140 | * doing it in the application would incur an extra system call |
| 1141 | * (splice in + splice out, as compared to just sendfile()). So this helper |
| 1142 | * can splice directly through a process-private pipe. |
| 1143 | * |
| 1144 | */ |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1145 | long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, |
| 1146 | size_t len, unsigned int flags) |
| 1147 | { |
| 1148 | struct splice_desc sd = { |
| 1149 | .len = len, |
| 1150 | .total_len = len, |
| 1151 | .flags = flags, |
| 1152 | .pos = *ppos, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1153 | .u.file = out, |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1154 | }; |
| 1155 | size_t ret; |
| 1156 | |
| 1157 | ret = splice_direct_to_actor(in, &sd, direct_splice_actor); |
| 1158 | *ppos = sd.pos; |
| 1159 | return ret; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1162 | /* |
Jens Axboe | ddac0d3 | 2006-11-04 12:49:32 +0100 | [diff] [blame] | 1163 | * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same |
| 1164 | * location, so checking ->i_pipe is not enough to verify that this is a |
| 1165 | * pipe. |
| 1166 | */ |
| 1167 | static inline struct pipe_inode_info *pipe_info(struct inode *inode) |
| 1168 | { |
| 1169 | if (S_ISFIFO(inode->i_mode)) |
| 1170 | return inode->i_pipe; |
| 1171 | |
| 1172 | return NULL; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
Jens Axboe | 83f9135 | 2006-04-02 23:05:09 +0200 | [diff] [blame] | 1176 | * Determine where to splice to/from. |
| 1177 | */ |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1178 | static long do_splice(struct file *in, loff_t __user *off_in, |
| 1179 | struct file *out, loff_t __user *off_out, |
| 1180 | size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1181 | { |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1182 | struct pipe_inode_info *pipe; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1183 | loff_t offset, *off; |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1184 | long ret; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1185 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1186 | pipe = pipe_info(in->f_path.dentry->d_inode); |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1187 | if (pipe) { |
| 1188 | if (off_in) |
| 1189 | return -ESPIPE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1190 | if (off_out) { |
| 1191 | if (out->f_op->llseek == no_llseek) |
| 1192 | return -EINVAL; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1193 | if (copy_from_user(&offset, off_out, sizeof(loff_t))) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1194 | return -EFAULT; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1195 | off = &offset; |
| 1196 | } else |
| 1197 | off = &out->f_pos; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1198 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1199 | ret = do_splice_from(pipe, out, off, len, flags); |
| 1200 | |
| 1201 | if (off_out && copy_to_user(off_out, off, sizeof(loff_t))) |
| 1202 | ret = -EFAULT; |
| 1203 | |
| 1204 | return ret; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1205 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1206 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1207 | pipe = pipe_info(out->f_path.dentry->d_inode); |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1208 | if (pipe) { |
| 1209 | if (off_out) |
| 1210 | return -ESPIPE; |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1211 | if (off_in) { |
| 1212 | if (in->f_op->llseek == no_llseek) |
| 1213 | return -EINVAL; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1214 | if (copy_from_user(&offset, off_in, sizeof(loff_t))) |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1215 | return -EFAULT; |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 1216 | off = &offset; |
| 1217 | } else |
| 1218 | off = &in->f_pos; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1219 | |
Jens Axboe | a4514eb | 2006-04-19 15:57:05 +0200 | [diff] [blame] | 1220 | ret = do_splice_to(in, off, pipe, len, flags); |
| 1221 | |
| 1222 | if (off_in && copy_to_user(off_in, off, sizeof(loff_t))) |
| 1223 | ret = -EFAULT; |
| 1224 | |
| 1225 | return ret; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1226 | } |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1227 | |
| 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1231 | /* |
| 1232 | * Map an iov into an array of pages and offset/length tupples. With the |
| 1233 | * partial_page structure, we can map several non-contiguous ranges into |
| 1234 | * our ones pages[] map instead of splitting that operation into pieces. |
| 1235 | * Could easily be exported as a generic helper for other users, in which |
| 1236 | * case one would probably want to add a 'max_nr_pages' parameter as well. |
| 1237 | */ |
| 1238 | static int get_iovec_page_array(const struct iovec __user *iov, |
| 1239 | unsigned int nr_vecs, struct page **pages, |
Jens Axboe | 7afa6fd | 2006-05-01 20:02:33 +0200 | [diff] [blame] | 1240 | struct partial_page *partial, int aligned) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1241 | { |
| 1242 | int buffers = 0, error = 0; |
| 1243 | |
| 1244 | /* |
| 1245 | * It's ok to take the mmap_sem for reading, even |
| 1246 | * across a "get_user()". |
| 1247 | */ |
| 1248 | down_read(¤t->mm->mmap_sem); |
| 1249 | |
| 1250 | while (nr_vecs) { |
| 1251 | unsigned long off, npages; |
| 1252 | void __user *base; |
| 1253 | size_t len; |
| 1254 | int i; |
| 1255 | |
| 1256 | /* |
| 1257 | * Get user address base and length for this iovec. |
| 1258 | */ |
| 1259 | error = get_user(base, &iov->iov_base); |
| 1260 | if (unlikely(error)) |
| 1261 | break; |
| 1262 | error = get_user(len, &iov->iov_len); |
| 1263 | if (unlikely(error)) |
| 1264 | break; |
| 1265 | |
| 1266 | /* |
| 1267 | * Sanity check this iovec. 0 read succeeds. |
| 1268 | */ |
| 1269 | if (unlikely(!len)) |
| 1270 | break; |
| 1271 | error = -EFAULT; |
| 1272 | if (unlikely(!base)) |
| 1273 | break; |
| 1274 | |
| 1275 | /* |
| 1276 | * Get this base offset and number of pages, then map |
| 1277 | * in the user pages. |
| 1278 | */ |
| 1279 | off = (unsigned long) base & ~PAGE_MASK; |
Jens Axboe | 7afa6fd | 2006-05-01 20:02:33 +0200 | [diff] [blame] | 1280 | |
| 1281 | /* |
| 1282 | * If asked for alignment, the offset must be zero and the |
| 1283 | * length a multiple of the PAGE_SIZE. |
| 1284 | */ |
| 1285 | error = -EINVAL; |
| 1286 | if (aligned && (off || len & ~PAGE_MASK)) |
| 1287 | break; |
| 1288 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1289 | npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1290 | if (npages > PIPE_BUFFERS - buffers) |
| 1291 | npages = PIPE_BUFFERS - buffers; |
| 1292 | |
| 1293 | error = get_user_pages(current, current->mm, |
| 1294 | (unsigned long) base, npages, 0, 0, |
| 1295 | &pages[buffers], NULL); |
| 1296 | |
| 1297 | if (unlikely(error <= 0)) |
| 1298 | break; |
| 1299 | |
| 1300 | /* |
| 1301 | * Fill this contiguous range into the partial page map. |
| 1302 | */ |
| 1303 | for (i = 0; i < error; i++) { |
Jens Axboe | 7591489 | 2006-05-02 12:57:18 +0200 | [diff] [blame] | 1304 | const int plen = min_t(size_t, len, PAGE_SIZE - off); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1305 | |
| 1306 | partial[buffers].offset = off; |
| 1307 | partial[buffers].len = plen; |
| 1308 | |
| 1309 | off = 0; |
| 1310 | len -= plen; |
| 1311 | buffers++; |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * We didn't complete this iov, stop here since it probably |
| 1316 | * means we have to move some of this into a pipe to |
| 1317 | * be able to continue. |
| 1318 | */ |
| 1319 | if (len) |
| 1320 | break; |
| 1321 | |
| 1322 | /* |
| 1323 | * Don't continue if we mapped fewer pages than we asked for, |
| 1324 | * or if we mapped the max number of pages that we have |
| 1325 | * room for. |
| 1326 | */ |
| 1327 | if (error < npages || buffers == PIPE_BUFFERS) |
| 1328 | break; |
| 1329 | |
| 1330 | nr_vecs--; |
| 1331 | iov++; |
| 1332 | } |
| 1333 | |
| 1334 | up_read(¤t->mm->mmap_sem); |
| 1335 | |
| 1336 | if (buffers) |
| 1337 | return buffers; |
| 1338 | |
| 1339 | return error; |
| 1340 | } |
| 1341 | |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1342 | static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
| 1343 | struct splice_desc *sd) |
| 1344 | { |
| 1345 | char *src; |
| 1346 | int ret; |
| 1347 | |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 1348 | ret = buf->ops->confirm(pipe, buf); |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1349 | if (unlikely(ret)) |
| 1350 | return ret; |
| 1351 | |
| 1352 | /* |
| 1353 | * See if we can use the atomic maps, by prefaulting in the |
| 1354 | * pages and doing an atomic copy |
| 1355 | */ |
| 1356 | if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) { |
| 1357 | src = buf->ops->map(pipe, buf, 1); |
| 1358 | ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset, |
| 1359 | sd->len); |
| 1360 | buf->ops->unmap(pipe, buf, src); |
| 1361 | if (!ret) { |
| 1362 | ret = sd->len; |
| 1363 | goto out; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * No dice, use slow non-atomic map and copy |
| 1369 | */ |
| 1370 | src = buf->ops->map(pipe, buf, 0); |
| 1371 | |
| 1372 | ret = sd->len; |
| 1373 | if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len)) |
| 1374 | ret = -EFAULT; |
| 1375 | |
| 1376 | out: |
| 1377 | if (ret > 0) |
| 1378 | sd->u.userptr += ret; |
| 1379 | buf->ops->unmap(pipe, buf, src); |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * For lack of a better implementation, implement vmsplice() to userspace |
| 1385 | * as a simple copy of the pipes pages to the user iov. |
| 1386 | */ |
| 1387 | static long vmsplice_to_user(struct file *file, const struct iovec __user *iov, |
| 1388 | unsigned long nr_segs, unsigned int flags) |
| 1389 | { |
| 1390 | struct pipe_inode_info *pipe; |
| 1391 | struct splice_desc sd; |
| 1392 | ssize_t size; |
| 1393 | int error; |
| 1394 | long ret; |
| 1395 | |
| 1396 | pipe = pipe_info(file->f_path.dentry->d_inode); |
| 1397 | if (!pipe) |
| 1398 | return -EBADF; |
| 1399 | |
| 1400 | if (pipe->inode) |
| 1401 | mutex_lock(&pipe->inode->i_mutex); |
| 1402 | |
| 1403 | error = ret = 0; |
| 1404 | while (nr_segs) { |
| 1405 | void __user *base; |
| 1406 | size_t len; |
| 1407 | |
| 1408 | /* |
| 1409 | * Get user address base and length for this iovec. |
| 1410 | */ |
| 1411 | error = get_user(base, &iov->iov_base); |
| 1412 | if (unlikely(error)) |
| 1413 | break; |
| 1414 | error = get_user(len, &iov->iov_len); |
| 1415 | if (unlikely(error)) |
| 1416 | break; |
| 1417 | |
| 1418 | /* |
| 1419 | * Sanity check this iovec. 0 read succeeds. |
| 1420 | */ |
| 1421 | if (unlikely(!len)) |
| 1422 | break; |
| 1423 | if (unlikely(!base)) { |
| 1424 | error = -EFAULT; |
| 1425 | break; |
| 1426 | } |
| 1427 | |
| 1428 | sd.len = 0; |
| 1429 | sd.total_len = len; |
| 1430 | sd.flags = flags; |
| 1431 | sd.u.userptr = base; |
| 1432 | sd.pos = 0; |
| 1433 | |
| 1434 | size = __splice_from_pipe(pipe, &sd, pipe_to_user); |
| 1435 | if (size < 0) { |
| 1436 | if (!ret) |
| 1437 | ret = size; |
| 1438 | |
| 1439 | break; |
| 1440 | } |
| 1441 | |
| 1442 | ret += size; |
| 1443 | |
| 1444 | if (size < len) |
| 1445 | break; |
| 1446 | |
| 1447 | nr_segs--; |
| 1448 | iov++; |
| 1449 | } |
| 1450 | |
| 1451 | if (pipe->inode) |
| 1452 | mutex_unlock(&pipe->inode->i_mutex); |
| 1453 | |
| 1454 | if (!ret) |
| 1455 | ret = error; |
| 1456 | |
| 1457 | return ret; |
| 1458 | } |
| 1459 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1460 | /* |
| 1461 | * vmsplice splices a user address range into a pipe. It can be thought of |
| 1462 | * as splice-from-memory, where the regular splice is splice-from-file (or |
| 1463 | * to file). In both cases the output is a pipe, naturally. |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1464 | */ |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1465 | static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov, |
| 1466 | unsigned long nr_segs, unsigned int flags) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1467 | { |
Jens Axboe | ddac0d3 | 2006-11-04 12:49:32 +0100 | [diff] [blame] | 1468 | struct pipe_inode_info *pipe; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1469 | struct page *pages[PIPE_BUFFERS]; |
| 1470 | struct partial_page partial[PIPE_BUFFERS]; |
| 1471 | struct splice_pipe_desc spd = { |
| 1472 | .pages = pages, |
| 1473 | .partial = partial, |
| 1474 | .flags = flags, |
| 1475 | .ops = &user_page_pipe_buf_ops, |
| 1476 | }; |
| 1477 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1478 | pipe = pipe_info(file->f_path.dentry->d_inode); |
Jens Axboe | ddac0d3 | 2006-11-04 12:49:32 +0100 | [diff] [blame] | 1479 | if (!pipe) |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1480 | return -EBADF; |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1481 | |
Jens Axboe | 7afa6fd | 2006-05-01 20:02:33 +0200 | [diff] [blame] | 1482 | spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial, |
| 1483 | flags & SPLICE_F_GIFT); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1484 | if (spd.nr_pages <= 0) |
| 1485 | return spd.nr_pages; |
| 1486 | |
Jens Axboe | 00522fb | 2006-04-26 14:39:29 +0200 | [diff] [blame] | 1487 | return splice_to_pipe(pipe, &spd); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1490 | /* |
| 1491 | * Note that vmsplice only really supports true splicing _from_ user memory |
| 1492 | * to a pipe, not the other way around. Splicing from user memory is a simple |
| 1493 | * operation that can be supported without any funky alignment restrictions |
| 1494 | * or nasty vm tricks. We simply map in the user memory and fill them into |
| 1495 | * a pipe. The reverse isn't quite as easy, though. There are two possible |
| 1496 | * solutions for that: |
| 1497 | * |
| 1498 | * - memcpy() the data internally, at which point we might as well just |
| 1499 | * do a regular read() on the buffer anyway. |
| 1500 | * - Lots of nasty vm tricks, that are neither fast nor flexible (it |
| 1501 | * has restriction limitations on both ends of the pipe). |
| 1502 | * |
| 1503 | * Currently we punt and implement it as a normal copy, see pipe_to_user(). |
| 1504 | * |
| 1505 | */ |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1506 | asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov, |
| 1507 | unsigned long nr_segs, unsigned int flags) |
| 1508 | { |
| 1509 | struct file *file; |
| 1510 | long error; |
| 1511 | int fput; |
| 1512 | |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1513 | if (unlikely(nr_segs > UIO_MAXIOV)) |
| 1514 | return -EINVAL; |
| 1515 | else if (unlikely(!nr_segs)) |
| 1516 | return 0; |
| 1517 | |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1518 | error = -EBADF; |
| 1519 | file = fget_light(fd, &fput); |
| 1520 | if (file) { |
| 1521 | if (file->f_mode & FMODE_WRITE) |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1522 | error = vmsplice_to_pipe(file, iov, nr_segs, flags); |
| 1523 | else if (file->f_mode & FMODE_READ) |
| 1524 | error = vmsplice_to_user(file, iov, nr_segs, flags); |
Jens Axboe | 912d35f | 2006-04-26 10:59:21 +0200 | [diff] [blame] | 1525 | |
| 1526 | fput_light(file, fput); |
| 1527 | } |
| 1528 | |
| 1529 | return error; |
| 1530 | } |
| 1531 | |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1532 | asmlinkage long sys_splice(int fd_in, loff_t __user *off_in, |
| 1533 | int fd_out, loff_t __user *off_out, |
| 1534 | size_t len, unsigned int flags) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1535 | { |
| 1536 | long error; |
| 1537 | struct file *in, *out; |
| 1538 | int fput_in, fput_out; |
| 1539 | |
| 1540 | if (unlikely(!len)) |
| 1541 | return 0; |
| 1542 | |
| 1543 | error = -EBADF; |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1544 | in = fget_light(fd_in, &fput_in); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1545 | if (in) { |
| 1546 | if (in->f_mode & FMODE_READ) { |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1547 | out = fget_light(fd_out, &fput_out); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1548 | if (out) { |
| 1549 | if (out->f_mode & FMODE_WRITE) |
Ingo Molnar | 529565d | 2006-04-10 15:18:58 +0200 | [diff] [blame] | 1550 | error = do_splice(in, off_in, |
| 1551 | out, off_out, |
| 1552 | len, flags); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 1553 | fput_light(out, fput_out); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | fput_light(in, fput_in); |
| 1558 | } |
| 1559 | |
| 1560 | return error; |
| 1561 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1562 | |
| 1563 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1564 | * Make sure there's data to read. Wait for input if we can, otherwise |
| 1565 | * return an appropriate error. |
| 1566 | */ |
| 1567 | static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
| 1568 | { |
| 1569 | int ret; |
| 1570 | |
| 1571 | /* |
| 1572 | * Check ->nrbufs without the inode lock first. This function |
| 1573 | * is speculative anyways, so missing one is ok. |
| 1574 | */ |
| 1575 | if (pipe->nrbufs) |
| 1576 | return 0; |
| 1577 | |
| 1578 | ret = 0; |
| 1579 | mutex_lock(&pipe->inode->i_mutex); |
| 1580 | |
| 1581 | while (!pipe->nrbufs) { |
| 1582 | if (signal_pending(current)) { |
| 1583 | ret = -ERESTARTSYS; |
| 1584 | break; |
| 1585 | } |
| 1586 | if (!pipe->writers) |
| 1587 | break; |
| 1588 | if (!pipe->waiting_writers) { |
| 1589 | if (flags & SPLICE_F_NONBLOCK) { |
| 1590 | ret = -EAGAIN; |
| 1591 | break; |
| 1592 | } |
| 1593 | } |
| 1594 | pipe_wait(pipe); |
| 1595 | } |
| 1596 | |
| 1597 | mutex_unlock(&pipe->inode->i_mutex); |
| 1598 | return ret; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * Make sure there's writeable room. Wait for room if we can, otherwise |
| 1603 | * return an appropriate error. |
| 1604 | */ |
| 1605 | static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
| 1606 | { |
| 1607 | int ret; |
| 1608 | |
| 1609 | /* |
| 1610 | * Check ->nrbufs without the inode lock first. This function |
| 1611 | * is speculative anyways, so missing one is ok. |
| 1612 | */ |
| 1613 | if (pipe->nrbufs < PIPE_BUFFERS) |
| 1614 | return 0; |
| 1615 | |
| 1616 | ret = 0; |
| 1617 | mutex_lock(&pipe->inode->i_mutex); |
| 1618 | |
| 1619 | while (pipe->nrbufs >= PIPE_BUFFERS) { |
| 1620 | if (!pipe->readers) { |
| 1621 | send_sig(SIGPIPE, current, 0); |
| 1622 | ret = -EPIPE; |
| 1623 | break; |
| 1624 | } |
| 1625 | if (flags & SPLICE_F_NONBLOCK) { |
| 1626 | ret = -EAGAIN; |
| 1627 | break; |
| 1628 | } |
| 1629 | if (signal_pending(current)) { |
| 1630 | ret = -ERESTARTSYS; |
| 1631 | break; |
| 1632 | } |
| 1633 | pipe->waiting_writers++; |
| 1634 | pipe_wait(pipe); |
| 1635 | pipe->waiting_writers--; |
| 1636 | } |
| 1637 | |
| 1638 | mutex_unlock(&pipe->inode->i_mutex); |
| 1639 | return ret; |
| 1640 | } |
| 1641 | |
| 1642 | /* |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1643 | * Link contents of ipipe to opipe. |
| 1644 | */ |
| 1645 | static int link_pipe(struct pipe_inode_info *ipipe, |
| 1646 | struct pipe_inode_info *opipe, |
| 1647 | size_t len, unsigned int flags) |
| 1648 | { |
| 1649 | struct pipe_buffer *ibuf, *obuf; |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1650 | int ret = 0, i = 0, nbuf; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1651 | |
| 1652 | /* |
| 1653 | * Potential ABBA deadlock, work around it by ordering lock |
| 1654 | * grabbing by inode address. Otherwise two different processes |
| 1655 | * could deadlock (one doing tee from A -> B, the other from B -> A). |
| 1656 | */ |
Mark Fasheh | 62752ee | 2006-10-17 10:31:38 +0200 | [diff] [blame] | 1657 | inode_double_lock(ipipe->inode, opipe->inode); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1658 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1659 | do { |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1660 | if (!opipe->readers) { |
| 1661 | send_sig(SIGPIPE, current, 0); |
| 1662 | if (!ret) |
| 1663 | ret = -EPIPE; |
| 1664 | break; |
| 1665 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1666 | |
| 1667 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1668 | * If we have iterated all input buffers or ran out of |
| 1669 | * output room, break. |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1670 | */ |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1671 | if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1672 | break; |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1673 | |
| 1674 | ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1)); |
| 1675 | nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1); |
| 1676 | |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1677 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1678 | * Get a reference to this pipe buffer, |
| 1679 | * so we can copy the contents over. |
Jens Axboe | 2a27250e | 2006-04-19 15:56:40 +0200 | [diff] [blame] | 1680 | */ |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1681 | ibuf->ops->get(ipipe, ibuf); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1682 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1683 | obuf = opipe->bufs + nbuf; |
| 1684 | *obuf = *ibuf; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1685 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1686 | /* |
| 1687 | * Don't inherit the gift flag, we need to |
| 1688 | * prevent multiple steals of this page. |
| 1689 | */ |
| 1690 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
| 1691 | |
| 1692 | if (obuf->len > len) |
| 1693 | obuf->len = len; |
| 1694 | |
| 1695 | opipe->nrbufs++; |
| 1696 | ret += obuf->len; |
| 1697 | len -= obuf->len; |
| 1698 | i++; |
| 1699 | } while (len); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1700 | |
Mark Fasheh | 62752ee | 2006-10-17 10:31:38 +0200 | [diff] [blame] | 1701 | inode_double_unlock(ipipe->inode, opipe->inode); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1702 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1703 | /* |
| 1704 | * If we put data in the output pipe, wakeup any potential readers. |
| 1705 | */ |
| 1706 | if (ret > 0) { |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1707 | smp_mb(); |
| 1708 | if (waitqueue_active(&opipe->wait)) |
| 1709 | wake_up_interruptible(&opipe->wait); |
| 1710 | kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); |
| 1711 | } |
| 1712 | |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * This is a tee(1) implementation that works on pipes. It doesn't copy |
| 1718 | * any data, it simply references the 'in' pages on the 'out' pipe. |
| 1719 | * The 'flags' used are the SPLICE_F_* variants, currently the only |
| 1720 | * applicable one is SPLICE_F_NONBLOCK. |
| 1721 | */ |
| 1722 | static long do_tee(struct file *in, struct file *out, size_t len, |
| 1723 | unsigned int flags) |
| 1724 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1725 | struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode); |
| 1726 | struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode); |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1727 | int ret = -EINVAL; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1728 | |
| 1729 | /* |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1730 | * Duplicate the contents of ipipe to opipe without actually |
| 1731 | * copying the data. |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1732 | */ |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1733 | if (ipipe && opipe && ipipe != opipe) { |
| 1734 | /* |
| 1735 | * Keep going, unless we encounter an error. The ipipe/opipe |
| 1736 | * ordering doesn't really matter. |
| 1737 | */ |
| 1738 | ret = link_ipipe_prep(ipipe, flags); |
| 1739 | if (!ret) { |
| 1740 | ret = link_opipe_prep(opipe, flags); |
| 1741 | if (!ret) { |
| 1742 | ret = link_pipe(ipipe, opipe, len, flags); |
| 1743 | if (!ret && (flags & SPLICE_F_NONBLOCK)) |
| 1744 | ret = -EAGAIN; |
| 1745 | } |
| 1746 | } |
| 1747 | } |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1748 | |
Jens Axboe | aadd06e | 2006-07-10 11:00:01 +0200 | [diff] [blame] | 1749 | return ret; |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags) |
| 1753 | { |
| 1754 | struct file *in; |
| 1755 | int error, fput_in; |
| 1756 | |
| 1757 | if (unlikely(!len)) |
| 1758 | return 0; |
| 1759 | |
| 1760 | error = -EBADF; |
| 1761 | in = fget_light(fdin, &fput_in); |
| 1762 | if (in) { |
| 1763 | if (in->f_mode & FMODE_READ) { |
| 1764 | int fput_out; |
| 1765 | struct file *out = fget_light(fdout, &fput_out); |
| 1766 | |
| 1767 | if (out) { |
| 1768 | if (out->f_mode & FMODE_WRITE) |
| 1769 | error = do_tee(in, out, len, flags); |
| 1770 | fput_light(out, fput_out); |
| 1771 | } |
| 1772 | } |
| 1773 | fput_light(in, fput_in); |
| 1774 | } |
| 1775 | |
| 1776 | return error; |
| 1777 | } |