blob: 5d74cc354e30d3c6538b4d2cb8e01588d78383ef [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{
Jens Axboed6d48192008-01-29 14:04:06 +010015 struct bio_vec *bv, *bvprv = NULL;
Jens Axboe1e428072009-02-23 09:03:10 +010016 int cluster, i, high, highprv = 1;
17 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +010018 struct bio *fbio, *bbio;
Jens Axboed6d48192008-01-29 14:04:06 +010019
Jens Axboe1e428072009-02-23 09:03:10 +010020 if (!bio)
21 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +010022
Jens Axboe1e428072009-02-23 09:03:10 +010023 fbio = bio;
Martin K. Petersene692cb62010-12-01 19:41:49 +010024 cluster = blk_queue_cluster(q);
Mikulas Patocka5df97b92008-08-15 10:20:02 +020025 seg_size = 0;
Andi Kleen2c8919d2010-06-21 11:02:47 +020026 nr_phys_segs = 0;
Jens Axboe1e428072009-02-23 09:03:10 +010027 for_each_bio(bio) {
28 bio_for_each_segment(bv, bio, i) {
29 /*
30 * the trick here is making sure that a high page is
31 * never considered part of another segment, since that
32 * might change with the bounce page.
33 */
Martin K. Petersenae03bf62009-05-22 17:17:50 -040034 high = page_to_pfn(bv->bv_page) > queue_bounce_pfn(q);
Jens Axboe1e428072009-02-23 09:03:10 +010035 if (high || highprv)
Jens Axboed6d48192008-01-29 14:04:06 +010036 goto new_segment;
Jens Axboe1e428072009-02-23 09:03:10 +010037 if (cluster) {
Martin K. Petersenae03bf62009-05-22 17:17:50 -040038 if (seg_size + bv->bv_len
39 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +010040 goto new_segment;
41 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
42 goto new_segment;
43 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
44 goto new_segment;
Subhash Jadavani6e25ce32013-01-10 02:15:13 +053045 if ((bvprv->bv_page != bv->bv_page) &&
46 (bvprv->bv_page + 1) != bv->bv_page)
47 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +010048
Jens Axboe1e428072009-02-23 09:03:10 +010049 seg_size += bv->bv_len;
50 bvprv = bv;
51 continue;
52 }
Jens Axboed6d48192008-01-29 14:04:06 +010053new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +010054 if (nr_phys_segs == 1 && seg_size >
55 fbio->bi_seg_front_size)
56 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +020057
Jens Axboe1e428072009-02-23 09:03:10 +010058 nr_phys_segs++;
59 bvprv = bv;
60 seg_size = bv->bv_len;
61 highprv = high;
62 }
Jens Axboe59247ea2009-03-06 08:55:24 +010063 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +010064 }
65
Jens Axboe59247ea2009-03-06 08:55:24 +010066 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
67 fbio->bi_seg_front_size = seg_size;
68 if (seg_size > bbio->bi_seg_back_size)
69 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +010070
71 return nr_phys_segs;
72}
73
74void blk_recalc_rq_segments(struct request *rq)
75{
Jens Axboe59247ea2009-03-06 08:55:24 +010076 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
Jens Axboed6d48192008-01-29 14:04:06 +010077}
78
79void blk_recount_segments(struct request_queue *q, struct bio *bio)
80{
Jens Axboed6d48192008-01-29 14:04:06 +010081 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +010082
Jens Axboed6d48192008-01-29 14:04:06 +010083 bio->bi_next = NULL;
Jens Axboe59247ea2009-03-06 08:55:24 +010084 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +010085 bio->bi_next = nxt;
Jens Axboed6d48192008-01-29 14:04:06 +010086 bio->bi_flags |= (1 << BIO_SEG_VALID);
87}
88EXPORT_SYMBOL(blk_recount_segments);
89
90static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
91 struct bio *nxt)
92{
Martin K. Petersene692cb62010-12-01 19:41:49 +010093 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +010094 return 0;
95
FUJITA Tomonori86771422008-10-13 14:19:05 +020096 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -040097 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +010098 return 0;
99
David Woodhousee17fc0a2008-08-09 16:42:20 +0100100 if (!bio_has_data(bio))
101 return 1;
102
103 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
104 return 0;
105
Jens Axboed6d48192008-01-29 14:04:06 +0100106 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100107 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100108 * these two to be merged into one
109 */
110 if (BIO_SEG_BOUNDARY(q, bio, nxt))
111 return 1;
112
113 return 0;
114}
115
Jens Axboed6d48192008-01-29 14:04:06 +0100116/*
117 * map a request to scatterlist, return number of sg entries setup. Caller
118 * must make sure sg can hold rq->nr_phys_segments entries
119 */
120int blk_rq_map_sg(struct request_queue *q, struct request *rq,
121 struct scatterlist *sglist)
122{
123 struct bio_vec *bvec, *bvprv;
124 struct req_iterator iter;
125 struct scatterlist *sg;
126 int nsegs, cluster;
127
128 nsegs = 0;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100129 cluster = blk_queue_cluster(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100130
131 /*
132 * for each bio in rq
133 */
134 bvprv = NULL;
135 sg = NULL;
136 rq_for_each_segment(bvec, rq, iter) {
137 int nbytes = bvec->bv_len;
138
139 if (bvprv && cluster) {
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400140 if (sg->length + nbytes > queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100141 goto new_segment;
142
143 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
144 goto new_segment;
145 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
146 goto new_segment;
Subhash Jadavani6e25ce32013-01-10 02:15:13 +0530147 if ((bvprv->bv_page != bvec->bv_page) &&
148 ((bvprv->bv_page + 1) != bvec->bv_page))
149 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100150
151 sg->length += nbytes;
152 } else {
153new_segment:
154 if (!sg)
155 sg = sglist;
156 else {
157 /*
158 * If the driver previously mapped a shorter
159 * list, we could see a termination bit
160 * prematurely unless it fully inits the sg
161 * table on each mapping. We KNOW that there
162 * must be more entries here or the driver
163 * would be buggy, so force clear the
164 * termination bit to avoid doing a full
165 * sg_init_table() in drivers for each command.
166 */
167 sg->page_link &= ~0x02;
168 sg = sg_next(sg);
169 }
170
171 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
172 nsegs++;
173 }
174 bvprv = bvec;
175 } /* segments in rq */
176
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200177
178 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900179 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
180 unsigned int pad_len =
181 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200182
183 sg->length += pad_len;
184 rq->extra_len += pad_len;
185 }
186
Tejun Heo2fb98e82008-02-19 11:36:53 +0100187 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200188 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100189 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
190
Jens Axboed6d48192008-01-29 14:04:06 +0100191 sg->page_link &= ~0x02;
192 sg = sg_next(sg);
193 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
194 q->dma_drain_size,
195 ((unsigned long)q->dma_drain_buffer) &
196 (PAGE_SIZE - 1));
197 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100198 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100199 }
200
201 if (sg)
202 sg_mark_end(sg);
203
204 return nsegs;
205}
Jens Axboed6d48192008-01-29 14:04:06 +0100206EXPORT_SYMBOL(blk_rq_map_sg);
207
Jens Axboed6d48192008-01-29 14:04:06 +0100208static inline int ll_new_hw_segment(struct request_queue *q,
209 struct request *req,
210 struct bio *bio)
211{
Jens Axboed6d48192008-01-29 14:04:06 +0100212 int nr_phys_segs = bio_phys_segments(q, bio);
213
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200214 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
215 goto no_merge;
216
217 if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio))
218 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100219
220 /*
221 * This will form the start of a new hw segment. Bump both
222 * counters.
223 */
Jens Axboed6d48192008-01-29 14:04:06 +0100224 req->nr_phys_segments += nr_phys_segs;
225 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200226
227no_merge:
228 req->cmd_flags |= REQ_NOMERGE;
229 if (req == q->last_merge)
230 q->last_merge = NULL;
231 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100232}
233
234int ll_back_merge_fn(struct request_queue *q, struct request *req,
235 struct bio *bio)
236{
237 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100238
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200239 if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC))
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400240 max_sectors = queue_max_hw_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100241 else
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400242 max_sectors = queue_max_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100243
Tejun Heo83096eb2009-05-07 22:24:39 +0900244 if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
Jens Axboed6d48192008-01-29 14:04:06 +0100245 req->cmd_flags |= REQ_NOMERGE;
246 if (req == q->last_merge)
247 q->last_merge = NULL;
248 return 0;
249 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200250 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100251 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200252 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100253 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100254
255 return ll_new_hw_segment(q, req, bio);
256}
257
Jens Axboe6728cb02008-01-31 13:03:55 +0100258int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100259 struct bio *bio)
260{
261 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100262
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200263 if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC))
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400264 max_sectors = queue_max_hw_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100265 else
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400266 max_sectors = queue_max_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100267
268
Tejun Heo83096eb2009-05-07 22:24:39 +0900269 if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
Jens Axboed6d48192008-01-29 14:04:06 +0100270 req->cmd_flags |= REQ_NOMERGE;
271 if (req == q->last_merge)
272 q->last_merge = NULL;
273 return 0;
274 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200275 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100276 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200277 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100278 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100279
280 return ll_new_hw_segment(q, req, bio);
281}
282
283static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
284 struct request *next)
285{
286 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200287 unsigned int seg_size =
288 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100289
290 /*
291 * First check if the either of the requests are re-queued
292 * requests. Can't merge them if they are.
293 */
294 if (req->special || next->special)
295 return 0;
296
297 /*
298 * Will it become too large?
299 */
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400300 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > queue_max_sectors(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100301 return 0;
302
303 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200304 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
305 if (req->nr_phys_segments == 1)
306 req->bio->bi_seg_front_size = seg_size;
307 if (next->nr_phys_segments == 1)
308 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100309 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200310 }
Jens Axboed6d48192008-01-29 14:04:06 +0100311
Martin K. Petersen8a783622010-02-26 00:20:39 -0500312 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100313 return 0;
314
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200315 if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next))
316 return 0;
317
Jens Axboed6d48192008-01-29 14:04:06 +0100318 /* Merge is OK... */
319 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100320 return 1;
321}
322
Tejun Heo80a761f2009-07-03 17:48:17 +0900323/**
324 * blk_rq_set_mixed_merge - mark a request as mixed merge
325 * @rq: request to mark as mixed merge
326 *
327 * Description:
328 * @rq is about to be mixed merged. Make sure the attributes
329 * which can be mixed are set in each bio and mark @rq as mixed
330 * merged.
331 */
332void blk_rq_set_mixed_merge(struct request *rq)
333{
334 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
335 struct bio *bio;
336
337 if (rq->cmd_flags & REQ_MIXED_MERGE)
338 return;
339
340 /*
341 * @rq will no longer represent mixable attributes for all the
342 * contained bios. It will just track those of the first one.
343 * Distributes the attributs to each bio.
344 */
345 for (bio = rq->bio; bio; bio = bio->bi_next) {
346 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
347 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
348 bio->bi_rw |= ff;
349 }
350 rq->cmd_flags |= REQ_MIXED_MERGE;
351}
352
Jerome Marchand26308ea2009-03-27 10:31:51 +0100353static void blk_account_io_merge(struct request *req)
354{
355 if (blk_do_io_stat(req)) {
356 struct hd_struct *part;
357 int cpu;
358
359 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100360 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100361
362 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200363 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100364
Jens Axboe6c23a962011-01-07 08:43:37 +0100365 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100366 part_stat_unlock();
367 }
368}
369
Jens Axboed6d48192008-01-29 14:04:06 +0100370/*
371 * Has to be called with the request spinlock acquired
372 */
373static int attempt_merge(struct request_queue *q, struct request *req,
374 struct request *next)
375{
376 if (!rq_mergeable(req) || !rq_mergeable(next))
377 return 0;
378
379 /*
Adrian Hunterf281fb52010-09-25 12:42:55 +0200380 * Don't merge file system requests and discard requests
381 */
382 if ((req->cmd_flags & REQ_DISCARD) != (next->cmd_flags & REQ_DISCARD))
383 return 0;
384
385 /*
386 * Don't merge discard requests and secure discard requests
387 */
388 if ((req->cmd_flags & REQ_SECURE) != (next->cmd_flags & REQ_SECURE))
389 return 0;
390
391 /*
Maya Erez73937f52012-05-24 23:33:05 +0300392 * Don't merge file system requests and sanitize requests
393 */
394 if ((req->cmd_flags & REQ_SANITIZE) != (next->cmd_flags & REQ_SANITIZE))
395 return 0;
396
397 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100398 * not contiguous
399 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900400 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100401 return 0;
402
403 if (rq_data_dir(req) != rq_data_dir(next)
404 || req->rq_disk != next->rq_disk
405 || next->special)
406 return 0;
407
408 /*
409 * If we are allowed to merge, then append bio list
410 * from next to rq and release next. merge_requests_fn
411 * will have updated segment counts, update sector
412 * counts here.
413 */
414 if (!ll_merge_requests_fn(q, req, next))
415 return 0;
416
417 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900418 * If failfast settings disagree or any of the two is already
419 * a mixed merge, mark both as mixed before proceeding. This
420 * makes sure that all involved bios have mixable attributes
421 * set properly.
422 */
423 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
424 (req->cmd_flags & REQ_FAILFAST_MASK) !=
425 (next->cmd_flags & REQ_FAILFAST_MASK)) {
426 blk_rq_set_mixed_merge(req);
427 blk_rq_set_mixed_merge(next);
428 }
429
430 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100431 * At this point we have either done a back merge
432 * or front merge. We need the smaller start_time of
433 * the merged requests to be the current request
434 * for accounting purposes.
435 */
436 if (time_after(req->start_time, next->start_time))
437 req->start_time = next->start_time;
438
439 req->biotail->bi_next = next->bio;
440 req->biotail = next->biotail;
441
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900442 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100443
444 elv_merge_requests(q, req, next);
445
Jerome Marchand42dad762009-04-22 14:01:49 +0200446 /*
447 * 'next' is going away, so update stats accordingly
448 */
449 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100450
451 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200452 if (blk_rq_cpu_valid(next))
453 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100454
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100455 /* owner-ship of bio passed from next to req */
456 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100457 __blk_put_request(q, next);
458 return 1;
459}
460
461int attempt_back_merge(struct request_queue *q, struct request *rq)
462{
463 struct request *next = elv_latter_request(q, rq);
464
465 if (next)
466 return attempt_merge(q, rq, next);
467
468 return 0;
469}
470
471int attempt_front_merge(struct request_queue *q, struct request *rq)
472{
473 struct request *prev = elv_former_request(q, rq);
474
475 if (prev)
476 return attempt_merge(q, prev, rq);
477
478 return 0;
479}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100480
481int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
482 struct request *next)
483{
484 return attempt_merge(q, rq, next);
485}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100486
487bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
488{
489 if (!rq_mergeable(rq))
490 return false;
491
492 /* don't merge file system requests and discard requests */
493 if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
494 return false;
495
496 /* don't merge discard requests and secure discard requests */
497 if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
498 return false;
499
500 /* different data direction or already started, don't merge */
501 if (bio_data_dir(bio) != rq_data_dir(rq))
502 return false;
503
504 /* must be same device and not a special request */
505 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
506 return false;
507
508 /* only merge integrity protected bio into ditto rq */
509 if (bio_integrity(bio) != blk_integrity_rq(rq))
510 return false;
511
512 return true;
513}
514
515int blk_try_merge(struct request *rq, struct bio *bio)
516{
517 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_sector)
518 return ELEVATOR_BACK_MERGE;
519 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_sector)
520 return ELEVATOR_FRONT_MERGE;
521 return ELEVATOR_NO_MERGE;
522}