blob: 57b6a1901c917127eccb8247cd1015a9b588159e [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
Mikulas Patocka586e80e2008-10-21 17:44:59 +010016#include <linux/device-mapper.h>
17
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070018#define DM_MSG_PREFIX "delay"
19
20struct delay_c {
21 struct timer_list delay_timer;
Alasdair G Kergonac818642007-07-12 17:26:47 +010022 struct mutex timer_lock;
Mikulas Patocka718822c2013-11-15 16:12:20 -050023 struct workqueue_struct *kdelayd_wq;
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;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070027
28 struct dm_dev *dev_read;
29 sector_t start_read;
30 unsigned read_delay;
31 unsigned reads;
32
33 struct dm_dev *dev_write;
34 sector_t start_write;
35 unsigned write_delay;
36 unsigned writes;
37};
38
Alasdair G Kergon028867a2007-07-12 17:26:32 +010039struct dm_delay_info {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070040 struct delay_c *context;
41 struct list_head list;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070042 unsigned long expires;
43};
44
45static DEFINE_MUTEX(delayed_bios_lock);
46
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070047static void handle_delayed_timer(unsigned long data)
48{
49 struct delay_c *dc = (struct delay_c *)data;
50
Mikulas Patocka718822c2013-11-15 16:12:20 -050051 queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070052}
53
54static void queue_timeout(struct delay_c *dc, unsigned long expires)
55{
Alasdair G Kergonac818642007-07-12 17:26:47 +010056 mutex_lock(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070057
58 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
59 mod_timer(&dc->delay_timer, expires);
60
Alasdair G Kergonac818642007-07-12 17:26:47 +010061 mutex_unlock(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070062}
63
64static void flush_bios(struct bio *bio)
65{
66 struct bio *n;
67
68 while (bio) {
69 n = bio->bi_next;
70 bio->bi_next = NULL;
71 generic_make_request(bio);
72 bio = n;
73 }
74}
75
76static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
77{
Alasdair G Kergon028867a2007-07-12 17:26:32 +010078 struct dm_delay_info *delayed, *next;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070079 unsigned long next_expires = 0;
80 int start_timer = 0;
Alasdair G Kergon051814c2007-10-19 22:38:55 +010081 struct bio_list flush_bios = { };
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070082
83 mutex_lock(&delayed_bios_lock);
84 list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
85 if (flush_all || time_after_eq(jiffies, delayed->expires)) {
Mikulas Patocka42065462013-11-15 16:12:51 -050086 struct bio *bio = dm_bio_from_per_bio_data(delayed,
87 sizeof(struct dm_delay_info));
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070088 list_del(&delayed->list);
Mikulas Patocka42065462013-11-15 16:12:51 -050089 bio_list_add(&flush_bios, bio);
90 if ((bio_data_dir(bio) == WRITE))
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070091 delayed->context->writes--;
92 else
93 delayed->context->reads--;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070094 continue;
95 }
96
97 if (!start_timer) {
98 start_timer = 1;
99 next_expires = delayed->expires;
100 } else
101 next_expires = min(next_expires, delayed->expires);
102 }
103
104 mutex_unlock(&delayed_bios_lock);
105
106 if (start_timer)
107 queue_timeout(dc, next_expires);
108
109 return bio_list_get(&flush_bios);
110}
111
112static void flush_expired_bios(struct work_struct *work)
113{
114 struct delay_c *dc;
115
116 dc = container_of(work, struct delay_c, flush_expired_bios);
117 flush_bios(flush_delayed_bios(dc, 0));
118}
119
120/*
121 * Mapping parameters:
122 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
123 *
124 * With separate write parameters, the first set is only used for reads.
125 * Delays are specified in milliseconds.
126 */
127static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
128{
129 struct delay_c *dc;
130 unsigned long long tmpll;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100131 char dummy;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700132
133 if (argc != 3 && argc != 6) {
134 ti->error = "requires exactly 3 or 6 arguments";
135 return -EINVAL;
136 }
137
138 dc = kmalloc(sizeof(*dc), GFP_KERNEL);
139 if (!dc) {
140 ti->error = "Cannot allocate context";
141 return -ENOMEM;
142 }
143
144 dc->reads = dc->writes = 0;
145
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100146 if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700147 ti->error = "Invalid device sector";
148 goto bad;
149 }
150 dc->start_read = tmpll;
151
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100152 if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700153 ti->error = "Invalid delay";
154 goto bad;
155 }
156
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000157 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
158 &dc->dev_read)) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700159 ti->error = "Device lookup failed";
160 goto bad;
161 }
162
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100163 dc->dev_write = NULL;
164 if (argc == 3)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700165 goto out;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700166
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100167 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700168 ti->error = "Invalid write device sector";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100169 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700170 }
171 dc->start_write = tmpll;
172
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100173 if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700174 ti->error = "Invalid write delay";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100175 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700176 }
177
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000178 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
179 &dc->dev_write)) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700180 ti->error = "Write device lookup failed";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100181 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700182 }
183
184out:
Mikulas Patocka718822c2013-11-15 16:12:20 -0500185 dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
186 if (!dc->kdelayd_wq) {
187 DMERR("Couldn't start kdelayd");
188 goto bad_queue;
189 }
190
Alasdair G Kergonac818642007-07-12 17:26:47 +0100191 setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700192
193 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
194 INIT_LIST_HEAD(&dc->delayed_bios);
Alasdair G Kergonac818642007-07-12 17:26:47 +0100195 mutex_init(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700196 atomic_set(&dc->may_delay, 1);
197
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000198 ti->num_flush_bios = 1;
199 ti->num_discard_bios = 1;
Mikulas Patocka42065462013-11-15 16:12:51 -0500200 ti->per_bio_data_size = sizeof(struct dm_delay_info);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700201 ti->private = dc;
202 return 0;
203
Mikulas Patocka718822c2013-11-15 16:12:20 -0500204bad_queue:
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100205 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
Mikulas Patocka718822c2013-11-15 16:12:20 -0500218 destroy_workqueue(dc->kdelayd_wq);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700219
220 dm_put_device(ti, dc->dev_read);
221
222 if (dc->dev_write)
223 dm_put_device(ti, dc->dev_write);
224
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700225 kfree(dc);
226}
227
228static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
229{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100230 struct dm_delay_info *delayed;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700231 unsigned long expires = 0;
232
233 if (!delay || !atomic_read(&dc->may_delay))
234 return 1;
235
Mikulas Patocka42065462013-11-15 16:12:51 -0500236 delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700237
238 delayed->context = dc;
Nicholas Mc Guireaca607b2015-03-17 07:47:58 -0400239 delayed->expires = expires = jiffies + msecs_to_jiffies(delay);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700240
241 mutex_lock(&delayed_bios_lock);
242
243 if (bio_data_dir(bio) == WRITE)
244 dc->writes++;
245 else
246 dc->reads++;
247
248 list_add_tail(&delayed->list, &dc->delayed_bios);
249
250 mutex_unlock(&delayed_bios_lock);
251
252 queue_timeout(dc, expires);
253
254 return 0;
255}
256
257static void delay_presuspend(struct dm_target *ti)
258{
259 struct delay_c *dc = ti->private;
260
261 atomic_set(&dc->may_delay, 0);
262 del_timer_sync(&dc->delay_timer);
263 flush_bios(flush_delayed_bios(dc, 1));
264}
265
266static void delay_resume(struct dm_target *ti)
267{
268 struct delay_c *dc = ti->private;
269
270 atomic_set(&dc->may_delay, 1);
271}
272
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000273static int delay_map(struct dm_target *ti, struct bio *bio)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700274{
275 struct delay_c *dc = ti->private;
276
277 if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
278 bio->bi_bdev = dc->dev_write->bdev;
Mikulas Patockac9272592009-06-22 10:12:23 +0100279 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700280 bio->bi_iter.bi_sector = dc->start_write +
281 dm_target_offset(ti, bio->bi_iter.bi_sector);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700282
283 return delay_bio(dc, dc->write_delay, bio);
284 }
285
286 bio->bi_bdev = dc->dev_read->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700287 bio->bi_iter.bi_sector = dc->start_read +
288 dm_target_offset(ti, bio->bi_iter.bi_sector);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700289
290 return delay_bio(dc, dc->read_delay, bio);
291}
292
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000293static void delay_status(struct dm_target *ti, status_type_t type,
294 unsigned status_flags, char *result, unsigned maxlen)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700295{
296 struct delay_c *dc = ti->private;
297 int sz = 0;
298
299 switch (type) {
300 case STATUSTYPE_INFO:
301 DMEMIT("%u %u", dc->reads, dc->writes);
302 break;
303
304 case STATUSTYPE_TABLE:
305 DMEMIT("%s %llu %u", dc->dev_read->name,
306 (unsigned long long) dc->start_read,
307 dc->read_delay);
308 if (dc->dev_write)
Milan Broz79662d12007-10-19 22:38:42 +0100309 DMEMIT(" %s %llu %u", dc->dev_write->name,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700310 (unsigned long long) dc->start_write,
311 dc->write_delay);
312 break;
313 }
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700314}
315
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100316static int delay_iterate_devices(struct dm_target *ti,
317 iterate_devices_callout_fn fn, void *data)
318{
319 struct delay_c *dc = ti->private;
320 int ret = 0;
321
Mike Snitzer5dea2712009-07-23 20:30:42 +0100322 ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100323 if (ret)
324 goto out;
325
326 if (dc->dev_write)
Mike Snitzer5dea2712009-07-23 20:30:42 +0100327 ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100328
329out:
330 return ret;
331}
332
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700333static struct target_type delay_target = {
334 .name = "delay",
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000335 .version = {1, 2, 1},
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700336 .module = THIS_MODULE,
337 .ctr = delay_ctr,
338 .dtr = delay_dtr,
339 .map = delay_map,
340 .presuspend = delay_presuspend,
341 .resume = delay_resume,
342 .status = delay_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100343 .iterate_devices = delay_iterate_devices,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700344};
345
346static int __init dm_delay_init(void)
347{
Mikulas Patocka42065462013-11-15 16:12:51 -0500348 int r;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700349
350 r = dm_register_target(&delay_target);
351 if (r < 0) {
352 DMERR("register failed %d", r);
353 goto bad_register;
354 }
355
356 return 0;
357
358bad_register:
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700359 return r;
360}
361
362static void __exit dm_delay_exit(void)
363{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000364 dm_unregister_target(&delay_target);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700365}
366
367/* Module hooks */
368module_init(dm_delay_init);
369module_exit(dm_delay_exit);
370
371MODULE_DESCRIPTION(DM_NAME " delay target");
372MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
373MODULE_LICENSE("GPL");