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