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