blob: 1699df5b0493a721a5ee1945a07c8ec516856164 [file] [log] [blame]
Jens Axboed6d48192008-01-29 14:04:06 +01001/*
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 Krinkincda22642015-12-03 17:32:30 +030010#include <trace/events/block.h>
11
Jens Axboed6d48192008-01-29 14:04:06 +010012#include "blk.h"
13
Kent Overstreet54efd502015-04-23 22:37:18 -070014static struct bio *blk_bio_discard_split(struct request_queue *q,
15 struct bio *bio,
Ming Leibdced432015-10-20 23:13:52 +080016 struct bio_set *bs,
17 unsigned *nsegs)
Kent Overstreet54efd502015-04-23 22:37:18 -070018{
19 unsigned int max_discard_sectors, granularity;
20 int alignment;
21 sector_t tmp;
22 unsigned split_sectors;
23
Ming Leibdced432015-10-20 23:13:52 +080024 *nsegs = 1;
25
Kent Overstreet54efd502015-04-23 22:37:18 -070026 /* 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
57static struct bio *blk_bio_write_same_split(struct request_queue *q,
58 struct bio *bio,
Ming Leibdced432015-10-20 23:13:52 +080059 struct bio_set *bs,
60 unsigned *nsegs)
Kent Overstreet54efd502015-04-23 22:37:18 -070061{
Ming Leibdced432015-10-20 23:13:52 +080062 *nsegs = 1;
63
Kent Overstreet54efd502015-04-23 22:37:18 -070064 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
73static struct bio *blk_bio_segment_split(struct request_queue *q,
74 struct bio *bio,
Ming Leibdced432015-10-20 23:13:52 +080075 struct bio_set *bs,
76 unsigned *segs)
Kent Overstreet54efd502015-04-23 22:37:18 -070077{
Jens Axboe5014c312015-09-02 16:46:02 -060078 struct bio_vec bv, bvprv, *bvprvp = NULL;
Kent Overstreet54efd502015-04-23 22:37:18 -070079 struct bvec_iter iter;
Kent Overstreet8ae12662015-04-27 23:48:34 -070080 unsigned seg_size = 0, nsegs = 0, sectors = 0;
Ming Lei02e70742015-11-24 10:35:30 +080081 unsigned front_seg_size = bio->bi_seg_front_size;
82 bool do_split = true;
83 struct bio *new = NULL;
Kent Overstreet54efd502015-04-23 22:37:18 -070084
Kent Overstreet54efd502015-04-23 22:37:18 -070085 bio_for_each_segment(bv, bio, iter) {
Kent Overstreet54efd502015-04-23 22:37:18 -070086 /*
87 * If the queue doesn't support SG gaps and adding this
88 * offset would create a gap, disallow it.
89 */
Jens Axboe5014c312015-09-02 16:46:02 -060090 if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
Kent Overstreet54efd502015-04-23 22:37:18 -070091 goto split;
92
Keith Busche36f6202016-01-12 15:08:39 -070093 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 Axboe5014c312015-09-02 16:46:02 -0600109 if (bvprvp && blk_queue_cluster(q)) {
Kent Overstreet54efd502015-04-23 22:37:18 -0700110 if (seg_size + bv.bv_len > queue_max_segment_size(q))
111 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -0600112 if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -0700113 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -0600114 if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -0700115 goto new_segment;
116
117 seg_size += bv.bv_len;
118 bvprv = bv;
Ming Lei578270b2015-11-24 10:35:29 +0800119 bvprvp = &bvprv;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600120 sectors += bv.bv_len >> 9;
Ming Leia88d32a2015-11-30 16:05:49 +0800121
122 if (nsegs == 1 && seg_size > front_seg_size)
123 front_seg_size = seg_size;
Kent Overstreet54efd502015-04-23 22:37:18 -0700124 continue;
125 }
126new_segment:
127 if (nsegs == queue_max_segments(q))
128 goto split;
129
130 nsegs++;
131 bvprv = bv;
Ming Lei578270b2015-11-24 10:35:29 +0800132 bvprvp = &bvprv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700133 seg_size = bv.bv_len;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600134 sectors += bv.bv_len >> 9;
Ming Lei02e70742015-11-24 10:35:30 +0800135
136 if (nsegs == 1 && seg_size > front_seg_size)
137 front_seg_size = seg_size;
Kent Overstreet54efd502015-04-23 22:37:18 -0700138 }
139
Ming Lei02e70742015-11-24 10:35:30 +0800140 do_split = false;
Kent Overstreet54efd502015-04-23 22:37:18 -0700141split:
Ming Leibdced432015-10-20 23:13:52 +0800142 *segs = nsegs;
Ming Lei02e70742015-11-24 10:35:30 +0800143
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 Overstreet54efd502015-04-23 22:37:18 -0700155}
156
157void blk_queue_split(struct request_queue *q, struct bio **bio,
158 struct bio_set *bs)
159{
Ming Leibdced432015-10-20 23:13:52 +0800160 struct bio *split, *res;
161 unsigned nsegs;
Kent Overstreet54efd502015-04-23 22:37:18 -0700162
163 if ((*bio)->bi_rw & REQ_DISCARD)
Ming Leibdced432015-10-20 23:13:52 +0800164 split = blk_bio_discard_split(q, *bio, bs, &nsegs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700165 else if ((*bio)->bi_rw & REQ_WRITE_SAME)
Ming Leibdced432015-10-20 23:13:52 +0800166 split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700167 else
Ming Leibdced432015-10-20 23:13:52 +0800168 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 Overstreet54efd502015-04-23 22:37:18 -0700174
175 if (split) {
Ming Lei6ac45ae2015-10-20 23:13:53 +0800176 /* there isn't chance to merge the splitted bio */
177 split->bi_rw |= REQ_NOMERGE;
178
Kent Overstreet54efd502015-04-23 22:37:18 -0700179 bio_chain(split, *bio);
Mike Krinkincda22642015-12-03 17:32:30 +0300180 trace_block_split(q, split, (*bio)->bi_iter.bi_sector);
Kent Overstreet54efd502015-04-23 22:37:18 -0700181 generic_make_request(*bio);
182 *bio = split;
183 }
184}
185EXPORT_SYMBOL(blk_queue_split);
186
Jens Axboe1e428072009-02-23 09:03:10 +0100187static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +0800188 struct bio *bio,
189 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +0100190{
Kent Overstreet79886132013-11-23 17:19:00 -0800191 struct bio_vec bv, bvprv = { NULL };
Kent Overstreet54efd502015-04-23 22:37:18 -0700192 int cluster, prev = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100193 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +0100194 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -0800195 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +0100196
Jens Axboe1e428072009-02-23 09:03:10 +0100197 if (!bio)
198 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100199
Kent Overstreet5cb88502014-02-07 13:53:46 -0700200 /*
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 Axboe1e428072009-02-23 09:03:10 +0100210 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100211 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200212 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +0200213 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100214 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -0800215 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +0100216 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -0600217 * If SG merging is disabled, each bio vector is
218 * a segment
219 */
220 if (no_sg_merge)
221 goto new_segment;
222
Kent Overstreet54efd502015-04-23 22:37:18 -0700223 if (prev && cluster) {
Kent Overstreet79886132013-11-23 17:19:00 -0800224 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400225 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +0100226 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800227 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100228 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800229 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100230 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100231
Kent Overstreet79886132013-11-23 17:19:00 -0800232 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100233 bvprv = bv;
234 continue;
235 }
Jens Axboed6d48192008-01-29 14:04:06 +0100236new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +0100237 if (nr_phys_segs == 1 && seg_size >
238 fbio->bi_seg_front_size)
239 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200240
Jens Axboe1e428072009-02-23 09:03:10 +0100241 nr_phys_segs++;
242 bvprv = bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700243 prev = 1;
Kent Overstreet79886132013-11-23 17:19:00 -0800244 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100245 }
Jens Axboe59247ea2009-03-06 08:55:24 +0100246 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +0100247 }
248
Jens Axboe59247ea2009-03-06 08:55:24 +0100249 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 Axboe1e428072009-02-23 09:03:10 +0100253
254 return nr_phys_segs;
255}
256
257void blk_recalc_rq_segments(struct request *rq)
258{
Ming Lei07388542014-09-02 23:02:59 +0800259 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 Axboed6d48192008-01-29 14:04:06 +0100264}
265
266void blk_recount_segments(struct request_queue *q, struct bio *bio)
267{
Ming Lei7f60dca2014-11-12 00:15:41 +0800268 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800269
Ming Lei7f60dca2014-11-12 00:15:41 +0800270 /* 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 Axboe05f1dd52014-05-29 09:53:32 -0600279 else {
280 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100281
Jens Axboe05f1dd52014-05-29 09:53:32 -0600282 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800283 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600284 bio->bi_next = nxt;
285 }
286
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600287 bio_set_flag(bio, BIO_SEG_VALID);
Jens Axboed6d48192008-01-29 14:04:06 +0100288}
289EXPORT_SYMBOL(blk_recount_segments);
290
291static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
292 struct bio *nxt)
293{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700294 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700295 struct bvec_iter iter;
296
Martin K. Petersene692cb62010-12-01 19:41:49 +0100297 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100298 return 0;
299
FUJITA Tomonori86771422008-10-13 14:19:05 +0200300 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400301 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100302 return 0;
303
David Woodhousee17fc0a2008-08-09 16:42:20 +0100304 if (!bio_has_data(bio))
305 return 1;
306
Kent Overstreetf619d252013-08-07 14:30:33 -0700307 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 Woodhousee17fc0a2008-08-09 16:42:20 +0100314 return 0;
315
Jens Axboed6d48192008-01-29 14:04:06 +0100316 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100317 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100318 * these two to be merged into one
319 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700320 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100321 return 1;
322
323 return 0;
324}
325
Kent Overstreet79886132013-11-23 17:19:00 -0800326static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200327__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800328 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200329 struct scatterlist **sg, int *nsegs, int *cluster)
330{
331
332 int nbytes = bvec->bv_len;
333
Kent Overstreet79886132013-11-23 17:19:00 -0800334 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200335 if ((*sg)->length + nbytes > queue_max_segment_size(q))
336 goto new_segment;
337
Kent Overstreet79886132013-11-23 17:19:00 -0800338 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200339 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800340 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200341 goto new_segment;
342
343 (*sg)->length += nbytes;
344 } else {
345new_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 Bonzinic8164d82013-03-20 15:37:08 +1030359 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200360 *sg = sg_next(*sg);
361 }
362
363 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
364 (*nsegs)++;
365 }
Kent Overstreet79886132013-11-23 17:19:00 -0800366 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200367}
368
Kent Overstreet5cb88502014-02-07 13:53:46 -0700369static 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) {
396single_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 Axboed6d48192008-01-29 14:04:06 +0100411/*
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 */
415int blk_rq_map_sg(struct request_queue *q, struct request *rq,
416 struct scatterlist *sglist)
417{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700418 struct scatterlist *sg = NULL;
419 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100420
Kent Overstreet5cb88502014-02-07 13:53:46 -0700421 if (rq->bio)
422 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200423
424 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900425 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
426 unsigned int pad_len =
427 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200428
429 sg->length += pad_len;
430 rq->extra_len += pad_len;
431 }
432
Tejun Heo2fb98e82008-02-19 11:36:53 +0100433 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200434 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100435 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
436
Dan Williamsda81ed12015-08-07 18:15:14 +0200437 sg_unmark_end(sg);
Jens Axboed6d48192008-01-29 14:04:06 +0100438 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 Tomonori7a85f882008-03-04 11:17:11 +0100444 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100445 }
446
447 if (sg)
448 sg_mark_end(sg);
449
Ming Lei12e57f52015-11-24 10:35:31 +0800450 /*
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 Axboed6d48192008-01-29 14:04:06 +0100456 return nsegs;
457}
Jens Axboed6d48192008-01-29 14:04:06 +0100458EXPORT_SYMBOL(blk_rq_map_sg);
459
Jens Axboed6d48192008-01-29 14:04:06 +0100460static inline int ll_new_hw_segment(struct request_queue *q,
461 struct request *req,
462 struct bio *bio)
463{
Jens Axboed6d48192008-01-29 14:04:06 +0100464 int nr_phys_segs = bio_phys_segments(q, bio);
465
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200466 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
467 goto no_merge;
468
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400469 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200470 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100471
472 /*
473 * This will form the start of a new hw segment. Bump both
474 * counters.
475 */
Jens Axboed6d48192008-01-29 14:04:06 +0100476 req->nr_phys_segments += nr_phys_segs;
477 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200478
479no_merge:
480 req->cmd_flags |= REQ_NOMERGE;
481 if (req == q->last_merge)
482 q->last_merge = NULL;
483 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100484}
485
486int ll_back_merge_fn(struct request_queue *q, struct request *req,
487 struct bio *bio)
488{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300489 if (req_gap_back_merge(req, bio))
490 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600491 if (blk_integrity_rq(req) &&
492 integrity_req_gap_back_merge(req, bio))
493 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400494 if (blk_rq_sectors(req) + bio_sectors(bio) >
495 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100496 req->cmd_flags |= REQ_NOMERGE;
497 if (req == q->last_merge)
498 q->last_merge = NULL;
499 return 0;
500 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200501 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100502 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200503 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100504 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100505
506 return ll_new_hw_segment(q, req, bio);
507}
508
Jens Axboe6728cb02008-01-31 13:03:55 +0100509int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100510 struct bio *bio)
511{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300512
513 if (req_gap_front_merge(req, bio))
514 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600515 if (blk_integrity_rq(req) &&
516 integrity_req_gap_front_merge(req, bio))
517 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400518 if (blk_rq_sectors(req) + bio_sectors(bio) >
519 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100520 req->cmd_flags |= REQ_NOMERGE;
521 if (req == q->last_merge)
522 q->last_merge = NULL;
523 return 0;
524 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200525 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100526 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200527 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100528 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100529
530 return ll_new_hw_segment(q, req, bio);
531}
532
Jens Axboee7e24502013-10-29 12:11:47 -0600533/*
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 */
537static 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 Axboed6d48192008-01-29 14:04:06 +0100544static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
545 struct request *next)
546{
547 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200548 unsigned int seg_size =
549 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100550
551 /*
552 * First check if the either of the requests are re-queued
553 * requests. Can't merge them if they are.
554 */
Jens Axboee7e24502013-10-29 12:11:47 -0600555 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100556 return 0;
557
Jens Axboe5e7c4272015-09-03 19:28:20 +0300558 if (req_gap_back_merge(req, next->bio))
Keith Busch854fbb92015-02-11 08:20:13 -0700559 return 0;
560
Jens Axboed6d48192008-01-29 14:04:06 +0100561 /*
562 * Will it become too large?
563 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400564 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
565 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100566 return 0;
567
568 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200569 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 Axboed6d48192008-01-29 14:04:06 +0100574 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200575 }
Jens Axboed6d48192008-01-29 14:04:06 +0100576
Martin K. Petersen8a783622010-02-26 00:20:39 -0500577 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100578 return 0;
579
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400580 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200581 return 0;
582
Jens Axboed6d48192008-01-29 14:04:06 +0100583 /* Merge is OK... */
584 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100585 return 1;
586}
587
Tejun Heo80a761f2009-07-03 17:48:17 +0900588/**
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 */
597void 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 Marchand26308ea2009-03-27 10:31:51 +0100618static 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 Marchand09e099d2011-01-05 16:57:38 +0100625 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100626
627 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200628 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100629
Jens Axboe6c23a962011-01-07 08:43:37 +0100630 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100631 part_stat_unlock();
632 }
633}
634
Jens Axboed6d48192008-01-29 14:04:06 +0100635/*
636 * Has to be called with the request spinlock acquired
637 */
638static 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. Petersenf31dc1c2012-09-18 12:19:26 -0400644 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
645 return 0;
646
Jens Axboed6d48192008-01-29 14:04:06 +0100647 /*
648 * not contiguous
649 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900650 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100651 return 0;
652
653 if (rq_data_dir(req) != rq_data_dir(next)
654 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600655 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100656 return 0;
657
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400658 if (req->cmd_flags & REQ_WRITE_SAME &&
659 !blk_write_same_mergeable(req->bio, next->bio))
660 return 0;
661
Jens Axboed6d48192008-01-29 14:04:06 +0100662 /*
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 Heo80a761f2009-07-03 17:48:17 +0900672 * 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 Axboed6d48192008-01-29 14:04:06 +0100685 * 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 Heoa2dec7b2009-05-07 22:24:44 +0900696 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100697
698 elv_merge_requests(q, req, next);
699
Jerome Marchand42dad762009-04-22 14:01:49 +0200700 /*
701 * 'next' is going away, so update stats accordingly
702 */
703 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100704
705 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200706 if (blk_rq_cpu_valid(next))
707 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100708
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100709 /* owner-ship of bio passed from next to req */
710 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100711 __blk_put_request(q, next);
712 return 1;
713}
714
715int 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
725int 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 Axboe5e84ea32011-03-21 10:14:27 +0100734
735int 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 Heo050c8ea2012-02-08 09:19:38 +0100740
741bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
742{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400743 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100744 return false;
745
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400746 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
747 return false;
748
Tejun Heo050c8ea2012-02-08 09:19:38 +0100749 /* 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 Axboee7e24502013-10-29 12:11:47 -0600754 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100755 return false;
756
757 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400758 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100759 return false;
760
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400761 /* 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 Heo050c8ea2012-02-08 09:19:38 +0100766 return true;
767}
768
769int blk_try_merge(struct request *rq, struct bio *bio)
770{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700771 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100772 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700773 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100774 return ELEVATOR_FRONT_MERGE;
775 return ELEVATOR_NO_MERGE;
776}