blob: 9fa7765bd0debee7bc28d8237959738e50d57486 [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>
Amir Samuelov6a22e462014-05-26 11:44:06 +03009#include <linux/security.h>
Jens Axboed6d48192008-01-29 14:04:06 +010010
11#include "blk.h"
12
Jens Axboe1e428072009-02-23 09:03:10 +010013static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
Jens Axboe59247ea2009-03-06 08:55:24 +010014 struct bio *bio)
Jens Axboed6d48192008-01-29 14:04:06 +010015{
Jens Axboed6d48192008-01-29 14:04:06 +010016 struct bio_vec *bv, *bvprv = NULL;
Jens Axboe1e428072009-02-23 09:03:10 +010017 int cluster, i, high, highprv = 1;
18 unsigned int seg_size, nr_phys_segs;
Jens Axboe59247ea2009-03-06 08:55:24 +010019 struct bio *fbio, *bbio;
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) {
29 bio_for_each_segment(bv, bio, i) {
30 /*
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 */
Martin K. Petersenae03bf62009-05-22 17:17:50 -040035 high = page_to_pfn(bv->bv_page) > queue_bounce_pfn(q);
Jens Axboe1e428072009-02-23 09:03:10 +010036 if (high || highprv)
Jens Axboed6d48192008-01-29 14:04:06 +010037 goto new_segment;
Jens Axboe1e428072009-02-23 09:03:10 +010038 if (cluster) {
Martin K. Petersenae03bf62009-05-22 17:17:50 -040039 if (seg_size + bv->bv_len
40 > queue_max_segment_size(q))
Jens Axboe1e428072009-02-23 09:03:10 +010041 goto new_segment;
42 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
43 goto new_segment;
44 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
45 goto new_segment;
Subhash Jadavani6e25ce32013-01-10 02:15:13 +053046 if ((bvprv->bv_page != bv->bv_page) &&
47 (bvprv->bv_page + 1) != bv->bv_page)
48 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +010049
Jens Axboe1e428072009-02-23 09:03:10 +010050 seg_size += bv->bv_len;
51 bvprv = bv;
52 continue;
53 }
Jens Axboed6d48192008-01-29 14:04:06 +010054new_segment:
Jens Axboe1e428072009-02-23 09:03:10 +010055 if (nr_phys_segs == 1 && seg_size >
56 fbio->bi_seg_front_size)
57 fbio->bi_seg_front_size = seg_size;
FUJITA Tomonori86771422008-10-13 14:19:05 +020058
Jens Axboe1e428072009-02-23 09:03:10 +010059 nr_phys_segs++;
60 bvprv = bv;
61 seg_size = bv->bv_len;
62 highprv = high;
63 }
Jens Axboe59247ea2009-03-06 08:55:24 +010064 bbio = bio;
Jens Axboed6d48192008-01-29 14:04:06 +010065 }
66
Jens Axboe59247ea2009-03-06 08:55:24 +010067 if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
68 fbio->bi_seg_front_size = seg_size;
69 if (seg_size > bbio->bi_seg_back_size)
70 bbio->bi_seg_back_size = seg_size;
Jens Axboe1e428072009-02-23 09:03:10 +010071
72 return nr_phys_segs;
73}
74
75void blk_recalc_rq_segments(struct request *rq)
76{
Jens Axboe59247ea2009-03-06 08:55:24 +010077 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
Jens Axboed6d48192008-01-29 14:04:06 +010078}
79
80void blk_recount_segments(struct request_queue *q, struct bio *bio)
81{
Jens Axboed6d48192008-01-29 14:04:06 +010082 struct bio *nxt = bio->bi_next;
Jens Axboe1e428072009-02-23 09:03:10 +010083
Jens Axboed6d48192008-01-29 14:04:06 +010084 bio->bi_next = NULL;
Jens Axboe59247ea2009-03-06 08:55:24 +010085 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +010086 bio->bi_next = nxt;
Jens Axboed6d48192008-01-29 14:04:06 +010087 bio->bi_flags |= (1 << BIO_SEG_VALID);
88}
89EXPORT_SYMBOL(blk_recount_segments);
90
91static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
92 struct bio *nxt)
93{
Martin K. Petersene692cb62010-12-01 19:41:49 +010094 if (!blk_queue_cluster(q))
Jens Axboed6d48192008-01-29 14:04:06 +010095 return 0;
96
FUJITA Tomonori86771422008-10-13 14:19:05 +020097 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
Martin K. Petersenae03bf62009-05-22 17:17:50 -040098 queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +010099 return 0;
100
David Woodhousee17fc0a2008-08-09 16:42:20 +0100101 if (!bio_has_data(bio))
102 return 1;
103
104 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
105 return 0;
106
Jens Axboed6d48192008-01-29 14:04:06 +0100107 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100108 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100109 * these two to be merged into one
110 */
111 if (BIO_SEG_BOUNDARY(q, bio, nxt))
112 return 1;
113
114 return 0;
115}
116
Jens Axboed6d48192008-01-29 14:04:06 +0100117/*
118 * map a request to scatterlist, return number of sg entries setup. Caller
119 * must make sure sg can hold rq->nr_phys_segments entries
120 */
121int blk_rq_map_sg(struct request_queue *q, struct request *rq,
122 struct scatterlist *sglist)
123{
124 struct bio_vec *bvec, *bvprv;
125 struct req_iterator iter;
126 struct scatterlist *sg;
127 int nsegs, cluster;
128
129 nsegs = 0;
Martin K. Petersene692cb62010-12-01 19:41:49 +0100130 cluster = blk_queue_cluster(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100131
132 /*
133 * for each bio in rq
134 */
135 bvprv = NULL;
136 sg = NULL;
137 rq_for_each_segment(bvec, rq, iter) {
138 int nbytes = bvec->bv_len;
139
140 if (bvprv && cluster) {
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400141 if (sg->length + nbytes > queue_max_segment_size(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100142 goto new_segment;
143
144 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
145 goto new_segment;
146 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
147 goto new_segment;
Subhash Jadavani6e25ce32013-01-10 02:15:13 +0530148 if ((bvprv->bv_page != bvec->bv_page) &&
149 ((bvprv->bv_page + 1) != bvec->bv_page))
150 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +0100151
152 sg->length += nbytes;
153 } else {
154new_segment:
155 if (!sg)
156 sg = sglist;
157 else {
158 /*
159 * If the driver previously mapped a shorter
160 * list, we could see a termination bit
161 * prematurely unless it fully inits the sg
162 * table on each mapping. We KNOW that there
163 * must be more entries here or the driver
164 * would be buggy, so force clear the
165 * termination bit to avoid doing a full
166 * sg_init_table() in drivers for each command.
167 */
168 sg->page_link &= ~0x02;
169 sg = sg_next(sg);
170 }
171
172 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
173 nsegs++;
174 }
175 bvprv = bvec;
176 } /* segments in rq */
177
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200178
179 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900180 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
181 unsigned int pad_len =
182 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200183
184 sg->length += pad_len;
185 rq->extra_len += pad_len;
186 }
187
Tejun Heo2fb98e82008-02-19 11:36:53 +0100188 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200189 if (rq->cmd_flags & REQ_WRITE)
Tejun Heodb0a2e02008-02-19 11:36:55 +0100190 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
191
Jens Axboed6d48192008-01-29 14:04:06 +0100192 sg->page_link &= ~0x02;
193 sg = sg_next(sg);
194 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
195 q->dma_drain_size,
196 ((unsigned long)q->dma_drain_buffer) &
197 (PAGE_SIZE - 1));
198 nsegs++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100199 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100200 }
201
202 if (sg)
203 sg_mark_end(sg);
204
205 return nsegs;
206}
Jens Axboed6d48192008-01-29 14:04:06 +0100207EXPORT_SYMBOL(blk_rq_map_sg);
208
Jens Axboed6d48192008-01-29 14:04:06 +0100209static inline int ll_new_hw_segment(struct request_queue *q,
210 struct request *req,
211 struct bio *bio)
212{
Jens Axboed6d48192008-01-29 14:04:06 +0100213 int nr_phys_segs = bio_phys_segments(q, bio);
214
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200215 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
216 goto no_merge;
217
218 if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio))
219 goto no_merge;
Jens Axboed6d48192008-01-29 14:04:06 +0100220
221 /*
222 * This will form the start of a new hw segment. Bump both
223 * counters.
224 */
Jens Axboed6d48192008-01-29 14:04:06 +0100225 req->nr_phys_segments += nr_phys_segs;
226 return 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200227
228no_merge:
229 req->cmd_flags |= REQ_NOMERGE;
230 if (req == q->last_merge)
231 q->last_merge = NULL;
232 return 0;
Jens Axboed6d48192008-01-29 14:04:06 +0100233}
234
235int ll_back_merge_fn(struct request_queue *q, struct request *req,
236 struct bio *bio)
237{
238 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100239
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200240 if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC))
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400241 max_sectors = queue_max_hw_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100242 else
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400243 max_sectors = queue_max_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100244
Tejun Heo83096eb2009-05-07 22:24:39 +0900245 if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
Jens Axboed6d48192008-01-29 14:04:06 +0100246 req->cmd_flags |= REQ_NOMERGE;
247 if (req == q->last_merge)
248 q->last_merge = NULL;
249 return 0;
250 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200251 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100252 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200253 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100254 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100255
256 return ll_new_hw_segment(q, req, bio);
257}
258
Jens Axboe6728cb02008-01-31 13:03:55 +0100259int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100260 struct bio *bio)
261{
262 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100263
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200264 if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC))
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400265 max_sectors = queue_max_hw_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100266 else
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400267 max_sectors = queue_max_sectors(q);
Jens Axboed6d48192008-01-29 14:04:06 +0100268
269
Tejun Heo83096eb2009-05-07 22:24:39 +0900270 if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
Jens Axboed6d48192008-01-29 14:04:06 +0100271 req->cmd_flags |= REQ_NOMERGE;
272 if (req == q->last_merge)
273 q->last_merge = NULL;
274 return 0;
275 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200276 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100277 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200278 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100279 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100280
281 return ll_new_hw_segment(q, req, bio);
282}
283
284static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
285 struct request *next)
286{
287 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200288 unsigned int seg_size =
289 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100290
291 /*
292 * First check if the either of the requests are re-queued
293 * requests. Can't merge them if they are.
294 */
295 if (req->special || next->special)
296 return 0;
297
298 /*
299 * Will it become too large?
300 */
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400301 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > queue_max_sectors(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100302 return 0;
303
304 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200305 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
306 if (req->nr_phys_segments == 1)
307 req->bio->bi_seg_front_size = seg_size;
308 if (next->nr_phys_segments == 1)
309 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100310 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200311 }
Jens Axboed6d48192008-01-29 14:04:06 +0100312
Martin K. Petersen8a783622010-02-26 00:20:39 -0500313 if (total_phys_segments > queue_max_segments(q))
Jens Axboed6d48192008-01-29 14:04:06 +0100314 return 0;
315
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200316 if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next))
317 return 0;
318
Jens Axboed6d48192008-01-29 14:04:06 +0100319 /* Merge is OK... */
320 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100321 return 1;
322}
323
Tejun Heo80a761f2009-07-03 17:48:17 +0900324/**
325 * blk_rq_set_mixed_merge - mark a request as mixed merge
326 * @rq: request to mark as mixed merge
327 *
328 * Description:
329 * @rq is about to be mixed merged. Make sure the attributes
330 * which can be mixed are set in each bio and mark @rq as mixed
331 * merged.
332 */
333void blk_rq_set_mixed_merge(struct request *rq)
334{
335 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
336 struct bio *bio;
337
338 if (rq->cmd_flags & REQ_MIXED_MERGE)
339 return;
340
341 /*
342 * @rq will no longer represent mixable attributes for all the
343 * contained bios. It will just track those of the first one.
344 * Distributes the attributs to each bio.
345 */
346 for (bio = rq->bio; bio; bio = bio->bi_next) {
347 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
348 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
349 bio->bi_rw |= ff;
350 }
351 rq->cmd_flags |= REQ_MIXED_MERGE;
352}
353
Jerome Marchand26308ea2009-03-27 10:31:51 +0100354static void blk_account_io_merge(struct request *req)
355{
356 if (blk_do_io_stat(req)) {
357 struct hd_struct *part;
358 int cpu;
359
360 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100361 part = req->part;
Jerome Marchand26308ea2009-03-27 10:31:51 +0100362
363 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200364 part_dec_in_flight(part, rq_data_dir(req));
Jerome Marchand26308ea2009-03-27 10:31:51 +0100365
Jens Axboe6c23a962011-01-07 08:43:37 +0100366 hd_struct_put(part);
Jerome Marchand26308ea2009-03-27 10:31:51 +0100367 part_stat_unlock();
368 }
369}
370
Jens Axboed6d48192008-01-29 14:04:06 +0100371/*
372 * Has to be called with the request spinlock acquired
373 */
374static int attempt_merge(struct request_queue *q, struct request *req,
375 struct request *next)
376{
377 if (!rq_mergeable(req) || !rq_mergeable(next))
378 return 0;
379
380 /*
Adrian Hunterf281fb52010-09-25 12:42:55 +0200381 * Don't merge file system requests and discard requests
382 */
383 if ((req->cmd_flags & REQ_DISCARD) != (next->cmd_flags & REQ_DISCARD))
384 return 0;
385
386 /*
387 * Don't merge discard requests and secure discard requests
388 */
389 if ((req->cmd_flags & REQ_SECURE) != (next->cmd_flags & REQ_SECURE))
390 return 0;
391
392 /*
Maya Erez73937f52012-05-24 23:33:05 +0300393 * Don't merge file system requests and sanitize requests
394 */
395 if ((req->cmd_flags & REQ_SANITIZE) != (next->cmd_flags & REQ_SANITIZE))
396 return 0;
397
398 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100399 * not contiguous
400 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900401 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
Jens Axboed6d48192008-01-29 14:04:06 +0100402 return 0;
403
404 if (rq_data_dir(req) != rq_data_dir(next)
405 || req->rq_disk != next->rq_disk
406 || next->special)
407 return 0;
408
409 /*
410 * If we are allowed to merge, then append bio list
411 * from next to rq and release next. merge_requests_fn
412 * will have updated segment counts, update sector
413 * counts here.
414 */
415 if (!ll_merge_requests_fn(q, req, next))
416 return 0;
417
418 /*
Tejun Heo80a761f2009-07-03 17:48:17 +0900419 * If failfast settings disagree or any of the two is already
420 * a mixed merge, mark both as mixed before proceeding. This
421 * makes sure that all involved bios have mixable attributes
422 * set properly.
423 */
424 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
425 (req->cmd_flags & REQ_FAILFAST_MASK) !=
426 (next->cmd_flags & REQ_FAILFAST_MASK)) {
427 blk_rq_set_mixed_merge(req);
428 blk_rq_set_mixed_merge(next);
429 }
430
431 /*
Jens Axboed6d48192008-01-29 14:04:06 +0100432 * At this point we have either done a back merge
433 * or front merge. We need the smaller start_time of
434 * the merged requests to be the current request
435 * for accounting purposes.
436 */
437 if (time_after(req->start_time, next->start_time))
438 req->start_time = next->start_time;
439
440 req->biotail->bi_next = next->bio;
441 req->biotail = next->biotail;
442
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900443 req->__data_len += blk_rq_bytes(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100444
445 elv_merge_requests(q, req, next);
446
Jerome Marchand42dad762009-04-22 14:01:49 +0200447 /*
448 * 'next' is going away, so update stats accordingly
449 */
450 blk_account_io_merge(next);
Jens Axboed6d48192008-01-29 14:04:06 +0100451
452 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200453 if (blk_rq_cpu_valid(next))
454 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100455
Boaz Harrosh1cd96c22009-03-24 12:35:07 +0100456 /* owner-ship of bio passed from next to req */
457 next->bio = NULL;
Jens Axboed6d48192008-01-29 14:04:06 +0100458 __blk_put_request(q, next);
459 return 1;
460}
461
462int attempt_back_merge(struct request_queue *q, struct request *rq)
463{
464 struct request *next = elv_latter_request(q, rq);
465
466 if (next)
467 return attempt_merge(q, rq, next);
468
469 return 0;
470}
471
472int attempt_front_merge(struct request_queue *q, struct request *rq)
473{
474 struct request *prev = elv_former_request(q, rq);
475
476 if (prev)
477 return attempt_merge(q, prev, rq);
478
479 return 0;
480}
Jens Axboe5e84ea32011-03-21 10:14:27 +0100481
482int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
483 struct request *next)
484{
485 return attempt_merge(q, rq, next);
486}
Tejun Heo050c8ea2012-02-08 09:19:38 +0100487
488bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
489{
490 if (!rq_mergeable(rq))
491 return false;
492
493 /* don't merge file system requests and discard requests */
494 if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
495 return false;
496
497 /* don't merge discard requests and secure discard requests */
498 if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
499 return false;
500
501 /* different data direction or already started, don't merge */
502 if (bio_data_dir(bio) != rq_data_dir(rq))
503 return false;
504
505 /* must be same device and not a special request */
506 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
507 return false;
508
509 /* only merge integrity protected bio into ditto rq */
510 if (bio_integrity(bio) != blk_integrity_rq(rq))
511 return false;
512
Amir Samuelov6a22e462014-05-26 11:44:06 +0300513 /* Don't merge bios of files with different encryption */
514 if (!security_allow_merge_bio(rq->bio, bio))
515 return false;
516
Tejun Heo050c8ea2012-02-08 09:19:38 +0100517 return true;
518}
519
520int blk_try_merge(struct request *rq, struct bio *bio)
521{
522 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_sector)
523 return ELEVATOR_BACK_MERGE;
524 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_sector)
525 return ELEVATOR_FRONT_MERGE;
526 return ELEVATOR_NO_MERGE;
527}