[PATCH] libata: propogate host private data from probe function
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / as-iosched.c
bloba78e160b59a3545b8a06b78e2c78e503f1494d24
1 /*
2 * linux/drivers/block/as-iosched.c
4 * Anticipatory & deadline i/o scheduler.
6 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
7 * Nick Piggin <nickpiggin@yahoo.com.au>
9 */
10 #include <linux/kernel.h>
11 #include <linux/fs.h>
12 #include <linux/blkdev.h>
13 #include <linux/elevator.h>
14 #include <linux/bio.h>
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/compiler.h>
20 #include <linux/hash.h>
21 #include <linux/rbtree.h>
22 #include <linux/interrupt.h>
24 #define REQ_SYNC 1
25 #define REQ_ASYNC 0
28 * See Documentation/block/as-iosched.txt
32 * max time before a read is submitted.
34 #define default_read_expire (HZ / 8)
37 * ditto for writes, these limits are not hard, even
38 * if the disk is capable of satisfying them.
40 #define default_write_expire (HZ / 4)
43 * read_batch_expire describes how long we will allow a stream of reads to
44 * persist before looking to see whether it is time to switch over to writes.
46 #define default_read_batch_expire (HZ / 2)
49 * write_batch_expire describes how long we want a stream of writes to run for.
50 * This is not a hard limit, but a target we set for the auto-tuning thingy.
51 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
52 * a short amount of time...
54 #define default_write_batch_expire (HZ / 8)
57 * max time we may wait to anticipate a read (default around 6ms)
59 #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
62 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
63 * however huge values tend to interfere and not decay fast enough. A program
64 * might be in a non-io phase of operation. Waiting on user input for example,
65 * or doing a lengthy computation. A small penalty can be justified there, and
66 * will still catch out those processes that constantly have large thinktimes.
68 #define MAX_THINKTIME (HZ/50UL)
70 /* Bits in as_io_context.state */
71 enum as_io_states {
72 AS_TASK_RUNNING=0, /* Process has not exited */
73 AS_TASK_IOSTARTED, /* Process has started some IO */
74 AS_TASK_IORUNNING, /* Process has completed some IO */
77 enum anticipation_status {
78 ANTIC_OFF=0, /* Not anticipating (normal operation) */
79 ANTIC_WAIT_REQ, /* The last read has not yet completed */
80 ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
81 last read (which has completed) */
82 ANTIC_FINISHED, /* Anticipating but have found a candidate
83 * or timed out */
86 struct as_data {
88 * run time data
91 struct request_queue *q; /* the "owner" queue */
94 * requests (as_rq s) are present on both sort_list and fifo_list
96 struct rb_root sort_list[2];
97 struct list_head fifo_list[2];
99 struct as_rq *next_arq[2]; /* next in sort order */
100 sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
101 struct list_head *hash; /* request hash */
103 unsigned long exit_prob; /* probability a task will exit while
104 being waited on */
105 unsigned long exit_no_coop; /* probablility an exited task will
106 not be part of a later cooperating
107 request */
108 unsigned long new_ttime_total; /* mean thinktime on new proc */
109 unsigned long new_ttime_mean;
110 u64 new_seek_total; /* mean seek on new proc */
111 sector_t new_seek_mean;
113 unsigned long current_batch_expires;
114 unsigned long last_check_fifo[2];
115 int changed_batch; /* 1: waiting for old batch to end */
116 int new_batch; /* 1: waiting on first read complete */
117 int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
118 int write_batch_count; /* max # of reqs in a write batch */
119 int current_write_count; /* how many requests left this batch */
120 int write_batch_idled; /* has the write batch gone idle? */
121 mempool_t *arq_pool;
123 enum anticipation_status antic_status;
124 unsigned long antic_start; /* jiffies: when it started */
125 struct timer_list antic_timer; /* anticipatory scheduling timer */
126 struct work_struct antic_work; /* Deferred unplugging */
127 struct io_context *io_context; /* Identify the expected process */
128 int ioc_finished; /* IO associated with io_context is finished */
129 int nr_dispatched;
132 * settings that change how the i/o scheduler behaves
134 unsigned long fifo_expire[2];
135 unsigned long batch_expire[2];
136 unsigned long antic_expire;
139 #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
142 * per-request data.
144 enum arq_state {
145 AS_RQ_NEW=0, /* New - not referenced and not on any lists */
146 AS_RQ_QUEUED, /* In the request queue. It belongs to the
147 scheduler */
148 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
149 driver now */
150 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
151 AS_RQ_REMOVED,
152 AS_RQ_MERGED,
153 AS_RQ_POSTSCHED, /* when they shouldn't be */
156 struct as_rq {
158 * rbtree index, key is the starting offset
160 struct rb_node rb_node;
161 sector_t rb_key;
163 struct request *request;
165 struct io_context *io_context; /* The submitting task */
168 * request hash, key is the ending offset (for back merge lookup)
170 struct list_head hash;
171 unsigned int on_hash;
174 * expire fifo
176 struct list_head fifo;
177 unsigned long expires;
179 unsigned int is_sync;
180 enum arq_state state;
183 #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
185 static kmem_cache_t *arq_pool;
188 * IO Context helper functions
191 /* Called to deallocate the as_io_context */
192 static void free_as_io_context(struct as_io_context *aic)
194 kfree(aic);
197 /* Called when the task exits */
198 static void exit_as_io_context(struct as_io_context *aic)
200 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
201 clear_bit(AS_TASK_RUNNING, &aic->state);
204 static struct as_io_context *alloc_as_io_context(void)
206 struct as_io_context *ret;
208 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
209 if (ret) {
210 ret->dtor = free_as_io_context;
211 ret->exit = exit_as_io_context;
212 ret->state = 1 << AS_TASK_RUNNING;
213 atomic_set(&ret->nr_queued, 0);
214 atomic_set(&ret->nr_dispatched, 0);
215 spin_lock_init(&ret->lock);
216 ret->ttime_total = 0;
217 ret->ttime_samples = 0;
218 ret->ttime_mean = 0;
219 ret->seek_total = 0;
220 ret->seek_samples = 0;
221 ret->seek_mean = 0;
224 return ret;
228 * If the current task has no AS IO context then create one and initialise it.
229 * Then take a ref on the task's io context and return it.
231 static struct io_context *as_get_io_context(void)
233 struct io_context *ioc = get_io_context(GFP_ATOMIC);
234 if (ioc && !ioc->aic) {
235 ioc->aic = alloc_as_io_context();
236 if (!ioc->aic) {
237 put_io_context(ioc);
238 ioc = NULL;
241 return ioc;
244 static void as_put_io_context(struct as_rq *arq)
246 struct as_io_context *aic;
248 if (unlikely(!arq->io_context))
249 return;
251 aic = arq->io_context->aic;
253 if (arq->is_sync == REQ_SYNC && aic) {
254 spin_lock(&aic->lock);
255 set_bit(AS_TASK_IORUNNING, &aic->state);
256 aic->last_end_request = jiffies;
257 spin_unlock(&aic->lock);
260 put_io_context(arq->io_context);
264 * the back merge hash support functions
266 static const int as_hash_shift = 6;
267 #define AS_HASH_BLOCK(sec) ((sec) >> 3)
268 #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
269 #define AS_HASH_ENTRIES (1 << as_hash_shift)
270 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
271 #define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash)
273 static inline void __as_del_arq_hash(struct as_rq *arq)
275 arq->on_hash = 0;
276 list_del_init(&arq->hash);
279 static inline void as_del_arq_hash(struct as_rq *arq)
281 if (arq->on_hash)
282 __as_del_arq_hash(arq);
285 static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
287 struct request *rq = arq->request;
289 BUG_ON(arq->on_hash);
291 arq->on_hash = 1;
292 list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
296 * move hot entry to front of chain
298 static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
300 struct request *rq = arq->request;
301 struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
303 if (!arq->on_hash) {
304 WARN_ON(1);
305 return;
308 if (arq->hash.prev != head) {
309 list_del(&arq->hash);
310 list_add(&arq->hash, head);
314 static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
316 struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
317 struct list_head *entry, *next = hash_list->next;
319 while ((entry = next) != hash_list) {
320 struct as_rq *arq = list_entry_hash(entry);
321 struct request *__rq = arq->request;
323 next = entry->next;
325 BUG_ON(!arq->on_hash);
327 if (!rq_mergeable(__rq)) {
328 as_del_arq_hash(arq);
329 continue;
332 if (rq_hash_key(__rq) == offset)
333 return __rq;
336 return NULL;
340 * rb tree support functions
342 #define RB_NONE (2)
343 #define RB_EMPTY(root) ((root)->rb_node == NULL)
344 #define ON_RB(node) ((node)->rb_color != RB_NONE)
345 #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
346 #define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
347 #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
348 #define rq_rb_key(rq) (rq)->sector
351 * as_find_first_arq finds the first (lowest sector numbered) request
352 * for the specified data_dir. Used to sweep back to the start of the disk
353 * (1-way elevator) after we process the last (highest sector) request.
355 static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
357 struct rb_node *n = ad->sort_list[data_dir].rb_node;
359 if (n == NULL)
360 return NULL;
362 for (;;) {
363 if (n->rb_left == NULL)
364 return rb_entry_arq(n);
366 n = n->rb_left;
371 * Add the request to the rb tree if it is unique. If there is an alias (an
372 * existing request against the same sector), which can happen when using
373 * direct IO, then return the alias.
375 static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
377 struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
378 struct rb_node *parent = NULL;
379 struct as_rq *__arq;
380 struct request *rq = arq->request;
382 arq->rb_key = rq_rb_key(rq);
384 while (*p) {
385 parent = *p;
386 __arq = rb_entry_arq(parent);
388 if (arq->rb_key < __arq->rb_key)
389 p = &(*p)->rb_left;
390 else if (arq->rb_key > __arq->rb_key)
391 p = &(*p)->rb_right;
392 else
393 return __arq;
396 rb_link_node(&arq->rb_node, parent, p);
397 rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
399 return NULL;
402 static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
404 if (!ON_RB(&arq->rb_node)) {
405 WARN_ON(1);
406 return;
409 rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
410 RB_CLEAR(&arq->rb_node);
413 static struct request *
414 as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
416 struct rb_node *n = ad->sort_list[data_dir].rb_node;
417 struct as_rq *arq;
419 while (n) {
420 arq = rb_entry_arq(n);
422 if (sector < arq->rb_key)
423 n = n->rb_left;
424 else if (sector > arq->rb_key)
425 n = n->rb_right;
426 else
427 return arq->request;
430 return NULL;
434 * IO Scheduler proper
437 #define MAXBACK (1024 * 1024) /*
438 * Maximum distance the disk will go backward
439 * for a request.
442 #define BACK_PENALTY 2
445 * as_choose_req selects the preferred one of two requests of the same data_dir
446 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
448 static struct as_rq *
449 as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
451 int data_dir;
452 sector_t last, s1, s2, d1, d2;
453 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
454 const sector_t maxback = MAXBACK;
456 if (arq1 == NULL || arq1 == arq2)
457 return arq2;
458 if (arq2 == NULL)
459 return arq1;
461 data_dir = arq1->is_sync;
463 last = ad->last_sector[data_dir];
464 s1 = arq1->request->sector;
465 s2 = arq2->request->sector;
467 BUG_ON(data_dir != arq2->is_sync);
470 * Strict one way elevator _except_ in the case where we allow
471 * short backward seeks which are biased as twice the cost of a
472 * similar forward seek.
474 if (s1 >= last)
475 d1 = s1 - last;
476 else if (s1+maxback >= last)
477 d1 = (last - s1)*BACK_PENALTY;
478 else {
479 r1_wrap = 1;
480 d1 = 0; /* shut up, gcc */
483 if (s2 >= last)
484 d2 = s2 - last;
485 else if (s2+maxback >= last)
486 d2 = (last - s2)*BACK_PENALTY;
487 else {
488 r2_wrap = 1;
489 d2 = 0;
492 /* Found required data */
493 if (!r1_wrap && r2_wrap)
494 return arq1;
495 else if (!r2_wrap && r1_wrap)
496 return arq2;
497 else if (r1_wrap && r2_wrap) {
498 /* both behind the head */
499 if (s1 <= s2)
500 return arq1;
501 else
502 return arq2;
505 /* Both requests in front of the head */
506 if (d1 < d2)
507 return arq1;
508 else if (d2 < d1)
509 return arq2;
510 else {
511 if (s1 >= s2)
512 return arq1;
513 else
514 return arq2;
519 * as_find_next_arq finds the next request after @prev in elevator order.
520 * this with as_choose_req form the basis for how the scheduler chooses
521 * what request to process next. Anticipation works on top of this.
523 static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
525 const int data_dir = last->is_sync;
526 struct as_rq *ret;
527 struct rb_node *rbnext = rb_next(&last->rb_node);
528 struct rb_node *rbprev = rb_prev(&last->rb_node);
529 struct as_rq *arq_next, *arq_prev;
531 BUG_ON(!ON_RB(&last->rb_node));
533 if (rbprev)
534 arq_prev = rb_entry_arq(rbprev);
535 else
536 arq_prev = NULL;
538 if (rbnext)
539 arq_next = rb_entry_arq(rbnext);
540 else {
541 arq_next = as_find_first_arq(ad, data_dir);
542 if (arq_next == last)
543 arq_next = NULL;
546 ret = as_choose_req(ad, arq_next, arq_prev);
548 return ret;
552 * anticipatory scheduling functions follow
556 * as_antic_expired tells us when we have anticipated too long.
557 * The funny "absolute difference" math on the elapsed time is to handle
558 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
560 static int as_antic_expired(struct as_data *ad)
562 long delta_jif;
564 delta_jif = jiffies - ad->antic_start;
565 if (unlikely(delta_jif < 0))
566 delta_jif = -delta_jif;
567 if (delta_jif < ad->antic_expire)
568 return 0;
570 return 1;
574 * as_antic_waitnext starts anticipating that a nice request will soon be
575 * submitted. See also as_antic_waitreq
577 static void as_antic_waitnext(struct as_data *ad)
579 unsigned long timeout;
581 BUG_ON(ad->antic_status != ANTIC_OFF
582 && ad->antic_status != ANTIC_WAIT_REQ);
584 timeout = ad->antic_start + ad->antic_expire;
586 mod_timer(&ad->antic_timer, timeout);
588 ad->antic_status = ANTIC_WAIT_NEXT;
592 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
593 * until the request that we're anticipating on has finished. This means we
594 * are timing from when the candidate process wakes up hopefully.
596 static void as_antic_waitreq(struct as_data *ad)
598 BUG_ON(ad->antic_status == ANTIC_FINISHED);
599 if (ad->antic_status == ANTIC_OFF) {
600 if (!ad->io_context || ad->ioc_finished)
601 as_antic_waitnext(ad);
602 else
603 ad->antic_status = ANTIC_WAIT_REQ;
608 * This is called directly by the functions in this file to stop anticipation.
609 * We kill the timer and schedule a call to the request_fn asap.
611 static void as_antic_stop(struct as_data *ad)
613 int status = ad->antic_status;
615 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
616 if (status == ANTIC_WAIT_NEXT)
617 del_timer(&ad->antic_timer);
618 ad->antic_status = ANTIC_FINISHED;
619 /* see as_work_handler */
620 kblockd_schedule_work(&ad->antic_work);
625 * as_antic_timeout is the timer function set by as_antic_waitnext.
627 static void as_antic_timeout(unsigned long data)
629 struct request_queue *q = (struct request_queue *)data;
630 struct as_data *ad = q->elevator->elevator_data;
631 unsigned long flags;
633 spin_lock_irqsave(q->queue_lock, flags);
634 if (ad->antic_status == ANTIC_WAIT_REQ
635 || ad->antic_status == ANTIC_WAIT_NEXT) {
636 struct as_io_context *aic = ad->io_context->aic;
638 ad->antic_status = ANTIC_FINISHED;
639 kblockd_schedule_work(&ad->antic_work);
641 if (aic->ttime_samples == 0) {
642 /* process anticipated on has exited or timed out*/
643 ad->exit_prob = (7*ad->exit_prob + 256)/8;
645 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
646 /* process not "saved" by a cooperating request */
647 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
650 spin_unlock_irqrestore(q->queue_lock, flags);
653 static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
654 unsigned long ttime)
656 /* fixed point: 1.0 == 1<<8 */
657 if (aic->ttime_samples == 0) {
658 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
659 ad->new_ttime_mean = ad->new_ttime_total / 256;
661 ad->exit_prob = (7*ad->exit_prob)/8;
663 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
664 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
665 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
668 static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
669 sector_t sdist)
671 u64 total;
673 if (aic->seek_samples == 0) {
674 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
675 ad->new_seek_mean = ad->new_seek_total / 256;
679 * Don't allow the seek distance to get too large from the
680 * odd fragment, pagein, etc
682 if (aic->seek_samples <= 60) /* second&third seek */
683 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
684 else
685 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
687 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
688 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
689 total = aic->seek_total + (aic->seek_samples/2);
690 do_div(total, aic->seek_samples);
691 aic->seek_mean = (sector_t)total;
695 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
696 * updates @aic->ttime_mean based on that. It is called when a new
697 * request is queued.
699 static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
700 struct request *rq)
702 struct as_rq *arq = RQ_DATA(rq);
703 int data_dir = arq->is_sync;
704 unsigned long thinktime = 0;
705 sector_t seek_dist;
707 if (aic == NULL)
708 return;
710 if (data_dir == REQ_SYNC) {
711 unsigned long in_flight = atomic_read(&aic->nr_queued)
712 + atomic_read(&aic->nr_dispatched);
713 spin_lock(&aic->lock);
714 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
715 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
716 /* Calculate read -> read thinktime */
717 if (test_bit(AS_TASK_IORUNNING, &aic->state)
718 && in_flight == 0) {
719 thinktime = jiffies - aic->last_end_request;
720 thinktime = min(thinktime, MAX_THINKTIME-1);
722 as_update_thinktime(ad, aic, thinktime);
724 /* Calculate read -> read seek distance */
725 if (aic->last_request_pos < rq->sector)
726 seek_dist = rq->sector - aic->last_request_pos;
727 else
728 seek_dist = aic->last_request_pos - rq->sector;
729 as_update_seekdist(ad, aic, seek_dist);
731 aic->last_request_pos = rq->sector + rq->nr_sectors;
732 set_bit(AS_TASK_IOSTARTED, &aic->state);
733 spin_unlock(&aic->lock);
738 * as_close_req decides if one request is considered "close" to the
739 * previous one issued.
741 static int as_close_req(struct as_data *ad, struct as_io_context *aic,
742 struct as_rq *arq)
744 unsigned long delay; /* milliseconds */
745 sector_t last = ad->last_sector[ad->batch_data_dir];
746 sector_t next = arq->request->sector;
747 sector_t delta; /* acceptable close offset (in sectors) */
748 sector_t s;
750 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
751 delay = 0;
752 else
753 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
755 if (delay == 0)
756 delta = 8192;
757 else if (delay <= 20 && delay <= ad->antic_expire)
758 delta = 8192 << delay;
759 else
760 return 1;
762 if ((last <= next + (delta>>1)) && (next <= last + delta))
763 return 1;
765 if (last < next)
766 s = next - last;
767 else
768 s = last - next;
770 if (aic->seek_samples == 0) {
772 * Process has just started IO. Use past statistics to
773 * gauge success possibility
775 if (ad->new_seek_mean > s) {
776 /* this request is better than what we're expecting */
777 return 1;
780 } else {
781 if (aic->seek_mean > s) {
782 /* this request is better than what we're expecting */
783 return 1;
787 return 0;
791 * as_can_break_anticipation returns true if we have been anticipating this
792 * request.
794 * It also returns true if the process against which we are anticipating
795 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
796 * dispatch it ASAP, because we know that application will not be submitting
797 * any new reads.
799 * If the task which has submitted the request has exited, break anticipation.
801 * If this task has queued some other IO, do not enter enticipation.
803 static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
805 struct io_context *ioc;
806 struct as_io_context *aic;
808 ioc = ad->io_context;
809 BUG_ON(!ioc);
811 if (arq && ioc == arq->io_context) {
812 /* request from same process */
813 return 1;
816 if (ad->ioc_finished && as_antic_expired(ad)) {
818 * In this situation status should really be FINISHED,
819 * however the timer hasn't had the chance to run yet.
821 return 1;
824 aic = ioc->aic;
825 if (!aic)
826 return 0;
828 if (atomic_read(&aic->nr_queued) > 0) {
829 /* process has more requests queued */
830 return 1;
833 if (atomic_read(&aic->nr_dispatched) > 0) {
834 /* process has more requests dispatched */
835 return 1;
838 if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, aic, arq)) {
840 * Found a close request that is not one of ours.
842 * This makes close requests from another process update
843 * our IO history. Is generally useful when there are
844 * two or more cooperating processes working in the same
845 * area.
847 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
848 if (aic->ttime_samples == 0)
849 ad->exit_prob = (7*ad->exit_prob + 256)/8;
851 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
854 as_update_iohist(ad, aic, arq->request);
855 return 1;
858 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
859 /* process anticipated on has exited */
860 if (aic->ttime_samples == 0)
861 ad->exit_prob = (7*ad->exit_prob + 256)/8;
863 if (ad->exit_no_coop > 128)
864 return 1;
867 if (aic->ttime_samples == 0) {
868 if (ad->new_ttime_mean > ad->antic_expire)
869 return 1;
870 if (ad->exit_prob * ad->exit_no_coop > 128*256)
871 return 1;
872 } else if (aic->ttime_mean > ad->antic_expire) {
873 /* the process thinks too much between requests */
874 return 1;
877 return 0;
881 * as_can_anticipate indicates weather we should either run arq
882 * or keep anticipating a better request.
884 static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
886 if (!ad->io_context)
888 * Last request submitted was a write
890 return 0;
892 if (ad->antic_status == ANTIC_FINISHED)
894 * Don't restart if we have just finished. Run the next request
896 return 0;
898 if (as_can_break_anticipation(ad, arq))
900 * This request is a good candidate. Don't keep anticipating,
901 * run it.
903 return 0;
906 * OK from here, we haven't finished, and don't have a decent request!
907 * Status is either ANTIC_OFF so start waiting,
908 * ANTIC_WAIT_REQ so continue waiting for request to finish
909 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
912 return 1;
916 * as_update_arq must be called whenever a request (arq) is added to
917 * the sort_list. This function keeps caches up to date, and checks if the
918 * request might be one we are "anticipating"
920 static void as_update_arq(struct as_data *ad, struct as_rq *arq)
922 const int data_dir = arq->is_sync;
924 /* keep the next_arq cache up to date */
925 ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
928 * have we been anticipating this request?
929 * or does it come from the same process as the one we are anticipating
930 * for?
932 if (ad->antic_status == ANTIC_WAIT_REQ
933 || ad->antic_status == ANTIC_WAIT_NEXT) {
934 if (as_can_break_anticipation(ad, arq))
935 as_antic_stop(ad);
940 * Gathers timings and resizes the write batch automatically
942 static void update_write_batch(struct as_data *ad)
944 unsigned long batch = ad->batch_expire[REQ_ASYNC];
945 long write_time;
947 write_time = (jiffies - ad->current_batch_expires) + batch;
948 if (write_time < 0)
949 write_time = 0;
951 if (write_time > batch && !ad->write_batch_idled) {
952 if (write_time > batch * 3)
953 ad->write_batch_count /= 2;
954 else
955 ad->write_batch_count--;
956 } else if (write_time < batch && ad->current_write_count == 0) {
957 if (batch > write_time * 3)
958 ad->write_batch_count *= 2;
959 else
960 ad->write_batch_count++;
963 if (ad->write_batch_count < 1)
964 ad->write_batch_count = 1;
968 * as_completed_request is to be called when a request has completed and
969 * returned something to the requesting process, be it an error or data.
971 static void as_completed_request(request_queue_t *q, struct request *rq)
973 struct as_data *ad = q->elevator->elevator_data;
974 struct as_rq *arq = RQ_DATA(rq);
976 WARN_ON(!list_empty(&rq->queuelist));
978 if (arq->state != AS_RQ_REMOVED) {
979 printk("arq->state %d\n", arq->state);
980 WARN_ON(1);
981 goto out;
984 if (ad->changed_batch && ad->nr_dispatched == 1) {
985 kblockd_schedule_work(&ad->antic_work);
986 ad->changed_batch = 0;
988 if (ad->batch_data_dir == REQ_SYNC)
989 ad->new_batch = 1;
991 WARN_ON(ad->nr_dispatched == 0);
992 ad->nr_dispatched--;
995 * Start counting the batch from when a request of that direction is
996 * actually serviced. This should help devices with big TCQ windows
997 * and writeback caches
999 if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
1000 update_write_batch(ad);
1001 ad->current_batch_expires = jiffies +
1002 ad->batch_expire[REQ_SYNC];
1003 ad->new_batch = 0;
1006 if (ad->io_context == arq->io_context && ad->io_context) {
1007 ad->antic_start = jiffies;
1008 ad->ioc_finished = 1;
1009 if (ad->antic_status == ANTIC_WAIT_REQ) {
1011 * We were waiting on this request, now anticipate
1012 * the next one
1014 as_antic_waitnext(ad);
1018 as_put_io_context(arq);
1019 out:
1020 arq->state = AS_RQ_POSTSCHED;
1024 * as_remove_queued_request removes a request from the pre dispatch queue
1025 * without updating refcounts. It is expected the caller will drop the
1026 * reference unless it replaces the request at somepart of the elevator
1027 * (ie. the dispatch queue)
1029 static void as_remove_queued_request(request_queue_t *q, struct request *rq)
1031 struct as_rq *arq = RQ_DATA(rq);
1032 const int data_dir = arq->is_sync;
1033 struct as_data *ad = q->elevator->elevator_data;
1035 WARN_ON(arq->state != AS_RQ_QUEUED);
1037 if (arq->io_context && arq->io_context->aic) {
1038 BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
1039 atomic_dec(&arq->io_context->aic->nr_queued);
1043 * Update the "next_arq" cache if we are about to remove its
1044 * entry
1046 if (ad->next_arq[data_dir] == arq)
1047 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1049 list_del_init(&arq->fifo);
1050 as_del_arq_hash(arq);
1051 as_del_arq_rb(ad, arq);
1055 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
1056 * 1 otherwise. It is ratelimited so that we only perform the check once per
1057 * `fifo_expire' interval. Otherwise a large number of expired requests
1058 * would create a hopeless seekstorm.
1060 * See as_antic_expired comment.
1062 static int as_fifo_expired(struct as_data *ad, int adir)
1064 struct as_rq *arq;
1065 long delta_jif;
1067 delta_jif = jiffies - ad->last_check_fifo[adir];
1068 if (unlikely(delta_jif < 0))
1069 delta_jif = -delta_jif;
1070 if (delta_jif < ad->fifo_expire[adir])
1071 return 0;
1073 ad->last_check_fifo[adir] = jiffies;
1075 if (list_empty(&ad->fifo_list[adir]))
1076 return 0;
1078 arq = list_entry_fifo(ad->fifo_list[adir].next);
1080 return time_after(jiffies, arq->expires);
1084 * as_batch_expired returns true if the current batch has expired. A batch
1085 * is a set of reads or a set of writes.
1087 static inline int as_batch_expired(struct as_data *ad)
1089 if (ad->changed_batch || ad->new_batch)
1090 return 0;
1092 if (ad->batch_data_dir == REQ_SYNC)
1093 /* TODO! add a check so a complete fifo gets written? */
1094 return time_after(jiffies, ad->current_batch_expires);
1096 return time_after(jiffies, ad->current_batch_expires)
1097 || ad->current_write_count == 0;
1101 * move an entry to dispatch queue
1103 static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
1105 struct request *rq = arq->request;
1106 const int data_dir = arq->is_sync;
1108 BUG_ON(!ON_RB(&arq->rb_node));
1110 as_antic_stop(ad);
1111 ad->antic_status = ANTIC_OFF;
1114 * This has to be set in order to be correctly updated by
1115 * as_find_next_arq
1117 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
1119 if (data_dir == REQ_SYNC) {
1120 /* In case we have to anticipate after this */
1121 copy_io_context(&ad->io_context, &arq->io_context);
1122 } else {
1123 if (ad->io_context) {
1124 put_io_context(ad->io_context);
1125 ad->io_context = NULL;
1128 if (ad->current_write_count != 0)
1129 ad->current_write_count--;
1131 ad->ioc_finished = 0;
1133 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1136 * take it off the sort and fifo list, add to dispatch queue
1138 while (!list_empty(&rq->queuelist)) {
1139 struct request *__rq = list_entry_rq(rq->queuelist.next);
1140 struct as_rq *__arq = RQ_DATA(__rq);
1142 list_del(&__rq->queuelist);
1144 elv_dispatch_add_tail(ad->q, __rq);
1146 if (__arq->io_context && __arq->io_context->aic)
1147 atomic_inc(&__arq->io_context->aic->nr_dispatched);
1149 WARN_ON(__arq->state != AS_RQ_QUEUED);
1150 __arq->state = AS_RQ_DISPATCHED;
1152 ad->nr_dispatched++;
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;
1174 struct as_rq *arq;
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.
1186 int dispatched = 0;
1188 ad->batch_data_dir = REQ_SYNC;
1189 ad->changed_batch = 0;
1190 ad->new_batch = 0;
1192 while (ad->next_arq[REQ_SYNC]) {
1193 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1194 dispatched++;
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]);
1200 dispatched++;
1202 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1204 return dispatched;
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)
1217 return 0;
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))
1227 goto fifo_expired;
1229 if (as_can_anticipate(ad, arq)) {
1230 as_antic_waitreq(ad);
1231 return 0;
1235 if (arq) {
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)
1249 if (reads) {
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
1272 if (writes) {
1273 dispatch_writes:
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.
1284 ad->new_batch = 0;
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;
1293 BUG();
1294 return 0;
1296 dispatch_request:
1298 * If a request has expired, service it.
1301 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1302 fifo_expired:
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)
1311 return 0;
1313 if (ad->batch_data_dir == REQ_ASYNC)
1314 ad->current_batch_expires = jiffies +
1315 ad->batch_expire[REQ_ASYNC];
1316 else
1317 ad->new_batch = 1;
1319 ad->changed_batch = 0;
1323 * arq is the selected appropriate request.
1325 as_move_to_dispatch(ad, arq);
1327 return 1;
1331 * Add arq to a list behind alias
1333 static inline void
1334 as_add_aliased_request(struct as_data *ad, struct as_rq *arq,
1335 struct as_rq *alias)
1337 struct request *req = arq->request;
1338 struct list_head *insert = alias->request->queuelist.prev;
1341 * Transfer list of aliases
1343 while (!list_empty(&req->queuelist)) {
1344 struct request *__rq = list_entry_rq(req->queuelist.next);
1345 struct as_rq *__arq = RQ_DATA(__rq);
1347 list_move_tail(&__rq->queuelist, &alias->request->queuelist);
1349 WARN_ON(__arq->state != AS_RQ_QUEUED);
1353 * Another request with the same start sector on the rbtree.
1354 * Link this request to that sector. They are untangled in
1355 * as_move_to_dispatch
1357 list_add(&arq->request->queuelist, insert);
1360 * Don't want to have to handle merges.
1362 as_del_arq_hash(arq);
1363 arq->request->flags |= REQ_NOMERGE;
1367 * add arq to rbtree and fifo
1369 static void as_add_request(request_queue_t *q, struct request *rq)
1371 struct as_data *ad = q->elevator->elevator_data;
1372 struct as_rq *arq = RQ_DATA(rq);
1373 struct as_rq *alias;
1374 int data_dir;
1376 if (arq->state != AS_RQ_PRESCHED) {
1377 printk("arq->state: %d\n", arq->state);
1378 WARN_ON(1);
1380 arq->state = AS_RQ_NEW;
1382 if (rq_data_dir(arq->request) == READ
1383 || current->flags&PF_SYNCWRITE)
1384 arq->is_sync = 1;
1385 else
1386 arq->is_sync = 0;
1387 data_dir = arq->is_sync;
1389 arq->io_context = as_get_io_context();
1391 if (arq->io_context) {
1392 as_update_iohist(ad, arq->io_context->aic, arq->request);
1393 atomic_inc(&arq->io_context->aic->nr_queued);
1396 alias = as_add_arq_rb(ad, arq);
1397 if (!alias) {
1399 * set expire time (only used for reads) and add to fifo list
1401 arq->expires = jiffies + ad->fifo_expire[data_dir];
1402 list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
1404 if (rq_mergeable(arq->request))
1405 as_add_arq_hash(ad, arq);
1406 as_update_arq(ad, arq); /* keep state machine up to date */
1408 } else {
1409 as_add_aliased_request(ad, arq, alias);
1412 * have we been anticipating this request?
1413 * or does it come from the same process as the one we are
1414 * anticipating for?
1416 if (ad->antic_status == ANTIC_WAIT_REQ
1417 || ad->antic_status == ANTIC_WAIT_NEXT) {
1418 if (as_can_break_anticipation(ad, arq))
1419 as_antic_stop(ad);
1423 arq->state = AS_RQ_QUEUED;
1426 static void as_activate_request(request_queue_t *q, struct request *rq)
1428 struct as_rq *arq = RQ_DATA(rq);
1430 WARN_ON(arq->state != AS_RQ_DISPATCHED);
1431 arq->state = AS_RQ_REMOVED;
1432 if (arq->io_context && arq->io_context->aic)
1433 atomic_dec(&arq->io_context->aic->nr_dispatched);
1436 static void as_deactivate_request(request_queue_t *q, struct request *rq)
1438 struct as_rq *arq = RQ_DATA(rq);
1440 WARN_ON(arq->state != AS_RQ_REMOVED);
1441 arq->state = AS_RQ_DISPATCHED;
1442 if (arq->io_context && arq->io_context->aic)
1443 atomic_inc(&arq->io_context->aic->nr_dispatched);
1447 * as_queue_empty tells us if there are requests left in the device. It may
1448 * not be the case that a driver can get the next request even if the queue
1449 * is not empty - it is used in the block layer to check for plugging and
1450 * merging opportunities
1452 static int as_queue_empty(request_queue_t *q)
1454 struct as_data *ad = q->elevator->elevator_data;
1456 return list_empty(&ad->fifo_list[REQ_ASYNC])
1457 && list_empty(&ad->fifo_list[REQ_SYNC]);
1460 static struct request *as_former_request(request_queue_t *q,
1461 struct request *rq)
1463 struct as_rq *arq = RQ_DATA(rq);
1464 struct rb_node *rbprev = rb_prev(&arq->rb_node);
1465 struct request *ret = NULL;
1467 if (rbprev)
1468 ret = rb_entry_arq(rbprev)->request;
1470 return ret;
1473 static struct request *as_latter_request(request_queue_t *q,
1474 struct request *rq)
1476 struct as_rq *arq = RQ_DATA(rq);
1477 struct rb_node *rbnext = rb_next(&arq->rb_node);
1478 struct request *ret = NULL;
1480 if (rbnext)
1481 ret = rb_entry_arq(rbnext)->request;
1483 return ret;
1486 static int
1487 as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1489 struct as_data *ad = q->elevator->elevator_data;
1490 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1491 struct request *__rq;
1492 int ret;
1495 * see if the merge hash can satisfy a back merge
1497 __rq = as_find_arq_hash(ad, bio->bi_sector);
1498 if (__rq) {
1499 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
1501 if (elv_rq_merge_ok(__rq, bio)) {
1502 ret = ELEVATOR_BACK_MERGE;
1503 goto out;
1508 * check for front merge
1510 __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
1511 if (__rq) {
1512 BUG_ON(rb_key != rq_rb_key(__rq));
1514 if (elv_rq_merge_ok(__rq, bio)) {
1515 ret = ELEVATOR_FRONT_MERGE;
1516 goto out;
1520 return ELEVATOR_NO_MERGE;
1521 out:
1522 if (ret) {
1523 if (rq_mergeable(__rq))
1524 as_hot_arq_hash(ad, RQ_DATA(__rq));
1526 *req = __rq;
1527 return ret;
1530 static void as_merged_request(request_queue_t *q, struct request *req)
1532 struct as_data *ad = q->elevator->elevator_data;
1533 struct as_rq *arq = RQ_DATA(req);
1536 * hash always needs to be repositioned, key is end sector
1538 as_del_arq_hash(arq);
1539 as_add_arq_hash(ad, arq);
1542 * if the merge was a front merge, we need to reposition request
1544 if (rq_rb_key(req) != arq->rb_key) {
1545 struct as_rq *alias, *next_arq = NULL;
1547 if (ad->next_arq[arq->is_sync] == arq)
1548 next_arq = as_find_next_arq(ad, arq);
1551 * Note! We should really be moving any old aliased requests
1552 * off this request and try to insert them into the rbtree. We
1553 * currently don't bother. Ditto the next function.
1555 as_del_arq_rb(ad, arq);
1556 if ((alias = as_add_arq_rb(ad, arq))) {
1557 list_del_init(&arq->fifo);
1558 as_add_aliased_request(ad, arq, alias);
1559 if (next_arq)
1560 ad->next_arq[arq->is_sync] = next_arq;
1563 * Note! At this stage of this and the next function, our next
1564 * request may not be optimal - eg the request may have "grown"
1565 * behind the disk head. We currently don't bother adjusting.
1570 static void as_merged_requests(request_queue_t *q, struct request *req,
1571 struct request *next)
1573 struct as_data *ad = q->elevator->elevator_data;
1574 struct as_rq *arq = RQ_DATA(req);
1575 struct as_rq *anext = RQ_DATA(next);
1577 BUG_ON(!arq);
1578 BUG_ON(!anext);
1581 * reposition arq (this is the merged request) in hash, and in rbtree
1582 * in case of a front merge
1584 as_del_arq_hash(arq);
1585 as_add_arq_hash(ad, arq);
1587 if (rq_rb_key(req) != arq->rb_key) {
1588 struct as_rq *alias, *next_arq = NULL;
1590 if (ad->next_arq[arq->is_sync] == arq)
1591 next_arq = as_find_next_arq(ad, arq);
1593 as_del_arq_rb(ad, arq);
1594 if ((alias = as_add_arq_rb(ad, arq))) {
1595 list_del_init(&arq->fifo);
1596 as_add_aliased_request(ad, arq, alias);
1597 if (next_arq)
1598 ad->next_arq[arq->is_sync] = next_arq;
1603 * if anext expires before arq, assign its expire time to arq
1604 * and move into anext position (anext will be deleted) in fifo
1606 if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
1607 if (time_before(anext->expires, arq->expires)) {
1608 list_move(&arq->fifo, &anext->fifo);
1609 arq->expires = anext->expires;
1611 * Don't copy here but swap, because when anext is
1612 * removed below, it must contain the unused context
1614 swap_io_context(&arq->io_context, &anext->io_context);
1619 * Transfer list of aliases
1621 while (!list_empty(&next->queuelist)) {
1622 struct request *__rq = list_entry_rq(next->queuelist.next);
1623 struct as_rq *__arq = RQ_DATA(__rq);
1625 list_move_tail(&__rq->queuelist, &req->queuelist);
1627 WARN_ON(__arq->state != AS_RQ_QUEUED);
1631 * kill knowledge of next, this one is a goner
1633 as_remove_queued_request(q, next);
1634 as_put_io_context(anext);
1636 anext->state = AS_RQ_MERGED;
1640 * This is executed in a "deferred" process context, by kblockd. It calls the
1641 * driver's request_fn so the driver can submit that request.
1643 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1644 * state before calling, and don't rely on any state over calls.
1646 * FIXME! dispatch queue is not a queue at all!
1648 static void as_work_handler(void *data)
1650 struct request_queue *q = data;
1651 unsigned long flags;
1653 spin_lock_irqsave(q->queue_lock, flags);
1654 if (!as_queue_empty(q))
1655 q->request_fn(q);
1656 spin_unlock_irqrestore(q->queue_lock, flags);
1659 static void as_put_request(request_queue_t *q, struct request *rq)
1661 struct as_data *ad = q->elevator->elevator_data;
1662 struct as_rq *arq = RQ_DATA(rq);
1664 if (!arq) {
1665 WARN_ON(1);
1666 return;
1669 if (unlikely(arq->state != AS_RQ_POSTSCHED &&
1670 arq->state != AS_RQ_PRESCHED &&
1671 arq->state != AS_RQ_MERGED)) {
1672 printk("arq->state %d\n", arq->state);
1673 WARN_ON(1);
1676 mempool_free(arq, ad->arq_pool);
1677 rq->elevator_private = NULL;
1680 static int as_set_request(request_queue_t *q, struct request *rq,
1681 struct bio *bio, gfp_t gfp_mask)
1683 struct as_data *ad = q->elevator->elevator_data;
1684 struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1686 if (arq) {
1687 memset(arq, 0, sizeof(*arq));
1688 RB_CLEAR(&arq->rb_node);
1689 arq->request = rq;
1690 arq->state = AS_RQ_PRESCHED;
1691 arq->io_context = NULL;
1692 INIT_LIST_HEAD(&arq->hash);
1693 arq->on_hash = 0;
1694 INIT_LIST_HEAD(&arq->fifo);
1695 rq->elevator_private = arq;
1696 return 0;
1699 return 1;
1702 static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
1704 int ret = ELV_MQUEUE_MAY;
1705 struct as_data *ad = q->elevator->elevator_data;
1706 struct io_context *ioc;
1707 if (ad->antic_status == ANTIC_WAIT_REQ ||
1708 ad->antic_status == ANTIC_WAIT_NEXT) {
1709 ioc = as_get_io_context();
1710 if (ad->io_context == ioc)
1711 ret = ELV_MQUEUE_MUST;
1712 put_io_context(ioc);
1715 return ret;
1718 static void as_exit_queue(elevator_t *e)
1720 struct as_data *ad = e->elevator_data;
1722 del_timer_sync(&ad->antic_timer);
1723 kblockd_flush();
1725 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1726 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1728 mempool_destroy(ad->arq_pool);
1729 put_io_context(ad->io_context);
1730 kfree(ad->hash);
1731 kfree(ad);
1735 * initialize elevator private data (as_data), and alloc a arq for
1736 * each request on the free lists
1738 static int as_init_queue(request_queue_t *q, elevator_t *e)
1740 struct as_data *ad;
1741 int i;
1743 if (!arq_pool)
1744 return -ENOMEM;
1746 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
1747 if (!ad)
1748 return -ENOMEM;
1749 memset(ad, 0, sizeof(*ad));
1751 ad->q = q; /* Identify what queue the data belongs to */
1753 ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES,
1754 GFP_KERNEL, q->node);
1755 if (!ad->hash) {
1756 kfree(ad);
1757 return -ENOMEM;
1760 ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1761 mempool_free_slab, arq_pool, q->node);
1762 if (!ad->arq_pool) {
1763 kfree(ad->hash);
1764 kfree(ad);
1765 return -ENOMEM;
1768 /* anticipatory scheduling helpers */
1769 ad->antic_timer.function = as_antic_timeout;
1770 ad->antic_timer.data = (unsigned long)q;
1771 init_timer(&ad->antic_timer);
1772 INIT_WORK(&ad->antic_work, as_work_handler, q);
1774 for (i = 0; i < AS_HASH_ENTRIES; i++)
1775 INIT_LIST_HEAD(&ad->hash[i]);
1777 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1778 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1779 ad->sort_list[REQ_SYNC] = RB_ROOT;
1780 ad->sort_list[REQ_ASYNC] = RB_ROOT;
1781 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1782 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1783 ad->antic_expire = default_antic_expire;
1784 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1785 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1786 e->elevator_data = ad;
1788 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1789 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1790 if (ad->write_batch_count < 2)
1791 ad->write_batch_count = 2;
1793 return 0;
1797 * sysfs parts below
1799 struct as_fs_entry {
1800 struct attribute attr;
1801 ssize_t (*show)(struct as_data *, char *);
1802 ssize_t (*store)(struct as_data *, const char *, size_t);
1805 static ssize_t
1806 as_var_show(unsigned int var, char *page)
1808 return sprintf(page, "%d\n", var);
1811 static ssize_t
1812 as_var_store(unsigned long *var, const char *page, size_t count)
1814 char *p = (char *) page;
1816 *var = simple_strtoul(p, &p, 10);
1817 return count;
1820 static ssize_t as_est_show(struct as_data *ad, char *page)
1822 int pos = 0;
1824 pos += sprintf(page+pos, "%lu %% exit probability\n",
1825 100*ad->exit_prob/256);
1826 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1827 "cooperating process submitting IO\n",
1828 100*ad->exit_no_coop/256);
1829 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
1830 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1831 (unsigned long long)ad->new_seek_mean);
1833 return pos;
1836 #define SHOW_FUNCTION(__FUNC, __VAR) \
1837 static ssize_t __FUNC(struct as_data *ad, char *page) \
1839 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1841 SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
1842 SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
1843 SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
1844 SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
1845 SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
1846 #undef SHOW_FUNCTION
1848 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1849 static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
1851 int ret = as_var_store(__PTR, (page), count); \
1852 if (*(__PTR) < (MIN)) \
1853 *(__PTR) = (MIN); \
1854 else if (*(__PTR) > (MAX)) \
1855 *(__PTR) = (MAX); \
1856 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1857 return ret; \
1859 STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1860 STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1861 STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
1862 STORE_FUNCTION(as_read_batchexpire_store,
1863 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
1864 STORE_FUNCTION(as_write_batchexpire_store,
1865 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1866 #undef STORE_FUNCTION
1868 static struct as_fs_entry as_est_entry = {
1869 .attr = {.name = "est_time", .mode = S_IRUGO },
1870 .show = as_est_show,
1872 static struct as_fs_entry as_readexpire_entry = {
1873 .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
1874 .show = as_readexpire_show,
1875 .store = as_readexpire_store,
1877 static struct as_fs_entry as_writeexpire_entry = {
1878 .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
1879 .show = as_writeexpire_show,
1880 .store = as_writeexpire_store,
1882 static struct as_fs_entry as_anticexpire_entry = {
1883 .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
1884 .show = as_anticexpire_show,
1885 .store = as_anticexpire_store,
1887 static struct as_fs_entry as_read_batchexpire_entry = {
1888 .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
1889 .show = as_read_batchexpire_show,
1890 .store = as_read_batchexpire_store,
1892 static struct as_fs_entry as_write_batchexpire_entry = {
1893 .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
1894 .show = as_write_batchexpire_show,
1895 .store = as_write_batchexpire_store,
1898 static struct attribute *default_attrs[] = {
1899 &as_est_entry.attr,
1900 &as_readexpire_entry.attr,
1901 &as_writeexpire_entry.attr,
1902 &as_anticexpire_entry.attr,
1903 &as_read_batchexpire_entry.attr,
1904 &as_write_batchexpire_entry.attr,
1905 NULL,
1908 #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
1910 static ssize_t
1911 as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1913 elevator_t *e = container_of(kobj, elevator_t, kobj);
1914 struct as_fs_entry *entry = to_as(attr);
1916 if (!entry->show)
1917 return -EIO;
1919 return entry->show(e->elevator_data, page);
1922 static ssize_t
1923 as_attr_store(struct kobject *kobj, struct attribute *attr,
1924 const char *page, size_t length)
1926 elevator_t *e = container_of(kobj, elevator_t, kobj);
1927 struct as_fs_entry *entry = to_as(attr);
1929 if (!entry->store)
1930 return -EIO;
1932 return entry->store(e->elevator_data, page, length);
1935 static struct sysfs_ops as_sysfs_ops = {
1936 .show = as_attr_show,
1937 .store = as_attr_store,
1940 static struct kobj_type as_ktype = {
1941 .sysfs_ops = &as_sysfs_ops,
1942 .default_attrs = default_attrs,
1945 static struct elevator_type iosched_as = {
1946 .ops = {
1947 .elevator_merge_fn = as_merge,
1948 .elevator_merged_fn = as_merged_request,
1949 .elevator_merge_req_fn = as_merged_requests,
1950 .elevator_dispatch_fn = as_dispatch_request,
1951 .elevator_add_req_fn = as_add_request,
1952 .elevator_activate_req_fn = as_activate_request,
1953 .elevator_deactivate_req_fn = as_deactivate_request,
1954 .elevator_queue_empty_fn = as_queue_empty,
1955 .elevator_completed_req_fn = as_completed_request,
1956 .elevator_former_req_fn = as_former_request,
1957 .elevator_latter_req_fn = as_latter_request,
1958 .elevator_set_req_fn = as_set_request,
1959 .elevator_put_req_fn = as_put_request,
1960 .elevator_may_queue_fn = as_may_queue,
1961 .elevator_init_fn = as_init_queue,
1962 .elevator_exit_fn = as_exit_queue,
1965 .elevator_ktype = &as_ktype,
1966 .elevator_name = "anticipatory",
1967 .elevator_owner = THIS_MODULE,
1970 static int __init as_init(void)
1972 int ret;
1974 arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
1975 0, 0, NULL, NULL);
1976 if (!arq_pool)
1977 return -ENOMEM;
1979 ret = elv_register(&iosched_as);
1980 if (!ret) {
1982 * don't allow AS to get unregistered, since we would have
1983 * to browse all tasks in the system and release their
1984 * as_io_context first
1986 __module_get(THIS_MODULE);
1987 return 0;
1990 kmem_cache_destroy(arq_pool);
1991 return ret;
1994 static void __exit as_exit(void)
1996 elv_unregister(&iosched_as);
1997 kmem_cache_destroy(arq_pool);
2000 module_init(as_init);
2001 module_exit(as_exit);
2003 MODULE_AUTHOR("Nick Piggin");
2004 MODULE_LICENSE("GPL");
2005 MODULE_DESCRIPTION("anticipatory IO scheduler");