[PATCH] Fix cfq_find_next_crq()
[linux-2.6-block.git] / drivers / block / cfq-iosched.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/block/cfq-iosched.c
3 *
4 * CFQ, or complete fairness queueing, disk scheduler.
5 *
6 * Based on ideas from a previously unfinished io
7 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
8 *
9 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
10 */
11#include <linux/kernel.h>
12#include <linux/fs.h>
13#include <linux/blkdev.h>
14#include <linux/elevator.h>
15#include <linux/bio.h>
16#include <linux/config.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/compiler.h>
21#include <linux/hash.h>
22#include <linux/rbtree.h>
23#include <linux/mempool.h>
22e2c507
JA
24#include <linux/ioprio.h>
25#include <linux/writeback.h>
1da177e4
LT
26
27/*
28 * tunables
29 */
30static int cfq_quantum = 4; /* max queue in one round of service */
31static int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
22e2c507 32static int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
1da177e4
LT
33static int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
34static int cfq_back_penalty = 2; /* penalty of a backwards seek */
35
22e2c507
JA
36static int cfq_slice_sync = HZ / 10;
37static int cfq_slice_async = HZ / 50;
38static int cfq_slice_async_rq = 2;
39static int cfq_slice_idle = HZ / 50;
40
41#define CFQ_IDLE_GRACE (HZ / 10)
42#define CFQ_SLICE_SCALE (5)
43
44#define CFQ_KEY_ASYNC (0)
45
46/*
47 * disable queueing at the driver/hardware level
48 */
49static int cfq_max_depth = 1;
50
1da177e4
LT
51/*
52 * for the hash of cfqq inside the cfqd
53 */
54#define CFQ_QHASH_SHIFT 6
55#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
56#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
57
58/*
59 * for the hash of crq inside the cfqq
60 */
61#define CFQ_MHASH_SHIFT 6
62#define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
63#define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
64#define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
65#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
66#define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
67
68#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
22e2c507 69#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
1da177e4
LT
70
71#define RQ_DATA(rq) (rq)->elevator_private
72
73/*
74 * rb-tree defines
75 */
76#define RB_NONE (2)
77#define RB_EMPTY(node) ((node)->rb_node == NULL)
78#define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
79#define RB_CLEAR(node) do { \
80 (node)->rb_parent = NULL; \
81 RB_CLEAR_COLOR((node)); \
82 (node)->rb_right = NULL; \
83 (node)->rb_left = NULL; \
84} while (0)
85#define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
86#define ON_RB(node) ((node)->rb_color != RB_NONE)
87#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
88#define rq_rb_key(rq) (rq)->sector
89
1da177e4
LT
90static kmem_cache_t *crq_pool;
91static kmem_cache_t *cfq_pool;
92static kmem_cache_t *cfq_ioc_pool;
93
22e2c507
JA
94#define CFQ_PRIO_LISTS IOPRIO_BE_NR
95#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
96#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
97#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
98
99#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
100
101/*
102 * Per block device queue structure
103 */
1da177e4 104struct cfq_data {
22e2c507
JA
105 atomic_t ref;
106 request_queue_t *queue;
107
108 /*
109 * rr list of queues with requests and the count of them
110 */
111 struct list_head rr_list[CFQ_PRIO_LISTS];
112 struct list_head busy_rr;
113 struct list_head cur_rr;
114 struct list_head idle_rr;
115 unsigned int busy_queues;
116
117 /*
118 * non-ordered list of empty cfqq's
119 */
1da177e4
LT
120 struct list_head empty_list;
121
22e2c507
JA
122 /*
123 * cfqq lookup hash
124 */
1da177e4 125 struct hlist_head *cfq_hash;
1da177e4 126
22e2c507
JA
127 /*
128 * global crq hash for all queues
129 */
130 struct hlist_head *crq_hash;
1da177e4
LT
131
132 unsigned int max_queued;
133
22e2c507 134 mempool_t *crq_pool;
1da177e4 135
22e2c507 136 int rq_in_driver;
1da177e4 137
22e2c507
JA
138 /*
139 * schedule slice state info
140 */
141 /*
142 * idle window management
143 */
144 struct timer_list idle_slice_timer;
145 struct work_struct unplug_work;
1da177e4 146
22e2c507
JA
147 struct cfq_queue *active_queue;
148 struct cfq_io_context *active_cic;
149 int cur_prio, cur_end_prio;
150 unsigned int dispatch_slice;
151
152 struct timer_list idle_class_timer;
1da177e4
LT
153
154 sector_t last_sector;
22e2c507 155 unsigned long last_end_request;
1da177e4 156
22e2c507 157 unsigned int rq_starved;
1da177e4
LT
158
159 /*
160 * tunables, see top of file
161 */
162 unsigned int cfq_quantum;
163 unsigned int cfq_queued;
22e2c507 164 unsigned int cfq_fifo_expire[2];
1da177e4
LT
165 unsigned int cfq_back_penalty;
166 unsigned int cfq_back_max;
22e2c507
JA
167 unsigned int cfq_slice[2];
168 unsigned int cfq_slice_async_rq;
169 unsigned int cfq_slice_idle;
170 unsigned int cfq_max_depth;
1da177e4
LT
171};
172
22e2c507
JA
173/*
174 * Per process-grouping structure
175 */
1da177e4
LT
176struct cfq_queue {
177 /* reference count */
178 atomic_t ref;
179 /* parent cfq_data */
180 struct cfq_data *cfqd;
22e2c507 181 /* cfqq lookup hash */
1da177e4
LT
182 struct hlist_node cfq_hash;
183 /* hash key */
22e2c507 184 unsigned int key;
1da177e4
LT
185 /* on either rr or empty list of cfqd */
186 struct list_head cfq_list;
187 /* sorted list of pending requests */
188 struct rb_root sort_list;
189 /* if fifo isn't expired, next request to serve */
190 struct cfq_rq *next_crq;
191 /* requests queued in sort_list */
192 int queued[2];
193 /* currently allocated requests */
194 int allocated[2];
195 /* fifo list of requests in sort_list */
22e2c507 196 struct list_head fifo;
1da177e4 197
22e2c507
JA
198 unsigned long slice_start;
199 unsigned long slice_end;
200 unsigned long slice_left;
201 unsigned long service_last;
1da177e4
LT
202
203 /* number of requests that have been handed to the driver */
204 int in_flight;
22e2c507
JA
205
206 /* io prio of this group */
207 unsigned short ioprio, org_ioprio;
208 unsigned short ioprio_class, org_ioprio_class;
209
210 /* whether queue is on rr (or empty) list */
211 unsigned on_rr : 1;
212 /* idle slice, waiting for new request submission */
213 unsigned wait_request : 1;
214 /* set when wait_request gets set, reset on first rq alloc */
215 unsigned must_alloc : 1;
216 /* only gets one must_alloc per slice */
217 unsigned must_alloc_slice : 1;
218 /* idle slice, request added, now waiting to dispatch it */
219 unsigned must_dispatch : 1;
220 /* fifo expire per-slice */
221 unsigned fifo_expire : 1;
222
223 unsigned idle_window : 1;
224 unsigned prio_changed : 1;
1da177e4
LT
225};
226
227struct cfq_rq {
228 struct rb_node rb_node;
229 sector_t rb_key;
230 struct request *request;
231 struct hlist_node hash;
232
233 struct cfq_queue *cfq_queue;
234 struct cfq_io_context *io_context;
235
22e2c507
JA
236 unsigned in_flight : 1;
237 unsigned accounted : 1;
238 unsigned is_sync : 1;
239 unsigned requeued : 1;
1da177e4
LT
240};
241
22e2c507 242static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int);
1da177e4 243static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *);
1da177e4
LT
244static void cfq_put_cfqd(struct cfq_data *cfqd);
245
22e2c507 246#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
1da177e4
LT
247
248/*
249 * lots of deadline iosched dupes, can be abstracted later...
250 */
251static inline void cfq_del_crq_hash(struct cfq_rq *crq)
252{
253 hlist_del_init(&crq->hash);
254}
255
256static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
257{
258 cfq_del_crq_hash(crq);
259
260 if (q->last_merge == crq->request)
261 q->last_merge = NULL;
1da177e4
LT
262}
263
264static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
265{
266 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
267
1da177e4
LT
268 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
269}
270
271static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
272{
273 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
274 struct hlist_node *entry, *next;
275
276 hlist_for_each_safe(entry, next, hash_list) {
277 struct cfq_rq *crq = list_entry_hash(entry);
278 struct request *__rq = crq->request;
279
1da177e4
LT
280 if (!rq_mergeable(__rq)) {
281 cfq_del_crq_hash(crq);
282 continue;
283 }
284
285 if (rq_hash_key(__rq) == offset)
286 return __rq;
287 }
288
289 return NULL;
290}
291
292/*
293 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
294 * We choose the request that is closest to the head right now. Distance
295 * behind the head are penalized and only allowed to a certain extent.
296 */
297static struct cfq_rq *
298cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
299{
300 sector_t last, s1, s2, d1 = 0, d2 = 0;
301 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
302 unsigned long back_max;
303
304 if (crq1 == NULL || crq1 == crq2)
305 return crq2;
306 if (crq2 == NULL)
307 return crq1;
22e2c507
JA
308 if (crq1->requeued)
309 return crq1;
310 if (crq2->requeued)
311 return crq2;
1da177e4
LT
312
313 s1 = crq1->request->sector;
314 s2 = crq2->request->sector;
315
316 last = cfqd->last_sector;
317
1da177e4
LT
318 /*
319 * by definition, 1KiB is 2 sectors
320 */
321 back_max = cfqd->cfq_back_max * 2;
322
323 /*
324 * Strict one way elevator _except_ in the case where we allow
325 * short backward seeks which are biased as twice the cost of a
326 * similar forward seek.
327 */
328 if (s1 >= last)
329 d1 = s1 - last;
330 else if (s1 + back_max >= last)
331 d1 = (last - s1) * cfqd->cfq_back_penalty;
332 else
333 r1_wrap = 1;
334
335 if (s2 >= last)
336 d2 = s2 - last;
337 else if (s2 + back_max >= last)
338 d2 = (last - s2) * cfqd->cfq_back_penalty;
339 else
340 r2_wrap = 1;
341
342 /* Found required data */
343 if (!r1_wrap && r2_wrap)
344 return crq1;
345 else if (!r2_wrap && r1_wrap)
346 return crq2;
347 else if (r1_wrap && r2_wrap) {
348 /* both behind the head */
349 if (s1 <= s2)
350 return crq1;
351 else
352 return crq2;
353 }
354
355 /* Both requests in front of the head */
356 if (d1 < d2)
357 return crq1;
358 else if (d2 < d1)
359 return crq2;
360 else {
361 if (s1 >= s2)
362 return crq1;
363 else
364 return crq2;
365 }
366}
367
368/*
369 * would be nice to take fifo expire time into account as well
370 */
371static struct cfq_rq *
372cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
373 struct cfq_rq *last)
374{
375 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
376 struct rb_node *rbnext, *rbprev;
377
3d25f356 378 rbnext = NULL;
22e2c507
JA
379 if (ON_RB(&last->rb_node))
380 rbnext = rb_next(&last->rb_node);
3d25f356 381 if (!rbnext) {
1da177e4 382 rbnext = rb_first(&cfqq->sort_list);
22e2c507
JA
383 if (rbnext == &last->rb_node)
384 rbnext = NULL;
385 }
1da177e4
LT
386
387 rbprev = rb_prev(&last->rb_node);
388
389 if (rbprev)
390 crq_prev = rb_entry_crq(rbprev);
391 if (rbnext)
392 crq_next = rb_entry_crq(rbnext);
393
394 return cfq_choose_req(cfqd, crq_next, crq_prev);
395}
396
397static void cfq_update_next_crq(struct cfq_rq *crq)
398{
399 struct cfq_queue *cfqq = crq->cfq_queue;
400
401 if (cfqq->next_crq == crq)
402 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
403}
404
22e2c507 405static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
1da177e4 406{
22e2c507
JA
407 struct cfq_data *cfqd = cfqq->cfqd;
408 struct list_head *list, *entry;
1da177e4 409
22e2c507 410 BUG_ON(!cfqq->on_rr);
1da177e4 411
22e2c507 412 list_del(&cfqq->cfq_list);
1da177e4 413
22e2c507
JA
414 if (cfq_class_rt(cfqq))
415 list = &cfqd->cur_rr;
416 else if (cfq_class_idle(cfqq))
417 list = &cfqd->idle_rr;
418 else {
419 /*
420 * if cfqq has requests in flight, don't allow it to be
421 * found in cfq_set_active_queue before it has finished them.
422 * this is done to increase fairness between a process that
423 * has lots of io pending vs one that only generates one
424 * sporadically or synchronously
425 */
426 if (cfqq->in_flight)
427 list = &cfqd->busy_rr;
428 else
429 list = &cfqd->rr_list[cfqq->ioprio];
1da177e4
LT
430 }
431
22e2c507
JA
432 /*
433 * if queue was preempted, just add to front to be fair. busy_rr
434 * isn't sorted.
435 */
436 if (preempted || list == &cfqd->busy_rr) {
437 list_add(&cfqq->cfq_list, list);
1da177e4 438 return;
22e2c507 439 }
1da177e4
LT
440
441 /*
22e2c507 442 * sort by when queue was last serviced
1da177e4 443 */
22e2c507
JA
444 entry = list;
445 while ((entry = entry->prev) != list) {
1da177e4
LT
446 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
447
22e2c507
JA
448 if (!__cfqq->service_last)
449 break;
450 if (time_before(__cfqq->service_last, cfqq->service_last))
1da177e4 451 break;
1da177e4
LT
452 }
453
454 list_add(&cfqq->cfq_list, entry);
455}
456
457/*
458 * add to busy list of queues for service, trying to be fair in ordering
22e2c507 459 * the pending list according to last request service
1da177e4
LT
460 */
461static inline void
22e2c507 462cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
1da177e4 463{
22e2c507 464 BUG_ON(cfqq->on_rr);
1da177e4
LT
465 cfqq->on_rr = 1;
466 cfqd->busy_queues++;
467
22e2c507 468 cfq_resort_rr_list(cfqq, requeue);
1da177e4
LT
469}
470
471static inline void
472cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
473{
22e2c507 474 BUG_ON(!cfqq->on_rr);
1da177e4 475 cfqq->on_rr = 0;
22e2c507 476 list_move(&cfqq->cfq_list, &cfqd->empty_list);
1da177e4
LT
477
478 BUG_ON(!cfqd->busy_queues);
479 cfqd->busy_queues--;
480}
481
482/*
483 * rb tree support functions
484 */
485static inline void cfq_del_crq_rb(struct cfq_rq *crq)
486{
487 struct cfq_queue *cfqq = crq->cfq_queue;
488
489 if (ON_RB(&crq->rb_node)) {
490 struct cfq_data *cfqd = cfqq->cfqd;
22e2c507 491 const int sync = crq->is_sync;
1da177e4 492
22e2c507
JA
493 BUG_ON(!cfqq->queued[sync]);
494 cfqq->queued[sync]--;
1da177e4
LT
495
496 cfq_update_next_crq(crq);
497
1da177e4
LT
498 rb_erase(&crq->rb_node, &cfqq->sort_list);
499 RB_CLEAR_COLOR(&crq->rb_node);
500
22e2c507 501 if (cfqq->on_rr && RB_EMPTY(&cfqq->sort_list))
1da177e4
LT
502 cfq_del_cfqq_rr(cfqd, cfqq);
503 }
504}
505
506static struct cfq_rq *
507__cfq_add_crq_rb(struct cfq_rq *crq)
508{
509 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
510 struct rb_node *parent = NULL;
511 struct cfq_rq *__crq;
512
513 while (*p) {
514 parent = *p;
515 __crq = rb_entry_crq(parent);
516
517 if (crq->rb_key < __crq->rb_key)
518 p = &(*p)->rb_left;
519 else if (crq->rb_key > __crq->rb_key)
520 p = &(*p)->rb_right;
521 else
522 return __crq;
523 }
524
525 rb_link_node(&crq->rb_node, parent, p);
526 return NULL;
527}
528
529static void cfq_add_crq_rb(struct cfq_rq *crq)
530{
531 struct cfq_queue *cfqq = crq->cfq_queue;
532 struct cfq_data *cfqd = cfqq->cfqd;
533 struct request *rq = crq->request;
534 struct cfq_rq *__alias;
535
536 crq->rb_key = rq_rb_key(rq);
537 cfqq->queued[crq->is_sync]++;
538
539 /*
540 * looks a little odd, but the first insert might return an alias.
541 * if that happens, put the alias on the dispatch list
542 */
543 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
544 cfq_dispatch_sort(cfqd->queue, __alias);
545
546 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
547
548 if (!cfqq->on_rr)
22e2c507 549 cfq_add_cfqq_rr(cfqd, cfqq, crq->requeued);
1da177e4
LT
550
551 /*
552 * check if this request is a better next-serve candidate
553 */
554 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
555}
556
557static inline void
558cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
559{
560 if (ON_RB(&crq->rb_node)) {
561 rb_erase(&crq->rb_node, &cfqq->sort_list);
562 cfqq->queued[crq->is_sync]--;
563 }
564
565 cfq_add_crq_rb(crq);
566}
567
22e2c507
JA
568static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
569
1da177e4 570{
22e2c507 571 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid);
1da177e4
LT
572 struct rb_node *n;
573
574 if (!cfqq)
575 goto out;
576
577 n = cfqq->sort_list.rb_node;
578 while (n) {
579 struct cfq_rq *crq = rb_entry_crq(n);
580
581 if (sector < crq->rb_key)
582 n = n->rb_left;
583 else if (sector > crq->rb_key)
584 n = n->rb_right;
585 else
586 return crq->request;
587 }
588
589out:
590 return NULL;
591}
592
593static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
594{
22e2c507 595 struct cfq_data *cfqd = q->elevator->elevator_data;
1da177e4
LT
596 struct cfq_rq *crq = RQ_DATA(rq);
597
598 if (crq) {
599 struct cfq_queue *cfqq = crq->cfq_queue;
600
1da177e4
LT
601 if (crq->accounted) {
602 crq->accounted = 0;
22e2c507
JA
603 WARN_ON(!cfqd->rq_in_driver);
604 cfqd->rq_in_driver--;
1da177e4 605 }
22e2c507
JA
606 if (crq->in_flight) {
607 crq->in_flight = 0;
608 WARN_ON(!cfqq->in_flight);
609 cfqq->in_flight--;
610 }
611 crq->requeued = 1;
1da177e4
LT
612 }
613}
614
615/*
616 * make sure the service time gets corrected on reissue of this request
617 */
618static void cfq_requeue_request(request_queue_t *q, struct request *rq)
619{
620 cfq_deactivate_request(q, rq);
621 list_add(&rq->queuelist, &q->queue_head);
622}
623
624static void cfq_remove_request(request_queue_t *q, struct request *rq)
625{
626 struct cfq_rq *crq = RQ_DATA(rq);
627
628 if (crq) {
1da177e4 629 list_del_init(&rq->queuelist);
22e2c507
JA
630 cfq_del_crq_rb(crq);
631 cfq_remove_merge_hints(q, crq);
1da177e4 632
1da177e4
LT
633 }
634}
635
636static int
637cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
638{
639 struct cfq_data *cfqd = q->elevator->elevator_data;
640 struct request *__rq;
641 int ret;
642
643 ret = elv_try_last_merge(q, bio);
644 if (ret != ELEVATOR_NO_MERGE) {
645 __rq = q->last_merge;
646 goto out_insert;
647 }
648
649 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
22e2c507
JA
650 if (__rq && elv_rq_merge_ok(__rq, bio)) {
651 ret = ELEVATOR_BACK_MERGE;
652 goto out;
1da177e4
LT
653 }
654
655 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
22e2c507
JA
656 if (__rq && elv_rq_merge_ok(__rq, bio)) {
657 ret = ELEVATOR_FRONT_MERGE;
658 goto out;
1da177e4
LT
659 }
660
661 return ELEVATOR_NO_MERGE;
662out:
663 q->last_merge = __rq;
664out_insert:
665 *req = __rq;
666 return ret;
667}
668
669static void cfq_merged_request(request_queue_t *q, struct request *req)
670{
671 struct cfq_data *cfqd = q->elevator->elevator_data;
672 struct cfq_rq *crq = RQ_DATA(req);
673
674 cfq_del_crq_hash(crq);
675 cfq_add_crq_hash(cfqd, crq);
676
677 if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
678 struct cfq_queue *cfqq = crq->cfq_queue;
679
680 cfq_update_next_crq(crq);
681 cfq_reposition_crq_rb(cfqq, crq);
682 }
683
684 q->last_merge = req;
685}
686
687static void
688cfq_merged_requests(request_queue_t *q, struct request *rq,
689 struct request *next)
690{
1da177e4
LT
691 cfq_merged_request(q, rq);
692
22e2c507
JA
693 /*
694 * reposition in fifo if next is older than rq
695 */
696 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
697 time_before(next->start_time, rq->start_time))
698 list_move(&rq->queuelist, &next->queuelist);
699
700 cfq_remove_request(q, next);
701}
702
703static inline void
704__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
705{
706 if (cfqq) {
707 /*
708 * stop potential idle class queues waiting service
709 */
710 del_timer(&cfqd->idle_class_timer);
711
712 cfqq->slice_start = jiffies;
713 cfqq->slice_end = 0;
714 cfqq->slice_left = 0;
715 cfqq->must_alloc_slice = 0;
716 cfqq->fifo_expire = 0;
717 }
718
719 cfqd->active_queue = cfqq;
720}
721
722/*
723 * 0
724 * 0,1
725 * 0,1,2
726 * 0,1,2,3
727 * 0,1,2,3,4
728 * 0,1,2,3,4,5
729 * 0,1,2,3,4,5,6
730 * 0,1,2,3,4,5,6,7
731 */
732static int cfq_get_next_prio_level(struct cfq_data *cfqd)
733{
734 int prio, wrap;
735
736 prio = -1;
737 wrap = 0;
738 do {
739 int p;
740
741 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
742 if (!list_empty(&cfqd->rr_list[p])) {
743 prio = p;
744 break;
745 }
746 }
747
748 if (prio != -1)
749 break;
750 cfqd->cur_prio = 0;
751 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
752 cfqd->cur_end_prio = 0;
753 if (wrap)
754 break;
755 wrap = 1;
1da177e4 756 }
22e2c507
JA
757 } while (1);
758
759 if (unlikely(prio == -1))
760 return -1;
761
762 BUG_ON(prio >= CFQ_PRIO_LISTS);
763
764 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
765
766 cfqd->cur_prio = prio + 1;
767 if (cfqd->cur_prio > cfqd->cur_end_prio) {
768 cfqd->cur_end_prio = cfqd->cur_prio;
769 cfqd->cur_prio = 0;
770 }
771 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
772 cfqd->cur_prio = 0;
773 cfqd->cur_end_prio = 0;
1da177e4
LT
774 }
775
22e2c507
JA
776 return prio;
777}
778
779static void cfq_set_active_queue(struct cfq_data *cfqd)
780{
781 struct cfq_queue *cfqq = NULL;
782
783 /*
784 * if current list is non-empty, grab first entry. if it is empty,
785 * get next prio level and grab first entry then if any are spliced
786 */
787 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
788 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
789
790 /*
791 * if we have idle queues and no rt or be queues had pending
792 * requests, either allow immediate service if the grace period
793 * has passed or arm the idle grace timer
794 */
795 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
796 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
797
798 if (time_after_eq(jiffies, end))
799 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
800 else
801 mod_timer(&cfqd->idle_class_timer, end);
802 }
803
804 __cfq_set_active_queue(cfqd, cfqq);
805}
806
807/*
808 * current cfqq expired its slice (or was too idle), select new one
809 */
810static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
811{
812 struct cfq_queue *cfqq = cfqd->active_queue;
813
814 if (cfqq) {
815 unsigned long now = jiffies;
816
817 if (cfqq->wait_request)
818 del_timer(&cfqd->idle_slice_timer);
819
820 if (!preempted && !cfqq->in_flight)
821 cfqq->service_last = now;
822
823 cfqq->must_dispatch = 0;
824 cfqq->wait_request = 0;
825
826 /*
827 * store what was left of this slice, if the queue idled out
828 * or was preempted
829 */
830 if (time_after(now, cfqq->slice_end))
831 cfqq->slice_left = now - cfqq->slice_end;
832 else
833 cfqq->slice_left = 0;
834
835 if (cfqq->on_rr)
836 cfq_resort_rr_list(cfqq, preempted);
837
838 cfqd->active_queue = NULL;
839
840 if (cfqd->active_cic) {
841 put_io_context(cfqd->active_cic->ioc);
842 cfqd->active_cic = NULL;
843 }
844 }
845
846 cfqd->dispatch_slice = 0;
847}
848
849static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
850
851{
852 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
853 WARN_ON(cfqq != cfqd->active_queue);
854
855 /*
856 * idle is disabled, either manually or by past process history
857 */
858 if (!cfqd->cfq_slice_idle)
859 return 0;
860 if (!cfqq->idle_window)
861 return 0;
862 /*
863 * task has exited, don't wait
864 */
865 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
866 return 0;
867
868 cfqq->wait_request = 1;
869 cfqq->must_alloc = 1;
870
871 if (!timer_pending(&cfqd->idle_slice_timer)) {
872 unsigned long slice_left = cfqq->slice_end - 1;
873
874 cfqd->idle_slice_timer.expires = min(jiffies + cfqd->cfq_slice_idle, slice_left);
875 add_timer(&cfqd->idle_slice_timer);
876 }
877
878 return 1;
1da177e4
LT
879}
880
881/*
882 * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
883 * this function sector sorts the selected request to minimize seeks. we start
884 * at cfqd->last_sector, not 0.
885 */
886static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
887{
888 struct cfq_data *cfqd = q->elevator->elevator_data;
889 struct cfq_queue *cfqq = crq->cfq_queue;
890 struct list_head *head = &q->queue_head, *entry = head;
891 struct request *__rq;
892 sector_t last;
893
1da177e4
LT
894 list_del(&crq->request->queuelist);
895
896 last = cfqd->last_sector;
22e2c507
JA
897 list_for_each_entry_reverse(__rq, head, queuelist) {
898 struct cfq_rq *__crq = RQ_DATA(__rq);
1da177e4 899
22e2c507
JA
900 if (blk_barrier_rq(__rq))
901 break;
902 if (!blk_fs_request(__rq))
1da177e4 903 break;
22e2c507 904 if (__crq->requeued)
1da177e4
LT
905 break;
906
22e2c507 907 if (__rq->sector <= crq->request->sector)
1da177e4
LT
908 break;
909 if (__rq->sector > last && crq->request->sector < last) {
22e2c507 910 last = crq->request->sector + crq->request->nr_sectors;
1da177e4
LT
911 break;
912 }
22e2c507 913 entry = &__rq->queuelist;
1da177e4
LT
914 }
915
916 cfqd->last_sector = last;
22e2c507
JA
917
918 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
919
920 cfq_del_crq_rb(crq);
921 cfq_remove_merge_hints(q, crq);
922
1da177e4 923 crq->in_flight = 1;
22e2c507 924 crq->requeued = 0;
1da177e4 925 cfqq->in_flight++;
22e2c507 926 list_add_tail(&crq->request->queuelist, entry);
1da177e4
LT
927}
928
929/*
930 * return expired entry, or NULL to just start from scratch in rbtree
931 */
932static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
933{
934 struct cfq_data *cfqd = cfqq->cfqd;
22e2c507 935 struct request *rq;
1da177e4
LT
936 struct cfq_rq *crq;
937
22e2c507 938 if (cfqq->fifo_expire)
1da177e4
LT
939 return NULL;
940
22e2c507
JA
941 if (!list_empty(&cfqq->fifo)) {
942 int fifo = cfq_cfqq_sync(cfqq);
1da177e4 943
22e2c507
JA
944 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
945 rq = crq->request;
946 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
947 cfqq->fifo_expire = 1;
948 return crq;
949 }
1da177e4
LT
950 }
951
952 return NULL;
953}
954
955/*
22e2c507 956 * Scale schedule slice based on io priority
1da177e4 957 */
22e2c507
JA
958static inline int
959cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
960{
961 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
962
963 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
964
965 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
966}
967
1da177e4 968static inline void
22e2c507 969cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 970{
22e2c507
JA
971 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
972}
1da177e4 973
22e2c507
JA
974static inline int
975cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
976{
977 const int base_rq = cfqd->cfq_slice_async_rq;
1da177e4 978
22e2c507 979 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1da177e4 980
22e2c507 981 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1da177e4
LT
982}
983
22e2c507
JA
984/*
985 * get next queue for service
986 */
987static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
1da177e4 988{
22e2c507 989 unsigned long now = jiffies;
1da177e4 990 struct cfq_queue *cfqq;
1da177e4 991
22e2c507
JA
992 cfqq = cfqd->active_queue;
993 if (!cfqq)
994 goto new_queue;
1da177e4 995
22e2c507
JA
996 /*
997 * slice has expired
998 */
999 if (!cfqq->must_dispatch && time_after(jiffies, cfqq->slice_end))
1000 goto new_queue;
1da177e4 1001
22e2c507
JA
1002 /*
1003 * if queue has requests, dispatch one. if not, check if
1004 * enough slice is left to wait for one
1005 */
1006 if (!RB_EMPTY(&cfqq->sort_list))
1007 goto keep_queue;
1008 else if (!force && cfq_cfqq_sync(cfqq) &&
1009 time_before(now, cfqq->slice_end)) {
1010 if (cfq_arm_slice_timer(cfqd, cfqq))
1011 return NULL;
1012 }
1013
1014new_queue:
1015 cfq_slice_expired(cfqd, 0);
1016 cfq_set_active_queue(cfqd);
1017keep_queue:
1018 return cfqd->active_queue;
1019}
1020
1021static int
1022__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1023 int max_dispatch)
1024{
1025 int dispatched = 0;
1026
1027 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1028
1029 do {
1030 struct cfq_rq *crq;
1da177e4
LT
1031
1032 /*
22e2c507 1033 * follow expired path, else get first next available
1da177e4 1034 */
22e2c507
JA
1035 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1036 crq = cfqq->next_crq;
1037
1038 /*
1039 * finally, insert request into driver dispatch list
1040 */
1041 cfq_dispatch_sort(cfqd->queue, crq);
1da177e4 1042
22e2c507
JA
1043 cfqd->dispatch_slice++;
1044 dispatched++;
1da177e4 1045
22e2c507
JA
1046 if (!cfqd->active_cic) {
1047 atomic_inc(&crq->io_context->ioc->refcount);
1048 cfqd->active_cic = crq->io_context;
1049 }
1da177e4 1050
22e2c507
JA
1051 if (RB_EMPTY(&cfqq->sort_list))
1052 break;
1053
1054 } while (dispatched < max_dispatch);
1055
1056 /*
1057 * if slice end isn't set yet, set it. if at least one request was
1058 * sync, use the sync time slice value
1059 */
1060 if (!cfqq->slice_end)
1061 cfq_set_prio_slice(cfqd, cfqq);
1062
1063 /*
1064 * expire an async queue immediately if it has used up its slice. idle
1065 * queue always expire after 1 dispatch round.
1066 */
1067 if ((!cfq_cfqq_sync(cfqq) &&
1068 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1069 cfq_class_idle(cfqq))
1070 cfq_slice_expired(cfqd, 0);
1071
1072 return dispatched;
1073}
1074
1075static int
1076cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1077{
1078 struct cfq_data *cfqd = q->elevator->elevator_data;
1079 struct cfq_queue *cfqq;
1080
1081 if (!cfqd->busy_queues)
1082 return 0;
1083
1084 cfqq = cfq_select_queue(cfqd, force);
1085 if (cfqq) {
1086 cfqq->wait_request = 0;
1087 cfqq->must_dispatch = 0;
1088 del_timer(&cfqd->idle_slice_timer);
1089
1090 if (cfq_class_idle(cfqq))
1091 max_dispatch = 1;
1da177e4 1092
22e2c507 1093 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1da177e4
LT
1094 }
1095
22e2c507 1096 return 0;
1da177e4
LT
1097}
1098
1099static inline void cfq_account_dispatch(struct cfq_rq *crq)
1100{
1101 struct cfq_queue *cfqq = crq->cfq_queue;
1102 struct cfq_data *cfqd = cfqq->cfqd;
1da177e4 1103
22e2c507 1104 if (unlikely(!blk_fs_request(crq->request)))
1da177e4
LT
1105 return;
1106
1107 /*
1108 * accounted bit is necessary since some drivers will call
1109 * elv_next_request() many times for the same request (eg ide)
1110 */
1111 if (crq->accounted)
1112 return;
1113
1da177e4 1114 crq->accounted = 1;
22e2c507 1115 cfqd->rq_in_driver++;
1da177e4
LT
1116}
1117
1118static inline void
1119cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1120{
1121 struct cfq_data *cfqd = cfqq->cfqd;
22e2c507 1122 unsigned long now;
1da177e4
LT
1123
1124 if (!crq->accounted)
1125 return;
1126
22e2c507
JA
1127 now = jiffies;
1128
1da177e4
LT
1129 WARN_ON(!cfqd->rq_in_driver);
1130 cfqd->rq_in_driver--;
1131
22e2c507
JA
1132 if (!cfq_class_idle(cfqq))
1133 cfqd->last_end_request = now;
1da177e4 1134
22e2c507
JA
1135 if (!cfqq->in_flight && cfqq->on_rr) {
1136 cfqq->service_last = now;
1137 cfq_resort_rr_list(cfqq, 0);
1da177e4 1138 }
22e2c507
JA
1139
1140 if (crq->is_sync)
1141 crq->io_context->last_end_request = now;
1da177e4
LT
1142}
1143
1144static struct request *cfq_next_request(request_queue_t *q)
1145{
1146 struct cfq_data *cfqd = q->elevator->elevator_data;
1147 struct request *rq;
1148
1149 if (!list_empty(&q->queue_head)) {
1150 struct cfq_rq *crq;
1151dispatch:
1152 rq = list_entry_rq(q->queue_head.next);
1153
22e2c507
JA
1154 crq = RQ_DATA(rq);
1155 if (crq) {
1156 /*
1157 * if idle window is disabled, allow queue buildup
1158 */
1159 if (!crq->in_flight && !crq->cfq_queue->idle_window &&
1160 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1161 return NULL;
1162
1da177e4
LT
1163 cfq_remove_merge_hints(q, crq);
1164 cfq_account_dispatch(crq);
1165 }
1166
1167 return rq;
1168 }
1169
22e2c507 1170 if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1da177e4
LT
1171 goto dispatch;
1172
1173 return NULL;
1174}
1175
1176/*
1177 * task holds one reference to the queue, dropped when task exits. each crq
1178 * in-flight on this queue also holds a reference, dropped when crq is freed.
1179 *
1180 * queue lock must be held here.
1181 */
1182static void cfq_put_queue(struct cfq_queue *cfqq)
1183{
22e2c507
JA
1184 struct cfq_data *cfqd = cfqq->cfqd;
1185
1186 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1da177e4
LT
1187
1188 if (!atomic_dec_and_test(&cfqq->ref))
1189 return;
1190
1191 BUG_ON(rb_first(&cfqq->sort_list));
22e2c507 1192 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1da177e4
LT
1193 BUG_ON(cfqq->on_rr);
1194
22e2c507
JA
1195 if (unlikely(cfqd->active_queue == cfqq)) {
1196 cfq_slice_expired(cfqd, 0);
1197 kblockd_schedule_work(&cfqd->unplug_work);
1198 }
1199
1da177e4
LT
1200 cfq_put_cfqd(cfqq->cfqd);
1201
1202 /*
1203 * it's on the empty list and still hashed
1204 */
1205 list_del(&cfqq->cfq_list);
1206 hlist_del(&cfqq->cfq_hash);
1207 kmem_cache_free(cfq_pool, cfqq);
1208}
1209
1210static inline struct cfq_queue *
22e2c507 1211__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, const int hashval)
1da177e4
LT
1212{
1213 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1214 struct hlist_node *entry, *next;
1215
1216 hlist_for_each_safe(entry, next, hash_list) {
1217 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1218
1219 if (__cfqq->key == key)
1220 return __cfqq;
1221 }
1222
1223 return NULL;
1224}
1225
1226static struct cfq_queue *
22e2c507 1227cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key)
1da177e4
LT
1228{
1229 return __cfq_find_cfq_hash(cfqd, key, hash_long(key, CFQ_QHASH_SHIFT));
1230}
1231
22e2c507 1232static void cfq_free_io_context(struct cfq_io_context *cic)
1da177e4 1233{
22e2c507
JA
1234 struct cfq_io_context *__cic;
1235 struct list_head *entry, *next;
1da177e4 1236
22e2c507
JA
1237 list_for_each_safe(entry, next, &cic->list) {
1238 __cic = list_entry(entry, struct cfq_io_context, list);
1239 kmem_cache_free(cfq_ioc_pool, __cic);
1da177e4
LT
1240 }
1241
22e2c507 1242 kmem_cache_free(cfq_ioc_pool, cic);
1da177e4
LT
1243}
1244
22e2c507
JA
1245/*
1246 * Called with interrupts disabled
1247 */
1248static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1da177e4 1249{
22e2c507
JA
1250 struct cfq_data *cfqd = cic->cfqq->cfqd;
1251 request_queue_t *q = cfqd->queue;
1252
1253 WARN_ON(!irqs_disabled());
1254
1255 spin_lock(q->queue_lock);
1256
1257 if (unlikely(cic->cfqq == cfqd->active_queue)) {
1258 cfq_slice_expired(cfqd, 0);
1259 kblockd_schedule_work(&cfqd->unplug_work);
1260 }
1261
1262 cfq_put_queue(cic->cfqq);
1263 cic->cfqq = NULL;
1264 spin_unlock(q->queue_lock);
1da177e4
LT
1265}
1266
1267/*
22e2c507
JA
1268 * Another task may update the task cic list, if it is doing a queue lookup
1269 * on its behalf. cfq_cic_lock excludes such concurrent updates
1da177e4
LT
1270 */
1271static void cfq_exit_io_context(struct cfq_io_context *cic)
1272{
22e2c507
JA
1273 struct cfq_io_context *__cic;
1274 struct list_head *entry;
1da177e4
LT
1275 unsigned long flags;
1276
22e2c507
JA
1277 local_irq_save(flags);
1278
1da177e4
LT
1279 /*
1280 * put the reference this task is holding to the various queues
1281 */
22e2c507 1282 list_for_each(entry, &cic->list) {
1da177e4 1283 __cic = list_entry(entry, struct cfq_io_context, list);
22e2c507 1284 cfq_exit_single_io_context(__cic);
1da177e4
LT
1285 }
1286
22e2c507
JA
1287 cfq_exit_single_io_context(cic);
1288 local_irq_restore(flags);
1da177e4
LT
1289}
1290
22e2c507
JA
1291static struct cfq_io_context *
1292cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1da177e4 1293{
22e2c507 1294 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1da177e4
LT
1295
1296 if (cic) {
1da177e4
LT
1297 INIT_LIST_HEAD(&cic->list);
1298 cic->cfqq = NULL;
22e2c507
JA
1299 cic->key = NULL;
1300 cic->last_end_request = jiffies;
1301 cic->ttime_total = 0;
1302 cic->ttime_samples = 0;
1303 cic->ttime_mean = 0;
1304 cic->dtor = cfq_free_io_context;
1305 cic->exit = cfq_exit_io_context;
1da177e4
LT
1306 }
1307
1308 return cic;
1309}
1310
22e2c507
JA
1311static void cfq_init_prio_data(struct cfq_queue *cfqq)
1312{
1313 struct task_struct *tsk = current;
1314 int ioprio_class;
1315
1316 if (!cfqq->prio_changed)
1317 return;
1318
1319 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1320 switch (ioprio_class) {
1321 default:
1322 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1323 case IOPRIO_CLASS_NONE:
1324 /*
1325 * no prio set, place us in the middle of the BE classes
1326 */
1327 cfqq->ioprio = task_nice_ioprio(tsk);
1328 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1329 break;
1330 case IOPRIO_CLASS_RT:
1331 cfqq->ioprio = task_ioprio(tsk);
1332 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1333 break;
1334 case IOPRIO_CLASS_BE:
1335 cfqq->ioprio = task_ioprio(tsk);
1336 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1337 break;
1338 case IOPRIO_CLASS_IDLE:
1339 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1340 cfqq->ioprio = 7;
1341 cfqq->idle_window = 0;
1342 break;
1343 }
1344
1345 /*
1346 * keep track of original prio settings in case we have to temporarily
1347 * elevate the priority of this queue
1348 */
1349 cfqq->org_ioprio = cfqq->ioprio;
1350 cfqq->org_ioprio_class = cfqq->ioprio_class;
1351
1352 if (cfqq->on_rr)
1353 cfq_resort_rr_list(cfqq, 0);
1354
1355 cfqq->prio_changed = 0;
1356}
1357
1358static inline void changed_ioprio(struct cfq_queue *cfqq)
1359{
1360 if (cfqq) {
1361 struct cfq_data *cfqd = cfqq->cfqd;
1362
1363 spin_lock(cfqd->queue->queue_lock);
1364 cfqq->prio_changed = 1;
1365 cfq_init_prio_data(cfqq);
1366 spin_unlock(cfqd->queue->queue_lock);
1367 }
1368}
1369
1370/*
1371 * callback from sys_ioprio_set, irqs are disabled
1372 */
1373static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1374{
1375 struct cfq_io_context *cic = ioc->cic;
1376
1377 changed_ioprio(cic->cfqq);
1378
1379 list_for_each_entry(cic, &cic->list, list)
1380 changed_ioprio(cic->cfqq);
1381
1382 return 0;
1383}
1384
1385static struct cfq_queue *
1386cfq_get_queue(struct cfq_data *cfqd, unsigned int key, int gfp_mask)
1387{
1388 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1389 struct cfq_queue *cfqq, *new_cfqq = NULL;
1390
1391retry:
1392 cfqq = __cfq_find_cfq_hash(cfqd, key, hashval);
1393
1394 if (!cfqq) {
1395 if (new_cfqq) {
1396 cfqq = new_cfqq;
1397 new_cfqq = NULL;
1398 } else if (gfp_mask & __GFP_WAIT) {
1399 spin_unlock_irq(cfqd->queue->queue_lock);
1400 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1401 spin_lock_irq(cfqd->queue->queue_lock);
1402 goto retry;
1403 } else {
1404 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1405 if (!cfqq)
1406 goto out;
1407 }
1408
1409 memset(cfqq, 0, sizeof(*cfqq));
1410
1411 INIT_HLIST_NODE(&cfqq->cfq_hash);
1412 INIT_LIST_HEAD(&cfqq->cfq_list);
1413 RB_CLEAR_ROOT(&cfqq->sort_list);
1414 INIT_LIST_HEAD(&cfqq->fifo);
1415
1416 cfqq->key = key;
1417 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1418 atomic_set(&cfqq->ref, 0);
1419 cfqq->cfqd = cfqd;
1420 atomic_inc(&cfqd->ref);
1421 cfqq->service_last = 0;
1422 /*
1423 * set ->slice_left to allow preemption for a new process
1424 */
1425 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1426 cfqq->idle_window = 1;
1427 cfqq->ioprio = -1;
1428 cfqq->ioprio_class = -1;
1429 cfqq->prio_changed = 1;
1430 }
1431
1432 if (new_cfqq)
1433 kmem_cache_free(cfq_pool, new_cfqq);
1434
1435 atomic_inc(&cfqq->ref);
1436out:
1437 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1438 return cfqq;
1439}
1440
1da177e4
LT
1441/*
1442 * Setup general io context and cfq io context. There can be several cfq
1443 * io contexts per general io context, if this process is doing io to more
1444 * than one device managed by cfq. Note that caller is holding a reference to
1445 * cfqq, so we don't need to worry about it disappearing
1446 */
1447static struct cfq_io_context *
22e2c507 1448cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1da177e4 1449{
22e2c507 1450 struct io_context *ioc = NULL;
1da177e4 1451 struct cfq_io_context *cic;
1da177e4 1452
22e2c507 1453 might_sleep_if(gfp_mask & __GFP_WAIT);
1da177e4 1454
22e2c507 1455 ioc = get_io_context(gfp_mask);
1da177e4
LT
1456 if (!ioc)
1457 return NULL;
1458
1459 if ((cic = ioc->cic) == NULL) {
22e2c507 1460 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1da177e4
LT
1461
1462 if (cic == NULL)
1463 goto err;
1464
22e2c507
JA
1465 /*
1466 * manually increment generic io_context usage count, it
1467 * cannot go away since we are already holding one ref to it
1468 */
1da177e4 1469 ioc->cic = cic;
22e2c507 1470 ioc->set_ioprio = cfq_ioc_set_ioprio;
1da177e4 1471 cic->ioc = ioc;
22e2c507
JA
1472 cic->key = cfqd;
1473 atomic_inc(&cfqd->ref);
1da177e4
LT
1474 } else {
1475 struct cfq_io_context *__cic;
1da177e4
LT
1476
1477 /*
22e2c507 1478 * the first cic on the list is actually the head itself
1da177e4 1479 */
22e2c507 1480 if (cic->key == cfqd)
1da177e4
LT
1481 goto out;
1482
1483 /*
1484 * cic exists, check if we already are there. linear search
1485 * should be ok here, the list will usually not be more than
1486 * 1 or a few entries long
1487 */
1da177e4
LT
1488 list_for_each_entry(__cic, &cic->list, list) {
1489 /*
1490 * this process is already holding a reference to
1491 * this queue, so no need to get one more
1492 */
22e2c507 1493 if (__cic->key == cfqd) {
1da177e4 1494 cic = __cic;
1da177e4
LT
1495 goto out;
1496 }
1497 }
1da177e4
LT
1498
1499 /*
1500 * nope, process doesn't have a cic assoicated with this
1501 * cfqq yet. get a new one and add to list
1502 */
22e2c507 1503 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1da177e4
LT
1504 if (__cic == NULL)
1505 goto err;
1506
1507 __cic->ioc = ioc;
22e2c507
JA
1508 __cic->key = cfqd;
1509 atomic_inc(&cfqd->ref);
1da177e4 1510 list_add(&__cic->list, &cic->list);
1da177e4 1511 cic = __cic;
1da177e4
LT
1512 }
1513
1514out:
1da177e4
LT
1515 return cic;
1516err:
1517 put_io_context(ioc);
1518 return NULL;
1519}
1520
22e2c507
JA
1521static void
1522cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1da177e4 1523{
22e2c507 1524 unsigned long elapsed, ttime;
1da177e4 1525
22e2c507
JA
1526 /*
1527 * if this context already has stuff queued, thinktime is from
1528 * last queue not last end
1529 */
1530#if 0
1531 if (time_after(cic->last_end_request, cic->last_queue))
1532 elapsed = jiffies - cic->last_end_request;
1533 else
1534 elapsed = jiffies - cic->last_queue;
1535#else
1536 elapsed = jiffies - cic->last_end_request;
1537#endif
1da177e4 1538
22e2c507 1539 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
db3b5848 1540
22e2c507
JA
1541 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1542 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1543 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1544}
1da177e4 1545
22e2c507 1546#define sample_valid(samples) ((samples) > 80)
1da177e4 1547
22e2c507
JA
1548/*
1549 * Disable idle window if the process thinks too long or seeks so much that
1550 * it doesn't matter
1551 */
1552static void
1553cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1554 struct cfq_io_context *cic)
1555{
1556 int enable_idle = cfqq->idle_window;
1da177e4 1557
22e2c507
JA
1558 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1559 enable_idle = 0;
1560 else if (sample_valid(cic->ttime_samples)) {
1561 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1562 enable_idle = 0;
1563 else
1564 enable_idle = 1;
1da177e4
LT
1565 }
1566
22e2c507
JA
1567 cfqq->idle_window = enable_idle;
1568}
1da177e4 1569
22e2c507
JA
1570
1571/*
1572 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1573 * no or if we aren't sure, a 1 will cause a preempt.
1574 */
1575static int
1576cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1577 struct cfq_rq *crq)
1578{
1579 struct cfq_queue *cfqq = cfqd->active_queue;
1580
1581 if (cfq_class_idle(new_cfqq))
1582 return 0;
1583
1584 if (!cfqq)
1585 return 1;
1586
1587 if (cfq_class_idle(cfqq))
1588 return 1;
1589 if (!new_cfqq->wait_request)
1590 return 0;
1591 /*
1592 * if it doesn't have slice left, forget it
1593 */
1594 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1595 return 0;
1596 if (crq->is_sync && !cfq_cfqq_sync(cfqq))
1597 return 1;
1598
1599 return 0;
1600}
1601
1602/*
1603 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1604 * let it have half of its nominal slice.
1605 */
1606static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1607{
1608 struct cfq_queue *__cfqq, *next;
1609
1610 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1611 cfq_resort_rr_list(__cfqq, 1);
1612
1613 if (!cfqq->slice_left)
1614 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1615
1616 cfqq->slice_end = cfqq->slice_left + jiffies;
1617 cfq_slice_expired(cfqd, 1);
1618 __cfq_set_active_queue(cfqd, cfqq);
1619}
1620
1621/*
1622 * should really be a ll_rw_blk.c helper
1623 */
1624static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1625{
1626 request_queue_t *q = cfqd->queue;
1627
1628 if (!blk_queue_plugged(q))
1629 q->request_fn(q);
1630 else
1631 __generic_unplug_device(q);
1632}
1633
1634/*
1635 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1636 * something we should do about it
1637 */
1638static void
1639cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1640 struct cfq_rq *crq)
1641{
1642 const int sync = crq->is_sync;
1643
1644 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1645
1646 if (sync) {
1647 struct cfq_io_context *cic = crq->io_context;
1648
1649 cfq_update_io_thinktime(cfqd, cic);
1650 cfq_update_idle_window(cfqd, cfqq, cic);
1651
1652 cic->last_queue = jiffies;
1653 }
1654
1655 if (cfqq == cfqd->active_queue) {
1656 /*
1657 * if we are waiting for a request for this queue, let it rip
1658 * immediately and flag that we must not expire this queue
1659 * just now
1660 */
1661 if (cfqq->wait_request) {
1662 cfqq->must_dispatch = 1;
1663 del_timer(&cfqd->idle_slice_timer);
1664 cfq_start_queueing(cfqd, cfqq);
1665 }
1666 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1667 /*
1668 * not the active queue - expire current slice if it is
1669 * idle and has expired it's mean thinktime or this new queue
1670 * has some old slice time left and is of higher priority
1671 */
1672 cfq_preempt_queue(cfqd, cfqq);
1673 cfqq->must_dispatch = 1;
1674 cfq_start_queueing(cfqd, cfqq);
1675 }
1da177e4
LT
1676}
1677
22e2c507 1678static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1da177e4 1679{
22e2c507
JA
1680 struct cfq_rq *crq = RQ_DATA(rq);
1681 struct cfq_queue *cfqq = crq->cfq_queue;
1682
1683 cfq_init_prio_data(cfqq);
1da177e4
LT
1684
1685 cfq_add_crq_rb(crq);
1da177e4 1686
22e2c507
JA
1687 list_add_tail(&rq->queuelist, &cfqq->fifo);
1688
1689 if (rq_mergeable(rq)) {
1690 cfq_add_crq_hash(cfqd, crq);
1691
1692 if (!cfqd->queue->last_merge)
1693 cfqd->queue->last_merge = rq;
1694 }
1695
1696 cfq_crq_enqueued(cfqd, cfqq, crq);
1da177e4
LT
1697}
1698
1699static void
1700cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1701{
1702 struct cfq_data *cfqd = q->elevator->elevator_data;
1da177e4
LT
1703
1704 switch (where) {
1705 case ELEVATOR_INSERT_BACK:
22e2c507 1706 while (cfq_dispatch_requests(q, INT_MAX, 1))
1da177e4
LT
1707 ;
1708 list_add_tail(&rq->queuelist, &q->queue_head);
22e2c507
JA
1709 /*
1710 * If we were idling with pending requests on
1711 * inactive cfqqs, force dispatching will
1712 * remove the idle timer and the queue won't
1713 * be kicked by __make_request() afterward.
1714 * Kick it here.
1715 */
1716 kblockd_schedule_work(&cfqd->unplug_work);
1da177e4
LT
1717 break;
1718 case ELEVATOR_INSERT_FRONT:
1719 list_add(&rq->queuelist, &q->queue_head);
1720 break;
1721 case ELEVATOR_INSERT_SORT:
1722 BUG_ON(!blk_fs_request(rq));
22e2c507 1723 cfq_enqueue(cfqd, rq);
1da177e4
LT
1724 break;
1725 default:
1726 printk("%s: bad insert point %d\n", __FUNCTION__,where);
1727 return;
1728 }
22e2c507 1729}
1da177e4 1730
22e2c507
JA
1731static inline int cfq_pending_requests(struct cfq_data *cfqd)
1732{
1733 return !list_empty(&cfqd->queue->queue_head) || cfqd->busy_queues;
1da177e4
LT
1734}
1735
1736static int cfq_queue_empty(request_queue_t *q)
1737{
1738 struct cfq_data *cfqd = q->elevator->elevator_data;
1739
22e2c507 1740 return !cfq_pending_requests(cfqd);
1da177e4
LT
1741}
1742
1743static void cfq_completed_request(request_queue_t *q, struct request *rq)
1744{
1745 struct cfq_rq *crq = RQ_DATA(rq);
1746 struct cfq_queue *cfqq;
1747
1748 if (unlikely(!blk_fs_request(rq)))
1749 return;
1750
1751 cfqq = crq->cfq_queue;
1752
1753 if (crq->in_flight) {
1754 WARN_ON(!cfqq->in_flight);
1755 cfqq->in_flight--;
1756 }
1757
1758 cfq_account_completion(cfqq, crq);
1759}
1760
1761static struct request *
1762cfq_former_request(request_queue_t *q, struct request *rq)
1763{
1764 struct cfq_rq *crq = RQ_DATA(rq);
1765 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1766
1767 if (rbprev)
1768 return rb_entry_crq(rbprev)->request;
1769
1770 return NULL;
1771}
1772
1773static struct request *
1774cfq_latter_request(request_queue_t *q, struct request *rq)
1775{
1776 struct cfq_rq *crq = RQ_DATA(rq);
1777 struct rb_node *rbnext = rb_next(&crq->rb_node);
1778
1779 if (rbnext)
1780 return rb_entry_crq(rbnext)->request;
1781
1782 return NULL;
1783}
1784
22e2c507
JA
1785/*
1786 * we temporarily boost lower priority queues if they are holding fs exclusive
1787 * resources. they are boosted to normal prio (CLASS_BE/4)
1788 */
1789static void cfq_prio_boost(struct cfq_queue *cfqq)
1da177e4 1790{
22e2c507
JA
1791 const int ioprio_class = cfqq->ioprio_class;
1792 const int ioprio = cfqq->ioprio;
1da177e4 1793
22e2c507
JA
1794 if (has_fs_excl()) {
1795 /*
1796 * boost idle prio on transactions that would lock out other
1797 * users of the filesystem
1798 */
1799 if (cfq_class_idle(cfqq))
1800 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1801 if (cfqq->ioprio > IOPRIO_NORM)
1802 cfqq->ioprio = IOPRIO_NORM;
1803 } else {
1804 /*
1805 * check if we need to unboost the queue
1806 */
1807 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1808 cfqq->ioprio_class = cfqq->org_ioprio_class;
1809 if (cfqq->ioprio != cfqq->org_ioprio)
1810 cfqq->ioprio = cfqq->org_ioprio;
1811 }
1da177e4 1812
22e2c507
JA
1813 /*
1814 * refile between round-robin lists if we moved the priority class
1815 */
1816 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1817 cfqq->on_rr)
1818 cfq_resort_rr_list(cfqq, 0);
1819}
1da177e4 1820
22e2c507
JA
1821static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1822{
1823 if (rw == READ || process_sync(task))
1824 return task->pid;
1da177e4 1825
22e2c507
JA
1826 return CFQ_KEY_ASYNC;
1827}
1da177e4 1828
22e2c507
JA
1829static inline int
1830__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1831 struct task_struct *task, int rw)
1832{
1833 if (cfqq->wait_request && cfqq->must_alloc)
1834 return ELV_MQUEUE_MUST;
1da177e4 1835
22e2c507
JA
1836 return ELV_MQUEUE_MAY;
1837#if 0
1838 if (!cfqq || task->flags & PF_MEMALLOC)
1839 return ELV_MQUEUE_MAY;
1840 if (!cfqq->allocated[rw] || cfqq->must_alloc) {
1841 if (cfqq->wait_request)
1842 return ELV_MQUEUE_MUST;
1da177e4 1843
22e2c507
JA
1844 /*
1845 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1846 * can quickly flood the queue with writes from a single task
1847 */
1848 if (rw == READ || !cfqq->must_alloc_slice) {
1849 cfqq->must_alloc_slice = 1;
1850 return ELV_MQUEUE_MUST;
1da177e4 1851 }
22e2c507
JA
1852
1853 return ELV_MQUEUE_MAY;
1da177e4 1854 }
22e2c507
JA
1855 if (cfq_class_idle(cfqq))
1856 return ELV_MQUEUE_NO;
1857 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1858 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1859 int ret = ELV_MQUEUE_NO;
1da177e4 1860
22e2c507
JA
1861 if (ioc && ioc->nr_batch_requests)
1862 ret = ELV_MQUEUE_MAY;
1863
1864 put_io_context(ioc);
1865 return ret;
1866 }
1867
1868 return ELV_MQUEUE_MAY;
1869#endif
1870}
1871
1872static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1873{
1874 struct cfq_data *cfqd = q->elevator->elevator_data;
1875 struct task_struct *tsk = current;
1876 struct cfq_queue *cfqq;
1877
1878 /*
1879 * don't force setup of a queue from here, as a call to may_queue
1880 * does not necessarily imply that a request actually will be queued.
1881 * so just lookup a possibly existing queue, or return 'may queue'
1882 * if that fails
1883 */
1884 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw));
1885 if (cfqq) {
1886 cfq_init_prio_data(cfqq);
1887 cfq_prio_boost(cfqq);
1888
1889 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1890 }
1891
1892 return ELV_MQUEUE_MAY;
1da177e4
LT
1893}
1894
1895static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1896{
22e2c507 1897 struct cfq_data *cfqd = q->elevator->elevator_data;
1da177e4 1898 struct request_list *rl = &q->rq;
1da177e4 1899
22e2c507
JA
1900 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1901 smp_mb();
1902 if (waitqueue_active(&rl->wait[READ]))
1903 wake_up(&rl->wait[READ]);
1904 }
1905
1906 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1907 smp_mb();
1908 if (waitqueue_active(&rl->wait[WRITE]))
1909 wake_up(&rl->wait[WRITE]);
1910 }
1da177e4
LT
1911}
1912
1913/*
1914 * queue lock held here
1915 */
1916static void cfq_put_request(request_queue_t *q, struct request *rq)
1917{
1918 struct cfq_data *cfqd = q->elevator->elevator_data;
1919 struct cfq_rq *crq = RQ_DATA(rq);
1920
1921 if (crq) {
1922 struct cfq_queue *cfqq = crq->cfq_queue;
22e2c507 1923 const int rw = rq_data_dir(rq);
1da177e4 1924
22e2c507
JA
1925 BUG_ON(!cfqq->allocated[rw]);
1926 cfqq->allocated[rw]--;
1da177e4 1927
22e2c507 1928 put_io_context(crq->io_context->ioc);
1da177e4
LT
1929
1930 mempool_free(crq, cfqd->crq_pool);
1931 rq->elevator_private = NULL;
1932
1da177e4
LT
1933 cfq_check_waiters(q, cfqq);
1934 cfq_put_queue(cfqq);
1935 }
1936}
1937
1938/*
22e2c507 1939 * Allocate cfq data structures associated with this request.
1da177e4 1940 */
22e2c507
JA
1941static int
1942cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1943 int gfp_mask)
1da177e4
LT
1944{
1945 struct cfq_data *cfqd = q->elevator->elevator_data;
1946 struct cfq_io_context *cic;
1947 const int rw = rq_data_dir(rq);
22e2c507 1948 struct cfq_queue *cfqq;
1da177e4
LT
1949 struct cfq_rq *crq;
1950 unsigned long flags;
1951
1952 might_sleep_if(gfp_mask & __GFP_WAIT);
1953
22e2c507
JA
1954 cic = cfq_get_io_context(cfqd, cfq_queue_pid(current, rw), gfp_mask);
1955
1da177e4
LT
1956 spin_lock_irqsave(q->queue_lock, flags);
1957
22e2c507
JA
1958 if (!cic)
1959 goto queue_fail;
1960
1961 if (!cic->cfqq) {
1962 cfqq = cfq_get_queue(cfqd, current->pid, gfp_mask);
1963 if (!cfqq)
1964 goto queue_fail;
1da177e4 1965
22e2c507
JA
1966 cic->cfqq = cfqq;
1967 } else
1968 cfqq = cic->cfqq;
1da177e4
LT
1969
1970 cfqq->allocated[rw]++;
22e2c507
JA
1971 cfqq->must_alloc = 0;
1972 cfqd->rq_starved = 0;
1973 atomic_inc(&cfqq->ref);
1da177e4
LT
1974 spin_unlock_irqrestore(q->queue_lock, flags);
1975
1da177e4
LT
1976 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1977 if (crq) {
1978 RB_CLEAR(&crq->rb_node);
1979 crq->rb_key = 0;
1980 crq->request = rq;
1981 INIT_HLIST_NODE(&crq->hash);
1982 crq->cfq_queue = cfqq;
1983 crq->io_context = cic;
22e2c507
JA
1984 crq->in_flight = crq->accounted = 0;
1985 crq->is_sync = (rw == READ || process_sync(current));
1986 crq->requeued = 0;
1da177e4 1987 rq->elevator_private = crq;
1da177e4
LT
1988 return 0;
1989 }
1990
1da177e4
LT
1991 spin_lock_irqsave(q->queue_lock, flags);
1992 cfqq->allocated[rw]--;
22e2c507
JA
1993 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
1994 cfqq->must_alloc = 1;
1da177e4 1995 cfq_put_queue(cfqq);
22e2c507
JA
1996queue_fail:
1997 if (cic)
1998 put_io_context(cic->ioc);
1999 /*
2000 * mark us rq allocation starved. we need to kickstart the process
2001 * ourselves if there are no pending requests that can do it for us.
2002 * that would be an extremely rare OOM situation
2003 */
2004 cfqd->rq_starved = 1;
2005 kblockd_schedule_work(&cfqd->unplug_work);
1da177e4
LT
2006 spin_unlock_irqrestore(q->queue_lock, flags);
2007 return 1;
2008}
2009
22e2c507
JA
2010static void cfq_kick_queue(void *data)
2011{
2012 request_queue_t *q = data;
2013 struct cfq_data *cfqd = q->elevator->elevator_data;
2014 unsigned long flags;
2015
2016 spin_lock_irqsave(q->queue_lock, flags);
2017
2018 if (cfqd->rq_starved) {
2019 struct request_list *rl = &q->rq;
2020
2021 /*
2022 * we aren't guaranteed to get a request after this, but we
2023 * have to be opportunistic
2024 */
2025 smp_mb();
2026 if (waitqueue_active(&rl->wait[READ]))
2027 wake_up(&rl->wait[READ]);
2028 if (waitqueue_active(&rl->wait[WRITE]))
2029 wake_up(&rl->wait[WRITE]);
2030 }
2031
2032 blk_remove_plug(q);
2033 q->request_fn(q);
2034 spin_unlock_irqrestore(q->queue_lock, flags);
2035}
2036
2037/*
2038 * Timer running if the active_queue is currently idling inside its time slice
2039 */
2040static void cfq_idle_slice_timer(unsigned long data)
2041{
2042 struct cfq_data *cfqd = (struct cfq_data *) data;
2043 struct cfq_queue *cfqq;
2044 unsigned long flags;
2045
2046 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2047
2048 if ((cfqq = cfqd->active_queue) != NULL) {
2049 unsigned long now = jiffies;
2050
2051 /*
2052 * expired
2053 */
2054 if (time_after(now, cfqq->slice_end))
2055 goto expire;
2056
2057 /*
2058 * only expire and reinvoke request handler, if there are
2059 * other queues with pending requests
2060 */
2061 if (!cfq_pending_requests(cfqd)) {
2062 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2063 add_timer(&cfqd->idle_slice_timer);
2064 goto out_cont;
2065 }
2066
2067 /*
2068 * not expired and it has a request pending, let it dispatch
2069 */
2070 if (!RB_EMPTY(&cfqq->sort_list)) {
2071 cfqq->must_dispatch = 1;
2072 goto out_kick;
2073 }
2074 }
2075expire:
2076 cfq_slice_expired(cfqd, 0);
2077out_kick:
2078 if (cfq_pending_requests(cfqd))
2079 kblockd_schedule_work(&cfqd->unplug_work);
2080out_cont:
2081 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2082}
2083
2084/*
2085 * Timer running if an idle class queue is waiting for service
2086 */
2087static void cfq_idle_class_timer(unsigned long data)
2088{
2089 struct cfq_data *cfqd = (struct cfq_data *) data;
2090 unsigned long flags, end;
2091
2092 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2093
2094 /*
2095 * race with a non-idle queue, reset timer
2096 */
2097 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2098 if (!time_after_eq(jiffies, end)) {
2099 cfqd->idle_class_timer.expires = end;
2100 add_timer(&cfqd->idle_class_timer);
2101 } else
2102 kblockd_schedule_work(&cfqd->unplug_work);
2103
2104 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2105}
2106
2107
1da177e4
LT
2108static void cfq_put_cfqd(struct cfq_data *cfqd)
2109{
2110 request_queue_t *q = cfqd->queue;
2111
2112 if (!atomic_dec_and_test(&cfqd->ref))
2113 return;
2114
22e2c507
JA
2115 blk_sync_queue(q);
2116
1da177e4
LT
2117 blk_put_queue(q);
2118
2119 mempool_destroy(cfqd->crq_pool);
2120 kfree(cfqd->crq_hash);
2121 kfree(cfqd->cfq_hash);
2122 kfree(cfqd);
2123}
2124
2125static void cfq_exit_queue(elevator_t *e)
2126{
22e2c507
JA
2127 struct cfq_data *cfqd = e->elevator_data;
2128
2129 del_timer_sync(&cfqd->idle_slice_timer);
2130 del_timer_sync(&cfqd->idle_class_timer);
2131 cfq_put_cfqd(cfqd);
1da177e4
LT
2132}
2133
2134static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2135{
2136 struct cfq_data *cfqd;
2137 int i;
2138
2139 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2140 if (!cfqd)
2141 return -ENOMEM;
2142
2143 memset(cfqd, 0, sizeof(*cfqd));
22e2c507
JA
2144
2145 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2146 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2147
2148 INIT_LIST_HEAD(&cfqd->busy_rr);
2149 INIT_LIST_HEAD(&cfqd->cur_rr);
2150 INIT_LIST_HEAD(&cfqd->idle_rr);
1da177e4
LT
2151 INIT_LIST_HEAD(&cfqd->empty_list);
2152
2153 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2154 if (!cfqd->crq_hash)
2155 goto out_crqhash;
2156
2157 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2158 if (!cfqd->cfq_hash)
2159 goto out_cfqhash;
2160
2161 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2162 if (!cfqd->crq_pool)
2163 goto out_crqpool;
2164
2165 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2166 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2167 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2168 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2169
2170 e->elevator_data = cfqd;
2171
2172 cfqd->queue = q;
2173 atomic_inc(&q->refcnt);
2174
22e2c507 2175 cfqd->max_queued = q->nr_requests / 4;
1da177e4 2176 q->nr_batching = cfq_queued;
22e2c507
JA
2177
2178 init_timer(&cfqd->idle_slice_timer);
2179 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2180 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2181
2182 init_timer(&cfqd->idle_class_timer);
2183 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2184 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2185
2186 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2187
1da177e4
LT
2188 atomic_set(&cfqd->ref, 1);
2189
2190 cfqd->cfq_queued = cfq_queued;
2191 cfqd->cfq_quantum = cfq_quantum;
22e2c507
JA
2192 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2193 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
1da177e4
LT
2194 cfqd->cfq_back_max = cfq_back_max;
2195 cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507
JA
2196 cfqd->cfq_slice[0] = cfq_slice_async;
2197 cfqd->cfq_slice[1] = cfq_slice_sync;
2198 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2199 cfqd->cfq_slice_idle = cfq_slice_idle;
2200 cfqd->cfq_max_depth = cfq_max_depth;
1da177e4
LT
2201 return 0;
2202out_crqpool:
2203 kfree(cfqd->cfq_hash);
2204out_cfqhash:
2205 kfree(cfqd->crq_hash);
2206out_crqhash:
2207 kfree(cfqd);
2208 return -ENOMEM;
2209}
2210
2211static void cfq_slab_kill(void)
2212{
2213 if (crq_pool)
2214 kmem_cache_destroy(crq_pool);
2215 if (cfq_pool)
2216 kmem_cache_destroy(cfq_pool);
2217 if (cfq_ioc_pool)
2218 kmem_cache_destroy(cfq_ioc_pool);
2219}
2220
2221static int __init cfq_slab_setup(void)
2222{
2223 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2224 NULL, NULL);
2225 if (!crq_pool)
2226 goto fail;
2227
2228 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2229 NULL, NULL);
2230 if (!cfq_pool)
2231 goto fail;
2232
2233 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2234 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2235 if (!cfq_ioc_pool)
2236 goto fail;
2237
2238 return 0;
2239fail:
2240 cfq_slab_kill();
2241 return -ENOMEM;
2242}
2243
1da177e4
LT
2244/*
2245 * sysfs parts below -->
2246 */
2247struct cfq_fs_entry {
2248 struct attribute attr;
2249 ssize_t (*show)(struct cfq_data *, char *);
2250 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2251};
2252
2253static ssize_t
2254cfq_var_show(unsigned int var, char *page)
2255{
2256 return sprintf(page, "%d\n", var);
2257}
2258
2259static ssize_t
2260cfq_var_store(unsigned int *var, const char *page, size_t count)
2261{
2262 char *p = (char *) page;
2263
2264 *var = simple_strtoul(p, &p, 10);
2265 return count;
2266}
2267
1da177e4
LT
2268#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2269static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2270{ \
2271 unsigned int __data = __VAR; \
2272 if (__CONV) \
2273 __data = jiffies_to_msecs(__data); \
2274 return cfq_var_show(__data, (page)); \
2275}
2276SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2277SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
22e2c507
JA
2278SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2279SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
1da177e4
LT
2280SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2281SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
22e2c507
JA
2282SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2283SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2284SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2285SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2286SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
1da177e4
LT
2287#undef SHOW_FUNCTION
2288
2289#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2290static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2291{ \
2292 unsigned int __data; \
2293 int ret = cfq_var_store(&__data, (page), count); \
2294 if (__data < (MIN)) \
2295 __data = (MIN); \
2296 else if (__data > (MAX)) \
2297 __data = (MAX); \
2298 if (__CONV) \
2299 *(__PTR) = msecs_to_jiffies(__data); \
2300 else \
2301 *(__PTR) = __data; \
2302 return ret; \
2303}
2304STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2305STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
22e2c507
JA
2306STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2307STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
1da177e4
LT
2308STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2309STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
22e2c507
JA
2310STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2311STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2312STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2313STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2314STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
1da177e4
LT
2315#undef STORE_FUNCTION
2316
2317static struct cfq_fs_entry cfq_quantum_entry = {
2318 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2319 .show = cfq_quantum_show,
2320 .store = cfq_quantum_store,
2321};
2322static struct cfq_fs_entry cfq_queued_entry = {
2323 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2324 .show = cfq_queued_show,
2325 .store = cfq_queued_store,
2326};
22e2c507 2327static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
1da177e4 2328 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
22e2c507
JA
2329 .show = cfq_fifo_expire_sync_show,
2330 .store = cfq_fifo_expire_sync_store,
1da177e4 2331};
22e2c507 2332static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
1da177e4 2333 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
22e2c507
JA
2334 .show = cfq_fifo_expire_async_show,
2335 .store = cfq_fifo_expire_async_store,
1da177e4
LT
2336};
2337static struct cfq_fs_entry cfq_back_max_entry = {
2338 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2339 .show = cfq_back_max_show,
2340 .store = cfq_back_max_store,
2341};
2342static struct cfq_fs_entry cfq_back_penalty_entry = {
2343 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2344 .show = cfq_back_penalty_show,
2345 .store = cfq_back_penalty_store,
2346};
22e2c507
JA
2347static struct cfq_fs_entry cfq_slice_sync_entry = {
2348 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2349 .show = cfq_slice_sync_show,
2350 .store = cfq_slice_sync_store,
1da177e4 2351};
22e2c507
JA
2352static struct cfq_fs_entry cfq_slice_async_entry = {
2353 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2354 .show = cfq_slice_async_show,
2355 .store = cfq_slice_async_store,
2356};
2357static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2358 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2359 .show = cfq_slice_async_rq_show,
2360 .store = cfq_slice_async_rq_store,
2361};
2362static struct cfq_fs_entry cfq_slice_idle_entry = {
2363 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2364 .show = cfq_slice_idle_show,
2365 .store = cfq_slice_idle_store,
2366};
2367static struct cfq_fs_entry cfq_max_depth_entry = {
2368 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2369 .show = cfq_max_depth_show,
2370 .store = cfq_max_depth_store,
1da177e4 2371};
1da177e4
LT
2372static struct attribute *default_attrs[] = {
2373 &cfq_quantum_entry.attr,
2374 &cfq_queued_entry.attr,
22e2c507
JA
2375 &cfq_fifo_expire_sync_entry.attr,
2376 &cfq_fifo_expire_async_entry.attr,
1da177e4
LT
2377 &cfq_back_max_entry.attr,
2378 &cfq_back_penalty_entry.attr,
22e2c507
JA
2379 &cfq_slice_sync_entry.attr,
2380 &cfq_slice_async_entry.attr,
2381 &cfq_slice_async_rq_entry.attr,
2382 &cfq_slice_idle_entry.attr,
2383 &cfq_max_depth_entry.attr,
1da177e4
LT
2384 NULL,
2385};
2386
2387#define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2388
2389static ssize_t
2390cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2391{
2392 elevator_t *e = container_of(kobj, elevator_t, kobj);
2393 struct cfq_fs_entry *entry = to_cfq(attr);
2394
2395 if (!entry->show)
6c1852a0 2396 return -EIO;
1da177e4
LT
2397
2398 return entry->show(e->elevator_data, page);
2399}
2400
2401static ssize_t
2402cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2403 const char *page, size_t length)
2404{
2405 elevator_t *e = container_of(kobj, elevator_t, kobj);
2406 struct cfq_fs_entry *entry = to_cfq(attr);
2407
2408 if (!entry->store)
6c1852a0 2409 return -EIO;
1da177e4
LT
2410
2411 return entry->store(e->elevator_data, page, length);
2412}
2413
2414static struct sysfs_ops cfq_sysfs_ops = {
2415 .show = cfq_attr_show,
2416 .store = cfq_attr_store,
2417};
2418
2419static struct kobj_type cfq_ktype = {
2420 .sysfs_ops = &cfq_sysfs_ops,
2421 .default_attrs = default_attrs,
2422};
2423
2424static struct elevator_type iosched_cfq = {
2425 .ops = {
2426 .elevator_merge_fn = cfq_merge,
2427 .elevator_merged_fn = cfq_merged_request,
2428 .elevator_merge_req_fn = cfq_merged_requests,
2429 .elevator_next_req_fn = cfq_next_request,
2430 .elevator_add_req_fn = cfq_insert_request,
2431 .elevator_remove_req_fn = cfq_remove_request,
2432 .elevator_requeue_req_fn = cfq_requeue_request,
2433 .elevator_deactivate_req_fn = cfq_deactivate_request,
2434 .elevator_queue_empty_fn = cfq_queue_empty,
2435 .elevator_completed_req_fn = cfq_completed_request,
2436 .elevator_former_req_fn = cfq_former_request,
2437 .elevator_latter_req_fn = cfq_latter_request,
2438 .elevator_set_req_fn = cfq_set_request,
2439 .elevator_put_req_fn = cfq_put_request,
2440 .elevator_may_queue_fn = cfq_may_queue,
2441 .elevator_init_fn = cfq_init_queue,
2442 .elevator_exit_fn = cfq_exit_queue,
2443 },
2444 .elevator_ktype = &cfq_ktype,
2445 .elevator_name = "cfq",
2446 .elevator_owner = THIS_MODULE,
2447};
2448
2449static int __init cfq_init(void)
2450{
2451 int ret;
2452
22e2c507
JA
2453 /*
2454 * could be 0 on HZ < 1000 setups
2455 */
2456 if (!cfq_slice_async)
2457 cfq_slice_async = 1;
2458 if (!cfq_slice_idle)
2459 cfq_slice_idle = 1;
2460
1da177e4
LT
2461 if (cfq_slab_setup())
2462 return -ENOMEM;
2463
2464 ret = elv_register(&iosched_cfq);
22e2c507
JA
2465 if (ret)
2466 cfq_slab_kill();
1da177e4 2467
1da177e4
LT
2468 return ret;
2469}
2470
2471static void __exit cfq_exit(void)
2472{
22e2c507
JA
2473 struct task_struct *g, *p;
2474 unsigned long flags;
2475
2476 read_lock_irqsave(&tasklist_lock, flags);
2477
2478 /*
2479 * iterate each process in the system, removing our io_context
2480 */
2481 do_each_thread(g, p) {
2482 struct io_context *ioc = p->io_context;
2483
2484 if (ioc && ioc->cic) {
2485 ioc->cic->exit(ioc->cic);
2486 cfq_free_io_context(ioc->cic);
2487 ioc->cic = NULL;
2488 }
2489 } while_each_thread(g, p);
2490
2491 read_unlock_irqrestore(&tasklist_lock, flags);
2492
1da177e4
LT
2493 cfq_slab_kill();
2494 elv_unregister(&iosched_cfq);
2495}
2496
2497module_init(cfq_init);
2498module_exit(cfq_exit);
2499
2500MODULE_AUTHOR("Jens Axboe");
2501MODULE_LICENSE("GPL");
2502MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");