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