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 {
49 static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
56 static const struct dm_arg _args[] = {
57 {0, 7, "Invalid number of feature args"},
58 {1, UINT_MAX, "Invalid corrupt bio byte"},
59 {0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
60 {0, UINT_MAX, "Invalid corrupt bio flags mask"},
63 /* No feature arguments supplied. */
67 r = dm_read_arg_group(_args, as, &argc, &ti->error);
72 arg_name = dm_shift_arg(as);
76 ti->error = "Insufficient feature arguments";
83 if (!strcasecmp(arg_name, "error_reads")) {
84 if (test_and_set_bit(ERROR_READS, &fc->flags)) {
85 ti->error = "Feature error_reads duplicated";
94 if (!strcasecmp(arg_name, "drop_writes")) {
95 if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
96 ti->error = "Feature drop_writes duplicated";
98 } else if (test_bit(ERROR_WRITES, &fc->flags)) {
99 ti->error = "Feature drop_writes conflicts with feature error_writes";
109 if (!strcasecmp(arg_name, "error_writes")) {
110 if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
111 ti->error = "Feature error_writes duplicated";
114 } else if (test_bit(DROP_WRITES, &fc->flags)) {
115 ti->error = "Feature error_writes conflicts with feature drop_writes";
123 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
125 if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
127 ti->error = "Feature corrupt_bio_byte requires parameters";
131 r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
139 arg_name = dm_shift_arg(as);
140 if (arg_name && !strcasecmp(arg_name, "w"))
141 fc->corrupt_bio_rw = WRITE;
142 else if (arg_name && !strcasecmp(arg_name, "r"))
143 fc->corrupt_bio_rw = READ;
145 ti->error = "Invalid corrupt bio direction (r or w)";
151 * Value of byte (0-255) to write in place of correct one.
153 r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
159 * Only corrupt bios with these flags set.
161 BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
162 sizeof(unsigned int));
163 r = dm_read_arg(_args + 3, as,
164 (__force unsigned int *)&fc->corrupt_bio_flags,
173 ti->error = "Unrecognised flakey feature requested";
177 if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
178 ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
181 } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
182 ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
186 if (!fc->corrupt_bio_byte && !test_bit(ERROR_READS, &fc->flags) &&
187 !test_bit(DROP_WRITES, &fc->flags) && !test_bit(ERROR_WRITES, &fc->flags)) {
188 set_bit(ERROR_WRITES, &fc->flags);
189 set_bit(ERROR_READS, &fc->flags);
196 * Construct a flakey mapping:
197 * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
201 * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
203 * Nth_byte starts from 1 for the first byte.
204 * Direction is r for READ or w for WRITE.
205 * bio_flags is ignored if 0.
207 static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
209 static const struct dm_arg _args[] = {
210 {0, UINT_MAX, "Invalid up interval"},
211 {0, UINT_MAX, "Invalid down interval"},
216 unsigned long long tmpll;
217 struct dm_arg_set as;
225 ti->error = "Invalid argument count";
229 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
231 ti->error = "Cannot allocate context";
234 fc->start_time = jiffies;
236 devname = dm_shift_arg(&as);
239 if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
240 ti->error = "Invalid device sector";
245 r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
249 r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
253 if (!(fc->up_interval + fc->down_interval)) {
254 ti->error = "Total (up + down) interval is zero";
259 if (fc->up_interval + fc->down_interval < fc->up_interval) {
260 ti->error = "Interval overflow";
265 r = parse_features(&as, fc, ti);
269 r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
271 ti->error = "Device lookup failed";
275 ti->num_flush_bios = 1;
276 ti->num_discard_bios = 1;
277 ti->per_io_data_size = sizeof(struct per_bio_data);
286 static void flakey_dtr(struct dm_target *ti)
288 struct flakey_c *fc = ti->private;
290 dm_put_device(ti, fc->dev);
294 static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
296 struct flakey_c *fc = ti->private;
298 return fc->start + dm_target_offset(ti, bi_sector);
301 static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
303 struct flakey_c *fc = ti->private;
305 bio_set_dev(bio, fc->dev->bdev);
306 bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
309 static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
311 unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
313 struct bvec_iter iter;
316 if (!bio_has_data(bio))
320 * Overwrite the Nth byte of the bio's data, on whichever page
323 bio_for_each_segment(bvec, bio, iter) {
324 if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
326 struct page *page = bio_iter_page(bio, iter);
327 if (unlikely(page == ZERO_PAGE(0)))
329 segment = bvec_kmap_local(&bvec);
330 segment[corrupt_bio_byte] = fc->corrupt_bio_value;
331 kunmap_local(segment);
332 DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
333 "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
334 bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
335 (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
336 (unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
339 corrupt_bio_byte -= bio_iter_len(bio, iter);
343 static int flakey_map(struct dm_target *ti, struct bio *bio)
345 struct flakey_c *fc = ti->private;
346 unsigned int elapsed;
347 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
349 pb->bio_submitted = false;
351 if (op_is_zone_mgmt(bio_op(bio)))
355 elapsed = (jiffies - fc->start_time) / HZ;
356 if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
358 * Flag this bio as submitted while down.
360 pb->bio_submitted = true;
363 * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
364 * Otherwise, flakey_end_io() will decide if the reads should be modified.
366 if (bio_data_dir(bio) == READ) {
367 if (test_bit(ERROR_READS, &fc->flags))
368 return DM_MAPIO_KILL;
373 * Drop or error writes?
375 if (test_bit(DROP_WRITES, &fc->flags)) {
377 return DM_MAPIO_SUBMITTED;
378 } else if (test_bit(ERROR_WRITES, &fc->flags)) {
380 return DM_MAPIO_SUBMITTED;
384 * Corrupt matching writes.
386 if (fc->corrupt_bio_byte) {
387 if (fc->corrupt_bio_rw == WRITE) {
388 if (all_corrupt_bio_flags_match(bio, fc))
389 corrupt_bio_data(bio, fc);
396 flakey_map_bio(ti, bio);
398 return DM_MAPIO_REMAPPED;
401 static int flakey_end_io(struct dm_target *ti, struct bio *bio,
404 struct flakey_c *fc = ti->private;
405 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
407 if (op_is_zone_mgmt(bio_op(bio)))
408 return DM_ENDIO_DONE;
410 if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
411 if (fc->corrupt_bio_byte) {
412 if ((fc->corrupt_bio_rw == READ) &&
413 all_corrupt_bio_flags_match(bio, fc)) {
415 * Corrupt successful matching READs while in down state.
417 corrupt_bio_data(bio, fc);
420 if (test_bit(ERROR_READS, &fc->flags)) {
422 * Error read during the down_interval if drop_writes
423 * and error_writes were not configured.
425 *error = BLK_STS_IOERR;
429 return DM_ENDIO_DONE;
432 static void flakey_status(struct dm_target *ti, status_type_t type,
433 unsigned int status_flags, char *result, unsigned int maxlen)
436 struct flakey_c *fc = ti->private;
437 unsigned int error_reads, drop_writes, error_writes;
440 case STATUSTYPE_INFO:
444 case STATUSTYPE_TABLE:
445 DMEMIT("%s %llu %u %u", fc->dev->name,
446 (unsigned long long)fc->start, fc->up_interval,
449 error_reads = test_bit(ERROR_READS, &fc->flags);
450 drop_writes = test_bit(DROP_WRITES, &fc->flags);
451 error_writes = test_bit(ERROR_WRITES, &fc->flags);
452 DMEMIT(" %u", error_reads + drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
455 DMEMIT(" error_reads");
457 DMEMIT(" drop_writes");
458 else if (error_writes)
459 DMEMIT(" error_writes");
461 if (fc->corrupt_bio_byte)
462 DMEMIT(" corrupt_bio_byte %u %c %u %u",
463 fc->corrupt_bio_byte,
464 (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
465 fc->corrupt_bio_value, fc->corrupt_bio_flags);
475 static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
477 struct flakey_c *fc = ti->private;
479 *bdev = fc->dev->bdev;
482 * Only pass ioctls through if the device sizes match exactly.
484 if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
489 #ifdef CONFIG_BLK_DEV_ZONED
490 static int flakey_report_zones(struct dm_target *ti,
491 struct dm_report_zones_args *args, unsigned int nr_zones)
493 struct flakey_c *fc = ti->private;
495 return dm_report_zones(fc->dev->bdev, fc->start,
496 flakey_map_sector(ti, args->next_sector),
500 #define flakey_report_zones NULL
503 static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
505 struct flakey_c *fc = ti->private;
507 return fn(ti, fc->dev, fc->start, ti->len, data);
510 static struct target_type flakey_target = {
512 .version = {1, 5, 0},
513 .features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
514 .report_zones = flakey_report_zones,
515 .module = THIS_MODULE,
519 .end_io = flakey_end_io,
520 .status = flakey_status,
521 .prepare_ioctl = flakey_prepare_ioctl,
522 .iterate_devices = flakey_iterate_devices,
526 MODULE_DESCRIPTION(DM_NAME " flakey target");
528 MODULE_LICENSE("GPL");