This helper is used for iterating over multi-page bvec for bio split & merge code. Reviewed-by: Omar Sandoval <osandov@xxxxxx> Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- include/linux/bio.h | 25 ++++++++++++++++++++++--- include/linux/bvec.h | 36 +++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/include/linux/bio.h b/include/linux/bio.h index 056fb627edb3..7560209d6a8a 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -76,6 +76,9 @@ #define bio_data_dir(bio) \ (op_is_write(bio_op(bio)) ? WRITE : READ) +#define bio_iter_mp_iovec(bio, iter) \ + segment_iter_bvec((bio)->bi_io_vec, (iter)) + /* * Check whether this bio carries any data or not. A NULL bio is allowed. */ @@ -135,18 +138,24 @@ static inline bool bio_full(struct bio *bio) #define bio_for_each_segment_all(bvl, bio, i) \ for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++) -static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter, - unsigned bytes) +static inline void __bio_advance_iter(struct bio *bio, struct bvec_iter *iter, + unsigned bytes, unsigned max_seg_len) { iter->bi_sector += bytes >> 9; if (bio_no_advance_iter(bio)) iter->bi_size -= bytes; else - bvec_iter_advance(bio->bi_io_vec, iter, bytes); + __bvec_iter_advance(bio->bi_io_vec, iter, bytes, max_seg_len); /* TODO: It is reasonable to complete bio with error here. */ } +static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter, + unsigned bytes) +{ + __bio_advance_iter(bio, iter, bytes, PAGE_SIZE); +} + #define __bio_for_each_segment(bvl, bio, iter, start) \ for (iter = (start); \ (iter).bi_size && \ @@ -156,6 +165,16 @@ static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter, #define bio_for_each_segment(bvl, bio, iter) \ __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter) +#define __bio_for_each_bvec(bvl, bio, iter, start) \ + for (iter = (start); \ + (iter).bi_size && \ + ((bvl = bio_iter_mp_iovec((bio), (iter))), 1); \ + __bio_advance_iter((bio), &(iter), (bvl).bv_len, BVEC_MAX_LEN)) + +/* returns one real segment(multi-page bvec) each time */ +#define bio_for_each_bvec(bvl, bio, iter) \ + __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter) + #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len) static inline unsigned bio_segments(struct bio *bio) diff --git a/include/linux/bvec.h b/include/linux/bvec.h index ed90bbf4c9c9..b279218c5c4d 100644 --- a/include/linux/bvec.h +++ b/include/linux/bvec.h @@ -25,6 +25,8 @@ #include <linux/errno.h> #include <linux/mm.h> +#define BVEC_MAX_LEN ((unsigned int)-1) + /* * was unsigned short, but we might as well be ready for > 64kB I/O pages */ @@ -87,8 +89,15 @@ struct bvec_iter { .bv_offset = bvec_iter_offset((bvec), (iter)), \ }) -static inline bool bvec_iter_advance(const struct bio_vec *bv, - struct bvec_iter *iter, unsigned bytes) +#define segment_iter_bvec(bvec, iter) \ +((struct bio_vec) { \ + .bv_page = segment_iter_page((bvec), (iter)), \ + .bv_len = segment_iter_len((bvec), (iter)), \ + .bv_offset = segment_iter_offset((bvec), (iter)), \ +}) + +static inline bool __bvec_iter_advance(const struct bio_vec *bv, + struct bvec_iter *iter, unsigned bytes, unsigned max_seg_len) { if (WARN_ONCE(bytes > iter->bi_size, "Attempted to advance past end of bvec iter\n")) { @@ -97,12 +106,18 @@ static inline bool bvec_iter_advance(const struct bio_vec *bv, } while (bytes) { - unsigned iter_len = bvec_iter_len(bv, *iter); - unsigned len = min(bytes, iter_len); + unsigned segment_len = segment_iter_len(bv, *iter); - bytes -= len; - iter->bi_size -= len; - iter->bi_bvec_done += len; + if (max_seg_len < BVEC_MAX_LEN) + segment_len = min_t(unsigned, segment_len, + max_seg_len - + bvec_iter_offset(bv, *iter)); + + segment_len = min(bytes, segment_len); + + bytes -= segment_len; + iter->bi_size -= segment_len; + iter->bi_bvec_done += segment_len; if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) { iter->bi_bvec_done = 0; @@ -136,6 +151,13 @@ static inline bool bvec_iter_rewind(const struct bio_vec *bv, return true; } +static inline bool bvec_iter_advance(const struct bio_vec *bv, + struct bvec_iter *iter, + unsigned bytes) +{ + return __bvec_iter_advance(bv, iter, bytes, PAGE_SIZE); +} + #define for_each_bvec(bvl, bio_vec, iter, start) \ for (iter = (start); \ (iter).bi_size && \ -- 2.9.5