Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Block device elevator/IO-scheduler. |
| 3 | * |
| 4 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE |
| 5 | * |
| 6 | * 30042000 Jens Axboe <axboe@suse.de> : |
| 7 | * |
| 8 | * Split the elevator a bit so that it is possible to choose a different |
| 9 | * one or even write a new "plug in". There are three pieces: |
| 10 | * - elevator_fn, inserts a new request in the queue list |
| 11 | * - elevator_merge_fn, decides whether a new buffer can be merged with |
| 12 | * an existing request |
| 13 | * - elevator_dequeue_fn, called when a request is taken off the active list |
| 14 | * |
| 15 | * 20082000 Dave Jones <davej@suse.de> : |
| 16 | * Removed tests for max-bomb-segments, which was breaking elvtune |
| 17 | * when run without -bN |
| 18 | * |
| 19 | * Jens: |
| 20 | * - Rework again to work with bio instead of buffer_heads |
| 21 | * - loose bi_dev comparisons, partition handling is right now |
| 22 | * - completely modularize elevator setup and teardown |
| 23 | * |
| 24 | */ |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/blkdev.h> |
| 28 | #include <linux/elevator.h> |
| 29 | #include <linux/bio.h> |
| 30 | #include <linux/config.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/compiler.h> |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 35 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/uaccess.h> |
| 38 | |
| 39 | static DEFINE_SPINLOCK(elv_list_lock); |
| 40 | static LIST_HEAD(elv_list); |
| 41 | |
| 42 | /* |
| 43 | * can we safely merge with this request? |
| 44 | */ |
| 45 | inline int elv_rq_merge_ok(struct request *rq, struct bio *bio) |
| 46 | { |
| 47 | if (!rq_mergeable(rq)) |
| 48 | return 0; |
| 49 | |
| 50 | /* |
| 51 | * different data direction or already started, don't merge |
| 52 | */ |
| 53 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 54 | return 0; |
| 55 | |
| 56 | /* |
| 57 | * same device and no special stuff set, merge is ok |
| 58 | */ |
| 59 | if (rq->rq_disk == bio->bi_bdev->bd_disk && |
| 60 | !rq->waiting && !rq->special) |
| 61 | return 1; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | EXPORT_SYMBOL(elv_rq_merge_ok); |
| 66 | |
Coywolf Qi Hunt | 769db45 | 2005-12-28 10:55:49 +0100 | [diff] [blame] | 67 | static inline int elv_try_merge(struct request *__rq, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | int ret = ELEVATOR_NO_MERGE; |
| 70 | |
| 71 | /* |
| 72 | * we can merge and sequence is ok, check if it's possible |
| 73 | */ |
| 74 | if (elv_rq_merge_ok(__rq, bio)) { |
| 75 | if (__rq->sector + __rq->nr_sectors == bio->bi_sector) |
| 76 | ret = ELEVATOR_BACK_MERGE; |
| 77 | else if (__rq->sector - bio_sectors(bio) == bio->bi_sector) |
| 78 | ret = ELEVATOR_FRONT_MERGE; |
| 79 | } |
| 80 | |
| 81 | return ret; |
| 82 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static struct elevator_type *elevator_find(const char *name) |
| 85 | { |
| 86 | struct elevator_type *e = NULL; |
| 87 | struct list_head *entry; |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | list_for_each(entry, &elv_list) { |
| 90 | struct elevator_type *__e; |
| 91 | |
| 92 | __e = list_entry(entry, struct elevator_type, list); |
| 93 | |
| 94 | if (!strcmp(__e->elevator_name, name)) { |
| 95 | e = __e; |
| 96 | break; |
| 97 | } |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | return e; |
| 101 | } |
| 102 | |
| 103 | static void elevator_put(struct elevator_type *e) |
| 104 | { |
| 105 | module_put(e->elevator_owner); |
| 106 | } |
| 107 | |
| 108 | static struct elevator_type *elevator_get(const char *name) |
| 109 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 110 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 112 | spin_lock_irq(&elv_list_lock); |
| 113 | |
| 114 | e = elevator_find(name); |
| 115 | if (e && !try_module_get(e->elevator_owner)) |
| 116 | e = NULL; |
| 117 | |
| 118 | spin_unlock_irq(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | return e; |
| 121 | } |
| 122 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 123 | static int elevator_attach(request_queue_t *q, struct elevator_queue *eq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | int ret = 0; |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | q->elevator = eq; |
| 128 | |
| 129 | if (eq->ops->elevator_init_fn) |
| 130 | ret = eq->ops->elevator_init_fn(q, eq); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | static char chosen_elevator[16]; |
| 136 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 137 | static int __init elevator_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Chuck Ebbert | 752a3b7 | 2006-01-16 09:47:37 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Be backwards-compatible with previous kernels, so users |
| 141 | * won't get the wrong elevator. |
| 142 | */ |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 143 | if (!strcmp(str, "as")) |
Chuck Ebbert | 752a3b7 | 2006-01-16 09:47:37 +0100 | [diff] [blame] | 144 | strcpy(chosen_elevator, "anticipatory"); |
Zachary Amsden | cff3ba2 | 2005-11-09 13:24:20 +0100 | [diff] [blame] | 145 | else |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 146 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | __setup("elevator=", elevator_setup); |
| 151 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 152 | static struct kobj_type elv_ktype; |
| 153 | |
| 154 | static elevator_t *elevator_alloc(struct elevator_type *e) |
| 155 | { |
| 156 | elevator_t *eq = kmalloc(sizeof(elevator_t), GFP_KERNEL); |
| 157 | if (eq) { |
| 158 | memset(eq, 0, sizeof(*eq)); |
| 159 | eq->ops = &e->ops; |
| 160 | eq->elevator_type = e; |
| 161 | kobject_init(&eq->kobj); |
| 162 | snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched"); |
| 163 | eq->kobj.ktype = &elv_ktype; |
| 164 | mutex_init(&eq->sysfs_lock); |
| 165 | } else { |
| 166 | elevator_put(e); |
| 167 | } |
| 168 | return eq; |
| 169 | } |
| 170 | |
| 171 | static void elevator_release(struct kobject *kobj) |
| 172 | { |
| 173 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
| 174 | elevator_put(e->elevator_type); |
| 175 | kfree(e); |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 189 | if (name && !(e = elevator_get(name))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return -EINVAL; |
| 191 | |
Nate Diller | 248d5ca | 2006-01-24 10:09:14 +0100 | [diff] [blame] | 192 | if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator))) |
| 193 | printk("I/O scheduler %s not found\n", chosen_elevator); |
| 194 | |
| 195 | if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) { |
| 196 | printk("Default I/O scheduler not found, using no-op\n"); |
| 197 | e = elevator_get("noop"); |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 200 | eq = elevator_alloc(e); |
| 201 | if (!eq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 204 | ret = elevator_attach(q, eq); |
| 205 | if (ret) |
| 206 | kobject_put(&eq->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | void elevator_exit(elevator_t *e) |
| 212 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 213 | mutex_lock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (e->ops->elevator_exit_fn) |
| 215 | e->ops->elevator_exit_fn(e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 216 | e->ops = NULL; |
| 217 | mutex_unlock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 219 | kobject_put(&e->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 230 | struct list_head *entry; |
| 231 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 232 | if (q->last_merge == rq) |
| 233 | q->last_merge = NULL; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 234 | q->nr_sorted--; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 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 | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 293 | q->nr_sorted--; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 294 | |
| 295 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 298 | void elv_requeue_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
| 300 | elevator_t *e = q->elevator; |
| 301 | |
| 302 | /* |
| 303 | * it already went through dequeue, we need to decrement the |
| 304 | * in_flight count again |
| 305 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 306 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | q->in_flight--; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 308 | if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn) |
| 309 | e->ops->elevator_deactivate_req_fn(q, rq); |
| 310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | rq->flags &= ~REQ_STARTED; |
| 313 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 314 | elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 317 | static void elv_drain_elevator(request_queue_t *q) |
| 318 | { |
| 319 | static int printed; |
| 320 | while (q->elevator->ops->elevator_dispatch_fn(q, 1)) |
| 321 | ; |
| 322 | if (q->nr_sorted == 0) |
| 323 | return; |
| 324 | if (printed++ < 10) { |
| 325 | printk(KERN_ERR "%s: forced dispatching is broken " |
| 326 | "(nr_sorted=%u), please report this\n", |
| 327 | q->elevator->elevator_type->elevator_name, q->nr_sorted); |
| 328 | } |
| 329 | } |
| 330 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 331 | void elv_insert(request_queue_t *q, struct request *rq, int where) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 333 | struct list_head *pos; |
| 334 | unsigned ordseq; |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | rq->q = q; |
| 337 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 338 | switch (where) { |
| 339 | case ELEVATOR_INSERT_FRONT: |
| 340 | rq->flags |= REQ_SOFTBARRIER; |
| 341 | |
| 342 | list_add(&rq->queuelist, &q->queue_head); |
| 343 | break; |
| 344 | |
| 345 | case ELEVATOR_INSERT_BACK: |
| 346 | rq->flags |= REQ_SOFTBARRIER; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 347 | elv_drain_elevator(q); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 348 | list_add_tail(&rq->queuelist, &q->queue_head); |
| 349 | /* |
| 350 | * We kick the queue here for the following reasons. |
| 351 | * - The elevator might have returned NULL previously |
| 352 | * to delay requests and returned them now. As the |
| 353 | * queue wasn't empty before this request, ll_rw_blk |
| 354 | * won't run the queue on return, resulting in hang. |
| 355 | * - Usually, back inserted requests won't be merged |
| 356 | * with anything. There's no point in delaying queue |
| 357 | * processing. |
| 358 | */ |
| 359 | blk_remove_plug(q); |
| 360 | q->request_fn(q); |
| 361 | break; |
| 362 | |
| 363 | case ELEVATOR_INSERT_SORT: |
| 364 | BUG_ON(!blk_fs_request(rq)); |
| 365 | rq->flags |= REQ_SORTED; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 366 | q->nr_sorted++; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 367 | if (q->last_merge == NULL && rq_mergeable(rq)) |
| 368 | q->last_merge = rq; |
Tejun Heo | ca23509 | 2005-11-01 17:23:49 +0900 | [diff] [blame] | 369 | /* |
| 370 | * Some ioscheds (cfq) run q->request_fn directly, so |
| 371 | * rq cannot be accessed after calling |
| 372 | * elevator_add_req_fn. |
| 373 | */ |
| 374 | q->elevator->ops->elevator_add_req_fn(q, rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 375 | break; |
| 376 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 377 | case ELEVATOR_INSERT_REQUEUE: |
| 378 | /* |
| 379 | * If ordered flush isn't in progress, we do front |
| 380 | * insertion; otherwise, requests should be requeued |
| 381 | * in ordseq order. |
| 382 | */ |
| 383 | rq->flags |= REQ_SOFTBARRIER; |
| 384 | |
| 385 | if (q->ordseq == 0) { |
| 386 | list_add(&rq->queuelist, &q->queue_head); |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | ordseq = blk_ordered_req_seq(rq); |
| 391 | |
| 392 | list_for_each(pos, &q->queue_head) { |
| 393 | struct request *pos_rq = list_entry_rq(pos); |
| 394 | if (ordseq <= blk_ordered_req_seq(pos_rq)) |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | list_add_tail(&rq->queuelist, pos); |
| 399 | break; |
| 400 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 401 | default: |
| 402 | printk(KERN_ERR "%s: bad insertion point %d\n", |
| 403 | __FUNCTION__, where); |
| 404 | BUG(); |
| 405 | } |
| 406 | |
| 407 | if (blk_queue_plugged(q)) { |
| 408 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] |
| 409 | - q->in_flight; |
| 410 | |
| 411 | if (nrq >= q->unplug_thresh) |
| 412 | __generic_unplug_device(q); |
| 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 416 | void __elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 417 | int plug) |
| 418 | { |
| 419 | if (q->ordcolor) |
| 420 | rq->flags |= REQ_ORDERED_COLOR; |
| 421 | |
| 422 | if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) { |
| 423 | /* |
| 424 | * toggle ordered color |
| 425 | */ |
| 426 | if (blk_barrier_rq(rq)) |
| 427 | q->ordcolor ^= 1; |
| 428 | |
| 429 | /* |
| 430 | * barriers implicitly indicate back insertion |
| 431 | */ |
| 432 | if (where == ELEVATOR_INSERT_SORT) |
| 433 | where = ELEVATOR_INSERT_BACK; |
| 434 | |
| 435 | /* |
| 436 | * this request is scheduling boundary, update |
| 437 | * end_sector |
| 438 | */ |
| 439 | if (blk_fs_request(rq)) { |
| 440 | q->end_sector = rq_end_sector(rq); |
| 441 | q->boundary_rq = rq; |
| 442 | } |
| 443 | } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT) |
| 444 | where = ELEVATOR_INSERT_BACK; |
| 445 | |
| 446 | if (plug) |
| 447 | blk_plug_device(q); |
| 448 | |
| 449 | elv_insert(q, rq, where); |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | void elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 453 | int plug) |
| 454 | { |
| 455 | unsigned long flags; |
| 456 | |
| 457 | spin_lock_irqsave(q->queue_lock, flags); |
| 458 | __elv_add_request(q, rq, where, plug); |
| 459 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 460 | } |
| 461 | |
| 462 | static inline struct request *__elv_next_request(request_queue_t *q) |
| 463 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 464 | struct request *rq; |
| 465 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 466 | while (1) { |
| 467 | while (!list_empty(&q->queue_head)) { |
| 468 | rq = list_entry_rq(q->queue_head.next); |
| 469 | if (blk_do_ordered(q, &rq)) |
| 470 | return rq; |
| 471 | } |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 472 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 473 | if (!q->elevator->ops->elevator_dispatch_fn(q, 0)) |
| 474 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | struct request *elv_next_request(request_queue_t *q) |
| 479 | { |
| 480 | struct request *rq; |
| 481 | int ret; |
| 482 | |
| 483 | while ((rq = __elv_next_request(q)) != NULL) { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 484 | if (!(rq->flags & REQ_STARTED)) { |
| 485 | elevator_t *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 487 | /* |
| 488 | * This is the first time the device driver |
| 489 | * sees this request (possibly after |
| 490 | * requeueing). Notify IO scheduler. |
| 491 | */ |
| 492 | if (blk_sorted_rq(rq) && |
| 493 | e->ops->elevator_activate_req_fn) |
| 494 | e->ops->elevator_activate_req_fn(q, rq); |
| 495 | |
| 496 | /* |
| 497 | * just mark as started even if we don't start |
| 498 | * it, a request that has been delayed should |
| 499 | * not be passed by new incoming requests |
| 500 | */ |
| 501 | rq->flags |= REQ_STARTED; |
| 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 504 | if (!q->boundary_rq || q->boundary_rq == rq) { |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 505 | q->end_sector = rq_end_sector(rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 506 | q->boundary_rq = NULL; |
| 507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn) |
| 510 | break; |
| 511 | |
| 512 | ret = q->prep_rq_fn(q, rq); |
| 513 | if (ret == BLKPREP_OK) { |
| 514 | break; |
| 515 | } else if (ret == BLKPREP_DEFER) { |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 516 | /* |
| 517 | * the request may have been (partially) prepped. |
| 518 | * we need to keep this request in the front to |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 519 | * avoid resource deadlock. REQ_STARTED will |
| 520 | * prevent other fs requests from passing this one. |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 521 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | rq = NULL; |
| 523 | break; |
| 524 | } else if (ret == BLKPREP_KILL) { |
| 525 | int nr_bytes = rq->hard_nr_sectors << 9; |
| 526 | |
| 527 | if (!nr_bytes) |
| 528 | nr_bytes = rq->data_len; |
| 529 | |
| 530 | blkdev_dequeue_request(rq); |
| 531 | rq->flags |= REQ_QUIET; |
| 532 | end_that_request_chunk(rq, 0, nr_bytes); |
Tejun Heo | 8ffdc65 | 2006-01-06 09:49:03 +0100 | [diff] [blame] | 533 | end_that_request_last(rq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } else { |
| 535 | printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__, |
| 536 | ret); |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return rq; |
| 542 | } |
| 543 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 544 | void elv_dequeue_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 546 | BUG_ON(list_empty(&rq->queuelist)); |
| 547 | |
| 548 | list_del_init(&rq->queuelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* |
| 551 | * the time frame between a request being removed from the lists |
| 552 | * 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] | 553 | * the driver side. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | */ |
| 555 | if (blk_account_rq(rq)) |
| 556 | q->in_flight++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | int elv_queue_empty(request_queue_t *q) |
| 560 | { |
| 561 | elevator_t *e = q->elevator; |
| 562 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 563 | if (!list_empty(&q->queue_head)) |
| 564 | return 0; |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (e->ops->elevator_queue_empty_fn) |
| 567 | return e->ops->elevator_queue_empty_fn(q); |
| 568 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 569 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | struct request *elv_latter_request(request_queue_t *q, struct request *rq) |
| 573 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | elevator_t *e = q->elevator; |
| 575 | |
| 576 | if (e->ops->elevator_latter_req_fn) |
| 577 | return e->ops->elevator_latter_req_fn(q, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | return NULL; |
| 579 | } |
| 580 | |
| 581 | struct request *elv_former_request(request_queue_t *q, struct request *rq) |
| 582 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | elevator_t *e = q->elevator; |
| 584 | |
| 585 | if (e->ops->elevator_former_req_fn) |
| 586 | return e->ops->elevator_former_req_fn(q, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | return NULL; |
| 588 | } |
| 589 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 590 | 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] | 591 | gfp_t gfp_mask) |
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_set_req_fn) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 596 | return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | rq->elevator_private = NULL; |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | void elv_put_request(request_queue_t *q, struct request *rq) |
| 603 | { |
| 604 | elevator_t *e = q->elevator; |
| 605 | |
| 606 | if (e->ops->elevator_put_req_fn) |
| 607 | e->ops->elevator_put_req_fn(q, rq); |
| 608 | } |
| 609 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 610 | 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] | 611 | { |
| 612 | elevator_t *e = q->elevator; |
| 613 | |
| 614 | if (e->ops->elevator_may_queue_fn) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 615 | return e->ops->elevator_may_queue_fn(q, rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | return ELV_MQUEUE_MAY; |
| 618 | } |
| 619 | |
| 620 | void elv_completed_request(request_queue_t *q, struct request *rq) |
| 621 | { |
| 622 | elevator_t *e = q->elevator; |
| 623 | |
| 624 | /* |
| 625 | * request is released from the driver, io must be done |
| 626 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 627 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | q->in_flight--; |
Tejun Heo | 1bc691d | 2006-01-12 15:39:26 +0100 | [diff] [blame] | 629 | if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn) |
| 630 | e->ops->elevator_completed_req_fn(q, rq); |
| 631 | } |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 632 | |
Tejun Heo | 1bc691d | 2006-01-12 15:39:26 +0100 | [diff] [blame] | 633 | /* |
| 634 | * Check if the queue is waiting for fs requests to be |
| 635 | * drained for flush sequence. |
| 636 | */ |
| 637 | if (unlikely(q->ordseq)) { |
| 638 | struct request *first_rq = list_entry_rq(q->queue_head.next); |
| 639 | if (q->in_flight == 0 && |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 640 | blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN && |
| 641 | blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) { |
| 642 | blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0); |
| 643 | q->request_fn(q); |
| 644 | } |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 648 | #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) |
| 649 | |
| 650 | static ssize_t |
| 651 | elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 652 | { |
| 653 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
| 654 | struct elv_fs_entry *entry = to_elv(attr); |
| 655 | ssize_t error; |
| 656 | |
| 657 | if (!entry->show) |
| 658 | return -EIO; |
| 659 | |
| 660 | mutex_lock(&e->sysfs_lock); |
| 661 | error = e->ops ? entry->show(e, page) : -ENOENT; |
| 662 | mutex_unlock(&e->sysfs_lock); |
| 663 | return error; |
| 664 | } |
| 665 | |
| 666 | static ssize_t |
| 667 | elv_attr_store(struct kobject *kobj, struct attribute *attr, |
| 668 | const char *page, size_t length) |
| 669 | { |
| 670 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
| 671 | struct elv_fs_entry *entry = to_elv(attr); |
| 672 | ssize_t error; |
| 673 | |
| 674 | if (!entry->store) |
| 675 | return -EIO; |
| 676 | |
| 677 | mutex_lock(&e->sysfs_lock); |
| 678 | error = e->ops ? entry->store(e, page, length) : -ENOENT; |
| 679 | mutex_unlock(&e->sysfs_lock); |
| 680 | return error; |
| 681 | } |
| 682 | |
| 683 | static struct sysfs_ops elv_sysfs_ops = { |
| 684 | .show = elv_attr_show, |
| 685 | .store = elv_attr_store, |
| 686 | }; |
| 687 | |
| 688 | static struct kobj_type elv_ktype = { |
| 689 | .sysfs_ops = &elv_sysfs_ops, |
| 690 | .release = elevator_release, |
| 691 | }; |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | int elv_register_queue(struct request_queue *q) |
| 694 | { |
| 695 | elevator_t *e = q->elevator; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 696 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 698 | e->kobj.parent = &q->kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 700 | error = kobject_add(&e->kobj); |
| 701 | if (!error) { |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame^] | 702 | struct elv_fs_entry *attr = e->elevator_type->elevator_attrs; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 703 | if (attr) { |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame^] | 704 | while (attr->attr.name) { |
| 705 | if (sysfs_create_file(&e->kobj, &attr->attr)) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 706 | break; |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame^] | 707 | attr++; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | kobject_uevent(&e->kobj, KOBJ_ADD); |
| 711 | } |
| 712 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void elv_unregister_queue(struct request_queue *q) |
| 716 | { |
| 717 | if (q) { |
| 718 | elevator_t *e = q->elevator; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 719 | kobject_uevent(&e->kobj, KOBJ_REMOVE); |
| 720 | kobject_del(&e->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | int elv_register(struct elevator_type *e) |
| 725 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 726 | spin_lock_irq(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (elevator_find(e->elevator_name)) |
| 728 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | list_add_tail(&e->list, &elv_list); |
| 730 | spin_unlock_irq(&elv_list_lock); |
| 731 | |
| 732 | printk(KERN_INFO "io scheduler %s registered", e->elevator_name); |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 733 | if (!strcmp(e->elevator_name, chosen_elevator) || |
| 734 | (!*chosen_elevator && |
| 735 | !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED))) |
| 736 | printk(" (default)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | printk("\n"); |
| 738 | return 0; |
| 739 | } |
| 740 | EXPORT_SYMBOL_GPL(elv_register); |
| 741 | |
| 742 | void elv_unregister(struct elevator_type *e) |
| 743 | { |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 744 | struct task_struct *g, *p; |
| 745 | |
| 746 | /* |
| 747 | * Iterate every thread in the process to remove the io contexts. |
| 748 | */ |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 749 | if (e->ops.trim) { |
| 750 | read_lock(&tasklist_lock); |
| 751 | do_each_thread(g, p) { |
| 752 | task_lock(p); |
| 753 | e->ops.trim(p->io_context); |
| 754 | task_unlock(p); |
| 755 | } while_each_thread(g, p); |
| 756 | read_unlock(&tasklist_lock); |
| 757 | } |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | spin_lock_irq(&elv_list_lock); |
| 760 | list_del_init(&e->list); |
| 761 | spin_unlock_irq(&elv_list_lock); |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 764 | |
| 765 | /* |
| 766 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 767 | * we don't free the old io scheduler, before we have allocated what we |
| 768 | * 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] | 769 | * one, if the new one fails init for some reason. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | */ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 771 | static int elevator_switch(request_queue_t *q, struct elevator_type *new_e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | { |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 773 | elevator_t *old_elevator, *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 775 | /* |
| 776 | * Allocate new elevator |
| 777 | */ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 778 | e = elevator_alloc(new_e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (!e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 780 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | /* |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 783 | * Turn on BYPASS and drain all requests w/ elevator private data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | */ |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 785 | spin_lock_irq(q->queue_lock); |
| 786 | |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 787 | set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 788 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 789 | elv_drain_elevator(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 790 | |
| 791 | while (q->rq.elvpriv) { |
Tejun Heo | 407df2a | 2005-11-10 08:48:21 +0100 | [diff] [blame] | 792 | blk_remove_plug(q); |
| 793 | q->request_fn(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 794 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 795 | msleep(10); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 796 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 797 | elv_drain_elevator(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | spin_unlock_irq(q->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * unregister old elevator data |
| 804 | */ |
| 805 | elv_unregister_queue(q); |
| 806 | old_elevator = q->elevator; |
| 807 | |
| 808 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | * attach and start new elevator |
| 810 | */ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 811 | if (elevator_attach(q, e)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | goto fail; |
| 813 | |
| 814 | if (elv_register_queue(q)) |
| 815 | goto fail_register; |
| 816 | |
| 817 | /* |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 818 | * finally exit old elevator and turn off BYPASS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | */ |
| 820 | elevator_exit(old_elevator); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 821 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 822 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
| 824 | fail_register: |
| 825 | /* |
| 826 | * switch failed, exit the new io scheduler and reattach the old |
| 827 | * one again (along with re-adding the sysfs dir) |
| 828 | */ |
| 829 | elevator_exit(e); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 830 | e = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | fail: |
| 832 | q->elevator = old_elevator; |
| 833 | elv_register_queue(q); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 834 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 835 | if (e) |
| 836 | kobject_put(&e->kobj); |
| 837 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count) |
| 841 | { |
| 842 | char elevator_name[ELV_NAME_MAX]; |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 843 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | struct elevator_type *e; |
| 845 | |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 846 | elevator_name[sizeof(elevator_name) - 1] = '\0'; |
| 847 | strncpy(elevator_name, name, sizeof(elevator_name) - 1); |
| 848 | len = strlen(elevator_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 850 | if (len && elevator_name[len - 1] == '\n') |
| 851 | elevator_name[len - 1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
| 853 | e = elevator_get(elevator_name); |
| 854 | if (!e) { |
| 855 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 859 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) { |
| 860 | elevator_put(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return count; |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 864 | if (!elevator_switch(q, e)) |
| 865 | printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | return count; |
| 867 | } |
| 868 | |
| 869 | ssize_t elv_iosched_show(request_queue_t *q, char *name) |
| 870 | { |
| 871 | elevator_t *e = q->elevator; |
| 872 | struct elevator_type *elv = e->elevator_type; |
| 873 | struct list_head *entry; |
| 874 | int len = 0; |
| 875 | |
| 876 | spin_lock_irq(q->queue_lock); |
| 877 | list_for_each(entry, &elv_list) { |
| 878 | struct elevator_type *__e; |
| 879 | |
| 880 | __e = list_entry(entry, struct elevator_type, list); |
| 881 | if (!strcmp(elv->elevator_name, __e->elevator_name)) |
| 882 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
| 883 | else |
| 884 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 885 | } |
| 886 | spin_unlock_irq(q->queue_lock); |
| 887 | |
| 888 | len += sprintf(len+name, "\n"); |
| 889 | return len; |
| 890 | } |
| 891 | |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 892 | EXPORT_SYMBOL(elv_dispatch_sort); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | EXPORT_SYMBOL(elv_add_request); |
| 894 | EXPORT_SYMBOL(__elv_add_request); |
| 895 | EXPORT_SYMBOL(elv_requeue_request); |
| 896 | EXPORT_SYMBOL(elv_next_request); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 897 | EXPORT_SYMBOL(elv_dequeue_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | EXPORT_SYMBOL(elv_queue_empty); |
| 899 | EXPORT_SYMBOL(elv_completed_request); |
| 900 | EXPORT_SYMBOL(elevator_exit); |
| 901 | EXPORT_SYMBOL(elevator_init); |