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 | /* |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 289 | * the request is prepped and may have some resources allocated. |
| 290 | * allowing unprepped requests to pass this one may cause resource |
| 291 | * deadlock. turn on softbarrier. |
| 292 | */ |
| 293 | rq->flags |= REQ_SOFTBARRIER; |
| 294 | |
| 295 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * if iosched has an explicit requeue hook, then use that. otherwise |
| 297 | * just put the request at the front of the queue |
| 298 | */ |
| 299 | if (q->elevator->ops->elevator_requeue_req_fn) |
| 300 | q->elevator->ops->elevator_requeue_req_fn(q, rq); |
| 301 | else |
| 302 | __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0); |
| 303 | } |
| 304 | |
| 305 | void __elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 306 | int plug) |
| 307 | { |
| 308 | /* |
| 309 | * barriers implicitly indicate back insertion |
| 310 | */ |
| 311 | if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER) && |
| 312 | where == ELEVATOR_INSERT_SORT) |
| 313 | where = ELEVATOR_INSERT_BACK; |
| 314 | |
| 315 | if (plug) |
| 316 | blk_plug_device(q); |
| 317 | |
| 318 | rq->q = q; |
| 319 | |
| 320 | if (!test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags)) { |
| 321 | q->elevator->ops->elevator_add_req_fn(q, rq, where); |
| 322 | |
| 323 | if (blk_queue_plugged(q)) { |
| 324 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] |
| 325 | - q->in_flight; |
| 326 | |
Tejun Heo | c374f12 | 2005-06-16 12:57:31 +0200 | [diff] [blame] | 327 | if (nrq >= q->unplug_thresh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | __generic_unplug_device(q); |
| 329 | } |
| 330 | } else |
| 331 | /* |
| 332 | * if drain is set, store the request "locally". when the drain |
| 333 | * is finished, the requests will be handed ordered to the io |
| 334 | * scheduler |
| 335 | */ |
| 336 | list_add_tail(&rq->queuelist, &q->drain_list); |
| 337 | } |
| 338 | |
| 339 | void elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 340 | int plug) |
| 341 | { |
| 342 | unsigned long flags; |
| 343 | |
| 344 | spin_lock_irqsave(q->queue_lock, flags); |
| 345 | __elv_add_request(q, rq, where, plug); |
| 346 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 347 | } |
| 348 | |
| 349 | static inline struct request *__elv_next_request(request_queue_t *q) |
| 350 | { |
| 351 | struct request *rq = q->elevator->ops->elevator_next_req_fn(q); |
| 352 | |
| 353 | /* |
| 354 | * if this is a barrier write and the device has to issue a |
| 355 | * flush sequence to support it, check how far we are |
| 356 | */ |
| 357 | if (rq && blk_fs_request(rq) && blk_barrier_rq(rq)) { |
| 358 | BUG_ON(q->ordered == QUEUE_ORDERED_NONE); |
| 359 | |
| 360 | if (q->ordered == QUEUE_ORDERED_FLUSH && |
| 361 | !blk_barrier_preflush(rq)) |
| 362 | rq = blk_start_pre_flush(q, rq); |
| 363 | } |
| 364 | |
| 365 | return rq; |
| 366 | } |
| 367 | |
| 368 | struct request *elv_next_request(request_queue_t *q) |
| 369 | { |
| 370 | struct request *rq; |
| 371 | int ret; |
| 372 | |
| 373 | while ((rq = __elv_next_request(q)) != NULL) { |
| 374 | /* |
| 375 | * just mark as started even if we don't start it, a request |
| 376 | * that has been delayed should not be passed by new incoming |
| 377 | * requests |
| 378 | */ |
| 379 | rq->flags |= REQ_STARTED; |
| 380 | |
| 381 | if (rq == q->last_merge) |
| 382 | q->last_merge = NULL; |
| 383 | |
| 384 | if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn) |
| 385 | break; |
| 386 | |
| 387 | ret = q->prep_rq_fn(q, rq); |
| 388 | if (ret == BLKPREP_OK) { |
| 389 | break; |
| 390 | } else if (ret == BLKPREP_DEFER) { |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 391 | /* |
| 392 | * the request may have been (partially) prepped. |
| 393 | * we need to keep this request in the front to |
| 394 | * avoid resource deadlock. turn on softbarrier. |
| 395 | */ |
| 396 | rq->flags |= REQ_SOFTBARRIER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | rq = NULL; |
| 398 | break; |
| 399 | } else if (ret == BLKPREP_KILL) { |
| 400 | int nr_bytes = rq->hard_nr_sectors << 9; |
| 401 | |
| 402 | if (!nr_bytes) |
| 403 | nr_bytes = rq->data_len; |
| 404 | |
| 405 | blkdev_dequeue_request(rq); |
| 406 | rq->flags |= REQ_QUIET; |
| 407 | end_that_request_chunk(rq, 0, nr_bytes); |
| 408 | end_that_request_last(rq); |
| 409 | } else { |
| 410 | printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__, |
| 411 | ret); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return rq; |
| 417 | } |
| 418 | |
| 419 | void elv_remove_request(request_queue_t *q, struct request *rq) |
| 420 | { |
| 421 | elevator_t *e = q->elevator; |
| 422 | |
| 423 | /* |
| 424 | * the time frame between a request being removed from the lists |
| 425 | * and to it is freed is accounted as io that is in progress at |
| 426 | * the driver side. note that we only account requests that the |
| 427 | * driver has seen (REQ_STARTED set), to avoid false accounting |
| 428 | * for request-request merges |
| 429 | */ |
| 430 | if (blk_account_rq(rq)) |
| 431 | q->in_flight++; |
| 432 | |
| 433 | /* |
| 434 | * the main clearing point for q->last_merge is on retrieval of |
| 435 | * request by driver (it calls elv_next_request()), but it _can_ |
| 436 | * also happen here if a request is added to the queue but later |
| 437 | * deleted without ever being given to driver (merged with another |
| 438 | * request). |
| 439 | */ |
| 440 | if (rq == q->last_merge) |
| 441 | q->last_merge = NULL; |
| 442 | |
| 443 | if (e->ops->elevator_remove_req_fn) |
| 444 | e->ops->elevator_remove_req_fn(q, rq); |
| 445 | } |
| 446 | |
| 447 | int elv_queue_empty(request_queue_t *q) |
| 448 | { |
| 449 | elevator_t *e = q->elevator; |
| 450 | |
| 451 | if (e->ops->elevator_queue_empty_fn) |
| 452 | return e->ops->elevator_queue_empty_fn(q); |
| 453 | |
| 454 | return list_empty(&q->queue_head); |
| 455 | } |
| 456 | |
| 457 | struct request *elv_latter_request(request_queue_t *q, struct request *rq) |
| 458 | { |
| 459 | struct list_head *next; |
| 460 | |
| 461 | elevator_t *e = q->elevator; |
| 462 | |
| 463 | if (e->ops->elevator_latter_req_fn) |
| 464 | return e->ops->elevator_latter_req_fn(q, rq); |
| 465 | |
| 466 | next = rq->queuelist.next; |
| 467 | if (next != &q->queue_head && next != &rq->queuelist) |
| 468 | return list_entry_rq(next); |
| 469 | |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | struct request *elv_former_request(request_queue_t *q, struct request *rq) |
| 474 | { |
| 475 | struct list_head *prev; |
| 476 | |
| 477 | elevator_t *e = q->elevator; |
| 478 | |
| 479 | if (e->ops->elevator_former_req_fn) |
| 480 | return e->ops->elevator_former_req_fn(q, rq); |
| 481 | |
| 482 | prev = rq->queuelist.prev; |
| 483 | if (prev != &q->queue_head && prev != &rq->queuelist) |
| 484 | return list_entry_rq(prev); |
| 485 | |
| 486 | return NULL; |
| 487 | } |
| 488 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 489 | int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio, |
Al Viro | 8267e26 | 2005-10-21 03:20:53 -0400 | [diff] [blame^] | 490 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | elevator_t *e = q->elevator; |
| 493 | |
| 494 | if (e->ops->elevator_set_req_fn) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 495 | return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | rq->elevator_private = NULL; |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | void elv_put_request(request_queue_t *q, struct request *rq) |
| 502 | { |
| 503 | elevator_t *e = q->elevator; |
| 504 | |
| 505 | if (e->ops->elevator_put_req_fn) |
| 506 | e->ops->elevator_put_req_fn(q, rq); |
| 507 | } |
| 508 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 509 | int elv_may_queue(request_queue_t *q, int rw, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | elevator_t *e = q->elevator; |
| 512 | |
| 513 | if (e->ops->elevator_may_queue_fn) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 514 | return e->ops->elevator_may_queue_fn(q, rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
| 516 | return ELV_MQUEUE_MAY; |
| 517 | } |
| 518 | |
| 519 | void elv_completed_request(request_queue_t *q, struct request *rq) |
| 520 | { |
| 521 | elevator_t *e = q->elevator; |
| 522 | |
| 523 | /* |
| 524 | * request is released from the driver, io must be done |
| 525 | */ |
| 526 | if (blk_account_rq(rq)) |
| 527 | q->in_flight--; |
| 528 | |
| 529 | if (e->ops->elevator_completed_req_fn) |
| 530 | e->ops->elevator_completed_req_fn(q, rq); |
| 531 | } |
| 532 | |
| 533 | int elv_register_queue(struct request_queue *q) |
| 534 | { |
| 535 | elevator_t *e = q->elevator; |
| 536 | |
| 537 | e->kobj.parent = kobject_get(&q->kobj); |
| 538 | if (!e->kobj.parent) |
| 539 | return -EBUSY; |
| 540 | |
| 541 | snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched"); |
| 542 | e->kobj.ktype = e->elevator_type->elevator_ktype; |
| 543 | |
| 544 | return kobject_register(&e->kobj); |
| 545 | } |
| 546 | |
| 547 | void elv_unregister_queue(struct request_queue *q) |
| 548 | { |
| 549 | if (q) { |
| 550 | elevator_t *e = q->elevator; |
| 551 | kobject_unregister(&e->kobj); |
| 552 | kobject_put(&q->kobj); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | int elv_register(struct elevator_type *e) |
| 557 | { |
| 558 | if (elevator_find(e->elevator_name)) |
| 559 | BUG(); |
| 560 | |
| 561 | spin_lock_irq(&elv_list_lock); |
| 562 | list_add_tail(&e->list, &elv_list); |
| 563 | spin_unlock_irq(&elv_list_lock); |
| 564 | |
| 565 | printk(KERN_INFO "io scheduler %s registered", e->elevator_name); |
| 566 | if (!strcmp(e->elevator_name, chosen_elevator)) |
| 567 | printk(" (default)"); |
| 568 | printk("\n"); |
| 569 | return 0; |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(elv_register); |
| 572 | |
| 573 | void elv_unregister(struct elevator_type *e) |
| 574 | { |
| 575 | spin_lock_irq(&elv_list_lock); |
| 576 | list_del_init(&e->list); |
| 577 | spin_unlock_irq(&elv_list_lock); |
| 578 | } |
| 579 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 580 | |
| 581 | /* |
| 582 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 583 | * we don't free the old io scheduler, before we have allocated what we |
| 584 | * need for the new one. this way we have a chance of going back to the old |
| 585 | * one, if the new one fails init for some reason. we also do an intermediate |
| 586 | * switch to noop to ensure safety with stack-allocated requests, since they |
| 587 | * don't originate from the block layer allocator. noop is safe here, because |
| 588 | * it never needs to touch the elevator itself for completion events. DRAIN |
| 589 | * flags will make sure we don't touch it for additions either. |
| 590 | */ |
| 591 | static void elevator_switch(request_queue_t *q, struct elevator_type *new_e) |
| 592 | { |
| 593 | elevator_t *e = kmalloc(sizeof(elevator_t), GFP_KERNEL); |
| 594 | struct elevator_type *noop_elevator = NULL; |
| 595 | elevator_t *old_elevator; |
| 596 | |
| 597 | if (!e) |
| 598 | goto error; |
| 599 | |
| 600 | /* |
| 601 | * first step, drain requests from the block freelist |
| 602 | */ |
| 603 | blk_wait_queue_drained(q, 0); |
| 604 | |
| 605 | /* |
| 606 | * unregister old elevator data |
| 607 | */ |
| 608 | elv_unregister_queue(q); |
| 609 | old_elevator = q->elevator; |
| 610 | |
| 611 | /* |
| 612 | * next step, switch to noop since it uses no private rq structures |
| 613 | * and doesn't allocate any memory for anything. then wait for any |
| 614 | * non-fs requests in-flight |
| 615 | */ |
| 616 | noop_elevator = elevator_get("noop"); |
| 617 | spin_lock_irq(q->queue_lock); |
| 618 | elevator_attach(q, noop_elevator, e); |
| 619 | spin_unlock_irq(q->queue_lock); |
| 620 | |
| 621 | blk_wait_queue_drained(q, 1); |
| 622 | |
| 623 | /* |
| 624 | * attach and start new elevator |
| 625 | */ |
| 626 | if (elevator_attach(q, new_e, e)) |
| 627 | goto fail; |
| 628 | |
| 629 | if (elv_register_queue(q)) |
| 630 | goto fail_register; |
| 631 | |
| 632 | /* |
| 633 | * finally exit old elevator and start queue again |
| 634 | */ |
| 635 | elevator_exit(old_elevator); |
| 636 | blk_finish_queue_drain(q); |
| 637 | elevator_put(noop_elevator); |
| 638 | return; |
| 639 | |
| 640 | fail_register: |
| 641 | /* |
| 642 | * switch failed, exit the new io scheduler and reattach the old |
| 643 | * one again (along with re-adding the sysfs dir) |
| 644 | */ |
| 645 | elevator_exit(e); |
| 646 | fail: |
| 647 | q->elevator = old_elevator; |
| 648 | elv_register_queue(q); |
| 649 | blk_finish_queue_drain(q); |
| 650 | error: |
| 651 | if (noop_elevator) |
| 652 | elevator_put(noop_elevator); |
| 653 | elevator_put(new_e); |
| 654 | printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name); |
| 655 | } |
| 656 | |
| 657 | ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count) |
| 658 | { |
| 659 | char elevator_name[ELV_NAME_MAX]; |
| 660 | struct elevator_type *e; |
| 661 | |
| 662 | memset(elevator_name, 0, sizeof(elevator_name)); |
| 663 | strncpy(elevator_name, name, sizeof(elevator_name)); |
| 664 | |
| 665 | if (elevator_name[strlen(elevator_name) - 1] == '\n') |
| 666 | elevator_name[strlen(elevator_name) - 1] = '\0'; |
| 667 | |
| 668 | e = elevator_get(elevator_name); |
| 669 | if (!e) { |
| 670 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | |
| 674 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) |
| 675 | return count; |
| 676 | |
| 677 | elevator_switch(q, e); |
| 678 | return count; |
| 679 | } |
| 680 | |
| 681 | ssize_t elv_iosched_show(request_queue_t *q, char *name) |
| 682 | { |
| 683 | elevator_t *e = q->elevator; |
| 684 | struct elevator_type *elv = e->elevator_type; |
| 685 | struct list_head *entry; |
| 686 | int len = 0; |
| 687 | |
| 688 | spin_lock_irq(q->queue_lock); |
| 689 | list_for_each(entry, &elv_list) { |
| 690 | struct elevator_type *__e; |
| 691 | |
| 692 | __e = list_entry(entry, struct elevator_type, list); |
| 693 | if (!strcmp(elv->elevator_name, __e->elevator_name)) |
| 694 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
| 695 | else |
| 696 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 697 | } |
| 698 | spin_unlock_irq(q->queue_lock); |
| 699 | |
| 700 | len += sprintf(len+name, "\n"); |
| 701 | return len; |
| 702 | } |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | EXPORT_SYMBOL(elv_add_request); |
| 705 | EXPORT_SYMBOL(__elv_add_request); |
| 706 | EXPORT_SYMBOL(elv_requeue_request); |
| 707 | EXPORT_SYMBOL(elv_next_request); |
| 708 | EXPORT_SYMBOL(elv_remove_request); |
| 709 | EXPORT_SYMBOL(elv_queue_empty); |
| 710 | EXPORT_SYMBOL(elv_completed_request); |
| 711 | EXPORT_SYMBOL(elevator_exit); |
| 712 | EXPORT_SYMBOL(elevator_init); |