Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 1 | #include <linux/export.h> |
Christoph Hellwig | 2f8b544 | 2016-11-01 07:40:13 -0600 | [diff] [blame] | 2 | #include <linux/bvec.h> |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 3 | #include <linux/uio.h> |
| 4 | #include <linux/pagemap.h> |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 5 | #include <linux/slab.h> |
| 6 | #include <linux/vmalloc.h> |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 7 | #include <linux/splice.h> |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 8 | #include <net/checksum.h> |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 9 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 10 | #define PIPE_PARANOIA /* for now */ |
| 11 | |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 12 | #define iterate_iovec(i, n, __v, __p, skip, STEP) { \ |
| 13 | size_t left; \ |
| 14 | size_t wanted = n; \ |
| 15 | __p = i->iov; \ |
| 16 | __v.iov_len = min(n, __p->iov_len - skip); \ |
| 17 | if (likely(__v.iov_len)) { \ |
| 18 | __v.iov_base = __p->iov_base + skip; \ |
| 19 | left = (STEP); \ |
| 20 | __v.iov_len -= left; \ |
| 21 | skip += __v.iov_len; \ |
| 22 | n -= __v.iov_len; \ |
| 23 | } else { \ |
| 24 | left = 0; \ |
| 25 | } \ |
| 26 | while (unlikely(!left && n)) { \ |
| 27 | __p++; \ |
| 28 | __v.iov_len = min(n, __p->iov_len); \ |
| 29 | if (unlikely(!__v.iov_len)) \ |
| 30 | continue; \ |
| 31 | __v.iov_base = __p->iov_base; \ |
| 32 | left = (STEP); \ |
| 33 | __v.iov_len -= left; \ |
| 34 | skip = __v.iov_len; \ |
| 35 | n -= __v.iov_len; \ |
| 36 | } \ |
| 37 | n = wanted - n; \ |
| 38 | } |
| 39 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 40 | #define iterate_kvec(i, n, __v, __p, skip, STEP) { \ |
| 41 | size_t wanted = n; \ |
| 42 | __p = i->kvec; \ |
| 43 | __v.iov_len = min(n, __p->iov_len - skip); \ |
| 44 | if (likely(__v.iov_len)) { \ |
| 45 | __v.iov_base = __p->iov_base + skip; \ |
| 46 | (void)(STEP); \ |
| 47 | skip += __v.iov_len; \ |
| 48 | n -= __v.iov_len; \ |
| 49 | } \ |
| 50 | while (unlikely(n)) { \ |
| 51 | __p++; \ |
| 52 | __v.iov_len = min(n, __p->iov_len); \ |
| 53 | if (unlikely(!__v.iov_len)) \ |
| 54 | continue; \ |
| 55 | __v.iov_base = __p->iov_base; \ |
| 56 | (void)(STEP); \ |
| 57 | skip = __v.iov_len; \ |
| 58 | n -= __v.iov_len; \ |
| 59 | } \ |
| 60 | n = wanted; \ |
| 61 | } |
| 62 | |
Ming Lei | 1bdc76a | 2016-05-30 21:34:32 +0800 | [diff] [blame] | 63 | #define iterate_bvec(i, n, __v, __bi, skip, STEP) { \ |
| 64 | struct bvec_iter __start; \ |
| 65 | __start.bi_size = n; \ |
| 66 | __start.bi_bvec_done = skip; \ |
| 67 | __start.bi_idx = 0; \ |
| 68 | for_each_bvec(__v, i->bvec, __bi, __start) { \ |
| 69 | if (!__v.bv_len) \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 70 | continue; \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 71 | (void)(STEP); \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 72 | } \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 75 | #define iterate_all_kinds(i, n, v, I, B, K) { \ |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 76 | if (likely(n)) { \ |
| 77 | size_t skip = i->iov_offset; \ |
| 78 | if (unlikely(i->type & ITER_BVEC)) { \ |
| 79 | struct bio_vec v; \ |
| 80 | struct bvec_iter __bi; \ |
| 81 | iterate_bvec(i, n, v, __bi, skip, (B)) \ |
| 82 | } else if (unlikely(i->type & ITER_KVEC)) { \ |
| 83 | const struct kvec *kvec; \ |
| 84 | struct kvec v; \ |
| 85 | iterate_kvec(i, n, v, kvec, skip, (K)) \ |
| 86 | } else { \ |
| 87 | const struct iovec *iov; \ |
| 88 | struct iovec v; \ |
| 89 | iterate_iovec(i, n, v, iov, skip, (I)) \ |
| 90 | } \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 91 | } \ |
| 92 | } |
| 93 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 94 | #define iterate_and_advance(i, n, v, I, B, K) { \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 95 | if (unlikely(i->count < n)) \ |
| 96 | n = i->count; \ |
Al Viro | 19f1845 | 2016-05-25 17:36:19 -0400 | [diff] [blame] | 97 | if (i->count) { \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 98 | size_t skip = i->iov_offset; \ |
| 99 | if (unlikely(i->type & ITER_BVEC)) { \ |
Ming Lei | 1bdc76a | 2016-05-30 21:34:32 +0800 | [diff] [blame] | 100 | const struct bio_vec *bvec = i->bvec; \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 101 | struct bio_vec v; \ |
Ming Lei | 1bdc76a | 2016-05-30 21:34:32 +0800 | [diff] [blame] | 102 | struct bvec_iter __bi; \ |
| 103 | iterate_bvec(i, n, v, __bi, skip, (B)) \ |
| 104 | i->bvec = __bvec_iter_bvec(i->bvec, __bi); \ |
| 105 | i->nr_segs -= i->bvec - bvec; \ |
| 106 | skip = __bi.bi_bvec_done; \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 107 | } else if (unlikely(i->type & ITER_KVEC)) { \ |
| 108 | const struct kvec *kvec; \ |
| 109 | struct kvec v; \ |
| 110 | iterate_kvec(i, n, v, kvec, skip, (K)) \ |
| 111 | if (skip == kvec->iov_len) { \ |
| 112 | kvec++; \ |
| 113 | skip = 0; \ |
| 114 | } \ |
| 115 | i->nr_segs -= kvec - i->kvec; \ |
| 116 | i->kvec = kvec; \ |
| 117 | } else { \ |
| 118 | const struct iovec *iov; \ |
| 119 | struct iovec v; \ |
| 120 | iterate_iovec(i, n, v, iov, skip, (I)) \ |
| 121 | if (skip == iov->iov_len) { \ |
| 122 | iov++; \ |
| 123 | skip = 0; \ |
| 124 | } \ |
| 125 | i->nr_segs -= iov - i->iov; \ |
| 126 | i->iov = iov; \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 127 | } \ |
Al Viro | dd254f5 | 2016-05-09 11:54:48 -0400 | [diff] [blame] | 128 | i->count -= n; \ |
| 129 | i->iov_offset = skip; \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 130 | } \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 133 | static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes, |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 134 | struct iov_iter *i) |
| 135 | { |
| 136 | size_t skip, copy, left, wanted; |
| 137 | const struct iovec *iov; |
| 138 | char __user *buf; |
| 139 | void *kaddr, *from; |
| 140 | |
| 141 | if (unlikely(bytes > i->count)) |
| 142 | bytes = i->count; |
| 143 | |
| 144 | if (unlikely(!bytes)) |
| 145 | return 0; |
| 146 | |
| 147 | wanted = bytes; |
| 148 | iov = i->iov; |
| 149 | skip = i->iov_offset; |
| 150 | buf = iov->iov_base + skip; |
| 151 | copy = min(bytes, iov->iov_len - skip); |
| 152 | |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 153 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) { |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 154 | kaddr = kmap_atomic(page); |
| 155 | from = kaddr + offset; |
| 156 | |
| 157 | /* first chunk, usually the only one */ |
| 158 | left = __copy_to_user_inatomic(buf, from, copy); |
| 159 | copy -= left; |
| 160 | skip += copy; |
| 161 | from += copy; |
| 162 | bytes -= copy; |
| 163 | |
| 164 | while (unlikely(!left && bytes)) { |
| 165 | iov++; |
| 166 | buf = iov->iov_base; |
| 167 | copy = min(bytes, iov->iov_len); |
| 168 | left = __copy_to_user_inatomic(buf, from, copy); |
| 169 | copy -= left; |
| 170 | skip = copy; |
| 171 | from += copy; |
| 172 | bytes -= copy; |
| 173 | } |
| 174 | if (likely(!bytes)) { |
| 175 | kunmap_atomic(kaddr); |
| 176 | goto done; |
| 177 | } |
| 178 | offset = from - kaddr; |
| 179 | buf += copy; |
| 180 | kunmap_atomic(kaddr); |
| 181 | copy = min(bytes, iov->iov_len - skip); |
| 182 | } |
| 183 | /* Too bad - revert to non-atomic kmap */ |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 184 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 185 | kaddr = kmap(page); |
| 186 | from = kaddr + offset; |
| 187 | left = __copy_to_user(buf, from, copy); |
| 188 | copy -= left; |
| 189 | skip += copy; |
| 190 | from += copy; |
| 191 | bytes -= copy; |
| 192 | while (unlikely(!left && bytes)) { |
| 193 | iov++; |
| 194 | buf = iov->iov_base; |
| 195 | copy = min(bytes, iov->iov_len); |
| 196 | left = __copy_to_user(buf, from, copy); |
| 197 | copy -= left; |
| 198 | skip = copy; |
| 199 | from += copy; |
| 200 | bytes -= copy; |
| 201 | } |
| 202 | kunmap(page); |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 203 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 204 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 205 | if (skip == iov->iov_len) { |
| 206 | iov++; |
| 207 | skip = 0; |
| 208 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 209 | i->count -= wanted - bytes; |
| 210 | i->nr_segs -= iov - i->iov; |
| 211 | i->iov = iov; |
| 212 | i->iov_offset = skip; |
| 213 | return wanted - bytes; |
| 214 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 215 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 216 | static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes, |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 217 | struct iov_iter *i) |
| 218 | { |
| 219 | size_t skip, copy, left, wanted; |
| 220 | const struct iovec *iov; |
| 221 | char __user *buf; |
| 222 | void *kaddr, *to; |
| 223 | |
| 224 | if (unlikely(bytes > i->count)) |
| 225 | bytes = i->count; |
| 226 | |
| 227 | if (unlikely(!bytes)) |
| 228 | return 0; |
| 229 | |
| 230 | wanted = bytes; |
| 231 | iov = i->iov; |
| 232 | skip = i->iov_offset; |
| 233 | buf = iov->iov_base + skip; |
| 234 | copy = min(bytes, iov->iov_len - skip); |
| 235 | |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 236 | if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) { |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 237 | kaddr = kmap_atomic(page); |
| 238 | to = kaddr + offset; |
| 239 | |
| 240 | /* first chunk, usually the only one */ |
| 241 | left = __copy_from_user_inatomic(to, buf, copy); |
| 242 | copy -= left; |
| 243 | skip += copy; |
| 244 | to += copy; |
| 245 | bytes -= copy; |
| 246 | |
| 247 | while (unlikely(!left && bytes)) { |
| 248 | iov++; |
| 249 | buf = iov->iov_base; |
| 250 | copy = min(bytes, iov->iov_len); |
| 251 | left = __copy_from_user_inatomic(to, buf, copy); |
| 252 | copy -= left; |
| 253 | skip = copy; |
| 254 | to += copy; |
| 255 | bytes -= copy; |
| 256 | } |
| 257 | if (likely(!bytes)) { |
| 258 | kunmap_atomic(kaddr); |
| 259 | goto done; |
| 260 | } |
| 261 | offset = to - kaddr; |
| 262 | buf += copy; |
| 263 | kunmap_atomic(kaddr); |
| 264 | copy = min(bytes, iov->iov_len - skip); |
| 265 | } |
| 266 | /* Too bad - revert to non-atomic kmap */ |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 267 | |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 268 | kaddr = kmap(page); |
| 269 | to = kaddr + offset; |
| 270 | left = __copy_from_user(to, buf, copy); |
| 271 | copy -= left; |
| 272 | skip += copy; |
| 273 | to += copy; |
| 274 | bytes -= copy; |
| 275 | while (unlikely(!left && bytes)) { |
| 276 | iov++; |
| 277 | buf = iov->iov_base; |
| 278 | copy = min(bytes, iov->iov_len); |
| 279 | left = __copy_from_user(to, buf, copy); |
| 280 | copy -= left; |
| 281 | skip = copy; |
| 282 | to += copy; |
| 283 | bytes -= copy; |
| 284 | } |
| 285 | kunmap(page); |
Mikulas Patocka | 3fa6c50 | 2016-07-28 15:48:50 -0700 | [diff] [blame] | 286 | |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 287 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 288 | if (skip == iov->iov_len) { |
| 289 | iov++; |
| 290 | skip = 0; |
| 291 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 292 | i->count -= wanted - bytes; |
| 293 | i->nr_segs -= iov - i->iov; |
| 294 | i->iov = iov; |
| 295 | i->iov_offset = skip; |
| 296 | return wanted - bytes; |
| 297 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 298 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 299 | #ifdef PIPE_PARANOIA |
| 300 | static bool sanity(const struct iov_iter *i) |
| 301 | { |
| 302 | struct pipe_inode_info *pipe = i->pipe; |
| 303 | int idx = i->idx; |
| 304 | int next = pipe->curbuf + pipe->nrbufs; |
| 305 | if (i->iov_offset) { |
| 306 | struct pipe_buffer *p; |
| 307 | if (unlikely(!pipe->nrbufs)) |
| 308 | goto Bad; // pipe must be non-empty |
| 309 | if (unlikely(idx != ((next - 1) & (pipe->buffers - 1)))) |
| 310 | goto Bad; // must be at the last buffer... |
| 311 | |
| 312 | p = &pipe->bufs[idx]; |
| 313 | if (unlikely(p->offset + p->len != i->iov_offset)) |
| 314 | goto Bad; // ... at the end of segment |
| 315 | } else { |
| 316 | if (idx != (next & (pipe->buffers - 1))) |
| 317 | goto Bad; // must be right after the last buffer |
| 318 | } |
| 319 | return true; |
| 320 | Bad: |
| 321 | printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset); |
| 322 | printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n", |
| 323 | pipe->curbuf, pipe->nrbufs, pipe->buffers); |
| 324 | for (idx = 0; idx < pipe->buffers; idx++) |
| 325 | printk(KERN_ERR "[%p %p %d %d]\n", |
| 326 | pipe->bufs[idx].ops, |
| 327 | pipe->bufs[idx].page, |
| 328 | pipe->bufs[idx].offset, |
| 329 | pipe->bufs[idx].len); |
| 330 | WARN_ON(1); |
| 331 | return false; |
| 332 | } |
| 333 | #else |
| 334 | #define sanity(i) true |
| 335 | #endif |
| 336 | |
| 337 | static inline int next_idx(int idx, struct pipe_inode_info *pipe) |
| 338 | { |
| 339 | return (idx + 1) & (pipe->buffers - 1); |
| 340 | } |
| 341 | |
| 342 | static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, |
| 343 | struct iov_iter *i) |
| 344 | { |
| 345 | struct pipe_inode_info *pipe = i->pipe; |
| 346 | struct pipe_buffer *buf; |
| 347 | size_t off; |
| 348 | int idx; |
| 349 | |
| 350 | if (unlikely(bytes > i->count)) |
| 351 | bytes = i->count; |
| 352 | |
| 353 | if (unlikely(!bytes)) |
| 354 | return 0; |
| 355 | |
| 356 | if (!sanity(i)) |
| 357 | return 0; |
| 358 | |
| 359 | off = i->iov_offset; |
| 360 | idx = i->idx; |
| 361 | buf = &pipe->bufs[idx]; |
| 362 | if (off) { |
| 363 | if (offset == off && buf->page == page) { |
| 364 | /* merge with the last one */ |
| 365 | buf->len += bytes; |
| 366 | i->iov_offset += bytes; |
| 367 | goto out; |
| 368 | } |
| 369 | idx = next_idx(idx, pipe); |
| 370 | buf = &pipe->bufs[idx]; |
| 371 | } |
| 372 | if (idx == pipe->curbuf && pipe->nrbufs) |
| 373 | return 0; |
| 374 | pipe->nrbufs++; |
| 375 | buf->ops = &page_cache_pipe_buf_ops; |
| 376 | get_page(buf->page = page); |
| 377 | buf->offset = offset; |
| 378 | buf->len = bytes; |
| 379 | i->iov_offset = offset + bytes; |
| 380 | i->idx = idx; |
| 381 | out: |
| 382 | i->count -= bytes; |
| 383 | return bytes; |
| 384 | } |
| 385 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 386 | /* |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 387 | * Fault in one or more iovecs of the given iov_iter, to a maximum length of |
| 388 | * bytes. For each iovec, fault in each page that constitutes the iovec. |
| 389 | * |
| 390 | * Return 0 on success, or non-zero if the memory could not be accessed (i.e. |
| 391 | * because it is an invalid address). |
| 392 | */ |
Al Viro | d4690f1 | 2016-09-16 00:11:45 +0100 | [diff] [blame] | 393 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 394 | { |
| 395 | size_t skip = i->iov_offset; |
| 396 | const struct iovec *iov; |
| 397 | int err; |
| 398 | struct iovec v; |
| 399 | |
| 400 | if (!(i->type & (ITER_BVEC|ITER_KVEC))) { |
| 401 | iterate_iovec(i, bytes, v, iov, skip, ({ |
Al Viro | 4bce9f6e | 2016-09-17 18:02:44 -0400 | [diff] [blame] | 402 | err = fault_in_pages_readable(v.iov_base, v.iov_len); |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 403 | if (unlikely(err)) |
| 404 | return err; |
| 405 | 0;})) |
| 406 | } |
| 407 | return 0; |
| 408 | } |
Al Viro | d4690f1 | 2016-09-16 00:11:45 +0100 | [diff] [blame] | 409 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
Anton Altaparmakov | 171a020 | 2015-03-11 10:43:31 -0400 | [diff] [blame] | 410 | |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 411 | void iov_iter_init(struct iov_iter *i, int direction, |
| 412 | const struct iovec *iov, unsigned long nr_segs, |
| 413 | size_t count) |
| 414 | { |
| 415 | /* It will get better. Eventually... */ |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 416 | if (uaccess_kernel()) { |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 417 | direction |= ITER_KVEC; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 418 | i->type = direction; |
| 419 | i->kvec = (struct kvec *)iov; |
| 420 | } else { |
| 421 | i->type = direction; |
| 422 | i->iov = iov; |
| 423 | } |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 424 | i->nr_segs = nr_segs; |
| 425 | i->iov_offset = 0; |
| 426 | i->count = count; |
| 427 | } |
| 428 | EXPORT_SYMBOL(iov_iter_init); |
Al Viro | 7b2c99d | 2014-03-15 04:05:57 -0400 | [diff] [blame] | 429 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 430 | static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len) |
| 431 | { |
| 432 | char *from = kmap_atomic(page); |
| 433 | memcpy(to, from + offset, len); |
| 434 | kunmap_atomic(from); |
| 435 | } |
| 436 | |
Al Viro | 36f7a8a | 2015-12-06 16:49:22 -0500 | [diff] [blame] | 437 | static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 438 | { |
| 439 | char *to = kmap_atomic(page); |
| 440 | memcpy(to + offset, from, len); |
| 441 | kunmap_atomic(to); |
| 442 | } |
| 443 | |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 444 | static void memzero_page(struct page *page, size_t offset, size_t len) |
| 445 | { |
| 446 | char *addr = kmap_atomic(page); |
| 447 | memset(addr + offset, 0, len); |
| 448 | kunmap_atomic(addr); |
| 449 | } |
| 450 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 451 | static inline bool allocated(struct pipe_buffer *buf) |
| 452 | { |
| 453 | return buf->ops == &default_pipe_buf_ops; |
| 454 | } |
| 455 | |
| 456 | static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp) |
| 457 | { |
| 458 | size_t off = i->iov_offset; |
| 459 | int idx = i->idx; |
| 460 | if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) { |
| 461 | idx = next_idx(idx, i->pipe); |
| 462 | off = 0; |
| 463 | } |
| 464 | *idxp = idx; |
| 465 | *offp = off; |
| 466 | } |
| 467 | |
| 468 | static size_t push_pipe(struct iov_iter *i, size_t size, |
| 469 | int *idxp, size_t *offp) |
| 470 | { |
| 471 | struct pipe_inode_info *pipe = i->pipe; |
| 472 | size_t off; |
| 473 | int idx; |
| 474 | ssize_t left; |
| 475 | |
| 476 | if (unlikely(size > i->count)) |
| 477 | size = i->count; |
| 478 | if (unlikely(!size)) |
| 479 | return 0; |
| 480 | |
| 481 | left = size; |
| 482 | data_start(i, &idx, &off); |
| 483 | *idxp = idx; |
| 484 | *offp = off; |
| 485 | if (off) { |
| 486 | left -= PAGE_SIZE - off; |
| 487 | if (left <= 0) { |
| 488 | pipe->bufs[idx].len += size; |
| 489 | return size; |
| 490 | } |
| 491 | pipe->bufs[idx].len = PAGE_SIZE; |
| 492 | idx = next_idx(idx, pipe); |
| 493 | } |
| 494 | while (idx != pipe->curbuf || !pipe->nrbufs) { |
| 495 | struct page *page = alloc_page(GFP_USER); |
| 496 | if (!page) |
| 497 | break; |
| 498 | pipe->nrbufs++; |
| 499 | pipe->bufs[idx].ops = &default_pipe_buf_ops; |
| 500 | pipe->bufs[idx].page = page; |
| 501 | pipe->bufs[idx].offset = 0; |
| 502 | if (left <= PAGE_SIZE) { |
| 503 | pipe->bufs[idx].len = left; |
| 504 | return size; |
| 505 | } |
| 506 | pipe->bufs[idx].len = PAGE_SIZE; |
| 507 | left -= PAGE_SIZE; |
| 508 | idx = next_idx(idx, pipe); |
| 509 | } |
| 510 | return size - left; |
| 511 | } |
| 512 | |
| 513 | static size_t copy_pipe_to_iter(const void *addr, size_t bytes, |
| 514 | struct iov_iter *i) |
| 515 | { |
| 516 | struct pipe_inode_info *pipe = i->pipe; |
| 517 | size_t n, off; |
| 518 | int idx; |
| 519 | |
| 520 | if (!sanity(i)) |
| 521 | return 0; |
| 522 | |
| 523 | bytes = n = push_pipe(i, bytes, &idx, &off); |
| 524 | if (unlikely(!n)) |
| 525 | return 0; |
| 526 | for ( ; n; idx = next_idx(idx, pipe), off = 0) { |
| 527 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
| 528 | memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk); |
| 529 | i->idx = idx; |
| 530 | i->iov_offset = off + chunk; |
| 531 | n -= chunk; |
| 532 | addr += chunk; |
| 533 | } |
| 534 | i->count -= bytes; |
| 535 | return bytes; |
| 536 | } |
| 537 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 538 | size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 539 | { |
Al Viro | 36f7a8a | 2015-12-06 16:49:22 -0500 | [diff] [blame] | 540 | const char *from = addr; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 541 | if (unlikely(i->type & ITER_PIPE)) |
| 542 | return copy_pipe_to_iter(addr, bytes, i); |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 543 | iterate_and_advance(i, bytes, v, |
| 544 | __copy_to_user(v.iov_base, (from += v.iov_len) - v.iov_len, |
| 545 | v.iov_len), |
| 546 | memcpy_to_page(v.bv_page, v.bv_offset, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 547 | (from += v.bv_len) - v.bv_len, v.bv_len), |
| 548 | memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len) |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 549 | ) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 550 | |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 551 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 552 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 553 | EXPORT_SYMBOL(_copy_to_iter); |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 554 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 555 | size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 556 | { |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 557 | char *to = addr; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 558 | if (unlikely(i->type & ITER_PIPE)) { |
| 559 | WARN_ON(1); |
| 560 | return 0; |
| 561 | } |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 562 | iterate_and_advance(i, bytes, v, |
| 563 | __copy_from_user((to += v.iov_len) - v.iov_len, v.iov_base, |
| 564 | v.iov_len), |
| 565 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 566 | v.bv_offset, v.bv_len), |
| 567 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 568 | ) |
| 569 | |
| 570 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 571 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 572 | EXPORT_SYMBOL(_copy_from_iter); |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 573 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 574 | bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 575 | { |
| 576 | char *to = addr; |
| 577 | if (unlikely(i->type & ITER_PIPE)) { |
| 578 | WARN_ON(1); |
| 579 | return false; |
| 580 | } |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 581 | if (unlikely(i->count < bytes)) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 582 | return false; |
| 583 | |
| 584 | iterate_all_kinds(i, bytes, v, ({ |
| 585 | if (__copy_from_user((to += v.iov_len) - v.iov_len, |
| 586 | v.iov_base, v.iov_len)) |
| 587 | return false; |
| 588 | 0;}), |
| 589 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 590 | v.bv_offset, v.bv_len), |
| 591 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) |
| 592 | ) |
| 593 | |
| 594 | iov_iter_advance(i, bytes); |
| 595 | return true; |
| 596 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 597 | EXPORT_SYMBOL(_copy_from_iter_full); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 598 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 599 | size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 600 | { |
| 601 | char *to = addr; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 602 | if (unlikely(i->type & ITER_PIPE)) { |
| 603 | WARN_ON(1); |
| 604 | return 0; |
| 605 | } |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 606 | iterate_and_advance(i, bytes, v, |
Al Viro | 3f76345 | 2017-03-25 18:47:28 -0400 | [diff] [blame] | 607 | __copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len, |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 608 | v.iov_base, v.iov_len), |
| 609 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 610 | v.bv_offset, v.bv_len), |
| 611 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) |
| 612 | ) |
| 613 | |
| 614 | return bytes; |
| 615 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 616 | EXPORT_SYMBOL(_copy_from_iter_nocache); |
Al Viro | aa58309 | 2014-11-27 20:27:08 -0500 | [diff] [blame] | 617 | |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 618 | bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 619 | { |
| 620 | char *to = addr; |
| 621 | if (unlikely(i->type & ITER_PIPE)) { |
| 622 | WARN_ON(1); |
| 623 | return false; |
| 624 | } |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 625 | if (unlikely(i->count < bytes)) |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 626 | return false; |
| 627 | iterate_all_kinds(i, bytes, v, ({ |
Al Viro | 3f76345 | 2017-03-25 18:47:28 -0400 | [diff] [blame] | 628 | if (__copy_from_user_inatomic_nocache((to += v.iov_len) - v.iov_len, |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 629 | v.iov_base, v.iov_len)) |
| 630 | return false; |
| 631 | 0;}), |
| 632 | memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page, |
| 633 | v.bv_offset, v.bv_len), |
| 634 | memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) |
| 635 | ) |
| 636 | |
| 637 | iov_iter_advance(i, bytes); |
| 638 | return true; |
| 639 | } |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 640 | EXPORT_SYMBOL(_copy_from_iter_full_nocache); |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 641 | |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame^] | 642 | static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) |
| 643 | { |
| 644 | size_t v = n + offset; |
| 645 | if (likely(n <= v && v <= (PAGE_SIZE << compound_order(page)))) |
| 646 | return true; |
| 647 | WARN_ON(1); |
| 648 | return false; |
| 649 | } |
| 650 | |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 651 | size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, |
| 652 | struct iov_iter *i) |
| 653 | { |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame^] | 654 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
| 655 | return 0; |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 656 | if (i->type & (ITER_BVEC|ITER_KVEC)) { |
| 657 | void *kaddr = kmap_atomic(page); |
| 658 | size_t wanted = copy_to_iter(kaddr + offset, bytes, i); |
| 659 | kunmap_atomic(kaddr); |
| 660 | return wanted; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 661 | } else if (likely(!(i->type & ITER_PIPE))) |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 662 | return copy_page_to_iter_iovec(page, offset, bytes, i); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 663 | else |
| 664 | return copy_page_to_iter_pipe(page, offset, bytes, i); |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 665 | } |
| 666 | EXPORT_SYMBOL(copy_page_to_iter); |
| 667 | |
| 668 | size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, |
| 669 | struct iov_iter *i) |
| 670 | { |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame^] | 671 | if (unlikely(!page_copy_sane(page, offset, bytes))) |
| 672 | return 0; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 673 | if (unlikely(i->type & ITER_PIPE)) { |
| 674 | WARN_ON(1); |
| 675 | return 0; |
| 676 | } |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 677 | if (i->type & (ITER_BVEC|ITER_KVEC)) { |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 678 | void *kaddr = kmap_atomic(page); |
Al Viro | aa28de2 | 2017-06-29 21:45:10 -0400 | [diff] [blame] | 679 | size_t wanted = _copy_from_iter(kaddr + offset, bytes, i); |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 680 | kunmap_atomic(kaddr); |
| 681 | return wanted; |
| 682 | } else |
| 683 | return copy_page_from_iter_iovec(page, offset, bytes, i); |
| 684 | } |
| 685 | EXPORT_SYMBOL(copy_page_from_iter); |
| 686 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 687 | static size_t pipe_zero(size_t bytes, struct iov_iter *i) |
| 688 | { |
| 689 | struct pipe_inode_info *pipe = i->pipe; |
| 690 | size_t n, off; |
| 691 | int idx; |
| 692 | |
| 693 | if (!sanity(i)) |
| 694 | return 0; |
| 695 | |
| 696 | bytes = n = push_pipe(i, bytes, &idx, &off); |
| 697 | if (unlikely(!n)) |
| 698 | return 0; |
| 699 | |
| 700 | for ( ; n; idx = next_idx(idx, pipe), off = 0) { |
| 701 | size_t chunk = min_t(size_t, n, PAGE_SIZE - off); |
| 702 | memzero_page(pipe->bufs[idx].page, off, chunk); |
| 703 | i->idx = idx; |
| 704 | i->iov_offset = off + chunk; |
| 705 | n -= chunk; |
| 706 | } |
| 707 | i->count -= bytes; |
| 708 | return bytes; |
| 709 | } |
| 710 | |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 711 | size_t iov_iter_zero(size_t bytes, struct iov_iter *i) |
| 712 | { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 713 | if (unlikely(i->type & ITER_PIPE)) |
| 714 | return pipe_zero(bytes, i); |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 715 | iterate_and_advance(i, bytes, v, |
| 716 | __clear_user(v.iov_base, v.iov_len), |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 717 | memzero_page(v.bv_page, v.bv_offset, v.bv_len), |
| 718 | memset(v.iov_base, 0, v.iov_len) |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 719 | ) |
| 720 | |
| 721 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 722 | } |
| 723 | EXPORT_SYMBOL(iov_iter_zero); |
| 724 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 725 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
| 726 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 727 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 728 | char *kaddr = kmap_atomic(page), *p = kaddr + offset; |
Al Viro | 72e809e | 2017-06-29 21:52:57 -0400 | [diff] [blame^] | 729 | if (unlikely(!page_copy_sane(page, offset, bytes))) { |
| 730 | kunmap_atomic(kaddr); |
| 731 | return 0; |
| 732 | } |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 733 | if (unlikely(i->type & ITER_PIPE)) { |
| 734 | kunmap_atomic(kaddr); |
| 735 | WARN_ON(1); |
| 736 | return 0; |
| 737 | } |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 738 | iterate_all_kinds(i, bytes, v, |
| 739 | __copy_from_user_inatomic((p += v.iov_len) - v.iov_len, |
| 740 | v.iov_base, v.iov_len), |
| 741 | memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 742 | v.bv_offset, v.bv_len), |
| 743 | memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len) |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 744 | ) |
| 745 | kunmap_atomic(kaddr); |
| 746 | return bytes; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 747 | } |
| 748 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); |
| 749 | |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 750 | static inline void pipe_truncate(struct iov_iter *i) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 751 | { |
| 752 | struct pipe_inode_info *pipe = i->pipe; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 753 | if (pipe->nrbufs) { |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 754 | size_t off = i->iov_offset; |
| 755 | int idx = i->idx; |
| 756 | int nrbufs = (idx - pipe->curbuf) & (pipe->buffers - 1); |
| 757 | if (off) { |
| 758 | pipe->bufs[idx].len = off - pipe->bufs[idx].offset; |
| 759 | idx = next_idx(idx, pipe); |
| 760 | nrbufs++; |
| 761 | } |
| 762 | while (pipe->nrbufs > nrbufs) { |
Miklos Szeredi | a779638 | 2016-09-27 10:45:12 +0200 | [diff] [blame] | 763 | pipe_buf_release(pipe, &pipe->bufs[idx]); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 764 | idx = next_idx(idx, pipe); |
| 765 | pipe->nrbufs--; |
| 766 | } |
| 767 | } |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void pipe_advance(struct iov_iter *i, size_t size) |
| 771 | { |
| 772 | struct pipe_inode_info *pipe = i->pipe; |
| 773 | if (unlikely(i->count < size)) |
| 774 | size = i->count; |
| 775 | if (size) { |
| 776 | struct pipe_buffer *buf; |
| 777 | size_t off = i->iov_offset, left = size; |
| 778 | int idx = i->idx; |
| 779 | if (off) /* make it relative to the beginning of buffer */ |
| 780 | left += off - pipe->bufs[idx].offset; |
| 781 | while (1) { |
| 782 | buf = &pipe->bufs[idx]; |
| 783 | if (left <= buf->len) |
| 784 | break; |
| 785 | left -= buf->len; |
| 786 | idx = next_idx(idx, pipe); |
| 787 | } |
| 788 | i->idx = idx; |
| 789 | i->iov_offset = buf->offset + left; |
| 790 | } |
| 791 | i->count -= size; |
| 792 | /* ... and discard everything past that point */ |
| 793 | pipe_truncate(i); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 794 | } |
| 795 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 796 | void iov_iter_advance(struct iov_iter *i, size_t size) |
| 797 | { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 798 | if (unlikely(i->type & ITER_PIPE)) { |
| 799 | pipe_advance(i, size); |
| 800 | return; |
| 801 | } |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 802 | iterate_and_advance(i, size, v, 0, 0, 0) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 803 | } |
| 804 | EXPORT_SYMBOL(iov_iter_advance); |
| 805 | |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 806 | void iov_iter_revert(struct iov_iter *i, size_t unroll) |
| 807 | { |
| 808 | if (!unroll) |
| 809 | return; |
Al Viro | 5b47d59 | 2017-05-08 13:54:47 -0400 | [diff] [blame] | 810 | if (WARN_ON(unroll > MAX_RW_COUNT)) |
| 811 | return; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 812 | i->count += unroll; |
| 813 | if (unlikely(i->type & ITER_PIPE)) { |
| 814 | struct pipe_inode_info *pipe = i->pipe; |
| 815 | int idx = i->idx; |
| 816 | size_t off = i->iov_offset; |
| 817 | while (1) { |
| 818 | size_t n = off - pipe->bufs[idx].offset; |
| 819 | if (unroll < n) { |
Al Viro | 4fa55ce | 2017-04-29 16:42:30 -0400 | [diff] [blame] | 820 | off -= unroll; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 821 | break; |
| 822 | } |
| 823 | unroll -= n; |
| 824 | if (!unroll && idx == i->start_idx) { |
| 825 | off = 0; |
| 826 | break; |
| 827 | } |
| 828 | if (!idx--) |
| 829 | idx = pipe->buffers - 1; |
| 830 | off = pipe->bufs[idx].offset + pipe->bufs[idx].len; |
| 831 | } |
| 832 | i->iov_offset = off; |
| 833 | i->idx = idx; |
| 834 | pipe_truncate(i); |
| 835 | return; |
| 836 | } |
| 837 | if (unroll <= i->iov_offset) { |
| 838 | i->iov_offset -= unroll; |
| 839 | return; |
| 840 | } |
| 841 | unroll -= i->iov_offset; |
| 842 | if (i->type & ITER_BVEC) { |
| 843 | const struct bio_vec *bvec = i->bvec; |
| 844 | while (1) { |
| 845 | size_t n = (--bvec)->bv_len; |
| 846 | i->nr_segs++; |
| 847 | if (unroll <= n) { |
| 848 | i->bvec = bvec; |
| 849 | i->iov_offset = n - unroll; |
| 850 | return; |
| 851 | } |
| 852 | unroll -= n; |
| 853 | } |
| 854 | } else { /* same logics for iovec and kvec */ |
| 855 | const struct iovec *iov = i->iov; |
| 856 | while (1) { |
| 857 | size_t n = (--iov)->iov_len; |
| 858 | i->nr_segs++; |
| 859 | if (unroll <= n) { |
| 860 | i->iov = iov; |
| 861 | i->iov_offset = n - unroll; |
| 862 | return; |
| 863 | } |
| 864 | unroll -= n; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | EXPORT_SYMBOL(iov_iter_revert); |
| 869 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 870 | /* |
| 871 | * Return the count of just the current iov_iter segment. |
| 872 | */ |
| 873 | size_t iov_iter_single_seg_count(const struct iov_iter *i) |
| 874 | { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 875 | if (unlikely(i->type & ITER_PIPE)) |
| 876 | return i->count; // it is a silly place, anyway |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 877 | if (i->nr_segs == 1) |
| 878 | return i->count; |
| 879 | else if (i->type & ITER_BVEC) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 880 | return min(i->count, i->bvec->bv_len - i->iov_offset); |
Paul Mackerras | ad0eab9 | 2014-11-13 20:15:23 +1100 | [diff] [blame] | 881 | else |
| 882 | return min(i->count, i->iov->iov_len - i->iov_offset); |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 883 | } |
| 884 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
| 885 | |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 886 | void iov_iter_kvec(struct iov_iter *i, int direction, |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 887 | const struct kvec *kvec, unsigned long nr_segs, |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 888 | size_t count) |
| 889 | { |
| 890 | BUG_ON(!(direction & ITER_KVEC)); |
| 891 | i->type = direction; |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 892 | i->kvec = kvec; |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame] | 893 | i->nr_segs = nr_segs; |
| 894 | i->iov_offset = 0; |
| 895 | i->count = count; |
| 896 | } |
| 897 | EXPORT_SYMBOL(iov_iter_kvec); |
| 898 | |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 899 | void iov_iter_bvec(struct iov_iter *i, int direction, |
| 900 | const struct bio_vec *bvec, unsigned long nr_segs, |
| 901 | size_t count) |
| 902 | { |
| 903 | BUG_ON(!(direction & ITER_BVEC)); |
| 904 | i->type = direction; |
| 905 | i->bvec = bvec; |
| 906 | i->nr_segs = nr_segs; |
| 907 | i->iov_offset = 0; |
| 908 | i->count = count; |
| 909 | } |
| 910 | EXPORT_SYMBOL(iov_iter_bvec); |
| 911 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 912 | void iov_iter_pipe(struct iov_iter *i, int direction, |
| 913 | struct pipe_inode_info *pipe, |
| 914 | size_t count) |
| 915 | { |
| 916 | BUG_ON(direction != ITER_PIPE); |
Al Viro | b9dc6f6 | 2017-01-14 19:33:08 -0500 | [diff] [blame] | 917 | WARN_ON(pipe->nrbufs == pipe->buffers); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 918 | i->type = direction; |
| 919 | i->pipe = pipe; |
| 920 | i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1); |
| 921 | i->iov_offset = 0; |
| 922 | i->count = count; |
Al Viro | 27c0e37 | 2017-02-17 18:42:24 -0500 | [diff] [blame] | 923 | i->start_idx = i->idx; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 924 | } |
| 925 | EXPORT_SYMBOL(iov_iter_pipe); |
| 926 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 927 | unsigned long iov_iter_alignment(const struct iov_iter *i) |
| 928 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 929 | unsigned long res = 0; |
| 930 | size_t size = i->count; |
| 931 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 932 | if (unlikely(i->type & ITER_PIPE)) { |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 933 | if (size && i->iov_offset && allocated(&i->pipe->bufs[i->idx])) |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 934 | return size | i->iov_offset; |
| 935 | return size; |
| 936 | } |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 937 | iterate_all_kinds(i, size, v, |
| 938 | (res |= (unsigned long)v.iov_base | v.iov_len, 0), |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 939 | res |= v.bv_offset | v.bv_len, |
| 940 | res |= (unsigned long)v.iov_base | v.iov_len |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 941 | ) |
| 942 | return res; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 943 | } |
| 944 | EXPORT_SYMBOL(iov_iter_alignment); |
| 945 | |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 946 | unsigned long iov_iter_gap_alignment(const struct iov_iter *i) |
| 947 | { |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 948 | unsigned long res = 0; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 949 | size_t size = i->count; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 950 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 951 | if (unlikely(i->type & ITER_PIPE)) { |
| 952 | WARN_ON(1); |
| 953 | return ~0U; |
| 954 | } |
| 955 | |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 956 | iterate_all_kinds(i, size, v, |
| 957 | (res |= (!res ? 0 : (unsigned long)v.iov_base) | |
| 958 | (size != v.iov_len ? size : 0), 0), |
| 959 | (res |= (!res ? 0 : (unsigned long)v.bv_offset) | |
| 960 | (size != v.bv_len ? size : 0)), |
| 961 | (res |= (!res ? 0 : (unsigned long)v.iov_base) | |
| 962 | (size != v.iov_len ? size : 0)) |
| 963 | ); |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 964 | return res; |
Al Viro | 357f435 | 2016-04-08 19:05:19 -0400 | [diff] [blame] | 965 | } |
| 966 | EXPORT_SYMBOL(iov_iter_gap_alignment); |
| 967 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 968 | static inline size_t __pipe_get_pages(struct iov_iter *i, |
| 969 | size_t maxsize, |
| 970 | struct page **pages, |
| 971 | int idx, |
| 972 | size_t *start) |
| 973 | { |
| 974 | struct pipe_inode_info *pipe = i->pipe; |
Al Viro | 1689c73 | 2016-10-11 18:21:14 +0100 | [diff] [blame] | 975 | ssize_t n = push_pipe(i, maxsize, &idx, start); |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 976 | if (!n) |
| 977 | return -EFAULT; |
| 978 | |
| 979 | maxsize = n; |
| 980 | n += *start; |
Al Viro | 1689c73 | 2016-10-11 18:21:14 +0100 | [diff] [blame] | 981 | while (n > 0) { |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 982 | get_page(*pages++ = pipe->bufs[idx].page); |
| 983 | idx = next_idx(idx, pipe); |
| 984 | n -= PAGE_SIZE; |
| 985 | } |
| 986 | |
| 987 | return maxsize; |
| 988 | } |
| 989 | |
| 990 | static ssize_t pipe_get_pages(struct iov_iter *i, |
| 991 | struct page **pages, size_t maxsize, unsigned maxpages, |
| 992 | size_t *start) |
| 993 | { |
| 994 | unsigned npages; |
| 995 | size_t capacity; |
| 996 | int idx; |
| 997 | |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 998 | if (!maxsize) |
| 999 | return 0; |
| 1000 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1001 | if (!sanity(i)) |
| 1002 | return -EFAULT; |
| 1003 | |
| 1004 | data_start(i, &idx, start); |
| 1005 | /* some of this one + all after this one */ |
| 1006 | npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1; |
| 1007 | capacity = min(npages,maxpages) * PAGE_SIZE - *start; |
| 1008 | |
| 1009 | return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start); |
| 1010 | } |
| 1011 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1012 | ssize_t iov_iter_get_pages(struct iov_iter *i, |
Miklos Szeredi | 2c80929 | 2014-09-24 17:09:11 +0200 | [diff] [blame] | 1013 | struct page **pages, size_t maxsize, unsigned maxpages, |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1014 | size_t *start) |
| 1015 | { |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1016 | if (maxsize > i->count) |
| 1017 | maxsize = i->count; |
| 1018 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1019 | if (unlikely(i->type & ITER_PIPE)) |
| 1020 | return pipe_get_pages(i, pages, maxsize, maxpages, start); |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1021 | iterate_all_kinds(i, maxsize, v, ({ |
| 1022 | unsigned long addr = (unsigned long)v.iov_base; |
| 1023 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); |
| 1024 | int n; |
| 1025 | int res; |
| 1026 | |
| 1027 | if (len > maxpages * PAGE_SIZE) |
| 1028 | len = maxpages * PAGE_SIZE; |
| 1029 | addr &= ~(PAGE_SIZE - 1); |
| 1030 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
| 1031 | res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages); |
| 1032 | if (unlikely(res < 0)) |
| 1033 | return res; |
| 1034 | return (res == n ? len : res * PAGE_SIZE) - *start; |
| 1035 | 0;}),({ |
| 1036 | /* can't be more than PAGE_SIZE */ |
| 1037 | *start = v.bv_offset; |
| 1038 | get_page(*pages = v.bv_page); |
| 1039 | return v.bv_len; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1040 | }),({ |
| 1041 | return -EFAULT; |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 1042 | }) |
| 1043 | ) |
| 1044 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1045 | } |
| 1046 | EXPORT_SYMBOL(iov_iter_get_pages); |
| 1047 | |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1048 | static struct page **get_pages_array(size_t n) |
| 1049 | { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 1050 | return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL); |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1051 | } |
| 1052 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1053 | static ssize_t pipe_get_pages_alloc(struct iov_iter *i, |
| 1054 | struct page ***pages, size_t maxsize, |
| 1055 | size_t *start) |
| 1056 | { |
| 1057 | struct page **p; |
| 1058 | size_t n; |
| 1059 | int idx; |
| 1060 | int npages; |
| 1061 | |
Al Viro | 33844e6 | 2016-12-21 21:55:02 -0500 | [diff] [blame] | 1062 | if (!maxsize) |
| 1063 | return 0; |
| 1064 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1065 | if (!sanity(i)) |
| 1066 | return -EFAULT; |
| 1067 | |
| 1068 | data_start(i, &idx, start); |
| 1069 | /* some of this one + all after this one */ |
| 1070 | npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1; |
| 1071 | n = npages * PAGE_SIZE - *start; |
| 1072 | if (maxsize > n) |
| 1073 | maxsize = n; |
| 1074 | else |
| 1075 | npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); |
| 1076 | p = get_pages_array(npages); |
| 1077 | if (!p) |
| 1078 | return -ENOMEM; |
| 1079 | n = __pipe_get_pages(i, maxsize, p, idx, start); |
| 1080 | if (n > 0) |
| 1081 | *pages = p; |
| 1082 | else |
| 1083 | kvfree(p); |
| 1084 | return n; |
| 1085 | } |
| 1086 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1087 | ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, |
| 1088 | struct page ***pages, size_t maxsize, |
| 1089 | size_t *start) |
| 1090 | { |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1091 | struct page **p; |
| 1092 | |
| 1093 | if (maxsize > i->count) |
| 1094 | maxsize = i->count; |
| 1095 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1096 | if (unlikely(i->type & ITER_PIPE)) |
| 1097 | return pipe_get_pages_alloc(i, pages, maxsize, start); |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1098 | iterate_all_kinds(i, maxsize, v, ({ |
| 1099 | unsigned long addr = (unsigned long)v.iov_base; |
| 1100 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); |
| 1101 | int n; |
| 1102 | int res; |
| 1103 | |
| 1104 | addr &= ~(PAGE_SIZE - 1); |
| 1105 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
| 1106 | p = get_pages_array(n); |
| 1107 | if (!p) |
| 1108 | return -ENOMEM; |
| 1109 | res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p); |
| 1110 | if (unlikely(res < 0)) { |
| 1111 | kvfree(p); |
| 1112 | return res; |
| 1113 | } |
| 1114 | *pages = p; |
| 1115 | return (res == n ? len : res * PAGE_SIZE) - *start; |
| 1116 | 0;}),({ |
| 1117 | /* can't be more than PAGE_SIZE */ |
| 1118 | *start = v.bv_offset; |
| 1119 | *pages = p = get_pages_array(1); |
| 1120 | if (!p) |
| 1121 | return -ENOMEM; |
| 1122 | get_page(*p = v.bv_page); |
| 1123 | return v.bv_len; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1124 | }),({ |
| 1125 | return -EFAULT; |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 1126 | }) |
| 1127 | ) |
| 1128 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1129 | } |
| 1130 | EXPORT_SYMBOL(iov_iter_get_pages_alloc); |
| 1131 | |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1132 | size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, |
| 1133 | struct iov_iter *i) |
| 1134 | { |
| 1135 | char *to = addr; |
| 1136 | __wsum sum, next; |
| 1137 | size_t off = 0; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1138 | sum = *csum; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1139 | if (unlikely(i->type & ITER_PIPE)) { |
| 1140 | WARN_ON(1); |
| 1141 | return 0; |
| 1142 | } |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1143 | iterate_and_advance(i, bytes, v, ({ |
| 1144 | int err = 0; |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1145 | next = csum_and_copy_from_user(v.iov_base, |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1146 | (to += v.iov_len) - v.iov_len, |
| 1147 | v.iov_len, 0, &err); |
| 1148 | if (!err) { |
| 1149 | sum = csum_block_add(sum, next, off); |
| 1150 | off += v.iov_len; |
| 1151 | } |
| 1152 | err ? v.iov_len : 0; |
| 1153 | }), ({ |
| 1154 | char *p = kmap_atomic(v.bv_page); |
| 1155 | next = csum_partial_copy_nocheck(p + v.bv_offset, |
| 1156 | (to += v.bv_len) - v.bv_len, |
| 1157 | v.bv_len, 0); |
| 1158 | kunmap_atomic(p); |
| 1159 | sum = csum_block_add(sum, next, off); |
| 1160 | off += v.bv_len; |
| 1161 | }),({ |
| 1162 | next = csum_partial_copy_nocheck(v.iov_base, |
| 1163 | (to += v.iov_len) - v.iov_len, |
| 1164 | v.iov_len, 0); |
| 1165 | sum = csum_block_add(sum, next, off); |
| 1166 | off += v.iov_len; |
| 1167 | }) |
| 1168 | ) |
| 1169 | *csum = sum; |
| 1170 | return bytes; |
| 1171 | } |
| 1172 | EXPORT_SYMBOL(csum_and_copy_from_iter); |
| 1173 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1174 | bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, |
| 1175 | struct iov_iter *i) |
| 1176 | { |
| 1177 | char *to = addr; |
| 1178 | __wsum sum, next; |
| 1179 | size_t off = 0; |
| 1180 | sum = *csum; |
| 1181 | if (unlikely(i->type & ITER_PIPE)) { |
| 1182 | WARN_ON(1); |
| 1183 | return false; |
| 1184 | } |
| 1185 | if (unlikely(i->count < bytes)) |
| 1186 | return false; |
| 1187 | iterate_all_kinds(i, bytes, v, ({ |
| 1188 | int err = 0; |
| 1189 | next = csum_and_copy_from_user(v.iov_base, |
| 1190 | (to += v.iov_len) - v.iov_len, |
| 1191 | v.iov_len, 0, &err); |
| 1192 | if (err) |
| 1193 | return false; |
| 1194 | sum = csum_block_add(sum, next, off); |
| 1195 | off += v.iov_len; |
| 1196 | 0; |
| 1197 | }), ({ |
| 1198 | char *p = kmap_atomic(v.bv_page); |
| 1199 | next = csum_partial_copy_nocheck(p + v.bv_offset, |
| 1200 | (to += v.bv_len) - v.bv_len, |
| 1201 | v.bv_len, 0); |
| 1202 | kunmap_atomic(p); |
| 1203 | sum = csum_block_add(sum, next, off); |
| 1204 | off += v.bv_len; |
| 1205 | }),({ |
| 1206 | next = csum_partial_copy_nocheck(v.iov_base, |
| 1207 | (to += v.iov_len) - v.iov_len, |
| 1208 | v.iov_len, 0); |
| 1209 | sum = csum_block_add(sum, next, off); |
| 1210 | off += v.iov_len; |
| 1211 | }) |
| 1212 | ) |
| 1213 | *csum = sum; |
| 1214 | iov_iter_advance(i, bytes); |
| 1215 | return true; |
| 1216 | } |
| 1217 | EXPORT_SYMBOL(csum_and_copy_from_iter_full); |
| 1218 | |
Al Viro | 36f7a8a | 2015-12-06 16:49:22 -0500 | [diff] [blame] | 1219 | size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum, |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1220 | struct iov_iter *i) |
| 1221 | { |
Al Viro | 36f7a8a | 2015-12-06 16:49:22 -0500 | [diff] [blame] | 1222 | const char *from = addr; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1223 | __wsum sum, next; |
| 1224 | size_t off = 0; |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1225 | sum = *csum; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1226 | if (unlikely(i->type & ITER_PIPE)) { |
| 1227 | WARN_ON(1); /* for now */ |
| 1228 | return 0; |
| 1229 | } |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1230 | iterate_and_advance(i, bytes, v, ({ |
| 1231 | int err = 0; |
| 1232 | next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len, |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1233 | v.iov_base, |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 1234 | v.iov_len, 0, &err); |
| 1235 | if (!err) { |
| 1236 | sum = csum_block_add(sum, next, off); |
| 1237 | off += v.iov_len; |
| 1238 | } |
| 1239 | err ? v.iov_len : 0; |
| 1240 | }), ({ |
| 1241 | char *p = kmap_atomic(v.bv_page); |
| 1242 | next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len, |
| 1243 | p + v.bv_offset, |
| 1244 | v.bv_len, 0); |
| 1245 | kunmap_atomic(p); |
| 1246 | sum = csum_block_add(sum, next, off); |
| 1247 | off += v.bv_len; |
| 1248 | }),({ |
| 1249 | next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len, |
| 1250 | v.iov_base, |
| 1251 | v.iov_len, 0); |
| 1252 | sum = csum_block_add(sum, next, off); |
| 1253 | off += v.iov_len; |
| 1254 | }) |
| 1255 | ) |
| 1256 | *csum = sum; |
| 1257 | return bytes; |
| 1258 | } |
| 1259 | EXPORT_SYMBOL(csum_and_copy_to_iter); |
| 1260 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1261 | int iov_iter_npages(const struct iov_iter *i, int maxpages) |
| 1262 | { |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1263 | size_t size = i->count; |
| 1264 | int npages = 0; |
| 1265 | |
| 1266 | if (!size) |
| 1267 | return 0; |
| 1268 | |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1269 | if (unlikely(i->type & ITER_PIPE)) { |
| 1270 | struct pipe_inode_info *pipe = i->pipe; |
| 1271 | size_t off; |
| 1272 | int idx; |
| 1273 | |
| 1274 | if (!sanity(i)) |
| 1275 | return 0; |
| 1276 | |
| 1277 | data_start(i, &idx, &off); |
| 1278 | /* some of this one + all after this one */ |
| 1279 | npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1; |
| 1280 | if (npages >= maxpages) |
| 1281 | return maxpages; |
| 1282 | } else iterate_all_kinds(i, size, v, ({ |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1283 | unsigned long p = (unsigned long)v.iov_base; |
| 1284 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) |
| 1285 | - p / PAGE_SIZE; |
| 1286 | if (npages >= maxpages) |
| 1287 | return maxpages; |
| 1288 | 0;}),({ |
| 1289 | npages++; |
| 1290 | if (npages >= maxpages) |
| 1291 | return maxpages; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 1292 | }),({ |
| 1293 | unsigned long p = (unsigned long)v.iov_base; |
| 1294 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) |
| 1295 | - p / PAGE_SIZE; |
| 1296 | if (npages >= maxpages) |
| 1297 | return maxpages; |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 1298 | }) |
| 1299 | ) |
| 1300 | return npages; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 1301 | } |
Al Viro | f67da30 | 2014-03-19 01:16:16 -0400 | [diff] [blame] | 1302 | EXPORT_SYMBOL(iov_iter_npages); |
Al Viro | 4b8164b | 2015-01-31 20:08:47 -0500 | [diff] [blame] | 1303 | |
| 1304 | const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) |
| 1305 | { |
| 1306 | *new = *old; |
Al Viro | 241699c | 2016-09-22 16:33:12 -0400 | [diff] [blame] | 1307 | if (unlikely(new->type & ITER_PIPE)) { |
| 1308 | WARN_ON(1); |
| 1309 | return NULL; |
| 1310 | } |
Al Viro | 4b8164b | 2015-01-31 20:08:47 -0500 | [diff] [blame] | 1311 | if (new->type & ITER_BVEC) |
| 1312 | return new->bvec = kmemdup(new->bvec, |
| 1313 | new->nr_segs * sizeof(struct bio_vec), |
| 1314 | flags); |
| 1315 | else |
| 1316 | /* iovec and kvec have identical layout */ |
| 1317 | return new->iov = kmemdup(new->iov, |
| 1318 | new->nr_segs * sizeof(struct iovec), |
| 1319 | flags); |
| 1320 | } |
| 1321 | EXPORT_SYMBOL(dup_iter); |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 1322 | |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 1323 | /** |
| 1324 | * import_iovec() - Copy an array of &struct iovec from userspace |
| 1325 | * into the kernel, check that it is valid, and initialize a new |
| 1326 | * &struct iov_iter iterator to access it. |
| 1327 | * |
| 1328 | * @type: One of %READ or %WRITE. |
| 1329 | * @uvector: Pointer to the userspace array. |
| 1330 | * @nr_segs: Number of elements in userspace array. |
| 1331 | * @fast_segs: Number of elements in @iov. |
| 1332 | * @iov: (input and output parameter) Pointer to pointer to (usually small |
| 1333 | * on-stack) kernel array. |
| 1334 | * @i: Pointer to iterator that will be initialized on success. |
| 1335 | * |
| 1336 | * If the array pointed to by *@iov is large enough to hold all @nr_segs, |
| 1337 | * then this function places %NULL in *@iov on return. Otherwise, a new |
| 1338 | * array will be allocated and the result placed in *@iov. This means that |
| 1339 | * the caller may call kfree() on *@iov regardless of whether the small |
| 1340 | * on-stack array was used or not (and regardless of whether this function |
| 1341 | * returns an error or not). |
| 1342 | * |
| 1343 | * Return: 0 on success or negative error code on error. |
| 1344 | */ |
Al Viro | bc917be | 2015-03-21 17:45:43 -0400 | [diff] [blame] | 1345 | int import_iovec(int type, const struct iovec __user * uvector, |
| 1346 | unsigned nr_segs, unsigned fast_segs, |
| 1347 | struct iovec **iov, struct iov_iter *i) |
| 1348 | { |
| 1349 | ssize_t n; |
| 1350 | struct iovec *p; |
| 1351 | n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs, |
| 1352 | *iov, &p); |
| 1353 | if (n < 0) { |
| 1354 | if (p != *iov) |
| 1355 | kfree(p); |
| 1356 | *iov = NULL; |
| 1357 | return n; |
| 1358 | } |
| 1359 | iov_iter_init(i, type, p, nr_segs, n); |
| 1360 | *iov = p == *iov ? NULL : p; |
| 1361 | return 0; |
| 1362 | } |
| 1363 | EXPORT_SYMBOL(import_iovec); |
| 1364 | |
| 1365 | #ifdef CONFIG_COMPAT |
| 1366 | #include <linux/compat.h> |
| 1367 | |
| 1368 | int compat_import_iovec(int type, const struct compat_iovec __user * uvector, |
| 1369 | unsigned nr_segs, unsigned fast_segs, |
| 1370 | struct iovec **iov, struct iov_iter *i) |
| 1371 | { |
| 1372 | ssize_t n; |
| 1373 | struct iovec *p; |
| 1374 | n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs, |
| 1375 | *iov, &p); |
| 1376 | if (n < 0) { |
| 1377 | if (p != *iov) |
| 1378 | kfree(p); |
| 1379 | *iov = NULL; |
| 1380 | return n; |
| 1381 | } |
| 1382 | iov_iter_init(i, type, p, nr_segs, n); |
| 1383 | *iov = p == *iov ? NULL : p; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | #endif |
| 1387 | |
| 1388 | int import_single_range(int rw, void __user *buf, size_t len, |
| 1389 | struct iovec *iov, struct iov_iter *i) |
| 1390 | { |
| 1391 | if (len > MAX_RW_COUNT) |
| 1392 | len = MAX_RW_COUNT; |
| 1393 | if (unlikely(!access_ok(!rw, buf, len))) |
| 1394 | return -EFAULT; |
| 1395 | |
| 1396 | iov->iov_base = buf; |
| 1397 | iov->iov_len = len; |
| 1398 | iov_iter_init(i, rw, iov, 1, len); |
| 1399 | return 0; |
| 1400 | } |
Al Viro | e126758 | 2015-12-06 20:38:56 -0500 | [diff] [blame] | 1401 | EXPORT_SYMBOL(import_single_range); |