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 static int elevator_global_init(void)
228 int elv_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
230 elevator_t
*e
= q
->elevator
;
232 if (e
->ops
->elevator_merge_fn
)
233 return e
->ops
->elevator_merge_fn(q
, req
, bio
);
235 return ELEVATOR_NO_MERGE
;
238 void elv_merged_request(request_queue_t
*q
, struct request
*rq
)
240 elevator_t
*e
= q
->elevator
;
242 if (e
->ops
->elevator_merged_fn
)
243 e
->ops
->elevator_merged_fn(q
, rq
);
246 void elv_merge_requests(request_queue_t
*q
, struct request
*rq
,
247 struct request
*next
)
249 elevator_t
*e
= q
->elevator
;
251 if (q
->last_merge
== next
)
252 q
->last_merge
= NULL
;
254 if (e
->ops
->elevator_merge_req_fn
)
255 e
->ops
->elevator_merge_req_fn(q
, rq
, next
);
259 * For careful internal use by the block layer. Essentially the same as
260 * a requeue in that it tells the io scheduler that this request is not
261 * active in the driver or hardware anymore, but we don't want the request
262 * added back to the scheduler. Function is not exported.
264 void elv_deactivate_request(request_queue_t
*q
, struct request
*rq
)
266 elevator_t
*e
= q
->elevator
;
269 * it already went through dequeue, we need to decrement the
270 * in_flight count again
272 if (blk_account_rq(rq
))
275 rq
->flags
&= ~REQ_STARTED
;
277 if (e
->ops
->elevator_deactivate_req_fn
)
278 e
->ops
->elevator_deactivate_req_fn(q
, rq
);
281 void elv_requeue_request(request_queue_t
*q
, struct request
*rq
)
283 elv_deactivate_request(q
, rq
);
286 * if this is the flush, requeue the original instead and drop the flush
288 if (rq
->flags
& REQ_BAR_FLUSH
) {
289 clear_bit(QUEUE_FLAG_FLUSH
, &q
->queue_flags
);
290 rq
= rq
->end_io_data
;
294 * if iosched has an explicit requeue hook, then use that. otherwise
295 * just put the request at the front of the queue
297 if (q
->elevator
->ops
->elevator_requeue_req_fn
)
298 q
->elevator
->ops
->elevator_requeue_req_fn(q
, rq
);
300 __elv_add_request(q
, rq
, ELEVATOR_INSERT_FRONT
, 0);
303 void __elv_add_request(request_queue_t
*q
, struct request
*rq
, int where
,
307 * barriers implicitly indicate back insertion
309 if (rq
->flags
& (REQ_SOFTBARRIER
| REQ_HARDBARRIER
) &&
310 where
== ELEVATOR_INSERT_SORT
)
311 where
= ELEVATOR_INSERT_BACK
;
318 if (!test_bit(QUEUE_FLAG_DRAIN
, &q
->queue_flags
)) {
319 q
->elevator
->ops
->elevator_add_req_fn(q
, rq
, where
);
321 if (blk_queue_plugged(q
)) {
322 int nrq
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]
325 if (nrq
== q
->unplug_thresh
)
326 __generic_unplug_device(q
);
330 * if drain is set, store the request "locally". when the drain
331 * is finished, the requests will be handed ordered to the io
334 list_add_tail(&rq
->queuelist
, &q
->drain_list
);
337 void elv_add_request(request_queue_t
*q
, struct request
*rq
, int where
,
342 spin_lock_irqsave(q
->queue_lock
, flags
);
343 __elv_add_request(q
, rq
, where
, plug
);
344 spin_unlock_irqrestore(q
->queue_lock
, flags
);
347 static inline struct request
*__elv_next_request(request_queue_t
*q
)
349 struct request
*rq
= q
->elevator
->ops
->elevator_next_req_fn(q
);
352 * if this is a barrier write and the device has to issue a
353 * flush sequence to support it, check how far we are
355 if (rq
&& blk_fs_request(rq
) && blk_barrier_rq(rq
)) {
356 BUG_ON(q
->ordered
== QUEUE_ORDERED_NONE
);
358 if (q
->ordered
== QUEUE_ORDERED_FLUSH
&&
359 !blk_barrier_preflush(rq
))
360 rq
= blk_start_pre_flush(q
, rq
);
366 struct request
*elv_next_request(request_queue_t
*q
)
371 while ((rq
= __elv_next_request(q
)) != NULL
) {
373 * just mark as started even if we don't start it, a request
374 * that has been delayed should not be passed by new incoming
377 rq
->flags
|= REQ_STARTED
;
379 if (rq
== q
->last_merge
)
380 q
->last_merge
= NULL
;
382 if ((rq
->flags
& REQ_DONTPREP
) || !q
->prep_rq_fn
)
385 ret
= q
->prep_rq_fn(q
, rq
);
386 if (ret
== BLKPREP_OK
) {
388 } else if (ret
== BLKPREP_DEFER
) {
391 } else if (ret
== BLKPREP_KILL
) {
392 int nr_bytes
= rq
->hard_nr_sectors
<< 9;
395 nr_bytes
= rq
->data_len
;
397 blkdev_dequeue_request(rq
);
398 rq
->flags
|= REQ_QUIET
;
399 end_that_request_chunk(rq
, 0, nr_bytes
);
400 end_that_request_last(rq
);
402 printk(KERN_ERR
"%s: bad return=%d\n", __FUNCTION__
,
411 void elv_remove_request(request_queue_t
*q
, struct request
*rq
)
413 elevator_t
*e
= q
->elevator
;
416 * the time frame between a request being removed from the lists
417 * and to it is freed is accounted as io that is in progress at
418 * the driver side. note that we only account requests that the
419 * driver has seen (REQ_STARTED set), to avoid false accounting
420 * for request-request merges
422 if (blk_account_rq(rq
))
426 * the main clearing point for q->last_merge is on retrieval of
427 * request by driver (it calls elv_next_request()), but it _can_
428 * also happen here if a request is added to the queue but later
429 * deleted without ever being given to driver (merged with another
432 if (rq
== q
->last_merge
)
433 q
->last_merge
= NULL
;
435 if (e
->ops
->elevator_remove_req_fn
)
436 e
->ops
->elevator_remove_req_fn(q
, rq
);
439 int elv_queue_empty(request_queue_t
*q
)
441 elevator_t
*e
= q
->elevator
;
443 if (e
->ops
->elevator_queue_empty_fn
)
444 return e
->ops
->elevator_queue_empty_fn(q
);
446 return list_empty(&q
->queue_head
);
449 struct request
*elv_latter_request(request_queue_t
*q
, struct request
*rq
)
451 struct list_head
*next
;
453 elevator_t
*e
= q
->elevator
;
455 if (e
->ops
->elevator_latter_req_fn
)
456 return e
->ops
->elevator_latter_req_fn(q
, rq
);
458 next
= rq
->queuelist
.next
;
459 if (next
!= &q
->queue_head
&& next
!= &rq
->queuelist
)
460 return list_entry_rq(next
);
465 struct request
*elv_former_request(request_queue_t
*q
, struct request
*rq
)
467 struct list_head
*prev
;
469 elevator_t
*e
= q
->elevator
;
471 if (e
->ops
->elevator_former_req_fn
)
472 return e
->ops
->elevator_former_req_fn(q
, rq
);
474 prev
= rq
->queuelist
.prev
;
475 if (prev
!= &q
->queue_head
&& prev
!= &rq
->queuelist
)
476 return list_entry_rq(prev
);
481 int elv_set_request(request_queue_t
*q
, struct request
*rq
, int gfp_mask
)
483 elevator_t
*e
= q
->elevator
;
485 if (e
->ops
->elevator_set_req_fn
)
486 return e
->ops
->elevator_set_req_fn(q
, rq
, gfp_mask
);
488 rq
->elevator_private
= NULL
;
492 void elv_put_request(request_queue_t
*q
, struct request
*rq
)
494 elevator_t
*e
= q
->elevator
;
496 if (e
->ops
->elevator_put_req_fn
)
497 e
->ops
->elevator_put_req_fn(q
, rq
);
500 int elv_may_queue(request_queue_t
*q
, int rw
)
502 elevator_t
*e
= q
->elevator
;
504 if (e
->ops
->elevator_may_queue_fn
)
505 return e
->ops
->elevator_may_queue_fn(q
, rw
);
507 return ELV_MQUEUE_MAY
;
510 void elv_completed_request(request_queue_t
*q
, struct request
*rq
)
512 elevator_t
*e
= q
->elevator
;
515 * request is released from the driver, io must be done
517 if (blk_account_rq(rq
))
520 if (e
->ops
->elevator_completed_req_fn
)
521 e
->ops
->elevator_completed_req_fn(q
, rq
);
524 int elv_register_queue(struct request_queue
*q
)
526 elevator_t
*e
= q
->elevator
;
528 e
->kobj
.parent
= kobject_get(&q
->kobj
);
532 snprintf(e
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "iosched");
533 e
->kobj
.ktype
= e
->elevator_type
->elevator_ktype
;
535 return kobject_register(&e
->kobj
);
538 void elv_unregister_queue(struct request_queue
*q
)
541 elevator_t
*e
= q
->elevator
;
542 kobject_unregister(&e
->kobj
);
543 kobject_put(&q
->kobj
);
547 int elv_register(struct elevator_type
*e
)
549 if (elevator_find(e
->elevator_name
))
552 spin_lock_irq(&elv_list_lock
);
553 list_add_tail(&e
->list
, &elv_list
);
554 spin_unlock_irq(&elv_list_lock
);
556 printk(KERN_INFO
"io scheduler %s registered", e
->elevator_name
);
557 if (!strcmp(e
->elevator_name
, chosen_elevator
))
558 printk(" (default)");
562 EXPORT_SYMBOL_GPL(elv_register
);
564 void elv_unregister(struct elevator_type
*e
)
566 spin_lock_irq(&elv_list_lock
);
567 list_del_init(&e
->list
);
568 spin_unlock_irq(&elv_list_lock
);
570 EXPORT_SYMBOL_GPL(elv_unregister
);
573 * switch to new_e io scheduler. be careful not to introduce deadlocks -
574 * we don't free the old io scheduler, before we have allocated what we
575 * need for the new one. this way we have a chance of going back to the old
576 * one, if the new one fails init for some reason. we also do an intermediate
577 * switch to noop to ensure safety with stack-allocated requests, since they
578 * don't originate from the block layer allocator. noop is safe here, because
579 * it never needs to touch the elevator itself for completion events. DRAIN
580 * flags will make sure we don't touch it for additions either.
582 static void elevator_switch(request_queue_t
*q
, struct elevator_type
*new_e
)
584 elevator_t
*e
= kmalloc(sizeof(elevator_t
), GFP_KERNEL
);
585 struct elevator_type
*noop_elevator
= NULL
;
586 elevator_t
*old_elevator
;
592 * first step, drain requests from the block freelist
594 blk_wait_queue_drained(q
, 0);
597 * unregister old elevator data
599 elv_unregister_queue(q
);
600 old_elevator
= q
->elevator
;
603 * next step, switch to noop since it uses no private rq structures
604 * and doesn't allocate any memory for anything. then wait for any
605 * non-fs requests in-flight
607 noop_elevator
= elevator_get("noop");
608 spin_lock_irq(q
->queue_lock
);
609 elevator_attach(q
, noop_elevator
, e
);
610 spin_unlock_irq(q
->queue_lock
);
612 blk_wait_queue_drained(q
, 1);
615 * attach and start new elevator
617 if (elevator_attach(q
, new_e
, e
))
620 if (elv_register_queue(q
))
624 * finally exit old elevator and start queue again
626 elevator_exit(old_elevator
);
627 blk_finish_queue_drain(q
);
628 elevator_put(noop_elevator
);
633 * switch failed, exit the new io scheduler and reattach the old
634 * one again (along with re-adding the sysfs dir)
638 q
->elevator
= old_elevator
;
639 elv_register_queue(q
);
640 blk_finish_queue_drain(q
);
643 elevator_put(noop_elevator
);
645 printk(KERN_ERR
"elevator: switch to %s failed\n",new_e
->elevator_name
);
648 ssize_t
elv_iosched_store(request_queue_t
*q
, const char *name
, size_t count
)
650 char elevator_name
[ELV_NAME_MAX
];
651 struct elevator_type
*e
;
653 memset(elevator_name
, 0, sizeof(elevator_name
));
654 strncpy(elevator_name
, name
, sizeof(elevator_name
));
656 if (elevator_name
[strlen(elevator_name
) - 1] == '\n')
657 elevator_name
[strlen(elevator_name
) - 1] = '\0';
659 e
= elevator_get(elevator_name
);
661 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
665 if (!strcmp(elevator_name
, q
->elevator
->elevator_type
->elevator_name
))
668 elevator_switch(q
, e
);
672 ssize_t
elv_iosched_show(request_queue_t
*q
, char *name
)
674 elevator_t
*e
= q
->elevator
;
675 struct elevator_type
*elv
= e
->elevator_type
;
676 struct list_head
*entry
;
679 spin_lock_irq(q
->queue_lock
);
680 list_for_each(entry
, &elv_list
) {
681 struct elevator_type
*__e
;
683 __e
= list_entry(entry
, struct elevator_type
, list
);
684 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
685 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
687 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
689 spin_unlock_irq(q
->queue_lock
);
691 len
+= sprintf(len
+name
, "\n");
695 module_init(elevator_global_init
);
697 EXPORT_SYMBOL(elv_add_request
);
698 EXPORT_SYMBOL(__elv_add_request
);
699 EXPORT_SYMBOL(elv_requeue_request
);
700 EXPORT_SYMBOL(elv_next_request
);
701 EXPORT_SYMBOL(elv_remove_request
);
702 EXPORT_SYMBOL(elv_queue_empty
);
703 EXPORT_SYMBOL(elv_completed_request
);
704 EXPORT_SYMBOL(elevator_exit
);
705 EXPORT_SYMBOL(elevator_init
);