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