1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2007 Red Hat GmbH
5 * A target that delays reads and/or writes and can send
6 * them to different devices.
8 * This file is released under the GPL.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/blkdev.h>
14 #include <linux/bio.h>
15 #include <linux/slab.h>
17 #include <linux/device-mapper.h>
19 #define DM_MSG_PREFIX "delay"
29 struct timer_list delay_timer;
30 struct mutex timer_lock;
31 struct workqueue_struct *kdelayd_wq;
32 struct work_struct flush_expired_bios;
33 struct list_head delayed_bios;
36 struct delay_class read;
37 struct delay_class write;
38 struct delay_class flush;
43 struct dm_delay_info {
44 struct delay_c *context;
45 struct delay_class *class;
46 struct list_head list;
47 unsigned long expires;
50 static DEFINE_MUTEX(delayed_bios_lock);
52 static void handle_delayed_timer(struct timer_list *t)
54 struct delay_c *dc = from_timer(dc, t, delay_timer);
56 queue_work(dc->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)
76 dm_submit_bio_remap(bio, NULL);
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 unsigned long start_timer = 0;
86 struct 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 struct bio *bio = dm_bio_from_per_bio_data(delayed,
92 sizeof(struct dm_delay_info));
93 list_del(&delayed->list);
94 bio_list_add(&flush_bios, bio);
95 delayed->class->ops--;
101 next_expires = delayed->expires;
103 next_expires = min(next_expires, delayed->expires);
105 mutex_unlock(&delayed_bios_lock);
108 queue_timeout(dc, next_expires);
110 return bio_list_get(&flush_bios);
113 static void flush_expired_bios(struct work_struct *work)
117 dc = container_of(work, struct delay_c, flush_expired_bios);
118 flush_bios(flush_delayed_bios(dc, 0));
121 static void delay_dtr(struct dm_target *ti)
123 struct delay_c *dc = ti->private;
126 destroy_workqueue(dc->kdelayd_wq);
129 dm_put_device(ti, dc->read.dev);
131 dm_put_device(ti, dc->write.dev);
133 dm_put_device(ti, dc->flush.dev);
135 mutex_destroy(&dc->timer_lock);
140 static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
143 unsigned long long tmpll;
146 if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
147 ti->error = "Invalid device sector";
152 if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
153 ti->error = "Invalid delay";
157 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
159 ti->error = "Device lookup failed";
167 * Mapping parameters:
168 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
170 * With separate write parameters, the first set is only used for reads.
171 * Offsets are specified in sectors.
172 * Delays are specified in milliseconds.
174 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
179 if (argc != 3 && argc != 6 && argc != 9) {
180 ti->error = "Requires exactly 3, 6 or 9 arguments";
184 dc = kzalloc(sizeof(*dc), GFP_KERNEL);
186 ti->error = "Cannot allocate context";
191 timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
192 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
193 INIT_LIST_HEAD(&dc->delayed_bios);
194 mutex_init(&dc->timer_lock);
195 atomic_set(&dc->may_delay, 1);
198 ret = delay_class_ctr(ti, &dc->read, argv);
203 ret = delay_class_ctr(ti, &dc->write, argv);
206 ret = delay_class_ctr(ti, &dc->flush, argv);
212 ret = delay_class_ctr(ti, &dc->write, argv + 3);
216 ret = delay_class_ctr(ti, &dc->flush, argv + 3);
222 ret = delay_class_ctr(ti, &dc->flush, argv + 6);
227 dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
228 if (!dc->kdelayd_wq) {
230 DMERR("Couldn't start kdelayd");
234 ti->num_flush_bios = 1;
235 ti->num_discard_bios = 1;
236 ti->accounts_remapped_io = true;
237 ti->per_io_data_size = sizeof(struct dm_delay_info);
245 static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
247 struct dm_delay_info *delayed;
248 unsigned long expires = 0;
250 if (!c->delay || !atomic_read(&dc->may_delay))
251 return DM_MAPIO_REMAPPED;
253 delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
255 delayed->context = dc;
256 delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
258 mutex_lock(&delayed_bios_lock);
260 list_add_tail(&delayed->list, &dc->delayed_bios);
261 mutex_unlock(&delayed_bios_lock);
263 queue_timeout(dc, expires);
265 return DM_MAPIO_SUBMITTED;
268 static void delay_presuspend(struct dm_target *ti)
270 struct delay_c *dc = ti->private;
272 atomic_set(&dc->may_delay, 0);
273 del_timer_sync(&dc->delay_timer);
274 flush_bios(flush_delayed_bios(dc, 1));
277 static void delay_resume(struct dm_target *ti)
279 struct delay_c *dc = ti->private;
281 atomic_set(&dc->may_delay, 1);
284 static int delay_map(struct dm_target *ti, struct bio *bio)
286 struct delay_c *dc = ti->private;
287 struct delay_class *c;
288 struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
290 if (bio_data_dir(bio) == WRITE) {
291 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
299 bio_set_dev(bio, c->dev->bdev);
300 bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
302 return delay_bio(dc, c, bio);
305 #define DMEMIT_DELAY_CLASS(c) \
306 DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
308 static void delay_status(struct dm_target *ti, status_type_t type,
309 unsigned int status_flags, char *result, unsigned int maxlen)
311 struct delay_c *dc = ti->private;
315 case STATUSTYPE_INFO:
316 DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
319 case STATUSTYPE_TABLE:
320 DMEMIT_DELAY_CLASS(&dc->read);
323 DMEMIT_DELAY_CLASS(&dc->write);
327 DMEMIT_DELAY_CLASS(&dc->flush);
337 static int delay_iterate_devices(struct dm_target *ti,
338 iterate_devices_callout_fn fn, void *data)
340 struct delay_c *dc = ti->private;
343 ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
346 ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
349 ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
357 static struct target_type delay_target = {
359 .version = {1, 3, 0},
360 .features = DM_TARGET_PASSES_INTEGRITY,
361 .module = THIS_MODULE,
365 .presuspend = delay_presuspend,
366 .resume = delay_resume,
367 .status = delay_status,
368 .iterate_devices = delay_iterate_devices,
372 MODULE_DESCRIPTION(DM_NAME " delay target");
373 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
374 MODULE_LICENSE("GPL");