ASoC: s3c2443-ac97: convert semaphore to mutex
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / elevator.c
blobca861927ba41b2dfc9ff80c139f6c8cc162fc251
1 /*
2 * Block device elevator/IO-scheduler.
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 * 30042000 Jens Axboe <axboe@kernel.dk> :
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
15 * 20082000 Dave Jones <davej@suse.de> :
16 * Removed tests for max-bomb-segments, which was breaking elvtune
17 * when run without -bN
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
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/module.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/compiler.h>
34 #include <linux/delay.h>
35 #include <linux/blktrace_api.h>
36 #include <linux/hash.h>
37 #include <linux/uaccess.h>
39 #include <trace/events/block.h>
41 #include "blk.h"
43 static DEFINE_SPINLOCK(elv_list_lock);
44 static LIST_HEAD(elv_list);
47 * Merge hash stuff.
49 static const int elv_hash_shift = 6;
50 #define ELV_HASH_BLOCK(sec) ((sec) >> 3)
51 #define ELV_HASH_FN(sec) \
52 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
53 #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
54 #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
57 * Query io scheduler to see if the current process issuing bio may be
58 * merged with rq.
60 static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
62 struct request_queue *q = rq->q;
63 struct elevator_queue *e = q->elevator;
65 if (e->ops->elevator_allow_merge_fn)
66 return e->ops->elevator_allow_merge_fn(q, rq, bio);
68 return 1;
72 * can we safely merge with this request?
74 int elv_rq_merge_ok(struct request *rq, struct bio *bio)
76 if (!rq_mergeable(rq))
77 return 0;
80 * Don't merge file system requests and discard requests
82 if (bio_discard(bio) != bio_discard(rq->bio))
83 return 0;
86 * different data direction or already started, don't merge
88 if (bio_data_dir(bio) != rq_data_dir(rq))
89 return 0;
92 * must be same device and not a special request
94 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
95 return 0;
98 * only merge integrity protected bio into ditto rq
100 if (bio_integrity(bio) != blk_integrity_rq(rq))
101 return 0;
103 if (!elv_iosched_allow_merge(rq, bio))
104 return 0;
106 return 1;
108 EXPORT_SYMBOL(elv_rq_merge_ok);
110 static inline int elv_try_merge(struct request *__rq, struct bio *bio)
112 int ret = ELEVATOR_NO_MERGE;
115 * we can merge and sequence is ok, check if it's possible
117 if (elv_rq_merge_ok(__rq, bio)) {
118 if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector)
119 ret = ELEVATOR_BACK_MERGE;
120 else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector)
121 ret = ELEVATOR_FRONT_MERGE;
124 return ret;
127 static struct elevator_type *elevator_find(const char *name)
129 struct elevator_type *e;
131 list_for_each_entry(e, &elv_list, list) {
132 if (!strcmp(e->elevator_name, name))
133 return e;
136 return NULL;
139 static void elevator_put(struct elevator_type *e)
141 module_put(e->elevator_owner);
144 static struct elevator_type *elevator_get(const char *name)
146 struct elevator_type *e;
148 spin_lock(&elv_list_lock);
150 e = elevator_find(name);
151 if (!e) {
152 char elv[ELV_NAME_MAX + strlen("-iosched")];
154 spin_unlock(&elv_list_lock);
156 if (!strcmp(name, "anticipatory"))
157 sprintf(elv, "as-iosched");
158 else
159 sprintf(elv, "%s-iosched", name);
161 request_module("%s", elv);
162 spin_lock(&elv_list_lock);
163 e = elevator_find(name);
166 if (e && !try_module_get(e->elevator_owner))
167 e = NULL;
169 spin_unlock(&elv_list_lock);
171 return e;
174 static void *elevator_init_queue(struct request_queue *q,
175 struct elevator_queue *eq)
177 return eq->ops->elevator_init_fn(q);
180 static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
181 void *data)
183 q->elevator = eq;
184 eq->elevator_data = data;
187 static char chosen_elevator[16];
189 static int __init elevator_setup(char *str)
192 * Be backwards-compatible with previous kernels, so users
193 * won't get the wrong elevator.
195 if (!strcmp(str, "as"))
196 strcpy(chosen_elevator, "anticipatory");
197 else
198 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
199 return 1;
202 __setup("elevator=", elevator_setup);
204 static struct kobj_type elv_ktype;
206 static struct elevator_queue *elevator_alloc(struct request_queue *q,
207 struct elevator_type *e)
209 struct elevator_queue *eq;
210 int i;
212 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
213 if (unlikely(!eq))
214 goto err;
216 eq->ops = &e->ops;
217 eq->elevator_type = e;
218 kobject_init(&eq->kobj, &elv_ktype);
219 mutex_init(&eq->sysfs_lock);
221 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
222 GFP_KERNEL, q->node);
223 if (!eq->hash)
224 goto err;
226 for (i = 0; i < ELV_HASH_ENTRIES; i++)
227 INIT_HLIST_HEAD(&eq->hash[i]);
229 return eq;
230 err:
231 kfree(eq);
232 elevator_put(e);
233 return NULL;
236 static void elevator_release(struct kobject *kobj)
238 struct elevator_queue *e;
240 e = container_of(kobj, struct elevator_queue, kobj);
241 elevator_put(e->elevator_type);
242 kfree(e->hash);
243 kfree(e);
246 int elevator_init(struct request_queue *q, char *name)
248 struct elevator_type *e = NULL;
249 struct elevator_queue *eq;
250 int ret = 0;
251 void *data;
253 INIT_LIST_HEAD(&q->queue_head);
254 q->last_merge = NULL;
255 q->end_sector = 0;
256 q->boundary_rq = NULL;
258 if (name) {
259 e = elevator_get(name);
260 if (!e)
261 return -EINVAL;
264 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);
271 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");
281 eq = elevator_alloc(q, e);
282 if (!eq)
283 return -ENOMEM;
285 data = elevator_init_queue(q, eq);
286 if (!data) {
287 kobject_put(&eq->kobj);
288 return -ENOMEM;
291 elevator_attach(q, eq, data);
292 return ret;
294 EXPORT_SYMBOL(elevator_init);
296 void elevator_exit(struct elevator_queue *e)
298 mutex_lock(&e->sysfs_lock);
299 if (e->ops->elevator_exit_fn)
300 e->ops->elevator_exit_fn(e);
301 e->ops = NULL;
302 mutex_unlock(&e->sysfs_lock);
304 kobject_put(&e->kobj);
306 EXPORT_SYMBOL(elevator_exit);
308 static inline void __elv_rqhash_del(struct request *rq)
310 hlist_del_init(&rq->hash);
313 static void elv_rqhash_del(struct request_queue *q, struct request *rq)
315 if (ELV_ON_HASH(rq))
316 __elv_rqhash_del(rq);
319 static void elv_rqhash_add(struct request_queue *q, struct request *rq)
321 struct elevator_queue *e = q->elevator;
323 BUG_ON(ELV_ON_HASH(rq));
324 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
327 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
329 __elv_rqhash_del(rq);
330 elv_rqhash_add(q, rq);
333 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
335 struct elevator_queue *e = q->elevator;
336 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
337 struct hlist_node *entry, *next;
338 struct request *rq;
340 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
341 BUG_ON(!ELV_ON_HASH(rq));
343 if (unlikely(!rq_mergeable(rq))) {
344 __elv_rqhash_del(rq);
345 continue;
348 if (rq_hash_key(rq) == offset)
349 return rq;
352 return NULL;
356 * RB-tree support functions for inserting/lookup/removal of requests
357 * in a sorted RB tree.
359 struct request *elv_rb_add(struct rb_root *root, struct request *rq)
361 struct rb_node **p = &root->rb_node;
362 struct rb_node *parent = NULL;
363 struct request *__rq;
365 while (*p) {
366 parent = *p;
367 __rq = rb_entry(parent, struct request, rb_node);
369 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
370 p = &(*p)->rb_left;
371 else if (blk_rq_pos(rq) > blk_rq_pos(__rq))
372 p = &(*p)->rb_right;
373 else
374 return __rq;
377 rb_link_node(&rq->rb_node, parent, p);
378 rb_insert_color(&rq->rb_node, root);
379 return NULL;
381 EXPORT_SYMBOL(elv_rb_add);
383 void elv_rb_del(struct rb_root *root, struct request *rq)
385 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
386 rb_erase(&rq->rb_node, root);
387 RB_CLEAR_NODE(&rq->rb_node);
389 EXPORT_SYMBOL(elv_rb_del);
391 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
393 struct rb_node *n = root->rb_node;
394 struct request *rq;
396 while (n) {
397 rq = rb_entry(n, struct request, rb_node);
399 if (sector < blk_rq_pos(rq))
400 n = n->rb_left;
401 else if (sector > blk_rq_pos(rq))
402 n = n->rb_right;
403 else
404 return rq;
407 return NULL;
409 EXPORT_SYMBOL(elv_rb_find);
412 * Insert rq into dispatch queue of q. Queue lock must be held on
413 * entry. rq is sort instead into the dispatch queue. To be used by
414 * specific elevators.
416 void elv_dispatch_sort(struct request_queue *q, struct request *rq)
418 sector_t boundary;
419 struct list_head *entry;
420 int stop_flags;
422 if (q->last_merge == rq)
423 q->last_merge = NULL;
425 elv_rqhash_del(q, rq);
427 q->nr_sorted--;
429 boundary = q->end_sector;
430 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
431 list_for_each_prev(entry, &q->queue_head) {
432 struct request *pos = list_entry_rq(entry);
434 if (blk_discard_rq(rq) != blk_discard_rq(pos))
435 break;
436 if (rq_data_dir(rq) != rq_data_dir(pos))
437 break;
438 if (pos->cmd_flags & stop_flags)
439 break;
440 if (blk_rq_pos(rq) >= boundary) {
441 if (blk_rq_pos(pos) < boundary)
442 continue;
443 } else {
444 if (blk_rq_pos(pos) >= boundary)
445 break;
447 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
448 break;
451 list_add(&rq->queuelist, entry);
453 EXPORT_SYMBOL(elv_dispatch_sort);
456 * 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.
460 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
462 if (q->last_merge == rq)
463 q->last_merge = NULL;
465 elv_rqhash_del(q, rq);
467 q->nr_sorted--;
469 q->end_sector = rq_end_sector(rq);
470 q->boundary_rq = rq;
471 list_add_tail(&rq->queuelist, &q->queue_head);
473 EXPORT_SYMBOL(elv_dispatch_add_tail);
475 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
477 struct elevator_queue *e = q->elevator;
478 struct request *__rq;
479 int ret;
482 * First try one-hit cache.
484 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;
492 if (blk_queue_nomerges(q))
493 return ELEVATOR_NO_MERGE;
496 * See if our hash lookup can find a potential backmerge.
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;
504 if (e->ops->elevator_merge_fn)
505 return e->ops->elevator_merge_fn(q, req, bio);
507 return ELEVATOR_NO_MERGE;
510 void elv_merged_request(struct request_queue *q, struct request *rq, int type)
512 struct elevator_queue *e = q->elevator;
514 if (e->ops->elevator_merged_fn)
515 e->ops->elevator_merged_fn(q, rq, type);
517 if (type == ELEVATOR_BACK_MERGE)
518 elv_rqhash_reposition(q, rq);
520 q->last_merge = rq;
523 void elv_merge_requests(struct request_queue *q, struct request *rq,
524 struct request *next)
526 struct elevator_queue *e = q->elevator;
528 if (e->ops->elevator_merge_req_fn)
529 e->ops->elevator_merge_req_fn(q, rq, next);
531 elv_rqhash_reposition(q, rq);
532 elv_rqhash_del(q, next);
534 q->nr_sorted--;
535 q->last_merge = rq;
538 void elv_requeue_request(struct request_queue *q, struct request *rq)
541 * it already went through dequeue, we need to decrement the
542 * in_flight count again
544 if (blk_account_rq(rq)) {
545 q->in_flight[rq_is_sync(rq)]--;
546 if (blk_sorted_rq(rq))
547 elv_deactivate_rq(q, rq);
550 rq->cmd_flags &= ~REQ_STARTED;
552 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
555 void elv_drain_elevator(struct request_queue *q)
557 static int printed;
558 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
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);
570 * Call with queue lock held, interrupts disabled
572 void elv_quiesce_start(struct request_queue *q)
574 if (!q->elevator)
575 return;
577 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
580 * make sure we don't have any requests in flight
582 elv_drain_elevator(q);
583 while (q->rq.elvpriv) {
584 __blk_run_queue(q);
585 spin_unlock_irq(q->queue_lock);
586 msleep(10);
587 spin_lock_irq(q->queue_lock);
588 elv_drain_elevator(q);
592 void elv_quiesce_end(struct request_queue *q)
594 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
597 void elv_insert(struct request_queue *q, struct request *rq, int where)
599 struct list_head *pos;
600 unsigned ordseq;
601 int unplug_it = 1;
603 trace_block_rq_insert(q, rq);
605 rq->q = q;
607 switch (where) {
608 case ELEVATOR_INSERT_FRONT:
609 rq->cmd_flags |= REQ_SOFTBARRIER;
611 list_add(&rq->queuelist, &q->queue_head);
612 break;
614 case ELEVATOR_INSERT_BACK:
615 rq->cmd_flags |= REQ_SOFTBARRIER;
616 elv_drain_elevator(q);
617 list_add_tail(&rq->queuelist, &q->queue_head);
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.
628 __blk_run_queue(q);
629 break;
631 case ELEVATOR_INSERT_SORT:
632 BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
633 rq->cmd_flags |= REQ_SORTED;
634 q->nr_sorted++;
635 if (rq_mergeable(rq)) {
636 elv_rqhash_add(q, rq);
637 if (!q->last_merge)
638 q->last_merge = rq;
642 * Some ioscheds (cfq) run q->request_fn directly, so
643 * rq cannot be accessed after calling
644 * elevator_add_req_fn.
646 q->elevator->ops->elevator_add_req_fn(q, rq);
647 break;
649 case ELEVATOR_INSERT_REQUEUE:
651 * If ordered flush isn't in progress, we do front
652 * insertion; otherwise, requests should be requeued
653 * in ordseq order.
655 rq->cmd_flags |= REQ_SOFTBARRIER;
658 * Most requeues happen because of a busy condition,
659 * don't force unplug of the queue for that case.
661 unplug_it = 0;
663 if (q->ordseq == 0) {
664 list_add(&rq->queuelist, &q->queue_head);
665 break;
668 ordseq = blk_ordered_req_seq(rq);
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;
676 list_add_tail(&rq->queuelist, pos);
677 break;
679 default:
680 printk(KERN_ERR "%s: bad insertion point %d\n",
681 __func__, where);
682 BUG();
685 if (unplug_it && blk_queue_plugged(q)) {
686 int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC]
687 - queue_in_flight(q);
689 if (nrq >= q->unplug_thresh)
690 __generic_unplug_device(q);
694 void __elv_add_request(struct request_queue *q, struct request *rq, int where,
695 int plug)
697 if (q->ordcolor)
698 rq->cmd_flags |= REQ_ORDERED_COLOR;
700 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
702 * toggle ordered color
704 if (blk_barrier_rq(rq))
705 q->ordcolor ^= 1;
708 * barriers implicitly indicate back insertion
710 if (where == ELEVATOR_INSERT_SORT)
711 where = ELEVATOR_INSERT_BACK;
714 * this request is scheduling boundary, update
715 * end_sector
717 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
718 q->end_sector = rq_end_sector(rq);
719 q->boundary_rq = rq;
721 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
722 where == ELEVATOR_INSERT_SORT)
723 where = ELEVATOR_INSERT_BACK;
725 if (plug)
726 blk_plug_device(q);
728 elv_insert(q, rq, where);
730 EXPORT_SYMBOL(__elv_add_request);
732 void elv_add_request(struct request_queue *q, struct request *rq, int where,
733 int plug)
735 unsigned long flags;
737 spin_lock_irqsave(q->queue_lock, flags);
738 __elv_add_request(q, rq, where, plug);
739 spin_unlock_irqrestore(q->queue_lock, flags);
741 EXPORT_SYMBOL(elv_add_request);
743 int elv_queue_empty(struct request_queue *q)
745 struct elevator_queue *e = q->elevator;
747 if (!list_empty(&q->queue_head))
748 return 0;
750 if (e->ops->elevator_queue_empty_fn)
751 return e->ops->elevator_queue_empty_fn(q);
753 return 1;
755 EXPORT_SYMBOL(elv_queue_empty);
757 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
759 struct elevator_queue *e = q->elevator;
761 if (e->ops->elevator_latter_req_fn)
762 return e->ops->elevator_latter_req_fn(q, rq);
763 return NULL;
766 struct request *elv_former_request(struct request_queue *q, struct request *rq)
768 struct elevator_queue *e = q->elevator;
770 if (e->ops->elevator_former_req_fn)
771 return e->ops->elevator_former_req_fn(q, rq);
772 return NULL;
775 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
777 struct elevator_queue *e = q->elevator;
779 if (e->ops->elevator_set_req_fn)
780 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
782 rq->elevator_private = NULL;
783 return 0;
786 void elv_put_request(struct request_queue *q, struct request *rq)
788 struct elevator_queue *e = q->elevator;
790 if (e->ops->elevator_put_req_fn)
791 e->ops->elevator_put_req_fn(rq);
794 int elv_may_queue(struct request_queue *q, int rw)
796 struct elevator_queue *e = q->elevator;
798 if (e->ops->elevator_may_queue_fn)
799 return e->ops->elevator_may_queue_fn(q, rw);
801 return ELV_MQUEUE_MAY;
804 void elv_abort_queue(struct request_queue *q)
806 struct request *rq;
808 while (!list_empty(&q->queue_head)) {
809 rq = list_entry_rq(q->queue_head.next);
810 rq->cmd_flags |= REQ_QUIET;
811 trace_block_rq_abort(q, rq);
813 * Mark this request as started so we don't trigger
814 * any debug logic in the end I/O path.
816 blk_start_request(rq);
817 __blk_end_request_all(rq, -EIO);
820 EXPORT_SYMBOL(elv_abort_queue);
822 void elv_completed_request(struct request_queue *q, struct request *rq)
824 struct elevator_queue *e = q->elevator;
827 * request is released from the driver, io must be done
829 if (blk_account_rq(rq)) {
830 q->in_flight[rq_is_sync(rq)]--;
831 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
832 e->ops->elevator_completed_req_fn(q, rq);
836 * Check if the queue is waiting for fs requests to be
837 * drained for flush sequence.
839 if (unlikely(q->ordseq)) {
840 struct request *next = NULL;
842 if (!list_empty(&q->queue_head))
843 next = list_entry_rq(q->queue_head.next);
845 if (!queue_in_flight(q) &&
846 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
847 (!next || blk_ordered_req_seq(next) > QUEUE_ORDSEQ_DRAIN)) {
848 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
849 __blk_run_queue(q);
854 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
856 static ssize_t
857 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
859 struct elv_fs_entry *entry = to_elv(attr);
860 struct elevator_queue *e;
861 ssize_t error;
863 if (!entry->show)
864 return -EIO;
866 e = container_of(kobj, struct elevator_queue, kobj);
867 mutex_lock(&e->sysfs_lock);
868 error = e->ops ? entry->show(e, page) : -ENOENT;
869 mutex_unlock(&e->sysfs_lock);
870 return error;
873 static ssize_t
874 elv_attr_store(struct kobject *kobj, struct attribute *attr,
875 const char *page, size_t length)
877 struct elv_fs_entry *entry = to_elv(attr);
878 struct elevator_queue *e;
879 ssize_t error;
881 if (!entry->store)
882 return -EIO;
884 e = container_of(kobj, struct elevator_queue, kobj);
885 mutex_lock(&e->sysfs_lock);
886 error = e->ops ? entry->store(e, page, length) : -ENOENT;
887 mutex_unlock(&e->sysfs_lock);
888 return error;
891 static struct sysfs_ops elv_sysfs_ops = {
892 .show = elv_attr_show,
893 .store = elv_attr_store,
896 static struct kobj_type elv_ktype = {
897 .sysfs_ops = &elv_sysfs_ops,
898 .release = elevator_release,
901 int elv_register_queue(struct request_queue *q)
903 struct elevator_queue *e = q->elevator;
904 int error;
906 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
907 if (!error) {
908 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
909 if (attr) {
910 while (attr->attr.name) {
911 if (sysfs_create_file(&e->kobj, &attr->attr))
912 break;
913 attr++;
916 kobject_uevent(&e->kobj, KOBJ_ADD);
918 return error;
921 static void __elv_unregister_queue(struct elevator_queue *e)
923 kobject_uevent(&e->kobj, KOBJ_REMOVE);
924 kobject_del(&e->kobj);
927 void elv_unregister_queue(struct request_queue *q)
929 if (q)
930 __elv_unregister_queue(q->elevator);
933 void elv_register(struct elevator_type *e)
935 char *def = "";
937 spin_lock(&elv_list_lock);
938 BUG_ON(elevator_find(e->elevator_name));
939 list_add_tail(&e->list, &elv_list);
940 spin_unlock(&elv_list_lock);
942 if (!strcmp(e->elevator_name, chosen_elevator) ||
943 (!*chosen_elevator &&
944 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
945 def = " (default)";
947 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
948 def);
950 EXPORT_SYMBOL_GPL(elv_register);
952 void elv_unregister(struct elevator_type *e)
954 struct task_struct *g, *p;
957 * Iterate every thread in the process to remove the io contexts.
959 if (e->ops.trim) {
960 read_lock(&tasklist_lock);
961 do_each_thread(g, p) {
962 task_lock(p);
963 if (p->io_context)
964 e->ops.trim(p->io_context);
965 task_unlock(p);
966 } while_each_thread(g, p);
967 read_unlock(&tasklist_lock);
970 spin_lock(&elv_list_lock);
971 list_del_init(&e->list);
972 spin_unlock(&elv_list_lock);
974 EXPORT_SYMBOL_GPL(elv_unregister);
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
980 * one, if the new one fails init for some reason.
982 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
984 struct elevator_queue *old_elevator, *e;
985 void *data;
988 * Allocate new elevator
990 e = elevator_alloc(q, new_e);
991 if (!e)
992 return 0;
994 data = elevator_init_queue(q, e);
995 if (!data) {
996 kobject_put(&e->kobj);
997 return 0;
1001 * Turn on BYPASS and drain all requests w/ elevator private data
1003 spin_lock_irq(q->queue_lock);
1004 elv_quiesce_start(q);
1007 * Remember old elevator.
1009 old_elevator = q->elevator;
1012 * attach and start new elevator
1014 elevator_attach(q, e, data);
1016 spin_unlock_irq(q->queue_lock);
1018 __elv_unregister_queue(old_elevator);
1020 if (elv_register_queue(q))
1021 goto fail_register;
1024 * finally exit old elevator and turn off BYPASS.
1026 elevator_exit(old_elevator);
1027 spin_lock_irq(q->queue_lock);
1028 elv_quiesce_end(q);
1029 spin_unlock_irq(q->queue_lock);
1031 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
1033 return 1;
1035 fail_register:
1037 * switch failed, exit the new io scheduler and reattach the old
1038 * one again (along with re-adding the sysfs dir)
1040 elevator_exit(e);
1041 q->elevator = old_elevator;
1042 elv_register_queue(q);
1044 spin_lock_irq(q->queue_lock);
1045 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1046 spin_unlock_irq(q->queue_lock);
1048 return 0;
1051 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1052 size_t count)
1054 char elevator_name[ELV_NAME_MAX];
1055 struct elevator_type *e;
1057 if (!q->elevator)
1058 return count;
1060 strlcpy(elevator_name, name, sizeof(elevator_name));
1061 strstrip(elevator_name);
1063 e = elevator_get(elevator_name);
1064 if (!e) {
1065 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1066 return -EINVAL;
1069 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1070 elevator_put(e);
1071 return count;
1074 if (!elevator_switch(q, e))
1075 printk(KERN_ERR "elevator: switch to %s failed\n",
1076 elevator_name);
1077 return count;
1080 ssize_t elv_iosched_show(struct request_queue *q, char *name)
1082 struct elevator_queue *e = q->elevator;
1083 struct elevator_type *elv;
1084 struct elevator_type *__e;
1085 int len = 0;
1087 if (!q->elevator)
1088 return sprintf(name, "none\n");
1090 elv = e->elevator_type;
1092 spin_lock(&elv_list_lock);
1093 list_for_each_entry(__e, &elv_list, list) {
1094 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);
1099 spin_unlock(&elv_list_lock);
1101 len += sprintf(len+name, "\n");
1102 return len;
1105 struct request *elv_rb_former_request(struct request_queue *q,
1106 struct request *rq)
1108 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1110 if (rbprev)
1111 return rb_entry_rq(rbprev);
1113 return NULL;
1115 EXPORT_SYMBOL(elv_rb_former_request);
1117 struct request *elv_rb_latter_request(struct request_queue *q,
1118 struct request *rq)
1120 struct rb_node *rbnext = rb_next(&rq->rb_node);
1122 if (rbnext)
1123 return rb_entry_rq(rbnext);
1125 return NULL;
1127 EXPORT_SYMBOL(elv_rb_latter_request);