0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/device-mapper.h>
0009
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/blkdev.h>
0013 #include <linux/bio.h>
0014 #include <linux/slab.h>
0015
0016 #define DM_MSG_PREFIX "flakey"
0017
0018 #define all_corrupt_bio_flags_match(bio, fc) \
0019 (((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
0020
0021
0022
0023
0024
0025 struct flakey_c {
0026 struct dm_dev *dev;
0027 unsigned long start_time;
0028 sector_t start;
0029 unsigned up_interval;
0030 unsigned down_interval;
0031 unsigned long flags;
0032 unsigned corrupt_bio_byte;
0033 unsigned corrupt_bio_rw;
0034 unsigned corrupt_bio_value;
0035 blk_opf_t corrupt_bio_flags;
0036 };
0037
0038 enum feature_flag_bits {
0039 DROP_WRITES,
0040 ERROR_WRITES
0041 };
0042
0043 struct per_bio_data {
0044 bool bio_submitted;
0045 };
0046
0047 static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
0048 struct dm_target *ti)
0049 {
0050 int r;
0051 unsigned argc;
0052 const char *arg_name;
0053
0054 static const struct dm_arg _args[] = {
0055 {0, 6, "Invalid number of feature args"},
0056 {1, UINT_MAX, "Invalid corrupt bio byte"},
0057 {0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
0058 {0, UINT_MAX, "Invalid corrupt bio flags mask"},
0059 };
0060
0061
0062 if (!as->argc)
0063 return 0;
0064
0065 r = dm_read_arg_group(_args, as, &argc, &ti->error);
0066 if (r)
0067 return r;
0068
0069 while (argc) {
0070 arg_name = dm_shift_arg(as);
0071 argc--;
0072
0073 if (!arg_name) {
0074 ti->error = "Insufficient feature arguments";
0075 return -EINVAL;
0076 }
0077
0078
0079
0080
0081 if (!strcasecmp(arg_name, "drop_writes")) {
0082 if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
0083 ti->error = "Feature drop_writes duplicated";
0084 return -EINVAL;
0085 } else if (test_bit(ERROR_WRITES, &fc->flags)) {
0086 ti->error = "Feature drop_writes conflicts with feature error_writes";
0087 return -EINVAL;
0088 }
0089
0090 continue;
0091 }
0092
0093
0094
0095
0096 if (!strcasecmp(arg_name, "error_writes")) {
0097 if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
0098 ti->error = "Feature error_writes duplicated";
0099 return -EINVAL;
0100
0101 } else if (test_bit(DROP_WRITES, &fc->flags)) {
0102 ti->error = "Feature error_writes conflicts with feature drop_writes";
0103 return -EINVAL;
0104 }
0105
0106 continue;
0107 }
0108
0109
0110
0111
0112 if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
0113 if (!argc) {
0114 ti->error = "Feature corrupt_bio_byte requires parameters";
0115 return -EINVAL;
0116 }
0117
0118 r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
0119 if (r)
0120 return r;
0121 argc--;
0122
0123
0124
0125
0126 arg_name = dm_shift_arg(as);
0127 if (!strcasecmp(arg_name, "w"))
0128 fc->corrupt_bio_rw = WRITE;
0129 else if (!strcasecmp(arg_name, "r"))
0130 fc->corrupt_bio_rw = READ;
0131 else {
0132 ti->error = "Invalid corrupt bio direction (r or w)";
0133 return -EINVAL;
0134 }
0135 argc--;
0136
0137
0138
0139
0140 r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
0141 if (r)
0142 return r;
0143 argc--;
0144
0145
0146
0147
0148 BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
0149 sizeof(unsigned int));
0150 r = dm_read_arg(_args + 3, as,
0151 (__force unsigned *)&fc->corrupt_bio_flags,
0152 &ti->error);
0153 if (r)
0154 return r;
0155 argc--;
0156
0157 continue;
0158 }
0159
0160 ti->error = "Unrecognised flakey feature requested";
0161 return -EINVAL;
0162 }
0163
0164 if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
0165 ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
0166 return -EINVAL;
0167
0168 } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
0169 ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
0170 return -EINVAL;
0171 }
0172
0173 return 0;
0174 }
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
0189 {
0190 static const struct dm_arg _args[] = {
0191 {0, UINT_MAX, "Invalid up interval"},
0192 {0, UINT_MAX, "Invalid down interval"},
0193 };
0194
0195 int r;
0196 struct flakey_c *fc;
0197 unsigned long long tmpll;
0198 struct dm_arg_set as;
0199 const char *devname;
0200 char dummy;
0201
0202 as.argc = argc;
0203 as.argv = argv;
0204
0205 if (argc < 4) {
0206 ti->error = "Invalid argument count";
0207 return -EINVAL;
0208 }
0209
0210 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
0211 if (!fc) {
0212 ti->error = "Cannot allocate context";
0213 return -ENOMEM;
0214 }
0215 fc->start_time = jiffies;
0216
0217 devname = dm_shift_arg(&as);
0218
0219 r = -EINVAL;
0220 if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
0221 ti->error = "Invalid device sector";
0222 goto bad;
0223 }
0224 fc->start = tmpll;
0225
0226 r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
0227 if (r)
0228 goto bad;
0229
0230 r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
0231 if (r)
0232 goto bad;
0233
0234 if (!(fc->up_interval + fc->down_interval)) {
0235 ti->error = "Total (up + down) interval is zero";
0236 r = -EINVAL;
0237 goto bad;
0238 }
0239
0240 if (fc->up_interval + fc->down_interval < fc->up_interval) {
0241 ti->error = "Interval overflow";
0242 r = -EINVAL;
0243 goto bad;
0244 }
0245
0246 r = parse_features(&as, fc, ti);
0247 if (r)
0248 goto bad;
0249
0250 r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
0251 if (r) {
0252 ti->error = "Device lookup failed";
0253 goto bad;
0254 }
0255
0256 ti->num_flush_bios = 1;
0257 ti->num_discard_bios = 1;
0258 ti->per_io_data_size = sizeof(struct per_bio_data);
0259 ti->private = fc;
0260 return 0;
0261
0262 bad:
0263 kfree(fc);
0264 return r;
0265 }
0266
0267 static void flakey_dtr(struct dm_target *ti)
0268 {
0269 struct flakey_c *fc = ti->private;
0270
0271 dm_put_device(ti, fc->dev);
0272 kfree(fc);
0273 }
0274
0275 static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
0276 {
0277 struct flakey_c *fc = ti->private;
0278
0279 return fc->start + dm_target_offset(ti, bi_sector);
0280 }
0281
0282 static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
0283 {
0284 struct flakey_c *fc = ti->private;
0285
0286 bio_set_dev(bio, fc->dev->bdev);
0287 bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
0288 }
0289
0290 static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
0291 {
0292 unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
0293
0294 struct bvec_iter iter;
0295 struct bio_vec bvec;
0296
0297 if (!bio_has_data(bio))
0298 return;
0299
0300
0301
0302
0303
0304 bio_for_each_segment(bvec, bio, iter) {
0305 if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
0306 char *segment = (page_address(bio_iter_page(bio, iter))
0307 + bio_iter_offset(bio, iter));
0308 segment[corrupt_bio_byte] = fc->corrupt_bio_value;
0309 DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
0310 "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
0311 bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
0312 (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
0313 (unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
0314 break;
0315 }
0316 corrupt_bio_byte -= bio_iter_len(bio, iter);
0317 }
0318 }
0319
0320 static int flakey_map(struct dm_target *ti, struct bio *bio)
0321 {
0322 struct flakey_c *fc = ti->private;
0323 unsigned elapsed;
0324 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
0325 pb->bio_submitted = false;
0326
0327 if (op_is_zone_mgmt(bio_op(bio)))
0328 goto map_bio;
0329
0330
0331 elapsed = (jiffies - fc->start_time) / HZ;
0332 if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
0333
0334
0335
0336 pb->bio_submitted = true;
0337
0338
0339
0340
0341
0342 if (bio_data_dir(bio) == READ) {
0343 if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
0344 !test_bit(ERROR_WRITES, &fc->flags))
0345 return DM_MAPIO_KILL;
0346 goto map_bio;
0347 }
0348
0349
0350
0351
0352 if (test_bit(DROP_WRITES, &fc->flags)) {
0353 bio_endio(bio);
0354 return DM_MAPIO_SUBMITTED;
0355 }
0356 else if (test_bit(ERROR_WRITES, &fc->flags)) {
0357 bio_io_error(bio);
0358 return DM_MAPIO_SUBMITTED;
0359 }
0360
0361
0362
0363
0364 if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
0365 if (all_corrupt_bio_flags_match(bio, fc))
0366 corrupt_bio_data(bio, fc);
0367 goto map_bio;
0368 }
0369
0370
0371
0372
0373 return DM_MAPIO_KILL;
0374 }
0375
0376 map_bio:
0377 flakey_map_bio(ti, bio);
0378
0379 return DM_MAPIO_REMAPPED;
0380 }
0381
0382 static int flakey_end_io(struct dm_target *ti, struct bio *bio,
0383 blk_status_t *error)
0384 {
0385 struct flakey_c *fc = ti->private;
0386 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
0387
0388 if (op_is_zone_mgmt(bio_op(bio)))
0389 return DM_ENDIO_DONE;
0390
0391 if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
0392 if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
0393 all_corrupt_bio_flags_match(bio, fc)) {
0394
0395
0396
0397 corrupt_bio_data(bio, fc);
0398
0399 } else if (!test_bit(DROP_WRITES, &fc->flags) &&
0400 !test_bit(ERROR_WRITES, &fc->flags)) {
0401
0402
0403
0404
0405 *error = BLK_STS_IOERR;
0406 }
0407 }
0408
0409 return DM_ENDIO_DONE;
0410 }
0411
0412 static void flakey_status(struct dm_target *ti, status_type_t type,
0413 unsigned status_flags, char *result, unsigned maxlen)
0414 {
0415 unsigned sz = 0;
0416 struct flakey_c *fc = ti->private;
0417 unsigned drop_writes, error_writes;
0418
0419 switch (type) {
0420 case STATUSTYPE_INFO:
0421 result[0] = '\0';
0422 break;
0423
0424 case STATUSTYPE_TABLE:
0425 DMEMIT("%s %llu %u %u ", fc->dev->name,
0426 (unsigned long long)fc->start, fc->up_interval,
0427 fc->down_interval);
0428
0429 drop_writes = test_bit(DROP_WRITES, &fc->flags);
0430 error_writes = test_bit(ERROR_WRITES, &fc->flags);
0431 DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
0432
0433 if (drop_writes)
0434 DMEMIT("drop_writes ");
0435 else if (error_writes)
0436 DMEMIT("error_writes ");
0437
0438 if (fc->corrupt_bio_byte)
0439 DMEMIT("corrupt_bio_byte %u %c %u %u ",
0440 fc->corrupt_bio_byte,
0441 (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
0442 fc->corrupt_bio_value, fc->corrupt_bio_flags);
0443
0444 break;
0445
0446 case STATUSTYPE_IMA:
0447 result[0] = '\0';
0448 break;
0449 }
0450 }
0451
0452 static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
0453 {
0454 struct flakey_c *fc = ti->private;
0455
0456 *bdev = fc->dev->bdev;
0457
0458
0459
0460
0461 if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
0462 return 1;
0463 return 0;
0464 }
0465
0466 #ifdef CONFIG_BLK_DEV_ZONED
0467 static int flakey_report_zones(struct dm_target *ti,
0468 struct dm_report_zones_args *args, unsigned int nr_zones)
0469 {
0470 struct flakey_c *fc = ti->private;
0471
0472 return dm_report_zones(fc->dev->bdev, fc->start,
0473 flakey_map_sector(ti, args->next_sector),
0474 args, nr_zones);
0475 }
0476 #else
0477 #define flakey_report_zones NULL
0478 #endif
0479
0480 static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
0481 {
0482 struct flakey_c *fc = ti->private;
0483
0484 return fn(ti, fc->dev, fc->start, ti->len, data);
0485 }
0486
0487 static struct target_type flakey_target = {
0488 .name = "flakey",
0489 .version = {1, 5, 0},
0490 .features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
0491 .report_zones = flakey_report_zones,
0492 .module = THIS_MODULE,
0493 .ctr = flakey_ctr,
0494 .dtr = flakey_dtr,
0495 .map = flakey_map,
0496 .end_io = flakey_end_io,
0497 .status = flakey_status,
0498 .prepare_ioctl = flakey_prepare_ioctl,
0499 .iterate_devices = flakey_iterate_devices,
0500 };
0501
0502 static int __init dm_flakey_init(void)
0503 {
0504 int r = dm_register_target(&flakey_target);
0505
0506 if (r < 0)
0507 DMERR("register failed %d", r);
0508
0509 return r;
0510 }
0511
0512 static void __exit dm_flakey_exit(void)
0513 {
0514 dm_unregister_target(&flakey_target);
0515 }
0516
0517
0518 module_init(dm_flakey_init);
0519 module_exit(dm_flakey_exit);
0520
0521 MODULE_DESCRIPTION(DM_NAME " flakey target");
0522 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
0523 MODULE_LICENSE("GPL");