blob: de5716d8e525969e7849767a775aabec9e4d8b96 [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;
Kent Overstreet54efd502015-04-23 22:37:18 -070079
Kent Overstreet54efd502015-04-23 22:37:18 -070080 bio_for_each_segment(bv, bio, iter) {
Ming Lei52cc6ee2015-09-17 09:58:38 -060081 if (sectors + (bv.bv_len >> 9) > queue_max_sectors(q))
Kent Overstreet54efd502015-04-23 22:37:18 -070082 goto split;
83
84 /*
85 * If the queue doesn't support SG gaps and adding this
86 * offset would create a gap, disallow it.
87 */
Jens Axboe5014c312015-09-02 16:46:02 -060088 if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
Kent Overstreet54efd502015-04-23 22:37:18 -070089 goto split;
90
Jens Axboe5014c312015-09-02 16:46:02 -060091 if (bvprvp && blk_queue_cluster(q)) {
Kent Overstreet54efd502015-04-23 22:37:18 -070092 if (seg_size + bv.bv_len > queue_max_segment_size(q))
93 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -060094 if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -070095 goto new_segment;
Jens Axboe5014c312015-09-02 16:46:02 -060096 if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
Kent Overstreet54efd502015-04-23 22:37:18 -070097 goto new_segment;
98
99 seg_size += bv.bv_len;
100 bvprv = bv;
Jens Axboe5014c312015-09-02 16:46:02 -0600101 bvprvp = &bv;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600102 sectors += bv.bv_len >> 9;
Kent Overstreet54efd502015-04-23 22:37:18 -0700103 continue;
104 }
105new_segment:
106 if (nsegs == queue_max_segments(q))
107 goto split;
108
109 nsegs++;
110 bvprv = bv;
Jens Axboe5014c312015-09-02 16:46:02 -0600111 bvprvp = &bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700112 seg_size = bv.bv_len;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600113 sectors += bv.bv_len >> 9;
Kent Overstreet54efd502015-04-23 22:37:18 -0700114 }
115
Ming Leibdced432015-10-20 23:13:52 +0800116 *segs = nsegs;
Kent Overstreet54efd502015-04-23 22:37:18 -0700117 return NULL;
118split:
Ming Leibdced432015-10-20 23:13:52 +0800119 *segs = nsegs;
Ming Lei52cc6ee2015-09-17 09:58:38 -0600120 return bio_split(bio, sectors, GFP_NOIO, bs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700121}
122
123void blk_queue_split(struct request_queue *q, struct bio **bio,
124 struct bio_set *bs)
125{
Ming Leibdced432015-10-20 23:13:52 +0800126 struct bio *split, *res;
127 unsigned nsegs;
Kent Overstreet54efd502015-04-23 22:37:18 -0700128
129 if ((*bio)->bi_rw & REQ_DISCARD)
Ming Leibdced432015-10-20 23:13:52 +0800130 split = blk_bio_discard_split(q, *bio, bs, &nsegs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700131 else if ((*bio)->bi_rw & REQ_WRITE_SAME)
Ming Leibdced432015-10-20 23:13:52 +0800132 split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
Kent Overstreet54efd502015-04-23 22:37:18 -0700133 else
Ming Leibdced432015-10-20 23:13:52 +0800134 split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs);
135
136 /* physical segments can be figured out during splitting */
137 res = split ? split : *bio;
138 res->bi_phys_segments = nsegs;
139 bio_set_flag(res, BIO_SEG_VALID);
Kent Overstreet54efd502015-04-23 22:37:18 -0700140
141 if (split) {
Ming Lei6ac45ae2015-10-20 23:13:53 +0800142 /* there isn't chance to merge the splitted bio */
143 split->bi_rw |= REQ_NOMERGE;
144
Kent Overstreet54efd502015-04-23 22:37:18 -0700145 bio_chain(split, *bio);
146 generic_make_request(*bio);
147 *bio = split;
148 }
149}
150EXPORT_SYMBOL(blk_queue_split);
151
Jens Axboe1e428072009-02-23 09:03:10 +0100152static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +0800153 struct bio *bio,
154 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +0100155{
Kent Overstreet79886132013-11-23 17:19:00 -0800156 struct bio_vec bv, bvprv = { NULL };
Kent Overstreet54efd502015-04-23 22:37:18 -0700157 int cluster, prev = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100158 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +0100159 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -0800160 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +0100161
Jens Axboe1e428072009-02-23 09:03:10 +0100162 if (!bio)
163 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100164
Kent Overstreet5cb88502014-02-07 13:53:46 -0700165 /*
166 * This should probably be returning 0, but blk_add_request_payload()
167 * (Christoph!!!!)
168 */
169 if (bio->bi_rw & REQ_DISCARD)
170 return 1;
171
172 if (bio->bi_rw & REQ_WRITE_SAME)
173 return 1;
174
Jens Axboe1e428072009-02-23 09:03:10 +0100175 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100176 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200177 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +0200178 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +0100179 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -0800180 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +0100181 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -0600182 * If SG merging is disabled, each bio vector is
183 * a segment
184 */
185 if (no_sg_merge)
186 goto new_segment;
187
Kent Overstreet54efd502015-04-23 22:37:18 -0700188 if (prev && cluster) {
Kent Overstreet79886132013-11-23 17:19:00 -0800189 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400190 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +0100191 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800192 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100193 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800194 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +0100195 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100196
Kent Overstreet79886132013-11-23 17:19:00 -0800197 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100198 bvprv = bv;
199 continue;
200 }
Jens Axboed6d48192008-01-29 14:04:06 +0100201new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +0100202 if (nr_phys_segs == 1 && seg_size >
203 fbio->bi_seg_front_size)
204 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200205
Jens Axboe1e428072009-02-23 09:03:10 +0100206 nr_phys_segs++;
207 bvprv = bv;
Kent Overstreet54efd502015-04-23 22:37:18 -0700208 prev = 1;
Kent Overstreet79886132013-11-23 17:19:00 -0800209 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +0100210 }
Jens Axboe59247ea2009-03-06 08:55:24 +0100211 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +0100212 }
213
Jens Axboe59247ea2009-03-06 08:55:24 +0100214 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
215 fbio->bi_seg_front_size = seg_size;
216 if (seg_size > bbio->bi_seg_back_size)
217 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +0100218
219 return nr_phys_segs;
220}
221
222void blk_recalc_rq_segments(struct request *rq)
223{
Ming Lei07388542014-09-02 23:02:59 +0800224 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
225 &rq->q->queue_flags);
226
227 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
228 no_sg_merge);
Jens Axboed6d48192008-01-29 14:04:06 +0100229}
230
231void blk_recount_segments(struct request_queue *q, struct bio *bio)
232{
Ming Lei7f60dca2014-11-12 00:15:41 +0800233 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800234
Ming Lei7f60dca2014-11-12 00:15:41 +0800235 /* estimate segment number by bi_vcnt for non-cloned bio */
236 if (bio_flagged(bio, BIO_CLONED))
237 seg_cnt = bio_segments(bio);
238 else
239 seg_cnt = bio->bi_vcnt;
240
241 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
242 (seg_cnt < queue_max_segments(q)))
243 bio->bi_phys_segments = seg_cnt;
Jens Axboe05f1dd52014-05-29 09:53:32 -0600244 else {
245 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100246
Jens Axboe05f1dd52014-05-29 09:53:32 -0600247 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800248 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600249 bio->bi_next = nxt;
250 }
251
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600252 bio_set_flag(bio, BIO_SEG_VALID);
Jens Axboed6d48192008-01-29 14:04:06 +0100253}
254EXPORT_SYMBOL(blk_recount_segments);
255
256static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
257 struct bio *nxt)
258{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700259 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700260 struct bvec_iter iter;
261
Martin K. Petersene692cb62010-12-01 19:41:49 +0100262 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100263 return 0;
264
FUJITA Tomonori86771422008-10-13 14:19:05 +0200265 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400266 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100267 return 0;
268
David Woodhousee17fc0a2008-08-09 16:42:20 +0100269 if (!bio_has_data(bio))
270 return 1;
271
Kent Overstreetf619d252013-08-07 14:30:33 -0700272 bio_for_each_segment(end_bv, bio, iter)
273 if (end_bv.bv_len == iter.bi_size)
274 break;
275
276 nxt_bv = bio_iovec(nxt);
277
278 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100279 return 0;
280
Jens Axboed6d48192008-01-29 14:04:06 +0100281 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100282 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100283 * these two to be merged into one
284 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700285 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100286 return 1;
287
288 return 0;
289}
290
Kent Overstreet79886132013-11-23 17:19:00 -0800291static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200292__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800293 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200294 struct scatterlist **sg, int *nsegs, int *cluster)
295{
296
297 int nbytes = bvec->bv_len;
298
Kent Overstreet79886132013-11-23 17:19:00 -0800299 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200300 if ((*sg)->length + nbytes > queue_max_segment_size(q))
301 goto new_segment;
302
Kent Overstreet79886132013-11-23 17:19:00 -0800303 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200304 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800305 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200306 goto new_segment;
307
308 (*sg)->length += nbytes;
309 } else {
310new_segment:
311 if (!*sg)
312 *sg = sglist;
313 else {
314 /*
315 * If the driver previously mapped a shorter
316 * list, we could see a termination bit
317 * prematurely unless it fully inits the sg
318 * table on each mapping. We KNOW that there
319 * must be more entries here or the driver
320 * would be buggy, so force clear the
321 * termination bit to avoid doing a full
322 * sg_init_table() in drivers for each command.
323 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030324 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200325 *sg = sg_next(*sg);
326 }
327
328 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
329 (*nsegs)++;
330 }
Kent Overstreet79886132013-11-23 17:19:00 -0800331 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200332}
333
Kent Overstreet5cb88502014-02-07 13:53:46 -0700334static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
335 struct scatterlist *sglist,
336 struct scatterlist **sg)
337{
338 struct bio_vec bvec, bvprv = { NULL };
339 struct bvec_iter iter;
340 int nsegs, cluster;
341
342 nsegs = 0;
343 cluster = blk_queue_cluster(q);
344
345 if (bio->bi_rw & REQ_DISCARD) {
346 /*
347 * This is a hack - drivers should be neither modifying the
348 * biovec, nor relying on bi_vcnt - but because of
349 * blk_add_request_payload(), a discard bio may or may not have
350 * a payload we need to set up here (thank you Christoph) and
351 * bi_vcnt is really the only way of telling if we need to.
352 */
353
354 if (bio->bi_vcnt)
355 goto single_segment;
356
357 return 0;
358 }
359
360 if (bio->bi_rw & REQ_WRITE_SAME) {
361single_segment:
362 *sg = sglist;
363 bvec = bio_iovec(bio);
364 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
365 return 1;
366 }
367
368 for_each_bio(bio)
369 bio_for_each_segment(bvec, bio, iter)
370 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
371 &nsegs, &cluster);
372
373 return nsegs;
374}
375
Jens Axboed6d48192008-01-29 14:04:06 +0100376/*
377 * map a request to scatterlist, return number of sg entries setup. Caller
378 * must make sure sg can hold rq->nr_phys_segments entries
379 */
380int blk_rq_map_sg(struct request_queue *q, struct request *rq,
381 struct scatterlist *sglist)
382{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700383 struct scatterlist *sg = NULL;
384 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100385
Kent Overstreet5cb88502014-02-07 13:53:46 -0700386 if (rq->bio)
387 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200388
389 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900390 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
391 unsigned int pad_len =
392 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200393
394 sg->length += pad_len;
395 rq->extra_len += pad_len;
396 }
397
Tejun Heo2fb98e82008-02-19 11:36:53 +0100398 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200399 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100400 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
401
Dan Williamsda81ed12015-08-07 18:15:14 +0200402 sg_unmark_end(sg);
Jens Axboed6d48192008-01-29 14:04:06 +0100403 sg = sg_next(sg);
404 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
405 q->dma_drain_size,
406 ((unsigned long)q->dma_drain_buffer) &
407 (PAGE_SIZE - 1));
408 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100409 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100410 }
411
412 if (sg)
413 sg_mark_end(sg);
414
415 return nsegs;
416}
Jens Axboed6d48192008-01-29 14:04:06 +0100417EXPORT_SYMBOL(blk_rq_map_sg);
418
Jens Axboed6d48192008-01-29 14:04:06 +0100419static inline int ll_new_hw_segment(struct request_queue *q,
420 struct request *req,
421 struct bio *bio)
422{
Jens Axboed6d48192008-01-29 14:04:06 +0100423 int nr_phys_segs = bio_phys_segments(q, bio);
424
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200425 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
426 goto no_merge;
427
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400428 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200429 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100430
431 /*
432 * This will form the start of a new hw segment. Bump both
433 * counters.
434 */
Jens Axboed6d48192008-01-29 14:04:06 +0100435 req->nr_phys_segments += nr_phys_segs;
436 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200437
438no_merge:
439 req->cmd_flags |= REQ_NOMERGE;
440 if (req == q->last_merge)
441 q->last_merge = NULL;
442 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100443}
444
445int ll_back_merge_fn(struct request_queue *q, struct request *req,
446 struct bio *bio)
447{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300448 if (req_gap_back_merge(req, bio))
449 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600450 if (blk_integrity_rq(req) &&
451 integrity_req_gap_back_merge(req, bio))
452 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400453 if (blk_rq_sectors(req) + bio_sectors(bio) >
454 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100455 req->cmd_flags |= REQ_NOMERGE;
456 if (req == q->last_merge)
457 q->last_merge = NULL;
458 return 0;
459 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200460 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100461 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200462 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100463 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100464
465 return ll_new_hw_segment(q, req, bio);
466}
467
Jens Axboe6728cb02008-01-31 13:03:55 +0100468int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100469 struct bio *bio)
470{
Jens Axboe5e7c4272015-09-03 19:28:20 +0300471
472 if (req_gap_front_merge(req, bio))
473 return 0;
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600474 if (blk_integrity_rq(req) &&
475 integrity_req_gap_front_merge(req, bio))
476 return 0;
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400477 if (blk_rq_sectors(req) + bio_sectors(bio) >
478 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100479 req->cmd_flags |= REQ_NOMERGE;
480 if (req == q->last_merge)
481 q->last_merge = NULL;
482 return 0;
483 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200484 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100485 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200486 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100487 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100488
489 return ll_new_hw_segment(q, req, bio);
490}
491
Jens Axboee7e24502013-10-29 12:11:47 -0600492/*
493 * blk-mq uses req->special to carry normal driver per-request payload, it
494 * does not indicate a prepared command that we cannot merge with.
495 */
496static bool req_no_special_merge(struct request *req)
497{
498 struct request_queue *q = req->q;
499
500 return !q->mq_ops && req->special;
501}
502
Jens Axboed6d48192008-01-29 14:04:06 +0100503static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
504 struct request *next)
505{
506 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200507 unsigned int seg_size =
508 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100509
510 /*
511 * First check if the either of the requests are re-queued
512 * requests. Can't merge them if they are.
513 */
Jens Axboee7e24502013-10-29 12:11:47 -0600514 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100515 return 0;
516
Jens Axboe5e7c4272015-09-03 19:28:20 +0300517 if (req_gap_back_merge(req, next->bio))
Keith Busch854fbb92015-02-11 08:20:13 -0700518 return 0;
519
Jens Axboed6d48192008-01-29 14:04:06 +0100520 /*
521 * Will it become too large?
522 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400523 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
524 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100525 return 0;
526
527 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200528 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
529 if (req->nr_phys_segments == 1)
530 req->bio->bi_seg_front_size = seg_size;
531 if (next->nr_phys_segments == 1)
532 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100533 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200534 }
Jens Axboed6d48192008-01-29 14:04:06 +0100535
Martin K. Petersen8a783622010-02-26 00:20:39 -0500536 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100537 return 0;
538
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400539 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200540 return 0;
541
Jens Axboed6d48192008-01-29 14:04:06 +0100542 /* Merge is OK... */
543 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100544 return 1;
545}
546
Tejun Heo80a761f2009-07-03 17:48:17 +0900547/**
548 * blk_rq_set_mixed_merge - mark a request as mixed merge
549 * @rq: request to mark as mixed merge
550 *
551 * Description:
552 * @rq is about to be mixed merged. Make sure the attributes
553 * which can be mixed are set in each bio and mark @rq as mixed
554 * merged.
555 */
556void blk_rq_set_mixed_merge(struct request *rq)
557{
558 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
559 struct bio *bio;
560
561 if (rq->cmd_flags & REQ_MIXED_MERGE)
562 return;
563
564 /*
565 * @rq will no longer represent mixable attributes for all the
566 * contained bios. It will just track those of the first one.
567 * Distributes the attributs to each bio.
568 */
569 for (bio = rq->bio; bio; bio = bio->bi_next) {
570 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
571 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
572 bio->bi_rw |= ff;
573 }
574 rq->cmd_flags |= REQ_MIXED_MERGE;
575}
576
Jerome Marchand26308ea2009-03-27 10:31:51 +0100577static void blk_account_io_merge(struct request *req)
578{
579 if (blk_do_io_stat(req)) {
580 struct hd_struct *part;
581 int cpu;
582
583 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100584 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100585
586 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200587 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100588
Jens Axboe6c23a962011-01-07 08:43:37 +0100589 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100590 part_stat_unlock();
591 }
592}
593
Jens Axboed6d48192008-01-29 14:04:06 +0100594/*
595 * Has to be called with the request spinlock acquired
596 */
597static int attempt_merge(struct request_queue *q, struct request *req,
598 struct request *next)
599{
600 if (!rq_mergeable(req) || !rq_mergeable(next))
601 return 0;
602
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400603 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
604 return 0;
605
Jens Axboed6d48192008-01-29 14:04:06 +0100606 /*
607 * not contiguous
608 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900609 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100610 return 0;
611
612 if (rq_data_dir(req) != rq_data_dir(next)
613 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600614 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100615 return 0;
616
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400617 if (req->cmd_flags & REQ_WRITE_SAME &&
618 !blk_write_same_mergeable(req->bio, next->bio))
619 return 0;
620
Jens Axboed6d48192008-01-29 14:04:06 +0100621 /*
622 * If we are allowed to merge, then append bio list
623 * from next to rq and release next. merge_requests_fn
624 * will have updated segment counts, update sector
625 * counts here.
626 */
627 if (!ll_merge_requests_fn(q, req, next))
628 return 0;
629
630 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900631 * If failfast settings disagree or any of the two is already
632 * a mixed merge, mark both as mixed before proceeding. This
633 * makes sure that all involved bios have mixable attributes
634 * set properly.
635 */
636 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
637 (req->cmd_flags & REQ_FAILFAST_MASK) !=
638 (next->cmd_flags & REQ_FAILFAST_MASK)) {
639 blk_rq_set_mixed_merge(req);
640 blk_rq_set_mixed_merge(next);
641 }
642
643 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100644 * At this point we have either done a back merge
645 * or front merge. We need the smaller start_time of
646 * the merged requests to be the current request
647 * for accounting purposes.
648 */
649 if (time_after(req->start_time, next->start_time))
650 req->start_time = next->start_time;
651
652 req->biotail->bi_next = next->bio;
653 req->biotail = next->biotail;
654
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900655 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100656
657 elv_merge_requests(q, req, next);
658
Jerome Marchand42dad762009-04-22 14:01:49 +0200659 /*
660 * 'next' is going away, so update stats accordingly
661 */
662 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100663
664 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200665 if (blk_rq_cpu_valid(next))
666 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100667
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100668 /* owner-ship of bio passed from next to req */
669 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100670 __blk_put_request(q, next);
671 return 1;
672}
673
674int attempt_back_merge(struct request_queue *q, struct request *rq)
675{
676 struct request *next = elv_latter_request(q, rq);
677
678 if (next)
679 return attempt_merge(q, rq, next);
680
681 return 0;
682}
683
684int attempt_front_merge(struct request_queue *q, struct request *rq)
685{
686 struct request *prev = elv_former_request(q, rq);
687
688 if (prev)
689 return attempt_merge(q, prev, rq);
690
691 return 0;
692}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100693
694int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
695 struct request *next)
696{
697 return attempt_merge(q, rq, next);
698}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100699
700bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
701{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400702 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100703 return false;
704
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400705 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
706 return false;
707
Tejun Heo050c8ea2012-02-08 09:19:38 +0100708 /* different data direction or already started, don't merge */
709 if (bio_data_dir(bio) != rq_data_dir(rq))
710 return false;
711
712 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600713 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100714 return false;
715
716 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400717 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100718 return false;
719
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400720 /* must be using the same buffer */
721 if (rq->cmd_flags & REQ_WRITE_SAME &&
722 !blk_write_same_mergeable(rq->bio, bio))
723 return false;
724
Tejun Heo050c8ea2012-02-08 09:19:38 +0100725 return true;
726}
727
728int blk_try_merge(struct request *rq, struct bio *bio)
729{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700730 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100731 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700732 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100733 return ELEVATOR_FRONT_MERGE;
734 return ELEVATOR_NO_MERGE;
735}