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 | |
Mike Krinkin | cda2264 | 2015-12-03 17:32:30 +0300 | [diff] [blame] | 10 | #include <trace/events/block.h> |
| 11 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 12 | #include "blk.h" |
| 13 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 14 | static struct bio *blk_bio_discard_split(struct request_queue *q, |
| 15 | struct bio *bio, |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 16 | struct bio_set *bs, |
| 17 | unsigned *nsegs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 18 | { |
| 19 | unsigned int max_discard_sectors, granularity; |
| 20 | int alignment; |
| 21 | sector_t tmp; |
| 22 | unsigned split_sectors; |
| 23 | |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 24 | *nsegs = 1; |
| 25 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 26 | /* Zero-sector (unknown) and one-sector granularities are the same. */ |
| 27 | granularity = max(q->limits.discard_granularity >> 9, 1U); |
| 28 | |
| 29 | max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); |
| 30 | max_discard_sectors -= max_discard_sectors % granularity; |
| 31 | |
| 32 | if (unlikely(!max_discard_sectors)) { |
| 33 | /* XXX: warn */ |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | if (bio_sectors(bio) <= max_discard_sectors) |
| 38 | return NULL; |
| 39 | |
| 40 | split_sectors = max_discard_sectors; |
| 41 | |
| 42 | /* |
| 43 | * If the next starting sector would be misaligned, stop the discard at |
| 44 | * the previous aligned sector. |
| 45 | */ |
| 46 | alignment = (q->limits.discard_alignment >> 9) % granularity; |
| 47 | |
| 48 | tmp = bio->bi_iter.bi_sector + split_sectors - alignment; |
| 49 | tmp = sector_div(tmp, granularity); |
| 50 | |
| 51 | if (split_sectors > tmp) |
| 52 | split_sectors -= tmp; |
| 53 | |
| 54 | return bio_split(bio, split_sectors, GFP_NOIO, bs); |
| 55 | } |
| 56 | |
| 57 | static struct bio *blk_bio_write_same_split(struct request_queue *q, |
| 58 | struct bio *bio, |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 59 | struct bio_set *bs, |
| 60 | unsigned *nsegs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 61 | { |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 62 | *nsegs = 1; |
| 63 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 64 | if (!q->limits.max_write_same_sectors) |
| 65 | return NULL; |
| 66 | |
| 67 | if (bio_sectors(bio) <= q->limits.max_write_same_sectors) |
| 68 | return NULL; |
| 69 | |
| 70 | return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); |
| 71 | } |
| 72 | |
| 73 | static struct bio *blk_bio_segment_split(struct request_queue *q, |
| 74 | struct bio *bio, |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 75 | struct bio_set *bs, |
| 76 | unsigned *segs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 77 | { |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 78 | struct bio_vec bv, bvprv, *bvprvp = NULL; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 79 | struct bvec_iter iter; |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 80 | unsigned seg_size = 0, nsegs = 0, sectors = 0; |
Ming Lei | 02e7074 | 2015-11-24 10:35:30 +0800 | [diff] [blame] | 81 | unsigned front_seg_size = bio->bi_seg_front_size; |
| 82 | bool do_split = true; |
| 83 | struct bio *new = NULL; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 84 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 85 | bio_for_each_segment(bv, bio, iter) { |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 86 | /* |
| 87 | * If the queue doesn't support SG gaps and adding this |
| 88 | * offset would create a gap, disallow it. |
| 89 | */ |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 90 | if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 91 | goto split; |
| 92 | |
Keith Busch | e36f620 | 2016-01-12 15:08:39 -0700 | [diff] [blame] | 93 | if (sectors + (bv.bv_len >> 9) > |
| 94 | blk_max_size_offset(q, bio->bi_iter.bi_sector)) { |
| 95 | /* |
| 96 | * Consider this a new segment if we're splitting in |
| 97 | * the middle of this vector. |
| 98 | */ |
| 99 | if (nsegs < queue_max_segments(q) && |
| 100 | sectors < blk_max_size_offset(q, |
| 101 | bio->bi_iter.bi_sector)) { |
| 102 | nsegs++; |
| 103 | sectors = blk_max_size_offset(q, |
| 104 | bio->bi_iter.bi_sector); |
| 105 | } |
| 106 | goto split; |
| 107 | } |
| 108 | |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 109 | if (bvprvp && blk_queue_cluster(q)) { |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 110 | if (seg_size + bv.bv_len > queue_max_segment_size(q)) |
| 111 | goto new_segment; |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 112 | if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv)) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 113 | goto new_segment; |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 114 | if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv)) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 115 | goto new_segment; |
| 116 | |
| 117 | seg_size += bv.bv_len; |
| 118 | bvprv = bv; |
Ming Lei | 578270b | 2015-11-24 10:35:29 +0800 | [diff] [blame] | 119 | bvprvp = &bvprv; |
Ming Lei | 52cc6ee | 2015-09-17 09:58:38 -0600 | [diff] [blame] | 120 | sectors += bv.bv_len >> 9; |
Ming Lei | a88d32a | 2015-11-30 16:05:49 +0800 | [diff] [blame] | 121 | |
| 122 | if (nsegs == 1 && seg_size > front_seg_size) |
| 123 | front_seg_size = seg_size; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | new_segment: |
| 127 | if (nsegs == queue_max_segments(q)) |
| 128 | goto split; |
| 129 | |
| 130 | nsegs++; |
| 131 | bvprv = bv; |
Ming Lei | 578270b | 2015-11-24 10:35:29 +0800 | [diff] [blame] | 132 | bvprvp = &bvprv; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 133 | seg_size = bv.bv_len; |
Ming Lei | 52cc6ee | 2015-09-17 09:58:38 -0600 | [diff] [blame] | 134 | sectors += bv.bv_len >> 9; |
Ming Lei | 02e7074 | 2015-11-24 10:35:30 +0800 | [diff] [blame] | 135 | |
| 136 | if (nsegs == 1 && seg_size > front_seg_size) |
| 137 | front_seg_size = seg_size; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ming Lei | 02e7074 | 2015-11-24 10:35:30 +0800 | [diff] [blame] | 140 | do_split = false; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 141 | split: |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 142 | *segs = nsegs; |
Ming Lei | 02e7074 | 2015-11-24 10:35:30 +0800 | [diff] [blame] | 143 | |
| 144 | if (do_split) { |
| 145 | new = bio_split(bio, sectors, GFP_NOIO, bs); |
| 146 | if (new) |
| 147 | bio = new; |
| 148 | } |
| 149 | |
| 150 | bio->bi_seg_front_size = front_seg_size; |
| 151 | if (seg_size > bio->bi_seg_back_size) |
| 152 | bio->bi_seg_back_size = seg_size; |
| 153 | |
| 154 | return do_split ? new : NULL; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void blk_queue_split(struct request_queue *q, struct bio **bio, |
| 158 | struct bio_set *bs) |
| 159 | { |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 160 | struct bio *split, *res; |
| 161 | unsigned nsegs; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 162 | |
| 163 | if ((*bio)->bi_rw & REQ_DISCARD) |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 164 | split = blk_bio_discard_split(q, *bio, bs, &nsegs); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 165 | else if ((*bio)->bi_rw & REQ_WRITE_SAME) |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 166 | split = blk_bio_write_same_split(q, *bio, bs, &nsegs); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 167 | else |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 168 | split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs); |
| 169 | |
| 170 | /* physical segments can be figured out during splitting */ |
| 171 | res = split ? split : *bio; |
| 172 | res->bi_phys_segments = nsegs; |
| 173 | bio_set_flag(res, BIO_SEG_VALID); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 174 | |
| 175 | if (split) { |
Ming Lei | 6ac45ae | 2015-10-20 23:13:53 +0800 | [diff] [blame] | 176 | /* there isn't chance to merge the splitted bio */ |
| 177 | split->bi_rw |= REQ_NOMERGE; |
| 178 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 179 | bio_chain(split, *bio); |
Mike Krinkin | cda2264 | 2015-12-03 17:32:30 +0300 | [diff] [blame] | 180 | trace_block_split(q, split, (*bio)->bi_iter.bi_sector); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 181 | generic_make_request(*bio); |
| 182 | *bio = split; |
| 183 | } |
| 184 | } |
| 185 | EXPORT_SYMBOL(blk_queue_split); |
| 186 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 187 | static unsigned int __blk_recalc_rq_segments(struct request_queue *q, |
Ming Lei | 0738854 | 2014-09-02 23:02:59 +0800 | [diff] [blame] | 188 | struct bio *bio, |
| 189 | bool no_sg_merge) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 190 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 191 | struct bio_vec bv, bvprv = { NULL }; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 192 | int cluster, prev = 0; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 193 | unsigned int seg_size, nr_phys_segs; |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 194 | struct bio *fbio, *bbio; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 195 | struct bvec_iter iter; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 196 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 197 | if (!bio) |
| 198 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 199 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 200 | /* |
| 201 | * This should probably be returning 0, but blk_add_request_payload() |
| 202 | * (Christoph!!!!) |
| 203 | */ |
| 204 | if (bio->bi_rw & REQ_DISCARD) |
| 205 | return 1; |
| 206 | |
| 207 | if (bio->bi_rw & REQ_WRITE_SAME) |
| 208 | return 1; |
| 209 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 210 | fbio = bio; |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 211 | cluster = blk_queue_cluster(q); |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 212 | seg_size = 0; |
Andi Kleen | 2c8919d | 2010-06-21 11:02:47 +0200 | [diff] [blame] | 213 | nr_phys_segs = 0; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 214 | for_each_bio(bio) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 215 | bio_for_each_segment(bv, bio, iter) { |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 216 | /* |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 217 | * If SG merging is disabled, each bio vector is |
| 218 | * a segment |
| 219 | */ |
| 220 | if (no_sg_merge) |
| 221 | goto new_segment; |
| 222 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 223 | if (prev && cluster) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 224 | if (seg_size + bv.bv_len |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 225 | > queue_max_segment_size(q)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 226 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 227 | if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 228 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 229 | if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 230 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 231 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 232 | seg_size += bv.bv_len; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 233 | bvprv = bv; |
| 234 | continue; |
| 235 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 236 | new_segment: |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 237 | if (nr_phys_segs == 1 && seg_size > |
| 238 | fbio->bi_seg_front_size) |
| 239 | fbio->bi_seg_front_size = seg_size; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 240 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 241 | nr_phys_segs++; |
| 242 | bvprv = bv; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 243 | prev = 1; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 244 | seg_size = bv.bv_len; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 245 | } |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 246 | bbio = bio; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 249 | if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) |
| 250 | fbio->bi_seg_front_size = seg_size; |
| 251 | if (seg_size > bbio->bi_seg_back_size) |
| 252 | bbio->bi_seg_back_size = seg_size; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 253 | |
| 254 | return nr_phys_segs; |
| 255 | } |
| 256 | |
| 257 | void blk_recalc_rq_segments(struct request *rq) |
| 258 | { |
Ming Lei | 0738854 | 2014-09-02 23:02:59 +0800 | [diff] [blame] | 259 | bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, |
| 260 | &rq->q->queue_flags); |
| 261 | |
| 262 | rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, |
| 263 | no_sg_merge); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void blk_recount_segments(struct request_queue *q, struct bio *bio) |
| 267 | { |
Ming Lei | 7f60dca | 2014-11-12 00:15:41 +0800 | [diff] [blame] | 268 | unsigned short seg_cnt; |
Ming Lei | 764f612 | 2014-10-09 23:17:35 +0800 | [diff] [blame] | 269 | |
Ming Lei | 7f60dca | 2014-11-12 00:15:41 +0800 | [diff] [blame] | 270 | /* estimate segment number by bi_vcnt for non-cloned bio */ |
| 271 | if (bio_flagged(bio, BIO_CLONED)) |
| 272 | seg_cnt = bio_segments(bio); |
| 273 | else |
| 274 | seg_cnt = bio->bi_vcnt; |
| 275 | |
| 276 | if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && |
| 277 | (seg_cnt < queue_max_segments(q))) |
| 278 | bio->bi_phys_segments = seg_cnt; |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 279 | else { |
| 280 | struct bio *nxt = bio->bi_next; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 281 | |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 282 | bio->bi_next = NULL; |
Ming Lei | 7f60dca | 2014-11-12 00:15:41 +0800 | [diff] [blame] | 283 | bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 284 | bio->bi_next = nxt; |
| 285 | } |
| 286 | |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 287 | bio_set_flag(bio, BIO_SEG_VALID); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 288 | } |
| 289 | EXPORT_SYMBOL(blk_recount_segments); |
| 290 | |
| 291 | static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, |
| 292 | struct bio *nxt) |
| 293 | { |
Kent Overstreet | 2b8221e | 2013-12-03 14:29:09 -0700 | [diff] [blame] | 294 | struct bio_vec end_bv = { NULL }, nxt_bv; |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 295 | struct bvec_iter iter; |
| 296 | |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 297 | if (!blk_queue_cluster(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 300 | if (bio->bi_seg_back_size + nxt->bi_seg_front_size > |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 301 | queue_max_segment_size(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 302 | return 0; |
| 303 | |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 304 | if (!bio_has_data(bio)) |
| 305 | return 1; |
| 306 | |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 307 | bio_for_each_segment(end_bv, bio, iter) |
| 308 | if (end_bv.bv_len == iter.bi_size) |
| 309 | break; |
| 310 | |
| 311 | nxt_bv = bio_iovec(nxt); |
| 312 | |
| 313 | if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 314 | return 0; |
| 315 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 316 | /* |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 317 | * bio and nxt are contiguous in memory; check if the queue allows |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 318 | * these two to be merged into one |
| 319 | */ |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 320 | if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 321 | return 1; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 326 | static inline void |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 327 | __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 328 | struct scatterlist *sglist, struct bio_vec *bvprv, |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 329 | struct scatterlist **sg, int *nsegs, int *cluster) |
| 330 | { |
| 331 | |
| 332 | int nbytes = bvec->bv_len; |
| 333 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 334 | if (*sg && *cluster) { |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 335 | if ((*sg)->length + nbytes > queue_max_segment_size(q)) |
| 336 | goto new_segment; |
| 337 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 338 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 339 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 340 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 341 | goto new_segment; |
| 342 | |
| 343 | (*sg)->length += nbytes; |
| 344 | } else { |
| 345 | new_segment: |
| 346 | if (!*sg) |
| 347 | *sg = sglist; |
| 348 | else { |
| 349 | /* |
| 350 | * If the driver previously mapped a shorter |
| 351 | * list, we could see a termination bit |
| 352 | * prematurely unless it fully inits the sg |
| 353 | * table on each mapping. We KNOW that there |
| 354 | * must be more entries here or the driver |
| 355 | * would be buggy, so force clear the |
| 356 | * termination bit to avoid doing a full |
| 357 | * sg_init_table() in drivers for each command. |
| 358 | */ |
Paolo Bonzini | c8164d8 | 2013-03-20 15:37:08 +1030 | [diff] [blame] | 359 | sg_unmark_end(*sg); |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 360 | *sg = sg_next(*sg); |
| 361 | } |
| 362 | |
| 363 | sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); |
| 364 | (*nsegs)++; |
| 365 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 366 | *bvprv = *bvec; |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 369 | static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, |
| 370 | struct scatterlist *sglist, |
| 371 | struct scatterlist **sg) |
| 372 | { |
| 373 | struct bio_vec bvec, bvprv = { NULL }; |
| 374 | struct bvec_iter iter; |
| 375 | int nsegs, cluster; |
| 376 | |
| 377 | nsegs = 0; |
| 378 | cluster = blk_queue_cluster(q); |
| 379 | |
| 380 | if (bio->bi_rw & REQ_DISCARD) { |
| 381 | /* |
| 382 | * This is a hack - drivers should be neither modifying the |
| 383 | * biovec, nor relying on bi_vcnt - but because of |
| 384 | * blk_add_request_payload(), a discard bio may or may not have |
| 385 | * a payload we need to set up here (thank you Christoph) and |
| 386 | * bi_vcnt is really the only way of telling if we need to. |
| 387 | */ |
| 388 | |
| 389 | if (bio->bi_vcnt) |
| 390 | goto single_segment; |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | if (bio->bi_rw & REQ_WRITE_SAME) { |
| 396 | single_segment: |
| 397 | *sg = sglist; |
| 398 | bvec = bio_iovec(bio); |
| 399 | sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset); |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | for_each_bio(bio) |
| 404 | bio_for_each_segment(bvec, bio, iter) |
| 405 | __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, |
| 406 | &nsegs, &cluster); |
| 407 | |
| 408 | return nsegs; |
| 409 | } |
| 410 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 411 | /* |
| 412 | * map a request to scatterlist, return number of sg entries setup. Caller |
| 413 | * must make sure sg can hold rq->nr_phys_segments entries |
| 414 | */ |
| 415 | int blk_rq_map_sg(struct request_queue *q, struct request *rq, |
| 416 | struct scatterlist *sglist) |
| 417 | { |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 418 | struct scatterlist *sg = NULL; |
| 419 | int nsegs = 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 420 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 421 | if (rq->bio) |
| 422 | nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 423 | |
| 424 | if (unlikely(rq->cmd_flags & REQ_COPY_USER) && |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 425 | (blk_rq_bytes(rq) & q->dma_pad_mask)) { |
| 426 | unsigned int pad_len = |
| 427 | (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 428 | |
| 429 | sg->length += pad_len; |
| 430 | rq->extra_len += pad_len; |
| 431 | } |
| 432 | |
Tejun Heo | 2fb98e8 | 2008-02-19 11:36:53 +0100 | [diff] [blame] | 433 | if (q->dma_drain_size && q->dma_drain_needed(rq)) { |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 434 | if (rq->cmd_flags & REQ_WRITE) |
Tejun Heo | db0a2e0 | 2008-02-19 11:36:55 +0100 | [diff] [blame] | 435 | memset(q->dma_drain_buffer, 0, q->dma_drain_size); |
| 436 | |
Dan Williams | da81ed1 | 2015-08-07 18:15:14 +0200 | [diff] [blame] | 437 | sg_unmark_end(sg); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 438 | sg = sg_next(sg); |
| 439 | sg_set_page(sg, virt_to_page(q->dma_drain_buffer), |
| 440 | q->dma_drain_size, |
| 441 | ((unsigned long)q->dma_drain_buffer) & |
| 442 | (PAGE_SIZE - 1)); |
| 443 | nsegs++; |
FUJITA Tomonori | 7a85f88 | 2008-03-04 11:17:11 +0100 | [diff] [blame] | 444 | rq->extra_len += q->dma_drain_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (sg) |
| 448 | sg_mark_end(sg); |
| 449 | |
Ming Lei | 12e57f5 | 2015-11-24 10:35:31 +0800 | [diff] [blame] | 450 | /* |
| 451 | * Something must have been wrong if the figured number of |
| 452 | * segment is bigger than number of req's physical segments |
| 453 | */ |
| 454 | WARN_ON(nsegs > rq->nr_phys_segments); |
| 455 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 456 | return nsegs; |
| 457 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 458 | EXPORT_SYMBOL(blk_rq_map_sg); |
| 459 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 460 | static inline int ll_new_hw_segment(struct request_queue *q, |
| 461 | struct request *req, |
| 462 | struct bio *bio) |
| 463 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 464 | int nr_phys_segs = bio_phys_segments(q, bio); |
| 465 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 466 | if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) |
| 467 | goto no_merge; |
| 468 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 469 | if (blk_integrity_merge_bio(q, req, bio) == false) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 470 | goto no_merge; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 471 | |
| 472 | /* |
| 473 | * This will form the start of a new hw segment. Bump both |
| 474 | * counters. |
| 475 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 476 | req->nr_phys_segments += nr_phys_segs; |
| 477 | return 1; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 478 | |
| 479 | no_merge: |
| 480 | req->cmd_flags |= REQ_NOMERGE; |
| 481 | if (req == q->last_merge) |
| 482 | q->last_merge = NULL; |
| 483 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | int ll_back_merge_fn(struct request_queue *q, struct request *req, |
| 487 | struct bio *bio) |
| 488 | { |
Jens Axboe | 5e7c427 | 2015-09-03 19:28:20 +0300 | [diff] [blame] | 489 | if (req_gap_back_merge(req, bio)) |
| 490 | return 0; |
Sagi Grimberg | 7f39add | 2015-09-11 09:03:04 -0600 | [diff] [blame] | 491 | if (blk_integrity_rq(req) && |
| 492 | integrity_req_gap_back_merge(req, bio)) |
| 493 | return 0; |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 494 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 495 | blk_rq_get_max_sectors(req)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 496 | req->cmd_flags |= REQ_NOMERGE; |
| 497 | if (req == q->last_merge) |
| 498 | q->last_merge = NULL; |
| 499 | return 0; |
| 500 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 501 | if (!bio_flagged(req->biotail, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 502 | blk_recount_segments(q, req->biotail); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 503 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 504 | blk_recount_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 505 | |
| 506 | return ll_new_hw_segment(q, req, bio); |
| 507 | } |
| 508 | |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 509 | int ll_front_merge_fn(struct request_queue *q, struct request *req, |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 510 | struct bio *bio) |
| 511 | { |
Jens Axboe | 5e7c427 | 2015-09-03 19:28:20 +0300 | [diff] [blame] | 512 | |
| 513 | if (req_gap_front_merge(req, bio)) |
| 514 | return 0; |
Sagi Grimberg | 7f39add | 2015-09-11 09:03:04 -0600 | [diff] [blame] | 515 | if (blk_integrity_rq(req) && |
| 516 | integrity_req_gap_front_merge(req, bio)) |
| 517 | return 0; |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 518 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 519 | blk_rq_get_max_sectors(req)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 520 | req->cmd_flags |= REQ_NOMERGE; |
| 521 | if (req == q->last_merge) |
| 522 | q->last_merge = NULL; |
| 523 | return 0; |
| 524 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 525 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 526 | blk_recount_segments(q, bio); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 527 | if (!bio_flagged(req->bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 528 | blk_recount_segments(q, req->bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 529 | |
| 530 | return ll_new_hw_segment(q, req, bio); |
| 531 | } |
| 532 | |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 533 | /* |
| 534 | * blk-mq uses req->special to carry normal driver per-request payload, it |
| 535 | * does not indicate a prepared command that we cannot merge with. |
| 536 | */ |
| 537 | static bool req_no_special_merge(struct request *req) |
| 538 | { |
| 539 | struct request_queue *q = req->q; |
| 540 | |
| 541 | return !q->mq_ops && req->special; |
| 542 | } |
| 543 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 544 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, |
| 545 | struct request *next) |
| 546 | { |
| 547 | int total_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 548 | unsigned int seg_size = |
| 549 | req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * First check if the either of the requests are re-queued |
| 553 | * requests. Can't merge them if they are. |
| 554 | */ |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 555 | if (req_no_special_merge(req) || req_no_special_merge(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 556 | return 0; |
| 557 | |
Jens Axboe | 5e7c427 | 2015-09-03 19:28:20 +0300 | [diff] [blame] | 558 | if (req_gap_back_merge(req, next->bio)) |
Keith Busch | 854fbb9 | 2015-02-11 08:20:13 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 561 | /* |
| 562 | * Will it become too large? |
| 563 | */ |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 564 | if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > |
| 565 | blk_rq_get_max_sectors(req)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 566 | return 0; |
| 567 | |
| 568 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 569 | if (blk_phys_contig_segment(q, req->biotail, next->bio)) { |
| 570 | if (req->nr_phys_segments == 1) |
| 571 | req->bio->bi_seg_front_size = seg_size; |
| 572 | if (next->nr_phys_segments == 1) |
| 573 | next->biotail->bi_seg_back_size = seg_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 574 | total_phys_segments--; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 575 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 576 | |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 577 | if (total_phys_segments > queue_max_segments(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 578 | return 0; |
| 579 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 580 | if (blk_integrity_merge_rq(q, req, next) == false) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 581 | return 0; |
| 582 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 583 | /* Merge is OK... */ |
| 584 | req->nr_phys_segments = total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 585 | return 1; |
| 586 | } |
| 587 | |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 588 | /** |
| 589 | * blk_rq_set_mixed_merge - mark a request as mixed merge |
| 590 | * @rq: request to mark as mixed merge |
| 591 | * |
| 592 | * Description: |
| 593 | * @rq is about to be mixed merged. Make sure the attributes |
| 594 | * which can be mixed are set in each bio and mark @rq as mixed |
| 595 | * merged. |
| 596 | */ |
| 597 | void blk_rq_set_mixed_merge(struct request *rq) |
| 598 | { |
| 599 | unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; |
| 600 | struct bio *bio; |
| 601 | |
| 602 | if (rq->cmd_flags & REQ_MIXED_MERGE) |
| 603 | return; |
| 604 | |
| 605 | /* |
| 606 | * @rq will no longer represent mixable attributes for all the |
| 607 | * contained bios. It will just track those of the first one. |
| 608 | * Distributes the attributs to each bio. |
| 609 | */ |
| 610 | for (bio = rq->bio; bio; bio = bio->bi_next) { |
| 611 | WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && |
| 612 | (bio->bi_rw & REQ_FAILFAST_MASK) != ff); |
| 613 | bio->bi_rw |= ff; |
| 614 | } |
| 615 | rq->cmd_flags |= REQ_MIXED_MERGE; |
| 616 | } |
| 617 | |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 618 | static void blk_account_io_merge(struct request *req) |
| 619 | { |
| 620 | if (blk_do_io_stat(req)) { |
| 621 | struct hd_struct *part; |
| 622 | int cpu; |
| 623 | |
| 624 | cpu = part_stat_lock(); |
Jerome Marchand | 09e099d | 2011-01-05 16:57:38 +0100 | [diff] [blame] | 625 | part = req->part; |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 626 | |
| 627 | part_round_stats(cpu, part); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 628 | part_dec_in_flight(part, rq_data_dir(req)); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 629 | |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 630 | hd_struct_put(part); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 631 | part_stat_unlock(); |
| 632 | } |
| 633 | } |
| 634 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 635 | /* |
| 636 | * Has to be called with the request spinlock acquired |
| 637 | */ |
| 638 | static int attempt_merge(struct request_queue *q, struct request *req, |
| 639 | struct request *next) |
| 640 | { |
| 641 | if (!rq_mergeable(req) || !rq_mergeable(next)) |
| 642 | return 0; |
| 643 | |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 644 | if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags)) |
| 645 | return 0; |
| 646 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 647 | /* |
| 648 | * not contiguous |
| 649 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 650 | 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] | 651 | return 0; |
| 652 | |
| 653 | if (rq_data_dir(req) != rq_data_dir(next) |
| 654 | || req->rq_disk != next->rq_disk |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 655 | || req_no_special_merge(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 656 | return 0; |
| 657 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 658 | if (req->cmd_flags & REQ_WRITE_SAME && |
| 659 | !blk_write_same_mergeable(req->bio, next->bio)) |
| 660 | return 0; |
| 661 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 662 | /* |
| 663 | * If we are allowed to merge, then append bio list |
| 664 | * from next to rq and release next. merge_requests_fn |
| 665 | * will have updated segment counts, update sector |
| 666 | * counts here. |
| 667 | */ |
| 668 | if (!ll_merge_requests_fn(q, req, next)) |
| 669 | return 0; |
| 670 | |
| 671 | /* |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 672 | * If failfast settings disagree or any of the two is already |
| 673 | * a mixed merge, mark both as mixed before proceeding. This |
| 674 | * makes sure that all involved bios have mixable attributes |
| 675 | * set properly. |
| 676 | */ |
| 677 | if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || |
| 678 | (req->cmd_flags & REQ_FAILFAST_MASK) != |
| 679 | (next->cmd_flags & REQ_FAILFAST_MASK)) { |
| 680 | blk_rq_set_mixed_merge(req); |
| 681 | blk_rq_set_mixed_merge(next); |
| 682 | } |
| 683 | |
| 684 | /* |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 685 | * At this point we have either done a back merge |
| 686 | * or front merge. We need the smaller start_time of |
| 687 | * the merged requests to be the current request |
| 688 | * for accounting purposes. |
| 689 | */ |
| 690 | if (time_after(req->start_time, next->start_time)) |
| 691 | req->start_time = next->start_time; |
| 692 | |
| 693 | req->biotail->bi_next = next->bio; |
| 694 | req->biotail = next->biotail; |
| 695 | |
Tejun Heo | a2dec7b | 2009-05-07 22:24:44 +0900 | [diff] [blame] | 696 | req->__data_len += blk_rq_bytes(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 697 | |
| 698 | elv_merge_requests(q, req, next); |
| 699 | |
Jerome Marchand | 42dad76 | 2009-04-22 14:01:49 +0200 | [diff] [blame] | 700 | /* |
| 701 | * 'next' is going away, so update stats accordingly |
| 702 | */ |
| 703 | blk_account_io_merge(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 704 | |
| 705 | req->ioprio = ioprio_best(req->ioprio, next->ioprio); |
Jens Axboe | ab780f1 | 2008-08-26 10:25:02 +0200 | [diff] [blame] | 706 | if (blk_rq_cpu_valid(next)) |
| 707 | req->cpu = next->cpu; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 708 | |
Boaz Harrosh | 1cd96c2 | 2009-03-24 12:35:07 +0100 | [diff] [blame] | 709 | /* owner-ship of bio passed from next to req */ |
| 710 | next->bio = NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 711 | __blk_put_request(q, next); |
| 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | int attempt_back_merge(struct request_queue *q, struct request *rq) |
| 716 | { |
| 717 | struct request *next = elv_latter_request(q, rq); |
| 718 | |
| 719 | if (next) |
| 720 | return attempt_merge(q, rq, next); |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | int attempt_front_merge(struct request_queue *q, struct request *rq) |
| 726 | { |
| 727 | struct request *prev = elv_former_request(q, rq); |
| 728 | |
| 729 | if (prev) |
| 730 | return attempt_merge(q, prev, rq); |
| 731 | |
| 732 | return 0; |
| 733 | } |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 734 | |
| 735 | int blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
| 736 | struct request *next) |
| 737 | { |
| 738 | return attempt_merge(q, rq, next); |
| 739 | } |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 740 | |
| 741 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio) |
| 742 | { |
Martin K. Petersen | e2a60da | 2012-09-18 12:19:25 -0400 | [diff] [blame] | 743 | if (!rq_mergeable(rq) || !bio_mergeable(bio)) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 744 | return false; |
| 745 | |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 746 | if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw)) |
| 747 | return false; |
| 748 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 749 | /* different data direction or already started, don't merge */ |
| 750 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 751 | return false; |
| 752 | |
| 753 | /* must be same device and not a special request */ |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 754 | 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] | 755 | return false; |
| 756 | |
| 757 | /* only merge integrity protected bio into ditto rq */ |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 758 | if (blk_integrity_merge_bio(rq->q, rq, bio) == false) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 759 | return false; |
| 760 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 761 | /* must be using the same buffer */ |
| 762 | if (rq->cmd_flags & REQ_WRITE_SAME && |
| 763 | !blk_write_same_mergeable(rq->bio, bio)) |
| 764 | return false; |
| 765 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 766 | return true; |
| 767 | } |
| 768 | |
| 769 | int blk_try_merge(struct request *rq, struct bio *bio) |
| 770 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 771 | 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] | 772 | return ELEVATOR_BACK_MERGE; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 773 | 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] | 774 | return ELEVATOR_FRONT_MERGE; |
| 775 | return ELEVATOR_NO_MERGE; |
| 776 | } |