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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/compiler.h> |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 34 | #include <linux/delay.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 35 | #include <linux/blktrace_api.h> |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 36 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/uaccess.h> |
| 39 | |
| 40 | static DEFINE_SPINLOCK(elv_list_lock); |
| 41 | static LIST_HEAD(elv_list); |
| 42 | |
| 43 | /* |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 44 | * Merge hash stuff. |
| 45 | */ |
| 46 | static const int elv_hash_shift = 6; |
| 47 | #define ELV_HASH_BLOCK(sec) ((sec) >> 3) |
| 48 | #define ELV_HASH_FN(sec) (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift)) |
| 49 | #define ELV_HASH_ENTRIES (1 << elv_hash_shift) |
| 50 | #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors) |
| 51 | #define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash)) |
| 52 | |
| 53 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * can we safely merge with this request? |
| 55 | */ |
| 56 | inline int elv_rq_merge_ok(struct request *rq, struct bio *bio) |
| 57 | { |
| 58 | if (!rq_mergeable(rq)) |
| 59 | return 0; |
| 60 | |
| 61 | /* |
| 62 | * different data direction or already started, don't merge |
| 63 | */ |
| 64 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 65 | return 0; |
| 66 | |
| 67 | /* |
| 68 | * same device and no special stuff set, merge is ok |
| 69 | */ |
Jens Axboe | c00895a | 2006-09-30 20:29:12 +0200 | [diff] [blame] | 70 | if (rq->rq_disk == bio->bi_bdev->bd_disk && !rq->special) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return 1; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL(elv_rq_merge_ok); |
| 76 | |
Coywolf Qi Hunt | 769db45 | 2005-12-28 10:55:49 +0100 | [diff] [blame] | 77 | static inline int elv_try_merge(struct request *__rq, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | int ret = ELEVATOR_NO_MERGE; |
| 80 | |
| 81 | /* |
| 82 | * we can merge and sequence is ok, check if it's possible |
| 83 | */ |
| 84 | if (elv_rq_merge_ok(__rq, bio)) { |
| 85 | if (__rq->sector + __rq->nr_sectors == bio->bi_sector) |
| 86 | ret = ELEVATOR_BACK_MERGE; |
| 87 | else if (__rq->sector - bio_sectors(bio) == bio->bi_sector) |
| 88 | ret = ELEVATOR_FRONT_MERGE; |
| 89 | } |
| 90 | |
| 91 | return ret; |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | static struct elevator_type *elevator_find(const char *name) |
| 95 | { |
| 96 | struct elevator_type *e = NULL; |
| 97 | struct list_head *entry; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | list_for_each(entry, &elv_list) { |
| 100 | struct elevator_type *__e; |
| 101 | |
| 102 | __e = list_entry(entry, struct elevator_type, list); |
| 103 | |
| 104 | if (!strcmp(__e->elevator_name, name)) { |
| 105 | e = __e; |
| 106 | break; |
| 107 | } |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | return e; |
| 111 | } |
| 112 | |
| 113 | static void elevator_put(struct elevator_type *e) |
| 114 | { |
| 115 | module_put(e->elevator_owner); |
| 116 | } |
| 117 | |
| 118 | static struct elevator_type *elevator_get(const char *name) |
| 119 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 120 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 122 | spin_lock_irq(&elv_list_lock); |
| 123 | |
| 124 | e = elevator_find(name); |
| 125 | if (e && !try_module_get(e->elevator_owner)) |
| 126 | e = NULL; |
| 127 | |
| 128 | spin_unlock_irq(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | return e; |
| 131 | } |
| 132 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 133 | static void *elevator_init_queue(request_queue_t *q, struct elevator_queue *eq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 135 | return eq->ops->elevator_init_fn(q, eq); |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 138 | static void elevator_attach(request_queue_t *q, struct elevator_queue *eq, |
| 139 | void *data) |
| 140 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | q->elevator = eq; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 142 | eq->elevator_data = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static char chosen_elevator[16]; |
| 146 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 147 | static int __init elevator_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Chuck Ebbert | 752a3b7 | 2006-01-16 09:47:37 +0100 | [diff] [blame] | 149 | /* |
| 150 | * Be backwards-compatible with previous kernels, so users |
| 151 | * won't get the wrong elevator. |
| 152 | */ |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 153 | if (!strcmp(str, "as")) |
Chuck Ebbert | 752a3b7 | 2006-01-16 09:47:37 +0100 | [diff] [blame] | 154 | strcpy(chosen_elevator, "anticipatory"); |
Zachary Amsden | cff3ba2 | 2005-11-09 13:24:20 +0100 | [diff] [blame] | 155 | else |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 156 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 157 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | __setup("elevator=", elevator_setup); |
| 161 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 162 | static struct kobj_type elv_ktype; |
| 163 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame^] | 164 | static elevator_t *elevator_alloc(request_queue_t *q, struct elevator_type *e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 165 | { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 166 | elevator_t *eq; |
| 167 | int i; |
| 168 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame^] | 169 | eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL, q->node); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 170 | if (unlikely(!eq)) |
| 171 | goto err; |
| 172 | |
| 173 | memset(eq, 0, sizeof(*eq)); |
| 174 | eq->ops = &e->ops; |
| 175 | eq->elevator_type = e; |
| 176 | kobject_init(&eq->kobj); |
| 177 | snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched"); |
| 178 | eq->kobj.ktype = &elv_ktype; |
| 179 | mutex_init(&eq->sysfs_lock); |
| 180 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame^] | 181 | eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES, |
| 182 | GFP_KERNEL, q->node); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 183 | if (!eq->hash) |
| 184 | goto err; |
| 185 | |
| 186 | for (i = 0; i < ELV_HASH_ENTRIES; i++) |
| 187 | INIT_HLIST_HEAD(&eq->hash[i]); |
| 188 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 189 | return eq; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 190 | err: |
| 191 | kfree(eq); |
| 192 | elevator_put(e); |
| 193 | return NULL; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void elevator_release(struct kobject *kobj) |
| 197 | { |
| 198 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 199 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 200 | elevator_put(e->elevator_type); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 201 | kfree(e->hash); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 202 | kfree(e); |
| 203 | } |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | int elevator_init(request_queue_t *q, char *name) |
| 206 | { |
| 207 | struct elevator_type *e = NULL; |
| 208 | struct elevator_queue *eq; |
| 209 | int ret = 0; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 210 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 212 | INIT_LIST_HEAD(&q->queue_head); |
| 213 | q->last_merge = NULL; |
| 214 | q->end_sector = 0; |
| 215 | q->boundary_rq = NULL; |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 216 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 217 | if (name && !(e = elevator_get(name))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | |
Nate Diller | 248d5ca | 2006-01-24 10:09:14 +0100 | [diff] [blame] | 220 | if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator))) |
| 221 | printk("I/O scheduler %s not found\n", chosen_elevator); |
| 222 | |
| 223 | if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) { |
| 224 | printk("Default I/O scheduler not found, using no-op\n"); |
| 225 | e = elevator_get("noop"); |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame^] | 228 | eq = elevator_alloc(q, e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 229 | if (!eq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 232 | data = elevator_init_queue(q, eq); |
| 233 | if (!data) { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 234 | kobject_put(&eq->kobj); |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 235 | return -ENOMEM; |
| 236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 238 | elevator_attach(q, eq, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return ret; |
| 240 | } |
| 241 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 242 | EXPORT_SYMBOL(elevator_init); |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | void elevator_exit(elevator_t *e) |
| 245 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 246 | mutex_lock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (e->ops->elevator_exit_fn) |
| 248 | e->ops->elevator_exit_fn(e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 249 | e->ops = NULL; |
| 250 | mutex_unlock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 252 | kobject_put(&e->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 255 | EXPORT_SYMBOL(elevator_exit); |
| 256 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 257 | static inline void __elv_rqhash_del(struct request *rq) |
| 258 | { |
| 259 | hlist_del_init(&rq->hash); |
| 260 | } |
| 261 | |
| 262 | static void elv_rqhash_del(request_queue_t *q, struct request *rq) |
| 263 | { |
| 264 | if (ELV_ON_HASH(rq)) |
| 265 | __elv_rqhash_del(rq); |
| 266 | } |
| 267 | |
| 268 | static void elv_rqhash_add(request_queue_t *q, struct request *rq) |
| 269 | { |
| 270 | elevator_t *e = q->elevator; |
| 271 | |
| 272 | BUG_ON(ELV_ON_HASH(rq)); |
| 273 | hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]); |
| 274 | } |
| 275 | |
| 276 | static void elv_rqhash_reposition(request_queue_t *q, struct request *rq) |
| 277 | { |
| 278 | __elv_rqhash_del(rq); |
| 279 | elv_rqhash_add(q, rq); |
| 280 | } |
| 281 | |
| 282 | static struct request *elv_rqhash_find(request_queue_t *q, sector_t offset) |
| 283 | { |
| 284 | elevator_t *e = q->elevator; |
| 285 | struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)]; |
| 286 | struct hlist_node *entry, *next; |
| 287 | struct request *rq; |
| 288 | |
| 289 | hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) { |
| 290 | BUG_ON(!ELV_ON_HASH(rq)); |
| 291 | |
| 292 | if (unlikely(!rq_mergeable(rq))) { |
| 293 | __elv_rqhash_del(rq); |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | if (rq_hash_key(rq) == offset) |
| 298 | return rq; |
| 299 | } |
| 300 | |
| 301 | return NULL; |
| 302 | } |
| 303 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 304 | /* |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 305 | * RB-tree support functions for inserting/lookup/removal of requests |
| 306 | * in a sorted RB tree. |
| 307 | */ |
| 308 | struct request *elv_rb_add(struct rb_root *root, struct request *rq) |
| 309 | { |
| 310 | struct rb_node **p = &root->rb_node; |
| 311 | struct rb_node *parent = NULL; |
| 312 | struct request *__rq; |
| 313 | |
| 314 | while (*p) { |
| 315 | parent = *p; |
| 316 | __rq = rb_entry(parent, struct request, rb_node); |
| 317 | |
| 318 | if (rq->sector < __rq->sector) |
| 319 | p = &(*p)->rb_left; |
| 320 | else if (rq->sector > __rq->sector) |
| 321 | p = &(*p)->rb_right; |
| 322 | else |
| 323 | return __rq; |
| 324 | } |
| 325 | |
| 326 | rb_link_node(&rq->rb_node, parent, p); |
| 327 | rb_insert_color(&rq->rb_node, root); |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | EXPORT_SYMBOL(elv_rb_add); |
| 332 | |
| 333 | void elv_rb_del(struct rb_root *root, struct request *rq) |
| 334 | { |
| 335 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); |
| 336 | rb_erase(&rq->rb_node, root); |
| 337 | RB_CLEAR_NODE(&rq->rb_node); |
| 338 | } |
| 339 | |
| 340 | EXPORT_SYMBOL(elv_rb_del); |
| 341 | |
| 342 | struct request *elv_rb_find(struct rb_root *root, sector_t sector) |
| 343 | { |
| 344 | struct rb_node *n = root->rb_node; |
| 345 | struct request *rq; |
| 346 | |
| 347 | while (n) { |
| 348 | rq = rb_entry(n, struct request, rb_node); |
| 349 | |
| 350 | if (sector < rq->sector) |
| 351 | n = n->rb_left; |
| 352 | else if (sector > rq->sector) |
| 353 | n = n->rb_right; |
| 354 | else |
| 355 | return rq; |
| 356 | } |
| 357 | |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | EXPORT_SYMBOL(elv_rb_find); |
| 362 | |
| 363 | /* |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 364 | * Insert rq into dispatch queue of q. Queue lock must be held on |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 365 | * entry. rq is sort insted into the dispatch queue. To be used by |
| 366 | * specific elevators. |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 367 | */ |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 368 | void elv_dispatch_sort(request_queue_t *q, struct request *rq) |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 369 | { |
| 370 | sector_t boundary; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 371 | struct list_head *entry; |
| 372 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 373 | if (q->last_merge == rq) |
| 374 | q->last_merge = NULL; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 375 | |
| 376 | elv_rqhash_del(q, rq); |
| 377 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 378 | q->nr_sorted--; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 379 | |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 380 | boundary = q->end_sector; |
Tejun Heo | cb19833 | 2005-10-24 08:35:58 +0200 | [diff] [blame] | 381 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 382 | list_for_each_prev(entry, &q->queue_head) { |
| 383 | struct request *pos = list_entry_rq(entry); |
| 384 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 385 | if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED)) |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 386 | break; |
| 387 | if (rq->sector >= boundary) { |
| 388 | if (pos->sector < boundary) |
| 389 | continue; |
| 390 | } else { |
| 391 | if (pos->sector >= boundary) |
| 392 | break; |
| 393 | } |
| 394 | if (rq->sector >= pos->sector) |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | list_add(&rq->queuelist, entry); |
| 399 | } |
| 400 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 401 | EXPORT_SYMBOL(elv_dispatch_sort); |
| 402 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 403 | /* |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 404 | * Insert rq into dispatch queue of q. Queue lock must be held on |
| 405 | * entry. rq is added to the back of the dispatch queue. To be used by |
| 406 | * specific elevators. |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 407 | */ |
| 408 | void elv_dispatch_add_tail(struct request_queue *q, struct request *rq) |
| 409 | { |
| 410 | if (q->last_merge == rq) |
| 411 | q->last_merge = NULL; |
| 412 | |
| 413 | elv_rqhash_del(q, rq); |
| 414 | |
| 415 | q->nr_sorted--; |
| 416 | |
| 417 | q->end_sector = rq_end_sector(rq); |
| 418 | q->boundary_rq = rq; |
| 419 | list_add_tail(&rq->queuelist, &q->queue_head); |
| 420 | } |
| 421 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 422 | EXPORT_SYMBOL(elv_dispatch_add_tail); |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | int elv_merge(request_queue_t *q, struct request **req, struct bio *bio) |
| 425 | { |
| 426 | elevator_t *e = q->elevator; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 427 | struct request *__rq; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 428 | int ret; |
| 429 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 430 | /* |
| 431 | * First try one-hit cache. |
| 432 | */ |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 433 | if (q->last_merge) { |
| 434 | ret = elv_try_merge(q->last_merge, bio); |
| 435 | if (ret != ELEVATOR_NO_MERGE) { |
| 436 | *req = q->last_merge; |
| 437 | return ret; |
| 438 | } |
| 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 441 | /* |
| 442 | * See if our hash lookup can find a potential backmerge. |
| 443 | */ |
| 444 | __rq = elv_rqhash_find(q, bio->bi_sector); |
| 445 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
| 446 | *req = __rq; |
| 447 | return ELEVATOR_BACK_MERGE; |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | if (e->ops->elevator_merge_fn) |
| 451 | return e->ops->elevator_merge_fn(q, req, bio); |
| 452 | |
| 453 | return ELEVATOR_NO_MERGE; |
| 454 | } |
| 455 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 456 | void elv_merged_request(request_queue_t *q, struct request *rq, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
| 458 | elevator_t *e = q->elevator; |
| 459 | |
| 460 | if (e->ops->elevator_merged_fn) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 461 | e->ops->elevator_merged_fn(q, rq, type); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 462 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 463 | if (type == ELEVATOR_BACK_MERGE) |
| 464 | elv_rqhash_reposition(q, rq); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 465 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 466 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void elv_merge_requests(request_queue_t *q, struct request *rq, |
| 470 | struct request *next) |
| 471 | { |
| 472 | elevator_t *e = q->elevator; |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (e->ops->elevator_merge_req_fn) |
| 475 | e->ops->elevator_merge_req_fn(q, rq, next); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 476 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 477 | elv_rqhash_reposition(q, rq); |
| 478 | elv_rqhash_del(q, next); |
| 479 | |
| 480 | q->nr_sorted--; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 481 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 484 | void elv_requeue_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | elevator_t *e = q->elevator; |
| 487 | |
| 488 | /* |
| 489 | * it already went through dequeue, we need to decrement the |
| 490 | * in_flight count again |
| 491 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 492 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | q->in_flight--; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 494 | if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn) |
| 495 | e->ops->elevator_deactivate_req_fn(q, rq); |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 498 | rq->cmd_flags &= ~REQ_STARTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 500 | elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 503 | static void elv_drain_elevator(request_queue_t *q) |
| 504 | { |
| 505 | static int printed; |
| 506 | while (q->elevator->ops->elevator_dispatch_fn(q, 1)) |
| 507 | ; |
| 508 | if (q->nr_sorted == 0) |
| 509 | return; |
| 510 | if (printed++ < 10) { |
| 511 | printk(KERN_ERR "%s: forced dispatching is broken " |
| 512 | "(nr_sorted=%u), please report this\n", |
| 513 | q->elevator->elevator_type->elevator_name, q->nr_sorted); |
| 514 | } |
| 515 | } |
| 516 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 517 | void elv_insert(request_queue_t *q, struct request *rq, int where) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 519 | struct list_head *pos; |
| 520 | unsigned ordseq; |
Jens Axboe | dac07ec | 2006-05-11 08:20:16 +0200 | [diff] [blame] | 521 | int unplug_it = 1; |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 522 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 523 | blk_add_trace_rq(q, rq, BLK_TA_INSERT); |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | rq->q = q; |
| 526 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 527 | switch (where) { |
| 528 | case ELEVATOR_INSERT_FRONT: |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 529 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 530 | |
| 531 | list_add(&rq->queuelist, &q->queue_head); |
| 532 | break; |
| 533 | |
| 534 | case ELEVATOR_INSERT_BACK: |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 535 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 536 | elv_drain_elevator(q); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 537 | list_add_tail(&rq->queuelist, &q->queue_head); |
| 538 | /* |
| 539 | * We kick the queue here for the following reasons. |
| 540 | * - The elevator might have returned NULL previously |
| 541 | * to delay requests and returned them now. As the |
| 542 | * queue wasn't empty before this request, ll_rw_blk |
| 543 | * won't run the queue on return, resulting in hang. |
| 544 | * - Usually, back inserted requests won't be merged |
| 545 | * with anything. There's no point in delaying queue |
| 546 | * processing. |
| 547 | */ |
| 548 | blk_remove_plug(q); |
| 549 | q->request_fn(q); |
| 550 | break; |
| 551 | |
| 552 | case ELEVATOR_INSERT_SORT: |
| 553 | BUG_ON(!blk_fs_request(rq)); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 554 | rq->cmd_flags |= REQ_SORTED; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 555 | q->nr_sorted++; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 556 | if (rq_mergeable(rq)) { |
| 557 | elv_rqhash_add(q, rq); |
| 558 | if (!q->last_merge) |
| 559 | q->last_merge = rq; |
| 560 | } |
| 561 | |
Tejun Heo | ca23509 | 2005-11-01 17:23:49 +0900 | [diff] [blame] | 562 | /* |
| 563 | * Some ioscheds (cfq) run q->request_fn directly, so |
| 564 | * rq cannot be accessed after calling |
| 565 | * elevator_add_req_fn. |
| 566 | */ |
| 567 | q->elevator->ops->elevator_add_req_fn(q, rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 568 | break; |
| 569 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 570 | case ELEVATOR_INSERT_REQUEUE: |
| 571 | /* |
| 572 | * If ordered flush isn't in progress, we do front |
| 573 | * insertion; otherwise, requests should be requeued |
| 574 | * in ordseq order. |
| 575 | */ |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 576 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 577 | |
| 578 | if (q->ordseq == 0) { |
| 579 | list_add(&rq->queuelist, &q->queue_head); |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | ordseq = blk_ordered_req_seq(rq); |
| 584 | |
| 585 | list_for_each(pos, &q->queue_head) { |
| 586 | struct request *pos_rq = list_entry_rq(pos); |
| 587 | if (ordseq <= blk_ordered_req_seq(pos_rq)) |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | list_add_tail(&rq->queuelist, pos); |
Jens Axboe | dac07ec | 2006-05-11 08:20:16 +0200 | [diff] [blame] | 592 | /* |
| 593 | * most requeues happen because of a busy condition, don't |
| 594 | * force unplug of the queue for that case. |
| 595 | */ |
| 596 | unplug_it = 0; |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 597 | break; |
| 598 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 599 | default: |
| 600 | printk(KERN_ERR "%s: bad insertion point %d\n", |
| 601 | __FUNCTION__, where); |
| 602 | BUG(); |
| 603 | } |
| 604 | |
Jens Axboe | dac07ec | 2006-05-11 08:20:16 +0200 | [diff] [blame] | 605 | if (unplug_it && blk_queue_plugged(q)) { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 606 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] |
| 607 | - q->in_flight; |
| 608 | |
| 609 | if (nrq >= q->unplug_thresh) |
| 610 | __generic_unplug_device(q); |
| 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 614 | void __elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 615 | int plug) |
| 616 | { |
| 617 | if (q->ordcolor) |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 618 | rq->cmd_flags |= REQ_ORDERED_COLOR; |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 619 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 620 | if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) { |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 621 | /* |
| 622 | * toggle ordered color |
| 623 | */ |
| 624 | if (blk_barrier_rq(rq)) |
| 625 | q->ordcolor ^= 1; |
| 626 | |
| 627 | /* |
| 628 | * barriers implicitly indicate back insertion |
| 629 | */ |
| 630 | if (where == ELEVATOR_INSERT_SORT) |
| 631 | where = ELEVATOR_INSERT_BACK; |
| 632 | |
| 633 | /* |
| 634 | * this request is scheduling boundary, update |
| 635 | * end_sector |
| 636 | */ |
| 637 | if (blk_fs_request(rq)) { |
| 638 | q->end_sector = rq_end_sector(rq); |
| 639 | q->boundary_rq = rq; |
| 640 | } |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 641 | } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT) |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 642 | where = ELEVATOR_INSERT_BACK; |
| 643 | |
| 644 | if (plug) |
| 645 | blk_plug_device(q); |
| 646 | |
| 647 | elv_insert(q, rq, where); |
| 648 | } |
| 649 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 650 | EXPORT_SYMBOL(__elv_add_request); |
| 651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | void elv_add_request(request_queue_t *q, struct request *rq, int where, |
| 653 | int plug) |
| 654 | { |
| 655 | unsigned long flags; |
| 656 | |
| 657 | spin_lock_irqsave(q->queue_lock, flags); |
| 658 | __elv_add_request(q, rq, where, plug); |
| 659 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 660 | } |
| 661 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 662 | EXPORT_SYMBOL(elv_add_request); |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | static inline struct request *__elv_next_request(request_queue_t *q) |
| 665 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 666 | struct request *rq; |
| 667 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 668 | while (1) { |
| 669 | while (!list_empty(&q->queue_head)) { |
| 670 | rq = list_entry_rq(q->queue_head.next); |
| 671 | if (blk_do_ordered(q, &rq)) |
| 672 | return rq; |
| 673 | } |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 674 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 675 | if (!q->elevator->ops->elevator_dispatch_fn(q, 0)) |
| 676 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | struct request *elv_next_request(request_queue_t *q) |
| 681 | { |
| 682 | struct request *rq; |
| 683 | int ret; |
| 684 | |
| 685 | while ((rq = __elv_next_request(q)) != NULL) { |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 686 | if (!(rq->cmd_flags & REQ_STARTED)) { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 687 | elevator_t *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 689 | /* |
| 690 | * This is the first time the device driver |
| 691 | * sees this request (possibly after |
| 692 | * requeueing). Notify IO scheduler. |
| 693 | */ |
| 694 | if (blk_sorted_rq(rq) && |
| 695 | e->ops->elevator_activate_req_fn) |
| 696 | e->ops->elevator_activate_req_fn(q, rq); |
| 697 | |
| 698 | /* |
| 699 | * just mark as started even if we don't start |
| 700 | * it, a request that has been delayed should |
| 701 | * not be passed by new incoming requests |
| 702 | */ |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 703 | rq->cmd_flags |= REQ_STARTED; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 704 | blk_add_trace_rq(q, rq, BLK_TA_ISSUE); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 707 | if (!q->boundary_rq || q->boundary_rq == rq) { |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 708 | q->end_sector = rq_end_sector(rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 709 | q->boundary_rq = NULL; |
| 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 712 | if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | break; |
| 714 | |
| 715 | ret = q->prep_rq_fn(q, rq); |
| 716 | if (ret == BLKPREP_OK) { |
| 717 | break; |
| 718 | } else if (ret == BLKPREP_DEFER) { |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 719 | /* |
| 720 | * the request may have been (partially) prepped. |
| 721 | * we need to keep this request in the front to |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 722 | * avoid resource deadlock. REQ_STARTED will |
| 723 | * prevent other fs requests from passing this one. |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 724 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | rq = NULL; |
| 726 | break; |
| 727 | } else if (ret == BLKPREP_KILL) { |
| 728 | int nr_bytes = rq->hard_nr_sectors << 9; |
| 729 | |
| 730 | if (!nr_bytes) |
| 731 | nr_bytes = rq->data_len; |
| 732 | |
| 733 | blkdev_dequeue_request(rq); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 734 | rq->cmd_flags |= REQ_QUIET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | end_that_request_chunk(rq, 0, nr_bytes); |
Tejun Heo | 8ffdc65 | 2006-01-06 09:49:03 +0100 | [diff] [blame] | 736 | end_that_request_last(rq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } else { |
| 738 | printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__, |
| 739 | ret); |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return rq; |
| 745 | } |
| 746 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 747 | EXPORT_SYMBOL(elv_next_request); |
| 748 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 749 | void elv_dequeue_request(request_queue_t *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 751 | BUG_ON(list_empty(&rq->queuelist)); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 752 | BUG_ON(ELV_ON_HASH(rq)); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 753 | |
| 754 | list_del_init(&rq->queuelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | /* |
| 757 | * the time frame between a request being removed from the lists |
| 758 | * 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] | 759 | * the driver side. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | */ |
| 761 | if (blk_account_rq(rq)) |
| 762 | q->in_flight++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 765 | EXPORT_SYMBOL(elv_dequeue_request); |
| 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | int elv_queue_empty(request_queue_t *q) |
| 768 | { |
| 769 | elevator_t *e = q->elevator; |
| 770 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 771 | if (!list_empty(&q->queue_head)) |
| 772 | return 0; |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | if (e->ops->elevator_queue_empty_fn) |
| 775 | return e->ops->elevator_queue_empty_fn(q); |
| 776 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 777 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 780 | EXPORT_SYMBOL(elv_queue_empty); |
| 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | struct request *elv_latter_request(request_queue_t *q, struct request *rq) |
| 783 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | elevator_t *e = q->elevator; |
| 785 | |
| 786 | if (e->ops->elevator_latter_req_fn) |
| 787 | return e->ops->elevator_latter_req_fn(q, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | struct request *elv_former_request(request_queue_t *q, struct request *rq) |
| 792 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | elevator_t *e = q->elevator; |
| 794 | |
| 795 | if (e->ops->elevator_former_req_fn) |
| 796 | return e->ops->elevator_former_req_fn(q, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return NULL; |
| 798 | } |
| 799 | |
Jens Axboe | cb78b28 | 2006-07-28 09:32:57 +0200 | [diff] [blame] | 800 | int elv_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
| 802 | elevator_t *e = q->elevator; |
| 803 | |
| 804 | if (e->ops->elevator_set_req_fn) |
Jens Axboe | cb78b28 | 2006-07-28 09:32:57 +0200 | [diff] [blame] | 805 | return e->ops->elevator_set_req_fn(q, rq, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | rq->elevator_private = NULL; |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | void elv_put_request(request_queue_t *q, struct request *rq) |
| 812 | { |
| 813 | elevator_t *e = q->elevator; |
| 814 | |
| 815 | if (e->ops->elevator_put_req_fn) |
| 816 | e->ops->elevator_put_req_fn(q, rq); |
| 817 | } |
| 818 | |
Jens Axboe | cb78b28 | 2006-07-28 09:32:57 +0200 | [diff] [blame] | 819 | int elv_may_queue(request_queue_t *q, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | elevator_t *e = q->elevator; |
| 822 | |
| 823 | if (e->ops->elevator_may_queue_fn) |
Jens Axboe | cb78b28 | 2006-07-28 09:32:57 +0200 | [diff] [blame] | 824 | return e->ops->elevator_may_queue_fn(q, rw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | return ELV_MQUEUE_MAY; |
| 827 | } |
| 828 | |
| 829 | void elv_completed_request(request_queue_t *q, struct request *rq) |
| 830 | { |
| 831 | elevator_t *e = q->elevator; |
| 832 | |
| 833 | /* |
| 834 | * request is released from the driver, io must be done |
| 835 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 836 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | q->in_flight--; |
Tejun Heo | 1bc691d | 2006-01-12 15:39:26 +0100 | [diff] [blame] | 838 | if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn) |
| 839 | e->ops->elevator_completed_req_fn(q, rq); |
| 840 | } |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 841 | |
Tejun Heo | 1bc691d | 2006-01-12 15:39:26 +0100 | [diff] [blame] | 842 | /* |
| 843 | * Check if the queue is waiting for fs requests to be |
| 844 | * drained for flush sequence. |
| 845 | */ |
| 846 | if (unlikely(q->ordseq)) { |
| 847 | struct request *first_rq = list_entry_rq(q->queue_head.next); |
| 848 | if (q->in_flight == 0 && |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 849 | blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN && |
| 850 | blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) { |
| 851 | blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0); |
| 852 | q->request_fn(q); |
| 853 | } |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 857 | #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) |
| 858 | |
| 859 | static ssize_t |
| 860 | elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 861 | { |
| 862 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
| 863 | struct elv_fs_entry *entry = to_elv(attr); |
| 864 | ssize_t error; |
| 865 | |
| 866 | if (!entry->show) |
| 867 | return -EIO; |
| 868 | |
| 869 | mutex_lock(&e->sysfs_lock); |
| 870 | error = e->ops ? entry->show(e, page) : -ENOENT; |
| 871 | mutex_unlock(&e->sysfs_lock); |
| 872 | return error; |
| 873 | } |
| 874 | |
| 875 | static ssize_t |
| 876 | elv_attr_store(struct kobject *kobj, struct attribute *attr, |
| 877 | const char *page, size_t length) |
| 878 | { |
| 879 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
| 880 | struct elv_fs_entry *entry = to_elv(attr); |
| 881 | ssize_t error; |
| 882 | |
| 883 | if (!entry->store) |
| 884 | return -EIO; |
| 885 | |
| 886 | mutex_lock(&e->sysfs_lock); |
| 887 | error = e->ops ? entry->store(e, page, length) : -ENOENT; |
| 888 | mutex_unlock(&e->sysfs_lock); |
| 889 | return error; |
| 890 | } |
| 891 | |
| 892 | static struct sysfs_ops elv_sysfs_ops = { |
| 893 | .show = elv_attr_show, |
| 894 | .store = elv_attr_store, |
| 895 | }; |
| 896 | |
| 897 | static struct kobj_type elv_ktype = { |
| 898 | .sysfs_ops = &elv_sysfs_ops, |
| 899 | .release = elevator_release, |
| 900 | }; |
| 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | int elv_register_queue(struct request_queue *q) |
| 903 | { |
| 904 | elevator_t *e = q->elevator; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 905 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 907 | e->kobj.parent = &q->kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 909 | error = kobject_add(&e->kobj); |
| 910 | if (!error) { |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 911 | struct elv_fs_entry *attr = e->elevator_type->elevator_attrs; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 912 | if (attr) { |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 913 | while (attr->attr.name) { |
| 914 | if (sysfs_create_file(&e->kobj, &attr->attr)) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 915 | break; |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 916 | attr++; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | kobject_uevent(&e->kobj, KOBJ_ADD); |
| 920 | } |
| 921 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 924 | static void __elv_unregister_queue(elevator_t *e) |
| 925 | { |
| 926 | kobject_uevent(&e->kobj, KOBJ_REMOVE); |
| 927 | kobject_del(&e->kobj); |
| 928 | } |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | void elv_unregister_queue(struct request_queue *q) |
| 931 | { |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 932 | if (q) |
| 933 | __elv_unregister_queue(q->elevator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | int elv_register(struct elevator_type *e) |
| 937 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 938 | spin_lock_irq(&elv_list_lock); |
Eric Sesterhenn | ce52449 | 2006-03-24 18:43:26 +0100 | [diff] [blame] | 939 | BUG_ON(elevator_find(e->elevator_name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | list_add_tail(&e->list, &elv_list); |
| 941 | spin_unlock_irq(&elv_list_lock); |
| 942 | |
| 943 | printk(KERN_INFO "io scheduler %s registered", e->elevator_name); |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 944 | if (!strcmp(e->elevator_name, chosen_elevator) || |
| 945 | (!*chosen_elevator && |
| 946 | !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED))) |
| 947 | printk(" (default)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | printk("\n"); |
| 949 | return 0; |
| 950 | } |
| 951 | EXPORT_SYMBOL_GPL(elv_register); |
| 952 | |
| 953 | void elv_unregister(struct elevator_type *e) |
| 954 | { |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 955 | struct task_struct *g, *p; |
| 956 | |
| 957 | /* |
| 958 | * Iterate every thread in the process to remove the io contexts. |
| 959 | */ |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 960 | if (e->ops.trim) { |
| 961 | read_lock(&tasklist_lock); |
| 962 | do_each_thread(g, p) { |
| 963 | task_lock(p); |
Oleg Nesterov | 2d8f613 | 2006-08-22 21:22:13 +0400 | [diff] [blame] | 964 | if (p->io_context) |
| 965 | e->ops.trim(p->io_context); |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 966 | task_unlock(p); |
| 967 | } while_each_thread(g, p); |
| 968 | read_unlock(&tasklist_lock); |
| 969 | } |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | spin_lock_irq(&elv_list_lock); |
| 972 | list_del_init(&e->list); |
| 973 | spin_unlock_irq(&elv_list_lock); |
| 974 | } |
| 975 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 976 | |
| 977 | /* |
| 978 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 979 | * we don't free the old io scheduler, before we have allocated what we |
| 980 | * 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] | 981 | * one, if the new one fails init for some reason. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | */ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 983 | 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] | 984 | { |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 985 | elevator_t *old_elevator, *e; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 986 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 988 | /* |
| 989 | * Allocate new elevator |
| 990 | */ |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame^] | 991 | e = elevator_alloc(q, new_e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | if (!e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 993 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 995 | data = elevator_init_queue(q, e); |
| 996 | if (!data) { |
| 997 | kobject_put(&e->kobj); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | /* |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1002 | * Turn on BYPASS and drain all requests w/ elevator private data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | */ |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1004 | spin_lock_irq(q->queue_lock); |
| 1005 | |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1006 | set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1007 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 1008 | elv_drain_elevator(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1009 | |
| 1010 | while (q->rq.elvpriv) { |
Tejun Heo | 407df2a | 2005-11-10 08:48:21 +0100 | [diff] [blame] | 1011 | blk_remove_plug(q); |
| 1012 | q->request_fn(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1013 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1014 | msleep(10); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1015 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 1016 | elv_drain_elevator(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | /* |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1020 | * Remember old elevator. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | old_elevator = q->elevator; |
| 1023 | |
| 1024 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | * attach and start new elevator |
| 1026 | */ |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1027 | elevator_attach(q, e, data); |
| 1028 | |
| 1029 | spin_unlock_irq(q->queue_lock); |
| 1030 | |
| 1031 | __elv_unregister_queue(old_elevator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
| 1033 | if (elv_register_queue(q)) |
| 1034 | goto fail_register; |
| 1035 | |
| 1036 | /* |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1037 | * finally exit old elevator and turn off BYPASS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | */ |
| 1039 | elevator_exit(old_elevator); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1040 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1041 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | fail_register: |
| 1044 | /* |
| 1045 | * switch failed, exit the new io scheduler and reattach the old |
| 1046 | * one again (along with re-adding the sysfs dir) |
| 1047 | */ |
| 1048 | elevator_exit(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | q->elevator = old_elevator; |
| 1050 | elv_register_queue(q); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1051 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1052 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count) |
| 1056 | { |
| 1057 | char elevator_name[ELV_NAME_MAX]; |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 1058 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | struct elevator_type *e; |
| 1060 | |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 1061 | elevator_name[sizeof(elevator_name) - 1] = '\0'; |
| 1062 | strncpy(elevator_name, name, sizeof(elevator_name) - 1); |
| 1063 | len = strlen(elevator_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 1065 | if (len && elevator_name[len - 1] == '\n') |
| 1066 | elevator_name[len - 1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | e = elevator_get(elevator_name); |
| 1069 | if (!e) { |
| 1070 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); |
| 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 1074 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) { |
| 1075 | elevator_put(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | return count; |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 1077 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1079 | if (!elevator_switch(q, e)) |
| 1080 | printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | return count; |
| 1082 | } |
| 1083 | |
| 1084 | ssize_t elv_iosched_show(request_queue_t *q, char *name) |
| 1085 | { |
| 1086 | elevator_t *e = q->elevator; |
| 1087 | struct elevator_type *elv = e->elevator_type; |
| 1088 | struct list_head *entry; |
| 1089 | int len = 0; |
| 1090 | |
| 1091 | spin_lock_irq(q->queue_lock); |
| 1092 | list_for_each(entry, &elv_list) { |
| 1093 | struct elevator_type *__e; |
| 1094 | |
| 1095 | __e = list_entry(entry, struct elevator_type, list); |
| 1096 | if (!strcmp(elv->elevator_name, __e->elevator_name)) |
| 1097 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
| 1098 | else |
| 1099 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 1100 | } |
| 1101 | spin_unlock_irq(q->queue_lock); |
| 1102 | |
| 1103 | len += sprintf(len+name, "\n"); |
| 1104 | return len; |
| 1105 | } |
| 1106 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 1107 | struct request *elv_rb_former_request(request_queue_t *q, struct request *rq) |
| 1108 | { |
| 1109 | struct rb_node *rbprev = rb_prev(&rq->rb_node); |
| 1110 | |
| 1111 | if (rbprev) |
| 1112 | return rb_entry_rq(rbprev); |
| 1113 | |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | |
| 1117 | EXPORT_SYMBOL(elv_rb_former_request); |
| 1118 | |
| 1119 | struct request *elv_rb_latter_request(request_queue_t *q, struct request *rq) |
| 1120 | { |
| 1121 | struct rb_node *rbnext = rb_next(&rq->rb_node); |
| 1122 | |
| 1123 | if (rbnext) |
| 1124 | return rb_entry_rq(rbnext); |
| 1125 | |
| 1126 | return NULL; |
| 1127 | } |
| 1128 | |
| 1129 | EXPORT_SYMBOL(elv_rb_latter_request); |