blob: 96a00c822748cf8a673071a32806358963f5cdad [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/*
Jens Axboeda775262006-12-20 11:04:12 +010054 * Query io scheduler to see if the current process issuing bio may be
55 * merged with rq.
56 */
57static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
58{
59 request_queue_t *q = rq->q;
60 elevator_t *e = q->elevator;
61
62 if (e->ops->elevator_allow_merge_fn)
63 return e->ops->elevator_allow_merge_fn(q, rq, bio);
64
65 return 1;
66}
67
68/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * can we safely merge with this request?
70 */
71inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
72{
73 if (!rq_mergeable(rq))
74 return 0;
75
76 /*
77 * different data direction or already started, don't merge
78 */
79 if (bio_data_dir(bio) != rq_data_dir(rq))
80 return 0;
81
82 /*
Jens Axboeda775262006-12-20 11:04:12 +010083 * must be same device and not a special request
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
Jens Axboebb4067e2006-12-21 21:20:01 +010085 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
Jens Axboeda775262006-12-20 11:04:12 +010086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Jens Axboeda775262006-12-20 11:04:12 +010088 if (!elv_iosched_allow_merge(rq, bio))
89 return 0;
90
91 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93EXPORT_SYMBOL(elv_rq_merge_ok);
94
Coywolf Qi Hunt769db452005-12-28 10:55:49 +010095static inline int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 int ret = ELEVATOR_NO_MERGE;
98
99 /*
100 * we can merge and sequence is ok, check if it's possible
101 */
102 if (elv_rq_merge_ok(__rq, bio)) {
103 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
104 ret = ELEVATOR_BACK_MERGE;
105 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
106 ret = ELEVATOR_FRONT_MERGE;
107 }
108
109 return ret;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct elevator_type *elevator_find(const char *name)
113{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200114 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct list_head *entry;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 list_for_each(entry, &elv_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Vasily Tarasova22b1692006-10-11 09:24:27 +0200119 e = list_entry(entry, struct elevator_type, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Vasily Tarasova22b1692006-10-11 09:24:27 +0200121 if (!strcmp(e->elevator_name, name))
122 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Vasily Tarasova22b1692006-10-11 09:24:27 +0200125 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void elevator_put(struct elevator_type *e)
129{
130 module_put(e->elevator_owner);
131}
132
133static struct elevator_type *elevator_get(const char *name)
134{
Tejun Heo2824bc932005-10-20 10:56:41 +0200135 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Tejun Heo2824bc932005-10-20 10:56:41 +0200137 spin_lock_irq(&elv_list_lock);
138
139 e = elevator_find(name);
140 if (e && !try_module_get(e->elevator_owner))
141 e = NULL;
142
143 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return e;
146}
147
Jens Axboebc1c1162006-06-08 08:49:06 +0200148static void *elevator_init_queue(request_queue_t *q, struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Jens Axboebb37b942006-12-01 10:42:33 +0100150 return eq->ops->elevator_init_fn(q);
Jens Axboebc1c1162006-06-08 08:49:06 +0200151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jens Axboebc1c1162006-06-08 08:49:06 +0200153static void elevator_attach(request_queue_t *q, struct elevator_queue *eq,
154 void *data)
155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200157 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static char chosen_elevator[16];
161
Nate Diller5f003972006-01-24 10:07:58 +0100162static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100164 /*
165 * Be backwards-compatible with previous kernels, so users
166 * won't get the wrong elevator.
167 */
Nate Diller5f003972006-01-24 10:07:58 +0100168 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100169 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100170 else
Nate Diller5f003972006-01-24 10:07:58 +0100171 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800172 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175__setup("elevator=", elevator_setup);
176
Al Viro3d1ab402006-03-18 18:35:43 -0500177static struct kobj_type elv_ktype;
178
Jens Axboeb5deef92006-07-19 23:39:40 +0200179static elevator_t *elevator_alloc(request_queue_t *q, struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500180{
Jens Axboe98170642006-07-28 09:23:08 +0200181 elevator_t *eq;
182 int i;
183
Jens Axboeb5deef92006-07-19 23:39:40 +0200184 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200185 if (unlikely(!eq))
186 goto err;
187
188 memset(eq, 0, sizeof(*eq));
189 eq->ops = &e->ops;
190 eq->elevator_type = e;
191 kobject_init(&eq->kobj);
192 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
193 eq->kobj.ktype = &elv_ktype;
194 mutex_init(&eq->sysfs_lock);
195
Jens Axboeb5deef92006-07-19 23:39:40 +0200196 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
197 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200198 if (!eq->hash)
199 goto err;
200
201 for (i = 0; i < ELV_HASH_ENTRIES; i++)
202 INIT_HLIST_HEAD(&eq->hash[i]);
203
Al Viro3d1ab402006-03-18 18:35:43 -0500204 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200205err:
206 kfree(eq);
207 elevator_put(e);
208 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500209}
210
211static void elevator_release(struct kobject *kobj)
212{
213 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200214
Al Viro3d1ab402006-03-18 18:35:43 -0500215 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200216 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500217 kfree(e);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220int elevator_init(request_queue_t *q, char *name)
221{
222 struct elevator_type *e = NULL;
223 struct elevator_queue *eq;
224 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200225 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Tejun Heocb98fc82005-10-28 08:29:39 +0200227 INIT_LIST_HEAD(&q->queue_head);
228 q->last_merge = NULL;
229 q->end_sector = 0;
230 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200231
Nate Diller5f003972006-01-24 10:07:58 +0100232 if (name && !(e = elevator_get(name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EINVAL;
234
Nate Diller248d5ca2006-01-24 10:09:14 +0100235 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
236 printk("I/O scheduler %s not found\n", chosen_elevator);
237
238 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
239 printk("Default I/O scheduler not found, using no-op\n");
240 e = elevator_get("noop");
Nate Diller5f003972006-01-24 10:07:58 +0100241 }
242
Jens Axboeb5deef92006-07-19 23:39:40 +0200243 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500244 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jens Axboebc1c1162006-06-08 08:49:06 +0200247 data = elevator_init_queue(q, eq);
248 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500249 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200250 return -ENOMEM;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jens Axboebc1c1162006-06-08 08:49:06 +0200253 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ret;
255}
256
Jens Axboe2e662b62006-07-13 11:55:04 +0200257EXPORT_SYMBOL(elevator_init);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259void elevator_exit(elevator_t *e)
260{
Al Viro3d1ab402006-03-18 18:35:43 -0500261 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (e->ops->elevator_exit_fn)
263 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500264 e->ops = NULL;
265 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Al Viro3d1ab402006-03-18 18:35:43 -0500267 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Jens Axboe2e662b62006-07-13 11:55:04 +0200270EXPORT_SYMBOL(elevator_exit);
271
Jens Axboecad97512007-01-14 22:26:09 +1100272static void elv_activate_rq(request_queue_t *q, struct request *rq)
273{
274 elevator_t *e = q->elevator;
275
276 if (e->ops->elevator_activate_req_fn)
277 e->ops->elevator_activate_req_fn(q, rq);
278}
279
280static void elv_deactivate_rq(request_queue_t *q, struct request *rq)
281{
282 elevator_t *e = q->elevator;
283
284 if (e->ops->elevator_deactivate_req_fn)
285 e->ops->elevator_deactivate_req_fn(q, rq);
286}
287
Jens Axboe98170642006-07-28 09:23:08 +0200288static inline void __elv_rqhash_del(struct request *rq)
289{
290 hlist_del_init(&rq->hash);
291}
292
293static void elv_rqhash_del(request_queue_t *q, struct request *rq)
294{
295 if (ELV_ON_HASH(rq))
296 __elv_rqhash_del(rq);
297}
298
299static void elv_rqhash_add(request_queue_t *q, struct request *rq)
300{
301 elevator_t *e = q->elevator;
302
303 BUG_ON(ELV_ON_HASH(rq));
304 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
305}
306
307static void elv_rqhash_reposition(request_queue_t *q, struct request *rq)
308{
309 __elv_rqhash_del(rq);
310 elv_rqhash_add(q, rq);
311}
312
313static struct request *elv_rqhash_find(request_queue_t *q, sector_t offset)
314{
315 elevator_t *e = q->elevator;
316 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
317 struct hlist_node *entry, *next;
318 struct request *rq;
319
320 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
321 BUG_ON(!ELV_ON_HASH(rq));
322
323 if (unlikely(!rq_mergeable(rq))) {
324 __elv_rqhash_del(rq);
325 continue;
326 }
327
328 if (rq_hash_key(rq) == offset)
329 return rq;
330 }
331
332 return NULL;
333}
334
Tejun Heo8922e162005-10-20 16:23:44 +0200335/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200336 * RB-tree support functions for inserting/lookup/removal of requests
337 * in a sorted RB tree.
338 */
339struct request *elv_rb_add(struct rb_root *root, struct request *rq)
340{
341 struct rb_node **p = &root->rb_node;
342 struct rb_node *parent = NULL;
343 struct request *__rq;
344
345 while (*p) {
346 parent = *p;
347 __rq = rb_entry(parent, struct request, rb_node);
348
349 if (rq->sector < __rq->sector)
350 p = &(*p)->rb_left;
351 else if (rq->sector > __rq->sector)
352 p = &(*p)->rb_right;
353 else
354 return __rq;
355 }
356
357 rb_link_node(&rq->rb_node, parent, p);
358 rb_insert_color(&rq->rb_node, root);
359 return NULL;
360}
361
362EXPORT_SYMBOL(elv_rb_add);
363
364void elv_rb_del(struct rb_root *root, struct request *rq)
365{
366 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
367 rb_erase(&rq->rb_node, root);
368 RB_CLEAR_NODE(&rq->rb_node);
369}
370
371EXPORT_SYMBOL(elv_rb_del);
372
373struct request *elv_rb_find(struct rb_root *root, sector_t sector)
374{
375 struct rb_node *n = root->rb_node;
376 struct request *rq;
377
378 while (n) {
379 rq = rb_entry(n, struct request, rb_node);
380
381 if (sector < rq->sector)
382 n = n->rb_left;
383 else if (sector > rq->sector)
384 n = n->rb_right;
385 else
386 return rq;
387 }
388
389 return NULL;
390}
391
392EXPORT_SYMBOL(elv_rb_find);
393
394/*
Tejun Heo8922e162005-10-20 16:23:44 +0200395 * Insert rq into dispatch queue of q. Queue lock must be held on
Jens Axboe2e662b62006-07-13 11:55:04 +0200396 * entry. rq is sort insted into the dispatch queue. To be used by
397 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200398 */
Jens Axboe1b47f532005-10-20 16:37:00 +0200399void elv_dispatch_sort(request_queue_t *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200400{
401 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200402 struct list_head *entry;
403
Tejun Heo06b86242005-10-20 16:46:23 +0200404 if (q->last_merge == rq)
405 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200406
407 elv_rqhash_del(q, rq);
408
Tejun Heo15853af2005-11-10 08:52:05 +0100409 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200410
Jens Axboe1b47f532005-10-20 16:37:00 +0200411 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200412
Tejun Heo8922e162005-10-20 16:23:44 +0200413 list_for_each_prev(entry, &q->queue_head) {
414 struct request *pos = list_entry_rq(entry);
415
Jens Axboe783660b2007-01-19 11:27:47 +1100416 if (rq_data_dir(rq) != rq_data_dir(pos))
417 break;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200418 if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
Tejun Heo8922e162005-10-20 16:23:44 +0200419 break;
420 if (rq->sector >= boundary) {
421 if (pos->sector < boundary)
422 continue;
423 } else {
424 if (pos->sector >= boundary)
425 break;
426 }
427 if (rq->sector >= pos->sector)
428 break;
429 }
430
431 list_add(&rq->queuelist, entry);
432}
433
Jens Axboe2e662b62006-07-13 11:55:04 +0200434EXPORT_SYMBOL(elv_dispatch_sort);
435
Jens Axboe98170642006-07-28 09:23:08 +0200436/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200437 * Insert rq into dispatch queue of q. Queue lock must be held on
438 * entry. rq is added to the back of the dispatch queue. To be used by
439 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200440 */
441void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
442{
443 if (q->last_merge == rq)
444 q->last_merge = NULL;
445
446 elv_rqhash_del(q, rq);
447
448 q->nr_sorted--;
449
450 q->end_sector = rq_end_sector(rq);
451 q->boundary_rq = rq;
452 list_add_tail(&rq->queuelist, &q->queue_head);
453}
454
Jens Axboe2e662b62006-07-13 11:55:04 +0200455EXPORT_SYMBOL(elv_dispatch_add_tail);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
458{
459 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200460 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200461 int ret;
462
Jens Axboe98170642006-07-28 09:23:08 +0200463 /*
464 * First try one-hit cache.
465 */
Tejun Heo06b86242005-10-20 16:46:23 +0200466 if (q->last_merge) {
467 ret = elv_try_merge(q->last_merge, bio);
468 if (ret != ELEVATOR_NO_MERGE) {
469 *req = q->last_merge;
470 return ret;
471 }
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jens Axboe98170642006-07-28 09:23:08 +0200474 /*
475 * See if our hash lookup can find a potential backmerge.
476 */
477 __rq = elv_rqhash_find(q, bio->bi_sector);
478 if (__rq && elv_rq_merge_ok(__rq, bio)) {
479 *req = __rq;
480 return ELEVATOR_BACK_MERGE;
481 }
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (e->ops->elevator_merge_fn)
484 return e->ops->elevator_merge_fn(q, req, bio);
485
486 return ELEVATOR_NO_MERGE;
487}
488
Jens Axboe2e662b62006-07-13 11:55:04 +0200489void elv_merged_request(request_queue_t *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 elevator_t *e = q->elevator;
492
493 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200494 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200495
Jens Axboe2e662b62006-07-13 11:55:04 +0200496 if (type == ELEVATOR_BACK_MERGE)
497 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200498
Tejun Heo06b86242005-10-20 16:46:23 +0200499 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502void elv_merge_requests(request_queue_t *q, struct request *rq,
503 struct request *next)
504{
505 elevator_t *e = q->elevator;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (e->ops->elevator_merge_req_fn)
508 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200509
Jens Axboe98170642006-07-28 09:23:08 +0200510 elv_rqhash_reposition(q, rq);
511 elv_rqhash_del(q, next);
512
513 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200514 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Tejun Heo8922e162005-10-20 16:23:44 +0200517void elv_requeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * it already went through dequeue, we need to decrement the
521 * in_flight count again
522 */
Tejun Heo8922e162005-10-20 16:23:44 +0200523 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 q->in_flight--;
Jens Axboecad97512007-01-14 22:26:09 +1100525 if (blk_sorted_rq(rq))
526 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jens Axboe4aff5e22006-08-10 08:44:47 +0200529 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Tejun Heo30e96562006-02-08 01:01:31 -0800531 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Tejun Heo15853af2005-11-10 08:52:05 +0100534static void elv_drain_elevator(request_queue_t *q)
535{
536 static int printed;
537 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
538 ;
539 if (q->nr_sorted == 0)
540 return;
541 if (printed++ < 10) {
542 printk(KERN_ERR "%s: forced dispatching is broken "
543 "(nr_sorted=%u), please report this\n",
544 q->elevator->elevator_type->elevator_name, q->nr_sorted);
545 }
546}
547
Tejun Heo30e96562006-02-08 01:01:31 -0800548void elv_insert(request_queue_t *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Tejun Heo797e7db2006-01-06 09:51:03 +0100550 struct list_head *pos;
551 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200552 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100553
Jens Axboe2056a782006-03-23 20:00:26 +0100554 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 rq->q = q;
557
Tejun Heo8922e162005-10-20 16:23:44 +0200558 switch (where) {
559 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200560 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200561
562 list_add(&rq->queuelist, &q->queue_head);
563 break;
564
565 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200566 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100567 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200568 list_add_tail(&rq->queuelist, &q->queue_head);
569 /*
570 * We kick the queue here for the following reasons.
571 * - The elevator might have returned NULL previously
572 * to delay requests and returned them now. As the
573 * queue wasn't empty before this request, ll_rw_blk
574 * won't run the queue on return, resulting in hang.
575 * - Usually, back inserted requests won't be merged
576 * with anything. There's no point in delaying queue
577 * processing.
578 */
579 blk_remove_plug(q);
580 q->request_fn(q);
581 break;
582
583 case ELEVATOR_INSERT_SORT:
584 BUG_ON(!blk_fs_request(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200585 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100586 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200587 if (rq_mergeable(rq)) {
588 elv_rqhash_add(q, rq);
589 if (!q->last_merge)
590 q->last_merge = rq;
591 }
592
Tejun Heoca235092005-11-01 17:23:49 +0900593 /*
594 * Some ioscheds (cfq) run q->request_fn directly, so
595 * rq cannot be accessed after calling
596 * elevator_add_req_fn.
597 */
598 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200599 break;
600
Tejun Heo797e7db2006-01-06 09:51:03 +0100601 case ELEVATOR_INSERT_REQUEUE:
602 /*
603 * If ordered flush isn't in progress, we do front
604 * insertion; otherwise, requests should be requeued
605 * in ordseq order.
606 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200607 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100608
Linas Vepstas95543172007-01-23 19:40:54 +0100609 /*
610 * Most requeues happen because of a busy condition,
611 * don't force unplug of the queue for that case.
612 */
613 unplug_it = 0;
614
Tejun Heo797e7db2006-01-06 09:51:03 +0100615 if (q->ordseq == 0) {
616 list_add(&rq->queuelist, &q->queue_head);
617 break;
618 }
619
620 ordseq = blk_ordered_req_seq(rq);
621
622 list_for_each(pos, &q->queue_head) {
623 struct request *pos_rq = list_entry_rq(pos);
624 if (ordseq <= blk_ordered_req_seq(pos_rq))
625 break;
626 }
627
628 list_add_tail(&rq->queuelist, pos);
629 break;
630
Tejun Heo8922e162005-10-20 16:23:44 +0200631 default:
632 printk(KERN_ERR "%s: bad insertion point %d\n",
633 __FUNCTION__, where);
634 BUG();
635 }
636
Jens Axboedac07ec2006-05-11 08:20:16 +0200637 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200638 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
639 - q->in_flight;
640
641 if (nrq >= q->unplug_thresh)
642 __generic_unplug_device(q);
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Tejun Heo30e96562006-02-08 01:01:31 -0800646void __elv_add_request(request_queue_t *q, struct request *rq, int where,
647 int plug)
648{
649 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200650 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800651
Jens Axboe4aff5e22006-08-10 08:44:47 +0200652 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800653 /*
654 * toggle ordered color
655 */
656 if (blk_barrier_rq(rq))
657 q->ordcolor ^= 1;
658
659 /*
660 * barriers implicitly indicate back insertion
661 */
662 if (where == ELEVATOR_INSERT_SORT)
663 where = ELEVATOR_INSERT_BACK;
664
665 /*
666 * this request is scheduling boundary, update
667 * end_sector
668 */
669 if (blk_fs_request(rq)) {
670 q->end_sector = rq_end_sector(rq);
671 q->boundary_rq = rq;
672 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200673 } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800674 where = ELEVATOR_INSERT_BACK;
675
676 if (plug)
677 blk_plug_device(q);
678
679 elv_insert(q, rq, where);
680}
681
Jens Axboe2e662b62006-07-13 11:55:04 +0200682EXPORT_SYMBOL(__elv_add_request);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684void elv_add_request(request_queue_t *q, struct request *rq, int where,
685 int plug)
686{
687 unsigned long flags;
688
689 spin_lock_irqsave(q->queue_lock, flags);
690 __elv_add_request(q, rq, where, plug);
691 spin_unlock_irqrestore(q->queue_lock, flags);
692}
693
Jens Axboe2e662b62006-07-13 11:55:04 +0200694EXPORT_SYMBOL(elv_add_request);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static inline struct request *__elv_next_request(request_queue_t *q)
697{
Tejun Heo8922e162005-10-20 16:23:44 +0200698 struct request *rq;
699
Tejun Heo797e7db2006-01-06 09:51:03 +0100700 while (1) {
701 while (!list_empty(&q->queue_head)) {
702 rq = list_entry_rq(q->queue_head.next);
703 if (blk_do_ordered(q, &rq))
704 return rq;
705 }
Tejun Heo8922e162005-10-20 16:23:44 +0200706
Tejun Heo797e7db2006-01-06 09:51:03 +0100707 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
708 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712struct request *elv_next_request(request_queue_t *q)
713{
714 struct request *rq;
715 int ret;
716
717 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200718 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200719 /*
720 * This is the first time the device driver
721 * sees this request (possibly after
722 * requeueing). Notify IO scheduler.
723 */
Jens Axboecad97512007-01-14 22:26:09 +1100724 if (blk_sorted_rq(rq))
725 elv_activate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200726
727 /*
728 * just mark as started even if we don't start
729 * it, a request that has been delayed should
730 * not be passed by new incoming requests
731 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200732 rq->cmd_flags |= REQ_STARTED;
Jens Axboe2056a782006-03-23 20:00:26 +0100733 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
Tejun Heo8922e162005-10-20 16:23:44 +0200734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Tejun Heo8922e162005-10-20 16:23:44 +0200736 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200737 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200738 q->boundary_rq = NULL;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jens Axboe4aff5e22006-08-10 08:44:47 +0200741 if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743
744 ret = q->prep_rq_fn(q, rq);
745 if (ret == BLKPREP_OK) {
746 break;
747 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500748 /*
749 * the request may have been (partially) prepped.
750 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200751 * avoid resource deadlock. REQ_STARTED will
752 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rq = NULL;
755 break;
756 } else if (ret == BLKPREP_KILL) {
757 int nr_bytes = rq->hard_nr_sectors << 9;
758
759 if (!nr_bytes)
760 nr_bytes = rq->data_len;
761
762 blkdev_dequeue_request(rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200763 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 end_that_request_chunk(rq, 0, nr_bytes);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100765 end_that_request_last(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 } else {
767 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
768 ret);
769 break;
770 }
771 }
772
773 return rq;
774}
775
Jens Axboe2e662b62006-07-13 11:55:04 +0200776EXPORT_SYMBOL(elv_next_request);
777
Tejun Heo8922e162005-10-20 16:23:44 +0200778void elv_dequeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Tejun Heo8922e162005-10-20 16:23:44 +0200780 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200781 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200782
783 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /*
786 * the time frame between a request being removed from the lists
787 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200788 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
790 if (blk_account_rq(rq))
791 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Jens Axboe2e662b62006-07-13 11:55:04 +0200794EXPORT_SYMBOL(elv_dequeue_request);
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796int elv_queue_empty(request_queue_t *q)
797{
798 elevator_t *e = q->elevator;
799
Tejun Heo8922e162005-10-20 16:23:44 +0200800 if (!list_empty(&q->queue_head))
801 return 0;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (e->ops->elevator_queue_empty_fn)
804 return e->ops->elevator_queue_empty_fn(q);
805
Tejun Heo8922e162005-10-20 16:23:44 +0200806 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Jens Axboe2e662b62006-07-13 11:55:04 +0200809EXPORT_SYMBOL(elv_queue_empty);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811struct request *elv_latter_request(request_queue_t *q, struct request *rq)
812{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 elevator_t *e = q->elevator;
814
815 if (e->ops->elevator_latter_req_fn)
816 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return NULL;
818}
819
820struct request *elv_former_request(request_queue_t *q, struct request *rq)
821{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 elevator_t *e = q->elevator;
823
824 if (e->ops->elevator_former_req_fn)
825 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return NULL;
827}
828
Jens Axboecb78b282006-07-28 09:32:57 +0200829int elv_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 elevator_t *e = q->elevator;
832
833 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200834 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 rq->elevator_private = NULL;
837 return 0;
838}
839
840void elv_put_request(request_queue_t *q, struct request *rq)
841{
842 elevator_t *e = q->elevator;
843
844 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100845 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Jens Axboecb78b282006-07-28 09:32:57 +0200848int elv_may_queue(request_queue_t *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 elevator_t *e = q->elevator;
851
852 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200853 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 return ELV_MQUEUE_MAY;
856}
857
858void elv_completed_request(request_queue_t *q, struct request *rq)
859{
860 elevator_t *e = q->elevator;
861
862 /*
863 * request is released from the driver, io must be done
864 */
Tejun Heo8922e162005-10-20 16:23:44 +0200865 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100867 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
868 e->ops->elevator_completed_req_fn(q, rq);
869 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100870
Tejun Heo1bc691d2006-01-12 15:39:26 +0100871 /*
872 * Check if the queue is waiting for fs requests to be
873 * drained for flush sequence.
874 */
875 if (unlikely(q->ordseq)) {
876 struct request *first_rq = list_entry_rq(q->queue_head.next);
877 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100878 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
879 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
880 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
881 q->request_fn(q);
882 }
Tejun Heo8922e162005-10-20 16:23:44 +0200883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Al Viro3d1ab402006-03-18 18:35:43 -0500886#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
887
888static ssize_t
889elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
890{
891 elevator_t *e = container_of(kobj, elevator_t, kobj);
892 struct elv_fs_entry *entry = to_elv(attr);
893 ssize_t error;
894
895 if (!entry->show)
896 return -EIO;
897
898 mutex_lock(&e->sysfs_lock);
899 error = e->ops ? entry->show(e, page) : -ENOENT;
900 mutex_unlock(&e->sysfs_lock);
901 return error;
902}
903
904static ssize_t
905elv_attr_store(struct kobject *kobj, struct attribute *attr,
906 const char *page, size_t length)
907{
908 elevator_t *e = container_of(kobj, elevator_t, kobj);
909 struct elv_fs_entry *entry = to_elv(attr);
910 ssize_t error;
911
912 if (!entry->store)
913 return -EIO;
914
915 mutex_lock(&e->sysfs_lock);
916 error = e->ops ? entry->store(e, page, length) : -ENOENT;
917 mutex_unlock(&e->sysfs_lock);
918 return error;
919}
920
921static struct sysfs_ops elv_sysfs_ops = {
922 .show = elv_attr_show,
923 .store = elv_attr_store,
924};
925
926static struct kobj_type elv_ktype = {
927 .sysfs_ops = &elv_sysfs_ops,
928 .release = elevator_release,
929};
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931int elv_register_queue(struct request_queue *q)
932{
933 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500934 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Al Viro3d1ab402006-03-18 18:35:43 -0500936 e->kobj.parent = &q->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Al Viro3d1ab402006-03-18 18:35:43 -0500938 error = kobject_add(&e->kobj);
939 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500940 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500941 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500942 while (attr->attr.name) {
943 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500944 break;
Al Viroe572ec72006-03-18 22:27:18 -0500945 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500946 }
947 }
948 kobject_uevent(&e->kobj, KOBJ_ADD);
949 }
950 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Jens Axboebc1c1162006-06-08 08:49:06 +0200953static void __elv_unregister_queue(elevator_t *e)
954{
955 kobject_uevent(&e->kobj, KOBJ_REMOVE);
956 kobject_del(&e->kobj);
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959void elv_unregister_queue(struct request_queue *q)
960{
Jens Axboebc1c1162006-06-08 08:49:06 +0200961 if (q)
962 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965int elv_register(struct elevator_type *e)
966{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100967 char *def = "";
Tejun Heo2824bc932005-10-20 10:56:41 +0200968 spin_lock_irq(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +0100969 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 list_add_tail(&e->list, &elv_list);
971 spin_unlock_irq(&elv_list_lock);
972
Nate Diller5f003972006-01-24 10:07:58 +0100973 if (!strcmp(e->elevator_name, chosen_elevator) ||
974 (!*chosen_elevator &&
975 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100976 def = " (default)";
977
978 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, def);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981EXPORT_SYMBOL_GPL(elv_register);
982
983void elv_unregister(struct elevator_type *e)
984{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800985 struct task_struct *g, *p;
986
987 /*
988 * Iterate every thread in the process to remove the io contexts.
989 */
Al Viroe17a9482006-03-18 13:21:20 -0500990 if (e->ops.trim) {
991 read_lock(&tasklist_lock);
992 do_each_thread(g, p) {
993 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +0400994 if (p->io_context)
995 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -0500996 task_unlock(p);
997 } while_each_thread(g, p);
998 read_unlock(&tasklist_lock);
999 }
Christoph Hellwig83521d32005-10-30 15:01:39 -08001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 spin_lock_irq(&elv_list_lock);
1002 list_del_init(&e->list);
1003 spin_unlock_irq(&elv_list_lock);
1004}
1005EXPORT_SYMBOL_GPL(elv_unregister);
1006
1007/*
1008 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1009 * we don't free the old io scheduler, before we have allocated what we
1010 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001011 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Al Viro3d1ab402006-03-18 18:35:43 -05001013static int elevator_switch(request_queue_t *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Tejun Heocb98fc82005-10-28 08:29:39 +02001015 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +02001016 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Tejun Heocb98fc82005-10-28 08:29:39 +02001018 /*
1019 * Allocate new elevator
1020 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001021 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -05001023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Jens Axboebc1c1162006-06-08 08:49:06 +02001025 data = elevator_init_queue(q, e);
1026 if (!data) {
1027 kobject_put(&e->kobj);
1028 return 0;
1029 }
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001032 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001034 spin_lock_irq(q->queue_lock);
1035
Jens Axboe64521d12005-10-28 08:30:39 +02001036 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001037
Tejun Heo15853af2005-11-10 08:52:05 +01001038 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001039
1040 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +01001041 blk_remove_plug(q);
1042 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001043 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001044 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001045 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001046 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001047 }
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001050 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 old_elevator = q->elevator;
1053
1054 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * attach and start new elevator
1056 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001057 elevator_attach(q, e, data);
1058
1059 spin_unlock_irq(q->queue_lock);
1060
1061 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (elv_register_queue(q))
1064 goto fail_register;
1065
1066 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001067 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
1069 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +02001070 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001071 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073fail_register:
1074 /*
1075 * switch failed, exit the new io scheduler and reattach the old
1076 * one again (along with re-adding the sysfs dir)
1077 */
1078 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 q->elevator = old_elevator;
1080 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +02001081 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
1086{
1087 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +01001088 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct elevator_type *e;
1090
Tejun Heobe561232005-11-10 08:55:01 +01001091 elevator_name[sizeof(elevator_name) - 1] = '\0';
1092 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1093 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Tejun Heobe561232005-11-10 08:55:01 +01001095 if (len && elevator_name[len - 1] == '\n')
1096 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 e = elevator_get(elevator_name);
1099 if (!e) {
1100 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1101 return -EINVAL;
1102 }
1103
Nate Diller2ca7d932005-10-30 15:02:24 -08001104 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1105 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Al Viro3d1ab402006-03-18 18:35:43 -05001109 if (!elevator_switch(q, e))
1110 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return count;
1112}
1113
1114ssize_t elv_iosched_show(request_queue_t *q, char *name)
1115{
1116 elevator_t *e = q->elevator;
1117 struct elevator_type *elv = e->elevator_type;
1118 struct list_head *entry;
1119 int len = 0;
1120
Vasily Tarasovc5841642006-10-11 13:26:30 +02001121 spin_lock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 list_for_each(entry, &elv_list) {
1123 struct elevator_type *__e;
1124
1125 __e = list_entry(entry, struct elevator_type, list);
1126 if (!strcmp(elv->elevator_name, __e->elevator_name))
1127 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1128 else
1129 len += sprintf(name+len, "%s ", __e->elevator_name);
1130 }
Vasily Tarasovc5841642006-10-11 13:26:30 +02001131 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 len += sprintf(len+name, "\n");
1134 return len;
1135}
1136
Jens Axboe2e662b62006-07-13 11:55:04 +02001137struct request *elv_rb_former_request(request_queue_t *q, struct request *rq)
1138{
1139 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1140
1141 if (rbprev)
1142 return rb_entry_rq(rbprev);
1143
1144 return NULL;
1145}
1146
1147EXPORT_SYMBOL(elv_rb_former_request);
1148
1149struct request *elv_rb_latter_request(request_queue_t *q, struct request *rq)
1150{
1151 struct rb_node *rbnext = rb_next(&rq->rb_node);
1152
1153 if (rbnext)
1154 return rb_entry_rq(rbnext);
1155
1156 return NULL;
1157}
1158
1159EXPORT_SYMBOL(elv_rb_latter_request);