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> 7 * - July2000 8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001 9 */ 10 11 /* 12 * This handles all read/write requests to block devices 13 */ 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/backing-dev.h> 17 #include <linux/bio.h> 18 #include <linux/blkdev.h> 19 #include <linux/highmem.h> 20 #include <linux/mm.h> 21 #include <linux/kernel_stat.h> 22 #include <linux/string.h> 23 #include <linux/init.h> 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 #include "blk.h" 35 36 static int __make_request(struct request_queue *q, struct bio *bio); 37 38 /* 39 * For the allocated request tables 40 */ 41 struct kmem_cache *request_cachep; 42 43 /* 44 * For queue allocation 45 */ 46 struct kmem_cache *blk_requestq_cachep; 47 48 /* 49 * Controlling structure to kblockd 50 */ 51 static struct workqueue_struct *kblockd_workqueue; 52 53 static DEFINE_PER_CPU(struct list_head, blk_cpu_done); 54 55 static void drive_stat_acct(struct request *rq, int new_io) 56 { 57 int rw = rq_data_dir(rq); 58 59 if (!blk_fs_request(rq) || !rq->rq_disk) 60 return; 61 62 if (!new_io) { 63 __disk_stat_inc(rq->rq_disk, merges[rw]); 64 } else { 65 disk_round_stats(rq->rq_disk); 66 rq->rq_disk->in_flight++; 67 } 68 } 69 70 void blk_queue_congestion_threshold(struct request_queue *q) 71 { 72 int nr; 73 74 nr = q->nr_requests - (q->nr_requests / 8) + 1; 75 if (nr > q->nr_requests) 76 nr = q->nr_requests; 77 q->nr_congestion_on = nr; 78 79 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1; 80 if (nr < 1) 81 nr = 1; 82 q->nr_congestion_off = nr; 83 } 84 85 /** 86 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info 87 * @bdev: device 88 * 89 * Locates the passed device's request queue and returns the address of its 90 * backing_dev_info 91 * 92 * Will return NULL if the request queue cannot be located. 93 */ 94 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev) 95 { 96 struct backing_dev_info *ret = NULL; 97 struct request_queue *q = bdev_get_queue(bdev); 98 99 if (q) 100 ret = &q->backing_dev_info; 101 return ret; 102 } 103 EXPORT_SYMBOL(blk_get_backing_dev_info); 104 105 void rq_init(struct request_queue *q, struct request *rq) 106 { 107 INIT_LIST_HEAD(&rq->queuelist); 108 INIT_LIST_HEAD(&rq->donelist); 109 110 rq->errors = 0; 111 rq->bio = rq->biotail = NULL; 112 INIT_HLIST_NODE(&rq->hash); 113 RB_CLEAR_NODE(&rq->rb_node); 114 rq->ioprio = 0; 115 rq->buffer = NULL; 116 rq->ref_count = 1; 117 rq->q = q; 118 rq->special = NULL; 119 rq->data_len = 0; 120 rq->data = NULL; 121 rq->nr_phys_segments = 0; 122 rq->sense = NULL; 123 rq->end_io = NULL; 124 rq->end_io_data = NULL; 125 rq->completion_data = NULL; 126 rq->next_rq = NULL; 127 } 128 129 static void req_bio_endio(struct request *rq, struct bio *bio, 130 unsigned int nbytes, int error) 131 { 132 struct request_queue *q = rq->q; 133 134 if (&q->bar_rq != rq) { 135 if (error) 136 clear_bit(BIO_UPTODATE, &bio->bi_flags); 137 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) 138 error = -EIO; 139 140 if (unlikely(nbytes > bio->bi_size)) { 141 printk(KERN_ERR "%s: want %u bytes done, %u left\n", 142 __FUNCTION__, nbytes, bio->bi_size); 143 nbytes = bio->bi_size; 144 } 145 146 bio->bi_size -= nbytes; 147 bio->bi_sector += (nbytes >> 9); 148 if (bio->bi_size == 0) 149 bio_endio(bio, error); 150 } else { 151 152 /* 153 * Okay, this is the barrier request in progress, just 154 * record the error; 155 */ 156 if (error && !q->orderr) 157 q->orderr = error; 158 } 159 } 160 161 void blk_dump_rq_flags(struct request *rq, char *msg) 162 { 163 int bit; 164 165 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg, 166 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type, 167 rq->cmd_flags); 168 169 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n", 170 (unsigned long long)rq->sector, 171 rq->nr_sectors, 172 rq->current_nr_sectors); 173 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n", 174 rq->bio, rq->biotail, 175 rq->buffer, rq->data, 176 rq->data_len); 177 178 if (blk_pc_request(rq)) { 179 printk(KERN_INFO " cdb: "); 180 for (bit = 0; bit < sizeof(rq->cmd); bit++) 181 printk("%02x ", rq->cmd[bit]); 182 printk("\n"); 183 } 184 } 185 EXPORT_SYMBOL(blk_dump_rq_flags); 186 187 /* 188 * "plug" the device if there are no outstanding requests: this will 189 * force the transfer to start only after we have put all the requests 190 * on the list. 191 * 192 * This is called with interrupts off and no requests on the queue and 193 * with the queue lock held. 194 */ 195 void blk_plug_device(struct request_queue *q) 196 { 197 WARN_ON(!irqs_disabled()); 198 199 /* 200 * don't plug a stopped queue, it must be paired with blk_start_queue() 201 * which will restart the queueing 202 */ 203 if (blk_queue_stopped(q)) 204 return; 205 206 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) { 207 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay); 208 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG); 209 } 210 } 211 EXPORT_SYMBOL(blk_plug_device); 212 213 /* 214 * remove the queue from the plugged list, if present. called with 215 * queue lock held and interrupts disabled. 216 */ 217 int blk_remove_plug(struct request_queue *q) 218 { 219 WARN_ON(!irqs_disabled()); 220 221 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) 222 return 0; 223 224 del_timer(&q->unplug_timer); 225 return 1; 226 } 227 EXPORT_SYMBOL(blk_remove_plug); 228 229 /* 230 * remove the plug and let it rip.. 231 */ 232 void __generic_unplug_device(struct request_queue *q) 233 { 234 if (unlikely(blk_queue_stopped(q))) 235 return; 236 237 if (!blk_remove_plug(q)) 238 return; 239 240 q->request_fn(q); 241 } 242 EXPORT_SYMBOL(__generic_unplug_device); 243 244 /** 245 * generic_unplug_device - fire a request queue 246 * @q: The &struct request_queue in question 247 * 248 * Description: 249 * Linux uses plugging to build bigger requests queues before letting 250 * the device have at them. If a queue is plugged, the I/O scheduler 251 * is still adding and merging requests on the queue. Once the queue 252 * gets unplugged, the request_fn defined for the queue is invoked and 253 * transfers started. 254 **/ 255 void generic_unplug_device(struct request_queue *q) 256 { 257 spin_lock_irq(q->queue_lock); 258 __generic_unplug_device(q); 259 spin_unlock_irq(q->queue_lock); 260 } 261 EXPORT_SYMBOL(generic_unplug_device); 262 263 static void blk_backing_dev_unplug(struct backing_dev_info *bdi, 264 struct page *page) 265 { 266 struct request_queue *q = bdi->unplug_io_data; 267 268 blk_unplug(q); 269 } 270 271 void blk_unplug_work(struct work_struct *work) 272 { 273 struct request_queue *q = 274 container_of(work, struct request_queue, unplug_work); 275 276 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL, 277 q->rq.count[READ] + q->rq.count[WRITE]); 278 279 q->unplug_fn(q); 280 } 281 282 void blk_unplug_timeout(unsigned long data) 283 { 284 struct request_queue *q = (struct request_queue *)data; 285 286 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL, 287 q->rq.count[READ] + q->rq.count[WRITE]); 288 289 kblockd_schedule_work(&q->unplug_work); 290 } 291 292 void blk_unplug(struct request_queue *q) 293 { 294 /* 295 * devices don't necessarily have an ->unplug_fn defined 296 */ 297 if (q->unplug_fn) { 298 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL, 299 q->rq.count[READ] + q->rq.count[WRITE]); 300 301 q->unplug_fn(q); 302 } 303 } 304 EXPORT_SYMBOL(blk_unplug); 305 306 /** 307 * blk_start_queue - restart a previously stopped queue 308 * @q: The &struct request_queue in question 309 * 310 * Description: 311 * blk_start_queue() will clear the stop flag on the queue, and call 312 * the request_fn for the queue if it was in a stopped state when 313 * entered. Also see blk_stop_queue(). Queue lock must be held. 314 **/ 315 void blk_start_queue(struct request_queue *q) 316 { 317 WARN_ON(!irqs_disabled()); 318 319 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags); 320 321 /* 322 * one level of recursion is ok and is much faster than kicking 323 * the unplug handling 324 */ 325 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) { 326 q->request_fn(q); 327 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags); 328 } else { 329 blk_plug_device(q); 330 kblockd_schedule_work(&q->unplug_work); 331 } 332 } 333 EXPORT_SYMBOL(blk_start_queue); 334 335 /** 336 * blk_stop_queue - stop a queue 337 * @q: The &struct request_queue in question 338 * 339 * Description: 340 * The Linux block layer assumes that a block driver will consume all 341 * entries on the request queue when the request_fn strategy is called. 342 * Often this will not happen, because of hardware limitations (queue 343 * depth settings). If a device driver gets a 'queue full' response, 344 * or if it simply chooses not to queue more I/O at one point, it can 345 * call this function to prevent the request_fn from being called until 346 * the driver has signalled it's ready to go again. This happens by calling 347 * blk_start_queue() to restart queue operations. Queue lock must be held. 348 **/ 349 void blk_stop_queue(struct request_queue *q) 350 { 351 blk_remove_plug(q); 352 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags); 353 } 354 EXPORT_SYMBOL(blk_stop_queue); 355 356 /** 357 * blk_sync_queue - cancel any pending callbacks on a queue 358 * @q: the queue 359 * 360 * Description: 361 * The block layer may perform asynchronous callback activity 362 * on a queue, such as calling the unplug function after a timeout. 363 * A block device may call blk_sync_queue to ensure that any 364 * such activity is cancelled, thus allowing it to release resources 365 * that the callbacks might use. The caller must already have made sure 366 * that its ->make_request_fn will not re-add plugging prior to calling 367 * this function. 368 * 369 */ 370 void blk_sync_queue(struct request_queue *q) 371 { 372 del_timer_sync(&q->unplug_timer); 373 kblockd_flush_work(&q->unplug_work); 374 } 375 EXPORT_SYMBOL(blk_sync_queue); 376 377 /** 378 * blk_run_queue - run a single device queue 379 * @q: The queue to run 380 */ 381 void blk_run_queue(struct request_queue *q) 382 { 383 unsigned long flags; 384 385 spin_lock_irqsave(q->queue_lock, flags); 386 blk_remove_plug(q); 387 388 /* 389 * Only recurse once to avoid overrunning the stack, let the unplug 390 * handling reinvoke the handler shortly if we already got there. 391 */ 392 if (!elv_queue_empty(q)) { 393 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) { 394 q->request_fn(q); 395 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags); 396 } else { 397 blk_plug_device(q); 398 kblockd_schedule_work(&q->unplug_work); 399 } 400 } 401 402 spin_unlock_irqrestore(q->queue_lock, flags); 403 } 404 EXPORT_SYMBOL(blk_run_queue); 405 406 void blk_put_queue(struct request_queue *q) 407 { 408 kobject_put(&q->kobj); 409 } 410 EXPORT_SYMBOL(blk_put_queue); 411 412 void blk_cleanup_queue(struct request_queue *q) 413 { 414 mutex_lock(&q->sysfs_lock); 415 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags); 416 mutex_unlock(&q->sysfs_lock); 417 418 if (q->elevator) 419 elevator_exit(q->elevator); 420 421 blk_put_queue(q); 422 } 423 EXPORT_SYMBOL(blk_cleanup_queue); 424 425 static int blk_init_free_list(struct request_queue *q) 426 { 427 struct request_list *rl = &q->rq; 428 429 rl->count[READ] = rl->count[WRITE] = 0; 430 rl->starved[READ] = rl->starved[WRITE] = 0; 431 rl->elvpriv = 0; 432 init_waitqueue_head(&rl->wait[READ]); 433 init_waitqueue_head(&rl->wait[WRITE]); 434 435 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab, 436 mempool_free_slab, request_cachep, q->node); 437 438 if (!rl->rq_pool) 439 return -ENOMEM; 440 441 return 0; 442 } 443 444 struct request_queue *blk_alloc_queue(gfp_t gfp_mask) 445 { 446 return blk_alloc_queue_node(gfp_mask, -1); 447 } 448 EXPORT_SYMBOL(blk_alloc_queue); 449 450 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id) 451 { 452 struct request_queue *q; 453 int err; 454 455 q = kmem_cache_alloc_node(blk_requestq_cachep, 456 gfp_mask | __GFP_ZERO, node_id); 457 if (!q) 458 return NULL; 459 460 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug; 461 q->backing_dev_info.unplug_io_data = q; 462 err = bdi_init(&q->backing_dev_info); 463 if (err) { 464 kmem_cache_free(blk_requestq_cachep, q); 465 return NULL; 466 } 467 468 init_timer(&q->unplug_timer); 469 470 kobject_init(&q->kobj, &blk_queue_ktype); 471 472 mutex_init(&q->sysfs_lock); 473 474 return q; 475 } 476 EXPORT_SYMBOL(blk_alloc_queue_node); 477 478 /** 479 * blk_init_queue - prepare a request queue for use with a block device 480 * @rfn: The function to be called to process requests that have been 481 * placed on the queue. 482 * @lock: Request queue spin lock 483 * 484 * Description: 485 * If a block device wishes to use the standard request handling procedures, 486 * which sorts requests and coalesces adjacent requests, then it must 487 * call blk_init_queue(). The function @rfn will be called when there 488 * are requests on the queue that need to be processed. If the device 489 * supports plugging, then @rfn may not be called immediately when requests 490 * are available on the queue, but may be called at some time later instead. 491 * Plugged queues are generally unplugged when a buffer belonging to one 492 * of the requests on the queue is needed, or due to memory pressure. 493 * 494 * @rfn is not required, or even expected, to remove all requests off the 495 * queue, but only as many as it can handle at a time. If it does leave 496 * requests on the queue, it is responsible for arranging that the requests 497 * get dealt with eventually. 498 * 499 * The queue spin lock must be held while manipulating the requests on the 500 * request queue; this lock will be taken also from interrupt context, so irq 501 * disabling is needed for it. 502 * 503 * Function returns a pointer to the initialized request queue, or NULL if 504 * it didn't succeed. 505 * 506 * Note: 507 * blk_init_queue() must be paired with a blk_cleanup_queue() call 508 * when the block device is deactivated (such as at module unload). 509 **/ 510 511 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock) 512 { 513 return blk_init_queue_node(rfn, lock, -1); 514 } 515 EXPORT_SYMBOL(blk_init_queue); 516 517 struct request_queue * 518 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) 519 { 520 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id); 521 522 if (!q) 523 return NULL; 524 525 q->node = node_id; 526 if (blk_init_free_list(q)) { 527 kmem_cache_free(blk_requestq_cachep, q); 528 return NULL; 529 } 530 531 /* 532 * if caller didn't supply a lock, they get per-queue locking with 533 * our embedded lock 534 */ 535 if (!lock) { 536 spin_lock_init(&q->__queue_lock); 537 lock = &q->__queue_lock; 538 } 539 540 q->request_fn = rfn; 541 q->prep_rq_fn = NULL; 542 q->unplug_fn = generic_unplug_device; 543 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER); 544 q->queue_lock = lock; 545 546 blk_queue_segment_boundary(q, 0xffffffff); 547 548 blk_queue_make_request(q, __make_request); 549 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE); 550 551 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS); 552 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS); 553 554 q->sg_reserved_size = INT_MAX; 555 556 /* 557 * all done 558 */ 559 if (!elevator_init(q, NULL)) { 560 blk_queue_congestion_threshold(q); 561 return q; 562 } 563 564 blk_put_queue(q); 565 return NULL; 566 } 567 EXPORT_SYMBOL(blk_init_queue_node); 568 569 int blk_get_queue(struct request_queue *q) 570 { 571 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) { 572 kobject_get(&q->kobj); 573 return 0; 574 } 575 576 return 1; 577 } 578 EXPORT_SYMBOL(blk_get_queue); 579 580 static inline void blk_free_request(struct request_queue *q, struct request *rq) 581 { 582 if (rq->cmd_flags & REQ_ELVPRIV) 583 elv_put_request(q, rq); 584 mempool_free(rq, q->rq.rq_pool); 585 } 586 587 static struct request * 588 blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask) 589 { 590 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask); 591 592 if (!rq) 593 return NULL; 594 595 /* 596 * first three bits are identical in rq->cmd_flags and bio->bi_rw, 597 * see bio.h and blkdev.h 598 */ 599 rq->cmd_flags = rw | REQ_ALLOCED; 600 601 if (priv) { 602 if (unlikely(elv_set_request(q, rq, gfp_mask))) { 603 mempool_free(rq, q->rq.rq_pool); 604 return NULL; 605 } 606 rq->cmd_flags |= REQ_ELVPRIV; 607 } 608 609 return rq; 610 } 611 612 /* 613 * ioc_batching returns true if the ioc is a valid batching request and 614 * should be given priority access to a request. 615 */ 616 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc) 617 { 618 if (!ioc) 619 return 0; 620 621 /* 622 * Make sure the process is able to allocate at least 1 request 623 * even if the batch times out, otherwise we could theoretically 624 * lose wakeups. 625 */ 626 return ioc->nr_batch_requests == q->nr_batching || 627 (ioc->nr_batch_requests > 0 628 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME)); 629 } 630 631 /* 632 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This 633 * will cause the process to be a "batcher" on all queues in the system. This 634 * is the behaviour we want though - once it gets a wakeup it should be given 635 * a nice run. 636 */ 637 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc) 638 { 639 if (!ioc || ioc_batching(q, ioc)) 640 return; 641 642 ioc->nr_batch_requests = q->nr_batching; 643 ioc->last_waited = jiffies; 644 } 645 646 static void __freed_request(struct request_queue *q, int rw) 647 { 648 struct request_list *rl = &q->rq; 649 650 if (rl->count[rw] < queue_congestion_off_threshold(q)) 651 blk_clear_queue_congested(q, rw); 652 653 if (rl->count[rw] + 1 <= q->nr_requests) { 654 if (waitqueue_active(&rl->wait[rw])) 655 wake_up(&rl->wait[rw]); 656 657 blk_clear_queue_full(q, rw); 658 } 659 } 660 661 /* 662 * A request has just been released. Account for it, update the full and 663 * congestion status, wake up any waiters. Called under q->queue_lock. 664 */ 665 static void freed_request(struct request_queue *q, int rw, int priv) 666 { 667 struct request_list *rl = &q->rq; 668 669 rl->count[rw]--; 670 if (priv) 671 rl->elvpriv--; 672 673 __freed_request(q, rw); 674 675 if (unlikely(rl->starved[rw ^ 1])) 676 __freed_request(q, rw ^ 1); 677 } 678 679 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist) 680 /* 681 * Get a free request, queue_lock must be held. 682 * Returns NULL on failure, with queue_lock held. 683 * Returns !NULL on success, with queue_lock *not held*. 684 */ 685 static struct request *get_request(struct request_queue *q, int rw_flags, 686 struct bio *bio, gfp_t gfp_mask) 687 { 688 struct request *rq = NULL; 689 struct request_list *rl = &q->rq; 690 struct io_context *ioc = NULL; 691 const int rw = rw_flags & 0x01; 692 int may_queue, priv; 693 694 may_queue = elv_may_queue(q, rw_flags); 695 if (may_queue == ELV_MQUEUE_NO) 696 goto rq_starved; 697 698 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) { 699 if (rl->count[rw]+1 >= q->nr_requests) { 700 ioc = current_io_context(GFP_ATOMIC, q->node); 701 /* 702 * The queue will fill after this allocation, so set 703 * it as full, and mark this process as "batching". 704 * This process will be allowed to complete a batch of 705 * requests, others will be blocked. 706 */ 707 if (!blk_queue_full(q, rw)) { 708 ioc_set_batching(q, ioc); 709 blk_set_queue_full(q, rw); 710 } else { 711 if (may_queue != ELV_MQUEUE_MUST 712 && !ioc_batching(q, ioc)) { 713 /* 714 * The queue is full and the allocating 715 * process is not a "batcher", and not 716 * exempted by the IO scheduler 717 */ 718 goto out; 719 } 720 } 721 } 722 blk_set_queue_congested(q, rw); 723 } 724 725 /* 726 * Only allow batching queuers to allocate up to 50% over the defined 727 * limit of requests, otherwise we could have thousands of requests 728 * allocated with any setting of ->nr_requests 729 */ 730 if (rl->count[rw] >= (3 * q->nr_requests / 2)) 731 goto out; 732 733 rl->count[rw]++; 734 rl->starved[rw] = 0; 735 736 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); 737 if (priv) 738 rl->elvpriv++; 739 740 spin_unlock_irq(q->queue_lock); 741 742 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask); 743 if (unlikely(!rq)) { 744 /* 745 * Allocation failed presumably due to memory. Undo anything 746 * we might have messed up. 747 * 748 * Allocating task should really be put onto the front of the 749 * wait queue, but this is pretty rare. 750 */ 751 spin_lock_irq(q->queue_lock); 752 freed_request(q, rw, priv); 753 754 /* 755 * in the very unlikely event that allocation failed and no 756 * requests for this direction was pending, mark us starved 757 * so that freeing of a request in the other direction will 758 * notice us. another possible fix would be to split the 759 * rq mempool into READ and WRITE 760 */ 761 rq_starved: 762 if (unlikely(rl->count[rw] == 0)) 763 rl->starved[rw] = 1; 764 765 goto out; 766 } 767 768 /* 769 * ioc may be NULL here, and ioc_batching will be false. That's 770 * OK, if the queue is under the request limit then requests need 771 * not count toward the nr_batch_requests limit. There will always 772 * be some limit enforced by BLK_BATCH_TIME. 773 */ 774 if (ioc_batching(q, ioc)) 775 ioc->nr_batch_requests--; 776 777 rq_init(q, rq); 778 779 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ); 780 out: 781 return rq; 782 } 783 784 /* 785 * No available requests for this queue, unplug the device and wait for some 786 * requests to become available. 787 * 788 * Called with q->queue_lock held, and returns with it unlocked. 789 */ 790 static struct request *get_request_wait(struct request_queue *q, int rw_flags, 791 struct bio *bio) 792 { 793 const int rw = rw_flags & 0x01; 794 struct request *rq; 795 796 rq = get_request(q, rw_flags, bio, GFP_NOIO); 797 while (!rq) { 798 DEFINE_WAIT(wait); 799 struct request_list *rl = &q->rq; 800 801 prepare_to_wait_exclusive(&rl->wait[rw], &wait, 802 TASK_UNINTERRUPTIBLE); 803 804 rq = get_request(q, rw_flags, bio, GFP_NOIO); 805 806 if (!rq) { 807 struct io_context *ioc; 808 809 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ); 810 811 __generic_unplug_device(q); 812 spin_unlock_irq(q->queue_lock); 813 io_schedule(); 814 815 /* 816 * After sleeping, we become a "batching" process and 817 * will be able to allocate at least one request, and 818 * up to a big batch of them for a small period time. 819 * See ioc_batching, ioc_set_batching 820 */ 821 ioc = current_io_context(GFP_NOIO, q->node); 822 ioc_set_batching(q, ioc); 823 824 spin_lock_irq(q->queue_lock); 825 } 826 finish_wait(&rl->wait[rw], &wait); 827 } 828 829 return rq; 830 } 831 832 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask) 833 { 834 struct request *rq; 835 836 BUG_ON(rw != READ && rw != WRITE); 837 838 spin_lock_irq(q->queue_lock); 839 if (gfp_mask & __GFP_WAIT) { 840 rq = get_request_wait(q, rw, NULL); 841 } else { 842 rq = get_request(q, rw, NULL, gfp_mask); 843 if (!rq) 844 spin_unlock_irq(q->queue_lock); 845 } 846 /* q->queue_lock is unlocked at this point */ 847 848 return rq; 849 } 850 EXPORT_SYMBOL(blk_get_request); 851 852 /** 853 * blk_start_queueing - initiate dispatch of requests to device 854 * @q: request queue to kick into gear 855 * 856 * This is basically a helper to remove the need to know whether a queue 857 * is plugged or not if someone just wants to initiate dispatch of requests 858 * for this queue. 859 * 860 * The queue lock must be held with interrupts disabled. 861 */ 862 void blk_start_queueing(struct request_queue *q) 863 { 864 if (!blk_queue_plugged(q)) 865 q->request_fn(q); 866 else 867 __generic_unplug_device(q); 868 } 869 EXPORT_SYMBOL(blk_start_queueing); 870 871 /** 872 * blk_requeue_request - put a request back on queue 873 * @q: request queue where request should be inserted 874 * @rq: request to be inserted 875 * 876 * Description: 877 * Drivers often keep queueing requests until the hardware cannot accept 878 * more, when that condition happens we need to put the request back 879 * on the queue. Must be called with queue lock held. 880 */ 881 void blk_requeue_request(struct request_queue *q, struct request *rq) 882 { 883 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE); 884 885 if (blk_rq_tagged(rq)) 886 blk_queue_end_tag(q, rq); 887 888 elv_requeue_request(q, rq); 889 } 890 EXPORT_SYMBOL(blk_requeue_request); 891 892 /** 893 * blk_insert_request - insert a special request in to a request queue 894 * @q: request queue where request should be inserted 895 * @rq: request to be inserted 896 * @at_head: insert request at head or tail of queue 897 * @data: private data 898 * 899 * Description: 900 * Many block devices need to execute commands asynchronously, so they don't 901 * block the whole kernel from preemption during request execution. This is 902 * accomplished normally by inserting aritficial requests tagged as 903 * REQ_SPECIAL in to the corresponding request queue, and letting them be 904 * scheduled for actual execution by the request queue. 905 * 906 * We have the option of inserting the head or the tail of the queue. 907 * Typically we use the tail for new ioctls and so forth. We use the head 908 * of the queue for things like a QUEUE_FULL message from a device, or a 909 * host that is unable to accept a particular command. 910 */ 911 void blk_insert_request(struct request_queue *q, struct request *rq, 912 int at_head, void *data) 913 { 914 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK; 915 unsigned long flags; 916 917 /* 918 * tell I/O scheduler that this isn't a regular read/write (ie it 919 * must not attempt merges on this) and that it acts as a soft 920 * barrier 921 */ 922 rq->cmd_type = REQ_TYPE_SPECIAL; 923 rq->cmd_flags |= REQ_SOFTBARRIER; 924 925 rq->special = data; 926 927 spin_lock_irqsave(q->queue_lock, flags); 928 929 /* 930 * If command is tagged, release the tag 931 */ 932 if (blk_rq_tagged(rq)) 933 blk_queue_end_tag(q, rq); 934 935 drive_stat_acct(rq, 1); 936 __elv_add_request(q, rq, where, 0); 937 blk_start_queueing(q); 938 spin_unlock_irqrestore(q->queue_lock, flags); 939 } 940 EXPORT_SYMBOL(blk_insert_request); 941 942 /* 943 * add-request adds a request to the linked list. 944 * queue lock is held and interrupts disabled, as we muck with the 945 * request queue list. 946 */ 947 static inline void add_request(struct request_queue *q, struct request *req) 948 { 949 drive_stat_acct(req, 1); 950 951 /* 952 * elevator indicated where it wants this request to be 953 * inserted at elevator_merge time 954 */ 955 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0); 956 } 957 958 /* 959 * disk_round_stats() - Round off the performance stats on a struct 960 * disk_stats. 961 * 962 * The average IO queue length and utilisation statistics are maintained 963 * by observing the current state of the queue length and the amount of 964 * time it has been in this state for. 965 * 966 * Normally, that accounting is done on IO completion, but that can result 967 * in more than a second's worth of IO being accounted for within any one 968 * second, leading to >100% utilisation. To deal with that, we call this 969 * function to do a round-off before returning the results when reading 970 * /proc/diskstats. This accounts immediately for all queue usage up to 971 * the current jiffies and restarts the counters again. 972 */ 973 void disk_round_stats(struct gendisk *disk) 974 { 975 unsigned long now = jiffies; 976 977 if (now == disk->stamp) 978 return; 979 980 if (disk->in_flight) { 981 __disk_stat_add(disk, time_in_queue, 982 disk->in_flight * (now - disk->stamp)); 983 __disk_stat_add(disk, io_ticks, (now - disk->stamp)); 984 } 985 disk->stamp = now; 986 } 987 EXPORT_SYMBOL_GPL(disk_round_stats); 988 989 /* 990 * queue lock must be held 991 */ 992 void __blk_put_request(struct request_queue *q, struct request *req) 993 { 994 if (unlikely(!q)) 995 return; 996 if (unlikely(--req->ref_count)) 997 return; 998 999 elv_completed_request(q, req); 1000 1001 /* 1002 * Request may not have originated from ll_rw_blk. if not, 1003 * it didn't come out of our reserved rq pools 1004 */ 1005 if (req->cmd_flags & REQ_ALLOCED) { 1006 int rw = rq_data_dir(req); 1007 int priv = req->cmd_flags & REQ_ELVPRIV; 1008 1009 BUG_ON(!list_empty(&req->queuelist)); 1010 BUG_ON(!hlist_unhashed(&req->hash)); 1011 1012 blk_free_request(q, req); 1013 freed_request(q, rw, priv); 1014 } 1015 } 1016 EXPORT_SYMBOL_GPL(__blk_put_request); 1017 1018 void blk_put_request(struct request *req) 1019 { 1020 unsigned long flags; 1021 struct request_queue *q = req->q; 1022 1023 /* 1024 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the 1025 * following if (q) test. 1026 */ 1027 if (q) { 1028 spin_lock_irqsave(q->queue_lock, flags); 1029 __blk_put_request(q, req); 1030 spin_unlock_irqrestore(q->queue_lock, flags); 1031 } 1032 } 1033 EXPORT_SYMBOL(blk_put_request); 1034 1035 void init_request_from_bio(struct request *req, struct bio *bio) 1036 { 1037 req->cmd_type = REQ_TYPE_FS; 1038 1039 /* 1040 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST) 1041 */ 1042 if (bio_rw_ahead(bio) || bio_failfast(bio)) 1043 req->cmd_flags |= REQ_FAILFAST; 1044 1045 /* 1046 * REQ_BARRIER implies no merging, but lets make it explicit 1047 */ 1048 if (unlikely(bio_barrier(bio))) 1049 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE); 1050 1051 if (bio_sync(bio)) 1052 req->cmd_flags |= REQ_RW_SYNC; 1053 if (bio_rw_meta(bio)) 1054 req->cmd_flags |= REQ_RW_META; 1055 1056 req->errors = 0; 1057 req->hard_sector = req->sector = bio->bi_sector; 1058 req->ioprio = bio_prio(bio); 1059 req->start_time = jiffies; 1060 blk_rq_bio_prep(req->q, req, bio); 1061 } 1062 1063 static int __make_request(struct request_queue *q, struct bio *bio) 1064 { 1065 struct request *req; 1066 int el_ret, nr_sectors, barrier, err; 1067 const unsigned short prio = bio_prio(bio); 1068 const int sync = bio_sync(bio); 1069 int rw_flags; 1070 1071 nr_sectors = bio_sectors(bio); 1072 1073 /* 1074 * low level driver can indicate that it wants pages above a 1075 * certain limit bounced to low memory (ie for highmem, or even 1076 * ISA dma in theory) 1077 */ 1078 blk_queue_bounce(q, &bio); 1079 1080 barrier = bio_barrier(bio); 1081 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) { 1082 err = -EOPNOTSUPP; 1083 goto end_io; 1084 } 1085 1086 spin_lock_irq(q->queue_lock); 1087 1088 if (unlikely(barrier) || elv_queue_empty(q)) 1089 goto get_rq; 1090 1091 el_ret = elv_merge(q, &req, bio); 1092 switch (el_ret) { 1093 case ELEVATOR_BACK_MERGE: 1094 BUG_ON(!rq_mergeable(req)); 1095 1096 if (!ll_back_merge_fn(q, req, bio)) 1097 break; 1098 1099 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE); 1100 1101 req->biotail->bi_next = bio; 1102 req->biotail = bio; 1103 req->nr_sectors = req->hard_nr_sectors += nr_sectors; 1104 req->ioprio = ioprio_best(req->ioprio, prio); 1105 drive_stat_acct(req, 0); 1106 if (!attempt_back_merge(q, req)) 1107 elv_merged_request(q, req, el_ret); 1108 goto out; 1109 1110 case ELEVATOR_FRONT_MERGE: 1111 BUG_ON(!rq_mergeable(req)); 1112 1113 if (!ll_front_merge_fn(q, req, bio)) 1114 break; 1115 1116 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE); 1117 1118 bio->bi_next = req->bio; 1119 req->bio = bio; 1120 1121 /* 1122 * may not be valid. if the low level driver said 1123 * it didn't need a bounce buffer then it better 1124 * not touch req->buffer either... 1125 */ 1126 req->buffer = bio_data(bio); 1127 req->current_nr_sectors = bio_cur_sectors(bio); 1128 req->hard_cur_sectors = req->current_nr_sectors; 1129 req->sector = req->hard_sector = bio->bi_sector; 1130 req->nr_sectors = req->hard_nr_sectors += nr_sectors; 1131 req->ioprio = ioprio_best(req->ioprio, prio); 1132 drive_stat_acct(req, 0); 1133 if (!attempt_front_merge(q, req)) 1134 elv_merged_request(q, req, el_ret); 1135 goto out; 1136 1137 /* ELV_NO_MERGE: elevator says don't/can't merge. */ 1138 default: 1139 ; 1140 } 1141 1142 get_rq: 1143 /* 1144 * This sync check and mask will be re-done in init_request_from_bio(), 1145 * but we need to set it earlier to expose the sync flag to the 1146 * rq allocator and io schedulers. 1147 */ 1148 rw_flags = bio_data_dir(bio); 1149 if (sync) 1150 rw_flags |= REQ_RW_SYNC; 1151 1152 /* 1153 * Grab a free request. This is might sleep but can not fail. 1154 * Returns with the queue unlocked. 1155 */ 1156 req = get_request_wait(q, rw_flags, bio); 1157 1158 /* 1159 * After dropping the lock and possibly sleeping here, our request 1160 * may now be mergeable after it had proven unmergeable (above). 1161 * We don't worry about that case for efficiency. It won't happen 1162 * often, and the elevators are able to handle it. 1163 */ 1164 init_request_from_bio(req, bio); 1165 1166 spin_lock_irq(q->queue_lock); 1167 if (elv_queue_empty(q)) 1168 blk_plug_device(q); 1169 add_request(q, req); 1170 out: 1171 if (sync) 1172 __generic_unplug_device(q); 1173 1174 spin_unlock_irq(q->queue_lock); 1175 return 0; 1176 1177 end_io: 1178 bio_endio(bio, err); 1179 return 0; 1180 } 1181 1182 /* 1183 * If bio->bi_dev is a partition, remap the location 1184 */ 1185 static inline void blk_partition_remap(struct bio *bio) 1186 { 1187 struct block_device *bdev = bio->bi_bdev; 1188 1189 if (bio_sectors(bio) && bdev != bdev->bd_contains) { 1190 struct hd_struct *p = bdev->bd_part; 1191 const int rw = bio_data_dir(bio); 1192 1193 p->sectors[rw] += bio_sectors(bio); 1194 p->ios[rw]++; 1195 1196 bio->bi_sector += p->start_sect; 1197 bio->bi_bdev = bdev->bd_contains; 1198 1199 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio, 1200 bdev->bd_dev, bio->bi_sector, 1201 bio->bi_sector - p->start_sect); 1202 } 1203 } 1204 1205 static void handle_bad_sector(struct bio *bio) 1206 { 1207 char b[BDEVNAME_SIZE]; 1208 1209 printk(KERN_INFO "attempt to access beyond end of device\n"); 1210 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n", 1211 bdevname(bio->bi_bdev, b), 1212 bio->bi_rw, 1213 (unsigned long long)bio->bi_sector + bio_sectors(bio), 1214 (long long)(bio->bi_bdev->bd_inode->i_size >> 9)); 1215 1216 set_bit(BIO_EOF, &bio->bi_flags); 1217 } 1218 1219 #ifdef CONFIG_FAIL_MAKE_REQUEST 1220 1221 static DECLARE_FAULT_ATTR(fail_make_request); 1222 1223 static int __init setup_fail_make_request(char *str) 1224 { 1225 return setup_fault_attr(&fail_make_request, str); 1226 } 1227 __setup("fail_make_request=", setup_fail_make_request); 1228 1229 static int should_fail_request(struct bio *bio) 1230 { 1231 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) || 1232 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail)) 1233 return should_fail(&fail_make_request, bio->bi_size); 1234 1235 return 0; 1236 } 1237 1238 static int __init fail_make_request_debugfs(void) 1239 { 1240 return init_fault_attr_dentries(&fail_make_request, 1241 "fail_make_request"); 1242 } 1243 1244 late_initcall(fail_make_request_debugfs); 1245 1246 #else /* CONFIG_FAIL_MAKE_REQUEST */ 1247 1248 static inline int should_fail_request(struct bio *bio) 1249 { 1250 return 0; 1251 } 1252 1253 #endif /* CONFIG_FAIL_MAKE_REQUEST */ 1254 1255 /* 1256 * Check whether this bio extends beyond the end of the device. 1257 */ 1258 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors) 1259 { 1260 sector_t maxsector; 1261 1262 if (!nr_sectors) 1263 return 0; 1264 1265 /* Test device or partition size, when known. */ 1266 maxsector = bio->bi_bdev->bd_inode->i_size >> 9; 1267 if (maxsector) { 1268 sector_t sector = bio->bi_sector; 1269 1270 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) { 1271 /* 1272 * This may well happen - the kernel calls bread() 1273 * without checking the size of the device, e.g., when 1274 * mounting a device. 1275 */ 1276 handle_bad_sector(bio); 1277 return 1; 1278 } 1279 } 1280 1281 return 0; 1282 } 1283 1284 /** 1285 * generic_make_request: hand a buffer to its device driver for I/O 1286 * @bio: The bio describing the location in memory and on the device. 1287 * 1288 * generic_make_request() is used to make I/O requests of block 1289 * devices. It is passed a &struct bio, which describes the I/O that needs 1290 * to be done. 1291 * 1292 * generic_make_request() does not return any status. The 1293 * success/failure status of the request, along with notification of 1294 * completion, is delivered asynchronously through the bio->bi_end_io 1295 * function described (one day) else where. 1296 * 1297 * The caller of generic_make_request must make sure that bi_io_vec 1298 * are set to describe the memory buffer, and that bi_dev and bi_sector are 1299 * set to describe the device address, and the 1300 * bi_end_io and optionally bi_private are set to describe how 1301 * completion notification should be signaled. 1302 * 1303 * generic_make_request and the drivers it calls may use bi_next if this 1304 * bio happens to be merged with someone else, and may change bi_dev and 1305 * bi_sector for remaps as it sees fit. So the values of these fields 1306 * should NOT be depended on after the call to generic_make_request. 1307 */ 1308 static inline void __generic_make_request(struct bio *bio) 1309 { 1310 struct request_queue *q; 1311 sector_t old_sector; 1312 int ret, nr_sectors = bio_sectors(bio); 1313 dev_t old_dev; 1314 int err = -EIO; 1315 1316 might_sleep(); 1317 1318 if (bio_check_eod(bio, nr_sectors)) 1319 goto end_io; 1320 1321 /* 1322 * Resolve the mapping until finished. (drivers are 1323 * still free to implement/resolve their own stacking 1324 * by explicitly returning 0) 1325 * 1326 * NOTE: we don't repeat the blk_size check for each new device. 1327 * Stacking drivers are expected to know what they are doing. 1328 */ 1329 old_sector = -1; 1330 old_dev = 0; 1331 do { 1332 char b[BDEVNAME_SIZE]; 1333 1334 q = bdev_get_queue(bio->bi_bdev); 1335 if (!q) { 1336 printk(KERN_ERR 1337 "generic_make_request: Trying to access " 1338 "nonexistent block-device %s (%Lu)\n", 1339 bdevname(bio->bi_bdev, b), 1340 (long long) bio->bi_sector); 1341 end_io: 1342 bio_endio(bio, err); 1343 break; 1344 } 1345 1346 if (unlikely(nr_sectors > q->max_hw_sectors)) { 1347 printk(KERN_ERR "bio too big device %s (%u > %u)\n", 1348 bdevname(bio->bi_bdev, b), 1349 bio_sectors(bio), 1350 q->max_hw_sectors); 1351 goto end_io; 1352 } 1353 1354 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) 1355 goto end_io; 1356 1357 if (should_fail_request(bio)) 1358 goto end_io; 1359 1360 /* 1361 * If this device has partitions, remap block n 1362 * of partition p to block n+start(p) of the disk. 1363 */ 1364 blk_partition_remap(bio); 1365 1366 if (old_sector != -1) 1367 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector, 1368 old_sector); 1369 1370 blk_add_trace_bio(q, bio, BLK_TA_QUEUE); 1371 1372 old_sector = bio->bi_sector; 1373 old_dev = bio->bi_bdev->bd_dev; 1374 1375 if (bio_check_eod(bio, nr_sectors)) 1376 goto end_io; 1377 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) { 1378 err = -EOPNOTSUPP; 1379 goto end_io; 1380 } 1381 1382 ret = q->make_request_fn(q, bio); 1383 } while (ret); 1384 } 1385 1386 /* 1387 * We only want one ->make_request_fn to be active at a time, 1388 * else stack usage with stacked devices could be a problem. 1389 * So use current->bio_{list,tail} to keep a list of requests 1390 * submited by a make_request_fn function. 1391 * current->bio_tail is also used as a flag to say if 1392 * generic_make_request is currently active in this task or not. 1393 * If it is NULL, then no make_request is active. If it is non-NULL, 1394 * then a make_request is active, and new requests should be added 1395 * at the tail 1396 */ 1397 void generic_make_request(struct bio *bio) 1398 { 1399 if (current->bio_tail) { 1400 /* make_request is active */ 1401 *(current->bio_tail) = bio; 1402 bio->bi_next = NULL; 1403 current->bio_tail = &bio->bi_next; 1404 return; 1405 } 1406 /* following loop may be a bit non-obvious, and so deserves some 1407 * explanation. 1408 * Before entering the loop, bio->bi_next is NULL (as all callers 1409 * ensure that) so we have a list with a single bio. 1410 * We pretend that we have just taken it off a longer list, so 1411 * we assign bio_list to the next (which is NULL) and bio_tail 1412 * to &bio_list, thus initialising the bio_list of new bios to be 1413 * added. __generic_make_request may indeed add some more bios 1414 * through a recursive call to generic_make_request. If it 1415 * did, we find a non-NULL value in bio_list and re-enter the loop 1416 * from the top. In this case we really did just take the bio 1417 * of the top of the list (no pretending) and so fixup bio_list and 1418 * bio_tail or bi_next, and call into __generic_make_request again. 1419 * 1420 * The loop was structured like this to make only one call to 1421 * __generic_make_request (which is important as it is large and 1422 * inlined) and to keep the structure simple. 1423 */ 1424 BUG_ON(bio->bi_next); 1425 do { 1426 current->bio_list = bio->bi_next; 1427 if (bio->bi_next == NULL) 1428 current->bio_tail = ¤t->bio_list; 1429 else 1430 bio->bi_next = NULL; 1431 __generic_make_request(bio); 1432 bio = current->bio_list; 1433 } while (bio); 1434 current->bio_tail = NULL; /* deactivate */ 1435 } 1436 EXPORT_SYMBOL(generic_make_request); 1437 1438 /** 1439 * submit_bio: submit a bio to the block device layer for I/O 1440 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead) 1441 * @bio: The &struct bio which describes the I/O 1442 * 1443 * submit_bio() is very similar in purpose to generic_make_request(), and 1444 * uses that function to do most of the work. Both are fairly rough 1445 * interfaces, @bio must be presetup and ready for I/O. 1446 * 1447 */ 1448 void submit_bio(int rw, struct bio *bio) 1449 { 1450 int count = bio_sectors(bio); 1451 1452 bio->bi_rw |= rw; 1453 1454 /* 1455 * If it's a regular read/write or a barrier with data attached, 1456 * go through the normal accounting stuff before submission. 1457 */ 1458 if (!bio_empty_barrier(bio)) { 1459 1460 BIO_BUG_ON(!bio->bi_size); 1461 BIO_BUG_ON(!bio->bi_io_vec); 1462 1463 if (rw & WRITE) { 1464 count_vm_events(PGPGOUT, count); 1465 } else { 1466 task_io_account_read(bio->bi_size); 1467 count_vm_events(PGPGIN, count); 1468 } 1469 1470 if (unlikely(block_dump)) { 1471 char b[BDEVNAME_SIZE]; 1472 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n", 1473 current->comm, task_pid_nr(current), 1474 (rw & WRITE) ? "WRITE" : "READ", 1475 (unsigned long long)bio->bi_sector, 1476 bdevname(bio->bi_bdev, b)); 1477 } 1478 } 1479 1480 generic_make_request(bio); 1481 } 1482 EXPORT_SYMBOL(submit_bio); 1483 1484 /** 1485 * __end_that_request_first - end I/O on a request 1486 * @req: the request being processed 1487 * @error: 0 for success, < 0 for error 1488 * @nr_bytes: number of bytes to complete 1489 * 1490 * Description: 1491 * Ends I/O on a number of bytes attached to @req, and sets it up 1492 * for the next range of segments (if any) in the cluster. 1493 * 1494 * Return: 1495 * 0 - we are done with this request, call end_that_request_last() 1496 * 1 - still buffers pending for this request 1497 **/ 1498 static int __end_that_request_first(struct request *req, int error, 1499 int nr_bytes) 1500 { 1501 int total_bytes, bio_nbytes, next_idx = 0; 1502 struct bio *bio; 1503 1504 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE); 1505 1506 /* 1507 * for a REQ_BLOCK_PC request, we want to carry any eventual 1508 * sense key with us all the way through 1509 */ 1510 if (!blk_pc_request(req)) 1511 req->errors = 0; 1512 1513 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) { 1514 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n", 1515 req->rq_disk ? req->rq_disk->disk_name : "?", 1516 (unsigned long long)req->sector); 1517 } 1518 1519 if (blk_fs_request(req) && req->rq_disk) { 1520 const int rw = rq_data_dir(req); 1521 1522 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9); 1523 } 1524 1525 total_bytes = bio_nbytes = 0; 1526 while ((bio = req->bio) != NULL) { 1527 int nbytes; 1528 1529 /* 1530 * For an empty barrier request, the low level driver must 1531 * store a potential error location in ->sector. We pass 1532 * that back up in ->bi_sector. 1533 */ 1534 if (blk_empty_barrier(req)) 1535 bio->bi_sector = req->sector; 1536 1537 if (nr_bytes >= bio->bi_size) { 1538 req->bio = bio->bi_next; 1539 nbytes = bio->bi_size; 1540 req_bio_endio(req, bio, nbytes, error); 1541 next_idx = 0; 1542 bio_nbytes = 0; 1543 } else { 1544 int idx = bio->bi_idx + next_idx; 1545 1546 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) { 1547 blk_dump_rq_flags(req, "__end_that"); 1548 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n", 1549 __FUNCTION__, bio->bi_idx, 1550 bio->bi_vcnt); 1551 break; 1552 } 1553 1554 nbytes = bio_iovec_idx(bio, idx)->bv_len; 1555 BIO_BUG_ON(nbytes > bio->bi_size); 1556 1557 /* 1558 * not a complete bvec done 1559 */ 1560 if (unlikely(nbytes > nr_bytes)) { 1561 bio_nbytes += nr_bytes; 1562 total_bytes += nr_bytes; 1563 break; 1564 } 1565 1566 /* 1567 * advance to the next vector 1568 */ 1569 next_idx++; 1570 bio_nbytes += nbytes; 1571 } 1572 1573 total_bytes += nbytes; 1574 nr_bytes -= nbytes; 1575 1576 bio = req->bio; 1577 if (bio) { 1578 /* 1579 * end more in this run, or just return 'not-done' 1580 */ 1581 if (unlikely(nr_bytes <= 0)) 1582 break; 1583 } 1584 } 1585 1586 /* 1587 * completely done 1588 */ 1589 if (!req->bio) 1590 return 0; 1591 1592 /* 1593 * if the request wasn't completed, update state 1594 */ 1595 if (bio_nbytes) { 1596 req_bio_endio(req, bio, bio_nbytes, error); 1597 bio->bi_idx += next_idx; 1598 bio_iovec(bio)->bv_offset += nr_bytes; 1599 bio_iovec(bio)->bv_len -= nr_bytes; 1600 } 1601 1602 blk_recalc_rq_sectors(req, total_bytes >> 9); 1603 blk_recalc_rq_segments(req); 1604 return 1; 1605 } 1606 1607 /* 1608 * splice the completion data to a local structure and hand off to 1609 * process_completion_queue() to complete the requests 1610 */ 1611 static void blk_done_softirq(struct softirq_action *h) 1612 { 1613 struct list_head *cpu_list, local_list; 1614 1615 local_irq_disable(); 1616 cpu_list = &__get_cpu_var(blk_cpu_done); 1617 list_replace_init(cpu_list, &local_list); 1618 local_irq_enable(); 1619 1620 while (!list_empty(&local_list)) { 1621 struct request *rq; 1622 1623 rq = list_entry(local_list.next, struct request, donelist); 1624 list_del_init(&rq->donelist); 1625 rq->q->softirq_done_fn(rq); 1626 } 1627 } 1628 1629 static int __cpuinit blk_cpu_notify(struct notifier_block *self, 1630 unsigned long action, void *hcpu) 1631 { 1632 /* 1633 * If a CPU goes away, splice its entries to the current CPU 1634 * and trigger a run of the softirq 1635 */ 1636 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) { 1637 int cpu = (unsigned long) hcpu; 1638 1639 local_irq_disable(); 1640 list_splice_init(&per_cpu(blk_cpu_done, cpu), 1641 &__get_cpu_var(blk_cpu_done)); 1642 raise_softirq_irqoff(BLOCK_SOFTIRQ); 1643 local_irq_enable(); 1644 } 1645 1646 return NOTIFY_OK; 1647 } 1648 1649 1650 static struct notifier_block blk_cpu_notifier __cpuinitdata = { 1651 .notifier_call = blk_cpu_notify, 1652 }; 1653 1654 /** 1655 * blk_complete_request - end I/O on a request 1656 * @req: the request being processed 1657 * 1658 * Description: 1659 * Ends all I/O on a request. It does not handle partial completions, 1660 * unless the driver actually implements this in its completion callback 1661 * through requeueing. The actual completion happens out-of-order, 1662 * through a softirq handler. The user must have registered a completion 1663 * callback through blk_queue_softirq_done(). 1664 **/ 1665 1666 void blk_complete_request(struct request *req) 1667 { 1668 struct list_head *cpu_list; 1669 unsigned long flags; 1670 1671 BUG_ON(!req->q->softirq_done_fn); 1672 1673 local_irq_save(flags); 1674 1675 cpu_list = &__get_cpu_var(blk_cpu_done); 1676 list_add_tail(&req->donelist, cpu_list); 1677 raise_softirq_irqoff(BLOCK_SOFTIRQ); 1678 1679 local_irq_restore(flags); 1680 } 1681 EXPORT_SYMBOL(blk_complete_request); 1682 1683 /* 1684 * queue lock must be held 1685 */ 1686 static void end_that_request_last(struct request *req, int error) 1687 { 1688 struct gendisk *disk = req->rq_disk; 1689 1690 if (blk_rq_tagged(req)) 1691 blk_queue_end_tag(req->q, req); 1692 1693 if (blk_queued_rq(req)) 1694 blkdev_dequeue_request(req); 1695 1696 if (unlikely(laptop_mode) && blk_fs_request(req)) 1697 laptop_io_completion(); 1698 1699 /* 1700 * Account IO completion. bar_rq isn't accounted as a normal 1701 * IO on queueing nor completion. Accounting the containing 1702 * request is enough. 1703 */ 1704 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) { 1705 unsigned long duration = jiffies - req->start_time; 1706 const int rw = rq_data_dir(req); 1707 1708 __disk_stat_inc(disk, ios[rw]); 1709 __disk_stat_add(disk, ticks[rw], duration); 1710 disk_round_stats(disk); 1711 disk->in_flight--; 1712 } 1713 1714 if (req->end_io) 1715 req->end_io(req, error); 1716 else { 1717 if (blk_bidi_rq(req)) 1718 __blk_put_request(req->next_rq->q, req->next_rq); 1719 1720 __blk_put_request(req->q, req); 1721 } 1722 } 1723 1724 static inline void __end_request(struct request *rq, int uptodate, 1725 unsigned int nr_bytes) 1726 { 1727 int error = 0; 1728 1729 if (uptodate <= 0) 1730 error = uptodate ? uptodate : -EIO; 1731 1732 __blk_end_request(rq, error, nr_bytes); 1733 } 1734 1735 /** 1736 * blk_rq_bytes - Returns bytes left to complete in the entire request 1737 **/ 1738 unsigned int blk_rq_bytes(struct request *rq) 1739 { 1740 if (blk_fs_request(rq)) 1741 return rq->hard_nr_sectors << 9; 1742 1743 return rq->data_len; 1744 } 1745 EXPORT_SYMBOL_GPL(blk_rq_bytes); 1746 1747 /** 1748 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment 1749 **/ 1750 unsigned int blk_rq_cur_bytes(struct request *rq) 1751 { 1752 if (blk_fs_request(rq)) 1753 return rq->current_nr_sectors << 9; 1754 1755 if (rq->bio) 1756 return rq->bio->bi_size; 1757 1758 return rq->data_len; 1759 } 1760 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes); 1761 1762 /** 1763 * end_queued_request - end all I/O on a queued request 1764 * @rq: the request being processed 1765 * @uptodate: error value or 0/1 uptodate flag 1766 * 1767 * Description: 1768 * Ends all I/O on a request, and removes it from the block layer queues. 1769 * Not suitable for normal IO completion, unless the driver still has 1770 * the request attached to the block layer. 1771 * 1772 **/ 1773 void end_queued_request(struct request *rq, int uptodate) 1774 { 1775 __end_request(rq, uptodate, blk_rq_bytes(rq)); 1776 } 1777 EXPORT_SYMBOL(end_queued_request); 1778 1779 /** 1780 * end_dequeued_request - end all I/O on a dequeued request 1781 * @rq: the request being processed 1782 * @uptodate: error value or 0/1 uptodate flag 1783 * 1784 * Description: 1785 * Ends all I/O on a request. The request must already have been 1786 * dequeued using blkdev_dequeue_request(), as is normally the case 1787 * for most drivers. 1788 * 1789 **/ 1790 void end_dequeued_request(struct request *rq, int uptodate) 1791 { 1792 __end_request(rq, uptodate, blk_rq_bytes(rq)); 1793 } 1794 EXPORT_SYMBOL(end_dequeued_request); 1795 1796 1797 /** 1798 * end_request - end I/O on the current segment of the request 1799 * @req: the request being processed 1800 * @uptodate: error value or 0/1 uptodate flag 1801 * 1802 * Description: 1803 * Ends I/O on the current segment of a request. If that is the only 1804 * remaining segment, the request is also completed and freed. 1805 * 1806 * This is a remnant of how older block drivers handled IO completions. 1807 * Modern drivers typically end IO on the full request in one go, unless 1808 * they have a residual value to account for. For that case this function 1809 * isn't really useful, unless the residual just happens to be the 1810 * full current segment. In other words, don't use this function in new 1811 * code. Either use end_request_completely(), or the 1812 * end_that_request_chunk() (along with end_that_request_last()) for 1813 * partial completions. 1814 * 1815 **/ 1816 void end_request(struct request *req, int uptodate) 1817 { 1818 __end_request(req, uptodate, req->hard_cur_sectors << 9); 1819 } 1820 EXPORT_SYMBOL(end_request); 1821 1822 /** 1823 * blk_end_io - Generic end_io function to complete a request. 1824 * @rq: the request being processed 1825 * @error: 0 for success, < 0 for error 1826 * @nr_bytes: number of bytes to complete @rq 1827 * @bidi_bytes: number of bytes to complete @rq->next_rq 1828 * @drv_callback: function called between completion of bios in the request 1829 * and completion of the request. 1830 * If the callback returns non 0, this helper returns without 1831 * completion of the request. 1832 * 1833 * Description: 1834 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq. 1835 * If @rq has leftover, sets it up for the next range of segments. 1836 * 1837 * Return: 1838 * 0 - we are done with this request 1839 * 1 - this request is not freed yet, it still has pending buffers. 1840 **/ 1841 static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes, 1842 unsigned int bidi_bytes, 1843 int (drv_callback)(struct request *)) 1844 { 1845 struct request_queue *q = rq->q; 1846 unsigned long flags = 0UL; 1847 1848 if (blk_fs_request(rq) || blk_pc_request(rq)) { 1849 if (__end_that_request_first(rq, error, nr_bytes)) 1850 return 1; 1851 1852 /* Bidi request must be completed as a whole */ 1853 if (blk_bidi_rq(rq) && 1854 __end_that_request_first(rq->next_rq, error, bidi_bytes)) 1855 return 1; 1856 } 1857 1858 /* Special feature for tricky drivers */ 1859 if (drv_callback && drv_callback(rq)) 1860 return 1; 1861 1862 add_disk_randomness(rq->rq_disk); 1863 1864 spin_lock_irqsave(q->queue_lock, flags); 1865 end_that_request_last(rq, error); 1866 spin_unlock_irqrestore(q->queue_lock, flags); 1867 1868 return 0; 1869 } 1870 1871 /** 1872 * blk_end_request - Helper function for drivers to complete the request. 1873 * @rq: the request being processed 1874 * @error: 0 for success, < 0 for error 1875 * @nr_bytes: number of bytes to complete 1876 * 1877 * Description: 1878 * Ends I/O on a number of bytes attached to @rq. 1879 * If @rq has leftover, sets it up for the next range of segments. 1880 * 1881 * Return: 1882 * 0 - we are done with this request 1883 * 1 - still buffers pending for this request 1884 **/ 1885 int blk_end_request(struct request *rq, int error, unsigned int nr_bytes) 1886 { 1887 return blk_end_io(rq, error, nr_bytes, 0, NULL); 1888 } 1889 EXPORT_SYMBOL_GPL(blk_end_request); 1890 1891 /** 1892 * __blk_end_request - Helper function for drivers to complete the request. 1893 * @rq: the request being processed 1894 * @error: 0 for success, < 0 for error 1895 * @nr_bytes: number of bytes to complete 1896 * 1897 * Description: 1898 * Must be called with queue lock held unlike blk_end_request(). 1899 * 1900 * Return: 1901 * 0 - we are done with this request 1902 * 1 - still buffers pending for this request 1903 **/ 1904 int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes) 1905 { 1906 if (blk_fs_request(rq) || blk_pc_request(rq)) { 1907 if (__end_that_request_first(rq, error, nr_bytes)) 1908 return 1; 1909 } 1910 1911 add_disk_randomness(rq->rq_disk); 1912 1913 end_that_request_last(rq, error); 1914 1915 return 0; 1916 } 1917 EXPORT_SYMBOL_GPL(__blk_end_request); 1918 1919 /** 1920 * blk_end_bidi_request - Helper function for drivers to complete bidi request. 1921 * @rq: the bidi request being processed 1922 * @error: 0 for success, < 0 for error 1923 * @nr_bytes: number of bytes to complete @rq 1924 * @bidi_bytes: number of bytes to complete @rq->next_rq 1925 * 1926 * Description: 1927 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq. 1928 * 1929 * Return: 1930 * 0 - we are done with this request 1931 * 1 - still buffers pending for this request 1932 **/ 1933 int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes, 1934 unsigned int bidi_bytes) 1935 { 1936 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL); 1937 } 1938 EXPORT_SYMBOL_GPL(blk_end_bidi_request); 1939 1940 /** 1941 * blk_end_request_callback - Special helper function for tricky drivers 1942 * @rq: the request being processed 1943 * @error: 0 for success, < 0 for error 1944 * @nr_bytes: number of bytes to complete 1945 * @drv_callback: function called between completion of bios in the request 1946 * and completion of the request. 1947 * If the callback returns non 0, this helper returns without 1948 * completion of the request. 1949 * 1950 * Description: 1951 * Ends I/O on a number of bytes attached to @rq. 1952 * If @rq has leftover, sets it up for the next range of segments. 1953 * 1954 * This special helper function is used only for existing tricky drivers. 1955 * (e.g. cdrom_newpc_intr() of ide-cd) 1956 * This interface will be removed when such drivers are rewritten. 1957 * Don't use this interface in other places anymore. 1958 * 1959 * Return: 1960 * 0 - we are done with this request 1961 * 1 - this request is not freed yet. 1962 * this request still has pending buffers or 1963 * the driver doesn't want to finish this request yet. 1964 **/ 1965 int blk_end_request_callback(struct request *rq, int error, 1966 unsigned int nr_bytes, 1967 int (drv_callback)(struct request *)) 1968 { 1969 return blk_end_io(rq, error, nr_bytes, 0, drv_callback); 1970 } 1971 EXPORT_SYMBOL_GPL(blk_end_request_callback); 1972 1973 void blk_rq_bio_prep(struct request_queue *q, struct request *rq, 1974 struct bio *bio) 1975 { 1976 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */ 1977 rq->cmd_flags |= (bio->bi_rw & 3); 1978 1979 rq->nr_phys_segments = bio_phys_segments(q, bio); 1980 rq->nr_hw_segments = bio_hw_segments(q, bio); 1981 rq->current_nr_sectors = bio_cur_sectors(bio); 1982 rq->hard_cur_sectors = rq->current_nr_sectors; 1983 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio); 1984 rq->buffer = bio_data(bio); 1985 rq->data_len = bio->bi_size; 1986 1987 rq->bio = rq->biotail = bio; 1988 1989 if (bio->bi_bdev) 1990 rq->rq_disk = bio->bi_bdev->bd_disk; 1991 } 1992 1993 int kblockd_schedule_work(struct work_struct *work) 1994 { 1995 return queue_work(kblockd_workqueue, work); 1996 } 1997 EXPORT_SYMBOL(kblockd_schedule_work); 1998 1999 void kblockd_flush_work(struct work_struct *work) 2000 { 2001 cancel_work_sync(work); 2002 } 2003 EXPORT_SYMBOL(kblockd_flush_work); 2004 2005 int __init blk_dev_init(void) 2006 { 2007 int i; 2008 2009 kblockd_workqueue = create_workqueue("kblockd"); 2010 if (!kblockd_workqueue) 2011 panic("Failed to create kblockd\n"); 2012 2013 request_cachep = kmem_cache_create("blkdev_requests", 2014 sizeof(struct request), 0, SLAB_PANIC, NULL); 2015 2016 blk_requestq_cachep = kmem_cache_create("blkdev_queue", 2017 sizeof(struct request_queue), 0, SLAB_PANIC, NULL); 2018 2019 for_each_possible_cpu(i) 2020 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i)); 2021 2022 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL); 2023 register_hotcpu_notifier(&blk_cpu_notifier); 2024 2025 return 0; 2026 } 2027 2028