DebugFS : more file/directory creation error handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / elevator.c
blobc0063f345c5d620320cd82438394eefe71b442dc
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) (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
49 #define ELV_HASH_ENTRIES (1 << elv_hash_shift)
50 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
51 #define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
54 * can we safely merge with this request?
56 inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
58 if (!rq_mergeable(rq))
59 return 0;
62 * different data direction or already started, don't merge
64 if (bio_data_dir(bio) != rq_data_dir(rq))
65 return 0;
68 * same device and no special stuff set, merge is ok
70 if (rq->rq_disk == bio->bi_bdev->bd_disk && !rq->special)
71 return 1;
73 return 0;
75 EXPORT_SYMBOL(elv_rq_merge_ok);
77 static inline int elv_try_merge(struct request *__rq, struct bio *bio)
79 int ret = ELEVATOR_NO_MERGE;
82 * we can merge and sequence is ok, check if it's possible
84 if (elv_rq_merge_ok(__rq, bio)) {
85 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
86 ret = ELEVATOR_BACK_MERGE;
87 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
88 ret = ELEVATOR_FRONT_MERGE;
91 return ret;
94 static struct elevator_type *elevator_find(const char *name)
96 struct elevator_type *e;
97 struct list_head *entry;
99 list_for_each(entry, &elv_list) {
101 e = list_entry(entry, struct elevator_type, list);
103 if (!strcmp(e->elevator_name, name))
104 return e;
107 return NULL;
110 static void elevator_put(struct elevator_type *e)
112 module_put(e->elevator_owner);
115 static struct elevator_type *elevator_get(const char *name)
117 struct elevator_type *e;
119 spin_lock_irq(&elv_list_lock);
121 e = elevator_find(name);
122 if (e && !try_module_get(e->elevator_owner))
123 e = NULL;
125 spin_unlock_irq(&elv_list_lock);
127 return e;
130 static void *elevator_init_queue(request_queue_t *q, struct elevator_queue *eq)
132 return eq->ops->elevator_init_fn(q);
135 static void elevator_attach(request_queue_t *q, struct elevator_queue *eq,
136 void *data)
138 q->elevator = eq;
139 eq->elevator_data = data;
142 static char chosen_elevator[16];
144 static int __init elevator_setup(char *str)
147 * Be backwards-compatible with previous kernels, so users
148 * won't get the wrong elevator.
150 if (!strcmp(str, "as"))
151 strcpy(chosen_elevator, "anticipatory");
152 else
153 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
154 return 1;
157 __setup("elevator=", elevator_setup);
159 static struct kobj_type elv_ktype;
161 static elevator_t *elevator_alloc(request_queue_t *q, struct elevator_type *e)
163 elevator_t *eq;
164 int i;
166 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL, q->node);
167 if (unlikely(!eq))
168 goto err;
170 memset(eq, 0, sizeof(*eq));
171 eq->ops = &e->ops;
172 eq->elevator_type = e;
173 kobject_init(&eq->kobj);
174 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
175 eq->kobj.ktype = &elv_ktype;
176 mutex_init(&eq->sysfs_lock);
178 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
179 GFP_KERNEL, q->node);
180 if (!eq->hash)
181 goto err;
183 for (i = 0; i < ELV_HASH_ENTRIES; i++)
184 INIT_HLIST_HEAD(&eq->hash[i]);
186 return eq;
187 err:
188 kfree(eq);
189 elevator_put(e);
190 return NULL;
193 static void elevator_release(struct kobject *kobj)
195 elevator_t *e = container_of(kobj, elevator_t, kobj);
197 elevator_put(e->elevator_type);
198 kfree(e->hash);
199 kfree(e);
202 int elevator_init(request_queue_t *q, char *name)
204 struct elevator_type *e = NULL;
205 struct elevator_queue *eq;
206 int ret = 0;
207 void *data;
209 INIT_LIST_HEAD(&q->queue_head);
210 q->last_merge = NULL;
211 q->end_sector = 0;
212 q->boundary_rq = NULL;
214 if (name && !(e = elevator_get(name)))
215 return -EINVAL;
217 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
218 printk("I/O scheduler %s not found\n", chosen_elevator);
220 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
221 printk("Default I/O scheduler not found, using no-op\n");
222 e = elevator_get("noop");
225 eq = elevator_alloc(q, e);
226 if (!eq)
227 return -ENOMEM;
229 data = elevator_init_queue(q, eq);
230 if (!data) {
231 kobject_put(&eq->kobj);
232 return -ENOMEM;
235 elevator_attach(q, eq, data);
236 return ret;
239 EXPORT_SYMBOL(elevator_init);
241 void elevator_exit(elevator_t *e)
243 mutex_lock(&e->sysfs_lock);
244 if (e->ops->elevator_exit_fn)
245 e->ops->elevator_exit_fn(e);
246 e->ops = NULL;
247 mutex_unlock(&e->sysfs_lock);
249 kobject_put(&e->kobj);
252 EXPORT_SYMBOL(elevator_exit);
254 static inline void __elv_rqhash_del(struct request *rq)
256 hlist_del_init(&rq->hash);
259 static void elv_rqhash_del(request_queue_t *q, struct request *rq)
261 if (ELV_ON_HASH(rq))
262 __elv_rqhash_del(rq);
265 static void elv_rqhash_add(request_queue_t *q, struct request *rq)
267 elevator_t *e = q->elevator;
269 BUG_ON(ELV_ON_HASH(rq));
270 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
273 static void elv_rqhash_reposition(request_queue_t *q, struct request *rq)
275 __elv_rqhash_del(rq);
276 elv_rqhash_add(q, rq);
279 static struct request *elv_rqhash_find(request_queue_t *q, sector_t offset)
281 elevator_t *e = q->elevator;
282 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
283 struct hlist_node *entry, *next;
284 struct request *rq;
286 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
287 BUG_ON(!ELV_ON_HASH(rq));
289 if (unlikely(!rq_mergeable(rq))) {
290 __elv_rqhash_del(rq);
291 continue;
294 if (rq_hash_key(rq) == offset)
295 return rq;
298 return NULL;
302 * RB-tree support functions for inserting/lookup/removal of requests
303 * in a sorted RB tree.
305 struct request *elv_rb_add(struct rb_root *root, struct request *rq)
307 struct rb_node **p = &root->rb_node;
308 struct rb_node *parent = NULL;
309 struct request *__rq;
311 while (*p) {
312 parent = *p;
313 __rq = rb_entry(parent, struct request, rb_node);
315 if (rq->sector < __rq->sector)
316 p = &(*p)->rb_left;
317 else if (rq->sector > __rq->sector)
318 p = &(*p)->rb_right;
319 else
320 return __rq;
323 rb_link_node(&rq->rb_node, parent, p);
324 rb_insert_color(&rq->rb_node, root);
325 return NULL;
328 EXPORT_SYMBOL(elv_rb_add);
330 void elv_rb_del(struct rb_root *root, struct request *rq)
332 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
333 rb_erase(&rq->rb_node, root);
334 RB_CLEAR_NODE(&rq->rb_node);
337 EXPORT_SYMBOL(elv_rb_del);
339 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
341 struct rb_node *n = root->rb_node;
342 struct request *rq;
344 while (n) {
345 rq = rb_entry(n, struct request, rb_node);
347 if (sector < rq->sector)
348 n = n->rb_left;
349 else if (sector > rq->sector)
350 n = n->rb_right;
351 else
352 return rq;
355 return NULL;
358 EXPORT_SYMBOL(elv_rb_find);
361 * Insert rq into dispatch queue of q. Queue lock must be held on
362 * entry. rq is sort insted into the dispatch queue. To be used by
363 * specific elevators.
365 void elv_dispatch_sort(request_queue_t *q, struct request *rq)
367 sector_t boundary;
368 struct list_head *entry;
370 if (q->last_merge == rq)
371 q->last_merge = NULL;
373 elv_rqhash_del(q, rq);
375 q->nr_sorted--;
377 boundary = q->end_sector;
379 list_for_each_prev(entry, &q->queue_head) {
380 struct request *pos = list_entry_rq(entry);
382 if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
383 break;
384 if (rq->sector >= boundary) {
385 if (pos->sector < boundary)
386 continue;
387 } else {
388 if (pos->sector >= boundary)
389 break;
391 if (rq->sector >= pos->sector)
392 break;
395 list_add(&rq->queuelist, entry);
398 EXPORT_SYMBOL(elv_dispatch_sort);
401 * Insert rq into dispatch queue of q. Queue lock must be held on
402 * entry. rq is added to the back of the dispatch queue. To be used by
403 * specific elevators.
405 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
407 if (q->last_merge == rq)
408 q->last_merge = NULL;
410 elv_rqhash_del(q, rq);
412 q->nr_sorted--;
414 q->end_sector = rq_end_sector(rq);
415 q->boundary_rq = rq;
416 list_add_tail(&rq->queuelist, &q->queue_head);
419 EXPORT_SYMBOL(elv_dispatch_add_tail);
421 int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
423 elevator_t *e = q->elevator;
424 struct request *__rq;
425 int ret;
428 * First try one-hit cache.
430 if (q->last_merge) {
431 ret = elv_try_merge(q->last_merge, bio);
432 if (ret != ELEVATOR_NO_MERGE) {
433 *req = q->last_merge;
434 return ret;
439 * See if our hash lookup can find a potential backmerge.
441 __rq = elv_rqhash_find(q, bio->bi_sector);
442 if (__rq && elv_rq_merge_ok(__rq, bio)) {
443 *req = __rq;
444 return ELEVATOR_BACK_MERGE;
447 if (e->ops->elevator_merge_fn)
448 return e->ops->elevator_merge_fn(q, req, bio);
450 return ELEVATOR_NO_MERGE;
453 void elv_merged_request(request_queue_t *q, struct request *rq, int type)
455 elevator_t *e = q->elevator;
457 if (e->ops->elevator_merged_fn)
458 e->ops->elevator_merged_fn(q, rq, type);
460 if (type == ELEVATOR_BACK_MERGE)
461 elv_rqhash_reposition(q, rq);
463 q->last_merge = rq;
466 void elv_merge_requests(request_queue_t *q, struct request *rq,
467 struct request *next)
469 elevator_t *e = q->elevator;
471 if (e->ops->elevator_merge_req_fn)
472 e->ops->elevator_merge_req_fn(q, rq, next);
474 elv_rqhash_reposition(q, rq);
475 elv_rqhash_del(q, next);
477 q->nr_sorted--;
478 q->last_merge = rq;
481 void elv_requeue_request(request_queue_t *q, struct request *rq)
483 elevator_t *e = q->elevator;
486 * it already went through dequeue, we need to decrement the
487 * in_flight count again
489 if (blk_account_rq(rq)) {
490 q->in_flight--;
491 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
492 e->ops->elevator_deactivate_req_fn(q, rq);
495 rq->cmd_flags &= ~REQ_STARTED;
497 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
500 static void elv_drain_elevator(request_queue_t *q)
502 static int printed;
503 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
505 if (q->nr_sorted == 0)
506 return;
507 if (printed++ < 10) {
508 printk(KERN_ERR "%s: forced dispatching is broken "
509 "(nr_sorted=%u), please report this\n",
510 q->elevator->elevator_type->elevator_name, q->nr_sorted);
514 void elv_insert(request_queue_t *q, struct request *rq, int where)
516 struct list_head *pos;
517 unsigned ordseq;
518 int unplug_it = 1;
520 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
522 rq->q = q;
524 switch (where) {
525 case ELEVATOR_INSERT_FRONT:
526 rq->cmd_flags |= REQ_SOFTBARRIER;
528 list_add(&rq->queuelist, &q->queue_head);
529 break;
531 case ELEVATOR_INSERT_BACK:
532 rq->cmd_flags |= REQ_SOFTBARRIER;
533 elv_drain_elevator(q);
534 list_add_tail(&rq->queuelist, &q->queue_head);
536 * We kick the queue here for the following reasons.
537 * - The elevator might have returned NULL previously
538 * to delay requests and returned them now. As the
539 * queue wasn't empty before this request, ll_rw_blk
540 * won't run the queue on return, resulting in hang.
541 * - Usually, back inserted requests won't be merged
542 * with anything. There's no point in delaying queue
543 * processing.
545 blk_remove_plug(q);
546 q->request_fn(q);
547 break;
549 case ELEVATOR_INSERT_SORT:
550 BUG_ON(!blk_fs_request(rq));
551 rq->cmd_flags |= REQ_SORTED;
552 q->nr_sorted++;
553 if (rq_mergeable(rq)) {
554 elv_rqhash_add(q, rq);
555 if (!q->last_merge)
556 q->last_merge = rq;
560 * Some ioscheds (cfq) run q->request_fn directly, so
561 * rq cannot be accessed after calling
562 * elevator_add_req_fn.
564 q->elevator->ops->elevator_add_req_fn(q, rq);
565 break;
567 case ELEVATOR_INSERT_REQUEUE:
569 * If ordered flush isn't in progress, we do front
570 * insertion; otherwise, requests should be requeued
571 * in ordseq order.
573 rq->cmd_flags |= REQ_SOFTBARRIER;
575 if (q->ordseq == 0) {
576 list_add(&rq->queuelist, &q->queue_head);
577 break;
580 ordseq = blk_ordered_req_seq(rq);
582 list_for_each(pos, &q->queue_head) {
583 struct request *pos_rq = list_entry_rq(pos);
584 if (ordseq <= blk_ordered_req_seq(pos_rq))
585 break;
588 list_add_tail(&rq->queuelist, pos);
590 * most requeues happen because of a busy condition, don't
591 * force unplug of the queue for that case.
593 unplug_it = 0;
594 break;
596 default:
597 printk(KERN_ERR "%s: bad insertion point %d\n",
598 __FUNCTION__, where);
599 BUG();
602 if (unplug_it && blk_queue_plugged(q)) {
603 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
604 - q->in_flight;
606 if (nrq >= q->unplug_thresh)
607 __generic_unplug_device(q);
611 void __elv_add_request(request_queue_t *q, struct request *rq, int where,
612 int plug)
614 if (q->ordcolor)
615 rq->cmd_flags |= REQ_ORDERED_COLOR;
617 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
619 * toggle ordered color
621 if (blk_barrier_rq(rq))
622 q->ordcolor ^= 1;
625 * barriers implicitly indicate back insertion
627 if (where == ELEVATOR_INSERT_SORT)
628 where = ELEVATOR_INSERT_BACK;
631 * this request is scheduling boundary, update
632 * end_sector
634 if (blk_fs_request(rq)) {
635 q->end_sector = rq_end_sector(rq);
636 q->boundary_rq = rq;
638 } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
639 where = ELEVATOR_INSERT_BACK;
641 if (plug)
642 blk_plug_device(q);
644 elv_insert(q, rq, where);
647 EXPORT_SYMBOL(__elv_add_request);
649 void elv_add_request(request_queue_t *q, struct request *rq, int where,
650 int plug)
652 unsigned long flags;
654 spin_lock_irqsave(q->queue_lock, flags);
655 __elv_add_request(q, rq, where, plug);
656 spin_unlock_irqrestore(q->queue_lock, flags);
659 EXPORT_SYMBOL(elv_add_request);
661 static inline struct request *__elv_next_request(request_queue_t *q)
663 struct request *rq;
665 while (1) {
666 while (!list_empty(&q->queue_head)) {
667 rq = list_entry_rq(q->queue_head.next);
668 if (blk_do_ordered(q, &rq))
669 return rq;
672 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
673 return NULL;
677 struct request *elv_next_request(request_queue_t *q)
679 struct request *rq;
680 int ret;
682 while ((rq = __elv_next_request(q)) != NULL) {
683 if (!(rq->cmd_flags & REQ_STARTED)) {
684 elevator_t *e = q->elevator;
687 * This is the first time the device driver
688 * sees this request (possibly after
689 * requeueing). Notify IO scheduler.
691 if (blk_sorted_rq(rq) &&
692 e->ops->elevator_activate_req_fn)
693 e->ops->elevator_activate_req_fn(q, rq);
696 * just mark as started even if we don't start
697 * it, a request that has been delayed should
698 * not be passed by new incoming requests
700 rq->cmd_flags |= REQ_STARTED;
701 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
704 if (!q->boundary_rq || q->boundary_rq == rq) {
705 q->end_sector = rq_end_sector(rq);
706 q->boundary_rq = NULL;
709 if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
710 break;
712 ret = q->prep_rq_fn(q, rq);
713 if (ret == BLKPREP_OK) {
714 break;
715 } else if (ret == BLKPREP_DEFER) {
717 * the request may have been (partially) prepped.
718 * we need to keep this request in the front to
719 * avoid resource deadlock. REQ_STARTED will
720 * prevent other fs requests from passing this one.
722 rq = NULL;
723 break;
724 } else if (ret == BLKPREP_KILL) {
725 int nr_bytes = rq->hard_nr_sectors << 9;
727 if (!nr_bytes)
728 nr_bytes = rq->data_len;
730 blkdev_dequeue_request(rq);
731 rq->cmd_flags |= REQ_QUIET;
732 end_that_request_chunk(rq, 0, nr_bytes);
733 end_that_request_last(rq, 0);
734 } else {
735 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
736 ret);
737 break;
741 return rq;
744 EXPORT_SYMBOL(elv_next_request);
746 void elv_dequeue_request(request_queue_t *q, struct request *rq)
748 BUG_ON(list_empty(&rq->queuelist));
749 BUG_ON(ELV_ON_HASH(rq));
751 list_del_init(&rq->queuelist);
754 * the time frame between a request being removed from the lists
755 * and to it is freed is accounted as io that is in progress at
756 * the driver side.
758 if (blk_account_rq(rq))
759 q->in_flight++;
762 EXPORT_SYMBOL(elv_dequeue_request);
764 int elv_queue_empty(request_queue_t *q)
766 elevator_t *e = q->elevator;
768 if (!list_empty(&q->queue_head))
769 return 0;
771 if (e->ops->elevator_queue_empty_fn)
772 return e->ops->elevator_queue_empty_fn(q);
774 return 1;
777 EXPORT_SYMBOL(elv_queue_empty);
779 struct request *elv_latter_request(request_queue_t *q, struct request *rq)
781 elevator_t *e = q->elevator;
783 if (e->ops->elevator_latter_req_fn)
784 return e->ops->elevator_latter_req_fn(q, rq);
785 return NULL;
788 struct request *elv_former_request(request_queue_t *q, struct request *rq)
790 elevator_t *e = q->elevator;
792 if (e->ops->elevator_former_req_fn)
793 return e->ops->elevator_former_req_fn(q, rq);
794 return NULL;
797 int elv_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
799 elevator_t *e = q->elevator;
801 if (e->ops->elevator_set_req_fn)
802 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
804 rq->elevator_private = NULL;
805 return 0;
808 void elv_put_request(request_queue_t *q, struct request *rq)
810 elevator_t *e = q->elevator;
812 if (e->ops->elevator_put_req_fn)
813 e->ops->elevator_put_req_fn(rq);
816 int elv_may_queue(request_queue_t *q, int rw)
818 elevator_t *e = q->elevator;
820 if (e->ops->elevator_may_queue_fn)
821 return e->ops->elevator_may_queue_fn(q, rw);
823 return ELV_MQUEUE_MAY;
826 void elv_completed_request(request_queue_t *q, struct request *rq)
828 elevator_t *e = q->elevator;
831 * request is released from the driver, io must be done
833 if (blk_account_rq(rq)) {
834 q->in_flight--;
835 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
836 e->ops->elevator_completed_req_fn(q, rq);
840 * Check if the queue is waiting for fs requests to be
841 * drained for flush sequence.
843 if (unlikely(q->ordseq)) {
844 struct request *first_rq = list_entry_rq(q->queue_head.next);
845 if (q->in_flight == 0 &&
846 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
847 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
848 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
849 q->request_fn(q);
854 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
856 static ssize_t
857 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
859 elevator_t *e = container_of(kobj, elevator_t, kobj);
860 struct elv_fs_entry *entry = to_elv(attr);
861 ssize_t error;
863 if (!entry->show)
864 return -EIO;
866 mutex_lock(&e->sysfs_lock);
867 error = e->ops ? entry->show(e, page) : -ENOENT;
868 mutex_unlock(&e->sysfs_lock);
869 return error;
872 static ssize_t
873 elv_attr_store(struct kobject *kobj, struct attribute *attr,
874 const char *page, size_t length)
876 elevator_t *e = container_of(kobj, elevator_t, kobj);
877 struct elv_fs_entry *entry = to_elv(attr);
878 ssize_t error;
880 if (!entry->store)
881 return -EIO;
883 mutex_lock(&e->sysfs_lock);
884 error = e->ops ? entry->store(e, page, length) : -ENOENT;
885 mutex_unlock(&e->sysfs_lock);
886 return error;
889 static struct sysfs_ops elv_sysfs_ops = {
890 .show = elv_attr_show,
891 .store = elv_attr_store,
894 static struct kobj_type elv_ktype = {
895 .sysfs_ops = &elv_sysfs_ops,
896 .release = elevator_release,
899 int elv_register_queue(struct request_queue *q)
901 elevator_t *e = q->elevator;
902 int error;
904 e->kobj.parent = &q->kobj;
906 error = kobject_add(&e->kobj);
907 if (!error) {
908 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
909 if (attr) {
910 while (attr->attr.name) {
911 if (sysfs_create_file(&e->kobj, &attr->attr))
912 break;
913 attr++;
916 kobject_uevent(&e->kobj, KOBJ_ADD);
918 return error;
921 static void __elv_unregister_queue(elevator_t *e)
923 kobject_uevent(&e->kobj, KOBJ_REMOVE);
924 kobject_del(&e->kobj);
927 void elv_unregister_queue(struct request_queue *q)
929 if (q)
930 __elv_unregister_queue(q->elevator);
933 int elv_register(struct elevator_type *e)
935 spin_lock_irq(&elv_list_lock);
936 BUG_ON(elevator_find(e->elevator_name));
937 list_add_tail(&e->list, &elv_list);
938 spin_unlock_irq(&elv_list_lock);
940 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
941 if (!strcmp(e->elevator_name, chosen_elevator) ||
942 (!*chosen_elevator &&
943 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
944 printk(" (default)");
945 printk("\n");
946 return 0;
948 EXPORT_SYMBOL_GPL(elv_register);
950 void elv_unregister(struct elevator_type *e)
952 struct task_struct *g, *p;
955 * Iterate every thread in the process to remove the io contexts.
957 if (e->ops.trim) {
958 read_lock(&tasklist_lock);
959 do_each_thread(g, p) {
960 task_lock(p);
961 if (p->io_context)
962 e->ops.trim(p->io_context);
963 task_unlock(p);
964 } while_each_thread(g, p);
965 read_unlock(&tasklist_lock);
968 spin_lock_irq(&elv_list_lock);
969 list_del_init(&e->list);
970 spin_unlock_irq(&elv_list_lock);
972 EXPORT_SYMBOL_GPL(elv_unregister);
975 * switch to new_e io scheduler. be careful not to introduce deadlocks -
976 * we don't free the old io scheduler, before we have allocated what we
977 * need for the new one. this way we have a chance of going back to the old
978 * one, if the new one fails init for some reason.
980 static int elevator_switch(request_queue_t *q, struct elevator_type *new_e)
982 elevator_t *old_elevator, *e;
983 void *data;
986 * Allocate new elevator
988 e = elevator_alloc(q, new_e);
989 if (!e)
990 return 0;
992 data = elevator_init_queue(q, e);
993 if (!data) {
994 kobject_put(&e->kobj);
995 return 0;
999 * Turn on BYPASS and drain all requests w/ elevator private data
1001 spin_lock_irq(q->queue_lock);
1003 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1005 elv_drain_elevator(q);
1007 while (q->rq.elvpriv) {
1008 blk_remove_plug(q);
1009 q->request_fn(q);
1010 spin_unlock_irq(q->queue_lock);
1011 msleep(10);
1012 spin_lock_irq(q->queue_lock);
1013 elv_drain_elevator(q);
1017 * Remember old elevator.
1019 old_elevator = q->elevator;
1022 * attach and start new elevator
1024 elevator_attach(q, e, data);
1026 spin_unlock_irq(q->queue_lock);
1028 __elv_unregister_queue(old_elevator);
1030 if (elv_register_queue(q))
1031 goto fail_register;
1034 * finally exit old elevator and turn off BYPASS.
1036 elevator_exit(old_elevator);
1037 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1038 return 1;
1040 fail_register:
1042 * switch failed, exit the new io scheduler and reattach the old
1043 * one again (along with re-adding the sysfs dir)
1045 elevator_exit(e);
1046 q->elevator = old_elevator;
1047 elv_register_queue(q);
1048 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1049 return 0;
1052 ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
1054 char elevator_name[ELV_NAME_MAX];
1055 size_t len;
1056 struct elevator_type *e;
1058 elevator_name[sizeof(elevator_name) - 1] = '\0';
1059 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1060 len = strlen(elevator_name);
1062 if (len && elevator_name[len - 1] == '\n')
1063 elevator_name[len - 1] = '\0';
1065 e = elevator_get(elevator_name);
1066 if (!e) {
1067 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1068 return -EINVAL;
1071 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1072 elevator_put(e);
1073 return count;
1076 if (!elevator_switch(q, e))
1077 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
1078 return count;
1081 ssize_t elv_iosched_show(request_queue_t *q, char *name)
1083 elevator_t *e = q->elevator;
1084 struct elevator_type *elv = e->elevator_type;
1085 struct list_head *entry;
1086 int len = 0;
1088 spin_lock_irq(&elv_list_lock);
1089 list_for_each(entry, &elv_list) {
1090 struct elevator_type *__e;
1092 __e = list_entry(entry, struct elevator_type, list);
1093 if (!strcmp(elv->elevator_name, __e->elevator_name))
1094 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1095 else
1096 len += sprintf(name+len, "%s ", __e->elevator_name);
1098 spin_unlock_irq(&elv_list_lock);
1100 len += sprintf(len+name, "\n");
1101 return len;
1104 struct request *elv_rb_former_request(request_queue_t *q, struct request *rq)
1106 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1108 if (rbprev)
1109 return rb_entry_rq(rbprev);
1111 return NULL;
1114 EXPORT_SYMBOL(elv_rb_former_request);
1116 struct request *elv_rb_latter_request(request_queue_t *q, struct request *rq)
1118 struct rb_node *rbnext = rb_next(&rq->rb_node);
1120 if (rbnext)
1121 return rb_entry_rq(rbnext);
1123 return NULL;
1126 EXPORT_SYMBOL(elv_rb_latter_request);