blob: ca861927ba41b2dfc9ff80c139f6c8cc162fc251 [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>
Jens Axboe0835da62008-08-26 09:15:47 +020037#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Li Zefan55782132009-06-09 13:43:05 +080039#include <trace/events/block.h>
40
Jens Axboe242f9dc2008-09-14 05:55:09 -070041#include "blk.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static DEFINE_SPINLOCK(elv_list_lock);
44static LIST_HEAD(elv_list);
45
46/*
Jens Axboe98170642006-07-28 09:23:08 +020047 * Merge hash stuff.
48 */
49static const int elv_hash_shift = 6;
50#define ELV_HASH_BLOCK(sec) ((sec) >> 3)
Jens Axboe4eb166d2008-02-01 00:37:27 +010051#define ELV_HASH_FN(sec) \
52 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
Jens Axboe98170642006-07-28 09:23:08 +020053#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
Tejun Heo83096eb2009-05-07 22:24:39 +090054#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020055
56/*
Jens Axboeda775262006-12-20 11:04:12 +010057 * Query io scheduler to see if the current process issuing bio may be
58 * merged with rq.
59 */
60static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
61{
Jens Axboe165125e2007-07-24 09:28:11 +020062 struct request_queue *q = rq->q;
Jens Axboeb374d182008-10-31 10:05:07 +010063 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010064
65 if (e->ops->elevator_allow_merge_fn)
66 return e->ops->elevator_allow_merge_fn(q, rq, bio);
67
68 return 1;
69}
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * can we safely merge with this request?
73 */
Adrian Bunk72ed0bf2008-04-29 09:49:05 +020074int elv_rq_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 if (!rq_mergeable(rq))
77 return 0;
78
79 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +010080 * Don't merge file system requests and discard requests
81 */
82 if (bio_discard(bio) != bio_discard(rq->bio))
83 return 0;
84
85 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * different data direction or already started, don't merge
87 */
88 if (bio_data_dir(bio) != rq_data_dir(rq))
89 return 0;
90
91 /*
Jens Axboeda775262006-12-20 11:04:12 +010092 * must be same device and not a special request
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Jens Axboebb4067e2006-12-21 21:20:01 +010094 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
Jens Axboeda775262006-12-20 11:04:12 +010095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020097 /*
98 * only merge integrity protected bio into ditto rq
99 */
100 if (bio_integrity(bio) != blk_integrity_rq(rq))
101 return 0;
102
Jens Axboeda775262006-12-20 11:04:12 +0100103 if (!elv_iosched_allow_merge(rq, bio))
104 return 0;
105
106 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108EXPORT_SYMBOL(elv_rq_merge_ok);
109
Coywolf Qi Hunt769db452005-12-28 10:55:49 +0100110static inline int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 int ret = ELEVATOR_NO_MERGE;
113
114 /*
115 * we can merge and sequence is ok, check if it's possible
116 */
117 if (elv_rq_merge_ok(__rq, bio)) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900118 if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ret = ELEVATOR_BACK_MERGE;
Tejun Heo83096eb2009-05-07 22:24:39 +0900120 else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 ret = ELEVATOR_FRONT_MERGE;
122 }
123
124 return ret;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct elevator_type *elevator_find(const char *name)
128{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200129 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200131 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +0200132 if (!strcmp(e->elevator_name, name))
133 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Vasily Tarasova22b1692006-10-11 09:24:27 +0200136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static void elevator_put(struct elevator_type *e)
140{
141 module_put(e->elevator_owner);
142}
143
144static struct elevator_type *elevator_get(const char *name)
145{
Tejun Heo2824bc932005-10-20 10:56:41 +0200146 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200148 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200149
150 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100151 if (!e) {
152 char elv[ELV_NAME_MAX + strlen("-iosched")];
153
154 spin_unlock(&elv_list_lock);
155
156 if (!strcmp(name, "anticipatory"))
157 sprintf(elv, "as-iosched");
158 else
159 sprintf(elv, "%s-iosched", name);
160
maximilian attemse180f592008-07-01 09:42:47 +0200161 request_module("%s", elv);
Jens Axboee1640942008-02-19 10:20:37 +0100162 spin_lock(&elv_list_lock);
163 e = elevator_find(name);
164 }
165
Tejun Heo2824bc932005-10-20 10:56:41 +0200166 if (e && !try_module_get(e->elevator_owner))
167 e = NULL;
168
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200169 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return e;
172}
173
Jens Axboe165125e2007-07-24 09:28:11 +0200174static void *elevator_init_queue(struct request_queue *q,
175 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jens Axboebb37b942006-12-01 10:42:33 +0100177 return eq->ops->elevator_init_fn(q);
Jens Axboebc1c1162006-06-08 08:49:06 +0200178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jens Axboe165125e2007-07-24 09:28:11 +0200180static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
Jens Axboebc1c1162006-06-08 08:49:06 +0200181 void *data)
182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200184 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static char chosen_elevator[16];
188
Nate Diller5f003972006-01-24 10:07:58 +0100189static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100191 /*
192 * Be backwards-compatible with previous kernels, so users
193 * won't get the wrong elevator.
194 */
Nate Diller5f003972006-01-24 10:07:58 +0100195 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100196 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100197 else
Nate Diller5f003972006-01-24 10:07:58 +0100198 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800199 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202__setup("elevator=", elevator_setup);
203
Al Viro3d1ab402006-03-18 18:35:43 -0500204static struct kobj_type elv_ktype;
205
Jens Axboeb374d182008-10-31 10:05:07 +0100206static struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200207 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500208{
Jens Axboeb374d182008-10-31 10:05:07 +0100209 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200210 int i;
211
Jens Axboeb374d182008-10-31 10:05:07 +0100212 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200213 if (unlikely(!eq))
214 goto err;
215
Jens Axboe98170642006-07-28 09:23:08 +0200216 eq->ops = &e->ops;
217 eq->elevator_type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700218 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200219 mutex_init(&eq->sysfs_lock);
220
Jens Axboeb5deef92006-07-19 23:39:40 +0200221 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
222 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200223 if (!eq->hash)
224 goto err;
225
226 for (i = 0; i < ELV_HASH_ENTRIES; i++)
227 INIT_HLIST_HEAD(&eq->hash[i]);
228
Al Viro3d1ab402006-03-18 18:35:43 -0500229 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200230err:
231 kfree(eq);
232 elevator_put(e);
233 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500234}
235
236static void elevator_release(struct kobject *kobj)
237{
Jens Axboeb374d182008-10-31 10:05:07 +0100238 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200239
Jens Axboeb374d182008-10-31 10:05:07 +0100240 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500241 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200242 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500243 kfree(e);
244}
245
Jens Axboe165125e2007-07-24 09:28:11 +0200246int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct elevator_type *e = NULL;
249 struct elevator_queue *eq;
250 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200251 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Tejun Heocb98fc82005-10-28 08:29:39 +0200253 INIT_LIST_HEAD(&q->queue_head);
254 q->last_merge = NULL;
255 q->end_sector = 0;
256 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200257
Jens Axboe4eb166d2008-02-01 00:37:27 +0100258 if (name) {
259 e = elevator_get(name);
260 if (!e)
261 return -EINVAL;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jens Axboe4eb166d2008-02-01 00:37:27 +0100264 if (!e && *chosen_elevator) {
265 e = elevator_get(chosen_elevator);
266 if (!e)
267 printk(KERN_ERR "I/O scheduler %s not found\n",
268 chosen_elevator);
269 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100270
Jens Axboe4eb166d2008-02-01 00:37:27 +0100271 if (!e) {
272 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
273 if (!e) {
274 printk(KERN_ERR
275 "Default I/O scheduler not found. " \
276 "Using noop.\n");
277 e = elevator_get("noop");
278 }
Nate Diller5f003972006-01-24 10:07:58 +0100279 }
280
Jens Axboeb5deef92006-07-19 23:39:40 +0200281 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500282 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jens Axboebc1c1162006-06-08 08:49:06 +0200285 data = elevator_init_queue(q, eq);
286 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500287 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200288 return -ENOMEM;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jens Axboebc1c1162006-06-08 08:49:06 +0200291 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return ret;
293}
Jens Axboe2e662b62006-07-13 11:55:04 +0200294EXPORT_SYMBOL(elevator_init);
295
Jens Axboeb374d182008-10-31 10:05:07 +0100296void elevator_exit(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Al Viro3d1ab402006-03-18 18:35:43 -0500298 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (e->ops->elevator_exit_fn)
300 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500301 e->ops = NULL;
302 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Al Viro3d1ab402006-03-18 18:35:43 -0500304 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
Jens Axboe2e662b62006-07-13 11:55:04 +0200306EXPORT_SYMBOL(elevator_exit);
307
Jens Axboe98170642006-07-28 09:23:08 +0200308static inline void __elv_rqhash_del(struct request *rq)
309{
310 hlist_del_init(&rq->hash);
311}
312
Jens Axboe165125e2007-07-24 09:28:11 +0200313static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200314{
315 if (ELV_ON_HASH(rq))
316 __elv_rqhash_del(rq);
317}
318
Jens Axboe165125e2007-07-24 09:28:11 +0200319static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200320{
Jens Axboeb374d182008-10-31 10:05:07 +0100321 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200322
323 BUG_ON(ELV_ON_HASH(rq));
324 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
325}
326
Jens Axboe165125e2007-07-24 09:28:11 +0200327static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200328{
329 __elv_rqhash_del(rq);
330 elv_rqhash_add(q, rq);
331}
332
Jens Axboe165125e2007-07-24 09:28:11 +0200333static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200334{
Jens Axboeb374d182008-10-31 10:05:07 +0100335 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200336 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
337 struct hlist_node *entry, *next;
338 struct request *rq;
339
340 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
341 BUG_ON(!ELV_ON_HASH(rq));
342
343 if (unlikely(!rq_mergeable(rq))) {
344 __elv_rqhash_del(rq);
345 continue;
346 }
347
348 if (rq_hash_key(rq) == offset)
349 return rq;
350 }
351
352 return NULL;
353}
354
Tejun Heo8922e162005-10-20 16:23:44 +0200355/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200356 * RB-tree support functions for inserting/lookup/removal of requests
357 * in a sorted RB tree.
358 */
359struct request *elv_rb_add(struct rb_root *root, struct request *rq)
360{
361 struct rb_node **p = &root->rb_node;
362 struct rb_node *parent = NULL;
363 struct request *__rq;
364
365 while (*p) {
366 parent = *p;
367 __rq = rb_entry(parent, struct request, rb_node);
368
Tejun Heo83096eb2009-05-07 22:24:39 +0900369 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200370 p = &(*p)->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900371 else if (blk_rq_pos(rq) > blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200372 p = &(*p)->rb_right;
373 else
374 return __rq;
375 }
376
377 rb_link_node(&rq->rb_node, parent, p);
378 rb_insert_color(&rq->rb_node, root);
379 return NULL;
380}
Jens Axboe2e662b62006-07-13 11:55:04 +0200381EXPORT_SYMBOL(elv_rb_add);
382
383void elv_rb_del(struct rb_root *root, struct request *rq)
384{
385 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
386 rb_erase(&rq->rb_node, root);
387 RB_CLEAR_NODE(&rq->rb_node);
388}
Jens Axboe2e662b62006-07-13 11:55:04 +0200389EXPORT_SYMBOL(elv_rb_del);
390
391struct request *elv_rb_find(struct rb_root *root, sector_t sector)
392{
393 struct rb_node *n = root->rb_node;
394 struct request *rq;
395
396 while (n) {
397 rq = rb_entry(n, struct request, rb_node);
398
Tejun Heo83096eb2009-05-07 22:24:39 +0900399 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200400 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900401 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200402 n = n->rb_right;
403 else
404 return rq;
405 }
406
407 return NULL;
408}
Jens Axboe2e662b62006-07-13 11:55:04 +0200409EXPORT_SYMBOL(elv_rb_find);
410
411/*
Tejun Heo8922e162005-10-20 16:23:44 +0200412 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200413 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200414 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200415 */
Jens Axboe165125e2007-07-24 09:28:11 +0200416void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200417{
418 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200419 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100420 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200421
Tejun Heo06b86242005-10-20 16:46:23 +0200422 if (q->last_merge == rq)
423 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200424
425 elv_rqhash_del(q, rq);
426
Tejun Heo15853af2005-11-10 08:52:05 +0100427 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200428
Jens Axboe1b47f532005-10-20 16:37:00 +0200429 boundary = q->end_sector;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100430 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200431 list_for_each_prev(entry, &q->queue_head) {
432 struct request *pos = list_entry_rq(entry);
433
David Woodhousee17fc0a2008-08-09 16:42:20 +0100434 if (blk_discard_rq(rq) != blk_discard_rq(pos))
435 break;
Jens Axboe783660b2007-01-19 11:27:47 +1100436 if (rq_data_dir(rq) != rq_data_dir(pos))
437 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100438 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200439 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900440 if (blk_rq_pos(rq) >= boundary) {
441 if (blk_rq_pos(pos) < boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200442 continue;
443 } else {
Tejun Heo83096eb2009-05-07 22:24:39 +0900444 if (blk_rq_pos(pos) >= boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200445 break;
446 }
Tejun Heo83096eb2009-05-07 22:24:39 +0900447 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
Tejun Heo8922e162005-10-20 16:23:44 +0200448 break;
449 }
450
451 list_add(&rq->queuelist, entry);
452}
Jens Axboe2e662b62006-07-13 11:55:04 +0200453EXPORT_SYMBOL(elv_dispatch_sort);
454
Jens Axboe98170642006-07-28 09:23:08 +0200455/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200456 * Insert rq into dispatch queue of q. Queue lock must be held on
457 * entry. rq is added to the back of the dispatch queue. To be used by
458 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200459 */
460void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
461{
462 if (q->last_merge == rq)
463 q->last_merge = NULL;
464
465 elv_rqhash_del(q, rq);
466
467 q->nr_sorted--;
468
469 q->end_sector = rq_end_sector(rq);
470 q->boundary_rq = rq;
471 list_add_tail(&rq->queuelist, &q->queue_head);
472}
Jens Axboe2e662b62006-07-13 11:55:04 +0200473EXPORT_SYMBOL(elv_dispatch_add_tail);
474
Jens Axboe165125e2007-07-24 09:28:11 +0200475int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jens Axboeb374d182008-10-31 10:05:07 +0100477 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200478 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200479 int ret;
480
Jens Axboe98170642006-07-28 09:23:08 +0200481 /*
482 * First try one-hit cache.
483 */
Tejun Heo06b86242005-10-20 16:46:23 +0200484 if (q->last_merge) {
485 ret = elv_try_merge(q->last_merge, bio);
486 if (ret != ELEVATOR_NO_MERGE) {
487 *req = q->last_merge;
488 return ret;
489 }
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200492 if (blk_queue_nomerges(q))
493 return ELEVATOR_NO_MERGE;
494
Jens Axboe98170642006-07-28 09:23:08 +0200495 /*
496 * See if our hash lookup can find a potential backmerge.
497 */
498 __rq = elv_rqhash_find(q, bio->bi_sector);
499 if (__rq && elv_rq_merge_ok(__rq, bio)) {
500 *req = __rq;
501 return ELEVATOR_BACK_MERGE;
502 }
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (e->ops->elevator_merge_fn)
505 return e->ops->elevator_merge_fn(q, req, bio);
506
507 return ELEVATOR_NO_MERGE;
508}
509
Jens Axboe165125e2007-07-24 09:28:11 +0200510void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Jens Axboeb374d182008-10-31 10:05:07 +0100512 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200515 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200516
Jens Axboe2e662b62006-07-13 11:55:04 +0200517 if (type == ELEVATOR_BACK_MERGE)
518 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200519
Tejun Heo06b86242005-10-20 16:46:23 +0200520 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Jens Axboe165125e2007-07-24 09:28:11 +0200523void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct request *next)
525{
Jens Axboeb374d182008-10-31 10:05:07 +0100526 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (e->ops->elevator_merge_req_fn)
529 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200530
Jens Axboe98170642006-07-28 09:23:08 +0200531 elv_rqhash_reposition(q, rq);
532 elv_rqhash_del(q, next);
533
534 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200535 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Jens Axboe165125e2007-07-24 09:28:11 +0200538void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * it already went through dequeue, we need to decrement the
542 * in_flight count again
543 */
Tejun Heo8922e162005-10-20 16:23:44 +0200544 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200545 q->in_flight[rq_is_sync(rq)]--;
Jens Axboecad97512007-01-14 22:26:09 +1100546 if (blk_sorted_rq(rq))
547 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Jens Axboe4aff5e22006-08-10 08:44:47 +0200550 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Tejun Heo30e96562006-02-08 01:01:31 -0800552 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Jerome Marchand26308ea2009-03-27 10:31:51 +0100555void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100556{
557 static int printed;
558 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
559 ;
560 if (q->nr_sorted == 0)
561 return;
562 if (printed++ < 10) {
563 printk(KERN_ERR "%s: forced dispatching is broken "
564 "(nr_sorted=%u), please report this\n",
565 q->elevator->elevator_type->elevator_name, q->nr_sorted);
566 }
567}
568
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100569/*
570 * Call with queue lock held, interrupts disabled
571 */
Jens Axboef600abe2009-04-08 14:22:01 +0200572void elv_quiesce_start(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100573{
Martin K. Petersencd43e262009-05-22 17:17:52 -0400574 if (!q->elevator)
575 return;
576
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100577 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
578
579 /*
580 * make sure we don't have any requests in flight
581 */
582 elv_drain_elevator(q);
583 while (q->rq.elvpriv) {
Tejun Heoa7f55792009-04-23 11:05:17 +0900584 __blk_run_queue(q);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100585 spin_unlock_irq(q->queue_lock);
586 msleep(10);
587 spin_lock_irq(q->queue_lock);
588 elv_drain_elevator(q);
589 }
590}
591
Jens Axboef600abe2009-04-08 14:22:01 +0200592void elv_quiesce_end(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100593{
594 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
595}
596
Jens Axboe165125e2007-07-24 09:28:11 +0200597void elv_insert(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Tejun Heo797e7db2006-01-06 09:51:03 +0100599 struct list_head *pos;
600 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200601 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100602
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100603 trace_block_rq_insert(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 rq->q = q;
606
Tejun Heo8922e162005-10-20 16:23:44 +0200607 switch (where) {
608 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200609 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200610
611 list_add(&rq->queuelist, &q->queue_head);
612 break;
613
614 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200615 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100616 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200617 list_add_tail(&rq->queuelist, &q->queue_head);
618 /*
619 * We kick the queue here for the following reasons.
620 * - The elevator might have returned NULL previously
621 * to delay requests and returned them now. As the
622 * queue wasn't empty before this request, ll_rw_blk
623 * won't run the queue on return, resulting in hang.
624 * - Usually, back inserted requests won't be merged
625 * with anything. There's no point in delaying queue
626 * processing.
627 */
Tejun Heoa7f55792009-04-23 11:05:17 +0900628 __blk_run_queue(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200629 break;
630
631 case ELEVATOR_INSERT_SORT:
David Woodhousee17fc0a2008-08-09 16:42:20 +0100632 BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200633 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100634 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200635 if (rq_mergeable(rq)) {
636 elv_rqhash_add(q, rq);
637 if (!q->last_merge)
638 q->last_merge = rq;
639 }
640
Tejun Heoca235092005-11-01 17:23:49 +0900641 /*
642 * Some ioscheds (cfq) run q->request_fn directly, so
643 * rq cannot be accessed after calling
644 * elevator_add_req_fn.
645 */
646 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200647 break;
648
Tejun Heo797e7db2006-01-06 09:51:03 +0100649 case ELEVATOR_INSERT_REQUEUE:
650 /*
651 * If ordered flush isn't in progress, we do front
652 * insertion; otherwise, requests should be requeued
653 * in ordseq order.
654 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200655 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100656
Linas Vepstas95543172007-01-23 19:40:54 +0100657 /*
658 * Most requeues happen because of a busy condition,
659 * don't force unplug of the queue for that case.
660 */
661 unplug_it = 0;
662
Tejun Heo797e7db2006-01-06 09:51:03 +0100663 if (q->ordseq == 0) {
664 list_add(&rq->queuelist, &q->queue_head);
665 break;
666 }
667
668 ordseq = blk_ordered_req_seq(rq);
669
670 list_for_each(pos, &q->queue_head) {
671 struct request *pos_rq = list_entry_rq(pos);
672 if (ordseq <= blk_ordered_req_seq(pos_rq))
673 break;
674 }
675
676 list_add_tail(&rq->queuelist, pos);
677 break;
678
Tejun Heo8922e162005-10-20 16:23:44 +0200679 default:
680 printk(KERN_ERR "%s: bad insertion point %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700681 __func__, where);
Tejun Heo8922e162005-10-20 16:23:44 +0200682 BUG();
683 }
684
Jens Axboedac07ec2006-05-11 08:20:16 +0200685 if (unplug_it && blk_queue_plugged(q)) {
Jens Axboe1faa16d2009-04-06 14:48:01 +0200686 int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC]
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200687 - queue_in_flight(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200688
689 if (nrq >= q->unplug_thresh)
690 __generic_unplug_device(q);
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Jens Axboe165125e2007-07-24 09:28:11 +0200694void __elv_add_request(struct request_queue *q, struct request *rq, int where,
Tejun Heo30e96562006-02-08 01:01:31 -0800695 int plug)
696{
697 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200698 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800699
Jens Axboe4aff5e22006-08-10 08:44:47 +0200700 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800701 /*
702 * toggle ordered color
703 */
704 if (blk_barrier_rq(rq))
705 q->ordcolor ^= 1;
706
707 /*
708 * barriers implicitly indicate back insertion
709 */
710 if (where == ELEVATOR_INSERT_SORT)
711 where = ELEVATOR_INSERT_BACK;
712
713 /*
714 * this request is scheduling boundary, update
715 * end_sector
716 */
David Woodhousee17fc0a2008-08-09 16:42:20 +0100717 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800718 q->end_sector = rq_end_sector(rq);
719 q->boundary_rq = rq;
720 }
Jens Axboe4eb166d2008-02-01 00:37:27 +0100721 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
722 where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800723 where = ELEVATOR_INSERT_BACK;
724
725 if (plug)
726 blk_plug_device(q);
727
728 elv_insert(q, rq, where);
729}
Jens Axboe2e662b62006-07-13 11:55:04 +0200730EXPORT_SYMBOL(__elv_add_request);
731
Jens Axboe165125e2007-07-24 09:28:11 +0200732void elv_add_request(struct request_queue *q, struct request *rq, int where,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int plug)
734{
735 unsigned long flags;
736
737 spin_lock_irqsave(q->queue_lock, flags);
738 __elv_add_request(q, rq, where, plug);
739 spin_unlock_irqrestore(q->queue_lock, flags);
740}
Jens Axboe2e662b62006-07-13 11:55:04 +0200741EXPORT_SYMBOL(elv_add_request);
742
Jens Axboe165125e2007-07-24 09:28:11 +0200743int elv_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Jens Axboeb374d182008-10-31 10:05:07 +0100745 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Tejun Heo8922e162005-10-20 16:23:44 +0200747 if (!list_empty(&q->queue_head))
748 return 0;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (e->ops->elevator_queue_empty_fn)
751 return e->ops->elevator_queue_empty_fn(q);
752
Tejun Heo8922e162005-10-20 16:23:44 +0200753 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Jens Axboe2e662b62006-07-13 11:55:04 +0200755EXPORT_SYMBOL(elv_queue_empty);
756
Jens Axboe165125e2007-07-24 09:28:11 +0200757struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Jens Axboeb374d182008-10-31 10:05:07 +0100759 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (e->ops->elevator_latter_req_fn)
762 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return NULL;
764}
765
Jens Axboe165125e2007-07-24 09:28:11 +0200766struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Jens Axboeb374d182008-10-31 10:05:07 +0100768 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (e->ops->elevator_former_req_fn)
771 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return NULL;
773}
774
Jens Axboe165125e2007-07-24 09:28:11 +0200775int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Jens Axboeb374d182008-10-31 10:05:07 +0100777 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200780 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 rq->elevator_private = NULL;
783 return 0;
784}
785
Jens Axboe165125e2007-07-24 09:28:11 +0200786void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jens Axboeb374d182008-10-31 10:05:07 +0100788 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100791 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Jens Axboe165125e2007-07-24 09:28:11 +0200794int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Jens Axboeb374d182008-10-31 10:05:07 +0100796 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200799 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 return ELV_MQUEUE_MAY;
802}
803
Mike Anderson11914a52008-09-13 20:31:27 +0200804void elv_abort_queue(struct request_queue *q)
805{
806 struct request *rq;
807
808 while (!list_empty(&q->queue_head)) {
809 rq = list_entry_rq(q->queue_head.next);
810 rq->cmd_flags |= REQ_QUIET;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100811 trace_block_rq_abort(q, rq);
Kiyoshi Ueda53c663c2009-06-02 08:44:01 +0200812 /*
813 * Mark this request as started so we don't trigger
814 * any debug logic in the end I/O path.
815 */
816 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900817 __blk_end_request_all(rq, -EIO);
Mike Anderson11914a52008-09-13 20:31:27 +0200818 }
819}
820EXPORT_SYMBOL(elv_abort_queue);
821
Jens Axboe165125e2007-07-24 09:28:11 +0200822void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jens Axboeb374d182008-10-31 10:05:07 +0100824 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
827 * request is released from the driver, io must be done
828 */
Tejun Heo8922e162005-10-20 16:23:44 +0200829 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200830 q->in_flight[rq_is_sync(rq)]--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100831 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
832 e->ops->elevator_completed_req_fn(q, rq);
833 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100834
Tejun Heo1bc691d2006-01-12 15:39:26 +0100835 /*
836 * Check if the queue is waiting for fs requests to be
837 * drained for flush sequence.
838 */
839 if (unlikely(q->ordseq)) {
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900840 struct request *next = NULL;
841
842 if (!list_empty(&q->queue_head))
843 next = list_entry_rq(q->queue_head.next);
844
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200845 if (!queue_in_flight(q) &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100846 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
Tejun Heo8f11b3e2008-11-28 13:32:05 +0900847 (!next || blk_ordered_req_seq(next) > QUEUE_ORDSEQ_DRAIN)) {
Tejun Heo797e7db2006-01-06 09:51:03 +0100848 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +0900849 __blk_run_queue(q);
Tejun Heo797e7db2006-01-06 09:51:03 +0100850 }
Tejun Heo8922e162005-10-20 16:23:44 +0200851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Al Viro3d1ab402006-03-18 18:35:43 -0500854#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
855
856static ssize_t
857elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
858{
Al Viro3d1ab402006-03-18 18:35:43 -0500859 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100860 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500861 ssize_t error;
862
863 if (!entry->show)
864 return -EIO;
865
Jens Axboeb374d182008-10-31 10:05:07 +0100866 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500867 mutex_lock(&e->sysfs_lock);
868 error = e->ops ? entry->show(e, page) : -ENOENT;
869 mutex_unlock(&e->sysfs_lock);
870 return error;
871}
872
873static ssize_t
874elv_attr_store(struct kobject *kobj, struct attribute *attr,
875 const char *page, size_t length)
876{
Al Viro3d1ab402006-03-18 18:35:43 -0500877 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100878 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500879 ssize_t error;
880
881 if (!entry->store)
882 return -EIO;
883
Jens Axboeb374d182008-10-31 10:05:07 +0100884 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500885 mutex_lock(&e->sysfs_lock);
886 error = e->ops ? entry->store(e, page, length) : -ENOENT;
887 mutex_unlock(&e->sysfs_lock);
888 return error;
889}
890
891static struct sysfs_ops elv_sysfs_ops = {
892 .show = elv_attr_show,
893 .store = elv_attr_store,
894};
895
896static struct kobj_type elv_ktype = {
897 .sysfs_ops = &elv_sysfs_ops,
898 .release = elevator_release,
899};
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901int elv_register_queue(struct request_queue *q)
902{
Jens Axboeb374d182008-10-31 10:05:07 +0100903 struct elevator_queue *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500904 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700906 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500907 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -0500908 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500909 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500910 while (attr->attr.name) {
911 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500912 break;
Al Viroe572ec72006-03-18 22:27:18 -0500913 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500914 }
915 }
916 kobject_uevent(&e->kobj, KOBJ_ADD);
917 }
918 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Jens Axboeb374d182008-10-31 10:05:07 +0100921static void __elv_unregister_queue(struct elevator_queue *e)
Jens Axboebc1c1162006-06-08 08:49:06 +0200922{
923 kobject_uevent(&e->kobj, KOBJ_REMOVE);
924 kobject_del(&e->kobj);
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927void elv_unregister_queue(struct request_queue *q)
928{
Jens Axboebc1c1162006-06-08 08:49:06 +0200929 if (q)
930 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Adrian Bunk2fdd82b2007-12-12 18:51:56 +0100933void elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100935 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200936
937 spin_lock(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +0100938 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200940 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Nate Diller5f003972006-01-24 10:07:58 +0100942 if (!strcmp(e->elevator_name, chosen_elevator) ||
943 (!*chosen_elevator &&
944 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100945 def = " (default)";
946
Jens Axboe4eb166d2008-02-01 00:37:27 +0100947 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
948 def);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950EXPORT_SYMBOL_GPL(elv_register);
951
952void elv_unregister(struct elevator_type *e)
953{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800954 struct task_struct *g, *p;
955
956 /*
957 * Iterate every thread in the process to remove the io contexts.
958 */
Al Viroe17a9482006-03-18 13:21:20 -0500959 if (e->ops.trim) {
960 read_lock(&tasklist_lock);
961 do_each_thread(g, p) {
962 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +0400963 if (p->io_context)
964 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -0500965 task_unlock(p);
966 } while_each_thread(g, p);
967 read_unlock(&tasklist_lock);
968 }
Christoph Hellwig83521d32005-10-30 15:01:39 -0800969
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200970 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200972 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974EXPORT_SYMBOL_GPL(elv_unregister);
975
976/*
977 * switch to new_e io scheduler. be careful not to introduce deadlocks -
978 * we don't free the old io scheduler, before we have allocated what we
979 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200980 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
Jens Axboe165125e2007-07-24 09:28:11 +0200982static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jens Axboeb374d182008-10-31 10:05:07 +0100984 struct elevator_queue *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +0200985 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Tejun Heocb98fc82005-10-28 08:29:39 +0200987 /*
988 * Allocate new elevator
989 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200990 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -0500992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Jens Axboebc1c1162006-06-08 08:49:06 +0200994 data = elevator_init_queue(q, e);
995 if (!data) {
996 kobject_put(&e->kobj);
997 return 0;
998 }
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001001 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001003 spin_lock_irq(q->queue_lock);
Jens Axboef600abe2009-04-08 14:22:01 +02001004 elv_quiesce_start(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001007 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 old_elevator = q->elevator;
1010
1011 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * attach and start new elevator
1013 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001014 elevator_attach(q, e, data);
1015
1016 spin_unlock_irq(q->queue_lock);
1017
1018 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (elv_register_queue(q))
1021 goto fail_register;
1022
1023 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001024 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
1026 elevator_exit(old_elevator);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001027 spin_lock_irq(q->queue_lock);
Jens Axboef600abe2009-04-08 14:22:01 +02001028 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001029 spin_unlock_irq(q->queue_lock);
1030
Alan D. Brunelle4722dc52008-05-27 14:55:00 +02001031 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
1032
Al Viro3d1ab402006-03-18 18:35:43 -05001033 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035fail_register:
1036 /*
1037 * switch failed, exit the new io scheduler and reattach the old
1038 * one again (along with re-adding the sysfs dir)
1039 */
1040 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 q->elevator = old_elevator;
1042 elv_register_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001043
1044 spin_lock_irq(q->queue_lock);
1045 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1046 spin_unlock_irq(q->queue_lock);
1047
Al Viro3d1ab402006-03-18 18:35:43 -05001048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Jens Axboe165125e2007-07-24 09:28:11 +02001051ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1052 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 char elevator_name[ELV_NAME_MAX];
1055 struct elevator_type *e;
1056
Martin K. Petersencd43e262009-05-22 17:17:52 -04001057 if (!q->elevator)
1058 return count;
1059
Li Zefanee2e9922008-10-14 08:49:56 +02001060 strlcpy(elevator_name, name, sizeof(elevator_name));
1061 strstrip(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 e = elevator_get(elevator_name);
1064 if (!e) {
1065 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1066 return -EINVAL;
1067 }
1068
Nate Diller2ca7d932005-10-30 15:02:24 -08001069 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1070 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Al Viro3d1ab402006-03-18 18:35:43 -05001074 if (!elevator_switch(q, e))
Jens Axboe4eb166d2008-02-01 00:37:27 +01001075 printk(KERN_ERR "elevator: switch to %s failed\n",
1076 elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return count;
1078}
1079
Jens Axboe165125e2007-07-24 09:28:11 +02001080ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Jens Axboeb374d182008-10-31 10:05:07 +01001082 struct elevator_queue *e = q->elevator;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001083 struct elevator_type *elv;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001084 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int len = 0;
1086
Martin K. Petersencd43e262009-05-22 17:17:52 -04001087 if (!q->elevator)
1088 return sprintf(name, "none\n");
1089
1090 elv = e->elevator_type;
1091
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001092 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001093 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!strcmp(elv->elevator_name, __e->elevator_name))
1095 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1096 else
1097 len += sprintf(name+len, "%s ", __e->elevator_name);
1098 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001099 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 len += sprintf(len+name, "\n");
1102 return len;
1103}
1104
Jens Axboe165125e2007-07-24 09:28:11 +02001105struct request *elv_rb_former_request(struct request_queue *q,
1106 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001107{
1108 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1109
1110 if (rbprev)
1111 return rb_entry_rq(rbprev);
1112
1113 return NULL;
1114}
Jens Axboe2e662b62006-07-13 11:55:04 +02001115EXPORT_SYMBOL(elv_rb_former_request);
1116
Jens Axboe165125e2007-07-24 09:28:11 +02001117struct request *elv_rb_latter_request(struct request_queue *q,
1118 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001119{
1120 struct rb_node *rbnext = rb_next(&rq->rb_node);
1121
1122 if (rbnext)
1123 return rb_entry_rq(rbnext);
1124
1125 return NULL;
1126}
Jens Axboe2e662b62006-07-13 11:55:04 +02001127EXPORT_SYMBOL(elv_rb_latter_request);