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