blob: f016855a46b094628190f68e698ae21a936912e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Block device elevator/IO-scheduler.
3 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 *
Jens Axboe0fe23472006-09-04 15:41:16 +02006 * 30042000 Jens Axboe <axboe@kernel.dk> :
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/init.h>
33#include <linux/compiler.h>
Jens Axboe2056a782006-03-23 20:00:26 +010034#include <linux/blktrace_api.h>
Jens Axboe98170642006-07-28 09:23:08 +020035#include <linux/hash.h>
Jens Axboe0835da62008-08-26 09:15:47 +020036#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Li Zefan55782132009-06-09 13:43:05 +080038#include <trace/events/block.h>
39
Jens Axboe242f9dc2008-09-14 05:55:09 -070040#include "blk.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_SPINLOCK(elv_list_lock);
43static LIST_HEAD(elv_list);
44
45/*
Jens Axboe98170642006-07-28 09:23:08 +020046 * Merge hash stuff.
47 */
48static const int elv_hash_shift = 6;
49#define ELV_HASH_BLOCK(sec) ((sec) >> 3)
Jens Axboe4eb166d2008-02-01 00:37:27 +010050#define ELV_HASH_FN(sec) \
51 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
Jens Axboe98170642006-07-28 09:23:08 +020052#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
Tejun Heo83096eb2009-05-07 22:24:39 +090053#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020054
55/*
Jens Axboeda775262006-12-20 11:04:12 +010056 * Query io scheduler to see if the current process issuing bio may be
57 * merged with rq.
58 */
59static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
60{
Jens Axboe165125e2007-07-24 09:28:11 +020061 struct request_queue *q = rq->q;
Jens Axboeb374d182008-10-31 10:05:07 +010062 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010063
Tejun Heo22f746e2011-12-14 00:33:41 +010064 if (e->type->ops.elevator_allow_merge_fn)
65 return e->type->ops.elevator_allow_merge_fn(q, rq, bio);
Jens Axboeda775262006-12-20 11:04:12 +010066
67 return 1;
68}
69
70/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * can we safely merge with this request?
72 */
Tejun Heo050c8ea2012-02-08 09:19:38 +010073bool elv_rq_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Tejun Heo050c8ea2012-02-08 09:19:38 +010075 if (!blk_rq_merge_ok(rq, bio))
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020076 return 0;
77
Jens Axboeda775262006-12-20 11:04:12 +010078 if (!elv_iosched_allow_merge(rq, bio))
79 return 0;
80
81 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83EXPORT_SYMBOL(elv_rq_merge_ok);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static struct elevator_type *elevator_find(const char *name)
86{
Vasily Tarasova22b1692006-10-11 09:24:27 +020087 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Matthias Kaehlcke70cee262007-07-10 12:26:24 +020089 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +020090 if (!strcmp(e->elevator_name, name))
91 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Vasily Tarasova22b1692006-10-11 09:24:27 +020094 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static void elevator_put(struct elevator_type *e)
98{
99 module_put(e->elevator_owner);
100}
101
102static struct elevator_type *elevator_get(const char *name)
103{
Tejun Heo2824bc932005-10-20 10:56:41 +0200104 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200106 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200107
108 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100109 if (!e) {
Jens Axboee1640942008-02-19 10:20:37 +0100110 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600111 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100112 spin_lock(&elv_list_lock);
113 e = elevator_find(name);
114 }
115
Tejun Heo2824bc932005-10-20 10:56:41 +0200116 if (e && !try_module_get(e->elevator_owner))
117 e = NULL;
118
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200119 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 return e;
122}
123
Tejun Heof8fc8772011-12-14 00:33:40 +0100124static int elevator_init_queue(struct request_queue *q,
125 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Tejun Heo22f746e2011-12-14 00:33:41 +0100127 eq->elevator_data = eq->type->ops.elevator_init_fn(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100128 if (eq->elevator_data)
129 return 0;
130 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Wang Sheng-Hui484fc252011-09-08 12:32:14 +0200133static char chosen_elevator[ELV_NAME_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Nate Diller5f003972006-01-24 10:07:58 +0100135static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100137 /*
138 * Be backwards-compatible with previous kernels, so users
139 * won't get the wrong elevator.
140 */
Jens Axboe492af632009-10-03 09:37:51 +0200141 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145__setup("elevator=", elevator_setup);
146
Al Viro3d1ab402006-03-18 18:35:43 -0500147static struct kobj_type elv_ktype;
148
Jens Axboeb374d182008-10-31 10:05:07 +0100149static struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200150 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500151{
Jens Axboeb374d182008-10-31 10:05:07 +0100152 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200153 int i;
154
Jens Axboeb374d182008-10-31 10:05:07 +0100155 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200156 if (unlikely(!eq))
157 goto err;
158
Tejun Heo22f746e2011-12-14 00:33:41 +0100159 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700160 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200161 mutex_init(&eq->sysfs_lock);
162
Jens Axboeb5deef92006-07-19 23:39:40 +0200163 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
164 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200165 if (!eq->hash)
166 goto err;
167
168 for (i = 0; i < ELV_HASH_ENTRIES; i++)
169 INIT_HLIST_HEAD(&eq->hash[i]);
170
Al Viro3d1ab402006-03-18 18:35:43 -0500171 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200172err:
173 kfree(eq);
174 elevator_put(e);
175 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500176}
177
178static void elevator_release(struct kobject *kobj)
179{
Jens Axboeb374d182008-10-31 10:05:07 +0100180 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200181
Jens Axboeb374d182008-10-31 10:05:07 +0100182 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100183 elevator_put(e->type);
Jens Axboe98170642006-07-28 09:23:08 +0200184 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500185 kfree(e);
186}
187
Jens Axboe165125e2007-07-24 09:28:11 +0200188int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct elevator_type *e = NULL;
191 struct elevator_queue *eq;
Tejun Heof8fc8772011-12-14 00:33:40 +0100192 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400194 if (unlikely(q->elevator))
195 return 0;
196
Tejun Heocb98fc82005-10-28 08:29:39 +0200197 INIT_LIST_HEAD(&q->queue_head);
198 q->last_merge = NULL;
199 q->end_sector = 0;
200 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200201
Jens Axboe4eb166d2008-02-01 00:37:27 +0100202 if (name) {
203 e = elevator_get(name);
204 if (!e)
205 return -EINVAL;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jens Axboe4eb166d2008-02-01 00:37:27 +0100208 if (!e && *chosen_elevator) {
209 e = elevator_get(chosen_elevator);
210 if (!e)
211 printk(KERN_ERR "I/O scheduler %s not found\n",
212 chosen_elevator);
213 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100214
Jens Axboe4eb166d2008-02-01 00:37:27 +0100215 if (!e) {
216 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
217 if (!e) {
218 printk(KERN_ERR
219 "Default I/O scheduler not found. " \
220 "Using noop.\n");
221 e = elevator_get("noop");
222 }
Nate Diller5f003972006-01-24 10:07:58 +0100223 }
224
Jens Axboeb5deef92006-07-19 23:39:40 +0200225 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500226 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Tejun Heof8fc8772011-12-14 00:33:40 +0100229 err = elevator_init_queue(q, eq);
230 if (err) {
Al Viro3d1ab402006-03-18 18:35:43 -0500231 kobject_put(&eq->kobj);
Tejun Heof8fc8772011-12-14 00:33:40 +0100232 return err;
Jens Axboebc1c1162006-06-08 08:49:06 +0200233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Tejun Heof8fc8772011-12-14 00:33:40 +0100235 q->elevator = eq;
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Jens Axboe2e662b62006-07-13 11:55:04 +0200238EXPORT_SYMBOL(elevator_init);
239
Jens Axboeb374d182008-10-31 10:05:07 +0100240void elevator_exit(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Al Viro3d1ab402006-03-18 18:35:43 -0500242 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100243 if (e->type->ops.elevator_exit_fn)
244 e->type->ops.elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500245 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Al Viro3d1ab402006-03-18 18:35:43 -0500247 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Jens Axboe2e662b62006-07-13 11:55:04 +0200249EXPORT_SYMBOL(elevator_exit);
250
Jens Axboe98170642006-07-28 09:23:08 +0200251static inline void __elv_rqhash_del(struct request *rq)
252{
253 hlist_del_init(&rq->hash);
254}
255
Jens Axboe165125e2007-07-24 09:28:11 +0200256static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200257{
258 if (ELV_ON_HASH(rq))
259 __elv_rqhash_del(rq);
260}
261
Jens Axboe165125e2007-07-24 09:28:11 +0200262static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200263{
Jens Axboeb374d182008-10-31 10:05:07 +0100264 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200265
266 BUG_ON(ELV_ON_HASH(rq));
267 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
268}
269
Jens Axboe165125e2007-07-24 09:28:11 +0200270static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200271{
272 __elv_rqhash_del(rq);
273 elv_rqhash_add(q, rq);
274}
275
Jens Axboe165125e2007-07-24 09:28:11 +0200276static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200277{
Jens Axboeb374d182008-10-31 10:05:07 +0100278 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200279 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
280 struct hlist_node *entry, *next;
281 struct request *rq;
282
283 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
284 BUG_ON(!ELV_ON_HASH(rq));
285
286 if (unlikely(!rq_mergeable(rq))) {
287 __elv_rqhash_del(rq);
288 continue;
289 }
290
291 if (rq_hash_key(rq) == offset)
292 return rq;
293 }
294
295 return NULL;
296}
297
Tejun Heo8922e162005-10-20 16:23:44 +0200298/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200299 * RB-tree support functions for inserting/lookup/removal of requests
300 * in a sorted RB tree.
301 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200302void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200303{
304 struct rb_node **p = &root->rb_node;
305 struct rb_node *parent = NULL;
306 struct request *__rq;
307
308 while (*p) {
309 parent = *p;
310 __rq = rb_entry(parent, struct request, rb_node);
311
Tejun Heo83096eb2009-05-07 22:24:39 +0900312 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200313 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200314 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200315 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200316 }
317
318 rb_link_node(&rq->rb_node, parent, p);
319 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200320}
Jens Axboe2e662b62006-07-13 11:55:04 +0200321EXPORT_SYMBOL(elv_rb_add);
322
323void elv_rb_del(struct rb_root *root, struct request *rq)
324{
325 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
326 rb_erase(&rq->rb_node, root);
327 RB_CLEAR_NODE(&rq->rb_node);
328}
Jens Axboe2e662b62006-07-13 11:55:04 +0200329EXPORT_SYMBOL(elv_rb_del);
330
331struct request *elv_rb_find(struct rb_root *root, sector_t sector)
332{
333 struct rb_node *n = root->rb_node;
334 struct request *rq;
335
336 while (n) {
337 rq = rb_entry(n, struct request, rb_node);
338
Tejun Heo83096eb2009-05-07 22:24:39 +0900339 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200340 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900341 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200342 n = n->rb_right;
343 else
344 return rq;
345 }
346
347 return NULL;
348}
Jens Axboe2e662b62006-07-13 11:55:04 +0200349EXPORT_SYMBOL(elv_rb_find);
350
351/*
Tejun Heo8922e162005-10-20 16:23:44 +0200352 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200353 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200354 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200355 */
Jens Axboe165125e2007-07-24 09:28:11 +0200356void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200357{
358 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200359 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100360 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200361
Tejun Heo06b86242005-10-20 16:46:23 +0200362 if (q->last_merge == rq)
363 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200364
365 elv_rqhash_del(q, rq);
366
Tejun Heo15853af2005-11-10 08:52:05 +0100367 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200368
Jens Axboe1b47f532005-10-20 16:37:00 +0200369 boundary = q->end_sector;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100370 stop_flags = REQ_SOFTBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200371 list_for_each_prev(entry, &q->queue_head) {
372 struct request *pos = list_entry_rq(entry);
373
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200374 if ((rq->cmd_flags & REQ_DISCARD) !=
375 (pos->cmd_flags & REQ_DISCARD))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100376 break;
Jens Axboe783660b2007-01-19 11:27:47 +1100377 if (rq_data_dir(rq) != rq_data_dir(pos))
378 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100379 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200380 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900381 if (blk_rq_pos(rq) >= boundary) {
382 if (blk_rq_pos(pos) < boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200383 continue;
384 } else {
Tejun Heo83096eb2009-05-07 22:24:39 +0900385 if (blk_rq_pos(pos) >= boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200386 break;
387 }
Tejun Heo83096eb2009-05-07 22:24:39 +0900388 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
Tejun Heo8922e162005-10-20 16:23:44 +0200389 break;
390 }
391
392 list_add(&rq->queuelist, entry);
393}
Jens Axboe2e662b62006-07-13 11:55:04 +0200394EXPORT_SYMBOL(elv_dispatch_sort);
395
Jens Axboe98170642006-07-28 09:23:08 +0200396/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200397 * Insert rq into dispatch queue of q. Queue lock must be held on
398 * entry. rq is added to the back of the dispatch queue. To be used by
399 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200400 */
401void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
402{
403 if (q->last_merge == rq)
404 q->last_merge = NULL;
405
406 elv_rqhash_del(q, rq);
407
408 q->nr_sorted--;
409
410 q->end_sector = rq_end_sector(rq);
411 q->boundary_rq = rq;
412 list_add_tail(&rq->queuelist, &q->queue_head);
413}
Jens Axboe2e662b62006-07-13 11:55:04 +0200414EXPORT_SYMBOL(elv_dispatch_add_tail);
415
Jens Axboe165125e2007-07-24 09:28:11 +0200416int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jens Axboeb374d182008-10-31 10:05:07 +0100418 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200419 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200420 int ret;
421
Jens Axboe98170642006-07-28 09:23:08 +0200422 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100423 * Levels of merges:
424 * nomerges: No merges at all attempted
425 * noxmerges: Only simple one-hit cache try
426 * merges: All merge tries attempted
427 */
428 if (blk_queue_nomerges(q))
429 return ELEVATOR_NO_MERGE;
430
431 /*
Jens Axboe98170642006-07-28 09:23:08 +0200432 * First try one-hit cache.
433 */
Tejun Heo050c8ea2012-02-08 09:19:38 +0100434 if (q->last_merge && elv_rq_merge_ok(q->last_merge, bio)) {
435 ret = blk_try_merge(q->last_merge, bio);
Tejun Heo06b86242005-10-20 16:46:23 +0200436 if (ret != ELEVATOR_NO_MERGE) {
437 *req = q->last_merge;
438 return ret;
439 }
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100442 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200443 return ELEVATOR_NO_MERGE;
444
Jens Axboe98170642006-07-28 09:23:08 +0200445 /*
446 * See if our hash lookup can find a potential backmerge.
447 */
448 __rq = elv_rqhash_find(q, bio->bi_sector);
449 if (__rq && elv_rq_merge_ok(__rq, bio)) {
450 *req = __rq;
451 return ELEVATOR_BACK_MERGE;
452 }
453
Tejun Heo22f746e2011-12-14 00:33:41 +0100454 if (e->type->ops.elevator_merge_fn)
455 return e->type->ops.elevator_merge_fn(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 return ELEVATOR_NO_MERGE;
458}
459
Jens Axboe5e84ea32011-03-21 10:14:27 +0100460/*
461 * Attempt to do an insertion back merge. Only check for the case where
462 * we can append 'rq' to an existing request, so we can throw 'rq' away
463 * afterwards.
464 *
465 * Returns true if we merged, false otherwise
466 */
467static bool elv_attempt_insert_merge(struct request_queue *q,
468 struct request *rq)
469{
470 struct request *__rq;
471
472 if (blk_queue_nomerges(q))
473 return false;
474
475 /*
476 * First try one-hit cache.
477 */
478 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
479 return true;
480
481 if (blk_queue_noxmerges(q))
482 return false;
483
484 /*
485 * See if our hash lookup can find a potential backmerge.
486 */
Jens Axboe5d381ef2012-01-15 10:29:48 +0100487 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
488 if (__rq && blk_attempt_req_merge(q, __rq, rq))
489 return true;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100490
Jens Axboe5d381ef2012-01-15 10:29:48 +0100491 return false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100492}
493
Jens Axboe165125e2007-07-24 09:28:11 +0200494void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Jens Axboeb374d182008-10-31 10:05:07 +0100496 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Tejun Heo22f746e2011-12-14 00:33:41 +0100498 if (e->type->ops.elevator_merged_fn)
499 e->type->ops.elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200500
Jens Axboe2e662b62006-07-13 11:55:04 +0200501 if (type == ELEVATOR_BACK_MERGE)
502 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200503
Tejun Heo06b86242005-10-20 16:46:23 +0200504 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Jens Axboe165125e2007-07-24 09:28:11 +0200507void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct request *next)
509{
Jens Axboeb374d182008-10-31 10:05:07 +0100510 struct elevator_queue *e = q->elevator;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100511 const int next_sorted = next->cmd_flags & REQ_SORTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Tejun Heo22f746e2011-12-14 00:33:41 +0100513 if (next_sorted && e->type->ops.elevator_merge_req_fn)
514 e->type->ops.elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200515
Jens Axboe98170642006-07-28 09:23:08 +0200516 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200517
Jens Axboe5e84ea32011-03-21 10:14:27 +0100518 if (next_sorted) {
519 elv_rqhash_del(q, next);
520 q->nr_sorted--;
521 }
522
Tejun Heo06b86242005-10-20 16:46:23 +0200523 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Divyesh Shah812d4022010-04-08 21:14:23 -0700526void elv_bio_merged(struct request_queue *q, struct request *rq,
527 struct bio *bio)
528{
529 struct elevator_queue *e = q->elevator;
530
Tejun Heo22f746e2011-12-14 00:33:41 +0100531 if (e->type->ops.elevator_bio_merged_fn)
532 e->type->ops.elevator_bio_merged_fn(q, rq, bio);
Divyesh Shah812d4022010-04-08 21:14:23 -0700533}
534
Jens Axboe165125e2007-07-24 09:28:11 +0200535void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * it already went through dequeue, we need to decrement the
539 * in_flight count again
540 */
Tejun Heo8922e162005-10-20 16:23:44 +0200541 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200542 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200543 if (rq->cmd_flags & REQ_SORTED)
Jens Axboecad97512007-01-14 22:26:09 +1100544 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jens Axboe4aff5e22006-08-10 08:44:47 +0200547 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jens Axboeb710a482011-03-30 09:52:30 +0200549 __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Jerome Marchand26308ea2009-03-27 10:31:51 +0100552void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100553{
554 static int printed;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200555
556 lockdep_assert_held(q->queue_lock);
557
Tejun Heo22f746e2011-12-14 00:33:41 +0100558 while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
Tejun Heo15853af2005-11-10 08:52:05 +0100559 ;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200560 if (q->nr_sorted && printed++ < 10) {
Tejun Heo15853af2005-11-10 08:52:05 +0100561 printk(KERN_ERR "%s: forced dispatching is broken "
562 "(nr_sorted=%u), please report this\n",
Tejun Heo22f746e2011-12-14 00:33:41 +0100563 q->elevator->type->elevator_name, q->nr_sorted);
Tejun Heo15853af2005-11-10 08:52:05 +0100564 }
565}
566
Jens Axboef600abe2009-04-08 14:22:01 +0200567void elv_quiesce_start(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100568{
Martin K. Petersencd43e262009-05-22 17:17:52 -0400569 if (!q->elevator)
570 return;
571
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200572 spin_lock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100573 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200574 spin_unlock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100575
Tejun Heoc9a929d2011-10-19 14:42:16 +0200576 blk_drain_queue(q, false);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100577}
578
Jens Axboef600abe2009-04-08 14:22:01 +0200579void elv_quiesce_end(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100580{
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200581 spin_lock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100582 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200583 spin_unlock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100584}
585
Jens Axboeb710a482011-03-30 09:52:30 +0200586void __elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100588 trace_block_rq_insert(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 rq->q = q;
591
Jens Axboeb710a482011-03-30 09:52:30 +0200592 if (rq->cmd_flags & REQ_SOFTBARRIER) {
593 /* barriers are scheduling boundary, update end_sector */
594 if (rq->cmd_type == REQ_TYPE_FS ||
595 (rq->cmd_flags & REQ_DISCARD)) {
596 q->end_sector = rq_end_sector(rq);
597 q->boundary_rq = rq;
598 }
599 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
Jens Axboe3aa72872011-04-21 19:28:35 +0200600 (where == ELEVATOR_INSERT_SORT ||
601 where == ELEVATOR_INSERT_SORT_MERGE))
Jens Axboeb710a482011-03-30 09:52:30 +0200602 where = ELEVATOR_INSERT_BACK;
603
Tejun Heo8922e162005-10-20 16:23:44 +0200604 switch (where) {
Tejun Heo28e7d182010-09-03 11:56:16 +0200605 case ELEVATOR_INSERT_REQUEUE:
Tejun Heo8922e162005-10-20 16:23:44 +0200606 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200607 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200608 list_add(&rq->queuelist, &q->queue_head);
609 break;
610
611 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200612 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100613 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200614 list_add_tail(&rq->queuelist, &q->queue_head);
615 /*
616 * We kick the queue here for the following reasons.
617 * - The elevator might have returned NULL previously
618 * to delay requests and returned them now. As the
619 * queue wasn't empty before this request, ll_rw_blk
620 * won't run the queue on return, resulting in hang.
621 * - Usually, back inserted requests won't be merged
622 * with anything. There's no point in delaying queue
623 * processing.
624 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200625 __blk_run_queue(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200626 break;
627
Jens Axboe5e84ea32011-03-21 10:14:27 +0100628 case ELEVATOR_INSERT_SORT_MERGE:
629 /*
630 * If we succeed in merging this request with one in the
631 * queue already, we are done - rq has now been freed,
632 * so no need to do anything further.
633 */
634 if (elv_attempt_insert_merge(q, rq))
635 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200636 case ELEVATOR_INSERT_SORT:
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200637 BUG_ON(rq->cmd_type != REQ_TYPE_FS &&
638 !(rq->cmd_flags & REQ_DISCARD));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200639 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100640 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200641 if (rq_mergeable(rq)) {
642 elv_rqhash_add(q, rq);
643 if (!q->last_merge)
644 q->last_merge = rq;
645 }
646
Tejun Heoca235092005-11-01 17:23:49 +0900647 /*
648 * Some ioscheds (cfq) run q->request_fn directly, so
649 * rq cannot be accessed after calling
650 * elevator_add_req_fn.
651 */
Tejun Heo22f746e2011-12-14 00:33:41 +0100652 q->elevator->type->ops.elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200653 break;
654
Tejun Heoae1b1532011-01-25 12:43:54 +0100655 case ELEVATOR_INSERT_FLUSH:
656 rq->cmd_flags |= REQ_SOFTBARRIER;
657 blk_insert_flush(rq);
658 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200659 default:
660 printk(KERN_ERR "%s: bad insertion point %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700661 __func__, where);
Tejun Heo8922e162005-10-20 16:23:44 +0200662 BUG();
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Jens Axboe2e662b62006-07-13 11:55:04 +0200665EXPORT_SYMBOL(__elv_add_request);
666
Jens Axboe7eaceac2011-03-10 08:52:07 +0100667void elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 unsigned long flags;
670
671 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100672 __elv_add_request(q, rq, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 spin_unlock_irqrestore(q->queue_lock, flags);
674}
Jens Axboe2e662b62006-07-13 11:55:04 +0200675EXPORT_SYMBOL(elv_add_request);
676
Jens Axboe165125e2007-07-24 09:28:11 +0200677struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Jens Axboeb374d182008-10-31 10:05:07 +0100679 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Tejun Heo22f746e2011-12-14 00:33:41 +0100681 if (e->type->ops.elevator_latter_req_fn)
682 return e->type->ops.elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return NULL;
684}
685
Jens Axboe165125e2007-07-24 09:28:11 +0200686struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jens Axboeb374d182008-10-31 10:05:07 +0100688 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Tejun Heo22f746e2011-12-14 00:33:41 +0100690 if (e->type->ops.elevator_former_req_fn)
691 return e->type->ops.elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return NULL;
693}
694
Jens Axboe165125e2007-07-24 09:28:11 +0200695int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Jens Axboeb374d182008-10-31 10:05:07 +0100697 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Tejun Heo22f746e2011-12-14 00:33:41 +0100699 if (e->type->ops.elevator_set_req_fn)
700 return e->type->ops.elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
Jens Axboe165125e2007-07-24 09:28:11 +0200704void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Jens Axboeb374d182008-10-31 10:05:07 +0100706 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Tejun Heo22f746e2011-12-14 00:33:41 +0100708 if (e->type->ops.elevator_put_req_fn)
709 e->type->ops.elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Jens Axboe165125e2007-07-24 09:28:11 +0200712int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Jens Axboeb374d182008-10-31 10:05:07 +0100714 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Tejun Heo22f746e2011-12-14 00:33:41 +0100716 if (e->type->ops.elevator_may_queue_fn)
717 return e->type->ops.elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return ELV_MQUEUE_MAY;
720}
721
Mike Anderson11914a52008-09-13 20:31:27 +0200722void elv_abort_queue(struct request_queue *q)
723{
724 struct request *rq;
725
Tejun Heoae1b1532011-01-25 12:43:54 +0100726 blk_abort_flushes(q);
727
Mike Anderson11914a52008-09-13 20:31:27 +0200728 while (!list_empty(&q->queue_head)) {
729 rq = list_entry_rq(q->queue_head.next);
730 rq->cmd_flags |= REQ_QUIET;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100731 trace_block_rq_abort(q, rq);
Kiyoshi Ueda53c663c2009-06-02 08:44:01 +0200732 /*
733 * Mark this request as started so we don't trigger
734 * any debug logic in the end I/O path.
735 */
736 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900737 __blk_end_request_all(rq, -EIO);
Mike Anderson11914a52008-09-13 20:31:27 +0200738 }
739}
740EXPORT_SYMBOL(elv_abort_queue);
741
Jens Axboe165125e2007-07-24 09:28:11 +0200742void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Jens Axboeb374d182008-10-31 10:05:07 +0100744 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /*
747 * request is released from the driver, io must be done
748 */
Tejun Heo8922e162005-10-20 16:23:44 +0200749 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200750 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200751 if ((rq->cmd_flags & REQ_SORTED) &&
Tejun Heo22f746e2011-12-14 00:33:41 +0100752 e->type->ops.elevator_completed_req_fn)
753 e->type->ops.elevator_completed_req_fn(q, rq);
Tejun Heo1bc691d2006-01-12 15:39:26 +0100754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Al Viro3d1ab402006-03-18 18:35:43 -0500757#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
758
759static ssize_t
760elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
761{
Al Viro3d1ab402006-03-18 18:35:43 -0500762 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100763 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500764 ssize_t error;
765
766 if (!entry->show)
767 return -EIO;
768
Jens Axboeb374d182008-10-31 10:05:07 +0100769 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500770 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100771 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500772 mutex_unlock(&e->sysfs_lock);
773 return error;
774}
775
776static ssize_t
777elv_attr_store(struct kobject *kobj, struct attribute *attr,
778 const char *page, size_t length)
779{
Al Viro3d1ab402006-03-18 18:35:43 -0500780 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100781 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500782 ssize_t error;
783
784 if (!entry->store)
785 return -EIO;
786
Jens Axboeb374d182008-10-31 10:05:07 +0100787 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500788 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100789 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500790 mutex_unlock(&e->sysfs_lock);
791 return error;
792}
793
Emese Revfy52cf25d2010-01-19 02:58:23 +0100794static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500795 .show = elv_attr_show,
796 .store = elv_attr_store,
797};
798
799static struct kobj_type elv_ktype = {
800 .sysfs_ops = &elv_sysfs_ops,
801 .release = elevator_release,
802};
803
Tejun Heof8fc8772011-12-14 00:33:40 +0100804int __elv_register_queue(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Al Viro3d1ab402006-03-18 18:35:43 -0500806 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700808 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500809 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100810 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500811 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500812 while (attr->attr.name) {
813 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500814 break;
Al Viroe572ec72006-03-18 22:27:18 -0500815 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500816 }
817 }
818 kobject_uevent(&e->kobj, KOBJ_ADD);
Jens Axboe430c62f2010-10-07 09:35:16 +0200819 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500820 }
821 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Tejun Heof8fc8772011-12-14 00:33:40 +0100824int elv_register_queue(struct request_queue *q)
Jens Axboebc1c1162006-06-08 08:49:06 +0200825{
Tejun Heof8fc8772011-12-14 00:33:40 +0100826 return __elv_register_queue(q, q->elevator);
Jens Axboebc1c1162006-06-08 08:49:06 +0200827}
Tejun Heof8fc8772011-12-14 00:33:40 +0100828EXPORT_SYMBOL(elv_register_queue);
Jens Axboebc1c1162006-06-08 08:49:06 +0200829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830void elv_unregister_queue(struct request_queue *q)
831{
Tejun Heof8fc8772011-12-14 00:33:40 +0100832 if (q) {
833 struct elevator_queue *e = q->elevator;
834
835 kobject_uevent(&e->kobj, KOBJ_REMOVE);
836 kobject_del(&e->kobj);
837 e->registered = 0;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
Mike Snitzer01effb02010-05-11 08:57:42 +0200840EXPORT_SYMBOL(elv_unregister_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Tejun Heo3d3c2372011-12-14 00:33:42 +0100842int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100844 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200845
Tejun Heo3d3c2372011-12-14 00:33:42 +0100846 /* create icq_cache if requested */
847 if (e->icq_size) {
848 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
849 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
850 return -EINVAL;
851
852 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
853 "%s_io_cq", e->elevator_name);
854 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
855 e->icq_align, 0, NULL);
856 if (!e->icq_cache)
857 return -ENOMEM;
858 }
859
860 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200861 spin_lock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100862 if (elevator_find(e->elevator_name)) {
863 spin_unlock(&elv_list_lock);
864 if (e->icq_cache)
865 kmem_cache_destroy(e->icq_cache);
866 return -EBUSY;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200869 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Tejun Heo3d3c2372011-12-14 00:33:42 +0100871 /* print pretty message */
Nate Diller5f003972006-01-24 10:07:58 +0100872 if (!strcmp(e->elevator_name, chosen_elevator) ||
873 (!*chosen_elevator &&
874 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100875 def = " (default)";
876
Jens Axboe4eb166d2008-02-01 00:37:27 +0100877 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
878 def);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881EXPORT_SYMBOL_GPL(elv_register);
882
883void elv_unregister(struct elevator_type *e)
884{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100885 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200886 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200888 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100889
890 /*
891 * Destroy icq_cache if it exists. icq's are RCU managed. Make
892 * sure all RCU operations are complete before proceeding.
893 */
894 if (e->icq_cache) {
895 rcu_barrier();
896 kmem_cache_destroy(e->icq_cache);
897 e->icq_cache = NULL;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900EXPORT_SYMBOL_GPL(elv_unregister);
901
902/*
903 * switch to new_e io scheduler. be careful not to introduce deadlocks -
904 * we don't free the old io scheduler, before we have allocated what we
905 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200906 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Jens Axboe165125e2007-07-24 09:28:11 +0200908static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Jens Axboeb374d182008-10-31 10:05:07 +0100910 struct elevator_queue *old_elevator, *e;
Jens Axboe5dd531a2010-08-23 13:52:19 +0200911 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Tejun Heof8fc8772011-12-14 00:33:40 +0100913 /* allocate new elevator */
Jens Axboeb5deef92006-07-19 23:39:40 +0200914 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!e)
Jens Axboe5dd531a2010-08-23 13:52:19 +0200916 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Tejun Heof8fc8772011-12-14 00:33:40 +0100918 err = elevator_init_queue(q, e);
919 if (err) {
Jens Axboebc1c1162006-06-08 08:49:06 +0200920 kobject_put(&e->kobj);
Tejun Heof8fc8772011-12-14 00:33:40 +0100921 return err;
Jens Axboebc1c1162006-06-08 08:49:06 +0200922 }
923
Tejun Heof8fc8772011-12-14 00:33:40 +0100924 /* turn on BYPASS and drain all requests w/ elevator private data */
Jens Axboef600abe2009-04-08 14:22:01 +0200925 elv_quiesce_start(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200926
Tejun Heof8fc8772011-12-14 00:33:40 +0100927 /* unregister old queue, register new one and kill old elevator */
928 if (q->elevator->registered) {
929 elv_unregister_queue(q);
930 err = __elv_register_queue(q, e);
Jens Axboe430c62f2010-10-07 09:35:16 +0200931 if (err)
932 goto fail_register;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Tejun Heo7e5a8792011-12-14 00:33:42 +0100935 /* done, clear io_cq's, switch elevators and turn off BYPASS */
Tejun Heof8fc8772011-12-14 00:33:40 +0100936 spin_lock_irq(q->queue_lock);
Tejun Heo7e5a8792011-12-14 00:33:42 +0100937 ioc_clear_queue(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100938 old_elevator = q->elevator;
939 q->elevator = e;
940 spin_unlock_irq(q->queue_lock);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 elevator_exit(old_elevator);
Jens Axboef600abe2009-04-08 14:22:01 +0200943 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200944
Tejun Heo22f746e2011-12-14 00:33:41 +0100945 blk_add_trace_msg(q, "elv switch: %s", e->type->elevator_name);
Alan D. Brunelle4722dc52008-05-27 14:55:00 +0200946
Jens Axboe5dd531a2010-08-23 13:52:19 +0200947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949fail_register:
950 /*
951 * switch failed, exit the new io scheduler and reattach the old
952 * one again (along with re-adding the sysfs dir)
953 */
954 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 elv_register_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200956 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200957
Jens Axboe5dd531a2010-08-23 13:52:19 +0200958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Jens Axboe5dd531a2010-08-23 13:52:19 +0200961/*
962 * Switch this queue to the given IO scheduler.
963 */
964int elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 char elevator_name[ELV_NAME_MAX];
967 struct elevator_type *e;
968
Martin K. Petersencd43e262009-05-22 17:17:52 -0400969 if (!q->elevator)
Jens Axboe5dd531a2010-08-23 13:52:19 +0200970 return -ENXIO;
Martin K. Petersencd43e262009-05-22 17:17:52 -0400971
Li Zefanee2e9922008-10-14 08:49:56 +0200972 strlcpy(elevator_name, name, sizeof(elevator_name));
KOSAKI Motohiro8c279592009-10-09 08:48:08 +0200973 e = elevator_get(strstrip(elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!e) {
975 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
976 return -EINVAL;
977 }
978
Tejun Heo22f746e2011-12-14 00:33:41 +0100979 if (!strcmp(elevator_name, q->elevator->type->elevator_name)) {
Nate Diller2ca7d932005-10-30 15:02:24 -0800980 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200981 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -0800982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Jens Axboe5dd531a2010-08-23 13:52:19 +0200984 return elevator_switch(q, e);
985}
986EXPORT_SYMBOL(elevator_change);
987
988ssize_t elv_iosched_store(struct request_queue *q, const char *name,
989 size_t count)
990{
991 int ret;
992
993 if (!q->elevator)
994 return count;
995
996 ret = elevator_change(q, name);
997 if (!ret)
998 return count;
999
1000 printk(KERN_ERR "elevator: switch to %s failed\n", name);
1001 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Jens Axboe165125e2007-07-24 09:28:11 +02001004ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Jens Axboeb374d182008-10-31 10:05:07 +01001006 struct elevator_queue *e = q->elevator;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001007 struct elevator_type *elv;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001008 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int len = 0;
1010
Mike Snitzere36f7242010-05-24 09:07:32 +02001011 if (!q->elevator || !blk_queue_stackable(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -04001012 return sprintf(name, "none\n");
1013
Tejun Heo22f746e2011-12-14 00:33:41 +01001014 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001015
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001016 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001017 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!strcmp(elv->elevator_name, __e->elevator_name))
1019 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1020 else
1021 len += sprintf(name+len, "%s ", __e->elevator_name);
1022 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001023 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 len += sprintf(len+name, "\n");
1026 return len;
1027}
1028
Jens Axboe165125e2007-07-24 09:28:11 +02001029struct request *elv_rb_former_request(struct request_queue *q,
1030 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001031{
1032 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1033
1034 if (rbprev)
1035 return rb_entry_rq(rbprev);
1036
1037 return NULL;
1038}
Jens Axboe2e662b62006-07-13 11:55:04 +02001039EXPORT_SYMBOL(elv_rb_former_request);
1040
Jens Axboe165125e2007-07-24 09:28:11 +02001041struct request *elv_rb_latter_request(struct request_queue *q,
1042 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001043{
1044 struct rb_node *rbnext = rb_next(&rq->rb_node);
1045
1046 if (rbnext)
1047 return rb_entry_rq(rbnext);
1048
1049 return NULL;
1050}
Jens Axboe2e662b62006-07-13 11:55:04 +02001051EXPORT_SYMBOL(elv_rb_latter_request);