Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 1 | #include <linux/export.h> |
| 2 | #include <linux/uio.h> |
| 3 | #include <linux/pagemap.h> |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 4 | #include <linux/slab.h> |
| 5 | #include <linux/vmalloc.h> |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 6 | #include <net/checksum.h> |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 7 | |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 8 | #define iterate_iovec(i, n, __v, __p, skip, STEP) { \ |
| 9 | size_t left; \ |
| 10 | size_t wanted = n; \ |
| 11 | __p = i->iov; \ |
| 12 | __v.iov_len = min(n, __p->iov_len - skip); \ |
| 13 | if (likely(__v.iov_len)) { \ |
| 14 | __v.iov_base = __p->iov_base + skip; \ |
| 15 | left = (STEP); \ |
| 16 | __v.iov_len -= left; \ |
| 17 | skip += __v.iov_len; \ |
| 18 | n -= __v.iov_len; \ |
| 19 | } else { \ |
| 20 | left = 0; \ |
| 21 | } \ |
| 22 | while (unlikely(!left && n)) { \ |
| 23 | __p++; \ |
| 24 | __v.iov_len = min(n, __p->iov_len); \ |
| 25 | if (unlikely(!__v.iov_len)) \ |
| 26 | continue; \ |
| 27 | __v.iov_base = __p->iov_base; \ |
| 28 | left = (STEP); \ |
| 29 | __v.iov_len -= left; \ |
| 30 | skip = __v.iov_len; \ |
| 31 | n -= __v.iov_len; \ |
| 32 | } \ |
| 33 | n = wanted - n; \ |
| 34 | } |
| 35 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 36 | #define iterate_kvec(i, n, __v, __p, skip, STEP) { \ |
| 37 | size_t wanted = n; \ |
| 38 | __p = i->kvec; \ |
| 39 | __v.iov_len = min(n, __p->iov_len - skip); \ |
| 40 | if (likely(__v.iov_len)) { \ |
| 41 | __v.iov_base = __p->iov_base + skip; \ |
| 42 | (void)(STEP); \ |
| 43 | skip += __v.iov_len; \ |
| 44 | n -= __v.iov_len; \ |
| 45 | } \ |
| 46 | while (unlikely(n)) { \ |
| 47 | __p++; \ |
| 48 | __v.iov_len = min(n, __p->iov_len); \ |
| 49 | if (unlikely(!__v.iov_len)) \ |
| 50 | continue; \ |
| 51 | __v.iov_base = __p->iov_base; \ |
| 52 | (void)(STEP); \ |
| 53 | skip = __v.iov_len; \ |
| 54 | n -= __v.iov_len; \ |
| 55 | } \ |
| 56 | n = wanted; \ |
| 57 | } |
| 58 | |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 59 | #define iterate_bvec(i, n, __v, __p, skip, STEP) { \ |
| 60 | size_t wanted = n; \ |
| 61 | __p = i->bvec; \ |
| 62 | __v.bv_len = min_t(size_t, n, __p->bv_len - skip); \ |
| 63 | if (likely(__v.bv_len)) { \ |
| 64 | __v.bv_page = __p->bv_page; \ |
| 65 | __v.bv_offset = __p->bv_offset + skip; \ |
| 66 | (void)(STEP); \ |
| 67 | skip += __v.bv_len; \ |
| 68 | n -= __v.bv_len; \ |
| 69 | } \ |
| 70 | while (unlikely(n)) { \ |
| 71 | __p++; \ |
| 72 | __v.bv_len = min_t(size_t, n, __p->bv_len); \ |
| 73 | if (unlikely(!__v.bv_len)) \ |
| 74 | continue; \ |
| 75 | __v.bv_page = __p->bv_page; \ |
| 76 | __v.bv_offset = __p->bv_offset; \ |
| 77 | (void)(STEP); \ |
| 78 | skip = __v.bv_len; \ |
| 79 | n -= __v.bv_len; \ |
| 80 | } \ |
| 81 | n = wanted; \ |
| 82 | } |
| 83 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 84 | #define iterate_all_kinds(i, n, v, I, B, K) { \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 85 | size_t skip = i->iov_offset; \ |
| 86 | if (unlikely(i->type & ITER_BVEC)) { \ |
| 87 | const struct bio_vec *bvec; \ |
| 88 | struct bio_vec v; \ |
| 89 | iterate_bvec(i, n, v, bvec, skip, (B)) \ |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 90 | } else if (unlikely(i->type & ITER_KVEC)) { \ |
| 91 | const struct kvec *kvec; \ |
| 92 | struct kvec v; \ |
| 93 | iterate_kvec(i, n, v, kvec, skip, (K)) \ |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 94 | } else { \ |
| 95 | const struct iovec *iov; \ |
| 96 | struct iovec v; \ |
| 97 | iterate_iovec(i, n, v, iov, skip, (I)) \ |
| 98 | } \ |
| 99 | } |
| 100 | |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 101 | #define iterate_and_advance(i, n, v, I, B, K) { \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 102 | size_t skip = i->iov_offset; \ |
| 103 | if (unlikely(i->type & ITER_BVEC)) { \ |
| 104 | const struct bio_vec *bvec; \ |
| 105 | struct bio_vec v; \ |
| 106 | iterate_bvec(i, n, v, bvec, skip, (B)) \ |
| 107 | if (skip == bvec->bv_len) { \ |
| 108 | bvec++; \ |
| 109 | skip = 0; \ |
| 110 | } \ |
| 111 | i->nr_segs -= bvec - i->bvec; \ |
| 112 | i->bvec = bvec; \ |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 113 | } else if (unlikely(i->type & ITER_KVEC)) { \ |
| 114 | const struct kvec *kvec; \ |
| 115 | struct kvec v; \ |
| 116 | iterate_kvec(i, n, v, kvec, skip, (K)) \ |
| 117 | if (skip == kvec->iov_len) { \ |
| 118 | kvec++; \ |
| 119 | skip = 0; \ |
| 120 | } \ |
| 121 | i->nr_segs -= kvec - i->kvec; \ |
| 122 | i->kvec = kvec; \ |
Al Viro | 7ce2a91 | 2014-11-27 13:59:45 -0500 | [diff] [blame] | 123 | } else { \ |
| 124 | const struct iovec *iov; \ |
| 125 | struct iovec v; \ |
| 126 | iterate_iovec(i, n, v, iov, skip, (I)) \ |
| 127 | if (skip == iov->iov_len) { \ |
| 128 | iov++; \ |
| 129 | skip = 0; \ |
| 130 | } \ |
| 131 | i->nr_segs -= iov - i->iov; \ |
| 132 | i->iov = iov; \ |
| 133 | } \ |
| 134 | i->count -= n; \ |
| 135 | i->iov_offset = skip; \ |
| 136 | } |
| 137 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 138 | 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] | 139 | struct iov_iter *i) |
| 140 | { |
| 141 | size_t skip, copy, left, wanted; |
| 142 | const struct iovec *iov; |
| 143 | char __user *buf; |
| 144 | void *kaddr, *from; |
| 145 | |
| 146 | if (unlikely(bytes > i->count)) |
| 147 | bytes = i->count; |
| 148 | |
| 149 | if (unlikely(!bytes)) |
| 150 | return 0; |
| 151 | |
| 152 | wanted = bytes; |
| 153 | iov = i->iov; |
| 154 | skip = i->iov_offset; |
| 155 | buf = iov->iov_base + skip; |
| 156 | copy = min(bytes, iov->iov_len - skip); |
| 157 | |
| 158 | if (!fault_in_pages_writeable(buf, copy)) { |
| 159 | kaddr = kmap_atomic(page); |
| 160 | from = kaddr + offset; |
| 161 | |
| 162 | /* first chunk, usually the only one */ |
| 163 | left = __copy_to_user_inatomic(buf, from, copy); |
| 164 | copy -= left; |
| 165 | skip += copy; |
| 166 | from += copy; |
| 167 | bytes -= copy; |
| 168 | |
| 169 | while (unlikely(!left && bytes)) { |
| 170 | iov++; |
| 171 | buf = iov->iov_base; |
| 172 | copy = min(bytes, iov->iov_len); |
| 173 | left = __copy_to_user_inatomic(buf, from, copy); |
| 174 | copy -= left; |
| 175 | skip = copy; |
| 176 | from += copy; |
| 177 | bytes -= copy; |
| 178 | } |
| 179 | if (likely(!bytes)) { |
| 180 | kunmap_atomic(kaddr); |
| 181 | goto done; |
| 182 | } |
| 183 | offset = from - kaddr; |
| 184 | buf += copy; |
| 185 | kunmap_atomic(kaddr); |
| 186 | copy = min(bytes, iov->iov_len - skip); |
| 187 | } |
| 188 | /* Too bad - revert to non-atomic kmap */ |
| 189 | kaddr = kmap(page); |
| 190 | from = kaddr + offset; |
| 191 | left = __copy_to_user(buf, from, copy); |
| 192 | copy -= left; |
| 193 | skip += copy; |
| 194 | from += copy; |
| 195 | bytes -= copy; |
| 196 | while (unlikely(!left && bytes)) { |
| 197 | iov++; |
| 198 | buf = iov->iov_base; |
| 199 | copy = min(bytes, iov->iov_len); |
| 200 | left = __copy_to_user(buf, from, copy); |
| 201 | copy -= left; |
| 202 | skip = copy; |
| 203 | from += copy; |
| 204 | bytes -= copy; |
| 205 | } |
| 206 | kunmap(page); |
| 207 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 208 | if (skip == iov->iov_len) { |
| 209 | iov++; |
| 210 | skip = 0; |
| 211 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 212 | i->count -= wanted - bytes; |
| 213 | i->nr_segs -= iov - i->iov; |
| 214 | i->iov = iov; |
| 215 | i->iov_offset = skip; |
| 216 | return wanted - bytes; |
| 217 | } |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 218 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 219 | 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] | 220 | struct iov_iter *i) |
| 221 | { |
| 222 | size_t skip, copy, left, wanted; |
| 223 | const struct iovec *iov; |
| 224 | char __user *buf; |
| 225 | void *kaddr, *to; |
| 226 | |
| 227 | if (unlikely(bytes > i->count)) |
| 228 | bytes = i->count; |
| 229 | |
| 230 | if (unlikely(!bytes)) |
| 231 | return 0; |
| 232 | |
| 233 | wanted = bytes; |
| 234 | iov = i->iov; |
| 235 | skip = i->iov_offset; |
| 236 | buf = iov->iov_base + skip; |
| 237 | copy = min(bytes, iov->iov_len - skip); |
| 238 | |
| 239 | if (!fault_in_pages_readable(buf, copy)) { |
| 240 | kaddr = kmap_atomic(page); |
| 241 | to = kaddr + offset; |
| 242 | |
| 243 | /* first chunk, usually the only one */ |
| 244 | left = __copy_from_user_inatomic(to, buf, copy); |
| 245 | copy -= left; |
| 246 | skip += copy; |
| 247 | to += copy; |
| 248 | bytes -= copy; |
| 249 | |
| 250 | while (unlikely(!left && bytes)) { |
| 251 | iov++; |
| 252 | buf = iov->iov_base; |
| 253 | copy = min(bytes, iov->iov_len); |
| 254 | left = __copy_from_user_inatomic(to, buf, copy); |
| 255 | copy -= left; |
| 256 | skip = copy; |
| 257 | to += copy; |
| 258 | bytes -= copy; |
| 259 | } |
| 260 | if (likely(!bytes)) { |
| 261 | kunmap_atomic(kaddr); |
| 262 | goto done; |
| 263 | } |
| 264 | offset = to - kaddr; |
| 265 | buf += copy; |
| 266 | kunmap_atomic(kaddr); |
| 267 | copy = min(bytes, iov->iov_len - skip); |
| 268 | } |
| 269 | /* Too bad - revert to non-atomic kmap */ |
| 270 | kaddr = kmap(page); |
| 271 | to = kaddr + offset; |
| 272 | left = __copy_from_user(to, buf, copy); |
| 273 | copy -= left; |
| 274 | skip += copy; |
| 275 | to += copy; |
| 276 | bytes -= copy; |
| 277 | while (unlikely(!left && bytes)) { |
| 278 | iov++; |
| 279 | buf = iov->iov_base; |
| 280 | copy = min(bytes, iov->iov_len); |
| 281 | left = __copy_from_user(to, buf, copy); |
| 282 | copy -= left; |
| 283 | skip = copy; |
| 284 | to += copy; |
| 285 | bytes -= copy; |
| 286 | } |
| 287 | kunmap(page); |
| 288 | done: |
Al Viro | 81055e5 | 2014-04-04 19:23:46 -0400 | [diff] [blame] | 289 | if (skip == iov->iov_len) { |
| 290 | iov++; |
| 291 | skip = 0; |
| 292 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 293 | i->count -= wanted - bytes; |
| 294 | i->nr_segs -= iov - i->iov; |
| 295 | i->iov = iov; |
| 296 | i->iov_offset = skip; |
| 297 | return wanted - bytes; |
| 298 | } |
Al Viro | f0d1bec | 2014-04-03 15:05:18 -0400 | [diff] [blame] | 299 | |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 300 | /* |
| 301 | * Fault in the first iovec of the given iov_iter, to a maximum length |
| 302 | * of bytes. Returns 0 on success, or non-zero if the memory could not be |
| 303 | * accessed (ie. because it is an invalid address). |
| 304 | * |
| 305 | * writev-intensive code may want this to prefault several iovecs -- that |
| 306 | * would be possible (callers must not rely on the fact that _only_ the |
| 307 | * first iovec will be faulted with the current implementation). |
| 308 | */ |
| 309 | int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) |
| 310 | { |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 311 | if (!(i->type & (ITER_BVEC|ITER_KVEC))) { |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 312 | char __user *buf = i->iov->iov_base + i->iov_offset; |
| 313 | bytes = min(bytes, i->iov->iov_len - i->iov_offset); |
| 314 | return fault_in_pages_readable(buf, bytes); |
| 315 | } |
| 316 | return 0; |
Al Viro | 4f18cd3 | 2014-02-05 19:11:33 -0500 | [diff] [blame] | 317 | } |
| 318 | EXPORT_SYMBOL(iov_iter_fault_in_readable); |
| 319 | |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 320 | void iov_iter_init(struct iov_iter *i, int direction, |
| 321 | const struct iovec *iov, unsigned long nr_segs, |
| 322 | size_t count) |
| 323 | { |
| 324 | /* It will get better. Eventually... */ |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 325 | if (segment_eq(get_fs(), KERNEL_DS)) { |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 326 | direction |= ITER_KVEC; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 327 | i->type = direction; |
| 328 | i->kvec = (struct kvec *)iov; |
| 329 | } else { |
| 330 | i->type = direction; |
| 331 | i->iov = iov; |
| 332 | } |
Al Viro | 71d8e53 | 2014-03-05 19:28:09 -0500 | [diff] [blame] | 333 | i->nr_segs = nr_segs; |
| 334 | i->iov_offset = 0; |
| 335 | i->count = count; |
| 336 | } |
| 337 | EXPORT_SYMBOL(iov_iter_init); |
Al Viro | 7b2c99d | 2014-03-15 04:05:57 -0400 | [diff] [blame] | 338 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 339 | static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len) |
| 340 | { |
| 341 | char *from = kmap_atomic(page); |
| 342 | memcpy(to, from + offset, len); |
| 343 | kunmap_atomic(from); |
| 344 | } |
| 345 | |
| 346 | static void memcpy_to_page(struct page *page, size_t offset, char *from, size_t len) |
| 347 | { |
| 348 | char *to = kmap_atomic(page); |
| 349 | memcpy(to + offset, from, len); |
| 350 | kunmap_atomic(to); |
| 351 | } |
| 352 | |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 353 | static void memzero_page(struct page *page, size_t offset, size_t len) |
| 354 | { |
| 355 | char *addr = kmap_atomic(page); |
| 356 | memset(addr + offset, 0, len); |
| 357 | kunmap_atomic(addr); |
| 358 | } |
| 359 | |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 360 | size_t copy_to_iter(void *addr, size_t bytes, struct iov_iter *i) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 361 | { |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 362 | char *from = addr; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 363 | if (unlikely(bytes > i->count)) |
| 364 | bytes = i->count; |
| 365 | |
| 366 | if (unlikely(!bytes)) |
| 367 | return 0; |
| 368 | |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 369 | iterate_and_advance(i, bytes, v, |
| 370 | __copy_to_user(v.iov_base, (from += v.iov_len) - v.iov_len, |
| 371 | v.iov_len), |
| 372 | memcpy_to_page(v.bv_page, v.bv_offset, |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 373 | (from += v.bv_len) - v.bv_len, v.bv_len), |
| 374 | 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] | 375 | ) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 376 | |
Al Viro | 3d4d3e4 | 2014-11-27 14:28:06 -0500 | [diff] [blame] | 377 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 378 | } |
| 379 | EXPORT_SYMBOL(copy_to_iter); |
| 380 | |
| 381 | size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) |
| 382 | { |
Al Viro | 0dbca9a | 2014-11-27 14:26:43 -0500 | [diff] [blame] | 383 | char *to = addr; |
| 384 | if (unlikely(bytes > i->count)) |
| 385 | bytes = i->count; |
| 386 | |
| 387 | if (unlikely(!bytes)) |
| 388 | return 0; |
| 389 | |
| 390 | iterate_and_advance(i, bytes, v, |
| 391 | __copy_from_user((to += v.iov_len) - v.iov_len, v.iov_base, |
| 392 | v.iov_len), |
| 393 | 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] | 394 | v.bv_offset, v.bv_len), |
| 395 | 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] | 396 | ) |
| 397 | |
| 398 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 399 | } |
| 400 | EXPORT_SYMBOL(copy_from_iter); |
| 401 | |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 402 | size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, |
| 403 | struct iov_iter *i) |
| 404 | { |
| 405 | if (i->type & (ITER_BVEC|ITER_KVEC)) { |
| 406 | void *kaddr = kmap_atomic(page); |
| 407 | size_t wanted = copy_to_iter(kaddr + offset, bytes, i); |
| 408 | kunmap_atomic(kaddr); |
| 409 | return wanted; |
| 410 | } else |
| 411 | return copy_page_to_iter_iovec(page, offset, bytes, i); |
| 412 | } |
| 413 | EXPORT_SYMBOL(copy_page_to_iter); |
| 414 | |
| 415 | size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, |
| 416 | struct iov_iter *i) |
| 417 | { |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 418 | if (i->type & (ITER_BVEC|ITER_KVEC)) { |
Al Viro | d271524 | 2014-11-27 14:22:37 -0500 | [diff] [blame] | 419 | void *kaddr = kmap_atomic(page); |
| 420 | size_t wanted = copy_from_iter(kaddr + offset, bytes, i); |
| 421 | kunmap_atomic(kaddr); |
| 422 | return wanted; |
| 423 | } else |
| 424 | return copy_page_from_iter_iovec(page, offset, bytes, i); |
| 425 | } |
| 426 | EXPORT_SYMBOL(copy_page_from_iter); |
| 427 | |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 428 | size_t iov_iter_zero(size_t bytes, struct iov_iter *i) |
| 429 | { |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 430 | if (unlikely(bytes > i->count)) |
| 431 | bytes = i->count; |
| 432 | |
| 433 | if (unlikely(!bytes)) |
| 434 | return 0; |
| 435 | |
| 436 | iterate_and_advance(i, bytes, v, |
| 437 | __clear_user(v.iov_base, v.iov_len), |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 438 | memzero_page(v.bv_page, v.bv_offset, v.bv_len), |
| 439 | memset(v.iov_base, 0, v.iov_len) |
Al Viro | 8442fa4 | 2014-11-27 14:18:54 -0500 | [diff] [blame] | 440 | ) |
| 441 | |
| 442 | return bytes; |
Matthew Wilcox | c35e024 | 2014-08-01 09:27:22 -0400 | [diff] [blame] | 443 | } |
| 444 | EXPORT_SYMBOL(iov_iter_zero); |
| 445 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 446 | size_t iov_iter_copy_from_user_atomic(struct page *page, |
| 447 | struct iov_iter *i, unsigned long offset, size_t bytes) |
| 448 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 449 | char *kaddr = kmap_atomic(page), *p = kaddr + offset; |
| 450 | iterate_all_kinds(i, bytes, v, |
| 451 | __copy_from_user_inatomic((p += v.iov_len) - v.iov_len, |
| 452 | v.iov_base, v.iov_len), |
| 453 | 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] | 454 | v.bv_offset, v.bv_len), |
| 455 | 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] | 456 | ) |
| 457 | kunmap_atomic(kaddr); |
| 458 | return bytes; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 459 | } |
| 460 | EXPORT_SYMBOL(iov_iter_copy_from_user_atomic); |
| 461 | |
| 462 | void iov_iter_advance(struct iov_iter *i, size_t size) |
| 463 | { |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 464 | iterate_and_advance(i, size, v, 0, 0, 0) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 465 | } |
| 466 | EXPORT_SYMBOL(iov_iter_advance); |
| 467 | |
| 468 | /* |
| 469 | * Return the count of just the current iov_iter segment. |
| 470 | */ |
| 471 | size_t iov_iter_single_seg_count(const struct iov_iter *i) |
| 472 | { |
| 473 | if (i->nr_segs == 1) |
| 474 | return i->count; |
| 475 | else if (i->type & ITER_BVEC) |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 476 | return min(i->count, i->bvec->bv_len - i->iov_offset); |
Paul Mackerras | ad0eab9 | 2014-11-13 20:15:23 +1100 | [diff] [blame] | 477 | else |
| 478 | return min(i->count, i->iov->iov_len - i->iov_offset); |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 479 | } |
| 480 | EXPORT_SYMBOL(iov_iter_single_seg_count); |
| 481 | |
Al Viro | abb78f8 | 2014-11-24 14:46:11 -0500 | [diff] [blame^] | 482 | void iov_iter_kvec(struct iov_iter *i, int direction, |
| 483 | const struct kvec *iov, unsigned long nr_segs, |
| 484 | size_t count) |
| 485 | { |
| 486 | BUG_ON(!(direction & ITER_KVEC)); |
| 487 | i->type = direction; |
| 488 | i->kvec = (struct kvec *)iov; |
| 489 | i->nr_segs = nr_segs; |
| 490 | i->iov_offset = 0; |
| 491 | i->count = count; |
| 492 | } |
| 493 | EXPORT_SYMBOL(iov_iter_kvec); |
| 494 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 495 | unsigned long iov_iter_alignment(const struct iov_iter *i) |
| 496 | { |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 497 | unsigned long res = 0; |
| 498 | size_t size = i->count; |
| 499 | |
| 500 | if (!size) |
| 501 | return 0; |
| 502 | |
| 503 | iterate_all_kinds(i, size, v, |
| 504 | (res |= (unsigned long)v.iov_base | v.iov_len, 0), |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 505 | res |= v.bv_offset | v.bv_len, |
| 506 | res |= (unsigned long)v.iov_base | v.iov_len |
Al Viro | 04a3116 | 2014-11-27 13:51:41 -0500 | [diff] [blame] | 507 | ) |
| 508 | return res; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 509 | } |
| 510 | EXPORT_SYMBOL(iov_iter_alignment); |
| 511 | |
| 512 | ssize_t iov_iter_get_pages(struct iov_iter *i, |
Miklos Szeredi | 2c80929 | 2014-09-24 17:09:11 +0200 | [diff] [blame] | 513 | struct page **pages, size_t maxsize, unsigned maxpages, |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 514 | size_t *start) |
| 515 | { |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 516 | if (maxsize > i->count) |
| 517 | maxsize = i->count; |
| 518 | |
| 519 | if (!maxsize) |
| 520 | return 0; |
| 521 | |
| 522 | iterate_all_kinds(i, maxsize, v, ({ |
| 523 | unsigned long addr = (unsigned long)v.iov_base; |
| 524 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); |
| 525 | int n; |
| 526 | int res; |
| 527 | |
| 528 | if (len > maxpages * PAGE_SIZE) |
| 529 | len = maxpages * PAGE_SIZE; |
| 530 | addr &= ~(PAGE_SIZE - 1); |
| 531 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
| 532 | res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages); |
| 533 | if (unlikely(res < 0)) |
| 534 | return res; |
| 535 | return (res == n ? len : res * PAGE_SIZE) - *start; |
| 536 | 0;}),({ |
| 537 | /* can't be more than PAGE_SIZE */ |
| 538 | *start = v.bv_offset; |
| 539 | get_page(*pages = v.bv_page); |
| 540 | return v.bv_len; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 541 | }),({ |
| 542 | return -EFAULT; |
Al Viro | e5393fa | 2014-11-27 14:12:09 -0500 | [diff] [blame] | 543 | }) |
| 544 | ) |
| 545 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 546 | } |
| 547 | EXPORT_SYMBOL(iov_iter_get_pages); |
| 548 | |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 549 | static struct page **get_pages_array(size_t n) |
| 550 | { |
| 551 | struct page **p = kmalloc(n * sizeof(struct page *), GFP_KERNEL); |
| 552 | if (!p) |
| 553 | p = vmalloc(n * sizeof(struct page *)); |
| 554 | return p; |
| 555 | } |
| 556 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 557 | ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, |
| 558 | struct page ***pages, size_t maxsize, |
| 559 | size_t *start) |
| 560 | { |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 561 | struct page **p; |
| 562 | |
| 563 | if (maxsize > i->count) |
| 564 | maxsize = i->count; |
| 565 | |
| 566 | if (!maxsize) |
| 567 | return 0; |
| 568 | |
| 569 | iterate_all_kinds(i, maxsize, v, ({ |
| 570 | unsigned long addr = (unsigned long)v.iov_base; |
| 571 | size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1)); |
| 572 | int n; |
| 573 | int res; |
| 574 | |
| 575 | addr &= ~(PAGE_SIZE - 1); |
| 576 | n = DIV_ROUND_UP(len, PAGE_SIZE); |
| 577 | p = get_pages_array(n); |
| 578 | if (!p) |
| 579 | return -ENOMEM; |
| 580 | res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p); |
| 581 | if (unlikely(res < 0)) { |
| 582 | kvfree(p); |
| 583 | return res; |
| 584 | } |
| 585 | *pages = p; |
| 586 | return (res == n ? len : res * PAGE_SIZE) - *start; |
| 587 | 0;}),({ |
| 588 | /* can't be more than PAGE_SIZE */ |
| 589 | *start = v.bv_offset; |
| 590 | *pages = p = get_pages_array(1); |
| 591 | if (!p) |
| 592 | return -ENOMEM; |
| 593 | get_page(*p = v.bv_page); |
| 594 | return v.bv_len; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 595 | }),({ |
| 596 | return -EFAULT; |
Al Viro | 1b17f1f | 2014-11-27 14:14:31 -0500 | [diff] [blame] | 597 | }) |
| 598 | ) |
| 599 | return 0; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 600 | } |
| 601 | EXPORT_SYMBOL(iov_iter_get_pages_alloc); |
| 602 | |
Al Viro | a604ec7 | 2014-11-24 01:08:00 -0500 | [diff] [blame] | 603 | size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, |
| 604 | struct iov_iter *i) |
| 605 | { |
| 606 | char *to = addr; |
| 607 | __wsum sum, next; |
| 608 | size_t off = 0; |
| 609 | if (unlikely(bytes > i->count)) |
| 610 | bytes = i->count; |
| 611 | |
| 612 | if (unlikely(!bytes)) |
| 613 | return 0; |
| 614 | |
| 615 | sum = *csum; |
| 616 | iterate_and_advance(i, bytes, v, ({ |
| 617 | int err = 0; |
| 618 | next = csum_and_copy_from_user(v.iov_base, |
| 619 | (to += v.iov_len) - v.iov_len, |
| 620 | v.iov_len, 0, &err); |
| 621 | if (!err) { |
| 622 | sum = csum_block_add(sum, next, off); |
| 623 | off += v.iov_len; |
| 624 | } |
| 625 | err ? v.iov_len : 0; |
| 626 | }), ({ |
| 627 | char *p = kmap_atomic(v.bv_page); |
| 628 | next = csum_partial_copy_nocheck(p + v.bv_offset, |
| 629 | (to += v.bv_len) - v.bv_len, |
| 630 | v.bv_len, 0); |
| 631 | kunmap_atomic(p); |
| 632 | sum = csum_block_add(sum, next, off); |
| 633 | off += v.bv_len; |
| 634 | }),({ |
| 635 | next = csum_partial_copy_nocheck(v.iov_base, |
| 636 | (to += v.iov_len) - v.iov_len, |
| 637 | v.iov_len, 0); |
| 638 | sum = csum_block_add(sum, next, off); |
| 639 | off += v.iov_len; |
| 640 | }) |
| 641 | ) |
| 642 | *csum = sum; |
| 643 | return bytes; |
| 644 | } |
| 645 | EXPORT_SYMBOL(csum_and_copy_from_iter); |
| 646 | |
| 647 | size_t csum_and_copy_to_iter(void *addr, size_t bytes, __wsum *csum, |
| 648 | struct iov_iter *i) |
| 649 | { |
| 650 | char *from = addr; |
| 651 | __wsum sum, next; |
| 652 | size_t off = 0; |
| 653 | if (unlikely(bytes > i->count)) |
| 654 | bytes = i->count; |
| 655 | |
| 656 | if (unlikely(!bytes)) |
| 657 | return 0; |
| 658 | |
| 659 | sum = *csum; |
| 660 | iterate_and_advance(i, bytes, v, ({ |
| 661 | int err = 0; |
| 662 | next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len, |
| 663 | v.iov_base, |
| 664 | v.iov_len, 0, &err); |
| 665 | if (!err) { |
| 666 | sum = csum_block_add(sum, next, off); |
| 667 | off += v.iov_len; |
| 668 | } |
| 669 | err ? v.iov_len : 0; |
| 670 | }), ({ |
| 671 | char *p = kmap_atomic(v.bv_page); |
| 672 | next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len, |
| 673 | p + v.bv_offset, |
| 674 | v.bv_len, 0); |
| 675 | kunmap_atomic(p); |
| 676 | sum = csum_block_add(sum, next, off); |
| 677 | off += v.bv_len; |
| 678 | }),({ |
| 679 | next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len, |
| 680 | v.iov_base, |
| 681 | v.iov_len, 0); |
| 682 | sum = csum_block_add(sum, next, off); |
| 683 | off += v.iov_len; |
| 684 | }) |
| 685 | ) |
| 686 | *csum = sum; |
| 687 | return bytes; |
| 688 | } |
| 689 | EXPORT_SYMBOL(csum_and_copy_to_iter); |
| 690 | |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 691 | int iov_iter_npages(const struct iov_iter *i, int maxpages) |
| 692 | { |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 693 | size_t size = i->count; |
| 694 | int npages = 0; |
| 695 | |
| 696 | if (!size) |
| 697 | return 0; |
| 698 | |
| 699 | iterate_all_kinds(i, size, v, ({ |
| 700 | unsigned long p = (unsigned long)v.iov_base; |
| 701 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) |
| 702 | - p / PAGE_SIZE; |
| 703 | if (npages >= maxpages) |
| 704 | return maxpages; |
| 705 | 0;}),({ |
| 706 | npages++; |
| 707 | if (npages >= maxpages) |
| 708 | return maxpages; |
Al Viro | a280455 | 2014-11-27 14:48:42 -0500 | [diff] [blame] | 709 | }),({ |
| 710 | unsigned long p = (unsigned long)v.iov_base; |
| 711 | npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE) |
| 712 | - p / PAGE_SIZE; |
| 713 | if (npages >= maxpages) |
| 714 | return maxpages; |
Al Viro | e0f2dc4 | 2014-11-27 14:09:46 -0500 | [diff] [blame] | 715 | }) |
| 716 | ) |
| 717 | return npages; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 718 | } |
Al Viro | f67da30 | 2014-03-19 01:16:16 -0400 | [diff] [blame] | 719 | EXPORT_SYMBOL(iov_iter_npages); |