block: implement extended dev numbers
[linux-2.6/mini2440.git] / block / blk-merge.c
blobd926a24bf1fd8df3ef73aa46cbc50b67136adc96
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) || blk_discard_rq(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 unsigned int phys_size;
45 struct bio_vec *bv, *bvprv = NULL;
46 int seg_size;
47 int cluster;
48 struct req_iterator iter;
49 int high, highprv = 1;
50 struct request_queue *q = rq->q;
52 if (!rq->bio)
53 return;
55 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
56 seg_size = 0;
57 phys_size = nr_phys_segs = 0;
58 rq_for_each_segment(bv, rq, iter) {
60 * the trick here is making sure that a high page is never
61 * considered part of another segment, since that might
62 * change with the bounce page.
64 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
65 if (high || highprv)
66 goto new_segment;
67 if (cluster) {
68 if (seg_size + bv->bv_len > q->max_segment_size)
69 goto new_segment;
70 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
71 goto new_segment;
72 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
73 goto new_segment;
75 seg_size += bv->bv_len;
76 bvprv = bv;
77 continue;
79 new_segment:
80 nr_phys_segs++;
81 bvprv = bv;
82 seg_size = bv->bv_len;
83 highprv = high;
86 rq->nr_phys_segments = nr_phys_segs;
89 void blk_recount_segments(struct request_queue *q, struct bio *bio)
91 struct request rq;
92 struct bio *nxt = bio->bi_next;
93 rq.q = q;
94 rq.bio = rq.biotail = bio;
95 bio->bi_next = NULL;
96 blk_recalc_rq_segments(&rq);
97 bio->bi_next = nxt;
98 bio->bi_phys_segments = rq.nr_phys_segments;
99 bio->bi_flags |= (1 << BIO_SEG_VALID);
101 EXPORT_SYMBOL(blk_recount_segments);
103 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
104 struct bio *nxt)
106 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
107 return 0;
109 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
110 return 0;
112 if (!bio_has_data(bio))
113 return 1;
115 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
116 return 0;
119 * bio and nxt are contiguous in memory; check if the queue allows
120 * these two to be merged into one
122 if (BIO_SEG_BOUNDARY(q, bio, nxt))
123 return 1;
125 return 0;
129 * map a request to scatterlist, return number of sg entries setup. Caller
130 * must make sure sg can hold rq->nr_phys_segments entries
132 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
133 struct scatterlist *sglist)
135 struct bio_vec *bvec, *bvprv;
136 struct req_iterator iter;
137 struct scatterlist *sg;
138 int nsegs, cluster;
140 nsegs = 0;
141 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
144 * for each bio in rq
146 bvprv = NULL;
147 sg = NULL;
148 rq_for_each_segment(bvec, rq, iter) {
149 int nbytes = bvec->bv_len;
151 if (bvprv && cluster) {
152 if (sg->length + nbytes > q->max_segment_size)
153 goto new_segment;
155 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
156 goto new_segment;
157 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
158 goto new_segment;
160 sg->length += nbytes;
161 } else {
162 new_segment:
163 if (!sg)
164 sg = sglist;
165 else {
167 * If the driver previously mapped a shorter
168 * list, we could see a termination bit
169 * prematurely unless it fully inits the sg
170 * table on each mapping. We KNOW that there
171 * must be more entries here or the driver
172 * would be buggy, so force clear the
173 * termination bit to avoid doing a full
174 * sg_init_table() in drivers for each command.
176 sg->page_link &= ~0x02;
177 sg = sg_next(sg);
180 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
181 nsegs++;
183 bvprv = bvec;
184 } /* segments in rq */
187 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
188 (rq->data_len & q->dma_pad_mask)) {
189 unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1;
191 sg->length += pad_len;
192 rq->extra_len += pad_len;
195 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
196 if (rq->cmd_flags & REQ_RW)
197 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
199 sg->page_link &= ~0x02;
200 sg = sg_next(sg);
201 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
202 q->dma_drain_size,
203 ((unsigned long)q->dma_drain_buffer) &
204 (PAGE_SIZE - 1));
205 nsegs++;
206 rq->extra_len += q->dma_drain_size;
209 if (sg)
210 sg_mark_end(sg);
212 return nsegs;
214 EXPORT_SYMBOL(blk_rq_map_sg);
216 static inline int ll_new_mergeable(struct request_queue *q,
217 struct request *req,
218 struct bio *bio)
220 int nr_phys_segs = bio_phys_segments(q, bio);
222 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
223 req->cmd_flags |= REQ_NOMERGE;
224 if (req == q->last_merge)
225 q->last_merge = NULL;
226 return 0;
230 * A hw segment is just getting larger, bump just the phys
231 * counter.
233 req->nr_phys_segments += nr_phys_segs;
234 return 1;
237 static inline int ll_new_hw_segment(struct request_queue *q,
238 struct request *req,
239 struct bio *bio)
241 int nr_phys_segs = bio_phys_segments(q, bio);
243 if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
244 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
245 req->cmd_flags |= REQ_NOMERGE;
246 if (req == q->last_merge)
247 q->last_merge = NULL;
248 return 0;
252 * This will form the start of a new hw segment. Bump both
253 * counters.
255 req->nr_phys_segments += nr_phys_segs;
256 return 1;
259 int ll_back_merge_fn(struct request_queue *q, struct request *req,
260 struct bio *bio)
262 unsigned short max_sectors;
264 if (unlikely(blk_pc_request(req)))
265 max_sectors = q->max_hw_sectors;
266 else
267 max_sectors = q->max_sectors;
269 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
270 req->cmd_flags |= REQ_NOMERGE;
271 if (req == q->last_merge)
272 q->last_merge = NULL;
273 return 0;
275 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
276 blk_recount_segments(q, req->biotail);
277 if (!bio_flagged(bio, BIO_SEG_VALID))
278 blk_recount_segments(q, bio);
280 return ll_new_hw_segment(q, req, bio);
283 int ll_front_merge_fn(struct request_queue *q, struct request *req,
284 struct bio *bio)
286 unsigned short max_sectors;
288 if (unlikely(blk_pc_request(req)))
289 max_sectors = q->max_hw_sectors;
290 else
291 max_sectors = q->max_sectors;
294 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
295 req->cmd_flags |= REQ_NOMERGE;
296 if (req == q->last_merge)
297 q->last_merge = NULL;
298 return 0;
300 if (!bio_flagged(bio, BIO_SEG_VALID))
301 blk_recount_segments(q, bio);
302 if (!bio_flagged(req->bio, BIO_SEG_VALID))
303 blk_recount_segments(q, req->bio);
305 return ll_new_hw_segment(q, req, bio);
308 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
309 struct request *next)
311 int total_phys_segments;
314 * First check if the either of the requests are re-queued
315 * requests. Can't merge them if they are.
317 if (req->special || next->special)
318 return 0;
321 * Will it become too large?
323 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
324 return 0;
326 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
327 if (blk_phys_contig_segment(q, req->biotail, next->bio))
328 total_phys_segments--;
330 if (total_phys_segments > q->max_phys_segments)
331 return 0;
333 if (total_phys_segments > q->max_hw_segments)
334 return 0;
336 /* Merge is OK... */
337 req->nr_phys_segments = total_phys_segments;
338 return 1;
342 * Has to be called with the request spinlock acquired
344 static int attempt_merge(struct request_queue *q, struct request *req,
345 struct request *next)
347 if (!rq_mergeable(req) || !rq_mergeable(next))
348 return 0;
351 * not contiguous
353 if (req->sector + req->nr_sectors != next->sector)
354 return 0;
356 if (rq_data_dir(req) != rq_data_dir(next)
357 || req->rq_disk != next->rq_disk
358 || next->special)
359 return 0;
361 if (blk_integrity_rq(req) != blk_integrity_rq(next))
362 return 0;
365 * If we are allowed to merge, then append bio list
366 * from next to rq and release next. merge_requests_fn
367 * will have updated segment counts, update sector
368 * counts here.
370 if (!ll_merge_requests_fn(q, req, next))
371 return 0;
374 * At this point we have either done a back merge
375 * or front merge. We need the smaller start_time of
376 * the merged requests to be the current request
377 * for accounting purposes.
379 if (time_after(req->start_time, next->start_time))
380 req->start_time = next->start_time;
382 req->biotail->bi_next = next->bio;
383 req->biotail = next->biotail;
385 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
387 elv_merge_requests(q, req, next);
389 if (req->rq_disk) {
390 struct hd_struct *part;
391 int cpu;
393 cpu = disk_stat_lock();
394 part = disk_map_sector_rcu(req->rq_disk, req->sector);
396 disk_round_stats(cpu, req->rq_disk);
397 req->rq_disk->in_flight--;
398 if (part) {
399 part_round_stats(cpu, part);
400 part->in_flight--;
403 disk_stat_unlock();
406 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
408 __blk_put_request(q, next);
409 return 1;
412 int attempt_back_merge(struct request_queue *q, struct request *rq)
414 struct request *next = elv_latter_request(q, rq);
416 if (next)
417 return attempt_merge(q, rq, next);
419 return 0;
422 int attempt_front_merge(struct request_queue *q, struct request *rq)
424 struct request *prev = elv_former_request(q, rq);
426 if (prev)
427 return attempt_merge(q, prev, rq);
429 return 0;