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_pages_readable(v
.iov_base
, v
.iov_len
);
406 EXPORT_SYMBOL(iov_iter_fault_in_readable
);
408 void iov_iter_init(struct iov_iter
*i
, int direction
,
409 const struct iovec
*iov
, unsigned long nr_segs
,
412 /* It will get better. Eventually... */
413 if (segment_eq(get_fs(), KERNEL_DS
)) {
414 direction
|= ITER_KVEC
;
416 i
->kvec
= (struct kvec
*)iov
;
421 i
->nr_segs
= nr_segs
;
425 EXPORT_SYMBOL(iov_iter_init
);
427 static void memcpy_from_page(char *to
, struct page
*page
, size_t offset
, size_t len
)
429 char *from
= kmap_atomic(page
);
430 memcpy(to
, from
+ offset
, len
);
434 static void memcpy_to_page(struct page
*page
, size_t offset
, const char *from
, size_t len
)
436 char *to
= kmap_atomic(page
);
437 memcpy(to
+ offset
, from
, len
);
441 static void memzero_page(struct page
*page
, size_t offset
, size_t len
)
443 char *addr
= kmap_atomic(page
);
444 memset(addr
+ offset
, 0, len
);
448 static inline bool allocated(struct pipe_buffer
*buf
)
450 return buf
->ops
== &default_pipe_buf_ops
;
453 static inline void data_start(const struct iov_iter
*i
, int *idxp
, size_t *offp
)
455 size_t off
= i
->iov_offset
;
457 if (off
&& (!allocated(&i
->pipe
->bufs
[idx
]) || off
== PAGE_SIZE
)) {
458 idx
= next_idx(idx
, i
->pipe
);
465 static size_t push_pipe(struct iov_iter
*i
, size_t size
,
466 int *idxp
, size_t *offp
)
468 struct pipe_inode_info
*pipe
= i
->pipe
;
473 if (unlikely(size
> i
->count
))
479 data_start(i
, &idx
, &off
);
483 left
-= PAGE_SIZE
- off
;
485 pipe
->bufs
[idx
].len
+= size
;
488 pipe
->bufs
[idx
].len
= PAGE_SIZE
;
489 idx
= next_idx(idx
, pipe
);
491 while (idx
!= pipe
->curbuf
|| !pipe
->nrbufs
) {
492 struct page
*page
= alloc_page(GFP_USER
);
496 pipe
->bufs
[idx
].ops
= &default_pipe_buf_ops
;
497 pipe
->bufs
[idx
].page
= page
;
498 pipe
->bufs
[idx
].offset
= 0;
499 if (left
<= PAGE_SIZE
) {
500 pipe
->bufs
[idx
].len
= left
;
503 pipe
->bufs
[idx
].len
= PAGE_SIZE
;
505 idx
= next_idx(idx
, pipe
);
510 static size_t copy_pipe_to_iter(const void *addr
, size_t bytes
,
513 struct pipe_inode_info
*pipe
= i
->pipe
;
520 bytes
= n
= push_pipe(i
, bytes
, &idx
, &off
);
523 for ( ; n
; idx
= next_idx(idx
, pipe
), off
= 0) {
524 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
525 memcpy_to_page(pipe
->bufs
[idx
].page
, off
, addr
, chunk
);
527 i
->iov_offset
= off
+ chunk
;
535 size_t copy_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
)
537 const char *from
= addr
;
538 if (unlikely(i
->type
& ITER_PIPE
))
539 return copy_pipe_to_iter(addr
, bytes
, i
);
540 iterate_and_advance(i
, bytes
, v
,
541 __copy_to_user(v
.iov_base
, (from
+= v
.iov_len
) - v
.iov_len
,
543 memcpy_to_page(v
.bv_page
, v
.bv_offset
,
544 (from
+= v
.bv_len
) - v
.bv_len
, v
.bv_len
),
545 memcpy(v
.iov_base
, (from
+= v
.iov_len
) - v
.iov_len
, v
.iov_len
)
550 EXPORT_SYMBOL(copy_to_iter
);
552 size_t copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
555 if (unlikely(i
->type
& ITER_PIPE
)) {
559 iterate_and_advance(i
, bytes
, v
,
560 __copy_from_user((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
,
562 memcpy_from_page((to
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
563 v
.bv_offset
, v
.bv_len
),
564 memcpy((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
569 EXPORT_SYMBOL(copy_from_iter
);
571 size_t copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
574 if (unlikely(i
->type
& ITER_PIPE
)) {
578 iterate_and_advance(i
, bytes
, v
,
579 __copy_from_user_nocache((to
+= v
.iov_len
) - v
.iov_len
,
580 v
.iov_base
, v
.iov_len
),
581 memcpy_from_page((to
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
582 v
.bv_offset
, v
.bv_len
),
583 memcpy((to
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
588 EXPORT_SYMBOL(copy_from_iter_nocache
);
590 size_t copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
593 if (i
->type
& (ITER_BVEC
|ITER_KVEC
)) {
594 void *kaddr
= kmap_atomic(page
);
595 size_t wanted
= copy_to_iter(kaddr
+ offset
, bytes
, i
);
596 kunmap_atomic(kaddr
);
598 } else if (likely(!(i
->type
& ITER_PIPE
)))
599 return copy_page_to_iter_iovec(page
, offset
, bytes
, i
);
601 return copy_page_to_iter_pipe(page
, offset
, bytes
, i
);
603 EXPORT_SYMBOL(copy_page_to_iter
);
605 size_t copy_page_from_iter(struct page
*page
, size_t offset
, size_t bytes
,
608 if (unlikely(i
->type
& ITER_PIPE
)) {
612 if (i
->type
& (ITER_BVEC
|ITER_KVEC
)) {
613 void *kaddr
= kmap_atomic(page
);
614 size_t wanted
= copy_from_iter(kaddr
+ offset
, bytes
, i
);
615 kunmap_atomic(kaddr
);
618 return copy_page_from_iter_iovec(page
, offset
, bytes
, i
);
620 EXPORT_SYMBOL(copy_page_from_iter
);
622 static size_t pipe_zero(size_t bytes
, struct iov_iter
*i
)
624 struct pipe_inode_info
*pipe
= i
->pipe
;
631 bytes
= n
= push_pipe(i
, bytes
, &idx
, &off
);
635 for ( ; n
; idx
= next_idx(idx
, pipe
), off
= 0) {
636 size_t chunk
= min_t(size_t, n
, PAGE_SIZE
- off
);
637 memzero_page(pipe
->bufs
[idx
].page
, off
, chunk
);
639 i
->iov_offset
= off
+ chunk
;
646 size_t iov_iter_zero(size_t bytes
, struct iov_iter
*i
)
648 if (unlikely(i
->type
& ITER_PIPE
))
649 return pipe_zero(bytes
, i
);
650 iterate_and_advance(i
, bytes
, v
,
651 __clear_user(v
.iov_base
, v
.iov_len
),
652 memzero_page(v
.bv_page
, v
.bv_offset
, v
.bv_len
),
653 memset(v
.iov_base
, 0, v
.iov_len
)
658 EXPORT_SYMBOL(iov_iter_zero
);
660 size_t iov_iter_copy_from_user_atomic(struct page
*page
,
661 struct iov_iter
*i
, unsigned long offset
, size_t bytes
)
663 char *kaddr
= kmap_atomic(page
), *p
= kaddr
+ offset
;
664 if (unlikely(i
->type
& ITER_PIPE
)) {
665 kunmap_atomic(kaddr
);
669 iterate_all_kinds(i
, bytes
, v
,
670 __copy_from_user_inatomic((p
+= v
.iov_len
) - v
.iov_len
,
671 v
.iov_base
, v
.iov_len
),
672 memcpy_from_page((p
+= v
.bv_len
) - v
.bv_len
, v
.bv_page
,
673 v
.bv_offset
, v
.bv_len
),
674 memcpy((p
+= v
.iov_len
) - v
.iov_len
, v
.iov_base
, v
.iov_len
)
676 kunmap_atomic(kaddr
);
679 EXPORT_SYMBOL(iov_iter_copy_from_user_atomic
);
681 static inline void pipe_truncate(struct iov_iter
*i
)
683 struct pipe_inode_info
*pipe
= i
->pipe
;
685 size_t off
= i
->iov_offset
;
687 int nrbufs
= (idx
- pipe
->curbuf
) & (pipe
->buffers
- 1);
689 pipe
->bufs
[idx
].len
= off
- pipe
->bufs
[idx
].offset
;
690 idx
= next_idx(idx
, pipe
);
693 while (pipe
->nrbufs
> nrbufs
) {
694 pipe_buf_release(pipe
, &pipe
->bufs
[idx
]);
695 idx
= next_idx(idx
, pipe
);
701 static void pipe_advance(struct iov_iter
*i
, size_t size
)
703 struct pipe_inode_info
*pipe
= i
->pipe
;
704 if (unlikely(i
->count
< size
))
707 struct pipe_buffer
*buf
;
708 size_t off
= i
->iov_offset
, left
= size
;
710 if (off
) /* make it relative to the beginning of buffer */
711 left
+= off
- pipe
->bufs
[idx
].offset
;
713 buf
= &pipe
->bufs
[idx
];
714 if (left
<= buf
->len
)
717 idx
= next_idx(idx
, pipe
);
720 i
->iov_offset
= buf
->offset
+ left
;
723 /* ... and discard everything past that point */
727 void iov_iter_advance(struct iov_iter
*i
, size_t size
)
729 if (unlikely(i
->type
& ITER_PIPE
)) {
730 pipe_advance(i
, size
);
733 iterate_and_advance(i
, size
, v
, 0, 0, 0)
735 EXPORT_SYMBOL(iov_iter_advance
);
737 void iov_iter_revert(struct iov_iter
*i
, size_t unroll
)
742 if (unlikely(i
->type
& ITER_PIPE
)) {
743 struct pipe_inode_info
*pipe
= i
->pipe
;
745 size_t off
= i
->iov_offset
;
747 size_t n
= off
- pipe
->bufs
[idx
].offset
;
753 if (!unroll
&& idx
== i
->start_idx
) {
758 idx
= pipe
->buffers
- 1;
759 off
= pipe
->bufs
[idx
].offset
+ pipe
->bufs
[idx
].len
;
766 if (unroll
<= i
->iov_offset
) {
767 i
->iov_offset
-= unroll
;
770 unroll
-= i
->iov_offset
;
771 if (i
->type
& ITER_BVEC
) {
772 const struct bio_vec
*bvec
= i
->bvec
;
774 size_t n
= (--bvec
)->bv_len
;
778 i
->iov_offset
= n
- unroll
;
783 } else { /* same logics for iovec and kvec */
784 const struct iovec
*iov
= i
->iov
;
786 size_t n
= (--iov
)->iov_len
;
790 i
->iov_offset
= n
- unroll
;
797 EXPORT_SYMBOL(iov_iter_revert
);
800 * Return the count of just the current iov_iter segment.
802 size_t iov_iter_single_seg_count(const struct iov_iter
*i
)
804 if (unlikely(i
->type
& ITER_PIPE
))
805 return i
->count
; // it is a silly place, anyway
808 else if (i
->type
& ITER_BVEC
)
809 return min(i
->count
, i
->bvec
->bv_len
- i
->iov_offset
);
811 return min(i
->count
, i
->iov
->iov_len
- i
->iov_offset
);
813 EXPORT_SYMBOL(iov_iter_single_seg_count
);
815 void iov_iter_kvec(struct iov_iter
*i
, int direction
,
816 const struct kvec
*kvec
, unsigned long nr_segs
,
819 BUG_ON(!(direction
& ITER_KVEC
));
822 i
->nr_segs
= nr_segs
;
826 EXPORT_SYMBOL(iov_iter_kvec
);
828 void iov_iter_bvec(struct iov_iter
*i
, int direction
,
829 const struct bio_vec
*bvec
, unsigned long nr_segs
,
832 BUG_ON(!(direction
& ITER_BVEC
));
835 i
->nr_segs
= nr_segs
;
839 EXPORT_SYMBOL(iov_iter_bvec
);
841 void iov_iter_pipe(struct iov_iter
*i
, int direction
,
842 struct pipe_inode_info
*pipe
,
845 BUG_ON(direction
!= ITER_PIPE
);
846 WARN_ON(pipe
->nrbufs
== pipe
->buffers
);
849 i
->idx
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
852 i
->start_idx
= i
->idx
;
854 EXPORT_SYMBOL(iov_iter_pipe
);
856 unsigned long iov_iter_alignment(const struct iov_iter
*i
)
858 unsigned long res
= 0;
859 size_t size
= i
->count
;
864 if (unlikely(i
->type
& ITER_PIPE
)) {
865 if (i
->iov_offset
&& allocated(&i
->pipe
->bufs
[i
->idx
]))
866 return size
| i
->iov_offset
;
869 iterate_all_kinds(i
, size
, v
,
870 (res
|= (unsigned long)v
.iov_base
| v
.iov_len
, 0),
871 res
|= v
.bv_offset
| v
.bv_len
,
872 res
|= (unsigned long)v
.iov_base
| v
.iov_len
876 EXPORT_SYMBOL(iov_iter_alignment
);
878 unsigned long iov_iter_gap_alignment(const struct iov_iter
*i
)
880 unsigned long res
= 0;
881 size_t size
= i
->count
;
885 if (unlikely(i
->type
& ITER_PIPE
)) {
890 iterate_all_kinds(i
, size
, v
,
891 (res
|= (!res
? 0 : (unsigned long)v
.iov_base
) |
892 (size
!= v
.iov_len
? size
: 0), 0),
893 (res
|= (!res
? 0 : (unsigned long)v
.bv_offset
) |
894 (size
!= v
.bv_len
? size
: 0)),
895 (res
|= (!res
? 0 : (unsigned long)v
.iov_base
) |
896 (size
!= v
.iov_len
? size
: 0))
900 EXPORT_SYMBOL(iov_iter_gap_alignment
);
902 static inline size_t __pipe_get_pages(struct iov_iter
*i
,
908 struct pipe_inode_info
*pipe
= i
->pipe
;
909 ssize_t n
= push_pipe(i
, maxsize
, &idx
, start
);
916 get_page(*pages
++ = pipe
->bufs
[idx
].page
);
917 idx
= next_idx(idx
, pipe
);
924 static ssize_t
pipe_get_pages(struct iov_iter
*i
,
925 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
935 data_start(i
, &idx
, start
);
936 /* some of this one + all after this one */
937 npages
= ((i
->pipe
->curbuf
- idx
- 1) & (i
->pipe
->buffers
- 1)) + 1;
938 capacity
= min(npages
,maxpages
) * PAGE_SIZE
- *start
;
940 return __pipe_get_pages(i
, min(maxsize
, capacity
), pages
, idx
, start
);
943 ssize_t
iov_iter_get_pages(struct iov_iter
*i
,
944 struct page
**pages
, size_t maxsize
, unsigned maxpages
,
947 if (maxsize
> i
->count
)
953 if (unlikely(i
->type
& ITER_PIPE
))
954 return pipe_get_pages(i
, pages
, maxsize
, maxpages
, start
);
955 iterate_all_kinds(i
, maxsize
, v
, ({
956 unsigned long addr
= (unsigned long)v
.iov_base
;
957 size_t len
= v
.iov_len
+ (*start
= addr
& (PAGE_SIZE
- 1));
961 if (len
> maxpages
* PAGE_SIZE
)
962 len
= maxpages
* PAGE_SIZE
;
963 addr
&= ~(PAGE_SIZE
- 1);
964 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
965 res
= get_user_pages_fast(addr
, n
, (i
->type
& WRITE
) != WRITE
, pages
);
966 if (unlikely(res
< 0))
968 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
970 /* can't be more than PAGE_SIZE */
971 *start
= v
.bv_offset
;
972 get_page(*pages
= v
.bv_page
);
980 EXPORT_SYMBOL(iov_iter_get_pages
);
982 static struct page
**get_pages_array(size_t n
)
984 struct page
**p
= kmalloc(n
* sizeof(struct page
*), GFP_KERNEL
);
986 p
= vmalloc(n
* sizeof(struct page
*));
990 static ssize_t
pipe_get_pages_alloc(struct iov_iter
*i
,
991 struct page
***pages
, size_t maxsize
,
1002 data_start(i
, &idx
, start
);
1003 /* some of this one + all after this one */
1004 npages
= ((i
->pipe
->curbuf
- idx
- 1) & (i
->pipe
->buffers
- 1)) + 1;
1005 n
= npages
* PAGE_SIZE
- *start
;
1009 npages
= DIV_ROUND_UP(maxsize
+ *start
, PAGE_SIZE
);
1010 p
= get_pages_array(npages
);
1013 n
= __pipe_get_pages(i
, maxsize
, p
, idx
, start
);
1021 ssize_t
iov_iter_get_pages_alloc(struct iov_iter
*i
,
1022 struct page
***pages
, size_t maxsize
,
1027 if (maxsize
> i
->count
)
1033 if (unlikely(i
->type
& ITER_PIPE
))
1034 return pipe_get_pages_alloc(i
, pages
, maxsize
, start
);
1035 iterate_all_kinds(i
, maxsize
, v
, ({
1036 unsigned long addr
= (unsigned long)v
.iov_base
;
1037 size_t len
= v
.iov_len
+ (*start
= addr
& (PAGE_SIZE
- 1));
1041 addr
&= ~(PAGE_SIZE
- 1);
1042 n
= DIV_ROUND_UP(len
, PAGE_SIZE
);
1043 p
= get_pages_array(n
);
1046 res
= get_user_pages_fast(addr
, n
, (i
->type
& WRITE
) != WRITE
, p
);
1047 if (unlikely(res
< 0)) {
1052 return (res
== n
? len
: res
* PAGE_SIZE
) - *start
;
1054 /* can't be more than PAGE_SIZE */
1055 *start
= v
.bv_offset
;
1056 *pages
= p
= get_pages_array(1);
1059 get_page(*p
= v
.bv_page
);
1067 EXPORT_SYMBOL(iov_iter_get_pages_alloc
);
1069 size_t csum_and_copy_from_iter(void *addr
, size_t bytes
, __wsum
*csum
,
1076 if (unlikely(i
->type
& ITER_PIPE
)) {
1080 iterate_and_advance(i
, bytes
, v
, ({
1082 next
= csum_and_copy_from_user(v
.iov_base
,
1083 (to
+= v
.iov_len
) - v
.iov_len
,
1084 v
.iov_len
, 0, &err
);
1086 sum
= csum_block_add(sum
, next
, off
);
1089 err
? v
.iov_len
: 0;
1091 char *p
= kmap_atomic(v
.bv_page
);
1092 next
= csum_partial_copy_nocheck(p
+ v
.bv_offset
,
1093 (to
+= v
.bv_len
) - v
.bv_len
,
1096 sum
= csum_block_add(sum
, next
, off
);
1099 next
= csum_partial_copy_nocheck(v
.iov_base
,
1100 (to
+= v
.iov_len
) - v
.iov_len
,
1102 sum
= csum_block_add(sum
, next
, off
);
1109 EXPORT_SYMBOL(csum_and_copy_from_iter
);
1111 size_t csum_and_copy_to_iter(const void *addr
, size_t bytes
, __wsum
*csum
,
1114 const char *from
= addr
;
1118 if (unlikely(i
->type
& ITER_PIPE
)) {
1119 WARN_ON(1); /* for now */
1122 iterate_and_advance(i
, bytes
, v
, ({
1124 next
= csum_and_copy_to_user((from
+= v
.iov_len
) - v
.iov_len
,
1126 v
.iov_len
, 0, &err
);
1128 sum
= csum_block_add(sum
, next
, off
);
1131 err
? v
.iov_len
: 0;
1133 char *p
= kmap_atomic(v
.bv_page
);
1134 next
= csum_partial_copy_nocheck((from
+= v
.bv_len
) - v
.bv_len
,
1138 sum
= csum_block_add(sum
, next
, off
);
1141 next
= csum_partial_copy_nocheck((from
+= v
.iov_len
) - v
.iov_len
,
1144 sum
= csum_block_add(sum
, next
, off
);
1151 EXPORT_SYMBOL(csum_and_copy_to_iter
);
1153 int iov_iter_npages(const struct iov_iter
*i
, int maxpages
)
1155 size_t size
= i
->count
;
1161 if (unlikely(i
->type
& ITER_PIPE
)) {
1162 struct pipe_inode_info
*pipe
= i
->pipe
;
1169 data_start(i
, &idx
, &off
);
1170 /* some of this one + all after this one */
1171 npages
= ((pipe
->curbuf
- idx
- 1) & (pipe
->buffers
- 1)) + 1;
1172 if (npages
>= maxpages
)
1174 } else iterate_all_kinds(i
, size
, v
, ({
1175 unsigned long p
= (unsigned long)v
.iov_base
;
1176 npages
+= DIV_ROUND_UP(p
+ v
.iov_len
, PAGE_SIZE
)
1178 if (npages
>= maxpages
)
1182 if (npages
>= maxpages
)
1185 unsigned long p
= (unsigned long)v
.iov_base
;
1186 npages
+= DIV_ROUND_UP(p
+ v
.iov_len
, PAGE_SIZE
)
1188 if (npages
>= maxpages
)
1194 EXPORT_SYMBOL(iov_iter_npages
);
1196 const void *dup_iter(struct iov_iter
*new, struct iov_iter
*old
, gfp_t flags
)
1199 if (unlikely(new->type
& ITER_PIPE
)) {
1203 if (new->type
& ITER_BVEC
)
1204 return new->bvec
= kmemdup(new->bvec
,
1205 new->nr_segs
* sizeof(struct bio_vec
),
1208 /* iovec and kvec have identical layout */
1209 return new->iov
= kmemdup(new->iov
,
1210 new->nr_segs
* sizeof(struct iovec
),
1213 EXPORT_SYMBOL(dup_iter
);
1216 * import_iovec() - Copy an array of &struct iovec from userspace
1217 * into the kernel, check that it is valid, and initialize a new
1218 * &struct iov_iter iterator to access it.
1220 * @type: One of %READ or %WRITE.
1221 * @uvector: Pointer to the userspace array.
1222 * @nr_segs: Number of elements in userspace array.
1223 * @fast_segs: Number of elements in @iov.
1224 * @iov: (input and output parameter) Pointer to pointer to (usually small
1225 * on-stack) kernel array.
1226 * @i: Pointer to iterator that will be initialized on success.
1228 * If the array pointed to by *@iov is large enough to hold all @nr_segs,
1229 * then this function places %NULL in *@iov on return. Otherwise, a new
1230 * array will be allocated and the result placed in *@iov. This means that
1231 * the caller may call kfree() on *@iov regardless of whether the small
1232 * on-stack array was used or not (and regardless of whether this function
1233 * returns an error or not).
1235 * Return: 0 on success or negative error code on error.
1237 int import_iovec(int type
, const struct iovec __user
* uvector
,
1238 unsigned nr_segs
, unsigned fast_segs
,
1239 struct iovec
**iov
, struct iov_iter
*i
)
1243 n
= rw_copy_check_uvector(type
, uvector
, nr_segs
, fast_segs
,
1251 iov_iter_init(i
, type
, p
, nr_segs
, n
);
1252 *iov
= p
== *iov
? NULL
: p
;
1255 EXPORT_SYMBOL(import_iovec
);
1257 #ifdef CONFIG_COMPAT
1258 #include <linux/compat.h>
1260 int compat_import_iovec(int type
, const struct compat_iovec __user
* uvector
,
1261 unsigned nr_segs
, unsigned fast_segs
,
1262 struct iovec
**iov
, struct iov_iter
*i
)
1266 n
= compat_rw_copy_check_uvector(type
, uvector
, nr_segs
, fast_segs
,
1274 iov_iter_init(i
, type
, p
, nr_segs
, n
);
1275 *iov
= p
== *iov
? NULL
: p
;
1280 int import_single_range(int rw
, void __user
*buf
, size_t len
,
1281 struct iovec
*iov
, struct iov_iter
*i
)
1283 if (len
> MAX_RW_COUNT
)
1285 if (unlikely(!access_ok(!rw
, buf
, len
)))
1288 iov
->iov_base
= buf
;
1290 iov_iter_init(i
, rw
, iov
, 1, len
);
1293 EXPORT_SYMBOL(import_single_range
);