blob: 908d3e11ac523e032f3f73a91932eb49f1a5994a [file] [log] [blame]
Jens Axboed6d48192008-01-29 14:04:06 +01001/*
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{
David Woodhousee17fc0a2008-08-09 16:42:20 +010014 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
Jens Axboed6d48192008-01-29 14:04:06 +010015 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) {
Jens Axboe6728cb02008-01-31 13:03:55 +010035 printk(KERN_ERR "blk: request botched\n");
Jens Axboed6d48192008-01-29 14:04:06 +010036 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;
Jens Axboed6d48192008-01-29 14:04:06 +010044 unsigned int phys_size;
Jens Axboed6d48192008-01-29 14:04:06 +010045 struct bio_vec *bv, *bvprv = NULL;
46 int seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +010047 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
Nick Piggin75ad23b2008-04-29 14:48:33 +020055 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
Mikulas Patocka5df97b92008-08-15 10:20:02 +020056 seg_size = 0;
57 phys_size = nr_phys_segs = 0;
Jens Axboed6d48192008-01-29 14:04:06 +010058 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)
Mikulas Patockab8b3e162008-08-15 10:15:19 +020066 goto new_segment;
Jens Axboed6d48192008-01-29 14:04:06 +010067 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;
Jens Axboed6d48192008-01-29 14:04:06 +010074
75 seg_size += bv->bv_len;
Jens Axboed6d48192008-01-29 14:04:06 +010076 bvprv = bv;
77 continue;
78 }
79new_segment:
Jens Axboed6d48192008-01-29 14:04:06 +010080 nr_phys_segs++;
81 bvprv = bv;
82 seg_size = bv->bv_len;
83 highprv = high;
84 }
85
Jens Axboed6d48192008-01-29 14:04:06 +010086 rq->nr_phys_segments = nr_phys_segs;
Jens Axboed6d48192008-01-29 14:04:06 +010087}
88
89void blk_recount_segments(struct request_queue *q, struct bio *bio)
90{
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;
Jens Axboed6d48192008-01-29 14:04:06 +010099 bio->bi_flags |= (1 << BIO_SEG_VALID);
100}
101EXPORT_SYMBOL(blk_recount_segments);
102
103static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
104 struct bio *nxt)
105{
Nick Piggin75ad23b2008-04-29 14:48:33 +0200106 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
Jens Axboed6d48192008-01-29 14:04:06 +0100107 return 0;
108
Jens Axboed6d48192008-01-29 14:04:06 +0100109 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
110 return 0;
111
David Woodhousee17fc0a2008-08-09 16:42:20 +0100112 if (!bio_has_data(bio))
113 return 1;
114
115 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
116 return 0;
117
Jens Axboed6d48192008-01-29 14:04:06 +0100118 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100119 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100120 * these two to be merged into one
121 */
122 if (BIO_SEG_BOUNDARY(q, bio, nxt))
123 return 1;
124
125 return 0;
126}
127
Jens Axboed6d48192008-01-29 14:04:06 +0100128/*
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
131 */
132int blk_rq_map_sg(struct request_queue *q, struct request *rq,
133 struct scatterlist *sglist)
134{
135 struct bio_vec *bvec, *bvprv;
136 struct req_iterator iter;
137 struct scatterlist *sg;
138 int nsegs, cluster;
139
140 nsegs = 0;
Nick Piggin75ad23b2008-04-29 14:48:33 +0200141 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
Jens Axboed6d48192008-01-29 14:04:06 +0100142
143 /*
144 * for each bio in rq
145 */
146 bvprv = NULL;
147 sg = NULL;
148 rq_for_each_segment(bvec, rq, iter) {
149 int nbytes = bvec->bv_len;
150
151 if (bvprv && cluster) {
152 if (sg->length + nbytes > q->max_segment_size)
153 goto new_segment;
154
155 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
156 goto new_segment;
157 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
158 goto new_segment;
159
160 sg->length += nbytes;
161 } else {
162new_segment:
163 if (!sg)
164 sg = sglist;
165 else {
166 /*
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.
175 */
176 sg->page_link &= ~0x02;
177 sg = sg_next(sg);
178 }
179
180 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
181 nsegs++;
182 }
183 bvprv = bvec;
184 } /* segments in rq */
185
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200186
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;
190
191 sg->length += pad_len;
192 rq->extra_len += pad_len;
193 }
194
Tejun Heo2fb98e82008-02-19 11:36:53 +0100195 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Tejun Heodb0a2e02008-02-19 11:36:55 +0100196 if (rq->cmd_flags & REQ_RW)
197 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
198
Jens Axboed6d48192008-01-29 14:04:06 +0100199 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++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100206 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100207 }
208
209 if (sg)
210 sg_mark_end(sg);
211
212 return nsegs;
213}
Jens Axboed6d48192008-01-29 14:04:06 +0100214EXPORT_SYMBOL(blk_rq_map_sg);
215
216static inline int ll_new_mergeable(struct request_queue *q,
217 struct request *req,
218 struct bio *bio)
219{
220 int nr_phys_segs = bio_phys_segments(q, bio);
221
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;
227 }
228
229 /*
230 * A hw segment is just getting larger, bump just the phys
231 * counter.
232 */
233 req->nr_phys_segments += nr_phys_segs;
234 return 1;
235}
236
237static inline int ll_new_hw_segment(struct request_queue *q,
238 struct request *req,
239 struct bio *bio)
240{
Jens Axboed6d48192008-01-29 14:04:06 +0100241 int nr_phys_segs = bio_phys_segments(q, bio);
242
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200243 if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
Jens Axboed6d48192008-01-29 14:04:06 +0100244 || 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;
249 }
250
251 /*
252 * This will form the start of a new hw segment. Bump both
253 * counters.
254 */
Jens Axboed6d48192008-01-29 14:04:06 +0100255 req->nr_phys_segments += nr_phys_segs;
256 return 1;
257}
258
259int ll_back_merge_fn(struct request_queue *q, struct request *req,
260 struct bio *bio)
261{
262 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100263
264 if (unlikely(blk_pc_request(req)))
265 max_sectors = q->max_hw_sectors;
266 else
267 max_sectors = q->max_sectors;
268
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;
274 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200275 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100276 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200277 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100278 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100279
280 return ll_new_hw_segment(q, req, bio);
281}
282
Jens Axboe6728cb02008-01-31 13:03:55 +0100283int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100284 struct bio *bio)
285{
286 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100287
288 if (unlikely(blk_pc_request(req)))
289 max_sectors = q->max_hw_sectors;
290 else
291 max_sectors = q->max_sectors;
292
293
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;
299 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200300 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100301 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200302 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100303 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100304
305 return ll_new_hw_segment(q, req, bio);
306}
307
308static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
309 struct request *next)
310{
311 int total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100312
313 /*
314 * First check if the either of the requests are re-queued
315 * requests. Can't merge them if they are.
316 */
317 if (req->special || next->special)
318 return 0;
319
320 /*
321 * Will it become too large?
322 */
323 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
324 return 0;
325
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--;
329
330 if (total_phys_segments > q->max_phys_segments)
331 return 0;
332
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200333 if (total_phys_segments > q->max_hw_segments)
Jens Axboed6d48192008-01-29 14:04:06 +0100334 return 0;
335
336 /* Merge is OK... */
337 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100338 return 1;
339}
340
341/*
342 * Has to be called with the request spinlock acquired
343 */
344static int attempt_merge(struct request_queue *q, struct request *req,
345 struct request *next)
346{
347 if (!rq_mergeable(req) || !rq_mergeable(next))
348 return 0;
349
350 /*
351 * not contiguous
352 */
353 if (req->sector + req->nr_sectors != next->sector)
354 return 0;
355
356 if (rq_data_dir(req) != rq_data_dir(next)
357 || req->rq_disk != next->rq_disk
358 || next->special)
359 return 0;
360
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200361 if (blk_integrity_rq(req) != blk_integrity_rq(next))
362 return 0;
363
Jens Axboed6d48192008-01-29 14:04:06 +0100364 /*
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.
369 */
370 if (!ll_merge_requests_fn(q, req, next))
371 return 0;
372
373 /*
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.
378 */
379 if (time_after(req->start_time, next->start_time))
380 req->start_time = next->start_time;
381
382 req->biotail->bi_next = next->bio;
383 req->biotail = next->biotail;
384
385 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
386
387 elv_merge_requests(q, req, next);
388
389 if (req->rq_disk) {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200390 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +0900391 int cpu;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200392
Tejun Heo074a7ac2008-08-25 19:56:14 +0900393 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200394 part = disk_map_sector_rcu(req->rq_disk, req->sector);
Tejun Heoc9959052008-08-25 19:47:21 +0900395
Tejun Heo074a7ac2008-08-25 19:56:14 +0900396 part_round_stats(cpu, part);
397 part_dec_in_flight(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200398
Tejun Heo074a7ac2008-08-25 19:56:14 +0900399 part_stat_unlock();
Jens Axboed6d48192008-01-29 14:04:06 +0100400 }
401
402 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200403 if (blk_rq_cpu_valid(next))
404 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100405
406 __blk_put_request(q, next);
407 return 1;
408}
409
410int attempt_back_merge(struct request_queue *q, struct request *rq)
411{
412 struct request *next = elv_latter_request(q, rq);
413
414 if (next)
415 return attempt_merge(q, rq, next);
416
417 return 0;
418}
419
420int attempt_front_merge(struct request_queue *q, struct request *rq)
421{
422 struct request *prev = elv_former_request(q, rq);
423
424 if (prev)
425 return attempt_merge(q, prev, rq);
426
427 return 0;
428}