blob: 1783d80c9cad029b61c261c41ed5c5dbdc8c0624 [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
Kees Cook8376d3c2017-10-16 17:01:48 -070047static void handle_delayed_timer(struct timer_list *t)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070048{
Kees Cook8376d3c2017-10-16 17:01:48 -070049 struct delay_c *dc = from_timer(dc, t, delay_timer);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070050
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.
Tomohiro Kusumif49e8692015-10-28 04:38:56 +0900125 * Offsets are specified in sectors.
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700126 * Delays are specified in milliseconds.
127 */
128static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
129{
130 struct delay_c *dc;
131 unsigned long long tmpll;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100132 char dummy;
Vivek Goyale80d1c82015-07-31 09:20:36 -0400133 int ret;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700134
135 if (argc != 3 && argc != 6) {
Tomohiro Kusumie213f332015-10-28 04:38:57 +0900136 ti->error = "Requires exactly 3 or 6 arguments";
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700137 return -EINVAL;
138 }
139
140 dc = kmalloc(sizeof(*dc), GFP_KERNEL);
141 if (!dc) {
142 ti->error = "Cannot allocate context";
143 return -ENOMEM;
144 }
145
146 dc->reads = dc->writes = 0;
147
Vivek Goyale80d1c82015-07-31 09:20:36 -0400148 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100149 if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700150 ti->error = "Invalid device sector";
151 goto bad;
152 }
153 dc->start_read = tmpll;
154
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100155 if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700156 ti->error = "Invalid delay";
157 goto bad;
158 }
159
Vivek Goyale80d1c82015-07-31 09:20:36 -0400160 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
161 &dc->dev_read);
162 if (ret) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700163 ti->error = "Device lookup failed";
164 goto bad;
165 }
166
Vivek Goyale80d1c82015-07-31 09:20:36 -0400167 ret = -EINVAL;
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100168 dc->dev_write = NULL;
169 if (argc == 3)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700170 goto out;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700171
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100172 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700173 ti->error = "Invalid write device sector";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100174 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700175 }
176 dc->start_write = tmpll;
177
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100178 if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700179 ti->error = "Invalid write delay";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100180 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700181 }
182
Vivek Goyale80d1c82015-07-31 09:20:36 -0400183 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
184 &dc->dev_write);
185 if (ret) {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700186 ti->error = "Write device lookup failed";
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100187 goto bad_dev_read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700188 }
189
190out:
Vivek Goyale80d1c82015-07-31 09:20:36 -0400191 ret = -EINVAL;
Mikulas Patocka718822c2013-11-15 16:12:20 -0500192 dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
193 if (!dc->kdelayd_wq) {
194 DMERR("Couldn't start kdelayd");
195 goto bad_queue;
196 }
197
Kees Cook8376d3c2017-10-16 17:01:48 -0700198 timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700199
200 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
201 INIT_LIST_HEAD(&dc->delayed_bios);
Alasdair G Kergonac818642007-07-12 17:26:47 +0100202 mutex_init(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700203 atomic_set(&dc->may_delay, 1);
204
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000205 ti->num_flush_bios = 1;
206 ti->num_discard_bios = 1;
Mike Snitzer30187e12016-01-31 13:28:26 -0500207 ti->per_io_data_size = sizeof(struct dm_delay_info);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700208 ti->private = dc;
209 return 0;
210
Mikulas Patocka718822c2013-11-15 16:12:20 -0500211bad_queue:
Dmitry Monakhov2e64a0f2007-10-19 22:38:41 +0100212 if (dc->dev_write)
213 dm_put_device(ti, dc->dev_write);
214bad_dev_read:
215 dm_put_device(ti, dc->dev_read);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700216bad:
217 kfree(dc);
Vivek Goyale80d1c82015-07-31 09:20:36 -0400218 return ret;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700219}
220
221static void delay_dtr(struct dm_target *ti)
222{
223 struct delay_c *dc = ti->private;
224
Mikulas Patocka718822c2013-11-15 16:12:20 -0500225 destroy_workqueue(dc->kdelayd_wq);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700226
227 dm_put_device(ti, dc->dev_read);
228
229 if (dc->dev_write)
230 dm_put_device(ti, dc->dev_write);
231
Mike Snitzerd5ffebd2018-01-05 21:17:20 -0500232 mutex_destroy(&dc->timer_lock);
233
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700234 kfree(dc);
235}
236
237static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
238{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100239 struct dm_delay_info *delayed;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700240 unsigned long expires = 0;
241
242 if (!delay || !atomic_read(&dc->may_delay))
Tomohiro Kusumi340c9ec2015-10-28 04:38:55 +0900243 return DM_MAPIO_REMAPPED;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700244
Mikulas Patocka42065462013-11-15 16:12:51 -0500245 delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700246
247 delayed->context = dc;
Nicholas Mc Guireaca607b2015-03-17 07:47:58 -0400248 delayed->expires = expires = jiffies + msecs_to_jiffies(delay);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700249
250 mutex_lock(&delayed_bios_lock);
251
252 if (bio_data_dir(bio) == WRITE)
253 dc->writes++;
254 else
255 dc->reads++;
256
257 list_add_tail(&delayed->list, &dc->delayed_bios);
258
259 mutex_unlock(&delayed_bios_lock);
260
261 queue_timeout(dc, expires);
262
Tomohiro Kusumi340c9ec2015-10-28 04:38:55 +0900263 return DM_MAPIO_SUBMITTED;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700264}
265
266static void delay_presuspend(struct dm_target *ti)
267{
268 struct delay_c *dc = ti->private;
269
270 atomic_set(&dc->may_delay, 0);
271 del_timer_sync(&dc->delay_timer);
272 flush_bios(flush_delayed_bios(dc, 1));
273}
274
275static void delay_resume(struct dm_target *ti)
276{
277 struct delay_c *dc = ti->private;
278
279 atomic_set(&dc->may_delay, 1);
280}
281
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000282static int delay_map(struct dm_target *ti, struct bio *bio)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700283{
284 struct delay_c *dc = ti->private;
285
286 if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200287 bio_set_dev(bio, dc->dev_write->bdev);
Mikulas Patockac9272592009-06-22 10:12:23 +0100288 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700289 bio->bi_iter.bi_sector = dc->start_write +
290 dm_target_offset(ti, bio->bi_iter.bi_sector);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700291
292 return delay_bio(dc, dc->write_delay, bio);
293 }
294
Christoph Hellwig74d46992017-08-23 19:10:32 +0200295 bio_set_dev(bio, dc->dev_read->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700296 bio->bi_iter.bi_sector = dc->start_read +
297 dm_target_offset(ti, bio->bi_iter.bi_sector);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700298
299 return delay_bio(dc, dc->read_delay, bio);
300}
301
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000302static void delay_status(struct dm_target *ti, status_type_t type,
303 unsigned status_flags, char *result, unsigned maxlen)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700304{
305 struct delay_c *dc = ti->private;
306 int sz = 0;
307
308 switch (type) {
309 case STATUSTYPE_INFO:
310 DMEMIT("%u %u", dc->reads, dc->writes);
311 break;
312
313 case STATUSTYPE_TABLE:
314 DMEMIT("%s %llu %u", dc->dev_read->name,
315 (unsigned long long) dc->start_read,
316 dc->read_delay);
317 if (dc->dev_write)
Milan Broz79662d12007-10-19 22:38:42 +0100318 DMEMIT(" %s %llu %u", dc->dev_write->name,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700319 (unsigned long long) dc->start_write,
320 dc->write_delay);
321 break;
322 }
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700323}
324
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100325static int delay_iterate_devices(struct dm_target *ti,
326 iterate_devices_callout_fn fn, void *data)
327{
328 struct delay_c *dc = ti->private;
329 int ret = 0;
330
Mike Snitzer5dea2712009-07-23 20:30:42 +0100331 ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100332 if (ret)
333 goto out;
334
335 if (dc->dev_write)
Mike Snitzer5dea2712009-07-23 20:30:42 +0100336 ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100337
338out:
339 return ret;
340}
341
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700342static struct target_type delay_target = {
343 .name = "delay",
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000344 .version = {1, 2, 1},
Mikulas Patockae2460f22017-04-18 16:51:48 -0400345 .features = DM_TARGET_PASSES_INTEGRITY,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700346 .module = THIS_MODULE,
347 .ctr = delay_ctr,
348 .dtr = delay_dtr,
349 .map = delay_map,
350 .presuspend = delay_presuspend,
351 .resume = delay_resume,
352 .status = delay_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100353 .iterate_devices = delay_iterate_devices,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700354};
355
356static int __init dm_delay_init(void)
357{
Mikulas Patocka42065462013-11-15 16:12:51 -0500358 int r;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700359
360 r = dm_register_target(&delay_target);
361 if (r < 0) {
362 DMERR("register failed %d", r);
363 goto bad_register;
364 }
365
366 return 0;
367
368bad_register:
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700369 return r;
370}
371
372static void __exit dm_delay_exit(void)
373{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000374 dm_unregister_target(&delay_target);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700375}
376
377/* Module hooks */
378module_init(dm_delay_init);
379module_exit(dm_delay_exit);
380
381MODULE_DESCRIPTION(DM_NAME " delay target");
382MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
383MODULE_LICENSE("GPL");