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 spin_lock_irq(&elv_list_lock);
101 list_for_each(entry, &elv_list) {
102 struct elevator_type *__e;
104 __e = list_entry(entry, struct elevator_type, list);
106 if (!strcmp(__e->elevator_name, name)) {
111 spin_unlock_irq(&elv_list_lock);
116 static void elevator_put(struct elevator_type *e)
118 module_put(e->elevator_owner);
121 static struct elevator_type *elevator_get(const char *name)
123 struct elevator_type *e = elevator_find(name);
127 if (!try_module_get(e->elevator_owner))
133 static int elevator_attach(request_queue_t *q, struct elevator_type *e,
134 struct elevator_queue *eq)
138 memset(eq, 0, sizeof(*eq));
140 eq->elevator_type = e;
142 INIT_LIST_HEAD(&q->queue_head);
143 q->last_merge = NULL;
146 if (eq->ops->elevator_init_fn)
147 ret = eq->ops->elevator_init_fn(q, eq);
152 static char chosen_elevator[16];
154 static void elevator_setup_default(void)
157 * check if default is set and exists
159 if (chosen_elevator[0] && elevator_find(chosen_elevator))
162 #if defined(CONFIG_IOSCHED_AS)
163 strcpy(chosen_elevator, "anticipatory");
164 #elif defined(CONFIG_IOSCHED_DEADLINE)
165 strcpy(chosen_elevator, "deadline");
166 #elif defined(CONFIG_IOSCHED_CFQ)
167 strcpy(chosen_elevator, "cfq");
168 #elif defined(CONFIG_IOSCHED_NOOP)
169 strcpy(chosen_elevator, "noop");
171 #error "You must build at least 1 IO scheduler into the kernel"
175 static int __init elevator_setup(char *str)
177 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
181 __setup("elevator=", elevator_setup);
183 int elevator_init(request_queue_t *q, char *name)
185 struct elevator_type *e = NULL;
186 struct elevator_queue *eq;
189 elevator_setup_default();
192 name = chosen_elevator;
194 e = elevator_get(name);
198 eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL);
200 elevator_put(e->elevator_type);
204 ret = elevator_attach(q, e, eq);
207 elevator_put(e->elevator_type);
213 void elevator_exit(elevator_t *e)
215 if (e->ops->elevator_exit_fn)
216 e->ops->elevator_exit_fn(e);
218 elevator_put(e->elevator_type);
219 e->elevator_type = NULL;
223 int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
225 elevator_t *e = q->elevator;
227 if (e->ops->elevator_merge_fn)
228 return e->ops->elevator_merge_fn(q, req, bio);
230 return ELEVATOR_NO_MERGE;
233 void elv_merged_request(request_queue_t *q, struct request *rq)
235 elevator_t *e = q->elevator;
237 if (e->ops->elevator_merged_fn)
238 e->ops->elevator_merged_fn(q, rq);
241 void elv_merge_requests(request_queue_t *q, struct request *rq,
242 struct request *next)
244 elevator_t *e = q->elevator;
246 if (q->last_merge == next)
247 q->last_merge = NULL;
249 if (e->ops->elevator_merge_req_fn)
250 e->ops->elevator_merge_req_fn(q, rq, next);
254 * For careful internal use by the block layer. Essentially the same as
255 * a requeue in that it tells the io scheduler that this request is not
256 * active in the driver or hardware anymore, but we don't want the request
257 * added back to the scheduler. Function is not exported.
259 void elv_deactivate_request(request_queue_t *q, struct request *rq)
261 elevator_t *e = q->elevator;
264 * it already went through dequeue, we need to decrement the
265 * in_flight count again
267 if (blk_account_rq(rq))
270 rq->flags &= ~REQ_STARTED;
272 if (e->ops->elevator_deactivate_req_fn)
273 e->ops->elevator_deactivate_req_fn(q, rq);
276 void elv_requeue_request(request_queue_t *q, struct request *rq)
278 elv_deactivate_request(q, rq);
281 * if this is the flush, requeue the original instead and drop the flush
283 if (rq->flags & REQ_BAR_FLUSH) {
284 clear_bit(QUEUE_FLAG_FLUSH, &q->queue_flags);
285 rq = rq->end_io_data;
289 * the request is prepped and may have some resources allocated.
290 * allowing unprepped requests to pass this one may cause resource
291 * deadlock. turn on softbarrier.
293 rq->flags |= REQ_SOFTBARRIER;
296 * if iosched has an explicit requeue hook, then use that. otherwise
297 * just put the request at the front of the queue
299 if (q->elevator->ops->elevator_requeue_req_fn)
300 q->elevator->ops->elevator_requeue_req_fn(q, rq);
302 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
305 void __elv_add_request(request_queue_t *q, struct request *rq, int where,
309 * barriers implicitly indicate back insertion
311 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER) &&
312 where == ELEVATOR_INSERT_SORT)
313 where = ELEVATOR_INSERT_BACK;
320 if (!test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags)) {
321 q->elevator->ops->elevator_add_req_fn(q, rq, where);
323 if (blk_queue_plugged(q)) {
324 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
327 if (nrq >= q->unplug_thresh)
328 __generic_unplug_device(q);
332 * if drain is set, store the request "locally". when the drain
333 * is finished, the requests will be handed ordered to the io
336 list_add_tail(&rq->queuelist, &q->drain_list);
339 void elv_add_request(request_queue_t *q, struct request *rq, int where,
344 spin_lock_irqsave(q->queue_lock, flags);
345 __elv_add_request(q, rq, where, plug);
346 spin_unlock_irqrestore(q->queue_lock, flags);
349 static inline struct request *__elv_next_request(request_queue_t *q)
351 struct request *rq = q->elevator->ops->elevator_next_req_fn(q);
354 * if this is a barrier write and the device has to issue a
355 * flush sequence to support it, check how far we are
357 if (rq && blk_fs_request(rq) && blk_barrier_rq(rq)) {
358 BUG_ON(q->ordered == QUEUE_ORDERED_NONE);
360 if (q->ordered == QUEUE_ORDERED_FLUSH &&
361 !blk_barrier_preflush(rq))
362 rq = blk_start_pre_flush(q, rq);
368 struct request *elv_next_request(request_queue_t *q)
373 while ((rq = __elv_next_request(q)) != NULL) {
375 * just mark as started even if we don't start it, a request
376 * that has been delayed should not be passed by new incoming
379 rq->flags |= REQ_STARTED;
381 if (rq == q->last_merge)
382 q->last_merge = NULL;
384 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
387 ret = q->prep_rq_fn(q, rq);
388 if (ret == BLKPREP_OK) {
390 } else if (ret == BLKPREP_DEFER) {
392 * the request may have been (partially) prepped.
393 * we need to keep this request in the front to
394 * avoid resource deadlock. turn on softbarrier.
396 rq->flags |= REQ_SOFTBARRIER;
399 } else if (ret == BLKPREP_KILL) {
400 int nr_bytes = rq->hard_nr_sectors << 9;
403 nr_bytes = rq->data_len;
405 blkdev_dequeue_request(rq);
406 rq->flags |= REQ_QUIET;
407 end_that_request_chunk(rq, 0, nr_bytes);
408 end_that_request_last(rq);
410 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
419 void elv_remove_request(request_queue_t *q, struct request *rq)
421 elevator_t *e = q->elevator;
424 * the time frame between a request being removed from the lists
425 * and to it is freed is accounted as io that is in progress at
426 * the driver side. note that we only account requests that the
427 * driver has seen (REQ_STARTED set), to avoid false accounting
428 * for request-request merges
430 if (blk_account_rq(rq))
434 * the main clearing point for q->last_merge is on retrieval of
435 * request by driver (it calls elv_next_request()), but it _can_
436 * also happen here if a request is added to the queue but later
437 * deleted without ever being given to driver (merged with another
440 if (rq == q->last_merge)
441 q->last_merge = NULL;
443 if (e->ops->elevator_remove_req_fn)
444 e->ops->elevator_remove_req_fn(q, rq);
447 int elv_queue_empty(request_queue_t *q)
449 elevator_t *e = q->elevator;
451 if (e->ops->elevator_queue_empty_fn)
452 return e->ops->elevator_queue_empty_fn(q);
454 return list_empty(&q->queue_head);
457 struct request *elv_latter_request(request_queue_t *q, struct request *rq)
459 struct list_head *next;
461 elevator_t *e = q->elevator;
463 if (e->ops->elevator_latter_req_fn)
464 return e->ops->elevator_latter_req_fn(q, rq);
466 next = rq->queuelist.next;
467 if (next != &q->queue_head && next != &rq->queuelist)
468 return list_entry_rq(next);
473 struct request *elv_former_request(request_queue_t *q, struct request *rq)
475 struct list_head *prev;
477 elevator_t *e = q->elevator;
479 if (e->ops->elevator_former_req_fn)
480 return e->ops->elevator_former_req_fn(q, rq);
482 prev = rq->queuelist.prev;
483 if (prev != &q->queue_head && prev != &rq->queuelist)
484 return list_entry_rq(prev);
489 int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
492 elevator_t *e = q->elevator;
494 if (e->ops->elevator_set_req_fn)
495 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
497 rq->elevator_private = NULL;
501 void elv_put_request(request_queue_t *q, struct request *rq)
503 elevator_t *e = q->elevator;
505 if (e->ops->elevator_put_req_fn)
506 e->ops->elevator_put_req_fn(q, rq);
509 int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
511 elevator_t *e = q->elevator;
513 if (e->ops->elevator_may_queue_fn)
514 return e->ops->elevator_may_queue_fn(q, rw, bio);
516 return ELV_MQUEUE_MAY;
519 void elv_completed_request(request_queue_t *q, struct request *rq)
521 elevator_t *e = q->elevator;
524 * request is released from the driver, io must be done
526 if (blk_account_rq(rq))
529 if (e->ops->elevator_completed_req_fn)
530 e->ops->elevator_completed_req_fn(q, rq);
533 int elv_register_queue(struct request_queue *q)
535 elevator_t *e = q->elevator;
537 e->kobj.parent = kobject_get(&q->kobj);
541 snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
542 e->kobj.ktype = e->elevator_type->elevator_ktype;
544 return kobject_register(&e->kobj);
547 void elv_unregister_queue(struct request_queue *q)
550 elevator_t *e = q->elevator;
551 kobject_unregister(&e->kobj);
552 kobject_put(&q->kobj);
556 int elv_register(struct elevator_type *e)
558 if (elevator_find(e->elevator_name))
561 spin_lock_irq(&elv_list_lock);
562 list_add_tail(&e->list, &elv_list);
563 spin_unlock_irq(&elv_list_lock);
565 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
566 if (!strcmp(e->elevator_name, chosen_elevator))
567 printk(" (default)");
571 EXPORT_SYMBOL_GPL(elv_register);
573 void elv_unregister(struct elevator_type *e)
575 spin_lock_irq(&elv_list_lock);
576 list_del_init(&e->list);
577 spin_unlock_irq(&elv_list_lock);
579 EXPORT_SYMBOL_GPL(elv_unregister);
582 * switch to new_e io scheduler. be careful not to introduce deadlocks -
583 * we don't free the old io scheduler, before we have allocated what we
584 * need for the new one. this way we have a chance of going back to the old
585 * one, if the new one fails init for some reason. we also do an intermediate
586 * switch to noop to ensure safety with stack-allocated requests, since they
587 * don't originate from the block layer allocator. noop is safe here, because
588 * it never needs to touch the elevator itself for completion events. DRAIN
589 * flags will make sure we don't touch it for additions either.
591 static void elevator_switch(request_queue_t *q, struct elevator_type *new_e)
593 elevator_t *e = kmalloc(sizeof(elevator_t), GFP_KERNEL);
594 struct elevator_type *noop_elevator = NULL;
595 elevator_t *old_elevator;
601 * first step, drain requests from the block freelist
603 blk_wait_queue_drained(q, 0);
606 * unregister old elevator data
608 elv_unregister_queue(q);
609 old_elevator = q->elevator;
612 * next step, switch to noop since it uses no private rq structures
613 * and doesn't allocate any memory for anything. then wait for any
614 * non-fs requests in-flight
616 noop_elevator = elevator_get("noop");
617 spin_lock_irq(q->queue_lock);
618 elevator_attach(q, noop_elevator, e);
619 spin_unlock_irq(q->queue_lock);
621 blk_wait_queue_drained(q, 1);
624 * attach and start new elevator
626 if (elevator_attach(q, new_e, e))
629 if (elv_register_queue(q))
633 * finally exit old elevator and start queue again
635 elevator_exit(old_elevator);
636 blk_finish_queue_drain(q);
637 elevator_put(noop_elevator);
642 * switch failed, exit the new io scheduler and reattach the old
643 * one again (along with re-adding the sysfs dir)
647 q->elevator = old_elevator;
648 elv_register_queue(q);
649 blk_finish_queue_drain(q);
652 elevator_put(noop_elevator);
654 printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name);
657 ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
659 char elevator_name[ELV_NAME_MAX];
660 struct elevator_type *e;
662 memset(elevator_name, 0, sizeof(elevator_name));
663 strncpy(elevator_name, name, sizeof(elevator_name));
665 if (elevator_name[strlen(elevator_name) - 1] == '\n')
666 elevator_name[strlen(elevator_name) - 1] = '\0';
668 e = elevator_get(elevator_name);
670 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
674 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name))
677 elevator_switch(q, e);
681 ssize_t elv_iosched_show(request_queue_t *q, char *name)
683 elevator_t *e = q->elevator;
684 struct elevator_type *elv = e->elevator_type;
685 struct list_head *entry;
688 spin_lock_irq(q->queue_lock);
689 list_for_each(entry, &elv_list) {
690 struct elevator_type *__e;
692 __e = list_entry(entry, struct elevator_type, list);
693 if (!strcmp(elv->elevator_name, __e->elevator_name))
694 len += sprintf(name+len, "[%s] ", elv->elevator_name);
696 len += sprintf(name+len, "%s ", __e->elevator_name);
698 spin_unlock_irq(q->queue_lock);
700 len += sprintf(len+name, "\n");
704 EXPORT_SYMBOL(elv_add_request);
705 EXPORT_SYMBOL(__elv_add_request);
706 EXPORT_SYMBOL(elv_requeue_request);
707 EXPORT_SYMBOL(elv_next_request);
708 EXPORT_SYMBOL(elv_remove_request);
709 EXPORT_SYMBOL(elv_queue_empty);
710 EXPORT_SYMBOL(elv_completed_request);
711 EXPORT_SYMBOL(elevator_exit);
712 EXPORT_SYMBOL(elevator_init);