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