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