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