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