blob: 530fcfe2ef075640323573fef56f67783c17675c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Block device elevator/IO-scheduler.
3 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 *
Jens Axboe0fe23472006-09-04 15:41:16 +02006 * 30042000 Jens Axboe <axboe@kernel.dk> :
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/init.h>
33#include <linux/compiler.h>
Tejun Heocb98fc82005-10-28 08:29:39 +020034#include <linux/delay.h>
Jens Axboe2056a782006-03-23 20:00:26 +010035#include <linux/blktrace_api.h>
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010036#include <trace/block.h>
Jens Axboe98170642006-07-28 09:23:08 +020037#include <linux/hash.h>
Jens Axboe0835da62008-08-26 09:15:47 +020038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jens Axboe242f9dc2008-09-14 05:55:09 -070040#include "blk.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_SPINLOCK(elv_list_lock);
43static LIST_HEAD(elv_list);
44
45/*
Jens Axboe98170642006-07-28 09:23:08 +020046 * Merge hash stuff.
47 */
48static const int elv_hash_shift = 6;
49#define ELV_HASH_BLOCK(sec) ((sec) >> 3)
Jens Axboe4eb166d2008-02-01 00:37:27 +010050#define ELV_HASH_FN(sec) \
51 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
Jens Axboe98170642006-07-28 09:23:08 +020052#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
53#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
54#define ELV_ON_HASH(rq) (!hlist_unhashed(&(rq)->hash))
55
56/*
Jens Axboeda775262006-12-20 11:04:12 +010057 * 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{
Jens Axboe165125e2007-07-24 09:28:11 +020062 struct request_queue *q = rq->q;
Jens Axboeda775262006-12-20 11:04:12 +010063 elevator_t *e = q->elevator;
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
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * can we safely merge with this request?
73 */
Adrian Bunk72ed0bf2008-04-29 09:49:05 +020074int elv_rq_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 if (!rq_mergeable(rq))
77 return 0;
78
79 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +010080 * Don't merge file system requests and discard requests
81 */
82 if (bio_discard(bio) != bio_discard(rq->bio))
83 return 0;
84
85 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * different data direction or already started, don't merge
87 */
88 if (bio_data_dir(bio) != rq_data_dir(rq))
89 return 0;
90
91 /*
Jens Axboeda775262006-12-20 11:04:12 +010092 * must be same device and not a special request
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Jens Axboebb4067e2006-12-21 21:20:01 +010094 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
Jens Axboeda775262006-12-20 11:04:12 +010095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020097 /*
98 * only merge integrity protected bio into ditto rq
99 */
100 if (bio_integrity(bio) != blk_integrity_rq(rq))
101 return 0;
102
Jens Axboeda775262006-12-20 11:04:12 +0100103 if (!elv_iosched_allow_merge(rq, bio))
104 return 0;
105
106 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108EXPORT_SYMBOL(elv_rq_merge_ok);
109
Coywolf Qi Hunt769db452005-12-28 10:55:49 +0100110static inline int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 int ret = ELEVATOR_NO_MERGE;
113
114 /*
115 * we can merge and sequence is ok, check if it's possible
116 */
117 if (elv_rq_merge_ok(__rq, bio)) {
118 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
119 ret = ELEVATOR_BACK_MERGE;
120 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
121 ret = ELEVATOR_FRONT_MERGE;
122 }
123
124 return ret;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct elevator_type *elevator_find(const char *name)
128{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200129 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200131 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +0200132 if (!strcmp(e->elevator_name, name))
133 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Vasily Tarasova22b1692006-10-11 09:24:27 +0200136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static void elevator_put(struct elevator_type *e)
140{
141 module_put(e->elevator_owner);
142}
143
144static struct elevator_type *elevator_get(const char *name)
145{
Tejun Heo2824bc932005-10-20 10:56:41 +0200146 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200148 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200149
150 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100151 if (!e) {
152 char elv[ELV_NAME_MAX + strlen("-iosched")];
153
154 spin_unlock(&elv_list_lock);
155
156 if (!strcmp(name, "anticipatory"))
157 sprintf(elv, "as-iosched");
158 else
159 sprintf(elv, "%s-iosched", name);
160
maximilian attemse180f592008-07-01 09:42:47 +0200161 request_module("%s", elv);
Jens Axboee1640942008-02-19 10:20:37 +0100162 spin_lock(&elv_list_lock);
163 e = elevator_find(name);
164 }
165
Tejun Heo2824bc932005-10-20 10:56:41 +0200166 if (e && !try_module_get(e->elevator_owner))
167 e = NULL;
168
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200169 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return e;
172}
173
Jens Axboe165125e2007-07-24 09:28:11 +0200174static void *elevator_init_queue(struct request_queue *q,
175 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jens Axboebb37b942006-12-01 10:42:33 +0100177 return eq->ops->elevator_init_fn(q);
Jens Axboebc1c1162006-06-08 08:49:06 +0200178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jens Axboe165125e2007-07-24 09:28:11 +0200180static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
Jens Axboebc1c1162006-06-08 08:49:06 +0200181 void *data)
182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 q->elevator = eq;
Jens Axboebc1c1162006-06-08 08:49:06 +0200184 eq->elevator_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static char chosen_elevator[16];
188
Nate Diller5f003972006-01-24 10:07:58 +0100189static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100191 /*
192 * Be backwards-compatible with previous kernels, so users
193 * won't get the wrong elevator.
194 */
Nate Diller5f003972006-01-24 10:07:58 +0100195 if (!strcmp(str, "as"))
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100196 strcpy(chosen_elevator, "anticipatory");
Zachary Amsdencff3ba22005-11-09 13:24:20 +0100197 else
Nate Diller5f003972006-01-24 10:07:58 +0100198 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800199 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202__setup("elevator=", elevator_setup);
203
Al Viro3d1ab402006-03-18 18:35:43 -0500204static struct kobj_type elv_ktype;
205
Jens Axboe165125e2007-07-24 09:28:11 +0200206static elevator_t *elevator_alloc(struct request_queue *q,
207 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500208{
Jens Axboe98170642006-07-28 09:23:08 +0200209 elevator_t *eq;
210 int i;
211
Christoph Lameter94f60302007-07-17 04:03:29 -0700212 eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200213 if (unlikely(!eq))
214 goto err;
215
Jens Axboe98170642006-07-28 09:23:08 +0200216 eq->ops = &e->ops;
217 eq->elevator_type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700218 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200219 mutex_init(&eq->sysfs_lock);
220
Jens Axboeb5deef92006-07-19 23:39:40 +0200221 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
222 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200223 if (!eq->hash)
224 goto err;
225
226 for (i = 0; i < ELV_HASH_ENTRIES; i++)
227 INIT_HLIST_HEAD(&eq->hash[i]);
228
Al Viro3d1ab402006-03-18 18:35:43 -0500229 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200230err:
231 kfree(eq);
232 elevator_put(e);
233 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500234}
235
236static void elevator_release(struct kobject *kobj)
237{
238 elevator_t *e = container_of(kobj, elevator_t, kobj);
Jens Axboe98170642006-07-28 09:23:08 +0200239
Al Viro3d1ab402006-03-18 18:35:43 -0500240 elevator_put(e->elevator_type);
Jens Axboe98170642006-07-28 09:23:08 +0200241 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500242 kfree(e);
243}
244
Jens Axboe165125e2007-07-24 09:28:11 +0200245int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct elevator_type *e = NULL;
248 struct elevator_queue *eq;
249 int ret = 0;
Jens Axboebc1c1162006-06-08 08:49:06 +0200250 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Tejun Heocb98fc82005-10-28 08:29:39 +0200252 INIT_LIST_HEAD(&q->queue_head);
253 q->last_merge = NULL;
254 q->end_sector = 0;
255 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200256
Jens Axboe4eb166d2008-02-01 00:37:27 +0100257 if (name) {
258 e = elevator_get(name);
259 if (!e)
260 return -EINVAL;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jens Axboe4eb166d2008-02-01 00:37:27 +0100263 if (!e && *chosen_elevator) {
264 e = elevator_get(chosen_elevator);
265 if (!e)
266 printk(KERN_ERR "I/O scheduler %s not found\n",
267 chosen_elevator);
268 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100269
Jens Axboe4eb166d2008-02-01 00:37:27 +0100270 if (!e) {
271 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
272 if (!e) {
273 printk(KERN_ERR
274 "Default I/O scheduler not found. " \
275 "Using noop.\n");
276 e = elevator_get("noop");
277 }
Nate Diller5f003972006-01-24 10:07:58 +0100278 }
279
Jens Axboeb5deef92006-07-19 23:39:40 +0200280 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500281 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jens Axboebc1c1162006-06-08 08:49:06 +0200284 data = elevator_init_queue(q, eq);
285 if (!data) {
Al Viro3d1ab402006-03-18 18:35:43 -0500286 kobject_put(&eq->kobj);
Jens Axboebc1c1162006-06-08 08:49:06 +0200287 return -ENOMEM;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Jens Axboebc1c1162006-06-08 08:49:06 +0200290 elevator_attach(q, eq, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ret;
292}
Jens Axboe2e662b62006-07-13 11:55:04 +0200293EXPORT_SYMBOL(elevator_init);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295void elevator_exit(elevator_t *e)
296{
Al Viro3d1ab402006-03-18 18:35:43 -0500297 mutex_lock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (e->ops->elevator_exit_fn)
299 e->ops->elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500300 e->ops = NULL;
301 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Al Viro3d1ab402006-03-18 18:35:43 -0500303 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Jens Axboe2e662b62006-07-13 11:55:04 +0200305EXPORT_SYMBOL(elevator_exit);
306
Jens Axboe165125e2007-07-24 09:28:11 +0200307static void elv_activate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100308{
309 elevator_t *e = q->elevator;
310
311 if (e->ops->elevator_activate_req_fn)
312 e->ops->elevator_activate_req_fn(q, rq);
313}
314
Jens Axboe165125e2007-07-24 09:28:11 +0200315static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
Jens Axboecad97512007-01-14 22:26:09 +1100316{
317 elevator_t *e = q->elevator;
318
319 if (e->ops->elevator_deactivate_req_fn)
320 e->ops->elevator_deactivate_req_fn(q, rq);
321}
322
Jens Axboe98170642006-07-28 09:23:08 +0200323static inline void __elv_rqhash_del(struct request *rq)
324{
325 hlist_del_init(&rq->hash);
326}
327
Jens Axboe165125e2007-07-24 09:28:11 +0200328static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200329{
330 if (ELV_ON_HASH(rq))
331 __elv_rqhash_del(rq);
332}
333
Jens Axboe165125e2007-07-24 09:28:11 +0200334static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200335{
336 elevator_t *e = q->elevator;
337
338 BUG_ON(ELV_ON_HASH(rq));
339 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
340}
341
Jens Axboe165125e2007-07-24 09:28:11 +0200342static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200343{
344 __elv_rqhash_del(rq);
345 elv_rqhash_add(q, rq);
346}
347
Jens Axboe165125e2007-07-24 09:28:11 +0200348static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200349{
350 elevator_t *e = q->elevator;
351 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
352 struct hlist_node *entry, *next;
353 struct request *rq;
354
355 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
356 BUG_ON(!ELV_ON_HASH(rq));
357
358 if (unlikely(!rq_mergeable(rq))) {
359 __elv_rqhash_del(rq);
360 continue;
361 }
362
363 if (rq_hash_key(rq) == offset)
364 return rq;
365 }
366
367 return NULL;
368}
369
Tejun Heo8922e162005-10-20 16:23:44 +0200370/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200371 * RB-tree support functions for inserting/lookup/removal of requests
372 * in a sorted RB tree.
373 */
374struct request *elv_rb_add(struct rb_root *root, struct request *rq)
375{
376 struct rb_node **p = &root->rb_node;
377 struct rb_node *parent = NULL;
378 struct request *__rq;
379
380 while (*p) {
381 parent = *p;
382 __rq = rb_entry(parent, struct request, rb_node);
383
384 if (rq->sector < __rq->sector)
385 p = &(*p)->rb_left;
386 else if (rq->sector > __rq->sector)
387 p = &(*p)->rb_right;
388 else
389 return __rq;
390 }
391
392 rb_link_node(&rq->rb_node, parent, p);
393 rb_insert_color(&rq->rb_node, root);
394 return NULL;
395}
Jens Axboe2e662b62006-07-13 11:55:04 +0200396EXPORT_SYMBOL(elv_rb_add);
397
398void elv_rb_del(struct rb_root *root, struct request *rq)
399{
400 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
401 rb_erase(&rq->rb_node, root);
402 RB_CLEAR_NODE(&rq->rb_node);
403}
Jens Axboe2e662b62006-07-13 11:55:04 +0200404EXPORT_SYMBOL(elv_rb_del);
405
406struct request *elv_rb_find(struct rb_root *root, sector_t sector)
407{
408 struct rb_node *n = root->rb_node;
409 struct request *rq;
410
411 while (n) {
412 rq = rb_entry(n, struct request, rb_node);
413
414 if (sector < rq->sector)
415 n = n->rb_left;
416 else if (sector > rq->sector)
417 n = n->rb_right;
418 else
419 return rq;
420 }
421
422 return NULL;
423}
Jens Axboe2e662b62006-07-13 11:55:04 +0200424EXPORT_SYMBOL(elv_rb_find);
425
426/*
Tejun Heo8922e162005-10-20 16:23:44 +0200427 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200428 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200429 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200430 */
Jens Axboe165125e2007-07-24 09:28:11 +0200431void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200432{
433 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200434 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100435 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200436
Tejun Heo06b86242005-10-20 16:46:23 +0200437 if (q->last_merge == rq)
438 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200439
440 elv_rqhash_del(q, rq);
441
Tejun Heo15853af2005-11-10 08:52:05 +0100442 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200443
Jens Axboe1b47f532005-10-20 16:37:00 +0200444 boundary = q->end_sector;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100445 stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200446 list_for_each_prev(entry, &q->queue_head) {
447 struct request *pos = list_entry_rq(entry);
448
David Woodhousee17fc0a2008-08-09 16:42:20 +0100449 if (blk_discard_rq(rq) != blk_discard_rq(pos))
450 break;
Jens Axboe783660b2007-01-19 11:27:47 +1100451 if (rq_data_dir(rq) != rq_data_dir(pos))
452 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100453 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200454 break;
455 if (rq->sector >= boundary) {
456 if (pos->sector < boundary)
457 continue;
458 } else {
459 if (pos->sector >= boundary)
460 break;
461 }
462 if (rq->sector >= pos->sector)
463 break;
464 }
465
466 list_add(&rq->queuelist, entry);
467}
Jens Axboe2e662b62006-07-13 11:55:04 +0200468EXPORT_SYMBOL(elv_dispatch_sort);
469
Jens Axboe98170642006-07-28 09:23:08 +0200470/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200471 * Insert rq into dispatch queue of q. Queue lock must be held on
472 * entry. rq is added to the back of the dispatch queue. To be used by
473 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200474 */
475void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
476{
477 if (q->last_merge == rq)
478 q->last_merge = NULL;
479
480 elv_rqhash_del(q, rq);
481
482 q->nr_sorted--;
483
484 q->end_sector = rq_end_sector(rq);
485 q->boundary_rq = rq;
486 list_add_tail(&rq->queuelist, &q->queue_head);
487}
Jens Axboe2e662b62006-07-13 11:55:04 +0200488EXPORT_SYMBOL(elv_dispatch_add_tail);
489
Jens Axboe165125e2007-07-24 09:28:11 +0200490int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 elevator_t *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200493 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200494 int ret;
495
Jens Axboe98170642006-07-28 09:23:08 +0200496 /*
497 * First try one-hit cache.
498 */
Tejun Heo06b86242005-10-20 16:46:23 +0200499 if (q->last_merge) {
500 ret = elv_try_merge(q->last_merge, bio);
501 if (ret != ELEVATOR_NO_MERGE) {
502 *req = q->last_merge;
503 return ret;
504 }
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200507 if (blk_queue_nomerges(q))
508 return ELEVATOR_NO_MERGE;
509
Jens Axboe98170642006-07-28 09:23:08 +0200510 /*
511 * See if our hash lookup can find a potential backmerge.
512 */
513 __rq = elv_rqhash_find(q, bio->bi_sector);
514 if (__rq && elv_rq_merge_ok(__rq, bio)) {
515 *req = __rq;
516 return ELEVATOR_BACK_MERGE;
517 }
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (e->ops->elevator_merge_fn)
520 return e->ops->elevator_merge_fn(q, req, bio);
521
522 return ELEVATOR_NO_MERGE;
523}
524
Jens Axboe165125e2007-07-24 09:28:11 +0200525void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 elevator_t *e = q->elevator;
528
529 if (e->ops->elevator_merged_fn)
Jens Axboe2e662b62006-07-13 11:55:04 +0200530 e->ops->elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200531
Jens Axboe2e662b62006-07-13 11:55:04 +0200532 if (type == ELEVATOR_BACK_MERGE)
533 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200534
Tejun Heo06b86242005-10-20 16:46:23 +0200535 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Jens Axboe165125e2007-07-24 09:28:11 +0200538void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct request *next)
540{
541 elevator_t *e = q->elevator;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (e->ops->elevator_merge_req_fn)
544 e->ops->elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200545
Jens Axboe98170642006-07-28 09:23:08 +0200546 elv_rqhash_reposition(q, rq);
547 elv_rqhash_del(q, next);
548
549 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200550 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Jens Axboe165125e2007-07-24 09:28:11 +0200553void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 * it already went through dequeue, we need to decrement the
557 * in_flight count again
558 */
Tejun Heo8922e162005-10-20 16:23:44 +0200559 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 q->in_flight--;
Jens Axboecad97512007-01-14 22:26:09 +1100561 if (blk_sorted_rq(rq))
562 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jens Axboe4aff5e22006-08-10 08:44:47 +0200565 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Tejun Heo30e96562006-02-08 01:01:31 -0800567 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Jens Axboe165125e2007-07-24 09:28:11 +0200570static void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100571{
572 static int printed;
573 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
574 ;
575 if (q->nr_sorted == 0)
576 return;
577 if (printed++ < 10) {
578 printk(KERN_ERR "%s: forced dispatching is broken "
579 "(nr_sorted=%u), please report this\n",
580 q->elevator->elevator_type->elevator_name, q->nr_sorted);
581 }
582}
583
Jens Axboe165125e2007-07-24 09:28:11 +0200584void elv_insert(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Tejun Heo797e7db2006-01-06 09:51:03 +0100586 struct list_head *pos;
587 unsigned ordseq;
Jens Axboedac07ec2006-05-11 08:20:16 +0200588 int unplug_it = 1;
Tejun Heo797e7db2006-01-06 09:51:03 +0100589
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100590 trace_block_rq_insert(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 rq->q = q;
593
Tejun Heo8922e162005-10-20 16:23:44 +0200594 switch (where) {
595 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200596 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200597
598 list_add(&rq->queuelist, &q->queue_head);
599 break;
600
601 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200602 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100603 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200604 list_add_tail(&rq->queuelist, &q->queue_head);
605 /*
606 * We kick the queue here for the following reasons.
607 * - The elevator might have returned NULL previously
608 * to delay requests and returned them now. As the
609 * queue wasn't empty before this request, ll_rw_blk
610 * won't run the queue on return, resulting in hang.
611 * - Usually, back inserted requests won't be merged
612 * with anything. There's no point in delaying queue
613 * processing.
614 */
615 blk_remove_plug(q);
Jens Axboe80a4b582008-10-14 09:51:06 +0200616 blk_start_queueing(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200617 break;
618
619 case ELEVATOR_INSERT_SORT:
David Woodhousee17fc0a2008-08-09 16:42:20 +0100620 BUG_ON(!blk_fs_request(rq) && !blk_discard_rq(rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200621 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100622 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200623 if (rq_mergeable(rq)) {
624 elv_rqhash_add(q, rq);
625 if (!q->last_merge)
626 q->last_merge = rq;
627 }
628
Tejun Heoca235092005-11-01 17:23:49 +0900629 /*
630 * Some ioscheds (cfq) run q->request_fn directly, so
631 * rq cannot be accessed after calling
632 * elevator_add_req_fn.
633 */
634 q->elevator->ops->elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200635 break;
636
Tejun Heo797e7db2006-01-06 09:51:03 +0100637 case ELEVATOR_INSERT_REQUEUE:
638 /*
639 * If ordered flush isn't in progress, we do front
640 * insertion; otherwise, requests should be requeued
641 * in ordseq order.
642 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200643 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100644
Linas Vepstas95543172007-01-23 19:40:54 +0100645 /*
646 * Most requeues happen because of a busy condition,
647 * don't force unplug of the queue for that case.
648 */
649 unplug_it = 0;
650
Tejun Heo797e7db2006-01-06 09:51:03 +0100651 if (q->ordseq == 0) {
652 list_add(&rq->queuelist, &q->queue_head);
653 break;
654 }
655
656 ordseq = blk_ordered_req_seq(rq);
657
658 list_for_each(pos, &q->queue_head) {
659 struct request *pos_rq = list_entry_rq(pos);
660 if (ordseq <= blk_ordered_req_seq(pos_rq))
661 break;
662 }
663
664 list_add_tail(&rq->queuelist, pos);
665 break;
666
Tejun Heo8922e162005-10-20 16:23:44 +0200667 default:
668 printk(KERN_ERR "%s: bad insertion point %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700669 __func__, where);
Tejun Heo8922e162005-10-20 16:23:44 +0200670 BUG();
671 }
672
Jens Axboedac07ec2006-05-11 08:20:16 +0200673 if (unplug_it && blk_queue_plugged(q)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200674 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
675 - q->in_flight;
676
677 if (nrq >= q->unplug_thresh)
678 __generic_unplug_device(q);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Jens Axboe165125e2007-07-24 09:28:11 +0200682void __elv_add_request(struct request_queue *q, struct request *rq, int where,
Tejun Heo30e96562006-02-08 01:01:31 -0800683 int plug)
684{
685 if (q->ordcolor)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200686 rq->cmd_flags |= REQ_ORDERED_COLOR;
Tejun Heo30e96562006-02-08 01:01:31 -0800687
Jens Axboe4aff5e22006-08-10 08:44:47 +0200688 if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800689 /*
690 * toggle ordered color
691 */
692 if (blk_barrier_rq(rq))
693 q->ordcolor ^= 1;
694
695 /*
696 * barriers implicitly indicate back insertion
697 */
698 if (where == ELEVATOR_INSERT_SORT)
699 where = ELEVATOR_INSERT_BACK;
700
701 /*
702 * this request is scheduling boundary, update
703 * end_sector
704 */
David Woodhousee17fc0a2008-08-09 16:42:20 +0100705 if (blk_fs_request(rq) || blk_discard_rq(rq)) {
Tejun Heo30e96562006-02-08 01:01:31 -0800706 q->end_sector = rq_end_sector(rq);
707 q->boundary_rq = rq;
708 }
Jens Axboe4eb166d2008-02-01 00:37:27 +0100709 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
710 where == ELEVATOR_INSERT_SORT)
Tejun Heo30e96562006-02-08 01:01:31 -0800711 where = ELEVATOR_INSERT_BACK;
712
713 if (plug)
714 blk_plug_device(q);
715
716 elv_insert(q, rq, where);
717}
Jens Axboe2e662b62006-07-13 11:55:04 +0200718EXPORT_SYMBOL(__elv_add_request);
719
Jens Axboe165125e2007-07-24 09:28:11 +0200720void elv_add_request(struct request_queue *q, struct request *rq, int where,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int plug)
722{
723 unsigned long flags;
724
725 spin_lock_irqsave(q->queue_lock, flags);
726 __elv_add_request(q, rq, where, plug);
727 spin_unlock_irqrestore(q->queue_lock, flags);
728}
Jens Axboe2e662b62006-07-13 11:55:04 +0200729EXPORT_SYMBOL(elv_add_request);
730
Jens Axboe165125e2007-07-24 09:28:11 +0200731static inline struct request *__elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Tejun Heo8922e162005-10-20 16:23:44 +0200733 struct request *rq;
734
Tejun Heo797e7db2006-01-06 09:51:03 +0100735 while (1) {
736 while (!list_empty(&q->queue_head)) {
737 rq = list_entry_rq(q->queue_head.next);
738 if (blk_do_ordered(q, &rq))
739 return rq;
740 }
Tejun Heo8922e162005-10-20 16:23:44 +0200741
Tejun Heo797e7db2006-01-06 09:51:03 +0100742 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
743 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Jens Axboe165125e2007-07-24 09:28:11 +0200747struct request *elv_next_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct request *rq;
750 int ret;
751
752 while ((rq = __elv_next_request(q)) != NULL) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200753 /*
754 * Kill the empty barrier place holder, the driver must
755 * not ever see it.
756 */
757 if (blk_empty_barrier(rq)) {
Kiyoshi Ueda99cd3382008-10-01 10:13:44 -0400758 __blk_end_request(rq, 0, blk_rq_bytes(rq));
Jens Axboebf2de6f2007-09-27 13:01:25 +0200759 continue;
760 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200761 if (!(rq->cmd_flags & REQ_STARTED)) {
Tejun Heo8922e162005-10-20 16:23:44 +0200762 /*
763 * This is the first time the device driver
764 * sees this request (possibly after
765 * requeueing). Notify IO scheduler.
766 */
Jens Axboecad97512007-01-14 22:26:09 +1100767 if (blk_sorted_rq(rq))
768 elv_activate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200769
770 /*
771 * just mark as started even if we don't start
772 * it, a request that has been delayed should
773 * not be passed by new incoming requests
774 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200775 rq->cmd_flags |= REQ_STARTED;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100776 trace_block_rq_issue(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tejun Heo8922e162005-10-20 16:23:44 +0200779 if (!q->boundary_rq || q->boundary_rq == rq) {
Jens Axboe1b47f532005-10-20 16:37:00 +0200780 q->end_sector = rq_end_sector(rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200781 q->boundary_rq = NULL;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600784 if (rq->cmd_flags & REQ_DONTPREP)
785 break;
786
787 if (q->dma_drain_size && rq->data_len) {
788 /*
789 * make sure space for the drain appears we
790 * know we can do this because max_hw_segments
791 * has been adjusted to be one fewer than the
792 * device can handle
793 */
794 rq->nr_phys_segments++;
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600795 }
796
797 if (!q->prep_rq_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799
800 ret = q->prep_rq_fn(q, rq);
801 if (ret == BLKPREP_OK) {
802 break;
803 } else if (ret == BLKPREP_DEFER) {
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500804 /*
805 * the request may have been (partially) prepped.
806 * we need to keep this request in the front to
Tejun Heo8922e162005-10-20 16:23:44 +0200807 * avoid resource deadlock. REQ_STARTED will
808 * prevent other fs requests from passing this one.
Tejun Heo 2e759cd2005-04-24 02:04:21 -0500809 */
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600810 if (q->dma_drain_size && rq->data_len &&
811 !(rq->cmd_flags & REQ_DONTPREP)) {
812 /*
813 * remove the space for the drain we added
814 * so that we don't add it again
815 */
816 --rq->nr_phys_segments;
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600817 }
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 rq = NULL;
820 break;
821 } else if (ret == BLKPREP_KILL) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200822 rq->cmd_flags |= REQ_QUIET;
Kiyoshi Ueda99cd3382008-10-01 10:13:44 -0400823 __blk_end_request(rq, -EIO, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 } else {
Harvey Harrison24c03d42008-05-01 04:35:17 -0700825 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827 }
828 }
829
830 return rq;
831}
Jens Axboe2e662b62006-07-13 11:55:04 +0200832EXPORT_SYMBOL(elv_next_request);
833
Jens Axboe165125e2007-07-24 09:28:11 +0200834void elv_dequeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Tejun Heo8922e162005-10-20 16:23:44 +0200836 BUG_ON(list_empty(&rq->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +0200837 BUG_ON(ELV_ON_HASH(rq));
Tejun Heo8922e162005-10-20 16:23:44 +0200838
839 list_del_init(&rq->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /*
842 * the time frame between a request being removed from the lists
843 * and to it is freed is accounted as io that is in progress at
Tejun Heo8922e162005-10-20 16:23:44 +0200844 * the driver side.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
846 if (blk_account_rq(rq))
847 q->in_flight++;
Tejun Heo2920ebb2008-10-30 08:32:29 +0100848
849 /*
850 * We are now handing the request to the hardware, add the
851 * timeout handler.
852 */
853 blk_add_timer(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Jens Axboe2e662b62006-07-13 11:55:04 +0200855EXPORT_SYMBOL(elv_dequeue_request);
856
Jens Axboe165125e2007-07-24 09:28:11 +0200857int elv_queue_empty(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 elevator_t *e = q->elevator;
860
Tejun Heo8922e162005-10-20 16:23:44 +0200861 if (!list_empty(&q->queue_head))
862 return 0;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (e->ops->elevator_queue_empty_fn)
865 return e->ops->elevator_queue_empty_fn(q);
866
Tejun Heo8922e162005-10-20 16:23:44 +0200867 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
Jens Axboe2e662b62006-07-13 11:55:04 +0200869EXPORT_SYMBOL(elv_queue_empty);
870
Jens Axboe165125e2007-07-24 09:28:11 +0200871struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 elevator_t *e = q->elevator;
874
875 if (e->ops->elevator_latter_req_fn)
876 return e->ops->elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return NULL;
878}
879
Jens Axboe165125e2007-07-24 09:28:11 +0200880struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 elevator_t *e = q->elevator;
883
884 if (e->ops->elevator_former_req_fn)
885 return e->ops->elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return NULL;
887}
888
Jens Axboe165125e2007-07-24 09:28:11 +0200889int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 elevator_t *e = q->elevator;
892
893 if (e->ops->elevator_set_req_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200894 return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 rq->elevator_private = NULL;
897 return 0;
898}
899
Jens Axboe165125e2007-07-24 09:28:11 +0200900void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 elevator_t *e = q->elevator;
903
904 if (e->ops->elevator_put_req_fn)
Jens Axboebb37b942006-12-01 10:42:33 +0100905 e->ops->elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Jens Axboe165125e2007-07-24 09:28:11 +0200908int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 elevator_t *e = q->elevator;
911
912 if (e->ops->elevator_may_queue_fn)
Jens Axboecb78b282006-07-28 09:32:57 +0200913 return e->ops->elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return ELV_MQUEUE_MAY;
916}
917
Mike Anderson11914a52008-09-13 20:31:27 +0200918void elv_abort_queue(struct request_queue *q)
919{
920 struct request *rq;
921
922 while (!list_empty(&q->queue_head)) {
923 rq = list_entry_rq(q->queue_head.next);
924 rq->cmd_flags |= REQ_QUIET;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100925 trace_block_rq_abort(q, rq);
Kiyoshi Ueda99cd3382008-10-01 10:13:44 -0400926 __blk_end_request(rq, -EIO, blk_rq_bytes(rq));
Mike Anderson11914a52008-09-13 20:31:27 +0200927 }
928}
929EXPORT_SYMBOL(elv_abort_queue);
930
Jens Axboe165125e2007-07-24 09:28:11 +0200931void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 elevator_t *e = q->elevator;
934
935 /*
936 * request is released from the driver, io must be done
937 */
Tejun Heo8922e162005-10-20 16:23:44 +0200938 if (blk_account_rq(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 q->in_flight--;
Tejun Heo1bc691d2006-01-12 15:39:26 +0100940 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
941 e->ops->elevator_completed_req_fn(q, rq);
942 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100943
Tejun Heo1bc691d2006-01-12 15:39:26 +0100944 /*
945 * Check if the queue is waiting for fs requests to be
946 * drained for flush sequence.
947 */
948 if (unlikely(q->ordseq)) {
949 struct request *first_rq = list_entry_rq(q->queue_head.next);
950 if (q->in_flight == 0 &&
Tejun Heo797e7db2006-01-06 09:51:03 +0100951 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
952 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
953 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
Jens Axboe80a4b582008-10-14 09:51:06 +0200954 blk_start_queueing(q);
Tejun Heo797e7db2006-01-06 09:51:03 +0100955 }
Tejun Heo8922e162005-10-20 16:23:44 +0200956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Al Viro3d1ab402006-03-18 18:35:43 -0500959#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
960
961static ssize_t
962elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
963{
964 elevator_t *e = container_of(kobj, elevator_t, kobj);
965 struct elv_fs_entry *entry = to_elv(attr);
966 ssize_t error;
967
968 if (!entry->show)
969 return -EIO;
970
971 mutex_lock(&e->sysfs_lock);
972 error = e->ops ? entry->show(e, page) : -ENOENT;
973 mutex_unlock(&e->sysfs_lock);
974 return error;
975}
976
977static ssize_t
978elv_attr_store(struct kobject *kobj, struct attribute *attr,
979 const char *page, size_t length)
980{
981 elevator_t *e = container_of(kobj, elevator_t, kobj);
982 struct elv_fs_entry *entry = to_elv(attr);
983 ssize_t error;
984
985 if (!entry->store)
986 return -EIO;
987
988 mutex_lock(&e->sysfs_lock);
989 error = e->ops ? entry->store(e, page, length) : -ENOENT;
990 mutex_unlock(&e->sysfs_lock);
991 return error;
992}
993
994static struct sysfs_ops elv_sysfs_ops = {
995 .show = elv_attr_show,
996 .store = elv_attr_store,
997};
998
999static struct kobj_type elv_ktype = {
1000 .sysfs_ops = &elv_sysfs_ops,
1001 .release = elevator_release,
1002};
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004int elv_register_queue(struct request_queue *q)
1005{
1006 elevator_t *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -05001007 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001009 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -05001010 if (!error) {
Al Viroe572ec72006-03-18 22:27:18 -05001011 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -05001012 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -05001013 while (attr->attr.name) {
1014 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -05001015 break;
Al Viroe572ec72006-03-18 22:27:18 -05001016 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -05001017 }
1018 }
1019 kobject_uevent(&e->kobj, KOBJ_ADD);
1020 }
1021 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Jens Axboebc1c1162006-06-08 08:49:06 +02001024static void __elv_unregister_queue(elevator_t *e)
1025{
1026 kobject_uevent(&e->kobj, KOBJ_REMOVE);
1027 kobject_del(&e->kobj);
1028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030void elv_unregister_queue(struct request_queue *q)
1031{
Jens Axboebc1c1162006-06-08 08:49:06 +02001032 if (q)
1033 __elv_unregister_queue(q->elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01001036void elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +01001038 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001039
1040 spin_lock(&elv_list_lock);
Eric Sesterhennce524492006-03-24 18:43:26 +01001041 BUG_ON(elevator_find(e->elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001043 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Nate Diller5f003972006-01-24 10:07:58 +01001045 if (!strcmp(e->elevator_name, chosen_elevator) ||
1046 (!*chosen_elevator &&
1047 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +01001048 def = " (default)";
1049
Jens Axboe4eb166d2008-02-01 00:37:27 +01001050 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
1051 def);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053EXPORT_SYMBOL_GPL(elv_register);
1054
1055void elv_unregister(struct elevator_type *e)
1056{
Christoph Hellwig83521d32005-10-30 15:01:39 -08001057 struct task_struct *g, *p;
1058
1059 /*
1060 * Iterate every thread in the process to remove the io contexts.
1061 */
Al Viroe17a9482006-03-18 13:21:20 -05001062 if (e->ops.trim) {
1063 read_lock(&tasklist_lock);
1064 do_each_thread(g, p) {
1065 task_lock(p);
Oleg Nesterov2d8f6132006-08-22 21:22:13 +04001066 if (p->io_context)
1067 e->ops.trim(p->io_context);
Al Viroe17a9482006-03-18 13:21:20 -05001068 task_unlock(p);
1069 } while_each_thread(g, p);
1070 read_unlock(&tasklist_lock);
1071 }
Christoph Hellwig83521d32005-10-30 15:01:39 -08001072
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001073 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001075 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077EXPORT_SYMBOL_GPL(elv_unregister);
1078
1079/*
1080 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1081 * we don't free the old io scheduler, before we have allocated what we
1082 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001083 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Jens Axboe165125e2007-07-24 09:28:11 +02001085static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Tejun Heocb98fc82005-10-28 08:29:39 +02001087 elevator_t *old_elevator, *e;
Jens Axboebc1c1162006-06-08 08:49:06 +02001088 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Tejun Heocb98fc82005-10-28 08:29:39 +02001090 /*
1091 * Allocate new elevator
1092 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001093 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!e)
Al Viro3d1ab402006-03-18 18:35:43 -05001095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Jens Axboebc1c1162006-06-08 08:49:06 +02001097 data = elevator_init_queue(q, e);
1098 if (!data) {
1099 kobject_put(&e->kobj);
1100 return 0;
1101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001104 * Turn on BYPASS and drain all requests w/ elevator private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001106 spin_lock_irq(q->queue_lock);
1107
Nick Piggin75ad23b2008-04-29 14:48:33 +02001108 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001109
Tejun Heo15853af2005-11-10 08:52:05 +01001110 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001111
1112 while (q->rq.elvpriv) {
Jens Axboe80a4b582008-10-14 09:51:06 +02001113 blk_start_queueing(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001114 spin_unlock_irq(q->queue_lock);
Jens Axboe64521d12005-10-28 08:30:39 +02001115 msleep(10);
Tejun Heocb98fc82005-10-28 08:29:39 +02001116 spin_lock_irq(q->queue_lock);
Tejun Heo15853af2005-11-10 08:52:05 +01001117 elv_drain_elevator(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001118 }
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
Jens Axboebc1c1162006-06-08 08:49:06 +02001121 * Remember old elevator.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 old_elevator = q->elevator;
1124
1125 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * attach and start new elevator
1127 */
Jens Axboebc1c1162006-06-08 08:49:06 +02001128 elevator_attach(q, e, data);
1129
1130 spin_unlock_irq(q->queue_lock);
1131
1132 __elv_unregister_queue(old_elevator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (elv_register_queue(q))
1135 goto fail_register;
1136
1137 /*
Tejun Heocb98fc82005-10-28 08:29:39 +02001138 * finally exit old elevator and turn off BYPASS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
1140 elevator_exit(old_elevator);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001141 spin_lock_irq(q->queue_lock);
1142 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1143 spin_unlock_irq(q->queue_lock);
1144
Alan D. Brunelle4722dc52008-05-27 14:55:00 +02001145 blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
1146
Al Viro3d1ab402006-03-18 18:35:43 -05001147 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149fail_register:
1150 /*
1151 * switch failed, exit the new io scheduler and reattach the old
1152 * one again (along with re-adding the sysfs dir)
1153 */
1154 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 q->elevator = old_elevator;
1156 elv_register_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001157
1158 spin_lock_irq(q->queue_lock);
1159 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
1160 spin_unlock_irq(q->queue_lock);
1161
Al Viro3d1ab402006-03-18 18:35:43 -05001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Jens Axboe165125e2007-07-24 09:28:11 +02001165ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1166 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 char elevator_name[ELV_NAME_MAX];
1169 struct elevator_type *e;
1170
Li Zefanee2e9922008-10-14 08:49:56 +02001171 strlcpy(elevator_name, name, sizeof(elevator_name));
1172 strstrip(elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 e = elevator_get(elevator_name);
1175 if (!e) {
1176 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1177 return -EINVAL;
1178 }
1179
Nate Diller2ca7d932005-10-30 15:02:24 -08001180 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
1181 elevator_put(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return count;
Nate Diller2ca7d932005-10-30 15:02:24 -08001183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Al Viro3d1ab402006-03-18 18:35:43 -05001185 if (!elevator_switch(q, e))
Jens Axboe4eb166d2008-02-01 00:37:27 +01001186 printk(KERN_ERR "elevator: switch to %s failed\n",
1187 elevator_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return count;
1189}
1190
Jens Axboe165125e2007-07-24 09:28:11 +02001191ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 elevator_t *e = q->elevator;
1194 struct elevator_type *elv = e->elevator_type;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001195 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int len = 0;
1197
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001198 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001199 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!strcmp(elv->elevator_name, __e->elevator_name))
1201 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1202 else
1203 len += sprintf(name+len, "%s ", __e->elevator_name);
1204 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001205 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 len += sprintf(len+name, "\n");
1208 return len;
1209}
1210
Jens Axboe165125e2007-07-24 09:28:11 +02001211struct request *elv_rb_former_request(struct request_queue *q,
1212 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001213{
1214 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1215
1216 if (rbprev)
1217 return rb_entry_rq(rbprev);
1218
1219 return NULL;
1220}
Jens Axboe2e662b62006-07-13 11:55:04 +02001221EXPORT_SYMBOL(elv_rb_former_request);
1222
Jens Axboe165125e2007-07-24 09:28:11 +02001223struct request *elv_rb_latter_request(struct request_queue *q,
1224 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001225{
1226 struct rb_node *rbnext = rb_next(&rq->rb_node);
1227
1228 if (rbnext)
1229 return rb_entry_rq(rbnext);
1230
1231 return NULL;
1232}
Jens Axboe2e662b62006-07-13 11:55:04 +02001233EXPORT_SYMBOL(elv_rb_latter_request);