blob: fd3fee81c23ce2f1cdc73d2bf4e76188c90358cb [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
Jens Axboe1e428072009-02-23 09:03:10 +010012static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Ming Lei07388542014-09-02 23:02:59 +080013 struct bio *bio,
14 bool no_sg_merge)
Jens Axboed6d48192008-01-29 14:04:06 +010015{
Kent Overstreet79886132013-11-23 17:19:00 -080016 struct bio_vec bv, bvprv = { NULL };
Ming Lei07388542014-09-02 23:02:59 +080017 int cluster, high, highprv = 1;
Jens Axboe1e428072009-02-23 09:03:10 +010018 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +010019 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -080020 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +010021
Jens Axboe1e428072009-02-23 09:03:10 +010022 if (!bio)
23 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +010024
Kent Overstreet5cb88502014-02-07 13:53:46 -070025 /*
26 * This should probably be returning 0, but blk_add_request_payload()
27 * (Christoph!!!!)
28 */
29 if (bio->bi_rw & REQ_DISCARD)
30 return 1;
31
32 if (bio->bi_rw & REQ_WRITE_SAME)
33 return 1;
34
Jens Axboe1e428072009-02-23 09:03:10 +010035 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +010036 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +020037 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +020038 nr_phys_segs = 0;
Jens Axboe05f1dd52014-05-29 09:53:32 -060039 high = 0;
Jens Axboe1e428072009-02-23 09:03:10 +010040 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -080041 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +010042 /*
Jens Axboe05f1dd52014-05-29 09:53:32 -060043 * If SG merging is disabled, each bio vector is
44 * a segment
45 */
46 if (no_sg_merge)
47 goto new_segment;
48
49 /*
Jens Axboe1e428072009-02-23 09:03:10 +010050 * the trick here is making sure that a high page is
Jens Axboe05f1dd52014-05-29 09:53:32 -060051 * never considered part of another segment, since
52 * that might change with the bounce page.
Jens Axboe1e428072009-02-23 09:03:10 +010053 */
Kent Overstreet79886132013-11-23 17:19:00 -080054 high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q);
55 if (!high && !highprv && cluster) {
56 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -040057 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +010058 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -080059 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +010060 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -080061 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +010062 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +010063
Kent Overstreet79886132013-11-23 17:19:00 -080064 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +010065 bvprv = bv;
66 continue;
67 }
Jens Axboed6d48192008-01-29 14:04:06 +010068new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +010069 if (nr_phys_segs == 1 && seg_size >
70 fbio->bi_seg_front_size)
71 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +020072
Jens Axboe1e428072009-02-23 09:03:10 +010073 nr_phys_segs++;
74 bvprv = bv;
Kent Overstreet79886132013-11-23 17:19:00 -080075 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +010076 highprv = high;
77 }
Jens Axboe59247ea2009-03-06 08:55:24 +010078 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +010079 }
80
Jens Axboe59247ea2009-03-06 08:55:24 +010081 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
82 fbio->bi_seg_front_size = seg_size;
83 if (seg_size > bbio->bi_seg_back_size)
84 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +010085
86 return nr_phys_segs;
87}
88
89void blk_recalc_rq_segments(struct request *rq)
90{
Ming Lei07388542014-09-02 23:02:59 +080091 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
92 &rq->q->queue_flags);
93
94 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
95 no_sg_merge);
Jens Axboed6d48192008-01-29 14:04:06 +010096}
97
98void blk_recount_segments(struct request_queue *q, struct bio *bio)
99{
Ming Lei7f60dca2014-11-12 00:15:41 +0800100 unsigned short seg_cnt;
Ming Lei764f6122014-10-09 23:17:35 +0800101
Ming Lei7f60dca2014-11-12 00:15:41 +0800102 /* estimate segment number by bi_vcnt for non-cloned bio */
103 if (bio_flagged(bio, BIO_CLONED))
104 seg_cnt = bio_segments(bio);
105 else
106 seg_cnt = bio->bi_vcnt;
107
108 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
109 (seg_cnt < queue_max_segments(q)))
110 bio->bi_phys_segments = seg_cnt;
Jens Axboe05f1dd52014-05-29 09:53:32 -0600111 else {
112 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100113
Jens Axboe05f1dd52014-05-29 09:53:32 -0600114 bio->bi_next = NULL;
Ming Lei7f60dca2014-11-12 00:15:41 +0800115 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600116 bio->bi_next = nxt;
117 }
118
Jens Axboed6d48192008-01-29 14:04:06 +0100119 bio->bi_flags |= (1 << BIO_SEG_VALID);
120}
121EXPORT_SYMBOL(blk_recount_segments);
122
123static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
124 struct bio *nxt)
125{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700126 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700127 struct bvec_iter iter;
128
Martin K. Petersene692cb62010-12-01 19:41:49 +0100129 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100130 return 0;
131
FUJITA Tomonori86771422008-10-13 14:19:05 +0200132 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400133 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100134 return 0;
135
David Woodhousee17fc0a2008-08-09 16:42:20 +0100136 if (!bio_has_data(bio))
137 return 1;
138
Kent Overstreetf619d252013-08-07 14:30:33 -0700139 bio_for_each_segment(end_bv, bio, iter)
140 if (end_bv.bv_len == iter.bi_size)
141 break;
142
143 nxt_bv = bio_iovec(nxt);
144
145 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100146 return 0;
147
Jens Axboed6d48192008-01-29 14:04:06 +0100148 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100149 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100150 * these two to be merged into one
151 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700152 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100153 return 1;
154
155 return 0;
156}
157
Kent Overstreet79886132013-11-23 17:19:00 -0800158static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200159__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800160 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200161 struct scatterlist **sg, int *nsegs, int *cluster)
162{
163
164 int nbytes = bvec->bv_len;
165
Kent Overstreet79886132013-11-23 17:19:00 -0800166 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200167 if ((*sg)->length + nbytes > queue_max_segment_size(q))
168 goto new_segment;
169
Kent Overstreet79886132013-11-23 17:19:00 -0800170 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200171 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800172 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200173 goto new_segment;
174
175 (*sg)->length += nbytes;
176 } else {
177new_segment:
178 if (!*sg)
179 *sg = sglist;
180 else {
181 /*
182 * If the driver previously mapped a shorter
183 * list, we could see a termination bit
184 * prematurely unless it fully inits the sg
185 * table on each mapping. We KNOW that there
186 * must be more entries here or the driver
187 * would be buggy, so force clear the
188 * termination bit to avoid doing a full
189 * sg_init_table() in drivers for each command.
190 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030191 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200192 *sg = sg_next(*sg);
193 }
194
195 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
196 (*nsegs)++;
197 }
Kent Overstreet79886132013-11-23 17:19:00 -0800198 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200199}
200
Kent Overstreet5cb88502014-02-07 13:53:46 -0700201static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
202 struct scatterlist *sglist,
203 struct scatterlist **sg)
204{
205 struct bio_vec bvec, bvprv = { NULL };
206 struct bvec_iter iter;
207 int nsegs, cluster;
208
209 nsegs = 0;
210 cluster = blk_queue_cluster(q);
211
212 if (bio->bi_rw & REQ_DISCARD) {
213 /*
214 * This is a hack - drivers should be neither modifying the
215 * biovec, nor relying on bi_vcnt - but because of
216 * blk_add_request_payload(), a discard bio may or may not have
217 * a payload we need to set up here (thank you Christoph) and
218 * bi_vcnt is really the only way of telling if we need to.
219 */
220
221 if (bio->bi_vcnt)
222 goto single_segment;
223
224 return 0;
225 }
226
227 if (bio->bi_rw & REQ_WRITE_SAME) {
228single_segment:
229 *sg = sglist;
230 bvec = bio_iovec(bio);
231 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
232 return 1;
233 }
234
235 for_each_bio(bio)
236 bio_for_each_segment(bvec, bio, iter)
237 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
238 &nsegs, &cluster);
239
240 return nsegs;
241}
242
Jens Axboed6d48192008-01-29 14:04:06 +0100243/*
244 * map a request to scatterlist, return number of sg entries setup. Caller
245 * must make sure sg can hold rq->nr_phys_segments entries
246 */
247int blk_rq_map_sg(struct request_queue *q, struct request *rq,
248 struct scatterlist *sglist)
249{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700250 struct scatterlist *sg = NULL;
251 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100252
Kent Overstreet5cb88502014-02-07 13:53:46 -0700253 if (rq->bio)
254 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200255
256 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900257 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
258 unsigned int pad_len =
259 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200260
261 sg->length += pad_len;
262 rq->extra_len += pad_len;
263 }
264
Tejun Heo2fb98e82008-02-19 11:36:53 +0100265 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200266 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100267 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
268
Jens Axboed6d48192008-01-29 14:04:06 +0100269 sg->page_link &= ~0x02;
270 sg = sg_next(sg);
271 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
272 q->dma_drain_size,
273 ((unsigned long)q->dma_drain_buffer) &
274 (PAGE_SIZE - 1));
275 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100276 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100277 }
278
279 if (sg)
280 sg_mark_end(sg);
281
282 return nsegs;
283}
Jens Axboed6d48192008-01-29 14:04:06 +0100284EXPORT_SYMBOL(blk_rq_map_sg);
285
Jens Axboed6d48192008-01-29 14:04:06 +0100286static inline int ll_new_hw_segment(struct request_queue *q,
287 struct request *req,
288 struct bio *bio)
289{
Jens Axboed6d48192008-01-29 14:04:06 +0100290 int nr_phys_segs = bio_phys_segments(q, bio);
291
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200292 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
293 goto no_merge;
294
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400295 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200296 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100297
298 /*
299 * This will form the start of a new hw segment. Bump both
300 * counters.
301 */
Jens Axboed6d48192008-01-29 14:04:06 +0100302 req->nr_phys_segments += nr_phys_segs;
303 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200304
305no_merge:
306 req->cmd_flags |= REQ_NOMERGE;
307 if (req == q->last_merge)
308 q->last_merge = NULL;
309 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100310}
311
312int ll_back_merge_fn(struct request_queue *q, struct request *req,
313 struct bio *bio)
314{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400315 if (blk_rq_sectors(req) + bio_sectors(bio) >
316 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100317 req->cmd_flags |= REQ_NOMERGE;
318 if (req == q->last_merge)
319 q->last_merge = NULL;
320 return 0;
321 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200322 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100323 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200324 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100325 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100326
327 return ll_new_hw_segment(q, req, bio);
328}
329
Jens Axboe6728cb02008-01-31 13:03:55 +0100330int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100331 struct bio *bio)
332{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400333 if (blk_rq_sectors(req) + bio_sectors(bio) >
334 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100335 req->cmd_flags |= REQ_NOMERGE;
336 if (req == q->last_merge)
337 q->last_merge = NULL;
338 return 0;
339 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200340 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100341 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200342 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100343 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100344
345 return ll_new_hw_segment(q, req, bio);
346}
347
Jens Axboee7e24502013-10-29 12:11:47 -0600348/*
349 * blk-mq uses req->special to carry normal driver per-request payload, it
350 * does not indicate a prepared command that we cannot merge with.
351 */
352static bool req_no_special_merge(struct request *req)
353{
354 struct request_queue *q = req->q;
355
356 return !q->mq_ops && req->special;
357}
358
Keith Busch854fbb92015-02-11 08:20:13 -0700359static int req_gap_to_prev(struct request *req, struct request *next)
360{
361 struct bio *prev = req->biotail;
362
363 return bvec_gap_to_prev(&prev->bi_io_vec[prev->bi_vcnt - 1],
364 next->bio->bi_io_vec[0].bv_offset);
365}
366
Jens Axboed6d48192008-01-29 14:04:06 +0100367static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
368 struct request *next)
369{
370 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200371 unsigned int seg_size =
372 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100373
374 /*
375 * First check if the either of the requests are re-queued
376 * requests. Can't merge them if they are.
377 */
Jens Axboee7e24502013-10-29 12:11:47 -0600378 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100379 return 0;
380
Keith Busch854fbb92015-02-11 08:20:13 -0700381 if (test_bit(QUEUE_FLAG_SG_GAPS, &q->queue_flags) &&
382 req_gap_to_prev(req, next))
383 return 0;
384
Jens Axboed6d48192008-01-29 14:04:06 +0100385 /*
386 * Will it become too large?
387 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400388 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
389 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100390 return 0;
391
392 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200393 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
394 if (req->nr_phys_segments == 1)
395 req->bio->bi_seg_front_size = seg_size;
396 if (next->nr_phys_segments == 1)
397 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100398 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200399 }
Jens Axboed6d48192008-01-29 14:04:06 +0100400
Martin K. Petersen8a783622010-02-26 00:20:39 -0500401 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100402 return 0;
403
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400404 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200405 return 0;
406
Jens Axboed6d48192008-01-29 14:04:06 +0100407 /* Merge is OK... */
408 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100409 return 1;
410}
411
Tejun Heo80a761f2009-07-03 17:48:17 +0900412/**
413 * blk_rq_set_mixed_merge - mark a request as mixed merge
414 * @rq: request to mark as mixed merge
415 *
416 * Description:
417 * @rq is about to be mixed merged. Make sure the attributes
418 * which can be mixed are set in each bio and mark @rq as mixed
419 * merged.
420 */
421void blk_rq_set_mixed_merge(struct request *rq)
422{
423 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
424 struct bio *bio;
425
426 if (rq->cmd_flags & REQ_MIXED_MERGE)
427 return;
428
429 /*
430 * @rq will no longer represent mixable attributes for all the
431 * contained bios. It will just track those of the first one.
432 * Distributes the attributs to each bio.
433 */
434 for (bio = rq->bio; bio; bio = bio->bi_next) {
435 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
436 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
437 bio->bi_rw |= ff;
438 }
439 rq->cmd_flags |= REQ_MIXED_MERGE;
440}
441
Jerome Marchand26308ea2009-03-27 10:31:51 +0100442static void blk_account_io_merge(struct request *req)
443{
444 if (blk_do_io_stat(req)) {
445 struct hd_struct *part;
446 int cpu;
447
448 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100449 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100450
451 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200452 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100453
Jens Axboe6c23a962011-01-07 08:43:37 +0100454 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100455 part_stat_unlock();
456 }
457}
458
Jens Axboed6d48192008-01-29 14:04:06 +0100459/*
460 * Has to be called with the request spinlock acquired
461 */
462static int attempt_merge(struct request_queue *q, struct request *req,
463 struct request *next)
464{
465 if (!rq_mergeable(req) || !rq_mergeable(next))
466 return 0;
467
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400468 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
469 return 0;
470
Jens Axboed6d48192008-01-29 14:04:06 +0100471 /*
472 * not contiguous
473 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900474 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100475 return 0;
476
477 if (rq_data_dir(req) != rq_data_dir(next)
478 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600479 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100480 return 0;
481
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400482 if (req->cmd_flags & REQ_WRITE_SAME &&
483 !blk_write_same_mergeable(req->bio, next->bio))
484 return 0;
485
Jens Axboed6d48192008-01-29 14:04:06 +0100486 /*
487 * If we are allowed to merge, then append bio list
488 * from next to rq and release next. merge_requests_fn
489 * will have updated segment counts, update sector
490 * counts here.
491 */
492 if (!ll_merge_requests_fn(q, req, next))
493 return 0;
494
495 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900496 * If failfast settings disagree or any of the two is already
497 * a mixed merge, mark both as mixed before proceeding. This
498 * makes sure that all involved bios have mixable attributes
499 * set properly.
500 */
501 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
502 (req->cmd_flags & REQ_FAILFAST_MASK) !=
503 (next->cmd_flags & REQ_FAILFAST_MASK)) {
504 blk_rq_set_mixed_merge(req);
505 blk_rq_set_mixed_merge(next);
506 }
507
508 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100509 * At this point we have either done a back merge
510 * or front merge. We need the smaller start_time of
511 * the merged requests to be the current request
512 * for accounting purposes.
513 */
514 if (time_after(req->start_time, next->start_time))
515 req->start_time = next->start_time;
516
517 req->biotail->bi_next = next->bio;
518 req->biotail = next->biotail;
519
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900520 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100521
522 elv_merge_requests(q, req, next);
523
Jerome Marchand42dad762009-04-22 14:01:49 +0200524 /*
525 * 'next' is going away, so update stats accordingly
526 */
527 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100528
529 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200530 if (blk_rq_cpu_valid(next))
531 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100532
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100533 /* owner-ship of bio passed from next to req */
534 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100535 __blk_put_request(q, next);
536 return 1;
537}
538
539int attempt_back_merge(struct request_queue *q, struct request *rq)
540{
541 struct request *next = elv_latter_request(q, rq);
542
543 if (next)
544 return attempt_merge(q, rq, next);
545
546 return 0;
547}
548
549int attempt_front_merge(struct request_queue *q, struct request *rq)
550{
551 struct request *prev = elv_former_request(q, rq);
552
553 if (prev)
554 return attempt_merge(q, prev, rq);
555
556 return 0;
557}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100558
559int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
560 struct request *next)
561{
562 return attempt_merge(q, rq, next);
563}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100564
565bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
566{
Jens Axboe66cb45a2014-06-24 16:22:24 -0600567 struct request_queue *q = rq->q;
568
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400569 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100570 return false;
571
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400572 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
573 return false;
574
Tejun Heo050c8ea2012-02-08 09:19:38 +0100575 /* different data direction or already started, don't merge */
576 if (bio_data_dir(bio) != rq_data_dir(rq))
577 return false;
578
579 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600580 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100581 return false;
582
583 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400584 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100585 return false;
586
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400587 /* must be using the same buffer */
588 if (rq->cmd_flags & REQ_WRITE_SAME &&
589 !blk_write_same_mergeable(rq->bio, bio))
590 return false;
591
Jens Axboe66cb45a2014-06-24 16:22:24 -0600592 if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS)) {
593 struct bio_vec *bprev;
594
Wenbo Wang7ee8e4f32015-03-20 01:04:54 -0400595 bprev = &rq->biotail->bi_io_vec[rq->biotail->bi_vcnt - 1];
Jens Axboe66cb45a2014-06-24 16:22:24 -0600596 if (bvec_gap_to_prev(bprev, bio->bi_io_vec[0].bv_offset))
597 return false;
598 }
599
Tejun Heo050c8ea2012-02-08 09:19:38 +0100600 return true;
601}
602
603int blk_try_merge(struct request *rq, struct bio *bio)
604{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700605 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100606 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700607 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100608 return ELEVATOR_FRONT_MERGE;
609 return ELEVATOR_NO_MERGE;
610}