blob: 0d2db536c6a759c9c16cb2e7d24abe6561d00a54 [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 *
6 * 30042000 Jens Axboe <axboe@suse.de> :
7 *
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>
30#include <linux/config.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/compiler.h>
Tejun Heocb98fc82005-10-28 08:29:39 +020035#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38
39static DEFINE_SPINLOCK(elv_list_lock);
40static LIST_HEAD(elv_list);
41
42/*
43 * can we safely merge with this request?
44 */
45inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
46{
47 if (!rq_mergeable(rq))
48 return 0;
49
50 /*
51 * different data direction or already started, don't merge
52 */
53 if (bio_data_dir(bio) != rq_data_dir(rq))
54 return 0;
55
56 /*
57 * same device and no special stuff set, merge is ok
58 */
59 if (rq->rq_disk == bio->bi_bdev->bd_disk &&
60 !rq->waiting && !rq->special)
61 return 1;
62
63 return 0;
64}
65EXPORT_SYMBOL(elv_rq_merge_ok);
66
Coywolf Qi Hunt769db452005-12-28 10:55:49 +010067static inline int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int ret = ELEVATOR_NO_MERGE;
70
71 /*
72 * we can merge and sequence is ok, check if it's possible
73 */
74 if (elv_rq_merge_ok(__rq, bio)) {
75 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
76 ret = ELEVATOR_BACK_MERGE;
77 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
78 ret = ELEVATOR_FRONT_MERGE;
79 }
80
81 return ret;
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static struct elevator_type *elevator_find(const char *name)
85{
86 struct elevator_type *e = NULL;
87 struct list_head *entry;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 list_for_each(entry, &elv_list) {
90 struct elevator_type *__e;
91
92 __e = list_entry(entry, struct elevator_type, list);
93
94 if (!strcmp(__e->elevator_name, name)) {
95 e = __e;
96 break;
97 }
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return e;
101}
102
103static void elevator_put(struct elevator_type *e)
104{
105 module_put(e->elevator_owner);
106}
107
108static struct elevator_type *elevator_get(const char *name)
109{
Tejun Heo2824bc932005-10-20 10:56:41 +0200110 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Tejun Heo2824bc932005-10-20 10:56:41 +0200112 spin_lock_irq(&elv_list_lock);
113
114 e = elevator_find(name);
115 if (e && !try_module_get(e->elevator_owner))
116 e = NULL;
117
118 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return e;
121}
122
Al Viro3d1ab402006-03-18 18:35:43 -0500123static int elevator_attach(request_queue_t *q, struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int ret = 0;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 q->elevator = eq;
128
129 if (eq->ops->elevator_init_fn)
130 ret = eq->ops->elevator_init_fn(q, eq);
131
132 return ret;
133}
134
135static char chosen_elevator[16];
136
Nate Diller5f003972006-01-24 10:07:58 +0100137static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100139 /*
140 * Be backwards-compatible with previous kernels, so users
141 * won't get the wrong elevator.
142 */
Nate Diller5f003972006-01-24 10:07:58 +0100143 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100144 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100145 else
Nate Diller5f003972006-01-24 10:07:58 +0100146 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148}
149
150__setup("elevator=", elevator_setup);
151
Al Viro3d1ab402006-03-18 18:35:43 -0500152static struct kobj_type elv_ktype;
153
154static elevator_t *elevator_alloc(struct elevator_type *e)
155{
156 elevator_t *eq = kmalloc(sizeof(elevator_t), GFP_KERNEL);
157 if (eq) {
158 memset(eq, 0, sizeof(*eq));
159 eq->ops = &e->ops;
160 eq->elevator_type = e;
161 kobject_init(&eq->kobj);
162 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
163 eq->kobj.ktype = &elv_ktype;
164 mutex_init(&eq->sysfs_lock);
165 } else {
166 elevator_put(e);
167 }
168 return eq;
169}
170
171static void elevator_release(struct kobject *kobj)
172{
173 elevator_t *e = container_of(kobj, elevator_t, kobj);
174 elevator_put(e->elevator_type);
175 kfree(e);
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178int elevator_init(request_queue_t *q, char *name)
179{
180 struct elevator_type *e = NULL;
181 struct elevator_queue *eq;
182 int ret = 0;
183
Tejun Heocb98fc82005-10-28 08:29:39 +0200184 INIT_LIST_HEAD(&q->queue_head);
185 q->last_merge = NULL;
186 q->end_sector = 0;
187 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200188
Nate Diller5f003972006-01-24 10:07:58 +0100189 if (name && !(e = elevator_get(name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EINVAL;
191
Nate Diller248d5ca2006-01-24 10:09:14 +0100192 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
193 printk("I/O scheduler %s not found\n", chosen_elevator);
194
195 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
196 printk("Default I/O scheduler not found, using no-op\n");
197 e = elevator_get("noop");
Nate Diller5f003972006-01-24 10:07:58 +0100198 }
199
Al Viro3d1ab402006-03-18 18:35:43 -0500200 eq = elevator_alloc(e);
201 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Al Viro3d1ab402006-03-18 18:35:43 -0500204 ret = elevator_attach(q, eq);
205 if (ret)
206 kobject_put(&eq->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 return ret;
209}
210
211void elevator_exit(elevator_t *e)
212{
Al Viro3d1ab402006-03-18 18:35:43 -0500213 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (e->ops->elevator_exit_fn)
215 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500216 e->ops = NULL;
217 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Al Viro3d1ab402006-03-18 18:35:43 -0500219 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Tejun Heo8922e162005-10-20 16:23:44 +0200222/*
223 * Insert rq into dispatch queue of q. Queue lock must be held on
224 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
225 * appended to the dispatch queue. To be used by specific elevators.
226 */
Jens Axboe1b47f532005-10-20 16:37:00 +0200227void elv_dispatch_sort(request_queue_t *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200228{
229 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200230 struct list_head *entry;
231
Tejun Heo06b86242005-10-20 16:46:23 +0200232 if (q->last_merge == rq)
233 q->last_merge = NULL;
Tejun Heo15853af2005-11-10 08:52:05 +0100234 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200235
Jens Axboe1b47f532005-10-20 16:37:00 +0200236 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200237
Tejun Heo8922e162005-10-20 16:23:44 +0200238 list_for_each_prev(entry, &q->queue_head) {
239 struct request *pos = list_entry_rq(entry);
240
241 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
242 break;
243 if (rq->sector >= boundary) {
244 if (pos->sector < boundary)
245 continue;
246 } else {
247 if (pos->sector >= boundary)
248 break;
249 }
250 if (rq->sector >= pos->sector)
251 break;
252 }
253
254 list_add(&rq->queuelist, entry);
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
258{
259 elevator_t *e = q->elevator;
Tejun Heo06b86242005-10-20 16:46:23 +0200260 int ret;
261
262 if (q->last_merge) {
263 ret = elv_try_merge(q->last_merge, bio);
264 if (ret != ELEVATOR_NO_MERGE) {
265 *req = q->last_merge;
266 return ret;
267 }
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (e->ops->elevator_merge_fn)
271 return e->ops->elevator_merge_fn(q, req, bio);
272
273 return ELEVATOR_NO_MERGE;
274}
275
276void elv_merged_request(request_queue_t *q, struct request *rq)
277{
278 elevator_t *e = q->elevator;
279
280 if (e->ops->elevator_merged_fn)
281 e->ops->elevator_merged_fn(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200282
283 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286void elv_merge_requests(request_queue_t *q, struct request *rq,
287 struct request *next)
288{
289 elevator_t *e = q->elevator;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (e->ops->elevator_merge_req_fn)
292 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo15853af2005-11-10 08:52:05 +0100293 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200294
295 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Tejun Heo8922e162005-10-20 16:23:44 +0200298void elv_requeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 elevator_t *e = q->elevator;
301
302 /*
303 * it already went through dequeue, we need to decrement the
304 * in_flight count again
305 */
Tejun Heo8922e162005-10-20 16:23:44 +0200306 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 q->in_flight--;
Tejun Heo8922e162005-10-20 16:23:44 +0200308 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
309 e->ops->elevator_deactivate_req_fn(q, rq);
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 rq->flags &= ~REQ_STARTED;
313
Tejun Heo30e96562006-02-08 01:01:31 -0800314 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Tejun Heo15853af2005-11-10 08:52:05 +0100317static void elv_drain_elevator(request_queue_t *q)
318{
319 static int printed;
320 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
321 ;
322 if (q->nr_sorted == 0)
323 return;
324 if (printed++ < 10) {
325 printk(KERN_ERR "%s: forced dispatching is broken "
326 "(nr_sorted=%u), please report this\n",
327 q->elevator->elevator_type->elevator_name, q->nr_sorted);
328 }
329}
330
Tejun Heo30e96562006-02-08 01:01:31 -0800331void elv_insert(request_queue_t *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Tejun Heo797e7db2006-01-06 09:51:03 +0100333 struct list_head *pos;
334 unsigned ordseq;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 rq->q = q;
337
Tejun Heo8922e162005-10-20 16:23:44 +0200338 switch (where) {
339 case ELEVATOR_INSERT_FRONT:
340 rq->flags |= REQ_SOFTBARRIER;
341
342 list_add(&rq->queuelist, &q->queue_head);
343 break;
344
345 case ELEVATOR_INSERT_BACK:
346 rq->flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100347 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200348 list_add_tail(&rq->queuelist, &q->queue_head);
349 /*
350 * We kick the queue here for the following reasons.
351 * - The elevator might have returned NULL previously
352 * to delay requests and returned them now. As the
353 * queue wasn't empty before this request, ll_rw_blk
354 * won't run the queue on return, resulting in hang.
355 * - Usually, back inserted requests won't be merged
356 * with anything. There's no point in delaying queue
357 * processing.
358 */
359 blk_remove_plug(q);
360 q->request_fn(q);
361 break;
362
363 case ELEVATOR_INSERT_SORT:
364 BUG_ON(!blk_fs_request(rq));
365 rq->flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100366 q->nr_sorted++;
Tejun Heo06b86242005-10-20 16:46:23 +0200367 if (q->last_merge == NULL && rq_mergeable(rq))
368 q->last_merge = rq;
Tejun Heoca235092005-11-01 17:23:49 +0900369 /*
370 * Some ioscheds (cfq) run q->request_fn directly, so
371 * rq cannot be accessed after calling
372 * elevator_add_req_fn.
373 */
374 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200375 break;
376
Tejun Heo797e7db2006-01-06 09:51:03 +0100377 case ELEVATOR_INSERT_REQUEUE:
378 /*
379 * If ordered flush isn't in progress, we do front
380 * insertion; otherwise, requests should be requeued
381 * in ordseq order.
382 */
383 rq->flags |= REQ_SOFTBARRIER;
384
385 if (q->ordseq == 0) {
386 list_add(&rq->queuelist, &q->queue_head);
387 break;
388 }
389
390 ordseq = blk_ordered_req_seq(rq);
391
392 list_for_each(pos, &q->queue_head) {
393 struct request *pos_rq = list_entry_rq(pos);
394 if (ordseq <= blk_ordered_req_seq(pos_rq))
395 break;
396 }
397
398 list_add_tail(&rq->queuelist, pos);
399 break;
400
Tejun Heo8922e162005-10-20 16:23:44 +0200401 default:
402 printk(KERN_ERR "%s: bad insertion point %d\n",
403 __FUNCTION__, where);
404 BUG();
405 }
406
407 if (blk_queue_plugged(q)) {
408 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
409 - q->in_flight;
410
411 if (nrq >= q->unplug_thresh)
412 __generic_unplug_device(q);
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Tejun Heo30e96562006-02-08 01:01:31 -0800416void __elv_add_request(request_queue_t *q, struct request *rq, int where,
417 int plug)
418{
419 if (q->ordcolor)
420 rq->flags |= REQ_ORDERED_COLOR;
421
422 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
423 /*
424 * toggle ordered color
425 */
426 if (blk_barrier_rq(rq))
427 q->ordcolor ^= 1;
428
429 /*
430 * barriers implicitly indicate back insertion
431 */
432 if (where == ELEVATOR_INSERT_SORT)
433 where = ELEVATOR_INSERT_BACK;
434
435 /*
436 * this request is scheduling boundary, update
437 * end_sector
438 */
439 if (blk_fs_request(rq)) {
440 q->end_sector = rq_end_sector(rq);
441 q->boundary_rq = rq;
442 }
443 } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
444 where = ELEVATOR_INSERT_BACK;
445
446 if (plug)
447 blk_plug_device(q);
448
449 elv_insert(q, rq, where);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452void elv_add_request(request_queue_t *q, struct request *rq, int where,
453 int plug)
454{
455 unsigned long flags;
456
457 spin_lock_irqsave(q->queue_lock, flags);
458 __elv_add_request(q, rq, where, plug);
459 spin_unlock_irqrestore(q->queue_lock, flags);
460}
461
462static inline struct request *__elv_next_request(request_queue_t *q)
463{
Tejun Heo8922e162005-10-20 16:23:44 +0200464 struct request *rq;
465
Tejun Heo797e7db2006-01-06 09:51:03 +0100466 while (1) {
467 while (!list_empty(&q->queue_head)) {
468 rq = list_entry_rq(q->queue_head.next);
469 if (blk_do_ordered(q, &rq))
470 return rq;
471 }
Tejun Heo8922e162005-10-20 16:23:44 +0200472
Tejun Heo797e7db2006-01-06 09:51:03 +0100473 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
474 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478struct request *elv_next_request(request_queue_t *q)
479{
480 struct request *rq;
481 int ret;
482
483 while ((rq = __elv_next_request(q)) != NULL) {
Tejun Heo8922e162005-10-20 16:23:44 +0200484 if (!(rq->flags & REQ_STARTED)) {
485 elevator_t *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Tejun Heo8922e162005-10-20 16:23:44 +0200487 /*
488 * This is the first time the device driver
489 * sees this request (possibly after
490 * requeueing). Notify IO scheduler.
491 */
492 if (blk_sorted_rq(rq) &&
493 e->ops->elevator_activate_req_fn)
494 e->ops->elevator_activate_req_fn(q, rq);
495
496 /*
497 * just mark as started even if we don't start
498 * it, a request that has been delayed should
499 * not be passed by new incoming requests
500 */
501 rq->flags |= REQ_STARTED;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Tejun Heo8922e162005-10-20 16:23:44 +0200504 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200505 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200506 q->boundary_rq = NULL;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
510 break;
511
512 ret = q->prep_rq_fn(q, rq);
513 if (ret == BLKPREP_OK) {
514 break;
515 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500516 /*
517 * the request may have been (partially) prepped.
518 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200519 * avoid resource deadlock. REQ_STARTED will
520 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rq = NULL;
523 break;
524 } else if (ret == BLKPREP_KILL) {
525 int nr_bytes = rq->hard_nr_sectors << 9;
526
527 if (!nr_bytes)
528 nr_bytes = rq->data_len;
529
530 blkdev_dequeue_request(rq);
531 rq->flags |= REQ_QUIET;
532 end_that_request_chunk(rq, 0, nr_bytes);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100533 end_that_request_last(rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else {
535 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
536 ret);
537 break;
538 }
539 }
540
541 return rq;
542}
543
Tejun Heo8922e162005-10-20 16:23:44 +0200544void elv_dequeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Tejun Heo8922e162005-10-20 16:23:44 +0200546 BUG_ON(list_empty(&rq->queuelist));
547
548 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /*
551 * the time frame between a request being removed from the lists
552 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200553 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555 if (blk_account_rq(rq))
556 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559int elv_queue_empty(request_queue_t *q)
560{
561 elevator_t *e = q->elevator;
562
Tejun Heo8922e162005-10-20 16:23:44 +0200563 if (!list_empty(&q->queue_head))
564 return 0;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (e->ops->elevator_queue_empty_fn)
567 return e->ops->elevator_queue_empty_fn(q);
568
Tejun Heo8922e162005-10-20 16:23:44 +0200569 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572struct request *elv_latter_request(request_queue_t *q, struct request *rq)
573{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 elevator_t *e = q->elevator;
575
576 if (e->ops->elevator_latter_req_fn)
577 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579}
580
581struct request *elv_former_request(request_queue_t *q, struct request *rq)
582{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 elevator_t *e = q->elevator;
584
585 if (e->ops->elevator_former_req_fn)
586 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return NULL;
588}
589
Jens Axboe22e2c502005-06-27 10:55:12 +0200590int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -0400591 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 elevator_t *e = q->elevator;
594
595 if (e->ops->elevator_set_req_fn)
Jens Axboe22e2c502005-06-27 10:55:12 +0200596 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 rq->elevator_private = NULL;
599 return 0;
600}
601
602void elv_put_request(request_queue_t *q, struct request *rq)
603{
604 elevator_t *e = q->elevator;
605
606 if (e->ops->elevator_put_req_fn)
607 e->ops->elevator_put_req_fn(q, rq);
608}
609
Jens Axboe22e2c502005-06-27 10:55:12 +0200610int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 elevator_t *e = q->elevator;
613
614 if (e->ops->elevator_may_queue_fn)
Jens Axboe22e2c502005-06-27 10:55:12 +0200615 return e->ops->elevator_may_queue_fn(q, rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 return ELV_MQUEUE_MAY;
618}
619
620void elv_completed_request(request_queue_t *q, struct request *rq)
621{
622 elevator_t *e = q->elevator;
623
624 /*
625 * request is released from the driver, io must be done
626 */
Tejun Heo8922e162005-10-20 16:23:44 +0200627 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100629 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
630 e->ops->elevator_completed_req_fn(q, rq);
631 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100632
Tejun Heo1bc691d2006-01-12 15:39:26 +0100633 /*
634 * Check if the queue is waiting for fs requests to be
635 * drained for flush sequence.
636 */
637 if (unlikely(q->ordseq)) {
638 struct request *first_rq = list_entry_rq(q->queue_head.next);
639 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100640 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
641 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
642 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
643 q->request_fn(q);
644 }
Tejun Heo8922e162005-10-20 16:23:44 +0200645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Al Viro3d1ab402006-03-18 18:35:43 -0500648#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
649
650static ssize_t
651elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
652{
653 elevator_t *e = container_of(kobj, elevator_t, kobj);
654 struct elv_fs_entry *entry = to_elv(attr);
655 ssize_t error;
656
657 if (!entry->show)
658 return -EIO;
659
660 mutex_lock(&e->sysfs_lock);
661 error = e->ops ? entry->show(e, page) : -ENOENT;
662 mutex_unlock(&e->sysfs_lock);
663 return error;
664}
665
666static ssize_t
667elv_attr_store(struct kobject *kobj, struct attribute *attr,
668 const char *page, size_t length)
669{
670 elevator_t *e = container_of(kobj, elevator_t, kobj);
671 struct elv_fs_entry *entry = to_elv(attr);
672 ssize_t error;
673
674 if (!entry->store)
675 return -EIO;
676
677 mutex_lock(&e->sysfs_lock);
678 error = e->ops ? entry->store(e, page, length) : -ENOENT;
679 mutex_unlock(&e->sysfs_lock);
680 return error;
681}
682
683static struct sysfs_ops elv_sysfs_ops = {
684 .show = elv_attr_show,
685 .store = elv_attr_store,
686};
687
688static struct kobj_type elv_ktype = {
689 .sysfs_ops = &elv_sysfs_ops,
690 .release = elevator_release,
691};
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693int elv_register_queue(struct request_queue *q)
694{
695 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500696 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Al Viro3d1ab402006-03-18 18:35:43 -0500698 e->kobj.parent = &q->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Al Viro3d1ab402006-03-18 18:35:43 -0500700 error = kobject_add(&e->kobj);
701 if (!error) {
702 struct attribute **attr = e->elevator_type->elevator_attrs;
703 if (attr) {
704 while (*attr) {
705 if (sysfs_create_file(&e->kobj,*attr++))
706 break;
707 }
708 }
709 kobject_uevent(&e->kobj, KOBJ_ADD);
710 }
711 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714void elv_unregister_queue(struct request_queue *q)
715{
716 if (q) {
717 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500718 kobject_uevent(&e->kobj, KOBJ_REMOVE);
719 kobject_del(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721}
722
723int elv_register(struct elevator_type *e)
724{
Tejun Heo2824bc932005-10-20 10:56:41 +0200725 spin_lock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (elevator_find(e->elevator_name))
727 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 list_add_tail(&e->list, &elv_list);
729 spin_unlock_irq(&elv_list_lock);
730
731 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
Nate Diller5f003972006-01-24 10:07:58 +0100732 if (!strcmp(e->elevator_name, chosen_elevator) ||
733 (!*chosen_elevator &&
734 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
735 printk(" (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 printk("\n");
737 return 0;
738}
739EXPORT_SYMBOL_GPL(elv_register);
740
741void elv_unregister(struct elevator_type *e)
742{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800743 struct task_struct *g, *p;
744
745 /*
746 * Iterate every thread in the process to remove the io contexts.
747 */
Al Viroe17a9482006-03-18 13:21:20 -0500748 if (e->ops.trim) {
749 read_lock(&tasklist_lock);
750 do_each_thread(g, p) {
751 task_lock(p);
752 e->ops.trim(p->io_context);
753 task_unlock(p);
754 } while_each_thread(g, p);
755 read_unlock(&tasklist_lock);
756 }
Christoph Hellwig83521d32005-10-30 15:01:39 -0800757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 spin_lock_irq(&elv_list_lock);
759 list_del_init(&e->list);
760 spin_unlock_irq(&elv_list_lock);
761}
762EXPORT_SYMBOL_GPL(elv_unregister);
763
764/*
765 * switch to new_e io scheduler. be careful not to introduce deadlocks -
766 * we don't free the old io scheduler, before we have allocated what we
767 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200768 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Al Viro3d1ab402006-03-18 18:35:43 -0500770static int elevator_switch(request_queue_t *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Tejun Heocb98fc82005-10-28 08:29:39 +0200772 elevator_t *old_elevator, *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Tejun Heocb98fc82005-10-28 08:29:39 +0200774 /*
775 * Allocate new elevator
776 */
Al Viro3d1ab402006-03-18 18:35:43 -0500777 e = elevator_alloc(new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -0500779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /*
Tejun Heocb98fc82005-10-28 08:29:39 +0200782 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Tejun Heocb98fc82005-10-28 08:29:39 +0200784 spin_lock_irq(q->queue_lock);
785
Jens Axboe64521d12005-10-28 08:30:39 +0200786 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200787
Tejun Heo15853af2005-11-10 08:52:05 +0100788 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200789
790 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +0100791 blk_remove_plug(q);
792 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200793 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +0200794 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +0200795 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +0100796 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200797 }
798
799 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /*
802 * unregister old elevator data
803 */
804 elv_unregister_queue(q);
805 old_elevator = q->elevator;
806
807 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * attach and start new elevator
809 */
Al Viro3d1ab402006-03-18 18:35:43 -0500810 if (elevator_attach(q, e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto fail;
812
813 if (elv_register_queue(q))
814 goto fail_register;
815
816 /*
Tejun Heocb98fc82005-10-28 08:29:39 +0200817 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
819 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +0200820 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -0500821 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823fail_register:
824 /*
825 * switch failed, exit the new io scheduler and reattach the old
826 * one again (along with re-adding the sysfs dir)
827 */
828 elevator_exit(e);
Tejun Heocb98fc82005-10-28 08:29:39 +0200829 e = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830fail:
831 q->elevator = old_elevator;
832 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +0200833 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Al Viro3d1ab402006-03-18 18:35:43 -0500834 if (e)
835 kobject_put(&e->kobj);
836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
840{
841 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +0100842 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct elevator_type *e;
844
Tejun Heobe561232005-11-10 08:55:01 +0100845 elevator_name[sizeof(elevator_name) - 1] = '\0';
846 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
847 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Tejun Heobe561232005-11-10 08:55:01 +0100849 if (len && elevator_name[len - 1] == '\n')
850 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 e = elevator_get(elevator_name);
853 if (!e) {
854 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
855 return -EINVAL;
856 }
857
Nate Diller2ca7d932005-10-30 15:02:24 -0800858 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
859 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -0800861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Al Viro3d1ab402006-03-18 18:35:43 -0500863 if (!elevator_switch(q, e))
864 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return count;
866}
867
868ssize_t elv_iosched_show(request_queue_t *q, char *name)
869{
870 elevator_t *e = q->elevator;
871 struct elevator_type *elv = e->elevator_type;
872 struct list_head *entry;
873 int len = 0;
874
875 spin_lock_irq(q->queue_lock);
876 list_for_each(entry, &elv_list) {
877 struct elevator_type *__e;
878
879 __e = list_entry(entry, struct elevator_type, list);
880 if (!strcmp(elv->elevator_name, __e->elevator_name))
881 len += sprintf(name+len, "[%s] ", elv->elevator_name);
882 else
883 len += sprintf(name+len, "%s ", __e->elevator_name);
884 }
885 spin_unlock_irq(q->queue_lock);
886
887 len += sprintf(len+name, "\n");
888 return len;
889}
890
Jens Axboe1b47f532005-10-20 16:37:00 +0200891EXPORT_SYMBOL(elv_dispatch_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892EXPORT_SYMBOL(elv_add_request);
893EXPORT_SYMBOL(__elv_add_request);
894EXPORT_SYMBOL(elv_requeue_request);
895EXPORT_SYMBOL(elv_next_request);
Tejun Heo8922e162005-10-20 16:23:44 +0200896EXPORT_SYMBOL(elv_dequeue_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897EXPORT_SYMBOL(elv_queue_empty);
898EXPORT_SYMBOL(elv_completed_request);
899EXPORT_SYMBOL(elevator_exit);
900EXPORT_SYMBOL(elevator_init);