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"
28 struct timer_list delay_timer
;
29 struct mutex timer_lock
;
30 struct workqueue_struct
*kdelayd_wq
;
31 struct work_struct flush_expired_bios
;
32 struct list_head delayed_bios
;
35 struct delay_class read
;
36 struct delay_class write
;
37 struct delay_class flush
;
42 struct dm_delay_info
{
43 struct delay_c
*context
;
44 struct delay_class
*class;
45 struct list_head list
;
46 unsigned long expires
;
49 static DEFINE_MUTEX(delayed_bios_lock
);
51 static void handle_delayed_timer(struct timer_list
*t
)
53 struct delay_c
*dc
= from_timer(dc
, t
, delay_timer
);
55 queue_work(dc
->kdelayd_wq
, &dc
->flush_expired_bios
);
58 static void queue_timeout(struct delay_c
*dc
, unsigned long expires
)
60 mutex_lock(&dc
->timer_lock
);
62 if (!timer_pending(&dc
->delay_timer
) || expires
< dc
->delay_timer
.expires
)
63 mod_timer(&dc
->delay_timer
, expires
);
65 mutex_unlock(&dc
->timer_lock
);
68 static void flush_bios(struct bio
*bio
)
75 generic_make_request(bio
);
80 static struct bio
*flush_delayed_bios(struct delay_c
*dc
, int flush_all
)
82 struct dm_delay_info
*delayed
, *next
;
83 unsigned long next_expires
= 0;
84 unsigned long start_timer
= 0;
85 struct bio_list flush_bios
= { };
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
)) {
90 struct bio
*bio
= dm_bio_from_per_bio_data(delayed
,
91 sizeof(struct dm_delay_info
));
92 list_del(&delayed
->list
);
93 bio_list_add(&flush_bios
, bio
);
94 delayed
->class->ops
--;
100 next_expires
= delayed
->expires
;
102 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));
120 static void delay_dtr(struct dm_target
*ti
)
122 struct delay_c
*dc
= ti
->private;
124 destroy_workqueue(dc
->kdelayd_wq
);
127 dm_put_device(ti
, dc
->read
.dev
);
129 dm_put_device(ti
, dc
->write
.dev
);
131 dm_put_device(ti
, dc
->flush
.dev
);
133 mutex_destroy(&dc
->timer_lock
);
138 static int delay_class_ctr(struct dm_target
*ti
, struct delay_class
*c
, char **argv
)
141 unsigned long long tmpll
;
144 if (sscanf(argv
[1], "%llu%c", &tmpll
, &dummy
) != 1) {
145 ti
->error
= "Invalid device sector";
150 if (sscanf(argv
[2], "%u%c", &c
->delay
, &dummy
) != 1) {
151 ti
->error
= "Invalid delay";
155 ret
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &c
->dev
);
157 ti
->error
= "Device lookup failed";
165 * Mapping parameters:
166 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
168 * With separate write parameters, the first set is only used for reads.
169 * Offsets are specified in sectors.
170 * Delays are specified in milliseconds.
172 static int delay_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
177 if (argc
!= 3 && argc
!= 6 && argc
!= 9) {
178 ti
->error
= "Requires exactly 3, 6 or 9 arguments";
182 dc
= kzalloc(sizeof(*dc
), GFP_KERNEL
);
184 ti
->error
= "Cannot allocate context";
189 timer_setup(&dc
->delay_timer
, handle_delayed_timer
, 0);
190 INIT_WORK(&dc
->flush_expired_bios
, flush_expired_bios
);
191 INIT_LIST_HEAD(&dc
->delayed_bios
);
192 mutex_init(&dc
->timer_lock
);
193 atomic_set(&dc
->may_delay
, 1);
196 ret
= delay_class_ctr(ti
, &dc
->read
, argv
);
201 ret
= delay_class_ctr(ti
, &dc
->write
, argv
);
204 ret
= delay_class_ctr(ti
, &dc
->flush
, argv
);
210 ret
= delay_class_ctr(ti
, &dc
->write
, argv
+ 3);
214 ret
= delay_class_ctr(ti
, &dc
->flush
, argv
+ 3);
220 ret
= delay_class_ctr(ti
, &dc
->flush
, argv
+ 6);
225 dc
->kdelayd_wq
= alloc_workqueue("kdelayd", WQ_MEM_RECLAIM
, 0);
226 if (!dc
->kdelayd_wq
) {
228 DMERR("Couldn't start kdelayd");
232 ti
->num_flush_bios
= 1;
233 ti
->num_discard_bios
= 1;
234 ti
->per_io_data_size
= sizeof(struct dm_delay_info
);
242 static int delay_bio(struct delay_c
*dc
, struct delay_class
*c
, struct bio
*bio
)
244 struct dm_delay_info
*delayed
;
245 unsigned long expires
= 0;
247 if (!c
->delay
|| !atomic_read(&dc
->may_delay
))
248 return DM_MAPIO_REMAPPED
;
250 delayed
= dm_per_bio_data(bio
, sizeof(struct dm_delay_info
));
252 delayed
->context
= dc
;
253 delayed
->expires
= expires
= jiffies
+ msecs_to_jiffies(c
->delay
);
255 mutex_lock(&delayed_bios_lock
);
257 list_add_tail(&delayed
->list
, &dc
->delayed_bios
);
258 mutex_unlock(&delayed_bios_lock
);
260 queue_timeout(dc
, expires
);
262 return DM_MAPIO_SUBMITTED
;
265 static void delay_presuspend(struct dm_target
*ti
)
267 struct delay_c
*dc
= ti
->private;
269 atomic_set(&dc
->may_delay
, 0);
270 del_timer_sync(&dc
->delay_timer
);
271 flush_bios(flush_delayed_bios(dc
, 1));
274 static void delay_resume(struct dm_target
*ti
)
276 struct delay_c
*dc
= ti
->private;
278 atomic_set(&dc
->may_delay
, 1);
281 static int delay_map(struct dm_target
*ti
, struct bio
*bio
)
283 struct delay_c
*dc
= ti
->private;
284 struct delay_class
*c
;
285 struct dm_delay_info
*delayed
= dm_per_bio_data(bio
, sizeof(struct dm_delay_info
));
287 if (bio_data_dir(bio
) == WRITE
) {
288 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
))
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
);
300 return delay_bio(dc
, c
, bio
);
303 #define DMEMIT_DELAY_CLASS(c) \
304 DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
306 static void delay_status(struct dm_target
*ti
, status_type_t type
,
307 unsigned status_flags
, char *result
, unsigned maxlen
)
309 struct delay_c
*dc
= ti
->private;
313 case STATUSTYPE_INFO
:
314 DMEMIT("%u %u %u", dc
->read
.ops
, dc
->write
.ops
, dc
->flush
.ops
);
317 case STATUSTYPE_TABLE
:
318 DMEMIT_DELAY_CLASS(&dc
->read
);
321 DMEMIT_DELAY_CLASS(&dc
->write
);
325 DMEMIT_DELAY_CLASS(&dc
->flush
);
331 static int delay_iterate_devices(struct dm_target
*ti
,
332 iterate_devices_callout_fn fn
, void *data
)
334 struct delay_c
*dc
= ti
->private;
337 ret
= fn(ti
, dc
->read
.dev
, dc
->read
.start
, ti
->len
, data
);
340 ret
= fn(ti
, dc
->write
.dev
, dc
->write
.start
, ti
->len
, data
);
343 ret
= fn(ti
, dc
->flush
.dev
, dc
->flush
.start
, ti
->len
, data
);
351 static struct target_type delay_target
= {
353 .version
= {1, 2, 1},
354 .features
= DM_TARGET_PASSES_INTEGRITY
,
355 .module
= THIS_MODULE
,
359 .presuspend
= delay_presuspend
,
360 .resume
= delay_resume
,
361 .status
= delay_status
,
362 .iterate_devices
= delay_iterate_devices
,
365 static int __init
dm_delay_init(void)
369 r
= dm_register_target(&delay_target
);
371 DMERR("register failed %d", r
);
381 static void __exit
dm_delay_exit(void)
383 dm_unregister_target(&delay_target
);
387 module_init(dm_delay_init
);
388 module_exit(dm_delay_exit
);
390 MODULE_DESCRIPTION(DM_NAME
" delay target");
391 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
392 MODULE_LICENSE("GPL");