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