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/blktrace_api.h>
35 #include <linux/hash.h>
36 #include <linux/uaccess.h>
38 #include <trace/events/block.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) (blk_rq_pos(rq) + blk_rq_sectors(rq))
56 * Query io scheduler to see if the current process issuing bio may be
59 static int elv_iosched_allow_merge(struct request
*rq
, struct bio
*bio
)
61 struct request_queue
*q
= rq
->q
;
62 struct elevator_queue
*e
= q
->elevator
;
64 if (e
->type
->ops
.elevator_allow_merge_fn
)
65 return e
->type
->ops
.elevator_allow_merge_fn(q
, rq
, bio
);
71 * can we safely merge with this request?
73 int elv_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
75 if (!rq_mergeable(rq
))
79 * Don't merge file system requests and discard requests
81 if ((bio
->bi_rw
& REQ_DISCARD
) != (rq
->bio
->bi_rw
& REQ_DISCARD
))
85 * Don't merge discard requests and secure discard requests
87 if ((bio
->bi_rw
& REQ_SECURE
) != (rq
->bio
->bi_rw
& REQ_SECURE
))
91 * different data direction or already started, don't merge
93 if (bio_data_dir(bio
) != rq_data_dir(rq
))
97 * must be same device and not a special request
99 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| rq
->special
)
103 * only merge integrity protected bio into ditto rq
105 if (bio_integrity(bio
) != blk_integrity_rq(rq
))
108 if (!elv_iosched_allow_merge(rq
, bio
))
113 EXPORT_SYMBOL(elv_rq_merge_ok
);
115 int elv_try_merge(struct request
*__rq
, struct bio
*bio
)
117 int ret
= ELEVATOR_NO_MERGE
;
120 * we can merge and sequence is ok, check if it's possible
122 if (elv_rq_merge_ok(__rq
, bio
)) {
123 if (blk_rq_pos(__rq
) + blk_rq_sectors(__rq
) == bio
->bi_sector
)
124 ret
= ELEVATOR_BACK_MERGE
;
125 else if (blk_rq_pos(__rq
) - bio_sectors(bio
) == bio
->bi_sector
)
126 ret
= ELEVATOR_FRONT_MERGE
;
132 static struct elevator_type
*elevator_find(const char *name
)
134 struct elevator_type
*e
;
136 list_for_each_entry(e
, &elv_list
, list
) {
137 if (!strcmp(e
->elevator_name
, name
))
144 static void elevator_put(struct elevator_type
*e
)
146 module_put(e
->elevator_owner
);
149 static struct elevator_type
*elevator_get(const char *name
)
151 struct elevator_type
*e
;
153 spin_lock(&elv_list_lock
);
155 e
= elevator_find(name
);
157 spin_unlock(&elv_list_lock
);
158 request_module("%s-iosched", name
);
159 spin_lock(&elv_list_lock
);
160 e
= elevator_find(name
);
163 if (e
&& !try_module_get(e
->elevator_owner
))
166 spin_unlock(&elv_list_lock
);
171 static int elevator_init_queue(struct request_queue
*q
,
172 struct elevator_queue
*eq
)
174 eq
->elevator_data
= eq
->type
->ops
.elevator_init_fn(q
);
175 if (eq
->elevator_data
)
180 static char chosen_elevator
[ELV_NAME_MAX
];
182 static int __init
elevator_setup(char *str
)
185 * Be backwards-compatible with previous kernels, so users
186 * won't get the wrong elevator.
188 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
192 __setup("elevator=", elevator_setup
);
194 static struct kobj_type elv_ktype
;
196 static struct elevator_queue
*elevator_alloc(struct request_queue
*q
,
197 struct elevator_type
*e
)
199 struct elevator_queue
*eq
;
202 eq
= kmalloc_node(sizeof(*eq
), GFP_KERNEL
| __GFP_ZERO
, q
->node
);
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 struct elevator_queue
*e
;
229 e
= container_of(kobj
, struct elevator_queue
, kobj
);
230 elevator_put(e
->type
);
235 int elevator_init(struct request_queue
*q
, char *name
)
237 struct elevator_type
*e
= NULL
;
238 struct elevator_queue
*eq
;
241 if (unlikely(q
->elevator
))
244 INIT_LIST_HEAD(&q
->queue_head
);
245 q
->last_merge
= NULL
;
247 q
->boundary_rq
= NULL
;
250 e
= elevator_get(name
);
255 if (!e
&& *chosen_elevator
) {
256 e
= elevator_get(chosen_elevator
);
258 printk(KERN_ERR
"I/O scheduler %s not found\n",
263 e
= elevator_get(CONFIG_DEFAULT_IOSCHED
);
266 "Default I/O scheduler not found. " \
268 e
= elevator_get("noop");
272 eq
= elevator_alloc(q
, e
);
276 err
= elevator_init_queue(q
, eq
);
278 kobject_put(&eq
->kobj
);
285 EXPORT_SYMBOL(elevator_init
);
287 void elevator_exit(struct elevator_queue
*e
)
289 mutex_lock(&e
->sysfs_lock
);
290 if (e
->type
->ops
.elevator_exit_fn
)
291 e
->type
->ops
.elevator_exit_fn(e
);
292 mutex_unlock(&e
->sysfs_lock
);
294 kobject_put(&e
->kobj
);
296 EXPORT_SYMBOL(elevator_exit
);
298 static inline void __elv_rqhash_del(struct request
*rq
)
300 hlist_del_init(&rq
->hash
);
303 static void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
306 __elv_rqhash_del(rq
);
309 static void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
311 struct elevator_queue
*e
= q
->elevator
;
313 BUG_ON(ELV_ON_HASH(rq
));
314 hlist_add_head(&rq
->hash
, &e
->hash
[ELV_HASH_FN(rq_hash_key(rq
))]);
317 static void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
319 __elv_rqhash_del(rq
);
320 elv_rqhash_add(q
, rq
);
323 static struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
325 struct elevator_queue
*e
= q
->elevator
;
326 struct hlist_head
*hash_list
= &e
->hash
[ELV_HASH_FN(offset
)];
327 struct hlist_node
*entry
, *next
;
330 hlist_for_each_entry_safe(rq
, entry
, next
, hash_list
, hash
) {
331 BUG_ON(!ELV_ON_HASH(rq
));
333 if (unlikely(!rq_mergeable(rq
))) {
334 __elv_rqhash_del(rq
);
338 if (rq_hash_key(rq
) == offset
)
346 * RB-tree support functions for inserting/lookup/removal of requests
347 * in a sorted RB tree.
349 void elv_rb_add(struct rb_root
*root
, struct request
*rq
)
351 struct rb_node
**p
= &root
->rb_node
;
352 struct rb_node
*parent
= NULL
;
353 struct request
*__rq
;
357 __rq
= rb_entry(parent
, struct request
, rb_node
);
359 if (blk_rq_pos(rq
) < blk_rq_pos(__rq
))
361 else if (blk_rq_pos(rq
) >= blk_rq_pos(__rq
))
365 rb_link_node(&rq
->rb_node
, parent
, p
);
366 rb_insert_color(&rq
->rb_node
, root
);
368 EXPORT_SYMBOL(elv_rb_add
);
370 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
372 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
373 rb_erase(&rq
->rb_node
, root
);
374 RB_CLEAR_NODE(&rq
->rb_node
);
376 EXPORT_SYMBOL(elv_rb_del
);
378 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
380 struct rb_node
*n
= root
->rb_node
;
384 rq
= rb_entry(n
, struct request
, rb_node
);
386 if (sector
< blk_rq_pos(rq
))
388 else if (sector
> blk_rq_pos(rq
))
396 EXPORT_SYMBOL(elv_rb_find
);
399 * Insert rq into dispatch queue of q. Queue lock must be held on
400 * entry. rq is sort instead into the dispatch queue. To be used by
401 * specific elevators.
403 void elv_dispatch_sort(struct request_queue
*q
, struct request
*rq
)
406 struct list_head
*entry
;
409 if (q
->last_merge
== rq
)
410 q
->last_merge
= NULL
;
412 elv_rqhash_del(q
, rq
);
416 boundary
= q
->end_sector
;
417 stop_flags
= REQ_SOFTBARRIER
| REQ_STARTED
;
418 list_for_each_prev(entry
, &q
->queue_head
) {
419 struct request
*pos
= list_entry_rq(entry
);
421 if ((rq
->cmd_flags
& REQ_DISCARD
) !=
422 (pos
->cmd_flags
& REQ_DISCARD
))
424 if (rq_data_dir(rq
) != rq_data_dir(pos
))
426 if (pos
->cmd_flags
& stop_flags
)
428 if (blk_rq_pos(rq
) >= boundary
) {
429 if (blk_rq_pos(pos
) < boundary
)
432 if (blk_rq_pos(pos
) >= boundary
)
435 if (blk_rq_pos(rq
) >= blk_rq_pos(pos
))
439 list_add(&rq
->queuelist
, entry
);
441 EXPORT_SYMBOL(elv_dispatch_sort
);
444 * Insert rq into dispatch queue of q. Queue lock must be held on
445 * entry. rq is added to the back of the dispatch queue. To be used by
446 * specific elevators.
448 void elv_dispatch_add_tail(struct request_queue
*q
, struct request
*rq
)
450 if (q
->last_merge
== rq
)
451 q
->last_merge
= NULL
;
453 elv_rqhash_del(q
, rq
);
457 q
->end_sector
= rq_end_sector(rq
);
459 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
461 EXPORT_SYMBOL(elv_dispatch_add_tail
);
463 int elv_merge(struct request_queue
*q
, struct request
**req
, struct bio
*bio
)
465 struct elevator_queue
*e
= q
->elevator
;
466 struct request
*__rq
;
471 * nomerges: No merges at all attempted
472 * noxmerges: Only simple one-hit cache try
473 * merges: All merge tries attempted
475 if (blk_queue_nomerges(q
))
476 return ELEVATOR_NO_MERGE
;
479 * First try one-hit cache.
482 ret
= elv_try_merge(q
->last_merge
, bio
);
483 if (ret
!= ELEVATOR_NO_MERGE
) {
484 *req
= q
->last_merge
;
489 if (blk_queue_noxmerges(q
))
490 return ELEVATOR_NO_MERGE
;
493 * See if our hash lookup can find a potential backmerge.
495 __rq
= elv_rqhash_find(q
, bio
->bi_sector
);
496 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
498 return ELEVATOR_BACK_MERGE
;
501 if (e
->type
->ops
.elevator_merge_fn
)
502 return e
->type
->ops
.elevator_merge_fn(q
, req
, bio
);
504 return ELEVATOR_NO_MERGE
;
508 * Attempt to do an insertion back merge. Only check for the case where
509 * we can append 'rq' to an existing request, so we can throw 'rq' away
512 * Returns true if we merged, false otherwise
514 static bool elv_attempt_insert_merge(struct request_queue
*q
,
517 struct request
*__rq
;
519 if (blk_queue_nomerges(q
))
523 * First try one-hit cache.
525 if (q
->last_merge
&& blk_attempt_req_merge(q
, q
->last_merge
, rq
))
528 if (blk_queue_noxmerges(q
))
532 * See if our hash lookup can find a potential backmerge.
534 __rq
= elv_rqhash_find(q
, blk_rq_pos(rq
));
535 if (__rq
&& blk_attempt_req_merge(q
, __rq
, rq
))
541 void elv_merged_request(struct request_queue
*q
, struct request
*rq
, int type
)
543 struct elevator_queue
*e
= q
->elevator
;
545 if (e
->type
->ops
.elevator_merged_fn
)
546 e
->type
->ops
.elevator_merged_fn(q
, rq
, type
);
548 if (type
== ELEVATOR_BACK_MERGE
)
549 elv_rqhash_reposition(q
, rq
);
554 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
555 struct request
*next
)
557 struct elevator_queue
*e
= q
->elevator
;
558 const int next_sorted
= next
->cmd_flags
& REQ_SORTED
;
560 if (next_sorted
&& e
->type
->ops
.elevator_merge_req_fn
)
561 e
->type
->ops
.elevator_merge_req_fn(q
, rq
, next
);
563 elv_rqhash_reposition(q
, rq
);
566 elv_rqhash_del(q
, next
);
573 void elv_bio_merged(struct request_queue
*q
, struct request
*rq
,
576 struct elevator_queue
*e
= q
->elevator
;
578 if (e
->type
->ops
.elevator_bio_merged_fn
)
579 e
->type
->ops
.elevator_bio_merged_fn(q
, rq
, bio
);
582 void elv_requeue_request(struct request_queue
*q
, struct request
*rq
)
585 * it already went through dequeue, we need to decrement the
586 * in_flight count again
588 if (blk_account_rq(rq
)) {
589 q
->in_flight
[rq_is_sync(rq
)]--;
590 if (rq
->cmd_flags
& REQ_SORTED
)
591 elv_deactivate_rq(q
, rq
);
594 rq
->cmd_flags
&= ~REQ_STARTED
;
596 __elv_add_request(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
599 void elv_drain_elevator(struct request_queue
*q
)
603 lockdep_assert_held(q
->queue_lock
);
605 while (q
->elevator
->type
->ops
.elevator_dispatch_fn(q
, 1))
607 if (q
->nr_sorted
&& printed
++ < 10) {
608 printk(KERN_ERR
"%s: forced dispatching is broken "
609 "(nr_sorted=%u), please report this\n",
610 q
->elevator
->type
->elevator_name
, q
->nr_sorted
);
614 void elv_quiesce_start(struct request_queue
*q
)
619 spin_lock_irq(q
->queue_lock
);
620 queue_flag_set(QUEUE_FLAG_ELVSWITCH
, q
);
621 spin_unlock_irq(q
->queue_lock
);
623 blk_drain_queue(q
, false);
626 void elv_quiesce_end(struct request_queue
*q
)
628 spin_lock_irq(q
->queue_lock
);
629 queue_flag_clear(QUEUE_FLAG_ELVSWITCH
, q
);
630 spin_unlock_irq(q
->queue_lock
);
633 void __elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
)
635 trace_block_rq_insert(q
, rq
);
639 if (rq
->cmd_flags
& REQ_SOFTBARRIER
) {
640 /* barriers are scheduling boundary, update end_sector */
641 if (rq
->cmd_type
== REQ_TYPE_FS
||
642 (rq
->cmd_flags
& REQ_DISCARD
)) {
643 q
->end_sector
= rq_end_sector(rq
);
646 } else if (!(rq
->cmd_flags
& REQ_ELVPRIV
) &&
647 (where
== ELEVATOR_INSERT_SORT
||
648 where
== ELEVATOR_INSERT_SORT_MERGE
))
649 where
= ELEVATOR_INSERT_BACK
;
652 case ELEVATOR_INSERT_REQUEUE
:
653 case ELEVATOR_INSERT_FRONT
:
654 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
655 list_add(&rq
->queuelist
, &q
->queue_head
);
658 case ELEVATOR_INSERT_BACK
:
659 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
660 elv_drain_elevator(q
);
661 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
663 * We kick the queue here for the following reasons.
664 * - The elevator might have returned NULL previously
665 * to delay requests and returned them now. As the
666 * queue wasn't empty before this request, ll_rw_blk
667 * won't run the queue on return, resulting in hang.
668 * - Usually, back inserted requests won't be merged
669 * with anything. There's no point in delaying queue
675 case ELEVATOR_INSERT_SORT_MERGE
:
677 * If we succeed in merging this request with one in the
678 * queue already, we are done - rq has now been freed,
679 * so no need to do anything further.
681 if (elv_attempt_insert_merge(q
, rq
))
683 case ELEVATOR_INSERT_SORT
:
684 BUG_ON(rq
->cmd_type
!= REQ_TYPE_FS
&&
685 !(rq
->cmd_flags
& REQ_DISCARD
));
686 rq
->cmd_flags
|= REQ_SORTED
;
688 if (rq_mergeable(rq
)) {
689 elv_rqhash_add(q
, rq
);
695 * Some ioscheds (cfq) run q->request_fn directly, so
696 * rq cannot be accessed after calling
697 * elevator_add_req_fn.
699 q
->elevator
->type
->ops
.elevator_add_req_fn(q
, rq
);
702 case ELEVATOR_INSERT_FLUSH
:
703 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
704 blk_insert_flush(rq
);
707 printk(KERN_ERR
"%s: bad insertion point %d\n",
712 EXPORT_SYMBOL(__elv_add_request
);
714 void elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
)
718 spin_lock_irqsave(q
->queue_lock
, flags
);
719 __elv_add_request(q
, rq
, where
);
720 spin_unlock_irqrestore(q
->queue_lock
, flags
);
722 EXPORT_SYMBOL(elv_add_request
);
724 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
726 struct elevator_queue
*e
= q
->elevator
;
728 if (e
->type
->ops
.elevator_latter_req_fn
)
729 return e
->type
->ops
.elevator_latter_req_fn(q
, rq
);
733 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
735 struct elevator_queue
*e
= q
->elevator
;
737 if (e
->type
->ops
.elevator_former_req_fn
)
738 return e
->type
->ops
.elevator_former_req_fn(q
, rq
);
742 int elv_set_request(struct request_queue
*q
, struct request
*rq
, gfp_t gfp_mask
)
744 struct elevator_queue
*e
= q
->elevator
;
746 if (e
->type
->ops
.elevator_set_req_fn
)
747 return e
->type
->ops
.elevator_set_req_fn(q
, rq
, gfp_mask
);
751 void elv_put_request(struct request_queue
*q
, struct request
*rq
)
753 struct elevator_queue
*e
= q
->elevator
;
755 if (e
->type
->ops
.elevator_put_req_fn
)
756 e
->type
->ops
.elevator_put_req_fn(rq
);
759 int elv_may_queue(struct request_queue
*q
, int rw
)
761 struct elevator_queue
*e
= q
->elevator
;
763 if (e
->type
->ops
.elevator_may_queue_fn
)
764 return e
->type
->ops
.elevator_may_queue_fn(q
, rw
);
766 return ELV_MQUEUE_MAY
;
769 void elv_abort_queue(struct request_queue
*q
)
773 blk_abort_flushes(q
);
775 while (!list_empty(&q
->queue_head
)) {
776 rq
= list_entry_rq(q
->queue_head
.next
);
777 rq
->cmd_flags
|= REQ_QUIET
;
778 trace_block_rq_abort(q
, rq
);
780 * Mark this request as started so we don't trigger
781 * any debug logic in the end I/O path.
783 blk_start_request(rq
);
784 __blk_end_request_all(rq
, -EIO
);
787 EXPORT_SYMBOL(elv_abort_queue
);
789 void elv_completed_request(struct request_queue
*q
, struct request
*rq
)
791 struct elevator_queue
*e
= q
->elevator
;
794 * request is released from the driver, io must be done
796 if (blk_account_rq(rq
)) {
797 q
->in_flight
[rq_is_sync(rq
)]--;
798 if ((rq
->cmd_flags
& REQ_SORTED
) &&
799 e
->type
->ops
.elevator_completed_req_fn
)
800 e
->type
->ops
.elevator_completed_req_fn(q
, rq
);
804 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
807 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
809 struct elv_fs_entry
*entry
= to_elv(attr
);
810 struct elevator_queue
*e
;
816 e
= container_of(kobj
, struct elevator_queue
, kobj
);
817 mutex_lock(&e
->sysfs_lock
);
818 error
= e
->type
? entry
->show(e
, page
) : -ENOENT
;
819 mutex_unlock(&e
->sysfs_lock
);
824 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
825 const char *page
, size_t length
)
827 struct elv_fs_entry
*entry
= to_elv(attr
);
828 struct elevator_queue
*e
;
834 e
= container_of(kobj
, struct elevator_queue
, kobj
);
835 mutex_lock(&e
->sysfs_lock
);
836 error
= e
->type
? entry
->store(e
, page
, length
) : -ENOENT
;
837 mutex_unlock(&e
->sysfs_lock
);
841 static const struct sysfs_ops elv_sysfs_ops
= {
842 .show
= elv_attr_show
,
843 .store
= elv_attr_store
,
846 static struct kobj_type elv_ktype
= {
847 .sysfs_ops
= &elv_sysfs_ops
,
848 .release
= elevator_release
,
851 int __elv_register_queue(struct request_queue
*q
, struct elevator_queue
*e
)
855 error
= kobject_add(&e
->kobj
, &q
->kobj
, "%s", "iosched");
857 struct elv_fs_entry
*attr
= e
->type
->elevator_attrs
;
859 while (attr
->attr
.name
) {
860 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
865 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
871 int elv_register_queue(struct request_queue
*q
)
873 return __elv_register_queue(q
, q
->elevator
);
875 EXPORT_SYMBOL(elv_register_queue
);
877 void elv_unregister_queue(struct request_queue
*q
)
880 struct elevator_queue
*e
= q
->elevator
;
882 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
883 kobject_del(&e
->kobj
);
887 EXPORT_SYMBOL(elv_unregister_queue
);
889 int elv_register(struct elevator_type
*e
)
893 /* create icq_cache if requested */
895 if (WARN_ON(e
->icq_size
< sizeof(struct io_cq
)) ||
896 WARN_ON(e
->icq_align
< __alignof__(struct io_cq
)))
899 snprintf(e
->icq_cache_name
, sizeof(e
->icq_cache_name
),
900 "%s_io_cq", e
->elevator_name
);
901 e
->icq_cache
= kmem_cache_create(e
->icq_cache_name
, e
->icq_size
,
902 e
->icq_align
, 0, NULL
);
907 /* register, don't allow duplicate names */
908 spin_lock(&elv_list_lock
);
909 if (elevator_find(e
->elevator_name
)) {
910 spin_unlock(&elv_list_lock
);
912 kmem_cache_destroy(e
->icq_cache
);
915 list_add_tail(&e
->list
, &elv_list
);
916 spin_unlock(&elv_list_lock
);
918 /* print pretty message */
919 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
920 (!*chosen_elevator
&&
921 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
924 printk(KERN_INFO
"io scheduler %s registered%s\n", e
->elevator_name
,
928 EXPORT_SYMBOL_GPL(elv_register
);
930 void elv_unregister(struct elevator_type
*e
)
933 spin_lock(&elv_list_lock
);
934 list_del_init(&e
->list
);
935 spin_unlock(&elv_list_lock
);
938 * Destroy icq_cache if it exists. icq's are RCU managed. Make
939 * sure all RCU operations are complete before proceeding.
943 kmem_cache_destroy(e
->icq_cache
);
947 EXPORT_SYMBOL_GPL(elv_unregister
);
950 * switch to new_e io scheduler. be careful not to introduce deadlocks -
951 * we don't free the old io scheduler, before we have allocated what we
952 * need for the new one. this way we have a chance of going back to the old
953 * one, if the new one fails init for some reason.
955 static int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
957 struct elevator_queue
*old_elevator
, *e
;
960 /* allocate new elevator */
961 e
= elevator_alloc(q
, new_e
);
965 err
= elevator_init_queue(q
, e
);
967 kobject_put(&e
->kobj
);
971 /* turn on BYPASS and drain all requests w/ elevator private data */
972 elv_quiesce_start(q
);
974 /* unregister old queue, register new one and kill old elevator */
975 if (q
->elevator
->registered
) {
976 elv_unregister_queue(q
);
977 err
= __elv_register_queue(q
, e
);
982 /* done, clear io_cq's, switch elevators and turn off BYPASS */
983 spin_lock_irq(q
->queue_lock
);
985 old_elevator
= q
->elevator
;
987 spin_unlock_irq(q
->queue_lock
);
989 elevator_exit(old_elevator
);
992 blk_add_trace_msg(q
, "elv switch: %s", e
->type
->elevator_name
);
998 * switch failed, exit the new io scheduler and reattach the old
999 * one again (along with re-adding the sysfs dir)
1002 elv_register_queue(q
);
1009 * Switch this queue to the given IO scheduler.
1011 int elevator_change(struct request_queue
*q
, const char *name
)
1013 char elevator_name
[ELV_NAME_MAX
];
1014 struct elevator_type
*e
;
1019 strlcpy(elevator_name
, name
, sizeof(elevator_name
));
1020 e
= elevator_get(strstrip(elevator_name
));
1022 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
1026 if (!strcmp(elevator_name
, q
->elevator
->type
->elevator_name
)) {
1031 return elevator_switch(q
, e
);
1033 EXPORT_SYMBOL(elevator_change
);
1035 ssize_t
elv_iosched_store(struct request_queue
*q
, const char *name
,
1043 ret
= elevator_change(q
, name
);
1047 printk(KERN_ERR
"elevator: switch to %s failed\n", name
);
1051 ssize_t
elv_iosched_show(struct request_queue
*q
, char *name
)
1053 struct elevator_queue
*e
= q
->elevator
;
1054 struct elevator_type
*elv
;
1055 struct elevator_type
*__e
;
1058 if (!q
->elevator
|| !blk_queue_stackable(q
))
1059 return sprintf(name
, "none\n");
1063 spin_lock(&elv_list_lock
);
1064 list_for_each_entry(__e
, &elv_list
, list
) {
1065 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
1066 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
1068 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1070 spin_unlock(&elv_list_lock
);
1072 len
+= sprintf(len
+name
, "\n");
1076 struct request
*elv_rb_former_request(struct request_queue
*q
,
1079 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
1082 return rb_entry_rq(rbprev
);
1086 EXPORT_SYMBOL(elv_rb_former_request
);
1088 struct request
*elv_rb_latter_request(struct request_queue
*q
,
1091 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
1094 return rb_entry_rq(rbnext
);
1098 EXPORT_SYMBOL(elv_rb_latter_request
);