block: add end_queued_request() and end_dequeued_request() helpers
[safe/jmp/linux-2.6] / 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/delay.h>
35 #include <linux/blktrace_api.h>
36 #include <linux/hash.h>
37
38 #include <asm/uaccess.h>
39
40 static DEFINE_SPINLOCK(elv_list_lock);
41 static LIST_HEAD(elv_list);
42
43 /*
44  * Merge hash stuff.
45  */
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))
52
53 /*
54  * Query io scheduler to see if the current process issuing bio may be
55  * merged with rq.
56  */
57 static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
58 {
59         struct request_queue *q = rq->q;
60         elevator_t *e = q->elevator;
61
62         if (e->ops->elevator_allow_merge_fn)
63                 return e->ops->elevator_allow_merge_fn(q, rq, bio);
64
65         return 1;
66 }
67
68 /*
69  * can we safely merge with this request?
70  */
71 inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
72 {
73         if (!rq_mergeable(rq))
74                 return 0;
75
76         /*
77          * different data direction or already started, don't merge
78          */
79         if (bio_data_dir(bio) != rq_data_dir(rq))
80                 return 0;
81
82         /*
83          * must be same device and not a special request
84          */
85         if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
86                 return 0;
87
88         if (!elv_iosched_allow_merge(rq, bio))
89                 return 0;
90
91         return 1;
92 }
93 EXPORT_SYMBOL(elv_rq_merge_ok);
94
95 static inline int elv_try_merge(struct request *__rq, struct bio *bio)
96 {
97         int ret = ELEVATOR_NO_MERGE;
98
99         /*
100          * we can merge and sequence is ok, check if it's possible
101          */
102         if (elv_rq_merge_ok(__rq, bio)) {
103                 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
104                         ret = ELEVATOR_BACK_MERGE;
105                 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
106                         ret = ELEVATOR_FRONT_MERGE;
107         }
108
109         return ret;
110 }
111
112 static struct elevator_type *elevator_find(const char *name)
113 {
114         struct elevator_type *e;
115
116         list_for_each_entry(e, &elv_list, list) {
117                 if (!strcmp(e->elevator_name, name))
118                         return e;
119         }
120
121         return NULL;
122 }
123
124 static void elevator_put(struct elevator_type *e)
125 {
126         module_put(e->elevator_owner);
127 }
128
129 static struct elevator_type *elevator_get(const char *name)
130 {
131         struct elevator_type *e;
132
133         spin_lock(&elv_list_lock);
134
135         e = elevator_find(name);
136         if (e && !try_module_get(e->elevator_owner))
137                 e = NULL;
138
139         spin_unlock(&elv_list_lock);
140
141         return e;
142 }
143
144 static void *elevator_init_queue(struct request_queue *q,
145                                  struct elevator_queue *eq)
146 {
147         return eq->ops->elevator_init_fn(q);
148 }
149
150 static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
151                            void *data)
152 {
153         q->elevator = eq;
154         eq->elevator_data = data;
155 }
156
157 static char chosen_elevator[16];
158
159 static int __init elevator_setup(char *str)
160 {
161         /*
162          * Be backwards-compatible with previous kernels, so users
163          * won't get the wrong elevator.
164          */
165         if (!strcmp(str, "as"))
166                 strcpy(chosen_elevator, "anticipatory");
167         else
168                 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
169         return 1;
170 }
171
172 __setup("elevator=", elevator_setup);
173
174 static struct kobj_type elv_ktype;
175
176 static elevator_t *elevator_alloc(struct request_queue *q,
177                                   struct elevator_type *e)
178 {
179         elevator_t *eq;
180         int i;
181
182         eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
183         if (unlikely(!eq))
184                 goto err;
185
186         eq->ops = &e->ops;
187         eq->elevator_type = e;
188         kobject_init(&eq->kobj);
189         kobject_set_name(&eq->kobj, "%s", "iosched");
190         eq->kobj.ktype = &elv_ktype;
191         mutex_init(&eq->sysfs_lock);
192
193         eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
194                                         GFP_KERNEL, q->node);
195         if (!eq->hash)
196                 goto err;
197
198         for (i = 0; i < ELV_HASH_ENTRIES; i++)
199                 INIT_HLIST_HEAD(&eq->hash[i]);
200
201         return eq;
202 err:
203         kfree(eq);
204         elevator_put(e);
205         return NULL;
206 }
207
208 static void elevator_release(struct kobject *kobj)
209 {
210         elevator_t *e = container_of(kobj, elevator_t, kobj);
211
212         elevator_put(e->elevator_type);
213         kfree(e->hash);
214         kfree(e);
215 }
216
217 int elevator_init(struct request_queue *q, char *name)
218 {
219         struct elevator_type *e = NULL;
220         struct elevator_queue *eq;
221         int ret = 0;
222         void *data;
223
224         INIT_LIST_HEAD(&q->queue_head);
225         q->last_merge = NULL;
226         q->end_sector = 0;
227         q->boundary_rq = NULL;
228
229         if (name && !(e = elevator_get(name)))
230                 return -EINVAL;
231
232         if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
233                 printk("I/O scheduler %s not found\n", chosen_elevator);
234
235         if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
236                 printk("Default I/O scheduler not found, using no-op\n");
237                 e = elevator_get("noop");
238         }
239
240         eq = elevator_alloc(q, e);
241         if (!eq)
242                 return -ENOMEM;
243
244         data = elevator_init_queue(q, eq);
245         if (!data) {
246                 kobject_put(&eq->kobj);
247                 return -ENOMEM;
248         }
249
250         elevator_attach(q, eq, data);
251         return ret;
252 }
253
254 EXPORT_SYMBOL(elevator_init);
255
256 void elevator_exit(elevator_t *e)
257 {
258         mutex_lock(&e->sysfs_lock);
259         if (e->ops->elevator_exit_fn)
260                 e->ops->elevator_exit_fn(e);
261         e->ops = NULL;
262         mutex_unlock(&e->sysfs_lock);
263
264         kobject_put(&e->kobj);
265 }
266
267 EXPORT_SYMBOL(elevator_exit);
268
269 static void elv_activate_rq(struct request_queue *q, struct request *rq)
270 {
271         elevator_t *e = q->elevator;
272
273         if (e->ops->elevator_activate_req_fn)
274                 e->ops->elevator_activate_req_fn(q, rq);
275 }
276
277 static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
278 {
279         elevator_t *e = q->elevator;
280
281         if (e->ops->elevator_deactivate_req_fn)
282                 e->ops->elevator_deactivate_req_fn(q, rq);
283 }
284
285 static inline void __elv_rqhash_del(struct request *rq)
286 {
287         hlist_del_init(&rq->hash);
288 }
289
290 static void elv_rqhash_del(struct request_queue *q, struct request *rq)
291 {
292         if (ELV_ON_HASH(rq))
293                 __elv_rqhash_del(rq);
294 }
295
296 static void elv_rqhash_add(struct request_queue *q, struct request *rq)
297 {
298         elevator_t *e = q->elevator;
299
300         BUG_ON(ELV_ON_HASH(rq));
301         hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
302 }
303
304 static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
305 {
306         __elv_rqhash_del(rq);
307         elv_rqhash_add(q, rq);
308 }
309
310 static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
311 {
312         elevator_t *e = q->elevator;
313         struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
314         struct hlist_node *entry, *next;
315         struct request *rq;
316
317         hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
318                 BUG_ON(!ELV_ON_HASH(rq));
319
320                 if (unlikely(!rq_mergeable(rq))) {
321                         __elv_rqhash_del(rq);
322                         continue;
323                 }
324
325                 if (rq_hash_key(rq) == offset)
326                         return rq;
327         }
328
329         return NULL;
330 }
331
332 /*
333  * RB-tree support functions for inserting/lookup/removal of requests
334  * in a sorted RB tree.
335  */
336 struct request *elv_rb_add(struct rb_root *root, struct request *rq)
337 {
338         struct rb_node **p = &root->rb_node;
339         struct rb_node *parent = NULL;
340         struct request *__rq;
341
342         while (*p) {
343                 parent = *p;
344                 __rq = rb_entry(parent, struct request, rb_node);
345
346                 if (rq->sector < __rq->sector)
347                         p = &(*p)->rb_left;
348                 else if (rq->sector > __rq->sector)
349                         p = &(*p)->rb_right;
350                 else
351                         return __rq;
352         }
353
354         rb_link_node(&rq->rb_node, parent, p);
355         rb_insert_color(&rq->rb_node, root);
356         return NULL;
357 }
358
359 EXPORT_SYMBOL(elv_rb_add);
360
361 void elv_rb_del(struct rb_root *root, struct request *rq)
362 {
363         BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
364         rb_erase(&rq->rb_node, root);
365         RB_CLEAR_NODE(&rq->rb_node);
366 }
367
368 EXPORT_SYMBOL(elv_rb_del);
369
370 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
371 {
372         struct rb_node *n = root->rb_node;
373         struct request *rq;
374
375         while (n) {
376                 rq = rb_entry(n, struct request, rb_node);
377
378                 if (sector < rq->sector)
379                         n = n->rb_left;
380                 else if (sector > rq->sector)
381                         n = n->rb_right;
382                 else
383                         return rq;
384         }
385
386         return NULL;
387 }
388
389 EXPORT_SYMBOL(elv_rb_find);
390
391 /*
392  * Insert rq into dispatch queue of q.  Queue lock must be held on
393  * entry.  rq is sort insted into the dispatch queue. To be used by
394  * specific elevators.
395  */
396 void elv_dispatch_sort(struct request_queue *q, struct request *rq)
397 {
398         sector_t boundary;
399         struct list_head *entry;
400
401         if (q->last_merge == rq)
402                 q->last_merge = NULL;
403
404         elv_rqhash_del(q, rq);
405
406         q->nr_sorted--;
407
408         boundary = q->end_sector;
409
410         list_for_each_prev(entry, &q->queue_head) {
411                 struct request *pos = list_entry_rq(entry);
412
413                 if (rq_data_dir(rq) != rq_data_dir(pos))
414                         break;
415                 if (pos->cmd_flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
416                         break;
417                 if (rq->sector >= boundary) {
418                         if (pos->sector < boundary)
419                                 continue;
420                 } else {
421                         if (pos->sector >= boundary)
422                                 break;
423                 }
424                 if (rq->sector >= pos->sector)
425                         break;
426         }
427
428         list_add(&rq->queuelist, entry);
429 }
430
431 EXPORT_SYMBOL(elv_dispatch_sort);
432
433 /*
434  * Insert rq into dispatch queue of q.  Queue lock must be held on
435  * entry.  rq is added to the back of the dispatch queue. To be used by
436  * specific elevators.
437  */
438 void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
439 {
440         if (q->last_merge == rq)
441                 q->last_merge = NULL;
442
443         elv_rqhash_del(q, rq);
444
445         q->nr_sorted--;
446
447         q->end_sector = rq_end_sector(rq);
448         q->boundary_rq = rq;
449         list_add_tail(&rq->queuelist, &q->queue_head);
450 }
451
452 EXPORT_SYMBOL(elv_dispatch_add_tail);
453
454 int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
455 {
456         elevator_t *e = q->elevator;
457         struct request *__rq;
458         int ret;
459
460         /*
461          * First try one-hit cache.
462          */
463         if (q->last_merge) {
464                 ret = elv_try_merge(q->last_merge, bio);
465                 if (ret != ELEVATOR_NO_MERGE) {
466                         *req = q->last_merge;
467                         return ret;
468                 }
469         }
470
471         /*
472          * See if our hash lookup can find a potential backmerge.
473          */
474         __rq = elv_rqhash_find(q, bio->bi_sector);
475         if (__rq && elv_rq_merge_ok(__rq, bio)) {
476                 *req = __rq;
477                 return ELEVATOR_BACK_MERGE;
478         }
479
480         if (e->ops->elevator_merge_fn)
481                 return e->ops->elevator_merge_fn(q, req, bio);
482
483         return ELEVATOR_NO_MERGE;
484 }
485
486 void elv_merged_request(struct request_queue *q, struct request *rq, int type)
487 {
488         elevator_t *e = q->elevator;
489
490         if (e->ops->elevator_merged_fn)
491                 e->ops->elevator_merged_fn(q, rq, type);
492
493         if (type == ELEVATOR_BACK_MERGE)
494                 elv_rqhash_reposition(q, rq);
495
496         q->last_merge = rq;
497 }
498
499 void elv_merge_requests(struct request_queue *q, struct request *rq,
500                              struct request *next)
501 {
502         elevator_t *e = q->elevator;
503
504         if (e->ops->elevator_merge_req_fn)
505                 e->ops->elevator_merge_req_fn(q, rq, next);
506
507         elv_rqhash_reposition(q, rq);
508         elv_rqhash_del(q, next);
509
510         q->nr_sorted--;
511         q->last_merge = rq;
512 }
513
514 void elv_requeue_request(struct request_queue *q, struct request *rq)
515 {
516         /*
517          * it already went through dequeue, we need to decrement the
518          * in_flight count again
519          */
520         if (blk_account_rq(rq)) {
521                 q->in_flight--;
522                 if (blk_sorted_rq(rq))
523                         elv_deactivate_rq(q, rq);
524         }
525
526         rq->cmd_flags &= ~REQ_STARTED;
527
528         elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
529 }
530
531 static void elv_drain_elevator(struct request_queue *q)
532 {
533         static int printed;
534         while (q->elevator->ops->elevator_dispatch_fn(q, 1))
535                 ;
536         if (q->nr_sorted == 0)
537                 return;
538         if (printed++ < 10) {
539                 printk(KERN_ERR "%s: forced dispatching is broken "
540                        "(nr_sorted=%u), please report this\n",
541                        q->elevator->elevator_type->elevator_name, q->nr_sorted);
542         }
543 }
544
545 void elv_insert(struct request_queue *q, struct request *rq, int where)
546 {
547         struct list_head *pos;
548         unsigned ordseq;
549         int unplug_it = 1;
550
551         blk_add_trace_rq(q, rq, BLK_TA_INSERT);
552
553         rq->q = q;
554
555         switch (where) {
556         case ELEVATOR_INSERT_FRONT:
557                 rq->cmd_flags |= REQ_SOFTBARRIER;
558
559                 list_add(&rq->queuelist, &q->queue_head);
560                 break;
561
562         case ELEVATOR_INSERT_BACK:
563                 rq->cmd_flags |= REQ_SOFTBARRIER;
564                 elv_drain_elevator(q);
565                 list_add_tail(&rq->queuelist, &q->queue_head);
566                 /*
567                  * We kick the queue here for the following reasons.
568                  * - The elevator might have returned NULL previously
569                  *   to delay requests and returned them now.  As the
570                  *   queue wasn't empty before this request, ll_rw_blk
571                  *   won't run the queue on return, resulting in hang.
572                  * - Usually, back inserted requests won't be merged
573                  *   with anything.  There's no point in delaying queue
574                  *   processing.
575                  */
576                 blk_remove_plug(q);
577                 q->request_fn(q);
578                 break;
579
580         case ELEVATOR_INSERT_SORT:
581                 BUG_ON(!blk_fs_request(rq));
582                 rq->cmd_flags |= REQ_SORTED;
583                 q->nr_sorted++;
584                 if (rq_mergeable(rq)) {
585                         elv_rqhash_add(q, rq);
586                         if (!q->last_merge)
587                                 q->last_merge = rq;
588                 }
589
590                 /*
591                  * Some ioscheds (cfq) run q->request_fn directly, so
592                  * rq cannot be accessed after calling
593                  * elevator_add_req_fn.
594                  */
595                 q->elevator->ops->elevator_add_req_fn(q, rq);
596                 break;
597
598         case ELEVATOR_INSERT_REQUEUE:
599                 /*
600                  * If ordered flush isn't in progress, we do front
601                  * insertion; otherwise, requests should be requeued
602                  * in ordseq order.
603                  */
604                 rq->cmd_flags |= REQ_SOFTBARRIER;
605
606                 /*
607                  * Most requeues happen because of a busy condition,
608                  * don't force unplug of the queue for that case.
609                  */
610                 unplug_it = 0;
611
612                 if (q->ordseq == 0) {
613                         list_add(&rq->queuelist, &q->queue_head);
614                         break;
615                 }
616
617                 ordseq = blk_ordered_req_seq(rq);
618
619                 list_for_each(pos, &q->queue_head) {
620                         struct request *pos_rq = list_entry_rq(pos);
621                         if (ordseq <= blk_ordered_req_seq(pos_rq))
622                                 break;
623                 }
624
625                 list_add_tail(&rq->queuelist, pos);
626                 break;
627
628         default:
629                 printk(KERN_ERR "%s: bad insertion point %d\n",
630                        __FUNCTION__, where);
631                 BUG();
632         }
633
634         if (unplug_it && blk_queue_plugged(q)) {
635                 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
636                         - q->in_flight;
637
638                 if (nrq >= q->unplug_thresh)
639                         __generic_unplug_device(q);
640         }
641 }
642
643 void __elv_add_request(struct request_queue *q, struct request *rq, int where,
644                        int plug)
645 {
646         if (q->ordcolor)
647                 rq->cmd_flags |= REQ_ORDERED_COLOR;
648
649         if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
650                 /*
651                  * toggle ordered color
652                  */
653                 if (blk_barrier_rq(rq))
654                         q->ordcolor ^= 1;
655
656                 /*
657                  * barriers implicitly indicate back insertion
658                  */
659                 if (where == ELEVATOR_INSERT_SORT)
660                         where = ELEVATOR_INSERT_BACK;
661
662                 /*
663                  * this request is scheduling boundary, update
664                  * end_sector
665                  */
666                 if (blk_fs_request(rq)) {
667                         q->end_sector = rq_end_sector(rq);
668                         q->boundary_rq = rq;
669                 }
670         } else if (!(rq->cmd_flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
671                 where = ELEVATOR_INSERT_BACK;
672
673         if (plug)
674                 blk_plug_device(q);
675
676         elv_insert(q, rq, where);
677 }
678
679 EXPORT_SYMBOL(__elv_add_request);
680
681 void elv_add_request(struct request_queue *q, struct request *rq, int where,
682                      int plug)
683 {
684         unsigned long flags;
685
686         spin_lock_irqsave(q->queue_lock, flags);
687         __elv_add_request(q, rq, where, plug);
688         spin_unlock_irqrestore(q->queue_lock, flags);
689 }
690
691 EXPORT_SYMBOL(elv_add_request);
692
693 static inline struct request *__elv_next_request(struct request_queue *q)
694 {
695         struct request *rq;
696
697         while (1) {
698                 while (!list_empty(&q->queue_head)) {
699                         rq = list_entry_rq(q->queue_head.next);
700                         if (blk_do_ordered(q, &rq))
701                                 return rq;
702                 }
703
704                 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
705                         return NULL;
706         }
707 }
708
709 struct request *elv_next_request(struct request_queue *q)
710 {
711         struct request *rq;
712         int ret;
713
714         while ((rq = __elv_next_request(q)) != NULL) {
715                 if (!(rq->cmd_flags & REQ_STARTED)) {
716                         /*
717                          * This is the first time the device driver
718                          * sees this request (possibly after
719                          * requeueing).  Notify IO scheduler.
720                          */
721                         if (blk_sorted_rq(rq))
722                                 elv_activate_rq(q, rq);
723
724                         /*
725                          * just mark as started even if we don't start
726                          * it, a request that has been delayed should
727                          * not be passed by new incoming requests
728                          */
729                         rq->cmd_flags |= REQ_STARTED;
730                         blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
731                 }
732
733                 if (!q->boundary_rq || q->boundary_rq == rq) {
734                         q->end_sector = rq_end_sector(rq);
735                         q->boundary_rq = NULL;
736                 }
737
738                 if ((rq->cmd_flags & REQ_DONTPREP) || !q->prep_rq_fn)
739                         break;
740
741                 ret = q->prep_rq_fn(q, rq);
742                 if (ret == BLKPREP_OK) {
743                         break;
744                 } else if (ret == BLKPREP_DEFER) {
745                         /*
746                          * the request may have been (partially) prepped.
747                          * we need to keep this request in the front to
748                          * avoid resource deadlock.  REQ_STARTED will
749                          * prevent other fs requests from passing this one.
750                          */
751                         rq = NULL;
752                         break;
753                 } else if (ret == BLKPREP_KILL) {
754                         rq->cmd_flags |= REQ_QUIET;
755                         end_queued_request(rq, 0);
756                 } else {
757                         printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
758                                                                 ret);
759                         break;
760                 }
761         }
762
763         return rq;
764 }
765
766 EXPORT_SYMBOL(elv_next_request);
767
768 void elv_dequeue_request(struct request_queue *q, struct request *rq)
769 {
770         BUG_ON(list_empty(&rq->queuelist));
771         BUG_ON(ELV_ON_HASH(rq));
772
773         list_del_init(&rq->queuelist);
774
775         /*
776          * the time frame between a request being removed from the lists
777          * and to it is freed is accounted as io that is in progress at
778          * the driver side.
779          */
780         if (blk_account_rq(rq))
781                 q->in_flight++;
782 }
783
784 EXPORT_SYMBOL(elv_dequeue_request);
785
786 int elv_queue_empty(struct request_queue *q)
787 {
788         elevator_t *e = q->elevator;
789
790         if (!list_empty(&q->queue_head))
791                 return 0;
792
793         if (e->ops->elevator_queue_empty_fn)
794                 return e->ops->elevator_queue_empty_fn(q);
795
796         return 1;
797 }
798
799 EXPORT_SYMBOL(elv_queue_empty);
800
801 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
802 {
803         elevator_t *e = q->elevator;
804
805         if (e->ops->elevator_latter_req_fn)
806                 return e->ops->elevator_latter_req_fn(q, rq);
807         return NULL;
808 }
809
810 struct request *elv_former_request(struct request_queue *q, struct request *rq)
811 {
812         elevator_t *e = q->elevator;
813
814         if (e->ops->elevator_former_req_fn)
815                 return e->ops->elevator_former_req_fn(q, rq);
816         return NULL;
817 }
818
819 int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
820 {
821         elevator_t *e = q->elevator;
822
823         if (e->ops->elevator_set_req_fn)
824                 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
825
826         rq->elevator_private = NULL;
827         return 0;
828 }
829
830 void elv_put_request(struct request_queue *q, struct request *rq)
831 {
832         elevator_t *e = q->elevator;
833
834         if (e->ops->elevator_put_req_fn)
835                 e->ops->elevator_put_req_fn(rq);
836 }
837
838 int elv_may_queue(struct request_queue *q, int rw)
839 {
840         elevator_t *e = q->elevator;
841
842         if (e->ops->elevator_may_queue_fn)
843                 return e->ops->elevator_may_queue_fn(q, rw);
844
845         return ELV_MQUEUE_MAY;
846 }
847
848 void elv_completed_request(struct request_queue *q, struct request *rq)
849 {
850         elevator_t *e = q->elevator;
851
852         /*
853          * request is released from the driver, io must be done
854          */
855         if (blk_account_rq(rq)) {
856                 q->in_flight--;
857                 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
858                         e->ops->elevator_completed_req_fn(q, rq);
859         }
860
861         /*
862          * Check if the queue is waiting for fs requests to be
863          * drained for flush sequence.
864          */
865         if (unlikely(q->ordseq)) {
866                 struct request *first_rq = list_entry_rq(q->queue_head.next);
867                 if (q->in_flight == 0 &&
868                     blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
869                     blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
870                         blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
871                         q->request_fn(q);
872                 }
873         }
874 }
875
876 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
877
878 static ssize_t
879 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
880 {
881         elevator_t *e = container_of(kobj, elevator_t, kobj);
882         struct elv_fs_entry *entry = to_elv(attr);
883         ssize_t error;
884
885         if (!entry->show)
886                 return -EIO;
887
888         mutex_lock(&e->sysfs_lock);
889         error = e->ops ? entry->show(e, page) : -ENOENT;
890         mutex_unlock(&e->sysfs_lock);
891         return error;
892 }
893
894 static ssize_t
895 elv_attr_store(struct kobject *kobj, struct attribute *attr,
896                const char *page, size_t length)
897 {
898         elevator_t *e = container_of(kobj, elevator_t, kobj);
899         struct elv_fs_entry *entry = to_elv(attr);
900         ssize_t error;
901
902         if (!entry->store)
903                 return -EIO;
904
905         mutex_lock(&e->sysfs_lock);
906         error = e->ops ? entry->store(e, page, length) : -ENOENT;
907         mutex_unlock(&e->sysfs_lock);
908         return error;
909 }
910
911 static struct sysfs_ops elv_sysfs_ops = {
912         .show   = elv_attr_show,
913         .store  = elv_attr_store,
914 };
915
916 static struct kobj_type elv_ktype = {
917         .sysfs_ops      = &elv_sysfs_ops,
918         .release        = elevator_release,
919 };
920
921 int elv_register_queue(struct request_queue *q)
922 {
923         elevator_t *e = q->elevator;
924         int error;
925
926         e->kobj.parent = &q->kobj;
927
928         error = kobject_add(&e->kobj);
929         if (!error) {
930                 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
931                 if (attr) {
932                         while (attr->attr.name) {
933                                 if (sysfs_create_file(&e->kobj, &attr->attr))
934                                         break;
935                                 attr++;
936                         }
937                 }
938                 kobject_uevent(&e->kobj, KOBJ_ADD);
939         }
940         return error;
941 }
942
943 static void __elv_unregister_queue(elevator_t *e)
944 {
945         kobject_uevent(&e->kobj, KOBJ_REMOVE);
946         kobject_del(&e->kobj);
947 }
948
949 void elv_unregister_queue(struct request_queue *q)
950 {
951         if (q)
952                 __elv_unregister_queue(q->elevator);
953 }
954
955 int elv_register(struct elevator_type *e)
956 {
957         char *def = "";
958
959         spin_lock(&elv_list_lock);
960         BUG_ON(elevator_find(e->elevator_name));
961         list_add_tail(&e->list, &elv_list);
962         spin_unlock(&elv_list_lock);
963
964         if (!strcmp(e->elevator_name, chosen_elevator) ||
965                         (!*chosen_elevator &&
966                          !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
967                                 def = " (default)";
968
969         printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, def);
970         return 0;
971 }
972 EXPORT_SYMBOL_GPL(elv_register);
973
974 void elv_unregister(struct elevator_type *e)
975 {
976         struct task_struct *g, *p;
977
978         /*
979          * Iterate every thread in the process to remove the io contexts.
980          */
981         if (e->ops.trim) {
982                 read_lock(&tasklist_lock);
983                 do_each_thread(g, p) {
984                         task_lock(p);
985                         if (p->io_context)
986                                 e->ops.trim(p->io_context);
987                         task_unlock(p);
988                 } while_each_thread(g, p);
989                 read_unlock(&tasklist_lock);
990         }
991
992         spin_lock(&elv_list_lock);
993         list_del_init(&e->list);
994         spin_unlock(&elv_list_lock);
995 }
996 EXPORT_SYMBOL_GPL(elv_unregister);
997
998 /*
999  * switch to new_e io scheduler. be careful not to introduce deadlocks -
1000  * we don't free the old io scheduler, before we have allocated what we
1001  * need for the new one. this way we have a chance of going back to the old
1002  * one, if the new one fails init for some reason.
1003  */
1004 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1005 {
1006         elevator_t *old_elevator, *e;
1007         void *data;
1008
1009         /*
1010          * Allocate new elevator
1011          */
1012         e = elevator_alloc(q, new_e);
1013         if (!e)
1014                 return 0;
1015
1016         data = elevator_init_queue(q, e);
1017         if (!data) {
1018                 kobject_put(&e->kobj);
1019                 return 0;
1020         }
1021
1022         /*
1023          * Turn on BYPASS and drain all requests w/ elevator private data
1024          */
1025         spin_lock_irq(q->queue_lock);
1026
1027         set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1028
1029         elv_drain_elevator(q);
1030
1031         while (q->rq.elvpriv) {
1032                 blk_remove_plug(q);
1033                 q->request_fn(q);
1034                 spin_unlock_irq(q->queue_lock);
1035                 msleep(10);
1036                 spin_lock_irq(q->queue_lock);
1037                 elv_drain_elevator(q);
1038         }
1039
1040         /*
1041          * Remember old elevator.
1042          */
1043         old_elevator = q->elevator;
1044
1045         /*
1046          * attach and start new elevator
1047          */
1048         elevator_attach(q, e, data);
1049
1050         spin_unlock_irq(q->queue_lock);
1051
1052         __elv_unregister_queue(old_elevator);
1053
1054         if (elv_register_queue(q))
1055                 goto fail_register;
1056
1057         /*
1058          * finally exit old elevator and turn off BYPASS.
1059          */
1060         elevator_exit(old_elevator);
1061         clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1062         return 1;
1063
1064 fail_register:
1065         /*
1066          * switch failed, exit the new io scheduler and reattach the old
1067          * one again (along with re-adding the sysfs dir)
1068          */
1069         elevator_exit(e);
1070         q->elevator = old_elevator;
1071         elv_register_queue(q);
1072         clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
1073         return 0;
1074 }
1075
1076 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1077                           size_t count)
1078 {
1079         char elevator_name[ELV_NAME_MAX];
1080         size_t len;
1081         struct elevator_type *e;
1082
1083         elevator_name[sizeof(elevator_name) - 1] = '\0';
1084         strncpy(elevator_name, name, sizeof(elevator_name) - 1);
1085         len = strlen(elevator_name);
1086
1087         if (len && elevator_name[len - 1] == '\n')
1088                 elevator_name[len - 1] = '\0';
1089
1090         e = elevator_get(elevator_name);
1091         if (!e) {
1092                 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1093                 return -EINVAL;
1094         }
1095
1096         if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1097                 elevator_put(e);
1098                 return count;
1099         }
1100
1101         if (!elevator_switch(q, e))
1102                 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
1103         return count;
1104 }
1105
1106 ssize_t elv_iosched_show(struct request_queue *q, char *name)
1107 {
1108         elevator_t *e = q->elevator;
1109         struct elevator_type *elv = e->elevator_type;
1110         struct elevator_type *__e;
1111         int len = 0;
1112
1113         spin_lock(&elv_list_lock);
1114         list_for_each_entry(__e, &elv_list, list) {
1115                 if (!strcmp(elv->elevator_name, __e->elevator_name))
1116                         len += sprintf(name+len, "[%s] ", elv->elevator_name);
1117                 else
1118                         len += sprintf(name+len, "%s ", __e->elevator_name);
1119         }
1120         spin_unlock(&elv_list_lock);
1121
1122         len += sprintf(len+name, "\n");
1123         return len;
1124 }
1125
1126 struct request *elv_rb_former_request(struct request_queue *q,
1127                                       struct request *rq)
1128 {
1129         struct rb_node *rbprev = rb_prev(&rq->rb_node);
1130
1131         if (rbprev)
1132                 return rb_entry_rq(rbprev);
1133
1134         return NULL;
1135 }
1136
1137 EXPORT_SYMBOL(elv_rb_former_request);
1138
1139 struct request *elv_rb_latter_request(struct request_queue *q,
1140                                       struct request *rq)
1141 {
1142         struct rb_node *rbnext = rb_next(&rq->rb_node);
1143
1144         if (rbnext)
1145                 return rb_entry_rq(rbnext);
1146
1147         return NULL;
1148 }
1149
1150 EXPORT_SYMBOL(elv_rb_latter_request);