firesat: avc resend
[linux-2.6-block.git] / block / blk-merge.c
CommitLineData
d6d48196
JA
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{
e17fc0a1 14 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
d6d48196
JA
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) {
6728cb0e 35 printk(KERN_ERR "blk: request botched\n");
d6d48196
JA
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;
d6d48196 44 unsigned int phys_size;
d6d48196
JA
45 struct bio_vec *bv, *bvprv = NULL;
46 int seg_size;
d6d48196
JA
47 int cluster;
48 struct req_iterator iter;
49 int high, highprv = 1;
50 struct request_queue *q = rq->q;
51
52 if (!rq->bio)
53 return;
54
75ad23bc 55 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
5df97b91
MP
56 seg_size = 0;
57 phys_size = nr_phys_segs = 0;
d6d48196
JA
58 rq_for_each_segment(bv, rq, iter) {
59 /*
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.
63 */
64 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
65 if (high || highprv)
b8b3e16c 66 goto new_segment;
d6d48196
JA
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;
d6d48196
JA
74
75 seg_size += bv->bv_len;
d6d48196
JA
76 bvprv = bv;
77 continue;
78 }
79new_segment:
86771427
FT
80 if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size)
81 rq->bio->bi_seg_front_size = seg_size;
82
d6d48196
JA
83 nr_phys_segs++;
84 bvprv = bv;
85 seg_size = bv->bv_len;
86 highprv = high;
87 }
88
86771427
FT
89 if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size)
90 rq->bio->bi_seg_front_size = seg_size;
91 if (seg_size > rq->biotail->bi_seg_back_size)
92 rq->biotail->bi_seg_back_size = seg_size;
93
d6d48196 94 rq->nr_phys_segments = nr_phys_segs;
d6d48196
JA
95}
96
97void blk_recount_segments(struct request_queue *q, struct bio *bio)
98{
99 struct request rq;
100 struct bio *nxt = bio->bi_next;
101 rq.q = q;
102 rq.bio = rq.biotail = bio;
103 bio->bi_next = NULL;
104 blk_recalc_rq_segments(&rq);
105 bio->bi_next = nxt;
106 bio->bi_phys_segments = rq.nr_phys_segments;
d6d48196
JA
107 bio->bi_flags |= (1 << BIO_SEG_VALID);
108}
109EXPORT_SYMBOL(blk_recount_segments);
110
111static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
112 struct bio *nxt)
113{
75ad23bc 114 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
d6d48196
JA
115 return 0;
116
86771427
FT
117 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
118 q->max_segment_size)
d6d48196
JA
119 return 0;
120
e17fc0a1
DW
121 if (!bio_has_data(bio))
122 return 1;
123
124 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
125 return 0;
126
d6d48196 127 /*
e17fc0a1 128 * bio and nxt are contiguous in memory; check if the queue allows
d6d48196
JA
129 * these two to be merged into one
130 */
131 if (BIO_SEG_BOUNDARY(q, bio, nxt))
132 return 1;
133
134 return 0;
135}
136
d6d48196
JA
137/*
138 * map a request to scatterlist, return number of sg entries setup. Caller
139 * must make sure sg can hold rq->nr_phys_segments entries
140 */
141int blk_rq_map_sg(struct request_queue *q, struct request *rq,
142 struct scatterlist *sglist)
143{
144 struct bio_vec *bvec, *bvprv;
145 struct req_iterator iter;
146 struct scatterlist *sg;
147 int nsegs, cluster;
148
149 nsegs = 0;
75ad23bc 150 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
d6d48196
JA
151
152 /*
153 * for each bio in rq
154 */
155 bvprv = NULL;
156 sg = NULL;
157 rq_for_each_segment(bvec, rq, iter) {
158 int nbytes = bvec->bv_len;
159
160 if (bvprv && cluster) {
161 if (sg->length + nbytes > q->max_segment_size)
162 goto new_segment;
163
164 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
165 goto new_segment;
166 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
167 goto new_segment;
168
169 sg->length += nbytes;
170 } else {
171new_segment:
172 if (!sg)
173 sg = sglist;
174 else {
175 /*
176 * If the driver previously mapped a shorter
177 * list, we could see a termination bit
178 * prematurely unless it fully inits the sg
179 * table on each mapping. We KNOW that there
180 * must be more entries here or the driver
181 * would be buggy, so force clear the
182 * termination bit to avoid doing a full
183 * sg_init_table() in drivers for each command.
184 */
185 sg->page_link &= ~0x02;
186 sg = sg_next(sg);
187 }
188
189 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
190 nsegs++;
191 }
192 bvprv = bvec;
193 } /* segments in rq */
194
f18573ab
FT
195
196 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
197 (rq->data_len & q->dma_pad_mask)) {
198 unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1;
199
200 sg->length += pad_len;
201 rq->extra_len += pad_len;
202 }
203
2fb98e84 204 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
db0a2e00
TH
205 if (rq->cmd_flags & REQ_RW)
206 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
207
d6d48196
JA
208 sg->page_link &= ~0x02;
209 sg = sg_next(sg);
210 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
211 q->dma_drain_size,
212 ((unsigned long)q->dma_drain_buffer) &
213 (PAGE_SIZE - 1));
214 nsegs++;
7a85f889 215 rq->extra_len += q->dma_drain_size;
d6d48196
JA
216 }
217
218 if (sg)
219 sg_mark_end(sg);
220
221 return nsegs;
222}
d6d48196
JA
223EXPORT_SYMBOL(blk_rq_map_sg);
224
d6d48196
JA
225static inline int ll_new_hw_segment(struct request_queue *q,
226 struct request *req,
227 struct bio *bio)
228{
d6d48196
JA
229 int nr_phys_segs = bio_phys_segments(q, bio);
230
5df97b91 231 if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
d6d48196
JA
232 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
233 req->cmd_flags |= REQ_NOMERGE;
234 if (req == q->last_merge)
235 q->last_merge = NULL;
236 return 0;
237 }
238
239 /*
240 * This will form the start of a new hw segment. Bump both
241 * counters.
242 */
d6d48196
JA
243 req->nr_phys_segments += nr_phys_segs;
244 return 1;
245}
246
247int ll_back_merge_fn(struct request_queue *q, struct request *req,
248 struct bio *bio)
249{
250 unsigned short max_sectors;
d6d48196
JA
251
252 if (unlikely(blk_pc_request(req)))
253 max_sectors = q->max_hw_sectors;
254 else
255 max_sectors = q->max_sectors;
256
257 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
258 req->cmd_flags |= REQ_NOMERGE;
259 if (req == q->last_merge)
260 q->last_merge = NULL;
261 return 0;
262 }
2cdf79ca 263 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
d6d48196 264 blk_recount_segments(q, req->biotail);
2cdf79ca 265 if (!bio_flagged(bio, BIO_SEG_VALID))
d6d48196 266 blk_recount_segments(q, bio);
d6d48196
JA
267
268 return ll_new_hw_segment(q, req, bio);
269}
270
6728cb0e 271int ll_front_merge_fn(struct request_queue *q, struct request *req,
d6d48196
JA
272 struct bio *bio)
273{
274 unsigned short max_sectors;
d6d48196
JA
275
276 if (unlikely(blk_pc_request(req)))
277 max_sectors = q->max_hw_sectors;
278 else
279 max_sectors = q->max_sectors;
280
281
282 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
283 req->cmd_flags |= REQ_NOMERGE;
284 if (req == q->last_merge)
285 q->last_merge = NULL;
286 return 0;
287 }
2cdf79ca 288 if (!bio_flagged(bio, BIO_SEG_VALID))
d6d48196 289 blk_recount_segments(q, bio);
2cdf79ca 290 if (!bio_flagged(req->bio, BIO_SEG_VALID))
d6d48196 291 blk_recount_segments(q, req->bio);
d6d48196
JA
292
293 return ll_new_hw_segment(q, req, bio);
294}
295
296static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
297 struct request *next)
298{
299 int total_phys_segments;
86771427
FT
300 unsigned int seg_size =
301 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
d6d48196
JA
302
303 /*
304 * First check if the either of the requests are re-queued
305 * requests. Can't merge them if they are.
306 */
307 if (req->special || next->special)
308 return 0;
309
310 /*
311 * Will it become too large?
312 */
313 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
314 return 0;
315
316 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
86771427
FT
317 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
318 if (req->nr_phys_segments == 1)
319 req->bio->bi_seg_front_size = seg_size;
320 if (next->nr_phys_segments == 1)
321 next->biotail->bi_seg_back_size = seg_size;
d6d48196 322 total_phys_segments--;
86771427 323 }
d6d48196
JA
324
325 if (total_phys_segments > q->max_phys_segments)
326 return 0;
327
5df97b91 328 if (total_phys_segments > q->max_hw_segments)
d6d48196
JA
329 return 0;
330
331 /* Merge is OK... */
332 req->nr_phys_segments = total_phys_segments;
d6d48196
JA
333 return 1;
334}
335
336/*
337 * Has to be called with the request spinlock acquired
338 */
339static int attempt_merge(struct request_queue *q, struct request *req,
340 struct request *next)
341{
342 if (!rq_mergeable(req) || !rq_mergeable(next))
343 return 0;
344
345 /*
346 * not contiguous
347 */
348 if (req->sector + req->nr_sectors != next->sector)
349 return 0;
350
351 if (rq_data_dir(req) != rq_data_dir(next)
352 || req->rq_disk != next->rq_disk
353 || next->special)
354 return 0;
355
7ba1ba12
MP
356 if (blk_integrity_rq(req) != blk_integrity_rq(next))
357 return 0;
358
d6d48196
JA
359 /*
360 * If we are allowed to merge, then append bio list
361 * from next to rq and release next. merge_requests_fn
362 * will have updated segment counts, update sector
363 * counts here.
364 */
365 if (!ll_merge_requests_fn(q, req, next))
366 return 0;
367
368 /*
369 * At this point we have either done a back merge
370 * or front merge. We need the smaller start_time of
371 * the merged requests to be the current request
372 * for accounting purposes.
373 */
374 if (time_after(req->start_time, next->start_time))
375 req->start_time = next->start_time;
376
377 req->biotail->bi_next = next->bio;
378 req->biotail = next->biotail;
379
380 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
381
382 elv_merge_requests(q, req, next);
383
384 if (req->rq_disk) {
e71bf0d0 385 struct hd_struct *part;
c9959059 386 int cpu;
e71bf0d0 387
074a7aca 388 cpu = part_stat_lock();
e71bf0d0 389 part = disk_map_sector_rcu(req->rq_disk, req->sector);
c9959059 390
074a7aca
TH
391 part_round_stats(cpu, part);
392 part_dec_in_flight(part);
e71bf0d0 393
074a7aca 394 part_stat_unlock();
d6d48196
JA
395 }
396
397 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
ab780f1e
JA
398 if (blk_rq_cpu_valid(next))
399 req->cpu = next->cpu;
d6d48196
JA
400
401 __blk_put_request(q, next);
402 return 1;
403}
404
405int attempt_back_merge(struct request_queue *q, struct request *rq)
406{
407 struct request *next = elv_latter_request(q, rq);
408
409 if (next)
410 return attempt_merge(q, rq, next);
411
412 return 0;
413}
414
415int attempt_front_merge(struct request_queue *q, struct request *rq)
416{
417 struct request *prev = elv_former_request(q, rq);
418
419 if (prev)
420 return attempt_merge(q, prev, rq);
421
422 return 0;
423}