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>
38 #include <asm/uaccess.h>
40 static DEFINE_SPINLOCK(elv_list_lock
);
41 static LIST_HEAD(elv_list
);
46 static const int elv_hash_shift
= 6;
47 #define ELV_HASH_BLOCK(sec) ((sec) >> 3)
48 #define ELV_HASH_FN(sec) (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 * can we safely merge with this request?
56 inline int elv_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
58 if (!rq_mergeable(rq
))
62 * different data direction or already started, don't merge
64 if (bio_data_dir(bio
) != rq_data_dir(rq
))
68 * same device and no special stuff set, merge is ok
70 if (rq
->rq_disk
== bio
->bi_bdev
->bd_disk
&& !rq
->special
)
75 EXPORT_SYMBOL(elv_rq_merge_ok
);
77 static inline int elv_try_merge(struct request
*__rq
, struct bio
*bio
)
79 int ret
= ELEVATOR_NO_MERGE
;
82 * we can merge and sequence is ok, check if it's possible
84 if (elv_rq_merge_ok(__rq
, bio
)) {
85 if (__rq
->sector
+ __rq
->nr_sectors
== bio
->bi_sector
)
86 ret
= ELEVATOR_BACK_MERGE
;
87 else if (__rq
->sector
- bio_sectors(bio
) == bio
->bi_sector
)
88 ret
= ELEVATOR_FRONT_MERGE
;
94 static struct elevator_type
*elevator_find(const char *name
)
96 struct elevator_type
*e
;
97 struct list_head
*entry
;
99 list_for_each(entry
, &elv_list
) {
101 e
= list_entry(entry
, struct elevator_type
, list
);
103 if (!strcmp(e
->elevator_name
, name
))
110 static void elevator_put(struct elevator_type
*e
)
112 module_put(e
->elevator_owner
);
115 static struct elevator_type
*elevator_get(const char *name
)
117 struct elevator_type
*e
;
119 spin_lock_irq(&elv_list_lock
);
121 e
= elevator_find(name
);
122 if (e
&& !try_module_get(e
->elevator_owner
))
125 spin_unlock_irq(&elv_list_lock
);
130 static void *elevator_init_queue(request_queue_t
*q
, struct elevator_queue
*eq
)
132 return eq
->ops
->elevator_init_fn(q
);
135 static void elevator_attach(request_queue_t
*q
, struct elevator_queue
*eq
,
139 eq
->elevator_data
= data
;
142 static char chosen_elevator
[16];
144 static int __init
elevator_setup(char *str
)
147 * Be backwards-compatible with previous kernels, so users
148 * won't get the wrong elevator.
150 if (!strcmp(str
, "as"))
151 strcpy(chosen_elevator
, "anticipatory");
153 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
157 __setup("elevator=", elevator_setup
);
159 static struct kobj_type elv_ktype
;
161 static elevator_t
*elevator_alloc(request_queue_t
*q
, struct elevator_type
*e
)
166 eq
= kmalloc_node(sizeof(elevator_t
), GFP_KERNEL
, q
->node
);
170 memset(eq
, 0, sizeof(*eq
));
172 eq
->elevator_type
= e
;
173 kobject_init(&eq
->kobj
);
174 snprintf(eq
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "iosched");
175 eq
->kobj
.ktype
= &elv_ktype
;
176 mutex_init(&eq
->sysfs_lock
);
178 eq
->hash
= kmalloc_node(sizeof(struct hlist_head
) * ELV_HASH_ENTRIES
,
179 GFP_KERNEL
, q
->node
);
183 for (i
= 0; i
< ELV_HASH_ENTRIES
; i
++)
184 INIT_HLIST_HEAD(&eq
->hash
[i
]);
193 static void elevator_release(struct kobject
*kobj
)
195 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
197 elevator_put(e
->elevator_type
);
202 int elevator_init(request_queue_t
*q
, char *name
)
204 struct elevator_type
*e
= NULL
;
205 struct elevator_queue
*eq
;
209 INIT_LIST_HEAD(&q
->queue_head
);
210 q
->last_merge
= NULL
;
212 q
->boundary_rq
= NULL
;
214 if (name
&& !(e
= elevator_get(name
)))
217 if (!e
&& *chosen_elevator
&& !(e
= elevator_get(chosen_elevator
)))
218 printk("I/O scheduler %s not found\n", chosen_elevator
);
220 if (!e
&& !(e
= elevator_get(CONFIG_DEFAULT_IOSCHED
))) {
221 printk("Default I/O scheduler not found, using no-op\n");
222 e
= elevator_get("noop");
225 eq
= elevator_alloc(q
, e
);
229 data
= elevator_init_queue(q
, eq
);
231 kobject_put(&eq
->kobj
);
235 elevator_attach(q
, eq
, data
);
239 EXPORT_SYMBOL(elevator_init
);
241 void elevator_exit(elevator_t
*e
)
243 mutex_lock(&e
->sysfs_lock
);
244 if (e
->ops
->elevator_exit_fn
)
245 e
->ops
->elevator_exit_fn(e
);
247 mutex_unlock(&e
->sysfs_lock
);
249 kobject_put(&e
->kobj
);
252 EXPORT_SYMBOL(elevator_exit
);
254 static inline void __elv_rqhash_del(struct request
*rq
)
256 hlist_del_init(&rq
->hash
);
259 static void elv_rqhash_del(request_queue_t
*q
, struct request
*rq
)
262 __elv_rqhash_del(rq
);
265 static void elv_rqhash_add(request_queue_t
*q
, struct request
*rq
)
267 elevator_t
*e
= q
->elevator
;
269 BUG_ON(ELV_ON_HASH(rq
));
270 hlist_add_head(&rq
->hash
, &e
->hash
[ELV_HASH_FN(rq_hash_key(rq
))]);
273 static void elv_rqhash_reposition(request_queue_t
*q
, struct request
*rq
)
275 __elv_rqhash_del(rq
);
276 elv_rqhash_add(q
, rq
);
279 static struct request
*elv_rqhash_find(request_queue_t
*q
, sector_t offset
)
281 elevator_t
*e
= q
->elevator
;
282 struct hlist_head
*hash_list
= &e
->hash
[ELV_HASH_FN(offset
)];
283 struct hlist_node
*entry
, *next
;
286 hlist_for_each_entry_safe(rq
, entry
, next
, hash_list
, hash
) {
287 BUG_ON(!ELV_ON_HASH(rq
));
289 if (unlikely(!rq_mergeable(rq
))) {
290 __elv_rqhash_del(rq
);
294 if (rq_hash_key(rq
) == offset
)
302 * RB-tree support functions for inserting/lookup/removal of requests
303 * in a sorted RB tree.
305 struct request
*elv_rb_add(struct rb_root
*root
, struct request
*rq
)
307 struct rb_node
**p
= &root
->rb_node
;
308 struct rb_node
*parent
= NULL
;
309 struct request
*__rq
;
313 __rq
= rb_entry(parent
, struct request
, rb_node
);
315 if (rq
->sector
< __rq
->sector
)
317 else if (rq
->sector
> __rq
->sector
)
323 rb_link_node(&rq
->rb_node
, parent
, p
);
324 rb_insert_color(&rq
->rb_node
, root
);
328 EXPORT_SYMBOL(elv_rb_add
);
330 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
332 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
333 rb_erase(&rq
->rb_node
, root
);
334 RB_CLEAR_NODE(&rq
->rb_node
);
337 EXPORT_SYMBOL(elv_rb_del
);
339 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
341 struct rb_node
*n
= root
->rb_node
;
345 rq
= rb_entry(n
, struct request
, rb_node
);
347 if (sector
< rq
->sector
)
349 else if (sector
> rq
->sector
)
358 EXPORT_SYMBOL(elv_rb_find
);
361 * Insert rq into dispatch queue of q. Queue lock must be held on
362 * entry. rq is sort insted into the dispatch queue. To be used by
363 * specific elevators.
365 void elv_dispatch_sort(request_queue_t
*q
, struct request
*rq
)
368 struct list_head
*entry
;
370 if (q
->last_merge
== rq
)
371 q
->last_merge
= NULL
;
373 elv_rqhash_del(q
, rq
);
377 boundary
= q
->end_sector
;
379 list_for_each_prev(entry
, &q
->queue_head
) {
380 struct request
*pos
= list_entry_rq(entry
);
382 if (pos
->cmd_flags
& (REQ_SOFTBARRIER
|REQ_HARDBARRIER
|REQ_STARTED
))
384 if (rq
->sector
>= boundary
) {
385 if (pos
->sector
< boundary
)
388 if (pos
->sector
>= boundary
)
391 if (rq
->sector
>= pos
->sector
)
395 list_add(&rq
->queuelist
, entry
);
398 EXPORT_SYMBOL(elv_dispatch_sort
);
401 * Insert rq into dispatch queue of q. Queue lock must be held on
402 * entry. rq is added to the back of the dispatch queue. To be used by
403 * specific elevators.
405 void elv_dispatch_add_tail(struct request_queue
*q
, struct request
*rq
)
407 if (q
->last_merge
== rq
)
408 q
->last_merge
= NULL
;
410 elv_rqhash_del(q
, rq
);
414 q
->end_sector
= rq_end_sector(rq
);
416 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
419 EXPORT_SYMBOL(elv_dispatch_add_tail
);
421 int elv_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
423 elevator_t
*e
= q
->elevator
;
424 struct request
*__rq
;
428 * First try one-hit cache.
431 ret
= elv_try_merge(q
->last_merge
, bio
);
432 if (ret
!= ELEVATOR_NO_MERGE
) {
433 *req
= q
->last_merge
;
439 * See if our hash lookup can find a potential backmerge.
441 __rq
= elv_rqhash_find(q
, bio
->bi_sector
);
442 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
444 return ELEVATOR_BACK_MERGE
;
447 if (e
->ops
->elevator_merge_fn
)
448 return e
->ops
->elevator_merge_fn(q
, req
, bio
);
450 return ELEVATOR_NO_MERGE
;
453 void elv_merged_request(request_queue_t
*q
, struct request
*rq
, int type
)
455 elevator_t
*e
= q
->elevator
;
457 if (e
->ops
->elevator_merged_fn
)
458 e
->ops
->elevator_merged_fn(q
, rq
, type
);
460 if (type
== ELEVATOR_BACK_MERGE
)
461 elv_rqhash_reposition(q
, rq
);
466 void elv_merge_requests(request_queue_t
*q
, struct request
*rq
,
467 struct request
*next
)
469 elevator_t
*e
= q
->elevator
;
471 if (e
->ops
->elevator_merge_req_fn
)
472 e
->ops
->elevator_merge_req_fn(q
, rq
, next
);
474 elv_rqhash_reposition(q
, rq
);
475 elv_rqhash_del(q
, next
);
481 void elv_requeue_request(request_queue_t
*q
, struct request
*rq
)
483 elevator_t
*e
= q
->elevator
;
486 * it already went through dequeue, we need to decrement the
487 * in_flight count again
489 if (blk_account_rq(rq
)) {
491 if (blk_sorted_rq(rq
) && e
->ops
->elevator_deactivate_req_fn
)
492 e
->ops
->elevator_deactivate_req_fn(q
, rq
);
495 rq
->cmd_flags
&= ~REQ_STARTED
;
497 elv_insert(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
500 static void elv_drain_elevator(request_queue_t
*q
)
503 while (q
->elevator
->ops
->elevator_dispatch_fn(q
, 1))
505 if (q
->nr_sorted
== 0)
507 if (printed
++ < 10) {
508 printk(KERN_ERR
"%s: forced dispatching is broken "
509 "(nr_sorted=%u), please report this\n",
510 q
->elevator
->elevator_type
->elevator_name
, q
->nr_sorted
);
514 void elv_insert(request_queue_t
*q
, struct request
*rq
, int where
)
516 struct list_head
*pos
;
520 blk_add_trace_rq(q
, rq
, BLK_TA_INSERT
);
525 case ELEVATOR_INSERT_FRONT
:
526 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
528 list_add(&rq
->queuelist
, &q
->queue_head
);
531 case ELEVATOR_INSERT_BACK
:
532 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
533 elv_drain_elevator(q
);
534 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
536 * We kick the queue here for the following reasons.
537 * - The elevator might have returned NULL previously
538 * to delay requests and returned them now. As the
539 * queue wasn't empty before this request, ll_rw_blk
540 * won't run the queue on return, resulting in hang.
541 * - Usually, back inserted requests won't be merged
542 * with anything. There's no point in delaying queue
549 case ELEVATOR_INSERT_SORT
:
550 BUG_ON(!blk_fs_request(rq
));
551 rq
->cmd_flags
|= REQ_SORTED
;
553 if (rq_mergeable(rq
)) {
554 elv_rqhash_add(q
, rq
);
560 * Some ioscheds (cfq) run q->request_fn directly, so
561 * rq cannot be accessed after calling
562 * elevator_add_req_fn.
564 q
->elevator
->ops
->elevator_add_req_fn(q
, rq
);
567 case ELEVATOR_INSERT_REQUEUE
:
569 * If ordered flush isn't in progress, we do front
570 * insertion; otherwise, requests should be requeued
573 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
575 if (q
->ordseq
== 0) {
576 list_add(&rq
->queuelist
, &q
->queue_head
);
580 ordseq
= blk_ordered_req_seq(rq
);
582 list_for_each(pos
, &q
->queue_head
) {
583 struct request
*pos_rq
= list_entry_rq(pos
);
584 if (ordseq
<= blk_ordered_req_seq(pos_rq
))
588 list_add_tail(&rq
->queuelist
, pos
);
590 * most requeues happen because of a busy condition, don't
591 * force unplug of the queue for that case.
597 printk(KERN_ERR
"%s: bad insertion point %d\n",
598 __FUNCTION__
, where
);
602 if (unplug_it
&& blk_queue_plugged(q
)) {
603 int nrq
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]
606 if (nrq
>= q
->unplug_thresh
)
607 __generic_unplug_device(q
);
611 void __elv_add_request(request_queue_t
*q
, struct request
*rq
, int where
,
615 rq
->cmd_flags
|= REQ_ORDERED_COLOR
;
617 if (rq
->cmd_flags
& (REQ_SOFTBARRIER
| REQ_HARDBARRIER
)) {
619 * toggle ordered color
621 if (blk_barrier_rq(rq
))
625 * barriers implicitly indicate back insertion
627 if (where
== ELEVATOR_INSERT_SORT
)
628 where
= ELEVATOR_INSERT_BACK
;
631 * this request is scheduling boundary, update
634 if (blk_fs_request(rq
)) {
635 q
->end_sector
= rq_end_sector(rq
);
638 } else if (!(rq
->cmd_flags
& REQ_ELVPRIV
) && where
== ELEVATOR_INSERT_SORT
)
639 where
= ELEVATOR_INSERT_BACK
;
644 elv_insert(q
, rq
, where
);
647 EXPORT_SYMBOL(__elv_add_request
);
649 void elv_add_request(request_queue_t
*q
, struct request
*rq
, int where
,
654 spin_lock_irqsave(q
->queue_lock
, flags
);
655 __elv_add_request(q
, rq
, where
, plug
);
656 spin_unlock_irqrestore(q
->queue_lock
, flags
);
659 EXPORT_SYMBOL(elv_add_request
);
661 static inline struct request
*__elv_next_request(request_queue_t
*q
)
666 while (!list_empty(&q
->queue_head
)) {
667 rq
= list_entry_rq(q
->queue_head
.next
);
668 if (blk_do_ordered(q
, &rq
))
672 if (!q
->elevator
->ops
->elevator_dispatch_fn(q
, 0))
677 struct request
*elv_next_request(request_queue_t
*q
)
682 while ((rq
= __elv_next_request(q
)) != NULL
) {
683 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
684 elevator_t
*e
= q
->elevator
;
687 * This is the first time the device driver
688 * sees this request (possibly after
689 * requeueing). Notify IO scheduler.
691 if (blk_sorted_rq(rq
) &&
692 e
->ops
->elevator_activate_req_fn
)
693 e
->ops
->elevator_activate_req_fn(q
, rq
);
696 * just mark as started even if we don't start
697 * it, a request that has been delayed should
698 * not be passed by new incoming requests
700 rq
->cmd_flags
|= REQ_STARTED
;
701 blk_add_trace_rq(q
, rq
, BLK_TA_ISSUE
);
704 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
705 q
->end_sector
= rq_end_sector(rq
);
706 q
->boundary_rq
= NULL
;
709 if ((rq
->cmd_flags
& REQ_DONTPREP
) || !q
->prep_rq_fn
)
712 ret
= q
->prep_rq_fn(q
, rq
);
713 if (ret
== BLKPREP_OK
) {
715 } else if (ret
== BLKPREP_DEFER
) {
717 * the request may have been (partially) prepped.
718 * we need to keep this request in the front to
719 * avoid resource deadlock. REQ_STARTED will
720 * prevent other fs requests from passing this one.
724 } else if (ret
== BLKPREP_KILL
) {
725 int nr_bytes
= rq
->hard_nr_sectors
<< 9;
728 nr_bytes
= rq
->data_len
;
730 blkdev_dequeue_request(rq
);
731 rq
->cmd_flags
|= REQ_QUIET
;
732 end_that_request_chunk(rq
, 0, nr_bytes
);
733 end_that_request_last(rq
, 0);
735 printk(KERN_ERR
"%s: bad return=%d\n", __FUNCTION__
,
744 EXPORT_SYMBOL(elv_next_request
);
746 void elv_dequeue_request(request_queue_t
*q
, struct request
*rq
)
748 BUG_ON(list_empty(&rq
->queuelist
));
749 BUG_ON(ELV_ON_HASH(rq
));
751 list_del_init(&rq
->queuelist
);
754 * the time frame between a request being removed from the lists
755 * and to it is freed is accounted as io that is in progress at
758 if (blk_account_rq(rq
))
762 EXPORT_SYMBOL(elv_dequeue_request
);
764 int elv_queue_empty(request_queue_t
*q
)
766 elevator_t
*e
= q
->elevator
;
768 if (!list_empty(&q
->queue_head
))
771 if (e
->ops
->elevator_queue_empty_fn
)
772 return e
->ops
->elevator_queue_empty_fn(q
);
777 EXPORT_SYMBOL(elv_queue_empty
);
779 struct request
*elv_latter_request(request_queue_t
*q
, struct request
*rq
)
781 elevator_t
*e
= q
->elevator
;
783 if (e
->ops
->elevator_latter_req_fn
)
784 return e
->ops
->elevator_latter_req_fn(q
, rq
);
788 struct request
*elv_former_request(request_queue_t
*q
, struct request
*rq
)
790 elevator_t
*e
= q
->elevator
;
792 if (e
->ops
->elevator_former_req_fn
)
793 return e
->ops
->elevator_former_req_fn(q
, rq
);
797 int elv_set_request(request_queue_t
*q
, struct request
*rq
, gfp_t gfp_mask
)
799 elevator_t
*e
= q
->elevator
;
801 if (e
->ops
->elevator_set_req_fn
)
802 return e
->ops
->elevator_set_req_fn(q
, rq
, gfp_mask
);
804 rq
->elevator_private
= NULL
;
808 void elv_put_request(request_queue_t
*q
, struct request
*rq
)
810 elevator_t
*e
= q
->elevator
;
812 if (e
->ops
->elevator_put_req_fn
)
813 e
->ops
->elevator_put_req_fn(rq
);
816 int elv_may_queue(request_queue_t
*q
, int rw
)
818 elevator_t
*e
= q
->elevator
;
820 if (e
->ops
->elevator_may_queue_fn
)
821 return e
->ops
->elevator_may_queue_fn(q
, rw
);
823 return ELV_MQUEUE_MAY
;
826 void elv_completed_request(request_queue_t
*q
, struct request
*rq
)
828 elevator_t
*e
= q
->elevator
;
831 * request is released from the driver, io must be done
833 if (blk_account_rq(rq
)) {
835 if (blk_sorted_rq(rq
) && e
->ops
->elevator_completed_req_fn
)
836 e
->ops
->elevator_completed_req_fn(q
, rq
);
840 * Check if the queue is waiting for fs requests to be
841 * drained for flush sequence.
843 if (unlikely(q
->ordseq
)) {
844 struct request
*first_rq
= list_entry_rq(q
->queue_head
.next
);
845 if (q
->in_flight
== 0 &&
846 blk_ordered_cur_seq(q
) == QUEUE_ORDSEQ_DRAIN
&&
847 blk_ordered_req_seq(first_rq
) > QUEUE_ORDSEQ_DRAIN
) {
848 blk_ordered_complete_seq(q
, QUEUE_ORDSEQ_DRAIN
, 0);
854 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
857 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
859 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
860 struct elv_fs_entry
*entry
= to_elv(attr
);
866 mutex_lock(&e
->sysfs_lock
);
867 error
= e
->ops
? entry
->show(e
, page
) : -ENOENT
;
868 mutex_unlock(&e
->sysfs_lock
);
873 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
874 const char *page
, size_t length
)
876 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
877 struct elv_fs_entry
*entry
= to_elv(attr
);
883 mutex_lock(&e
->sysfs_lock
);
884 error
= e
->ops
? entry
->store(e
, page
, length
) : -ENOENT
;
885 mutex_unlock(&e
->sysfs_lock
);
889 static struct sysfs_ops elv_sysfs_ops
= {
890 .show
= elv_attr_show
,
891 .store
= elv_attr_store
,
894 static struct kobj_type elv_ktype
= {
895 .sysfs_ops
= &elv_sysfs_ops
,
896 .release
= elevator_release
,
899 int elv_register_queue(struct request_queue
*q
)
901 elevator_t
*e
= q
->elevator
;
904 e
->kobj
.parent
= &q
->kobj
;
906 error
= kobject_add(&e
->kobj
);
908 struct elv_fs_entry
*attr
= e
->elevator_type
->elevator_attrs
;
910 while (attr
->attr
.name
) {
911 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
916 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
921 static void __elv_unregister_queue(elevator_t
*e
)
923 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
924 kobject_del(&e
->kobj
);
927 void elv_unregister_queue(struct request_queue
*q
)
930 __elv_unregister_queue(q
->elevator
);
933 int elv_register(struct elevator_type
*e
)
935 spin_lock_irq(&elv_list_lock
);
936 BUG_ON(elevator_find(e
->elevator_name
));
937 list_add_tail(&e
->list
, &elv_list
);
938 spin_unlock_irq(&elv_list_lock
);
940 printk(KERN_INFO
"io scheduler %s registered", e
->elevator_name
);
941 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
942 (!*chosen_elevator
&&
943 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
944 printk(" (default)");
948 EXPORT_SYMBOL_GPL(elv_register
);
950 void elv_unregister(struct elevator_type
*e
)
952 struct task_struct
*g
, *p
;
955 * Iterate every thread in the process to remove the io contexts.
958 read_lock(&tasklist_lock
);
959 do_each_thread(g
, p
) {
962 e
->ops
.trim(p
->io_context
);
964 } while_each_thread(g
, p
);
965 read_unlock(&tasklist_lock
);
968 spin_lock_irq(&elv_list_lock
);
969 list_del_init(&e
->list
);
970 spin_unlock_irq(&elv_list_lock
);
972 EXPORT_SYMBOL_GPL(elv_unregister
);
975 * switch to new_e io scheduler. be careful not to introduce deadlocks -
976 * we don't free the old io scheduler, before we have allocated what we
977 * need for the new one. this way we have a chance of going back to the old
978 * one, if the new one fails init for some reason.
980 static int elevator_switch(request_queue_t
*q
, struct elevator_type
*new_e
)
982 elevator_t
*old_elevator
, *e
;
986 * Allocate new elevator
988 e
= elevator_alloc(q
, new_e
);
992 data
= elevator_init_queue(q
, e
);
994 kobject_put(&e
->kobj
);
999 * Turn on BYPASS and drain all requests w/ elevator private data
1001 spin_lock_irq(q
->queue_lock
);
1003 set_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1005 elv_drain_elevator(q
);
1007 while (q
->rq
.elvpriv
) {
1010 spin_unlock_irq(q
->queue_lock
);
1012 spin_lock_irq(q
->queue_lock
);
1013 elv_drain_elevator(q
);
1017 * Remember old elevator.
1019 old_elevator
= q
->elevator
;
1022 * attach and start new elevator
1024 elevator_attach(q
, e
, data
);
1026 spin_unlock_irq(q
->queue_lock
);
1028 __elv_unregister_queue(old_elevator
);
1030 if (elv_register_queue(q
))
1034 * finally exit old elevator and turn off BYPASS.
1036 elevator_exit(old_elevator
);
1037 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1042 * switch failed, exit the new io scheduler and reattach the old
1043 * one again (along with re-adding the sysfs dir)
1046 q
->elevator
= old_elevator
;
1047 elv_register_queue(q
);
1048 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1052 ssize_t
elv_iosched_store(request_queue_t
*q
, const char *name
, size_t count
)
1054 char elevator_name
[ELV_NAME_MAX
];
1056 struct elevator_type
*e
;
1058 elevator_name
[sizeof(elevator_name
) - 1] = '\0';
1059 strncpy(elevator_name
, name
, sizeof(elevator_name
) - 1);
1060 len
= strlen(elevator_name
);
1062 if (len
&& elevator_name
[len
- 1] == '\n')
1063 elevator_name
[len
- 1] = '\0';
1065 e
= elevator_get(elevator_name
);
1067 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
1071 if (!strcmp(elevator_name
, q
->elevator
->elevator_type
->elevator_name
)) {
1076 if (!elevator_switch(q
, e
))
1077 printk(KERN_ERR
"elevator: switch to %s failed\n",elevator_name
);
1081 ssize_t
elv_iosched_show(request_queue_t
*q
, char *name
)
1083 elevator_t
*e
= q
->elevator
;
1084 struct elevator_type
*elv
= e
->elevator_type
;
1085 struct list_head
*entry
;
1088 spin_lock_irq(&elv_list_lock
);
1089 list_for_each(entry
, &elv_list
) {
1090 struct elevator_type
*__e
;
1092 __e
= list_entry(entry
, struct elevator_type
, list
);
1093 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
1094 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
1096 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1098 spin_unlock_irq(&elv_list_lock
);
1100 len
+= sprintf(len
+name
, "\n");
1104 struct request
*elv_rb_former_request(request_queue_t
*q
, struct request
*rq
)
1106 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
1109 return rb_entry_rq(rbprev
);
1114 EXPORT_SYMBOL(elv_rb_former_request
);
1116 struct request
*elv_rb_latter_request(request_queue_t
*q
, struct request
*rq
)
1118 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
1121 return rb_entry_rq(rbnext
);
1126 EXPORT_SYMBOL(elv_rb_latter_request
);