xref: /openbmc/linux/drivers/md/dm-flakey.c (revision aa7d7bc99fed71664e9a241b32294ee15a88d938)
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 all_corrupt_bio_flags_match(bio, fc)	\
201eff9d32SJens Axboe 	(((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
21a3998799SMike Snitzer 
223407ef52SJosef Bacik /*
233407ef52SJosef Bacik  * Flakey: Used for testing only, simulates intermittent,
243407ef52SJosef Bacik  * catastrophic device failure.
253407ef52SJosef Bacik  */
263407ef52SJosef Bacik struct flakey_c {
273407ef52SJosef Bacik 	struct dm_dev *dev;
283407ef52SJosef Bacik 	unsigned long start_time;
293407ef52SJosef Bacik 	sector_t start;
3086a3238cSHeinz Mauelshagen 	unsigned int up_interval;
3186a3238cSHeinz Mauelshagen 	unsigned int down_interval;
32b26f5e3dSMike Snitzer 	unsigned long flags;
3386a3238cSHeinz Mauelshagen 	unsigned int corrupt_bio_byte;
3486a3238cSHeinz Mauelshagen 	unsigned int corrupt_bio_rw;
3586a3238cSHeinz Mauelshagen 	unsigned int corrupt_bio_value;
36eff17e51SBart Van Assche 	blk_opf_t corrupt_bio_flags;
373407ef52SJosef Bacik };
383407ef52SJosef Bacik 
39b26f5e3dSMike Snitzer enum feature_flag_bits {
40*aa7d7bc9SMikulas Patocka 	ERROR_READS,
41ef548c55SMike Snitzer 	DROP_WRITES,
42ef548c55SMike Snitzer 	ERROR_WRITES
43b26f5e3dSMike Snitzer };
44b26f5e3dSMike Snitzer 
45c7cfdf59SMikulas Patocka struct per_bio_data {
46c7cfdf59SMikulas Patocka 	bool bio_submitted;
47c7cfdf59SMikulas Patocka };
48c7cfdf59SMikulas Patocka 
49b26f5e3dSMike Snitzer static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
50b26f5e3dSMike Snitzer 			  struct dm_target *ti)
51dfd068b0SMike Snitzer {
52dfd068b0SMike Snitzer 	int r;
5386a3238cSHeinz Mauelshagen 	unsigned int argc;
54dfd068b0SMike Snitzer 	const char *arg_name;
55dfd068b0SMike Snitzer 
565916a22bSEric Biggers 	static const struct dm_arg _args[] = {
57*aa7d7bc9SMikulas Patocka 		{0, 7, "Invalid number of feature args"},
58a3998799SMike Snitzer 		{1, UINT_MAX, "Invalid corrupt bio byte"},
59a3998799SMike Snitzer 		{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
60a3998799SMike Snitzer 		{0, UINT_MAX, "Invalid corrupt bio flags mask"},
61dfd068b0SMike Snitzer 	};
62dfd068b0SMike Snitzer 
63dfd068b0SMike Snitzer 	/* No feature arguments supplied. */
64dfd068b0SMike Snitzer 	if (!as->argc)
65dfd068b0SMike Snitzer 		return 0;
66dfd068b0SMike Snitzer 
67dfd068b0SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
68dfd068b0SMike Snitzer 	if (r)
69a3998799SMike Snitzer 		return r;
70dfd068b0SMike Snitzer 
71a3998799SMike Snitzer 	while (argc) {
72dfd068b0SMike Snitzer 		arg_name = dm_shift_arg(as);
73dfd068b0SMike Snitzer 		argc--;
74dfd068b0SMike Snitzer 
757690e253SGoldwyn Rodrigues 		if (!arg_name) {
767690e253SGoldwyn Rodrigues 			ti->error = "Insufficient feature arguments";
777690e253SGoldwyn Rodrigues 			return -EINVAL;
787690e253SGoldwyn Rodrigues 		}
797690e253SGoldwyn Rodrigues 
80b26f5e3dSMike Snitzer 		/*
81*aa7d7bc9SMikulas Patocka 		 * error_reads
82*aa7d7bc9SMikulas Patocka 		 */
83*aa7d7bc9SMikulas Patocka 		if (!strcasecmp(arg_name, "error_reads")) {
84*aa7d7bc9SMikulas Patocka 			if (test_and_set_bit(ERROR_READS, &fc->flags)) {
85*aa7d7bc9SMikulas Patocka 				ti->error = "Feature error_reads duplicated";
86*aa7d7bc9SMikulas Patocka 				return -EINVAL;
87*aa7d7bc9SMikulas Patocka 			}
88*aa7d7bc9SMikulas Patocka 			continue;
89*aa7d7bc9SMikulas Patocka 		}
90*aa7d7bc9SMikulas Patocka 
91*aa7d7bc9SMikulas Patocka 		/*
92b26f5e3dSMike Snitzer 		 * drop_writes
93b26f5e3dSMike Snitzer 		 */
94b26f5e3dSMike Snitzer 		if (!strcasecmp(arg_name, "drop_writes")) {
95b26f5e3dSMike Snitzer 			if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
96b26f5e3dSMike Snitzer 				ti->error = "Feature drop_writes duplicated";
97b26f5e3dSMike Snitzer 				return -EINVAL;
98ef548c55SMike Snitzer 			} else if (test_bit(ERROR_WRITES, &fc->flags)) {
99ef548c55SMike Snitzer 				ti->error = "Feature drop_writes conflicts with feature error_writes";
100ef548c55SMike Snitzer 				return -EINVAL;
101ef548c55SMike Snitzer 			}
102ef548c55SMike Snitzer 
103ef548c55SMike Snitzer 			continue;
104ef548c55SMike Snitzer 		}
105ef548c55SMike Snitzer 
106ef548c55SMike Snitzer 		/*
107ef548c55SMike Snitzer 		 * error_writes
108ef548c55SMike Snitzer 		 */
109ef548c55SMike Snitzer 		if (!strcasecmp(arg_name, "error_writes")) {
110ef548c55SMike Snitzer 			if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
111ef548c55SMike Snitzer 				ti->error = "Feature error_writes duplicated";
112ef548c55SMike Snitzer 				return -EINVAL;
113ef548c55SMike Snitzer 
114ef548c55SMike Snitzer 			} else if (test_bit(DROP_WRITES, &fc->flags)) {
115ef548c55SMike Snitzer 				ti->error = "Feature error_writes conflicts with feature drop_writes";
116ef548c55SMike Snitzer 				return -EINVAL;
117b26f5e3dSMike Snitzer 			}
118b26f5e3dSMike Snitzer 
119b26f5e3dSMike Snitzer 			continue;
120b26f5e3dSMike Snitzer 		}
121b26f5e3dSMike Snitzer 
122a3998799SMike Snitzer 		/*
123a3998799SMike Snitzer 		 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
124a3998799SMike Snitzer 		 */
125a3998799SMike Snitzer 		if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
12668e58a29SMike Snitzer 			if (!argc) {
127a3998799SMike Snitzer 				ti->error = "Feature corrupt_bio_byte requires parameters";
12868e58a29SMike Snitzer 				return -EINVAL;
12968e58a29SMike Snitzer 			}
130a3998799SMike Snitzer 
131a3998799SMike Snitzer 			r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
132a3998799SMike Snitzer 			if (r)
133a3998799SMike Snitzer 				return r;
134a3998799SMike Snitzer 			argc--;
135a3998799SMike Snitzer 
136a3998799SMike Snitzer 			/*
137a3998799SMike Snitzer 			 * Direction r or w?
138a3998799SMike Snitzer 			 */
139a3998799SMike Snitzer 			arg_name = dm_shift_arg(as);
14098dba02dSMikulas Patocka 			if (arg_name && !strcasecmp(arg_name, "w"))
141a3998799SMike Snitzer 				fc->corrupt_bio_rw = WRITE;
14298dba02dSMikulas Patocka 			else if (arg_name && !strcasecmp(arg_name, "r"))
143a3998799SMike Snitzer 				fc->corrupt_bio_rw = READ;
144a3998799SMike Snitzer 			else {
145a3998799SMike Snitzer 				ti->error = "Invalid corrupt bio direction (r or w)";
146a3998799SMike Snitzer 				return -EINVAL;
147a3998799SMike Snitzer 			}
148a3998799SMike Snitzer 			argc--;
149a3998799SMike Snitzer 
150a3998799SMike Snitzer 			/*
151a3998799SMike Snitzer 			 * Value of byte (0-255) to write in place of correct one.
152a3998799SMike Snitzer 			 */
153a3998799SMike Snitzer 			r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
154a3998799SMike Snitzer 			if (r)
155a3998799SMike Snitzer 				return r;
156a3998799SMike Snitzer 			argc--;
157a3998799SMike Snitzer 
158a3998799SMike Snitzer 			/*
159a3998799SMike Snitzer 			 * Only corrupt bios with these flags set.
160a3998799SMike Snitzer 			 */
161eff17e51SBart Van Assche 			BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
162eff17e51SBart Van Assche 				     sizeof(unsigned int));
163eff17e51SBart Van Assche 			r = dm_read_arg(_args + 3, as,
16486a3238cSHeinz Mauelshagen 				(__force unsigned int *)&fc->corrupt_bio_flags,
165eff17e51SBart Van Assche 				&ti->error);
166a3998799SMike Snitzer 			if (r)
167a3998799SMike Snitzer 				return r;
168a3998799SMike Snitzer 			argc--;
169a3998799SMike Snitzer 
170a3998799SMike Snitzer 			continue;
171dfd068b0SMike Snitzer 		}
172dfd068b0SMike Snitzer 
173a3998799SMike Snitzer 		ti->error = "Unrecognised flakey feature requested";
174a3998799SMike Snitzer 		return -EINVAL;
175a3998799SMike Snitzer 	}
176a3998799SMike Snitzer 
177a3998799SMike Snitzer 	if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
178a3998799SMike Snitzer 		ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
179a3998799SMike Snitzer 		return -EINVAL;
180ef548c55SMike Snitzer 
181ef548c55SMike Snitzer 	} else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
182ef548c55SMike Snitzer 		ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
183ef548c55SMike Snitzer 		return -EINVAL;
184a3998799SMike Snitzer 	}
185a3998799SMike Snitzer 
186*aa7d7bc9SMikulas Patocka 	if (!fc->corrupt_bio_byte && !test_bit(ERROR_READS, &fc->flags) &&
187*aa7d7bc9SMikulas Patocka 	    !test_bit(DROP_WRITES, &fc->flags) && !test_bit(ERROR_WRITES, &fc->flags)) {
188*aa7d7bc9SMikulas Patocka 		set_bit(ERROR_WRITES, &fc->flags);
189*aa7d7bc9SMikulas Patocka 		set_bit(ERROR_READS, &fc->flags);
190*aa7d7bc9SMikulas Patocka 	}
191*aa7d7bc9SMikulas Patocka 
192a3998799SMike Snitzer 	return 0;
193dfd068b0SMike Snitzer }
194dfd068b0SMike Snitzer 
1953407ef52SJosef Bacik /*
196dfd068b0SMike Snitzer  * Construct a flakey mapping:
197dfd068b0SMike Snitzer  * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
198b26f5e3dSMike Snitzer  *
199b26f5e3dSMike Snitzer  *   Feature args:
200b26f5e3dSMike Snitzer  *     [drop_writes]
201a3998799SMike Snitzer  *     [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
202a3998799SMike Snitzer  *
203a3998799SMike Snitzer  *   Nth_byte starts from 1 for the first byte.
204a3998799SMike Snitzer  *   Direction is r for READ or w for WRITE.
205a3998799SMike Snitzer  *   bio_flags is ignored if 0.
2063407ef52SJosef Bacik  */
2073407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2083407ef52SJosef Bacik {
2095916a22bSEric Biggers 	static const struct dm_arg _args[] = {
210dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid up interval"},
211dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid down interval"},
212dfd068b0SMike Snitzer 	};
2133407ef52SJosef Bacik 
214dfd068b0SMike Snitzer 	int r;
215dfd068b0SMike Snitzer 	struct flakey_c *fc;
216dfd068b0SMike Snitzer 	unsigned long long tmpll;
217dfd068b0SMike Snitzer 	struct dm_arg_set as;
218dfd068b0SMike Snitzer 	const char *devname;
21931998ef1SMikulas Patocka 	char dummy;
220dfd068b0SMike Snitzer 
221dfd068b0SMike Snitzer 	as.argc = argc;
222dfd068b0SMike Snitzer 	as.argv = argv;
223dfd068b0SMike Snitzer 
224dfd068b0SMike Snitzer 	if (argc < 4) {
225dfd068b0SMike Snitzer 		ti->error = "Invalid argument count";
2263407ef52SJosef Bacik 		return -EINVAL;
2273407ef52SJosef Bacik 	}
2283407ef52SJosef Bacik 
229b26f5e3dSMike Snitzer 	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
2303407ef52SJosef Bacik 	if (!fc) {
23175e3a0f5SAlasdair G Kergon 		ti->error = "Cannot allocate context";
2323407ef52SJosef Bacik 		return -ENOMEM;
2333407ef52SJosef Bacik 	}
2343407ef52SJosef Bacik 	fc->start_time = jiffies;
2353407ef52SJosef Bacik 
236dfd068b0SMike Snitzer 	devname = dm_shift_arg(&as);
2373407ef52SJosef Bacik 
238e80d1c80SVivek Goyal 	r = -EINVAL;
239ef87bfc2SMilan Broz 	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
240dfd068b0SMike Snitzer 		ti->error = "Invalid device sector";
2413407ef52SJosef Bacik 		goto bad;
2423407ef52SJosef Bacik 	}
243dfd068b0SMike Snitzer 	fc->start = tmpll;
2443407ef52SJosef Bacik 
245dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
246dfd068b0SMike Snitzer 	if (r)
2473407ef52SJosef Bacik 		goto bad;
248dfd068b0SMike Snitzer 
249dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
250dfd068b0SMike Snitzer 	if (r)
251dfd068b0SMike Snitzer 		goto bad;
2523407ef52SJosef Bacik 
2533407ef52SJosef Bacik 	if (!(fc->up_interval + fc->down_interval)) {
254dfd068b0SMike Snitzer 		ti->error = "Total (up + down) interval is zero";
255bff7e067SWei Yongjun 		r = -EINVAL;
2563407ef52SJosef Bacik 		goto bad;
2573407ef52SJosef Bacik 	}
2583407ef52SJosef Bacik 
2593407ef52SJosef Bacik 	if (fc->up_interval + fc->down_interval < fc->up_interval) {
260dfd068b0SMike Snitzer 		ti->error = "Interval overflow";
261bff7e067SWei Yongjun 		r = -EINVAL;
2623407ef52SJosef Bacik 		goto bad;
2633407ef52SJosef Bacik 	}
2643407ef52SJosef Bacik 
265b26f5e3dSMike Snitzer 	r = parse_features(&as, fc, ti);
266dfd068b0SMike Snitzer 	if (r)
267dfd068b0SMike Snitzer 		goto bad;
268dfd068b0SMike Snitzer 
269e80d1c80SVivek Goyal 	r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
270e80d1c80SVivek Goyal 	if (r) {
271dfd068b0SMike Snitzer 		ti->error = "Device lookup failed";
2723407ef52SJosef Bacik 		goto bad;
2733407ef52SJosef Bacik 	}
2743407ef52SJosef Bacik 
27555a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
27655a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
27730187e1dSMike Snitzer 	ti->per_io_data_size = sizeof(struct per_bio_data);
2783407ef52SJosef Bacik 	ti->private = fc;
2793407ef52SJosef Bacik 	return 0;
2803407ef52SJosef Bacik 
2813407ef52SJosef Bacik bad:
2823407ef52SJosef Bacik 	kfree(fc);
283e80d1c80SVivek Goyal 	return r;
2843407ef52SJosef Bacik }
2853407ef52SJosef Bacik 
2863407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti)
2873407ef52SJosef Bacik {
2883407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2893407ef52SJosef Bacik 
2903407ef52SJosef Bacik 	dm_put_device(ti, fc->dev);
2913407ef52SJosef Bacik 	kfree(fc);
2923407ef52SJosef Bacik }
2933407ef52SJosef Bacik 
2943407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
2953407ef52SJosef Bacik {
2963407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2973407ef52SJosef Bacik 
29830e4171bSMike Snitzer 	return fc->start + dm_target_offset(ti, bi_sector);
2993407ef52SJosef Bacik }
3003407ef52SJosef Bacik 
3013407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
3023407ef52SJosef Bacik {
3033407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
3043407ef52SJosef Bacik 
30574d46992SChristoph Hellwig 	bio_set_dev(bio, fc->dev->bdev);
306e86f2b00SMike Snitzer 	bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
3073407ef52SJosef Bacik }
3083407ef52SJosef Bacik 
309a3998799SMike Snitzer static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
310a3998799SMike Snitzer {
311a00f5276SSweet Tea 	unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
312a00f5276SSweet Tea 
313a00f5276SSweet Tea 	struct bvec_iter iter;
314a00f5276SSweet Tea 	struct bio_vec bvec;
315a00f5276SSweet Tea 
316a00f5276SSweet Tea 	if (!bio_has_data(bio))
317a00f5276SSweet Tea 		return;
318a3998799SMike Snitzer 
319a3998799SMike Snitzer 	/*
320a00f5276SSweet Tea 	 * Overwrite the Nth byte of the bio's data, on whichever page
321a00f5276SSweet Tea 	 * it falls.
322a3998799SMike Snitzer 	 */
323a00f5276SSweet Tea 	bio_for_each_segment(bvec, bio, iter) {
324a00f5276SSweet Tea 		if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
325f50714b5SMikulas Patocka 			char *segment;
326f50714b5SMikulas Patocka 			struct page *page = bio_iter_page(bio, iter);
327f50714b5SMikulas Patocka 			if (unlikely(page == ZERO_PAGE(0)))
328f50714b5SMikulas Patocka 				break;
3298eb29c4fSMikulas Patocka 			segment = bvec_kmap_local(&bvec);
330a00f5276SSweet Tea 			segment[corrupt_bio_byte] = fc->corrupt_bio_value;
3318eb29c4fSMikulas Patocka 			kunmap_local(segment);
332a3998799SMike Snitzer 			DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
333a00f5276SSweet Tea 				"(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
334a3998799SMike Snitzer 				bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
3351eff9d32SJens Axboe 				(bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
336a00f5276SSweet Tea 				(unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
337a00f5276SSweet Tea 			break;
338a00f5276SSweet Tea 		}
339a00f5276SSweet Tea 		corrupt_bio_byte -= bio_iter_len(bio, iter);
340a3998799SMike Snitzer 	}
341a3998799SMike Snitzer }
342a3998799SMike Snitzer 
3437de3ee57SMikulas Patocka static int flakey_map(struct dm_target *ti, struct bio *bio)
3443407ef52SJosef Bacik {
3453407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
34686a3238cSHeinz Mauelshagen 	unsigned int elapsed;
347c7cfdf59SMikulas Patocka 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
3480ef0b471SHeinz Mauelshagen 
349c7cfdf59SMikulas Patocka 	pb->bio_submitted = false;
3503407ef52SJosef Bacik 
3512e2d6f7eSAjay Joshi 	if (op_is_zone_mgmt(bio_op(bio)))
352124c4454SDamien Le Moal 		goto map_bio;
353124c4454SDamien Le Moal 
3543407ef52SJosef Bacik 	/* Are we alive ? */
3553407ef52SJosef Bacik 	elapsed = (jiffies - fc->start_time) / HZ;
356b26f5e3dSMike Snitzer 	if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
357a3998799SMike Snitzer 		/*
358a3998799SMike Snitzer 		 * Flag this bio as submitted while down.
359a3998799SMike Snitzer 		 */
360c7cfdf59SMikulas Patocka 		pb->bio_submitted = true;
3613407ef52SJosef Bacik 
362b26f5e3dSMike Snitzer 		/*
363ef548c55SMike Snitzer 		 * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
364299f6230SMike Snitzer 		 * Otherwise, flakey_end_io() will decide if the reads should be modified.
365a3998799SMike Snitzer 		 */
36699f3c90dSMike Snitzer 		if (bio_data_dir(bio) == READ) {
367*aa7d7bc9SMikulas Patocka 			if (test_bit(ERROR_READS, &fc->flags))
368846785e6SChristoph Hellwig 				return DM_MAPIO_KILL;
369299f6230SMike Snitzer 			goto map_bio;
37099f3c90dSMike Snitzer 		}
371a3998799SMike Snitzer 
372a3998799SMike Snitzer 		/*
373ef548c55SMike Snitzer 		 * Drop or error writes?
374b26f5e3dSMike Snitzer 		 */
375b26f5e3dSMike Snitzer 		if (test_bit(DROP_WRITES, &fc->flags)) {
3764246a0b6SChristoph Hellwig 			bio_endio(bio);
377b26f5e3dSMike Snitzer 			return DM_MAPIO_SUBMITTED;
37803b18887SHeinz Mauelshagen 		} else if (test_bit(ERROR_WRITES, &fc->flags)) {
379ef548c55SMike Snitzer 			bio_io_error(bio);
380ef548c55SMike Snitzer 			return DM_MAPIO_SUBMITTED;
381ef548c55SMike Snitzer 		}
382a3998799SMike Snitzer 
383a3998799SMike Snitzer 		/*
384a3998799SMike Snitzer 		 * Corrupt matching writes.
385a3998799SMike Snitzer 		 */
386aa56b9b7SMikulas Patocka 		if (fc->corrupt_bio_byte) {
387aa56b9b7SMikulas Patocka 			if (fc->corrupt_bio_rw == WRITE) {
388a3998799SMike Snitzer 				if (all_corrupt_bio_flags_match(bio, fc))
389a3998799SMike Snitzer 					corrupt_bio_data(bio, fc);
390aa56b9b7SMikulas Patocka 			}
391b26f5e3dSMike Snitzer 			goto map_bio;
392b26f5e3dSMike Snitzer 		}
393b26f5e3dSMike Snitzer 	}
394b26f5e3dSMike Snitzer 
395b26f5e3dSMike Snitzer map_bio:
3963407ef52SJosef Bacik 	flakey_map_bio(ti, bio);
3973407ef52SJosef Bacik 
3983407ef52SJosef Bacik 	return DM_MAPIO_REMAPPED;
3993407ef52SJosef Bacik }
4003407ef52SJosef Bacik 
4014e4cbee9SChristoph Hellwig static int flakey_end_io(struct dm_target *ti, struct bio *bio,
4024e4cbee9SChristoph Hellwig 			 blk_status_t *error)
403a3998799SMike Snitzer {
404a3998799SMike Snitzer 	struct flakey_c *fc = ti->private;
405c7cfdf59SMikulas Patocka 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
406a3998799SMike Snitzer 
4072e2d6f7eSAjay Joshi 	if (op_is_zone_mgmt(bio_op(bio)))
408124c4454SDamien Le Moal 		return DM_ENDIO_DONE;
409124c4454SDamien Le Moal 
4101be56909SChristoph Hellwig 	if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
411aa56b9b7SMikulas Patocka 		if (fc->corrupt_bio_byte) {
412aa56b9b7SMikulas Patocka 			if ((fc->corrupt_bio_rw == READ) &&
413299f6230SMike Snitzer 			    all_corrupt_bio_flags_match(bio, fc)) {
414299f6230SMike Snitzer 				/*
415299f6230SMike Snitzer 				 * Corrupt successful matching READs while in down state.
416299f6230SMike Snitzer 				 */
417a3998799SMike Snitzer 				corrupt_bio_data(bio, fc);
418aa56b9b7SMikulas Patocka 			}
419*aa7d7bc9SMikulas Patocka 		}
420*aa7d7bc9SMikulas Patocka 		if (test_bit(ERROR_READS, &fc->flags)) {
421299f6230SMike Snitzer 			/*
422299f6230SMike Snitzer 			 * Error read during the down_interval if drop_writes
423ef548c55SMike Snitzer 			 * and error_writes were not configured.
424299f6230SMike Snitzer 			 */
4254e4cbee9SChristoph Hellwig 			*error = BLK_STS_IOERR;
42699f3c90dSMike Snitzer 		}
427299f6230SMike Snitzer 	}
428a3998799SMike Snitzer 
4291be56909SChristoph Hellwig 	return DM_ENDIO_DONE;
430a3998799SMike Snitzer }
431a3998799SMike Snitzer 
432fd7c092eSMikulas Patocka static void flakey_status(struct dm_target *ti, status_type_t type,
43386a3238cSHeinz Mauelshagen 			  unsigned int status_flags, char *result, unsigned int maxlen)
4343407ef52SJosef Bacik {
43586a3238cSHeinz Mauelshagen 	unsigned int sz = 0;
4363407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
437*aa7d7bc9SMikulas Patocka 	unsigned int error_reads, drop_writes, error_writes;
4383407ef52SJosef Bacik 
4393407ef52SJosef Bacik 	switch (type) {
4403407ef52SJosef Bacik 	case STATUSTYPE_INFO:
4413407ef52SJosef Bacik 		result[0] = '\0';
4423407ef52SJosef Bacik 		break;
4433407ef52SJosef Bacik 
4443407ef52SJosef Bacik 	case STATUSTYPE_TABLE:
445b26f5e3dSMike Snitzer 		DMEMIT("%s %llu %u %u", fc->dev->name,
4463407ef52SJosef Bacik 		       (unsigned long long)fc->start, fc->up_interval,
4473407ef52SJosef Bacik 		       fc->down_interval);
448b26f5e3dSMike Snitzer 
449*aa7d7bc9SMikulas Patocka 		error_reads = test_bit(ERROR_READS, &fc->flags);
450b26f5e3dSMike Snitzer 		drop_writes = test_bit(DROP_WRITES, &fc->flags);
451ef548c55SMike Snitzer 		error_writes = test_bit(ERROR_WRITES, &fc->flags);
452*aa7d7bc9SMikulas Patocka 		DMEMIT(" %u", error_reads + drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
453a3998799SMike Snitzer 
454*aa7d7bc9SMikulas Patocka 		if (error_reads)
455*aa7d7bc9SMikulas Patocka 			DMEMIT(" error_reads");
456b26f5e3dSMike Snitzer 		if (drop_writes)
457b26f5e3dSMike Snitzer 			DMEMIT(" drop_writes");
458ef548c55SMike Snitzer 		else if (error_writes)
459ef548c55SMike Snitzer 			DMEMIT(" error_writes");
460a3998799SMike Snitzer 
461a3998799SMike Snitzer 		if (fc->corrupt_bio_byte)
462a3998799SMike Snitzer 			DMEMIT(" corrupt_bio_byte %u %c %u %u",
463a3998799SMike Snitzer 			       fc->corrupt_bio_byte,
464a3998799SMike Snitzer 			       (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
465a3998799SMike Snitzer 			       fc->corrupt_bio_value, fc->corrupt_bio_flags);
466a3998799SMike Snitzer 
4673407ef52SJosef Bacik 		break;
4688ec45662STushar Sugandhi 
4698ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
4708ec45662STushar Sugandhi 		result[0] = '\0';
4718ec45662STushar Sugandhi 		break;
4723407ef52SJosef Bacik 	}
4733407ef52SJosef Bacik }
4743407ef52SJosef Bacik 
4755bd5e8d8SMike Snitzer static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
4763407ef52SJosef Bacik {
4773407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
478e56f81e0SChristoph Hellwig 
479e56f81e0SChristoph Hellwig 	*bdev = fc->dev->bdev;
4803407ef52SJosef Bacik 
481ec8013beSPaolo Bonzini 	/*
482ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
483ec8013beSPaolo Bonzini 	 */
4846dcbb52cSChristoph Hellwig 	if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
485e56f81e0SChristoph Hellwig 		return 1;
486e56f81e0SChristoph Hellwig 	return 0;
4873407ef52SJosef Bacik }
4883407ef52SJosef Bacik 
489e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
490d4100351SChristoph Hellwig static int flakey_report_zones(struct dm_target *ti,
491d4100351SChristoph Hellwig 		struct dm_report_zones_args *args, unsigned int nr_zones)
492e76239a3SChristoph Hellwig {
493e76239a3SChristoph Hellwig 	struct flakey_c *fc = ti->private;
494e76239a3SChristoph Hellwig 
495912e8875SDamien Le Moal 	return dm_report_zones(fc->dev->bdev, fc->start,
496912e8875SDamien Le Moal 			       flakey_map_sector(ti, args->next_sector),
497912e8875SDamien Le Moal 			       args, nr_zones);
498e76239a3SChristoph Hellwig }
499e3290b94SMike Snitzer #else
500e3290b94SMike Snitzer #define flakey_report_zones NULL
501e76239a3SChristoph Hellwig #endif
502e76239a3SChristoph Hellwig 
5033407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
5043407ef52SJosef Bacik {
5053407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
5063407ef52SJosef Bacik 
5073407ef52SJosef Bacik 	return fn(ti, fc->dev, fc->start, ti->len, data);
5083407ef52SJosef Bacik }
5093407ef52SJosef Bacik 
5103407ef52SJosef Bacik static struct target_type flakey_target = {
5113407ef52SJosef Bacik 	.name   = "flakey",
512124c4454SDamien Le Moal 	.version = {1, 5, 0},
5133db564b4SSatya Tangirala 	.features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
514e76239a3SChristoph Hellwig 	.report_zones = flakey_report_zones,
5153407ef52SJosef Bacik 	.module = THIS_MODULE,
5163407ef52SJosef Bacik 	.ctr    = flakey_ctr,
5173407ef52SJosef Bacik 	.dtr    = flakey_dtr,
5183407ef52SJosef Bacik 	.map    = flakey_map,
519a3998799SMike Snitzer 	.end_io = flakey_end_io,
5203407ef52SJosef Bacik 	.status = flakey_status,
521e56f81e0SChristoph Hellwig 	.prepare_ioctl = flakey_prepare_ioctl,
5223407ef52SJosef Bacik 	.iterate_devices = flakey_iterate_devices,
5233407ef52SJosef Bacik };
5243664ff82SYangtao Li module_dm(flakey);
5253407ef52SJosef Bacik 
5263407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target");
5273407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
5283407ef52SJosef Bacik MODULE_LICENSE("GPL");
529