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