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 1334 /* Foxconn modified start pling 07/31/2012 */ 1335 /* WNDR4500v2 Mantis 2704: fix a potential null pointer bug */ 1336 /* int nbytes = bvec->bv_len; */ 1337 int nbytes; 1338 if (!bvec) 1339 continue; 1340 else 1341 nbytes = bvec->bv_len; 1342 /* Foxconn modified end pling 07/31/2012 */ 1343 1344 if (bvprv && cluster) { 1345 if (sg[nsegs - 1].length + nbytes > q->max_segment_size) 1346 goto new_segment; 1347 1348 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 1349 goto new_segment; 1350 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 1351 goto new_segment; 1352 1353 sg[nsegs - 1].length += nbytes; 1354 } else { 1355new_segment: 1356 memset(&sg[nsegs],0,sizeof(struct scatterlist)); 1357 sg[nsegs].page = bvec->bv_page; 1358 sg[nsegs].length = nbytes; 1359 sg[nsegs].offset = bvec->bv_offset; 1360 1361 nsegs++; 1362 } 1363 bvprv = bvec; 1364 } /* segments in bio */ 1365 } /* bios in rq */ 1366 1367 return nsegs; 1368} 1369 1370EXPORT_SYMBOL(blk_rq_map_sg); 1371 1372/* 1373 * the standard queue merge functions, can be overridden with device 1374 * specific ones if so desired 1375 */ 1376 1377static inline int ll_new_mergeable(request_queue_t *q, 1378 struct request *req, 1379 struct bio *bio) 1380{ 1381 int nr_phys_segs = bio_phys_segments(q, bio); 1382 1383 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { 1384 req->cmd_flags |= REQ_NOMERGE; 1385 if (req == q->last_merge) 1386 q->last_merge = NULL; 1387 return 0; 1388 } 1389 1390 /* 1391 * A hw segment is just getting larger, bump just the phys 1392 * counter. 1393 */ 1394 req->nr_phys_segments += nr_phys_segs; 1395 return 1; 1396} 1397 1398static inline int ll_new_hw_segment(request_queue_t *q, 1399 struct request *req, 1400 struct bio *bio) 1401{ 1402 int nr_hw_segs = bio_hw_segments(q, bio); 1403 int nr_phys_segs = bio_phys_segments(q, bio); 1404 1405 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments 1406 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) { 1407 req->cmd_flags |= REQ_NOMERGE; 1408 if (req == q->last_merge) 1409 q->last_merge = NULL; 1410 return 0; 1411 } 1412 1413 /* 1414 * This will form the start of a new hw segment. Bump both 1415 * counters. 1416 */ 1417 req->nr_hw_segments += nr_hw_segs; 1418 req->nr_phys_segments += nr_phys_segs; 1419 return 1; 1420} 1421 1422int ll_back_merge_fn(request_queue_t *q, struct request *req, struct bio *bio) 1423{ 1424 unsigned short max_sectors; 1425 int len; 1426 1427 if (unlikely(blk_pc_request(req))) 1428 max_sectors = q->max_hw_sectors; 1429 else 1430 max_sectors = q->max_sectors; 1431 1432 if (req->nr_sectors + bio_sectors(bio) > max_sectors) { 1433 req->cmd_flags |= REQ_NOMERGE; 1434 if (req == q->last_merge) 1435 q->last_merge = NULL; 1436 return 0; 1437 } 1438 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID))) 1439 blk_recount_segments(q, req->biotail); 1440 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) 1441 blk_recount_segments(q, bio); 1442 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size; 1443 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) && 1444 !BIOVEC_VIRT_OVERSIZE(len)) { 1445 int mergeable = ll_new_mergeable(q, req, bio); 1446 1447 if (mergeable) { 1448 if (req->nr_hw_segments == 1) 1449 req->bio->bi_hw_front_size = len; 1450 if (bio->bi_hw_segments == 1) 1451 bio->bi_hw_back_size = len; 1452 } 1453 return mergeable; 1454 } 1455 1456 return ll_new_hw_segment(q, req, bio); 1457} 1458EXPORT_SYMBOL(ll_back_merge_fn); 1459 1460static int ll_front_merge_fn(request_queue_t *q, struct request *req, 1461 struct bio *bio) 1462{ 1463 unsigned short max_sectors; 1464 int len; 1465 1466 if (unlikely(blk_pc_request(req))) 1467 max_sectors = q->max_hw_sectors; 1468 else 1469 max_sectors = q->max_sectors; 1470 1471 1472 if (req->nr_sectors + bio_sectors(bio) > max_sectors) { 1473 req->cmd_flags |= REQ_NOMERGE; 1474 if (req == q->last_merge) 1475 q->last_merge = NULL; 1476 return 0; 1477 } 1478 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size; 1479 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) 1480 blk_recount_segments(q, bio); 1481 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID))) 1482 blk_recount_segments(q, req->bio); 1483 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) && 1484 !BIOVEC_VIRT_OVERSIZE(len)) { 1485 int mergeable = ll_new_mergeable(q, req, bio); 1486 1487 if (mergeable) { 1488 if (bio->bi_hw_segments == 1) 1489 bio->bi_hw_front_size = len; 1490 if (req->nr_hw_segments == 1) 1491 req->biotail->bi_hw_back_size = len; 1492 } 1493 return mergeable; 1494 } 1495 1496 return ll_new_hw_segment(q, req, bio); 1497} 1498 1499static int ll_merge_requests_fn(request_queue_t *q, struct request *req, 1500 struct request *next) 1501{ 1502 int total_phys_segments; 1503 int total_hw_segments; 1504 1505 /* 1506 * First check if the either of the requests are re-queued 1507 * requests. Can't merge them if they are. 1508 */ 1509 if (req->special || next->special) 1510 return 0; 1511 1512 /* 1513 * Will it become too large? 1514 */ 1515 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors) 1516 return 0; 1517 1518 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 1519 if (blk_phys_contig_segment(q, req->biotail, next->bio)) 1520 total_phys_segments--; 1521 1522 if (total_phys_segments > q->max_phys_segments) 1523 return 0; 1524 1525 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments; 1526 if (blk_hw_contig_segment(q, req->biotail, next->bio)) { 1527 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size; 1528 /* 1529 * propagate the combined length to the end of the requests 1530 */ 1531 if (req->nr_hw_segments == 1) 1532 req->bio->bi_hw_front_size = len; 1533 if (next->nr_hw_segments == 1) 1534 next->biotail->bi_hw_back_size = len; 1535 total_hw_segments--; 1536 } 1537 1538 if (total_hw_segments > q->max_hw_segments) 1539 return 0; 1540 1541 /* Merge is OK... */ 1542 req->nr_phys_segments = total_phys_segments; 1543 req->nr_hw_segments = total_hw_segments; 1544 return 1; 1545} 1546 1547/* 1548 * "plug" the device if there are no outstanding requests: this will 1549 * force the transfer to start only after we have put all the requests 1550 * on the list. 1551 * 1552 * This is called with interrupts off and no requests on the queue and 1553 * with the queue lock held. 1554 */ 1555void blk_plug_device(request_queue_t *q) 1556{ 1557 WARN_ON(!irqs_disabled()); 1558 1559 /* 1560 * don't plug a stopped queue, it must be paired with blk_start_queue() 1561 * which will restart the queueing 1562 */ 1563 if (blk_queue_stopped(q)) 1564 return; 1565 1566 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) { 1567 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay); 1568 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG); 1569 } 1570} 1571 1572EXPORT_SYMBOL(blk_plug_device); 1573 1574/* 1575 * remove the queue from the plugged list, if present. called with 1576 * queue lock held and interrupts disabled. 1577 */ 1578int blk_remove_plug(request_queue_t *q) 1579{ 1580 WARN_ON(!irqs_disabled()); 1581 1582 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) 1583 return 0; 1584 1585 del_timer(&q->unplug_timer); 1586 return 1; 1587} 1588 1589EXPORT_SYMBOL(blk_remove_plug); 1590 1591/* 1592 * remove the plug and let it rip.. 1593 */ 1594void __generic_unplug_device(request_queue_t *q) 1595{ 1596 if (unlikely(blk_queue_stopped(q))) 1597 return; 1598 1599 if (!blk_remove_plug(q)) 1600 return; 1601 1602 q->request_fn(q); 1603} 1604EXPORT_SYMBOL(__generic_unplug_device); 1605 1606/** 1607 * generic_unplug_device - fire a request queue 1608 * @q: The &request_queue_t in question 1609 * 1610 * Description: 1611 * Linux uses plugging to build bigger requests queues before letting 1612 * the device have at them. If a queue is plugged, the I/O scheduler 1613 * is still adding and merging requests on the queue. Once the queue 1614 * gets unplugged, the request_fn defined for the queue is invoked and 1615 * transfers started. 1616 **/ 1617void generic_unplug_device(request_queue_t *q) 1618{ 1619 spin_lock_irq(q->queue_lock); 1620 __generic_unplug_device(q); 1621 spin_unlock_irq(q->queue_lock); 1622} 1623EXPORT_SYMBOL(generic_unplug_device); 1624 1625static void blk_backing_dev_unplug(struct backing_dev_info *bdi, 1626 struct page *page) 1627{ 1628 request_queue_t *q = bdi->unplug_io_data; 1629 1630 /* 1631 * devices don't necessarily have an ->unplug_fn defined 1632 */ 1633 if (q->unplug_fn) { 1634 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL, 1635 q->rq.count[READ] + q->rq.count[WRITE]); 1636 1637 q->unplug_fn(q); 1638 } 1639} 1640 1641static void blk_unplug_work(struct work_struct *work) 1642{ 1643 request_queue_t *q = container_of(work, request_queue_t, unplug_work); 1644 1645 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL, 1646 q->rq.count[READ] + q->rq.count[WRITE]); 1647 1648 q->unplug_fn(q); 1649} 1650 1651static void blk_unplug_timeout(unsigned long data) 1652{ 1653 request_queue_t *q = (request_queue_t *)data; 1654 1655 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL, 1656 q->rq.count[READ] + q->rq.count[WRITE]); 1657 1658 kblockd_schedule_work(&q->unplug_work); 1659} 1660 1661/** 1662 * blk_start_queue - restart a previously stopped queue 1663 * @q: The &request_queue_t in question 1664 * 1665 * Description: 1666 * blk_start_queue() will clear the stop flag on the queue, and call 1667 * the request_fn for the queue if it was in a stopped state when 1668 * entered. Also see blk_stop_queue(). Queue lock must be held. 1669 **/ 1670void blk_start_queue(request_queue_t *q) 1671{ 1672 WARN_ON(!irqs_disabled()); 1673 1674 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags); 1675 1676 /* 1677 * one level of recursion is ok and is much faster than kicking 1678 * the unplug handling 1679 */ 1680 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) { 1681 q->request_fn(q); 1682 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags); 1683 } else { 1684 blk_plug_device(q); 1685 kblockd_schedule_work(&q->unplug_work); 1686 } 1687} 1688 1689EXPORT_SYMBOL(blk_start_queue); 1690 1691/** 1692 * blk_stop_queue - stop a queue 1693 * @q: The &request_queue_t in question 1694 * 1695 * Description: 1696 * The Linux block layer assumes that a block driver will consume all 1697 * entries on the request queue when the request_fn strategy is called. 1698 * Often this will not happen, because of hardware limitations (queue 1699 * depth settings). If a device driver gets a 'queue full' response, 1700 * or if it simply chooses not to queue more I/O at one point, it can 1701 * call this function to prevent the request_fn from being called until 1702 * the driver has signalled it's ready to go again. This happens by calling 1703 * blk_start_queue() to restart queue operations. Queue lock must be held. 1704 **/ 1705void blk_stop_queue(request_queue_t *q) 1706{ 1707 blk_remove_plug(q); 1708 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags); 1709} 1710EXPORT_SYMBOL(blk_stop_queue); 1711 1712/** 1713 * blk_sync_queue - cancel any pending callbacks on a queue 1714 * @q: the queue 1715 * 1716 * Description: 1717 * The block layer may perform asynchronous callback activity 1718 * on a queue, such as calling the unplug function after a timeout. 1719 * A block device may call blk_sync_queue to ensure that any 1720 * such activity is cancelled, thus allowing it to release resources 1721 * that the callbacks might use. The caller must already have made sure 1722 * that its ->make_request_fn will not re-add plugging prior to calling 1723 * this function. 1724 * 1725 */ 1726void blk_sync_queue(struct request_queue *q) 1727{ 1728 del_timer_sync(&q->unplug_timer); 1729} 1730EXPORT_SYMBOL(blk_sync_queue); 1731 1732/** 1733 * blk_run_queue - run a single device queue 1734 * @q: The queue to run 1735 */ 1736void blk_run_queue(struct request_queue *q) 1737{ 1738 unsigned long flags; 1739 1740 spin_lock_irqsave(q->queue_lock, flags); 1741 blk_remove_plug(q); 1742 1743 /* 1744 * Only recurse once to avoid overrunning the stack, let the unplug 1745 * handling reinvoke the handler shortly if we already got there. 1746 */ 1747 if (!elv_queue_empty(q)) { 1748 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) { 1749 q->request_fn(q); 1750 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags); 1751 } else { 1752 blk_plug_device(q); 1753 kblockd_schedule_work(&q->unplug_work); 1754 } 1755 } 1756 1757 spin_unlock_irqrestore(q->queue_lock, flags); 1758} 1759EXPORT_SYMBOL(blk_run_queue); 1760 1761/** 1762 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed 1763 * @kobj: the kobj belonging of the request queue to be released 1764 * 1765 * Description: 1766 * blk_cleanup_queue is the pair to blk_init_queue() or 1767 * blk_queue_make_request(). It should be called when a request queue is 1768 * being released; typically when a block device is being de-registered. 1769 * Currently, its primary task it to free all the &struct request 1770 * structures that were allocated to the queue and the queue itself. 1771 * 1772 * Caveat: 1773 * Hopefully the low level driver will have finished any 1774 * outstanding requests first... 1775 **/ 1776static void blk_release_queue(struct kobject *kobj) 1777{ 1778 request_queue_t *q = container_of(kobj, struct request_queue, kobj); 1779 struct request_list *rl = &q->rq; 1780 1781 blk_sync_queue(q); 1782 1783 if (rl->rq_pool) 1784 mempool_destroy(rl->rq_pool); 1785 1786 if (q->queue_tags) 1787 __blk_queue_free_tags(q); 1788 1789 blk_trace_shutdown(q); 1790 1791 kmem_cache_free(requestq_cachep, q); 1792} 1793 1794void blk_put_queue(request_queue_t *q) 1795{ 1796 kobject_put(&q->kobj); 1797} 1798EXPORT_SYMBOL(blk_put_queue); 1799 1800void blk_cleanup_queue(request_queue_t * q) 1801{ 1802 mutex_lock(&q->sysfs_lock); 1803 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags); 1804 mutex_unlock(&q->sysfs_lock); 1805 1806 if (q->elevator) 1807 elevator_exit(q->elevator); 1808 1809 blk_put_queue(q); 1810} 1811 1812EXPORT_SYMBOL(blk_cleanup_queue); 1813 1814static int blk_init_free_list(request_queue_t *q) 1815{ 1816 struct request_list *rl = &q->rq; 1817 1818 rl->count[READ] = rl->count[WRITE] = 0; 1819 rl->starved[READ] = rl->starved[WRITE] = 0; 1820 rl->elvpriv = 0; 1821 init_waitqueue_head(&rl->wait[READ]); 1822 init_waitqueue_head(&rl->wait[WRITE]); 1823 1824 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab, 1825 mempool_free_slab, request_cachep, q->node); 1826 1827 if (!rl->rq_pool) 1828 return -ENOMEM; 1829 1830 return 0; 1831} 1832 1833request_queue_t *blk_alloc_queue(gfp_t gfp_mask) 1834{ 1835 return blk_alloc_queue_node(gfp_mask, -1); 1836} 1837EXPORT_SYMBOL(blk_alloc_queue); 1838 1839static struct kobj_type queue_ktype; 1840 1841request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id) 1842{ 1843 request_queue_t *q; 1844 1845 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id); 1846 if (!q) 1847 return NULL; 1848 1849 memset(q, 0, sizeof(*q)); 1850 init_timer(&q->unplug_timer); 1851 1852 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue"); 1853 q->kobj.ktype = &queue_ktype; 1854 kobject_init(&q->kobj); 1855 1856 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug; 1857 q->backing_dev_info.unplug_io_data = q; 1858 1859 mutex_init(&q->sysfs_lock); 1860 1861 return q; 1862} 1863EXPORT_SYMBOL(blk_alloc_queue_node); 1864 1865/** 1866 * blk_init_queue - prepare a request queue for use with a block device 1867 * @rfn: The function to be called to process requests that have been 1868 * placed on the queue. 1869 * @lock: Request queue spin lock 1870 * 1871 * Description: 1872 * If a block device wishes to use the standard request handling procedures, 1873 * which sorts requests and coalesces adjacent requests, then it must 1874 * call blk_init_queue(). The function @rfn will be called when there 1875 * are requests on the queue that need to be processed. If the device 1876 * supports plugging, then @rfn may not be called immediately when requests 1877 * are available on the queue, but may be called at some time later instead. 1878 * Plugged queues are generally unplugged when a buffer belonging to one 1879 * of the requests on the queue is needed, or due to memory pressure. 1880 * 1881 * @rfn is not required, or even expected, to remove all requests off the 1882 * queue, but only as many as it can handle at a time. If it does leave 1883 * requests on the queue, it is responsible for arranging that the requests 1884 * get dealt with eventually. 1885 * 1886 * The queue spin lock must be held while manipulating the requests on the 1887 * request queue; this lock will be taken also from interrupt context, so irq 1888 * disabling is needed for it. 1889 * 1890 * Function returns a pointer to the initialized request queue, or NULL if 1891 * it didn't succeed. 1892 * 1893 * Note: 1894 * blk_init_queue() must be paired with a blk_cleanup_queue() call 1895 * when the block device is deactivated (such as at module unload). 1896 **/ 1897 1898request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock) 1899{ 1900 return blk_init_queue_node(rfn, lock, -1); 1901} 1902EXPORT_SYMBOL(blk_init_queue); 1903 1904request_queue_t * 1905blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) 1906{ 1907 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id); 1908 1909 if (!q) 1910 return NULL; 1911 1912 q->node = node_id; 1913 if (blk_init_free_list(q)) { 1914 kmem_cache_free(requestq_cachep, q); 1915 return NULL; 1916 } 1917 1918 /* 1919 * if caller didn't supply a lock, they get per-queue locking with 1920 * our embedded lock 1921 */ 1922 if (!lock) { 1923 spin_lock_init(&q->__queue_lock); 1924 lock = &q->__queue_lock; 1925 } 1926 1927 q->request_fn = rfn; 1928 q->prep_rq_fn = NULL; 1929 q->unplug_fn = generic_unplug_device; 1930 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER); 1931 q->queue_lock = lock; 1932 1933 blk_queue_segment_boundary(q, 0xffffffff); 1934 1935 blk_queue_make_request(q, __make_request); 1936 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE); 1937 1938 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS); 1939 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS); 1940 1941 q->sg_reserved_size = INT_MAX; 1942 1943 /* 1944 * all done 1945 */ 1946 if (!elevator_init(q, NULL)) { 1947 blk_queue_congestion_threshold(q); 1948 return q; 1949 } 1950 1951 blk_put_queue(q); 1952 return NULL; 1953} 1954EXPORT_SYMBOL(blk_init_queue_node); 1955 1956int blk_get_queue(request_queue_t *q) 1957{ 1958 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) { 1959 kobject_get(&q->kobj); 1960 return 0; 1961 } 1962 1963 return 1; 1964} 1965 1966EXPORT_SYMBOL(blk_get_queue); 1967 1968static inline void blk_free_request(request_queue_t *q, struct request *rq) 1969{ 1970 if (rq->cmd_flags & REQ_ELVPRIV) 1971 elv_put_request(q, rq); 1972 mempool_free(rq, q->rq.rq_pool); 1973} 1974 1975static struct request * 1976blk_alloc_request(request_queue_t *q, int rw, int priv, gfp_t gfp_mask) 1977{ 1978 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask); 1979 1980 if (!rq) 1981 return NULL; 1982 1983 /* 1984 * first three bits are identical in rq->cmd_flags and bio->bi_rw, 1985 * see bio.h and blkdev.h 1986 */ 1987 rq->cmd_flags = rw | REQ_ALLOCED; 1988 1989 if (priv) { 1990 if (unlikely(elv_set_request(q, rq, gfp_mask))) { 1991 mempool_free(rq, q->rq.rq_pool); 1992 return NULL; 1993 } 1994 rq->cmd_flags |= REQ_ELVPRIV; 1995 } 1996 1997 return rq; 1998} 1999 2000/* 2001 * ioc_batching returns true if the ioc is a valid batching request and 2002 * should be given priority access to a request. 2003 */ 2004static inline int ioc_batching(request_queue_t *q, struct io_context *ioc) 2005{ 2006 if (!ioc) 2007 return 0; 2008 2009 /* 2010 * Make sure the process is able to allocate at least 1 request 2011 * even if the batch times out, otherwise we could theoretically 2012 * lose wakeups. 2013 */ 2014 return ioc->nr_batch_requests == q->nr_batching || 2015 (ioc->nr_batch_requests > 0 2016 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME)); 2017} 2018 2019/* 2020 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This 2021 * will cause the process to be a "batcher" on all queues in the system. This 2022 * is the behaviour we want though - once it gets a wakeup it should be given 2023 * a nice run. 2024 */ 2025static void ioc_set_batching(request_queue_t *q, struct io_context *ioc) 2026{ 2027 if (!ioc || ioc_batching(q, ioc)) 2028 return; 2029 2030 ioc->nr_batch_requests = q->nr_batching; 2031 ioc->last_waited = jiffies; 2032} 2033 2034static void __freed_request(request_queue_t *q, int rw) 2035{ 2036 struct request_list *rl = &q->rq; 2037 2038 if (rl->count[rw] < queue_congestion_off_threshold(q)) 2039 blk_clear_queue_congested(q, rw); 2040 2041 if (rl->count[rw] + 1 <= q->nr_requests) { 2042 if (waitqueue_active(&rl->wait[rw])) 2043 wake_up(&rl->wait[rw]); 2044 2045 blk_clear_queue_full(q, rw); 2046 } 2047} 2048 2049/* 2050 * A request has just been released. Account for it, update the full and 2051 * congestion status, wake up any waiters. Called under q->queue_lock. 2052 */ 2053static void freed_request(request_queue_t *q, int rw, int priv) 2054{ 2055 struct request_list *rl = &q->rq; 2056 2057 rl->count[rw]--; 2058 if (priv) 2059 rl->elvpriv--; 2060 2061 __freed_request(q, rw); 2062 2063 if (unlikely(rl->starved[rw ^ 1])) 2064 __freed_request(q, rw ^ 1); 2065} 2066 2067#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist) 2068/* 2069 * Get a free request, queue_lock must be held. 2070 * Returns NULL on failure, with queue_lock held. 2071 * Returns !NULL on success, with queue_lock *not held*. 2072 */ 2073static struct request *get_request(request_queue_t *q, int rw_flags, 2074 struct bio *bio, gfp_t gfp_mask) 2075{ 2076 struct request *rq = NULL; 2077 struct request_list *rl = &q->rq; 2078 struct io_context *ioc = NULL; 2079 const int rw = rw_flags & 0x01; 2080 int may_queue, priv; 2081 2082 may_queue = elv_may_queue(q, rw_flags); 2083 if (may_queue == ELV_MQUEUE_NO) 2084 goto rq_starved; 2085 2086 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) { 2087 if (rl->count[rw]+1 >= q->nr_requests) { 2088 ioc = current_io_context(GFP_ATOMIC, q->node); 2089 /* 2090 * The queue will fill after this allocation, so set 2091 * it as full, and mark this process as "batching". 2092 * This process will be allowed to complete a batch of 2093 * requests, others will be blocked. 2094 */ 2095 if (!blk_queue_full(q, rw)) { 2096 ioc_set_batching(q, ioc); 2097 blk_set_queue_full(q, rw); 2098 } else { 2099 if (may_queue != ELV_MQUEUE_MUST 2100 && !ioc_batching(q, ioc)) { 2101 /* 2102 * The queue is full and the allocating 2103 * process is not a "batcher", and not 2104 * exempted by the IO scheduler 2105 */ 2106 goto out; 2107 } 2108 } 2109 } 2110 blk_set_queue_congested(q, rw); 2111 } 2112 2113 /* 2114 * Only allow batching queuers to allocate up to 50% over the defined 2115 * limit of requests, otherwise we could have thousands of requests 2116 * allocated with any setting of ->nr_requests 2117 */ 2118 if (rl->count[rw] >= (3 * q->nr_requests / 2)) 2119 goto out; 2120 2121 rl->count[rw]++; 2122 rl->starved[rw] = 0; 2123 2124 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); 2125 if (priv) 2126 rl->elvpriv++; 2127 2128 spin_unlock_irq(q->queue_lock); 2129 2130 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask); 2131 if (unlikely(!rq)) { 2132 /* 2133 * Allocation failed presumably due to memory. Undo anything 2134 * we might have messed up. 2135 * 2136 * Allocating task should really be put onto the front of the 2137 * wait queue, but this is pretty rare. 2138 */ 2139 spin_lock_irq(q->queue_lock); 2140 freed_request(q, rw, priv); 2141 2142 /* 2143 * in the very unlikely event that allocation failed and no 2144 * requests for this direction was pending, mark us starved 2145 * so that freeing of a request in the other direction will 2146 * notice us. another possible fix would be to split the 2147 * rq mempool into READ and WRITE 2148 */ 2149rq_starved: 2150 if (unlikely(rl->count[rw] == 0)) 2151 rl->starved[rw] = 1; 2152 2153 goto out; 2154 } 2155 2156 /* 2157 * ioc may be NULL here, and ioc_batching will be false. That's 2158 * OK, if the queue is under the request limit then requests need 2159 * not count toward the nr_batch_requests limit. There will always 2160 * be some limit enforced by BLK_BATCH_TIME. 2161 */ 2162 if (ioc_batching(q, ioc)) 2163 ioc->nr_batch_requests--; 2164 2165 rq_init(q, rq); 2166 2167 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ); 2168out: 2169 return rq; 2170} 2171 2172/* 2173 * No available requests for this queue, unplug the device and wait for some 2174 * requests to become available. 2175 * 2176 * Called with q->queue_lock held, and returns with it unlocked. 2177 */ 2178static struct request *get_request_wait(request_queue_t *q, int rw_flags, 2179 struct bio *bio) 2180{ 2181 const int rw = rw_flags & 0x01; 2182 struct request *rq; 2183 2184 rq = get_request(q, rw_flags, bio, GFP_NOIO); 2185 while (!rq) { 2186 DEFINE_WAIT(wait); 2187 struct request_list *rl = &q->rq; 2188 2189 prepare_to_wait_exclusive(&rl->wait[rw], &wait, 2190 TASK_UNINTERRUPTIBLE); 2191 2192 rq = get_request(q, rw_flags, bio, GFP_NOIO); 2193 2194 if (!rq) { 2195 struct io_context *ioc; 2196 2197 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ); 2198 2199 __generic_unplug_device(q); 2200 spin_unlock_irq(q->queue_lock); 2201 io_schedule(); 2202 2203 /* 2204 * After sleeping, we become a "batching" process and 2205 * will be able to allocate at least one request, and 2206 * up to a big batch of them for a small period time. 2207 * See ioc_batching, ioc_set_batching 2208 */ 2209 ioc = current_io_context(GFP_NOIO, q->node); 2210 ioc_set_batching(q, ioc); 2211 2212 spin_lock_irq(q->queue_lock); 2213 } 2214 finish_wait(&rl->wait[rw], &wait); 2215 } 2216 2217 return rq; 2218} 2219 2220struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask) 2221{ 2222 struct request *rq; 2223 2224 BUG_ON(rw != READ && rw != WRITE); 2225 2226 spin_lock_irq(q->queue_lock); 2227 if (gfp_mask & __GFP_WAIT) { 2228 rq = get_request_wait(q, rw, NULL); 2229 } else { 2230 rq = get_request(q, rw, NULL, gfp_mask); 2231 if (!rq) 2232 spin_unlock_irq(q->queue_lock); 2233 } 2234 /* q->queue_lock is unlocked at this point */ 2235 2236 return rq; 2237} 2238EXPORT_SYMBOL(blk_get_request); 2239 2240/** 2241 * blk_start_queueing - initiate dispatch of requests to device 2242 * @q: request queue to kick into gear 2243 * 2244 * This is basically a helper to remove the need to know whether a queue 2245 * is plugged or not if someone just wants to initiate dispatch of requests 2246 * for this queue. 2247 * 2248 * The queue lock must be held with interrupts disabled. 2249 */ 2250void blk_start_queueing(request_queue_t *q) 2251{ 2252 if (!blk_queue_plugged(q)) 2253 q->request_fn(q); 2254 else 2255 __generic_unplug_device(q); 2256} 2257EXPORT_SYMBOL(blk_start_queueing); 2258 2259/** 2260 * blk_requeue_request - put a request back on queue 2261 * @q: request queue where request should be inserted 2262 * @rq: request to be inserted 2263 * 2264 * Description: 2265 * Drivers often keep queueing requests until the hardware cannot accept 2266 * more, when that condition happens we need to put the request back 2267 * on the queue. Must be called with queue lock held. 2268 */ 2269void blk_requeue_request(request_queue_t *q, struct request *rq) 2270{ 2271 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE); 2272 2273 if (blk_rq_tagged(rq)) 2274 blk_queue_end_tag(q, rq); 2275 2276 elv_requeue_request(q, rq); 2277} 2278 2279EXPORT_SYMBOL(blk_requeue_request); 2280 2281/** 2282 * blk_insert_request - insert a special request in to a request queue 2283 * @q: request queue where request should be inserted 2284 * @rq: request to be inserted 2285 * @at_head: insert request at head or tail of queue 2286 * @data: private data 2287 * 2288 * Description: 2289 * Many block devices need to execute commands asynchronously, so they don't 2290 * block the whole kernel from preemption during request execution. This is 2291 * accomplished normally by inserting aritficial requests tagged as 2292 * REQ_SPECIAL in to the corresponding request queue, and letting them be 2293 * scheduled for actual execution by the request queue. 2294 * 2295 * We have the option of inserting the head or the tail of the queue. 2296 * Typically we use the tail for new ioctls and so forth. We use the head 2297 * of the queue for things like a QUEUE_FULL message from a device, or a 2298 * host that is unable to accept a particular command. 2299 */ 2300void blk_insert_request(request_queue_t *q, struct request *rq, 2301 int at_head, void *data) 2302{ 2303 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK; 2304 unsigned long flags; 2305 2306 /* 2307 * tell I/O scheduler that this isn't a regular read/write (ie it 2308 * must not attempt merges on this) and that it acts as a soft 2309 * barrier 2310 */ 2311 rq->cmd_type = REQ_TYPE_SPECIAL; 2312 rq->cmd_flags |= REQ_SOFTBARRIER; 2313 2314 rq->special = data; 2315 2316 spin_lock_irqsave(q->queue_lock, flags); 2317 2318 /* 2319 * If command is tagged, release the tag 2320 */ 2321 if (blk_rq_tagged(rq)) 2322 blk_queue_end_tag(q, rq); 2323 2324 drive_stat_acct(rq, rq->nr_sectors, 1); 2325 __elv_add_request(q, rq, where, 0); 2326 blk_start_queueing(q); 2327 spin_unlock_irqrestore(q->queue_lock, flags); 2328} 2329 2330EXPORT_SYMBOL(blk_insert_request); 2331 2332static int __blk_rq_unmap_user(struct bio *bio) 2333{ 2334 int ret = 0; 2335 2336 if (bio) { 2337 if (bio_flagged(bio, BIO_USER_MAPPED)) 2338 bio_unmap_user(bio); 2339 else 2340 ret = bio_uncopy_user(bio); 2341 } 2342 2343 return ret; 2344} 2345 2346static int __blk_rq_map_user(request_queue_t *q, struct request *rq, 2347 void __user *ubuf, unsigned int len) 2348{ 2349 unsigned long uaddr; 2350 struct bio *bio, *orig_bio; 2351 int reading, ret; 2352 2353 reading = rq_data_dir(rq) == READ; 2354 2355 /* 2356 * if alignment requirement is satisfied, map in user pages for 2357 * direct dma. else, set up kernel bounce buffers 2358 */ 2359 uaddr = (unsigned long) ubuf; 2360 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q))) 2361 bio = bio_map_user(q, NULL, uaddr, len, reading); 2362 else 2363 bio = bio_copy_user(q, uaddr, len, reading); 2364 2365 if (IS_ERR(bio)) 2366 return PTR_ERR(bio); 2367 2368 orig_bio = bio; 2369 blk_queue_bounce(q, &bio); 2370 2371 /* 2372 * We link the bounce buffer in and could have to traverse it 2373 * later so we have to get a ref to prevent it from being freed 2374 */ 2375 bio_get(bio); 2376 2377 if (!rq->bio) 2378 blk_rq_bio_prep(q, rq, bio); 2379 else if (!ll_back_merge_fn(q, rq, bio)) { 2380 ret = -EINVAL; 2381 goto unmap_bio; 2382 } else { 2383 rq->biotail->bi_next = bio; 2384 rq->biotail = bio; 2385 2386 rq->data_len += bio->bi_size; 2387 } 2388 2389 return bio->bi_size; 2390 2391unmap_bio: 2392 /* if it was boucned we must call the end io function */ 2393 bio_endio(bio, bio->bi_size, 0); 2394 __blk_rq_unmap_user(orig_bio); 2395 bio_put(bio); 2396 return ret; 2397} 2398 2399/** 2400 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage 2401 * @q: request queue where request should be inserted 2402 * @rq: request structure to fill 2403 * @ubuf: the user buffer 2404 * @len: length of user data 2405 * 2406 * Description: 2407 * Data will be mapped directly for zero copy io, if possible. Otherwise 2408 * a kernel bounce buffer is used. 2409 * 2410 * A matching blk_rq_unmap_user() must be issued at the end of io, while 2411 * still in process context. 2412 * 2413 * Note: The mapped bio may need to be bounced through blk_queue_bounce() 2414 * before being submitted to the device, as pages mapped may be out of 2415 * reach. It's the callers responsibility to make sure this happens. The 2416 * original bio must be passed back in to blk_rq_unmap_user() for proper 2417 * unmapping. 2418 */ 2419int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf, 2420 unsigned long len) 2421{ 2422 unsigned long bytes_read = 0; 2423 struct bio *bio = NULL; 2424 int ret; 2425 2426 if (len > (q->max_hw_sectors << 9)) 2427 return -EINVAL; 2428 if (!len || !ubuf) 2429 return -EINVAL; 2430 2431 while (bytes_read != len) { 2432 unsigned long map_len, end, start; 2433 2434 map_len = min_t(unsigned long, len - bytes_read, BIO_MAX_SIZE); 2435 end = ((unsigned long)ubuf + map_len + PAGE_SIZE - 1) 2436 >> PAGE_SHIFT; 2437 start = (unsigned long)ubuf >> PAGE_SHIFT; 2438 2439 /* 2440 * A bad offset could cause us to require BIO_MAX_PAGES + 1 2441 * pages. If this happens we just lower the requested 2442 * mapping len by a page so that we can fit 2443 */ 2444 if (end - start > BIO_MAX_PAGES) 2445 map_len -= PAGE_SIZE; 2446 2447 ret = __blk_rq_map_user(q, rq, ubuf, map_len); 2448 if (ret < 0) 2449 goto unmap_rq; 2450 if (!bio) 2451 bio = rq->bio; 2452 bytes_read += ret; 2453 ubuf += ret; 2454 } 2455 2456 rq->buffer = rq->data = NULL; 2457 return 0; 2458unmap_rq: 2459 blk_rq_unmap_user(bio); 2460 return ret; 2461} 2462 2463EXPORT_SYMBOL(blk_rq_map_user); 2464 2465/** 2466 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage 2467 * @q: request queue where request should be inserted 2468 * @rq: request to map data to 2469 * @iov: pointer to the iovec 2470 * @iov_count: number of elements in the iovec 2471 * @len: I/O byte count 2472 * 2473 * Description: 2474 * Data will be mapped directly for zero copy io, if possible. Otherwise 2475 * a kernel bounce buffer is used. 2476 * 2477 * A matching blk_rq_unmap_user() must be issued at the end of io, while 2478 * still in process context. 2479 * 2480 * Note: The mapped bio may need to be bounced through blk_queue_bounce() 2481 * before being submitted to the device, as pages mapped may be out of 2482 * reach. It's the callers responsibility to make sure this happens. The 2483 * original bio must be passed back in to blk_rq_unmap_user() for proper 2484 * unmapping. 2485 */ 2486int blk_rq_map_user_iov(request_queue_t *q, struct request *rq, 2487 struct sg_iovec *iov, int iov_count, unsigned int len) 2488{ 2489 struct bio *bio; 2490 2491 if (!iov || iov_count <= 0) 2492 return -EINVAL; 2493 2494 /* we don't allow misaligned data like bio_map_user() does. If the 2495 * user is using sg, they're expected to know the alignment constraints 2496 * and respect them accordingly */ 2497 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ); 2498 if (IS_ERR(bio)) 2499 return PTR_ERR(bio); 2500 2501 if (bio->bi_size != len) { 2502 bio_endio(bio, bio->bi_size, 0); 2503 bio_unmap_user(bio); 2504 return -EINVAL; 2505 } 2506 2507 bio_get(bio); 2508 blk_rq_bio_prep(q, rq, bio); 2509 rq->buffer = rq->data = NULL; 2510 return 0; 2511} 2512 2513EXPORT_SYMBOL(blk_rq_map_user_iov); 2514 2515/** 2516 * blk_rq_unmap_user - unmap a request with user data 2517 * @bio: start of bio list 2518 * 2519 * Description: 2520 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must 2521 * supply the original rq->bio from the blk_rq_map_user() return, since 2522 * the io completion may have changed rq->bio. 2523 */ 2524int blk_rq_unmap_user(struct bio *bio) 2525{ 2526 struct bio *mapped_bio; 2527 int ret = 0, ret2; 2528 2529 while (bio) { 2530 mapped_bio = bio; 2531 if (unlikely(bio_flagged(bio, BIO_BOUNCED))) 2532 mapped_bio = bio->bi_private; 2533 2534 ret2 = __blk_rq_unmap_user(mapped_bio); 2535 if (ret2 && !ret) 2536 ret = ret2; 2537 2538 mapped_bio = bio; 2539 bio = bio->bi_next; 2540 bio_put(mapped_bio); 2541 } 2542 2543 return ret; 2544} 2545 2546EXPORT_SYMBOL(blk_rq_unmap_user); 2547 2548/** 2549 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage 2550 * @q: request queue where request should be inserted 2551 * @rq: request to fill 2552 * @kbuf: the kernel buffer 2553 * @len: length of user data 2554 * @gfp_mask: memory allocation flags 2555 */ 2556int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf, 2557 unsigned int len, gfp_t gfp_mask) 2558{ 2559 struct bio *bio; 2560 2561 if (len > (q->max_hw_sectors << 9)) 2562 return -EINVAL; 2563 if (!len || !kbuf) 2564 return -EINVAL; 2565 2566 bio = bio_map_kern(q, kbuf, len, gfp_mask); 2567 if (IS_ERR(bio)) 2568 return PTR_ERR(bio); 2569 2570 if (rq_data_dir(rq) == WRITE) 2571 bio->bi_rw |= (1 << BIO_RW); 2572 2573 blk_rq_bio_prep(q, rq, bio); 2574 blk_queue_bounce(q, &rq->bio); 2575 rq->buffer = rq->data = NULL; 2576 return 0; 2577} 2578 2579EXPORT_SYMBOL(blk_rq_map_kern); 2580 2581/** 2582 * blk_execute_rq_nowait - insert a request into queue for execution 2583 * @q: queue to insert the request in 2584 * @bd_disk: matching gendisk 2585 * @rq: request to insert 2586 * @at_head: insert request at head or tail of queue 2587 * @done: I/O completion handler 2588 * 2589 * Description: 2590 * Insert a fully prepared request at the back of the io scheduler queue 2591 * for execution. Don't wait for completion. 2592 */ 2593void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk, 2594 struct request *rq, int at_head, 2595 rq_end_io_fn *done) 2596{ 2597 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK; 2598 2599 rq->rq_disk = bd_disk; 2600 rq->cmd_flags |= REQ_NOMERGE; 2601 rq->end_io = done; 2602 WARN_ON(irqs_disabled()); 2603 spin_lock_irq(q->queue_lock); 2604 __elv_add_request(q, rq, where, 1); 2605 __generic_unplug_device(q); 2606 spin_unlock_irq(q->queue_lock); 2607} 2608EXPORT_SYMBOL_GPL(blk_execute_rq_nowait); 2609 2610/** 2611 * blk_execute_rq - insert a request into queue for execution 2612 * @q: queue to insert the request in 2613 * @bd_disk: matching gendisk 2614 * @rq: request to insert 2615 * @at_head: insert request at head or tail of queue 2616 * 2617 * Description: 2618 * Insert a fully prepared request at the back of the io scheduler queue 2619 * for execution and wait for completion. 2620 */ 2621int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk, 2622 struct request *rq, int at_head) 2623{ 2624 DECLARE_COMPLETION_ONSTACK(wait); 2625 char sense[SCSI_SENSE_BUFFERSIZE]; 2626 int err = 0; 2627 2628 /* 2629 * we need an extra reference to the request, so we can look at 2630 * it after io completion 2631 */ 2632 rq->ref_count++; 2633 2634 if (!rq->sense) { 2635 memset(sense, 0, sizeof(sense)); 2636 rq->sense = sense; 2637 rq->sense_len = 0; 2638 } 2639 2640 rq->end_io_data = &wait; 2641 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq); 2642 wait_for_completion(&wait); 2643 2644 if (rq->errors) 2645 err = -EIO; 2646 2647 return err; 2648} 2649 2650EXPORT_SYMBOL(blk_execute_rq); 2651 2652/** 2653 * blkdev_issue_flush - queue a flush 2654 * @bdev: blockdev to issue flush for 2655 * @error_sector: error sector 2656 * 2657 * Description: 2658 * Issue a flush for the block device in question. Caller can supply 2659 * room for storing the error offset in case of a flush error, if they 2660 * wish to. Caller must run wait_for_completion() on its own. 2661 */ 2662int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector) 2663{ 2664 request_queue_t *q; 2665 2666 if (bdev->bd_disk == NULL) 2667 return -ENXIO; 2668 2669 q = bdev_get_queue(bdev); 2670 if (!q) 2671 return -ENXIO; 2672 if (!q->issue_flush_fn) 2673 return -EOPNOTSUPP; 2674 2675 return q->issue_flush_fn(q, bdev->bd_disk, error_sector); 2676} 2677 2678EXPORT_SYMBOL(blkdev_issue_flush); 2679 2680static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io) 2681{ 2682 int rw = rq_data_dir(rq); 2683 2684 if (!blk_fs_request(rq) || !rq->rq_disk) 2685 return; 2686 2687 if (!new_io) { 2688 __disk_stat_inc(rq->rq_disk, merges[rw]); 2689 } else { 2690 disk_round_stats(rq->rq_disk); 2691 rq->rq_disk->in_flight++; 2692 } 2693} 2694 2695/* 2696 * add-request adds a request to the linked list. 2697 * queue lock is held and interrupts disabled, as we muck with the 2698 * request queue list. 2699 */ 2700static inline void add_request(request_queue_t * q, struct request * req) 2701{ 2702 drive_stat_acct(req, req->nr_sectors, 1); 2703 2704 /* 2705 * elevator indicated where it wants this request to be 2706 * inserted at elevator_merge time 2707 */ 2708 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0); 2709} 2710 2711/* 2712 * disk_round_stats() - Round off the performance stats on a struct 2713 * disk_stats. 2714 * 2715 * The average IO queue length and utilisation statistics are maintained 2716 * by observing the current state of the queue length and the amount of 2717 * time it has been in this state for. 2718 * 2719 * Normally, that accounting is done on IO completion, but that can result 2720 * in more than a second's worth of IO being accounted for within any one 2721 * second, leading to >100% utilisation. To deal with that, we call this 2722 * function to do a round-off before returning the results when reading 2723 * /proc/diskstats. This accounts immediately for all queue usage up to 2724 * the current jiffies and restarts the counters again. 2725 */ 2726void disk_round_stats(struct gendisk *disk) 2727{ 2728 unsigned long now = jiffies; 2729 2730 if (now == disk->stamp) 2731 return; 2732 2733 if (disk->in_flight) { 2734 __disk_stat_add(disk, time_in_queue, 2735 disk->in_flight * (now - disk->stamp)); 2736 __disk_stat_add(disk, io_ticks, (now - disk->stamp)); 2737 } 2738 disk->stamp = now; 2739} 2740 2741EXPORT_SYMBOL_GPL(disk_round_stats); 2742 2743/* 2744 * queue lock must be held 2745 */ 2746void __blk_put_request(request_queue_t *q, struct request *req) 2747{ 2748 if (unlikely(!q)) 2749 return; 2750 if (unlikely(--req->ref_count)) 2751 return; 2752 2753 elv_completed_request(q, req); 2754 2755 /* 2756 * Request may not have originated from ll_rw_blk. if not, 2757 * it didn't come out of our reserved rq pools 2758 */ 2759 if (req->cmd_flags & REQ_ALLOCED) { 2760 int rw = rq_data_dir(req); 2761 int priv = req->cmd_flags & REQ_ELVPRIV; 2762 2763 BUG_ON(!list_empty(&req->queuelist)); 2764 BUG_ON(!hlist_unhashed(&req->hash)); 2765 2766 blk_free_request(q, req); 2767 freed_request(q, rw, priv); 2768 } 2769} 2770 2771EXPORT_SYMBOL_GPL(__blk_put_request); 2772 2773void blk_put_request(struct request *req) 2774{ 2775 unsigned long flags; 2776 request_queue_t *q = req->q; 2777 2778 /* 2779 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the 2780 * following if (q) test. 2781 */ 2782 if (q) { 2783 spin_lock_irqsave(q->queue_lock, flags); 2784 __blk_put_request(q, req); 2785 spin_unlock_irqrestore(q->queue_lock, flags); 2786 } 2787} 2788 2789EXPORT_SYMBOL(blk_put_request); 2790 2791/** 2792 * blk_end_sync_rq - executes a completion event on a request 2793 * @rq: request to complete 2794 * @error: end io status of the request 2795 */ 2796void blk_end_sync_rq(struct request *rq, int error) 2797{ 2798 struct completion *waiting = rq->end_io_data; 2799 2800 rq->end_io_data = NULL; 2801 __blk_put_request(rq->q, rq); 2802 2803 /* 2804 * complete last, if this is a stack request the process (and thus 2805 * the rq pointer) could be invalid right after this complete() 2806 */ 2807 complete(waiting); 2808} 2809EXPORT_SYMBOL(blk_end_sync_rq); 2810 2811/* 2812 * Has to be called with the request spinlock acquired 2813 */ 2814static int attempt_merge(request_queue_t *q, struct request *req, 2815 struct request *next) 2816{ 2817 if (!rq_mergeable(req) || !rq_mergeable(next)) 2818 return 0; 2819 2820 /* 2821 * not contiguous 2822 */ 2823 if (req->sector + req->nr_sectors != next->sector) 2824 return 0; 2825 2826 if (rq_data_dir(req) != rq_data_dir(next) 2827 || req->rq_disk != next->rq_disk 2828 || next->special) 2829 return 0; 2830 2831 /* 2832 * If we are allowed to merge, then append bio list 2833 * from next to rq and release next. merge_requests_fn 2834 * will have updated segment counts, update sector 2835 * counts here. 2836 */ 2837 if (!ll_merge_requests_fn(q, req, next)) 2838 return 0; 2839 2840 /* 2841 * At this point we have either done a back merge 2842 * or front merge. We need the smaller start_time of 2843 * the merged requests to be the current request 2844 * for accounting purposes. 2845 */ 2846 if (time_after(req->start_time, next->start_time)) 2847 req->start_time = next->start_time; 2848 2849 req->biotail->bi_next = next->bio; 2850 req->biotail = next->biotail; 2851 2852 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors; 2853 2854 elv_merge_requests(q, req, next); 2855 2856 if (req->rq_disk) { 2857 disk_round_stats(req->rq_disk); 2858 req->rq_disk->in_flight--; 2859 } 2860 2861 req->ioprio = ioprio_best(req->ioprio, next->ioprio); 2862 2863 __blk_put_request(q, next); 2864 return 1; 2865} 2866 2867static inline int attempt_back_merge(request_queue_t *q, struct request *rq) 2868{ 2869 struct request *next = elv_latter_request(q, rq); 2870 2871 if (next) 2872 return attempt_merge(q, rq, next); 2873 2874 return 0; 2875} 2876 2877static inline int attempt_front_merge(request_queue_t *q, struct request *rq) 2878{ 2879 struct request *prev = elv_former_request(q, rq); 2880 2881 if (prev) 2882 return attempt_merge(q, prev, rq); 2883 2884 return 0; 2885} 2886 2887static void init_request_from_bio(struct request *req, struct bio *bio) 2888{ 2889 req->cmd_type = REQ_TYPE_FS; 2890 2891 /* 2892 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST) 2893 */ 2894 if (bio_rw_ahead(bio) || bio_failfast(bio)) 2895 req->cmd_flags |= REQ_FAILFAST; 2896 2897 /* 2898 * REQ_BARRIER implies no merging, but lets make it explicit 2899 */ 2900 if (unlikely(bio_barrier(bio))) 2901 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE); 2902 2903 if (bio_sync(bio)) 2904 req->cmd_flags |= REQ_RW_SYNC; 2905 if (bio_rw_meta(bio)) 2906 req->cmd_flags |= REQ_RW_META; 2907 2908 req->errors = 0; 2909 req->hard_sector = req->sector = bio->bi_sector; 2910 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio); 2911 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio); 2912 req->nr_phys_segments = bio_phys_segments(req->q, bio); 2913 req->nr_hw_segments = bio_hw_segments(req->q, bio); 2914 req->buffer = bio_data(bio); /* see ->buffer comment above */ 2915 req->bio = req->biotail = bio; 2916 req->ioprio = bio_prio(bio); 2917 req->rq_disk = bio->bi_bdev->bd_disk; 2918 req->start_time = jiffies; 2919} 2920 2921static int __make_request(request_queue_t *q, struct bio *bio) 2922{ 2923 struct request *req; 2924 int el_ret, nr_sectors, barrier, err; 2925 const unsigned short prio = bio_prio(bio); 2926 const int sync = bio_sync(bio); 2927 int rw_flags; 2928 2929 nr_sectors = bio_sectors(bio); 2930 2931 /* 2932 * low level driver can indicate that it wants pages above a 2933 * certain limit bounced to low memory (ie for highmem, or even 2934 * ISA dma in theory) 2935 */ 2936 blk_queue_bounce(q, &bio); 2937 2938 barrier = bio_barrier(bio); 2939 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) { 2940 err = -EOPNOTSUPP; 2941 goto end_io; 2942 } 2943 2944 spin_lock_irq(q->queue_lock); 2945 2946 if (unlikely(barrier) || elv_queue_empty(q)) 2947 goto get_rq; 2948 2949 el_ret = elv_merge(q, &req, bio); 2950 switch (el_ret) { 2951 case ELEVATOR_BACK_MERGE: 2952 BUG_ON(!rq_mergeable(req)); 2953 2954 if (!ll_back_merge_fn(q, req, bio)) 2955 break; 2956 2957 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE); 2958 2959 req->biotail->bi_next = bio; 2960 req->biotail = bio; 2961 req->nr_sectors = req->hard_nr_sectors += nr_sectors; 2962 req->ioprio = ioprio_best(req->ioprio, prio); 2963 drive_stat_acct(req, nr_sectors, 0); 2964 if (!attempt_back_merge(q, req)) 2965 elv_merged_request(q, req, el_ret); 2966 goto out; 2967 2968 case ELEVATOR_FRONT_MERGE: 2969 BUG_ON(!rq_mergeable(req)); 2970 2971 if (!ll_front_merge_fn(q, req, bio)) 2972 break; 2973 2974 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE); 2975 2976 bio->bi_next = req->bio; 2977 req->bio = bio; 2978 2979 /* 2980 * may not be valid. if the low level driver said 2981 * it didn't need a bounce buffer then it better 2982 * not touch req->buffer either... 2983 */ 2984 req->buffer = bio_data(bio); 2985 req->current_nr_sectors = bio_cur_sectors(bio); 2986 req->hard_cur_sectors = req->current_nr_sectors; 2987 req->sector = req->hard_sector = bio->bi_sector; 2988 req->nr_sectors = req->hard_nr_sectors += nr_sectors; 2989 req->ioprio = ioprio_best(req->ioprio, prio); 2990 drive_stat_acct(req, nr_sectors, 0); 2991 if (!attempt_front_merge(q, req)) 2992 elv_merged_request(q, req, el_ret); 2993 goto out; 2994 2995 /* ELV_NO_MERGE: elevator says don't/can't merge. */ 2996 default: 2997 ; 2998 } 2999 3000get_rq: 3001 /* 3002 * This sync check and mask will be re-done in init_request_from_bio(), 3003 * but we need to set it earlier to expose the sync flag to the 3004 * rq allocator and io schedulers. 3005 */ 3006 rw_flags = bio_data_dir(bio); 3007 if (sync) 3008 rw_flags |= REQ_RW_SYNC; 3009 3010 /* 3011 * Grab a free request. This is might sleep but can not fail. 3012 * Returns with the queue unlocked. 3013 */ 3014 req = get_request_wait(q, rw_flags, bio); 3015 3016 /* 3017 * After dropping the lock and possibly sleeping here, our request 3018 * may now be mergeable after it had proven unmergeable (above). 3019 * We don't worry about that case for efficiency. It won't happen 3020 * often, and the elevators are able to handle it. 3021 */ 3022 init_request_from_bio(req, bio); 3023 3024 spin_lock_irq(q->queue_lock); 3025 if (elv_queue_empty(q)) 3026 blk_plug_device(q); 3027 add_request(q, req); 3028out: 3029 if (sync) 3030 __generic_unplug_device(q); 3031 3032 spin_unlock_irq(q->queue_lock); 3033 return 0; 3034 3035end_io: 3036 bio_endio(bio, nr_sectors << 9, err); 3037 return 0; 3038} 3039 3040/* 3041 * If bio->bi_dev is a partition, remap the location 3042 */ 3043static inline void blk_partition_remap(struct bio *bio) 3044{ 3045 struct block_device *bdev = bio->bi_bdev; 3046 3047 if (bdev != bdev->bd_contains) { 3048 struct hd_struct *p = bdev->bd_part; 3049 const int rw = bio_data_dir(bio); 3050 3051 p->sectors[rw] += bio_sectors(bio); 3052 p->ios[rw]++; 3053 3054 bio->bi_sector += p->start_sect; 3055 bio->bi_bdev = bdev->bd_contains; 3056 } 3057} 3058 3059static void handle_bad_sector(struct bio *bio) 3060{ 3061 char b[BDEVNAME_SIZE]; 3062 3063 printk(KERN_INFO "attempt to access beyond end of device\n"); 3064 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n", 3065 bdevname(bio->bi_bdev, b), 3066 bio->bi_rw, 3067 (unsigned long long)bio->bi_sector + bio_sectors(bio), 3068 (long long)(bio->bi_bdev->bd_inode->i_size >> 9)); 3069 3070 set_bit(BIO_EOF, &bio->bi_flags); 3071} 3072 3073#ifdef CONFIG_FAIL_MAKE_REQUEST 3074 3075static DECLARE_FAULT_ATTR(fail_make_request); 3076 3077static int __init setup_fail_make_request(char *str) 3078{ 3079 return setup_fault_attr(&fail_make_request, str); 3080} 3081__setup("fail_make_request=", setup_fail_make_request); 3082 3083static int should_fail_request(struct bio *bio) 3084{ 3085 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) || 3086 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail)) 3087 return should_fail(&fail_make_request, bio->bi_size); 3088 3089 return 0; 3090} 3091 3092static int __init fail_make_request_debugfs(void) 3093{ 3094 return init_fault_attr_dentries(&fail_make_request, 3095 "fail_make_request"); 3096} 3097 3098late_initcall(fail_make_request_debugfs); 3099 3100#else /* CONFIG_FAIL_MAKE_REQUEST */ 3101 3102static inline int should_fail_request(struct bio *bio) 3103{ 3104 return 0; 3105} 3106 3107#endif /* CONFIG_FAIL_MAKE_REQUEST */ 3108 3109/** 3110 * generic_make_request: hand a buffer to its device driver for I/O 3111 * @bio: The bio describing the location in memory and on the device. 3112 * 3113 * generic_make_request() is used to make I/O requests of block 3114 * devices. It is passed a &struct bio, which describes the I/O that needs 3115 * to be done. 3116 * 3117 * generic_make_request() does not return any status. The 3118 * success/failure status of the request, along with notification of 3119 * completion, is delivered asynchronously through the bio->bi_end_io 3120 * function described (one day) else where. 3121 * 3122 * The caller of generic_make_request must make sure that bi_io_vec 3123 * are set to describe the memory buffer, and that bi_dev and bi_sector are 3124 * set to describe the device address, and the 3125 * bi_end_io and optionally bi_private are set to describe how 3126 * completion notification should be signaled. 3127 * 3128 * generic_make_request and the drivers it calls may use bi_next if this 3129 * bio happens to be merged with someone else, and may change bi_dev and 3130 * bi_sector for remaps as it sees fit. So the values of these fields 3131 * should NOT be depended on after the call to generic_make_request. 3132 */ 3133static inline void __generic_make_request(struct bio *bio) 3134{ 3135 request_queue_t *q; 3136 sector_t maxsector; 3137 sector_t old_sector; 3138 int ret, nr_sectors = bio_sectors(bio); 3139 dev_t old_dev; 3140 3141 might_sleep(); 3142 /* Test device or partition size, when known. */ 3143 maxsector = bio->bi_bdev->bd_inode->i_size >> 9; 3144 if (maxsector) { 3145 sector_t sector = bio->bi_sector; 3146 3147 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) { 3148 /* 3149 * This may well happen - the kernel calls bread() 3150 * without checking the size of the device, e.g., when 3151 * mounting a device. 3152 */ 3153 handle_bad_sector(bio); 3154 goto end_io; 3155 } 3156 } 3157 3158 /* 3159 * Resolve the mapping until finished. (drivers are 3160 * still free to implement/resolve their own stacking 3161 * by explicitly returning 0) 3162 * 3163 * NOTE: we don't repeat the blk_size check for each new device. 3164 * Stacking drivers are expected to know what they are doing. 3165 */ 3166 old_sector = -1; 3167 old_dev = 0; 3168 do { 3169 char b[BDEVNAME_SIZE]; 3170 3171 q = bdev_get_queue(bio->bi_bdev); 3172 if (!q) { 3173 printk(KERN_ERR 3174 "generic_make_request: Trying to access " 3175 "nonexistent block-device %s (%Lu)\n", 3176 bdevname(bio->bi_bdev, b), 3177 (long long) bio->bi_sector); 3178end_io: 3179 bio_endio(bio, bio->bi_size, -EIO); 3180 break; 3181 } 3182 3183 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) { 3184 printk("bio too big device %s (%u > %u)\n", 3185 bdevname(bio->bi_bdev, b), 3186 bio_sectors(bio), 3187 q->max_hw_sectors); 3188 goto end_io; 3189 } 3190 3191 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) 3192 goto end_io; 3193 3194 if (should_fail_request(bio)) 3195 goto end_io; 3196 3197 /* 3198 * If this device has partitions, remap block n 3199 * of partition p to block n+start(p) of the disk. 3200 */ 3201 blk_partition_remap(bio); 3202 3203 if (old_sector != -1) 3204 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector, 3205 old_sector); 3206 3207 blk_add_trace_bio(q, bio, BLK_TA_QUEUE); 3208 3209 old_sector = bio->bi_sector; 3210 old_dev = bio->bi_bdev->bd_dev; 3211 3212 maxsector = bio->bi_bdev->bd_inode->i_size >> 9; 3213 if (maxsector) { 3214 sector_t sector = bio->bi_sector; 3215 3216 if (maxsector < nr_sectors || 3217 maxsector - nr_sectors < sector) { 3218 /* 3219 * This may well happen - partitions are not 3220 * checked to make sure they are within the size 3221 * of the whole device. 3222 */ 3223 handle_bad_sector(bio); 3224 goto end_io; 3225 } 3226 } 3227 3228 ret = q->make_request_fn(q, bio); 3229 } while (ret); 3230} 3231 3232/* 3233 * We only want one ->make_request_fn to be active at a time, 3234 * else stack usage with stacked devices could be a problem. 3235 * So use current->bio_{list,tail} to keep a list of requests 3236 * submited by a make_request_fn function. 3237 * current->bio_tail is also used as a flag to say if 3238 * generic_make_request is currently active in this task or not. 3239 * If it is NULL, then no make_request is active. If it is non-NULL, 3240 * then a make_request is active, and new requests should be added 3241 * at the tail 3242 */ 3243void generic_make_request(struct bio *bio) 3244{ 3245 if (current->bio_tail) { 3246 /* make_request is active */ 3247 *(current->bio_tail) = bio; 3248 bio->bi_next = NULL; 3249 current->bio_tail = &bio->bi_next; 3250 return; 3251 } 3252 /* following loop may be a bit non-obvious, and so deserves some 3253 * explanation. 3254 * Before entering the loop, bio->bi_next is NULL (as all callers 3255 * ensure that) so we have a list with a single bio. 3256 * We pretend that we have just taken it off a longer list, so 3257 * we assign bio_list to the next (which is NULL) and bio_tail 3258 * to &bio_list, thus initialising the bio_list of new bios to be 3259 * added. __generic_make_request may indeed add some more bios 3260 * through a recursive call to generic_make_request. If it 3261 * did, we find a non-NULL value in bio_list and re-enter the loop 3262 * from the top. In this case we really did just take the bio 3263 * of the top of the list (no pretending) and so fixup bio_list and 3264 * bio_tail or bi_next, and call into __generic_make_request again. 3265 * 3266 * The loop was structured like this to make only one call to 3267 * __generic_make_request (which is important as it is large and 3268 * inlined) and to keep the structure simple. 3269 */ 3270 BUG_ON(bio->bi_next); 3271 do { 3272 current->bio_list = bio->bi_next; 3273 if (bio->bi_next == NULL) 3274 current->bio_tail = ¤t->bio_list; 3275 else 3276 bio->bi_next = NULL; 3277 __generic_make_request(bio); 3278 bio = current->bio_list; 3279 } while (bio); 3280 current->bio_tail = NULL; /* deactivate */ 3281} 3282 3283EXPORT_SYMBOL(generic_make_request); 3284 3285/** 3286 * submit_bio: submit a bio to the block device layer for I/O 3287 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead) 3288 * @bio: The &struct bio which describes the I/O 3289 * 3290 * submit_bio() is very similar in purpose to generic_make_request(), and 3291 * uses that function to do most of the work. Both are fairly rough 3292 * interfaces, @bio must be presetup and ready for I/O. 3293 * 3294 */ 3295void submit_bio(int rw, struct bio *bio) 3296{ 3297 int count = bio_sectors(bio); 3298 3299 BIO_BUG_ON(!bio->bi_size); 3300 BIO_BUG_ON(!bio->bi_io_vec); 3301 bio->bi_rw |= rw; 3302 if (rw & WRITE) { 3303 count_vm_events(PGPGOUT, count); 3304 } else { 3305 task_io_account_read(bio->bi_size); 3306 count_vm_events(PGPGIN, count); 3307 } 3308 3309 if (unlikely(block_dump)) { 3310 char b[BDEVNAME_SIZE]; 3311 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n", 3312 current->comm, current->pid, 3313 (rw & WRITE) ? "WRITE" : "READ", 3314 (unsigned long long)bio->bi_sector, 3315 bdevname(bio->bi_bdev,b)); 3316 } 3317 3318 generic_make_request(bio); 3319} 3320 3321EXPORT_SYMBOL(submit_bio); 3322 3323static void blk_recalc_rq_segments(struct request *rq) 3324{ 3325 struct bio *bio, *prevbio = NULL; 3326 int nr_phys_segs, nr_hw_segs; 3327 unsigned int phys_size, hw_size; 3328 request_queue_t *q = rq->q; 3329 3330 if (!rq->bio) 3331 return; 3332 3333 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0; 3334 rq_for_each_bio(bio, rq) { 3335 /* Force bio hw/phys segs to be recalculated. */ 3336 bio->bi_flags &= ~(1 << BIO_SEG_VALID); 3337 3338 nr_phys_segs += bio_phys_segments(q, bio); 3339 nr_hw_segs += bio_hw_segments(q, bio); 3340 if (prevbio) { 3341 int pseg = phys_size + prevbio->bi_size + bio->bi_size; 3342 int hseg = hw_size + prevbio->bi_size + bio->bi_size; 3343 3344 if (blk_phys_contig_segment(q, prevbio, bio) && 3345 pseg <= q->max_segment_size) { 3346 nr_phys_segs--; 3347 phys_size += prevbio->bi_size + bio->bi_size; 3348 } else 3349 phys_size = 0; 3350 3351 if (blk_hw_contig_segment(q, prevbio, bio) && 3352 hseg <= q->max_segment_size) { 3353 nr_hw_segs--; 3354 hw_size += prevbio->bi_size + bio->bi_size; 3355 } else 3356 hw_size = 0; 3357 } 3358 prevbio = bio; 3359 } 3360 3361 rq->nr_phys_segments = nr_phys_segs; 3362 rq->nr_hw_segments = nr_hw_segs; 3363} 3364 3365static void blk_recalc_rq_sectors(struct request *rq, int nsect) 3366{ 3367 if (blk_fs_request(rq)) { 3368 rq->hard_sector += nsect; 3369 rq->hard_nr_sectors -= nsect; 3370 3371 /* 3372 * Move the I/O submission pointers ahead if required. 3373 */ 3374 if ((rq->nr_sectors >= rq->hard_nr_sectors) && 3375 (rq->sector <= rq->hard_sector)) { 3376 rq->sector = rq->hard_sector; 3377 rq->nr_sectors = rq->hard_nr_sectors; 3378 rq->hard_cur_sectors = bio_cur_sectors(rq->bio); 3379 rq->current_nr_sectors = rq->hard_cur_sectors; 3380 rq->buffer = bio_data(rq->bio); 3381 } 3382 3383 /* 3384 * if total number of sectors is less than the first segment 3385 * size, something has gone terribly wrong 3386 */ 3387 if (rq->nr_sectors < rq->current_nr_sectors) { 3388 printk("blk: request botched\n"); 3389 rq->nr_sectors = rq->current_nr_sectors; 3390 } 3391 } 3392} 3393 3394static int __end_that_request_first(struct request *req, int uptodate, 3395 int nr_bytes) 3396{ 3397 int total_bytes, bio_nbytes, error, next_idx = 0; 3398 struct bio *bio; 3399 3400 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE); 3401 3402 /* 3403 * extend uptodate bool to allow < 0 value to be direct io error 3404 */ 3405 error = 0; 3406 if (end_io_error(uptodate)) 3407 error = !uptodate ? -EIO : uptodate; 3408 3409 /* 3410 * for a REQ_BLOCK_PC request, we want to carry any eventual 3411 * sense key with us all the way through 3412 */ 3413 if (!blk_pc_request(req)) 3414 req->errors = 0; 3415 3416 if (!uptodate) { 3417 if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET)) 3418 printk("end_request: I/O error, dev %s, sector %llu\n", 3419 req->rq_disk ? req->rq_disk->disk_name : "?", 3420 (unsigned long long)req->sector); 3421 } 3422 3423 if (blk_fs_request(req) && req->rq_disk) { 3424 const int rw = rq_data_dir(req); 3425 3426 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9); 3427 } 3428 3429 total_bytes = bio_nbytes = 0; 3430 while ((bio = req->bio) != NULL) { 3431 int nbytes; 3432 3433 if (nr_bytes >= bio->bi_size) { 3434 req->bio = bio->bi_next; 3435 nbytes = bio->bi_size; 3436 if (!ordered_bio_endio(req, bio, nbytes, error)) 3437 bio_endio(bio, nbytes, error); 3438 next_idx = 0; 3439 bio_nbytes = 0; 3440 } else { 3441 int idx = bio->bi_idx + next_idx; 3442 3443 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) { 3444 blk_dump_rq_flags(req, "__end_that"); 3445 printk("%s: bio idx %d >= vcnt %d\n", 3446 __FUNCTION__, 3447 bio->bi_idx, bio->bi_vcnt); 3448 break; 3449 } 3450 3451 nbytes = bio_iovec_idx(bio, idx)->bv_len; 3452 BIO_BUG_ON(nbytes > bio->bi_size); 3453 3454 /* 3455 * not a complete bvec done 3456 */ 3457 if (unlikely(nbytes > nr_bytes)) { 3458 bio_nbytes += nr_bytes; 3459 total_bytes += nr_bytes; 3460 break; 3461 } 3462 3463 /* 3464 * advance to the next vector 3465 */ 3466 next_idx++; 3467 bio_nbytes += nbytes; 3468 } 3469 3470 total_bytes += nbytes; 3471 nr_bytes -= nbytes; 3472 3473 if ((bio = req->bio)) { 3474 /* 3475 * end more in this run, or just return 'not-done' 3476 */ 3477 if (unlikely(nr_bytes <= 0)) 3478 break; 3479 } 3480 } 3481 3482 /* 3483 * completely done 3484 */ 3485 if (!req->bio) 3486 return 0; 3487 3488 /* 3489 * if the request wasn't completed, update state 3490 */ 3491 if (bio_nbytes) { 3492 if (!ordered_bio_endio(req, bio, bio_nbytes, error)) 3493 bio_endio(bio, bio_nbytes, error); 3494 bio->bi_idx += next_idx; 3495 bio_iovec(bio)->bv_offset += nr_bytes; 3496 bio_iovec(bio)->bv_len -= nr_bytes; 3497 } 3498 3499 blk_recalc_rq_sectors(req, total_bytes >> 9); 3500 blk_recalc_rq_segments(req); 3501 return 1; 3502} 3503 3504/** 3505 * end_that_request_first - end I/O on a request 3506 * @req: the request being processed 3507 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error 3508 * @nr_sectors: number of sectors to end I/O on 3509 * 3510 * Description: 3511 * Ends I/O on a number of sectors attached to @req, and sets it up 3512 * for the next range of segments (if any) in the cluster. 3513 * 3514 * Return: 3515 * 0 - we are done with this request, call end_that_request_last() 3516 * 1 - still buffers pending for this request 3517 **/ 3518int end_that_request_first(struct request *req, int uptodate, int nr_sectors) 3519{ 3520 return __end_that_request_first(req, uptodate, nr_sectors << 9); 3521} 3522 3523EXPORT_SYMBOL(end_that_request_first); 3524 3525/** 3526 * end_that_request_chunk - end I/O on a request 3527 * @req: the request being processed 3528 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error 3529 * @nr_bytes: number of bytes to complete 3530 * 3531 * Description: 3532 * Ends I/O on a number of bytes attached to @req, and sets it up 3533 * for the next range of segments (if any). Like end_that_request_first(), 3534 * but deals with bytes instead of sectors. 3535 * 3536 * Return: 3537 * 0 - we are done with this request, call end_that_request_last() 3538 * 1 - still buffers pending for this request 3539 **/ 3540int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes) 3541{ 3542 return __end_that_request_first(req, uptodate, nr_bytes); 3543} 3544 3545EXPORT_SYMBOL(end_that_request_chunk); 3546 3547/* 3548 * splice the completion data to a local structure and hand off to 3549 * process_completion_queue() to complete the requests 3550 */ 3551static void blk_done_softirq(struct softirq_action *h) 3552{ 3553 struct list_head *cpu_list, local_list; 3554 3555 local_irq_disable(); 3556 cpu_list = &__get_cpu_var(blk_cpu_done); 3557 list_replace_init(cpu_list, &local_list); 3558 local_irq_enable(); 3559 3560 while (!list_empty(&local_list)) { 3561 struct request *rq = list_entry(local_list.next, struct request, donelist); 3562 3563 list_del_init(&rq->donelist); 3564 rq->q->softirq_done_fn(rq); 3565 } 3566} 3567 3568static int blk_cpu_notify(struct notifier_block *self, unsigned long action, 3569 void *hcpu) 3570{ 3571 /* 3572 * If a CPU goes away, splice its entries to the current CPU 3573 * and trigger a run of the softirq 3574 */ 3575 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) { 3576 int cpu = (unsigned long) hcpu; 3577 3578 local_irq_disable(); 3579 list_splice_init(&per_cpu(blk_cpu_done, cpu), 3580 &__get_cpu_var(blk_cpu_done)); 3581 raise_softirq_irqoff(BLOCK_SOFTIRQ); 3582 local_irq_enable(); 3583 } 3584 3585 return NOTIFY_OK; 3586} 3587 3588 3589static struct notifier_block __devinitdata blk_cpu_notifier = { 3590 .notifier_call = blk_cpu_notify, 3591}; 3592 3593/** 3594 * blk_complete_request - end I/O on a request 3595 * @req: the request being processed 3596 * 3597 * Description: 3598 * Ends all I/O on a request. It does not handle partial completions, 3599 * unless the driver actually implements this in its completion callback 3600 * through requeueing. Theh actual completion happens out-of-order, 3601 * through a softirq handler. The user must have registered a completion 3602 * callback through blk_queue_softirq_done(). 3603 **/ 3604 3605void blk_complete_request(struct request *req) 3606{ 3607 struct list_head *cpu_list; 3608 unsigned long flags; 3609 3610 BUG_ON(!req->q->softirq_done_fn); 3611 3612 local_irq_save(flags); 3613 3614 cpu_list = &__get_cpu_var(blk_cpu_done); 3615 list_add_tail(&req->donelist, cpu_list); 3616 raise_softirq_irqoff(BLOCK_SOFTIRQ); 3617 3618 local_irq_restore(flags); 3619} 3620 3621EXPORT_SYMBOL(blk_complete_request); 3622 3623/* 3624 * queue lock must be held 3625 */ 3626void end_that_request_last(struct request *req, int uptodate) 3627{ 3628 struct gendisk *disk = req->rq_disk; 3629 int error; 3630 3631 /* 3632 * extend uptodate bool to allow < 0 value to be direct io error 3633 */ 3634 error = 0; 3635 if (end_io_error(uptodate)) 3636 error = !uptodate ? -EIO : uptodate; 3637 3638 if (unlikely(laptop_mode) && blk_fs_request(req)) 3639 laptop_io_completion(); 3640 3641 /* 3642 * Account IO completion. bar_rq isn't accounted as a normal 3643 * IO on queueing nor completion. Accounting the containing 3644 * request is enough. 3645 */ 3646 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) { 3647 unsigned long duration = jiffies - req->start_time; 3648 const int rw = rq_data_dir(req); 3649 3650 __disk_stat_inc(disk, ios[rw]); 3651 __disk_stat_add(disk, ticks[rw], duration); 3652 disk_round_stats(disk); 3653 disk->in_flight--; 3654 } 3655 if (req->end_io) 3656 req->end_io(req, error); 3657 else 3658 __blk_put_request(req->q, req); 3659} 3660 3661EXPORT_SYMBOL(end_that_request_last); 3662 3663void end_request(struct request *req, int uptodate) 3664{ 3665 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) { 3666 add_disk_randomness(req->rq_disk); 3667 blkdev_dequeue_request(req); 3668 end_that_request_last(req, uptodate); 3669 } 3670} 3671 3672EXPORT_SYMBOL(end_request); 3673 3674void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio) 3675{ 3676 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */ 3677 rq->cmd_flags |= (bio->bi_rw & 3); 3678 3679 rq->nr_phys_segments = bio_phys_segments(q, bio); 3680 rq->nr_hw_segments = bio_hw_segments(q, bio); 3681 rq->current_nr_sectors = bio_cur_sectors(bio); 3682 rq->hard_cur_sectors = rq->current_nr_sectors; 3683 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio); 3684 rq->buffer = bio_data(bio); 3685 rq->data_len = bio->bi_size; 3686 3687 rq->bio = rq->biotail = bio; 3688} 3689 3690EXPORT_SYMBOL(blk_rq_bio_prep); 3691 3692int kblockd_schedule_work(struct work_struct *work) 3693{ 3694 return queue_work(kblockd_workqueue, work); 3695} 3696 3697EXPORT_SYMBOL(kblockd_schedule_work); 3698 3699void kblockd_flush_work(struct work_struct *work) 3700{ 3701 cancel_work_sync(work); 3702} 3703EXPORT_SYMBOL(kblockd_flush_work); 3704 3705int __init blk_dev_init(void) 3706{ 3707 int i; 3708 3709 kblockd_workqueue = create_workqueue("kblockd"); 3710 if (!kblockd_workqueue) 3711 panic("Failed to create kblockd\n"); 3712 3713 request_cachep = kmem_cache_create("blkdev_requests", 3714 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL); 3715 3716 requestq_cachep = kmem_cache_create("blkdev_queue", 3717 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL); 3718 3719 iocontext_cachep = kmem_cache_create("blkdev_ioc", 3720 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL); 3721 3722 for_each_possible_cpu(i) 3723 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i)); 3724 3725 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL); 3726 register_hotcpu_notifier(&blk_cpu_notifier); 3727 3728 blk_max_low_pfn = max_low_pfn - 1; 3729 blk_max_pfn = max_pfn - 1; 3730 3731 return 0; 3732} 3733 3734/* 3735 * IO Context helper functions 3736 */ 3737void put_io_context(struct io_context *ioc) 3738{ 3739 if (ioc == NULL) 3740 return; 3741 3742 BUG_ON(atomic_read(&ioc->refcount) == 0); 3743 3744 if (atomic_dec_and_test(&ioc->refcount)) { 3745 struct cfq_io_context *cic; 3746 3747 rcu_read_lock(); 3748 if (ioc->aic && ioc->aic->dtor) 3749 ioc->aic->dtor(ioc->aic); 3750 if (ioc->cic_root.rb_node != NULL) { 3751 struct rb_node *n = rb_first(&ioc->cic_root); 3752 3753 cic = rb_entry(n, struct cfq_io_context, rb_node); 3754 cic->dtor(ioc); 3755 } 3756 rcu_read_unlock(); 3757 3758 kmem_cache_free(iocontext_cachep, ioc); 3759 } 3760} 3761EXPORT_SYMBOL(put_io_context); 3762 3763/* Called by the exitting task */ 3764void exit_io_context(void) 3765{ 3766 struct io_context *ioc; 3767 struct cfq_io_context *cic; 3768 3769 task_lock(current); 3770 ioc = current->io_context; 3771 current->io_context = NULL; 3772 task_unlock(current); 3773 3774 ioc->task = NULL; 3775 if (ioc->aic && ioc->aic->exit) 3776 ioc->aic->exit(ioc->aic); 3777 if (ioc->cic_root.rb_node != NULL) { 3778 cic = rb_entry(rb_first(&ioc->cic_root), struct cfq_io_context, rb_node); 3779 cic->exit(ioc); 3780 } 3781 3782 put_io_context(ioc); 3783} 3784 3785/* 3786 * If the current task has no IO context then create one and initialise it. 3787 * Otherwise, return its existing IO context. 3788 * 3789 * This returned IO context doesn't have a specifically elevated refcount, 3790 * but since the current task itself holds a reference, the context can be 3791 * used in general code, so long as it stays within `current` context. 3792 */ 3793static struct io_context *current_io_context(gfp_t gfp_flags, int node) 3794{ 3795 struct task_struct *tsk = current; 3796 struct io_context *ret; 3797 3798 ret = tsk->io_context; 3799 if (likely(ret)) 3800 return ret; 3801 3802 ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node); 3803 if (ret) { 3804 atomic_set(&ret->refcount, 1); 3805 ret->task = current; 3806 ret->ioprio_changed = 0; 3807 ret->last_waited = jiffies; /* doesn't matter... */ 3808 ret->nr_batch_requests = 0; /* because this is 0 */ 3809 ret->aic = NULL; 3810 ret->cic_root.rb_node = NULL; 3811 ret->ioc_data = NULL; 3812 /* make sure set_task_ioprio() sees the settings above */ 3813 smp_wmb(); 3814 tsk->io_context = ret; 3815 } 3816 3817 return ret; 3818} 3819 3820/* 3821 * If the current task has no IO context then create one and initialise it. 3822 * If it does have a context, take a ref on it. 3823 * 3824 * This is always called in the context of the task which submitted the I/O. 3825 */ 3826struct io_context *get_io_context(gfp_t gfp_flags, int node) 3827{ 3828 struct io_context *ret; 3829 ret = current_io_context(gfp_flags, node); 3830 if (likely(ret)) 3831 atomic_inc(&ret->refcount); 3832 return ret; 3833} 3834EXPORT_SYMBOL(get_io_context); 3835 3836void copy_io_context(struct io_context **pdst, struct io_context **psrc) 3837{ 3838 struct io_context *src = *psrc; 3839 struct io_context *dst = *pdst; 3840 3841 if (src) { 3842 BUG_ON(atomic_read(&src->refcount) == 0); 3843 atomic_inc(&src->refcount); 3844 put_io_context(dst); 3845 *pdst = src; 3846 } 3847} 3848EXPORT_SYMBOL(copy_io_context); 3849 3850void swap_io_context(struct io_context **ioc1, struct io_context **ioc2) 3851{ 3852 struct io_context *temp; 3853 temp = *ioc1; 3854 *ioc1 = *ioc2; 3855 *ioc2 = temp; 3856} 3857EXPORT_SYMBOL(swap_io_context); 3858 3859/* 3860 * sysfs parts below 3861 */ 3862struct queue_sysfs_entry { 3863 struct attribute attr; 3864 ssize_t (*show)(struct request_queue *, char *); 3865 ssize_t (*store)(struct request_queue *, const char *, size_t); 3866}; 3867 3868static ssize_t 3869queue_var_show(unsigned int var, char *page) 3870{ 3871 return sprintf(page, "%d\n", var); 3872} 3873 3874static ssize_t 3875queue_var_store(unsigned long *var, const char *page, size_t count) 3876{ 3877 char *p = (char *) page; 3878 3879 *var = simple_strtoul(p, &p, 10); 3880 return count; 3881} 3882 3883static ssize_t queue_requests_show(struct request_queue *q, char *page) 3884{ 3885 return queue_var_show(q->nr_requests, (page)); 3886} 3887 3888static ssize_t 3889queue_requests_store(struct request_queue *q, const char *page, size_t count) 3890{ 3891 struct request_list *rl = &q->rq; 3892 unsigned long nr; 3893 int ret = queue_var_store(&nr, page, count); 3894 if (nr < BLKDEV_MIN_RQ) 3895 nr = BLKDEV_MIN_RQ; 3896 3897 spin_lock_irq(q->queue_lock); 3898 q->nr_requests = nr; 3899 blk_queue_congestion_threshold(q); 3900 3901 if (rl->count[READ] >= queue_congestion_on_threshold(q)) 3902 blk_set_queue_congested(q, READ); 3903 else if (rl->count[READ] < queue_congestion_off_threshold(q)) 3904 blk_clear_queue_congested(q, READ); 3905 3906 if (rl->count[WRITE] >= queue_congestion_on_threshold(q)) 3907 blk_set_queue_congested(q, WRITE); 3908 else if (rl->count[WRITE] < queue_congestion_off_threshold(q)) 3909 blk_clear_queue_congested(q, WRITE); 3910 3911 if (rl->count[READ] >= q->nr_requests) { 3912 blk_set_queue_full(q, READ); 3913 } else if (rl->count[READ]+1 <= q->nr_requests) { 3914 blk_clear_queue_full(q, READ); 3915 wake_up(&rl->wait[READ]); 3916 } 3917 3918 if (rl->count[WRITE] >= q->nr_requests) { 3919 blk_set_queue_full(q, WRITE); 3920 } else if (rl->count[WRITE]+1 <= q->nr_requests) { 3921 blk_clear_queue_full(q, WRITE); 3922 wake_up(&rl->wait[WRITE]); 3923 } 3924 spin_unlock_irq(q->queue_lock); 3925 return ret; 3926} 3927 3928static ssize_t queue_ra_show(struct request_queue *q, char *page) 3929{ 3930 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10); 3931 3932 return queue_var_show(ra_kb, (page)); 3933} 3934 3935static ssize_t 3936queue_ra_store(struct request_queue *q, const char *page, size_t count) 3937{ 3938 unsigned long ra_kb; 3939 ssize_t ret = queue_var_store(&ra_kb, page, count); 3940 3941 spin_lock_irq(q->queue_lock); 3942 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); 3943 spin_unlock_irq(q->queue_lock); 3944 3945 return ret; 3946} 3947 3948static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) 3949{ 3950 int max_sectors_kb = q->max_sectors >> 1; 3951 3952 return queue_var_show(max_sectors_kb, (page)); 3953} 3954 3955static ssize_t 3956queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) 3957{ 3958 unsigned long max_sectors_kb, 3959 max_hw_sectors_kb = q->max_hw_sectors >> 1, 3960 page_kb = 1 << (PAGE_CACHE_SHIFT - 10); 3961 ssize_t ret = queue_var_store(&max_sectors_kb, page, count); 3962 int ra_kb; 3963 3964 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) 3965 return -EINVAL; 3966 /* 3967 * Take the queue lock to update the readahead and max_sectors 3968 * values synchronously: 3969 */ 3970 spin_lock_irq(q->queue_lock); 3971 /* 3972 * Trim readahead window as well, if necessary: 3973 */ 3974 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10); 3975 if (ra_kb > max_sectors_kb) 3976 q->backing_dev_info.ra_pages = 3977 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10); 3978 3979 q->max_sectors = max_sectors_kb << 1; 3980 spin_unlock_irq(q->queue_lock); 3981 3982 return ret; 3983} 3984 3985static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) 3986{ 3987 int max_hw_sectors_kb = q->max_hw_sectors >> 1; 3988 3989 return queue_var_show(max_hw_sectors_kb, (page)); 3990} 3991 3992 3993static struct queue_sysfs_entry queue_requests_entry = { 3994 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 3995 .show = queue_requests_show, 3996 .store = queue_requests_store, 3997}; 3998 3999static struct queue_sysfs_entry queue_ra_entry = { 4000 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, 4001 .show = queue_ra_show, 4002 .store = queue_ra_store, 4003}; 4004 4005static struct queue_sysfs_entry queue_max_sectors_entry = { 4006 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, 4007 .show = queue_max_sectors_show, 4008 .store = queue_max_sectors_store, 4009}; 4010 4011static struct queue_sysfs_entry queue_max_hw_sectors_entry = { 4012 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, 4013 .show = queue_max_hw_sectors_show, 4014}; 4015 4016static struct queue_sysfs_entry queue_iosched_entry = { 4017 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, 4018 .show = elv_iosched_show, 4019 .store = elv_iosched_store, 4020}; 4021 4022static struct attribute *default_attrs[] = { 4023 &queue_requests_entry.attr, 4024 &queue_ra_entry.attr, 4025 &queue_max_hw_sectors_entry.attr, 4026 &queue_max_sectors_entry.attr, 4027 &queue_iosched_entry.attr, 4028 NULL, 4029}; 4030 4031#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) 4032 4033static ssize_t 4034queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 4035{ 4036 struct queue_sysfs_entry *entry = to_queue(attr); 4037 request_queue_t *q = container_of(kobj, struct request_queue, kobj); 4038 ssize_t res; 4039 4040 if (!entry->show) 4041 return -EIO; 4042 mutex_lock(&q->sysfs_lock); 4043 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 4044 mutex_unlock(&q->sysfs_lock); 4045 return -ENOENT; 4046 } 4047 res = entry->show(q, page); 4048 mutex_unlock(&q->sysfs_lock); 4049 return res; 4050} 4051 4052static ssize_t 4053queue_attr_store(struct kobject *kobj, struct attribute *attr, 4054 const char *page, size_t length) 4055{ 4056 struct queue_sysfs_entry *entry = to_queue(attr); 4057 request_queue_t *q = container_of(kobj, struct request_queue, kobj); 4058 4059 ssize_t res; 4060 4061 if (!entry->store) 4062 return -EIO; 4063 mutex_lock(&q->sysfs_lock); 4064 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 4065 mutex_unlock(&q->sysfs_lock); 4066 return -ENOENT; 4067 } 4068 res = entry->store(q, page, length); 4069 mutex_unlock(&q->sysfs_lock); 4070 return res; 4071} 4072 4073static struct sysfs_ops queue_sysfs_ops = { 4074 .show = queue_attr_show, 4075 .store = queue_attr_store, 4076}; 4077 4078static struct kobj_type queue_ktype = { 4079 .sysfs_ops = &queue_sysfs_ops, 4080 .default_attrs = default_attrs, 4081 .release = blk_release_queue, 4082}; 4083 4084int blk_register_queue(struct gendisk *disk) 4085{ 4086 int ret; 4087 4088 request_queue_t *q = disk->queue; 4089 4090 if (!q || !q->request_fn) 4091 return -ENXIO; 4092 4093 q->kobj.parent = kobject_get(&disk->kobj); 4094 4095 ret = kobject_add(&q->kobj); 4096 if (ret < 0) 4097 return ret; 4098 4099 kobject_uevent(&q->kobj, KOBJ_ADD); 4100 4101 ret = elv_register_queue(q); 4102 if (ret) { 4103 kobject_uevent(&q->kobj, KOBJ_REMOVE); 4104 kobject_del(&q->kobj); 4105 return ret; 4106 } 4107 4108 return 0; 4109} 4110 4111void blk_unregister_queue(struct gendisk *disk) 4112{ 4113 request_queue_t *q = disk->queue; 4114 4115 if (q && q->request_fn) { 4116 elv_unregister_queue(q); 4117 4118 kobject_uevent(&q->kobj, KOBJ_REMOVE); 4119 kobject_del(&q->kobj); 4120 kobject_put(&disk->kobj); 4121 } 4122} 4123