Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[platform/adaptation/renesas_rcar/renesas_kernel.git] / block / elevator.c
1 /*
2  *  Block device elevator/IO-scheduler.
3  *
4  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5  *
6  * 30042000 Jens Axboe <axboe@kernel.dk> :
7  *
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
14  *
15  * 20082000 Dave Jones <davej@suse.de> :
16  * Removed tests for max-bomb-segments, which was breaking elvtune
17  *  when run without -bN
18  *
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
23  *
24  */
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/blktrace_api.h>
35 #include <linux/hash.h>
36 #include <linux/uaccess.h>
37
38 #include <trace/events/block.h>
39
40 #include "blk.h"
41
42 static DEFINE_SPINLOCK(elv_list_lock);
43 static LIST_HEAD(elv_list);
44
45 /*
46  * Merge hash stuff.
47  */
48 static const int elv_hash_shift = 6;
49 #define ELV_HASH_BLOCK(sec)     ((sec) >> 3)
50 #define ELV_HASH_FN(sec)        \
51                 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
52 #define ELV_HASH_ENTRIES        (1 << elv_hash_shift)
53 #define rq_hash_key(rq)         (blk_rq_pos(rq) + blk_rq_sectors(rq))
54
55 /*
56  * Query io scheduler to see if the current process issuing bio may be
57  * merged with rq.
58  */
59 static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
60 {
61         struct request_queue *q = rq->q;
62         struct elevator_queue *e = q->elevator;
63
64         if (e->type->ops.elevator_allow_merge_fn)
65                 return e->type->ops.elevator_allow_merge_fn(q, rq, bio);
66
67         return 1;
68 }
69
70 /*
71  * can we safely merge with this request?
72  */
73 bool elv_rq_merge_ok(struct request *rq, struct bio *bio)
74 {
75         if (!blk_rq_merge_ok(rq, bio))
76                 return 0;
77
78         if (!elv_iosched_allow_merge(rq, bio))
79                 return 0;
80
81         return 1;
82 }
83 EXPORT_SYMBOL(elv_rq_merge_ok);
84
85 static struct elevator_type *elevator_find(const char *name)
86 {
87         struct elevator_type *e;
88
89         list_for_each_entry(e, &elv_list, list) {
90                 if (!strcmp(e->elevator_name, name))
91                         return e;
92         }
93
94         return NULL;
95 }
96
97 static void elevator_put(struct elevator_type *e)
98 {
99         module_put(e->elevator_owner);
100 }
101
102 static struct elevator_type *elevator_get(const char *name)
103 {
104         struct elevator_type *e;
105
106         spin_lock(&elv_list_lock);
107
108         e = elevator_find(name);
109         if (!e) {
110                 spin_unlock(&elv_list_lock);
111                 request_module("%s-iosched", name);
112                 spin_lock(&elv_list_lock);
113                 e = elevator_find(name);
114         }
115
116         if (e && !try_module_get(e->elevator_owner))
117                 e = NULL;
118
119         spin_unlock(&elv_list_lock);
120
121         return e;
122 }
123
124 static int elevator_init_queue(struct request_queue *q,
125                                struct elevator_queue *eq)
126 {
127         eq->elevator_data = eq->type->ops.elevator_init_fn(q);
128         if (eq->elevator_data)
129                 return 0;
130         return -ENOMEM;
131 }
132
133 static char chosen_elevator[ELV_NAME_MAX];
134
135 static int __init elevator_setup(char *str)
136 {
137         /*
138          * Be backwards-compatible with previous kernels, so users
139          * won't get the wrong elevator.
140          */
141         strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
142         return 1;
143 }
144
145 __setup("elevator=", elevator_setup);
146
147 static struct kobj_type elv_ktype;
148
149 static struct elevator_queue *elevator_alloc(struct request_queue *q,
150                                   struct elevator_type *e)
151 {
152         struct elevator_queue *eq;
153         int i;
154
155         eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
156         if (unlikely(!eq))
157                 goto err;
158
159         eq->type = e;
160         kobject_init(&eq->kobj, &elv_ktype);
161         mutex_init(&eq->sysfs_lock);
162
163         eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
164                                         GFP_KERNEL, q->node);
165         if (!eq->hash)
166                 goto err;
167
168         for (i = 0; i < ELV_HASH_ENTRIES; i++)
169                 INIT_HLIST_HEAD(&eq->hash[i]);
170
171         return eq;
172 err:
173         kfree(eq);
174         elevator_put(e);
175         return NULL;
176 }
177
178 static void elevator_release(struct kobject *kobj)
179 {
180         struct elevator_queue *e;
181
182         e = container_of(kobj, struct elevator_queue, kobj);
183         elevator_put(e->type);
184         kfree(e->hash);
185         kfree(e);
186 }
187
188 int elevator_init(struct request_queue *q, char *name)
189 {
190         struct elevator_type *e = NULL;
191         struct elevator_queue *eq;
192         int err;
193
194         if (unlikely(q->elevator))
195                 return 0;
196
197         INIT_LIST_HEAD(&q->queue_head);
198         q->last_merge = NULL;
199         q->end_sector = 0;
200         q->boundary_rq = NULL;
201
202         if (name) {
203                 e = elevator_get(name);
204                 if (!e)
205                         return -EINVAL;
206         }
207
208         if (!e && *chosen_elevator) {
209                 e = elevator_get(chosen_elevator);
210                 if (!e)
211                         printk(KERN_ERR "I/O scheduler %s not found\n",
212                                                         chosen_elevator);
213         }
214
215         if (!e) {
216                 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
217                 if (!e) {
218                         printk(KERN_ERR
219                                 "Default I/O scheduler not found. " \
220                                 "Using noop.\n");
221                         e = elevator_get("noop");
222                 }
223         }
224
225         eq = elevator_alloc(q, e);
226         if (!eq)
227                 return -ENOMEM;
228
229         err = elevator_init_queue(q, eq);
230         if (err) {
231                 kobject_put(&eq->kobj);
232                 return err;
233         }
234
235         q->elevator = eq;
236         return 0;
237 }
238 EXPORT_SYMBOL(elevator_init);
239
240 void elevator_exit(struct elevator_queue *e)
241 {
242         mutex_lock(&e->sysfs_lock);
243         if (e->type->ops.elevator_exit_fn)
244                 e->type->ops.elevator_exit_fn(e);
245         mutex_unlock(&e->sysfs_lock);
246
247         kobject_put(&e->kobj);
248 }
249 EXPORT_SYMBOL(elevator_exit);
250
251 static inline void __elv_rqhash_del(struct request *rq)
252 {
253         hlist_del_init(&rq->hash);
254 }
255
256 static void elv_rqhash_del(struct request_queue *q, struct request *rq)
257 {
258         if (ELV_ON_HASH(rq))
259                 __elv_rqhash_del(rq);
260 }
261
262 static void elv_rqhash_add(struct request_queue *q, struct request *rq)
263 {
264         struct elevator_queue *e = q->elevator;
265
266         BUG_ON(ELV_ON_HASH(rq));
267         hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
268 }
269
270 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
271 {
272         __elv_rqhash_del(rq);
273         elv_rqhash_add(q, rq);
274 }
275
276 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
277 {
278         struct elevator_queue *e = q->elevator;
279         struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
280         struct hlist_node *entry, *next;
281         struct request *rq;
282
283         hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
284                 BUG_ON(!ELV_ON_HASH(rq));
285
286                 if (unlikely(!rq_mergeable(rq))) {
287                         __elv_rqhash_del(rq);
288                         continue;
289                 }
290
291                 if (rq_hash_key(rq) == offset)
292                         return rq;
293         }
294
295         return NULL;
296 }
297
298 /*
299  * RB-tree support functions for inserting/lookup/removal of requests
300  * in a sorted RB tree.
301  */
302 void elv_rb_add(struct rb_root *root, struct request *rq)
303 {
304         struct rb_node **p = &root->rb_node;
305         struct rb_node *parent = NULL;
306         struct request *__rq;
307
308         while (*p) {
309                 parent = *p;
310                 __rq = rb_entry(parent, struct request, rb_node);
311
312                 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
313                         p = &(*p)->rb_left;
314                 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
315                         p = &(*p)->rb_right;
316         }
317
318         rb_link_node(&rq->rb_node, parent, p);
319         rb_insert_color(&rq->rb_node, root);
320 }
321 EXPORT_SYMBOL(elv_rb_add);
322
323 void elv_rb_del(struct rb_root *root, struct request *rq)
324 {
325         BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
326         rb_erase(&rq->rb_node, root);
327         RB_CLEAR_NODE(&rq->rb_node);
328 }
329 EXPORT_SYMBOL(elv_rb_del);
330
331 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
332 {
333         struct rb_node *n = root->rb_node;
334         struct request *rq;
335
336         while (n) {
337                 rq = rb_entry(n, struct request, rb_node);
338
339                 if (sector < blk_rq_pos(rq))
340                         n = n->rb_left;
341                 else if (sector > blk_rq_pos(rq))
342                         n = n->rb_right;
343                 else
344                         return rq;
345         }
346
347         return NULL;
348 }
349 EXPORT_SYMBOL(elv_rb_find);
350
351 /*
352  * Insert rq into dispatch queue of q.  Queue lock must be held on
353  * entry.  rq is sort instead into the dispatch queue. To be used by
354  * specific elevators.
355  */
356 void elv_dispatch_sort(struct request_queue *q, struct request *rq)
357 {
358         sector_t boundary;
359         struct list_head *entry;
360         int stop_flags;
361
362         if (q->last_merge == rq)
363                 q->last_merge = NULL;
364
365         elv_rqhash_del(q, rq);
366
367         q->nr_sorted--;
368
369         boundary = q->end_sector;
370         stop_flags = REQ_SOFTBARRIER | REQ_STARTED;
371         list_for_each_prev(entry, &q->queue_head) {
372                 struct request *pos = list_entry_rq(entry);
373
374                 if ((rq->cmd_flags & REQ_DISCARD) !=
375                     (pos->cmd_flags & REQ_DISCARD))
376                         break;
377                 if (rq_data_dir(rq) != rq_data_dir(pos))
378                         break;
379                 if (pos->cmd_flags & stop_flags)
380                         break;
381                 if (blk_rq_pos(rq) >= boundary) {
382                         if (blk_rq_pos(pos) < boundary)
383                                 continue;
384                 } else {
385                         if (blk_rq_pos(pos) >= boundary)
386                                 break;
387                 }
388                 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
389                         break;
390         }
391
392         list_add(&rq->queuelist, entry);
393 }
394 EXPORT_SYMBOL(elv_dispatch_sort);
395
396 /*
397  * Insert rq into dispatch queue of q.  Queue lock must be held on
398  * entry.  rq is added to the back of the dispatch queue. To be used by
399  * specific elevators.
400  */
401 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
402 {
403         if (q->last_merge == rq)
404                 q->last_merge = NULL;
405
406         elv_rqhash_del(q, rq);
407
408         q->nr_sorted--;
409
410         q->end_sector = rq_end_sector(rq);
411         q->boundary_rq = rq;
412         list_add_tail(&rq->queuelist, &q->queue_head);
413 }
414 EXPORT_SYMBOL(elv_dispatch_add_tail);
415
416 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
417 {
418         struct elevator_queue *e = q->elevator;
419         struct request *__rq;
420         int ret;
421
422         /*
423          * Levels of merges:
424          *      nomerges:  No merges at all attempted
425          *      noxmerges: Only simple one-hit cache try
426          *      merges:    All merge tries attempted
427          */
428         if (blk_queue_nomerges(q))
429                 return ELEVATOR_NO_MERGE;
430
431         /*
432          * First try one-hit cache.
433          */
434         if (q->last_merge && elv_rq_merge_ok(q->last_merge, bio)) {
435                 ret = blk_try_merge(q->last_merge, bio);
436                 if (ret != ELEVATOR_NO_MERGE) {
437                         *req = q->last_merge;
438                         return ret;
439                 }
440         }
441
442         if (blk_queue_noxmerges(q))
443                 return ELEVATOR_NO_MERGE;
444
445         /*
446          * See if our hash lookup can find a potential backmerge.
447          */
448         __rq = elv_rqhash_find(q, bio->bi_sector);
449         if (__rq && elv_rq_merge_ok(__rq, bio)) {
450                 *req = __rq;
451                 return ELEVATOR_BACK_MERGE;
452         }
453
454         if (e->type->ops.elevator_merge_fn)
455                 return e->type->ops.elevator_merge_fn(q, req, bio);
456
457         return ELEVATOR_NO_MERGE;
458 }
459
460 /*
461  * Attempt to do an insertion back merge. Only check for the case where
462  * we can append 'rq' to an existing request, so we can throw 'rq' away
463  * afterwards.
464  *
465  * Returns true if we merged, false otherwise
466  */
467 static bool elv_attempt_insert_merge(struct request_queue *q,
468                                      struct request *rq)
469 {
470         struct request *__rq;
471
472         if (blk_queue_nomerges(q))
473                 return false;
474
475         /*
476          * First try one-hit cache.
477          */
478         if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
479                 return true;
480
481         if (blk_queue_noxmerges(q))
482                 return false;
483
484         /*
485          * See if our hash lookup can find a potential backmerge.
486          */
487         __rq = elv_rqhash_find(q, blk_rq_pos(rq));
488         if (__rq && blk_attempt_req_merge(q, __rq, rq))
489                 return true;
490
491         return false;
492 }
493
494 void elv_merged_request(struct request_queue *q, struct request *rq, int type)
495 {
496         struct elevator_queue *e = q->elevator;
497
498         if (e->type->ops.elevator_merged_fn)
499                 e->type->ops.elevator_merged_fn(q, rq, type);
500
501         if (type == ELEVATOR_BACK_MERGE)
502                 elv_rqhash_reposition(q, rq);
503
504         q->last_merge = rq;
505 }
506
507 void elv_merge_requests(struct request_queue *q, struct request *rq,
508                              struct request *next)
509 {
510         struct elevator_queue *e = q->elevator;
511         const int next_sorted = next->cmd_flags & REQ_SORTED;
512
513         if (next_sorted && e->type->ops.elevator_merge_req_fn)
514                 e->type->ops.elevator_merge_req_fn(q, rq, next);
515
516         elv_rqhash_reposition(q, rq);
517
518         if (next_sorted) {
519                 elv_rqhash_del(q, next);
520                 q->nr_sorted--;
521         }
522
523         q->last_merge = rq;
524 }
525
526 void elv_bio_merged(struct request_queue *q, struct request *rq,
527                         struct bio *bio)
528 {
529         struct elevator_queue *e = q->elevator;
530
531         if (e->type->ops.elevator_bio_merged_fn)
532                 e->type->ops.elevator_bio_merged_fn(q, rq, bio);
533 }
534
535 void elv_requeue_request(struct request_queue *q, struct request *rq)
536 {
537         /*
538          * it already went through dequeue, we need to decrement the
539          * in_flight count again
540          */
541         if (blk_account_rq(rq)) {
542                 q->in_flight[rq_is_sync(rq)]--;
543                 if (rq->cmd_flags & REQ_SORTED)
544                         elv_deactivate_rq(q, rq);
545         }
546
547         rq->cmd_flags &= ~REQ_STARTED;
548
549         __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
550 }
551
552 void elv_drain_elevator(struct request_queue *q)
553 {
554         static int printed;
555
556         lockdep_assert_held(q->queue_lock);
557
558         while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
559                 ;
560         if (q->nr_sorted && printed++ < 10) {
561                 printk(KERN_ERR "%s: forced dispatching is broken "
562                        "(nr_sorted=%u), please report this\n",
563                        q->elevator->type->elevator_name, q->nr_sorted);
564         }
565 }
566
567 void elv_quiesce_start(struct request_queue *q)
568 {
569         if (!q->elevator)
570                 return;
571
572         spin_lock_irq(q->queue_lock);
573         queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
574         spin_unlock_irq(q->queue_lock);
575
576         blk_drain_queue(q, false);
577 }
578
579 void elv_quiesce_end(struct request_queue *q)
580 {
581         spin_lock_irq(q->queue_lock);
582         queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
583         spin_unlock_irq(q->queue_lock);
584 }
585
586 void __elv_add_request(struct request_queue *q, struct request *rq, int where)
587 {
588         trace_block_rq_insert(q, rq);
589
590         rq->q = q;
591
592         if (rq->cmd_flags & REQ_SOFTBARRIER) {
593                 /* barriers are scheduling boundary, update end_sector */
594                 if (rq->cmd_type == REQ_TYPE_FS ||
595                     (rq->cmd_flags & REQ_DISCARD)) {
596                         q->end_sector = rq_end_sector(rq);
597                         q->boundary_rq = rq;
598                 }
599         } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
600                     (where == ELEVATOR_INSERT_SORT ||
601                      where == ELEVATOR_INSERT_SORT_MERGE))
602                 where = ELEVATOR_INSERT_BACK;
603
604         switch (where) {
605         case ELEVATOR_INSERT_REQUEUE:
606         case ELEVATOR_INSERT_FRONT:
607                 rq->cmd_flags |= REQ_SOFTBARRIER;
608                 list_add(&rq->queuelist, &q->queue_head);
609                 break;
610
611         case ELEVATOR_INSERT_BACK:
612                 rq->cmd_flags |= REQ_SOFTBARRIER;
613                 elv_drain_elevator(q);
614                 list_add_tail(&rq->queuelist, &q->queue_head);
615                 /*
616                  * We kick the queue here for the following reasons.
617                  * - The elevator might have returned NULL previously
618                  *   to delay requests and returned them now.  As the
619                  *   queue wasn't empty before this request, ll_rw_blk
620                  *   won't run the queue on return, resulting in hang.
621                  * - Usually, back inserted requests won't be merged
622                  *   with anything.  There's no point in delaying queue
623                  *   processing.
624                  */
625                 __blk_run_queue(q);
626                 break;
627
628         case ELEVATOR_INSERT_SORT_MERGE:
629                 /*
630                  * If we succeed in merging this request with one in the
631                  * queue already, we are done - rq has now been freed,
632                  * so no need to do anything further.
633                  */
634                 if (elv_attempt_insert_merge(q, rq))
635                         break;
636         case ELEVATOR_INSERT_SORT:
637                 BUG_ON(rq->cmd_type != REQ_TYPE_FS &&
638                        !(rq->cmd_flags & REQ_DISCARD));
639                 rq->cmd_flags |= REQ_SORTED;
640                 q->nr_sorted++;
641                 if (rq_mergeable(rq)) {
642                         elv_rqhash_add(q, rq);
643                         if (!q->last_merge)
644                                 q->last_merge = rq;
645                 }
646
647                 /*
648                  * Some ioscheds (cfq) run q->request_fn directly, so
649                  * rq cannot be accessed after calling
650                  * elevator_add_req_fn.
651                  */
652                 q->elevator->type->ops.elevator_add_req_fn(q, rq);
653                 break;
654
655         case ELEVATOR_INSERT_FLUSH:
656                 rq->cmd_flags |= REQ_SOFTBARRIER;
657                 blk_insert_flush(rq);
658                 break;
659         default:
660                 printk(KERN_ERR "%s: bad insertion point %d\n",
661                        __func__, where);
662                 BUG();
663         }
664 }
665 EXPORT_SYMBOL(__elv_add_request);
666
667 void elv_add_request(struct request_queue *q, struct request *rq, int where)
668 {
669         unsigned long flags;
670
671         spin_lock_irqsave(q->queue_lock, flags);
672         __elv_add_request(q, rq, where);
673         spin_unlock_irqrestore(q->queue_lock, flags);
674 }
675 EXPORT_SYMBOL(elv_add_request);
676
677 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
678 {
679         struct elevator_queue *e = q->elevator;
680
681         if (e->type->ops.elevator_latter_req_fn)
682                 return e->type->ops.elevator_latter_req_fn(q, rq);
683         return NULL;
684 }
685
686 struct request *elv_former_request(struct request_queue *q, struct request *rq)
687 {
688         struct elevator_queue *e = q->elevator;
689
690         if (e->type->ops.elevator_former_req_fn)
691                 return e->type->ops.elevator_former_req_fn(q, rq);
692         return NULL;
693 }
694
695 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
696 {
697         struct elevator_queue *e = q->elevator;
698
699         if (e->type->ops.elevator_set_req_fn)
700                 return e->type->ops.elevator_set_req_fn(q, rq, gfp_mask);
701         return 0;
702 }
703
704 void elv_put_request(struct request_queue *q, struct request *rq)
705 {
706         struct elevator_queue *e = q->elevator;
707
708         if (e->type->ops.elevator_put_req_fn)
709                 e->type->ops.elevator_put_req_fn(rq);
710 }
711
712 int elv_may_queue(struct request_queue *q, int rw)
713 {
714         struct elevator_queue *e = q->elevator;
715
716         if (e->type->ops.elevator_may_queue_fn)
717                 return e->type->ops.elevator_may_queue_fn(q, rw);
718
719         return ELV_MQUEUE_MAY;
720 }
721
722 void elv_abort_queue(struct request_queue *q)
723 {
724         struct request *rq;
725
726         blk_abort_flushes(q);
727
728         while (!list_empty(&q->queue_head)) {
729                 rq = list_entry_rq(q->queue_head.next);
730                 rq->cmd_flags |= REQ_QUIET;
731                 trace_block_rq_abort(q, rq);
732                 /*
733                  * Mark this request as started so we don't trigger
734                  * any debug logic in the end I/O path.
735                  */
736                 blk_start_request(rq);
737                 __blk_end_request_all(rq, -EIO);
738         }
739 }
740 EXPORT_SYMBOL(elv_abort_queue);
741
742 void elv_completed_request(struct request_queue *q, struct request *rq)
743 {
744         struct elevator_queue *e = q->elevator;
745
746         /*
747          * request is released from the driver, io must be done
748          */
749         if (blk_account_rq(rq)) {
750                 q->in_flight[rq_is_sync(rq)]--;
751                 if ((rq->cmd_flags & REQ_SORTED) &&
752                     e->type->ops.elevator_completed_req_fn)
753                         e->type->ops.elevator_completed_req_fn(q, rq);
754         }
755 }
756
757 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
758
759 static ssize_t
760 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
761 {
762         struct elv_fs_entry *entry = to_elv(attr);
763         struct elevator_queue *e;
764         ssize_t error;
765
766         if (!entry->show)
767                 return -EIO;
768
769         e = container_of(kobj, struct elevator_queue, kobj);
770         mutex_lock(&e->sysfs_lock);
771         error = e->type ? entry->show(e, page) : -ENOENT;
772         mutex_unlock(&e->sysfs_lock);
773         return error;
774 }
775
776 static ssize_t
777 elv_attr_store(struct kobject *kobj, struct attribute *attr,
778                const char *page, size_t length)
779 {
780         struct elv_fs_entry *entry = to_elv(attr);
781         struct elevator_queue *e;
782         ssize_t error;
783
784         if (!entry->store)
785                 return -EIO;
786
787         e = container_of(kobj, struct elevator_queue, kobj);
788         mutex_lock(&e->sysfs_lock);
789         error = e->type ? entry->store(e, page, length) : -ENOENT;
790         mutex_unlock(&e->sysfs_lock);
791         return error;
792 }
793
794 static const struct sysfs_ops elv_sysfs_ops = {
795         .show   = elv_attr_show,
796         .store  = elv_attr_store,
797 };
798
799 static struct kobj_type elv_ktype = {
800         .sysfs_ops      = &elv_sysfs_ops,
801         .release        = elevator_release,
802 };
803
804 int __elv_register_queue(struct request_queue *q, struct elevator_queue *e)
805 {
806         int error;
807
808         error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
809         if (!error) {
810                 struct elv_fs_entry *attr = e->type->elevator_attrs;
811                 if (attr) {
812                         while (attr->attr.name) {
813                                 if (sysfs_create_file(&e->kobj, &attr->attr))
814                                         break;
815                                 attr++;
816                         }
817                 }
818                 kobject_uevent(&e->kobj, KOBJ_ADD);
819                 e->registered = 1;
820         }
821         return error;
822 }
823
824 int elv_register_queue(struct request_queue *q)
825 {
826         return __elv_register_queue(q, q->elevator);
827 }
828 EXPORT_SYMBOL(elv_register_queue);
829
830 void elv_unregister_queue(struct request_queue *q)
831 {
832         if (q) {
833                 struct elevator_queue *e = q->elevator;
834
835                 kobject_uevent(&e->kobj, KOBJ_REMOVE);
836                 kobject_del(&e->kobj);
837                 e->registered = 0;
838         }
839 }
840 EXPORT_SYMBOL(elv_unregister_queue);
841
842 int elv_register(struct elevator_type *e)
843 {
844         char *def = "";
845
846         /* create icq_cache if requested */
847         if (e->icq_size) {
848                 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
849                     WARN_ON(e->icq_align < __alignof__(struct io_cq)))
850                         return -EINVAL;
851
852                 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
853                          "%s_io_cq", e->elevator_name);
854                 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
855                                                  e->icq_align, 0, NULL);
856                 if (!e->icq_cache)
857                         return -ENOMEM;
858         }
859
860         /* register, don't allow duplicate names */
861         spin_lock(&elv_list_lock);
862         if (elevator_find(e->elevator_name)) {
863                 spin_unlock(&elv_list_lock);
864                 if (e->icq_cache)
865                         kmem_cache_destroy(e->icq_cache);
866                 return -EBUSY;
867         }
868         list_add_tail(&e->list, &elv_list);
869         spin_unlock(&elv_list_lock);
870
871         /* print pretty message */
872         if (!strcmp(e->elevator_name, chosen_elevator) ||
873                         (!*chosen_elevator &&
874                          !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
875                                 def = " (default)";
876
877         printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
878                                                                 def);
879         return 0;
880 }
881 EXPORT_SYMBOL_GPL(elv_register);
882
883 void elv_unregister(struct elevator_type *e)
884 {
885         /* unregister */
886         spin_lock(&elv_list_lock);
887         list_del_init(&e->list);
888         spin_unlock(&elv_list_lock);
889
890         /*
891          * Destroy icq_cache if it exists.  icq's are RCU managed.  Make
892          * sure all RCU operations are complete before proceeding.
893          */
894         if (e->icq_cache) {
895                 rcu_barrier();
896                 kmem_cache_destroy(e->icq_cache);
897                 e->icq_cache = NULL;
898         }
899 }
900 EXPORT_SYMBOL_GPL(elv_unregister);
901
902 /*
903  * switch to new_e io scheduler. be careful not to introduce deadlocks -
904  * we don't free the old io scheduler, before we have allocated what we
905  * need for the new one. this way we have a chance of going back to the old
906  * one, if the new one fails init for some reason.
907  */
908 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
909 {
910         struct elevator_queue *old_elevator, *e;
911         int err;
912
913         /* allocate new elevator */
914         e = elevator_alloc(q, new_e);
915         if (!e)
916                 return -ENOMEM;
917
918         err = elevator_init_queue(q, e);
919         if (err) {
920                 kobject_put(&e->kobj);
921                 return err;
922         }
923
924         /* turn on BYPASS and drain all requests w/ elevator private data */
925         elv_quiesce_start(q);
926
927         /* unregister old queue, register new one and kill old elevator */
928         if (q->elevator->registered) {
929                 elv_unregister_queue(q);
930                 err = __elv_register_queue(q, e);
931                 if (err)
932                         goto fail_register;
933         }
934
935         /* done, clear io_cq's, switch elevators and turn off BYPASS */
936         spin_lock_irq(q->queue_lock);
937         ioc_clear_queue(q);
938         old_elevator = q->elevator;
939         q->elevator = e;
940         spin_unlock_irq(q->queue_lock);
941
942         elevator_exit(old_elevator);
943         elv_quiesce_end(q);
944
945         blk_add_trace_msg(q, "elv switch: %s", e->type->elevator_name);
946
947         return 0;
948
949 fail_register:
950         /*
951          * switch failed, exit the new io scheduler and reattach the old
952          * one again (along with re-adding the sysfs dir)
953          */
954         elevator_exit(e);
955         elv_register_queue(q);
956         elv_quiesce_end(q);
957
958         return err;
959 }
960
961 /*
962  * Switch this queue to the given IO scheduler.
963  */
964 int elevator_change(struct request_queue *q, const char *name)
965 {
966         char elevator_name[ELV_NAME_MAX];
967         struct elevator_type *e;
968
969         if (!q->elevator)
970                 return -ENXIO;
971
972         strlcpy(elevator_name, name, sizeof(elevator_name));
973         e = elevator_get(strstrip(elevator_name));
974         if (!e) {
975                 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
976                 return -EINVAL;
977         }
978
979         if (!strcmp(elevator_name, q->elevator->type->elevator_name)) {
980                 elevator_put(e);
981                 return 0;
982         }
983
984         return elevator_switch(q, e);
985 }
986 EXPORT_SYMBOL(elevator_change);
987
988 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
989                           size_t count)
990 {
991         int ret;
992
993         if (!q->elevator)
994                 return count;
995
996         ret = elevator_change(q, name);
997         if (!ret)
998                 return count;
999
1000         printk(KERN_ERR "elevator: switch to %s failed\n", name);
1001         return ret;
1002 }
1003
1004 ssize_t elv_iosched_show(struct request_queue *q, char *name)
1005 {
1006         struct elevator_queue *e = q->elevator;
1007         struct elevator_type *elv;
1008         struct elevator_type *__e;
1009         int len = 0;
1010
1011         if (!q->elevator || !blk_queue_stackable(q))
1012                 return sprintf(name, "none\n");
1013
1014         elv = e->type;
1015
1016         spin_lock(&elv_list_lock);
1017         list_for_each_entry(__e, &elv_list, list) {
1018                 if (!strcmp(elv->elevator_name, __e->elevator_name))
1019                         len += sprintf(name+len, "[%s] ", elv->elevator_name);
1020                 else
1021                         len += sprintf(name+len, "%s ", __e->elevator_name);
1022         }
1023         spin_unlock(&elv_list_lock);
1024
1025         len += sprintf(len+name, "\n");
1026         return len;
1027 }
1028
1029 struct request *elv_rb_former_request(struct request_queue *q,
1030                                       struct request *rq)
1031 {
1032         struct rb_node *rbprev = rb_prev(&rq->rb_node);
1033
1034         if (rbprev)
1035                 return rb_entry_rq(rbprev);
1036
1037         return NULL;
1038 }
1039 EXPORT_SYMBOL(elv_rb_former_request);
1040
1041 struct request *elv_rb_latter_request(struct request_queue *q,
1042                                       struct request *rq)
1043 {
1044         struct rb_node *rbnext = rb_next(&rq->rb_node);
1045
1046         if (rbnext)
1047                 return rb_entry_rq(rbnext);
1048
1049         return NULL;
1050 }
1051 EXPORT_SYMBOL(elv_rb_latter_request);