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
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
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>
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 static DEFINE_SPINLOCK(elv_list_lock);
40 static LIST_HEAD(elv_list);
45 static const int elv_hash_shift = 6;
46 #define ELV_HASH_BLOCK(sec) ((sec) >> 3)
47 #define ELV_HASH_FN(sec) \
48 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
49 #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
50 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
51 #define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
54 * Query io scheduler to see if the current process issuing bio may be
57 static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
59 struct request_queue *q = rq->q;
60 elevator_t *e = q->elevator;
62 if (e->ops->elevator_allow_merge_fn)
63 return e->ops->elevator_allow_merge_fn(q, rq, bio);
69 * can we safely merge with this request?
71 int elv_rq_merge_ok(struct request *rq, struct bio *bio)
73 if (!rq_mergeable(rq))
77 * Don't merge file system requests and discard requests
79 if (bio_discard(bio) != bio_discard(rq->bio))
83 * different data direction or already started, don't merge
85 if (bio_data_dir(bio) != rq_data_dir(rq))
89 * must be same device and not a special request
91 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
95 * only merge integrity protected bio into ditto rq
97 if (bio_integrity(bio) != blk_integrity_rq(rq))
100 if (!elv_iosched_allow_merge(rq, bio))
105 EXPORT_SYMBOL(elv_rq_merge_ok);
107 static inline int elv_try_merge(struct request *__rq, struct bio *bio)
109 int ret = ELEVATOR_NO_MERGE;
112 * we can merge and sequence is ok, check if it's possible
114 if (elv_rq_merge_ok(__rq, bio)) {
115 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
116 ret = ELEVATOR_BACK_MERGE;
117 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
118 ret = ELEVATOR_FRONT_MERGE;
124 static struct elevator_type *elevator_find(const char *name)
126 struct elevator_type *e;
128 list_for_each_entry(e, &elv_list, list) {
129 if (!strcmp(e->elevator_name, name))
136 static void elevator_put(struct elevator_type *e)
138 module_put(e->elevator_owner);
141 static struct elevator_type *elevator_get(const char *name)
143 struct elevator_type *e;
145 spin_lock(&elv_list_lock);
147 e = elevator_find(name);
149 char elv[ELV_NAME_MAX + strlen("-iosched")];
151 spin_unlock(&elv_list_lock);
153 if (!strcmp(name, "anticipatory"))
154 sprintf(elv, "as-iosched");
156 sprintf(elv, "%s-iosched", name);
158 request_module("%s", elv);
159 spin_lock(&elv_list_lock);
160 e = elevator_find(name);
163 if (e && !try_module_get(e->elevator_owner))
166 spin_unlock(&elv_list_lock);
171 static void *elevator_init_queue(struct request_queue *q,
172 struct elevator_queue *eq)
174 return eq->ops->elevator_init_fn(q);
177 static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
181 eq->elevator_data = data;
184 static char chosen_elevator[16];
186 static int __init elevator_setup(char *str)
189 * Be backwards-compatible with previous kernels, so users
190 * won't get the wrong elevator.
192 if (!strcmp(str, "as"))
193 strcpy(chosen_elevator, "anticipatory");
195 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
199 __setup("elevator=", elevator_setup);
201 static struct kobj_type elv_ktype;
203 static elevator_t *elevator_alloc(struct request_queue *q,
204 struct elevator_type *e)
209 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
214 eq->elevator_type = e;
215 kobject_init(&eq->kobj, &elv_ktype);
216 mutex_init(&eq->sysfs_lock);
218 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
219 GFP_KERNEL, q->node);
223 for (i = 0; i < ELV_HASH_ENTRIES; i++)
224 INIT_HLIST_HEAD(&eq->hash[i]);
233 static void elevator_release(struct kobject *kobj)
235 elevator_t *e = container_of(kobj, elevator_t, kobj);
237 elevator_put(e->elevator_type);
242 int elevator_init(struct request_queue *q, char *name)
244 struct elevator_type *e = NULL;
245 struct elevator_queue *eq;
249 INIT_LIST_HEAD(&q->queue_head);
250 q->last_merge = NULL;
252 q->boundary_rq = NULL;
255 e = elevator_get(name);
260 if (!e && *chosen_elevator) {
261 e = elevator_get(chosen_elevator);
263 printk(KERN_ERR "I/O scheduler %s not found\n",
268 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
271 "Default I/O scheduler not found. " \
273 e = elevator_get("noop");
277 eq = elevator_alloc(q, e);
281 data = elevator_init_queue(q, eq);
283 kobject_put(&eq->kobj);
287 elevator_attach(q, eq, data);
290 EXPORT_SYMBOL(elevator_init);
292 void elevator_exit(elevator_t *e)
294 mutex_lock(&e->sysfs_lock);
295 if (e->ops->elevator_exit_fn)
296 e->ops->elevator_exit_fn(e);
298 mutex_unlock(&e->sysfs_lock);
300 kobject_put(&e->kobj);
302 EXPORT_SYMBOL(elevator_exit);
304 static void elv_activate_rq(struct request_queue *q, struct request *rq)
306 elevator_t *e = q->elevator;
308 if (e->ops->elevator_activate_req_fn)
309 e->ops->elevator_activate_req_fn(q, rq);
312 static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
314 elevator_t *e = q->elevator;
316 if (e->ops->elevator_deactivate_req_fn)
317 e->ops->elevator_deactivate_req_fn(q, rq);
320 static inline void __elv_rqhash_del(struct request *rq)
322 hlist_del_init(&rq->hash);
325 static void elv_rqhash_del(struct request_queue *q, struct request *rq)
328 __elv_rqhash_del(rq);
331 static void elv_rqhash_add(struct request_queue *q, struct request *rq)
333 elevator_t *e = q->elevator;
335 BUG_ON(ELV_ON_HASH(rq));
336 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
339 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
341 __elv_rqhash_del(rq);
342 elv_rqhash_add(q, rq);
345 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
347 elevator_t *e = q->elevator;
348 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
349 struct hlist_node *entry, *next;
352 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
353 BUG_ON(!ELV_ON_HASH(rq));
355 if (unlikely(!rq_mergeable(rq))) {
356 __elv_rqhash_del(rq);
360 if (rq_hash_key(rq) == offset)
368 * RB-tree support functions for inserting/lookup/removal of requests
369 * in a sorted RB tree.
371 struct request *elv_rb_add(struct rb_root *root, struct request *rq)
373 struct rb_node **p = &root->rb_node;
374 struct rb_node *parent = NULL;
375 struct request *__rq;
379 __rq = rb_entry(parent, struct request, rb_node);
381 if (rq->sector < __rq->sector)
383 else if (rq->sector > __rq->sector)
389 rb_link_node(&rq->rb_node, parent, p);
390 rb_insert_color(&rq->rb_node, root);
393 EXPORT_SYMBOL(elv_rb_add);
395 void elv_rb_del(struct rb_root *root, struct request *rq)
397 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
398 rb_erase(&rq->rb_node, root);
399 RB_CLEAR_NODE(&rq->rb_node);
401 EXPORT_SYMBOL(elv_rb_del);
403 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
405 struct rb_node *n = root->rb_node;
409 rq = rb_entry(n, struct request, rb_node);
411 if (sector < rq->sector)
413 else if (sector > rq->sector)
421 EXPORT_SYMBOL(elv_rb_find);
424 * Insert rq into dispatch queue of q. Queue lock must be held on
425 * entry. rq is sort instead into the dispatch queue. To be used by
426 * specific elevators.
428 void elv_dispatch_sort(struct request_queue *q, struct request *rq)
431 struct list_head *entry;
434 if (q->last_merge == rq)
435 q->last_merge = NULL;
437 elv_rqhash_del(q, rq);
441 boundary = q->end_sector;
442 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
443 list_for_each_prev(entry, &q->queue_head) {
444 struct request *pos = list_entry_rq(entry);
446 if (blk_discard_rq(rq) != blk_discard_rq(pos))
448 if (rq_data_dir(rq) != rq_data_dir(pos))
450 if (pos->cmd_flags & stop_flags)
452 if (rq->sector >= boundary) {
453 if (pos->sector < boundary)
456 if (pos->sector >= boundary)
459 if (rq->sector >= pos->sector)
463 list_add(&rq->queuelist, entry);
465 EXPORT_SYMBOL(elv_dispatch_sort);
468 * Insert rq into dispatch queue of q. Queue lock must be held on
469 * entry. rq is added to the back of the dispatch queue. To be used by
470 * specific elevators.
472 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
474 if (q->last_merge == rq)
475 q->last_merge = NULL;
477 elv_rqhash_del(q, rq);
481 q->end_sector = rq_end_sector(rq);
483 list_add_tail(&rq->queuelist, &q->queue_head);
485 EXPORT_SYMBOL(elv_dispatch_add_tail);
487 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
489 elevator_t *e = q->elevator;
490 struct request *__rq;
494 * First try one-hit cache.
497 ret = elv_try_merge(q->last_merge, bio);
498 if (ret != ELEVATOR_NO_MERGE) {
499 *req = q->last_merge;
504 if (blk_queue_nomerges(q))
505 return ELEVATOR_NO_MERGE;
508 * See if our hash lookup can find a potential backmerge.
510 __rq = elv_rqhash_find(q, bio->bi_sector);
511 if (__rq && elv_rq_merge_ok(__rq, bio)) {
513 return ELEVATOR_BACK_MERGE;
516 if (e->ops->elevator_merge_fn)
517 return e->ops->elevator_merge_fn(q, req, bio);
519 return ELEVATOR_NO_MERGE;
522 void elv_merged_request(struct request_queue *q, struct request *rq, int type)
524 elevator_t *e = q->elevator;
526 if (e->ops->elevator_merged_fn)
527 e->ops->elevator_merged_fn(q, rq, type);
529 if (type == ELEVATOR_BACK_MERGE)
530 elv_rqhash_reposition(q, rq);
535 void elv_merge_requests(struct request_queue *q, struct request *rq,
536 struct request *next)
538 elevator_t *e = q->elevator;
540 if (e->ops->elevator_merge_req_fn)
541 e->ops->elevator_merge_req_fn(q, rq, next);
543 elv_rqhash_reposition(q, rq);
544 elv_rqhash_del(q, next);
550 void elv_requeue_request(struct request_queue *q, struct request *rq)
553 * it already went through dequeue, we need to decrement the
554 * in_flight count again
556 if (blk_account_rq(rq)) {
558 if (blk_sorted_rq(rq))
559 elv_deactivate_rq(q, rq);
562 rq->cmd_flags &= ~REQ_STARTED;
564 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
567 static void elv_drain_elevator(struct request_queue *q)
570 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
572 if (q->nr_sorted == 0)
574 if (printed++ < 10) {
575 printk(KERN_ERR "%s: forced dispatching is broken "
576 "(nr_sorted=%u), please report this\n",
577 q->elevator->elevator_type->elevator_name, q->nr_sorted);
581 void elv_insert(struct request_queue *q, struct request *rq, int where)
583 struct list_head *pos;
587 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
592 case ELEVATOR_INSERT_FRONT:
593 rq->cmd_flags |= REQ_SOFTBARRIER;
595 list_add(&rq->queuelist, &q->queue_head);
598 case ELEVATOR_INSERT_BACK:
599 rq->cmd_flags |= REQ_SOFTBARRIER;
600 elv_drain_elevator(q);
601 list_add_tail(&rq->queuelist, &q->queue_head);
603 * We kick the queue here for the following reasons.
604 * - The elevator might have returned NULL previously
605 * to delay requests and returned them now. As the
606 * queue wasn't empty before this request, ll_rw_blk
607 * won't run the queue on return, resulting in hang.
608 * - Usually, back inserted requests won't be merged
609 * with anything. There's no point in delaying queue
616 case ELEVATOR_INSERT_SORT:
617 BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
618 rq->cmd_flags |= REQ_SORTED;
620 if (rq_mergeable(rq)) {
621 elv_rqhash_add(q, rq);
627 * Some ioscheds (cfq) run q->request_fn directly, so
628 * rq cannot be accessed after calling
629 * elevator_add_req_fn.
631 q->elevator->ops->elevator_add_req_fn(q, rq);
634 case ELEVATOR_INSERT_REQUEUE:
636 * If ordered flush isn't in progress, we do front
637 * insertion; otherwise, requests should be requeued
640 rq->cmd_flags |= REQ_SOFTBARRIER;
643 * Most requeues happen because of a busy condition,
644 * don't force unplug of the queue for that case.
648 if (q->ordseq == 0) {
649 list_add(&rq->queuelist, &q->queue_head);
653 ordseq = blk_ordered_req_seq(rq);
655 list_for_each(pos, &q->queue_head) {
656 struct request *pos_rq = list_entry_rq(pos);
657 if (ordseq <= blk_ordered_req_seq(pos_rq))
661 list_add_tail(&rq->queuelist, pos);
665 printk(KERN_ERR "%s: bad insertion point %d\n",
670 if (unplug_it && blk_queue_plugged(q)) {
671 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
674 if (nrq >= q->unplug_thresh)
675 __generic_unplug_device(q);
679 void __elv_add_request(struct request_queue *q, struct request *rq, int where,
683 rq->cmd_flags |= REQ_ORDERED_COLOR;
685 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
687 * toggle ordered color
689 if (blk_barrier_rq(rq))
693 * barriers implicitly indicate back insertion
695 if (where == ELEVATOR_INSERT_SORT)
696 where = ELEVATOR_INSERT_BACK;
699 * this request is scheduling boundary, update
702 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
703 q->end_sector = rq_end_sector(rq);
706 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
707 where == ELEVATOR_INSERT_SORT)
708 where = ELEVATOR_INSERT_BACK;
713 elv_insert(q, rq, where);
715 EXPORT_SYMBOL(__elv_add_request);
717 void elv_add_request(struct request_queue *q, struct request *rq, int where,
722 spin_lock_irqsave(q->queue_lock, flags);
723 __elv_add_request(q, rq, where, plug);
724 spin_unlock_irqrestore(q->queue_lock, flags);
726 EXPORT_SYMBOL(elv_add_request);
728 static inline struct request *__elv_next_request(struct request_queue *q)
733 while (!list_empty(&q->queue_head)) {
734 rq = list_entry_rq(q->queue_head.next);
735 if (blk_do_ordered(q, &rq))
739 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
744 struct request *elv_next_request(struct request_queue *q)
749 while ((rq = __elv_next_request(q)) != NULL) {
751 * Kill the empty barrier place holder, the driver must
754 if (blk_empty_barrier(rq)) {
755 end_queued_request(rq, 1);
758 if (!(rq->cmd_flags & REQ_STARTED)) {
760 * This is the first time the device driver
761 * sees this request (possibly after
762 * requeueing). Notify IO scheduler.
764 if (blk_sorted_rq(rq))
765 elv_activate_rq(q, rq);
768 * just mark as started even if we don't start
769 * it, a request that has been delayed should
770 * not be passed by new incoming requests
772 rq->cmd_flags |= REQ_STARTED;
773 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
776 if (!q->boundary_rq || q->boundary_rq == rq) {
777 q->end_sector = rq_end_sector(rq);
778 q->boundary_rq = NULL;
781 if (rq->cmd_flags & REQ_DONTPREP)
784 if (q->dma_drain_size && rq->data_len) {
786 * make sure space for the drain appears we
787 * know we can do this because max_hw_segments
788 * has been adjusted to be one fewer than the
791 rq->nr_phys_segments++;
797 ret = q->prep_rq_fn(q, rq);
798 if (ret == BLKPREP_OK) {
800 } else if (ret == BLKPREP_DEFER) {
802 * the request may have been (partially) prepped.
803 * we need to keep this request in the front to
804 * avoid resource deadlock. REQ_STARTED will
805 * prevent other fs requests from passing this one.
807 if (q->dma_drain_size && rq->data_len &&
808 !(rq->cmd_flags & REQ_DONTPREP)) {
810 * remove the space for the drain we added
811 * so that we don't add it again
813 --rq->nr_phys_segments;
818 } else if (ret == BLKPREP_KILL) {
819 rq->cmd_flags |= REQ_QUIET;
820 end_queued_request(rq, 0);
822 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
829 EXPORT_SYMBOL(elv_next_request);
831 void elv_dequeue_request(struct request_queue *q, struct request *rq)
833 BUG_ON(list_empty(&rq->queuelist));
834 BUG_ON(ELV_ON_HASH(rq));
836 list_del_init(&rq->queuelist);
839 * the time frame between a request being removed from the lists
840 * and to it is freed is accounted as io that is in progress at
843 if (blk_account_rq(rq))
846 EXPORT_SYMBOL(elv_dequeue_request);
848 int elv_queue_empty(struct request_queue *q)
850 elevator_t *e = q->elevator;
852 if (!list_empty(&q->queue_head))
855 if (e->ops->elevator_queue_empty_fn)
856 return e->ops->elevator_queue_empty_fn(q);
860 EXPORT_SYMBOL(elv_queue_empty);
862 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
864 elevator_t *e = q->elevator;
866 if (e->ops->elevator_latter_req_fn)
867 return e->ops->elevator_latter_req_fn(q, rq);
871 struct request *elv_former_request(struct request_queue *q, struct request *rq)
873 elevator_t *e = q->elevator;
875 if (e->ops->elevator_former_req_fn)
876 return e->ops->elevator_former_req_fn(q, rq);
880 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
882 elevator_t *e = q->elevator;
884 if (e->ops->elevator_set_req_fn)
885 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
887 rq->elevator_private = NULL;
891 void elv_put_request(struct request_queue *q, struct request *rq)
893 elevator_t *e = q->elevator;
895 if (e->ops->elevator_put_req_fn)
896 e->ops->elevator_put_req_fn(rq);
899 int elv_may_queue(struct request_queue *q, int rw)
901 elevator_t *e = q->elevator;
903 if (e->ops->elevator_may_queue_fn)
904 return e->ops->elevator_may_queue_fn(q, rw);
906 return ELV_MQUEUE_MAY;
909 void elv_completed_request(struct request_queue *q, struct request *rq)
911 elevator_t *e = q->elevator;
914 * request is released from the driver, io must be done
916 if (blk_account_rq(rq)) {
918 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
919 e->ops->elevator_completed_req_fn(q, rq);
923 * Check if the queue is waiting for fs requests to be
924 * drained for flush sequence.
926 if (unlikely(q->ordseq)) {
927 struct request *first_rq = list_entry_rq(q->queue_head.next);
928 if (q->in_flight == 0 &&
929 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
930 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
931 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
937 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
940 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
942 elevator_t *e = container_of(kobj, elevator_t, kobj);
943 struct elv_fs_entry *entry = to_elv(attr);
949 mutex_lock(&e->sysfs_lock);
950 error = e->ops ? entry->show(e, page) : -ENOENT;
951 mutex_unlock(&e->sysfs_lock);
956 elv_attr_store(struct kobject *kobj, struct attribute *attr,
957 const char *page, size_t length)
959 elevator_t *e = container_of(kobj, elevator_t, kobj);
960 struct elv_fs_entry *entry = to_elv(attr);
966 mutex_lock(&e->sysfs_lock);
967 error = e->ops ? entry->store(e, page, length) : -ENOENT;
968 mutex_unlock(&e->sysfs_lock);
972 static struct sysfs_ops elv_sysfs_ops = {
973 .show = elv_attr_show,
974 .store = elv_attr_store,
977 static struct kobj_type elv_ktype = {
978 .sysfs_ops = &elv_sysfs_ops,
979 .release = elevator_release,
982 int elv_register_queue(struct request_queue *q)
984 elevator_t *e = q->elevator;
987 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
989 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
991 while (attr->attr.name) {
992 if (sysfs_create_file(&e->kobj, &attr->attr))
997 kobject_uevent(&e->kobj, KOBJ_ADD);
1002 static void __elv_unregister_queue(elevator_t *e)
1004 kobject_uevent(&e->kobj, KOBJ_REMOVE);
1005 kobject_del(&e->kobj);
1008 void elv_unregister_queue(struct request_queue *q)
1011 __elv_unregister_queue(q->elevator);
1014 void elv_register(struct elevator_type *e)
1018 spin_lock(&elv_list_lock);
1019 BUG_ON(elevator_find(e->elevator_name));
1020 list_add_tail(&e->list, &elv_list);
1021 spin_unlock(&elv_list_lock);
1023 if (!strcmp(e->elevator_name, chosen_elevator) ||
1024 (!*chosen_elevator &&
1025 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
1028 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
1031 EXPORT_SYMBOL_GPL(elv_register);
1033 void elv_unregister(struct elevator_type *e)
1035 struct task_struct *g, *p;
1038 * Iterate every thread in the process to remove the io contexts.
1041 read_lock(&tasklist_lock);
1042 do_each_thread(g, p) {
1045 e->ops.trim(p->io_context);
1047 } while_each_thread(g, p);
1048 read_unlock(&tasklist_lock);
1051 spin_lock(&elv_list_lock);
1052 list_del_init(&e->list);
1053 spin_unlock(&elv_list_lock);
1055 EXPORT_SYMBOL_GPL(elv_unregister);
1058 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1059 * we don't free the old io scheduler, before we have allocated what we
1060 * need for the new one. this way we have a chance of going back to the old
1061 * one, if the new one fails init for some reason.
1063 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1065 elevator_t *old_elevator, *e;
1069 * Allocate new elevator
1071 e = elevator_alloc(q, new_e);
1075 data = elevator_init_queue(q, e);
1077 kobject_put(&e->kobj);
1082 * Turn on BYPASS and drain all requests w/ elevator private data
1084 spin_lock_irq(q->queue_lock);
1086 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
1088 elv_drain_elevator(q);
1090 while (q->rq.elvpriv) {
1093 spin_unlock_irq(q->queue_lock);
1095 spin_lock_irq(q->queue_lock);
1096 elv_drain_elevator(q);
1100 * Remember old elevator.
1102 old_elevator = q->elevator;
1105 * attach and start new elevator
1107 elevator_attach(q, e, data);
1109 spin_unlock_irq(q->queue_lock);
1111 __elv_unregister_queue(old_elevator);
1113 if (elv_register_queue(q))
1117 * finally exit old elevator and turn off BYPASS.
1119 elevator_exit(old_elevator);
1120 spin_lock_irq(q->queue_lock);
1121 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1122 spin_unlock_irq(q->queue_lock);
1124 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
1130 * switch failed, exit the new io scheduler and reattach the old
1131 * one again (along with re-adding the sysfs dir)
1134 q->elevator = old_elevator;
1135 elv_register_queue(q);
1137 spin_lock_irq(q->queue_lock);
1138 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1139 spin_unlock_irq(q->queue_lock);
1144 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1147 char elevator_name[ELV_NAME_MAX];
1149 struct elevator_type *e;
1151 elevator_name[sizeof(elevator_name) - 1] = '\0';
1152 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1153 len = strlen(elevator_name);
1155 if (len && elevator_name[len - 1] == '\n')
1156 elevator_name[len - 1] = '\0';
1158 e = elevator_get(elevator_name);
1160 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1164 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1169 if (!elevator_switch(q, e))
1170 printk(KERN_ERR "elevator: switch to %s failed\n",
1175 ssize_t elv_iosched_show(struct request_queue *q, char *name)
1177 elevator_t *e = q->elevator;
1178 struct elevator_type *elv = e->elevator_type;
1179 struct elevator_type *__e;
1182 spin_lock(&elv_list_lock);
1183 list_for_each_entry(__e, &elv_list, list) {
1184 if (!strcmp(elv->elevator_name, __e->elevator_name))
1185 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1187 len += sprintf(name+len, "%s ", __e->elevator_name);
1189 spin_unlock(&elv_list_lock);
1191 len += sprintf(len+name, "\n");
1195 struct request *elv_rb_former_request(struct request_queue *q,
1198 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1201 return rb_entry_rq(rbprev);
1205 EXPORT_SYMBOL(elv_rb_former_request);
1207 struct request *elv_rb_latter_request(struct request_queue *q,
1210 struct rb_node *rbnext = rb_next(&rq->rb_node);
1213 return rb_entry_rq(rbnext);
1217 EXPORT_SYMBOL(elv_rb_latter_request);