blob: e01405a3e8b3f51ce0424a844fadb3304bda5e44 [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,
Ming Leibdced432015-10-20 23:13:52 +080014 struct bio_set *bs,
15 unsigned *nsegs)
Kent Overstreet54efd502015-04-23 22:37:18 -070016{
17 unsigned int max_discard_sectors, granularity;
18 int alignment;
19 sector_t tmp;
20 unsigned split_sectors;
21
Ming Leibdced432015-10-20 23:13:52 +080022 *nsegs = 1;
23
Kent Overstreet54efd502015-04-23 22:37:18 -070024 /* Zero-sector (unknown) and one-sector granularities are the same. */
25 granularity = max(q->limits.discard_granularity >> 9, 1U);
26
27 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
28 max_discard_sectors -= max_discard_sectors % granularity;
29
30 if (unlikely(!max_discard_sectors)) {
31 /* XXX: warn */
32 return NULL;
33 }
34
35 if (bio_sectors(bio) <= max_discard_sectors)
36 return NULL;
37
38 split_sectors = max_discard_sectors;
39
40 /*
41 * If the next starting sector would be misaligned, stop the discard at
42 * the previous aligned sector.
43 */
44 alignment = (q->limits.discard_alignment >> 9) % granularity;
45
46 tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
47 tmp = sector_div(tmp, granularity);
48
49 if (split_sectors > tmp)
50 split_sectors -= tmp;
51
52 return bio_split(bio, split_sectors, GFP_NOIO, bs);
53}
54
55static struct bio *blk_bio_write_same_split(struct request_queue *q,
56 struct bio *bio,
Ming Leibdced432015-10-20 23:13:52 +080057 struct bio_set *bs,
58 unsigned *nsegs)
Kent Overstreet54efd502015-04-23 22:37:18 -070059{
Ming Leibdced432015-10-20 23:13:52 +080060 *nsegs = 1;
61
Kent Overstreet54efd502015-04-23 22:37:18 -070062 if (!q->limits.max_write_same_sectors)
63 return NULL;
64
65 if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
66 return NULL;
67
68 return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
69}
70
71static struct bio *blk_bio_segment_split(struct request_queue *q,
72 struct bio *bio,
Ming Leibdced432015-10-20 23:13:52 +080073 struct bio_set *bs,
74 unsigned *segs)
Kent Overstreet54efd502015-04-23 22:37:18 -070075{
Jens Axboe5014c312015-09-02 16:46:02 -060076 struct bio_vec bv, bvprv, *bvprvp = NULL;
Kent Overstreet54efd502015-04-23 22:37:18 -070077 struct bvec_iter iter;
Kent Overstreet8ae12662015-04-27 23:48:34 -070078 unsigned seg_size = 0, nsegs = 0, sectors = 0;
Ming Lei02e70742015-11-24 10:35:30 +080079 unsigned front_seg_size = bio->bi_seg_front_size;
80 bool do_split = true;
81 struct bio *new = NULL;
Kent Overstreet54efd502015-04-23 22:37:18 -070082
Kent Overstreet54efd502015-04-23 22:37:18 -070083 bio_for_each_segment(bv, bio, iter) {
Jens Axboe6126eb22016-01-08 09:00:29 -070084 if (sectors + (bv.bv_len >> 9) > queue_max_sectors(q))
Kent Overstreet54efd502015-04-23 22:37:18 -070085 goto split;
86
87 /*
88 * If the queue doesn't support SG gaps and adding this
89 * offset would create a gap, disallow it.
90 */
Jens Axboe5014c312015-09-02 16:46:02 -060091 if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
Kent Overstreet54efd502015-04-23 22:37:18 -070092 goto split;
93
Jens Axboe5014c312015-09-02 16:46:02 -060094 if (bvprvp && blk_queue_cluster(q)) {
Kent Overstreet54efd502015-04-23 22:37:18 -070095 if (seg_size + bv.bv_len > queue_max_segment_size(q))
96 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -060097 if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -070098 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -060099 if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -0700100 goto new_segment;
101
102 seg_size += bv.bv_len;
103 bvprv = bv;
Ming Lei578270b2015-11-24 10:35:29 +0800104 bvprvp = &bvprv;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600105 sectors += bv.bv_len >> 9;
Ming Leia88d32a2015-11-30 16:05:49 +0800106
107 if (nsegs == 1 && seg_size > front_seg_size)
108 front_seg_size = seg_size;
Kent Overstreet54efd502015-04-23 22:37:18 -0700109 continue;
110 }
111new_segment:
112 if (nsegs == queue_max_segments(q))
113 goto split;
114
115 nsegs++;
116 bvprv = bv;
Ming Lei578270b2015-11-24 10:35:29 +0800117 bvprvp = &bvprv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700118 seg_size = bv.bv_len;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600119 sectors += bv.bv_len >> 9;
Ming Lei02e70742015-11-24 10:35:30 +0800120
121 if (nsegs == 1 && seg_size > front_seg_size)
122 front_seg_size = seg_size;
Kent Overstreet54efd502015-04-23 22:37:18 -0700123 }
124
Ming Lei02e70742015-11-24 10:35:30 +0800125 do_split = false;
Kent Overstreet54efd502015-04-23 22:37:18 -0700126split:
Ming Leibdced432015-10-20 23:13:52 +0800127 *segs = nsegs;
Ming Lei02e70742015-11-24 10:35:30 +0800128
129 if (do_split) {
130 new = bio_split(bio, sectors, GFP_NOIO, bs);
131 if (new)
132 bio = new;
133 }
134
135 bio->bi_seg_front_size = front_seg_size;
136 if (seg_size > bio->bi_seg_back_size)
137 bio->bi_seg_back_size = seg_size;
138
139 return do_split ? new : NULL;
Kent Overstreet54efd502015-04-23 22:37:18 -0700140}
141
142void blk_queue_split(struct request_queue *q, struct bio **bio,
143 struct bio_set *bs)
144{
Ming Leibdced432015-10-20 23:13:52 +0800145 struct bio *split, *res;
146 unsigned nsegs;
Kent Overstreet54efd502015-04-23 22:37:18 -0700147
148 if ((*bio)->bi_rw & REQ_DISCARD)
Ming Leibdced432015-10-20 23:13:52 +0800149 split = blk_bio_discard_split(q, *bio, bs, &nsegs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700150 else if ((*bio)->bi_rw & REQ_WRITE_SAME)
Ming Leibdced432015-10-20 23:13:52 +0800151 split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700152 else
Ming Leibdced432015-10-20 23:13:52 +0800153 split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs);
154
155 /* physical segments can be figured out during splitting */
156 res = split ? split : *bio;
157 res->bi_phys_segments = nsegs;
158 bio_set_flag(res, BIO_SEG_VALID);
Kent Overstreet54efd502015-04-23 22:37:18 -0700159
160 if (split) {
Ming Lei6ac45ae2015-10-20 23:13:53 +0800161 /* there isn't chance to merge the splitted bio */
162 split->bi_rw |= REQ_NOMERGE;
163
Kent Overstreet54efd502015-04-23 22:37:18 -0700164 bio_chain(split, *bio);
165 generic_make_request(*bio);
166 *bio = split;
167 }
168}
169EXPORT_SYMBOL(blk_queue_split);
170
Jens Axboe1e428072009-02-23 09:03:10 +0100171static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +0800172 struct bio *bio,
173 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +0100174{
Kent Overstreet79886132013-11-23 17:19:00 -0800175 struct bio_vec bv, bvprv = { NULL };
Kent Overstreet54efd502015-04-23 22:37:18 -0700176 int cluster, prev = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100177 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +0100178 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -0800179 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +0100180
Jens Axboe1e428072009-02-23 09:03:10 +0100181 if (!bio)
182 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100183
Kent Overstreet5cb88502014-02-07 13:53:46 -0700184 /*
185 * This should probably be returning 0, but blk_add_request_payload()
186 * (Christoph!!!!)
187 */
188 if (bio->bi_rw & REQ_DISCARD)
189 return 1;
190
191 if (bio->bi_rw & REQ_WRITE_SAME)
192 return 1;
193
Jens Axboe1e428072009-02-23 09:03:10 +0100194 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100195 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200196 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +0200197 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100198 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -0800199 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +0100200 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -0600201 * If SG merging is disabled, each bio vector is
202 * a segment
203 */
204 if (no_sg_merge)
205 goto new_segment;
206
Kent Overstreet54efd502015-04-23 22:37:18 -0700207 if (prev && cluster) {
Kent Overstreet79886132013-11-23 17:19:00 -0800208 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400209 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +0100210 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800211 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100212 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800213 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100214 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100215
Kent Overstreet79886132013-11-23 17:19:00 -0800216 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100217 bvprv = bv;
218 continue;
219 }
Jens Axboed6d48192008-01-29 14:04:06 +0100220new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +0100221 if (nr_phys_segs == 1 && seg_size >
222 fbio->bi_seg_front_size)
223 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200224
Jens Axboe1e428072009-02-23 09:03:10 +0100225 nr_phys_segs++;
226 bvprv = bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700227 prev = 1;
Kent Overstreet79886132013-11-23 17:19:00 -0800228 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100229 }
Jens Axboe59247ea2009-03-06 08:55:24 +0100230 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +0100231 }
232
Jens Axboe59247ea2009-03-06 08:55:24 +0100233 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
234 fbio->bi_seg_front_size = seg_size;
235 if (seg_size > bbio->bi_seg_back_size)
236 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +0100237
238 return nr_phys_segs;
239}
240
241void blk_recalc_rq_segments(struct request *rq)
242{
Ming Lei07388542014-09-02 23:02:59 +0800243 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
244 &rq->q->queue_flags);
245
246 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
247 no_sg_merge);
Jens Axboed6d48192008-01-29 14:04:06 +0100248}
249
250void blk_recount_segments(struct request_queue *q, struct bio *bio)
251{
Ming Lei7f60dca2014-11-12 00:15:41 +0800252 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800253
Ming Lei7f60dca2014-11-12 00:15:41 +0800254 /* estimate segment number by bi_vcnt for non-cloned bio */
255 if (bio_flagged(bio, BIO_CLONED))
256 seg_cnt = bio_segments(bio);
257 else
258 seg_cnt = bio->bi_vcnt;
259
260 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
261 (seg_cnt < queue_max_segments(q)))
262 bio->bi_phys_segments = seg_cnt;
Jens Axboe05f1dd52014-05-29 09:53:32 -0600263 else {
264 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100265
Jens Axboe05f1dd52014-05-29 09:53:32 -0600266 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800267 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600268 bio->bi_next = nxt;
269 }
270
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600271 bio_set_flag(bio, BIO_SEG_VALID);
Jens Axboed6d48192008-01-29 14:04:06 +0100272}
273EXPORT_SYMBOL(blk_recount_segments);
274
275static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
276 struct bio *nxt)
277{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700278 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700279 struct bvec_iter iter;
280
Martin K. Petersene692cb62010-12-01 19:41:49 +0100281 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100282 return 0;
283
FUJITA Tomonori86771422008-10-13 14:19:05 +0200284 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400285 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100286 return 0;
287
David Woodhousee17fc0a2008-08-09 16:42:20 +0100288 if (!bio_has_data(bio))
289 return 1;
290
Kent Overstreetf619d252013-08-07 14:30:33 -0700291 bio_for_each_segment(end_bv, bio, iter)
292 if (end_bv.bv_len == iter.bi_size)
293 break;
294
295 nxt_bv = bio_iovec(nxt);
296
297 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100298 return 0;
299
Jens Axboed6d48192008-01-29 14:04:06 +0100300 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100301 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100302 * these two to be merged into one
303 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700304 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100305 return 1;
306
307 return 0;
308}
309
Kent Overstreet79886132013-11-23 17:19:00 -0800310static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200311__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800312 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200313 struct scatterlist **sg, int *nsegs, int *cluster)
314{
315
316 int nbytes = bvec->bv_len;
317
Kent Overstreet79886132013-11-23 17:19:00 -0800318 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200319 if ((*sg)->length + nbytes > queue_max_segment_size(q))
320 goto new_segment;
321
Kent Overstreet79886132013-11-23 17:19:00 -0800322 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200323 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800324 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200325 goto new_segment;
326
327 (*sg)->length += nbytes;
328 } else {
329new_segment:
330 if (!*sg)
331 *sg = sglist;
332 else {
333 /*
334 * If the driver previously mapped a shorter
335 * list, we could see a termination bit
336 * prematurely unless it fully inits the sg
337 * table on each mapping. We KNOW that there
338 * must be more entries here or the driver
339 * would be buggy, so force clear the
340 * termination bit to avoid doing a full
341 * sg_init_table() in drivers for each command.
342 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030343 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200344 *sg = sg_next(*sg);
345 }
346
347 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
348 (*nsegs)++;
349 }
Kent Overstreet79886132013-11-23 17:19:00 -0800350 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200351}
352
Kent Overstreet5cb88502014-02-07 13:53:46 -0700353static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
354 struct scatterlist *sglist,
355 struct scatterlist **sg)
356{
357 struct bio_vec bvec, bvprv = { NULL };
358 struct bvec_iter iter;
359 int nsegs, cluster;
360
361 nsegs = 0;
362 cluster = blk_queue_cluster(q);
363
364 if (bio->bi_rw & REQ_DISCARD) {
365 /*
366 * This is a hack - drivers should be neither modifying the
367 * biovec, nor relying on bi_vcnt - but because of
368 * blk_add_request_payload(), a discard bio may or may not have
369 * a payload we need to set up here (thank you Christoph) and
370 * bi_vcnt is really the only way of telling if we need to.
371 */
372
373 if (bio->bi_vcnt)
374 goto single_segment;
375
376 return 0;
377 }
378
379 if (bio->bi_rw & REQ_WRITE_SAME) {
380single_segment:
381 *sg = sglist;
382 bvec = bio_iovec(bio);
383 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
384 return 1;
385 }
386
387 for_each_bio(bio)
388 bio_for_each_segment(bvec, bio, iter)
389 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
390 &nsegs, &cluster);
391
392 return nsegs;
393}
394
Jens Axboed6d48192008-01-29 14:04:06 +0100395/*
396 * map a request to scatterlist, return number of sg entries setup. Caller
397 * must make sure sg can hold rq->nr_phys_segments entries
398 */
399int blk_rq_map_sg(struct request_queue *q, struct request *rq,
400 struct scatterlist *sglist)
401{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700402 struct scatterlist *sg = NULL;
403 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100404
Kent Overstreet5cb88502014-02-07 13:53:46 -0700405 if (rq->bio)
406 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200407
408 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900409 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
410 unsigned int pad_len =
411 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200412
413 sg->length += pad_len;
414 rq->extra_len += pad_len;
415 }
416
Tejun Heo2fb98e82008-02-19 11:36:53 +0100417 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200418 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100419 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
420
Dan Williamsda81ed12015-08-07 18:15:14 +0200421 sg_unmark_end(sg);
Jens Axboed6d48192008-01-29 14:04:06 +0100422 sg = sg_next(sg);
423 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
424 q->dma_drain_size,
425 ((unsigned long)q->dma_drain_buffer) &
426 (PAGE_SIZE - 1));
427 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100428 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100429 }
430
431 if (sg)
432 sg_mark_end(sg);
433
Ming Lei12e57f52015-11-24 10:35:31 +0800434 /*
435 * Something must have been wrong if the figured number of
436 * segment is bigger than number of req's physical segments
437 */
438 WARN_ON(nsegs > rq->nr_phys_segments);
439
Jens Axboed6d48192008-01-29 14:04:06 +0100440 return nsegs;
441}
Jens Axboed6d48192008-01-29 14:04:06 +0100442EXPORT_SYMBOL(blk_rq_map_sg);
443
Jens Axboed6d48192008-01-29 14:04:06 +0100444static inline int ll_new_hw_segment(struct request_queue *q,
445 struct request *req,
446 struct bio *bio)
447{
Jens Axboed6d48192008-01-29 14:04:06 +0100448 int nr_phys_segs = bio_phys_segments(q, bio);
449
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200450 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
451 goto no_merge;
452
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400453 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200454 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100455
456 /*
457 * This will form the start of a new hw segment. Bump both
458 * counters.
459 */
Jens Axboed6d48192008-01-29 14:04:06 +0100460 req->nr_phys_segments += nr_phys_segs;
461 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200462
463no_merge:
464 req->cmd_flags |= REQ_NOMERGE;
465 if (req == q->last_merge)
466 q->last_merge = NULL;
467 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100468}
469
470int ll_back_merge_fn(struct request_queue *q, struct request *req,
471 struct bio *bio)
472{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300473 if (req_gap_back_merge(req, bio))
474 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600475 if (blk_integrity_rq(req) &&
476 integrity_req_gap_back_merge(req, bio))
477 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400478 if (blk_rq_sectors(req) + bio_sectors(bio) >
479 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100480 req->cmd_flags |= REQ_NOMERGE;
481 if (req == q->last_merge)
482 q->last_merge = NULL;
483 return 0;
484 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200485 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100486 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200487 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100488 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100489
490 return ll_new_hw_segment(q, req, bio);
491}
492
Jens Axboe6728cb02008-01-31 13:03:55 +0100493int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100494 struct bio *bio)
495{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300496
497 if (req_gap_front_merge(req, bio))
498 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600499 if (blk_integrity_rq(req) &&
500 integrity_req_gap_front_merge(req, bio))
501 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400502 if (blk_rq_sectors(req) + bio_sectors(bio) >
503 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100504 req->cmd_flags |= REQ_NOMERGE;
505 if (req == q->last_merge)
506 q->last_merge = NULL;
507 return 0;
508 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200509 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100510 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200511 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100512 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100513
514 return ll_new_hw_segment(q, req, bio);
515}
516
Jens Axboee7e24502013-10-29 12:11:47 -0600517/*
518 * blk-mq uses req->special to carry normal driver per-request payload, it
519 * does not indicate a prepared command that we cannot merge with.
520 */
521static bool req_no_special_merge(struct request *req)
522{
523 struct request_queue *q = req->q;
524
525 return !q->mq_ops && req->special;
526}
527
Jens Axboed6d48192008-01-29 14:04:06 +0100528static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
529 struct request *next)
530{
531 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200532 unsigned int seg_size =
533 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100534
535 /*
536 * First check if the either of the requests are re-queued
537 * requests. Can't merge them if they are.
538 */
Jens Axboee7e24502013-10-29 12:11:47 -0600539 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100540 return 0;
541
Jens Axboe5e7c4272015-09-03 19:28:20 +0300542 if (req_gap_back_merge(req, next->bio))
Keith Busch854fbb92015-02-11 08:20:13 -0700543 return 0;
544
Jens Axboed6d48192008-01-29 14:04:06 +0100545 /*
546 * Will it become too large?
547 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400548 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
549 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100550 return 0;
551
552 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200553 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
554 if (req->nr_phys_segments == 1)
555 req->bio->bi_seg_front_size = seg_size;
556 if (next->nr_phys_segments == 1)
557 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100558 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200559 }
Jens Axboed6d48192008-01-29 14:04:06 +0100560
Martin K. Petersen8a783622010-02-26 00:20:39 -0500561 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100562 return 0;
563
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400564 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200565 return 0;
566
Jens Axboed6d48192008-01-29 14:04:06 +0100567 /* Merge is OK... */
568 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100569 return 1;
570}
571
Tejun Heo80a761f2009-07-03 17:48:17 +0900572/**
573 * blk_rq_set_mixed_merge - mark a request as mixed merge
574 * @rq: request to mark as mixed merge
575 *
576 * Description:
577 * @rq is about to be mixed merged. Make sure the attributes
578 * which can be mixed are set in each bio and mark @rq as mixed
579 * merged.
580 */
581void blk_rq_set_mixed_merge(struct request *rq)
582{
583 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
584 struct bio *bio;
585
586 if (rq->cmd_flags & REQ_MIXED_MERGE)
587 return;
588
589 /*
590 * @rq will no longer represent mixable attributes for all the
591 * contained bios. It will just track those of the first one.
592 * Distributes the attributs to each bio.
593 */
594 for (bio = rq->bio; bio; bio = bio->bi_next) {
595 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
596 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
597 bio->bi_rw |= ff;
598 }
599 rq->cmd_flags |= REQ_MIXED_MERGE;
600}
601
Jerome Marchand26308ea2009-03-27 10:31:51 +0100602static void blk_account_io_merge(struct request *req)
603{
604 if (blk_do_io_stat(req)) {
605 struct hd_struct *part;
606 int cpu;
607
608 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100609 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100610
611 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200612 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100613
Jens Axboe6c23a962011-01-07 08:43:37 +0100614 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100615 part_stat_unlock();
616 }
617}
618
Jens Axboed6d48192008-01-29 14:04:06 +0100619/*
620 * Has to be called with the request spinlock acquired
621 */
622static int attempt_merge(struct request_queue *q, struct request *req,
623 struct request *next)
624{
625 if (!rq_mergeable(req) || !rq_mergeable(next))
626 return 0;
627
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400628 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
629 return 0;
630
Jens Axboed6d48192008-01-29 14:04:06 +0100631 /*
632 * not contiguous
633 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900634 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100635 return 0;
636
637 if (rq_data_dir(req) != rq_data_dir(next)
638 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600639 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100640 return 0;
641
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400642 if (req->cmd_flags & REQ_WRITE_SAME &&
643 !blk_write_same_mergeable(req->bio, next->bio))
644 return 0;
645
Jens Axboed6d48192008-01-29 14:04:06 +0100646 /*
647 * If we are allowed to merge, then append bio list
648 * from next to rq and release next. merge_requests_fn
649 * will have updated segment counts, update sector
650 * counts here.
651 */
652 if (!ll_merge_requests_fn(q, req, next))
653 return 0;
654
655 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900656 * If failfast settings disagree or any of the two is already
657 * a mixed merge, mark both as mixed before proceeding. This
658 * makes sure that all involved bios have mixable attributes
659 * set properly.
660 */
661 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
662 (req->cmd_flags & REQ_FAILFAST_MASK) !=
663 (next->cmd_flags & REQ_FAILFAST_MASK)) {
664 blk_rq_set_mixed_merge(req);
665 blk_rq_set_mixed_merge(next);
666 }
667
668 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100669 * At this point we have either done a back merge
670 * or front merge. We need the smaller start_time of
671 * the merged requests to be the current request
672 * for accounting purposes.
673 */
674 if (time_after(req->start_time, next->start_time))
675 req->start_time = next->start_time;
676
677 req->biotail->bi_next = next->bio;
678 req->biotail = next->biotail;
679
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900680 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100681
682 elv_merge_requests(q, req, next);
683
Jerome Marchand42dad762009-04-22 14:01:49 +0200684 /*
685 * 'next' is going away, so update stats accordingly
686 */
687 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100688
689 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200690 if (blk_rq_cpu_valid(next))
691 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100692
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100693 /* owner-ship of bio passed from next to req */
694 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100695 __blk_put_request(q, next);
696 return 1;
697}
698
699int attempt_back_merge(struct request_queue *q, struct request *rq)
700{
701 struct request *next = elv_latter_request(q, rq);
702
703 if (next)
704 return attempt_merge(q, rq, next);
705
706 return 0;
707}
708
709int attempt_front_merge(struct request_queue *q, struct request *rq)
710{
711 struct request *prev = elv_former_request(q, rq);
712
713 if (prev)
714 return attempt_merge(q, prev, rq);
715
716 return 0;
717}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100718
719int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
720 struct request *next)
721{
722 return attempt_merge(q, rq, next);
723}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100724
725bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
726{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400727 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100728 return false;
729
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400730 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
731 return false;
732
Tejun Heo050c8ea2012-02-08 09:19:38 +0100733 /* different data direction or already started, don't merge */
734 if (bio_data_dir(bio) != rq_data_dir(rq))
735 return false;
736
737 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600738 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100739 return false;
740
741 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400742 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100743 return false;
744
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400745 /* must be using the same buffer */
746 if (rq->cmd_flags & REQ_WRITE_SAME &&
747 !blk_write_same_mergeable(rq->bio, bio))
748 return false;
749
Tejun Heo050c8ea2012-02-08 09:19:38 +0100750 return true;
751}
752
753int blk_try_merge(struct request *rq, struct bio *bio)
754{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700755 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100756 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700757 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100758 return ELEVATOR_FRONT_MERGE;
759 return ELEVATOR_NO_MERGE;
760}