jbd2: fix BH_JWrite setting in checkpointing code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / elevator.c
blob91e18f8af9becaace380cc687d279eb1f47cb79a
1 /*
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
12 * an existing request
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
19 * Jens:
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>
26 #include <linux/fs.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>
40 #include "blk.h"
42 static DEFINE_SPINLOCK(elv_list_lock);
43 static LIST_HEAD(elv_list);
46 * Merge hash stuff.
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
57 * merged with rq.
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);
67 return 1;
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))
76 return 0;
79 * Don't merge file system requests and discard requests
81 if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
82 return 0;
85 * Don't merge discard requests and secure discard requests
87 if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
88 return 0;
91 * different data direction or already started, don't merge
93 if (bio_data_dir(bio) != rq_data_dir(rq))
94 return 0;
97 * must be same device and not a special request
99 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
100 return 0;
103 * only merge integrity protected bio into ditto rq
105 if (bio_integrity(bio) != blk_integrity_rq(rq))
106 return 0;
108 if (!elv_iosched_allow_merge(rq, bio))
109 return 0;
111 return 1;
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;
129 return ret;
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))
138 return e;
141 return NULL;
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);
156 if (!e) {
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))
164 e = NULL;
166 spin_unlock(&elv_list_lock);
168 return e;
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)
176 return 0;
177 return -ENOMEM;
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);
189 return 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;
200 int i;
202 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
203 if (unlikely(!eq))
204 goto err;
206 eq->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);
212 if (!eq->hash)
213 goto err;
215 for (i = 0; i < ELV_HASH_ENTRIES; i++)
216 INIT_HLIST_HEAD(&eq->hash[i]);
218 return eq;
219 err:
220 kfree(eq);
221 elevator_put(e);
222 return NULL;
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);
231 kfree(e->hash);
232 kfree(e);
235 int elevator_init(struct request_queue *q, char *name)
237 struct elevator_type *e = NULL;
238 struct elevator_queue *eq;
239 int err;
241 if (unlikely(q->elevator))
242 return 0;
244 INIT_LIST_HEAD(&q->queue_head);
245 q->last_merge = NULL;
246 q->end_sector = 0;
247 q->boundary_rq = NULL;
249 if (name) {
250 e = elevator_get(name);
251 if (!e)
252 return -EINVAL;
255 if (!e && *chosen_elevator) {
256 e = elevator_get(chosen_elevator);
257 if (!e)
258 printk(KERN_ERR "I/O scheduler %s not found\n",
259 chosen_elevator);
262 if (!e) {
263 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
264 if (!e) {
265 printk(KERN_ERR
266 "Default I/O scheduler not found. " \
267 "Using noop.\n");
268 e = elevator_get("noop");
272 eq = elevator_alloc(q, e);
273 if (!eq)
274 return -ENOMEM;
276 err = elevator_init_queue(q, eq);
277 if (err) {
278 kobject_put(&eq->kobj);
279 return err;
282 q->elevator = eq;
283 return 0;
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)
305 if (ELV_ON_HASH(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;
328 struct request *rq;
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);
335 continue;
338 if (rq_hash_key(rq) == offset)
339 return rq;
342 return NULL;
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;
355 while (*p) {
356 parent = *p;
357 __rq = rb_entry(parent, struct request, rb_node);
359 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
360 p = &(*p)->rb_left;
361 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
362 p = &(*p)->rb_right;
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;
381 struct request *rq;
383 while (n) {
384 rq = rb_entry(n, struct request, rb_node);
386 if (sector < blk_rq_pos(rq))
387 n = n->rb_left;
388 else if (sector > blk_rq_pos(rq))
389 n = n->rb_right;
390 else
391 return rq;
394 return NULL;
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)
405 sector_t boundary;
406 struct list_head *entry;
407 int stop_flags;
409 if (q->last_merge == rq)
410 q->last_merge = NULL;
412 elv_rqhash_del(q, rq);
414 q->nr_sorted--;
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))
423 break;
424 if (rq_data_dir(rq) != rq_data_dir(pos))
425 break;
426 if (pos->cmd_flags & stop_flags)
427 break;
428 if (blk_rq_pos(rq) >= boundary) {
429 if (blk_rq_pos(pos) < boundary)
430 continue;
431 } else {
432 if (blk_rq_pos(pos) >= boundary)
433 break;
435 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
436 break;
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);
455 q->nr_sorted--;
457 q->end_sector = rq_end_sector(rq);
458 q->boundary_rq = 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;
467 int ret;
470 * Levels of merges:
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.
481 if (q->last_merge) {
482 ret = elv_try_merge(q->last_merge, bio);
483 if (ret != ELEVATOR_NO_MERGE) {
484 *req = q->last_merge;
485 return ret;
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)) {
497 *req = __rq;
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
510 * afterwards.
512 * Returns true if we merged, false otherwise
514 static bool elv_attempt_insert_merge(struct request_queue *q,
515 struct request *rq)
517 struct request *__rq;
519 if (blk_queue_nomerges(q))
520 return false;
523 * First try one-hit cache.
525 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
526 return true;
528 if (blk_queue_noxmerges(q))
529 return false;
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))
536 return true;
538 return false;
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);
551 q->last_merge = 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);
565 if (next_sorted) {
566 elv_rqhash_del(q, next);
567 q->nr_sorted--;
570 q->last_merge = rq;
573 void elv_bio_merged(struct request_queue *q, struct request *rq,
574 struct bio *bio)
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)
601 static int printed;
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)
616 if (!q->elevator)
617 return;
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);
637 rq->q = q;
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);
644 q->boundary_rq = 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;
651 switch (where) {
652 case ELEVATOR_INSERT_REQUEUE:
653 case ELEVATOR_INSERT_FRONT:
654 rq->cmd_flags |= REQ_SOFTBARRIER;
655 list_add(&rq->queuelist, &q->queue_head);
656 break;
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
670 * processing.
672 __blk_run_queue(q);
673 break;
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))
682 break;
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;
687 q->nr_sorted++;
688 if (rq_mergeable(rq)) {
689 elv_rqhash_add(q, rq);
690 if (!q->last_merge)
691 q->last_merge = 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);
700 break;
702 case ELEVATOR_INSERT_FLUSH:
703 rq->cmd_flags |= REQ_SOFTBARRIER;
704 blk_insert_flush(rq);
705 break;
706 default:
707 printk(KERN_ERR "%s: bad insertion point %d\n",
708 __func__, where);
709 BUG();
712 EXPORT_SYMBOL(__elv_add_request);
714 void elv_add_request(struct request_queue *q, struct request *rq, int where)
716 unsigned long flags;
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);
730 return NULL;
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);
739 return NULL;
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);
748 return 0;
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)
771 struct request *rq;
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)
806 static ssize_t
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;
811 ssize_t error;
813 if (!entry->show)
814 return -EIO;
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);
820 return error;
823 static ssize_t
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;
829 ssize_t error;
831 if (!entry->store)
832 return -EIO;
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);
838 return error;
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)
853 int error;
855 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
856 if (!error) {
857 struct elv_fs_entry *attr = e->type->elevator_attrs;
858 if (attr) {
859 while (attr->attr.name) {
860 if (sysfs_create_file(&e->kobj, &attr->attr))
861 break;
862 attr++;
865 kobject_uevent(&e->kobj, KOBJ_ADD);
866 e->registered = 1;
868 return error;
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)
879 if (q) {
880 struct elevator_queue *e = q->elevator;
882 kobject_uevent(&e->kobj, KOBJ_REMOVE);
883 kobject_del(&e->kobj);
884 e->registered = 0;
887 EXPORT_SYMBOL(elv_unregister_queue);
889 int elv_register(struct elevator_type *e)
891 char *def = "";
893 /* create icq_cache if requested */
894 if (e->icq_size) {
895 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
896 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
897 return -EINVAL;
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);
903 if (!e->icq_cache)
904 return -ENOMEM;
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);
911 if (e->icq_cache)
912 kmem_cache_destroy(e->icq_cache);
913 return -EBUSY;
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)))
922 def = " (default)";
924 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
925 def);
926 return 0;
928 EXPORT_SYMBOL_GPL(elv_register);
930 void elv_unregister(struct elevator_type *e)
932 /* unregister */
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.
941 if (e->icq_cache) {
942 rcu_barrier();
943 kmem_cache_destroy(e->icq_cache);
944 e->icq_cache = NULL;
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;
958 int err;
960 /* allocate new elevator */
961 e = elevator_alloc(q, new_e);
962 if (!e)
963 return -ENOMEM;
965 err = elevator_init_queue(q, e);
966 if (err) {
967 kobject_put(&e->kobj);
968 return err;
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);
978 if (err)
979 goto fail_register;
982 /* done, clear io_cq's, switch elevators and turn off BYPASS */
983 spin_lock_irq(q->queue_lock);
984 ioc_clear_queue(q);
985 old_elevator = q->elevator;
986 q->elevator = e;
987 spin_unlock_irq(q->queue_lock);
989 elevator_exit(old_elevator);
990 elv_quiesce_end(q);
992 blk_add_trace_msg(q, "elv switch: %s", e->type->elevator_name);
994 return 0;
996 fail_register:
998 * switch failed, exit the new io scheduler and reattach the old
999 * one again (along with re-adding the sysfs dir)
1001 elevator_exit(e);
1002 elv_register_queue(q);
1003 elv_quiesce_end(q);
1005 return err;
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;
1016 if (!q->elevator)
1017 return -ENXIO;
1019 strlcpy(elevator_name, name, sizeof(elevator_name));
1020 e = elevator_get(strstrip(elevator_name));
1021 if (!e) {
1022 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1023 return -EINVAL;
1026 if (!strcmp(elevator_name, q->elevator->type->elevator_name)) {
1027 elevator_put(e);
1028 return 0;
1031 return elevator_switch(q, e);
1033 EXPORT_SYMBOL(elevator_change);
1035 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1036 size_t count)
1038 int ret;
1040 if (!q->elevator)
1041 return count;
1043 ret = elevator_change(q, name);
1044 if (!ret)
1045 return count;
1047 printk(KERN_ERR "elevator: switch to %s failed\n", name);
1048 return ret;
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;
1056 int len = 0;
1058 if (!q->elevator || !blk_queue_stackable(q))
1059 return sprintf(name, "none\n");
1061 elv = e->type;
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);
1067 else
1068 len += sprintf(name+len, "%s ", __e->elevator_name);
1070 spin_unlock(&elv_list_lock);
1072 len += sprintf(len+name, "\n");
1073 return len;
1076 struct request *elv_rb_former_request(struct request_queue *q,
1077 struct request *rq)
1079 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1081 if (rbprev)
1082 return rb_entry_rq(rbprev);
1084 return NULL;
1086 EXPORT_SYMBOL(elv_rb_former_request);
1088 struct request *elv_rb_latter_request(struct request_queue *q,
1089 struct request *rq)
1091 struct rb_node *rbnext = rb_next(&rq->rb_node);
1093 if (rbnext)
1094 return rb_entry_rq(rbnext);
1096 return NULL;
1098 EXPORT_SYMBOL(elv_rb_latter_request);