blob: 8681cd6f9911427419b41b99f678e9eb623bbf5e [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:
FUJITA Tomonori86771422008-10-13 14:19:05 +020080 if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size)
81 rq->bio->bi_seg_front_size = seg_size;
82
Jens Axboed6d48192008-01-29 14:04:06 +010083 nr_phys_segs++;
84 bvprv = bv;
85 seg_size = bv->bv_len;
86 highprv = high;
87 }
88
FUJITA Tomonori86771422008-10-13 14:19:05 +020089 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
Jens Axboed6d48192008-01-29 14:04:06 +010094 rq->nr_phys_segments = nr_phys_segs;
Jens Axboed6d48192008-01-29 14:04:06 +010095}
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;
Jens Axboed6d48192008-01-29 14:04:06 +0100107 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{
Nick Piggin75ad23b2008-04-29 14:48:33 +0200114 if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
Jens Axboed6d48192008-01-29 14:04:06 +0100115 return 0;
116
FUJITA Tomonori86771422008-10-13 14:19:05 +0200117 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
118 q->max_segment_size)
Jens Axboed6d48192008-01-29 14:04:06 +0100119 return 0;
120
David Woodhousee17fc0a2008-08-09 16:42:20 +0100121 if (!bio_has_data(bio))
122 return 1;
123
124 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
125 return 0;
126
Jens Axboed6d48192008-01-29 14:04:06 +0100127 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +0100128 * bio and nxt are contiguous in memory; check if the queue allows
Jens Axboed6d48192008-01-29 14:04:06 +0100129 * 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
Jens Axboed6d48192008-01-29 14:04:06 +0100137/*
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;
Nick Piggin75ad23b2008-04-29 14:48:33 +0200150 cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
Jens Axboed6d48192008-01-29 14:04:06 +0100151
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
FUJITA Tomonorif18573a2008-04-11 12:56:52 +0200195
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
Tejun Heo2fb98e82008-02-19 11:36:53 +0100204 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
Tejun Heodb0a2e02008-02-19 11:36:55 +0100205 if (rq->cmd_flags & REQ_RW)
206 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
207
Jens Axboed6d48192008-01-29 14:04:06 +0100208 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++;
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100215 rq->extra_len += q->dma_drain_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100216 }
217
218 if (sg)
219 sg_mark_end(sg);
220
221 return nsegs;
222}
Jens Axboed6d48192008-01-29 14:04:06 +0100223EXPORT_SYMBOL(blk_rq_map_sg);
224
225static inline int ll_new_mergeable(struct request_queue *q,
226 struct request *req,
227 struct bio *bio)
228{
229 int nr_phys_segs = bio_phys_segments(q, bio);
230
231 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
232 req->cmd_flags |= REQ_NOMERGE;
233 if (req == q->last_merge)
234 q->last_merge = NULL;
235 return 0;
236 }
237
238 /*
239 * A hw segment is just getting larger, bump just the phys
240 * counter.
241 */
242 req->nr_phys_segments += nr_phys_segs;
243 return 1;
244}
245
246static inline int ll_new_hw_segment(struct request_queue *q,
247 struct request *req,
248 struct bio *bio)
249{
Jens Axboed6d48192008-01-29 14:04:06 +0100250 int nr_phys_segs = bio_phys_segments(q, bio);
251
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200252 if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
Jens Axboed6d48192008-01-29 14:04:06 +0100253 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
254 req->cmd_flags |= REQ_NOMERGE;
255 if (req == q->last_merge)
256 q->last_merge = NULL;
257 return 0;
258 }
259
260 /*
261 * This will form the start of a new hw segment. Bump both
262 * counters.
263 */
Jens Axboed6d48192008-01-29 14:04:06 +0100264 req->nr_phys_segments += nr_phys_segs;
265 return 1;
266}
267
268int ll_back_merge_fn(struct request_queue *q, struct request *req,
269 struct bio *bio)
270{
271 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100272
273 if (unlikely(blk_pc_request(req)))
274 max_sectors = q->max_hw_sectors;
275 else
276 max_sectors = q->max_sectors;
277
278 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
279 req->cmd_flags |= REQ_NOMERGE;
280 if (req == q->last_merge)
281 q->last_merge = NULL;
282 return 0;
283 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200284 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100285 blk_recount_segments(q, req->biotail);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200286 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100287 blk_recount_segments(q, bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100288
289 return ll_new_hw_segment(q, req, bio);
290}
291
Jens Axboe6728cb02008-01-31 13:03:55 +0100292int ll_front_merge_fn(struct request_queue *q, struct request *req,
Jens Axboed6d48192008-01-29 14:04:06 +0100293 struct bio *bio)
294{
295 unsigned short max_sectors;
Jens Axboed6d48192008-01-29 14:04:06 +0100296
297 if (unlikely(blk_pc_request(req)))
298 max_sectors = q->max_hw_sectors;
299 else
300 max_sectors = q->max_sectors;
301
302
303 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
304 req->cmd_flags |= REQ_NOMERGE;
305 if (req == q->last_merge)
306 q->last_merge = NULL;
307 return 0;
308 }
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200309 if (!bio_flagged(bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100310 blk_recount_segments(q, bio);
Jens Axboe2cdf79c2008-05-07 09:33:55 +0200311 if (!bio_flagged(req->bio, BIO_SEG_VALID))
Jens Axboed6d48192008-01-29 14:04:06 +0100312 blk_recount_segments(q, req->bio);
Jens Axboed6d48192008-01-29 14:04:06 +0100313
314 return ll_new_hw_segment(q, req, bio);
315}
316
317static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
318 struct request *next)
319{
320 int total_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200321 unsigned int seg_size =
322 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100323
324 /*
325 * First check if the either of the requests are re-queued
326 * requests. Can't merge them if they are.
327 */
328 if (req->special || next->special)
329 return 0;
330
331 /*
332 * Will it become too large?
333 */
334 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
335 return 0;
336
337 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200338 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
339 if (req->nr_phys_segments == 1)
340 req->bio->bi_seg_front_size = seg_size;
341 if (next->nr_phys_segments == 1)
342 next->biotail->bi_seg_back_size = seg_size;
Jens Axboed6d48192008-01-29 14:04:06 +0100343 total_phys_segments--;
FUJITA Tomonori86771422008-10-13 14:19:05 +0200344 }
Jens Axboed6d48192008-01-29 14:04:06 +0100345
346 if (total_phys_segments > q->max_phys_segments)
347 return 0;
348
Mikulas Patocka5df97b92008-08-15 10:20:02 +0200349 if (total_phys_segments > q->max_hw_segments)
Jens Axboed6d48192008-01-29 14:04:06 +0100350 return 0;
351
352 /* Merge is OK... */
353 req->nr_phys_segments = total_phys_segments;
Jens Axboed6d48192008-01-29 14:04:06 +0100354 return 1;
355}
356
357/*
358 * Has to be called with the request spinlock acquired
359 */
360static int attempt_merge(struct request_queue *q, struct request *req,
361 struct request *next)
362{
363 if (!rq_mergeable(req) || !rq_mergeable(next))
364 return 0;
365
366 /*
367 * not contiguous
368 */
369 if (req->sector + req->nr_sectors != next->sector)
370 return 0;
371
372 if (rq_data_dir(req) != rq_data_dir(next)
373 || req->rq_disk != next->rq_disk
374 || next->special)
375 return 0;
376
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200377 if (blk_integrity_rq(req) != blk_integrity_rq(next))
378 return 0;
379
Jens Axboed6d48192008-01-29 14:04:06 +0100380 /*
381 * If we are allowed to merge, then append bio list
382 * from next to rq and release next. merge_requests_fn
383 * will have updated segment counts, update sector
384 * counts here.
385 */
386 if (!ll_merge_requests_fn(q, req, next))
387 return 0;
388
389 /*
390 * At this point we have either done a back merge
391 * or front merge. We need the smaller start_time of
392 * the merged requests to be the current request
393 * for accounting purposes.
394 */
395 if (time_after(req->start_time, next->start_time))
396 req->start_time = next->start_time;
397
398 req->biotail->bi_next = next->bio;
399 req->biotail = next->biotail;
400
401 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
402
403 elv_merge_requests(q, req, next);
404
405 if (req->rq_disk) {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200406 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +0900407 int cpu;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200408
Tejun Heo074a7ac2008-08-25 19:56:14 +0900409 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200410 part = disk_map_sector_rcu(req->rq_disk, req->sector);
Tejun Heoc9959052008-08-25 19:47:21 +0900411
Tejun Heo074a7ac2008-08-25 19:56:14 +0900412 part_round_stats(cpu, part);
413 part_dec_in_flight(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200414
Tejun Heo074a7ac2008-08-25 19:56:14 +0900415 part_stat_unlock();
Jens Axboed6d48192008-01-29 14:04:06 +0100416 }
417
418 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
Jens Axboeab780f12008-08-26 10:25:02 +0200419 if (blk_rq_cpu_valid(next))
420 req->cpu = next->cpu;
Jens Axboed6d48192008-01-29 14:04:06 +0100421
422 __blk_put_request(q, next);
423 return 1;
424}
425
426int attempt_back_merge(struct request_queue *q, struct request *rq)
427{
428 struct request *next = elv_latter_request(q, rq);
429
430 if (next)
431 return attempt_merge(q, rq, next);
432
433 return 0;
434}
435
436int attempt_front_merge(struct request_queue *q, struct request *rq)
437{
438 struct request *prev = elv_former_request(q, rq);
439
440 if (prev)
441 return attempt_merge(q, prev, rq);
442
443 return 0;
444}