[PATCH] cfq-iosched: kill crq
[linux-2.6-block.git] / block / as-iosched.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Anticipatory & deadline i/o scheduler.
3 *
4 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
f5b3db00 5 * Nick Piggin <nickpiggin@yahoo.com.au>
1da177e4
LT
6 *
7 */
8#include <linux/kernel.h>
9#include <linux/fs.h>
10#include <linux/blkdev.h>
11#include <linux/elevator.h>
12#include <linux/bio.h>
1da177e4
LT
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/compiler.h>
1da177e4
LT
17#include <linux/rbtree.h>
18#include <linux/interrupt.h>
19
20#define REQ_SYNC 1
21#define REQ_ASYNC 0
22
23/*
24 * See Documentation/block/as-iosched.txt
25 */
26
27/*
28 * max time before a read is submitted.
29 */
30#define default_read_expire (HZ / 8)
31
32/*
33 * ditto for writes, these limits are not hard, even
34 * if the disk is capable of satisfying them.
35 */
36#define default_write_expire (HZ / 4)
37
38/*
39 * read_batch_expire describes how long we will allow a stream of reads to
40 * persist before looking to see whether it is time to switch over to writes.
41 */
42#define default_read_batch_expire (HZ / 2)
43
44/*
45 * write_batch_expire describes how long we want a stream of writes to run for.
46 * This is not a hard limit, but a target we set for the auto-tuning thingy.
47 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
48 * a short amount of time...
49 */
50#define default_write_batch_expire (HZ / 8)
51
52/*
53 * max time we may wait to anticipate a read (default around 6ms)
54 */
55#define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
56
57/*
58 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
59 * however huge values tend to interfere and not decay fast enough. A program
60 * might be in a non-io phase of operation. Waiting on user input for example,
61 * or doing a lengthy computation. A small penalty can be justified there, and
62 * will still catch out those processes that constantly have large thinktimes.
63 */
64#define MAX_THINKTIME (HZ/50UL)
65
66/* Bits in as_io_context.state */
67enum as_io_states {
f5b3db00 68 AS_TASK_RUNNING=0, /* Process has not exited */
1da177e4
LT
69 AS_TASK_IOSTARTED, /* Process has started some IO */
70 AS_TASK_IORUNNING, /* Process has completed some IO */
71};
72
73enum anticipation_status {
74 ANTIC_OFF=0, /* Not anticipating (normal operation) */
75 ANTIC_WAIT_REQ, /* The last read has not yet completed */
76 ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
77 last read (which has completed) */
78 ANTIC_FINISHED, /* Anticipating but have found a candidate
79 * or timed out */
80};
81
82struct as_data {
83 /*
84 * run time data
85 */
86
87 struct request_queue *q; /* the "owner" queue */
88
89 /*
90 * requests (as_rq s) are present on both sort_list and fifo_list
91 */
92 struct rb_root sort_list[2];
93 struct list_head fifo_list[2];
94
95 struct as_rq *next_arq[2]; /* next in sort order */
96 sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
1da177e4
LT
97
98 unsigned long exit_prob; /* probability a task will exit while
99 being waited on */
f5b3db00
NP
100 unsigned long exit_no_coop; /* probablility an exited task will
101 not be part of a later cooperating
102 request */
1da177e4
LT
103 unsigned long new_ttime_total; /* mean thinktime on new proc */
104 unsigned long new_ttime_mean;
105 u64 new_seek_total; /* mean seek on new proc */
106 sector_t new_seek_mean;
107
108 unsigned long current_batch_expires;
109 unsigned long last_check_fifo[2];
110 int changed_batch; /* 1: waiting for old batch to end */
111 int new_batch; /* 1: waiting on first read complete */
112 int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
113 int write_batch_count; /* max # of reqs in a write batch */
114 int current_write_count; /* how many requests left this batch */
115 int write_batch_idled; /* has the write batch gone idle? */
116 mempool_t *arq_pool;
117
118 enum anticipation_status antic_status;
119 unsigned long antic_start; /* jiffies: when it started */
120 struct timer_list antic_timer; /* anticipatory scheduling timer */
121 struct work_struct antic_work; /* Deferred unplugging */
122 struct io_context *io_context; /* Identify the expected process */
123 int ioc_finished; /* IO associated with io_context is finished */
124 int nr_dispatched;
125
126 /*
127 * settings that change how the i/o scheduler behaves
128 */
129 unsigned long fifo_expire[2];
130 unsigned long batch_expire[2];
131 unsigned long antic_expire;
132};
133
1da177e4
LT
134/*
135 * per-request data.
136 */
137enum arq_state {
138 AS_RQ_NEW=0, /* New - not referenced and not on any lists */
139 AS_RQ_QUEUED, /* In the request queue. It belongs to the
140 scheduler */
141 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
142 driver now */
143 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
144 AS_RQ_REMOVED,
145 AS_RQ_MERGED,
146 AS_RQ_POSTSCHED, /* when they shouldn't be */
147};
148
149struct as_rq {
1da177e4
LT
150 struct request *request;
151
152 struct io_context *io_context; /* The submitting task */
153
1da177e4
LT
154 enum arq_state state;
155};
156
157#define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
158
159static kmem_cache_t *arq_pool;
160
334e94de
AV
161static atomic_t ioc_count = ATOMIC_INIT(0);
162static struct completion *ioc_gone;
163
ef9be1d3
TH
164static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq);
165static void as_antic_stop(struct as_data *ad);
166
1da177e4
LT
167/*
168 * IO Context helper functions
169 */
170
171/* Called to deallocate the as_io_context */
172static void free_as_io_context(struct as_io_context *aic)
173{
174 kfree(aic);
334e94de
AV
175 if (atomic_dec_and_test(&ioc_count) && ioc_gone)
176 complete(ioc_gone);
1da177e4
LT
177}
178
e17a9489
AV
179static void as_trim(struct io_context *ioc)
180{
334e94de
AV
181 if (ioc->aic)
182 free_as_io_context(ioc->aic);
e17a9489
AV
183 ioc->aic = NULL;
184}
185
1da177e4
LT
186/* Called when the task exits */
187static void exit_as_io_context(struct as_io_context *aic)
188{
189 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
190 clear_bit(AS_TASK_RUNNING, &aic->state);
191}
192
193static struct as_io_context *alloc_as_io_context(void)
194{
195 struct as_io_context *ret;
196
197 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
198 if (ret) {
199 ret->dtor = free_as_io_context;
200 ret->exit = exit_as_io_context;
201 ret->state = 1 << AS_TASK_RUNNING;
202 atomic_set(&ret->nr_queued, 0);
203 atomic_set(&ret->nr_dispatched, 0);
204 spin_lock_init(&ret->lock);
205 ret->ttime_total = 0;
206 ret->ttime_samples = 0;
207 ret->ttime_mean = 0;
208 ret->seek_total = 0;
209 ret->seek_samples = 0;
210 ret->seek_mean = 0;
334e94de 211 atomic_inc(&ioc_count);
1da177e4
LT
212 }
213
214 return ret;
215}
216
217/*
218 * If the current task has no AS IO context then create one and initialise it.
219 * Then take a ref on the task's io context and return it.
220 */
221static struct io_context *as_get_io_context(void)
222{
223 struct io_context *ioc = get_io_context(GFP_ATOMIC);
224 if (ioc && !ioc->aic) {
225 ioc->aic = alloc_as_io_context();
226 if (!ioc->aic) {
227 put_io_context(ioc);
228 ioc = NULL;
229 }
230 }
231 return ioc;
232}
233
b4878f24
JA
234static void as_put_io_context(struct as_rq *arq)
235{
236 struct as_io_context *aic;
237
238 if (unlikely(!arq->io_context))
239 return;
240
241 aic = arq->io_context->aic;
242
9e2585a8 243 if (rq_is_sync(arq->request) && aic) {
b4878f24
JA
244 spin_lock(&aic->lock);
245 set_bit(AS_TASK_IORUNNING, &aic->state);
246 aic->last_end_request = jiffies;
247 spin_unlock(&aic->lock);
248 }
249
250 put_io_context(arq->io_context);
251}
252
1da177e4
LT
253/*
254 * rb tree support functions
255 */
9e2585a8 256#define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))])
1da177e4 257
e37f346e 258static void as_add_arq_rb(struct as_data *ad, struct request *rq)
ef9be1d3 259{
e37f346e 260 struct request *alias;
ef9be1d3 261
9e2585a8 262 while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) {
e37f346e 263 as_move_to_dispatch(ad, RQ_DATA(alias));
ef9be1d3
TH
264 as_antic_stop(ad);
265 }
266}
267
e37f346e 268static inline void as_del_arq_rb(struct as_data *ad, struct request *rq)
1da177e4 269{
9e2585a8 270 elv_rb_del(RQ_RB_ROOT(ad, rq), rq);
1da177e4
LT
271}
272
273/*
274 * IO Scheduler proper
275 */
276
277#define MAXBACK (1024 * 1024) /*
278 * Maximum distance the disk will go backward
279 * for a request.
280 */
281
282#define BACK_PENALTY 2
283
284/*
285 * as_choose_req selects the preferred one of two requests of the same data_dir
286 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
287 */
288static struct as_rq *
289as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
290{
291 int data_dir;
292 sector_t last, s1, s2, d1, d2;
293 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
294 const sector_t maxback = MAXBACK;
295
296 if (arq1 == NULL || arq1 == arq2)
297 return arq2;
298 if (arq2 == NULL)
299 return arq1;
300
9e2585a8 301 data_dir = rq_is_sync(arq1->request);
1da177e4
LT
302
303 last = ad->last_sector[data_dir];
304 s1 = arq1->request->sector;
305 s2 = arq2->request->sector;
306
9e2585a8 307 BUG_ON(data_dir != rq_is_sync(arq2->request));
1da177e4
LT
308
309 /*
310 * Strict one way elevator _except_ in the case where we allow
311 * short backward seeks which are biased as twice the cost of a
312 * similar forward seek.
313 */
314 if (s1 >= last)
315 d1 = s1 - last;
316 else if (s1+maxback >= last)
317 d1 = (last - s1)*BACK_PENALTY;
318 else {
319 r1_wrap = 1;
320 d1 = 0; /* shut up, gcc */
321 }
322
323 if (s2 >= last)
324 d2 = s2 - last;
325 else if (s2+maxback >= last)
326 d2 = (last - s2)*BACK_PENALTY;
327 else {
328 r2_wrap = 1;
329 d2 = 0;
330 }
331
332 /* Found required data */
333 if (!r1_wrap && r2_wrap)
334 return arq1;
335 else if (!r2_wrap && r1_wrap)
336 return arq2;
337 else if (r1_wrap && r2_wrap) {
338 /* both behind the head */
339 if (s1 <= s2)
340 return arq1;
341 else
342 return arq2;
343 }
344
345 /* Both requests in front of the head */
346 if (d1 < d2)
347 return arq1;
348 else if (d2 < d1)
349 return arq2;
350 else {
351 if (s1 >= s2)
352 return arq1;
353 else
354 return arq2;
355 }
356}
357
358/*
359 * as_find_next_arq finds the next request after @prev in elevator order.
360 * this with as_choose_req form the basis for how the scheduler chooses
361 * what request to process next. Anticipation works on top of this.
362 */
e37f346e 363static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *arq)
1da177e4 364{
e37f346e 365 struct request *last = arq->request;
1da177e4
LT
366 struct rb_node *rbnext = rb_next(&last->rb_node);
367 struct rb_node *rbprev = rb_prev(&last->rb_node);
e37f346e 368 struct as_rq *next = NULL, *prev = NULL;
1da177e4 369
e37f346e 370 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4
LT
371
372 if (rbprev)
e37f346e 373 prev = RQ_DATA(rb_entry_rq(rbprev));
1da177e4
LT
374
375 if (rbnext)
e37f346e 376 next = RQ_DATA(rb_entry_rq(rbnext));
1da177e4 377 else {
9e2585a8 378 const int data_dir = rq_is_sync(last);
1da177e4 379
e37f346e
JA
380 rbnext = rb_first(&ad->sort_list[data_dir]);
381 if (rbnext && rbnext != &last->rb_node)
382 next = RQ_DATA(rb_entry_rq(rbnext));
383 }
1da177e4 384
e37f346e 385 return as_choose_req(ad, next, prev);
1da177e4
LT
386}
387
388/*
389 * anticipatory scheduling functions follow
390 */
391
392/*
393 * as_antic_expired tells us when we have anticipated too long.
394 * The funny "absolute difference" math on the elapsed time is to handle
395 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
396 */
397static int as_antic_expired(struct as_data *ad)
398{
399 long delta_jif;
400
401 delta_jif = jiffies - ad->antic_start;
402 if (unlikely(delta_jif < 0))
403 delta_jif = -delta_jif;
404 if (delta_jif < ad->antic_expire)
405 return 0;
406
407 return 1;
408}
409
410/*
411 * as_antic_waitnext starts anticipating that a nice request will soon be
412 * submitted. See also as_antic_waitreq
413 */
414static void as_antic_waitnext(struct as_data *ad)
415{
416 unsigned long timeout;
417
418 BUG_ON(ad->antic_status != ANTIC_OFF
419 && ad->antic_status != ANTIC_WAIT_REQ);
420
421 timeout = ad->antic_start + ad->antic_expire;
422
423 mod_timer(&ad->antic_timer, timeout);
424
425 ad->antic_status = ANTIC_WAIT_NEXT;
426}
427
428/*
429 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
430 * until the request that we're anticipating on has finished. This means we
431 * are timing from when the candidate process wakes up hopefully.
432 */
433static void as_antic_waitreq(struct as_data *ad)
434{
435 BUG_ON(ad->antic_status == ANTIC_FINISHED);
436 if (ad->antic_status == ANTIC_OFF) {
437 if (!ad->io_context || ad->ioc_finished)
438 as_antic_waitnext(ad);
439 else
440 ad->antic_status = ANTIC_WAIT_REQ;
441 }
442}
443
444/*
445 * This is called directly by the functions in this file to stop anticipation.
446 * We kill the timer and schedule a call to the request_fn asap.
447 */
448static void as_antic_stop(struct as_data *ad)
449{
450 int status = ad->antic_status;
451
452 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
453 if (status == ANTIC_WAIT_NEXT)
454 del_timer(&ad->antic_timer);
455 ad->antic_status = ANTIC_FINISHED;
456 /* see as_work_handler */
457 kblockd_schedule_work(&ad->antic_work);
458 }
459}
460
461/*
462 * as_antic_timeout is the timer function set by as_antic_waitnext.
463 */
464static void as_antic_timeout(unsigned long data)
465{
466 struct request_queue *q = (struct request_queue *)data;
467 struct as_data *ad = q->elevator->elevator_data;
468 unsigned long flags;
469
470 spin_lock_irqsave(q->queue_lock, flags);
471 if (ad->antic_status == ANTIC_WAIT_REQ
472 || ad->antic_status == ANTIC_WAIT_NEXT) {
473 struct as_io_context *aic = ad->io_context->aic;
474
475 ad->antic_status = ANTIC_FINISHED;
476 kblockd_schedule_work(&ad->antic_work);
477
478 if (aic->ttime_samples == 0) {
f5b3db00 479 /* process anticipated on has exited or timed out*/
1da177e4
LT
480 ad->exit_prob = (7*ad->exit_prob + 256)/8;
481 }
f5b3db00
NP
482 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
483 /* process not "saved" by a cooperating request */
484 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
485 }
1da177e4
LT
486 }
487 spin_unlock_irqrestore(q->queue_lock, flags);
488}
489
f5b3db00
NP
490static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
491 unsigned long ttime)
492{
493 /* fixed point: 1.0 == 1<<8 */
494 if (aic->ttime_samples == 0) {
495 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
496 ad->new_ttime_mean = ad->new_ttime_total / 256;
497
498 ad->exit_prob = (7*ad->exit_prob)/8;
499 }
500 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
501 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
502 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
503}
504
505static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
506 sector_t sdist)
507{
508 u64 total;
509
510 if (aic->seek_samples == 0) {
511 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
512 ad->new_seek_mean = ad->new_seek_total / 256;
513 }
514
515 /*
516 * Don't allow the seek distance to get too large from the
517 * odd fragment, pagein, etc
518 */
519 if (aic->seek_samples <= 60) /* second&third seek */
520 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
521 else
522 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
523
524 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
525 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
526 total = aic->seek_total + (aic->seek_samples/2);
527 do_div(total, aic->seek_samples);
528 aic->seek_mean = (sector_t)total;
529}
530
531/*
532 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
533 * updates @aic->ttime_mean based on that. It is called when a new
534 * request is queued.
535 */
536static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
537 struct request *rq)
538{
9e2585a8 539 int data_dir = rq_is_sync(rq);
f5b3db00
NP
540 unsigned long thinktime = 0;
541 sector_t seek_dist;
542
543 if (aic == NULL)
544 return;
545
546 if (data_dir == REQ_SYNC) {
547 unsigned long in_flight = atomic_read(&aic->nr_queued)
548 + atomic_read(&aic->nr_dispatched);
549 spin_lock(&aic->lock);
550 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
551 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
552 /* Calculate read -> read thinktime */
553 if (test_bit(AS_TASK_IORUNNING, &aic->state)
554 && in_flight == 0) {
555 thinktime = jiffies - aic->last_end_request;
556 thinktime = min(thinktime, MAX_THINKTIME-1);
557 }
558 as_update_thinktime(ad, aic, thinktime);
559
560 /* Calculate read -> read seek distance */
561 if (aic->last_request_pos < rq->sector)
562 seek_dist = rq->sector - aic->last_request_pos;
563 else
564 seek_dist = aic->last_request_pos - rq->sector;
565 as_update_seekdist(ad, aic, seek_dist);
566 }
567 aic->last_request_pos = rq->sector + rq->nr_sectors;
568 set_bit(AS_TASK_IOSTARTED, &aic->state);
569 spin_unlock(&aic->lock);
570 }
571}
572
1da177e4
LT
573/*
574 * as_close_req decides if one request is considered "close" to the
575 * previous one issued.
576 */
f5b3db00
NP
577static int as_close_req(struct as_data *ad, struct as_io_context *aic,
578 struct as_rq *arq)
1da177e4
LT
579{
580 unsigned long delay; /* milliseconds */
581 sector_t last = ad->last_sector[ad->batch_data_dir];
582 sector_t next = arq->request->sector;
583 sector_t delta; /* acceptable close offset (in sectors) */
f5b3db00 584 sector_t s;
1da177e4
LT
585
586 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
587 delay = 0;
588 else
589 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
590
f5b3db00
NP
591 if (delay == 0)
592 delta = 8192;
1da177e4 593 else if (delay <= 20 && delay <= ad->antic_expire)
f5b3db00 594 delta = 8192 << delay;
1da177e4
LT
595 else
596 return 1;
597
f5b3db00
NP
598 if ((last <= next + (delta>>1)) && (next <= last + delta))
599 return 1;
600
601 if (last < next)
602 s = next - last;
603 else
604 s = last - next;
605
606 if (aic->seek_samples == 0) {
607 /*
608 * Process has just started IO. Use past statistics to
609 * gauge success possibility
610 */
611 if (ad->new_seek_mean > s) {
612 /* this request is better than what we're expecting */
613 return 1;
614 }
615
616 } else {
617 if (aic->seek_mean > s) {
618 /* this request is better than what we're expecting */
619 return 1;
620 }
621 }
622
623 return 0;
1da177e4
LT
624}
625
626/*
627 * as_can_break_anticipation returns true if we have been anticipating this
628 * request.
629 *
630 * It also returns true if the process against which we are anticipating
631 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
632 * dispatch it ASAP, because we know that application will not be submitting
633 * any new reads.
634 *
f5b3db00 635 * If the task which has submitted the request has exited, break anticipation.
1da177e4
LT
636 *
637 * If this task has queued some other IO, do not enter enticipation.
638 */
639static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
640{
641 struct io_context *ioc;
642 struct as_io_context *aic;
1da177e4
LT
643
644 ioc = ad->io_context;
645 BUG_ON(!ioc);
646
647 if (arq && ioc == arq->io_context) {
648 /* request from same process */
649 return 1;
650 }
651
652 if (ad->ioc_finished && as_antic_expired(ad)) {
653 /*
654 * In this situation status should really be FINISHED,
655 * however the timer hasn't had the chance to run yet.
656 */
657 return 1;
658 }
659
660 aic = ioc->aic;
661 if (!aic)
662 return 0;
663
1da177e4
LT
664 if (atomic_read(&aic->nr_queued) > 0) {
665 /* process has more requests queued */
666 return 1;
667 }
668
669 if (atomic_read(&aic->nr_dispatched) > 0) {
670 /* process has more requests dispatched */
671 return 1;
672 }
673
9e2585a8 674 if (arq && rq_is_sync(arq->request) && as_close_req(ad, aic, arq)) {
1da177e4
LT
675 /*
676 * Found a close request that is not one of ours.
677 *
f5b3db00
NP
678 * This makes close requests from another process update
679 * our IO history. Is generally useful when there are
1da177e4
LT
680 * two or more cooperating processes working in the same
681 * area.
682 */
f5b3db00
NP
683 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
684 if (aic->ttime_samples == 0)
685 ad->exit_prob = (7*ad->exit_prob + 256)/8;
686
687 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
688 }
689
690 as_update_iohist(ad, aic, arq->request);
1da177e4
LT
691 return 1;
692 }
693
f5b3db00
NP
694 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
695 /* process anticipated on has exited */
696 if (aic->ttime_samples == 0)
697 ad->exit_prob = (7*ad->exit_prob + 256)/8;
698
699 if (ad->exit_no_coop > 128)
700 return 1;
701 }
1da177e4
LT
702
703 if (aic->ttime_samples == 0) {
704 if (ad->new_ttime_mean > ad->antic_expire)
705 return 1;
f5b3db00 706 if (ad->exit_prob * ad->exit_no_coop > 128*256)
1da177e4
LT
707 return 1;
708 } else if (aic->ttime_mean > ad->antic_expire) {
709 /* the process thinks too much between requests */
710 return 1;
711 }
712
1da177e4
LT
713 return 0;
714}
715
716/*
d6e05edc 717 * as_can_anticipate indicates whether we should either run arq
1da177e4
LT
718 * or keep anticipating a better request.
719 */
720static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
721{
722 if (!ad->io_context)
723 /*
724 * Last request submitted was a write
725 */
726 return 0;
727
728 if (ad->antic_status == ANTIC_FINISHED)
729 /*
730 * Don't restart if we have just finished. Run the next request
731 */
732 return 0;
733
734 if (as_can_break_anticipation(ad, arq))
735 /*
736 * This request is a good candidate. Don't keep anticipating,
737 * run it.
738 */
739 return 0;
740
741 /*
742 * OK from here, we haven't finished, and don't have a decent request!
743 * Status is either ANTIC_OFF so start waiting,
744 * ANTIC_WAIT_REQ so continue waiting for request to finish
745 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
1da177e4
LT
746 */
747
748 return 1;
749}
750
1da177e4
LT
751/*
752 * as_update_arq must be called whenever a request (arq) is added to
753 * the sort_list. This function keeps caches up to date, and checks if the
754 * request might be one we are "anticipating"
755 */
756static void as_update_arq(struct as_data *ad, struct as_rq *arq)
757{
9e2585a8 758 const int data_dir = rq_is_sync(arq->request);
1da177e4
LT
759
760 /* keep the next_arq cache up to date */
761 ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
762
763 /*
764 * have we been anticipating this request?
765 * or does it come from the same process as the one we are anticipating
766 * for?
767 */
768 if (ad->antic_status == ANTIC_WAIT_REQ
769 || ad->antic_status == ANTIC_WAIT_NEXT) {
770 if (as_can_break_anticipation(ad, arq))
771 as_antic_stop(ad);
772 }
773}
774
775/*
776 * Gathers timings and resizes the write batch automatically
777 */
778static void update_write_batch(struct as_data *ad)
779{
780 unsigned long batch = ad->batch_expire[REQ_ASYNC];
781 long write_time;
782
783 write_time = (jiffies - ad->current_batch_expires) + batch;
784 if (write_time < 0)
785 write_time = 0;
786
787 if (write_time > batch && !ad->write_batch_idled) {
788 if (write_time > batch * 3)
789 ad->write_batch_count /= 2;
790 else
791 ad->write_batch_count--;
792 } else if (write_time < batch && ad->current_write_count == 0) {
793 if (batch > write_time * 3)
794 ad->write_batch_count *= 2;
795 else
796 ad->write_batch_count++;
797 }
798
799 if (ad->write_batch_count < 1)
800 ad->write_batch_count = 1;
801}
802
803/*
804 * as_completed_request is to be called when a request has completed and
805 * returned something to the requesting process, be it an error or data.
806 */
807static void as_completed_request(request_queue_t *q, struct request *rq)
808{
809 struct as_data *ad = q->elevator->elevator_data;
810 struct as_rq *arq = RQ_DATA(rq);
811
812 WARN_ON(!list_empty(&rq->queuelist));
813
1da177e4
LT
814 if (arq->state != AS_RQ_REMOVED) {
815 printk("arq->state %d\n", arq->state);
816 WARN_ON(1);
817 goto out;
818 }
819
1da177e4
LT
820 if (ad->changed_batch && ad->nr_dispatched == 1) {
821 kblockd_schedule_work(&ad->antic_work);
822 ad->changed_batch = 0;
823
824 if (ad->batch_data_dir == REQ_SYNC)
825 ad->new_batch = 1;
826 }
827 WARN_ON(ad->nr_dispatched == 0);
828 ad->nr_dispatched--;
829
830 /*
831 * Start counting the batch from when a request of that direction is
832 * actually serviced. This should help devices with big TCQ windows
833 * and writeback caches
834 */
9e2585a8 835 if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) {
1da177e4
LT
836 update_write_batch(ad);
837 ad->current_batch_expires = jiffies +
838 ad->batch_expire[REQ_SYNC];
839 ad->new_batch = 0;
840 }
841
842 if (ad->io_context == arq->io_context && ad->io_context) {
843 ad->antic_start = jiffies;
844 ad->ioc_finished = 1;
845 if (ad->antic_status == ANTIC_WAIT_REQ) {
846 /*
847 * We were waiting on this request, now anticipate
848 * the next one
849 */
850 as_antic_waitnext(ad);
851 }
852 }
853
b4878f24 854 as_put_io_context(arq);
1da177e4
LT
855out:
856 arq->state = AS_RQ_POSTSCHED;
857}
858
859/*
860 * as_remove_queued_request removes a request from the pre dispatch queue
861 * without updating refcounts. It is expected the caller will drop the
862 * reference unless it replaces the request at somepart of the elevator
863 * (ie. the dispatch queue)
864 */
865static void as_remove_queued_request(request_queue_t *q, struct request *rq)
866{
867 struct as_rq *arq = RQ_DATA(rq);
9e2585a8 868 const int data_dir = rq_is_sync(rq);
1da177e4
LT
869 struct as_data *ad = q->elevator->elevator_data;
870
871 WARN_ON(arq->state != AS_RQ_QUEUED);
872
873 if (arq->io_context && arq->io_context->aic) {
874 BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
875 atomic_dec(&arq->io_context->aic->nr_queued);
876 }
877
878 /*
879 * Update the "next_arq" cache if we are about to remove its
880 * entry
881 */
882 if (ad->next_arq[data_dir] == arq)
883 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
884
d4f2f462 885 rq_fifo_clear(rq);
e37f346e 886 as_del_arq_rb(ad, rq);
1da177e4
LT
887}
888
1da177e4
LT
889/*
890 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
891 * 1 otherwise. It is ratelimited so that we only perform the check once per
892 * `fifo_expire' interval. Otherwise a large number of expired requests
893 * would create a hopeless seekstorm.
894 *
895 * See as_antic_expired comment.
896 */
897static int as_fifo_expired(struct as_data *ad, int adir)
898{
d4f2f462 899 struct request *rq;
1da177e4
LT
900 long delta_jif;
901
902 delta_jif = jiffies - ad->last_check_fifo[adir];
903 if (unlikely(delta_jif < 0))
904 delta_jif = -delta_jif;
905 if (delta_jif < ad->fifo_expire[adir])
906 return 0;
907
908 ad->last_check_fifo[adir] = jiffies;
909
910 if (list_empty(&ad->fifo_list[adir]))
911 return 0;
912
d4f2f462 913 rq = rq_entry_fifo(ad->fifo_list[adir].next);
1da177e4 914
d4f2f462 915 return time_after(jiffies, rq_fifo_time(rq));
1da177e4
LT
916}
917
918/*
919 * as_batch_expired returns true if the current batch has expired. A batch
920 * is a set of reads or a set of writes.
921 */
922static inline int as_batch_expired(struct as_data *ad)
923{
924 if (ad->changed_batch || ad->new_batch)
925 return 0;
926
927 if (ad->batch_data_dir == REQ_SYNC)
928 /* TODO! add a check so a complete fifo gets written? */
929 return time_after(jiffies, ad->current_batch_expires);
930
931 return time_after(jiffies, ad->current_batch_expires)
932 || ad->current_write_count == 0;
933}
934
935/*
936 * move an entry to dispatch queue
937 */
938static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
939{
940 struct request *rq = arq->request;
9e2585a8 941 const int data_dir = rq_is_sync(rq);
1da177e4 942
e37f346e 943 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
1da177e4
LT
944
945 as_antic_stop(ad);
946 ad->antic_status = ANTIC_OFF;
947
948 /*
949 * This has to be set in order to be correctly updated by
950 * as_find_next_arq
951 */
952 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
953
954 if (data_dir == REQ_SYNC) {
955 /* In case we have to anticipate after this */
956 copy_io_context(&ad->io_context, &arq->io_context);
957 } else {
958 if (ad->io_context) {
959 put_io_context(ad->io_context);
960 ad->io_context = NULL;
961 }
962
963 if (ad->current_write_count != 0)
964 ad->current_write_count--;
965 }
966 ad->ioc_finished = 0;
967
968 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
969
970 /*
971 * take it off the sort and fifo list, add to dispatch queue
972 */
1da177e4
LT
973 as_remove_queued_request(ad->q, rq);
974 WARN_ON(arq->state != AS_RQ_QUEUED);
975
b4878f24
JA
976 elv_dispatch_sort(ad->q, rq);
977
1da177e4
LT
978 arq->state = AS_RQ_DISPATCHED;
979 if (arq->io_context && arq->io_context->aic)
980 atomic_inc(&arq->io_context->aic->nr_dispatched);
981 ad->nr_dispatched++;
982}
983
984/*
985 * as_dispatch_request selects the best request according to
986 * read/write expire, batch expire, etc, and moves it to the dispatch
987 * queue. Returns 1 if a request was found, 0 otherwise.
988 */
b4878f24 989static int as_dispatch_request(request_queue_t *q, int force)
1da177e4 990{
b4878f24 991 struct as_data *ad = q->elevator->elevator_data;
1da177e4
LT
992 struct as_rq *arq;
993 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
994 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
995
b4878f24
JA
996 if (unlikely(force)) {
997 /*
998 * Forced dispatch, accounting is useless. Reset
999 * accounting states and dump fifo_lists. Note that
1000 * batch_data_dir is reset to REQ_SYNC to avoid
1001 * screwing write batch accounting as write batch
1002 * accounting occurs on W->R transition.
1003 */
1004 int dispatched = 0;
1005
1006 ad->batch_data_dir = REQ_SYNC;
1007 ad->changed_batch = 0;
1008 ad->new_batch = 0;
1009
1010 while (ad->next_arq[REQ_SYNC]) {
1011 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1012 dispatched++;
1013 }
1014 ad->last_check_fifo[REQ_SYNC] = jiffies;
1015
1016 while (ad->next_arq[REQ_ASYNC]) {
1017 as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
1018 dispatched++;
1019 }
1020 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1021
1022 return dispatched;
1023 }
1024
1da177e4
LT
1025 /* Signal that the write batch was uncontended, so we can't time it */
1026 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1027 if (ad->current_write_count == 0 || !writes)
1028 ad->write_batch_idled = 1;
1029 }
1030
1031 if (!(reads || writes)
1032 || ad->antic_status == ANTIC_WAIT_REQ
1033 || ad->antic_status == ANTIC_WAIT_NEXT
1034 || ad->changed_batch)
1035 return 0;
1036
f5b3db00 1037 if (!(reads && writes && as_batch_expired(ad))) {
1da177e4
LT
1038 /*
1039 * batch is still running or no reads or no writes
1040 */
1041 arq = ad->next_arq[ad->batch_data_dir];
1042
1043 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1044 if (as_fifo_expired(ad, REQ_SYNC))
1045 goto fifo_expired;
1046
1047 if (as_can_anticipate(ad, arq)) {
1048 as_antic_waitreq(ad);
1049 return 0;
1050 }
1051 }
1052
1053 if (arq) {
1054 /* we have a "next request" */
1055 if (reads && !writes)
1056 ad->current_batch_expires =
1057 jiffies + ad->batch_expire[REQ_SYNC];
1058 goto dispatch_request;
1059 }
1060 }
1061
1062 /*
1063 * at this point we are not running a batch. select the appropriate
1064 * data direction (read / write)
1065 */
1066
1067 if (reads) {
dd67d051 1068 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC]));
1da177e4
LT
1069
1070 if (writes && ad->batch_data_dir == REQ_SYNC)
1071 /*
1072 * Last batch was a read, switch to writes
1073 */
1074 goto dispatch_writes;
1075
1076 if (ad->batch_data_dir == REQ_ASYNC) {
1077 WARN_ON(ad->new_batch);
1078 ad->changed_batch = 1;
1079 }
1080 ad->batch_data_dir = REQ_SYNC;
d4f2f462 1081 arq = RQ_DATA(rq_entry_fifo(ad->fifo_list[REQ_SYNC].next));
1da177e4
LT
1082 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1083 goto dispatch_request;
1084 }
1085
1086 /*
1087 * the last batch was a read
1088 */
1089
1090 if (writes) {
1091dispatch_writes:
dd67d051 1092 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC]));
1da177e4
LT
1093
1094 if (ad->batch_data_dir == REQ_SYNC) {
1095 ad->changed_batch = 1;
1096
1097 /*
1098 * new_batch might be 1 when the queue runs out of
1099 * reads. A subsequent submission of a write might
1100 * cause a change of batch before the read is finished.
1101 */
1102 ad->new_batch = 0;
1103 }
1104 ad->batch_data_dir = REQ_ASYNC;
1105 ad->current_write_count = ad->write_batch_count;
1106 ad->write_batch_idled = 0;
1107 arq = ad->next_arq[ad->batch_data_dir];
1108 goto dispatch_request;
1109 }
1110
1111 BUG();
1112 return 0;
1113
1114dispatch_request:
1115 /*
1116 * If a request has expired, service it.
1117 */
1118
1119 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1120fifo_expired:
d4f2f462 1121 arq = RQ_DATA(rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next));
1da177e4
LT
1122 }
1123
1124 if (ad->changed_batch) {
1125 WARN_ON(ad->new_batch);
1126
1127 if (ad->nr_dispatched)
1128 return 0;
1129
1130 if (ad->batch_data_dir == REQ_ASYNC)
1131 ad->current_batch_expires = jiffies +
1132 ad->batch_expire[REQ_ASYNC];
1133 else
1134 ad->new_batch = 1;
1135
1136 ad->changed_batch = 0;
1137 }
1138
1139 /*
1140 * arq is the selected appropriate request.
1141 */
1142 as_move_to_dispatch(ad, arq);
1143
1144 return 1;
1145}
1146
1da177e4
LT
1147/*
1148 * add arq to rbtree and fifo
1149 */
b4878f24 1150static void as_add_request(request_queue_t *q, struct request *rq)
1da177e4 1151{
b4878f24
JA
1152 struct as_data *ad = q->elevator->elevator_data;
1153 struct as_rq *arq = RQ_DATA(rq);
1da177e4
LT
1154 int data_dir;
1155
b4878f24
JA
1156 arq->state = AS_RQ_NEW;
1157
9e2585a8 1158 data_dir = rq_is_sync(rq);
1da177e4
LT
1159
1160 arq->io_context = as_get_io_context();
1161
1162 if (arq->io_context) {
1163 as_update_iohist(ad, arq->io_context->aic, arq->request);
1164 atomic_inc(&arq->io_context->aic->nr_queued);
1165 }
1166
e37f346e 1167 as_add_arq_rb(ad, rq);
1da177e4 1168
ef9be1d3
TH
1169 /*
1170 * set expire time (only used for reads) and add to fifo list
1171 */
d4f2f462
JA
1172 rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]);
1173 list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]);
1da177e4 1174
ef9be1d3 1175 as_update_arq(ad, arq); /* keep state machine up to date */
1da177e4
LT
1176 arq->state = AS_RQ_QUEUED;
1177}
1178
b4878f24 1179static void as_activate_request(request_queue_t *q, struct request *rq)
1da177e4 1180{
1da177e4
LT
1181 struct as_rq *arq = RQ_DATA(rq);
1182
b4878f24
JA
1183 WARN_ON(arq->state != AS_RQ_DISPATCHED);
1184 arq->state = AS_RQ_REMOVED;
1185 if (arq->io_context && arq->io_context->aic)
1186 atomic_dec(&arq->io_context->aic->nr_dispatched);
1da177e4
LT
1187}
1188
b4878f24 1189static void as_deactivate_request(request_queue_t *q, struct request *rq)
1da177e4 1190{
1da177e4
LT
1191 struct as_rq *arq = RQ_DATA(rq);
1192
b4878f24
JA
1193 WARN_ON(arq->state != AS_RQ_REMOVED);
1194 arq->state = AS_RQ_DISPATCHED;
1195 if (arq->io_context && arq->io_context->aic)
1196 atomic_inc(&arq->io_context->aic->nr_dispatched);
1da177e4
LT
1197}
1198
1199/*
1200 * as_queue_empty tells us if there are requests left in the device. It may
1201 * not be the case that a driver can get the next request even if the queue
1202 * is not empty - it is used in the block layer to check for plugging and
1203 * merging opportunities
1204 */
1205static int as_queue_empty(request_queue_t *q)
1206{
1207 struct as_data *ad = q->elevator->elevator_data;
1208
b4878f24
JA
1209 return list_empty(&ad->fifo_list[REQ_ASYNC])
1210 && list_empty(&ad->fifo_list[REQ_SYNC]);
1da177e4
LT
1211}
1212
1da177e4
LT
1213static int
1214as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1215{
1216 struct as_data *ad = q->elevator->elevator_data;
1217 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1218 struct request *__rq;
1da177e4
LT
1219
1220 /*
1221 * check for front merge
1222 */
e37f346e 1223 __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key);
9817064b
JA
1224 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1225 *req = __rq;
1226 return ELEVATOR_FRONT_MERGE;
1da177e4
LT
1227 }
1228
1229 return ELEVATOR_NO_MERGE;
1da177e4
LT
1230}
1231
e37f346e 1232static void as_merged_request(request_queue_t *q, struct request *req, int type)
1da177e4
LT
1233{
1234 struct as_data *ad = q->elevator->elevator_data;
1da177e4 1235
1da177e4
LT
1236 /*
1237 * if the merge was a front merge, we need to reposition request
1238 */
e37f346e
JA
1239 if (type == ELEVATOR_FRONT_MERGE) {
1240 as_del_arq_rb(ad, req);
1241 as_add_arq_rb(ad, req);
1da177e4
LT
1242 /*
1243 * Note! At this stage of this and the next function, our next
1244 * request may not be optimal - eg the request may have "grown"
1245 * behind the disk head. We currently don't bother adjusting.
1246 */
1247 }
1da177e4
LT
1248}
1249
f5b3db00
NP
1250static void as_merged_requests(request_queue_t *q, struct request *req,
1251 struct request *next)
1da177e4 1252{
1da177e4
LT
1253 struct as_rq *arq = RQ_DATA(req);
1254 struct as_rq *anext = RQ_DATA(next);
1255
1256 BUG_ON(!arq);
1257 BUG_ON(!anext);
1258
1da177e4
LT
1259 /*
1260 * if anext expires before arq, assign its expire time to arq
1261 * and move into anext position (anext will be deleted) in fifo
1262 */
d4f2f462
JA
1263 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
1264 if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
1265 list_move(&req->queuelist, &next->queuelist);
1266 rq_set_fifo_time(req, rq_fifo_time(next));
1da177e4
LT
1267 /*
1268 * Don't copy here but swap, because when anext is
1269 * removed below, it must contain the unused context
1270 */
1271 swap_io_context(&arq->io_context, &anext->io_context);
1272 }
1273 }
1274
1da177e4
LT
1275 /*
1276 * kill knowledge of next, this one is a goner
1277 */
1278 as_remove_queued_request(q, next);
b4878f24 1279 as_put_io_context(anext);
1da177e4
LT
1280
1281 anext->state = AS_RQ_MERGED;
1282}
1283
1284/*
1285 * This is executed in a "deferred" process context, by kblockd. It calls the
1286 * driver's request_fn so the driver can submit that request.
1287 *
1288 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1289 * state before calling, and don't rely on any state over calls.
1290 *
1291 * FIXME! dispatch queue is not a queue at all!
1292 */
1293static void as_work_handler(void *data)
1294{
1295 struct request_queue *q = data;
1296 unsigned long flags;
1297
1298 spin_lock_irqsave(q->queue_lock, flags);
b4878f24 1299 if (!as_queue_empty(q))
1da177e4
LT
1300 q->request_fn(q);
1301 spin_unlock_irqrestore(q->queue_lock, flags);
1302}
1303
1304static void as_put_request(request_queue_t *q, struct request *rq)
1305{
1306 struct as_data *ad = q->elevator->elevator_data;
1307 struct as_rq *arq = RQ_DATA(rq);
1308
1309 if (!arq) {
1310 WARN_ON(1);
1311 return;
1312 }
1313
b4878f24
JA
1314 if (unlikely(arq->state != AS_RQ_POSTSCHED &&
1315 arq->state != AS_RQ_PRESCHED &&
1316 arq->state != AS_RQ_MERGED)) {
1da177e4
LT
1317 printk("arq->state %d\n", arq->state);
1318 WARN_ON(1);
1319 }
1320
1321 mempool_free(arq, ad->arq_pool);
1322 rq->elevator_private = NULL;
1323}
1324
22e2c507 1325static int as_set_request(request_queue_t *q, struct request *rq,
8267e268 1326 struct bio *bio, gfp_t gfp_mask)
1da177e4
LT
1327{
1328 struct as_data *ad = q->elevator->elevator_data;
1329 struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1330
1331 if (arq) {
1332 memset(arq, 0, sizeof(*arq));
1da177e4
LT
1333 arq->request = rq;
1334 arq->state = AS_RQ_PRESCHED;
1335 arq->io_context = NULL;
1da177e4
LT
1336 rq->elevator_private = arq;
1337 return 0;
1338 }
1339
1340 return 1;
1341}
1342
22e2c507 1343static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
1da177e4
LT
1344{
1345 int ret = ELV_MQUEUE_MAY;
1346 struct as_data *ad = q->elevator->elevator_data;
1347 struct io_context *ioc;
1348 if (ad->antic_status == ANTIC_WAIT_REQ ||
1349 ad->antic_status == ANTIC_WAIT_NEXT) {
1350 ioc = as_get_io_context();
1351 if (ad->io_context == ioc)
1352 ret = ELV_MQUEUE_MUST;
1353 put_io_context(ioc);
1354 }
1355
1356 return ret;
1357}
1358
1359static void as_exit_queue(elevator_t *e)
1360{
1361 struct as_data *ad = e->elevator_data;
1362
1363 del_timer_sync(&ad->antic_timer);
1364 kblockd_flush();
1365
1366 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1367 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1368
1369 mempool_destroy(ad->arq_pool);
1370 put_io_context(ad->io_context);
1da177e4
LT
1371 kfree(ad);
1372}
1373
1374/*
1375 * initialize elevator private data (as_data), and alloc a arq for
1376 * each request on the free lists
1377 */
bc1c1169 1378static void *as_init_queue(request_queue_t *q, elevator_t *e)
1da177e4
LT
1379{
1380 struct as_data *ad;
1da177e4
LT
1381
1382 if (!arq_pool)
bc1c1169 1383 return NULL;
1da177e4 1384
1946089a 1385 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
1da177e4 1386 if (!ad)
bc1c1169 1387 return NULL;
1da177e4
LT
1388 memset(ad, 0, sizeof(*ad));
1389
1390 ad->q = q; /* Identify what queue the data belongs to */
1391
1946089a
CL
1392 ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1393 mempool_free_slab, arq_pool, q->node);
1da177e4 1394 if (!ad->arq_pool) {
1da177e4 1395 kfree(ad);
bc1c1169 1396 return NULL;
1da177e4
LT
1397 }
1398
1399 /* anticipatory scheduling helpers */
1400 ad->antic_timer.function = as_antic_timeout;
1401 ad->antic_timer.data = (unsigned long)q;
1402 init_timer(&ad->antic_timer);
1403 INIT_WORK(&ad->antic_work, as_work_handler, q);
1404
1da177e4
LT
1405 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1406 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1407 ad->sort_list[REQ_SYNC] = RB_ROOT;
1408 ad->sort_list[REQ_ASYNC] = RB_ROOT;
1da177e4
LT
1409 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1410 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1411 ad->antic_expire = default_antic_expire;
1412 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1413 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1da177e4
LT
1414
1415 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1416 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1417 if (ad->write_batch_count < 2)
1418 ad->write_batch_count = 2;
1419
bc1c1169 1420 return ad;
1da177e4
LT
1421}
1422
1423/*
1424 * sysfs parts below
1425 */
1da177e4
LT
1426
1427static ssize_t
1428as_var_show(unsigned int var, char *page)
1429{
1da177e4
LT
1430 return sprintf(page, "%d\n", var);
1431}
1432
1433static ssize_t
1434as_var_store(unsigned long *var, const char *page, size_t count)
1435{
1da177e4
LT
1436 char *p = (char *) page;
1437
c9b3ad67 1438 *var = simple_strtoul(p, &p, 10);
1da177e4
LT
1439 return count;
1440}
1441
e572ec7e 1442static ssize_t est_time_show(elevator_t *e, char *page)
1da177e4 1443{
3d1ab40f 1444 struct as_data *ad = e->elevator_data;
1da177e4
LT
1445 int pos = 0;
1446
f5b3db00
NP
1447 pos += sprintf(page+pos, "%lu %% exit probability\n",
1448 100*ad->exit_prob/256);
1449 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1450 "cooperating process submitting IO\n",
1451 100*ad->exit_no_coop/256);
1da177e4 1452 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
f5b3db00
NP
1453 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1454 (unsigned long long)ad->new_seek_mean);
1da177e4
LT
1455
1456 return pos;
1457}
1458
1459#define SHOW_FUNCTION(__FUNC, __VAR) \
3d1ab40f 1460static ssize_t __FUNC(elevator_t *e, char *page) \
1da177e4 1461{ \
3d1ab40f 1462 struct as_data *ad = e->elevator_data; \
1da177e4
LT
1463 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1464}
e572ec7e
AV
1465SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
1466SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
1467SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
1468SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
1469SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
1da177e4
LT
1470#undef SHOW_FUNCTION
1471
1472#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
3d1ab40f 1473static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
1da177e4 1474{ \
3d1ab40f
AV
1475 struct as_data *ad = e->elevator_data; \
1476 int ret = as_var_store(__PTR, (page), count); \
1da177e4
LT
1477 if (*(__PTR) < (MIN)) \
1478 *(__PTR) = (MIN); \
1479 else if (*(__PTR) > (MAX)) \
1480 *(__PTR) = (MAX); \
1481 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1482 return ret; \
1483}
e572ec7e
AV
1484STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1485STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1486STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1487STORE_FUNCTION(as_read_batch_expire_store,
1da177e4 1488 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
e572ec7e 1489STORE_FUNCTION(as_write_batch_expire_store,
1da177e4
LT
1490 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1491#undef STORE_FUNCTION
1492
e572ec7e
AV
1493#define AS_ATTR(name) \
1494 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
1495
1496static struct elv_fs_entry as_attrs[] = {
1497 __ATTR_RO(est_time),
1498 AS_ATTR(read_expire),
1499 AS_ATTR(write_expire),
1500 AS_ATTR(antic_expire),
1501 AS_ATTR(read_batch_expire),
1502 AS_ATTR(write_batch_expire),
1503 __ATTR_NULL
1da177e4
LT
1504};
1505
1da177e4
LT
1506static struct elevator_type iosched_as = {
1507 .ops = {
1508 .elevator_merge_fn = as_merge,
1509 .elevator_merged_fn = as_merged_request,
1510 .elevator_merge_req_fn = as_merged_requests,
b4878f24
JA
1511 .elevator_dispatch_fn = as_dispatch_request,
1512 .elevator_add_req_fn = as_add_request,
1513 .elevator_activate_req_fn = as_activate_request,
1da177e4
LT
1514 .elevator_deactivate_req_fn = as_deactivate_request,
1515 .elevator_queue_empty_fn = as_queue_empty,
1516 .elevator_completed_req_fn = as_completed_request,
e37f346e
JA
1517 .elevator_former_req_fn = elv_rb_former_request,
1518 .elevator_latter_req_fn = elv_rb_latter_request,
1da177e4
LT
1519 .elevator_set_req_fn = as_set_request,
1520 .elevator_put_req_fn = as_put_request,
1521 .elevator_may_queue_fn = as_may_queue,
1522 .elevator_init_fn = as_init_queue,
1523 .elevator_exit_fn = as_exit_queue,
e17a9489 1524 .trim = as_trim,
1da177e4
LT
1525 },
1526
3d1ab40f 1527 .elevator_attrs = as_attrs,
1da177e4
LT
1528 .elevator_name = "anticipatory",
1529 .elevator_owner = THIS_MODULE,
1530};
1531
1532static int __init as_init(void)
1533{
1534 int ret;
1535
1536 arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
1537 0, 0, NULL, NULL);
1538 if (!arq_pool)
1539 return -ENOMEM;
1540
1541 ret = elv_register(&iosched_as);
1542 if (!ret) {
1543 /*
1544 * don't allow AS to get unregistered, since we would have
1545 * to browse all tasks in the system and release their
1546 * as_io_context first
1547 */
1548 __module_get(THIS_MODULE);
1549 return 0;
1550 }
1551
1552 kmem_cache_destroy(arq_pool);
1553 return ret;
1554}
1555
1556static void __exit as_exit(void)
1557{
334e94de 1558 DECLARE_COMPLETION(all_gone);
1da177e4 1559 elv_unregister(&iosched_as);
334e94de 1560 ioc_gone = &all_gone;
fba82272
OH
1561 /* ioc_gone's update must be visible before reading ioc_count */
1562 smp_wmb();
334e94de 1563 if (atomic_read(&ioc_count))
fba82272 1564 wait_for_completion(ioc_gone);
334e94de 1565 synchronize_rcu();
83521d3e 1566 kmem_cache_destroy(arq_pool);
1da177e4
LT
1567}
1568
1569module_init(as_init);
1570module_exit(as_exit);
1571
1572MODULE_AUTHOR("Nick Piggin");
1573MODULE_LICENSE("GPL");
1574MODULE_DESCRIPTION("anticipatory IO scheduler");