blob: bafbae0344d319e909e3e7add07467b1c0463a73 [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)
Jens Axboe4eb166d2008-02-01 00:37:27 +010048#define ELV_HASH_FN(sec) \
49 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
Jens Axboe98170642006-07-28 09:23:08 +020050#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
51#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
52#define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
53
54/*
Jens Axboeda775262006-12-20 11:04:12 +010055 * Query io scheduler to see if the current process issuing bio may be
56 * merged with rq.
57 */
58static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
59{
Jens Axboe165125e2007-07-24 09:28:11 +020060 struct request_queue *q = rq->q;
Jens Axboeda775262006-12-20 11:04:12 +010061 elevator_t *e = q->elevator;
62
63 if (e->ops->elevator_allow_merge_fn)
64 return e->ops->elevator_allow_merge_fn(q, rq, bio);
65
66 return 1;
67}
68
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * can we safely merge with this request?
71 */
72inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
73{
74 if (!rq_mergeable(rq))
75 return 0;
76
77 /*
78 * different data direction or already started, don't merge
79 */
80 if (bio_data_dir(bio) != rq_data_dir(rq))
81 return 0;
82
83 /*
Jens Axboeda775262006-12-20 11:04:12 +010084 * must be same device and not a special request
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
Jens Axboebb4067e2006-12-21 21:20:01 +010086 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
Jens Axboeda775262006-12-20 11:04:12 +010087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jens Axboeda775262006-12-20 11:04:12 +010089 if (!elv_iosched_allow_merge(rq, bio))
90 return 0;
91
92 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94EXPORT_SYMBOL(elv_rq_merge_ok);
95
Coywolf Qi Hunt769db452005-12-28 10:55:49 +010096static inline int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 int ret = ELEVATOR_NO_MERGE;
99
100 /*
101 * we can merge and sequence is ok, check if it's possible
102 */
103 if (elv_rq_merge_ok(__rq, bio)) {
104 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
105 ret = ELEVATOR_BACK_MERGE;
106 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
107 ret = ELEVATOR_FRONT_MERGE;
108 }
109
110 return ret;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static struct elevator_type *elevator_find(const char *name)
114{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200115 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200117 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +0200118 if (!strcmp(e->elevator_name, name))
119 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Vasily Tarasova22b1692006-10-11 09:24:27 +0200122 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static void elevator_put(struct elevator_type *e)
126{
127 module_put(e->elevator_owner);
128}
129
130static struct elevator_type *elevator_get(const char *name)
131{
Tejun Heo2824bc932005-10-20 10:56:41 +0200132 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200134 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200135
136 e = elevator_find(name);
137 if (e && !try_module_get(e->elevator_owner))
138 e = NULL;
139
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200140 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return e;
143}
144
Jens Axboe165125e2007-07-24 09:28:11 +0200145static void *elevator_init_queue(struct request_queue *q,
146 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Jens Axboebb37b942006-12-01 10:42:33 +0100148 return eq->ops->elevator_init_fn(q);
Jens Axboebc1c1162006-06-08 08:49:06 +0200149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jens Axboe165125e2007-07-24 09:28:11 +0200151static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
Jens Axboebc1c1162006-06-08 08:49:06 +0200152 void *data)
153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200155 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static char chosen_elevator[16];
159
Nate Diller5f003972006-01-24 10:07:58 +0100160static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100162 /*
163 * Be backwards-compatible with previous kernels, so users
164 * won't get the wrong elevator.
165 */
Nate Diller5f003972006-01-24 10:07:58 +0100166 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100167 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100168 else
Nate Diller5f003972006-01-24 10:07:58 +0100169 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800170 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173__setup("elevator=", elevator_setup);
174
Al Viro3d1ab402006-03-18 18:35:43 -0500175static struct kobj_type elv_ktype;
176
Jens Axboe165125e2007-07-24 09:28:11 +0200177static elevator_t *elevator_alloc(struct request_queue *q,
178 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500179{
Jens Axboe98170642006-07-28 09:23:08 +0200180 elevator_t *eq;
181 int i;
182
Christoph Lameter94f60302007-07-17 04:03:29 -0700183 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200184 if (unlikely(!eq))
185 goto err;
186
Jens Axboe98170642006-07-28 09:23:08 +0200187 eq->ops = &e->ops;
188 eq->elevator_type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700189 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200190 mutex_init(&eq->sysfs_lock);
191
Jens Axboeb5deef92006-07-19 23:39:40 +0200192 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
193 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200194 if (!eq->hash)
195 goto err;
196
197 for (i = 0; i < ELV_HASH_ENTRIES; i++)
198 INIT_HLIST_HEAD(&eq->hash[i]);
199
Al Viro3d1ab402006-03-18 18:35:43 -0500200 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200201err:
202 kfree(eq);
203 elevator_put(e);
204 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500205}
206
207static void elevator_release(struct kobject *kobj)
208{
209 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200210
Al Viro3d1ab402006-03-18 18:35:43 -0500211 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200212 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500213 kfree(e);
214}
215
Jens Axboe165125e2007-07-24 09:28:11 +0200216int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct elevator_type *e = NULL;
219 struct elevator_queue *eq;
220 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200221 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Tejun Heocb98fc82005-10-28 08:29:39 +0200223 INIT_LIST_HEAD(&q->queue_head);
224 q->last_merge = NULL;
225 q->end_sector = 0;
226 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200227
Jens Axboe4eb166d2008-02-01 00:37:27 +0100228 if (name) {
229 e = elevator_get(name);
230 if (!e)
231 return -EINVAL;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jens Axboe4eb166d2008-02-01 00:37:27 +0100234 if (!e && *chosen_elevator) {
235 e = elevator_get(chosen_elevator);
236 if (!e)
237 printk(KERN_ERR "I/O scheduler %s not found\n",
238 chosen_elevator);
239 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100240
Jens Axboe4eb166d2008-02-01 00:37:27 +0100241 if (!e) {
242 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
243 if (!e) {
244 printk(KERN_ERR
245 "Default I/O scheduler not found. " \
246 "Using noop.\n");
247 e = elevator_get("noop");
248 }
Nate Diller5f003972006-01-24 10:07:58 +0100249 }
250
Jens Axboeb5deef92006-07-19 23:39:40 +0200251 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500252 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jens Axboebc1c1162006-06-08 08:49:06 +0200255 data = elevator_init_queue(q, eq);
256 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500257 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200258 return -ENOMEM;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jens Axboebc1c1162006-06-08 08:49:06 +0200261 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
Jens Axboe2e662b62006-07-13 11:55:04 +0200264EXPORT_SYMBOL(elevator_init);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266void elevator_exit(elevator_t *e)
267{
Al Viro3d1ab402006-03-18 18:35:43 -0500268 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (e->ops->elevator_exit_fn)
270 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500271 e->ops = NULL;
272 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Al Viro3d1ab402006-03-18 18:35:43 -0500274 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Jens Axboe2e662b62006-07-13 11:55:04 +0200276EXPORT_SYMBOL(elevator_exit);
277
Jens Axboe165125e2007-07-24 09:28:11 +0200278static void elv_activate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100279{
280 elevator_t *e = q->elevator;
281
282 if (e->ops->elevator_activate_req_fn)
283 e->ops->elevator_activate_req_fn(q, rq);
284}
285
Jens Axboe165125e2007-07-24 09:28:11 +0200286static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100287{
288 elevator_t *e = q->elevator;
289
290 if (e->ops->elevator_deactivate_req_fn)
291 e->ops->elevator_deactivate_req_fn(q, rq);
292}
293
Jens Axboe98170642006-07-28 09:23:08 +0200294static inline void __elv_rqhash_del(struct request *rq)
295{
296 hlist_del_init(&rq->hash);
297}
298
Jens Axboe165125e2007-07-24 09:28:11 +0200299static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200300{
301 if (ELV_ON_HASH(rq))
302 __elv_rqhash_del(rq);
303}
304
Jens Axboe165125e2007-07-24 09:28:11 +0200305static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200306{
307 elevator_t *e = q->elevator;
308
309 BUG_ON(ELV_ON_HASH(rq));
310 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
311}
312
Jens Axboe165125e2007-07-24 09:28:11 +0200313static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200314{
315 __elv_rqhash_del(rq);
316 elv_rqhash_add(q, rq);
317}
318
Jens Axboe165125e2007-07-24 09:28:11 +0200319static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200320{
321 elevator_t *e = q->elevator;
322 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
323 struct hlist_node *entry, *next;
324 struct request *rq;
325
326 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
327 BUG_ON(!ELV_ON_HASH(rq));
328
329 if (unlikely(!rq_mergeable(rq))) {
330 __elv_rqhash_del(rq);
331 continue;
332 }
333
334 if (rq_hash_key(rq) == offset)
335 return rq;
336 }
337
338 return NULL;
339}
340
Tejun Heo8922e162005-10-20 16:23:44 +0200341/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200342 * RB-tree support functions for inserting/lookup/removal of requests
343 * in a sorted RB tree.
344 */
345struct request *elv_rb_add(struct rb_root *root, struct request *rq)
346{
347 struct rb_node **p = &root->rb_node;
348 struct rb_node *parent = NULL;
349 struct request *__rq;
350
351 while (*p) {
352 parent = *p;
353 __rq = rb_entry(parent, struct request, rb_node);
354
355 if (rq->sector < __rq->sector)
356 p = &(*p)->rb_left;
357 else if (rq->sector > __rq->sector)
358 p = &(*p)->rb_right;
359 else
360 return __rq;
361 }
362
363 rb_link_node(&rq->rb_node, parent, p);
364 rb_insert_color(&rq->rb_node, root);
365 return NULL;
366}
Jens Axboe2e662b62006-07-13 11:55:04 +0200367EXPORT_SYMBOL(elv_rb_add);
368
369void elv_rb_del(struct rb_root *root, struct request *rq)
370{
371 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
372 rb_erase(&rq->rb_node, root);
373 RB_CLEAR_NODE(&rq->rb_node);
374}
Jens Axboe2e662b62006-07-13 11:55:04 +0200375EXPORT_SYMBOL(elv_rb_del);
376
377struct request *elv_rb_find(struct rb_root *root, sector_t sector)
378{
379 struct rb_node *n = root->rb_node;
380 struct request *rq;
381
382 while (n) {
383 rq = rb_entry(n, struct request, rb_node);
384
385 if (sector < rq->sector)
386 n = n->rb_left;
387 else if (sector > rq->sector)
388 n = n->rb_right;
389 else
390 return rq;
391 }
392
393 return NULL;
394}
Jens Axboe2e662b62006-07-13 11:55:04 +0200395EXPORT_SYMBOL(elv_rb_find);
396
397/*
Tejun Heo8922e162005-10-20 16:23:44 +0200398 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200399 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200400 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200401 */
Jens Axboe165125e2007-07-24 09:28:11 +0200402void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200403{
404 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200405 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100406 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200407
Tejun Heo06b86242005-10-20 16:46:23 +0200408 if (q->last_merge == rq)
409 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200410
411 elv_rqhash_del(q, rq);
412
Tejun Heo15853af2005-11-10 08:52:05 +0100413 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200414
Jens Axboe1b47f532005-10-20 16:37:00 +0200415 boundary = q->end_sector;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100416 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200417 list_for_each_prev(entry, &q->queue_head) {
418 struct request *pos = list_entry_rq(entry);
419
Jens Axboe783660b2007-01-19 11:27:47 +1100420 if (rq_data_dir(rq) != rq_data_dir(pos))
421 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100422 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200423 break;
424 if (rq->sector >= boundary) {
425 if (pos->sector < boundary)
426 continue;
427 } else {
428 if (pos->sector >= boundary)
429 break;
430 }
431 if (rq->sector >= pos->sector)
432 break;
433 }
434
435 list_add(&rq->queuelist, entry);
436}
Jens Axboe2e662b62006-07-13 11:55:04 +0200437EXPORT_SYMBOL(elv_dispatch_sort);
438
Jens Axboe98170642006-07-28 09:23:08 +0200439/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200440 * Insert rq into dispatch queue of q. Queue lock must be held on
441 * entry. rq is added to the back of the dispatch queue. To be used by
442 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200443 */
444void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
445{
446 if (q->last_merge == rq)
447 q->last_merge = NULL;
448
449 elv_rqhash_del(q, rq);
450
451 q->nr_sorted--;
452
453 q->end_sector = rq_end_sector(rq);
454 q->boundary_rq = rq;
455 list_add_tail(&rq->queuelist, &q->queue_head);
456}
Jens Axboe2e662b62006-07-13 11:55:04 +0200457EXPORT_SYMBOL(elv_dispatch_add_tail);
458
Jens Axboe165125e2007-07-24 09:28:11 +0200459int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200462 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200463 int ret;
464
Jens Axboe98170642006-07-28 09:23:08 +0200465 /*
466 * First try one-hit cache.
467 */
Tejun Heo06b86242005-10-20 16:46:23 +0200468 if (q->last_merge) {
469 ret = elv_try_merge(q->last_merge, bio);
470 if (ret != ELEVATOR_NO_MERGE) {
471 *req = q->last_merge;
472 return ret;
473 }
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jens Axboe98170642006-07-28 09:23:08 +0200476 /*
477 * See if our hash lookup can find a potential backmerge.
478 */
479 __rq = elv_rqhash_find(q, bio->bi_sector);
480 if (__rq && elv_rq_merge_ok(__rq, bio)) {
481 *req = __rq;
482 return ELEVATOR_BACK_MERGE;
483 }
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (e->ops->elevator_merge_fn)
486 return e->ops->elevator_merge_fn(q, req, bio);
487
488 return ELEVATOR_NO_MERGE;
489}
490
Jens Axboe165125e2007-07-24 09:28:11 +0200491void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 elevator_t *e = q->elevator;
494
495 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200496 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200497
Jens Axboe2e662b62006-07-13 11:55:04 +0200498 if (type == ELEVATOR_BACK_MERGE)
499 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200500
Tejun Heo06b86242005-10-20 16:46:23 +0200501 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Jens Axboe165125e2007-07-24 09:28:11 +0200504void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct request *next)
506{
507 elevator_t *e = q->elevator;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (e->ops->elevator_merge_req_fn)
510 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200511
Jens Axboe98170642006-07-28 09:23:08 +0200512 elv_rqhash_reposition(q, rq);
513 elv_rqhash_del(q, next);
514
515 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200516 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Jens Axboe165125e2007-07-24 09:28:11 +0200519void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * it already went through dequeue, we need to decrement the
523 * in_flight count again
524 */
Tejun Heo8922e162005-10-20 16:23:44 +0200525 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 q->in_flight--;
Jens Axboecad97512007-01-14 22:26:09 +1100527 if (blk_sorted_rq(rq))
528 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jens Axboe4aff5e22006-08-10 08:44:47 +0200531 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Tejun Heo30e96562006-02-08 01:01:31 -0800533 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Jens Axboe165125e2007-07-24 09:28:11 +0200536static void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100537{
538 static int printed;
539 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
540 ;
541 if (q->nr_sorted == 0)
542 return;
543 if (printed++ < 10) {
544 printk(KERN_ERR "%s: forced dispatching is broken "
545 "(nr_sorted=%u), please report this\n",
546 q->elevator->elevator_type->elevator_name, q->nr_sorted);
547 }
548}
549
Jens Axboe165125e2007-07-24 09:28:11 +0200550void elv_insert(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Tejun Heo797e7db2006-01-06 09:51:03 +0100552 struct list_head *pos;
553 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200554 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100555
Jens Axboe2056a782006-03-23 20:00:26 +0100556 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rq->q = q;
559
Tejun Heo8922e162005-10-20 16:23:44 +0200560 switch (where) {
561 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200562 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200563
564 list_add(&rq->queuelist, &q->queue_head);
565 break;
566
567 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200568 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100569 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200570 list_add_tail(&rq->queuelist, &q->queue_head);
571 /*
572 * We kick the queue here for the following reasons.
573 * - The elevator might have returned NULL previously
574 * to delay requests and returned them now. As the
575 * queue wasn't empty before this request, ll_rw_blk
576 * won't run the queue on return, resulting in hang.
577 * - Usually, back inserted requests won't be merged
578 * with anything. There's no point in delaying queue
579 * processing.
580 */
581 blk_remove_plug(q);
582 q->request_fn(q);
583 break;
584
585 case ELEVATOR_INSERT_SORT:
586 BUG_ON(!blk_fs_request(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200587 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100588 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200589 if (rq_mergeable(rq)) {
590 elv_rqhash_add(q, rq);
591 if (!q->last_merge)
592 q->last_merge = rq;
593 }
594
Tejun Heoca235092005-11-01 17:23:49 +0900595 /*
596 * Some ioscheds (cfq) run q->request_fn directly, so
597 * rq cannot be accessed after calling
598 * elevator_add_req_fn.
599 */
600 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200601 break;
602
Tejun Heo797e7db2006-01-06 09:51:03 +0100603 case ELEVATOR_INSERT_REQUEUE:
604 /*
605 * If ordered flush isn't in progress, we do front
606 * insertion; otherwise, requests should be requeued
607 * in ordseq order.
608 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200609 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100610
Linas Vepstas95543172007-01-23 19:40:54 +0100611 /*
612 * Most requeues happen because of a busy condition,
613 * don't force unplug of the queue for that case.
614 */
615 unplug_it = 0;
616
Tejun Heo797e7db2006-01-06 09:51:03 +0100617 if (q->ordseq == 0) {
618 list_add(&rq->queuelist, &q->queue_head);
619 break;
620 }
621
622 ordseq = blk_ordered_req_seq(rq);
623
624 list_for_each(pos, &q->queue_head) {
625 struct request *pos_rq = list_entry_rq(pos);
626 if (ordseq <= blk_ordered_req_seq(pos_rq))
627 break;
628 }
629
630 list_add_tail(&rq->queuelist, pos);
631 break;
632
Tejun Heo8922e162005-10-20 16:23:44 +0200633 default:
634 printk(KERN_ERR "%s: bad insertion point %d\n",
635 __FUNCTION__, where);
636 BUG();
637 }
638
Jens Axboedac07ec2006-05-11 08:20:16 +0200639 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200640 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
641 - q->in_flight;
642
643 if (nrq >= q->unplug_thresh)
644 __generic_unplug_device(q);
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Jens Axboe165125e2007-07-24 09:28:11 +0200648void __elv_add_request(struct request_queue *q, struct request *rq, int where,
Tejun Heo30e96562006-02-08 01:01:31 -0800649 int plug)
650{
651 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200652 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800653
Jens Axboe4aff5e22006-08-10 08:44:47 +0200654 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800655 /*
656 * toggle ordered color
657 */
658 if (blk_barrier_rq(rq))
659 q->ordcolor ^= 1;
660
661 /*
662 * barriers implicitly indicate back insertion
663 */
664 if (where == ELEVATOR_INSERT_SORT)
665 where = ELEVATOR_INSERT_BACK;
666
667 /*
668 * this request is scheduling boundary, update
669 * end_sector
670 */
671 if (blk_fs_request(rq)) {
672 q->end_sector = rq_end_sector(rq);
673 q->boundary_rq = rq;
674 }
Jens Axboe4eb166d2008-02-01 00:37:27 +0100675 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
676 where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800677 where = ELEVATOR_INSERT_BACK;
678
679 if (plug)
680 blk_plug_device(q);
681
682 elv_insert(q, rq, where);
683}
Jens Axboe2e662b62006-07-13 11:55:04 +0200684EXPORT_SYMBOL(__elv_add_request);
685
Jens Axboe165125e2007-07-24 09:28:11 +0200686void elv_add_request(struct request_queue *q, struct request *rq, int where,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int plug)
688{
689 unsigned long flags;
690
691 spin_lock_irqsave(q->queue_lock, flags);
692 __elv_add_request(q, rq, where, plug);
693 spin_unlock_irqrestore(q->queue_lock, flags);
694}
Jens Axboe2e662b62006-07-13 11:55:04 +0200695EXPORT_SYMBOL(elv_add_request);
696
Jens Axboe165125e2007-07-24 09:28:11 +0200697static inline struct request *__elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Tejun Heo8922e162005-10-20 16:23:44 +0200699 struct request *rq;
700
Tejun Heo797e7db2006-01-06 09:51:03 +0100701 while (1) {
702 while (!list_empty(&q->queue_head)) {
703 rq = list_entry_rq(q->queue_head.next);
704 if (blk_do_ordered(q, &rq))
705 return rq;
706 }
Tejun Heo8922e162005-10-20 16:23:44 +0200707
Tejun Heo797e7db2006-01-06 09:51:03 +0100708 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
709 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Jens Axboe165125e2007-07-24 09:28:11 +0200713struct request *elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct request *rq;
716 int ret;
717
718 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200719 /*
720 * Kill the empty barrier place holder, the driver must
721 * not ever see it.
722 */
723 if (blk_empty_barrier(rq)) {
724 end_queued_request(rq, 1);
725 continue;
726 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200727 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200728 /*
729 * This is the first time the device driver
730 * sees this request (possibly after
731 * requeueing). Notify IO scheduler.
732 */
Jens Axboecad97512007-01-14 22:26:09 +1100733 if (blk_sorted_rq(rq))
734 elv_activate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200735
736 /*
737 * just mark as started even if we don't start
738 * it, a request that has been delayed should
739 * not be passed by new incoming requests
740 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200741 rq->cmd_flags |= REQ_STARTED;
Jens Axboe2056a782006-03-23 20:00:26 +0100742 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
Tejun Heo8922e162005-10-20 16:23:44 +0200743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Tejun Heo8922e162005-10-20 16:23:44 +0200745 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200746 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200747 q->boundary_rq = NULL;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600750 if (rq->cmd_flags & REQ_DONTPREP)
751 break;
752
753 if (q->dma_drain_size && rq->data_len) {
754 /*
755 * make sure space for the drain appears we
756 * know we can do this because max_hw_segments
757 * has been adjusted to be one fewer than the
758 * device can handle
759 */
760 rq->nr_phys_segments++;
761 rq->nr_hw_segments++;
762 }
763
764 if (!q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766
767 ret = q->prep_rq_fn(q, rq);
768 if (ret == BLKPREP_OK) {
769 break;
770 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500771 /*
772 * the request may have been (partially) prepped.
773 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200774 * avoid resource deadlock. REQ_STARTED will
775 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500776 */
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600777 if (q->dma_drain_size && rq->data_len &&
778 !(rq->cmd_flags & REQ_DONTPREP)) {
779 /*
780 * remove the space for the drain we added
781 * so that we don't add it again
782 */
783 --rq->nr_phys_segments;
784 --rq->nr_hw_segments;
785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 rq = NULL;
788 break;
789 } else if (ret == BLKPREP_KILL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200790 rq->cmd_flags |= REQ_QUIET;
Jens Axboea0cd1282007-09-21 10:41:07 +0200791 end_queued_request(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 } else {
793 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
794 ret);
795 break;
796 }
797 }
798
799 return rq;
800}
Jens Axboe2e662b62006-07-13 11:55:04 +0200801EXPORT_SYMBOL(elv_next_request);
802
Jens Axboe165125e2007-07-24 09:28:11 +0200803void elv_dequeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Tejun Heo8922e162005-10-20 16:23:44 +0200805 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200806 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200807
808 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * the time frame between a request being removed from the lists
812 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200813 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
815 if (blk_account_rq(rq))
816 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Jens Axboe2e662b62006-07-13 11:55:04 +0200818EXPORT_SYMBOL(elv_dequeue_request);
819
Jens Axboe165125e2007-07-24 09:28:11 +0200820int elv_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 elevator_t *e = q->elevator;
823
Tejun Heo8922e162005-10-20 16:23:44 +0200824 if (!list_empty(&q->queue_head))
825 return 0;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (e->ops->elevator_queue_empty_fn)
828 return e->ops->elevator_queue_empty_fn(q);
829
Tejun Heo8922e162005-10-20 16:23:44 +0200830 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
Jens Axboe2e662b62006-07-13 11:55:04 +0200832EXPORT_SYMBOL(elv_queue_empty);
833
Jens Axboe165125e2007-07-24 09:28:11 +0200834struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 elevator_t *e = q->elevator;
837
838 if (e->ops->elevator_latter_req_fn)
839 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return NULL;
841}
842
Jens Axboe165125e2007-07-24 09:28:11 +0200843struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 elevator_t *e = q->elevator;
846
847 if (e->ops->elevator_former_req_fn)
848 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return NULL;
850}
851
Jens Axboe165125e2007-07-24 09:28:11 +0200852int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 elevator_t *e = q->elevator;
855
856 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200857 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 rq->elevator_private = NULL;
860 return 0;
861}
862
Jens Axboe165125e2007-07-24 09:28:11 +0200863void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 elevator_t *e = q->elevator;
866
867 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100868 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Jens Axboe165125e2007-07-24 09:28:11 +0200871int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 elevator_t *e = q->elevator;
874
875 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200876 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return ELV_MQUEUE_MAY;
879}
880
Jens Axboe165125e2007-07-24 09:28:11 +0200881void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 elevator_t *e = q->elevator;
884
885 /*
886 * request is released from the driver, io must be done
887 */
Tejun Heo8922e162005-10-20 16:23:44 +0200888 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100890 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
891 e->ops->elevator_completed_req_fn(q, rq);
892 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100893
Tejun Heo1bc691d2006-01-12 15:39:26 +0100894 /*
895 * Check if the queue is waiting for fs requests to be
896 * drained for flush sequence.
897 */
898 if (unlikely(q->ordseq)) {
899 struct request *first_rq = list_entry_rq(q->queue_head.next);
900 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100901 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
902 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
903 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
904 q->request_fn(q);
905 }
Tejun Heo8922e162005-10-20 16:23:44 +0200906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Al Viro3d1ab402006-03-18 18:35:43 -0500909#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
910
911static ssize_t
912elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
913{
914 elevator_t *e = container_of(kobj, elevator_t, kobj);
915 struct elv_fs_entry *entry = to_elv(attr);
916 ssize_t error;
917
918 if (!entry->show)
919 return -EIO;
920
921 mutex_lock(&e->sysfs_lock);
922 error = e->ops ? entry->show(e, page) : -ENOENT;
923 mutex_unlock(&e->sysfs_lock);
924 return error;
925}
926
927static ssize_t
928elv_attr_store(struct kobject *kobj, struct attribute *attr,
929 const char *page, size_t length)
930{
931 elevator_t *e = container_of(kobj, elevator_t, kobj);
932 struct elv_fs_entry *entry = to_elv(attr);
933 ssize_t error;
934
935 if (!entry->store)
936 return -EIO;
937
938 mutex_lock(&e->sysfs_lock);
939 error = e->ops ? entry->store(e, page, length) : -ENOENT;
940 mutex_unlock(&e->sysfs_lock);
941 return error;
942}
943
944static struct sysfs_ops elv_sysfs_ops = {
945 .show = elv_attr_show,
946 .store = elv_attr_store,
947};
948
949static struct kobj_type elv_ktype = {
950 .sysfs_ops = &elv_sysfs_ops,
951 .release = elevator_release,
952};
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954int elv_register_queue(struct request_queue *q)
955{
956 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500957 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700959 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500960 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500961 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500962 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500963 while (attr->attr.name) {
964 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500965 break;
Al Viroe572ec72006-03-18 22:27:18 -0500966 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500967 }
968 }
969 kobject_uevent(&e->kobj, KOBJ_ADD);
970 }
971 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Jens Axboebc1c1162006-06-08 08:49:06 +0200974static void __elv_unregister_queue(elevator_t *e)
975{
976 kobject_uevent(&e->kobj, KOBJ_REMOVE);
977 kobject_del(&e->kobj);
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980void elv_unregister_queue(struct request_queue *q)
981{
Jens Axboebc1c1162006-06-08 08:49:06 +0200982 if (q)
983 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Adrian Bunk2fdd82b2007-12-12 18:51:56 +0100986void elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100988 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200989
990 spin_lock(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +0100991 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200993 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Nate Diller5f003972006-01-24 10:07:58 +0100995 if (!strcmp(e->elevator_name, chosen_elevator) ||
996 (!*chosen_elevator &&
997 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100998 def = " (default)";
999
Jens Axboe4eb166d2008-02-01 00:37:27 +01001000 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
1001 def);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003EXPORT_SYMBOL_GPL(elv_register);
1004
1005void elv_unregister(struct elevator_type *e)
1006{
Christoph Hellwig83521d32005-10-30 15:01:39 -08001007 struct task_struct *g, *p;
1008
1009 /*
1010 * Iterate every thread in the process to remove the io contexts.
1011 */
Al Viroe17a9482006-03-18 13:21:20 -05001012 if (e->ops.trim) {
1013 read_lock(&tasklist_lock);
1014 do_each_thread(g, p) {
1015 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +04001016 if (p->io_context)
1017 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -05001018 task_unlock(p);
1019 } while_each_thread(g, p);
1020 read_unlock(&tasklist_lock);
1021 }
Christoph Hellwig83521d32005-10-30 15:01:39 -08001022
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001023 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001025 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027EXPORT_SYMBOL_GPL(elv_unregister);
1028
1029/*
1030 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1031 * we don't free the old io scheduler, before we have allocated what we
1032 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001033 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Jens Axboe165125e2007-07-24 09:28:11 +02001035static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Tejun Heocb98fc82005-10-28 08:29:39 +02001037 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +02001038 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Tejun Heocb98fc82005-10-28 08:29:39 +02001040 /*
1041 * Allocate new elevator
1042 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001043 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -05001045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Jens Axboebc1c1162006-06-08 08:49:06 +02001047 data = elevator_init_queue(q, e);
1048 if (!data) {
1049 kobject_put(&e->kobj);
1050 return 0;
1051 }
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001054 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001056 spin_lock_irq(q->queue_lock);
1057
Jens Axboe64521d12005-10-28 08:30:39 +02001058 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001059
Tejun Heo15853af2005-11-10 08:52:05 +01001060 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001061
1062 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +01001063 blk_remove_plug(q);
1064 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001065 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001066 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001067 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001068 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001069 }
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001072 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 old_elevator = q->elevator;
1075
1076 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * attach and start new elevator
1078 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001079 elevator_attach(q, e, data);
1080
1081 spin_unlock_irq(q->queue_lock);
1082
1083 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (elv_register_queue(q))
1086 goto fail_register;
1087
1088 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001089 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
1091 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +02001092 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001093 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095fail_register:
1096 /*
1097 * switch failed, exit the new io scheduler and reattach the old
1098 * one again (along with re-adding the sysfs dir)
1099 */
1100 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 q->elevator = old_elevator;
1102 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +02001103 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -05001104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Jens Axboe165125e2007-07-24 09:28:11 +02001107ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1108 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +01001111 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct elevator_type *e;
1113
Tejun Heobe561232005-11-10 08:55:01 +01001114 elevator_name[sizeof(elevator_name) - 1] = '\0';
1115 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1116 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Tejun Heobe561232005-11-10 08:55:01 +01001118 if (len && elevator_name[len - 1] == '\n')
1119 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 e = elevator_get(elevator_name);
1122 if (!e) {
1123 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1124 return -EINVAL;
1125 }
1126
Nate Diller2ca7d932005-10-30 15:02:24 -08001127 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1128 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Al Viro3d1ab402006-03-18 18:35:43 -05001132 if (!elevator_switch(q, e))
Jens Axboe4eb166d2008-02-01 00:37:27 +01001133 printk(KERN_ERR "elevator: switch to %s failed\n",
1134 elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return count;
1136}
1137
Jens Axboe165125e2007-07-24 09:28:11 +02001138ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 elevator_t *e = q->elevator;
1141 struct elevator_type *elv = e->elevator_type;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001142 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int len = 0;
1144
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001145 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001146 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!strcmp(elv->elevator_name, __e->elevator_name))
1148 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1149 else
1150 len += sprintf(name+len, "%s ", __e->elevator_name);
1151 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001152 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 len += sprintf(len+name, "\n");
1155 return len;
1156}
1157
Jens Axboe165125e2007-07-24 09:28:11 +02001158struct request *elv_rb_former_request(struct request_queue *q,
1159 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001160{
1161 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1162
1163 if (rbprev)
1164 return rb_entry_rq(rbprev);
1165
1166 return NULL;
1167}
Jens Axboe2e662b62006-07-13 11:55:04 +02001168EXPORT_SYMBOL(elv_rb_former_request);
1169
Jens Axboe165125e2007-07-24 09:28:11 +02001170struct request *elv_rb_latter_request(struct request_queue *q,
1171 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001172{
1173 struct rb_node *rbnext = rb_next(&rq->rb_node);
1174
1175 if (rbnext)
1176 return rb_entry_rq(rbnext);
1177
1178 return NULL;
1179}
Jens Axboe2e662b62006-07-13 11:55:04 +02001180EXPORT_SYMBOL(elv_rb_latter_request);