xref: /linux/drivers/md/dm-delay.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
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 <linux/device-mapper.h>
17 
18 #define DM_MSG_PREFIX "delay"
19 
20 struct delay_c {
21 	struct timer_list delay_timer;
22 	struct mutex timer_lock;
23 	struct workqueue_struct *kdelayd_wq;
24 	struct work_struct flush_expired_bios;
25 	struct list_head delayed_bios;
26 	atomic_t may_delay;
27 
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 
39 struct dm_delay_info {
40 	struct delay_c *context;
41 	struct list_head list;
42 	unsigned long expires;
43 };
44 
45 static DEFINE_MUTEX(delayed_bios_lock);
46 
47 static void handle_delayed_timer(unsigned long data)
48 {
49 	struct delay_c *dc = (struct delay_c *)data;
50 
51 	queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
52 }
53 
54 static void queue_timeout(struct delay_c *dc, unsigned long expires)
55 {
56 	mutex_lock(&dc->timer_lock);
57 
58 	if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
59 		mod_timer(&dc->delay_timer, expires);
60 
61 	mutex_unlock(&dc->timer_lock);
62 }
63 
64 static 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 
76 static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
77 {
78 	struct dm_delay_info *delayed, *next;
79 	unsigned long next_expires = 0;
80 	int start_timer = 0;
81 	struct bio_list flush_bios = { };
82 
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)) {
86 			struct bio *bio = dm_bio_from_per_bio_data(delayed,
87 						sizeof(struct dm_delay_info));
88 			list_del(&delayed->list);
89 			bio_list_add(&flush_bios, bio);
90 			if ((bio_data_dir(bio) == WRITE))
91 				delayed->context->writes--;
92 			else
93 				delayed->context->reads--;
94 			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 
112 static 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  */
127 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
128 {
129 	struct delay_c *dc;
130 	unsigned long long tmpll;
131 	char dummy;
132 	int ret;
133 
134 	if (argc != 3 && argc != 6) {
135 		ti->error = "requires exactly 3 or 6 arguments";
136 		return -EINVAL;
137 	}
138 
139 	dc = kmalloc(sizeof(*dc), GFP_KERNEL);
140 	if (!dc) {
141 		ti->error = "Cannot allocate context";
142 		return -ENOMEM;
143 	}
144 
145 	dc->reads = dc->writes = 0;
146 
147 	ret = -EINVAL;
148 	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
149 		ti->error = "Invalid device sector";
150 		goto bad;
151 	}
152 	dc->start_read = tmpll;
153 
154 	if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) {
155 		ti->error = "Invalid delay";
156 		goto bad;
157 	}
158 
159 	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
160 			    &dc->dev_read);
161 	if (ret) {
162 		ti->error = "Device lookup failed";
163 		goto bad;
164 	}
165 
166 	ret = -EINVAL;
167 	dc->dev_write = NULL;
168 	if (argc == 3)
169 		goto out;
170 
171 	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
172 		ti->error = "Invalid write device sector";
173 		goto bad_dev_read;
174 	}
175 	dc->start_write = tmpll;
176 
177 	if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) {
178 		ti->error = "Invalid write delay";
179 		goto bad_dev_read;
180 	}
181 
182 	ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
183 			    &dc->dev_write);
184 	if (ret) {
185 		ti->error = "Write device lookup failed";
186 		goto bad_dev_read;
187 	}
188 
189 out:
190 	ret = -EINVAL;
191 	dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
192 	if (!dc->kdelayd_wq) {
193 		DMERR("Couldn't start kdelayd");
194 		goto bad_queue;
195 	}
196 
197 	setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
198 
199 	INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
200 	INIT_LIST_HEAD(&dc->delayed_bios);
201 	mutex_init(&dc->timer_lock);
202 	atomic_set(&dc->may_delay, 1);
203 
204 	ti->num_flush_bios = 1;
205 	ti->num_discard_bios = 1;
206 	ti->per_bio_data_size = sizeof(struct dm_delay_info);
207 	ti->private = dc;
208 	return 0;
209 
210 bad_queue:
211 	if (dc->dev_write)
212 		dm_put_device(ti, dc->dev_write);
213 bad_dev_read:
214 	dm_put_device(ti, dc->dev_read);
215 bad:
216 	kfree(dc);
217 	return ret;
218 }
219 
220 static void delay_dtr(struct dm_target *ti)
221 {
222 	struct delay_c *dc = ti->private;
223 
224 	destroy_workqueue(dc->kdelayd_wq);
225 
226 	dm_put_device(ti, dc->dev_read);
227 
228 	if (dc->dev_write)
229 		dm_put_device(ti, dc->dev_write);
230 
231 	kfree(dc);
232 }
233 
234 static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
235 {
236 	struct dm_delay_info *delayed;
237 	unsigned long expires = 0;
238 
239 	if (!delay || !atomic_read(&dc->may_delay))
240 		return 1;
241 
242 	delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
243 
244 	delayed->context = dc;
245 	delayed->expires = expires = jiffies + msecs_to_jiffies(delay);
246 
247 	mutex_lock(&delayed_bios_lock);
248 
249 	if (bio_data_dir(bio) == WRITE)
250 		dc->writes++;
251 	else
252 		dc->reads++;
253 
254 	list_add_tail(&delayed->list, &dc->delayed_bios);
255 
256 	mutex_unlock(&delayed_bios_lock);
257 
258 	queue_timeout(dc, expires);
259 
260 	return 0;
261 }
262 
263 static void delay_presuspend(struct dm_target *ti)
264 {
265 	struct delay_c *dc = ti->private;
266 
267 	atomic_set(&dc->may_delay, 0);
268 	del_timer_sync(&dc->delay_timer);
269 	flush_bios(flush_delayed_bios(dc, 1));
270 }
271 
272 static void delay_resume(struct dm_target *ti)
273 {
274 	struct delay_c *dc = ti->private;
275 
276 	atomic_set(&dc->may_delay, 1);
277 }
278 
279 static int delay_map(struct dm_target *ti, struct bio *bio)
280 {
281 	struct delay_c *dc = ti->private;
282 
283 	if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
284 		bio->bi_bdev = dc->dev_write->bdev;
285 		if (bio_sectors(bio))
286 			bio->bi_iter.bi_sector = dc->start_write +
287 				dm_target_offset(ti, bio->bi_iter.bi_sector);
288 
289 		return delay_bio(dc, dc->write_delay, bio);
290 	}
291 
292 	bio->bi_bdev = dc->dev_read->bdev;
293 	bio->bi_iter.bi_sector = dc->start_read +
294 		dm_target_offset(ti, bio->bi_iter.bi_sector);
295 
296 	return delay_bio(dc, dc->read_delay, bio);
297 }
298 
299 static void delay_status(struct dm_target *ti, status_type_t type,
300 			 unsigned status_flags, char *result, unsigned maxlen)
301 {
302 	struct delay_c *dc = ti->private;
303 	int sz = 0;
304 
305 	switch (type) {
306 	case STATUSTYPE_INFO:
307 		DMEMIT("%u %u", dc->reads, dc->writes);
308 		break;
309 
310 	case STATUSTYPE_TABLE:
311 		DMEMIT("%s %llu %u", dc->dev_read->name,
312 		       (unsigned long long) dc->start_read,
313 		       dc->read_delay);
314 		if (dc->dev_write)
315 			DMEMIT(" %s %llu %u", dc->dev_write->name,
316 			       (unsigned long long) dc->start_write,
317 			       dc->write_delay);
318 		break;
319 	}
320 }
321 
322 static int delay_iterate_devices(struct dm_target *ti,
323 				 iterate_devices_callout_fn fn, void *data)
324 {
325 	struct delay_c *dc = ti->private;
326 	int ret = 0;
327 
328 	ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data);
329 	if (ret)
330 		goto out;
331 
332 	if (dc->dev_write)
333 		ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data);
334 
335 out:
336 	return ret;
337 }
338 
339 static struct target_type delay_target = {
340 	.name	     = "delay",
341 	.version     = {1, 2, 1},
342 	.module      = THIS_MODULE,
343 	.ctr	     = delay_ctr,
344 	.dtr	     = delay_dtr,
345 	.map	     = delay_map,
346 	.presuspend  = delay_presuspend,
347 	.resume	     = delay_resume,
348 	.status	     = delay_status,
349 	.iterate_devices = delay_iterate_devices,
350 };
351 
352 static int __init dm_delay_init(void)
353 {
354 	int r;
355 
356 	r = dm_register_target(&delay_target);
357 	if (r < 0) {
358 		DMERR("register failed %d", r);
359 		goto bad_register;
360 	}
361 
362 	return 0;
363 
364 bad_register:
365 	return r;
366 }
367 
368 static void __exit dm_delay_exit(void)
369 {
370 	dm_unregister_target(&delay_target);
371 }
372 
373 /* Module hooks */
374 module_init(dm_delay_init);
375 module_exit(dm_delay_exit);
376 
377 MODULE_DESCRIPTION(DM_NAME " delay target");
378 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
379 MODULE_LICENSE("GPL");
380