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