blob: d088cffb810508a5e55f7543bc134537329d00ec [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
10#include "blk.h"
11
Kent Overstreet54efd502015-04-23 22:37:18 -070012static 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
52static 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
65static 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;
Kent Overstreet8ae12662015-04-27 23:48:34 -070072 unsigned seg_size = 0, nsegs = 0, sectors = 0;
Kent Overstreet54efd502015-04-23 22:37:18 -070073 int prev = 0;
74
Kent Overstreet54efd502015-04-23 22:37:18 -070075 bio_for_each_segment(bv, bio, iter) {
Kent Overstreet8ae12662015-04-27 23:48:34 -070076 sectors += bv.bv_len >> 9;
Kent Overstreet54efd502015-04-23 22:37:18 -070077
Kent Overstreet8ae12662015-04-27 23:48:34 -070078 if (sectors > queue_max_sectors(q))
Kent Overstreet54efd502015-04-23 22:37:18 -070079 goto split;
80
81 /*
82 * If the queue doesn't support SG gaps and adding this
83 * offset would create a gap, disallow it.
84 */
Keith Busch03100aa2015-08-19 14:24:05 -070085 if (prev && bvec_gap_to_prev(q, &bvprv, bv.bv_offset))
Kent Overstreet54efd502015-04-23 22:37:18 -070086 goto split;
87
88 if (prev && blk_queue_cluster(q)) {
89 if (seg_size + bv.bv_len > queue_max_segment_size(q))
90 goto new_segment;
91 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
92 goto new_segment;
93 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
94 goto new_segment;
95
96 seg_size += bv.bv_len;
97 bvprv = bv;
98 prev = 1;
99 continue;
100 }
101new_segment:
102 if (nsegs == queue_max_segments(q))
103 goto split;
104
105 nsegs++;
106 bvprv = bv;
107 prev = 1;
108 seg_size = bv.bv_len;
109 }
110
111 return NULL;
112split:
113 split = bio_clone_bioset(bio, GFP_NOIO, bs);
114
115 split->bi_iter.bi_size -= iter.bi_size;
116 bio->bi_iter = iter;
117
118 if (bio_integrity(bio)) {
119 bio_integrity_advance(bio, split->bi_iter.bi_size);
120 bio_integrity_trim(split, 0, bio_sectors(split));
121 }
122
123 return split;
124}
125
126void blk_queue_split(struct request_queue *q, struct bio **bio,
127 struct bio_set *bs)
128{
129 struct bio *split;
130
131 if ((*bio)->bi_rw & REQ_DISCARD)
132 split = blk_bio_discard_split(q, *bio, bs);
133 else if ((*bio)->bi_rw & REQ_WRITE_SAME)
134 split = blk_bio_write_same_split(q, *bio, bs);
135 else
136 split = blk_bio_segment_split(q, *bio, q->bio_split);
137
138 if (split) {
139 bio_chain(split, *bio);
140 generic_make_request(*bio);
141 *bio = split;
142 }
143}
144EXPORT_SYMBOL(blk_queue_split);
145
Jens Axboe1e428072009-02-23 09:03:10 +0100146static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +0800147 struct bio *bio,
148 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +0100149{
Kent Overstreet79886132013-11-23 17:19:00 -0800150 struct bio_vec bv, bvprv = { NULL };
Kent Overstreet54efd502015-04-23 22:37:18 -0700151 int cluster, prev = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100152 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +0100153 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -0800154 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +0100155
Jens Axboe1e428072009-02-23 09:03:10 +0100156 if (!bio)
157 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100158
Kent Overstreet5cb88502014-02-07 13:53:46 -0700159 /*
160 * This should probably be returning 0, but blk_add_request_payload()
161 * (Christoph!!!!)
162 */
163 if (bio->bi_rw & REQ_DISCARD)
164 return 1;
165
166 if (bio->bi_rw & REQ_WRITE_SAME)
167 return 1;
168
Jens Axboe1e428072009-02-23 09:03:10 +0100169 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100170 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200171 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +0200172 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100173 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -0800174 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +0100175 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -0600176 * If SG merging is disabled, each bio vector is
177 * a segment
178 */
179 if (no_sg_merge)
180 goto new_segment;
181
Kent Overstreet54efd502015-04-23 22:37:18 -0700182 if (prev && cluster) {
Kent Overstreet79886132013-11-23 17:19:00 -0800183 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400184 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +0100185 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800186 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100187 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800188 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100189 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100190
Kent Overstreet79886132013-11-23 17:19:00 -0800191 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100192 bvprv = bv;
193 continue;
194 }
Jens Axboed6d48192008-01-29 14:04:06 +0100195new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +0100196 if (nr_phys_segs == 1 && seg_size >
197 fbio->bi_seg_front_size)
198 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200199
Jens Axboe1e428072009-02-23 09:03:10 +0100200 nr_phys_segs++;
201 bvprv = bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700202 prev = 1;
Kent Overstreet79886132013-11-23 17:19:00 -0800203 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100204 }
Jens Axboe59247ea2009-03-06 08:55:24 +0100205 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +0100206 }
207
Jens Axboe59247ea2009-03-06 08:55:24 +0100208 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
209 fbio->bi_seg_front_size = seg_size;
210 if (seg_size > bbio->bi_seg_back_size)
211 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +0100212
213 return nr_phys_segs;
214}
215
216void blk_recalc_rq_segments(struct request *rq)
217{
Ming Lei07388542014-09-02 23:02:59 +0800218 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
219 &rq->q->queue_flags);
220
221 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
222 no_sg_merge);
Jens Axboed6d48192008-01-29 14:04:06 +0100223}
224
225void blk_recount_segments(struct request_queue *q, struct bio *bio)
226{
Ming Lei7f60dca2014-11-12 00:15:41 +0800227 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800228
Ming Lei7f60dca2014-11-12 00:15:41 +0800229 /* estimate segment number by bi_vcnt for non-cloned bio */
230 if (bio_flagged(bio, BIO_CLONED))
231 seg_cnt = bio_segments(bio);
232 else
233 seg_cnt = bio->bi_vcnt;
234
235 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
236 (seg_cnt < queue_max_segments(q)))
237 bio->bi_phys_segments = seg_cnt;
Jens Axboe05f1dd52014-05-29 09:53:32 -0600238 else {
239 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100240
Jens Axboe05f1dd52014-05-29 09:53:32 -0600241 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800242 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600243 bio->bi_next = nxt;
244 }
245
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600246 bio_set_flag(bio, BIO_SEG_VALID);
Jens Axboed6d48192008-01-29 14:04:06 +0100247}
248EXPORT_SYMBOL(blk_recount_segments);
249
250static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
251 struct bio *nxt)
252{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700253 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700254 struct bvec_iter iter;
255
Martin K. Petersene692cb62010-12-01 19:41:49 +0100256 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100257 return 0;
258
FUJITA Tomonori86771422008-10-13 14:19:05 +0200259 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400260 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100261 return 0;
262
David Woodhousee17fc0a2008-08-09 16:42:20 +0100263 if (!bio_has_data(bio))
264 return 1;
265
Kent Overstreetf619d252013-08-07 14:30:33 -0700266 bio_for_each_segment(end_bv, bio, iter)
267 if (end_bv.bv_len == iter.bi_size)
268 break;
269
270 nxt_bv = bio_iovec(nxt);
271
272 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100273 return 0;
274
Jens Axboed6d48192008-01-29 14:04:06 +0100275 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100276 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100277 * these two to be merged into one
278 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700279 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100280 return 1;
281
282 return 0;
283}
284
Kent Overstreet79886132013-11-23 17:19:00 -0800285static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200286__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800287 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200288 struct scatterlist **sg, int *nsegs, int *cluster)
289{
290
291 int nbytes = bvec->bv_len;
292
Kent Overstreet79886132013-11-23 17:19:00 -0800293 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200294 if ((*sg)->length + nbytes > queue_max_segment_size(q))
295 goto new_segment;
296
Kent Overstreet79886132013-11-23 17:19:00 -0800297 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200298 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800299 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200300 goto new_segment;
301
302 (*sg)->length += nbytes;
303 } else {
304new_segment:
305 if (!*sg)
306 *sg = sglist;
307 else {
308 /*
309 * If the driver previously mapped a shorter
310 * list, we could see a termination bit
311 * prematurely unless it fully inits the sg
312 * table on each mapping. We KNOW that there
313 * must be more entries here or the driver
314 * would be buggy, so force clear the
315 * termination bit to avoid doing a full
316 * sg_init_table() in drivers for each command.
317 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030318 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200319 *sg = sg_next(*sg);
320 }
321
322 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
323 (*nsegs)++;
324 }
Kent Overstreet79886132013-11-23 17:19:00 -0800325 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200326}
327
Kent Overstreet5cb88502014-02-07 13:53:46 -0700328static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
329 struct scatterlist *sglist,
330 struct scatterlist **sg)
331{
332 struct bio_vec bvec, bvprv = { NULL };
333 struct bvec_iter iter;
334 int nsegs, cluster;
335
336 nsegs = 0;
337 cluster = blk_queue_cluster(q);
338
339 if (bio->bi_rw & REQ_DISCARD) {
340 /*
341 * This is a hack - drivers should be neither modifying the
342 * biovec, nor relying on bi_vcnt - but because of
343 * blk_add_request_payload(), a discard bio may or may not have
344 * a payload we need to set up here (thank you Christoph) and
345 * bi_vcnt is really the only way of telling if we need to.
346 */
347
348 if (bio->bi_vcnt)
349 goto single_segment;
350
351 return 0;
352 }
353
354 if (bio->bi_rw & REQ_WRITE_SAME) {
355single_segment:
356 *sg = sglist;
357 bvec = bio_iovec(bio);
358 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
359 return 1;
360 }
361
362 for_each_bio(bio)
363 bio_for_each_segment(bvec, bio, iter)
364 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
365 &nsegs, &cluster);
366
367 return nsegs;
368}
369
Jens Axboed6d48192008-01-29 14:04:06 +0100370/*
371 * map a request to scatterlist, return number of sg entries setup. Caller
372 * must make sure sg can hold rq->nr_phys_segments entries
373 */
374int blk_rq_map_sg(struct request_queue *q, struct request *rq,
375 struct scatterlist *sglist)
376{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700377 struct scatterlist *sg = NULL;
378 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100379
Kent Overstreet5cb88502014-02-07 13:53:46 -0700380 if (rq->bio)
381 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200382
383 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900384 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
385 unsigned int pad_len =
386 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200387
388 sg->length += pad_len;
389 rq->extra_len += pad_len;
390 }
391
Tejun Heo2fb98e82008-02-19 11:36:53 +0100392 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200393 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100394 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
395
Dan Williamsda81ed12015-08-07 18:15:14 +0200396 sg_unmark_end(sg);
Jens Axboed6d48192008-01-29 14:04:06 +0100397 sg = sg_next(sg);
398 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
399 q->dma_drain_size,
400 ((unsigned long)q->dma_drain_buffer) &
401 (PAGE_SIZE - 1));
402 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100403 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100404 }
405
406 if (sg)
407 sg_mark_end(sg);
408
409 return nsegs;
410}
Jens Axboed6d48192008-01-29 14:04:06 +0100411EXPORT_SYMBOL(blk_rq_map_sg);
412
Jens Axboed6d48192008-01-29 14:04:06 +0100413static inline int ll_new_hw_segment(struct request_queue *q,
414 struct request *req,
415 struct bio *bio)
416{
Jens Axboed6d48192008-01-29 14:04:06 +0100417 int nr_phys_segs = bio_phys_segments(q, bio);
418
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200419 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
420 goto no_merge;
421
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400422 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200423 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100424
425 /*
426 * This will form the start of a new hw segment. Bump both
427 * counters.
428 */
Jens Axboed6d48192008-01-29 14:04:06 +0100429 req->nr_phys_segments += nr_phys_segs;
430 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200431
432no_merge:
433 req->cmd_flags |= REQ_NOMERGE;
434 if (req == q->last_merge)
435 q->last_merge = NULL;
436 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100437}
438
439int ll_back_merge_fn(struct request_queue *q, struct request *req,
440 struct bio *bio)
441{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400442 if (blk_rq_sectors(req) + bio_sectors(bio) >
443 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100444 req->cmd_flags |= REQ_NOMERGE;
445 if (req == q->last_merge)
446 q->last_merge = NULL;
447 return 0;
448 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200449 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100450 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200451 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100452 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100453
454 return ll_new_hw_segment(q, req, bio);
455}
456
Jens Axboe6728cb02008-01-31 13:03:55 +0100457int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100458 struct bio *bio)
459{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400460 if (blk_rq_sectors(req) + bio_sectors(bio) >
461 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100462 req->cmd_flags |= REQ_NOMERGE;
463 if (req == q->last_merge)
464 q->last_merge = NULL;
465 return 0;
466 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200467 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100468 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200469 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100470 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100471
472 return ll_new_hw_segment(q, req, bio);
473}
474
Jens Axboee7e24502013-10-29 12:11:47 -0600475/*
476 * blk-mq uses req->special to carry normal driver per-request payload, it
477 * does not indicate a prepared command that we cannot merge with.
478 */
479static bool req_no_special_merge(struct request *req)
480{
481 struct request_queue *q = req->q;
482
483 return !q->mq_ops && req->special;
484}
485
Keith Busch03100aa2015-08-19 14:24:05 -0700486static int req_gap_to_prev(struct request *req, struct bio *next)
Keith Busch854fbb92015-02-11 08:20:13 -0700487{
488 struct bio *prev = req->biotail;
489
Keith Busch03100aa2015-08-19 14:24:05 -0700490 return bvec_gap_to_prev(req->q, &prev->bi_io_vec[prev->bi_vcnt - 1],
Keith Busch2ca495a2015-09-01 10:46:44 -0600491 next->bi_io_vec[0].bv_offset);
Keith Busch854fbb92015-02-11 08:20:13 -0700492}
493
Jens Axboed6d48192008-01-29 14:04:06 +0100494static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
495 struct request *next)
496{
497 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200498 unsigned int seg_size =
499 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100500
501 /*
502 * First check if the either of the requests are re-queued
503 * requests. Can't merge them if they are.
504 */
Jens Axboee7e24502013-10-29 12:11:47 -0600505 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100506 return 0;
507
Keith Busch03100aa2015-08-19 14:24:05 -0700508 if (req_gap_to_prev(req, next->bio))
Keith Busch854fbb92015-02-11 08:20:13 -0700509 return 0;
510
Jens Axboed6d48192008-01-29 14:04:06 +0100511 /*
512 * Will it become too large?
513 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400514 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
515 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100516 return 0;
517
518 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200519 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
520 if (req->nr_phys_segments == 1)
521 req->bio->bi_seg_front_size = seg_size;
522 if (next->nr_phys_segments == 1)
523 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100524 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200525 }
Jens Axboed6d48192008-01-29 14:04:06 +0100526
Martin K. Petersen8a783622010-02-26 00:20:39 -0500527 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100528 return 0;
529
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400530 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200531 return 0;
532
Jens Axboed6d48192008-01-29 14:04:06 +0100533 /* Merge is OK... */
534 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100535 return 1;
536}
537
Tejun Heo80a761f2009-07-03 17:48:17 +0900538/**
539 * blk_rq_set_mixed_merge - mark a request as mixed merge
540 * @rq: request to mark as mixed merge
541 *
542 * Description:
543 * @rq is about to be mixed merged. Make sure the attributes
544 * which can be mixed are set in each bio and mark @rq as mixed
545 * merged.
546 */
547void blk_rq_set_mixed_merge(struct request *rq)
548{
549 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
550 struct bio *bio;
551
552 if (rq->cmd_flags & REQ_MIXED_MERGE)
553 return;
554
555 /*
556 * @rq will no longer represent mixable attributes for all the
557 * contained bios. It will just track those of the first one.
558 * Distributes the attributs to each bio.
559 */
560 for (bio = rq->bio; bio; bio = bio->bi_next) {
561 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
562 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
563 bio->bi_rw |= ff;
564 }
565 rq->cmd_flags |= REQ_MIXED_MERGE;
566}
567
Jerome Marchand26308ea2009-03-27 10:31:51 +0100568static void blk_account_io_merge(struct request *req)
569{
570 if (blk_do_io_stat(req)) {
571 struct hd_struct *part;
572 int cpu;
573
574 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100575 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100576
577 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200578 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100579
Jens Axboe6c23a962011-01-07 08:43:37 +0100580 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100581 part_stat_unlock();
582 }
583}
584
Jens Axboed6d48192008-01-29 14:04:06 +0100585/*
586 * Has to be called with the request spinlock acquired
587 */
588static int attempt_merge(struct request_queue *q, struct request *req,
589 struct request *next)
590{
591 if (!rq_mergeable(req) || !rq_mergeable(next))
592 return 0;
593
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400594 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
595 return 0;
596
Jens Axboed6d48192008-01-29 14:04:06 +0100597 /*
598 * not contiguous
599 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900600 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100601 return 0;
602
603 if (rq_data_dir(req) != rq_data_dir(next)
604 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600605 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100606 return 0;
607
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400608 if (req->cmd_flags & REQ_WRITE_SAME &&
609 !blk_write_same_mergeable(req->bio, next->bio))
610 return 0;
611
Jens Axboed6d48192008-01-29 14:04:06 +0100612 /*
613 * If we are allowed to merge, then append bio list
614 * from next to rq and release next. merge_requests_fn
615 * will have updated segment counts, update sector
616 * counts here.
617 */
618 if (!ll_merge_requests_fn(q, req, next))
619 return 0;
620
621 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900622 * If failfast settings disagree or any of the two is already
623 * a mixed merge, mark both as mixed before proceeding. This
624 * makes sure that all involved bios have mixable attributes
625 * set properly.
626 */
627 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
628 (req->cmd_flags & REQ_FAILFAST_MASK) !=
629 (next->cmd_flags & REQ_FAILFAST_MASK)) {
630 blk_rq_set_mixed_merge(req);
631 blk_rq_set_mixed_merge(next);
632 }
633
634 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100635 * At this point we have either done a back merge
636 * or front merge. We need the smaller start_time of
637 * the merged requests to be the current request
638 * for accounting purposes.
639 */
640 if (time_after(req->start_time, next->start_time))
641 req->start_time = next->start_time;
642
643 req->biotail->bi_next = next->bio;
644 req->biotail = next->biotail;
645
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900646 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100647
648 elv_merge_requests(q, req, next);
649
Jerome Marchand42dad762009-04-22 14:01:49 +0200650 /*
651 * 'next' is going away, so update stats accordingly
652 */
653 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100654
655 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200656 if (blk_rq_cpu_valid(next))
657 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100658
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100659 /* owner-ship of bio passed from next to req */
660 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100661 __blk_put_request(q, next);
662 return 1;
663}
664
665int attempt_back_merge(struct request_queue *q, struct request *rq)
666{
667 struct request *next = elv_latter_request(q, rq);
668
669 if (next)
670 return attempt_merge(q, rq, next);
671
672 return 0;
673}
674
675int attempt_front_merge(struct request_queue *q, struct request *rq)
676{
677 struct request *prev = elv_former_request(q, rq);
678
679 if (prev)
680 return attempt_merge(q, prev, rq);
681
682 return 0;
683}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100684
685int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
686 struct request *next)
687{
688 return attempt_merge(q, rq, next);
689}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100690
691bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
692{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400693 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100694 return false;
695
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400696 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
697 return false;
698
Tejun Heo050c8ea2012-02-08 09:19:38 +0100699 /* different data direction or already started, don't merge */
700 if (bio_data_dir(bio) != rq_data_dir(rq))
701 return false;
702
703 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600704 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100705 return false;
706
707 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400708 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100709 return false;
710
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400711 /* must be using the same buffer */
712 if (rq->cmd_flags & REQ_WRITE_SAME &&
713 !blk_write_same_mergeable(rq->bio, bio))
714 return false;
715
Jens Axboebeefa6b2015-05-29 13:10:23 -0600716 /* Only check gaps if the bio carries data */
Keith Busch03100aa2015-08-19 14:24:05 -0700717 if (bio_has_data(bio) && req_gap_to_prev(rq, bio))
718 return false;
Jens Axboe66cb45a2014-06-24 16:22:24 -0600719
Tejun Heo050c8ea2012-02-08 09:19:38 +0100720 return true;
721}
722
723int blk_try_merge(struct request *rq, struct bio *bio)
724{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700725 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100726 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700727 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100728 return ELEVATOR_FRONT_MERGE;
729 return ELEVATOR_NO_MERGE;
730}