]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Block device elevator/IO-scheduler. | |
3 | * | |
4 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE | |
5 | * | |
6 | * 30042000 Jens Axboe <axboe@suse.de> : | |
7 | * | |
8 | * Split the elevator a bit so that it is possible to choose a different | |
9 | * one or even write a new "plug in". There are three pieces: | |
10 | * - elevator_fn, inserts a new request in the queue list | |
11 | * - elevator_merge_fn, decides whether a new buffer can be merged with | |
12 | * an existing request | |
13 | * - elevator_dequeue_fn, called when a request is taken off the active list | |
14 | * | |
15 | * 20082000 Dave Jones <davej@suse.de> : | |
16 | * Removed tests for max-bomb-segments, which was breaking elvtune | |
17 | * when run without -bN | |
18 | * | |
19 | * Jens: | |
20 | * - Rework again to work with bio instead of buffer_heads | |
21 | * - loose bi_dev comparisons, partition handling is right now | |
22 | * - completely modularize elevator setup and teardown | |
23 | * | |
24 | */ | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/fs.h> | |
27 | #include <linux/blkdev.h> | |
28 | #include <linux/elevator.h> | |
29 | #include <linux/bio.h> | |
30 | #include <linux/config.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/init.h> | |
34 | #include <linux/compiler.h> | |
35 | #include <linux/delay.h> | |
36 | ||
37 | #include <asm/uaccess.h> | |
38 | ||
39 | static DEFINE_SPINLOCK(elv_list_lock); | |
40 | static LIST_HEAD(elv_list); | |
41 | ||
42 | /* | |
43 | * can we safely merge with this request? | |
44 | */ | |
45 | inline int elv_rq_merge_ok(struct request *rq, struct bio *bio) | |
46 | { | |
47 | if (!rq_mergeable(rq)) | |
48 | return 0; | |
49 | ||
50 | /* | |
51 | * different data direction or already started, don't merge | |
52 | */ | |
53 | if (bio_data_dir(bio) != rq_data_dir(rq)) | |
54 | return 0; | |
55 | ||
56 | /* | |
57 | * same device and no special stuff set, merge is ok | |
58 | */ | |
59 | if (rq->rq_disk == bio->bi_bdev->bd_disk && | |
60 | !rq->waiting && !rq->special) | |
61 | return 1; | |
62 | ||
63 | return 0; | |
64 | } | |
65 | EXPORT_SYMBOL(elv_rq_merge_ok); | |
66 | ||
67 | static inline int elv_try_merge(struct request *__rq, struct bio *bio) | |
68 | { | |
69 | int ret = ELEVATOR_NO_MERGE; | |
70 | ||
71 | /* | |
72 | * we can merge and sequence is ok, check if it's possible | |
73 | */ | |
74 | if (elv_rq_merge_ok(__rq, bio)) { | |
75 | if (__rq->sector + __rq->nr_sectors == bio->bi_sector) | |
76 | ret = ELEVATOR_BACK_MERGE; | |
77 | else if (__rq->sector - bio_sectors(bio) == bio->bi_sector) | |
78 | ret = ELEVATOR_FRONT_MERGE; | |
79 | } | |
80 | ||
81 | return ret; | |
82 | } | |
83 | ||
84 | static struct elevator_type *elevator_find(const char *name) | |
85 | { | |
86 | struct elevator_type *e = NULL; | |
87 | struct list_head *entry; | |
88 | ||
89 | list_for_each(entry, &elv_list) { | |
90 | struct elevator_type *__e; | |
91 | ||
92 | __e = list_entry(entry, struct elevator_type, list); | |
93 | ||
94 | if (!strcmp(__e->elevator_name, name)) { | |
95 | e = __e; | |
96 | break; | |
97 | } | |
98 | } | |
99 | ||
100 | return e; | |
101 | } | |
102 | ||
103 | static void elevator_put(struct elevator_type *e) | |
104 | { | |
105 | module_put(e->elevator_owner); | |
106 | } | |
107 | ||
108 | static struct elevator_type *elevator_get(const char *name) | |
109 | { | |
110 | struct elevator_type *e; | |
111 | ||
112 | spin_lock_irq(&elv_list_lock); | |
113 | ||
114 | e = elevator_find(name); | |
115 | if (e && !try_module_get(e->elevator_owner)) | |
116 | e = NULL; | |
117 | ||
118 | spin_unlock_irq(&elv_list_lock); | |
119 | ||
120 | return e; | |
121 | } | |
122 | ||
123 | static int elevator_attach(request_queue_t *q, struct elevator_type *e, | |
124 | struct elevator_queue *eq) | |
125 | { | |
126 | int ret = 0; | |
127 | ||
128 | memset(eq, 0, sizeof(*eq)); | |
129 | eq->ops = &e->ops; | |
130 | eq->elevator_type = e; | |
131 | ||
132 | q->elevator = eq; | |
133 | ||
134 | if (eq->ops->elevator_init_fn) | |
135 | ret = eq->ops->elevator_init_fn(q, eq); | |
136 | ||
137 | return ret; | |
138 | } | |
139 | ||
140 | static char chosen_elevator[16]; | |
141 | ||
142 | static int __init elevator_setup(char *str) | |
143 | { | |
144 | /* | |
145 | * Be backwards-compatible with previous kernels, so users | |
146 | * won't get the wrong elevator. | |
147 | */ | |
148 | if (!strcmp(str, "as")) | |
149 | strcpy(chosen_elevator, "anticipatory"); | |
150 | else | |
151 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); | |
152 | return 0; | |
153 | } | |
154 | ||
155 | __setup("elevator=", elevator_setup); | |
156 | ||
157 | int elevator_init(request_queue_t *q, char *name) | |
158 | { | |
159 | struct elevator_type *e = NULL; | |
160 | struct elevator_queue *eq; | |
161 | int ret = 0; | |
162 | ||
163 | INIT_LIST_HEAD(&q->queue_head); | |
164 | q->last_merge = NULL; | |
165 | q->end_sector = 0; | |
166 | q->boundary_rq = NULL; | |
167 | ||
168 | if (name && !(e = elevator_get(name))) | |
169 | return -EINVAL; | |
170 | ||
171 | if (!e && !(e = elevator_get(chosen_elevator))) { | |
172 | e = elevator_get(CONFIG_DEFAULT_IOSCHED); | |
173 | if (*chosen_elevator) | |
174 | printk("I/O scheduler %s not found\n", chosen_elevator); | |
175 | } | |
176 | ||
177 | eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL); | |
178 | if (!eq) { | |
179 | elevator_put(e); | |
180 | return -ENOMEM; | |
181 | } | |
182 | ||
183 | ret = elevator_attach(q, e, eq); | |
184 | if (ret) { | |
185 | kfree(eq); | |
186 | elevator_put(e); | |
187 | } | |
188 | ||
189 | return ret; | |
190 | } | |
191 | ||
192 | void elevator_exit(elevator_t *e) | |
193 | { | |
194 | if (e->ops->elevator_exit_fn) | |
195 | e->ops->elevator_exit_fn(e); | |
196 | ||
197 | elevator_put(e->elevator_type); | |
198 | e->elevator_type = NULL; | |
199 | kfree(e); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Insert rq into dispatch queue of q. Queue lock must be held on | |
204 | * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be | |
205 | * appended to the dispatch queue. To be used by specific elevators. | |
206 | */ | |
207 | void elv_dispatch_sort(request_queue_t *q, struct request *rq) | |
208 | { | |
209 | sector_t boundary; | |
210 | struct list_head *entry; | |
211 | ||
212 | if (q->last_merge == rq) | |
213 | q->last_merge = NULL; | |
214 | q->nr_sorted--; | |
215 | ||
216 | boundary = q->end_sector; | |
217 | ||
218 | list_for_each_prev(entry, &q->queue_head) { | |
219 | struct request *pos = list_entry_rq(entry); | |
220 | ||
221 | if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED)) | |
222 | break; | |
223 | if (rq->sector >= boundary) { | |
224 | if (pos->sector < boundary) | |
225 | continue; | |
226 | } else { | |
227 | if (pos->sector >= boundary) | |
228 | break; | |
229 | } | |
230 | if (rq->sector >= pos->sector) | |
231 | break; | |
232 | } | |
233 | ||
234 | list_add(&rq->queuelist, entry); | |
235 | } | |
236 | ||
237 | int elv_merge(request_queue_t *q, struct request **req, struct bio *bio) | |
238 | { | |
239 | elevator_t *e = q->elevator; | |
240 | int ret; | |
241 | ||
242 | if (q->last_merge) { | |
243 | ret = elv_try_merge(q->last_merge, bio); | |
244 | if (ret != ELEVATOR_NO_MERGE) { | |
245 | *req = q->last_merge; | |
246 | return ret; | |
247 | } | |
248 | } | |
249 | ||
250 | if (e->ops->elevator_merge_fn) | |
251 | return e->ops->elevator_merge_fn(q, req, bio); | |
252 | ||
253 | return ELEVATOR_NO_MERGE; | |
254 | } | |
255 | ||
256 | void elv_merged_request(request_queue_t *q, struct request *rq) | |
257 | { | |
258 | elevator_t *e = q->elevator; | |
259 | ||
260 | if (e->ops->elevator_merged_fn) | |
261 | e->ops->elevator_merged_fn(q, rq); | |
262 | ||
263 | q->last_merge = rq; | |
264 | } | |
265 | ||
266 | void elv_merge_requests(request_queue_t *q, struct request *rq, | |
267 | struct request *next) | |
268 | { | |
269 | elevator_t *e = q->elevator; | |
270 | ||
271 | if (e->ops->elevator_merge_req_fn) | |
272 | e->ops->elevator_merge_req_fn(q, rq, next); | |
273 | q->nr_sorted--; | |
274 | ||
275 | q->last_merge = rq; | |
276 | } | |
277 | ||
278 | void elv_requeue_request(request_queue_t *q, struct request *rq) | |
279 | { | |
280 | elevator_t *e = q->elevator; | |
281 | ||
282 | /* | |
283 | * it already went through dequeue, we need to decrement the | |
284 | * in_flight count again | |
285 | */ | |
286 | if (blk_account_rq(rq)) { | |
287 | q->in_flight--; | |
288 | if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn) | |
289 | e->ops->elevator_deactivate_req_fn(q, rq); | |
290 | } | |
291 | ||
292 | rq->flags &= ~REQ_STARTED; | |
293 | ||
294 | __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE, 0); | |
295 | } | |
296 | ||
297 | static void elv_drain_elevator(request_queue_t *q) | |
298 | { | |
299 | static int printed; | |
300 | while (q->elevator->ops->elevator_dispatch_fn(q, 1)) | |
301 | ; | |
302 | if (q->nr_sorted == 0) | |
303 | return; | |
304 | if (printed++ < 10) { | |
305 | printk(KERN_ERR "%s: forced dispatching is broken " | |
306 | "(nr_sorted=%u), please report this\n", | |
307 | q->elevator->elevator_type->elevator_name, q->nr_sorted); | |
308 | } | |
309 | } | |
310 | ||
311 | void __elv_add_request(request_queue_t *q, struct request *rq, int where, | |
312 | int plug) | |
313 | { | |
314 | struct list_head *pos; | |
315 | unsigned ordseq; | |
316 | ||
317 | if (q->ordcolor) | |
318 | rq->flags |= REQ_ORDERED_COLOR; | |
319 | ||
320 | if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) { | |
321 | /* | |
322 | * toggle ordered color | |
323 | */ | |
324 | q->ordcolor ^= 1; | |
325 | ||
326 | /* | |
327 | * barriers implicitly indicate back insertion | |
328 | */ | |
329 | if (where == ELEVATOR_INSERT_SORT) | |
330 | where = ELEVATOR_INSERT_BACK; | |
331 | ||
332 | /* | |
333 | * this request is scheduling boundary, update end_sector | |
334 | */ | |
335 | if (blk_fs_request(rq)) { | |
336 | q->end_sector = rq_end_sector(rq); | |
337 | q->boundary_rq = rq; | |
338 | } | |
339 | } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT) | |
340 | where = ELEVATOR_INSERT_BACK; | |
341 | ||
342 | if (plug) | |
343 | blk_plug_device(q); | |
344 | ||
345 | rq->q = q; | |
346 | ||
347 | switch (where) { | |
348 | case ELEVATOR_INSERT_FRONT: | |
349 | rq->flags |= REQ_SOFTBARRIER; | |
350 | ||
351 | list_add(&rq->queuelist, &q->queue_head); | |
352 | break; | |
353 | ||
354 | case ELEVATOR_INSERT_BACK: | |
355 | rq->flags |= REQ_SOFTBARRIER; | |
356 | elv_drain_elevator(q); | |
357 | list_add_tail(&rq->queuelist, &q->queue_head); | |
358 | /* | |
359 | * We kick the queue here for the following reasons. | |
360 | * - The elevator might have returned NULL previously | |
361 | * to delay requests and returned them now. As the | |
362 | * queue wasn't empty before this request, ll_rw_blk | |
363 | * won't run the queue on return, resulting in hang. | |
364 | * - Usually, back inserted requests won't be merged | |
365 | * with anything. There's no point in delaying queue | |
366 | * processing. | |
367 | */ | |
368 | blk_remove_plug(q); | |
369 | q->request_fn(q); | |
370 | break; | |
371 | ||
372 | case ELEVATOR_INSERT_SORT: | |
373 | BUG_ON(!blk_fs_request(rq)); | |
374 | rq->flags |= REQ_SORTED; | |
375 | q->nr_sorted++; | |
376 | if (q->last_merge == NULL && rq_mergeable(rq)) | |
377 | q->last_merge = rq; | |
378 | /* | |
379 | * Some ioscheds (cfq) run q->request_fn directly, so | |
380 | * rq cannot be accessed after calling | |
381 | * elevator_add_req_fn. | |
382 | */ | |
383 | q->elevator->ops->elevator_add_req_fn(q, rq); | |
384 | break; | |
385 | ||
386 | case ELEVATOR_INSERT_REQUEUE: | |
387 | /* | |
388 | * If ordered flush isn't in progress, we do front | |
389 | * insertion; otherwise, requests should be requeued | |
390 | * in ordseq order. | |
391 | */ | |
392 | rq->flags |= REQ_SOFTBARRIER; | |
393 | ||
394 | if (q->ordseq == 0) { | |
395 | list_add(&rq->queuelist, &q->queue_head); | |
396 | break; | |
397 | } | |
398 | ||
399 | ordseq = blk_ordered_req_seq(rq); | |
400 | ||
401 | list_for_each(pos, &q->queue_head) { | |
402 | struct request *pos_rq = list_entry_rq(pos); | |
403 | if (ordseq <= blk_ordered_req_seq(pos_rq)) | |
404 | break; | |
405 | } | |
406 | ||
407 | list_add_tail(&rq->queuelist, pos); | |
408 | break; | |
409 | ||
410 | default: | |
411 | printk(KERN_ERR "%s: bad insertion point %d\n", | |
412 | __FUNCTION__, where); | |
413 | BUG(); | |
414 | } | |
415 | ||
416 | if (blk_queue_plugged(q)) { | |
417 | int nrq = q->rq.count[READ] + q->rq.count[WRITE] | |
418 | - q->in_flight; | |
419 | ||
420 | if (nrq >= q->unplug_thresh) | |
421 | __generic_unplug_device(q); | |
422 | } | |
423 | } | |
424 | ||
425 | void elv_add_request(request_queue_t *q, struct request *rq, int where, | |
426 | int plug) | |
427 | { | |
428 | unsigned long flags; | |
429 | ||
430 | spin_lock_irqsave(q->queue_lock, flags); | |
431 | __elv_add_request(q, rq, where, plug); | |
432 | spin_unlock_irqrestore(q->queue_lock, flags); | |
433 | } | |
434 | ||
435 | static inline struct request *__elv_next_request(request_queue_t *q) | |
436 | { | |
437 | struct request *rq; | |
438 | ||
439 | while (1) { | |
440 | while (!list_empty(&q->queue_head)) { | |
441 | rq = list_entry_rq(q->queue_head.next); | |
442 | if (blk_do_ordered(q, &rq)) | |
443 | return rq; | |
444 | } | |
445 | ||
446 | if (!q->elevator->ops->elevator_dispatch_fn(q, 0)) | |
447 | return NULL; | |
448 | } | |
449 | } | |
450 | ||
451 | struct request *elv_next_request(request_queue_t *q) | |
452 | { | |
453 | struct request *rq; | |
454 | int ret; | |
455 | ||
456 | while ((rq = __elv_next_request(q)) != NULL) { | |
457 | if (!(rq->flags & REQ_STARTED)) { | |
458 | elevator_t *e = q->elevator; | |
459 | ||
460 | /* | |
461 | * This is the first time the device driver | |
462 | * sees this request (possibly after | |
463 | * requeueing). Notify IO scheduler. | |
464 | */ | |
465 | if (blk_sorted_rq(rq) && | |
466 | e->ops->elevator_activate_req_fn) | |
467 | e->ops->elevator_activate_req_fn(q, rq); | |
468 | ||
469 | /* | |
470 | * just mark as started even if we don't start | |
471 | * it, a request that has been delayed should | |
472 | * not be passed by new incoming requests | |
473 | */ | |
474 | rq->flags |= REQ_STARTED; | |
475 | } | |
476 | ||
477 | if (!q->boundary_rq || q->boundary_rq == rq) { | |
478 | q->end_sector = rq_end_sector(rq); | |
479 | q->boundary_rq = NULL; | |
480 | } | |
481 | ||
482 | if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn) | |
483 | break; | |
484 | ||
485 | ret = q->prep_rq_fn(q, rq); | |
486 | if (ret == BLKPREP_OK) { | |
487 | break; | |
488 | } else if (ret == BLKPREP_DEFER) { | |
489 | /* | |
490 | * the request may have been (partially) prepped. | |
491 | * we need to keep this request in the front to | |
492 | * avoid resource deadlock. REQ_STARTED will | |
493 | * prevent other fs requests from passing this one. | |
494 | */ | |
495 | rq = NULL; | |
496 | break; | |
497 | } else if (ret == BLKPREP_KILL) { | |
498 | int nr_bytes = rq->hard_nr_sectors << 9; | |
499 | ||
500 | if (!nr_bytes) | |
501 | nr_bytes = rq->data_len; | |
502 | ||
503 | blkdev_dequeue_request(rq); | |
504 | rq->flags |= REQ_QUIET; | |
505 | end_that_request_chunk(rq, 0, nr_bytes); | |
506 | end_that_request_last(rq, 0); | |
507 | } else { | |
508 | printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__, | |
509 | ret); | |
510 | break; | |
511 | } | |
512 | } | |
513 | ||
514 | return rq; | |
515 | } | |
516 | ||
517 | void elv_dequeue_request(request_queue_t *q, struct request *rq) | |
518 | { | |
519 | BUG_ON(list_empty(&rq->queuelist)); | |
520 | ||
521 | list_del_init(&rq->queuelist); | |
522 | ||
523 | /* | |
524 | * the time frame between a request being removed from the lists | |
525 | * and to it is freed is accounted as io that is in progress at | |
526 | * the driver side. | |
527 | */ | |
528 | if (blk_account_rq(rq)) | |
529 | q->in_flight++; | |
530 | } | |
531 | ||
532 | int elv_queue_empty(request_queue_t *q) | |
533 | { | |
534 | elevator_t *e = q->elevator; | |
535 | ||
536 | if (!list_empty(&q->queue_head)) | |
537 | return 0; | |
538 | ||
539 | if (e->ops->elevator_queue_empty_fn) | |
540 | return e->ops->elevator_queue_empty_fn(q); | |
541 | ||
542 | return 1; | |
543 | } | |
544 | ||
545 | struct request *elv_latter_request(request_queue_t *q, struct request *rq) | |
546 | { | |
547 | elevator_t *e = q->elevator; | |
548 | ||
549 | if (e->ops->elevator_latter_req_fn) | |
550 | return e->ops->elevator_latter_req_fn(q, rq); | |
551 | return NULL; | |
552 | } | |
553 | ||
554 | struct request *elv_former_request(request_queue_t *q, struct request *rq) | |
555 | { | |
556 | elevator_t *e = q->elevator; | |
557 | ||
558 | if (e->ops->elevator_former_req_fn) | |
559 | return e->ops->elevator_former_req_fn(q, rq); | |
560 | return NULL; | |
561 | } | |
562 | ||
563 | int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio, | |
564 | gfp_t gfp_mask) | |
565 | { | |
566 | elevator_t *e = q->elevator; | |
567 | ||
568 | if (e->ops->elevator_set_req_fn) | |
569 | return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask); | |
570 | ||
571 | rq->elevator_private = NULL; | |
572 | return 0; | |
573 | } | |
574 | ||
575 | void elv_put_request(request_queue_t *q, struct request *rq) | |
576 | { | |
577 | elevator_t *e = q->elevator; | |
578 | ||
579 | if (e->ops->elevator_put_req_fn) | |
580 | e->ops->elevator_put_req_fn(q, rq); | |
581 | } | |
582 | ||
583 | int elv_may_queue(request_queue_t *q, int rw, struct bio *bio) | |
584 | { | |
585 | elevator_t *e = q->elevator; | |
586 | ||
587 | if (e->ops->elevator_may_queue_fn) | |
588 | return e->ops->elevator_may_queue_fn(q, rw, bio); | |
589 | ||
590 | return ELV_MQUEUE_MAY; | |
591 | } | |
592 | ||
593 | void elv_completed_request(request_queue_t *q, struct request *rq) | |
594 | { | |
595 | elevator_t *e = q->elevator; | |
596 | ||
597 | /* | |
598 | * request is released from the driver, io must be done | |
599 | */ | |
600 | if (blk_account_rq(rq)) { | |
601 | q->in_flight--; | |
602 | if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn) | |
603 | e->ops->elevator_completed_req_fn(q, rq); | |
604 | } | |
605 | ||
606 | /* | |
607 | * Check if the queue is waiting for fs requests to be | |
608 | * drained for flush sequence. | |
609 | */ | |
610 | if (unlikely(q->ordseq)) { | |
611 | struct request *first_rq = list_entry_rq(q->queue_head.next); | |
612 | if (q->in_flight == 0 && | |
613 | blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN && | |
614 | blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) { | |
615 | blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0); | |
616 | q->request_fn(q); | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
621 | int elv_register_queue(struct request_queue *q) | |
622 | { | |
623 | elevator_t *e = q->elevator; | |
624 | ||
625 | e->kobj.parent = kobject_get(&q->kobj); | |
626 | if (!e->kobj.parent) | |
627 | return -EBUSY; | |
628 | ||
629 | snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched"); | |
630 | e->kobj.ktype = e->elevator_type->elevator_ktype; | |
631 | ||
632 | return kobject_register(&e->kobj); | |
633 | } | |
634 | ||
635 | void elv_unregister_queue(struct request_queue *q) | |
636 | { | |
637 | if (q) { | |
638 | elevator_t *e = q->elevator; | |
639 | kobject_unregister(&e->kobj); | |
640 | kobject_put(&q->kobj); | |
641 | } | |
642 | } | |
643 | ||
644 | int elv_register(struct elevator_type *e) | |
645 | { | |
646 | spin_lock_irq(&elv_list_lock); | |
647 | if (elevator_find(e->elevator_name)) | |
648 | BUG(); | |
649 | list_add_tail(&e->list, &elv_list); | |
650 | spin_unlock_irq(&elv_list_lock); | |
651 | ||
652 | printk(KERN_INFO "io scheduler %s registered", e->elevator_name); | |
653 | if (!strcmp(e->elevator_name, chosen_elevator) || | |
654 | (!*chosen_elevator && | |
655 | !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED))) | |
656 | printk(" (default)"); | |
657 | printk("\n"); | |
658 | return 0; | |
659 | } | |
660 | EXPORT_SYMBOL_GPL(elv_register); | |
661 | ||
662 | void elv_unregister(struct elevator_type *e) | |
663 | { | |
664 | struct task_struct *g, *p; | |
665 | ||
666 | /* | |
667 | * Iterate every thread in the process to remove the io contexts. | |
668 | */ | |
669 | read_lock(&tasklist_lock); | |
670 | do_each_thread(g, p) { | |
671 | struct io_context *ioc = p->io_context; | |
672 | if (ioc && ioc->cic) { | |
673 | ioc->cic->exit(ioc->cic); | |
674 | ioc->cic->dtor(ioc->cic); | |
675 | ioc->cic = NULL; | |
676 | } | |
677 | if (ioc && ioc->aic) { | |
678 | ioc->aic->exit(ioc->aic); | |
679 | ioc->aic->dtor(ioc->aic); | |
680 | ioc->aic = NULL; | |
681 | } | |
682 | } while_each_thread(g, p); | |
683 | read_unlock(&tasklist_lock); | |
684 | ||
685 | spin_lock_irq(&elv_list_lock); | |
686 | list_del_init(&e->list); | |
687 | spin_unlock_irq(&elv_list_lock); | |
688 | } | |
689 | EXPORT_SYMBOL_GPL(elv_unregister); | |
690 | ||
691 | /* | |
692 | * switch to new_e io scheduler. be careful not to introduce deadlocks - | |
693 | * we don't free the old io scheduler, before we have allocated what we | |
694 | * need for the new one. this way we have a chance of going back to the old | |
695 | * one, if the new one fails init for some reason. | |
696 | */ | |
697 | static void elevator_switch(request_queue_t *q, struct elevator_type *new_e) | |
698 | { | |
699 | elevator_t *old_elevator, *e; | |
700 | ||
701 | /* | |
702 | * Allocate new elevator | |
703 | */ | |
704 | e = kmalloc(sizeof(elevator_t), GFP_KERNEL); | |
705 | if (!e) | |
706 | goto error; | |
707 | ||
708 | /* | |
709 | * Turn on BYPASS and drain all requests w/ elevator private data | |
710 | */ | |
711 | spin_lock_irq(q->queue_lock); | |
712 | ||
713 | set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); | |
714 | ||
715 | elv_drain_elevator(q); | |
716 | ||
717 | while (q->rq.elvpriv) { | |
718 | blk_remove_plug(q); | |
719 | q->request_fn(q); | |
720 | spin_unlock_irq(q->queue_lock); | |
721 | msleep(10); | |
722 | spin_lock_irq(q->queue_lock); | |
723 | elv_drain_elevator(q); | |
724 | } | |
725 | ||
726 | spin_unlock_irq(q->queue_lock); | |
727 | ||
728 | /* | |
729 | * unregister old elevator data | |
730 | */ | |
731 | elv_unregister_queue(q); | |
732 | old_elevator = q->elevator; | |
733 | ||
734 | /* | |
735 | * attach and start new elevator | |
736 | */ | |
737 | if (elevator_attach(q, new_e, e)) | |
738 | goto fail; | |
739 | ||
740 | if (elv_register_queue(q)) | |
741 | goto fail_register; | |
742 | ||
743 | /* | |
744 | * finally exit old elevator and turn off BYPASS. | |
745 | */ | |
746 | elevator_exit(old_elevator); | |
747 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); | |
748 | return; | |
749 | ||
750 | fail_register: | |
751 | /* | |
752 | * switch failed, exit the new io scheduler and reattach the old | |
753 | * one again (along with re-adding the sysfs dir) | |
754 | */ | |
755 | elevator_exit(e); | |
756 | e = NULL; | |
757 | fail: | |
758 | q->elevator = old_elevator; | |
759 | elv_register_queue(q); | |
760 | clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); | |
761 | kfree(e); | |
762 | error: | |
763 | elevator_put(new_e); | |
764 | printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name); | |
765 | } | |
766 | ||
767 | ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count) | |
768 | { | |
769 | char elevator_name[ELV_NAME_MAX]; | |
770 | size_t len; | |
771 | struct elevator_type *e; | |
772 | ||
773 | elevator_name[sizeof(elevator_name) - 1] = '\0'; | |
774 | strncpy(elevator_name, name, sizeof(elevator_name) - 1); | |
775 | len = strlen(elevator_name); | |
776 | ||
777 | if (len && elevator_name[len - 1] == '\n') | |
778 | elevator_name[len - 1] = '\0'; | |
779 | ||
780 | e = elevator_get(elevator_name); | |
781 | if (!e) { | |
782 | printk(KERN_ERR "elevator: type %s not found\n", elevator_name); | |
783 | return -EINVAL; | |
784 | } | |
785 | ||
786 | if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) { | |
787 | elevator_put(e); | |
788 | return count; | |
789 | } | |
790 | ||
791 | elevator_switch(q, e); | |
792 | return count; | |
793 | } | |
794 | ||
795 | ssize_t elv_iosched_show(request_queue_t *q, char *name) | |
796 | { | |
797 | elevator_t *e = q->elevator; | |
798 | struct elevator_type *elv = e->elevator_type; | |
799 | struct list_head *entry; | |
800 | int len = 0; | |
801 | ||
802 | spin_lock_irq(q->queue_lock); | |
803 | list_for_each(entry, &elv_list) { | |
804 | struct elevator_type *__e; | |
805 | ||
806 | __e = list_entry(entry, struct elevator_type, list); | |
807 | if (!strcmp(elv->elevator_name, __e->elevator_name)) | |
808 | len += sprintf(name+len, "[%s] ", elv->elevator_name); | |
809 | else | |
810 | len += sprintf(name+len, "%s ", __e->elevator_name); | |
811 | } | |
812 | spin_unlock_irq(q->queue_lock); | |
813 | ||
814 | len += sprintf(len+name, "\n"); | |
815 | return len; | |
816 | } | |
817 | ||
818 | EXPORT_SYMBOL(elv_dispatch_sort); | |
819 | EXPORT_SYMBOL(elv_add_request); | |
820 | EXPORT_SYMBOL(__elv_add_request); | |
821 | EXPORT_SYMBOL(elv_requeue_request); | |
822 | EXPORT_SYMBOL(elv_next_request); | |
823 | EXPORT_SYMBOL(elv_dequeue_request); | |
824 | EXPORT_SYMBOL(elv_queue_empty); | |
825 | EXPORT_SYMBOL(elv_completed_request); | |
826 | EXPORT_SYMBOL(elevator_exit); | |
827 | EXPORT_SYMBOL(elevator_init); |