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