Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/block/elevator.c |
| 3 | * |
| 4 | * Block device elevator/IO-scheduler. |
| 5 | * |
| 6 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE |
| 7 | * |
| 8 | * 30042000 Jens Axboe <axboe@suse.de> : |
| 9 | * |
| 10 | * Split the elevator a bit so that it is possible to choose a different |
| 11 | * one or even write a new "plug in". There are three pieces: |
| 12 | * - elevator_fn, inserts a new request in the queue list |
| 13 | * - elevator_merge_fn, decides whether a new buffer can be merged with |
| 14 | * an existing request |
| 15 | * - elevator_dequeue_fn, called when a request is taken off the active list |
| 16 | * |
| 17 | * 20082000 Dave Jones <davej@suse.de> : |
| 18 | * Removed tests for max-bomb-segments, which was breaking elvtune |
| 19 | * when run without -bN |
| 20 | * |
| 21 | * Jens: |
| 22 | * - Rework again to work with bio instead of buffer_heads |
| 23 | * - loose bi_dev comparisons, partition handling is right now |
| 24 | * - completely modularize elevator setup and teardown |
| 25 | * |
| 26 | */ |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/blkdev.h> |
| 30 | #include <linux/elevator.h> |
| 31 | #include <linux/bio.h> |
| 32 | #include <linux/config.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/compiler.h> |
| 37 | |
| 38 | #include <asm/uaccess.h> |
| 39 | |
| 40 | static DEFINE_SPINLOCK(elv_list_lock); |
| 41 | static LIST_HEAD(elv_list); |
| 42 | |
| 43 | /* |
| 44 | * can we safely merge with this request? |
| 45 | */ |
| 46 | inline int elv_rq_merge_ok(struct request *rq, struct bio *bio) |
| 47 | { |
| 48 | if (!rq_mergeable(rq)) |
| 49 | return 0; |
| 50 | |
| 51 | /* |
| 52 | * different data direction or already started, don't merge |
| 53 | */ |
| 54 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 55 | return 0; |
| 56 | |
| 57 | /* |
| 58 | * same device and no special stuff set, merge is ok |
| 59 | */ |
| 60 | if (rq->rq_disk == bio->bi_bdev->bd_disk && |
| 61 | !rq->waiting && !rq->special) |
| 62 | return 1; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | EXPORT_SYMBOL(elv_rq_merge_ok); |
| 67 | |
| 68 | inline int elv_try_merge(struct request *__rq, struct bio *bio) |
| 69 | { |
| 70 | int ret = ELEVATOR_NO_MERGE; |
| 71 | |
| 72 | /* |
| 73 | * we can merge and sequence is ok, check if it's possible |
| 74 | */ |
| 75 | if (elv_rq_merge_ok(__rq, bio)) { |
| 76 | if (__rq->sector + __rq->nr_sectors == bio->bi_sector) |
| 77 | ret = ELEVATOR_BACK_MERGE; |
| 78 | else if (__rq->sector - bio_sectors(bio) == bio->bi_sector) |
| 79 | ret = ELEVATOR_FRONT_MERGE; |
| 80 | } |
| 81 | |
| 82 | return ret; |
| 83 | } |
| 84 | EXPORT_SYMBOL(elv_try_merge); |
| 85 | |
| 86 | inline int elv_try_last_merge(request_queue_t *q, struct bio *bio) |
| 87 | { |
| 88 | if (q->last_merge) |
| 89 | return elv_try_merge(q->last_merge, bio); |
| 90 | |
| 91 | return ELEVATOR_NO_MERGE; |
| 92 | } |
| 93 | EXPORT_SYMBOL(elv_try_last_merge); |
| 94 | |
| 95 | static struct elevator_type *elevator_find(const char *name) |
| 96 | { |
| 97 | struct elevator_type *e = NULL; |
| 98 | struct list_head *entry; |
| 99 | |
| 100 | spin_lock_irq(&elv_list_lock); |
| 101 | list_for_each(entry, &elv_list) { |
| 102 | struct elevator_type *__e; |
| 103 | |
| 104 | __e = list_entry(entry, struct elevator_type, list); |
| 105 | |
| 106 | if (!strcmp(__e->elevator_name, name)) { |
| 107 | e = __e; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | spin_unlock_irq(&elv_list_lock); |
| 112 | |
| 113 | return e; |
| 114 | } |
| 115 | |
| 116 | static void elevator_put(struct elevator_type *e) |
| 117 | { |
| 118 | module_put(e->elevator_owner); |
| 119 | } |
| 120 | |
| 121 | static struct elevator_type *elevator_get(const char *name) |
| 122 | { |
| 123 | struct elevator_type *e = elevator_find(name); |
| 124 | |
| 125 | if (!e) |
| 126 | return NULL; |
| 127 | if (!try_module_get(e->elevator_owner)) |
| 128 | return NULL; |
| 129 | |
| 130 | return e; |
| 131 | } |
| 132 | |
| 133 | static int elevator_attach(request_queue_t *q, struct elevator_type *e, |
| 134 | struct elevator_queue *eq) |
| 135 | { |
| 136 | int ret = 0; |
| 137 | |
| 138 | memset(eq, 0, sizeof(*eq)); |
| 139 | eq->ops = &e->ops; |
| 140 | eq->elevator_type = e; |
| 141 | |
| 142 | INIT_LIST_HEAD(&q->queue_head); |
| 143 | q->last_merge = NULL; |
| 144 | q->elevator = eq; |
| 145 | |
| 146 | if (eq->ops->elevator_init_fn) |
| 147 | ret = eq->ops->elevator_init_fn(q, eq); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static char chosen_elevator[16]; |
| 153 | |
| 154 | static void elevator_setup_default(void) |
| 155 | { |
| 156 | /* |
| 157 | * check if default is set and exists |
| 158 | */ |
| 159 | if (chosen_elevator[0] && elevator_find(chosen_elevator)) |
| 160 | return; |
| 161 | |
| 162 | #if defined(CONFIG_IOSCHED_AS) |
| 163 | strcpy(chosen_elevator, "anticipatory"); |
| 164 | #elif defined(CONFIG_IOSCHED_DEADLINE) |
| 165 | strcpy(chosen_elevator, "deadline"); |
| 166 | #elif defined(CONFIG_IOSCHED_CFQ) |
| 167 | strcpy(chosen_elevator, "cfq"); |
| 168 | #elif defined(CONFIG_IOSCHED_NOOP) |
| 169 | strcpy(chosen_elevator, "noop"); |
| 170 | #else |
| 171 | #error "You must build at least 1 IO scheduler into the kernel" |
| 172 | #endif |
| 173 | } |
| 174 | |
| 175 | static int __init elevator_setup(char *str) |
| 176 | { |
| 177 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | __setup("elevator=", elevator_setup); |
| 182 | |
| 183 | int elevator_init(request_queue_t *q, char *name) |
| 184 | { |
| 185 | struct elevator_type *e = NULL; |
| 186 | struct elevator_queue *eq; |
| 187 | int ret = 0; |
| 188 | |
| 189 | elevator_setup_default(); |
| 190 | |
| 191 | if (!name) |
| 192 | name = chosen_elevator; |
| 193 | |
| 194 | e = elevator_get(name); |
| 195 | if (!e) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL); |
| 199 | if (!eq) { |
| 200 | elevator_put(e->elevator_type); |
| 201 | return -ENOMEM; |
| 202 | } |
| 203 | |
| 204 | ret = elevator_attach(q, e, eq); |
| 205 | if (ret) { |
| 206 | kfree(eq); |
| 207 | elevator_put(e->elevator_type); |
| 208 | } |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | void elevator_exit(elevator_t *e) |
| 214 | { |
| 215 | if (e->ops->elevator_exit_fn) |
| 216 | e->ops->elevator_exit_fn(e); |
| 217 | |
| 218 | elevator_put(e->elevator_type); |
| 219 | e->elevator_type = NULL; |
| 220 | kfree(e); |
| 221 | } |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | int elv_merge(request_queue_t *q, struct request **req, struct bio *bio) |
| 224 | { |
| 225 | elevator_t *e = q->elevator; |
| 226 | |
| 227 | if (e->ops->elevator_merge_fn) |
| 228 | return e->ops->elevator_merge_fn(q, req, bio); |
| 229 | |
| 230 | return ELEVATOR_NO_MERGE; |
| 231 | } |
| 232 | |
| 233 | void elv_merged_request(request_queue_t *q, struct request *rq) |
| 234 | { |
| 235 | elevator_t *e = q->elevator; |
| 236 | |
| 237 | if (e->ops->elevator_merged_fn) |
| 238 | e->ops->elevator_merged_fn(q, rq); |
| 239 | } |
| 240 | |
| 241 | void elv_merge_requests(request_queue_t *q, struct request *rq, |
| 242 | struct request *next) |
| 243 | { |
| 244 | elevator_t *e = q->elevator; |
| 245 | |
| 246 | if (q->last_merge == next) |
| 247 | q->last_merge = NULL; |
| 248 | |
| 249 | if (e->ops->elevator_merge_req_fn) |
| 250 | e->ops->elevator_merge_req_fn(q, rq, next); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * For careful internal use by the block layer. Essentially the same as |
| 255 | * a requeue in that it tells the io scheduler that this request is not |
| 256 | * active in the driver or hardware anymore, but we don't want the request |
| 257 | * added back to the scheduler. Function is not exported. |
| 258 | */ |
| 259 | void elv_deactivate_request(request_queue_t *q, struct request *rq) |
| 260 | { |
| 261 | elevator_t *e = q->elevator; |
| 262 | |
| 263 | /* |
| 264 | * it already went through dequeue, we need to decrement the |
| 265 | * in_flight count again |
| 266 | */ |
| 267 | if (blk_account_rq(rq)) |
| 268 | q->in_flight--; |
| 269 | |
| 270 | rq->flags &= ~REQ_STARTED; |
| 271 | |
| 272 | if (e->ops->elevator_deactivate_req_fn) |
| 273 | e->ops->elevator_deactivate_req_fn(q, rq); |
| 274 | } |
| 275 | |
| 276 | void elv_requeue_request(request_queue_t *q, struct request *rq) |
| 277 | { |
| 278 | elv_deactivate_request(q, rq); |
| 279 | |
| 280 | /* |
| 281 | * if this is the flush, requeue the original instead and drop the flush |
| 282 | */ |
| 283 | if (rq->flags & REQ_BAR_FLUSH) { |
| 284 | clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags); |
| 285 | rq = rq->end_io_data; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * if iosched has an explicit requeue hook, then use that. otherwise |
| 290 | * just put the request at the front of the queue |
| 291 | */ |
| 292 | if (q->elevator->ops->elevator_requeue_req_fn) |
| 293 | q->elevator->ops->elevator_requeue_req_fn(q, rq); |
| 294 | else |
| 295 | __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0); |
| 296 | } |
| 297 | |
| 298 | void __elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 299 | int plug) |
| 300 | { |
| 301 | /* |
| 302 | * barriers implicitly indicate back insertion |
| 303 | */ |
| 304 | if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER) && |
| 305 | where == ELEVATOR_INSERT_SORT) |
| 306 | where = ELEVATOR_INSERT_BACK; |
| 307 | |
| 308 | if (plug) |
| 309 | blk_plug_device(q); |
| 310 | |
| 311 | rq->q = q; |
| 312 | |
| 313 | if (!test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags)) { |
| 314 | q->elevator->ops->elevator_add_req_fn(q, rq, where); |
| 315 | |
| 316 | if (blk_queue_plugged(q)) { |
| 317 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] |
| 318 | - q->in_flight; |
| 319 | |
Tejun Heo | c374f12 | 2005-06-16 12:57:31 +0200 | [diff] [blame] | 320 | if (nrq >= q->unplug_thresh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | __generic_unplug_device(q); |
| 322 | } |
| 323 | } else |
| 324 | /* |
| 325 | * if drain is set, store the request "locally". when the drain |
| 326 | * is finished, the requests will be handed ordered to the io |
| 327 | * scheduler |
| 328 | */ |
| 329 | list_add_tail(&rq->queuelist, &q->drain_list); |
| 330 | } |
| 331 | |
| 332 | void elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 333 | int plug) |
| 334 | { |
| 335 | unsigned long flags; |
| 336 | |
| 337 | spin_lock_irqsave(q->queue_lock, flags); |
| 338 | __elv_add_request(q, rq, where, plug); |
| 339 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 340 | } |
| 341 | |
| 342 | static inline struct request *__elv_next_request(request_queue_t *q) |
| 343 | { |
| 344 | struct request *rq = q->elevator->ops->elevator_next_req_fn(q); |
| 345 | |
| 346 | /* |
| 347 | * if this is a barrier write and the device has to issue a |
| 348 | * flush sequence to support it, check how far we are |
| 349 | */ |
| 350 | if (rq && blk_fs_request(rq) && blk_barrier_rq(rq)) { |
| 351 | BUG_ON(q->ordered == QUEUE_ORDERED_NONE); |
| 352 | |
| 353 | if (q->ordered == QUEUE_ORDERED_FLUSH && |
| 354 | !blk_barrier_preflush(rq)) |
| 355 | rq = blk_start_pre_flush(q, rq); |
| 356 | } |
| 357 | |
| 358 | return rq; |
| 359 | } |
| 360 | |
| 361 | struct request *elv_next_request(request_queue_t *q) |
| 362 | { |
| 363 | struct request *rq; |
| 364 | int ret; |
| 365 | |
| 366 | while ((rq = __elv_next_request(q)) != NULL) { |
| 367 | /* |
| 368 | * just mark as started even if we don't start it, a request |
| 369 | * that has been delayed should not be passed by new incoming |
| 370 | * requests |
| 371 | */ |
| 372 | rq->flags |= REQ_STARTED; |
| 373 | |
| 374 | if (rq == q->last_merge) |
| 375 | q->last_merge = NULL; |
| 376 | |
| 377 | if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn) |
| 378 | break; |
| 379 | |
| 380 | ret = q->prep_rq_fn(q, rq); |
| 381 | if (ret == BLKPREP_OK) { |
| 382 | break; |
| 383 | } else if (ret == BLKPREP_DEFER) { |
| 384 | rq = NULL; |
| 385 | break; |
| 386 | } else if (ret == BLKPREP_KILL) { |
| 387 | int nr_bytes = rq->hard_nr_sectors << 9; |
| 388 | |
| 389 | if (!nr_bytes) |
| 390 | nr_bytes = rq->data_len; |
| 391 | |
| 392 | blkdev_dequeue_request(rq); |
| 393 | rq->flags |= REQ_QUIET; |
| 394 | end_that_request_chunk(rq, 0, nr_bytes); |
| 395 | end_that_request_last(rq); |
| 396 | } else { |
| 397 | printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__, |
| 398 | ret); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return rq; |
| 404 | } |
| 405 | |
| 406 | void elv_remove_request(request_queue_t *q, struct request *rq) |
| 407 | { |
| 408 | elevator_t *e = q->elevator; |
| 409 | |
| 410 | /* |
| 411 | * the time frame between a request being removed from the lists |
| 412 | * and to it is freed is accounted as io that is in progress at |
| 413 | * the driver side. note that we only account requests that the |
| 414 | * driver has seen (REQ_STARTED set), to avoid false accounting |
| 415 | * for request-request merges |
| 416 | */ |
| 417 | if (blk_account_rq(rq)) |
| 418 | q->in_flight++; |
| 419 | |
| 420 | /* |
| 421 | * the main clearing point for q->last_merge is on retrieval of |
| 422 | * request by driver (it calls elv_next_request()), but it _can_ |
| 423 | * also happen here if a request is added to the queue but later |
| 424 | * deleted without ever being given to driver (merged with another |
| 425 | * request). |
| 426 | */ |
| 427 | if (rq == q->last_merge) |
| 428 | q->last_merge = NULL; |
| 429 | |
| 430 | if (e->ops->elevator_remove_req_fn) |
| 431 | e->ops->elevator_remove_req_fn(q, rq); |
| 432 | } |
| 433 | |
| 434 | int elv_queue_empty(request_queue_t *q) |
| 435 | { |
| 436 | elevator_t *e = q->elevator; |
| 437 | |
| 438 | if (e->ops->elevator_queue_empty_fn) |
| 439 | return e->ops->elevator_queue_empty_fn(q); |
| 440 | |
| 441 | return list_empty(&q->queue_head); |
| 442 | } |
| 443 | |
| 444 | struct request *elv_latter_request(request_queue_t *q, struct request *rq) |
| 445 | { |
| 446 | struct list_head *next; |
| 447 | |
| 448 | elevator_t *e = q->elevator; |
| 449 | |
| 450 | if (e->ops->elevator_latter_req_fn) |
| 451 | return e->ops->elevator_latter_req_fn(q, rq); |
| 452 | |
| 453 | next = rq->queuelist.next; |
| 454 | if (next != &q->queue_head && next != &rq->queuelist) |
| 455 | return list_entry_rq(next); |
| 456 | |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | struct request *elv_former_request(request_queue_t *q, struct request *rq) |
| 461 | { |
| 462 | struct list_head *prev; |
| 463 | |
| 464 | elevator_t *e = q->elevator; |
| 465 | |
| 466 | if (e->ops->elevator_former_req_fn) |
| 467 | return e->ops->elevator_former_req_fn(q, rq); |
| 468 | |
| 469 | prev = rq->queuelist.prev; |
| 470 | if (prev != &q->queue_head && prev != &rq->queuelist) |
| 471 | return list_entry_rq(prev); |
| 472 | |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | int elv_set_request(request_queue_t *q, struct request *rq, int gfp_mask) |
| 477 | { |
| 478 | elevator_t *e = q->elevator; |
| 479 | |
| 480 | if (e->ops->elevator_set_req_fn) |
| 481 | return e->ops->elevator_set_req_fn(q, rq, gfp_mask); |
| 482 | |
| 483 | rq->elevator_private = NULL; |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | void elv_put_request(request_queue_t *q, struct request *rq) |
| 488 | { |
| 489 | elevator_t *e = q->elevator; |
| 490 | |
| 491 | if (e->ops->elevator_put_req_fn) |
| 492 | e->ops->elevator_put_req_fn(q, rq); |
| 493 | } |
| 494 | |
| 495 | int elv_may_queue(request_queue_t *q, int rw) |
| 496 | { |
| 497 | elevator_t *e = q->elevator; |
| 498 | |
| 499 | if (e->ops->elevator_may_queue_fn) |
| 500 | return e->ops->elevator_may_queue_fn(q, rw); |
| 501 | |
| 502 | return ELV_MQUEUE_MAY; |
| 503 | } |
| 504 | |
| 505 | void elv_completed_request(request_queue_t *q, struct request *rq) |
| 506 | { |
| 507 | elevator_t *e = q->elevator; |
| 508 | |
| 509 | /* |
| 510 | * request is released from the driver, io must be done |
| 511 | */ |
| 512 | if (blk_account_rq(rq)) |
| 513 | q->in_flight--; |
| 514 | |
| 515 | if (e->ops->elevator_completed_req_fn) |
| 516 | e->ops->elevator_completed_req_fn(q, rq); |
| 517 | } |
| 518 | |
| 519 | int elv_register_queue(struct request_queue *q) |
| 520 | { |
| 521 | elevator_t *e = q->elevator; |
| 522 | |
| 523 | e->kobj.parent = kobject_get(&q->kobj); |
| 524 | if (!e->kobj.parent) |
| 525 | return -EBUSY; |
| 526 | |
| 527 | snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched"); |
| 528 | e->kobj.ktype = e->elevator_type->elevator_ktype; |
| 529 | |
| 530 | return kobject_register(&e->kobj); |
| 531 | } |
| 532 | |
| 533 | void elv_unregister_queue(struct request_queue *q) |
| 534 | { |
| 535 | if (q) { |
| 536 | elevator_t *e = q->elevator; |
| 537 | kobject_unregister(&e->kobj); |
| 538 | kobject_put(&q->kobj); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | int elv_register(struct elevator_type *e) |
| 543 | { |
| 544 | if (elevator_find(e->elevator_name)) |
| 545 | BUG(); |
| 546 | |
| 547 | spin_lock_irq(&elv_list_lock); |
| 548 | list_add_tail(&e->list, &elv_list); |
| 549 | spin_unlock_irq(&elv_list_lock); |
| 550 | |
| 551 | printk(KERN_INFO "io scheduler %s registered", e->elevator_name); |
| 552 | if (!strcmp(e->elevator_name, chosen_elevator)) |
| 553 | printk(" (default)"); |
| 554 | printk("\n"); |
| 555 | return 0; |
| 556 | } |
| 557 | EXPORT_SYMBOL_GPL(elv_register); |
| 558 | |
| 559 | void elv_unregister(struct elevator_type *e) |
| 560 | { |
| 561 | spin_lock_irq(&elv_list_lock); |
| 562 | list_del_init(&e->list); |
| 563 | spin_unlock_irq(&elv_list_lock); |
| 564 | } |
| 565 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 566 | |
| 567 | /* |
| 568 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 569 | * we don't free the old io scheduler, before we have allocated what we |
| 570 | * need for the new one. this way we have a chance of going back to the old |
| 571 | * one, if the new one fails init for some reason. we also do an intermediate |
| 572 | * switch to noop to ensure safety with stack-allocated requests, since they |
| 573 | * don't originate from the block layer allocator. noop is safe here, because |
| 574 | * it never needs to touch the elevator itself for completion events. DRAIN |
| 575 | * flags will make sure we don't touch it for additions either. |
| 576 | */ |
| 577 | static void elevator_switch(request_queue_t *q, struct elevator_type *new_e) |
| 578 | { |
| 579 | elevator_t *e = kmalloc(sizeof(elevator_t), GFP_KERNEL); |
| 580 | struct elevator_type *noop_elevator = NULL; |
| 581 | elevator_t *old_elevator; |
| 582 | |
| 583 | if (!e) |
| 584 | goto error; |
| 585 | |
| 586 | /* |
| 587 | * first step, drain requests from the block freelist |
| 588 | */ |
| 589 | blk_wait_queue_drained(q, 0); |
| 590 | |
| 591 | /* |
| 592 | * unregister old elevator data |
| 593 | */ |
| 594 | elv_unregister_queue(q); |
| 595 | old_elevator = q->elevator; |
| 596 | |
| 597 | /* |
| 598 | * next step, switch to noop since it uses no private rq structures |
| 599 | * and doesn't allocate any memory for anything. then wait for any |
| 600 | * non-fs requests in-flight |
| 601 | */ |
| 602 | noop_elevator = elevator_get("noop"); |
| 603 | spin_lock_irq(q->queue_lock); |
| 604 | elevator_attach(q, noop_elevator, e); |
| 605 | spin_unlock_irq(q->queue_lock); |
| 606 | |
| 607 | blk_wait_queue_drained(q, 1); |
| 608 | |
| 609 | /* |
| 610 | * attach and start new elevator |
| 611 | */ |
| 612 | if (elevator_attach(q, new_e, e)) |
| 613 | goto fail; |
| 614 | |
| 615 | if (elv_register_queue(q)) |
| 616 | goto fail_register; |
| 617 | |
| 618 | /* |
| 619 | * finally exit old elevator and start queue again |
| 620 | */ |
| 621 | elevator_exit(old_elevator); |
| 622 | blk_finish_queue_drain(q); |
| 623 | elevator_put(noop_elevator); |
| 624 | return; |
| 625 | |
| 626 | fail_register: |
| 627 | /* |
| 628 | * switch failed, exit the new io scheduler and reattach the old |
| 629 | * one again (along with re-adding the sysfs dir) |
| 630 | */ |
| 631 | elevator_exit(e); |
| 632 | fail: |
| 633 | q->elevator = old_elevator; |
| 634 | elv_register_queue(q); |
| 635 | blk_finish_queue_drain(q); |
| 636 | error: |
| 637 | if (noop_elevator) |
| 638 | elevator_put(noop_elevator); |
| 639 | elevator_put(new_e); |
| 640 | printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name); |
| 641 | } |
| 642 | |
| 643 | ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count) |
| 644 | { |
| 645 | char elevator_name[ELV_NAME_MAX]; |
| 646 | struct elevator_type *e; |
| 647 | |
| 648 | memset(elevator_name, 0, sizeof(elevator_name)); |
| 649 | strncpy(elevator_name, name, sizeof(elevator_name)); |
| 650 | |
| 651 | if (elevator_name[strlen(elevator_name) - 1] == '\n') |
| 652 | elevator_name[strlen(elevator_name) - 1] = '\0'; |
| 653 | |
| 654 | e = elevator_get(elevator_name); |
| 655 | if (!e) { |
| 656 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); |
| 657 | return -EINVAL; |
| 658 | } |
| 659 | |
| 660 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) |
| 661 | return count; |
| 662 | |
| 663 | elevator_switch(q, e); |
| 664 | return count; |
| 665 | } |
| 666 | |
| 667 | ssize_t elv_iosched_show(request_queue_t *q, char *name) |
| 668 | { |
| 669 | elevator_t *e = q->elevator; |
| 670 | struct elevator_type *elv = e->elevator_type; |
| 671 | struct list_head *entry; |
| 672 | int len = 0; |
| 673 | |
| 674 | spin_lock_irq(q->queue_lock); |
| 675 | list_for_each(entry, &elv_list) { |
| 676 | struct elevator_type *__e; |
| 677 | |
| 678 | __e = list_entry(entry, struct elevator_type, list); |
| 679 | if (!strcmp(elv->elevator_name, __e->elevator_name)) |
| 680 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
| 681 | else |
| 682 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 683 | } |
| 684 | spin_unlock_irq(q->queue_lock); |
| 685 | |
| 686 | len += sprintf(len+name, "\n"); |
| 687 | return len; |
| 688 | } |
| 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | EXPORT_SYMBOL(elv_add_request); |
| 691 | EXPORT_SYMBOL(__elv_add_request); |
| 692 | EXPORT_SYMBOL(elv_requeue_request); |
| 693 | EXPORT_SYMBOL(elv_next_request); |
| 694 | EXPORT_SYMBOL(elv_remove_request); |
| 695 | EXPORT_SYMBOL(elv_queue_empty); |
| 696 | EXPORT_SYMBOL(elv_completed_request); |
| 697 | EXPORT_SYMBOL(elevator_exit); |
| 698 | EXPORT_SYMBOL(elevator_init); |