ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / block / as-iosched.c
1 /*
2  *  linux/drivers/block/as-iosched.c
3  *
4  *  Anticipatory & deadline i/o scheduler.
5  *
6  *  Copyright (C) 2002 Jens Axboe <axboe@suse.de>
7  *                     Nick Piggin <piggin@cyberone.com.au>
8  *
9  */
10 #include <linux/kernel.h>
11 #include <linux/fs.h>
12 #include <linux/blkdev.h>
13 #include <linux/elevator.h>
14 #include <linux/bio.h>
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/compiler.h>
20 #include <linux/hash.h>
21 #include <linux/rbtree.h>
22 #include <linux/interrupt.h>
23
24 #define REQ_SYNC        1
25 #define REQ_ASYNC       0
26
27 /*
28  * See Documentation/as-iosched.txt
29  */
30
31 /*
32  * max time before a read is submitted.
33  */
34 #define default_read_expire (HZ / 8)
35
36 /*
37  * ditto for writes, these limits are not hard, even
38  * if the disk is capable of satisfying them.
39  */
40 #define default_write_expire (HZ / 4)
41
42 /*
43  * read_batch_expire describes how long we will allow a stream of reads to
44  * persist before looking to see whether it is time to switch over to writes.
45  */
46 #define default_read_batch_expire (HZ / 4)
47
48 /*
49  * write_batch_expire describes how long we want a stream of writes to run for.
50  * This is not a hard limit, but a target we set for the auto-tuning thingy.
51  * See, the problem is: we can send a lot of writes to disk cache / TCQ in
52  * a short amount of time...
53  */
54 #define default_write_batch_expire (HZ / 16)
55
56 /*
57  * max time we may wait to anticipate a read (default around 6ms)
58  */
59 #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
60
61 /*
62  * Keep track of up to 20ms thinktimes. We can go as big as we like here,
63  * however huge values tend to interfere and not decay fast enough. A program
64  * might be in a non-io phase of operation. Waiting on user input for example,
65  * or doing a lengthy computation. A small penalty can be justified there, and
66  * will still catch out those processes that constantly have large thinktimes.
67  */
68 #define MAX_THINKTIME (HZ/50UL)
69
70 /* Bits in as_io_context.state */
71 enum as_io_states {
72         AS_TASK_RUNNING=0,      /* Process has not exitted */
73         AS_TASK_IOSTARTED,      /* Process has started some IO */
74         AS_TASK_IORUNNING,      /* Process has completed some IO */
75 };
76
77 enum anticipation_status {
78         ANTIC_OFF=0,            /* Not anticipating (normal operation)  */
79         ANTIC_WAIT_REQ,         /* The last read has not yet completed  */
80         ANTIC_WAIT_NEXT,        /* Currently anticipating a request vs
81                                    last read (which has completed) */
82         ANTIC_FINISHED,         /* Anticipating but have found a candidate
83                                  * or timed out */
84 };
85
86 struct as_data {
87         /*
88          * run time data
89          */
90
91         struct request_queue *q;        /* the "owner" queue */
92
93         /*
94          * requests (as_rq s) are present on both sort_list and fifo_list
95          */
96         struct rb_root sort_list[2];
97         struct list_head fifo_list[2];
98
99         struct as_rq *next_arq[2];      /* next in sort order */
100         sector_t last_sector[2];        /* last REQ_SYNC & REQ_ASYNC sectors */
101         struct list_head *dispatch;     /* driver dispatch queue */
102         struct list_head *hash;         /* request hash */
103
104         unsigned long exit_prob;        /* probability a task will exit while
105                                            being waited on */
106         unsigned long new_ttime_total;  /* mean thinktime on new proc */
107         unsigned long new_ttime_mean;
108         u64 new_seek_total;             /* mean seek on new proc */
109         sector_t new_seek_mean;
110
111         unsigned long current_batch_expires;
112         unsigned long last_check_fifo[2];
113         int changed_batch;              /* 1: waiting for old batch to end */
114         int new_batch;                  /* 1: waiting on first read complete */
115         int batch_data_dir;             /* current batch REQ_SYNC / REQ_ASYNC */
116         int write_batch_count;          /* max # of reqs in a write batch */
117         int current_write_count;        /* how many requests left this batch */
118         int write_batch_idled;          /* has the write batch gone idle? */
119         mempool_t *arq_pool;
120
121         enum anticipation_status antic_status;
122         unsigned long antic_start;      /* jiffies: when it started */
123         struct timer_list antic_timer;  /* anticipatory scheduling timer */
124         struct work_struct antic_work;  /* Deferred unplugging */
125         struct io_context *io_context;  /* Identify the expected process */
126         int ioc_finished; /* IO associated with io_context is finished */
127         int nr_dispatched;
128
129         /*
130          * settings that change how the i/o scheduler behaves
131          */
132         unsigned long fifo_expire[2];
133         unsigned long batch_expire[2];
134         unsigned long antic_expire;
135 };
136
137 #define list_entry_fifo(ptr)    list_entry((ptr), struct as_rq, fifo)
138
139 /*
140  * per-request data.
141  */
142 enum arq_state {
143         AS_RQ_NEW=0,            /* New - not referenced and not on any lists */
144         AS_RQ_QUEUED,           /* In the request queue. It belongs to the
145                                    scheduler */
146         AS_RQ_DISPATCHED,       /* On the dispatch list. It belongs to the
147                                    driver now */
148         AS_RQ_PRESCHED,         /* Debug poisoning for requests being used */
149         AS_RQ_REMOVED,
150         AS_RQ_MERGED,
151         AS_RQ_POSTSCHED,        /* when they shouldn't be */
152 };
153
154 struct as_rq {
155         /*
156          * rbtree index, key is the starting offset
157          */
158         struct rb_node rb_node;
159         sector_t rb_key;
160
161         struct request *request;
162
163         struct io_context *io_context;  /* The submitting task */
164
165         /*
166          * request hash, key is the ending offset (for back merge lookup)
167          */
168         struct list_head hash;
169         unsigned int on_hash;
170
171         /*
172          * expire fifo
173          */
174         struct list_head fifo;
175         unsigned long expires;
176
177         unsigned int is_sync;
178         enum arq_state state;
179 };
180
181 #define RQ_DATA(rq)     ((struct as_rq *) (rq)->elevator_private)
182
183 static kmem_cache_t *arq_pool;
184
185 /*
186  * IO Context helper functions
187  */
188
189 /* Called to deallocate the as_io_context */
190 static void free_as_io_context(struct as_io_context *aic)
191 {
192         kfree(aic);
193 }
194
195 /* Called when the task exits */
196 static void exit_as_io_context(struct as_io_context *aic)
197 {
198         WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
199         clear_bit(AS_TASK_RUNNING, &aic->state);
200 }
201
202 static struct as_io_context *alloc_as_io_context(void)
203 {
204         struct as_io_context *ret;
205
206         ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
207         if (ret) {
208                 ret->dtor = free_as_io_context;
209                 ret->exit = exit_as_io_context;
210                 ret->state = 1 << AS_TASK_RUNNING;
211                 atomic_set(&ret->nr_queued, 0);
212                 atomic_set(&ret->nr_dispatched, 0);
213                 spin_lock_init(&ret->lock);
214                 ret->ttime_total = 0;
215                 ret->ttime_samples = 0;
216                 ret->ttime_mean = 0;
217                 ret->seek_total = 0;
218                 ret->seek_samples = 0;
219                 ret->seek_mean = 0;
220         }
221
222         return ret;
223 }
224
225 /*
226  * If the current task has no AS IO context then create one and initialise it.
227  * Then take a ref on the task's io context and return it.
228  */
229 static struct io_context *as_get_io_context(void)
230 {
231         struct io_context *ioc = get_io_context(GFP_ATOMIC);
232         if (ioc && !ioc->aic) {
233                 ioc->aic = alloc_as_io_context();
234                 if (!ioc->aic) {
235                         put_io_context(ioc);
236                         ioc = NULL;
237                 }
238         }
239         return ioc;
240 }
241
242 /*
243  * the back merge hash support functions
244  */
245 static const int as_hash_shift = 6;
246 #define AS_HASH_BLOCK(sec)      ((sec) >> 3)
247 #define AS_HASH_FN(sec)         (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
248 #define AS_HASH_ENTRIES         (1 << as_hash_shift)
249 #define rq_hash_key(rq)         ((rq)->sector + (rq)->nr_sectors)
250 #define list_entry_hash(ptr)    list_entry((ptr), struct as_rq, hash)
251
252 static inline void __as_del_arq_hash(struct as_rq *arq)
253 {
254         arq->on_hash = 0;
255         list_del_init(&arq->hash);
256 }
257
258 static inline void as_del_arq_hash(struct as_rq *arq)
259 {
260         if (arq->on_hash)
261                 __as_del_arq_hash(arq);
262 }
263
264 static void as_remove_merge_hints(request_queue_t *q, struct as_rq *arq)
265 {
266         as_del_arq_hash(arq);
267
268         if (q->last_merge == arq->request)
269                 q->last_merge = NULL;
270 }
271
272 static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
273 {
274         struct request *rq = arq->request;
275
276         BUG_ON(arq->on_hash);
277
278         arq->on_hash = 1;
279         list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
280 }
281
282 /*
283  * move hot entry to front of chain
284  */
285 static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
286 {
287         struct request *rq = arq->request;
288         struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
289
290         if (!arq->on_hash) {
291                 WARN_ON(1);
292                 return;
293         }
294
295         if (arq->hash.prev != head) {
296                 list_del(&arq->hash);
297                 list_add(&arq->hash, head);
298         }
299 }
300
301 static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
302 {
303         struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
304         struct list_head *entry, *next = hash_list->next;
305
306         while ((entry = next) != hash_list) {
307                 struct as_rq *arq = list_entry_hash(entry);
308                 struct request *__rq = arq->request;
309
310                 next = entry->next;
311
312                 BUG_ON(!arq->on_hash);
313
314                 if (!rq_mergeable(__rq)) {
315                         as_remove_merge_hints(ad->q, arq);
316                         continue;
317                 }
318
319                 if (rq_hash_key(__rq) == offset)
320                         return __rq;
321         }
322
323         return NULL;
324 }
325
326 /*
327  * rb tree support functions
328  */
329 #define RB_NONE         (2)
330 #define RB_EMPTY(root)  ((root)->rb_node == NULL)
331 #define ON_RB(node)     ((node)->rb_color != RB_NONE)
332 #define RB_CLEAR(node)  ((node)->rb_color = RB_NONE)
333 #define rb_entry_arq(node)      rb_entry((node), struct as_rq, rb_node)
334 #define ARQ_RB_ROOT(ad, arq)    (&(ad)->sort_list[(arq)->is_sync])
335 #define rq_rb_key(rq)           (rq)->sector
336
337 /*
338  * as_find_first_arq finds the first (lowest sector numbered) request
339  * for the specified data_dir. Used to sweep back to the start of the disk
340  * (1-way elevator) after we process the last (highest sector) request.
341  */
342 static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
343 {
344         struct rb_node *n = ad->sort_list[data_dir].rb_node;
345
346         if (n == NULL)
347                 return NULL;
348
349         for (;;) {
350                 if (n->rb_left == NULL)
351                         return rb_entry_arq(n);
352
353                 n = n->rb_left;
354         }
355 }
356
357 /*
358  * Add the request to the rb tree if it is unique.  If there is an alias (an
359  * existing request against the same sector), which can happen when using
360  * direct IO, then return the alias.
361  */
362 static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
363 {
364         struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
365         struct rb_node *parent = NULL;
366         struct as_rq *__arq;
367         struct request *rq = arq->request;
368
369         arq->rb_key = rq_rb_key(rq);
370
371         while (*p) {
372                 parent = *p;
373                 __arq = rb_entry_arq(parent);
374
375                 if (arq->rb_key < __arq->rb_key)
376                         p = &(*p)->rb_left;
377                 else if (arq->rb_key > __arq->rb_key)
378                         p = &(*p)->rb_right;
379                 else
380                         return __arq;
381         }
382
383         rb_link_node(&arq->rb_node, parent, p);
384         rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
385
386         return NULL;
387 }
388
389 static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
390 {
391         if (!ON_RB(&arq->rb_node)) {
392                 WARN_ON(1);
393                 return;
394         }
395
396         rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
397         RB_CLEAR(&arq->rb_node);
398 }
399
400 static struct request *
401 as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
402 {
403         struct rb_node *n = ad->sort_list[data_dir].rb_node;
404         struct as_rq *arq;
405
406         while (n) {
407                 arq = rb_entry_arq(n);
408
409                 if (sector < arq->rb_key)
410                         n = n->rb_left;
411                 else if (sector > arq->rb_key)
412                         n = n->rb_right;
413                 else
414                         return arq->request;
415         }
416
417         return NULL;
418 }
419
420 /*
421  * IO Scheduler proper
422  */
423
424 #define MAXBACK (1024 * 1024)   /*
425                                  * Maximum distance the disk will go backward
426                                  * for a request.
427                                  */
428
429 #define BACK_PENALTY    2
430
431 /*
432  * as_choose_req selects the preferred one of two requests of the same data_dir
433  * ignoring time - eg. timeouts, which is the job of as_dispatch_request
434  */
435 static struct as_rq *
436 as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
437 {
438         int data_dir;
439         sector_t last, s1, s2, d1, d2;
440         int r1_wrap=0, r2_wrap=0;       /* requests are behind the disk head */
441         const sector_t maxback = MAXBACK;
442
443         if (arq1 == NULL || arq1 == arq2)
444                 return arq2;
445         if (arq2 == NULL)
446                 return arq1;
447
448         data_dir = arq1->is_sync;
449
450         last = ad->last_sector[data_dir];
451         s1 = arq1->request->sector;
452         s2 = arq2->request->sector;
453
454         BUG_ON(data_dir != arq2->is_sync);
455
456         /*
457          * Strict one way elevator _except_ in the case where we allow
458          * short backward seeks which are biased as twice the cost of a
459          * similar forward seek.
460          */
461         if (s1 >= last)
462                 d1 = s1 - last;
463         else if (s1+maxback >= last)
464                 d1 = (last - s1)*BACK_PENALTY;
465         else {
466                 r1_wrap = 1;
467                 d1 = 0; /* shut up, gcc */
468         }
469
470         if (s2 >= last)
471                 d2 = s2 - last;
472         else if (s2+maxback >= last)
473                 d2 = (last - s2)*BACK_PENALTY;
474         else {
475                 r2_wrap = 1;
476                 d2 = 0;
477         }
478
479         /* Found required data */
480         if (!r1_wrap && r2_wrap)
481                 return arq1;
482         else if (!r2_wrap && r1_wrap)
483                 return arq2;
484         else if (r1_wrap && r2_wrap) {
485                 /* both behind the head */
486                 if (s1 <= s2)
487                         return arq1;
488                 else
489                         return arq2;
490         }
491
492         /* Both requests in front of the head */
493         if (d1 < d2)
494                 return arq1;
495         else if (d2 < d1)
496                 return arq2;
497         else {
498                 if (s1 >= s2)
499                         return arq1;
500                 else
501                         return arq2;
502         }
503 }
504
505 /*
506  * as_find_next_arq finds the next request after @prev in elevator order.
507  * this with as_choose_req form the basis for how the scheduler chooses
508  * what request to process next. Anticipation works on top of this.
509  */
510 static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
511 {
512         const int data_dir = last->is_sync;
513         struct as_rq *ret;
514         struct rb_node *rbnext = rb_next(&last->rb_node);
515         struct rb_node *rbprev = rb_prev(&last->rb_node);
516         struct as_rq *arq_next, *arq_prev;
517
518         BUG_ON(!ON_RB(&last->rb_node));
519
520         if (rbprev)
521                 arq_prev = rb_entry_arq(rbprev);
522         else
523                 arq_prev = NULL;
524
525         if (rbnext)
526                 arq_next = rb_entry_arq(rbnext);
527         else {
528                 arq_next = as_find_first_arq(ad, data_dir);
529                 if (arq_next == last)
530                         arq_next = NULL;
531         }
532
533         ret = as_choose_req(ad, arq_next, arq_prev);
534
535         return ret;
536 }
537
538 /*
539  * anticipatory scheduling functions follow
540  */
541
542 /*
543  * as_antic_expired tells us when we have anticipated too long.
544  * The funny "absolute difference" math on the elapsed time is to handle
545  * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
546  */
547 static int as_antic_expired(struct as_data *ad)
548 {
549         long delta_jif;
550
551         delta_jif = jiffies - ad->antic_start;
552         if (unlikely(delta_jif < 0))
553                 delta_jif = -delta_jif;
554         if (delta_jif < ad->antic_expire)
555                 return 0;
556
557         return 1;
558 }
559
560 /*
561  * as_antic_waitnext starts anticipating that a nice request will soon be
562  * submitted. See also as_antic_waitreq
563  */
564 static void as_antic_waitnext(struct as_data *ad)
565 {
566         unsigned long timeout;
567
568         BUG_ON(ad->antic_status != ANTIC_OFF
569                         && ad->antic_status != ANTIC_WAIT_REQ);
570
571         timeout = ad->antic_start + ad->antic_expire;
572
573         mod_timer(&ad->antic_timer, timeout);
574
575         ad->antic_status = ANTIC_WAIT_NEXT;
576 }
577
578 /*
579  * as_antic_waitreq starts anticipating. We don't start timing the anticipation
580  * until the request that we're anticipating on has finished. This means we
581  * are timing from when the candidate process wakes up hopefully.
582  */
583 static void as_antic_waitreq(struct as_data *ad)
584 {
585         BUG_ON(ad->antic_status == ANTIC_FINISHED);
586         if (ad->antic_status == ANTIC_OFF) {
587                 if (!ad->io_context || ad->ioc_finished)
588                         as_antic_waitnext(ad);
589                 else
590                         ad->antic_status = ANTIC_WAIT_REQ;
591         }
592 }
593
594 /*
595  * This is called directly by the functions in this file to stop anticipation.
596  * We kill the timer and schedule a call to the request_fn asap.
597  */
598 static void as_antic_stop(struct as_data *ad)
599 {
600         int status = ad->antic_status;
601
602         if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
603                 if (status == ANTIC_WAIT_NEXT)
604                         del_timer(&ad->antic_timer);
605                 ad->antic_status = ANTIC_FINISHED;
606                 /* see as_work_handler */
607                 kblockd_schedule_work(&ad->antic_work);
608         }
609 }
610
611 /*
612  * as_antic_timeout is the timer function set by as_antic_waitnext.
613  */
614 static void as_antic_timeout(unsigned long data)
615 {
616         struct request_queue *q = (struct request_queue *)data;
617         struct as_data *ad = q->elevator.elevator_data;
618         unsigned long flags;
619
620         spin_lock_irqsave(q->queue_lock, flags);
621         if (ad->antic_status == ANTIC_WAIT_REQ
622                         || ad->antic_status == ANTIC_WAIT_NEXT) {
623                 struct as_io_context *aic = ad->io_context->aic;
624
625                 ad->antic_status = ANTIC_FINISHED;
626                 kblockd_schedule_work(&ad->antic_work);
627
628                 if (aic->ttime_samples == 0) {
629                         /* process anticipated on has exitted or timed out*/
630                         ad->exit_prob = (7*ad->exit_prob + 256)/8;
631                 }
632         }
633         spin_unlock_irqrestore(q->queue_lock, flags);
634 }
635
636 /*
637  * as_close_req decides if one request is considered "close" to the
638  * previous one issued.
639  */
640 static int as_close_req(struct as_data *ad, struct as_rq *arq)
641 {
642         unsigned long delay;    /* milliseconds */
643         sector_t last = ad->last_sector[ad->batch_data_dir];
644         sector_t next = arq->request->sector;
645         sector_t delta; /* acceptable close offset (in sectors) */
646
647         if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
648                 delay = 0;
649         else
650                 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
651
652         if (delay <= 1)
653                 delta = 64;
654         else if (delay <= 20 && delay <= ad->antic_expire)
655                 delta = 64 << (delay-1);
656         else
657                 return 1;
658
659         return (last - (delta>>1) <= next) && (next <= last + delta);
660 }
661
662 /*
663  * as_can_break_anticipation returns true if we have been anticipating this
664  * request.
665  *
666  * It also returns true if the process against which we are anticipating
667  * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
668  * dispatch it ASAP, because we know that application will not be submitting
669  * any new reads.
670  *
671  * If the task which has submitted the request has exitted, break anticipation.
672  *
673  * If this task has queued some other IO, do not enter enticipation.
674  */
675 static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
676 {
677         struct io_context *ioc;
678         struct as_io_context *aic;
679         sector_t s;
680
681         ioc = ad->io_context;
682         BUG_ON(!ioc);
683
684         if (arq && ioc == arq->io_context) {
685                 /* request from same process */
686                 return 1;
687         }
688
689         if (ad->ioc_finished && as_antic_expired(ad)) {
690                 /*
691                  * In this situation status should really be FINISHED,
692                  * however the timer hasn't had the chance to run yet.
693                  */
694                 return 1;
695         }
696
697         aic = ioc->aic;
698         if (!aic)
699                 return 0;
700
701         if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
702                 /* process anticipated on has exitted */
703                 if (aic->ttime_samples == 0)
704                         ad->exit_prob = (7*ad->exit_prob + 256)/8;
705                 return 1;
706         }
707
708         if (atomic_read(&aic->nr_queued) > 0) {
709                 /* process has more requests queued */
710                 return 1;
711         }
712
713         if (atomic_read(&aic->nr_dispatched) > 0) {
714                 /* process has more requests dispatched */
715                 return 1;
716         }
717
718         if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, arq)) {
719                 /*
720                  * Found a close request that is not one of ours.
721                  *
722                  * This makes close requests from another process reset
723                  * our thinktime delay. Is generally useful when there are
724                  * two or more cooperating processes working in the same
725                  * area.
726                  */
727                 spin_lock(&aic->lock);
728                 aic->last_end_request = jiffies;
729                 spin_unlock(&aic->lock);
730                 return 1;
731         }
732
733
734         if (aic->ttime_samples == 0) {
735                 if (ad->new_ttime_mean > ad->antic_expire)
736                         return 1;
737                 if (ad->exit_prob > 128)
738                         return 1;
739         } else if (aic->ttime_mean > ad->antic_expire) {
740                 /* the process thinks too much between requests */
741                 return 1;
742         }
743
744         if (!arq)
745                 return 0;
746
747         if (ad->last_sector[REQ_SYNC] < arq->request->sector)
748                 s = arq->request->sector - ad->last_sector[REQ_SYNC];
749         else
750                 s = ad->last_sector[REQ_SYNC] - arq->request->sector;
751
752         if (aic->seek_samples == 0) {
753                 /*
754                  * Process has just started IO. Use past statistics to
755                  * guage success possibility
756                  */
757                 if (ad->new_seek_mean > s) {
758                         /* this request is better than what we're expecting */
759                         return 1;
760                 }
761
762         } else {
763                 if (aic->seek_mean > s) {
764                         /* this request is better than what we're expecting */
765                         return 1;
766                 }
767         }
768
769         return 0;
770 }
771
772 /*
773  * as_can_anticipate indicates weather we should either run arq
774  * or keep anticipating a better request.
775  */
776 static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
777 {
778         if (!ad->io_context)
779                 /*
780                  * Last request submitted was a write
781                  */
782                 return 0;
783
784         if (ad->antic_status == ANTIC_FINISHED)
785                 /*
786                  * Don't restart if we have just finished. Run the next request
787                  */
788                 return 0;
789
790         if (as_can_break_anticipation(ad, arq))
791                 /*
792                  * This request is a good candidate. Don't keep anticipating,
793                  * run it.
794                  */
795                 return 0;
796
797         /*
798          * OK from here, we haven't finished, and don't have a decent request!
799          * Status is either ANTIC_OFF so start waiting,
800          * ANTIC_WAIT_REQ so continue waiting for request to finish
801          * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
802          *
803          */
804
805         return 1;
806 }
807
808 static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, unsigned long ttime)
809 {
810         /* fixed point: 1.0 == 1<<8 */
811         if (aic->ttime_samples == 0) {
812                 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
813                 ad->new_ttime_mean = ad->new_ttime_total / 256;
814
815                 ad->exit_prob = (7*ad->exit_prob)/8;
816         }
817         aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
818         aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
819         aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
820 }
821
822 static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, sector_t sdist)
823 {
824         u64 total;
825
826         if (aic->seek_samples == 0) {
827                 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
828                 ad->new_seek_mean = ad->new_seek_total / 256;
829         }
830
831         /*
832          * Don't allow the seek distance to get too large from the
833          * odd fragment, pagein, etc
834          */
835         if (aic->seek_samples <= 60) /* second&third seek */
836                 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
837         else
838                 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
839
840         aic->seek_samples = (7*aic->seek_samples + 256) / 8;
841         aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
842         total = aic->seek_total + (aic->seek_samples/2);
843         do_div(total, aic->seek_samples);
844         aic->seek_mean = (sector_t)total;
845 }
846
847 /*
848  * as_update_iohist keeps a decaying histogram of IO thinktimes, and
849  * updates @aic->ttime_mean based on that. It is called when a new
850  * request is queued.
851  */
852 static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, struct request *rq)
853 {
854         struct as_rq *arq = RQ_DATA(rq);
855         int data_dir = arq->is_sync;
856         unsigned long thinktime;
857         sector_t seek_dist;
858
859         if (aic == NULL)
860                 return;
861
862         if (data_dir == REQ_SYNC) {
863                 unsigned long in_flight = atomic_read(&aic->nr_queued)
864                                         + atomic_read(&aic->nr_dispatched);
865                 spin_lock(&aic->lock);
866                 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
867                         test_bit(AS_TASK_IOSTARTED, &aic->state)) {
868                         /* Calculate read -> read thinktime */
869                         if (test_bit(AS_TASK_IORUNNING, &aic->state)
870                                                         && in_flight == 0) {
871                                 thinktime = jiffies - aic->last_end_request;
872                                 thinktime = min(thinktime, MAX_THINKTIME-1);
873                         } else
874                                 thinktime = 0;
875                         as_update_thinktime(ad, aic, thinktime);
876
877                         /* Calculate read -> read seek distance */
878                         if (aic->last_request_pos < rq->sector)
879                                 seek_dist = rq->sector - aic->last_request_pos;
880                         else
881                                 seek_dist = aic->last_request_pos - rq->sector;
882                         as_update_seekdist(ad, aic, seek_dist);
883                 }
884                 aic->last_request_pos = rq->sector + rq->nr_sectors;
885                 set_bit(AS_TASK_IOSTARTED, &aic->state);
886                 spin_unlock(&aic->lock);
887         }
888 }
889
890 /*
891  * as_update_arq must be called whenever a request (arq) is added to
892  * the sort_list. This function keeps caches up to date, and checks if the
893  * request might be one we are "anticipating"
894  */
895 static void as_update_arq(struct as_data *ad, struct as_rq *arq)
896 {
897         const int data_dir = arq->is_sync;
898
899         /* keep the next_arq cache up to date */
900         ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
901
902         /*
903          * have we been anticipating this request?
904          * or does it come from the same process as the one we are anticipating
905          * for?
906          */
907         if (ad->antic_status == ANTIC_WAIT_REQ
908                         || ad->antic_status == ANTIC_WAIT_NEXT) {
909                 if (as_can_break_anticipation(ad, arq))
910                         as_antic_stop(ad);
911         }
912 }
913
914 /*
915  * Gathers timings and resizes the write batch automatically
916  */
917 static void update_write_batch(struct as_data *ad)
918 {
919         unsigned long batch = ad->batch_expire[REQ_ASYNC];
920         long write_time;
921
922         write_time = (jiffies - ad->current_batch_expires) + batch;
923         if (write_time < 0)
924                 write_time = 0;
925
926         if (write_time > batch && !ad->write_batch_idled) {
927                 if (write_time > batch * 3)
928                         ad->write_batch_count /= 2;
929                 else
930                         ad->write_batch_count--;
931         } else if (write_time < batch && ad->current_write_count == 0) {
932                 if (batch > write_time * 3)
933                         ad->write_batch_count *= 2;
934                 else
935                         ad->write_batch_count++;
936         }
937
938         if (ad->write_batch_count < 1)
939                 ad->write_batch_count = 1;
940 }
941
942 /*
943  * as_completed_request is to be called when a request has completed and
944  * returned something to the requesting process, be it an error or data.
945  */
946 static void as_completed_request(request_queue_t *q, struct request *rq)
947 {
948         struct as_data *ad = q->elevator.elevator_data;
949         struct as_rq *arq = RQ_DATA(rq);
950
951         WARN_ON(!list_empty(&rq->queuelist));
952
953         if (arq->state == AS_RQ_PRESCHED) {
954                 WARN_ON(arq->io_context);
955                 goto out;
956         }
957
958         if (arq->state == AS_RQ_MERGED)
959                 goto out_ioc;
960
961         if (arq->state != AS_RQ_REMOVED) {
962                 printk("arq->state %d\n", arq->state);
963                 WARN_ON(1);
964                 goto out;
965         }
966
967         if (!blk_fs_request(rq))
968                 goto out;
969
970         if (ad->changed_batch && ad->nr_dispatched == 1) {
971                 kblockd_schedule_work(&ad->antic_work);
972                 ad->changed_batch = 0;
973
974                 if (ad->batch_data_dir == REQ_SYNC)
975                         ad->new_batch = 1;
976         }
977         WARN_ON(ad->nr_dispatched == 0);
978         ad->nr_dispatched--;
979
980         /*
981          * Start counting the batch from when a request of that direction is
982          * actually serviced. This should help devices with big TCQ windows
983          * and writeback caches
984          */
985         if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
986                 update_write_batch(ad);
987                 ad->current_batch_expires = jiffies +
988                                 ad->batch_expire[REQ_SYNC];
989                 ad->new_batch = 0;
990         }
991
992         if (ad->io_context == arq->io_context && ad->io_context) {
993                 ad->antic_start = jiffies;
994                 ad->ioc_finished = 1;
995                 if (ad->antic_status == ANTIC_WAIT_REQ) {
996                         /*
997                          * We were waiting on this request, now anticipate
998                          * the next one
999                          */
1000                         as_antic_waitnext(ad);
1001                 }
1002         }
1003
1004 out_ioc:
1005         if (!arq->io_context)
1006                 goto out;
1007
1008         if (arq->is_sync == REQ_SYNC) {
1009                 struct as_io_context *aic = arq->io_context->aic;
1010                 if (aic) {
1011                         spin_lock(&aic->lock);
1012                         set_bit(AS_TASK_IORUNNING, &aic->state);
1013                         aic->last_end_request = jiffies;
1014                         spin_unlock(&aic->lock);
1015                 }
1016         }
1017
1018         put_io_context(arq->io_context);
1019 out:
1020         arq->state = AS_RQ_POSTSCHED;
1021 }
1022
1023 /*
1024  * as_remove_queued_request removes a request from the pre dispatch queue
1025  * without updating refcounts. It is expected the caller will drop the
1026  * reference unless it replaces the request at somepart of the elevator
1027  * (ie. the dispatch queue)
1028  */
1029 static void as_remove_queued_request(request_queue_t *q, struct request *rq)
1030 {
1031         struct as_rq *arq = RQ_DATA(rq);
1032         const int data_dir = arq->is_sync;
1033         struct as_data *ad = q->elevator.elevator_data;
1034
1035         WARN_ON(arq->state != AS_RQ_QUEUED);
1036
1037         if (arq->io_context && arq->io_context->aic) {
1038                 BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
1039                 atomic_dec(&arq->io_context->aic->nr_queued);
1040         }
1041
1042         /*
1043          * Update the "next_arq" cache if we are about to remove its
1044          * entry
1045          */
1046         if (ad->next_arq[data_dir] == arq)
1047                 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1048
1049         list_del_init(&arq->fifo);
1050         as_remove_merge_hints(q, arq);
1051         as_del_arq_rb(ad, arq);
1052 }
1053
1054 /*
1055  * as_remove_dispatched_request is called to remove a request which has gone
1056  * to the dispatch list.
1057  */
1058 static void as_remove_dispatched_request(request_queue_t *q, struct request *rq)
1059 {
1060         struct as_rq *arq = RQ_DATA(rq);
1061         struct as_io_context *aic;
1062
1063         if (!arq) {
1064                 WARN_ON(1);
1065                 return;
1066         }
1067
1068         WARN_ON(arq->state != AS_RQ_DISPATCHED);
1069         WARN_ON(ON_RB(&arq->rb_node));
1070         if (arq->io_context && arq->io_context->aic) {
1071                 aic = arq->io_context->aic;
1072                 if (aic) {
1073                         WARN_ON(!atomic_read(&aic->nr_dispatched));
1074                         atomic_dec(&aic->nr_dispatched);
1075                 }
1076         }
1077 }
1078
1079 /*
1080  * as_remove_request is called when a driver has finished with a request.
1081  * This should be only called for dispatched requests, but for some reason
1082  * a POWER4 box running hwscan it does not.
1083  */
1084 static void as_remove_request(request_queue_t *q, struct request *rq)
1085 {
1086         struct as_rq *arq = RQ_DATA(rq);
1087
1088         if (unlikely(arq->state == AS_RQ_NEW))
1089                 goto out;
1090
1091         if (ON_RB(&arq->rb_node)) {
1092                 if (arq->state != AS_RQ_QUEUED) {
1093                         printk("arq->state %d\n", arq->state);
1094                         WARN_ON(1);
1095                         goto out;
1096                 }
1097                 /*
1098                  * We'll lose the aliased request(s) here. I don't think this
1099                  * will ever happen, but if it does, hopefully someone will
1100                  * report it.
1101                  */
1102                 WARN_ON(!list_empty(&rq->queuelist));
1103                 as_remove_queued_request(q, rq);
1104         } else {
1105                 if (arq->state != AS_RQ_DISPATCHED) {
1106                         printk("arq->state %d\n", arq->state);
1107                         WARN_ON(1);
1108                         goto out;
1109                 }
1110                 as_remove_dispatched_request(q, rq);
1111         }
1112 out:
1113         arq->state = AS_RQ_REMOVED;
1114 }
1115
1116 /*
1117  * as_fifo_expired returns 0 if there are no expired reads on the fifo,
1118  * 1 otherwise.  It is ratelimited so that we only perform the check once per
1119  * `fifo_expire' interval.  Otherwise a large number of expired requests
1120  * would create a hopeless seekstorm.
1121  *
1122  * See as_antic_expired comment.
1123  */
1124 static int as_fifo_expired(struct as_data *ad, int adir)
1125 {
1126         struct as_rq *arq;
1127         long delta_jif;
1128
1129         delta_jif = jiffies - ad->last_check_fifo[adir];
1130         if (unlikely(delta_jif < 0))
1131                 delta_jif = -delta_jif;
1132         if (delta_jif < ad->fifo_expire[adir])
1133                 return 0;
1134
1135         ad->last_check_fifo[adir] = jiffies;
1136
1137         if (list_empty(&ad->fifo_list[adir]))
1138                 return 0;
1139
1140         arq = list_entry_fifo(ad->fifo_list[adir].next);
1141
1142         return time_after(jiffies, arq->expires);
1143 }
1144
1145 /*
1146  * as_batch_expired returns true if the current batch has expired. A batch
1147  * is a set of reads or a set of writes.
1148  */
1149 static inline int as_batch_expired(struct as_data *ad)
1150 {
1151         if (ad->changed_batch || ad->new_batch)
1152                 return 0;
1153
1154         if (ad->batch_data_dir == REQ_SYNC)
1155                 /* TODO! add a check so a complete fifo gets written? */
1156                 return time_after(jiffies, ad->current_batch_expires);
1157
1158         return time_after(jiffies, ad->current_batch_expires)
1159                 || ad->current_write_count == 0;
1160 }
1161
1162 /*
1163  * move an entry to dispatch queue
1164  */
1165 static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
1166 {
1167         struct request *rq = arq->request;
1168         struct list_head *insert;
1169         const int data_dir = arq->is_sync;
1170
1171         BUG_ON(!ON_RB(&arq->rb_node));
1172
1173         as_antic_stop(ad);
1174         ad->antic_status = ANTIC_OFF;
1175
1176         /*
1177          * This has to be set in order to be correctly updated by
1178          * as_find_next_arq
1179          */
1180         ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
1181
1182         if (data_dir == REQ_SYNC) {
1183                 /* In case we have to anticipate after this */
1184                 copy_io_context(&ad->io_context, &arq->io_context);
1185         } else {
1186                 if (ad->io_context) {
1187                         put_io_context(ad->io_context);
1188                         ad->io_context = NULL;
1189                 }
1190
1191                 if (ad->current_write_count != 0)
1192                         ad->current_write_count--;
1193         }
1194         ad->ioc_finished = 0;
1195
1196         ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1197
1198         /*
1199          * take it off the sort and fifo list, add to dispatch queue
1200          */
1201         insert = ad->dispatch->prev;
1202
1203         while (!list_empty(&rq->queuelist)) {
1204                 struct request *__rq = list_entry_rq(rq->queuelist.next);
1205                 struct as_rq *__arq = RQ_DATA(__rq);
1206
1207                 list_move_tail(&__rq->queuelist, ad->dispatch);
1208
1209                 if (__arq->io_context && __arq->io_context->aic)
1210                         atomic_inc(&__arq->io_context->aic->nr_dispatched);
1211
1212                 WARN_ON(__arq->state != AS_RQ_QUEUED);
1213                 __arq->state = AS_RQ_DISPATCHED;
1214
1215                 ad->nr_dispatched++;
1216         }
1217
1218         as_remove_queued_request(ad->q, rq);
1219         list_add(&rq->queuelist, insert);
1220         if (arq->io_context && arq->io_context->aic)
1221                 atomic_inc(&arq->io_context->aic->nr_dispatched);
1222
1223         WARN_ON(arq->state != AS_RQ_QUEUED);
1224         arq->state = AS_RQ_DISPATCHED;
1225
1226         ad->nr_dispatched++;
1227 }
1228
1229 /*
1230  * as_dispatch_request selects the best request according to
1231  * read/write expire, batch expire, etc, and moves it to the dispatch
1232  * queue. Returns 1 if a request was found, 0 otherwise.
1233  */
1234 static int as_dispatch_request(struct as_data *ad)
1235 {
1236         struct as_rq *arq;
1237         const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1238         const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
1239
1240         /* Signal that the write batch was uncontended, so we can't time it */
1241         if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1242                 if (ad->current_write_count == 0 || !writes)
1243                         ad->write_batch_idled = 1;
1244         }
1245
1246         if (!(reads || writes)
1247                 || ad->antic_status == ANTIC_WAIT_REQ
1248                 || ad->antic_status == ANTIC_WAIT_NEXT
1249                 || ad->changed_batch)
1250                 return 0;
1251
1252         if (!(reads && writes && as_batch_expired(ad)) ) {
1253                 /*
1254                  * batch is still running or no reads or no writes
1255                  */
1256                 arq = ad->next_arq[ad->batch_data_dir];
1257
1258                 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1259                         if (as_fifo_expired(ad, REQ_SYNC))
1260                                 goto fifo_expired;
1261
1262                         if (as_can_anticipate(ad, arq)) {
1263                                 as_antic_waitreq(ad);
1264                                 return 0;
1265                         }
1266                 }
1267
1268                 if (arq) {
1269                         /* we have a "next request" */
1270                         if (reads && !writes)
1271                                 ad->current_batch_expires =
1272                                         jiffies + ad->batch_expire[REQ_SYNC];
1273                         goto dispatch_request;
1274                 }
1275         }
1276
1277         /*
1278          * at this point we are not running a batch. select the appropriate
1279          * data direction (read / write)
1280          */
1281
1282         if (reads) {
1283                 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC]));
1284
1285                 if (writes && ad->batch_data_dir == REQ_SYNC)
1286                         /*
1287                          * Last batch was a read, switch to writes
1288                          */
1289                         goto dispatch_writes;
1290
1291                 if (ad->batch_data_dir == REQ_ASYNC) {
1292                         WARN_ON(ad->new_batch);
1293                         ad->changed_batch = 1;
1294                 }
1295                 ad->batch_data_dir = REQ_SYNC;
1296                 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1297                 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1298                 goto dispatch_request;
1299         }
1300
1301         /*
1302          * the last batch was a read
1303          */
1304
1305         if (writes) {
1306 dispatch_writes:
1307                 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC]));
1308
1309                 if (ad->batch_data_dir == REQ_SYNC) {
1310                         ad->changed_batch = 1;
1311
1312                         /*
1313                          * new_batch might be 1 when the queue runs out of
1314                          * reads. A subsequent submission of a write might
1315                          * cause a change of batch before the read is finished.
1316                          */
1317                         ad->new_batch = 0;
1318                 }
1319                 ad->batch_data_dir = REQ_ASYNC;
1320                 ad->current_write_count = ad->write_batch_count;
1321                 ad->write_batch_idled = 0;
1322                 arq = ad->next_arq[ad->batch_data_dir];
1323                 goto dispatch_request;
1324         }
1325
1326         BUG();
1327         return 0;
1328
1329 dispatch_request:
1330         /*
1331          * If a request has expired, service it.
1332          */
1333
1334         if (as_fifo_expired(ad, ad->batch_data_dir)) {
1335 fifo_expired:
1336                 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1337                 BUG_ON(arq == NULL);
1338         }
1339
1340         if (ad->changed_batch) {
1341                 WARN_ON(ad->new_batch);
1342
1343                 if (ad->nr_dispatched)
1344                         return 0;
1345
1346                 if (ad->batch_data_dir == REQ_ASYNC)
1347                         ad->current_batch_expires = jiffies +
1348                                         ad->batch_expire[REQ_ASYNC];
1349                 else
1350                         ad->new_batch = 1;
1351
1352                 ad->changed_batch = 0;
1353         }
1354
1355         /*
1356          * arq is the selected appropriate request.
1357          */
1358         as_move_to_dispatch(ad, arq);
1359
1360         return 1;
1361 }
1362
1363 static struct request *as_next_request(request_queue_t *q)
1364 {
1365         struct as_data *ad = q->elevator.elevator_data;
1366         struct request *rq = NULL;
1367
1368         /*
1369          * if there are still requests on the dispatch queue, grab the first
1370          */
1371         if (!list_empty(ad->dispatch) || as_dispatch_request(ad))
1372                 rq = list_entry_rq(ad->dispatch->next);
1373
1374         return rq;
1375 }
1376
1377 /*
1378  * Add arq to a list behind alias
1379  */
1380 static inline void
1381 as_add_aliased_request(struct as_data *ad, struct as_rq *arq, struct as_rq *alias)
1382 {
1383         struct request  *req = arq->request;
1384         struct list_head *insert = alias->request->queuelist.prev;
1385
1386         /*
1387          * Transfer list of aliases
1388          */
1389         while (!list_empty(&req->queuelist)) {
1390                 struct request *__rq = list_entry_rq(req->queuelist.next);
1391                 struct as_rq *__arq = RQ_DATA(__rq);
1392
1393                 list_move_tail(&__rq->queuelist, &alias->request->queuelist);
1394
1395                 WARN_ON(__arq->state != AS_RQ_QUEUED);
1396         }
1397
1398         /*
1399          * Another request with the same start sector on the rbtree.
1400          * Link this request to that sector. They are untangled in
1401          * as_move_to_dispatch
1402          */
1403         list_add(&arq->request->queuelist, insert);
1404
1405         /*
1406          * Don't want to have to handle merges.
1407          */
1408         as_remove_merge_hints(ad->q, arq);
1409 }
1410
1411 /*
1412  * add arq to rbtree and fifo
1413  */
1414 static void as_add_request(struct as_data *ad, struct as_rq *arq)
1415 {
1416         struct as_rq *alias;
1417         int data_dir;
1418
1419         if (rq_data_dir(arq->request) == READ
1420                         || current->flags&PF_SYNCWRITE)
1421                 arq->is_sync = 1;
1422         else
1423                 arq->is_sync = 0;
1424         data_dir = arq->is_sync;
1425
1426         arq->io_context = as_get_io_context();
1427
1428         if (arq->io_context) {
1429                 as_update_iohist(ad, arq->io_context->aic, arq->request);
1430                 atomic_inc(&arq->io_context->aic->nr_queued);
1431         }
1432
1433         alias = as_add_arq_rb(ad, arq);
1434         if (!alias) {
1435                 /*
1436                  * set expire time (only used for reads) and add to fifo list
1437                  */
1438                 arq->expires = jiffies + ad->fifo_expire[data_dir];
1439                 list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
1440
1441                 if (rq_mergeable(arq->request)) {
1442                         as_add_arq_hash(ad, arq);
1443
1444                         if (!ad->q->last_merge)
1445                                 ad->q->last_merge = arq->request;
1446                 }
1447                 as_update_arq(ad, arq); /* keep state machine up to date */
1448
1449         } else {
1450                 as_add_aliased_request(ad, arq, alias);
1451
1452                 /*
1453                  * have we been anticipating this request?
1454                  * or does it come from the same process as the one we are
1455                  * anticipating for?
1456                  */
1457                 if (ad->antic_status == ANTIC_WAIT_REQ
1458                                 || ad->antic_status == ANTIC_WAIT_NEXT) {
1459                         if (as_can_break_anticipation(ad, arq))
1460                                 as_antic_stop(ad);
1461                 }
1462         }
1463
1464         arq->state = AS_RQ_QUEUED;
1465 }
1466
1467 /*
1468  * requeue the request. The request has not been completed, nor is it a
1469  * new request, so don't touch accounting.
1470  */
1471 static void as_requeue_request(request_queue_t *q, struct request *rq)
1472 {
1473         struct as_data *ad = q->elevator.elevator_data;
1474         struct as_rq *arq = RQ_DATA(rq);
1475
1476         if (arq) {
1477                 if (arq->state != AS_RQ_REMOVED) {
1478                         printk("arq->state %d\n", arq->state);
1479                         WARN_ON(1);
1480                 }
1481
1482                 arq->state = AS_RQ_DISPATCHED;
1483                 if (arq->io_context && arq->io_context->aic)
1484                         atomic_inc(&arq->io_context->aic->nr_dispatched);
1485         } else
1486                 WARN_ON(blk_fs_request(rq)
1487                         && (!(rq->flags & (REQ_HARDBARRIER|REQ_SOFTBARRIER))) );
1488
1489         list_add(&rq->queuelist, ad->dispatch);
1490
1491         /* Stop anticipating - let this request get through */
1492         as_antic_stop(ad);
1493 }
1494
1495 static void
1496 as_insert_request(request_queue_t *q, struct request *rq, int where)
1497 {
1498         struct as_data *ad = q->elevator.elevator_data;
1499         struct as_rq *arq = RQ_DATA(rq);
1500
1501         if (arq) {
1502                 if (arq->state != AS_RQ_PRESCHED) {
1503                         printk("arq->state: %d\n", arq->state);
1504                         WARN_ON(1);
1505                 }
1506                 arq->state = AS_RQ_NEW;
1507         }
1508
1509         /* barriers must flush the reorder queue */
1510         if (unlikely(rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)
1511                         && where == ELEVATOR_INSERT_SORT)) {
1512                 WARN_ON(1);
1513                 where = ELEVATOR_INSERT_BACK;
1514         }
1515
1516         switch (where) {
1517                 case ELEVATOR_INSERT_BACK:
1518                         while (ad->next_arq[REQ_SYNC])
1519                                 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1520
1521                         while (ad->next_arq[REQ_ASYNC])
1522                                 as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
1523
1524                         list_add_tail(&rq->queuelist, ad->dispatch);
1525                         as_antic_stop(ad);
1526                         break;
1527                 case ELEVATOR_INSERT_FRONT:
1528                         list_add(&rq->queuelist, ad->dispatch);
1529                         as_antic_stop(ad);
1530                         break;
1531                 case ELEVATOR_INSERT_SORT:
1532                         BUG_ON(!blk_fs_request(rq));
1533                         as_add_request(ad, arq);
1534                         break;
1535                 default:
1536                         BUG();
1537                         return;
1538         }
1539 }
1540
1541 /*
1542  * as_queue_empty tells us if there are requests left in the device. It may
1543  * not be the case that a driver can get the next request even if the queue
1544  * is not empty - it is used in the block layer to check for plugging and
1545  * merging opportunities
1546  */
1547 static int as_queue_empty(request_queue_t *q)
1548 {
1549         struct as_data *ad = q->elevator.elevator_data;
1550
1551         if (!list_empty(&ad->fifo_list[REQ_ASYNC])
1552                 || !list_empty(&ad->fifo_list[REQ_SYNC])
1553                 || !list_empty(ad->dispatch))
1554                         return 0;
1555
1556         return 1;
1557 }
1558
1559 static struct request *
1560 as_former_request(request_queue_t *q, struct request *rq)
1561 {
1562         struct as_rq *arq = RQ_DATA(rq);
1563         struct rb_node *rbprev = rb_prev(&arq->rb_node);
1564         struct request *ret = NULL;
1565
1566         if (rbprev)
1567                 ret = rb_entry_arq(rbprev)->request;
1568
1569         return ret;
1570 }
1571
1572 static struct request *
1573 as_latter_request(request_queue_t *q, struct request *rq)
1574 {
1575         struct as_rq *arq = RQ_DATA(rq);
1576         struct rb_node *rbnext = rb_next(&arq->rb_node);
1577         struct request *ret = NULL;
1578
1579         if (rbnext)
1580                 ret = rb_entry_arq(rbnext)->request;
1581
1582         return ret;
1583 }
1584
1585 static int
1586 as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1587 {
1588         struct as_data *ad = q->elevator.elevator_data;
1589         sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1590         struct request *__rq;
1591         int ret;
1592
1593         /*
1594          * try last_merge to avoid going to hash
1595          */
1596         ret = elv_try_last_merge(q, bio);
1597         if (ret != ELEVATOR_NO_MERGE) {
1598                 __rq = q->last_merge;
1599                 goto out_insert;
1600         }
1601
1602         /*
1603          * see if the merge hash can satisfy a back merge
1604          */
1605         __rq = as_find_arq_hash(ad, bio->bi_sector);
1606         if (__rq) {
1607                 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
1608
1609                 if (elv_rq_merge_ok(__rq, bio)) {
1610                         ret = ELEVATOR_BACK_MERGE;
1611                         goto out;
1612                 }
1613         }
1614
1615         /*
1616          * check for front merge
1617          */
1618         __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
1619         if (__rq) {
1620                 BUG_ON(rb_key != rq_rb_key(__rq));
1621
1622                 if (elv_rq_merge_ok(__rq, bio)) {
1623                         ret = ELEVATOR_FRONT_MERGE;
1624                         goto out;
1625                 }
1626         }
1627
1628         return ELEVATOR_NO_MERGE;
1629 out:
1630         if (rq_mergeable(__rq))
1631                 q->last_merge = __rq;
1632 out_insert:
1633         if (ret) {
1634                 if (rq_mergeable(__rq))
1635                         as_hot_arq_hash(ad, RQ_DATA(__rq));
1636         }
1637         *req = __rq;
1638         return ret;
1639 }
1640
1641 static void as_merged_request(request_queue_t *q, struct request *req)
1642 {
1643         struct as_data *ad = q->elevator.elevator_data;
1644         struct as_rq *arq = RQ_DATA(req);
1645
1646         /*
1647          * hash always needs to be repositioned, key is end sector
1648          */
1649         as_del_arq_hash(arq);
1650         as_add_arq_hash(ad, arq);
1651
1652         /*
1653          * if the merge was a front merge, we need to reposition request
1654          */
1655         if (rq_rb_key(req) != arq->rb_key) {
1656                 struct as_rq *alias, *next_arq = NULL;
1657
1658                 if (ad->next_arq[arq->is_sync] == arq)
1659                         next_arq = as_find_next_arq(ad, arq);
1660
1661                 /*
1662                  * Note! We should really be moving any old aliased requests
1663                  * off this request and try to insert them into the rbtree. We
1664                  * currently don't bother. Ditto the next function.
1665                  */
1666                 as_del_arq_rb(ad, arq);
1667                 if ((alias = as_add_arq_rb(ad, arq)) ) {
1668                         list_del_init(&arq->fifo);
1669                         as_add_aliased_request(ad, arq, alias);
1670                         if (next_arq)
1671                                 ad->next_arq[arq->is_sync] = next_arq;
1672                 }
1673                 /*
1674                  * Note! At this stage of this and the next function, our next
1675                  * request may not be optimal - eg the request may have "grown"
1676                  * behind the disk head. We currently don't bother adjusting.
1677                  */
1678         }
1679
1680         if (arq->on_hash)
1681                 q->last_merge = req;
1682 }
1683
1684 static void
1685 as_merged_requests(request_queue_t *q, struct request *req,
1686                          struct request *next)
1687 {
1688         struct as_data *ad = q->elevator.elevator_data;
1689         struct as_rq *arq = RQ_DATA(req);
1690         struct as_rq *anext = RQ_DATA(next);
1691
1692         BUG_ON(!arq);
1693         BUG_ON(!anext);
1694
1695         /*
1696          * reposition arq (this is the merged request) in hash, and in rbtree
1697          * in case of a front merge
1698          */
1699         as_del_arq_hash(arq);
1700         as_add_arq_hash(ad, arq);
1701
1702         if (rq_rb_key(req) != arq->rb_key) {
1703                 struct as_rq *alias, *next_arq = NULL;
1704
1705                 if (ad->next_arq[arq->is_sync] == arq)
1706                         next_arq = as_find_next_arq(ad, arq);
1707
1708                 as_del_arq_rb(ad, arq);
1709                 if ((alias = as_add_arq_rb(ad, arq)) ) {
1710                         list_del_init(&arq->fifo);
1711                         as_add_aliased_request(ad, arq, alias);
1712                         if (next_arq)
1713                                 ad->next_arq[arq->is_sync] = next_arq;
1714                 }
1715         }
1716
1717         /*
1718          * if anext expires before arq, assign its expire time to arq
1719          * and move into anext position (anext will be deleted) in fifo
1720          */
1721         if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
1722                 if (time_before(anext->expires, arq->expires)) {
1723                         list_move(&arq->fifo, &anext->fifo);
1724                         arq->expires = anext->expires;
1725                         /*
1726                          * Don't copy here but swap, because when anext is
1727                          * removed below, it must contain the unused context
1728                          */
1729                         swap_io_context(&arq->io_context, &anext->io_context);
1730                 }
1731         }
1732
1733         /*
1734          * Transfer list of aliases
1735          */
1736         while (!list_empty(&next->queuelist)) {
1737                 struct request *__rq = list_entry_rq(next->queuelist.next);
1738                 struct as_rq *__arq = RQ_DATA(__rq);
1739
1740                 list_move_tail(&__rq->queuelist, &req->queuelist);
1741
1742                 WARN_ON(__arq->state != AS_RQ_QUEUED);
1743         }
1744
1745         /*
1746          * kill knowledge of next, this one is a goner
1747          */
1748         as_remove_queued_request(q, next);
1749
1750         anext->state = AS_RQ_MERGED;
1751 }
1752
1753 /*
1754  * This is executed in a "deferred" process context, by kblockd. It calls the
1755  * driver's request_fn so the driver can submit that request.
1756  *
1757  * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1758  * state before calling, and don't rely on any state over calls.
1759  *
1760  * FIXME! dispatch queue is not a queue at all!
1761  */
1762 static void as_work_handler(void *data)
1763 {
1764         struct request_queue *q = data;
1765         unsigned long flags;
1766
1767         spin_lock_irqsave(q->queue_lock, flags);
1768         if (as_next_request(q))
1769                 q->request_fn(q);
1770         spin_unlock_irqrestore(q->queue_lock, flags);
1771 }
1772
1773 static void as_put_request(request_queue_t *q, struct request *rq)
1774 {
1775         struct as_data *ad = q->elevator.elevator_data;
1776         struct as_rq *arq = RQ_DATA(rq);
1777
1778         if (!arq) {
1779                 WARN_ON(1);
1780                 return;
1781         }
1782
1783         if (arq->state != AS_RQ_POSTSCHED && arq->state != AS_RQ_PRESCHED) {
1784                 printk("arq->state %d\n", arq->state);
1785                 WARN_ON(1);
1786         }
1787
1788         mempool_free(arq, ad->arq_pool);
1789         rq->elevator_private = NULL;
1790 }
1791
1792 static int as_set_request(request_queue_t *q, struct request *rq, int gfp_mask)
1793 {
1794         struct as_data *ad = q->elevator.elevator_data;
1795         struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1796
1797         if (arq) {
1798                 memset(arq, 0, sizeof(*arq));
1799                 RB_CLEAR(&arq->rb_node);
1800                 arq->request = rq;
1801                 arq->state = AS_RQ_PRESCHED;
1802                 arq->io_context = NULL;
1803                 INIT_LIST_HEAD(&arq->hash);
1804                 arq->on_hash = 0;
1805                 INIT_LIST_HEAD(&arq->fifo);
1806                 rq->elevator_private = arq;
1807                 return 0;
1808         }
1809
1810         return 1;
1811 }
1812
1813 static int as_may_queue(request_queue_t *q, int rw)
1814 {
1815         int ret = 0;
1816         struct as_data *ad = q->elevator.elevator_data;
1817         struct io_context *ioc;
1818         if (ad->antic_status == ANTIC_WAIT_REQ ||
1819                         ad->antic_status == ANTIC_WAIT_NEXT) {
1820                 ioc = as_get_io_context();
1821                 if (ad->io_context == ioc)
1822                         ret = 1;
1823                 put_io_context(ioc);
1824         }
1825
1826         return ret;
1827 }
1828
1829 static void as_exit(request_queue_t *q, elevator_t *e)
1830 {
1831         struct as_data *ad = e->elevator_data;
1832
1833         del_timer_sync(&ad->antic_timer);
1834         kblockd_flush();
1835
1836         BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1837         BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1838
1839         mempool_destroy(ad->arq_pool);
1840         put_io_context(ad->io_context);
1841         kfree(ad->hash);
1842         kfree(ad);
1843 }
1844
1845 /*
1846  * initialize elevator private data (as_data), and alloc a arq for
1847  * each request on the free lists
1848  */
1849 static int as_init(request_queue_t *q, elevator_t *e)
1850 {
1851         struct as_data *ad;
1852         int i;
1853
1854         if (!arq_pool)
1855                 return -ENOMEM;
1856
1857         ad = kmalloc(sizeof(*ad), GFP_KERNEL);
1858         if (!ad)
1859                 return -ENOMEM;
1860         memset(ad, 0, sizeof(*ad));
1861
1862         ad->q = q; /* Identify what queue the data belongs to */
1863
1864         ad->hash = kmalloc(sizeof(struct list_head)*AS_HASH_ENTRIES,GFP_KERNEL);
1865         if (!ad->hash) {
1866                 kfree(ad);
1867                 return -ENOMEM;
1868         }
1869
1870         ad->arq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, arq_pool);
1871         if (!ad->arq_pool) {
1872                 kfree(ad->hash);
1873                 kfree(ad);
1874                 return -ENOMEM;
1875         }
1876
1877         /* anticipatory scheduling helpers */
1878         ad->antic_timer.function = as_antic_timeout;
1879         ad->antic_timer.data = (unsigned long)q;
1880         init_timer(&ad->antic_timer);
1881         INIT_WORK(&ad->antic_work, as_work_handler, q);
1882
1883         for (i = 0; i < AS_HASH_ENTRIES; i++)
1884                 INIT_LIST_HEAD(&ad->hash[i]);
1885
1886         INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1887         INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1888         ad->sort_list[REQ_SYNC] = RB_ROOT;
1889         ad->sort_list[REQ_ASYNC] = RB_ROOT;
1890         ad->dispatch = &q->queue_head;
1891         ad->fifo_expire[REQ_SYNC] = default_read_expire;
1892         ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1893         ad->antic_expire = default_antic_expire;
1894         ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1895         ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1896         e->elevator_data = ad;
1897
1898         ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1899         ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1900         if (ad->write_batch_count < 2)
1901                 ad->write_batch_count = 2;
1902
1903         return 0;
1904 }
1905
1906 /*
1907  * sysfs parts below
1908  */
1909 struct as_fs_entry {
1910         struct attribute attr;
1911         ssize_t (*show)(struct as_data *, char *);
1912         ssize_t (*store)(struct as_data *, const char *, size_t);
1913 };
1914
1915 static ssize_t
1916 as_var_show(unsigned int var, char *page)
1917 {
1918         var = (var * 1000) / HZ;
1919         return sprintf(page, "%d\n", var);
1920 }
1921
1922 static ssize_t
1923 as_var_store(unsigned long *var, const char *page, size_t count)
1924 {
1925         unsigned long tmp;
1926         char *p = (char *) page;
1927
1928         tmp = simple_strtoul(p, &p, 10);
1929         if (tmp != 0) {
1930                 tmp = (tmp * HZ) / 1000;
1931                 if (tmp == 0)
1932                         tmp = 1;
1933         }
1934         *var = tmp;
1935         return count;
1936 }
1937
1938 static ssize_t as_est_show(struct as_data *ad, char *page)
1939 {
1940         int pos = 0;
1941
1942         pos += sprintf(page+pos, "%lu %% exit probability\n", 100*ad->exit_prob/256);
1943         pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
1944         pos += sprintf(page+pos, "%llu sectors new seek distance\n", (unsigned long long)ad->new_seek_mean);
1945
1946         return pos;
1947 }
1948
1949 #define SHOW_FUNCTION(__FUNC, __VAR)                                    \
1950 static ssize_t __FUNC(struct as_data *ad, char *page)           \
1951 {                                                                       \
1952         return as_var_show(__VAR, (page));                      \
1953 }
1954 SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
1955 SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
1956 SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
1957 SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
1958 SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
1959 #undef SHOW_FUNCTION
1960
1961 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX)                         \
1962 static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count)       \
1963 {                                                                       \
1964         int ret = as_var_store(__PTR, (page), count);           \
1965         if (*(__PTR) < (MIN))                                           \
1966                 *(__PTR) = (MIN);                                       \
1967         else if (*(__PTR) > (MAX))                                      \
1968                 *(__PTR) = (MAX);                                       \
1969         return ret;                                                     \
1970 }
1971 STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1972 STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1973 STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
1974 STORE_FUNCTION(as_read_batchexpire_store,
1975                         &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
1976 STORE_FUNCTION(as_write_batchexpire_store,
1977                         &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1978 #undef STORE_FUNCTION
1979
1980 static struct as_fs_entry as_est_entry = {
1981         .attr = {.name = "est_time", .mode = S_IRUGO },
1982         .show = as_est_show,
1983 };
1984 static struct as_fs_entry as_readexpire_entry = {
1985         .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
1986         .show = as_readexpire_show,
1987         .store = as_readexpire_store,
1988 };
1989 static struct as_fs_entry as_writeexpire_entry = {
1990         .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
1991         .show = as_writeexpire_show,
1992         .store = as_writeexpire_store,
1993 };
1994 static struct as_fs_entry as_anticexpire_entry = {
1995         .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
1996         .show = as_anticexpire_show,
1997         .store = as_anticexpire_store,
1998 };
1999 static struct as_fs_entry as_read_batchexpire_entry = {
2000         .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
2001         .show = as_read_batchexpire_show,
2002         .store = as_read_batchexpire_store,
2003 };
2004 static struct as_fs_entry as_write_batchexpire_entry = {
2005         .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
2006         .show = as_write_batchexpire_show,
2007         .store = as_write_batchexpire_store,
2008 };
2009
2010 static struct attribute *default_attrs[] = {
2011         &as_est_entry.attr,
2012         &as_readexpire_entry.attr,
2013         &as_writeexpire_entry.attr,
2014         &as_anticexpire_entry.attr,
2015         &as_read_batchexpire_entry.attr,
2016         &as_write_batchexpire_entry.attr,
2017         NULL,
2018 };
2019
2020 #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
2021
2022 static ssize_t
2023 as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2024 {
2025         elevator_t *e = container_of(kobj, elevator_t, kobj);
2026         struct as_fs_entry *entry = to_as(attr);
2027
2028         if (!entry->show)
2029                 return 0;
2030
2031         return entry->show(e->elevator_data, page);
2032 }
2033
2034 static ssize_t
2035 as_attr_store(struct kobject *kobj, struct attribute *attr,
2036                     const char *page, size_t length)
2037 {
2038         elevator_t *e = container_of(kobj, elevator_t, kobj);
2039         struct as_fs_entry *entry = to_as(attr);
2040
2041         if (!entry->store)
2042                 return -EINVAL;
2043
2044         return entry->store(e->elevator_data, page, length);
2045 }
2046
2047 static struct sysfs_ops as_sysfs_ops = {
2048         .show   = as_attr_show,
2049         .store  = as_attr_store,
2050 };
2051
2052 static struct kobj_type as_ktype = {
2053         .sysfs_ops      = &as_sysfs_ops,
2054         .default_attrs  = default_attrs,
2055 };
2056
2057 static int __init as_slab_setup(void)
2058 {
2059         arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
2060                                      0, 0, NULL, NULL);
2061
2062         if (!arq_pool)
2063                 panic("as: can't init slab pool\n");
2064
2065         return 0;
2066 }
2067
2068 subsys_initcall(as_slab_setup);
2069
2070 elevator_t iosched_as = {
2071         .elevator_merge_fn =            as_merge,
2072         .elevator_merged_fn =           as_merged_request,
2073         .elevator_merge_req_fn =        as_merged_requests,
2074         .elevator_next_req_fn =         as_next_request,
2075         .elevator_add_req_fn =          as_insert_request,
2076         .elevator_remove_req_fn =       as_remove_request,
2077         .elevator_requeue_req_fn =      as_requeue_request,
2078         .elevator_queue_empty_fn =      as_queue_empty,
2079         .elevator_completed_req_fn =    as_completed_request,
2080         .elevator_former_req_fn =       as_former_request,
2081         .elevator_latter_req_fn =       as_latter_request,
2082         .elevator_set_req_fn =          as_set_request,
2083         .elevator_put_req_fn =          as_put_request,
2084         .elevator_may_queue_fn =        as_may_queue,
2085         .elevator_init_fn =             as_init,
2086         .elevator_exit_fn =             as_exit,
2087
2088         .elevator_ktype =               &as_ktype,
2089         .elevator_name =                "anticipatory",
2090 };
2091
2092 EXPORT_SYMBOL(iosched_as);