Skip to content

Commit a399879

Browse files
snitmkergon
authored andcommitted
dm flakey: add corrupt_bio_byte feature
Add corrupt_bio_byte feature to simulate corruption by overwriting a byte at a specified position with a specified value during intervals when the device is "down". Signed-off-by: Mike Snitzer <[email protected]> Signed-off-by: Alasdair G Kergon <[email protected]>
1 parent b26f5e3 commit a399879

File tree

2 files changed

+159
-15
lines changed

2 files changed

+159
-15
lines changed

Documentation/device-mapper/dm-flakey.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@ Optional feature parameters:
3232
drop_writes:
3333
All write I/O is silently ignored.
3434
Read I/O is handled correctly.
35+
36+
corrupt_bio_byte <Nth_byte> <direction> <value> <flags>:
37+
During <down interval>, replace <Nth_byte> of the data of
38+
each matching bio with <value>.
39+
40+
<Nth_byte>: The offset of the byte to replace.
41+
Counting starts at 1, to replace the first byte.
42+
<direction>: Either 'r' to corrupt reads or 'w' to corrupt writes.
43+
'w' is incompatible with drop_writes.
44+
<value>: The value (from 0-255) to write.
45+
<flags>: Perform the replacement only if bio->bi_rw has all the
46+
selected flags set.
47+
48+
Examples:
49+
corrupt_bio_byte 32 r 1 0
50+
- replaces the 32nd byte of READ bios with the value 1
51+
52+
corrupt_bio_byte 224 w 0 32
53+
- replaces the 224th byte of REQ_META (=32) bios with the value 0

drivers/md/dm-flakey.c

Lines changed: 140 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2003 Sistina Software (UK) Limited.
3-
* Copyright (C) 2004, 2010 Red Hat, Inc. All rights reserved.
3+
* Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
44
*
55
* This file is released under the GPL.
66
*/
@@ -15,6 +15,9 @@
1515

1616
#define DM_MSG_PREFIX "flakey"
1717

18+
#define all_corrupt_bio_flags_match(bio, fc) \
19+
(((bio)->bi_rw & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
20+
1821
/*
1922
* Flakey: Used for testing only, simulates intermittent,
2023
* catastrophic device failure.
@@ -26,6 +29,10 @@ struct flakey_c {
2629
unsigned up_interval;
2730
unsigned down_interval;
2831
unsigned long flags;
32+
unsigned corrupt_bio_byte;
33+
unsigned corrupt_bio_rw;
34+
unsigned corrupt_bio_value;
35+
unsigned corrupt_bio_flags;
2936
};
3037

3138
enum feature_flag_bits {
@@ -40,7 +47,10 @@ static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
4047
const char *arg_name;
4148

4249
static struct dm_arg _args[] = {
43-
{0, 1, "Invalid number of feature args"},
50+
{0, 6, "Invalid number of feature args"},
51+
{1, UINT_MAX, "Invalid corrupt bio byte"},
52+
{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
53+
{0, UINT_MAX, "Invalid corrupt bio flags mask"},
4454
};
4555

4656
/* No feature arguments supplied. */
@@ -49,9 +59,9 @@ static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
4959

5060
r = dm_read_arg_group(_args, as, &argc, &ti->error);
5161
if (r)
52-
return -EINVAL;
62+
return r;
5363

54-
while (argc && !r) {
64+
while (argc) {
5565
arg_name = dm_shift_arg(as);
5666
argc--;
5767

@@ -67,11 +77,61 @@ static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
6777
continue;
6878
}
6979

80+
/*
81+
* corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
82+
*/
83+
if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
84+
if (!argc)
85+
ti->error = "Feature corrupt_bio_byte requires parameters";
86+
87+
r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
88+
if (r)
89+
return r;
90+
argc--;
91+
92+
/*
93+
* Direction r or w?
94+
*/
95+
arg_name = dm_shift_arg(as);
96+
if (!strcasecmp(arg_name, "w"))
97+
fc->corrupt_bio_rw = WRITE;
98+
else if (!strcasecmp(arg_name, "r"))
99+
fc->corrupt_bio_rw = READ;
100+
else {
101+
ti->error = "Invalid corrupt bio direction (r or w)";
102+
return -EINVAL;
103+
}
104+
argc--;
105+
106+
/*
107+
* Value of byte (0-255) to write in place of correct one.
108+
*/
109+
r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
110+
if (r)
111+
return r;
112+
argc--;
113+
114+
/*
115+
* Only corrupt bios with these flags set.
116+
*/
117+
r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
118+
if (r)
119+
return r;
120+
argc--;
121+
122+
continue;
123+
}
124+
70125
ti->error = "Unrecognised flakey feature requested";
71-
r = -EINVAL;
126+
return -EINVAL;
72127
}
73128

74-
return r;
129+
if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
130+
ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
131+
return -EINVAL;
132+
}
133+
134+
return 0;
75135
}
76136

77137
/*
@@ -80,6 +140,11 @@ static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
80140
*
81141
* Feature args:
82142
* [drop_writes]
143+
* [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
144+
*
145+
* Nth_byte starts from 1 for the first byte.
146+
* Direction is r for READ or w for WRITE.
147+
* bio_flags is ignored if 0.
83148
*/
84149
static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
85150
{
@@ -178,31 +243,64 @@ static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
178243
bio->bi_sector = flakey_map_sector(ti, bio->bi_sector);
179244
}
180245

246+
static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
247+
{
248+
unsigned bio_bytes = bio_cur_bytes(bio);
249+
char *data = bio_data(bio);
250+
251+
/*
252+
* Overwrite the Nth byte of the data returned.
253+
*/
254+
if (data && bio_bytes >= fc->corrupt_bio_byte) {
255+
data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
256+
257+
DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
258+
"(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
259+
bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
260+
(bio_data_dir(bio) == WRITE) ? 'w' : 'r',
261+
bio->bi_rw, (unsigned long long)bio->bi_sector, bio_bytes);
262+
}
263+
}
264+
181265
static int flakey_map(struct dm_target *ti, struct bio *bio,
182266
union map_info *map_context)
183267
{
184268
struct flakey_c *fc = ti->private;
185269
unsigned elapsed;
186-
unsigned rw;
187270

188271
/* Are we alive ? */
189272
elapsed = (jiffies - fc->start_time) / HZ;
190273
if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
191-
rw = bio_data_dir(bio);
274+
/*
275+
* Flag this bio as submitted while down.
276+
*/
277+
map_context->ll = 1;
278+
279+
/*
280+
* Map reads as normal.
281+
*/
282+
if (bio_data_dir(bio) == READ)
283+
goto map_bio;
192284

193285
/*
194-
* Drop writes. Map reads as normal.
286+
* Drop writes?
195287
*/
196288
if (test_bit(DROP_WRITES, &fc->flags)) {
197-
if (rw == WRITE) {
198-
bio_endio(bio, 0);
199-
return DM_MAPIO_SUBMITTED;
200-
}
289+
bio_endio(bio, 0);
290+
return DM_MAPIO_SUBMITTED;
291+
}
292+
293+
/*
294+
* Corrupt matching writes.
295+
*/
296+
if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
297+
if (all_corrupt_bio_flags_match(bio, fc))
298+
corrupt_bio_data(bio, fc);
201299
goto map_bio;
202300
}
203301

204302
/*
205-
* Default setting errors all I/O.
303+
* By default, error all I/O.
206304
*/
207305
return -EIO;
208306
}
@@ -213,6 +311,24 @@ static int flakey_map(struct dm_target *ti, struct bio *bio,
213311
return DM_MAPIO_REMAPPED;
214312
}
215313

314+
static int flakey_end_io(struct dm_target *ti, struct bio *bio,
315+
int error, union map_info *map_context)
316+
{
317+
struct flakey_c *fc = ti->private;
318+
unsigned bio_submitted_while_down = map_context->ll;
319+
320+
/*
321+
* Corrupt successful READs while in down state.
322+
* If flags were specified, only corrupt those that match.
323+
*/
324+
if (!error && bio_submitted_while_down &&
325+
(bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
326+
all_corrupt_bio_flags_match(bio, fc))
327+
corrupt_bio_data(bio, fc);
328+
329+
return error;
330+
}
331+
216332
static int flakey_status(struct dm_target *ti, status_type_t type,
217333
char *result, unsigned int maxlen)
218334
{
@@ -231,9 +347,17 @@ static int flakey_status(struct dm_target *ti, status_type_t type,
231347
fc->down_interval);
232348

233349
drop_writes = test_bit(DROP_WRITES, &fc->flags);
234-
DMEMIT("%u ", drop_writes);
350+
DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
351+
235352
if (drop_writes)
236353
DMEMIT("drop_writes ");
354+
355+
if (fc->corrupt_bio_byte)
356+
DMEMIT("corrupt_bio_byte %u %c %u %u ",
357+
fc->corrupt_bio_byte,
358+
(fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
359+
fc->corrupt_bio_value, fc->corrupt_bio_flags);
360+
237361
break;
238362
}
239363
return 0;
@@ -275,6 +399,7 @@ static struct target_type flakey_target = {
275399
.ctr = flakey_ctr,
276400
.dtr = flakey_dtr,
277401
.map = flakey_map,
402+
.end_io = flakey_end_io,
278403
.status = flakey_status,
279404
.ioctl = flakey_ioctl,
280405
.merge = flakey_merge,

0 commit comments

Comments
 (0)