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