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