13407ef52SJosef Bacik /* 23407ef52SJosef Bacik * Copyright (C) 2003 Sistina Software (UK) Limited. 33407ef52SJosef Bacik * Copyright (C) 2004, 2010 Red Hat, Inc. All rights reserved. 43407ef52SJosef Bacik * 53407ef52SJosef Bacik * This file is released under the GPL. 63407ef52SJosef Bacik */ 73407ef52SJosef Bacik 83407ef52SJosef Bacik #include <linux/device-mapper.h> 93407ef52SJosef Bacik 103407ef52SJosef Bacik #include <linux/module.h> 113407ef52SJosef Bacik #include <linux/init.h> 123407ef52SJosef Bacik #include <linux/blkdev.h> 133407ef52SJosef Bacik #include <linux/bio.h> 143407ef52SJosef Bacik #include <linux/slab.h> 153407ef52SJosef Bacik 163407ef52SJosef Bacik #define DM_MSG_PREFIX "flakey" 173407ef52SJosef Bacik 183407ef52SJosef Bacik /* 193407ef52SJosef Bacik * Flakey: Used for testing only, simulates intermittent, 203407ef52SJosef Bacik * catastrophic device failure. 213407ef52SJosef Bacik */ 223407ef52SJosef Bacik struct flakey_c { 233407ef52SJosef Bacik struct dm_dev *dev; 243407ef52SJosef Bacik unsigned long start_time; 253407ef52SJosef Bacik sector_t start; 263407ef52SJosef Bacik unsigned up_interval; 273407ef52SJosef Bacik unsigned down_interval; 283407ef52SJosef Bacik }; 293407ef52SJosef Bacik 303407ef52SJosef Bacik /* 313407ef52SJosef Bacik * Construct a flakey mapping: <dev_path> <offset> <up interval> <down interval> 323407ef52SJosef Bacik */ 333407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv) 343407ef52SJosef Bacik { 353407ef52SJosef Bacik struct flakey_c *fc; 363407ef52SJosef Bacik unsigned long long tmp; 373407ef52SJosef Bacik 383407ef52SJosef Bacik if (argc != 4) { 393407ef52SJosef Bacik ti->error = "dm-flakey: Invalid argument count"; 403407ef52SJosef Bacik return -EINVAL; 413407ef52SJosef Bacik } 423407ef52SJosef Bacik 433407ef52SJosef Bacik fc = kmalloc(sizeof(*fc), GFP_KERNEL); 443407ef52SJosef Bacik if (!fc) { 453407ef52SJosef Bacik ti->error = "dm-flakey: Cannot allocate linear context"; 463407ef52SJosef Bacik return -ENOMEM; 473407ef52SJosef Bacik } 483407ef52SJosef Bacik fc->start_time = jiffies; 493407ef52SJosef Bacik 503407ef52SJosef Bacik if (sscanf(argv[1], "%llu", &tmp) != 1) { 513407ef52SJosef Bacik ti->error = "dm-flakey: Invalid device sector"; 523407ef52SJosef Bacik goto bad; 533407ef52SJosef Bacik } 543407ef52SJosef Bacik fc->start = tmp; 553407ef52SJosef Bacik 563407ef52SJosef Bacik if (sscanf(argv[2], "%u", &fc->up_interval) != 1) { 573407ef52SJosef Bacik ti->error = "dm-flakey: Invalid up interval"; 583407ef52SJosef Bacik goto bad; 593407ef52SJosef Bacik } 603407ef52SJosef Bacik 613407ef52SJosef Bacik if (sscanf(argv[3], "%u", &fc->down_interval) != 1) { 623407ef52SJosef Bacik ti->error = "dm-flakey: Invalid down interval"; 633407ef52SJosef Bacik goto bad; 643407ef52SJosef Bacik } 653407ef52SJosef Bacik 663407ef52SJosef Bacik if (!(fc->up_interval + fc->down_interval)) { 673407ef52SJosef Bacik ti->error = "dm-flakey: Total (up + down) interval is zero"; 683407ef52SJosef Bacik goto bad; 693407ef52SJosef Bacik } 703407ef52SJosef Bacik 713407ef52SJosef Bacik if (fc->up_interval + fc->down_interval < fc->up_interval) { 723407ef52SJosef Bacik ti->error = "dm-flakey: Interval overflow"; 733407ef52SJosef Bacik goto bad; 743407ef52SJosef Bacik } 753407ef52SJosef Bacik 763407ef52SJosef Bacik if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &fc->dev)) { 773407ef52SJosef Bacik ti->error = "dm-flakey: Device lookup failed"; 783407ef52SJosef Bacik goto bad; 793407ef52SJosef Bacik } 803407ef52SJosef Bacik 813407ef52SJosef Bacik ti->num_flush_requests = 1; 82*30e4171bSMike Snitzer ti->num_discard_requests = 1; 833407ef52SJosef Bacik ti->private = fc; 843407ef52SJosef Bacik return 0; 853407ef52SJosef Bacik 863407ef52SJosef Bacik bad: 873407ef52SJosef Bacik kfree(fc); 883407ef52SJosef Bacik return -EINVAL; 893407ef52SJosef Bacik } 903407ef52SJosef Bacik 913407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti) 923407ef52SJosef Bacik { 933407ef52SJosef Bacik struct flakey_c *fc = ti->private; 943407ef52SJosef Bacik 953407ef52SJosef Bacik dm_put_device(ti, fc->dev); 963407ef52SJosef Bacik kfree(fc); 973407ef52SJosef Bacik } 983407ef52SJosef Bacik 993407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector) 1003407ef52SJosef Bacik { 1013407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1023407ef52SJosef Bacik 103*30e4171bSMike Snitzer return fc->start + dm_target_offset(ti, bi_sector); 1043407ef52SJosef Bacik } 1053407ef52SJosef Bacik 1063407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio) 1073407ef52SJosef Bacik { 1083407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1093407ef52SJosef Bacik 1103407ef52SJosef Bacik bio->bi_bdev = fc->dev->bdev; 1113407ef52SJosef Bacik if (bio_sectors(bio)) 1123407ef52SJosef Bacik bio->bi_sector = flakey_map_sector(ti, bio->bi_sector); 1133407ef52SJosef Bacik } 1143407ef52SJosef Bacik 1153407ef52SJosef Bacik static int flakey_map(struct dm_target *ti, struct bio *bio, 1163407ef52SJosef Bacik union map_info *map_context) 1173407ef52SJosef Bacik { 1183407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1193407ef52SJosef Bacik unsigned elapsed; 1203407ef52SJosef Bacik 1213407ef52SJosef Bacik /* Are we alive ? */ 1223407ef52SJosef Bacik elapsed = (jiffies - fc->start_time) / HZ; 1233407ef52SJosef Bacik if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) 1243407ef52SJosef Bacik return -EIO; 1253407ef52SJosef Bacik 1263407ef52SJosef Bacik flakey_map_bio(ti, bio); 1273407ef52SJosef Bacik 1283407ef52SJosef Bacik return DM_MAPIO_REMAPPED; 1293407ef52SJosef Bacik } 1303407ef52SJosef Bacik 1313407ef52SJosef Bacik static int flakey_status(struct dm_target *ti, status_type_t type, 1323407ef52SJosef Bacik char *result, unsigned int maxlen) 1333407ef52SJosef Bacik { 1343407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1353407ef52SJosef Bacik 1363407ef52SJosef Bacik switch (type) { 1373407ef52SJosef Bacik case STATUSTYPE_INFO: 1383407ef52SJosef Bacik result[0] = '\0'; 1393407ef52SJosef Bacik break; 1403407ef52SJosef Bacik 1413407ef52SJosef Bacik case STATUSTYPE_TABLE: 1423407ef52SJosef Bacik snprintf(result, maxlen, "%s %llu %u %u", fc->dev->name, 1433407ef52SJosef Bacik (unsigned long long)fc->start, fc->up_interval, 1443407ef52SJosef Bacik fc->down_interval); 1453407ef52SJosef Bacik break; 1463407ef52SJosef Bacik } 1473407ef52SJosef Bacik return 0; 1483407ef52SJosef Bacik } 1493407ef52SJosef Bacik 1503407ef52SJosef Bacik static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg) 1513407ef52SJosef Bacik { 1523407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1533407ef52SJosef Bacik 1543407ef52SJosef Bacik return __blkdev_driver_ioctl(fc->dev->bdev, fc->dev->mode, cmd, arg); 1553407ef52SJosef Bacik } 1563407ef52SJosef Bacik 1573407ef52SJosef Bacik static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm, 1583407ef52SJosef Bacik struct bio_vec *biovec, int max_size) 1593407ef52SJosef Bacik { 1603407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1613407ef52SJosef Bacik struct request_queue *q = bdev_get_queue(fc->dev->bdev); 1623407ef52SJosef Bacik 1633407ef52SJosef Bacik if (!q->merge_bvec_fn) 1643407ef52SJosef Bacik return max_size; 1653407ef52SJosef Bacik 1663407ef52SJosef Bacik bvm->bi_bdev = fc->dev->bdev; 1673407ef52SJosef Bacik bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector); 1683407ef52SJosef Bacik 1693407ef52SJosef Bacik return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); 1703407ef52SJosef Bacik } 1713407ef52SJosef Bacik 1723407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data) 1733407ef52SJosef Bacik { 1743407ef52SJosef Bacik struct flakey_c *fc = ti->private; 1753407ef52SJosef Bacik 1763407ef52SJosef Bacik return fn(ti, fc->dev, fc->start, ti->len, data); 1773407ef52SJosef Bacik } 1783407ef52SJosef Bacik 1793407ef52SJosef Bacik static struct target_type flakey_target = { 1803407ef52SJosef Bacik .name = "flakey", 1813407ef52SJosef Bacik .version = {1, 1, 0}, 1823407ef52SJosef Bacik .module = THIS_MODULE, 1833407ef52SJosef Bacik .ctr = flakey_ctr, 1843407ef52SJosef Bacik .dtr = flakey_dtr, 1853407ef52SJosef Bacik .map = flakey_map, 1863407ef52SJosef Bacik .status = flakey_status, 1873407ef52SJosef Bacik .ioctl = flakey_ioctl, 1883407ef52SJosef Bacik .merge = flakey_merge, 1893407ef52SJosef Bacik .iterate_devices = flakey_iterate_devices, 1903407ef52SJosef Bacik }; 1913407ef52SJosef Bacik 1923407ef52SJosef Bacik static int __init dm_flakey_init(void) 1933407ef52SJosef Bacik { 1943407ef52SJosef Bacik int r = dm_register_target(&flakey_target); 1953407ef52SJosef Bacik 1963407ef52SJosef Bacik if (r < 0) 1973407ef52SJosef Bacik DMERR("register failed %d", r); 1983407ef52SJosef Bacik 1993407ef52SJosef Bacik return r; 2003407ef52SJosef Bacik } 2013407ef52SJosef Bacik 2023407ef52SJosef Bacik static void __exit dm_flakey_exit(void) 2033407ef52SJosef Bacik { 2043407ef52SJosef Bacik dm_unregister_target(&flakey_target); 2053407ef52SJosef Bacik } 2063407ef52SJosef Bacik 2073407ef52SJosef Bacik /* Module hooks */ 2083407ef52SJosef Bacik module_init(dm_flakey_init); 2093407ef52SJosef Bacik module_exit(dm_flakey_exit); 2103407ef52SJosef Bacik 2113407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target"); 2123407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 2133407ef52SJosef Bacik MODULE_LICENSE("GPL"); 214