blob: c4e9c37f3e38122e5125502d62ab1ddd2e887408 [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{
Jens Axboe5014c312015-09-02 16:46:02 -060069 struct bio_vec bv, bvprv, *bvprvp = NULL;
Kent Overstreet54efd502015-04-23 22:37:18 -070070 struct bvec_iter iter;
Kent Overstreet8ae12662015-04-27 23:48:34 -070071 unsigned seg_size = 0, nsegs = 0, sectors = 0;
Kent Overstreet54efd502015-04-23 22:37:18 -070072
Kent Overstreet54efd502015-04-23 22:37:18 -070073 bio_for_each_segment(bv, bio, iter) {
Ming Lei52cc6ee2015-09-17 09:58:38 -060074 if (sectors + (bv.bv_len >> 9) > queue_max_sectors(q))
Kent Overstreet54efd502015-04-23 22:37:18 -070075 goto split;
76
77 /*
78 * If the queue doesn't support SG gaps and adding this
79 * offset would create a gap, disallow it.
80 */
Jens Axboe5014c312015-09-02 16:46:02 -060081 if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
Kent Overstreet54efd502015-04-23 22:37:18 -070082 goto split;
83
Jens Axboe5014c312015-09-02 16:46:02 -060084 if (bvprvp && blk_queue_cluster(q)) {
Kent Overstreet54efd502015-04-23 22:37:18 -070085 if (seg_size + bv.bv_len > queue_max_segment_size(q))
86 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -060087 if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -070088 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -060089 if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -070090 goto new_segment;
91
92 seg_size += bv.bv_len;
93 bvprv = bv;
Jens Axboe5014c312015-09-02 16:46:02 -060094 bvprvp = &bv;
Ming Lei52cc6ee2015-09-17 09:58:38 -060095 sectors += bv.bv_len >> 9;
Kent Overstreet54efd502015-04-23 22:37:18 -070096 continue;
97 }
98new_segment:
99 if (nsegs == queue_max_segments(q))
100 goto split;
101
102 nsegs++;
103 bvprv = bv;
Jens Axboe5014c312015-09-02 16:46:02 -0600104 bvprvp = &bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700105 seg_size = bv.bv_len;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600106 sectors += bv.bv_len >> 9;
Kent Overstreet54efd502015-04-23 22:37:18 -0700107 }
108
109 return NULL;
110split:
Ming Lei52cc6ee2015-09-17 09:58:38 -0600111 return bio_split(bio, sectors, GFP_NOIO, bs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700112}
113
114void blk_queue_split(struct request_queue *q, struct bio **bio,
115 struct bio_set *bs)
116{
117 struct bio *split;
118
119 if ((*bio)->bi_rw & REQ_DISCARD)
120 split = blk_bio_discard_split(q, *bio, bs);
121 else if ((*bio)->bi_rw & REQ_WRITE_SAME)
122 split = blk_bio_write_same_split(q, *bio, bs);
123 else
124 split = blk_bio_segment_split(q, *bio, q->bio_split);
125
126 if (split) {
127 bio_chain(split, *bio);
128 generic_make_request(*bio);
129 *bio = split;
130 }
131}
132EXPORT_SYMBOL(blk_queue_split);
133
Jens Axboe1e428072009-02-23 09:03:10 +0100134static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +0800135 struct bio *bio,
136 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +0100137{
Kent Overstreet79886132013-11-23 17:19:00 -0800138 struct bio_vec bv, bvprv = { NULL };
Kent Overstreet54efd502015-04-23 22:37:18 -0700139 int cluster, prev = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100140 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +0100141 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -0800142 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +0100143
Jens Axboe1e428072009-02-23 09:03:10 +0100144 if (!bio)
145 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100146
Kent Overstreet5cb88502014-02-07 13:53:46 -0700147 /*
148 * This should probably be returning 0, but blk_add_request_payload()
149 * (Christoph!!!!)
150 */
151 if (bio->bi_rw & REQ_DISCARD)
152 return 1;
153
154 if (bio->bi_rw & REQ_WRITE_SAME)
155 return 1;
156
Jens Axboe1e428072009-02-23 09:03:10 +0100157 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100158 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200159 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +0200160 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100161 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -0800162 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +0100163 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -0600164 * If SG merging is disabled, each bio vector is
165 * a segment
166 */
167 if (no_sg_merge)
168 goto new_segment;
169
Kent Overstreet54efd502015-04-23 22:37:18 -0700170 if (prev && cluster) {
Kent Overstreet79886132013-11-23 17:19:00 -0800171 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400172 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +0100173 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800174 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100175 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800176 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100177 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100178
Kent Overstreet79886132013-11-23 17:19:00 -0800179 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100180 bvprv = bv;
181 continue;
182 }
Jens Axboed6d48192008-01-29 14:04:06 +0100183new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +0100184 if (nr_phys_segs == 1 && seg_size >
185 fbio->bi_seg_front_size)
186 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200187
Jens Axboe1e428072009-02-23 09:03:10 +0100188 nr_phys_segs++;
189 bvprv = bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700190 prev = 1;
Kent Overstreet79886132013-11-23 17:19:00 -0800191 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100192 }
Jens Axboe59247ea2009-03-06 08:55:24 +0100193 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +0100194 }
195
Jens Axboe59247ea2009-03-06 08:55:24 +0100196 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
197 fbio->bi_seg_front_size = seg_size;
198 if (seg_size > bbio->bi_seg_back_size)
199 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +0100200
201 return nr_phys_segs;
202}
203
204void blk_recalc_rq_segments(struct request *rq)
205{
Ming Lei07388542014-09-02 23:02:59 +0800206 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
207 &rq->q->queue_flags);
208
209 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
210 no_sg_merge);
Jens Axboed6d48192008-01-29 14:04:06 +0100211}
212
213void blk_recount_segments(struct request_queue *q, struct bio *bio)
214{
Ming Lei7f60dca2014-11-12 00:15:41 +0800215 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800216
Ming Lei7f60dca2014-11-12 00:15:41 +0800217 /* estimate segment number by bi_vcnt for non-cloned bio */
218 if (bio_flagged(bio, BIO_CLONED))
219 seg_cnt = bio_segments(bio);
220 else
221 seg_cnt = bio->bi_vcnt;
222
223 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
224 (seg_cnt < queue_max_segments(q)))
225 bio->bi_phys_segments = seg_cnt;
Jens Axboe05f1dd52014-05-29 09:53:32 -0600226 else {
227 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100228
Jens Axboe05f1dd52014-05-29 09:53:32 -0600229 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800230 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600231 bio->bi_next = nxt;
232 }
233
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600234 bio_set_flag(bio, BIO_SEG_VALID);
Jens Axboed6d48192008-01-29 14:04:06 +0100235}
236EXPORT_SYMBOL(blk_recount_segments);
237
238static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
239 struct bio *nxt)
240{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700241 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700242 struct bvec_iter iter;
243
Martin K. Petersene692cb62010-12-01 19:41:49 +0100244 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100245 return 0;
246
FUJITA Tomonori86771422008-10-13 14:19:05 +0200247 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400248 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100249 return 0;
250
David Woodhousee17fc0a2008-08-09 16:42:20 +0100251 if (!bio_has_data(bio))
252 return 1;
253
Kent Overstreetf619d252013-08-07 14:30:33 -0700254 bio_for_each_segment(end_bv, bio, iter)
255 if (end_bv.bv_len == iter.bi_size)
256 break;
257
258 nxt_bv = bio_iovec(nxt);
259
260 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100261 return 0;
262
Jens Axboed6d48192008-01-29 14:04:06 +0100263 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100264 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100265 * these two to be merged into one
266 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700267 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100268 return 1;
269
270 return 0;
271}
272
Kent Overstreet79886132013-11-23 17:19:00 -0800273static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200274__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800275 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200276 struct scatterlist **sg, int *nsegs, int *cluster)
277{
278
279 int nbytes = bvec->bv_len;
280
Kent Overstreet79886132013-11-23 17:19:00 -0800281 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200282 if ((*sg)->length + nbytes > queue_max_segment_size(q))
283 goto new_segment;
284
Kent Overstreet79886132013-11-23 17:19:00 -0800285 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200286 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800287 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200288 goto new_segment;
289
290 (*sg)->length += nbytes;
291 } else {
292new_segment:
293 if (!*sg)
294 *sg = sglist;
295 else {
296 /*
297 * If the driver previously mapped a shorter
298 * list, we could see a termination bit
299 * prematurely unless it fully inits the sg
300 * table on each mapping. We KNOW that there
301 * must be more entries here or the driver
302 * would be buggy, so force clear the
303 * termination bit to avoid doing a full
304 * sg_init_table() in drivers for each command.
305 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030306 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200307 *sg = sg_next(*sg);
308 }
309
310 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
311 (*nsegs)++;
312 }
Kent Overstreet79886132013-11-23 17:19:00 -0800313 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200314}
315
Kent Overstreet5cb88502014-02-07 13:53:46 -0700316static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
317 struct scatterlist *sglist,
318 struct scatterlist **sg)
319{
320 struct bio_vec bvec, bvprv = { NULL };
321 struct bvec_iter iter;
322 int nsegs, cluster;
323
324 nsegs = 0;
325 cluster = blk_queue_cluster(q);
326
327 if (bio->bi_rw & REQ_DISCARD) {
328 /*
329 * This is a hack - drivers should be neither modifying the
330 * biovec, nor relying on bi_vcnt - but because of
331 * blk_add_request_payload(), a discard bio may or may not have
332 * a payload we need to set up here (thank you Christoph) and
333 * bi_vcnt is really the only way of telling if we need to.
334 */
335
336 if (bio->bi_vcnt)
337 goto single_segment;
338
339 return 0;
340 }
341
342 if (bio->bi_rw & REQ_WRITE_SAME) {
343single_segment:
344 *sg = sglist;
345 bvec = bio_iovec(bio);
346 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
347 return 1;
348 }
349
350 for_each_bio(bio)
351 bio_for_each_segment(bvec, bio, iter)
352 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
353 &nsegs, &cluster);
354
355 return nsegs;
356}
357
Jens Axboed6d48192008-01-29 14:04:06 +0100358/*
359 * map a request to scatterlist, return number of sg entries setup. Caller
360 * must make sure sg can hold rq->nr_phys_segments entries
361 */
362int blk_rq_map_sg(struct request_queue *q, struct request *rq,
363 struct scatterlist *sglist)
364{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700365 struct scatterlist *sg = NULL;
366 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100367
Kent Overstreet5cb88502014-02-07 13:53:46 -0700368 if (rq->bio)
369 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200370
371 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900372 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
373 unsigned int pad_len =
374 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200375
376 sg->length += pad_len;
377 rq->extra_len += pad_len;
378 }
379
Tejun Heo2fb98e82008-02-19 11:36:53 +0100380 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200381 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100382 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
383
Dan Williamsda81ed12015-08-07 18:15:14 +0200384 sg_unmark_end(sg);
Jens Axboed6d48192008-01-29 14:04:06 +0100385 sg = sg_next(sg);
386 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
387 q->dma_drain_size,
388 ((unsigned long)q->dma_drain_buffer) &
389 (PAGE_SIZE - 1));
390 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100391 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100392 }
393
394 if (sg)
395 sg_mark_end(sg);
396
397 return nsegs;
398}
Jens Axboed6d48192008-01-29 14:04:06 +0100399EXPORT_SYMBOL(blk_rq_map_sg);
400
Jens Axboed6d48192008-01-29 14:04:06 +0100401static inline int ll_new_hw_segment(struct request_queue *q,
402 struct request *req,
403 struct bio *bio)
404{
Jens Axboed6d48192008-01-29 14:04:06 +0100405 int nr_phys_segs = bio_phys_segments(q, bio);
406
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200407 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
408 goto no_merge;
409
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400410 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200411 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100412
413 /*
414 * This will form the start of a new hw segment. Bump both
415 * counters.
416 */
Jens Axboed6d48192008-01-29 14:04:06 +0100417 req->nr_phys_segments += nr_phys_segs;
418 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200419
420no_merge:
421 req->cmd_flags |= REQ_NOMERGE;
422 if (req == q->last_merge)
423 q->last_merge = NULL;
424 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100425}
426
427int ll_back_merge_fn(struct request_queue *q, struct request *req,
428 struct bio *bio)
429{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300430 if (req_gap_back_merge(req, bio))
431 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600432 if (blk_integrity_rq(req) &&
433 integrity_req_gap_back_merge(req, bio))
434 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400435 if (blk_rq_sectors(req) + bio_sectors(bio) >
436 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100437 req->cmd_flags |= REQ_NOMERGE;
438 if (req == q->last_merge)
439 q->last_merge = NULL;
440 return 0;
441 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200442 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100443 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200444 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100445 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100446
447 return ll_new_hw_segment(q, req, bio);
448}
449
Jens Axboe6728cb02008-01-31 13:03:55 +0100450int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100451 struct bio *bio)
452{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300453
454 if (req_gap_front_merge(req, bio))
455 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600456 if (blk_integrity_rq(req) &&
457 integrity_req_gap_front_merge(req, bio))
458 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400459 if (blk_rq_sectors(req) + bio_sectors(bio) >
460 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100461 req->cmd_flags |= REQ_NOMERGE;
462 if (req == q->last_merge)
463 q->last_merge = NULL;
464 return 0;
465 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200466 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100467 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200468 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100469 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100470
471 return ll_new_hw_segment(q, req, bio);
472}
473
Jens Axboee7e24502013-10-29 12:11:47 -0600474/*
475 * blk-mq uses req->special to carry normal driver per-request payload, it
476 * does not indicate a prepared command that we cannot merge with.
477 */
478static bool req_no_special_merge(struct request *req)
479{
480 struct request_queue *q = req->q;
481
482 return !q->mq_ops && req->special;
483}
484
Jens Axboed6d48192008-01-29 14:04:06 +0100485static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
486 struct request *next)
487{
488 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200489 unsigned int seg_size =
490 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100491
492 /*
493 * First check if the either of the requests are re-queued
494 * requests. Can't merge them if they are.
495 */
Jens Axboee7e24502013-10-29 12:11:47 -0600496 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100497 return 0;
498
Jens Axboe5e7c4272015-09-03 19:28:20 +0300499 if (req_gap_back_merge(req, next->bio))
Keith Busch854fbb92015-02-11 08:20:13 -0700500 return 0;
501
Jens Axboed6d48192008-01-29 14:04:06 +0100502 /*
503 * Will it become too large?
504 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400505 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
506 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100507 return 0;
508
509 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200510 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
511 if (req->nr_phys_segments == 1)
512 req->bio->bi_seg_front_size = seg_size;
513 if (next->nr_phys_segments == 1)
514 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100515 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200516 }
Jens Axboed6d48192008-01-29 14:04:06 +0100517
Martin K. Petersen8a783622010-02-26 00:20:39 -0500518 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100519 return 0;
520
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400521 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200522 return 0;
523
Jens Axboed6d48192008-01-29 14:04:06 +0100524 /* Merge is OK... */
525 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100526 return 1;
527}
528
Tejun Heo80a761f2009-07-03 17:48:17 +0900529/**
530 * blk_rq_set_mixed_merge - mark a request as mixed merge
531 * @rq: request to mark as mixed merge
532 *
533 * Description:
534 * @rq is about to be mixed merged. Make sure the attributes
535 * which can be mixed are set in each bio and mark @rq as mixed
536 * merged.
537 */
538void blk_rq_set_mixed_merge(struct request *rq)
539{
540 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
541 struct bio *bio;
542
543 if (rq->cmd_flags & REQ_MIXED_MERGE)
544 return;
545
546 /*
547 * @rq will no longer represent mixable attributes for all the
548 * contained bios. It will just track those of the first one.
549 * Distributes the attributs to each bio.
550 */
551 for (bio = rq->bio; bio; bio = bio->bi_next) {
552 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
553 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
554 bio->bi_rw |= ff;
555 }
556 rq->cmd_flags |= REQ_MIXED_MERGE;
557}
558
Jerome Marchand26308ea2009-03-27 10:31:51 +0100559static void blk_account_io_merge(struct request *req)
560{
561 if (blk_do_io_stat(req)) {
562 struct hd_struct *part;
563 int cpu;
564
565 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100566 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100567
568 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200569 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100570
Jens Axboe6c23a962011-01-07 08:43:37 +0100571 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100572 part_stat_unlock();
573 }
574}
575
Jens Axboed6d48192008-01-29 14:04:06 +0100576/*
577 * Has to be called with the request spinlock acquired
578 */
579static int attempt_merge(struct request_queue *q, struct request *req,
580 struct request *next)
581{
582 if (!rq_mergeable(req) || !rq_mergeable(next))
583 return 0;
584
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400585 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
586 return 0;
587
Jens Axboed6d48192008-01-29 14:04:06 +0100588 /*
589 * not contiguous
590 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900591 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100592 return 0;
593
594 if (rq_data_dir(req) != rq_data_dir(next)
595 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600596 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100597 return 0;
598
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400599 if (req->cmd_flags & REQ_WRITE_SAME &&
600 !blk_write_same_mergeable(req->bio, next->bio))
601 return 0;
602
Jens Axboed6d48192008-01-29 14:04:06 +0100603 /*
604 * If we are allowed to merge, then append bio list
605 * from next to rq and release next. merge_requests_fn
606 * will have updated segment counts, update sector
607 * counts here.
608 */
609 if (!ll_merge_requests_fn(q, req, next))
610 return 0;
611
612 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900613 * If failfast settings disagree or any of the two is already
614 * a mixed merge, mark both as mixed before proceeding. This
615 * makes sure that all involved bios have mixable attributes
616 * set properly.
617 */
618 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
619 (req->cmd_flags & REQ_FAILFAST_MASK) !=
620 (next->cmd_flags & REQ_FAILFAST_MASK)) {
621 blk_rq_set_mixed_merge(req);
622 blk_rq_set_mixed_merge(next);
623 }
624
625 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100626 * At this point we have either done a back merge
627 * or front merge. We need the smaller start_time of
628 * the merged requests to be the current request
629 * for accounting purposes.
630 */
631 if (time_after(req->start_time, next->start_time))
632 req->start_time = next->start_time;
633
634 req->biotail->bi_next = next->bio;
635 req->biotail = next->biotail;
636
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900637 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100638
639 elv_merge_requests(q, req, next);
640
Jerome Marchand42dad762009-04-22 14:01:49 +0200641 /*
642 * 'next' is going away, so update stats accordingly
643 */
644 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100645
646 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200647 if (blk_rq_cpu_valid(next))
648 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100649
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100650 /* owner-ship of bio passed from next to req */
651 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100652 __blk_put_request(q, next);
653 return 1;
654}
655
656int attempt_back_merge(struct request_queue *q, struct request *rq)
657{
658 struct request *next = elv_latter_request(q, rq);
659
660 if (next)
661 return attempt_merge(q, rq, next);
662
663 return 0;
664}
665
666int attempt_front_merge(struct request_queue *q, struct request *rq)
667{
668 struct request *prev = elv_former_request(q, rq);
669
670 if (prev)
671 return attempt_merge(q, prev, rq);
672
673 return 0;
674}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100675
676int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
677 struct request *next)
678{
679 return attempt_merge(q, rq, next);
680}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100681
682bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
683{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400684 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100685 return false;
686
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400687 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
688 return false;
689
Tejun Heo050c8ea2012-02-08 09:19:38 +0100690 /* different data direction or already started, don't merge */
691 if (bio_data_dir(bio) != rq_data_dir(rq))
692 return false;
693
694 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600695 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100696 return false;
697
698 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400699 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100700 return false;
701
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400702 /* must be using the same buffer */
703 if (rq->cmd_flags & REQ_WRITE_SAME &&
704 !blk_write_same_mergeable(rq->bio, bio))
705 return false;
706
Tejun Heo050c8ea2012-02-08 09:19:38 +0100707 return true;
708}
709
710int blk_try_merge(struct request *rq, struct bio *bio)
711{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700712 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100713 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700714 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100715 return ELEVATOR_FRONT_MERGE;
716 return ELEVATOR_NO_MERGE;
717}