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