dir_index: error out instead of BUG on corrupt dx dirs
[linux-2.6/mini2440.git] / drivers / md / dm-delay.c
blob6928c136d3c540d8fdcec9730216f436e1cd9131
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 "dm.h"
17 #include "dm-bio-list.h"
19 #define DM_MSG_PREFIX "delay"
21 struct delay_c {
22 struct timer_list delay_timer;
23 struct mutex timer_lock;
24 struct work_struct flush_expired_bios;
25 struct list_head delayed_bios;
26 atomic_t may_delay;
27 mempool_t *delayed_pool;
29 struct dm_dev *dev_read;
30 sector_t start_read;
31 unsigned read_delay;
32 unsigned reads;
34 struct dm_dev *dev_write;
35 sector_t start_write;
36 unsigned write_delay;
37 unsigned writes;
40 struct dm_delay_info {
41 struct delay_c *context;
42 struct list_head list;
43 struct bio *bio;
44 unsigned long expires;
47 static DEFINE_MUTEX(delayed_bios_lock);
49 static struct workqueue_struct *kdelayd_wq;
50 static struct kmem_cache *delayed_cache;
52 static void handle_delayed_timer(unsigned long data)
54 struct delay_c *dc = (struct delay_c *)data;
56 queue_work(kdelayd_wq, &dc->flush_expired_bios);
59 static void queue_timeout(struct delay_c *dc, unsigned long expires)
61 mutex_lock(&dc->timer_lock);
63 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
64 mod_timer(&dc->delay_timer, expires);
66 mutex_unlock(&dc->timer_lock);
69 static void flush_bios(struct bio *bio)
71 struct bio *n;
73 while (bio) {
74 n = bio->bi_next;
75 bio->bi_next = NULL;
76 generic_make_request(bio);
77 bio = n;
81 static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
83 struct dm_delay_info *delayed, *next;
84 unsigned long next_expires = 0;
85 int start_timer = 0;
86 BIO_LIST(flush_bios);
88 mutex_lock(&delayed_bios_lock);
89 list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
90 if (flush_all || time_after_eq(jiffies, delayed->expires)) {
91 list_del(&delayed->list);
92 bio_list_add(&flush_bios, delayed->bio);
93 if ((bio_data_dir(delayed->bio) == WRITE))
94 delayed->context->writes--;
95 else
96 delayed->context->reads--;
97 mempool_free(delayed, dc->delayed_pool);
98 continue;
101 if (!start_timer) {
102 start_timer = 1;
103 next_expires = delayed->expires;
104 } else
105 next_expires = min(next_expires, delayed->expires);
108 mutex_unlock(&delayed_bios_lock);
110 if (start_timer)
111 queue_timeout(dc, next_expires);
113 return bio_list_get(&flush_bios);
116 static void flush_expired_bios(struct work_struct *work)
118 struct delay_c *dc;
120 dc = container_of(work, struct delay_c, flush_expired_bios);
121 flush_bios(flush_delayed_bios(dc, 0));
125 * Mapping parameters:
126 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
128 * With separate write parameters, the first set is only used for reads.
129 * Delays are specified in milliseconds.
131 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
133 struct delay_c *dc;
134 unsigned long long tmpll;
136 if (argc != 3 && argc != 6) {
137 ti->error = "requires exactly 3 or 6 arguments";
138 return -EINVAL;
141 dc = kmalloc(sizeof(*dc), GFP_KERNEL);
142 if (!dc) {
143 ti->error = "Cannot allocate context";
144 return -ENOMEM;
147 dc->reads = dc->writes = 0;
149 if (sscanf(argv[1], "%llu", &tmpll) != 1) {
150 ti->error = "Invalid device sector";
151 goto bad;
153 dc->start_read = tmpll;
155 if (sscanf(argv[2], "%u", &dc->read_delay) != 1) {
156 ti->error = "Invalid delay";
157 goto bad;
160 if (dm_get_device(ti, argv[0], dc->start_read, ti->len,
161 dm_table_get_mode(ti->table), &dc->dev_read)) {
162 ti->error = "Device lookup failed";
163 goto bad;
166 if (argc == 3) {
167 dc->dev_write = NULL;
168 goto out;
171 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
172 ti->error = "Invalid write device sector";
173 goto bad;
175 dc->start_write = tmpll;
177 if (sscanf(argv[5], "%u", &dc->write_delay) != 1) {
178 ti->error = "Invalid write delay";
179 goto bad;
182 if (dm_get_device(ti, argv[3], dc->start_write, ti->len,
183 dm_table_get_mode(ti->table), &dc->dev_write)) {
184 ti->error = "Write device lookup failed";
185 dm_put_device(ti, dc->dev_read);
186 goto bad;
189 out:
190 dc->delayed_pool = mempool_create_slab_pool(128, delayed_cache);
191 if (!dc->delayed_pool) {
192 DMERR("Couldn't create delayed bio pool.");
193 goto bad;
196 setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
198 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
199 INIT_LIST_HEAD(&dc->delayed_bios);
200 mutex_init(&dc->timer_lock);
201 atomic_set(&dc->may_delay, 1);
203 ti->private = dc;
204 return 0;
206 bad:
207 kfree(dc);
208 return -EINVAL;
211 static void delay_dtr(struct dm_target *ti)
213 struct delay_c *dc = ti->private;
215 flush_workqueue(kdelayd_wq);
217 dm_put_device(ti, dc->dev_read);
219 if (dc->dev_write)
220 dm_put_device(ti, dc->dev_write);
222 mempool_destroy(dc->delayed_pool);
223 kfree(dc);
226 static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
228 struct dm_delay_info *delayed;
229 unsigned long expires = 0;
231 if (!delay || !atomic_read(&dc->may_delay))
232 return 1;
234 delayed = mempool_alloc(dc->delayed_pool, GFP_NOIO);
236 delayed->context = dc;
237 delayed->bio = bio;
238 delayed->expires = expires = jiffies + (delay * HZ / 1000);
240 mutex_lock(&delayed_bios_lock);
242 if (bio_data_dir(bio) == WRITE)
243 dc->writes++;
244 else
245 dc->reads++;
247 list_add_tail(&delayed->list, &dc->delayed_bios);
249 mutex_unlock(&delayed_bios_lock);
251 queue_timeout(dc, expires);
253 return 0;
256 static void delay_presuspend(struct dm_target *ti)
258 struct delay_c *dc = ti->private;
260 atomic_set(&dc->may_delay, 0);
261 del_timer_sync(&dc->delay_timer);
262 flush_bios(flush_delayed_bios(dc, 1));
265 static void delay_resume(struct dm_target *ti)
267 struct delay_c *dc = ti->private;
269 atomic_set(&dc->may_delay, 1);
272 static int delay_map(struct dm_target *ti, struct bio *bio,
273 union map_info *map_context)
275 struct delay_c *dc = ti->private;
277 if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
278 bio->bi_bdev = dc->dev_write->bdev;
279 bio->bi_sector = dc->start_write +
280 (bio->bi_sector - ti->begin);
282 return delay_bio(dc, dc->write_delay, bio);
285 bio->bi_bdev = dc->dev_read->bdev;
286 bio->bi_sector = dc->start_read +
287 (bio->bi_sector - ti->begin);
289 return delay_bio(dc, dc->read_delay, bio);
292 static int delay_status(struct dm_target *ti, status_type_t type,
293 char *result, unsigned maxlen)
295 struct delay_c *dc = ti->private;
296 int sz = 0;
298 switch (type) {
299 case STATUSTYPE_INFO:
300 DMEMIT("%u %u", dc->reads, dc->writes);
301 break;
303 case STATUSTYPE_TABLE:
304 DMEMIT("%s %llu %u", dc->dev_read->name,
305 (unsigned long long) dc->start_read,
306 dc->read_delay);
307 if (dc->dev_write)
308 DMEMIT("%s %llu %u", dc->dev_write->name,
309 (unsigned long long) dc->start_write,
310 dc->write_delay);
311 break;
314 return 0;
317 static struct target_type delay_target = {
318 .name = "delay",
319 .version = {1, 0, 2},
320 .module = THIS_MODULE,
321 .ctr = delay_ctr,
322 .dtr = delay_dtr,
323 .map = delay_map,
324 .presuspend = delay_presuspend,
325 .resume = delay_resume,
326 .status = delay_status,
329 static int __init dm_delay_init(void)
331 int r = -ENOMEM;
333 kdelayd_wq = create_workqueue("kdelayd");
334 if (!kdelayd_wq) {
335 DMERR("Couldn't start kdelayd");
336 goto bad_queue;
339 delayed_cache = KMEM_CACHE(dm_delay_info, 0);
340 if (!delayed_cache) {
341 DMERR("Couldn't create delayed bio cache.");
342 goto bad_memcache;
345 r = dm_register_target(&delay_target);
346 if (r < 0) {
347 DMERR("register failed %d", r);
348 goto bad_register;
351 return 0;
353 bad_register:
354 kmem_cache_destroy(delayed_cache);
355 bad_memcache:
356 destroy_workqueue(kdelayd_wq);
357 bad_queue:
358 return r;
361 static void __exit dm_delay_exit(void)
363 int r = dm_unregister_target(&delay_target);
365 if (r < 0)
366 DMERR("unregister failed %d", r);
368 kmem_cache_destroy(delayed_cache);
369 destroy_workqueue(kdelayd_wq);
372 /* Module hooks */
373 module_init(dm_delay_init);
374 module_exit(dm_delay_exit);
376 MODULE_DESCRIPTION(DM_NAME " delay target");
377 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
378 MODULE_LICENSE("GPL");