blob: d9c3a75e4a60a677f3d0aa4c0f1df19fad029ae1 [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;
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 }
113new_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;
124split:
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
138void 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}
156EXPORT_SYMBOL(blk_queue_split);
157
Jens Axboe1e428072009-02-23 09:03:10 +0100158static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +0800159 struct bio *bio,
160 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +0100161{
Kent Overstreet79886132013-11-23 17:19:00 -0800162 struct bio_vec bv, bvprv = { NULL };
Kent Overstreet54efd502015-04-23 22:37:18 -0700163 int cluster, prev = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100164 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +0100165 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -0800166 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +0100167
Jens Axboe1e428072009-02-23 09:03:10 +0100168 if (!bio)
169 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100170
Kent Overstreet5cb88502014-02-07 13:53:46 -0700171 /*
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 Axboe1e428072009-02-23 09:03:10 +0100181 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100182 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200183 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +0200184 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100185 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -0800186 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +0100187 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -0600188 * If SG merging is disabled, each bio vector is
189 * a segment
190 */
191 if (no_sg_merge)
192 goto new_segment;
193
Kent Overstreet54efd502015-04-23 22:37:18 -0700194 if (prev && cluster) {
Kent Overstreet79886132013-11-23 17:19:00 -0800195 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400196 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +0100197 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800198 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100199 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800200 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100201 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100202
Kent Overstreet79886132013-11-23 17:19:00 -0800203 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100204 bvprv = bv;
205 continue;
206 }
Jens Axboed6d48192008-01-29 14:04:06 +0100207new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +0100208 if (nr_phys_segs == 1 && seg_size >
209 fbio->bi_seg_front_size)
210 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200211
Jens Axboe1e428072009-02-23 09:03:10 +0100212 nr_phys_segs++;
213 bvprv = bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700214 prev = 1;
Kent Overstreet79886132013-11-23 17:19:00 -0800215 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100216 }
Jens Axboe59247ea2009-03-06 08:55:24 +0100217 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +0100218 }
219
Jens Axboe59247ea2009-03-06 08:55:24 +0100220 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 Axboe1e428072009-02-23 09:03:10 +0100224
225 return nr_phys_segs;
226}
227
228void blk_recalc_rq_segments(struct request *rq)
229{
Ming Lei07388542014-09-02 23:02:59 +0800230 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 Axboed6d48192008-01-29 14:04:06 +0100235}
236
237void blk_recount_segments(struct request_queue *q, struct bio *bio)
238{
Ming Lei7f60dca2014-11-12 00:15:41 +0800239 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800240
Ming Lei7f60dca2014-11-12 00:15:41 +0800241 /* 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 Axboe05f1dd52014-05-29 09:53:32 -0600250 else {
251 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100252
Jens Axboe05f1dd52014-05-29 09:53:32 -0600253 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800254 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600255 bio->bi_next = nxt;
256 }
257
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600258 bio_set_flag(bio, BIO_SEG_VALID);
Jens Axboed6d48192008-01-29 14:04:06 +0100259}
260EXPORT_SYMBOL(blk_recount_segments);
261
262static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
263 struct bio *nxt)
264{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700265 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700266 struct bvec_iter iter;
267
Martin K. Petersene692cb62010-12-01 19:41:49 +0100268 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100269 return 0;
270
FUJITA Tomonori86771422008-10-13 14:19:05 +0200271 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400272 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100273 return 0;
274
David Woodhousee17fc0a2008-08-09 16:42:20 +0100275 if (!bio_has_data(bio))
276 return 1;
277
Kent Overstreetf619d252013-08-07 14:30:33 -0700278 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 Woodhousee17fc0a2008-08-09 16:42:20 +0100285 return 0;
286
Jens Axboed6d48192008-01-29 14:04:06 +0100287 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100288 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100289 * these two to be merged into one
290 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700291 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100292 return 1;
293
294 return 0;
295}
296
Kent Overstreet79886132013-11-23 17:19:00 -0800297static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200298__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800299 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200300 struct scatterlist **sg, int *nsegs, int *cluster)
301{
302
303 int nbytes = bvec->bv_len;
304
Kent Overstreet79886132013-11-23 17:19:00 -0800305 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200306 if ((*sg)->length + nbytes > queue_max_segment_size(q))
307 goto new_segment;
308
Kent Overstreet79886132013-11-23 17:19:00 -0800309 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200310 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800311 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200312 goto new_segment;
313
314 (*sg)->length += nbytes;
315 } else {
316new_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 Bonzinic8164d82013-03-20 15:37:08 +1030330 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200331 *sg = sg_next(*sg);
332 }
333
334 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
335 (*nsegs)++;
336 }
Kent Overstreet79886132013-11-23 17:19:00 -0800337 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200338}
339
Kent Overstreet5cb88502014-02-07 13:53:46 -0700340static 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) {
367single_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 Axboed6d48192008-01-29 14:04:06 +0100382/*
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 */
386int blk_rq_map_sg(struct request_queue *q, struct request *rq,
387 struct scatterlist *sglist)
388{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700389 struct scatterlist *sg = NULL;
390 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100391
Kent Overstreet5cb88502014-02-07 13:53:46 -0700392 if (rq->bio)
393 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200394
395 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900396 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
397 unsigned int pad_len =
398 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200399
400 sg->length += pad_len;
401 rq->extra_len += pad_len;
402 }
403
Tejun Heo2fb98e82008-02-19 11:36:53 +0100404 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200405 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100406 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
407
Jens Axboed6d48192008-01-29 14:04:06 +0100408 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 Tomonori7a85f882008-03-04 11:17:11 +0100415 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100416 }
417
418 if (sg)
419 sg_mark_end(sg);
420
421 return nsegs;
422}
Jens Axboed6d48192008-01-29 14:04:06 +0100423EXPORT_SYMBOL(blk_rq_map_sg);
424
Jens Axboed6d48192008-01-29 14:04:06 +0100425static inline int ll_new_hw_segment(struct request_queue *q,
426 struct request *req,
427 struct bio *bio)
428{
Jens Axboed6d48192008-01-29 14:04:06 +0100429 int nr_phys_segs = bio_phys_segments(q, bio);
430
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200431 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
432 goto no_merge;
433
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400434 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200435 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100436
437 /*
438 * This will form the start of a new hw segment. Bump both
439 * counters.
440 */
Jens Axboed6d48192008-01-29 14:04:06 +0100441 req->nr_phys_segments += nr_phys_segs;
442 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200443
444no_merge:
445 req->cmd_flags |= REQ_NOMERGE;
446 if (req == q->last_merge)
447 q->last_merge = NULL;
448 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100449}
450
451int ll_back_merge_fn(struct request_queue *q, struct request *req,
452 struct bio *bio)
453{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400454 if (blk_rq_sectors(req) + bio_sectors(bio) >
455 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100456 req->cmd_flags |= REQ_NOMERGE;
457 if (req == q->last_merge)
458 q->last_merge = NULL;
459 return 0;
460 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200461 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100462 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200463 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100464 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100465
466 return ll_new_hw_segment(q, req, bio);
467}
468
Jens Axboe6728cb02008-01-31 13:03:55 +0100469int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100470 struct bio *bio)
471{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400472 if (blk_rq_sectors(req) + bio_sectors(bio) >
473 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100474 req->cmd_flags |= REQ_NOMERGE;
475 if (req == q->last_merge)
476 q->last_merge = NULL;
477 return 0;
478 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200479 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100480 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200481 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100482 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100483
484 return ll_new_hw_segment(q, req, bio);
485}
486
Jens Axboee7e24502013-10-29 12:11:47 -0600487/*
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 */
491static 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 Busch854fbb92015-02-11 08:20:13 -0700498static 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 Axboed6d48192008-01-29 14:04:06 +0100506static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
507 struct request *next)
508{
509 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200510 unsigned int seg_size =
511 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100512
513 /*
514 * First check if the either of the requests are re-queued
515 * requests. Can't merge them if they are.
516 */
Jens Axboee7e24502013-10-29 12:11:47 -0600517 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100518 return 0;
519
Keith Busch854fbb92015-02-11 08:20:13 -0700520 if (test_bit(QUEUE_FLAG_SG_GAPS, &q->queue_flags) &&
521 req_gap_to_prev(req, next))
522 return 0;
523
Jens Axboed6d48192008-01-29 14:04:06 +0100524 /*
525 * Will it become too large?
526 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400527 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
528 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100529 return 0;
530
531 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200532 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 Axboed6d48192008-01-29 14:04:06 +0100537 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200538 }
Jens Axboed6d48192008-01-29 14:04:06 +0100539
Martin K. Petersen8a783622010-02-26 00:20:39 -0500540 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100541 return 0;
542
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400543 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200544 return 0;
545
Jens Axboed6d48192008-01-29 14:04:06 +0100546 /* Merge is OK... */
547 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100548 return 1;
549}
550
Tejun Heo80a761f2009-07-03 17:48:17 +0900551/**
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 */
560void 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 Marchand26308ea2009-03-27 10:31:51 +0100581static 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 Marchand09e099d2011-01-05 16:57:38 +0100588 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100589
590 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200591 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100592
Jens Axboe6c23a962011-01-07 08:43:37 +0100593 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100594 part_stat_unlock();
595 }
596}
597
Jens Axboed6d48192008-01-29 14:04:06 +0100598/*
599 * Has to be called with the request spinlock acquired
600 */
601static 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. Petersenf31dc1c2012-09-18 12:19:26 -0400607 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
608 return 0;
609
Jens Axboed6d48192008-01-29 14:04:06 +0100610 /*
611 * not contiguous
612 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900613 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100614 return 0;
615
616 if (rq_data_dir(req) != rq_data_dir(next)
617 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600618 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100619 return 0;
620
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400621 if (req->cmd_flags & REQ_WRITE_SAME &&
622 !blk_write_same_mergeable(req->bio, next->bio))
623 return 0;
624
Jens Axboed6d48192008-01-29 14:04:06 +0100625 /*
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 Heo80a761f2009-07-03 17:48:17 +0900635 * 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 Axboed6d48192008-01-29 14:04:06 +0100648 * 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 Heoa2dec7b2009-05-07 22:24:44 +0900659 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100660
661 elv_merge_requests(q, req, next);
662
Jerome Marchand42dad762009-04-22 14:01:49 +0200663 /*
664 * 'next' is going away, so update stats accordingly
665 */
666 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100667
668 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200669 if (blk_rq_cpu_valid(next))
670 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100671
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100672 /* owner-ship of bio passed from next to req */
673 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100674 __blk_put_request(q, next);
675 return 1;
676}
677
678int 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
688int 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 Axboe5e84ea32011-03-21 10:14:27 +0100697
698int 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 Heo050c8ea2012-02-08 09:19:38 +0100703
704bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
705{
Jens Axboe66cb45a2014-06-24 16:22:24 -0600706 struct request_queue *q = rq->q;
707
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400708 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100709 return false;
710
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400711 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
712 return false;
713
Tejun Heo050c8ea2012-02-08 09:19:38 +0100714 /* 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 Axboee7e24502013-10-29 12:11:47 -0600719 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100720 return false;
721
722 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400723 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100724 return false;
725
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400726 /* 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 Axboebeefa6b2015-05-29 13:10:23 -0600731 /* Only check gaps if the bio carries data */
732 if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS) && bio_has_data(bio)) {
Jens Axboe66cb45a2014-06-24 16:22:24 -0600733 struct bio_vec *bprev;
734
Wenbo Wang7ee8e4f32015-03-20 01:04:54 -0400735 bprev = &rq->biotail->bi_io_vec[rq->biotail->bi_vcnt - 1];
Jens Axboe66cb45a2014-06-24 16:22:24 -0600736 if (bvec_gap_to_prev(bprev, bio->bi_io_vec[0].bv_offset))
737 return false;
738 }
739
Tejun Heo050c8ea2012-02-08 09:19:38 +0100740 return true;
741}
742
743int blk_try_merge(struct request *rq, struct bio *bio)
744{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700745 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100746 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700747 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100748 return ELEVATOR_FRONT_MERGE;
749 return ELEVATOR_NO_MERGE;
750}