ACPI: pci_root: simplify list traversals
[linux-2.6/linux-acpi-2.6.git] / drivers / md / dm-delay.c
blob559dbb52bc851039702692e201a00a700e402187
1 /*
2 * Copyright (C) 2005-2007 Red Hat GmbH
4 * A target that delays reads and/or writes and can send
5 * them to different devices.
7 * This file is released under the GPL.
8 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/bio.h>
14 #include <linux/slab.h>
16 #include <linux/device-mapper.h>
18 #define DM_MSG_PREFIX "delay"
20 struct delay_c {
21 struct timer_list delay_timer;
22 struct mutex timer_lock;
23 struct work_struct flush_expired_bios;
24 struct list_head delayed_bios;
25 atomic_t may_delay;
26 mempool_t *delayed_pool;
28 struct dm_dev *dev_read;
29 sector_t start_read;
30 unsigned read_delay;
31 unsigned reads;
33 struct dm_dev *dev_write;
34 sector_t start_write;
35 unsigned write_delay;
36 unsigned writes;
39 struct dm_delay_info {
40 struct delay_c *context;
41 struct list_head list;
42 struct bio *bio;
43 unsigned long expires;
46 static DEFINE_MUTEX(delayed_bios_lock);
48 static struct workqueue_struct *kdelayd_wq;
49 static struct kmem_cache *delayed_cache;
51 static void handle_delayed_timer(unsigned long data)
53 struct delay_c *dc = (struct delay_c *)data;
55 queue_work(kdelayd_wq, &dc->flush_expired_bios);
58 static void queue_timeout(struct delay_c *dc, unsigned long expires)
60 mutex_lock(&dc->timer_lock);
62 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
63 mod_timer(&dc->delay_timer, expires);
65 mutex_unlock(&dc->timer_lock);
68 static void flush_bios(struct bio *bio)
70 struct bio *n;
72 while (bio) {
73 n = bio->bi_next;
74 bio->bi_next = NULL;
75 generic_make_request(bio);
76 bio = n;
80 static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
82 struct dm_delay_info *delayed, *next;
83 unsigned long next_expires = 0;
84 int start_timer = 0;
85 struct bio_list flush_bios = { };
87 mutex_lock(&delayed_bios_lock);
88 list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
89 if (flush_all || time_after_eq(jiffies, delayed->expires)) {
90 list_del(&delayed->list);
91 bio_list_add(&flush_bios, delayed->bio);
92 if ((bio_data_dir(delayed->bio) == WRITE))
93 delayed->context->writes--;
94 else
95 delayed->context->reads--;
96 mempool_free(delayed, dc->delayed_pool);
97 continue;
100 if (!start_timer) {
101 start_timer = 1;
102 next_expires = delayed->expires;
103 } else
104 next_expires = min(next_expires, delayed->expires);
107 mutex_unlock(&delayed_bios_lock);
109 if (start_timer)
110 queue_timeout(dc, next_expires);
112 return bio_list_get(&flush_bios);
115 static void flush_expired_bios(struct work_struct *work)
117 struct delay_c *dc;
119 dc = container_of(work, struct delay_c, flush_expired_bios);
120 flush_bios(flush_delayed_bios(dc, 0));
124 * Mapping parameters:
125 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
127 * With separate write parameters, the first set is only used for reads.
128 * Delays are specified in milliseconds.
130 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
132 struct delay_c *dc;
133 unsigned long long tmpll;
135 if (argc != 3 && argc != 6) {
136 ti->error = "requires exactly 3 or 6 arguments";
137 return -EINVAL;
140 dc = kmalloc(sizeof(*dc), GFP_KERNEL);
141 if (!dc) {
142 ti->error = "Cannot allocate context";
143 return -ENOMEM;
146 dc->reads = dc->writes = 0;
148 if (sscanf(argv[1], "%llu", &tmpll) != 1) {
149 ti->error = "Invalid device sector";
150 goto bad;
152 dc->start_read = tmpll;
154 if (sscanf(argv[2], "%u", &dc->read_delay) != 1) {
155 ti->error = "Invalid delay";
156 goto bad;
159 if (dm_get_device(ti, argv[0], dc->start_read, ti->len,
160 dm_table_get_mode(ti->table), &dc->dev_read)) {
161 ti->error = "Device lookup failed";
162 goto bad;
165 dc->dev_write = NULL;
166 if (argc == 3)
167 goto out;
169 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
170 ti->error = "Invalid write device sector";
171 goto bad_dev_read;
173 dc->start_write = tmpll;
175 if (sscanf(argv[5], "%u", &dc->write_delay) != 1) {
176 ti->error = "Invalid write delay";
177 goto bad_dev_read;
180 if (dm_get_device(ti, argv[3], dc->start_write, ti->len,
181 dm_table_get_mode(ti->table), &dc->dev_write)) {
182 ti->error = "Write device lookup failed";
183 goto bad_dev_read;
186 out:
187 dc->delayed_pool = mempool_create_slab_pool(128, delayed_cache);
188 if (!dc->delayed_pool) {
189 DMERR("Couldn't create delayed bio pool.");
190 goto bad_dev_write;
193 setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
195 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
196 INIT_LIST_HEAD(&dc->delayed_bios);
197 mutex_init(&dc->timer_lock);
198 atomic_set(&dc->may_delay, 1);
200 ti->private = dc;
201 return 0;
203 bad_dev_write:
204 if (dc->dev_write)
205 dm_put_device(ti, dc->dev_write);
206 bad_dev_read:
207 dm_put_device(ti, dc->dev_read);
208 bad:
209 kfree(dc);
210 return -EINVAL;
213 static void delay_dtr(struct dm_target *ti)
215 struct delay_c *dc = ti->private;
217 flush_workqueue(kdelayd_wq);
219 dm_put_device(ti, dc->dev_read);
221 if (dc->dev_write)
222 dm_put_device(ti, dc->dev_write);
224 mempool_destroy(dc->delayed_pool);
225 kfree(dc);
228 static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
230 struct dm_delay_info *delayed;
231 unsigned long expires = 0;
233 if (!delay || !atomic_read(&dc->may_delay))
234 return 1;
236 delayed = mempool_alloc(dc->delayed_pool, GFP_NOIO);
238 delayed->context = dc;
239 delayed->bio = bio;
240 delayed->expires = expires = jiffies + (delay * HZ / 1000);
242 mutex_lock(&delayed_bios_lock);
244 if (bio_data_dir(bio) == WRITE)
245 dc->writes++;
246 else
247 dc->reads++;
249 list_add_tail(&delayed->list, &dc->delayed_bios);
251 mutex_unlock(&delayed_bios_lock);
253 queue_timeout(dc, expires);
255 return 0;
258 static void delay_presuspend(struct dm_target *ti)
260 struct delay_c *dc = ti->private;
262 atomic_set(&dc->may_delay, 0);
263 del_timer_sync(&dc->delay_timer);
264 flush_bios(flush_delayed_bios(dc, 1));
267 static void delay_resume(struct dm_target *ti)
269 struct delay_c *dc = ti->private;
271 atomic_set(&dc->may_delay, 1);
274 static int delay_map(struct dm_target *ti, struct bio *bio,
275 union map_info *map_context)
277 struct delay_c *dc = ti->private;
279 if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
280 bio->bi_bdev = dc->dev_write->bdev;
281 bio->bi_sector = dc->start_write +
282 (bio->bi_sector - ti->begin);
284 return delay_bio(dc, dc->write_delay, bio);
287 bio->bi_bdev = dc->dev_read->bdev;
288 bio->bi_sector = dc->start_read +
289 (bio->bi_sector - ti->begin);
291 return delay_bio(dc, dc->read_delay, bio);
294 static int delay_status(struct dm_target *ti, status_type_t type,
295 char *result, unsigned maxlen)
297 struct delay_c *dc = ti->private;
298 int sz = 0;
300 switch (type) {
301 case STATUSTYPE_INFO:
302 DMEMIT("%u %u", dc->reads, dc->writes);
303 break;
305 case STATUSTYPE_TABLE:
306 DMEMIT("%s %llu %u", dc->dev_read->name,
307 (unsigned long long) dc->start_read,
308 dc->read_delay);
309 if (dc->dev_write)
310 DMEMIT(" %s %llu %u", dc->dev_write->name,
311 (unsigned long long) dc->start_write,
312 dc->write_delay);
313 break;
316 return 0;
319 static struct target_type delay_target = {
320 .name = "delay",
321 .version = {1, 0, 2},
322 .module = THIS_MODULE,
323 .ctr = delay_ctr,
324 .dtr = delay_dtr,
325 .map = delay_map,
326 .presuspend = delay_presuspend,
327 .resume = delay_resume,
328 .status = delay_status,
331 static int __init dm_delay_init(void)
333 int r = -ENOMEM;
335 kdelayd_wq = create_workqueue("kdelayd");
336 if (!kdelayd_wq) {
337 DMERR("Couldn't start kdelayd");
338 goto bad_queue;
341 delayed_cache = KMEM_CACHE(dm_delay_info, 0);
342 if (!delayed_cache) {
343 DMERR("Couldn't create delayed bio cache.");
344 goto bad_memcache;
347 r = dm_register_target(&delay_target);
348 if (r < 0) {
349 DMERR("register failed %d", r);
350 goto bad_register;
353 return 0;
355 bad_register:
356 kmem_cache_destroy(delayed_cache);
357 bad_memcache:
358 destroy_workqueue(kdelayd_wq);
359 bad_queue:
360 return r;
363 static void __exit dm_delay_exit(void)
365 dm_unregister_target(&delay_target);
366 kmem_cache_destroy(delayed_cache);
367 destroy_workqueue(kdelayd_wq);
370 /* Module hooks */
371 module_init(dm_delay_init);
372 module_exit(dm_delay_exit);
374 MODULE_DESCRIPTION(DM_NAME " delay target");
375 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
376 MODULE_LICENSE("GPL");