blob: fddffe251bf6bf5c2ded195c31e392bc228568d8 [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
Mikulas Patocka3876ac72018-04-17 00:33:13 +020020struct delay_class {
21 struct dm_dev *dev;
22 sector_t start;
23 unsigned delay;
24 unsigned ops;
25};
26
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070027struct delay_c {
28 struct timer_list delay_timer;
Alasdair G Kergonac818642007-07-12 17:26:47 +010029 struct mutex timer_lock;
Mikulas Patocka718822c2013-11-15 16:12:20 -050030 struct workqueue_struct *kdelayd_wq;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070031 struct work_struct flush_expired_bios;
32 struct list_head delayed_bios;
33 atomic_t may_delay;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070034
Mikulas Patocka3876ac72018-04-17 00:33:13 +020035 struct delay_class read;
36 struct delay_class write;
Mikulas Patockacda6b5a2018-04-17 00:33:14 +020037 struct delay_class flush;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070038
Mikulas Patocka3876ac72018-04-17 00:33:13 +020039 int argc;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070040};
41
Alasdair G Kergon028867a2007-07-12 17:26:32 +010042struct dm_delay_info {
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070043 struct delay_c *context;
Mikulas Patocka3876ac72018-04-17 00:33:13 +020044 struct delay_class *class;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070045 struct list_head list;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070046 unsigned long expires;
47};
48
49static DEFINE_MUTEX(delayed_bios_lock);
50
Kees Cook8376d3c2017-10-16 17:01:48 -070051static void handle_delayed_timer(struct timer_list *t)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070052{
Kees Cook8376d3c2017-10-16 17:01:48 -070053 struct delay_c *dc = from_timer(dc, t, delay_timer);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070054
Mikulas Patocka718822c2013-11-15 16:12:20 -050055 queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070056}
57
58static void queue_timeout(struct delay_c *dc, unsigned long expires)
59{
Alasdair G Kergonac818642007-07-12 17:26:47 +010060 mutex_lock(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070061
62 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
63 mod_timer(&dc->delay_timer, expires);
64
Alasdair G Kergonac818642007-07-12 17:26:47 +010065 mutex_unlock(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070066}
67
68static void flush_bios(struct bio *bio)
69{
70 struct bio *n;
71
72 while (bio) {
73 n = bio->bi_next;
74 bio->bi_next = NULL;
75 generic_make_request(bio);
76 bio = n;
77 }
78}
79
80static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
81{
Alasdair G Kergon028867a2007-07-12 17:26:32 +010082 struct dm_delay_info *delayed, *next;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070083 unsigned long next_expires = 0;
Mikulas Patocka3876ac72018-04-17 00:33:13 +020084 unsigned long start_timer = 0;
Alasdair G Kergon051814c2007-10-19 22:38:55 +010085 struct bio_list flush_bios = { };
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070086
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)) {
Mikulas Patocka42065462013-11-15 16:12:51 -050090 struct bio *bio = dm_bio_from_per_bio_data(delayed,
91 sizeof(struct dm_delay_info));
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070092 list_del(&delayed->list);
Mikulas Patocka42065462013-11-15 16:12:51 -050093 bio_list_add(&flush_bios, bio);
Mikulas Patocka3876ac72018-04-17 00:33:13 +020094 delayed->class->ops--;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -070095 continue;
96 }
97
98 if (!start_timer) {
99 start_timer = 1;
100 next_expires = delayed->expires;
101 } else
102 next_expires = min(next_expires, delayed->expires);
103 }
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700104 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
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200120static void delay_dtr(struct dm_target *ti)
121{
122 struct delay_c *dc = ti->private;
123
124 destroy_workqueue(dc->kdelayd_wq);
125
126 if (dc->read.dev)
127 dm_put_device(ti, dc->read.dev);
128 if (dc->write.dev)
129 dm_put_device(ti, dc->write.dev);
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200130 if (dc->flush.dev)
131 dm_put_device(ti, dc->flush.dev);
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200132
133 mutex_destroy(&dc->timer_lock);
134
135 kfree(dc);
136}
137
138static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
139{
140 int ret;
141 unsigned long long tmpll;
142 char dummy;
143
Milan Broz887b1c92018-11-07 22:24:55 +0100144 if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200145 ti->error = "Invalid device sector";
146 return -EINVAL;
147 }
148 c->start = tmpll;
149
150 if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
151 ti->error = "Invalid delay";
152 return -EINVAL;
153 }
154
155 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
156 if (ret) {
157 ti->error = "Device lookup failed";
158 return ret;
159 }
160
161 return 0;
162}
163
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700164/*
165 * Mapping parameters:
166 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
167 *
168 * With separate write parameters, the first set is only used for reads.
Tomohiro Kusumif49e8692015-10-28 04:38:56 +0900169 * Offsets are specified in sectors.
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700170 * Delays are specified in milliseconds.
171 */
172static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
173{
174 struct delay_c *dc;
Vivek Goyale80d1c82015-07-31 09:20:36 -0400175 int ret;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700176
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200177 if (argc != 3 && argc != 6 && argc != 9) {
178 ti->error = "Requires exactly 3, 6 or 9 arguments";
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700179 return -EINVAL;
180 }
181
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200182 dc = kzalloc(sizeof(*dc), GFP_KERNEL);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700183 if (!dc) {
184 ti->error = "Cannot allocate context";
185 return -ENOMEM;
186 }
187
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200188 ti->private = dc;
Kees Cook8376d3c2017-10-16 17:01:48 -0700189 timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700190 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
191 INIT_LIST_HEAD(&dc->delayed_bios);
Alasdair G Kergonac818642007-07-12 17:26:47 +0100192 mutex_init(&dc->timer_lock);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700193 atomic_set(&dc->may_delay, 1);
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200194 dc->argc = argc;
195
196 ret = delay_class_ctr(ti, &dc->read, argv);
197 if (ret)
198 goto bad;
199
200 if (argc == 3) {
201 ret = delay_class_ctr(ti, &dc->write, argv);
202 if (ret)
203 goto bad;
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200204 ret = delay_class_ctr(ti, &dc->flush, argv);
205 if (ret)
206 goto bad;
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200207 goto out;
208 }
209
210 ret = delay_class_ctr(ti, &dc->write, argv + 3);
211 if (ret)
212 goto bad;
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200213 if (argc == 6) {
214 ret = delay_class_ctr(ti, &dc->flush, argv + 3);
215 if (ret)
216 goto bad;
217 goto out;
218 }
219
220 ret = delay_class_ctr(ti, &dc->flush, argv + 6);
221 if (ret)
222 goto bad;
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200223
224out:
225 dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
226 if (!dc->kdelayd_wq) {
227 ret = -EINVAL;
228 DMERR("Couldn't start kdelayd");
229 goto bad;
230 }
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700231
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000232 ti->num_flush_bios = 1;
233 ti->num_discard_bios = 1;
Mike Snitzer30187e12016-01-31 13:28:26 -0500234 ti->per_io_data_size = sizeof(struct dm_delay_info);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700235 return 0;
236
237bad:
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200238 delay_dtr(ti);
Vivek Goyale80d1c82015-07-31 09:20:36 -0400239 return ret;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700240}
241
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200242static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700243{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100244 struct dm_delay_info *delayed;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700245 unsigned long expires = 0;
246
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200247 if (!c->delay || !atomic_read(&dc->may_delay))
Tomohiro Kusumi340c9ec2015-10-28 04:38:55 +0900248 return DM_MAPIO_REMAPPED;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700249
Mikulas Patocka42065462013-11-15 16:12:51 -0500250 delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700251
252 delayed->context = dc;
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200253 delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700254
255 mutex_lock(&delayed_bios_lock);
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200256 c->ops++;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700257 list_add_tail(&delayed->list, &dc->delayed_bios);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700258 mutex_unlock(&delayed_bios_lock);
259
260 queue_timeout(dc, expires);
261
Tomohiro Kusumi340c9ec2015-10-28 04:38:55 +0900262 return DM_MAPIO_SUBMITTED;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700263}
264
265static void delay_presuspend(struct dm_target *ti)
266{
267 struct delay_c *dc = ti->private;
268
269 atomic_set(&dc->may_delay, 0);
270 del_timer_sync(&dc->delay_timer);
271 flush_bios(flush_delayed_bios(dc, 1));
272}
273
274static void delay_resume(struct dm_target *ti)
275{
276 struct delay_c *dc = ti->private;
277
278 atomic_set(&dc->may_delay, 1);
279}
280
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000281static int delay_map(struct dm_target *ti, struct bio *bio)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700282{
283 struct delay_c *dc = ti->private;
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200284 struct delay_class *c;
285 struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700286
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200287 if (bio_data_dir(bio) == WRITE) {
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200288 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
289 c = &dc->flush;
290 else
291 c = &dc->write;
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200292 } else {
293 c = &dc->read;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700294 }
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200295 delayed->class = c;
296 bio_set_dev(bio, c->dev->bdev);
297 if (bio_sectors(bio))
298 bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700299
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200300 return delay_bio(dc, c, bio);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700301}
302
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200303#define DMEMIT_DELAY_CLASS(c) \
304 DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
305
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000306static void delay_status(struct dm_target *ti, status_type_t type,
307 unsigned status_flags, char *result, unsigned maxlen)
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700308{
309 struct delay_c *dc = ti->private;
310 int sz = 0;
311
312 switch (type) {
313 case STATUSTYPE_INFO:
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200314 DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700315 break;
316
317 case STATUSTYPE_TABLE:
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200318 DMEMIT_DELAY_CLASS(&dc->read);
319 if (dc->argc >= 6) {
320 DMEMIT(" ");
321 DMEMIT_DELAY_CLASS(&dc->write);
322 }
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200323 if (dc->argc >= 9) {
324 DMEMIT(" ");
325 DMEMIT_DELAY_CLASS(&dc->flush);
326 }
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700327 break;
328 }
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700329}
330
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100331static int delay_iterate_devices(struct dm_target *ti,
332 iterate_devices_callout_fn fn, void *data)
333{
334 struct delay_c *dc = ti->private;
335 int ret = 0;
336
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200337 ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100338 if (ret)
339 goto out;
Mikulas Patocka3876ac72018-04-17 00:33:13 +0200340 ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
341 if (ret)
342 goto out;
Mikulas Patockacda6b5a2018-04-17 00:33:14 +0200343 ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
344 if (ret)
345 goto out;
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100346
347out:
348 return ret;
349}
350
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700351static struct target_type delay_target = {
352 .name = "delay",
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000353 .version = {1, 2, 1},
Mikulas Patockae2460f22017-04-18 16:51:48 -0400354 .features = DM_TARGET_PASSES_INTEGRITY,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700355 .module = THIS_MODULE,
356 .ctr = delay_ctr,
357 .dtr = delay_dtr,
358 .map = delay_map,
359 .presuspend = delay_presuspend,
360 .resume = delay_resume,
361 .status = delay_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100362 .iterate_devices = delay_iterate_devices,
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700363};
364
365static int __init dm_delay_init(void)
366{
Mikulas Patocka42065462013-11-15 16:12:51 -0500367 int r;
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700368
369 r = dm_register_target(&delay_target);
370 if (r < 0) {
371 DMERR("register failed %d", r);
372 goto bad_register;
373 }
374
375 return 0;
376
377bad_register:
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700378 return r;
379}
380
381static void __exit dm_delay_exit(void)
382{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000383 dm_unregister_target(&delay_target);
Heinz Mauelshagen26b9f222007-05-09 02:33:06 -0700384}
385
386/* Module hooks */
387module_init(dm_delay_init);
388module_exit(dm_delay_exit);
389
390MODULE_DESCRIPTION(DM_NAME " delay target");
391MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
392MODULE_LICENSE("GPL");