Blame view

block/elevator.c 24.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
   *  Block device elevator/IO-scheduler.
   *
   *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
   *
0fe234795   Jens Axboe   [PATCH] Update ax...
6
   * 30042000 Jens Axboe <axboe@kernel.dk> :
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   *
   * 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
30
31
32
33
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/init.h>
  #include <linux/compiler.h>
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
34
  #include <linux/delay.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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38

55782138e   Li Zefan   tracing/events: c...
39
  #include <trace/events/block.h>
242f9dcb8   Jens Axboe   block: unify requ...
40
  #include "blk.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
  static DEFINE_SPINLOCK(elv_list_lock);
  static LIST_HEAD(elv_list);
  
  /*
9817064b6   Jens Axboe   [PATCH] elevator:...
45
46
47
48
   * Merge hash stuff.
   */
  static const int elv_hash_shift = 6;
  #define ELV_HASH_BLOCK(sec)	((sec) >> 3)
4eb166d98   Jens Axboe   block: make eleva...
49
50
  #define ELV_HASH_FN(sec)	\
  		(hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
9817064b6   Jens Axboe   [PATCH] elevator:...
51
  #define ELV_HASH_ENTRIES	(1 << elv_hash_shift)
83096ebf1   Tejun Heo   block: convert to...
52
  #define rq_hash_key(rq)		(blk_rq_pos(rq) + blk_rq_sectors(rq))
9817064b6   Jens Axboe   [PATCH] elevator:...
53
54
  
  /*
da7752650   Jens Axboe   [PATCH] cfq-iosch...
55
56
57
58
59
   * Query io scheduler to see if the current process issuing bio may be
   * merged with rq.
   */
  static int elv_iosched_allow_merge(struct request *rq, struct bio *bio)
  {
165125e1e   Jens Axboe   [BLOCK] Get rid o...
60
  	struct request_queue *q = rq->q;
b374d18a4   Jens Axboe   block: get rid of...
61
  	struct elevator_queue *e = q->elevator;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
62
63
64
65
66
67
68
69
  
  	if (e->ops->elevator_allow_merge_fn)
  		return e->ops->elevator_allow_merge_fn(q, rq, bio);
  
  	return 1;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
   * can we safely merge with this request?
   */
72ed0bf60   Adrian Bunk   block/elevator.c:...
72
  int elv_rq_merge_ok(struct request *rq, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
  {
  	if (!rq_mergeable(rq))
  		return 0;
  
  	/*
e17fc0a1c   David Woodhouse   Allow elevators t...
78
79
  	 * Don't merge file system requests and discard requests
  	 */
7b6d91dae   Christoph Hellwig   block: unify flag...
80
  	if ((bio->bi_rw & REQ_DISCARD) != (rq->bio->bi_rw & REQ_DISCARD))
e17fc0a1c   David Woodhouse   Allow elevators t...
81
82
83
  		return 0;
  
  	/*
8d57a98cc   Adrian Hunter   block: add secure...
84
85
86
87
88
89
  	 * Don't merge discard requests and secure discard requests
  	 */
  	if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
  		return 0;
  
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
94
95
  	 * different data direction or already started, don't merge
  	 */
  	if (bio_data_dir(bio) != rq_data_dir(rq))
  		return 0;
  
  	/*
da7752650   Jens Axboe   [PATCH] cfq-iosch...
96
  	 * must be same device and not a special request
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  	 */
bb4067e34   Jens Axboe   [PATCH] elevator:...
98
  	if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
da7752650   Jens Axboe   [PATCH] cfq-iosch...
99
  		return 0;
7ba1ba12e   Martin K. Petersen   block: Block laye...
100
101
102
103
104
  	/*
  	 * only merge integrity protected bio into ditto rq
  	 */
  	if (bio_integrity(bio) != blk_integrity_rq(rq))
  		return 0;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
105
106
  	if (!elv_iosched_allow_merge(rq, bio))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107

da7752650   Jens Axboe   [PATCH] cfq-iosch...
108
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  }
  EXPORT_SYMBOL(elv_rq_merge_ok);
769db45b7   Coywolf Qi Hunt   make elv_try_merg...
111
  static inline int elv_try_merge(struct request *__rq, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
115
116
117
118
  {
  	int ret = ELEVATOR_NO_MERGE;
  
  	/*
  	 * we can merge and sequence is ok, check if it's possible
  	 */
  	if (elv_rq_merge_ok(__rq, bio)) {
83096ebf1   Tejun Heo   block: convert to...
119
  		if (blk_rq_pos(__rq) + blk_rq_sectors(__rq) == bio->bi_sector)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  			ret = ELEVATOR_BACK_MERGE;
83096ebf1   Tejun Heo   block: convert to...
121
  		else if (blk_rq_pos(__rq) - bio_sectors(bio) == bio->bi_sector)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
  			ret = ELEVATOR_FRONT_MERGE;
  	}
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
  static struct elevator_type *elevator_find(const char *name)
  {
a22b169df   Vasily Tarasov   [PATCH] block lay...
130
  	struct elevator_type *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131

70cee26e0   Matthias Kaehlcke   Use list_for_each...
132
  	list_for_each_entry(e, &elv_list, list) {
a22b169df   Vasily Tarasov   [PATCH] block lay...
133
134
  		if (!strcmp(e->elevator_name, name))
  			return e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136

a22b169df   Vasily Tarasov   [PATCH] block lay...
137
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
141
142
143
144
145
146
  }
  
  static void elevator_put(struct elevator_type *e)
  {
  	module_put(e->elevator_owner);
  }
  
  static struct elevator_type *elevator_get(const char *name)
  {
2824bc932   Tejun Heo   [PATCH] fix try_m...
147
  	struct elevator_type *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148

2a12dcd71   Jens Axboe   [PATCH] elevator:...
149
  	spin_lock(&elv_list_lock);
2824bc932   Tejun Heo   [PATCH] fix try_m...
150
151
  
  	e = elevator_find(name);
e16409496   Jens Axboe   elevator: make el...
152
153
154
155
  	if (!e) {
  		char elv[ELV_NAME_MAX + strlen("-iosched")];
  
  		spin_unlock(&elv_list_lock);
a506aedc5   wzt.wzt@gmail.com   Block: Fix block/...
156
  		snprintf(elv, sizeof(elv), "%s-iosched", name);
e16409496   Jens Axboe   elevator: make el...
157

e180f5949   maximilian attems   block: request_mo...
158
  		request_module("%s", elv);
e16409496   Jens Axboe   elevator: make el...
159
160
161
  		spin_lock(&elv_list_lock);
  		e = elevator_find(name);
  	}
2824bc932   Tejun Heo   [PATCH] fix try_m...
162
163
  	if (e && !try_module_get(e->elevator_owner))
  		e = NULL;
2a12dcd71   Jens Axboe   [PATCH] elevator:...
164
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
  
  	return e;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
168
169
  static void *elevator_init_queue(struct request_queue *q,
  				 struct elevator_queue *eq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  {
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
171
  	return eq->ops->elevator_init_fn(q);
bc1c11697   Jens Axboe   [PATCH] elevator ...
172
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173

165125e1e   Jens Axboe   [BLOCK] Get rid o...
174
  static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
bc1c11697   Jens Axboe   [PATCH] elevator ...
175
176
  			   void *data)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  	q->elevator = eq;
bc1c11697   Jens Axboe   [PATCH] elevator ...
178
  	eq->elevator_data = data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
  }
  
  static char chosen_elevator[16];
5f0039764   Nate Diller   [BLOCK] elevator:...
182
  static int __init elevator_setup(char *str)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  {
752a3b796   Chuck Ebbert   [BLOCK] elevator:...
184
185
186
187
  	/*
  	 * Be backwards-compatible with previous kernels, so users
  	 * won't get the wrong elevator.
  	 */
492af6350   Jens Axboe   block: remove the...
188
  	strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
9b41046cd   OGAWA Hirofumi   [PATCH] Don't pas...
189
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
  }
  
  __setup("elevator=", elevator_setup);
3d1ab40f4   Al Viro   [PATCH] elevator_...
193
  static struct kobj_type elv_ktype;
b374d18a4   Jens Axboe   block: get rid of...
194
  static struct elevator_queue *elevator_alloc(struct request_queue *q,
165125e1e   Jens Axboe   [BLOCK] Get rid o...
195
  				  struct elevator_type *e)
3d1ab40f4   Al Viro   [PATCH] elevator_...
196
  {
b374d18a4   Jens Axboe   block: get rid of...
197
  	struct elevator_queue *eq;
9817064b6   Jens Axboe   [PATCH] elevator:...
198
  	int i;
b374d18a4   Jens Axboe   block: get rid of...
199
  	eq = kmalloc_node(sizeof(*eq), GFP_KERNEL | __GFP_ZERO, q->node);
9817064b6   Jens Axboe   [PATCH] elevator:...
200
201
  	if (unlikely(!eq))
  		goto err;
9817064b6   Jens Axboe   [PATCH] elevator:...
202
203
  	eq->ops = &e->ops;
  	eq->elevator_type = e;
f9cb074bf   Greg Kroah-Hartman   Kobject: rename k...
204
  	kobject_init(&eq->kobj, &elv_ktype);
9817064b6   Jens Axboe   [PATCH] elevator:...
205
  	mutex_init(&eq->sysfs_lock);
b5deef901   Jens Axboe   [PATCH] Make sure...
206
207
  	eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
  					GFP_KERNEL, q->node);
9817064b6   Jens Axboe   [PATCH] elevator:...
208
209
210
211
212
  	if (!eq->hash)
  		goto err;
  
  	for (i = 0; i < ELV_HASH_ENTRIES; i++)
  		INIT_HLIST_HEAD(&eq->hash[i]);
3d1ab40f4   Al Viro   [PATCH] elevator_...
213
  	return eq;
9817064b6   Jens Axboe   [PATCH] elevator:...
214
215
216
217
  err:
  	kfree(eq);
  	elevator_put(e);
  	return NULL;
3d1ab40f4   Al Viro   [PATCH] elevator_...
218
219
220
221
  }
  
  static void elevator_release(struct kobject *kobj)
  {
b374d18a4   Jens Axboe   block: get rid of...
222
  	struct elevator_queue *e;
9817064b6   Jens Axboe   [PATCH] elevator:...
223

b374d18a4   Jens Axboe   block: get rid of...
224
  	e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f4   Al Viro   [PATCH] elevator_...
225
  	elevator_put(e->elevator_type);
9817064b6   Jens Axboe   [PATCH] elevator:...
226
  	kfree(e->hash);
3d1ab40f4   Al Viro   [PATCH] elevator_...
227
228
  	kfree(e);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
229
  int elevator_init(struct request_queue *q, char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
  {
  	struct elevator_type *e = NULL;
  	struct elevator_queue *eq;
bc1c11697   Jens Axboe   [PATCH] elevator ...
233
  	void *data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234

1abec4fdb   Mike Snitzer   block: make blk_i...
235
236
  	if (unlikely(q->elevator))
  		return 0;
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
237
238
239
240
  	INIT_LIST_HEAD(&q->queue_head);
  	q->last_merge = NULL;
  	q->end_sector = 0;
  	q->boundary_rq = NULL;
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
241

4eb166d98   Jens Axboe   block: make eleva...
242
243
244
245
246
  	if (name) {
  		e = elevator_get(name);
  		if (!e)
  			return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247

4eb166d98   Jens Axboe   block: make eleva...
248
249
250
251
252
253
254
  	if (!e && *chosen_elevator) {
  		e = elevator_get(chosen_elevator);
  		if (!e)
  			printk(KERN_ERR "I/O scheduler %s not found
  ",
  							chosen_elevator);
  	}
248d5ca5e   Nate Diller   [BLOCK] elevator:...
255

4eb166d98   Jens Axboe   block: make eleva...
256
257
258
259
260
261
262
263
264
  	if (!e) {
  		e = elevator_get(CONFIG_DEFAULT_IOSCHED);
  		if (!e) {
  			printk(KERN_ERR
  				"Default I/O scheduler not found. " \
  				"Using noop.
  ");
  			e = elevator_get("noop");
  		}
5f0039764   Nate Diller   [BLOCK] elevator:...
265
  	}
b5deef901   Jens Axboe   [PATCH] Make sure...
266
  	eq = elevator_alloc(q, e);
3d1ab40f4   Al Viro   [PATCH] elevator_...
267
  	if (!eq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269

bc1c11697   Jens Axboe   [PATCH] elevator ...
270
271
  	data = elevator_init_queue(q, eq);
  	if (!data) {
3d1ab40f4   Al Viro   [PATCH] elevator_...
272
  		kobject_put(&eq->kobj);
bc1c11697   Jens Axboe   [PATCH] elevator ...
273
274
  		return -ENOMEM;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275

bc1c11697   Jens Axboe   [PATCH] elevator ...
276
  	elevator_attach(q, eq, data);
1abec4fdb   Mike Snitzer   block: make blk_i...
277
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
279
  EXPORT_SYMBOL(elevator_init);
b374d18a4   Jens Axboe   block: get rid of...
280
  void elevator_exit(struct elevator_queue *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
282
  	mutex_lock(&e->sysfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
  	if (e->ops->elevator_exit_fn)
  		e->ops->elevator_exit_fn(e);
3d1ab40f4   Al Viro   [PATCH] elevator_...
285
286
  	e->ops = NULL;
  	mutex_unlock(&e->sysfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287

3d1ab40f4   Al Viro   [PATCH] elevator_...
288
  	kobject_put(&e->kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
290
  EXPORT_SYMBOL(elevator_exit);
9817064b6   Jens Axboe   [PATCH] elevator:...
291
292
293
294
  static inline void __elv_rqhash_del(struct request *rq)
  {
  	hlist_del_init(&rq->hash);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
295
  static void elv_rqhash_del(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
296
297
298
299
  {
  	if (ELV_ON_HASH(rq))
  		__elv_rqhash_del(rq);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
300
  static void elv_rqhash_add(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
301
  {
b374d18a4   Jens Axboe   block: get rid of...
302
  	struct elevator_queue *e = q->elevator;
9817064b6   Jens Axboe   [PATCH] elevator:...
303
304
305
306
  
  	BUG_ON(ELV_ON_HASH(rq));
  	hlist_add_head(&rq->hash, &e->hash[ELV_HASH_FN(rq_hash_key(rq))]);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
307
  static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
308
309
310
311
  {
  	__elv_rqhash_del(rq);
  	elv_rqhash_add(q, rq);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
312
  static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
9817064b6   Jens Axboe   [PATCH] elevator:...
313
  {
b374d18a4   Jens Axboe   block: get rid of...
314
  	struct elevator_queue *e = q->elevator;
9817064b6   Jens Axboe   [PATCH] elevator:...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  	struct hlist_head *hash_list = &e->hash[ELV_HASH_FN(offset)];
  	struct hlist_node *entry, *next;
  	struct request *rq;
  
  	hlist_for_each_entry_safe(rq, entry, next, hash_list, hash) {
  		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...
333
  /*
2e662b65f   Jens Axboe   [PATCH] elevator:...
334
335
336
337
338
339
340
341
342
343
344
345
   * RB-tree support functions for inserting/lookup/removal of requests
   * in a sorted RB tree.
   */
  struct request *elv_rb_add(struct rb_root *root, struct request *rq)
  {
  	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...
346
  		if (blk_rq_pos(rq) < blk_rq_pos(__rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
347
  			p = &(*p)->rb_left;
83096ebf1   Tejun Heo   block: convert to...
348
  		else if (blk_rq_pos(rq) > blk_rq_pos(__rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
349
350
351
352
353
354
355
356
357
  			p = &(*p)->rb_right;
  		else
  			return __rq;
  	}
  
  	rb_link_node(&rq->rb_node, parent, p);
  	rb_insert_color(&rq->rb_node, root);
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
358
359
360
361
362
363
364
365
  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:...
366
367
368
369
370
371
372
373
374
  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...
375
  		if (sector < blk_rq_pos(rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
376
  			n = n->rb_left;
83096ebf1   Tejun Heo   block: convert to...
377
  		else if (sector > blk_rq_pos(rq))
2e662b65f   Jens Axboe   [PATCH] elevator:...
378
379
380
381
382
383
384
  			n = n->rb_right;
  		else
  			return rq;
  	}
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
385
386
387
  EXPORT_SYMBOL(elv_rb_find);
  
  /*
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
388
   * Insert rq into dispatch queue of q.  Queue lock must be held on
dbe7f76dd   Uwe Kleine-König   fix typo "insted"...
389
   * entry.  rq is sort instead into the dispatch queue. To be used by
2e662b65f   Jens Axboe   [PATCH] elevator:...
390
   * specific elevators.
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
391
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
392
  void elv_dispatch_sort(struct request_queue *q, struct request *rq)
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
393
394
  {
  	sector_t boundary;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
395
  	struct list_head *entry;
4eb166d98   Jens Axboe   block: make eleva...
396
  	int stop_flags;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
397

06b86245c   Tejun Heo   [PATCH] 03/05 mov...
398
399
  	if (q->last_merge == rq)
  		q->last_merge = NULL;
9817064b6   Jens Axboe   [PATCH] elevator:...
400
401
  
  	elv_rqhash_del(q, rq);
15853af9f   Tejun Heo   [BLOCK] Implement...
402
  	q->nr_sorted--;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
403

1b47f531e   Jens Axboe   [PATCH] generic d...
404
  	boundary = q->end_sector;
02e031cbc   Christoph Hellwig   block: remove REQ...
405
  	stop_flags = REQ_SOFTBARRIER | REQ_STARTED;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
406
407
  	list_for_each_prev(entry, &q->queue_head) {
  		struct request *pos = list_entry_rq(entry);
33659ebba   Christoph Hellwig   block: remove wra...
408
409
  		if ((rq->cmd_flags & REQ_DISCARD) !=
  		    (pos->cmd_flags & REQ_DISCARD))
e17fc0a1c   David Woodhouse   Allow elevators t...
410
  			break;
783660b2f   Jens Axboe   elevator: don't s...
411
412
  		if (rq_data_dir(rq) != rq_data_dir(pos))
  			break;
4eb166d98   Jens Axboe   block: make eleva...
413
  		if (pos->cmd_flags & stop_flags)
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
414
  			break;
83096ebf1   Tejun Heo   block: convert to...
415
416
  		if (blk_rq_pos(rq) >= boundary) {
  			if (blk_rq_pos(pos) < boundary)
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
417
418
  				continue;
  		} else {
83096ebf1   Tejun Heo   block: convert to...
419
  			if (blk_rq_pos(pos) >= boundary)
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
420
421
  				break;
  		}
83096ebf1   Tejun Heo   block: convert to...
422
  		if (blk_rq_pos(rq) >= blk_rq_pos(pos))
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
423
424
425
426
427
  			break;
  	}
  
  	list_add(&rq->queuelist, entry);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
428
  EXPORT_SYMBOL(elv_dispatch_sort);
9817064b6   Jens Axboe   [PATCH] elevator:...
429
  /*
2e662b65f   Jens Axboe   [PATCH] elevator:...
430
431
432
   * Insert rq into dispatch queue of q.  Queue lock must be held on
   * entry.  rq is added to the back of the dispatch queue. To be used by
   * specific elevators.
9817064b6   Jens Axboe   [PATCH] elevator:...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
   */
  void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
  {
  	if (q->last_merge == rq)
  		q->last_merge = NULL;
  
  	elv_rqhash_del(q, rq);
  
  	q->nr_sorted--;
  
  	q->end_sector = rq_end_sector(rq);
  	q->boundary_rq = rq;
  	list_add_tail(&rq->queuelist, &q->queue_head);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
447
  EXPORT_SYMBOL(elv_dispatch_add_tail);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
448
  int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  {
b374d18a4   Jens Axboe   block: get rid of...
450
  	struct elevator_queue *e = q->elevator;
9817064b6   Jens Axboe   [PATCH] elevator:...
451
  	struct request *__rq;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
452
  	int ret;
9817064b6   Jens Axboe   [PATCH] elevator:...
453
  	/*
488991e28   Alan D. Brunelle   block: Added in s...
454
455
456
457
458
459
460
461
462
  	 * Levels of merges:
  	 * 	nomerges:  No merges at all attempted
  	 * 	noxmerges: Only simple one-hit cache try
  	 * 	merges:	   All merge tries attempted
  	 */
  	if (blk_queue_nomerges(q))
  		return ELEVATOR_NO_MERGE;
  
  	/*
9817064b6   Jens Axboe   [PATCH] elevator:...
463
464
  	 * First try one-hit cache.
  	 */
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
465
466
467
468
469
470
471
  	if (q->last_merge) {
  		ret = elv_try_merge(q->last_merge, bio);
  		if (ret != ELEVATOR_NO_MERGE) {
  			*req = q->last_merge;
  			return ret;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472

488991e28   Alan D. Brunelle   block: Added in s...
473
  	if (blk_queue_noxmerges(q))
ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
474
  		return ELEVATOR_NO_MERGE;
9817064b6   Jens Axboe   [PATCH] elevator:...
475
476
477
478
479
480
481
482
  	/*
  	 * See if our hash lookup can find a potential backmerge.
  	 */
  	__rq = elv_rqhash_find(q, bio->bi_sector);
  	if (__rq && elv_rq_merge_ok(__rq, bio)) {
  		*req = __rq;
  		return ELEVATOR_BACK_MERGE;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
484
485
486
487
  	if (e->ops->elevator_merge_fn)
  		return e->ops->elevator_merge_fn(q, req, bio);
  
  	return ELEVATOR_NO_MERGE;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
488
  void elv_merged_request(struct request_queue *q, struct request *rq, int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
  {
b374d18a4   Jens Axboe   block: get rid of...
490
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
  
  	if (e->ops->elevator_merged_fn)
2e662b65f   Jens Axboe   [PATCH] elevator:...
493
  		e->ops->elevator_merged_fn(q, rq, type);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
494

2e662b65f   Jens Axboe   [PATCH] elevator:...
495
496
  	if (type == ELEVATOR_BACK_MERGE)
  		elv_rqhash_reposition(q, rq);
9817064b6   Jens Axboe   [PATCH] elevator:...
497

06b86245c   Tejun Heo   [PATCH] 03/05 mov...
498
  	q->last_merge = rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
500
  void elv_merge_requests(struct request_queue *q, struct request *rq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
502
  			     struct request *next)
  {
b374d18a4   Jens Axboe   block: get rid of...
503
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
  	if (e->ops->elevator_merge_req_fn)
  		e->ops->elevator_merge_req_fn(q, rq, next);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
507

9817064b6   Jens Axboe   [PATCH] elevator:...
508
509
510
511
  	elv_rqhash_reposition(q, rq);
  	elv_rqhash_del(q, next);
  
  	q->nr_sorted--;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
512
  	q->last_merge = rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  }
812d40264   Divyesh Shah   blkio: Add io_mer...
514
515
516
517
518
519
520
521
  void elv_bio_merged(struct request_queue *q, struct request *rq,
  			struct bio *bio)
  {
  	struct elevator_queue *e = q->elevator;
  
  	if (e->ops->elevator_bio_merged_fn)
  		e->ops->elevator_bio_merged_fn(q, rq, bio);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
522
  void elv_requeue_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
523
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
525
526
527
  	/*
  	 * it already went through dequeue, we need to decrement the
  	 * in_flight count again
  	 */
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
528
  	if (blk_account_rq(rq)) {
0a7ae2ff0   Jens Axboe   block: change the...
529
  		q->in_flight[rq_is_sync(rq)]--;
33659ebba   Christoph Hellwig   block: remove wra...
530
  		if (rq->cmd_flags & REQ_SORTED)
cad975164   Jens Axboe   elevator: abstrac...
531
  			elv_deactivate_rq(q, rq);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
532
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
533

4aff5e233   Jens Axboe   [PATCH] Split str...
534
  	rq->cmd_flags &= ~REQ_STARTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535

30e9656cc   Tejun Heo   [PATCH] block: im...
536
  	elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
  }
26308eab6   Jerome Marchand   block: fix incons...
538
  void elv_drain_elevator(struct request_queue *q)
15853af9f   Tejun Heo   [BLOCK] Implement...
539
540
541
542
543
544
545
546
547
548
549
550
551
  {
  	static int printed;
  	while (q->elevator->ops->elevator_dispatch_fn(q, 1))
  		;
  	if (q->nr_sorted == 0)
  		return;
  	if (printed++ < 10) {
  		printk(KERN_ERR "%s: forced dispatching is broken "
  		       "(nr_sorted=%u), please report this
  ",
  		       q->elevator->elevator_type->elevator_name, q->nr_sorted);
  	}
  }
6c7e8cee6   Jens Axboe   block: elevator q...
552
553
554
  /*
   * Call with queue lock held, interrupts disabled
   */
f600abe2d   Jens Axboe   block: fix bad sp...
555
  void elv_quiesce_start(struct request_queue *q)
6c7e8cee6   Jens Axboe   block: elevator q...
556
  {
cd43e26f0   Martin K. Petersen   block: Expose sta...
557
558
  	if (!q->elevator)
  		return;
6c7e8cee6   Jens Axboe   block: elevator q...
559
560
561
562
563
564
565
  	queue_flag_set(QUEUE_FLAG_ELVSWITCH, q);
  
  	/*
  	 * make sure we don't have any requests in flight
  	 */
  	elv_drain_elevator(q);
  	while (q->rq.elvpriv) {
a7f557923   Tejun Heo   block: kill blk_s...
566
  		__blk_run_queue(q);
6c7e8cee6   Jens Axboe   block: elevator q...
567
568
569
570
571
572
  		spin_unlock_irq(q->queue_lock);
  		msleep(10);
  		spin_lock_irq(q->queue_lock);
  		elv_drain_elevator(q);
  	}
  }
f600abe2d   Jens Axboe   block: fix bad sp...
573
  void elv_quiesce_end(struct request_queue *q)
6c7e8cee6   Jens Axboe   block: elevator q...
574
575
576
  {
  	queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
577
  void elv_insert(struct request_queue *q, struct request *rq, int where)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
  {
dac07ec12   Jens Axboe   [BLOCK] limit req...
579
  	int unplug_it = 1;
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
580

5f3ea37c7   Arnaldo Carvalho de Melo   blktrace: port to...
581
  	trace_block_rq_insert(q, rq);
2056a782f   Jens Axboe   [PATCH] Block que...
582

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
583
  	rq->q = q;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
584
  	switch (where) {
28e7d1845   Tejun Heo   block: drop barri...
585
586
587
588
589
590
591
  	case ELEVATOR_INSERT_REQUEUE:
  		/*
  		 * Most requeues happen because of a busy condition,
  		 * don't force unplug of the queue for that case.
  		 * Clear unplug_it and fall through.
  		 */
  		unplug_it = 0;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
592
  	case ELEVATOR_INSERT_FRONT:
4aff5e233   Jens Axboe   [PATCH] Split str...
593
  		rq->cmd_flags |= REQ_SOFTBARRIER;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
594
595
596
597
  		list_add(&rq->queuelist, &q->queue_head);
  		break;
  
  	case ELEVATOR_INSERT_BACK:
4aff5e233   Jens Axboe   [PATCH] Split str...
598
  		rq->cmd_flags |= REQ_SOFTBARRIER;
15853af9f   Tejun Heo   [BLOCK] Implement...
599
  		elv_drain_elevator(q);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
600
601
602
603
604
605
606
607
608
609
610
  		list_add_tail(&rq->queuelist, &q->queue_head);
  		/*
  		 * We kick the queue here for the following reasons.
  		 * - The elevator might have returned NULL previously
  		 *   to delay requests and returned them now.  As the
  		 *   queue wasn't empty before this request, ll_rw_blk
  		 *   won't run the queue on return, resulting in hang.
  		 * - Usually, back inserted requests won't be merged
  		 *   with anything.  There's no point in delaying queue
  		 *   processing.
  		 */
a7f557923   Tejun Heo   block: kill blk_s...
611
  		__blk_run_queue(q);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
612
613
614
  		break;
  
  	case ELEVATOR_INSERT_SORT:
33659ebba   Christoph Hellwig   block: remove wra...
615
616
  		BUG_ON(rq->cmd_type != REQ_TYPE_FS &&
  		       !(rq->cmd_flags & REQ_DISCARD));
4aff5e233   Jens Axboe   [PATCH] Split str...
617
  		rq->cmd_flags |= REQ_SORTED;
15853af9f   Tejun Heo   [BLOCK] Implement...
618
  		q->nr_sorted++;
9817064b6   Jens Axboe   [PATCH] elevator:...
619
620
621
622
623
  		if (rq_mergeable(rq)) {
  			elv_rqhash_add(q, rq);
  			if (!q->last_merge)
  				q->last_merge = rq;
  		}
ca23509fb   Tejun Heo   [PATCH] blk: fix ...
624
625
626
627
628
629
  		/*
  		 * Some ioscheds (cfq) run q->request_fn directly, so
  		 * rq cannot be accessed after calling
  		 * elevator_add_req_fn.
  		 */
  		q->elevator->ops->elevator_add_req_fn(q, rq);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
630
631
632
633
634
  		break;
  
  	default:
  		printk(KERN_ERR "%s: bad insertion point %d
  ",
24c03d47d   Harvey Harrison   block: remove rem...
635
  		       __func__, where);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
636
637
  		BUG();
  	}
dac07ec12   Jens Axboe   [BLOCK] limit req...
638
  	if (unplug_it && blk_queue_plugged(q)) {
1faa16d22   Jens Axboe   block: change the...
639
  		int nrq = q->rq.count[BLK_RW_SYNC] + q->rq.count[BLK_RW_ASYNC]
0a7ae2ff0   Jens Axboe   block: change the...
640
  				- queue_in_flight(q);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
641
642
643
644
  
  		if (nrq >= q->unplug_thresh)
  			__generic_unplug_device(q);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
645
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
646
  void __elv_add_request(struct request_queue *q, struct request *rq, int where,
30e9656cc   Tejun Heo   [PATCH] block: im...
647
648
  		       int plug)
  {
02e031cbc   Christoph Hellwig   block: remove REQ...
649
  	if (rq->cmd_flags & REQ_SOFTBARRIER) {
28e7d1845   Tejun Heo   block: drop barri...
650
  		/* barriers are scheduling boundary, update end_sector */
33659ebba   Christoph Hellwig   block: remove wra...
651
652
  		if (rq->cmd_type == REQ_TYPE_FS ||
  		    (rq->cmd_flags & REQ_DISCARD)) {
30e9656cc   Tejun Heo   [PATCH] block: im...
653
654
655
  			q->end_sector = rq_end_sector(rq);
  			q->boundary_rq = rq;
  		}
4eb166d98   Jens Axboe   block: make eleva...
656
657
  	} else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
  		    where == ELEVATOR_INSERT_SORT)
30e9656cc   Tejun Heo   [PATCH] block: im...
658
659
660
661
662
663
664
  		where = ELEVATOR_INSERT_BACK;
  
  	if (plug)
  		blk_plug_device(q);
  
  	elv_insert(q, rq, where);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
665
  EXPORT_SYMBOL(__elv_add_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
666
  void elv_add_request(struct request_queue *q, struct request *rq, int where,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667
668
669
670
671
672
673
674
  		     int plug)
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(q->queue_lock, flags);
  	__elv_add_request(q, rq, where, plug);
  	spin_unlock_irqrestore(q->queue_lock, flags);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
675
  EXPORT_SYMBOL(elv_add_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
676
  int elv_queue_empty(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
  {
b374d18a4   Jens Axboe   block: get rid of...
678
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679

8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
680
681
  	if (!list_empty(&q->queue_head))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
683
  	if (e->ops->elevator_queue_empty_fn)
  		return e->ops->elevator_queue_empty_fn(q);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
684
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
685
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
686
  EXPORT_SYMBOL(elv_queue_empty);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
687
  struct request *elv_latter_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
  {
b374d18a4   Jens Axboe   block: get rid of...
689
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
692
  
  	if (e->ops->elevator_latter_req_fn)
  		return e->ops->elevator_latter_req_fn(q, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
694
  	return NULL;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
695
  struct request *elv_former_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  {
b374d18a4   Jens Axboe   block: get rid of...
697
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698
699
700
  
  	if (e->ops->elevator_former_req_fn)
  		return e->ops->elevator_former_req_fn(q, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
  	return NULL;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
703
  int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
  {
b374d18a4   Jens Axboe   block: get rid of...
705
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
  
  	if (e->ops->elevator_set_req_fn)
cb78b285c   Jens Axboe   [PATCH] Drop usel...
708
  		return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
712
  
  	rq->elevator_private = NULL;
  	return 0;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
713
  void elv_put_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
  {
b374d18a4   Jens Axboe   block: get rid of...
715
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
717
  
  	if (e->ops->elevator_put_req_fn)
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
718
  		e->ops->elevator_put_req_fn(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
720
  int elv_may_queue(struct request_queue *q, int rw)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
  {
b374d18a4   Jens Axboe   block: get rid of...
722
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
724
  
  	if (e->ops->elevator_may_queue_fn)
cb78b285c   Jens Axboe   [PATCH] Drop usel...
725
  		return e->ops->elevator_may_queue_fn(q, rw);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
  
  	return ELV_MQUEUE_MAY;
  }
11914a53d   Mike Anderson   block: Add interf...
729
730
731
732
733
734
735
  void elv_abort_queue(struct request_queue *q)
  {
  	struct request *rq;
  
  	while (!list_empty(&q->queue_head)) {
  		rq = list_entry_rq(q->queue_head.next);
  		rq->cmd_flags |= REQ_QUIET;
5f3ea37c7   Arnaldo Carvalho de Melo   blktrace: port to...
736
  		trace_block_rq_abort(q, rq);
53c663ce0   Kiyoshi Ueda   block: fix a poss...
737
738
739
740
741
  		/*
  		 * Mark this request as started so we don't trigger
  		 * any debug logic in the end I/O path.
  		 */
  		blk_start_request(rq);
40cbbb781   Tejun Heo   block: implement ...
742
  		__blk_end_request_all(rq, -EIO);
11914a53d   Mike Anderson   block: Add interf...
743
744
745
  	}
  }
  EXPORT_SYMBOL(elv_abort_queue);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
746
  void elv_completed_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
  {
b374d18a4   Jens Axboe   block: get rid of...
748
  	struct elevator_queue *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
750
751
752
  
  	/*
  	 * request is released from the driver, io must be done
  	 */
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
753
  	if (blk_account_rq(rq)) {
0a7ae2ff0   Jens Axboe   block: change the...
754
  		q->in_flight[rq_is_sync(rq)]--;
33659ebba   Christoph Hellwig   block: remove wra...
755
756
  		if ((rq->cmd_flags & REQ_SORTED) &&
  		    e->ops->elevator_completed_req_fn)
1bc691d35   Tejun Heo   [PATCH] fix queue...
757
758
  			e->ops->elevator_completed_req_fn(q, rq);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
  }
3d1ab40f4   Al Viro   [PATCH] elevator_...
760
761
762
763
  #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
764
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
765
  	struct elv_fs_entry *entry = to_elv(attr);
b374d18a4   Jens Axboe   block: get rid of...
766
  	struct elevator_queue *e;
3d1ab40f4   Al Viro   [PATCH] elevator_...
767
768
769
770
  	ssize_t error;
  
  	if (!entry->show)
  		return -EIO;
b374d18a4   Jens Axboe   block: get rid of...
771
  	e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f4   Al Viro   [PATCH] elevator_...
772
773
774
775
776
  	mutex_lock(&e->sysfs_lock);
  	error = e->ops ? entry->show(e, page) : -ENOENT;
  	mutex_unlock(&e->sysfs_lock);
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777

3d1ab40f4   Al Viro   [PATCH] elevator_...
778
779
780
781
  static ssize_t
  elv_attr_store(struct kobject *kobj, struct attribute *attr,
  	       const char *page, size_t length)
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
782
  	struct elv_fs_entry *entry = to_elv(attr);
b374d18a4   Jens Axboe   block: get rid of...
783
  	struct elevator_queue *e;
3d1ab40f4   Al Viro   [PATCH] elevator_...
784
  	ssize_t error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785

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

b374d18a4   Jens Axboe   block: get rid of...
789
  	e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f4   Al Viro   [PATCH] elevator_...
790
791
792
793
794
  	mutex_lock(&e->sysfs_lock);
  	error = e->ops ? entry->store(e, page, length) : -ENOENT;
  	mutex_unlock(&e->sysfs_lock);
  	return error;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
795
  static const struct sysfs_ops elv_sysfs_ops = {
3d1ab40f4   Al Viro   [PATCH] elevator_...
796
797
798
799
800
801
802
803
804
805
806
  	.show	= elv_attr_show,
  	.store	= elv_attr_store,
  };
  
  static struct kobj_type elv_ktype = {
  	.sysfs_ops	= &elv_sysfs_ops,
  	.release	= elevator_release,
  };
  
  int elv_register_queue(struct request_queue *q)
  {
b374d18a4   Jens Axboe   block: get rid of...
807
  	struct elevator_queue *e = q->elevator;
3d1ab40f4   Al Viro   [PATCH] elevator_...
808
  	int error;
b2d6db587   Greg Kroah-Hartman   Kobject: rename k...
809
  	error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
3d1ab40f4   Al Viro   [PATCH] elevator_...
810
  	if (!error) {
e572ec7e4   Al Viro   [PATCH] fix rmmod...
811
  		struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
3d1ab40f4   Al Viro   [PATCH] elevator_...
812
  		if (attr) {
e572ec7e4   Al Viro   [PATCH] fix rmmod...
813
814
  			while (attr->attr.name) {
  				if (sysfs_create_file(&e->kobj, &attr->attr))
3d1ab40f4   Al Viro   [PATCH] elevator_...
815
  					break;
e572ec7e4   Al Viro   [PATCH] fix rmmod...
816
  				attr++;
3d1ab40f4   Al Viro   [PATCH] elevator_...
817
818
819
  			}
  		}
  		kobject_uevent(&e->kobj, KOBJ_ADD);
430c62fb2   Jens Axboe   elevator: fix oop...
820
  		e->registered = 1;
3d1ab40f4   Al Viro   [PATCH] elevator_...
821
822
  	}
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
  }
01effb0dc   Mike Snitzer   block: allow init...
824
  EXPORT_SYMBOL(elv_register_queue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825

b374d18a4   Jens Axboe   block: get rid of...
826
  static void __elv_unregister_queue(struct elevator_queue *e)
bc1c11697   Jens Axboe   [PATCH] elevator ...
827
828
829
  {
  	kobject_uevent(&e->kobj, KOBJ_REMOVE);
  	kobject_del(&e->kobj);
430c62fb2   Jens Axboe   elevator: fix oop...
830
  	e->registered = 0;
bc1c11697   Jens Axboe   [PATCH] elevator ...
831
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
833
  void elv_unregister_queue(struct request_queue *q)
  {
bc1c11697   Jens Axboe   [PATCH] elevator ...
834
835
  	if (q)
  		__elv_unregister_queue(q->elevator);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
836
  }
01effb0dc   Mike Snitzer   block: allow init...
837
  EXPORT_SYMBOL(elv_unregister_queue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
838

2fdd82bd8   Adrian Bunk   block: let elv_re...
839
  void elv_register(struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840
  {
1ffb96c58   Thibaut VARENE   make elv_register...
841
  	char *def = "";
2a12dcd71   Jens Axboe   [PATCH] elevator:...
842
843
  
  	spin_lock(&elv_list_lock);
ce5244974   Eric Sesterhenn   BUG_ON() Conversi...
844
  	BUG_ON(elevator_find(e->elevator_name));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
  	list_add_tail(&e->list, &elv_list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
846
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847

5f0039764   Nate Diller   [BLOCK] elevator:...
848
849
850
  	if (!strcmp(e->elevator_name, chosen_elevator) ||
  			(!*chosen_elevator &&
  			 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
1ffb96c58   Thibaut VARENE   make elv_register...
851
  				def = " (default)";
4eb166d98   Jens Axboe   block: make eleva...
852
853
854
  	printk(KERN_INFO "io scheduler %s registered%s
  ", e->elevator_name,
  								def);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
857
858
859
  }
  EXPORT_SYMBOL_GPL(elv_register);
  
  void elv_unregister(struct elevator_type *e)
  {
83521d3eb   Christoph Hellwig   [PATCH] cfq-iosch...
860
861
862
863
864
  	struct task_struct *g, *p;
  
  	/*
  	 * Iterate every thread in the process to remove the io contexts.
  	 */
e17a9489b   Al Viro   [PATCH] stop elv_...
865
866
867
868
  	if (e->ops.trim) {
  		read_lock(&tasklist_lock);
  		do_each_thread(g, p) {
  			task_lock(p);
2d8f61316   Oleg Nesterov   elv_unregister: f...
869
870
  			if (p->io_context)
  				e->ops.trim(p->io_context);
e17a9489b   Al Viro   [PATCH] stop elv_...
871
872
873
874
  			task_unlock(p);
  		} while_each_thread(g, p);
  		read_unlock(&tasklist_lock);
  	}
83521d3eb   Christoph Hellwig   [PATCH] cfq-iosch...
875

2a12dcd71   Jens Axboe   [PATCH] elevator:...
876
  	spin_lock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
877
  	list_del_init(&e->list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
878
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
879
880
881
882
883
884
885
  }
  EXPORT_SYMBOL_GPL(elv_unregister);
  
  /*
   * 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...
886
   * one, if the new one fails init for some reason.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
888
  static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
  {
b374d18a4   Jens Axboe   block: get rid of...
890
  	struct elevator_queue *old_elevator, *e;
bc1c11697   Jens Axboe   [PATCH] elevator ...
891
  	void *data;
5dd531a03   Jens Axboe   block: add functi...
892
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893

cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
894
895
896
  	/*
  	 * Allocate new elevator
  	 */
b5deef901   Jens Axboe   [PATCH] Make sure...
897
  	e = elevator_alloc(q, new_e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
  	if (!e)
5dd531a03   Jens Axboe   block: add functi...
899
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900

bc1c11697   Jens Axboe   [PATCH] elevator ...
901
902
903
  	data = elevator_init_queue(q, e);
  	if (!data) {
  		kobject_put(&e->kobj);
5dd531a03   Jens Axboe   block: add functi...
904
  		return -ENOMEM;
bc1c11697   Jens Axboe   [PATCH] elevator ...
905
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
  	/*
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
907
  	 * Turn on BYPASS and drain all requests w/ elevator private data
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
  	 */
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
909
  	spin_lock_irq(q->queue_lock);
f600abe2d   Jens Axboe   block: fix bad sp...
910
  	elv_quiesce_start(q);
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
911

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
  	/*
bc1c11697   Jens Axboe   [PATCH] elevator ...
913
  	 * Remember old elevator.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915
916
917
  	old_elevator = q->elevator;
  
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
918
919
  	 * attach and start new elevator
  	 */
bc1c11697   Jens Axboe   [PATCH] elevator ...
920
921
922
  	elevator_attach(q, e, data);
  
  	spin_unlock_irq(q->queue_lock);
430c62fb2   Jens Axboe   elevator: fix oop...
923
924
  	if (old_elevator->registered) {
  		__elv_unregister_queue(old_elevator);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
925

430c62fb2   Jens Axboe   elevator: fix oop...
926
927
928
929
  		err = elv_register_queue(q);
  		if (err)
  			goto fail_register;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930
931
  
  	/*
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
932
  	 * finally exit old elevator and turn off BYPASS.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
933
934
  	 */
  	elevator_exit(old_elevator);
75ad23bc0   Nick Piggin   block: make queue...
935
  	spin_lock_irq(q->queue_lock);
f600abe2d   Jens Axboe   block: fix bad sp...
936
  	elv_quiesce_end(q);
75ad23bc0   Nick Piggin   block: make queue...
937
  	spin_unlock_irq(q->queue_lock);
4722dc52a   Alan D. Brunelle   Added in elevator...
938
  	blk_add_trace_msg(q, "elv switch: %s", e->elevator_type->elevator_name);
5dd531a03   Jens Axboe   block: add functi...
939
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
940
941
942
943
944
945
946
  
  fail_register:
  	/*
  	 * switch failed, exit the new io scheduler and reattach the old
  	 * one again (along with re-adding the sysfs dir)
  	 */
  	elevator_exit(e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
947
948
  	q->elevator = old_elevator;
  	elv_register_queue(q);
75ad23bc0   Nick Piggin   block: make queue...
949
950
951
952
  
  	spin_lock_irq(q->queue_lock);
  	queue_flag_clear(QUEUE_FLAG_ELVSWITCH, q);
  	spin_unlock_irq(q->queue_lock);
5dd531a03   Jens Axboe   block: add functi...
953
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
954
  }
5dd531a03   Jens Axboe   block: add functi...
955
956
957
958
  /*
   * Switch this queue to the given IO scheduler.
   */
  int elevator_change(struct request_queue *q, const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
960
961
  {
  	char elevator_name[ELV_NAME_MAX];
  	struct elevator_type *e;
cd43e26f0   Martin K. Petersen   block: Expose sta...
962
  	if (!q->elevator)
5dd531a03   Jens Axboe   block: add functi...
963
  		return -ENXIO;
cd43e26f0   Martin K. Petersen   block: Expose sta...
964

ee2e992cc   Li Zefan   block: simplify s...
965
  	strlcpy(elevator_name, name, sizeof(elevator_name));
8c2795985   KOSAKI Motohiro   elv_iosched_store...
966
  	e = elevator_get(strstrip(elevator_name));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
968
969
970
971
  	if (!e) {
  		printk(KERN_ERR "elevator: type %s not found
  ", elevator_name);
  		return -EINVAL;
  	}
2ca7d93bb   Nate Diller   [PATCH] block cle...
972
973
  	if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  		elevator_put(e);
5dd531a03   Jens Axboe   block: add functi...
974
  		return 0;
2ca7d93bb   Nate Diller   [PATCH] block cle...
975
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
976

5dd531a03   Jens Axboe   block: add functi...
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
  	return elevator_switch(q, e);
  }
  EXPORT_SYMBOL(elevator_change);
  
  ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  			  size_t count)
  {
  	int ret;
  
  	if (!q->elevator)
  		return count;
  
  	ret = elevator_change(q, name);
  	if (!ret)
  		return count;
  
  	printk(KERN_ERR "elevator: switch to %s failed
  ", name);
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
997
  ssize_t elv_iosched_show(struct request_queue *q, char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
  {
b374d18a4   Jens Axboe   block: get rid of...
999
  	struct elevator_queue *e = q->elevator;
cd43e26f0   Martin K. Petersen   block: Expose sta...
1000
  	struct elevator_type *elv;
70cee26e0   Matthias Kaehlcke   Use list_for_each...
1001
  	struct elevator_type *__e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1002
  	int len = 0;
e36f724b4   Mike Snitzer   block: Adjust elv...
1003
  	if (!q->elevator || !blk_queue_stackable(q))
cd43e26f0   Martin K. Petersen   block: Expose sta...
1004
1005
1006
1007
  		return sprintf(name, "none
  ");
  
  	elv = e->elevator_type;
2a12dcd71   Jens Axboe   [PATCH] elevator:...
1008
  	spin_lock(&elv_list_lock);
70cee26e0   Matthias Kaehlcke   Use list_for_each...
1009
  	list_for_each_entry(__e, &elv_list, list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
1013
1014
  		if (!strcmp(elv->elevator_name, __e->elevator_name))
  			len += sprintf(name+len, "[%s] ", elv->elevator_name);
  		else
  			len += sprintf(name+len, "%s ", __e->elevator_name);
  	}
2a12dcd71   Jens Axboe   [PATCH] elevator:...
1015
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1016
1017
1018
1019
1020
  
  	len += sprintf(len+name, "
  ");
  	return len;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1021
1022
  struct request *elv_rb_former_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
1023
1024
1025
1026
1027
1028
1029
1030
  {
  	struct rb_node *rbprev = rb_prev(&rq->rb_node);
  
  	if (rbprev)
  		return rb_entry_rq(rbprev);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
1031
  EXPORT_SYMBOL(elv_rb_former_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1032
1033
  struct request *elv_rb_latter_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
1034
1035
1036
1037
1038
1039
1040
1041
  {
  	struct rb_node *rbnext = rb_next(&rq->rb_node);
  
  	if (rbnext)
  		return rb_entry_rq(rbnext);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
1042
  EXPORT_SYMBOL(elv_rb_latter_request);