1 #include <linux/export.h>
3 #include <linux/pagemap.h>
4 #include <linux/slab.h>
5 #include <linux/vmalloc.h>
6 #include <linux/splice.h>
7 #include <net/checksum.h>
9 #define PIPE_PARANOIA /* for now */
11 #define iterate_iovec(i, n, __v, __p, skip, STEP) { \
15 __v.iov_len = min(n, __p->iov_len - skip); \
16 if (likely(__v.iov_len)) { \
17 __v.iov_base = __p->iov_base + skip; \
19 __v.iov_len -= left; \
20 skip += __v.iov_len; \
25 while (unlikely(!left && n)) { \
27 __v.iov_len = min(n, __p->iov_len); \
28 if (unlikely(!__v.iov_len)) \
30 __v.iov_base = __p->iov_base; \
32 __v.iov_len -= left; \
39 #define iterate_kvec(i, n, __v, __p, skip, STEP) { \
42 __v.iov_len = min(n, __p->iov_len - skip); \
43 if (likely(__v.iov_len)) { \
44 __v.iov_base = __p->iov_base + skip; \
46 skip += __v.iov_len; \
49 while (unlikely(n)) { \
51 __v.iov_len = min(n, __p->iov_len); \
52 if (unlikely(!__v.iov_len)) \
54 __v.iov_base = __p->iov_base; \
62 #define iterate_bvec(i, n, __v, __bi, skip, STEP) { \
63 struct bvec_iter __start; \
64 __start.bi_size = n; \
65 __start.bi_bvec_done = skip; \
67 for_each_bvec(__v, i->bvec, __bi, __start) { \
74 #define iterate_all_kinds(i, n, v, I, B, K) { \
75 size_t skip = i->iov_offset; \
76 if (unlikely(i->type & ITER_BVEC)) { \
78 struct bvec_iter __bi; \
79 iterate_bvec(i, n, v, __bi, skip, (B)) \
80 } else if (unlikely(i->type & ITER_KVEC)) { \
81 const struct kvec *kvec; \
83 iterate_kvec(i, n, v, kvec, skip, (K)) \
85 const struct iovec *iov; \
87 iterate_iovec(i, n, v, iov, skip, (I)) \
91 #define iterate_and_advance(i, n, v, I, B, K) { \
92 if (unlikely(i->count < n)) \
95 size_t skip = i->iov_offset; \
96 if (unlikely(i->type & ITER_BVEC)) { \
97 const struct bio_vec *bvec = i->bvec; \
99 struct bvec_iter __bi; \
100 iterate_bvec(i, n, v, __bi, skip, (B)) \
101 i->bvec = __bvec_iter_bvec(i->bvec, __bi); \
102 i->nr_segs -= i->bvec - bvec; \
103 skip = __bi.bi_bvec_done; \
104 } else if (unlikely(i->type & ITER_KVEC)) { \
105 const struct kvec *kvec; \
107 iterate_kvec(i, n, v, kvec, skip, (K)) \
108 if (skip == kvec->iov_len) { \
112 i->nr_segs -= kvec - i->kvec; \
115 const struct iovec *iov; \
117 iterate_iovec(i, n, v, iov, skip, (I)) \
118 if (skip == iov->iov_len) { \
122 i->nr_segs -= iov - i->iov; \
126 i->iov_offset = skip; \
130 static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes,
133 size_t skip, copy, left, wanted;
134 const struct iovec *iov;
138 if (unlikely(bytes > i->count))
141 if (unlikely(!bytes))
146 skip = i->iov_offset;
147 buf = iov->iov_base + skip;
148 copy = min(bytes, iov->iov_len - skip);
150 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_writeable(buf, copy)) {
151 kaddr = kmap_atomic(page);
152 from = kaddr + offset;
154 /* first chunk, usually the only one */
155 left = __copy_to_user_inatomic(buf, from, copy);
161 while (unlikely(!left && bytes)) {
164 copy = min(bytes, iov->iov_len);
165 left = __copy_to_user_inatomic(buf, from, copy);
171 if (likely(!bytes)) {
172 kunmap_atomic(kaddr);
175 offset = from - kaddr;
177 kunmap_atomic(kaddr);
178 copy = min(bytes, iov->iov_len - skip);
180 /* Too bad - revert to non-atomic kmap */
183 from = kaddr + offset;
184 left = __copy_to_user(buf, from, copy);
189 while (unlikely(!left && bytes)) {
192 copy = min(bytes, iov->iov_len);
193 left = __copy_to_user(buf, from, copy);
202 if (skip == iov->iov_len) {
206 i->count -= wanted - bytes;
207 i->nr_segs -= iov - i->iov;
209 i->iov_offset = skip;
210 return wanted - bytes;
213 static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t bytes,
216 size_t skip, copy, left, wanted;
217 const struct iovec *iov;
221 if (unlikely(bytes > i->count))
224 if (unlikely(!bytes))
229 skip = i->iov_offset;
230 buf = iov->iov_base + skip;
231 copy = min(bytes, iov->iov_len - skip);
233 if (IS_ENABLED(CONFIG_HIGHMEM) && !fault_in_pages_readable(buf, copy)) {
234 kaddr = kmap_atomic(page);
237 /* first chunk, usually the only one */
238 left = __copy_from_user_inatomic(to, buf, copy);
244 while (unlikely(!left && bytes)) {
247 copy = min(bytes, iov->iov_len);
248 left = __copy_from_user_inatomic(to, buf, copy);
254 if (likely(!bytes)) {
255 kunmap_atomic(kaddr);
260 kunmap_atomic(kaddr);
261 copy = min(bytes, iov->iov_len - skip);
263 /* Too bad - revert to non-atomic kmap */
267 left = __copy_from_user(to, buf, copy);
272 while (unlikely(!left && bytes)) {
275 copy = min(bytes, iov->iov_len);
276 left = __copy_from_user(to, buf, copy);
285 if (skip == iov->iov_len) {
289 i->count -= wanted - bytes;
290 i->nr_segs -= iov - i->iov;
292 i->iov_offset = skip;
293 return wanted - bytes;
297 static bool sanity(const struct iov_iter *i)
299 struct pipe_inode_info *pipe = i->pipe;
301 int next = pipe->curbuf + pipe->nrbufs;
303 struct pipe_buffer *p;
304 if (unlikely(!pipe->nrbufs))
305 goto Bad; // pipe must be non-empty
306 if (unlikely(idx != ((next - 1) & (pipe->buffers - 1))))
307 goto Bad; // must be at the last buffer...
309 p = &pipe->bufs[idx];
310 if (unlikely(p->offset + p->len != i->iov_offset))
311 goto Bad; // ... at the end of segment
313 if (idx != (next & (pipe->buffers - 1)))
314 goto Bad; // must be right after the last buffer
318 printk(KERN_ERR "idx = %d, offset = %zd\n", i->idx, i->iov_offset);
319 printk(KERN_ERR "curbuf = %d, nrbufs = %d, buffers = %d\n",
320 pipe->curbuf, pipe->nrbufs, pipe->buffers);
321 for (idx = 0; idx < pipe->buffers; idx++)
322 printk(KERN_ERR "[%p %p %d %d]\n",
324 pipe->bufs[idx].page,
325 pipe->bufs[idx].offset,
326 pipe->bufs[idx].len);
331 #define sanity(i) true
334 static inline int next_idx(int idx, struct pipe_inode_info *pipe)
336 return (idx + 1) & (pipe->buffers - 1);
339 static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes,
342 struct pipe_inode_info *pipe = i->pipe;
343 struct pipe_buffer *buf;
347 if (unlikely(bytes > i->count))
350 if (unlikely(!bytes))
358 buf = &pipe->bufs[idx];
360 if (offset == off && buf->page == page) {
361 /* merge with the last one */
363 i->iov_offset += bytes;
366 idx = next_idx(idx, pipe);
367 buf = &pipe->bufs[idx];
369 if (idx == pipe->curbuf && pipe->nrbufs)
372 buf->ops = &page_cache_pipe_buf_ops;
373 get_page(buf->page = page);
374 buf->offset = offset;
376 i->iov_offset = offset + bytes;
384 * Fault in one or more iovecs of the given iov_iter, to a maximum length of
385 * bytes. For each iovec, fault in each page that constitutes the iovec.
387 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
388 * because it is an invalid address).
390 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
392 size_t skip = i->iov_offset;
393 const struct iovec *iov;
397 if (!(i->type & (ITER_BVEC|ITER_KVEC))) {
398 iterate_iovec(i, bytes, v, iov, skip, ({
399 err = fault_in_multipages_readable(v.iov_base,
407 EXPORT_SYMBOL(iov_iter_fault_in_readable);
409 void iov_iter_init(struct iov_iter *i, int direction,
410 const struct iovec *iov, unsigned long nr_segs,
413 /* It will get better. Eventually... */
414 if (segment_eq(get_fs(), KERNEL_DS)) {
415 direction |= ITER_KVEC;
417 i->kvec = (struct kvec *)iov;
422 i->nr_segs = nr_segs;
426 EXPORT_SYMBOL(iov_iter_init);
428 static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
430 char *from = kmap_atomic(page);
431 memcpy(to, from + offset, len);
435 static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
437 char *to = kmap_atomic(page);
438 memcpy(to + offset, from, len);
442 static void memzero_page(struct page *page, size_t offset, size_t len)
444 char *addr = kmap_atomic(page);
445 memset(addr + offset, 0, len);
449 static inline bool allocated(struct pipe_buffer *buf)
451 return buf->ops == &default_pipe_buf_ops;
454 static inline void data_start(const struct iov_iter *i, int *idxp, size_t *offp)
456 size_t off = i->iov_offset;
458 if (off && (!allocated(&i->pipe->bufs[idx]) || off == PAGE_SIZE)) {
459 idx = next_idx(idx, i->pipe);
466 static size_t push_pipe(struct iov_iter *i, size_t size,
467 int *idxp, size_t *offp)
469 struct pipe_inode_info *pipe = i->pipe;
474 if (unlikely(size > i->count))
480 data_start(i, &idx, &off);
484 left -= PAGE_SIZE - off;
486 pipe->bufs[idx].len += size;
489 pipe->bufs[idx].len = PAGE_SIZE;
490 idx = next_idx(idx, pipe);
492 while (idx != pipe->curbuf || !pipe->nrbufs) {
493 struct page *page = alloc_page(GFP_USER);
497 pipe->bufs[idx].ops = &default_pipe_buf_ops;
498 pipe->bufs[idx].page = page;
499 pipe->bufs[idx].offset = 0;
500 if (left <= PAGE_SIZE) {
501 pipe->bufs[idx].len = left;
504 pipe->bufs[idx].len = PAGE_SIZE;
506 idx = next_idx(idx, pipe);
511 static size_t copy_pipe_to_iter(const void *addr, size_t bytes,
514 struct pipe_inode_info *pipe = i->pipe;
521 bytes = n = push_pipe(i, bytes, &idx, &off);
524 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
525 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
526 memcpy_to_page(pipe->bufs[idx].page, off, addr, chunk);
528 i->iov_offset = off + chunk;
536 size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
538 const char *from = addr;
539 if (unlikely(i->type & ITER_PIPE))
540 return copy_pipe_to_iter(addr, bytes, i);
541 iterate_and_advance(i, bytes, v,
542 __copy_to_user(v.iov_base, (from += v.iov_len) - v.iov_len,
544 memcpy_to_page(v.bv_page, v.bv_offset,
545 (from += v.bv_len) - v.bv_len, v.bv_len),
546 memcpy(v.iov_base, (from += v.iov_len) - v.iov_len, v.iov_len)
551 EXPORT_SYMBOL(copy_to_iter);
553 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
556 if (unlikely(i->type & ITER_PIPE)) {
560 iterate_and_advance(i, bytes, v,
561 __copy_from_user((to += v.iov_len) - v.iov_len, v.iov_base,
563 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
564 v.bv_offset, v.bv_len),
565 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
570 EXPORT_SYMBOL(copy_from_iter);
572 size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
575 if (unlikely(i->type & ITER_PIPE)) {
579 iterate_and_advance(i, bytes, v,
580 __copy_from_user_nocache((to += v.iov_len) - v.iov_len,
581 v.iov_base, v.iov_len),
582 memcpy_from_page((to += v.bv_len) - v.bv_len, v.bv_page,
583 v.bv_offset, v.bv_len),
584 memcpy((to += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
589 EXPORT_SYMBOL(copy_from_iter_nocache);
591 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
594 if (i->type & (ITER_BVEC|ITER_KVEC)) {
595 void *kaddr = kmap_atomic(page);
596 size_t wanted = copy_to_iter(kaddr + offset, bytes, i);
597 kunmap_atomic(kaddr);
599 } else if (likely(!(i->type & ITER_PIPE)))
600 return copy_page_to_iter_iovec(page, offset, bytes, i);
602 return copy_page_to_iter_pipe(page, offset, bytes, i);
604 EXPORT_SYMBOL(copy_page_to_iter);
606 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
609 if (unlikely(i->type & ITER_PIPE)) {
613 if (i->type & (ITER_BVEC|ITER_KVEC)) {
614 void *kaddr = kmap_atomic(page);
615 size_t wanted = copy_from_iter(kaddr + offset, bytes, i);
616 kunmap_atomic(kaddr);
619 return copy_page_from_iter_iovec(page, offset, bytes, i);
621 EXPORT_SYMBOL(copy_page_from_iter);
623 static size_t pipe_zero(size_t bytes, struct iov_iter *i)
625 struct pipe_inode_info *pipe = i->pipe;
632 bytes = n = push_pipe(i, bytes, &idx, &off);
636 for ( ; n; idx = next_idx(idx, pipe), off = 0) {
637 size_t chunk = min_t(size_t, n, PAGE_SIZE - off);
638 memzero_page(pipe->bufs[idx].page, off, chunk);
640 i->iov_offset = off + chunk;
647 size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
649 if (unlikely(i->type & ITER_PIPE))
650 return pipe_zero(bytes, i);
651 iterate_and_advance(i, bytes, v,
652 __clear_user(v.iov_base, v.iov_len),
653 memzero_page(v.bv_page, v.bv_offset, v.bv_len),
654 memset(v.iov_base, 0, v.iov_len)
659 EXPORT_SYMBOL(iov_iter_zero);
661 size_t iov_iter_copy_from_user_atomic(struct page *page,
662 struct iov_iter *i, unsigned long offset, size_t bytes)
664 char *kaddr = kmap_atomic(page), *p = kaddr + offset;
665 if (unlikely(i->type & ITER_PIPE)) {
666 kunmap_atomic(kaddr);
670 iterate_all_kinds(i, bytes, v,
671 __copy_from_user_inatomic((p += v.iov_len) - v.iov_len,
672 v.iov_base, v.iov_len),
673 memcpy_from_page((p += v.bv_len) - v.bv_len, v.bv_page,
674 v.bv_offset, v.bv_len),
675 memcpy((p += v.iov_len) - v.iov_len, v.iov_base, v.iov_len)
677 kunmap_atomic(kaddr);
680 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
682 static void pipe_advance(struct iov_iter *i, size_t size)
684 struct pipe_inode_info *pipe = i->pipe;
685 struct pipe_buffer *buf;
687 size_t off = i->iov_offset;
689 if (unlikely(i->count < size))
693 if (off) /* make it relative to the beginning of buffer */
694 size += off - pipe->bufs[idx].offset;
696 buf = &pipe->bufs[idx];
697 if (size <= buf->len)
700 idx = next_idx(idx, pipe);
704 off = i->iov_offset = buf->offset + size;
707 idx = next_idx(idx, pipe);
709 int unused = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
710 /* [curbuf,unused) is in use. Free [idx,unused) */
711 while (idx != unused) {
712 pipe_buf_release(pipe, &pipe->bufs[idx]);
713 idx = next_idx(idx, pipe);
719 void iov_iter_advance(struct iov_iter *i, size_t size)
721 if (unlikely(i->type & ITER_PIPE)) {
722 pipe_advance(i, size);
725 iterate_and_advance(i, size, v, 0, 0, 0)
727 EXPORT_SYMBOL(iov_iter_advance);
730 * Return the count of just the current iov_iter segment.
732 size_t iov_iter_single_seg_count(const struct iov_iter *i)
734 if (unlikely(i->type & ITER_PIPE))
735 return i->count; // it is a silly place, anyway
738 else if (i->type & ITER_BVEC)
739 return min(i->count, i->bvec->bv_len - i->iov_offset);
741 return min(i->count, i->iov->iov_len - i->iov_offset);
743 EXPORT_SYMBOL(iov_iter_single_seg_count);
745 void iov_iter_kvec(struct iov_iter *i, int direction,
746 const struct kvec *kvec, unsigned long nr_segs,
749 BUG_ON(!(direction & ITER_KVEC));
752 i->nr_segs = nr_segs;
756 EXPORT_SYMBOL(iov_iter_kvec);
758 void iov_iter_bvec(struct iov_iter *i, int direction,
759 const struct bio_vec *bvec, unsigned long nr_segs,
762 BUG_ON(!(direction & ITER_BVEC));
765 i->nr_segs = nr_segs;
769 EXPORT_SYMBOL(iov_iter_bvec);
771 void iov_iter_pipe(struct iov_iter *i, int direction,
772 struct pipe_inode_info *pipe,
775 BUG_ON(direction != ITER_PIPE);
778 i->idx = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
782 EXPORT_SYMBOL(iov_iter_pipe);
784 unsigned long iov_iter_alignment(const struct iov_iter *i)
786 unsigned long res = 0;
787 size_t size = i->count;
792 if (unlikely(i->type & ITER_PIPE)) {
793 if (i->iov_offset && allocated(&i->pipe->bufs[i->idx]))
794 return size | i->iov_offset;
797 iterate_all_kinds(i, size, v,
798 (res |= (unsigned long)v.iov_base | v.iov_len, 0),
799 res |= v.bv_offset | v.bv_len,
800 res |= (unsigned long)v.iov_base | v.iov_len
804 EXPORT_SYMBOL(iov_iter_alignment);
806 unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
808 unsigned long res = 0;
809 size_t size = i->count;
813 if (unlikely(i->type & ITER_PIPE)) {
818 iterate_all_kinds(i, size, v,
819 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
820 (size != v.iov_len ? size : 0), 0),
821 (res |= (!res ? 0 : (unsigned long)v.bv_offset) |
822 (size != v.bv_len ? size : 0)),
823 (res |= (!res ? 0 : (unsigned long)v.iov_base) |
824 (size != v.iov_len ? size : 0))
828 EXPORT_SYMBOL(iov_iter_gap_alignment);
830 static inline size_t __pipe_get_pages(struct iov_iter *i,
836 struct pipe_inode_info *pipe = i->pipe;
837 size_t n = push_pipe(i, maxsize, &idx, start);
843 while (n >= PAGE_SIZE) {
844 get_page(*pages++ = pipe->bufs[idx].page);
845 idx = next_idx(idx, pipe);
852 static ssize_t pipe_get_pages(struct iov_iter *i,
853 struct page **pages, size_t maxsize, unsigned maxpages,
863 data_start(i, &idx, start);
864 /* some of this one + all after this one */
865 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
866 capacity = min(npages,maxpages) * PAGE_SIZE - *start;
868 return __pipe_get_pages(i, min(maxsize, capacity), pages, idx, start);
871 ssize_t iov_iter_get_pages(struct iov_iter *i,
872 struct page **pages, size_t maxsize, unsigned maxpages,
875 if (maxsize > i->count)
881 if (unlikely(i->type & ITER_PIPE))
882 return pipe_get_pages(i, pages, maxsize, maxpages, start);
883 iterate_all_kinds(i, maxsize, v, ({
884 unsigned long addr = (unsigned long)v.iov_base;
885 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
889 if (len > maxpages * PAGE_SIZE)
890 len = maxpages * PAGE_SIZE;
891 addr &= ~(PAGE_SIZE - 1);
892 n = DIV_ROUND_UP(len, PAGE_SIZE);
893 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, pages);
894 if (unlikely(res < 0))
896 return (res == n ? len : res * PAGE_SIZE) - *start;
898 /* can't be more than PAGE_SIZE */
899 *start = v.bv_offset;
900 get_page(*pages = v.bv_page);
908 EXPORT_SYMBOL(iov_iter_get_pages);
910 static struct page **get_pages_array(size_t n)
912 struct page **p = kmalloc(n * sizeof(struct page *), GFP_KERNEL);
914 p = vmalloc(n * sizeof(struct page *));
918 static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
919 struct page ***pages, size_t maxsize,
930 data_start(i, &idx, start);
931 /* some of this one + all after this one */
932 npages = ((i->pipe->curbuf - idx - 1) & (i->pipe->buffers - 1)) + 1;
933 n = npages * PAGE_SIZE - *start;
937 npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
938 p = get_pages_array(npages);
941 n = __pipe_get_pages(i, maxsize, p, idx, start);
949 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
950 struct page ***pages, size_t maxsize,
955 if (maxsize > i->count)
961 if (unlikely(i->type & ITER_PIPE))
962 return pipe_get_pages_alloc(i, pages, maxsize, start);
963 iterate_all_kinds(i, maxsize, v, ({
964 unsigned long addr = (unsigned long)v.iov_base;
965 size_t len = v.iov_len + (*start = addr & (PAGE_SIZE - 1));
969 addr &= ~(PAGE_SIZE - 1);
970 n = DIV_ROUND_UP(len, PAGE_SIZE);
971 p = get_pages_array(n);
974 res = get_user_pages_fast(addr, n, (i->type & WRITE) != WRITE, p);
975 if (unlikely(res < 0)) {
980 return (res == n ? len : res * PAGE_SIZE) - *start;
982 /* can't be more than PAGE_SIZE */
983 *start = v.bv_offset;
984 *pages = p = get_pages_array(1);
987 get_page(*p = v.bv_page);
995 EXPORT_SYMBOL(iov_iter_get_pages_alloc);
997 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
1004 if (unlikely(i->type & ITER_PIPE)) {
1008 iterate_and_advance(i, bytes, v, ({
1010 next = csum_and_copy_from_user(v.iov_base,
1011 (to += v.iov_len) - v.iov_len,
1012 v.iov_len, 0, &err);
1014 sum = csum_block_add(sum, next, off);
1017 err ? v.iov_len : 0;
1019 char *p = kmap_atomic(v.bv_page);
1020 next = csum_partial_copy_nocheck(p + v.bv_offset,
1021 (to += v.bv_len) - v.bv_len,
1024 sum = csum_block_add(sum, next, off);
1027 next = csum_partial_copy_nocheck(v.iov_base,
1028 (to += v.iov_len) - v.iov_len,
1030 sum = csum_block_add(sum, next, off);
1037 EXPORT_SYMBOL(csum_and_copy_from_iter);
1039 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum,
1042 const char *from = addr;
1046 if (unlikely(i->type & ITER_PIPE)) {
1047 WARN_ON(1); /* for now */
1050 iterate_and_advance(i, bytes, v, ({
1052 next = csum_and_copy_to_user((from += v.iov_len) - v.iov_len,
1054 v.iov_len, 0, &err);
1056 sum = csum_block_add(sum, next, off);
1059 err ? v.iov_len : 0;
1061 char *p = kmap_atomic(v.bv_page);
1062 next = csum_partial_copy_nocheck((from += v.bv_len) - v.bv_len,
1066 sum = csum_block_add(sum, next, off);
1069 next = csum_partial_copy_nocheck((from += v.iov_len) - v.iov_len,
1072 sum = csum_block_add(sum, next, off);
1079 EXPORT_SYMBOL(csum_and_copy_to_iter);
1081 int iov_iter_npages(const struct iov_iter *i, int maxpages)
1083 size_t size = i->count;
1089 if (unlikely(i->type & ITER_PIPE)) {
1090 struct pipe_inode_info *pipe = i->pipe;
1097 data_start(i, &idx, &off);
1098 /* some of this one + all after this one */
1099 npages = ((pipe->curbuf - idx - 1) & (pipe->buffers - 1)) + 1;
1100 if (npages >= maxpages)
1102 } else iterate_all_kinds(i, size, v, ({
1103 unsigned long p = (unsigned long)v.iov_base;
1104 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1106 if (npages >= maxpages)
1110 if (npages >= maxpages)
1113 unsigned long p = (unsigned long)v.iov_base;
1114 npages += DIV_ROUND_UP(p + v.iov_len, PAGE_SIZE)
1116 if (npages >= maxpages)
1122 EXPORT_SYMBOL(iov_iter_npages);
1124 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
1127 if (unlikely(new->type & ITER_PIPE)) {
1131 if (new->type & ITER_BVEC)
1132 return new->bvec = kmemdup(new->bvec,
1133 new->nr_segs * sizeof(struct bio_vec),
1136 /* iovec and kvec have identical layout */
1137 return new->iov = kmemdup(new->iov,
1138 new->nr_segs * sizeof(struct iovec),
1141 EXPORT_SYMBOL(dup_iter);
1143 int import_iovec(int type, const struct iovec __user * uvector,
1144 unsigned nr_segs, unsigned fast_segs,
1145 struct iovec **iov, struct iov_iter *i)
1149 n = rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1157 iov_iter_init(i, type, p, nr_segs, n);
1158 *iov = p == *iov ? NULL : p;
1161 EXPORT_SYMBOL(import_iovec);
1163 #ifdef CONFIG_COMPAT
1164 #include <linux/compat.h>
1166 int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
1167 unsigned nr_segs, unsigned fast_segs,
1168 struct iovec **iov, struct iov_iter *i)
1172 n = compat_rw_copy_check_uvector(type, uvector, nr_segs, fast_segs,
1180 iov_iter_init(i, type, p, nr_segs, n);
1181 *iov = p == *iov ? NULL : p;
1186 int import_single_range(int rw, void __user *buf, size_t len,
1187 struct iovec *iov, struct iov_iter *i)
1189 if (len > MAX_RW_COUNT)
1191 if (unlikely(!access_ok(!rw, buf, len)))
1194 iov->iov_base = buf;
1196 iov_iter_init(i, rw, iov, 1, len);
1199 EXPORT_SYMBOL(import_single_range);