mac80211: fix hardware scan completion
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-merge.c
blob7506c4fe0264bd6038d24108f5438fea42478508
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 = 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) {
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 nr_phys_segs++;
99 bvprv = bv;
100 seg_size = bv->bv_len;
101 highprv = high;
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;
113 void blk_recount_segments(struct request_queue *q, struct bio *bio)
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);
126 EXPORT_SYMBOL(blk_recount_segments);
128 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
129 struct bio *nxt)
131 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
132 return 0;
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;
140 * bio and nxt are contigous in memory, check if the queue allows
141 * these two to be merged into one
143 if (BIO_SEG_BOUNDARY(q, bio, nxt))
144 return 1;
146 return 0;
149 static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
150 struct bio *nxt)
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;
162 return 1;
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
169 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
170 struct scatterlist *sglist)
172 struct bio_vec *bvec, *bvprv;
173 struct req_iterator iter;
174 struct scatterlist *sg;
175 int nsegs, cluster;
177 nsegs = 0;
178 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
181 * for each bio in rq
183 bvprv = NULL;
184 sg = NULL;
185 rq_for_each_segment(bvec, rq, iter) {
186 int nbytes = bvec->bv_len;
188 if (bvprv && cluster) {
189 if (sg->length + nbytes > q->max_segment_size)
190 goto new_segment;
192 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
193 goto new_segment;
194 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
195 goto new_segment;
197 sg->length += nbytes;
198 } else {
199 new_segment:
200 if (!sg)
201 sg = sglist;
202 else {
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.
213 sg->page_link &= ~0x02;
214 sg = sg_next(sg);
217 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
218 nsegs++;
220 bvprv = bvec;
221 } /* segments in rq */
223 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
224 if (rq->cmd_flags & REQ_RW)
225 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
227 sg->page_link &= ~0x02;
228 sg = sg_next(sg);
229 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
230 q->dma_drain_size,
231 ((unsigned long)q->dma_drain_buffer) &
232 (PAGE_SIZE - 1));
233 nsegs++;
234 rq->data_len += q->dma_drain_size;
237 if (sg)
238 sg_mark_end(sg);
240 return nsegs;
242 EXPORT_SYMBOL(blk_rq_map_sg);
244 static inline int ll_new_mergeable(struct request_queue *q,
245 struct request *req,
246 struct bio *bio)
248 int nr_phys_segs = bio_phys_segments(q, bio);
250 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
251 req->cmd_flags |= REQ_NOMERGE;
252 if (req == q->last_merge)
253 q->last_merge = NULL;
254 return 0;
258 * A hw segment is just getting larger, bump just the phys
259 * counter.
261 req->nr_phys_segments += nr_phys_segs;
262 return 1;
265 static inline int ll_new_hw_segment(struct request_queue *q,
266 struct request *req,
267 struct bio *bio)
269 int nr_hw_segs = bio_hw_segments(q, bio);
270 int nr_phys_segs = bio_phys_segments(q, bio);
272 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
273 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
274 req->cmd_flags |= REQ_NOMERGE;
275 if (req == q->last_merge)
276 q->last_merge = NULL;
277 return 0;
281 * This will form the start of a new hw segment. Bump both
282 * counters.
284 req->nr_hw_segments += nr_hw_segs;
285 req->nr_phys_segments += nr_phys_segs;
286 return 1;
289 int ll_back_merge_fn(struct request_queue *q, struct request *req,
290 struct bio *bio)
292 unsigned short max_sectors;
293 int len;
295 if (unlikely(blk_pc_request(req)))
296 max_sectors = q->max_hw_sectors;
297 else
298 max_sectors = q->max_sectors;
300 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
301 req->cmd_flags |= REQ_NOMERGE;
302 if (req == q->last_merge)
303 q->last_merge = NULL;
304 return 0;
306 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
307 blk_recount_segments(q, req->biotail);
308 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
309 blk_recount_segments(q, bio);
310 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
311 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio))
312 && !BIOVEC_VIRT_OVERSIZE(len)) {
313 int mergeable = ll_new_mergeable(q, req, bio);
315 if (mergeable) {
316 if (req->nr_hw_segments == 1)
317 req->bio->bi_hw_front_size = len;
318 if (bio->bi_hw_segments == 1)
319 bio->bi_hw_back_size = len;
321 return mergeable;
324 return ll_new_hw_segment(q, req, bio);
327 int ll_front_merge_fn(struct request_queue *q, struct request *req,
328 struct bio *bio)
330 unsigned short max_sectors;
331 int len;
333 if (unlikely(blk_pc_request(req)))
334 max_sectors = q->max_hw_sectors;
335 else
336 max_sectors = q->max_sectors;
339 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
340 req->cmd_flags |= REQ_NOMERGE;
341 if (req == q->last_merge)
342 q->last_merge = NULL;
343 return 0;
345 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
346 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
347 blk_recount_segments(q, bio);
348 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
349 blk_recount_segments(q, req->bio);
350 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
351 !BIOVEC_VIRT_OVERSIZE(len)) {
352 int mergeable = ll_new_mergeable(q, req, bio);
354 if (mergeable) {
355 if (bio->bi_hw_segments == 1)
356 bio->bi_hw_front_size = len;
357 if (req->nr_hw_segments == 1)
358 req->biotail->bi_hw_back_size = len;
360 return mergeable;
363 return ll_new_hw_segment(q, req, bio);
366 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
367 struct request *next)
369 int total_phys_segments;
370 int total_hw_segments;
373 * First check if the either of the requests are re-queued
374 * requests. Can't merge them if they are.
376 if (req->special || next->special)
377 return 0;
380 * Will it become too large?
382 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
383 return 0;
385 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
386 if (blk_phys_contig_segment(q, req->biotail, next->bio))
387 total_phys_segments--;
389 if (total_phys_segments > q->max_phys_segments)
390 return 0;
392 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
393 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
394 int len = req->biotail->bi_hw_back_size +
395 next->bio->bi_hw_front_size;
397 * propagate the combined length to the end of the requests
399 if (req->nr_hw_segments == 1)
400 req->bio->bi_hw_front_size = len;
401 if (next->nr_hw_segments == 1)
402 next->biotail->bi_hw_back_size = len;
403 total_hw_segments--;
406 if (total_hw_segments > q->max_hw_segments)
407 return 0;
409 /* Merge is OK... */
410 req->nr_phys_segments = total_phys_segments;
411 req->nr_hw_segments = total_hw_segments;
412 return 1;
416 * Has to be called with the request spinlock acquired
418 static int attempt_merge(struct request_queue *q, struct request *req,
419 struct request *next)
421 if (!rq_mergeable(req) || !rq_mergeable(next))
422 return 0;
425 * not contiguous
427 if (req->sector + req->nr_sectors != next->sector)
428 return 0;
430 if (rq_data_dir(req) != rq_data_dir(next)
431 || req->rq_disk != next->rq_disk
432 || next->special)
433 return 0;
436 * If we are allowed to merge, then append bio list
437 * from next to rq and release next. merge_requests_fn
438 * will have updated segment counts, update sector
439 * counts here.
441 if (!ll_merge_requests_fn(q, req, next))
442 return 0;
445 * At this point we have either done a back merge
446 * or front merge. We need the smaller start_time of
447 * the merged requests to be the current request
448 * for accounting purposes.
450 if (time_after(req->start_time, next->start_time))
451 req->start_time = next->start_time;
453 req->biotail->bi_next = next->bio;
454 req->biotail = next->biotail;
456 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
458 elv_merge_requests(q, req, next);
460 if (req->rq_disk) {
461 struct hd_struct *part
462 = get_part(req->rq_disk, req->sector);
463 disk_round_stats(req->rq_disk);
464 req->rq_disk->in_flight--;
465 if (part) {
466 part_round_stats(part);
467 part->in_flight--;
471 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
473 __blk_put_request(q, next);
474 return 1;
477 int attempt_back_merge(struct request_queue *q, struct request *rq)
479 struct request *next = elv_latter_request(q, rq);
481 if (next)
482 return attempt_merge(q, rq, next);
484 return 0;
487 int attempt_front_merge(struct request_queue *q, struct request *rq)
489 struct request *prev = elv_former_request(q, rq);
491 if (prev)
492 return attempt_merge(q, prev, rq);
494 return 0;