blob: 91e18f8af9becaace380cc687d279eb1f47cb79a [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>
Jens Axboe2056a782006-03-23 20:00:26 +010034#include <linux/blktrace_api.h>
Jens Axboe98170642006-07-28 09:23:08 +020035#include <linux/hash.h>
Jens Axboe0835da62008-08-26 09:15:47 +020036#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Li Zefan55782132009-06-09 13:43:05 +080038#include <trace/events/block.h>
39
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)
Tejun Heo83096eb2009-05-07 22:24:39 +090053#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020054
55/*
Jens Axboeda775262006-12-20 11:04:12 +010056 * Query io scheduler to see if the current process issuing bio may be
57 * merged with rq.
58 */
59static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
60{
Jens Axboe165125e2007-07-24 09:28:11 +020061 struct request_queue *q = rq->q;
Jens Axboeb374d182008-10-31 10:05:07 +010062 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010063
Tejun Heo22f746e2011-12-14 00:33:41 +010064 if (e->type->ops.elevator_allow_merge_fn)
65 return e->type->ops.elevator_allow_merge_fn(q, rq, bio);
Jens Axboeda775262006-12-20 11:04:12 +010066
67 return 1;
68}
69
70/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * can we safely merge with this request?
72 */
Adrian Bunk72ed0bf2008-04-29 09:49:05 +020073int elv_rq_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 if (!rq_mergeable(rq))
76 return 0;
77
78 /*
David Woodhousee17fc0a2008-08-09 16:42:20 +010079 * Don't merge file system requests and discard requests
80 */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +020081 if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
David Woodhousee17fc0a2008-08-09 16:42:20 +010082 return 0;
83
84 /*
Adrian Hunter8d57a982010-08-11 14:17:49 -070085 * Don't merge discard requests and secure discard requests
86 */
87 if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
88 return 0;
89
90 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * different data direction or already started, don't merge
92 */
93 if (bio_data_dir(bio) != rq_data_dir(rq))
94 return 0;
95
96 /*
Jens Axboeda775262006-12-20 11:04:12 +010097 * must be same device and not a special request
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Jens Axboebb4067e2006-12-21 21:20:01 +010099 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
Jens Axboeda775262006-12-20 11:04:12 +0100100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200102 /*
103 * only merge integrity protected bio into ditto rq
104 */
105 if (bio_integrity(bio) != blk_integrity_rq(rq))
106 return 0;
107
Jens Axboeda775262006-12-20 11:04:12 +0100108 if (!elv_iosched_allow_merge(rq, bio))
109 return 0;
110
111 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113EXPORT_SYMBOL(elv_rq_merge_ok);
114
Jens Axboe73c10102011-03-08 13:19:51 +0100115int elv_try_merge(struct request *__rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 int ret = ELEVATOR_NO_MERGE;
118
119 /*
120 * we can merge and sequence is ok, check if it's possible
121 */
122 if (elv_rq_merge_ok(__rq, bio)) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900123 if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ret = ELEVATOR_BACK_MERGE;
Tejun Heo83096eb2009-05-07 22:24:39 +0900125 else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ret = ELEVATOR_FRONT_MERGE;
127 }
128
129 return ret;
130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct elevator_type *elevator_find(const char *name)
133{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200134 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200136 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +0200137 if (!strcmp(e->elevator_name, name))
138 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Vasily Tarasova22b1692006-10-11 09:24:27 +0200141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static void elevator_put(struct elevator_type *e)
145{
146 module_put(e->elevator_owner);
147}
148
149static struct elevator_type *elevator_get(const char *name)
150{
Tejun Heo2824bc932005-10-20 10:56:41 +0200151 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200153 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200154
155 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100156 if (!e) {
Jens Axboee1640942008-02-19 10:20:37 +0100157 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600158 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100159 spin_lock(&elv_list_lock);
160 e = elevator_find(name);
161 }
162
Tejun Heo2824bc932005-10-20 10:56:41 +0200163 if (e && !try_module_get(e->elevator_owner))
164 e = NULL;
165
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200166 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return e;
169}
170
Tejun Heof8fc8772011-12-14 00:33:40 +0100171static int elevator_init_queue(struct request_queue *q,
172 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Tejun Heo22f746e2011-12-14 00:33:41 +0100174 eq->elevator_data = eq->type->ops.elevator_init_fn(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100175 if (eq->elevator_data)
176 return 0;
177 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Wang Sheng-Hui484fc252011-09-08 12:32:14 +0200180static char chosen_elevator[ELV_NAME_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Nate Diller5f003972006-01-24 10:07:58 +0100182static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100184 /*
185 * Be backwards-compatible with previous kernels, so users
186 * won't get the wrong elevator.
187 */
Jens Axboe492af632009-10-03 09:37:51 +0200188 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192__setup("elevator=", elevator_setup);
193
Al Viro3d1ab402006-03-18 18:35:43 -0500194static struct kobj_type elv_ktype;
195
Jens Axboeb374d182008-10-31 10:05:07 +0100196static struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200197 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500198{
Jens Axboeb374d182008-10-31 10:05:07 +0100199 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200200 int i;
201
Jens Axboeb374d182008-10-31 10:05:07 +0100202 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200203 if (unlikely(!eq))
204 goto err;
205
Tejun Heo22f746e2011-12-14 00:33:41 +0100206 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700207 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200208 mutex_init(&eq->sysfs_lock);
209
Jens Axboeb5deef92006-07-19 23:39:40 +0200210 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
211 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200212 if (!eq->hash)
213 goto err;
214
215 for (i = 0; i < ELV_HASH_ENTRIES; i++)
216 INIT_HLIST_HEAD(&eq->hash[i]);
217
Al Viro3d1ab402006-03-18 18:35:43 -0500218 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200219err:
220 kfree(eq);
221 elevator_put(e);
222 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500223}
224
225static void elevator_release(struct kobject *kobj)
226{
Jens Axboeb374d182008-10-31 10:05:07 +0100227 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200228
Jens Axboeb374d182008-10-31 10:05:07 +0100229 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100230 elevator_put(e->type);
Jens Axboe98170642006-07-28 09:23:08 +0200231 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500232 kfree(e);
233}
234
Jens Axboe165125e2007-07-24 09:28:11 +0200235int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct elevator_type *e = NULL;
238 struct elevator_queue *eq;
Tejun Heof8fc8772011-12-14 00:33:40 +0100239 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400241 if (unlikely(q->elevator))
242 return 0;
243
Tejun Heocb98fc82005-10-28 08:29:39 +0200244 INIT_LIST_HEAD(&q->queue_head);
245 q->last_merge = NULL;
246 q->end_sector = 0;
247 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200248
Jens Axboe4eb166d2008-02-01 00:37:27 +0100249 if (name) {
250 e = elevator_get(name);
251 if (!e)
252 return -EINVAL;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jens Axboe4eb166d2008-02-01 00:37:27 +0100255 if (!e && *chosen_elevator) {
256 e = elevator_get(chosen_elevator);
257 if (!e)
258 printk(KERN_ERR "I/O scheduler %s not found\n",
259 chosen_elevator);
260 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100261
Jens Axboe4eb166d2008-02-01 00:37:27 +0100262 if (!e) {
263 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
264 if (!e) {
265 printk(KERN_ERR
266 "Default I/O scheduler not found. " \
267 "Using noop.\n");
268 e = elevator_get("noop");
269 }
Nate Diller5f003972006-01-24 10:07:58 +0100270 }
271
Jens Axboeb5deef92006-07-19 23:39:40 +0200272 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500273 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Tejun Heof8fc8772011-12-14 00:33:40 +0100276 err = elevator_init_queue(q, eq);
277 if (err) {
Al Viro3d1ab402006-03-18 18:35:43 -0500278 kobject_put(&eq->kobj);
Tejun Heof8fc8772011-12-14 00:33:40 +0100279 return err;
Jens Axboebc1c1162006-06-08 08:49:06 +0200280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Tejun Heof8fc8772011-12-14 00:33:40 +0100282 q->elevator = eq;
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
Jens Axboe2e662b62006-07-13 11:55:04 +0200285EXPORT_SYMBOL(elevator_init);
286
Jens Axboeb374d182008-10-31 10:05:07 +0100287void elevator_exit(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Al Viro3d1ab402006-03-18 18:35:43 -0500289 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100290 if (e->type->ops.elevator_exit_fn)
291 e->type->ops.elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500292 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Al Viro3d1ab402006-03-18 18:35:43 -0500294 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Jens Axboe2e662b62006-07-13 11:55:04 +0200296EXPORT_SYMBOL(elevator_exit);
297
Jens Axboe98170642006-07-28 09:23:08 +0200298static inline void __elv_rqhash_del(struct request *rq)
299{
300 hlist_del_init(&rq->hash);
301}
302
Jens Axboe165125e2007-07-24 09:28:11 +0200303static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200304{
305 if (ELV_ON_HASH(rq))
306 __elv_rqhash_del(rq);
307}
308
Jens Axboe165125e2007-07-24 09:28:11 +0200309static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200310{
Jens Axboeb374d182008-10-31 10:05:07 +0100311 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200312
313 BUG_ON(ELV_ON_HASH(rq));
314 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
315}
316
Jens Axboe165125e2007-07-24 09:28:11 +0200317static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200318{
319 __elv_rqhash_del(rq);
320 elv_rqhash_add(q, rq);
321}
322
Jens Axboe165125e2007-07-24 09:28:11 +0200323static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200324{
Jens Axboeb374d182008-10-31 10:05:07 +0100325 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200326 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
327 struct hlist_node *entry, *next;
328 struct request *rq;
329
330 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
331 BUG_ON(!ELV_ON_HASH(rq));
332
333 if (unlikely(!rq_mergeable(rq))) {
334 __elv_rqhash_del(rq);
335 continue;
336 }
337
338 if (rq_hash_key(rq) == offset)
339 return rq;
340 }
341
342 return NULL;
343}
344
Tejun Heo8922e162005-10-20 16:23:44 +0200345/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200346 * RB-tree support functions for inserting/lookup/removal of requests
347 * in a sorted RB tree.
348 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200349void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200350{
351 struct rb_node **p = &root->rb_node;
352 struct rb_node *parent = NULL;
353 struct request *__rq;
354
355 while (*p) {
356 parent = *p;
357 __rq = rb_entry(parent, struct request, rb_node);
358
Tejun Heo83096eb2009-05-07 22:24:39 +0900359 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200360 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200361 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200362 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200363 }
364
365 rb_link_node(&rq->rb_node, parent, p);
366 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200367}
Jens Axboe2e662b62006-07-13 11:55:04 +0200368EXPORT_SYMBOL(elv_rb_add);
369
370void elv_rb_del(struct rb_root *root, struct request *rq)
371{
372 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
373 rb_erase(&rq->rb_node, root);
374 RB_CLEAR_NODE(&rq->rb_node);
375}
Jens Axboe2e662b62006-07-13 11:55:04 +0200376EXPORT_SYMBOL(elv_rb_del);
377
378struct request *elv_rb_find(struct rb_root *root, sector_t sector)
379{
380 struct rb_node *n = root->rb_node;
381 struct request *rq;
382
383 while (n) {
384 rq = rb_entry(n, struct request, rb_node);
385
Tejun Heo83096eb2009-05-07 22:24:39 +0900386 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200387 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900388 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200389 n = n->rb_right;
390 else
391 return rq;
392 }
393
394 return NULL;
395}
Jens Axboe2e662b62006-07-13 11:55:04 +0200396EXPORT_SYMBOL(elv_rb_find);
397
398/*
Tejun Heo8922e162005-10-20 16:23:44 +0200399 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200400 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200401 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200402 */
Jens Axboe165125e2007-07-24 09:28:11 +0200403void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200404{
405 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200406 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100407 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200408
Tejun Heo06b86242005-10-20 16:46:23 +0200409 if (q->last_merge == rq)
410 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200411
412 elv_rqhash_del(q, rq);
413
Tejun Heo15853af2005-11-10 08:52:05 +0100414 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200415
Jens Axboe1b47f532005-10-20 16:37:00 +0200416 boundary = q->end_sector;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100417 stop_flags = REQ_SOFTBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200418 list_for_each_prev(entry, &q->queue_head) {
419 struct request *pos = list_entry_rq(entry);
420
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200421 if ((rq->cmd_flags & REQ_DISCARD) !=
422 (pos->cmd_flags & REQ_DISCARD))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100423 break;
Jens Axboe783660b2007-01-19 11:27:47 +1100424 if (rq_data_dir(rq) != rq_data_dir(pos))
425 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100426 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200427 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900428 if (blk_rq_pos(rq) >= boundary) {
429 if (blk_rq_pos(pos) < boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200430 continue;
431 } else {
Tejun Heo83096eb2009-05-07 22:24:39 +0900432 if (blk_rq_pos(pos) >= boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200433 break;
434 }
Tejun Heo83096eb2009-05-07 22:24:39 +0900435 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
Tejun Heo8922e162005-10-20 16:23:44 +0200436 break;
437 }
438
439 list_add(&rq->queuelist, entry);
440}
Jens Axboe2e662b62006-07-13 11:55:04 +0200441EXPORT_SYMBOL(elv_dispatch_sort);
442
Jens Axboe98170642006-07-28 09:23:08 +0200443/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200444 * Insert rq into dispatch queue of q. Queue lock must be held on
445 * entry. rq is added to the back of the dispatch queue. To be used by
446 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200447 */
448void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
449{
450 if (q->last_merge == rq)
451 q->last_merge = NULL;
452
453 elv_rqhash_del(q, rq);
454
455 q->nr_sorted--;
456
457 q->end_sector = rq_end_sector(rq);
458 q->boundary_rq = rq;
459 list_add_tail(&rq->queuelist, &q->queue_head);
460}
Jens Axboe2e662b62006-07-13 11:55:04 +0200461EXPORT_SYMBOL(elv_dispatch_add_tail);
462
Jens Axboe165125e2007-07-24 09:28:11 +0200463int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jens Axboeb374d182008-10-31 10:05:07 +0100465 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200466 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200467 int ret;
468
Jens Axboe98170642006-07-28 09:23:08 +0200469 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100470 * Levels of merges:
471 * nomerges: No merges at all attempted
472 * noxmerges: Only simple one-hit cache try
473 * merges: All merge tries attempted
474 */
475 if (blk_queue_nomerges(q))
476 return ELEVATOR_NO_MERGE;
477
478 /*
Jens Axboe98170642006-07-28 09:23:08 +0200479 * First try one-hit cache.
480 */
Tejun Heo06b86242005-10-20 16:46:23 +0200481 if (q->last_merge) {
482 ret = elv_try_merge(q->last_merge, bio);
483 if (ret != ELEVATOR_NO_MERGE) {
484 *req = q->last_merge;
485 return ret;
486 }
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100489 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200490 return ELEVATOR_NO_MERGE;
491
Jens Axboe98170642006-07-28 09:23:08 +0200492 /*
493 * See if our hash lookup can find a potential backmerge.
494 */
495 __rq = elv_rqhash_find(q, bio->bi_sector);
496 if (__rq && elv_rq_merge_ok(__rq, bio)) {
497 *req = __rq;
498 return ELEVATOR_BACK_MERGE;
499 }
500
Tejun Heo22f746e2011-12-14 00:33:41 +0100501 if (e->type->ops.elevator_merge_fn)
502 return e->type->ops.elevator_merge_fn(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return ELEVATOR_NO_MERGE;
505}
506
Jens Axboe5e84ea32011-03-21 10:14:27 +0100507/*
508 * Attempt to do an insertion back merge. Only check for the case where
509 * we can append 'rq' to an existing request, so we can throw 'rq' away
510 * afterwards.
511 *
512 * Returns true if we merged, false otherwise
513 */
514static bool elv_attempt_insert_merge(struct request_queue *q,
515 struct request *rq)
516{
517 struct request *__rq;
518
519 if (blk_queue_nomerges(q))
520 return false;
521
522 /*
523 * First try one-hit cache.
524 */
525 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
526 return true;
527
528 if (blk_queue_noxmerges(q))
529 return false;
530
531 /*
532 * See if our hash lookup can find a potential backmerge.
533 */
Jens Axboe5d381ef2012-01-15 10:29:48 +0100534 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
535 if (__rq && blk_attempt_req_merge(q, __rq, rq))
536 return true;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100537
Jens Axboe5d381ef2012-01-15 10:29:48 +0100538 return false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100539}
540
Jens Axboe165125e2007-07-24 09:28:11 +0200541void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jens Axboeb374d182008-10-31 10:05:07 +0100543 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Tejun Heo22f746e2011-12-14 00:33:41 +0100545 if (e->type->ops.elevator_merged_fn)
546 e->type->ops.elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200547
Jens Axboe2e662b62006-07-13 11:55:04 +0200548 if (type == ELEVATOR_BACK_MERGE)
549 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200550
Tejun Heo06b86242005-10-20 16:46:23 +0200551 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Jens Axboe165125e2007-07-24 09:28:11 +0200554void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct request *next)
556{
Jens Axboeb374d182008-10-31 10:05:07 +0100557 struct elevator_queue *e = q->elevator;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100558 const int next_sorted = next->cmd_flags & REQ_SORTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Tejun Heo22f746e2011-12-14 00:33:41 +0100560 if (next_sorted && e->type->ops.elevator_merge_req_fn)
561 e->type->ops.elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200562
Jens Axboe98170642006-07-28 09:23:08 +0200563 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200564
Jens Axboe5e84ea32011-03-21 10:14:27 +0100565 if (next_sorted) {
566 elv_rqhash_del(q, next);
567 q->nr_sorted--;
568 }
569
Tejun Heo06b86242005-10-20 16:46:23 +0200570 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Divyesh Shah812d4022010-04-08 21:14:23 -0700573void elv_bio_merged(struct request_queue *q, struct request *rq,
574 struct bio *bio)
575{
576 struct elevator_queue *e = q->elevator;
577
Tejun Heo22f746e2011-12-14 00:33:41 +0100578 if (e->type->ops.elevator_bio_merged_fn)
579 e->type->ops.elevator_bio_merged_fn(q, rq, bio);
Divyesh Shah812d4022010-04-08 21:14:23 -0700580}
581
Jens Axboe165125e2007-07-24 09:28:11 +0200582void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * it already went through dequeue, we need to decrement the
586 * in_flight count again
587 */
Tejun Heo8922e162005-10-20 16:23:44 +0200588 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200589 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200590 if (rq->cmd_flags & REQ_SORTED)
Jens Axboecad97512007-01-14 22:26:09 +1100591 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Jens Axboe4aff5e22006-08-10 08:44:47 +0200594 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Jens Axboeb710a482011-03-30 09:52:30 +0200596 __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Jerome Marchand26308ea2009-03-27 10:31:51 +0100599void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100600{
601 static int printed;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200602
603 lockdep_assert_held(q->queue_lock);
604
Tejun Heo22f746e2011-12-14 00:33:41 +0100605 while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
Tejun Heo15853af2005-11-10 08:52:05 +0100606 ;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200607 if (q->nr_sorted && printed++ < 10) {
Tejun Heo15853af2005-11-10 08:52:05 +0100608 printk(KERN_ERR "%s: forced dispatching is broken "
609 "(nr_sorted=%u), please report this\n",
Tejun Heo22f746e2011-12-14 00:33:41 +0100610 q->elevator->type->elevator_name, q->nr_sorted);
Tejun Heo15853af2005-11-10 08:52:05 +0100611 }
612}
613
Jens Axboef600abe2009-04-08 14:22:01 +0200614void elv_quiesce_start(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100615{
Martin K. Petersencd43e262009-05-22 17:17:52 -0400616 if (!q->elevator)
617 return;
618
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200619 spin_lock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100620 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200621 spin_unlock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100622
Tejun Heoc9a929d2011-10-19 14:42:16 +0200623 blk_drain_queue(q, false);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100624}
625
Jens Axboef600abe2009-04-08 14:22:01 +0200626void elv_quiesce_end(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100627{
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200628 spin_lock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100629 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200630 spin_unlock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100631}
632
Jens Axboeb710a482011-03-30 09:52:30 +0200633void __elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100635 trace_block_rq_insert(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 rq->q = q;
638
Jens Axboeb710a482011-03-30 09:52:30 +0200639 if (rq->cmd_flags & REQ_SOFTBARRIER) {
640 /* barriers are scheduling boundary, update end_sector */
641 if (rq->cmd_type == REQ_TYPE_FS ||
642 (rq->cmd_flags & REQ_DISCARD)) {
643 q->end_sector = rq_end_sector(rq);
644 q->boundary_rq = rq;
645 }
646 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
Jens Axboe3aa72872011-04-21 19:28:35 +0200647 (where == ELEVATOR_INSERT_SORT ||
648 where == ELEVATOR_INSERT_SORT_MERGE))
Jens Axboeb710a482011-03-30 09:52:30 +0200649 where = ELEVATOR_INSERT_BACK;
650
Tejun Heo8922e162005-10-20 16:23:44 +0200651 switch (where) {
Tejun Heo28e7d182010-09-03 11:56:16 +0200652 case ELEVATOR_INSERT_REQUEUE:
Tejun Heo8922e162005-10-20 16:23:44 +0200653 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200654 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200655 list_add(&rq->queuelist, &q->queue_head);
656 break;
657
658 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200659 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100660 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200661 list_add_tail(&rq->queuelist, &q->queue_head);
662 /*
663 * We kick the queue here for the following reasons.
664 * - The elevator might have returned NULL previously
665 * to delay requests and returned them now. As the
666 * queue wasn't empty before this request, ll_rw_blk
667 * won't run the queue on return, resulting in hang.
668 * - Usually, back inserted requests won't be merged
669 * with anything. There's no point in delaying queue
670 * processing.
671 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200672 __blk_run_queue(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200673 break;
674
Jens Axboe5e84ea32011-03-21 10:14:27 +0100675 case ELEVATOR_INSERT_SORT_MERGE:
676 /*
677 * If we succeed in merging this request with one in the
678 * queue already, we are done - rq has now been freed,
679 * so no need to do anything further.
680 */
681 if (elv_attempt_insert_merge(q, rq))
682 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200683 case ELEVATOR_INSERT_SORT:
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200684 BUG_ON(rq->cmd_type != REQ_TYPE_FS &&
685 !(rq->cmd_flags & REQ_DISCARD));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200686 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100687 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200688 if (rq_mergeable(rq)) {
689 elv_rqhash_add(q, rq);
690 if (!q->last_merge)
691 q->last_merge = rq;
692 }
693
Tejun Heoca235092005-11-01 17:23:49 +0900694 /*
695 * Some ioscheds (cfq) run q->request_fn directly, so
696 * rq cannot be accessed after calling
697 * elevator_add_req_fn.
698 */
Tejun Heo22f746e2011-12-14 00:33:41 +0100699 q->elevator->type->ops.elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200700 break;
701
Tejun Heoae1b1532011-01-25 12:43:54 +0100702 case ELEVATOR_INSERT_FLUSH:
703 rq->cmd_flags |= REQ_SOFTBARRIER;
704 blk_insert_flush(rq);
705 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200706 default:
707 printk(KERN_ERR "%s: bad insertion point %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700708 __func__, where);
Tejun Heo8922e162005-10-20 16:23:44 +0200709 BUG();
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
Jens Axboe2e662b62006-07-13 11:55:04 +0200712EXPORT_SYMBOL(__elv_add_request);
713
Jens Axboe7eaceac2011-03-10 08:52:07 +0100714void elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 unsigned long flags;
717
718 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100719 __elv_add_request(q, rq, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock_irqrestore(q->queue_lock, flags);
721}
Jens Axboe2e662b62006-07-13 11:55:04 +0200722EXPORT_SYMBOL(elv_add_request);
723
Jens Axboe165125e2007-07-24 09:28:11 +0200724struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Jens Axboeb374d182008-10-31 10:05:07 +0100726 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Tejun Heo22f746e2011-12-14 00:33:41 +0100728 if (e->type->ops.elevator_latter_req_fn)
729 return e->type->ops.elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return NULL;
731}
732
Jens Axboe165125e2007-07-24 09:28:11 +0200733struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Jens Axboeb374d182008-10-31 10:05:07 +0100735 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Tejun Heo22f746e2011-12-14 00:33:41 +0100737 if (e->type->ops.elevator_former_req_fn)
738 return e->type->ops.elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return NULL;
740}
741
Jens Axboe165125e2007-07-24 09:28:11 +0200742int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Jens Axboeb374d182008-10-31 10:05:07 +0100744 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Tejun Heo22f746e2011-12-14 00:33:41 +0100746 if (e->type->ops.elevator_set_req_fn)
747 return e->type->ops.elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
Jens Axboe165125e2007-07-24 09:28:11 +0200751void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Jens Axboeb374d182008-10-31 10:05:07 +0100753 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Tejun Heo22f746e2011-12-14 00:33:41 +0100755 if (e->type->ops.elevator_put_req_fn)
756 e->type->ops.elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Jens Axboe165125e2007-07-24 09:28:11 +0200759int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Jens Axboeb374d182008-10-31 10:05:07 +0100761 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Tejun Heo22f746e2011-12-14 00:33:41 +0100763 if (e->type->ops.elevator_may_queue_fn)
764 return e->type->ops.elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 return ELV_MQUEUE_MAY;
767}
768
Mike Anderson11914a52008-09-13 20:31:27 +0200769void elv_abort_queue(struct request_queue *q)
770{
771 struct request *rq;
772
Tejun Heoae1b1532011-01-25 12:43:54 +0100773 blk_abort_flushes(q);
774
Mike Anderson11914a52008-09-13 20:31:27 +0200775 while (!list_empty(&q->queue_head)) {
776 rq = list_entry_rq(q->queue_head.next);
777 rq->cmd_flags |= REQ_QUIET;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100778 trace_block_rq_abort(q, rq);
Kiyoshi Ueda53c663c2009-06-02 08:44:01 +0200779 /*
780 * Mark this request as started so we don't trigger
781 * any debug logic in the end I/O path.
782 */
783 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900784 __blk_end_request_all(rq, -EIO);
Mike Anderson11914a52008-09-13 20:31:27 +0200785 }
786}
787EXPORT_SYMBOL(elv_abort_queue);
788
Jens Axboe165125e2007-07-24 09:28:11 +0200789void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Jens Axboeb374d182008-10-31 10:05:07 +0100791 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /*
794 * request is released from the driver, io must be done
795 */
Tejun Heo8922e162005-10-20 16:23:44 +0200796 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200797 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200798 if ((rq->cmd_flags & REQ_SORTED) &&
Tejun Heo22f746e2011-12-14 00:33:41 +0100799 e->type->ops.elevator_completed_req_fn)
800 e->type->ops.elevator_completed_req_fn(q, rq);
Tejun Heo1bc691d2006-01-12 15:39:26 +0100801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Al Viro3d1ab402006-03-18 18:35:43 -0500804#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
805
806static ssize_t
807elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
808{
Al Viro3d1ab402006-03-18 18:35:43 -0500809 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100810 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500811 ssize_t error;
812
813 if (!entry->show)
814 return -EIO;
815
Jens Axboeb374d182008-10-31 10:05:07 +0100816 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500817 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100818 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500819 mutex_unlock(&e->sysfs_lock);
820 return error;
821}
822
823static ssize_t
824elv_attr_store(struct kobject *kobj, struct attribute *attr,
825 const char *page, size_t length)
826{
Al Viro3d1ab402006-03-18 18:35:43 -0500827 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100828 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500829 ssize_t error;
830
831 if (!entry->store)
832 return -EIO;
833
Jens Axboeb374d182008-10-31 10:05:07 +0100834 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500835 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100836 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500837 mutex_unlock(&e->sysfs_lock);
838 return error;
839}
840
Emese Revfy52cf25d2010-01-19 02:58:23 +0100841static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500842 .show = elv_attr_show,
843 .store = elv_attr_store,
844};
845
846static struct kobj_type elv_ktype = {
847 .sysfs_ops = &elv_sysfs_ops,
848 .release = elevator_release,
849};
850
Tejun Heof8fc8772011-12-14 00:33:40 +0100851int __elv_register_queue(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Al Viro3d1ab402006-03-18 18:35:43 -0500853 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700855 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500856 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100857 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500858 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500859 while (attr->attr.name) {
860 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500861 break;
Al Viroe572ec72006-03-18 22:27:18 -0500862 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500863 }
864 }
865 kobject_uevent(&e->kobj, KOBJ_ADD);
Jens Axboe430c62f2010-10-07 09:35:16 +0200866 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500867 }
868 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Tejun Heof8fc8772011-12-14 00:33:40 +0100871int elv_register_queue(struct request_queue *q)
Jens Axboebc1c1162006-06-08 08:49:06 +0200872{
Tejun Heof8fc8772011-12-14 00:33:40 +0100873 return __elv_register_queue(q, q->elevator);
Jens Axboebc1c1162006-06-08 08:49:06 +0200874}
Tejun Heof8fc8772011-12-14 00:33:40 +0100875EXPORT_SYMBOL(elv_register_queue);
Jens Axboebc1c1162006-06-08 08:49:06 +0200876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877void elv_unregister_queue(struct request_queue *q)
878{
Tejun Heof8fc8772011-12-14 00:33:40 +0100879 if (q) {
880 struct elevator_queue *e = q->elevator;
881
882 kobject_uevent(&e->kobj, KOBJ_REMOVE);
883 kobject_del(&e->kobj);
884 e->registered = 0;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Mike Snitzer01effb02010-05-11 08:57:42 +0200887EXPORT_SYMBOL(elv_unregister_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Tejun Heo3d3c2372011-12-14 00:33:42 +0100889int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100891 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200892
Tejun Heo3d3c2372011-12-14 00:33:42 +0100893 /* create icq_cache if requested */
894 if (e->icq_size) {
895 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
896 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
897 return -EINVAL;
898
899 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
900 "%s_io_cq", e->elevator_name);
901 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
902 e->icq_align, 0, NULL);
903 if (!e->icq_cache)
904 return -ENOMEM;
905 }
906
907 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200908 spin_lock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100909 if (elevator_find(e->elevator_name)) {
910 spin_unlock(&elv_list_lock);
911 if (e->icq_cache)
912 kmem_cache_destroy(e->icq_cache);
913 return -EBUSY;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200916 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Tejun Heo3d3c2372011-12-14 00:33:42 +0100918 /* print pretty message */
Nate Diller5f003972006-01-24 10:07:58 +0100919 if (!strcmp(e->elevator_name, chosen_elevator) ||
920 (!*chosen_elevator &&
921 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100922 def = " (default)";
923
Jens Axboe4eb166d2008-02-01 00:37:27 +0100924 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
925 def);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928EXPORT_SYMBOL_GPL(elv_register);
929
930void elv_unregister(struct elevator_type *e)
931{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100932 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200933 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200935 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100936
937 /*
938 * Destroy icq_cache if it exists. icq's are RCU managed. Make
939 * sure all RCU operations are complete before proceeding.
940 */
941 if (e->icq_cache) {
942 rcu_barrier();
943 kmem_cache_destroy(e->icq_cache);
944 e->icq_cache = NULL;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947EXPORT_SYMBOL_GPL(elv_unregister);
948
949/*
950 * switch to new_e io scheduler. be careful not to introduce deadlocks -
951 * we don't free the old io scheduler, before we have allocated what we
952 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200953 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Jens Axboe165125e2007-07-24 09:28:11 +0200955static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Jens Axboeb374d182008-10-31 10:05:07 +0100957 struct elevator_queue *old_elevator, *e;
Jens Axboe5dd531a2010-08-23 13:52:19 +0200958 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Tejun Heof8fc8772011-12-14 00:33:40 +0100960 /* allocate new elevator */
Jens Axboeb5deef92006-07-19 23:39:40 +0200961 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!e)
Jens Axboe5dd531a2010-08-23 13:52:19 +0200963 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Tejun Heof8fc8772011-12-14 00:33:40 +0100965 err = elevator_init_queue(q, e);
966 if (err) {
Jens Axboebc1c1162006-06-08 08:49:06 +0200967 kobject_put(&e->kobj);
Tejun Heof8fc8772011-12-14 00:33:40 +0100968 return err;
Jens Axboebc1c1162006-06-08 08:49:06 +0200969 }
970
Tejun Heof8fc8772011-12-14 00:33:40 +0100971 /* turn on BYPASS and drain all requests w/ elevator private data */
Jens Axboef600abe2009-04-08 14:22:01 +0200972 elv_quiesce_start(q);
Tejun Heocb98fc82005-10-28 08:29:39 +0200973
Tejun Heof8fc8772011-12-14 00:33:40 +0100974 /* unregister old queue, register new one and kill old elevator */
975 if (q->elevator->registered) {
976 elv_unregister_queue(q);
977 err = __elv_register_queue(q, e);
Jens Axboe430c62f2010-10-07 09:35:16 +0200978 if (err)
979 goto fail_register;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Tejun Heo7e5a8792011-12-14 00:33:42 +0100982 /* done, clear io_cq's, switch elevators and turn off BYPASS */
Tejun Heof8fc8772011-12-14 00:33:40 +0100983 spin_lock_irq(q->queue_lock);
Tejun Heo7e5a8792011-12-14 00:33:42 +0100984 ioc_clear_queue(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100985 old_elevator = q->elevator;
986 q->elevator = e;
987 spin_unlock_irq(q->queue_lock);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 elevator_exit(old_elevator);
Jens Axboef600abe2009-04-08 14:22:01 +0200990 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200991
Tejun Heo22f746e2011-12-14 00:33:41 +0100992 blk_add_trace_msg(q, "elv switch: %s", e->type->elevator_name);
Alan D. Brunelle4722dc52008-05-27 14:55:00 +0200993
Jens Axboe5dd531a2010-08-23 13:52:19 +0200994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996fail_register:
997 /*
998 * switch failed, exit the new io scheduler and reattach the old
999 * one again (along with re-adding the sysfs dir)
1000 */
1001 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 elv_register_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +02001003 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001004
Jens Axboe5dd531a2010-08-23 13:52:19 +02001005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Jens Axboe5dd531a2010-08-23 13:52:19 +02001008/*
1009 * Switch this queue to the given IO scheduler.
1010 */
1011int elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 char elevator_name[ELV_NAME_MAX];
1014 struct elevator_type *e;
1015
Martin K. Petersencd43e262009-05-22 17:17:52 -04001016 if (!q->elevator)
Jens Axboe5dd531a2010-08-23 13:52:19 +02001017 return -ENXIO;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001018
Li Zefanee2e9922008-10-14 08:49:56 +02001019 strlcpy(elevator_name, name, sizeof(elevator_name));
KOSAKI Motohiro8c279592009-10-09 08:48:08 +02001020 e = elevator_get(strstrip(elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!e) {
1022 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1023 return -EINVAL;
1024 }
1025
Tejun Heo22f746e2011-12-14 00:33:41 +01001026 if (!strcmp(elevator_name, q->elevator->type->elevator_name)) {
Nate Diller2ca7d932005-10-30 15:02:24 -08001027 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +02001028 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -08001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Jens Axboe5dd531a2010-08-23 13:52:19 +02001031 return elevator_switch(q, e);
1032}
1033EXPORT_SYMBOL(elevator_change);
1034
1035ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1036 size_t count)
1037{
1038 int ret;
1039
1040 if (!q->elevator)
1041 return count;
1042
1043 ret = elevator_change(q, name);
1044 if (!ret)
1045 return count;
1046
1047 printk(KERN_ERR "elevator: switch to %s failed\n", name);
1048 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Jens Axboe165125e2007-07-24 09:28:11 +02001051ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Jens Axboeb374d182008-10-31 10:05:07 +01001053 struct elevator_queue *e = q->elevator;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001054 struct elevator_type *elv;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001055 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int len = 0;
1057
Mike Snitzere36f7242010-05-24 09:07:32 +02001058 if (!q->elevator || !blk_queue_stackable(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -04001059 return sprintf(name, "none\n");
1060
Tejun Heo22f746e2011-12-14 00:33:41 +01001061 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001062
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001063 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001064 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (!strcmp(elv->elevator_name, __e->elevator_name))
1066 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1067 else
1068 len += sprintf(name+len, "%s ", __e->elevator_name);
1069 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001070 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 len += sprintf(len+name, "\n");
1073 return len;
1074}
1075
Jens Axboe165125e2007-07-24 09:28:11 +02001076struct request *elv_rb_former_request(struct request_queue *q,
1077 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001078{
1079 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1080
1081 if (rbprev)
1082 return rb_entry_rq(rbprev);
1083
1084 return NULL;
1085}
Jens Axboe2e662b62006-07-13 11:55:04 +02001086EXPORT_SYMBOL(elv_rb_former_request);
1087
Jens Axboe165125e2007-07-24 09:28:11 +02001088struct request *elv_rb_latter_request(struct request_queue *q,
1089 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001090{
1091 struct rb_node *rbnext = rb_next(&rq->rb_node);
1092
1093 if (rbnext)
1094 return rb_entry_rq(rbnext);
1095
1096 return NULL;
1097}
Jens Axboe2e662b62006-07-13 11:55:04 +02001098EXPORT_SYMBOL(elv_rb_latter_request);