13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
23407ef52SJosef Bacik /*
33407ef52SJosef Bacik * Copyright (C) 2003 Sistina Software (UK) Limited.
4a3998799SMike Snitzer * Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
53407ef52SJosef Bacik *
63407ef52SJosef Bacik * This file is released under the GPL.
73407ef52SJosef Bacik */
83407ef52SJosef Bacik
93407ef52SJosef Bacik #include <linux/device-mapper.h>
103407ef52SJosef Bacik
113407ef52SJosef Bacik #include <linux/module.h>
123407ef52SJosef Bacik #include <linux/init.h>
133407ef52SJosef Bacik #include <linux/blkdev.h>
143407ef52SJosef Bacik #include <linux/bio.h>
153407ef52SJosef Bacik #include <linux/slab.h>
163407ef52SJosef Bacik
173407ef52SJosef Bacik #define DM_MSG_PREFIX "flakey"
183407ef52SJosef Bacik
19a3998799SMike Snitzer #define PROBABILITY_BASE 1000000000
201eff9d32SJens Axboe
21a3998799SMike Snitzer #define all_corrupt_bio_flags_match(bio, fc) \
223407ef52SJosef Bacik (((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
233407ef52SJosef Bacik
243407ef52SJosef Bacik /*
253407ef52SJosef Bacik * Flakey: Used for testing only, simulates intermittent,
263407ef52SJosef Bacik * catastrophic device failure.
273407ef52SJosef Bacik */
283407ef52SJosef Bacik struct flakey_c {
293407ef52SJosef Bacik struct dm_dev *dev;
3086a3238cSHeinz Mauelshagen unsigned long start_time;
3186a3238cSHeinz Mauelshagen sector_t start;
32b26f5e3dSMike Snitzer unsigned int up_interval;
3386a3238cSHeinz Mauelshagen unsigned int down_interval;
3486a3238cSHeinz Mauelshagen unsigned long flags;
3586a3238cSHeinz Mauelshagen unsigned int corrupt_bio_byte;
36eff17e51SBart Van Assche unsigned int corrupt_bio_rw;
373407ef52SJosef Bacik unsigned int corrupt_bio_value;
383407ef52SJosef Bacik blk_opf_t corrupt_bio_flags;
39b26f5e3dSMike Snitzer unsigned int random_read_corrupt;
40*aa7d7bc9SMikulas Patocka unsigned int random_write_corrupt;
41ef548c55SMike Snitzer };
42ef548c55SMike Snitzer
43b26f5e3dSMike Snitzer enum feature_flag_bits {
44b26f5e3dSMike Snitzer ERROR_READS,
45c7cfdf59SMikulas Patocka DROP_WRITES,
46c7cfdf59SMikulas Patocka ERROR_WRITES
47c7cfdf59SMikulas Patocka };
48c7cfdf59SMikulas Patocka
49b26f5e3dSMike Snitzer struct per_bio_data {
50b26f5e3dSMike Snitzer bool bio_submitted;
51dfd068b0SMike Snitzer };
52dfd068b0SMike Snitzer
parse_features(struct dm_arg_set * as,struct flakey_c * fc,struct dm_target * ti)5386a3238cSHeinz Mauelshagen static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
54dfd068b0SMike Snitzer struct dm_target *ti)
55dfd068b0SMike Snitzer {
565916a22bSEric Biggers int r;
57*aa7d7bc9SMikulas Patocka unsigned int argc;
58a3998799SMike Snitzer const char *arg_name;
59a3998799SMike Snitzer
60a3998799SMike Snitzer static const struct dm_arg _args[] = {
61dfd068b0SMike Snitzer {0, 11, "Invalid number of feature args"},
62dfd068b0SMike Snitzer {1, UINT_MAX, "Invalid corrupt bio byte"},
63dfd068b0SMike Snitzer {0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
64dfd068b0SMike Snitzer {0, UINT_MAX, "Invalid corrupt bio flags mask"},
65dfd068b0SMike Snitzer {0, PROBABILITY_BASE, "Invalid random corrupt argument"},
66dfd068b0SMike Snitzer };
67dfd068b0SMike Snitzer
68dfd068b0SMike Snitzer /* No feature arguments supplied. */
69a3998799SMike Snitzer if (!as->argc)
70dfd068b0SMike Snitzer return 0;
71a3998799SMike Snitzer
72dfd068b0SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error);
73dfd068b0SMike Snitzer if (r)
74dfd068b0SMike Snitzer return r;
757690e253SGoldwyn Rodrigues
767690e253SGoldwyn Rodrigues while (argc) {
777690e253SGoldwyn Rodrigues arg_name = dm_shift_arg(as);
787690e253SGoldwyn Rodrigues argc--;
797690e253SGoldwyn Rodrigues
80b26f5e3dSMike Snitzer if (!arg_name) {
81*aa7d7bc9SMikulas Patocka ti->error = "Insufficient feature arguments";
82*aa7d7bc9SMikulas Patocka return -EINVAL;
83*aa7d7bc9SMikulas Patocka }
84*aa7d7bc9SMikulas Patocka
85*aa7d7bc9SMikulas Patocka /*
86*aa7d7bc9SMikulas Patocka * error_reads
87*aa7d7bc9SMikulas Patocka */
88*aa7d7bc9SMikulas Patocka if (!strcasecmp(arg_name, "error_reads")) {
89*aa7d7bc9SMikulas Patocka if (test_and_set_bit(ERROR_READS, &fc->flags)) {
90*aa7d7bc9SMikulas Patocka ti->error = "Feature error_reads duplicated";
91*aa7d7bc9SMikulas Patocka return -EINVAL;
92b26f5e3dSMike Snitzer }
93b26f5e3dSMike Snitzer continue;
94b26f5e3dSMike Snitzer }
95b26f5e3dSMike Snitzer
96b26f5e3dSMike Snitzer /*
97b26f5e3dSMike Snitzer * drop_writes
98ef548c55SMike Snitzer */
99ef548c55SMike Snitzer if (!strcasecmp(arg_name, "drop_writes")) {
100ef548c55SMike Snitzer if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
101ef548c55SMike Snitzer ti->error = "Feature drop_writes duplicated";
102ef548c55SMike Snitzer return -EINVAL;
103ef548c55SMike Snitzer } else if (test_bit(ERROR_WRITES, &fc->flags)) {
104ef548c55SMike Snitzer ti->error = "Feature drop_writes conflicts with feature error_writes";
105ef548c55SMike Snitzer return -EINVAL;
106ef548c55SMike Snitzer }
107ef548c55SMike Snitzer
108ef548c55SMike Snitzer continue;
109ef548c55SMike Snitzer }
110ef548c55SMike Snitzer
111ef548c55SMike Snitzer /*
112ef548c55SMike Snitzer * error_writes
113ef548c55SMike Snitzer */
114ef548c55SMike Snitzer if (!strcasecmp(arg_name, "error_writes")) {
115ef548c55SMike Snitzer if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
116ef548c55SMike Snitzer ti->error = "Feature error_writes duplicated";
117b26f5e3dSMike Snitzer return -EINVAL;
118b26f5e3dSMike Snitzer
119b26f5e3dSMike Snitzer } else if (test_bit(DROP_WRITES, &fc->flags)) {
120b26f5e3dSMike Snitzer ti->error = "Feature error_writes conflicts with feature drop_writes";
121b26f5e3dSMike Snitzer return -EINVAL;
122a3998799SMike Snitzer }
123a3998799SMike Snitzer
124a3998799SMike Snitzer continue;
125a3998799SMike Snitzer }
12668e58a29SMike Snitzer
127a3998799SMike Snitzer /*
12868e58a29SMike Snitzer * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
12968e58a29SMike Snitzer */
130a3998799SMike Snitzer if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
131a3998799SMike Snitzer if (!argc) {
132a3998799SMike Snitzer ti->error = "Feature corrupt_bio_byte requires parameters";
133a3998799SMike Snitzer return -EINVAL;
134a3998799SMike Snitzer }
135a3998799SMike Snitzer
136a3998799SMike Snitzer r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
137a3998799SMike Snitzer if (r)
138a3998799SMike Snitzer return r;
139a3998799SMike Snitzer argc--;
14098dba02dSMikulas Patocka
141a3998799SMike Snitzer /*
14298dba02dSMikulas Patocka * Direction r or w?
143a3998799SMike Snitzer */
144a3998799SMike Snitzer arg_name = dm_shift_arg(as);
145a3998799SMike Snitzer if (arg_name && !strcasecmp(arg_name, "w"))
146a3998799SMike Snitzer fc->corrupt_bio_rw = WRITE;
147a3998799SMike Snitzer else if (arg_name && !strcasecmp(arg_name, "r"))
148a3998799SMike Snitzer fc->corrupt_bio_rw = READ;
149a3998799SMike Snitzer else {
150a3998799SMike Snitzer ti->error = "Invalid corrupt bio direction (r or w)";
151a3998799SMike Snitzer return -EINVAL;
152a3998799SMike Snitzer }
153a3998799SMike Snitzer argc--;
154a3998799SMike Snitzer
155a3998799SMike Snitzer /*
156a3998799SMike Snitzer * Value of byte (0-255) to write in place of correct one.
157a3998799SMike Snitzer */
158a3998799SMike Snitzer r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
159a3998799SMike Snitzer if (r)
160a3998799SMike Snitzer return r;
161eff17e51SBart Van Assche argc--;
162eff17e51SBart Van Assche
163eff17e51SBart Van Assche /*
16486a3238cSHeinz Mauelshagen * Only corrupt bios with these flags set.
165eff17e51SBart Van Assche */
166a3998799SMike Snitzer BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
167a3998799SMike Snitzer sizeof(unsigned int));
168a3998799SMike Snitzer r = dm_read_arg(_args + 3, as,
169a3998799SMike Snitzer (__force unsigned int *)&fc->corrupt_bio_flags,
170a3998799SMike Snitzer &ti->error);
171dfd068b0SMike Snitzer if (r)
172dfd068b0SMike Snitzer return r;
173a3998799SMike Snitzer argc--;
174a3998799SMike Snitzer
175a3998799SMike Snitzer continue;
176a3998799SMike Snitzer }
177a3998799SMike Snitzer
178a3998799SMike Snitzer if (!strcasecmp(arg_name, "random_read_corrupt")) {
179a3998799SMike Snitzer if (!argc) {
180ef548c55SMike Snitzer ti->error = "Feature random_read_corrupt requires a parameter";
181ef548c55SMike Snitzer return -EINVAL;
182ef548c55SMike Snitzer }
183ef548c55SMike Snitzer r = dm_read_arg(_args + 4, as, &fc->random_read_corrupt, &ti->error);
184a3998799SMike Snitzer if (r)
185a3998799SMike Snitzer return r;
186*aa7d7bc9SMikulas Patocka argc--;
187*aa7d7bc9SMikulas Patocka
188*aa7d7bc9SMikulas Patocka continue;
189*aa7d7bc9SMikulas Patocka }
190*aa7d7bc9SMikulas Patocka
191*aa7d7bc9SMikulas Patocka if (!strcasecmp(arg_name, "random_write_corrupt")) {
192a3998799SMike Snitzer if (!argc) {
193dfd068b0SMike Snitzer ti->error = "Feature random_write_corrupt requires a parameter";
194dfd068b0SMike Snitzer return -EINVAL;
1953407ef52SJosef Bacik }
196dfd068b0SMike Snitzer r = dm_read_arg(_args + 4, as, &fc->random_write_corrupt, &ti->error);
197dfd068b0SMike Snitzer if (r)
198b26f5e3dSMike Snitzer return r;
199b26f5e3dSMike Snitzer argc--;
200b26f5e3dSMike Snitzer
201a3998799SMike Snitzer continue;
202a3998799SMike Snitzer }
203a3998799SMike Snitzer
204a3998799SMike Snitzer ti->error = "Unrecognised flakey feature requested";
205a3998799SMike Snitzer return -EINVAL;
2063407ef52SJosef Bacik }
2073407ef52SJosef Bacik
2083407ef52SJosef Bacik if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
2095916a22bSEric Biggers ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
210dfd068b0SMike Snitzer return -EINVAL;
211dfd068b0SMike Snitzer
212dfd068b0SMike Snitzer } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
2133407ef52SJosef Bacik ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
214dfd068b0SMike Snitzer return -EINVAL;
215dfd068b0SMike Snitzer }
216dfd068b0SMike Snitzer
217dfd068b0SMike Snitzer if (!fc->corrupt_bio_byte && !test_bit(ERROR_READS, &fc->flags) &&
218dfd068b0SMike Snitzer !test_bit(DROP_WRITES, &fc->flags) && !test_bit(ERROR_WRITES, &fc->flags) &&
21931998ef1SMikulas Patocka !fc->random_read_corrupt && !fc->random_write_corrupt) {
220dfd068b0SMike Snitzer set_bit(ERROR_WRITES, &fc->flags);
221dfd068b0SMike Snitzer set_bit(ERROR_READS, &fc->flags);
222dfd068b0SMike Snitzer }
223dfd068b0SMike Snitzer
224dfd068b0SMike Snitzer return 0;
225dfd068b0SMike Snitzer }
2263407ef52SJosef Bacik
2273407ef52SJosef Bacik /*
2283407ef52SJosef Bacik * Construct a flakey mapping:
229b26f5e3dSMike Snitzer * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
2303407ef52SJosef Bacik *
23175e3a0f5SAlasdair G Kergon * Feature args:
2323407ef52SJosef Bacik * [drop_writes]
2333407ef52SJosef Bacik * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
2343407ef52SJosef Bacik *
2353407ef52SJosef Bacik * Nth_byte starts from 1 for the first byte.
236dfd068b0SMike Snitzer * Direction is r for READ or w for WRITE.
2373407ef52SJosef Bacik * bio_flags is ignored if 0.
238e80d1c80SVivek Goyal */
flakey_ctr(struct dm_target * ti,unsigned int argc,char ** argv)239ef87bfc2SMilan Broz static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
240dfd068b0SMike Snitzer {
2413407ef52SJosef Bacik static const struct dm_arg _args[] = {
2423407ef52SJosef Bacik {0, UINT_MAX, "Invalid up interval"},
243dfd068b0SMike Snitzer {0, UINT_MAX, "Invalid down interval"},
2443407ef52SJosef Bacik };
245dfd068b0SMike Snitzer
246dfd068b0SMike Snitzer int r;
2473407ef52SJosef Bacik struct flakey_c *fc;
248dfd068b0SMike Snitzer unsigned long long tmpll;
249dfd068b0SMike Snitzer struct dm_arg_set as;
250dfd068b0SMike Snitzer const char *devname;
251dfd068b0SMike Snitzer char dummy;
2523407ef52SJosef Bacik
2533407ef52SJosef Bacik as.argc = argc;
254dfd068b0SMike Snitzer as.argv = argv;
255bff7e067SWei Yongjun
2563407ef52SJosef Bacik if (argc < 4) {
2573407ef52SJosef Bacik ti->error = "Invalid argument count";
2583407ef52SJosef Bacik return -EINVAL;
2593407ef52SJosef Bacik }
260dfd068b0SMike Snitzer
261bff7e067SWei Yongjun fc = kzalloc(sizeof(*fc), GFP_KERNEL);
2623407ef52SJosef Bacik if (!fc) {
2633407ef52SJosef Bacik ti->error = "Cannot allocate context";
2643407ef52SJosef Bacik return -ENOMEM;
265b26f5e3dSMike Snitzer }
266dfd068b0SMike Snitzer fc->start_time = jiffies;
267dfd068b0SMike Snitzer
268dfd068b0SMike Snitzer devname = dm_shift_arg(&as);
269e80d1c80SVivek Goyal
270e80d1c80SVivek Goyal r = -EINVAL;
271dfd068b0SMike Snitzer if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
2723407ef52SJosef Bacik ti->error = "Invalid device sector";
2733407ef52SJosef Bacik goto bad;
2743407ef52SJosef Bacik }
27555a62eefSAlasdair G Kergon fc->start = tmpll;
27655a62eefSAlasdair G Kergon
27730187e1dSMike Snitzer r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
2783407ef52SJosef Bacik if (r)
2793407ef52SJosef Bacik goto bad;
2803407ef52SJosef Bacik
2813407ef52SJosef Bacik r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
2823407ef52SJosef Bacik if (r)
283e80d1c80SVivek Goyal goto bad;
2843407ef52SJosef Bacik
2853407ef52SJosef Bacik if (!(fc->up_interval + fc->down_interval)) {
2863407ef52SJosef Bacik ti->error = "Total (up + down) interval is zero";
2873407ef52SJosef Bacik r = -EINVAL;
2883407ef52SJosef Bacik goto bad;
2893407ef52SJosef Bacik }
2903407ef52SJosef Bacik
2913407ef52SJosef Bacik if (fc->up_interval + fc->down_interval < fc->up_interval) {
2923407ef52SJosef Bacik ti->error = "Interval overflow";
2933407ef52SJosef Bacik r = -EINVAL;
2943407ef52SJosef Bacik goto bad;
2953407ef52SJosef Bacik }
2963407ef52SJosef Bacik
2973407ef52SJosef Bacik r = parse_features(&as, fc, ti);
29830e4171bSMike Snitzer if (r)
2993407ef52SJosef Bacik goto bad;
3003407ef52SJosef Bacik
3013407ef52SJosef Bacik r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
3023407ef52SJosef Bacik if (r) {
3033407ef52SJosef Bacik ti->error = "Device lookup failed";
3043407ef52SJosef Bacik goto bad;
30574d46992SChristoph Hellwig }
306e86f2b00SMike Snitzer
3073407ef52SJosef Bacik ti->num_flush_bios = 1;
3083407ef52SJosef Bacik ti->num_discard_bios = 1;
309a3998799SMike Snitzer ti->per_io_data_size = sizeof(struct per_bio_data);
310a3998799SMike Snitzer ti->private = fc;
311a00f5276SSweet Tea return 0;
312a00f5276SSweet Tea
313a00f5276SSweet Tea bad:
314a00f5276SSweet Tea kfree(fc);
315a00f5276SSweet Tea return r;
316a00f5276SSweet Tea }
317a00f5276SSweet Tea
flakey_dtr(struct dm_target * ti)318a3998799SMike Snitzer static void flakey_dtr(struct dm_target *ti)
319a3998799SMike Snitzer {
320a00f5276SSweet Tea struct flakey_c *fc = ti->private;
321a00f5276SSweet Tea
322a3998799SMike Snitzer dm_put_device(ti, fc->dev);
323a00f5276SSweet Tea kfree(fc);
324a00f5276SSweet Tea }
325f50714b5SMikulas Patocka
flakey_map_sector(struct dm_target * ti,sector_t bi_sector)326f50714b5SMikulas Patocka static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
327f50714b5SMikulas Patocka {
328f50714b5SMikulas Patocka struct flakey_c *fc = ti->private;
3298eb29c4fSMikulas Patocka
330a00f5276SSweet Tea return fc->start + dm_target_offset(ti, bi_sector);
3318eb29c4fSMikulas Patocka }
332a3998799SMike Snitzer
flakey_map_bio(struct dm_target * ti,struct bio * bio)333a00f5276SSweet Tea static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
334a3998799SMike Snitzer {
3351eff9d32SJens Axboe struct flakey_c *fc = ti->private;
336a00f5276SSweet Tea
337a00f5276SSweet Tea bio_set_dev(bio, fc->dev->bdev);
338a00f5276SSweet Tea bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
339a00f5276SSweet Tea }
340a3998799SMike Snitzer
corrupt_bio_common(struct bio * bio,unsigned int corrupt_bio_byte,unsigned char corrupt_bio_value)341a3998799SMike Snitzer static void corrupt_bio_common(struct bio *bio, unsigned int corrupt_bio_byte,
342a3998799SMike Snitzer unsigned char corrupt_bio_value)
3437de3ee57SMikulas Patocka {
3443407ef52SJosef Bacik struct bvec_iter iter;
3453407ef52SJosef Bacik struct bio_vec bvec;
34686a3238cSHeinz Mauelshagen
347c7cfdf59SMikulas Patocka /*
3480ef0b471SHeinz Mauelshagen * Overwrite the Nth byte of the bio's data, on whichever page
349c7cfdf59SMikulas Patocka * it falls.
3503407ef52SJosef Bacik */
3512e2d6f7eSAjay Joshi bio_for_each_segment(bvec, bio, iter) {
352124c4454SDamien Le Moal if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
353124c4454SDamien Le Moal unsigned char *segment = bvec_kmap_local(&bvec);
3543407ef52SJosef Bacik segment[corrupt_bio_byte] = corrupt_bio_value;
3553407ef52SJosef Bacik kunmap_local(segment);
356b26f5e3dSMike Snitzer DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
357a3998799SMike Snitzer "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
358a3998799SMike Snitzer bio, corrupt_bio_value, corrupt_bio_byte,
359a3998799SMike Snitzer (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
360c7cfdf59SMikulas Patocka (unsigned long long)bio->bi_iter.bi_sector,
3613407ef52SJosef Bacik bio->bi_iter.bi_size);
362b26f5e3dSMike Snitzer break;
363ef548c55SMike Snitzer }
364299f6230SMike Snitzer corrupt_bio_byte -= bio_iter_len(bio, iter);
365a3998799SMike Snitzer }
36699f3c90dSMike Snitzer }
367*aa7d7bc9SMikulas Patocka
corrupt_bio_data(struct bio * bio,struct flakey_c * fc)368846785e6SChristoph Hellwig static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
369299f6230SMike Snitzer {
37099f3c90dSMike Snitzer unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
371a3998799SMike Snitzer
372a3998799SMike Snitzer if (!bio_has_data(bio))
373ef548c55SMike Snitzer return;
374b26f5e3dSMike Snitzer
375b26f5e3dSMike Snitzer corrupt_bio_common(bio, corrupt_bio_byte, fc->corrupt_bio_value);
3764246a0b6SChristoph Hellwig }
377b26f5e3dSMike Snitzer
corrupt_bio_random(struct bio * bio)37803b18887SHeinz Mauelshagen static void corrupt_bio_random(struct bio *bio)
379ef548c55SMike Snitzer {
380ef548c55SMike Snitzer unsigned int corrupt_byte;
381ef548c55SMike Snitzer unsigned char corrupt_value;
382a3998799SMike Snitzer
383a3998799SMike Snitzer if (!bio_has_data(bio))
384a3998799SMike Snitzer return;
385a3998799SMike Snitzer
386aa56b9b7SMikulas Patocka corrupt_byte = get_random_u32() % bio->bi_iter.bi_size;
387aa56b9b7SMikulas Patocka corrupt_value = get_random_u8();
388a3998799SMike Snitzer
389a3998799SMike Snitzer corrupt_bio_common(bio, corrupt_byte, corrupt_value);
390aa56b9b7SMikulas Patocka }
391b26f5e3dSMike Snitzer
clone_free(struct bio * clone)392b26f5e3dSMike Snitzer static void clone_free(struct bio *clone)
393b26f5e3dSMike Snitzer {
394b26f5e3dSMike Snitzer struct folio_iter fi;
395b26f5e3dSMike Snitzer
3963407ef52SJosef Bacik if (clone->bi_vcnt > 0) { /* bio_for_each_folio_all crashes with an empty bio */
3973407ef52SJosef Bacik bio_for_each_folio_all(fi, clone)
3983407ef52SJosef Bacik folio_put(fi.folio);
3993407ef52SJosef Bacik }
4003407ef52SJosef Bacik
4014e4cbee9SChristoph Hellwig bio_uninit(clone);
4024e4cbee9SChristoph Hellwig kfree(clone);
403a3998799SMike Snitzer }
404a3998799SMike Snitzer
clone_endio(struct bio * clone)405c7cfdf59SMikulas Patocka static void clone_endio(struct bio *clone)
406a3998799SMike Snitzer {
4072e2d6f7eSAjay Joshi struct bio *bio = clone->bi_private;
408124c4454SDamien Le Moal bio->bi_status = clone->bi_status;
409124c4454SDamien Le Moal clone_free(clone);
4101be56909SChristoph Hellwig bio_endio(bio);
411aa56b9b7SMikulas Patocka }
412aa56b9b7SMikulas Patocka
clone_bio(struct dm_target * ti,struct flakey_c * fc,struct bio * bio)413299f6230SMike Snitzer static struct bio *clone_bio(struct dm_target *ti, struct flakey_c *fc, struct bio *bio)
414299f6230SMike Snitzer {
415299f6230SMike Snitzer struct bio *clone;
416299f6230SMike Snitzer unsigned size, remaining_size, nr_iovecs, order;
417a3998799SMike Snitzer struct bvec_iter iter = bio->bi_iter;
418aa56b9b7SMikulas Patocka
419*aa7d7bc9SMikulas Patocka if (unlikely(bio->bi_iter.bi_size > UIO_MAXIOV << PAGE_SHIFT))
420*aa7d7bc9SMikulas Patocka dm_accept_partial_bio(bio, UIO_MAXIOV << PAGE_SHIFT >> SECTOR_SHIFT);
421299f6230SMike Snitzer
422299f6230SMike Snitzer size = bio->bi_iter.bi_size;
423ef548c55SMike Snitzer nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
424299f6230SMike Snitzer
4254e4cbee9SChristoph Hellwig clone = bio_kmalloc(nr_iovecs, GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
42699f3c90dSMike Snitzer if (!clone)
427299f6230SMike Snitzer return NULL;
428a3998799SMike Snitzer
4291be56909SChristoph Hellwig bio_init(clone, fc->dev->bdev, bio->bi_inline_vecs, nr_iovecs, bio->bi_opf);
430a3998799SMike Snitzer
431a3998799SMike Snitzer clone->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
432fd7c092eSMikulas Patocka clone->bi_private = bio;
43386a3238cSHeinz Mauelshagen clone->bi_end_io = clone_endio;
4343407ef52SJosef Bacik
43586a3238cSHeinz Mauelshagen remaining_size = size;
4363407ef52SJosef Bacik
437*aa7d7bc9SMikulas Patocka order = MAX_ORDER - 1;
4383407ef52SJosef Bacik while (remaining_size) {
4393407ef52SJosef Bacik struct page *pages;
4403407ef52SJosef Bacik unsigned size_to_add, to_copy;
4413407ef52SJosef Bacik unsigned char *virt;
4423407ef52SJosef Bacik unsigned remaining_order = __fls((remaining_size + PAGE_SIZE - 1) >> PAGE_SHIFT);
4433407ef52SJosef Bacik order = min(order, remaining_order);
4443407ef52SJosef Bacik
445b26f5e3dSMike Snitzer retry_alloc_pages:
4463407ef52SJosef Bacik pages = alloc_pages(GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN | __GFP_COMP, order);
4473407ef52SJosef Bacik if (unlikely(!pages)) {
448b26f5e3dSMike Snitzer if (order) {
449*aa7d7bc9SMikulas Patocka order--;
450b26f5e3dSMike Snitzer goto retry_alloc_pages;
451ef548c55SMike Snitzer }
452*aa7d7bc9SMikulas Patocka clone_free(clone);
453a3998799SMike Snitzer return NULL;
454*aa7d7bc9SMikulas Patocka }
455*aa7d7bc9SMikulas Patocka size_to_add = min((unsigned)PAGE_SIZE << order, remaining_size);
456b26f5e3dSMike Snitzer
457b26f5e3dSMike Snitzer virt = page_to_virt(pages);
458ef548c55SMike Snitzer to_copy = size_to_add;
459ef548c55SMike Snitzer do {
460a3998799SMike Snitzer struct bio_vec bvec = bvec_iter_bvec(bio->bi_io_vec, iter);
461a3998799SMike Snitzer unsigned this_step = min(bvec.bv_len, to_copy);
462a3998799SMike Snitzer void *map = bvec_kmap_local(&bvec);
463a3998799SMike Snitzer memcpy(virt, map, this_step);
464a3998799SMike Snitzer kunmap_local(map);
465a3998799SMike Snitzer
466a3998799SMike Snitzer bvec_iter_advance(bio->bi_io_vec, &iter, this_step);
4673407ef52SJosef Bacik to_copy -= this_step;
4688ec45662STushar Sugandhi virt += this_step;
4698ec45662STushar Sugandhi } while (to_copy);
4708ec45662STushar Sugandhi
4718ec45662STushar Sugandhi __bio_add_page(clone, pages, size_to_add, 0);
4723407ef52SJosef Bacik remaining_size -= size_to_add;
4733407ef52SJosef Bacik }
4743407ef52SJosef Bacik
4755bd5e8d8SMike Snitzer return clone;
4763407ef52SJosef Bacik }
4773407ef52SJosef Bacik
flakey_map(struct dm_target * ti,struct bio * bio)478e56f81e0SChristoph Hellwig static int flakey_map(struct dm_target *ti, struct bio *bio)
479e56f81e0SChristoph Hellwig {
4803407ef52SJosef Bacik struct flakey_c *fc = ti->private;
481ec8013beSPaolo Bonzini unsigned int elapsed;
482ec8013beSPaolo Bonzini struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
483ec8013beSPaolo Bonzini
4846dcbb52cSChristoph Hellwig pb->bio_submitted = false;
485e56f81e0SChristoph Hellwig
486e56f81e0SChristoph Hellwig if (op_is_zone_mgmt(bio_op(bio)))
4873407ef52SJosef Bacik goto map_bio;
4883407ef52SJosef Bacik
489e76239a3SChristoph Hellwig /* Are we alive ? */
490d4100351SChristoph Hellwig elapsed = (jiffies - fc->start_time) / HZ;
491d4100351SChristoph Hellwig if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
492e76239a3SChristoph Hellwig bool corrupt_fixed, corrupt_random;
493e76239a3SChristoph Hellwig /*
494e76239a3SChristoph Hellwig * Flag this bio as submitted while down.
495912e8875SDamien Le Moal */
496912e8875SDamien Le Moal pb->bio_submitted = true;
497912e8875SDamien Le Moal
498e76239a3SChristoph Hellwig /*
499e3290b94SMike Snitzer * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
500e3290b94SMike Snitzer * Otherwise, flakey_end_io() will decide if the reads should be modified.
501e76239a3SChristoph Hellwig */
502e76239a3SChristoph Hellwig if (bio_data_dir(bio) == READ) {
5033407ef52SJosef Bacik if (test_bit(ERROR_READS, &fc->flags))
5043407ef52SJosef Bacik return DM_MAPIO_KILL;
5053407ef52SJosef Bacik goto map_bio;
5063407ef52SJosef Bacik }
5073407ef52SJosef Bacik
5083407ef52SJosef Bacik /*
5093407ef52SJosef Bacik * Drop or error writes?
5103407ef52SJosef Bacik */
5113407ef52SJosef Bacik if (test_bit(DROP_WRITES, &fc->flags)) {
512124c4454SDamien Le Moal bio_endio(bio);
5133db564b4SSatya Tangirala return DM_MAPIO_SUBMITTED;
514e76239a3SChristoph Hellwig } else if (test_bit(ERROR_WRITES, &fc->flags)) {
5153407ef52SJosef Bacik bio_io_error(bio);
5163407ef52SJosef Bacik return DM_MAPIO_SUBMITTED;
5173407ef52SJosef Bacik }
5183407ef52SJosef Bacik
519a3998799SMike Snitzer /*
5203407ef52SJosef Bacik * Corrupt matching writes.
521e56f81e0SChristoph Hellwig */
5223407ef52SJosef Bacik corrupt_fixed = false;
5233407ef52SJosef Bacik corrupt_random = false;
5243664ff82SYangtao Li if (fc->corrupt_bio_byte && fc->corrupt_bio_rw == WRITE) {
5253407ef52SJosef Bacik if (all_corrupt_bio_flags_match(bio, fc))
5263407ef52SJosef Bacik corrupt_fixed = true;
5273407ef52SJosef Bacik }
5283407ef52SJosef Bacik if (fc->random_write_corrupt) {
529 u64 rnd = get_random_u64();
530 u32 rem = do_div(rnd, PROBABILITY_BASE);
531 if (rem < fc->random_write_corrupt)
532 corrupt_random = true;
533 }
534 if (corrupt_fixed || corrupt_random) {
535 struct bio *clone = clone_bio(ti, fc, bio);
536 if (clone) {
537 if (corrupt_fixed)
538 corrupt_bio_data(clone, fc);
539 if (corrupt_random)
540 corrupt_bio_random(clone);
541 submit_bio(clone);
542 return DM_MAPIO_SUBMITTED;
543 }
544 }
545 }
546
547 map_bio:
548 flakey_map_bio(ti, bio);
549
550 return DM_MAPIO_REMAPPED;
551 }
552
flakey_end_io(struct dm_target * ti,struct bio * bio,blk_status_t * error)553 static int flakey_end_io(struct dm_target *ti, struct bio *bio,
554 blk_status_t *error)
555 {
556 struct flakey_c *fc = ti->private;
557 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
558
559 if (op_is_zone_mgmt(bio_op(bio)))
560 return DM_ENDIO_DONE;
561
562 if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
563 if (fc->corrupt_bio_byte) {
564 if ((fc->corrupt_bio_rw == READ) &&
565 all_corrupt_bio_flags_match(bio, fc)) {
566 /*
567 * Corrupt successful matching READs while in down state.
568 */
569 corrupt_bio_data(bio, fc);
570 }
571 }
572 if (fc->random_read_corrupt) {
573 u64 rnd = get_random_u64();
574 u32 rem = do_div(rnd, PROBABILITY_BASE);
575 if (rem < fc->random_read_corrupt)
576 corrupt_bio_random(bio);
577 }
578 if (test_bit(ERROR_READS, &fc->flags)) {
579 /*
580 * Error read during the down_interval if drop_writes
581 * and error_writes were not configured.
582 */
583 *error = BLK_STS_IOERR;
584 }
585 }
586
587 return DM_ENDIO_DONE;
588 }
589
flakey_status(struct dm_target * ti,status_type_t type,unsigned int status_flags,char * result,unsigned int maxlen)590 static void flakey_status(struct dm_target *ti, status_type_t type,
591 unsigned int status_flags, char *result, unsigned int maxlen)
592 {
593 unsigned int sz = 0;
594 struct flakey_c *fc = ti->private;
595 unsigned int error_reads, drop_writes, error_writes;
596
597 switch (type) {
598 case STATUSTYPE_INFO:
599 result[0] = '\0';
600 break;
601
602 case STATUSTYPE_TABLE:
603 DMEMIT("%s %llu %u %u", fc->dev->name,
604 (unsigned long long)fc->start, fc->up_interval,
605 fc->down_interval);
606
607 error_reads = test_bit(ERROR_READS, &fc->flags);
608 drop_writes = test_bit(DROP_WRITES, &fc->flags);
609 error_writes = test_bit(ERROR_WRITES, &fc->flags);
610 DMEMIT(" %u", error_reads + drop_writes + error_writes +
611 (fc->corrupt_bio_byte > 0) * 5 +
612 (fc->random_read_corrupt > 0) * 2 +
613 (fc->random_write_corrupt > 0) * 2);
614
615 if (error_reads)
616 DMEMIT(" error_reads");
617 if (drop_writes)
618 DMEMIT(" drop_writes");
619 else if (error_writes)
620 DMEMIT(" error_writes");
621
622 if (fc->corrupt_bio_byte)
623 DMEMIT(" corrupt_bio_byte %u %c %u %u",
624 fc->corrupt_bio_byte,
625 (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
626 fc->corrupt_bio_value, fc->corrupt_bio_flags);
627
628 if (fc->random_read_corrupt > 0)
629 DMEMIT(" random_read_corrupt %u", fc->random_read_corrupt);
630 if (fc->random_write_corrupt > 0)
631 DMEMIT(" random_write_corrupt %u", fc->random_write_corrupt);
632
633 break;
634
635 case STATUSTYPE_IMA:
636 result[0] = '\0';
637 break;
638 }
639 }
640
flakey_prepare_ioctl(struct dm_target * ti,struct block_device ** bdev)641 static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
642 {
643 struct flakey_c *fc = ti->private;
644
645 *bdev = fc->dev->bdev;
646
647 /*
648 * Only pass ioctls through if the device sizes match exactly.
649 */
650 if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
651 return 1;
652 return 0;
653 }
654
655 #ifdef CONFIG_BLK_DEV_ZONED
flakey_report_zones(struct dm_target * ti,struct dm_report_zones_args * args,unsigned int nr_zones)656 static int flakey_report_zones(struct dm_target *ti,
657 struct dm_report_zones_args *args, unsigned int nr_zones)
658 {
659 struct flakey_c *fc = ti->private;
660
661 return dm_report_zones(fc->dev->bdev, fc->start,
662 flakey_map_sector(ti, args->next_sector),
663 args, nr_zones);
664 }
665 #else
666 #define flakey_report_zones NULL
667 #endif
668
flakey_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)669 static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
670 {
671 struct flakey_c *fc = ti->private;
672
673 return fn(ti, fc->dev, fc->start, ti->len, data);
674 }
675
676 static struct target_type flakey_target = {
677 .name = "flakey",
678 .version = {1, 5, 0},
679 .features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
680 .report_zones = flakey_report_zones,
681 .module = THIS_MODULE,
682 .ctr = flakey_ctr,
683 .dtr = flakey_dtr,
684 .map = flakey_map,
685 .end_io = flakey_end_io,
686 .status = flakey_status,
687 .prepare_ioctl = flakey_prepare_ioctl,
688 .iterate_devices = flakey_iterate_devices,
689 };
690 module_dm(flakey);
691
692 MODULE_DESCRIPTION(DM_NAME " flakey target");
693 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
694 MODULE_LICENSE("GPL");
695