blob: 77856bf295688f5c4c8675cdbb75e4411043ebe3 [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 Mingc8158812013-03-23 11:42:27 +080037#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040038#include <linux/blk-cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Li Zefan55782132009-06-09 13:43:05 +080040#include <trace/events/block.h>
41
Jens Axboe242f9dc2008-09-14 05:55:09 -070042#include "blk.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070043#include "blk-mq-sched.h"
Jan Kara8330cdb2017-04-19 11:33:27 +020044#include "blk-wbt.h"
Jens Axboe242f9dc2008-09-14 05:55:09 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_SPINLOCK(elv_list_lock);
47static LIST_HEAD(elv_list);
48
49/*
Jens Axboe98170642006-07-28 09:23:08 +020050 * Merge hash stuff.
51 */
Tejun Heo83096eb2009-05-07 22:24:39 +090052#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020053
54/*
Jens Axboeda775262006-12-20 11:04:12 +010055 * Query io scheduler to see if the current process issuing bio may be
56 * merged with rq.
57 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070058static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +010059{
Jens Axboe165125e2007-07-24 09:28:11 +020060 struct request_queue *q = rq->q;
Jens Axboeb374d182008-10-31 10:05:07 +010061 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010062
Jens Axboebd166ef2017-01-17 06:03:22 -070063 if (e->uses_mq && e->type->ops.mq.allow_merge)
64 return e->type->ops.mq.allow_merge(q, rq, bio);
65 else if (!e->uses_mq && e->type->ops.sq.elevator_allow_bio_merge_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -070066 return e->type->ops.sq.elevator_allow_bio_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 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070074bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Tejun Heo050c8ea2012-02-08 09:19:38 +010076 if (!blk_rq_merge_ok(rq, bio))
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070077 return false;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020078
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070079 if (!elv_iosched_allow_bio_merge(rq, bio))
80 return false;
Jens Axboeda775262006-12-20 11:04:12 +010081
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070082 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070084EXPORT_SYMBOL(elv_bio_merge_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jens Axboe2527d992017-10-25 12:33:42 -060086/*
87 * Return scheduler with name 'name' and with matching 'mq capability
88 */
89static struct elevator_type *elevator_find(const char *name, bool mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Vasily Tarasova22b1692006-10-11 09:24:27 +020091 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Matthias Kaehlcke70cee262007-07-10 12:26:24 +020093 list_for_each_entry(e, &elv_list, list) {
Jens Axboe2527d992017-10-25 12:33:42 -060094 if (!strcmp(e->elevator_name, name) && (mq == e->uses_mq))
Vasily Tarasova22b1692006-10-11 09:24:27 +020095 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Vasily Tarasova22b1692006-10-11 09:24:27 +020098 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static void elevator_put(struct elevator_type *e)
102{
103 module_put(e->elevator_owner);
104}
105
Jens Axboe2527d992017-10-25 12:33:42 -0600106static struct elevator_type *elevator_get(struct request_queue *q,
107 const char *name, bool try_loading)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Tejun Heo2824bc932005-10-20 10:56:41 +0200109 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200111 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200112
Jens Axboe2527d992017-10-25 12:33:42 -0600113 e = elevator_find(name, q->mq_ops != NULL);
Tejun Heo21c3c5d2013-01-22 16:48:03 -0800114 if (!e && try_loading) {
Jens Axboee1640942008-02-19 10:20:37 +0100115 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600116 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100117 spin_lock(&elv_list_lock);
Jens Axboe2527d992017-10-25 12:33:42 -0600118 e = elevator_find(name, q->mq_ops != NULL);
Jens Axboee1640942008-02-19 10:20:37 +0100119 }
120
Tejun Heo2824bc932005-10-20 10:56:41 +0200121 if (e && !try_module_get(e->elevator_owner))
122 e = NULL;
123
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200124 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return e;
126}
127
Wang Sheng-Hui484fc252011-09-08 12:32:14 +0200128static char chosen_elevator[ELV_NAME_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Nate Diller5f003972006-01-24 10:07:58 +0100130static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100132 /*
133 * Be backwards-compatible with previous kernels, so users
134 * won't get the wrong elevator.
135 */
Jens Axboe492af632009-10-03 09:37:51 +0200136 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800137 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140__setup("elevator=", elevator_setup);
141
Tejun Heobb813f42013-01-18 14:05:56 -0800142/* called during boot to load the elevator chosen by the elevator param */
143void __init load_default_elevator_module(void)
144{
145 struct elevator_type *e;
146
147 if (!chosen_elevator[0])
148 return;
149
Jens Axboe2527d992017-10-25 12:33:42 -0600150 /*
151 * Boot parameter is deprecated, we haven't supported that for MQ.
152 * Only look for non-mq schedulers from here.
153 */
Tejun Heobb813f42013-01-18 14:05:56 -0800154 spin_lock(&elv_list_lock);
Jens Axboe2527d992017-10-25 12:33:42 -0600155 e = elevator_find(chosen_elevator, false);
Tejun Heobb813f42013-01-18 14:05:56 -0800156 spin_unlock(&elv_list_lock);
157
158 if (!e)
159 request_module("%s-iosched", chosen_elevator);
160}
161
Al Viro3d1ab402006-03-18 18:35:43 -0500162static struct kobj_type elv_ktype;
163
Jianpeng Mad50235b2013-07-03 13:25:24 +0200164struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200165 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500166{
Jens Axboeb374d182008-10-31 10:05:07 +0100167 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200168
Joe Perchesc1b511e2013-08-29 15:21:42 -0700169 eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200170 if (unlikely(!eq))
Chao Yu8406a4d2015-04-23 10:47:44 -0600171 return NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200172
Tejun Heo22f746e2011-12-14 00:33:41 +0100173 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700174 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200175 mutex_init(&eq->sysfs_lock);
Sasha Levin242d98f2012-12-17 10:01:27 -0500176 hash_init(eq->hash);
Jens Axboebd166ef2017-01-17 06:03:22 -0700177 eq->uses_mq = e->uses_mq;
Jens Axboe98170642006-07-28 09:23:08 +0200178
Al Viro3d1ab402006-03-18 18:35:43 -0500179 return eq;
180}
Jianpeng Mad50235b2013-07-03 13:25:24 +0200181EXPORT_SYMBOL(elevator_alloc);
Al Viro3d1ab402006-03-18 18:35:43 -0500182
183static void elevator_release(struct kobject *kobj)
184{
Jens Axboeb374d182008-10-31 10:05:07 +0100185 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200186
Jens Axboeb374d182008-10-31 10:05:07 +0100187 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100188 elevator_put(e->type);
Al Viro3d1ab402006-03-18 18:35:43 -0500189 kfree(e);
190}
191
Jens Axboe165125e2007-07-24 09:28:11 +0200192int elevator_init(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct elevator_type *e = NULL;
Tejun Heof8fc8772011-12-14 00:33:40 +0100195 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600197 /*
198 * q->sysfs_lock must be held to provide mutual exclusion between
199 * elevator_switch() and here.
200 */
201 lockdep_assert_held(&q->sysfs_lock);
202
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400203 if (unlikely(q->elevator))
204 return 0;
205
Tejun Heocb98fc82005-10-28 08:29:39 +0200206 INIT_LIST_HEAD(&q->queue_head);
207 q->last_merge = NULL;
208 q->end_sector = 0;
209 q->boundary_rq = NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200210
Jens Axboe4eb166d2008-02-01 00:37:27 +0100211 if (name) {
Jens Axboe2527d992017-10-25 12:33:42 -0600212 e = elevator_get(q, name, true);
Jens Axboe4eb166d2008-02-01 00:37:27 +0100213 if (!e)
214 return -EINVAL;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Tejun Heo21c3c5d2013-01-22 16:48:03 -0800217 /*
Jens Axboed1a987f2017-02-14 08:16:41 -0700218 * Use the default elevator specified by config boot param for
219 * non-mq devices, or by config option. Don't try to load modules
220 * as we could be running off async and request_module() isn't
221 * allowed from async.
Tejun Heo21c3c5d2013-01-22 16:48:03 -0800222 */
Jens Axboed1a987f2017-02-14 08:16:41 -0700223 if (!e && !q->mq_ops && *chosen_elevator) {
Jens Axboe2527d992017-10-25 12:33:42 -0600224 e = elevator_get(q, chosen_elevator, false);
Jens Axboe4eb166d2008-02-01 00:37:27 +0100225 if (!e)
226 printk(KERN_ERR "I/O scheduler %s not found\n",
227 chosen_elevator);
228 }
Nate Diller248d5ca2006-01-24 10:09:14 +0100229
Jens Axboe4eb166d2008-02-01 00:37:27 +0100230 if (!e) {
Jens Axboeb86dd812017-02-22 13:19:45 -0700231 /*
232 * For blk-mq devices, we default to using mq-deadline,
233 * if available, for single queue devices. If deadline
234 * isn't available OR we have multiple queues, default
235 * to "none".
236 */
237 if (q->mq_ops) {
238 if (q->nr_hw_queues == 1)
Jens Axboe2527d992017-10-25 12:33:42 -0600239 e = elevator_get(q, "mq-deadline", false);
Jens Axboeb86dd812017-02-22 13:19:45 -0700240 if (!e)
241 return 0;
242 } else
Jens Axboe2527d992017-10-25 12:33:42 -0600243 e = elevator_get(q, CONFIG_DEFAULT_IOSCHED, false);
Jens Axboed3484992017-01-13 14:43:58 -0700244
Jens Axboe4eb166d2008-02-01 00:37:27 +0100245 if (!e) {
246 printk(KERN_ERR
247 "Default I/O scheduler not found. " \
Jens Axboeb86dd812017-02-22 13:19:45 -0700248 "Using noop.\n");
Jens Axboe2527d992017-10-25 12:33:42 -0600249 e = elevator_get(q, "noop", false);
Jens Axboe4eb166d2008-02-01 00:37:27 +0100250 }
Nate Diller5f003972006-01-24 10:07:58 +0100251 }
252
Omar Sandoval6917ff02017-04-05 12:01:30 -0700253 if (e->uses_mq)
254 err = blk_mq_init_sched(q, e);
255 else
Jens Axboebd166ef2017-01-17 06:03:22 -0700256 err = e->ops.sq.elevator_init_fn(q, e);
Omar Sandoval6917ff02017-04-05 12:01:30 -0700257 if (err)
Sudip Mukherjeed32f6b52014-10-23 22:16:48 +0530258 elevator_put(e);
259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Jens Axboe2e662b62006-07-13 11:55:04 +0200261EXPORT_SYMBOL(elevator_init);
262
Omar Sandoval54d53292017-04-07 08:52:27 -0600263void elevator_exit(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Al Viro3d1ab402006-03-18 18:35:43 -0500265 mutex_lock(&e->sysfs_lock);
Jens Axboebd166ef2017-01-17 06:03:22 -0700266 if (e->uses_mq && e->type->ops.mq.exit_sched)
Omar Sandoval54d53292017-04-07 08:52:27 -0600267 blk_mq_exit_sched(q, e);
Jens Axboebd166ef2017-01-17 06:03:22 -0700268 else if (!e->uses_mq && e->type->ops.sq.elevator_exit_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -0700269 e->type->ops.sq.elevator_exit_fn(e);
Al Viro3d1ab402006-03-18 18:35:43 -0500270 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Al Viro3d1ab402006-03-18 18:35:43 -0500272 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Jens Axboe2e662b62006-07-13 11:55:04 +0200274EXPORT_SYMBOL(elevator_exit);
275
Jens Axboe98170642006-07-28 09:23:08 +0200276static inline void __elv_rqhash_del(struct request *rq)
277{
Sasha Levin242d98f2012-12-17 10:01:27 -0500278 hash_del(&rq->hash);
Christoph Hellwige8064022016-10-20 15:12:13 +0200279 rq->rq_flags &= ~RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200280}
281
Jens Axboe70b3ea02016-12-07 08:43:31 -0700282void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200283{
284 if (ELV_ON_HASH(rq))
285 __elv_rqhash_del(rq);
286}
Jens Axboebd166ef2017-01-17 06:03:22 -0700287EXPORT_SYMBOL_GPL(elv_rqhash_del);
Jens Axboe98170642006-07-28 09:23:08 +0200288
Jens Axboe70b3ea02016-12-07 08:43:31 -0700289void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200290{
Jens Axboeb374d182008-10-31 10:05:07 +0100291 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200292
293 BUG_ON(ELV_ON_HASH(rq));
Sasha Levin242d98f2012-12-17 10:01:27 -0500294 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
Christoph Hellwige8064022016-10-20 15:12:13 +0200295 rq->rq_flags |= RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200296}
Jens Axboebd166ef2017-01-17 06:03:22 -0700297EXPORT_SYMBOL_GPL(elv_rqhash_add);
Jens Axboe98170642006-07-28 09:23:08 +0200298
Jens Axboe70b3ea02016-12-07 08:43:31 -0700299void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200300{
301 __elv_rqhash_del(rq);
302 elv_rqhash_add(q, rq);
303}
304
Jens Axboe70b3ea02016-12-07 08:43:31 -0700305struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200306{
Jens Axboeb374d182008-10-31 10:05:07 +0100307 struct elevator_queue *e = q->elevator;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800308 struct hlist_node *next;
Jens Axboe98170642006-07-28 09:23:08 +0200309 struct request *rq;
310
Linus Torvaldsee89f812013-02-28 12:52:24 -0800311 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
Jens Axboe98170642006-07-28 09:23:08 +0200312 BUG_ON(!ELV_ON_HASH(rq));
313
314 if (unlikely(!rq_mergeable(rq))) {
315 __elv_rqhash_del(rq);
316 continue;
317 }
318
319 if (rq_hash_key(rq) == offset)
320 return rq;
321 }
322
323 return NULL;
324}
325
Tejun Heo8922e162005-10-20 16:23:44 +0200326/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200327 * RB-tree support functions for inserting/lookup/removal of requests
328 * in a sorted RB tree.
329 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200330void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200331{
332 struct rb_node **p = &root->rb_node;
333 struct rb_node *parent = NULL;
334 struct request *__rq;
335
336 while (*p) {
337 parent = *p;
338 __rq = rb_entry(parent, struct request, rb_node);
339
Tejun Heo83096eb2009-05-07 22:24:39 +0900340 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200341 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200342 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200343 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200344 }
345
346 rb_link_node(&rq->rb_node, parent, p);
347 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200348}
Jens Axboe2e662b62006-07-13 11:55:04 +0200349EXPORT_SYMBOL(elv_rb_add);
350
351void elv_rb_del(struct rb_root *root, struct request *rq)
352{
353 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
354 rb_erase(&rq->rb_node, root);
355 RB_CLEAR_NODE(&rq->rb_node);
356}
Jens Axboe2e662b62006-07-13 11:55:04 +0200357EXPORT_SYMBOL(elv_rb_del);
358
359struct request *elv_rb_find(struct rb_root *root, sector_t sector)
360{
361 struct rb_node *n = root->rb_node;
362 struct request *rq;
363
364 while (n) {
365 rq = rb_entry(n, struct request, rb_node);
366
Tejun Heo83096eb2009-05-07 22:24:39 +0900367 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200368 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900369 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200370 n = n->rb_right;
371 else
372 return rq;
373 }
374
375 return NULL;
376}
Jens Axboe2e662b62006-07-13 11:55:04 +0200377EXPORT_SYMBOL(elv_rb_find);
378
379/*
Tejun Heo8922e162005-10-20 16:23:44 +0200380 * Insert rq into dispatch queue of q. Queue lock must be held on
Uwe Kleine-Königdbe7f762007-10-20 01:55:04 +0200381 * entry. rq is sort instead into the dispatch queue. To be used by
Jens Axboe2e662b62006-07-13 11:55:04 +0200382 * specific elevators.
Tejun Heo8922e162005-10-20 16:23:44 +0200383 */
Jens Axboe165125e2007-07-24 09:28:11 +0200384void elv_dispatch_sort(struct request_queue *q, struct request *rq)
Tejun Heo8922e162005-10-20 16:23:44 +0200385{
386 sector_t boundary;
Tejun Heo8922e162005-10-20 16:23:44 +0200387 struct list_head *entry;
388
Tejun Heo06b86242005-10-20 16:46:23 +0200389 if (q->last_merge == rq)
390 q->last_merge = NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200391
392 elv_rqhash_del(q, rq);
393
Tejun Heo15853af2005-11-10 08:52:05 +0100394 q->nr_sorted--;
Tejun Heo06b86242005-10-20 16:46:23 +0200395
Jens Axboe1b47f532005-10-20 16:37:00 +0200396 boundary = q->end_sector;
Tejun Heo8922e162005-10-20 16:23:44 +0200397 list_for_each_prev(entry, &q->queue_head) {
398 struct request *pos = list_entry_rq(entry);
399
Adrian Hunter7afafc82016-08-16 10:59:35 +0300400 if (req_op(rq) != req_op(pos))
David Woodhousee17fc0a2008-08-09 16:42:20 +0100401 break;
Jens Axboe783660b2007-01-19 11:27:47 +1100402 if (rq_data_dir(rq) != rq_data_dir(pos))
403 break;
Christoph Hellwige8064022016-10-20 15:12:13 +0200404 if (pos->rq_flags & (RQF_STARTED | RQF_SOFTBARRIER))
Tejun Heo8922e162005-10-20 16:23:44 +0200405 break;
Tejun Heo83096eb2009-05-07 22:24:39 +0900406 if (blk_rq_pos(rq) >= boundary) {
407 if (blk_rq_pos(pos) < boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200408 continue;
409 } else {
Tejun Heo83096eb2009-05-07 22:24:39 +0900410 if (blk_rq_pos(pos) >= boundary)
Tejun Heo8922e162005-10-20 16:23:44 +0200411 break;
412 }
Tejun Heo83096eb2009-05-07 22:24:39 +0900413 if (blk_rq_pos(rq) >= blk_rq_pos(pos))
Tejun Heo8922e162005-10-20 16:23:44 +0200414 break;
415 }
416
417 list_add(&rq->queuelist, entry);
418}
Jens Axboe2e662b62006-07-13 11:55:04 +0200419EXPORT_SYMBOL(elv_dispatch_sort);
420
Jens Axboe98170642006-07-28 09:23:08 +0200421/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200422 * Insert rq into dispatch queue of q. Queue lock must be held on
423 * entry. rq is added to the back of the dispatch queue. To be used by
424 * specific elevators.
Jens Axboe98170642006-07-28 09:23:08 +0200425 */
426void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
427{
428 if (q->last_merge == rq)
429 q->last_merge = NULL;
430
431 elv_rqhash_del(q, rq);
432
433 q->nr_sorted--;
434
435 q->end_sector = rq_end_sector(rq);
436 q->boundary_rq = rq;
437 list_add_tail(&rq->queuelist, &q->queue_head);
438}
Jens Axboe2e662b62006-07-13 11:55:04 +0200439EXPORT_SYMBOL(elv_dispatch_add_tail);
440
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100441enum elv_merge elv_merge(struct request_queue *q, struct request **req,
442 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jens Axboeb374d182008-10-31 10:05:07 +0100444 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200445 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200446
Jens Axboe98170642006-07-28 09:23:08 +0200447 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100448 * Levels of merges:
449 * nomerges: No merges at all attempted
450 * noxmerges: Only simple one-hit cache try
451 * merges: All merge tries attempted
452 */
Ming Lei7460d382015-10-20 23:13:55 +0800453 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100454 return ELEVATOR_NO_MERGE;
455
456 /*
Jens Axboe98170642006-07-28 09:23:08 +0200457 * First try one-hit cache.
458 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700459 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100460 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
461
Tejun Heo06b86242005-10-20 16:46:23 +0200462 if (ret != ELEVATOR_NO_MERGE) {
463 *req = q->last_merge;
464 return ret;
465 }
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100468 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200469 return ELEVATOR_NO_MERGE;
470
Jens Axboe98170642006-07-28 09:23:08 +0200471 /*
472 * See if our hash lookup can find a potential backmerge.
473 */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700474 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700475 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200476 *req = __rq;
477 return ELEVATOR_BACK_MERGE;
478 }
479
Jens Axboebd166ef2017-01-17 06:03:22 -0700480 if (e->uses_mq && e->type->ops.mq.request_merge)
481 return e->type->ops.mq.request_merge(q, req, bio);
482 else if (!e->uses_mq && e->type->ops.sq.elevator_merge_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -0700483 return e->type->ops.sq.elevator_merge_fn(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 return ELEVATOR_NO_MERGE;
486}
487
Jens Axboe5e84ea32011-03-21 10:14:27 +0100488/*
489 * Attempt to do an insertion back merge. Only check for the case where
490 * we can append 'rq' to an existing request, so we can throw 'rq' away
491 * afterwards.
492 *
493 * Returns true if we merged, false otherwise
494 */
Jens Axboebd166ef2017-01-17 06:03:22 -0700495bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
Jens Axboe5e84ea32011-03-21 10:14:27 +0100496{
497 struct request *__rq;
Shaohua Libee03932012-11-09 08:44:27 +0100498 bool ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100499
500 if (blk_queue_nomerges(q))
501 return false;
502
503 /*
504 * First try one-hit cache.
505 */
506 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
507 return true;
508
509 if (blk_queue_noxmerges(q))
510 return false;
511
Shaohua Libee03932012-11-09 08:44:27 +0100512 ret = false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100513 /*
514 * See if our hash lookup can find a potential backmerge.
515 */
Shaohua Libee03932012-11-09 08:44:27 +0100516 while (1) {
517 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
518 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
519 break;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100520
Shaohua Libee03932012-11-09 08:44:27 +0100521 /* The merged request could be merged with others, try again */
522 ret = true;
523 rq = __rq;
524 }
525
526 return ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100527}
528
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100529void elv_merged_request(struct request_queue *q, struct request *rq,
530 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jens Axboeb374d182008-10-31 10:05:07 +0100532 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jens Axboebd166ef2017-01-17 06:03:22 -0700534 if (e->uses_mq && e->type->ops.mq.request_merged)
535 e->type->ops.mq.request_merged(q, rq, type);
536 else if (!e->uses_mq && e->type->ops.sq.elevator_merged_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -0700537 e->type->ops.sq.elevator_merged_fn(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200538
Jens Axboe2e662b62006-07-13 11:55:04 +0200539 if (type == ELEVATOR_BACK_MERGE)
540 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200541
Tejun Heo06b86242005-10-20 16:46:23 +0200542 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Jens Axboe165125e2007-07-24 09:28:11 +0200545void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct request *next)
547{
Jens Axboeb374d182008-10-31 10:05:07 +0100548 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -0700549 bool next_sorted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jens Axboebd166ef2017-01-17 06:03:22 -0700551 if (e->uses_mq && e->type->ops.mq.requests_merged)
552 e->type->ops.mq.requests_merged(q, rq, next);
553 else if (e->type->ops.sq.elevator_merge_req_fn) {
Bart Van Asschea1ae0f72017-02-01 12:22:23 -0700554 next_sorted = (__force bool)(next->rq_flags & RQF_SORTED);
Jens Axboebd166ef2017-01-17 06:03:22 -0700555 if (next_sorted)
556 e->type->ops.sq.elevator_merge_req_fn(q, rq, next);
557 }
Tejun Heo06b86242005-10-20 16:46:23 +0200558
Jens Axboe98170642006-07-28 09:23:08 +0200559 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200560
Jens Axboe5e84ea32011-03-21 10:14:27 +0100561 if (next_sorted) {
562 elv_rqhash_del(q, next);
563 q->nr_sorted--;
564 }
565
Tejun Heo06b86242005-10-20 16:46:23 +0200566 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Divyesh Shah812d4022010-04-08 21:14:23 -0700569void elv_bio_merged(struct request_queue *q, struct request *rq,
570 struct bio *bio)
571{
572 struct elevator_queue *e = q->elevator;
573
Jens Axboebd166ef2017-01-17 06:03:22 -0700574 if (WARN_ON_ONCE(e->uses_mq))
575 return;
576
Jens Axboec51ca6c2016-12-10 15:13:59 -0700577 if (e->type->ops.sq.elevator_bio_merged_fn)
578 e->type->ops.sq.elevator_bio_merged_fn(q, rq, bio);
Divyesh Shah812d4022010-04-08 21:14:23 -0700579}
580
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +0100581#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +0800582static void blk_pm_requeue_request(struct request *rq)
583{
Christoph Hellwige8064022016-10-20 15:12:13 +0200584 if (rq->q->dev && !(rq->rq_flags & RQF_PM))
Lin Mingc8158812013-03-23 11:42:27 +0800585 rq->q->nr_pending--;
586}
587
588static void blk_pm_add_request(struct request_queue *q, struct request *rq)
589{
Christoph Hellwige8064022016-10-20 15:12:13 +0200590 if (q->dev && !(rq->rq_flags & RQF_PM) && q->nr_pending++ == 0 &&
Lin Mingc8158812013-03-23 11:42:27 +0800591 (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
592 pm_request_resume(q->dev);
593}
594#else
595static inline void blk_pm_requeue_request(struct request *rq) {}
596static inline void blk_pm_add_request(struct request_queue *q,
597 struct request *rq)
598{
599}
600#endif
601
Jens Axboe165125e2007-07-24 09:28:11 +0200602void elv_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * it already went through dequeue, we need to decrement the
606 * in_flight count again
607 */
Tejun Heo8922e162005-10-20 16:23:44 +0200608 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200609 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwige8064022016-10-20 15:12:13 +0200610 if (rq->rq_flags & RQF_SORTED)
Jens Axboecad97512007-01-14 22:26:09 +1100611 elv_deactivate_rq(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Christoph Hellwige8064022016-10-20 15:12:13 +0200614 rq->rq_flags &= ~RQF_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Lin Mingc8158812013-03-23 11:42:27 +0800616 blk_pm_requeue_request(rq);
617
Jens Axboeb710a482011-03-30 09:52:30 +0200618 __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Jerome Marchand26308ea2009-03-27 10:31:51 +0100621void elv_drain_elevator(struct request_queue *q)
Tejun Heo15853af2005-11-10 08:52:05 +0100622{
Jens Axboebd166ef2017-01-17 06:03:22 -0700623 struct elevator_queue *e = q->elevator;
Tejun Heo15853af2005-11-10 08:52:05 +0100624 static int printed;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200625
Jens Axboebd166ef2017-01-17 06:03:22 -0700626 if (WARN_ON_ONCE(e->uses_mq))
627 return;
628
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200629 lockdep_assert_held(q->queue_lock);
630
Jens Axboebd166ef2017-01-17 06:03:22 -0700631 while (e->type->ops.sq.elevator_dispatch_fn(q, 1))
Tejun Heo15853af2005-11-10 08:52:05 +0100632 ;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200633 if (q->nr_sorted && printed++ < 10) {
Tejun Heo15853af2005-11-10 08:52:05 +0100634 printk(KERN_ERR "%s: forced dispatching is broken "
635 "(nr_sorted=%u), please report this\n",
Tejun Heo22f746e2011-12-14 00:33:41 +0100636 q->elevator->type->elevator_name, q->nr_sorted);
Tejun Heo15853af2005-11-10 08:52:05 +0100637 }
638}
639
Jens Axboeb710a482011-03-30 09:52:30 +0200640void __elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100642 trace_block_rq_insert(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100643
Lin Mingc8158812013-03-23 11:42:27 +0800644 blk_pm_add_request(q, rq);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 rq->q = q;
647
Christoph Hellwige8064022016-10-20 15:12:13 +0200648 if (rq->rq_flags & RQF_SOFTBARRIER) {
Jens Axboeb710a482011-03-30 09:52:30 +0200649 /* barriers are scheduling boundary, update end_sector */
Christoph Hellwig57292b52017-01-31 16:57:29 +0100650 if (!blk_rq_is_passthrough(rq)) {
Jens Axboeb710a482011-03-30 09:52:30 +0200651 q->end_sector = rq_end_sector(rq);
652 q->boundary_rq = rq;
653 }
Christoph Hellwige8064022016-10-20 15:12:13 +0200654 } else if (!(rq->rq_flags & RQF_ELVPRIV) &&
Jens Axboe3aa72872011-04-21 19:28:35 +0200655 (where == ELEVATOR_INSERT_SORT ||
656 where == ELEVATOR_INSERT_SORT_MERGE))
Jens Axboeb710a482011-03-30 09:52:30 +0200657 where = ELEVATOR_INSERT_BACK;
658
Tejun Heo8922e162005-10-20 16:23:44 +0200659 switch (where) {
Tejun Heo28e7d182010-09-03 11:56:16 +0200660 case ELEVATOR_INSERT_REQUEUE:
Tejun Heo8922e162005-10-20 16:23:44 +0200661 case ELEVATOR_INSERT_FRONT:
Christoph Hellwige8064022016-10-20 15:12:13 +0200662 rq->rq_flags |= RQF_SOFTBARRIER;
Tejun Heo8922e162005-10-20 16:23:44 +0200663 list_add(&rq->queuelist, &q->queue_head);
664 break;
665
666 case ELEVATOR_INSERT_BACK:
Christoph Hellwige8064022016-10-20 15:12:13 +0200667 rq->rq_flags |= RQF_SOFTBARRIER;
Tejun Heo15853af2005-11-10 08:52:05 +0100668 elv_drain_elevator(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200669 list_add_tail(&rq->queuelist, &q->queue_head);
670 /*
671 * We kick the queue here for the following reasons.
672 * - The elevator might have returned NULL previously
673 * to delay requests and returned them now. As the
674 * queue wasn't empty before this request, ll_rw_blk
675 * won't run the queue on return, resulting in hang.
676 * - Usually, back inserted requests won't be merged
677 * with anything. There's no point in delaying queue
678 * processing.
679 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200680 __blk_run_queue(q);
Tejun Heo8922e162005-10-20 16:23:44 +0200681 break;
682
Jens Axboe5e84ea32011-03-21 10:14:27 +0100683 case ELEVATOR_INSERT_SORT_MERGE:
684 /*
685 * If we succeed in merging this request with one in the
686 * queue already, we are done - rq has now been freed,
687 * so no need to do anything further.
688 */
689 if (elv_attempt_insert_merge(q, rq))
690 break;
Bart Van Asschee29387e2017-06-21 09:40:11 -0700691 /* fall through */
Tejun Heo8922e162005-10-20 16:23:44 +0200692 case ELEVATOR_INSERT_SORT:
Christoph Hellwig57292b52017-01-31 16:57:29 +0100693 BUG_ON(blk_rq_is_passthrough(rq));
Christoph Hellwige8064022016-10-20 15:12:13 +0200694 rq->rq_flags |= RQF_SORTED;
Tejun Heo15853af2005-11-10 08:52:05 +0100695 q->nr_sorted++;
Jens Axboe98170642006-07-28 09:23:08 +0200696 if (rq_mergeable(rq)) {
697 elv_rqhash_add(q, rq);
698 if (!q->last_merge)
699 q->last_merge = rq;
700 }
701
Tejun Heoca235092005-11-01 17:23:49 +0900702 /*
703 * Some ioscheds (cfq) run q->request_fn directly, so
704 * rq cannot be accessed after calling
705 * elevator_add_req_fn.
706 */
Jens Axboec51ca6c2016-12-10 15:13:59 -0700707 q->elevator->type->ops.sq.elevator_add_req_fn(q, rq);
Tejun Heo8922e162005-10-20 16:23:44 +0200708 break;
709
Tejun Heoae1b1532011-01-25 12:43:54 +0100710 case ELEVATOR_INSERT_FLUSH:
Christoph Hellwige8064022016-10-20 15:12:13 +0200711 rq->rq_flags |= RQF_SOFTBARRIER;
Tejun Heoae1b1532011-01-25 12:43:54 +0100712 blk_insert_flush(rq);
713 break;
Tejun Heo8922e162005-10-20 16:23:44 +0200714 default:
715 printk(KERN_ERR "%s: bad insertion point %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700716 __func__, where);
Tejun Heo8922e162005-10-20 16:23:44 +0200717 BUG();
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Jens Axboe2e662b62006-07-13 11:55:04 +0200720EXPORT_SYMBOL(__elv_add_request);
721
Jens Axboe7eaceac2011-03-10 08:52:07 +0100722void elv_add_request(struct request_queue *q, struct request *rq, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 unsigned long flags;
725
726 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100727 __elv_add_request(q, rq, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spin_unlock_irqrestore(q->queue_lock, flags);
729}
Jens Axboe2e662b62006-07-13 11:55:04 +0200730EXPORT_SYMBOL(elv_add_request);
731
Jens Axboe165125e2007-07-24 09:28:11 +0200732struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Jens Axboeb374d182008-10-31 10:05:07 +0100734 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jens Axboebd166ef2017-01-17 06:03:22 -0700736 if (e->uses_mq && e->type->ops.mq.next_request)
737 return e->type->ops.mq.next_request(q, rq);
738 else if (!e->uses_mq && e->type->ops.sq.elevator_latter_req_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -0700739 return e->type->ops.sq.elevator_latter_req_fn(q, rq);
Jens Axboebd166ef2017-01-17 06:03:22 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return NULL;
742}
743
Jens Axboe165125e2007-07-24 09:28:11 +0200744struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Jens Axboeb374d182008-10-31 10:05:07 +0100746 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Jens Axboebd166ef2017-01-17 06:03:22 -0700748 if (e->uses_mq && e->type->ops.mq.former_request)
749 return e->type->ops.mq.former_request(q, rq);
750 if (!e->uses_mq && e->type->ops.sq.elevator_former_req_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -0700751 return e->type->ops.sq.elevator_former_req_fn(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return NULL;
753}
754
Tejun Heo852c7882012-03-05 13:15:27 -0800755int elv_set_request(struct request_queue *q, struct request *rq,
756 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jens Axboeb374d182008-10-31 10:05:07 +0100758 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Jens Axboebd166ef2017-01-17 06:03:22 -0700760 if (WARN_ON_ONCE(e->uses_mq))
761 return 0;
762
Jens Axboec51ca6c2016-12-10 15:13:59 -0700763 if (e->type->ops.sq.elevator_set_req_fn)
764 return e->type->ops.sq.elevator_set_req_fn(q, rq, bio, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766}
767
Jens Axboe165125e2007-07-24 09:28:11 +0200768void elv_put_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Jens Axboeb374d182008-10-31 10:05:07 +0100770 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Jens Axboebd166ef2017-01-17 06:03:22 -0700772 if (WARN_ON_ONCE(e->uses_mq))
773 return;
774
Jens Axboec51ca6c2016-12-10 15:13:59 -0700775 if (e->type->ops.sq.elevator_put_req_fn)
776 e->type->ops.sq.elevator_put_req_fn(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600779int elv_may_queue(struct request_queue *q, unsigned int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Jens Axboeb374d182008-10-31 10:05:07 +0100781 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jens Axboebd166ef2017-01-17 06:03:22 -0700783 if (WARN_ON_ONCE(e->uses_mq))
784 return 0;
785
Jens Axboec51ca6c2016-12-10 15:13:59 -0700786 if (e->type->ops.sq.elevator_may_queue_fn)
787 return e->type->ops.sq.elevator_may_queue_fn(q, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 return ELV_MQUEUE_MAY;
790}
791
Jens Axboe165125e2007-07-24 09:28:11 +0200792void elv_completed_request(struct request_queue *q, struct request *rq)
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
Jens Axboebd166ef2017-01-17 06:03:22 -0700796 if (WARN_ON_ONCE(e->uses_mq))
797 return;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /*
800 * request is released from the driver, io must be done
801 */
Tejun Heo8922e162005-10-20 16:23:44 +0200802 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200803 q->in_flight[rq_is_sync(rq)]--;
Christoph Hellwige8064022016-10-20 15:12:13 +0200804 if ((rq->rq_flags & RQF_SORTED) &&
Jens Axboec51ca6c2016-12-10 15:13:59 -0700805 e->type->ops.sq.elevator_completed_req_fn)
806 e->type->ops.sq.elevator_completed_req_fn(q, rq);
Tejun Heo1bc691d2006-01-12 15:39:26 +0100807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Al Viro3d1ab402006-03-18 18:35:43 -0500810#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
811
812static ssize_t
813elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
814{
Al Viro3d1ab402006-03-18 18:35:43 -0500815 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100816 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500817 ssize_t error;
818
819 if (!entry->show)
820 return -EIO;
821
Jens Axboeb374d182008-10-31 10:05:07 +0100822 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500823 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100824 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500825 mutex_unlock(&e->sysfs_lock);
826 return error;
827}
828
829static ssize_t
830elv_attr_store(struct kobject *kobj, struct attribute *attr,
831 const char *page, size_t length)
832{
Al Viro3d1ab402006-03-18 18:35:43 -0500833 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100834 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500835 ssize_t error;
836
837 if (!entry->store)
838 return -EIO;
839
Jens Axboeb374d182008-10-31 10:05:07 +0100840 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500841 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100842 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500843 mutex_unlock(&e->sysfs_lock);
844 return error;
845}
846
Emese Revfy52cf25d2010-01-19 02:58:23 +0100847static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500848 .show = elv_attr_show,
849 .store = elv_attr_store,
850};
851
852static struct kobj_type elv_ktype = {
853 .sysfs_ops = &elv_sysfs_ops,
854 .release = elevator_release,
855};
856
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800857int elv_register_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800859 struct elevator_queue *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500860 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700862 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500863 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100864 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500865 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500866 while (attr->attr.name) {
867 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500868 break;
Al Viroe572ec72006-03-18 22:27:18 -0500869 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500870 }
871 }
872 kobject_uevent(&e->kobj, KOBJ_ADD);
Jens Axboe430c62f2010-10-07 09:35:16 +0200873 e->registered = 1;
Jens Axboebd166ef2017-01-17 06:03:22 -0700874 if (!e->uses_mq && e->type->ops.sq.elevator_registered_fn)
Jens Axboec51ca6c2016-12-10 15:13:59 -0700875 e->type->ops.sq.elevator_registered_fn(q);
Al Viro3d1ab402006-03-18 18:35:43 -0500876 }
877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
Tejun Heof8fc8772011-12-14 00:33:40 +0100879EXPORT_SYMBOL(elv_register_queue);
Jens Axboebc1c1162006-06-08 08:49:06 +0200880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881void elv_unregister_queue(struct request_queue *q)
882{
Tejun Heof8fc8772011-12-14 00:33:40 +0100883 if (q) {
884 struct elevator_queue *e = q->elevator;
885
886 kobject_uevent(&e->kobj, KOBJ_REMOVE);
887 kobject_del(&e->kobj);
888 e->registered = 0;
Jan Kara8330cdb2017-04-19 11:33:27 +0200889 /* Re-enable throttling in case elevator disabled it */
890 wbt_enable_default(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
Mike Snitzer01effb02010-05-11 08:57:42 +0200893EXPORT_SYMBOL(elv_unregister_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jens Axboee567bf72014-06-22 16:32:48 -0600895int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100897 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200898
Tejun Heo3d3c2372011-12-14 00:33:42 +0100899 /* create icq_cache if requested */
900 if (e->icq_size) {
901 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
902 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
903 return -EINVAL;
904
905 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
906 "%s_io_cq", e->elevator_name);
907 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
908 e->icq_align, 0, NULL);
909 if (!e->icq_cache)
910 return -ENOMEM;
911 }
912
913 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200914 spin_lock(&elv_list_lock);
Jens Axboe2527d992017-10-25 12:33:42 -0600915 if (elevator_find(e->elevator_name, e->uses_mq)) {
Tejun Heo3d3c2372011-12-14 00:33:42 +0100916 spin_unlock(&elv_list_lock);
917 if (e->icq_cache)
918 kmem_cache_destroy(e->icq_cache);
919 return -EBUSY;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200922 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Tejun Heo3d3c2372011-12-14 00:33:42 +0100924 /* print pretty message */
Nate Diller5f003972006-01-24 10:07:58 +0100925 if (!strcmp(e->elevator_name, chosen_elevator) ||
926 (!*chosen_elevator &&
927 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100928 def = " (default)";
929
Jens Axboe4eb166d2008-02-01 00:37:27 +0100930 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
931 def);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934EXPORT_SYMBOL_GPL(elv_register);
935
936void elv_unregister(struct elevator_type *e)
937{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100938 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200939 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200941 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100942
943 /*
944 * Destroy icq_cache if it exists. icq's are RCU managed. Make
945 * sure all RCU operations are complete before proceeding.
946 */
947 if (e->icq_cache) {
948 rcu_barrier();
949 kmem_cache_destroy(e->icq_cache);
950 e->icq_cache = NULL;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953EXPORT_SYMBOL_GPL(elv_unregister);
954
Omar Sandoval54d53292017-04-07 08:52:27 -0600955static int elevator_switch_mq(struct request_queue *q,
956 struct elevator_type *new_e)
957{
958 int ret;
959
960 blk_mq_freeze_queue(q);
Omar Sandoval54d53292017-04-07 08:52:27 -0600961
962 if (q->elevator) {
963 if (q->elevator->registered)
964 elv_unregister_queue(q);
965 ioc_clear_queue(q);
966 elevator_exit(q, q->elevator);
967 }
968
969 ret = blk_mq_init_sched(q, new_e);
970 if (ret)
971 goto out;
972
973 if (new_e) {
974 ret = elv_register_queue(q);
975 if (ret) {
976 elevator_exit(q, q->elevator);
977 goto out;
978 }
979 }
980
981 if (new_e)
982 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
983 else
984 blk_add_trace_msg(q, "elv switch: none");
985
986out:
987 blk_mq_unfreeze_queue(q);
Omar Sandoval54d53292017-04-07 08:52:27 -0600988 return ret;
Omar Sandoval54d53292017-04-07 08:52:27 -0600989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
992 * switch to new_e io scheduler. be careful not to introduce deadlocks -
993 * we don't free the old io scheduler, before we have allocated what we
994 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200995 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Jens Axboe165125e2007-07-24 09:28:11 +0200997static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800999 struct elevator_queue *old = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -07001000 bool old_registered = false;
Tejun Heoe8989fa2012-03-05 13:15:20 -08001001 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Omar Sandoval54d53292017-04-07 08:52:27 -06001003 if (q->mq_ops)
1004 return elevator_switch_mq(q, new_e);
Jens Axboebd166ef2017-01-17 06:03:22 -07001005
Tejun Heo5a5bafd2012-03-05 13:14:56 -08001006 /*
1007 * Turn on BYPASS and drain all requests w/ elevator private data.
1008 * Block layer doesn't call into a quiesced elevator - all requests
1009 * are directly put on the dispatch list without elevator data
1010 * using INSERT_BACK. All requests have SOFTBARRIER set and no
1011 * merge happens either.
1012 */
Jens Axboebd166ef2017-01-17 06:03:22 -07001013 if (old) {
1014 old_registered = old->registered;
Tejun Heocb98fc82005-10-28 08:29:39 +02001015
Omar Sandoval54d53292017-04-07 08:52:27 -06001016 blk_queue_bypass_start(q);
Jens Axboebd166ef2017-01-17 06:03:22 -07001017
1018 /* unregister and clear all auxiliary data of the old elevator */
1019 if (old_registered)
1020 elv_unregister_queue(q);
1021
Jens Axboebd166ef2017-01-17 06:03:22 -07001022 ioc_clear_queue(q);
Jens Axboebd166ef2017-01-17 06:03:22 -07001023 }
Tejun Heo5a5bafd2012-03-05 13:14:56 -08001024
1025 /* allocate, init and register new elevator */
Omar Sandoval54d53292017-04-07 08:52:27 -06001026 err = new_e->ops.sq.elevator_init_fn(q, new_e);
Omar Sandoval6917ff02017-04-05 12:01:30 -07001027 if (err)
1028 goto fail_init;
Tejun Heo5a5bafd2012-03-05 13:14:56 -08001029
Omar Sandoval54d53292017-04-07 08:52:27 -06001030 err = elv_register_queue(q);
1031 if (err)
1032 goto fail_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Tejun Heo5a5bafd2012-03-05 13:14:56 -08001034 /* done, kill the old one and finish */
Jens Axboebd166ef2017-01-17 06:03:22 -07001035 if (old) {
Omar Sandoval54d53292017-04-07 08:52:27 -06001036 elevator_exit(q, old);
1037 blk_queue_bypass_end(q);
Jens Axboebd166ef2017-01-17 06:03:22 -07001038 }
Nick Piggin75ad23b2008-04-29 14:48:33 +02001039
Omar Sandoval54d53292017-04-07 08:52:27 -06001040 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
Alan D. Brunelle4722dc52008-05-27 14:55:00 +02001041
Jens Axboe5dd531a2010-08-23 13:52:19 +02001042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044fail_register:
Omar Sandoval54d53292017-04-07 08:52:27 -06001045 elevator_exit(q, q->elevator);
Tejun Heo5a5bafd2012-03-05 13:14:56 -08001046fail_init:
1047 /* switch failed, restore and re-register old elevator */
Jens Axboebd166ef2017-01-17 06:03:22 -07001048 if (old) {
1049 q->elevator = old;
1050 elv_register_queue(q);
Omar Sandoval54d53292017-04-07 08:52:27 -06001051 blk_queue_bypass_end(q);
Jens Axboebd166ef2017-01-17 06:03:22 -07001052 }
Nick Piggin75ad23b2008-04-29 14:48:33 +02001053
Jens Axboe5dd531a2010-08-23 13:52:19 +02001054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Jens Axboe5dd531a2010-08-23 13:52:19 +02001057/*
1058 * Switch this queue to the given IO scheduler.
1059 */
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -06001060static int __elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 char elevator_name[ELV_NAME_MAX];
1063 struct elevator_type *e;
1064
David Jefferye9a823f2017-08-28 10:52:44 -06001065 /* Make sure queue is not in the middle of being removed */
1066 if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
1067 return -ENOENT;
1068
Jens Axboebd166ef2017-01-17 06:03:22 -07001069 /*
1070 * Special case for mq, turn off scheduling
1071 */
1072 if (q->mq_ops && !strncmp(name, "none", 4))
1073 return elevator_switch(q, NULL);
Martin K. Petersencd43e262009-05-22 17:17:52 -04001074
Li Zefanee2e9922008-10-14 08:49:56 +02001075 strlcpy(elevator_name, name, sizeof(elevator_name));
Jens Axboe2527d992017-10-25 12:33:42 -06001076 e = elevator_get(q, strstrip(elevator_name), true);
Jens Axboe340ff322017-05-10 07:40:04 -06001077 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Jens Axboebd166ef2017-01-17 06:03:22 -07001080 if (q->elevator &&
1081 !strcmp(elevator_name, q->elevator->type->elevator_name)) {
Nate Diller2ca7d932005-10-30 15:02:24 -08001082 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +02001083 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -08001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Jens Axboe5dd531a2010-08-23 13:52:19 +02001086 return elevator_switch(q, e);
1087}
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -06001088
Ming Lei3a5088c2017-04-15 20:38:22 +08001089static inline bool elv_support_iosched(struct request_queue *q)
1090{
1091 if (q->mq_ops && q->tag_set && (q->tag_set->flags &
1092 BLK_MQ_F_NO_SCHED))
1093 return false;
1094 return true;
1095}
1096
Jens Axboe5dd531a2010-08-23 13:52:19 +02001097ssize_t elv_iosched_store(struct request_queue *q, const char *name,
1098 size_t count)
1099{
1100 int ret;
1101
Ming Lei3a5088c2017-04-15 20:38:22 +08001102 if (!(q->mq_ops || q->request_fn) || !elv_support_iosched(q))
Jens Axboe5dd531a2010-08-23 13:52:19 +02001103 return count;
1104
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -06001105 ret = __elevator_change(q, name);
Jens Axboe5dd531a2010-08-23 13:52:19 +02001106 if (!ret)
1107 return count;
1108
Jens Axboe5dd531a2010-08-23 13:52:19 +02001109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Jens Axboe165125e2007-07-24 09:28:11 +02001112ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Jens Axboeb374d182008-10-31 10:05:07 +01001114 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -07001115 struct elevator_type *elv = NULL;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001116 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int len = 0;
1118
Christoph Hellwig5fdee212017-10-05 21:22:52 +02001119 if (!queue_is_rq_based(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -04001120 return sprintf(name, "none\n");
1121
Jens Axboebd166ef2017-01-17 06:03:22 -07001122 if (!q->elevator)
1123 len += sprintf(name+len, "[none] ");
1124 else
1125 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -04001126
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001127 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +02001128 list_for_each_entry(__e, &elv_list, list) {
Jens Axboebd166ef2017-01-17 06:03:22 -07001129 if (elv && !strcmp(elv->elevator_name, __e->elevator_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 len += sprintf(name+len, "[%s] ", elv->elevator_name);
Jens Axboebd166ef2017-01-17 06:03:22 -07001131 continue;
1132 }
Ming Lei3a5088c2017-04-15 20:38:22 +08001133 if (__e->uses_mq && q->mq_ops && elv_support_iosched(q))
Jens Axboebd166ef2017-01-17 06:03:22 -07001134 len += sprintf(name+len, "%s ", __e->elevator_name);
1135 else if (!__e->uses_mq && !q->mq_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 len += sprintf(name+len, "%s ", __e->elevator_name);
1137 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +02001138 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jens Axboebd166ef2017-01-17 06:03:22 -07001140 if (q->mq_ops && q->elevator)
1141 len += sprintf(name+len, "none");
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 len += sprintf(len+name, "\n");
1144 return len;
1145}
1146
Jens Axboe165125e2007-07-24 09:28:11 +02001147struct request *elv_rb_former_request(struct request_queue *q,
1148 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001149{
1150 struct rb_node *rbprev = rb_prev(&rq->rb_node);
1151
1152 if (rbprev)
1153 return rb_entry_rq(rbprev);
1154
1155 return NULL;
1156}
Jens Axboe2e662b62006-07-13 11:55:04 +02001157EXPORT_SYMBOL(elv_rb_former_request);
1158
Jens Axboe165125e2007-07-24 09:28:11 +02001159struct request *elv_rb_latter_request(struct request_queue *q,
1160 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +02001161{
1162 struct rb_node *rbnext = rb_next(&rq->rb_node);
1163
1164 if (rbnext)
1165 return rb_entry_rq(rbnext);
1166
1167 return NULL;
1168}
Jens Axboe2e662b62006-07-13 11:55:04 +02001169EXPORT_SYMBOL(elv_rb_latter_request);