Commit 85b9f66a authored by Asias He's avatar Asias He Committed by Jens Axboe

block: Add blk_bio_map_sg() helper

Add a helper to map a bio to a scatterlist, modelled after

This helper is useful for any driver that wants to create
a scatterlist from its ->make_request_fn method.

Changes in v2:
 - Use __blk_segment_map_sg to avoid duplicated code
 - Add cocbook style function comment

Cc: Rusty Russell <>
Cc: Christoph Hellwig <>
Cc: Tejun Heo <>
Cc: Shaohua Li <>
Cc: "Michael S. Tsirkin" <>
Signed-off-by: default avatarChristoph Hellwig <>
Signed-off-by: default avatarMinchan Kim <>
Signed-off-by: default avatarAsias He <>
Signed-off-by: default avatarJens Axboe <>
parent 963ab9e5
......@@ -209,6 +209,43 @@ int blk_rq_map_sg(struct request_queue *q, struct request *rq,
* blk_bio_map_sg - map a bio to a scatterlist
* @q: request_queue in question
* @bio: bio being mapped
* @sglist: scatterlist being mapped
* Note:
* Caller must make sure sg can hold bio->bi_phys_segments entries
* Will return the number of sg entries setup
int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
struct scatterlist *sglist)
struct bio_vec *bvec, *bvprv;
struct scatterlist *sg;
int nsegs, cluster;
unsigned long i;
nsegs = 0;
cluster = blk_queue_cluster(q);
bvprv = NULL;
sg = NULL;
bio_for_each_segment(bvec, bio, i) {
__blk_segment_map_sg(q, bvec, sglist, &bvprv, &sg,
&nsegs, &cluster);
} /* segments in bio */
if (sg)
BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
return nsegs;
static inline int ll_new_hw_segment(struct request_queue *q,
struct request *req,
struct bio *bio)
......@@ -894,6 +894,8 @@ extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable);
extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev);
extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
extern int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
struct scatterlist *sglist);
extern void blk_dump_rq_flags(struct request *, char *);
extern long nr_blockdev_pages(void);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment