Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / elevator.c
blobbafbae0344d319e909e3e7add07467b1c0463a73
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 inline 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 && !try_module_get(e->elevator_owner))
138 e = NULL;
140 spin_unlock(&elv_list_lock);
142 return e;
145 static void *elevator_init_queue(struct request_queue *q,
146 struct elevator_queue *eq)
148 return eq->ops->elevator_init_fn(q);
151 static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
152 void *data)
154 q->elevator = eq;
155 eq->elevator_data = data;
158 static char chosen_elevator[16];
160 static int __init elevator_setup(char *str)
163 * Be backwards-compatible with previous kernels, so users
164 * won't get the wrong elevator.
166 if (!strcmp(str, "as"))
167 strcpy(chosen_elevator, "anticipatory");
168 else
169 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
170 return 1;
173 __setup("elevator=", elevator_setup);
175 static struct kobj_type elv_ktype;
177 static elevator_t *elevator_alloc(struct request_queue *q,
178 struct elevator_type *e)
180 elevator_t *eq;
181 int i;
183 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
184 if (unlikely(!eq))
185 goto err;
187 eq->ops = &e->ops;
188 eq->elevator_type = e;
189 kobject_init(&eq->kobj, &elv_ktype);
190 mutex_init(&eq->sysfs_lock);
192 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
193 GFP_KERNEL, q->node);
194 if (!eq->hash)
195 goto err;
197 for (i = 0; i < ELV_HASH_ENTRIES; i++)
198 INIT_HLIST_HEAD(&eq->hash[i]);
200 return eq;
201 err:
202 kfree(eq);
203 elevator_put(e);
204 return NULL;
207 static void elevator_release(struct kobject *kobj)
209 elevator_t *e = container_of(kobj, elevator_t, kobj);
211 elevator_put(e->elevator_type);
212 kfree(e->hash);
213 kfree(e);
216 int elevator_init(struct request_queue *q, char *name)
218 struct elevator_type *e = NULL;
219 struct elevator_queue *eq;
220 int ret = 0;
221 void *data;
223 INIT_LIST_HEAD(&q->queue_head);
224 q->last_merge = NULL;
225 q->end_sector = 0;
226 q->boundary_rq = NULL;
228 if (name) {
229 e = elevator_get(name);
230 if (!e)
231 return -EINVAL;
234 if (!e && *chosen_elevator) {
235 e = elevator_get(chosen_elevator);
236 if (!e)
237 printk(KERN_ERR "I/O scheduler %s not found\n",
238 chosen_elevator);
241 if (!e) {
242 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
243 if (!e) {
244 printk(KERN_ERR
245 "Default I/O scheduler not found. " \
246 "Using noop.\n");
247 e = elevator_get("noop");
251 eq = elevator_alloc(q, e);
252 if (!eq)
253 return -ENOMEM;
255 data = elevator_init_queue(q, eq);
256 if (!data) {
257 kobject_put(&eq->kobj);
258 return -ENOMEM;
261 elevator_attach(q, eq, data);
262 return ret;
264 EXPORT_SYMBOL(elevator_init);
266 void elevator_exit(elevator_t *e)
268 mutex_lock(&e->sysfs_lock);
269 if (e->ops->elevator_exit_fn)
270 e->ops->elevator_exit_fn(e);
271 e->ops = NULL;
272 mutex_unlock(&e->sysfs_lock);
274 kobject_put(&e->kobj);
276 EXPORT_SYMBOL(elevator_exit);
278 static void elv_activate_rq(struct request_queue *q, struct request *rq)
280 elevator_t *e = q->elevator;
282 if (e->ops->elevator_activate_req_fn)
283 e->ops->elevator_activate_req_fn(q, rq);
286 static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
288 elevator_t *e = q->elevator;
290 if (e->ops->elevator_deactivate_req_fn)
291 e->ops->elevator_deactivate_req_fn(q, rq);
294 static inline void __elv_rqhash_del(struct request *rq)
296 hlist_del_init(&rq->hash);
299 static void elv_rqhash_del(struct request_queue *q, struct request *rq)
301 if (ELV_ON_HASH(rq))
302 __elv_rqhash_del(rq);
305 static void elv_rqhash_add(struct request_queue *q, struct request *rq)
307 elevator_t *e = q->elevator;
309 BUG_ON(ELV_ON_HASH(rq));
310 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
313 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
315 __elv_rqhash_del(rq);
316 elv_rqhash_add(q, rq);
319 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
321 elevator_t *e = q->elevator;
322 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
323 struct hlist_node *entry, *next;
324 struct request *rq;
326 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
327 BUG_ON(!ELV_ON_HASH(rq));
329 if (unlikely(!rq_mergeable(rq))) {
330 __elv_rqhash_del(rq);
331 continue;
334 if (rq_hash_key(rq) == offset)
335 return rq;
338 return NULL;
342 * RB-tree support functions for inserting/lookup/removal of requests
343 * in a sorted RB tree.
345 struct request *elv_rb_add(struct rb_root *root, struct request *rq)
347 struct rb_node **p = &root->rb_node;
348 struct rb_node *parent = NULL;
349 struct request *__rq;
351 while (*p) {
352 parent = *p;
353 __rq = rb_entry(parent, struct request, rb_node);
355 if (rq->sector < __rq->sector)
356 p = &(*p)->rb_left;
357 else if (rq->sector > __rq->sector)
358 p = &(*p)->rb_right;
359 else
360 return __rq;
363 rb_link_node(&rq->rb_node, parent, p);
364 rb_insert_color(&rq->rb_node, root);
365 return NULL;
367 EXPORT_SYMBOL(elv_rb_add);
369 void elv_rb_del(struct rb_root *root, struct request *rq)
371 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
372 rb_erase(&rq->rb_node, root);
373 RB_CLEAR_NODE(&rq->rb_node);
375 EXPORT_SYMBOL(elv_rb_del);
377 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
379 struct rb_node *n = root->rb_node;
380 struct request *rq;
382 while (n) {
383 rq = rb_entry(n, struct request, rb_node);
385 if (sector < rq->sector)
386 n = n->rb_left;
387 else if (sector > rq->sector)
388 n = n->rb_right;
389 else
390 return rq;
393 return NULL;
395 EXPORT_SYMBOL(elv_rb_find);
398 * Insert rq into dispatch queue of q. Queue lock must be held on
399 * entry. rq is sort instead into the dispatch queue. To be used by
400 * specific elevators.
402 void elv_dispatch_sort(struct request_queue *q, struct request *rq)
404 sector_t boundary;
405 struct list_head *entry;
406 int stop_flags;
408 if (q->last_merge == rq)
409 q->last_merge = NULL;
411 elv_rqhash_del(q, rq);
413 q->nr_sorted--;
415 boundary = q->end_sector;
416 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
417 list_for_each_prev(entry, &q->queue_head) {
418 struct request *pos = list_entry_rq(entry);
420 if (rq_data_dir(rq) != rq_data_dir(pos))
421 break;
422 if (pos->cmd_flags & stop_flags)
423 break;
424 if (rq->sector >= boundary) {
425 if (pos->sector < boundary)
426 continue;
427 } else {
428 if (pos->sector >= boundary)
429 break;
431 if (rq->sector >= pos->sector)
432 break;
435 list_add(&rq->queuelist, entry);
437 EXPORT_SYMBOL(elv_dispatch_sort);
440 * Insert rq into dispatch queue of q. Queue lock must be held on
441 * entry. rq is added to the back of the dispatch queue. To be used by
442 * specific elevators.
444 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
446 if (q->last_merge == rq)
447 q->last_merge = NULL;
449 elv_rqhash_del(q, rq);
451 q->nr_sorted--;
453 q->end_sector = rq_end_sector(rq);
454 q->boundary_rq = rq;
455 list_add_tail(&rq->queuelist, &q->queue_head);
457 EXPORT_SYMBOL(elv_dispatch_add_tail);
459 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
461 elevator_t *e = q->elevator;
462 struct request *__rq;
463 int ret;
466 * First try one-hit cache.
468 if (q->last_merge) {
469 ret = elv_try_merge(q->last_merge, bio);
470 if (ret != ELEVATOR_NO_MERGE) {
471 *req = q->last_merge;
472 return ret;
477 * See if our hash lookup can find a potential backmerge.
479 __rq = elv_rqhash_find(q, bio->bi_sector);
480 if (__rq && elv_rq_merge_ok(__rq, bio)) {
481 *req = __rq;
482 return ELEVATOR_BACK_MERGE;
485 if (e->ops->elevator_merge_fn)
486 return e->ops->elevator_merge_fn(q, req, bio);
488 return ELEVATOR_NO_MERGE;
491 void elv_merged_request(struct request_queue *q, struct request *rq, int type)
493 elevator_t *e = q->elevator;
495 if (e->ops->elevator_merged_fn)
496 e->ops->elevator_merged_fn(q, rq, type);
498 if (type == ELEVATOR_BACK_MERGE)
499 elv_rqhash_reposition(q, rq);
501 q->last_merge = rq;
504 void elv_merge_requests(struct request_queue *q, struct request *rq,
505 struct request *next)
507 elevator_t *e = q->elevator;
509 if (e->ops->elevator_merge_req_fn)
510 e->ops->elevator_merge_req_fn(q, rq, next);
512 elv_rqhash_reposition(q, rq);
513 elv_rqhash_del(q, next);
515 q->nr_sorted--;
516 q->last_merge = rq;
519 void elv_requeue_request(struct request_queue *q, struct request *rq)
522 * it already went through dequeue, we need to decrement the
523 * in_flight count again
525 if (blk_account_rq(rq)) {
526 q->in_flight--;
527 if (blk_sorted_rq(rq))
528 elv_deactivate_rq(q, rq);
531 rq->cmd_flags &= ~REQ_STARTED;
533 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
536 static void elv_drain_elevator(struct request_queue *q)
538 static int printed;
539 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
541 if (q->nr_sorted == 0)
542 return;
543 if (printed++ < 10) {
544 printk(KERN_ERR "%s: forced dispatching is broken "
545 "(nr_sorted=%u), please report this\n",
546 q->elevator->elevator_type->elevator_name, q->nr_sorted);
550 void elv_insert(struct request_queue *q, struct request *rq, int where)
552 struct list_head *pos;
553 unsigned ordseq;
554 int unplug_it = 1;
556 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
558 rq->q = q;
560 switch (where) {
561 case ELEVATOR_INSERT_FRONT:
562 rq->cmd_flags |= REQ_SOFTBARRIER;
564 list_add(&rq->queuelist, &q->queue_head);
565 break;
567 case ELEVATOR_INSERT_BACK:
568 rq->cmd_flags |= REQ_SOFTBARRIER;
569 elv_drain_elevator(q);
570 list_add_tail(&rq->queuelist, &q->queue_head);
572 * We kick the queue here for the following reasons.
573 * - The elevator might have returned NULL previously
574 * to delay requests and returned them now. As the
575 * queue wasn't empty before this request, ll_rw_blk
576 * won't run the queue on return, resulting in hang.
577 * - Usually, back inserted requests won't be merged
578 * with anything. There's no point in delaying queue
579 * processing.
581 blk_remove_plug(q);
582 q->request_fn(q);
583 break;
585 case ELEVATOR_INSERT_SORT:
586 BUG_ON(!blk_fs_request(rq));
587 rq->cmd_flags |= REQ_SORTED;
588 q->nr_sorted++;
589 if (rq_mergeable(rq)) {
590 elv_rqhash_add(q, rq);
591 if (!q->last_merge)
592 q->last_merge = rq;
596 * Some ioscheds (cfq) run q->request_fn directly, so
597 * rq cannot be accessed after calling
598 * elevator_add_req_fn.
600 q->elevator->ops->elevator_add_req_fn(q, rq);
601 break;
603 case ELEVATOR_INSERT_REQUEUE:
605 * If ordered flush isn't in progress, we do front
606 * insertion; otherwise, requests should be requeued
607 * in ordseq order.
609 rq->cmd_flags |= REQ_SOFTBARRIER;
612 * Most requeues happen because of a busy condition,
613 * don't force unplug of the queue for that case.
615 unplug_it = 0;
617 if (q->ordseq == 0) {
618 list_add(&rq->queuelist, &q->queue_head);
619 break;
622 ordseq = blk_ordered_req_seq(rq);
624 list_for_each(pos, &q->queue_head) {
625 struct request *pos_rq = list_entry_rq(pos);
626 if (ordseq <= blk_ordered_req_seq(pos_rq))
627 break;
630 list_add_tail(&rq->queuelist, pos);
631 break;
633 default:
634 printk(KERN_ERR "%s: bad insertion point %d\n",
635 __FUNCTION__, where);
636 BUG();
639 if (unplug_it && blk_queue_plugged(q)) {
640 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
641 - q->in_flight;
643 if (nrq >= q->unplug_thresh)
644 __generic_unplug_device(q);
648 void __elv_add_request(struct request_queue *q, struct request *rq, int where,
649 int plug)
651 if (q->ordcolor)
652 rq->cmd_flags |= REQ_ORDERED_COLOR;
654 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
656 * toggle ordered color
658 if (blk_barrier_rq(rq))
659 q->ordcolor ^= 1;
662 * barriers implicitly indicate back insertion
664 if (where == ELEVATOR_INSERT_SORT)
665 where = ELEVATOR_INSERT_BACK;
668 * this request is scheduling boundary, update
669 * end_sector
671 if (blk_fs_request(rq)) {
672 q->end_sector = rq_end_sector(rq);
673 q->boundary_rq = rq;
675 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
676 where == ELEVATOR_INSERT_SORT)
677 where = ELEVATOR_INSERT_BACK;
679 if (plug)
680 blk_plug_device(q);
682 elv_insert(q, rq, where);
684 EXPORT_SYMBOL(__elv_add_request);
686 void elv_add_request(struct request_queue *q, struct request *rq, int where,
687 int plug)
689 unsigned long flags;
691 spin_lock_irqsave(q->queue_lock, flags);
692 __elv_add_request(q, rq, where, plug);
693 spin_unlock_irqrestore(q->queue_lock, flags);
695 EXPORT_SYMBOL(elv_add_request);
697 static inline struct request *__elv_next_request(struct request_queue *q)
699 struct request *rq;
701 while (1) {
702 while (!list_empty(&q->queue_head)) {
703 rq = list_entry_rq(q->queue_head.next);
704 if (blk_do_ordered(q, &rq))
705 return rq;
708 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
709 return NULL;
713 struct request *elv_next_request(struct request_queue *q)
715 struct request *rq;
716 int ret;
718 while ((rq = __elv_next_request(q)) != NULL) {
720 * Kill the empty barrier place holder, the driver must
721 * not ever see it.
723 if (blk_empty_barrier(rq)) {
724 end_queued_request(rq, 1);
725 continue;
727 if (!(rq->cmd_flags & REQ_STARTED)) {
729 * This is the first time the device driver
730 * sees this request (possibly after
731 * requeueing). Notify IO scheduler.
733 if (blk_sorted_rq(rq))
734 elv_activate_rq(q, rq);
737 * just mark as started even if we don't start
738 * it, a request that has been delayed should
739 * not be passed by new incoming requests
741 rq->cmd_flags |= REQ_STARTED;
742 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
745 if (!q->boundary_rq || q->boundary_rq == rq) {
746 q->end_sector = rq_end_sector(rq);
747 q->boundary_rq = NULL;
750 if (rq->cmd_flags & REQ_DONTPREP)
751 break;
753 if (q->dma_drain_size && rq->data_len) {
755 * make sure space for the drain appears we
756 * know we can do this because max_hw_segments
757 * has been adjusted to be one fewer than the
758 * device can handle
760 rq->nr_phys_segments++;
761 rq->nr_hw_segments++;
764 if (!q->prep_rq_fn)
765 break;
767 ret = q->prep_rq_fn(q, rq);
768 if (ret == BLKPREP_OK) {
769 break;
770 } else if (ret == BLKPREP_DEFER) {
772 * the request may have been (partially) prepped.
773 * we need to keep this request in the front to
774 * avoid resource deadlock. REQ_STARTED will
775 * prevent other fs requests from passing this one.
777 if (q->dma_drain_size && rq->data_len &&
778 !(rq->cmd_flags & REQ_DONTPREP)) {
780 * remove the space for the drain we added
781 * so that we don't add it again
783 --rq->nr_phys_segments;
784 --rq->nr_hw_segments;
787 rq = NULL;
788 break;
789 } else if (ret == BLKPREP_KILL) {
790 rq->cmd_flags |= REQ_QUIET;
791 end_queued_request(rq, 0);
792 } else {
793 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
794 ret);
795 break;
799 return rq;
801 EXPORT_SYMBOL(elv_next_request);
803 void elv_dequeue_request(struct request_queue *q, struct request *rq)
805 BUG_ON(list_empty(&rq->queuelist));
806 BUG_ON(ELV_ON_HASH(rq));
808 list_del_init(&rq->queuelist);
811 * the time frame between a request being removed from the lists
812 * and to it is freed is accounted as io that is in progress at
813 * the driver side.
815 if (blk_account_rq(rq))
816 q->in_flight++;
818 EXPORT_SYMBOL(elv_dequeue_request);
820 int elv_queue_empty(struct request_queue *q)
822 elevator_t *e = q->elevator;
824 if (!list_empty(&q->queue_head))
825 return 0;
827 if (e->ops->elevator_queue_empty_fn)
828 return e->ops->elevator_queue_empty_fn(q);
830 return 1;
832 EXPORT_SYMBOL(elv_queue_empty);
834 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
836 elevator_t *e = q->elevator;
838 if (e->ops->elevator_latter_req_fn)
839 return e->ops->elevator_latter_req_fn(q, rq);
840 return NULL;
843 struct request *elv_former_request(struct request_queue *q, struct request *rq)
845 elevator_t *e = q->elevator;
847 if (e->ops->elevator_former_req_fn)
848 return e->ops->elevator_former_req_fn(q, rq);
849 return NULL;
852 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
854 elevator_t *e = q->elevator;
856 if (e->ops->elevator_set_req_fn)
857 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
859 rq->elevator_private = NULL;
860 return 0;
863 void elv_put_request(struct request_queue *q, struct request *rq)
865 elevator_t *e = q->elevator;
867 if (e->ops->elevator_put_req_fn)
868 e->ops->elevator_put_req_fn(rq);
871 int elv_may_queue(struct request_queue *q, int rw)
873 elevator_t *e = q->elevator;
875 if (e->ops->elevator_may_queue_fn)
876 return e->ops->elevator_may_queue_fn(q, rw);
878 return ELV_MQUEUE_MAY;
881 void elv_completed_request(struct request_queue *q, struct request *rq)
883 elevator_t *e = q->elevator;
886 * request is released from the driver, io must be done
888 if (blk_account_rq(rq)) {
889 q->in_flight--;
890 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
891 e->ops->elevator_completed_req_fn(q, rq);
895 * Check if the queue is waiting for fs requests to be
896 * drained for flush sequence.
898 if (unlikely(q->ordseq)) {
899 struct request *first_rq = list_entry_rq(q->queue_head.next);
900 if (q->in_flight == 0 &&
901 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
902 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
903 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
904 q->request_fn(q);
909 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
911 static ssize_t
912 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
914 elevator_t *e = container_of(kobj, elevator_t, kobj);
915 struct elv_fs_entry *entry = to_elv(attr);
916 ssize_t error;
918 if (!entry->show)
919 return -EIO;
921 mutex_lock(&e->sysfs_lock);
922 error = e->ops ? entry->show(e, page) : -ENOENT;
923 mutex_unlock(&e->sysfs_lock);
924 return error;
927 static ssize_t
928 elv_attr_store(struct kobject *kobj, struct attribute *attr,
929 const char *page, size_t length)
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->store)
936 return -EIO;
938 mutex_lock(&e->sysfs_lock);
939 error = e->ops ? entry->store(e, page, length) : -ENOENT;
940 mutex_unlock(&e->sysfs_lock);
941 return error;
944 static struct sysfs_ops elv_sysfs_ops = {
945 .show = elv_attr_show,
946 .store = elv_attr_store,
949 static struct kobj_type elv_ktype = {
950 .sysfs_ops = &elv_sysfs_ops,
951 .release = elevator_release,
954 int elv_register_queue(struct request_queue *q)
956 elevator_t *e = q->elevator;
957 int error;
959 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
960 if (!error) {
961 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
962 if (attr) {
963 while (attr->attr.name) {
964 if (sysfs_create_file(&e->kobj, &attr->attr))
965 break;
966 attr++;
969 kobject_uevent(&e->kobj, KOBJ_ADD);
971 return error;
974 static void __elv_unregister_queue(elevator_t *e)
976 kobject_uevent(&e->kobj, KOBJ_REMOVE);
977 kobject_del(&e->kobj);
980 void elv_unregister_queue(struct request_queue *q)
982 if (q)
983 __elv_unregister_queue(q->elevator);
986 void elv_register(struct elevator_type *e)
988 char *def = "";
990 spin_lock(&elv_list_lock);
991 BUG_ON(elevator_find(e->elevator_name));
992 list_add_tail(&e->list, &elv_list);
993 spin_unlock(&elv_list_lock);
995 if (!strcmp(e->elevator_name, chosen_elevator) ||
996 (!*chosen_elevator &&
997 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
998 def = " (default)";
1000 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
1001 def);
1003 EXPORT_SYMBOL_GPL(elv_register);
1005 void elv_unregister(struct elevator_type *e)
1007 struct task_struct *g, *p;
1010 * Iterate every thread in the process to remove the io contexts.
1012 if (e->ops.trim) {
1013 read_lock(&tasklist_lock);
1014 do_each_thread(g, p) {
1015 task_lock(p);
1016 if (p->io_context)
1017 e->ops.trim(p->io_context);
1018 task_unlock(p);
1019 } while_each_thread(g, p);
1020 read_unlock(&tasklist_lock);
1023 spin_lock(&elv_list_lock);
1024 list_del_init(&e->list);
1025 spin_unlock(&elv_list_lock);
1027 EXPORT_SYMBOL_GPL(elv_unregister);
1030 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1031 * we don't free the old io scheduler, before we have allocated what we
1032 * need for the new one. this way we have a chance of going back to the old
1033 * one, if the new one fails init for some reason.
1035 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1037 elevator_t *old_elevator, *e;
1038 void *data;
1041 * Allocate new elevator
1043 e = elevator_alloc(q, new_e);
1044 if (!e)
1045 return 0;
1047 data = elevator_init_queue(q, e);
1048 if (!data) {
1049 kobject_put(&e->kobj);
1050 return 0;
1054 * Turn on BYPASS and drain all requests w/ elevator private data
1056 spin_lock_irq(q->queue_lock);
1058 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1060 elv_drain_elevator(q);
1062 while (q->rq.elvpriv) {
1063 blk_remove_plug(q);
1064 q->request_fn(q);
1065 spin_unlock_irq(q->queue_lock);
1066 msleep(10);
1067 spin_lock_irq(q->queue_lock);
1068 elv_drain_elevator(q);
1072 * Remember old elevator.
1074 old_elevator = q->elevator;
1077 * attach and start new elevator
1079 elevator_attach(q, e, data);
1081 spin_unlock_irq(q->queue_lock);
1083 __elv_unregister_queue(old_elevator);
1085 if (elv_register_queue(q))
1086 goto fail_register;
1089 * finally exit old elevator and turn off BYPASS.
1091 elevator_exit(old_elevator);
1092 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1093 return 1;
1095 fail_register:
1097 * switch failed, exit the new io scheduler and reattach the old
1098 * one again (along with re-adding the sysfs dir)
1100 elevator_exit(e);
1101 q->elevator = old_elevator;
1102 elv_register_queue(q);
1103 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1104 return 0;
1107 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1108 size_t count)
1110 char elevator_name[ELV_NAME_MAX];
1111 size_t len;
1112 struct elevator_type *e;
1114 elevator_name[sizeof(elevator_name) - 1] = '\0';
1115 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1116 len = strlen(elevator_name);
1118 if (len && elevator_name[len - 1] == '\n')
1119 elevator_name[len - 1] = '\0';
1121 e = elevator_get(elevator_name);
1122 if (!e) {
1123 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1124 return -EINVAL;
1127 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1128 elevator_put(e);
1129 return count;
1132 if (!elevator_switch(q, e))
1133 printk(KERN_ERR "elevator: switch to %s failed\n",
1134 elevator_name);
1135 return count;
1138 ssize_t elv_iosched_show(struct request_queue *q, char *name)
1140 elevator_t *e = q->elevator;
1141 struct elevator_type *elv = e->elevator_type;
1142 struct elevator_type *__e;
1143 int len = 0;
1145 spin_lock(&elv_list_lock);
1146 list_for_each_entry(__e, &elv_list, list) {
1147 if (!strcmp(elv->elevator_name, __e->elevator_name))
1148 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1149 else
1150 len += sprintf(name+len, "%s ", __e->elevator_name);
1152 spin_unlock(&elv_list_lock);
1154 len += sprintf(len+name, "\n");
1155 return len;
1158 struct request *elv_rb_former_request(struct request_queue *q,
1159 struct request *rq)
1161 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1163 if (rbprev)
1164 return rb_entry_rq(rbprev);
1166 return NULL;
1168 EXPORT_SYMBOL(elv_rb_former_request);
1170 struct request *elv_rb_latter_request(struct request_queue *q,
1171 struct request *rq)
1173 struct rb_node *rbnext = rb_next(&rq->rb_node);
1175 if (rbnext)
1176 return rb_entry_rq(rbnext);
1178 return NULL;
1180 EXPORT_SYMBOL(elv_rb_latter_request);