2 * linux/drivers/block/elevator.c
4 * Block device elevator/IO-scheduler.
6 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
8 * 30042000 Jens Axboe <axboe@suse.de> :
10 * Split the elevator a bit so that it is possible to choose a different
11 * one or even write a new "plug in". There are three pieces:
12 * - elevator_fn, inserts a new request in the queue list
13 * - elevator_merge_fn, decides whether a new buffer can be merged with
15 * - elevator_dequeue_fn, called when a request is taken off the active list
17 * 20082000 Dave Jones <davej@suse.de> :
18 * Removed tests for max-bomb-segments, which was breaking elvtune
19 * when run without -bN
22 * - Rework again to work with bio instead of buffer_heads
23 * - loose bi_dev comparisons, partition handling is right now
24 * - completely modularize elevator setup and teardown
27 #include <linux/kernel.h>
29 #include <linux/blkdev.h>
30 #include <linux/elevator.h>
31 #include <linux/bio.h>
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/compiler.h>
38 #include <asm/uaccess.h>
40 static DEFINE_SPINLOCK(elv_list_lock);
41 static LIST_HEAD(elv_list);
44 * can we safely merge with this request?
46 inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
48 if (!rq_mergeable(rq))
52 * different data direction or already started, don't merge
54 if (bio_data_dir(bio) != rq_data_dir(rq))
58 * same device and no special stuff set, merge is ok
60 if (rq->rq_disk == bio->bi_bdev->bd_disk &&
61 !rq->waiting && !rq->special)
66 EXPORT_SYMBOL(elv_rq_merge_ok);
68 inline int elv_try_merge(struct request *__rq, struct bio *bio)
70 int ret = ELEVATOR_NO_MERGE;
73 * we can merge and sequence is ok, check if it's possible
75 if (elv_rq_merge_ok(__rq, bio)) {
76 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
77 ret = ELEVATOR_BACK_MERGE;
78 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
79 ret = ELEVATOR_FRONT_MERGE;
84 EXPORT_SYMBOL(elv_try_merge);
86 inline int elv_try_last_merge(request_queue_t *q, struct bio *bio)
89 return elv_try_merge(q->last_merge, bio);
91 return ELEVATOR_NO_MERGE;
93 EXPORT_SYMBOL(elv_try_last_merge);
95 static struct elevator_type *elevator_find(const char *name)
97 struct elevator_type *e = NULL;
98 struct list_head *entry;
100 list_for_each(entry, &elv_list) {
101 struct elevator_type *__e;
103 __e = list_entry(entry, struct elevator_type, list);
105 if (!strcmp(__e->elevator_name, name)) {
114 static void elevator_put(struct elevator_type *e)
116 module_put(e->elevator_owner);
119 static struct elevator_type *elevator_get(const char *name)
121 struct elevator_type *e;
123 spin_lock_irq(&elv_list_lock);
125 e = elevator_find(name);
126 if (e && !try_module_get(e->elevator_owner))
129 spin_unlock_irq(&elv_list_lock);
134 static int elevator_attach(request_queue_t *q, struct elevator_type *e,
135 struct elevator_queue *eq)
139 memset(eq, 0, sizeof(*eq));
141 eq->elevator_type = e;
143 INIT_LIST_HEAD(&q->queue_head);
144 q->last_merge = NULL;
147 q->boundary_rq = NULL;
150 if (eq->ops->elevator_init_fn)
151 ret = eq->ops->elevator_init_fn(q, eq);
156 static char chosen_elevator[16];
158 static void elevator_setup_default(void)
160 struct elevator_type *e;
163 * check if default is set and exists
165 if (chosen_elevator[0] && (e = elevator_get(chosen_elevator))) {
170 #if defined(CONFIG_IOSCHED_AS)
171 strcpy(chosen_elevator, "anticipatory");
172 #elif defined(CONFIG_IOSCHED_DEADLINE)
173 strcpy(chosen_elevator, "deadline");
174 #elif defined(CONFIG_IOSCHED_CFQ)
175 strcpy(chosen_elevator, "cfq");
176 #elif defined(CONFIG_IOSCHED_NOOP)
177 strcpy(chosen_elevator, "noop");
179 #error "You must build at least 1 IO scheduler into the kernel"
183 static int __init elevator_setup(char *str)
185 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
189 __setup("elevator=", elevator_setup);
191 int elevator_init(request_queue_t *q, char *name)
193 struct elevator_type *e = NULL;
194 struct elevator_queue *eq;
197 elevator_setup_default();
200 name = chosen_elevator;
202 e = elevator_get(name);
206 eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL);
208 elevator_put(e->elevator_type);
212 ret = elevator_attach(q, e, eq);
215 elevator_put(e->elevator_type);
221 void elevator_exit(elevator_t *e)
223 if (e->ops->elevator_exit_fn)
224 e->ops->elevator_exit_fn(e);
226 elevator_put(e->elevator_type);
227 e->elevator_type = NULL;
231 void elv_dispatch_insert_tail(request_queue_t *q, struct request *rq)
236 * Insert rq into dispatch queue of q. Queue lock must be held on
237 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
238 * appended to the dispatch queue. To be used by specific elevators.
240 void elv_dispatch_sort(request_queue_t *q, struct request *rq)
244 struct list_head *entry;
246 boundary = q->end_sector;
247 max_back = q->max_back_kb * 2;
248 boundary = boundary > max_back ? boundary - max_back : 0;
250 list_for_each_prev(entry, &q->queue_head) {
251 struct request *pos = list_entry_rq(entry);
253 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
255 if (rq->sector >= boundary) {
256 if (pos->sector < boundary)
259 if (pos->sector >= boundary)
262 if (rq->sector >= pos->sector)
266 list_add(&rq->queuelist, entry);
269 int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
271 elevator_t *e = q->elevator;
273 if (e->ops->elevator_merge_fn)
274 return e->ops->elevator_merge_fn(q, req, bio);
276 return ELEVATOR_NO_MERGE;
279 void elv_merged_request(request_queue_t *q, struct request *rq)
281 elevator_t *e = q->elevator;
283 if (e->ops->elevator_merged_fn)
284 e->ops->elevator_merged_fn(q, rq);
287 void elv_merge_requests(request_queue_t *q, struct request *rq,
288 struct request *next)
290 elevator_t *e = q->elevator;
292 if (q->last_merge == next)
293 q->last_merge = NULL;
295 if (e->ops->elevator_merge_req_fn)
296 e->ops->elevator_merge_req_fn(q, rq, next);
299 void elv_requeue_request(request_queue_t *q, struct request *rq)
301 elevator_t *e = q->elevator;
304 * it already went through dequeue, we need to decrement the
305 * in_flight count again
307 if (blk_account_rq(rq)) {
309 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
310 e->ops->elevator_deactivate_req_fn(q, rq);
313 rq->flags &= ~REQ_STARTED;
316 * if this is the flush, requeue the original instead and drop the flush
318 if (rq->flags & REQ_BAR_FLUSH) {
319 clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
320 rq = rq->end_io_data;
323 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
326 void __elv_add_request(request_queue_t *q, struct request *rq, int where,
329 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
331 * barriers implicitly indicate back insertion
333 if (where == ELEVATOR_INSERT_SORT)
334 where = ELEVATOR_INSERT_BACK;
337 * this request is scheduling boundary, update end_sector
339 if (blk_fs_request(rq)) {
340 q->end_sector = rq_end_sector(rq);
350 if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))) {
352 * if drain is set, store the request "locally". when the drain
353 * is finished, the requests will be handed ordered to the io
356 list_add_tail(&rq->queuelist, &q->drain_list);
361 case ELEVATOR_INSERT_FRONT:
362 rq->flags |= REQ_SOFTBARRIER;
364 list_add(&rq->queuelist, &q->queue_head);
367 case ELEVATOR_INSERT_BACK:
368 rq->flags |= REQ_SOFTBARRIER;
370 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
372 list_add_tail(&rq->queuelist, &q->queue_head);
374 * We kick the queue here for the following reasons.
375 * - The elevator might have returned NULL previously
376 * to delay requests and returned them now. As the
377 * queue wasn't empty before this request, ll_rw_blk
378 * won't run the queue on return, resulting in hang.
379 * - Usually, back inserted requests won't be merged
380 * with anything. There's no point in delaying queue
387 case ELEVATOR_INSERT_SORT:
388 BUG_ON(!blk_fs_request(rq));
389 rq->flags |= REQ_SORTED;
390 q->elevator->ops->elevator_add_req_fn(q, rq);
394 printk(KERN_ERR "%s: bad insertion point %d\n",
395 __FUNCTION__, where);
399 if (blk_queue_plugged(q)) {
400 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
403 if (nrq >= q->unplug_thresh)
404 __generic_unplug_device(q);
408 void elv_add_request(request_queue_t *q, struct request *rq, int where,
413 spin_lock_irqsave(q->queue_lock, flags);
414 __elv_add_request(q, rq, where, plug);
415 spin_unlock_irqrestore(q->queue_lock, flags);
418 static inline struct request *__elv_next_request(request_queue_t *q)
422 if (unlikely(list_empty(&q->queue_head) &&
423 !q->elevator->ops->elevator_dispatch_fn(q, 0)))
426 rq = list_entry_rq(q->queue_head.next);
429 * if this is a barrier write and the device has to issue a
430 * flush sequence to support it, check how far we are
432 if (blk_fs_request(rq) && blk_barrier_rq(rq)) {
433 BUG_ON(q->ordered == QUEUE_ORDERED_NONE);
435 if (q->ordered == QUEUE_ORDERED_FLUSH &&
436 !blk_barrier_preflush(rq))
437 rq = blk_start_pre_flush(q, rq);
443 struct request *elv_next_request(request_queue_t *q)
448 while ((rq = __elv_next_request(q)) != NULL) {
449 if (!(rq->flags & REQ_STARTED)) {
450 elevator_t *e = q->elevator;
453 * This is the first time the device driver
454 * sees this request (possibly after
455 * requeueing). Notify IO scheduler.
457 if (blk_sorted_rq(rq) &&
458 e->ops->elevator_activate_req_fn)
459 e->ops->elevator_activate_req_fn(q, rq);
462 * just mark as started even if we don't start
463 * it, a request that has been delayed should
464 * not be passed by new incoming requests
466 rq->flags |= REQ_STARTED;
469 if (rq == q->last_merge)
470 q->last_merge = NULL;
472 if (!q->boundary_rq || q->boundary_rq == rq) {
473 q->end_sector = rq_end_sector(rq);
474 q->boundary_rq = NULL;
477 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
480 ret = q->prep_rq_fn(q, rq);
481 if (ret == BLKPREP_OK) {
483 } else if (ret == BLKPREP_DEFER) {
485 * the request may have been (partially) prepped.
486 * we need to keep this request in the front to
487 * avoid resource deadlock. REQ_STARTED will
488 * prevent other fs requests from passing this one.
492 } else if (ret == BLKPREP_KILL) {
493 int nr_bytes = rq->hard_nr_sectors << 9;
496 nr_bytes = rq->data_len;
498 blkdev_dequeue_request(rq);
499 rq->flags |= REQ_QUIET;
500 end_that_request_chunk(rq, 0, nr_bytes);
501 end_that_request_last(rq);
503 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
512 void elv_dequeue_request(request_queue_t *q, struct request *rq)
514 BUG_ON(list_empty(&rq->queuelist));
516 list_del_init(&rq->queuelist);
519 * the time frame between a request being removed from the lists
520 * and to it is freed is accounted as io that is in progress at
523 if (blk_account_rq(rq))
527 * the main clearing point for q->last_merge is on retrieval of
528 * request by driver (it calls elv_next_request()), but it _can_
529 * also happen here if a request is added to the queue but later
530 * deleted without ever being given to driver (merged with another
533 if (rq == q->last_merge)
534 q->last_merge = NULL;
537 int elv_queue_empty(request_queue_t *q)
539 elevator_t *e = q->elevator;
541 if (!list_empty(&q->queue_head))
544 if (e->ops->elevator_queue_empty_fn)
545 return e->ops->elevator_queue_empty_fn(q);
550 struct request *elv_latter_request(request_queue_t *q, struct request *rq)
552 struct list_head *next;
554 elevator_t *e = q->elevator;
556 if (e->ops->elevator_latter_req_fn)
557 return e->ops->elevator_latter_req_fn(q, rq);
559 next = rq->queuelist.next;
560 if (next != &q->queue_head && next != &rq->queuelist)
561 return list_entry_rq(next);
566 struct request *elv_former_request(request_queue_t *q, struct request *rq)
568 struct list_head *prev;
570 elevator_t *e = q->elevator;
572 if (e->ops->elevator_former_req_fn)
573 return e->ops->elevator_former_req_fn(q, rq);
575 prev = rq->queuelist.prev;
576 if (prev != &q->queue_head && prev != &rq->queuelist)
577 return list_entry_rq(prev);
582 int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
585 elevator_t *e = q->elevator;
587 if (e->ops->elevator_set_req_fn)
588 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
590 rq->elevator_private = NULL;
594 void elv_put_request(request_queue_t *q, struct request *rq)
596 elevator_t *e = q->elevator;
598 if (e->ops->elevator_put_req_fn)
599 e->ops->elevator_put_req_fn(q, rq);
602 int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
604 elevator_t *e = q->elevator;
606 if (e->ops->elevator_may_queue_fn)
607 return e->ops->elevator_may_queue_fn(q, rw, bio);
609 return ELV_MQUEUE_MAY;
612 void elv_completed_request(request_queue_t *q, struct request *rq)
614 elevator_t *e = q->elevator;
617 * request is released from the driver, io must be done
619 if (blk_account_rq(rq)) {
621 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
622 e->ops->elevator_completed_req_fn(q, rq);
626 int elv_register_queue(struct request_queue *q)
628 elevator_t *e = q->elevator;
630 e->kobj.parent = kobject_get(&q->kobj);
634 snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
635 e->kobj.ktype = e->elevator_type->elevator_ktype;
637 return kobject_register(&e->kobj);
640 void elv_unregister_queue(struct request_queue *q)
643 elevator_t *e = q->elevator;
644 kobject_unregister(&e->kobj);
645 kobject_put(&q->kobj);
649 int elv_register(struct elevator_type *e)
651 spin_lock_irq(&elv_list_lock);
652 if (elevator_find(e->elevator_name))
654 list_add_tail(&e->list, &elv_list);
655 spin_unlock_irq(&elv_list_lock);
657 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
658 if (!strcmp(e->elevator_name, chosen_elevator))
659 printk(" (default)");
663 EXPORT_SYMBOL_GPL(elv_register);
665 void elv_unregister(struct elevator_type *e)
667 spin_lock_irq(&elv_list_lock);
668 list_del_init(&e->list);
669 spin_unlock_irq(&elv_list_lock);
671 EXPORT_SYMBOL_GPL(elv_unregister);
674 * switch to new_e io scheduler. be careful not to introduce deadlocks -
675 * we don't free the old io scheduler, before we have allocated what we
676 * need for the new one. this way we have a chance of going back to the old
677 * one, if the new one fails init for some reason. we also do an intermediate
678 * switch to noop to ensure safety with stack-allocated requests, since they
679 * don't originate from the block layer allocator. noop is safe here, because
680 * it never needs to touch the elevator itself for completion events. DRAIN
681 * flags will make sure we don't touch it for additions either.
683 static void elevator_switch(request_queue_t *q, struct elevator_type *new_e)
685 elevator_t *e = kmalloc(sizeof(elevator_t), GFP_KERNEL);
686 struct elevator_type *noop_elevator = NULL;
687 elevator_t *old_elevator;
693 * first step, drain requests from the block freelist
695 blk_wait_queue_drained(q, 0);
698 * unregister old elevator data
700 elv_unregister_queue(q);
701 old_elevator = q->elevator;
704 * next step, switch to noop since it uses no private rq structures
705 * and doesn't allocate any memory for anything. then wait for any
706 * non-fs requests in-flight
708 noop_elevator = elevator_get("noop");
709 spin_lock_irq(q->queue_lock);
710 elevator_attach(q, noop_elevator, e);
711 spin_unlock_irq(q->queue_lock);
713 blk_wait_queue_drained(q, 1);
716 * attach and start new elevator
718 if (elevator_attach(q, new_e, e))
721 if (elv_register_queue(q))
725 * finally exit old elevator and start queue again
727 elevator_exit(old_elevator);
728 blk_finish_queue_drain(q);
729 elevator_put(noop_elevator);
734 * switch failed, exit the new io scheduler and reattach the old
735 * one again (along with re-adding the sysfs dir)
739 q->elevator = old_elevator;
740 elv_register_queue(q);
741 blk_finish_queue_drain(q);
744 elevator_put(noop_elevator);
746 printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name);
749 ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
751 char elevator_name[ELV_NAME_MAX];
752 struct elevator_type *e;
754 memset(elevator_name, 0, sizeof(elevator_name));
755 strncpy(elevator_name, name, sizeof(elevator_name));
757 if (elevator_name[strlen(elevator_name) - 1] == '\n')
758 elevator_name[strlen(elevator_name) - 1] = '\0';
760 e = elevator_get(elevator_name);
762 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
766 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name))
769 elevator_switch(q, e);
773 ssize_t elv_iosched_show(request_queue_t *q, char *name)
775 elevator_t *e = q->elevator;
776 struct elevator_type *elv = e->elevator_type;
777 struct list_head *entry;
780 spin_lock_irq(q->queue_lock);
781 list_for_each(entry, &elv_list) {
782 struct elevator_type *__e;
784 __e = list_entry(entry, struct elevator_type, list);
785 if (!strcmp(elv->elevator_name, __e->elevator_name))
786 len += sprintf(name+len, "[%s] ", elv->elevator_name);
788 len += sprintf(name+len, "%s ", __e->elevator_name);
790 spin_unlock_irq(q->queue_lock);
792 len += sprintf(len+name, "\n");
796 EXPORT_SYMBOL(elv_dispatch_sort);
797 EXPORT_SYMBOL(elv_add_request);
798 EXPORT_SYMBOL(__elv_add_request);
799 EXPORT_SYMBOL(elv_requeue_request);
800 EXPORT_SYMBOL(elv_next_request);
801 EXPORT_SYMBOL(elv_dequeue_request);
802 EXPORT_SYMBOL(elv_queue_empty);
803 EXPORT_SYMBOL(elv_completed_request);
804 EXPORT_SYMBOL(elevator_exit);
805 EXPORT_SYMBOL(elevator_init);