Blame view

block/elevator.c 25.6 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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
41
42
43
  
  #include <asm/uaccess.h>
  
  static DEFINE_SPINLOCK(elv_list_lock);
  static LIST_HEAD(elv_list);
  
  /*
9817064b6   Jens Axboe   [PATCH] elevator:...
44
45
46
47
   * Merge hash stuff.
   */
  static const int elv_hash_shift = 6;
  #define ELV_HASH_BLOCK(sec)	((sec) >> 3)
4eb166d98   Jens Axboe   block: make eleva...
48
49
  #define ELV_HASH_FN(sec)	\
  		(hash_long(ELV_HASH_BLOCK((sec)), elv_hash_shift))
9817064b6   Jens Axboe   [PATCH] elevator:...
50
51
52
53
54
  #define ELV_HASH_ENTRIES	(1 << elv_hash_shift)
  #define rq_hash_key(rq)		((rq)->sector + (rq)->nr_sectors)
  #define ELV_ON_HASH(rq)		(!hlist_unhashed(&(rq)->hash))
  
  /*
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;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
61
62
63
64
65
66
67
68
69
  	elevator_t *e = q->elevator;
  
  	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
72
73
74
75
76
77
78
79
80
81
82
83
   * can we safely merge with this request?
   */
  inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
  {
  	if (!rq_mergeable(rq))
  		return 0;
  
  	/*
  	 * 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...
84
  	 * must be same device and not a special request
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  	 */
bb4067e34   Jens Axboe   [PATCH] elevator:...
86
  	if (rq->rq_disk != bio->bi_bdev->bd_disk || rq->special)
da7752650   Jens Axboe   [PATCH] cfq-iosch...
87
88
89
90
  		return 0;
  
  	if (!elv_iosched_allow_merge(rq, bio))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91

da7752650   Jens Axboe   [PATCH] cfq-iosch...
92
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
  }
  EXPORT_SYMBOL(elv_rq_merge_ok);
769db45b7   Coywolf Qi Hunt   make elv_try_merg...
95
  static inline int elv_try_merge(struct request *__rq, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  {
  	int ret = ELEVATOR_NO_MERGE;
  
  	/*
  	 * we can merge and sequence is ok, check if it's possible
  	 */
  	if (elv_rq_merge_ok(__rq, bio)) {
  		if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
  			ret = ELEVATOR_BACK_MERGE;
  		else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
  			ret = ELEVATOR_FRONT_MERGE;
  	}
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
  static struct elevator_type *elevator_find(const char *name)
  {
a22b169df   Vasily Tarasov   [PATCH] block lay...
114
  	struct elevator_type *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115

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

a22b169df   Vasily Tarasov   [PATCH] block lay...
121
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
128
129
130
  }
  
  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...
131
  	struct elevator_type *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132

2a12dcd71   Jens Axboe   [PATCH] elevator:...
133
  	spin_lock(&elv_list_lock);
2824bc932   Tejun Heo   [PATCH] fix try_m...
134
135
136
137
  
  	e = elevator_find(name);
  	if (e && !try_module_get(e->elevator_owner))
  		e = NULL;
2a12dcd71   Jens Axboe   [PATCH] elevator:...
138
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
  
  	return e;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
142
143
  static void *elevator_init_queue(struct request_queue *q,
  				 struct elevator_queue *eq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
  {
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
145
  	return eq->ops->elevator_init_fn(q);
bc1c11697   Jens Axboe   [PATCH] elevator ...
146
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147

165125e1e   Jens Axboe   [BLOCK] Get rid o...
148
  static void elevator_attach(struct request_queue *q, struct elevator_queue *eq,
bc1c11697   Jens Axboe   [PATCH] elevator ...
149
150
  			   void *data)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  	q->elevator = eq;
bc1c11697   Jens Axboe   [PATCH] elevator ...
152
  	eq->elevator_data = data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
155
  }
  
  static char chosen_elevator[16];
5f0039764   Nate Diller   [BLOCK] elevator:...
156
  static int __init elevator_setup(char *str)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  {
752a3b796   Chuck Ebbert   [BLOCK] elevator:...
158
159
160
161
  	/*
  	 * Be backwards-compatible with previous kernels, so users
  	 * won't get the wrong elevator.
  	 */
5f0039764   Nate Diller   [BLOCK] elevator:...
162
  	if (!strcmp(str, "as"))
752a3b796   Chuck Ebbert   [BLOCK] elevator:...
163
  		strcpy(chosen_elevator, "anticipatory");
cff3ba220   Zachary Amsden   [BLOCK] elevator ...
164
  	else
5f0039764   Nate Diller   [BLOCK] elevator:...
165
  		strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
9b41046cd   OGAWA Hirofumi   [PATCH] Don't pas...
166
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
  }
  
  __setup("elevator=", elevator_setup);
3d1ab40f4   Al Viro   [PATCH] elevator_...
170
  static struct kobj_type elv_ktype;
165125e1e   Jens Axboe   [BLOCK] Get rid o...
171
172
  static elevator_t *elevator_alloc(struct request_queue *q,
  				  struct elevator_type *e)
3d1ab40f4   Al Viro   [PATCH] elevator_...
173
  {
9817064b6   Jens Axboe   [PATCH] elevator:...
174
175
  	elevator_t *eq;
  	int i;
94f6030ca   Christoph Lameter   Slab allocators: ...
176
  	eq = kmalloc_node(sizeof(elevator_t), GFP_KERNEL | __GFP_ZERO, q->node);
9817064b6   Jens Axboe   [PATCH] elevator:...
177
178
  	if (unlikely(!eq))
  		goto err;
9817064b6   Jens Axboe   [PATCH] elevator:...
179
180
  	eq->ops = &e->ops;
  	eq->elevator_type = e;
f9cb074bf   Greg Kroah-Hartman   Kobject: rename k...
181
  	kobject_init(&eq->kobj, &elv_ktype);
9817064b6   Jens Axboe   [PATCH] elevator:...
182
  	mutex_init(&eq->sysfs_lock);
b5deef901   Jens Axboe   [PATCH] Make sure...
183
184
  	eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
  					GFP_KERNEL, q->node);
9817064b6   Jens Axboe   [PATCH] elevator:...
185
186
187
188
189
  	if (!eq->hash)
  		goto err;
  
  	for (i = 0; i < ELV_HASH_ENTRIES; i++)
  		INIT_HLIST_HEAD(&eq->hash[i]);
3d1ab40f4   Al Viro   [PATCH] elevator_...
190
  	return eq;
9817064b6   Jens Axboe   [PATCH] elevator:...
191
192
193
194
  err:
  	kfree(eq);
  	elevator_put(e);
  	return NULL;
3d1ab40f4   Al Viro   [PATCH] elevator_...
195
196
197
198
199
  }
  
  static void elevator_release(struct kobject *kobj)
  {
  	elevator_t *e = container_of(kobj, elevator_t, kobj);
9817064b6   Jens Axboe   [PATCH] elevator:...
200

3d1ab40f4   Al Viro   [PATCH] elevator_...
201
  	elevator_put(e->elevator_type);
9817064b6   Jens Axboe   [PATCH] elevator:...
202
  	kfree(e->hash);
3d1ab40f4   Al Viro   [PATCH] elevator_...
203
204
  	kfree(e);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
205
  int elevator_init(struct request_queue *q, char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
  {
  	struct elevator_type *e = NULL;
  	struct elevator_queue *eq;
  	int ret = 0;
bc1c11697   Jens Axboe   [PATCH] elevator ...
210
  	void *data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211

cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
212
213
214
215
  	INIT_LIST_HEAD(&q->queue_head);
  	q->last_merge = NULL;
  	q->end_sector = 0;
  	q->boundary_rq = NULL;
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
216

4eb166d98   Jens Axboe   block: make eleva...
217
218
219
220
221
  	if (name) {
  		e = elevator_get(name);
  		if (!e)
  			return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222

4eb166d98   Jens Axboe   block: make eleva...
223
224
225
226
227
228
229
  	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:...
230

4eb166d98   Jens Axboe   block: make eleva...
231
232
233
234
235
236
237
238
239
  	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:...
240
  	}
b5deef901   Jens Axboe   [PATCH] Make sure...
241
  	eq = elevator_alloc(q, e);
3d1ab40f4   Al Viro   [PATCH] elevator_...
242
  	if (!eq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244

bc1c11697   Jens Axboe   [PATCH] elevator ...
245
246
  	data = elevator_init_queue(q, eq);
  	if (!data) {
3d1ab40f4   Al Viro   [PATCH] elevator_...
247
  		kobject_put(&eq->kobj);
bc1c11697   Jens Axboe   [PATCH] elevator ...
248
249
  		return -ENOMEM;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250

bc1c11697   Jens Axboe   [PATCH] elevator ...
251
  	elevator_attach(q, eq, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
  	return ret;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
254
  EXPORT_SYMBOL(elevator_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
  void elevator_exit(elevator_t *e)
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
257
  	mutex_lock(&e->sysfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
  	if (e->ops->elevator_exit_fn)
  		e->ops->elevator_exit_fn(e);
3d1ab40f4   Al Viro   [PATCH] elevator_...
260
261
  	e->ops = NULL;
  	mutex_unlock(&e->sysfs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262

3d1ab40f4   Al Viro   [PATCH] elevator_...
263
  	kobject_put(&e->kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
265
  EXPORT_SYMBOL(elevator_exit);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
266
  static void elv_activate_rq(struct request_queue *q, struct request *rq)
cad975164   Jens Axboe   elevator: abstrac...
267
268
269
270
271
272
  {
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_activate_req_fn)
  		e->ops->elevator_activate_req_fn(q, rq);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
273
  static void elv_deactivate_rq(struct request_queue *q, struct request *rq)
cad975164   Jens Axboe   elevator: abstrac...
274
275
276
277
278
279
  {
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_deactivate_req_fn)
  		e->ops->elevator_deactivate_req_fn(q, rq);
  }
9817064b6   Jens Axboe   [PATCH] elevator:...
280
281
282
283
  static inline void __elv_rqhash_del(struct request *rq)
  {
  	hlist_del_init(&rq->hash);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
284
  static void elv_rqhash_del(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
285
286
287
288
  {
  	if (ELV_ON_HASH(rq))
  		__elv_rqhash_del(rq);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
289
  static void elv_rqhash_add(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
290
291
292
293
294
295
  {
  	elevator_t *e = q->elevator;
  
  	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...
296
  static void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
9817064b6   Jens Axboe   [PATCH] elevator:...
297
298
299
300
  {
  	__elv_rqhash_del(rq);
  	elv_rqhash_add(q, rq);
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
301
  static struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
9817064b6   Jens Axboe   [PATCH] elevator:...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  {
  	elevator_t *e = q->elevator;
  	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...
322
  /*
2e662b65f   Jens Axboe   [PATCH] elevator:...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
   * 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);
  
  		if (rq->sector < __rq->sector)
  			p = &(*p)->rb_left;
  		else if (rq->sector > __rq->sector)
  			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:...
348
349
350
351
352
353
354
355
  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:...
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  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);
  
  		if (sector < rq->sector)
  			n = n->rb_left;
  		else if (sector > rq->sector)
  			n = n->rb_right;
  		else
  			return rq;
  	}
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
376
377
378
  EXPORT_SYMBOL(elv_rb_find);
  
  /*
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
379
   * Insert rq into dispatch queue of q.  Queue lock must be held on
dbe7f76dd   Uwe Kleine-König   fix typo "insted"...
380
   * entry.  rq is sort instead into the dispatch queue. To be used by
2e662b65f   Jens Axboe   [PATCH] elevator:...
381
   * specific elevators.
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
382
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
383
  void elv_dispatch_sort(struct request_queue *q, struct request *rq)
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
384
385
  {
  	sector_t boundary;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
386
  	struct list_head *entry;
4eb166d98   Jens Axboe   block: make eleva...
387
  	int stop_flags;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
388

06b86245c   Tejun Heo   [PATCH] 03/05 mov...
389
390
  	if (q->last_merge == rq)
  		q->last_merge = NULL;
9817064b6   Jens Axboe   [PATCH] elevator:...
391
392
  
  	elv_rqhash_del(q, rq);
15853af9f   Tejun Heo   [BLOCK] Implement...
393
  	q->nr_sorted--;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
394

1b47f531e   Jens Axboe   [PATCH] generic d...
395
  	boundary = q->end_sector;
4eb166d98   Jens Axboe   block: make eleva...
396
  	stop_flags = REQ_SOFTBARRIER | REQ_HARDBARRIER | REQ_STARTED;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
397
398
  	list_for_each_prev(entry, &q->queue_head) {
  		struct request *pos = list_entry_rq(entry);
783660b2f   Jens Axboe   elevator: don't s...
399
400
  		if (rq_data_dir(rq) != rq_data_dir(pos))
  			break;
4eb166d98   Jens Axboe   block: make eleva...
401
  		if (pos->cmd_flags & stop_flags)
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  			break;
  		if (rq->sector >= boundary) {
  			if (pos->sector < boundary)
  				continue;
  		} else {
  			if (pos->sector >= boundary)
  				break;
  		}
  		if (rq->sector >= pos->sector)
  			break;
  	}
  
  	list_add(&rq->queuelist, entry);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
416
  EXPORT_SYMBOL(elv_dispatch_sort);
9817064b6   Jens Axboe   [PATCH] elevator:...
417
  /*
2e662b65f   Jens Axboe   [PATCH] elevator:...
418
419
420
   * 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:...
421
422
423
424
425
426
427
428
429
430
431
432
433
434
   */
  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:...
435
  EXPORT_SYMBOL(elv_dispatch_add_tail);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
436
  int elv_merge(struct request_queue *q, struct request **req, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
438
  {
  	elevator_t *e = q->elevator;
9817064b6   Jens Axboe   [PATCH] elevator:...
439
  	struct request *__rq;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
440
  	int ret;
9817064b6   Jens Axboe   [PATCH] elevator:...
441
442
443
  	/*
  	 * First try one-hit cache.
  	 */
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
444
445
446
447
448
449
450
  	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
451

9817064b6   Jens Axboe   [PATCH] elevator:...
452
453
454
455
456
457
458
459
  	/*
  	 * 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
460
461
462
463
464
  	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...
465
  void elv_merged_request(struct request_queue *q, struct request *rq, int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
  {
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_merged_fn)
2e662b65f   Jens Axboe   [PATCH] elevator:...
470
  		e->ops->elevator_merged_fn(q, rq, type);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
471

2e662b65f   Jens Axboe   [PATCH] elevator:...
472
473
  	if (type == ELEVATOR_BACK_MERGE)
  		elv_rqhash_reposition(q, rq);
9817064b6   Jens Axboe   [PATCH] elevator:...
474

06b86245c   Tejun Heo   [PATCH] 03/05 mov...
475
  	q->last_merge = rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
477
  void elv_merge_requests(struct request_queue *q, struct request *rq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
  			     struct request *next)
  {
  	elevator_t *e = q->elevator;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
482
  	if (e->ops->elevator_merge_req_fn)
  		e->ops->elevator_merge_req_fn(q, rq, next);
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
483

9817064b6   Jens Axboe   [PATCH] elevator:...
484
485
486
487
  	elv_rqhash_reposition(q, rq);
  	elv_rqhash_del(q, next);
  
  	q->nr_sorted--;
06b86245c   Tejun Heo   [PATCH] 03/05 mov...
488
  	q->last_merge = rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
490
  void elv_requeue_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
493
494
495
  	/*
  	 * it already went through dequeue, we need to decrement the
  	 * in_flight count again
  	 */
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
496
  	if (blk_account_rq(rq)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
  		q->in_flight--;
cad975164   Jens Axboe   elevator: abstrac...
498
499
  		if (blk_sorted_rq(rq))
  			elv_deactivate_rq(q, rq);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
500
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501

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

30e9656cc   Tejun Heo   [PATCH] block: im...
504
  	elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
506
  static void elv_drain_elevator(struct request_queue *q)
15853af9f   Tejun Heo   [BLOCK] Implement...
507
508
509
510
511
512
513
514
515
516
517
518
519
  {
  	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);
  	}
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
520
  void elv_insert(struct request_queue *q, struct request *rq, int where)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
  {
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
522
523
  	struct list_head *pos;
  	unsigned ordseq;
dac07ec12   Jens Axboe   [BLOCK] limit req...
524
  	int unplug_it = 1;
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
525

2056a782f   Jens Axboe   [PATCH] Block que...
526
  	blk_add_trace_rq(q, rq, BLK_TA_INSERT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
  	rq->q = q;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
528
529
  	switch (where) {
  	case ELEVATOR_INSERT_FRONT:
4aff5e233   Jens Axboe   [PATCH] Split str...
530
  		rq->cmd_flags |= REQ_SOFTBARRIER;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
531
532
533
534
535
  
  		list_add(&rq->queuelist, &q->queue_head);
  		break;
  
  	case ELEVATOR_INSERT_BACK:
4aff5e233   Jens Axboe   [PATCH] Split str...
536
  		rq->cmd_flags |= REQ_SOFTBARRIER;
15853af9f   Tejun Heo   [BLOCK] Implement...
537
  		elv_drain_elevator(q);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  		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.
  		 */
  		blk_remove_plug(q);
  		q->request_fn(q);
  		break;
  
  	case ELEVATOR_INSERT_SORT:
  		BUG_ON(!blk_fs_request(rq));
4aff5e233   Jens Axboe   [PATCH] Split str...
555
  		rq->cmd_flags |= REQ_SORTED;
15853af9f   Tejun Heo   [BLOCK] Implement...
556
  		q->nr_sorted++;
9817064b6   Jens Axboe   [PATCH] elevator:...
557
558
559
560
561
  		if (rq_mergeable(rq)) {
  			elv_rqhash_add(q, rq);
  			if (!q->last_merge)
  				q->last_merge = rq;
  		}
ca23509fb   Tejun Heo   [PATCH] blk: fix ...
562
563
564
565
566
567
  		/*
  		 * 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...
568
  		break;
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
569
570
571
572
573
574
  	case ELEVATOR_INSERT_REQUEUE:
  		/*
  		 * If ordered flush isn't in progress, we do front
  		 * insertion; otherwise, requests should be requeued
  		 * in ordseq order.
  		 */
4aff5e233   Jens Axboe   [PATCH] Split str...
575
  		rq->cmd_flags |= REQ_SOFTBARRIER;
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
576

95543179f   Linas Vepstas   [PATCH] elevator:...
577
578
579
580
581
  		/*
  		 * Most requeues happen because of a busy condition,
  		 * don't force unplug of the queue for that case.
  		 */
  		unplug_it = 0;
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
  		if (q->ordseq == 0) {
  			list_add(&rq->queuelist, &q->queue_head);
  			break;
  		}
  
  		ordseq = blk_ordered_req_seq(rq);
  
  		list_for_each(pos, &q->queue_head) {
  			struct request *pos_rq = list_entry_rq(pos);
  			if (ordseq <= blk_ordered_req_seq(pos_rq))
  				break;
  		}
  
  		list_add_tail(&rq->queuelist, pos);
  		break;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
597
598
599
600
601
602
  	default:
  		printk(KERN_ERR "%s: bad insertion point %d
  ",
  		       __FUNCTION__, where);
  		BUG();
  	}
dac07ec12   Jens Axboe   [BLOCK] limit req...
603
  	if (unplug_it && blk_queue_plugged(q)) {
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
604
605
606
607
608
609
  		int nrq = q->rq.count[READ] + q->rq.count[WRITE]
  			- q->in_flight;
  
  		if (nrq >= q->unplug_thresh)
  			__generic_unplug_device(q);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
611
  void __elv_add_request(struct request_queue *q, struct request *rq, int where,
30e9656cc   Tejun Heo   [PATCH] block: im...
612
613
614
  		       int plug)
  {
  	if (q->ordcolor)
4aff5e233   Jens Axboe   [PATCH] Split str...
615
  		rq->cmd_flags |= REQ_ORDERED_COLOR;
30e9656cc   Tejun Heo   [PATCH] block: im...
616

4aff5e233   Jens Axboe   [PATCH] Split str...
617
  	if (rq->cmd_flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
30e9656cc   Tejun Heo   [PATCH] block: im...
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  		/*
  		 * toggle ordered color
  		 */
  		if (blk_barrier_rq(rq))
  			q->ordcolor ^= 1;
  
  		/*
  		 * barriers implicitly indicate back insertion
  		 */
  		if (where == ELEVATOR_INSERT_SORT)
  			where = ELEVATOR_INSERT_BACK;
  
  		/*
  		 * this request is scheduling boundary, update
  		 * end_sector
  		 */
  		if (blk_fs_request(rq)) {
  			q->end_sector = rq_end_sector(rq);
  			q->boundary_rq = rq;
  		}
4eb166d98   Jens Axboe   block: make eleva...
638
639
  	} else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
  		    where == ELEVATOR_INSERT_SORT)
30e9656cc   Tejun Heo   [PATCH] block: im...
640
641
642
643
644
645
646
  		where = ELEVATOR_INSERT_BACK;
  
  	if (plug)
  		blk_plug_device(q);
  
  	elv_insert(q, rq, where);
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
647
  EXPORT_SYMBOL(__elv_add_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
648
  void elv_add_request(struct request_queue *q, struct request *rq, int where,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
650
651
652
653
654
655
656
  		     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:...
657
  EXPORT_SYMBOL(elv_add_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
658
  static inline struct request *__elv_next_request(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
  {
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
660
  	struct request *rq;
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
661
662
663
664
665
666
  	while (1) {
  		while (!list_empty(&q->queue_head)) {
  			rq = list_entry_rq(q->queue_head.next);
  			if (blk_do_ordered(q, &rq))
  				return rq;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667

797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
668
669
  		if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
  			return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
672
  struct request *elv_next_request(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
  {
  	struct request *rq;
  	int ret;
  
  	while ((rq = __elv_next_request(q)) != NULL) {
bf2de6f5a   Jens Axboe   block: Initial su...
678
679
680
681
682
683
684
685
  		/*
  		 * Kill the empty barrier place holder, the driver must
  		 * not ever see it.
  		 */
  		if (blk_empty_barrier(rq)) {
  			end_queued_request(rq, 1);
  			continue;
  		}
4aff5e233   Jens Axboe   [PATCH] Split str...
686
  		if (!(rq->cmd_flags & REQ_STARTED)) {
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
687
688
689
690
691
  			/*
  			 * This is the first time the device driver
  			 * sees this request (possibly after
  			 * requeueing).  Notify IO scheduler.
  			 */
cad975164   Jens Axboe   elevator: abstrac...
692
693
  			if (blk_sorted_rq(rq))
  				elv_activate_rq(q, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694

8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
695
696
697
698
699
  			/*
  			 * just mark as started even if we don't start
  			 * it, a request that has been delayed should
  			 * not be passed by new incoming requests
  			 */
4aff5e233   Jens Axboe   [PATCH] Split str...
700
  			rq->cmd_flags |= REQ_STARTED;
2056a782f   Jens Axboe   [PATCH] Block que...
701
  			blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
702
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703

8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
704
  		if (!q->boundary_rq || q->boundary_rq == rq) {
1b47f531e   Jens Axboe   [PATCH] generic d...
705
  			q->end_sector = rq_end_sector(rq);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
706
707
  			q->boundary_rq = NULL;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708

fa0ccd837   James Bottomley   block: implement ...
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
  		if (rq->cmd_flags & REQ_DONTPREP)
  			break;
  
  		if (q->dma_drain_size && rq->data_len) {
  			/*
  			 * make sure space for the drain appears we
  			 * know we can do this because max_hw_segments
  			 * has been adjusted to be one fewer than the
  			 * device can handle
  			 */
  			rq->nr_phys_segments++;
  			rq->nr_hw_segments++;
  		}
  
  		if (!q->prep_rq_fn)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
725
726
727
728
729
  			break;
  
  		ret = q->prep_rq_fn(q, rq);
  		if (ret == BLKPREP_OK) {
  			break;
  		} else if (ret == BLKPREP_DEFER) {
2e759cd4f   Tejun Heo   [SCSI] make blk l...
730
731
732
  			/*
  			 * the request may have been (partially) prepped.
  			 * we need to keep this request in the front to
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
733
734
  			 * avoid resource deadlock.  REQ_STARTED will
  			 * prevent other fs requests from passing this one.
2e759cd4f   Tejun Heo   [SCSI] make blk l...
735
  			 */
fa0ccd837   James Bottomley   block: implement ...
736
737
738
739
740
741
742
743
744
  			if (q->dma_drain_size && rq->data_len &&
  			    !(rq->cmd_flags & REQ_DONTPREP)) {
  				/*
  				 * remove the space for the drain we added
  				 * so that we don't add it again
  				 */
  				--rq->nr_phys_segments;
  				--rq->nr_hw_segments;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
  			rq = NULL;
  			break;
  		} else if (ret == BLKPREP_KILL) {
4aff5e233   Jens Axboe   [PATCH] Split str...
748
  			rq->cmd_flags |= REQ_QUIET;
a0cd12854   Jens Axboe   block: add end_qu...
749
  			end_queued_request(rq, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
751
752
753
754
755
756
757
758
759
  		} else {
  			printk(KERN_ERR "%s: bad return=%d
  ", __FUNCTION__,
  								ret);
  			break;
  		}
  	}
  
  	return rq;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
760
  EXPORT_SYMBOL(elv_next_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
761
  void elv_dequeue_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
  {
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
763
  	BUG_ON(list_empty(&rq->queuelist));
9817064b6   Jens Axboe   [PATCH] elevator:...
764
  	BUG_ON(ELV_ON_HASH(rq));
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
765
766
  
  	list_del_init(&rq->queuelist);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
768
769
770
  
  	/*
  	 * the time frame between a request being removed from the lists
  	 * and to it is freed is accounted as io that is in progress at
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
771
  	 * the driver side.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
772
773
774
  	 */
  	if (blk_account_rq(rq))
  		q->in_flight++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
776
  EXPORT_SYMBOL(elv_dequeue_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
777
  int elv_queue_empty(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
778
779
  {
  	elevator_t *e = q->elevator;
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
780
781
  	if (!list_empty(&q->queue_head))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
  	if (e->ops->elevator_queue_empty_fn)
  		return e->ops->elevator_queue_empty_fn(q);
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
784
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
786
  EXPORT_SYMBOL(elv_queue_empty);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
787
  struct request *elv_latter_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
790
791
792
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_latter_req_fn)
  		return e->ops->elevator_latter_req_fn(q, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
  	return NULL;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
795
  struct request *elv_former_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
797
798
799
800
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_former_req_fn)
  		return e->ops->elevator_former_req_fn(q, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
  	return NULL;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
803
  int elv_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
805
806
807
  {
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_set_req_fn)
cb78b285c   Jens Axboe   [PATCH] Drop usel...
808
  		return e->ops->elevator_set_req_fn(q, rq, gfp_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809
810
811
812
  
  	rq->elevator_private = NULL;
  	return 0;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
813
  void elv_put_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
814
815
816
817
  {
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_put_req_fn)
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
818
  		e->ops->elevator_put_req_fn(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
820
  int elv_may_queue(struct request_queue *q, int rw)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
822
823
824
  {
  	elevator_t *e = q->elevator;
  
  	if (e->ops->elevator_may_queue_fn)
cb78b285c   Jens Axboe   [PATCH] Drop usel...
825
  		return e->ops->elevator_may_queue_fn(q, rw);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
827
828
  
  	return ELV_MQUEUE_MAY;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
829
  void elv_completed_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830
831
832
833
834
835
  {
  	elevator_t *e = q->elevator;
  
  	/*
  	 * request is released from the driver, io must be done
  	 */
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
836
  	if (blk_account_rq(rq)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
  		q->in_flight--;
1bc691d35   Tejun Heo   [PATCH] fix queue...
838
839
840
  		if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
  			e->ops->elevator_completed_req_fn(q, rq);
  	}
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
841

1bc691d35   Tejun Heo   [PATCH] fix queue...
842
843
844
845
846
847
848
  	/*
  	 * Check if the queue is waiting for fs requests to be
  	 * drained for flush sequence.
  	 */
  	if (unlikely(q->ordseq)) {
  		struct request *first_rq = list_entry_rq(q->queue_head.next);
  		if (q->in_flight == 0 &&
797e7dbbe   Tejun Heo   [BLOCK] reimpleme...
849
850
851
852
853
  		    blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
  		    blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
  			blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
  			q->request_fn(q);
  		}
8922e16cf   Tejun Heo   [PATCH] 01/05 Imp...
854
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
  }
3d1ab40f4   Al Viro   [PATCH] elevator_...
856
857
858
859
  #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
860
  {
3d1ab40f4   Al Viro   [PATCH] elevator_...
861
862
863
864
865
866
867
868
869
870
871
872
  	elevator_t *e = container_of(kobj, elevator_t, kobj);
  	struct elv_fs_entry *entry = to_elv(attr);
  	ssize_t error;
  
  	if (!entry->show)
  		return -EIO;
  
  	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
873

3d1ab40f4   Al Viro   [PATCH] elevator_...
874
875
876
877
878
879
880
  static ssize_t
  elv_attr_store(struct kobject *kobj, struct attribute *attr,
  	       const char *page, size_t length)
  {
  	elevator_t *e = container_of(kobj, elevator_t, kobj);
  	struct elv_fs_entry *entry = to_elv(attr);
  	ssize_t error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
881

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

3d1ab40f4   Al Viro   [PATCH] elevator_...
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  	mutex_lock(&e->sysfs_lock);
  	error = e->ops ? entry->store(e, page, length) : -ENOENT;
  	mutex_unlock(&e->sysfs_lock);
  	return error;
  }
  
  static struct sysfs_ops elv_sysfs_ops = {
  	.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)
  {
  	elevator_t *e = q->elevator;
  	int error;
b2d6db587   Greg Kroah-Hartman   Kobject: rename k...
905
  	error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
3d1ab40f4   Al Viro   [PATCH] elevator_...
906
  	if (!error) {
e572ec7e4   Al Viro   [PATCH] fix rmmod...
907
  		struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
3d1ab40f4   Al Viro   [PATCH] elevator_...
908
  		if (attr) {
e572ec7e4   Al Viro   [PATCH] fix rmmod...
909
910
  			while (attr->attr.name) {
  				if (sysfs_create_file(&e->kobj, &attr->attr))
3d1ab40f4   Al Viro   [PATCH] elevator_...
911
  					break;
e572ec7e4   Al Viro   [PATCH] fix rmmod...
912
  				attr++;
3d1ab40f4   Al Viro   [PATCH] elevator_...
913
914
915
916
917
  			}
  		}
  		kobject_uevent(&e->kobj, KOBJ_ADD);
  	}
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
918
  }
bc1c11697   Jens Axboe   [PATCH] elevator ...
919
920
921
922
923
  static void __elv_unregister_queue(elevator_t *e)
  {
  	kobject_uevent(&e->kobj, KOBJ_REMOVE);
  	kobject_del(&e->kobj);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  void elv_unregister_queue(struct request_queue *q)
  {
bc1c11697   Jens Axboe   [PATCH] elevator ...
926
927
  	if (q)
  		__elv_unregister_queue(q->elevator);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
  }
2fdd82bd8   Adrian Bunk   block: let elv_re...
929
  void elv_register(struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930
  {
1ffb96c58   Thibaut VARENE   make elv_register...
931
  	char *def = "";
2a12dcd71   Jens Axboe   [PATCH] elevator:...
932
933
  
  	spin_lock(&elv_list_lock);
ce5244974   Eric Sesterhenn   BUG_ON() Conversi...
934
  	BUG_ON(elevator_find(e->elevator_name));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
  	list_add_tail(&e->list, &elv_list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
936
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937

5f0039764   Nate Diller   [BLOCK] elevator:...
938
939
940
  	if (!strcmp(e->elevator_name, chosen_elevator) ||
  			(!*chosen_elevator &&
  			 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
1ffb96c58   Thibaut VARENE   make elv_register...
941
  				def = " (default)";
4eb166d98   Jens Axboe   block: make eleva...
942
943
944
  	printk(KERN_INFO "io scheduler %s registered%s
  ", e->elevator_name,
  								def);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
946
947
948
949
  }
  EXPORT_SYMBOL_GPL(elv_register);
  
  void elv_unregister(struct elevator_type *e)
  {
83521d3eb   Christoph Hellwig   [PATCH] cfq-iosch...
950
951
952
953
954
  	struct task_struct *g, *p;
  
  	/*
  	 * Iterate every thread in the process to remove the io contexts.
  	 */
e17a9489b   Al Viro   [PATCH] stop elv_...
955
956
957
958
  	if (e->ops.trim) {
  		read_lock(&tasklist_lock);
  		do_each_thread(g, p) {
  			task_lock(p);
2d8f61316   Oleg Nesterov   elv_unregister: f...
959
960
  			if (p->io_context)
  				e->ops.trim(p->io_context);
e17a9489b   Al Viro   [PATCH] stop elv_...
961
962
963
964
  			task_unlock(p);
  		} while_each_thread(g, p);
  		read_unlock(&tasklist_lock);
  	}
83521d3eb   Christoph Hellwig   [PATCH] cfq-iosch...
965

2a12dcd71   Jens Axboe   [PATCH] elevator:...
966
  	spin_lock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
  	list_del_init(&e->list);
2a12dcd71   Jens Axboe   [PATCH] elevator:...
968
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
969
970
971
972
973
974
975
  }
  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...
976
   * one, if the new one fails init for some reason.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
977
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
978
  static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
979
  {
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
980
  	elevator_t *old_elevator, *e;
bc1c11697   Jens Axboe   [PATCH] elevator ...
981
  	void *data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982

cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
983
984
985
  	/*
  	 * Allocate new elevator
  	 */
b5deef901   Jens Axboe   [PATCH] Make sure...
986
  	e = elevator_alloc(q, new_e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
  	if (!e)
3d1ab40f4   Al Viro   [PATCH] elevator_...
988
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
989

bc1c11697   Jens Axboe   [PATCH] elevator ...
990
991
992
993
994
  	data = elevator_init_queue(q, e);
  	if (!data) {
  		kobject_put(&e->kobj);
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
995
  	/*
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
996
  	 * Turn on BYPASS and drain all requests w/ elevator private data
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
997
  	 */
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
998
  	spin_lock_irq(q->queue_lock);
64521d1a3   Jens Axboe   [BLOCK] elevator ...
999
  	set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
1000

15853af9f   Tejun Heo   [BLOCK] Implement...
1001
  	elv_drain_elevator(q);
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
1002
1003
  
  	while (q->rq.elvpriv) {
407df2aa2   Tejun Heo   [BLOCK] elevator:...
1004
1005
  		blk_remove_plug(q);
  		q->request_fn(q);
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
1006
  		spin_unlock_irq(q->queue_lock);
64521d1a3   Jens Axboe   [BLOCK] elevator ...
1007
  		msleep(10);
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
1008
  		spin_lock_irq(q->queue_lock);
15853af9f   Tejun Heo   [BLOCK] Implement...
1009
  		elv_drain_elevator(q);
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
1010
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1011
  	/*
bc1c11697   Jens Axboe   [PATCH] elevator ...
1012
  	 * Remember old elevator.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1014
1015
1016
  	old_elevator = q->elevator;
  
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1017
1018
  	 * attach and start new elevator
  	 */
bc1c11697   Jens Axboe   [PATCH] elevator ...
1019
1020
1021
1022
1023
  	elevator_attach(q, e, data);
  
  	spin_unlock_irq(q->queue_lock);
  
  	__elv_unregister_queue(old_elevator);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1024
1025
1026
1027
1028
  
  	if (elv_register_queue(q))
  		goto fail_register;
  
  	/*
cb98fc8bb   Tejun Heo   [BLOCK] Reimpleme...
1029
  	 * finally exit old elevator and turn off BYPASS.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1030
1031
  	 */
  	elevator_exit(old_elevator);
64521d1a3   Jens Axboe   [BLOCK] elevator ...
1032
  	clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
3d1ab40f4   Al Viro   [PATCH] elevator_...
1033
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1034
1035
1036
1037
1038
1039
1040
  
  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
1041
1042
  	q->elevator = old_elevator;
  	elv_register_queue(q);
64521d1a3   Jens Axboe   [BLOCK] elevator ...
1043
  	clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
3d1ab40f4   Al Viro   [PATCH] elevator_...
1044
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1045
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1046
1047
  ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  			  size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1048
1049
  {
  	char elevator_name[ELV_NAME_MAX];
be5612356   Tejun Heo   [BLOCK] fix strin...
1050
  	size_t len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1051
  	struct elevator_type *e;
be5612356   Tejun Heo   [BLOCK] fix strin...
1052
1053
1054
  	elevator_name[sizeof(elevator_name) - 1] = '\0';
  	strncpy(elevator_name, name, sizeof(elevator_name) - 1);
  	len = strlen(elevator_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055

be5612356   Tejun Heo   [BLOCK] fix strin...
1056
1057
1058
  	if (len && elevator_name[len - 1] == '
  ')
  		elevator_name[len - 1] = '\0';
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
1061
1062
1063
1064
1065
  
  	e = elevator_get(elevator_name);
  	if (!e) {
  		printk(KERN_ERR "elevator: type %s not found
  ", elevator_name);
  		return -EINVAL;
  	}
2ca7d93bb   Nate Diller   [PATCH] block cle...
1066
1067
  	if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
  		elevator_put(e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1068
  		return count;
2ca7d93bb   Nate Diller   [PATCH] block cle...
1069
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070

3d1ab40f4   Al Viro   [PATCH] elevator_...
1071
  	if (!elevator_switch(q, e))
4eb166d98   Jens Axboe   block: make eleva...
1072
1073
1074
  		printk(KERN_ERR "elevator: switch to %s failed
  ",
  							elevator_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
1076
  	return count;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1077
  ssize_t elv_iosched_show(struct request_queue *q, char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078
1079
1080
  {
  	elevator_t *e = q->elevator;
  	struct elevator_type *elv = e->elevator_type;
70cee26e0   Matthias Kaehlcke   Use list_for_each...
1081
  	struct elevator_type *__e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082
  	int len = 0;
2a12dcd71   Jens Axboe   [PATCH] elevator:...
1083
  	spin_lock(&elv_list_lock);
70cee26e0   Matthias Kaehlcke   Use list_for_each...
1084
  	list_for_each_entry(__e, &elv_list, list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1085
1086
1087
1088
1089
  		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:...
1090
  	spin_unlock(&elv_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
1092
1093
1094
1095
  
  	len += sprintf(len+name, "
  ");
  	return len;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1096
1097
  struct request *elv_rb_former_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
1098
1099
1100
1101
1102
1103
1104
1105
  {
  	struct rb_node *rbprev = rb_prev(&rq->rb_node);
  
  	if (rbprev)
  		return rb_entry_rq(rbprev);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
1106
  EXPORT_SYMBOL(elv_rb_former_request);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1107
1108
  struct request *elv_rb_latter_request(struct request_queue *q,
  				      struct request *rq)
2e662b65f   Jens Axboe   [PATCH] elevator:...
1109
1110
1111
1112
1113
1114
1115
1116
  {
  	struct rb_node *rbnext = rb_next(&rq->rb_node);
  
  	if (rbnext)
  		return rb_entry_rq(rbnext);
  
  	return NULL;
  }
2e662b65f   Jens Axboe   [PATCH] elevator:...
1117
  EXPORT_SYMBOL(elv_rb_latter_request);