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 work_struct flush_expired_bios
;
24 struct list_head delayed_bios
;
26 mempool_t
*delayed_pool
;
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
;
43 unsigned long expires
;
46 static DEFINE_MUTEX(delayed_bios_lock
);
48 static struct workqueue_struct
*kdelayd_wq
;
49 static struct kmem_cache
*delayed_cache
;
51 static void handle_delayed_timer(unsigned long data
)
53 struct delay_c
*dc
= (struct delay_c
*)data
;
55 queue_work(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;
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 list_del(&delayed
->list
);
91 bio_list_add(&flush_bios
, delayed
->bio
);
92 if ((bio_data_dir(delayed
->bio
) == WRITE
))
93 delayed
->context
->writes
--;
95 delayed
->context
->reads
--;
96 mempool_free(delayed
, dc
->delayed_pool
);
102 next_expires
= delayed
->expires
;
104 next_expires
= min(next_expires
, delayed
->expires
);
107 mutex_unlock(&delayed_bios_lock
);
110 queue_timeout(dc
, next_expires
);
112 return bio_list_get(&flush_bios
);
115 static void flush_expired_bios(struct work_struct
*work
)
119 dc
= container_of(work
, struct delay_c
, flush_expired_bios
);
120 flush_bios(flush_delayed_bios(dc
, 0));
124 * Mapping parameters:
125 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
127 * With separate write parameters, the first set is only used for reads.
128 * Delays are specified in milliseconds.
130 static int delay_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
133 unsigned long long tmpll
;
136 if (argc
!= 3 && argc
!= 6) {
137 ti
->error
= "requires exactly 3 or 6 arguments";
141 dc
= kmalloc(sizeof(*dc
), GFP_KERNEL
);
143 ti
->error
= "Cannot allocate context";
147 dc
->reads
= dc
->writes
= 0;
149 if (sscanf(argv
[1], "%llu%c", &tmpll
, &dummy
) != 1) {
150 ti
->error
= "Invalid device sector";
153 dc
->start_read
= tmpll
;
155 if (sscanf(argv
[2], "%u%c", &dc
->read_delay
, &dummy
) != 1) {
156 ti
->error
= "Invalid delay";
160 if (dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
),
162 ti
->error
= "Device lookup failed";
166 dc
->dev_write
= NULL
;
170 if (sscanf(argv
[4], "%llu%c", &tmpll
, &dummy
) != 1) {
171 ti
->error
= "Invalid write device sector";
174 dc
->start_write
= tmpll
;
176 if (sscanf(argv
[5], "%u%c", &dc
->write_delay
, &dummy
) != 1) {
177 ti
->error
= "Invalid write delay";
181 if (dm_get_device(ti
, argv
[3], dm_table_get_mode(ti
->table
),
183 ti
->error
= "Write device lookup failed";
188 dc
->delayed_pool
= mempool_create_slab_pool(128, delayed_cache
);
189 if (!dc
->delayed_pool
) {
190 DMERR("Couldn't create delayed bio pool.");
194 setup_timer(&dc
->delay_timer
, handle_delayed_timer
, (unsigned long)dc
);
196 INIT_WORK(&dc
->flush_expired_bios
, flush_expired_bios
);
197 INIT_LIST_HEAD(&dc
->delayed_bios
);
198 mutex_init(&dc
->timer_lock
);
199 atomic_set(&dc
->may_delay
, 1);
201 ti
->num_flush_requests
= 1;
202 ti
->num_discard_requests
= 1;
208 dm_put_device(ti
, dc
->dev_write
);
210 dm_put_device(ti
, dc
->dev_read
);
216 static void delay_dtr(struct dm_target
*ti
)
218 struct delay_c
*dc
= ti
->private;
220 flush_workqueue(kdelayd_wq
);
222 dm_put_device(ti
, dc
->dev_read
);
225 dm_put_device(ti
, dc
->dev_write
);
227 mempool_destroy(dc
->delayed_pool
);
231 static int delay_bio(struct delay_c
*dc
, int delay
, struct bio
*bio
)
233 struct dm_delay_info
*delayed
;
234 unsigned long expires
= 0;
236 if (!delay
|| !atomic_read(&dc
->may_delay
))
239 delayed
= mempool_alloc(dc
->delayed_pool
, GFP_NOIO
);
241 delayed
->context
= dc
;
243 delayed
->expires
= expires
= jiffies
+ (delay
* HZ
/ 1000);
245 mutex_lock(&delayed_bios_lock
);
247 if (bio_data_dir(bio
) == WRITE
)
252 list_add_tail(&delayed
->list
, &dc
->delayed_bios
);
254 mutex_unlock(&delayed_bios_lock
);
256 queue_timeout(dc
, expires
);
261 static void delay_presuspend(struct dm_target
*ti
)
263 struct delay_c
*dc
= ti
->private;
265 atomic_set(&dc
->may_delay
, 0);
266 del_timer_sync(&dc
->delay_timer
);
267 flush_bios(flush_delayed_bios(dc
, 1));
270 static void delay_resume(struct dm_target
*ti
)
272 struct delay_c
*dc
= ti
->private;
274 atomic_set(&dc
->may_delay
, 1);
277 static int delay_map(struct dm_target
*ti
, struct bio
*bio
,
278 union map_info
*map_context
)
280 struct delay_c
*dc
= ti
->private;
282 if ((bio_data_dir(bio
) == WRITE
) && (dc
->dev_write
)) {
283 bio
->bi_bdev
= dc
->dev_write
->bdev
;
284 if (bio_sectors(bio
))
285 bio
->bi_sector
= dc
->start_write
+
286 dm_target_offset(ti
, bio
->bi_sector
);
288 return delay_bio(dc
, dc
->write_delay
, bio
);
291 bio
->bi_bdev
= dc
->dev_read
->bdev
;
292 bio
->bi_sector
= dc
->start_read
+ dm_target_offset(ti
, bio
->bi_sector
);
294 return delay_bio(dc
, dc
->read_delay
, bio
);
297 static int delay_status(struct dm_target
*ti
, status_type_t type
,
298 char *result
, unsigned maxlen
)
300 struct delay_c
*dc
= ti
->private;
304 case STATUSTYPE_INFO
:
305 DMEMIT("%u %u", dc
->reads
, dc
->writes
);
308 case STATUSTYPE_TABLE
:
309 DMEMIT("%s %llu %u", dc
->dev_read
->name
,
310 (unsigned long long) dc
->start_read
,
313 DMEMIT(" %s %llu %u", dc
->dev_write
->name
,
314 (unsigned long long) dc
->start_write
,
322 static int delay_iterate_devices(struct dm_target
*ti
,
323 iterate_devices_callout_fn fn
, void *data
)
325 struct delay_c
*dc
= ti
->private;
328 ret
= fn(ti
, dc
->dev_read
, dc
->start_read
, ti
->len
, data
);
333 ret
= fn(ti
, dc
->dev_write
, dc
->start_write
, ti
->len
, data
);
339 static struct target_type delay_target
= {
341 .version
= {1, 1, 0},
342 .module
= THIS_MODULE
,
346 .presuspend
= delay_presuspend
,
347 .resume
= delay_resume
,
348 .status
= delay_status
,
349 .iterate_devices
= delay_iterate_devices
,
352 static int __init
dm_delay_init(void)
356 kdelayd_wq
= alloc_workqueue("kdelayd", WQ_MEM_RECLAIM
, 0);
358 DMERR("Couldn't start kdelayd");
362 delayed_cache
= KMEM_CACHE(dm_delay_info
, 0);
363 if (!delayed_cache
) {
364 DMERR("Couldn't create delayed bio cache.");
368 r
= dm_register_target(&delay_target
);
370 DMERR("register failed %d", r
);
377 kmem_cache_destroy(delayed_cache
);
379 destroy_workqueue(kdelayd_wq
);
384 static void __exit
dm_delay_exit(void)
386 dm_unregister_target(&delay_target
);
387 kmem_cache_destroy(delayed_cache
);
388 destroy_workqueue(kdelayd_wq
);
392 module_init(dm_delay_init
);
393 module_exit(dm_delay_exit
);
395 MODULE_DESCRIPTION(DM_NAME
" delay target");
396 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
397 MODULE_LICENSE("GPL");