blob: c6d153de9fd68ba33fcfb9cc65c00deb35623e8b [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;
188 kobject_init(&eq->kobj);
189 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
190 eq->kobj.ktype = &elv_ktype;
191 mutex_init(&eq->sysfs_lock);
192
Jens Axboeb5deef92006-07-19 23:39:40 +0200193 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
194 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200195 if (!eq->hash)
196 goto err;
197
198 for (i = 0; i < ELV_HASH_ENTRIES; i++)
199 INIT_HLIST_HEAD(&eq->hash[i]);
200
Al Viro3d1ab402006-03-18 18:35:43 -0500201 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200202err:
203 kfree(eq);
204 elevator_put(e);
205 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500206}
207
208static void elevator_release(struct kobject *kobj)
209{
210 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200211
Al Viro3d1ab402006-03-18 18:35:43 -0500212 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200213 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500214 kfree(e);
215}
216
Jens Axboe165125e2007-07-24 09:28:11 +0200217int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct elevator_type *e = NULL;
220 struct elevator_queue *eq;
221 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200222 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heocb98fc82005-10-28 08:29:39 +0200224 INIT_LIST_HEAD(&q->queue_head);
225 q->last_merge = NULL;
226 q->end_sector = 0;
227 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200228
Nate Diller5f003972006-01-24 10:07:58 +0100229 if (name && !(e = elevator_get(name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EINVAL;
231
Nate Diller248d5ca2006-01-24 10:09:14 +0100232 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
233 printk("I/O scheduler %s not found\n", chosen_elevator);
234
235 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
236 printk("Default I/O scheduler not found, using no-op\n");
237 e = elevator_get("noop");
Nate Diller5f003972006-01-24 10:07:58 +0100238 }
239
Jens Axboeb5deef92006-07-19 23:39:40 +0200240 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500241 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Jens Axboebc1c1162006-06-08 08:49:06 +0200244 data = elevator_init_queue(q, eq);
245 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500246 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200247 return -ENOMEM;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jens Axboebc1c1162006-06-08 08:49:06 +0200250 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return ret;
252}
253
Jens Axboe2e662b62006-07-13 11:55:04 +0200254EXPORT_SYMBOL(elevator_init);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256void elevator_exit(elevator_t *e)
257{
Al Viro3d1ab402006-03-18 18:35:43 -0500258 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (e->ops->elevator_exit_fn)
260 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500261 e->ops = NULL;
262 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Al Viro3d1ab402006-03-18 18:35:43 -0500264 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Jens Axboe2e662b62006-07-13 11:55:04 +0200267EXPORT_SYMBOL(elevator_exit);
268
Jens Axboe165125e2007-07-24 09:28:11 +0200269static void elv_activate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100270{
271 elevator_t *e = q->elevator;
272
273 if (e->ops->elevator_activate_req_fn)
274 e->ops->elevator_activate_req_fn(q, rq);
275}
276
Jens Axboe165125e2007-07-24 09:28:11 +0200277static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100278{
279 elevator_t *e = q->elevator;
280
281 if (e->ops->elevator_deactivate_req_fn)
282 e->ops->elevator_deactivate_req_fn(q, rq);
283}
284
Jens Axboe98170642006-07-28 09:23:08 +0200285static inline void __elv_rqhash_del(struct request *rq)
286{
287 hlist_del_init(&rq->hash);
288}
289
Jens Axboe165125e2007-07-24 09:28:11 +0200290static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200291{
292 if (ELV_ON_HASH(rq))
293 __elv_rqhash_del(rq);
294}
295
Jens Axboe165125e2007-07-24 09:28:11 +0200296static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200297{
298 elevator_t *e = q->elevator;
299
300 BUG_ON(ELV_ON_HASH(rq));
301 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
302}
303
Jens Axboe165125e2007-07-24 09:28:11 +0200304static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200305{
306 __elv_rqhash_del(rq);
307 elv_rqhash_add(q, rq);
308}
309
Jens Axboe165125e2007-07-24 09:28:11 +0200310static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200311{
312 elevator_t *e = q->elevator;
313 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
314 struct hlist_node *entry, *next;
315 struct request *rq;
316
317 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
318 BUG_ON(!ELV_ON_HASH(rq));
319
320 if (unlikely(!rq_mergeable(rq))) {
321 __elv_rqhash_del(rq);
322 continue;
323 }
324
325 if (rq_hash_key(rq) == offset)
326 return rq;
327 }
328
329 return NULL;
330}
331
Tejun Heo8922e162005-10-20 16:23:44 +0200332/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200333 * RB-tree support functions for inserting/lookup/removal of requests
334 * in a sorted RB tree.
335 */
336struct request *elv_rb_add(struct rb_root *root, struct request *rq)
337{
338 struct rb_node **p = &root->rb_node;
339 struct rb_node *parent = NULL;
340 struct request *__rq;
341
342 while (*p) {
343 parent = *p;
344 __rq = rb_entry(parent, struct request, rb_node);
345
346 if (rq->sector < __rq->sector)
347 p = &(*p)->rb_left;
348 else if (rq->sector > __rq->sector)
349 p = &(*p)->rb_right;
350 else
351 return __rq;
352 }
353
354 rb_link_node(&rq->rb_node, parent, p);
355 rb_insert_color(&rq->rb_node, root);
356 return NULL;
357}
358
359EXPORT_SYMBOL(elv_rb_add);
360
361void elv_rb_del(struct rb_root *root, struct request *rq)
362{
363 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
364 rb_erase(&rq->rb_node, root);
365 RB_CLEAR_NODE(&rq->rb_node);
366}
367
368EXPORT_SYMBOL(elv_rb_del);
369
370struct request *elv_rb_find(struct rb_root *root, sector_t sector)
371{
372 struct rb_node *n = root->rb_node;
373 struct request *rq;
374
375 while (n) {
376 rq = rb_entry(n, struct request, rb_node);
377
378 if (sector < rq->sector)
379 n = n->rb_left;
380 else if (sector > rq->sector)
381 n = n->rb_right;
382 else
383 return rq;
384 }
385
386 return NULL;
387}
388
389EXPORT_SYMBOL(elv_rb_find);
390
391/*
Tejun Heo8922e162005-10-20 16:23:44 +0200392 * Insert rq into dispatch queue of q. Queue lock must be held on
Jens Axboe2e662b62006-07-13 11:55:04 +0200393 * entry. rq is sort insted into the dispatch queue. To be used by
394 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200395 */
Jens Axboe165125e2007-07-24 09:28:11 +0200396void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200397{
398 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200399 struct list_head *entry;
400
Tejun Heo06b86242005-10-20 16:46:23 +0200401 if (q->last_merge == rq)
402 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200403
404 elv_rqhash_del(q, rq);
405
Tejun Heo15853af2005-11-10 08:52:05 +0100406 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200407
Jens Axboe1b47f532005-10-20 16:37:00 +0200408 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200409
Tejun Heo8922e162005-10-20 16:23:44 +0200410 list_for_each_prev(entry, &q->queue_head) {
411 struct request *pos = list_entry_rq(entry);
412
Jens Axboe783660b2007-01-19 11:27:47 +1100413 if (rq_data_dir(rq) != rq_data_dir(pos))
414 break;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200415 if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
Tejun Heo8922e162005-10-20 16:23:44 +0200416 break;
417 if (rq->sector >= boundary) {
418 if (pos->sector < boundary)
419 continue;
420 } else {
421 if (pos->sector >= boundary)
422 break;
423 }
424 if (rq->sector >= pos->sector)
425 break;
426 }
427
428 list_add(&rq->queuelist, entry);
429}
430
Jens Axboe2e662b62006-07-13 11:55:04 +0200431EXPORT_SYMBOL(elv_dispatch_sort);
432
Jens Axboe98170642006-07-28 09:23:08 +0200433/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200434 * Insert rq into dispatch queue of q. Queue lock must be held on
435 * entry. rq is added to the back of the dispatch queue. To be used by
436 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200437 */
438void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
439{
440 if (q->last_merge == rq)
441 q->last_merge = NULL;
442
443 elv_rqhash_del(q, rq);
444
445 q->nr_sorted--;
446
447 q->end_sector = rq_end_sector(rq);
448 q->boundary_rq = rq;
449 list_add_tail(&rq->queuelist, &q->queue_head);
450}
451
Jens Axboe2e662b62006-07-13 11:55:04 +0200452EXPORT_SYMBOL(elv_dispatch_add_tail);
453
Jens Axboe165125e2007-07-24 09:28:11 +0200454int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200457 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200458 int ret;
459
Jens Axboe98170642006-07-28 09:23:08 +0200460 /*
461 * First try one-hit cache.
462 */
Tejun Heo06b86242005-10-20 16:46:23 +0200463 if (q->last_merge) {
464 ret = elv_try_merge(q->last_merge, bio);
465 if (ret != ELEVATOR_NO_MERGE) {
466 *req = q->last_merge;
467 return ret;
468 }
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jens Axboe98170642006-07-28 09:23:08 +0200471 /*
472 * See if our hash lookup can find a potential backmerge.
473 */
474 __rq = elv_rqhash_find(q, bio->bi_sector);
475 if (__rq && elv_rq_merge_ok(__rq, bio)) {
476 *req = __rq;
477 return ELEVATOR_BACK_MERGE;
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (e->ops->elevator_merge_fn)
481 return e->ops->elevator_merge_fn(q, req, bio);
482
483 return ELEVATOR_NO_MERGE;
484}
485
Jens Axboe165125e2007-07-24 09:28:11 +0200486void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 elevator_t *e = q->elevator;
489
490 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200491 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200492
Jens Axboe2e662b62006-07-13 11:55:04 +0200493 if (type == ELEVATOR_BACK_MERGE)
494 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200495
Tejun Heo06b86242005-10-20 16:46:23 +0200496 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Jens Axboe165125e2007-07-24 09:28:11 +0200499void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct request *next)
501{
502 elevator_t *e = q->elevator;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (e->ops->elevator_merge_req_fn)
505 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200506
Jens Axboe98170642006-07-28 09:23:08 +0200507 elv_rqhash_reposition(q, rq);
508 elv_rqhash_del(q, next);
509
510 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200511 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Jens Axboe165125e2007-07-24 09:28:11 +0200514void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * it already went through dequeue, we need to decrement the
518 * in_flight count again
519 */
Tejun Heo8922e162005-10-20 16:23:44 +0200520 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 q->in_flight--;
Jens Axboecad97512007-01-14 22:26:09 +1100522 if (blk_sorted_rq(rq))
523 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jens Axboe4aff5e22006-08-10 08:44:47 +0200526 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Tejun Heo30e96562006-02-08 01:01:31 -0800528 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Jens Axboe165125e2007-07-24 09:28:11 +0200531static void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100532{
533 static int printed;
534 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
535 ;
536 if (q->nr_sorted == 0)
537 return;
538 if (printed++ < 10) {
539 printk(KERN_ERR "%s: forced dispatching is broken "
540 "(nr_sorted=%u), please report this\n",
541 q->elevator->elevator_type->elevator_name, q->nr_sorted);
542 }
543}
544
Jens Axboe165125e2007-07-24 09:28:11 +0200545void elv_insert(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Tejun Heo797e7db2006-01-06 09:51:03 +0100547 struct list_head *pos;
548 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200549 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100550
Jens Axboe2056a782006-03-23 20:00:26 +0100551 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 rq->q = q;
554
Tejun Heo8922e162005-10-20 16:23:44 +0200555 switch (where) {
556 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200557 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200558
559 list_add(&rq->queuelist, &q->queue_head);
560 break;
561
562 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200563 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100564 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200565 list_add_tail(&rq->queuelist, &q->queue_head);
566 /*
567 * We kick the queue here for the following reasons.
568 * - The elevator might have returned NULL previously
569 * to delay requests and returned them now. As the
570 * queue wasn't empty before this request, ll_rw_blk
571 * won't run the queue on return, resulting in hang.
572 * - Usually, back inserted requests won't be merged
573 * with anything. There's no point in delaying queue
574 * processing.
575 */
576 blk_remove_plug(q);
577 q->request_fn(q);
578 break;
579
580 case ELEVATOR_INSERT_SORT:
581 BUG_ON(!blk_fs_request(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200582 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100583 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200584 if (rq_mergeable(rq)) {
585 elv_rqhash_add(q, rq);
586 if (!q->last_merge)
587 q->last_merge = rq;
588 }
589
Tejun Heoca235092005-11-01 17:23:49 +0900590 /*
591 * Some ioscheds (cfq) run q->request_fn directly, so
592 * rq cannot be accessed after calling
593 * elevator_add_req_fn.
594 */
595 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200596 break;
597
Tejun Heo797e7db2006-01-06 09:51:03 +0100598 case ELEVATOR_INSERT_REQUEUE:
599 /*
600 * If ordered flush isn't in progress, we do front
601 * insertion; otherwise, requests should be requeued
602 * in ordseq order.
603 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200604 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100605
Linas Vepstas95543172007-01-23 19:40:54 +0100606 /*
607 * Most requeues happen because of a busy condition,
608 * don't force unplug of the queue for that case.
609 */
610 unplug_it = 0;
611
Tejun Heo797e7db2006-01-06 09:51:03 +0100612 if (q->ordseq == 0) {
613 list_add(&rq->queuelist, &q->queue_head);
614 break;
615 }
616
617 ordseq = blk_ordered_req_seq(rq);
618
619 list_for_each(pos, &q->queue_head) {
620 struct request *pos_rq = list_entry_rq(pos);
621 if (ordseq <= blk_ordered_req_seq(pos_rq))
622 break;
623 }
624
625 list_add_tail(&rq->queuelist, pos);
626 break;
627
Tejun Heo8922e162005-10-20 16:23:44 +0200628 default:
629 printk(KERN_ERR "%s: bad insertion point %d\n",
630 __FUNCTION__, where);
631 BUG();
632 }
633
Jens Axboedac07ec2006-05-11 08:20:16 +0200634 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200635 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
636 - q->in_flight;
637
638 if (nrq >= q->unplug_thresh)
639 __generic_unplug_device(q);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Jens Axboe165125e2007-07-24 09:28:11 +0200643void __elv_add_request(struct request_queue *q, struct request *rq, int where,
Tejun Heo30e96562006-02-08 01:01:31 -0800644 int plug)
645{
646 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200647 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800648
Jens Axboe4aff5e22006-08-10 08:44:47 +0200649 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800650 /*
651 * toggle ordered color
652 */
653 if (blk_barrier_rq(rq))
654 q->ordcolor ^= 1;
655
656 /*
657 * barriers implicitly indicate back insertion
658 */
659 if (where == ELEVATOR_INSERT_SORT)
660 where = ELEVATOR_INSERT_BACK;
661
662 /*
663 * this request is scheduling boundary, update
664 * end_sector
665 */
666 if (blk_fs_request(rq)) {
667 q->end_sector = rq_end_sector(rq);
668 q->boundary_rq = rq;
669 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200670 } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800671 where = ELEVATOR_INSERT_BACK;
672
673 if (plug)
674 blk_plug_device(q);
675
676 elv_insert(q, rq, where);
677}
678
Jens Axboe2e662b62006-07-13 11:55:04 +0200679EXPORT_SYMBOL(__elv_add_request);
680
Jens Axboe165125e2007-07-24 09:28:11 +0200681void elv_add_request(struct request_queue *q, struct request *rq, int where,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int plug)
683{
684 unsigned long flags;
685
686 spin_lock_irqsave(q->queue_lock, flags);
687 __elv_add_request(q, rq, where, plug);
688 spin_unlock_irqrestore(q->queue_lock, flags);
689}
690
Jens Axboe2e662b62006-07-13 11:55:04 +0200691EXPORT_SYMBOL(elv_add_request);
692
Jens Axboe165125e2007-07-24 09:28:11 +0200693static inline struct request *__elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Tejun Heo8922e162005-10-20 16:23:44 +0200695 struct request *rq;
696
Tejun Heo797e7db2006-01-06 09:51:03 +0100697 while (1) {
698 while (!list_empty(&q->queue_head)) {
699 rq = list_entry_rq(q->queue_head.next);
700 if (blk_do_ordered(q, &rq))
701 return rq;
702 }
Tejun Heo8922e162005-10-20 16:23:44 +0200703
Tejun Heo797e7db2006-01-06 09:51:03 +0100704 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Jens Axboe165125e2007-07-24 09:28:11 +0200709struct request *elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct request *rq;
712 int ret;
713
714 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200715 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200716 /*
717 * This is the first time the device driver
718 * sees this request (possibly after
719 * requeueing). Notify IO scheduler.
720 */
Jens Axboecad97512007-01-14 22:26:09 +1100721 if (blk_sorted_rq(rq))
722 elv_activate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200723
724 /*
725 * just mark as started even if we don't start
726 * it, a request that has been delayed should
727 * not be passed by new incoming requests
728 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200729 rq->cmd_flags |= REQ_STARTED;
Jens Axboe2056a782006-03-23 20:00:26 +0100730 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
Tejun Heo8922e162005-10-20 16:23:44 +0200731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Tejun Heo8922e162005-10-20 16:23:44 +0200733 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200734 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200735 q->boundary_rq = NULL;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jens Axboe4aff5e22006-08-10 08:44:47 +0200738 if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740
741 ret = q->prep_rq_fn(q, rq);
742 if (ret == BLKPREP_OK) {
743 break;
744 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500745 /*
746 * the request may have been (partially) prepped.
747 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200748 * avoid resource deadlock. REQ_STARTED will
749 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rq = NULL;
752 break;
753 } else if (ret == BLKPREP_KILL) {
754 int nr_bytes = rq->hard_nr_sectors << 9;
755
756 if (!nr_bytes)
757 nr_bytes = rq->data_len;
758
759 blkdev_dequeue_request(rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200760 rq->cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 end_that_request_chunk(rq, 0, nr_bytes);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100762 end_that_request_last(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } else {
764 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
765 ret);
766 break;
767 }
768 }
769
770 return rq;
771}
772
Jens Axboe2e662b62006-07-13 11:55:04 +0200773EXPORT_SYMBOL(elv_next_request);
774
Jens Axboe165125e2007-07-24 09:28:11 +0200775void elv_dequeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Tejun Heo8922e162005-10-20 16:23:44 +0200777 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200778 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200779
780 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
783 * the time frame between a request being removed from the lists
784 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200785 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
787 if (blk_account_rq(rq))
788 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Jens Axboe2e662b62006-07-13 11:55:04 +0200791EXPORT_SYMBOL(elv_dequeue_request);
792
Jens Axboe165125e2007-07-24 09:28:11 +0200793int elv_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 elevator_t *e = q->elevator;
796
Tejun Heo8922e162005-10-20 16:23:44 +0200797 if (!list_empty(&q->queue_head))
798 return 0;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (e->ops->elevator_queue_empty_fn)
801 return e->ops->elevator_queue_empty_fn(q);
802
Tejun Heo8922e162005-10-20 16:23:44 +0200803 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Jens Axboe2e662b62006-07-13 11:55:04 +0200806EXPORT_SYMBOL(elv_queue_empty);
807
Jens Axboe165125e2007-07-24 09:28:11 +0200808struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 elevator_t *e = q->elevator;
811
812 if (e->ops->elevator_latter_req_fn)
813 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return NULL;
815}
816
Jens Axboe165125e2007-07-24 09:28:11 +0200817struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 elevator_t *e = q->elevator;
820
821 if (e->ops->elevator_former_req_fn)
822 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return NULL;
824}
825
Jens Axboe165125e2007-07-24 09:28:11 +0200826int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 elevator_t *e = q->elevator;
829
830 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200831 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 rq->elevator_private = NULL;
834 return 0;
835}
836
Jens Axboe165125e2007-07-24 09:28:11 +0200837void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 elevator_t *e = q->elevator;
840
841 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100842 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Jens Axboe165125e2007-07-24 09:28:11 +0200845int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 elevator_t *e = q->elevator;
848
849 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200850 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 return ELV_MQUEUE_MAY;
853}
854
Jens Axboe165125e2007-07-24 09:28:11 +0200855void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 elevator_t *e = q->elevator;
858
859 /*
860 * request is released from the driver, io must be done
861 */
Tejun Heo8922e162005-10-20 16:23:44 +0200862 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100864 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
865 e->ops->elevator_completed_req_fn(q, rq);
866 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100867
Tejun Heo1bc691d2006-01-12 15:39:26 +0100868 /*
869 * Check if the queue is waiting for fs requests to be
870 * drained for flush sequence.
871 */
872 if (unlikely(q->ordseq)) {
873 struct request *first_rq = list_entry_rq(q->queue_head.next);
874 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100875 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
876 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
877 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
878 q->request_fn(q);
879 }
Tejun Heo8922e162005-10-20 16:23:44 +0200880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Al Viro3d1ab402006-03-18 18:35:43 -0500883#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
884
885static ssize_t
886elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
887{
888 elevator_t *e = container_of(kobj, elevator_t, kobj);
889 struct elv_fs_entry *entry = to_elv(attr);
890 ssize_t error;
891
892 if (!entry->show)
893 return -EIO;
894
895 mutex_lock(&e->sysfs_lock);
896 error = e->ops ? entry->show(e, page) : -ENOENT;
897 mutex_unlock(&e->sysfs_lock);
898 return error;
899}
900
901static ssize_t
902elv_attr_store(struct kobject *kobj, struct attribute *attr,
903 const char *page, size_t length)
904{
905 elevator_t *e = container_of(kobj, elevator_t, kobj);
906 struct elv_fs_entry *entry = to_elv(attr);
907 ssize_t error;
908
909 if (!entry->store)
910 return -EIO;
911
912 mutex_lock(&e->sysfs_lock);
913 error = e->ops ? entry->store(e, page, length) : -ENOENT;
914 mutex_unlock(&e->sysfs_lock);
915 return error;
916}
917
918static struct sysfs_ops elv_sysfs_ops = {
919 .show = elv_attr_show,
920 .store = elv_attr_store,
921};
922
923static struct kobj_type elv_ktype = {
924 .sysfs_ops = &elv_sysfs_ops,
925 .release = elevator_release,
926};
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928int elv_register_queue(struct request_queue *q)
929{
930 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500931 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Al Viro3d1ab402006-03-18 18:35:43 -0500933 e->kobj.parent = &q->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Al Viro3d1ab402006-03-18 18:35:43 -0500935 error = kobject_add(&e->kobj);
936 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500937 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500938 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500939 while (attr->attr.name) {
940 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500941 break;
Al Viroe572ec72006-03-18 22:27:18 -0500942 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500943 }
944 }
945 kobject_uevent(&e->kobj, KOBJ_ADD);
946 }
947 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Jens Axboebc1c1162006-06-08 08:49:06 +0200950static void __elv_unregister_queue(elevator_t *e)
951{
952 kobject_uevent(&e->kobj, KOBJ_REMOVE);
953 kobject_del(&e->kobj);
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956void elv_unregister_queue(struct request_queue *q)
957{
Jens Axboebc1c1162006-06-08 08:49:06 +0200958 if (q)
959 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962int elv_register(struct elevator_type *e)
963{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100964 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200965
966 spin_lock(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +0100967 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200969 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Nate Diller5f003972006-01-24 10:07:58 +0100971 if (!strcmp(e->elevator_name, chosen_elevator) ||
972 (!*chosen_elevator &&
973 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100974 def = " (default)";
975
976 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, def);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978}
979EXPORT_SYMBOL_GPL(elv_register);
980
981void elv_unregister(struct elevator_type *e)
982{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800983 struct task_struct *g, *p;
984
985 /*
986 * Iterate every thread in the process to remove the io contexts.
987 */
Al Viroe17a9482006-03-18 13:21:20 -0500988 if (e->ops.trim) {
989 read_lock(&tasklist_lock);
990 do_each_thread(g, p) {
991 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +0400992 if (p->io_context)
993 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -0500994 task_unlock(p);
995 } while_each_thread(g, p);
996 read_unlock(&tasklist_lock);
997 }
Christoph Hellwig83521d32005-10-30 15:01:39 -0800998
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200999 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001001 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003EXPORT_SYMBOL_GPL(elv_unregister);
1004
1005/*
1006 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1007 * we don't free the old io scheduler, before we have allocated what we
1008 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001009 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Jens Axboe165125e2007-07-24 09:28:11 +02001011static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Tejun Heocb98fc82005-10-28 08:29:39 +02001013 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +02001014 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Tejun Heocb98fc82005-10-28 08:29:39 +02001016 /*
1017 * Allocate new elevator
1018 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001019 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -05001021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jens Axboebc1c1162006-06-08 08:49:06 +02001023 data = elevator_init_queue(q, e);
1024 if (!data) {
1025 kobject_put(&e->kobj);
1026 return 0;
1027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001030 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001032 spin_lock_irq(q->queue_lock);
1033
Jens Axboe64521d12005-10-28 08:30:39 +02001034 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001035
Tejun Heo15853af2005-11-10 08:52:05 +01001036 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001037
1038 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +01001039 blk_remove_plug(q);
1040 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001041 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001042 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001043 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001044 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001045 }
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001048 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 old_elevator = q->elevator;
1051
1052 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 * attach and start new elevator
1054 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001055 elevator_attach(q, e, data);
1056
1057 spin_unlock_irq(q->queue_lock);
1058
1059 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (elv_register_queue(q))
1062 goto fail_register;
1063
1064 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001065 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
1067 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +02001068 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001069 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071fail_register:
1072 /*
1073 * switch failed, exit the new io scheduler and reattach the old
1074 * one again (along with re-adding the sysfs dir)
1075 */
1076 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 q->elevator = old_elevator;
1078 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +02001079 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Jens Axboe165125e2007-07-24 09:28:11 +02001083ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1084 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +01001087 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 struct elevator_type *e;
1089
Tejun Heobe561232005-11-10 08:55:01 +01001090 elevator_name[sizeof(elevator_name) - 1] = '\0';
1091 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1092 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Tejun Heobe561232005-11-10 08:55:01 +01001094 if (len && elevator_name[len - 1] == '\n')
1095 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 e = elevator_get(elevator_name);
1098 if (!e) {
1099 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1100 return -EINVAL;
1101 }
1102
Nate Diller2ca7d932005-10-30 15:02:24 -08001103 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1104 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Al Viro3d1ab402006-03-18 18:35:43 -05001108 if (!elevator_switch(q, e))
1109 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return count;
1111}
1112
Jens Axboe165125e2007-07-24 09:28:11 +02001113ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 elevator_t *e = q->elevator;
1116 struct elevator_type *elv = e->elevator_type;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001117 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int len = 0;
1119
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001120 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001121 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!strcmp(elv->elevator_name, __e->elevator_name))
1123 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1124 else
1125 len += sprintf(name+len, "%s ", __e->elevator_name);
1126 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001127 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 len += sprintf(len+name, "\n");
1130 return len;
1131}
1132
Jens Axboe165125e2007-07-24 09:28:11 +02001133struct request *elv_rb_former_request(struct request_queue *q,
1134 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001135{
1136 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1137
1138 if (rbprev)
1139 return rb_entry_rq(rbprev);
1140
1141 return NULL;
1142}
1143
1144EXPORT_SYMBOL(elv_rb_former_request);
1145
Jens Axboe165125e2007-07-24 09:28:11 +02001146struct request *elv_rb_latter_request(struct request_queue *q,
1147 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001148{
1149 struct rb_node *rbnext = rb_next(&rq->rb_node);
1150
1151 if (rbnext)
1152 return rb_entry_rq(rbnext);
1153
1154 return NULL;
1155}
1156
1157EXPORT_SYMBOL(elv_rb_latter_request);