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 <trace/block.h>
37 #include <linux/hash.h>
38 #include <linux/uaccess.h>
42 static DEFINE_SPINLOCK(elv_list_lock
);
43 static LIST_HEAD(elv_list
);
48 static const int elv_hash_shift
= 6;
49 #define ELV_HASH_BLOCK(sec) ((sec) >> 3)
50 #define ELV_HASH_FN(sec) \
51 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
52 #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
53 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
54 #define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
57 * Query io scheduler to see if the current process issuing bio may be
60 static int elv_iosched_allow_merge(struct request
*rq
, struct bio
*bio
)
62 struct request_queue
*q
= rq
->q
;
63 elevator_t
*e
= q
->elevator
;
65 if (e
->ops
->elevator_allow_merge_fn
)
66 return e
->ops
->elevator_allow_merge_fn(q
, rq
, bio
);
72 * can we safely merge with this request?
74 int elv_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
76 if (!rq_mergeable(rq
))
80 * Don't merge file system requests and discard requests
82 if (bio_discard(bio
) != bio_discard(rq
->bio
))
86 * different data direction or already started, don't merge
88 if (bio_data_dir(bio
) != rq_data_dir(rq
))
92 * must be same device and not a special request
94 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| rq
->special
)
98 * only merge integrity protected bio into ditto rq
100 if (bio_integrity(bio
) != blk_integrity_rq(rq
))
103 if (!elv_iosched_allow_merge(rq
, bio
))
108 EXPORT_SYMBOL(elv_rq_merge_ok
);
110 static inline int elv_try_merge(struct request
*__rq
, struct bio
*bio
)
112 int ret
= ELEVATOR_NO_MERGE
;
115 * we can merge and sequence is ok, check if it's possible
117 if (elv_rq_merge_ok(__rq
, bio
)) {
118 if (__rq
->sector
+ __rq
->nr_sectors
== bio
->bi_sector
)
119 ret
= ELEVATOR_BACK_MERGE
;
120 else if (__rq
->sector
- bio_sectors(bio
) == bio
->bi_sector
)
121 ret
= ELEVATOR_FRONT_MERGE
;
127 static struct elevator_type
*elevator_find(const char *name
)
129 struct elevator_type
*e
;
131 list_for_each_entry(e
, &elv_list
, list
) {
132 if (!strcmp(e
->elevator_name
, name
))
139 static void elevator_put(struct elevator_type
*e
)
141 module_put(e
->elevator_owner
);
144 static struct elevator_type
*elevator_get(const char *name
)
146 struct elevator_type
*e
;
148 spin_lock(&elv_list_lock
);
150 e
= elevator_find(name
);
152 char elv
[ELV_NAME_MAX
+ strlen("-iosched")];
154 spin_unlock(&elv_list_lock
);
156 if (!strcmp(name
, "anticipatory"))
157 sprintf(elv
, "as-iosched");
159 sprintf(elv
, "%s-iosched", name
);
161 request_module("%s", elv
);
162 spin_lock(&elv_list_lock
);
163 e
= elevator_find(name
);
166 if (e
&& !try_module_get(e
->elevator_owner
))
169 spin_unlock(&elv_list_lock
);
174 static void *elevator_init_queue(struct request_queue
*q
,
175 struct elevator_queue
*eq
)
177 return eq
->ops
->elevator_init_fn(q
);
180 static void elevator_attach(struct request_queue
*q
, struct elevator_queue
*eq
,
184 eq
->elevator_data
= data
;
187 static char chosen_elevator
[16];
189 static int __init
elevator_setup(char *str
)
192 * Be backwards-compatible with previous kernels, so users
193 * won't get the wrong elevator.
195 if (!strcmp(str
, "as"))
196 strcpy(chosen_elevator
, "anticipatory");
198 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
202 __setup("elevator=", elevator_setup
);
204 static struct kobj_type elv_ktype
;
206 static elevator_t
*elevator_alloc(struct request_queue
*q
,
207 struct elevator_type
*e
)
212 eq
= kmalloc_node(sizeof(elevator_t
), GFP_KERNEL
| __GFP_ZERO
, q
->node
);
217 eq
->elevator_type
= e
;
218 kobject_init(&eq
->kobj
, &elv_ktype
);
219 mutex_init(&eq
->sysfs_lock
);
221 eq
->hash
= kmalloc_node(sizeof(struct hlist_head
) * ELV_HASH_ENTRIES
,
222 GFP_KERNEL
, q
->node
);
226 for (i
= 0; i
< ELV_HASH_ENTRIES
; i
++)
227 INIT_HLIST_HEAD(&eq
->hash
[i
]);
236 static void elevator_release(struct kobject
*kobj
)
238 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
240 elevator_put(e
->elevator_type
);
245 int elevator_init(struct request_queue
*q
, char *name
)
247 struct elevator_type
*e
= NULL
;
248 struct elevator_queue
*eq
;
252 INIT_LIST_HEAD(&q
->queue_head
);
253 q
->last_merge
= NULL
;
255 q
->boundary_rq
= NULL
;
258 e
= elevator_get(name
);
263 if (!e
&& *chosen_elevator
) {
264 e
= elevator_get(chosen_elevator
);
266 printk(KERN_ERR
"I/O scheduler %s not found\n",
271 e
= elevator_get(CONFIG_DEFAULT_IOSCHED
);
274 "Default I/O scheduler not found. " \
276 e
= elevator_get("noop");
280 eq
= elevator_alloc(q
, e
);
284 data
= elevator_init_queue(q
, eq
);
286 kobject_put(&eq
->kobj
);
290 elevator_attach(q
, eq
, data
);
293 EXPORT_SYMBOL(elevator_init
);
295 void elevator_exit(elevator_t
*e
)
297 mutex_lock(&e
->sysfs_lock
);
298 if (e
->ops
->elevator_exit_fn
)
299 e
->ops
->elevator_exit_fn(e
);
301 mutex_unlock(&e
->sysfs_lock
);
303 kobject_put(&e
->kobj
);
305 EXPORT_SYMBOL(elevator_exit
);
307 static void elv_activate_rq(struct request_queue
*q
, struct request
*rq
)
309 elevator_t
*e
= q
->elevator
;
311 if (e
->ops
->elevator_activate_req_fn
)
312 e
->ops
->elevator_activate_req_fn(q
, rq
);
315 static void elv_deactivate_rq(struct request_queue
*q
, struct request
*rq
)
317 elevator_t
*e
= q
->elevator
;
319 if (e
->ops
->elevator_deactivate_req_fn
)
320 e
->ops
->elevator_deactivate_req_fn(q
, rq
);
323 static inline void __elv_rqhash_del(struct request
*rq
)
325 hlist_del_init(&rq
->hash
);
328 static void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
331 __elv_rqhash_del(rq
);
334 static void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
336 elevator_t
*e
= q
->elevator
;
338 BUG_ON(ELV_ON_HASH(rq
));
339 hlist_add_head(&rq
->hash
, &e
->hash
[ELV_HASH_FN(rq_hash_key(rq
))]);
342 static void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
344 __elv_rqhash_del(rq
);
345 elv_rqhash_add(q
, rq
);
348 static struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
350 elevator_t
*e
= q
->elevator
;
351 struct hlist_head
*hash_list
= &e
->hash
[ELV_HASH_FN(offset
)];
352 struct hlist_node
*entry
, *next
;
355 hlist_for_each_entry_safe(rq
, entry
, next
, hash_list
, hash
) {
356 BUG_ON(!ELV_ON_HASH(rq
));
358 if (unlikely(!rq_mergeable(rq
))) {
359 __elv_rqhash_del(rq
);
363 if (rq_hash_key(rq
) == offset
)
371 * RB-tree support functions for inserting/lookup/removal of requests
372 * in a sorted RB tree.
374 struct request
*elv_rb_add(struct rb_root
*root
, struct request
*rq
)
376 struct rb_node
**p
= &root
->rb_node
;
377 struct rb_node
*parent
= NULL
;
378 struct request
*__rq
;
382 __rq
= rb_entry(parent
, struct request
, rb_node
);
384 if (rq
->sector
< __rq
->sector
)
386 else if (rq
->sector
> __rq
->sector
)
392 rb_link_node(&rq
->rb_node
, parent
, p
);
393 rb_insert_color(&rq
->rb_node
, root
);
396 EXPORT_SYMBOL(elv_rb_add
);
398 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
400 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
401 rb_erase(&rq
->rb_node
, root
);
402 RB_CLEAR_NODE(&rq
->rb_node
);
404 EXPORT_SYMBOL(elv_rb_del
);
406 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
408 struct rb_node
*n
= root
->rb_node
;
412 rq
= rb_entry(n
, struct request
, rb_node
);
414 if (sector
< rq
->sector
)
416 else if (sector
> rq
->sector
)
424 EXPORT_SYMBOL(elv_rb_find
);
427 * Insert rq into dispatch queue of q. Queue lock must be held on
428 * entry. rq is sort instead into the dispatch queue. To be used by
429 * specific elevators.
431 void elv_dispatch_sort(struct request_queue
*q
, struct request
*rq
)
434 struct list_head
*entry
;
437 if (q
->last_merge
== rq
)
438 q
->last_merge
= NULL
;
440 elv_rqhash_del(q
, rq
);
444 boundary
= q
->end_sector
;
445 stop_flags
= REQ_SOFTBARRIER
| REQ_HARDBARRIER
| REQ_STARTED
;
446 list_for_each_prev(entry
, &q
->queue_head
) {
447 struct request
*pos
= list_entry_rq(entry
);
449 if (blk_discard_rq(rq
) != blk_discard_rq(pos
))
451 if (rq_data_dir(rq
) != rq_data_dir(pos
))
453 if (pos
->cmd_flags
& stop_flags
)
455 if (rq
->sector
>= boundary
) {
456 if (pos
->sector
< boundary
)
459 if (pos
->sector
>= boundary
)
462 if (rq
->sector
>= pos
->sector
)
466 list_add(&rq
->queuelist
, entry
);
468 EXPORT_SYMBOL(elv_dispatch_sort
);
471 * Insert rq into dispatch queue of q. Queue lock must be held on
472 * entry. rq is added to the back of the dispatch queue. To be used by
473 * specific elevators.
475 void elv_dispatch_add_tail(struct request_queue
*q
, struct request
*rq
)
477 if (q
->last_merge
== rq
)
478 q
->last_merge
= NULL
;
480 elv_rqhash_del(q
, rq
);
484 q
->end_sector
= rq_end_sector(rq
);
486 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
488 EXPORT_SYMBOL(elv_dispatch_add_tail
);
490 int elv_merge(struct request_queue
*q
, struct request
**req
, struct bio
*bio
)
492 elevator_t
*e
= q
->elevator
;
493 struct request
*__rq
;
497 * First try one-hit cache.
500 ret
= elv_try_merge(q
->last_merge
, bio
);
501 if (ret
!= ELEVATOR_NO_MERGE
) {
502 *req
= q
->last_merge
;
507 if (blk_queue_nomerges(q
))
508 return ELEVATOR_NO_MERGE
;
511 * See if our hash lookup can find a potential backmerge.
513 __rq
= elv_rqhash_find(q
, bio
->bi_sector
);
514 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
516 return ELEVATOR_BACK_MERGE
;
519 if (e
->ops
->elevator_merge_fn
)
520 return e
->ops
->elevator_merge_fn(q
, req
, bio
);
522 return ELEVATOR_NO_MERGE
;
525 void elv_merged_request(struct request_queue
*q
, struct request
*rq
, int type
)
527 elevator_t
*e
= q
->elevator
;
529 if (e
->ops
->elevator_merged_fn
)
530 e
->ops
->elevator_merged_fn(q
, rq
, type
);
532 if (type
== ELEVATOR_BACK_MERGE
)
533 elv_rqhash_reposition(q
, rq
);
538 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
539 struct request
*next
)
541 elevator_t
*e
= q
->elevator
;
543 if (e
->ops
->elevator_merge_req_fn
)
544 e
->ops
->elevator_merge_req_fn(q
, rq
, next
);
546 elv_rqhash_reposition(q
, rq
);
547 elv_rqhash_del(q
, next
);
553 void elv_requeue_request(struct request_queue
*q
, struct request
*rq
)
556 * it already went through dequeue, we need to decrement the
557 * in_flight count again
559 if (blk_account_rq(rq
)) {
561 if (blk_sorted_rq(rq
))
562 elv_deactivate_rq(q
, rq
);
565 rq
->cmd_flags
&= ~REQ_STARTED
;
567 elv_insert(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
570 static void elv_drain_elevator(struct request_queue
*q
)
573 while (q
->elevator
->ops
->elevator_dispatch_fn(q
, 1))
575 if (q
->nr_sorted
== 0)
577 if (printed
++ < 10) {
578 printk(KERN_ERR
"%s: forced dispatching is broken "
579 "(nr_sorted=%u), please report this\n",
580 q
->elevator
->elevator_type
->elevator_name
, q
->nr_sorted
);
584 void elv_insert(struct request_queue
*q
, struct request
*rq
, int where
)
586 struct list_head
*pos
;
590 trace_block_rq_insert(q
, rq
);
595 case ELEVATOR_INSERT_FRONT
:
596 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
598 list_add(&rq
->queuelist
, &q
->queue_head
);
601 case ELEVATOR_INSERT_BACK
:
602 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
603 elv_drain_elevator(q
);
604 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
606 * We kick the queue here for the following reasons.
607 * - The elevator might have returned NULL previously
608 * to delay requests and returned them now. As the
609 * queue wasn't empty before this request, ll_rw_blk
610 * won't run the queue on return, resulting in hang.
611 * - Usually, back inserted requests won't be merged
612 * with anything. There's no point in delaying queue
616 blk_start_queueing(q
);
619 case ELEVATOR_INSERT_SORT
:
620 BUG_ON(!blk_fs_request(rq
) && !blk_discard_rq(rq
));
621 rq
->cmd_flags
|= REQ_SORTED
;
623 if (rq_mergeable(rq
)) {
624 elv_rqhash_add(q
, rq
);
630 * Some ioscheds (cfq) run q->request_fn directly, so
631 * rq cannot be accessed after calling
632 * elevator_add_req_fn.
634 q
->elevator
->ops
->elevator_add_req_fn(q
, rq
);
637 case ELEVATOR_INSERT_REQUEUE
:
639 * If ordered flush isn't in progress, we do front
640 * insertion; otherwise, requests should be requeued
643 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
646 * Most requeues happen because of a busy condition,
647 * don't force unplug of the queue for that case.
651 if (q
->ordseq
== 0) {
652 list_add(&rq
->queuelist
, &q
->queue_head
);
656 ordseq
= blk_ordered_req_seq(rq
);
658 list_for_each(pos
, &q
->queue_head
) {
659 struct request
*pos_rq
= list_entry_rq(pos
);
660 if (ordseq
<= blk_ordered_req_seq(pos_rq
))
664 list_add_tail(&rq
->queuelist
, pos
);
668 printk(KERN_ERR
"%s: bad insertion point %d\n",
673 if (unplug_it
&& blk_queue_plugged(q
)) {
674 int nrq
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]
677 if (nrq
>= q
->unplug_thresh
)
678 __generic_unplug_device(q
);
682 void __elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
,
686 rq
->cmd_flags
|= REQ_ORDERED_COLOR
;
688 if (rq
->cmd_flags
& (REQ_SOFTBARRIER
| REQ_HARDBARRIER
)) {
690 * toggle ordered color
692 if (blk_barrier_rq(rq
))
696 * barriers implicitly indicate back insertion
698 if (where
== ELEVATOR_INSERT_SORT
)
699 where
= ELEVATOR_INSERT_BACK
;
702 * this request is scheduling boundary, update
705 if (blk_fs_request(rq
) || blk_discard_rq(rq
)) {
706 q
->end_sector
= rq_end_sector(rq
);
709 } else if (!(rq
->cmd_flags
& REQ_ELVPRIV
) &&
710 where
== ELEVATOR_INSERT_SORT
)
711 where
= ELEVATOR_INSERT_BACK
;
716 elv_insert(q
, rq
, where
);
718 EXPORT_SYMBOL(__elv_add_request
);
720 void elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
,
725 spin_lock_irqsave(q
->queue_lock
, flags
);
726 __elv_add_request(q
, rq
, where
, plug
);
727 spin_unlock_irqrestore(q
->queue_lock
, flags
);
729 EXPORT_SYMBOL(elv_add_request
);
731 static inline struct request
*__elv_next_request(struct request_queue
*q
)
736 while (!list_empty(&q
->queue_head
)) {
737 rq
= list_entry_rq(q
->queue_head
.next
);
738 if (blk_do_ordered(q
, &rq
))
742 if (!q
->elevator
->ops
->elevator_dispatch_fn(q
, 0))
747 struct request
*elv_next_request(struct request_queue
*q
)
752 while ((rq
= __elv_next_request(q
)) != NULL
) {
754 * Kill the empty barrier place holder, the driver must
757 if (blk_empty_barrier(rq
)) {
758 __blk_end_request(rq
, 0, blk_rq_bytes(rq
));
761 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
763 * This is the first time the device driver
764 * sees this request (possibly after
765 * requeueing). Notify IO scheduler.
767 if (blk_sorted_rq(rq
))
768 elv_activate_rq(q
, rq
);
771 * just mark as started even if we don't start
772 * it, a request that has been delayed should
773 * not be passed by new incoming requests
775 rq
->cmd_flags
|= REQ_STARTED
;
776 trace_block_rq_issue(q
, rq
);
779 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
780 q
->end_sector
= rq_end_sector(rq
);
781 q
->boundary_rq
= NULL
;
784 if (rq
->cmd_flags
& REQ_DONTPREP
)
787 if (q
->dma_drain_size
&& rq
->data_len
) {
789 * make sure space for the drain appears we
790 * know we can do this because max_hw_segments
791 * has been adjusted to be one fewer than the
794 rq
->nr_phys_segments
++;
800 ret
= q
->prep_rq_fn(q
, rq
);
801 if (ret
== BLKPREP_OK
) {
803 } else if (ret
== BLKPREP_DEFER
) {
805 * the request may have been (partially) prepped.
806 * we need to keep this request in the front to
807 * avoid resource deadlock. REQ_STARTED will
808 * prevent other fs requests from passing this one.
810 if (q
->dma_drain_size
&& rq
->data_len
&&
811 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
813 * remove the space for the drain we added
814 * so that we don't add it again
816 --rq
->nr_phys_segments
;
821 } else if (ret
== BLKPREP_KILL
) {
822 rq
->cmd_flags
|= REQ_QUIET
;
823 __blk_end_request(rq
, -EIO
, blk_rq_bytes(rq
));
825 printk(KERN_ERR
"%s: bad return=%d\n", __func__
, ret
);
832 EXPORT_SYMBOL(elv_next_request
);
834 void elv_dequeue_request(struct request_queue
*q
, struct request
*rq
)
836 BUG_ON(list_empty(&rq
->queuelist
));
837 BUG_ON(ELV_ON_HASH(rq
));
839 list_del_init(&rq
->queuelist
);
842 * the time frame between a request being removed from the lists
843 * and to it is freed is accounted as io that is in progress at
846 if (blk_account_rq(rq
))
850 * We are now handing the request to the hardware, add the
855 EXPORT_SYMBOL(elv_dequeue_request
);
857 int elv_queue_empty(struct request_queue
*q
)
859 elevator_t
*e
= q
->elevator
;
861 if (!list_empty(&q
->queue_head
))
864 if (e
->ops
->elevator_queue_empty_fn
)
865 return e
->ops
->elevator_queue_empty_fn(q
);
869 EXPORT_SYMBOL(elv_queue_empty
);
871 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
873 elevator_t
*e
= q
->elevator
;
875 if (e
->ops
->elevator_latter_req_fn
)
876 return e
->ops
->elevator_latter_req_fn(q
, rq
);
880 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
882 elevator_t
*e
= q
->elevator
;
884 if (e
->ops
->elevator_former_req_fn
)
885 return e
->ops
->elevator_former_req_fn(q
, rq
);
889 int elv_set_request(struct request_queue
*q
, struct request
*rq
, gfp_t gfp_mask
)
891 elevator_t
*e
= q
->elevator
;
893 if (e
->ops
->elevator_set_req_fn
)
894 return e
->ops
->elevator_set_req_fn(q
, rq
, gfp_mask
);
896 rq
->elevator_private
= NULL
;
900 void elv_put_request(struct request_queue
*q
, struct request
*rq
)
902 elevator_t
*e
= q
->elevator
;
904 if (e
->ops
->elevator_put_req_fn
)
905 e
->ops
->elevator_put_req_fn(rq
);
908 int elv_may_queue(struct request_queue
*q
, int rw
)
910 elevator_t
*e
= q
->elevator
;
912 if (e
->ops
->elevator_may_queue_fn
)
913 return e
->ops
->elevator_may_queue_fn(q
, rw
);
915 return ELV_MQUEUE_MAY
;
918 void elv_abort_queue(struct request_queue
*q
)
922 while (!list_empty(&q
->queue_head
)) {
923 rq
= list_entry_rq(q
->queue_head
.next
);
924 rq
->cmd_flags
|= REQ_QUIET
;
925 trace_block_rq_abort(q
, rq
);
926 __blk_end_request(rq
, -EIO
, blk_rq_bytes(rq
));
929 EXPORT_SYMBOL(elv_abort_queue
);
931 void elv_completed_request(struct request_queue
*q
, struct request
*rq
)
933 elevator_t
*e
= q
->elevator
;
936 * request is released from the driver, io must be done
938 if (blk_account_rq(rq
)) {
940 if (blk_sorted_rq(rq
) && e
->ops
->elevator_completed_req_fn
)
941 e
->ops
->elevator_completed_req_fn(q
, rq
);
945 * Check if the queue is waiting for fs requests to be
946 * drained for flush sequence.
948 if (unlikely(q
->ordseq
)) {
949 struct request
*first_rq
= list_entry_rq(q
->queue_head
.next
);
950 if (q
->in_flight
== 0 &&
951 blk_ordered_cur_seq(q
) == QUEUE_ORDSEQ_DRAIN
&&
952 blk_ordered_req_seq(first_rq
) > QUEUE_ORDSEQ_DRAIN
) {
953 blk_ordered_complete_seq(q
, QUEUE_ORDSEQ_DRAIN
, 0);
954 blk_start_queueing(q
);
959 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
962 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
964 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
965 struct elv_fs_entry
*entry
= to_elv(attr
);
971 mutex_lock(&e
->sysfs_lock
);
972 error
= e
->ops
? entry
->show(e
, page
) : -ENOENT
;
973 mutex_unlock(&e
->sysfs_lock
);
978 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
979 const char *page
, size_t length
)
981 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
982 struct elv_fs_entry
*entry
= to_elv(attr
);
988 mutex_lock(&e
->sysfs_lock
);
989 error
= e
->ops
? entry
->store(e
, page
, length
) : -ENOENT
;
990 mutex_unlock(&e
->sysfs_lock
);
994 static struct sysfs_ops elv_sysfs_ops
= {
995 .show
= elv_attr_show
,
996 .store
= elv_attr_store
,
999 static struct kobj_type elv_ktype
= {
1000 .sysfs_ops
= &elv_sysfs_ops
,
1001 .release
= elevator_release
,
1004 int elv_register_queue(struct request_queue
*q
)
1006 elevator_t
*e
= q
->elevator
;
1009 error
= kobject_add(&e
->kobj
, &q
->kobj
, "%s", "iosched");
1011 struct elv_fs_entry
*attr
= e
->elevator_type
->elevator_attrs
;
1013 while (attr
->attr
.name
) {
1014 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
1019 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
1024 static void __elv_unregister_queue(elevator_t
*e
)
1026 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
1027 kobject_del(&e
->kobj
);
1030 void elv_unregister_queue(struct request_queue
*q
)
1033 __elv_unregister_queue(q
->elevator
);
1036 void elv_register(struct elevator_type
*e
)
1040 spin_lock(&elv_list_lock
);
1041 BUG_ON(elevator_find(e
->elevator_name
));
1042 list_add_tail(&e
->list
, &elv_list
);
1043 spin_unlock(&elv_list_lock
);
1045 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
1046 (!*chosen_elevator
&&
1047 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
1050 printk(KERN_INFO
"io scheduler %s registered%s\n", e
->elevator_name
,
1053 EXPORT_SYMBOL_GPL(elv_register
);
1055 void elv_unregister(struct elevator_type
*e
)
1057 struct task_struct
*g
, *p
;
1060 * Iterate every thread in the process to remove the io contexts.
1063 read_lock(&tasklist_lock
);
1064 do_each_thread(g
, p
) {
1067 e
->ops
.trim(p
->io_context
);
1069 } while_each_thread(g
, p
);
1070 read_unlock(&tasklist_lock
);
1073 spin_lock(&elv_list_lock
);
1074 list_del_init(&e
->list
);
1075 spin_unlock(&elv_list_lock
);
1077 EXPORT_SYMBOL_GPL(elv_unregister
);
1080 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1081 * we don't free the old io scheduler, before we have allocated what we
1082 * need for the new one. this way we have a chance of going back to the old
1083 * one, if the new one fails init for some reason.
1085 static int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
1087 elevator_t
*old_elevator
, *e
;
1091 * Allocate new elevator
1093 e
= elevator_alloc(q
, new_e
);
1097 data
= elevator_init_queue(q
, e
);
1099 kobject_put(&e
->kobj
);
1104 * Turn on BYPASS and drain all requests w/ elevator private data
1106 spin_lock_irq(q
->queue_lock
);
1108 queue_flag_set(QUEUE_FLAG_ELVSWITCH
, q
);
1110 elv_drain_elevator(q
);
1112 while (q
->rq
.elvpriv
) {
1113 blk_start_queueing(q
);
1114 spin_unlock_irq(q
->queue_lock
);
1116 spin_lock_irq(q
->queue_lock
);
1117 elv_drain_elevator(q
);
1121 * Remember old elevator.
1123 old_elevator
= q
->elevator
;
1126 * attach and start new elevator
1128 elevator_attach(q
, e
, data
);
1130 spin_unlock_irq(q
->queue_lock
);
1132 __elv_unregister_queue(old_elevator
);
1134 if (elv_register_queue(q
))
1138 * finally exit old elevator and turn off BYPASS.
1140 elevator_exit(old_elevator
);
1141 spin_lock_irq(q
->queue_lock
);
1142 queue_flag_clear(QUEUE_FLAG_ELVSWITCH
, q
);
1143 spin_unlock_irq(q
->queue_lock
);
1145 blk_add_trace_msg(q
, "elv switch: %s", e
->elevator_type
->elevator_name
);
1151 * switch failed, exit the new io scheduler and reattach the old
1152 * one again (along with re-adding the sysfs dir)
1155 q
->elevator
= old_elevator
;
1156 elv_register_queue(q
);
1158 spin_lock_irq(q
->queue_lock
);
1159 queue_flag_clear(QUEUE_FLAG_ELVSWITCH
, q
);
1160 spin_unlock_irq(q
->queue_lock
);
1165 ssize_t
elv_iosched_store(struct request_queue
*q
, const char *name
,
1168 char elevator_name
[ELV_NAME_MAX
];
1169 struct elevator_type
*e
;
1171 strlcpy(elevator_name
, name
, sizeof(elevator_name
));
1172 strstrip(elevator_name
);
1174 e
= elevator_get(elevator_name
);
1176 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
1180 if (!strcmp(elevator_name
, q
->elevator
->elevator_type
->elevator_name
)) {
1185 if (!elevator_switch(q
, e
))
1186 printk(KERN_ERR
"elevator: switch to %s failed\n",
1191 ssize_t
elv_iosched_show(struct request_queue
*q
, char *name
)
1193 elevator_t
*e
= q
->elevator
;
1194 struct elevator_type
*elv
= e
->elevator_type
;
1195 struct elevator_type
*__e
;
1198 spin_lock(&elv_list_lock
);
1199 list_for_each_entry(__e
, &elv_list
, list
) {
1200 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
1201 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
1203 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1205 spin_unlock(&elv_list_lock
);
1207 len
+= sprintf(len
+name
, "\n");
1211 struct request
*elv_rb_former_request(struct request_queue
*q
,
1214 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
1217 return rb_entry_rq(rbprev
);
1221 EXPORT_SYMBOL(elv_rb_former_request
);
1223 struct request
*elv_rb_latter_request(struct request_queue
*q
,
1226 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
1229 return rb_entry_rq(rbnext
);
1233 EXPORT_SYMBOL(elv_rb_latter_request
);