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