diff options
-rw-r--r-- | block/Makefile | 2 | ||||
-rw-r--r-- | block/blk-core.c | 478 | ||||
-rw-r--r-- | block/blk-merge.c | 485 | ||||
-rw-r--r-- | block/blk.h | 11 |
4 files changed, 495 insertions, 481 deletions
diff --git a/block/Makefile b/block/Makefile index 2002046d0a9e..5a43c7d79594 100644 --- a/block/Makefile +++ b/block/Makefile | |||
@@ -4,7 +4,7 @@ | |||
4 | 4 | ||
5 | obj-$(CONFIG_BLOCK) := elevator.o blk-core.o blk-tag.o blk-sysfs.o \ | 5 | obj-$(CONFIG_BLOCK) := elevator.o blk-core.o blk-tag.o blk-sysfs.o \ |
6 | blk-barrier.o blk-settings.o blk-ioc.o blk-map.o \ | 6 | blk-barrier.o blk-settings.o blk-ioc.o blk-map.o \ |
7 | blk-exec.o ioctl.o genhd.o scsi_ioctl.o | 7 | blk-exec.o blk-merge.o ioctl.o genhd.o scsi_ioctl.o |
8 | 8 | ||
9 | obj-$(CONFIG_BLK_DEV_BSG) += bsg.o | 9 | obj-$(CONFIG_BLK_DEV_BSG) += bsg.o |
10 | obj-$(CONFIG_IOSCHED_NOOP) += noop-iosched.o | 10 | obj-$(CONFIG_IOSCHED_NOOP) += noop-iosched.o |
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 | ||
182 | EXPORT_SYMBOL(blk_dump_rq_flags); | 181 | EXPORT_SYMBOL(blk_dump_rq_flags); |
183 | 182 | ||
184 | static 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 | } | ||
228 | new_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 { | ||
233 | new_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 | |||
256 | void 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 | } | ||
269 | EXPORT_SYMBOL(blk_recount_segments); | ||
270 | |||
271 | static 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 | |||
292 | static 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 | */ | ||
312 | int 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 { | ||
342 | new_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 | |||
382 | EXPORT_SYMBOL(blk_rq_map_sg); | ||
383 | |||
384 | static 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 | |||
405 | static 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 | |||
429 | int 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 | |||
467 | static 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 | |||
506 | static 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 | ||
1410 | EXPORT_SYMBOL(blk_put_request); | 1039 | EXPORT_SYMBOL(blk_put_request); |
1411 | 1040 | ||
1412 | /* | ||
1413 | * Has to be called with the request spinlock acquired | ||
1414 | */ | ||
1415 | static 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 | |||
1468 | static 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 | |||
1479 | static 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 | |||
1490 | void init_request_from_bio(struct request *req, struct bio *bio) | 1041 | void 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 | ||
1939 | EXPORT_SYMBOL(submit_bio); | 1490 | EXPORT_SYMBOL(submit_bio); |
1940 | 1491 | ||
1941 | static 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 |
diff --git a/block/blk-merge.c b/block/blk-merge.c new file mode 100644 index 000000000000..5023f0b08073 --- /dev/null +++ b/block/blk-merge.c | |||
@@ -0,0 +1,485 @@ | |||
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 | |||
12 | void blk_recalc_rq_sectors(struct request *rq, int nsect) | ||
13 | { | ||
14 | if (blk_fs_request(rq)) { | ||
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) { | ||
35 | printk("blk: request botched\n"); | ||
36 | rq->nr_sectors = rq->current_nr_sectors; | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
41 | void blk_recalc_rq_segments(struct request *rq) | ||
42 | { | ||
43 | int nr_phys_segs; | ||
44 | int nr_hw_segs; | ||
45 | unsigned int phys_size; | ||
46 | unsigned int hw_size; | ||
47 | struct bio_vec *bv, *bvprv = NULL; | ||
48 | int seg_size; | ||
49 | int hw_seg_size; | ||
50 | int cluster; | ||
51 | struct req_iterator iter; | ||
52 | int high, highprv = 1; | ||
53 | struct request_queue *q = rq->q; | ||
54 | |||
55 | if (!rq->bio) | ||
56 | return; | ||
57 | |||
58 | cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER); | ||
59 | hw_seg_size = seg_size = 0; | ||
60 | phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0; | ||
61 | rq_for_each_segment(bv, rq, iter) { | ||
62 | /* | ||
63 | * the trick here is making sure that a high page is never | ||
64 | * considered part of another segment, since that might | ||
65 | * change with the bounce page. | ||
66 | */ | ||
67 | high = page_to_pfn(bv->bv_page) > q->bounce_pfn; | ||
68 | if (high || highprv) | ||
69 | goto new_hw_segment; | ||
70 | if (cluster) { | ||
71 | if (seg_size + bv->bv_len > q->max_segment_size) | ||
72 | goto new_segment; | ||
73 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv)) | ||
74 | goto new_segment; | ||
75 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv)) | ||
76 | goto new_segment; | ||
77 | if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) | ||
78 | goto new_hw_segment; | ||
79 | |||
80 | seg_size += bv->bv_len; | ||
81 | hw_seg_size += bv->bv_len; | ||
82 | bvprv = bv; | ||
83 | continue; | ||
84 | } | ||
85 | new_segment: | ||
86 | if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) && | ||
87 | !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) | ||
88 | hw_seg_size += bv->bv_len; | ||
89 | else { | ||
90 | new_hw_segment: | ||
91 | if (nr_hw_segs == 1 && | ||
92 | hw_seg_size > rq->bio->bi_hw_front_size) | ||
93 | rq->bio->bi_hw_front_size = hw_seg_size; | ||
94 | hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len; | ||
95 | nr_hw_segs++; | ||
96 | } | ||
97 | |||
98 | nr_phys_segs++; | ||
99 | bvprv = bv; | ||
100 | seg_size = bv->bv_len; | ||
101 | highprv = high; | ||
102 | } | ||
103 | |||
104 | if (nr_hw_segs == 1 && | ||
105 | hw_seg_size > rq->bio->bi_hw_front_size) | ||
106 | rq->bio->bi_hw_front_size = hw_seg_size; | ||
107 | if (hw_seg_size > rq->biotail->bi_hw_back_size) | ||
108 | rq->biotail->bi_hw_back_size = hw_seg_size; | ||
109 | rq->nr_phys_segments = nr_phys_segs; | ||
110 | rq->nr_hw_segments = nr_hw_segs; | ||
111 | } | ||
112 | |||
113 | void blk_recount_segments(struct request_queue *q, struct bio *bio) | ||
114 | { | ||
115 | struct request rq; | ||
116 | struct bio *nxt = bio->bi_next; | ||
117 | rq.q = q; | ||
118 | rq.bio = rq.biotail = bio; | ||
119 | bio->bi_next = NULL; | ||
120 | blk_recalc_rq_segments(&rq); | ||
121 | bio->bi_next = nxt; | ||
122 | bio->bi_phys_segments = rq.nr_phys_segments; | ||
123 | bio->bi_hw_segments = rq.nr_hw_segments; | ||
124 | bio->bi_flags |= (1 << BIO_SEG_VALID); | ||
125 | } | ||
126 | EXPORT_SYMBOL(blk_recount_segments); | ||
127 | |||
128 | static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, | ||
129 | struct bio *nxt) | ||
130 | { | ||
131 | if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER))) | ||
132 | return 0; | ||
133 | |||
134 | if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt))) | ||
135 | return 0; | ||
136 | if (bio->bi_size + nxt->bi_size > q->max_segment_size) | ||
137 | return 0; | ||
138 | |||
139 | /* | ||
140 | * bio and nxt are contigous in memory, check if the queue allows | ||
141 | * these two to be merged into one | ||
142 | */ | ||
143 | if (BIO_SEG_BOUNDARY(q, bio, nxt)) | ||
144 | return 1; | ||
145 | |||
146 | return 0; | ||
147 | } | ||
148 | |||
149 | static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio, | ||
150 | struct bio *nxt) | ||
151 | { | ||
152 | if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) | ||
153 | blk_recount_segments(q, bio); | ||
154 | if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID))) | ||
155 | blk_recount_segments(q, nxt); | ||
156 | if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) || | ||
157 | BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size)) | ||
158 | return 0; | ||
159 | if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size) | ||
160 | return 0; | ||
161 | |||
162 | return 1; | ||
163 | } | ||
164 | |||
165 | /* | ||
166 | * map a request to scatterlist, return number of sg entries setup. Caller | ||
167 | * must make sure sg can hold rq->nr_phys_segments entries | ||
168 | */ | ||
169 | int blk_rq_map_sg(struct request_queue *q, struct request *rq, | ||
170 | struct scatterlist *sglist) | ||
171 | { | ||
172 | struct bio_vec *bvec, *bvprv; | ||
173 | struct req_iterator iter; | ||
174 | struct scatterlist *sg; | ||
175 | int nsegs, cluster; | ||
176 | |||
177 | nsegs = 0; | ||
178 | cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER); | ||
179 | |||
180 | /* | ||
181 | * for each bio in rq | ||
182 | */ | ||
183 | bvprv = NULL; | ||
184 | sg = NULL; | ||
185 | rq_for_each_segment(bvec, rq, iter) { | ||
186 | int nbytes = bvec->bv_len; | ||
187 | |||
188 | if (bvprv && cluster) { | ||
189 | if (sg->length + nbytes > q->max_segment_size) | ||
190 | goto new_segment; | ||
191 | |||
192 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) | ||
193 | goto new_segment; | ||
194 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) | ||
195 | goto new_segment; | ||
196 | |||
197 | sg->length += nbytes; | ||
198 | } else { | ||
199 | new_segment: | ||
200 | if (!sg) | ||
201 | sg = sglist; | ||
202 | else { | ||
203 | /* | ||
204 | * If the driver previously mapped a shorter | ||
205 | * list, we could see a termination bit | ||
206 | * prematurely unless it fully inits the sg | ||
207 | * table on each mapping. We KNOW that there | ||
208 | * must be more entries here or the driver | ||
209 | * would be buggy, so force clear the | ||
210 | * termination bit to avoid doing a full | ||
211 | * sg_init_table() in drivers for each command. | ||
212 | */ | ||
213 | sg->page_link &= ~0x02; | ||
214 | sg = sg_next(sg); | ||
215 | } | ||
216 | |||
217 | sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset); | ||
218 | nsegs++; | ||
219 | } | ||
220 | bvprv = bvec; | ||
221 | } /* segments in rq */ | ||
222 | |||
223 | if (q->dma_drain_size) { | ||
224 | sg->page_link &= ~0x02; | ||
225 | sg = sg_next(sg); | ||
226 | sg_set_page(sg, virt_to_page(q->dma_drain_buffer), | ||
227 | q->dma_drain_size, | ||
228 | ((unsigned long)q->dma_drain_buffer) & | ||
229 | (PAGE_SIZE - 1)); | ||
230 | nsegs++; | ||
231 | } | ||
232 | |||
233 | if (sg) | ||
234 | sg_mark_end(sg); | ||
235 | |||
236 | return nsegs; | ||
237 | } | ||
238 | |||
239 | EXPORT_SYMBOL(blk_rq_map_sg); | ||
240 | |||
241 | static inline int ll_new_mergeable(struct request_queue *q, | ||
242 | struct request *req, | ||
243 | struct bio *bio) | ||
244 | { | ||
245 | int nr_phys_segs = bio_phys_segments(q, bio); | ||
246 | |||
247 | if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { | ||
248 | req->cmd_flags |= REQ_NOMERGE; | ||
249 | if (req == q->last_merge) | ||
250 | q->last_merge = NULL; | ||
251 | return 0; | ||
252 | } | ||
253 | |||
254 | /* | ||
255 | * A hw segment is just getting larger, bump just the phys | ||
256 | * counter. | ||
257 | */ | ||
258 | req->nr_phys_segments += nr_phys_segs; | ||
259 | return 1; | ||
260 | } | ||
261 | |||
262 | static inline int ll_new_hw_segment(struct request_queue *q, | ||
263 | struct request *req, | ||
264 | struct bio *bio) | ||
265 | { | ||
266 | int nr_hw_segs = bio_hw_segments(q, bio); | ||
267 | int nr_phys_segs = bio_phys_segments(q, bio); | ||
268 | |||
269 | if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments | ||
270 | || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { | ||
271 | req->cmd_flags |= REQ_NOMERGE; | ||
272 | if (req == q->last_merge) | ||
273 | q->last_merge = NULL; | ||
274 | return 0; | ||
275 | } | ||
276 | |||
277 | /* | ||
278 | * This will form the start of a new hw segment. Bump both | ||
279 | * counters. | ||
280 | */ | ||
281 | req->nr_hw_segments += nr_hw_segs; | ||
282 | req->nr_phys_segments += nr_phys_segs; | ||
283 | return 1; | ||
284 | } | ||
285 | |||
286 | int ll_back_merge_fn(struct request_queue *q, struct request *req, | ||
287 | struct bio *bio) | ||
288 | { | ||
289 | unsigned short max_sectors; | ||
290 | int len; | ||
291 | |||
292 | if (unlikely(blk_pc_request(req))) | ||
293 | max_sectors = q->max_hw_sectors; | ||
294 | else | ||
295 | max_sectors = q->max_sectors; | ||
296 | |||
297 | if (req->nr_sectors + bio_sectors(bio) > max_sectors) { | ||
298 | req->cmd_flags |= REQ_NOMERGE; | ||
299 | if (req == q->last_merge) | ||
300 | q->last_merge = NULL; | ||
301 | return 0; | ||
302 | } | ||
303 | if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID))) | ||
304 | blk_recount_segments(q, req->biotail); | ||
305 | if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) | ||
306 | blk_recount_segments(q, bio); | ||
307 | len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size; | ||
308 | if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) && | ||
309 | !BIOVEC_VIRT_OVERSIZE(len)) { | ||
310 | int mergeable = ll_new_mergeable(q, req, bio); | ||
311 | |||
312 | if (mergeable) { | ||
313 | if (req->nr_hw_segments == 1) | ||
314 | req->bio->bi_hw_front_size = len; | ||
315 | if (bio->bi_hw_segments == 1) | ||
316 | bio->bi_hw_back_size = len; | ||
317 | } | ||
318 | return mergeable; | ||
319 | } | ||
320 | |||
321 | return ll_new_hw_segment(q, req, bio); | ||
322 | } | ||
323 | |||
324 | int ll_front_merge_fn(struct request_queue *q, struct request *req, | ||
325 | struct bio *bio) | ||
326 | { | ||
327 | unsigned short max_sectors; | ||
328 | int len; | ||
329 | |||
330 | if (unlikely(blk_pc_request(req))) | ||
331 | max_sectors = q->max_hw_sectors; | ||
332 | else | ||
333 | max_sectors = q->max_sectors; | ||
334 | |||
335 | |||
336 | if (req->nr_sectors + bio_sectors(bio) > max_sectors) { | ||
337 | req->cmd_flags |= REQ_NOMERGE; | ||
338 | if (req == q->last_merge) | ||
339 | q->last_merge = NULL; | ||
340 | return 0; | ||
341 | } | ||
342 | len = bio->bi_hw_back_size + req->bio->bi_hw_front_size; | ||
343 | if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) | ||
344 | blk_recount_segments(q, bio); | ||
345 | if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID))) | ||
346 | blk_recount_segments(q, req->bio); | ||
347 | if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) && | ||
348 | !BIOVEC_VIRT_OVERSIZE(len)) { | ||
349 | int mergeable = ll_new_mergeable(q, req, bio); | ||
350 | |||
351 | if (mergeable) { | ||
352 | if (bio->bi_hw_segments == 1) | ||
353 | bio->bi_hw_front_size = len; | ||
354 | if (req->nr_hw_segments == 1) | ||
355 | req->biotail->bi_hw_back_size = len; | ||
356 | } | ||
357 | return mergeable; | ||
358 | } | ||
359 | |||
360 | return ll_new_hw_segment(q, req, bio); | ||
361 | } | ||
362 | |||
363 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, | ||
364 | struct request *next) | ||
365 | { | ||
366 | int total_phys_segments; | ||
367 | int total_hw_segments; | ||
368 | |||
369 | /* | ||
370 | * First check if the either of the requests are re-queued | ||
371 | * requests. Can't merge them if they are. | ||
372 | */ | ||
373 | if (req->special || next->special) | ||
374 | return 0; | ||
375 | |||
376 | /* | ||
377 | * Will it become too large? | ||
378 | */ | ||
379 | if ((req->nr_sectors + next->nr_sectors) > q->max_sectors) | ||
380 | return 0; | ||
381 | |||
382 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; | ||
383 | if (blk_phys_contig_segment(q, req->biotail, next->bio)) | ||
384 | total_phys_segments--; | ||
385 | |||
386 | if (total_phys_segments > q->max_phys_segments) | ||
387 | return 0; | ||
388 | |||
389 | total_hw_segments = req->nr_hw_segments + next->nr_hw_segments; | ||
390 | if (blk_hw_contig_segment(q, req->biotail, next->bio)) { | ||
391 | int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size; | ||
392 | /* | ||
393 | * propagate the combined length to the end of the requests | ||
394 | */ | ||
395 | if (req->nr_hw_segments == 1) | ||
396 | req->bio->bi_hw_front_size = len; | ||
397 | if (next->nr_hw_segments == 1) | ||
398 | next->biotail->bi_hw_back_size = len; | ||
399 | total_hw_segments--; | ||
400 | } | ||
401 | |||
402 | if (total_hw_segments > q->max_hw_segments) | ||
403 | return 0; | ||
404 | |||
405 | /* Merge is OK... */ | ||
406 | req->nr_phys_segments = total_phys_segments; | ||
407 | req->nr_hw_segments = total_hw_segments; | ||
408 | return 1; | ||
409 | } | ||
410 | |||
411 | /* | ||
412 | * Has to be called with the request spinlock acquired | ||
413 | */ | ||
414 | static int attempt_merge(struct request_queue *q, struct request *req, | ||
415 | struct request *next) | ||
416 | { | ||
417 | if (!rq_mergeable(req) || !rq_mergeable(next)) | ||
418 | return 0; | ||
419 | |||
420 | /* | ||
421 | * not contiguous | ||
422 | */ | ||
423 | if (req->sector + req->nr_sectors != next->sector) | ||
424 | return 0; | ||
425 | |||
426 | if (rq_data_dir(req) != rq_data_dir(next) | ||
427 | || req->rq_disk != next->rq_disk | ||
428 | || next->special) | ||
429 | return 0; | ||
430 | |||
431 | /* | ||
432 | * If we are allowed to merge, then append bio list | ||
433 | * from next to rq and release next. merge_requests_fn | ||
434 | * will have updated segment counts, update sector | ||
435 | * counts here. | ||
436 | */ | ||
437 | if (!ll_merge_requests_fn(q, req, next)) | ||
438 | return 0; | ||
439 | |||
440 | /* | ||
441 | * At this point we have either done a back merge | ||
442 | * or front merge. We need the smaller start_time of | ||
443 | * the merged requests to be the current request | ||
444 | * for accounting purposes. | ||
445 | */ | ||
446 | if (time_after(req->start_time, next->start_time)) | ||
447 | req->start_time = next->start_time; | ||
448 | |||
449 | req->biotail->bi_next = next->bio; | ||
450 | req->biotail = next->biotail; | ||
451 | |||
452 | req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors; | ||
453 | |||
454 | elv_merge_requests(q, req, next); | ||
455 | |||
456 | if (req->rq_disk) { | ||
457 | disk_round_stats(req->rq_disk); | ||
458 | req->rq_disk->in_flight--; | ||
459 | } | ||
460 | |||
461 | req->ioprio = ioprio_best(req->ioprio, next->ioprio); | ||
462 | |||
463 | __blk_put_request(q, next); | ||
464 | return 1; | ||
465 | } | ||
466 | |||
467 | int attempt_back_merge(struct request_queue *q, struct request *rq) | ||
468 | { | ||
469 | struct request *next = elv_latter_request(q, rq); | ||
470 | |||
471 | if (next) | ||
472 | return attempt_merge(q, rq, next); | ||
473 | |||
474 | return 0; | ||
475 | } | ||
476 | |||
477 | int attempt_front_merge(struct request_queue *q, struct request *rq) | ||
478 | { | ||
479 | struct request *prev = elv_former_request(q, rq); | ||
480 | |||
481 | if (prev) | ||
482 | return attempt_merge(q, prev, rq); | ||
483 | |||
484 | return 0; | ||
485 | } | ||
diff --git a/block/blk.h b/block/blk.h index 083394007199..ec898dd0c65c 100644 --- a/block/blk.h +++ b/block/blk.h | |||
@@ -14,8 +14,6 @@ void rq_init(struct request_queue *q, struct request *rq); | |||
14 | void init_request_from_bio(struct request *req, struct bio *bio); | 14 | void init_request_from_bio(struct request *req, struct bio *bio); |
15 | void blk_rq_bio_prep(struct request_queue *q, struct request *rq, | 15 | void blk_rq_bio_prep(struct request_queue *q, struct request *rq, |
16 | struct bio *bio); | 16 | struct bio *bio); |
17 | int ll_back_merge_fn(struct request_queue *q, struct request *req, | ||
18 | struct bio *bio); | ||
19 | void __blk_queue_free_tags(struct request_queue *q); | 17 | void __blk_queue_free_tags(struct request_queue *q); |
20 | 18 | ||
21 | void blk_unplug_work(struct work_struct *work); | 19 | void blk_unplug_work(struct work_struct *work); |
@@ -23,6 +21,15 @@ void blk_unplug_timeout(unsigned long data); | |||
23 | 21 | ||
24 | struct io_context *current_io_context(gfp_t gfp_flags, int node); | 22 | struct io_context *current_io_context(gfp_t gfp_flags, int node); |
25 | 23 | ||
24 | int ll_back_merge_fn(struct request_queue *q, struct request *req, | ||
25 | struct bio *bio); | ||
26 | int ll_front_merge_fn(struct request_queue *q, struct request *req, | ||
27 | struct bio *bio); | ||
28 | int attempt_back_merge(struct request_queue *q, struct request *rq); | ||
29 | int attempt_front_merge(struct request_queue *q, struct request *rq); | ||
30 | void blk_recalc_rq_segments(struct request *rq); | ||
31 | void blk_recalc_rq_sectors(struct request *rq, int nsect); | ||
32 | |||
26 | void blk_queue_congestion_threshold(struct request_queue *q); | 33 | void blk_queue_congestion_threshold(struct request_queue *q); |
27 | 34 | ||
28 | /* | 35 | /* |