1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d6d48196SJens Axboe /* 3d6d48196SJens Axboe * Functions related to segment and merge handling 4d6d48196SJens Axboe */ 5d6d48196SJens Axboe #include <linux/kernel.h> 6d6d48196SJens Axboe #include <linux/module.h> 7d6d48196SJens Axboe #include <linux/bio.h> 8d6d48196SJens Axboe #include <linux/blkdev.h> 9d6d48196SJens Axboe #include <linux/scatterlist.h> 10d6d48196SJens Axboe 11cda22646SMike Krinkin #include <trace/events/block.h> 12cda22646SMike Krinkin 13d6d48196SJens Axboe #include "blk.h" 14d6d48196SJens Axboe 1554efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 1654efd50bSKent Overstreet struct bio *bio, 17bdced438SMing Lei struct bio_set *bs, 18bdced438SMing Lei unsigned *nsegs) 1954efd50bSKent Overstreet { 2054efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 2154efd50bSKent Overstreet int alignment; 2254efd50bSKent Overstreet sector_t tmp; 2354efd50bSKent Overstreet unsigned split_sectors; 2454efd50bSKent Overstreet 25bdced438SMing Lei *nsegs = 1; 26bdced438SMing Lei 2754efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 2854efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 2954efd50bSKent Overstreet 3054efd50bSKent Overstreet max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); 3154efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 3254efd50bSKent Overstreet 3354efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 3454efd50bSKent Overstreet /* XXX: warn */ 3554efd50bSKent Overstreet return NULL; 3654efd50bSKent Overstreet } 3754efd50bSKent Overstreet 3854efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 3954efd50bSKent Overstreet return NULL; 4054efd50bSKent Overstreet 4154efd50bSKent Overstreet split_sectors = max_discard_sectors; 4254efd50bSKent Overstreet 4354efd50bSKent Overstreet /* 4454efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 4554efd50bSKent Overstreet * the previous aligned sector. 4654efd50bSKent Overstreet */ 4754efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 4854efd50bSKent Overstreet 4954efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 5054efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 5154efd50bSKent Overstreet 5254efd50bSKent Overstreet if (split_sectors > tmp) 5354efd50bSKent Overstreet split_sectors -= tmp; 5454efd50bSKent Overstreet 5554efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 5654efd50bSKent Overstreet } 5754efd50bSKent Overstreet 58885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 59885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 60885fa13fSChristoph Hellwig { 61885fa13fSChristoph Hellwig *nsegs = 1; 62885fa13fSChristoph Hellwig 63885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 64885fa13fSChristoph Hellwig return NULL; 65885fa13fSChristoph Hellwig 66885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 67885fa13fSChristoph Hellwig return NULL; 68885fa13fSChristoph Hellwig 69885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 70885fa13fSChristoph Hellwig } 71885fa13fSChristoph Hellwig 7254efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 7354efd50bSKent Overstreet struct bio *bio, 74bdced438SMing Lei struct bio_set *bs, 75bdced438SMing Lei unsigned *nsegs) 7654efd50bSKent Overstreet { 77bdced438SMing Lei *nsegs = 1; 78bdced438SMing Lei 7954efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 8054efd50bSKent Overstreet return NULL; 8154efd50bSKent Overstreet 8254efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 8354efd50bSKent Overstreet return NULL; 8454efd50bSKent Overstreet 8554efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 8654efd50bSKent Overstreet } 8754efd50bSKent Overstreet 88d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 89d0e5fbb0SMing Lei struct bio *bio) 90d0e5fbb0SMing Lei { 91d0e5fbb0SMing Lei unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector); 92d0e5fbb0SMing Lei unsigned mask = queue_logical_block_size(q) - 1; 93d0e5fbb0SMing Lei 94d0e5fbb0SMing Lei /* aligned to logical block size */ 95d0e5fbb0SMing Lei sectors &= ~(mask >> 9); 96d0e5fbb0SMing Lei 97d0e5fbb0SMing Lei return sectors; 98d0e5fbb0SMing Lei } 99d0e5fbb0SMing Lei 10054efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 10154efd50bSKent Overstreet struct bio *bio, 102bdced438SMing Lei struct bio_set *bs, 103bdced438SMing Lei unsigned *segs) 10454efd50bSKent Overstreet { 1055014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 10654efd50bSKent Overstreet struct bvec_iter iter; 1078ae12666SKent Overstreet unsigned seg_size = 0, nsegs = 0, sectors = 0; 10802e70742SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 10902e70742SMing Lei bool do_split = true; 11002e70742SMing Lei struct bio *new = NULL; 111d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 11254efd50bSKent Overstreet 11354efd50bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 11454efd50bSKent Overstreet /* 11554efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 11654efd50bSKent Overstreet * offset would create a gap, disallow it. 11754efd50bSKent Overstreet */ 1185014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 11954efd50bSKent Overstreet goto split; 12054efd50bSKent Overstreet 121d0e5fbb0SMing Lei if (sectors + (bv.bv_len >> 9) > max_sectors) { 122e36f6204SKeith Busch /* 123e36f6204SKeith Busch * Consider this a new segment if we're splitting in 124e36f6204SKeith Busch * the middle of this vector. 125e36f6204SKeith Busch */ 126e36f6204SKeith Busch if (nsegs < queue_max_segments(q) && 127d0e5fbb0SMing Lei sectors < max_sectors) { 128e36f6204SKeith Busch nsegs++; 129d0e5fbb0SMing Lei sectors = max_sectors; 130e36f6204SKeith Busch } 131e36f6204SKeith Busch goto split; 132e36f6204SKeith Busch } 133e36f6204SKeith Busch 1345014c311SJens Axboe if (bvprvp && blk_queue_cluster(q)) { 135*b4b6cb61SMing Lei if (seg_size + bv.bv_len > queue_max_segment_size(q)) 136*b4b6cb61SMing Lei goto new_segment; 1375014c311SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv)) 13854efd50bSKent Overstreet goto new_segment; 1395014c311SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv)) 14054efd50bSKent Overstreet goto new_segment; 14154efd50bSKent Overstreet 14254efd50bSKent Overstreet seg_size += bv.bv_len; 14354efd50bSKent Overstreet bvprv = bv; 144578270bfSMing Lei bvprvp = &bvprv; 14552cc6eeaSMing Lei sectors += bv.bv_len >> 9; 146a88d32afSMing Lei 14754efd50bSKent Overstreet continue; 14854efd50bSKent Overstreet } 14954efd50bSKent Overstreet new_segment: 15054efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 15154efd50bSKent Overstreet goto split; 15254efd50bSKent Overstreet 1536a501bf0SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 1546a501bf0SMing Lei front_seg_size = seg_size; 1556a501bf0SMing Lei 15654efd50bSKent Overstreet nsegs++; 15754efd50bSKent Overstreet bvprv = bv; 158578270bfSMing Lei bvprvp = &bvprv; 15954efd50bSKent Overstreet seg_size = bv.bv_len; 16052cc6eeaSMing Lei sectors += bv.bv_len >> 9; 16102e70742SMing Lei 16254efd50bSKent Overstreet } 16354efd50bSKent Overstreet 16402e70742SMing Lei do_split = false; 16554efd50bSKent Overstreet split: 166bdced438SMing Lei *segs = nsegs; 16702e70742SMing Lei 16802e70742SMing Lei if (do_split) { 16902e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 17002e70742SMing Lei if (new) 17102e70742SMing Lei bio = new; 17202e70742SMing Lei } 17302e70742SMing Lei 1746a501bf0SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 1756a501bf0SMing Lei front_seg_size = seg_size; 17602e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 17702e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 17802e70742SMing Lei bio->bi_seg_back_size = seg_size; 17902e70742SMing Lei 18002e70742SMing Lei return do_split ? new : NULL; 18154efd50bSKent Overstreet } 18254efd50bSKent Overstreet 183af67c31fSNeilBrown void blk_queue_split(struct request_queue *q, struct bio **bio) 18454efd50bSKent Overstreet { 185bdced438SMing Lei struct bio *split, *res; 186bdced438SMing Lei unsigned nsegs; 18754efd50bSKent Overstreet 1887afafc8aSAdrian Hunter switch (bio_op(*bio)) { 1897afafc8aSAdrian Hunter case REQ_OP_DISCARD: 1907afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 191af67c31fSNeilBrown split = blk_bio_discard_split(q, *bio, q->bio_split, &nsegs); 1927afafc8aSAdrian Hunter break; 193a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 194af67c31fSNeilBrown split = blk_bio_write_zeroes_split(q, *bio, q->bio_split, &nsegs); 195a6f0788eSChaitanya Kulkarni break; 1967afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 197af67c31fSNeilBrown split = blk_bio_write_same_split(q, *bio, q->bio_split, &nsegs); 1987afafc8aSAdrian Hunter break; 1997afafc8aSAdrian Hunter default: 200bdced438SMing Lei split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs); 2017afafc8aSAdrian Hunter break; 2027afafc8aSAdrian Hunter } 203bdced438SMing Lei 204bdced438SMing Lei /* physical segments can be figured out during splitting */ 205bdced438SMing Lei res = split ? split : *bio; 206bdced438SMing Lei res->bi_phys_segments = nsegs; 207bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 20854efd50bSKent Overstreet 20954efd50bSKent Overstreet if (split) { 2106ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 2111eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 2126ac45aebSMing Lei 21354efd50bSKent Overstreet bio_chain(split, *bio); 214cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 21554efd50bSKent Overstreet generic_make_request(*bio); 21654efd50bSKent Overstreet *bio = split; 21754efd50bSKent Overstreet } 21854efd50bSKent Overstreet } 21954efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 22054efd50bSKent Overstreet 2211e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 22207388549SMing Lei struct bio *bio, 22307388549SMing Lei bool no_sg_merge) 224d6d48196SJens Axboe { 2257988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 22654efd50bSKent Overstreet int cluster, prev = 0; 2271e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 22859247eaeSJens Axboe struct bio *fbio, *bbio; 2297988613bSKent Overstreet struct bvec_iter iter; 230d6d48196SJens Axboe 2311e428079SJens Axboe if (!bio) 2321e428079SJens Axboe return 0; 233d6d48196SJens Axboe 234a6f0788eSChaitanya Kulkarni switch (bio_op(bio)) { 235a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 236a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 237a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 238f9d03f96SChristoph Hellwig return 0; 239f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 2405cb8850cSKent Overstreet return 1; 241a6f0788eSChaitanya Kulkarni } 2425cb8850cSKent Overstreet 2431e428079SJens Axboe fbio = bio; 244e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 2455df97b91SMikulas Patocka seg_size = 0; 2462c8919deSAndi Kleen nr_phys_segs = 0; 2471e428079SJens Axboe for_each_bio(bio) { 2487988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 249d6d48196SJens Axboe /* 25005f1dd53SJens Axboe * If SG merging is disabled, each bio vector is 25105f1dd53SJens Axboe * a segment 25205f1dd53SJens Axboe */ 25305f1dd53SJens Axboe if (no_sg_merge) 25405f1dd53SJens Axboe goto new_segment; 25505f1dd53SJens Axboe 25654efd50bSKent Overstreet if (prev && cluster) { 2577988613bSKent Overstreet if (seg_size + bv.bv_len 258ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 259d6d48196SJens Axboe goto new_segment; 2607988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) 261d6d48196SJens Axboe goto new_segment; 2627988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) 263d6d48196SJens Axboe goto new_segment; 264d6d48196SJens Axboe 2657988613bSKent Overstreet seg_size += bv.bv_len; 266d6d48196SJens Axboe bvprv = bv; 267d6d48196SJens Axboe continue; 268d6d48196SJens Axboe } 269d6d48196SJens Axboe new_segment: 2701e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 2711e428079SJens Axboe fbio->bi_seg_front_size) 2721e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 27386771427SFUJITA Tomonori 274d6d48196SJens Axboe nr_phys_segs++; 275d6d48196SJens Axboe bvprv = bv; 27654efd50bSKent Overstreet prev = 1; 2777988613bSKent Overstreet seg_size = bv.bv_len; 278d6d48196SJens Axboe } 27959247eaeSJens Axboe bbio = bio; 2801e428079SJens Axboe } 281d6d48196SJens Axboe 28259247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 28359247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 28459247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 28559247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 2861e428079SJens Axboe 2871e428079SJens Axboe return nr_phys_segs; 2881e428079SJens Axboe } 2891e428079SJens Axboe 2901e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 2911e428079SJens Axboe { 29207388549SMing Lei bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, 29307388549SMing Lei &rq->q->queue_flags); 29407388549SMing Lei 29507388549SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, 29607388549SMing Lei no_sg_merge); 297d6d48196SJens Axboe } 298d6d48196SJens Axboe 299d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 300d6d48196SJens Axboe { 3017f60dcaaSMing Lei unsigned short seg_cnt; 302764f612cSMing Lei 3037f60dcaaSMing Lei /* estimate segment number by bi_vcnt for non-cloned bio */ 3047f60dcaaSMing Lei if (bio_flagged(bio, BIO_CLONED)) 3057f60dcaaSMing Lei seg_cnt = bio_segments(bio); 3067f60dcaaSMing Lei else 3077f60dcaaSMing Lei seg_cnt = bio->bi_vcnt; 3087f60dcaaSMing Lei 3097f60dcaaSMing Lei if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && 3107f60dcaaSMing Lei (seg_cnt < queue_max_segments(q))) 3117f60dcaaSMing Lei bio->bi_phys_segments = seg_cnt; 31205f1dd53SJens Axboe else { 313d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 3141e428079SJens Axboe 315d6d48196SJens Axboe bio->bi_next = NULL; 3167f60dcaaSMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); 317d6d48196SJens Axboe bio->bi_next = nxt; 31805f1dd53SJens Axboe } 31905f1dd53SJens Axboe 320b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 321d6d48196SJens Axboe } 322d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 323d6d48196SJens Axboe 324d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 325d6d48196SJens Axboe struct bio *nxt) 326d6d48196SJens Axboe { 3272b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 328f619d254SKent Overstreet 329e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 330d6d48196SJens Axboe return 0; 331d6d48196SJens Axboe 33286771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 333ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 334d6d48196SJens Axboe return 0; 335d6d48196SJens Axboe 336e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 337e17fc0a1SDavid Woodhouse return 1; 338e17fc0a1SDavid Woodhouse 339e827091cSMing Lei bio_get_last_bvec(bio, &end_bv); 340e827091cSMing Lei bio_get_first_bvec(nxt, &nxt_bv); 341f619d254SKent Overstreet 342f619d254SKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) 343e17fc0a1SDavid Woodhouse return 0; 344e17fc0a1SDavid Woodhouse 345d6d48196SJens Axboe /* 346e17fc0a1SDavid Woodhouse * bio and nxt are contiguous in memory; check if the queue allows 347d6d48196SJens Axboe * these two to be merged into one 348d6d48196SJens Axboe */ 349f619d254SKent Overstreet if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) 350d6d48196SJens Axboe return 1; 351d6d48196SJens Axboe 352d6d48196SJens Axboe return 0; 353d6d48196SJens Axboe } 354d6d48196SJens Axboe 3557988613bSKent Overstreet static inline void 356963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 3577988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 358963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 359963ab9e5SAsias He { 360963ab9e5SAsias He 361963ab9e5SAsias He int nbytes = bvec->bv_len; 362963ab9e5SAsias He 3637988613bSKent Overstreet if (*sg && *cluster) { 364*b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 365*b4b6cb61SMing Lei goto new_segment; 366*b4b6cb61SMing Lei 3677988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 368963ab9e5SAsias He goto new_segment; 3697988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 370963ab9e5SAsias He goto new_segment; 371963ab9e5SAsias He 372963ab9e5SAsias He (*sg)->length += nbytes; 373963ab9e5SAsias He } else { 374963ab9e5SAsias He new_segment: 375963ab9e5SAsias He if (!*sg) 376963ab9e5SAsias He *sg = sglist; 377963ab9e5SAsias He else { 378963ab9e5SAsias He /* 379963ab9e5SAsias He * If the driver previously mapped a shorter 380963ab9e5SAsias He * list, we could see a termination bit 381963ab9e5SAsias He * prematurely unless it fully inits the sg 382963ab9e5SAsias He * table on each mapping. We KNOW that there 383963ab9e5SAsias He * must be more entries here or the driver 384963ab9e5SAsias He * would be buggy, so force clear the 385963ab9e5SAsias He * termination bit to avoid doing a full 386963ab9e5SAsias He * sg_init_table() in drivers for each command. 387963ab9e5SAsias He */ 388c8164d89SPaolo Bonzini sg_unmark_end(*sg); 389963ab9e5SAsias He *sg = sg_next(*sg); 390963ab9e5SAsias He } 391963ab9e5SAsias He 392963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 393963ab9e5SAsias He (*nsegs)++; 394963ab9e5SAsias He } 3957988613bSKent Overstreet *bvprv = *bvec; 396963ab9e5SAsias He } 397963ab9e5SAsias He 398f9d03f96SChristoph Hellwig static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv, 399f9d03f96SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **sg) 400f9d03f96SChristoph Hellwig { 401f9d03f96SChristoph Hellwig *sg = sglist; 402f9d03f96SChristoph Hellwig sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 403f9d03f96SChristoph Hellwig return 1; 404f9d03f96SChristoph Hellwig } 405f9d03f96SChristoph Hellwig 4065cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4075cb8850cSKent Overstreet struct scatterlist *sglist, 4085cb8850cSKent Overstreet struct scatterlist **sg) 4095cb8850cSKent Overstreet { 4105cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 4115cb8850cSKent Overstreet struct bvec_iter iter; 412f9d03f96SChristoph Hellwig int cluster = blk_queue_cluster(q), nsegs = 0; 4135cb8850cSKent Overstreet 4145cb8850cSKent Overstreet for_each_bio(bio) 4155cb8850cSKent Overstreet bio_for_each_segment(bvec, bio, iter) 4165cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 4175cb8850cSKent Overstreet &nsegs, &cluster); 4185cb8850cSKent Overstreet 4195cb8850cSKent Overstreet return nsegs; 4205cb8850cSKent Overstreet } 4215cb8850cSKent Overstreet 422d6d48196SJens Axboe /* 423d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 424d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 425d6d48196SJens Axboe */ 426d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 427d6d48196SJens Axboe struct scatterlist *sglist) 428d6d48196SJens Axboe { 4295cb8850cSKent Overstreet struct scatterlist *sg = NULL; 4305cb8850cSKent Overstreet int nsegs = 0; 431d6d48196SJens Axboe 432f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 433f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg); 434f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 435f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg); 436f9d03f96SChristoph Hellwig else if (rq->bio) 4375cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 438f18573abSFUJITA Tomonori 439e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 4402e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 4412e46e8b2STejun Heo unsigned int pad_len = 4422e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 443f18573abSFUJITA Tomonori 444f18573abSFUJITA Tomonori sg->length += pad_len; 445f18573abSFUJITA Tomonori rq->extra_len += pad_len; 446f18573abSFUJITA Tomonori } 447f18573abSFUJITA Tomonori 4482fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 449a8ebb056SMike Christie if (op_is_write(req_op(rq))) 450db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 451db0a2e00STejun Heo 452da81ed16SDan Williams sg_unmark_end(sg); 453d6d48196SJens Axboe sg = sg_next(sg); 454d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 455d6d48196SJens Axboe q->dma_drain_size, 456d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 457d6d48196SJens Axboe (PAGE_SIZE - 1)); 458d6d48196SJens Axboe nsegs++; 4597a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 460d6d48196SJens Axboe } 461d6d48196SJens Axboe 462d6d48196SJens Axboe if (sg) 463d6d48196SJens Axboe sg_mark_end(sg); 464d6d48196SJens Axboe 46512e57f59SMing Lei /* 46612e57f59SMing Lei * Something must have been wrong if the figured number of 46712e57f59SMing Lei * segment is bigger than number of req's physical segments 46812e57f59SMing Lei */ 469f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 47012e57f59SMing Lei 471d6d48196SJens Axboe return nsegs; 472d6d48196SJens Axboe } 473d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 474d6d48196SJens Axboe 475d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 476d6d48196SJens Axboe struct request *req, 477d6d48196SJens Axboe struct bio *bio) 478d6d48196SJens Axboe { 479d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 480d6d48196SJens Axboe 48113f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 48213f05c8dSMartin K. Petersen goto no_merge; 48313f05c8dSMartin K. Petersen 4844eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 48513f05c8dSMartin K. Petersen goto no_merge; 486d6d48196SJens Axboe 487d6d48196SJens Axboe /* 488d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 489d6d48196SJens Axboe * counters. 490d6d48196SJens Axboe */ 491d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 492d6d48196SJens Axboe return 1; 49313f05c8dSMartin K. Petersen 49413f05c8dSMartin K. Petersen no_merge: 495e0c72300SRitesh Harjani req_set_nomerge(q, req); 49613f05c8dSMartin K. Petersen return 0; 497d6d48196SJens Axboe } 498d6d48196SJens Axboe 499d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 500d6d48196SJens Axboe struct bio *bio) 501d6d48196SJens Axboe { 5025e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5035e7c4274SJens Axboe return 0; 5047f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5057f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5067f39add3SSagi Grimberg return 0; 507f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 50817007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 509e0c72300SRitesh Harjani req_set_nomerge(q, req); 510d6d48196SJens Axboe return 0; 511d6d48196SJens Axboe } 5122cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 513d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 5142cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 515d6d48196SJens Axboe blk_recount_segments(q, bio); 516d6d48196SJens Axboe 517d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 518d6d48196SJens Axboe } 519d6d48196SJens Axboe 520d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 521d6d48196SJens Axboe struct bio *bio) 522d6d48196SJens Axboe { 5235e7c4274SJens Axboe 5245e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 5255e7c4274SJens Axboe return 0; 5267f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5277f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 5287f39add3SSagi Grimberg return 0; 529f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 53017007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 531e0c72300SRitesh Harjani req_set_nomerge(q, req); 532d6d48196SJens Axboe return 0; 533d6d48196SJens Axboe } 5342cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 535d6d48196SJens Axboe blk_recount_segments(q, bio); 5362cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 537d6d48196SJens Axboe blk_recount_segments(q, req->bio); 538d6d48196SJens Axboe 539d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 540d6d48196SJens Axboe } 541d6d48196SJens Axboe 542e7e24500SJens Axboe /* 543e7e24500SJens Axboe * blk-mq uses req->special to carry normal driver per-request payload, it 544e7e24500SJens Axboe * does not indicate a prepared command that we cannot merge with. 545e7e24500SJens Axboe */ 546e7e24500SJens Axboe static bool req_no_special_merge(struct request *req) 547e7e24500SJens Axboe { 548e7e24500SJens Axboe struct request_queue *q = req->q; 549e7e24500SJens Axboe 550e7e24500SJens Axboe return !q->mq_ops && req->special; 551e7e24500SJens Axboe } 552e7e24500SJens Axboe 553d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 554d6d48196SJens Axboe struct request *next) 555d6d48196SJens Axboe { 556d6d48196SJens Axboe int total_phys_segments; 55786771427SFUJITA Tomonori unsigned int seg_size = 55886771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 559d6d48196SJens Axboe 560d6d48196SJens Axboe /* 561d6d48196SJens Axboe * First check if the either of the requests are re-queued 562d6d48196SJens Axboe * requests. Can't merge them if they are. 563d6d48196SJens Axboe */ 564e7e24500SJens Axboe if (req_no_special_merge(req) || req_no_special_merge(next)) 565d6d48196SJens Axboe return 0; 566d6d48196SJens Axboe 5675e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 568854fbb9cSKeith Busch return 0; 569854fbb9cSKeith Busch 570d6d48196SJens Axboe /* 571d6d48196SJens Axboe * Will it become too large? 572d6d48196SJens Axboe */ 573f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 57417007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 575d6d48196SJens Axboe return 0; 576d6d48196SJens Axboe 577d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 57886771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 57986771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 58086771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 58186771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 58286771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 583d6d48196SJens Axboe total_phys_segments--; 58486771427SFUJITA Tomonori } 585d6d48196SJens Axboe 5868a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 587d6d48196SJens Axboe return 0; 588d6d48196SJens Axboe 5894eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 59013f05c8dSMartin K. Petersen return 0; 59113f05c8dSMartin K. Petersen 592d6d48196SJens Axboe /* Merge is OK... */ 593d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 594d6d48196SJens Axboe return 1; 595d6d48196SJens Axboe } 596d6d48196SJens Axboe 59780a761fdSTejun Heo /** 59880a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 59980a761fdSTejun Heo * @rq: request to mark as mixed merge 60080a761fdSTejun Heo * 60180a761fdSTejun Heo * Description: 60280a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 60380a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 60480a761fdSTejun Heo * merged. 60580a761fdSTejun Heo */ 60680a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 60780a761fdSTejun Heo { 60880a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 60980a761fdSTejun Heo struct bio *bio; 61080a761fdSTejun Heo 611e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 61280a761fdSTejun Heo return; 61380a761fdSTejun Heo 61480a761fdSTejun Heo /* 61580a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 61680a761fdSTejun Heo * contained bios. It will just track those of the first one. 61780a761fdSTejun Heo * Distributes the attributs to each bio. 61880a761fdSTejun Heo */ 61980a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 6201eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 6211eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 6221eff9d32SJens Axboe bio->bi_opf |= ff; 62380a761fdSTejun Heo } 624e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 62580a761fdSTejun Heo } 62680a761fdSTejun Heo 62726308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 62826308eabSJerome Marchand { 62926308eabSJerome Marchand if (blk_do_io_stat(req)) { 63026308eabSJerome Marchand struct hd_struct *part; 63126308eabSJerome Marchand int cpu; 63226308eabSJerome Marchand 63326308eabSJerome Marchand cpu = part_stat_lock(); 63409e099d4SJerome Marchand part = req->part; 63526308eabSJerome Marchand 636d62e26b3SJens Axboe part_round_stats(req->q, cpu, part); 637d62e26b3SJens Axboe part_dec_in_flight(req->q, part, rq_data_dir(req)); 63826308eabSJerome Marchand 6396c23a968SJens Axboe hd_struct_put(part); 64026308eabSJerome Marchand part_stat_unlock(); 64126308eabSJerome Marchand } 64226308eabSJerome Marchand } 64326308eabSJerome Marchand 644d6d48196SJens Axboe /* 645b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 646b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 647d6d48196SJens Axboe */ 648b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 649b973cb7eSJens Axboe struct request *req, struct request *next) 650d6d48196SJens Axboe { 6512fff8a92SBart Van Assche if (!q->mq_ops) 6522fff8a92SBart Van Assche lockdep_assert_held(q->queue_lock); 6532fff8a92SBart Van Assche 654d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 655b973cb7eSJens Axboe return NULL; 656d6d48196SJens Axboe 657288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 658b973cb7eSJens Axboe return NULL; 659f31dc1cdSMartin K. Petersen 660d6d48196SJens Axboe /* 661d6d48196SJens Axboe * not contiguous 662d6d48196SJens Axboe */ 66383096ebfSTejun Heo if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) 664b973cb7eSJens Axboe return NULL; 665d6d48196SJens Axboe 666d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 667d6d48196SJens Axboe || req->rq_disk != next->rq_disk 668e7e24500SJens Axboe || req_no_special_merge(next)) 669b973cb7eSJens Axboe return NULL; 670d6d48196SJens Axboe 6718fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 6724363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 673b973cb7eSJens Axboe return NULL; 6744363ac7cSMartin K. Petersen 675d6d48196SJens Axboe /* 676cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 677cb6934f8SJens Axboe * non-hint IO. 678cb6934f8SJens Axboe */ 679cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 680cb6934f8SJens Axboe return NULL; 681cb6934f8SJens Axboe 682cb6934f8SJens 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 */ 78974d46992SChristoph Hellwig if (rq->rq_disk != bio->bi_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 801cb6934f8SJens Axboe /* 802cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 803cb6934f8SJens Axboe * non-hint IO. 804cb6934f8SJens Axboe */ 805cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 806cb6934f8SJens Axboe return false; 807cb6934f8SJens Axboe 808050c8ea8STejun Heo return true; 809050c8ea8STejun Heo } 810050c8ea8STejun Heo 81134fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 812050c8ea8STejun Heo { 8131e739730SChristoph Hellwig if (req_op(rq) == REQ_OP_DISCARD && 8141e739730SChristoph Hellwig queue_max_discard_segments(rq->q) > 1) 8151e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 8161e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 817050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 8184f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 819050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 820050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 821050c8ea8STejun Heo } 822