blob: f9736fbdab0308f7121694df2faa3ffa68258994 [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{
Jens Axboe165125e2007-07-24 09:28:11 +020059 struct request_queue *q = rq->q;
Jens Axboeda775262006-12-20 11:04:12 +010060 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
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200116 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +0200117 if (!strcmp(e->elevator_name, name))
118 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Vasily Tarasova22b1692006-10-11 09:24:27 +0200121 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static void elevator_put(struct elevator_type *e)
125{
126 module_put(e->elevator_owner);
127}
128
129static struct elevator_type *elevator_get(const char *name)
130{
Tejun Heo2824bc932005-10-20 10:56:41 +0200131 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200133 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200134
135 e = elevator_find(name);
136 if (e && !try_module_get(e->elevator_owner))
137 e = NULL;
138
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200139 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return e;
142}
143
Jens Axboe165125e2007-07-24 09:28:11 +0200144static void *elevator_init_queue(struct request_queue *q,
145 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jens Axboebb37b942006-12-01 10:42:33 +0100147 return eq->ops->elevator_init_fn(q);
Jens Axboebc1c1162006-06-08 08:49:06 +0200148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jens Axboe165125e2007-07-24 09:28:11 +0200150static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
Jens Axboebc1c1162006-06-08 08:49:06 +0200151 void *data)
152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200154 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static char chosen_elevator[16];
158
Nate Diller5f003972006-01-24 10:07:58 +0100159static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100161 /*
162 * Be backwards-compatible with previous kernels, so users
163 * won't get the wrong elevator.
164 */
Nate Diller5f003972006-01-24 10:07:58 +0100165 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100166 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100167 else
Nate Diller5f003972006-01-24 10:07:58 +0100168 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800169 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172__setup("elevator=", elevator_setup);
173
Al Viro3d1ab402006-03-18 18:35:43 -0500174static struct kobj_type elv_ktype;
175
Jens Axboe165125e2007-07-24 09:28:11 +0200176static elevator_t *elevator_alloc(struct request_queue *q,
177 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500178{
Jens Axboe98170642006-07-28 09:23:08 +0200179 elevator_t *eq;
180 int i;
181
Christoph Lameter94f60302007-07-17 04:03:29 -0700182 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200183 if (unlikely(!eq))
184 goto err;
185
Jens Axboe98170642006-07-28 09:23:08 +0200186 eq->ops = &e->ops;
187 eq->elevator_type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700188 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200189 mutex_init(&eq->sysfs_lock);
190
Jens Axboeb5deef92006-07-19 23:39:40 +0200191 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
192 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200193 if (!eq->hash)
194 goto err;
195
196 for (i = 0; i < ELV_HASH_ENTRIES; i++)
197 INIT_HLIST_HEAD(&eq->hash[i]);
198
Al Viro3d1ab402006-03-18 18:35:43 -0500199 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200200err:
201 kfree(eq);
202 elevator_put(e);
203 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500204}
205
206static void elevator_release(struct kobject *kobj)
207{
208 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200209
Al Viro3d1ab402006-03-18 18:35:43 -0500210 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200211 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500212 kfree(e);
213}
214
Jens Axboe165125e2007-07-24 09:28:11 +0200215int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct elevator_type *e = NULL;
218 struct elevator_queue *eq;
219 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200220 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heocb98fc82005-10-28 08:29:39 +0200222 INIT_LIST_HEAD(&q->queue_head);
223 q->last_merge = NULL;
224 q->end_sector = 0;
225 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200226
Nate Diller5f003972006-01-24 10:07:58 +0100227 if (name && !(e = elevator_get(name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EINVAL;
229
Nate Diller248d5ca2006-01-24 10:09:14 +0100230 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
231 printk("I/O scheduler %s not found\n", chosen_elevator);
232
233 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
234 printk("Default I/O scheduler not found, using no-op\n");
235 e = elevator_get("noop");
Nate Diller5f003972006-01-24 10:07:58 +0100236 }
237
Jens Axboeb5deef92006-07-19 23:39:40 +0200238 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500239 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jens Axboebc1c1162006-06-08 08:49:06 +0200242 data = elevator_init_queue(q, eq);
243 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500244 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200245 return -ENOMEM;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jens Axboebc1c1162006-06-08 08:49:06 +0200248 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return ret;
250}
251
Jens Axboe2e662b62006-07-13 11:55:04 +0200252EXPORT_SYMBOL(elevator_init);
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254void elevator_exit(elevator_t *e)
255{
Al Viro3d1ab402006-03-18 18:35:43 -0500256 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (e->ops->elevator_exit_fn)
258 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500259 e->ops = NULL;
260 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Al Viro3d1ab402006-03-18 18:35:43 -0500262 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Jens Axboe2e662b62006-07-13 11:55:04 +0200265EXPORT_SYMBOL(elevator_exit);
266
Jens Axboe165125e2007-07-24 09:28:11 +0200267static void elv_activate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100268{
269 elevator_t *e = q->elevator;
270
271 if (e->ops->elevator_activate_req_fn)
272 e->ops->elevator_activate_req_fn(q, rq);
273}
274
Jens Axboe165125e2007-07-24 09:28:11 +0200275static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100276{
277 elevator_t *e = q->elevator;
278
279 if (e->ops->elevator_deactivate_req_fn)
280 e->ops->elevator_deactivate_req_fn(q, rq);
281}
282
Jens Axboe98170642006-07-28 09:23:08 +0200283static inline void __elv_rqhash_del(struct request *rq)
284{
285 hlist_del_init(&rq->hash);
286}
287
Jens Axboe165125e2007-07-24 09:28:11 +0200288static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200289{
290 if (ELV_ON_HASH(rq))
291 __elv_rqhash_del(rq);
292}
293
Jens Axboe165125e2007-07-24 09:28:11 +0200294static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200295{
296 elevator_t *e = q->elevator;
297
298 BUG_ON(ELV_ON_HASH(rq));
299 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
300}
301
Jens Axboe165125e2007-07-24 09:28:11 +0200302static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200303{
304 __elv_rqhash_del(rq);
305 elv_rqhash_add(q, rq);
306}
307
Jens Axboe165125e2007-07-24 09:28:11 +0200308static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200309{
310 elevator_t *e = q->elevator;
311 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
312 struct hlist_node *entry, *next;
313 struct request *rq;
314
315 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
316 BUG_ON(!ELV_ON_HASH(rq));
317
318 if (unlikely(!rq_mergeable(rq))) {
319 __elv_rqhash_del(rq);
320 continue;
321 }
322
323 if (rq_hash_key(rq) == offset)
324 return rq;
325 }
326
327 return NULL;
328}
329
Tejun Heo8922e162005-10-20 16:23:44 +0200330/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200331 * RB-tree support functions for inserting/lookup/removal of requests
332 * in a sorted RB tree.
333 */
334struct request *elv_rb_add(struct rb_root *root, struct request *rq)
335{
336 struct rb_node **p = &root->rb_node;
337 struct rb_node *parent = NULL;
338 struct request *__rq;
339
340 while (*p) {
341 parent = *p;
342 __rq = rb_entry(parent, struct request, rb_node);
343
344 if (rq->sector < __rq->sector)
345 p = &(*p)->rb_left;
346 else if (rq->sector > __rq->sector)
347 p = &(*p)->rb_right;
348 else
349 return __rq;
350 }
351
352 rb_link_node(&rq->rb_node, parent, p);
353 rb_insert_color(&rq->rb_node, root);
354 return NULL;
355}
356
357EXPORT_SYMBOL(elv_rb_add);
358
359void elv_rb_del(struct rb_root *root, struct request *rq)
360{
361 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
362 rb_erase(&rq->rb_node, root);
363 RB_CLEAR_NODE(&rq->rb_node);
364}
365
366EXPORT_SYMBOL(elv_rb_del);
367
368struct request *elv_rb_find(struct rb_root *root, sector_t sector)
369{
370 struct rb_node *n = root->rb_node;
371 struct request *rq;
372
373 while (n) {
374 rq = rb_entry(n, struct request, rb_node);
375
376 if (sector < rq->sector)
377 n = n->rb_left;
378 else if (sector > rq->sector)
379 n = n->rb_right;
380 else
381 return rq;
382 }
383
384 return NULL;
385}
386
387EXPORT_SYMBOL(elv_rb_find);
388
389/*
Tejun Heo8922e162005-10-20 16:23:44 +0200390 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200391 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200392 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200393 */
Jens Axboe165125e2007-07-24 09:28:11 +0200394void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200395{
396 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200397 struct list_head *entry;
398
Tejun Heo06b86242005-10-20 16:46:23 +0200399 if (q->last_merge == rq)
400 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200401
402 elv_rqhash_del(q, rq);
403
Tejun Heo15853af2005-11-10 08:52:05 +0100404 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200405
Jens Axboe1b47f532005-10-20 16:37:00 +0200406 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200407
Tejun Heo8922e162005-10-20 16:23:44 +0200408 list_for_each_prev(entry, &q->queue_head) {
409 struct request *pos = list_entry_rq(entry);
410
Jens Axboe783660b2007-01-19 11:27:47 +1100411 if (rq_data_dir(rq) != rq_data_dir(pos))
412 break;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200413 if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
Tejun Heo8922e162005-10-20 16:23:44 +0200414 break;
415 if (rq->sector >= boundary) {
416 if (pos->sector < boundary)
417 continue;
418 } else {
419 if (pos->sector >= boundary)
420 break;
421 }
422 if (rq->sector >= pos->sector)
423 break;
424 }
425
426 list_add(&rq->queuelist, entry);
427}
428
Jens Axboe2e662b62006-07-13 11:55:04 +0200429EXPORT_SYMBOL(elv_dispatch_sort);
430
Jens Axboe98170642006-07-28 09:23:08 +0200431/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200432 * Insert rq into dispatch queue of q. Queue lock must be held on
433 * entry. rq is added to the back of the dispatch queue. To be used by
434 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200435 */
436void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
437{
438 if (q->last_merge == rq)
439 q->last_merge = NULL;
440
441 elv_rqhash_del(q, rq);
442
443 q->nr_sorted--;
444
445 q->end_sector = rq_end_sector(rq);
446 q->boundary_rq = rq;
447 list_add_tail(&rq->queuelist, &q->queue_head);
448}
449
Jens Axboe2e662b62006-07-13 11:55:04 +0200450EXPORT_SYMBOL(elv_dispatch_add_tail);
451
Jens Axboe165125e2007-07-24 09:28:11 +0200452int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200455 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200456 int ret;
457
Jens Axboe98170642006-07-28 09:23:08 +0200458 /*
459 * First try one-hit cache.
460 */
Tejun Heo06b86242005-10-20 16:46:23 +0200461 if (q->last_merge) {
462 ret = elv_try_merge(q->last_merge, bio);
463 if (ret != ELEVATOR_NO_MERGE) {
464 *req = q->last_merge;
465 return ret;
466 }
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jens Axboe98170642006-07-28 09:23:08 +0200469 /*
470 * See if our hash lookup can find a potential backmerge.
471 */
472 __rq = elv_rqhash_find(q, bio->bi_sector);
473 if (__rq && elv_rq_merge_ok(__rq, bio)) {
474 *req = __rq;
475 return ELEVATOR_BACK_MERGE;
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (e->ops->elevator_merge_fn)
479 return e->ops->elevator_merge_fn(q, req, bio);
480
481 return ELEVATOR_NO_MERGE;
482}
483
Jens Axboe165125e2007-07-24 09:28:11 +0200484void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 elevator_t *e = q->elevator;
487
488 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200489 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200490
Jens Axboe2e662b62006-07-13 11:55:04 +0200491 if (type == ELEVATOR_BACK_MERGE)
492 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200493
Tejun Heo06b86242005-10-20 16:46:23 +0200494 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Jens Axboe165125e2007-07-24 09:28:11 +0200497void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct request *next)
499{
500 elevator_t *e = q->elevator;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (e->ops->elevator_merge_req_fn)
503 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200504
Jens Axboe98170642006-07-28 09:23:08 +0200505 elv_rqhash_reposition(q, rq);
506 elv_rqhash_del(q, next);
507
508 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200509 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Jens Axboe165125e2007-07-24 09:28:11 +0200512void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * it already went through dequeue, we need to decrement the
516 * in_flight count again
517 */
Tejun Heo8922e162005-10-20 16:23:44 +0200518 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 q->in_flight--;
Jens Axboecad97512007-01-14 22:26:09 +1100520 if (blk_sorted_rq(rq))
521 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Jens Axboe4aff5e22006-08-10 08:44:47 +0200524 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Tejun Heo30e96562006-02-08 01:01:31 -0800526 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Jens Axboe165125e2007-07-24 09:28:11 +0200529static void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100530{
531 static int printed;
532 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
533 ;
534 if (q->nr_sorted == 0)
535 return;
536 if (printed++ < 10) {
537 printk(KERN_ERR "%s: forced dispatching is broken "
538 "(nr_sorted=%u), please report this\n",
539 q->elevator->elevator_type->elevator_name, q->nr_sorted);
540 }
541}
542
Jens Axboe165125e2007-07-24 09:28:11 +0200543void elv_insert(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Tejun Heo797e7db2006-01-06 09:51:03 +0100545 struct list_head *pos;
546 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200547 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100548
Jens Axboe2056a782006-03-23 20:00:26 +0100549 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rq->q = q;
552
Tejun Heo8922e162005-10-20 16:23:44 +0200553 switch (where) {
554 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200555 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200556
557 list_add(&rq->queuelist, &q->queue_head);
558 break;
559
560 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200561 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100562 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200563 list_add_tail(&rq->queuelist, &q->queue_head);
564 /*
565 * We kick the queue here for the following reasons.
566 * - The elevator might have returned NULL previously
567 * to delay requests and returned them now. As the
568 * queue wasn't empty before this request, ll_rw_blk
569 * won't run the queue on return, resulting in hang.
570 * - Usually, back inserted requests won't be merged
571 * with anything. There's no point in delaying queue
572 * processing.
573 */
574 blk_remove_plug(q);
575 q->request_fn(q);
576 break;
577
578 case ELEVATOR_INSERT_SORT:
579 BUG_ON(!blk_fs_request(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200580 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100581 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200582 if (rq_mergeable(rq)) {
583 elv_rqhash_add(q, rq);
584 if (!q->last_merge)
585 q->last_merge = rq;
586 }
587
Tejun Heoca235092005-11-01 17:23:49 +0900588 /*
589 * Some ioscheds (cfq) run q->request_fn directly, so
590 * rq cannot be accessed after calling
591 * elevator_add_req_fn.
592 */
593 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200594 break;
595
Tejun Heo797e7db2006-01-06 09:51:03 +0100596 case ELEVATOR_INSERT_REQUEUE:
597 /*
598 * If ordered flush isn't in progress, we do front
599 * insertion; otherwise, requests should be requeued
600 * in ordseq order.
601 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200602 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100603
Linas Vepstas95543172007-01-23 19:40:54 +0100604 /*
605 * Most requeues happen because of a busy condition,
606 * don't force unplug of the queue for that case.
607 */
608 unplug_it = 0;
609
Tejun Heo797e7db2006-01-06 09:51:03 +0100610 if (q->ordseq == 0) {
611 list_add(&rq->queuelist, &q->queue_head);
612 break;
613 }
614
615 ordseq = blk_ordered_req_seq(rq);
616
617 list_for_each(pos, &q->queue_head) {
618 struct request *pos_rq = list_entry_rq(pos);
619 if (ordseq <= blk_ordered_req_seq(pos_rq))
620 break;
621 }
622
623 list_add_tail(&rq->queuelist, pos);
624 break;
625
Tejun Heo8922e162005-10-20 16:23:44 +0200626 default:
627 printk(KERN_ERR "%s: bad insertion point %d\n",
628 __FUNCTION__, where);
629 BUG();
630 }
631
Jens Axboedac07ec2006-05-11 08:20:16 +0200632 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200633 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
634 - q->in_flight;
635
636 if (nrq >= q->unplug_thresh)
637 __generic_unplug_device(q);
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Jens Axboe165125e2007-07-24 09:28:11 +0200641void __elv_add_request(struct request_queue *q, struct request *rq, int where,
Tejun Heo30e96562006-02-08 01:01:31 -0800642 int plug)
643{
644 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200645 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800646
Jens Axboe4aff5e22006-08-10 08:44:47 +0200647 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800648 /*
649 * toggle ordered color
650 */
651 if (blk_barrier_rq(rq))
652 q->ordcolor ^= 1;
653
654 /*
655 * barriers implicitly indicate back insertion
656 */
657 if (where == ELEVATOR_INSERT_SORT)
658 where = ELEVATOR_INSERT_BACK;
659
660 /*
661 * this request is scheduling boundary, update
662 * end_sector
663 */
664 if (blk_fs_request(rq)) {
665 q->end_sector = rq_end_sector(rq);
666 q->boundary_rq = rq;
667 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200668 } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800669 where = ELEVATOR_INSERT_BACK;
670
671 if (plug)
672 blk_plug_device(q);
673
674 elv_insert(q, rq, where);
675}
676
Jens Axboe2e662b62006-07-13 11:55:04 +0200677EXPORT_SYMBOL(__elv_add_request);
678
Jens Axboe165125e2007-07-24 09:28:11 +0200679void elv_add_request(struct request_queue *q, struct request *rq, int where,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int plug)
681{
682 unsigned long flags;
683
684 spin_lock_irqsave(q->queue_lock, flags);
685 __elv_add_request(q, rq, where, plug);
686 spin_unlock_irqrestore(q->queue_lock, flags);
687}
688
Jens Axboe2e662b62006-07-13 11:55:04 +0200689EXPORT_SYMBOL(elv_add_request);
690
Jens Axboe165125e2007-07-24 09:28:11 +0200691static inline struct request *__elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Tejun Heo8922e162005-10-20 16:23:44 +0200693 struct request *rq;
694
Tejun Heo797e7db2006-01-06 09:51:03 +0100695 while (1) {
696 while (!list_empty(&q->queue_head)) {
697 rq = list_entry_rq(q->queue_head.next);
698 if (blk_do_ordered(q, &rq))
699 return rq;
700 }
Tejun Heo8922e162005-10-20 16:23:44 +0200701
Tejun Heo797e7db2006-01-06 09:51:03 +0100702 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
703 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Jens Axboe165125e2007-07-24 09:28:11 +0200707struct request *elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct request *rq;
710 int ret;
711
712 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200713 /*
714 * Kill the empty barrier place holder, the driver must
715 * not ever see it.
716 */
717 if (blk_empty_barrier(rq)) {
718 end_queued_request(rq, 1);
719 continue;
720 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200721 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200722 /*
723 * This is the first time the device driver
724 * sees this request (possibly after
725 * requeueing). Notify IO scheduler.
726 */
Jens Axboecad97512007-01-14 22:26:09 +1100727 if (blk_sorted_rq(rq))
728 elv_activate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200729
730 /*
731 * just mark as started even if we don't start
732 * it, a request that has been delayed should
733 * not be passed by new incoming requests
734 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200735 rq->cmd_flags |= REQ_STARTED;
Jens Axboe2056a782006-03-23 20:00:26 +0100736 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
Tejun Heo8922e162005-10-20 16:23:44 +0200737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Tejun Heo8922e162005-10-20 16:23:44 +0200739 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200740 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200741 q->boundary_rq = NULL;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jens Axboe4aff5e22006-08-10 08:44:47 +0200744 if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746
747 ret = q->prep_rq_fn(q, rq);
748 if (ret == BLKPREP_OK) {
749 break;
750 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500751 /*
752 * the request may have been (partially) prepped.
753 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200754 * avoid resource deadlock. REQ_STARTED will
755 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rq = NULL;
758 break;
759 } else if (ret == BLKPREP_KILL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200760 rq->cmd_flags |= REQ_QUIET;
Jens Axboea0cd1282007-09-21 10:41:07 +0200761 end_queued_request(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else {
763 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
764 ret);
765 break;
766 }
767 }
768
769 return rq;
770}
771
Jens Axboe2e662b62006-07-13 11:55:04 +0200772EXPORT_SYMBOL(elv_next_request);
773
Jens Axboe165125e2007-07-24 09:28:11 +0200774void elv_dequeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Tejun Heo8922e162005-10-20 16:23:44 +0200776 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200777 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200778
779 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /*
782 * the time frame between a request being removed from the lists
783 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200784 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
786 if (blk_account_rq(rq))
787 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Jens Axboe2e662b62006-07-13 11:55:04 +0200790EXPORT_SYMBOL(elv_dequeue_request);
791
Jens Axboe165125e2007-07-24 09:28:11 +0200792int elv_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 elevator_t *e = q->elevator;
795
Tejun Heo8922e162005-10-20 16:23:44 +0200796 if (!list_empty(&q->queue_head))
797 return 0;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (e->ops->elevator_queue_empty_fn)
800 return e->ops->elevator_queue_empty_fn(q);
801
Tejun Heo8922e162005-10-20 16:23:44 +0200802 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Jens Axboe2e662b62006-07-13 11:55:04 +0200805EXPORT_SYMBOL(elv_queue_empty);
806
Jens Axboe165125e2007-07-24 09:28:11 +0200807struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 elevator_t *e = q->elevator;
810
811 if (e->ops->elevator_latter_req_fn)
812 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return NULL;
814}
815
Jens Axboe165125e2007-07-24 09:28:11 +0200816struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 elevator_t *e = q->elevator;
819
820 if (e->ops->elevator_former_req_fn)
821 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return NULL;
823}
824
Jens Axboe165125e2007-07-24 09:28:11 +0200825int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 elevator_t *e = q->elevator;
828
829 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200830 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 rq->elevator_private = NULL;
833 return 0;
834}
835
Jens Axboe165125e2007-07-24 09:28:11 +0200836void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 elevator_t *e = q->elevator;
839
840 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100841 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Jens Axboe165125e2007-07-24 09:28:11 +0200844int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 elevator_t *e = q->elevator;
847
848 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200849 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 return ELV_MQUEUE_MAY;
852}
853
Jens Axboe165125e2007-07-24 09:28:11 +0200854void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 elevator_t *e = q->elevator;
857
858 /*
859 * request is released from the driver, io must be done
860 */
Tejun Heo8922e162005-10-20 16:23:44 +0200861 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100863 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
864 e->ops->elevator_completed_req_fn(q, rq);
865 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100866
Tejun Heo1bc691d2006-01-12 15:39:26 +0100867 /*
868 * Check if the queue is waiting for fs requests to be
869 * drained for flush sequence.
870 */
871 if (unlikely(q->ordseq)) {
872 struct request *first_rq = list_entry_rq(q->queue_head.next);
873 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100874 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
875 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
876 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
877 q->request_fn(q);
878 }
Tejun Heo8922e162005-10-20 16:23:44 +0200879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Al Viro3d1ab402006-03-18 18:35:43 -0500882#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
883
884static ssize_t
885elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
886{
887 elevator_t *e = container_of(kobj, elevator_t, kobj);
888 struct elv_fs_entry *entry = to_elv(attr);
889 ssize_t error;
890
891 if (!entry->show)
892 return -EIO;
893
894 mutex_lock(&e->sysfs_lock);
895 error = e->ops ? entry->show(e, page) : -ENOENT;
896 mutex_unlock(&e->sysfs_lock);
897 return error;
898}
899
900static ssize_t
901elv_attr_store(struct kobject *kobj, struct attribute *attr,
902 const char *page, size_t length)
903{
904 elevator_t *e = container_of(kobj, elevator_t, kobj);
905 struct elv_fs_entry *entry = to_elv(attr);
906 ssize_t error;
907
908 if (!entry->store)
909 return -EIO;
910
911 mutex_lock(&e->sysfs_lock);
912 error = e->ops ? entry->store(e, page, length) : -ENOENT;
913 mutex_unlock(&e->sysfs_lock);
914 return error;
915}
916
917static struct sysfs_ops elv_sysfs_ops = {
918 .show = elv_attr_show,
919 .store = elv_attr_store,
920};
921
922static struct kobj_type elv_ktype = {
923 .sysfs_ops = &elv_sysfs_ops,
924 .release = elevator_release,
925};
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927int elv_register_queue(struct request_queue *q)
928{
929 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500930 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700932 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500933 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500934 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500935 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500936 while (attr->attr.name) {
937 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500938 break;
Al Viroe572ec72006-03-18 22:27:18 -0500939 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500940 }
941 }
942 kobject_uevent(&e->kobj, KOBJ_ADD);
943 }
944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Jens Axboebc1c1162006-06-08 08:49:06 +0200947static void __elv_unregister_queue(elevator_t *e)
948{
949 kobject_uevent(&e->kobj, KOBJ_REMOVE);
950 kobject_del(&e->kobj);
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void elv_unregister_queue(struct request_queue *q)
954{
Jens Axboebc1c1162006-06-08 08:49:06 +0200955 if (q)
956 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Adrian Bunk2fdd82b2007-12-12 18:51:56 +0100959void elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100961 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200962
963 spin_lock(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +0100964 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200966 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Nate Diller5f003972006-01-24 10:07:58 +0100968 if (!strcmp(e->elevator_name, chosen_elevator) ||
969 (!*chosen_elevator &&
970 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100971 def = " (default)";
972
973 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, def);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975EXPORT_SYMBOL_GPL(elv_register);
976
977void elv_unregister(struct elevator_type *e)
978{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800979 struct task_struct *g, *p;
980
981 /*
982 * Iterate every thread in the process to remove the io contexts.
983 */
Al Viroe17a9482006-03-18 13:21:20 -0500984 if (e->ops.trim) {
985 read_lock(&tasklist_lock);
986 do_each_thread(g, p) {
987 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +0400988 if (p->io_context)
989 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -0500990 task_unlock(p);
991 } while_each_thread(g, p);
992 read_unlock(&tasklist_lock);
993 }
Christoph Hellwig83521d32005-10-30 15:01:39 -0800994
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200995 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200997 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999EXPORT_SYMBOL_GPL(elv_unregister);
1000
1001/*
1002 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1003 * we don't free the old io scheduler, before we have allocated what we
1004 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001005 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Jens Axboe165125e2007-07-24 09:28:11 +02001007static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Tejun Heocb98fc82005-10-28 08:29:39 +02001009 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +02001010 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Tejun Heocb98fc82005-10-28 08:29:39 +02001012 /*
1013 * Allocate new elevator
1014 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001015 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -05001017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Jens Axboebc1c1162006-06-08 08:49:06 +02001019 data = elevator_init_queue(q, e);
1020 if (!data) {
1021 kobject_put(&e->kobj);
1022 return 0;
1023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001026 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001028 spin_lock_irq(q->queue_lock);
1029
Jens Axboe64521d12005-10-28 08:30:39 +02001030 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001031
Tejun Heo15853af2005-11-10 08:52:05 +01001032 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001033
1034 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +01001035 blk_remove_plug(q);
1036 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001037 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001038 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001039 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001040 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001041 }
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001044 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 old_elevator = q->elevator;
1047
1048 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * attach and start new elevator
1050 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001051 elevator_attach(q, e, data);
1052
1053 spin_unlock_irq(q->queue_lock);
1054
1055 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (elv_register_queue(q))
1058 goto fail_register;
1059
1060 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001061 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
1063 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +02001064 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001065 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067fail_register:
1068 /*
1069 * switch failed, exit the new io scheduler and reattach the old
1070 * one again (along with re-adding the sysfs dir)
1071 */
1072 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 q->elevator = old_elevator;
1074 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +02001075 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Jens Axboe165125e2007-07-24 09:28:11 +02001079ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1080 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +01001083 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct elevator_type *e;
1085
Tejun Heobe561232005-11-10 08:55:01 +01001086 elevator_name[sizeof(elevator_name) - 1] = '\0';
1087 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1088 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Tejun Heobe561232005-11-10 08:55:01 +01001090 if (len && elevator_name[len - 1] == '\n')
1091 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 e = elevator_get(elevator_name);
1094 if (!e) {
1095 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1096 return -EINVAL;
1097 }
1098
Nate Diller2ca7d932005-10-30 15:02:24 -08001099 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1100 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Al Viro3d1ab402006-03-18 18:35:43 -05001104 if (!elevator_switch(q, e))
1105 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return count;
1107}
1108
Jens Axboe165125e2007-07-24 09:28:11 +02001109ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 elevator_t *e = q->elevator;
1112 struct elevator_type *elv = e->elevator_type;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001113 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int len = 0;
1115
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001116 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001117 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!strcmp(elv->elevator_name, __e->elevator_name))
1119 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1120 else
1121 len += sprintf(name+len, "%s ", __e->elevator_name);
1122 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001123 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 len += sprintf(len+name, "\n");
1126 return len;
1127}
1128
Jens Axboe165125e2007-07-24 09:28:11 +02001129struct request *elv_rb_former_request(struct request_queue *q,
1130 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001131{
1132 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1133
1134 if (rbprev)
1135 return rb_entry_rq(rbprev);
1136
1137 return NULL;
1138}
1139
1140EXPORT_SYMBOL(elv_rb_former_request);
1141
Jens Axboe165125e2007-07-24 09:28:11 +02001142struct request *elv_rb_latter_request(struct request_queue *q,
1143 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001144{
1145 struct rb_node *rbnext = rb_next(&rq->rb_node);
1146
1147 if (rbnext)
1148 return rb_entry_rq(rbnext);
1149
1150 return NULL;
1151}
1152
1153EXPORT_SYMBOL(elv_rb_latter_request);