1d6d48196SJens Axboe /* 2d6d48196SJens Axboe * Functions related to segment and merge handling 3d6d48196SJens Axboe */ 4d6d48196SJens Axboe #include <linux/kernel.h> 5d6d48196SJens Axboe #include <linux/module.h> 6d6d48196SJens Axboe #include <linux/bio.h> 7d6d48196SJens Axboe #include <linux/blkdev.h> 8d6d48196SJens Axboe #include <linux/scatterlist.h> 9d6d48196SJens Axboe 10cda22646SMike Krinkin #include <trace/events/block.h> 11cda22646SMike Krinkin 12d6d48196SJens Axboe #include "blk.h" 13d6d48196SJens Axboe 1454efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 1554efd50bSKent Overstreet struct bio *bio, 16bdced438SMing Lei struct bio_set *bs, 17bdced438SMing Lei unsigned *nsegs) 1854efd50bSKent Overstreet { 1954efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 2054efd50bSKent Overstreet int alignment; 2154efd50bSKent Overstreet sector_t tmp; 2254efd50bSKent Overstreet unsigned split_sectors; 2354efd50bSKent Overstreet 24bdced438SMing Lei *nsegs = 1; 25bdced438SMing Lei 2654efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 2754efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 2854efd50bSKent Overstreet 2954efd50bSKent Overstreet max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); 3054efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 3154efd50bSKent Overstreet 3254efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 3354efd50bSKent Overstreet /* XXX: warn */ 3454efd50bSKent Overstreet return NULL; 3554efd50bSKent Overstreet } 3654efd50bSKent Overstreet 3754efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 3854efd50bSKent Overstreet return NULL; 3954efd50bSKent Overstreet 4054efd50bSKent Overstreet split_sectors = max_discard_sectors; 4154efd50bSKent Overstreet 4254efd50bSKent Overstreet /* 4354efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 4454efd50bSKent Overstreet * the previous aligned sector. 4554efd50bSKent Overstreet */ 4654efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 4754efd50bSKent Overstreet 4854efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 4954efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 5054efd50bSKent Overstreet 5154efd50bSKent Overstreet if (split_sectors > tmp) 5254efd50bSKent Overstreet split_sectors -= tmp; 5354efd50bSKent Overstreet 5454efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 5554efd50bSKent Overstreet } 5654efd50bSKent Overstreet 5754efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 5854efd50bSKent Overstreet struct bio *bio, 59bdced438SMing Lei struct bio_set *bs, 60bdced438SMing Lei unsigned *nsegs) 6154efd50bSKent Overstreet { 62bdced438SMing Lei *nsegs = 1; 63bdced438SMing Lei 6454efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 6554efd50bSKent Overstreet return NULL; 6654efd50bSKent Overstreet 6754efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 6854efd50bSKent Overstreet return NULL; 6954efd50bSKent Overstreet 7054efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 7154efd50bSKent Overstreet } 7254efd50bSKent Overstreet 73d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 74d0e5fbb0SMing Lei struct bio *bio) 75d0e5fbb0SMing Lei { 76d0e5fbb0SMing Lei unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector); 77d0e5fbb0SMing Lei unsigned mask = queue_logical_block_size(q) - 1; 78d0e5fbb0SMing Lei 79d0e5fbb0SMing Lei /* aligned to logical block size */ 80d0e5fbb0SMing Lei sectors &= ~(mask >> 9); 81d0e5fbb0SMing Lei 82d0e5fbb0SMing Lei return sectors; 83d0e5fbb0SMing Lei } 84d0e5fbb0SMing Lei 8554efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 8654efd50bSKent Overstreet struct bio *bio, 87bdced438SMing Lei struct bio_set *bs, 88bdced438SMing Lei unsigned *segs) 8954efd50bSKent Overstreet { 905014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 9154efd50bSKent Overstreet struct bvec_iter iter; 928ae12666SKent Overstreet unsigned seg_size = 0, nsegs = 0, sectors = 0; 9302e70742SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 9402e70742SMing Lei bool do_split = true; 9502e70742SMing Lei struct bio *new = NULL; 96d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 974d70dca4SMing Lei unsigned bvecs = 0; 9854efd50bSKent Overstreet 9954efd50bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 10054efd50bSKent Overstreet /* 1014d70dca4SMing Lei * With arbitrary bio size, the incoming bio may be very 1024d70dca4SMing Lei * big. We have to split the bio into small bios so that 1034d70dca4SMing Lei * each holds at most BIO_MAX_PAGES bvecs because 1044d70dca4SMing Lei * bio_clone() can fail to allocate big bvecs. 1054d70dca4SMing Lei * 1064d70dca4SMing Lei * It should have been better to apply the limit per 1074d70dca4SMing Lei * request queue in which bio_clone() is involved, 1084d70dca4SMing Lei * instead of globally. The biggest blocker is the 1094d70dca4SMing Lei * bio_clone() in bio bounce. 1104d70dca4SMing Lei * 1114d70dca4SMing Lei * If bio is splitted by this reason, we should have 1124d70dca4SMing Lei * allowed to continue bios merging, but don't do 1134d70dca4SMing Lei * that now for making the change simple. 1144d70dca4SMing Lei * 1154d70dca4SMing Lei * TODO: deal with bio bounce's bio_clone() gracefully 1164d70dca4SMing Lei * and convert the global limit into per-queue limit. 1174d70dca4SMing Lei */ 1184d70dca4SMing Lei if (bvecs++ >= BIO_MAX_PAGES) 1194d70dca4SMing Lei goto split; 1204d70dca4SMing Lei 1214d70dca4SMing Lei /* 12254efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 12354efd50bSKent Overstreet * offset would create a gap, disallow it. 12454efd50bSKent Overstreet */ 1255014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 12654efd50bSKent Overstreet goto split; 12754efd50bSKent Overstreet 128d0e5fbb0SMing Lei if (sectors + (bv.bv_len >> 9) > max_sectors) { 129e36f6204SKeith Busch /* 130e36f6204SKeith Busch * Consider this a new segment if we're splitting in 131e36f6204SKeith Busch * the middle of this vector. 132e36f6204SKeith Busch */ 133e36f6204SKeith Busch if (nsegs < queue_max_segments(q) && 134d0e5fbb0SMing Lei sectors < max_sectors) { 135e36f6204SKeith Busch nsegs++; 136d0e5fbb0SMing Lei sectors = max_sectors; 137e36f6204SKeith Busch } 138d0e5fbb0SMing Lei if (sectors) 139e36f6204SKeith Busch goto split; 140d0e5fbb0SMing Lei /* Make this single bvec as the 1st segment */ 141e36f6204SKeith Busch } 142e36f6204SKeith Busch 1435014c311SJens Axboe if (bvprvp && blk_queue_cluster(q)) { 14454efd50bSKent Overstreet if (seg_size + bv.bv_len > queue_max_segment_size(q)) 14554efd50bSKent Overstreet goto new_segment; 1465014c311SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv)) 14754efd50bSKent Overstreet goto new_segment; 1485014c311SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv)) 14954efd50bSKent Overstreet goto new_segment; 15054efd50bSKent Overstreet 15154efd50bSKent Overstreet seg_size += bv.bv_len; 15254efd50bSKent Overstreet bvprv = bv; 153578270bfSMing Lei bvprvp = &bvprv; 15452cc6eeaSMing Lei sectors += bv.bv_len >> 9; 155a88d32afSMing Lei 156a88d32afSMing Lei if (nsegs == 1 && seg_size > front_seg_size) 157a88d32afSMing Lei front_seg_size = seg_size; 15854efd50bSKent Overstreet continue; 15954efd50bSKent Overstreet } 16054efd50bSKent Overstreet new_segment: 16154efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 16254efd50bSKent Overstreet goto split; 16354efd50bSKent Overstreet 16454efd50bSKent Overstreet nsegs++; 16554efd50bSKent Overstreet bvprv = bv; 166578270bfSMing Lei bvprvp = &bvprv; 16754efd50bSKent Overstreet seg_size = bv.bv_len; 16852cc6eeaSMing Lei sectors += bv.bv_len >> 9; 16902e70742SMing Lei 17002e70742SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 17102e70742SMing Lei front_seg_size = seg_size; 17254efd50bSKent Overstreet } 17354efd50bSKent Overstreet 17402e70742SMing Lei do_split = false; 17554efd50bSKent Overstreet split: 176bdced438SMing Lei *segs = nsegs; 17702e70742SMing Lei 17802e70742SMing Lei if (do_split) { 17902e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 18002e70742SMing Lei if (new) 18102e70742SMing Lei bio = new; 18202e70742SMing Lei } 18302e70742SMing Lei 18402e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 18502e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 18602e70742SMing Lei bio->bi_seg_back_size = seg_size; 18702e70742SMing Lei 18802e70742SMing Lei return do_split ? new : NULL; 18954efd50bSKent Overstreet } 19054efd50bSKent Overstreet 19154efd50bSKent Overstreet void blk_queue_split(struct request_queue *q, struct bio **bio, 19254efd50bSKent Overstreet struct bio_set *bs) 19354efd50bSKent Overstreet { 194bdced438SMing Lei struct bio *split, *res; 195bdced438SMing Lei unsigned nsegs; 19654efd50bSKent Overstreet 1977afafc8aSAdrian Hunter switch (bio_op(*bio)) { 1987afafc8aSAdrian Hunter case REQ_OP_DISCARD: 1997afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 200bdced438SMing Lei split = blk_bio_discard_split(q, *bio, bs, &nsegs); 2017afafc8aSAdrian Hunter break; 202a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 203a6f0788eSChaitanya Kulkarni split = NULL; 204a6f0788eSChaitanya Kulkarni nsegs = (*bio)->bi_phys_segments; 205a6f0788eSChaitanya Kulkarni break; 2067afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 207bdced438SMing Lei split = blk_bio_write_same_split(q, *bio, bs, &nsegs); 2087afafc8aSAdrian Hunter break; 2097afafc8aSAdrian Hunter default: 210bdced438SMing Lei split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs); 2117afafc8aSAdrian Hunter break; 2127afafc8aSAdrian Hunter } 213bdced438SMing Lei 214bdced438SMing Lei /* physical segments can be figured out during splitting */ 215bdced438SMing Lei res = split ? split : *bio; 216bdced438SMing Lei res->bi_phys_segments = nsegs; 217bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 21854efd50bSKent Overstreet 21954efd50bSKent Overstreet if (split) { 2206ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 2211eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 2226ac45aebSMing Lei 22354efd50bSKent Overstreet bio_chain(split, *bio); 224cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 22554efd50bSKent Overstreet generic_make_request(*bio); 22654efd50bSKent Overstreet *bio = split; 22754efd50bSKent Overstreet } 22854efd50bSKent Overstreet } 22954efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 23054efd50bSKent Overstreet 2311e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 23207388549SMing Lei struct bio *bio, 23307388549SMing Lei bool no_sg_merge) 234d6d48196SJens Axboe { 2357988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 23654efd50bSKent Overstreet int cluster, prev = 0; 2371e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 23859247eaeSJens Axboe struct bio *fbio, *bbio; 2397988613bSKent Overstreet struct bvec_iter iter; 240d6d48196SJens Axboe 2411e428079SJens Axboe if (!bio) 2421e428079SJens Axboe return 0; 243d6d48196SJens Axboe 244a6f0788eSChaitanya Kulkarni switch (bio_op(bio)) { 245a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 246a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 247a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 248f9d03f96SChristoph Hellwig return 0; 249f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 2505cb8850cSKent Overstreet return 1; 251a6f0788eSChaitanya Kulkarni } 2525cb8850cSKent Overstreet 2531e428079SJens Axboe fbio = bio; 254e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 2555df97b91SMikulas Patocka seg_size = 0; 2562c8919deSAndi Kleen nr_phys_segs = 0; 2571e428079SJens Axboe for_each_bio(bio) { 2587988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 259d6d48196SJens Axboe /* 26005f1dd53SJens Axboe * If SG merging is disabled, each bio vector is 26105f1dd53SJens Axboe * a segment 26205f1dd53SJens Axboe */ 26305f1dd53SJens Axboe if (no_sg_merge) 26405f1dd53SJens Axboe goto new_segment; 26505f1dd53SJens Axboe 26654efd50bSKent Overstreet if (prev && cluster) { 2677988613bSKent Overstreet if (seg_size + bv.bv_len 268ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 269d6d48196SJens Axboe goto new_segment; 2707988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) 271d6d48196SJens Axboe goto new_segment; 2727988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) 273d6d48196SJens Axboe goto new_segment; 274d6d48196SJens Axboe 2757988613bSKent Overstreet seg_size += bv.bv_len; 276d6d48196SJens Axboe bvprv = bv; 277d6d48196SJens Axboe continue; 278d6d48196SJens Axboe } 279d6d48196SJens Axboe new_segment: 2801e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 2811e428079SJens Axboe fbio->bi_seg_front_size) 2821e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 28386771427SFUJITA Tomonori 284d6d48196SJens Axboe nr_phys_segs++; 285d6d48196SJens Axboe bvprv = bv; 28654efd50bSKent Overstreet prev = 1; 2877988613bSKent Overstreet seg_size = bv.bv_len; 288d6d48196SJens Axboe } 28959247eaeSJens Axboe bbio = bio; 2901e428079SJens Axboe } 291d6d48196SJens Axboe 29259247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 29359247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 29459247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 29559247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 2961e428079SJens Axboe 2971e428079SJens Axboe return nr_phys_segs; 2981e428079SJens Axboe } 2991e428079SJens Axboe 3001e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 3011e428079SJens Axboe { 30207388549SMing Lei bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, 30307388549SMing Lei &rq->q->queue_flags); 30407388549SMing Lei 30507388549SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, 30607388549SMing Lei no_sg_merge); 307d6d48196SJens Axboe } 308d6d48196SJens Axboe 309d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 310d6d48196SJens Axboe { 3117f60dcaaSMing Lei unsigned short seg_cnt; 312764f612cSMing Lei 3137f60dcaaSMing Lei /* estimate segment number by bi_vcnt for non-cloned bio */ 3147f60dcaaSMing Lei if (bio_flagged(bio, BIO_CLONED)) 3157f60dcaaSMing Lei seg_cnt = bio_segments(bio); 3167f60dcaaSMing Lei else 3177f60dcaaSMing Lei seg_cnt = bio->bi_vcnt; 3187f60dcaaSMing Lei 3197f60dcaaSMing Lei if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && 3207f60dcaaSMing Lei (seg_cnt < queue_max_segments(q))) 3217f60dcaaSMing Lei bio->bi_phys_segments = seg_cnt; 32205f1dd53SJens Axboe else { 323d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 3241e428079SJens Axboe 325d6d48196SJens Axboe bio->bi_next = NULL; 3267f60dcaaSMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); 327d6d48196SJens Axboe bio->bi_next = nxt; 32805f1dd53SJens Axboe } 32905f1dd53SJens Axboe 330b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 331d6d48196SJens Axboe } 332d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 333d6d48196SJens Axboe 334d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 335d6d48196SJens Axboe struct bio *nxt) 336d6d48196SJens Axboe { 3372b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 338f619d254SKent Overstreet 339e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 340d6d48196SJens Axboe return 0; 341d6d48196SJens Axboe 34286771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 343ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 344d6d48196SJens Axboe return 0; 345d6d48196SJens Axboe 346e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 347e17fc0a1SDavid Woodhouse return 1; 348e17fc0a1SDavid Woodhouse 349e827091cSMing Lei bio_get_last_bvec(bio, &end_bv); 350e827091cSMing Lei bio_get_first_bvec(nxt, &nxt_bv); 351f619d254SKent Overstreet 352f619d254SKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) 353e17fc0a1SDavid Woodhouse return 0; 354e17fc0a1SDavid Woodhouse 355d6d48196SJens Axboe /* 356e17fc0a1SDavid Woodhouse * bio and nxt are contiguous in memory; check if the queue allows 357d6d48196SJens Axboe * these two to be merged into one 358d6d48196SJens Axboe */ 359f619d254SKent Overstreet if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) 360d6d48196SJens Axboe return 1; 361d6d48196SJens Axboe 362d6d48196SJens Axboe return 0; 363d6d48196SJens Axboe } 364d6d48196SJens Axboe 3657988613bSKent Overstreet static inline void 366963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 3677988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 368963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 369963ab9e5SAsias He { 370963ab9e5SAsias He 371963ab9e5SAsias He int nbytes = bvec->bv_len; 372963ab9e5SAsias He 3737988613bSKent Overstreet if (*sg && *cluster) { 374963ab9e5SAsias He if ((*sg)->length + nbytes > queue_max_segment_size(q)) 375963ab9e5SAsias He goto new_segment; 376963ab9e5SAsias He 3777988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 378963ab9e5SAsias He goto new_segment; 3797988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 380963ab9e5SAsias He goto new_segment; 381963ab9e5SAsias He 382963ab9e5SAsias He (*sg)->length += nbytes; 383963ab9e5SAsias He } else { 384963ab9e5SAsias He new_segment: 385963ab9e5SAsias He if (!*sg) 386963ab9e5SAsias He *sg = sglist; 387963ab9e5SAsias He else { 388963ab9e5SAsias He /* 389963ab9e5SAsias He * If the driver previously mapped a shorter 390963ab9e5SAsias He * list, we could see a termination bit 391963ab9e5SAsias He * prematurely unless it fully inits the sg 392963ab9e5SAsias He * table on each mapping. We KNOW that there 393963ab9e5SAsias He * must be more entries here or the driver 394963ab9e5SAsias He * would be buggy, so force clear the 395963ab9e5SAsias He * termination bit to avoid doing a full 396963ab9e5SAsias He * sg_init_table() in drivers for each command. 397963ab9e5SAsias He */ 398c8164d89SPaolo Bonzini sg_unmark_end(*sg); 399963ab9e5SAsias He *sg = sg_next(*sg); 400963ab9e5SAsias He } 401963ab9e5SAsias He 402963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 403963ab9e5SAsias He (*nsegs)++; 404963ab9e5SAsias He } 4057988613bSKent Overstreet *bvprv = *bvec; 406963ab9e5SAsias He } 407963ab9e5SAsias He 408f9d03f96SChristoph Hellwig static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv, 409f9d03f96SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **sg) 410f9d03f96SChristoph Hellwig { 411f9d03f96SChristoph Hellwig *sg = sglist; 412f9d03f96SChristoph Hellwig sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 413f9d03f96SChristoph Hellwig return 1; 414f9d03f96SChristoph Hellwig } 415f9d03f96SChristoph Hellwig 4165cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4175cb8850cSKent Overstreet struct scatterlist *sglist, 4185cb8850cSKent Overstreet struct scatterlist **sg) 4195cb8850cSKent Overstreet { 4205cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 4215cb8850cSKent Overstreet struct bvec_iter iter; 422f9d03f96SChristoph Hellwig int cluster = blk_queue_cluster(q), nsegs = 0; 4235cb8850cSKent Overstreet 4245cb8850cSKent Overstreet for_each_bio(bio) 4255cb8850cSKent Overstreet bio_for_each_segment(bvec, bio, iter) 4265cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 4275cb8850cSKent Overstreet &nsegs, &cluster); 4285cb8850cSKent Overstreet 4295cb8850cSKent Overstreet return nsegs; 4305cb8850cSKent Overstreet } 4315cb8850cSKent Overstreet 432d6d48196SJens Axboe /* 433d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 434d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 435d6d48196SJens Axboe */ 436d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 437d6d48196SJens Axboe struct scatterlist *sglist) 438d6d48196SJens Axboe { 4395cb8850cSKent Overstreet struct scatterlist *sg = NULL; 4405cb8850cSKent Overstreet int nsegs = 0; 441d6d48196SJens Axboe 442f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 443f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg); 444f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 445f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg); 446f9d03f96SChristoph Hellwig else if (rq->bio) 4475cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 448f18573abSFUJITA Tomonori 449e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 4502e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 4512e46e8b2STejun Heo unsigned int pad_len = 4522e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 453f18573abSFUJITA Tomonori 454f18573abSFUJITA Tomonori sg->length += pad_len; 455f18573abSFUJITA Tomonori rq->extra_len += pad_len; 456f18573abSFUJITA Tomonori } 457f18573abSFUJITA Tomonori 4582fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 459a8ebb056SMike Christie if (op_is_write(req_op(rq))) 460db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 461db0a2e00STejun Heo 462da81ed16SDan Williams sg_unmark_end(sg); 463d6d48196SJens Axboe sg = sg_next(sg); 464d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 465d6d48196SJens Axboe q->dma_drain_size, 466d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 467d6d48196SJens Axboe (PAGE_SIZE - 1)); 468d6d48196SJens Axboe nsegs++; 4697a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 470d6d48196SJens Axboe } 471d6d48196SJens Axboe 472d6d48196SJens Axboe if (sg) 473d6d48196SJens Axboe sg_mark_end(sg); 474d6d48196SJens Axboe 47512e57f59SMing Lei /* 47612e57f59SMing Lei * Something must have been wrong if the figured number of 47712e57f59SMing Lei * segment is bigger than number of req's physical segments 47812e57f59SMing Lei */ 479f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 48012e57f59SMing Lei 481d6d48196SJens Axboe return nsegs; 482d6d48196SJens Axboe } 483d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 484d6d48196SJens Axboe 485d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 486d6d48196SJens Axboe struct request *req, 487d6d48196SJens Axboe struct bio *bio) 488d6d48196SJens Axboe { 489d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 490d6d48196SJens Axboe 49113f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 49213f05c8dSMartin K. Petersen goto no_merge; 49313f05c8dSMartin K. Petersen 4944eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 49513f05c8dSMartin K. Petersen goto no_merge; 496d6d48196SJens Axboe 497d6d48196SJens Axboe /* 498d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 499d6d48196SJens Axboe * counters. 500d6d48196SJens Axboe */ 501d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 502d6d48196SJens Axboe return 1; 50313f05c8dSMartin K. Petersen 50413f05c8dSMartin K. Petersen no_merge: 505e0c72300SRitesh Harjani req_set_nomerge(q, req); 50613f05c8dSMartin K. Petersen return 0; 507d6d48196SJens Axboe } 508d6d48196SJens Axboe 509d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 510d6d48196SJens Axboe struct bio *bio) 511d6d48196SJens Axboe { 5125e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5135e7c4274SJens Axboe return 0; 5147f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5157f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5167f39add3SSagi Grimberg return 0; 517f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 51817007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 519e0c72300SRitesh Harjani req_set_nomerge(q, req); 520d6d48196SJens Axboe return 0; 521d6d48196SJens Axboe } 5222cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 523d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 5242cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 525d6d48196SJens Axboe blk_recount_segments(q, bio); 526d6d48196SJens Axboe 527d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 528d6d48196SJens Axboe } 529d6d48196SJens Axboe 530d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 531d6d48196SJens Axboe struct bio *bio) 532d6d48196SJens Axboe { 5335e7c4274SJens Axboe 5345e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 5355e7c4274SJens Axboe return 0; 5367f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5377f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 5387f39add3SSagi Grimberg return 0; 539f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 54017007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 541e0c72300SRitesh Harjani req_set_nomerge(q, req); 542d6d48196SJens Axboe return 0; 543d6d48196SJens Axboe } 5442cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 545d6d48196SJens Axboe blk_recount_segments(q, bio); 5462cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 547d6d48196SJens Axboe blk_recount_segments(q, req->bio); 548d6d48196SJens Axboe 549d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 550d6d48196SJens Axboe } 551d6d48196SJens Axboe 552e7e24500SJens Axboe /* 553e7e24500SJens Axboe * blk-mq uses req->special to carry normal driver per-request payload, it 554e7e24500SJens Axboe * does not indicate a prepared command that we cannot merge with. 555e7e24500SJens Axboe */ 556e7e24500SJens Axboe static bool req_no_special_merge(struct request *req) 557e7e24500SJens Axboe { 558e7e24500SJens Axboe struct request_queue *q = req->q; 559e7e24500SJens Axboe 560e7e24500SJens Axboe return !q->mq_ops && req->special; 561e7e24500SJens Axboe } 562e7e24500SJens Axboe 563d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 564d6d48196SJens Axboe struct request *next) 565d6d48196SJens Axboe { 566d6d48196SJens Axboe int total_phys_segments; 56786771427SFUJITA Tomonori unsigned int seg_size = 56886771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 569d6d48196SJens Axboe 570d6d48196SJens Axboe /* 571d6d48196SJens Axboe * First check if the either of the requests are re-queued 572d6d48196SJens Axboe * requests. Can't merge them if they are. 573d6d48196SJens Axboe */ 574e7e24500SJens Axboe if (req_no_special_merge(req) || req_no_special_merge(next)) 575d6d48196SJens Axboe return 0; 576d6d48196SJens Axboe 5775e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 578854fbb9cSKeith Busch return 0; 579854fbb9cSKeith Busch 580d6d48196SJens Axboe /* 581d6d48196SJens Axboe * Will it become too large? 582d6d48196SJens Axboe */ 583f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 58417007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 585d6d48196SJens Axboe return 0; 586d6d48196SJens Axboe 587d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 58886771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 58986771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 59086771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 59186771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 59286771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 593d6d48196SJens Axboe total_phys_segments--; 59486771427SFUJITA Tomonori } 595d6d48196SJens Axboe 5968a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 597d6d48196SJens Axboe return 0; 598d6d48196SJens Axboe 5994eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 60013f05c8dSMartin K. Petersen return 0; 60113f05c8dSMartin K. Petersen 602d6d48196SJens Axboe /* Merge is OK... */ 603d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 604d6d48196SJens Axboe return 1; 605d6d48196SJens Axboe } 606d6d48196SJens Axboe 60780a761fdSTejun Heo /** 60880a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 60980a761fdSTejun Heo * @rq: request to mark as mixed merge 61080a761fdSTejun Heo * 61180a761fdSTejun Heo * Description: 61280a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 61380a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 61480a761fdSTejun Heo * merged. 61580a761fdSTejun Heo */ 61680a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 61780a761fdSTejun Heo { 61880a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 61980a761fdSTejun Heo struct bio *bio; 62080a761fdSTejun Heo 621e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 62280a761fdSTejun Heo return; 62380a761fdSTejun Heo 62480a761fdSTejun Heo /* 62580a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 62680a761fdSTejun Heo * contained bios. It will just track those of the first one. 62780a761fdSTejun Heo * Distributes the attributs to each bio. 62880a761fdSTejun Heo */ 62980a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 6301eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 6311eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 6321eff9d32SJens Axboe bio->bi_opf |= ff; 63380a761fdSTejun Heo } 634e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 63580a761fdSTejun Heo } 63680a761fdSTejun Heo 63726308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 63826308eabSJerome Marchand { 63926308eabSJerome Marchand if (blk_do_io_stat(req)) { 64026308eabSJerome Marchand struct hd_struct *part; 64126308eabSJerome Marchand int cpu; 64226308eabSJerome Marchand 64326308eabSJerome Marchand cpu = part_stat_lock(); 64409e099d4SJerome Marchand part = req->part; 64526308eabSJerome Marchand 64626308eabSJerome Marchand part_round_stats(cpu, part); 647316d315bSNikanth Karthikesan part_dec_in_flight(part, rq_data_dir(req)); 64826308eabSJerome Marchand 6496c23a968SJens Axboe hd_struct_put(part); 65026308eabSJerome Marchand part_stat_unlock(); 65126308eabSJerome Marchand } 65226308eabSJerome Marchand } 65326308eabSJerome Marchand 654d6d48196SJens Axboe /* 655b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 656b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 657d6d48196SJens Axboe */ 658b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 659b973cb7eSJens Axboe struct request *req, struct request *next) 660d6d48196SJens Axboe { 661d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 662b973cb7eSJens Axboe return NULL; 663d6d48196SJens Axboe 664288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 665b973cb7eSJens Axboe return NULL; 666f31dc1cdSMartin K. Petersen 667d6d48196SJens Axboe /* 668d6d48196SJens Axboe * not contiguous 669d6d48196SJens Axboe */ 67083096ebfSTejun Heo if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) 671b973cb7eSJens Axboe return NULL; 672d6d48196SJens Axboe 673d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 674d6d48196SJens Axboe || req->rq_disk != next->rq_disk 675e7e24500SJens Axboe || req_no_special_merge(next)) 676b973cb7eSJens Axboe return NULL; 677d6d48196SJens Axboe 6788fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 6794363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 680b973cb7eSJens Axboe return NULL; 6814363ac7cSMartin K. Petersen 682d6d48196SJens Axboe /* 683d6d48196SJens Axboe * If we are allowed to merge, then append bio list 684d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 685d6d48196SJens Axboe * will have updated segment counts, update sector 686d6d48196SJens Axboe * counts here. 687d6d48196SJens Axboe */ 688d6d48196SJens Axboe if (!ll_merge_requests_fn(q, req, next)) 689b973cb7eSJens Axboe return NULL; 690d6d48196SJens Axboe 691d6d48196SJens Axboe /* 69280a761fdSTejun Heo * If failfast settings disagree or any of the two is already 69380a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 69480a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 69580a761fdSTejun Heo * set properly. 69680a761fdSTejun Heo */ 697e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 69880a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 69980a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 70080a761fdSTejun Heo blk_rq_set_mixed_merge(req); 70180a761fdSTejun Heo blk_rq_set_mixed_merge(next); 70280a761fdSTejun Heo } 70380a761fdSTejun Heo 70480a761fdSTejun Heo /* 705d6d48196SJens Axboe * At this point we have either done a back merge 706d6d48196SJens Axboe * or front merge. We need the smaller start_time of 707d6d48196SJens Axboe * the merged requests to be the current request 708d6d48196SJens Axboe * for accounting purposes. 709d6d48196SJens Axboe */ 710d6d48196SJens Axboe if (time_after(req->start_time, next->start_time)) 711d6d48196SJens Axboe req->start_time = next->start_time; 712d6d48196SJens Axboe 713d6d48196SJens Axboe req->biotail->bi_next = next->bio; 714d6d48196SJens Axboe req->biotail = next->biotail; 715d6d48196SJens Axboe 716a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 717d6d48196SJens Axboe 718d6d48196SJens Axboe elv_merge_requests(q, req, next); 719d6d48196SJens Axboe 72042dad764SJerome Marchand /* 72142dad764SJerome Marchand * 'next' is going away, so update stats accordingly 72242dad764SJerome Marchand */ 72342dad764SJerome Marchand blk_account_io_merge(next); 724d6d48196SJens Axboe 725d6d48196SJens Axboe req->ioprio = ioprio_best(req->ioprio, next->ioprio); 726ab780f1eSJens Axboe if (blk_rq_cpu_valid(next)) 727ab780f1eSJens Axboe req->cpu = next->cpu; 728d6d48196SJens Axboe 729e4d750c9SJens Axboe /* 730e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 731e4d750c9SJens Axboe * the caller to free 732e4d750c9SJens Axboe */ 7331cd96c24SBoaz Harrosh next->bio = NULL; 734b973cb7eSJens Axboe return next; 735d6d48196SJens Axboe } 736d6d48196SJens Axboe 737b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 738d6d48196SJens Axboe { 739d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 740d6d48196SJens Axboe 741d6d48196SJens Axboe if (next) 742d6d48196SJens Axboe return attempt_merge(q, rq, next); 743d6d48196SJens Axboe 744b973cb7eSJens Axboe return NULL; 745d6d48196SJens Axboe } 746d6d48196SJens Axboe 747b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 748d6d48196SJens Axboe { 749d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 750d6d48196SJens Axboe 751d6d48196SJens Axboe if (prev) 752d6d48196SJens Axboe return attempt_merge(q, prev, rq); 753d6d48196SJens Axboe 754b973cb7eSJens Axboe return NULL; 755d6d48196SJens Axboe } 7565e84ea3aSJens Axboe 7575e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 7585e84ea3aSJens Axboe struct request *next) 7595e84ea3aSJens Axboe { 76072ef799bSTahsin Erdogan struct elevator_queue *e = q->elevator; 761e4d750c9SJens Axboe struct request *free; 76272ef799bSTahsin Erdogan 763bd166ef1SJens Axboe if (!e->uses_mq && e->type->ops.sq.elevator_allow_rq_merge_fn) 764c51ca6cfSJens Axboe if (!e->type->ops.sq.elevator_allow_rq_merge_fn(q, rq, next)) 76572ef799bSTahsin Erdogan return 0; 76672ef799bSTahsin Erdogan 767e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 768e4d750c9SJens Axboe if (free) { 769e4d750c9SJens Axboe __blk_put_request(q, free); 770e4d750c9SJens Axboe return 1; 771e4d750c9SJens Axboe } 772e4d750c9SJens Axboe 773e4d750c9SJens Axboe return 0; 7745e84ea3aSJens Axboe } 775050c8ea8STejun Heo 776050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 777050c8ea8STejun Heo { 778e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 779050c8ea8STejun Heo return false; 780050c8ea8STejun Heo 781288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 782f31dc1cdSMartin K. Petersen return false; 783f31dc1cdSMartin K. Petersen 784050c8ea8STejun Heo /* different data direction or already started, don't merge */ 785050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 786050c8ea8STejun Heo return false; 787050c8ea8STejun Heo 788050c8ea8STejun Heo /* must be same device and not a special request */ 789e7e24500SJens Axboe if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq)) 790050c8ea8STejun Heo return false; 791050c8ea8STejun Heo 792050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 7934eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 794050c8ea8STejun Heo return false; 795050c8ea8STejun Heo 7964363ac7cSMartin K. Petersen /* must be using the same buffer */ 7978fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 7984363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 7994363ac7cSMartin K. Petersen return false; 8004363ac7cSMartin K. Petersen 801050c8ea8STejun Heo return true; 802050c8ea8STejun Heo } 803050c8ea8STejun Heo 80434fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 805050c8ea8STejun Heo { 806*1e739730SChristoph Hellwig if (req_op(rq) == REQ_OP_DISCARD && 807*1e739730SChristoph Hellwig queue_max_discard_segments(rq->q) > 1) 808*1e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 809*1e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 810050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 8114f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 812050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 813050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 814050c8ea8STejun Heo } 815