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) \
49 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
50 #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
51 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
52 #define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
55 * Query io scheduler to see if the current process issuing bio may be
58 static int elv_iosched_allow_merge(struct request
*rq
, struct bio
*bio
)
60 struct request_queue
*q
= rq
->q
;
61 elevator_t
*e
= q
->elevator
;
63 if (e
->ops
->elevator_allow_merge_fn
)
64 return e
->ops
->elevator_allow_merge_fn(q
, rq
, bio
);
70 * can we safely merge with this request?
72 inline int elv_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
74 if (!rq_mergeable(rq
))
78 * different data direction or already started, don't merge
80 if (bio_data_dir(bio
) != rq_data_dir(rq
))
84 * must be same device and not a special request
86 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| rq
->special
)
89 if (!elv_iosched_allow_merge(rq
, bio
))
94 EXPORT_SYMBOL(elv_rq_merge_ok
);
96 static inline int elv_try_merge(struct request
*__rq
, struct bio
*bio
)
98 int ret
= ELEVATOR_NO_MERGE
;
101 * we can merge and sequence is ok, check if it's possible
103 if (elv_rq_merge_ok(__rq
, bio
)) {
104 if (__rq
->sector
+ __rq
->nr_sectors
== bio
->bi_sector
)
105 ret
= ELEVATOR_BACK_MERGE
;
106 else if (__rq
->sector
- bio_sectors(bio
) == bio
->bi_sector
)
107 ret
= ELEVATOR_FRONT_MERGE
;
113 static struct elevator_type
*elevator_find(const char *name
)
115 struct elevator_type
*e
;
117 list_for_each_entry(e
, &elv_list
, list
) {
118 if (!strcmp(e
->elevator_name
, name
))
125 static void elevator_put(struct elevator_type
*e
)
127 module_put(e
->elevator_owner
);
130 static struct elevator_type
*elevator_get(const char *name
)
132 struct elevator_type
*e
;
134 spin_lock(&elv_list_lock
);
136 e
= elevator_find(name
);
137 <<<<<<< HEAD
:block
/elevator
.c
140 char elv
[ELV_NAME_MAX
+ strlen("-iosched")];
142 spin_unlock(&elv_list_lock
);
144 if (!strcmp(name
, "anticipatory"))
145 sprintf(elv
, "as-iosched");
147 sprintf(elv
, "%s-iosched", name
);
150 spin_lock(&elv_list_lock
);
151 e
= elevator_find(name
);
154 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:block
/elevator
.c
155 if (e
&& !try_module_get(e
->elevator_owner
))
158 spin_unlock(&elv_list_lock
);
163 static void *elevator_init_queue(struct request_queue
*q
,
164 struct elevator_queue
*eq
)
166 return eq
->ops
->elevator_init_fn(q
);
169 static void elevator_attach(struct request_queue
*q
, struct elevator_queue
*eq
,
173 eq
->elevator_data
= data
;
176 static char chosen_elevator
[16];
178 static int __init
elevator_setup(char *str
)
181 * Be backwards-compatible with previous kernels, so users
182 * won't get the wrong elevator.
184 if (!strcmp(str
, "as"))
185 strcpy(chosen_elevator
, "anticipatory");
187 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
191 __setup("elevator=", elevator_setup
);
193 static struct kobj_type elv_ktype
;
195 static elevator_t
*elevator_alloc(struct request_queue
*q
,
196 struct elevator_type
*e
)
201 eq
= kmalloc_node(sizeof(elevator_t
), GFP_KERNEL
| __GFP_ZERO
, q
->node
);
206 eq
->elevator_type
= e
;
207 kobject_init(&eq
->kobj
, &elv_ktype
);
208 mutex_init(&eq
->sysfs_lock
);
210 eq
->hash
= kmalloc_node(sizeof(struct hlist_head
) * ELV_HASH_ENTRIES
,
211 GFP_KERNEL
, q
->node
);
215 for (i
= 0; i
< ELV_HASH_ENTRIES
; i
++)
216 INIT_HLIST_HEAD(&eq
->hash
[i
]);
225 static void elevator_release(struct kobject
*kobj
)
227 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
229 elevator_put(e
->elevator_type
);
234 int elevator_init(struct request_queue
*q
, char *name
)
236 struct elevator_type
*e
= NULL
;
237 struct elevator_queue
*eq
;
241 INIT_LIST_HEAD(&q
->queue_head
);
242 q
->last_merge
= NULL
;
244 q
->boundary_rq
= NULL
;
247 e
= elevator_get(name
);
252 if (!e
&& *chosen_elevator
) {
253 e
= elevator_get(chosen_elevator
);
255 printk(KERN_ERR
"I/O scheduler %s not found\n",
260 e
= elevator_get(CONFIG_DEFAULT_IOSCHED
);
263 "Default I/O scheduler not found. " \
265 e
= elevator_get("noop");
269 eq
= elevator_alloc(q
, e
);
273 data
= elevator_init_queue(q
, eq
);
275 kobject_put(&eq
->kobj
);
279 elevator_attach(q
, eq
, data
);
282 EXPORT_SYMBOL(elevator_init
);
284 void elevator_exit(elevator_t
*e
)
286 mutex_lock(&e
->sysfs_lock
);
287 if (e
->ops
->elevator_exit_fn
)
288 e
->ops
->elevator_exit_fn(e
);
290 mutex_unlock(&e
->sysfs_lock
);
292 kobject_put(&e
->kobj
);
294 EXPORT_SYMBOL(elevator_exit
);
296 static void elv_activate_rq(struct request_queue
*q
, struct request
*rq
)
298 elevator_t
*e
= q
->elevator
;
300 if (e
->ops
->elevator_activate_req_fn
)
301 e
->ops
->elevator_activate_req_fn(q
, rq
);
304 static void elv_deactivate_rq(struct request_queue
*q
, struct request
*rq
)
306 elevator_t
*e
= q
->elevator
;
308 if (e
->ops
->elevator_deactivate_req_fn
)
309 e
->ops
->elevator_deactivate_req_fn(q
, rq
);
312 static inline void __elv_rqhash_del(struct request
*rq
)
314 hlist_del_init(&rq
->hash
);
317 static void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
320 __elv_rqhash_del(rq
);
323 static void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
325 elevator_t
*e
= q
->elevator
;
327 BUG_ON(ELV_ON_HASH(rq
));
328 hlist_add_head(&rq
->hash
, &e
->hash
[ELV_HASH_FN(rq_hash_key(rq
))]);
331 static void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
333 __elv_rqhash_del(rq
);
334 elv_rqhash_add(q
, rq
);
337 static struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
339 elevator_t
*e
= q
->elevator
;
340 struct hlist_head
*hash_list
= &e
->hash
[ELV_HASH_FN(offset
)];
341 struct hlist_node
*entry
, *next
;
344 hlist_for_each_entry_safe(rq
, entry
, next
, hash_list
, hash
) {
345 BUG_ON(!ELV_ON_HASH(rq
));
347 if (unlikely(!rq_mergeable(rq
))) {
348 __elv_rqhash_del(rq
);
352 if (rq_hash_key(rq
) == offset
)
360 * RB-tree support functions for inserting/lookup/removal of requests
361 * in a sorted RB tree.
363 struct request
*elv_rb_add(struct rb_root
*root
, struct request
*rq
)
365 struct rb_node
**p
= &root
->rb_node
;
366 struct rb_node
*parent
= NULL
;
367 struct request
*__rq
;
371 __rq
= rb_entry(parent
, struct request
, rb_node
);
373 if (rq
->sector
< __rq
->sector
)
375 else if (rq
->sector
> __rq
->sector
)
381 rb_link_node(&rq
->rb_node
, parent
, p
);
382 rb_insert_color(&rq
->rb_node
, root
);
385 EXPORT_SYMBOL(elv_rb_add
);
387 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
389 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
390 rb_erase(&rq
->rb_node
, root
);
391 RB_CLEAR_NODE(&rq
->rb_node
);
393 EXPORT_SYMBOL(elv_rb_del
);
395 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
397 struct rb_node
*n
= root
->rb_node
;
401 rq
= rb_entry(n
, struct request
, rb_node
);
403 if (sector
< rq
->sector
)
405 else if (sector
> rq
->sector
)
413 EXPORT_SYMBOL(elv_rb_find
);
416 * Insert rq into dispatch queue of q. Queue lock must be held on
417 * entry. rq is sort instead into the dispatch queue. To be used by
418 * specific elevators.
420 void elv_dispatch_sort(struct request_queue
*q
, struct request
*rq
)
423 struct list_head
*entry
;
426 if (q
->last_merge
== rq
)
427 q
->last_merge
= NULL
;
429 elv_rqhash_del(q
, rq
);
433 boundary
= q
->end_sector
;
434 stop_flags
= REQ_SOFTBARRIER
| REQ_HARDBARRIER
| REQ_STARTED
;
435 list_for_each_prev(entry
, &q
->queue_head
) {
436 struct request
*pos
= list_entry_rq(entry
);
438 if (rq_data_dir(rq
) != rq_data_dir(pos
))
440 if (pos
->cmd_flags
& stop_flags
)
442 if (rq
->sector
>= boundary
) {
443 if (pos
->sector
< boundary
)
446 if (pos
->sector
>= boundary
)
449 if (rq
->sector
>= pos
->sector
)
453 list_add(&rq
->queuelist
, entry
);
455 EXPORT_SYMBOL(elv_dispatch_sort
);
458 * Insert rq into dispatch queue of q. Queue lock must be held on
459 * entry. rq is added to the back of the dispatch queue. To be used by
460 * specific elevators.
462 void elv_dispatch_add_tail(struct request_queue
*q
, struct request
*rq
)
464 if (q
->last_merge
== rq
)
465 q
->last_merge
= NULL
;
467 elv_rqhash_del(q
, rq
);
471 q
->end_sector
= rq_end_sector(rq
);
473 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
475 EXPORT_SYMBOL(elv_dispatch_add_tail
);
477 int elv_merge(struct request_queue
*q
, struct request
**req
, struct bio
*bio
)
479 elevator_t
*e
= q
->elevator
;
480 struct request
*__rq
;
484 * First try one-hit cache.
487 ret
= elv_try_merge(q
->last_merge
, bio
);
488 if (ret
!= ELEVATOR_NO_MERGE
) {
489 *req
= q
->last_merge
;
495 * See if our hash lookup can find a potential backmerge.
497 __rq
= elv_rqhash_find(q
, bio
->bi_sector
);
498 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
500 return ELEVATOR_BACK_MERGE
;
503 if (e
->ops
->elevator_merge_fn
)
504 return e
->ops
->elevator_merge_fn(q
, req
, bio
);
506 return ELEVATOR_NO_MERGE
;
509 void elv_merged_request(struct request_queue
*q
, struct request
*rq
, int type
)
511 elevator_t
*e
= q
->elevator
;
513 if (e
->ops
->elevator_merged_fn
)
514 e
->ops
->elevator_merged_fn(q
, rq
, type
);
516 if (type
== ELEVATOR_BACK_MERGE
)
517 elv_rqhash_reposition(q
, rq
);
522 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
523 struct request
*next
)
525 elevator_t
*e
= q
->elevator
;
527 if (e
->ops
->elevator_merge_req_fn
)
528 e
->ops
->elevator_merge_req_fn(q
, rq
, next
);
530 elv_rqhash_reposition(q
, rq
);
531 elv_rqhash_del(q
, next
);
537 void elv_requeue_request(struct request_queue
*q
, struct request
*rq
)
540 * it already went through dequeue, we need to decrement the
541 * in_flight count again
543 if (blk_account_rq(rq
)) {
545 if (blk_sorted_rq(rq
))
546 elv_deactivate_rq(q
, rq
);
549 rq
->cmd_flags
&= ~REQ_STARTED
;
551 elv_insert(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
554 static void elv_drain_elevator(struct request_queue
*q
)
557 while (q
->elevator
->ops
->elevator_dispatch_fn(q
, 1))
559 if (q
->nr_sorted
== 0)
561 if (printed
++ < 10) {
562 printk(KERN_ERR
"%s: forced dispatching is broken "
563 "(nr_sorted=%u), please report this\n",
564 q
->elevator
->elevator_type
->elevator_name
, q
->nr_sorted
);
568 void elv_insert(struct request_queue
*q
, struct request
*rq
, int where
)
570 struct list_head
*pos
;
574 blk_add_trace_rq(q
, rq
, BLK_TA_INSERT
);
579 case ELEVATOR_INSERT_FRONT
:
580 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
582 list_add(&rq
->queuelist
, &q
->queue_head
);
585 case ELEVATOR_INSERT_BACK
:
586 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
587 elv_drain_elevator(q
);
588 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
590 * We kick the queue here for the following reasons.
591 * - The elevator might have returned NULL previously
592 * to delay requests and returned them now. As the
593 * queue wasn't empty before this request, ll_rw_blk
594 * won't run the queue on return, resulting in hang.
595 * - Usually, back inserted requests won't be merged
596 * with anything. There's no point in delaying queue
603 case ELEVATOR_INSERT_SORT
:
604 BUG_ON(!blk_fs_request(rq
));
605 rq
->cmd_flags
|= REQ_SORTED
;
607 if (rq_mergeable(rq
)) {
608 elv_rqhash_add(q
, rq
);
614 * Some ioscheds (cfq) run q->request_fn directly, so
615 * rq cannot be accessed after calling
616 * elevator_add_req_fn.
618 q
->elevator
->ops
->elevator_add_req_fn(q
, rq
);
621 case ELEVATOR_INSERT_REQUEUE
:
623 * If ordered flush isn't in progress, we do front
624 * insertion; otherwise, requests should be requeued
627 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
630 * Most requeues happen because of a busy condition,
631 * don't force unplug of the queue for that case.
635 if (q
->ordseq
== 0) {
636 list_add(&rq
->queuelist
, &q
->queue_head
);
640 ordseq
= blk_ordered_req_seq(rq
);
642 list_for_each(pos
, &q
->queue_head
) {
643 struct request
*pos_rq
= list_entry_rq(pos
);
644 if (ordseq
<= blk_ordered_req_seq(pos_rq
))
648 list_add_tail(&rq
->queuelist
, pos
);
652 printk(KERN_ERR
"%s: bad insertion point %d\n",
653 __FUNCTION__
, where
);
657 if (unplug_it
&& blk_queue_plugged(q
)) {
658 int nrq
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]
661 if (nrq
>= q
->unplug_thresh
)
662 __generic_unplug_device(q
);
666 void __elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
,
670 rq
->cmd_flags
|= REQ_ORDERED_COLOR
;
672 if (rq
->cmd_flags
& (REQ_SOFTBARRIER
| REQ_HARDBARRIER
)) {
674 * toggle ordered color
676 if (blk_barrier_rq(rq
))
680 * barriers implicitly indicate back insertion
682 if (where
== ELEVATOR_INSERT_SORT
)
683 where
= ELEVATOR_INSERT_BACK
;
686 * this request is scheduling boundary, update
689 if (blk_fs_request(rq
)) {
690 q
->end_sector
= rq_end_sector(rq
);
693 } else if (!(rq
->cmd_flags
& REQ_ELVPRIV
) &&
694 where
== ELEVATOR_INSERT_SORT
)
695 where
= ELEVATOR_INSERT_BACK
;
700 elv_insert(q
, rq
, where
);
702 EXPORT_SYMBOL(__elv_add_request
);
704 void elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
,
709 spin_lock_irqsave(q
->queue_lock
, flags
);
710 __elv_add_request(q
, rq
, where
, plug
);
711 spin_unlock_irqrestore(q
->queue_lock
, flags
);
713 EXPORT_SYMBOL(elv_add_request
);
715 static inline struct request
*__elv_next_request(struct request_queue
*q
)
720 while (!list_empty(&q
->queue_head
)) {
721 rq
= list_entry_rq(q
->queue_head
.next
);
722 if (blk_do_ordered(q
, &rq
))
726 if (!q
->elevator
->ops
->elevator_dispatch_fn(q
, 0))
731 struct request
*elv_next_request(struct request_queue
*q
)
736 while ((rq
= __elv_next_request(q
)) != NULL
) {
738 * Kill the empty barrier place holder, the driver must
741 if (blk_empty_barrier(rq
)) {
742 end_queued_request(rq
, 1);
745 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
747 * This is the first time the device driver
748 * sees this request (possibly after
749 * requeueing). Notify IO scheduler.
751 if (blk_sorted_rq(rq
))
752 elv_activate_rq(q
, rq
);
755 * just mark as started even if we don't start
756 * it, a request that has been delayed should
757 * not be passed by new incoming requests
759 rq
->cmd_flags
|= REQ_STARTED
;
760 blk_add_trace_rq(q
, rq
, BLK_TA_ISSUE
);
763 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
764 q
->end_sector
= rq_end_sector(rq
);
765 q
->boundary_rq
= NULL
;
768 if (rq
->cmd_flags
& REQ_DONTPREP
)
771 if (q
->dma_drain_size
&& rq
->data_len
) {
773 * make sure space for the drain appears we
774 * know we can do this because max_hw_segments
775 * has been adjusted to be one fewer than the
778 rq
->nr_phys_segments
++;
779 rq
->nr_hw_segments
++;
785 ret
= q
->prep_rq_fn(q
, rq
);
786 if (ret
== BLKPREP_OK
) {
788 } else if (ret
== BLKPREP_DEFER
) {
790 * the request may have been (partially) prepped.
791 * we need to keep this request in the front to
792 * avoid resource deadlock. REQ_STARTED will
793 * prevent other fs requests from passing this one.
795 if (q
->dma_drain_size
&& rq
->data_len
&&
796 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
798 * remove the space for the drain we added
799 * so that we don't add it again
801 --rq
->nr_phys_segments
;
802 --rq
->nr_hw_segments
;
807 } else if (ret
== BLKPREP_KILL
) {
808 rq
->cmd_flags
|= REQ_QUIET
;
809 end_queued_request(rq
, 0);
811 printk(KERN_ERR
"%s: bad return=%d\n", __FUNCTION__
,
819 EXPORT_SYMBOL(elv_next_request
);
821 void elv_dequeue_request(struct request_queue
*q
, struct request
*rq
)
823 BUG_ON(list_empty(&rq
->queuelist
));
824 BUG_ON(ELV_ON_HASH(rq
));
826 list_del_init(&rq
->queuelist
);
829 * the time frame between a request being removed from the lists
830 * and to it is freed is accounted as io that is in progress at
833 if (blk_account_rq(rq
))
836 EXPORT_SYMBOL(elv_dequeue_request
);
838 int elv_queue_empty(struct request_queue
*q
)
840 elevator_t
*e
= q
->elevator
;
842 if (!list_empty(&q
->queue_head
))
845 if (e
->ops
->elevator_queue_empty_fn
)
846 return e
->ops
->elevator_queue_empty_fn(q
);
850 EXPORT_SYMBOL(elv_queue_empty
);
852 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
854 elevator_t
*e
= q
->elevator
;
856 if (e
->ops
->elevator_latter_req_fn
)
857 return e
->ops
->elevator_latter_req_fn(q
, rq
);
861 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
863 elevator_t
*e
= q
->elevator
;
865 if (e
->ops
->elevator_former_req_fn
)
866 return e
->ops
->elevator_former_req_fn(q
, rq
);
870 int elv_set_request(struct request_queue
*q
, struct request
*rq
, gfp_t gfp_mask
)
872 elevator_t
*e
= q
->elevator
;
874 if (e
->ops
->elevator_set_req_fn
)
875 return e
->ops
->elevator_set_req_fn(q
, rq
, gfp_mask
);
877 rq
->elevator_private
= NULL
;
881 void elv_put_request(struct request_queue
*q
, struct request
*rq
)
883 elevator_t
*e
= q
->elevator
;
885 if (e
->ops
->elevator_put_req_fn
)
886 e
->ops
->elevator_put_req_fn(rq
);
889 int elv_may_queue(struct request_queue
*q
, int rw
)
891 elevator_t
*e
= q
->elevator
;
893 if (e
->ops
->elevator_may_queue_fn
)
894 return e
->ops
->elevator_may_queue_fn(q
, rw
);
896 return ELV_MQUEUE_MAY
;
899 void elv_completed_request(struct request_queue
*q
, struct request
*rq
)
901 elevator_t
*e
= q
->elevator
;
904 * request is released from the driver, io must be done
906 if (blk_account_rq(rq
)) {
908 if (blk_sorted_rq(rq
) && e
->ops
->elevator_completed_req_fn
)
909 e
->ops
->elevator_completed_req_fn(q
, rq
);
913 * Check if the queue is waiting for fs requests to be
914 * drained for flush sequence.
916 if (unlikely(q
->ordseq
)) {
917 struct request
*first_rq
= list_entry_rq(q
->queue_head
.next
);
918 if (q
->in_flight
== 0 &&
919 blk_ordered_cur_seq(q
) == QUEUE_ORDSEQ_DRAIN
&&
920 blk_ordered_req_seq(first_rq
) > QUEUE_ORDSEQ_DRAIN
) {
921 blk_ordered_complete_seq(q
, QUEUE_ORDSEQ_DRAIN
, 0);
927 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
930 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
932 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
933 struct elv_fs_entry
*entry
= to_elv(attr
);
939 mutex_lock(&e
->sysfs_lock
);
940 error
= e
->ops
? entry
->show(e
, page
) : -ENOENT
;
941 mutex_unlock(&e
->sysfs_lock
);
946 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
947 const char *page
, size_t length
)
949 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
950 struct elv_fs_entry
*entry
= to_elv(attr
);
956 mutex_lock(&e
->sysfs_lock
);
957 error
= e
->ops
? entry
->store(e
, page
, length
) : -ENOENT
;
958 mutex_unlock(&e
->sysfs_lock
);
962 static struct sysfs_ops elv_sysfs_ops
= {
963 .show
= elv_attr_show
,
964 .store
= elv_attr_store
,
967 static struct kobj_type elv_ktype
= {
968 .sysfs_ops
= &elv_sysfs_ops
,
969 .release
= elevator_release
,
972 int elv_register_queue(struct request_queue
*q
)
974 elevator_t
*e
= q
->elevator
;
977 error
= kobject_add(&e
->kobj
, &q
->kobj
, "%s", "iosched");
979 struct elv_fs_entry
*attr
= e
->elevator_type
->elevator_attrs
;
981 while (attr
->attr
.name
) {
982 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
987 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
992 static void __elv_unregister_queue(elevator_t
*e
)
994 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
995 kobject_del(&e
->kobj
);
998 void elv_unregister_queue(struct request_queue
*q
)
1001 __elv_unregister_queue(q
->elevator
);
1004 void elv_register(struct elevator_type
*e
)
1008 spin_lock(&elv_list_lock
);
1009 BUG_ON(elevator_find(e
->elevator_name
));
1010 list_add_tail(&e
->list
, &elv_list
);
1011 spin_unlock(&elv_list_lock
);
1013 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
1014 (!*chosen_elevator
&&
1015 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
1018 printk(KERN_INFO
"io scheduler %s registered%s\n", e
->elevator_name
,
1021 EXPORT_SYMBOL_GPL(elv_register
);
1023 void elv_unregister(struct elevator_type
*e
)
1025 struct task_struct
*g
, *p
;
1028 * Iterate every thread in the process to remove the io contexts.
1031 read_lock(&tasklist_lock
);
1032 do_each_thread(g
, p
) {
1035 e
->ops
.trim(p
->io_context
);
1037 } while_each_thread(g
, p
);
1038 read_unlock(&tasklist_lock
);
1041 spin_lock(&elv_list_lock
);
1042 list_del_init(&e
->list
);
1043 spin_unlock(&elv_list_lock
);
1045 EXPORT_SYMBOL_GPL(elv_unregister
);
1048 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1049 * we don't free the old io scheduler, before we have allocated what we
1050 * need for the new one. this way we have a chance of going back to the old
1051 * one, if the new one fails init for some reason.
1053 static int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
1055 elevator_t
*old_elevator
, *e
;
1059 * Allocate new elevator
1061 e
= elevator_alloc(q
, new_e
);
1065 data
= elevator_init_queue(q
, e
);
1067 kobject_put(&e
->kobj
);
1072 * Turn on BYPASS and drain all requests w/ elevator private data
1074 spin_lock_irq(q
->queue_lock
);
1076 set_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1078 elv_drain_elevator(q
);
1080 while (q
->rq
.elvpriv
) {
1083 spin_unlock_irq(q
->queue_lock
);
1085 spin_lock_irq(q
->queue_lock
);
1086 elv_drain_elevator(q
);
1090 * Remember old elevator.
1092 old_elevator
= q
->elevator
;
1095 * attach and start new elevator
1097 elevator_attach(q
, e
, data
);
1099 spin_unlock_irq(q
->queue_lock
);
1101 __elv_unregister_queue(old_elevator
);
1103 if (elv_register_queue(q
))
1107 * finally exit old elevator and turn off BYPASS.
1109 elevator_exit(old_elevator
);
1110 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1115 * switch failed, exit the new io scheduler and reattach the old
1116 * one again (along with re-adding the sysfs dir)
1119 q
->elevator
= old_elevator
;
1120 elv_register_queue(q
);
1121 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1125 ssize_t
elv_iosched_store(struct request_queue
*q
, const char *name
,
1128 char elevator_name
[ELV_NAME_MAX
];
1130 struct elevator_type
*e
;
1132 elevator_name
[sizeof(elevator_name
) - 1] = '\0';
1133 strncpy(elevator_name
, name
, sizeof(elevator_name
) - 1);
1134 len
= strlen(elevator_name
);
1136 if (len
&& elevator_name
[len
- 1] == '\n')
1137 elevator_name
[len
- 1] = '\0';
1139 e
= elevator_get(elevator_name
);
1141 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
1145 if (!strcmp(elevator_name
, q
->elevator
->elevator_type
->elevator_name
)) {
1150 if (!elevator_switch(q
, e
))
1151 printk(KERN_ERR
"elevator: switch to %s failed\n",
1156 ssize_t
elv_iosched_show(struct request_queue
*q
, char *name
)
1158 elevator_t
*e
= q
->elevator
;
1159 struct elevator_type
*elv
= e
->elevator_type
;
1160 struct elevator_type
*__e
;
1163 spin_lock(&elv_list_lock
);
1164 list_for_each_entry(__e
, &elv_list
, list
) {
1165 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
1166 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
1168 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1170 spin_unlock(&elv_list_lock
);
1172 len
+= sprintf(len
+name
, "\n");
1176 struct request
*elv_rb_former_request(struct request_queue
*q
,
1179 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
1182 return rb_entry_rq(rbprev
);
1186 EXPORT_SYMBOL(elv_rb_former_request
);
1188 struct request
*elv_rb_latter_request(struct request_queue
*q
,
1191 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
1194 return rb_entry_rq(rbnext
);
1198 EXPORT_SYMBOL(elv_rb_latter_request
);