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