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 10d6d48196SJens Axboe #include "blk.h" 11d6d48196SJens Axboe 12d6d48196SJens Axboe void blk_recalc_rq_sectors(struct request *rq, int nsect) 13d6d48196SJens Axboe { 14d6d48196SJens Axboe if (blk_fs_request(rq)) { 15d6d48196SJens Axboe rq->hard_sector += nsect; 16d6d48196SJens Axboe rq->hard_nr_sectors -= nsect; 17d6d48196SJens Axboe 18d6d48196SJens Axboe /* 19d6d48196SJens Axboe * Move the I/O submission pointers ahead if required. 20d6d48196SJens Axboe */ 21d6d48196SJens Axboe if ((rq->nr_sectors >= rq->hard_nr_sectors) && 22d6d48196SJens Axboe (rq->sector <= rq->hard_sector)) { 23d6d48196SJens Axboe rq->sector = rq->hard_sector; 24d6d48196SJens Axboe rq->nr_sectors = rq->hard_nr_sectors; 25d6d48196SJens Axboe rq->hard_cur_sectors = bio_cur_sectors(rq->bio); 26d6d48196SJens Axboe rq->current_nr_sectors = rq->hard_cur_sectors; 27d6d48196SJens Axboe rq->buffer = bio_data(rq->bio); 28d6d48196SJens Axboe } 29d6d48196SJens Axboe 30d6d48196SJens Axboe /* 31d6d48196SJens Axboe * if total number of sectors is less than the first segment 32d6d48196SJens Axboe * size, something has gone terribly wrong 33d6d48196SJens Axboe */ 34d6d48196SJens Axboe if (rq->nr_sectors < rq->current_nr_sectors) { 356728cb0eSJens Axboe printk(KERN_ERR "blk: request botched\n"); 36d6d48196SJens Axboe rq->nr_sectors = rq->current_nr_sectors; 37d6d48196SJens Axboe } 38d6d48196SJens Axboe } 39d6d48196SJens Axboe } 40d6d48196SJens Axboe 41d6d48196SJens Axboe void blk_recalc_rq_segments(struct request *rq) 42d6d48196SJens Axboe { 43d6d48196SJens Axboe int nr_phys_segs; 44d6d48196SJens Axboe int nr_hw_segs; 45d6d48196SJens Axboe unsigned int phys_size; 46d6d48196SJens Axboe unsigned int hw_size; 47d6d48196SJens Axboe struct bio_vec *bv, *bvprv = NULL; 48d6d48196SJens Axboe int seg_size; 49d6d48196SJens Axboe int hw_seg_size; 50d6d48196SJens Axboe int cluster; 51d6d48196SJens Axboe struct req_iterator iter; 52d6d48196SJens Axboe int high, highprv = 1; 53d6d48196SJens Axboe struct request_queue *q = rq->q; 54d6d48196SJens Axboe 55d6d48196SJens Axboe if (!rq->bio) 56d6d48196SJens Axboe return; 57d6d48196SJens Axboe 5875ad23bcSNick Piggin cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags); 59d6d48196SJens Axboe hw_seg_size = seg_size = 0; 60d6d48196SJens Axboe phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0; 61d6d48196SJens Axboe rq_for_each_segment(bv, rq, iter) { 62d6d48196SJens Axboe /* 63d6d48196SJens Axboe * the trick here is making sure that a high page is never 64d6d48196SJens Axboe * considered part of another segment, since that might 65d6d48196SJens Axboe * change with the bounce page. 66d6d48196SJens Axboe */ 67d6d48196SJens Axboe high = page_to_pfn(bv->bv_page) > q->bounce_pfn; 68d6d48196SJens Axboe if (high || highprv) 69d6d48196SJens Axboe goto new_hw_segment; 70d6d48196SJens Axboe if (cluster) { 71d6d48196SJens Axboe if (seg_size + bv->bv_len > q->max_segment_size) 72d6d48196SJens Axboe goto new_segment; 73d6d48196SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv)) 74d6d48196SJens Axboe goto new_segment; 75d6d48196SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv)) 76d6d48196SJens Axboe goto new_segment; 77d6d48196SJens Axboe if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) 78d6d48196SJens Axboe goto new_hw_segment; 79d6d48196SJens Axboe 80d6d48196SJens Axboe seg_size += bv->bv_len; 81d6d48196SJens Axboe hw_seg_size += bv->bv_len; 82d6d48196SJens Axboe bvprv = bv; 83d6d48196SJens Axboe continue; 84d6d48196SJens Axboe } 85d6d48196SJens Axboe new_segment: 86d6d48196SJens Axboe if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) && 87d6d48196SJens Axboe !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) 88d6d48196SJens Axboe hw_seg_size += bv->bv_len; 89d6d48196SJens Axboe else { 90d6d48196SJens Axboe new_hw_segment: 91d6d48196SJens Axboe if (nr_hw_segs == 1 && 92d6d48196SJens Axboe hw_seg_size > rq->bio->bi_hw_front_size) 93d6d48196SJens Axboe rq->bio->bi_hw_front_size = hw_seg_size; 94d6d48196SJens Axboe hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len; 95d6d48196SJens Axboe nr_hw_segs++; 96d6d48196SJens Axboe } 97d6d48196SJens Axboe 98d6d48196SJens Axboe nr_phys_segs++; 99d6d48196SJens Axboe bvprv = bv; 100d6d48196SJens Axboe seg_size = bv->bv_len; 101d6d48196SJens Axboe highprv = high; 102d6d48196SJens Axboe } 103d6d48196SJens Axboe 104d6d48196SJens Axboe if (nr_hw_segs == 1 && 105d6d48196SJens Axboe hw_seg_size > rq->bio->bi_hw_front_size) 106d6d48196SJens Axboe rq->bio->bi_hw_front_size = hw_seg_size; 107d6d48196SJens Axboe if (hw_seg_size > rq->biotail->bi_hw_back_size) 108d6d48196SJens Axboe rq->biotail->bi_hw_back_size = hw_seg_size; 109d6d48196SJens Axboe rq->nr_phys_segments = nr_phys_segs; 110d6d48196SJens Axboe rq->nr_hw_segments = nr_hw_segs; 111d6d48196SJens Axboe } 112d6d48196SJens Axboe 113d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 114d6d48196SJens Axboe { 115d6d48196SJens Axboe struct request rq; 116d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 117d6d48196SJens Axboe rq.q = q; 118d6d48196SJens Axboe rq.bio = rq.biotail = bio; 119d6d48196SJens Axboe bio->bi_next = NULL; 120d6d48196SJens Axboe blk_recalc_rq_segments(&rq); 121d6d48196SJens Axboe bio->bi_next = nxt; 122d6d48196SJens Axboe bio->bi_phys_segments = rq.nr_phys_segments; 123d6d48196SJens Axboe bio->bi_hw_segments = rq.nr_hw_segments; 124d6d48196SJens Axboe bio->bi_flags |= (1 << BIO_SEG_VALID); 125d6d48196SJens Axboe } 126d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 127d6d48196SJens Axboe 128d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 129d6d48196SJens Axboe struct bio *nxt) 130d6d48196SJens Axboe { 13175ad23bcSNick Piggin if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags)) 132d6d48196SJens Axboe return 0; 133d6d48196SJens Axboe 134d6d48196SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt))) 135d6d48196SJens Axboe return 0; 136d6d48196SJens Axboe if (bio->bi_size + nxt->bi_size > q->max_segment_size) 137d6d48196SJens Axboe return 0; 138d6d48196SJens Axboe 139d6d48196SJens Axboe /* 140d6d48196SJens Axboe * bio and nxt are contigous in memory, check if the queue allows 141d6d48196SJens Axboe * these two to be merged into one 142d6d48196SJens Axboe */ 143d6d48196SJens Axboe if (BIO_SEG_BOUNDARY(q, bio, nxt)) 144d6d48196SJens Axboe return 1; 145d6d48196SJens Axboe 146d6d48196SJens Axboe return 0; 147d6d48196SJens Axboe } 148d6d48196SJens Axboe 149d6d48196SJens Axboe static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio, 150d6d48196SJens Axboe struct bio *nxt) 151d6d48196SJens Axboe { 152*2cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 153d6d48196SJens Axboe blk_recount_segments(q, bio); 154*2cdf79caSJens Axboe if (!bio_flagged(nxt, BIO_SEG_VALID)) 155d6d48196SJens Axboe blk_recount_segments(q, nxt); 156d6d48196SJens Axboe if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) || 157d6d48196SJens Axboe BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size)) 158d6d48196SJens Axboe return 0; 159d6d48196SJens Axboe if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size) 160d6d48196SJens Axboe return 0; 161d6d48196SJens Axboe 162d6d48196SJens Axboe return 1; 163d6d48196SJens Axboe } 164d6d48196SJens Axboe 165d6d48196SJens Axboe /* 166d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 167d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 168d6d48196SJens Axboe */ 169d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 170d6d48196SJens Axboe struct scatterlist *sglist) 171d6d48196SJens Axboe { 172d6d48196SJens Axboe struct bio_vec *bvec, *bvprv; 173d6d48196SJens Axboe struct req_iterator iter; 174d6d48196SJens Axboe struct scatterlist *sg; 175d6d48196SJens Axboe int nsegs, cluster; 176d6d48196SJens Axboe 177d6d48196SJens Axboe nsegs = 0; 17875ad23bcSNick Piggin cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags); 179d6d48196SJens Axboe 180d6d48196SJens Axboe /* 181d6d48196SJens Axboe * for each bio in rq 182d6d48196SJens Axboe */ 183d6d48196SJens Axboe bvprv = NULL; 184d6d48196SJens Axboe sg = NULL; 185d6d48196SJens Axboe rq_for_each_segment(bvec, rq, iter) { 186d6d48196SJens Axboe int nbytes = bvec->bv_len; 187d6d48196SJens Axboe 188d6d48196SJens Axboe if (bvprv && cluster) { 189d6d48196SJens Axboe if (sg->length + nbytes > q->max_segment_size) 190d6d48196SJens Axboe goto new_segment; 191d6d48196SJens Axboe 192d6d48196SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 193d6d48196SJens Axboe goto new_segment; 194d6d48196SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 195d6d48196SJens Axboe goto new_segment; 196d6d48196SJens Axboe 197d6d48196SJens Axboe sg->length += nbytes; 198d6d48196SJens Axboe } else { 199d6d48196SJens Axboe new_segment: 200d6d48196SJens Axboe if (!sg) 201d6d48196SJens Axboe sg = sglist; 202d6d48196SJens Axboe else { 203d6d48196SJens Axboe /* 204d6d48196SJens Axboe * If the driver previously mapped a shorter 205d6d48196SJens Axboe * list, we could see a termination bit 206d6d48196SJens Axboe * prematurely unless it fully inits the sg 207d6d48196SJens Axboe * table on each mapping. We KNOW that there 208d6d48196SJens Axboe * must be more entries here or the driver 209d6d48196SJens Axboe * would be buggy, so force clear the 210d6d48196SJens Axboe * termination bit to avoid doing a full 211d6d48196SJens Axboe * sg_init_table() in drivers for each command. 212d6d48196SJens Axboe */ 213d6d48196SJens Axboe sg->page_link &= ~0x02; 214d6d48196SJens Axboe sg = sg_next(sg); 215d6d48196SJens Axboe } 216d6d48196SJens Axboe 217d6d48196SJens Axboe sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset); 218d6d48196SJens Axboe nsegs++; 219d6d48196SJens Axboe } 220d6d48196SJens Axboe bvprv = bvec; 221d6d48196SJens Axboe } /* segments in rq */ 222d6d48196SJens Axboe 223f18573abSFUJITA Tomonori 224f18573abSFUJITA Tomonori if (unlikely(rq->cmd_flags & REQ_COPY_USER) && 225f18573abSFUJITA Tomonori (rq->data_len & q->dma_pad_mask)) { 226f18573abSFUJITA Tomonori unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1; 227f18573abSFUJITA Tomonori 228f18573abSFUJITA Tomonori sg->length += pad_len; 229f18573abSFUJITA Tomonori rq->extra_len += pad_len; 230f18573abSFUJITA Tomonori } 231f18573abSFUJITA Tomonori 2322fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 233db0a2e00STejun Heo if (rq->cmd_flags & REQ_RW) 234db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 235db0a2e00STejun Heo 236d6d48196SJens Axboe sg->page_link &= ~0x02; 237d6d48196SJens Axboe sg = sg_next(sg); 238d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 239d6d48196SJens Axboe q->dma_drain_size, 240d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 241d6d48196SJens Axboe (PAGE_SIZE - 1)); 242d6d48196SJens Axboe nsegs++; 2437a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 244d6d48196SJens Axboe } 245d6d48196SJens Axboe 246d6d48196SJens Axboe if (sg) 247d6d48196SJens Axboe sg_mark_end(sg); 248d6d48196SJens Axboe 249d6d48196SJens Axboe return nsegs; 250d6d48196SJens Axboe } 251d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 252d6d48196SJens Axboe 253d6d48196SJens Axboe static inline int ll_new_mergeable(struct request_queue *q, 254d6d48196SJens Axboe struct request *req, 255d6d48196SJens Axboe struct bio *bio) 256d6d48196SJens Axboe { 257d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 258d6d48196SJens Axboe 259d6d48196SJens Axboe if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { 260d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 261d6d48196SJens Axboe if (req == q->last_merge) 262d6d48196SJens Axboe q->last_merge = NULL; 263d6d48196SJens Axboe return 0; 264d6d48196SJens Axboe } 265d6d48196SJens Axboe 266d6d48196SJens Axboe /* 267d6d48196SJens Axboe * A hw segment is just getting larger, bump just the phys 268d6d48196SJens Axboe * counter. 269d6d48196SJens Axboe */ 270d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 271d6d48196SJens Axboe return 1; 272d6d48196SJens Axboe } 273d6d48196SJens Axboe 274d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 275d6d48196SJens Axboe struct request *req, 276d6d48196SJens Axboe struct bio *bio) 277d6d48196SJens Axboe { 278d6d48196SJens Axboe int nr_hw_segs = bio_hw_segments(q, bio); 279d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 280d6d48196SJens Axboe 281d6d48196SJens Axboe if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments 282d6d48196SJens Axboe || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { 283d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 284d6d48196SJens Axboe if (req == q->last_merge) 285d6d48196SJens Axboe q->last_merge = NULL; 286d6d48196SJens Axboe return 0; 287d6d48196SJens Axboe } 288d6d48196SJens Axboe 289d6d48196SJens Axboe /* 290d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 291d6d48196SJens Axboe * counters. 292d6d48196SJens Axboe */ 293d6d48196SJens Axboe req->nr_hw_segments += nr_hw_segs; 294d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 295d6d48196SJens Axboe return 1; 296d6d48196SJens Axboe } 297d6d48196SJens Axboe 298d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 299d6d48196SJens Axboe struct bio *bio) 300d6d48196SJens Axboe { 301d6d48196SJens Axboe unsigned short max_sectors; 302d6d48196SJens Axboe int len; 303d6d48196SJens Axboe 304d6d48196SJens Axboe if (unlikely(blk_pc_request(req))) 305d6d48196SJens Axboe max_sectors = q->max_hw_sectors; 306d6d48196SJens Axboe else 307d6d48196SJens Axboe max_sectors = q->max_sectors; 308d6d48196SJens Axboe 309d6d48196SJens Axboe if (req->nr_sectors + bio_sectors(bio) > max_sectors) { 310d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 311d6d48196SJens Axboe if (req == q->last_merge) 312d6d48196SJens Axboe q->last_merge = NULL; 313d6d48196SJens Axboe return 0; 314d6d48196SJens Axboe } 315*2cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 316d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 317*2cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 318d6d48196SJens Axboe blk_recount_segments(q, bio); 319d6d48196SJens Axboe len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size; 3206728cb0eSJens Axboe if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) 3216728cb0eSJens Axboe && !BIOVEC_VIRT_OVERSIZE(len)) { 322d6d48196SJens Axboe int mergeable = ll_new_mergeable(q, req, bio); 323d6d48196SJens Axboe 324d6d48196SJens Axboe if (mergeable) { 325d6d48196SJens Axboe if (req->nr_hw_segments == 1) 326d6d48196SJens Axboe req->bio->bi_hw_front_size = len; 327d6d48196SJens Axboe if (bio->bi_hw_segments == 1) 328d6d48196SJens Axboe bio->bi_hw_back_size = len; 329d6d48196SJens Axboe } 330d6d48196SJens Axboe return mergeable; 331d6d48196SJens Axboe } 332d6d48196SJens Axboe 333d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 334d6d48196SJens Axboe } 335d6d48196SJens Axboe 336d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 337d6d48196SJens Axboe struct bio *bio) 338d6d48196SJens Axboe { 339d6d48196SJens Axboe unsigned short max_sectors; 340d6d48196SJens Axboe int len; 341d6d48196SJens Axboe 342d6d48196SJens Axboe if (unlikely(blk_pc_request(req))) 343d6d48196SJens Axboe max_sectors = q->max_hw_sectors; 344d6d48196SJens Axboe else 345d6d48196SJens Axboe max_sectors = q->max_sectors; 346d6d48196SJens Axboe 347d6d48196SJens Axboe 348d6d48196SJens Axboe if (req->nr_sectors + bio_sectors(bio) > max_sectors) { 349d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 350d6d48196SJens Axboe if (req == q->last_merge) 351d6d48196SJens Axboe q->last_merge = NULL; 352d6d48196SJens Axboe return 0; 353d6d48196SJens Axboe } 354d6d48196SJens Axboe len = bio->bi_hw_back_size + req->bio->bi_hw_front_size; 355*2cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 356d6d48196SJens Axboe blk_recount_segments(q, bio); 357*2cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 358d6d48196SJens Axboe blk_recount_segments(q, req->bio); 359d6d48196SJens Axboe if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) && 360d6d48196SJens Axboe !BIOVEC_VIRT_OVERSIZE(len)) { 361d6d48196SJens Axboe int mergeable = ll_new_mergeable(q, req, bio); 362d6d48196SJens Axboe 363d6d48196SJens Axboe if (mergeable) { 364d6d48196SJens Axboe if (bio->bi_hw_segments == 1) 365d6d48196SJens Axboe bio->bi_hw_front_size = len; 366d6d48196SJens Axboe if (req->nr_hw_segments == 1) 367d6d48196SJens Axboe req->biotail->bi_hw_back_size = len; 368d6d48196SJens Axboe } 369d6d48196SJens Axboe return mergeable; 370d6d48196SJens Axboe } 371d6d48196SJens Axboe 372d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 373d6d48196SJens Axboe } 374d6d48196SJens Axboe 375d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 376d6d48196SJens Axboe struct request *next) 377d6d48196SJens Axboe { 378d6d48196SJens Axboe int total_phys_segments; 379d6d48196SJens Axboe int total_hw_segments; 380d6d48196SJens Axboe 381d6d48196SJens Axboe /* 382d6d48196SJens Axboe * First check if the either of the requests are re-queued 383d6d48196SJens Axboe * requests. Can't merge them if they are. 384d6d48196SJens Axboe */ 385d6d48196SJens Axboe if (req->special || next->special) 386d6d48196SJens Axboe return 0; 387d6d48196SJens Axboe 388d6d48196SJens Axboe /* 389d6d48196SJens Axboe * Will it become too large? 390d6d48196SJens Axboe */ 391d6d48196SJens Axboe if ((req->nr_sectors + next->nr_sectors) > q->max_sectors) 392d6d48196SJens Axboe return 0; 393d6d48196SJens Axboe 394d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 395d6d48196SJens Axboe if (blk_phys_contig_segment(q, req->biotail, next->bio)) 396d6d48196SJens Axboe total_phys_segments--; 397d6d48196SJens Axboe 398d6d48196SJens Axboe if (total_phys_segments > q->max_phys_segments) 399d6d48196SJens Axboe return 0; 400d6d48196SJens Axboe 401d6d48196SJens Axboe total_hw_segments = req->nr_hw_segments + next->nr_hw_segments; 402d6d48196SJens Axboe if (blk_hw_contig_segment(q, req->biotail, next->bio)) { 4036728cb0eSJens Axboe int len = req->biotail->bi_hw_back_size + 4046728cb0eSJens Axboe next->bio->bi_hw_front_size; 405d6d48196SJens Axboe /* 406d6d48196SJens Axboe * propagate the combined length to the end of the requests 407d6d48196SJens Axboe */ 408d6d48196SJens Axboe if (req->nr_hw_segments == 1) 409d6d48196SJens Axboe req->bio->bi_hw_front_size = len; 410d6d48196SJens Axboe if (next->nr_hw_segments == 1) 411d6d48196SJens Axboe next->biotail->bi_hw_back_size = len; 412d6d48196SJens Axboe total_hw_segments--; 413d6d48196SJens Axboe } 414d6d48196SJens Axboe 415d6d48196SJens Axboe if (total_hw_segments > q->max_hw_segments) 416d6d48196SJens Axboe return 0; 417d6d48196SJens Axboe 418d6d48196SJens Axboe /* Merge is OK... */ 419d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 420d6d48196SJens Axboe req->nr_hw_segments = total_hw_segments; 421d6d48196SJens Axboe return 1; 422d6d48196SJens Axboe } 423d6d48196SJens Axboe 424d6d48196SJens Axboe /* 425d6d48196SJens Axboe * Has to be called with the request spinlock acquired 426d6d48196SJens Axboe */ 427d6d48196SJens Axboe static int attempt_merge(struct request_queue *q, struct request *req, 428d6d48196SJens Axboe struct request *next) 429d6d48196SJens Axboe { 430d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 431d6d48196SJens Axboe return 0; 432d6d48196SJens Axboe 433d6d48196SJens Axboe /* 434d6d48196SJens Axboe * not contiguous 435d6d48196SJens Axboe */ 436d6d48196SJens Axboe if (req->sector + req->nr_sectors != next->sector) 437d6d48196SJens Axboe return 0; 438d6d48196SJens Axboe 439d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 440d6d48196SJens Axboe || req->rq_disk != next->rq_disk 441d6d48196SJens Axboe || next->special) 442d6d48196SJens Axboe return 0; 443d6d48196SJens Axboe 444d6d48196SJens Axboe /* 445d6d48196SJens Axboe * If we are allowed to merge, then append bio list 446d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 447d6d48196SJens Axboe * will have updated segment counts, update sector 448d6d48196SJens Axboe * counts here. 449d6d48196SJens Axboe */ 450d6d48196SJens Axboe if (!ll_merge_requests_fn(q, req, next)) 451d6d48196SJens Axboe return 0; 452d6d48196SJens Axboe 453d6d48196SJens Axboe /* 454d6d48196SJens Axboe * At this point we have either done a back merge 455d6d48196SJens Axboe * or front merge. We need the smaller start_time of 456d6d48196SJens Axboe * the merged requests to be the current request 457d6d48196SJens Axboe * for accounting purposes. 458d6d48196SJens Axboe */ 459d6d48196SJens Axboe if (time_after(req->start_time, next->start_time)) 460d6d48196SJens Axboe req->start_time = next->start_time; 461d6d48196SJens Axboe 462d6d48196SJens Axboe req->biotail->bi_next = next->bio; 463d6d48196SJens Axboe req->biotail = next->biotail; 464d6d48196SJens Axboe 465d6d48196SJens Axboe req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors; 466d6d48196SJens Axboe 467d6d48196SJens Axboe elv_merge_requests(q, req, next); 468d6d48196SJens Axboe 469d6d48196SJens Axboe if (req->rq_disk) { 4706f2576afSJerome Marchand struct hd_struct *part 4716f2576afSJerome Marchand = get_part(req->rq_disk, req->sector); 472d6d48196SJens Axboe disk_round_stats(req->rq_disk); 473d6d48196SJens Axboe req->rq_disk->in_flight--; 4746f2576afSJerome Marchand if (part) { 4756f2576afSJerome Marchand part_round_stats(part); 4766f2576afSJerome Marchand part->in_flight--; 4776f2576afSJerome Marchand } 478d6d48196SJens Axboe } 479d6d48196SJens Axboe 480d6d48196SJens Axboe req->ioprio = ioprio_best(req->ioprio, next->ioprio); 481d6d48196SJens Axboe 482d6d48196SJens Axboe __blk_put_request(q, next); 483d6d48196SJens Axboe return 1; 484d6d48196SJens Axboe } 485d6d48196SJens Axboe 486d6d48196SJens Axboe int attempt_back_merge(struct request_queue *q, struct request *rq) 487d6d48196SJens Axboe { 488d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 489d6d48196SJens Axboe 490d6d48196SJens Axboe if (next) 491d6d48196SJens Axboe return attempt_merge(q, rq, next); 492d6d48196SJens Axboe 493d6d48196SJens Axboe return 0; 494d6d48196SJens Axboe } 495d6d48196SJens Axboe 496d6d48196SJens Axboe int attempt_front_merge(struct request_queue *q, struct request *rq) 497d6d48196SJens Axboe { 498d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 499d6d48196SJens Axboe 500d6d48196SJens Axboe if (prev) 501d6d48196SJens Axboe return attempt_merge(q, prev, rq); 502d6d48196SJens Axboe 503d6d48196SJens Axboe return 0; 504d6d48196SJens Axboe } 505