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 mempool_t *delayed_pool; 28 29 struct dm_dev *dev_read; 30 sector_t start_read; 31 unsigned read_delay; 32 unsigned reads; 33 34 struct dm_dev *dev_write; 35 sector_t start_write; 36 unsigned write_delay; 37 unsigned writes; 38 }; 39 40 struct dm_delay_info { 41 struct delay_c *context; 42 struct list_head list; 43 struct bio *bio; 44 unsigned long expires; 45 }; 46 47 static DEFINE_MUTEX(delayed_bios_lock); 48 49 static struct kmem_cache *delayed_cache; 50 51 static void handle_delayed_timer(unsigned long data) 52 { 53 struct delay_c *dc = (struct delay_c *)data; 54 55 queue_work(dc->kdelayd_wq, &dc->flush_expired_bios); 56 } 57 58 static void queue_timeout(struct delay_c *dc, unsigned long expires) 59 { 60 mutex_lock(&dc->timer_lock); 61 62 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires) 63 mod_timer(&dc->delay_timer, expires); 64 65 mutex_unlock(&dc->timer_lock); 66 } 67 68 static void flush_bios(struct bio *bio) 69 { 70 struct bio *n; 71 72 while (bio) { 73 n = bio->bi_next; 74 bio->bi_next = NULL; 75 generic_make_request(bio); 76 bio = n; 77 } 78 } 79 80 static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all) 81 { 82 struct dm_delay_info *delayed, *next; 83 unsigned long next_expires = 0; 84 int start_timer = 0; 85 struct bio_list flush_bios = { }; 86 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--; 94 else 95 delayed->context->reads--; 96 mempool_free(delayed, dc->delayed_pool); 97 continue; 98 } 99 100 if (!start_timer) { 101 start_timer = 1; 102 next_expires = delayed->expires; 103 } else 104 next_expires = min(next_expires, delayed->expires); 105 } 106 107 mutex_unlock(&delayed_bios_lock); 108 109 if (start_timer) 110 queue_timeout(dc, next_expires); 111 112 return bio_list_get(&flush_bios); 113 } 114 115 static void flush_expired_bios(struct work_struct *work) 116 { 117 struct delay_c *dc; 118 119 dc = container_of(work, struct delay_c, flush_expired_bios); 120 flush_bios(flush_delayed_bios(dc, 0)); 121 } 122 123 /* 124 * Mapping parameters: 125 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>] 126 * 127 * With separate write parameters, the first set is only used for reads. 128 * Delays are specified in milliseconds. 129 */ 130 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv) 131 { 132 struct delay_c *dc; 133 unsigned long long tmpll; 134 char dummy; 135 136 if (argc != 3 && argc != 6) { 137 ti->error = "requires exactly 3 or 6 arguments"; 138 return -EINVAL; 139 } 140 141 dc = kmalloc(sizeof(*dc), GFP_KERNEL); 142 if (!dc) { 143 ti->error = "Cannot allocate context"; 144 return -ENOMEM; 145 } 146 147 dc->reads = dc->writes = 0; 148 149 if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) { 150 ti->error = "Invalid device sector"; 151 goto bad; 152 } 153 dc->start_read = tmpll; 154 155 if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) { 156 ti->error = "Invalid delay"; 157 goto bad; 158 } 159 160 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), 161 &dc->dev_read)) { 162 ti->error = "Device lookup failed"; 163 goto bad; 164 } 165 166 dc->dev_write = NULL; 167 if (argc == 3) 168 goto out; 169 170 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) { 171 ti->error = "Invalid write device sector"; 172 goto bad_dev_read; 173 } 174 dc->start_write = tmpll; 175 176 if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) { 177 ti->error = "Invalid write delay"; 178 goto bad_dev_read; 179 } 180 181 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), 182 &dc->dev_write)) { 183 ti->error = "Write device lookup failed"; 184 goto bad_dev_read; 185 } 186 187 out: 188 dc->delayed_pool = mempool_create_slab_pool(128, delayed_cache); 189 if (!dc->delayed_pool) { 190 DMERR("Couldn't create delayed bio pool."); 191 goto bad_dev_write; 192 } 193 194 dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0); 195 if (!dc->kdelayd_wq) { 196 DMERR("Couldn't start kdelayd"); 197 goto bad_queue; 198 } 199 200 setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc); 201 202 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios); 203 INIT_LIST_HEAD(&dc->delayed_bios); 204 mutex_init(&dc->timer_lock); 205 atomic_set(&dc->may_delay, 1); 206 207 ti->num_flush_bios = 1; 208 ti->num_discard_bios = 1; 209 ti->private = dc; 210 return 0; 211 212 bad_queue: 213 mempool_destroy(dc->delayed_pool); 214 bad_dev_write: 215 if (dc->dev_write) 216 dm_put_device(ti, dc->dev_write); 217 bad_dev_read: 218 dm_put_device(ti, dc->dev_read); 219 bad: 220 kfree(dc); 221 return -EINVAL; 222 } 223 224 static void delay_dtr(struct dm_target *ti) 225 { 226 struct delay_c *dc = ti->private; 227 228 destroy_workqueue(dc->kdelayd_wq); 229 230 dm_put_device(ti, dc->dev_read); 231 232 if (dc->dev_write) 233 dm_put_device(ti, dc->dev_write); 234 235 mempool_destroy(dc->delayed_pool); 236 kfree(dc); 237 } 238 239 static int delay_bio(struct delay_c *dc, int delay, struct bio *bio) 240 { 241 struct dm_delay_info *delayed; 242 unsigned long expires = 0; 243 244 if (!delay || !atomic_read(&dc->may_delay)) 245 return 1; 246 247 delayed = mempool_alloc(dc->delayed_pool, GFP_NOIO); 248 249 delayed->context = dc; 250 delayed->bio = bio; 251 delayed->expires = expires = jiffies + (delay * HZ / 1000); 252 253 mutex_lock(&delayed_bios_lock); 254 255 if (bio_data_dir(bio) == WRITE) 256 dc->writes++; 257 else 258 dc->reads++; 259 260 list_add_tail(&delayed->list, &dc->delayed_bios); 261 262 mutex_unlock(&delayed_bios_lock); 263 264 queue_timeout(dc, expires); 265 266 return 0; 267 } 268 269 static void delay_presuspend(struct dm_target *ti) 270 { 271 struct delay_c *dc = ti->private; 272 273 atomic_set(&dc->may_delay, 0); 274 del_timer_sync(&dc->delay_timer); 275 flush_bios(flush_delayed_bios(dc, 1)); 276 } 277 278 static void delay_resume(struct dm_target *ti) 279 { 280 struct delay_c *dc = ti->private; 281 282 atomic_set(&dc->may_delay, 1); 283 } 284 285 static int delay_map(struct dm_target *ti, struct bio *bio) 286 { 287 struct delay_c *dc = ti->private; 288 289 if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) { 290 bio->bi_bdev = dc->dev_write->bdev; 291 if (bio_sectors(bio)) 292 bio->bi_sector = dc->start_write + 293 dm_target_offset(ti, bio->bi_sector); 294 295 return delay_bio(dc, dc->write_delay, bio); 296 } 297 298 bio->bi_bdev = dc->dev_read->bdev; 299 bio->bi_sector = dc->start_read + dm_target_offset(ti, bio->bi_sector); 300 301 return delay_bio(dc, dc->read_delay, bio); 302 } 303 304 static void delay_status(struct dm_target *ti, status_type_t type, 305 unsigned status_flags, char *result, unsigned maxlen) 306 { 307 struct delay_c *dc = ti->private; 308 int sz = 0; 309 310 switch (type) { 311 case STATUSTYPE_INFO: 312 DMEMIT("%u %u", dc->reads, dc->writes); 313 break; 314 315 case STATUSTYPE_TABLE: 316 DMEMIT("%s %llu %u", dc->dev_read->name, 317 (unsigned long long) dc->start_read, 318 dc->read_delay); 319 if (dc->dev_write) 320 DMEMIT(" %s %llu %u", dc->dev_write->name, 321 (unsigned long long) dc->start_write, 322 dc->write_delay); 323 break; 324 } 325 } 326 327 static int delay_iterate_devices(struct dm_target *ti, 328 iterate_devices_callout_fn fn, void *data) 329 { 330 struct delay_c *dc = ti->private; 331 int ret = 0; 332 333 ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data); 334 if (ret) 335 goto out; 336 337 if (dc->dev_write) 338 ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data); 339 340 out: 341 return ret; 342 } 343 344 static struct target_type delay_target = { 345 .name = "delay", 346 .version = {1, 2, 1}, 347 .module = THIS_MODULE, 348 .ctr = delay_ctr, 349 .dtr = delay_dtr, 350 .map = delay_map, 351 .presuspend = delay_presuspend, 352 .resume = delay_resume, 353 .status = delay_status, 354 .iterate_devices = delay_iterate_devices, 355 }; 356 357 static int __init dm_delay_init(void) 358 { 359 int r = -ENOMEM; 360 361 delayed_cache = KMEM_CACHE(dm_delay_info, 0); 362 if (!delayed_cache) { 363 DMERR("Couldn't create delayed bio cache."); 364 goto bad_memcache; 365 } 366 367 r = dm_register_target(&delay_target); 368 if (r < 0) { 369 DMERR("register failed %d", r); 370 goto bad_register; 371 } 372 373 return 0; 374 375 bad_register: 376 kmem_cache_destroy(delayed_cache); 377 bad_memcache: 378 return r; 379 } 380 381 static void __exit dm_delay_exit(void) 382 { 383 dm_unregister_target(&delay_target); 384 kmem_cache_destroy(delayed_cache); 385 } 386 387 /* Module hooks */ 388 module_init(dm_delay_init); 389 module_exit(dm_delay_exit); 390 391 MODULE_DESCRIPTION(DM_NAME " delay target"); 392 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>"); 393 MODULE_LICENSE("GPL"); 394