Blame view

block/elevator.c 18.6 KB
3dcf60bcb   Christoph Hellwig   block: add SPDX t...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
   *  Block device elevator/IO-scheduler.
   *
   *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
   *
0fe234795   Jens Axboe   [PATCH] Update ax...
7
   * 30042000 Jens Axboe <axboe@kernel.dk> :
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   *
   * Split the elevator a bit so that it is possible to choose a different
   * one or even write a new "plug in". There are three pieces:
   * - elevator_fn, inserts a new request in the queue list
   * - elevator_merge_fn, decides whether a new buffer can be merged with
   *   an existing request
   * - elevator_dequeue_fn, called when a request is taken off the active list
   *
   * 20082000 Dave Jones <davej@suse.de> :
   * Removed tests for max-bomb-segments, which was breaking elvtune
   *  when run without -bN
   *
   * Jens:
   * - Rework again to work with bio instead of buffer_heads
   * - loose bi_dev comparisons, partition handling is right now
   * - completely modularize elevator setup and teardown
   *
   */
  #include <linux/kernel.h>
  #include <linux/fs.h>
  #include <linux/blkdev.h>
  #include <linux/elevator.h>
  #include <linux/bio.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/init.h>
  #include <linux/compiler.h>
2056a782f   Jens Axboe   [PATCH] Block que...
35
  #include <linux/blktrace_api.h>
9817064b6   Jens Axboe   [PATCH] elevator:...
36
  #include <linux/hash.h>
0835da67c   Jens Axboe   block: use linux/...
37
  #include <linux/uaccess.h>
c8158819d   Lin Ming   block: implement ...
38
  #include <linux/pm_runtime.h>
eea8f41cc   Tejun Heo   blkcg: move block...
39
  #include <linux/blk-cgroup.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

55782138e   Li Zefan   tracing/events: c...
41
  #include <trace/events/block.h>
242f9dcb8   Jens Axboe   block: unify requ...
42
  #include "blk.h"
bd166ef18   Jens Axboe   blk-mq-sched: add...
43
  #include "blk-mq-sched.h"
bca6b067b   Bart Van Assche   block: Move power...
44
  #include "blk-pm.h"
8330cdb0f   Jan Kara   block: Make write...
45
  #include "blk-wbt.h"
242f9dcb8   Jens Axboe   block: unify requ...
46

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
50
  static DEFINE_SPINLOCK(elv_list_lock);
  static LIST_HEAD(elv_list);
  
  /*
9817064b6   Jens Axboe   [PATCH] elevator:...
51
52
   * Merge hash stuff.
   */
83096ebf1   Tejun Heo   block: convert to...
53
  #define rq_hash_key(rq)		(blk_rq_pos(rq) + blk_rq_sectors(rq))
9817064b6   Jens Axboe   [PATCH] elevator:...
54
55
  
  /*
da7752650   Jens Axboe   [PATCH] cfq-iosch...
56
57
58
   * Query io scheduler to see if the current process issuing bio may be
   * merged with rq.
   */
72ef799b3   Tahsin Erdogan   block: do not mer...
59
  static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
da7752650   Jens Axboe   [PATCH] cfq-iosch...
60
  {
165125e1e   Jens Axboe   [BLOCK] Get rid o...
61
  	struct request_queue *q = rq->q;
b374d18a4   Jens Axboe   block: get rid of...
62
  	struct elevator_queue *e = q->elevator;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
63

f9cd4bfe9   Jens Axboe   block: get rid of...
64
65
  	if (e->type->ops.allow_merge)
  		return e->type->ops.allow_merge(q, rq, bio);
da7752650   Jens Axboe   [PATCH] cfq-iosch...
66
67
68
69
70
  
  	return 1;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
   * can we safely merge with this request?
   */
72ef799b3   Tahsin Erdogan   block: do not mer...
73
  bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
  {
050c8ea80   Tejun Heo   block: separate o...
75
  	if (!blk_rq_merge_ok(rq, bio))
72ef799b3   Tahsin Erdogan   block: do not mer...
76
  		return false;
7ba1ba12e   Martin K. Petersen   block: Block laye...
77

72ef799b3   Tahsin Erdogan   block: do not mer...
78
79
  	if (!elv_iosched_allow_bio_merge(rq, bio))
  		return false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80

72ef799b3   Tahsin Erdogan   block: do not mer...
81
  	return true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  }
72ef799b3   Tahsin Erdogan   block: do not mer...
83
  EXPORT_SYMBOL(elv_bio_merge_ok);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84

68c43f133   Damien Le Moal   block: Introduce ...
85
86
  static inline bool elv_support_features(unsigned int elv_features,
  					unsigned int required_features)
8ac0d9a81   Jens Axboe   elevator: allow n...
87
  {
68c43f133   Damien Le Moal   block: Introduce ...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  	return (required_features & elv_features) == required_features;
  }
  
  /**
   * elevator_match - Test an elevator name and features
   * @e: Scheduler to test
   * @name: Elevator name to test
   * @required_features: Features that the elevator must provide
   *
   * Return true is the elevator @e name matches @name and if @e provides all the
   * the feratures spcified by @required_features.
   */
  static bool elevator_match(const struct elevator_type *e, const char *name,
  			   unsigned int required_features)
  {
  	if (!elv_support_features(e->elevator_features, required_features))
  		return false;
8ac0d9a81   Jens Axboe   elevator: allow n...
105
106
107
108
109
110
111
  	if (!strcmp(e->elevator_name, name))
  		return true;
  	if (e->elevator_alias && !strcmp(e->elevator_alias, name))
  		return true;
  
  	return false;
  }
68c43f133   Damien Le Moal   block: Introduce ...
112
113
114
115
116
117
118
  /**
   * elevator_find - Find an elevator
   * @name: Name of the elevator to find
   * @required_features: Features that the elevator must provide
   *
   * Return the first registered scheduler with name @name and supporting the
   * features @required_features and NULL otherwise.
2527d9978   Jens Axboe   elevator: lookup ...
119
   */
68c43f133   Damien Le Moal   block: Introduce ...
120
121
  static struct elevator_type *elevator_find(const char *name,
  					   unsigned int required_features)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  {
a22b169df   Vasily Tarasov   [PATCH] block lay...
123
  	struct elevator_type *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124

70cee26e0   Matthias Kaehlcke   Use list_for_each...
125
  	list_for_each_entry(e, &elv_list, list) {
68c43f133   Damien Le Moal   block: Introduce ...
126
  		if (elevator_match(e, name, required_features))
a22b169df   Vasily Tarasov   [PATCH] block lay...
127
  			return e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129

a22b169df   Vasily Tarasov   [PATCH] block lay...
130
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
134
135
136
  }
  
  static void elevator_put(struct elevator_type *e)
  {
  	module_put(e->elevator_owner);
  }
2527d9978   Jens Axboe   elevator: lookup ...
137
138
  static struct elevator_type *elevator_get(struct request_queue *q,
  					  const char *name, bool try_loading)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  {
2824bc932   Tejun Heo   [PATCH] fix try_m...
140
  	struct elevator_type *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141

2a12dcd71   Jens Axboe   [PATCH] elevator:...
142
  	spin_lock(&elv_list_lock);
2824bc932   Tejun Heo   [PATCH] fix try_m...
143

68c43f133   Damien Le Moal   block: Introduce ...
144
  	e = elevator_find(name, q->required_elevator_features);
21c3c5d28   Tejun Heo   block: don't requ...
145
  	if (!e && try_loading) {
e16409496   Jens Axboe   elevator: make el...
146
  		spin_unlock(&elv_list_lock);
490b94be0   Kees Cook   iosched: remove r...
147
  		request_module("%s-iosched", name);
e16409496   Jens Axboe   elevator: make el...
148
  		spin_lock(&elv_list_lock);
68c43f133   Damien Le Moal   block: Introduce ...
149
  		e = elevator_find(name, q->required_elevator_features);
e16409496   Jens Axboe   elevator: make el...
150
  	}
2824bc932   Tejun Heo   [PATCH] fix try_m...
151
152
  	if (e && !try_module_get(e->elevator_owner))
  		e = NULL;
2a12dcd71   Jens Axboe   [PATCH] elevator:...
153
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
  	return e;
  }
3d1ab40f4   Al Viro   [PATCH] elevator_...
156
  static struct kobj_type elv_ktype;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
157
  struct elevator_queue *elevator_alloc(struct request_queue *q,
165125e1e   Jens Axboe   [BLOCK] Get rid o...
158
  				  struct elevator_type *e)
3d1ab40f4   Al Viro   [PATCH] elevator_...
159
  {
b374d18a4   Jens Axboe   block: get rid of...
160
  	struct elevator_queue *eq;
9817064b6   Jens Axboe   [PATCH] elevator:...
161

c1b511eb2   Joe Perches   block: Convert km...
162
  	eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
9817064b6   Jens Axboe   [PATCH] elevator:...
163
  	if (unlikely(!eq))
8406a4d56   Chao Yu   elevator: fix dou...
164
  		return NULL;
9817064b6   Jens Axboe   [PATCH] elevator:...
165

22f746e23   Tejun Heo   block: remove ele...
166
  	eq->type = e;
f9cb074bf   Greg Kroah-Hartman   Kobject: rename k...
167
  	kobject_init(&eq->kobj, &elv_ktype);
9817064b6   Jens Axboe   [PATCH] elevator:...
168
  	mutex_init(&eq->sysfs_lock);
242d98f07   Sasha Levin   block,elevator: u...
169
  	hash_init(eq->hash);
9817064b6   Jens Axboe   [PATCH] elevator:...
170

3d1ab40f4   Al Viro   [PATCH] elevator_...
171
172
  	return eq;
  }
d50235b7b   Jianpeng Ma   elevator: Fix a r...
173
  EXPORT_SYMBOL(elevator_alloc);
3d1ab40f4   Al Viro   [PATCH] elevator_...
174
175
176
  
  static void elevator_release(struct kobject *kobj)
  {
b374d18a4   Jens Axboe   block: get rid of...
177
  	struct elevator_queue *e;
9817064b6   Jens Axboe   [PATCH] elevator:...
178

b374d18a4   Jens Axboe   block: get rid of...
179
  	e = container_of(kobj, struct elevator_queue, kobj);
22f746e23   Tejun Heo   block: remove ele...
180
  	elevator_put(e->type);
3d1ab40f4   Al Viro   [PATCH] elevator_...
181
182
  	kfree(e);
  }
c3e221921   Ming Lei   block: free sched...
183
  void __elevator_exit(struct request_queue *q, struct elevator_queue *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
185
  	mutex_lock(&e->sysfs_lock);
f9cd4bfe9   Jens Axboe   block: get rid of...
186
  	if (e->type->ops.exit_sched)
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
187
  		blk_mq_exit_sched(q, e);
3d1ab40f4   Al Viro   [PATCH] elevator_...
188
  	mutex_unlock(&e->sysfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189

3d1ab40f4   Al Viro   [PATCH] elevator_...
190
  	kobject_put(&e->kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
192

9817064b6   Jens Axboe   [PATCH] elevator:...
193
194
  static inline void __elv_rqhash_del(struct request *rq)
  {
242d98f07   Sasha Levin   block,elevator: u...
195
  	hash_del(&rq->hash);
e80640213   Christoph Hellwig   block: split out ...
196
  	rq->rq_flags &= ~RQF_HASHED;
9817064b6   Jens Axboe   [PATCH] elevator:...
197
  }
70b3ea056   Jens Axboe   elevator: make th...
198
  void elv_rqhash_del(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
199
200
201
202
  {
  	if (ELV_ON_HASH(rq))
  		__elv_rqhash_del(rq);
  }
bd166ef18   Jens Axboe   blk-mq-sched: add...
203
  EXPORT_SYMBOL_GPL(elv_rqhash_del);
9817064b6   Jens Axboe   [PATCH] elevator:...
204

70b3ea056   Jens Axboe   elevator: make th...
205
  void elv_rqhash_add(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
206
  {
b374d18a4   Jens Axboe   block: get rid of...
207
  	struct elevator_queue *e = q->elevator;
9817064b6   Jens Axboe   [PATCH] elevator:...
208
209
  
  	BUG_ON(ELV_ON_HASH(rq));
242d98f07   Sasha Levin   block,elevator: u...
210
  	hash_add(e->hash, &rq->hash, rq_hash_key(rq));
e80640213   Christoph Hellwig   block: split out ...
211
  	rq->rq_flags |= RQF_HASHED;
9817064b6   Jens Axboe   [PATCH] elevator:...
212
  }
bd166ef18   Jens Axboe   blk-mq-sched: add...
213
  EXPORT_SYMBOL_GPL(elv_rqhash_add);
9817064b6   Jens Axboe   [PATCH] elevator:...
214

70b3ea056   Jens Axboe   elevator: make th...
215
  void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
216
217
218
219
  {
  	__elv_rqhash_del(rq);
  	elv_rqhash_add(q, rq);
  }
70b3ea056   Jens Axboe   elevator: make th...
220
  struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
9817064b6   Jens Axboe   [PATCH] elevator:...
221
  {
b374d18a4   Jens Axboe   block: get rid of...
222
  	struct elevator_queue *e = q->elevator;
b67bfe0d4   Sasha Levin   hlist: drop the n...
223
  	struct hlist_node *next;
9817064b6   Jens Axboe   [PATCH] elevator:...
224
  	struct request *rq;
ee89f8125   Linus Torvalds   Merge branch 'for...
225
  	hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
9817064b6   Jens Axboe   [PATCH] elevator:...
226
227
228
229
230
231
232
233
234
235
236
237
238
  		BUG_ON(!ELV_ON_HASH(rq));
  
  		if (unlikely(!rq_mergeable(rq))) {
  			__elv_rqhash_del(rq);
  			continue;
  		}
  
  		if (rq_hash_key(rq) == offset)
  			return rq;
  	}
  
  	return NULL;
  }
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
239
  /*
2e662b65f   Jens Axboe   [PATCH] elevator:...
240
241
242
   * RB-tree support functions for inserting/lookup/removal of requests
   * in a sorted RB tree.
   */
796d5116c   Jeff Moyer   iosched: prevent ...
243
  void elv_rb_add(struct rb_root *root, struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
244
245
246
247
248
249
250
251
  {
  	struct rb_node **p = &root->rb_node;
  	struct rb_node *parent = NULL;
  	struct request *__rq;
  
  	while (*p) {
  		parent = *p;
  		__rq = rb_entry(parent, struct request, rb_node);
83096ebf1   Tejun Heo   block: convert to...
252
  		if (blk_rq_pos(rq) < blk_rq_pos(__rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
253
  			p = &(*p)->rb_left;
796d5116c   Jeff Moyer   iosched: prevent ...
254
  		else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
255
  			p = &(*p)->rb_right;
2e662b65f   Jens Axboe   [PATCH] elevator:...
256
257
258
259
  	}
  
  	rb_link_node(&rq->rb_node, parent, p);
  	rb_insert_color(&rq->rb_node, root);
2e662b65f   Jens Axboe   [PATCH] elevator:...
260
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
261
262
263
264
265
266
267
268
  EXPORT_SYMBOL(elv_rb_add);
  
  void elv_rb_del(struct rb_root *root, struct request *rq)
  {
  	BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
  	rb_erase(&rq->rb_node, root);
  	RB_CLEAR_NODE(&rq->rb_node);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
269
270
271
272
273
274
275
276
277
  EXPORT_SYMBOL(elv_rb_del);
  
  struct request *elv_rb_find(struct rb_root *root, sector_t sector)
  {
  	struct rb_node *n = root->rb_node;
  	struct request *rq;
  
  	while (n) {
  		rq = rb_entry(n, struct request, rb_node);
83096ebf1   Tejun Heo   block: convert to...
278
  		if (sector < blk_rq_pos(rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
279
  			n = n->rb_left;
83096ebf1   Tejun Heo   block: convert to...
280
  		else if (sector > blk_rq_pos(rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
281
282
283
284
285
286
287
  			n = n->rb_right;
  		else
  			return rq;
  	}
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
288
  EXPORT_SYMBOL(elv_rb_find);
34fe7c054   Christoph Hellwig   block: enumify EL...
289
290
  enum elv_merge elv_merge(struct request_queue *q, struct request **req,
  		struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  {
b374d18a4   Jens Axboe   block: get rid of...
292
  	struct elevator_queue *e = q->elevator;
9817064b6   Jens Axboe   [PATCH] elevator:...
293
  	struct request *__rq;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
294

9817064b6   Jens Axboe   [PATCH] elevator:...
295
  	/*
488991e28   Alan D. Brunelle   block: Added in s...
296
297
298
299
300
  	 * Levels of merges:
  	 * 	nomerges:  No merges at all attempted
  	 * 	noxmerges: Only simple one-hit cache try
  	 * 	merges:	   All merge tries attempted
  	 */
7460d389c   Ming Lei   block: check bio_...
301
  	if (blk_queue_nomerges(q) || !bio_mergeable(bio))
488991e28   Alan D. Brunelle   block: Added in s...
302
303
304
  		return ELEVATOR_NO_MERGE;
  
  	/*
9817064b6   Jens Axboe   [PATCH] elevator:...
305
306
  	 * First try one-hit cache.
  	 */
72ef799b3   Tahsin Erdogan   block: do not mer...
307
  	if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
34fe7c054   Christoph Hellwig   block: enumify EL...
308
  		enum elv_merge ret = blk_try_merge(q->last_merge, bio);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
309
310
311
312
313
  		if (ret != ELEVATOR_NO_MERGE) {
  			*req = q->last_merge;
  			return ret;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314

488991e28   Alan D. Brunelle   block: Added in s...
315
  	if (blk_queue_noxmerges(q))
ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
316
  		return ELEVATOR_NO_MERGE;
9817064b6   Jens Axboe   [PATCH] elevator:...
317
318
319
  	/*
  	 * See if our hash lookup can find a potential backmerge.
  	 */
4f024f379   Kent Overstreet   block: Abstract o...
320
  	__rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
72ef799b3   Tahsin Erdogan   block: do not mer...
321
  	if (__rq && elv_bio_merge_ok(__rq, bio)) {
9817064b6   Jens Axboe   [PATCH] elevator:...
322
323
324
  		*req = __rq;
  		return ELEVATOR_BACK_MERGE;
  	}
f9cd4bfe9   Jens Axboe   block: get rid of...
325
326
  	if (e->type->ops.request_merge)
  		return e->type->ops.request_merge(q, req, bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
  
  	return ELEVATOR_NO_MERGE;
  }
5e84ea3a9   Jens Axboe   block: attempt to...
330
331
332
333
334
335
336
  /*
   * Attempt to do an insertion back merge. Only check for the case where
   * we can append 'rq' to an existing request, so we can throw 'rq' away
   * afterwards.
   *
   * Returns true if we merged, false otherwise
   */
bd166ef18   Jens Axboe   blk-mq-sched: add...
337
  bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
5e84ea3a9   Jens Axboe   block: attempt to...
338
339
  {
  	struct request *__rq;
bee0393cc   Shaohua Li   block: recursive ...
340
  	bool ret;
5e84ea3a9   Jens Axboe   block: attempt to...
341
342
343
344
345
346
347
348
349
350
351
352
  
  	if (blk_queue_nomerges(q))
  		return false;
  
  	/*
  	 * First try one-hit cache.
  	 */
  	if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
  		return true;
  
  	if (blk_queue_noxmerges(q))
  		return false;
bee0393cc   Shaohua Li   block: recursive ...
353
  	ret = false;
5e84ea3a9   Jens Axboe   block: attempt to...
354
355
356
  	/*
  	 * See if our hash lookup can find a potential backmerge.
  	 */
bee0393cc   Shaohua Li   block: recursive ...
357
358
359
360
361
362
363
364
365
  	while (1) {
  		__rq = elv_rqhash_find(q, blk_rq_pos(rq));
  		if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
  			break;
  
  		/* The merged request could be merged with others, try again */
  		ret = true;
  		rq = __rq;
  	}
274193224   Shaohua Li   block: recursive ...
366

bee0393cc   Shaohua Li   block: recursive ...
367
  	return ret;
5e84ea3a9   Jens Axboe   block: attempt to...
368
  }
34fe7c054   Christoph Hellwig   block: enumify EL...
369
370
  void elv_merged_request(struct request_queue *q, struct request *rq,
  		enum elv_merge type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
  {
b374d18a4   Jens Axboe   block: get rid of...
372
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373

f9cd4bfe9   Jens Axboe   block: get rid of...
374
375
  	if (e->type->ops.request_merged)
  		e->type->ops.request_merged(q, rq, type);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
376

2e662b65f   Jens Axboe   [PATCH] elevator:...
377
378
  	if (type == ELEVATOR_BACK_MERGE)
  		elv_rqhash_reposition(q, rq);
9817064b6   Jens Axboe   [PATCH] elevator:...
379

06b86245c   Tejun Heo   [PATCH] 03/05 mov...
380
  	q->last_merge = rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
382
  void elv_merge_requests(struct request_queue *q, struct request *rq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
  			     struct request *next)
  {
b374d18a4   Jens Axboe   block: get rid of...
385
  	struct elevator_queue *e = q->elevator;
bd166ef18   Jens Axboe   blk-mq-sched: add...
386

f9cd4bfe9   Jens Axboe   block: get rid of...
387
388
  	if (e->type->ops.requests_merged)
  		e->type->ops.requests_merged(q, rq, next);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
389

9817064b6   Jens Axboe   [PATCH] elevator:...
390
  	elv_rqhash_reposition(q, rq);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
391
  	q->last_merge = rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
392
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
393
  struct request *elv_latter_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  {
b374d18a4   Jens Axboe   block: get rid of...
395
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396

f9cd4bfe9   Jens Axboe   block: get rid of...
397
398
  	if (e->type->ops.next_request)
  		return e->type->ops.next_request(q, rq);
bd166ef18   Jens Axboe   blk-mq-sched: add...
399

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
  	return NULL;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
402
  struct request *elv_former_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
  {
b374d18a4   Jens Axboe   block: get rid of...
404
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405

f9cd4bfe9   Jens Axboe   block: get rid of...
406
407
  	if (e->type->ops.former_request)
  		return e->type->ops.former_request(q, rq);
bd166ef18   Jens Axboe   blk-mq-sched: add...
408

a1ce35fa4   Jens Axboe   block: remove dea...
409
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  }
3d1ab40f4   Al Viro   [PATCH] elevator_...
411
412
413
414
  #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  
  static ssize_t
  elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
416
  	struct elv_fs_entry *entry = to_elv(attr);
b374d18a4   Jens Axboe   block: get rid of...
417
  	struct elevator_queue *e;
3d1ab40f4   Al Viro   [PATCH] elevator_...
418
419
420
421
  	ssize_t error;
  
  	if (!entry->show)
  		return -EIO;
b374d18a4   Jens Axboe   block: get rid of...
422
  	e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f4   Al Viro   [PATCH] elevator_...
423
  	mutex_lock(&e->sysfs_lock);
22f746e23   Tejun Heo   block: remove ele...
424
  	error = e->type ? entry->show(e, page) : -ENOENT;
3d1ab40f4   Al Viro   [PATCH] elevator_...
425
426
427
  	mutex_unlock(&e->sysfs_lock);
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428

3d1ab40f4   Al Viro   [PATCH] elevator_...
429
430
431
432
  static ssize_t
  elv_attr_store(struct kobject *kobj, struct attribute *attr,
  	       const char *page, size_t length)
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
433
  	struct elv_fs_entry *entry = to_elv(attr);
b374d18a4   Jens Axboe   block: get rid of...
434
  	struct elevator_queue *e;
3d1ab40f4   Al Viro   [PATCH] elevator_...
435
  	ssize_t error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436

3d1ab40f4   Al Viro   [PATCH] elevator_...
437
438
  	if (!entry->store)
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439

b374d18a4   Jens Axboe   block: get rid of...
440
  	e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f4   Al Viro   [PATCH] elevator_...
441
  	mutex_lock(&e->sysfs_lock);
22f746e23   Tejun Heo   block: remove ele...
442
  	error = e->type ? entry->store(e, page, length) : -ENOENT;
3d1ab40f4   Al Viro   [PATCH] elevator_...
443
444
445
  	mutex_unlock(&e->sysfs_lock);
  	return error;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
446
  static const struct sysfs_ops elv_sysfs_ops = {
3d1ab40f4   Al Viro   [PATCH] elevator_...
447
448
449
450
451
452
453
454
  	.show	= elv_attr_show,
  	.store	= elv_attr_store,
  };
  
  static struct kobj_type elv_ktype = {
  	.sysfs_ops	= &elv_sysfs_ops,
  	.release	= elevator_release,
  };
cecf5d87f   Ming Lei   block: split .sys...
455
456
457
458
459
460
  /*
   * elv_register_queue is called from either blk_register_queue or
   * elevator_switch, elevator switch is prevented from being happen
   * in the two paths, so it is safe to not hold q->sysfs_lock.
   */
  int elv_register_queue(struct request_queue *q, bool uevent)
3d1ab40f4   Al Viro   [PATCH] elevator_...
461
  {
5a5bafdc3   Tejun Heo   elevator: clear a...
462
  	struct elevator_queue *e = q->elevator;
3d1ab40f4   Al Viro   [PATCH] elevator_...
463
  	int error;
b2d6db587   Greg Kroah-Hartman   Kobject: rename k...
464
  	error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
3d1ab40f4   Al Viro   [PATCH] elevator_...
465
  	if (!error) {
22f746e23   Tejun Heo   block: remove ele...
466
  		struct elv_fs_entry *attr = e->type->elevator_attrs;
3d1ab40f4   Al Viro   [PATCH] elevator_...
467
  		if (attr) {
e572ec7e4   Al Viro   [PATCH] fix rmmod...
468
469
  			while (attr->attr.name) {
  				if (sysfs_create_file(&e->kobj, &attr->attr))
3d1ab40f4   Al Viro   [PATCH] elevator_...
470
  					break;
e572ec7e4   Al Viro   [PATCH] fix rmmod...
471
  				attr++;
3d1ab40f4   Al Viro   [PATCH] elevator_...
472
473
  			}
  		}
cecf5d87f   Ming Lei   block: split .sys...
474
475
  		if (uevent)
  			kobject_uevent(&e->kobj, KOBJ_ADD);
430c62fb2   Jens Axboe   elevator: fix oop...
476
  		e->registered = 1;
3d1ab40f4   Al Viro   [PATCH] elevator_...
477
478
  	}
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
  }
bc1c11697   Jens Axboe   [PATCH] elevator ...
480

cecf5d87f   Ming Lei   block: split .sys...
481
482
483
484
485
  /*
   * elv_unregister_queue is called from either blk_unregister_queue or
   * elevator_switch, elevator switch is prevented from being happen
   * in the two paths, so it is safe to not hold q->sysfs_lock.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
  void elv_unregister_queue(struct request_queue *q)
  {
f8fc877d3   Tejun Heo   block: reorder el...
488
489
490
491
492
  	if (q) {
  		struct elevator_queue *e = q->elevator;
  
  		kobject_uevent(&e->kobj, KOBJ_REMOVE);
  		kobject_del(&e->kobj);
cecf5d87f   Ming Lei   block: split .sys...
493

f8fc877d3   Tejun Heo   block: reorder el...
494
  		e->registered = 0;
8330cdb0f   Jan Kara   block: Make write...
495
496
  		/* Re-enable throttling in case elevator disabled it */
  		wbt_enable_default(q);
f8fc877d3   Tejun Heo   block: reorder el...
497
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498
  }
e567bf711   Jens Axboe   Revert "block: ad...
499
  int elv_register(struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  	/* create icq_cache if requested */
  	if (e->icq_size) {
  		if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
  		    WARN_ON(e->icq_align < __alignof__(struct io_cq)))
  			return -EINVAL;
  
  		snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
  			 "%s_io_cq", e->elevator_name);
  		e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
  						 e->icq_align, 0, NULL);
  		if (!e->icq_cache)
  			return -ENOMEM;
  	}
  
  	/* register, don't allow duplicate names */
2a12dcd71   Jens Axboe   [PATCH] elevator:...
516
  	spin_lock(&elv_list_lock);
68c43f133   Damien Le Moal   block: Introduce ...
517
  	if (elevator_find(e->elevator_name, 0)) {
3d3c2379f   Tejun Heo   block, cfq: move ...
518
  		spin_unlock(&elv_list_lock);
62d2a1940   Chengguang Xu   block: remove unn...
519
  		kmem_cache_destroy(e->icq_cache);
3d3c2379f   Tejun Heo   block, cfq: move ...
520
521
  		return -EBUSY;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
522
  	list_add_tail(&e->list, &elv_list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
523
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524

d0b0a81ac   Hisao Tanabe   block: remove unu...
525
526
  	printk(KERN_INFO "io scheduler %s registered
  ", e->elevator_name);
3d3c2379f   Tejun Heo   block, cfq: move ...
527
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
531
532
  }
  EXPORT_SYMBOL_GPL(elv_register);
  
  void elv_unregister(struct elevator_type *e)
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
533
  	/* unregister */
2a12dcd71   Jens Axboe   [PATCH] elevator:...
534
  	spin_lock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
  	list_del_init(&e->list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
536
  	spin_unlock(&elv_list_lock);
3d3c2379f   Tejun Heo   block, cfq: move ...
537
538
539
540
541
542
543
544
545
546
  
  	/*
  	 * Destroy icq_cache if it exists.  icq's are RCU managed.  Make
  	 * sure all RCU operations are complete before proceeding.
  	 */
  	if (e->icq_cache) {
  		rcu_barrier();
  		kmem_cache_destroy(e->icq_cache);
  		e->icq_cache = NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
  }
  EXPORT_SYMBOL_GPL(elv_unregister);
d48ece209   Jianchao Wang   blk-mq: init hctx...
549
  int elevator_switch_mq(struct request_queue *q,
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
550
551
552
  			      struct elevator_type *new_e)
  {
  	int ret;
14a23498b   Bart Van Assche   block: Document s...
553
  	lockdep_assert_held(&q->sysfs_lock);
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
554
  	if (q->elevator) {
b89f625e2   Ming Lei   block: don't rele...
555
  		if (q->elevator->registered)
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
556
  			elv_unregister_queue(q);
cecf5d87f   Ming Lei   block: split .sys...
557

54d5329d4   Omar Sandoval   blk-mq-sched: fix...
558
559
560
561
562
563
564
565
566
  		ioc_clear_queue(q);
  		elevator_exit(q, q->elevator);
  	}
  
  	ret = blk_mq_init_sched(q, new_e);
  	if (ret)
  		goto out;
  
  	if (new_e) {
cecf5d87f   Ming Lei   block: split .sys...
567
  		ret = elv_register_queue(q, true);
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
568
569
570
571
572
573
574
575
576
577
578
579
  		if (ret) {
  			elevator_exit(q, q->elevator);
  			goto out;
  		}
  	}
  
  	if (new_e)
  		blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
  	else
  		blk_add_trace_msg(q, "elv switch: none");
  
  out:
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
580
  	return ret;
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
581
  }
61db437d1   Damien Le Moal   block: Cleanup el...
582
583
  static inline bool elv_support_iosched(struct request_queue *q)
  {
7a7c5e715   Damien Le Moal   block: Fix elv_su...
584
585
  	if (!q->mq_ops ||
  	    (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)))
61db437d1   Damien Le Moal   block: Cleanup el...
586
587
588
  		return false;
  	return true;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  /*
a0958ba7f   Damien Le Moal   block: Improve de...
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
   * For single queue devices, default to using mq-deadline. If we have multiple
   * queues or mq-deadline is not available, default to "none".
   */
  static struct elevator_type *elevator_get_default(struct request_queue *q)
  {
  	if (q->nr_hw_queues != 1)
  		return NULL;
  
  	return elevator_get(q, "mq-deadline", false);
  }
  
  /*
   * Get the first elevator providing the features required by the request queue.
   * Default to "none" if no matching elevator is found.
   */
  static struct elevator_type *elevator_get_by_features(struct request_queue *q)
  {
a26142559   Jens Axboe   block: fix elevat...
607
  	struct elevator_type *e, *found = NULL;
a0958ba7f   Damien Le Moal   block: Improve de...
608
609
610
611
612
  
  	spin_lock(&elv_list_lock);
  
  	list_for_each_entry(e, &elv_list, list) {
  		if (elv_support_features(e->elevator_features,
a26142559   Jens Axboe   block: fix elevat...
613
614
  					 q->required_elevator_features)) {
  			found = e;
a0958ba7f   Damien Le Moal   block: Improve de...
615
  			break;
a26142559   Jens Axboe   block: fix elevat...
616
  		}
a0958ba7f   Damien Le Moal   block: Improve de...
617
  	}
a26142559   Jens Axboe   block: fix elevat...
618
619
  	if (found && !try_module_get(found->elevator_owner))
  		found = NULL;
a0958ba7f   Damien Le Moal   block: Improve de...
620
621
  
  	spin_unlock(&elv_list_lock);
a26142559   Jens Axboe   block: fix elevat...
622
  	return found;
a0958ba7f   Damien Le Moal   block: Improve de...
623
624
625
626
627
628
629
  }
  
  /*
   * For a device queue that has no required features, use the default elevator
   * settings. Otherwise, use the first elevator available matching the required
   * features. If no suitable elevator is find or if the chosen elevator
   * initialization fails, fall back to the "none" elevator (no elevator).
131d08e12   Christoph Hellwig   block: split the ...
630
   */
954b4a5ce   Damien Le Moal   block: Change ele...
631
  void elevator_init_mq(struct request_queue *q)
131d08e12   Christoph Hellwig   block: split the ...
632
633
  {
  	struct elevator_type *e;
954b4a5ce   Damien Le Moal   block: Change ele...
634
  	int err;
131d08e12   Christoph Hellwig   block: split the ...
635

61db437d1   Damien Le Moal   block: Cleanup el...
636
  	if (!elv_support_iosched(q))
954b4a5ce   Damien Le Moal   block: Change ele...
637
  		return;
61db437d1   Damien Le Moal   block: Cleanup el...
638

c48dac137   Ming Lei   block: don't hold...
639
  	WARN_ON_ONCE(test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags));
131d08e12   Christoph Hellwig   block: split the ...
640
  	if (unlikely(q->elevator))
954b4a5ce   Damien Le Moal   block: Change ele...
641
  		return;
131d08e12   Christoph Hellwig   block: split the ...
642

a0958ba7f   Damien Le Moal   block: Improve de...
643
644
645
646
  	if (!q->required_elevator_features)
  		e = elevator_get_default(q);
  	else
  		e = elevator_get_by_features(q);
131d08e12   Christoph Hellwig   block: split the ...
647
  	if (!e)
954b4a5ce   Damien Le Moal   block: Change ele...
648
  		return;
131d08e12   Christoph Hellwig   block: split the ...
649

737eb78e8   Damien Le Moal   block: Delay defa...
650
651
  	blk_mq_freeze_queue(q);
  	blk_mq_quiesce_queue(q);
131d08e12   Christoph Hellwig   block: split the ...
652
  	err = blk_mq_init_sched(q, e);
737eb78e8   Damien Le Moal   block: Delay defa...
653
654
655
  
  	blk_mq_unquiesce_queue(q);
  	blk_mq_unfreeze_queue(q);
954b4a5ce   Damien Le Moal   block: Change ele...
656
657
658
659
  	if (err) {
  		pr_warn("\"%s\" elevator initialization failed, "
  			"falling back to \"none\"
  ", e->elevator_name);
131d08e12   Christoph Hellwig   block: split the ...
660
  		elevator_put(e);
954b4a5ce   Damien Le Moal   block: Change ele...
661
  	}
131d08e12   Christoph Hellwig   block: split the ...
662
663
664
665
  }
  
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
668
   * switch to new_e io scheduler. be careful not to introduce deadlocks -
   * we don't free the old io scheduler, before we have allocated what we
   * need for the new one. this way we have a chance of going back to the old
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
669
   * one, if the new one fails init for some reason.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
671
  static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  {
e8989fae3   Tejun Heo   blkcg: unify blkg...
673
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674

14a23498b   Bart Van Assche   block: Document s...
675
  	lockdep_assert_held(&q->sysfs_lock);
a1ce35fa4   Jens Axboe   block: remove dea...
676
677
  	blk_mq_freeze_queue(q);
  	blk_mq_quiesce_queue(q);
4722dc52a   Alan D. Brunelle   Added in elevator...
678

a1ce35fa4   Jens Axboe   block: remove dea...
679
  	err = elevator_switch_mq(q, new_e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680

a1ce35fa4   Jens Axboe   block: remove dea...
681
682
  	blk_mq_unquiesce_queue(q);
  	blk_mq_unfreeze_queue(q);
75ad23bc0   Nick Piggin   block: make queue...
683

5dd531a03   Jens Axboe   block: add functi...
684
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
685
  }
5dd531a03   Jens Axboe   block: add functi...
686
687
688
  /*
   * Switch this queue to the given IO scheduler.
   */
7c8a3679e   Tomoki Sekiyama   elevator: acquire...
689
  static int __elevator_change(struct request_queue *q, const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
692
  {
  	char elevator_name[ELV_NAME_MAX];
  	struct elevator_type *e;
e9a823fb3   David Jeffery   block: fix warnin...
693
  	/* Make sure queue is not in the middle of being removed */
58c898ba3   Ming Lei   block: add helper...
694
  	if (!blk_queue_registered(q))
e9a823fb3   David Jeffery   block: fix warnin...
695
  		return -ENOENT;
bd166ef18   Jens Axboe   blk-mq-sched: add...
696
697
698
  	/*
  	 * Special case for mq, turn off scheduling
  	 */
fbd72127c   Aleksei Zakharov   block: avoid sett...
699
700
701
  	if (!strncmp(name, "none", 4)) {
  		if (!q->elevator)
  			return 0;
bd166ef18   Jens Axboe   blk-mq-sched: add...
702
  		return elevator_switch(q, NULL);
fbd72127c   Aleksei Zakharov   block: avoid sett...
703
  	}
cd43e26f0   Martin K. Petersen   block: Expose sta...
704

ee2e992cc   Li Zefan   block: simplify s...
705
  	strlcpy(elevator_name, name, sizeof(elevator_name));
2527d9978   Jens Axboe   elevator: lookup ...
706
  	e = elevator_get(q, strstrip(elevator_name), true);
340ff3216   Jens Axboe   elevator: remove ...
707
  	if (!e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709

68c43f133   Damien Le Moal   block: Introduce ...
710
711
  	if (q->elevator &&
  	    elevator_match(q->elevator->type, elevator_name, 0)) {
2ca7d93bb   Nate Diller   [PATCH] block cle...
712
  		elevator_put(e);
5dd531a03   Jens Axboe   block: add functi...
713
  		return 0;
2ca7d93bb   Nate Diller   [PATCH] block cle...
714
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715

5dd531a03   Jens Axboe   block: add functi...
716
717
  	return elevator_switch(q, e);
  }
7c8a3679e   Tomoki Sekiyama   elevator: acquire...
718

5dd531a03   Jens Axboe   block: add functi...
719
720
721
722
  ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  			  size_t count)
  {
  	int ret;
344e9ffcb   Jens Axboe   block: add queue_...
723
  	if (!queue_is_mq(q) || !elv_support_iosched(q))
5dd531a03   Jens Axboe   block: add functi...
724
  		return count;
7c8a3679e   Tomoki Sekiyama   elevator: acquire...
725
  	ret = __elevator_change(q, name);
5dd531a03   Jens Axboe   block: add functi...
726
727
  	if (!ret)
  		return count;
5dd531a03   Jens Axboe   block: add functi...
728
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
730
  ssize_t elv_iosched_show(struct request_queue *q, char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
  {
b374d18a4   Jens Axboe   block: get rid of...
732
  	struct elevator_queue *e = q->elevator;
bd166ef18   Jens Axboe   blk-mq-sched: add...
733
  	struct elevator_type *elv = NULL;
70cee26e0   Matthias Kaehlcke   Use list_for_each...
734
  	struct elevator_type *__e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
  	int len = 0;
344e9ffcb   Jens Axboe   block: add queue_...
736
  	if (!queue_is_mq(q))
cd43e26f0   Martin K. Petersen   block: Expose sta...
737
738
  		return sprintf(name, "none
  ");
bd166ef18   Jens Axboe   blk-mq-sched: add...
739
740
741
742
  	if (!q->elevator)
  		len += sprintf(name+len, "[none] ");
  	else
  		elv = e->type;
cd43e26f0   Martin K. Petersen   block: Expose sta...
743

2a12dcd71   Jens Axboe   [PATCH] elevator:...
744
  	spin_lock(&elv_list_lock);
70cee26e0   Matthias Kaehlcke   Use list_for_each...
745
  	list_for_each_entry(__e, &elv_list, list) {
68c43f133   Damien Le Moal   block: Introduce ...
746
  		if (elv && elevator_match(elv, __e->elevator_name, 0)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
  			len += sprintf(name+len, "[%s] ", elv->elevator_name);
bd166ef18   Jens Axboe   blk-mq-sched: add...
748
749
  			continue;
  		}
68c43f133   Damien Le Moal   block: Introduce ...
750
751
752
  		if (elv_support_iosched(q) &&
  		    elevator_match(__e, __e->elevator_name,
  				   q->required_elevator_features))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
  			len += sprintf(name+len, "%s ", __e->elevator_name);
  	}
2a12dcd71   Jens Axboe   [PATCH] elevator:...
755
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756

344e9ffcb   Jens Axboe   block: add queue_...
757
  	if (q->elevator)
bd166ef18   Jens Axboe   blk-mq-sched: add...
758
  		len += sprintf(name+len, "none");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
760
761
762
  	len += sprintf(len+name, "
  ");
  	return len;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
763
764
  struct request *elv_rb_former_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
765
766
767
768
769
770
771
772
  {
  	struct rb_node *rbprev = rb_prev(&rq->rb_node);
  
  	if (rbprev)
  		return rb_entry_rq(rbprev);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
773
  EXPORT_SYMBOL(elv_rb_former_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
774
775
  struct request *elv_rb_latter_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
776
777
778
779
780
781
782
783
  {
  	struct rb_node *rbnext = rb_next(&rq->rb_node);
  
  	if (rbnext)
  		return rb_entry_rq(rbnext);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
784
  EXPORT_SYMBOL(elv_rb_latter_request);