blob: e4c58827bb4660bc21bef638a3c9a926b2fc49c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/block/elevator.c
3 *
4 * Block device elevator/IO-scheduler.
5 *
6 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
7 *
8 * 30042000 Jens Axboe <axboe@suse.de> :
9 *
10 * Split the elevator a bit so that it is possible to choose a different
11 * one or even write a new "plug in". There are three pieces:
12 * - elevator_fn, inserts a new request in the queue list
13 * - elevator_merge_fn, decides whether a new buffer can be merged with
14 * an existing request
15 * - elevator_dequeue_fn, called when a request is taken off the active list
16 *
17 * 20082000 Dave Jones <davej@suse.de> :
18 * Removed tests for max-bomb-segments, which was breaking elvtune
19 * when run without -bN
20 *
21 * Jens:
22 * - Rework again to work with bio instead of buffer_heads
23 * - loose bi_dev comparisons, partition handling is right now
24 * - completely modularize elevator setup and teardown
25 *
26 */
27#include <linux/kernel.h>
28#include <linux/fs.h>
29#include <linux/blkdev.h>
30#include <linux/elevator.h>
31#include <linux/bio.h>
32#include <linux/config.h>
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/compiler.h>
Tejun Heocb98fc82005-10-28 08:29:39 +020037#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/uaccess.h>
40
41static DEFINE_SPINLOCK(elv_list_lock);
42static LIST_HEAD(elv_list);
43
44/*
45 * can we safely merge with this request?
46 */
47inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
48{
49 if (!rq_mergeable(rq))
50 return 0;
51
52 /*
53 * different data direction or already started, don't merge
54 */
55 if (bio_data_dir(bio) != rq_data_dir(rq))
56 return 0;
57
58 /*
59 * same device and no special stuff set, merge is ok
60 */
61 if (rq->rq_disk == bio->bi_bdev->bd_disk &&
62 !rq->waiting && !rq->special)
63 return 1;
64
65 return 0;
66}
67EXPORT_SYMBOL(elv_rq_merge_ok);
68
69inline int elv_try_merge(struct request *__rq, struct bio *bio)
70{
71 int ret = ELEVATOR_NO_MERGE;
72
73 /*
74 * we can merge and sequence is ok, check if it's possible
75 */
76 if (elv_rq_merge_ok(__rq, bio)) {
77 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
78 ret = ELEVATOR_BACK_MERGE;
79 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
80 ret = ELEVATOR_FRONT_MERGE;
81 }
82
83 return ret;
84}
85EXPORT_SYMBOL(elv_try_merge);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct elevator_type *elevator_find(const char *name)
88{
89 struct elevator_type *e = NULL;
90 struct list_head *entry;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 list_for_each(entry, &elv_list) {
93 struct elevator_type *__e;
94
95 __e = list_entry(entry, struct elevator_type, list);
96
97 if (!strcmp(__e->elevator_name, name)) {
98 e = __e;
99 break;
100 }
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 return e;
104}
105
106static void elevator_put(struct elevator_type *e)
107{
108 module_put(e->elevator_owner);
109}
110
111static struct elevator_type *elevator_get(const char *name)
112{
Tejun Heo2824bc932005-10-20 10:56:41 +0200113 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Tejun Heo2824bc932005-10-20 10:56:41 +0200115 spin_lock_irq(&elv_list_lock);
116
117 e = elevator_find(name);
118 if (e && !try_module_get(e->elevator_owner))
119 e = NULL;
120
121 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return e;
124}
125
126static int elevator_attach(request_queue_t *q, struct elevator_type *e,
127 struct elevator_queue *eq)
128{
129 int ret = 0;
130
131 memset(eq, 0, sizeof(*eq));
132 eq->ops = &e->ops;
133 eq->elevator_type = e;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 q->elevator = eq;
136
137 if (eq->ops->elevator_init_fn)
138 ret = eq->ops->elevator_init_fn(q, eq);
139
140 return ret;
141}
142
143static char chosen_elevator[16];
144
145static void elevator_setup_default(void)
146{
Tejun Heo2824bc932005-10-20 10:56:41 +0200147 struct elevator_type *e;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
Nate Diller131dda72005-10-30 15:02:19 -0800150 * If default has not been set, use the compiled-in selection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Nate Diller131dda72005-10-30 15:02:19 -0800152 if (!chosen_elevator[0])
153 strcpy(chosen_elevator, CONFIG_DEFAULT_IOSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Nate Diller131dda72005-10-30 15:02:19 -0800155 /*
156 * If the given scheduler is not available, fall back to no-op.
157 */
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100158 if ((e = elevator_find(chosen_elevator)))
159 elevator_put(e);
160 else
Nate Diller131dda72005-10-30 15:02:19 -0800161 strcpy(chosen_elevator, "noop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static int __init elevator_setup(char *str)
165{
166 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
167 return 0;
168}
169
170__setup("elevator=", elevator_setup);
171
172int elevator_init(request_queue_t *q, char *name)
173{
174 struct elevator_type *e = NULL;
175 struct elevator_queue *eq;
176 int ret = 0;
177
Tejun Heocb98fc82005-10-28 08:29:39 +0200178 INIT_LIST_HEAD(&q->queue_head);
179 q->last_merge = NULL;
180 q->end_sector = 0;
181 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 elevator_setup_default();
184
185 if (!name)
186 name = chosen_elevator;
187
188 e = elevator_get(name);
189 if (!e)
190 return -EINVAL;
191
192 eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL);
193 if (!eq) {
Zachary Amsdenb8ea2cb2005-11-09 13:23:01 +0100194 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -ENOMEM;
196 }
197
198 ret = elevator_attach(q, e, eq);
199 if (ret) {
200 kfree(eq);
Zachary Amsdenb8ea2cb2005-11-09 13:23:01 +0100201 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 return ret;
205}
206
207void elevator_exit(elevator_t *e)
208{
209 if (e->ops->elevator_exit_fn)
210 e->ops->elevator_exit_fn(e);
211
212 elevator_put(e->elevator_type);
213 e->elevator_type = NULL;
214 kfree(e);
215}
216
Tejun Heo8922e162005-10-20 16:23:44 +0200217/*
218 * Insert rq into dispatch queue of q. Queue lock must be held on
219 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
220 * appended to the dispatch queue. To be used by specific elevators.
221 */
Jens Axboe1b47f532005-10-20 16:37:00 +0200222void elv_dispatch_sort(request_queue_t *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200223{
224 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200225 struct list_head *entry;
226
Tejun Heo06b86242005-10-20 16:46:23 +0200227 if (q->last_merge == rq)
228 q->last_merge = NULL;
Tejun Heo15853af2005-11-10 08:52:05 +0100229 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200230
Jens Axboe1b47f532005-10-20 16:37:00 +0200231 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200232
Tejun Heo8922e162005-10-20 16:23:44 +0200233 list_for_each_prev(entry, &q->queue_head) {
234 struct request *pos = list_entry_rq(entry);
235
236 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
237 break;
238 if (rq->sector >= boundary) {
239 if (pos->sector < boundary)
240 continue;
241 } else {
242 if (pos->sector >= boundary)
243 break;
244 }
245 if (rq->sector >= pos->sector)
246 break;
247 }
248
249 list_add(&rq->queuelist, entry);
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
253{
254 elevator_t *e = q->elevator;
Tejun Heo06b86242005-10-20 16:46:23 +0200255 int ret;
256
257 if (q->last_merge) {
258 ret = elv_try_merge(q->last_merge, bio);
259 if (ret != ELEVATOR_NO_MERGE) {
260 *req = q->last_merge;
261 return ret;
262 }
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (e->ops->elevator_merge_fn)
266 return e->ops->elevator_merge_fn(q, req, bio);
267
268 return ELEVATOR_NO_MERGE;
269}
270
271void elv_merged_request(request_queue_t *q, struct request *rq)
272{
273 elevator_t *e = q->elevator;
274
275 if (e->ops->elevator_merged_fn)
276 e->ops->elevator_merged_fn(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200277
278 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281void elv_merge_requests(request_queue_t *q, struct request *rq,
282 struct request *next)
283{
284 elevator_t *e = q->elevator;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (e->ops->elevator_merge_req_fn)
287 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo15853af2005-11-10 08:52:05 +0100288 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200289
290 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Tejun Heo8922e162005-10-20 16:23:44 +0200293void elv_requeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 elevator_t *e = q->elevator;
296
297 /*
298 * it already went through dequeue, we need to decrement the
299 * in_flight count again
300 */
Tejun Heo8922e162005-10-20 16:23:44 +0200301 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 q->in_flight--;
Tejun Heo8922e162005-10-20 16:23:44 +0200303 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
304 e->ops->elevator_deactivate_req_fn(q, rq);
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 rq->flags &= ~REQ_STARTED;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
310 * if this is the flush, requeue the original instead and drop the flush
311 */
312 if (rq->flags & REQ_BAR_FLUSH) {
313 clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
314 rq = rq->end_io_data;
315 }
316
Tejun Heo8922e162005-10-20 16:23:44 +0200317 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Tejun Heo15853af2005-11-10 08:52:05 +0100320static void elv_drain_elevator(request_queue_t *q)
321{
322 static int printed;
323 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
324 ;
325 if (q->nr_sorted == 0)
326 return;
327 if (printed++ < 10) {
328 printk(KERN_ERR "%s: forced dispatching is broken "
329 "(nr_sorted=%u), please report this\n",
330 q->elevator->elevator_type->elevator_name, q->nr_sorted);
331 }
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334void __elv_add_request(request_queue_t *q, struct request *rq, int where,
335 int plug)
336{
Tejun Heo8922e162005-10-20 16:23:44 +0200337 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
338 /*
339 * barriers implicitly indicate back insertion
340 */
341 if (where == ELEVATOR_INSERT_SORT)
342 where = ELEVATOR_INSERT_BACK;
343
344 /*
Jens Axboe1b47f532005-10-20 16:37:00 +0200345 * this request is scheduling boundary, update end_sector
Tejun Heo8922e162005-10-20 16:23:44 +0200346 */
347 if (blk_fs_request(rq)) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200348 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200349 q->boundary_rq = rq;
350 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200351 } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
352 where = ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (plug)
355 blk_plug_device(q);
356
357 rq->q = q;
358
Tejun Heo8922e162005-10-20 16:23:44 +0200359 switch (where) {
360 case ELEVATOR_INSERT_FRONT:
361 rq->flags |= REQ_SOFTBARRIER;
362
363 list_add(&rq->queuelist, &q->queue_head);
364 break;
365
366 case ELEVATOR_INSERT_BACK:
367 rq->flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100368 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200369 list_add_tail(&rq->queuelist, &q->queue_head);
370 /*
371 * We kick the queue here for the following reasons.
372 * - The elevator might have returned NULL previously
373 * to delay requests and returned them now. As the
374 * queue wasn't empty before this request, ll_rw_blk
375 * won't run the queue on return, resulting in hang.
376 * - Usually, back inserted requests won't be merged
377 * with anything. There's no point in delaying queue
378 * processing.
379 */
380 blk_remove_plug(q);
381 q->request_fn(q);
382 break;
383
384 case ELEVATOR_INSERT_SORT:
385 BUG_ON(!blk_fs_request(rq));
386 rq->flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100387 q->nr_sorted++;
Tejun Heo06b86242005-10-20 16:46:23 +0200388 if (q->last_merge == NULL && rq_mergeable(rq))
389 q->last_merge = rq;
Tejun Heoca235092005-11-01 17:23:49 +0900390 /*
391 * Some ioscheds (cfq) run q->request_fn directly, so
392 * rq cannot be accessed after calling
393 * elevator_add_req_fn.
394 */
395 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200396 break;
397
398 default:
399 printk(KERN_ERR "%s: bad insertion point %d\n",
400 __FUNCTION__, where);
401 BUG();
402 }
403
404 if (blk_queue_plugged(q)) {
405 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
406 - q->in_flight;
407
408 if (nrq >= q->unplug_thresh)
409 __generic_unplug_device(q);
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413void elv_add_request(request_queue_t *q, struct request *rq, int where,
414 int plug)
415{
416 unsigned long flags;
417
418 spin_lock_irqsave(q->queue_lock, flags);
419 __elv_add_request(q, rq, where, plug);
420 spin_unlock_irqrestore(q->queue_lock, flags);
421}
422
423static inline struct request *__elv_next_request(request_queue_t *q)
424{
Tejun Heo8922e162005-10-20 16:23:44 +0200425 struct request *rq;
426
427 if (unlikely(list_empty(&q->queue_head) &&
428 !q->elevator->ops->elevator_dispatch_fn(q, 0)))
429 return NULL;
430
431 rq = list_entry_rq(q->queue_head.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /*
434 * if this is a barrier write and the device has to issue a
435 * flush sequence to support it, check how far we are
436 */
Tejun Heo8922e162005-10-20 16:23:44 +0200437 if (blk_fs_request(rq) && blk_barrier_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 BUG_ON(q->ordered == QUEUE_ORDERED_NONE);
439
440 if (q->ordered == QUEUE_ORDERED_FLUSH &&
441 !blk_barrier_preflush(rq))
442 rq = blk_start_pre_flush(q, rq);
443 }
444
445 return rq;
446}
447
448struct request *elv_next_request(request_queue_t *q)
449{
450 struct request *rq;
451 int ret;
452
453 while ((rq = __elv_next_request(q)) != NULL) {
Tejun Heo8922e162005-10-20 16:23:44 +0200454 if (!(rq->flags & REQ_STARTED)) {
455 elevator_t *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Tejun Heo8922e162005-10-20 16:23:44 +0200457 /*
458 * This is the first time the device driver
459 * sees this request (possibly after
460 * requeueing). Notify IO scheduler.
461 */
462 if (blk_sorted_rq(rq) &&
463 e->ops->elevator_activate_req_fn)
464 e->ops->elevator_activate_req_fn(q, rq);
465
466 /*
467 * just mark as started even if we don't start
468 * it, a request that has been delayed should
469 * not be passed by new incoming requests
470 */
471 rq->flags |= REQ_STARTED;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Tejun Heo8922e162005-10-20 16:23:44 +0200474 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200475 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200476 q->boundary_rq = NULL;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
480 break;
481
482 ret = q->prep_rq_fn(q, rq);
483 if (ret == BLKPREP_OK) {
484 break;
485 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500486 /*
487 * the request may have been (partially) prepped.
488 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200489 * avoid resource deadlock. REQ_STARTED will
490 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 rq = NULL;
493 break;
494 } else if (ret == BLKPREP_KILL) {
495 int nr_bytes = rq->hard_nr_sectors << 9;
496
497 if (!nr_bytes)
498 nr_bytes = rq->data_len;
499
500 blkdev_dequeue_request(rq);
501 rq->flags |= REQ_QUIET;
502 end_that_request_chunk(rq, 0, nr_bytes);
503 end_that_request_last(rq);
504 } else {
505 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
506 ret);
507 break;
508 }
509 }
510
511 return rq;
512}
513
Tejun Heo8922e162005-10-20 16:23:44 +0200514void elv_dequeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Tejun Heo8922e162005-10-20 16:23:44 +0200516 BUG_ON(list_empty(&rq->queuelist));
517
518 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
521 * the time frame between a request being removed from the lists
522 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200523 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
525 if (blk_account_rq(rq))
526 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529int elv_queue_empty(request_queue_t *q)
530{
531 elevator_t *e = q->elevator;
532
Tejun Heo8922e162005-10-20 16:23:44 +0200533 if (!list_empty(&q->queue_head))
534 return 0;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (e->ops->elevator_queue_empty_fn)
537 return e->ops->elevator_queue_empty_fn(q);
538
Tejun Heo8922e162005-10-20 16:23:44 +0200539 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542struct request *elv_latter_request(request_queue_t *q, struct request *rq)
543{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 elevator_t *e = q->elevator;
545
546 if (e->ops->elevator_latter_req_fn)
547 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return NULL;
549}
550
551struct request *elv_former_request(request_queue_t *q, struct request *rq)
552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 elevator_t *e = q->elevator;
554
555 if (e->ops->elevator_former_req_fn)
556 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return NULL;
558}
559
Jens Axboe22e2c502005-06-27 10:55:12 +0200560int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -0400561 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 elevator_t *e = q->elevator;
564
565 if (e->ops->elevator_set_req_fn)
Jens Axboe22e2c502005-06-27 10:55:12 +0200566 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 rq->elevator_private = NULL;
569 return 0;
570}
571
572void elv_put_request(request_queue_t *q, struct request *rq)
573{
574 elevator_t *e = q->elevator;
575
576 if (e->ops->elevator_put_req_fn)
577 e->ops->elevator_put_req_fn(q, rq);
578}
579
Jens Axboe22e2c502005-06-27 10:55:12 +0200580int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 elevator_t *e = q->elevator;
583
584 if (e->ops->elevator_may_queue_fn)
Jens Axboe22e2c502005-06-27 10:55:12 +0200585 return e->ops->elevator_may_queue_fn(q, rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 return ELV_MQUEUE_MAY;
588}
589
590void elv_completed_request(request_queue_t *q, struct request *rq)
591{
592 elevator_t *e = q->elevator;
593
594 /*
595 * request is released from the driver, io must be done
596 */
Tejun Heo8922e162005-10-20 16:23:44 +0200597 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 q->in_flight--;
Tejun Heo8922e162005-10-20 16:23:44 +0200599 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
600 e->ops->elevator_completed_req_fn(q, rq);
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604int elv_register_queue(struct request_queue *q)
605{
606 elevator_t *e = q->elevator;
607
608 e->kobj.parent = kobject_get(&q->kobj);
609 if (!e->kobj.parent)
610 return -EBUSY;
611
612 snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
613 e->kobj.ktype = e->elevator_type->elevator_ktype;
614
615 return kobject_register(&e->kobj);
616}
617
618void elv_unregister_queue(struct request_queue *q)
619{
620 if (q) {
621 elevator_t *e = q->elevator;
622 kobject_unregister(&e->kobj);
623 kobject_put(&q->kobj);
624 }
625}
626
627int elv_register(struct elevator_type *e)
628{
Tejun Heo2824bc932005-10-20 10:56:41 +0200629 spin_lock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (elevator_find(e->elevator_name))
631 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 list_add_tail(&e->list, &elv_list);
633 spin_unlock_irq(&elv_list_lock);
634
635 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
636 if (!strcmp(e->elevator_name, chosen_elevator))
637 printk(" (default)");
638 printk("\n");
639 return 0;
640}
641EXPORT_SYMBOL_GPL(elv_register);
642
643void elv_unregister(struct elevator_type *e)
644{
Christoph Hellwig83521d32005-10-30 15:01:39 -0800645 struct task_struct *g, *p;
646
647 /*
648 * Iterate every thread in the process to remove the io contexts.
649 */
650 read_lock(&tasklist_lock);
651 do_each_thread(g, p) {
652 struct io_context *ioc = p->io_context;
653 if (ioc && ioc->cic) {
654 ioc->cic->exit(ioc->cic);
655 ioc->cic->dtor(ioc->cic);
656 ioc->cic = NULL;
657 }
658 if (ioc && ioc->aic) {
659 ioc->aic->exit(ioc->aic);
660 ioc->aic->dtor(ioc->aic);
661 ioc->aic = NULL;
662 }
663 } while_each_thread(g, p);
664 read_unlock(&tasklist_lock);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_lock_irq(&elv_list_lock);
667 list_del_init(&e->list);
668 spin_unlock_irq(&elv_list_lock);
669}
670EXPORT_SYMBOL_GPL(elv_unregister);
671
672/*
673 * switch to new_e io scheduler. be careful not to introduce deadlocks -
674 * we don't free the old io scheduler, before we have allocated what we
675 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200676 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
678static void elevator_switch(request_queue_t *q, struct elevator_type *new_e)
679{
Tejun Heocb98fc82005-10-28 08:29:39 +0200680 elevator_t *old_elevator, *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Tejun Heocb98fc82005-10-28 08:29:39 +0200682 /*
683 * Allocate new elevator
684 */
685 e = kmalloc(sizeof(elevator_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!e)
687 goto error;
688
689 /*
Tejun Heocb98fc82005-10-28 08:29:39 +0200690 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Tejun Heocb98fc82005-10-28 08:29:39 +0200692 spin_lock_irq(q->queue_lock);
693
Jens Axboe64521d12005-10-28 08:30:39 +0200694 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200695
Tejun Heo15853af2005-11-10 08:52:05 +0100696 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200697
698 while (q->rq.elvpriv) {
Tejun Heo407df2a2005-11-10 08:48:21 +0100699 blk_remove_plug(q);
700 q->request_fn(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200701 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +0200702 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +0200703 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +0100704 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200705 }
706
707 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * unregister old elevator data
711 */
712 elv_unregister_queue(q);
713 old_elevator = q->elevator;
714
715 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * attach and start new elevator
717 */
718 if (elevator_attach(q, new_e, e))
719 goto fail;
720
721 if (elv_register_queue(q))
722 goto fail_register;
723
724 /*
Tejun Heocb98fc82005-10-28 08:29:39 +0200725 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
727 elevator_exit(old_elevator);
Jens Axboe64521d12005-10-28 08:30:39 +0200728 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return;
730
731fail_register:
732 /*
733 * switch failed, exit the new io scheduler and reattach the old
734 * one again (along with re-adding the sysfs dir)
735 */
736 elevator_exit(e);
Tejun Heocb98fc82005-10-28 08:29:39 +0200737 e = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738fail:
739 q->elevator = old_elevator;
740 elv_register_queue(q);
Jens Axboe64521d12005-10-28 08:30:39 +0200741 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200742 kfree(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 elevator_put(new_e);
745 printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name);
746}
747
748ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
749{
750 char elevator_name[ELV_NAME_MAX];
Tejun Heobe561232005-11-10 08:55:01 +0100751 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct elevator_type *e;
753
Tejun Heobe561232005-11-10 08:55:01 +0100754 elevator_name[sizeof(elevator_name) - 1] = '\0';
755 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
756 len = strlen(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Tejun Heobe561232005-11-10 08:55:01 +0100758 if (len && elevator_name[len - 1] == '\n')
759 elevator_name[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 e = elevator_get(elevator_name);
762 if (!e) {
763 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
764 return -EINVAL;
765 }
766
Nate Diller2ca7d932005-10-30 15:02:24 -0800767 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
768 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -0800770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 elevator_switch(q, e);
773 return count;
774}
775
776ssize_t elv_iosched_show(request_queue_t *q, char *name)
777{
778 elevator_t *e = q->elevator;
779 struct elevator_type *elv = e->elevator_type;
780 struct list_head *entry;
781 int len = 0;
782
783 spin_lock_irq(q->queue_lock);
784 list_for_each(entry, &elv_list) {
785 struct elevator_type *__e;
786
787 __e = list_entry(entry, struct elevator_type, list);
788 if (!strcmp(elv->elevator_name, __e->elevator_name))
789 len += sprintf(name+len, "[%s] ", elv->elevator_name);
790 else
791 len += sprintf(name+len, "%s ", __e->elevator_name);
792 }
793 spin_unlock_irq(q->queue_lock);
794
795 len += sprintf(len+name, "\n");
796 return len;
797}
798
Jens Axboe1b47f532005-10-20 16:37:00 +0200799EXPORT_SYMBOL(elv_dispatch_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800EXPORT_SYMBOL(elv_add_request);
801EXPORT_SYMBOL(__elv_add_request);
802EXPORT_SYMBOL(elv_requeue_request);
803EXPORT_SYMBOL(elv_next_request);
Tejun Heo8922e162005-10-20 16:23:44 +0200804EXPORT_SYMBOL(elv_dequeue_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805EXPORT_SYMBOL(elv_queue_empty);
806EXPORT_SYMBOL(elv_completed_request);
807EXPORT_SYMBOL(elevator_exit);
808EXPORT_SYMBOL(elevator_init);