2023-01-25 20:00:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-03-24 13:54:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003 Sistina Software (UK) Limited.
|
2011-08-02 11:32:06 +00:00
|
|
|
* Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
|
2011-03-24 13:54:24 +00:00
|
|
|
*
|
|
|
|
* This file is released under the GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/device-mapper.h>
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#define DM_MSG_PREFIX "flakey"
|
|
|
|
|
2023-05-01 13:20:08 +00:00
|
|
|
#define PROBABILITY_BASE 1000000000
|
|
|
|
|
2011-08-02 11:32:06 +00:00
|
|
|
#define all_corrupt_bio_flags_match(bio, fc) \
|
2016-08-05 21:35:16 +00:00
|
|
|
(((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
|
2011-08-02 11:32:06 +00:00
|
|
|
|
2011-03-24 13:54:24 +00:00
|
|
|
/*
|
|
|
|
* Flakey: Used for testing only, simulates intermittent,
|
|
|
|
* catastrophic device failure.
|
|
|
|
*/
|
|
|
|
struct flakey_c {
|
|
|
|
struct dm_dev *dev;
|
|
|
|
unsigned long start_time;
|
|
|
|
sector_t start;
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int up_interval;
|
|
|
|
unsigned int down_interval;
|
2011-08-02 11:32:05 +00:00
|
|
|
unsigned long flags;
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int corrupt_bio_byte;
|
|
|
|
unsigned int corrupt_bio_rw;
|
|
|
|
unsigned int corrupt_bio_value;
|
2022-07-14 18:06:51 +00:00
|
|
|
blk_opf_t corrupt_bio_flags;
|
2023-05-01 13:20:08 +00:00
|
|
|
unsigned int random_read_corrupt;
|
|
|
|
unsigned int random_write_corrupt;
|
2011-03-24 13:54:24 +00:00
|
|
|
};
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
enum feature_flag_bits {
|
2023-04-18 19:56:41 +00:00
|
|
|
ERROR_READS,
|
2016-12-13 19:54:50 +00:00
|
|
|
DROP_WRITES,
|
|
|
|
ERROR_WRITES
|
2011-08-02 11:32:05 +00:00
|
|
|
};
|
|
|
|
|
2012-12-21 20:23:39 +00:00
|
|
|
struct per_bio_data {
|
|
|
|
bool bio_submitted;
|
|
|
|
};
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
|
|
|
|
struct dm_target *ti)
|
2011-08-02 11:32:05 +00:00
|
|
|
{
|
|
|
|
int r;
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int argc;
|
2011-08-02 11:32:05 +00:00
|
|
|
const char *arg_name;
|
|
|
|
|
2017-06-22 18:32:45 +00:00
|
|
|
static const struct dm_arg _args[] = {
|
2023-05-01 13:20:08 +00:00
|
|
|
{0, 11, "Invalid number of feature args"},
|
2011-08-02 11:32:06 +00:00
|
|
|
{1, UINT_MAX, "Invalid corrupt bio byte"},
|
|
|
|
{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
|
|
|
|
{0, UINT_MAX, "Invalid corrupt bio flags mask"},
|
2023-05-01 13:20:08 +00:00
|
|
|
{0, PROBABILITY_BASE, "Invalid random corrupt argument"},
|
2011-08-02 11:32:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* No feature arguments supplied. */
|
|
|
|
if (!as->argc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = dm_read_arg_group(_args, as, &argc, &ti->error);
|
|
|
|
if (r)
|
2011-08-02 11:32:06 +00:00
|
|
|
return r;
|
2011-08-02 11:32:05 +00:00
|
|
|
|
2011-08-02 11:32:06 +00:00
|
|
|
while (argc) {
|
2011-08-02 11:32:05 +00:00
|
|
|
arg_name = dm_shift_arg(as);
|
|
|
|
argc--;
|
|
|
|
|
2017-12-04 03:14:12 +00:00
|
|
|
if (!arg_name) {
|
|
|
|
ti->error = "Insufficient feature arguments";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:56:41 +00:00
|
|
|
/*
|
|
|
|
* error_reads
|
|
|
|
*/
|
|
|
|
if (!strcasecmp(arg_name, "error_reads")) {
|
|
|
|
if (test_and_set_bit(ERROR_READS, &fc->flags)) {
|
|
|
|
ti->error = "Feature error_reads duplicated";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
/*
|
|
|
|
* drop_writes
|
|
|
|
*/
|
|
|
|
if (!strcasecmp(arg_name, "drop_writes")) {
|
|
|
|
if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
|
|
|
|
ti->error = "Feature drop_writes duplicated";
|
|
|
|
return -EINVAL;
|
2016-12-13 19:54:50 +00:00
|
|
|
} else if (test_bit(ERROR_WRITES, &fc->flags)) {
|
|
|
|
ti->error = "Feature drop_writes conflicts with feature error_writes";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* error_writes
|
|
|
|
*/
|
|
|
|
if (!strcasecmp(arg_name, "error_writes")) {
|
|
|
|
if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
|
|
|
|
ti->error = "Feature error_writes duplicated";
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
} else if (test_bit(DROP_WRITES, &fc->flags)) {
|
|
|
|
ti->error = "Feature error_writes conflicts with feature drop_writes";
|
|
|
|
return -EINVAL;
|
2011-08-02 11:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:06 +00:00
|
|
|
/*
|
|
|
|
* corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
|
|
|
|
*/
|
|
|
|
if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
|
2011-09-25 22:26:15 +00:00
|
|
|
if (!argc) {
|
2011-08-02 11:32:06 +00:00
|
|
|
ti->error = "Feature corrupt_bio_byte requires parameters";
|
2011-09-25 22:26:15 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-08-02 11:32:06 +00:00
|
|
|
|
|
|
|
r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Direction r or w?
|
|
|
|
*/
|
|
|
|
arg_name = dm_shift_arg(as);
|
2023-04-18 19:57:47 +00:00
|
|
|
if (arg_name && !strcasecmp(arg_name, "w"))
|
2011-08-02 11:32:06 +00:00
|
|
|
fc->corrupt_bio_rw = WRITE;
|
2023-04-18 19:57:47 +00:00
|
|
|
else if (arg_name && !strcasecmp(arg_name, "r"))
|
2011-08-02 11:32:06 +00:00
|
|
|
fc->corrupt_bio_rw = READ;
|
|
|
|
else {
|
|
|
|
ti->error = "Invalid corrupt bio direction (r or w)";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Value of byte (0-255) to write in place of correct one.
|
|
|
|
*/
|
|
|
|
r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only corrupt bios with these flags set.
|
|
|
|
*/
|
2022-07-14 18:06:51 +00:00
|
|
|
BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
|
|
|
|
sizeof(unsigned int));
|
|
|
|
r = dm_read_arg(_args + 3, as,
|
2023-01-25 20:14:58 +00:00
|
|
|
(__force unsigned int *)&fc->corrupt_bio_flags,
|
2022-07-14 18:06:51 +00:00
|
|
|
&ti->error);
|
2011-08-02 11:32:06 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-01 13:20:08 +00:00
|
|
|
if (!strcasecmp(arg_name, "random_read_corrupt")) {
|
|
|
|
if (!argc) {
|
|
|
|
ti->error = "Feature random_read_corrupt requires a parameter";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
r = dm_read_arg(_args + 4, as, &fc->random_read_corrupt, &ti->error);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(arg_name, "random_write_corrupt")) {
|
|
|
|
if (!argc) {
|
|
|
|
ti->error = "Feature random_write_corrupt requires a parameter";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
r = dm_read_arg(_args + 4, as, &fc->random_write_corrupt, &ti->error);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
ti->error = "Unrecognised flakey feature requested";
|
2011-08-02 11:32:06 +00:00
|
|
|
return -EINVAL;
|
2011-08-02 11:32:05 +00:00
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:06 +00:00
|
|
|
if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
|
|
|
|
ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
|
|
|
|
return -EINVAL;
|
2016-12-13 19:54:50 +00:00
|
|
|
|
|
|
|
} else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
|
|
|
|
ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
|
|
|
|
return -EINVAL;
|
2011-08-02 11:32:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:56:41 +00:00
|
|
|
if (!fc->corrupt_bio_byte && !test_bit(ERROR_READS, &fc->flags) &&
|
2023-05-01 13:20:08 +00:00
|
|
|
!test_bit(DROP_WRITES, &fc->flags) && !test_bit(ERROR_WRITES, &fc->flags) &&
|
|
|
|
!fc->random_read_corrupt && !fc->random_write_corrupt) {
|
2023-04-18 19:56:41 +00:00
|
|
|
set_bit(ERROR_WRITES, &fc->flags);
|
|
|
|
set_bit(ERROR_READS, &fc->flags);
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:06 +00:00
|
|
|
return 0;
|
2011-08-02 11:32:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 13:54:24 +00:00
|
|
|
/*
|
2011-08-02 11:32:05 +00:00
|
|
|
* Construct a flakey mapping:
|
|
|
|
* <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
|
2011-08-02 11:32:05 +00:00
|
|
|
*
|
|
|
|
* Feature args:
|
|
|
|
* [drop_writes]
|
2011-08-02 11:32:06 +00:00
|
|
|
* [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
|
|
|
|
*
|
|
|
|
* Nth_byte starts from 1 for the first byte.
|
|
|
|
* Direction is r for READ or w for WRITE.
|
|
|
|
* bio_flags is ignored if 0.
|
2011-03-24 13:54:24 +00:00
|
|
|
*/
|
|
|
|
static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
|
|
|
|
{
|
2017-06-22 18:32:45 +00:00
|
|
|
static const struct dm_arg _args[] = {
|
2011-08-02 11:32:05 +00:00
|
|
|
{0, UINT_MAX, "Invalid up interval"},
|
|
|
|
{0, UINT_MAX, "Invalid down interval"},
|
|
|
|
};
|
|
|
|
|
|
|
|
int r;
|
2011-03-24 13:54:24 +00:00
|
|
|
struct flakey_c *fc;
|
2011-08-02 11:32:05 +00:00
|
|
|
unsigned long long tmpll;
|
|
|
|
struct dm_arg_set as;
|
|
|
|
const char *devname;
|
dm: reject trailing characters in sccanf input
Device mapper uses sscanf to convert arguments to numbers. The problem is that
the way we use it ignores additional unmatched characters in the scanned string.
For example, this `if (sscanf(string, "%d", &number) == 1)' will match a number,
but also it will match number with some garbage appended, like "123abc".
As a result, device mapper accepts garbage after some numbers. For example
the command `dmsetup create vg1-new --table "0 16384 linear 254:1bla 34816bla"'
will pass without an error.
This patch fixes all sscanf uses in device mapper. It appends "%c" with
a pointer to a dummy character variable to every sscanf statement.
The construct `if (sscanf(string, "%d%c", &number, &dummy) == 1)' succeeds
only if string is a null-terminated number (optionally preceded by some
whitespace characters). If there is some character appended after the number,
sscanf matches "%c", writes the character to the dummy variable and returns 2.
We check the return value for 1 and consequently reject numbers with some
garbage appended.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
2012-03-28 17:41:26 +00:00
|
|
|
char dummy;
|
2011-03-24 13:54:24 +00:00
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
as.argc = argc;
|
|
|
|
as.argv = argv;
|
|
|
|
|
|
|
|
if (argc < 4) {
|
|
|
|
ti->error = "Invalid argument count";
|
2011-03-24 13:54:24 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
fc = kzalloc(sizeof(*fc), GFP_KERNEL);
|
2011-03-24 13:54:24 +00:00
|
|
|
if (!fc) {
|
2013-07-10 22:41:17 +00:00
|
|
|
ti->error = "Cannot allocate context";
|
2011-03-24 13:54:24 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
fc->start_time = jiffies;
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
devname = dm_shift_arg(&as);
|
|
|
|
|
2015-07-31 13:20:36 +00:00
|
|
|
r = -EINVAL;
|
2018-11-07 21:24:55 +00:00
|
|
|
if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
|
2011-08-02 11:32:05 +00:00
|
|
|
ti->error = "Invalid device sector";
|
2011-03-24 13:54:24 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
2011-08-02 11:32:05 +00:00
|
|
|
fc->start = tmpll;
|
2011-03-24 13:54:24 +00:00
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
|
|
|
|
if (r)
|
2011-03-24 13:54:24 +00:00
|
|
|
goto bad;
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
|
|
|
|
if (r)
|
2011-03-24 13:54:24 +00:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if (!(fc->up_interval + fc->down_interval)) {
|
2011-08-02 11:32:05 +00:00
|
|
|
ti->error = "Total (up + down) interval is zero";
|
2016-08-08 14:09:27 +00:00
|
|
|
r = -EINVAL;
|
2011-03-24 13:54:24 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fc->up_interval + fc->down_interval < fc->up_interval) {
|
2011-08-02 11:32:05 +00:00
|
|
|
ti->error = "Interval overflow";
|
2016-08-08 14:09:27 +00:00
|
|
|
r = -EINVAL;
|
2011-03-24 13:54:24 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
r = parse_features(&as, fc, ti);
|
2011-08-02 11:32:05 +00:00
|
|
|
if (r)
|
|
|
|
goto bad;
|
|
|
|
|
2015-07-31 13:20:36 +00:00
|
|
|
r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
|
|
|
|
if (r) {
|
2011-08-02 11:32:05 +00:00
|
|
|
ti->error = "Device lookup failed";
|
2011-03-24 13:54:24 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2013-03-01 22:45:47 +00:00
|
|
|
ti->num_flush_bios = 1;
|
|
|
|
ti->num_discard_bios = 1;
|
2016-01-31 18:28:26 +00:00
|
|
|
ti->per_io_data_size = sizeof(struct per_bio_data);
|
2011-03-24 13:54:24 +00:00
|
|
|
ti->private = fc;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
kfree(fc);
|
2015-07-31 13:20:36 +00:00
|
|
|
return r;
|
2011-03-24 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flakey_dtr(struct dm_target *ti)
|
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
|
|
|
|
dm_put_device(ti, fc->dev);
|
|
|
|
kfree(fc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
|
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
return fc->start + dm_target_offset(ti, bi_sector);
|
2011-03-24 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
|
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
|
2017-08-23 17:10:32 +00:00
|
|
|
bio_set_dev(bio, fc->dev->bdev);
|
2022-03-30 17:52:10 +00:00
|
|
|
bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
|
2011-03-24 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 13:20:08 +00:00
|
|
|
static void corrupt_bio_common(struct bio *bio, unsigned int corrupt_bio_byte,
|
|
|
|
unsigned char corrupt_bio_value)
|
2011-08-02 11:32:06 +00:00
|
|
|
{
|
2018-11-13 13:04:24 +00:00
|
|
|
struct bvec_iter iter;
|
|
|
|
struct bio_vec bvec;
|
|
|
|
|
2011-08-02 11:32:06 +00:00
|
|
|
/*
|
2018-11-13 13:04:24 +00:00
|
|
|
* Overwrite the Nth byte of the bio's data, on whichever page
|
|
|
|
* it falls.
|
2011-08-02 11:32:06 +00:00
|
|
|
*/
|
2018-11-13 13:04:24 +00:00
|
|
|
bio_for_each_segment(bvec, bio, iter) {
|
|
|
|
if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
|
2023-05-01 13:20:08 +00:00
|
|
|
unsigned char *segment = bvec_kmap_local(&bvec);
|
|
|
|
segment[corrupt_bio_byte] = corrupt_bio_value;
|
2023-01-22 19:03:31 +00:00
|
|
|
kunmap_local(segment);
|
2018-11-13 13:04:24 +00:00
|
|
|
DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
|
|
|
|
"(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
|
2023-05-01 13:20:08 +00:00
|
|
|
bio, corrupt_bio_value, corrupt_bio_byte,
|
2018-11-13 13:04:24 +00:00
|
|
|
(bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
|
2023-05-01 13:20:08 +00:00
|
|
|
(unsigned long long)bio->bi_iter.bi_sector,
|
|
|
|
bio->bi_iter.bi_size);
|
2018-11-13 13:04:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
corrupt_bio_byte -= bio_iter_len(bio, iter);
|
2011-08-02 11:32:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 13:20:08 +00:00
|
|
|
static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
|
|
|
|
{
|
|
|
|
unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
|
|
|
|
|
|
|
|
if (!bio_has_data(bio))
|
|
|
|
return;
|
|
|
|
|
|
|
|
corrupt_bio_common(bio, corrupt_bio_byte, fc->corrupt_bio_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void corrupt_bio_random(struct bio *bio)
|
|
|
|
{
|
|
|
|
unsigned int corrupt_byte;
|
|
|
|
unsigned char corrupt_value;
|
|
|
|
|
|
|
|
if (!bio_has_data(bio))
|
|
|
|
return;
|
|
|
|
|
|
|
|
corrupt_byte = get_random_u32() % bio->bi_iter.bi_size;
|
|
|
|
corrupt_value = get_random_u8();
|
|
|
|
|
|
|
|
corrupt_bio_common(bio, corrupt_byte, corrupt_value);
|
|
|
|
}
|
|
|
|
|
2023-05-01 13:19:45 +00:00
|
|
|
static void clone_free(struct bio *clone)
|
|
|
|
{
|
|
|
|
struct folio_iter fi;
|
|
|
|
|
|
|
|
if (clone->bi_vcnt > 0) { /* bio_for_each_folio_all crashes with an empty bio */
|
|
|
|
bio_for_each_folio_all(fi, clone)
|
|
|
|
folio_put(fi.folio);
|
|
|
|
}
|
|
|
|
|
|
|
|
bio_uninit(clone);
|
|
|
|
kfree(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clone_endio(struct bio *clone)
|
|
|
|
{
|
|
|
|
struct bio *bio = clone->bi_private;
|
|
|
|
bio->bi_status = clone->bi_status;
|
|
|
|
clone_free(clone);
|
|
|
|
bio_endio(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bio *clone_bio(struct dm_target *ti, struct flakey_c *fc, struct bio *bio)
|
|
|
|
{
|
|
|
|
struct bio *clone;
|
|
|
|
unsigned size, remaining_size, nr_iovecs, order;
|
|
|
|
struct bvec_iter iter = bio->bi_iter;
|
|
|
|
|
|
|
|
if (unlikely(bio->bi_iter.bi_size > UIO_MAXIOV << PAGE_SHIFT))
|
|
|
|
dm_accept_partial_bio(bio, UIO_MAXIOV << PAGE_SHIFT >> SECTOR_SHIFT);
|
|
|
|
|
|
|
|
size = bio->bi_iter.bi_size;
|
|
|
|
nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
|
|
|
|
|
|
clone = bio_kmalloc(nr_iovecs, GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
|
|
|
|
if (!clone)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bio_init(clone, fc->dev->bdev, bio->bi_inline_vecs, nr_iovecs, bio->bi_opf);
|
|
|
|
|
|
|
|
clone->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
|
|
|
|
clone->bi_private = bio;
|
|
|
|
clone->bi_end_io = clone_endio;
|
|
|
|
|
|
|
|
remaining_size = size;
|
|
|
|
|
2023-12-28 14:47:04 +00:00
|
|
|
order = MAX_PAGE_ORDER;
|
2023-05-01 13:19:45 +00:00
|
|
|
while (remaining_size) {
|
|
|
|
struct page *pages;
|
|
|
|
unsigned size_to_add, to_copy;
|
|
|
|
unsigned char *virt;
|
|
|
|
unsigned remaining_order = __fls((remaining_size + PAGE_SIZE - 1) >> PAGE_SHIFT);
|
|
|
|
order = min(order, remaining_order);
|
|
|
|
|
|
|
|
retry_alloc_pages:
|
|
|
|
pages = alloc_pages(GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN | __GFP_COMP, order);
|
|
|
|
if (unlikely(!pages)) {
|
|
|
|
if (order) {
|
|
|
|
order--;
|
|
|
|
goto retry_alloc_pages;
|
|
|
|
}
|
|
|
|
clone_free(clone);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_to_add = min((unsigned)PAGE_SIZE << order, remaining_size);
|
|
|
|
|
|
|
|
virt = page_to_virt(pages);
|
|
|
|
to_copy = size_to_add;
|
|
|
|
do {
|
|
|
|
struct bio_vec bvec = bvec_iter_bvec(bio->bi_io_vec, iter);
|
|
|
|
unsigned this_step = min(bvec.bv_len, to_copy);
|
|
|
|
void *map = bvec_kmap_local(&bvec);
|
|
|
|
memcpy(virt, map, this_step);
|
|
|
|
kunmap_local(map);
|
|
|
|
|
|
|
|
bvec_iter_advance(bio->bi_io_vec, &iter, this_step);
|
|
|
|
to_copy -= this_step;
|
|
|
|
virt += this_step;
|
|
|
|
} while (to_copy);
|
|
|
|
|
|
|
|
__bio_add_page(clone, pages, size_to_add, 0);
|
|
|
|
remaining_size -= size_to_add;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2012-12-21 20:23:41 +00:00
|
|
|
static int flakey_map(struct dm_target *ti, struct bio *bio)
|
2011-03-24 13:54:24 +00:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int elapsed;
|
2012-12-21 20:23:39 +00:00
|
|
|
struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
|
2023-02-01 22:42:29 +00:00
|
|
|
|
2012-12-21 20:23:39 +00:00
|
|
|
pb->bio_submitted = false;
|
2011-03-24 13:54:24 +00:00
|
|
|
|
2019-10-27 14:05:48 +00:00
|
|
|
if (op_is_zone_mgmt(bio_op(bio)))
|
2017-05-08 23:40:49 +00:00
|
|
|
goto map_bio;
|
|
|
|
|
2011-03-24 13:54:24 +00:00
|
|
|
/* Are we alive ? */
|
|
|
|
elapsed = (jiffies - fc->start_time) / HZ;
|
2011-08-02 11:32:05 +00:00
|
|
|
if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
|
2023-05-01 13:20:08 +00:00
|
|
|
bool corrupt_fixed, corrupt_random;
|
2011-08-02 11:32:06 +00:00
|
|
|
/*
|
|
|
|
* Flag this bio as submitted while down.
|
|
|
|
*/
|
2012-12-21 20:23:39 +00:00
|
|
|
pb->bio_submitted = true;
|
2011-08-02 11:32:06 +00:00
|
|
|
|
|
|
|
/*
|
2016-12-13 19:54:50 +00:00
|
|
|
* Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
|
2016-08-25 01:12:58 +00:00
|
|
|
* Otherwise, flakey_end_io() will decide if the reads should be modified.
|
2011-08-02 11:32:06 +00:00
|
|
|
*/
|
2016-07-29 17:19:55 +00:00
|
|
|
if (bio_data_dir(bio) == READ) {
|
2023-04-18 19:56:41 +00:00
|
|
|
if (test_bit(ERROR_READS, &fc->flags))
|
2017-06-03 07:38:02 +00:00
|
|
|
return DM_MAPIO_KILL;
|
2016-08-25 01:12:58 +00:00
|
|
|
goto map_bio;
|
2016-07-29 17:19:55 +00:00
|
|
|
}
|
2011-08-02 11:32:05 +00:00
|
|
|
|
|
|
|
/*
|
2016-12-13 19:54:50 +00:00
|
|
|
* Drop or error writes?
|
2011-08-02 11:32:05 +00:00
|
|
|
*/
|
|
|
|
if (test_bit(DROP_WRITES, &fc->flags)) {
|
2015-07-20 13:29:37 +00:00
|
|
|
bio_endio(bio);
|
2011-08-02 11:32:06 +00:00
|
|
|
return DM_MAPIO_SUBMITTED;
|
2023-01-30 21:13:54 +00:00
|
|
|
} else if (test_bit(ERROR_WRITES, &fc->flags)) {
|
2016-12-13 19:54:50 +00:00
|
|
|
bio_io_error(bio);
|
|
|
|
return DM_MAPIO_SUBMITTED;
|
|
|
|
}
|
2011-08-02 11:32:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Corrupt matching writes.
|
|
|
|
*/
|
2023-05-01 13:20:08 +00:00
|
|
|
corrupt_fixed = false;
|
|
|
|
corrupt_random = false;
|
2023-05-01 13:19:45 +00:00
|
|
|
if (fc->corrupt_bio_byte && fc->corrupt_bio_rw == WRITE) {
|
2023-05-01 13:20:08 +00:00
|
|
|
if (all_corrupt_bio_flags_match(bio, fc))
|
|
|
|
corrupt_fixed = true;
|
|
|
|
}
|
|
|
|
if (fc->random_write_corrupt) {
|
|
|
|
u64 rnd = get_random_u64();
|
|
|
|
u32 rem = do_div(rnd, PROBABILITY_BASE);
|
|
|
|
if (rem < fc->random_write_corrupt)
|
|
|
|
corrupt_random = true;
|
|
|
|
}
|
|
|
|
if (corrupt_fixed || corrupt_random) {
|
|
|
|
struct bio *clone = clone_bio(ti, fc, bio);
|
|
|
|
if (clone) {
|
|
|
|
if (corrupt_fixed)
|
2023-05-01 13:19:45 +00:00
|
|
|
corrupt_bio_data(clone, fc);
|
2023-05-01 13:20:08 +00:00
|
|
|
if (corrupt_random)
|
|
|
|
corrupt_bio_random(clone);
|
|
|
|
submit_bio(clone);
|
|
|
|
return DM_MAPIO_SUBMITTED;
|
2023-01-22 19:03:56 +00:00
|
|
|
}
|
2011-08-02 11:32:05 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-24 13:54:24 +00:00
|
|
|
|
2011-08-02 11:32:05 +00:00
|
|
|
map_bio:
|
2011-03-24 13:54:24 +00:00
|
|
|
flakey_map_bio(ti, bio);
|
|
|
|
|
|
|
|
return DM_MAPIO_REMAPPED;
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:38:06 +00:00
|
|
|
static int flakey_end_io(struct dm_target *ti, struct bio *bio,
|
2017-05-08 23:40:49 +00:00
|
|
|
blk_status_t *error)
|
2011-08-02 11:32:06 +00:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
2012-12-21 20:23:39 +00:00
|
|
|
struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
|
2011-08-02 11:32:06 +00:00
|
|
|
|
2019-10-27 14:05:48 +00:00
|
|
|
if (op_is_zone_mgmt(bio_op(bio)))
|
2017-05-08 23:40:49 +00:00
|
|
|
return DM_ENDIO_DONE;
|
|
|
|
|
2017-06-03 07:38:03 +00:00
|
|
|
if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
|
2023-01-22 19:03:56 +00:00
|
|
|
if (fc->corrupt_bio_byte) {
|
|
|
|
if ((fc->corrupt_bio_rw == READ) &&
|
|
|
|
all_corrupt_bio_flags_match(bio, fc)) {
|
|
|
|
/*
|
|
|
|
* Corrupt successful matching READs while in down state.
|
|
|
|
*/
|
|
|
|
corrupt_bio_data(bio, fc);
|
|
|
|
}
|
2023-04-18 19:56:41 +00:00
|
|
|
}
|
2023-05-01 13:20:08 +00:00
|
|
|
if (fc->random_read_corrupt) {
|
|
|
|
u64 rnd = get_random_u64();
|
|
|
|
u32 rem = do_div(rnd, PROBABILITY_BASE);
|
|
|
|
if (rem < fc->random_read_corrupt)
|
|
|
|
corrupt_bio_random(bio);
|
|
|
|
}
|
2023-04-18 19:56:41 +00:00
|
|
|
if (test_bit(ERROR_READS, &fc->flags)) {
|
2016-08-25 01:12:58 +00:00
|
|
|
/*
|
|
|
|
* Error read during the down_interval if drop_writes
|
2016-12-13 19:54:50 +00:00
|
|
|
* and error_writes were not configured.
|
2016-08-25 01:12:58 +00:00
|
|
|
*/
|
2017-06-03 07:38:06 +00:00
|
|
|
*error = BLK_STS_IOERR;
|
2016-08-25 01:12:58 +00:00
|
|
|
}
|
2016-07-29 17:19:55 +00:00
|
|
|
}
|
2011-08-02 11:32:06 +00:00
|
|
|
|
2017-06-03 07:38:03 +00:00
|
|
|
return DM_ENDIO_DONE;
|
2011-08-02 11:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 22:45:44 +00:00
|
|
|
static void flakey_status(struct dm_target *ti, status_type_t type,
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int status_flags, char *result, unsigned int maxlen)
|
2011-03-24 13:54:24 +00:00
|
|
|
{
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int sz = 0;
|
2011-03-24 13:54:24 +00:00
|
|
|
struct flakey_c *fc = ti->private;
|
2023-04-18 19:56:41 +00:00
|
|
|
unsigned int error_reads, drop_writes, error_writes;
|
2011-03-24 13:54:24 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case STATUSTYPE_INFO:
|
|
|
|
result[0] = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATUSTYPE_TABLE:
|
2023-04-18 19:56:09 +00:00
|
|
|
DMEMIT("%s %llu %u %u", fc->dev->name,
|
2011-08-02 11:32:05 +00:00
|
|
|
(unsigned long long)fc->start, fc->up_interval,
|
|
|
|
fc->down_interval);
|
|
|
|
|
2023-04-18 19:56:41 +00:00
|
|
|
error_reads = test_bit(ERROR_READS, &fc->flags);
|
2011-08-02 11:32:05 +00:00
|
|
|
drop_writes = test_bit(DROP_WRITES, &fc->flags);
|
2016-12-13 19:54:50 +00:00
|
|
|
error_writes = test_bit(ERROR_WRITES, &fc->flags);
|
2023-05-01 13:20:08 +00:00
|
|
|
DMEMIT(" %u", error_reads + drop_writes + error_writes +
|
|
|
|
(fc->corrupt_bio_byte > 0) * 5 +
|
|
|
|
(fc->random_read_corrupt > 0) * 2 +
|
|
|
|
(fc->random_write_corrupt > 0) * 2);
|
2011-08-02 11:32:06 +00:00
|
|
|
|
2023-04-18 19:56:41 +00:00
|
|
|
if (error_reads)
|
|
|
|
DMEMIT(" error_reads");
|
2011-08-02 11:32:05 +00:00
|
|
|
if (drop_writes)
|
2023-04-18 19:56:09 +00:00
|
|
|
DMEMIT(" drop_writes");
|
2016-12-13 19:54:50 +00:00
|
|
|
else if (error_writes)
|
2023-04-18 19:56:09 +00:00
|
|
|
DMEMIT(" error_writes");
|
2011-08-02 11:32:06 +00:00
|
|
|
|
|
|
|
if (fc->corrupt_bio_byte)
|
2023-04-18 19:56:09 +00:00
|
|
|
DMEMIT(" corrupt_bio_byte %u %c %u %u",
|
2011-08-02 11:32:06 +00:00
|
|
|
fc->corrupt_bio_byte,
|
|
|
|
(fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
|
|
|
|
fc->corrupt_bio_value, fc->corrupt_bio_flags);
|
|
|
|
|
2023-05-01 13:20:08 +00:00
|
|
|
if (fc->random_read_corrupt > 0)
|
|
|
|
DMEMIT(" random_read_corrupt %u", fc->random_read_corrupt);
|
|
|
|
if (fc->random_write_corrupt > 0)
|
|
|
|
DMEMIT(" random_write_corrupt %u", fc->random_write_corrupt);
|
|
|
|
|
2011-03-24 13:54:24 +00:00
|
|
|
break;
|
2021-07-13 00:49:03 +00:00
|
|
|
|
|
|
|
case STATUSTYPE_IMA:
|
|
|
|
result[0] = '\0';
|
|
|
|
break;
|
2011-03-24 13:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 20:54:10 +00:00
|
|
|
static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
|
2011-03-24 13:54:24 +00:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
2015-10-15 12:10:50 +00:00
|
|
|
|
|
|
|
*bdev = fc->dev->bdev;
|
2011-03-24 13:54:24 +00:00
|
|
|
|
2012-01-12 15:01:29 +00:00
|
|
|
/*
|
|
|
|
* Only pass ioctls through if the device sizes match exactly.
|
|
|
|
*/
|
2021-10-18 10:11:05 +00:00
|
|
|
if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
|
2015-10-15 12:10:50 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2011-03-24 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 10:08:49 +00:00
|
|
|
#ifdef CONFIG_BLK_DEV_ZONED
|
2019-11-11 02:39:30 +00:00
|
|
|
static int flakey_report_zones(struct dm_target *ti,
|
|
|
|
struct dm_report_zones_args *args, unsigned int nr_zones)
|
2018-10-12 10:08:49 +00:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
|
2021-05-25 21:24:57 +00:00
|
|
|
return dm_report_zones(fc->dev->bdev, fc->start,
|
|
|
|
flakey_map_sector(ti, args->next_sector),
|
|
|
|
args, nr_zones);
|
2018-10-12 10:08:49 +00:00
|
|
|
}
|
2021-02-10 22:38:30 +00:00
|
|
|
#else
|
|
|
|
#define flakey_report_zones NULL
|
2018-10-12 10:08:49 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-24 13:54:24 +00:00
|
|
|
static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
|
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
|
|
|
|
return fn(ti, fc->dev, fc->start, ti->len, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct target_type flakey_target = {
|
|
|
|
.name = "flakey",
|
2017-05-08 23:40:49 +00:00
|
|
|
.version = {1, 5, 0},
|
2021-02-01 05:10:19 +00:00
|
|
|
.features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
|
2018-10-12 10:08:49 +00:00
|
|
|
.report_zones = flakey_report_zones,
|
2011-03-24 13:54:24 +00:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
.ctr = flakey_ctr,
|
|
|
|
.dtr = flakey_dtr,
|
|
|
|
.map = flakey_map,
|
2011-08-02 11:32:06 +00:00
|
|
|
.end_io = flakey_end_io,
|
2011-03-24 13:54:24 +00:00
|
|
|
.status = flakey_status,
|
2015-10-15 12:10:50 +00:00
|
|
|
.prepare_ioctl = flakey_prepare_ioctl,
|
2011-03-24 13:54:24 +00:00
|
|
|
.iterate_devices = flakey_iterate_devices,
|
|
|
|
};
|
2023-04-09 16:43:37 +00:00
|
|
|
module_dm(flakey);
|
2011-03-24 13:54:24 +00:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION(DM_NAME " flakey target");
|
2024-02-07 20:51:24 +00:00
|
|
|
MODULE_AUTHOR("Joe Thornber <dm-devel@lists.linux.dev>");
|
2011-03-24 13:54:24 +00:00
|
|
|
MODULE_LICENSE("GPL");
|