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; 35eff17e51SBart Van Assche blk_opf_t 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 */ 148eff17e51SBart Van Assche BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) != 149eff17e51SBart Van Assche sizeof(unsigned int)); 150eff17e51SBart Van Assche r = dm_read_arg(_args + 3, as, 151eff17e51SBart Van Assche (__force unsigned *)&fc->corrupt_bio_flags, 152eff17e51SBart Van Assche &ti->error); 153a3998799SMike Snitzer if (r) 154a3998799SMike Snitzer return r; 155a3998799SMike Snitzer argc--; 156a3998799SMike Snitzer 157a3998799SMike Snitzer continue; 158dfd068b0SMike Snitzer } 159dfd068b0SMike Snitzer 160a3998799SMike Snitzer ti->error = "Unrecognised flakey feature requested"; 161a3998799SMike Snitzer return -EINVAL; 162a3998799SMike Snitzer } 163a3998799SMike Snitzer 164a3998799SMike Snitzer if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) { 165a3998799SMike Snitzer ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set"; 166a3998799SMike Snitzer return -EINVAL; 167ef548c55SMike Snitzer 168ef548c55SMike Snitzer } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) { 169ef548c55SMike Snitzer ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set"; 170ef548c55SMike Snitzer return -EINVAL; 171a3998799SMike Snitzer } 172a3998799SMike Snitzer 173a3998799SMike Snitzer return 0; 174dfd068b0SMike Snitzer } 175dfd068b0SMike Snitzer 1763407ef52SJosef Bacik /* 177dfd068b0SMike Snitzer * Construct a flakey mapping: 178dfd068b0SMike Snitzer * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*] 179b26f5e3dSMike Snitzer * 180b26f5e3dSMike Snitzer * Feature args: 181b26f5e3dSMike Snitzer * [drop_writes] 182a3998799SMike Snitzer * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>] 183a3998799SMike Snitzer * 184a3998799SMike Snitzer * Nth_byte starts from 1 for the first byte. 185a3998799SMike Snitzer * Direction is r for READ or w for WRITE. 186a3998799SMike Snitzer * bio_flags is ignored if 0. 1873407ef52SJosef Bacik */ 1883407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv) 1893407ef52SJosef Bacik { 1905916a22bSEric Biggers static const struct dm_arg _args[] = { 191dfd068b0SMike Snitzer {0, UINT_MAX, "Invalid up interval"}, 192dfd068b0SMike Snitzer {0, UINT_MAX, "Invalid down interval"}, 193dfd068b0SMike Snitzer }; 1943407ef52SJosef Bacik 195dfd068b0SMike Snitzer int r; 196dfd068b0SMike Snitzer struct flakey_c *fc; 197dfd068b0SMike Snitzer unsigned long long tmpll; 198dfd068b0SMike Snitzer struct dm_arg_set as; 199dfd068b0SMike Snitzer const char *devname; 20031998ef1SMikulas Patocka char dummy; 201dfd068b0SMike Snitzer 202dfd068b0SMike Snitzer as.argc = argc; 203dfd068b0SMike Snitzer as.argv = argv; 204dfd068b0SMike Snitzer 205dfd068b0SMike Snitzer if (argc < 4) { 206dfd068b0SMike Snitzer ti->error = "Invalid argument count"; 2073407ef52SJosef Bacik return -EINVAL; 2083407ef52SJosef Bacik } 2093407ef52SJosef Bacik 210b26f5e3dSMike Snitzer fc = kzalloc(sizeof(*fc), GFP_KERNEL); 2113407ef52SJosef Bacik if (!fc) { 21275e3a0f5SAlasdair G Kergon ti->error = "Cannot allocate context"; 2133407ef52SJosef Bacik return -ENOMEM; 2143407ef52SJosef Bacik } 2153407ef52SJosef Bacik fc->start_time = jiffies; 2163407ef52SJosef Bacik 217dfd068b0SMike Snitzer devname = dm_shift_arg(&as); 2183407ef52SJosef Bacik 219e80d1c80SVivek Goyal r = -EINVAL; 220ef87bfc2SMilan Broz if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) { 221dfd068b0SMike Snitzer ti->error = "Invalid device sector"; 2223407ef52SJosef Bacik goto bad; 2233407ef52SJosef Bacik } 224dfd068b0SMike Snitzer fc->start = tmpll; 2253407ef52SJosef Bacik 226dfd068b0SMike Snitzer r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error); 227dfd068b0SMike Snitzer if (r) 2283407ef52SJosef Bacik goto bad; 229dfd068b0SMike Snitzer 230dfd068b0SMike Snitzer r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error); 231dfd068b0SMike Snitzer if (r) 232dfd068b0SMike Snitzer goto bad; 2333407ef52SJosef Bacik 2343407ef52SJosef Bacik if (!(fc->up_interval + fc->down_interval)) { 235dfd068b0SMike Snitzer ti->error = "Total (up + down) interval is zero"; 236bff7e067SWei Yongjun r = -EINVAL; 2373407ef52SJosef Bacik goto bad; 2383407ef52SJosef Bacik } 2393407ef52SJosef Bacik 2403407ef52SJosef Bacik if (fc->up_interval + fc->down_interval < fc->up_interval) { 241dfd068b0SMike Snitzer ti->error = "Interval overflow"; 242bff7e067SWei Yongjun r = -EINVAL; 2433407ef52SJosef Bacik goto bad; 2443407ef52SJosef Bacik } 2453407ef52SJosef Bacik 246b26f5e3dSMike Snitzer r = parse_features(&as, fc, ti); 247dfd068b0SMike Snitzer if (r) 248dfd068b0SMike Snitzer goto bad; 249dfd068b0SMike Snitzer 250e80d1c80SVivek Goyal r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev); 251e80d1c80SVivek Goyal if (r) { 252dfd068b0SMike Snitzer ti->error = "Device lookup failed"; 2533407ef52SJosef Bacik goto bad; 2543407ef52SJosef Bacik } 2553407ef52SJosef Bacik 25655a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 25755a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 25830187e1dSMike Snitzer ti->per_io_data_size = sizeof(struct per_bio_data); 2593407ef52SJosef Bacik ti->private = fc; 2603407ef52SJosef Bacik return 0; 2613407ef52SJosef Bacik 2623407ef52SJosef Bacik bad: 2633407ef52SJosef Bacik kfree(fc); 264e80d1c80SVivek Goyal return r; 2653407ef52SJosef Bacik } 2663407ef52SJosef Bacik 2673407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti) 2683407ef52SJosef Bacik { 2693407ef52SJosef Bacik struct flakey_c *fc = ti->private; 2703407ef52SJosef Bacik 2713407ef52SJosef Bacik dm_put_device(ti, fc->dev); 2723407ef52SJosef Bacik kfree(fc); 2733407ef52SJosef Bacik } 2743407ef52SJosef Bacik 2753407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector) 2763407ef52SJosef Bacik { 2773407ef52SJosef Bacik struct flakey_c *fc = ti->private; 2783407ef52SJosef Bacik 27930e4171bSMike Snitzer return fc->start + dm_target_offset(ti, bi_sector); 2803407ef52SJosef Bacik } 2813407ef52SJosef Bacik 2823407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio) 2833407ef52SJosef Bacik { 2843407ef52SJosef Bacik struct flakey_c *fc = ti->private; 2853407ef52SJosef Bacik 28674d46992SChristoph Hellwig bio_set_dev(bio, fc->dev->bdev); 287e86f2b00SMike Snitzer bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector); 2883407ef52SJosef Bacik } 2893407ef52SJosef Bacik 290a3998799SMike Snitzer static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc) 291a3998799SMike Snitzer { 292a00f5276SSweet Tea unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1; 293a00f5276SSweet Tea 294a00f5276SSweet Tea struct bvec_iter iter; 295a00f5276SSweet Tea struct bio_vec bvec; 296a00f5276SSweet Tea 297a00f5276SSweet Tea if (!bio_has_data(bio)) 298a00f5276SSweet Tea return; 299a3998799SMike Snitzer 300a3998799SMike Snitzer /* 301a00f5276SSweet Tea * Overwrite the Nth byte of the bio's data, on whichever page 302a00f5276SSweet Tea * it falls. 303a3998799SMike Snitzer */ 304a00f5276SSweet Tea bio_for_each_segment(bvec, bio, iter) { 305a00f5276SSweet Tea if (bio_iter_len(bio, iter) > corrupt_bio_byte) { 306*f50714b5SMikulas Patocka char *segment; 307*f50714b5SMikulas Patocka struct page *page = bio_iter_page(bio, iter); 308*f50714b5SMikulas Patocka if (unlikely(page == ZERO_PAGE(0))) 309*f50714b5SMikulas Patocka break; 310*f50714b5SMikulas Patocka segment = (page_address(page) + bio_iter_offset(bio, iter)); 311a00f5276SSweet Tea segment[corrupt_bio_byte] = fc->corrupt_bio_value; 312a3998799SMike Snitzer DMDEBUG("Corrupting data bio=%p by writing %u to byte %u " 313a00f5276SSweet Tea "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n", 314a3998799SMike Snitzer bio, fc->corrupt_bio_value, fc->corrupt_bio_byte, 3151eff9d32SJens Axboe (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf, 316a00f5276SSweet Tea (unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size); 317a00f5276SSweet Tea break; 318a00f5276SSweet Tea } 319a00f5276SSweet Tea corrupt_bio_byte -= bio_iter_len(bio, iter); 320a3998799SMike Snitzer } 321a3998799SMike Snitzer } 322a3998799SMike Snitzer 3237de3ee57SMikulas Patocka static int flakey_map(struct dm_target *ti, struct bio *bio) 3243407ef52SJosef Bacik { 3253407ef52SJosef Bacik struct flakey_c *fc = ti->private; 3263407ef52SJosef Bacik unsigned elapsed; 327c7cfdf59SMikulas Patocka struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data)); 328c7cfdf59SMikulas Patocka pb->bio_submitted = false; 3293407ef52SJosef Bacik 3302e2d6f7eSAjay Joshi if (op_is_zone_mgmt(bio_op(bio))) 331124c4454SDamien Le Moal goto map_bio; 332124c4454SDamien Le Moal 3333407ef52SJosef Bacik /* Are we alive ? */ 3343407ef52SJosef Bacik elapsed = (jiffies - fc->start_time) / HZ; 335b26f5e3dSMike Snitzer if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) { 336a3998799SMike Snitzer /* 337a3998799SMike Snitzer * Flag this bio as submitted while down. 338a3998799SMike Snitzer */ 339c7cfdf59SMikulas Patocka pb->bio_submitted = true; 3403407ef52SJosef Bacik 341b26f5e3dSMike Snitzer /* 342ef548c55SMike Snitzer * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set. 343299f6230SMike Snitzer * Otherwise, flakey_end_io() will decide if the reads should be modified. 344a3998799SMike Snitzer */ 34599f3c90dSMike Snitzer if (bio_data_dir(bio) == READ) { 346ef548c55SMike Snitzer if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) && 347ef548c55SMike Snitzer !test_bit(ERROR_WRITES, &fc->flags)) 348846785e6SChristoph Hellwig return DM_MAPIO_KILL; 349299f6230SMike Snitzer goto map_bio; 35099f3c90dSMike Snitzer } 351a3998799SMike Snitzer 352a3998799SMike Snitzer /* 353ef548c55SMike Snitzer * Drop or error writes? 354b26f5e3dSMike Snitzer */ 355b26f5e3dSMike Snitzer if (test_bit(DROP_WRITES, &fc->flags)) { 3564246a0b6SChristoph Hellwig bio_endio(bio); 357b26f5e3dSMike Snitzer return DM_MAPIO_SUBMITTED; 358b26f5e3dSMike Snitzer } 359ef548c55SMike Snitzer else if (test_bit(ERROR_WRITES, &fc->flags)) { 360ef548c55SMike Snitzer bio_io_error(bio); 361ef548c55SMike Snitzer return DM_MAPIO_SUBMITTED; 362ef548c55SMike Snitzer } 363a3998799SMike Snitzer 364a3998799SMike Snitzer /* 365a3998799SMike Snitzer * Corrupt matching writes. 366a3998799SMike Snitzer */ 367a3998799SMike Snitzer if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) { 368a3998799SMike Snitzer if (all_corrupt_bio_flags_match(bio, fc)) 369a3998799SMike Snitzer corrupt_bio_data(bio, fc); 370b26f5e3dSMike Snitzer goto map_bio; 371b26f5e3dSMike Snitzer } 372b26f5e3dSMike Snitzer 373b26f5e3dSMike Snitzer /* 374a3998799SMike Snitzer * By default, error all I/O. 375b26f5e3dSMike Snitzer */ 376846785e6SChristoph Hellwig return DM_MAPIO_KILL; 377b26f5e3dSMike Snitzer } 378b26f5e3dSMike Snitzer 379b26f5e3dSMike Snitzer map_bio: 3803407ef52SJosef Bacik flakey_map_bio(ti, bio); 3813407ef52SJosef Bacik 3823407ef52SJosef Bacik return DM_MAPIO_REMAPPED; 3833407ef52SJosef Bacik } 3843407ef52SJosef Bacik 3854e4cbee9SChristoph Hellwig static int flakey_end_io(struct dm_target *ti, struct bio *bio, 3864e4cbee9SChristoph Hellwig blk_status_t *error) 387a3998799SMike Snitzer { 388a3998799SMike Snitzer struct flakey_c *fc = ti->private; 389c7cfdf59SMikulas Patocka struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data)); 390a3998799SMike Snitzer 3912e2d6f7eSAjay Joshi if (op_is_zone_mgmt(bio_op(bio))) 392124c4454SDamien Le Moal return DM_ENDIO_DONE; 393124c4454SDamien Le Moal 3941be56909SChristoph Hellwig if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) { 395299f6230SMike Snitzer if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) && 396299f6230SMike Snitzer all_corrupt_bio_flags_match(bio, fc)) { 397299f6230SMike Snitzer /* 398299f6230SMike Snitzer * Corrupt successful matching READs while in down state. 399299f6230SMike Snitzer */ 400a3998799SMike Snitzer corrupt_bio_data(bio, fc); 401299f6230SMike Snitzer 402ef548c55SMike Snitzer } else if (!test_bit(DROP_WRITES, &fc->flags) && 403ef548c55SMike Snitzer !test_bit(ERROR_WRITES, &fc->flags)) { 404299f6230SMike Snitzer /* 405299f6230SMike Snitzer * Error read during the down_interval if drop_writes 406ef548c55SMike Snitzer * and error_writes were not configured. 407299f6230SMike Snitzer */ 4084e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 40999f3c90dSMike Snitzer } 410299f6230SMike Snitzer } 411a3998799SMike Snitzer 4121be56909SChristoph Hellwig return DM_ENDIO_DONE; 413a3998799SMike Snitzer } 414a3998799SMike Snitzer 415fd7c092eSMikulas Patocka static void flakey_status(struct dm_target *ti, status_type_t type, 4161f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 4173407ef52SJosef Bacik { 418b26f5e3dSMike Snitzer unsigned sz = 0; 4193407ef52SJosef Bacik struct flakey_c *fc = ti->private; 420ef548c55SMike Snitzer unsigned drop_writes, error_writes; 4213407ef52SJosef Bacik 4223407ef52SJosef Bacik switch (type) { 4233407ef52SJosef Bacik case STATUSTYPE_INFO: 4243407ef52SJosef Bacik result[0] = '\0'; 4253407ef52SJosef Bacik break; 4263407ef52SJosef Bacik 4273407ef52SJosef Bacik case STATUSTYPE_TABLE: 428b26f5e3dSMike Snitzer DMEMIT("%s %llu %u %u ", fc->dev->name, 4293407ef52SJosef Bacik (unsigned long long)fc->start, fc->up_interval, 4303407ef52SJosef Bacik fc->down_interval); 431b26f5e3dSMike Snitzer 432b26f5e3dSMike Snitzer drop_writes = test_bit(DROP_WRITES, &fc->flags); 433ef548c55SMike Snitzer error_writes = test_bit(ERROR_WRITES, &fc->flags); 434ef548c55SMike Snitzer DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5); 435a3998799SMike Snitzer 436b26f5e3dSMike Snitzer if (drop_writes) 437b26f5e3dSMike Snitzer DMEMIT("drop_writes "); 438ef548c55SMike Snitzer else if (error_writes) 439ef548c55SMike Snitzer DMEMIT("error_writes "); 440a3998799SMike Snitzer 441a3998799SMike Snitzer if (fc->corrupt_bio_byte) 442a3998799SMike Snitzer DMEMIT("corrupt_bio_byte %u %c %u %u ", 443a3998799SMike Snitzer fc->corrupt_bio_byte, 444a3998799SMike Snitzer (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r', 445a3998799SMike Snitzer fc->corrupt_bio_value, fc->corrupt_bio_flags); 446a3998799SMike Snitzer 4473407ef52SJosef Bacik break; 4488ec45662STushar Sugandhi 4498ec45662STushar Sugandhi case STATUSTYPE_IMA: 4508ec45662STushar Sugandhi result[0] = '\0'; 4518ec45662STushar Sugandhi break; 4523407ef52SJosef Bacik } 4533407ef52SJosef Bacik } 4543407ef52SJosef Bacik 4555bd5e8d8SMike Snitzer static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev) 4563407ef52SJosef Bacik { 4573407ef52SJosef Bacik struct flakey_c *fc = ti->private; 458e56f81e0SChristoph Hellwig 459e56f81e0SChristoph Hellwig *bdev = fc->dev->bdev; 4603407ef52SJosef Bacik 461ec8013beSPaolo Bonzini /* 462ec8013beSPaolo Bonzini * Only pass ioctls through if the device sizes match exactly. 463ec8013beSPaolo Bonzini */ 4646dcbb52cSChristoph Hellwig if (fc->start || ti->len != bdev_nr_sectors((*bdev))) 465e56f81e0SChristoph Hellwig return 1; 466e56f81e0SChristoph Hellwig return 0; 4673407ef52SJosef Bacik } 4683407ef52SJosef Bacik 469e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED 470d4100351SChristoph Hellwig static int flakey_report_zones(struct dm_target *ti, 471d4100351SChristoph Hellwig struct dm_report_zones_args *args, unsigned int nr_zones) 472e76239a3SChristoph Hellwig { 473e76239a3SChristoph Hellwig struct flakey_c *fc = ti->private; 474e76239a3SChristoph Hellwig 475912e8875SDamien Le Moal return dm_report_zones(fc->dev->bdev, fc->start, 476912e8875SDamien Le Moal flakey_map_sector(ti, args->next_sector), 477912e8875SDamien Le Moal args, nr_zones); 478e76239a3SChristoph Hellwig } 479e3290b94SMike Snitzer #else 480e3290b94SMike Snitzer #define flakey_report_zones NULL 481e76239a3SChristoph Hellwig #endif 482e76239a3SChristoph Hellwig 4833407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data) 4843407ef52SJosef Bacik { 4853407ef52SJosef Bacik struct flakey_c *fc = ti->private; 4863407ef52SJosef Bacik 4873407ef52SJosef Bacik return fn(ti, fc->dev, fc->start, ti->len, data); 4883407ef52SJosef Bacik } 4893407ef52SJosef Bacik 4903407ef52SJosef Bacik static struct target_type flakey_target = { 4913407ef52SJosef Bacik .name = "flakey", 492124c4454SDamien Le Moal .version = {1, 5, 0}, 4933db564b4SSatya Tangirala .features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO, 494e76239a3SChristoph Hellwig .report_zones = flakey_report_zones, 4953407ef52SJosef Bacik .module = THIS_MODULE, 4963407ef52SJosef Bacik .ctr = flakey_ctr, 4973407ef52SJosef Bacik .dtr = flakey_dtr, 4983407ef52SJosef Bacik .map = flakey_map, 499a3998799SMike Snitzer .end_io = flakey_end_io, 5003407ef52SJosef Bacik .status = flakey_status, 501e56f81e0SChristoph Hellwig .prepare_ioctl = flakey_prepare_ioctl, 5023407ef52SJosef Bacik .iterate_devices = flakey_iterate_devices, 5033407ef52SJosef Bacik }; 5043407ef52SJosef Bacik 5053407ef52SJosef Bacik static int __init dm_flakey_init(void) 5063407ef52SJosef Bacik { 5073407ef52SJosef Bacik int r = dm_register_target(&flakey_target); 5083407ef52SJosef Bacik 5093407ef52SJosef Bacik if (r < 0) 5103407ef52SJosef Bacik DMERR("register failed %d", r); 5113407ef52SJosef Bacik 5123407ef52SJosef Bacik return r; 5133407ef52SJosef Bacik } 5143407ef52SJosef Bacik 5153407ef52SJosef Bacik static void __exit dm_flakey_exit(void) 5163407ef52SJosef Bacik { 5173407ef52SJosef Bacik dm_unregister_target(&flakey_target); 5183407ef52SJosef Bacik } 5193407ef52SJosef Bacik 5203407ef52SJosef Bacik /* Module hooks */ 5213407ef52SJosef Bacik module_init(dm_flakey_init); 5223407ef52SJosef Bacik module_exit(dm_flakey_exit); 5233407ef52SJosef Bacik 5243407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target"); 5253407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 5263407ef52SJosef Bacik MODULE_LICENSE("GPL"); 527