1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2003 Sistina Software (UK) Limited.
4 * Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
6 * This file is released under the GPL.
9 #include <linux/device-mapper.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/blkdev.h>
14 #include <linux/bio.h>
15 #include <linux/slab.h>
17 #define DM_MSG_PREFIX "flakey"
19 #define all_corrupt_bio_flags_match(bio, fc) \
20 (((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
23 * Flakey: Used for testing only, simulates intermittent,
24 * catastrophic device failure.
28 unsigned long start_time;
30 unsigned int up_interval;
31 unsigned int down_interval;
33 unsigned int corrupt_bio_byte;
34 unsigned int corrupt_bio_rw;
35 unsigned int corrupt_bio_value;
36 blk_opf_t corrupt_bio_flags;
39 enum feature_flag_bits {
48 static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
55 static const struct dm_arg _args[] = {
56 {0, 6, "Invalid number of feature args"},
57 {1, UINT_MAX, "Invalid corrupt bio byte"},
58 {0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
59 {0, UINT_MAX, "Invalid corrupt bio flags mask"},
62 /* No feature arguments supplied. */
66 r = dm_read_arg_group(_args, as, &argc, &ti->error);
71 arg_name = dm_shift_arg(as);
75 ti->error = "Insufficient feature arguments";
82 if (!strcasecmp(arg_name, "drop_writes")) {
83 if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
84 ti->error = "Feature drop_writes duplicated";
86 } else if (test_bit(ERROR_WRITES, &fc->flags)) {
87 ti->error = "Feature drop_writes conflicts with feature error_writes";
97 if (!strcasecmp(arg_name, "error_writes")) {
98 if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
99 ti->error = "Feature error_writes duplicated";
102 } else if (test_bit(DROP_WRITES, &fc->flags)) {
103 ti->error = "Feature error_writes conflicts with feature drop_writes";
111 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
113 if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
115 ti->error = "Feature corrupt_bio_byte requires parameters";
119 r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
127 arg_name = dm_shift_arg(as);
128 if (!strcasecmp(arg_name, "w"))
129 fc->corrupt_bio_rw = WRITE;
130 else if (!strcasecmp(arg_name, "r"))
131 fc->corrupt_bio_rw = READ;
133 ti->error = "Invalid corrupt bio direction (r or w)";
139 * Value of byte (0-255) to write in place of correct one.
141 r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
147 * Only corrupt bios with these flags set.
149 BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
150 sizeof(unsigned int));
151 r = dm_read_arg(_args + 3, as,
152 (__force unsigned int *)&fc->corrupt_bio_flags,
161 ti->error = "Unrecognised flakey feature requested";
165 if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
166 ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
169 } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
170 ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
178 * Construct a flakey mapping:
179 * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
183 * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
185 * Nth_byte starts from 1 for the first byte.
186 * Direction is r for READ or w for WRITE.
187 * bio_flags is ignored if 0.
189 static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
191 static const struct dm_arg _args[] = {
192 {0, UINT_MAX, "Invalid up interval"},
193 {0, UINT_MAX, "Invalid down interval"},
198 unsigned long long tmpll;
199 struct dm_arg_set as;
207 ti->error = "Invalid argument count";
211 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
213 ti->error = "Cannot allocate context";
216 fc->start_time = jiffies;
218 devname = dm_shift_arg(&as);
221 if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
222 ti->error = "Invalid device sector";
227 r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
231 r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
235 if (!(fc->up_interval + fc->down_interval)) {
236 ti->error = "Total (up + down) interval is zero";
241 if (fc->up_interval + fc->down_interval < fc->up_interval) {
242 ti->error = "Interval overflow";
247 r = parse_features(&as, fc, ti);
251 r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
253 ti->error = "Device lookup failed";
257 ti->num_flush_bios = 1;
258 ti->num_discard_bios = 1;
259 ti->per_io_data_size = sizeof(struct per_bio_data);
268 static void flakey_dtr(struct dm_target *ti)
270 struct flakey_c *fc = ti->private;
272 dm_put_device(ti, fc->dev);
276 static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
278 struct flakey_c *fc = ti->private;
280 return fc->start + dm_target_offset(ti, bi_sector);
283 static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
285 struct flakey_c *fc = ti->private;
287 bio_set_dev(bio, fc->dev->bdev);
288 bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
291 static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
293 unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
295 struct bvec_iter iter;
298 if (!bio_has_data(bio))
302 * Overwrite the Nth byte of the bio's data, on whichever page
305 bio_for_each_segment(bvec, bio, iter) {
306 if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
308 struct page *page = bio_iter_page(bio, iter);
309 if (unlikely(page == ZERO_PAGE(0)))
311 segment = bvec_kmap_local(&bvec);
312 segment[corrupt_bio_byte] = fc->corrupt_bio_value;
313 kunmap_local(segment);
314 DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
315 "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
316 bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
317 (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
318 (unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
321 corrupt_bio_byte -= bio_iter_len(bio, iter);
325 static int flakey_map(struct dm_target *ti, struct bio *bio)
327 struct flakey_c *fc = ti->private;
328 unsigned int elapsed;
329 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
331 pb->bio_submitted = false;
333 if (op_is_zone_mgmt(bio_op(bio)))
337 elapsed = (jiffies - fc->start_time) / HZ;
338 if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
340 * Flag this bio as submitted while down.
342 pb->bio_submitted = true;
345 * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
346 * Otherwise, flakey_end_io() will decide if the reads should be modified.
348 if (bio_data_dir(bio) == READ) {
349 if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
350 !test_bit(ERROR_WRITES, &fc->flags))
351 return DM_MAPIO_KILL;
356 * Drop or error writes?
358 if (test_bit(DROP_WRITES, &fc->flags)) {
360 return DM_MAPIO_SUBMITTED;
361 } else if (test_bit(ERROR_WRITES, &fc->flags)) {
363 return DM_MAPIO_SUBMITTED;
367 * Corrupt matching writes.
369 if (fc->corrupt_bio_byte) {
370 if (fc->corrupt_bio_rw == WRITE) {
371 if (all_corrupt_bio_flags_match(bio, fc))
372 corrupt_bio_data(bio, fc);
378 * By default, error all I/O.
380 return DM_MAPIO_KILL;
384 flakey_map_bio(ti, bio);
386 return DM_MAPIO_REMAPPED;
389 static int flakey_end_io(struct dm_target *ti, struct bio *bio,
392 struct flakey_c *fc = ti->private;
393 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
395 if (op_is_zone_mgmt(bio_op(bio)))
396 return DM_ENDIO_DONE;
398 if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
399 if (fc->corrupt_bio_byte) {
400 if ((fc->corrupt_bio_rw == READ) &&
401 all_corrupt_bio_flags_match(bio, fc)) {
403 * Corrupt successful matching READs while in down state.
405 corrupt_bio_data(bio, fc);
407 } else if (!test_bit(DROP_WRITES, &fc->flags) &&
408 !test_bit(ERROR_WRITES, &fc->flags)) {
410 * Error read during the down_interval if drop_writes
411 * and error_writes were not configured.
413 *error = BLK_STS_IOERR;
417 return DM_ENDIO_DONE;
420 static void flakey_status(struct dm_target *ti, status_type_t type,
421 unsigned int status_flags, char *result, unsigned int maxlen)
424 struct flakey_c *fc = ti->private;
425 unsigned int drop_writes, error_writes;
428 case STATUSTYPE_INFO:
432 case STATUSTYPE_TABLE:
433 DMEMIT("%s %llu %u %u ", fc->dev->name,
434 (unsigned long long)fc->start, fc->up_interval,
437 drop_writes = test_bit(DROP_WRITES, &fc->flags);
438 error_writes = test_bit(ERROR_WRITES, &fc->flags);
439 DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
442 DMEMIT("drop_writes ");
443 else if (error_writes)
444 DMEMIT("error_writes ");
446 if (fc->corrupt_bio_byte)
447 DMEMIT("corrupt_bio_byte %u %c %u %u ",
448 fc->corrupt_bio_byte,
449 (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
450 fc->corrupt_bio_value, fc->corrupt_bio_flags);
460 static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
462 struct flakey_c *fc = ti->private;
464 *bdev = fc->dev->bdev;
467 * Only pass ioctls through if the device sizes match exactly.
469 if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
474 #ifdef CONFIG_BLK_DEV_ZONED
475 static int flakey_report_zones(struct dm_target *ti,
476 struct dm_report_zones_args *args, unsigned int nr_zones)
478 struct flakey_c *fc = ti->private;
480 return dm_report_zones(fc->dev->bdev, fc->start,
481 flakey_map_sector(ti, args->next_sector),
485 #define flakey_report_zones NULL
488 static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
490 struct flakey_c *fc = ti->private;
492 return fn(ti, fc->dev, fc->start, ti->len, data);
495 static struct target_type flakey_target = {
497 .version = {1, 5, 0},
498 .features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
499 .report_zones = flakey_report_zones,
500 .module = THIS_MODULE,
504 .end_io = flakey_end_io,
505 .status = flakey_status,
506 .prepare_ioctl = flakey_prepare_ioctl,
507 .iterate_devices = flakey_iterate_devices,
510 static int __init dm_flakey_init(void)
512 int r = dm_register_target(&flakey_target);
515 DMERR("register failed %d", r);
520 static void __exit dm_flakey_exit(void)
522 dm_unregister_target(&flakey_target);
526 module_init(dm_flakey_init);
527 module_exit(dm_flakey_exit);
529 MODULE_DESCRIPTION(DM_NAME " flakey target");
530 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
531 MODULE_LICENSE("GPL");