1 #include <linux/export.h>
2 #include <linux/bvec.h>
4 #include <linux/pagemap.h>
5 #include <linux/slab.h>
6 #include <linux/vmalloc.h>
7 #include <linux/splice.h>
8 #include <net/checksum.h>
10 #define PIPE_PARANOIA /* for now */
12 #define iterate_iovec(i, n, __v, __p, skip, STEP) { \
16 __v.iov_len = min(n, __p->iov_len - skip); \
17 if (likely(__v.iov_len)) { \
18 __v.iov_base = __p->iov_base + skip; \
20 __v.iov_len -= left; \
21 skip += __v.iov_len; \
26 while (unlikely(!left && n)) { \
28 __v.iov_len = min(n, __p->iov_len); \
29 if (unlikely(!__v.iov_len)) \
31 __v.iov_base = __p->iov_base; \
33 __v.iov_len -= left; \
40 #define iterate_kvec(i, n, __v, __p, skip, STEP) { \
43 __v.iov_len = min(n, __p->iov_len - skip); \
44 if (likely(__v.iov_len)) { \
45 __v.iov_base = __p->iov_base + skip; \
47 skip += __v.iov_len; \
50 while (unlikely(n)) { \
52 __v.iov_len = min(n, __p->iov_len); \
53 if (unlikely(!__v.iov_len)) \
55 __v.iov_base = __p->iov_base; \
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; \
68 for_each_bvec(__v, i->bvec, __bi, __start) { \
75 #define iterate_all_kinds(i, n, v, I, B, K) { \
77 size_t skip = i->iov_offset; \
78 if (unlikely(i->type & ITER_BVEC)) { \
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; \
85 iterate_kvec(i, n, v, kvec, skip, (K)) \
87 const struct iovec *iov; \
89 iterate_iovec(i, n, v, iov, skip, (I)) \
94 #define iterate_and_advance(i, n, v, I, B, K) { \
95 if (unlikely(i->count < n)) \
98 size_t skip = i->iov_offset; \
99 if (unlikely(i->type & ITER_BVEC)) { \
100 const struct bio_vec *bvec = i->bvec; \
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; \
107 } else if (unlikely(i->type & ITER_KVEC)) { \
108 const struct kvec *kvec; \
110 iterate_kvec(i, n, v, kvec, skip, (K)) \
111 if (skip == kvec->iov_len) { \
115 i->nr_segs -= kvec - i->kvec; \
118 const struct iovec *iov; \
120 iterate_iovec(i, n, v, iov, skip, (I)) \
121 if (skip == iov->iov_len) { \
125 i->nr_segs -= iov - i->iov; \
129 i->iov_offset = skip; \
133 static size_t copy_page_to_iter_iovec(struct page
*page
, size_t offset
, size_t bytes
,
136 size_t skip
, copy
, left
, wanted
;
137 const struct iovec
*iov
;
141 if (unlikely(bytes
> i
->count
))
144 if (unlikely(!bytes
))
149 skip
= i
->iov_offset
;
150 buf
= iov
->iov_base
+ skip
;
151 copy
= min(bytes
, iov
->iov_len
- skip
);
153 if (IS_ENABLED(CONFIG_HIGHMEM
) && !fault_in_pages_writeable(buf
, copy
)) {
154 kaddr
= kmap_atomic(page
);
155 from
= kaddr
+ offset
;
157 /* first chunk, usually the only one */
158 left
= __copy_to_user_inatomic(buf
, from
, copy
);
164 while (unlikely(!left
&& bytes
)) {
167 copy
= min(bytes
, iov
->iov_len
);
168 left
= __copy_to_user_inatomic(buf
, from
, copy
);
174 if (likely(!bytes
)) {
175 kunmap_atomic(kaddr
);
178 offset
= from
- kaddr
;
180 kunmap_atomic(kaddr
);
181 copy
= min(bytes
, iov
->iov_len
- skip
);
183 /* Too bad - revert to non-atomic kmap */
186 from
= kaddr
+ offset
;
187 left
= __copy_to_user(buf
, from
, copy
);
192 while (unlikely(!left
&& bytes
)) {
195 copy
= min(bytes
, iov
->iov_len
);
196 left
= __copy_to_user(buf
, from
, copy
);
205 if (skip
== iov
->iov_len
) {
209 i
->count
-= wanted
- bytes
;
210 i
->nr_segs
-= iov
- i
->iov
;
212 i
->iov_offset
= skip
;
213 return wanted
- bytes
;
216 static size_t copy_page_from_iter_iovec(struct page
*page
, size_t offset
, size_t bytes
,
219 size_t skip
, copy
, left
, wanted
;
220 const struct iovec
*iov
;
224 if (unlikely(bytes
> i
->count
))
227 if (unlikely(!bytes
))
232 skip
= i
->iov_offset
;
233 buf
= iov
->iov_base
+ skip
;
234 copy
= min(bytes
, iov
->iov_len
- skip
);
236 if (IS_ENABLED(CONFIG_HIGHMEM
) && !fault_in_pages_readable(buf
, copy
)) {
237 kaddr
= kmap_atomic(page
);
240 /* first chunk, usually the only one */
241 left
= __copy_from_user_inatomic(to
, buf
, copy
);
247 while (unlikely(!left
&& bytes
)) {
250 copy
= min(bytes
, iov
->iov_len
);
251 left
= __copy_from_user_inatomic(to
, buf
, copy
);
257 if (likely(!bytes
)) {
258 kunmap_atomic(kaddr
);
263 kunmap_atomic(kaddr
);
264 copy
= min(bytes
, iov
->iov_len
- skip
);
266 /* Too bad - revert to non-atomic kmap */
270 left
= __copy_from_user(to
, buf
, copy
);
275 while (unlikely(!left
&& bytes
)) {
278 copy
= min(bytes
, iov
->iov_len
);
279 left
= __copy_from_user(to
, buf
, copy
);
288 if (skip
== iov
->iov_len
) {
292 i
->count
-= wanted
- bytes
;
293 i
->nr_segs
-= iov
- i
->iov
;
295 i
->iov_offset
= skip
;
296 return wanted
- bytes
;
300 static bool sanity(const struct iov_iter
*i
)
302 struct pipe_inode_info
*pipe
= i
->pipe
;
304 int next
= pipe
->curbuf
+ pipe
->nrbufs
;
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...
312 p
= &pipe
->bufs
[idx
];
313 if (unlikely(p
->offset
+ p
->len
!= i
->iov_offset
))
314 goto Bad
; // ... at the end of segment
316 if (idx
!= (next
& (pipe
->buffers
- 1)))
317 goto Bad
; // must be right after the last buffer
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",
327 pipe
->bufs
[idx
].page
,
328 pipe
->bufs
[idx
].offset
,
329 pipe
->bufs
[idx
].len
);
334 #define sanity(i) true
337 static inline int next_idx(int idx
, struct pipe_inode_info
*pipe
)
339 return (idx
+ 1) & (pipe
->buffers
- 1);
342 static size_t copy_page_to_iter_pipe(struct page
*page
, size_t offset
, size_t bytes
,
345 struct pipe_inode_info
*pipe
= i
->pipe
;
346 struct pipe_buffer
*buf
;
350 if (unlikely(bytes
> i
->count
))
353 if (unlikely(!bytes
))
361 buf
= &pipe
->bufs
[idx
];
363 if (offset
== off
&& buf
->page
== page
) {
364 /* merge with the last one */
366 i
->iov_offset
+= bytes
;
369 idx
= next_idx(idx
, pipe
);
370 buf
= &pipe
->bufs
[idx
];
372 if (idx
== pipe
->curbuf
&& pipe
->nrbufs
)
375 buf
->ops
= &page_cache_pipe_buf_ops
;
376 get_page(buf
->page
= page
);
377 buf
->offset
= offset
;
379 i
->iov_offset
= offset
+ bytes
;
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.
390 * Return 0 on success, or non-zero if the memory could not be accessed (i.e.
391 * because it is an invalid address).
393 int iov_iter_fault_in_readable(struct iov_iter
*i
, size_t bytes
)
395 size_t skip
= i
->iov_offset
;
396 const struct iovec
*iov
;
400 if (!(i
->type
& (ITER_BVEC
|ITER_KVEC
))) {
401 iterate_iovec(i
, bytes
, v
, iov
, skip
, ({
402 err
= fault_in_pages_readable(v
.iov_base
, v
.iov_len
);
409 EXPORT_SYMBOL(iov_iter_fault_in_readable
);
411 void iov_iter_init(struct iov_iter
*i
, int direction
,
412 const struct iovec
*iov
, unsigned long nr_segs
,
415 /* It will get better. Eventually... */
416 if (uaccess_kernel()) {
417 direction
|= ITER_KVEC
;
419 i
->kvec
= (struct kvec
*)iov
;
424 i
->nr_segs
= nr_segs
;
428 EXPORT_SYMBOL(iov_iter_init
);
430 static void memcpy_from_page(char *to
, struct page
*page
, size_t offset
, size_t len
)
432 char *from
= kmap_atomic(page
);
433 memcpy(to
, from
+ offset
, len
);
437 static void memcpy_to_page(struct page
*page
, size_t offset
, const char *from
, size_t len
)
439 char *to
= kmap_atomic(page
);
440 memcpy(to
+ offset
, from
, len
);
444 static void memzero_page(struct page
*page
, size_t offset
, size_t len
)
446 char *addr
= kmap_atomic(page
);
447 memset(addr
+ offset
, 0, len
);
451 static inline bool allocated(struct pipe_buffer
*buf
)
453 return buf
->ops
== &default_pipe_buf_ops
;
456 static inline void data_start(const struct iov_iter
*i
, int *idxp
, size_t *offp
)
458 size_t off
= i
->iov_offset
;
460 if (off
&& (!allocated(&i
->pipe
->bufs
[idx
]) || off
== PAGE_SIZE
)) {
461 idx
= next_idx(idx
, i
->pipe
);
468 static size_t push_pipe(struct iov_iter
*i
, size_t size
,
469 int *idxp
, size_t *offp
)
471 struct pipe_inode_info
*pipe
= i
->pipe
;
476 if (unlikely(size
> i
->count
))
482 data_start(i
, &idx
, &off
);
486 left
-= PAGE_SIZE
- off
;
488 pipe
->bufs
[idx
].len
+= size
;
491 pipe
->bufs
[idx
].len
= PAGE_SIZE
;
492 idx
= next_idx(idx
, pipe
);
494 while (idx
!= pipe
->curbuf
|| !pipe
->nrbufs
) {
495 struct page
*page
= alloc_page(GFP_USER
);
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
;
506 pipe
->bufs
[idx
].len
= PAGE_SIZE
;
508 idx
= next_idx(idx
, pipe
);
513 static size_t copy_pipe_to_iter(const void *addr
, size_t bytes
,
516 struct pipe_inode_info
*pipe
= i
->pipe
;
523 bytes
= n
= push_pipe(i
, bytes
, &idx
, &off
);
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
);
530 i
->iov_offset
= off
+ chunk
;
538 size_t copy_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
)
540 const char *from
= addr
;
541 if (unlikely(i
->type
& ITER_PIPE
))
542 return copy_pipe_to_iter(addr
, bytes
, i
);
543 iterate_and_advance(i
, bytes
, v
,
544 __copy_to_user(v
.iov_base
, (from
+= v
.iov_len
) - v
.iov_len
,
546 memcpy_to_page(v
.bv_page
, v
.bv_offset
,
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
)
553 EXPORT_SYMBOL(copy_to_iter
);
555 size_t copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
558 if (unlikely(i
->type
& ITER_PIPE
)) {
562 iterate_and_advance(i
, bytes
, v
,
563 __copy_from_user((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
,
565 memcpy_from_page((to
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
566 v
.bv_offset
, v
.bv_len
),
567 memcpy((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
572 EXPORT_SYMBOL(copy_from_iter
);
574 bool copy_from_iter_full(void *addr
, size_t bytes
, struct iov_iter
*i
)
577 if (unlikely(i
->type
& ITER_PIPE
)) {
581 if (unlikely(i
->count
< bytes
))
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
))
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
)
594 iov_iter_advance(i
, bytes
);
597 EXPORT_SYMBOL(copy_from_iter_full
);
599 size_t copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
602 if (unlikely(i
->type
& ITER_PIPE
)) {
606 iterate_and_advance(i
, bytes
, v
,
607 __copy_from_user_inatomic_nocache((to
+= v
.iov_len
) - v
.iov_len
,
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
)
616 EXPORT_SYMBOL(copy_from_iter_nocache
);
618 bool copy_from_iter_full_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
621 if (unlikely(i
->type
& ITER_PIPE
)) {
625 if (unlikely(i
->count
< bytes
))
627 iterate_all_kinds(i
, bytes
, v
, ({
628 if (__copy_from_user_inatomic_nocache((to
+= v
.iov_len
) - v
.iov_len
,
629 v
.iov_base
, v
.iov_len
))
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
)
637 iov_iter_advance(i
, bytes
);
640 EXPORT_SYMBOL(copy_from_iter_full_nocache
);
642 size_t copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
645 if (i
->type
& (ITER_BVEC
|ITER_KVEC
)) {
646 void *kaddr
= kmap_atomic(page
);
647 size_t wanted
= copy_to_iter(kaddr
+ offset
, bytes
, i
);
648 kunmap_atomic(kaddr
);
650 } else if (likely(!(i
->type
& ITER_PIPE
)))
651 return copy_page_to_iter_iovec(page
, offset
, bytes
, i
);
653 return copy_page_to_iter_pipe(page
, offset
, bytes
, i
);
655 EXPORT_SYMBOL(copy_page_to_iter
);
657 size_t copy_page_from_iter(struct page
*page
, size_t offset
, size_t bytes
,
660 if (unlikely(i
->type
& ITER_PIPE
)) {
664 if (i
->type
& (ITER_BVEC
|ITER_KVEC
)) {
665 void *kaddr
= kmap_atomic(page
);
666 size_t wanted
= copy_from_iter(kaddr
+ offset
, bytes
, i
);
667 kunmap_atomic(kaddr
);
670 return copy_page_from_iter_iovec(page
, offset
, bytes
, i
);
672 EXPORT_SYMBOL(copy_page_from_iter
);
674 static size_t pipe_zero(size_t bytes
, struct iov_iter
*i
)
676 struct pipe_inode_info
*pipe
= i
->pipe
;
683 bytes
= n
= push_pipe(i
, bytes
, &idx
, &off
);
687 for ( ; n
; idx
= next_idx(idx
, pipe
), off
= 0) {
688 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
689 memzero_page(pipe
->bufs
[idx
].page
, off
, chunk
);
691 i
->iov_offset
= off
+ chunk
;
698 size_t iov_iter_zero(size_t bytes
, struct iov_iter
*i
)
700 if (unlikely(i
->type
& ITER_PIPE
))
701 return pipe_zero(bytes
, i
);
702 iterate_and_advance(i
, bytes
, v
,
703 __clear_user(v
.iov_base
, v
.iov_len
),
704 memzero_page(v
.bv_page
, v
.bv_offset
, v
.bv_len
),
705 memset(v
.iov_base
, 0, v
.iov_len
)
710 EXPORT_SYMBOL(iov_iter_zero
);
712 size_t iov_iter_copy_from_user_atomic(struct page
*page
,
713 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
715 char *kaddr
= kmap_atomic(page
), *p
= kaddr
+ offset
;
716 if (unlikely(i
->type
& ITER_PIPE
)) {
717 kunmap_atomic(kaddr
);
721 iterate_all_kinds(i
, bytes
, v
,
722 __copy_from_user_inatomic((p
+= v
.iov_len
) - v
.iov_len
,
723 v
.iov_base
, v
.iov_len
),
724 memcpy_from_page((p
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
725 v
.bv_offset
, v
.bv_len
),
726 memcpy((p
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
728 kunmap_atomic(kaddr
);
731 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic
);
733 static inline void pipe_truncate(struct iov_iter
*i
)
735 struct pipe_inode_info
*pipe
= i
->pipe
;
737 size_t off
= i
->iov_offset
;
739 int nrbufs
= (idx
- pipe
->curbuf
) & (pipe
->buffers
- 1);
741 pipe
->bufs
[idx
].len
= off
- pipe
->bufs
[idx
].offset
;
742 idx
= next_idx(idx
, pipe
);
745 while (pipe
->nrbufs
> nrbufs
) {
746 pipe_buf_release(pipe
, &pipe
->bufs
[idx
]);
747 idx
= next_idx(idx
, pipe
);
753 static void pipe_advance(struct iov_iter
*i
, size_t size
)
755 struct pipe_inode_info
*pipe
= i
->pipe
;
756 if (unlikely(i
->count
< size
))
759 struct pipe_buffer
*buf
;
760 size_t off
= i
->iov_offset
, left
= size
;
762 if (off
) /* make it relative to the beginning of buffer */
763 left
+= off
- pipe
->bufs
[idx
].offset
;
765 buf
= &pipe
->bufs
[idx
];
766 if (left
<= buf
->len
)
769 idx
= next_idx(idx
, pipe
);
772 i
->iov_offset
= buf
->offset
+ left
;
775 /* ... and discard everything past that point */
779 void iov_iter_advance(struct iov_iter
*i
, size_t size
)
781 if (unlikely(i
->type
& ITER_PIPE
)) {
782 pipe_advance(i
, size
);
785 iterate_and_advance(i
, size
, v
, 0, 0, 0)
787 EXPORT_SYMBOL(iov_iter_advance
);
789 void iov_iter_revert(struct iov_iter
*i
, size_t unroll
)
793 if (WARN_ON(unroll
> MAX_RW_COUNT
))
796 if (unlikely(i
->type
& ITER_PIPE
)) {
797 struct pipe_inode_info
*pipe
= i
->pipe
;
799 size_t off
= i
->iov_offset
;
801 size_t n
= off
- pipe
->bufs
[idx
].offset
;
807 if (!unroll
&& idx
== i
->start_idx
) {
812 idx
= pipe
->buffers
- 1;
813 off
= pipe
->bufs
[idx
].offset
+ pipe
->bufs
[idx
].len
;
820 if (unroll
<= i
->iov_offset
) {
821 i
->iov_offset
-= unroll
;
824 unroll
-= i
->iov_offset
;
825 if (i
->type
& ITER_BVEC
) {
826 const struct bio_vec
*bvec
= i
->bvec
;
828 size_t n
= (--bvec
)->bv_len
;
832 i
->iov_offset
= n
- unroll
;
837 } else { /* same logics for iovec and kvec */
838 const struct iovec
*iov
= i
->iov
;
840 size_t n
= (--iov
)->iov_len
;
844 i
->iov_offset
= n
- unroll
;
851 EXPORT_SYMBOL(iov_iter_revert
);
854 * Return the count of just the current iov_iter segment.
856 size_t iov_iter_single_seg_count(const struct iov_iter
*i
)
858 if (unlikely(i
->type
& ITER_PIPE
))
859 return i
->count
; // it is a silly place, anyway
862 else if (i
->type
& ITER_BVEC
)
863 return min(i
->count
, i
->bvec
->bv_len
- i
->iov_offset
);
865 return min(i
->count
, i
->iov
->iov_len
- i
->iov_offset
);
867 EXPORT_SYMBOL(iov_iter_single_seg_count
);
869 void iov_iter_kvec(struct iov_iter
*i
, int direction
,
870 const struct kvec
*kvec
, unsigned long nr_segs
,
873 BUG_ON(!(direction
& ITER_KVEC
));
876 i
->nr_segs
= nr_segs
;
880 EXPORT_SYMBOL(iov_iter_kvec
);
882 void iov_iter_bvec(struct iov_iter
*i
, int direction
,
883 const struct bio_vec
*bvec
, unsigned long nr_segs
,
886 BUG_ON(!(direction
& ITER_BVEC
));
889 i
->nr_segs
= nr_segs
;
893 EXPORT_SYMBOL(iov_iter_bvec
);
895 void iov_iter_pipe(struct iov_iter
*i
, int direction
,
896 struct pipe_inode_info
*pipe
,
899 BUG_ON(direction
!= ITER_PIPE
);
900 WARN_ON(pipe
->nrbufs
== pipe
->buffers
);
903 i
->idx
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
906 i
->start_idx
= i
->idx
;
908 EXPORT_SYMBOL(iov_iter_pipe
);
910 unsigned long iov_iter_alignment(const struct iov_iter
*i
)
912 unsigned long res
= 0;
913 size_t size
= i
->count
;
915 if (unlikely(i
->type
& ITER_PIPE
)) {
916 if (size
&& i
->iov_offset
&& allocated(&i
->pipe
->bufs
[i
->idx
]))
917 return size
| i
->iov_offset
;
920 iterate_all_kinds(i
, size
, v
,
921 (res
|= (unsigned long)v
.iov_base
| v
.iov_len
, 0),
922 res
|= v
.bv_offset
| v
.bv_len
,
923 res
|= (unsigned long)v
.iov_base
| v
.iov_len
927 EXPORT_SYMBOL(iov_iter_alignment
);
929 unsigned long iov_iter_gap_alignment(const struct iov_iter
*i
)
931 unsigned long res
= 0;
932 size_t size
= i
->count
;
934 if (unlikely(i
->type
& ITER_PIPE
)) {
939 iterate_all_kinds(i
, size
, v
,
940 (res
|= (!res
? 0 : (unsigned long)v
.iov_base
) |
941 (size
!= v
.iov_len
? size
: 0), 0),
942 (res
|= (!res
? 0 : (unsigned long)v
.bv_offset
) |
943 (size
!= v
.bv_len
? size
: 0)),
944 (res
|= (!res
? 0 : (unsigned long)v
.iov_base
) |
945 (size
!= v
.iov_len
? size
: 0))
949 EXPORT_SYMBOL(iov_iter_gap_alignment
);
951 static inline size_t __pipe_get_pages(struct iov_iter
*i
,
957 struct pipe_inode_info
*pipe
= i
->pipe
;
958 ssize_t n
= push_pipe(i
, maxsize
, &idx
, start
);
965 get_page(*pages
++ = pipe
->bufs
[idx
].page
);
966 idx
= next_idx(idx
, pipe
);
973 static ssize_t
pipe_get_pages(struct iov_iter
*i
,
974 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
987 data_start(i
, &idx
, start
);
988 /* some of this one + all after this one */
989 npages
= ((i
->pipe
->curbuf
- idx
- 1) & (i
->pipe
->buffers
- 1)) + 1;
990 capacity
= min(npages
,maxpages
) * PAGE_SIZE
- *start
;
992 return __pipe_get_pages(i
, min(maxsize
, capacity
), pages
, idx
, start
);
995 ssize_t
iov_iter_get_pages(struct iov_iter
*i
,
996 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
999 if (maxsize
> i
->count
)
1002 if (unlikely(i
->type
& ITER_PIPE
))
1003 return pipe_get_pages(i
, pages
, maxsize
, maxpages
, start
);
1004 iterate_all_kinds(i
, maxsize
, v
, ({
1005 unsigned long addr
= (unsigned long)v
.iov_base
;
1006 size_t len
= v
.iov_len
+ (*start
= addr
& (PAGE_SIZE
- 1));
1010 if (len
> maxpages
* PAGE_SIZE
)
1011 len
= maxpages
* PAGE_SIZE
;
1012 addr
&= ~(PAGE_SIZE
- 1);
1013 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1014 res
= get_user_pages_fast(addr
, n
, (i
->type
& WRITE
) != WRITE
, pages
);
1015 if (unlikely(res
< 0))
1017 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1019 /* can't be more than PAGE_SIZE */
1020 *start
= v
.bv_offset
;
1021 get_page(*pages
= v
.bv_page
);
1029 EXPORT_SYMBOL(iov_iter_get_pages
);
1031 static struct page
**get_pages_array(size_t n
)
1033 return kvmalloc_array(n
, sizeof(struct page
*), GFP_KERNEL
);
1036 static ssize_t
pipe_get_pages_alloc(struct iov_iter
*i
,
1037 struct page
***pages
, size_t maxsize
,
1051 data_start(i
, &idx
, start
);
1052 /* some of this one + all after this one */
1053 npages
= ((i
->pipe
->curbuf
- idx
- 1) & (i
->pipe
->buffers
- 1)) + 1;
1054 n
= npages
* PAGE_SIZE
- *start
;
1058 npages
= DIV_ROUND_UP(maxsize
+ *start
, PAGE_SIZE
);
1059 p
= get_pages_array(npages
);
1062 n
= __pipe_get_pages(i
, maxsize
, p
, idx
, start
);
1070 ssize_t
iov_iter_get_pages_alloc(struct iov_iter
*i
,
1071 struct page
***pages
, size_t maxsize
,
1076 if (maxsize
> i
->count
)
1079 if (unlikely(i
->type
& ITER_PIPE
))
1080 return pipe_get_pages_alloc(i
, pages
, maxsize
, start
);
1081 iterate_all_kinds(i
, maxsize
, v
, ({
1082 unsigned long addr
= (unsigned long)v
.iov_base
;
1083 size_t len
= v
.iov_len
+ (*start
= addr
& (PAGE_SIZE
- 1));
1087 addr
&= ~(PAGE_SIZE
- 1);
1088 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1089 p
= get_pages_array(n
);
1092 res
= get_user_pages_fast(addr
, n
, (i
->type
& WRITE
) != WRITE
, p
);
1093 if (unlikely(res
< 0)) {
1098 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1100 /* can't be more than PAGE_SIZE */
1101 *start
= v
.bv_offset
;
1102 *pages
= p
= get_pages_array(1);
1105 get_page(*p
= v
.bv_page
);
1113 EXPORT_SYMBOL(iov_iter_get_pages_alloc
);
1115 size_t csum_and_copy_from_iter(void *addr
, size_t bytes
, __wsum
*csum
,
1122 if (unlikely(i
->type
& ITER_PIPE
)) {
1126 iterate_and_advance(i
, bytes
, v
, ({
1128 next
= csum_and_copy_from_user(v
.iov_base
,
1129 (to
+= v
.iov_len
) - v
.iov_len
,
1130 v
.iov_len
, 0, &err
);
1132 sum
= csum_block_add(sum
, next
, off
);
1135 err
? v
.iov_len
: 0;
1137 char *p
= kmap_atomic(v
.bv_page
);
1138 next
= csum_partial_copy_nocheck(p
+ v
.bv_offset
,
1139 (to
+= v
.bv_len
) - v
.bv_len
,
1142 sum
= csum_block_add(sum
, next
, off
);
1145 next
= csum_partial_copy_nocheck(v
.iov_base
,
1146 (to
+= v
.iov_len
) - v
.iov_len
,
1148 sum
= csum_block_add(sum
, next
, off
);
1155 EXPORT_SYMBOL(csum_and_copy_from_iter
);
1157 bool csum_and_copy_from_iter_full(void *addr
, size_t bytes
, __wsum
*csum
,
1164 if (unlikely(i
->type
& ITER_PIPE
)) {
1168 if (unlikely(i
->count
< bytes
))
1170 iterate_all_kinds(i
, bytes
, v
, ({
1172 next
= csum_and_copy_from_user(v
.iov_base
,
1173 (to
+= v
.iov_len
) - v
.iov_len
,
1174 v
.iov_len
, 0, &err
);
1177 sum
= csum_block_add(sum
, next
, off
);
1181 char *p
= kmap_atomic(v
.bv_page
);
1182 next
= csum_partial_copy_nocheck(p
+ v
.bv_offset
,
1183 (to
+= v
.bv_len
) - v
.bv_len
,
1186 sum
= csum_block_add(sum
, next
, off
);
1189 next
= csum_partial_copy_nocheck(v
.iov_base
,
1190 (to
+= v
.iov_len
) - v
.iov_len
,
1192 sum
= csum_block_add(sum
, next
, off
);
1197 iov_iter_advance(i
, bytes
);
1200 EXPORT_SYMBOL(csum_and_copy_from_iter_full
);
1202 size_t csum_and_copy_to_iter(const void *addr
, size_t bytes
, __wsum
*csum
,
1205 const char *from
= addr
;
1209 if (unlikely(i
->type
& ITER_PIPE
)) {
1210 WARN_ON(1); /* for now */
1213 iterate_and_advance(i
, bytes
, v
, ({
1215 next
= csum_and_copy_to_user((from
+= v
.iov_len
) - v
.iov_len
,
1217 v
.iov_len
, 0, &err
);
1219 sum
= csum_block_add(sum
, next
, off
);
1222 err
? v
.iov_len
: 0;
1224 char *p
= kmap_atomic(v
.bv_page
);
1225 next
= csum_partial_copy_nocheck((from
+= v
.bv_len
) - v
.bv_len
,
1229 sum
= csum_block_add(sum
, next
, off
);
1232 next
= csum_partial_copy_nocheck((from
+= v
.iov_len
) - v
.iov_len
,
1235 sum
= csum_block_add(sum
, next
, off
);
1242 EXPORT_SYMBOL(csum_and_copy_to_iter
);
1244 int iov_iter_npages(const struct iov_iter
*i
, int maxpages
)
1246 size_t size
= i
->count
;
1252 if (unlikely(i
->type
& ITER_PIPE
)) {
1253 struct pipe_inode_info
*pipe
= i
->pipe
;
1260 data_start(i
, &idx
, &off
);
1261 /* some of this one + all after this one */
1262 npages
= ((pipe
->curbuf
- idx
- 1) & (pipe
->buffers
- 1)) + 1;
1263 if (npages
>= maxpages
)
1265 } else iterate_all_kinds(i
, size
, v
, ({
1266 unsigned long p
= (unsigned long)v
.iov_base
;
1267 npages
+= DIV_ROUND_UP(p
+ v
.iov_len
, PAGE_SIZE
)
1269 if (npages
>= maxpages
)
1273 if (npages
>= maxpages
)
1276 unsigned long p
= (unsigned long)v
.iov_base
;
1277 npages
+= DIV_ROUND_UP(p
+ v
.iov_len
, PAGE_SIZE
)
1279 if (npages
>= maxpages
)
1285 EXPORT_SYMBOL(iov_iter_npages
);
1287 const void *dup_iter(struct iov_iter
*new, struct iov_iter
*old
, gfp_t flags
)
1290 if (unlikely(new->type
& ITER_PIPE
)) {
1294 if (new->type
& ITER_BVEC
)
1295 return new->bvec
= kmemdup(new->bvec
,
1296 new->nr_segs
* sizeof(struct bio_vec
),
1299 /* iovec and kvec have identical layout */
1300 return new->iov
= kmemdup(new->iov
,
1301 new->nr_segs
* sizeof(struct iovec
),
1304 EXPORT_SYMBOL(dup_iter
);
1307 * import_iovec() - Copy an array of &struct iovec from userspace
1308 * into the kernel, check that it is valid, and initialize a new
1309 * &struct iov_iter iterator to access it.
1311 * @type: One of %READ or %WRITE.
1312 * @uvector: Pointer to the userspace array.
1313 * @nr_segs: Number of elements in userspace array.
1314 * @fast_segs: Number of elements in @iov.
1315 * @iov: (input and output parameter) Pointer to pointer to (usually small
1316 * on-stack) kernel array.
1317 * @i: Pointer to iterator that will be initialized on success.
1319 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1320 * then this function places %NULL in *@iov on return. Otherwise, a new
1321 * array will be allocated and the result placed in *@iov. This means that
1322 * the caller may call kfree() on *@iov regardless of whether the small
1323 * on-stack array was used or not (and regardless of whether this function
1324 * returns an error or not).
1326 * Return: 0 on success or negative error code on error.
1328 int import_iovec(int type
, const struct iovec __user
* uvector
,
1329 unsigned nr_segs
, unsigned fast_segs
,
1330 struct iovec
**iov
, struct iov_iter
*i
)
1334 n
= rw_copy_check_uvector(type
, uvector
, nr_segs
, fast_segs
,
1342 iov_iter_init(i
, type
, p
, nr_segs
, n
);
1343 *iov
= p
== *iov
? NULL
: p
;
1346 EXPORT_SYMBOL(import_iovec
);
1348 #ifdef CONFIG_COMPAT
1349 #include <linux/compat.h>
1351 int compat_import_iovec(int type
, const struct compat_iovec __user
* uvector
,
1352 unsigned nr_segs
, unsigned fast_segs
,
1353 struct iovec
**iov
, struct iov_iter
*i
)
1357 n
= compat_rw_copy_check_uvector(type
, uvector
, nr_segs
, fast_segs
,
1365 iov_iter_init(i
, type
, p
, nr_segs
, n
);
1366 *iov
= p
== *iov
? NULL
: p
;
1371 int import_single_range(int rw
, void __user
*buf
, size_t len
,
1372 struct iovec
*iov
, struct iov_iter
*i
)
1374 if (len
> MAX_RW_COUNT
)
1376 if (unlikely(!access_ok(!rw
, buf
, len
)))
1379 iov
->iov_base
= buf
;
1381 iov_iter_init(i
, rw
, iov
, 1, len
);
1384 EXPORT_SYMBOL(import_single_range
);