1/* 2 * Copyright (C) 1991, 1992 Linus Torvalds 3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics 4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE 5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de> 6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000 7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001 8 */ 9 10/* 11 * This handles all read/write requests to block devices 12 */ 13#include <linux/kernel.h> 14#include <linux/module.h> 15#include <linux/backing-dev.h> 16#include <linux/bio.h> 17#include <linux/blkdev.h> 18#include <linux/highmem.h> 19#include <linux/mm.h> 20#include <linux/kernel_stat.h> 21#include <linux/string.h> 22#include <linux/init.h> 23#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */ 24#include <linux/completion.h> 25#include <linux/slab.h> 26#include <linux/swap.h> 27#include <linux/writeback.h> 28#include <linux/task_io_accounting_ops.h> 29#include <linux/interrupt.h> 30#include <linux/cpu.h> 31#include <linux/blktrace_api.h> 32#include <linux/fault-inject.h> 33 34/* 35 * for max sense size 36 */ 37#include <scsi/scsi_cmnd.h> 38 39static void blk_unplug_work(struct work_struct *work); 40static void blk_unplug_timeout(unsigned long data); 41static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io); 42static void init_request_from_bio(struct request *req, struct bio *bio); 43static int __make_request(request_queue_t *q, struct bio *bio); 44static struct io_context *current_io_context(gfp_t gfp_flags, int node); 45 46/* 47 * For the allocated request tables 48 */ 49static struct kmem_cache *request_cachep; 50 51/* 52 * For queue allocation 53 */ 54static struct kmem_cache *requestq_cachep; 55 56/* 57 * For io context allocations 58 */ 59static struct kmem_cache *iocontext_cachep; 60 61/* 62 * Controlling structure to kblockd 63 */ 64static struct workqueue_struct *kblockd_workqueue; 65 66unsigned long blk_max_low_pfn, blk_max_pfn; 67 68EXPORT_SYMBOL(blk_max_low_pfn); 69EXPORT_SYMBOL(blk_max_pfn); 70 71static DEFINE_PER_CPU(struct list_head, blk_cpu_done); 72 73/* Amount of time in which a process may batch requests */ 74#define BLK_BATCH_TIME (HZ/50UL) 75 76/* Number of requests a "batching" process may submit */ 77#define BLK_BATCH_REQ 32 78 79/* 80 * Return the threshold (number of used requests) at which the queue is 81 * considered to be congested. It include a little hysteresis to keep the 82 * context switch rate down. 83 */ 84static inline int queue_congestion_on_threshold(struct request_queue *q) 85{ 86 return q->nr_congestion_on; 87} 88 89/* 90 * The threshold at which a queue is considered to be uncongested 91 */ 92static inline int queue_congestion_off_threshold(struct request_queue *q) 93{ 94 return q->nr_congestion_off; 95} 96 97static void blk_queue_congestion_threshold(struct request_queue *q) 98{ 99 int nr; 100 101 nr = q->nr_requests - (q->nr_requests / 8) + 1; 102 if (nr > q->nr_requests) 103 nr = q->nr_requests; 104 q->nr_congestion_on = nr; 105 106 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1; 107 if (nr < 1) 108 nr = 1; 109 q->nr_congestion_off = nr; 110} 111 112/** 113 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info 114 * @bdev: device 115 * 116 * Locates the passed device's request queue and returns the address of its 117 * backing_dev_info 118 * 119 * Will return NULL if the request queue cannot be located. 120 */ 121struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev) 122{ 123 struct backing_dev_info *ret = NULL; 124 request_queue_t *q = bdev_get_queue(bdev); 125 126 if (q) 127 ret = &q->backing_dev_info; 128 return ret; 129} 130EXPORT_SYMBOL(blk_get_backing_dev_info); 131 132/** 133 * blk_queue_prep_rq - set a prepare_request function for queue 134 * @q: queue 135 * @pfn: prepare_request function 136 * 137 * It's possible for a queue to register a prepare_request callback which 138 * is invoked before the request is handed to the request_fn. The goal of 139 * the function is to prepare a request for I/O, it can be used to build a 140 * cdb from the request data for instance. 141 * 142 */ 143void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn) 144{ 145 q->prep_rq_fn = pfn; 146} 147 148EXPORT_SYMBOL(blk_queue_prep_rq); 149 150/** 151 * blk_queue_merge_bvec - set a merge_bvec function for queue 152 * @q: queue 153 * @mbfn: merge_bvec_fn 154 * 155 * Usually queues have static limitations on the max sectors or segments that 156 * we can put in a request. Stacking drivers may have some settings that 157 * are dynamic, and thus we have to query the queue whether it is ok to 158 * add a new bio_vec to a bio at a given offset or not. If the block device 159 * has such limitations, it needs to register a merge_bvec_fn to control 160 * the size of bio's sent to it. Note that a block device *must* allow a 161 * single page to be added to an empty bio. The block device driver may want 162 * to use the bio_split() function to deal with these bio's. By default 163 * no merge_bvec_fn is defined for a queue, and only the fixed limits are 164 * honored. 165 */ 166void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn) 167{ 168 q->merge_bvec_fn = mbfn; 169} 170 171EXPORT_SYMBOL(blk_queue_merge_bvec); 172 173void blk_queue_softirq_done(request_queue_t *q, softirq_done_fn *fn) 174{ 175 q->softirq_done_fn = fn; 176} 177 178EXPORT_SYMBOL(blk_queue_softirq_done); 179 180/** 181 * blk_queue_make_request - define an alternate make_request function for a device 182 * @q: the request queue for the device to be affected 183 * @mfn: the alternate make_request function 184 * 185 * Description: 186 * The normal way for &struct bios to be passed to a device 187 * driver is for them to be collected into requests on a request 188 * queue, and then to allow the device driver to select requests 189 * off that queue when it is ready. This works well for many block 190 * devices. However some block devices (typically virtual devices 191 * such as md or lvm) do not benefit from the processing on the 192 * request queue, and are served best by having the requests passed 193 * directly to them. This can be achieved by providing a function 194 * to blk_queue_make_request(). 195 * 196 * Caveat: 197 * The driver that does this *must* be able to deal appropriately 198 * with buffers in "highmemory". This can be accomplished by either calling 199 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling 200 * blk_queue_bounce() to create a buffer in normal memory. 201 **/ 202void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn) 203{ 204 /* 205 * set defaults 206 */ 207 q->nr_requests = BLKDEV_MAX_RQ; 208 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS); 209 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS); 210 q->make_request_fn = mfn; 211 q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; 212 q->backing_dev_info.state = 0; 213 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY; 214 blk_queue_max_sectors(q, SAFE_MAX_SECTORS); 215 blk_queue_hardsect_size(q, 512); 216 blk_queue_dma_alignment(q, 511); 217 blk_queue_congestion_threshold(q); 218 q->nr_batching = BLK_BATCH_REQ; 219 220 q->unplug_thresh = 4; /* hmm */ 221 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */ 222 if (q->unplug_delay == 0) 223 q->unplug_delay = 1; 224 225 INIT_WORK(&q->unplug_work, blk_unplug_work); 226 227 q->unplug_timer.function = blk_unplug_timeout; 228 q->unplug_timer.data = (unsigned long)q; 229 230 /* 231 * by default assume old behaviour and bounce for any highmem page 232 */ 233 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); 234} 235 236EXPORT_SYMBOL(blk_queue_make_request); 237 238static void rq_init(request_queue_t *q, struct request *rq) 239{ 240 INIT_LIST_HEAD(&rq->queuelist); 241 INIT_LIST_HEAD(&rq->donelist); 242 243 rq->errors = 0; 244 rq->bio = rq->biotail = NULL; 245 INIT_HLIST_NODE(&rq->hash); 246 RB_CLEAR_NODE(&rq->rb_node); 247 rq->ioprio = 0; 248 rq->buffer = NULL; 249 rq->ref_count = 1; 250 rq->q = q; 251 rq->special = NULL; 252 rq->data_len = 0; 253 rq->data = NULL; 254 rq->nr_phys_segments = 0; 255 rq->sense = NULL; 256 rq->end_io = NULL; 257 rq->end_io_data = NULL; 258 rq->completion_data = NULL; 259} 260 261/** 262 * blk_queue_ordered - does this queue support ordered writes 263 * @q: the request queue 264 * @ordered: one of QUEUE_ORDERED_* 265 * @prepare_flush_fn: rq setup helper for cache flush ordered writes 266 * 267 * Description: 268 * For journalled file systems, doing ordered writes on a commit 269 * block instead of explicitly doing wait_on_buffer (which is bad 270 * for performance) can be a big win. Block drivers supporting this 271 * feature should call this function and indicate so. 272 * 273 **/ 274int blk_queue_ordered(request_queue_t *q, unsigned ordered, 275 prepare_flush_fn *prepare_flush_fn) 276{ 277 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) && 278 prepare_flush_fn == NULL) { 279 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n"); 280 return -EINVAL; 281 } 282 283 if (ordered != QUEUE_ORDERED_NONE && 284 ordered != QUEUE_ORDERED_DRAIN && 285 ordered != QUEUE_ORDERED_DRAIN_FLUSH && 286 ordered != QUEUE_ORDERED_DRAIN_FUA && 287 ordered != QUEUE_ORDERED_TAG && 288 ordered != QUEUE_ORDERED_TAG_FLUSH && 289 ordered != QUEUE_ORDERED_TAG_FUA) { 290 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered); 291 return -EINVAL; 292 } 293 294 q->ordered = ordered; 295 q->next_ordered = ordered; 296 q->prepare_flush_fn = prepare_flush_fn; 297 298 return 0; 299} 300 301EXPORT_SYMBOL(blk_queue_ordered); 302 303/** 304 * blk_queue_issue_flush_fn - set function for issuing a flush 305 * @q: the request queue 306 * @iff: the function to be called issuing the flush 307 * 308 * Description: 309 * If a driver supports issuing a flush command, the support is notified 310 * to the block layer by defining it through this call. 311 * 312 **/ 313void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff) 314{ 315 q->issue_flush_fn = iff; 316} 317 318EXPORT_SYMBOL(blk_queue_issue_flush_fn); 319 320/* 321 * Cache flushing for ordered writes handling 322 */ 323inline unsigned blk_ordered_cur_seq(request_queue_t *q) 324{ 325 if (!q->ordseq) 326 return 0; 327 return 1 << ffz(q->ordseq); 328} 329 330unsigned blk_ordered_req_seq(struct request *rq) 331{ 332 request_queue_t *q = rq->q; 333 334 BUG_ON(q->ordseq == 0); 335 336 if (rq == &q->pre_flush_rq) 337 return QUEUE_ORDSEQ_PREFLUSH; 338 if (rq == &q->bar_rq) 339 return QUEUE_ORDSEQ_BAR; 340 if (rq == &q->post_flush_rq) 341 return QUEUE_ORDSEQ_POSTFLUSH; 342 343 /* 344 * !fs requests don't need to follow barrier ordering. Always 345 * put them at the front. This fixes the following deadlock. 346 * 347 * http://thread.gmane.org/gmane.linux.kernel/537473 348 */ 349 if (!blk_fs_request(rq)) 350 return QUEUE_ORDSEQ_DRAIN; 351 352 if ((rq->cmd_flags & REQ_ORDERED_COLOR) == 353 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR)) 354 return QUEUE_ORDSEQ_DRAIN; 355 else 356 return QUEUE_ORDSEQ_DONE; 357} 358 359void blk_ordered_complete_seq(request_queue_t *q, unsigned seq, int error) 360{ 361 struct request *rq; 362 int uptodate; 363 364 if (error && !q->orderr) 365 q->orderr = error; 366 367 BUG_ON(q->ordseq & seq); 368 q->ordseq |= seq; 369 370 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE) 371 return; 372 373 /* 374 * Okay, sequence complete. 375 */ 376 rq = q->orig_bar_rq; 377 uptodate = q->orderr ? q->orderr : 1; 378 379 q->ordseq = 0; 380 381 end_that_request_first(rq, uptodate, rq->hard_nr_sectors); 382 end_that_request_last(rq, uptodate); 383} 384 385static void pre_flush_end_io(struct request *rq, int error) 386{ 387 elv_completed_request(rq->q, rq); 388 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error); 389} 390 391static void bar_end_io(struct request *rq, int error) 392{ 393 elv_completed_request(rq->q, rq); 394 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error); 395} 396 397static void post_flush_end_io(struct request *rq, int error) 398{ 399 elv_completed_request(rq->q, rq); 400 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error); 401} 402 403static void queue_flush(request_queue_t *q, unsigned which) 404{ 405 struct request *rq; 406 rq_end_io_fn *end_io; 407 408 if (which == QUEUE_ORDERED_PREFLUSH) { 409 rq = &q->pre_flush_rq; 410 end_io = pre_flush_end_io; 411 } else { 412 rq = &q->post_flush_rq; 413 end_io = post_flush_end_io; 414 } 415 416 rq->cmd_flags = REQ_HARDBARRIER; 417 rq_init(q, rq); 418 rq->elevator_private = NULL; 419 rq->elevator_private2 = NULL; 420 rq->rq_disk = q->bar_rq.rq_disk; 421 rq->end_io = end_io; 422 q->prepare_flush_fn(q, rq); 423 424 elv_insert(q, rq, ELEVATOR_INSERT_FRONT); 425} 426 427static inline struct request *start_ordered(request_queue_t *q, 428 struct request *rq) 429{ 430 q->bi_size = 0; 431 q->orderr = 0; 432 q->ordered = q->next_ordered; 433 q->ordseq |= QUEUE_ORDSEQ_STARTED; 434 435 /* 436 * Prep proxy barrier request. 437 */ 438 blkdev_dequeue_request(rq); 439 q->orig_bar_rq = rq; 440 rq = &q->bar_rq; 441 rq->cmd_flags = 0; 442 rq_init(q, rq); 443 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE) 444 rq->cmd_flags |= REQ_RW; 445 rq->cmd_flags |= q->ordered & QUEUE_ORDERED_FUA ? REQ_FUA : 0; 446 rq->elevator_private = NULL; 447 rq->elevator_private2 = NULL; 448 init_request_from_bio(rq, q->orig_bar_rq->bio); 449 rq->end_io = bar_end_io; 450 451 /* 452 * Queue ordered sequence. As we stack them at the head, we 453 * need to queue in reverse order. Note that we rely on that 454 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs 455 * request gets inbetween ordered sequence. 456 */ 457 if (q->ordered & QUEUE_ORDERED_POSTFLUSH) 458 queue_flush(q, QUEUE_ORDERED_POSTFLUSH); 459 else 460 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH; 461 462 elv_insert(q, rq, ELEVATOR_INSERT_FRONT); 463 464 if (q->ordered & QUEUE_ORDERED_PREFLUSH) { 465 queue_flush(q, QUEUE_ORDERED_PREFLUSH); 466 rq = &q->pre_flush_rq; 467 } else 468 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH; 469 470 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0) 471 q->ordseq |= QUEUE_ORDSEQ_DRAIN; 472 else 473 rq = NULL; 474 475 return rq; 476} 477 478int blk_do_ordered(request_queue_t *q, struct request **rqp) 479{ 480 struct request *rq = *rqp; 481 int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq); 482 483 if (!q->ordseq) { 484 if (!is_barrier) 485 return 1; 486 487 if (q->next_ordered != QUEUE_ORDERED_NONE) { 488 *rqp = start_ordered(q, rq); 489 return 1; 490 } else { 491 /* 492 * This can happen when the queue switches to 493 * ORDERED_NONE while this request is on it. 494 */ 495 blkdev_dequeue_request(rq); 496 end_that_request_first(rq, -EOPNOTSUPP, 497 rq->hard_nr_sectors); 498 end_that_request_last(rq, -EOPNOTSUPP); 499 *rqp = NULL; 500 return 0; 501 } 502 } 503 504 /* 505 * Ordered sequence in progress 506 */ 507 508 /* Special requests are not subject to ordering rules. */ 509 if (!blk_fs_request(rq) && 510 rq != &q->pre_flush_rq && rq != &q->post_flush_rq) 511 return 1; 512 513 if (q->ordered & QUEUE_ORDERED_TAG) { 514 /* Ordered by tag. Blocking the next barrier is enough. */ 515 if (is_barrier && rq != &q->bar_rq) 516 *rqp = NULL; 517 } else { 518 /* Ordered by draining. Wait for turn. */ 519 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q)); 520 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q)) 521 *rqp = NULL; 522 } 523 524 return 1; 525} 526 527static int flush_dry_bio_endio(struct bio *bio, unsigned int bytes, int error) 528{ 529 request_queue_t *q = bio->bi_private; 530 struct bio_vec *bvec; 531 int i; 532 533 /* 534 * This is dry run, restore bio_sector and size. We'll finish 535 * this request again with the original bi_end_io after an 536 * error occurs or post flush is complete. 537 */ 538 q->bi_size += bytes; 539 540 if (bio->bi_size) 541 return 1; 542 543 /* Rewind bvec's */ 544 bio->bi_idx = 0; 545 bio_for_each_segment(bvec, bio, i) { 546 bvec->bv_len += bvec->bv_offset; 547 bvec->bv_offset = 0; 548 } 549 550 /* Reset bio */ 551 set_bit(BIO_UPTODATE, &bio->bi_flags); 552 bio->bi_size = q->bi_size; 553 bio->bi_sector -= (q->bi_size >> 9); 554 q->bi_size = 0; 555 556 return 0; 557} 558 559static int ordered_bio_endio(struct request *rq, struct bio *bio, 560 unsigned int nbytes, int error) 561{ 562 request_queue_t *q = rq->q; 563 bio_end_io_t *endio; 564 void *private; 565 566 if (&q->bar_rq != rq) 567 return 0; 568 569 /* 570 * Okay, this is the barrier request in progress, dry finish it. 571 */ 572 if (error && !q->orderr) 573 q->orderr = error; 574 575 endio = bio->bi_end_io; 576 private = bio->bi_private; 577 bio->bi_end_io = flush_dry_bio_endio; 578 bio->bi_private = q; 579 580 bio_endio(bio, nbytes, error); 581 582 bio->bi_end_io = endio; 583 bio->bi_private = private; 584 585 return 1; 586} 587 588/** 589 * blk_queue_bounce_limit - set bounce buffer limit for queue 590 * @q: the request queue for the device 591 * @dma_addr: bus address limit 592 * 593 * Description: 594 * Different hardware can have different requirements as to what pages 595 * it can do I/O directly to. A low level driver can call 596 * blk_queue_bounce_limit to have lower memory pages allocated as bounce 597 * buffers for doing I/O to pages residing above @page. 598 **/ 599void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr) 600{ 601 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT; 602 int dma = 0; 603 604 q->bounce_gfp = GFP_NOIO; 605#if BITS_PER_LONG == 64 606 /* Assume anything <= 4GB can be handled by IOMMU. 607 Actually some IOMMUs can handle everything, but I don't 608 know of a way to test this here. */ 609 if (bounce_pfn < (min_t(u64,0xffffffff,BLK_BOUNCE_HIGH) >> PAGE_SHIFT)) 610 dma = 1; 611 q->bounce_pfn = max_low_pfn; 612#else 613 if (bounce_pfn < blk_max_low_pfn) 614 dma = 1; 615 q->bounce_pfn = bounce_pfn; 616#endif 617 if (dma) { 618 init_emergency_isa_pool(); 619 q->bounce_gfp = GFP_NOIO | GFP_DMA; 620 q->bounce_pfn = bounce_pfn; 621 } 622} 623 624EXPORT_SYMBOL(blk_queue_bounce_limit); 625 626/** 627 * blk_queue_max_sectors - set max sectors for a request for this queue 628 * @q: the request queue for the device 629 * @max_sectors: max sectors in the usual 512b unit 630 * 631 * Description: 632 * Enables a low level driver to set an upper limit on the size of 633 * received requests. 634 **/ 635void blk_queue_max_sectors(request_queue_t *q, unsigned int max_sectors) 636{ 637 if ((max_sectors << 9) < PAGE_CACHE_SIZE) { 638 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); 639 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors); 640 } 641 642 if (BLK_DEF_MAX_SECTORS > max_sectors) 643 q->max_hw_sectors = q->max_sectors = max_sectors; 644 else { 645 q->max_sectors = BLK_DEF_MAX_SECTORS; 646 q->max_hw_sectors = max_sectors; 647 } 648} 649 650EXPORT_SYMBOL(blk_queue_max_sectors); 651 652/** 653 * blk_queue_max_phys_segments - set max phys segments for a request for this queue 654 * @q: the request queue for the device 655 * @max_segments: max number of segments 656 * 657 * Description: 658 * Enables a low level driver to set an upper limit on the number of 659 * physical data segments in a request. This would be the largest sized 660 * scatter list the driver could handle. 661 **/ 662void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments) 663{ 664 if (!max_segments) { 665 max_segments = 1; 666 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments); 667 } 668 669 q->max_phys_segments = max_segments; 670} 671 672EXPORT_SYMBOL(blk_queue_max_phys_segments); 673 674/** 675 * blk_queue_max_hw_segments - set max hw segments for a request for this queue 676 * @q: the request queue for the device 677 * @max_segments: max number of segments 678 * 679 * Description: 680 * Enables a low level driver to set an upper limit on the number of 681 * hw data segments in a request. This would be the largest number of 682 * address/length pairs the host adapter can actually give as once 683 * to the device. 684 **/ 685void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments) 686{ 687 if (!max_segments) { 688 max_segments = 1; 689 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments); 690 } 691 692 q->max_hw_segments = max_segments; 693} 694 695EXPORT_SYMBOL(blk_queue_max_hw_segments); 696 697/** 698 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg 699 * @q: the request queue for the device 700 * @max_size: max size of segment in bytes 701 * 702 * Description: 703 * Enables a low level driver to set an upper limit on the size of a 704 * coalesced segment 705 **/ 706void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size) 707{ 708 if (max_size < PAGE_CACHE_SIZE) { 709 max_size = PAGE_CACHE_SIZE; 710 printk("%s: set to minimum %d\n", __FUNCTION__, max_size); 711 } 712 713 q->max_segment_size = max_size; 714} 715 716EXPORT_SYMBOL(blk_queue_max_segment_size); 717 718/** 719 * blk_queue_hardsect_size - set hardware sector size for the queue 720 * @q: the request queue for the device 721 * @size: the hardware sector size, in bytes 722 * 723 * Description: 724 * This should typically be set to the lowest possible sector size 725 * that the hardware can operate on (possible without reverting to 726 * even internal read-modify-write operations). Usually the default 727 * of 512 covers most hardware. 728 **/ 729void blk_queue_hardsect_size(request_queue_t *q, unsigned short size) 730{ 731 q->hardsect_size = size; 732} 733 734EXPORT_SYMBOL(blk_queue_hardsect_size); 735 736/* 737 * Returns the minimum that is _not_ zero, unless both are zero. 738 */ 739#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r)) 740 741/** 742 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers 743 * @t: the stacking driver (top) 744 * @b: the underlying device (bottom) 745 **/ 746void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b) 747{ 748 /* zero is "infinity" */ 749 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors); 750 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors); 751 752 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments); 753 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments); 754 t->max_segment_size = min(t->max_segment_size,b->max_segment_size); 755 t->hardsect_size = max(t->hardsect_size,b->hardsect_size); 756 if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) 757 clear_bit(QUEUE_FLAG_CLUSTER, &t->queue_flags); 758} 759 760EXPORT_SYMBOL(blk_queue_stack_limits); 761 762/** 763 * blk_queue_segment_boundary - set boundary rules for segment merging 764 * @q: the request queue for the device 765 * @mask: the memory boundary mask 766 **/ 767void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask) 768{ 769 if (mask < PAGE_CACHE_SIZE - 1) { 770 mask = PAGE_CACHE_SIZE - 1; 771 printk("%s: set to minimum %lx\n", __FUNCTION__, mask); 772 } 773 774 q->seg_boundary_mask = mask; 775} 776 777EXPORT_SYMBOL(blk_queue_segment_boundary); 778 779/** 780 * blk_queue_dma_alignment - set dma length and memory alignment 781 * @q: the request queue for the device 782 * @mask: alignment mask 783 * 784 * description: 785 * set required memory and length aligment for direct dma transactions. 786 * this is used when buiding direct io requests for the queue. 787 * 788 **/ 789void blk_queue_dma_alignment(request_queue_t *q, int mask) 790{ 791 q->dma_alignment = mask; 792} 793 794EXPORT_SYMBOL(blk_queue_dma_alignment); 795 796/** 797 * blk_queue_find_tag - find a request by its tag and queue 798 * @q: The request queue for the device 799 * @tag: The tag of the request 800 * 801 * Notes: 802 * Should be used when a device returns a tag and you want to match 803 * it with a request. 804 * 805 * no locks need be held. 806 **/ 807struct request *blk_queue_find_tag(request_queue_t *q, int tag) 808{ 809 return blk_map_queue_find_tag(q->queue_tags, tag); 810} 811 812EXPORT_SYMBOL(blk_queue_find_tag); 813 814/** 815 * __blk_free_tags - release a given set of tag maintenance info 816 * @bqt: the tag map to free 817 * 818 * Tries to free the specified @bqt@. Returns true if it was 819 * actually freed and false if there are still references using it 820 */ 821static int __blk_free_tags(struct blk_queue_tag *bqt) 822{ 823 int retval; 824 825 retval = atomic_dec_and_test(&bqt->refcnt); 826 if (retval) { 827 BUG_ON(bqt->busy); 828 BUG_ON(!list_empty(&bqt->busy_list)); 829 830 kfree(bqt->tag_index); 831 bqt->tag_index = NULL; 832 833 kfree(bqt->tag_map); 834 bqt->tag_map = NULL; 835 836 kfree(bqt); 837 838 } 839 840 return retval; 841} 842 843/** 844 * __blk_queue_free_tags - release tag maintenance info 845 * @q: the request queue for the device 846 * 847 * Notes: 848 * blk_cleanup_queue() will take care of calling this function, if tagging 849 * has been used. So there's no need to call this directly. 850 **/ 851static void __blk_queue_free_tags(request_queue_t *q) 852{ 853 struct blk_queue_tag *bqt = q->queue_tags; 854 855 if (!bqt) 856 return; 857 858 __blk_free_tags(bqt); 859 860 q->queue_tags = NULL; 861 q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED); 862} 863 864 865/** 866 * blk_free_tags - release a given set of tag maintenance info 867 * @bqt: the tag map to free 868 * 869 * For externally managed @bqt@ frees the map. Callers of this 870 * function must guarantee to have released all the queues that 871 * might have been using this tag map. 872 */ 873void blk_free_tags(struct blk_queue_tag *bqt) 874{ 875 if (unlikely(!__blk_free_tags(bqt))) 876 BUG(); 877} 878EXPORT_SYMBOL(blk_free_tags); 879 880/** 881 * blk_queue_free_tags - release tag maintenance info 882 * @q: the request queue for the device 883 * 884 * Notes: 885 * This is used to disabled tagged queuing to a device, yet leave 886 * queue in function. 887 **/ 888void blk_queue_free_tags(request_queue_t *q) 889{ 890 clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags); 891} 892 893EXPORT_SYMBOL(blk_queue_free_tags); 894 895static int 896init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth) 897{ 898 struct request **tag_index; 899 unsigned long *tag_map; 900 int nr_ulongs; 901 902 if (q && depth > q->nr_requests * 2) { 903 depth = q->nr_requests * 2; 904 printk(KERN_ERR "%s: adjusted depth to %d\n", 905 __FUNCTION__, depth); 906 } 907 908 tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC); 909 if (!tag_index) 910 goto fail; 911 912 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG; 913 tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC); 914 if (!tag_map) 915 goto fail; 916 917 tags->real_max_depth = depth; 918 tags->max_depth = depth; 919 tags->tag_index = tag_index; 920 tags->tag_map = tag_map; 921 922 return 0; 923fail: 924 kfree(tag_index); 925 return -ENOMEM; 926} 927 928static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q, 929 int depth) 930{ 931 struct blk_queue_tag *tags; 932 933 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC); 934 if (!tags) 935 goto fail; 936 937 if (init_tag_map(q, tags, depth)) 938 goto fail; 939 940 INIT_LIST_HEAD(&tags->busy_list); 941 tags->busy = 0; 942 atomic_set(&tags->refcnt, 1); 943 return tags; 944fail: 945 kfree(tags); 946 return NULL; 947} 948 949/** 950 * blk_init_tags - initialize the tag info for an external tag map 951 * @depth: the maximum queue depth supported 952 * @tags: the tag to use 953 **/ 954struct blk_queue_tag *blk_init_tags(int depth) 955{ 956 return __blk_queue_init_tags(NULL, depth); 957} 958EXPORT_SYMBOL(blk_init_tags); 959 960/** 961 * blk_queue_init_tags - initialize the queue tag info 962 * @q: the request queue for the device 963 * @depth: the maximum queue depth supported 964 * @tags: the tag to use 965 **/ 966int blk_queue_init_tags(request_queue_t *q, int depth, 967 struct blk_queue_tag *tags) 968{ 969 int rc; 970 971 BUG_ON(tags && q->queue_tags && tags != q->queue_tags); 972 973 if (!tags && !q->queue_tags) { 974 tags = __blk_queue_init_tags(q, depth); 975 976 if (!tags) 977 goto fail; 978 } else if (q->queue_tags) { 979 if ((rc = blk_queue_resize_tags(q, depth))) 980 return rc; 981 set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags); 982 return 0; 983 } else 984 atomic_inc(&tags->refcnt); 985 986 /* 987 * assign it, all done 988 */ 989 q->queue_tags = tags; 990 q->queue_flags |= (1 << QUEUE_FLAG_QUEUED); 991 return 0; 992fail: 993 kfree(tags); 994 return -ENOMEM; 995} 996 997EXPORT_SYMBOL(blk_queue_init_tags); 998 999/** 1000 * blk_queue_resize_tags - change the queueing depth 1001 * @q: the request queue for the device 1002 * @new_depth: the new max command queueing depth 1003 * 1004 * Notes: 1005 * Must be called with the queue lock held. 1006 **/ 1007int blk_queue_resize_tags(request_queue_t *q, int new_depth) 1008{ 1009 struct blk_queue_tag *bqt = q->queue_tags; 1010 struct request **tag_index; 1011 unsigned long *tag_map; 1012 int max_depth, nr_ulongs; 1013 1014 if (!bqt) 1015 return -ENXIO; 1016 1017 /* 1018 * if we already have large enough real_max_depth. just 1019 * adjust max_depth. *NOTE* as requests with tag value 1020 * between new_depth and real_max_depth can be in-flight, tag 1021 * map can not be shrunk blindly here. 1022 */ 1023 if (new_depth <= bqt->real_max_depth) { 1024 bqt->max_depth = new_depth; 1025 return 0; 1026 } 1027 1028 /* 1029 * Currently cannot replace a shared tag map with a new 1030 * one, so error out if this is the case 1031 */ 1032 if (atomic_read(&bqt->refcnt) != 1) 1033 return -EBUSY; 1034 1035 /* 1036 * save the old state info, so we can copy it back 1037 */ 1038 tag_index = bqt->tag_index; 1039 tag_map = bqt->tag_map; 1040 max_depth = bqt->real_max_depth; 1041 1042 if (init_tag_map(q, bqt, new_depth)) 1043 return -ENOMEM; 1044 1045 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *)); 1046 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG; 1047 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long)); 1048 1049 kfree(tag_index); 1050 kfree(tag_map); 1051 return 0; 1052} 1053 1054EXPORT_SYMBOL(blk_queue_resize_tags); 1055 1056/** 1057 * blk_queue_end_tag - end tag operations for a request 1058 * @q: the request queue for the device 1059 * @rq: the request that has completed 1060 * 1061 * Description: 1062 * Typically called when end_that_request_first() returns 0, meaning 1063 * all transfers have been done for a request. It's important to call 1064 * this function before end_that_request_last(), as that will put the 1065 * request back on the free list thus corrupting the internal tag list. 1066 * 1067 * Notes: 1068 * queue lock must be held. 1069 **/ 1070void blk_queue_end_tag(request_queue_t *q, struct request *rq) 1071{ 1072 struct blk_queue_tag *bqt = q->queue_tags; 1073 int tag = rq->tag; 1074 1075 BUG_ON(tag == -1); 1076 1077 if (unlikely(tag >= bqt->real_max_depth)) 1078 return; 1079 1080 if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) { 1081 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n", 1082 __FUNCTION__, tag); 1083 return; 1084 } 1085 1086 list_del_init(&rq->queuelist); 1087 rq->cmd_flags &= ~REQ_QUEUED; 1088 rq->tag = -1; 1089 1090 if (unlikely(bqt->tag_index[tag] == NULL)) 1091 printk(KERN_ERR "%s: tag %d is missing\n", 1092 __FUNCTION__, tag); 1093 1094 bqt->tag_index[tag] = NULL; 1095 bqt->busy--; 1096} 1097 1098EXPORT_SYMBOL(blk_queue_end_tag); 1099 1100/** 1101 * blk_queue_start_tag - find a free tag and assign it 1102 * @q: the request queue for the device 1103 * @rq: the block request that needs tagging 1104 * 1105 * Description: 1106 * This can either be used as a stand-alone helper, or possibly be 1107 * assigned as the queue &prep_rq_fn (in which case &struct request 1108 * automagically gets a tag assigned). Note that this function 1109 * assumes that any type of request can be queued! if this is not 1110 * true for your device, you must check the request type before 1111 * calling this function. The request will also be removed from 1112 * the request queue, so it's the drivers responsibility to readd 1113 * it if it should need to be restarted for some reason. 1114 * 1115 * Notes: 1116 * queue lock must be held. 1117 **/ 1118int blk_queue_start_tag(request_queue_t *q, struct request *rq) 1119{ 1120 struct blk_queue_tag *bqt = q->queue_tags; 1121 int tag; 1122 1123 if (unlikely((rq->cmd_flags & REQ_QUEUED))) { 1124 printk(KERN_ERR 1125 "%s: request %p for device [%s] already tagged %d", 1126 __FUNCTION__, rq, 1127 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag); 1128 BUG(); 1129 } 1130 1131 /* 1132 * Protect against shared tag maps, as we may not have exclusive 1133 * access to the tag map. 1134 */ 1135 do { 1136 tag = find_first_zero_bit(bqt->tag_map, bqt->max_depth); 1137 if (tag >= bqt->max_depth) 1138 return 1; 1139 1140 } while (test_and_set_bit(tag, bqt->tag_map)); 1141 1142 rq->cmd_flags |= REQ_QUEUED; 1143 rq->tag = tag; 1144 bqt->tag_index[tag] = rq; 1145 blkdev_dequeue_request(rq); 1146 list_add(&rq->queuelist, &bqt->busy_list); 1147 bqt->busy++; 1148 return 0; 1149} 1150 1151EXPORT_SYMBOL(blk_queue_start_tag); 1152 1153/** 1154 * blk_queue_invalidate_tags - invalidate all pending tags 1155 * @q: the request queue for the device 1156 * 1157 * Description: 1158 * Hardware conditions may dictate a need to stop all pending requests. 1159 * In this case, we will safely clear the block side of the tag queue and 1160 * readd all requests to the request queue in the right order. 1161 * 1162 * Notes: 1163 * queue lock must be held. 1164 **/ 1165void blk_queue_invalidate_tags(request_queue_t *q) 1166{ 1167 struct blk_queue_tag *bqt = q->queue_tags; 1168 struct list_head *tmp, *n; 1169 struct request *rq; 1170 1171 list_for_each_safe(tmp, n, &bqt->busy_list) { 1172 rq = list_entry_rq(tmp); 1173 1174 if (rq->tag == -1) { 1175 printk(KERN_ERR 1176 "%s: bad tag found on list\n", __FUNCTION__); 1177 list_del_init(&rq->queuelist); 1178 rq->cmd_flags &= ~REQ_QUEUED; 1179 } else 1180 blk_queue_end_tag(q, rq); 1181 1182 rq->cmd_flags &= ~REQ_STARTED; 1183 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0); 1184 } 1185} 1186 1187EXPORT_SYMBOL(blk_queue_invalidate_tags); 1188 1189void blk_dump_rq_flags(struct request *rq, char *msg) 1190{ 1191 int bit; 1192 1193 printk("%s: dev %s: type=%x, flags=%x\n", msg, 1194 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type, 1195 rq->cmd_flags); 1196 1197 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector, 1198 rq->nr_sectors, 1199 rq->current_nr_sectors); 1200 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len); 1201 1202 if (blk_pc_request(rq)) { 1203 printk("cdb: "); 1204 for (bit = 0; bit < sizeof(rq->cmd); bit++) 1205 printk("%02x ", rq->cmd[bit]); 1206 printk("\n"); 1207 } 1208} 1209 1210EXPORT_SYMBOL(blk_dump_rq_flags); 1211 1212void blk_recount_segments(request_queue_t *q, struct bio *bio) 1213{ 1214 struct bio_vec *bv, *bvprv = NULL; 1215 int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster; 1216 int high, highprv = 1; 1217 1218 if (unlikely(!bio->bi_io_vec)) 1219 return; 1220 1221 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER); 1222 hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0; 1223 bio_for_each_segment(bv, bio, i) { 1224 /* 1225 * the trick here is making sure that a high page is never 1226 * considered part of another segment, since that might 1227 * change with the bounce page. 1228 */ 1229 high = page_to_pfn(bv->bv_page) > q->bounce_pfn; 1230 if (high || highprv) 1231 goto new_hw_segment; 1232 if (cluster) { 1233 if (seg_size + bv->bv_len > q->max_segment_size) 1234 goto new_segment; 1235 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv)) 1236 goto new_segment; 1237 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv)) 1238 goto new_segment; 1239 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) 1240 goto new_hw_segment; 1241 1242 seg_size += bv->bv_len; 1243 hw_seg_size += bv->bv_len; 1244 bvprv = bv; 1245 continue; 1246 } 1247new_segment: 1248 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) && 1249 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) { 1250 hw_seg_size += bv->bv_len; 1251 } else { 1252new_hw_segment: 1253 if (hw_seg_size > bio->bi_hw_front_size) 1254 bio->bi_hw_front_size = hw_seg_size; 1255 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len; 1256 nr_hw_segs++; 1257 } 1258 1259 nr_phys_segs++; 1260 bvprv = bv; 1261 seg_size = bv->bv_len; 1262 highprv = high; 1263 } 1264 if (hw_seg_size > bio->bi_hw_back_size) 1265 bio->bi_hw_back_size = hw_seg_size; 1266 if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size) 1267 bio->bi_hw_front_size = hw_seg_size; 1268 bio->bi_phys_segments = nr_phys_segs; 1269 bio->bi_hw_segments = nr_hw_segs; 1270 bio->bi_flags |= (1 << BIO_SEG_VALID); 1271} 1272EXPORT_SYMBOL(blk_recount_segments); 1273 1274static int blk_phys_contig_segment(request_queue_t *q, struct bio *bio, 1275 struct bio *nxt) 1276{ 1277 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER))) 1278 return 0; 1279 1280 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt))) 1281 return 0; 1282 if (bio->bi_size + nxt->bi_size > q->max_segment_size) 1283 return 0; 1284 1285 /* 1286 * bio and nxt are contigous in memory, check if the queue allows 1287 * these two to be merged into one 1288 */ 1289 if (BIO_SEG_BOUNDARY(q, bio, nxt)) 1290 return 1; 1291 1292 return 0; 1293} 1294 1295static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio, 1296 struct bio *nxt) 1297{ 1298 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) 1299 blk_recount_segments(q, bio); 1300 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID))) 1301 blk_recount_segments(q, nxt); 1302 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) || 1303 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size)) 1304 return 0; 1305 if (bio->bi_size + nxt->bi_size > q->max_segment_size) 1306 return 0; 1307 1308 return 1; 1309} 1310 1311/* 1312 * map a request to scatterlist, return number of sg entries setup. Caller 1313 * must make sure sg can hold rq->nr_phys_segments entries 1314 */ 1315int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg) 1316{ 1317 struct bio_vec *bvec, *bvprv; 1318 struct bio *bio; 1319 int nsegs, i, cluster; 1320 1321 nsegs = 0; 1322 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER); 1323 1324 /* 1325 * for each bio in rq 1326 */ 1327 bvprv = NULL; 1328 rq_for_each_bio(bio, rq) { 1329 /* 1330 * for each segment in bio 1331 */ 1332 bio_for_each_segment(bvec, bio, i) { 1333 int nbytes = bvec->bv_len; 1334 1335 if (bvprv && cluster) { 1336 if (sg[nsegs - 1].length + nbytes > q->max_segment_size) 1337 goto new_segment; 1338 1339 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 1340 goto new_segment; 1341 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 1342 goto new_segment; 1343 1344 sg[nsegs - 1].length += nbytes; 1345 } else { 1346new_segment: 1347 memset(&sg[nsegs],0,sizeof(struct scatterlist)); 1348 sg[nsegs].page = bvec->bv_page; 1349 sg[nsegs].length = nbytes; 1350 sg[nsegs].offset = bvec->bv_offset; 1351 1352 nsegs++; 1353 } 1354 bvprv = bvec; 1355 } /* segments in bio */ 1356 } /* bios in rq */ 1357 1358 return nsegs; 1359} 1360 1361EXPORT_SYMBOL(blk_rq_map_sg); 1362 1363/* 1364 * the standard queue merge functions, can be overridden with device 1365 * specific ones if so desired 1366 */ 1367 1368static inline int ll_new_mergeable(request_queue_t *q, 1369 struct request *req, 1370 struct bio *bio) 1371{ 1372 int nr_phys_segs = bio_phys_segments(q, bio); 1373 1374 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { 1375 req->cmd_flags |= REQ_NOMERGE; 1376 if (req == q->last_merge) 1377 q->last_merge = NULL; 1378 return 0; 1379 } 1380 1381 /* 1382 * A hw segment is just getting larger, bump just the phys 1383 * counter. 1384 */ 1385 req->nr_phys_segments += nr_phys_segs; 1386 return 1; 1387} 1388 1389static inline int ll_new_hw_segment(request_queue_t *q, 1390 struct request *req, 1391 struct bio *bio) 1392{ 1393 int nr_hw_segs = bio_hw_segments(q, bio); 1394 int nr_phys_segs = bio_phys_segments(q, bio); 1395 1396 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments 1397 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { 1398 req->cmd_flags |= REQ_NOMERGE; 1399 if (req == q->last_merge) 1400 q->last_merge = NULL; 1401 return 0; 1402 } 1403 1404 /* 1405 * This will form the start of a new hw segment. Bump both 1406 * counters. 1407 */ 1408 req->nr_hw_segments += nr_hw_segs; 1409 req->nr_phys_segments += nr_phys_segs; 1410 return 1; 1411} 1412 1413int ll_back_merge_fn(request_queue_t *q, struct request *req, struct bio *bio) 1414{ 1415 unsigned short max_sectors; 1416 int len; 1417 1418 if (unlikely(blk_pc_request(req))) 1419 max_sectors = q->max_hw_sectors; 1420 else 1421 max_sectors = q->max_sectors; 1422 1423 if (req->nr_sectors + bio_sectors(bio) > max_sectors) { 1424 req->cmd_flags |= REQ_NOMERGE; 1425 if (req == q->last_merge) 1426 q->last_merge = NULL; 1427 return 0; 1428 } 1429 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID))) 1430 blk_recount_segments(q, req->biotail); 1431 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) 1432 blk_recount_segments(q, bio); 1433 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size; 1434 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) && 1435 !BIOVEC_VIRT_OVERSIZE(len)) { 1436 int mergeable = ll_new_mergeable(q, req, bio); 1437 1438 if (mergeable) { 1439 if (req->nr_hw_segments == 1) 1440 req->bio->bi_hw_front_size = len; 1441 if (bio->bi_hw_segments == 1) 1442 bio->bi_hw_back_size = len; 1443 } 1444 return mergeable; 1445 } 1446 1447 return ll_new_hw_segment(q, req, bio); 1448} 1449EXPORT_SYMBOL(ll_back_merge_fn); 1450 1451static int ll_front_merge_fn(request_queue_t *q, struct request *req, 1452 struct bio *bio) 1453{ 1454 unsigned short max_sectors; 1455 int len; 1456 1457 if (unlikely(blk_pc_request(req))) 1458 max_sectors = q->max_hw_sectors; 1459 else 1460 max_sectors = q->max_sectors; 1461 1462 1463 if (req->nr_sectors + bio_sectors(bio) > max_sectors) { 1464 req->cmd_flags |= REQ_NOMERGE; 1465 if (req == q->last_merge) 1466 q->last_merge = NULL; 1467 return 0; 1468 } 1469 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size; 1470 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) 1471 blk_recount_segments(q, bio); 1472 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID))) 1473 blk_recount_segments(q, req->bio); 1474 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) && 1475 !BIOVEC_VIRT_OVERSIZE(len)) { 1476 int mergeable = ll_new_mergeable(q, req, bio); 1477 1478 if (mergeable) { 1479 if (bio->bi_hw_segments == 1) 1480 bio->bi_hw_front_size = len; 1481 if (req->nr_hw_segments == 1) 1482 req->biotail->bi_hw_back_size = len; 1483 } 1484 return mergeable; 1485 } 1486 1487 return ll_new_hw_segment(q, req, bio); 1488} 1489 1490static int ll_merge_requests_fn(request_queue_t *q, struct request *req, 1491 struct request *next) 1492{ 1493 int total_phys_segments; 1494 int total_hw_segments; 1495 1496 /* 1497 * First check if the either of the requests are re-queued 1498 * requests. Can't merge them if they are. 1499 */ 1500 if (req->special || next->special) 1501 return 0; 1502 1503 /* 1504 * Will it become too large? 1505 */ 1506 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors) 1507 return 0; 1508 1509 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 1510 if (blk_phys_contig_segment(q, req->biotail, next->bio)) 1511 total_phys_segments--; 1512 1513 if (total_phys_segments > q->max_phys_segments) 1514 return 0; 1515 1516 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments; 1517 if (blk_hw_contig_segment(q, req->biotail, next->bio)) { 1518 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size; 1519 /* 1520 * propagate the combined length to the end of the requests 1521 */ 1522 if (req->nr_hw_segments == 1) 1523 req->bio->bi_hw_front_size = len; 1524 if (next->nr_hw_segments == 1) 1525 next->biotail->bi_hw_back_size = len; 1526 total_hw_segments--; 1527 } 1528 1529 if (total_hw_segments > q->max_hw_segments) 1530 return 0; 1531 1532 /* Merge is OK... */ 1533 req->nr_phys_segments = total_phys_segments; 1534 req->nr_hw_segments = total_hw_segments; 1535 return 1; 1536} 1537 1538/* 1539 * "plug" the device if there are no outstanding requests: this will 1540 * force the transfer to start only after we have put all the requests 1541 * on the list. 1542 * 1543 * This is called with interrupts off and no requests on the queue and 1544 * with the queue lock held. 1545 */ 1546void blk_plug_device(request_queue_t *q) 1547{ 1548 WARN_ON(!irqs_disabled()); 1549 1550 /* 1551 * don't plug a stopped queue, it must be paired with blk_start_queue() 1552 * which will restart the queueing 1553 */ 1554 if (blk_queue_stopped(q)) 1555 return; 1556 1557 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) { 1558 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay); 1559 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG); 1560 } 1561} 1562 1563EXPORT_SYMBOL(blk_plug_device); 1564 1565/* 1566 * remove the queue from the plugged list, if present. called with 1567 * queue lock held and interrupts disabled. 1568 */ 1569int blk_remove_plug(request_queue_t *q) 1570{ 1571 WARN_ON(!irqs_disabled()); 1572 1573 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) 1574 return 0; 1575 1576 del_timer(&q->unplug_timer); 1577 return 1; 1578} 1579 1580EXPORT_SYMBOL(blk_remove_plug); 1581 1582/* 1583 * remove the plug and let it rip.. 1584 */ 1585void __generic_unplug_device(request_queue_t *q) 1586{ 1587 if (unlikely(blk_queue_stopped(q))) 1588 return; 1589 1590 if (!blk_remove_plug(q)) 1591 return; 1592 1593 q->request_fn(q); 1594} 1595EXPORT_SYMBOL(__generic_unplug_device); 1596 1597/** 1598 * generic_unplug_device - fire a request queue 1599 * @q: The &request_queue_t in question 1600 * 1601 * Description: 1602 * Linux uses plugging to build bigger requests queues before letting 1603 * the device have at them. If a queue is plugged, the I/O scheduler 1604 * is still adding and merging requests on the queue. Once the queue 1605 * gets unplugged, the request_fn defined for the queue is invoked and 1606 * transfers started. 1607 **/ 1608void generic_unplug_device(request_queue_t *q) 1609{ 1610 spin_lock_irq(q->queue_lock); 1611 __generic_unplug_device(q); 1612 spin_unlock_irq(q->queue_lock); 1613} 1614EXPORT_SYMBOL(generic_unplug_device); 1615 1616static void blk_backing_dev_unplug(struct backing_dev_info *bdi, 1617 struct page *page) 1618{ 1619 request_queue_t *q = bdi->unplug_io_data; 1620 1621 /* 1622 * devices don't necessarily have an ->unplug_fn defined 1623 */ 1624 if (q->unplug_fn) { 1625 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL, 1626 q->rq.count[READ] + q->rq.count[WRITE]); 1627 1628 q->unplug_fn(q); 1629 } 1630} 1631 1632static void blk_unplug_work(struct work_struct *work) 1633{ 1634 request_queue_t *q = container_of(work, request_queue_t, unplug_work); 1635 1636 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL, 1637 q->rq.count[READ] + q->rq.count[WRITE]); 1638 1639 q->unplug_fn(q); 1640} 1641 1642static void blk_unplug_timeout(unsigned long data) 1643{ 1644 request_queue_t *q = (request_queue_t *)data; 1645 1646 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL, 1647 q->rq.count[READ] + q->rq.count[WRITE]); 1648 1649 kblockd_schedule_work(&q->unplug_work); 1650} 1651 1652/** 1653 * blk_start_queue - restart a previously stopped queue 1654 * @q: The &request_queue_t in question 1655 * 1656 * Description: 1657 * blk_start_queue() will clear the stop flag on the queue, and call 1658 * the request_fn for the queue if it was in a stopped state when 1659 * entered. Also see blk_stop_queue(). Queue lock must be held. 1660 **/ 1661void blk_start_queue(request_queue_t *q) 1662{ 1663 WARN_ON(!irqs_disabled()); 1664 1665 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags); 1666 1667 /* 1668 * one level of recursion is ok and is much faster than kicking 1669 * the unplug handling 1670 */ 1671 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) { 1672 q->request_fn(q); 1673 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags); 1674 } else { 1675 blk_plug_device(q); 1676 kblockd_schedule_work(&q->unplug_work); 1677 } 1678} 1679 1680EXPORT_SYMBOL(blk_start_queue); 1681 1682/** 1683 * blk_stop_queue - stop a queue 1684 * @q: The &request_queue_t in question 1685 * 1686 * Description: 1687 * The Linux block layer assumes that a block driver will consume all 1688 * entries on the request queue when the request_fn strategy is called. 1689 * Often this will not happen, because of hardware limitations (queue 1690 * depth settings). If a device driver gets a 'queue full' response, 1691 * or if it simply chooses not to queue more I/O at one point, it can 1692 * call this function to prevent the request_fn from being called until 1693 * the driver has signalled it's ready to go again. This happens by calling 1694 * blk_start_queue() to restart queue operations. Queue lock must be held. 1695 **/ 1696void blk_stop_queue(request_queue_t *q) 1697{ 1698 blk_remove_plug(q); 1699 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags); 1700} 1701EXPORT_SYMBOL(blk_stop_queue); 1702 1703/** 1704 * blk_sync_queue - cancel any pending callbacks on a queue 1705 * @q: the queue 1706 * 1707 * Description: 1708 * The block layer may perform asynchronous callback activity 1709 * on a queue, such as calling the unplug function after a timeout. 1710 * A block device may call blk_sync_queue to ensure that any 1711 * such activity is cancelled, thus allowing it to release resources 1712 * that the callbacks might use. The caller must already have made sure 1713 * that its ->make_request_fn will not re-add plugging prior to calling 1714 * this function. 1715 * 1716 */ 1717void blk_sync_queue(struct request_queue *q) 1718{ 1719 del_timer_sync(&q->unplug_timer); 1720} 1721EXPORT_SYMBOL(blk_sync_queue); 1722 1723/** 1724 * blk_run_queue - run a single device queue 1725 * @q: The queue to run 1726 */ 1727void blk_run_queue(struct request_queue *q) 1728{ 1729 unsigned long flags; 1730 1731 spin_lock_irqsave(q->queue_lock, flags); 1732 blk_remove_plug(q); 1733 1734 /* 1735 * Only recurse once to avoid overrunning the stack, let the unplug 1736 * handling reinvoke the handler shortly if we already got there. 1737 */ 1738 if (!elv_queue_empty(q)) { 1739 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) { 1740 q->request_fn(q); 1741 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags); 1742 } else { 1743 blk_plug_device(q); 1744 kblockd_schedule_work(&q->unplug_work); 1745 } 1746 } 1747 1748 spin_unlock_irqrestore(q->queue_lock, flags); 1749} 1750EXPORT_SYMBOL(blk_run_queue); 1751 1752/** 1753 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed 1754 * @kobj: the kobj belonging of the request queue to be released 1755 * 1756 * Description: 1757 * blk_cleanup_queue is the pair to blk_init_queue() or 1758 * blk_queue_make_request(). It should be called when a request queue is 1759 * being released; typically when a block device is being de-registered. 1760 * Currently, its primary task it to free all the &struct request 1761 * structures that were allocated to the queue and the queue itself. 1762 * 1763 * Caveat: 1764 * Hopefully the low level driver will have finished any 1765 * outstanding requests first... 1766 **/ 1767static void blk_release_queue(struct kobject *kobj) 1768{ 1769 request_queue_t *q = container_of(kobj, struct request_queue, kobj); 1770 struct request_list *rl = &q->rq; 1771 1772 blk_sync_queue(q); 1773 1774 if (rl->rq_pool) 1775 mempool_destroy(rl->rq_pool); 1776 1777 if (q->queue_tags) 1778 __blk_queue_free_tags(q); 1779 1780 blk_trace_shutdown(q); 1781 1782 kmem_cache_free(requestq_cachep, q); 1783} 1784 1785void blk_put_queue(request_queue_t *q) 1786{ 1787 kobject_put(&q->kobj); 1788} 1789EXPORT_SYMBOL(blk_put_queue); 1790 1791void blk_cleanup_queue(request_queue_t * q) 1792{ 1793 mutex_lock(&q->sysfs_lock); 1794 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags); 1795 mutex_unlock(&q->sysfs_lock); 1796 1797 if (q->elevator) 1798 elevator_exit(q->elevator); 1799 1800 blk_put_queue(q); 1801} 1802 1803EXPORT_SYMBOL(blk_cleanup_queue); 1804 1805static int blk_init_free_list(request_queue_t *q) 1806{ 1807 struct request_list *rl = &q->rq; 1808 1809 rl->count[READ] = rl->count[WRITE] = 0; 1810 rl->starved[READ] = rl->starved[WRITE] = 0; 1811 rl->elvpriv = 0; 1812 init_waitqueue_head(&rl->wait[READ]); 1813 init_waitqueue_head(&rl->wait[WRITE]); 1814 1815 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab, 1816 mempool_free_slab, request_cachep, q->node); 1817 1818 if (!rl->rq_pool) 1819 return -ENOMEM; 1820 1821 return 0; 1822} 1823 1824request_queue_t *blk_alloc_queue(gfp_t gfp_mask) 1825{ 1826 return blk_alloc_queue_node(gfp_mask, -1); 1827} 1828EXPORT_SYMBOL(blk_alloc_queue); 1829 1830static struct kobj_type queue_ktype; 1831 1832request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id) 1833{ 1834 request_queue_t *q; 1835 1836 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id); 1837 if (!q) 1838 return NULL; 1839 1840 memset(q, 0, sizeof(*q)); 1841 init_timer(&q->unplug_timer); 1842 1843 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue"); 1844 q->kobj.ktype = &queue_ktype; 1845 kobject_init(&q->kobj); 1846 1847 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug; 1848 q->backing_dev_info.unplug_io_data = q; 1849 1850 mutex_init(&q->sysfs_lock); 1851 1852 return q; 1853} 1854EXPORT_SYMBOL(blk_alloc_queue_node); 1855 1856/** 1857 * blk_init_queue - prepare a request queue for use with a block device 1858 * @rfn: The function to be called to process requests that have been 1859 * placed on the queue. 1860 * @lock: Request queue spin lock 1861 * 1862 * Description: 1863 * If a block device wishes to use the standard request handling procedures, 1864 * which sorts requests and coalesces adjacent requests, then it must 1865 * call blk_init_queue(). The function @rfn will be called when there 1866 * are requests on the queue that need to be processed. If the device 1867 * supports plugging, then @rfn may not be called immediately when requests 1868 * are available on the queue, but may be called at some time later instead. 1869 * Plugged queues are generally unplugged when a buffer belonging to one 1870 * of the requests on the queue is needed, or due to memory pressure. 1871 * 1872 * @rfn is not required, or even expected, to remove all requests off the 1873 * queue, but only as many as it can handle at a time. If it does leave 1874 * requests on the queue, it is responsible for arranging that the requests 1875 * get dealt with eventually. 1876 * 1877 * The queue spin lock must be held while manipulating the requests on the 1878 * request queue; this lock will be taken also from interrupt context, so irq 1879 * disabling is needed for it. 1880 * 1881 * Function returns a pointer to the initialized request queue, or NULL if 1882 * it didn't succeed. 1883 * 1884 * Note: 1885 * blk_init_queue() must be paired with a blk_cleanup_queue() call 1886 * when the block device is deactivated (such as at module unload). 1887 **/ 1888 1889request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock) 1890{ 1891 return blk_init_queue_node(rfn, lock, -1); 1892} 1893EXPORT_SYMBOL(blk_init_queue); 1894 1895request_queue_t * 1896blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) 1897{ 1898 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id); 1899 1900 if (!q) 1901 return NULL; 1902 1903 q->node = node_id; 1904 if (blk_init_free_list(q)) { 1905 kmem_cache_free(requestq_cachep, q); 1906 return NULL; 1907 } 1908 1909 /* 1910 * if caller didn't supply a lock, they get per-queue locking with 1911 * our embedded lock 1912 */ 1913 if (!lock) { 1914 spin_lock_init(&q->__queue_lock); 1915 lock = &q->__queue_lock; 1916 } 1917 1918 q->request_fn = rfn; 1919 q->prep_rq_fn = NULL; 1920 q->unplug_fn = generic_unplug_device; 1921 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER); 1922 q->queue_lock = lock; 1923 1924 blk_queue_segment_boundary(q, 0xffffffff); 1925 1926 blk_queue_make_request(q, __make_request); 1927 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE); 1928 1929 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS); 1930 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS); 1931 1932 q->sg_reserved_size = INT_MAX; 1933 1934 /* 1935 * all done 1936 */ 1937 if (!elevator_init(q, NULL)) { 1938 blk_queue_congestion_threshold(q); 1939 return q; 1940 } 1941 1942 blk_put_queue(q); 1943 return NULL; 1944} 1945EXPORT_SYMBOL(blk_init_queue_node); 1946 1947int blk_get_queue(request_queue_t *q) 1948{ 1949 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) { 1950 kobject_get(&q->kobj); 1951 return 0; 1952 } 1953 1954 return 1; 1955} 1956 1957EXPORT_SYMBOL(blk_get_queue); 1958 1959static inline void blk_free_request(request_queue_t *q, struct request *rq) 1960{ 1961 if (rq->cmd_flags & REQ_ELVPRIV) 1962 elv_put_request(q, rq); 1963 mempool_free(rq, q->rq.rq_pool); 1964} 1965 1966static struct request * 1967blk_alloc_request(request_queue_t *q, int rw, int priv, gfp_t gfp_mask) 1968{ 1969 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask); 1970 1971 if (!rq) 1972 return NULL; 1973 1974 /* 1975 * first three bits are identical in rq->cmd_flags and bio->bi_rw, 1976 * see bio.h and blkdev.h 1977 */ 1978 rq->cmd_flags = rw | REQ_ALLOCED; 1979 1980 if (priv) { 1981 if (unlikely(elv_set_request(q, rq, gfp_mask))) { 1982 mempool_free(rq, q->rq.rq_pool); 1983 return NULL; 1984 } 1985 rq->cmd_flags |= REQ_ELVPRIV; 1986 } 1987 1988 return rq; 1989} 1990 1991/* 1992 * ioc_batching returns true if the ioc is a valid batching request and 1993 * should be given priority access to a request. 1994 */ 1995static inline int ioc_batching(request_queue_t *q, struct io_context *ioc) 1996{ 1997 if (!ioc) 1998 return 0; 1999 2000 /* 2001 * Make sure the process is able to allocate at least 1 request 2002 * even if the batch times out, otherwise we could theoretically 2003 * lose wakeups. 2004 */ 2005 return ioc->nr_batch_requests == q->nr_batching || 2006 (ioc->nr_batch_requests > 0 2007 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME)); 2008} 2009 2010/* 2011 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This 2012 * will cause the process to be a "batcher" on all queues in the system. This 2013 * is the behaviour we want though - once it gets a wakeup it should be given 2014 * a nice run. 2015 */ 2016static void ioc_set_batching(request_queue_t *q, struct io_context *ioc) 2017{ 2018 if (!ioc || ioc_batching(q, ioc)) 2019 return; 2020 2021 ioc->nr_batch_requests = q->nr_batching; 2022 ioc->last_waited = jiffies; 2023} 2024 2025static void __freed_request(request_queue_t *q, int rw) 2026{ 2027 struct request_list *rl = &q->rq; 2028 2029 if (rl->count[rw] < queue_congestion_off_threshold(q)) 2030 blk_clear_queue_congested(q, rw); 2031 2032 if (rl->count[rw] + 1 <= q->nr_requests) { 2033 if (waitqueue_active(&rl->wait[rw])) 2034 wake_up(&rl->wait[rw]); 2035 2036 blk_clear_queue_full(q, rw); 2037 } 2038} 2039 2040/* 2041 * A request has just been released. Account for it, update the full and 2042 * congestion status, wake up any waiters. Called under q->queue_lock. 2043 */ 2044static void freed_request(request_queue_t *q, int rw, int priv) 2045{ 2046 struct request_list *rl = &q->rq; 2047 2048 rl->count[rw]--; 2049 if (priv) 2050 rl->elvpriv--; 2051 2052 __freed_request(q, rw); 2053 2054 if (unlikely(rl->starved[rw ^ 1])) 2055 __freed_request(q, rw ^ 1); 2056} 2057 2058#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist) 2059/* 2060 * Get a free request, queue_lock must be held. 2061 * Returns NULL on failure, with queue_lock held. 2062 * Returns !NULL on success, with queue_lock *not held*. 2063 */ 2064static struct request *get_request(request_queue_t *q, int rw_flags, 2065 struct bio *bio, gfp_t gfp_mask) 2066{ 2067 struct request *rq = NULL; 2068 struct request_list *rl = &q->rq; 2069 struct io_context *ioc = NULL; 2070 const int rw = rw_flags & 0x01; 2071 int may_queue, priv; 2072 2073 may_queue = elv_may_queue(q, rw_flags); 2074 if (may_queue == ELV_MQUEUE_NO) 2075 goto rq_starved; 2076 2077 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) { 2078 if (rl->count[rw]+1 >= q->nr_requests) { 2079 ioc = current_io_context(GFP_ATOMIC, q->node); 2080 /* 2081 * The queue will fill after this allocation, so set 2082 * it as full, and mark this process as "batching". 2083 * This process will be allowed to complete a batch of 2084 * requests, others will be blocked. 2085 */ 2086 if (!blk_queue_full(q, rw)) { 2087 ioc_set_batching(q, ioc); 2088 blk_set_queue_full(q, rw); 2089 } else { 2090 if (may_queue != ELV_MQUEUE_MUST 2091 && !ioc_batching(q, ioc)) { 2092 /* 2093 * The queue is full and the allocating 2094 * process is not a "batcher", and not 2095 * exempted by the IO scheduler 2096 */ 2097 goto out; 2098 } 2099 } 2100 } 2101 blk_set_queue_congested(q, rw); 2102 } 2103 2104 /* 2105 * Only allow batching queuers to allocate up to 50% over the defined 2106 * limit of requests, otherwise we could have thousands of requests 2107 * allocated with any setting of ->nr_requests 2108 */ 2109 if (rl->count[rw] >= (3 * q->nr_requests / 2)) 2110 goto out; 2111 2112 rl->count[rw]++; 2113 rl->starved[rw] = 0; 2114 2115 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); 2116 if (priv) 2117 rl->elvpriv++; 2118 2119 spin_unlock_irq(q->queue_lock); 2120 2121 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask); 2122 if (unlikely(!rq)) { 2123 /* 2124 * Allocation failed presumably due to memory. Undo anything 2125 * we might have messed up. 2126 * 2127 * Allocating task should really be put onto the front of the 2128 * wait queue, but this is pretty rare. 2129 */ 2130 spin_lock_irq(q->queue_lock); 2131 freed_request(q, rw, priv); 2132 2133 /* 2134 * in the very unlikely event that allocation failed and no 2135 * requests for this direction was pending, mark us starved 2136 * so that freeing of a request in the other direction will 2137 * notice us. another possible fix would be to split the 2138 * rq mempool into READ and WRITE 2139 */ 2140rq_starved: 2141 if (unlikely(rl->count[rw] == 0)) 2142 rl->starved[rw] = 1; 2143 2144 goto out; 2145 } 2146 2147 /* 2148 * ioc may be NULL here, and ioc_batching will be false. That's 2149 * OK, if the queue is under the request limit then requests need 2150 * not count toward the nr_batch_requests limit. There will always 2151 * be some limit enforced by BLK_BATCH_TIME. 2152 */ 2153 if (ioc_batching(q, ioc)) 2154 ioc->nr_batch_requests--; 2155 2156 rq_init(q, rq); 2157 2158 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ); 2159out: 2160 return rq; 2161} 2162 2163/* 2164 * No available requests for this queue, unplug the device and wait for some 2165 * requests to become available. 2166 * 2167 * Called with q->queue_lock held, and returns with it unlocked. 2168 */ 2169static struct request *get_request_wait(request_queue_t *q, int rw_flags, 2170 struct bio *bio) 2171{ 2172 const int rw = rw_flags & 0x01; 2173 struct request *rq; 2174 2175 rq = get_request(q, rw_flags, bio, GFP_NOIO); 2176 while (!rq) { 2177 DEFINE_WAIT(wait); 2178 struct request_list *rl = &q->rq; 2179 2180 prepare_to_wait_exclusive(&rl->wait[rw], &wait, 2181 TASK_UNINTERRUPTIBLE); 2182 2183 rq = get_request(q, rw_flags, bio, GFP_NOIO); 2184 2185 if (!rq) { 2186 struct io_context *ioc; 2187 2188 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ); 2189 2190 __generic_unplug_device(q); 2191 spin_unlock_irq(q->queue_lock); 2192 io_schedule(); 2193 2194 /* 2195 * After sleeping, we become a "batching" process and 2196 * will be able to allocate at least one request, and 2197 * up to a big batch of them for a small period time. 2198 * See ioc_batching, ioc_set_batching 2199 */ 2200 ioc = current_io_context(GFP_NOIO, q->node); 2201 ioc_set_batching(q, ioc); 2202 2203 spin_lock_irq(q->queue_lock); 2204 } 2205 finish_wait(&rl->wait[rw], &wait); 2206 } 2207 2208 return rq; 2209} 2210 2211struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask) 2212{ 2213 struct request *rq; 2214 2215 BUG_ON(rw != READ && rw != WRITE); 2216 2217 spin_lock_irq(q->queue_lock); 2218 if (gfp_mask & __GFP_WAIT) { 2219 rq = get_request_wait(q, rw, NULL); 2220 } else { 2221 rq = get_request(q, rw, NULL, gfp_mask); 2222 if (!rq) 2223 spin_unlock_irq(q->queue_lock); 2224 } 2225 /* q->queue_lock is unlocked at this point */ 2226 2227 return rq; 2228} 2229EXPORT_SYMBOL(blk_get_request); 2230 2231/** 2232 * blk_start_queueing - initiate dispatch of requests to device 2233 * @q: request queue to kick into gear 2234 * 2235 * This is basically a helper to remove the need to know whether a queue 2236 * is plugged or not if someone just wants to initiate dispatch of requests 2237 * for this queue. 2238 * 2239 * The queue lock must be held with interrupts disabled. 2240 */ 2241void blk_start_queueing(request_queue_t *q) 2242{ 2243 if (!blk_queue_plugged(q)) 2244 q->request_fn(q); 2245 else 2246 __generic_unplug_device(q); 2247} 2248EXPORT_SYMBOL(blk_start_queueing); 2249 2250/** 2251 * blk_requeue_request - put a request back on queue 2252 * @q: request queue where request should be inserted 2253 * @rq: request to be inserted 2254 * 2255 * Description: 2256 * Drivers often keep queueing requests until the hardware cannot accept 2257 * more, when that condition happens we need to put the request back 2258 * on the queue. Must be called with queue lock held. 2259 */ 2260void blk_requeue_request(request_queue_t *q, struct request *rq) 2261{ 2262 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE); 2263 2264 if (blk_rq_tagged(rq)) 2265 blk_queue_end_tag(q, rq); 2266 2267 elv_requeue_request(q, rq); 2268} 2269 2270EXPORT_SYMBOL(blk_requeue_request); 2271 2272/** 2273 * blk_insert_request - insert a special request in to a request queue 2274 * @q: request queue where request should be inserted 2275 * @rq: request to be inserted 2276 * @at_head: insert request at head or tail of queue 2277 * @data: private data 2278 * 2279 * Description: 2280 * Many block devices need to execute commands asynchronously, so they don't 2281 * block the whole kernel from preemption during request execution. This is 2282 * accomplished normally by inserting aritficial requests tagged as 2283 * REQ_SPECIAL in to the corresponding request queue, and letting them be 2284 * scheduled for actual execution by the request queue. 2285 * 2286 * We have the option of inserting the head or the tail of the queue. 2287 * Typically we use the tail for new ioctls and so forth. We use the head 2288 * of the queue for things like a QUEUE_FULL message from a device, or a 2289 * host that is unable to accept a particular command. 2290 */ 2291void blk_insert_request(request_queue_t *q, struct request *rq, 2292 int at_head, void *data) 2293{ 2294 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK; 2295 unsigned long flags; 2296 2297 /* 2298 * tell I/O scheduler that this isn't a regular read/write (ie it 2299 * must not attempt merges on this) and that it acts as a soft 2300 * barrier 2301 */ 2302 rq->cmd_type = REQ_TYPE_SPECIAL; 2303 rq->cmd_flags |= REQ_SOFTBARRIER; 2304 2305 rq->special = data; 2306 2307 spin_lock_irqsave(q->queue_lock, flags); 2308 2309 /* 2310 * If command is tagged, release the tag 2311 */ 2312 if (blk_rq_tagged(rq)) 2313 blk_queue_end_tag(q, rq); 2314 2315 drive_stat_acct(rq, rq->nr_sectors, 1); 2316 __elv_add_request(q, rq, where, 0); 2317 blk_start_queueing(q); 2318 spin_unlock_irqrestore(q->queue_lock, flags); 2319} 2320 2321EXPORT_SYMBOL(blk_insert_request); 2322 2323static int __blk_rq_unmap_user(struct bio *bio) 2324{ 2325 int ret = 0; 2326 2327 if (bio) { 2328 if (bio_flagged(bio, BIO_USER_MAPPED)) 2329 bio_unmap_user(bio); 2330 else 2331 ret = bio_uncopy_user(bio); 2332 } 2333 2334 return ret; 2335} 2336 2337static int __blk_rq_map_user(request_queue_t *q, struct request *rq, 2338 void __user *ubuf, unsigned int len) 2339{ 2340 unsigned long uaddr; 2341 struct bio *bio, *orig_bio; 2342 int reading, ret; 2343 2344 reading = rq_data_dir(rq) == READ; 2345 2346 /* 2347 * if alignment requirement is satisfied, map in user pages for 2348 * direct dma. else, set up kernel bounce buffers 2349 */ 2350 uaddr = (unsigned long) ubuf; 2351 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q))) 2352 bio = bio_map_user(q, NULL, uaddr, len, reading); 2353 else 2354 bio = bio_copy_user(q, uaddr, len, reading); 2355 2356 if (IS_ERR(bio)) 2357 return PTR_ERR(bio); 2358 2359 orig_bio = bio; 2360 blk_queue_bounce(q, &bio); 2361 2362 /* 2363 * We link the bounce buffer in and could have to traverse it 2364 * later so we have to get a ref to prevent it from being freed 2365 */ 2366 bio_get(bio); 2367 2368 if (!rq->bio) 2369 blk_rq_bio_prep(q, rq, bio); 2370 else if (!ll_back_merge_fn(q, rq, bio)) { 2371 ret = -EINVAL; 2372 goto unmap_bio; 2373 } else { 2374 rq->biotail->bi_next = bio; 2375 rq->biotail = bio; 2376 2377 rq->data_len += bio->bi_size; 2378 } 2379 2380 return bio->bi_size; 2381 2382unmap_bio: 2383 /* if it was boucned we must call the end io function */ 2384 bio_endio(bio, bio->bi_size, 0); 2385 __blk_rq_unmap_user(orig_bio); 2386 bio_put(bio); 2387 return ret; 2388} 2389 2390/** 2391 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage 2392 * @q: request queue where request should be inserted 2393 * @rq: request structure to fill 2394 * @ubuf: the user buffer 2395 * @len: length of user data 2396 * 2397 * Description: 2398 * Data will be mapped directly for zero copy io, if possible. Otherwise 2399 * a kernel bounce buffer is used. 2400 * 2401 * A matching blk_rq_unmap_user() must be issued at the end of io, while 2402 * still in process context. 2403 * 2404 * Note: The mapped bio may need to be bounced through blk_queue_bounce() 2405 * before being submitted to the device, as pages mapped may be out of 2406 * reach. It's the callers responsibility to make sure this happens. The 2407 * original bio must be passed back in to blk_rq_unmap_user() for proper 2408 * unmapping. 2409 */ 2410int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf, 2411 unsigned long len) 2412{ 2413 unsigned long bytes_read = 0; 2414 struct bio *bio = NULL; 2415 int ret; 2416 2417 if (len > (q->max_hw_sectors << 9)) 2418 return -EINVAL; 2419 if (!len || !ubuf) 2420 return -EINVAL; 2421 2422 while (bytes_read != len) { 2423 unsigned long map_len, end, start; 2424 2425 map_len = min_t(unsigned long, len - bytes_read, BIO_MAX_SIZE); 2426 end = ((unsigned long)ubuf + map_len + PAGE_SIZE - 1) 2427 >> PAGE_SHIFT; 2428 start = (unsigned long)ubuf >> PAGE_SHIFT; 2429 2430 /* 2431 * A bad offset could cause us to require BIO_MAX_PAGES + 1 2432 * pages. If this happens we just lower the requested 2433 * mapping len by a page so that we can fit 2434 */ 2435 if (end - start > BIO_MAX_PAGES) 2436 map_len -= PAGE_SIZE; 2437 2438 ret = __blk_rq_map_user(q, rq, ubuf, map_len); 2439 if (ret < 0) 2440 goto unmap_rq; 2441 if (!bio) 2442 bio = rq->bio; 2443 bytes_read += ret; 2444 ubuf += ret; 2445 } 2446 2447 rq->buffer = rq->data = NULL; 2448 return 0; 2449unmap_rq: 2450 blk_rq_unmap_user(bio); 2451 return ret; 2452} 2453 2454EXPORT_SYMBOL(blk_rq_map_user); 2455 2456/** 2457 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage 2458 * @q: request queue where request should be inserted 2459 * @rq: request to map data to 2460 * @iov: pointer to the iovec 2461 * @iov_count: number of elements in the iovec 2462 * @len: I/O byte count 2463 * 2464 * Description: 2465 * Data will be mapped directly for zero copy io, if possible. Otherwise 2466 * a kernel bounce buffer is used. 2467 * 2468 * A matching blk_rq_unmap_user() must be issued at the end of io, while 2469 * still in process context. 2470 * 2471 * Note: The mapped bio may need to be bounced through blk_queue_bounce() 2472 * before being submitted to the device, as pages mapped may be out of 2473 * reach. It's the callers responsibility to make sure this happens. The 2474 * original bio must be passed back in to blk_rq_unmap_user() for proper 2475 * unmapping. 2476 */ 2477int blk_rq_map_user_iov(request_queue_t *q, struct request *rq, 2478 struct sg_iovec *iov, int iov_count, unsigned int len) 2479{ 2480 struct bio *bio; 2481 2482 if (!iov || iov_count <= 0) 2483 return -EINVAL; 2484 2485 /* we don't allow misaligned data like bio_map_user() does. If the 2486 * user is using sg, they're expected to know the alignment constraints 2487 * and respect them accordingly */ 2488 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ); 2489 if (IS_ERR(bio)) 2490 return PTR_ERR(bio); 2491 2492 if (bio->bi_size != len) { 2493 bio_endio(bio, bio->bi_size, 0); 2494 bio_unmap_user(bio); 2495 return -EINVAL; 2496 } 2497 2498 bio_get(bio); 2499 blk_rq_bio_prep(q, rq, bio); 2500 rq->buffer = rq->data = NULL; 2501 return 0; 2502} 2503 2504EXPORT_SYMBOL(blk_rq_map_user_iov); 2505 2506/** 2507 * blk_rq_unmap_user - unmap a request with user data 2508 * @bio: start of bio list 2509 * 2510 * Description: 2511 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must 2512 * supply the original rq->bio from the blk_rq_map_user() return, since 2513 * the io completion may have changed rq->bio. 2514 */ 2515int blk_rq_unmap_user(struct bio *bio) 2516{ 2517 struct bio *mapped_bio; 2518 int ret = 0, ret2; 2519 2520 while (bio) { 2521 mapped_bio = bio; 2522 if (unlikely(bio_flagged(bio, BIO_BOUNCED))) 2523 mapped_bio = bio->bi_private; 2524 2525 ret2 = __blk_rq_unmap_user(mapped_bio); 2526 if (ret2 && !ret) 2527 ret = ret2; 2528 2529 mapped_bio = bio; 2530 bio = bio->bi_next; 2531 bio_put(mapped_bio); 2532 } 2533 2534 return ret; 2535} 2536 2537EXPORT_SYMBOL(blk_rq_unmap_user); 2538 2539/** 2540 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage 2541 * @q: request queue where request should be inserted 2542 * @rq: request to fill 2543 * @kbuf: the kernel buffer 2544 * @len: length of user data 2545 * @gfp_mask: memory allocation flags 2546 */ 2547int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf, 2548 unsigned int len, gfp_t gfp_mask) 2549{ 2550 struct bio *bio; 2551 2552 if (len > (q->max_hw_sectors << 9)) 2553 return -EINVAL; 2554 if (!len || !kbuf) 2555 return -EINVAL; 2556 2557 bio = bio_map_kern(q, kbuf, len, gfp_mask); 2558 if (IS_ERR(bio)) 2559 return PTR_ERR(bio); 2560 2561 if (rq_data_dir(rq) == WRITE) 2562 bio->bi_rw |= (1 << BIO_RW); 2563 2564 blk_rq_bio_prep(q, rq, bio); 2565 blk_queue_bounce(q, &rq->bio); 2566 rq->buffer = rq->data = NULL; 2567 return 0; 2568} 2569 2570EXPORT_SYMBOL(blk_rq_map_kern); 2571 2572/** 2573 * blk_execute_rq_nowait - insert a request into queue for execution 2574 * @q: queue to insert the request in 2575 * @bd_disk: matching gendisk 2576 * @rq: request to insert 2577 * @at_head: insert request at head or tail of queue 2578 * @done: I/O completion handler 2579 * 2580 * Description: 2581 * Insert a fully prepared request at the back of the io scheduler queue 2582 * for execution. Don't wait for completion. 2583 */ 2584void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk, 2585 struct request *rq, int at_head, 2586 rq_end_io_fn *done) 2587{ 2588 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK; 2589 2590 rq->rq_disk = bd_disk; 2591 rq->cmd_flags |= REQ_NOMERGE; 2592 rq->end_io = done; 2593 WARN_ON(irqs_disabled()); 2594 spin_lock_irq(q->queue_lock); 2595 __elv_add_request(q, rq, where, 1); 2596 __generic_unplug_device(q); 2597 spin_unlock_irq(q->queue_lock); 2598} 2599EXPORT_SYMBOL_GPL(blk_execute_rq_nowait); 2600 2601/** 2602 * blk_execute_rq - insert a request into queue for execution 2603 * @q: queue to insert the request in 2604 * @bd_disk: matching gendisk 2605 * @rq: request to insert 2606 * @at_head: insert request at head or tail of queue 2607 * 2608 * Description: 2609 * Insert a fully prepared request at the back of the io scheduler queue 2610 * for execution and wait for completion. 2611 */ 2612int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk, 2613 struct request *rq, int at_head) 2614{ 2615 DECLARE_COMPLETION_ONSTACK(wait); 2616 char sense[SCSI_SENSE_BUFFERSIZE]; 2617 int err = 0; 2618 2619 /* 2620 * we need an extra reference to the request, so we can look at 2621 * it after io completion 2622 */ 2623 rq->ref_count++; 2624 2625 if (!rq->sense) { 2626 memset(sense, 0, sizeof(sense)); 2627 rq->sense = sense; 2628 rq->sense_len = 0; 2629 } 2630 2631 rq->end_io_data = &wait; 2632 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq); 2633 wait_for_completion(&wait); 2634 2635 if (rq->errors) 2636 err = -EIO; 2637 2638 return err; 2639} 2640 2641EXPORT_SYMBOL(blk_execute_rq); 2642 2643/** 2644 * blkdev_issue_flush - queue a flush 2645 * @bdev: blockdev to issue flush for 2646 * @error_sector: error sector 2647 * 2648 * Description: 2649 * Issue a flush for the block device in question. Caller can supply 2650 * room for storing the error offset in case of a flush error, if they 2651 * wish to. Caller must run wait_for_completion() on its own. 2652 */ 2653int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector) 2654{ 2655 request_queue_t *q; 2656 2657 if (bdev->bd_disk == NULL) 2658 return -ENXIO; 2659 2660 q = bdev_get_queue(bdev); 2661 if (!q) 2662 return -ENXIO; 2663 if (!q->issue_flush_fn) 2664 return -EOPNOTSUPP; 2665 2666 return q->issue_flush_fn(q, bdev->bd_disk, error_sector); 2667} 2668 2669EXPORT_SYMBOL(blkdev_issue_flush); 2670 2671static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io) 2672{ 2673 int rw = rq_data_dir(rq); 2674 2675 if (!blk_fs_request(rq) || !rq->rq_disk) 2676 return; 2677 2678 if (!new_io) { 2679 __disk_stat_inc(rq->rq_disk, merges[rw]); 2680 } else { 2681 disk_round_stats(rq->rq_disk); 2682 rq->rq_disk->in_flight++; 2683 } 2684} 2685 2686/* 2687 * add-request adds a request to the linked list. 2688 * queue lock is held and interrupts disabled, as we muck with the 2689 * request queue list. 2690 */ 2691static inline void add_request(request_queue_t * q, struct request * req) 2692{ 2693 drive_stat_acct(req, req->nr_sectors, 1); 2694 2695 /* 2696 * elevator indicated where it wants this request to be 2697 * inserted at elevator_merge time 2698 */ 2699 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0); 2700} 2701 2702/* 2703 * disk_round_stats() - Round off the performance stats on a struct 2704 * disk_stats. 2705 * 2706 * The average IO queue length and utilisation statistics are maintained 2707 * by observing the current state of the queue length and the amount of 2708 * time it has been in this state for. 2709 * 2710 * Normally, that accounting is done on IO completion, but that can result 2711 * in more than a second's worth of IO being accounted for within any one 2712 * second, leading to >100% utilisation. To deal with that, we call this 2713 * function to do a round-off before returning the results when reading 2714 * /proc/diskstats. This accounts immediately for all queue usage up to 2715 * the current jiffies and restarts the counters again. 2716 */ 2717void disk_round_stats(struct gendisk *disk) 2718{ 2719 unsigned long now = jiffies; 2720 2721 if (now == disk->stamp) 2722 return; 2723 2724 if (disk->in_flight) { 2725 __disk_stat_add(disk, time_in_queue, 2726 disk->in_flight * (now - disk->stamp)); 2727 __disk_stat_add(disk, io_ticks, (now - disk->stamp)); 2728 } 2729 disk->stamp = now; 2730} 2731 2732EXPORT_SYMBOL_GPL(disk_round_stats); 2733 2734/* 2735 * queue lock must be held 2736 */ 2737void __blk_put_request(request_queue_t *q, struct request *req) 2738{ 2739 if (unlikely(!q)) 2740 return; 2741 if (unlikely(--req->ref_count)) 2742 return; 2743 2744 elv_completed_request(q, req); 2745 2746 /* 2747 * Request may not have originated from ll_rw_blk. if not, 2748 * it didn't come out of our reserved rq pools 2749 */ 2750 if (req->cmd_flags & REQ_ALLOCED) { 2751 int rw = rq_data_dir(req); 2752 int priv = req->cmd_flags & REQ_ELVPRIV; 2753 2754 BUG_ON(!list_empty(&req->queuelist)); 2755 BUG_ON(!hlist_unhashed(&req->hash)); 2756 2757 blk_free_request(q, req); 2758 freed_request(q, rw, priv); 2759 } 2760} 2761 2762EXPORT_SYMBOL_GPL(__blk_put_request); 2763 2764void blk_put_request(struct request *req) 2765{ 2766 unsigned long flags; 2767 request_queue_t *q = req->q; 2768 2769 /* 2770 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the 2771 * following if (q) test. 2772 */ 2773 if (q) { 2774 spin_lock_irqsave(q->queue_lock, flags); 2775 __blk_put_request(q, req); 2776 spin_unlock_irqrestore(q->queue_lock, flags); 2777 } 2778} 2779 2780EXPORT_SYMBOL(blk_put_request); 2781 2782/** 2783 * blk_end_sync_rq - executes a completion event on a request 2784 * @rq: request to complete 2785 * @error: end io status of the request 2786 */ 2787void blk_end_sync_rq(struct request *rq, int error) 2788{ 2789 struct completion *waiting = rq->end_io_data; 2790 2791 rq->end_io_data = NULL; 2792 __blk_put_request(rq->q, rq); 2793 2794 /* 2795 * complete last, if this is a stack request the process (and thus 2796 * the rq pointer) could be invalid right after this complete() 2797 */ 2798 complete(waiting); 2799} 2800EXPORT_SYMBOL(blk_end_sync_rq); 2801 2802/* 2803 * Has to be called with the request spinlock acquired 2804 */ 2805static int attempt_merge(request_queue_t *q, struct request *req, 2806 struct request *next) 2807{ 2808 if (!rq_mergeable(req) || !rq_mergeable(next)) 2809 return 0; 2810 2811 /* 2812 * not contiguous 2813 */ 2814 if (req->sector + req->nr_sectors != next->sector) 2815 return 0; 2816 2817 if (rq_data_dir(req) != rq_data_dir(next) 2818 || req->rq_disk != next->rq_disk 2819 || next->special) 2820 return 0; 2821 2822 /* 2823 * If we are allowed to merge, then append bio list 2824 * from next to rq and release next. merge_requests_fn 2825 * will have updated segment counts, update sector 2826 * counts here. 2827 */ 2828 if (!ll_merge_requests_fn(q, req, next)) 2829 return 0; 2830 2831 /* 2832 * At this point we have either done a back merge 2833 * or front merge. We need the smaller start_time of 2834 * the merged requests to be the current request 2835 * for accounting purposes. 2836 */ 2837 if (time_after(req->start_time, next->start_time)) 2838 req->start_time = next->start_time; 2839 2840 req->biotail->bi_next = next->bio; 2841 req->biotail = next->biotail; 2842 2843 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors; 2844 2845 elv_merge_requests(q, req, next); 2846 2847 if (req->rq_disk) { 2848 disk_round_stats(req->rq_disk); 2849 req->rq_disk->in_flight--; 2850 } 2851 2852 req->ioprio = ioprio_best(req->ioprio, next->ioprio); 2853 2854 __blk_put_request(q, next); 2855 return 1; 2856} 2857 2858static inline int attempt_back_merge(request_queue_t *q, struct request *rq) 2859{ 2860 struct request *next = elv_latter_request(q, rq); 2861 2862 if (next) 2863 return attempt_merge(q, rq, next); 2864 2865 return 0; 2866} 2867 2868static inline int attempt_front_merge(request_queue_t *q, struct request *rq) 2869{ 2870 struct request *prev = elv_former_request(q, rq); 2871 2872 if (prev) 2873 return attempt_merge(q, prev, rq); 2874 2875 return 0; 2876} 2877 2878static void init_request_from_bio(struct request *req, struct bio *bio) 2879{ 2880 req->cmd_type = REQ_TYPE_FS; 2881 2882 /* 2883 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST) 2884 */ 2885 if (bio_rw_ahead(bio) || bio_failfast(bio)) 2886 req->cmd_flags |= REQ_FAILFAST; 2887 2888 /* 2889 * REQ_BARRIER implies no merging, but lets make it explicit 2890 */ 2891 if (unlikely(bio_barrier(bio))) 2892 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE); 2893 2894 if (bio_sync(bio)) 2895 req->cmd_flags |= REQ_RW_SYNC; 2896 if (bio_rw_meta(bio)) 2897 req->cmd_flags |= REQ_RW_META; 2898 2899 req->errors = 0; 2900 req->hard_sector = req->sector = bio->bi_sector; 2901 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio); 2902 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio); 2903 req->nr_phys_segments = bio_phys_segments(req->q, bio); 2904 req->nr_hw_segments = bio_hw_segments(req->q, bio); 2905 req->buffer = bio_data(bio); /* see ->buffer comment above */ 2906 req->bio = req->biotail = bio; 2907 req->ioprio = bio_prio(bio); 2908 req->rq_disk = bio->bi_bdev->bd_disk; 2909 req->start_time = jiffies; 2910} 2911 2912static int __make_request(request_queue_t *q, struct bio *bio) 2913{ 2914 struct request *req; 2915 int el_ret, nr_sectors, barrier, err; 2916 const unsigned short prio = bio_prio(bio); 2917 const int sync = bio_sync(bio); 2918 int rw_flags; 2919 2920 nr_sectors = bio_sectors(bio); 2921 2922 /* 2923 * low level driver can indicate that it wants pages above a 2924 * certain limit bounced to low memory (ie for highmem, or even 2925 * ISA dma in theory) 2926 */ 2927 blk_queue_bounce(q, &bio); 2928 2929 barrier = bio_barrier(bio); 2930 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) { 2931 err = -EOPNOTSUPP; 2932 goto end_io; 2933 } 2934 2935 spin_lock_irq(q->queue_lock); 2936 2937 if (unlikely(barrier) || elv_queue_empty(q)) 2938 goto get_rq; 2939 2940 el_ret = elv_merge(q, &req, bio); 2941 switch (el_ret) { 2942 case ELEVATOR_BACK_MERGE: 2943 BUG_ON(!rq_mergeable(req)); 2944 2945 if (!ll_back_merge_fn(q, req, bio)) 2946 break; 2947 2948 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE); 2949 2950 req->biotail->bi_next = bio; 2951 req->biotail = bio; 2952 req->nr_sectors = req->hard_nr_sectors += nr_sectors; 2953 req->ioprio = ioprio_best(req->ioprio, prio); 2954 drive_stat_acct(req, nr_sectors, 0); 2955 if (!attempt_back_merge(q, req)) 2956 elv_merged_request(q, req, el_ret); 2957 goto out; 2958 2959 case ELEVATOR_FRONT_MERGE: 2960 BUG_ON(!rq_mergeable(req)); 2961 2962 if (!ll_front_merge_fn(q, req, bio)) 2963 break; 2964 2965 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE); 2966 2967 bio->bi_next = req->bio; 2968 req->bio = bio; 2969 2970 /* 2971 * may not be valid. if the low level driver said 2972 * it didn't need a bounce buffer then it better 2973 * not touch req->buffer either... 2974 */ 2975 req->buffer = bio_data(bio); 2976 req->current_nr_sectors = bio_cur_sectors(bio); 2977 req->hard_cur_sectors = req->current_nr_sectors; 2978 req->sector = req->hard_sector = bio->bi_sector; 2979 req->nr_sectors = req->hard_nr_sectors += nr_sectors; 2980 req->ioprio = ioprio_best(req->ioprio, prio); 2981 drive_stat_acct(req, nr_sectors, 0); 2982 if (!attempt_front_merge(q, req)) 2983 elv_merged_request(q, req, el_ret); 2984 goto out; 2985 2986 /* ELV_NO_MERGE: elevator says don't/can't merge. */ 2987 default: 2988 ; 2989 } 2990 2991get_rq: 2992 /* 2993 * This sync check and mask will be re-done in init_request_from_bio(), 2994 * but we need to set it earlier to expose the sync flag to the 2995 * rq allocator and io schedulers. 2996 */ 2997 rw_flags = bio_data_dir(bio); 2998 if (sync) 2999 rw_flags |= REQ_RW_SYNC; 3000 3001 /* 3002 * Grab a free request. This is might sleep but can not fail. 3003 * Returns with the queue unlocked. 3004 */ 3005 req = get_request_wait(q, rw_flags, bio); 3006 3007 /* 3008 * After dropping the lock and possibly sleeping here, our request 3009 * may now be mergeable after it had proven unmergeable (above). 3010 * We don't worry about that case for efficiency. It won't happen 3011 * often, and the elevators are able to handle it. 3012 */ 3013 init_request_from_bio(req, bio); 3014 3015 spin_lock_irq(q->queue_lock); 3016 if (elv_queue_empty(q)) 3017 blk_plug_device(q); 3018 add_request(q, req); 3019out: 3020 if (sync) 3021 __generic_unplug_device(q); 3022 3023 spin_unlock_irq(q->queue_lock); 3024 return 0; 3025 3026end_io: 3027 bio_endio(bio, nr_sectors << 9, err); 3028 return 0; 3029} 3030 3031/* 3032 * If bio->bi_dev is a partition, remap the location 3033 */ 3034static inline void blk_partition_remap(struct bio *bio) 3035{ 3036 struct block_device *bdev = bio->bi_bdev; 3037 3038 if (bdev != bdev->bd_contains) { 3039 struct hd_struct *p = bdev->bd_part; 3040 const int rw = bio_data_dir(bio); 3041 3042 p->sectors[rw] += bio_sectors(bio); 3043 p->ios[rw]++; 3044 3045 bio->bi_sector += p->start_sect; 3046 bio->bi_bdev = bdev->bd_contains; 3047 } 3048} 3049 3050static void handle_bad_sector(struct bio *bio) 3051{ 3052 char b[BDEVNAME_SIZE]; 3053 3054 printk(KERN_INFO "attempt to access beyond end of device\n"); 3055 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n", 3056 bdevname(bio->bi_bdev, b), 3057 bio->bi_rw, 3058 (unsigned long long)bio->bi_sector + bio_sectors(bio), 3059 (long long)(bio->bi_bdev->bd_inode->i_size >> 9)); 3060 3061 set_bit(BIO_EOF, &bio->bi_flags); 3062} 3063 3064#ifdef CONFIG_FAIL_MAKE_REQUEST 3065 3066static DECLARE_FAULT_ATTR(fail_make_request); 3067 3068static int __init setup_fail_make_request(char *str) 3069{ 3070 return setup_fault_attr(&fail_make_request, str); 3071} 3072__setup("fail_make_request=", setup_fail_make_request); 3073 3074static int should_fail_request(struct bio *bio) 3075{ 3076 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) || 3077 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail)) 3078 return should_fail(&fail_make_request, bio->bi_size); 3079 3080 return 0; 3081} 3082 3083static int __init fail_make_request_debugfs(void) 3084{ 3085 return init_fault_attr_dentries(&fail_make_request, 3086 "fail_make_request"); 3087} 3088 3089late_initcall(fail_make_request_debugfs); 3090 3091#else /* CONFIG_FAIL_MAKE_REQUEST */ 3092 3093static inline int should_fail_request(struct bio *bio) 3094{ 3095 return 0; 3096} 3097 3098#endif /* CONFIG_FAIL_MAKE_REQUEST */ 3099 3100/** 3101 * generic_make_request: hand a buffer to its device driver for I/O 3102 * @bio: The bio describing the location in memory and on the device. 3103 * 3104 * generic_make_request() is used to make I/O requests of block 3105 * devices. It is passed a &struct bio, which describes the I/O that needs 3106 * to be done. 3107 * 3108 * generic_make_request() does not return any status. The 3109 * success/failure status of the request, along with notification of 3110 * completion, is delivered asynchronously through the bio->bi_end_io 3111 * function described (one day) else where. 3112 * 3113 * The caller of generic_make_request must make sure that bi_io_vec 3114 * are set to describe the memory buffer, and that bi_dev and bi_sector are 3115 * set to describe the device address, and the 3116 * bi_end_io and optionally bi_private are set to describe how 3117 * completion notification should be signaled. 3118 * 3119 * generic_make_request and the drivers it calls may use bi_next if this 3120 * bio happens to be merged with someone else, and may change bi_dev and 3121 * bi_sector for remaps as it sees fit. So the values of these fields 3122 * should NOT be depended on after the call to generic_make_request. 3123 */ 3124static inline void __generic_make_request(struct bio *bio) 3125{ 3126 request_queue_t *q; 3127 sector_t maxsector; 3128 sector_t old_sector; 3129 int ret, nr_sectors = bio_sectors(bio); 3130 dev_t old_dev; 3131 3132 might_sleep(); 3133 /* Test device or partition size, when known. */ 3134 maxsector = bio->bi_bdev->bd_inode->i_size >> 9; 3135 if (maxsector) { 3136 sector_t sector = bio->bi_sector; 3137 3138 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) { 3139 /* 3140 * This may well happen - the kernel calls bread() 3141 * without checking the size of the device, e.g., when 3142 * mounting a device. 3143 */ 3144 handle_bad_sector(bio); 3145 goto end_io; 3146 } 3147 } 3148 3149 /* 3150 * Resolve the mapping until finished. (drivers are 3151 * still free to implement/resolve their own stacking 3152 * by explicitly returning 0) 3153 * 3154 * NOTE: we don't repeat the blk_size check for each new device. 3155 * Stacking drivers are expected to know what they are doing. 3156 */ 3157 old_sector = -1; 3158 old_dev = 0; 3159 do { 3160 char b[BDEVNAME_SIZE]; 3161 3162 q = bdev_get_queue(bio->bi_bdev); 3163 if (!q) { 3164 printk(KERN_ERR 3165 "generic_make_request: Trying to access " 3166 "nonexistent block-device %s (%Lu)\n", 3167 bdevname(bio->bi_bdev, b), 3168 (long long) bio->bi_sector); 3169end_io: 3170 bio_endio(bio, bio->bi_size, -EIO); 3171 break; 3172 } 3173 3174 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) { 3175 printk("bio too big device %s (%u > %u)\n", 3176 bdevname(bio->bi_bdev, b), 3177 bio_sectors(bio), 3178 q->max_hw_sectors); 3179 goto end_io; 3180 } 3181 3182 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) 3183 goto end_io; 3184 3185 if (should_fail_request(bio)) 3186 goto end_io; 3187 3188 /* 3189 * If this device has partitions, remap block n 3190 * of partition p to block n+start(p) of the disk. 3191 */ 3192 blk_partition_remap(bio); 3193 3194 if (old_sector != -1) 3195 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector, 3196 old_sector); 3197 3198 blk_add_trace_bio(q, bio, BLK_TA_QUEUE); 3199 3200 old_sector = bio->bi_sector; 3201 old_dev = bio->bi_bdev->bd_dev; 3202 3203 maxsector = bio->bi_bdev->bd_inode->i_size >> 9; 3204 if (maxsector) { 3205 sector_t sector = bio->bi_sector; 3206 3207 if (maxsector < nr_sectors || 3208 maxsector - nr_sectors < sector) { 3209 /* 3210 * This may well happen - partitions are not 3211 * checked to make sure they are within the size 3212 * of the whole device. 3213 */ 3214 handle_bad_sector(bio); 3215 goto end_io; 3216 } 3217 } 3218 3219 ret = q->make_request_fn(q, bio); 3220 } while (ret); 3221} 3222 3223/* 3224 * We only want one ->make_request_fn to be active at a time, 3225 * else stack usage with stacked devices could be a problem. 3226 * So use current->bio_{list,tail} to keep a list of requests 3227 * submited by a make_request_fn function. 3228 * current->bio_tail is also used as a flag to say if 3229 * generic_make_request is currently active in this task or not. 3230 * If it is NULL, then no make_request is active. If it is non-NULL, 3231 * then a make_request is active, and new requests should be added 3232 * at the tail 3233 */ 3234void generic_make_request(struct bio *bio) 3235{ 3236 if (current->bio_tail) { 3237 /* make_request is active */ 3238 *(current->bio_tail) = bio; 3239 bio->bi_next = NULL; 3240 current->bio_tail = &bio->bi_next; 3241 return; 3242 } 3243 /* following loop may be a bit non-obvious, and so deserves some 3244 * explanation. 3245 * Before entering the loop, bio->bi_next is NULL (as all callers 3246 * ensure that) so we have a list with a single bio. 3247 * We pretend that we have just taken it off a longer list, so 3248 * we assign bio_list to the next (which is NULL) and bio_tail 3249 * to &bio_list, thus initialising the bio_list of new bios to be 3250 * added. __generic_make_request may indeed add some more bios 3251 * through a recursive call to generic_make_request. If it 3252 * did, we find a non-NULL value in bio_list and re-enter the loop 3253 * from the top. In this case we really did just take the bio 3254 * of the top of the list (no pretending) and so fixup bio_list and 3255 * bio_tail or bi_next, and call into __generic_make_request again. 3256 * 3257 * The loop was structured like this to make only one call to 3258 * __generic_make_request (which is important as it is large and 3259 * inlined) and to keep the structure simple. 3260 */ 3261 BUG_ON(bio->bi_next); 3262 do { 3263 current->bio_list = bio->bi_next; 3264 if (bio->bi_next == NULL) 3265 current->bio_tail = ¤t->bio_list; 3266 else 3267 bio->bi_next = NULL; 3268 __generic_make_request(bio); 3269 bio = current->bio_list; 3270 } while (bio); 3271 current->bio_tail = NULL; /* deactivate */ 3272} 3273 3274EXPORT_SYMBOL(generic_make_request); 3275 3276/** 3277 * submit_bio: submit a bio to the block device layer for I/O 3278 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead) 3279 * @bio: The &struct bio which describes the I/O 3280 * 3281 * submit_bio() is very similar in purpose to generic_make_request(), and 3282 * uses that function to do most of the work. Both are fairly rough 3283 * interfaces, @bio must be presetup and ready for I/O. 3284 * 3285 */ 3286void submit_bio(int rw, struct bio *bio) 3287{ 3288 int count = bio_sectors(bio); 3289 3290 BIO_BUG_ON(!bio->bi_size); 3291 BIO_BUG_ON(!bio->bi_io_vec); 3292 bio->bi_rw |= rw; 3293 if (rw & WRITE) { 3294 count_vm_events(PGPGOUT, count); 3295 } else { 3296 task_io_account_read(bio->bi_size); 3297 count_vm_events(PGPGIN, count); 3298 } 3299 3300 if (unlikely(block_dump)) { 3301 char b[BDEVNAME_SIZE]; 3302 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n", 3303 current->comm, current->pid, 3304 (rw & WRITE) ? "WRITE" : "READ", 3305 (unsigned long long)bio->bi_sector, 3306 bdevname(bio->bi_bdev,b)); 3307 } 3308 3309 generic_make_request(bio); 3310} 3311 3312EXPORT_SYMBOL(submit_bio); 3313 3314static void blk_recalc_rq_segments(struct request *rq) 3315{ 3316 struct bio *bio, *prevbio = NULL; 3317 int nr_phys_segs, nr_hw_segs; 3318 unsigned int phys_size, hw_size; 3319 request_queue_t *q = rq->q; 3320 3321 if (!rq->bio) 3322 return; 3323 3324 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0; 3325 rq_for_each_bio(bio, rq) { 3326 /* Force bio hw/phys segs to be recalculated. */ 3327 bio->bi_flags &= ~(1 << BIO_SEG_VALID); 3328 3329 nr_phys_segs += bio_phys_segments(q, bio); 3330 nr_hw_segs += bio_hw_segments(q, bio); 3331 if (prevbio) { 3332 int pseg = phys_size + prevbio->bi_size + bio->bi_size; 3333 int hseg = hw_size + prevbio->bi_size + bio->bi_size; 3334 3335 if (blk_phys_contig_segment(q, prevbio, bio) && 3336 pseg <= q->max_segment_size) { 3337 nr_phys_segs--; 3338 phys_size += prevbio->bi_size + bio->bi_size; 3339 } else 3340 phys_size = 0; 3341 3342 if (blk_hw_contig_segment(q, prevbio, bio) && 3343 hseg <= q->max_segment_size) { 3344 nr_hw_segs--; 3345 hw_size += prevbio->bi_size + bio->bi_size; 3346 } else 3347 hw_size = 0; 3348 } 3349 prevbio = bio; 3350 } 3351 3352 rq->nr_phys_segments = nr_phys_segs; 3353 rq->nr_hw_segments = nr_hw_segs; 3354} 3355 3356static void blk_recalc_rq_sectors(struct request *rq, int nsect) 3357{ 3358 if (blk_fs_request(rq)) { 3359 rq->hard_sector += nsect; 3360 rq->hard_nr_sectors -= nsect; 3361 3362 /* 3363 * Move the I/O submission pointers ahead if required. 3364 */ 3365 if ((rq->nr_sectors >= rq->hard_nr_sectors) && 3366 (rq->sector <= rq->hard_sector)) { 3367 rq->sector = rq->hard_sector; 3368 rq->nr_sectors = rq->hard_nr_sectors; 3369 rq->hard_cur_sectors = bio_cur_sectors(rq->bio); 3370 rq->current_nr_sectors = rq->hard_cur_sectors; 3371 rq->buffer = bio_data(rq->bio); 3372 } 3373 3374 /* 3375 * if total number of sectors is less than the first segment 3376 * size, something has gone terribly wrong 3377 */ 3378 if (rq->nr_sectors < rq->current_nr_sectors) { 3379 printk("blk: request botched\n"); 3380 rq->nr_sectors = rq->current_nr_sectors; 3381 } 3382 } 3383} 3384 3385static int __end_that_request_first(struct request *req, int uptodate, 3386 int nr_bytes) 3387{ 3388 int total_bytes, bio_nbytes, error, next_idx = 0; 3389 struct bio *bio; 3390 3391 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE); 3392 3393 /* 3394 * extend uptodate bool to allow < 0 value to be direct io error 3395 */ 3396 error = 0; 3397 if (end_io_error(uptodate)) 3398 error = !uptodate ? -EIO : uptodate; 3399 3400 /* 3401 * for a REQ_BLOCK_PC request, we want to carry any eventual 3402 * sense key with us all the way through 3403 */ 3404 if (!blk_pc_request(req)) 3405 req->errors = 0; 3406 3407 if (!uptodate) { 3408 if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET)) 3409 printk("end_request: I/O error, dev %s, sector %llu\n", 3410 req->rq_disk ? req->rq_disk->disk_name : "?", 3411 (unsigned long long)req->sector); 3412 } 3413 3414 if (blk_fs_request(req) && req->rq_disk) { 3415 const int rw = rq_data_dir(req); 3416 3417 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9); 3418 } 3419 3420 total_bytes = bio_nbytes = 0; 3421 while ((bio = req->bio) != NULL) { 3422 int nbytes; 3423 3424 if (nr_bytes >= bio->bi_size) { 3425 req->bio = bio->bi_next; 3426 nbytes = bio->bi_size; 3427 if (!ordered_bio_endio(req, bio, nbytes, error)) 3428 bio_endio(bio, nbytes, error); 3429 next_idx = 0; 3430 bio_nbytes = 0; 3431 } else { 3432 int idx = bio->bi_idx + next_idx; 3433 3434 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) { 3435 blk_dump_rq_flags(req, "__end_that"); 3436 printk("%s: bio idx %d >= vcnt %d\n", 3437 __FUNCTION__, 3438 bio->bi_idx, bio->bi_vcnt); 3439 break; 3440 } 3441 3442 nbytes = bio_iovec_idx(bio, idx)->bv_len; 3443 BIO_BUG_ON(nbytes > bio->bi_size); 3444 3445 /* 3446 * not a complete bvec done 3447 */ 3448 if (unlikely(nbytes > nr_bytes)) { 3449 bio_nbytes += nr_bytes; 3450 total_bytes += nr_bytes; 3451 break; 3452 } 3453 3454 /* 3455 * advance to the next vector 3456 */ 3457 next_idx++; 3458 bio_nbytes += nbytes; 3459 } 3460 3461 total_bytes += nbytes; 3462 nr_bytes -= nbytes; 3463 3464 if ((bio = req->bio)) { 3465 /* 3466 * end more in this run, or just return 'not-done' 3467 */ 3468 if (unlikely(nr_bytes <= 0)) 3469 break; 3470 } 3471 } 3472 3473 /* 3474 * completely done 3475 */ 3476 if (!req->bio) 3477 return 0; 3478 3479 /* 3480 * if the request wasn't completed, update state 3481 */ 3482 if (bio_nbytes) { 3483 if (!ordered_bio_endio(req, bio, bio_nbytes, error)) 3484 bio_endio(bio, bio_nbytes, error); 3485 bio->bi_idx += next_idx; 3486 bio_iovec(bio)->bv_offset += nr_bytes; 3487 bio_iovec(bio)->bv_len -= nr_bytes; 3488 } 3489 3490 blk_recalc_rq_sectors(req, total_bytes >> 9); 3491 blk_recalc_rq_segments(req); 3492 return 1; 3493} 3494 3495/** 3496 * end_that_request_first - end I/O on a request 3497 * @req: the request being processed 3498 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error 3499 * @nr_sectors: number of sectors to end I/O on 3500 * 3501 * Description: 3502 * Ends I/O on a number of sectors attached to @req, and sets it up 3503 * for the next range of segments (if any) in the cluster. 3504 * 3505 * Return: 3506 * 0 - we are done with this request, call end_that_request_last() 3507 * 1 - still buffers pending for this request 3508 **/ 3509int end_that_request_first(struct request *req, int uptodate, int nr_sectors) 3510{ 3511 return __end_that_request_first(req, uptodate, nr_sectors << 9); 3512} 3513 3514EXPORT_SYMBOL(end_that_request_first); 3515 3516/** 3517 * end_that_request_chunk - end I/O on a request 3518 * @req: the request being processed 3519 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error 3520 * @nr_bytes: number of bytes to complete 3521 * 3522 * Description: 3523 * Ends I/O on a number of bytes attached to @req, and sets it up 3524 * for the next range of segments (if any). Like end_that_request_first(), 3525 * but deals with bytes instead of sectors. 3526 * 3527 * Return: 3528 * 0 - we are done with this request, call end_that_request_last() 3529 * 1 - still buffers pending for this request 3530 **/ 3531int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes) 3532{ 3533 return __end_that_request_first(req, uptodate, nr_bytes); 3534} 3535 3536EXPORT_SYMBOL(end_that_request_chunk); 3537 3538/* 3539 * splice the completion data to a local structure and hand off to 3540 * process_completion_queue() to complete the requests 3541 */ 3542static void blk_done_softirq(struct softirq_action *h) 3543{ 3544 struct list_head *cpu_list, local_list; 3545 3546 local_irq_disable(); 3547 cpu_list = &__get_cpu_var(blk_cpu_done); 3548 list_replace_init(cpu_list, &local_list); 3549 local_irq_enable(); 3550 3551 while (!list_empty(&local_list)) { 3552 struct request *rq = list_entry(local_list.next, struct request, donelist); 3553 3554 list_del_init(&rq->donelist); 3555 rq->q->softirq_done_fn(rq); 3556 } 3557} 3558 3559static int blk_cpu_notify(struct notifier_block *self, unsigned long action, 3560 void *hcpu) 3561{ 3562 /* 3563 * If a CPU goes away, splice its entries to the current CPU 3564 * and trigger a run of the softirq 3565 */ 3566 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) { 3567 int cpu = (unsigned long) hcpu; 3568 3569 local_irq_disable(); 3570 list_splice_init(&per_cpu(blk_cpu_done, cpu), 3571 &__get_cpu_var(blk_cpu_done)); 3572 raise_softirq_irqoff(BLOCK_SOFTIRQ); 3573 local_irq_enable(); 3574 } 3575 3576 return NOTIFY_OK; 3577} 3578 3579 3580static struct notifier_block __devinitdata blk_cpu_notifier = { 3581 .notifier_call = blk_cpu_notify, 3582}; 3583 3584/** 3585 * blk_complete_request - end I/O on a request 3586 * @req: the request being processed 3587 * 3588 * Description: 3589 * Ends all I/O on a request. It does not handle partial completions, 3590 * unless the driver actually implements this in its completion callback 3591 * through requeueing. Theh actual completion happens out-of-order, 3592 * through a softirq handler. The user must have registered a completion 3593 * callback through blk_queue_softirq_done(). 3594 **/ 3595 3596void blk_complete_request(struct request *req) 3597{ 3598 struct list_head *cpu_list; 3599 unsigned long flags; 3600 3601 BUG_ON(!req->q->softirq_done_fn); 3602 3603 local_irq_save(flags); 3604 3605 cpu_list = &__get_cpu_var(blk_cpu_done); 3606 list_add_tail(&req->donelist, cpu_list); 3607 raise_softirq_irqoff(BLOCK_SOFTIRQ); 3608 3609 local_irq_restore(flags); 3610} 3611 3612EXPORT_SYMBOL(blk_complete_request); 3613 3614/* 3615 * queue lock must be held 3616 */ 3617void end_that_request_last(struct request *req, int uptodate) 3618{ 3619 struct gendisk *disk = req->rq_disk; 3620 int error; 3621 3622 /* 3623 * extend uptodate bool to allow < 0 value to be direct io error 3624 */ 3625 error = 0; 3626 if (end_io_error(uptodate)) 3627 error = !uptodate ? -EIO : uptodate; 3628 3629 if (unlikely(laptop_mode) && blk_fs_request(req)) 3630 laptop_io_completion(); 3631 3632 /* 3633 * Account IO completion. bar_rq isn't accounted as a normal 3634 * IO on queueing nor completion. Accounting the containing 3635 * request is enough. 3636 */ 3637 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) { 3638 unsigned long duration = jiffies - req->start_time; 3639 const int rw = rq_data_dir(req); 3640 3641 __disk_stat_inc(disk, ios[rw]); 3642 __disk_stat_add(disk, ticks[rw], duration); 3643 disk_round_stats(disk); 3644 disk->in_flight--; 3645 } 3646 if (req->end_io) 3647 req->end_io(req, error); 3648 else 3649 __blk_put_request(req->q, req); 3650} 3651 3652EXPORT_SYMBOL(end_that_request_last); 3653 3654void end_request(struct request *req, int uptodate) 3655{ 3656 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) { 3657 add_disk_randomness(req->rq_disk); 3658 blkdev_dequeue_request(req); 3659 end_that_request_last(req, uptodate); 3660 } 3661} 3662 3663EXPORT_SYMBOL(end_request); 3664 3665void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio) 3666{ 3667 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */ 3668 rq->cmd_flags |= (bio->bi_rw & 3); 3669 3670 rq->nr_phys_segments = bio_phys_segments(q, bio); 3671 rq->nr_hw_segments = bio_hw_segments(q, bio); 3672 rq->current_nr_sectors = bio_cur_sectors(bio); 3673 rq->hard_cur_sectors = rq->current_nr_sectors; 3674 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio); 3675 rq->buffer = bio_data(bio); 3676 rq->data_len = bio->bi_size; 3677 3678 rq->bio = rq->biotail = bio; 3679} 3680 3681EXPORT_SYMBOL(blk_rq_bio_prep); 3682 3683int kblockd_schedule_work(struct work_struct *work) 3684{ 3685 return queue_work(kblockd_workqueue, work); 3686} 3687 3688EXPORT_SYMBOL(kblockd_schedule_work); 3689 3690void kblockd_flush_work(struct work_struct *work) 3691{ 3692 cancel_work_sync(work); 3693} 3694EXPORT_SYMBOL(kblockd_flush_work); 3695 3696int __init blk_dev_init(void) 3697{ 3698 int i; 3699 3700 kblockd_workqueue = create_workqueue("kblockd"); 3701 if (!kblockd_workqueue) 3702 panic("Failed to create kblockd\n"); 3703 3704 request_cachep = kmem_cache_create("blkdev_requests", 3705 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL); 3706 3707 requestq_cachep = kmem_cache_create("blkdev_queue", 3708 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL); 3709 3710 iocontext_cachep = kmem_cache_create("blkdev_ioc", 3711 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL); 3712 3713 for_each_possible_cpu(i) 3714 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i)); 3715 3716 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL); 3717 register_hotcpu_notifier(&blk_cpu_notifier); 3718 3719 blk_max_low_pfn = max_low_pfn - 1; 3720 blk_max_pfn = max_pfn - 1; 3721 3722 return 0; 3723} 3724 3725/* 3726 * IO Context helper functions 3727 */ 3728void put_io_context(struct io_context *ioc) 3729{ 3730 if (ioc == NULL) 3731 return; 3732 3733 BUG_ON(atomic_read(&ioc->refcount) == 0); 3734 3735 if (atomic_dec_and_test(&ioc->refcount)) { 3736 struct cfq_io_context *cic; 3737 3738 rcu_read_lock(); 3739 if (ioc->aic && ioc->aic->dtor) 3740 ioc->aic->dtor(ioc->aic); 3741 if (ioc->cic_root.rb_node != NULL) { 3742 struct rb_node *n = rb_first(&ioc->cic_root); 3743 3744 cic = rb_entry(n, struct cfq_io_context, rb_node); 3745 cic->dtor(ioc); 3746 } 3747 rcu_read_unlock(); 3748 3749 kmem_cache_free(iocontext_cachep, ioc); 3750 } 3751} 3752EXPORT_SYMBOL(put_io_context); 3753 3754/* Called by the exitting task */ 3755void exit_io_context(void) 3756{ 3757 struct io_context *ioc; 3758 struct cfq_io_context *cic; 3759 3760 task_lock(current); 3761 ioc = current->io_context; 3762 current->io_context = NULL; 3763 task_unlock(current); 3764 3765 ioc->task = NULL; 3766 if (ioc->aic && ioc->aic->exit) 3767 ioc->aic->exit(ioc->aic); 3768 if (ioc->cic_root.rb_node != NULL) { 3769 cic = rb_entry(rb_first(&ioc->cic_root), struct cfq_io_context, rb_node); 3770 cic->exit(ioc); 3771 } 3772 3773 put_io_context(ioc); 3774} 3775 3776/* 3777 * If the current task has no IO context then create one and initialise it. 3778 * Otherwise, return its existing IO context. 3779 * 3780 * This returned IO context doesn't have a specifically elevated refcount, 3781 * but since the current task itself holds a reference, the context can be 3782 * used in general code, so long as it stays within `current` context. 3783 */ 3784static struct io_context *current_io_context(gfp_t gfp_flags, int node) 3785{ 3786 struct task_struct *tsk = current; 3787 struct io_context *ret; 3788 3789 ret = tsk->io_context; 3790 if (likely(ret)) 3791 return ret; 3792 3793 ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node); 3794 if (ret) { 3795 atomic_set(&ret->refcount, 1); 3796 ret->task = current; 3797 ret->ioprio_changed = 0; 3798 ret->last_waited = jiffies; /* doesn't matter... */ 3799 ret->nr_batch_requests = 0; /* because this is 0 */ 3800 ret->aic = NULL; 3801 ret->cic_root.rb_node = NULL; 3802 ret->ioc_data = NULL; 3803 /* make sure set_task_ioprio() sees the settings above */ 3804 smp_wmb(); 3805 tsk->io_context = ret; 3806 } 3807 3808 return ret; 3809} 3810 3811/* 3812 * If the current task has no IO context then create one and initialise it. 3813 * If it does have a context, take a ref on it. 3814 * 3815 * This is always called in the context of the task which submitted the I/O. 3816 */ 3817struct io_context *get_io_context(gfp_t gfp_flags, int node) 3818{ 3819 struct io_context *ret; 3820 ret = current_io_context(gfp_flags, node); 3821 if (likely(ret)) 3822 atomic_inc(&ret->refcount); 3823 return ret; 3824} 3825EXPORT_SYMBOL(get_io_context); 3826 3827void copy_io_context(struct io_context **pdst, struct io_context **psrc) 3828{ 3829 struct io_context *src = *psrc; 3830 struct io_context *dst = *pdst; 3831 3832 if (src) { 3833 BUG_ON(atomic_read(&src->refcount) == 0); 3834 atomic_inc(&src->refcount); 3835 put_io_context(dst); 3836 *pdst = src; 3837 } 3838} 3839EXPORT_SYMBOL(copy_io_context); 3840 3841void swap_io_context(struct io_context **ioc1, struct io_context **ioc2) 3842{ 3843 struct io_context *temp; 3844 temp = *ioc1; 3845 *ioc1 = *ioc2; 3846 *ioc2 = temp; 3847} 3848EXPORT_SYMBOL(swap_io_context); 3849 3850/* 3851 * sysfs parts below 3852 */ 3853struct queue_sysfs_entry { 3854 struct attribute attr; 3855 ssize_t (*show)(struct request_queue *, char *); 3856 ssize_t (*store)(struct request_queue *, const char *, size_t); 3857}; 3858 3859static ssize_t 3860queue_var_show(unsigned int var, char *page) 3861{ 3862 return sprintf(page, "%d\n", var); 3863} 3864 3865static ssize_t 3866queue_var_store(unsigned long *var, const char *page, size_t count) 3867{ 3868 char *p = (char *) page; 3869 3870 *var = simple_strtoul(p, &p, 10); 3871 return count; 3872} 3873 3874static ssize_t queue_requests_show(struct request_queue *q, char *page) 3875{ 3876 return queue_var_show(q->nr_requests, (page)); 3877} 3878 3879static ssize_t 3880queue_requests_store(struct request_queue *q, const char *page, size_t count) 3881{ 3882 struct request_list *rl = &q->rq; 3883 unsigned long nr; 3884 int ret = queue_var_store(&nr, page, count); 3885 if (nr < BLKDEV_MIN_RQ) 3886 nr = BLKDEV_MIN_RQ; 3887 3888 spin_lock_irq(q->queue_lock); 3889 q->nr_requests = nr; 3890 blk_queue_congestion_threshold(q); 3891 3892 if (rl->count[READ] >= queue_congestion_on_threshold(q)) 3893 blk_set_queue_congested(q, READ); 3894 else if (rl->count[READ] < queue_congestion_off_threshold(q)) 3895 blk_clear_queue_congested(q, READ); 3896 3897 if (rl->count[WRITE] >= queue_congestion_on_threshold(q)) 3898 blk_set_queue_congested(q, WRITE); 3899 else if (rl->count[WRITE] < queue_congestion_off_threshold(q)) 3900 blk_clear_queue_congested(q, WRITE); 3901 3902 if (rl->count[READ] >= q->nr_requests) { 3903 blk_set_queue_full(q, READ); 3904 } else if (rl->count[READ]+1 <= q->nr_requests) { 3905 blk_clear_queue_full(q, READ); 3906 wake_up(&rl->wait[READ]); 3907 } 3908 3909 if (rl->count[WRITE] >= q->nr_requests) { 3910 blk_set_queue_full(q, WRITE); 3911 } else if (rl->count[WRITE]+1 <= q->nr_requests) { 3912 blk_clear_queue_full(q, WRITE); 3913 wake_up(&rl->wait[WRITE]); 3914 } 3915 spin_unlock_irq(q->queue_lock); 3916 return ret; 3917} 3918 3919static ssize_t queue_ra_show(struct request_queue *q, char *page) 3920{ 3921 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10); 3922 3923 return queue_var_show(ra_kb, (page)); 3924} 3925 3926static ssize_t 3927queue_ra_store(struct request_queue *q, const char *page, size_t count) 3928{ 3929 unsigned long ra_kb; 3930 ssize_t ret = queue_var_store(&ra_kb, page, count); 3931 3932 spin_lock_irq(q->queue_lock); 3933 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); 3934 spin_unlock_irq(q->queue_lock); 3935 3936 return ret; 3937} 3938 3939static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) 3940{ 3941 int max_sectors_kb = q->max_sectors >> 1; 3942 3943 return queue_var_show(max_sectors_kb, (page)); 3944} 3945 3946static ssize_t 3947queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) 3948{ 3949 unsigned long max_sectors_kb, 3950 max_hw_sectors_kb = q->max_hw_sectors >> 1, 3951 page_kb = 1 << (PAGE_CACHE_SHIFT - 10); 3952 ssize_t ret = queue_var_store(&max_sectors_kb, page, count); 3953 int ra_kb; 3954 3955 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) 3956 return -EINVAL; 3957 /* 3958 * Take the queue lock to update the readahead and max_sectors 3959 * values synchronously: 3960 */ 3961 spin_lock_irq(q->queue_lock); 3962 /* 3963 * Trim readahead window as well, if necessary: 3964 */ 3965 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10); 3966 if (ra_kb > max_sectors_kb) 3967 q->backing_dev_info.ra_pages = 3968 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10); 3969 3970 q->max_sectors = max_sectors_kb << 1; 3971 spin_unlock_irq(q->queue_lock); 3972 3973 return ret; 3974} 3975 3976static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) 3977{ 3978 int max_hw_sectors_kb = q->max_hw_sectors >> 1; 3979 3980 return queue_var_show(max_hw_sectors_kb, (page)); 3981} 3982 3983 3984static struct queue_sysfs_entry queue_requests_entry = { 3985 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 3986 .show = queue_requests_show, 3987 .store = queue_requests_store, 3988}; 3989 3990static struct queue_sysfs_entry queue_ra_entry = { 3991 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, 3992 .show = queue_ra_show, 3993 .store = queue_ra_store, 3994}; 3995 3996static struct queue_sysfs_entry queue_max_sectors_entry = { 3997 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, 3998 .show = queue_max_sectors_show, 3999 .store = queue_max_sectors_store, 4000}; 4001 4002static struct queue_sysfs_entry queue_max_hw_sectors_entry = { 4003 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, 4004 .show = queue_max_hw_sectors_show, 4005}; 4006 4007static struct queue_sysfs_entry queue_iosched_entry = { 4008 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, 4009 .show = elv_iosched_show, 4010 .store = elv_iosched_store, 4011}; 4012 4013static struct attribute *default_attrs[] = { 4014 &queue_requests_entry.attr, 4015 &queue_ra_entry.attr, 4016 &queue_max_hw_sectors_entry.attr, 4017 &queue_max_sectors_entry.attr, 4018 &queue_iosched_entry.attr, 4019 NULL, 4020}; 4021 4022#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) 4023 4024static ssize_t 4025queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 4026{ 4027 struct queue_sysfs_entry *entry = to_queue(attr); 4028 request_queue_t *q = container_of(kobj, struct request_queue, kobj); 4029 ssize_t res; 4030 4031 if (!entry->show) 4032 return -EIO; 4033 mutex_lock(&q->sysfs_lock); 4034 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 4035 mutex_unlock(&q->sysfs_lock); 4036 return -ENOENT; 4037 } 4038 res = entry->show(q, page); 4039 mutex_unlock(&q->sysfs_lock); 4040 return res; 4041} 4042 4043static ssize_t 4044queue_attr_store(struct kobject *kobj, struct attribute *attr, 4045 const char *page, size_t length) 4046{ 4047 struct queue_sysfs_entry *entry = to_queue(attr); 4048 request_queue_t *q = container_of(kobj, struct request_queue, kobj); 4049 4050 ssize_t res; 4051 4052 if (!entry->store) 4053 return -EIO; 4054 mutex_lock(&q->sysfs_lock); 4055 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 4056 mutex_unlock(&q->sysfs_lock); 4057 return -ENOENT; 4058 } 4059 res = entry->store(q, page, length); 4060 mutex_unlock(&q->sysfs_lock); 4061 return res; 4062} 4063 4064static struct sysfs_ops queue_sysfs_ops = { 4065 .show = queue_attr_show, 4066 .store = queue_attr_store, 4067}; 4068 4069static struct kobj_type queue_ktype = { 4070 .sysfs_ops = &queue_sysfs_ops, 4071 .default_attrs = default_attrs, 4072 .release = blk_release_queue, 4073}; 4074 4075int blk_register_queue(struct gendisk *disk) 4076{ 4077 int ret; 4078 4079 request_queue_t *q = disk->queue; 4080 4081 if (!q || !q->request_fn) 4082 return -ENXIO; 4083 4084 q->kobj.parent = kobject_get(&disk->kobj); 4085 4086 ret = kobject_add(&q->kobj); 4087 if (ret < 0) 4088 return ret; 4089 4090 kobject_uevent(&q->kobj, KOBJ_ADD); 4091 4092 ret = elv_register_queue(q); 4093 if (ret) { 4094 kobject_uevent(&q->kobj, KOBJ_REMOVE); 4095 kobject_del(&q->kobj); 4096 return ret; 4097 } 4098 4099 return 0; 4100} 4101 4102void blk_unregister_queue(struct gendisk *disk) 4103{ 4104 request_queue_t *q = disk->queue; 4105 4106 if (q && q->request_fn) { 4107 elv_unregister_queue(q); 4108 4109 kobject_uevent(&q->kobj, KOBJ_REMOVE); 4110 kobject_del(&q->kobj); 4111 kobject_put(&disk->kobj); 4112 } 4113} 4114