2 * Copyright (C) 2005-2007 Red Hat GmbH
4 * A target that delays reads and/or writes and can send
5 * them to different devices.
7 * This file is released under the GPL.
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>
16 #include <linux/device-mapper.h>
18 #define DM_MSG_PREFIX "delay"
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
;
28 struct dm_dev
*dev_read
;
33 struct dm_dev
*dev_write
;
39 struct dm_delay_info
{
40 struct delay_c
*context
;
41 struct list_head list
;
42 unsigned long expires
;
45 static DEFINE_MUTEX(delayed_bios_lock
);
47 static void handle_delayed_timer(unsigned long data
)
49 struct delay_c
*dc
= (struct delay_c
*)data
;
51 queue_work(dc
->kdelayd_wq
, &dc
->flush_expired_bios
);
54 static void queue_timeout(struct delay_c
*dc
, unsigned long expires
)
56 mutex_lock(&dc
->timer_lock
);
58 if (!timer_pending(&dc
->delay_timer
) || expires
< dc
->delay_timer
.expires
)
59 mod_timer(&dc
->delay_timer
, expires
);
61 mutex_unlock(&dc
->timer_lock
);
64 static void flush_bios(struct bio
*bio
)
71 generic_make_request(bio
);
76 static struct bio
*flush_delayed_bios(struct delay_c
*dc
, int flush_all
)
78 struct dm_delay_info
*delayed
, *next
;
79 unsigned long next_expires
= 0;
81 struct bio_list flush_bios
= { };
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
--;
93 delayed
->context
->reads
--;
99 next_expires
= delayed
->expires
;
101 next_expires
= min(next_expires
, delayed
->expires
);
104 mutex_unlock(&delayed_bios_lock
);
107 queue_timeout(dc
, next_expires
);
109 return bio_list_get(&flush_bios
);
112 static void flush_expired_bios(struct work_struct
*work
)
116 dc
= container_of(work
, struct delay_c
, flush_expired_bios
);
117 flush_bios(flush_delayed_bios(dc
, 0));
121 * Mapping parameters:
122 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
124 * With separate write parameters, the first set is only used for reads.
125 * Delays are specified in milliseconds.
127 static int delay_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
130 unsigned long long tmpll
;
133 if (argc
!= 3 && argc
!= 6) {
134 ti
->error
= "requires exactly 3 or 6 arguments";
138 dc
= kmalloc(sizeof(*dc
), GFP_KERNEL
);
140 ti
->error
= "Cannot allocate context";
144 dc
->reads
= dc
->writes
= 0;
146 if (sscanf(argv
[1], "%llu%c", &tmpll
, &dummy
) != 1) {
147 ti
->error
= "Invalid device sector";
150 dc
->start_read
= tmpll
;
152 if (sscanf(argv
[2], "%u%c", &dc
->read_delay
, &dummy
) != 1) {
153 ti
->error
= "Invalid delay";
157 if (dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
),
159 ti
->error
= "Device lookup failed";
163 dc
->dev_write
= NULL
;
167 if (sscanf(argv
[4], "%llu%c", &tmpll
, &dummy
) != 1) {
168 ti
->error
= "Invalid write device sector";
171 dc
->start_write
= tmpll
;
173 if (sscanf(argv
[5], "%u%c", &dc
->write_delay
, &dummy
) != 1) {
174 ti
->error
= "Invalid write delay";
178 if (dm_get_device(ti
, argv
[3], dm_table_get_mode(ti
->table
),
180 ti
->error
= "Write device lookup failed";
185 dc
->kdelayd_wq
= alloc_workqueue("kdelayd", WQ_MEM_RECLAIM
, 0);
186 if (!dc
->kdelayd_wq
) {
187 DMERR("Couldn't start kdelayd");
191 setup_timer(&dc
->delay_timer
, handle_delayed_timer
, (unsigned long)dc
);
193 INIT_WORK(&dc
->flush_expired_bios
, flush_expired_bios
);
194 INIT_LIST_HEAD(&dc
->delayed_bios
);
195 mutex_init(&dc
->timer_lock
);
196 atomic_set(&dc
->may_delay
, 1);
198 ti
->num_flush_bios
= 1;
199 ti
->num_discard_bios
= 1;
200 ti
->per_bio_data_size
= sizeof(struct dm_delay_info
);
206 dm_put_device(ti
, dc
->dev_write
);
208 dm_put_device(ti
, dc
->dev_read
);
214 static void delay_dtr(struct dm_target
*ti
)
216 struct delay_c
*dc
= ti
->private;
218 destroy_workqueue(dc
->kdelayd_wq
);
220 dm_put_device(ti
, dc
->dev_read
);
223 dm_put_device(ti
, dc
->dev_write
);
228 static int delay_bio(struct delay_c
*dc
, int delay
, struct bio
*bio
)
230 struct dm_delay_info
*delayed
;
231 unsigned long expires
= 0;
233 if (!delay
|| !atomic_read(&dc
->may_delay
))
236 delayed
= dm_per_bio_data(bio
, sizeof(struct dm_delay_info
));
238 delayed
->context
= dc
;
239 delayed
->expires
= expires
= jiffies
+ msecs_to_jiffies(delay
);
241 mutex_lock(&delayed_bios_lock
);
243 if (bio_data_dir(bio
) == WRITE
)
248 list_add_tail(&delayed
->list
, &dc
->delayed_bios
);
250 mutex_unlock(&delayed_bios_lock
);
252 queue_timeout(dc
, expires
);
257 static void delay_presuspend(struct dm_target
*ti
)
259 struct delay_c
*dc
= ti
->private;
261 atomic_set(&dc
->may_delay
, 0);
262 del_timer_sync(&dc
->delay_timer
);
263 flush_bios(flush_delayed_bios(dc
, 1));
266 static void delay_resume(struct dm_target
*ti
)
268 struct delay_c
*dc
= ti
->private;
270 atomic_set(&dc
->may_delay
, 1);
273 static int delay_map(struct dm_target
*ti
, struct bio
*bio
)
275 struct delay_c
*dc
= ti
->private;
277 if ((bio_data_dir(bio
) == WRITE
) && (dc
->dev_write
)) {
278 bio
->bi_bdev
= dc
->dev_write
->bdev
;
279 if (bio_sectors(bio
))
280 bio
->bi_iter
.bi_sector
= dc
->start_write
+
281 dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
283 return delay_bio(dc
, dc
->write_delay
, bio
);
286 bio
->bi_bdev
= dc
->dev_read
->bdev
;
287 bio
->bi_iter
.bi_sector
= dc
->start_read
+
288 dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
290 return delay_bio(dc
, dc
->read_delay
, bio
);
293 static void delay_status(struct dm_target
*ti
, status_type_t type
,
294 unsigned status_flags
, char *result
, unsigned maxlen
)
296 struct delay_c
*dc
= ti
->private;
300 case STATUSTYPE_INFO
:
301 DMEMIT("%u %u", dc
->reads
, dc
->writes
);
304 case STATUSTYPE_TABLE
:
305 DMEMIT("%s %llu %u", dc
->dev_read
->name
,
306 (unsigned long long) dc
->start_read
,
309 DMEMIT(" %s %llu %u", dc
->dev_write
->name
,
310 (unsigned long long) dc
->start_write
,
316 static int delay_iterate_devices(struct dm_target
*ti
,
317 iterate_devices_callout_fn fn
, void *data
)
319 struct delay_c
*dc
= ti
->private;
322 ret
= fn(ti
, dc
->dev_read
, dc
->start_read
, ti
->len
, data
);
327 ret
= fn(ti
, dc
->dev_write
, dc
->start_write
, ti
->len
, data
);
333 static struct target_type delay_target
= {
335 .version
= {1, 2, 1},
336 .module
= THIS_MODULE
,
340 .presuspend
= delay_presuspend
,
341 .resume
= delay_resume
,
342 .status
= delay_status
,
343 .iterate_devices
= delay_iterate_devices
,
346 static int __init
dm_delay_init(void)
350 r
= dm_register_target(&delay_target
);
352 DMERR("register failed %d", r
);
362 static void __exit
dm_delay_exit(void)
364 dm_unregister_target(&delay_target
);
368 module_init(dm_delay_init
);
369 module_exit(dm_delay_exit
);
371 MODULE_DESCRIPTION(DM_NAME
" delay target");
372 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
373 MODULE_LICENSE("GPL");