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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static struct elevator_type *elevator_find(const char *name) |
| 87 | { |
| 88 | struct elevator_type *e = NULL; |
| 89 | struct list_head *entry; |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | list_for_each(entry, &elv_list) { |
| 92 | struct elevator_type *__e; |
| 93 | |
| 94 | __e = list_entry(entry, struct elevator_type, list); |
| 95 | |
| 96 | if (!strcmp(__e->elevator_name, name)) { |
| 97 | e = __e; |
| 98 | break; |
| 99 | } |
| 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | return e; |
| 103 | } |
| 104 | |
| 105 | static void elevator_put(struct elevator_type *e) |
| 106 | { |
| 107 | module_put(e->elevator_owner); |
| 108 | } |
| 109 | |
| 110 | static struct elevator_type *elevator_get(const char *name) |
| 111 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 112 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 114 | spin_lock_irq(&elv_list_lock); |
| 115 | |
| 116 | e = elevator_find(name); |
| 117 | if (e && !try_module_get(e->elevator_owner)) |
| 118 | e = NULL; |
| 119 | |
| 120 | spin_unlock_irq(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | return e; |
| 123 | } |
| 124 | |
| 125 | static int elevator_attach(request_queue_t *q, struct elevator_type *e, |
| 126 | struct elevator_queue *eq) |
| 127 | { |
| 128 | int ret = 0; |
| 129 | |
| 130 | memset(eq, 0, sizeof(*eq)); |
| 131 | eq->ops = &e->ops; |
| 132 | eq->elevator_type = e; |
| 133 | |
| 134 | INIT_LIST_HEAD(&q->queue_head); |
| 135 | q->last_merge = NULL; |
| 136 | q->elevator = eq; |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 137 | q->end_sector = 0; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 138 | q->boundary_rq = NULL; |
| 139 | q->max_back_kb = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | if (eq->ops->elevator_init_fn) |
| 142 | ret = eq->ops->elevator_init_fn(q, eq); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | static char chosen_elevator[16]; |
| 148 | |
| 149 | static void elevator_setup_default(void) |
| 150 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 151 | struct elevator_type *e; |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* |
| 154 | * check if default is set and exists |
| 155 | */ |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 156 | if (chosen_elevator[0] && (e = elevator_get(chosen_elevator))) { |
| 157 | elevator_put(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return; |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | #if defined(CONFIG_IOSCHED_AS) |
| 162 | strcpy(chosen_elevator, "anticipatory"); |
| 163 | #elif defined(CONFIG_IOSCHED_DEADLINE) |
| 164 | strcpy(chosen_elevator, "deadline"); |
| 165 | #elif defined(CONFIG_IOSCHED_CFQ) |
| 166 | strcpy(chosen_elevator, "cfq"); |
| 167 | #elif defined(CONFIG_IOSCHED_NOOP) |
| 168 | strcpy(chosen_elevator, "noop"); |
| 169 | #else |
| 170 | #error "You must build at least 1 IO scheduler into the kernel" |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | static int __init elevator_setup(char *str) |
| 175 | { |
| 176 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | __setup("elevator=", elevator_setup); |
| 181 | |
| 182 | int elevator_init(request_queue_t *q, char *name) |
| 183 | { |
| 184 | struct elevator_type *e = NULL; |
| 185 | struct elevator_queue *eq; |
| 186 | int ret = 0; |
| 187 | |
| 188 | elevator_setup_default(); |
| 189 | |
| 190 | if (!name) |
| 191 | name = chosen_elevator; |
| 192 | |
| 193 | e = elevator_get(name); |
| 194 | if (!e) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL); |
| 198 | if (!eq) { |
| 199 | elevator_put(e->elevator_type); |
| 200 | return -ENOMEM; |
| 201 | } |
| 202 | |
| 203 | ret = elevator_attach(q, e, eq); |
| 204 | if (ret) { |
| 205 | kfree(eq); |
| 206 | elevator_put(e->elevator_type); |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | void elevator_exit(elevator_t *e) |
| 213 | { |
| 214 | if (e->ops->elevator_exit_fn) |
| 215 | e->ops->elevator_exit_fn(e); |
| 216 | |
| 217 | elevator_put(e->elevator_type); |
| 218 | e->elevator_type = NULL; |
| 219 | kfree(e); |
| 220 | } |
| 221 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 222 | /* |
| 223 | * Insert rq into dispatch queue of q. Queue lock must be held on |
| 224 | * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be |
| 225 | * appended to the dispatch queue. To be used by specific elevators. |
| 226 | */ |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 227 | void elv_dispatch_sort(request_queue_t *q, struct request *rq) |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 228 | { |
| 229 | sector_t boundary; |
| 230 | unsigned max_back; |
| 231 | struct list_head *entry; |
| 232 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame^] | 233 | if (q->last_merge == rq) |
| 234 | q->last_merge = NULL; |
| 235 | |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 236 | boundary = q->end_sector; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 237 | max_back = q->max_back_kb * 2; |
| 238 | boundary = boundary > max_back ? boundary - max_back : 0; |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 239 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 240 | list_for_each_prev(entry, &q->queue_head) { |
| 241 | struct request *pos = list_entry_rq(entry); |
| 242 | |
| 243 | if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED)) |
| 244 | break; |
| 245 | if (rq->sector >= boundary) { |
| 246 | if (pos->sector < boundary) |
| 247 | continue; |
| 248 | } else { |
| 249 | if (pos->sector >= boundary) |
| 250 | break; |
| 251 | } |
| 252 | if (rq->sector >= pos->sector) |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | list_add(&rq->queuelist, entry); |
| 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | int elv_merge(request_queue_t *q, struct request **req, struct bio *bio) |
| 260 | { |
| 261 | elevator_t *e = q->elevator; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame^] | 262 | int ret; |
| 263 | |
| 264 | if (q->last_merge) { |
| 265 | ret = elv_try_merge(q->last_merge, bio); |
| 266 | if (ret != ELEVATOR_NO_MERGE) { |
| 267 | *req = q->last_merge; |
| 268 | return ret; |
| 269 | } |
| 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (e->ops->elevator_merge_fn) |
| 273 | return e->ops->elevator_merge_fn(q, req, bio); |
| 274 | |
| 275 | return ELEVATOR_NO_MERGE; |
| 276 | } |
| 277 | |
| 278 | void elv_merged_request(request_queue_t *q, struct request *rq) |
| 279 | { |
| 280 | elevator_t *e = q->elevator; |
| 281 | |
| 282 | if (e->ops->elevator_merged_fn) |
| 283 | e->ops->elevator_merged_fn(q, rq); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame^] | 284 | |
| 285 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void elv_merge_requests(request_queue_t *q, struct request *rq, |
| 289 | struct request *next) |
| 290 | { |
| 291 | elevator_t *e = q->elevator; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | if (e->ops->elevator_merge_req_fn) |
| 294 | e->ops->elevator_merge_req_fn(q, rq, next); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame^] | 295 | |
| 296 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 299 | void elv_requeue_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
| 301 | elevator_t *e = q->elevator; |
| 302 | |
| 303 | /* |
| 304 | * it already went through dequeue, we need to decrement the |
| 305 | * in_flight count again |
| 306 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 307 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | q->in_flight--; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 309 | if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn) |
| 310 | e->ops->elevator_deactivate_req_fn(q, rq); |
| 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | rq->flags &= ~REQ_STARTED; |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* |
| 316 | * if this is the flush, requeue the original instead and drop the flush |
| 317 | */ |
| 318 | if (rq->flags & REQ_BAR_FLUSH) { |
| 319 | clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags); |
| 320 | rq = rq->end_io_data; |
| 321 | } |
| 322 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 323 | __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void __elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 327 | int plug) |
| 328 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 329 | if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) { |
| 330 | /* |
| 331 | * barriers implicitly indicate back insertion |
| 332 | */ |
| 333 | if (where == ELEVATOR_INSERT_SORT) |
| 334 | where = ELEVATOR_INSERT_BACK; |
| 335 | |
| 336 | /* |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 337 | * this request is scheduling boundary, update end_sector |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 338 | */ |
| 339 | if (blk_fs_request(rq)) { |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 340 | q->end_sector = rq_end_sector(rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 341 | q->boundary_rq = rq; |
| 342 | } |
| 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | if (plug) |
| 346 | blk_plug_device(q); |
| 347 | |
| 348 | rq->q = q; |
| 349 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 350 | if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | /* |
| 352 | * if drain is set, store the request "locally". when the drain |
| 353 | * is finished, the requests will be handed ordered to the io |
| 354 | * scheduler |
| 355 | */ |
| 356 | list_add_tail(&rq->queuelist, &q->drain_list); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
| 360 | switch (where) { |
| 361 | case ELEVATOR_INSERT_FRONT: |
| 362 | rq->flags |= REQ_SOFTBARRIER; |
| 363 | |
| 364 | list_add(&rq->queuelist, &q->queue_head); |
| 365 | break; |
| 366 | |
| 367 | case ELEVATOR_INSERT_BACK: |
| 368 | rq->flags |= REQ_SOFTBARRIER; |
| 369 | |
| 370 | while (q->elevator->ops->elevator_dispatch_fn(q, 1)) |
| 371 | ; |
| 372 | list_add_tail(&rq->queuelist, &q->queue_head); |
| 373 | /* |
| 374 | * We kick the queue here for the following reasons. |
| 375 | * - The elevator might have returned NULL previously |
| 376 | * to delay requests and returned them now. As the |
| 377 | * queue wasn't empty before this request, ll_rw_blk |
| 378 | * won't run the queue on return, resulting in hang. |
| 379 | * - Usually, back inserted requests won't be merged |
| 380 | * with anything. There's no point in delaying queue |
| 381 | * processing. |
| 382 | */ |
| 383 | blk_remove_plug(q); |
| 384 | q->request_fn(q); |
| 385 | break; |
| 386 | |
| 387 | case ELEVATOR_INSERT_SORT: |
| 388 | BUG_ON(!blk_fs_request(rq)); |
| 389 | rq->flags |= REQ_SORTED; |
| 390 | q->elevator->ops->elevator_add_req_fn(q, rq); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame^] | 391 | if (q->last_merge == NULL && rq_mergeable(rq)) |
| 392 | q->last_merge = rq; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | default: |
| 396 | printk(KERN_ERR "%s: bad insertion point %d\n", |
| 397 | __FUNCTION__, where); |
| 398 | BUG(); |
| 399 | } |
| 400 | |
| 401 | if (blk_queue_plugged(q)) { |
| 402 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] |
| 403 | - q->in_flight; |
| 404 | |
| 405 | if (nrq >= q->unplug_thresh) |
| 406 | __generic_unplug_device(q); |
| 407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 411 | int plug) |
| 412 | { |
| 413 | unsigned long flags; |
| 414 | |
| 415 | spin_lock_irqsave(q->queue_lock, flags); |
| 416 | __elv_add_request(q, rq, where, plug); |
| 417 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 418 | } |
| 419 | |
| 420 | static inline struct request *__elv_next_request(request_queue_t *q) |
| 421 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 422 | struct request *rq; |
| 423 | |
| 424 | if (unlikely(list_empty(&q->queue_head) && |
| 425 | !q->elevator->ops->elevator_dispatch_fn(q, 0))) |
| 426 | return NULL; |
| 427 | |
| 428 | rq = list_entry_rq(q->queue_head.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * if this is a barrier write and the device has to issue a |
| 432 | * flush sequence to support it, check how far we are |
| 433 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 434 | if (blk_fs_request(rq) && blk_barrier_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | BUG_ON(q->ordered == QUEUE_ORDERED_NONE); |
| 436 | |
| 437 | if (q->ordered == QUEUE_ORDERED_FLUSH && |
| 438 | !blk_barrier_preflush(rq)) |
| 439 | rq = blk_start_pre_flush(q, rq); |
| 440 | } |
| 441 | |
| 442 | return rq; |
| 443 | } |
| 444 | |
| 445 | struct request *elv_next_request(request_queue_t *q) |
| 446 | { |
| 447 | struct request *rq; |
| 448 | int ret; |
| 449 | |
| 450 | while ((rq = __elv_next_request(q)) != NULL) { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 451 | if (!(rq->flags & REQ_STARTED)) { |
| 452 | elevator_t *e = q->elevator; |
| 453 | |
| 454 | /* |
| 455 | * This is the first time the device driver |
| 456 | * sees this request (possibly after |
| 457 | * requeueing). Notify IO scheduler. |
| 458 | */ |
| 459 | if (blk_sorted_rq(rq) && |
| 460 | e->ops->elevator_activate_req_fn) |
| 461 | e->ops->elevator_activate_req_fn(q, rq); |
| 462 | |
| 463 | /* |
| 464 | * just mark as started even if we don't start |
| 465 | * it, a request that has been delayed should |
| 466 | * not be passed by new incoming requests |
| 467 | */ |
| 468 | rq->flags |= REQ_STARTED; |
| 469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 471 | if (!q->boundary_rq || q->boundary_rq == rq) { |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 472 | q->end_sector = rq_end_sector(rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 473 | q->boundary_rq = NULL; |
| 474 | } |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn) |
| 477 | break; |
| 478 | |
| 479 | ret = q->prep_rq_fn(q, rq); |
| 480 | if (ret == BLKPREP_OK) { |
| 481 | break; |
| 482 | } else if (ret == BLKPREP_DEFER) { |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 483 | /* |
| 484 | * the request may have been (partially) prepped. |
| 485 | * we need to keep this request in the front to |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 486 | * avoid resource deadlock. REQ_STARTED will |
| 487 | * prevent other fs requests from passing this one. |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 488 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | rq = NULL; |
| 490 | break; |
| 491 | } else if (ret == BLKPREP_KILL) { |
| 492 | int nr_bytes = rq->hard_nr_sectors << 9; |
| 493 | |
| 494 | if (!nr_bytes) |
| 495 | nr_bytes = rq->data_len; |
| 496 | |
| 497 | blkdev_dequeue_request(rq); |
| 498 | rq->flags |= REQ_QUIET; |
| 499 | end_that_request_chunk(rq, 0, nr_bytes); |
| 500 | end_that_request_last(rq); |
| 501 | } else { |
| 502 | printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__, |
| 503 | ret); |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return rq; |
| 509 | } |
| 510 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 511 | void elv_dequeue_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 513 | BUG_ON(list_empty(&rq->queuelist)); |
| 514 | |
| 515 | list_del_init(&rq->queuelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | /* |
| 518 | * the time frame between a request being removed from the lists |
| 519 | * and to it is freed is accounted as io that is in progress at |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 520 | * the driver side. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | */ |
| 522 | if (blk_account_rq(rq)) |
| 523 | q->in_flight++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | int elv_queue_empty(request_queue_t *q) |
| 527 | { |
| 528 | elevator_t *e = q->elevator; |
| 529 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 530 | if (!list_empty(&q->queue_head)) |
| 531 | return 0; |
| 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (e->ops->elevator_queue_empty_fn) |
| 534 | return e->ops->elevator_queue_empty_fn(q); |
| 535 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 536 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | struct request *elv_latter_request(request_queue_t *q, struct request *rq) |
| 540 | { |
| 541 | struct list_head *next; |
| 542 | |
| 543 | elevator_t *e = q->elevator; |
| 544 | |
| 545 | if (e->ops->elevator_latter_req_fn) |
| 546 | return e->ops->elevator_latter_req_fn(q, rq); |
| 547 | |
| 548 | next = rq->queuelist.next; |
| 549 | if (next != &q->queue_head && next != &rq->queuelist) |
| 550 | return list_entry_rq(next); |
| 551 | |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | struct request *elv_former_request(request_queue_t *q, struct request *rq) |
| 556 | { |
| 557 | struct list_head *prev; |
| 558 | |
| 559 | elevator_t *e = q->elevator; |
| 560 | |
| 561 | if (e->ops->elevator_former_req_fn) |
| 562 | return e->ops->elevator_former_req_fn(q, rq); |
| 563 | |
| 564 | prev = rq->queuelist.prev; |
| 565 | if (prev != &q->queue_head && prev != &rq->queuelist) |
| 566 | return list_entry_rq(prev); |
| 567 | |
| 568 | return NULL; |
| 569 | } |
| 570 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 571 | int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio, |
| 572 | int gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | { |
| 574 | elevator_t *e = q->elevator; |
| 575 | |
| 576 | if (e->ops->elevator_set_req_fn) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 577 | return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | rq->elevator_private = NULL; |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | void elv_put_request(request_queue_t *q, struct request *rq) |
| 584 | { |
| 585 | elevator_t *e = q->elevator; |
| 586 | |
| 587 | if (e->ops->elevator_put_req_fn) |
| 588 | e->ops->elevator_put_req_fn(q, rq); |
| 589 | } |
| 590 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 591 | 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] | 592 | { |
| 593 | elevator_t *e = q->elevator; |
| 594 | |
| 595 | if (e->ops->elevator_may_queue_fn) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 596 | return e->ops->elevator_may_queue_fn(q, rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | return ELV_MQUEUE_MAY; |
| 599 | } |
| 600 | |
| 601 | void elv_completed_request(request_queue_t *q, struct request *rq) |
| 602 | { |
| 603 | elevator_t *e = q->elevator; |
| 604 | |
| 605 | /* |
| 606 | * request is released from the driver, io must be done |
| 607 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 608 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | q->in_flight--; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 610 | if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn) |
| 611 | e->ops->elevator_completed_req_fn(q, rq); |
| 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | int elv_register_queue(struct request_queue *q) |
| 616 | { |
| 617 | elevator_t *e = q->elevator; |
| 618 | |
| 619 | e->kobj.parent = kobject_get(&q->kobj); |
| 620 | if (!e->kobj.parent) |
| 621 | return -EBUSY; |
| 622 | |
| 623 | snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched"); |
| 624 | e->kobj.ktype = e->elevator_type->elevator_ktype; |
| 625 | |
| 626 | return kobject_register(&e->kobj); |
| 627 | } |
| 628 | |
| 629 | void elv_unregister_queue(struct request_queue *q) |
| 630 | { |
| 631 | if (q) { |
| 632 | elevator_t *e = q->elevator; |
| 633 | kobject_unregister(&e->kobj); |
| 634 | kobject_put(&q->kobj); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | int elv_register(struct elevator_type *e) |
| 639 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 640 | spin_lock_irq(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | if (elevator_find(e->elevator_name)) |
| 642 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | list_add_tail(&e->list, &elv_list); |
| 644 | spin_unlock_irq(&elv_list_lock); |
| 645 | |
| 646 | printk(KERN_INFO "io scheduler %s registered", e->elevator_name); |
| 647 | if (!strcmp(e->elevator_name, chosen_elevator)) |
| 648 | printk(" (default)"); |
| 649 | printk("\n"); |
| 650 | return 0; |
| 651 | } |
| 652 | EXPORT_SYMBOL_GPL(elv_register); |
| 653 | |
| 654 | void elv_unregister(struct elevator_type *e) |
| 655 | { |
| 656 | spin_lock_irq(&elv_list_lock); |
| 657 | list_del_init(&e->list); |
| 658 | spin_unlock_irq(&elv_list_lock); |
| 659 | } |
| 660 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 661 | |
| 662 | /* |
| 663 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 664 | * we don't free the old io scheduler, before we have allocated what we |
| 665 | * need for the new one. this way we have a chance of going back to the old |
| 666 | * one, if the new one fails init for some reason. we also do an intermediate |
| 667 | * switch to noop to ensure safety with stack-allocated requests, since they |
| 668 | * don't originate from the block layer allocator. noop is safe here, because |
| 669 | * it never needs to touch the elevator itself for completion events. DRAIN |
| 670 | * flags will make sure we don't touch it for additions either. |
| 671 | */ |
| 672 | static void elevator_switch(request_queue_t *q, struct elevator_type *new_e) |
| 673 | { |
| 674 | elevator_t *e = kmalloc(sizeof(elevator_t), GFP_KERNEL); |
| 675 | struct elevator_type *noop_elevator = NULL; |
| 676 | elevator_t *old_elevator; |
| 677 | |
| 678 | if (!e) |
| 679 | goto error; |
| 680 | |
| 681 | /* |
| 682 | * first step, drain requests from the block freelist |
| 683 | */ |
| 684 | blk_wait_queue_drained(q, 0); |
| 685 | |
| 686 | /* |
| 687 | * unregister old elevator data |
| 688 | */ |
| 689 | elv_unregister_queue(q); |
| 690 | old_elevator = q->elevator; |
| 691 | |
| 692 | /* |
| 693 | * next step, switch to noop since it uses no private rq structures |
| 694 | * and doesn't allocate any memory for anything. then wait for any |
| 695 | * non-fs requests in-flight |
| 696 | */ |
| 697 | noop_elevator = elevator_get("noop"); |
| 698 | spin_lock_irq(q->queue_lock); |
| 699 | elevator_attach(q, noop_elevator, e); |
| 700 | spin_unlock_irq(q->queue_lock); |
| 701 | |
| 702 | blk_wait_queue_drained(q, 1); |
| 703 | |
| 704 | /* |
| 705 | * attach and start new elevator |
| 706 | */ |
| 707 | if (elevator_attach(q, new_e, e)) |
| 708 | goto fail; |
| 709 | |
| 710 | if (elv_register_queue(q)) |
| 711 | goto fail_register; |
| 712 | |
| 713 | /* |
| 714 | * finally exit old elevator and start queue again |
| 715 | */ |
| 716 | elevator_exit(old_elevator); |
| 717 | blk_finish_queue_drain(q); |
| 718 | elevator_put(noop_elevator); |
| 719 | return; |
| 720 | |
| 721 | fail_register: |
| 722 | /* |
| 723 | * switch failed, exit the new io scheduler and reattach the old |
| 724 | * one again (along with re-adding the sysfs dir) |
| 725 | */ |
| 726 | elevator_exit(e); |
| 727 | fail: |
| 728 | q->elevator = old_elevator; |
| 729 | elv_register_queue(q); |
| 730 | blk_finish_queue_drain(q); |
| 731 | error: |
| 732 | if (noop_elevator) |
| 733 | elevator_put(noop_elevator); |
| 734 | elevator_put(new_e); |
| 735 | printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name); |
| 736 | } |
| 737 | |
| 738 | ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count) |
| 739 | { |
| 740 | char elevator_name[ELV_NAME_MAX]; |
| 741 | struct elevator_type *e; |
| 742 | |
| 743 | memset(elevator_name, 0, sizeof(elevator_name)); |
| 744 | strncpy(elevator_name, name, sizeof(elevator_name)); |
| 745 | |
| 746 | if (elevator_name[strlen(elevator_name) - 1] == '\n') |
| 747 | elevator_name[strlen(elevator_name) - 1] = '\0'; |
| 748 | |
| 749 | e = elevator_get(elevator_name); |
| 750 | if (!e) { |
| 751 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); |
| 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) |
| 756 | return count; |
| 757 | |
| 758 | elevator_switch(q, e); |
| 759 | return count; |
| 760 | } |
| 761 | |
| 762 | ssize_t elv_iosched_show(request_queue_t *q, char *name) |
| 763 | { |
| 764 | elevator_t *e = q->elevator; |
| 765 | struct elevator_type *elv = e->elevator_type; |
| 766 | struct list_head *entry; |
| 767 | int len = 0; |
| 768 | |
| 769 | spin_lock_irq(q->queue_lock); |
| 770 | list_for_each(entry, &elv_list) { |
| 771 | struct elevator_type *__e; |
| 772 | |
| 773 | __e = list_entry(entry, struct elevator_type, list); |
| 774 | if (!strcmp(elv->elevator_name, __e->elevator_name)) |
| 775 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
| 776 | else |
| 777 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 778 | } |
| 779 | spin_unlock_irq(q->queue_lock); |
| 780 | |
| 781 | len += sprintf(len+name, "\n"); |
| 782 | return len; |
| 783 | } |
| 784 | |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 785 | EXPORT_SYMBOL(elv_dispatch_sort); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | EXPORT_SYMBOL(elv_add_request); |
| 787 | EXPORT_SYMBOL(__elv_add_request); |
| 788 | EXPORT_SYMBOL(elv_requeue_request); |
| 789 | EXPORT_SYMBOL(elv_next_request); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 790 | EXPORT_SYMBOL(elv_dequeue_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | EXPORT_SYMBOL(elv_queue_empty); |
| 792 | EXPORT_SYMBOL(elv_completed_request); |
| 793 | EXPORT_SYMBOL(elevator_exit); |
| 794 | EXPORT_SYMBOL(elevator_init); |