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