25 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
33 #include <linux/compiler.h>
34 #include <linux/blktrace_api.h>
35 #include <linux/hash.h>
49 static const int elv_hash_shift = 6;
50 #define ELV_HASH_BLOCK(sec) ((sec) >> 3)
51 #define ELV_HASH_FN(sec) \
52 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
53 #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
54 #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
60 static int elv_iosched_allow_merge(
struct request *
rq,
struct bio *bio)
63 struct elevator_queue *
e = q->elevator;
65 if (e->type->ops.elevator_allow_merge_fn)
66 return e->type->ops.elevator_allow_merge_fn(q, rq, bio);
79 if (!elv_iosched_allow_merge(rq, bio))
86 static struct elevator_type *elevator_find(
const char *
name)
88 struct elevator_type *
e;
91 if (!
strcmp(e->elevator_name, name))
98 static void elevator_put(
struct elevator_type *e)
100 module_put(e->elevator_owner);
103 static struct elevator_type *elevator_get(
const char *name)
105 struct elevator_type *
e;
107 spin_lock(&elv_list_lock);
109 e = elevator_find(name);
111 spin_unlock(&elv_list_lock);
112 request_module(
"%s-iosched", name);
113 spin_lock(&elv_list_lock);
114 e = elevator_find(name);
117 if (e && !try_module_get(e->elevator_owner))
120 spin_unlock(&elv_list_lock);
125 static char chosen_elevator[ELV_NAME_MAX];
127 static int __init elevator_setup(
char *
str)
133 strncpy(chosen_elevator, str,
sizeof(chosen_elevator) - 1);
137 __setup(
"elevator=", elevator_setup);
141 static struct elevator_queue *elevator_alloc(
struct request_queue *
q,
142 struct elevator_type *e)
144 struct elevator_queue *eq;
170 static void elevator_release(
struct kobject *kobj)
172 struct elevator_queue *
e;
175 elevator_put(e->type);
182 struct elevator_type *e =
NULL;
188 INIT_LIST_HEAD(&q->queue_head);
189 q->last_merge =
NULL;
191 q->boundary_rq =
NULL;
194 e = elevator_get(name);
199 if (!e && *chosen_elevator) {
200 e = elevator_get(chosen_elevator);
207 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
210 "Default I/O scheduler not found. " \
212 e = elevator_get(
"noop");
216 q->elevator = elevator_alloc(q, e);
220 err = e->ops.elevator_init_fn(q);
233 if (e->type->ops.elevator_exit_fn)
234 e->type->ops.elevator_exit_fn(e);
241 static inline void __elv_rqhash_del(
struct request *
rq)
243 hlist_del_init(&rq->hash);
249 __elv_rqhash_del(rq);
254 struct elevator_queue *e = q->elevator;
262 __elv_rqhash_del(rq);
263 elv_rqhash_add(q, rq);
268 struct elevator_queue *e = q->elevator;
277 __elv_rqhash_del(rq);
302 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
304 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
308 rb_link_node(&rq->rb_node, parent, p);
329 if (sector < blk_rq_pos(rq))
331 else if (sector > blk_rq_pos(rq))
352 if (q->last_merge == rq)
353 q->last_merge =
NULL;
355 elv_rqhash_del(q, rq);
359 boundary = q->end_sector;
367 if (rq_data_dir(rq) != rq_data_dir(pos))
369 if (pos->cmd_flags & stop_flags)
371 if (blk_rq_pos(rq) >= boundary) {
372 if (blk_rq_pos(pos) < boundary)
375 if (blk_rq_pos(pos) >= boundary)
378 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
382 list_add(&rq->queuelist, entry);
393 if (q->last_merge == rq)
394 q->last_merge =
NULL;
396 elv_rqhash_del(q, rq);
400 q->end_sector = rq_end_sector(rq);
408 struct elevator_queue *e = q->elevator;
418 if (blk_queue_nomerges(q))
419 return ELEVATOR_NO_MERGE;
426 if (ret != ELEVATOR_NO_MERGE) {
427 *req = q->last_merge;
432 if (blk_queue_noxmerges(q))
433 return ELEVATOR_NO_MERGE;
438 __rq = elv_rqhash_find(q, bio->bi_sector);
441 return ELEVATOR_BACK_MERGE;
444 if (e->type->ops.elevator_merge_fn)
445 return e->type->ops.elevator_merge_fn(q, req, bio);
447 return ELEVATOR_NO_MERGE;
457 static bool elv_attempt_insert_merge(
struct request_queue *q,
462 if (blk_queue_nomerges(q))
471 if (blk_queue_noxmerges(q))
477 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
486 struct elevator_queue *e = q->elevator;
488 if (e->type->ops.elevator_merged_fn)
489 e->type->ops.elevator_merged_fn(q, rq, type);
491 if (type == ELEVATOR_BACK_MERGE)
492 elv_rqhash_reposition(q, rq);
500 struct elevator_queue *e = q->elevator;
501 const int next_sorted = next->cmd_flags &
REQ_SORTED;
503 if (next_sorted && e->type->ops.elevator_merge_req_fn)
504 e->type->ops.elevator_merge_req_fn(q, rq, next);
506 elv_rqhash_reposition(q, rq);
509 elv_rqhash_del(q, next);
519 struct elevator_queue *e = q->elevator;
521 if (e->type->ops.elevator_bio_merged_fn)
522 e->type->ops.elevator_bio_merged_fn(q, rq, bio);
531 if (blk_account_rq(rq)) {
532 q->in_flight[rq_is_sync(rq)]--;
534 elv_deactivate_rq(q, rq);
548 while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
550 if (q->nr_sorted && printed++ < 10) {
552 "(nr_sorted=%u), please report this\n",
553 q->elevator->type->elevator_name, q->nr_sorted);
559 trace_block_rq_insert(q, rq);
565 if (rq->cmd_type == REQ_TYPE_FS) {
566 q->end_sector = rq_end_sector(rq);
570 (where == ELEVATOR_INSERT_SORT ||
571 where == ELEVATOR_INSERT_SORT_MERGE))
572 where = ELEVATOR_INSERT_BACK;
575 case ELEVATOR_INSERT_REQUEUE:
576 case ELEVATOR_INSERT_FRONT:
578 list_add(&rq->queuelist, &q->queue_head);
581 case ELEVATOR_INSERT_BACK:
598 case ELEVATOR_INSERT_SORT_MERGE:
604 if (elv_attempt_insert_merge(q, rq))
606 case ELEVATOR_INSERT_SORT:
607 BUG_ON(rq->cmd_type != REQ_TYPE_FS);
610 if (rq_mergeable(rq)) {
611 elv_rqhash_add(q, rq);
621 q->elevator->type->ops.elevator_add_req_fn(q, rq);
624 case ELEVATOR_INSERT_FLUSH:
642 spin_unlock_irqrestore(q->queue_lock, flags);
648 struct elevator_queue *e = q->elevator;
650 if (e->type->ops.elevator_latter_req_fn)
651 return e->type->ops.elevator_latter_req_fn(q, rq);
657 struct elevator_queue *e = q->elevator;
659 if (e->type->ops.elevator_former_req_fn)
660 return e->type->ops.elevator_former_req_fn(q, rq);
667 struct elevator_queue *e = q->elevator;
669 if (e->type->ops.elevator_set_req_fn)
670 return e->type->ops.elevator_set_req_fn(q, rq, bio, gfp_mask);
676 struct elevator_queue *e = q->elevator;
678 if (e->type->ops.elevator_put_req_fn)
679 e->type->ops.elevator_put_req_fn(rq);
684 struct elevator_queue *e = q->elevator;
686 if (e->type->ops.elevator_may_queue_fn)
687 return e->type->ops.elevator_may_queue_fn(q, rw);
689 return ELV_MQUEUE_MAY;
698 while (!list_empty(&q->queue_head)) {
699 rq = list_entry_rq(q->queue_head.next);
701 trace_block_rq_abort(q, rq);
714 struct elevator_queue *e = q->elevator;
719 if (blk_account_rq(rq)) {
720 q->in_flight[rq_is_sync(rq)]--;
722 e->type->ops.elevator_completed_req_fn)
723 e->type->ops.elevator_completed_req_fn(q, rq);
727 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
732 struct elv_fs_entry *entry =
to_elv(attr);
733 struct elevator_queue *
e;
741 error = e->type ? entry->show(e, page) : -
ENOENT;
750 struct elv_fs_entry *entry =
to_elv(attr);
751 struct elevator_queue *
e;
759 error = e->type ? entry->store(e, page, length) : -
ENOENT;
764 static const struct sysfs_ops elv_sysfs_ops = {
765 .show = elv_attr_show,
766 .store = elv_attr_store,
770 .sysfs_ops = &elv_sysfs_ops,
771 .release = elevator_release,
776 struct elevator_queue *e = q->elevator;
779 error =
kobject_add(&e->kobj, &q->kobj,
"%s",
"iosched");
781 struct elv_fs_entry *attr = e->type->elevator_attrs;
783 while (attr->attr.name) {
799 struct elevator_queue *e = q->elevator;
818 snprintf(e->icq_cache_name,
sizeof(e->icq_cache_name),
819 "%s_io_cq", e->elevator_name);
821 e->icq_align, 0,
NULL);
827 spin_lock(&elv_list_lock);
828 if (elevator_find(e->elevator_name)) {
829 spin_unlock(&elv_list_lock);
835 spin_unlock(&elv_list_lock);
838 if (!
strcmp(e->elevator_name, chosen_elevator) ||
839 (!*chosen_elevator &&
840 !
strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
852 spin_lock(&elv_list_lock);
853 list_del_init(&e->list);
854 spin_unlock(&elv_list_lock);
874 static int elevator_switch(
struct request_queue *q,
struct elevator_type *new_e)
876 struct elevator_queue *old = q->elevator;
877 bool registered = old->registered;
893 spin_lock_irq(q->queue_lock);
895 spin_unlock_irq(q->queue_lock);
899 q->elevator = elevator_alloc(q, new_e);
903 err = new_e->ops.elevator_init_fn(q);
939 char elevator_name[ELV_NAME_MAX];
940 struct elevator_type *
e;
945 strlcpy(elevator_name, name,
sizeof(elevator_name));
946 e = elevator_get(strstrip(elevator_name));
952 if (!
strcmp(elevator_name, q->elevator->type->elevator_name)) {
957 return elevator_switch(q, e);
979 struct elevator_queue *e = q->elevator;
980 struct elevator_type *elv;
981 struct elevator_type *__e;
984 if (!q->elevator || !blk_queue_stackable(q))
985 return sprintf(name,
"none\n");
989 spin_lock(&elv_list_lock);
991 if (!
strcmp(elv->elevator_name, __e->elevator_name))
992 len +=
sprintf(name+len,
"[%s] ", elv->elevator_name);
994 len +=
sprintf(name+len,
"%s ", __e->elevator_name);
996 spin_unlock(&elv_list_lock);
998 len +=
sprintf(len+name,
"\n");
1008 return rb_entry_rq(rbprev);
1020 return rb_entry_rq(rbnext);