blob: d67c8288d95dba435bf608b6258c3168c8206d7a [file] [log] [blame]
Al Viro4f18cd32014-02-05 19:11:33 -05001#include <linux/export.h>
2#include <linux/uio.h>
3#include <linux/pagemap.h>
Al Viro91f79c42014-03-21 04:58:33 -04004#include <linux/slab.h>
5#include <linux/vmalloc.h>
Al Viroa604ec72014-11-24 01:08:00 -05006#include <net/checksum.h>
Al Viro4f18cd32014-02-05 19:11:33 -05007
Al Viro04a31162014-11-27 13:51:41 -05008#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 Viroa2804552014-11-27 14:48:42 -050036#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
Ming Lei1bdc76a2016-05-30 21:34:32 +080059#define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
60 struct bvec_iter __start; \
61 __start.bi_size = n; \
62 __start.bi_bvec_done = skip; \
63 __start.bi_idx = 0; \
64 for_each_bvec(__v, i->bvec, __bi, __start) { \
65 if (!__v.bv_len) \
Al Viro04a31162014-11-27 13:51:41 -050066 continue; \
Al Viro04a31162014-11-27 13:51:41 -050067 (void)(STEP); \
Al Viro04a31162014-11-27 13:51:41 -050068 } \
Al Viro04a31162014-11-27 13:51:41 -050069}
70
Al Viroa2804552014-11-27 14:48:42 -050071#define iterate_all_kinds(i, n, v, I, B, K) { \
Al Viro04a31162014-11-27 13:51:41 -050072 size_t skip = i->iov_offset; \
73 if (unlikely(i->type & ITER_BVEC)) { \
Al Viro04a31162014-11-27 13:51:41 -050074 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +080075 struct bvec_iter __bi; \
76 iterate_bvec(i, n, v, __bi, skip, (B)) \
Al Viroa2804552014-11-27 14:48:42 -050077 } else if (unlikely(i->type & ITER_KVEC)) { \
78 const struct kvec *kvec; \
79 struct kvec v; \
80 iterate_kvec(i, n, v, kvec, skip, (K)) \
Al Viro04a31162014-11-27 13:51:41 -050081 } else { \
82 const struct iovec *iov; \
83 struct iovec v; \
84 iterate_iovec(i, n, v, iov, skip, (I)) \
85 } \
86}
87
Al Viroa2804552014-11-27 14:48:42 -050088#define iterate_and_advance(i, n, v, I, B, K) { \
Al Virodd254f52016-05-09 11:54:48 -040089 if (unlikely(i->count < n)) \
90 n = i->count; \
Al Viro19f18452016-05-25 17:36:19 -040091 if (i->count) { \
Al Virodd254f52016-05-09 11:54:48 -040092 size_t skip = i->iov_offset; \
93 if (unlikely(i->type & ITER_BVEC)) { \
Ming Lei1bdc76a2016-05-30 21:34:32 +080094 const struct bio_vec *bvec = i->bvec; \
Al Virodd254f52016-05-09 11:54:48 -040095 struct bio_vec v; \
Ming Lei1bdc76a2016-05-30 21:34:32 +080096 struct bvec_iter __bi; \
97 iterate_bvec(i, n, v, __bi, skip, (B)) \
98 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
99 i->nr_segs -= i->bvec - bvec; \
100 skip = __bi.bi_bvec_done; \
Al Virodd254f52016-05-09 11:54:48 -0400101 } else if (unlikely(i->type & ITER_KVEC)) { \
102 const struct kvec *kvec; \
103 struct kvec v; \
104 iterate_kvec(i, n, v, kvec, skip, (K)) \
105 if (skip == kvec->iov_len) { \
106 kvec++; \
107 skip = 0; \
108 } \
109 i->nr_segs -= kvec - i->kvec; \
110 i->kvec = kvec; \
111 } else { \
112 const struct iovec *iov; \
113 struct iovec v; \
114 iterate_iovec(i, n, v, iov, skip, (I)) \
115 if (skip == iov->iov_len) { \
116 iov++; \
117 skip = 0; \
118 } \
119 i->nr_segs -= iov - i->iov; \
120 i->iov = iov; \
Al Viro7ce2a912014-11-27 13:59:45 -0500121 } \
Al Virodd254f52016-05-09 11:54:48 -0400122 i->count -= n; \
123 i->iov_offset = skip; \
Al Viro7ce2a912014-11-27 13:59:45 -0500124 } \
Al Viro7ce2a912014-11-27 13:59:45 -0500125}
126
Al Viro62a80672014-04-04 23:12:29 -0400127static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Viro4f18cd32014-02-05 19:11:33 -0500128 struct iov_iter *i)
129{
130 size_t skip, copy, left, wanted;
131 const struct iovec *iov;
132 char __user *buf;
133 void *kaddr, *from;
134
135 if (unlikely(bytes > i->count))
136 bytes = i->count;
137
138 if (unlikely(!bytes))
139 return 0;
140
141 wanted = bytes;
142 iov = i->iov;
143 skip = i->iov_offset;
144 buf = iov->iov_base + skip;
145 copy = min(bytes, iov->iov_len - skip);
146
147 if (!fault_in_pages_writeable(buf, copy)) {
148 kaddr = kmap_atomic(page);
149 from = kaddr + offset;
150
151 /* first chunk, usually the only one */
152 left = __copy_to_user_inatomic(buf, from, copy);
153 copy -= left;
154 skip += copy;
155 from += copy;
156 bytes -= copy;
157
158 while (unlikely(!left && bytes)) {
159 iov++;
160 buf = iov->iov_base;
161 copy = min(bytes, iov->iov_len);
162 left = __copy_to_user_inatomic(buf, from, copy);
163 copy -= left;
164 skip = copy;
165 from += copy;
166 bytes -= copy;
167 }
168 if (likely(!bytes)) {
169 kunmap_atomic(kaddr);
170 goto done;
171 }
172 offset = from - kaddr;
173 buf += copy;
174 kunmap_atomic(kaddr);
175 copy = min(bytes, iov->iov_len - skip);
176 }
177 /* Too bad - revert to non-atomic kmap */
178 kaddr = kmap(page);
179 from = kaddr + offset;
180 left = __copy_to_user(buf, from, copy);
181 copy -= left;
182 skip += copy;
183 from += copy;
184 bytes -= copy;
185 while (unlikely(!left && bytes)) {
186 iov++;
187 buf = iov->iov_base;
188 copy = min(bytes, iov->iov_len);
189 left = __copy_to_user(buf, from, copy);
190 copy -= left;
191 skip = copy;
192 from += copy;
193 bytes -= copy;
194 }
195 kunmap(page);
196done:
Al Viro81055e52014-04-04 19:23:46 -0400197 if (skip == iov->iov_len) {
198 iov++;
199 skip = 0;
200 }
Al Viro4f18cd32014-02-05 19:11:33 -0500201 i->count -= wanted - bytes;
202 i->nr_segs -= iov - i->iov;
203 i->iov = iov;
204 i->iov_offset = skip;
205 return wanted - bytes;
206}
Al Viro4f18cd32014-02-05 19:11:33 -0500207
Al Viro62a80672014-04-04 23:12:29 -0400208static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
Al Virof0d1bec2014-04-03 15:05:18 -0400209 struct iov_iter *i)
210{
211 size_t skip, copy, left, wanted;
212 const struct iovec *iov;
213 char __user *buf;
214 void *kaddr, *to;
215
216 if (unlikely(bytes > i->count))
217 bytes = i->count;
218
219 if (unlikely(!bytes))
220 return 0;
221
222 wanted = bytes;
223 iov = i->iov;
224 skip = i->iov_offset;
225 buf = iov->iov_base + skip;
226 copy = min(bytes, iov->iov_len - skip);
227
228 if (!fault_in_pages_readable(buf, copy)) {
229 kaddr = kmap_atomic(page);
230 to = kaddr + offset;
231
232 /* first chunk, usually the only one */
233 left = __copy_from_user_inatomic(to, buf, copy);
234 copy -= left;
235 skip += copy;
236 to += copy;
237 bytes -= copy;
238
239 while (unlikely(!left && bytes)) {
240 iov++;
241 buf = iov->iov_base;
242 copy = min(bytes, iov->iov_len);
243 left = __copy_from_user_inatomic(to, buf, copy);
244 copy -= left;
245 skip = copy;
246 to += copy;
247 bytes -= copy;
248 }
249 if (likely(!bytes)) {
250 kunmap_atomic(kaddr);
251 goto done;
252 }
253 offset = to - kaddr;
254 buf += copy;
255 kunmap_atomic(kaddr);
256 copy = min(bytes, iov->iov_len - skip);
257 }
258 /* Too bad - revert to non-atomic kmap */
259 kaddr = kmap(page);
260 to = kaddr + offset;
261 left = __copy_from_user(to, buf, copy);
262 copy -= left;
263 skip += copy;
264 to += copy;
265 bytes -= copy;
266 while (unlikely(!left && bytes)) {
267 iov++;
268 buf = iov->iov_base;
269 copy = min(bytes, iov->iov_len);
270 left = __copy_from_user(to, buf, copy);
271 copy -= left;
272 skip = copy;
273 to += copy;
274 bytes -= copy;
275 }
276 kunmap(page);
277done:
Al Viro81055e52014-04-04 19:23:46 -0400278 if (skip == iov->iov_len) {
279 iov++;
280 skip = 0;
281 }
Al Virof0d1bec2014-04-03 15:05:18 -0400282 i->count -= wanted - bytes;
283 i->nr_segs -= iov - i->iov;
284 i->iov = iov;
285 i->iov_offset = skip;
286 return wanted - bytes;
287}
Al Virof0d1bec2014-04-03 15:05:18 -0400288
Al Viro4f18cd32014-02-05 19:11:33 -0500289/*
290 * Fault in the first iovec of the given iov_iter, to a maximum length
291 * of bytes. Returns 0 on success, or non-zero if the memory could not be
292 * accessed (ie. because it is an invalid address).
293 *
294 * writev-intensive code may want this to prefault several iovecs -- that
295 * would be possible (callers must not rely on the fact that _only_ the
296 * first iovec will be faulted with the current implementation).
297 */
298int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
299{
Al Viroa2804552014-11-27 14:48:42 -0500300 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
Al Viro62a80672014-04-04 23:12:29 -0400301 char __user *buf = i->iov->iov_base + i->iov_offset;
302 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
303 return fault_in_pages_readable(buf, bytes);
304 }
305 return 0;
Al Viro4f18cd32014-02-05 19:11:33 -0500306}
307EXPORT_SYMBOL(iov_iter_fault_in_readable);
308
Anton Altaparmakov171a0202015-03-11 10:43:31 -0400309/*
310 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
311 * bytes. For each iovec, fault in each page that constitutes the iovec.
312 *
313 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
314 * because it is an invalid address).
315 */
316int iov_iter_fault_in_multipages_readable(struct iov_iter *i, size_t bytes)
317{
318 size_t skip = i->iov_offset;
319 const struct iovec *iov;
320 int err;
321 struct iovec v;
322
323 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
324 iterate_iovec(i, bytes, v, iov, skip, ({
325 err = fault_in_multipages_readable(v.iov_base,
326 v.iov_len);
327 if (unlikely(err))
328 return err;
329 0;}))
330 }
331 return 0;
332}
333EXPORT_SYMBOL(iov_iter_fault_in_multipages_readable);
334
Al Viro71d8e532014-03-05 19:28:09 -0500335void iov_iter_init(struct iov_iter *i, int direction,
336 const struct iovec *iov, unsigned long nr_segs,
337 size_t count)
338{
339 /* It will get better. Eventually... */
Al Viroa2804552014-11-27 14:48:42 -0500340 if (segment_eq(get_fs(), KERNEL_DS)) {
Al Viro62a80672014-04-04 23:12:29 -0400341 direction |= ITER_KVEC;
Al Viroa2804552014-11-27 14:48:42 -0500342 i->type = direction;
343 i->kvec = (struct kvec *)iov;
344 } else {
345 i->type = direction;
346 i->iov = iov;
347 }
Al Viro71d8e532014-03-05 19:28:09 -0500348 i->nr_segs = nr_segs;
349 i->iov_offset = 0;
350 i->count = count;
351}
352EXPORT_SYMBOL(iov_iter_init);
Al Viro7b2c99d2014-03-15 04:05:57 -0400353
Al Viro62a80672014-04-04 23:12:29 -0400354static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
355{
356 char *from = kmap_atomic(page);
357 memcpy(to, from + offset, len);
358 kunmap_atomic(from);
359}
360
Al Viro36f7a8a2015-12-06 16:49:22 -0500361static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
Al Viro62a80672014-04-04 23:12:29 -0400362{
363 char *to = kmap_atomic(page);
364 memcpy(to + offset, from, len);
365 kunmap_atomic(to);
366}
367
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400368static void memzero_page(struct page *page, size_t offset, size_t len)
369{
370 char *addr = kmap_atomic(page);
371 memset(addr + offset, 0, len);
372 kunmap_atomic(addr);
373}
374
Al Viro36f7a8a2015-12-06 16:49:22 -0500375size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
Al Viro62a80672014-04-04 23:12:29 -0400376{
Al Viro36f7a8a2015-12-06 16:49:22 -0500377 const char *from = addr;
Al Viro3d4d3e42014-11-27 14:28:06 -0500378 iterate_and_advance(i, bytes, v,
379 __copy_to_user(v.iov_base, (from += v.iov_len) - v.iov_len,
380 v.iov_len),
381 memcpy_to_page(v.bv_page, v.bv_offset,
Al Viroa2804552014-11-27 14:48:42 -0500382 (from += v.bv_len) - v.bv_len, v.bv_len),
383 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
Al Viro3d4d3e42014-11-27 14:28:06 -0500384 )
Al Viro62a80672014-04-04 23:12:29 -0400385
Al Viro3d4d3e42014-11-27 14:28:06 -0500386 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400387}
388EXPORT_SYMBOL(copy_to_iter);
389
390size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
391{
Al Viro0dbca9a2014-11-27 14:26:43 -0500392 char *to = addr;
Al Viro0dbca9a2014-11-27 14:26:43 -0500393 iterate_and_advance(i, bytes, v,
394 __copy_from_user((to += v.iov_len) - v.iov_len, v.iov_base,
395 v.iov_len),
396 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500397 v.bv_offset, v.bv_len),
398 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro0dbca9a2014-11-27 14:26:43 -0500399 )
400
401 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400402}
403EXPORT_SYMBOL(copy_from_iter);
404
Al Viroaa583092014-11-27 20:27:08 -0500405size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
406{
407 char *to = addr;
Al Viroaa583092014-11-27 20:27:08 -0500408 iterate_and_advance(i, bytes, v,
409 __copy_from_user_nocache((to += v.iov_len) - v.iov_len,
410 v.iov_base, v.iov_len),
411 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
412 v.bv_offset, v.bv_len),
413 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
414 )
415
416 return bytes;
417}
418EXPORT_SYMBOL(copy_from_iter_nocache);
419
Al Virod2715242014-11-27 14:22:37 -0500420size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
421 struct iov_iter *i)
422{
423 if (i->type & (ITER_BVEC|ITER_KVEC)) {
424 void *kaddr = kmap_atomic(page);
425 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
426 kunmap_atomic(kaddr);
427 return wanted;
428 } else
429 return copy_page_to_iter_iovec(page, offset, bytes, i);
430}
431EXPORT_SYMBOL(copy_page_to_iter);
432
433size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
434 struct iov_iter *i)
435{
Al Viroa2804552014-11-27 14:48:42 -0500436 if (i->type & (ITER_BVEC|ITER_KVEC)) {
Al Virod2715242014-11-27 14:22:37 -0500437 void *kaddr = kmap_atomic(page);
438 size_t wanted = copy_from_iter(kaddr + offset, bytes, i);
439 kunmap_atomic(kaddr);
440 return wanted;
441 } else
442 return copy_page_from_iter_iovec(page, offset, bytes, i);
443}
444EXPORT_SYMBOL(copy_page_from_iter);
445
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400446size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
447{
Al Viro8442fa42014-11-27 14:18:54 -0500448 iterate_and_advance(i, bytes, v,
449 __clear_user(v.iov_base, v.iov_len),
Al Viroa2804552014-11-27 14:48:42 -0500450 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
451 memset(v.iov_base, 0, v.iov_len)
Al Viro8442fa42014-11-27 14:18:54 -0500452 )
453
454 return bytes;
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400455}
456EXPORT_SYMBOL(iov_iter_zero);
457
Al Viro62a80672014-04-04 23:12:29 -0400458size_t iov_iter_copy_from_user_atomic(struct page *page,
459 struct iov_iter *i, unsigned long offset, size_t bytes)
460{
Al Viro04a31162014-11-27 13:51:41 -0500461 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
462 iterate_all_kinds(i, bytes, v,
463 __copy_from_user_inatomic((p += v.iov_len) - v.iov_len,
464 v.iov_base, v.iov_len),
465 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
Al Viroa2804552014-11-27 14:48:42 -0500466 v.bv_offset, v.bv_len),
467 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
Al Viro04a31162014-11-27 13:51:41 -0500468 )
469 kunmap_atomic(kaddr);
470 return bytes;
Al Viro62a80672014-04-04 23:12:29 -0400471}
472EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
473
474void iov_iter_advance(struct iov_iter *i, size_t size)
475{
Al Viroa2804552014-11-27 14:48:42 -0500476 iterate_and_advance(i, size, v, 0, 0, 0)
Al Viro62a80672014-04-04 23:12:29 -0400477}
478EXPORT_SYMBOL(iov_iter_advance);
479
480/*
481 * Return the count of just the current iov_iter segment.
482 */
483size_t iov_iter_single_seg_count(const struct iov_iter *i)
484{
485 if (i->nr_segs == 1)
486 return i->count;
487 else if (i->type & ITER_BVEC)
Al Viro62a80672014-04-04 23:12:29 -0400488 return min(i->count, i->bvec->bv_len - i->iov_offset);
Paul Mackerrasad0eab92014-11-13 20:15:23 +1100489 else
490 return min(i->count, i->iov->iov_len - i->iov_offset);
Al Viro62a80672014-04-04 23:12:29 -0400491}
492EXPORT_SYMBOL(iov_iter_single_seg_count);
493
Al Viroabb78f82014-11-24 14:46:11 -0500494void iov_iter_kvec(struct iov_iter *i, int direction,
Al Viro05afcb72015-01-23 01:08:07 -0500495 const struct kvec *kvec, unsigned long nr_segs,
Al Viroabb78f82014-11-24 14:46:11 -0500496 size_t count)
497{
498 BUG_ON(!(direction & ITER_KVEC));
499 i->type = direction;
Al Viro05afcb72015-01-23 01:08:07 -0500500 i->kvec = kvec;
Al Viroabb78f82014-11-24 14:46:11 -0500501 i->nr_segs = nr_segs;
502 i->iov_offset = 0;
503 i->count = count;
504}
505EXPORT_SYMBOL(iov_iter_kvec);
506
Al Viro05afcb72015-01-23 01:08:07 -0500507void iov_iter_bvec(struct iov_iter *i, int direction,
508 const struct bio_vec *bvec, unsigned long nr_segs,
509 size_t count)
510{
511 BUG_ON(!(direction & ITER_BVEC));
512 i->type = direction;
513 i->bvec = bvec;
514 i->nr_segs = nr_segs;
515 i->iov_offset = 0;
516 i->count = count;
517}
518EXPORT_SYMBOL(iov_iter_bvec);
519
Al Viro62a80672014-04-04 23:12:29 -0400520unsigned long iov_iter_alignment(const struct iov_iter *i)
521{
Al Viro04a31162014-11-27 13:51:41 -0500522 unsigned long res = 0;
523 size_t size = i->count;
524
525 if (!size)
526 return 0;
527
528 iterate_all_kinds(i, size, v,
529 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
Al Viroa2804552014-11-27 14:48:42 -0500530 res |= v.bv_offset | v.bv_len,
531 res |= (unsigned long)v.iov_base | v.iov_len
Al Viro04a31162014-11-27 13:51:41 -0500532 )
533 return res;
Al Viro62a80672014-04-04 23:12:29 -0400534}
535EXPORT_SYMBOL(iov_iter_alignment);
536
Al Viro357f4352016-04-08 19:05:19 -0400537unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
538{
539 unsigned long res = 0;
540 size_t size = i->count;
541 if (!size)
542 return 0;
543
544 iterate_all_kinds(i, size, v,
545 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
546 (size != v.iov_len ? size : 0), 0),
547 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
548 (size != v.bv_len ? size : 0)),
549 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
550 (size != v.iov_len ? size : 0))
551 );
552 return res;
553}
554EXPORT_SYMBOL(iov_iter_gap_alignment);
555
Al Viro62a80672014-04-04 23:12:29 -0400556ssize_t iov_iter_get_pages(struct iov_iter *i,
Miklos Szeredi2c809292014-09-24 17:09:11 +0200557 struct page **pages, size_t maxsize, unsigned maxpages,
Al Viro62a80672014-04-04 23:12:29 -0400558 size_t *start)
559{
Al Viroe5393fa2014-11-27 14:12:09 -0500560 if (maxsize > i->count)
561 maxsize = i->count;
562
563 if (!maxsize)
564 return 0;
565
566 iterate_all_kinds(i, maxsize, v, ({
567 unsigned long addr = (unsigned long)v.iov_base;
568 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
569 int n;
570 int res;
571
572 if (len > maxpages * PAGE_SIZE)
573 len = maxpages * PAGE_SIZE;
574 addr &= ~(PAGE_SIZE - 1);
575 n = DIV_ROUND_UP(len, PAGE_SIZE);
576 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages);
577 if (unlikely(res < 0))
578 return res;
579 return (res == n ? len : res * PAGE_SIZE) - *start;
580 0;}),({
581 /* can't be more than PAGE_SIZE */
582 *start = v.bv_offset;
583 get_page(*pages = v.bv_page);
584 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -0500585 }),({
586 return -EFAULT;
Al Viroe5393fa2014-11-27 14:12:09 -0500587 })
588 )
589 return 0;
Al Viro62a80672014-04-04 23:12:29 -0400590}
591EXPORT_SYMBOL(iov_iter_get_pages);
592
Al Viro1b17f1f2014-11-27 14:14:31 -0500593static struct page **get_pages_array(size_t n)
594{
595 struct page **p = kmalloc(n * sizeof(struct page *), GFP_KERNEL);
596 if (!p)
597 p = vmalloc(n * sizeof(struct page *));
598 return p;
599}
600
Al Viro62a80672014-04-04 23:12:29 -0400601ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
602 struct page ***pages, size_t maxsize,
603 size_t *start)
604{
Al Viro1b17f1f2014-11-27 14:14:31 -0500605 struct page **p;
606
607 if (maxsize > i->count)
608 maxsize = i->count;
609
610 if (!maxsize)
611 return 0;
612
613 iterate_all_kinds(i, maxsize, v, ({
614 unsigned long addr = (unsigned long)v.iov_base;
615 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
616 int n;
617 int res;
618
619 addr &= ~(PAGE_SIZE - 1);
620 n = DIV_ROUND_UP(len, PAGE_SIZE);
621 p = get_pages_array(n);
622 if (!p)
623 return -ENOMEM;
624 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p);
625 if (unlikely(res < 0)) {
626 kvfree(p);
627 return res;
628 }
629 *pages = p;
630 return (res == n ? len : res * PAGE_SIZE) - *start;
631 0;}),({
632 /* can't be more than PAGE_SIZE */
633 *start = v.bv_offset;
634 *pages = p = get_pages_array(1);
635 if (!p)
636 return -ENOMEM;
637 get_page(*p = v.bv_page);
638 return v.bv_len;
Al Viroa2804552014-11-27 14:48:42 -0500639 }),({
640 return -EFAULT;
Al Viro1b17f1f2014-11-27 14:14:31 -0500641 })
642 )
643 return 0;
Al Viro62a80672014-04-04 23:12:29 -0400644}
645EXPORT_SYMBOL(iov_iter_get_pages_alloc);
646
Al Viroa604ec72014-11-24 01:08:00 -0500647size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
648 struct iov_iter *i)
649{
650 char *to = addr;
651 __wsum sum, next;
652 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -0500653 sum = *csum;
654 iterate_and_advance(i, bytes, v, ({
655 int err = 0;
656 next = csum_and_copy_from_user(v.iov_base,
657 (to += v.iov_len) - v.iov_len,
658 v.iov_len, 0, &err);
659 if (!err) {
660 sum = csum_block_add(sum, next, off);
661 off += v.iov_len;
662 }
663 err ? v.iov_len : 0;
664 }), ({
665 char *p = kmap_atomic(v.bv_page);
666 next = csum_partial_copy_nocheck(p + v.bv_offset,
667 (to += v.bv_len) - v.bv_len,
668 v.bv_len, 0);
669 kunmap_atomic(p);
670 sum = csum_block_add(sum, next, off);
671 off += v.bv_len;
672 }),({
673 next = csum_partial_copy_nocheck(v.iov_base,
674 (to += v.iov_len) - v.iov_len,
675 v.iov_len, 0);
676 sum = csum_block_add(sum, next, off);
677 off += v.iov_len;
678 })
679 )
680 *csum = sum;
681 return bytes;
682}
683EXPORT_SYMBOL(csum_and_copy_from_iter);
684
Al Viro36f7a8a2015-12-06 16:49:22 -0500685size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
Al Viroa604ec72014-11-24 01:08:00 -0500686 struct iov_iter *i)
687{
Al Viro36f7a8a2015-12-06 16:49:22 -0500688 const char *from = addr;
Al Viroa604ec72014-11-24 01:08:00 -0500689 __wsum sum, next;
690 size_t off = 0;
Al Viroa604ec72014-11-24 01:08:00 -0500691 sum = *csum;
692 iterate_and_advance(i, bytes, v, ({
693 int err = 0;
694 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
695 v.iov_base,
696 v.iov_len, 0, &err);
697 if (!err) {
698 sum = csum_block_add(sum, next, off);
699 off += v.iov_len;
700 }
701 err ? v.iov_len : 0;
702 }), ({
703 char *p = kmap_atomic(v.bv_page);
704 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
705 p + v.bv_offset,
706 v.bv_len, 0);
707 kunmap_atomic(p);
708 sum = csum_block_add(sum, next, off);
709 off += v.bv_len;
710 }),({
711 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
712 v.iov_base,
713 v.iov_len, 0);
714 sum = csum_block_add(sum, next, off);
715 off += v.iov_len;
716 })
717 )
718 *csum = sum;
719 return bytes;
720}
721EXPORT_SYMBOL(csum_and_copy_to_iter);
722
Al Viro62a80672014-04-04 23:12:29 -0400723int iov_iter_npages(const struct iov_iter *i, int maxpages)
724{
Al Viroe0f2dc42014-11-27 14:09:46 -0500725 size_t size = i->count;
726 int npages = 0;
727
728 if (!size)
729 return 0;
730
731 iterate_all_kinds(i, size, v, ({
732 unsigned long p = (unsigned long)v.iov_base;
733 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
734 - p / PAGE_SIZE;
735 if (npages >= maxpages)
736 return maxpages;
737 0;}),({
738 npages++;
739 if (npages >= maxpages)
740 return maxpages;
Al Viroa2804552014-11-27 14:48:42 -0500741 }),({
742 unsigned long p = (unsigned long)v.iov_base;
743 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
744 - p / PAGE_SIZE;
745 if (npages >= maxpages)
746 return maxpages;
Al Viroe0f2dc42014-11-27 14:09:46 -0500747 })
748 )
749 return npages;
Al Viro62a80672014-04-04 23:12:29 -0400750}
Al Virof67da302014-03-19 01:16:16 -0400751EXPORT_SYMBOL(iov_iter_npages);
Al Viro4b8164b2015-01-31 20:08:47 -0500752
753const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
754{
755 *new = *old;
756 if (new->type & ITER_BVEC)
757 return new->bvec = kmemdup(new->bvec,
758 new->nr_segs * sizeof(struct bio_vec),
759 flags);
760 else
761 /* iovec and kvec have identical layout */
762 return new->iov = kmemdup(new->iov,
763 new->nr_segs * sizeof(struct iovec),
764 flags);
765}
766EXPORT_SYMBOL(dup_iter);
Al Virobc917be2015-03-21 17:45:43 -0400767
768int import_iovec(int type, const struct iovec __user * uvector,
769 unsigned nr_segs, unsigned fast_segs,
770 struct iovec **iov, struct iov_iter *i)
771{
772 ssize_t n;
773 struct iovec *p;
774 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
775 *iov, &p);
776 if (n < 0) {
777 if (p != *iov)
778 kfree(p);
779 *iov = NULL;
780 return n;
781 }
782 iov_iter_init(i, type, p, nr_segs, n);
783 *iov = p == *iov ? NULL : p;
784 return 0;
785}
786EXPORT_SYMBOL(import_iovec);
787
788#ifdef CONFIG_COMPAT
789#include <linux/compat.h>
790
791int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
792 unsigned nr_segs, unsigned fast_segs,
793 struct iovec **iov, struct iov_iter *i)
794{
795 ssize_t n;
796 struct iovec *p;
797 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
798 *iov, &p);
799 if (n < 0) {
800 if (p != *iov)
801 kfree(p);
802 *iov = NULL;
803 return n;
804 }
805 iov_iter_init(i, type, p, nr_segs, n);
806 *iov = p == *iov ? NULL : p;
807 return 0;
808}
809#endif
810
811int import_single_range(int rw, void __user *buf, size_t len,
812 struct iovec *iov, struct iov_iter *i)
813{
814 if (len > MAX_RW_COUNT)
815 len = MAX_RW_COUNT;
816 if (unlikely(!access_ok(!rw, buf, len)))
817 return -EFAULT;
818
819 iov->iov_base = buf;
820 iov->iov_len = len;
821 iov_iter_init(i, rw, iov, 1, len);
822 return 0;
823}
Al Viroe1267582015-12-06 20:38:56 -0500824EXPORT_SYMBOL(import_single_range);