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 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 59 | struct request_queue *q = rq->q; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 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 | |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 116 | list_for_each_entry(e, &elv_list, list) { |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 117 | if (!strcmp(e->elevator_name, name)) |
| 118 | return e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
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 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void elevator_put(struct elevator_type *e) |
| 125 | { |
| 126 | module_put(e->elevator_owner); |
| 127 | } |
| 128 | |
| 129 | static struct elevator_type *elevator_get(const char *name) |
| 130 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 131 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 133 | spin_lock(&elv_list_lock); |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 134 | |
| 135 | e = elevator_find(name); |
| 136 | if (e && !try_module_get(e->elevator_owner)) |
| 137 | e = NULL; |
| 138 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 139 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | return e; |
| 142 | } |
| 143 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 144 | static void *elevator_init_queue(struct request_queue *q, |
| 145 | struct elevator_queue *eq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Jens Axboe | bb37b94 | 2006-12-01 10:42:33 +0100 | [diff] [blame] | 147 | return eq->ops->elevator_init_fn(q); |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 150 | static void elevator_attach(struct request_queue *q, struct elevator_queue *eq, |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 151 | void *data) |
| 152 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | q->elevator = eq; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 154 | eq->elevator_data = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static char chosen_elevator[16]; |
| 158 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 159 | static int __init elevator_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Chuck Ebbert | 752a3b7 | 2006-01-16 09:47:37 +0100 | [diff] [blame] | 161 | /* |
| 162 | * Be backwards-compatible with previous kernels, so users |
| 163 | * won't get the wrong elevator. |
| 164 | */ |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 165 | if (!strcmp(str, "as")) |
Chuck Ebbert | 752a3b7 | 2006-01-16 09:47:37 +0100 | [diff] [blame] | 166 | strcpy(chosen_elevator, "anticipatory"); |
Zachary Amsden | cff3ba2 | 2005-11-09 13:24:20 +0100 | [diff] [blame] | 167 | else |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 168 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 169 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | __setup("elevator=", elevator_setup); |
| 173 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 174 | static struct kobj_type elv_ktype; |
| 175 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 176 | static elevator_t *elevator_alloc(struct request_queue *q, |
| 177 | struct elevator_type *e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 178 | { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 179 | elevator_t *eq; |
| 180 | int i; |
| 181 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 182 | eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 183 | if (unlikely(!eq)) |
| 184 | goto err; |
| 185 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 186 | eq->ops = &e->ops; |
| 187 | eq->elevator_type = e; |
| 188 | kobject_init(&eq->kobj); |
Greg Kroah-Hartman | 19c38de | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 189 | kobject_set_name(&eq->kobj, "%s", "iosched"); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 190 | eq->kobj.ktype = &elv_ktype; |
| 191 | mutex_init(&eq->sysfs_lock); |
| 192 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 193 | eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES, |
| 194 | GFP_KERNEL, q->node); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 195 | if (!eq->hash) |
| 196 | goto err; |
| 197 | |
| 198 | for (i = 0; i < ELV_HASH_ENTRIES; i++) |
| 199 | INIT_HLIST_HEAD(&eq->hash[i]); |
| 200 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 201 | return eq; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 202 | err: |
| 203 | kfree(eq); |
| 204 | elevator_put(e); |
| 205 | return NULL; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void elevator_release(struct kobject *kobj) |
| 209 | { |
| 210 | elevator_t *e = container_of(kobj, elevator_t, kobj); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 211 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 212 | elevator_put(e->elevator_type); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 213 | kfree(e->hash); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 214 | kfree(e); |
| 215 | } |
| 216 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 217 | int elevator_init(struct request_queue *q, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | struct elevator_type *e = NULL; |
| 220 | struct elevator_queue *eq; |
| 221 | int ret = 0; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 222 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 224 | INIT_LIST_HEAD(&q->queue_head); |
| 225 | q->last_merge = NULL; |
| 226 | q->end_sector = 0; |
| 227 | q->boundary_rq = NULL; |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 228 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 229 | if (name && !(e = elevator_get(name))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return -EINVAL; |
| 231 | |
Nate Diller | 248d5ca | 2006-01-24 10:09:14 +0100 | [diff] [blame] | 232 | if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator))) |
| 233 | printk("I/O scheduler %s not found\n", chosen_elevator); |
| 234 | |
| 235 | if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) { |
| 236 | printk("Default I/O scheduler not found, using no-op\n"); |
| 237 | e = elevator_get("noop"); |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 240 | eq = elevator_alloc(q, e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 241 | if (!eq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 244 | data = elevator_init_queue(q, eq); |
| 245 | if (!data) { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 246 | kobject_put(&eq->kobj); |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 247 | return -ENOMEM; |
| 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 250 | elevator_attach(q, eq, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return ret; |
| 252 | } |
| 253 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 254 | EXPORT_SYMBOL(elevator_init); |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | void elevator_exit(elevator_t *e) |
| 257 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 258 | mutex_lock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (e->ops->elevator_exit_fn) |
| 260 | e->ops->elevator_exit_fn(e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 261 | e->ops = NULL; |
| 262 | mutex_unlock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 264 | kobject_put(&e->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 267 | EXPORT_SYMBOL(elevator_exit); |
| 268 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 269 | static void elv_activate_rq(struct request_queue *q, struct request *rq) |
Jens Axboe | cad9751 | 2007-01-14 22:26:09 +1100 | [diff] [blame] | 270 | { |
| 271 | elevator_t *e = q->elevator; |
| 272 | |
| 273 | if (e->ops->elevator_activate_req_fn) |
| 274 | e->ops->elevator_activate_req_fn(q, rq); |
| 275 | } |
| 276 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 277 | static void elv_deactivate_rq(struct request_queue *q, struct request *rq) |
Jens Axboe | cad9751 | 2007-01-14 22:26:09 +1100 | [diff] [blame] | 278 | { |
| 279 | elevator_t *e = q->elevator; |
| 280 | |
| 281 | if (e->ops->elevator_deactivate_req_fn) |
| 282 | e->ops->elevator_deactivate_req_fn(q, rq); |
| 283 | } |
| 284 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 285 | static inline void __elv_rqhash_del(struct request *rq) |
| 286 | { |
| 287 | hlist_del_init(&rq->hash); |
| 288 | } |
| 289 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 290 | static void elv_rqhash_del(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 291 | { |
| 292 | if (ELV_ON_HASH(rq)) |
| 293 | __elv_rqhash_del(rq); |
| 294 | } |
| 295 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 296 | static void elv_rqhash_add(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 297 | { |
| 298 | elevator_t *e = q->elevator; |
| 299 | |
| 300 | BUG_ON(ELV_ON_HASH(rq)); |
| 301 | hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]); |
| 302 | } |
| 303 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 304 | static void elv_rqhash_reposition(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 305 | { |
| 306 | __elv_rqhash_del(rq); |
| 307 | elv_rqhash_add(q, rq); |
| 308 | } |
| 309 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 310 | static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 311 | { |
| 312 | elevator_t *e = q->elevator; |
| 313 | struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)]; |
| 314 | struct hlist_node *entry, *next; |
| 315 | struct request *rq; |
| 316 | |
| 317 | hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) { |
| 318 | BUG_ON(!ELV_ON_HASH(rq)); |
| 319 | |
| 320 | if (unlikely(!rq_mergeable(rq))) { |
| 321 | __elv_rqhash_del(rq); |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | if (rq_hash_key(rq) == offset) |
| 326 | return rq; |
| 327 | } |
| 328 | |
| 329 | return NULL; |
| 330 | } |
| 331 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 332 | /* |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 333 | * RB-tree support functions for inserting/lookup/removal of requests |
| 334 | * in a sorted RB tree. |
| 335 | */ |
| 336 | struct request *elv_rb_add(struct rb_root *root, struct request *rq) |
| 337 | { |
| 338 | struct rb_node **p = &root->rb_node; |
| 339 | struct rb_node *parent = NULL; |
| 340 | struct request *__rq; |
| 341 | |
| 342 | while (*p) { |
| 343 | parent = *p; |
| 344 | __rq = rb_entry(parent, struct request, rb_node); |
| 345 | |
| 346 | if (rq->sector < __rq->sector) |
| 347 | p = &(*p)->rb_left; |
| 348 | else if (rq->sector > __rq->sector) |
| 349 | p = &(*p)->rb_right; |
| 350 | else |
| 351 | return __rq; |
| 352 | } |
| 353 | |
| 354 | rb_link_node(&rq->rb_node, parent, p); |
| 355 | rb_insert_color(&rq->rb_node, root); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | EXPORT_SYMBOL(elv_rb_add); |
| 360 | |
| 361 | void elv_rb_del(struct rb_root *root, struct request *rq) |
| 362 | { |
| 363 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); |
| 364 | rb_erase(&rq->rb_node, root); |
| 365 | RB_CLEAR_NODE(&rq->rb_node); |
| 366 | } |
| 367 | |
| 368 | EXPORT_SYMBOL(elv_rb_del); |
| 369 | |
| 370 | struct request *elv_rb_find(struct rb_root *root, sector_t sector) |
| 371 | { |
| 372 | struct rb_node *n = root->rb_node; |
| 373 | struct request *rq; |
| 374 | |
| 375 | while (n) { |
| 376 | rq = rb_entry(n, struct request, rb_node); |
| 377 | |
| 378 | if (sector < rq->sector) |
| 379 | n = n->rb_left; |
| 380 | else if (sector > rq->sector) |
| 381 | n = n->rb_right; |
| 382 | else |
| 383 | return rq; |
| 384 | } |
| 385 | |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | EXPORT_SYMBOL(elv_rb_find); |
| 390 | |
| 391 | /* |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 392 | * Insert rq into dispatch queue of q. Queue lock must be held on |
Uwe Kleine-König | dbe7f76 | 2007-10-20 01:55:04 +0200 | [diff] [blame] | 393 | * entry. rq is sort instead into the dispatch queue. To be used by |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 394 | * specific elevators. |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 395 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 396 | void elv_dispatch_sort(struct request_queue *q, struct request *rq) |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 397 | { |
| 398 | sector_t boundary; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 399 | struct list_head *entry; |
| 400 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 401 | if (q->last_merge == rq) |
| 402 | q->last_merge = NULL; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 403 | |
| 404 | elv_rqhash_del(q, rq); |
| 405 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 406 | q->nr_sorted--; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 407 | |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 408 | boundary = q->end_sector; |
Tejun Heo | cb19833 | 2005-10-24 08:35:58 +0200 | [diff] [blame] | 409 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 410 | list_for_each_prev(entry, &q->queue_head) { |
| 411 | struct request *pos = list_entry_rq(entry); |
| 412 | |
Jens Axboe | 783660b | 2007-01-19 11:27:47 +1100 | [diff] [blame] | 413 | if (rq_data_dir(rq) != rq_data_dir(pos)) |
| 414 | break; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 415 | if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED)) |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 416 | break; |
| 417 | if (rq->sector >= boundary) { |
| 418 | if (pos->sector < boundary) |
| 419 | continue; |
| 420 | } else { |
| 421 | if (pos->sector >= boundary) |
| 422 | break; |
| 423 | } |
| 424 | if (rq->sector >= pos->sector) |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | list_add(&rq->queuelist, entry); |
| 429 | } |
| 430 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 431 | EXPORT_SYMBOL(elv_dispatch_sort); |
| 432 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 433 | /* |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 434 | * Insert rq into dispatch queue of q. Queue lock must be held on |
| 435 | * entry. rq is added to the back of the dispatch queue. To be used by |
| 436 | * specific elevators. |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 437 | */ |
| 438 | void elv_dispatch_add_tail(struct request_queue *q, struct request *rq) |
| 439 | { |
| 440 | if (q->last_merge == rq) |
| 441 | q->last_merge = NULL; |
| 442 | |
| 443 | elv_rqhash_del(q, rq); |
| 444 | |
| 445 | q->nr_sorted--; |
| 446 | |
| 447 | q->end_sector = rq_end_sector(rq); |
| 448 | q->boundary_rq = rq; |
| 449 | list_add_tail(&rq->queuelist, &q->queue_head); |
| 450 | } |
| 451 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 452 | EXPORT_SYMBOL(elv_dispatch_add_tail); |
| 453 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 454 | int elv_merge(struct request_queue *q, struct request **req, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | elevator_t *e = q->elevator; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 457 | struct request *__rq; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 458 | int ret; |
| 459 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 460 | /* |
| 461 | * First try one-hit cache. |
| 462 | */ |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 463 | if (q->last_merge) { |
| 464 | ret = elv_try_merge(q->last_merge, bio); |
| 465 | if (ret != ELEVATOR_NO_MERGE) { |
| 466 | *req = q->last_merge; |
| 467 | return ret; |
| 468 | } |
| 469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 471 | /* |
| 472 | * See if our hash lookup can find a potential backmerge. |
| 473 | */ |
| 474 | __rq = elv_rqhash_find(q, bio->bi_sector); |
| 475 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
| 476 | *req = __rq; |
| 477 | return ELEVATOR_BACK_MERGE; |
| 478 | } |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (e->ops->elevator_merge_fn) |
| 481 | return e->ops->elevator_merge_fn(q, req, bio); |
| 482 | |
| 483 | return ELEVATOR_NO_MERGE; |
| 484 | } |
| 485 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 486 | void elv_merged_request(struct request_queue *q, struct request *rq, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | elevator_t *e = q->elevator; |
| 489 | |
| 490 | if (e->ops->elevator_merged_fn) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 491 | e->ops->elevator_merged_fn(q, rq, type); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 492 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 493 | if (type == ELEVATOR_BACK_MERGE) |
| 494 | elv_rqhash_reposition(q, rq); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 495 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 496 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 499 | void elv_merge_requests(struct request_queue *q, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | struct request *next) |
| 501 | { |
| 502 | elevator_t *e = q->elevator; |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (e->ops->elevator_merge_req_fn) |
| 505 | e->ops->elevator_merge_req_fn(q, rq, next); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 506 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 507 | elv_rqhash_reposition(q, rq); |
| 508 | elv_rqhash_del(q, next); |
| 509 | |
| 510 | q->nr_sorted--; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 511 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 514 | void elv_requeue_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* |
| 517 | * it already went through dequeue, we need to decrement the |
| 518 | * in_flight count again |
| 519 | */ |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 520 | if (blk_account_rq(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | q->in_flight--; |
Jens Axboe | cad9751 | 2007-01-14 22:26:09 +1100 | [diff] [blame] | 522 | if (blk_sorted_rq(rq)) |
| 523 | elv_deactivate_rq(q, rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 526 | rq->cmd_flags &= ~REQ_STARTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 528 | elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 531 | static void elv_drain_elevator(struct request_queue *q) |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 532 | { |
| 533 | static int printed; |
| 534 | while (q->elevator->ops->elevator_dispatch_fn(q, 1)) |
| 535 | ; |
| 536 | if (q->nr_sorted == 0) |
| 537 | return; |
| 538 | if (printed++ < 10) { |
| 539 | printk(KERN_ERR "%s: forced dispatching is broken " |
| 540 | "(nr_sorted=%u), please report this\n", |
| 541 | q->elevator->elevator_type->elevator_name, q->nr_sorted); |
| 542 | } |
| 543 | } |
| 544 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 545 | void elv_insert(struct request_queue *q, struct request *rq, int where) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 547 | struct list_head *pos; |
| 548 | unsigned ordseq; |
Jens Axboe | dac07ec | 2006-05-11 08:20:16 +0200 | [diff] [blame] | 549 | int unplug_it = 1; |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 550 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 551 | blk_add_trace_rq(q, rq, BLK_TA_INSERT); |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | rq->q = q; |
| 554 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 555 | switch (where) { |
| 556 | case ELEVATOR_INSERT_FRONT: |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 557 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 558 | |
| 559 | list_add(&rq->queuelist, &q->queue_head); |
| 560 | break; |
| 561 | |
| 562 | case ELEVATOR_INSERT_BACK: |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 563 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 564 | elv_drain_elevator(q); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 565 | list_add_tail(&rq->queuelist, &q->queue_head); |
| 566 | /* |
| 567 | * We kick the queue here for the following reasons. |
| 568 | * - The elevator might have returned NULL previously |
| 569 | * to delay requests and returned them now. As the |
| 570 | * queue wasn't empty before this request, ll_rw_blk |
| 571 | * won't run the queue on return, resulting in hang. |
| 572 | * - Usually, back inserted requests won't be merged |
| 573 | * with anything. There's no point in delaying queue |
| 574 | * processing. |
| 575 | */ |
| 576 | blk_remove_plug(q); |
| 577 | q->request_fn(q); |
| 578 | break; |
| 579 | |
| 580 | case ELEVATOR_INSERT_SORT: |
| 581 | BUG_ON(!blk_fs_request(rq)); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 582 | rq->cmd_flags |= REQ_SORTED; |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 583 | q->nr_sorted++; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 584 | if (rq_mergeable(rq)) { |
| 585 | elv_rqhash_add(q, rq); |
| 586 | if (!q->last_merge) |
| 587 | q->last_merge = rq; |
| 588 | } |
| 589 | |
Tejun Heo | ca23509 | 2005-11-01 17:23:49 +0900 | [diff] [blame] | 590 | /* |
| 591 | * Some ioscheds (cfq) run q->request_fn directly, so |
| 592 | * rq cannot be accessed after calling |
| 593 | * elevator_add_req_fn. |
| 594 | */ |
| 595 | q->elevator->ops->elevator_add_req_fn(q, rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 596 | break; |
| 597 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 598 | case ELEVATOR_INSERT_REQUEUE: |
| 599 | /* |
| 600 | * If ordered flush isn't in progress, we do front |
| 601 | * insertion; otherwise, requests should be requeued |
| 602 | * in ordseq order. |
| 603 | */ |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 604 | rq->cmd_flags |= REQ_SOFTBARRIER; |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 605 | |
Linas Vepstas | 9554317 | 2007-01-23 19:40:54 +0100 | [diff] [blame] | 606 | /* |
| 607 | * Most requeues happen because of a busy condition, |
| 608 | * don't force unplug of the queue for that case. |
| 609 | */ |
| 610 | unplug_it = 0; |
| 611 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 612 | if (q->ordseq == 0) { |
| 613 | list_add(&rq->queuelist, &q->queue_head); |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | ordseq = blk_ordered_req_seq(rq); |
| 618 | |
| 619 | list_for_each(pos, &q->queue_head) { |
| 620 | struct request *pos_rq = list_entry_rq(pos); |
| 621 | if (ordseq <= blk_ordered_req_seq(pos_rq)) |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | list_add_tail(&rq->queuelist, pos); |
| 626 | break; |
| 627 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 628 | default: |
| 629 | printk(KERN_ERR "%s: bad insertion point %d\n", |
| 630 | __FUNCTION__, where); |
| 631 | BUG(); |
| 632 | } |
| 633 | |
Jens Axboe | dac07ec | 2006-05-11 08:20:16 +0200 | [diff] [blame] | 634 | if (unplug_it && blk_queue_plugged(q)) { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 635 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] |
| 636 | - q->in_flight; |
| 637 | |
| 638 | if (nrq >= q->unplug_thresh) |
| 639 | __generic_unplug_device(q); |
| 640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 643 | void __elv_add_request(struct request_queue *q, struct request *rq, int where, |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 644 | int plug) |
| 645 | { |
| 646 | if (q->ordcolor) |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 647 | rq->cmd_flags |= REQ_ORDERED_COLOR; |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 648 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 649 | if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) { |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 650 | /* |
| 651 | * toggle ordered color |
| 652 | */ |
| 653 | if (blk_barrier_rq(rq)) |
| 654 | q->ordcolor ^= 1; |
| 655 | |
| 656 | /* |
| 657 | * barriers implicitly indicate back insertion |
| 658 | */ |
| 659 | if (where == ELEVATOR_INSERT_SORT) |
| 660 | where = ELEVATOR_INSERT_BACK; |
| 661 | |
| 662 | /* |
| 663 | * this request is scheduling boundary, update |
| 664 | * end_sector |
| 665 | */ |
| 666 | if (blk_fs_request(rq)) { |
| 667 | q->end_sector = rq_end_sector(rq); |
| 668 | q->boundary_rq = rq; |
| 669 | } |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 670 | } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT) |
Tejun Heo | 30e9656 | 2006-02-08 01:01:31 -0800 | [diff] [blame] | 671 | where = ELEVATOR_INSERT_BACK; |
| 672 | |
| 673 | if (plug) |
| 674 | blk_plug_device(q); |
| 675 | |
| 676 | elv_insert(q, rq, where); |
| 677 | } |
| 678 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 679 | EXPORT_SYMBOL(__elv_add_request); |
| 680 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 681 | void elv_add_request(struct request_queue *q, struct request *rq, int where, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | int plug) |
| 683 | { |
| 684 | unsigned long flags; |
| 685 | |
| 686 | spin_lock_irqsave(q->queue_lock, flags); |
| 687 | __elv_add_request(q, rq, where, plug); |
| 688 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 689 | } |
| 690 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 691 | EXPORT_SYMBOL(elv_add_request); |
| 692 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 693 | static inline struct request *__elv_next_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 695 | struct request *rq; |
| 696 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 697 | while (1) { |
| 698 | while (!list_empty(&q->queue_head)) { |
| 699 | rq = list_entry_rq(q->queue_head.next); |
| 700 | if (blk_do_ordered(q, &rq)) |
| 701 | return rq; |
| 702 | } |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 703 | |
Tejun Heo | 797e7db | 2006-01-06 09:51:03 +0100 | [diff] [blame] | 704 | if (!q->elevator->ops->elevator_dispatch_fn(q, 0)) |
| 705 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 709 | struct request *elv_next_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
| 711 | struct request *rq; |
| 712 | int ret; |
| 713 | |
| 714 | while ((rq = __elv_next_request(q)) != NULL) { |
Jens Axboe | bf2de6f | 2007-09-27 13:01:25 +0200 | [diff] [blame] | 715 | /* |
| 716 | * Kill the empty barrier place holder, the driver must |
| 717 | * not ever see it. |
| 718 | */ |
| 719 | if (blk_empty_barrier(rq)) { |
| 720 | end_queued_request(rq, 1); |
| 721 | continue; |
| 722 | } |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 723 | if (!(rq->cmd_flags & REQ_STARTED)) { |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 724 | /* |
| 725 | * This is the first time the device driver |
| 726 | * sees this request (possibly after |
| 727 | * requeueing). Notify IO scheduler. |
| 728 | */ |
Jens Axboe | cad9751 | 2007-01-14 22:26:09 +1100 | [diff] [blame] | 729 | if (blk_sorted_rq(rq)) |
| 730 | elv_activate_rq(q, rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * just mark as started even if we don't start |
| 734 | * it, a request that has been delayed should |
| 735 | * not be passed by new incoming requests |
| 736 | */ |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 737 | rq->cmd_flags |= REQ_STARTED; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 738 | blk_add_trace_rq(q, rq, BLK_TA_ISSUE); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 741 | if (!q->boundary_rq || q->boundary_rq == rq) { |
Jens Axboe | 1b47f53 | 2005-10-20 16:37:00 +0200 | [diff] [blame] | 742 | q->end_sector = rq_end_sector(rq); |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 743 | q->boundary_rq = NULL; |
| 744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 746 | if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | break; |
| 748 | |
| 749 | ret = q->prep_rq_fn(q, rq); |
| 750 | if (ret == BLKPREP_OK) { |
| 751 | break; |
| 752 | } else if (ret == BLKPREP_DEFER) { |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 753 | /* |
| 754 | * the request may have been (partially) prepped. |
| 755 | * we need to keep this request in the front to |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 756 | * avoid resource deadlock. REQ_STARTED will |
| 757 | * prevent other fs requests from passing this one. |
Tejun Heo | 2e759cd | 2005-04-24 02:04:21 -0500 | [diff] [blame] | 758 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | rq = NULL; |
| 760 | break; |
| 761 | } else if (ret == BLKPREP_KILL) { |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 762 | rq->cmd_flags |= REQ_QUIET; |
Jens Axboe | a0cd128 | 2007-09-21 10:41:07 +0200 | [diff] [blame] | 763 | end_queued_request(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 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 776 | void elv_dequeue_request(struct request_queue *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 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 794 | int elv_queue_empty(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 809 | struct request *elv_latter_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 818 | struct request *elv_former_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 827 | int elv_set_request(struct request_queue *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 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 838 | void elv_put_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 846 | int elv_may_queue(struct request_queue *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 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 856 | void elv_completed_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Thibaut VARENE | 1ffb96c | 2007-03-15 12:59:19 +0100 | [diff] [blame] | 965 | char *def = ""; |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 966 | |
| 967 | spin_lock(&elv_list_lock); |
Eric Sesterhenn | ce52449 | 2006-03-24 18:43:26 +0100 | [diff] [blame] | 968 | BUG_ON(elevator_find(e->elevator_name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | list_add_tail(&e->list, &elv_list); |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 970 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Nate Diller | 5f00397 | 2006-01-24 10:07:58 +0100 | [diff] [blame] | 972 | if (!strcmp(e->elevator_name, chosen_elevator) || |
| 973 | (!*chosen_elevator && |
| 974 | !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED))) |
Thibaut VARENE | 1ffb96c | 2007-03-15 12:59:19 +0100 | [diff] [blame] | 975 | def = " (default)"; |
| 976 | |
| 977 | printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, def); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(elv_register); |
| 981 | |
| 982 | void elv_unregister(struct elevator_type *e) |
| 983 | { |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 984 | struct task_struct *g, *p; |
| 985 | |
| 986 | /* |
| 987 | * Iterate every thread in the process to remove the io contexts. |
| 988 | */ |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 989 | if (e->ops.trim) { |
| 990 | read_lock(&tasklist_lock); |
| 991 | do_each_thread(g, p) { |
| 992 | task_lock(p); |
Oleg Nesterov | 2d8f613 | 2006-08-22 21:22:13 +0400 | [diff] [blame] | 993 | if (p->io_context) |
| 994 | e->ops.trim(p->io_context); |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 995 | task_unlock(p); |
| 996 | } while_each_thread(g, p); |
| 997 | read_unlock(&tasklist_lock); |
| 998 | } |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 999 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 1000 | spin_lock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | list_del_init(&e->list); |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 1002 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | } |
| 1004 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 1005 | |
| 1006 | /* |
| 1007 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 1008 | * we don't free the old io scheduler, before we have allocated what we |
| 1009 | * 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] | 1010 | * one, if the new one fails init for some reason. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1012 | static int elevator_switch(struct request_queue *q, struct elevator_type *new_e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1014 | elevator_t *old_elevator, *e; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1015 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1017 | /* |
| 1018 | * Allocate new elevator |
| 1019 | */ |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 1020 | e = elevator_alloc(q, new_e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if (!e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1022 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1024 | data = elevator_init_queue(q, e); |
| 1025 | if (!data) { |
| 1026 | kobject_put(&e->kobj); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
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 | * Turn on BYPASS and drain all requests w/ elevator private data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | */ |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1033 | spin_lock_irq(q->queue_lock); |
| 1034 | |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1035 | set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1036 | |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 1037 | elv_drain_elevator(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1038 | |
| 1039 | while (q->rq.elvpriv) { |
Tejun Heo | 407df2a | 2005-11-10 08:48:21 +0100 | [diff] [blame] | 1040 | blk_remove_plug(q); |
| 1041 | q->request_fn(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1042 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1043 | msleep(10); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1044 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 15853af | 2005-11-10 08:52:05 +0100 | [diff] [blame] | 1045 | elv_drain_elevator(q); |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | /* |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1049 | * Remember old elevator. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | old_elevator = q->elevator; |
| 1052 | |
| 1053 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | * attach and start new elevator |
| 1055 | */ |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 1056 | elevator_attach(q, e, data); |
| 1057 | |
| 1058 | spin_unlock_irq(q->queue_lock); |
| 1059 | |
| 1060 | __elv_unregister_queue(old_elevator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | if (elv_register_queue(q)) |
| 1063 | goto fail_register; |
| 1064 | |
| 1065 | /* |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 1066 | * finally exit old elevator and turn off BYPASS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | */ |
| 1068 | elevator_exit(old_elevator); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1069 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1070 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
| 1072 | fail_register: |
| 1073 | /* |
| 1074 | * switch failed, exit the new io scheduler and reattach the old |
| 1075 | * one again (along with re-adding the sysfs dir) |
| 1076 | */ |
| 1077 | elevator_exit(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | q->elevator = old_elevator; |
| 1079 | elv_register_queue(q); |
Jens Axboe | 64521d1 | 2005-10-28 08:30:39 +0200 | [diff] [blame] | 1080 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1081 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1084 | ssize_t elv_iosched_store(struct request_queue *q, const char *name, |
| 1085 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { |
| 1087 | char elevator_name[ELV_NAME_MAX]; |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 1088 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | struct elevator_type *e; |
| 1090 | |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 1091 | elevator_name[sizeof(elevator_name) - 1] = '\0'; |
| 1092 | strncpy(elevator_name, name, sizeof(elevator_name) - 1); |
| 1093 | len = strlen(elevator_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
Tejun Heo | be56123 | 2005-11-10 08:55:01 +0100 | [diff] [blame] | 1095 | if (len && elevator_name[len - 1] == '\n') |
| 1096 | elevator_name[len - 1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
| 1098 | e = elevator_get(elevator_name); |
| 1099 | if (!e) { |
| 1100 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 1104 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) { |
| 1105 | elevator_put(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return count; |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 1107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 1109 | if (!elevator_switch(q, e)) |
| 1110 | printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return count; |
| 1112 | } |
| 1113 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1114 | ssize_t elv_iosched_show(struct request_queue *q, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | elevator_t *e = q->elevator; |
| 1117 | struct elevator_type *elv = e->elevator_type; |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 1118 | struct elevator_type *__e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | int len = 0; |
| 1120 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 1121 | spin_lock(&elv_list_lock); |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 1122 | list_for_each_entry(__e, &elv_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 1128 | spin_unlock(&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 | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1134 | struct request *elv_rb_former_request(struct request_queue *q, |
| 1135 | struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 1136 | { |
| 1137 | struct rb_node *rbprev = rb_prev(&rq->rb_node); |
| 1138 | |
| 1139 | if (rbprev) |
| 1140 | return rb_entry_rq(rbprev); |
| 1141 | |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | |
| 1145 | EXPORT_SYMBOL(elv_rb_former_request); |
| 1146 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1147 | struct request *elv_rb_latter_request(struct request_queue *q, |
| 1148 | struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 1149 | { |
| 1150 | struct rb_node *rbnext = rb_next(&rq->rb_node); |
| 1151 | |
| 1152 | if (rbnext) |
| 1153 | return rb_entry_rq(rbnext); |
| 1154 | |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | |
| 1158 | EXPORT_SYMBOL(elv_rb_latter_request); |