blob: 55f1f1e5b2d249a16eacaf506c324a7b5251a73a [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>
Lin Ming0fb59e52013-03-23 11:42:27 +080037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Li Zefan55782132009-06-09 13:43:05 +080039#include <trace/events/block.h>
40
Jens Axboe242f9dc2008-09-14 05:55:09 -070041#include "blk.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static DEFINE_SPINLOCK(elv_list_lock);
44static LIST_HEAD(elv_list);
45
46/*
Jens Axboe98170642006-07-28 09:23:08 +020047 * Merge hash stuff.
48 */
49static const int elv_hash_shift = 6;
50#define ELV_HASH_BLOCK(sec) ((sec) >> 3)
Jens Axboe4eb166d2008-02-01 00:37:27 +010051#define ELV_HASH_FN(sec) \
52 (hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
Jens Axboe98170642006-07-28 09:23:08 +020053#define ELV_HASH_ENTRIES (1 << elv_hash_shift)
Tejun Heo83096eb2009-05-07 22:24:39 +090054#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020055
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 Axboeb374d182008-10-31 10:05:07 +010063 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010064
Tejun Heo22f746e2011-12-14 00:33:41 +010065 if (e->type->ops.elevator_allow_merge_fn)
66 return e->type->ops.elevator_allow_merge_fn(q, rq, bio);
Jens Axboeda775262006-12-20 11:04:12 +010067
68 return 1;
69}
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * can we safely merge with this request?
73 */
Tejun Heo050c8ea2012-02-08 09:19:38 +010074bool 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 */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +020082 if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
David Woodhousee17fc0a2008-08-09 16:42:20 +010083 return 0;
84
85 /*
Adrian Hunter8d57a982010-08-11 14:17:49 -070086 * Don't merge discard requests and secure discard requests
87 */
88 if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
89 return 0;
90
91 /*
Maya Erez73937f52012-05-24 23:33:05 +030092 * Don't merge sanitize requests
93 */
94 if ((bio->bi_rw & REQ_SANITIZE) != (rq->bio->bi_rw & REQ_SANITIZE))
95 return 0;
96
97 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * different data direction or already started, don't merge
99 */
100 if (bio_data_dir(bio) != rq_data_dir(rq))
101 return 0;
102
103 /*
Jens Axboeda775262006-12-20 11:04:12 +0100104 * must be same device and not a special request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Jens Axboebb4067e2006-12-21 21:20:01 +0100106 if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
Jens Axboeda775262006-12-20 11:04:12 +0100107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200109 /*
110 * only merge integrity protected bio into ditto rq
111 */
112 if (bio_integrity(bio) != blk_integrity_rq(rq))
113 return 0;
114
Jens Axboeda775262006-12-20 11:04:12 +0100115 if (!elv_iosched_allow_merge(rq, bio))
116 return 0;
117
118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120EXPORT_SYMBOL(elv_rq_merge_ok);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static struct elevator_type *elevator_find(const char *name)
123{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200124 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200126 list_for_each_entry(e, &elv_list, list) {
Vasily Tarasova22b1692006-10-11 09:24:27 +0200127 if (!strcmp(e->elevator_name, name))
128 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Vasily Tarasova22b1692006-10-11 09:24:27 +0200131 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static void elevator_put(struct elevator_type *e)
135{
136 module_put(e->elevator_owner);
137}
138
139static struct elevator_type *elevator_get(const char *name)
140{
Tejun Heo2824bc932005-10-20 10:56:41 +0200141 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200143 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200144
145 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100146 if (!e) {
Jens Axboee1640942008-02-19 10:20:37 +0100147 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600148 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100149 spin_lock(&elv_list_lock);
150 e = elevator_find(name);
151 }
152
Tejun Heo2824bc932005-10-20 10:56:41 +0200153 if (e && !try_module_get(e->elevator_owner))
154 e = NULL;
155
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200156 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return e;
159}
160
Tejun Heof8fc8772011-12-14 00:33:40 +0100161static int elevator_init_queue(struct request_queue *q,
162 struct elevator_queue *eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Tejun Heo22f746e2011-12-14 00:33:41 +0100164 eq->elevator_data = eq->type->ops.elevator_init_fn(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100165 if (eq->elevator_data)
166 return 0;
167 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Wang Sheng-Hui484fc252011-09-08 12:32:14 +0200170static char chosen_elevator[ELV_NAME_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Nate Diller5f003972006-01-24 10:07:58 +0100172static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100174 /*
175 * Be backwards-compatible with previous kernels, so users
176 * won't get the wrong elevator.
177 */
Jens Axboe492af632009-10-03 09:37:51 +0200178 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800179 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182__setup("elevator=", elevator_setup);
183
Al Viro3d1ab402006-03-18 18:35:43 -0500184static struct kobj_type elv_ktype;
185
Jens Axboeb374d182008-10-31 10:05:07 +0100186static struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200187 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500188{
Jens Axboeb374d182008-10-31 10:05:07 +0100189 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200190 int i;
191
Jens Axboeb374d182008-10-31 10:05:07 +0100192 eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200193 if (unlikely(!eq))
194 goto err;
195
Tejun Heo22f746e2011-12-14 00:33:41 +0100196 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700197 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200198 mutex_init(&eq->sysfs_lock);
199
Jens Axboeb5deef92006-07-19 23:39:40 +0200200 eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
201 GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200202 if (!eq->hash)
203 goto err;
204
205 for (i = 0; i < ELV_HASH_ENTRIES; i++)
206 INIT_HLIST_HEAD(&eq->hash[i]);
207
Al Viro3d1ab402006-03-18 18:35:43 -0500208 return eq;
Jens Axboe98170642006-07-28 09:23:08 +0200209err:
210 kfree(eq);
211 elevator_put(e);
212 return NULL;
Al Viro3d1ab402006-03-18 18:35:43 -0500213}
214
215static void elevator_release(struct kobject *kobj)
216{
Jens Axboeb374d182008-10-31 10:05:07 +0100217 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200218
Jens Axboeb374d182008-10-31 10:05:07 +0100219 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100220 elevator_put(e->type);
Jens Axboe98170642006-07-28 09:23:08 +0200221 kfree(e->hash);
Al Viro3d1ab402006-03-18 18:35:43 -0500222 kfree(e);
223}
224
Jens Axboe165125e2007-07-24 09:28:11 +0200225int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct elevator_type *e = NULL;
228 struct elevator_queue *eq;
Tejun Heof8fc8772011-12-14 00:33:40 +0100229 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400231 if (unlikely(q->elevator))
232 return 0;
233
Tejun Heocb98fc82005-10-28 08:29:39 +0200234 INIT_LIST_HEAD(&q->queue_head);
235 q->last_merge = NULL;
236 q->end_sector = 0;
237 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200238
Jens Axboe4eb166d2008-02-01 00:37:27 +0100239 if (name) {
240 e = elevator_get(name);
241 if (!e)
242 return -EINVAL;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jens Axboe4eb166d2008-02-01 00:37:27 +0100245 if (!e && *chosen_elevator) {
246 e = elevator_get(chosen_elevator);
247 if (!e)
248 printk(KERN_ERR "I/O scheduler %s not found\n",
249 chosen_elevator);
250 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100251
Jens Axboe4eb166d2008-02-01 00:37:27 +0100252 if (!e) {
253 e = elevator_get(CONFIG_DEFAULT_IOSCHED);
254 if (!e) {
255 printk(KERN_ERR
256 "Default I/O scheduler not found. " \
257 "Using noop.\n");
258 e = elevator_get("noop");
259 }
Nate Diller5f003972006-01-24 10:07:58 +0100260 }
261
Jens Axboeb5deef92006-07-19 23:39:40 +0200262 eq = elevator_alloc(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500263 if (!eq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Tejun Heof8fc8772011-12-14 00:33:40 +0100266 err = elevator_init_queue(q, eq);
267 if (err) {
Al Viro3d1ab402006-03-18 18:35:43 -0500268 kobject_put(&eq->kobj);
Tejun Heof8fc8772011-12-14 00:33:40 +0100269 return err;
Jens Axboebc1c1162006-06-08 08:49:06 +0200270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Tejun Heof8fc8772011-12-14 00:33:40 +0100272 q->elevator = eq;
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Jens Axboe2e662b62006-07-13 11:55:04 +0200275EXPORT_SYMBOL(elevator_init);
276
Jens Axboeb374d182008-10-31 10:05:07 +0100277void elevator_exit(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Al Viro3d1ab402006-03-18 18:35:43 -0500279 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100280 if (e->type->ops.elevator_exit_fn)
281 e->type->ops.elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500282 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Al Viro3d1ab402006-03-18 18:35:43 -0500284 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
Jens Axboe2e662b62006-07-13 11:55:04 +0200286EXPORT_SYMBOL(elevator_exit);
287
Jens Axboe98170642006-07-28 09:23:08 +0200288static inline void __elv_rqhash_del(struct request *rq)
289{
290 hlist_del_init(&rq->hash);
291}
292
Jens Axboe165125e2007-07-24 09:28:11 +0200293static void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200294{
295 if (ELV_ON_HASH(rq))
296 __elv_rqhash_del(rq);
297}
298
Jens Axboe165125e2007-07-24 09:28:11 +0200299static void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200300{
Jens Axboeb374d182008-10-31 10:05:07 +0100301 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200302
303 BUG_ON(ELV_ON_HASH(rq));
304 hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
305}
306
Jens Axboe165125e2007-07-24 09:28:11 +0200307static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200308{
309 __elv_rqhash_del(rq);
310 elv_rqhash_add(q, rq);
311}
312
Jens Axboe165125e2007-07-24 09:28:11 +0200313static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200314{
Jens Axboeb374d182008-10-31 10:05:07 +0100315 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200316 struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
317 struct hlist_node *entry, *next;
318 struct request *rq;
319
320 hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
321 BUG_ON(!ELV_ON_HASH(rq));
322
323 if (unlikely(!rq_mergeable(rq))) {
324 __elv_rqhash_del(rq);
325 continue;
326 }
327
328 if (rq_hash_key(rq) == offset)
329 return rq;
330 }
331
332 return NULL;
333}
334
Tejun Heo8922e162005-10-20 16:23:44 +0200335/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200336 * RB-tree support functions for inserting/lookup/removal of requests
337 * in a sorted RB tree.
338 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200339void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200340{
341 struct rb_node **p = &root->rb_node;
342 struct rb_node *parent = NULL;
343 struct request *__rq;
344
345 while (*p) {
346 parent = *p;
347 __rq = rb_entry(parent, struct request, rb_node);
348
Tejun Heo83096eb2009-05-07 22:24:39 +0900349 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200350 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200351 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200352 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200353 }
354
355 rb_link_node(&rq->rb_node, parent, p);
356 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200357}
Jens Axboe2e662b62006-07-13 11:55:04 +0200358EXPORT_SYMBOL(elv_rb_add);
359
360void elv_rb_del(struct rb_root *root, struct request *rq)
361{
362 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
363 rb_erase(&rq->rb_node, root);
364 RB_CLEAR_NODE(&rq->rb_node);
365}
Jens Axboe2e662b62006-07-13 11:55:04 +0200366EXPORT_SYMBOL(elv_rb_del);
367
368struct request *elv_rb_find(struct rb_root *root, sector_t sector)
369{
370 struct rb_node *n = root->rb_node;
371 struct request *rq;
372
373 while (n) {
374 rq = rb_entry(n, struct request, rb_node);
375
Tejun Heo83096eb2009-05-07 22:24:39 +0900376 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200377 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900378 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200379 n = n->rb_right;
380 else
381 return rq;
382 }
383
384 return NULL;
385}
Jens Axboe2e662b62006-07-13 11:55:04 +0200386EXPORT_SYMBOL(elv_rb_find);
387
388/*
Tejun Heo8922e162005-10-20 16:23:44 +0200389 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200390 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200391 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200392 */
Jens Axboe165125e2007-07-24 09:28:11 +0200393void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200394{
395 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200396 struct list_head *entry;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100397 int stop_flags;
Tejun Heo8922e162005-10-20 16:23:44 +0200398
Tejun Heo06b86242005-10-20 16:46:23 +0200399 if (q->last_merge == rq)
400 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200401
402 elv_rqhash_del(q, rq);
403
Tejun Heo15853af2005-11-10 08:52:05 +0100404 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200405
Jens Axboe1b47f532005-10-20 16:37:00 +0200406 boundary = q->end_sector;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100407 stop_flags = REQ_SOFTBARRIER | REQ_STARTED;
Tejun Heo8922e162005-10-20 16:23:44 +0200408 list_for_each_prev(entry, &q->queue_head) {
409 struct request *pos = list_entry_rq(entry);
410
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200411 if ((rq->cmd_flags & REQ_DISCARD) !=
412 (pos->cmd_flags & REQ_DISCARD))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100413 break;
Jens Axboe783660b2007-01-19 11:27:47 +1100414 if (rq_data_dir(rq) != rq_data_dir(pos))
415 break;
Jens Axboe4eb166d2008-02-01 00:37:27 +0100416 if (pos->cmd_flags & stop_flags)
Tejun Heo8922e162005-10-20 16:23:44 +0200417 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900418 if (blk_rq_pos(rq) >= boundary) {
419 if (blk_rq_pos(pos) < boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200420 continue;
421 } else {
Tejun Heo83096eb2009-05-07 22:24:39 +0900422 if (blk_rq_pos(pos) >= boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200423 break;
424 }
Tejun Heo83096eb2009-05-07 22:24:39 +0900425 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
Tejun Heo8922e162005-10-20 16:23:44 +0200426 break;
427 }
428
429 list_add(&rq->queuelist, entry);
430}
Jens Axboe2e662b62006-07-13 11:55:04 +0200431EXPORT_SYMBOL(elv_dispatch_sort);
432
Jens Axboe98170642006-07-28 09:23:08 +0200433/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200434 * Insert rq into dispatch queue of q. Queue lock must be held on
435 * entry. rq is added to the back of the dispatch queue. To be used by
436 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200437 */
438void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
439{
440 if (q->last_merge == rq)
441 q->last_merge = NULL;
442
443 elv_rqhash_del(q, rq);
444
445 q->nr_sorted--;
446
447 q->end_sector = rq_end_sector(rq);
448 q->boundary_rq = rq;
449 list_add_tail(&rq->queuelist, &q->queue_head);
450}
Jens Axboe2e662b62006-07-13 11:55:04 +0200451EXPORT_SYMBOL(elv_dispatch_add_tail);
452
Jens Axboe165125e2007-07-24 09:28:11 +0200453int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jens Axboeb374d182008-10-31 10:05:07 +0100455 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200456 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200457 int ret;
458
Jens Axboe98170642006-07-28 09:23:08 +0200459 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100460 * Levels of merges:
461 * nomerges: No merges at all attempted
462 * noxmerges: Only simple one-hit cache try
463 * merges: All merge tries attempted
464 */
465 if (blk_queue_nomerges(q))
466 return ELEVATOR_NO_MERGE;
467
468 /*
Jens Axboe98170642006-07-28 09:23:08 +0200469 * First try one-hit cache.
470 */
Tejun Heo050c8ea2012-02-08 09:19:38 +0100471 if (q->last_merge && elv_rq_merge_ok(q->last_merge, bio)) {
472 ret = blk_try_merge(q->last_merge, bio);
Tejun Heo06b86242005-10-20 16:46:23 +0200473 if (ret != ELEVATOR_NO_MERGE) {
474 *req = q->last_merge;
475 return ret;
476 }
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100479 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200480 return ELEVATOR_NO_MERGE;
481
Jens Axboe98170642006-07-28 09:23:08 +0200482 /*
483 * See if our hash lookup can find a potential backmerge.
484 */
485 __rq = elv_rqhash_find(q, bio->bi_sector);
486 if (__rq && elv_rq_merge_ok(__rq, bio)) {
487 *req = __rq;
488 return ELEVATOR_BACK_MERGE;
489 }
490
Tejun Heo22f746e2011-12-14 00:33:41 +0100491 if (e->type->ops.elevator_merge_fn)
492 return e->type->ops.elevator_merge_fn(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 return ELEVATOR_NO_MERGE;
495}
496
Jens Axboe5e84ea32011-03-21 10:14:27 +0100497/*
498 * Attempt to do an insertion back merge. Only check for the case where
499 * we can append 'rq' to an existing request, so we can throw 'rq' away
500 * afterwards.
501 *
502 * Returns true if we merged, false otherwise
503 */
504static bool elv_attempt_insert_merge(struct request_queue *q,
505 struct request *rq)
506{
507 struct request *__rq;
508
509 if (blk_queue_nomerges(q))
510 return false;
511
512 /*
513 * First try one-hit cache.
514 */
515 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
516 return true;
517
518 if (blk_queue_noxmerges(q))
519 return false;
520
521 /*
522 * See if our hash lookup can find a potential backmerge.
523 */
Jens Axboe5d381ef2012-01-15 10:29:48 +0100524 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
525 if (__rq && blk_attempt_req_merge(q, __rq, rq))
526 return true;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100527
Jens Axboe5d381ef2012-01-15 10:29:48 +0100528 return false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100529}
530
Jens Axboe165125e2007-07-24 09:28:11 +0200531void elv_merged_request(struct request_queue *q, struct request *rq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Jens Axboeb374d182008-10-31 10:05:07 +0100533 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Tejun Heo22f746e2011-12-14 00:33:41 +0100535 if (e->type->ops.elevator_merged_fn)
536 e->type->ops.elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200537
Jens Axboe2e662b62006-07-13 11:55:04 +0200538 if (type == ELEVATOR_BACK_MERGE)
539 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200540
Tejun Heo06b86242005-10-20 16:46:23 +0200541 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Jens Axboe165125e2007-07-24 09:28:11 +0200544void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct request *next)
546{
Jens Axboeb374d182008-10-31 10:05:07 +0100547 struct elevator_queue *e = q->elevator;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100548 const int next_sorted = next->cmd_flags & REQ_SORTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Tejun Heo22f746e2011-12-14 00:33:41 +0100550 if (next_sorted && e->type->ops.elevator_merge_req_fn)
551 e->type->ops.elevator_merge_req_fn(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200552
Jens Axboe98170642006-07-28 09:23:08 +0200553 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200554
Jens Axboe5e84ea32011-03-21 10:14:27 +0100555 if (next_sorted) {
556 elv_rqhash_del(q, next);
557 q->nr_sorted--;
558 }
559
Tejun Heo06b86242005-10-20 16:46:23 +0200560 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Divyesh Shah812d4022010-04-08 21:14:23 -0700563void elv_bio_merged(struct request_queue *q, struct request *rq,
564 struct bio *bio)
565{
566 struct elevator_queue *e = q->elevator;
567
Tejun Heo22f746e2011-12-14 00:33:41 +0100568 if (e->type->ops.elevator_bio_merged_fn)
569 e->type->ops.elevator_bio_merged_fn(q, rq, bio);
Divyesh Shah812d4022010-04-08 21:14:23 -0700570}
571
Lin Ming0fb59e52013-03-23 11:42:27 +0800572#ifdef CONFIG_PM_RUNTIME
573static void blk_pm_requeue_request(struct request *rq)
574{
575 if (rq->q->dev && !(rq->cmd_flags & REQ_PM))
576 rq->q->nr_pending--;
577}
578
579static void blk_pm_add_request(struct request_queue *q, struct request *rq)
580{
581 if (q->dev && !(rq->cmd_flags & REQ_PM) && q->nr_pending++ == 0 &&
582 (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
583 pm_request_resume(q->dev);
584}
585#else
586static inline void blk_pm_requeue_request(struct request *rq) {}
587static inline void blk_pm_add_request(struct request_queue *q,
588 struct request *rq)
589{
590}
591#endif
592
Jens Axboe165125e2007-07-24 09:28:11 +0200593void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
596 * it already went through dequeue, we need to decrement the
597 * in_flight count again
598 */
Tejun Heo8922e162005-10-20 16:23:44 +0200599 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200600 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200601 if (rq->cmd_flags & REQ_SORTED)
Jens Axboecad97512007-01-14 22:26:09 +1100602 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Jens Axboe4aff5e22006-08-10 08:44:47 +0200605 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Lin Ming0fb59e52013-03-23 11:42:27 +0800607 blk_pm_requeue_request(rq);
608
Jens Axboeb710a482011-03-30 09:52:30 +0200609 __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Tatyana Brokhman57d80192012-12-04 15:54:43 +0200612/**
613 * elv_reinsert_request() - Insert a request back to the scheduler
614 * @q: request queue where request should be inserted
615 * @rq: request to be inserted
616 *
617 * This function returns the request back to the scheduler to be
618 * inserted as if it was never dispatched
619 *
620 * Return: 0 on success, error code on failure
621 */
622int elv_reinsert_request(struct request_queue *q, struct request *rq)
623{
624 int res;
625
626 if (!q->elevator->type->ops.elevator_reinsert_req_fn)
627 return -EPERM;
628
629 res = q->elevator->type->ops.elevator_reinsert_req_fn(q, rq);
630 if (!res) {
631 /*
632 * it already went through dequeue, we need to decrement the
633 * in_flight count again
634 */
635 if (blk_account_rq(rq)) {
636 q->in_flight[rq_is_sync(rq)]--;
637 if (rq->cmd_flags & REQ_SORTED)
638 elv_deactivate_rq(q, rq);
639 }
640 rq->cmd_flags &= ~REQ_STARTED;
641 q->nr_sorted++;
642 }
643
644 return res;
645}
646
Jerome Marchand26308ea2009-03-27 10:31:51 +0100647void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100648{
649 static int printed;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200650
651 lockdep_assert_held(q->queue_lock);
652
Tejun Heo22f746e2011-12-14 00:33:41 +0100653 while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
Tejun Heo15853af2005-11-10 08:52:05 +0100654 ;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200655 if (q->nr_sorted && printed++ < 10) {
Tejun Heo15853af2005-11-10 08:52:05 +0100656 printk(KERN_ERR "%s: forced dispatching is broken "
657 "(nr_sorted=%u), please report this\n",
Tejun Heo22f746e2011-12-14 00:33:41 +0100658 q->elevator->type->elevator_name, q->nr_sorted);
Tejun Heo15853af2005-11-10 08:52:05 +0100659 }
660}
661
Jens Axboef600abe2009-04-08 14:22:01 +0200662void elv_quiesce_start(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100663{
Martin K. Petersencd43e262009-05-22 17:17:52 -0400664 if (!q->elevator)
665 return;
666
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200667 spin_lock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100668 queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200669 spin_unlock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100670
Tejun Heoc9a929d2011-10-19 14:42:16 +0200671 blk_drain_queue(q, false);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100672}
673
Jens Axboef600abe2009-04-08 14:22:01 +0200674void elv_quiesce_end(struct request_queue *q)
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100675{
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200676 spin_lock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100677 queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200678 spin_unlock_irq(q->queue_lock);
Jens Axboe6c7e8ce2009-03-27 10:30:47 +0100679}
680
Jens Axboeb710a482011-03-30 09:52:30 +0200681void __elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100683 trace_block_rq_insert(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100684
Lin Ming0fb59e52013-03-23 11:42:27 +0800685 blk_pm_add_request(q, rq);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 rq->q = q;
688
Jens Axboeb710a482011-03-30 09:52:30 +0200689 if (rq->cmd_flags & REQ_SOFTBARRIER) {
690 /* barriers are scheduling boundary, update end_sector */
691 if (rq->cmd_type == REQ_TYPE_FS ||
Maya Erez73937f52012-05-24 23:33:05 +0300692 (rq->cmd_flags & (REQ_DISCARD | REQ_SANITIZE))) {
Jens Axboeb710a482011-03-30 09:52:30 +0200693 q->end_sector = rq_end_sector(rq);
694 q->boundary_rq = rq;
695 }
696 } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
Jens Axboe3aa72872011-04-21 19:28:35 +0200697 (where == ELEVATOR_INSERT_SORT ||
698 where == ELEVATOR_INSERT_SORT_MERGE))
Jens Axboeb710a482011-03-30 09:52:30 +0200699 where = ELEVATOR_INSERT_BACK;
700
Tejun Heo8922e162005-10-20 16:23:44 +0200701 switch (where) {
Tejun Heo28e7d182010-09-03 11:56:16 +0200702 case ELEVATOR_INSERT_REQUEUE:
Tejun Heo8922e162005-10-20 16:23:44 +0200703 case ELEVATOR_INSERT_FRONT:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200704 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200705 list_add(&rq->queuelist, &q->queue_head);
706 break;
707
708 case ELEVATOR_INSERT_BACK:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200709 rq->cmd_flags |= REQ_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100710 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200711 list_add_tail(&rq->queuelist, &q->queue_head);
712 /*
713 * We kick the queue here for the following reasons.
714 * - The elevator might have returned NULL previously
715 * to delay requests and returned them now. As the
716 * queue wasn't empty before this request, ll_rw_blk
717 * won't run the queue on return, resulting in hang.
718 * - Usually, back inserted requests won't be merged
719 * with anything. There's no point in delaying queue
720 * processing.
721 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200722 __blk_run_queue(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200723 break;
724
Jens Axboe5e84ea32011-03-21 10:14:27 +0100725 case ELEVATOR_INSERT_SORT_MERGE:
726 /*
727 * If we succeed in merging this request with one in the
728 * queue already, we are done - rq has now been freed,
729 * so no need to do anything further.
730 */
731 if (elv_attempt_insert_merge(q, rq))
732 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200733 case ELEVATOR_INSERT_SORT:
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200734 BUG_ON(rq->cmd_type != REQ_TYPE_FS &&
735 !(rq->cmd_flags & REQ_DISCARD));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200736 rq->cmd_flags |= REQ_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100737 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200738 if (rq_mergeable(rq)) {
739 elv_rqhash_add(q, rq);
740 if (!q->last_merge)
741 q->last_merge = rq;
742 }
743
Tejun Heoca235092005-11-01 17:23:49 +0900744 /*
745 * Some ioscheds (cfq) run q->request_fn directly, so
746 * rq cannot be accessed after calling
747 * elevator_add_req_fn.
748 */
Tejun Heo22f746e2011-12-14 00:33:41 +0100749 q->elevator->type->ops.elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200750 break;
751
Tejun Heoae1b1532011-01-25 12:43:54 +0100752 case ELEVATOR_INSERT_FLUSH:
753 rq->cmd_flags |= REQ_SOFTBARRIER;
754 blk_insert_flush(rq);
755 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200756 default:
757 printk(KERN_ERR "%s: bad insertion point %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700758 __func__, where);
Tejun Heo8922e162005-10-20 16:23:44 +0200759 BUG();
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Jens Axboe2e662b62006-07-13 11:55:04 +0200762EXPORT_SYMBOL(__elv_add_request);
763
Jens Axboe7eaceac2011-03-10 08:52:07 +0100764void elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 unsigned long flags;
767
768 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100769 __elv_add_request(q, rq, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 spin_unlock_irqrestore(q->queue_lock, flags);
771}
Jens Axboe2e662b62006-07-13 11:55:04 +0200772EXPORT_SYMBOL(elv_add_request);
773
Jens Axboe165125e2007-07-24 09:28:11 +0200774struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jens Axboeb374d182008-10-31 10:05:07 +0100776 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Tejun Heo22f746e2011-12-14 00:33:41 +0100778 if (e->type->ops.elevator_latter_req_fn)
779 return e->type->ops.elevator_latter_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return NULL;
781}
782
Jens Axboe165125e2007-07-24 09:28:11 +0200783struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jens Axboeb374d182008-10-31 10:05:07 +0100785 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Tejun Heo22f746e2011-12-14 00:33:41 +0100787 if (e->type->ops.elevator_former_req_fn)
788 return e->type->ops.elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return NULL;
790}
791
Jens Axboe165125e2007-07-24 09:28:11 +0200792int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Jens Axboeb374d182008-10-31 10:05:07 +0100794 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Tejun Heo22f746e2011-12-14 00:33:41 +0100796 if (e->type->ops.elevator_set_req_fn)
797 return e->type->ops.elevator_set_req_fn(q, rq, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
Jens Axboe165125e2007-07-24 09:28:11 +0200801void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Jens Axboeb374d182008-10-31 10:05:07 +0100803 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Tejun Heo22f746e2011-12-14 00:33:41 +0100805 if (e->type->ops.elevator_put_req_fn)
806 e->type->ops.elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Jens Axboe165125e2007-07-24 09:28:11 +0200809int elv_may_queue(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Jens Axboeb374d182008-10-31 10:05:07 +0100811 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Tejun Heo22f746e2011-12-14 00:33:41 +0100813 if (e->type->ops.elevator_may_queue_fn)
814 return e->type->ops.elevator_may_queue_fn(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 return ELV_MQUEUE_MAY;
817}
818
Mike Anderson11914a52008-09-13 20:31:27 +0200819void elv_abort_queue(struct request_queue *q)
820{
821 struct request *rq;
822
Tejun Heoae1b1532011-01-25 12:43:54 +0100823 blk_abort_flushes(q);
824
Mike Anderson11914a52008-09-13 20:31:27 +0200825 while (!list_empty(&q->queue_head)) {
826 rq = list_entry_rq(q->queue_head.next);
827 rq->cmd_flags |= REQ_QUIET;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100828 trace_block_rq_abort(q, rq);
Kiyoshi Ueda53c663c2009-06-02 08:44:01 +0200829 /*
830 * Mark this request as started so we don't trigger
831 * any debug logic in the end I/O path.
832 */
833 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900834 __blk_end_request_all(rq, -EIO);
Mike Anderson11914a52008-09-13 20:31:27 +0200835 }
836}
837EXPORT_SYMBOL(elv_abort_queue);
838
Jens Axboe165125e2007-07-24 09:28:11 +0200839void elv_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Jens Axboeb374d182008-10-31 10:05:07 +0100841 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Tatyana Brokhman2c415182013-03-12 21:02:33 +0200843 if (rq->cmd_flags & REQ_URGENT) {
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200844 q->notified_urgent = false;
845 q->dispatched_urgent = false;
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /*
848 * request is released from the driver, io must be done
849 */
Tejun Heo8922e162005-10-20 16:23:44 +0200850 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200851 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200852 if ((rq->cmd_flags & REQ_SORTED) &&
Tejun Heo22f746e2011-12-14 00:33:41 +0100853 e->type->ops.elevator_completed_req_fn)
854 e->type->ops.elevator_completed_req_fn(q, rq);
Tejun Heo1bc691d2006-01-12 15:39:26 +0100855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Al Viro3d1ab402006-03-18 18:35:43 -0500858#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
859
860static ssize_t
861elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
862{
Al Viro3d1ab402006-03-18 18:35:43 -0500863 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100864 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500865 ssize_t error;
866
867 if (!entry->show)
868 return -EIO;
869
Jens Axboeb374d182008-10-31 10:05:07 +0100870 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500871 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100872 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500873 mutex_unlock(&e->sysfs_lock);
874 return error;
875}
876
877static ssize_t
878elv_attr_store(struct kobject *kobj, struct attribute *attr,
879 const char *page, size_t length)
880{
Al Viro3d1ab402006-03-18 18:35:43 -0500881 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100882 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500883 ssize_t error;
884
885 if (!entry->store)
886 return -EIO;
887
Jens Axboeb374d182008-10-31 10:05:07 +0100888 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500889 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100890 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500891 mutex_unlock(&e->sysfs_lock);
892 return error;
893}
894
Emese Revfy52cf25d2010-01-19 02:58:23 +0100895static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500896 .show = elv_attr_show,
897 .store = elv_attr_store,
898};
899
900static struct kobj_type elv_ktype = {
901 .sysfs_ops = &elv_sysfs_ops,
902 .release = elevator_release,
903};
904
Tejun Heof8fc8772011-12-14 00:33:40 +0100905int __elv_register_queue(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Al Viro3d1ab402006-03-18 18:35:43 -0500907 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700909 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500910 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100911 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500912 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500913 while (attr->attr.name) {
914 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500915 break;
Al Viroe572ec72006-03-18 22:27:18 -0500916 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500917 }
918 }
919 kobject_uevent(&e->kobj, KOBJ_ADD);
Jens Axboe430c62f2010-10-07 09:35:16 +0200920 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500921 }
922 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Tejun Heof8fc8772011-12-14 00:33:40 +0100925int elv_register_queue(struct request_queue *q)
Jens Axboebc1c1162006-06-08 08:49:06 +0200926{
Tejun Heof8fc8772011-12-14 00:33:40 +0100927 return __elv_register_queue(q, q->elevator);
Jens Axboebc1c1162006-06-08 08:49:06 +0200928}
Tejun Heof8fc8772011-12-14 00:33:40 +0100929EXPORT_SYMBOL(elv_register_queue);
Jens Axboebc1c1162006-06-08 08:49:06 +0200930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931void elv_unregister_queue(struct request_queue *q)
932{
Tejun Heof8fc8772011-12-14 00:33:40 +0100933 if (q) {
934 struct elevator_queue *e = q->elevator;
935
936 kobject_uevent(&e->kobj, KOBJ_REMOVE);
937 kobject_del(&e->kobj);
938 e->registered = 0;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
Mike Snitzer01effb02010-05-11 08:57:42 +0200941EXPORT_SYMBOL(elv_unregister_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Tejun Heo3d3c2372011-12-14 00:33:42 +0100943int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100945 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200946
Tejun Heo3d3c2372011-12-14 00:33:42 +0100947 /* create icq_cache if requested */
948 if (e->icq_size) {
949 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
950 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
951 return -EINVAL;
952
953 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
954 "%s_io_cq", e->elevator_name);
955 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
956 e->icq_align, 0, NULL);
957 if (!e->icq_cache)
958 return -ENOMEM;
959 }
960
961 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200962 spin_lock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100963 if (elevator_find(e->elevator_name)) {
964 spin_unlock(&elv_list_lock);
965 if (e->icq_cache)
966 kmem_cache_destroy(e->icq_cache);
967 return -EBUSY;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200970 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Tejun Heo3d3c2372011-12-14 00:33:42 +0100972 /* print pretty message */
Nate Diller5f003972006-01-24 10:07:58 +0100973 if (!strcmp(e->elevator_name, chosen_elevator) ||
974 (!*chosen_elevator &&
975 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100976 def = " (default)";
977
Jens Axboe4eb166d2008-02-01 00:37:27 +0100978 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
979 def);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982EXPORT_SYMBOL_GPL(elv_register);
983
984void elv_unregister(struct elevator_type *e)
985{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100986 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200987 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200989 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100990
991 /*
992 * Destroy icq_cache if it exists. icq's are RCU managed. Make
993 * sure all RCU operations are complete before proceeding.
994 */
995 if (e->icq_cache) {
996 rcu_barrier();
997 kmem_cache_destroy(e->icq_cache);
998 e->icq_cache = NULL;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001EXPORT_SYMBOL_GPL(elv_unregister);
1002
1003/*
1004 * switch to new_e io scheduler. be careful not to introduce deadlocks -
1005 * we don't free the old io scheduler, before we have allocated what we
1006 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +02001007 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Jens Axboe165125e2007-07-24 09:28:11 +02001009static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Jens Axboeb374d182008-10-31 10:05:07 +01001011 struct elevator_queue *old_elevator, *e;
Jens Axboe5dd531a2010-08-23 13:52:19 +02001012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Tejun Heof8fc8772011-12-14 00:33:40 +01001014 /* allocate new elevator */
Jens Axboeb5deef92006-07-19 23:39:40 +02001015 e = elevator_alloc(q, new_e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!e)
Jens Axboe5dd531a2010-08-23 13:52:19 +02001017 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Tejun Heof8fc8772011-12-14 00:33:40 +01001019 err = elevator_init_queue(q, e);
1020 if (err) {
Jens Axboebc1c1162006-06-08 08:49:06 +02001021 kobject_put(&e->kobj);
Tejun Heof8fc8772011-12-14 00:33:40 +01001022 return err;
Jens Axboebc1c1162006-06-08 08:49:06 +02001023 }
1024
Tejun Heof8fc8772011-12-14 00:33:40 +01001025 /* turn on BYPASS and drain all requests w/ elevator private data */
Jens Axboef600abe2009-04-08 14:22:01 +02001026 elv_quiesce_start(q);
Tejun Heocb98fc82005-10-28 08:29:39 +02001027
Tejun Heof8fc8772011-12-14 00:33:40 +01001028 /* unregister old queue, register new one and kill old elevator */
1029 if (q->elevator->registered) {
1030 elv_unregister_queue(q);
1031 err = __elv_register_queue(q, e);
Jens Axboe430c62f2010-10-07 09:35:16 +02001032 if (err)
1033 goto fail_register;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Tejun Heo7e5a8792011-12-14 00:33:42 +01001036 /* done, clear io_cq's, switch elevators and turn off BYPASS */
Tejun Heof8fc8772011-12-14 00:33:40 +01001037 spin_lock_irq(q->queue_lock);
Tejun Heo7e5a8792011-12-14 00:33:42 +01001038 ioc_clear_queue(q);
Tejun Heof8fc8772011-12-14 00:33:40 +01001039 old_elevator = q->elevator;
1040 q->elevator = e;
1041 spin_unlock_irq(q->queue_lock);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 elevator_exit(old_elevator);
Jens Axboef600abe2009-04-08 14:22:01 +02001044 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001045
Tejun Heo22f746e2011-12-14 00:33:41 +01001046 blk_add_trace_msg(q, "elv switch: %s", e->type->elevator_name);
Alan D. Brunelle4722dc52008-05-27 14:55:00 +02001047
Jens Axboe5dd531a2010-08-23 13:52:19 +02001048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050fail_register:
1051 /*
1052 * switch failed, exit the new io scheduler and reattach the old
1053 * one again (along with re-adding the sysfs dir)
1054 */
1055 elevator_exit(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 elv_register_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +02001057 elv_quiesce_end(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +02001058
Jens Axboe5dd531a2010-08-23 13:52:19 +02001059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Jens Axboe5dd531a2010-08-23 13:52:19 +02001062/*
1063 * Switch this queue to the given IO scheduler.
1064 */
1065int elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 char elevator_name[ELV_NAME_MAX];
1068 struct elevator_type *e;
1069
Martin K. Petersencd43e262009-05-22 17:17:52 -04001070 if (!q->elevator)
Jens Axboe5dd531a2010-08-23 13:52:19 +02001071 return -ENXIO;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001072
Li Zefanee2e9922008-10-14 08:49:56 +02001073 strlcpy(elevator_name, name, sizeof(elevator_name));
KOSAKI Motohiro8c279592009-10-09 08:48:08 +02001074 e = elevator_get(strstrip(elevator_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!e) {
1076 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
1077 return -EINVAL;
1078 }
1079
Tejun Heo22f746e2011-12-14 00:33:41 +01001080 if (!strcmp(elevator_name, q->elevator->type->elevator_name)) {
Nate Diller2ca7d932005-10-30 15:02:24 -08001081 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +02001082 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -08001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Jens Axboe5dd531a2010-08-23 13:52:19 +02001085 return elevator_switch(q, e);
1086}
1087EXPORT_SYMBOL(elevator_change);
1088
1089ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1090 size_t count)
1091{
1092 int ret;
1093
1094 if (!q->elevator)
1095 return count;
1096
1097 ret = elevator_change(q, name);
1098 if (!ret)
1099 return count;
1100
1101 printk(KERN_ERR "elevator: switch to %s failed\n", name);
1102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Jens Axboe165125e2007-07-24 09:28:11 +02001105ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Jens Axboeb374d182008-10-31 10:05:07 +01001107 struct elevator_queue *e = q->elevator;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001108 struct elevator_type *elv;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001109 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int len = 0;
1111
Mike Snitzere36f7242010-05-24 09:07:32 +02001112 if (!q->elevator || !blk_queue_stackable(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -04001113 return sprintf(name, "none\n");
1114
Tejun Heo22f746e2011-12-14 00:33:41 +01001115 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001116
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001117 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001118 list_for_each_entry(__e, &elv_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!strcmp(elv->elevator_name, __e->elevator_name))
1120 len += sprintf(name+len, "[%s] ", elv->elevator_name);
1121 else
1122 len += sprintf(name+len, "%s ", __e->elevator_name);
1123 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001124 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 len += sprintf(len+name, "\n");
1127 return len;
1128}
1129
Jens Axboe165125e2007-07-24 09:28:11 +02001130struct request *elv_rb_former_request(struct request_queue *q,
1131 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001132{
1133 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1134
1135 if (rbprev)
1136 return rb_entry_rq(rbprev);
1137
1138 return NULL;
1139}
Jens Axboe2e662b62006-07-13 11:55:04 +02001140EXPORT_SYMBOL(elv_rb_former_request);
1141
Jens Axboe165125e2007-07-24 09:28:11 +02001142struct request *elv_rb_latter_request(struct request_queue *q,
1143 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001144{
1145 struct rb_node *rbnext = rb_next(&rq->rb_node);
1146
1147 if (rbnext)
1148 return rb_entry_rq(rbnext);
1149
1150 return NULL;
1151}
Jens Axboe2e662b62006-07-13 11:55:04 +02001152EXPORT_SYMBOL(elv_rb_latter_request);