ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-merge.c
blob857dce7634b6c1a5db8c37797677ce553167963c
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>
10 #include "blk.h"
12 void blk_recalc_rq_sectors(struct request *rq, int nsect)
14 if (blk_fs_request(rq)) {
15 rq->hard_sector += nsect;
16 rq->hard_nr_sectors -= nsect;
19 * Move the I/O submission pointers ahead if required.
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);
31 * if total number of sectors is less than the first segment
32 * size, something has gone terribly wrong
34 if (rq->nr_sectors < rq->current_nr_sectors) {
35 printk(KERN_ERR "blk: request botched\n");
36 rq->nr_sectors = rq->current_nr_sectors;
41 void blk_recalc_rq_segments(struct request *rq)
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;
55 if (!rq->bio)
56 return;
58 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
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) {
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.
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;
80 seg_size += bv->bv_len;
81 hw_seg_size += bv->bv_len;
82 bvprv = bv;
83 continue;
85 new_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 {
90 new_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++;
98 if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size)
99 rq->bio->bi_seg_front_size = seg_size;
101 nr_phys_segs++;
102 bvprv = bv;
103 seg_size = bv->bv_len;
104 highprv = high;
107 if (nr_hw_segs == 1 &&
108 hw_seg_size > rq->bio->bi_hw_front_size)
109 rq->bio->bi_hw_front_size = hw_seg_size;
110 if (hw_seg_size > rq->biotail->bi_hw_back_size)
111 rq->biotail->bi_hw_back_size = hw_seg_size;
112 if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size)
113 rq->bio->bi_seg_front_size = seg_size;
114 if (seg_size > rq->biotail->bi_seg_back_size)
115 rq->biotail->bi_seg_back_size = seg_size;
116 rq->nr_phys_segments = nr_phys_segs;
117 rq->nr_hw_segments = nr_hw_segs;
120 void blk_recount_segments(struct request_queue *q, struct bio *bio)
122 struct request rq;
123 struct bio *nxt = bio->bi_next;
124 rq.q = q;
125 rq.bio = rq.biotail = bio;
126 bio->bi_next = NULL;
127 blk_recalc_rq_segments(&rq);
128 bio->bi_next = nxt;
129 bio->bi_phys_segments = rq.nr_phys_segments;
130 bio->bi_hw_segments = rq.nr_hw_segments;
131 bio->bi_flags |= (1 << BIO_SEG_VALID);
133 EXPORT_SYMBOL(blk_recount_segments);
135 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
136 struct bio *nxt)
138 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
139 return 0;
141 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
142 return 0;
143 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
144 q->max_segment_size)
145 return 0;
148 * bio and nxt are contigous in memory, check if the queue allows
149 * these two to be merged into one
151 if (BIO_SEG_BOUNDARY(q, bio, nxt))
152 return 1;
154 return 0;
157 static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
158 struct bio *nxt)
160 if (!bio_flagged(bio, BIO_SEG_VALID))
161 blk_recount_segments(q, bio);
162 if (!bio_flagged(nxt, BIO_SEG_VALID))
163 blk_recount_segments(q, nxt);
164 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
165 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size))
166 return 0;
167 if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
168 return 0;
170 return 1;
174 * map a request to scatterlist, return number of sg entries setup. Caller
175 * must make sure sg can hold rq->nr_phys_segments entries
177 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
178 struct scatterlist *sglist)
180 struct bio_vec *bvec, *bvprv;
181 struct req_iterator iter;
182 struct scatterlist *sg;
183 int nsegs, cluster;
185 nsegs = 0;
186 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
189 * for each bio in rq
191 bvprv = NULL;
192 sg = NULL;
193 rq_for_each_segment(bvec, rq, iter) {
194 int nbytes = bvec->bv_len;
196 if (bvprv && cluster) {
197 if (sg->length + nbytes > q->max_segment_size)
198 goto new_segment;
200 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
201 goto new_segment;
202 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
203 goto new_segment;
205 sg->length += nbytes;
206 } else {
207 new_segment:
208 if (!sg)
209 sg = sglist;
210 else {
212 * If the driver previously mapped a shorter
213 * list, we could see a termination bit
214 * prematurely unless it fully inits the sg
215 * table on each mapping. We KNOW that there
216 * must be more entries here or the driver
217 * would be buggy, so force clear the
218 * termination bit to avoid doing a full
219 * sg_init_table() in drivers for each command.
221 sg->page_link &= ~0x02;
222 sg = sg_next(sg);
225 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
226 nsegs++;
228 bvprv = bvec;
229 } /* segments in rq */
232 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
233 (rq->data_len & q->dma_pad_mask)) {
234 unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1;
236 sg->length += pad_len;
237 rq->extra_len += pad_len;
240 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
241 if (rq->cmd_flags & REQ_RW)
242 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
244 sg->page_link &= ~0x02;
245 sg = sg_next(sg);
246 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
247 q->dma_drain_size,
248 ((unsigned long)q->dma_drain_buffer) &
249 (PAGE_SIZE - 1));
250 nsegs++;
251 rq->extra_len += q->dma_drain_size;
254 if (sg)
255 sg_mark_end(sg);
257 return nsegs;
259 EXPORT_SYMBOL(blk_rq_map_sg);
261 static inline int ll_new_mergeable(struct request_queue *q,
262 struct request *req,
263 struct bio *bio)
265 int nr_phys_segs = bio_phys_segments(q, bio);
267 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
268 req->cmd_flags |= REQ_NOMERGE;
269 if (req == q->last_merge)
270 q->last_merge = NULL;
271 return 0;
275 * A hw segment is just getting larger, bump just the phys
276 * counter.
278 req->nr_phys_segments += nr_phys_segs;
279 return 1;
282 static inline int ll_new_hw_segment(struct request_queue *q,
283 struct request *req,
284 struct bio *bio)
286 int nr_hw_segs = bio_hw_segments(q, bio);
287 int nr_phys_segs = bio_phys_segments(q, bio);
289 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
290 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
291 req->cmd_flags |= REQ_NOMERGE;
292 if (req == q->last_merge)
293 q->last_merge = NULL;
294 return 0;
298 * This will form the start of a new hw segment. Bump both
299 * counters.
301 req->nr_hw_segments += nr_hw_segs;
302 req->nr_phys_segments += nr_phys_segs;
303 return 1;
306 int ll_back_merge_fn(struct request_queue *q, struct request *req,
307 struct bio *bio)
309 unsigned short max_sectors;
310 int len;
312 if (unlikely(blk_pc_request(req)))
313 max_sectors = q->max_hw_sectors;
314 else
315 max_sectors = q->max_sectors;
317 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
318 req->cmd_flags |= REQ_NOMERGE;
319 if (req == q->last_merge)
320 q->last_merge = NULL;
321 return 0;
323 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
324 blk_recount_segments(q, req->biotail);
325 if (!bio_flagged(bio, BIO_SEG_VALID))
326 blk_recount_segments(q, bio);
327 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
328 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio))
329 && !BIOVEC_VIRT_OVERSIZE(len)) {
330 int mergeable = ll_new_mergeable(q, req, bio);
332 if (mergeable) {
333 if (req->nr_hw_segments == 1)
334 req->bio->bi_hw_front_size = len;
335 if (bio->bi_hw_segments == 1)
336 bio->bi_hw_back_size = len;
338 return mergeable;
341 return ll_new_hw_segment(q, req, bio);
344 int ll_front_merge_fn(struct request_queue *q, struct request *req,
345 struct bio *bio)
347 unsigned short max_sectors;
348 int len;
350 if (unlikely(blk_pc_request(req)))
351 max_sectors = q->max_hw_sectors;
352 else
353 max_sectors = q->max_sectors;
356 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
357 req->cmd_flags |= REQ_NOMERGE;
358 if (req == q->last_merge)
359 q->last_merge = NULL;
360 return 0;
362 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
363 if (!bio_flagged(bio, BIO_SEG_VALID))
364 blk_recount_segments(q, bio);
365 if (!bio_flagged(req->bio, BIO_SEG_VALID))
366 blk_recount_segments(q, req->bio);
367 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
368 !BIOVEC_VIRT_OVERSIZE(len)) {
369 int mergeable = ll_new_mergeable(q, req, bio);
371 if (mergeable) {
372 if (bio->bi_hw_segments == 1)
373 bio->bi_hw_front_size = len;
374 if (req->nr_hw_segments == 1)
375 req->biotail->bi_hw_back_size = len;
377 return mergeable;
380 return ll_new_hw_segment(q, req, bio);
383 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
384 struct request *next)
386 int total_phys_segments;
387 int total_hw_segments;
388 unsigned int seg_size =
389 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
392 * First check if the either of the requests are re-queued
393 * requests. Can't merge them if they are.
395 if (req->special || next->special)
396 return 0;
399 * Will it become too large?
401 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
402 return 0;
404 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
405 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
406 if (req->nr_phys_segments == 1)
407 req->bio->bi_seg_front_size = seg_size;
408 if (next->nr_phys_segments == 1)
409 next->biotail->bi_seg_back_size = seg_size;
410 total_phys_segments--;
413 if (total_phys_segments > q->max_phys_segments)
414 return 0;
416 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
417 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
418 int len = req->biotail->bi_hw_back_size +
419 next->bio->bi_hw_front_size;
421 * propagate the combined length to the end of the requests
423 if (req->nr_hw_segments == 1)
424 req->bio->bi_hw_front_size = len;
425 if (next->nr_hw_segments == 1)
426 next->biotail->bi_hw_back_size = len;
427 total_hw_segments--;
430 if (total_hw_segments > q->max_hw_segments)
431 return 0;
433 /* Merge is OK... */
434 req->nr_phys_segments = total_phys_segments;
435 req->nr_hw_segments = total_hw_segments;
436 return 1;
440 * Has to be called with the request spinlock acquired
442 static int attempt_merge(struct request_queue *q, struct request *req,
443 struct request *next)
445 if (!rq_mergeable(req) || !rq_mergeable(next))
446 return 0;
449 * not contiguous
451 if (req->sector + req->nr_sectors != next->sector)
452 return 0;
454 if (rq_data_dir(req) != rq_data_dir(next)
455 || req->rq_disk != next->rq_disk
456 || next->special)
457 return 0;
459 if (blk_integrity_rq(req) != blk_integrity_rq(next))
460 return 0;
463 * If we are allowed to merge, then append bio list
464 * from next to rq and release next. merge_requests_fn
465 * will have updated segment counts, update sector
466 * counts here.
468 if (!ll_merge_requests_fn(q, req, next))
469 return 0;
472 * At this point we have either done a back merge
473 * or front merge. We need the smaller start_time of
474 * the merged requests to be the current request
475 * for accounting purposes.
477 if (time_after(req->start_time, next->start_time))
478 req->start_time = next->start_time;
480 req->biotail->bi_next = next->bio;
481 req->biotail = next->biotail;
483 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
485 elv_merge_requests(q, req, next);
487 if (req->rq_disk) {
488 struct hd_struct *part
489 = get_part(req->rq_disk, req->sector);
490 disk_round_stats(req->rq_disk);
491 req->rq_disk->in_flight--;
492 if (part) {
493 part_round_stats(part);
494 part->in_flight--;
498 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
500 __blk_put_request(q, next);
501 return 1;
504 int attempt_back_merge(struct request_queue *q, struct request *rq)
506 struct request *next = elv_latter_request(q, rq);
508 if (next)
509 return attempt_merge(q, rq, next);
511 return 0;
514 int attempt_front_merge(struct request_queue *q, struct request *rq)
516 struct request *prev = elv_former_request(q, rq);
518 if (prev)
519 return attempt_merge(q, prev, rq);
521 return 0;