blob: 1ec5df466708a50b1fe2f3a6b13cda790a042dce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Anticipatory & deadline i/o scheduler.
3 *
4 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
Nick Pigginf5b3db02005-11-07 00:59:53 -08005 * Nick Piggin <nickpiggin@yahoo.com.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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>
13#include <linux/config.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/init.h>
17#include <linux/compiler.h>
18#include <linux/hash.h>
19#include <linux/rbtree.h>
20#include <linux/interrupt.h>
21
22#define REQ_SYNC 1
23#define REQ_ASYNC 0
24
25/*
26 * See Documentation/block/as-iosched.txt
27 */
28
29/*
30 * max time before a read is submitted.
31 */
32#define default_read_expire (HZ / 8)
33
34/*
35 * ditto for writes, these limits are not hard, even
36 * if the disk is capable of satisfying them.
37 */
38#define default_write_expire (HZ / 4)
39
40/*
41 * read_batch_expire describes how long we will allow a stream of reads to
42 * persist before looking to see whether it is time to switch over to writes.
43 */
44#define default_read_batch_expire (HZ / 2)
45
46/*
47 * write_batch_expire describes how long we want a stream of writes to run for.
48 * This is not a hard limit, but a target we set for the auto-tuning thingy.
49 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
50 * a short amount of time...
51 */
52#define default_write_batch_expire (HZ / 8)
53
54/*
55 * max time we may wait to anticipate a read (default around 6ms)
56 */
57#define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
58
59/*
60 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
61 * however huge values tend to interfere and not decay fast enough. A program
62 * might be in a non-io phase of operation. Waiting on user input for example,
63 * or doing a lengthy computation. A small penalty can be justified there, and
64 * will still catch out those processes that constantly have large thinktimes.
65 */
66#define MAX_THINKTIME (HZ/50UL)
67
68/* Bits in as_io_context.state */
69enum as_io_states {
Nick Pigginf5b3db02005-11-07 00:59:53 -080070 AS_TASK_RUNNING=0, /* Process has not exited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 AS_TASK_IOSTARTED, /* Process has started some IO */
72 AS_TASK_IORUNNING, /* Process has completed some IO */
73};
74
75enum anticipation_status {
76 ANTIC_OFF=0, /* Not anticipating (normal operation) */
77 ANTIC_WAIT_REQ, /* The last read has not yet completed */
78 ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
79 last read (which has completed) */
80 ANTIC_FINISHED, /* Anticipating but have found a candidate
81 * or timed out */
82};
83
84struct as_data {
85 /*
86 * run time data
87 */
88
89 struct request_queue *q; /* the "owner" queue */
90
91 /*
92 * requests (as_rq s) are present on both sort_list and fifo_list
93 */
94 struct rb_root sort_list[2];
95 struct list_head fifo_list[2];
96
97 struct as_rq *next_arq[2]; /* next in sort order */
98 sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
Akinobu Mitabae386f2006-04-24 21:12:59 +020099 struct hlist_head *hash; /* request hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 unsigned long exit_prob; /* probability a task will exit while
102 being waited on */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800103 unsigned long exit_no_coop; /* probablility an exited task will
104 not be part of a later cooperating
105 request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long new_ttime_total; /* mean thinktime on new proc */
107 unsigned long new_ttime_mean;
108 u64 new_seek_total; /* mean seek on new proc */
109 sector_t new_seek_mean;
110
111 unsigned long current_batch_expires;
112 unsigned long last_check_fifo[2];
113 int changed_batch; /* 1: waiting for old batch to end */
114 int new_batch; /* 1: waiting on first read complete */
115 int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
116 int write_batch_count; /* max # of reqs in a write batch */
117 int current_write_count; /* how many requests left this batch */
118 int write_batch_idled; /* has the write batch gone idle? */
119 mempool_t *arq_pool;
120
121 enum anticipation_status antic_status;
122 unsigned long antic_start; /* jiffies: when it started */
123 struct timer_list antic_timer; /* anticipatory scheduling timer */
124 struct work_struct antic_work; /* Deferred unplugging */
125 struct io_context *io_context; /* Identify the expected process */
126 int ioc_finished; /* IO associated with io_context is finished */
127 int nr_dispatched;
128
129 /*
130 * settings that change how the i/o scheduler behaves
131 */
132 unsigned long fifo_expire[2];
133 unsigned long batch_expire[2];
134 unsigned long antic_expire;
135};
136
137#define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
138
139/*
140 * per-request data.
141 */
142enum arq_state {
143 AS_RQ_NEW=0, /* New - not referenced and not on any lists */
144 AS_RQ_QUEUED, /* In the request queue. It belongs to the
145 scheduler */
146 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
147 driver now */
148 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
149 AS_RQ_REMOVED,
150 AS_RQ_MERGED,
151 AS_RQ_POSTSCHED, /* when they shouldn't be */
152};
153
154struct as_rq {
155 /*
156 * rbtree index, key is the starting offset
157 */
158 struct rb_node rb_node;
159 sector_t rb_key;
160
161 struct request *request;
162
163 struct io_context *io_context; /* The submitting task */
164
165 /*
166 * request hash, key is the ending offset (for back merge lookup)
167 */
Akinobu Mitabae386f2006-04-24 21:12:59 +0200168 struct hlist_node hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
171 * expire fifo
172 */
173 struct list_head fifo;
174 unsigned long expires;
175
176 unsigned int is_sync;
177 enum arq_state state;
178};
179
180#define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
181
182static kmem_cache_t *arq_pool;
183
Al Viro334e94d2006-03-18 15:05:53 -0500184static atomic_t ioc_count = ATOMIC_INIT(0);
185static struct completion *ioc_gone;
186
Tejun Heoef9be1d2005-11-11 14:27:09 +0100187static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq);
188static void as_antic_stop(struct as_data *ad);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * IO Context helper functions
192 */
193
194/* Called to deallocate the as_io_context */
195static void free_as_io_context(struct as_io_context *aic)
196{
197 kfree(aic);
Al Viro334e94d2006-03-18 15:05:53 -0500198 if (atomic_dec_and_test(&ioc_count) && ioc_gone)
199 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Al Viroe17a9482006-03-18 13:21:20 -0500202static void as_trim(struct io_context *ioc)
203{
Al Viro334e94d2006-03-18 15:05:53 -0500204 if (ioc->aic)
205 free_as_io_context(ioc->aic);
Al Viroe17a9482006-03-18 13:21:20 -0500206 ioc->aic = NULL;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* Called when the task exits */
210static void exit_as_io_context(struct as_io_context *aic)
211{
212 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
213 clear_bit(AS_TASK_RUNNING, &aic->state);
214}
215
216static struct as_io_context *alloc_as_io_context(void)
217{
218 struct as_io_context *ret;
219
220 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
221 if (ret) {
222 ret->dtor = free_as_io_context;
223 ret->exit = exit_as_io_context;
224 ret->state = 1 << AS_TASK_RUNNING;
225 atomic_set(&ret->nr_queued, 0);
226 atomic_set(&ret->nr_dispatched, 0);
227 spin_lock_init(&ret->lock);
228 ret->ttime_total = 0;
229 ret->ttime_samples = 0;
230 ret->ttime_mean = 0;
231 ret->seek_total = 0;
232 ret->seek_samples = 0;
233 ret->seek_mean = 0;
Al Viro334e94d2006-03-18 15:05:53 -0500234 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 return ret;
238}
239
240/*
241 * If the current task has no AS IO context then create one and initialise it.
242 * Then take a ref on the task's io context and return it.
243 */
244static struct io_context *as_get_io_context(void)
245{
246 struct io_context *ioc = get_io_context(GFP_ATOMIC);
247 if (ioc && !ioc->aic) {
248 ioc->aic = alloc_as_io_context();
249 if (!ioc->aic) {
250 put_io_context(ioc);
251 ioc = NULL;
252 }
253 }
254 return ioc;
255}
256
Jens Axboeb4878f22005-10-20 16:42:29 +0200257static void as_put_io_context(struct as_rq *arq)
258{
259 struct as_io_context *aic;
260
261 if (unlikely(!arq->io_context))
262 return;
263
264 aic = arq->io_context->aic;
265
266 if (arq->is_sync == REQ_SYNC && aic) {
267 spin_lock(&aic->lock);
268 set_bit(AS_TASK_IORUNNING, &aic->state);
269 aic->last_end_request = jiffies;
270 spin_unlock(&aic->lock);
271 }
272
273 put_io_context(arq->io_context);
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * the back merge hash support functions
278 */
279static const int as_hash_shift = 6;
280#define AS_HASH_BLOCK(sec) ((sec) >> 3)
281#define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
282#define AS_HASH_ENTRIES (1 << as_hash_shift)
283#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285static inline void __as_del_arq_hash(struct as_rq *arq)
286{
Akinobu Mitabae386f2006-04-24 21:12:59 +0200287 hlist_del_init(&arq->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static inline void as_del_arq_hash(struct as_rq *arq)
291{
Akinobu Mitabae386f2006-04-24 21:12:59 +0200292 if (!hlist_unhashed(&arq->hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 __as_del_arq_hash(arq);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
297{
298 struct request *rq = arq->request;
299
Akinobu Mitabae386f2006-04-24 21:12:59 +0200300 BUG_ON(!hlist_unhashed(&arq->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Akinobu Mitabae386f2006-04-24 21:12:59 +0200302 hlist_add_head(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * move hot entry to front of chain
307 */
308static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
309{
310 struct request *rq = arq->request;
Akinobu Mitabae386f2006-04-24 21:12:59 +0200311 struct hlist_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Akinobu Mitabae386f2006-04-24 21:12:59 +0200313 if (hlist_unhashed(&arq->hash)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 WARN_ON(1);
315 return;
316 }
317
Akinobu Mitabae386f2006-04-24 21:12:59 +0200318 if (&arq->hash != head->first) {
319 hlist_del(&arq->hash);
320 hlist_add_head(&arq->hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322}
323
324static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
325{
Akinobu Mitabae386f2006-04-24 21:12:59 +0200326 struct hlist_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
327 struct hlist_node *entry, *next;
328 struct as_rq *arq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Akinobu Mitabae386f2006-04-24 21:12:59 +0200330 hlist_for_each_entry_safe(arq, entry, next, hash_list, hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct request *__rq = arq->request;
332
Akinobu Mitabae386f2006-04-24 21:12:59 +0200333 BUG_ON(hlist_unhashed(&arq->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (!rq_mergeable(__rq)) {
Tejun Heo98b11472005-10-20 16:46:54 +0200336 as_del_arq_hash(arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 continue;
338 }
339
340 if (rq_hash_key(__rq) == offset)
341 return __rq;
342 }
343
344 return NULL;
345}
346
347/*
348 * rb tree support functions
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
351#define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
352#define rq_rb_key(rq) (rq)->sector
353
354/*
355 * as_find_first_arq finds the first (lowest sector numbered) request
356 * for the specified data_dir. Used to sweep back to the start of the disk
357 * (1-way elevator) after we process the last (highest sector) request.
358 */
359static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
360{
361 struct rb_node *n = ad->sort_list[data_dir].rb_node;
362
363 if (n == NULL)
364 return NULL;
365
366 for (;;) {
367 if (n->rb_left == NULL)
368 return rb_entry_arq(n);
369
370 n = n->rb_left;
371 }
372}
373
374/*
375 * Add the request to the rb tree if it is unique. If there is an alias (an
376 * existing request against the same sector), which can happen when using
377 * direct IO, then return the alias.
378 */
Tejun Heoef9be1d2005-11-11 14:27:09 +0100379static struct as_rq *__as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
382 struct rb_node *parent = NULL;
383 struct as_rq *__arq;
384 struct request *rq = arq->request;
385
386 arq->rb_key = rq_rb_key(rq);
387
388 while (*p) {
389 parent = *p;
390 __arq = rb_entry_arq(parent);
391
392 if (arq->rb_key < __arq->rb_key)
393 p = &(*p)->rb_left;
394 else if (arq->rb_key > __arq->rb_key)
395 p = &(*p)->rb_right;
396 else
397 return __arq;
398 }
399
400 rb_link_node(&arq->rb_node, parent, p);
401 rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
402
403 return NULL;
404}
405
Tejun Heoef9be1d2005-11-11 14:27:09 +0100406static void as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
407{
408 struct as_rq *alias;
409
410 while ((unlikely(alias = __as_add_arq_rb(ad, arq)))) {
411 as_move_to_dispatch(ad, alias);
412 as_antic_stop(ad);
413 }
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
417{
Jens Axboedd67d052006-06-21 09:36:18 +0200418 if (!RB_EMPTY_NODE(&arq->rb_node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 WARN_ON(1);
420 return;
421 }
422
423 rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
Jens Axboedd67d052006-06-21 09:36:18 +0200424 RB_CLEAR_NODE(&arq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static struct request *
428as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
429{
430 struct rb_node *n = ad->sort_list[data_dir].rb_node;
431 struct as_rq *arq;
432
433 while (n) {
434 arq = rb_entry_arq(n);
435
436 if (sector < arq->rb_key)
437 n = n->rb_left;
438 else if (sector > arq->rb_key)
439 n = n->rb_right;
440 else
441 return arq->request;
442 }
443
444 return NULL;
445}
446
447/*
448 * IO Scheduler proper
449 */
450
451#define MAXBACK (1024 * 1024) /*
452 * Maximum distance the disk will go backward
453 * for a request.
454 */
455
456#define BACK_PENALTY 2
457
458/*
459 * as_choose_req selects the preferred one of two requests of the same data_dir
460 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
461 */
462static struct as_rq *
463as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
464{
465 int data_dir;
466 sector_t last, s1, s2, d1, d2;
467 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
468 const sector_t maxback = MAXBACK;
469
470 if (arq1 == NULL || arq1 == arq2)
471 return arq2;
472 if (arq2 == NULL)
473 return arq1;
474
475 data_dir = arq1->is_sync;
476
477 last = ad->last_sector[data_dir];
478 s1 = arq1->request->sector;
479 s2 = arq2->request->sector;
480
481 BUG_ON(data_dir != arq2->is_sync);
482
483 /*
484 * Strict one way elevator _except_ in the case where we allow
485 * short backward seeks which are biased as twice the cost of a
486 * similar forward seek.
487 */
488 if (s1 >= last)
489 d1 = s1 - last;
490 else if (s1+maxback >= last)
491 d1 = (last - s1)*BACK_PENALTY;
492 else {
493 r1_wrap = 1;
494 d1 = 0; /* shut up, gcc */
495 }
496
497 if (s2 >= last)
498 d2 = s2 - last;
499 else if (s2+maxback >= last)
500 d2 = (last - s2)*BACK_PENALTY;
501 else {
502 r2_wrap = 1;
503 d2 = 0;
504 }
505
506 /* Found required data */
507 if (!r1_wrap && r2_wrap)
508 return arq1;
509 else if (!r2_wrap && r1_wrap)
510 return arq2;
511 else if (r1_wrap && r2_wrap) {
512 /* both behind the head */
513 if (s1 <= s2)
514 return arq1;
515 else
516 return arq2;
517 }
518
519 /* Both requests in front of the head */
520 if (d1 < d2)
521 return arq1;
522 else if (d2 < d1)
523 return arq2;
524 else {
525 if (s1 >= s2)
526 return arq1;
527 else
528 return arq2;
529 }
530}
531
532/*
533 * as_find_next_arq finds the next request after @prev in elevator order.
534 * this with as_choose_req form the basis for how the scheduler chooses
535 * what request to process next. Anticipation works on top of this.
536 */
537static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
538{
539 const int data_dir = last->is_sync;
540 struct as_rq *ret;
541 struct rb_node *rbnext = rb_next(&last->rb_node);
542 struct rb_node *rbprev = rb_prev(&last->rb_node);
543 struct as_rq *arq_next, *arq_prev;
544
Jens Axboedd67d052006-06-21 09:36:18 +0200545 BUG_ON(!RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (rbprev)
548 arq_prev = rb_entry_arq(rbprev);
549 else
550 arq_prev = NULL;
551
552 if (rbnext)
553 arq_next = rb_entry_arq(rbnext);
554 else {
555 arq_next = as_find_first_arq(ad, data_dir);
556 if (arq_next == last)
557 arq_next = NULL;
558 }
559
560 ret = as_choose_req(ad, arq_next, arq_prev);
561
562 return ret;
563}
564
565/*
566 * anticipatory scheduling functions follow
567 */
568
569/*
570 * as_antic_expired tells us when we have anticipated too long.
571 * The funny "absolute difference" math on the elapsed time is to handle
572 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
573 */
574static int as_antic_expired(struct as_data *ad)
575{
576 long delta_jif;
577
578 delta_jif = jiffies - ad->antic_start;
579 if (unlikely(delta_jif < 0))
580 delta_jif = -delta_jif;
581 if (delta_jif < ad->antic_expire)
582 return 0;
583
584 return 1;
585}
586
587/*
588 * as_antic_waitnext starts anticipating that a nice request will soon be
589 * submitted. See also as_antic_waitreq
590 */
591static void as_antic_waitnext(struct as_data *ad)
592{
593 unsigned long timeout;
594
595 BUG_ON(ad->antic_status != ANTIC_OFF
596 && ad->antic_status != ANTIC_WAIT_REQ);
597
598 timeout = ad->antic_start + ad->antic_expire;
599
600 mod_timer(&ad->antic_timer, timeout);
601
602 ad->antic_status = ANTIC_WAIT_NEXT;
603}
604
605/*
606 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
607 * until the request that we're anticipating on has finished. This means we
608 * are timing from when the candidate process wakes up hopefully.
609 */
610static void as_antic_waitreq(struct as_data *ad)
611{
612 BUG_ON(ad->antic_status == ANTIC_FINISHED);
613 if (ad->antic_status == ANTIC_OFF) {
614 if (!ad->io_context || ad->ioc_finished)
615 as_antic_waitnext(ad);
616 else
617 ad->antic_status = ANTIC_WAIT_REQ;
618 }
619}
620
621/*
622 * This is called directly by the functions in this file to stop anticipation.
623 * We kill the timer and schedule a call to the request_fn asap.
624 */
625static void as_antic_stop(struct as_data *ad)
626{
627 int status = ad->antic_status;
628
629 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
630 if (status == ANTIC_WAIT_NEXT)
631 del_timer(&ad->antic_timer);
632 ad->antic_status = ANTIC_FINISHED;
633 /* see as_work_handler */
634 kblockd_schedule_work(&ad->antic_work);
635 }
636}
637
638/*
639 * as_antic_timeout is the timer function set by as_antic_waitnext.
640 */
641static void as_antic_timeout(unsigned long data)
642{
643 struct request_queue *q = (struct request_queue *)data;
644 struct as_data *ad = q->elevator->elevator_data;
645 unsigned long flags;
646
647 spin_lock_irqsave(q->queue_lock, flags);
648 if (ad->antic_status == ANTIC_WAIT_REQ
649 || ad->antic_status == ANTIC_WAIT_NEXT) {
650 struct as_io_context *aic = ad->io_context->aic;
651
652 ad->antic_status = ANTIC_FINISHED;
653 kblockd_schedule_work(&ad->antic_work);
654
655 if (aic->ttime_samples == 0) {
Nick Pigginf5b3db02005-11-07 00:59:53 -0800656 /* process anticipated on has exited or timed out*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ad->exit_prob = (7*ad->exit_prob + 256)/8;
658 }
Nick Pigginf5b3db02005-11-07 00:59:53 -0800659 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
660 /* process not "saved" by a cooperating request */
661 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 spin_unlock_irqrestore(q->queue_lock, flags);
665}
666
Nick Pigginf5b3db02005-11-07 00:59:53 -0800667static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
668 unsigned long ttime)
669{
670 /* fixed point: 1.0 == 1<<8 */
671 if (aic->ttime_samples == 0) {
672 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
673 ad->new_ttime_mean = ad->new_ttime_total / 256;
674
675 ad->exit_prob = (7*ad->exit_prob)/8;
676 }
677 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
678 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
679 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
680}
681
682static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
683 sector_t sdist)
684{
685 u64 total;
686
687 if (aic->seek_samples == 0) {
688 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
689 ad->new_seek_mean = ad->new_seek_total / 256;
690 }
691
692 /*
693 * Don't allow the seek distance to get too large from the
694 * odd fragment, pagein, etc
695 */
696 if (aic->seek_samples <= 60) /* second&third seek */
697 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
698 else
699 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
700
701 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
702 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
703 total = aic->seek_total + (aic->seek_samples/2);
704 do_div(total, aic->seek_samples);
705 aic->seek_mean = (sector_t)total;
706}
707
708/*
709 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
710 * updates @aic->ttime_mean based on that. It is called when a new
711 * request is queued.
712 */
713static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
714 struct request *rq)
715{
716 struct as_rq *arq = RQ_DATA(rq);
717 int data_dir = arq->is_sync;
718 unsigned long thinktime = 0;
719 sector_t seek_dist;
720
721 if (aic == NULL)
722 return;
723
724 if (data_dir == REQ_SYNC) {
725 unsigned long in_flight = atomic_read(&aic->nr_queued)
726 + atomic_read(&aic->nr_dispatched);
727 spin_lock(&aic->lock);
728 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
729 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
730 /* Calculate read -> read thinktime */
731 if (test_bit(AS_TASK_IORUNNING, &aic->state)
732 && in_flight == 0) {
733 thinktime = jiffies - aic->last_end_request;
734 thinktime = min(thinktime, MAX_THINKTIME-1);
735 }
736 as_update_thinktime(ad, aic, thinktime);
737
738 /* Calculate read -> read seek distance */
739 if (aic->last_request_pos < rq->sector)
740 seek_dist = rq->sector - aic->last_request_pos;
741 else
742 seek_dist = aic->last_request_pos - rq->sector;
743 as_update_seekdist(ad, aic, seek_dist);
744 }
745 aic->last_request_pos = rq->sector + rq->nr_sectors;
746 set_bit(AS_TASK_IOSTARTED, &aic->state);
747 spin_unlock(&aic->lock);
748 }
749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/*
752 * as_close_req decides if one request is considered "close" to the
753 * previous one issued.
754 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800755static int as_close_req(struct as_data *ad, struct as_io_context *aic,
756 struct as_rq *arq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 unsigned long delay; /* milliseconds */
759 sector_t last = ad->last_sector[ad->batch_data_dir];
760 sector_t next = arq->request->sector;
761 sector_t delta; /* acceptable close offset (in sectors) */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800762 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
765 delay = 0;
766 else
767 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
768
Nick Pigginf5b3db02005-11-07 00:59:53 -0800769 if (delay == 0)
770 delta = 8192;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 else if (delay <= 20 && delay <= ad->antic_expire)
Nick Pigginf5b3db02005-11-07 00:59:53 -0800772 delta = 8192 << delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 else
774 return 1;
775
Nick Pigginf5b3db02005-11-07 00:59:53 -0800776 if ((last <= next + (delta>>1)) && (next <= last + delta))
777 return 1;
778
779 if (last < next)
780 s = next - last;
781 else
782 s = last - next;
783
784 if (aic->seek_samples == 0) {
785 /*
786 * Process has just started IO. Use past statistics to
787 * gauge success possibility
788 */
789 if (ad->new_seek_mean > s) {
790 /* this request is better than what we're expecting */
791 return 1;
792 }
793
794 } else {
795 if (aic->seek_mean > s) {
796 /* this request is better than what we're expecting */
797 return 1;
798 }
799 }
800
801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
805 * as_can_break_anticipation returns true if we have been anticipating this
806 * request.
807 *
808 * It also returns true if the process against which we are anticipating
809 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
810 * dispatch it ASAP, because we know that application will not be submitting
811 * any new reads.
812 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800813 * If the task which has submitted the request has exited, break anticipation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
815 * If this task has queued some other IO, do not enter enticipation.
816 */
817static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
818{
819 struct io_context *ioc;
820 struct as_io_context *aic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 ioc = ad->io_context;
823 BUG_ON(!ioc);
824
825 if (arq && ioc == arq->io_context) {
826 /* request from same process */
827 return 1;
828 }
829
830 if (ad->ioc_finished && as_antic_expired(ad)) {
831 /*
832 * In this situation status should really be FINISHED,
833 * however the timer hasn't had the chance to run yet.
834 */
835 return 1;
836 }
837
838 aic = ioc->aic;
839 if (!aic)
840 return 0;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (atomic_read(&aic->nr_queued) > 0) {
843 /* process has more requests queued */
844 return 1;
845 }
846
847 if (atomic_read(&aic->nr_dispatched) > 0) {
848 /* process has more requests dispatched */
849 return 1;
850 }
851
Nick Pigginf5b3db02005-11-07 00:59:53 -0800852 if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, aic, arq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * Found a close request that is not one of ours.
855 *
Nick Pigginf5b3db02005-11-07 00:59:53 -0800856 * This makes close requests from another process update
857 * our IO history. Is generally useful when there are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * two or more cooperating processes working in the same
859 * area.
860 */
Nick Pigginf5b3db02005-11-07 00:59:53 -0800861 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
862 if (aic->ttime_samples == 0)
863 ad->exit_prob = (7*ad->exit_prob + 256)/8;
864
865 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
866 }
867
868 as_update_iohist(ad, aic, arq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 1;
870 }
871
Nick Pigginf5b3db02005-11-07 00:59:53 -0800872 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
873 /* process anticipated on has exited */
874 if (aic->ttime_samples == 0)
875 ad->exit_prob = (7*ad->exit_prob + 256)/8;
876
877 if (ad->exit_no_coop > 128)
878 return 1;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 if (aic->ttime_samples == 0) {
882 if (ad->new_ttime_mean > ad->antic_expire)
883 return 1;
Nick Pigginf5b3db02005-11-07 00:59:53 -0800884 if (ad->exit_prob * ad->exit_no_coop > 128*256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 1;
886 } else if (aic->ttime_mean > ad->antic_expire) {
887 /* the process thinks too much between requests */
888 return 1;
889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
892}
893
894/*
895 * as_can_anticipate indicates weather we should either run arq
896 * or keep anticipating a better request.
897 */
898static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
899{
900 if (!ad->io_context)
901 /*
902 * Last request submitted was a write
903 */
904 return 0;
905
906 if (ad->antic_status == ANTIC_FINISHED)
907 /*
908 * Don't restart if we have just finished. Run the next request
909 */
910 return 0;
911
912 if (as_can_break_anticipation(ad, arq))
913 /*
914 * This request is a good candidate. Don't keep anticipating,
915 * run it.
916 */
917 return 0;
918
919 /*
920 * OK from here, we haven't finished, and don't have a decent request!
921 * Status is either ANTIC_OFF so start waiting,
922 * ANTIC_WAIT_REQ so continue waiting for request to finish
923 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
925
926 return 1;
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
930 * as_update_arq must be called whenever a request (arq) is added to
931 * the sort_list. This function keeps caches up to date, and checks if the
932 * request might be one we are "anticipating"
933 */
934static void as_update_arq(struct as_data *ad, struct as_rq *arq)
935{
936 const int data_dir = arq->is_sync;
937
938 /* keep the next_arq cache up to date */
939 ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
940
941 /*
942 * have we been anticipating this request?
943 * or does it come from the same process as the one we are anticipating
944 * for?
945 */
946 if (ad->antic_status == ANTIC_WAIT_REQ
947 || ad->antic_status == ANTIC_WAIT_NEXT) {
948 if (as_can_break_anticipation(ad, arq))
949 as_antic_stop(ad);
950 }
951}
952
953/*
954 * Gathers timings and resizes the write batch automatically
955 */
956static void update_write_batch(struct as_data *ad)
957{
958 unsigned long batch = ad->batch_expire[REQ_ASYNC];
959 long write_time;
960
961 write_time = (jiffies - ad->current_batch_expires) + batch;
962 if (write_time < 0)
963 write_time = 0;
964
965 if (write_time > batch && !ad->write_batch_idled) {
966 if (write_time > batch * 3)
967 ad->write_batch_count /= 2;
968 else
969 ad->write_batch_count--;
970 } else if (write_time < batch && ad->current_write_count == 0) {
971 if (batch > write_time * 3)
972 ad->write_batch_count *= 2;
973 else
974 ad->write_batch_count++;
975 }
976
977 if (ad->write_batch_count < 1)
978 ad->write_batch_count = 1;
979}
980
981/*
982 * as_completed_request is to be called when a request has completed and
983 * returned something to the requesting process, be it an error or data.
984 */
985static void as_completed_request(request_queue_t *q, struct request *rq)
986{
987 struct as_data *ad = q->elevator->elevator_data;
988 struct as_rq *arq = RQ_DATA(rq);
989
990 WARN_ON(!list_empty(&rq->queuelist));
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (arq->state != AS_RQ_REMOVED) {
993 printk("arq->state %d\n", arq->state);
994 WARN_ON(1);
995 goto out;
996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (ad->changed_batch && ad->nr_dispatched == 1) {
999 kblockd_schedule_work(&ad->antic_work);
1000 ad->changed_batch = 0;
1001
1002 if (ad->batch_data_dir == REQ_SYNC)
1003 ad->new_batch = 1;
1004 }
1005 WARN_ON(ad->nr_dispatched == 0);
1006 ad->nr_dispatched--;
1007
1008 /*
1009 * Start counting the batch from when a request of that direction is
1010 * actually serviced. This should help devices with big TCQ windows
1011 * and writeback caches
1012 */
1013 if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
1014 update_write_batch(ad);
1015 ad->current_batch_expires = jiffies +
1016 ad->batch_expire[REQ_SYNC];
1017 ad->new_batch = 0;
1018 }
1019
1020 if (ad->io_context == arq->io_context && ad->io_context) {
1021 ad->antic_start = jiffies;
1022 ad->ioc_finished = 1;
1023 if (ad->antic_status == ANTIC_WAIT_REQ) {
1024 /*
1025 * We were waiting on this request, now anticipate
1026 * the next one
1027 */
1028 as_antic_waitnext(ad);
1029 }
1030 }
1031
Jens Axboeb4878f22005-10-20 16:42:29 +02001032 as_put_io_context(arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033out:
1034 arq->state = AS_RQ_POSTSCHED;
1035}
1036
1037/*
1038 * as_remove_queued_request removes a request from the pre dispatch queue
1039 * without updating refcounts. It is expected the caller will drop the
1040 * reference unless it replaces the request at somepart of the elevator
1041 * (ie. the dispatch queue)
1042 */
1043static void as_remove_queued_request(request_queue_t *q, struct request *rq)
1044{
1045 struct as_rq *arq = RQ_DATA(rq);
1046 const int data_dir = arq->is_sync;
1047 struct as_data *ad = q->elevator->elevator_data;
1048
1049 WARN_ON(arq->state != AS_RQ_QUEUED);
1050
1051 if (arq->io_context && arq->io_context->aic) {
1052 BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
1053 atomic_dec(&arq->io_context->aic->nr_queued);
1054 }
1055
1056 /*
1057 * Update the "next_arq" cache if we are about to remove its
1058 * entry
1059 */
1060 if (ad->next_arq[data_dir] == arq)
1061 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1062
1063 list_del_init(&arq->fifo);
Tejun Heo98b11472005-10-20 16:46:54 +02001064 as_del_arq_hash(arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 as_del_arq_rb(ad, arq);
1066}
1067
1068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
1070 * 1 otherwise. It is ratelimited so that we only perform the check once per
1071 * `fifo_expire' interval. Otherwise a large number of expired requests
1072 * would create a hopeless seekstorm.
1073 *
1074 * See as_antic_expired comment.
1075 */
1076static int as_fifo_expired(struct as_data *ad, int adir)
1077{
1078 struct as_rq *arq;
1079 long delta_jif;
1080
1081 delta_jif = jiffies - ad->last_check_fifo[adir];
1082 if (unlikely(delta_jif < 0))
1083 delta_jif = -delta_jif;
1084 if (delta_jif < ad->fifo_expire[adir])
1085 return 0;
1086
1087 ad->last_check_fifo[adir] = jiffies;
1088
1089 if (list_empty(&ad->fifo_list[adir]))
1090 return 0;
1091
1092 arq = list_entry_fifo(ad->fifo_list[adir].next);
1093
1094 return time_after(jiffies, arq->expires);
1095}
1096
1097/*
1098 * as_batch_expired returns true if the current batch has expired. A batch
1099 * is a set of reads or a set of writes.
1100 */
1101static inline int as_batch_expired(struct as_data *ad)
1102{
1103 if (ad->changed_batch || ad->new_batch)
1104 return 0;
1105
1106 if (ad->batch_data_dir == REQ_SYNC)
1107 /* TODO! add a check so a complete fifo gets written? */
1108 return time_after(jiffies, ad->current_batch_expires);
1109
1110 return time_after(jiffies, ad->current_batch_expires)
1111 || ad->current_write_count == 0;
1112}
1113
1114/*
1115 * move an entry to dispatch queue
1116 */
1117static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
1118{
1119 struct request *rq = arq->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 const int data_dir = arq->is_sync;
1121
Jens Axboedd67d052006-06-21 09:36:18 +02001122 BUG_ON(!RB_EMPTY_NODE(&arq->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 as_antic_stop(ad);
1125 ad->antic_status = ANTIC_OFF;
1126
1127 /*
1128 * This has to be set in order to be correctly updated by
1129 * as_find_next_arq
1130 */
1131 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
1132
1133 if (data_dir == REQ_SYNC) {
1134 /* In case we have to anticipate after this */
1135 copy_io_context(&ad->io_context, &arq->io_context);
1136 } else {
1137 if (ad->io_context) {
1138 put_io_context(ad->io_context);
1139 ad->io_context = NULL;
1140 }
1141
1142 if (ad->current_write_count != 0)
1143 ad->current_write_count--;
1144 }
1145 ad->ioc_finished = 0;
1146
1147 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1148
1149 /*
1150 * take it off the sort and fifo list, add to dispatch queue
1151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 as_remove_queued_request(ad->q, rq);
1153 WARN_ON(arq->state != AS_RQ_QUEUED);
1154
Jens Axboeb4878f22005-10-20 16:42:29 +02001155 elv_dispatch_sort(ad->q, rq);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 arq->state = AS_RQ_DISPATCHED;
1158 if (arq->io_context && arq->io_context->aic)
1159 atomic_inc(&arq->io_context->aic->nr_dispatched);
1160 ad->nr_dispatched++;
1161}
1162
1163/*
1164 * as_dispatch_request selects the best request according to
1165 * read/write expire, batch expire, etc, and moves it to the dispatch
1166 * queue. Returns 1 if a request was found, 0 otherwise.
1167 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001168static int as_dispatch_request(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jens Axboeb4878f22005-10-20 16:42:29 +02001170 struct as_data *ad = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct as_rq *arq;
1172 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1173 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
1174
Jens Axboeb4878f22005-10-20 16:42:29 +02001175 if (unlikely(force)) {
1176 /*
1177 * Forced dispatch, accounting is useless. Reset
1178 * accounting states and dump fifo_lists. Note that
1179 * batch_data_dir is reset to REQ_SYNC to avoid
1180 * screwing write batch accounting as write batch
1181 * accounting occurs on W->R transition.
1182 */
1183 int dispatched = 0;
1184
1185 ad->batch_data_dir = REQ_SYNC;
1186 ad->changed_batch = 0;
1187 ad->new_batch = 0;
1188
1189 while (ad->next_arq[REQ_SYNC]) {
1190 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1191 dispatched++;
1192 }
1193 ad->last_check_fifo[REQ_SYNC] = jiffies;
1194
1195 while (ad->next_arq[REQ_ASYNC]) {
1196 as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
1197 dispatched++;
1198 }
1199 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1200
1201 return dispatched;
1202 }
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Signal that the write batch was uncontended, so we can't time it */
1205 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1206 if (ad->current_write_count == 0 || !writes)
1207 ad->write_batch_idled = 1;
1208 }
1209
1210 if (!(reads || writes)
1211 || ad->antic_status == ANTIC_WAIT_REQ
1212 || ad->antic_status == ANTIC_WAIT_NEXT
1213 || ad->changed_batch)
1214 return 0;
1215
Nick Pigginf5b3db02005-11-07 00:59:53 -08001216 if (!(reads && writes && as_batch_expired(ad))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /*
1218 * batch is still running or no reads or no writes
1219 */
1220 arq = ad->next_arq[ad->batch_data_dir];
1221
1222 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1223 if (as_fifo_expired(ad, REQ_SYNC))
1224 goto fifo_expired;
1225
1226 if (as_can_anticipate(ad, arq)) {
1227 as_antic_waitreq(ad);
1228 return 0;
1229 }
1230 }
1231
1232 if (arq) {
1233 /* we have a "next request" */
1234 if (reads && !writes)
1235 ad->current_batch_expires =
1236 jiffies + ad->batch_expire[REQ_SYNC];
1237 goto dispatch_request;
1238 }
1239 }
1240
1241 /*
1242 * at this point we are not running a batch. select the appropriate
1243 * data direction (read / write)
1244 */
1245
1246 if (reads) {
Jens Axboedd67d052006-06-21 09:36:18 +02001247 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (writes && ad->batch_data_dir == REQ_SYNC)
1250 /*
1251 * Last batch was a read, switch to writes
1252 */
1253 goto dispatch_writes;
1254
1255 if (ad->batch_data_dir == REQ_ASYNC) {
1256 WARN_ON(ad->new_batch);
1257 ad->changed_batch = 1;
1258 }
1259 ad->batch_data_dir = REQ_SYNC;
1260 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1261 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1262 goto dispatch_request;
1263 }
1264
1265 /*
1266 * the last batch was a read
1267 */
1268
1269 if (writes) {
1270dispatch_writes:
Jens Axboedd67d052006-06-21 09:36:18 +02001271 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (ad->batch_data_dir == REQ_SYNC) {
1274 ad->changed_batch = 1;
1275
1276 /*
1277 * new_batch might be 1 when the queue runs out of
1278 * reads. A subsequent submission of a write might
1279 * cause a change of batch before the read is finished.
1280 */
1281 ad->new_batch = 0;
1282 }
1283 ad->batch_data_dir = REQ_ASYNC;
1284 ad->current_write_count = ad->write_batch_count;
1285 ad->write_batch_idled = 0;
1286 arq = ad->next_arq[ad->batch_data_dir];
1287 goto dispatch_request;
1288 }
1289
1290 BUG();
1291 return 0;
1292
1293dispatch_request:
1294 /*
1295 * If a request has expired, service it.
1296 */
1297
1298 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1299fifo_expired:
1300 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1301 BUG_ON(arq == NULL);
1302 }
1303
1304 if (ad->changed_batch) {
1305 WARN_ON(ad->new_batch);
1306
1307 if (ad->nr_dispatched)
1308 return 0;
1309
1310 if (ad->batch_data_dir == REQ_ASYNC)
1311 ad->current_batch_expires = jiffies +
1312 ad->batch_expire[REQ_ASYNC];
1313 else
1314 ad->new_batch = 1;
1315
1316 ad->changed_batch = 0;
1317 }
1318
1319 /*
1320 * arq is the selected appropriate request.
1321 */
1322 as_move_to_dispatch(ad, arq);
1323
1324 return 1;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * add arq to rbtree and fifo
1329 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001330static void as_add_request(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Jens Axboeb4878f22005-10-20 16:42:29 +02001332 struct as_data *ad = q->elevator->elevator_data;
1333 struct as_rq *arq = RQ_DATA(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 int data_dir;
1335
Jens Axboeb4878f22005-10-20 16:42:29 +02001336 arq->state = AS_RQ_NEW;
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (rq_data_dir(arq->request) == READ
Jens Axboeb31dc662006-06-13 08:26:10 +02001339 || (arq->request->flags & REQ_RW_SYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 arq->is_sync = 1;
1341 else
1342 arq->is_sync = 0;
1343 data_dir = arq->is_sync;
1344
1345 arq->io_context = as_get_io_context();
1346
1347 if (arq->io_context) {
1348 as_update_iohist(ad, arq->io_context->aic, arq->request);
1349 atomic_inc(&arq->io_context->aic->nr_queued);
1350 }
1351
Tejun Heoef9be1d2005-11-11 14:27:09 +01001352 as_add_arq_rb(ad, arq);
1353 if (rq_mergeable(arq->request))
1354 as_add_arq_hash(ad, arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Tejun Heoef9be1d2005-11-11 14:27:09 +01001356 /*
1357 * set expire time (only used for reads) and add to fifo list
1358 */
1359 arq->expires = jiffies + ad->fifo_expire[data_dir];
1360 list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Tejun Heoef9be1d2005-11-11 14:27:09 +01001362 as_update_arq(ad, arq); /* keep state machine up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 arq->state = AS_RQ_QUEUED;
1364}
1365
Jens Axboeb4878f22005-10-20 16:42:29 +02001366static void as_activate_request(request_queue_t *q, struct request *rq)
1367{
1368 struct as_rq *arq = RQ_DATA(rq);
1369
1370 WARN_ON(arq->state != AS_RQ_DISPATCHED);
1371 arq->state = AS_RQ_REMOVED;
1372 if (arq->io_context && arq->io_context->aic)
1373 atomic_dec(&arq->io_context->aic->nr_dispatched);
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static void as_deactivate_request(request_queue_t *q, struct request *rq)
1377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct as_rq *arq = RQ_DATA(rq);
1379
Jens Axboeb4878f22005-10-20 16:42:29 +02001380 WARN_ON(arq->state != AS_RQ_REMOVED);
1381 arq->state = AS_RQ_DISPATCHED;
1382 if (arq->io_context && arq->io_context->aic)
1383 atomic_inc(&arq->io_context->aic->nr_dispatched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/*
1387 * as_queue_empty tells us if there are requests left in the device. It may
1388 * not be the case that a driver can get the next request even if the queue
1389 * is not empty - it is used in the block layer to check for plugging and
1390 * merging opportunities
1391 */
1392static int as_queue_empty(request_queue_t *q)
1393{
1394 struct as_data *ad = q->elevator->elevator_data;
1395
Jens Axboeb4878f22005-10-20 16:42:29 +02001396 return list_empty(&ad->fifo_list[REQ_ASYNC])
1397 && list_empty(&ad->fifo_list[REQ_SYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Nick Pigginf5b3db02005-11-07 00:59:53 -08001400static struct request *as_former_request(request_queue_t *q,
1401 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct as_rq *arq = RQ_DATA(rq);
1404 struct rb_node *rbprev = rb_prev(&arq->rb_node);
1405 struct request *ret = NULL;
1406
1407 if (rbprev)
1408 ret = rb_entry_arq(rbprev)->request;
1409
1410 return ret;
1411}
1412
Nick Pigginf5b3db02005-11-07 00:59:53 -08001413static struct request *as_latter_request(request_queue_t *q,
1414 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct as_rq *arq = RQ_DATA(rq);
1417 struct rb_node *rbnext = rb_next(&arq->rb_node);
1418 struct request *ret = NULL;
1419
1420 if (rbnext)
1421 ret = rb_entry_arq(rbnext)->request;
1422
1423 return ret;
1424}
1425
1426static int
1427as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1428{
1429 struct as_data *ad = q->elevator->elevator_data;
1430 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1431 struct request *__rq;
1432 int ret;
1433
1434 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * see if the merge hash can satisfy a back merge
1436 */
1437 __rq = as_find_arq_hash(ad, bio->bi_sector);
1438 if (__rq) {
1439 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
1440
1441 if (elv_rq_merge_ok(__rq, bio)) {
1442 ret = ELEVATOR_BACK_MERGE;
1443 goto out;
1444 }
1445 }
1446
1447 /*
1448 * check for front merge
1449 */
1450 __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
1451 if (__rq) {
1452 BUG_ON(rb_key != rq_rb_key(__rq));
1453
1454 if (elv_rq_merge_ok(__rq, bio)) {
1455 ret = ELEVATOR_FRONT_MERGE;
1456 goto out;
1457 }
1458 }
1459
1460 return ELEVATOR_NO_MERGE;
1461out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (ret) {
1463 if (rq_mergeable(__rq))
1464 as_hot_arq_hash(ad, RQ_DATA(__rq));
1465 }
1466 *req = __rq;
1467 return ret;
1468}
1469
1470static void as_merged_request(request_queue_t *q, struct request *req)
1471{
1472 struct as_data *ad = q->elevator->elevator_data;
1473 struct as_rq *arq = RQ_DATA(req);
1474
1475 /*
1476 * hash always needs to be repositioned, key is end sector
1477 */
1478 as_del_arq_hash(arq);
1479 as_add_arq_hash(ad, arq);
1480
1481 /*
1482 * if the merge was a front merge, we need to reposition request
1483 */
1484 if (rq_rb_key(req) != arq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 as_del_arq_rb(ad, arq);
Tejun Heoef9be1d2005-11-11 14:27:09 +01001486 as_add_arq_rb(ad, arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * Note! At this stage of this and the next function, our next
1489 * request may not be optimal - eg the request may have "grown"
1490 * behind the disk head. We currently don't bother adjusting.
1491 */
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Nick Pigginf5b3db02005-11-07 00:59:53 -08001495static void as_merged_requests(request_queue_t *q, struct request *req,
1496 struct request *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct as_data *ad = q->elevator->elevator_data;
1499 struct as_rq *arq = RQ_DATA(req);
1500 struct as_rq *anext = RQ_DATA(next);
1501
1502 BUG_ON(!arq);
1503 BUG_ON(!anext);
1504
1505 /*
1506 * reposition arq (this is the merged request) in hash, and in rbtree
1507 * in case of a front merge
1508 */
1509 as_del_arq_hash(arq);
1510 as_add_arq_hash(ad, arq);
1511
1512 if (rq_rb_key(req) != arq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 as_del_arq_rb(ad, arq);
Tejun Heoef9be1d2005-11-11 14:27:09 +01001514 as_add_arq_rb(ad, arq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
1517 /*
1518 * if anext expires before arq, assign its expire time to arq
1519 * and move into anext position (anext will be deleted) in fifo
1520 */
1521 if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
1522 if (time_before(anext->expires, arq->expires)) {
1523 list_move(&arq->fifo, &anext->fifo);
1524 arq->expires = anext->expires;
1525 /*
1526 * Don't copy here but swap, because when anext is
1527 * removed below, it must contain the unused context
1528 */
1529 swap_io_context(&arq->io_context, &anext->io_context);
1530 }
1531 }
1532
1533 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * kill knowledge of next, this one is a goner
1535 */
1536 as_remove_queued_request(q, next);
Jens Axboeb4878f22005-10-20 16:42:29 +02001537 as_put_io_context(anext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 anext->state = AS_RQ_MERGED;
1540}
1541
1542/*
1543 * This is executed in a "deferred" process context, by kblockd. It calls the
1544 * driver's request_fn so the driver can submit that request.
1545 *
1546 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1547 * state before calling, and don't rely on any state over calls.
1548 *
1549 * FIXME! dispatch queue is not a queue at all!
1550 */
1551static void as_work_handler(void *data)
1552{
1553 struct request_queue *q = data;
1554 unsigned long flags;
1555
1556 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboeb4878f22005-10-20 16:42:29 +02001557 if (!as_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 q->request_fn(q);
1559 spin_unlock_irqrestore(q->queue_lock, flags);
1560}
1561
1562static void as_put_request(request_queue_t *q, struct request *rq)
1563{
1564 struct as_data *ad = q->elevator->elevator_data;
1565 struct as_rq *arq = RQ_DATA(rq);
1566
1567 if (!arq) {
1568 WARN_ON(1);
1569 return;
1570 }
1571
Jens Axboeb4878f22005-10-20 16:42:29 +02001572 if (unlikely(arq->state != AS_RQ_POSTSCHED &&
1573 arq->state != AS_RQ_PRESCHED &&
1574 arq->state != AS_RQ_MERGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 printk("arq->state %d\n", arq->state);
1576 WARN_ON(1);
1577 }
1578
1579 mempool_free(arq, ad->arq_pool);
1580 rq->elevator_private = NULL;
1581}
1582
Jens Axboe22e2c502005-06-27 10:55:12 +02001583static int as_set_request(request_queue_t *q, struct request *rq,
Al Viro8267e262005-10-21 03:20:53 -04001584 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct as_data *ad = q->elevator->elevator_data;
1587 struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1588
1589 if (arq) {
1590 memset(arq, 0, sizeof(*arq));
Jens Axboedd67d052006-06-21 09:36:18 +02001591 RB_CLEAR_NODE(&arq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 arq->request = rq;
1593 arq->state = AS_RQ_PRESCHED;
1594 arq->io_context = NULL;
Akinobu Mitabae386f2006-04-24 21:12:59 +02001595 INIT_HLIST_NODE(&arq->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 INIT_LIST_HEAD(&arq->fifo);
1597 rq->elevator_private = arq;
1598 return 0;
1599 }
1600
1601 return 1;
1602}
1603
Jens Axboe22e2c502005-06-27 10:55:12 +02001604static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 int ret = ELV_MQUEUE_MAY;
1607 struct as_data *ad = q->elevator->elevator_data;
1608 struct io_context *ioc;
1609 if (ad->antic_status == ANTIC_WAIT_REQ ||
1610 ad->antic_status == ANTIC_WAIT_NEXT) {
1611 ioc = as_get_io_context();
1612 if (ad->io_context == ioc)
1613 ret = ELV_MQUEUE_MUST;
1614 put_io_context(ioc);
1615 }
1616
1617 return ret;
1618}
1619
1620static void as_exit_queue(elevator_t *e)
1621{
1622 struct as_data *ad = e->elevator_data;
1623
1624 del_timer_sync(&ad->antic_timer);
1625 kblockd_flush();
1626
1627 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1628 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1629
1630 mempool_destroy(ad->arq_pool);
1631 put_io_context(ad->io_context);
1632 kfree(ad->hash);
1633 kfree(ad);
1634}
1635
1636/*
1637 * initialize elevator private data (as_data), and alloc a arq for
1638 * each request on the free lists
1639 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001640static void *as_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct as_data *ad;
1643 int i;
1644
1645 if (!arq_pool)
Jens Axboebc1c1162006-06-08 08:49:06 +02001646 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Christoph Lameter19460892005-06-23 00:08:19 -07001648 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (!ad)
Jens Axboebc1c1162006-06-08 08:49:06 +02001650 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 memset(ad, 0, sizeof(*ad));
1652
1653 ad->q = q; /* Identify what queue the data belongs to */
1654
Akinobu Mitabae386f2006-04-24 21:12:59 +02001655 ad->hash = kmalloc_node(sizeof(struct hlist_head)*AS_HASH_ENTRIES,
Christoph Lameter19460892005-06-23 00:08:19 -07001656 GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!ad->hash) {
1658 kfree(ad);
Jens Axboebc1c1162006-06-08 08:49:06 +02001659 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661
Christoph Lameter19460892005-06-23 00:08:19 -07001662 ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1663 mempool_free_slab, arq_pool, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (!ad->arq_pool) {
1665 kfree(ad->hash);
1666 kfree(ad);
Jens Axboebc1c1162006-06-08 08:49:06 +02001667 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
1670 /* anticipatory scheduling helpers */
1671 ad->antic_timer.function = as_antic_timeout;
1672 ad->antic_timer.data = (unsigned long)q;
1673 init_timer(&ad->antic_timer);
1674 INIT_WORK(&ad->antic_work, as_work_handler, q);
1675
1676 for (i = 0; i < AS_HASH_ENTRIES; i++)
Akinobu Mitabae386f2006-04-24 21:12:59 +02001677 INIT_HLIST_HEAD(&ad->hash[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1680 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1681 ad->sort_list[REQ_SYNC] = RB_ROOT;
1682 ad->sort_list[REQ_ASYNC] = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1684 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1685 ad->antic_expire = default_antic_expire;
1686 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1687 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1690 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1691 if (ad->write_batch_count < 2)
1692 ad->write_batch_count = 2;
1693
Jens Axboebc1c1162006-06-08 08:49:06 +02001694 return ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697/*
1698 * sysfs parts below
1699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701static ssize_t
1702as_var_show(unsigned int var, char *page)
1703{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return sprintf(page, "%d\n", var);
1705}
1706
1707static ssize_t
1708as_var_store(unsigned long *var, const char *page, size_t count)
1709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 char *p = (char *) page;
1711
Jens Axboec9b3ad62005-07-27 11:43:37 -07001712 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return count;
1714}
1715
Al Viroe572ec72006-03-18 22:27:18 -05001716static ssize_t est_time_show(elevator_t *e, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Al Viro3d1ab402006-03-18 18:35:43 -05001718 struct as_data *ad = e->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int pos = 0;
1720
Nick Pigginf5b3db02005-11-07 00:59:53 -08001721 pos += sprintf(page+pos, "%lu %% exit probability\n",
1722 100*ad->exit_prob/256);
1723 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1724 "cooperating process submitting IO\n",
1725 100*ad->exit_no_coop/256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
Nick Pigginf5b3db02005-11-07 00:59:53 -08001727 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1728 (unsigned long long)ad->new_seek_mean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 return pos;
1731}
1732
1733#define SHOW_FUNCTION(__FUNC, __VAR) \
Al Viro3d1ab402006-03-18 18:35:43 -05001734static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001736 struct as_data *ad = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1738}
Al Viroe572ec72006-03-18 22:27:18 -05001739SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
1740SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
1741SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
1742SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
1743SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744#undef SHOW_FUNCTION
1745
1746#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
Al Viro3d1ab402006-03-18 18:35:43 -05001747static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{ \
Al Viro3d1ab402006-03-18 18:35:43 -05001749 struct as_data *ad = e->elevator_data; \
1750 int ret = as_var_store(__PTR, (page), count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (*(__PTR) < (MIN)) \
1752 *(__PTR) = (MIN); \
1753 else if (*(__PTR) > (MAX)) \
1754 *(__PTR) = (MAX); \
1755 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1756 return ret; \
1757}
Al Viroe572ec72006-03-18 22:27:18 -05001758STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1759STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1760STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1761STORE_FUNCTION(as_read_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
Al Viroe572ec72006-03-18 22:27:18 -05001763STORE_FUNCTION(as_write_batch_expire_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1765#undef STORE_FUNCTION
1766
Al Viroe572ec72006-03-18 22:27:18 -05001767#define AS_ATTR(name) \
1768 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Al Viroe572ec72006-03-18 22:27:18 -05001770static struct elv_fs_entry as_attrs[] = {
1771 __ATTR_RO(est_time),
1772 AS_ATTR(read_expire),
1773 AS_ATTR(write_expire),
1774 AS_ATTR(antic_expire),
1775 AS_ATTR(read_batch_expire),
1776 AS_ATTR(write_batch_expire),
1777 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778};
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780static struct elevator_type iosched_as = {
1781 .ops = {
1782 .elevator_merge_fn = as_merge,
1783 .elevator_merged_fn = as_merged_request,
1784 .elevator_merge_req_fn = as_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02001785 .elevator_dispatch_fn = as_dispatch_request,
1786 .elevator_add_req_fn = as_add_request,
1787 .elevator_activate_req_fn = as_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .elevator_deactivate_req_fn = as_deactivate_request,
1789 .elevator_queue_empty_fn = as_queue_empty,
1790 .elevator_completed_req_fn = as_completed_request,
1791 .elevator_former_req_fn = as_former_request,
1792 .elevator_latter_req_fn = as_latter_request,
1793 .elevator_set_req_fn = as_set_request,
1794 .elevator_put_req_fn = as_put_request,
1795 .elevator_may_queue_fn = as_may_queue,
1796 .elevator_init_fn = as_init_queue,
1797 .elevator_exit_fn = as_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05001798 .trim = as_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 },
1800
Al Viro3d1ab402006-03-18 18:35:43 -05001801 .elevator_attrs = as_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 .elevator_name = "anticipatory",
1803 .elevator_owner = THIS_MODULE,
1804};
1805
1806static int __init as_init(void)
1807{
1808 int ret;
1809
1810 arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
1811 0, 0, NULL, NULL);
1812 if (!arq_pool)
1813 return -ENOMEM;
1814
1815 ret = elv_register(&iosched_as);
1816 if (!ret) {
1817 /*
1818 * don't allow AS to get unregistered, since we would have
1819 * to browse all tasks in the system and release their
1820 * as_io_context first
1821 */
1822 __module_get(THIS_MODULE);
1823 return 0;
1824 }
1825
1826 kmem_cache_destroy(arq_pool);
1827 return ret;
1828}
1829
1830static void __exit as_exit(void)
1831{
Al Viro334e94d2006-03-18 15:05:53 -05001832 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 elv_unregister(&iosched_as);
Al Viro334e94d2006-03-18 15:05:53 -05001834 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001835 /* ioc_gone's update must be visible before reading ioc_count */
1836 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05001837 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02001838 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05001839 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08001840 kmem_cache_destroy(arq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843module_init(as_init);
1844module_exit(as_exit);
1845
1846MODULE_AUTHOR("Nick Piggin");
1847MODULE_LICENSE("GPL");
1848MODULE_DESCRIPTION("anticipatory IO scheduler");