2 * Anticipatory & deadline i/o scheduler.
4 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
5 * Nick Piggin <nickpiggin@yahoo.com.au>
8 #include <linux/kernel.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>
26 * See Documentation/block/as-iosched.txt
30 * max time before a read is submitted.
32 #define default_read_expire (HZ / 8)
35 * ditto for writes, these limits are not hard, even
36 * if the disk is capable of satisfying them.
38 #define default_write_expire (HZ / 4)
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.
44 #define default_read_batch_expire (HZ / 2)
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...
52 #define default_write_batch_expire (HZ / 8)
55 * max time we may wait to anticipate a read (default around 6ms)
57 #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
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.
66 #define MAX_THINKTIME (HZ/50UL)
68 /* Bits in as_io_context.state */
70 AS_TASK_RUNNING=0, /* Process has not exited */
71 AS_TASK_IOSTARTED, /* Process has started some IO */
72 AS_TASK_IORUNNING, /* Process has completed some IO */
75 enum 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
89 struct request_queue *q; /* the "owner" queue */
92 * requests (as_rq s) are present on both sort_list and fifo_list
94 struct rb_root sort_list[2];
95 struct list_head fifo_list[2];
97 struct as_rq *next_arq[2]; /* next in sort order */
98 sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
99 struct list_head *hash; /* request hash */
101 unsigned long exit_prob; /* probability a task will exit while
103 unsigned long exit_no_coop; /* probablility an exited task will
104 not be part of a later cooperating
106 unsigned long new_ttime_total; /* mean thinktime on new proc */
107 unsigned long new_ttime_mean;
108 u64 new_seek_total; /* mean seek on new proc */
109 sector_t new_seek_mean;
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? */
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 */
130 * settings that change how the i/o scheduler behaves
132 unsigned long fifo_expire[2];
133 unsigned long batch_expire[2];
134 unsigned long antic_expire;
137 #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
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
146 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
148 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
151 AS_RQ_POSTSCHED, /* when they shouldn't be */
156 * rbtree index, key is the starting offset
158 struct rb_node rb_node;
161 struct request *request;
163 struct io_context *io_context; /* The submitting task */
166 * request hash, key is the ending offset (for back merge lookup)
168 struct list_head hash;
169 unsigned int on_hash;
174 struct list_head fifo;
175 unsigned long expires;
177 unsigned int is_sync;
178 enum arq_state state;
181 #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
183 static kmem_cache_t *arq_pool;
185 static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq);
186 static void as_antic_stop(struct as_data *ad);
189 * IO Context helper functions
192 /* Called to deallocate the as_io_context */
193 static void free_as_io_context(struct as_io_context *aic)
198 static void as_trim(struct io_context *ioc)
204 /* Called when the task exits */
205 static void exit_as_io_context(struct as_io_context *aic)
207 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
208 clear_bit(AS_TASK_RUNNING, &aic->state);
211 static struct as_io_context *alloc_as_io_context(void)
213 struct as_io_context *ret;
215 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
217 ret->dtor = free_as_io_context;
218 ret->exit = exit_as_io_context;
219 ret->state = 1 << AS_TASK_RUNNING;
220 atomic_set(&ret->nr_queued, 0);
221 atomic_set(&ret->nr_dispatched, 0);
222 spin_lock_init(&ret->lock);
223 ret->ttime_total = 0;
224 ret->ttime_samples = 0;
227 ret->seek_samples = 0;
235 * If the current task has no AS IO context then create one and initialise it.
236 * Then take a ref on the task's io context and return it.
238 static struct io_context *as_get_io_context(void)
240 struct io_context *ioc = get_io_context(GFP_ATOMIC);
241 if (ioc && !ioc->aic) {
242 ioc->aic = alloc_as_io_context();
251 static void as_put_io_context(struct as_rq *arq)
253 struct as_io_context *aic;
255 if (unlikely(!arq->io_context))
258 aic = arq->io_context->aic;
260 if (arq->is_sync == REQ_SYNC && aic) {
261 spin_lock(&aic->lock);
262 set_bit(AS_TASK_IORUNNING, &aic->state);
263 aic->last_end_request = jiffies;
264 spin_unlock(&aic->lock);
267 put_io_context(arq->io_context);
271 * the back merge hash support functions
273 static const int as_hash_shift = 6;
274 #define AS_HASH_BLOCK(sec) ((sec) >> 3)
275 #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
276 #define AS_HASH_ENTRIES (1 << as_hash_shift)
277 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
278 #define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash)
280 static inline void __as_del_arq_hash(struct as_rq *arq)
283 list_del_init(&arq->hash);
286 static inline void as_del_arq_hash(struct as_rq *arq)
289 __as_del_arq_hash(arq);
292 static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
294 struct request *rq = arq->request;
296 BUG_ON(arq->on_hash);
299 list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
303 * move hot entry to front of chain
305 static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
307 struct request *rq = arq->request;
308 struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
315 if (arq->hash.prev != head) {
316 list_del(&arq->hash);
317 list_add(&arq->hash, head);
321 static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
323 struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
324 struct list_head *entry, *next = hash_list->next;
326 while ((entry = next) != hash_list) {
327 struct as_rq *arq = list_entry_hash(entry);
328 struct request *__rq = arq->request;
332 BUG_ON(!arq->on_hash);
334 if (!rq_mergeable(__rq)) {
335 as_del_arq_hash(arq);
339 if (rq_hash_key(__rq) == offset)
347 * rb tree support functions
350 #define RB_EMPTY(root) ((root)->rb_node == NULL)
351 #define ON_RB(node) ((node)->rb_color != RB_NONE)
352 #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
353 #define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
354 #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
355 #define rq_rb_key(rq) (rq)->sector
358 * as_find_first_arq finds the first (lowest sector numbered) request
359 * for the specified data_dir. Used to sweep back to the start of the disk
360 * (1-way elevator) after we process the last (highest sector) request.
362 static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
364 struct rb_node *n = ad->sort_list[data_dir].rb_node;
370 if (n->rb_left == NULL)
371 return rb_entry_arq(n);
378 * Add the request to the rb tree if it is unique. If there is an alias (an
379 * existing request against the same sector), which can happen when using
380 * direct IO, then return the alias.
382 static struct as_rq *__as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
384 struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
385 struct rb_node *parent = NULL;
387 struct request *rq = arq->request;
389 arq->rb_key = rq_rb_key(rq);
393 __arq = rb_entry_arq(parent);
395 if (arq->rb_key < __arq->rb_key)
397 else if (arq->rb_key > __arq->rb_key)
403 rb_link_node(&arq->rb_node, parent, p);
404 rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
409 static void as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
413 while ((unlikely(alias = __as_add_arq_rb(ad, arq)))) {
414 as_move_to_dispatch(ad, alias);
419 static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
421 if (!ON_RB(&arq->rb_node)) {
426 rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
427 RB_CLEAR(&arq->rb_node);
430 static struct request *
431 as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
433 struct rb_node *n = ad->sort_list[data_dir].rb_node;
437 arq = rb_entry_arq(n);
439 if (sector < arq->rb_key)
441 else if (sector > arq->rb_key)
451 * IO Scheduler proper
454 #define MAXBACK (1024 * 1024) /*
455 * Maximum distance the disk will go backward
459 #define BACK_PENALTY 2
462 * as_choose_req selects the preferred one of two requests of the same data_dir
463 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
465 static struct as_rq *
466 as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
469 sector_t last, s1, s2, d1, d2;
470 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
471 const sector_t maxback = MAXBACK;
473 if (arq1 == NULL || arq1 == arq2)
478 data_dir = arq1->is_sync;
480 last = ad->last_sector[data_dir];
481 s1 = arq1->request->sector;
482 s2 = arq2->request->sector;
484 BUG_ON(data_dir != arq2->is_sync);
487 * Strict one way elevator _except_ in the case where we allow
488 * short backward seeks which are biased as twice the cost of a
489 * similar forward seek.
493 else if (s1+maxback >= last)
494 d1 = (last - s1)*BACK_PENALTY;
497 d1 = 0; /* shut up, gcc */
502 else if (s2+maxback >= last)
503 d2 = (last - s2)*BACK_PENALTY;
509 /* Found required data */
510 if (!r1_wrap && r2_wrap)
512 else if (!r2_wrap && r1_wrap)
514 else if (r1_wrap && r2_wrap) {
515 /* both behind the head */
522 /* Both requests in front of the head */
536 * as_find_next_arq finds the next request after @prev in elevator order.
537 * this with as_choose_req form the basis for how the scheduler chooses
538 * what request to process next. Anticipation works on top of this.
540 static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
542 const int data_dir = last->is_sync;
544 struct rb_node *rbnext = rb_next(&last->rb_node);
545 struct rb_node *rbprev = rb_prev(&last->rb_node);
546 struct as_rq *arq_next, *arq_prev;
548 BUG_ON(!ON_RB(&last->rb_node));
551 arq_prev = rb_entry_arq(rbprev);
556 arq_next = rb_entry_arq(rbnext);
558 arq_next = as_find_first_arq(ad, data_dir);
559 if (arq_next == last)
563 ret = as_choose_req(ad, arq_next, arq_prev);
569 * anticipatory scheduling functions follow
573 * as_antic_expired tells us when we have anticipated too long.
574 * The funny "absolute difference" math on the elapsed time is to handle
575 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
577 static int as_antic_expired(struct as_data *ad)
581 delta_jif = jiffies - ad->antic_start;
582 if (unlikely(delta_jif < 0))
583 delta_jif = -delta_jif;
584 if (delta_jif < ad->antic_expire)
591 * as_antic_waitnext starts anticipating that a nice request will soon be
592 * submitted. See also as_antic_waitreq
594 static void as_antic_waitnext(struct as_data *ad)
596 unsigned long timeout;
598 BUG_ON(ad->antic_status != ANTIC_OFF
599 && ad->antic_status != ANTIC_WAIT_REQ);
601 timeout = ad->antic_start + ad->antic_expire;
603 mod_timer(&ad->antic_timer, timeout);
605 ad->antic_status = ANTIC_WAIT_NEXT;
609 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
610 * until the request that we're anticipating on has finished. This means we
611 * are timing from when the candidate process wakes up hopefully.
613 static void as_antic_waitreq(struct as_data *ad)
615 BUG_ON(ad->antic_status == ANTIC_FINISHED);
616 if (ad->antic_status == ANTIC_OFF) {
617 if (!ad->io_context || ad->ioc_finished)
618 as_antic_waitnext(ad);
620 ad->antic_status = ANTIC_WAIT_REQ;
625 * This is called directly by the functions in this file to stop anticipation.
626 * We kill the timer and schedule a call to the request_fn asap.
628 static void as_antic_stop(struct as_data *ad)
630 int status = ad->antic_status;
632 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
633 if (status == ANTIC_WAIT_NEXT)
634 del_timer(&ad->antic_timer);
635 ad->antic_status = ANTIC_FINISHED;
636 /* see as_work_handler */
637 kblockd_schedule_work(&ad->antic_work);
642 * as_antic_timeout is the timer function set by as_antic_waitnext.
644 static void as_antic_timeout(unsigned long data)
646 struct request_queue *q = (struct request_queue *)data;
647 struct as_data *ad = q->elevator->elevator_data;
650 spin_lock_irqsave(q->queue_lock, flags);
651 if (ad->antic_status == ANTIC_WAIT_REQ
652 || ad->antic_status == ANTIC_WAIT_NEXT) {
653 struct as_io_context *aic = ad->io_context->aic;
655 ad->antic_status = ANTIC_FINISHED;
656 kblockd_schedule_work(&ad->antic_work);
658 if (aic->ttime_samples == 0) {
659 /* process anticipated on has exited or timed out*/
660 ad->exit_prob = (7*ad->exit_prob + 256)/8;
662 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
663 /* process not "saved" by a cooperating request */
664 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
667 spin_unlock_irqrestore(q->queue_lock, flags);
670 static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
673 /* fixed point: 1.0 == 1<<8 */
674 if (aic->ttime_samples == 0) {
675 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
676 ad->new_ttime_mean = ad->new_ttime_total / 256;
678 ad->exit_prob = (7*ad->exit_prob)/8;
680 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
681 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
682 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
685 static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
690 if (aic->seek_samples == 0) {
691 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
692 ad->new_seek_mean = ad->new_seek_total / 256;
696 * Don't allow the seek distance to get too large from the
697 * odd fragment, pagein, etc
699 if (aic->seek_samples <= 60) /* second&third seek */
700 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
702 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
704 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
705 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
706 total = aic->seek_total + (aic->seek_samples/2);
707 do_div(total, aic->seek_samples);
708 aic->seek_mean = (sector_t)total;
712 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
713 * updates @aic->ttime_mean based on that. It is called when a new
716 static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
719 struct as_rq *arq = RQ_DATA(rq);
720 int data_dir = arq->is_sync;
721 unsigned long thinktime = 0;
727 if (data_dir == REQ_SYNC) {
728 unsigned long in_flight = atomic_read(&aic->nr_queued)
729 + atomic_read(&aic->nr_dispatched);
730 spin_lock(&aic->lock);
731 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
732 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
733 /* Calculate read -> read thinktime */
734 if (test_bit(AS_TASK_IORUNNING, &aic->state)
736 thinktime = jiffies - aic->last_end_request;
737 thinktime = min(thinktime, MAX_THINKTIME-1);
739 as_update_thinktime(ad, aic, thinktime);
741 /* Calculate read -> read seek distance */
742 if (aic->last_request_pos < rq->sector)
743 seek_dist = rq->sector - aic->last_request_pos;
745 seek_dist = aic->last_request_pos - rq->sector;
746 as_update_seekdist(ad, aic, seek_dist);
748 aic->last_request_pos = rq->sector + rq->nr_sectors;
749 set_bit(AS_TASK_IOSTARTED, &aic->state);
750 spin_unlock(&aic->lock);
755 * as_close_req decides if one request is considered "close" to the
756 * previous one issued.
758 static int as_close_req(struct as_data *ad, struct as_io_context *aic,
761 unsigned long delay; /* milliseconds */
762 sector_t last = ad->last_sector[ad->batch_data_dir];
763 sector_t next = arq->request->sector;
764 sector_t delta; /* acceptable close offset (in sectors) */
767 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
770 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
774 else if (delay <= 20 && delay <= ad->antic_expire)
775 delta = 8192 << delay;
779 if ((last <= next + (delta>>1)) && (next <= last + delta))
787 if (aic->seek_samples == 0) {
789 * Process has just started IO. Use past statistics to
790 * gauge success possibility
792 if (ad->new_seek_mean > s) {
793 /* this request is better than what we're expecting */
798 if (aic->seek_mean > s) {
799 /* this request is better than what we're expecting */
808 * as_can_break_anticipation returns true if we have been anticipating this
811 * It also returns true if the process against which we are anticipating
812 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
813 * dispatch it ASAP, because we know that application will not be submitting
816 * If the task which has submitted the request has exited, break anticipation.
818 * If this task has queued some other IO, do not enter enticipation.
820 static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
822 struct io_context *ioc;
823 struct as_io_context *aic;
825 ioc = ad->io_context;
828 if (arq && ioc == arq->io_context) {
829 /* request from same process */
833 if (ad->ioc_finished && as_antic_expired(ad)) {
835 * In this situation status should really be FINISHED,
836 * however the timer hasn't had the chance to run yet.
845 if (atomic_read(&aic->nr_queued) > 0) {
846 /* process has more requests queued */
850 if (atomic_read(&aic->nr_dispatched) > 0) {
851 /* process has more requests dispatched */
855 if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, aic, arq)) {
857 * Found a close request that is not one of ours.
859 * This makes close requests from another process update
860 * our IO history. Is generally useful when there are
861 * two or more cooperating processes working in the same
864 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
865 if (aic->ttime_samples == 0)
866 ad->exit_prob = (7*ad->exit_prob + 256)/8;
868 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
871 as_update_iohist(ad, aic, arq->request);
875 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
876 /* process anticipated on has exited */
877 if (aic->ttime_samples == 0)
878 ad->exit_prob = (7*ad->exit_prob + 256)/8;
880 if (ad->exit_no_coop > 128)
884 if (aic->ttime_samples == 0) {
885 if (ad->new_ttime_mean > ad->antic_expire)
887 if (ad->exit_prob * ad->exit_no_coop > 128*256)
889 } else if (aic->ttime_mean > ad->antic_expire) {
890 /* the process thinks too much between requests */
898 * as_can_anticipate indicates weather we should either run arq
899 * or keep anticipating a better request.
901 static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
905 * Last request submitted was a write
909 if (ad->antic_status == ANTIC_FINISHED)
911 * Don't restart if we have just finished. Run the next request
915 if (as_can_break_anticipation(ad, arq))
917 * This request is a good candidate. Don't keep anticipating,
923 * OK from here, we haven't finished, and don't have a decent request!
924 * Status is either ANTIC_OFF so start waiting,
925 * ANTIC_WAIT_REQ so continue waiting for request to finish
926 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
933 * as_update_arq must be called whenever a request (arq) is added to
934 * the sort_list. This function keeps caches up to date, and checks if the
935 * request might be one we are "anticipating"
937 static void as_update_arq(struct as_data *ad, struct as_rq *arq)
939 const int data_dir = arq->is_sync;
941 /* keep the next_arq cache up to date */
942 ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
945 * have we been anticipating this request?
946 * or does it come from the same process as the one we are anticipating
949 if (ad->antic_status == ANTIC_WAIT_REQ
950 || ad->antic_status == ANTIC_WAIT_NEXT) {
951 if (as_can_break_anticipation(ad, arq))
957 * Gathers timings and resizes the write batch automatically
959 static void update_write_batch(struct as_data *ad)
961 unsigned long batch = ad->batch_expire[REQ_ASYNC];
964 write_time = (jiffies - ad->current_batch_expires) + batch;
968 if (write_time > batch && !ad->write_batch_idled) {
969 if (write_time > batch * 3)
970 ad->write_batch_count /= 2;
972 ad->write_batch_count--;
973 } else if (write_time < batch && ad->current_write_count == 0) {
974 if (batch > write_time * 3)
975 ad->write_batch_count *= 2;
977 ad->write_batch_count++;
980 if (ad->write_batch_count < 1)
981 ad->write_batch_count = 1;
985 * as_completed_request is to be called when a request has completed and
986 * returned something to the requesting process, be it an error or data.
988 static void as_completed_request(request_queue_t *q, struct request *rq)
990 struct as_data *ad = q->elevator->elevator_data;
991 struct as_rq *arq = RQ_DATA(rq);
993 WARN_ON(!list_empty(&rq->queuelist));
995 if (arq->state != AS_RQ_REMOVED) {
996 printk("arq->state %d\n", arq->state);
1001 if (ad->changed_batch && ad->nr_dispatched == 1) {
1002 kblockd_schedule_work(&ad->antic_work);
1003 ad->changed_batch = 0;
1005 if (ad->batch_data_dir == REQ_SYNC)
1008 WARN_ON(ad->nr_dispatched == 0);
1009 ad->nr_dispatched--;
1012 * Start counting the batch from when a request of that direction is
1013 * actually serviced. This should help devices with big TCQ windows
1014 * and writeback caches
1016 if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
1017 update_write_batch(ad);
1018 ad->current_batch_expires = jiffies +
1019 ad->batch_expire[REQ_SYNC];
1023 if (ad->io_context == arq->io_context && ad->io_context) {
1024 ad->antic_start = jiffies;
1025 ad->ioc_finished = 1;
1026 if (ad->antic_status == ANTIC_WAIT_REQ) {
1028 * We were waiting on this request, now anticipate
1031 as_antic_waitnext(ad);
1035 as_put_io_context(arq);
1037 arq->state = AS_RQ_POSTSCHED;
1041 * as_remove_queued_request removes a request from the pre dispatch queue
1042 * without updating refcounts. It is expected the caller will drop the
1043 * reference unless it replaces the request at somepart of the elevator
1044 * (ie. the dispatch queue)
1046 static void as_remove_queued_request(request_queue_t *q, struct request *rq)
1048 struct as_rq *arq = RQ_DATA(rq);
1049 const int data_dir = arq->is_sync;
1050 struct as_data *ad = q->elevator->elevator_data;
1052 WARN_ON(arq->state != AS_RQ_QUEUED);
1054 if (arq->io_context && arq->io_context->aic) {
1055 BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
1056 atomic_dec(&arq->io_context->aic->nr_queued);
1060 * Update the "next_arq" cache if we are about to remove its
1063 if (ad->next_arq[data_dir] == arq)
1064 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1066 list_del_init(&arq->fifo);
1067 as_del_arq_hash(arq);
1068 as_del_arq_rb(ad, arq);
1072 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
1073 * 1 otherwise. It is ratelimited so that we only perform the check once per
1074 * `fifo_expire' interval. Otherwise a large number of expired requests
1075 * would create a hopeless seekstorm.
1077 * See as_antic_expired comment.
1079 static int as_fifo_expired(struct as_data *ad, int adir)
1084 delta_jif = jiffies - ad->last_check_fifo[adir];
1085 if (unlikely(delta_jif < 0))
1086 delta_jif = -delta_jif;
1087 if (delta_jif < ad->fifo_expire[adir])
1090 ad->last_check_fifo[adir] = jiffies;
1092 if (list_empty(&ad->fifo_list[adir]))
1095 arq = list_entry_fifo(ad->fifo_list[adir].next);
1097 return time_after(jiffies, arq->expires);
1101 * as_batch_expired returns true if the current batch has expired. A batch
1102 * is a set of reads or a set of writes.
1104 static inline int as_batch_expired(struct as_data *ad)
1106 if (ad->changed_batch || ad->new_batch)
1109 if (ad->batch_data_dir == REQ_SYNC)
1110 /* TODO! add a check so a complete fifo gets written? */
1111 return time_after(jiffies, ad->current_batch_expires);
1113 return time_after(jiffies, ad->current_batch_expires)
1114 || ad->current_write_count == 0;
1118 * move an entry to dispatch queue
1120 static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
1122 struct request *rq = arq->request;
1123 const int data_dir = arq->is_sync;
1125 BUG_ON(!ON_RB(&arq->rb_node));
1128 ad->antic_status = ANTIC_OFF;
1131 * This has to be set in order to be correctly updated by
1134 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
1136 if (data_dir == REQ_SYNC) {
1137 /* In case we have to anticipate after this */
1138 copy_io_context(&ad->io_context, &arq->io_context);
1140 if (ad->io_context) {
1141 put_io_context(ad->io_context);
1142 ad->io_context = NULL;
1145 if (ad->current_write_count != 0)
1146 ad->current_write_count--;
1148 ad->ioc_finished = 0;
1150 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1153 * take it off the sort and fifo list, add to dispatch queue
1155 as_remove_queued_request(ad->q, rq);
1156 WARN_ON(arq->state != AS_RQ_QUEUED);
1158 elv_dispatch_sort(ad->q, rq);
1160 arq->state = AS_RQ_DISPATCHED;
1161 if (arq->io_context && arq->io_context->aic)
1162 atomic_inc(&arq->io_context->aic->nr_dispatched);
1163 ad->nr_dispatched++;
1167 * as_dispatch_request selects the best request according to
1168 * read/write expire, batch expire, etc, and moves it to the dispatch
1169 * queue. Returns 1 if a request was found, 0 otherwise.
1171 static int as_dispatch_request(request_queue_t *q, int force)
1173 struct as_data *ad = q->elevator->elevator_data;
1175 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1176 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
1178 if (unlikely(force)) {
1180 * Forced dispatch, accounting is useless. Reset
1181 * accounting states and dump fifo_lists. Note that
1182 * batch_data_dir is reset to REQ_SYNC to avoid
1183 * screwing write batch accounting as write batch
1184 * accounting occurs on W->R transition.
1188 ad->batch_data_dir = REQ_SYNC;
1189 ad->changed_batch = 0;
1192 while (ad->next_arq[REQ_SYNC]) {
1193 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1196 ad->last_check_fifo[REQ_SYNC] = jiffies;
1198 while (ad->next_arq[REQ_ASYNC]) {
1199 as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
1202 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1207 /* Signal that the write batch was uncontended, so we can't time it */
1208 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1209 if (ad->current_write_count == 0 || !writes)
1210 ad->write_batch_idled = 1;
1213 if (!(reads || writes)
1214 || ad->antic_status == ANTIC_WAIT_REQ
1215 || ad->antic_status == ANTIC_WAIT_NEXT
1216 || ad->changed_batch)
1219 if (!(reads && writes && as_batch_expired(ad))) {
1221 * batch is still running or no reads or no writes
1223 arq = ad->next_arq[ad->batch_data_dir];
1225 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1226 if (as_fifo_expired(ad, REQ_SYNC))
1229 if (as_can_anticipate(ad, arq)) {
1230 as_antic_waitreq(ad);
1236 /* we have a "next request" */
1237 if (reads && !writes)
1238 ad->current_batch_expires =
1239 jiffies + ad->batch_expire[REQ_SYNC];
1240 goto dispatch_request;
1245 * at this point we are not running a batch. select the appropriate
1246 * data direction (read / write)
1250 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC]));
1252 if (writes && ad->batch_data_dir == REQ_SYNC)
1254 * Last batch was a read, switch to writes
1256 goto dispatch_writes;
1258 if (ad->batch_data_dir == REQ_ASYNC) {
1259 WARN_ON(ad->new_batch);
1260 ad->changed_batch = 1;
1262 ad->batch_data_dir = REQ_SYNC;
1263 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1264 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1265 goto dispatch_request;
1269 * the last batch was a read
1274 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC]));
1276 if (ad->batch_data_dir == REQ_SYNC) {
1277 ad->changed_batch = 1;
1280 * new_batch might be 1 when the queue runs out of
1281 * reads. A subsequent submission of a write might
1282 * cause a change of batch before the read is finished.
1286 ad->batch_data_dir = REQ_ASYNC;
1287 ad->current_write_count = ad->write_batch_count;
1288 ad->write_batch_idled = 0;
1289 arq = ad->next_arq[ad->batch_data_dir];
1290 goto dispatch_request;
1298 * If a request has expired, service it.
1301 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1303 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1304 BUG_ON(arq == NULL);
1307 if (ad->changed_batch) {
1308 WARN_ON(ad->new_batch);
1310 if (ad->nr_dispatched)
1313 if (ad->batch_data_dir == REQ_ASYNC)
1314 ad->current_batch_expires = jiffies +
1315 ad->batch_expire[REQ_ASYNC];
1319 ad->changed_batch = 0;
1323 * arq is the selected appropriate request.
1325 as_move_to_dispatch(ad, arq);
1331 * add arq to rbtree and fifo
1333 static void as_add_request(request_queue_t *q, struct request *rq)
1335 struct as_data *ad = q->elevator->elevator_data;
1336 struct as_rq *arq = RQ_DATA(rq);
1339 arq->state = AS_RQ_NEW;
1341 if (rq_data_dir(arq->request) == READ
1342 || current->flags&PF_SYNCWRITE)
1346 data_dir = arq->is_sync;
1348 arq->io_context = as_get_io_context();
1350 if (arq->io_context) {
1351 as_update_iohist(ad, arq->io_context->aic, arq->request);
1352 atomic_inc(&arq->io_context->aic->nr_queued);
1355 as_add_arq_rb(ad, arq);
1356 if (rq_mergeable(arq->request))
1357 as_add_arq_hash(ad, arq);
1360 * set expire time (only used for reads) and add to fifo list
1362 arq->expires = jiffies + ad->fifo_expire[data_dir];
1363 list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
1365 as_update_arq(ad, arq); /* keep state machine up to date */
1366 arq->state = AS_RQ_QUEUED;
1369 static void as_activate_request(request_queue_t *q, struct request *rq)
1371 struct as_rq *arq = RQ_DATA(rq);
1373 WARN_ON(arq->state != AS_RQ_DISPATCHED);
1374 arq->state = AS_RQ_REMOVED;
1375 if (arq->io_context && arq->io_context->aic)
1376 atomic_dec(&arq->io_context->aic->nr_dispatched);
1379 static void as_deactivate_request(request_queue_t *q, struct request *rq)
1381 struct as_rq *arq = RQ_DATA(rq);
1383 WARN_ON(arq->state != AS_RQ_REMOVED);
1384 arq->state = AS_RQ_DISPATCHED;
1385 if (arq->io_context && arq->io_context->aic)
1386 atomic_inc(&arq->io_context->aic->nr_dispatched);
1390 * as_queue_empty tells us if there are requests left in the device. It may
1391 * not be the case that a driver can get the next request even if the queue
1392 * is not empty - it is used in the block layer to check for plugging and
1393 * merging opportunities
1395 static int as_queue_empty(request_queue_t *q)
1397 struct as_data *ad = q->elevator->elevator_data;
1399 return list_empty(&ad->fifo_list[REQ_ASYNC])
1400 && list_empty(&ad->fifo_list[REQ_SYNC]);
1403 static struct request *as_former_request(request_queue_t *q,
1406 struct as_rq *arq = RQ_DATA(rq);
1407 struct rb_node *rbprev = rb_prev(&arq->rb_node);
1408 struct request *ret = NULL;
1411 ret = rb_entry_arq(rbprev)->request;
1416 static struct request *as_latter_request(request_queue_t *q,
1419 struct as_rq *arq = RQ_DATA(rq);
1420 struct rb_node *rbnext = rb_next(&arq->rb_node);
1421 struct request *ret = NULL;
1424 ret = rb_entry_arq(rbnext)->request;
1430 as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1432 struct as_data *ad = q->elevator->elevator_data;
1433 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1434 struct request *__rq;
1438 * see if the merge hash can satisfy a back merge
1440 __rq = as_find_arq_hash(ad, bio->bi_sector);
1442 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
1444 if (elv_rq_merge_ok(__rq, bio)) {
1445 ret = ELEVATOR_BACK_MERGE;
1451 * check for front merge
1453 __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
1455 BUG_ON(rb_key != rq_rb_key(__rq));
1457 if (elv_rq_merge_ok(__rq, bio)) {
1458 ret = ELEVATOR_FRONT_MERGE;
1463 return ELEVATOR_NO_MERGE;
1466 if (rq_mergeable(__rq))
1467 as_hot_arq_hash(ad, RQ_DATA(__rq));
1473 static void as_merged_request(request_queue_t *q, struct request *req)
1475 struct as_data *ad = q->elevator->elevator_data;
1476 struct as_rq *arq = RQ_DATA(req);
1479 * hash always needs to be repositioned, key is end sector
1481 as_del_arq_hash(arq);
1482 as_add_arq_hash(ad, arq);
1485 * if the merge was a front merge, we need to reposition request
1487 if (rq_rb_key(req) != arq->rb_key) {
1488 as_del_arq_rb(ad, arq);
1489 as_add_arq_rb(ad, arq);
1491 * Note! At this stage of this and the next function, our next
1492 * request may not be optimal - eg the request may have "grown"
1493 * behind the disk head. We currently don't bother adjusting.
1498 static void as_merged_requests(request_queue_t *q, struct request *req,
1499 struct request *next)
1501 struct as_data *ad = q->elevator->elevator_data;
1502 struct as_rq *arq = RQ_DATA(req);
1503 struct as_rq *anext = RQ_DATA(next);
1509 * reposition arq (this is the merged request) in hash, and in rbtree
1510 * in case of a front merge
1512 as_del_arq_hash(arq);
1513 as_add_arq_hash(ad, arq);
1515 if (rq_rb_key(req) != arq->rb_key) {
1516 as_del_arq_rb(ad, arq);
1517 as_add_arq_rb(ad, arq);
1521 * if anext expires before arq, assign its expire time to arq
1522 * and move into anext position (anext will be deleted) in fifo
1524 if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
1525 if (time_before(anext->expires, arq->expires)) {
1526 list_move(&arq->fifo, &anext->fifo);
1527 arq->expires = anext->expires;
1529 * Don't copy here but swap, because when anext is
1530 * removed below, it must contain the unused context
1532 swap_io_context(&arq->io_context, &anext->io_context);
1537 * kill knowledge of next, this one is a goner
1539 as_remove_queued_request(q, next);
1540 as_put_io_context(anext);
1542 anext->state = AS_RQ_MERGED;
1546 * This is executed in a "deferred" process context, by kblockd. It calls the
1547 * driver's request_fn so the driver can submit that request.
1549 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1550 * state before calling, and don't rely on any state over calls.
1552 * FIXME! dispatch queue is not a queue at all!
1554 static void as_work_handler(void *data)
1556 struct request_queue *q = data;
1557 unsigned long flags;
1559 spin_lock_irqsave(q->queue_lock, flags);
1560 if (!as_queue_empty(q))
1562 spin_unlock_irqrestore(q->queue_lock, flags);
1565 static void as_put_request(request_queue_t *q, struct request *rq)
1567 struct as_data *ad = q->elevator->elevator_data;
1568 struct as_rq *arq = RQ_DATA(rq);
1575 if (unlikely(arq->state != AS_RQ_POSTSCHED &&
1576 arq->state != AS_RQ_PRESCHED &&
1577 arq->state != AS_RQ_MERGED)) {
1578 printk("arq->state %d\n", arq->state);
1582 mempool_free(arq, ad->arq_pool);
1583 rq->elevator_private = NULL;
1586 static int as_set_request(request_queue_t *q, struct request *rq,
1587 struct bio *bio, gfp_t gfp_mask)
1589 struct as_data *ad = q->elevator->elevator_data;
1590 struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1593 memset(arq, 0, sizeof(*arq));
1594 RB_CLEAR(&arq->rb_node);
1596 arq->state = AS_RQ_PRESCHED;
1597 arq->io_context = NULL;
1598 INIT_LIST_HEAD(&arq->hash);
1600 INIT_LIST_HEAD(&arq->fifo);
1601 rq->elevator_private = arq;
1608 static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
1610 int ret = ELV_MQUEUE_MAY;
1611 struct as_data *ad = q->elevator->elevator_data;
1612 struct io_context *ioc;
1613 if (ad->antic_status == ANTIC_WAIT_REQ ||
1614 ad->antic_status == ANTIC_WAIT_NEXT) {
1615 ioc = as_get_io_context();
1616 if (ad->io_context == ioc)
1617 ret = ELV_MQUEUE_MUST;
1618 put_io_context(ioc);
1624 static void as_exit_queue(elevator_t *e)
1626 struct as_data *ad = e->elevator_data;
1628 del_timer_sync(&ad->antic_timer);
1631 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1632 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1634 mempool_destroy(ad->arq_pool);
1635 put_io_context(ad->io_context);
1641 * initialize elevator private data (as_data), and alloc a arq for
1642 * each request on the free lists
1644 static int as_init_queue(request_queue_t *q, elevator_t *e)
1652 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
1655 memset(ad, 0, sizeof(*ad));
1657 ad->q = q; /* Identify what queue the data belongs to */
1659 ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES,
1660 GFP_KERNEL, q->node);
1666 ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1667 mempool_free_slab, arq_pool, q->node);
1668 if (!ad->arq_pool) {
1674 /* anticipatory scheduling helpers */
1675 ad->antic_timer.function = as_antic_timeout;
1676 ad->antic_timer.data = (unsigned long)q;
1677 init_timer(&ad->antic_timer);
1678 INIT_WORK(&ad->antic_work, as_work_handler, q);
1680 for (i = 0; i < AS_HASH_ENTRIES; i++)
1681 INIT_LIST_HEAD(&ad->hash[i]);
1683 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1684 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1685 ad->sort_list[REQ_SYNC] = RB_ROOT;
1686 ad->sort_list[REQ_ASYNC] = RB_ROOT;
1687 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1688 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1689 ad->antic_expire = default_antic_expire;
1690 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1691 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1692 e->elevator_data = ad;
1694 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1695 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1696 if (ad->write_batch_count < 2)
1697 ad->write_batch_count = 2;
1705 struct as_fs_entry {
1706 struct attribute attr;
1707 ssize_t (*show)(struct as_data *, char *);
1708 ssize_t (*store)(struct as_data *, const char *, size_t);
1712 as_var_show(unsigned int var, char *page)
1714 return sprintf(page, "%d\n", var);
1718 as_var_store(unsigned long *var, const char *page, size_t count)
1720 char *p = (char *) page;
1722 *var = simple_strtoul(p, &p, 10);
1726 static ssize_t as_est_show(struct as_data *ad, char *page)
1730 pos += sprintf(page+pos, "%lu %% exit probability\n",
1731 100*ad->exit_prob/256);
1732 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1733 "cooperating process submitting IO\n",
1734 100*ad->exit_no_coop/256);
1735 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
1736 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1737 (unsigned long long)ad->new_seek_mean);
1742 #define SHOW_FUNCTION(__FUNC, __VAR) \
1743 static ssize_t __FUNC(struct as_data *ad, char *page) \
1745 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1747 SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
1748 SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
1749 SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
1750 SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
1751 SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
1752 #undef SHOW_FUNCTION
1754 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1755 static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
1757 int ret = as_var_store(__PTR, (page), count); \
1758 if (*(__PTR) < (MIN)) \
1760 else if (*(__PTR) > (MAX)) \
1762 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1765 STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1766 STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1767 STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
1768 STORE_FUNCTION(as_read_batchexpire_store,
1769 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
1770 STORE_FUNCTION(as_write_batchexpire_store,
1771 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1772 #undef STORE_FUNCTION
1774 static struct as_fs_entry as_est_entry = {
1775 .attr = {.name = "est_time", .mode = S_IRUGO },
1776 .show = as_est_show,
1778 static struct as_fs_entry as_readexpire_entry = {
1779 .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
1780 .show = as_readexpire_show,
1781 .store = as_readexpire_store,
1783 static struct as_fs_entry as_writeexpire_entry = {
1784 .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
1785 .show = as_writeexpire_show,
1786 .store = as_writeexpire_store,
1788 static struct as_fs_entry as_anticexpire_entry = {
1789 .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
1790 .show = as_anticexpire_show,
1791 .store = as_anticexpire_store,
1793 static struct as_fs_entry as_read_batchexpire_entry = {
1794 .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
1795 .show = as_read_batchexpire_show,
1796 .store = as_read_batchexpire_store,
1798 static struct as_fs_entry as_write_batchexpire_entry = {
1799 .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
1800 .show = as_write_batchexpire_show,
1801 .store = as_write_batchexpire_store,
1804 static struct attribute *default_attrs[] = {
1806 &as_readexpire_entry.attr,
1807 &as_writeexpire_entry.attr,
1808 &as_anticexpire_entry.attr,
1809 &as_read_batchexpire_entry.attr,
1810 &as_write_batchexpire_entry.attr,
1814 #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
1817 as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1819 elevator_t *e = container_of(kobj, elevator_t, kobj);
1820 struct as_fs_entry *entry = to_as(attr);
1825 return entry->show(e->elevator_data, page);
1829 as_attr_store(struct kobject *kobj, struct attribute *attr,
1830 const char *page, size_t length)
1832 elevator_t *e = container_of(kobj, elevator_t, kobj);
1833 struct as_fs_entry *entry = to_as(attr);
1838 return entry->store(e->elevator_data, page, length);
1841 static struct sysfs_ops as_sysfs_ops = {
1842 .show = as_attr_show,
1843 .store = as_attr_store,
1846 static struct kobj_type as_ktype = {
1847 .sysfs_ops = &as_sysfs_ops,
1848 .default_attrs = default_attrs,
1851 static struct elevator_type iosched_as = {
1853 .elevator_merge_fn = as_merge,
1854 .elevator_merged_fn = as_merged_request,
1855 .elevator_merge_req_fn = as_merged_requests,
1856 .elevator_dispatch_fn = as_dispatch_request,
1857 .elevator_add_req_fn = as_add_request,
1858 .elevator_activate_req_fn = as_activate_request,
1859 .elevator_deactivate_req_fn = as_deactivate_request,
1860 .elevator_queue_empty_fn = as_queue_empty,
1861 .elevator_completed_req_fn = as_completed_request,
1862 .elevator_former_req_fn = as_former_request,
1863 .elevator_latter_req_fn = as_latter_request,
1864 .elevator_set_req_fn = as_set_request,
1865 .elevator_put_req_fn = as_put_request,
1866 .elevator_may_queue_fn = as_may_queue,
1867 .elevator_init_fn = as_init_queue,
1868 .elevator_exit_fn = as_exit_queue,
1872 .elevator_ktype = &as_ktype,
1873 .elevator_name = "anticipatory",
1874 .elevator_owner = THIS_MODULE,
1877 static int __init as_init(void)
1881 arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
1886 ret = elv_register(&iosched_as);
1889 * don't allow AS to get unregistered, since we would have
1890 * to browse all tasks in the system and release their
1891 * as_io_context first
1893 __module_get(THIS_MODULE);
1897 kmem_cache_destroy(arq_pool);
1901 static void __exit as_exit(void)
1903 elv_unregister(&iosched_as);
1904 kmem_cache_destroy(arq_pool);
1907 module_init(as_init);
1908 module_exit(as_exit);
1910 MODULE_AUTHOR("Nick Piggin");
1911 MODULE_LICENSE("GPL");
1912 MODULE_DESCRIPTION("anticipatory IO scheduler");