blob: 8ccd163254b8acf1469628e432308ab73cedd97b [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>
Tejun Heocb98fc82005-10-28 08:29:39 +020034#include <linux/delay.h>
Jens Axboe2056a782006-03-23 20:00:26 +010035#include <linux/blktrace_api.h>
Jens Axboe98170642006-07-28 09:23:08 +020036#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
39
40static DEFINE_SPINLOCK(elv_list_lock);
41static LIST_HEAD(elv_list);
42
43/*
Jens Axboe98170642006-07-28 09:23:08 +020044 * Merge hash stuff.
45 */
46static const int elv_hash_shift = 6;
47#define ELV_HASH_BLOCK(sec) ((sec) >> 3)
48#define ELV_HASH_FN(sec) (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
49#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
50#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
51#define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
52
53/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * can we safely merge with this request?
55 */
56inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
57{
58 if (!rq_mergeable(rq))
59 return 0;
60
61 /*
62 * different data direction or already started, don't merge
63 */
64 if (bio_data_dir(bio) != rq_data_dir(rq))
65 return 0;
66
67 /*
68 * same device and no special stuff set, merge is ok
69 */
Jens Axboec00895a2006-09-30 20:29:12 +020070 if (rq->rq_disk == bio->bi_bdev->bd_disk && !rq->special)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return 1;
72
73 return 0;
74}
75EXPORT_SYMBOL(elv_rq_merge_ok);
76
Coywolf Qi Hunt769db452005-12-28 10:55:49 +010077static inline int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int ret = ELEVATOR_NO_MERGE;
80
81 /*
82 * we can merge and sequence is ok, check if it's possible
83 */
84 if (elv_rq_merge_ok(__rq, bio)) {
85 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
86 ret = ELEVATOR_BACK_MERGE;
87 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
88 ret = ELEVATOR_FRONT_MERGE;
89 }
90
91 return ret;
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct elevator_type *elevator_find(const char *name)
95{
Vasily Tarasova22b1692006-10-11 09:24:27 +020096 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct list_head *entry;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 list_for_each(entry, &elv_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Vasily Tarasova22b1692006-10-11 09:24:27 +0200101 e = list_entry(entry, struct elevator_type, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Vasily Tarasova22b1692006-10-11 09:24:27 +0200103 if (!strcmp(e->elevator_name, name))
104 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Vasily Tarasova22b1692006-10-11 09:24:27 +0200107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static void elevator_put(struct elevator_type *e)
111{
112 module_put(e->elevator_owner);
113}
114
115static struct elevator_type *elevator_get(const char *name)
116{
Tejun Heo2824bc932005-10-20 10:56:41 +0200117 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Tejun Heo2824bc932005-10-20 10:56:41 +0200119 spin_lock_irq(&elv_list_lock);
120
121 e = elevator_find(name);
122 if (e && !try_module_get(e->elevator_owner))
123 e = NULL;
124
125 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return e;
128}
129
Jens Axboebc1c1162006-06-08 08:49:06 +0200130static void *elevator_init_queue(request_queue_t *q, struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Jens Axboebc1c1162006-06-08 08:49:06 +0200132 return eq->ops->elevator_init_fn(q, eq);
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jens Axboebc1c1162006-06-08 08:49:06 +0200135static void elevator_attach(request_queue_t *q, struct elevator_queue *eq,
136 void *data)
137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200139 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142static char chosen_elevator[16];
143
Nate Diller5f003972006-01-24 10:07:58 +0100144static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100146 /*
147 * Be backwards-compatible with previous kernels, so users
148 * won't get the wrong elevator.
149 */
Nate Diller5f003972006-01-24 10:07:58 +0100150 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100151 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100152 else
Nate Diller5f003972006-01-24 10:07:58 +0100153 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157__setup("elevator=", elevator_setup);
158
Al Viro3d1ab402006-03-18 18:35:43 -0500159static struct kobj_type elv_ktype;
160
Jens Axboeb5deef92006-07-19 23:39:40 +0200161static elevator_t *elevator_alloc(request_queue_t *q, struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500162{
Jens Axboe98170642006-07-28 09:23:08 +0200163 elevator_t *eq;
164 int i;
165
Jens Axboeb5deef92006-07-19 23:39:40 +0200166 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200167 if (unlikely(!eq))
168 goto err;
169
170 memset(eq, 0, sizeof(*eq));
171 eq->ops = &e->ops;
172 eq->elevator_type = e;
173 kobject_init(&eq->kobj);
174 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
175 eq->kobj.ktype = &elv_ktype;
176 mutex_init(&eq->sysfs_lock);
177
Jens Axboeb5deef92006-07-19 23:39:40 +0200178 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
179 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200180 if (!eq->hash)
181 goto err;
182
183 for (i = 0; i < ELV_HASH_ENTRIES; i++)
184 INIT_HLIST_HEAD(&eq->hash[i]);
185
Al Viro3d1ab402006-03-18 18:35:43 -0500186 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200187err:
188 kfree(eq);
189 elevator_put(e);
190 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500191}
192
193static void elevator_release(struct kobject *kobj)
194{
195 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200196
Al Viro3d1ab402006-03-18 18:35:43 -0500197 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200198 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500199 kfree(e);
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202int elevator_init(request_queue_t *q, char *name)
203{
204 struct elevator_type *e = NULL;
205 struct elevator_queue *eq;
206 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200207 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Tejun Heocb98fc82005-10-28 08:29:39 +0200209 INIT_LIST_HEAD(&q->queue_head);
210 q->last_merge = NULL;
211 q->end_sector = 0;
212 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200213
Nate Diller5f003972006-01-24 10:07:58 +0100214 if (name && !(e = elevator_get(name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -EINVAL;
216
Nate Diller248d5ca2006-01-24 10:09:14 +0100217 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
218 printk("I/O scheduler %s not found\n", chosen_elevator);
219
220 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
221 printk("Default I/O scheduler not found, using no-op\n");
222 e = elevator_get("noop");
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
Jens Axboebc1c1162006-06-08 08:49:06 +0200229 data = elevator_init_queue(q, eq);
230 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500231 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200232 return -ENOMEM;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jens Axboebc1c1162006-06-08 08:49:06 +0200235 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ret;
237}
238
Jens Axboe2e662b62006-07-13 11:55:04 +0200239EXPORT_SYMBOL(elevator_init);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241void elevator_exit(elevator_t *e)
242{
Al Viro3d1ab402006-03-18 18:35:43 -0500243 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (e->ops->elevator_exit_fn)
245 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500246 e->ops = NULL;
247 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Al Viro3d1ab402006-03-18 18:35:43 -0500249 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Jens Axboe2e662b62006-07-13 11:55:04 +0200252EXPORT_SYMBOL(elevator_exit);
253
Jens Axboe98170642006-07-28 09:23:08 +0200254static inline void __elv_rqhash_del(struct request *rq)
255{
256 hlist_del_init(&rq->hash);
257}
258
259static void elv_rqhash_del(request_queue_t *q, struct request *rq)
260{
261 if (ELV_ON_HASH(rq))
262 __elv_rqhash_del(rq);
263}
264
265static void elv_rqhash_add(request_queue_t *q, struct request *rq)
266{
267 elevator_t *e = q->elevator;
268
269 BUG_ON(ELV_ON_HASH(rq));
270 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
271}
272
273static void elv_rqhash_reposition(request_queue_t *q, struct request *rq)
274{
275 __elv_rqhash_del(rq);
276 elv_rqhash_add(q, rq);
277}
278
279static struct request *elv_rqhash_find(request_queue_t *q, sector_t offset)
280{
281 elevator_t *e = q->elevator;
282 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
283 struct hlist_node *entry, *next;
284 struct request *rq;
285
286 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
287 BUG_ON(!ELV_ON_HASH(rq));
288
289 if (unlikely(!rq_mergeable(rq))) {
290 __elv_rqhash_del(rq);
291 continue;
292 }
293
294 if (rq_hash_key(rq) == offset)
295 return rq;
296 }
297
298 return NULL;
299}
300
Tejun Heo8922e162005-10-20 16:23:44 +0200301/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200302 * RB-tree support functions for inserting/lookup/removal of requests
303 * in a sorted RB tree.
304 */
305struct request *elv_rb_add(struct rb_root *root, struct request *rq)
306{
307 struct rb_node **p = &root->rb_node;
308 struct rb_node *parent = NULL;
309 struct request *__rq;
310
311 while (*p) {
312 parent = *p;
313 __rq = rb_entry(parent, struct request, rb_node);
314
315 if (rq->sector < __rq->sector)
316 p = &(*p)->rb_left;
317 else if (rq->sector > __rq->sector)
318 p = &(*p)->rb_right;
319 else
320 return __rq;
321 }
322
323 rb_link_node(&rq->rb_node, parent, p);
324 rb_insert_color(&rq->rb_node, root);
325 return NULL;
326}
327
328EXPORT_SYMBOL(elv_rb_add);
329
330void elv_rb_del(struct rb_root *root, struct request *rq)
331{
332 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
333 rb_erase(&rq->rb_node, root);
334 RB_CLEAR_NODE(&rq->rb_node);
335}
336
337EXPORT_SYMBOL(elv_rb_del);
338
339struct request *elv_rb_find(struct rb_root *root, sector_t sector)
340{
341 struct rb_node *n = root->rb_node;
342 struct request *rq;
343
344 while (n) {
345 rq = rb_entry(n, struct request, rb_node);
346
347 if (sector < rq->sector)
348 n = n->rb_left;
349 else if (sector > rq->sector)
350 n = n->rb_right;
351 else
352 return rq;
353 }
354
355 return NULL;
356}
357
358EXPORT_SYMBOL(elv_rb_find);
359
360/*
Tejun Heo8922e162005-10-20 16:23:44 +0200361 * Insert rq into dispatch queue of q. Queue lock must be held on
Jens Axboe2e662b62006-07-13 11:55:04 +0200362 * entry. rq is sort insted into the dispatch queue. To be used by
363 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200364 */
Jens Axboe1b47f532005-10-20 16:37:00 +0200365void elv_dispatch_sort(request_queue_t *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200366{
367 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200368 struct list_head *entry;
369
Tejun Heo06b86242005-10-20 16:46:23 +0200370 if (q->last_merge == rq)
371 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200372
373 elv_rqhash_del(q, rq);
374
Tejun Heo15853af2005-11-10 08:52:05 +0100375 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200376
Jens Axboe1b47f532005-10-20 16:37:00 +0200377 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200378
Tejun Heo8922e162005-10-20 16:23:44 +0200379 list_for_each_prev(entry, &q->queue_head) {
380 struct request *pos = list_entry_rq(entry);
381
Jens Axboe4aff5e22006-08-10 08:44:47 +0200382 if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
Tejun Heo8922e162005-10-20 16:23:44 +0200383 break;
384 if (rq->sector >= boundary) {
385 if (pos->sector < boundary)
386 continue;
387 } else {
388 if (pos->sector >= boundary)
389 break;
390 }
391 if (rq->sector >= pos->sector)
392 break;
393 }
394
395 list_add(&rq->queuelist, entry);
396}
397
Jens Axboe2e662b62006-07-13 11:55:04 +0200398EXPORT_SYMBOL(elv_dispatch_sort);
399
Jens Axboe98170642006-07-28 09:23:08 +0200400/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200401 * Insert rq into dispatch queue of q. Queue lock must be held on
402 * entry. rq is added to the back of the dispatch queue. To be used by
403 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200404 */
405void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
406{
407 if (q->last_merge == rq)
408 q->last_merge = NULL;
409
410 elv_rqhash_del(q, rq);
411
412 q->nr_sorted--;
413
414 q->end_sector = rq_end_sector(rq);
415 q->boundary_rq = rq;
416 list_add_tail(&rq->queuelist, &q->queue_head);
417}
418
Jens Axboe2e662b62006-07-13 11:55:04 +0200419EXPORT_SYMBOL(elv_dispatch_add_tail);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
422{
423 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200424 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200425 int ret;
426
Jens Axboe98170642006-07-28 09:23:08 +0200427 /*
428 * First try one-hit cache.
429 */
Tejun Heo06b86242005-10-20 16:46:23 +0200430 if (q->last_merge) {
431 ret = elv_try_merge(q->last_merge, bio);
432 if (ret != ELEVATOR_NO_MERGE) {
433 *req = q->last_merge;
434 return ret;
435 }
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jens Axboe98170642006-07-28 09:23:08 +0200438 /*
439 * See if our hash lookup can find a potential backmerge.
440 */
441 __rq = elv_rqhash_find(q, bio->bi_sector);
442 if (__rq && elv_rq_merge_ok(__rq, bio)) {
443 *req = __rq;
444 return ELEVATOR_BACK_MERGE;
445 }
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (e->ops->elevator_merge_fn)
448 return e->ops->elevator_merge_fn(q, req, bio);
449
450 return ELEVATOR_NO_MERGE;
451}
452
Jens Axboe2e662b62006-07-13 11:55:04 +0200453void elv_merged_request(request_queue_t *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 elevator_t *e = q->elevator;
456
457 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200458 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200459
Jens Axboe2e662b62006-07-13 11:55:04 +0200460 if (type == ELEVATOR_BACK_MERGE)
461 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200462
Tejun Heo06b86242005-10-20 16:46:23 +0200463 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466void elv_merge_requests(request_queue_t *q, struct request *rq,
467 struct request *next)
468{
469 elevator_t *e = q->elevator;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (e->ops->elevator_merge_req_fn)
472 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200473
Jens Axboe98170642006-07-28 09:23:08 +0200474 elv_rqhash_reposition(q, rq);
475 elv_rqhash_del(q, next);
476
477 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200478 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Tejun Heo8922e162005-10-20 16:23:44 +0200481void elv_requeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 elevator_t *e = q->elevator;
484
485 /*
486 * it already went through dequeue, we need to decrement the
487 * in_flight count again
488 */
Tejun Heo8922e162005-10-20 16:23:44 +0200489 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 q->in_flight--;
Tejun Heo8922e162005-10-20 16:23:44 +0200491 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
492 e->ops->elevator_deactivate_req_fn(q, rq);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jens Axboe4aff5e22006-08-10 08:44:47 +0200495 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Tejun Heo30e96562006-02-08 01:01:31 -0800497 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Tejun Heo15853af2005-11-10 08:52:05 +0100500static void elv_drain_elevator(request_queue_t *q)
501{
502 static int printed;
503 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
504 ;
505 if (q->nr_sorted == 0)
506 return;
507 if (printed++ < 10) {
508 printk(KERN_ERR "%s: forced dispatching is broken "
509 "(nr_sorted=%u), please report this\n",
510 q->elevator->elevator_type->elevator_name, q->nr_sorted);
511 }
512}
513
Tejun Heo30e96562006-02-08 01:01:31 -0800514void elv_insert(request_queue_t *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Tejun Heo797e7db2006-01-06 09:51:03 +0100516 struct list_head *pos;
517 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200518 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100519
Jens Axboe2056a782006-03-23 20:00:26 +0100520 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rq->q = q;
523
Tejun Heo8922e162005-10-20 16:23:44 +0200524 switch (where) {
525 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200526 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200527
528 list_add(&rq->queuelist, &q->queue_head);
529 break;
530
531 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200532 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100533 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200534 list_add_tail(&rq->queuelist, &q->queue_head);
535 /*
536 * We kick the queue here for the following reasons.
537 * - The elevator might have returned NULL previously
538 * to delay requests and returned them now. As the
539 * queue wasn't empty before this request, ll_rw_blk
540 * won't run the queue on return, resulting in hang.
541 * - Usually, back inserted requests won't be merged
542 * with anything. There's no point in delaying queue
543 * processing.
544 */
545 blk_remove_plug(q);
546 q->request_fn(q);
547 break;
548
549 case ELEVATOR_INSERT_SORT:
550 BUG_ON(!blk_fs_request(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200551 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100552 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200553 if (rq_mergeable(rq)) {
554 elv_rqhash_add(q, rq);
555 if (!q->last_merge)
556 q->last_merge = rq;
557 }
558
Tejun Heoca235092005-11-01 17:23:49 +0900559 /*
560 * Some ioscheds (cfq) run q->request_fn directly, so
561 * rq cannot be accessed after calling
562 * elevator_add_req_fn.
563 */
564 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200565 break;
566
Tejun Heo797e7db2006-01-06 09:51:03 +0100567 case ELEVATOR_INSERT_REQUEUE:
568 /*
569 * If ordered flush isn't in progress, we do front
570 * insertion; otherwise, requests should be requeued
571 * in ordseq order.
572 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200573 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100574
575 if (q->ordseq == 0) {
576 list_add(&rq->queuelist, &q->queue_head);
577 break;
578 }
579
580 ordseq = blk_ordered_req_seq(rq);
581
582 list_for_each(pos, &q->queue_head) {
583 struct request *pos_rq = list_entry_rq(pos);
584 if (ordseq <= blk_ordered_req_seq(pos_rq))
585 break;
586 }
587
588 list_add_tail(&rq->queuelist, pos);
Jens Axboedac07ec2006-05-11 08:20:16 +0200589 /*
590 * most requeues happen because of a busy condition, don't
591 * force unplug of the queue for that case.
592 */
593 unplug_it = 0;
Tejun Heo797e7db2006-01-06 09:51:03 +0100594 break;
595
Tejun Heo8922e162005-10-20 16:23:44 +0200596 default:
597 printk(KERN_ERR "%s: bad insertion point %d\n",
598 __FUNCTION__, where);
599 BUG();
600 }
601
Jens Axboedac07ec2006-05-11 08:20:16 +0200602 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200603 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
604 - q->in_flight;
605
606 if (nrq >= q->unplug_thresh)
607 __generic_unplug_device(q);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Tejun Heo30e96562006-02-08 01:01:31 -0800611void __elv_add_request(request_queue_t *q, struct request *rq, int where,
612 int plug)
613{
614 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200615 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800616
Jens Axboe4aff5e22006-08-10 08:44:47 +0200617 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800618 /*
619 * toggle ordered color
620 */
621 if (blk_barrier_rq(rq))
622 q->ordcolor ^= 1;
623
624 /*
625 * barriers implicitly indicate back insertion
626 */
627 if (where == ELEVATOR_INSERT_SORT)
628 where = ELEVATOR_INSERT_BACK;
629
630 /*
631 * this request is scheduling boundary, update
632 * end_sector
633 */
634 if (blk_fs_request(rq)) {
635 q->end_sector = rq_end_sector(rq);
636 q->boundary_rq = rq;
637 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200638 } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800639 where = ELEVATOR_INSERT_BACK;
640
641 if (plug)
642 blk_plug_device(q);
643
644 elv_insert(q, rq, where);
645}
646
Jens Axboe2e662b62006-07-13 11:55:04 +0200647EXPORT_SYMBOL(__elv_add_request);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649void elv_add_request(request_queue_t *q, struct request *rq, int where,
650 int plug)
651{
652 unsigned long flags;
653
654 spin_lock_irqsave(q->queue_lock, flags);
655 __elv_add_request(q, rq, where, plug);
656 spin_unlock_irqrestore(q->queue_lock, flags);
657}
658
Jens Axboe2e662b62006-07-13 11:55:04 +0200659EXPORT_SYMBOL(elv_add_request);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static inline struct request *__elv_next_request(request_queue_t *q)
662{
Tejun Heo8922e162005-10-20 16:23:44 +0200663 struct request *rq;
664
Tejun Heo797e7db2006-01-06 09:51:03 +0100665 while (1) {
666 while (!list_empty(&q->queue_head)) {
667 rq = list_entry_rq(q->queue_head.next);
668 if (blk_do_ordered(q, &rq))
669 return rq;
670 }
Tejun Heo8922e162005-10-20 16:23:44 +0200671
Tejun Heo797e7db2006-01-06 09:51:03 +0100672 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
673 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677struct request *elv_next_request(request_queue_t *q)
678{
679 struct request *rq;
680 int ret;
681
682 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200683 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200684 elevator_t *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Tejun Heo8922e162005-10-20 16:23:44 +0200686 /*
687 * This is the first time the device driver
688 * sees this request (possibly after
689 * requeueing). Notify IO scheduler.
690 */
691 if (blk_sorted_rq(rq) &&
692 e->ops->elevator_activate_req_fn)
693 e->ops->elevator_activate_req_fn(q, rq);
694
695 /*
696 * just mark as started even if we don't start
697 * it, a request that has been delayed should
698 * not be passed by new incoming requests
699 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200700 rq->cmd_flags |= REQ_STARTED;
Jens Axboe2056a782006-03-23 20:00:26 +0100701 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
Tejun Heo8922e162005-10-20 16:23:44 +0200702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Tejun Heo8922e162005-10-20 16:23:44 +0200704 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200705 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200706 q->boundary_rq = NULL;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jens Axboe4aff5e22006-08-10 08:44:47 +0200709 if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711
712 ret = q->prep_rq_fn(q, rq);
713 if (ret == BLKPREP_OK) {
714 break;
715 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500716 /*
717 * the request may have been (partially) prepped.
718 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200719 * avoid resource deadlock. REQ_STARTED will
720 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 rq = NULL;
723 break;
724 } else if (ret == BLKPREP_KILL) {
725 int nr_bytes = rq->hard_nr_sectors << 9;
726
727 if (!nr_bytes)
728 nr_bytes = rq->data_len;
729
730 blkdev_dequeue_request(rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200731 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 end_that_request_chunk(rq, 0, nr_bytes);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100733 end_that_request_last(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 } else {
735 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
736 ret);
737 break;
738 }
739 }
740
741 return rq;
742}
743
Jens Axboe2e662b62006-07-13 11:55:04 +0200744EXPORT_SYMBOL(elv_next_request);
745
Tejun Heo8922e162005-10-20 16:23:44 +0200746void elv_dequeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Tejun Heo8922e162005-10-20 16:23:44 +0200748 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200749 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200750
751 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * the time frame between a request being removed from the lists
755 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200756 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758 if (blk_account_rq(rq))
759 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Jens Axboe2e662b62006-07-13 11:55:04 +0200762EXPORT_SYMBOL(elv_dequeue_request);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764int elv_queue_empty(request_queue_t *q)
765{
766 elevator_t *e = q->elevator;
767
Tejun Heo8922e162005-10-20 16:23:44 +0200768 if (!list_empty(&q->queue_head))
769 return 0;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (e->ops->elevator_queue_empty_fn)
772 return e->ops->elevator_queue_empty_fn(q);
773
Tejun Heo8922e162005-10-20 16:23:44 +0200774 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Jens Axboe2e662b62006-07-13 11:55:04 +0200777EXPORT_SYMBOL(elv_queue_empty);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779struct request *elv_latter_request(request_queue_t *q, struct request *rq)
780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 elevator_t *e = q->elevator;
782
783 if (e->ops->elevator_latter_req_fn)
784 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return NULL;
786}
787
788struct request *elv_former_request(request_queue_t *q, struct request *rq)
789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 elevator_t *e = q->elevator;
791
792 if (e->ops->elevator_former_req_fn)
793 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return NULL;
795}
796
Jens Axboecb78b282006-07-28 09:32:57 +0200797int elv_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 elevator_t *e = q->elevator;
800
801 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200802 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 rq->elevator_private = NULL;
805 return 0;
806}
807
808void elv_put_request(request_queue_t *q, struct request *rq)
809{
810 elevator_t *e = q->elevator;
811
812 if (e->ops->elevator_put_req_fn)
813 e->ops->elevator_put_req_fn(q, rq);
814}
815
Jens Axboecb78b282006-07-28 09:32:57 +0200816int elv_may_queue(request_queue_t *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 elevator_t *e = q->elevator;
819
820 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200821 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 return ELV_MQUEUE_MAY;
824}
825
826void elv_completed_request(request_queue_t *q, struct request *rq)
827{
828 elevator_t *e = q->elevator;
829
830 /*
831 * request is released from the driver, io must be done
832 */
Tejun Heo8922e162005-10-20 16:23:44 +0200833 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100835 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
836 e->ops->elevator_completed_req_fn(q, rq);
837 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100838
Tejun Heo1bc691d2006-01-12 15:39:26 +0100839 /*
840 * Check if the queue is waiting for fs requests to be
841 * drained for flush sequence.
842 */
843 if (unlikely(q->ordseq)) {
844 struct request *first_rq = list_entry_rq(q->queue_head.next);
845 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100846 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
847 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
848 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
849 q->request_fn(q);
850 }
Tejun Heo8922e162005-10-20 16:23:44 +0200851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Al Viro3d1ab402006-03-18 18:35:43 -0500854#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
855
856static ssize_t
857elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
858{
859 elevator_t *e = container_of(kobj, elevator_t, kobj);
860 struct elv_fs_entry *entry = to_elv(attr);
861 ssize_t error;
862
863 if (!entry->show)
864 return -EIO;
865
866 mutex_lock(&e->sysfs_lock);
867 error = e->ops ? entry->show(e, page) : -ENOENT;
868 mutex_unlock(&e->sysfs_lock);
869 return error;
870}
871
872static ssize_t
873elv_attr_store(struct kobject *kobj, struct attribute *attr,
874 const char *page, size_t length)
875{
876 elevator_t *e = container_of(kobj, elevator_t, kobj);
877 struct elv_fs_entry *entry = to_elv(attr);
878 ssize_t error;
879
880 if (!entry->store)
881 return -EIO;
882
883 mutex_lock(&e->sysfs_lock);
884 error = e->ops ? entry->store(e, page, length) : -ENOENT;
885 mutex_unlock(&e->sysfs_lock);
886 return error;
887}
888
889static struct sysfs_ops elv_sysfs_ops = {
890 .show = elv_attr_show,
891 .store = elv_attr_store,
892};
893
894static struct kobj_type elv_ktype = {
895 .sysfs_ops = &elv_sysfs_ops,
896 .release = elevator_release,
897};
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899int elv_register_queue(struct request_queue *q)
900{
901 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500902 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Al Viro3d1ab402006-03-18 18:35:43 -0500904 e->kobj.parent = &q->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Al Viro3d1ab402006-03-18 18:35:43 -0500906 error = kobject_add(&e->kobj);
907 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500908 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500909 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500910 while (attr->attr.name) {
911 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500912 break;
Al Viroe572ec72006-03-18 22:27:18 -0500913 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500914 }
915 }
916 kobject_uevent(&e->kobj, KOBJ_ADD);
917 }
918 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Jens Axboebc1c1162006-06-08 08:49:06 +0200921static void __elv_unregister_queue(elevator_t *e)
922{
923 kobject_uevent(&e->kobj, KOBJ_REMOVE);
924 kobject_del(&e->kobj);
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927void elv_unregister_queue(struct request_queue *q)
928{
Jens Axboebc1c1162006-06-08 08:49:06 +0200929 if (q)
930 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933int elv_register(struct elevator_type *e)
934{
Tejun Heo2824bc932005-10-20 10:56:41 +0200935 spin_lock_irq(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +0100936 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 list_add_tail(&e->list, &elv_list);
938 spin_unlock_irq(&elv_list_lock);
939
940 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
Nate Diller5f003972006-01-24 10:07:58 +0100941 if (!strcmp(e->elevator_name, chosen_elevator) ||
942 (!*chosen_elevator &&
943 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
944 printk(" (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 printk("\n");
946 return 0;
947}
948EXPORT_SYMBOL_GPL(elv_register);
949
950void elv_unregister(struct elevator_type *e)
951{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800952 struct task_struct *g, *p;
953
954 /*
955 * Iterate every thread in the process to remove the io contexts.
956 */
Al Viroe17a9482006-03-18 13:21:20 -0500957 if (e->ops.trim) {
958 read_lock(&tasklist_lock);
959 do_each_thread(g, p) {
960 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +0400961 if (p->io_context)
962 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -0500963 task_unlock(p);
964 } while_each_thread(g, p);
965 read_unlock(&tasklist_lock);
966 }
Christoph Hellwig83521d32005-10-30 15:01:39 -0800967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_lock_irq(&elv_list_lock);
969 list_del_init(&e->list);
970 spin_unlock_irq(&elv_list_lock);
971}
972EXPORT_SYMBOL_GPL(elv_unregister);
973
974/*
975 * switch to new_e io scheduler. be careful not to introduce deadlocks -
976 * we don't free the old io scheduler, before we have allocated what we
977 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200978 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Al Viro3d1ab402006-03-18 18:35:43 -0500980static int elevator_switch(request_queue_t *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Tejun Heocb98fc82005-10-28 08:29:39 +0200982 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +0200983 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Tejun Heocb98fc82005-10-28 08:29:39 +0200985 /*
986 * Allocate new elevator
987 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200988 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -0500990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jens Axboebc1c1162006-06-08 08:49:06 +0200992 data = elevator_init_queue(q, e);
993 if (!data) {
994 kobject_put(&e->kobj);
995 return 0;
996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
Tejun Heocb98fc82005-10-28 08:29:39 +0200999 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001001 spin_lock_irq(q->queue_lock);
1002
Jens Axboe64521d12005-10-28 08:30:39 +02001003 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001004
Tejun Heo15853af2005-11-10 08:52:05 +01001005 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001006
1007 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +01001008 blk_remove_plug(q);
1009 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001010 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001011 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001012 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001013 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001014 }
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001017 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 old_elevator = q->elevator;
1020
1021 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * attach and start new elevator
1023 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001024 elevator_attach(q, e, data);
1025
1026 spin_unlock_irq(q->queue_lock);
1027
1028 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (elv_register_queue(q))
1031 goto fail_register;
1032
1033 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001034 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
1036 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +02001037 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001038 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040fail_register:
1041 /*
1042 * switch failed, exit the new io scheduler and reattach the old
1043 * one again (along with re-adding the sysfs dir)
1044 */
1045 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 q->elevator = old_elevator;
1047 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +02001048 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
1053{
1054 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +01001055 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct elevator_type *e;
1057
Tejun Heobe561232005-11-10 08:55:01 +01001058 elevator_name[sizeof(elevator_name) - 1] = '\0';
1059 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1060 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Tejun Heobe561232005-11-10 08:55:01 +01001062 if (len && elevator_name[len - 1] == '\n')
1063 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 e = elevator_get(elevator_name);
1066 if (!e) {
1067 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1068 return -EINVAL;
1069 }
1070
Nate Diller2ca7d932005-10-30 15:02:24 -08001071 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1072 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Al Viro3d1ab402006-03-18 18:35:43 -05001076 if (!elevator_switch(q, e))
1077 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return count;
1079}
1080
1081ssize_t elv_iosched_show(request_queue_t *q, char *name)
1082{
1083 elevator_t *e = q->elevator;
1084 struct elevator_type *elv = e->elevator_type;
1085 struct list_head *entry;
1086 int len = 0;
1087
Vasily Tarasovc5841642006-10-11 13:26:30 +02001088 spin_lock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 list_for_each(entry, &elv_list) {
1090 struct elevator_type *__e;
1091
1092 __e = list_entry(entry, struct elevator_type, list);
1093 if (!strcmp(elv->elevator_name, __e->elevator_name))
1094 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1095 else
1096 len += sprintf(name+len, "%s ", __e->elevator_name);
1097 }
Vasily Tarasovc5841642006-10-11 13:26:30 +02001098 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 len += sprintf(len+name, "\n");
1101 return len;
1102}
1103
Jens Axboe2e662b62006-07-13 11:55:04 +02001104struct request *elv_rb_former_request(request_queue_t *q, struct request *rq)
1105{
1106 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1107
1108 if (rbprev)
1109 return rb_entry_rq(rbprev);
1110
1111 return NULL;
1112}
1113
1114EXPORT_SYMBOL(elv_rb_former_request);
1115
1116struct request *elv_rb_latter_request(request_queue_t *q, struct request *rq)
1117{
1118 struct rb_node *rbnext = rb_next(&rq->rb_node);
1119
1120 if (rbnext)
1121 return rb_entry_rq(rbnext);
1122
1123 return NULL;
1124}
1125
1126EXPORT_SYMBOL(elv_rb_latter_request);