Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions related to segment and merge handling |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/bio.h> |
| 7 | #include <linux/blkdev.h> |
| 8 | #include <linux/scatterlist.h> |
| 9 | |
| 10 | #include "blk.h" |
| 11 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 12 | static unsigned int __blk_recalc_rq_segments(struct request_queue *q, |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 13 | struct bio *bio) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 14 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 15 | struct bio_vec bv, bvprv = { NULL }; |
| 16 | int cluster, high, highprv = 1; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 17 | unsigned int seg_size, nr_phys_segs; |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 18 | struct bio *fbio, *bbio; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 19 | struct bvec_iter iter; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 20 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 21 | if (!bio) |
| 22 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 23 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 24 | fbio = bio; |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 25 | cluster = blk_queue_cluster(q); |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 26 | seg_size = 0; |
Andi Kleen | 2c8919d | 2010-06-21 11:02:47 +0200 | [diff] [blame] | 27 | nr_phys_segs = 0; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 28 | for_each_bio(bio) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 29 | bio_for_each_segment(bv, bio, iter) { |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 30 | /* |
| 31 | * the trick here is making sure that a high page is |
| 32 | * never considered part of another segment, since that |
| 33 | * might change with the bounce page. |
| 34 | */ |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 35 | high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q); |
| 36 | if (!high && !highprv && cluster) { |
| 37 | if (seg_size + bv.bv_len |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 38 | > queue_max_segment_size(q)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 39 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 40 | if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 41 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 42 | if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 43 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 44 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 45 | seg_size += bv.bv_len; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 46 | bvprv = bv; |
| 47 | continue; |
| 48 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 49 | new_segment: |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 50 | if (nr_phys_segs == 1 && seg_size > |
| 51 | fbio->bi_seg_front_size) |
| 52 | fbio->bi_seg_front_size = seg_size; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 53 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 54 | nr_phys_segs++; |
| 55 | bvprv = bv; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 56 | seg_size = bv.bv_len; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 57 | highprv = high; |
| 58 | } |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 59 | bbio = bio; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 62 | if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) |
| 63 | fbio->bi_seg_front_size = seg_size; |
| 64 | if (seg_size > bbio->bi_seg_back_size) |
| 65 | bbio->bi_seg_back_size = seg_size; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 66 | |
| 67 | return nr_phys_segs; |
| 68 | } |
| 69 | |
| 70 | void blk_recalc_rq_segments(struct request *rq) |
| 71 | { |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 72 | rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void blk_recount_segments(struct request_queue *q, struct bio *bio) |
| 76 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 77 | struct bio *nxt = bio->bi_next; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 78 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 79 | bio->bi_next = NULL; |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 80 | bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 81 | bio->bi_next = nxt; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 82 | bio->bi_flags |= (1 << BIO_SEG_VALID); |
| 83 | } |
| 84 | EXPORT_SYMBOL(blk_recount_segments); |
| 85 | |
| 86 | static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, |
| 87 | struct bio *nxt) |
| 88 | { |
Kent Overstreet | 2b8221e | 2013-12-03 14:29:09 -0700 | [diff] [blame] | 89 | struct bio_vec end_bv = { NULL }, nxt_bv; |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 90 | struct bvec_iter iter; |
| 91 | |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 92 | if (!blk_queue_cluster(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 93 | return 0; |
| 94 | |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 95 | if (bio->bi_seg_back_size + nxt->bi_seg_front_size > |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 96 | queue_max_segment_size(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 97 | return 0; |
| 98 | |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 99 | if (!bio_has_data(bio)) |
| 100 | return 1; |
| 101 | |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 102 | bio_for_each_segment(end_bv, bio, iter) |
| 103 | if (end_bv.bv_len == iter.bi_size) |
| 104 | break; |
| 105 | |
| 106 | nxt_bv = bio_iovec(nxt); |
| 107 | |
| 108 | if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 109 | return 0; |
| 110 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 111 | /* |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 112 | * bio and nxt are contiguous in memory; check if the queue allows |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 113 | * these two to be merged into one |
| 114 | */ |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 115 | if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 116 | return 1; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 121 | static inline void |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 122 | __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 123 | struct scatterlist *sglist, struct bio_vec *bvprv, |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 124 | struct scatterlist **sg, int *nsegs, int *cluster) |
| 125 | { |
| 126 | |
| 127 | int nbytes = bvec->bv_len; |
| 128 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 129 | if (*sg && *cluster) { |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 130 | if ((*sg)->length + nbytes > queue_max_segment_size(q)) |
| 131 | goto new_segment; |
| 132 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 133 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 134 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 135 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 136 | goto new_segment; |
| 137 | |
| 138 | (*sg)->length += nbytes; |
| 139 | } else { |
| 140 | new_segment: |
| 141 | if (!*sg) |
| 142 | *sg = sglist; |
| 143 | else { |
| 144 | /* |
| 145 | * If the driver previously mapped a shorter |
| 146 | * list, we could see a termination bit |
| 147 | * prematurely unless it fully inits the sg |
| 148 | * table on each mapping. We KNOW that there |
| 149 | * must be more entries here or the driver |
| 150 | * would be buggy, so force clear the |
| 151 | * termination bit to avoid doing a full |
| 152 | * sg_init_table() in drivers for each command. |
| 153 | */ |
Paolo Bonzini | c8164d8 | 2013-03-20 15:37:08 +1030 | [diff] [blame] | 154 | sg_unmark_end(*sg); |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 155 | *sg = sg_next(*sg); |
| 156 | } |
| 157 | |
| 158 | sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); |
| 159 | (*nsegs)++; |
| 160 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 161 | *bvprv = *bvec; |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 164 | /* |
| 165 | * map a request to scatterlist, return number of sg entries setup. Caller |
| 166 | * must make sure sg can hold rq->nr_phys_segments entries |
| 167 | */ |
| 168 | int blk_rq_map_sg(struct request_queue *q, struct request *rq, |
| 169 | struct scatterlist *sglist) |
| 170 | { |
Kent Overstreet | 2b8221e | 2013-12-03 14:29:09 -0700 | [diff] [blame] | 171 | struct bio_vec bvec, bvprv = { NULL }; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 172 | struct req_iterator iter; |
| 173 | struct scatterlist *sg; |
| 174 | int nsegs, cluster; |
| 175 | |
| 176 | nsegs = 0; |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 177 | cluster = blk_queue_cluster(q); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * for each bio in rq |
| 181 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 182 | sg = NULL; |
| 183 | rq_for_each_segment(bvec, rq, iter) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 184 | __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg, |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 185 | &nsegs, &cluster); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 186 | } /* segments in rq */ |
| 187 | |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 188 | |
| 189 | if (unlikely(rq->cmd_flags & REQ_COPY_USER) && |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 190 | (blk_rq_bytes(rq) & q->dma_pad_mask)) { |
| 191 | unsigned int pad_len = |
| 192 | (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 193 | |
| 194 | sg->length += pad_len; |
| 195 | rq->extra_len += pad_len; |
| 196 | } |
| 197 | |
Tejun Heo | 2fb98e8 | 2008-02-19 11:36:53 +0100 | [diff] [blame] | 198 | if (q->dma_drain_size && q->dma_drain_needed(rq)) { |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 199 | if (rq->cmd_flags & REQ_WRITE) |
Tejun Heo | db0a2e0 | 2008-02-19 11:36:55 +0100 | [diff] [blame] | 200 | memset(q->dma_drain_buffer, 0, q->dma_drain_size); |
| 201 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 202 | sg->page_link &= ~0x02; |
| 203 | sg = sg_next(sg); |
| 204 | sg_set_page(sg, virt_to_page(q->dma_drain_buffer), |
| 205 | q->dma_drain_size, |
| 206 | ((unsigned long)q->dma_drain_buffer) & |
| 207 | (PAGE_SIZE - 1)); |
| 208 | nsegs++; |
FUJITA Tomonori | 7a85f88 | 2008-03-04 11:17:11 +0100 | [diff] [blame] | 209 | rq->extra_len += q->dma_drain_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (sg) |
| 213 | sg_mark_end(sg); |
| 214 | |
| 215 | return nsegs; |
| 216 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 217 | EXPORT_SYMBOL(blk_rq_map_sg); |
| 218 | |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 219 | /** |
| 220 | * blk_bio_map_sg - map a bio to a scatterlist |
| 221 | * @q: request_queue in question |
| 222 | * @bio: bio being mapped |
| 223 | * @sglist: scatterlist being mapped |
| 224 | * |
| 225 | * Note: |
| 226 | * Caller must make sure sg can hold bio->bi_phys_segments entries |
| 227 | * |
| 228 | * Will return the number of sg entries setup |
| 229 | */ |
| 230 | int blk_bio_map_sg(struct request_queue *q, struct bio *bio, |
| 231 | struct scatterlist *sglist) |
| 232 | { |
Kent Overstreet | 2b8221e | 2013-12-03 14:29:09 -0700 | [diff] [blame] | 233 | struct bio_vec bvec, bvprv = { NULL }; |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 234 | struct scatterlist *sg; |
| 235 | int nsegs, cluster; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 236 | struct bvec_iter iter; |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 237 | |
| 238 | nsegs = 0; |
| 239 | cluster = blk_queue_cluster(q); |
| 240 | |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 241 | sg = NULL; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 242 | bio_for_each_segment(bvec, bio, iter) { |
| 243 | __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg, |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 244 | &nsegs, &cluster); |
| 245 | } /* segments in bio */ |
| 246 | |
| 247 | if (sg) |
| 248 | sg_mark_end(sg); |
| 249 | |
| 250 | BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments); |
| 251 | return nsegs; |
| 252 | } |
| 253 | EXPORT_SYMBOL(blk_bio_map_sg); |
| 254 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 255 | static inline int ll_new_hw_segment(struct request_queue *q, |
| 256 | struct request *req, |
| 257 | struct bio *bio) |
| 258 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 259 | int nr_phys_segs = bio_phys_segments(q, bio); |
| 260 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 261 | if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) |
| 262 | goto no_merge; |
| 263 | |
| 264 | if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio)) |
| 265 | goto no_merge; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * This will form the start of a new hw segment. Bump both |
| 269 | * counters. |
| 270 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 271 | req->nr_phys_segments += nr_phys_segs; |
| 272 | return 1; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 273 | |
| 274 | no_merge: |
| 275 | req->cmd_flags |= REQ_NOMERGE; |
| 276 | if (req == q->last_merge) |
| 277 | q->last_merge = NULL; |
| 278 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | int ll_back_merge_fn(struct request_queue *q, struct request *req, |
| 282 | struct bio *bio) |
| 283 | { |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 284 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 285 | blk_rq_get_max_sectors(req)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 286 | req->cmd_flags |= REQ_NOMERGE; |
| 287 | if (req == q->last_merge) |
| 288 | q->last_merge = NULL; |
| 289 | return 0; |
| 290 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 291 | if (!bio_flagged(req->biotail, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 292 | blk_recount_segments(q, req->biotail); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 293 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 294 | blk_recount_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 295 | |
| 296 | return ll_new_hw_segment(q, req, bio); |
| 297 | } |
| 298 | |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 299 | int ll_front_merge_fn(struct request_queue *q, struct request *req, |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 300 | struct bio *bio) |
| 301 | { |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 302 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 303 | blk_rq_get_max_sectors(req)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 304 | req->cmd_flags |= REQ_NOMERGE; |
| 305 | if (req == q->last_merge) |
| 306 | q->last_merge = NULL; |
| 307 | return 0; |
| 308 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 309 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 310 | blk_recount_segments(q, bio); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 311 | if (!bio_flagged(req->bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 312 | blk_recount_segments(q, req->bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 313 | |
| 314 | return ll_new_hw_segment(q, req, bio); |
| 315 | } |
| 316 | |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 317 | /* |
| 318 | * blk-mq uses req->special to carry normal driver per-request payload, it |
| 319 | * does not indicate a prepared command that we cannot merge with. |
| 320 | */ |
| 321 | static bool req_no_special_merge(struct request *req) |
| 322 | { |
| 323 | struct request_queue *q = req->q; |
| 324 | |
| 325 | return !q->mq_ops && req->special; |
| 326 | } |
| 327 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 328 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, |
| 329 | struct request *next) |
| 330 | { |
| 331 | int total_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 332 | unsigned int seg_size = |
| 333 | req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * First check if the either of the requests are re-queued |
| 337 | * requests. Can't merge them if they are. |
| 338 | */ |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 339 | if (req_no_special_merge(req) || req_no_special_merge(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | |
| 342 | /* |
| 343 | * Will it become too large? |
| 344 | */ |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 345 | if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > |
| 346 | blk_rq_get_max_sectors(req)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | |
| 349 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 350 | if (blk_phys_contig_segment(q, req->biotail, next->bio)) { |
| 351 | if (req->nr_phys_segments == 1) |
| 352 | req->bio->bi_seg_front_size = seg_size; |
| 353 | if (next->nr_phys_segments == 1) |
| 354 | next->biotail->bi_seg_back_size = seg_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 355 | total_phys_segments--; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 356 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 357 | |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 358 | if (total_phys_segments > queue_max_segments(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 359 | return 0; |
| 360 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 361 | if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next)) |
| 362 | return 0; |
| 363 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 364 | /* Merge is OK... */ |
| 365 | req->nr_phys_segments = total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 366 | return 1; |
| 367 | } |
| 368 | |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 369 | /** |
| 370 | * blk_rq_set_mixed_merge - mark a request as mixed merge |
| 371 | * @rq: request to mark as mixed merge |
| 372 | * |
| 373 | * Description: |
| 374 | * @rq is about to be mixed merged. Make sure the attributes |
| 375 | * which can be mixed are set in each bio and mark @rq as mixed |
| 376 | * merged. |
| 377 | */ |
| 378 | void blk_rq_set_mixed_merge(struct request *rq) |
| 379 | { |
| 380 | unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; |
| 381 | struct bio *bio; |
| 382 | |
| 383 | if (rq->cmd_flags & REQ_MIXED_MERGE) |
| 384 | return; |
| 385 | |
| 386 | /* |
| 387 | * @rq will no longer represent mixable attributes for all the |
| 388 | * contained bios. It will just track those of the first one. |
| 389 | * Distributes the attributs to each bio. |
| 390 | */ |
| 391 | for (bio = rq->bio; bio; bio = bio->bi_next) { |
| 392 | WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && |
| 393 | (bio->bi_rw & REQ_FAILFAST_MASK) != ff); |
| 394 | bio->bi_rw |= ff; |
| 395 | } |
| 396 | rq->cmd_flags |= REQ_MIXED_MERGE; |
| 397 | } |
| 398 | |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 399 | static void blk_account_io_merge(struct request *req) |
| 400 | { |
| 401 | if (blk_do_io_stat(req)) { |
| 402 | struct hd_struct *part; |
| 403 | int cpu; |
| 404 | |
| 405 | cpu = part_stat_lock(); |
Jerome Marchand | 09e099d | 2011-01-05 16:57:38 +0100 | [diff] [blame] | 406 | part = req->part; |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 407 | |
| 408 | part_round_stats(cpu, part); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 409 | part_dec_in_flight(part, rq_data_dir(req)); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 410 | |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 411 | hd_struct_put(part); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 412 | part_stat_unlock(); |
| 413 | } |
| 414 | } |
| 415 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 416 | /* |
| 417 | * Has to be called with the request spinlock acquired |
| 418 | */ |
| 419 | static int attempt_merge(struct request_queue *q, struct request *req, |
| 420 | struct request *next) |
| 421 | { |
| 422 | if (!rq_mergeable(req) || !rq_mergeable(next)) |
| 423 | return 0; |
| 424 | |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 425 | if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags)) |
| 426 | return 0; |
| 427 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 428 | /* |
| 429 | * not contiguous |
| 430 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 431 | if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 432 | return 0; |
| 433 | |
| 434 | if (rq_data_dir(req) != rq_data_dir(next) |
| 435 | || req->rq_disk != next->rq_disk |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 436 | || req_no_special_merge(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 437 | return 0; |
| 438 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 439 | if (req->cmd_flags & REQ_WRITE_SAME && |
| 440 | !blk_write_same_mergeable(req->bio, next->bio)) |
| 441 | return 0; |
| 442 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 443 | /* |
| 444 | * If we are allowed to merge, then append bio list |
| 445 | * from next to rq and release next. merge_requests_fn |
| 446 | * will have updated segment counts, update sector |
| 447 | * counts here. |
| 448 | */ |
| 449 | if (!ll_merge_requests_fn(q, req, next)) |
| 450 | return 0; |
| 451 | |
| 452 | /* |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 453 | * If failfast settings disagree or any of the two is already |
| 454 | * a mixed merge, mark both as mixed before proceeding. This |
| 455 | * makes sure that all involved bios have mixable attributes |
| 456 | * set properly. |
| 457 | */ |
| 458 | if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || |
| 459 | (req->cmd_flags & REQ_FAILFAST_MASK) != |
| 460 | (next->cmd_flags & REQ_FAILFAST_MASK)) { |
| 461 | blk_rq_set_mixed_merge(req); |
| 462 | blk_rq_set_mixed_merge(next); |
| 463 | } |
| 464 | |
| 465 | /* |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 466 | * At this point we have either done a back merge |
| 467 | * or front merge. We need the smaller start_time of |
| 468 | * the merged requests to be the current request |
| 469 | * for accounting purposes. |
| 470 | */ |
| 471 | if (time_after(req->start_time, next->start_time)) |
| 472 | req->start_time = next->start_time; |
| 473 | |
| 474 | req->biotail->bi_next = next->bio; |
| 475 | req->biotail = next->biotail; |
| 476 | |
Tejun Heo | a2dec7b | 2009-05-07 22:24:44 +0900 | [diff] [blame] | 477 | req->__data_len += blk_rq_bytes(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 478 | |
| 479 | elv_merge_requests(q, req, next); |
| 480 | |
Jerome Marchand | 42dad76 | 2009-04-22 14:01:49 +0200 | [diff] [blame] | 481 | /* |
| 482 | * 'next' is going away, so update stats accordingly |
| 483 | */ |
| 484 | blk_account_io_merge(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 485 | |
| 486 | req->ioprio = ioprio_best(req->ioprio, next->ioprio); |
Jens Axboe | ab780f1 | 2008-08-26 10:25:02 +0200 | [diff] [blame] | 487 | if (blk_rq_cpu_valid(next)) |
| 488 | req->cpu = next->cpu; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 489 | |
Boaz Harrosh | 1cd96c2 | 2009-03-24 12:35:07 +0100 | [diff] [blame] | 490 | /* owner-ship of bio passed from next to req */ |
| 491 | next->bio = NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 492 | __blk_put_request(q, next); |
| 493 | return 1; |
| 494 | } |
| 495 | |
| 496 | int attempt_back_merge(struct request_queue *q, struct request *rq) |
| 497 | { |
| 498 | struct request *next = elv_latter_request(q, rq); |
| 499 | |
| 500 | if (next) |
| 501 | return attempt_merge(q, rq, next); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int attempt_front_merge(struct request_queue *q, struct request *rq) |
| 507 | { |
| 508 | struct request *prev = elv_former_request(q, rq); |
| 509 | |
| 510 | if (prev) |
| 511 | return attempt_merge(q, prev, rq); |
| 512 | |
| 513 | return 0; |
| 514 | } |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 515 | |
| 516 | int blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
| 517 | struct request *next) |
| 518 | { |
| 519 | return attempt_merge(q, rq, next); |
| 520 | } |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 521 | |
| 522 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio) |
| 523 | { |
Martin K. Petersen | e2a60da | 2012-09-18 12:19:25 -0400 | [diff] [blame] | 524 | if (!rq_mergeable(rq) || !bio_mergeable(bio)) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 525 | return false; |
| 526 | |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 527 | if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw)) |
| 528 | return false; |
| 529 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 530 | /* different data direction or already started, don't merge */ |
| 531 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 532 | return false; |
| 533 | |
| 534 | /* must be same device and not a special request */ |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 535 | if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq)) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 536 | return false; |
| 537 | |
| 538 | /* only merge integrity protected bio into ditto rq */ |
| 539 | if (bio_integrity(bio) != blk_integrity_rq(rq)) |
| 540 | return false; |
| 541 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 542 | /* must be using the same buffer */ |
| 543 | if (rq->cmd_flags & REQ_WRITE_SAME && |
| 544 | !blk_write_same_mergeable(rq->bio, bio)) |
| 545 | return false; |
| 546 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 547 | return true; |
| 548 | } |
| 549 | |
| 550 | int blk_try_merge(struct request *rq, struct bio *bio) |
| 551 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 552 | if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 553 | return ELEVATOR_BACK_MERGE; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 554 | else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 555 | return ELEVATOR_FRONT_MERGE; |
| 556 | return ELEVATOR_NO_MERGE; |
| 557 | } |