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