blob: bdd37f881c42ab757cb381514db10d4d232b854e [file] [log] [blame]
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -07001/*
2 * Copyright (C) 2005-2007 Red Hat GmbH
3 *
4 * A target that delays reads and/or writes and can send
5 * them to different devices.
6 *
7 * This file is released under the GPL.
8 */
9
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>
15
16#include "dm.h"
17#include "dm-bio-list.h"
18
19#define DM_MSG_PREFIX "delay"
20
21struct delay_c {
22 struct timer_list delay_timer;
Alasdair G Kergonac818642007-07-12 17:26:47 +010023 struct mutex timer_lock;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070024 struct work_struct flush_expired_bios;
25 struct list_head delayed_bios;
26 atomic_t may_delay;
27 mempool_t *delayed_pool;
28
29 struct dm_dev *dev_read;
30 sector_t start_read;
31 unsigned read_delay;
32 unsigned reads;
33
34 struct dm_dev *dev_write;
35 sector_t start_write;
36 unsigned write_delay;
37 unsigned writes;
38};
39
Alasdair G Kergon028867a2007-07-12 17:26:32 +010040struct dm_delay_info {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070041 struct delay_c *context;
42 struct list_head list;
43 struct bio *bio;
44 unsigned long expires;
45};
46
47static DEFINE_MUTEX(delayed_bios_lock);
48
49static struct workqueue_struct *kdelayd_wq;
50static struct kmem_cache *delayed_cache;
51
52static void handle_delayed_timer(unsigned long data)
53{
54 struct delay_c *dc = (struct delay_c *)data;
55
56 queue_work(kdelayd_wq, &dc->flush_expired_bios);
57}
58
59static void queue_timeout(struct delay_c *dc, unsigned long expires)
60{
Alasdair G Kergonac818642007-07-12 17:26:47 +010061 mutex_lock(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070062
63 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
64 mod_timer(&dc->delay_timer, expires);
65
Alasdair G Kergonac818642007-07-12 17:26:47 +010066 mutex_unlock(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070067}
68
69static void flush_bios(struct bio *bio)
70{
71 struct bio *n;
72
73 while (bio) {
74 n = bio->bi_next;
75 bio->bi_next = NULL;
76 generic_make_request(bio);
77 bio = n;
78 }
79}
80
81static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
82{
Alasdair G Kergon028867a2007-07-12 17:26:32 +010083 struct dm_delay_info *delayed, *next;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070084 unsigned long next_expires = 0;
85 int start_timer = 0;
Alasdair G Kergon051814c2007-10-19 22:38:55 +010086 struct bio_list flush_bios = { };
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070087
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;
99 }
100
101 if (!start_timer) {
102 start_timer = 1;
103 next_expires = delayed->expires;
104 } else
105 next_expires = min(next_expires, delayed->expires);
106 }
107
108 mutex_unlock(&delayed_bios_lock);
109
110 if (start_timer)
111 queue_timeout(dc, next_expires);
112
113 return bio_list_get(&flush_bios);
114}
115
116static void flush_expired_bios(struct work_struct *work)
117{
118 struct delay_c *dc;
119
120 dc = container_of(work, struct delay_c, flush_expired_bios);
121 flush_bios(flush_delayed_bios(dc, 0));
122}
123
124/*
125 * Mapping parameters:
126 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
127 *
128 * With separate write parameters, the first set is only used for reads.
129 * Delays are specified in milliseconds.
130 */
131static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
132{
133 struct delay_c *dc;
134 unsigned long long tmpll;
135
136 if (argc != 3 && argc != 6) {
137 ti->error = "requires exactly 3 or 6 arguments";
138 return -EINVAL;
139 }
140
141 dc = kmalloc(sizeof(*dc), GFP_KERNEL);
142 if (!dc) {
143 ti->error = "Cannot allocate context";
144 return -ENOMEM;
145 }
146
147 dc->reads = dc->writes = 0;
148
149 if (sscanf(argv[1], "%llu", &tmpll) != 1) {
150 ti->error = "Invalid device sector";
151 goto bad;
152 }
153 dc->start_read = tmpll;
154
155 if (sscanf(argv[2], "%u", &dc->read_delay) != 1) {
156 ti->error = "Invalid delay";
157 goto bad;
158 }
159
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;
164 }
165
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100166 dc->dev_write = NULL;
167 if (argc == 3)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700168 goto out;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700169
170 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
171 ti->error = "Invalid write device sector";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100172 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700173 }
174 dc->start_write = tmpll;
175
176 if (sscanf(argv[5], "%u", &dc->write_delay) != 1) {
177 ti->error = "Invalid write delay";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100178 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700179 }
180
181 if (dm_get_device(ti, argv[3], dc->start_write, ti->len,
182 dm_table_get_mode(ti->table), &dc->dev_write)) {
183 ti->error = "Write device lookup failed";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100184 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700185 }
186
187out:
188 dc->delayed_pool = mempool_create_slab_pool(128, delayed_cache);
189 if (!dc->delayed_pool) {
190 DMERR("Couldn't create delayed bio pool.");
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100191 goto bad_dev_write;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700192 }
193
Alasdair G Kergonac818642007-07-12 17:26:47 +0100194 setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700195
196 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
197 INIT_LIST_HEAD(&dc->delayed_bios);
Alasdair G Kergonac818642007-07-12 17:26:47 +0100198 mutex_init(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700199 atomic_set(&dc->may_delay, 1);
200
201 ti->private = dc;
202 return 0;
203
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100204bad_dev_write:
205 if (dc->dev_write)
206 dm_put_device(ti, dc->dev_write);
207bad_dev_read:
208 dm_put_device(ti, dc->dev_read);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700209bad:
210 kfree(dc);
211 return -EINVAL;
212}
213
214static void delay_dtr(struct dm_target *ti)
215{
216 struct delay_c *dc = ti->private;
217
218 flush_workqueue(kdelayd_wq);
219
220 dm_put_device(ti, dc->dev_read);
221
222 if (dc->dev_write)
223 dm_put_device(ti, dc->dev_write);
224
225 mempool_destroy(dc->delayed_pool);
226 kfree(dc);
227}
228
229static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
230{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100231 struct dm_delay_info *delayed;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700232 unsigned long expires = 0;
233
234 if (!delay || !atomic_read(&dc->may_delay))
235 return 1;
236
237 delayed = mempool_alloc(dc->delayed_pool, GFP_NOIO);
238
239 delayed->context = dc;
240 delayed->bio = bio;
241 delayed->expires = expires = jiffies + (delay * HZ / 1000);
242
243 mutex_lock(&delayed_bios_lock);
244
245 if (bio_data_dir(bio) == WRITE)
246 dc->writes++;
247 else
248 dc->reads++;
249
250 list_add_tail(&delayed->list, &dc->delayed_bios);
251
252 mutex_unlock(&delayed_bios_lock);
253
254 queue_timeout(dc, expires);
255
256 return 0;
257}
258
259static void delay_presuspend(struct dm_target *ti)
260{
261 struct delay_c *dc = ti->private;
262
263 atomic_set(&dc->may_delay, 0);
264 del_timer_sync(&dc->delay_timer);
265 flush_bios(flush_delayed_bios(dc, 1));
266}
267
268static void delay_resume(struct dm_target *ti)
269{
270 struct delay_c *dc = ti->private;
271
272 atomic_set(&dc->may_delay, 1);
273}
274
275static int delay_map(struct dm_target *ti, struct bio *bio,
276 union map_info *map_context)
277{
278 struct delay_c *dc = ti->private;
279
280 if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
281 bio->bi_bdev = dc->dev_write->bdev;
282 bio->bi_sector = dc->start_write +
283 (bio->bi_sector - ti->begin);
284
285 return delay_bio(dc, dc->write_delay, bio);
286 }
287
288 bio->bi_bdev = dc->dev_read->bdev;
289 bio->bi_sector = dc->start_read +
290 (bio->bi_sector - ti->begin);
291
292 return delay_bio(dc, dc->read_delay, bio);
293}
294
295static int delay_status(struct dm_target *ti, status_type_t type,
296 char *result, unsigned maxlen)
297{
298 struct delay_c *dc = ti->private;
299 int sz = 0;
300
301 switch (type) {
302 case STATUSTYPE_INFO:
303 DMEMIT("%u %u", dc->reads, dc->writes);
304 break;
305
306 case STATUSTYPE_TABLE:
307 DMEMIT("%s %llu %u", dc->dev_read->name,
308 (unsigned long long) dc->start_read,
309 dc->read_delay);
310 if (dc->dev_write)
Milan Broz79662d12007-10-19 22:38:42 +0100311 DMEMIT(" %s %llu %u", dc->dev_write->name,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700312 (unsigned long long) dc->start_write,
313 dc->write_delay);
314 break;
315 }
316
317 return 0;
318}
319
320static struct target_type delay_target = {
321 .name = "delay",
322 .version = {1, 0, 2},
323 .module = THIS_MODULE,
324 .ctr = delay_ctr,
325 .dtr = delay_dtr,
326 .map = delay_map,
327 .presuspend = delay_presuspend,
328 .resume = delay_resume,
329 .status = delay_status,
330};
331
332static int __init dm_delay_init(void)
333{
334 int r = -ENOMEM;
335
336 kdelayd_wq = create_workqueue("kdelayd");
337 if (!kdelayd_wq) {
338 DMERR("Couldn't start kdelayd");
339 goto bad_queue;
340 }
341
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100342 delayed_cache = KMEM_CACHE(dm_delay_info, 0);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700343 if (!delayed_cache) {
344 DMERR("Couldn't create delayed bio cache.");
345 goto bad_memcache;
346 }
347
348 r = dm_register_target(&delay_target);
349 if (r < 0) {
350 DMERR("register failed %d", r);
351 goto bad_register;
352 }
353
354 return 0;
355
356bad_register:
357 kmem_cache_destroy(delayed_cache);
358bad_memcache:
359 destroy_workqueue(kdelayd_wq);
360bad_queue:
361 return r;
362}
363
364static void __exit dm_delay_exit(void)
365{
366 int r = dm_unregister_target(&delay_target);
367
368 if (r < 0)
369 DMERR("unregister failed %d", r);
370
371 kmem_cache_destroy(delayed_cache);
372 destroy_workqueue(kdelayd_wq);
373}
374
375/* Module hooks */
376module_init(dm_delay_init);
377module_exit(dm_delay_exit);
378
379MODULE_DESCRIPTION(DM_NAME " delay target");
380MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
381MODULE_LICENSE("GPL");