blob: b3ac40aef46b317c5a432a92ba9b40c8e1942504 [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 Lei764f6122014-10-09 23:17:35 +0800100 bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
101 &q->queue_flags);
Ming Lei76d81372014-10-22 08:30:30 +0800102 bool merge_not_need = bio->bi_vcnt < queue_max_segments(q);
Ming Lei764f6122014-10-09 23:17:35 +0800103
104 if (no_sg_merge && !bio_flagged(bio, BIO_CLONED) &&
Ming Lei76d81372014-10-22 08:30:30 +0800105 merge_not_need)
Jens Axboe05f1dd52014-05-29 09:53:32 -0600106 bio->bi_phys_segments = bio->bi_vcnt;
107 else {
108 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +0100109
Jens Axboe05f1dd52014-05-29 09:53:32 -0600110 bio->bi_next = NULL;
Ming Lei764f6122014-10-09 23:17:35 +0800111 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio,
Ming Lei76d81372014-10-22 08:30:30 +0800112 no_sg_merge && merge_not_need);
Jens Axboe05f1dd52014-05-29 09:53:32 -0600113 bio->bi_next = nxt;
114 }
115
Jens Axboed6d48192008-01-29 14:04:06 +0100116 bio->bi_flags |= (1 << BIO_SEG_VALID);
117}
118EXPORT_SYMBOL(blk_recount_segments);
119
120static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
121 struct bio *nxt)
122{
Kent Overstreet2b8221e2013-12-03 14:29:09 -0700123 struct bio_vec end_bv = { NULL }, nxt_bv;
Kent Overstreetf619d252013-08-07 14:30:33 -0700124 struct bvec_iter iter;
125
Martin K. Petersene692cb62010-12-01 19:41:49 +0100126 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100127 return 0;
128
FUJITA Tomonori86771422008-10-13 14:19:05 +0200129 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400130 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100131 return 0;
132
David Woodhousee17fc0a2008-08-09 16:42:20 +0100133 if (!bio_has_data(bio))
134 return 1;
135
Kent Overstreetf619d252013-08-07 14:30:33 -0700136 bio_for_each_segment(end_bv, bio, iter)
137 if (end_bv.bv_len == iter.bi_size)
138 break;
139
140 nxt_bv = bio_iovec(nxt);
141
142 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100143 return 0;
144
Jens Axboed6d48192008-01-29 14:04:06 +0100145 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100146 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100147 * these two to be merged into one
148 */
Kent Overstreetf619d252013-08-07 14:30:33 -0700149 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
Jens Axboed6d48192008-01-29 14:04:06 +0100150 return 1;
151
152 return 0;
153}
154
Kent Overstreet79886132013-11-23 17:19:00 -0800155static inline void
Asias He963ab9e2012-08-02 23:42:03 +0200156__blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
Kent Overstreet79886132013-11-23 17:19:00 -0800157 struct scatterlist *sglist, struct bio_vec *bvprv,
Asias He963ab9e2012-08-02 23:42:03 +0200158 struct scatterlist **sg, int *nsegs, int *cluster)
159{
160
161 int nbytes = bvec->bv_len;
162
Kent Overstreet79886132013-11-23 17:19:00 -0800163 if (*sg && *cluster) {
Asias He963ab9e2012-08-02 23:42:03 +0200164 if ((*sg)->length + nbytes > queue_max_segment_size(q))
165 goto new_segment;
166
Kent Overstreet79886132013-11-23 17:19:00 -0800167 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200168 goto new_segment;
Kent Overstreet79886132013-11-23 17:19:00 -0800169 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
Asias He963ab9e2012-08-02 23:42:03 +0200170 goto new_segment;
171
172 (*sg)->length += nbytes;
173 } else {
174new_segment:
175 if (!*sg)
176 *sg = sglist;
177 else {
178 /*
179 * If the driver previously mapped a shorter
180 * list, we could see a termination bit
181 * prematurely unless it fully inits the sg
182 * table on each mapping. We KNOW that there
183 * must be more entries here or the driver
184 * would be buggy, so force clear the
185 * termination bit to avoid doing a full
186 * sg_init_table() in drivers for each command.
187 */
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030188 sg_unmark_end(*sg);
Asias He963ab9e2012-08-02 23:42:03 +0200189 *sg = sg_next(*sg);
190 }
191
192 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
193 (*nsegs)++;
194 }
Kent Overstreet79886132013-11-23 17:19:00 -0800195 *bvprv = *bvec;
Asias He963ab9e2012-08-02 23:42:03 +0200196}
197
Kent Overstreet5cb88502014-02-07 13:53:46 -0700198static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
199 struct scatterlist *sglist,
200 struct scatterlist **sg)
201{
202 struct bio_vec bvec, bvprv = { NULL };
203 struct bvec_iter iter;
204 int nsegs, cluster;
205
206 nsegs = 0;
207 cluster = blk_queue_cluster(q);
208
209 if (bio->bi_rw & REQ_DISCARD) {
210 /*
211 * This is a hack - drivers should be neither modifying the
212 * biovec, nor relying on bi_vcnt - but because of
213 * blk_add_request_payload(), a discard bio may or may not have
214 * a payload we need to set up here (thank you Christoph) and
215 * bi_vcnt is really the only way of telling if we need to.
216 */
217
218 if (bio->bi_vcnt)
219 goto single_segment;
220
221 return 0;
222 }
223
224 if (bio->bi_rw & REQ_WRITE_SAME) {
225single_segment:
226 *sg = sglist;
227 bvec = bio_iovec(bio);
228 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
229 return 1;
230 }
231
232 for_each_bio(bio)
233 bio_for_each_segment(bvec, bio, iter)
234 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
235 &nsegs, &cluster);
236
237 return nsegs;
238}
239
Jens Axboed6d48192008-01-29 14:04:06 +0100240/*
241 * map a request to scatterlist, return number of sg entries setup. Caller
242 * must make sure sg can hold rq->nr_phys_segments entries
243 */
244int blk_rq_map_sg(struct request_queue *q, struct request *rq,
245 struct scatterlist *sglist)
246{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700247 struct scatterlist *sg = NULL;
248 int nsegs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100249
Kent Overstreet5cb88502014-02-07 13:53:46 -0700250 if (rq->bio)
251 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200252
253 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900254 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
255 unsigned int pad_len =
256 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200257
258 sg->length += pad_len;
259 rq->extra_len += pad_len;
260 }
261
Tejun Heo2fb98e82008-02-19 11:36:53 +0100262 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200263 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100264 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
265
Jens Axboed6d48192008-01-29 14:04:06 +0100266 sg->page_link &= ~0x02;
267 sg = sg_next(sg);
268 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
269 q->dma_drain_size,
270 ((unsigned long)q->dma_drain_buffer) &
271 (PAGE_SIZE - 1));
272 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100273 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100274 }
275
276 if (sg)
277 sg_mark_end(sg);
278
279 return nsegs;
280}
Jens Axboed6d48192008-01-29 14:04:06 +0100281EXPORT_SYMBOL(blk_rq_map_sg);
282
Asias He85b9f662012-08-02 23:42:04 +0200283/**
284 * blk_bio_map_sg - map a bio to a scatterlist
285 * @q: request_queue in question
286 * @bio: bio being mapped
287 * @sglist: scatterlist being mapped
288 *
289 * Note:
290 * Caller must make sure sg can hold bio->bi_phys_segments entries
291 *
292 * Will return the number of sg entries setup
293 */
294int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
295 struct scatterlist *sglist)
296{
Kent Overstreet5cb88502014-02-07 13:53:46 -0700297 struct scatterlist *sg = NULL;
298 int nsegs;
299 struct bio *next = bio->bi_next;
300 bio->bi_next = NULL;
Asias He85b9f662012-08-02 23:42:04 +0200301
Kent Overstreet5cb88502014-02-07 13:53:46 -0700302 nsegs = __blk_bios_map_sg(q, bio, sglist, &sg);
303 bio->bi_next = next;
Asias He85b9f662012-08-02 23:42:04 +0200304 if (sg)
305 sg_mark_end(sg);
306
307 BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
308 return nsegs;
309}
310EXPORT_SYMBOL(blk_bio_map_sg);
311
Jens Axboed6d48192008-01-29 14:04:06 +0100312static inline int ll_new_hw_segment(struct request_queue *q,
313 struct request *req,
314 struct bio *bio)
315{
Jens Axboed6d48192008-01-29 14:04:06 +0100316 int nr_phys_segs = bio_phys_segments(q, bio);
317
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200318 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
319 goto no_merge;
320
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400321 if (blk_integrity_merge_bio(q, req, bio) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200322 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100323
324 /*
325 * This will form the start of a new hw segment. Bump both
326 * counters.
327 */
Jens Axboed6d48192008-01-29 14:04:06 +0100328 req->nr_phys_segments += nr_phys_segs;
329 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200330
331no_merge:
332 req->cmd_flags |= REQ_NOMERGE;
333 if (req == q->last_merge)
334 q->last_merge = NULL;
335 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100336}
337
338int ll_back_merge_fn(struct request_queue *q, struct request *req,
339 struct bio *bio)
340{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400341 if (blk_rq_sectors(req) + bio_sectors(bio) >
342 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100343 req->cmd_flags |= REQ_NOMERGE;
344 if (req == q->last_merge)
345 q->last_merge = NULL;
346 return 0;
347 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200348 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100349 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200350 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100351 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100352
353 return ll_new_hw_segment(q, req, bio);
354}
355
Jens Axboe6728cb02008-01-31 13:03:55 +0100356int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100357 struct bio *bio)
358{
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400359 if (blk_rq_sectors(req) + bio_sectors(bio) >
360 blk_rq_get_max_sectors(req)) {
Jens Axboed6d48192008-01-29 14:04:06 +0100361 req->cmd_flags |= REQ_NOMERGE;
362 if (req == q->last_merge)
363 q->last_merge = NULL;
364 return 0;
365 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200366 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100367 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200368 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100369 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100370
371 return ll_new_hw_segment(q, req, bio);
372}
373
Jens Axboee7e24502013-10-29 12:11:47 -0600374/*
375 * blk-mq uses req->special to carry normal driver per-request payload, it
376 * does not indicate a prepared command that we cannot merge with.
377 */
378static bool req_no_special_merge(struct request *req)
379{
380 struct request_queue *q = req->q;
381
382 return !q->mq_ops && req->special;
383}
384
Jens Axboed6d48192008-01-29 14:04:06 +0100385static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
386 struct request *next)
387{
388 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200389 unsigned int seg_size =
390 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100391
392 /*
393 * First check if the either of the requests are re-queued
394 * requests. Can't merge them if they are.
395 */
Jens Axboee7e24502013-10-29 12:11:47 -0600396 if (req_no_special_merge(req) || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100397 return 0;
398
399 /*
400 * Will it become too large?
401 */
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400402 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
403 blk_rq_get_max_sectors(req))
Jens Axboed6d48192008-01-29 14:04:06 +0100404 return 0;
405
406 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200407 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
408 if (req->nr_phys_segments == 1)
409 req->bio->bi_seg_front_size = seg_size;
410 if (next->nr_phys_segments == 1)
411 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100412 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200413 }
Jens Axboed6d48192008-01-29 14:04:06 +0100414
Martin K. Petersen8a783622010-02-26 00:20:39 -0500415 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100416 return 0;
417
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400418 if (blk_integrity_merge_rq(q, req, next) == false)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200419 return 0;
420
Jens Axboed6d48192008-01-29 14:04:06 +0100421 /* Merge is OK... */
422 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100423 return 1;
424}
425
Tejun Heo80a761f2009-07-03 17:48:17 +0900426/**
427 * blk_rq_set_mixed_merge - mark a request as mixed merge
428 * @rq: request to mark as mixed merge
429 *
430 * Description:
431 * @rq is about to be mixed merged. Make sure the attributes
432 * which can be mixed are set in each bio and mark @rq as mixed
433 * merged.
434 */
435void blk_rq_set_mixed_merge(struct request *rq)
436{
437 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
438 struct bio *bio;
439
440 if (rq->cmd_flags & REQ_MIXED_MERGE)
441 return;
442
443 /*
444 * @rq will no longer represent mixable attributes for all the
445 * contained bios. It will just track those of the first one.
446 * Distributes the attributs to each bio.
447 */
448 for (bio = rq->bio; bio; bio = bio->bi_next) {
449 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
450 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
451 bio->bi_rw |= ff;
452 }
453 rq->cmd_flags |= REQ_MIXED_MERGE;
454}
455
Jerome Marchand26308ea2009-03-27 10:31:51 +0100456static void blk_account_io_merge(struct request *req)
457{
458 if (blk_do_io_stat(req)) {
459 struct hd_struct *part;
460 int cpu;
461
462 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100463 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100464
465 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200466 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100467
Jens Axboe6c23a962011-01-07 08:43:37 +0100468 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100469 part_stat_unlock();
470 }
471}
472
Jens Axboed6d48192008-01-29 14:04:06 +0100473/*
474 * Has to be called with the request spinlock acquired
475 */
476static int attempt_merge(struct request_queue *q, struct request *req,
477 struct request *next)
478{
479 if (!rq_mergeable(req) || !rq_mergeable(next))
480 return 0;
481
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400482 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
483 return 0;
484
Jens Axboed6d48192008-01-29 14:04:06 +0100485 /*
486 * not contiguous
487 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900488 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100489 return 0;
490
491 if (rq_data_dir(req) != rq_data_dir(next)
492 || req->rq_disk != next->rq_disk
Jens Axboee7e24502013-10-29 12:11:47 -0600493 || req_no_special_merge(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100494 return 0;
495
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400496 if (req->cmd_flags & REQ_WRITE_SAME &&
497 !blk_write_same_mergeable(req->bio, next->bio))
498 return 0;
499
Jens Axboed6d48192008-01-29 14:04:06 +0100500 /*
501 * If we are allowed to merge, then append bio list
502 * from next to rq and release next. merge_requests_fn
503 * will have updated segment counts, update sector
504 * counts here.
505 */
506 if (!ll_merge_requests_fn(q, req, next))
507 return 0;
508
509 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900510 * If failfast settings disagree or any of the two is already
511 * a mixed merge, mark both as mixed before proceeding. This
512 * makes sure that all involved bios have mixable attributes
513 * set properly.
514 */
515 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
516 (req->cmd_flags & REQ_FAILFAST_MASK) !=
517 (next->cmd_flags & REQ_FAILFAST_MASK)) {
518 blk_rq_set_mixed_merge(req);
519 blk_rq_set_mixed_merge(next);
520 }
521
522 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100523 * At this point we have either done a back merge
524 * or front merge. We need the smaller start_time of
525 * the merged requests to be the current request
526 * for accounting purposes.
527 */
528 if (time_after(req->start_time, next->start_time))
529 req->start_time = next->start_time;
530
531 req->biotail->bi_next = next->bio;
532 req->biotail = next->biotail;
533
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900534 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100535
536 elv_merge_requests(q, req, next);
537
Jerome Marchand42dad762009-04-22 14:01:49 +0200538 /*
539 * 'next' is going away, so update stats accordingly
540 */
541 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100542
543 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200544 if (blk_rq_cpu_valid(next))
545 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100546
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100547 /* owner-ship of bio passed from next to req */
548 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100549 __blk_put_request(q, next);
550 return 1;
551}
552
553int attempt_back_merge(struct request_queue *q, struct request *rq)
554{
555 struct request *next = elv_latter_request(q, rq);
556
557 if (next)
558 return attempt_merge(q, rq, next);
559
560 return 0;
561}
562
563int attempt_front_merge(struct request_queue *q, struct request *rq)
564{
565 struct request *prev = elv_former_request(q, rq);
566
567 if (prev)
568 return attempt_merge(q, prev, rq);
569
570 return 0;
571}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100572
573int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
574 struct request *next)
575{
576 return attempt_merge(q, rq, next);
577}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100578
579bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
580{
Jens Axboe66cb45a2014-06-24 16:22:24 -0600581 struct request_queue *q = rq->q;
582
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400583 if (!rq_mergeable(rq) || !bio_mergeable(bio))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100584 return false;
585
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -0400586 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
587 return false;
588
Tejun Heo050c8ea2012-02-08 09:19:38 +0100589 /* different data direction or already started, don't merge */
590 if (bio_data_dir(bio) != rq_data_dir(rq))
591 return false;
592
593 /* must be same device and not a special request */
Jens Axboee7e24502013-10-29 12:11:47 -0600594 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
Tejun Heo050c8ea2012-02-08 09:19:38 +0100595 return false;
596
597 /* only merge integrity protected bio into ditto rq */
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400598 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100599 return false;
600
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400601 /* must be using the same buffer */
602 if (rq->cmd_flags & REQ_WRITE_SAME &&
603 !blk_write_same_mergeable(rq->bio, bio))
604 return false;
605
Jens Axboe66cb45a2014-06-24 16:22:24 -0600606 if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS)) {
607 struct bio_vec *bprev;
608
609 bprev = &rq->biotail->bi_io_vec[bio->bi_vcnt - 1];
610 if (bvec_gap_to_prev(bprev, bio->bi_io_vec[0].bv_offset))
611 return false;
612 }
613
Tejun Heo050c8ea2012-02-08 09:19:38 +0100614 return true;
615}
616
617int blk_try_merge(struct request *rq, struct bio *bio)
618{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700619 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100620 return ELEVATOR_BACK_MERGE;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700621 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
Tejun Heo050c8ea2012-02-08 09:19:38 +0100622 return ELEVATOR_FRONT_MERGE;
623 return ELEVATOR_NO_MERGE;
624}