aboutsummaryrefslogtreecommitdiffstats
path: root/block/blk-merge.c
diff options
context:
space:
mode:
Diffstat (limited to 'block/blk-merge.c')
-rw-r--r--block/blk-merge.c485
1 files changed, 485 insertions, 0 deletions
diff --git a/block/blk-merge.c b/block/blk-merge.c
new file mode 100644
index 000000000000..5023f0b08073
--- /dev/null
+++ b/block/blk-merge.c
@@ -0,0 +1,485 @@
1/*
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
12void blk_recalc_rq_sectors(struct request *rq, int nsect)
13{
14 if (blk_fs_request(rq)) {
15 rq->hard_sector += nsect;
16 rq->hard_nr_sectors -= nsect;
17
18 /*
19 * Move the I/O submission pointers ahead if required.
20 */
21 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
22 (rq->sector <= rq->hard_sector)) {
23 rq->sector = rq->hard_sector;
24 rq->nr_sectors = rq->hard_nr_sectors;
25 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
26 rq->current_nr_sectors = rq->hard_cur_sectors;
27 rq->buffer = bio_data(rq->bio);
28 }
29
30 /*
31 * if total number of sectors is less than the first segment
32 * size, something has gone terribly wrong
33 */
34 if (rq->nr_sectors < rq->current_nr_sectors) {
35 printk("blk: request botched\n");
36 rq->nr_sectors = rq->current_nr_sectors;
37 }
38 }
39}
40
41void blk_recalc_rq_segments(struct request *rq)
42{
43 int nr_phys_segs;
44 int nr_hw_segs;
45 unsigned int phys_size;
46 unsigned int hw_size;
47 struct bio_vec *bv, *bvprv = NULL;
48 int seg_size;
49 int hw_seg_size;
50 int cluster;
51 struct req_iterator iter;
52 int high, highprv = 1;
53 struct request_queue *q = rq->q;
54
55 if (!rq->bio)
56 return;
57
58 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
59 hw_seg_size = seg_size = 0;
60 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
61 rq_for_each_segment(bv, rq, iter) {
62 /*
63 * the trick here is making sure that a high page is never
64 * considered part of another segment, since that might
65 * change with the bounce page.
66 */
67 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
68 if (high || highprv)
69 goto new_hw_segment;
70 if (cluster) {
71 if (seg_size + bv->bv_len > q->max_segment_size)
72 goto new_segment;
73 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
74 goto new_segment;
75 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
76 goto new_segment;
77 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
78 goto new_hw_segment;
79
80 seg_size += bv->bv_len;
81 hw_seg_size += bv->bv_len;
82 bvprv = bv;
83 continue;
84 }
85new_segment:
86 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
87 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
88 hw_seg_size += bv->bv_len;
89 else {
90new_hw_segment:
91 if (nr_hw_segs == 1 &&
92 hw_seg_size > rq->bio->bi_hw_front_size)
93 rq->bio->bi_hw_front_size = hw_seg_size;
94 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
95 nr_hw_segs++;
96 }
97
98 nr_phys_segs++;
99 bvprv = bv;
100 seg_size = bv->bv_len;
101 highprv = high;
102 }
103
104 if (nr_hw_segs == 1 &&
105 hw_seg_size > rq->bio->bi_hw_front_size)
106 rq->bio->bi_hw_front_size = hw_seg_size;
107 if (hw_seg_size > rq->biotail->bi_hw_back_size)
108 rq->biotail->bi_hw_back_size = hw_seg_size;
109 rq->nr_phys_segments = nr_phys_segs;
110 rq->nr_hw_segments = nr_hw_segs;
111}
112
113void blk_recount_segments(struct request_queue *q, struct bio *bio)
114{
115 struct request rq;
116 struct bio *nxt = bio->bi_next;
117 rq.q = q;
118 rq.bio = rq.biotail = bio;
119 bio->bi_next = NULL;
120 blk_recalc_rq_segments(&rq);
121 bio->bi_next = nxt;
122 bio->bi_phys_segments = rq.nr_phys_segments;
123 bio->bi_hw_segments = rq.nr_hw_segments;
124 bio->bi_flags |= (1 << BIO_SEG_VALID);
125}
126EXPORT_SYMBOL(blk_recount_segments);
127
128static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
129 struct bio *nxt)
130{
131 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
132 return 0;
133
134 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
135 return 0;
136 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
137 return 0;
138
139 /*
140 * bio and nxt are contigous in memory, check if the queue allows
141 * these two to be merged into one
142 */
143 if (BIO_SEG_BOUNDARY(q, bio, nxt))
144 return 1;
145
146 return 0;
147}
148
149static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
150 struct bio *nxt)
151{
152 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
153 blk_recount_segments(q, bio);
154 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
155 blk_recount_segments(q, nxt);
156 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
157 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size))
158 return 0;
159 if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
160 return 0;
161
162 return 1;
163}
164
165/*
166 * map a request to scatterlist, return number of sg entries setup. Caller
167 * must make sure sg can hold rq->nr_phys_segments entries
168 */
169int blk_rq_map_sg(struct request_queue *q, struct request *rq,
170 struct scatterlist *sglist)
171{
172 struct bio_vec *bvec, *bvprv;
173 struct req_iterator iter;
174 struct scatterlist *sg;
175 int nsegs, cluster;
176
177 nsegs = 0;
178 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
179
180 /*
181 * for each bio in rq
182 */
183 bvprv = NULL;
184 sg = NULL;
185 rq_for_each_segment(bvec, rq, iter) {
186 int nbytes = bvec->bv_len;
187
188 if (bvprv && cluster) {
189 if (sg->length + nbytes > q->max_segment_size)
190 goto new_segment;
191
192 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
193 goto new_segment;
194 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
195 goto new_segment;
196
197 sg->length += nbytes;
198 } else {
199new_segment:
200 if (!sg)
201 sg = sglist;
202 else {
203 /*
204 * If the driver previously mapped a shorter
205 * list, we could see a termination bit
206 * prematurely unless it fully inits the sg
207 * table on each mapping. We KNOW that there
208 * must be more entries here or the driver
209 * would be buggy, so force clear the
210 * termination bit to avoid doing a full
211 * sg_init_table() in drivers for each command.
212 */
213 sg->page_link &= ~0x02;
214 sg = sg_next(sg);
215 }
216
217 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
218 nsegs++;
219 }
220 bvprv = bvec;
221 } /* segments in rq */
222
223 if (q->dma_drain_size) {
224 sg->page_link &= ~0x02;
225 sg = sg_next(sg);
226 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
227 q->dma_drain_size,
228 ((unsigned long)q->dma_drain_buffer) &
229 (PAGE_SIZE - 1));
230 nsegs++;
231 }
232
233 if (sg)
234 sg_mark_end(sg);
235
236 return nsegs;
237}
238
239EXPORT_SYMBOL(blk_rq_map_sg);
240
241static inline int ll_new_mergeable(struct request_queue *q,
242 struct request *req,
243 struct bio *bio)
244{
245 int nr_phys_segs = bio_phys_segments(q, bio);
246
247 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
248 req->cmd_flags |= REQ_NOMERGE;
249 if (req == q->last_merge)
250 q->last_merge = NULL;
251 return 0;
252 }
253
254 /*
255 * A hw segment is just getting larger, bump just the phys
256 * counter.
257 */
258 req->nr_phys_segments += nr_phys_segs;
259 return 1;
260}
261
262static inline int ll_new_hw_segment(struct request_queue *q,
263 struct request *req,
264 struct bio *bio)
265{
266 int nr_hw_segs = bio_hw_segments(q, bio);
267 int nr_phys_segs = bio_phys_segments(q, bio);
268
269 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
270 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
271 req->cmd_flags |= REQ_NOMERGE;
272 if (req == q->last_merge)
273 q->last_merge = NULL;
274 return 0;
275 }
276
277 /*
278 * This will form the start of a new hw segment. Bump both
279 * counters.
280 */
281 req->nr_hw_segments += nr_hw_segs;
282 req->nr_phys_segments += nr_phys_segs;
283 return 1;
284}
285
286int ll_back_merge_fn(struct request_queue *q, struct request *req,
287 struct bio *bio)
288{
289 unsigned short max_sectors;
290 int len;
291
292 if (unlikely(blk_pc_request(req)))
293 max_sectors = q->max_hw_sectors;
294 else
295 max_sectors = q->max_sectors;
296
297 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
298 req->cmd_flags |= REQ_NOMERGE;
299 if (req == q->last_merge)
300 q->last_merge = NULL;
301 return 0;
302 }
303 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
304 blk_recount_segments(q, req->biotail);
305 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
306 blk_recount_segments(q, bio);
307 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
308 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
309 !BIOVEC_VIRT_OVERSIZE(len)) {
310 int mergeable = ll_new_mergeable(q, req, bio);
311
312 if (mergeable) {
313 if (req->nr_hw_segments == 1)
314 req->bio->bi_hw_front_size = len;
315 if (bio->bi_hw_segments == 1)
316 bio->bi_hw_back_size = len;
317 }
318 return mergeable;
319 }
320
321 return ll_new_hw_segment(q, req, bio);
322}
323
324int ll_front_merge_fn(struct request_queue *q, struct request *req,
325 struct bio *bio)
326{
327 unsigned short max_sectors;
328 int len;
329
330 if (unlikely(blk_pc_request(req)))
331 max_sectors = q->max_hw_sectors;
332 else
333 max_sectors = q->max_sectors;
334
335
336 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
337 req->cmd_flags |= REQ_NOMERGE;
338 if (req == q->last_merge)
339 q->last_merge = NULL;
340 return 0;
341 }
342 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
343 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
344 blk_recount_segments(q, bio);
345 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
346 blk_recount_segments(q, req->bio);
347 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
348 !BIOVEC_VIRT_OVERSIZE(len)) {
349 int mergeable = ll_new_mergeable(q, req, bio);
350
351 if (mergeable) {
352 if (bio->bi_hw_segments == 1)
353 bio->bi_hw_front_size = len;
354 if (req->nr_hw_segments == 1)
355 req->biotail->bi_hw_back_size = len;
356 }
357 return mergeable;
358 }
359
360 return ll_new_hw_segment(q, req, bio);
361}
362
363static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
364 struct request *next)
365{
366 int total_phys_segments;
367 int total_hw_segments;
368
369 /*
370 * First check if the either of the requests are re-queued
371 * requests. Can't merge them if they are.
372 */
373 if (req->special || next->special)
374 return 0;
375
376 /*
377 * Will it become too large?
378 */
379 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
380 return 0;
381
382 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
383 if (blk_phys_contig_segment(q, req->biotail, next->bio))
384 total_phys_segments--;
385
386 if (total_phys_segments > q->max_phys_segments)
387 return 0;
388
389 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
390 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
391 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
392 /*
393 * propagate the combined length to the end of the requests
394 */
395 if (req->nr_hw_segments == 1)
396 req->bio->bi_hw_front_size = len;
397 if (next->nr_hw_segments == 1)
398 next->biotail->bi_hw_back_size = len;
399 total_hw_segments--;
400 }
401
402 if (total_hw_segments > q->max_hw_segments)
403 return 0;
404
405 /* Merge is OK... */
406 req->nr_phys_segments = total_phys_segments;
407 req->nr_hw_segments = total_hw_segments;
408 return 1;
409}
410
411/*
412 * Has to be called with the request spinlock acquired
413 */
414static int attempt_merge(struct request_queue *q, struct request *req,
415 struct request *next)
416{
417 if (!rq_mergeable(req) || !rq_mergeable(next))
418 return 0;
419
420 /*
421 * not contiguous
422 */
423 if (req->sector + req->nr_sectors != next->sector)
424 return 0;
425
426 if (rq_data_dir(req) != rq_data_dir(next)
427 || req->rq_disk != next->rq_disk
428 || next->special)
429 return 0;
430
431 /*
432 * If we are allowed to merge, then append bio list
433 * from next to rq and release next. merge_requests_fn
434 * will have updated segment counts, update sector
435 * counts here.
436 */
437 if (!ll_merge_requests_fn(q, req, next))
438 return 0;
439
440 /*
441 * At this point we have either done a back merge
442 * or front merge. We need the smaller start_time of
443 * the merged requests to be the current request
444 * for accounting purposes.
445 */
446 if (time_after(req->start_time, next->start_time))
447 req->start_time = next->start_time;
448
449 req->biotail->bi_next = next->bio;
450 req->biotail = next->biotail;
451
452 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
453
454 elv_merge_requests(q, req, next);
455
456 if (req->rq_disk) {
457 disk_round_stats(req->rq_disk);
458 req->rq_disk->in_flight--;
459 }
460
461 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
462
463 __blk_put_request(q, next);
464 return 1;
465}
466
467int attempt_back_merge(struct request_queue *q, struct request *rq)
468{
469 struct request *next = elv_latter_request(q, rq);
470
471 if (next)
472 return attempt_merge(q, rq, next);
473
474 return 0;
475}
476
477int attempt_front_merge(struct request_queue *q, struct request *rq)
478{
479 struct request *prev = elv_former_request(q, rq);
480
481 if (prev)
482 return attempt_merge(q, prev, rq);
483
484 return 0;
485}