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