blob: ac5310ef8270984e32799e0524917abf4246853a [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 */
Adrian Bunk72ed0bf2008-04-29 09:49:05 +020072int elv_rq_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
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);
Jens Axboee1640942008-02-19 10:20:37 +0100137 if (!e) {
138 char elv[ELV_NAME_MAX + strlen("-iosched")];
139
140 spin_unlock(&elv_list_lock);
141
142 if (!strcmp(name, "anticipatory"))
143 sprintf(elv, "as-iosched");
144 else
145 sprintf(elv, "%s-iosched", name);
146
147 request_module(elv);
148 spin_lock(&elv_list_lock);
149 e = elevator_find(name);
150 }
151
Tejun Heo2824bc932005-10-20 10:56:41 +0200152 if (e && !try_module_get(e->elevator_owner))
153 e = NULL;
154
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200155 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return e;
158}
159
Jens Axboe165125e2007-07-24 09:28:11 +0200160static void *elevator_init_queue(struct request_queue *q,
161 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Jens Axboebb37b942006-12-01 10:42:33 +0100163 return eq->ops->elevator_init_fn(q);
Jens Axboebc1c1162006-06-08 08:49:06 +0200164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jens Axboe165125e2007-07-24 09:28:11 +0200166static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
Jens Axboebc1c1162006-06-08 08:49:06 +0200167 void *data)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200170 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static char chosen_elevator[16];
174
Nate Diller5f003972006-01-24 10:07:58 +0100175static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100177 /*
178 * Be backwards-compatible with previous kernels, so users
179 * won't get the wrong elevator.
180 */
Nate Diller5f003972006-01-24 10:07:58 +0100181 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100182 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100183 else
Nate Diller5f003972006-01-24 10:07:58 +0100184 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800185 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188__setup("elevator=", elevator_setup);
189
Al Viro3d1ab402006-03-18 18:35:43 -0500190static struct kobj_type elv_ktype;
191
Jens Axboe165125e2007-07-24 09:28:11 +0200192static elevator_t *elevator_alloc(struct request_queue *q,
193 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500194{
Jens Axboe98170642006-07-28 09:23:08 +0200195 elevator_t *eq;
196 int i;
197
Christoph Lameter94f60302007-07-17 04:03:29 -0700198 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200199 if (unlikely(!eq))
200 goto err;
201
Jens Axboe98170642006-07-28 09:23:08 +0200202 eq->ops = &e->ops;
203 eq->elevator_type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700204 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200205 mutex_init(&eq->sysfs_lock);
206
Jens Axboeb5deef92006-07-19 23:39:40 +0200207 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
208 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200209 if (!eq->hash)
210 goto err;
211
212 for (i = 0; i < ELV_HASH_ENTRIES; i++)
213 INIT_HLIST_HEAD(&eq->hash[i]);
214
Al Viro3d1ab402006-03-18 18:35:43 -0500215 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200216err:
217 kfree(eq);
218 elevator_put(e);
219 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500220}
221
222static void elevator_release(struct kobject *kobj)
223{
224 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200225
Al Viro3d1ab402006-03-18 18:35:43 -0500226 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200227 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500228 kfree(e);
229}
230
Jens Axboe165125e2007-07-24 09:28:11 +0200231int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct elevator_type *e = NULL;
234 struct elevator_queue *eq;
235 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200236 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Tejun Heocb98fc82005-10-28 08:29:39 +0200238 INIT_LIST_HEAD(&q->queue_head);
239 q->last_merge = NULL;
240 q->end_sector = 0;
241 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200242
Jens Axboe4eb166d2008-02-01 00:37:27 +0100243 if (name) {
244 e = elevator_get(name);
245 if (!e)
246 return -EINVAL;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jens Axboe4eb166d2008-02-01 00:37:27 +0100249 if (!e && *chosen_elevator) {
250 e = elevator_get(chosen_elevator);
251 if (!e)
252 printk(KERN_ERR "I/O scheduler %s not found\n",
253 chosen_elevator);
254 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100255
Jens Axboe4eb166d2008-02-01 00:37:27 +0100256 if (!e) {
257 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
258 if (!e) {
259 printk(KERN_ERR
260 "Default I/O scheduler not found. " \
261 "Using noop.\n");
262 e = elevator_get("noop");
263 }
Nate Diller5f003972006-01-24 10:07:58 +0100264 }
265
Jens Axboeb5deef92006-07-19 23:39:40 +0200266 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500267 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jens Axboebc1c1162006-06-08 08:49:06 +0200270 data = elevator_init_queue(q, eq);
271 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500272 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200273 return -ENOMEM;
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jens Axboebc1c1162006-06-08 08:49:06 +0200276 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return ret;
278}
Jens Axboe2e662b62006-07-13 11:55:04 +0200279EXPORT_SYMBOL(elevator_init);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281void elevator_exit(elevator_t *e)
282{
Al Viro3d1ab402006-03-18 18:35:43 -0500283 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (e->ops->elevator_exit_fn)
285 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500286 e->ops = NULL;
287 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Al Viro3d1ab402006-03-18 18:35:43 -0500289 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Jens Axboe2e662b62006-07-13 11:55:04 +0200291EXPORT_SYMBOL(elevator_exit);
292
Jens Axboe165125e2007-07-24 09:28:11 +0200293static void elv_activate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100294{
295 elevator_t *e = q->elevator;
296
297 if (e->ops->elevator_activate_req_fn)
298 e->ops->elevator_activate_req_fn(q, rq);
299}
300
Jens Axboe165125e2007-07-24 09:28:11 +0200301static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100302{
303 elevator_t *e = q->elevator;
304
305 if (e->ops->elevator_deactivate_req_fn)
306 e->ops->elevator_deactivate_req_fn(q, rq);
307}
308
Jens Axboe98170642006-07-28 09:23:08 +0200309static inline void __elv_rqhash_del(struct request *rq)
310{
311 hlist_del_init(&rq->hash);
312}
313
Jens Axboe165125e2007-07-24 09:28:11 +0200314static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200315{
316 if (ELV_ON_HASH(rq))
317 __elv_rqhash_del(rq);
318}
319
Jens Axboe165125e2007-07-24 09:28:11 +0200320static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200321{
322 elevator_t *e = q->elevator;
323
324 BUG_ON(ELV_ON_HASH(rq));
325 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
326}
327
Jens Axboe165125e2007-07-24 09:28:11 +0200328static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200329{
330 __elv_rqhash_del(rq);
331 elv_rqhash_add(q, rq);
332}
333
Jens Axboe165125e2007-07-24 09:28:11 +0200334static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200335{
336 elevator_t *e = q->elevator;
337 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
338 struct hlist_node *entry, *next;
339 struct request *rq;
340
341 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
342 BUG_ON(!ELV_ON_HASH(rq));
343
344 if (unlikely(!rq_mergeable(rq))) {
345 __elv_rqhash_del(rq);
346 continue;
347 }
348
349 if (rq_hash_key(rq) == offset)
350 return rq;
351 }
352
353 return NULL;
354}
355
Tejun Heo8922e162005-10-20 16:23:44 +0200356/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200357 * RB-tree support functions for inserting/lookup/removal of requests
358 * in a sorted RB tree.
359 */
360struct request *elv_rb_add(struct rb_root *root, struct request *rq)
361{
362 struct rb_node **p = &root->rb_node;
363 struct rb_node *parent = NULL;
364 struct request *__rq;
365
366 while (*p) {
367 parent = *p;
368 __rq = rb_entry(parent, struct request, rb_node);
369
370 if (rq->sector < __rq->sector)
371 p = &(*p)->rb_left;
372 else if (rq->sector > __rq->sector)
373 p = &(*p)->rb_right;
374 else
375 return __rq;
376 }
377
378 rb_link_node(&rq->rb_node, parent, p);
379 rb_insert_color(&rq->rb_node, root);
380 return NULL;
381}
Jens Axboe2e662b62006-07-13 11:55:04 +0200382EXPORT_SYMBOL(elv_rb_add);
383
384void elv_rb_del(struct rb_root *root, struct request *rq)
385{
386 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
387 rb_erase(&rq->rb_node, root);
388 RB_CLEAR_NODE(&rq->rb_node);
389}
Jens Axboe2e662b62006-07-13 11:55:04 +0200390EXPORT_SYMBOL(elv_rb_del);
391
392struct request *elv_rb_find(struct rb_root *root, sector_t sector)
393{
394 struct rb_node *n = root->rb_node;
395 struct request *rq;
396
397 while (n) {
398 rq = rb_entry(n, struct request, rb_node);
399
400 if (sector < rq->sector)
401 n = n->rb_left;
402 else if (sector > rq->sector)
403 n = n->rb_right;
404 else
405 return rq;
406 }
407
408 return NULL;
409}
Jens Axboe2e662b62006-07-13 11:55:04 +0200410EXPORT_SYMBOL(elv_rb_find);
411
412/*
Tejun Heo8922e162005-10-20 16:23:44 +0200413 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200414 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200415 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200416 */
Jens Axboe165125e2007-07-24 09:28:11 +0200417void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200418{
419 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200420 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100421 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200422
Tejun Heo06b86242005-10-20 16:46:23 +0200423 if (q->last_merge == rq)
424 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200425
426 elv_rqhash_del(q, rq);
427
Tejun Heo15853af2005-11-10 08:52:05 +0100428 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200429
Jens Axboe1b47f532005-10-20 16:37:00 +0200430 boundary = q->end_sector;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100431 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200432 list_for_each_prev(entry, &q->queue_head) {
433 struct request *pos = list_entry_rq(entry);
434
Jens Axboe783660b2007-01-19 11:27:47 +1100435 if (rq_data_dir(rq) != rq_data_dir(pos))
436 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100437 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200438 break;
439 if (rq->sector >= boundary) {
440 if (pos->sector < boundary)
441 continue;
442 } else {
443 if (pos->sector >= boundary)
444 break;
445 }
446 if (rq->sector >= pos->sector)
447 break;
448 }
449
450 list_add(&rq->queuelist, entry);
451}
Jens Axboe2e662b62006-07-13 11:55:04 +0200452EXPORT_SYMBOL(elv_dispatch_sort);
453
Jens Axboe98170642006-07-28 09:23:08 +0200454/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200455 * Insert rq into dispatch queue of q. Queue lock must be held on
456 * entry. rq is added to the back of the dispatch queue. To be used by
457 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200458 */
459void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
460{
461 if (q->last_merge == rq)
462 q->last_merge = NULL;
463
464 elv_rqhash_del(q, rq);
465
466 q->nr_sorted--;
467
468 q->end_sector = rq_end_sector(rq);
469 q->boundary_rq = rq;
470 list_add_tail(&rq->queuelist, &q->queue_head);
471}
Jens Axboe2e662b62006-07-13 11:55:04 +0200472EXPORT_SYMBOL(elv_dispatch_add_tail);
473
Jens Axboe165125e2007-07-24 09:28:11 +0200474int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200477 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200478 int ret;
479
Jens Axboe98170642006-07-28 09:23:08 +0200480 /*
481 * First try one-hit cache.
482 */
Tejun Heo06b86242005-10-20 16:46:23 +0200483 if (q->last_merge) {
484 ret = elv_try_merge(q->last_merge, bio);
485 if (ret != ELEVATOR_NO_MERGE) {
486 *req = q->last_merge;
487 return ret;
488 }
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200491 if (blk_queue_nomerges(q))
492 return ELEVATOR_NO_MERGE;
493
Jens Axboe98170642006-07-28 09:23:08 +0200494 /*
495 * See if our hash lookup can find a potential backmerge.
496 */
497 __rq = elv_rqhash_find(q, bio->bi_sector);
498 if (__rq && elv_rq_merge_ok(__rq, bio)) {
499 *req = __rq;
500 return ELEVATOR_BACK_MERGE;
501 }
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (e->ops->elevator_merge_fn)
504 return e->ops->elevator_merge_fn(q, req, bio);
505
506 return ELEVATOR_NO_MERGE;
507}
508
Jens Axboe165125e2007-07-24 09:28:11 +0200509void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 elevator_t *e = q->elevator;
512
513 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200514 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200515
Jens Axboe2e662b62006-07-13 11:55:04 +0200516 if (type == ELEVATOR_BACK_MERGE)
517 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200518
Tejun Heo06b86242005-10-20 16:46:23 +0200519 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Jens Axboe165125e2007-07-24 09:28:11 +0200522void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct request *next)
524{
525 elevator_t *e = q->elevator;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (e->ops->elevator_merge_req_fn)
528 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200529
Jens Axboe98170642006-07-28 09:23:08 +0200530 elv_rqhash_reposition(q, rq);
531 elv_rqhash_del(q, next);
532
533 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200534 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Jens Axboe165125e2007-07-24 09:28:11 +0200537void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * it already went through dequeue, we need to decrement the
541 * in_flight count again
542 */
Tejun Heo8922e162005-10-20 16:23:44 +0200543 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 q->in_flight--;
Jens Axboecad97512007-01-14 22:26:09 +1100545 if (blk_sorted_rq(rq))
546 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jens Axboe4aff5e22006-08-10 08:44:47 +0200549 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Tejun Heo30e96562006-02-08 01:01:31 -0800551 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Jens Axboe165125e2007-07-24 09:28:11 +0200554static void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100555{
556 static int printed;
557 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
558 ;
559 if (q->nr_sorted == 0)
560 return;
561 if (printed++ < 10) {
562 printk(KERN_ERR "%s: forced dispatching is broken "
563 "(nr_sorted=%u), please report this\n",
564 q->elevator->elevator_type->elevator_name, q->nr_sorted);
565 }
566}
567
Jens Axboe165125e2007-07-24 09:28:11 +0200568void elv_insert(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Tejun Heo797e7db2006-01-06 09:51:03 +0100570 struct list_head *pos;
571 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200572 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100573
Jens Axboe2056a782006-03-23 20:00:26 +0100574 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rq->q = q;
577
Tejun Heo8922e162005-10-20 16:23:44 +0200578 switch (where) {
579 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200580 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200581
582 list_add(&rq->queuelist, &q->queue_head);
583 break;
584
585 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200586 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100587 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200588 list_add_tail(&rq->queuelist, &q->queue_head);
589 /*
590 * We kick the queue here for the following reasons.
591 * - The elevator might have returned NULL previously
592 * to delay requests and returned them now. As the
593 * queue wasn't empty before this request, ll_rw_blk
594 * won't run the queue on return, resulting in hang.
595 * - Usually, back inserted requests won't be merged
596 * with anything. There's no point in delaying queue
597 * processing.
598 */
599 blk_remove_plug(q);
600 q->request_fn(q);
601 break;
602
603 case ELEVATOR_INSERT_SORT:
604 BUG_ON(!blk_fs_request(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200605 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100606 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200607 if (rq_mergeable(rq)) {
608 elv_rqhash_add(q, rq);
609 if (!q->last_merge)
610 q->last_merge = rq;
611 }
612
Tejun Heoca235092005-11-01 17:23:49 +0900613 /*
614 * Some ioscheds (cfq) run q->request_fn directly, so
615 * rq cannot be accessed after calling
616 * elevator_add_req_fn.
617 */
618 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200619 break;
620
Tejun Heo797e7db2006-01-06 09:51:03 +0100621 case ELEVATOR_INSERT_REQUEUE:
622 /*
623 * If ordered flush isn't in progress, we do front
624 * insertion; otherwise, requests should be requeued
625 * in ordseq order.
626 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200627 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100628
Linas Vepstas95543172007-01-23 19:40:54 +0100629 /*
630 * Most requeues happen because of a busy condition,
631 * don't force unplug of the queue for that case.
632 */
633 unplug_it = 0;
634
Tejun Heo797e7db2006-01-06 09:51:03 +0100635 if (q->ordseq == 0) {
636 list_add(&rq->queuelist, &q->queue_head);
637 break;
638 }
639
640 ordseq = blk_ordered_req_seq(rq);
641
642 list_for_each(pos, &q->queue_head) {
643 struct request *pos_rq = list_entry_rq(pos);
644 if (ordseq <= blk_ordered_req_seq(pos_rq))
645 break;
646 }
647
648 list_add_tail(&rq->queuelist, pos);
649 break;
650
Tejun Heo8922e162005-10-20 16:23:44 +0200651 default:
652 printk(KERN_ERR "%s: bad insertion point %d\n",
653 __FUNCTION__, where);
654 BUG();
655 }
656
Jens Axboedac07ec2006-05-11 08:20:16 +0200657 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200658 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
659 - q->in_flight;
660
661 if (nrq >= q->unplug_thresh)
662 __generic_unplug_device(q);
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Jens Axboe165125e2007-07-24 09:28:11 +0200666void __elv_add_request(struct request_queue *q, struct request *rq, int where,
Tejun Heo30e96562006-02-08 01:01:31 -0800667 int plug)
668{
669 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200670 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800671
Jens Axboe4aff5e22006-08-10 08:44:47 +0200672 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800673 /*
674 * toggle ordered color
675 */
676 if (blk_barrier_rq(rq))
677 q->ordcolor ^= 1;
678
679 /*
680 * barriers implicitly indicate back insertion
681 */
682 if (where == ELEVATOR_INSERT_SORT)
683 where = ELEVATOR_INSERT_BACK;
684
685 /*
686 * this request is scheduling boundary, update
687 * end_sector
688 */
689 if (blk_fs_request(rq)) {
690 q->end_sector = rq_end_sector(rq);
691 q->boundary_rq = rq;
692 }
Jens Axboe4eb166d2008-02-01 00:37:27 +0100693 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
694 where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800695 where = ELEVATOR_INSERT_BACK;
696
697 if (plug)
698 blk_plug_device(q);
699
700 elv_insert(q, rq, where);
701}
Jens Axboe2e662b62006-07-13 11:55:04 +0200702EXPORT_SYMBOL(__elv_add_request);
703
Jens Axboe165125e2007-07-24 09:28:11 +0200704void elv_add_request(struct request_queue *q, struct request *rq, int where,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int plug)
706{
707 unsigned long flags;
708
709 spin_lock_irqsave(q->queue_lock, flags);
710 __elv_add_request(q, rq, where, plug);
711 spin_unlock_irqrestore(q->queue_lock, flags);
712}
Jens Axboe2e662b62006-07-13 11:55:04 +0200713EXPORT_SYMBOL(elv_add_request);
714
Jens Axboe165125e2007-07-24 09:28:11 +0200715static inline struct request *__elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Tejun Heo8922e162005-10-20 16:23:44 +0200717 struct request *rq;
718
Tejun Heo797e7db2006-01-06 09:51:03 +0100719 while (1) {
720 while (!list_empty(&q->queue_head)) {
721 rq = list_entry_rq(q->queue_head.next);
722 if (blk_do_ordered(q, &rq))
723 return rq;
724 }
Tejun Heo8922e162005-10-20 16:23:44 +0200725
Tejun Heo797e7db2006-01-06 09:51:03 +0100726 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
727 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Jens Axboe165125e2007-07-24 09:28:11 +0200731struct request *elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct request *rq;
734 int ret;
735
736 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200737 /*
738 * Kill the empty barrier place holder, the driver must
739 * not ever see it.
740 */
741 if (blk_empty_barrier(rq)) {
742 end_queued_request(rq, 1);
743 continue;
744 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200745 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200746 /*
747 * This is the first time the device driver
748 * sees this request (possibly after
749 * requeueing). Notify IO scheduler.
750 */
Jens Axboecad97512007-01-14 22:26:09 +1100751 if (blk_sorted_rq(rq))
752 elv_activate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200753
754 /*
755 * just mark as started even if we don't start
756 * it, a request that has been delayed should
757 * not be passed by new incoming requests
758 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200759 rq->cmd_flags |= REQ_STARTED;
Jens Axboe2056a782006-03-23 20:00:26 +0100760 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
Tejun Heo8922e162005-10-20 16:23:44 +0200761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Tejun Heo8922e162005-10-20 16:23:44 +0200763 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200764 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200765 q->boundary_rq = NULL;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600768 if (rq->cmd_flags & REQ_DONTPREP)
769 break;
770
771 if (q->dma_drain_size && rq->data_len) {
772 /*
773 * make sure space for the drain appears we
774 * know we can do this because max_hw_segments
775 * has been adjusted to be one fewer than the
776 * device can handle
777 */
778 rq->nr_phys_segments++;
779 rq->nr_hw_segments++;
780 }
781
782 if (!q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784
785 ret = q->prep_rq_fn(q, rq);
786 if (ret == BLKPREP_OK) {
787 break;
788 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500789 /*
790 * the request may have been (partially) prepped.
791 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200792 * avoid resource deadlock. REQ_STARTED will
793 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500794 */
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600795 if (q->dma_drain_size && rq->data_len &&
796 !(rq->cmd_flags & REQ_DONTPREP)) {
797 /*
798 * remove the space for the drain we added
799 * so that we don't add it again
800 */
801 --rq->nr_phys_segments;
802 --rq->nr_hw_segments;
803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 rq = NULL;
806 break;
807 } else if (ret == BLKPREP_KILL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200808 rq->cmd_flags |= REQ_QUIET;
Jens Axboea0cd1282007-09-21 10:41:07 +0200809 end_queued_request(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 } else {
811 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
812 ret);
813 break;
814 }
815 }
816
817 return rq;
818}
Jens Axboe2e662b62006-07-13 11:55:04 +0200819EXPORT_SYMBOL(elv_next_request);
820
Jens Axboe165125e2007-07-24 09:28:11 +0200821void elv_dequeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Tejun Heo8922e162005-10-20 16:23:44 +0200823 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200824 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200825
826 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /*
829 * the time frame between a request being removed from the lists
830 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200831 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
833 if (blk_account_rq(rq))
834 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
Jens Axboe2e662b62006-07-13 11:55:04 +0200836EXPORT_SYMBOL(elv_dequeue_request);
837
Jens Axboe165125e2007-07-24 09:28:11 +0200838int elv_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 elevator_t *e = q->elevator;
841
Tejun Heo8922e162005-10-20 16:23:44 +0200842 if (!list_empty(&q->queue_head))
843 return 0;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (e->ops->elevator_queue_empty_fn)
846 return e->ops->elevator_queue_empty_fn(q);
847
Tejun Heo8922e162005-10-20 16:23:44 +0200848 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
Jens Axboe2e662b62006-07-13 11:55:04 +0200850EXPORT_SYMBOL(elv_queue_empty);
851
Jens Axboe165125e2007-07-24 09:28:11 +0200852struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 elevator_t *e = q->elevator;
855
856 if (e->ops->elevator_latter_req_fn)
857 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return NULL;
859}
860
Jens Axboe165125e2007-07-24 09:28:11 +0200861struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 elevator_t *e = q->elevator;
864
865 if (e->ops->elevator_former_req_fn)
866 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return NULL;
868}
869
Jens Axboe165125e2007-07-24 09:28:11 +0200870int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 elevator_t *e = q->elevator;
873
874 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200875 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 rq->elevator_private = NULL;
878 return 0;
879}
880
Jens Axboe165125e2007-07-24 09:28:11 +0200881void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 elevator_t *e = q->elevator;
884
885 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100886 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Jens Axboe165125e2007-07-24 09:28:11 +0200889int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 elevator_t *e = q->elevator;
892
893 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200894 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 return ELV_MQUEUE_MAY;
897}
898
Jens Axboe165125e2007-07-24 09:28:11 +0200899void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 elevator_t *e = q->elevator;
902
903 /*
904 * request is released from the driver, io must be done
905 */
Tejun Heo8922e162005-10-20 16:23:44 +0200906 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100908 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
909 e->ops->elevator_completed_req_fn(q, rq);
910 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100911
Tejun Heo1bc691d2006-01-12 15:39:26 +0100912 /*
913 * Check if the queue is waiting for fs requests to be
914 * drained for flush sequence.
915 */
916 if (unlikely(q->ordseq)) {
917 struct request *first_rq = list_entry_rq(q->queue_head.next);
918 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100919 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
920 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
921 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
922 q->request_fn(q);
923 }
Tejun Heo8922e162005-10-20 16:23:44 +0200924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Al Viro3d1ab402006-03-18 18:35:43 -0500927#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
928
929static ssize_t
930elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
931{
932 elevator_t *e = container_of(kobj, elevator_t, kobj);
933 struct elv_fs_entry *entry = to_elv(attr);
934 ssize_t error;
935
936 if (!entry->show)
937 return -EIO;
938
939 mutex_lock(&e->sysfs_lock);
940 error = e->ops ? entry->show(e, page) : -ENOENT;
941 mutex_unlock(&e->sysfs_lock);
942 return error;
943}
944
945static ssize_t
946elv_attr_store(struct kobject *kobj, struct attribute *attr,
947 const char *page, size_t length)
948{
949 elevator_t *e = container_of(kobj, elevator_t, kobj);
950 struct elv_fs_entry *entry = to_elv(attr);
951 ssize_t error;
952
953 if (!entry->store)
954 return -EIO;
955
956 mutex_lock(&e->sysfs_lock);
957 error = e->ops ? entry->store(e, page, length) : -ENOENT;
958 mutex_unlock(&e->sysfs_lock);
959 return error;
960}
961
962static struct sysfs_ops elv_sysfs_ops = {
963 .show = elv_attr_show,
964 .store = elv_attr_store,
965};
966
967static struct kobj_type elv_ktype = {
968 .sysfs_ops = &elv_sysfs_ops,
969 .release = elevator_release,
970};
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972int elv_register_queue(struct request_queue *q)
973{
974 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500975 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700977 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500978 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500979 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500980 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500981 while (attr->attr.name) {
982 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500983 break;
Al Viroe572ec72006-03-18 22:27:18 -0500984 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500985 }
986 }
987 kobject_uevent(&e->kobj, KOBJ_ADD);
988 }
989 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Jens Axboebc1c1162006-06-08 08:49:06 +0200992static void __elv_unregister_queue(elevator_t *e)
993{
994 kobject_uevent(&e->kobj, KOBJ_REMOVE);
995 kobject_del(&e->kobj);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998void elv_unregister_queue(struct request_queue *q)
999{
Jens Axboebc1c1162006-06-08 08:49:06 +02001000 if (q)
1001 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01001004void elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +01001006 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001007
1008 spin_lock(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +01001009 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001011 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Nate Diller5f003972006-01-24 10:07:58 +01001013 if (!strcmp(e->elevator_name, chosen_elevator) ||
1014 (!*chosen_elevator &&
1015 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +01001016 def = " (default)";
1017
Jens Axboe4eb166d2008-02-01 00:37:27 +01001018 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
1019 def);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021EXPORT_SYMBOL_GPL(elv_register);
1022
1023void elv_unregister(struct elevator_type *e)
1024{
Christoph Hellwig83521d32005-10-30 15:01:39 -08001025 struct task_struct *g, *p;
1026
1027 /*
1028 * Iterate every thread in the process to remove the io contexts.
1029 */
Al Viroe17a9482006-03-18 13:21:20 -05001030 if (e->ops.trim) {
1031 read_lock(&tasklist_lock);
1032 do_each_thread(g, p) {
1033 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +04001034 if (p->io_context)
1035 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -05001036 task_unlock(p);
1037 } while_each_thread(g, p);
1038 read_unlock(&tasklist_lock);
1039 }
Christoph Hellwig83521d32005-10-30 15:01:39 -08001040
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001041 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001043 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045EXPORT_SYMBOL_GPL(elv_unregister);
1046
1047/*
1048 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1049 * we don't free the old io scheduler, before we have allocated what we
1050 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001051 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Jens Axboe165125e2007-07-24 09:28:11 +02001053static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Tejun Heocb98fc82005-10-28 08:29:39 +02001055 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +02001056 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Tejun Heocb98fc82005-10-28 08:29:39 +02001058 /*
1059 * Allocate new elevator
1060 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001061 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -05001063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Jens Axboebc1c1162006-06-08 08:49:06 +02001065 data = elevator_init_queue(q, e);
1066 if (!data) {
1067 kobject_put(&e->kobj);
1068 return 0;
1069 }
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001072 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001074 spin_lock_irq(q->queue_lock);
1075
Nick Piggin75ad23b2008-04-29 14:48:33 +02001076 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001077
Tejun Heo15853af2005-11-10 08:52:05 +01001078 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001079
1080 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +01001081 blk_remove_plug(q);
1082 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001083 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001084 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001085 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001086 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001090 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 old_elevator = q->elevator;
1093
1094 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * attach and start new elevator
1096 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001097 elevator_attach(q, e, data);
1098
1099 spin_unlock_irq(q->queue_lock);
1100
1101 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (elv_register_queue(q))
1104 goto fail_register;
1105
1106 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001107 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
1109 elevator_exit(old_elevator);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001110 spin_lock_irq(q->queue_lock);
1111 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1112 spin_unlock_irq(q->queue_lock);
1113
Al Viro3d1ab402006-03-18 18:35:43 -05001114 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116fail_register:
1117 /*
1118 * switch failed, exit the new io scheduler and reattach the old
1119 * one again (along with re-adding the sysfs dir)
1120 */
1121 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 q->elevator = old_elevator;
1123 elv_register_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001124
1125 spin_lock_irq(q->queue_lock);
1126 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1127 spin_unlock_irq(q->queue_lock);
1128
Al Viro3d1ab402006-03-18 18:35:43 -05001129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Jens Axboe165125e2007-07-24 09:28:11 +02001132ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1133 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +01001136 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct elevator_type *e;
1138
Tejun Heobe561232005-11-10 08:55:01 +01001139 elevator_name[sizeof(elevator_name) - 1] = '\0';
1140 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1141 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Tejun Heobe561232005-11-10 08:55:01 +01001143 if (len && elevator_name[len - 1] == '\n')
1144 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 e = elevator_get(elevator_name);
1147 if (!e) {
1148 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1149 return -EINVAL;
1150 }
1151
Nate Diller2ca7d932005-10-30 15:02:24 -08001152 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1153 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Al Viro3d1ab402006-03-18 18:35:43 -05001157 if (!elevator_switch(q, e))
Jens Axboe4eb166d2008-02-01 00:37:27 +01001158 printk(KERN_ERR "elevator: switch to %s failed\n",
1159 elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return count;
1161}
1162
Jens Axboe165125e2007-07-24 09:28:11 +02001163ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 elevator_t *e = q->elevator;
1166 struct elevator_type *elv = e->elevator_type;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001167 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int len = 0;
1169
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001170 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001171 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!strcmp(elv->elevator_name, __e->elevator_name))
1173 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1174 else
1175 len += sprintf(name+len, "%s ", __e->elevator_name);
1176 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001177 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 len += sprintf(len+name, "\n");
1180 return len;
1181}
1182
Jens Axboe165125e2007-07-24 09:28:11 +02001183struct request *elv_rb_former_request(struct request_queue *q,
1184 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001185{
1186 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1187
1188 if (rbprev)
1189 return rb_entry_rq(rbprev);
1190
1191 return NULL;
1192}
Jens Axboe2e662b62006-07-13 11:55:04 +02001193EXPORT_SYMBOL(elv_rb_former_request);
1194
Jens Axboe165125e2007-07-24 09:28:11 +02001195struct request *elv_rb_latter_request(struct request_queue *q,
1196 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001197{
1198 struct rb_node *rbnext = rb_next(&rq->rb_node);
1199
1200 if (rbnext)
1201 return rb_entry_rq(rbnext);
1202
1203 return NULL;
1204}
Jens Axboe2e662b62006-07-13 11:55:04 +02001205EXPORT_SYMBOL(elv_rb_latter_request);