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