blob: af2388e73f614343a2ec6540085591f49084b0ee [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>
37
38#include <asm/uaccess.h>
39
40static DEFINE_SPINLOCK(elv_list_lock);
41static LIST_HEAD(elv_list);
42
43/*
44 * can we safely merge with this request?
45 */
46inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
47{
48 if (!rq_mergeable(rq))
49 return 0;
50
51 /*
52 * different data direction or already started, don't merge
53 */
54 if (bio_data_dir(bio) != rq_data_dir(rq))
55 return 0;
56
57 /*
58 * same device and no special stuff set, merge is ok
59 */
60 if (rq->rq_disk == bio->bi_bdev->bd_disk &&
61 !rq->waiting && !rq->special)
62 return 1;
63
64 return 0;
65}
66EXPORT_SYMBOL(elv_rq_merge_ok);
67
68inline int elv_try_merge(struct request *__rq, struct bio *bio)
69{
70 int ret = ELEVATOR_NO_MERGE;
71
72 /*
73 * we can merge and sequence is ok, check if it's possible
74 */
75 if (elv_rq_merge_ok(__rq, bio)) {
76 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
77 ret = ELEVATOR_BACK_MERGE;
78 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
79 ret = ELEVATOR_FRONT_MERGE;
80 }
81
82 return ret;
83}
84EXPORT_SYMBOL(elv_try_merge);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct elevator_type *elevator_find(const char *name)
87{
88 struct elevator_type *e = NULL;
89 struct list_head *entry;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 list_for_each(entry, &elv_list) {
92 struct elevator_type *__e;
93
94 __e = list_entry(entry, struct elevator_type, list);
95
96 if (!strcmp(__e->elevator_name, name)) {
97 e = __e;
98 break;
99 }
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 return e;
103}
104
105static void elevator_put(struct elevator_type *e)
106{
107 module_put(e->elevator_owner);
108}
109
110static struct elevator_type *elevator_get(const char *name)
111{
Tejun Heo2824bc932005-10-20 10:56:41 +0200112 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Tejun Heo2824bc932005-10-20 10:56:41 +0200114 spin_lock_irq(&elv_list_lock);
115
116 e = elevator_find(name);
117 if (e && !try_module_get(e->elevator_owner))
118 e = NULL;
119
120 spin_unlock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 return e;
123}
124
125static int elevator_attach(request_queue_t *q, struct elevator_type *e,
126 struct elevator_queue *eq)
127{
128 int ret = 0;
129
130 memset(eq, 0, sizeof(*eq));
131 eq->ops = &e->ops;
132 eq->elevator_type = e;
133
134 INIT_LIST_HEAD(&q->queue_head);
135 q->last_merge = NULL;
136 q->elevator = eq;
Jens Axboe1b47f532005-10-20 16:37:00 +0200137 q->end_sector = 0;
Tejun Heo8922e162005-10-20 16:23:44 +0200138 q->boundary_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (eq->ops->elevator_init_fn)
141 ret = eq->ops->elevator_init_fn(q, eq);
142
143 return ret;
144}
145
146static char chosen_elevator[16];
147
148static void elevator_setup_default(void)
149{
Tejun Heo2824bc932005-10-20 10:56:41 +0200150 struct elevator_type *e;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /*
153 * check if default is set and exists
154 */
Tejun Heo2824bc932005-10-20 10:56:41 +0200155 if (chosen_elevator[0] && (e = elevator_get(chosen_elevator))) {
156 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return;
Tejun Heo2824bc932005-10-20 10:56:41 +0200158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#if defined(CONFIG_IOSCHED_AS)
161 strcpy(chosen_elevator, "anticipatory");
162#elif defined(CONFIG_IOSCHED_DEADLINE)
163 strcpy(chosen_elevator, "deadline");
164#elif defined(CONFIG_IOSCHED_CFQ)
165 strcpy(chosen_elevator, "cfq");
166#elif defined(CONFIG_IOSCHED_NOOP)
167 strcpy(chosen_elevator, "noop");
168#else
169#error "You must build at least 1 IO scheduler into the kernel"
170#endif
171}
172
173static int __init elevator_setup(char *str)
174{
175 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
176 return 0;
177}
178
179__setup("elevator=", elevator_setup);
180
181int elevator_init(request_queue_t *q, char *name)
182{
183 struct elevator_type *e = NULL;
184 struct elevator_queue *eq;
185 int ret = 0;
186
187 elevator_setup_default();
188
189 if (!name)
190 name = chosen_elevator;
191
192 e = elevator_get(name);
193 if (!e)
194 return -EINVAL;
195
196 eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL);
197 if (!eq) {
198 elevator_put(e->elevator_type);
199 return -ENOMEM;
200 }
201
202 ret = elevator_attach(q, e, eq);
203 if (ret) {
204 kfree(eq);
205 elevator_put(e->elevator_type);
206 }
207
208 return ret;
209}
210
211void elevator_exit(elevator_t *e)
212{
213 if (e->ops->elevator_exit_fn)
214 e->ops->elevator_exit_fn(e);
215
216 elevator_put(e->elevator_type);
217 e->elevator_type = NULL;
218 kfree(e);
219}
220
Tejun Heo8922e162005-10-20 16:23:44 +0200221/*
222 * Insert rq into dispatch queue of q. Queue lock must be held on
223 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
224 * appended to the dispatch queue. To be used by specific elevators.
225 */
Jens Axboe1b47f532005-10-20 16:37:00 +0200226void elv_dispatch_sort(request_queue_t *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200227{
228 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200229 struct list_head *entry;
230
Tejun Heo06b86242005-10-20 16:46:23 +0200231 if (q->last_merge == rq)
232 q->last_merge = NULL;
233
Jens Axboe1b47f532005-10-20 16:37:00 +0200234 boundary = q->end_sector;
Tejun Heocb198332005-10-24 08:35:58 +0200235
Tejun Heo8922e162005-10-20 16:23:44 +0200236 list_for_each_prev(entry, &q->queue_head) {
237 struct request *pos = list_entry_rq(entry);
238
239 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
240 break;
241 if (rq->sector >= boundary) {
242 if (pos->sector < boundary)
243 continue;
244 } else {
245 if (pos->sector >= boundary)
246 break;
247 }
248 if (rq->sector >= pos->sector)
249 break;
250 }
251
252 list_add(&rq->queuelist, entry);
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
256{
257 elevator_t *e = q->elevator;
Tejun Heo06b86242005-10-20 16:46:23 +0200258 int ret;
259
260 if (q->last_merge) {
261 ret = elv_try_merge(q->last_merge, bio);
262 if (ret != ELEVATOR_NO_MERGE) {
263 *req = q->last_merge;
264 return ret;
265 }
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (e->ops->elevator_merge_fn)
269 return e->ops->elevator_merge_fn(q, req, bio);
270
271 return ELEVATOR_NO_MERGE;
272}
273
274void elv_merged_request(request_queue_t *q, struct request *rq)
275{
276 elevator_t *e = q->elevator;
277
278 if (e->ops->elevator_merged_fn)
279 e->ops->elevator_merged_fn(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200280
281 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284void elv_merge_requests(request_queue_t *q, struct request *rq,
285 struct request *next)
286{
287 elevator_t *e = q->elevator;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (e->ops->elevator_merge_req_fn)
290 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200291
292 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Tejun Heo8922e162005-10-20 16:23:44 +0200295void elv_requeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 elevator_t *e = q->elevator;
298
299 /*
300 * it already went through dequeue, we need to decrement the
301 * in_flight count again
302 */
Tejun Heo8922e162005-10-20 16:23:44 +0200303 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 q->in_flight--;
Tejun Heo8922e162005-10-20 16:23:44 +0200305 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
306 e->ops->elevator_deactivate_req_fn(q, rq);
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 rq->flags &= ~REQ_STARTED;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * if this is the flush, requeue the original instead and drop the flush
313 */
314 if (rq->flags & REQ_BAR_FLUSH) {
315 clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
316 rq = rq->end_io_data;
317 }
318
Tejun Heo8922e162005-10-20 16:23:44 +0200319 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322void __elv_add_request(request_queue_t *q, struct request *rq, int where,
323 int plug)
324{
Tejun Heo8922e162005-10-20 16:23:44 +0200325 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
326 /*
327 * barriers implicitly indicate back insertion
328 */
329 if (where == ELEVATOR_INSERT_SORT)
330 where = ELEVATOR_INSERT_BACK;
331
332 /*
Jens Axboe1b47f532005-10-20 16:37:00 +0200333 * this request is scheduling boundary, update end_sector
Tejun Heo8922e162005-10-20 16:23:44 +0200334 */
335 if (blk_fs_request(rq)) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200336 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200337 q->boundary_rq = rq;
338 }
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (plug)
342 blk_plug_device(q);
343
344 rq->q = q;
345
Tejun Heo8922e162005-10-20 16:23:44 +0200346 if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /*
348 * if drain is set, store the request "locally". when the drain
349 * is finished, the requests will be handed ordered to the io
350 * scheduler
351 */
352 list_add_tail(&rq->queuelist, &q->drain_list);
Tejun Heo8922e162005-10-20 16:23:44 +0200353 return;
354 }
355
356 switch (where) {
357 case ELEVATOR_INSERT_FRONT:
358 rq->flags |= REQ_SOFTBARRIER;
359
360 list_add(&rq->queuelist, &q->queue_head);
361 break;
362
363 case ELEVATOR_INSERT_BACK:
364 rq->flags |= REQ_SOFTBARRIER;
365
366 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
367 ;
368 list_add_tail(&rq->queuelist, &q->queue_head);
369 /*
370 * We kick the queue here for the following reasons.
371 * - The elevator might have returned NULL previously
372 * to delay requests and returned them now. As the
373 * queue wasn't empty before this request, ll_rw_blk
374 * won't run the queue on return, resulting in hang.
375 * - Usually, back inserted requests won't be merged
376 * with anything. There's no point in delaying queue
377 * processing.
378 */
379 blk_remove_plug(q);
380 q->request_fn(q);
381 break;
382
383 case ELEVATOR_INSERT_SORT:
384 BUG_ON(!blk_fs_request(rq));
385 rq->flags |= REQ_SORTED;
386 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200387 if (q->last_merge == NULL && rq_mergeable(rq))
388 q->last_merge = rq;
Tejun Heo8922e162005-10-20 16:23:44 +0200389 break;
390
391 default:
392 printk(KERN_ERR "%s: bad insertion point %d\n",
393 __FUNCTION__, where);
394 BUG();
395 }
396
397 if (blk_queue_plugged(q)) {
398 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
399 - q->in_flight;
400
401 if (nrq >= q->unplug_thresh)
402 __generic_unplug_device(q);
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406void elv_add_request(request_queue_t *q, struct request *rq, int where,
407 int plug)
408{
409 unsigned long flags;
410
411 spin_lock_irqsave(q->queue_lock, flags);
412 __elv_add_request(q, rq, where, plug);
413 spin_unlock_irqrestore(q->queue_lock, flags);
414}
415
416static inline struct request *__elv_next_request(request_queue_t *q)
417{
Tejun Heo8922e162005-10-20 16:23:44 +0200418 struct request *rq;
419
420 if (unlikely(list_empty(&q->queue_head) &&
421 !q->elevator->ops->elevator_dispatch_fn(q, 0)))
422 return NULL;
423
424 rq = list_entry_rq(q->queue_head.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /*
427 * if this is a barrier write and the device has to issue a
428 * flush sequence to support it, check how far we are
429 */
Tejun Heo8922e162005-10-20 16:23:44 +0200430 if (blk_fs_request(rq) && blk_barrier_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 BUG_ON(q->ordered == QUEUE_ORDERED_NONE);
432
433 if (q->ordered == QUEUE_ORDERED_FLUSH &&
434 !blk_barrier_preflush(rq))
435 rq = blk_start_pre_flush(q, rq);
436 }
437
438 return rq;
439}
440
441struct request *elv_next_request(request_queue_t *q)
442{
443 struct request *rq;
444 int ret;
445
446 while ((rq = __elv_next_request(q)) != NULL) {
Tejun Heo8922e162005-10-20 16:23:44 +0200447 if (!(rq->flags & REQ_STARTED)) {
448 elevator_t *e = q->elevator;
449
450 /*
451 * This is the first time the device driver
452 * sees this request (possibly after
453 * requeueing). Notify IO scheduler.
454 */
455 if (blk_sorted_rq(rq) &&
456 e->ops->elevator_activate_req_fn)
457 e->ops->elevator_activate_req_fn(q, rq);
458
459 /*
460 * just mark as started even if we don't start
461 * it, a request that has been delayed should
462 * not be passed by new incoming requests
463 */
464 rq->flags |= REQ_STARTED;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Tejun Heo8922e162005-10-20 16:23:44 +0200467 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200468 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200469 q->boundary_rq = NULL;
470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
473 break;
474
475 ret = q->prep_rq_fn(q, rq);
476 if (ret == BLKPREP_OK) {
477 break;
478 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500479 /*
480 * the request may have been (partially) prepped.
481 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200482 * avoid resource deadlock. REQ_STARTED will
483 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 rq = NULL;
486 break;
487 } else if (ret == BLKPREP_KILL) {
488 int nr_bytes = rq->hard_nr_sectors << 9;
489
490 if (!nr_bytes)
491 nr_bytes = rq->data_len;
492
493 blkdev_dequeue_request(rq);
494 rq->flags |= REQ_QUIET;
495 end_that_request_chunk(rq, 0, nr_bytes);
496 end_that_request_last(rq);
497 } else {
498 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
499 ret);
500 break;
501 }
502 }
503
504 return rq;
505}
506
Tejun Heo8922e162005-10-20 16:23:44 +0200507void elv_dequeue_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Tejun Heo8922e162005-10-20 16:23:44 +0200509 BUG_ON(list_empty(&rq->queuelist));
510
511 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * the time frame between a request being removed from the lists
515 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200516 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518 if (blk_account_rq(rq))
519 q->in_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522int elv_queue_empty(request_queue_t *q)
523{
524 elevator_t *e = q->elevator;
525
Tejun Heo8922e162005-10-20 16:23:44 +0200526 if (!list_empty(&q->queue_head))
527 return 0;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (e->ops->elevator_queue_empty_fn)
530 return e->ops->elevator_queue_empty_fn(q);
531
Tejun Heo8922e162005-10-20 16:23:44 +0200532 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535struct request *elv_latter_request(request_queue_t *q, struct request *rq)
536{
537 struct list_head *next;
538
539 elevator_t *e = q->elevator;
540
541 if (e->ops->elevator_latter_req_fn)
542 return e->ops->elevator_latter_req_fn(q, rq);
543
544 next = rq->queuelist.next;
545 if (next != &q->queue_head && next != &rq->queuelist)
546 return list_entry_rq(next);
547
548 return NULL;
549}
550
551struct request *elv_former_request(request_queue_t *q, struct request *rq)
552{
553 struct list_head *prev;
554
555 elevator_t *e = q->elevator;
556
557 if (e->ops->elevator_former_req_fn)
558 return e->ops->elevator_former_req_fn(q, rq);
559
560 prev = rq->queuelist.prev;
561 if (prev != &q->queue_head && prev != &rq->queuelist)
562 return list_entry_rq(prev);
563
564 return NULL;
565}
566
Jens Axboe22e2c502005-06-27 10:55:12 +0200567int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
568 int gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 elevator_t *e = q->elevator;
571
572 if (e->ops->elevator_set_req_fn)
Jens Axboe22e2c502005-06-27 10:55:12 +0200573 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 rq->elevator_private = NULL;
576 return 0;
577}
578
579void elv_put_request(request_queue_t *q, struct request *rq)
580{
581 elevator_t *e = q->elevator;
582
583 if (e->ops->elevator_put_req_fn)
584 e->ops->elevator_put_req_fn(q, rq);
585}
586
Jens Axboe22e2c502005-06-27 10:55:12 +0200587int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 elevator_t *e = q->elevator;
590
591 if (e->ops->elevator_may_queue_fn)
Jens Axboe22e2c502005-06-27 10:55:12 +0200592 return e->ops->elevator_may_queue_fn(q, rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return ELV_MQUEUE_MAY;
595}
596
597void elv_completed_request(request_queue_t *q, struct request *rq)
598{
599 elevator_t *e = q->elevator;
600
601 /*
602 * request is released from the driver, io must be done
603 */
Tejun Heo8922e162005-10-20 16:23:44 +0200604 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 q->in_flight--;
Tejun Heo8922e162005-10-20 16:23:44 +0200606 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
607 e->ops->elevator_completed_req_fn(q, rq);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611int elv_register_queue(struct request_queue *q)
612{
613 elevator_t *e = q->elevator;
614
615 e->kobj.parent = kobject_get(&q->kobj);
616 if (!e->kobj.parent)
617 return -EBUSY;
618
619 snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
620 e->kobj.ktype = e->elevator_type->elevator_ktype;
621
622 return kobject_register(&e->kobj);
623}
624
625void elv_unregister_queue(struct request_queue *q)
626{
627 if (q) {
628 elevator_t *e = q->elevator;
629 kobject_unregister(&e->kobj);
630 kobject_put(&q->kobj);
631 }
632}
633
634int elv_register(struct elevator_type *e)
635{
Tejun Heo2824bc932005-10-20 10:56:41 +0200636 spin_lock_irq(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (elevator_find(e->elevator_name))
638 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 list_add_tail(&e->list, &elv_list);
640 spin_unlock_irq(&elv_list_lock);
641
642 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
643 if (!strcmp(e->elevator_name, chosen_elevator))
644 printk(" (default)");
645 printk("\n");
646 return 0;
647}
648EXPORT_SYMBOL_GPL(elv_register);
649
650void elv_unregister(struct elevator_type *e)
651{
652 spin_lock_irq(&elv_list_lock);
653 list_del_init(&e->list);
654 spin_unlock_irq(&elv_list_lock);
655}
656EXPORT_SYMBOL_GPL(elv_unregister);
657
658/*
659 * switch to new_e io scheduler. be careful not to introduce deadlocks -
660 * we don't free the old io scheduler, before we have allocated what we
661 * need for the new one. this way we have a chance of going back to the old
662 * one, if the new one fails init for some reason. we also do an intermediate
663 * switch to noop to ensure safety with stack-allocated requests, since they
664 * don't originate from the block layer allocator. noop is safe here, because
665 * it never needs to touch the elevator itself for completion events. DRAIN
666 * flags will make sure we don't touch it for additions either.
667 */
668static void elevator_switch(request_queue_t *q, struct elevator_type *new_e)
669{
670 elevator_t *e = kmalloc(sizeof(elevator_t), GFP_KERNEL);
671 struct elevator_type *noop_elevator = NULL;
672 elevator_t *old_elevator;
673
674 if (!e)
675 goto error;
676
677 /*
678 * first step, drain requests from the block freelist
679 */
680 blk_wait_queue_drained(q, 0);
681
682 /*
683 * unregister old elevator data
684 */
685 elv_unregister_queue(q);
686 old_elevator = q->elevator;
687
688 /*
689 * next step, switch to noop since it uses no private rq structures
690 * and doesn't allocate any memory for anything. then wait for any
691 * non-fs requests in-flight
692 */
693 noop_elevator = elevator_get("noop");
694 spin_lock_irq(q->queue_lock);
695 elevator_attach(q, noop_elevator, e);
696 spin_unlock_irq(q->queue_lock);
697
698 blk_wait_queue_drained(q, 1);
699
700 /*
701 * attach and start new elevator
702 */
703 if (elevator_attach(q, new_e, e))
704 goto fail;
705
706 if (elv_register_queue(q))
707 goto fail_register;
708
709 /*
710 * finally exit old elevator and start queue again
711 */
712 elevator_exit(old_elevator);
713 blk_finish_queue_drain(q);
714 elevator_put(noop_elevator);
715 return;
716
717fail_register:
718 /*
719 * switch failed, exit the new io scheduler and reattach the old
720 * one again (along with re-adding the sysfs dir)
721 */
722 elevator_exit(e);
723fail:
724 q->elevator = old_elevator;
725 elv_register_queue(q);
726 blk_finish_queue_drain(q);
727error:
728 if (noop_elevator)
729 elevator_put(noop_elevator);
730 elevator_put(new_e);
731 printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name);
732}
733
734ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
735{
736 char elevator_name[ELV_NAME_MAX];
737 struct elevator_type *e;
738
739 memset(elevator_name, 0, sizeof(elevator_name));
740 strncpy(elevator_name, name, sizeof(elevator_name));
741
742 if (elevator_name[strlen(elevator_name) - 1] == '\n')
743 elevator_name[strlen(elevator_name) - 1] = '\0';
744
745 e = elevator_get(elevator_name);
746 if (!e) {
747 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
748 return -EINVAL;
749 }
750
751 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name))
752 return count;
753
754 elevator_switch(q, e);
755 return count;
756}
757
758ssize_t elv_iosched_show(request_queue_t *q, char *name)
759{
760 elevator_t *e = q->elevator;
761 struct elevator_type *elv = e->elevator_type;
762 struct list_head *entry;
763 int len = 0;
764
765 spin_lock_irq(q->queue_lock);
766 list_for_each(entry, &elv_list) {
767 struct elevator_type *__e;
768
769 __e = list_entry(entry, struct elevator_type, list);
770 if (!strcmp(elv->elevator_name, __e->elevator_name))
771 len += sprintf(name+len, "[%s] ", elv->elevator_name);
772 else
773 len += sprintf(name+len, "%s ", __e->elevator_name);
774 }
775 spin_unlock_irq(q->queue_lock);
776
777 len += sprintf(len+name, "\n");
778 return len;
779}
780
Jens Axboe1b47f532005-10-20 16:37:00 +0200781EXPORT_SYMBOL(elv_dispatch_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782EXPORT_SYMBOL(elv_add_request);
783EXPORT_SYMBOL(__elv_add_request);
784EXPORT_SYMBOL(elv_requeue_request);
785EXPORT_SYMBOL(elv_next_request);
Tejun Heo8922e162005-10-20 16:23:44 +0200786EXPORT_SYMBOL(elv_dequeue_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787EXPORT_SYMBOL(elv_queue_empty);
788EXPORT_SYMBOL(elv_completed_request);
789EXPORT_SYMBOL(elevator_exit);
790EXPORT_SYMBOL(elevator_init);