1
2
3
4
5
6
7
8
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>
23
24#define REQ_SYNC 1
25#define REQ_ASYNC 0
26
27
28
29
30
31
32
33
34#define default_read_expire (HZ / 8)
35
36
37
38
39
40#define default_write_expire (HZ / 4)
41
42
43
44
45
46#define default_read_batch_expire (HZ / 2)
47
48
49
50
51
52
53
54#define default_write_batch_expire (HZ / 8)
55
56
57
58
59#define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
60
61
62
63
64
65
66
67
68#define MAX_THINKTIME (HZ/50UL)
69
70
71enum as_io_states {
72 AS_TASK_RUNNING=0,
73 AS_TASK_IOSTARTED,
74 AS_TASK_IORUNNING,
75};
76
77enum anticipation_status {
78 ANTIC_OFF=0,
79 ANTIC_WAIT_REQ,
80 ANTIC_WAIT_NEXT,
81
82 ANTIC_FINISHED,
83
84};
85
86struct as_data {
87
88
89
90
91 struct request_queue *q;
92
93
94
95
96 struct rb_root sort_list[2];
97 struct list_head fifo_list[2];
98
99 struct as_rq *next_arq[2];
100 sector_t last_sector[2];
101 struct list_head *dispatch;
102 struct list_head *hash;
103
104 unsigned long exit_prob;
105
106 unsigned long new_ttime_total;
107 unsigned long new_ttime_mean;
108 u64 new_seek_total;
109 sector_t new_seek_mean;
110
111 unsigned long current_batch_expires;
112 unsigned long last_check_fifo[2];
113 int changed_batch;
114 int new_batch;
115 int batch_data_dir;
116 int write_batch_count;
117 int current_write_count;
118 int write_batch_idled;
119 mempool_t *arq_pool;
120
121 enum anticipation_status antic_status;
122 unsigned long antic_start;
123 struct timer_list antic_timer;
124 struct work_struct antic_work;
125 struct io_context *io_context;
126 int ioc_finished;
127 int nr_dispatched;
128
129
130
131
132 unsigned long fifo_expire[2];
133 unsigned long batch_expire[2];
134 unsigned long antic_expire;
135};
136
137#define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
138
139
140
141
142enum arq_state {
143 AS_RQ_NEW=0,
144 AS_RQ_QUEUED,
145
146 AS_RQ_DISPATCHED,
147
148 AS_RQ_PRESCHED,
149 AS_RQ_REMOVED,
150 AS_RQ_MERGED,
151 AS_RQ_POSTSCHED,
152};
153
154struct as_rq {
155
156
157
158 struct rb_node rb_node;
159 sector_t rb_key;
160
161 struct request *request;
162
163 struct io_context *io_context;
164
165
166
167
168 struct list_head hash;
169 unsigned int on_hash;
170
171
172
173
174 struct list_head fifo;
175 unsigned long expires;
176
177 unsigned int is_sync;
178 enum arq_state state;
179};
180
181#define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
182
183static kmem_cache_t *arq_pool;
184
185
186
187
188
189
190static void free_as_io_context(struct as_io_context *aic)
191{
192 kfree(aic);
193}
194
195
196static void exit_as_io_context(struct as_io_context *aic)
197{
198 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
199 clear_bit(AS_TASK_RUNNING, &aic->state);
200}
201
202static struct as_io_context *alloc_as_io_context(void)
203{
204 struct as_io_context *ret;
205
206 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
207 if (ret) {
208 ret->dtor = free_as_io_context;
209 ret->exit = exit_as_io_context;
210 ret->state = 1 << AS_TASK_RUNNING;
211 atomic_set(&ret->nr_queued, 0);
212 atomic_set(&ret->nr_dispatched, 0);
213 spin_lock_init(&ret->lock);
214 ret->ttime_total = 0;
215 ret->ttime_samples = 0;
216 ret->ttime_mean = 0;
217 ret->seek_total = 0;
218 ret->seek_samples = 0;
219 ret->seek_mean = 0;
220 }
221
222 return ret;
223}
224
225
226
227
228
229static struct io_context *as_get_io_context(void)
230{
231 struct io_context *ioc = get_io_context(GFP_ATOMIC);
232 if (ioc && !ioc->aic) {
233 ioc->aic = alloc_as_io_context();
234 if (!ioc->aic) {
235 put_io_context(ioc);
236 ioc = NULL;
237 }
238 }
239 return ioc;
240}
241
242
243
244
245static const int as_hash_shift = 6;
246#define AS_HASH_BLOCK(sec) ((sec) >> 3)
247#define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
248#define AS_HASH_ENTRIES (1 << as_hash_shift)
249#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
250#define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash)
251
252static inline void __as_del_arq_hash(struct as_rq *arq)
253{
254 arq->on_hash = 0;
255 list_del_init(&arq->hash);
256}
257
258static inline void as_del_arq_hash(struct as_rq *arq)
259{
260 if (arq->on_hash)
261 __as_del_arq_hash(arq);
262}
263
264static void as_remove_merge_hints(request_queue_t *q, struct as_rq *arq)
265{
266 as_del_arq_hash(arq);
267
268 if (q->last_merge == arq->request)
269 q->last_merge = NULL;
270}
271
272static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
273{
274 struct request *rq = arq->request;
275
276 BUG_ON(arq->on_hash);
277
278 arq->on_hash = 1;
279 list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
280}
281
282
283
284
285static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
286{
287 struct request *rq = arq->request;
288 struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
289
290 if (!arq->on_hash) {
291 WARN_ON(1);
292 return;
293 }
294
295 if (arq->hash.prev != head) {
296 list_del(&arq->hash);
297 list_add(&arq->hash, head);
298 }
299}
300
301static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
302{
303 struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
304 struct list_head *entry, *next = hash_list->next;
305
306 while ((entry = next) != hash_list) {
307 struct as_rq *arq = list_entry_hash(entry);
308 struct request *__rq = arq->request;
309
310 next = entry->next;
311
312 BUG_ON(!arq->on_hash);
313
314 if (!rq_mergeable(__rq)) {
315 as_remove_merge_hints(ad->q, arq);
316 continue;
317 }
318
319 if (rq_hash_key(__rq) == offset)
320 return __rq;
321 }
322
323 return NULL;
324}
325
326
327
328
329#define RB_NONE (2)
330#define RB_EMPTY(root) ((root)->rb_node == NULL)
331#define ON_RB(node) ((node)->rb_color != RB_NONE)
332#define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
333#define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
334#define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
335#define rq_rb_key(rq) (rq)->sector
336
337
338
339
340
341
342static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
343{
344 struct rb_node *n = ad->sort_list[data_dir].rb_node;
345
346 if (n == NULL)
347 return NULL;
348
349 for (;;) {
350 if (n->rb_left == NULL)
351 return rb_entry_arq(n);
352
353 n = n->rb_left;
354 }
355}
356
357
358
359
360
361
362static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
363{
364 struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
365 struct rb_node *parent = NULL;
366 struct as_rq *__arq;
367 struct request *rq = arq->request;
368
369 arq->rb_key = rq_rb_key(rq);
370
371 while (*p) {
372 parent = *p;
373 __arq = rb_entry_arq(parent);
374
375 if (arq->rb_key < __arq->rb_key)
376 p = &(*p)->rb_left;
377 else if (arq->rb_key > __arq->rb_key)
378 p = &(*p)->rb_right;
379 else
380 return __arq;
381 }
382
383 rb_link_node(&arq->rb_node, parent, p);
384 rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
385
386 return NULL;
387}
388
389static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
390{
391 if (!ON_RB(&arq->rb_node)) {
392 WARN_ON(1);
393 return;
394 }
395
396 rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
397 RB_CLEAR(&arq->rb_node);
398}
399
400static struct request *
401as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
402{
403 struct rb_node *n = ad->sort_list[data_dir].rb_node;
404 struct as_rq *arq;
405
406 while (n) {
407 arq = rb_entry_arq(n);
408
409 if (sector < arq->rb_key)
410 n = n->rb_left;
411 else if (sector > arq->rb_key)
412 n = n->rb_right;
413 else
414 return arq->request;
415 }
416
417 return NULL;
418}
419
420
421
422
423
424#define MAXBACK (1024 * 1024)
425
426
427
428
429#define BACK_PENALTY 2
430
431
432
433
434
435static struct as_rq *
436as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
437{
438 int data_dir;
439 sector_t last, s1, s2, d1, d2;
440 int r1_wrap=0, r2_wrap=0;
441 const sector_t maxback = MAXBACK;
442
443 if (arq1 == NULL || arq1 == arq2)
444 return arq2;
445 if (arq2 == NULL)
446 return arq1;
447
448 data_dir = arq1->is_sync;
449
450 last = ad->last_sector[data_dir];
451 s1 = arq1->request->sector;
452 s2 = arq2->request->sector;
453
454 BUG_ON(data_dir != arq2->is_sync);
455
456
457
458
459
460
461 if (s1 >= last)
462 d1 = s1 - last;
463 else if (s1+maxback >= last)
464 d1 = (last - s1)*BACK_PENALTY;
465 else {
466 r1_wrap = 1;
467 d1 = 0;
468 }
469
470 if (s2 >= last)
471 d2 = s2 - last;
472 else if (s2+maxback >= last)
473 d2 = (last - s2)*BACK_PENALTY;
474 else {
475 r2_wrap = 1;
476 d2 = 0;
477 }
478
479
480 if (!r1_wrap && r2_wrap)
481 return arq1;
482 else if (!r2_wrap && r1_wrap)
483 return arq2;
484 else if (r1_wrap && r2_wrap) {
485
486 if (s1 <= s2)
487 return arq1;
488 else
489 return arq2;
490 }
491
492
493 if (d1 < d2)
494 return arq1;
495 else if (d2 < d1)
496 return arq2;
497 else {
498 if (s1 >= s2)
499 return arq1;
500 else
501 return arq2;
502 }
503}
504
505
506
507
508
509
510static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
511{
512 const int data_dir = last->is_sync;
513 struct as_rq *ret;
514 struct rb_node *rbnext = rb_next(&last->rb_node);
515 struct rb_node *rbprev = rb_prev(&last->rb_node);
516 struct as_rq *arq_next, *arq_prev;
517
518 BUG_ON(!ON_RB(&last->rb_node));
519
520 if (rbprev)
521 arq_prev = rb_entry_arq(rbprev);
522 else
523 arq_prev = NULL;
524
525 if (rbnext)
526 arq_next = rb_entry_arq(rbnext);
527 else {
528 arq_next = as_find_first_arq(ad, data_dir);
529 if (arq_next == last)
530 arq_next = NULL;
531 }
532
533 ret = as_choose_req(ad, arq_next, arq_prev);
534
535 return ret;
536}
537
538
539
540
541
542
543
544
545
546
547static int as_antic_expired(struct as_data *ad)
548{
549 long delta_jif;
550
551 delta_jif = jiffies - ad->antic_start;
552 if (unlikely(delta_jif < 0))
553 delta_jif = -delta_jif;
554 if (delta_jif < ad->antic_expire)
555 return 0;
556
557 return 1;
558}
559
560
561
562
563
564static void as_antic_waitnext(struct as_data *ad)
565{
566 unsigned long timeout;
567
568 BUG_ON(ad->antic_status != ANTIC_OFF
569 && ad->antic_status != ANTIC_WAIT_REQ);
570
571 timeout = ad->antic_start + ad->antic_expire;
572
573 mod_timer(&ad->antic_timer, timeout);
574
575 ad->antic_status = ANTIC_WAIT_NEXT;
576}
577
578
579
580
581
582
583static void as_antic_waitreq(struct as_data *ad)
584{
585 BUG_ON(ad->antic_status == ANTIC_FINISHED);
586 if (ad->antic_status == ANTIC_OFF) {
587 if (!ad->io_context || ad->ioc_finished)
588 as_antic_waitnext(ad);
589 else
590 ad->antic_status = ANTIC_WAIT_REQ;
591 }
592}
593
594
595
596
597
598static void as_antic_stop(struct as_data *ad)
599{
600 int status = ad->antic_status;
601
602 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
603 if (status == ANTIC_WAIT_NEXT)
604 del_timer(&ad->antic_timer);
605 ad->antic_status = ANTIC_FINISHED;
606
607 kblockd_schedule_work(&ad->antic_work);
608 }
609}
610
611
612
613
614static void as_antic_timeout(unsigned long data)
615{
616 struct request_queue *q = (struct request_queue *)data;
617 struct as_data *ad = q->elevator->elevator_data;
618 unsigned long flags;
619
620 spin_lock_irqsave(q->queue_lock, flags);
621 if (ad->antic_status == ANTIC_WAIT_REQ
622 || ad->antic_status == ANTIC_WAIT_NEXT) {
623 struct as_io_context *aic = ad->io_context->aic;
624
625 ad->antic_status = ANTIC_FINISHED;
626 kblockd_schedule_work(&ad->antic_work);
627
628 if (aic->ttime_samples == 0) {
629
630 ad->exit_prob = (7*ad->exit_prob + 256)/8;
631 }
632 }
633 spin_unlock_irqrestore(q->queue_lock, flags);
634}
635
636
637
638
639
640static int as_close_req(struct as_data *ad, struct as_rq *arq)
641{
642 unsigned long delay;
643 sector_t last = ad->last_sector[ad->batch_data_dir];
644 sector_t next = arq->request->sector;
645 sector_t delta;
646
647 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
648 delay = 0;
649 else
650 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
651
652 if (delay <= 1)
653 delta = 64;
654 else if (delay <= 20 && delay <= ad->antic_expire)
655 delta = 64 << (delay-1);
656 else
657 return 1;
658
659 return (last - (delta>>1) <= next) && (next <= last + delta);
660}
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
676{
677 struct io_context *ioc;
678 struct as_io_context *aic;
679 sector_t s;
680
681 ioc = ad->io_context;
682 BUG_ON(!ioc);
683
684 if (arq && ioc == arq->io_context) {
685
686 return 1;
687 }
688
689 if (ad->ioc_finished && as_antic_expired(ad)) {
690
691
692
693
694 return 1;
695 }
696
697 aic = ioc->aic;
698 if (!aic)
699 return 0;
700
701 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
702
703 if (aic->ttime_samples == 0)
704 ad->exit_prob = (7*ad->exit_prob + 256)/8;
705 return 1;
706 }
707
708 if (atomic_read(&aic->nr_queued) > 0) {
709
710 return 1;
711 }
712
713 if (atomic_read(&aic->nr_dispatched) > 0) {
714
715 return 1;
716 }
717
718 if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, arq)) {
719
720
721
722
723
724
725
726
727 spin_lock(&aic->lock);
728 aic->last_end_request = jiffies;
729 spin_unlock(&aic->lock);
730 return 1;
731 }
732
733
734 if (aic->ttime_samples == 0) {
735 if (ad->new_ttime_mean > ad->antic_expire)
736 return 1;
737 if (ad->exit_prob > 128)
738 return 1;
739 } else if (aic->ttime_mean > ad->antic_expire) {
740
741 return 1;
742 }
743
744 if (!arq)
745 return 0;
746
747 if (ad->last_sector[REQ_SYNC] < arq->request->sector)
748 s = arq->request->sector - ad->last_sector[REQ_SYNC];
749 else
750 s = ad->last_sector[REQ_SYNC] - arq->request->sector;
751
752 if (aic->seek_samples == 0) {
753
754
755
756
757 if (ad->new_seek_mean > s) {
758
759 return 1;
760 }
761
762 } else {
763 if (aic->seek_mean > s) {
764
765 return 1;
766 }
767 }
768
769 return 0;
770}
771
772
773
774
775
776static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
777{
778 if (!ad->io_context)
779
780
781
782 return 0;
783
784 if (ad->antic_status == ANTIC_FINISHED)
785
786
787
788 return 0;
789
790 if (as_can_break_anticipation(ad, arq))
791
792
793
794
795 return 0;
796
797
798
799
800
801
802
803
804
805 return 1;
806}
807
808static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, unsigned long ttime)
809{
810
811 if (aic->ttime_samples == 0) {
812 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
813 ad->new_ttime_mean = ad->new_ttime_total / 256;
814
815 ad->exit_prob = (7*ad->exit_prob)/8;
816 }
817 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
818 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
819 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
820}
821
822static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, sector_t sdist)
823{
824 u64 total;
825
826 if (aic->seek_samples == 0) {
827 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
828 ad->new_seek_mean = ad->new_seek_total / 256;
829 }
830
831
832
833
834
835 if (aic->seek_samples <= 60)
836 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
837 else
838 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
839
840 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
841 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
842 total = aic->seek_total + (aic->seek_samples/2);
843 do_div(total, aic->seek_samples);
844 aic->seek_mean = (sector_t)total;
845}
846
847
848
849
850
851
852static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, struct request *rq)
853{
854 struct as_rq *arq = RQ_DATA(rq);
855 int data_dir = arq->is_sync;
856 unsigned long thinktime;
857 sector_t seek_dist;
858
859 if (aic == NULL)
860 return;
861
862 if (data_dir == REQ_SYNC) {
863 unsigned long in_flight = atomic_read(&aic->nr_queued)
864 + atomic_read(&aic->nr_dispatched);
865 spin_lock(&aic->lock);
866 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
867 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
868
869 if (test_bit(AS_TASK_IORUNNING, &aic->state)
870 && in_flight == 0) {
871 thinktime = jiffies - aic->last_end_request;
872 thinktime = min(thinktime, MAX_THINKTIME-1);
873 } else
874 thinktime = 0;
875 as_update_thinktime(ad, aic, thinktime);
876
877
878 if (aic->last_request_pos < rq->sector)
879 seek_dist = rq->sector - aic->last_request_pos;
880 else
881 seek_dist = aic->last_request_pos - rq->sector;
882 as_update_seekdist(ad, aic, seek_dist);
883 }
884 aic->last_request_pos = rq->sector + rq->nr_sectors;
885 set_bit(AS_TASK_IOSTARTED, &aic->state);
886 spin_unlock(&aic->lock);
887 }
888}
889
890
891
892
893
894
895static void as_update_arq(struct as_data *ad, struct as_rq *arq)
896{
897 const int data_dir = arq->is_sync;
898
899
900 ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
901
902
903
904
905
906
907 if (ad->antic_status == ANTIC_WAIT_REQ
908 || ad->antic_status == ANTIC_WAIT_NEXT) {
909 if (as_can_break_anticipation(ad, arq))
910 as_antic_stop(ad);
911 }
912}
913
914
915
916
917static void update_write_batch(struct as_data *ad)
918{
919 unsigned long batch = ad->batch_expire[REQ_ASYNC];
920 long write_time;
921
922 write_time = (jiffies - ad->current_batch_expires) + batch;
923 if (write_time < 0)
924 write_time = 0;
925
926 if (write_time > batch && !ad->write_batch_idled) {
927 if (write_time > batch * 3)
928 ad->write_batch_count /= 2;
929 else
930 ad->write_batch_count--;
931 } else if (write_time < batch && ad->current_write_count == 0) {
932 if (batch > write_time * 3)
933 ad->write_batch_count *= 2;
934 else
935 ad->write_batch_count++;
936 }
937
938 if (ad->write_batch_count < 1)
939 ad->write_batch_count = 1;
940}
941
942
943
944
945
946static void as_completed_request(request_queue_t *q, struct request *rq)
947{
948 struct as_data *ad = q->elevator->elevator_data;
949 struct as_rq *arq = RQ_DATA(rq);
950
951 WARN_ON(!list_empty(&rq->queuelist));
952
953 if (arq->state == AS_RQ_PRESCHED) {
954 WARN_ON(arq->io_context);
955 goto out;
956 }
957
958 if (arq->state == AS_RQ_MERGED)
959 goto out_ioc;
960
961 if (arq->state != AS_RQ_REMOVED) {
962 printk("arq->state %d\n", arq->state);
963 WARN_ON(1);
964 goto out;
965 }
966
967 if (!blk_fs_request(rq))
968 goto out;
969
970 if (ad->changed_batch && ad->nr_dispatched == 1) {
971 kblockd_schedule_work(&ad->antic_work);
972 ad->changed_batch = 0;
973
974 if (ad->batch_data_dir == REQ_SYNC)
975 ad->new_batch = 1;
976 }
977 WARN_ON(ad->nr_dispatched == 0);
978 ad->nr_dispatched--;
979
980
981
982
983
984
985 if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
986 update_write_batch(ad);
987 ad->current_batch_expires = jiffies +
988 ad->batch_expire[REQ_SYNC];
989 ad->new_batch = 0;
990 }
991
992 if (ad->io_context == arq->io_context && ad->io_context) {
993 ad->antic_start = jiffies;
994 ad->ioc_finished = 1;
995 if (ad->antic_status == ANTIC_WAIT_REQ) {
996
997
998
999
1000 as_antic_waitnext(ad);
1001 }
1002 }
1003
1004out_ioc:
1005 if (!arq->io_context)
1006 goto out;
1007
1008 if (arq->is_sync == REQ_SYNC) {
1009 struct as_io_context *aic = arq->io_context->aic;
1010 if (aic) {
1011 spin_lock(&aic->lock);
1012 set_bit(AS_TASK_IORUNNING, &aic->state);
1013 aic->last_end_request = jiffies;
1014 spin_unlock(&aic->lock);
1015 }
1016 }
1017
1018 put_io_context(arq->io_context);
1019out:
1020 arq->state = AS_RQ_POSTSCHED;
1021}
1022
1023
1024
1025
1026
1027
1028
1029static void as_remove_queued_request(request_queue_t *q, struct request *rq)
1030{
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;
1034
1035 WARN_ON(arq->state != AS_RQ_QUEUED);
1036
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);
1040 }
1041
1042
1043
1044
1045
1046 if (ad->next_arq[data_dir] == arq)
1047 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1048
1049 list_del_init(&arq->fifo);
1050 as_remove_merge_hints(q, arq);
1051 as_del_arq_rb(ad, arq);
1052}
1053
1054
1055
1056
1057
1058static void as_remove_dispatched_request(request_queue_t *q, struct request *rq)
1059{
1060 struct as_rq *arq = RQ_DATA(rq);
1061 struct as_io_context *aic;
1062
1063 if (!arq) {
1064 WARN_ON(1);
1065 return;
1066 }
1067
1068 WARN_ON(arq->state != AS_RQ_DISPATCHED);
1069 WARN_ON(ON_RB(&arq->rb_node));
1070 if (arq->io_context && arq->io_context->aic) {
1071 aic = arq->io_context->aic;
1072 if (aic) {
1073 WARN_ON(!atomic_read(&aic->nr_dispatched));
1074 atomic_dec(&aic->nr_dispatched);
1075 }
1076 }
1077}
1078
1079
1080
1081
1082
1083
1084static void as_remove_request(request_queue_t *q, struct request *rq)
1085{
1086 struct as_rq *arq = RQ_DATA(rq);
1087
1088 if (unlikely(arq->state == AS_RQ_NEW))
1089 goto out;
1090
1091 if (ON_RB(&arq->rb_node)) {
1092 if (arq->state != AS_RQ_QUEUED) {
1093 printk("arq->state %d\n", arq->state);
1094 WARN_ON(1);
1095 goto out;
1096 }
1097
1098
1099
1100
1101
1102 WARN_ON(!list_empty(&rq->queuelist));
1103 as_remove_queued_request(q, rq);
1104 } else {
1105 if (arq->state != AS_RQ_DISPATCHED) {
1106 printk("arq->state %d\n", arq->state);
1107 WARN_ON(1);
1108 goto out;
1109 }
1110 as_remove_dispatched_request(q, rq);
1111 }
1112out:
1113 arq->state = AS_RQ_REMOVED;
1114}
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124static int as_fifo_expired(struct as_data *ad, int adir)
1125{
1126 struct as_rq *arq;
1127 long delta_jif;
1128
1129 delta_jif = jiffies - ad->last_check_fifo[adir];
1130 if (unlikely(delta_jif < 0))
1131 delta_jif = -delta_jif;
1132 if (delta_jif < ad->fifo_expire[adir])
1133 return 0;
1134
1135 ad->last_check_fifo[adir] = jiffies;
1136
1137 if (list_empty(&ad->fifo_list[adir]))
1138 return 0;
1139
1140 arq = list_entry_fifo(ad->fifo_list[adir].next);
1141
1142 return time_after(jiffies, arq->expires);
1143}
1144
1145
1146
1147
1148
1149static inline int as_batch_expired(struct as_data *ad)
1150{
1151 if (ad->changed_batch || ad->new_batch)
1152 return 0;
1153
1154 if (ad->batch_data_dir == REQ_SYNC)
1155
1156 return time_after(jiffies, ad->current_batch_expires);
1157
1158 return time_after(jiffies, ad->current_batch_expires)
1159 || ad->current_write_count == 0;
1160}
1161
1162
1163
1164
1165static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
1166{
1167 struct request *rq = arq->request;
1168 struct list_head *insert;
1169 const int data_dir = arq->is_sync;
1170
1171 BUG_ON(!ON_RB(&arq->rb_node));
1172
1173 as_antic_stop(ad);
1174 ad->antic_status = ANTIC_OFF;
1175
1176
1177
1178
1179
1180 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
1181
1182 if (data_dir == REQ_SYNC) {
1183
1184 copy_io_context(&ad->io_context, &arq->io_context);
1185 } else {
1186 if (ad->io_context) {
1187 put_io_context(ad->io_context);
1188 ad->io_context = NULL;
1189 }
1190
1191 if (ad->current_write_count != 0)
1192 ad->current_write_count--;
1193 }
1194 ad->ioc_finished = 0;
1195
1196 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1197
1198
1199
1200
1201 insert = ad->dispatch->prev;
1202
1203 while (!list_empty(&rq->queuelist)) {
1204 struct request *__rq = list_entry_rq(rq->queuelist.next);
1205 struct as_rq *__arq = RQ_DATA(__rq);
1206
1207 list_move_tail(&__rq->queuelist, ad->dispatch);
1208
1209 if (__arq->io_context && __arq->io_context->aic)
1210 atomic_inc(&__arq->io_context->aic->nr_dispatched);
1211
1212 WARN_ON(__arq->state != AS_RQ_QUEUED);
1213 __arq->state = AS_RQ_DISPATCHED;
1214
1215 ad->nr_dispatched++;
1216 }
1217
1218 as_remove_queued_request(ad->q, rq);
1219 WARN_ON(arq->state != AS_RQ_QUEUED);
1220
1221 list_add(&rq->queuelist, insert);
1222 arq->state = AS_RQ_DISPATCHED;
1223 if (arq->io_context && arq->io_context->aic)
1224 atomic_inc(&arq->io_context->aic->nr_dispatched);
1225 ad->nr_dispatched++;
1226}
1227
1228
1229
1230
1231
1232
1233static int as_dispatch_request(struct as_data *ad)
1234{
1235 struct as_rq *arq;
1236 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1237 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
1238
1239
1240 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1241 if (ad->current_write_count == 0 || !writes)
1242 ad->write_batch_idled = 1;
1243 }
1244
1245 if (!(reads || writes)
1246 || ad->antic_status == ANTIC_WAIT_REQ
1247 || ad->antic_status == ANTIC_WAIT_NEXT
1248 || ad->changed_batch)
1249 return 0;
1250
1251 if (!(reads && writes && as_batch_expired(ad)) ) {
1252
1253
1254
1255 arq = ad->next_arq[ad->batch_data_dir];
1256
1257 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1258 if (as_fifo_expired(ad, REQ_SYNC))
1259 goto fifo_expired;
1260
1261 if (as_can_anticipate(ad, arq)) {
1262 as_antic_waitreq(ad);
1263 return 0;
1264 }
1265 }
1266
1267 if (arq) {
1268
1269 if (reads && !writes)
1270 ad->current_batch_expires =
1271 jiffies + ad->batch_expire[REQ_SYNC];
1272 goto dispatch_request;
1273 }
1274 }
1275
1276
1277
1278
1279
1280
1281 if (reads) {
1282 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC]));
1283
1284 if (writes && ad->batch_data_dir == REQ_SYNC)
1285
1286
1287
1288 goto dispatch_writes;
1289
1290 if (ad->batch_data_dir == REQ_ASYNC) {
1291 WARN_ON(ad->new_batch);
1292 ad->changed_batch = 1;
1293 }
1294 ad->batch_data_dir = REQ_SYNC;
1295 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1296 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1297 goto dispatch_request;
1298 }
1299
1300
1301
1302
1303
1304 if (writes) {
1305dispatch_writes:
1306 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC]));
1307
1308 if (ad->batch_data_dir == REQ_SYNC) {
1309 ad->changed_batch = 1;
1310
1311
1312
1313
1314
1315
1316 ad->new_batch = 0;
1317 }
1318 ad->batch_data_dir = REQ_ASYNC;
1319 ad->current_write_count = ad->write_batch_count;
1320 ad->write_batch_idled = 0;
1321 arq = ad->next_arq[ad->batch_data_dir];
1322 goto dispatch_request;
1323 }
1324
1325 BUG();
1326 return 0;
1327
1328dispatch_request:
1329
1330
1331
1332
1333 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1334fifo_expired:
1335 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1336 BUG_ON(arq == NULL);
1337 }
1338
1339 if (ad->changed_batch) {
1340 WARN_ON(ad->new_batch);
1341
1342 if (ad->nr_dispatched)
1343 return 0;
1344
1345 if (ad->batch_data_dir == REQ_ASYNC)
1346 ad->current_batch_expires = jiffies +
1347 ad->batch_expire[REQ_ASYNC];
1348 else
1349 ad->new_batch = 1;
1350
1351 ad->changed_batch = 0;
1352 }
1353
1354
1355
1356
1357 as_move_to_dispatch(ad, arq);
1358
1359 return 1;
1360}
1361
1362static struct request *as_next_request(request_queue_t *q)
1363{
1364 struct as_data *ad = q->elevator->elevator_data;
1365 struct request *rq = NULL;
1366
1367
1368
1369
1370 if (!list_empty(ad->dispatch) || as_dispatch_request(ad))
1371 rq = list_entry_rq(ad->dispatch->next);
1372
1373 return rq;
1374}
1375
1376
1377
1378
1379static inline void
1380as_add_aliased_request(struct as_data *ad, struct as_rq *arq, struct as_rq *alias)
1381{
1382 struct request *req = arq->request;
1383 struct list_head *insert = alias->request->queuelist.prev;
1384
1385
1386
1387
1388 while (!list_empty(&req->queuelist)) {
1389 struct request *__rq = list_entry_rq(req->queuelist.next);
1390 struct as_rq *__arq = RQ_DATA(__rq);
1391
1392 list_move_tail(&__rq->queuelist, &alias->request->queuelist);
1393
1394 WARN_ON(__arq->state != AS_RQ_QUEUED);
1395 }
1396
1397
1398
1399
1400
1401
1402 list_add(&arq->request->queuelist, insert);
1403
1404
1405
1406
1407 as_remove_merge_hints(ad->q, arq);
1408}
1409
1410
1411
1412
1413static void as_add_request(struct as_data *ad, struct as_rq *arq)
1414{
1415 struct as_rq *alias;
1416 int data_dir;
1417
1418 if (rq_data_dir(arq->request) == READ
1419 || current->flags&PF_SYNCWRITE)
1420 arq->is_sync = 1;
1421 else
1422 arq->is_sync = 0;
1423 data_dir = arq->is_sync;
1424
1425 arq->io_context = as_get_io_context();
1426
1427 if (arq->io_context) {
1428 as_update_iohist(ad, arq->io_context->aic, arq->request);
1429 atomic_inc(&arq->io_context->aic->nr_queued);
1430 }
1431
1432 alias = as_add_arq_rb(ad, arq);
1433 if (!alias) {
1434
1435
1436
1437 arq->expires = jiffies + ad->fifo_expire[data_dir];
1438 list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
1439
1440 if (rq_mergeable(arq->request)) {
1441 as_add_arq_hash(ad, arq);
1442
1443 if (!ad->q->last_merge)
1444 ad->q->last_merge = arq->request;
1445 }
1446 as_update_arq(ad, arq);
1447
1448 } else {
1449 as_add_aliased_request(ad, arq, alias);
1450
1451
1452
1453
1454
1455
1456 if (ad->antic_status == ANTIC_WAIT_REQ
1457 || ad->antic_status == ANTIC_WAIT_NEXT) {
1458 if (as_can_break_anticipation(ad, arq))
1459 as_antic_stop(ad);
1460 }
1461 }
1462
1463 arq->state = AS_RQ_QUEUED;
1464}
1465
1466
1467
1468
1469
1470static void as_requeue_request(request_queue_t *q, struct request *rq)
1471{
1472 struct as_data *ad = q->elevator->elevator_data;
1473 struct as_rq *arq = RQ_DATA(rq);
1474
1475 if (arq) {
1476 if (arq->state != AS_RQ_REMOVED) {
1477 printk("arq->state %d\n", arq->state);
1478 WARN_ON(1);
1479 }
1480
1481 arq->state = AS_RQ_DISPATCHED;
1482 if (arq->io_context && arq->io_context->aic)
1483 atomic_inc(&arq->io_context->aic->nr_dispatched);
1484 } else
1485 WARN_ON(blk_fs_request(rq)
1486 && (!(rq->flags & (REQ_HARDBARRIER|REQ_SOFTBARRIER))) );
1487
1488 list_add(&rq->queuelist, ad->dispatch);
1489
1490
1491 as_antic_stop(ad);
1492}
1493
1494
1495
1496
1497
1498
1499
1500static void as_account_queued_request(struct as_data *ad, struct request *rq)
1501{
1502 if (blk_fs_request(rq)) {
1503 struct as_rq *arq = RQ_DATA(rq);
1504 arq->state = AS_RQ_DISPATCHED;
1505 ad->nr_dispatched++;
1506 }
1507}
1508
1509static void
1510as_insert_request(request_queue_t *q, struct request *rq, int where)
1511{
1512 struct as_data *ad = q->elevator->elevator_data;
1513 struct as_rq *arq = RQ_DATA(rq);
1514
1515 if (arq) {
1516 if (arq->state != AS_RQ_PRESCHED) {
1517 printk("arq->state: %d\n", arq->state);
1518 WARN_ON(1);
1519 }
1520 arq->state = AS_RQ_NEW;
1521 }
1522
1523
1524 if (unlikely(rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)
1525 && where == ELEVATOR_INSERT_SORT)) {
1526 WARN_ON(1);
1527 where = ELEVATOR_INSERT_BACK;
1528 }
1529
1530 switch (where) {
1531 case ELEVATOR_INSERT_BACK:
1532 while (ad->next_arq[REQ_SYNC])
1533 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1534
1535 while (ad->next_arq[REQ_ASYNC])
1536 as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
1537
1538 list_add_tail(&rq->queuelist, ad->dispatch);
1539 as_account_queued_request(ad, rq);
1540 as_antic_stop(ad);
1541 break;
1542 case ELEVATOR_INSERT_FRONT:
1543 list_add(&rq->queuelist, ad->dispatch);
1544 as_account_queued_request(ad, rq);
1545 as_antic_stop(ad);
1546 break;
1547 case ELEVATOR_INSERT_SORT:
1548 BUG_ON(!blk_fs_request(rq));
1549 as_add_request(ad, arq);
1550 break;
1551 default:
1552 BUG();
1553 return;
1554 }
1555}
1556
1557
1558
1559
1560
1561
1562
1563static int as_queue_empty(request_queue_t *q)
1564{
1565 struct as_data *ad = q->elevator->elevator_data;
1566
1567 if (!list_empty(&ad->fifo_list[REQ_ASYNC])
1568 || !list_empty(&ad->fifo_list[REQ_SYNC])
1569 || !list_empty(ad->dispatch))
1570 return 0;
1571
1572 return 1;
1573}
1574
1575static struct request *
1576as_former_request(request_queue_t *q, struct request *rq)
1577{
1578 struct as_rq *arq = RQ_DATA(rq);
1579 struct rb_node *rbprev = rb_prev(&arq->rb_node);
1580 struct request *ret = NULL;
1581
1582 if (rbprev)
1583 ret = rb_entry_arq(rbprev)->request;
1584
1585 return ret;
1586}
1587
1588static struct request *
1589as_latter_request(request_queue_t *q, struct request *rq)
1590{
1591 struct as_rq *arq = RQ_DATA(rq);
1592 struct rb_node *rbnext = rb_next(&arq->rb_node);
1593 struct request *ret = NULL;
1594
1595 if (rbnext)
1596 ret = rb_entry_arq(rbnext)->request;
1597
1598 return ret;
1599}
1600
1601static int
1602as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1603{
1604 struct as_data *ad = q->elevator->elevator_data;
1605 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1606 struct request *__rq;
1607 int ret;
1608
1609
1610
1611
1612 ret = elv_try_last_merge(q, bio);
1613 if (ret != ELEVATOR_NO_MERGE) {
1614 __rq = q->last_merge;
1615 goto out_insert;
1616 }
1617
1618
1619
1620
1621 __rq = as_find_arq_hash(ad, bio->bi_sector);
1622 if (__rq) {
1623 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
1624
1625 if (elv_rq_merge_ok(__rq, bio)) {
1626 ret = ELEVATOR_BACK_MERGE;
1627 goto out;
1628 }
1629 }
1630
1631
1632
1633
1634 __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
1635 if (__rq) {
1636 BUG_ON(rb_key != rq_rb_key(__rq));
1637
1638 if (elv_rq_merge_ok(__rq, bio)) {
1639 ret = ELEVATOR_FRONT_MERGE;
1640 goto out;
1641 }
1642 }
1643
1644 return ELEVATOR_NO_MERGE;
1645out:
1646 if (rq_mergeable(__rq))
1647 q->last_merge = __rq;
1648out_insert:
1649 if (ret) {
1650 if (rq_mergeable(__rq))
1651 as_hot_arq_hash(ad, RQ_DATA(__rq));
1652 }
1653 *req = __rq;
1654 return ret;
1655}
1656
1657static void as_merged_request(request_queue_t *q, struct request *req)
1658{
1659 struct as_data *ad = q->elevator->elevator_data;
1660 struct as_rq *arq = RQ_DATA(req);
1661
1662
1663
1664
1665 as_del_arq_hash(arq);
1666 as_add_arq_hash(ad, arq);
1667
1668
1669
1670
1671 if (rq_rb_key(req) != arq->rb_key) {
1672 struct as_rq *alias, *next_arq = NULL;
1673
1674 if (ad->next_arq[arq->is_sync] == arq)
1675 next_arq = as_find_next_arq(ad, arq);
1676
1677
1678
1679
1680
1681
1682 as_del_arq_rb(ad, arq);
1683 if ((alias = as_add_arq_rb(ad, arq)) ) {
1684 list_del_init(&arq->fifo);
1685 as_add_aliased_request(ad, arq, alias);
1686 if (next_arq)
1687 ad->next_arq[arq->is_sync] = next_arq;
1688 }
1689
1690
1691
1692
1693
1694 }
1695
1696 if (arq->on_hash)
1697 q->last_merge = req;
1698}
1699
1700static void
1701as_merged_requests(request_queue_t *q, struct request *req,
1702 struct request *next)
1703{
1704 struct as_data *ad = q->elevator->elevator_data;
1705 struct as_rq *arq = RQ_DATA(req);
1706 struct as_rq *anext = RQ_DATA(next);
1707
1708 BUG_ON(!arq);
1709 BUG_ON(!anext);
1710
1711
1712
1713
1714
1715 as_del_arq_hash(arq);
1716 as_add_arq_hash(ad, arq);
1717
1718 if (rq_rb_key(req) != arq->rb_key) {
1719 struct as_rq *alias, *next_arq = NULL;
1720
1721 if (ad->next_arq[arq->is_sync] == arq)
1722 next_arq = as_find_next_arq(ad, arq);
1723
1724 as_del_arq_rb(ad, arq);
1725 if ((alias = as_add_arq_rb(ad, arq)) ) {
1726 list_del_init(&arq->fifo);
1727 as_add_aliased_request(ad, arq, alias);
1728 if (next_arq)
1729 ad->next_arq[arq->is_sync] = next_arq;
1730 }
1731 }
1732
1733
1734
1735
1736
1737 if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
1738 if (time_before(anext->expires, arq->expires)) {
1739 list_move(&arq->fifo, &anext->fifo);
1740 arq->expires = anext->expires;
1741
1742
1743
1744
1745 swap_io_context(&arq->io_context, &anext->io_context);
1746 }
1747 }
1748
1749
1750
1751
1752 while (!list_empty(&next->queuelist)) {
1753 struct request *__rq = list_entry_rq(next->queuelist.next);
1754 struct as_rq *__arq = RQ_DATA(__rq);
1755
1756 list_move_tail(&__rq->queuelist, &req->queuelist);
1757
1758 WARN_ON(__arq->state != AS_RQ_QUEUED);
1759 }
1760
1761
1762
1763
1764 as_remove_queued_request(q, next);
1765
1766 anext->state = AS_RQ_MERGED;
1767}
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778static void as_work_handler(void *data)
1779{
1780 struct request_queue *q = data;
1781 unsigned long flags;
1782
1783 spin_lock_irqsave(q->queue_lock, flags);
1784 if (as_next_request(q))
1785 q->request_fn(q);
1786 spin_unlock_irqrestore(q->queue_lock, flags);
1787}
1788
1789static void as_put_request(request_queue_t *q, struct request *rq)
1790{
1791 struct as_data *ad = q->elevator->elevator_data;
1792 struct as_rq *arq = RQ_DATA(rq);
1793
1794 if (!arq) {
1795 WARN_ON(1);
1796 return;
1797 }
1798
1799 if (arq->state != AS_RQ_POSTSCHED && arq->state != AS_RQ_PRESCHED) {
1800 printk("arq->state %d\n", arq->state);
1801 WARN_ON(1);
1802 }
1803
1804 mempool_free(arq, ad->arq_pool);
1805 rq->elevator_private = NULL;
1806}
1807
1808static int as_set_request(request_queue_t *q, struct request *rq, int gfp_mask)
1809{
1810 struct as_data *ad = q->elevator->elevator_data;
1811 struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1812
1813 if (arq) {
1814 memset(arq, 0, sizeof(*arq));
1815 RB_CLEAR(&arq->rb_node);
1816 arq->request = rq;
1817 arq->state = AS_RQ_PRESCHED;
1818 arq->io_context = NULL;
1819 INIT_LIST_HEAD(&arq->hash);
1820 arq->on_hash = 0;
1821 INIT_LIST_HEAD(&arq->fifo);
1822 rq->elevator_private = arq;
1823 return 0;
1824 }
1825
1826 return 1;
1827}
1828
1829static int as_may_queue(request_queue_t *q, int rw)
1830{
1831 int ret = ELV_MQUEUE_MAY;
1832 struct as_data *ad = q->elevator->elevator_data;
1833 struct io_context *ioc;
1834 if (ad->antic_status == ANTIC_WAIT_REQ ||
1835 ad->antic_status == ANTIC_WAIT_NEXT) {
1836 ioc = as_get_io_context();
1837 if (ad->io_context == ioc)
1838 ret = ELV_MQUEUE_MUST;
1839 put_io_context(ioc);
1840 }
1841
1842 return ret;
1843}
1844
1845static void as_exit_queue(elevator_t *e)
1846{
1847 struct as_data *ad = e->elevator_data;
1848
1849 del_timer_sync(&ad->antic_timer);
1850 kblockd_flush();
1851
1852 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1853 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1854
1855 mempool_destroy(ad->arq_pool);
1856 put_io_context(ad->io_context);
1857 kfree(ad->hash);
1858 kfree(ad);
1859}
1860
1861
1862
1863
1864
1865static int as_init_queue(request_queue_t *q, elevator_t *e)
1866{
1867 struct as_data *ad;
1868 int i;
1869
1870 if (!arq_pool)
1871 return -ENOMEM;
1872
1873 ad = kmalloc(sizeof(*ad), GFP_KERNEL);
1874 if (!ad)
1875 return -ENOMEM;
1876 memset(ad, 0, sizeof(*ad));
1877
1878 ad->q = q;
1879
1880 ad->hash = kmalloc(sizeof(struct list_head)*AS_HASH_ENTRIES,GFP_KERNEL);
1881 if (!ad->hash) {
1882 kfree(ad);
1883 return -ENOMEM;
1884 }
1885
1886 ad->arq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, arq_pool);
1887 if (!ad->arq_pool) {
1888 kfree(ad->hash);
1889 kfree(ad);
1890 return -ENOMEM;
1891 }
1892
1893
1894 ad->antic_timer.function = as_antic_timeout;
1895 ad->antic_timer.data = (unsigned long)q;
1896 init_timer(&ad->antic_timer);
1897 INIT_WORK(&ad->antic_work, as_work_handler, q);
1898
1899 for (i = 0; i < AS_HASH_ENTRIES; i++)
1900 INIT_LIST_HEAD(&ad->hash[i]);
1901
1902 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1903 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1904 ad->sort_list[REQ_SYNC] = RB_ROOT;
1905 ad->sort_list[REQ_ASYNC] = RB_ROOT;
1906 ad->dispatch = &q->queue_head;
1907 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1908 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1909 ad->antic_expire = default_antic_expire;
1910 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1911 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1912 e->elevator_data = ad;
1913
1914 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1915 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1916 if (ad->write_batch_count < 2)
1917 ad->write_batch_count = 2;
1918
1919 return 0;
1920}
1921
1922
1923
1924
1925struct as_fs_entry {
1926 struct attribute attr;
1927 ssize_t (*show)(struct as_data *, char *);
1928 ssize_t (*store)(struct as_data *, const char *, size_t);
1929};
1930
1931static ssize_t
1932as_var_show(unsigned int var, char *page)
1933{
1934 var = (var * 1000) / HZ;
1935 return sprintf(page, "%d\n", var);
1936}
1937
1938static ssize_t
1939as_var_store(unsigned long *var, const char *page, size_t count)
1940{
1941 unsigned long tmp;
1942 char *p = (char *) page;
1943
1944 tmp = simple_strtoul(p, &p, 10);
1945 if (tmp != 0) {
1946 tmp = (tmp * HZ) / 1000;
1947 if (tmp == 0)
1948 tmp = 1;
1949 }
1950 *var = tmp;
1951 return count;
1952}
1953
1954static ssize_t as_est_show(struct as_data *ad, char *page)
1955{
1956 int pos = 0;
1957
1958 pos += sprintf(page+pos, "%lu %% exit probability\n", 100*ad->exit_prob/256);
1959 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
1960 pos += sprintf(page+pos, "%llu sectors new seek distance\n", (unsigned long long)ad->new_seek_mean);
1961
1962 return pos;
1963}
1964
1965#define SHOW_FUNCTION(__FUNC, __VAR) \
1966static ssize_t __FUNC(struct as_data *ad, char *page) \
1967{ \
1968 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1969}
1970SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
1971SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
1972SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
1973SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
1974SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
1975#undef SHOW_FUNCTION
1976
1977#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1978static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
1979{ \
1980 int ret = as_var_store(__PTR, (page), count); \
1981 if (*(__PTR) < (MIN)) \
1982 *(__PTR) = (MIN); \
1983 else if (*(__PTR) > (MAX)) \
1984 *(__PTR) = (MAX); \
1985 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1986 return ret; \
1987}
1988STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1989STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1990STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
1991STORE_FUNCTION(as_read_batchexpire_store,
1992 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
1993STORE_FUNCTION(as_write_batchexpire_store,
1994 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1995#undef STORE_FUNCTION
1996
1997static struct as_fs_entry as_est_entry = {
1998 .attr = {.name = "est_time", .mode = S_IRUGO },
1999 .show = as_est_show,
2000};
2001static struct as_fs_entry as_readexpire_entry = {
2002 .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
2003 .show = as_readexpire_show,
2004 .store = as_readexpire_store,
2005};
2006static struct as_fs_entry as_writeexpire_entry = {
2007 .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
2008 .show = as_writeexpire_show,
2009 .store = as_writeexpire_store,
2010};
2011static struct as_fs_entry as_anticexpire_entry = {
2012 .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
2013 .show = as_anticexpire_show,
2014 .store = as_anticexpire_store,
2015};
2016static struct as_fs_entry as_read_batchexpire_entry = {
2017 .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
2018 .show = as_read_batchexpire_show,
2019 .store = as_read_batchexpire_store,
2020};
2021static struct as_fs_entry as_write_batchexpire_entry = {
2022 .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
2023 .show = as_write_batchexpire_show,
2024 .store = as_write_batchexpire_store,
2025};
2026
2027static struct attribute *default_attrs[] = {
2028 &as_est_entry.attr,
2029 &as_readexpire_entry.attr,
2030 &as_writeexpire_entry.attr,
2031 &as_anticexpire_entry.attr,
2032 &as_read_batchexpire_entry.attr,
2033 &as_write_batchexpire_entry.attr,
2034 NULL,
2035};
2036
2037#define to_as(atr) container_of((atr), struct as_fs_entry, attr)
2038
2039static ssize_t
2040as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2041{
2042 elevator_t *e = container_of(kobj, elevator_t, kobj);
2043 struct as_fs_entry *entry = to_as(attr);
2044
2045 if (!entry->show)
2046 return 0;
2047
2048 return entry->show(e->elevator_data, page);
2049}
2050
2051static ssize_t
2052as_attr_store(struct kobject *kobj, struct attribute *attr,
2053 const char *page, size_t length)
2054{
2055 elevator_t *e = container_of(kobj, elevator_t, kobj);
2056 struct as_fs_entry *entry = to_as(attr);
2057
2058 if (!entry->store)
2059 return -EINVAL;
2060
2061 return entry->store(e->elevator_data, page, length);
2062}
2063
2064static struct sysfs_ops as_sysfs_ops = {
2065 .show = as_attr_show,
2066 .store = as_attr_store,
2067};
2068
2069static struct kobj_type as_ktype = {
2070 .sysfs_ops = &as_sysfs_ops,
2071 .default_attrs = default_attrs,
2072};
2073
2074static struct elevator_type iosched_as = {
2075 .ops = {
2076 .elevator_merge_fn = as_merge,
2077 .elevator_merged_fn = as_merged_request,
2078 .elevator_merge_req_fn = as_merged_requests,
2079 .elevator_next_req_fn = as_next_request,
2080 .elevator_add_req_fn = as_insert_request,
2081 .elevator_remove_req_fn = as_remove_request,
2082 .elevator_requeue_req_fn = as_requeue_request,
2083 .elevator_queue_empty_fn = as_queue_empty,
2084 .elevator_completed_req_fn = as_completed_request,
2085 .elevator_former_req_fn = as_former_request,
2086 .elevator_latter_req_fn = as_latter_request,
2087 .elevator_set_req_fn = as_set_request,
2088 .elevator_put_req_fn = as_put_request,
2089 .elevator_may_queue_fn = as_may_queue,
2090 .elevator_init_fn = as_init_queue,
2091 .elevator_exit_fn = as_exit_queue,
2092 },
2093
2094 .elevator_ktype = &as_ktype,
2095 .elevator_name = "anticipatory",
2096 .elevator_owner = THIS_MODULE,
2097};
2098
2099static int __init as_init(void)
2100{
2101 int ret;
2102
2103 arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
2104 0, 0, NULL, NULL);
2105 if (!arq_pool)
2106 return -ENOMEM;
2107
2108 ret = elv_register(&iosched_as);
2109 if (!ret) {
2110
2111
2112
2113
2114
2115 __module_get(THIS_MODULE);
2116 return 0;
2117 }
2118
2119 kmem_cache_destroy(arq_pool);
2120 return ret;
2121}
2122
2123static void __exit as_exit(void)
2124{
2125 kmem_cache_destroy(arq_pool);
2126 elv_unregister(&iosched_as);
2127}
2128
2129module_init(as_init);
2130module_exit(as_exit);
2131
2132MODULE_AUTHOR("Nick Piggin");
2133MODULE_LICENSE("GPL");
2134MODULE_DESCRIPTION("anticipatory IO scheduler");
2135