blob: 8f8adaa95466ccc8335cde7e313b551b375ed9b9 [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 };
16 int cluster, high, highprv = 1;
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
Jens Axboe1e428072009-02-23 09:03:10 +010024 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +010025 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +020026 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +020027 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +010028 for_each_bio(bio) {
Kent Overstreet79886132013-11-23 17:19:00 -080029 bio_for_each_segment(bv, bio, iter) {
Jens Axboe1e428072009-02-23 09:03:10 +010030 /*
31 * the trick here is making sure that a high page is
32 * never considered part of another segment, since that
33 * might change with the bounce page.
34 */
Kent Overstreet79886132013-11-23 17:19:00 -080035 high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q);
36 if (!high && !highprv && cluster) {
37 if (seg_size + bv.bv_len
Martin K. Petersenae03bf62009-05-22 17:17:50 -040038 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +010039 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -080040 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +010041 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -080042 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
Jens Axboe1e428072009-02-23 09:03:10 +010043 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +010044
Kent Overstreet79886132013-11-23 17:19:00 -080045 seg_size += bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +010046 bvprv = bv;
47 continue;
48 }
Jens Axboed6d48192008-01-29 14:04:06 +010049new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +010050 if (nr_phys_segs == 1 && seg_size >
51 fbio->bi_seg_front_size)
52 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +020053
Jens Axboe1e428072009-02-23 09:03:10 +010054 nr_phys_segs++;
55 bvprv = bv;
Kent Overstreet79886132013-11-23 17:19:00 -080056 seg_size = bv.bv_len;
Jens Axboe1e428072009-02-23 09:03:10 +010057 highprv = high;
58 }
Jens Axboe59247ea2009-03-06 08:55:24 +010059 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +010060 }
61
Jens Axboe59247ea2009-03-06 08:55:24 +010062 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
63 fbio->bi_seg_front_size = seg_size;
64 if (seg_size > bbio->bi_seg_back_size)
65 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +010066
67 return nr_phys_segs;
68}
69
70void blk_recalc_rq_segments(struct request *rq)
71{
Jens Axboe59247ea2009-03-06 08:55:24 +010072 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
Jens Axboed6d48192008-01-29 14:04:06 +010073}
74
75void blk_recount_segments(struct request_queue *q, struct bio *bio)
76{
Jens Axboed6d48192008-01-29 14:04:06 +010077 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +010078
Jens Axboed6d48192008-01-29 14:04:06 +010079 bio->bi_next = NULL;
Jens Axboe59247ea2009-03-06 08:55:24 +010080 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +010081 bio->bi_next = nxt;
Jens Axboed6d48192008-01-29 14:04:06 +010082 bio->bi_flags |= (1 << BIO_SEG_VALID);
83}
84EXPORT_SYMBOL(blk_recount_segments);
85
86static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
87 struct bio *nxt)
88{
Kent Overstreet2b8221e2013-12-03 14:29:09 -070089 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -070090 struct bvec_iter iter;
91
Martin K. Petersene692cb62010-12-01 19:41:49 +010092 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +010093 return 0;
94
FUJITA Tomonori86771422008-10-13 14:19:05 +020095 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -040096 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +010097 return 0;
98
David Woodhousee17fc0a2008-08-09 16:42:20 +010099 if (!bio_has_data(bio))
100 return 1;
101
Kent Overstreetf619d252013-08-07 14:30:33 -0700102 bio_for_each_segment(end_bv, bio, iter)
103 if (end_bv.bv_len == iter.bi_size)
104 break;
105
106 nxt_bv = bio_iovec(nxt);
107
108 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100109 return 0;
110
Jens Axboed6d48192008-01-29 14:04:06 +0100111 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100112 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100113 * these two to be merged into one
114 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700115 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100116 return 1;
117
118 return 0;
119}
120
Kent Overstreet79886132013-11-23 17:19:00 -0800121static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200122__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800123 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200124 struct scatterlist **sg, int *nsegs, int *cluster)
125{
126
127 int nbytes = bvec->bv_len;
128
Kent Overstreet79886132013-11-23 17:19:00 -0800129 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200130 if ((*sg)->length + nbytes > queue_max_segment_size(q))
131 goto new_segment;
132
Kent Overstreet79886132013-11-23 17:19:00 -0800133 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200134 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800135 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200136 goto new_segment;
137
138 (*sg)->length += nbytes;
139 } else {
140new_segment:
141 if (!*sg)
142 *sg = sglist;
143 else {
144 /*
145 * If the driver previously mapped a shorter
146 * list, we could see a termination bit
147 * prematurely unless it fully inits the sg
148 * table on each mapping. We KNOW that there
149 * must be more entries here or the driver
150 * would be buggy, so force clear the
151 * termination bit to avoid doing a full
152 * sg_init_table() in drivers for each command.
153 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030154 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200155 *sg = sg_next(*sg);
156 }
157
158 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
159 (*nsegs)++;
160 }
Kent Overstreet79886132013-11-23 17:19:00 -0800161 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200162}
163
Jens Axboed6d48192008-01-29 14:04:06 +0100164/*
165 * map a request to scatterlist, return number of sg entries setup. Caller
166 * must make sure sg can hold rq->nr_phys_segments entries
167 */
168int blk_rq_map_sg(struct request_queue *q, struct request *rq,
169 struct scatterlist *sglist)
170{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700171 struct bio_vec bvec, bvprv = { NULL };
Jens Axboed6d48192008-01-29 14:04:06 +0100172 struct req_iterator iter;
173 struct scatterlist *sg;
174 int nsegs, cluster;
175
176 nsegs = 0;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100177 cluster = blk_queue_cluster(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100178
179 /*
180 * for each bio in rq
181 */
Jens Axboed6d48192008-01-29 14:04:06 +0100182 sg = NULL;
183 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800184 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg,
Asias He963ab9e2012-08-02 23:42:03 +0200185 &nsegs, &cluster);
Jens Axboed6d48192008-01-29 14:04:06 +0100186 } /* segments in rq */
187
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200188
189 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900190 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
191 unsigned int pad_len =
192 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200193
194 sg->length += pad_len;
195 rq->extra_len += pad_len;
196 }
197
Tejun Heo2fb98e82008-02-19 11:36:53 +0100198 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200199 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100200 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
201
Jens Axboed6d48192008-01-29 14:04:06 +0100202 sg->page_link &= ~0x02;
203 sg = sg_next(sg);
204 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
205 q->dma_drain_size,
206 ((unsigned long)q->dma_drain_buffer) &
207 (PAGE_SIZE - 1));
208 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100209 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100210 }
211
212 if (sg)
213 sg_mark_end(sg);
214
215 return nsegs;
216}
Jens Axboed6d48192008-01-29 14:04:06 +0100217EXPORT_SYMBOL(blk_rq_map_sg);
218
Asias He85b9f662012-08-02 23:42:04 +0200219/**
220 * blk_bio_map_sg - map a bio to a scatterlist
221 * @q: request_queue in question
222 * @bio: bio being mapped
223 * @sglist: scatterlist being mapped
224 *
225 * Note:
226 * Caller must make sure sg can hold bio->bi_phys_segments entries
227 *
228 * Will return the number of sg entries setup
229 */
230int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
231 struct scatterlist *sglist)
232{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700233 struct bio_vec bvec, bvprv = { NULL };
Asias He85b9f662012-08-02 23:42:04 +0200234 struct scatterlist *sg;
235 int nsegs, cluster;
Kent Overstreet79886132013-11-23 17:19:00 -0800236 struct bvec_iter iter;
Asias He85b9f662012-08-02 23:42:04 +0200237
238 nsegs = 0;
239 cluster = blk_queue_cluster(q);
240
Asias He85b9f662012-08-02 23:42:04 +0200241 sg = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800242 bio_for_each_segment(bvec, bio, iter) {
243 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg,
Asias He85b9f662012-08-02 23:42:04 +0200244 &nsegs, &cluster);
245 } /* segments in bio */
246
247 if (sg)
248 sg_mark_end(sg);
249
250 BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
251 return nsegs;
252}
253EXPORT_SYMBOL(blk_bio_map_sg);
254
Jens Axboed6d48192008-01-29 14:04:06 +0100255static inline int ll_new_hw_segment(struct request_queue *q,
256 struct request *req,
257 struct bio *bio)
258{
Jens Axboed6d48192008-01-29 14:04:06 +0100259 int nr_phys_segs = bio_phys_segments(q, bio);
260
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200261 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
262 goto no_merge;
263
264 if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio))
265 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100266
267 /*
268 * This will form the start of a new hw segment. Bump both
269 * counters.
270 */
Jens Axboed6d48192008-01-29 14:04:06 +0100271 req->nr_phys_segments += nr_phys_segs;
272 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200273
274no_merge:
275 req->cmd_flags |= REQ_NOMERGE;
276 if (req == q->last_merge)
277 q->last_merge = NULL;
278 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100279}
280
281int ll_back_merge_fn(struct request_queue *q, struct request *req,
282 struct bio *bio)
283{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400284 if (blk_rq_sectors(req) + bio_sectors(bio) >
285 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100286 req->cmd_flags |= REQ_NOMERGE;
287 if (req == q->last_merge)
288 q->last_merge = NULL;
289 return 0;
290 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200291 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100292 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200293 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100294 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100295
296 return ll_new_hw_segment(q, req, bio);
297}
298
Jens Axboe6728cb02008-01-31 13:03:55 +0100299int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100300 struct bio *bio)
301{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400302 if (blk_rq_sectors(req) + bio_sectors(bio) >
303 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100304 req->cmd_flags |= REQ_NOMERGE;
305 if (req == q->last_merge)
306 q->last_merge = NULL;
307 return 0;
308 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200309 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100310 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200311 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100312 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100313
314 return ll_new_hw_segment(q, req, bio);
315}
316
Jens Axboee7e24502013-10-29 12:11:47 -0600317/*
318 * blk-mq uses req->special to carry normal driver per-request payload, it
319 * does not indicate a prepared command that we cannot merge with.
320 */
321static bool req_no_special_merge(struct request *req)
322{
323 struct request_queue *q = req->q;
324
325 return !q->mq_ops && req->special;
326}
327
Jens Axboed6d48192008-01-29 14:04:06 +0100328static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
329 struct request *next)
330{
331 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200332 unsigned int seg_size =
333 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100334
335 /*
336 * First check if the either of the requests are re-queued
337 * requests. Can't merge them if they are.
338 */
Jens Axboee7e24502013-10-29 12:11:47 -0600339 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100340 return 0;
341
342 /*
343 * Will it become too large?
344 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400345 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
346 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100347 return 0;
348
349 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200350 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
351 if (req->nr_phys_segments == 1)
352 req->bio->bi_seg_front_size = seg_size;
353 if (next->nr_phys_segments == 1)
354 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100355 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200356 }
Jens Axboed6d48192008-01-29 14:04:06 +0100357
Martin K. Petersen8a783622010-02-26 00:20:39 -0500358 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100359 return 0;
360
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200361 if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next))
362 return 0;
363
Jens Axboed6d48192008-01-29 14:04:06 +0100364 /* Merge is OK... */
365 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100366 return 1;
367}
368
Tejun Heo80a761f2009-07-03 17:48:17 +0900369/**
370 * blk_rq_set_mixed_merge - mark a request as mixed merge
371 * @rq: request to mark as mixed merge
372 *
373 * Description:
374 * @rq is about to be mixed merged. Make sure the attributes
375 * which can be mixed are set in each bio and mark @rq as mixed
376 * merged.
377 */
378void blk_rq_set_mixed_merge(struct request *rq)
379{
380 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
381 struct bio *bio;
382
383 if (rq->cmd_flags & REQ_MIXED_MERGE)
384 return;
385
386 /*
387 * @rq will no longer represent mixable attributes for all the
388 * contained bios. It will just track those of the first one.
389 * Distributes the attributs to each bio.
390 */
391 for (bio = rq->bio; bio; bio = bio->bi_next) {
392 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
393 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
394 bio->bi_rw |= ff;
395 }
396 rq->cmd_flags |= REQ_MIXED_MERGE;
397}
398
Jerome Marchand26308ea2009-03-27 10:31:51 +0100399static void blk_account_io_merge(struct request *req)
400{
401 if (blk_do_io_stat(req)) {
402 struct hd_struct *part;
403 int cpu;
404
405 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100406 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100407
408 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200409 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100410
Jens Axboe6c23a962011-01-07 08:43:37 +0100411 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100412 part_stat_unlock();
413 }
414}
415
Jens Axboed6d48192008-01-29 14:04:06 +0100416/*
417 * Has to be called with the request spinlock acquired
418 */
419static int attempt_merge(struct request_queue *q, struct request *req,
420 struct request *next)
421{
422 if (!rq_mergeable(req) || !rq_mergeable(next))
423 return 0;
424
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400425 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
426 return 0;
427
Jens Axboed6d48192008-01-29 14:04:06 +0100428 /*
429 * not contiguous
430 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900431 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100432 return 0;
433
434 if (rq_data_dir(req) != rq_data_dir(next)
435 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600436 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100437 return 0;
438
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400439 if (req->cmd_flags & REQ_WRITE_SAME &&
440 !blk_write_same_mergeable(req->bio, next->bio))
441 return 0;
442
Jens Axboed6d48192008-01-29 14:04:06 +0100443 /*
444 * If we are allowed to merge, then append bio list
445 * from next to rq and release next. merge_requests_fn
446 * will have updated segment counts, update sector
447 * counts here.
448 */
449 if (!ll_merge_requests_fn(q, req, next))
450 return 0;
451
452 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900453 * If failfast settings disagree or any of the two is already
454 * a mixed merge, mark both as mixed before proceeding. This
455 * makes sure that all involved bios have mixable attributes
456 * set properly.
457 */
458 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
459 (req->cmd_flags & REQ_FAILFAST_MASK) !=
460 (next->cmd_flags & REQ_FAILFAST_MASK)) {
461 blk_rq_set_mixed_merge(req);
462 blk_rq_set_mixed_merge(next);
463 }
464
465 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100466 * At this point we have either done a back merge
467 * or front merge. We need the smaller start_time of
468 * the merged requests to be the current request
469 * for accounting purposes.
470 */
471 if (time_after(req->start_time, next->start_time))
472 req->start_time = next->start_time;
473
474 req->biotail->bi_next = next->bio;
475 req->biotail = next->biotail;
476
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900477 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100478
479 elv_merge_requests(q, req, next);
480
Jerome Marchand42dad762009-04-22 14:01:49 +0200481 /*
482 * 'next' is going away, so update stats accordingly
483 */
484 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100485
486 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200487 if (blk_rq_cpu_valid(next))
488 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100489
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100490 /* owner-ship of bio passed from next to req */
491 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100492 __blk_put_request(q, next);
493 return 1;
494}
495
496int attempt_back_merge(struct request_queue *q, struct request *rq)
497{
498 struct request *next = elv_latter_request(q, rq);
499
500 if (next)
501 return attempt_merge(q, rq, next);
502
503 return 0;
504}
505
506int attempt_front_merge(struct request_queue *q, struct request *rq)
507{
508 struct request *prev = elv_former_request(q, rq);
509
510 if (prev)
511 return attempt_merge(q, prev, rq);
512
513 return 0;
514}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100515
516int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
517 struct request *next)
518{
519 return attempt_merge(q, rq, next);
520}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100521
522bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
523{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400524 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100525 return false;
526
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400527 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
528 return false;
529
Tejun Heo050c8ea2012-02-08 09:19:38 +0100530 /* different data direction or already started, don't merge */
531 if (bio_data_dir(bio) != rq_data_dir(rq))
532 return false;
533
534 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600535 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100536 return false;
537
538 /* only merge integrity protected bio into ditto rq */
539 if (bio_integrity(bio) != blk_integrity_rq(rq))
540 return false;
541
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400542 /* must be using the same buffer */
543 if (rq->cmd_flags & REQ_WRITE_SAME &&
544 !blk_write_same_mergeable(rq->bio, bio))
545 return false;
546
Tejun Heo050c8ea2012-02-08 09:19:38 +0100547 return true;
548}
549
550int blk_try_merge(struct request *rq, struct bio *bio)
551{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700552 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100553 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700554 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100555 return ELEVATOR_FRONT_MERGE;
556 return ELEVATOR_NO_MERGE;
557}