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