Blame view

block/elevator.c 18.5 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
  	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
   *
5b8f65e1f   Randy Dunlap   block: elevator: ...
97
98
   * Return true if the elevator @e name matches @name and if @e provides all
   * the features specified by @required_features.
68c43f133   Damien Le Moal   block: Introduce ...
99
100
101
102
103
104
   */
  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);
dd1c372d6   Yufen Yu   block: invoke blk...
186
  	blk_mq_exit_sched(q, e);
3d1ab40f4   Al Viro   [PATCH] elevator_...
187
  	mutex_unlock(&e->sysfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188

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

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

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

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

9817064b6   Jens Axboe   [PATCH] elevator:...
294
  	/*
488991e28   Alan D. Brunelle   block: Added in s...
295
296
297
298
299
  	 * 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_...
300
  	if (blk_queue_nomerges(q) || !bio_mergeable(bio))
488991e28   Alan D. Brunelle   block: Added in s...
301
302
303
  		return ELEVATOR_NO_MERGE;
  
  	/*
9817064b6   Jens Axboe   [PATCH] elevator:...
304
305
  	 * First try one-hit cache.
  	 */
72ef799b3   Tahsin Erdogan   block: do not mer...
306
  	if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
34fe7c054   Christoph Hellwig   block: enumify EL...
307
  		enum elv_merge ret = blk_try_merge(q->last_merge, bio);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
308
309
310
311
312
  		if (ret != ELEVATOR_NO_MERGE) {
  			*req = q->last_merge;
  			return ret;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313

488991e28   Alan D. Brunelle   block: Added in s...
314
  	if (blk_queue_noxmerges(q))
ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
315
  		return ELEVATOR_NO_MERGE;
9817064b6   Jens Axboe   [PATCH] elevator:...
316
317
318
  	/*
  	 * See if our hash lookup can find a potential backmerge.
  	 */
4f024f379   Kent Overstreet   block: Abstract o...
319
  	__rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
72ef799b3   Tahsin Erdogan   block: do not mer...
320
  	if (__rq && elv_bio_merge_ok(__rq, bio)) {
9817064b6   Jens Axboe   [PATCH] elevator:...
321
322
323
  		*req = __rq;
  		return ELEVATOR_BACK_MERGE;
  	}
f9cd4bfe9   Jens Axboe   block: get rid of...
324
325
  	if (e->type->ops.request_merge)
  		return e->type->ops.request_merge(q, req, bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
327
328
  
  	return ELEVATOR_NO_MERGE;
  }
5e84ea3a9   Jens Axboe   block: attempt to...
329
330
331
332
333
334
335
  /*
   * 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...
336
  bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
5e84ea3a9   Jens Axboe   block: attempt to...
337
338
  {
  	struct request *__rq;
bee0393cc   Shaohua Li   block: recursive ...
339
  	bool ret;
5e84ea3a9   Jens Axboe   block: attempt to...
340
341
342
343
344
345
346
347
348
349
350
351
  
  	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 ...
352
  	ret = false;
5e84ea3a9   Jens Axboe   block: attempt to...
353
354
355
  	/*
  	 * See if our hash lookup can find a potential backmerge.
  	 */
bee0393cc   Shaohua Li   block: recursive ...
356
357
358
359
360
361
362
363
364
  	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 ...
365

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

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

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

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

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

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

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

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

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

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

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

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

b374d18a4   Jens Axboe   block: get rid of...
439
  	e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f4   Al Viro   [PATCH] elevator_...
440
  	mutex_lock(&e->sysfs_lock);
22f746e23   Tejun Heo   block: remove ele...
441
  	error = e->type ? entry->store(e, page, length) : -ENOENT;
3d1ab40f4   Al Viro   [PATCH] elevator_...
442
443
444
  	mutex_unlock(&e->sysfs_lock);
  	return error;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
445
  static const struct sysfs_ops elv_sysfs_ops = {
3d1ab40f4   Al Viro   [PATCH] elevator_...
446
447
448
449
450
451
452
453
  	.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...
454
  int elv_register_queue(struct request_queue *q, bool uevent)
3d1ab40f4   Al Viro   [PATCH] elevator_...
455
  {
5a5bafdc3   Tejun Heo   elevator: clear a...
456
  	struct elevator_queue *e = q->elevator;
3d1ab40f4   Al Viro   [PATCH] elevator_...
457
  	int error;
f0c6ae09d   Yufen Yu   block: fix commen...
458
  	lockdep_assert_held(&q->sysfs_lock);
b2d6db587   Greg Kroah-Hartman   Kobject: rename k...
459
  	error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
3d1ab40f4   Al Viro   [PATCH] elevator_...
460
  	if (!error) {
22f746e23   Tejun Heo   block: remove ele...
461
  		struct elv_fs_entry *attr = e->type->elevator_attrs;
3d1ab40f4   Al Viro   [PATCH] elevator_...
462
  		if (attr) {
e572ec7e4   Al Viro   [PATCH] fix rmmod...
463
464
  			while (attr->attr.name) {
  				if (sysfs_create_file(&e->kobj, &attr->attr))
3d1ab40f4   Al Viro   [PATCH] elevator_...
465
  					break;
e572ec7e4   Al Viro   [PATCH] fix rmmod...
466
  				attr++;
3d1ab40f4   Al Viro   [PATCH] elevator_...
467
468
  			}
  		}
cecf5d87f   Ming Lei   block: split .sys...
469
470
  		if (uevent)
  			kobject_uevent(&e->kobj, KOBJ_ADD);
430c62fb2   Jens Axboe   elevator: fix oop...
471
  		e->registered = 1;
3d1ab40f4   Al Viro   [PATCH] elevator_...
472
473
  	}
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
  }
bc1c11697   Jens Axboe   [PATCH] elevator ...
475

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
  void elv_unregister_queue(struct request_queue *q)
  {
f0c6ae09d   Yufen Yu   block: fix commen...
478
  	lockdep_assert_held(&q->sysfs_lock);
f8fc877d3   Tejun Heo   block: reorder el...
479
480
481
482
483
  	if (q) {
  		struct elevator_queue *e = q->elevator;
  
  		kobject_uevent(&e->kobj, KOBJ_REMOVE);
  		kobject_del(&e->kobj);
cecf5d87f   Ming Lei   block: split .sys...
484

f8fc877d3   Tejun Heo   block: reorder el...
485
  		e->registered = 0;
8330cdb0f   Jan Kara   block: Make write...
486
487
  		/* Re-enable throttling in case elevator disabled it */
  		wbt_enable_default(q);
f8fc877d3   Tejun Heo   block: reorder el...
488
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
  }
e567bf711   Jens Axboe   Revert "block: ad...
490
  int elv_register(struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  	/* 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:...
507
  	spin_lock(&elv_list_lock);
68c43f133   Damien Le Moal   block: Introduce ...
508
  	if (elevator_find(e->elevator_name, 0)) {
3d3c2379f   Tejun Heo   block, cfq: move ...
509
  		spin_unlock(&elv_list_lock);
62d2a1940   Chengguang Xu   block: remove unn...
510
  		kmem_cache_destroy(e->icq_cache);
3d3c2379f   Tejun Heo   block, cfq: move ...
511
512
  		return -EBUSY;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  	list_add_tail(&e->list, &elv_list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
514
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515

d0b0a81ac   Hisao Tanabe   block: remove unu...
516
517
  	printk(KERN_INFO "io scheduler %s registered
  ", e->elevator_name);
3d3c2379f   Tejun Heo   block, cfq: move ...
518
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
523
  }
  EXPORT_SYMBOL_GPL(elv_register);
  
  void elv_unregister(struct elevator_type *e)
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
524
  	/* unregister */
2a12dcd71   Jens Axboe   [PATCH] elevator:...
525
  	spin_lock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  	list_del_init(&e->list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
527
  	spin_unlock(&elv_list_lock);
3d3c2379f   Tejun Heo   block, cfq: move ...
528
529
530
531
532
533
534
535
536
537
  
  	/*
  	 * 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
538
539
  }
  EXPORT_SYMBOL_GPL(elv_unregister);
d48ece209   Jianchao Wang   blk-mq: init hctx...
540
  int elevator_switch_mq(struct request_queue *q,
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
541
542
543
  			      struct elevator_type *new_e)
  {
  	int ret;
14a23498b   Bart Van Assche   block: Document s...
544
  	lockdep_assert_held(&q->sysfs_lock);
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
545
  	if (q->elevator) {
b89f625e2   Ming Lei   block: don't rele...
546
  		if (q->elevator->registered)
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
547
  			elv_unregister_queue(q);
cecf5d87f   Ming Lei   block: split .sys...
548

54d5329d4   Omar Sandoval   blk-mq-sched: fix...
549
550
551
552
553
554
555
556
557
  		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...
558
  		ret = elv_register_queue(q, true);
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
559
560
561
562
563
564
565
566
567
568
569
570
  		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...
571
  	return ret;
54d5329d4   Omar Sandoval   blk-mq-sched: fix...
572
  }
61db437d1   Damien Le Moal   block: Cleanup el...
573
574
  static inline bool elv_support_iosched(struct request_queue *q)
  {
6251b754f   Yufen Yu   block: remove red...
575
  	if (!queue_is_mq(q) ||
7a7c5e715   Damien Le Moal   block: Fix elv_su...
576
  	    (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)))
61db437d1   Damien Le Moal   block: Cleanup el...
577
578
579
  		return false;
  	return true;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
  /*
a0958ba7f   Damien Le Moal   block: Improve de...
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
   * 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...
598
  	struct elevator_type *e, *found = NULL;
a0958ba7f   Damien Le Moal   block: Improve de...
599
600
601
602
603
  
  	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...
604
605
  					 q->required_elevator_features)) {
  			found = e;
a0958ba7f   Damien Le Moal   block: Improve de...
606
  			break;
a26142559   Jens Axboe   block: fix elevat...
607
  		}
a0958ba7f   Damien Le Moal   block: Improve de...
608
  	}
a26142559   Jens Axboe   block: fix elevat...
609
610
  	if (found && !try_module_get(found->elevator_owner))
  		found = NULL;
a0958ba7f   Damien Le Moal   block: Improve de...
611
612
  
  	spin_unlock(&elv_list_lock);
a26142559   Jens Axboe   block: fix elevat...
613
  	return found;
a0958ba7f   Damien Le Moal   block: Improve de...
614
615
616
617
618
619
620
  }
  
  /*
   * 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 ...
621
   */
954b4a5ce   Damien Le Moal   block: Change ele...
622
  void elevator_init_mq(struct request_queue *q)
131d08e12   Christoph Hellwig   block: split the ...
623
624
  {
  	struct elevator_type *e;
954b4a5ce   Damien Le Moal   block: Change ele...
625
  	int err;
131d08e12   Christoph Hellwig   block: split the ...
626

61db437d1   Damien Le Moal   block: Cleanup el...
627
  	if (!elv_support_iosched(q))
954b4a5ce   Damien Le Moal   block: Change ele...
628
  		return;
61db437d1   Damien Le Moal   block: Cleanup el...
629

75e6c00fc   Yufen Yu   block: use helper...
630
  	WARN_ON_ONCE(blk_queue_registered(q));
c48dac137   Ming Lei   block: don't hold...
631

131d08e12   Christoph Hellwig   block: split the ...
632
  	if (unlikely(q->elevator))
954b4a5ce   Damien Le Moal   block: Change ele...
633
  		return;
131d08e12   Christoph Hellwig   block: split the ...
634

a0958ba7f   Damien Le Moal   block: Improve de...
635
636
637
638
  	if (!q->required_elevator_features)
  		e = elevator_get_default(q);
  	else
  		e = elevator_get_by_features(q);
131d08e12   Christoph Hellwig   block: split the ...
639
  	if (!e)
954b4a5ce   Damien Le Moal   block: Change ele...
640
  		return;
131d08e12   Christoph Hellwig   block: split the ...
641

737eb78e8   Damien Le Moal   block: Delay defa...
642
643
  	blk_mq_freeze_queue(q);
  	blk_mq_quiesce_queue(q);
131d08e12   Christoph Hellwig   block: split the ...
644
  	err = blk_mq_init_sched(q, e);
737eb78e8   Damien Le Moal   block: Delay defa...
645
646
647
  
  	blk_mq_unquiesce_queue(q);
  	blk_mq_unfreeze_queue(q);
954b4a5ce   Damien Le Moal   block: Change ele...
648
649
650
651
  	if (err) {
  		pr_warn("\"%s\" elevator initialization failed, "
  			"falling back to \"none\"
  ", e->elevator_name);
131d08e12   Christoph Hellwig   block: split the ...
652
  		elevator_put(e);
954b4a5ce   Damien Le Moal   block: Change ele...
653
  	}
131d08e12   Christoph Hellwig   block: split the ...
654
655
656
657
  }
  
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
659
660
   * 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...
661
   * one, if the new one fails init for some reason.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
663
  static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
  {
e8989fae3   Tejun Heo   blkcg: unify blkg...
665
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666

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

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

a1ce35fa4   Jens Axboe   block: remove dea...
673
674
  	blk_mq_unquiesce_queue(q);
  	blk_mq_unfreeze_queue(q);
75ad23bc0   Nick Piggin   block: make queue...
675

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

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

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

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

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

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

344e9ffcb   Jens Axboe   block: add queue_...
749
  	if (q->elevator)
bd166ef18   Jens Axboe   blk-mq-sched: add...
750
  		len += sprintf(name+len, "none");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
752
753
754
  	len += sprintf(len+name, "
  ");
  	return len;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
755
756
  struct request *elv_rb_former_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
757
758
759
760
761
762
763
764
  {
  	struct rb_node *rbprev = rb_prev(&rq->rb_node);
  
  	if (rbprev)
  		return rb_entry_rq(rbprev);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
765
  EXPORT_SYMBOL(elv_rb_former_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
766
767
  struct request *elv_rb_latter_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
768
769
770
771
772
773
774
775
  {
  	struct rb_node *rbnext = rb_next(&rq->rb_node);
  
  	if (rbnext)
  		return rb_entry_rq(rbnext);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
776
  EXPORT_SYMBOL(elv_rb_latter_request);
f8db38350   Jan Kara   block: Warn if el...
777
778
779
780
781
782
783
784
785
786
787
  
  static int __init elevator_setup(char *str)
  {
  	pr_warn("Kernel parameter elevator= does not have any effect anymore.
  "
  		"Please use sysfs to set IO scheduler for individual devices.
  ");
  	return 1;
  }
  
  __setup("elevator=", elevator_setup);