[ARM] S3C24XX: s3c243xx_clock_register() data can be __initdata
[linux-2.6/zen-sources.git] / block / elevator.c
blob902dd1344d56dfb9000ba5701dbb250bcf835d27
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/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);
44 * Merge hash stuff.
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
56 * merged with rq.
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);
66 return 1;
70 * can we safely merge with this request?
72 int elv_rq_merge_ok(struct request *rq, struct bio *bio)
74 if (!rq_mergeable(rq))
75 return 0;
78 * different data direction or already started, don't merge
80 if (bio_data_dir(bio) != rq_data_dir(rq))
81 return 0;
84 * must be same device and not a special request
86 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
87 return 0;
89 if (!elv_iosched_allow_merge(rq, bio))
90 return 0;
92 return 1;
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;
110 return ret;
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))
119 return e;
122 return NULL;
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 if (!e) {
138 char elv[ELV_NAME_MAX + strlen("-iosched")];
140 spin_unlock(&elv_list_lock);
142 if (!strcmp(name, "anticipatory"))
143 sprintf(elv, "as-iosched");
144 else
145 sprintf(elv, "%s-iosched", name);
147 request_module(elv);
148 spin_lock(&elv_list_lock);
149 e = elevator_find(name);
152 if (e && !try_module_get(e->elevator_owner))
153 e = NULL;
155 spin_unlock(&elv_list_lock);
157 return e;
160 static void *elevator_init_queue(struct request_queue *q,
161 struct elevator_queue *eq)
163 return eq->ops->elevator_init_fn(q);
166 static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
167 void *data)
169 q->elevator = eq;
170 eq->elevator_data = data;
173 static char chosen_elevator[16];
175 static int __init elevator_setup(char *str)
178 * Be backwards-compatible with previous kernels, so users
179 * won't get the wrong elevator.
181 if (!strcmp(str, "as"))
182 strcpy(chosen_elevator, "anticipatory");
183 else
184 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
185 return 1;
188 __setup("elevator=", elevator_setup);
190 static struct kobj_type elv_ktype;
192 static elevator_t *elevator_alloc(struct request_queue *q,
193 struct elevator_type *e)
195 elevator_t *eq;
196 int i;
198 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
199 if (unlikely(!eq))
200 goto err;
202 eq->ops = &e->ops;
203 eq->elevator_type = e;
204 kobject_init(&eq->kobj, &elv_ktype);
205 mutex_init(&eq->sysfs_lock);
207 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
208 GFP_KERNEL, q->node);
209 if (!eq->hash)
210 goto err;
212 for (i = 0; i < ELV_HASH_ENTRIES; i++)
213 INIT_HLIST_HEAD(&eq->hash[i]);
215 return eq;
216 err:
217 kfree(eq);
218 elevator_put(e);
219 return NULL;
222 static void elevator_release(struct kobject *kobj)
224 elevator_t *e = container_of(kobj, elevator_t, kobj);
226 elevator_put(e->elevator_type);
227 kfree(e->hash);
228 kfree(e);
231 int elevator_init(struct request_queue *q, char *name)
233 struct elevator_type *e = NULL;
234 struct elevator_queue *eq;
235 int ret = 0;
236 void *data;
238 INIT_LIST_HEAD(&q->queue_head);
239 q->last_merge = NULL;
240 q->end_sector = 0;
241 q->boundary_rq = NULL;
243 if (name) {
244 e = elevator_get(name);
245 if (!e)
246 return -EINVAL;
249 if (!e && *chosen_elevator) {
250 e = elevator_get(chosen_elevator);
251 if (!e)
252 printk(KERN_ERR "I/O scheduler %s not found\n",
253 chosen_elevator);
256 if (!e) {
257 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
258 if (!e) {
259 printk(KERN_ERR
260 "Default I/O scheduler not found. " \
261 "Using noop.\n");
262 e = elevator_get("noop");
266 eq = elevator_alloc(q, e);
267 if (!eq)
268 return -ENOMEM;
270 data = elevator_init_queue(q, eq);
271 if (!data) {
272 kobject_put(&eq->kobj);
273 return -ENOMEM;
276 elevator_attach(q, eq, data);
277 return ret;
279 EXPORT_SYMBOL(elevator_init);
281 void elevator_exit(elevator_t *e)
283 mutex_lock(&e->sysfs_lock);
284 if (e->ops->elevator_exit_fn)
285 e->ops->elevator_exit_fn(e);
286 e->ops = NULL;
287 mutex_unlock(&e->sysfs_lock);
289 kobject_put(&e->kobj);
291 EXPORT_SYMBOL(elevator_exit);
293 static void elv_activate_rq(struct request_queue *q, struct request *rq)
295 elevator_t *e = q->elevator;
297 if (e->ops->elevator_activate_req_fn)
298 e->ops->elevator_activate_req_fn(q, rq);
301 static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
303 elevator_t *e = q->elevator;
305 if (e->ops->elevator_deactivate_req_fn)
306 e->ops->elevator_deactivate_req_fn(q, rq);
309 static inline void __elv_rqhash_del(struct request *rq)
311 hlist_del_init(&rq->hash);
314 static void elv_rqhash_del(struct request_queue *q, struct request *rq)
316 if (ELV_ON_HASH(rq))
317 __elv_rqhash_del(rq);
320 static void elv_rqhash_add(struct request_queue *q, struct request *rq)
322 elevator_t *e = q->elevator;
324 BUG_ON(ELV_ON_HASH(rq));
325 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
328 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
330 __elv_rqhash_del(rq);
331 elv_rqhash_add(q, rq);
334 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
336 elevator_t *e = q->elevator;
337 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
338 struct hlist_node *entry, *next;
339 struct request *rq;
341 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
342 BUG_ON(!ELV_ON_HASH(rq));
344 if (unlikely(!rq_mergeable(rq))) {
345 __elv_rqhash_del(rq);
346 continue;
349 if (rq_hash_key(rq) == offset)
350 return rq;
353 return NULL;
357 * RB-tree support functions for inserting/lookup/removal of requests
358 * in a sorted RB tree.
360 struct request *elv_rb_add(struct rb_root *root, struct request *rq)
362 struct rb_node **p = &root->rb_node;
363 struct rb_node *parent = NULL;
364 struct request *__rq;
366 while (*p) {
367 parent = *p;
368 __rq = rb_entry(parent, struct request, rb_node);
370 if (rq->sector < __rq->sector)
371 p = &(*p)->rb_left;
372 else if (rq->sector > __rq->sector)
373 p = &(*p)->rb_right;
374 else
375 return __rq;
378 rb_link_node(&rq->rb_node, parent, p);
379 rb_insert_color(&rq->rb_node, root);
380 return NULL;
382 EXPORT_SYMBOL(elv_rb_add);
384 void elv_rb_del(struct rb_root *root, struct request *rq)
386 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
387 rb_erase(&rq->rb_node, root);
388 RB_CLEAR_NODE(&rq->rb_node);
390 EXPORT_SYMBOL(elv_rb_del);
392 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
394 struct rb_node *n = root->rb_node;
395 struct request *rq;
397 while (n) {
398 rq = rb_entry(n, struct request, rb_node);
400 if (sector < rq->sector)
401 n = n->rb_left;
402 else if (sector > rq->sector)
403 n = n->rb_right;
404 else
405 return rq;
408 return NULL;
410 EXPORT_SYMBOL(elv_rb_find);
413 * Insert rq into dispatch queue of q. Queue lock must be held on
414 * entry. rq is sort instead into the dispatch queue. To be used by
415 * specific elevators.
417 void elv_dispatch_sort(struct request_queue *q, struct request *rq)
419 sector_t boundary;
420 struct list_head *entry;
421 int stop_flags;
423 if (q->last_merge == rq)
424 q->last_merge = NULL;
426 elv_rqhash_del(q, rq);
428 q->nr_sorted--;
430 boundary = q->end_sector;
431 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
432 list_for_each_prev(entry, &q->queue_head) {
433 struct request *pos = list_entry_rq(entry);
435 if (rq_data_dir(rq) != rq_data_dir(pos))
436 break;
437 if (pos->cmd_flags & stop_flags)
438 break;
439 if (rq->sector >= boundary) {
440 if (pos->sector < boundary)
441 continue;
442 } else {
443 if (pos->sector >= boundary)
444 break;
446 if (rq->sector >= pos->sector)
447 break;
450 list_add(&rq->queuelist, entry);
452 EXPORT_SYMBOL(elv_dispatch_sort);
455 * Insert rq into dispatch queue of q. Queue lock must be held on
456 * entry. rq is added to the back of the dispatch queue. To be used by
457 * specific elevators.
459 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
461 if (q->last_merge == rq)
462 q->last_merge = NULL;
464 elv_rqhash_del(q, rq);
466 q->nr_sorted--;
468 q->end_sector = rq_end_sector(rq);
469 q->boundary_rq = rq;
470 list_add_tail(&rq->queuelist, &q->queue_head);
472 EXPORT_SYMBOL(elv_dispatch_add_tail);
474 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
476 elevator_t *e = q->elevator;
477 struct request *__rq;
478 int ret;
481 * First try one-hit cache.
483 if (q->last_merge) {
484 ret = elv_try_merge(q->last_merge, bio);
485 if (ret != ELEVATOR_NO_MERGE) {
486 *req = q->last_merge;
487 return ret;
491 if (blk_queue_nomerges(q))
492 return ELEVATOR_NO_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)) {
499 *req = __rq;
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);
519 q->last_merge = 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);
533 q->nr_sorted--;
534 q->last_merge = rq;
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)) {
544 q->in_flight--;
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)
556 static int printed;
557 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
559 if (q->nr_sorted == 0)
560 return;
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;
571 unsigned ordseq;
572 int unplug_it = 1;
574 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
576 rq->q = q;
578 switch (where) {
579 case ELEVATOR_INSERT_FRONT:
580 rq->cmd_flags |= REQ_SOFTBARRIER;
582 list_add(&rq->queuelist, &q->queue_head);
583 break;
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
597 * processing.
599 blk_remove_plug(q);
600 q->request_fn(q);
601 break;
603 case ELEVATOR_INSERT_SORT:
604 BUG_ON(!blk_fs_request(rq));
605 rq->cmd_flags |= REQ_SORTED;
606 q->nr_sorted++;
607 if (rq_mergeable(rq)) {
608 elv_rqhash_add(q, rq);
609 if (!q->last_merge)
610 q->last_merge = 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);
619 break;
621 case ELEVATOR_INSERT_REQUEUE:
623 * If ordered flush isn't in progress, we do front
624 * insertion; otherwise, requests should be requeued
625 * in ordseq order.
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.
633 unplug_it = 0;
635 if (q->ordseq == 0) {
636 list_add(&rq->queuelist, &q->queue_head);
637 break;
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))
645 break;
648 list_add_tail(&rq->queuelist, pos);
649 break;
651 default:
652 printk(KERN_ERR "%s: bad insertion point %d\n",
653 __func__, where);
654 BUG();
657 if (unplug_it && blk_queue_plugged(q)) {
658 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
659 - q->in_flight;
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,
667 int plug)
669 if (q->ordcolor)
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))
677 q->ordcolor ^= 1;
680 * barriers implicitly indicate back insertion
682 if (where == ELEVATOR_INSERT_SORT)
683 where = ELEVATOR_INSERT_BACK;
686 * this request is scheduling boundary, update
687 * end_sector
689 if (blk_fs_request(rq)) {
690 q->end_sector = rq_end_sector(rq);
691 q->boundary_rq = rq;
693 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
694 where == ELEVATOR_INSERT_SORT)
695 where = ELEVATOR_INSERT_BACK;
697 if (plug)
698 blk_plug_device(q);
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,
705 int plug)
707 unsigned long flags;
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)
717 struct request *rq;
719 while (1) {
720 while (!list_empty(&q->queue_head)) {
721 rq = list_entry_rq(q->queue_head.next);
722 if (blk_do_ordered(q, &rq))
723 return rq;
726 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
727 return NULL;
731 struct request *elv_next_request(struct request_queue *q)
733 struct request *rq;
734 int ret;
736 while ((rq = __elv_next_request(q)) != NULL) {
738 * Kill the empty barrier place holder, the driver must
739 * not ever see it.
741 if (blk_empty_barrier(rq)) {
742 end_queued_request(rq, 1);
743 continue;
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)
769 break;
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
776 * device can handle
778 rq->nr_phys_segments++;
779 rq->nr_hw_segments++;
782 if (!q->prep_rq_fn)
783 break;
785 ret = q->prep_rq_fn(q, rq);
786 if (ret == BLKPREP_OK) {
787 break;
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;
805 rq = NULL;
806 break;
807 } else if (ret == BLKPREP_KILL) {
808 rq->cmd_flags |= REQ_QUIET;
809 end_queued_request(rq, 0);
810 } else {
811 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
812 break;
816 return rq;
818 EXPORT_SYMBOL(elv_next_request);
820 void elv_dequeue_request(struct request_queue *q, struct request *rq)
822 BUG_ON(list_empty(&rq->queuelist));
823 BUG_ON(ELV_ON_HASH(rq));
825 list_del_init(&rq->queuelist);
828 * the time frame between a request being removed from the lists
829 * and to it is freed is accounted as io that is in progress at
830 * the driver side.
832 if (blk_account_rq(rq))
833 q->in_flight++;
835 EXPORT_SYMBOL(elv_dequeue_request);
837 int elv_queue_empty(struct request_queue *q)
839 elevator_t *e = q->elevator;
841 if (!list_empty(&q->queue_head))
842 return 0;
844 if (e->ops->elevator_queue_empty_fn)
845 return e->ops->elevator_queue_empty_fn(q);
847 return 1;
849 EXPORT_SYMBOL(elv_queue_empty);
851 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
853 elevator_t *e = q->elevator;
855 if (e->ops->elevator_latter_req_fn)
856 return e->ops->elevator_latter_req_fn(q, rq);
857 return NULL;
860 struct request *elv_former_request(struct request_queue *q, struct request *rq)
862 elevator_t *e = q->elevator;
864 if (e->ops->elevator_former_req_fn)
865 return e->ops->elevator_former_req_fn(q, rq);
866 return NULL;
869 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
871 elevator_t *e = q->elevator;
873 if (e->ops->elevator_set_req_fn)
874 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
876 rq->elevator_private = NULL;
877 return 0;
880 void elv_put_request(struct request_queue *q, struct request *rq)
882 elevator_t *e = q->elevator;
884 if (e->ops->elevator_put_req_fn)
885 e->ops->elevator_put_req_fn(rq);
888 int elv_may_queue(struct request_queue *q, int rw)
890 elevator_t *e = q->elevator;
892 if (e->ops->elevator_may_queue_fn)
893 return e->ops->elevator_may_queue_fn(q, rw);
895 return ELV_MQUEUE_MAY;
898 void elv_completed_request(struct request_queue *q, struct request *rq)
900 elevator_t *e = q->elevator;
903 * request is released from the driver, io must be done
905 if (blk_account_rq(rq)) {
906 q->in_flight--;
907 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
908 e->ops->elevator_completed_req_fn(q, rq);
912 * Check if the queue is waiting for fs requests to be
913 * drained for flush sequence.
915 if (unlikely(q->ordseq)) {
916 struct request *first_rq = list_entry_rq(q->queue_head.next);
917 if (q->in_flight == 0 &&
918 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
919 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
920 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
921 q->request_fn(q);
926 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
928 static ssize_t
929 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
931 elevator_t *e = container_of(kobj, elevator_t, kobj);
932 struct elv_fs_entry *entry = to_elv(attr);
933 ssize_t error;
935 if (!entry->show)
936 return -EIO;
938 mutex_lock(&e->sysfs_lock);
939 error = e->ops ? entry->show(e, page) : -ENOENT;
940 mutex_unlock(&e->sysfs_lock);
941 return error;
944 static ssize_t
945 elv_attr_store(struct kobject *kobj, struct attribute *attr,
946 const char *page, size_t length)
948 elevator_t *e = container_of(kobj, elevator_t, kobj);
949 struct elv_fs_entry *entry = to_elv(attr);
950 ssize_t error;
952 if (!entry->store)
953 return -EIO;
955 mutex_lock(&e->sysfs_lock);
956 error = e->ops ? entry->store(e, page, length) : -ENOENT;
957 mutex_unlock(&e->sysfs_lock);
958 return error;
961 static struct sysfs_ops elv_sysfs_ops = {
962 .show = elv_attr_show,
963 .store = elv_attr_store,
966 static struct kobj_type elv_ktype = {
967 .sysfs_ops = &elv_sysfs_ops,
968 .release = elevator_release,
971 int elv_register_queue(struct request_queue *q)
973 elevator_t *e = q->elevator;
974 int error;
976 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
977 if (!error) {
978 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
979 if (attr) {
980 while (attr->attr.name) {
981 if (sysfs_create_file(&e->kobj, &attr->attr))
982 break;
983 attr++;
986 kobject_uevent(&e->kobj, KOBJ_ADD);
988 return error;
991 static void __elv_unregister_queue(elevator_t *e)
993 kobject_uevent(&e->kobj, KOBJ_REMOVE);
994 kobject_del(&e->kobj);
997 void elv_unregister_queue(struct request_queue *q)
999 if (q)
1000 __elv_unregister_queue(q->elevator);
1003 void elv_register(struct elevator_type *e)
1005 char *def = "";
1007 spin_lock(&elv_list_lock);
1008 BUG_ON(elevator_find(e->elevator_name));
1009 list_add_tail(&e->list, &elv_list);
1010 spin_unlock(&elv_list_lock);
1012 if (!strcmp(e->elevator_name, chosen_elevator) ||
1013 (!*chosen_elevator &&
1014 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
1015 def = " (default)";
1017 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
1018 def);
1020 EXPORT_SYMBOL_GPL(elv_register);
1022 void elv_unregister(struct elevator_type *e)
1024 struct task_struct *g, *p;
1027 * Iterate every thread in the process to remove the io contexts.
1029 if (e->ops.trim) {
1030 read_lock(&tasklist_lock);
1031 do_each_thread(g, p) {
1032 task_lock(p);
1033 if (p->io_context)
1034 e->ops.trim(p->io_context);
1035 task_unlock(p);
1036 } while_each_thread(g, p);
1037 read_unlock(&tasklist_lock);
1040 spin_lock(&elv_list_lock);
1041 list_del_init(&e->list);
1042 spin_unlock(&elv_list_lock);
1044 EXPORT_SYMBOL_GPL(elv_unregister);
1047 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1048 * we don't free the old io scheduler, before we have allocated what we
1049 * need for the new one. this way we have a chance of going back to the old
1050 * one, if the new one fails init for some reason.
1052 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1054 elevator_t *old_elevator, *e;
1055 void *data;
1058 * Allocate new elevator
1060 e = elevator_alloc(q, new_e);
1061 if (!e)
1062 return 0;
1064 data = elevator_init_queue(q, e);
1065 if (!data) {
1066 kobject_put(&e->kobj);
1067 return 0;
1071 * Turn on BYPASS and drain all requests w/ elevator private data
1073 spin_lock_irq(q->queue_lock);
1075 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
1077 elv_drain_elevator(q);
1079 while (q->rq.elvpriv) {
1080 blk_remove_plug(q);
1081 q->request_fn(q);
1082 spin_unlock_irq(q->queue_lock);
1083 msleep(10);
1084 spin_lock_irq(q->queue_lock);
1085 elv_drain_elevator(q);
1089 * Remember old elevator.
1091 old_elevator = q->elevator;
1094 * attach and start new elevator
1096 elevator_attach(q, e, data);
1098 spin_unlock_irq(q->queue_lock);
1100 __elv_unregister_queue(old_elevator);
1102 if (elv_register_queue(q))
1103 goto fail_register;
1106 * finally exit old elevator and turn off BYPASS.
1108 elevator_exit(old_elevator);
1109 spin_lock_irq(q->queue_lock);
1110 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1111 spin_unlock_irq(q->queue_lock);
1113 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
1115 return 1;
1117 fail_register:
1119 * switch failed, exit the new io scheduler and reattach the old
1120 * one again (along with re-adding the sysfs dir)
1122 elevator_exit(e);
1123 q->elevator = old_elevator;
1124 elv_register_queue(q);
1126 spin_lock_irq(q->queue_lock);
1127 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1128 spin_unlock_irq(q->queue_lock);
1130 return 0;
1133 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1134 size_t count)
1136 char elevator_name[ELV_NAME_MAX];
1137 size_t len;
1138 struct elevator_type *e;
1140 elevator_name[sizeof(elevator_name) - 1] = '\0';
1141 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1142 len = strlen(elevator_name);
1144 if (len && elevator_name[len - 1] == '\n')
1145 elevator_name[len - 1] = '\0';
1147 e = elevator_get(elevator_name);
1148 if (!e) {
1149 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1150 return -EINVAL;
1153 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1154 elevator_put(e);
1155 return count;
1158 if (!elevator_switch(q, e))
1159 printk(KERN_ERR "elevator: switch to %s failed\n",
1160 elevator_name);
1161 return count;
1164 ssize_t elv_iosched_show(struct request_queue *q, char *name)
1166 elevator_t *e = q->elevator;
1167 struct elevator_type *elv = e->elevator_type;
1168 struct elevator_type *__e;
1169 int len = 0;
1171 spin_lock(&elv_list_lock);
1172 list_for_each_entry(__e, &elv_list, list) {
1173 if (!strcmp(elv->elevator_name, __e->elevator_name))
1174 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1175 else
1176 len += sprintf(name+len, "%s ", __e->elevator_name);
1178 spin_unlock(&elv_list_lock);
1180 len += sprintf(len+name, "\n");
1181 return len;
1184 struct request *elv_rb_former_request(struct request_queue *q,
1185 struct request *rq)
1187 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1189 if (rbprev)
1190 return rb_entry_rq(rbprev);
1192 return NULL;
1194 EXPORT_SYMBOL(elv_rb_former_request);
1196 struct request *elv_rb_latter_request(struct request_queue *q,
1197 struct request *rq)
1199 struct rb_node *rbnext = rb_next(&rq->rb_node);
1201 if (rbnext)
1202 return rb_entry_rq(rbnext);
1204 return NULL;
1206 EXPORT_SYMBOL(elv_rb_latter_request);