Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394/linux1...
[deliverable/linux.git] / block / elevator.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Block device elevator/IO-scheduler.
3 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 *
0fe23479 6 * 30042000 Jens Axboe <axboe@kernel.dk> :
1da177e4
LT
7 *
8 * Split the elevator a bit so that it is possible to choose a different
9 * one or even write a new "plug in". There are three pieces:
10 * - elevator_fn, inserts a new request in the queue list
11 * - elevator_merge_fn, decides whether a new buffer can be merged with
12 * an existing request
13 * - elevator_dequeue_fn, called when a request is taken off the active list
14 *
15 * 20082000 Dave Jones <davej@suse.de> :
16 * Removed tests for max-bomb-segments, which was breaking elvtune
17 * when run without -bN
18 *
19 * Jens:
20 * - Rework again to work with bio instead of buffer_heads
21 * - loose bi_dev comparisons, partition handling is right now
22 * - completely modularize elevator setup and teardown
23 *
24 */
25#include <linux/kernel.h>
26#include <linux/fs.h>
27#include <linux/blkdev.h>
28#include <linux/elevator.h>
29#include <linux/bio.h>
1da177e4
LT
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/init.h>
33#include <linux/compiler.h>
cb98fc8b 34#include <linux/delay.h>
2056a782 35#include <linux/blktrace_api.h>
9817064b 36#include <linux/hash.h>
0835da67 37#include <linux/uaccess.h>
1da177e4 38
55782138
LZ
39#include <trace/events/block.h>
40
242f9dcb
JA
41#include "blk.h"
42
1da177e4
LT
43static DEFINE_SPINLOCK(elv_list_lock);
44static LIST_HEAD(elv_list);
45
9817064b
JA
46/*
47 * Merge hash stuff.
48 */
49static const int elv_hash_shift = 6;
50#define ELV_HASH_BLOCK(sec) ((sec) >> 3)
4eb166d9
JA
51#define ELV_HASH_FN(sec) \
52 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
9817064b 53#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
83096ebf 54#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
9817064b 55
da775265
JA
56/*
57 * Query io scheduler to see if the current process issuing bio may be
58 * merged with rq.
59 */
60static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
61{
165125e1 62 struct request_queue *q = rq->q;
b374d18a 63 struct elevator_queue *e = q->elevator;
da775265
JA
64
65 if (e->ops->elevator_allow_merge_fn)
66 return e->ops->elevator_allow_merge_fn(q, rq, bio);
67
68 return 1;
69}
70
1da177e4
LT
71/*
72 * can we safely merge with this request?
73 */
72ed0bf6 74int elv_rq_merge_ok(struct request *rq, struct bio *bio)
1da177e4
LT
75{
76 if (!rq_mergeable(rq))
77 return 0;
78
e17fc0a1
DW
79 /*
80 * Don't merge file system requests and discard requests
81 */
7b6d91da 82 if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
e17fc0a1
DW
83 return 0;
84
8d57a98c
AH
85 /*
86 * Don't merge discard requests and secure discard requests
87 */
88 if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
89 return 0;
90
1da177e4
LT
91 /*
92 * different data direction or already started, don't merge
93 */
94 if (bio_data_dir(bio) != rq_data_dir(rq))
95 return 0;
96
97 /*
da775265 98 * must be same device and not a special request
1da177e4 99 */
bb4067e3 100 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
da775265
JA
101 return 0;
102
7ba1ba12
MP
103 /*
104 * only merge integrity protected bio into ditto rq
105 */
106 if (bio_integrity(bio) != blk_integrity_rq(rq))
107 return 0;
108
da775265
JA
109 if (!elv_iosched_allow_merge(rq, bio))
110 return 0;
1da177e4 111
da775265 112 return 1;
1da177e4
LT
113}
114EXPORT_SYMBOL(elv_rq_merge_ok);
115
73c10101 116int elv_try_merge(struct request *__rq, struct bio *bio)
1da177e4
LT
117{
118 int ret = ELEVATOR_NO_MERGE;
119
120 /*
121 * we can merge and sequence is ok, check if it's possible
122 */
123 if (elv_rq_merge_ok(__rq, bio)) {
83096ebf 124 if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector)
1da177e4 125 ret = ELEVATOR_BACK_MERGE;
83096ebf 126 else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector)
1da177e4
LT
127 ret = ELEVATOR_FRONT_MERGE;
128 }
129
130 return ret;
131}
1da177e4 132
1da177e4
LT
133static struct elevator_type *elevator_find(const char *name)
134{
a22b169d 135 struct elevator_type *e;
1da177e4 136
70cee26e 137 list_for_each_entry(e, &elv_list, list) {
a22b169d
VT
138 if (!strcmp(e->elevator_name, name))
139 return e;
1da177e4 140 }
1da177e4 141
a22b169d 142 return NULL;
1da177e4
LT
143}
144
145static void elevator_put(struct elevator_type *e)
146{
147 module_put(e->elevator_owner);
148}
149
150static struct elevator_type *elevator_get(const char *name)
151{
2824bc93 152 struct elevator_type *e;
1da177e4 153
2a12dcd7 154 spin_lock(&elv_list_lock);
2824bc93
TH
155
156 e = elevator_find(name);
e1640949
JA
157 if (!e) {
158 char elv[ELV_NAME_MAX + strlen("-iosched")];
159
160 spin_unlock(&elv_list_lock);
161
a506aedc 162 snprintf(elv, sizeof(elv), "%s-iosched", name);
e1640949 163
e180f594 164 request_module("%s", elv);
e1640949
JA
165 spin_lock(&elv_list_lock);
166 e = elevator_find(name);
167 }
168
2824bc93
TH
169 if (e && !try_module_get(e->elevator_owner))
170 e = NULL;
171
2a12dcd7 172 spin_unlock(&elv_list_lock);
1da177e4
LT
173
174 return e;
175}
176
165125e1
JA
177static void *elevator_init_queue(struct request_queue *q,
178 struct elevator_queue *eq)
1da177e4 179{
bb37b94c 180 return eq->ops->elevator_init_fn(q);
bc1c1169 181}
1da177e4 182
165125e1 183static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
bc1c1169
JA
184 void *data)
185{
1da177e4 186 q->elevator = eq;
bc1c1169 187 eq->elevator_data = data;
1da177e4
LT
188}
189
190static char chosen_elevator[16];
191
5f003976 192static int __init elevator_setup(char *str)
1da177e4 193{
752a3b79
CE
194 /*
195 * Be backwards-compatible with previous kernels, so users
196 * won't get the wrong elevator.
197 */
492af635 198 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
9b41046c 199 return 1;
1da177e4
LT
200}
201
202__setup("elevator=", elevator_setup);
203
3d1ab40f
AV
204static struct kobj_type elv_ktype;
205
b374d18a 206static struct elevator_queue *elevator_alloc(struct request_queue *q,
165125e1 207 struct elevator_type *e)
3d1ab40f 208{
b374d18a 209 struct elevator_queue *eq;
9817064b
JA
210 int i;
211
b374d18a 212 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
9817064b
JA
213 if (unlikely(!eq))
214 goto err;
215
9817064b
JA
216 eq->ops = &e->ops;
217 eq->elevator_type = e;
f9cb074b 218 kobject_init(&eq->kobj, &elv_ktype);
9817064b
JA
219 mutex_init(&eq->sysfs_lock);
220
b5deef90
JA
221 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
222 GFP_KERNEL, q->node);
9817064b
JA
223 if (!eq->hash)
224 goto err;
225
226 for (i = 0; i < ELV_HASH_ENTRIES; i++)
227 INIT_HLIST_HEAD(&eq->hash[i]);
228
3d1ab40f 229 return eq;
9817064b
JA
230err:
231 kfree(eq);
232 elevator_put(e);
233 return NULL;
3d1ab40f
AV
234}
235
236static void elevator_release(struct kobject *kobj)
237{
b374d18a 238 struct elevator_queue *e;
9817064b 239
b374d18a 240 e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f 241 elevator_put(e->elevator_type);
9817064b 242 kfree(e->hash);
3d1ab40f
AV
243 kfree(e);
244}
245
165125e1 246int elevator_init(struct request_queue *q, char *name)
1da177e4
LT
247{
248 struct elevator_type *e = NULL;
249 struct elevator_queue *eq;
bc1c1169 250 void *data;
1da177e4 251
1abec4fd
MS
252 if (unlikely(q->elevator))
253 return 0;
254
cb98fc8b
TH
255 INIT_LIST_HEAD(&q->queue_head);
256 q->last_merge = NULL;
257 q->end_sector = 0;
258 q->boundary_rq = NULL;
cb98fc8b 259
4eb166d9
JA
260 if (name) {
261 e = elevator_get(name);
262 if (!e)
263 return -EINVAL;
264 }
1da177e4 265
4eb166d9
JA
266 if (!e && *chosen_elevator) {
267 e = elevator_get(chosen_elevator);
268 if (!e)
269 printk(KERN_ERR "I/O scheduler %s not found\n",
270 chosen_elevator);
271 }
248d5ca5 272
4eb166d9
JA
273 if (!e) {
274 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
275 if (!e) {
276 printk(KERN_ERR
277 "Default I/O scheduler not found. " \
278 "Using noop.\n");
279 e = elevator_get("noop");
280 }
5f003976
ND
281 }
282
b5deef90 283 eq = elevator_alloc(q, e);
3d1ab40f 284 if (!eq)
1da177e4 285 return -ENOMEM;
1da177e4 286
bc1c1169
JA
287 data = elevator_init_queue(q, eq);
288 if (!data) {
3d1ab40f 289 kobject_put(&eq->kobj);
bc1c1169
JA
290 return -ENOMEM;
291 }
1da177e4 292
bc1c1169 293 elevator_attach(q, eq, data);
1abec4fd 294 return 0;
1da177e4 295}
2e662b65
JA
296EXPORT_SYMBOL(elevator_init);
297
b374d18a 298void elevator_exit(struct elevator_queue *e)
1da177e4 299{
3d1ab40f 300 mutex_lock(&e->sysfs_lock);
1da177e4
LT
301 if (e->ops->elevator_exit_fn)
302 e->ops->elevator_exit_fn(e);
3d1ab40f
AV
303 e->ops = NULL;
304 mutex_unlock(&e->sysfs_lock);
1da177e4 305
3d1ab40f 306 kobject_put(&e->kobj);
1da177e4 307}
2e662b65
JA
308EXPORT_SYMBOL(elevator_exit);
309
9817064b
JA
310static inline void __elv_rqhash_del(struct request *rq)
311{
312 hlist_del_init(&rq->hash);
313}
314
165125e1 315static void elv_rqhash_del(struct request_queue *q, struct request *rq)
9817064b
JA
316{
317 if (ELV_ON_HASH(rq))
318 __elv_rqhash_del(rq);
319}
320
165125e1 321static void elv_rqhash_add(struct request_queue *q, struct request *rq)
9817064b 322{
b374d18a 323 struct elevator_queue *e = q->elevator;
9817064b
JA
324
325 BUG_ON(ELV_ON_HASH(rq));
326 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
327}
328
165125e1 329static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
9817064b
JA
330{
331 __elv_rqhash_del(rq);
332 elv_rqhash_add(q, rq);
333}
334
165125e1 335static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
9817064b 336{
b374d18a 337 struct elevator_queue *e = q->elevator;
9817064b
JA
338 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
339 struct hlist_node *entry, *next;
340 struct request *rq;
341
342 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
343 BUG_ON(!ELV_ON_HASH(rq));
344
345 if (unlikely(!rq_mergeable(rq))) {
346 __elv_rqhash_del(rq);
347 continue;
348 }
349
350 if (rq_hash_key(rq) == offset)
351 return rq;
352 }
353
354 return NULL;
355}
356
2e662b65
JA
357/*
358 * RB-tree support functions for inserting/lookup/removal of requests
359 * in a sorted RB tree.
360 */
361struct request *elv_rb_add(struct rb_root *root, struct request *rq)
362{
363 struct rb_node **p = &root->rb_node;
364 struct rb_node *parent = NULL;
365 struct request *__rq;
366
367 while (*p) {
368 parent = *p;
369 __rq = rb_entry(parent, struct request, rb_node);
370
83096ebf 371 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
2e662b65 372 p = &(*p)->rb_left;
83096ebf 373 else if (blk_rq_pos(rq) > blk_rq_pos(__rq))
2e662b65
JA
374 p = &(*p)->rb_right;
375 else
376 return __rq;
377 }
378
379 rb_link_node(&rq->rb_node, parent, p);
380 rb_insert_color(&rq->rb_node, root);
381 return NULL;
382}
2e662b65
JA
383EXPORT_SYMBOL(elv_rb_add);
384
385void elv_rb_del(struct rb_root *root, struct request *rq)
386{
387 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
388 rb_erase(&rq->rb_node, root);
389 RB_CLEAR_NODE(&rq->rb_node);
390}
2e662b65
JA
391EXPORT_SYMBOL(elv_rb_del);
392
393struct request *elv_rb_find(struct rb_root *root, sector_t sector)
394{
395 struct rb_node *n = root->rb_node;
396 struct request *rq;
397
398 while (n) {
399 rq = rb_entry(n, struct request, rb_node);
400
83096ebf 401 if (sector < blk_rq_pos(rq))
2e662b65 402 n = n->rb_left;
83096ebf 403 else if (sector > blk_rq_pos(rq))
2e662b65
JA
404 n = n->rb_right;
405 else
406 return rq;
407 }
408
409 return NULL;
410}
2e662b65
JA
411EXPORT_SYMBOL(elv_rb_find);
412
8922e16c
TH
413/*
414 * Insert rq into dispatch queue of q. Queue lock must be held on
dbe7f76d 415 * entry. rq is sort instead into the dispatch queue. To be used by
2e662b65 416 * specific elevators.
8922e16c 417 */
165125e1 418void elv_dispatch_sort(struct request_queue *q, struct request *rq)
8922e16c
TH
419{
420 sector_t boundary;
8922e16c 421 struct list_head *entry;
4eb166d9 422 int stop_flags;
8922e16c 423
73c10101
JA
424 BUG_ON(rq->cmd_flags & REQ_ON_PLUG);
425
06b86245
TH
426 if (q->last_merge == rq)
427 q->last_merge = NULL;
9817064b
JA
428
429 elv_rqhash_del(q, rq);
430
15853af9 431 q->nr_sorted--;
06b86245 432
1b47f531 433 boundary = q->end_sector;
02e031cb 434 stop_flags = REQ_SOFTBARRIER | REQ_STARTED;
8922e16c
TH
435 list_for_each_prev(entry, &q->queue_head) {
436 struct request *pos = list_entry_rq(entry);
437
33659ebb
CH
438 if ((rq->cmd_flags & REQ_DISCARD) !=
439 (pos->cmd_flags & REQ_DISCARD))
e17fc0a1 440 break;
783660b2
JA
441 if (rq_data_dir(rq) != rq_data_dir(pos))
442 break;
4eb166d9 443 if (pos->cmd_flags & stop_flags)
8922e16c 444 break;
83096ebf
TH
445 if (blk_rq_pos(rq) >= boundary) {
446 if (blk_rq_pos(pos) < boundary)
8922e16c
TH
447 continue;
448 } else {
83096ebf 449 if (blk_rq_pos(pos) >= boundary)
8922e16c
TH
450 break;
451 }
83096ebf 452 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
8922e16c
TH
453 break;
454 }
455
456 list_add(&rq->queuelist, entry);
457}
2e662b65
JA
458EXPORT_SYMBOL(elv_dispatch_sort);
459
9817064b 460/*
2e662b65
JA
461 * Insert rq into dispatch queue of q. Queue lock must be held on
462 * entry. rq is added to the back of the dispatch queue. To be used by
463 * specific elevators.
9817064b
JA
464 */
465void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
466{
467 if (q->last_merge == rq)
468 q->last_merge = NULL;
469
470 elv_rqhash_del(q, rq);
471
472 q->nr_sorted--;
473
474 q->end_sector = rq_end_sector(rq);
475 q->boundary_rq = rq;
476 list_add_tail(&rq->queuelist, &q->queue_head);
477}
2e662b65
JA
478EXPORT_SYMBOL(elv_dispatch_add_tail);
479
165125e1 480int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
1da177e4 481{
b374d18a 482 struct elevator_queue *e = q->elevator;
9817064b 483 struct request *__rq;
06b86245
TH
484 int ret;
485
488991e2
AB
486 /*
487 * Levels of merges:
488 * nomerges: No merges at all attempted
489 * noxmerges: Only simple one-hit cache try
490 * merges: All merge tries attempted
491 */
492 if (blk_queue_nomerges(q))
493 return ELEVATOR_NO_MERGE;
494
9817064b
JA
495 /*
496 * First try one-hit cache.
497 */
06b86245
TH
498 if (q->last_merge) {
499 ret = elv_try_merge(q->last_merge, bio);
500 if (ret != ELEVATOR_NO_MERGE) {
501 *req = q->last_merge;
502 return ret;
503 }
504 }
1da177e4 505
488991e2 506 if (blk_queue_noxmerges(q))
ac9fafa1
AB
507 return ELEVATOR_NO_MERGE;
508
9817064b
JA
509 /*
510 * See if our hash lookup can find a potential backmerge.
511 */
512 __rq = elv_rqhash_find(q, bio->bi_sector);
513 if (__rq && elv_rq_merge_ok(__rq, bio)) {
514 *req = __rq;
515 return ELEVATOR_BACK_MERGE;
516 }
517
1da177e4
LT
518 if (e->ops->elevator_merge_fn)
519 return e->ops->elevator_merge_fn(q, req, bio);
520
521 return ELEVATOR_NO_MERGE;
522}
523
5e84ea3a
JA
524/*
525 * Attempt to do an insertion back merge. Only check for the case where
526 * we can append 'rq' to an existing request, so we can throw 'rq' away
527 * afterwards.
528 *
529 * Returns true if we merged, false otherwise
530 */
531static bool elv_attempt_insert_merge(struct request_queue *q,
532 struct request *rq)
533{
534 struct request *__rq;
535
536 if (blk_queue_nomerges(q))
537 return false;
538
539 /*
540 * First try one-hit cache.
541 */
542 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
543 return true;
544
545 if (blk_queue_noxmerges(q))
546 return false;
547
548 /*
549 * See if our hash lookup can find a potential backmerge.
550 */
551 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
552 if (__rq && blk_attempt_req_merge(q, __rq, rq))
553 return true;
554
555 return false;
556}
557
165125e1 558void elv_merged_request(struct request_queue *q, struct request *rq, int type)
1da177e4 559{
b374d18a 560 struct elevator_queue *e = q->elevator;
1da177e4
LT
561
562 if (e->ops->elevator_merged_fn)
2e662b65 563 e->ops->elevator_merged_fn(q, rq, type);
06b86245 564
2e662b65
JA
565 if (type == ELEVATOR_BACK_MERGE)
566 elv_rqhash_reposition(q, rq);
9817064b 567
06b86245 568 q->last_merge = rq;
1da177e4
LT
569}
570
165125e1 571void elv_merge_requests(struct request_queue *q, struct request *rq,
1da177e4
LT
572 struct request *next)
573{
b374d18a 574 struct elevator_queue *e = q->elevator;
5e84ea3a 575 const int next_sorted = next->cmd_flags & REQ_SORTED;
1da177e4 576
5e84ea3a 577 if (next_sorted && e->ops->elevator_merge_req_fn)
1da177e4 578 e->ops->elevator_merge_req_fn(q, rq, next);
06b86245 579
9817064b 580 elv_rqhash_reposition(q, rq);
9817064b 581
5e84ea3a
JA
582 if (next_sorted) {
583 elv_rqhash_del(q, next);
584 q->nr_sorted--;
585 }
586
06b86245 587 q->last_merge = rq;
1da177e4
LT
588}
589
812d4026
DS
590void elv_bio_merged(struct request_queue *q, struct request *rq,
591 struct bio *bio)
592{
593 struct elevator_queue *e = q->elevator;
594
595 if (e->ops->elevator_bio_merged_fn)
596 e->ops->elevator_bio_merged_fn(q, rq, bio);
597}
598
165125e1 599void elv_requeue_request(struct request_queue *q, struct request *rq)
1da177e4 600{
1da177e4
LT
601 /*
602 * it already went through dequeue, we need to decrement the
603 * in_flight count again
604 */
8922e16c 605 if (blk_account_rq(rq)) {
0a7ae2ff 606 q->in_flight[rq_is_sync(rq)]--;
33659ebb 607 if (rq->cmd_flags & REQ_SORTED)
cad97516 608 elv_deactivate_rq(q, rq);
8922e16c 609 }
1da177e4 610
4aff5e23 611 rq->cmd_flags &= ~REQ_STARTED;
1da177e4 612
b710a480 613 __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
1da177e4
LT
614}
615
26308eab 616void elv_drain_elevator(struct request_queue *q)
15853af9
TH
617{
618 static int printed;
619 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
620 ;
621 if (q->nr_sorted == 0)
622 return;
623 if (printed++ < 10) {
624 printk(KERN_ERR "%s: forced dispatching is broken "
625 "(nr_sorted=%u), please report this\n",
626 q->elevator->elevator_type->elevator_name, q->nr_sorted);
627 }
628}
629
6c7e8cee
JA
630/*
631 * Call with queue lock held, interrupts disabled
632 */
f600abe2 633void elv_quiesce_start(struct request_queue *q)
6c7e8cee 634{
cd43e26f
MP
635 if (!q->elevator)
636 return;
637
6c7e8cee
JA
638 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
639
640 /*
641 * make sure we don't have any requests in flight
642 */
643 elv_drain_elevator(q);
644 while (q->rq.elvpriv) {
24ecfbe2 645 __blk_run_queue(q);
6c7e8cee
JA
646 spin_unlock_irq(q->queue_lock);
647 msleep(10);
648 spin_lock_irq(q->queue_lock);
649 elv_drain_elevator(q);
650 }
651}
652
f600abe2 653void elv_quiesce_end(struct request_queue *q)
6c7e8cee
JA
654{
655 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
656}
657
b710a480 658void __elv_add_request(struct request_queue *q, struct request *rq, int where)
1da177e4 659{
5f3ea37c 660 trace_block_rq_insert(q, rq);
2056a782 661
1da177e4
LT
662 rq->q = q;
663
b710a480
JA
664 BUG_ON(rq->cmd_flags & REQ_ON_PLUG);
665
666 if (rq->cmd_flags & REQ_SOFTBARRIER) {
667 /* barriers are scheduling boundary, update end_sector */
668 if (rq->cmd_type == REQ_TYPE_FS ||
669 (rq->cmd_flags & REQ_DISCARD)) {
670 q->end_sector = rq_end_sector(rq);
671 q->boundary_rq = rq;
672 }
673 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
3aa72873
JA
674 (where == ELEVATOR_INSERT_SORT ||
675 where == ELEVATOR_INSERT_SORT_MERGE))
b710a480
JA
676 where = ELEVATOR_INSERT_BACK;
677
8922e16c 678 switch (where) {
28e7d184 679 case ELEVATOR_INSERT_REQUEUE:
8922e16c 680 case ELEVATOR_INSERT_FRONT:
4aff5e23 681 rq->cmd_flags |= REQ_SOFTBARRIER;
8922e16c
TH
682 list_add(&rq->queuelist, &q->queue_head);
683 break;
684
685 case ELEVATOR_INSERT_BACK:
4aff5e23 686 rq->cmd_flags |= REQ_SOFTBARRIER;
15853af9 687 elv_drain_elevator(q);
8922e16c
TH
688 list_add_tail(&rq->queuelist, &q->queue_head);
689 /*
690 * We kick the queue here for the following reasons.
691 * - The elevator might have returned NULL previously
692 * to delay requests and returned them now. As the
693 * queue wasn't empty before this request, ll_rw_blk
694 * won't run the queue on return, resulting in hang.
695 * - Usually, back inserted requests won't be merged
696 * with anything. There's no point in delaying queue
697 * processing.
698 */
24ecfbe2 699 __blk_run_queue(q);
8922e16c
TH
700 break;
701
5e84ea3a
JA
702 case ELEVATOR_INSERT_SORT_MERGE:
703 /*
704 * If we succeed in merging this request with one in the
705 * queue already, we are done - rq has now been freed,
706 * so no need to do anything further.
707 */
708 if (elv_attempt_insert_merge(q, rq))
709 break;
8922e16c 710 case ELEVATOR_INSERT_SORT:
33659ebb
CH
711 BUG_ON(rq->cmd_type != REQ_TYPE_FS &&
712 !(rq->cmd_flags & REQ_DISCARD));
4aff5e23 713 rq->cmd_flags |= REQ_SORTED;
15853af9 714 q->nr_sorted++;
9817064b
JA
715 if (rq_mergeable(rq)) {
716 elv_rqhash_add(q, rq);
717 if (!q->last_merge)
718 q->last_merge = rq;
719 }
720
ca23509f
TH
721 /*
722 * Some ioscheds (cfq) run q->request_fn directly, so
723 * rq cannot be accessed after calling
724 * elevator_add_req_fn.
725 */
726 q->elevator->ops->elevator_add_req_fn(q, rq);
8922e16c
TH
727 break;
728
ae1b1539
TH
729 case ELEVATOR_INSERT_FLUSH:
730 rq->cmd_flags |= REQ_SOFTBARRIER;
731 blk_insert_flush(rq);
732 break;
8922e16c
TH
733 default:
734 printk(KERN_ERR "%s: bad insertion point %d\n",
24c03d47 735 __func__, where);
8922e16c
TH
736 BUG();
737 }
1da177e4 738}
2e662b65
JA
739EXPORT_SYMBOL(__elv_add_request);
740
7eaceacc 741void elv_add_request(struct request_queue *q, struct request *rq, int where)
1da177e4
LT
742{
743 unsigned long flags;
744
745 spin_lock_irqsave(q->queue_lock, flags);
7eaceacc 746 __elv_add_request(q, rq, where);
1da177e4
LT
747 spin_unlock_irqrestore(q->queue_lock, flags);
748}
2e662b65
JA
749EXPORT_SYMBOL(elv_add_request);
750
165125e1 751struct request *elv_latter_request(struct request_queue *q, struct request *rq)
1da177e4 752{
b374d18a 753 struct elevator_queue *e = q->elevator;
1da177e4
LT
754
755 if (e->ops->elevator_latter_req_fn)
756 return e->ops->elevator_latter_req_fn(q, rq);
1da177e4
LT
757 return NULL;
758}
759
165125e1 760struct request *elv_former_request(struct request_queue *q, struct request *rq)
1da177e4 761{
b374d18a 762 struct elevator_queue *e = q->elevator;
1da177e4
LT
763
764 if (e->ops->elevator_former_req_fn)
765 return e->ops->elevator_former_req_fn(q, rq);
1da177e4
LT
766 return NULL;
767}
768
165125e1 769int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
1da177e4 770{
b374d18a 771 struct elevator_queue *e = q->elevator;
1da177e4
LT
772
773 if (e->ops->elevator_set_req_fn)
cb78b285 774 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
1da177e4 775
c186794d 776 rq->elevator_private[0] = NULL;
1da177e4
LT
777 return 0;
778}
779
165125e1 780void elv_put_request(struct request_queue *q, struct request *rq)
1da177e4 781{
b374d18a 782 struct elevator_queue *e = q->elevator;
1da177e4
LT
783
784 if (e->ops->elevator_put_req_fn)
bb37b94c 785 e->ops->elevator_put_req_fn(rq);
1da177e4
LT
786}
787
165125e1 788int elv_may_queue(struct request_queue *q, int rw)
1da177e4 789{
b374d18a 790 struct elevator_queue *e = q->elevator;
1da177e4
LT
791
792 if (e->ops->elevator_may_queue_fn)
cb78b285 793 return e->ops->elevator_may_queue_fn(q, rw);
1da177e4
LT
794
795 return ELV_MQUEUE_MAY;
796}
797
11914a53
MA
798void elv_abort_queue(struct request_queue *q)
799{
800 struct request *rq;
801
ae1b1539
TH
802 blk_abort_flushes(q);
803
11914a53
MA
804 while (!list_empty(&q->queue_head)) {
805 rq = list_entry_rq(q->queue_head.next);
806 rq->cmd_flags |= REQ_QUIET;
5f3ea37c 807 trace_block_rq_abort(q, rq);
53c663ce
KU
808 /*
809 * Mark this request as started so we don't trigger
810 * any debug logic in the end I/O path.
811 */
812 blk_start_request(rq);
40cbbb78 813 __blk_end_request_all(rq, -EIO);
11914a53
MA
814 }
815}
816EXPORT_SYMBOL(elv_abort_queue);
817
165125e1 818void elv_completed_request(struct request_queue *q, struct request *rq)
1da177e4 819{
b374d18a 820 struct elevator_queue *e = q->elevator;
1da177e4
LT
821
822 /*
823 * request is released from the driver, io must be done
824 */
8922e16c 825 if (blk_account_rq(rq)) {
0a7ae2ff 826 q->in_flight[rq_is_sync(rq)]--;
33659ebb
CH
827 if ((rq->cmd_flags & REQ_SORTED) &&
828 e->ops->elevator_completed_req_fn)
1bc691d3
TH
829 e->ops->elevator_completed_req_fn(q, rq);
830 }
1da177e4
LT
831}
832
3d1ab40f
AV
833#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
834
835static ssize_t
836elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1da177e4 837{
3d1ab40f 838 struct elv_fs_entry *entry = to_elv(attr);
b374d18a 839 struct elevator_queue *e;
3d1ab40f
AV
840 ssize_t error;
841
842 if (!entry->show)
843 return -EIO;
844
b374d18a 845 e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f
AV
846 mutex_lock(&e->sysfs_lock);
847 error = e->ops ? entry->show(e, page) : -ENOENT;
848 mutex_unlock(&e->sysfs_lock);
849 return error;
850}
1da177e4 851
3d1ab40f
AV
852static ssize_t
853elv_attr_store(struct kobject *kobj, struct attribute *attr,
854 const char *page, size_t length)
855{
3d1ab40f 856 struct elv_fs_entry *entry = to_elv(attr);
b374d18a 857 struct elevator_queue *e;
3d1ab40f 858 ssize_t error;
1da177e4 859
3d1ab40f
AV
860 if (!entry->store)
861 return -EIO;
1da177e4 862
b374d18a 863 e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f
AV
864 mutex_lock(&e->sysfs_lock);
865 error = e->ops ? entry->store(e, page, length) : -ENOENT;
866 mutex_unlock(&e->sysfs_lock);
867 return error;
868}
869
52cf25d0 870static const struct sysfs_ops elv_sysfs_ops = {
3d1ab40f
AV
871 .show = elv_attr_show,
872 .store = elv_attr_store,
873};
874
875static struct kobj_type elv_ktype = {
876 .sysfs_ops = &elv_sysfs_ops,
877 .release = elevator_release,
878};
879
880int elv_register_queue(struct request_queue *q)
881{
b374d18a 882 struct elevator_queue *e = q->elevator;
3d1ab40f
AV
883 int error;
884
b2d6db58 885 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
3d1ab40f 886 if (!error) {
e572ec7e 887 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
3d1ab40f 888 if (attr) {
e572ec7e
AV
889 while (attr->attr.name) {
890 if (sysfs_create_file(&e->kobj, &attr->attr))
3d1ab40f 891 break;
e572ec7e 892 attr++;
3d1ab40f
AV
893 }
894 }
895 kobject_uevent(&e->kobj, KOBJ_ADD);
430c62fb 896 e->registered = 1;
3d1ab40f
AV
897 }
898 return error;
1da177e4 899}
01effb0d 900EXPORT_SYMBOL(elv_register_queue);
1da177e4 901
b374d18a 902static void __elv_unregister_queue(struct elevator_queue *e)
bc1c1169
JA
903{
904 kobject_uevent(&e->kobj, KOBJ_REMOVE);
905 kobject_del(&e->kobj);
430c62fb 906 e->registered = 0;
bc1c1169
JA
907}
908
1da177e4
LT
909void elv_unregister_queue(struct request_queue *q)
910{
bc1c1169
JA
911 if (q)
912 __elv_unregister_queue(q->elevator);
1da177e4 913}
01effb0d 914EXPORT_SYMBOL(elv_unregister_queue);
1da177e4 915
2fdd82bd 916void elv_register(struct elevator_type *e)
1da177e4 917{
1ffb96c5 918 char *def = "";
2a12dcd7
JA
919
920 spin_lock(&elv_list_lock);
ce524497 921 BUG_ON(elevator_find(e->elevator_name));
1da177e4 922 list_add_tail(&e->list, &elv_list);
2a12dcd7 923 spin_unlock(&elv_list_lock);
1da177e4 924
5f003976
ND
925 if (!strcmp(e->elevator_name, chosen_elevator) ||
926 (!*chosen_elevator &&
927 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
1ffb96c5
TV
928 def = " (default)";
929
4eb166d9
JA
930 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
931 def);
1da177e4
LT
932}
933EXPORT_SYMBOL_GPL(elv_register);
934
935void elv_unregister(struct elevator_type *e)
936{
83521d3e
CH
937 struct task_struct *g, *p;
938
939 /*
940 * Iterate every thread in the process to remove the io contexts.
941 */
e17a9489
AV
942 if (e->ops.trim) {
943 read_lock(&tasklist_lock);
944 do_each_thread(g, p) {
945 task_lock(p);
2d8f6131
ON
946 if (p->io_context)
947 e->ops.trim(p->io_context);
e17a9489
AV
948 task_unlock(p);
949 } while_each_thread(g, p);
950 read_unlock(&tasklist_lock);
951 }
83521d3e 952
2a12dcd7 953 spin_lock(&elv_list_lock);
1da177e4 954 list_del_init(&e->list);
2a12dcd7 955 spin_unlock(&elv_list_lock);
1da177e4
LT
956}
957EXPORT_SYMBOL_GPL(elv_unregister);
958
959/*
960 * switch to new_e io scheduler. be careful not to introduce deadlocks -
961 * we don't free the old io scheduler, before we have allocated what we
962 * need for the new one. this way we have a chance of going back to the old
cb98fc8b 963 * one, if the new one fails init for some reason.
1da177e4 964 */
165125e1 965static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1da177e4 966{
b374d18a 967 struct elevator_queue *old_elevator, *e;
bc1c1169 968 void *data;
5dd531a0 969 int err;
1da177e4 970
cb98fc8b
TH
971 /*
972 * Allocate new elevator
973 */
b5deef90 974 e = elevator_alloc(q, new_e);
1da177e4 975 if (!e)
5dd531a0 976 return -ENOMEM;
1da177e4 977
bc1c1169
JA
978 data = elevator_init_queue(q, e);
979 if (!data) {
980 kobject_put(&e->kobj);
5dd531a0 981 return -ENOMEM;
bc1c1169
JA
982 }
983
1da177e4 984 /*
cb98fc8b 985 * Turn on BYPASS and drain all requests w/ elevator private data
1da177e4 986 */
cb98fc8b 987 spin_lock_irq(q->queue_lock);
f600abe2 988 elv_quiesce_start(q);
cb98fc8b 989
1da177e4 990 /*
bc1c1169 991 * Remember old elevator.
1da177e4 992 */
1da177e4
LT
993 old_elevator = q->elevator;
994
1da177e4
LT
995 /*
996 * attach and start new elevator
997 */
bc1c1169
JA
998 elevator_attach(q, e, data);
999
1000 spin_unlock_irq(q->queue_lock);
1001
430c62fb
JA
1002 if (old_elevator->registered) {
1003 __elv_unregister_queue(old_elevator);
1da177e4 1004
430c62fb
JA
1005 err = elv_register_queue(q);
1006 if (err)
1007 goto fail_register;
1008 }
1da177e4
LT
1009
1010 /*
cb98fc8b 1011 * finally exit old elevator and turn off BYPASS.
1da177e4
LT
1012 */
1013 elevator_exit(old_elevator);
75ad23bc 1014 spin_lock_irq(q->queue_lock);
f600abe2 1015 elv_quiesce_end(q);
75ad23bc
NP
1016 spin_unlock_irq(q->queue_lock);
1017
4722dc52
AB
1018 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
1019
5dd531a0 1020 return 0;
1da177e4
LT
1021
1022fail_register:
1023 /*
1024 * switch failed, exit the new io scheduler and reattach the old
1025 * one again (along with re-adding the sysfs dir)
1026 */
1027 elevator_exit(e);
1da177e4
LT
1028 q->elevator = old_elevator;
1029 elv_register_queue(q);
75ad23bc
NP
1030
1031 spin_lock_irq(q->queue_lock);
1032 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1033 spin_unlock_irq(q->queue_lock);
1034
5dd531a0 1035 return err;
1da177e4
LT
1036}
1037
5dd531a0
JA
1038/*
1039 * Switch this queue to the given IO scheduler.
1040 */
1041int elevator_change(struct request_queue *q, const char *name)
1da177e4
LT
1042{
1043 char elevator_name[ELV_NAME_MAX];
1044 struct elevator_type *e;
1045
cd43e26f 1046 if (!q->elevator)
5dd531a0 1047 return -ENXIO;
cd43e26f 1048
ee2e992c 1049 strlcpy(elevator_name, name, sizeof(elevator_name));
8c279598 1050 e = elevator_get(strstrip(elevator_name));
1da177e4
LT
1051 if (!e) {
1052 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1053 return -EINVAL;
1054 }
1055
2ca7d93b
ND
1056 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1057 elevator_put(e);
5dd531a0 1058 return 0;
2ca7d93b 1059 }
1da177e4 1060
5dd531a0
JA
1061 return elevator_switch(q, e);
1062}
1063EXPORT_SYMBOL(elevator_change);
1064
1065ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1066 size_t count)
1067{
1068 int ret;
1069
1070 if (!q->elevator)
1071 return count;
1072
1073 ret = elevator_change(q, name);
1074 if (!ret)
1075 return count;
1076
1077 printk(KERN_ERR "elevator: switch to %s failed\n", name);
1078 return ret;
1da177e4
LT
1079}
1080
165125e1 1081ssize_t elv_iosched_show(struct request_queue *q, char *name)
1da177e4 1082{
b374d18a 1083 struct elevator_queue *e = q->elevator;
cd43e26f 1084 struct elevator_type *elv;
70cee26e 1085 struct elevator_type *__e;
1da177e4
LT
1086 int len = 0;
1087
e36f724b 1088 if (!q->elevator || !blk_queue_stackable(q))
cd43e26f
MP
1089 return sprintf(name, "none\n");
1090
1091 elv = e->elevator_type;
1092
2a12dcd7 1093 spin_lock(&elv_list_lock);
70cee26e 1094 list_for_each_entry(__e, &elv_list, list) {
1da177e4
LT
1095 if (!strcmp(elv->elevator_name, __e->elevator_name))
1096 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1097 else
1098 len += sprintf(name+len, "%s ", __e->elevator_name);
1099 }
2a12dcd7 1100 spin_unlock(&elv_list_lock);
1da177e4
LT
1101
1102 len += sprintf(len+name, "\n");
1103 return len;
1104}
1105
165125e1
JA
1106struct request *elv_rb_former_request(struct request_queue *q,
1107 struct request *rq)
2e662b65
JA
1108{
1109 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1110
1111 if (rbprev)
1112 return rb_entry_rq(rbprev);
1113
1114 return NULL;
1115}
2e662b65
JA
1116EXPORT_SYMBOL(elv_rb_former_request);
1117
165125e1
JA
1118struct request *elv_rb_latter_request(struct request_queue *q,
1119 struct request *rq)
2e662b65
JA
1120{
1121 struct rb_node *rbnext = rb_next(&rq->rb_node);
1122
1123 if (rbnext)
1124 return rb_entry_rq(rbnext);
1125
1126 return NULL;
1127}
2e662b65 1128EXPORT_SYMBOL(elv_rb_latter_request);
This page took 0.526783 seconds and 5 git commands to generate.