2011-03-24 21:54:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003 Sistina Software (UK) Limited.
|
2011-08-02 19:32:06 +08:00
|
|
|
* Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
|
2011-03-24 21:54:24 +08: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"
|
|
|
|
|
2011-08-02 19:32:06 +08:00
|
|
|
#define all_corrupt_bio_flags_match(bio, fc) \
|
2016-08-06 05:35:16 +08:00
|
|
|
(((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
|
2011-08-02 19:32:06 +08:00
|
|
|
|
2011-03-24 21:54:24 +08: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;
|
|
|
|
unsigned up_interval;
|
|
|
|
unsigned down_interval;
|
2011-08-02 19:32:05 +08:00
|
|
|
unsigned long flags;
|
2011-08-02 19:32:06 +08:00
|
|
|
unsigned corrupt_bio_byte;
|
|
|
|
unsigned corrupt_bio_rw;
|
|
|
|
unsigned corrupt_bio_value;
|
|
|
|
unsigned corrupt_bio_flags;
|
2011-03-24 21:54:24 +08:00
|
|
|
};
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
enum feature_flag_bits {
|
2016-12-14 03:54:50 +08:00
|
|
|
DROP_WRITES,
|
|
|
|
ERROR_WRITES
|
2011-08-02 19:32:05 +08:00
|
|
|
};
|
|
|
|
|
2012-12-22 04:23:39 +08:00
|
|
|
struct per_bio_data {
|
|
|
|
bool bio_submitted;
|
|
|
|
};
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
|
|
|
|
struct dm_target *ti)
|
2011-08-02 19:32:05 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
unsigned argc;
|
|
|
|
const char *arg_name;
|
|
|
|
|
2017-06-23 02:32:45 +08:00
|
|
|
static const struct dm_arg _args[] = {
|
2011-08-02 19:32:06 +08:00
|
|
|
{0, 6, "Invalid number of feature args"},
|
|
|
|
{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"},
|
2011-08-02 19:32:05 +08: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 19:32:06 +08:00
|
|
|
return r;
|
2011-08-02 19:32:05 +08:00
|
|
|
|
2011-08-02 19:32:06 +08:00
|
|
|
while (argc) {
|
2011-08-02 19:32:05 +08:00
|
|
|
arg_name = dm_shift_arg(as);
|
|
|
|
argc--;
|
|
|
|
|
2017-12-04 11:14:12 +08:00
|
|
|
if (!arg_name) {
|
|
|
|
ti->error = "Insufficient feature arguments";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:05 +08: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-14 03:54:50 +08: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 19:32:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:06 +08:00
|
|
|
/*
|
|
|
|
* corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
|
|
|
|
*/
|
|
|
|
if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
|
2011-09-26 06:26:15 +08:00
|
|
|
if (!argc) {
|
2011-08-02 19:32:06 +08:00
|
|
|
ti->error = "Feature corrupt_bio_byte requires parameters";
|
2011-09-26 06:26:15 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-08-02 19:32:06 +08: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);
|
|
|
|
if (!strcasecmp(arg_name, "w"))
|
|
|
|
fc->corrupt_bio_rw = WRITE;
|
|
|
|
else if (!strcasecmp(arg_name, "r"))
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
ti->error = "Unrecognised flakey feature requested";
|
2011-08-02 19:32:06 +08:00
|
|
|
return -EINVAL;
|
2011-08-02 19:32:05 +08:00
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:06 +08: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-14 03:54:50 +08: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 19:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-08-02 19:32:05 +08:00
|
|
|
}
|
|
|
|
|
2011-03-24 21:54:24 +08:00
|
|
|
/*
|
2011-08-02 19:32:05 +08:00
|
|
|
* Construct a flakey mapping:
|
|
|
|
* <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
|
2011-08-02 19:32:05 +08:00
|
|
|
*
|
|
|
|
* Feature args:
|
|
|
|
* [drop_writes]
|
2011-08-02 19:32:06 +08: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 21:54:24 +08:00
|
|
|
*/
|
|
|
|
static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
|
|
|
|
{
|
2017-06-23 02:32:45 +08:00
|
|
|
static const struct dm_arg _args[] = {
|
2011-08-02 19:32:05 +08:00
|
|
|
{0, UINT_MAX, "Invalid up interval"},
|
|
|
|
{0, UINT_MAX, "Invalid down interval"},
|
|
|
|
};
|
|
|
|
|
|
|
|
int r;
|
2011-03-24 21:54:24 +08:00
|
|
|
struct flakey_c *fc;
|
2011-08-02 19:32:05 +08: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-29 01:41:26 +08:00
|
|
|
char dummy;
|
2011-03-24 21:54:24 +08:00
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
as.argc = argc;
|
|
|
|
as.argv = argv;
|
|
|
|
|
|
|
|
if (argc < 4) {
|
|
|
|
ti->error = "Invalid argument count";
|
2011-03-24 21:54:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
fc = kzalloc(sizeof(*fc), GFP_KERNEL);
|
2011-03-24 21:54:24 +08:00
|
|
|
if (!fc) {
|
2013-07-11 06:41:17 +08:00
|
|
|
ti->error = "Cannot allocate context";
|
2011-03-24 21:54:24 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
fc->start_time = jiffies;
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
devname = dm_shift_arg(&as);
|
|
|
|
|
2015-07-31 21:20:36 +08:00
|
|
|
r = -EINVAL;
|
2018-11-08 05:24:55 +08:00
|
|
|
if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
|
2011-08-02 19:32:05 +08:00
|
|
|
ti->error = "Invalid device sector";
|
2011-03-24 21:54:24 +08:00
|
|
|
goto bad;
|
|
|
|
}
|
2011-08-02 19:32:05 +08:00
|
|
|
fc->start = tmpll;
|
2011-03-24 21:54:24 +08:00
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
|
|
|
|
if (r)
|
2011-03-24 21:54:24 +08:00
|
|
|
goto bad;
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
|
|
|
|
if (r)
|
2011-03-24 21:54:24 +08:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if (!(fc->up_interval + fc->down_interval)) {
|
2011-08-02 19:32:05 +08:00
|
|
|
ti->error = "Total (up + down) interval is zero";
|
2016-08-08 22:09:27 +08:00
|
|
|
r = -EINVAL;
|
2011-03-24 21:54:24 +08:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fc->up_interval + fc->down_interval < fc->up_interval) {
|
2011-08-02 19:32:05 +08:00
|
|
|
ti->error = "Interval overflow";
|
2016-08-08 22:09:27 +08:00
|
|
|
r = -EINVAL;
|
2011-03-24 21:54:24 +08:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
r = parse_features(&as, fc, ti);
|
2011-08-02 19:32:05 +08:00
|
|
|
if (r)
|
|
|
|
goto bad;
|
|
|
|
|
2015-07-31 21:20:36 +08:00
|
|
|
r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
|
|
|
|
if (r) {
|
2011-08-02 19:32:05 +08:00
|
|
|
ti->error = "Device lookup failed";
|
2011-03-24 21:54:24 +08:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2013-03-02 06:45:47 +08:00
|
|
|
ti->num_flush_bios = 1;
|
|
|
|
ti->num_discard_bios = 1;
|
2016-02-01 02:28:26 +08:00
|
|
|
ti->per_io_data_size = sizeof(struct per_bio_data);
|
2011-03-24 21:54:24 +08:00
|
|
|
ti->private = fc;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
kfree(fc);
|
2015-07-31 21:20:36 +08:00
|
|
|
return r;
|
2011-03-24 21:54:24 +08: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 19:32:05 +08:00
|
|
|
return fc->start + dm_target_offset(ti, bi_sector);
|
2011-03-24 21:54:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
|
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
|
2017-08-24 01:10:32 +08:00
|
|
|
bio_set_dev(bio, fc->dev->bdev);
|
2019-10-27 22:05:48 +08:00
|
|
|
if (bio_sectors(bio) || op_is_zone_mgmt(bio_op(bio)))
|
2013-10-12 06:44:27 +08:00
|
|
|
bio->bi_iter.bi_sector =
|
|
|
|
flakey_map_sector(ti, bio->bi_iter.bi_sector);
|
2011-03-24 21:54:24 +08:00
|
|
|
}
|
|
|
|
|
2011-08-02 19:32:06 +08:00
|
|
|
static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
|
|
|
|
{
|
2018-11-13 21:04:24 +08:00
|
|
|
unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
|
|
|
|
|
|
|
|
struct bvec_iter iter;
|
|
|
|
struct bio_vec bvec;
|
|
|
|
|
|
|
|
if (!bio_has_data(bio))
|
|
|
|
return;
|
2011-08-02 19:32:06 +08:00
|
|
|
|
|
|
|
/*
|
2018-11-13 21:04:24 +08:00
|
|
|
* Overwrite the Nth byte of the bio's data, on whichever page
|
|
|
|
* it falls.
|
2011-08-02 19:32:06 +08:00
|
|
|
*/
|
2018-11-13 21:04:24 +08:00
|
|
|
bio_for_each_segment(bvec, bio, iter) {
|
|
|
|
if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
|
|
|
|
char *segment = (page_address(bio_iter_page(bio, iter))
|
|
|
|
+ bio_iter_offset(bio, iter));
|
|
|
|
segment[corrupt_bio_byte] = fc->corrupt_bio_value;
|
|
|
|
DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
|
|
|
|
"(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
|
|
|
|
bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
|
|
|
|
(bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
|
|
|
|
(unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
corrupt_bio_byte -= bio_iter_len(bio, iter);
|
2011-08-02 19:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-22 04:23:41 +08:00
|
|
|
static int flakey_map(struct dm_target *ti, struct bio *bio)
|
2011-03-24 21:54:24 +08:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
|
|
|
unsigned elapsed;
|
2012-12-22 04:23:39 +08:00
|
|
|
struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
|
|
|
|
pb->bio_submitted = false;
|
2011-03-24 21:54:24 +08:00
|
|
|
|
2019-10-27 22:05:48 +08:00
|
|
|
if (op_is_zone_mgmt(bio_op(bio)))
|
2017-05-09 07:40:49 +08:00
|
|
|
goto map_bio;
|
|
|
|
|
2011-03-24 21:54:24 +08:00
|
|
|
/* Are we alive ? */
|
|
|
|
elapsed = (jiffies - fc->start_time) / HZ;
|
2011-08-02 19:32:05 +08:00
|
|
|
if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
|
2011-08-02 19:32:06 +08:00
|
|
|
/*
|
|
|
|
* Flag this bio as submitted while down.
|
|
|
|
*/
|
2012-12-22 04:23:39 +08:00
|
|
|
pb->bio_submitted = true;
|
2011-08-02 19:32:06 +08:00
|
|
|
|
|
|
|
/*
|
2016-12-14 03:54:50 +08:00
|
|
|
* Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
|
2016-08-25 09:12:58 +08:00
|
|
|
* Otherwise, flakey_end_io() will decide if the reads should be modified.
|
2011-08-02 19:32:06 +08:00
|
|
|
*/
|
2016-07-30 01:19:55 +08:00
|
|
|
if (bio_data_dir(bio) == READ) {
|
2016-12-14 03:54:50 +08:00
|
|
|
if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
|
|
|
|
!test_bit(ERROR_WRITES, &fc->flags))
|
2017-06-03 15:38:02 +08:00
|
|
|
return DM_MAPIO_KILL;
|
2016-08-25 09:12:58 +08:00
|
|
|
goto map_bio;
|
2016-07-30 01:19:55 +08:00
|
|
|
}
|
2011-08-02 19:32:05 +08:00
|
|
|
|
|
|
|
/*
|
2016-12-14 03:54:50 +08:00
|
|
|
* Drop or error writes?
|
2011-08-02 19:32:05 +08:00
|
|
|
*/
|
|
|
|
if (test_bit(DROP_WRITES, &fc->flags)) {
|
2015-07-20 21:29:37 +08:00
|
|
|
bio_endio(bio);
|
2011-08-02 19:32:06 +08:00
|
|
|
return DM_MAPIO_SUBMITTED;
|
|
|
|
}
|
2016-12-14 03:54:50 +08:00
|
|
|
else if (test_bit(ERROR_WRITES, &fc->flags)) {
|
|
|
|
bio_io_error(bio);
|
|
|
|
return DM_MAPIO_SUBMITTED;
|
|
|
|
}
|
2011-08-02 19:32:06 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Corrupt matching writes.
|
|
|
|
*/
|
|
|
|
if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
|
|
|
|
if (all_corrupt_bio_flags_match(bio, fc))
|
|
|
|
corrupt_bio_data(bio, fc);
|
2011-08-02 19:32:05 +08:00
|
|
|
goto map_bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-08-02 19:32:06 +08:00
|
|
|
* By default, error all I/O.
|
2011-08-02 19:32:05 +08:00
|
|
|
*/
|
2017-06-03 15:38:02 +08:00
|
|
|
return DM_MAPIO_KILL;
|
2011-08-02 19:32:05 +08:00
|
|
|
}
|
2011-03-24 21:54:24 +08:00
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
map_bio:
|
2011-03-24 21:54:24 +08:00
|
|
|
flakey_map_bio(ti, bio);
|
|
|
|
|
|
|
|
return DM_MAPIO_REMAPPED;
|
|
|
|
}
|
|
|
|
|
2017-06-03 15:38:06 +08:00
|
|
|
static int flakey_end_io(struct dm_target *ti, struct bio *bio,
|
2017-05-09 07:40:49 +08:00
|
|
|
blk_status_t *error)
|
2011-08-02 19:32:06 +08:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
2012-12-22 04:23:39 +08:00
|
|
|
struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
|
2011-08-02 19:32:06 +08:00
|
|
|
|
2019-10-27 22:05:48 +08:00
|
|
|
if (op_is_zone_mgmt(bio_op(bio)))
|
2017-05-09 07:40:49 +08:00
|
|
|
return DM_ENDIO_DONE;
|
|
|
|
|
2017-06-03 15:38:03 +08:00
|
|
|
if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
|
2016-08-25 09:12:58 +08:00
|
|
|
if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
|
|
|
|
all_corrupt_bio_flags_match(bio, fc)) {
|
|
|
|
/*
|
|
|
|
* Corrupt successful matching READs while in down state.
|
|
|
|
*/
|
2016-07-30 01:19:55 +08:00
|
|
|
corrupt_bio_data(bio, fc);
|
2016-08-25 09:12:58 +08:00
|
|
|
|
2016-12-14 03:54:50 +08:00
|
|
|
} else if (!test_bit(DROP_WRITES, &fc->flags) &&
|
|
|
|
!test_bit(ERROR_WRITES, &fc->flags)) {
|
2016-08-25 09:12:58 +08:00
|
|
|
/*
|
|
|
|
* Error read during the down_interval if drop_writes
|
2016-12-14 03:54:50 +08:00
|
|
|
* and error_writes were not configured.
|
2016-08-25 09:12:58 +08:00
|
|
|
*/
|
2017-06-03 15:38:06 +08:00
|
|
|
*error = BLK_STS_IOERR;
|
2016-08-25 09:12:58 +08:00
|
|
|
}
|
2016-07-30 01:19:55 +08:00
|
|
|
}
|
2011-08-02 19:32:06 +08:00
|
|
|
|
2017-06-03 15:38:03 +08:00
|
|
|
return DM_ENDIO_DONE;
|
2011-08-02 19:32:06 +08:00
|
|
|
}
|
|
|
|
|
2013-03-02 06:45:44 +08:00
|
|
|
static void flakey_status(struct dm_target *ti, status_type_t type,
|
|
|
|
unsigned status_flags, char *result, unsigned maxlen)
|
2011-03-24 21:54:24 +08:00
|
|
|
{
|
2011-08-02 19:32:05 +08:00
|
|
|
unsigned sz = 0;
|
2011-03-24 21:54:24 +08:00
|
|
|
struct flakey_c *fc = ti->private;
|
2016-12-14 03:54:50 +08:00
|
|
|
unsigned drop_writes, error_writes;
|
2011-03-24 21:54:24 +08:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case STATUSTYPE_INFO:
|
|
|
|
result[0] = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATUSTYPE_TABLE:
|
2011-08-02 19:32:05 +08:00
|
|
|
DMEMIT("%s %llu %u %u ", fc->dev->name,
|
|
|
|
(unsigned long long)fc->start, fc->up_interval,
|
|
|
|
fc->down_interval);
|
|
|
|
|
|
|
|
drop_writes = test_bit(DROP_WRITES, &fc->flags);
|
2016-12-14 03:54:50 +08:00
|
|
|
error_writes = test_bit(ERROR_WRITES, &fc->flags);
|
|
|
|
DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
|
2011-08-02 19:32:06 +08:00
|
|
|
|
2011-08-02 19:32:05 +08:00
|
|
|
if (drop_writes)
|
|
|
|
DMEMIT("drop_writes ");
|
2016-12-14 03:54:50 +08:00
|
|
|
else if (error_writes)
|
|
|
|
DMEMIT("error_writes ");
|
2011-08-02 19:32:06 +08:00
|
|
|
|
|
|
|
if (fc->corrupt_bio_byte)
|
|
|
|
DMEMIT("corrupt_bio_byte %u %c %u %u ",
|
|
|
|
fc->corrupt_bio_byte,
|
|
|
|
(fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
|
|
|
|
fc->corrupt_bio_value, fc->corrupt_bio_flags);
|
|
|
|
|
2011-03-24 21:54:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:54:10 +08:00
|
|
|
static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
|
2011-03-24 21:54:24 +08:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
2015-10-15 20:10:50 +08:00
|
|
|
|
|
|
|
*bdev = fc->dev->bdev;
|
2011-03-24 21:54:24 +08:00
|
|
|
|
2012-01-12 23:01:29 +08:00
|
|
|
/*
|
|
|
|
* Only pass ioctls through if the device sizes match exactly.
|
|
|
|
*/
|
|
|
|
if (fc->start ||
|
2015-10-15 20:10:50 +08:00
|
|
|
ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2011-03-24 21:54:24 +08:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:08:49 +08:00
|
|
|
#ifdef CONFIG_BLK_DEV_ZONED
|
2019-11-11 10:39:30 +08:00
|
|
|
static int flakey_report_zones(struct dm_target *ti,
|
|
|
|
struct dm_report_zones_args *args, unsigned int nr_zones)
|
2018-10-12 18:08:49 +08:00
|
|
|
{
|
|
|
|
struct flakey_c *fc = ti->private;
|
2019-11-11 10:39:30 +08:00
|
|
|
sector_t sector = flakey_map_sector(ti, args->next_sector);
|
2018-10-12 18:08:49 +08:00
|
|
|
|
2019-11-11 10:39:30 +08:00
|
|
|
args->start = fc->start;
|
|
|
|
return blkdev_report_zones(fc->dev->bdev, sector, nr_zones,
|
|
|
|
dm_report_zones_cb, args);
|
2018-10-12 18:08:49 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-24 21:54:24 +08: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-09 07:40:49 +08:00
|
|
|
.version = {1, 5, 0},
|
2018-10-11 10:45:30 +08:00
|
|
|
#ifdef CONFIG_BLK_DEV_ZONED
|
2017-05-09 07:40:49 +08:00
|
|
|
.features = DM_TARGET_ZONED_HM,
|
2018-10-12 18:08:49 +08:00
|
|
|
.report_zones = flakey_report_zones,
|
2018-10-11 10:45:30 +08:00
|
|
|
#endif
|
2011-03-24 21:54:24 +08:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
.ctr = flakey_ctr,
|
|
|
|
.dtr = flakey_dtr,
|
|
|
|
.map = flakey_map,
|
2011-08-02 19:32:06 +08:00
|
|
|
.end_io = flakey_end_io,
|
2011-03-24 21:54:24 +08:00
|
|
|
.status = flakey_status,
|
2015-10-15 20:10:50 +08:00
|
|
|
.prepare_ioctl = flakey_prepare_ioctl,
|
2011-03-24 21:54:24 +08:00
|
|
|
.iterate_devices = flakey_iterate_devices,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init dm_flakey_init(void)
|
|
|
|
{
|
|
|
|
int r = dm_register_target(&flakey_target);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
DMERR("register failed %d", r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit dm_flakey_exit(void)
|
|
|
|
{
|
|
|
|
dm_unregister_target(&flakey_target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Module hooks */
|
|
|
|
module_init(dm_flakey_init);
|
|
|
|
module_exit(dm_flakey_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION(DM_NAME " flakey target");
|
|
|
|
MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|