13407ef52SJosef Bacik /* 23407ef52SJosef Bacik * Copyright (C) 2003 Sistina Software (UK) Limited. 3a3998799SMike Snitzer * Copyright (C) 2004, 2010-2011 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 18a3998799SMike Snitzer #define all_corrupt_bio_flags_match(bio, fc) \ 191eff9d32SJens Axboe (((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags) 20a3998799SMike Snitzer 213407ef52SJosef Bacik /* 223407ef52SJosef Bacik * Flakey: Used for testing only, simulates intermittent, 233407ef52SJosef Bacik * catastrophic device failure. 243407ef52SJosef Bacik */ 253407ef52SJosef Bacik struct flakey_c { 263407ef52SJosef Bacik struct dm_dev *dev; 273407ef52SJosef Bacik unsigned long start_time; 283407ef52SJosef Bacik sector_t start; 293407ef52SJosef Bacik unsigned up_interval; 303407ef52SJosef Bacik unsigned down_interval; 31b26f5e3dSMike Snitzer unsigned long flags; 32a3998799SMike Snitzer unsigned corrupt_bio_byte; 33a3998799SMike Snitzer unsigned corrupt_bio_rw; 34a3998799SMike Snitzer unsigned corrupt_bio_value; 35a3998799SMike Snitzer unsigned corrupt_bio_flags; 363407ef52SJosef Bacik }; 373407ef52SJosef Bacik 38b26f5e3dSMike Snitzer enum feature_flag_bits { 39ef548c55SMike Snitzer DROP_WRITES, 40ef548c55SMike Snitzer ERROR_WRITES 41b26f5e3dSMike Snitzer }; 42b26f5e3dSMike Snitzer 43c7cfdf59SMikulas Patocka struct per_bio_data { 44c7cfdf59SMikulas Patocka bool bio_submitted; 45c7cfdf59SMikulas Patocka }; 46c7cfdf59SMikulas Patocka 47b26f5e3dSMike Snitzer static int parse_features(struct dm_arg_set *as, struct flakey_c *fc, 48b26f5e3dSMike Snitzer struct dm_target *ti) 49dfd068b0SMike Snitzer { 50dfd068b0SMike Snitzer int r; 51dfd068b0SMike Snitzer unsigned argc; 52dfd068b0SMike Snitzer const char *arg_name; 53dfd068b0SMike Snitzer 545916a22bSEric Biggers static const struct dm_arg _args[] = { 55a3998799SMike Snitzer {0, 6, "Invalid number of feature args"}, 56a3998799SMike Snitzer {1, UINT_MAX, "Invalid corrupt bio byte"}, 57a3998799SMike Snitzer {0, 255, "Invalid corrupt value to write into bio byte (0-255)"}, 58a3998799SMike Snitzer {0, UINT_MAX, "Invalid corrupt bio flags mask"}, 59dfd068b0SMike Snitzer }; 60dfd068b0SMike Snitzer 61dfd068b0SMike Snitzer /* No feature arguments supplied. */ 62dfd068b0SMike Snitzer if (!as->argc) 63dfd068b0SMike Snitzer return 0; 64dfd068b0SMike Snitzer 65dfd068b0SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 66dfd068b0SMike Snitzer if (r) 67a3998799SMike Snitzer return r; 68dfd068b0SMike Snitzer 69a3998799SMike Snitzer while (argc) { 70dfd068b0SMike Snitzer arg_name = dm_shift_arg(as); 71dfd068b0SMike Snitzer argc--; 72dfd068b0SMike Snitzer 737690e253SGoldwyn Rodrigues if (!arg_name) { 747690e253SGoldwyn Rodrigues ti->error = "Insufficient feature arguments"; 757690e253SGoldwyn Rodrigues return -EINVAL; 767690e253SGoldwyn Rodrigues } 777690e253SGoldwyn Rodrigues 78b26f5e3dSMike Snitzer /* 79b26f5e3dSMike Snitzer * drop_writes 80b26f5e3dSMike Snitzer */ 81b26f5e3dSMike Snitzer if (!strcasecmp(arg_name, "drop_writes")) { 82b26f5e3dSMike Snitzer if (test_and_set_bit(DROP_WRITES, &fc->flags)) { 83b26f5e3dSMike Snitzer ti->error = "Feature drop_writes duplicated"; 84b26f5e3dSMike Snitzer return -EINVAL; 85ef548c55SMike Snitzer } else if (test_bit(ERROR_WRITES, &fc->flags)) { 86ef548c55SMike Snitzer ti->error = "Feature drop_writes conflicts with feature error_writes"; 87ef548c55SMike Snitzer return -EINVAL; 88ef548c55SMike Snitzer } 89ef548c55SMike Snitzer 90ef548c55SMike Snitzer continue; 91ef548c55SMike Snitzer } 92ef548c55SMike Snitzer 93ef548c55SMike Snitzer /* 94ef548c55SMike Snitzer * error_writes 95ef548c55SMike Snitzer */ 96ef548c55SMike Snitzer if (!strcasecmp(arg_name, "error_writes")) { 97ef548c55SMike Snitzer if (test_and_set_bit(ERROR_WRITES, &fc->flags)) { 98ef548c55SMike Snitzer ti->error = "Feature error_writes duplicated"; 99ef548c55SMike Snitzer return -EINVAL; 100ef548c55SMike Snitzer 101ef548c55SMike Snitzer } else if (test_bit(DROP_WRITES, &fc->flags)) { 102ef548c55SMike Snitzer ti->error = "Feature error_writes conflicts with feature drop_writes"; 103ef548c55SMike Snitzer return -EINVAL; 104b26f5e3dSMike Snitzer } 105b26f5e3dSMike Snitzer 106b26f5e3dSMike Snitzer continue; 107b26f5e3dSMike Snitzer } 108b26f5e3dSMike Snitzer 109a3998799SMike Snitzer /* 110a3998799SMike Snitzer * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags> 111a3998799SMike Snitzer */ 112a3998799SMike Snitzer if (!strcasecmp(arg_name, "corrupt_bio_byte")) { 11368e58a29SMike Snitzer if (!argc) { 114a3998799SMike Snitzer ti->error = "Feature corrupt_bio_byte requires parameters"; 11568e58a29SMike Snitzer return -EINVAL; 11668e58a29SMike Snitzer } 117a3998799SMike Snitzer 118a3998799SMike Snitzer r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error); 119a3998799SMike Snitzer if (r) 120a3998799SMike Snitzer return r; 121a3998799SMike Snitzer argc--; 122a3998799SMike Snitzer 123a3998799SMike Snitzer /* 124a3998799SMike Snitzer * Direction r or w? 125a3998799SMike Snitzer */ 126a3998799SMike Snitzer arg_name = dm_shift_arg(as); 127a3998799SMike Snitzer if (!strcasecmp(arg_name, "w")) 128a3998799SMike Snitzer fc->corrupt_bio_rw = WRITE; 129a3998799SMike Snitzer else if (!strcasecmp(arg_name, "r")) 130a3998799SMike Snitzer fc->corrupt_bio_rw = READ; 131a3998799SMike Snitzer else { 132a3998799SMike Snitzer ti->error = "Invalid corrupt bio direction (r or w)"; 133a3998799SMike Snitzer return -EINVAL; 134a3998799SMike Snitzer } 135a3998799SMike Snitzer argc--; 136a3998799SMike Snitzer 137a3998799SMike Snitzer /* 138a3998799SMike Snitzer * Value of byte (0-255) to write in place of correct one. 139a3998799SMike Snitzer */ 140a3998799SMike Snitzer r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error); 141a3998799SMike Snitzer if (r) 142a3998799SMike Snitzer return r; 143a3998799SMike Snitzer argc--; 144a3998799SMike Snitzer 145a3998799SMike Snitzer /* 146a3998799SMike Snitzer * Only corrupt bios with these flags set. 147a3998799SMike Snitzer */ 148a3998799SMike Snitzer r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error); 149a3998799SMike Snitzer if (r) 150a3998799SMike Snitzer return r; 151a3998799SMike Snitzer argc--; 152a3998799SMike Snitzer 153a3998799SMike Snitzer continue; 154dfd068b0SMike Snitzer } 155dfd068b0SMike Snitzer 156a3998799SMike Snitzer ti->error = "Unrecognised flakey feature requested"; 157a3998799SMike Snitzer return -EINVAL; 158a3998799SMike Snitzer } 159a3998799SMike Snitzer 160a3998799SMike Snitzer if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) { 161a3998799SMike Snitzer ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set"; 162a3998799SMike Snitzer return -EINVAL; 163ef548c55SMike Snitzer 164ef548c55SMike Snitzer } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) { 165ef548c55SMike Snitzer ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set"; 166ef548c55SMike Snitzer return -EINVAL; 167a3998799SMike Snitzer } 168a3998799SMike Snitzer 169a3998799SMike Snitzer return 0; 170dfd068b0SMike Snitzer } 171dfd068b0SMike Snitzer 1723407ef52SJosef Bacik /* 173dfd068b0SMike Snitzer * Construct a flakey mapping: 174dfd068b0SMike Snitzer * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*] 175b26f5e3dSMike Snitzer * 176b26f5e3dSMike Snitzer * Feature args: 177b26f5e3dSMike Snitzer * [drop_writes] 178a3998799SMike Snitzer * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>] 179a3998799SMike Snitzer * 180a3998799SMike Snitzer * Nth_byte starts from 1 for the first byte. 181a3998799SMike Snitzer * Direction is r for READ or w for WRITE. 182a3998799SMike Snitzer * bio_flags is ignored if 0. 1833407ef52SJosef Bacik */ 1843407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv) 1853407ef52SJosef Bacik { 1865916a22bSEric Biggers static const struct dm_arg _args[] = { 187dfd068b0SMike Snitzer {0, UINT_MAX, "Invalid up interval"}, 188dfd068b0SMike Snitzer {0, UINT_MAX, "Invalid down interval"}, 189dfd068b0SMike Snitzer }; 1903407ef52SJosef Bacik 191dfd068b0SMike Snitzer int r; 192dfd068b0SMike Snitzer struct flakey_c *fc; 193dfd068b0SMike Snitzer unsigned long long tmpll; 194dfd068b0SMike Snitzer struct dm_arg_set as; 195dfd068b0SMike Snitzer const char *devname; 19631998ef1SMikulas Patocka char dummy; 197dfd068b0SMike Snitzer 198dfd068b0SMike Snitzer as.argc = argc; 199dfd068b0SMike Snitzer as.argv = argv; 200dfd068b0SMike Snitzer 201dfd068b0SMike Snitzer if (argc < 4) { 202dfd068b0SMike Snitzer ti->error = "Invalid argument count"; 2033407ef52SJosef Bacik return -EINVAL; 2043407ef52SJosef Bacik } 2053407ef52SJosef Bacik 206b26f5e3dSMike Snitzer fc = kzalloc(sizeof(*fc), GFP_KERNEL); 2073407ef52SJosef Bacik if (!fc) { 20875e3a0f5SAlasdair G Kergon ti->error = "Cannot allocate context"; 2093407ef52SJosef Bacik return -ENOMEM; 2103407ef52SJosef Bacik } 2113407ef52SJosef Bacik fc->start_time = jiffies; 2123407ef52SJosef Bacik 213dfd068b0SMike Snitzer devname = dm_shift_arg(&as); 2143407ef52SJosef Bacik 215e80d1c80SVivek Goyal r = -EINVAL; 216*ef87bfc2SMilan Broz if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) { 217dfd068b0SMike Snitzer ti->error = "Invalid device sector"; 2183407ef52SJosef Bacik goto bad; 2193407ef52SJosef Bacik } 220dfd068b0SMike Snitzer fc->start = tmpll; 2213407ef52SJosef Bacik 222dfd068b0SMike Snitzer r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error); 223dfd068b0SMike Snitzer if (r) 2243407ef52SJosef Bacik goto bad; 225dfd068b0SMike Snitzer 226dfd068b0SMike Snitzer r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error); 227dfd068b0SMike Snitzer if (r) 228dfd068b0SMike Snitzer goto bad; 2293407ef52SJosef Bacik 2303407ef52SJosef Bacik if (!(fc->up_interval + fc->down_interval)) { 231dfd068b0SMike Snitzer ti->error = "Total (up + down) interval is zero"; 232bff7e067SWei Yongjun r = -EINVAL; 2333407ef52SJosef Bacik goto bad; 2343407ef52SJosef Bacik } 2353407ef52SJosef Bacik 2363407ef52SJosef Bacik if (fc->up_interval + fc->down_interval < fc->up_interval) { 237dfd068b0SMike Snitzer ti->error = "Interval overflow"; 238bff7e067SWei Yongjun r = -EINVAL; 2393407ef52SJosef Bacik goto bad; 2403407ef52SJosef Bacik } 2413407ef52SJosef Bacik 242b26f5e3dSMike Snitzer r = parse_features(&as, fc, ti); 243dfd068b0SMike Snitzer if (r) 244dfd068b0SMike Snitzer goto bad; 245dfd068b0SMike Snitzer 246e80d1c80SVivek Goyal r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev); 247e80d1c80SVivek Goyal if (r) { 248dfd068b0SMike Snitzer ti->error = "Device lookup failed"; 2493407ef52SJosef Bacik goto bad; 2503407ef52SJosef Bacik } 2513407ef52SJosef Bacik 25255a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 25355a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 25430187e1dSMike Snitzer ti->per_io_data_size = sizeof(struct per_bio_data); 2553407ef52SJosef Bacik ti->private = fc; 2563407ef52SJosef Bacik return 0; 2573407ef52SJosef Bacik 2583407ef52SJosef Bacik bad: 2593407ef52SJosef Bacik kfree(fc); 260e80d1c80SVivek Goyal return r; 2613407ef52SJosef Bacik } 2623407ef52SJosef Bacik 2633407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti) 2643407ef52SJosef Bacik { 2653407ef52SJosef Bacik struct flakey_c *fc = ti->private; 2663407ef52SJosef Bacik 2673407ef52SJosef Bacik dm_put_device(ti, fc->dev); 2683407ef52SJosef Bacik kfree(fc); 2693407ef52SJosef Bacik } 2703407ef52SJosef Bacik 2713407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector) 2723407ef52SJosef Bacik { 2733407ef52SJosef Bacik struct flakey_c *fc = ti->private; 2743407ef52SJosef Bacik 27530e4171bSMike Snitzer return fc->start + dm_target_offset(ti, bi_sector); 2763407ef52SJosef Bacik } 2773407ef52SJosef Bacik 2783407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio) 2793407ef52SJosef Bacik { 2803407ef52SJosef Bacik struct flakey_c *fc = ti->private; 2813407ef52SJosef Bacik 28274d46992SChristoph Hellwig bio_set_dev(bio, fc->dev->bdev); 283124c4454SDamien Le Moal if (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET) 2844f024f37SKent Overstreet bio->bi_iter.bi_sector = 2854f024f37SKent Overstreet flakey_map_sector(ti, bio->bi_iter.bi_sector); 2863407ef52SJosef Bacik } 2873407ef52SJosef Bacik 288a3998799SMike Snitzer static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc) 289a3998799SMike Snitzer { 290a3998799SMike Snitzer unsigned bio_bytes = bio_cur_bytes(bio); 291a3998799SMike Snitzer char *data = bio_data(bio); 292a3998799SMike Snitzer 293a3998799SMike Snitzer /* 294a3998799SMike Snitzer * Overwrite the Nth byte of the data returned. 295a3998799SMike Snitzer */ 296a3998799SMike Snitzer if (data && bio_bytes >= fc->corrupt_bio_byte) { 297a3998799SMike Snitzer data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value; 298a3998799SMike Snitzer 299a3998799SMike Snitzer DMDEBUG("Corrupting data bio=%p by writing %u to byte %u " 3001eff9d32SJens Axboe "(rw=%c bi_opf=%u bi_sector=%llu cur_bytes=%u)\n", 301a3998799SMike Snitzer bio, fc->corrupt_bio_value, fc->corrupt_bio_byte, 3021eff9d32SJens Axboe (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf, 3034f024f37SKent Overstreet (unsigned long long)bio->bi_iter.bi_sector, bio_bytes); 304a3998799SMike Snitzer } 305a3998799SMike Snitzer } 306a3998799SMike Snitzer 3077de3ee57SMikulas Patocka static int flakey_map(struct dm_target *ti, struct bio *bio) 3083407ef52SJosef Bacik { 3093407ef52SJosef Bacik struct flakey_c *fc = ti->private; 3103407ef52SJosef Bacik unsigned elapsed; 311c7cfdf59SMikulas Patocka struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data)); 312c7cfdf59SMikulas Patocka pb->bio_submitted = false; 3133407ef52SJosef Bacik 314124c4454SDamien Le Moal /* Do not fail reset zone */ 315124c4454SDamien Le Moal if (bio_op(bio) == REQ_OP_ZONE_RESET) 316124c4454SDamien Le Moal goto map_bio; 317124c4454SDamien Le Moal 3183407ef52SJosef Bacik /* Are we alive ? */ 3193407ef52SJosef Bacik elapsed = (jiffies - fc->start_time) / HZ; 320b26f5e3dSMike Snitzer if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) { 321a3998799SMike Snitzer /* 322a3998799SMike Snitzer * Flag this bio as submitted while down. 323a3998799SMike Snitzer */ 324c7cfdf59SMikulas Patocka pb->bio_submitted = true; 3253407ef52SJosef Bacik 326b26f5e3dSMike Snitzer /* 327ef548c55SMike Snitzer * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set. 328299f6230SMike Snitzer * Otherwise, flakey_end_io() will decide if the reads should be modified. 329a3998799SMike Snitzer */ 33099f3c90dSMike Snitzer if (bio_data_dir(bio) == READ) { 331ef548c55SMike Snitzer if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) && 332ef548c55SMike Snitzer !test_bit(ERROR_WRITES, &fc->flags)) 333846785e6SChristoph Hellwig return DM_MAPIO_KILL; 334299f6230SMike Snitzer goto map_bio; 33599f3c90dSMike Snitzer } 336a3998799SMike Snitzer 337a3998799SMike Snitzer /* 338ef548c55SMike Snitzer * Drop or error writes? 339b26f5e3dSMike Snitzer */ 340b26f5e3dSMike Snitzer if (test_bit(DROP_WRITES, &fc->flags)) { 3414246a0b6SChristoph Hellwig bio_endio(bio); 342b26f5e3dSMike Snitzer return DM_MAPIO_SUBMITTED; 343b26f5e3dSMike Snitzer } 344ef548c55SMike Snitzer else if (test_bit(ERROR_WRITES, &fc->flags)) { 345ef548c55SMike Snitzer bio_io_error(bio); 346ef548c55SMike Snitzer return DM_MAPIO_SUBMITTED; 347ef548c55SMike Snitzer } 348a3998799SMike Snitzer 349a3998799SMike Snitzer /* 350a3998799SMike Snitzer * Corrupt matching writes. 351a3998799SMike Snitzer */ 352a3998799SMike Snitzer if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) { 353a3998799SMike Snitzer if (all_corrupt_bio_flags_match(bio, fc)) 354a3998799SMike Snitzer corrupt_bio_data(bio, fc); 355b26f5e3dSMike Snitzer goto map_bio; 356b26f5e3dSMike Snitzer } 357b26f5e3dSMike Snitzer 358b26f5e3dSMike Snitzer /* 359a3998799SMike Snitzer * By default, error all I/O. 360b26f5e3dSMike Snitzer */ 361846785e6SChristoph Hellwig return DM_MAPIO_KILL; 362b26f5e3dSMike Snitzer } 363b26f5e3dSMike Snitzer 364b26f5e3dSMike Snitzer map_bio: 3653407ef52SJosef Bacik flakey_map_bio(ti, bio); 3663407ef52SJosef Bacik 3673407ef52SJosef Bacik return DM_MAPIO_REMAPPED; 3683407ef52SJosef Bacik } 3693407ef52SJosef Bacik 3704e4cbee9SChristoph Hellwig static int flakey_end_io(struct dm_target *ti, struct bio *bio, 3714e4cbee9SChristoph Hellwig blk_status_t *error) 372a3998799SMike Snitzer { 373a3998799SMike Snitzer struct flakey_c *fc = ti->private; 374c7cfdf59SMikulas Patocka struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data)); 375a3998799SMike Snitzer 376124c4454SDamien Le Moal if (bio_op(bio) == REQ_OP_ZONE_RESET) 377124c4454SDamien Le Moal return DM_ENDIO_DONE; 378124c4454SDamien Le Moal 3791be56909SChristoph Hellwig if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) { 380299f6230SMike Snitzer if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) && 381299f6230SMike Snitzer all_corrupt_bio_flags_match(bio, fc)) { 382299f6230SMike Snitzer /* 383299f6230SMike Snitzer * Corrupt successful matching READs while in down state. 384299f6230SMike Snitzer */ 385a3998799SMike Snitzer corrupt_bio_data(bio, fc); 386299f6230SMike Snitzer 387ef548c55SMike Snitzer } else if (!test_bit(DROP_WRITES, &fc->flags) && 388ef548c55SMike Snitzer !test_bit(ERROR_WRITES, &fc->flags)) { 389299f6230SMike Snitzer /* 390299f6230SMike Snitzer * Error read during the down_interval if drop_writes 391ef548c55SMike Snitzer * and error_writes were not configured. 392299f6230SMike Snitzer */ 3934e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 39499f3c90dSMike Snitzer } 395299f6230SMike Snitzer } 396a3998799SMike Snitzer 3971be56909SChristoph Hellwig return DM_ENDIO_DONE; 398a3998799SMike Snitzer } 399a3998799SMike Snitzer 400fd7c092eSMikulas Patocka static void flakey_status(struct dm_target *ti, status_type_t type, 4011f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 4023407ef52SJosef Bacik { 403b26f5e3dSMike Snitzer unsigned sz = 0; 4043407ef52SJosef Bacik struct flakey_c *fc = ti->private; 405ef548c55SMike Snitzer unsigned drop_writes, error_writes; 4063407ef52SJosef Bacik 4073407ef52SJosef Bacik switch (type) { 4083407ef52SJosef Bacik case STATUSTYPE_INFO: 4093407ef52SJosef Bacik result[0] = '\0'; 4103407ef52SJosef Bacik break; 4113407ef52SJosef Bacik 4123407ef52SJosef Bacik case STATUSTYPE_TABLE: 413b26f5e3dSMike Snitzer DMEMIT("%s %llu %u %u ", fc->dev->name, 4143407ef52SJosef Bacik (unsigned long long)fc->start, fc->up_interval, 4153407ef52SJosef Bacik fc->down_interval); 416b26f5e3dSMike Snitzer 417b26f5e3dSMike Snitzer drop_writes = test_bit(DROP_WRITES, &fc->flags); 418ef548c55SMike Snitzer error_writes = test_bit(ERROR_WRITES, &fc->flags); 419ef548c55SMike Snitzer DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5); 420a3998799SMike Snitzer 421b26f5e3dSMike Snitzer if (drop_writes) 422b26f5e3dSMike Snitzer DMEMIT("drop_writes "); 423ef548c55SMike Snitzer else if (error_writes) 424ef548c55SMike Snitzer DMEMIT("error_writes "); 425a3998799SMike Snitzer 426a3998799SMike Snitzer if (fc->corrupt_bio_byte) 427a3998799SMike Snitzer DMEMIT("corrupt_bio_byte %u %c %u %u ", 428a3998799SMike Snitzer fc->corrupt_bio_byte, 429a3998799SMike Snitzer (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r', 430a3998799SMike Snitzer fc->corrupt_bio_value, fc->corrupt_bio_flags); 431a3998799SMike Snitzer 4323407ef52SJosef Bacik break; 4333407ef52SJosef Bacik } 4343407ef52SJosef Bacik } 4353407ef52SJosef Bacik 4365bd5e8d8SMike Snitzer static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev) 4373407ef52SJosef Bacik { 4383407ef52SJosef Bacik struct flakey_c *fc = ti->private; 439e56f81e0SChristoph Hellwig 440e56f81e0SChristoph Hellwig *bdev = fc->dev->bdev; 4413407ef52SJosef Bacik 442ec8013beSPaolo Bonzini /* 443ec8013beSPaolo Bonzini * Only pass ioctls through if the device sizes match exactly. 444ec8013beSPaolo Bonzini */ 445ec8013beSPaolo Bonzini if (fc->start || 446e56f81e0SChristoph Hellwig ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 447e56f81e0SChristoph Hellwig return 1; 448e56f81e0SChristoph Hellwig return 0; 4493407ef52SJosef Bacik } 4503407ef52SJosef Bacik 451e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED 452e76239a3SChristoph Hellwig static int flakey_report_zones(struct dm_target *ti, sector_t sector, 453e76239a3SChristoph Hellwig struct blk_zone *zones, unsigned int *nr_zones, 454e76239a3SChristoph Hellwig gfp_t gfp_mask) 455e76239a3SChristoph Hellwig { 456e76239a3SChristoph Hellwig struct flakey_c *fc = ti->private; 457e76239a3SChristoph Hellwig int ret; 458e76239a3SChristoph Hellwig 459e76239a3SChristoph Hellwig /* Do report and remap it */ 460e76239a3SChristoph Hellwig ret = blkdev_report_zones(fc->dev->bdev, flakey_map_sector(ti, sector), 461e76239a3SChristoph Hellwig zones, nr_zones, gfp_mask); 462e76239a3SChristoph Hellwig if (ret != 0) 463e76239a3SChristoph Hellwig return ret; 464e76239a3SChristoph Hellwig 465e76239a3SChristoph Hellwig if (*nr_zones) 466e76239a3SChristoph Hellwig dm_remap_zone_report(ti, fc->start, zones, nr_zones); 467e76239a3SChristoph Hellwig return 0; 468e76239a3SChristoph Hellwig } 469e76239a3SChristoph Hellwig #endif 470e76239a3SChristoph Hellwig 4713407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data) 4723407ef52SJosef Bacik { 4733407ef52SJosef Bacik struct flakey_c *fc = ti->private; 4743407ef52SJosef Bacik 4753407ef52SJosef Bacik return fn(ti, fc->dev, fc->start, ti->len, data); 4763407ef52SJosef Bacik } 4773407ef52SJosef Bacik 4783407ef52SJosef Bacik static struct target_type flakey_target = { 4793407ef52SJosef Bacik .name = "flakey", 480124c4454SDamien Le Moal .version = {1, 5, 0}, 481118aa47cSDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED 482124c4454SDamien Le Moal .features = DM_TARGET_ZONED_HM, 483e76239a3SChristoph Hellwig .report_zones = flakey_report_zones, 484118aa47cSDamien Le Moal #endif 4853407ef52SJosef Bacik .module = THIS_MODULE, 4863407ef52SJosef Bacik .ctr = flakey_ctr, 4873407ef52SJosef Bacik .dtr = flakey_dtr, 4883407ef52SJosef Bacik .map = flakey_map, 489a3998799SMike Snitzer .end_io = flakey_end_io, 4903407ef52SJosef Bacik .status = flakey_status, 491e56f81e0SChristoph Hellwig .prepare_ioctl = flakey_prepare_ioctl, 4923407ef52SJosef Bacik .iterate_devices = flakey_iterate_devices, 4933407ef52SJosef Bacik }; 4943407ef52SJosef Bacik 4953407ef52SJosef Bacik static int __init dm_flakey_init(void) 4963407ef52SJosef Bacik { 4973407ef52SJosef Bacik int r = dm_register_target(&flakey_target); 4983407ef52SJosef Bacik 4993407ef52SJosef Bacik if (r < 0) 5003407ef52SJosef Bacik DMERR("register failed %d", r); 5013407ef52SJosef Bacik 5023407ef52SJosef Bacik return r; 5033407ef52SJosef Bacik } 5043407ef52SJosef Bacik 5053407ef52SJosef Bacik static void __exit dm_flakey_exit(void) 5063407ef52SJosef Bacik { 5073407ef52SJosef Bacik dm_unregister_target(&flakey_target); 5083407ef52SJosef Bacik } 5093407ef52SJosef Bacik 5103407ef52SJosef Bacik /* Module hooks */ 5113407ef52SJosef Bacik module_init(dm_flakey_init); 5123407ef52SJosef Bacik module_exit(dm_flakey_exit); 5133407ef52SJosef Bacik 5143407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target"); 5153407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 5163407ef52SJosef Bacik MODULE_LICENSE("GPL"); 517