1 /* 2 * Block device elevator/IO-scheduler. 3 * 4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE 5 * 6 * 30042000 Jens Axboe <axboe@kernel.dk> : 7 * 8 * Split the elevator a bit so that it is possible to choose a different 9 * one or even write a new "plug in". There are three pieces: 10 * - elevator_fn, inserts a new request in the queue list 11 * - elevator_merge_fn, decides whether a new buffer can be merged with 12 * an existing request 13 * - elevator_dequeue_fn, called when a request is taken off the active list 14 * 15 * 20082000 Dave Jones <davej@suse.de> : 16 * Removed tests for max-bomb-segments, which was breaking elvtune 17 * when run without -bN 18 * 19 * Jens: 20 * - Rework again to work with bio instead of buffer_heads 21 * - loose bi_dev comparisons, partition handling is right now 22 * - completely modularize elevator setup and teardown 23 * 24 */ 25 #include <linux/kernel.h> 26 #include <linux/fs.h> 27 #include <linux/blkdev.h> 28 #include <linux/elevator.h> 29 #include <linux/bio.h> 30 #include <linux/module.h> 31 #include <linux/slab.h> 32 #include <linux/init.h> 33 #include <linux/compiler.h> 34 #include <linux/delay.h> 35 #include <linux/blktrace_api.h> 36 #include <linux/hash.h> 37 #include <linux/uaccess.h> 38 39 #include <trace/events/block.h> 40 41 #include "blk.h" 42 43 static DEFINE_SPINLOCK(elv_list_lock); 44 static LIST_HEAD(elv_list); 45 46 /* 47 * Merge hash stuff. 48 */ 49 static const int elv_hash_shift = 6; 50 #define ELV_HASH_BLOCK(sec) ((sec) >> 3) 51 #define ELV_HASH_FN(sec) \ 52 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift)) 53 #define ELV_HASH_ENTRIES (1 << elv_hash_shift) 54 #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq)) 55 56 /* 57 * Query io scheduler to see if the current process issuing bio may be 58 * merged with rq. 59 */ 60 static int elv_iosched_allow_merge(struct request *rq, struct bio *bio) 61 { 62 struct request_queue *q = rq->q; 63 struct elevator_queue *e = q->elevator; 64 65 if (e->ops->elevator_allow_merge_fn) 66 return e->ops->elevator_allow_merge_fn(q, rq, bio); 67 68 return 1; 69 } 70 71 /* 72 * can we safely merge with this request? 73 */ 74 int elv_rq_merge_ok(struct request *rq, struct bio *bio) 75 { 76 if (!rq_mergeable(rq)) 77 return 0; 78 79 /* 80 * Don't merge file system requests and discard requests 81 */ 82 if (bio_discard(bio) != bio_discard(rq->bio)) 83 return 0; 84 85 /* 86 * different data direction or already started, don't merge 87 */ 88 if (bio_data_dir(bio) != rq_data_dir(rq)) 89 return 0; 90 91 /* 92 * must be same device and not a special request 93 */ 94 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special) 95 return 0; 96 97 /* 98 * only merge integrity protected bio into ditto rq 99 */ 100 if (bio_integrity(bio) != blk_integrity_rq(rq)) 101 return 0; 102 103 if (!elv_iosched_allow_merge(rq, bio)) 104 return 0; 105 106 return 1; 107 } 108 EXPORT_SYMBOL(elv_rq_merge_ok); 109 110 static inline int elv_try_merge(struct request *__rq, struct bio *bio) 111 { 112 int ret = ELEVATOR_NO_MERGE; 113 114 /* 115 * we can merge and sequence is ok, check if it's possible 116 */ 117 if (elv_rq_merge_ok(__rq, bio)) { 118 if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector) 119 ret = ELEVATOR_BACK_MERGE; 120 else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector) 121 ret = ELEVATOR_FRONT_MERGE; 122 } 123 124 return ret; 125 } 126 127 static struct elevator_type *elevator_find(const char *name) 128 { 129 struct elevator_type *e; 130 131 list_for_each_entry(e, &elv_list, list) { 132 if (!strcmp(e->elevator_name, name)) 133 return e; 134 } 135 136 return NULL; 137 } 138 139 static void elevator_put(struct elevator_type *e) 140 { 141 module_put(e->elevator_owner); 142 } 143 144 static struct elevator_type *elevator_get(const char *name) 145 { 146 struct elevator_type *e; 147 148 spin_lock(&elv_list_lock); 149 150 e = elevator_find(name); 151 if (!e) { 152 char elv[ELV_NAME_MAX + strlen("-iosched")]; 153 154 spin_unlock(&elv_list_lock); 155 156 if (!strcmp(name, "anticipatory")) 157 sprintf(elv, "as-iosched"); 158 else 159 sprintf(elv, "%s-iosched", name); 160 161 request_module("%s", elv); 162 spin_lock(&elv_list_lock); 163 e = elevator_find(name); 164 } 165 166 if (e && !try_module_get(e->elevator_owner)) 167 e = NULL; 168 169 spin_unlock(&elv_list_lock); 170 171 return e; 172 } 173 174 static void *elevator_init_queue(struct request_queue *q, 175 struct elevator_queue *eq) 176 { 177 return eq->ops->elevator_init_fn(q); 178 } 179 180 static void elevator_attach(struct request_queue *q, struct elevator_queue *eq, 181 void *data) 182 { 183 q->elevator = eq; 184 eq->elevator_data = data; 185 } 186 187 static char chosen_elevator[16]; 188 189 static int __init elevator_setup(char *str) 190 { 191 /* 192 * Be backwards-compatible with previous kernels, so users 193 * won't get the wrong elevator. 194 */ 195 if (!strcmp(str, "as")) 196 strcpy(chosen_elevator, "anticipatory"); 197 else 198 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); 199 return 1; 200 } 201 202 __setup("elevator=", elevator_setup); 203 204 static struct kobj_type elv_ktype; 205 206 static struct elevator_queue *elevator_alloc(struct request_queue *q, 207 struct elevator_type *e) 208 { 209 struct elevator_queue *eq; 210 int i; 211 212 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node); 213 if (unlikely(!eq)) 214 goto err; 215 216 eq->ops = &e->ops; 217 eq->elevator_type = e; 218 kobject_init(&eq->kobj, &elv_ktype); 219 mutex_init(&eq->sysfs_lock); 220 221 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES, 222 GFP_KERNEL, q->node); 223 if (!eq->hash) 224 goto err; 225 226 for (i = 0; i < ELV_HASH_ENTRIES; i++) 227 INIT_HLIST_HEAD(&eq->hash[i]); 228 229 return eq; 230 err: 231 kfree(eq); 232 elevator_put(e); 233 return NULL; 234 } 235 236 static void elevator_release(struct kobject *kobj) 237 { 238 struct elevator_queue *e; 239 240 e = container_of(kobj, struct elevator_queue, kobj); 241 elevator_put(e->elevator_type); 242 kfree(e->hash); 243 kfree(e); 244 } 245 246 int elevator_init(struct request_queue *q, char *name) 247 { 248 struct elevator_type *e = NULL; 249 struct elevator_queue *eq; 250 int ret = 0; 251 void *data; 252 253 INIT_LIST_HEAD(&q->queue_head); 254 q->last_merge = NULL; 255 q->end_sector = 0; 256 q->boundary_rq = NULL; 257 258 if (name) { 259 e = elevator_get(name); 260 if (!e) 261 return -EINVAL; 262 } 263 264 if (!e && *chosen_elevator) { 265 e = elevator_get(chosen_elevator); 266 if (!e) 267 printk(KERN_ERR "I/O scheduler %s not found\n", 268 chosen_elevator); 269 } 270 271 if (!e) { 272 e = elevator_get(CONFIG_DEFAULT_IOSCHED); 273 if (!e) { 274 printk(KERN_ERR 275 "Default I/O scheduler not found. " \ 276 "Using noop.\n"); 277 e = elevator_get("noop"); 278 } 279 } 280 281 eq = elevator_alloc(q, e); 282 if (!eq) 283 return -ENOMEM; 284 285 data = elevator_init_queue(q, eq); 286 if (!data) { 287 kobject_put(&eq->kobj); 288 return -ENOMEM; 289 } 290 291 elevator_attach(q, eq, data); 292 return ret; 293 } 294 EXPORT_SYMBOL(elevator_init); 295 296 void elevator_exit(struct elevator_queue *e) 297 { 298 mutex_lock(&e->sysfs_lock); 299 if (e->ops->elevator_exit_fn) 300 e->ops->elevator_exit_fn(e); 301 e->ops = NULL; 302 mutex_unlock(&e->sysfs_lock); 303 304 kobject_put(&e->kobj); 305 } 306 EXPORT_SYMBOL(elevator_exit); 307 308 static inline void __elv_rqhash_del(struct request *rq) 309 { 310 hlist_del_init(&rq->hash); 311 } 312 313 static void elv_rqhash_del(struct request_queue *q, struct request *rq) 314 { 315 if (ELV_ON_HASH(rq)) 316 __elv_rqhash_del(rq); 317 } 318 319 static void elv_rqhash_add(struct request_queue *q, struct request *rq) 320 { 321 struct elevator_queue *e = q->elevator; 322 323 BUG_ON(ELV_ON_HASH(rq)); 324 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]); 325 } 326 327 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq) 328 { 329 __elv_rqhash_del(rq); 330 elv_rqhash_add(q, rq); 331 } 332 333 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset) 334 { 335 struct elevator_queue *e = q->elevator; 336 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)]; 337 struct hlist_node *entry, *next; 338 struct request *rq; 339 340 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) { 341 BUG_ON(!ELV_ON_HASH(rq)); 342 343 if (unlikely(!rq_mergeable(rq))) { 344 __elv_rqhash_del(rq); 345 continue; 346 } 347 348 if (rq_hash_key(rq) == offset) 349 return rq; 350 } 351 352 return NULL; 353 } 354 355 /* 356 * RB-tree support functions for inserting/lookup/removal of requests 357 * in a sorted RB tree. 358 */ 359 struct request *elv_rb_add(struct rb_root *root, struct request *rq) 360 { 361 struct rb_node **p = &root->rb_node; 362 struct rb_node *parent = NULL; 363 struct request *__rq; 364 365 while (*p) { 366 parent = *p; 367 __rq = rb_entry(parent, struct request, rb_node); 368 369 if (blk_rq_pos(rq) < blk_rq_pos(__rq)) 370 p = &(*p)->rb_left; 371 else if (blk_rq_pos(rq) > blk_rq_pos(__rq)) 372 p = &(*p)->rb_right; 373 else 374 return __rq; 375 } 376 377 rb_link_node(&rq->rb_node, parent, p); 378 rb_insert_color(&rq->rb_node, root); 379 return NULL; 380 } 381 EXPORT_SYMBOL(elv_rb_add); 382 383 void elv_rb_del(struct rb_root *root, struct request *rq) 384 { 385 BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); 386 rb_erase(&rq->rb_node, root); 387 RB_CLEAR_NODE(&rq->rb_node); 388 } 389 EXPORT_SYMBOL(elv_rb_del); 390 391 struct request *elv_rb_find(struct rb_root *root, sector_t sector) 392 { 393 struct rb_node *n = root->rb_node; 394 struct request *rq; 395 396 while (n) { 397 rq = rb_entry(n, struct request, rb_node); 398 399 if (sector < blk_rq_pos(rq)) 400 n = n->rb_left; 401 else if (sector > blk_rq_pos(rq)) 402 n = n->rb_right; 403 else 404 return rq; 405 } 406 407 return NULL; 408 } 409 EXPORT_SYMBOL(elv_rb_find); 410 411 /* 412 * Insert rq into dispatch queue of q. Queue lock must be held on 413 * entry. rq is sort instead into the dispatch queue. To be used by 414 * specific elevators. 415 */ 416 void elv_dispatch_sort(struct request_queue *q, struct request *rq) 417 { 418 sector_t boundary; 419 struct list_head *entry; 420 int stop_flags; 421 422 if (q->last_merge == rq) 423 q->last_merge = NULL; 424 425 elv_rqhash_del(q, rq); 426 427 q->nr_sorted--; 428 429 boundary = q->end_sector; 430 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED; 431 list_for_each_prev(entry, &q->queue_head) { 432 struct request *pos = list_entry_rq(entry); 433 434 if (blk_discard_rq(rq) != blk_discard_rq(pos)) 435 break; 436 if (rq_data_dir(rq) != rq_data_dir(pos)) 437 break; 438 if (pos->cmd_flags & stop_flags) 439 break; 440 if (blk_rq_pos(rq) >= boundary) { 441 if (blk_rq_pos(pos) < boundary) 442 continue; 443 } else { 444 if (blk_rq_pos(pos) >= boundary) 445 break; 446 } 447 if (blk_rq_pos(rq) >= blk_rq_pos(pos)) 448 break; 449 } 450 451 list_add(&rq->queuelist, entry); 452 } 453 EXPORT_SYMBOL(elv_dispatch_sort); 454 455 /* 456 * Insert rq into dispatch queue of q. Queue lock must be held on 457 * entry. rq is added to the back of the dispatch queue. To be used by 458 * specific elevators. 459 */ 460 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq) 461 { 462 if (q->last_merge == rq) 463 q->last_merge = NULL; 464 465 elv_rqhash_del(q, rq); 466 467 q->nr_sorted--; 468 469 q->end_sector = rq_end_sector(rq); 470 q->boundary_rq = rq; 471 list_add_tail(&rq->queuelist, &q->queue_head); 472 } 473 EXPORT_SYMBOL(elv_dispatch_add_tail); 474 475 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio) 476 { 477 struct elevator_queue *e = q->elevator; 478 struct request *__rq; 479 int ret; 480 481 /* 482 * First try one-hit cache. 483 */ 484 if (q->last_merge) { 485 ret = elv_try_merge(q->last_merge, bio); 486 if (ret != ELEVATOR_NO_MERGE) { 487 *req = q->last_merge; 488 return ret; 489 } 490 } 491 492 if (blk_queue_nomerges(q)) 493 return ELEVATOR_NO_MERGE; 494 495 /* 496 * See if our hash lookup can find a potential backmerge. 497 */ 498 __rq = elv_rqhash_find(q, bio->bi_sector); 499 if (__rq && elv_rq_merge_ok(__rq, bio)) { 500 *req = __rq; 501 return ELEVATOR_BACK_MERGE; 502 } 503 504 if (e->ops->elevator_merge_fn) 505 return e->ops->elevator_merge_fn(q, req, bio); 506 507 return ELEVATOR_NO_MERGE; 508 } 509 510 void elv_merged_request(struct request_queue *q, struct request *rq, int type) 511 { 512 struct elevator_queue *e = q->elevator; 513 514 if (e->ops->elevator_merged_fn) 515 e->ops->elevator_merged_fn(q, rq, type); 516 517 if (type == ELEVATOR_BACK_MERGE) 518 elv_rqhash_reposition(q, rq); 519 520 q->last_merge = rq; 521 } 522 523 void elv_merge_requests(struct request_queue *q, struct request *rq, 524 struct request *next) 525 { 526 struct elevator_queue *e = q->elevator; 527 528 if (e->ops->elevator_merge_req_fn) 529 e->ops->elevator_merge_req_fn(q, rq, next); 530 531 elv_rqhash_reposition(q, rq); 532 elv_rqhash_del(q, next); 533 534 q->nr_sorted--; 535 q->last_merge = rq; 536 } 537 538 void elv_requeue_request(struct request_queue *q, struct request *rq) 539 { 540 /* 541 * it already went through dequeue, we need to decrement the 542 * in_flight count again 543 */ 544 if (blk_account_rq(rq)) { 545 q->in_flight[rq_is_sync(rq)]--; 546 if (blk_sorted_rq(rq)) 547 elv_deactivate_rq(q, rq); 548 } 549 550 rq->cmd_flags &= ~REQ_STARTED; 551 552 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE); 553 } 554 555 void elv_drain_elevator(struct request_queue *q) 556 { 557 static int printed; 558 while (q->elevator->ops->elevator_dispatch_fn(q, 1)) 559 ; 560 if (q->nr_sorted == 0) 561 return; 562 if (printed++ < 10) { 563 printk(KERN_ERR "%s: forced dispatching is broken " 564 "(nr_sorted=%u), please report this\n", 565 q->elevator->elevator_type->elevator_name, q->nr_sorted); 566 } 567 } 568 569 /* 570 * Call with queue lock held, interrupts disabled 571 */ 572 void elv_quiesce_start(struct request_queue *q) 573 { 574 if (!q->elevator) 575 return; 576 577 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q); 578 579 /* 580 * make sure we don't have any requests in flight 581 */ 582 elv_drain_elevator(q); 583 while (q->rq.elvpriv) { 584 __blk_run_queue(q); 585 spin_unlock_irq(q->queue_lock); 586 msleep(10); 587 spin_lock_irq(q->queue_lock); 588 elv_drain_elevator(q); 589 } 590 } 591 592 void elv_quiesce_end(struct request_queue *q) 593 { 594 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q); 595 } 596 597 void elv_insert(struct request_queue *q, struct request *rq, int where) 598 { 599 struct list_head *pos; 600 unsigned ordseq; 601 int unplug_it = 1; 602 603 trace_block_rq_insert(q, rq); 604 605 rq->q = q; 606 607 switch (where) { 608 case ELEVATOR_INSERT_FRONT: 609 rq->cmd_flags |= REQ_SOFTBARRIER; 610 611 list_add(&rq->queuelist, &q->queue_head); 612 break; 613 614 case ELEVATOR_INSERT_BACK: 615 rq->cmd_flags |= REQ_SOFTBARRIER; 616 elv_drain_elevator(q); 617 list_add_tail(&rq->queuelist, &q->queue_head); 618 /* 619 * We kick the queue here for the following reasons. 620 * - The elevator might have returned NULL previously 621 * to delay requests and returned them now. As the 622 * queue wasn't empty before this request, ll_rw_blk 623 * won't run the queue on return, resulting in hang. 624 * - Usually, back inserted requests won't be merged 625 * with anything. There's no point in delaying queue 626 * processing. 627 */ 628 __blk_run_queue(q); 629 break; 630 631 case ELEVATOR_INSERT_SORT: 632 BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq)); 633 rq->cmd_flags |= REQ_SORTED; 634 q->nr_sorted++; 635 if (rq_mergeable(rq)) { 636 elv_rqhash_add(q, rq); 637 if (!q->last_merge) 638 q->last_merge = rq; 639 } 640 641 /* 642 * Some ioscheds (cfq) run q->request_fn directly, so 643 * rq cannot be accessed after calling 644 * elevator_add_req_fn. 645 */ 646 q->elevator->ops->elevator_add_req_fn(q, rq); 647 break; 648 649 case ELEVATOR_INSERT_REQUEUE: 650 /* 651 * If ordered flush isn't in progress, we do front 652 * insertion; otherwise, requests should be requeued 653 * in ordseq order. 654 */ 655 rq->cmd_flags |= REQ_SOFTBARRIER; 656 657 /* 658 * Most requeues happen because of a busy condition, 659 * don't force unplug of the queue for that case. 660 */ 661 unplug_it = 0; 662 663 if (q->ordseq == 0) { 664 list_add(&rq->queuelist, &q->queue_head); 665 break; 666 } 667 668 ordseq = blk_ordered_req_seq(rq); 669 670 list_for_each(pos, &q->queue_head) { 671 struct request *pos_rq = list_entry_rq(pos); 672 if (ordseq <= blk_ordered_req_seq(pos_rq)) 673 break; 674 } 675 676 list_add_tail(&rq->queuelist, pos); 677 break; 678 679 default: 680 printk(KERN_ERR "%s: bad insertion point %d\n", 681 __func__, where); 682 BUG(); 683 } 684 685 if (unplug_it && blk_queue_plugged(q)) { 686 int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC] 687 - queue_in_flight(q); 688 689 if (nrq >= q->unplug_thresh) 690 __generic_unplug_device(q); 691 } 692 } 693 694 void __elv_add_request(struct request_queue *q, struct request *rq, int where, 695 int plug) 696 { 697 if (q->ordcolor) 698 rq->cmd_flags |= REQ_ORDERED_COLOR; 699 700 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) { 701 /* 702 * toggle ordered color 703 */ 704 if (blk_barrier_rq(rq)) 705 q->ordcolor ^= 1; 706 707 /* 708 * barriers implicitly indicate back insertion 709 */ 710 if (where == ELEVATOR_INSERT_SORT) 711 where = ELEVATOR_INSERT_BACK; 712 713 /* 714 * this request is scheduling boundary, update 715 * end_sector 716 */ 717 if (blk_fs_request(rq) || blk_discard_rq(rq)) { 718 q->end_sector = rq_end_sector(rq); 719 q->boundary_rq = rq; 720 } 721 } else if (!(rq->cmd_flags & REQ_ELVPRIV) && 722 where == ELEVATOR_INSERT_SORT) 723 where = ELEVATOR_INSERT_BACK; 724 725 if (plug) 726 blk_plug_device(q); 727 728 elv_insert(q, rq, where); 729 } 730 EXPORT_SYMBOL(__elv_add_request); 731 732 void elv_add_request(struct request_queue *q, struct request *rq, int where, 733 int plug) 734 { 735 unsigned long flags; 736 737 spin_lock_irqsave(q->queue_lock, flags); 738 __elv_add_request(q, rq, where, plug); 739 spin_unlock_irqrestore(q->queue_lock, flags); 740 } 741 EXPORT_SYMBOL(elv_add_request); 742 743 int elv_queue_empty(struct request_queue *q) 744 { 745 struct elevator_queue *e = q->elevator; 746 747 if (!list_empty(&q->queue_head)) 748 return 0; 749 750 if (e->ops->elevator_queue_empty_fn) 751 return e->ops->elevator_queue_empty_fn(q); 752 753 return 1; 754 } 755 EXPORT_SYMBOL(elv_queue_empty); 756 757 struct request *elv_latter_request(struct request_queue *q, struct request *rq) 758 { 759 struct elevator_queue *e = q->elevator; 760 761 if (e->ops->elevator_latter_req_fn) 762 return e->ops->elevator_latter_req_fn(q, rq); 763 return NULL; 764 } 765 766 struct request *elv_former_request(struct request_queue *q, struct request *rq) 767 { 768 struct elevator_queue *e = q->elevator; 769 770 if (e->ops->elevator_former_req_fn) 771 return e->ops->elevator_former_req_fn(q, rq); 772 return NULL; 773 } 774 775 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask) 776 { 777 struct elevator_queue *e = q->elevator; 778 779 if (e->ops->elevator_set_req_fn) 780 return e->ops->elevator_set_req_fn(q, rq, gfp_mask); 781 782 rq->elevator_private = NULL; 783 return 0; 784 } 785 786 void elv_put_request(struct request_queue *q, struct request *rq) 787 { 788 struct elevator_queue *e = q->elevator; 789 790 if (e->ops->elevator_put_req_fn) 791 e->ops->elevator_put_req_fn(rq); 792 } 793 794 int elv_may_queue(struct request_queue *q, int rw) 795 { 796 struct elevator_queue *e = q->elevator; 797 798 if (e->ops->elevator_may_queue_fn) 799 return e->ops->elevator_may_queue_fn(q, rw); 800 801 return ELV_MQUEUE_MAY; 802 } 803 804 void elv_abort_queue(struct request_queue *q) 805 { 806 struct request *rq; 807 808 while (!list_empty(&q->queue_head)) { 809 rq = list_entry_rq(q->queue_head.next); 810 rq->cmd_flags |= REQ_QUIET; 811 trace_block_rq_abort(q, rq); 812 /* 813 * Mark this request as started so we don't trigger 814 * any debug logic in the end I/O path. 815 */ 816 blk_start_request(rq); 817 __blk_end_request_all(rq, -EIO); 818 } 819 } 820 EXPORT_SYMBOL(elv_abort_queue); 821 822 void elv_completed_request(struct request_queue *q, struct request *rq) 823 { 824 struct elevator_queue *e = q->elevator; 825 826 /* 827 * request is released from the driver, io must be done 828 */ 829 if (blk_account_rq(rq)) { 830 q->in_flight[rq_is_sync(rq)]--; 831 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn) 832 e->ops->elevator_completed_req_fn(q, rq); 833 } 834 835 /* 836 * Check if the queue is waiting for fs requests to be 837 * drained for flush sequence. 838 */ 839 if (unlikely(q->ordseq)) { 840 struct request *next = NULL; 841 842 if (!list_empty(&q->queue_head)) 843 next = list_entry_rq(q->queue_head.next); 844 845 if (!queue_in_flight(q) && 846 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN && 847 (!next || blk_ordered_req_seq(next) > QUEUE_ORDSEQ_DRAIN)) { 848 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0); 849 __blk_run_queue(q); 850 } 851 } 852 } 853 854 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) 855 856 static ssize_t 857 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 858 { 859 struct elv_fs_entry *entry = to_elv(attr); 860 struct elevator_queue *e; 861 ssize_t error; 862 863 if (!entry->show) 864 return -EIO; 865 866 e = container_of(kobj, struct elevator_queue, kobj); 867 mutex_lock(&e->sysfs_lock); 868 error = e->ops ? entry->show(e, page) : -ENOENT; 869 mutex_unlock(&e->sysfs_lock); 870 return error; 871 } 872 873 static ssize_t 874 elv_attr_store(struct kobject *kobj, struct attribute *attr, 875 const char *page, size_t length) 876 { 877 struct elv_fs_entry *entry = to_elv(attr); 878 struct elevator_queue *e; 879 ssize_t error; 880 881 if (!entry->store) 882 return -EIO; 883 884 e = container_of(kobj, struct elevator_queue, kobj); 885 mutex_lock(&e->sysfs_lock); 886 error = e->ops ? entry->store(e, page, length) : -ENOENT; 887 mutex_unlock(&e->sysfs_lock); 888 return error; 889 } 890 891 static struct sysfs_ops elv_sysfs_ops = { 892 .show = elv_attr_show, 893 .store = elv_attr_store, 894 }; 895 896 static struct kobj_type elv_ktype = { 897 .sysfs_ops = &elv_sysfs_ops, 898 .release = elevator_release, 899 }; 900 901 int elv_register_queue(struct request_queue *q) 902 { 903 struct elevator_queue *e = q->elevator; 904 int error; 905 906 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched"); 907 if (!error) { 908 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs; 909 if (attr) { 910 while (attr->attr.name) { 911 if (sysfs_create_file(&e->kobj, &attr->attr)) 912 break; 913 attr++; 914 } 915 } 916 kobject_uevent(&e->kobj, KOBJ_ADD); 917 } 918 return error; 919 } 920 921 static void __elv_unregister_queue(struct elevator_queue *e) 922 { 923 kobject_uevent(&e->kobj, KOBJ_REMOVE); 924 kobject_del(&e->kobj); 925 } 926 927 void elv_unregister_queue(struct request_queue *q) 928 { 929 if (q) 930 __elv_unregister_queue(q->elevator); 931 } 932 933 void elv_register(struct elevator_type *e) 934 { 935 char *def = ""; 936 937 spin_lock(&elv_list_lock); 938 BUG_ON(elevator_find(e->elevator_name)); 939 list_add_tail(&e->list, &elv_list); 940 spin_unlock(&elv_list_lock); 941 942 if (!strcmp(e->elevator_name, chosen_elevator) || 943 (!*chosen_elevator && 944 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED))) 945 def = " (default)"; 946 947 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, 948 def); 949 } 950 EXPORT_SYMBOL_GPL(elv_register); 951 952 void elv_unregister(struct elevator_type *e) 953 { 954 struct task_struct *g, *p; 955 956 /* 957 * Iterate every thread in the process to remove the io contexts. 958 */ 959 if (e->ops.trim) { 960 read_lock(&tasklist_lock); 961 do_each_thread(g, p) { 962 task_lock(p); 963 if (p->io_context) 964 e->ops.trim(p->io_context); 965 task_unlock(p); 966 } while_each_thread(g, p); 967 read_unlock(&tasklist_lock); 968 } 969 970 spin_lock(&elv_list_lock); 971 list_del_init(&e->list); 972 spin_unlock(&elv_list_lock); 973 } 974 EXPORT_SYMBOL_GPL(elv_unregister); 975 976 /* 977 * switch to new_e io scheduler. be careful not to introduce deadlocks - 978 * we don't free the old io scheduler, before we have allocated what we 979 * need for the new one. this way we have a chance of going back to the old 980 * one, if the new one fails init for some reason. 981 */ 982 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e) 983 { 984 struct elevator_queue *old_elevator, *e; 985 void *data; 986 987 /* 988 * Allocate new elevator 989 */ 990 e = elevator_alloc(q, new_e); 991 if (!e) 992 return 0; 993 994 data = elevator_init_queue(q, e); 995 if (!data) { 996 kobject_put(&e->kobj); 997 return 0; 998 } 999 1000 /* 1001 * Turn on BYPASS and drain all requests w/ elevator private data 1002 */ 1003 spin_lock_irq(q->queue_lock); 1004 elv_quiesce_start(q); 1005 1006 /* 1007 * Remember old elevator. 1008 */ 1009 old_elevator = q->elevator; 1010 1011 /* 1012 * attach and start new elevator 1013 */ 1014 elevator_attach(q, e, data); 1015 1016 spin_unlock_irq(q->queue_lock); 1017 1018 __elv_unregister_queue(old_elevator); 1019 1020 if (elv_register_queue(q)) 1021 goto fail_register; 1022 1023 /* 1024 * finally exit old elevator and turn off BYPASS. 1025 */ 1026 elevator_exit(old_elevator); 1027 spin_lock_irq(q->queue_lock); 1028 elv_quiesce_end(q); 1029 spin_unlock_irq(q->queue_lock); 1030 1031 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name); 1032 1033 return 1; 1034 1035 fail_register: 1036 /* 1037 * switch failed, exit the new io scheduler and reattach the old 1038 * one again (along with re-adding the sysfs dir) 1039 */ 1040 elevator_exit(e); 1041 q->elevator = old_elevator; 1042 elv_register_queue(q); 1043 1044 spin_lock_irq(q->queue_lock); 1045 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q); 1046 spin_unlock_irq(q->queue_lock); 1047 1048 return 0; 1049 } 1050 1051 ssize_t elv_iosched_store(struct request_queue *q, const char *name, 1052 size_t count) 1053 { 1054 char elevator_name[ELV_NAME_MAX]; 1055 struct elevator_type *e; 1056 1057 if (!q->elevator) 1058 return count; 1059 1060 strlcpy(elevator_name, name, sizeof(elevator_name)); 1061 strstrip(elevator_name); 1062 1063 e = elevator_get(elevator_name); 1064 if (!e) { 1065 printk(KERN_ERR "elevator: type %s not found\n", elevator_name); 1066 return -EINVAL; 1067 } 1068 1069 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) { 1070 elevator_put(e); 1071 return count; 1072 } 1073 1074 if (!elevator_switch(q, e)) 1075 printk(KERN_ERR "elevator: switch to %s failed\n", 1076 elevator_name); 1077 return count; 1078 } 1079 1080 ssize_t elv_iosched_show(struct request_queue *q, char *name) 1081 { 1082 struct elevator_queue *e = q->elevator; 1083 struct elevator_type *elv; 1084 struct elevator_type *__e; 1085 int len = 0; 1086 1087 if (!q->elevator) 1088 return sprintf(name, "none\n"); 1089 1090 elv = e->elevator_type; 1091 1092 spin_lock(&elv_list_lock); 1093 list_for_each_entry(__e, &elv_list, list) { 1094 if (!strcmp(elv->elevator_name, __e->elevator_name)) 1095 len += sprintf(name+len, "[%s] ", elv->elevator_name); 1096 else 1097 len += sprintf(name+len, "%s ", __e->elevator_name); 1098 } 1099 spin_unlock(&elv_list_lock); 1100 1101 len += sprintf(len+name, "\n"); 1102 return len; 1103 } 1104 1105 struct request *elv_rb_former_request(struct request_queue *q, 1106 struct request *rq) 1107 { 1108 struct rb_node *rbprev = rb_prev(&rq->rb_node); 1109 1110 if (rbprev) 1111 return rb_entry_rq(rbprev); 1112 1113 return NULL; 1114 } 1115 EXPORT_SYMBOL(elv_rb_former_request); 1116 1117 struct request *elv_rb_latter_request(struct request_queue *q, 1118 struct request *rq) 1119 { 1120 struct rb_node *rbnext = rb_next(&rq->rb_node); 1121 1122 if (rbnext) 1123 return rb_entry_rq(rbnext); 1124 1125 return NULL; 1126 } 1127 EXPORT_SYMBOL(elv_rb_latter_request); 1128