blob: b3bf0df0f4c2743aa99ef7ac179db324eb5e4657 [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,
Jens Axboe59247ea2009-03-06 08:55:24 +010013 struct bio *bio)
Jens Axboed6d48192008-01-29 14:04:06 +010014{
Kent Overstreet79886132013-11-23 17:19:00 -080015 struct bio_vec bv, bvprv = { NULL };
Jens Axboe05f1dd52014-05-29 09:53:32 -060016 int cluster, high, highprv = 1, no_sg_merge;
Jens Axboe1e428072009-02-23 09:03:10 +010017 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +010018 struct bio *fbio, *bbio;
Kent Overstreet79886132013-11-23 17:19:00 -080019 struct bvec_iter iter;
Jens Axboed6d48192008-01-29 14:04:06 +010020
Jens Axboe1e428072009-02-23 09:03:10 +010021 if (!bio)
22 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +010023
Kent Overstreet5cb88502014-02-07 13:53:46 -070024 /*
25 * This should probably be returning 0, but blk_add_request_payload()
26 * (Christoph!!!!)
27 */
28 if (bio->bi_rw & REQ_DISCARD)
29 return 1;
30
31 if (bio->bi_rw & REQ_WRITE_SAME)
32 return 1;
33
Jens Axboe1e428072009-02-23 09:03:10 +010034 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +010035 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +020036 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +020037 nr_phys_segs = 0;
Jens Axboe05f1dd52014-05-29 09:53:32 -060038 no_sg_merge = test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
39 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{
Jens Axboe59247ea2009-03-06 08:55:24 +010091 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
Jens Axboed6d48192008-01-29 14:04:06 +010092}
93
94void blk_recount_segments(struct request_queue *q, struct bio *bio)
95{
Jens Axboe05f1dd52014-05-29 09:53:32 -060096 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags))
97 bio->bi_phys_segments = bio->bi_vcnt;
98 else {
99 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100100
Jens Axboe05f1dd52014-05-29 09:53:32 -0600101 bio->bi_next = NULL;
102 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
103 bio->bi_next = nxt;
104 }
105
Jens Axboed6d48192008-01-29 14:04:06 +0100106 bio->bi_flags |= (1 << BIO_SEG_VALID);
107}
108EXPORT_SYMBOL(blk_recount_segments);
109
110static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
111 struct bio *nxt)
112{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700113 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700114 struct bvec_iter iter;
115
Martin K. Petersene692cb62010-12-01 19:41:49 +0100116 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100117 return 0;
118
FUJITA Tomonori86771422008-10-13 14:19:05 +0200119 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400120 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100121 return 0;
122
David Woodhousee17fc0a2008-08-09 16:42:20 +0100123 if (!bio_has_data(bio))
124 return 1;
125
Kent Overstreetf619d252013-08-07 14:30:33 -0700126 bio_for_each_segment(end_bv, bio, iter)
127 if (end_bv.bv_len == iter.bi_size)
128 break;
129
130 nxt_bv = bio_iovec(nxt);
131
132 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100133 return 0;
134
Jens Axboed6d48192008-01-29 14:04:06 +0100135 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100136 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100137 * these two to be merged into one
138 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700139 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100140 return 1;
141
142 return 0;
143}
144
Kent Overstreet79886132013-11-23 17:19:00 -0800145static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200146__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800147 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200148 struct scatterlist **sg, int *nsegs, int *cluster)
149{
150
151 int nbytes = bvec->bv_len;
152
Kent Overstreet79886132013-11-23 17:19:00 -0800153 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200154 if ((*sg)->length + nbytes > queue_max_segment_size(q))
155 goto new_segment;
156
Kent Overstreet79886132013-11-23 17:19:00 -0800157 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200158 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800159 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200160 goto new_segment;
161
162 (*sg)->length += nbytes;
163 } else {
164new_segment:
165 if (!*sg)
166 *sg = sglist;
167 else {
168 /*
169 * If the driver previously mapped a shorter
170 * list, we could see a termination bit
171 * prematurely unless it fully inits the sg
172 * table on each mapping. We KNOW that there
173 * must be more entries here or the driver
174 * would be buggy, so force clear the
175 * termination bit to avoid doing a full
176 * sg_init_table() in drivers for each command.
177 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030178 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200179 *sg = sg_next(*sg);
180 }
181
182 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
183 (*nsegs)++;
184 }
Kent Overstreet79886132013-11-23 17:19:00 -0800185 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200186}
187
Kent Overstreet5cb88502014-02-07 13:53:46 -0700188static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
189 struct scatterlist *sglist,
190 struct scatterlist **sg)
191{
192 struct bio_vec bvec, bvprv = { NULL };
193 struct bvec_iter iter;
194 int nsegs, cluster;
195
196 nsegs = 0;
197 cluster = blk_queue_cluster(q);
198
199 if (bio->bi_rw & REQ_DISCARD) {
200 /*
201 * This is a hack - drivers should be neither modifying the
202 * biovec, nor relying on bi_vcnt - but because of
203 * blk_add_request_payload(), a discard bio may or may not have
204 * a payload we need to set up here (thank you Christoph) and
205 * bi_vcnt is really the only way of telling if we need to.
206 */
207
208 if (bio->bi_vcnt)
209 goto single_segment;
210
211 return 0;
212 }
213
214 if (bio->bi_rw & REQ_WRITE_SAME) {
215single_segment:
216 *sg = sglist;
217 bvec = bio_iovec(bio);
218 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
219 return 1;
220 }
221
222 for_each_bio(bio)
223 bio_for_each_segment(bvec, bio, iter)
224 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
225 &nsegs, &cluster);
226
227 return nsegs;
228}
229
Jens Axboed6d48192008-01-29 14:04:06 +0100230/*
231 * map a request to scatterlist, return number of sg entries setup. Caller
232 * must make sure sg can hold rq->nr_phys_segments entries
233 */
234int blk_rq_map_sg(struct request_queue *q, struct request *rq,
235 struct scatterlist *sglist)
236{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700237 struct scatterlist *sg = NULL;
238 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100239
Kent Overstreet5cb88502014-02-07 13:53:46 -0700240 if (rq->bio)
241 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200242
243 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900244 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
245 unsigned int pad_len =
246 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200247
248 sg->length += pad_len;
249 rq->extra_len += pad_len;
250 }
251
Tejun Heo2fb98e82008-02-19 11:36:53 +0100252 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200253 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100254 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
255
Jens Axboed6d48192008-01-29 14:04:06 +0100256 sg->page_link &= ~0x02;
257 sg = sg_next(sg);
258 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
259 q->dma_drain_size,
260 ((unsigned long)q->dma_drain_buffer) &
261 (PAGE_SIZE - 1));
262 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100263 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100264 }
265
266 if (sg)
267 sg_mark_end(sg);
268
269 return nsegs;
270}
Jens Axboed6d48192008-01-29 14:04:06 +0100271EXPORT_SYMBOL(blk_rq_map_sg);
272
Asias He85b9f662012-08-02 23:42:04 +0200273/**
274 * blk_bio_map_sg - map a bio to a scatterlist
275 * @q: request_queue in question
276 * @bio: bio being mapped
277 * @sglist: scatterlist being mapped
278 *
279 * Note:
280 * Caller must make sure sg can hold bio->bi_phys_segments entries
281 *
282 * Will return the number of sg entries setup
283 */
284int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
285 struct scatterlist *sglist)
286{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700287 struct scatterlist *sg = NULL;
288 int nsegs;
289 struct bio *next = bio->bi_next;
290 bio->bi_next = NULL;
Asias He85b9f662012-08-02 23:42:04 +0200291
Kent Overstreet5cb88502014-02-07 13:53:46 -0700292 nsegs = __blk_bios_map_sg(q, bio, sglist, &sg);
293 bio->bi_next = next;
Asias He85b9f662012-08-02 23:42:04 +0200294 if (sg)
295 sg_mark_end(sg);
296
297 BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
298 return nsegs;
299}
300EXPORT_SYMBOL(blk_bio_map_sg);
301
Jens Axboed6d48192008-01-29 14:04:06 +0100302static inline int ll_new_hw_segment(struct request_queue *q,
303 struct request *req,
304 struct bio *bio)
305{
Jens Axboed6d48192008-01-29 14:04:06 +0100306 int nr_phys_segs = bio_phys_segments(q, bio);
307
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200308 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
309 goto no_merge;
310
311 if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio))
312 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100313
314 /*
315 * This will form the start of a new hw segment. Bump both
316 * counters.
317 */
Jens Axboed6d48192008-01-29 14:04:06 +0100318 req->nr_phys_segments += nr_phys_segs;
319 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200320
321no_merge:
322 req->cmd_flags |= REQ_NOMERGE;
323 if (req == q->last_merge)
324 q->last_merge = NULL;
325 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100326}
327
328int ll_back_merge_fn(struct request_queue *q, struct request *req,
329 struct bio *bio)
330{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400331 if (blk_rq_sectors(req) + bio_sectors(bio) >
332 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100333 req->cmd_flags |= REQ_NOMERGE;
334 if (req == q->last_merge)
335 q->last_merge = NULL;
336 return 0;
337 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200338 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100339 blk_recount_segments(q, req->biotail);
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 Axboed6d48192008-01-29 14:04:06 +0100342
343 return ll_new_hw_segment(q, req, bio);
344}
345
Jens Axboe6728cb02008-01-31 13:03:55 +0100346int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100347 struct bio *bio)
348{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400349 if (blk_rq_sectors(req) + bio_sectors(bio) >
350 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100351 req->cmd_flags |= REQ_NOMERGE;
352 if (req == q->last_merge)
353 q->last_merge = NULL;
354 return 0;
355 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200356 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100357 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200358 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100359 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100360
361 return ll_new_hw_segment(q, req, bio);
362}
363
Jens Axboee7e24502013-10-29 12:11:47 -0600364/*
365 * blk-mq uses req->special to carry normal driver per-request payload, it
366 * does not indicate a prepared command that we cannot merge with.
367 */
368static bool req_no_special_merge(struct request *req)
369{
370 struct request_queue *q = req->q;
371
372 return !q->mq_ops && req->special;
373}
374
Jens Axboed6d48192008-01-29 14:04:06 +0100375static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
376 struct request *next)
377{
378 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200379 unsigned int seg_size =
380 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100381
382 /*
383 * First check if the either of the requests are re-queued
384 * requests. Can't merge them if they are.
385 */
Jens Axboee7e24502013-10-29 12:11:47 -0600386 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100387 return 0;
388
389 /*
390 * Will it become too large?
391 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400392 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
393 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100394 return 0;
395
396 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200397 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
398 if (req->nr_phys_segments == 1)
399 req->bio->bi_seg_front_size = seg_size;
400 if (next->nr_phys_segments == 1)
401 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100402 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200403 }
Jens Axboed6d48192008-01-29 14:04:06 +0100404
Martin K. Petersen8a783622010-02-26 00:20:39 -0500405 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100406 return 0;
407
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200408 if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next))
409 return 0;
410
Jens Axboed6d48192008-01-29 14:04:06 +0100411 /* Merge is OK... */
412 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100413 return 1;
414}
415
Tejun Heo80a761f2009-07-03 17:48:17 +0900416/**
417 * blk_rq_set_mixed_merge - mark a request as mixed merge
418 * @rq: request to mark as mixed merge
419 *
420 * Description:
421 * @rq is about to be mixed merged. Make sure the attributes
422 * which can be mixed are set in each bio and mark @rq as mixed
423 * merged.
424 */
425void blk_rq_set_mixed_merge(struct request *rq)
426{
427 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
428 struct bio *bio;
429
430 if (rq->cmd_flags & REQ_MIXED_MERGE)
431 return;
432
433 /*
434 * @rq will no longer represent mixable attributes for all the
435 * contained bios. It will just track those of the first one.
436 * Distributes the attributs to each bio.
437 */
438 for (bio = rq->bio; bio; bio = bio->bi_next) {
439 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
440 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
441 bio->bi_rw |= ff;
442 }
443 rq->cmd_flags |= REQ_MIXED_MERGE;
444}
445
Jerome Marchand26308ea2009-03-27 10:31:51 +0100446static void blk_account_io_merge(struct request *req)
447{
448 if (blk_do_io_stat(req)) {
449 struct hd_struct *part;
450 int cpu;
451
452 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100453 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100454
455 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200456 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100457
Jens Axboe6c23a962011-01-07 08:43:37 +0100458 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100459 part_stat_unlock();
460 }
461}
462
Jens Axboed6d48192008-01-29 14:04:06 +0100463/*
464 * Has to be called with the request spinlock acquired
465 */
466static int attempt_merge(struct request_queue *q, struct request *req,
467 struct request *next)
468{
469 if (!rq_mergeable(req) || !rq_mergeable(next))
470 return 0;
471
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400472 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
473 return 0;
474
Jens Axboed6d48192008-01-29 14:04:06 +0100475 /*
476 * not contiguous
477 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900478 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100479 return 0;
480
481 if (rq_data_dir(req) != rq_data_dir(next)
482 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600483 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100484 return 0;
485
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400486 if (req->cmd_flags & REQ_WRITE_SAME &&
487 !blk_write_same_mergeable(req->bio, next->bio))
488 return 0;
489
Jens Axboed6d48192008-01-29 14:04:06 +0100490 /*
491 * If we are allowed to merge, then append bio list
492 * from next to rq and release next. merge_requests_fn
493 * will have updated segment counts, update sector
494 * counts here.
495 */
496 if (!ll_merge_requests_fn(q, req, next))
497 return 0;
498
499 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900500 * If failfast settings disagree or any of the two is already
501 * a mixed merge, mark both as mixed before proceeding. This
502 * makes sure that all involved bios have mixable attributes
503 * set properly.
504 */
505 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
506 (req->cmd_flags & REQ_FAILFAST_MASK) !=
507 (next->cmd_flags & REQ_FAILFAST_MASK)) {
508 blk_rq_set_mixed_merge(req);
509 blk_rq_set_mixed_merge(next);
510 }
511
512 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100513 * At this point we have either done a back merge
514 * or front merge. We need the smaller start_time of
515 * the merged requests to be the current request
516 * for accounting purposes.
517 */
518 if (time_after(req->start_time, next->start_time))
519 req->start_time = next->start_time;
520
521 req->biotail->bi_next = next->bio;
522 req->biotail = next->biotail;
523
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900524 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100525
526 elv_merge_requests(q, req, next);
527
Jerome Marchand42dad762009-04-22 14:01:49 +0200528 /*
529 * 'next' is going away, so update stats accordingly
530 */
531 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100532
533 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200534 if (blk_rq_cpu_valid(next))
535 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100536
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100537 /* owner-ship of bio passed from next to req */
538 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100539 __blk_put_request(q, next);
540 return 1;
541}
542
543int attempt_back_merge(struct request_queue *q, struct request *rq)
544{
545 struct request *next = elv_latter_request(q, rq);
546
547 if (next)
548 return attempt_merge(q, rq, next);
549
550 return 0;
551}
552
553int attempt_front_merge(struct request_queue *q, struct request *rq)
554{
555 struct request *prev = elv_former_request(q, rq);
556
557 if (prev)
558 return attempt_merge(q, prev, rq);
559
560 return 0;
561}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100562
563int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
564 struct request *next)
565{
566 return attempt_merge(q, rq, next);
567}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100568
569bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
570{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400571 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100572 return false;
573
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400574 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
575 return false;
576
Tejun Heo050c8ea2012-02-08 09:19:38 +0100577 /* different data direction or already started, don't merge */
578 if (bio_data_dir(bio) != rq_data_dir(rq))
579 return false;
580
581 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600582 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100583 return false;
584
585 /* only merge integrity protected bio into ditto rq */
586 if (bio_integrity(bio) != blk_integrity_rq(rq))
587 return false;
588
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400589 /* must be using the same buffer */
590 if (rq->cmd_flags & REQ_WRITE_SAME &&
591 !blk_write_same_mergeable(rq->bio, bio))
592 return false;
593
Tejun Heo050c8ea2012-02-08 09:19:38 +0100594 return true;
595}
596
597int blk_try_merge(struct request *rq, struct bio *bio)
598{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700599 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100600 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700601 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100602 return ELEVATOR_FRONT_MERGE;
603 return ELEVATOR_NO_MERGE;
604}