Add a helper to insert a bio into a bio_list so that the list remains sorted by bi_sector.
While it is on the upper hand of the size for an inline function, it is a trade off for having it with the common helpers, but not bloating the kernel unless the so far only user is enabled. If it gets more widely used moving it out of line should be considered. Signed-off-by: Christoph Hellwig <[email protected]> --- include/linux/bio.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/linux/bio.h b/include/linux/bio.h index ad2d57908c1c..e5f92bc88eea 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -565,6 +565,27 @@ static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio) bl->tail = bio; } +static inline void bio_list_add_sorted(struct bio_list *bl, struct bio *bio) +{ + if (bio_list_empty(bl) || + bio->bi_iter.bi_sector > bl->tail->bi_iter.bi_sector) { + bio_list_add(bl, bio); + } else if (bio->bi_iter.bi_sector < bl->head->bi_iter.bi_sector) { + bio_list_add_head(bl, bio); + } else { + struct bio *n = bl->head, *next; + + while ((next = n->bi_next) != NULL) { + if (bio->bi_iter.bi_sector < next->bi_iter.bi_sector) { + bio->bi_next = next; + n->bi_next = bio; + break; + } + n = next; + } + } +} + static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2) { if (!bl2->head) -- 2.47.3
