aboutsummaryrefslogtreecommitdiffstats
path: root/block/blk-core.c
diff options
context:
space:
mode:
authorJens Axboe <jens.axboe@oracle.com>2008-01-29 08:04:06 -0500
committerJens Axboe <jens.axboe@oracle.com>2008-01-29 15:55:12 -0500
commitd6d48196961729b79509dcaeeb0f0fb4fbb47d29 (patch)
treee80f5cdc7f0d682a46b816f5a489357f7d622d62 /block/blk-core.c
parentdb1d08c646e47f3c7835ecdabb51a906ec311f69 (diff)
block: ll_rw_blk.c split, add blk-merge.c
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Diffstat (limited to 'block/blk-core.c')
-rw-r--r--block/blk-core.c478
1 files changed, 0 insertions, 478 deletions
diff --git a/block/blk-core.c b/block/blk-core.c
index 99f4d641edf5..8ff99440ee44 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -29,7 +29,6 @@
29#include <linux/cpu.h> 29#include <linux/cpu.h>
30#include <linux/blktrace_api.h> 30#include <linux/blktrace_api.h>
31#include <linux/fault-inject.h> 31#include <linux/fault-inject.h>
32#include <linux/scatterlist.h>
33 32
34#include "blk.h" 33#include "blk.h"
35 34
@@ -181,376 +180,6 @@ void blk_dump_rq_flags(struct request *rq, char *msg)
181 180
182EXPORT_SYMBOL(blk_dump_rq_flags); 181EXPORT_SYMBOL(blk_dump_rq_flags);
183 182
184static void blk_recalc_rq_segments(struct request *rq)
185{
186 int nr_phys_segs;
187 int nr_hw_segs;
188 unsigned int phys_size;
189 unsigned int hw_size;
190 struct bio_vec *bv, *bvprv = NULL;
191 int seg_size;
192 int hw_seg_size;
193 int cluster;
194 struct req_iterator iter;
195 int high, highprv = 1;
196 struct request_queue *q = rq->q;
197
198 if (!rq->bio)
199 return;
200
201 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
202 hw_seg_size = seg_size = 0;
203 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
204 rq_for_each_segment(bv, rq, iter) {
205 /*
206 * the trick here is making sure that a high page is never
207 * considered part of another segment, since that might
208 * change with the bounce page.
209 */
210 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
211 if (high || highprv)
212 goto new_hw_segment;
213 if (cluster) {
214 if (seg_size + bv->bv_len > q->max_segment_size)
215 goto new_segment;
216 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
217 goto new_segment;
218 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
219 goto new_segment;
220 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
221 goto new_hw_segment;
222
223 seg_size += bv->bv_len;
224 hw_seg_size += bv->bv_len;
225 bvprv = bv;
226 continue;
227 }
228new_segment:
229 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
230 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
231 hw_seg_size += bv->bv_len;
232 else {
233new_hw_segment:
234 if (nr_hw_segs == 1 &&
235 hw_seg_size > rq->bio->bi_hw_front_size)
236 rq->bio->bi_hw_front_size = hw_seg_size;
237 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
238 nr_hw_segs++;
239 }
240
241 nr_phys_segs++;
242 bvprv = bv;
243 seg_size = bv->bv_len;
244 highprv = high;
245 }
246
247 if (nr_hw_segs == 1 &&
248 hw_seg_size > rq->bio->bi_hw_front_size)
249 rq->bio->bi_hw_front_size = hw_seg_size;
250 if (hw_seg_size > rq->biotail->bi_hw_back_size)
251 rq->biotail->bi_hw_back_size = hw_seg_size;
252 rq->nr_phys_segments = nr_phys_segs;
253 rq->nr_hw_segments = nr_hw_segs;
254}
255
256void blk_recount_segments(struct request_queue *q, struct bio *bio)
257{
258 struct request rq;
259 struct bio *nxt = bio->bi_next;
260 rq.q = q;
261 rq.bio = rq.biotail = bio;
262 bio->bi_next = NULL;
263 blk_recalc_rq_segments(&rq);
264 bio->bi_next = nxt;
265 bio->bi_phys_segments = rq.nr_phys_segments;
266 bio->bi_hw_segments = rq.nr_hw_segments;
267 bio->bi_flags |= (1 << BIO_SEG_VALID);
268}
269EXPORT_SYMBOL(blk_recount_segments);
270
271static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
272 struct bio *nxt)
273{
274 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
275 return 0;
276
277 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
278 return 0;
279 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
280 return 0;
281
282 /*
283 * bio and nxt are contigous in memory, check if the queue allows
284 * these two to be merged into one
285 */
286 if (BIO_SEG_BOUNDARY(q, bio, nxt))
287 return 1;
288
289 return 0;
290}
291
292static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
293 struct bio *nxt)
294{
295 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
296 blk_recount_segments(q, bio);
297 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
298 blk_recount_segments(q, nxt);
299 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
300 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size))
301 return 0;
302 if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
303 return 0;
304
305 return 1;
306}
307
308/*
309 * map a request to scatterlist, return number of sg entries setup. Caller
310 * must make sure sg can hold rq->nr_phys_segments entries
311 */
312int blk_rq_map_sg(struct request_queue *q, struct request *rq,
313 struct scatterlist *sglist)
314{
315 struct bio_vec *bvec, *bvprv;
316 struct req_iterator iter;
317 struct scatterlist *sg;
318 int nsegs, cluster;
319
320 nsegs = 0;
321 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
322
323 /*
324 * for each bio in rq
325 */
326 bvprv = NULL;
327 sg = NULL;
328 rq_for_each_segment(bvec, rq, iter) {
329 int nbytes = bvec->bv_len;
330
331 if (bvprv && cluster) {
332 if (sg->length + nbytes > q->max_segment_size)
333 goto new_segment;
334
335 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
336 goto new_segment;
337 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
338 goto new_segment;
339
340 sg->length += nbytes;
341 } else {
342new_segment:
343 if (!sg)
344 sg = sglist;
345 else {
346 /*
347 * If the driver previously mapped a shorter
348 * list, we could see a termination bit
349 * prematurely unless it fully inits the sg
350 * table on each mapping. We KNOW that there
351 * must be more entries here or the driver
352 * would be buggy, so force clear the
353 * termination bit to avoid doing a full
354 * sg_init_table() in drivers for each command.
355 */
356 sg->page_link &= ~0x02;
357 sg = sg_next(sg);
358 }
359
360 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
361 nsegs++;
362 }
363 bvprv = bvec;
364 } /* segments in rq */
365
366 if (q->dma_drain_size) {
367 sg->page_link &= ~0x02;
368 sg = sg_next(sg);
369 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
370 q->dma_drain_size,
371 ((unsigned long)q->dma_drain_buffer) &
372 (PAGE_SIZE - 1));
373 nsegs++;
374 }
375
376 if (sg)
377 sg_mark_end(sg);
378
379 return nsegs;
380}
381
382EXPORT_SYMBOL(blk_rq_map_sg);
383
384static inline int ll_new_mergeable(struct request_queue *q,
385 struct request *req,
386 struct bio *bio)
387{
388 int nr_phys_segs = bio_phys_segments(q, bio);
389
390 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
391 req->cmd_flags |= REQ_NOMERGE;
392 if (req == q->last_merge)
393 q->last_merge = NULL;
394 return 0;
395 }
396
397 /*
398 * A hw segment is just getting larger, bump just the phys
399 * counter.
400 */
401 req->nr_phys_segments += nr_phys_segs;
402 return 1;
403}
404
405static inline int ll_new_hw_segment(struct request_queue *q,
406 struct request *req,
407 struct bio *bio)
408{
409 int nr_hw_segs = bio_hw_segments(q, bio);
410 int nr_phys_segs = bio_phys_segments(q, bio);
411
412 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
413 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
414 req->cmd_flags |= REQ_NOMERGE;
415 if (req == q->last_merge)
416 q->last_merge = NULL;
417 return 0;
418 }
419
420 /*
421 * This will form the start of a new hw segment. Bump both
422 * counters.
423 */
424 req->nr_hw_segments += nr_hw_segs;
425 req->nr_phys_segments += nr_phys_segs;
426 return 1;
427}
428
429int ll_back_merge_fn(struct request_queue *q, struct request *req,
430 struct bio *bio)
431{
432 unsigned short max_sectors;
433 int len;
434
435 if (unlikely(blk_pc_request(req)))
436 max_sectors = q->max_hw_sectors;
437 else
438 max_sectors = q->max_sectors;
439
440 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
441 req->cmd_flags |= REQ_NOMERGE;
442 if (req == q->last_merge)
443 q->last_merge = NULL;
444 return 0;
445 }
446 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
447 blk_recount_segments(q, req->biotail);
448 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
449 blk_recount_segments(q, bio);
450 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
451 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
452 !BIOVEC_VIRT_OVERSIZE(len)) {
453 int mergeable = ll_new_mergeable(q, req, bio);
454
455 if (mergeable) {
456 if (req->nr_hw_segments == 1)
457 req->bio->bi_hw_front_size = len;
458 if (bio->bi_hw_segments == 1)
459 bio->bi_hw_back_size = len;
460 }
461 return mergeable;
462 }
463
464 return ll_new_hw_segment(q, req, bio);
465}
466
467static int ll_front_merge_fn(struct request_queue *q, struct request *req,
468 struct bio *bio)
469{
470 unsigned short max_sectors;
471 int len;
472
473 if (unlikely(blk_pc_request(req)))
474 max_sectors = q->max_hw_sectors;
475 else
476 max_sectors = q->max_sectors;
477
478
479 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
480 req->cmd_flags |= REQ_NOMERGE;
481 if (req == q->last_merge)
482 q->last_merge = NULL;
483 return 0;
484 }
485 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
486 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
487 blk_recount_segments(q, bio);
488 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
489 blk_recount_segments(q, req->bio);
490 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
491 !BIOVEC_VIRT_OVERSIZE(len)) {
492 int mergeable = ll_new_mergeable(q, req, bio);
493
494 if (mergeable) {
495 if (bio->bi_hw_segments == 1)
496 bio->bi_hw_front_size = len;
497 if (req->nr_hw_segments == 1)
498 req->biotail->bi_hw_back_size = len;
499 }
500 return mergeable;
501 }
502
503 return ll_new_hw_segment(q, req, bio);
504}
505
506static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
507 struct request *next)
508{
509 int total_phys_segments;
510 int total_hw_segments;
511
512 /*
513 * First check if the either of the requests are re-queued
514 * requests. Can't merge them if they are.
515 */
516 if (req->special || next->special)
517 return 0;
518
519 /*
520 * Will it become too large?
521 */
522 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
523 return 0;
524
525 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
526 if (blk_phys_contig_segment(q, req->biotail, next->bio))
527 total_phys_segments--;
528
529 if (total_phys_segments > q->max_phys_segments)
530 return 0;
531
532 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
533 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
534 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
535 /*
536 * propagate the combined length to the end of the requests
537 */
538 if (req->nr_hw_segments == 1)
539 req->bio->bi_hw_front_size = len;
540 if (next->nr_hw_segments == 1)
541 next->biotail->bi_hw_back_size = len;
542 total_hw_segments--;
543 }
544
545 if (total_hw_segments > q->max_hw_segments)
546 return 0;
547
548 /* Merge is OK... */
549 req->nr_phys_segments = total_phys_segments;
550 req->nr_hw_segments = total_hw_segments;
551 return 1;
552}
553
554/* 183/*
555 * "plug" the device if there are no outstanding requests: this will 184 * "plug" the device if there are no outstanding requests: this will
556 * force the transfer to start only after we have put all the requests 185 * force the transfer to start only after we have put all the requests
@@ -1409,84 +1038,6 @@ void blk_put_request(struct request *req)
1409 1038
1410EXPORT_SYMBOL(blk_put_request); 1039EXPORT_SYMBOL(blk_put_request);
1411 1040
1412/*
1413 * Has to be called with the request spinlock acquired
1414 */
1415static int attempt_merge(struct request_queue *q, struct request *req,
1416 struct request *next)
1417{
1418 if (!rq_mergeable(req) || !rq_mergeable(next))
1419 return 0;
1420
1421 /*
1422 * not contiguous
1423 */
1424 if (req->sector + req->nr_sectors != next->sector)
1425 return 0;
1426
1427 if (rq_data_dir(req) != rq_data_dir(next)
1428 || req->rq_disk != next->rq_disk
1429 || next->special)
1430 return 0;
1431
1432 /*
1433 * If we are allowed to merge, then append bio list
1434 * from next to rq and release next. merge_requests_fn
1435 * will have updated segment counts, update sector
1436 * counts here.
1437 */
1438 if (!ll_merge_requests_fn(q, req, next))
1439 return 0;
1440
1441 /*
1442 * At this point we have either done a back merge
1443 * or front merge. We need the smaller start_time of
1444 * the merged requests to be the current request
1445 * for accounting purposes.
1446 */
1447 if (time_after(req->start_time, next->start_time))
1448 req->start_time = next->start_time;
1449
1450 req->biotail->bi_next = next->bio;
1451 req->biotail = next->biotail;
1452
1453 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
1454
1455 elv_merge_requests(q, req, next);
1456
1457 if (req->rq_disk) {
1458 disk_round_stats(req->rq_disk);
1459 req->rq_disk->in_flight--;
1460 }
1461
1462 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
1463
1464 __blk_put_request(q, next);
1465 return 1;
1466}
1467
1468static inline int attempt_back_merge(struct request_queue *q,
1469 struct request *rq)
1470{
1471 struct request *next = elv_latter_request(q, rq);
1472
1473 if (next)
1474 return attempt_merge(q, rq, next);
1475
1476 return 0;
1477}
1478
1479static inline int attempt_front_merge(struct request_queue *q,
1480 struct request *rq)
1481{
1482 struct request *prev = elv_former_request(q, rq);
1483
1484 if (prev)
1485 return attempt_merge(q, prev, rq);
1486
1487 return 0;
1488}
1489
1490void init_request_from_bio(struct request *req, struct bio *bio) 1041void init_request_from_bio(struct request *req, struct bio *bio)
1491{ 1042{
1492 req->cmd_type = REQ_TYPE_FS; 1043 req->cmd_type = REQ_TYPE_FS;
@@ -1938,35 +1489,6 @@ void submit_bio(int rw, struct bio *bio)
1938 1489
1939EXPORT_SYMBOL(submit_bio); 1490EXPORT_SYMBOL(submit_bio);
1940 1491
1941static void blk_recalc_rq_sectors(struct request *rq, int nsect)
1942{
1943 if (blk_fs_request(rq)) {
1944 rq->hard_sector += nsect;
1945 rq->hard_nr_sectors -= nsect;
1946
1947 /*
1948 * Move the I/O submission pointers ahead if required.
1949 */
1950 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
1951 (rq->sector <= rq->hard_sector)) {
1952 rq->sector = rq->hard_sector;
1953 rq->nr_sectors = rq->hard_nr_sectors;
1954 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
1955 rq->current_nr_sectors = rq->hard_cur_sectors;
1956 rq->buffer = bio_data(rq->bio);
1957 }
1958
1959 /*
1960 * if total number of sectors is less than the first segment
1961 * size, something has gone terribly wrong
1962 */
1963 if (rq->nr_sectors < rq->current_nr_sectors) {
1964 printk("blk: request botched\n");
1965 rq->nr_sectors = rq->current_nr_sectors;
1966 }
1967 }
1968}
1969
1970/** 1492/**
1971 * __end_that_request_first - end I/O on a request 1493 * __end_that_request_first - end I/O on a request
1972 * @req: the request being processed 1494 * @req: the request being processed