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 * 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 inline int elv_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
73 if (!rq_mergeable(rq
))
77 * different data direction or already started, don't merge
79 if (bio_data_dir(bio
) != rq_data_dir(rq
))
83 * must be same device and not a special request
85 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| rq
->special
)
88 if (!elv_iosched_allow_merge(rq
, bio
))
93 EXPORT_SYMBOL(elv_rq_merge_ok
);
95 static inline int elv_try_merge(struct request
*__rq
, struct bio
*bio
)
97 int ret
= ELEVATOR_NO_MERGE
;
100 * we can merge and sequence is ok, check if it's possible
102 if (elv_rq_merge_ok(__rq
, bio
)) {
103 if (__rq
->sector
+ __rq
->nr_sectors
== bio
->bi_sector
)
104 ret
= ELEVATOR_BACK_MERGE
;
105 else if (__rq
->sector
- bio_sectors(bio
) == bio
->bi_sector
)
106 ret
= ELEVATOR_FRONT_MERGE
;
112 static struct elevator_type
*elevator_find(const char *name
)
114 struct elevator_type
*e
;
116 list_for_each_entry(e
, &elv_list
, list
) {
117 if (!strcmp(e
->elevator_name
, name
))
124 static void elevator_put(struct elevator_type
*e
)
126 module_put(e
->elevator_owner
);
129 static struct elevator_type
*elevator_get(const char *name
)
131 struct elevator_type
*e
;
133 spin_lock(&elv_list_lock
);
135 e
= elevator_find(name
);
136 if (e
&& !try_module_get(e
->elevator_owner
))
139 spin_unlock(&elv_list_lock
);
144 static void *elevator_init_queue(struct request_queue
*q
,
145 struct elevator_queue
*eq
)
147 return eq
->ops
->elevator_init_fn(q
);
150 static void elevator_attach(struct request_queue
*q
, struct elevator_queue
*eq
,
154 eq
->elevator_data
= data
;
157 static char chosen_elevator
[16];
159 static int __init
elevator_setup(char *str
)
162 * Be backwards-compatible with previous kernels, so users
163 * won't get the wrong elevator.
165 if (!strcmp(str
, "as"))
166 strcpy(chosen_elevator
, "anticipatory");
168 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
172 __setup("elevator=", elevator_setup
);
174 static struct kobj_type elv_ktype
;
176 static elevator_t
*elevator_alloc(struct request_queue
*q
,
177 struct elevator_type
*e
)
182 eq
= kmalloc_node(sizeof(elevator_t
), GFP_KERNEL
| __GFP_ZERO
, q
->node
);
187 eq
->elevator_type
= e
;
188 kobject_init(&eq
->kobj
, &elv_ktype
);
189 mutex_init(&eq
->sysfs_lock
);
191 eq
->hash
= kmalloc_node(sizeof(struct hlist_head
) * ELV_HASH_ENTRIES
,
192 GFP_KERNEL
, q
->node
);
196 for (i
= 0; i
< ELV_HASH_ENTRIES
; i
++)
197 INIT_HLIST_HEAD(&eq
->hash
[i
]);
206 static void elevator_release(struct kobject
*kobj
)
208 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
210 elevator_put(e
->elevator_type
);
215 int elevator_init(struct request_queue
*q
, char *name
)
217 struct elevator_type
*e
= NULL
;
218 struct elevator_queue
*eq
;
222 INIT_LIST_HEAD(&q
->queue_head
);
223 q
->last_merge
= NULL
;
225 q
->boundary_rq
= NULL
;
227 if (name
&& !(e
= elevator_get(name
)))
230 if (!e
&& *chosen_elevator
&& !(e
= elevator_get(chosen_elevator
)))
231 printk("I/O scheduler %s not found\n", chosen_elevator
);
233 if (!e
&& !(e
= elevator_get(CONFIG_DEFAULT_IOSCHED
))) {
234 printk("Default I/O scheduler not found, using no-op\n");
235 e
= elevator_get("noop");
238 eq
= elevator_alloc(q
, e
);
242 data
= elevator_init_queue(q
, eq
);
244 kobject_put(&eq
->kobj
);
248 elevator_attach(q
, eq
, data
);
252 EXPORT_SYMBOL(elevator_init
);
254 void elevator_exit(elevator_t
*e
)
256 mutex_lock(&e
->sysfs_lock
);
257 if (e
->ops
->elevator_exit_fn
)
258 e
->ops
->elevator_exit_fn(e
);
260 mutex_unlock(&e
->sysfs_lock
);
262 kobject_put(&e
->kobj
);
265 EXPORT_SYMBOL(elevator_exit
);
267 static void elv_activate_rq(struct request_queue
*q
, struct request
*rq
)
269 elevator_t
*e
= q
->elevator
;
271 if (e
->ops
->elevator_activate_req_fn
)
272 e
->ops
->elevator_activate_req_fn(q
, rq
);
275 static void elv_deactivate_rq(struct request_queue
*q
, struct request
*rq
)
277 elevator_t
*e
= q
->elevator
;
279 if (e
->ops
->elevator_deactivate_req_fn
)
280 e
->ops
->elevator_deactivate_req_fn(q
, rq
);
283 static inline void __elv_rqhash_del(struct request
*rq
)
285 hlist_del_init(&rq
->hash
);
288 static void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
291 __elv_rqhash_del(rq
);
294 static void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
296 elevator_t
*e
= q
->elevator
;
298 BUG_ON(ELV_ON_HASH(rq
));
299 hlist_add_head(&rq
->hash
, &e
->hash
[ELV_HASH_FN(rq_hash_key(rq
))]);
302 static void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
304 __elv_rqhash_del(rq
);
305 elv_rqhash_add(q
, rq
);
308 static struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
310 elevator_t
*e
= q
->elevator
;
311 struct hlist_head
*hash_list
= &e
->hash
[ELV_HASH_FN(offset
)];
312 struct hlist_node
*entry
, *next
;
315 hlist_for_each_entry_safe(rq
, entry
, next
, hash_list
, hash
) {
316 BUG_ON(!ELV_ON_HASH(rq
));
318 if (unlikely(!rq_mergeable(rq
))) {
319 __elv_rqhash_del(rq
);
323 if (rq_hash_key(rq
) == offset
)
331 * RB-tree support functions for inserting/lookup/removal of requests
332 * in a sorted RB tree.
334 struct request
*elv_rb_add(struct rb_root
*root
, struct request
*rq
)
336 struct rb_node
**p
= &root
->rb_node
;
337 struct rb_node
*parent
= NULL
;
338 struct request
*__rq
;
342 __rq
= rb_entry(parent
, struct request
, rb_node
);
344 if (rq
->sector
< __rq
->sector
)
346 else if (rq
->sector
> __rq
->sector
)
352 rb_link_node(&rq
->rb_node
, parent
, p
);
353 rb_insert_color(&rq
->rb_node
, root
);
357 EXPORT_SYMBOL(elv_rb_add
);
359 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
361 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
362 rb_erase(&rq
->rb_node
, root
);
363 RB_CLEAR_NODE(&rq
->rb_node
);
366 EXPORT_SYMBOL(elv_rb_del
);
368 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
370 struct rb_node
*n
= root
->rb_node
;
374 rq
= rb_entry(n
, struct request
, rb_node
);
376 if (sector
< rq
->sector
)
378 else if (sector
> rq
->sector
)
387 EXPORT_SYMBOL(elv_rb_find
);
390 * Insert rq into dispatch queue of q. Queue lock must be held on
391 * entry. rq is sort instead into the dispatch queue. To be used by
392 * specific elevators.
394 void elv_dispatch_sort(struct request_queue
*q
, struct request
*rq
)
397 struct list_head
*entry
;
399 if (q
->last_merge
== rq
)
400 q
->last_merge
= NULL
;
402 elv_rqhash_del(q
, rq
);
406 boundary
= q
->end_sector
;
408 list_for_each_prev(entry
, &q
->queue_head
) {
409 struct request
*pos
= list_entry_rq(entry
);
411 if (rq_data_dir(rq
) != rq_data_dir(pos
))
413 if (pos
->cmd_flags
& (REQ_SOFTBARRIER
|REQ_HARDBARRIER
|REQ_STARTED
))
415 if (rq
->sector
>= boundary
) {
416 if (pos
->sector
< boundary
)
419 if (pos
->sector
>= boundary
)
422 if (rq
->sector
>= pos
->sector
)
426 list_add(&rq
->queuelist
, entry
);
429 EXPORT_SYMBOL(elv_dispatch_sort
);
432 * Insert rq into dispatch queue of q. Queue lock must be held on
433 * entry. rq is added to the back of the dispatch queue. To be used by
434 * specific elevators.
436 void elv_dispatch_add_tail(struct request_queue
*q
, struct request
*rq
)
438 if (q
->last_merge
== rq
)
439 q
->last_merge
= NULL
;
441 elv_rqhash_del(q
, rq
);
445 q
->end_sector
= rq_end_sector(rq
);
447 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
450 EXPORT_SYMBOL(elv_dispatch_add_tail
);
452 int elv_merge(struct request_queue
*q
, struct request
**req
, struct bio
*bio
)
454 elevator_t
*e
= q
->elevator
;
455 struct request
*__rq
;
459 * First try one-hit cache.
462 ret
= elv_try_merge(q
->last_merge
, bio
);
463 if (ret
!= ELEVATOR_NO_MERGE
) {
464 *req
= q
->last_merge
;
470 * See if our hash lookup can find a potential backmerge.
472 __rq
= elv_rqhash_find(q
, bio
->bi_sector
);
473 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
475 return ELEVATOR_BACK_MERGE
;
478 if (e
->ops
->elevator_merge_fn
)
479 return e
->ops
->elevator_merge_fn(q
, req
, bio
);
481 return ELEVATOR_NO_MERGE
;
484 void elv_merged_request(struct request_queue
*q
, struct request
*rq
, int type
)
486 elevator_t
*e
= q
->elevator
;
488 if (e
->ops
->elevator_merged_fn
)
489 e
->ops
->elevator_merged_fn(q
, rq
, type
);
491 if (type
== ELEVATOR_BACK_MERGE
)
492 elv_rqhash_reposition(q
, rq
);
497 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
498 struct request
*next
)
500 elevator_t
*e
= q
->elevator
;
502 if (e
->ops
->elevator_merge_req_fn
)
503 e
->ops
->elevator_merge_req_fn(q
, rq
, next
);
505 elv_rqhash_reposition(q
, rq
);
506 elv_rqhash_del(q
, next
);
512 void elv_requeue_request(struct request_queue
*q
, struct request
*rq
)
515 * it already went through dequeue, we need to decrement the
516 * in_flight count again
518 if (blk_account_rq(rq
)) {
520 if (blk_sorted_rq(rq
))
521 elv_deactivate_rq(q
, rq
);
524 rq
->cmd_flags
&= ~REQ_STARTED
;
526 elv_insert(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
529 static void elv_drain_elevator(struct request_queue
*q
)
532 while (q
->elevator
->ops
->elevator_dispatch_fn(q
, 1))
534 if (q
->nr_sorted
== 0)
536 if (printed
++ < 10) {
537 printk(KERN_ERR
"%s: forced dispatching is broken "
538 "(nr_sorted=%u), please report this\n",
539 q
->elevator
->elevator_type
->elevator_name
, q
->nr_sorted
);
543 void elv_insert(struct request_queue
*q
, struct request
*rq
, int where
)
545 struct list_head
*pos
;
549 blk_add_trace_rq(q
, rq
, BLK_TA_INSERT
);
554 case ELEVATOR_INSERT_FRONT
:
555 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
557 list_add(&rq
->queuelist
, &q
->queue_head
);
560 case ELEVATOR_INSERT_BACK
:
561 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
562 elv_drain_elevator(q
);
563 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
565 * We kick the queue here for the following reasons.
566 * - The elevator might have returned NULL previously
567 * to delay requests and returned them now. As the
568 * queue wasn't empty before this request, ll_rw_blk
569 * won't run the queue on return, resulting in hang.
570 * - Usually, back inserted requests won't be merged
571 * with anything. There's no point in delaying queue
578 case ELEVATOR_INSERT_SORT
:
579 BUG_ON(!blk_fs_request(rq
));
580 rq
->cmd_flags
|= REQ_SORTED
;
582 if (rq_mergeable(rq
)) {
583 elv_rqhash_add(q
, rq
);
589 * Some ioscheds (cfq) run q->request_fn directly, so
590 * rq cannot be accessed after calling
591 * elevator_add_req_fn.
593 q
->elevator
->ops
->elevator_add_req_fn(q
, rq
);
596 case ELEVATOR_INSERT_REQUEUE
:
598 * If ordered flush isn't in progress, we do front
599 * insertion; otherwise, requests should be requeued
602 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
605 * Most requeues happen because of a busy condition,
606 * don't force unplug of the queue for that case.
610 if (q
->ordseq
== 0) {
611 list_add(&rq
->queuelist
, &q
->queue_head
);
615 ordseq
= blk_ordered_req_seq(rq
);
617 list_for_each(pos
, &q
->queue_head
) {
618 struct request
*pos_rq
= list_entry_rq(pos
);
619 if (ordseq
<= blk_ordered_req_seq(pos_rq
))
623 list_add_tail(&rq
->queuelist
, pos
);
627 printk(KERN_ERR
"%s: bad insertion point %d\n",
628 __FUNCTION__
, where
);
632 if (unplug_it
&& blk_queue_plugged(q
)) {
633 int nrq
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]
636 if (nrq
>= q
->unplug_thresh
)
637 __generic_unplug_device(q
);
641 void __elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
,
645 rq
->cmd_flags
|= REQ_ORDERED_COLOR
;
647 if (rq
->cmd_flags
& (REQ_SOFTBARRIER
| REQ_HARDBARRIER
)) {
649 * toggle ordered color
651 if (blk_barrier_rq(rq
))
655 * barriers implicitly indicate back insertion
657 if (where
== ELEVATOR_INSERT_SORT
)
658 where
= ELEVATOR_INSERT_BACK
;
661 * this request is scheduling boundary, update
664 if (blk_fs_request(rq
)) {
665 q
->end_sector
= rq_end_sector(rq
);
668 } else if (!(rq
->cmd_flags
& REQ_ELVPRIV
) && where
== ELEVATOR_INSERT_SORT
)
669 where
= ELEVATOR_INSERT_BACK
;
674 elv_insert(q
, rq
, where
);
677 EXPORT_SYMBOL(__elv_add_request
);
679 void elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
,
684 spin_lock_irqsave(q
->queue_lock
, flags
);
685 __elv_add_request(q
, rq
, where
, plug
);
686 spin_unlock_irqrestore(q
->queue_lock
, flags
);
689 EXPORT_SYMBOL(elv_add_request
);
691 static inline struct request
*__elv_next_request(struct request_queue
*q
)
696 while (!list_empty(&q
->queue_head
)) {
697 rq
= list_entry_rq(q
->queue_head
.next
);
698 if (blk_do_ordered(q
, &rq
))
702 if (!q
->elevator
->ops
->elevator_dispatch_fn(q
, 0))
707 struct request
*elv_next_request(struct request_queue
*q
)
712 while ((rq
= __elv_next_request(q
)) != NULL
) {
714 * Kill the empty barrier place holder, the driver must
717 if (blk_empty_barrier(rq
)) {
718 end_queued_request(rq
, 1);
721 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
723 * This is the first time the device driver
724 * sees this request (possibly after
725 * requeueing). Notify IO scheduler.
727 if (blk_sorted_rq(rq
))
728 elv_activate_rq(q
, rq
);
731 * just mark as started even if we don't start
732 * it, a request that has been delayed should
733 * not be passed by new incoming requests
735 rq
->cmd_flags
|= REQ_STARTED
;
736 blk_add_trace_rq(q
, rq
, BLK_TA_ISSUE
);
739 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
740 q
->end_sector
= rq_end_sector(rq
);
741 q
->boundary_rq
= NULL
;
744 if (rq
->cmd_flags
& REQ_DONTPREP
)
747 if (q
->dma_drain_size
&& rq
->data_len
) {
749 * make sure space for the drain appears we
750 * know we can do this because max_hw_segments
751 * has been adjusted to be one fewer than the
754 rq
->nr_phys_segments
++;
755 rq
->nr_hw_segments
++;
761 ret
= q
->prep_rq_fn(q
, rq
);
762 if (ret
== BLKPREP_OK
) {
764 } else if (ret
== BLKPREP_DEFER
) {
766 * the request may have been (partially) prepped.
767 * we need to keep this request in the front to
768 * avoid resource deadlock. REQ_STARTED will
769 * prevent other fs requests from passing this one.
771 if (q
->dma_drain_size
&& rq
->data_len
&&
772 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
774 * remove the space for the drain we added
775 * so that we don't add it again
777 --rq
->nr_phys_segments
;
778 --rq
->nr_hw_segments
;
783 } else if (ret
== BLKPREP_KILL
) {
784 rq
->cmd_flags
|= REQ_QUIET
;
785 end_queued_request(rq
, 0);
787 printk(KERN_ERR
"%s: bad return=%d\n", __FUNCTION__
,
796 EXPORT_SYMBOL(elv_next_request
);
798 void elv_dequeue_request(struct request_queue
*q
, struct request
*rq
)
800 BUG_ON(list_empty(&rq
->queuelist
));
801 BUG_ON(ELV_ON_HASH(rq
));
803 list_del_init(&rq
->queuelist
);
806 * the time frame between a request being removed from the lists
807 * and to it is freed is accounted as io that is in progress at
810 if (blk_account_rq(rq
))
814 EXPORT_SYMBOL(elv_dequeue_request
);
816 int elv_queue_empty(struct request_queue
*q
)
818 elevator_t
*e
= q
->elevator
;
820 if (!list_empty(&q
->queue_head
))
823 if (e
->ops
->elevator_queue_empty_fn
)
824 return e
->ops
->elevator_queue_empty_fn(q
);
829 EXPORT_SYMBOL(elv_queue_empty
);
831 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
833 elevator_t
*e
= q
->elevator
;
835 if (e
->ops
->elevator_latter_req_fn
)
836 return e
->ops
->elevator_latter_req_fn(q
, rq
);
840 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
842 elevator_t
*e
= q
->elevator
;
844 if (e
->ops
->elevator_former_req_fn
)
845 return e
->ops
->elevator_former_req_fn(q
, rq
);
849 int elv_set_request(struct request_queue
*q
, struct request
*rq
, gfp_t gfp_mask
)
851 elevator_t
*e
= q
->elevator
;
853 if (e
->ops
->elevator_set_req_fn
)
854 return e
->ops
->elevator_set_req_fn(q
, rq
, gfp_mask
);
856 rq
->elevator_private
= NULL
;
860 void elv_put_request(struct request_queue
*q
, struct request
*rq
)
862 elevator_t
*e
= q
->elevator
;
864 if (e
->ops
->elevator_put_req_fn
)
865 e
->ops
->elevator_put_req_fn(rq
);
868 int elv_may_queue(struct request_queue
*q
, int rw
)
870 elevator_t
*e
= q
->elevator
;
872 if (e
->ops
->elevator_may_queue_fn
)
873 return e
->ops
->elevator_may_queue_fn(q
, rw
);
875 return ELV_MQUEUE_MAY
;
878 void elv_completed_request(struct request_queue
*q
, struct request
*rq
)
880 elevator_t
*e
= q
->elevator
;
883 * request is released from the driver, io must be done
885 if (blk_account_rq(rq
)) {
887 if (blk_sorted_rq(rq
) && e
->ops
->elevator_completed_req_fn
)
888 e
->ops
->elevator_completed_req_fn(q
, rq
);
892 * Check if the queue is waiting for fs requests to be
893 * drained for flush sequence.
895 if (unlikely(q
->ordseq
)) {
896 struct request
*first_rq
= list_entry_rq(q
->queue_head
.next
);
897 if (q
->in_flight
== 0 &&
898 blk_ordered_cur_seq(q
) == QUEUE_ORDSEQ_DRAIN
&&
899 blk_ordered_req_seq(first_rq
) > QUEUE_ORDSEQ_DRAIN
) {
900 blk_ordered_complete_seq(q
, QUEUE_ORDSEQ_DRAIN
, 0);
906 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
909 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
911 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
912 struct elv_fs_entry
*entry
= to_elv(attr
);
918 mutex_lock(&e
->sysfs_lock
);
919 error
= e
->ops
? entry
->show(e
, page
) : -ENOENT
;
920 mutex_unlock(&e
->sysfs_lock
);
925 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
926 const char *page
, size_t length
)
928 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
929 struct elv_fs_entry
*entry
= to_elv(attr
);
935 mutex_lock(&e
->sysfs_lock
);
936 error
= e
->ops
? entry
->store(e
, page
, length
) : -ENOENT
;
937 mutex_unlock(&e
->sysfs_lock
);
941 static struct sysfs_ops elv_sysfs_ops
= {
942 .show
= elv_attr_show
,
943 .store
= elv_attr_store
,
946 static struct kobj_type elv_ktype
= {
947 .sysfs_ops
= &elv_sysfs_ops
,
948 .release
= elevator_release
,
951 int elv_register_queue(struct request_queue
*q
)
953 elevator_t
*e
= q
->elevator
;
956 error
= kobject_add(&e
->kobj
, &q
->kobj
, "%s", "iosched");
958 struct elv_fs_entry
*attr
= e
->elevator_type
->elevator_attrs
;
960 while (attr
->attr
.name
) {
961 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
966 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
971 static void __elv_unregister_queue(elevator_t
*e
)
973 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
974 kobject_del(&e
->kobj
);
977 void elv_unregister_queue(struct request_queue
*q
)
980 __elv_unregister_queue(q
->elevator
);
983 void elv_register(struct elevator_type
*e
)
987 spin_lock(&elv_list_lock
);
988 BUG_ON(elevator_find(e
->elevator_name
));
989 list_add_tail(&e
->list
, &elv_list
);
990 spin_unlock(&elv_list_lock
);
992 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
993 (!*chosen_elevator
&&
994 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
997 printk(KERN_INFO
"io scheduler %s registered%s\n", e
->elevator_name
, def
);
999 EXPORT_SYMBOL_GPL(elv_register
);
1001 void elv_unregister(struct elevator_type
*e
)
1003 struct task_struct
*g
, *p
;
1006 * Iterate every thread in the process to remove the io contexts.
1009 read_lock(&tasklist_lock
);
1010 do_each_thread(g
, p
) {
1013 e
->ops
.trim(p
->io_context
);
1015 } while_each_thread(g
, p
);
1016 read_unlock(&tasklist_lock
);
1019 spin_lock(&elv_list_lock
);
1020 list_del_init(&e
->list
);
1021 spin_unlock(&elv_list_lock
);
1023 EXPORT_SYMBOL_GPL(elv_unregister
);
1026 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1027 * we don't free the old io scheduler, before we have allocated what we
1028 * need for the new one. this way we have a chance of going back to the old
1029 * one, if the new one fails init for some reason.
1031 static int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
1033 elevator_t
*old_elevator
, *e
;
1037 * Allocate new elevator
1039 e
= elevator_alloc(q
, new_e
);
1043 data
= elevator_init_queue(q
, e
);
1045 kobject_put(&e
->kobj
);
1050 * Turn on BYPASS and drain all requests w/ elevator private data
1052 spin_lock_irq(q
->queue_lock
);
1054 set_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1056 elv_drain_elevator(q
);
1058 while (q
->rq
.elvpriv
) {
1061 spin_unlock_irq(q
->queue_lock
);
1063 spin_lock_irq(q
->queue_lock
);
1064 elv_drain_elevator(q
);
1068 * Remember old elevator.
1070 old_elevator
= q
->elevator
;
1073 * attach and start new elevator
1075 elevator_attach(q
, e
, data
);
1077 spin_unlock_irq(q
->queue_lock
);
1079 __elv_unregister_queue(old_elevator
);
1081 if (elv_register_queue(q
))
1085 * finally exit old elevator and turn off BYPASS.
1087 elevator_exit(old_elevator
);
1088 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1093 * switch failed, exit the new io scheduler and reattach the old
1094 * one again (along with re-adding the sysfs dir)
1097 q
->elevator
= old_elevator
;
1098 elv_register_queue(q
);
1099 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1103 ssize_t
elv_iosched_store(struct request_queue
*q
, const char *name
,
1106 char elevator_name
[ELV_NAME_MAX
];
1108 struct elevator_type
*e
;
1110 elevator_name
[sizeof(elevator_name
) - 1] = '\0';
1111 strncpy(elevator_name
, name
, sizeof(elevator_name
) - 1);
1112 len
= strlen(elevator_name
);
1114 if (len
&& elevator_name
[len
- 1] == '\n')
1115 elevator_name
[len
- 1] = '\0';
1117 e
= elevator_get(elevator_name
);
1119 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
1123 if (!strcmp(elevator_name
, q
->elevator
->elevator_type
->elevator_name
)) {
1128 if (!elevator_switch(q
, e
))
1129 printk(KERN_ERR
"elevator: switch to %s failed\n",elevator_name
);
1133 ssize_t
elv_iosched_show(struct request_queue
*q
, char *name
)
1135 elevator_t
*e
= q
->elevator
;
1136 struct elevator_type
*elv
= e
->elevator_type
;
1137 struct elevator_type
*__e
;
1140 spin_lock(&elv_list_lock
);
1141 list_for_each_entry(__e
, &elv_list
, list
) {
1142 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
1143 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
1145 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1147 spin_unlock(&elv_list_lock
);
1149 len
+= sprintf(len
+name
, "\n");
1153 struct request
*elv_rb_former_request(struct request_queue
*q
,
1156 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
1159 return rb_entry_rq(rbprev
);
1164 EXPORT_SYMBOL(elv_rb_former_request
);
1166 struct request
*elv_rb_latter_request(struct request_queue
*q
,
1169 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
1172 return rb_entry_rq(rbnext
);
1177 EXPORT_SYMBOL(elv_rb_latter_request
);