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