xref: /openbmc/linux/drivers/md/dm-flakey.c (revision 5bd5e8d891c1fd2d966a7e2c26f0452d22410683)
13407ef52SJosef Bacik /*
23407ef52SJosef Bacik  * Copyright (C) 2003 Sistina Software (UK) Limited.
3a3998799SMike Snitzer  * Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
43407ef52SJosef Bacik  *
53407ef52SJosef Bacik  * This file is released under the GPL.
63407ef52SJosef Bacik  */
73407ef52SJosef Bacik 
83407ef52SJosef Bacik #include <linux/device-mapper.h>
93407ef52SJosef Bacik 
103407ef52SJosef Bacik #include <linux/module.h>
113407ef52SJosef Bacik #include <linux/init.h>
123407ef52SJosef Bacik #include <linux/blkdev.h>
133407ef52SJosef Bacik #include <linux/bio.h>
143407ef52SJosef Bacik #include <linux/slab.h>
153407ef52SJosef Bacik 
163407ef52SJosef Bacik #define DM_MSG_PREFIX "flakey"
173407ef52SJosef Bacik 
18a3998799SMike Snitzer #define all_corrupt_bio_flags_match(bio, fc)	\
191eff9d32SJens Axboe 	(((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
20a3998799SMike Snitzer 
213407ef52SJosef Bacik /*
223407ef52SJosef Bacik  * Flakey: Used for testing only, simulates intermittent,
233407ef52SJosef Bacik  * catastrophic device failure.
243407ef52SJosef Bacik  */
253407ef52SJosef Bacik struct flakey_c {
263407ef52SJosef Bacik 	struct dm_dev *dev;
273407ef52SJosef Bacik 	unsigned long start_time;
283407ef52SJosef Bacik 	sector_t start;
293407ef52SJosef Bacik 	unsigned up_interval;
303407ef52SJosef Bacik 	unsigned down_interval;
31b26f5e3dSMike Snitzer 	unsigned long flags;
32a3998799SMike Snitzer 	unsigned corrupt_bio_byte;
33a3998799SMike Snitzer 	unsigned corrupt_bio_rw;
34a3998799SMike Snitzer 	unsigned corrupt_bio_value;
35a3998799SMike Snitzer 	unsigned corrupt_bio_flags;
363407ef52SJosef Bacik };
373407ef52SJosef Bacik 
38b26f5e3dSMike Snitzer enum feature_flag_bits {
39ef548c55SMike Snitzer 	DROP_WRITES,
40ef548c55SMike Snitzer 	ERROR_WRITES
41b26f5e3dSMike Snitzer };
42b26f5e3dSMike Snitzer 
43c7cfdf59SMikulas Patocka struct per_bio_data {
44c7cfdf59SMikulas Patocka 	bool bio_submitted;
45c7cfdf59SMikulas Patocka };
46c7cfdf59SMikulas Patocka 
47b26f5e3dSMike Snitzer static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
48b26f5e3dSMike Snitzer 			  struct dm_target *ti)
49dfd068b0SMike Snitzer {
50dfd068b0SMike Snitzer 	int r;
51dfd068b0SMike Snitzer 	unsigned argc;
52dfd068b0SMike Snitzer 	const char *arg_name;
53dfd068b0SMike Snitzer 
545916a22bSEric Biggers 	static const struct dm_arg _args[] = {
55a3998799SMike Snitzer 		{0, 6, "Invalid number of feature args"},
56a3998799SMike Snitzer 		{1, UINT_MAX, "Invalid corrupt bio byte"},
57a3998799SMike Snitzer 		{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
58a3998799SMike Snitzer 		{0, UINT_MAX, "Invalid corrupt bio flags mask"},
59dfd068b0SMike Snitzer 	};
60dfd068b0SMike Snitzer 
61dfd068b0SMike Snitzer 	/* No feature arguments supplied. */
62dfd068b0SMike Snitzer 	if (!as->argc)
63dfd068b0SMike Snitzer 		return 0;
64dfd068b0SMike Snitzer 
65dfd068b0SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
66dfd068b0SMike Snitzer 	if (r)
67a3998799SMike Snitzer 		return r;
68dfd068b0SMike Snitzer 
69a3998799SMike Snitzer 	while (argc) {
70dfd068b0SMike Snitzer 		arg_name = dm_shift_arg(as);
71dfd068b0SMike Snitzer 		argc--;
72dfd068b0SMike Snitzer 
737690e253SGoldwyn Rodrigues 		if (!arg_name) {
747690e253SGoldwyn Rodrigues 			ti->error = "Insufficient feature arguments";
757690e253SGoldwyn Rodrigues 			return -EINVAL;
767690e253SGoldwyn Rodrigues 		}
777690e253SGoldwyn Rodrigues 
78b26f5e3dSMike Snitzer 		/*
79b26f5e3dSMike Snitzer 		 * drop_writes
80b26f5e3dSMike Snitzer 		 */
81b26f5e3dSMike Snitzer 		if (!strcasecmp(arg_name, "drop_writes")) {
82b26f5e3dSMike Snitzer 			if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
83b26f5e3dSMike Snitzer 				ti->error = "Feature drop_writes duplicated";
84b26f5e3dSMike Snitzer 				return -EINVAL;
85ef548c55SMike Snitzer 			} else if (test_bit(ERROR_WRITES, &fc->flags)) {
86ef548c55SMike Snitzer 				ti->error = "Feature drop_writes conflicts with feature error_writes";
87ef548c55SMike Snitzer 				return -EINVAL;
88ef548c55SMike Snitzer 			}
89ef548c55SMike Snitzer 
90ef548c55SMike Snitzer 			continue;
91ef548c55SMike Snitzer 		}
92ef548c55SMike Snitzer 
93ef548c55SMike Snitzer 		/*
94ef548c55SMike Snitzer 		 * error_writes
95ef548c55SMike Snitzer 		 */
96ef548c55SMike Snitzer 		if (!strcasecmp(arg_name, "error_writes")) {
97ef548c55SMike Snitzer 			if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
98ef548c55SMike Snitzer 				ti->error = "Feature error_writes duplicated";
99ef548c55SMike Snitzer 				return -EINVAL;
100ef548c55SMike Snitzer 
101ef548c55SMike Snitzer 			} else if (test_bit(DROP_WRITES, &fc->flags)) {
102ef548c55SMike Snitzer 				ti->error = "Feature error_writes conflicts with feature drop_writes";
103ef548c55SMike Snitzer 				return -EINVAL;
104b26f5e3dSMike Snitzer 			}
105b26f5e3dSMike Snitzer 
106b26f5e3dSMike Snitzer 			continue;
107b26f5e3dSMike Snitzer 		}
108b26f5e3dSMike Snitzer 
109a3998799SMike Snitzer 		/*
110a3998799SMike Snitzer 		 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
111a3998799SMike Snitzer 		 */
112a3998799SMike Snitzer 		if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
11368e58a29SMike Snitzer 			if (!argc) {
114a3998799SMike Snitzer 				ti->error = "Feature corrupt_bio_byte requires parameters";
11568e58a29SMike Snitzer 				return -EINVAL;
11668e58a29SMike Snitzer 			}
117a3998799SMike Snitzer 
118a3998799SMike Snitzer 			r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
119a3998799SMike Snitzer 			if (r)
120a3998799SMike Snitzer 				return r;
121a3998799SMike Snitzer 			argc--;
122a3998799SMike Snitzer 
123a3998799SMike Snitzer 			/*
124a3998799SMike Snitzer 			 * Direction r or w?
125a3998799SMike Snitzer 			 */
126a3998799SMike Snitzer 			arg_name = dm_shift_arg(as);
127a3998799SMike Snitzer 			if (!strcasecmp(arg_name, "w"))
128a3998799SMike Snitzer 				fc->corrupt_bio_rw = WRITE;
129a3998799SMike Snitzer 			else if (!strcasecmp(arg_name, "r"))
130a3998799SMike Snitzer 				fc->corrupt_bio_rw = READ;
131a3998799SMike Snitzer 			else {
132a3998799SMike Snitzer 				ti->error = "Invalid corrupt bio direction (r or w)";
133a3998799SMike Snitzer 				return -EINVAL;
134a3998799SMike Snitzer 			}
135a3998799SMike Snitzer 			argc--;
136a3998799SMike Snitzer 
137a3998799SMike Snitzer 			/*
138a3998799SMike Snitzer 			 * Value of byte (0-255) to write in place of correct one.
139a3998799SMike Snitzer 			 */
140a3998799SMike Snitzer 			r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
141a3998799SMike Snitzer 			if (r)
142a3998799SMike Snitzer 				return r;
143a3998799SMike Snitzer 			argc--;
144a3998799SMike Snitzer 
145a3998799SMike Snitzer 			/*
146a3998799SMike Snitzer 			 * Only corrupt bios with these flags set.
147a3998799SMike Snitzer 			 */
148a3998799SMike Snitzer 			r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
149a3998799SMike Snitzer 			if (r)
150a3998799SMike Snitzer 				return r;
151a3998799SMike Snitzer 			argc--;
152a3998799SMike Snitzer 
153a3998799SMike Snitzer 			continue;
154dfd068b0SMike Snitzer 		}
155dfd068b0SMike Snitzer 
156a3998799SMike Snitzer 		ti->error = "Unrecognised flakey feature requested";
157a3998799SMike Snitzer 		return -EINVAL;
158a3998799SMike Snitzer 	}
159a3998799SMike Snitzer 
160a3998799SMike Snitzer 	if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
161a3998799SMike Snitzer 		ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
162a3998799SMike Snitzer 		return -EINVAL;
163ef548c55SMike Snitzer 
164ef548c55SMike Snitzer 	} else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
165ef548c55SMike Snitzer 		ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
166ef548c55SMike Snitzer 		return -EINVAL;
167a3998799SMike Snitzer 	}
168a3998799SMike Snitzer 
169a3998799SMike Snitzer 	return 0;
170dfd068b0SMike Snitzer }
171dfd068b0SMike Snitzer 
1723407ef52SJosef Bacik /*
173dfd068b0SMike Snitzer  * Construct a flakey mapping:
174dfd068b0SMike Snitzer  * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
175b26f5e3dSMike Snitzer  *
176b26f5e3dSMike Snitzer  *   Feature args:
177b26f5e3dSMike Snitzer  *     [drop_writes]
178a3998799SMike Snitzer  *     [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
179a3998799SMike Snitzer  *
180a3998799SMike Snitzer  *   Nth_byte starts from 1 for the first byte.
181a3998799SMike Snitzer  *   Direction is r for READ or w for WRITE.
182a3998799SMike Snitzer  *   bio_flags is ignored if 0.
1833407ef52SJosef Bacik  */
1843407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1853407ef52SJosef Bacik {
1865916a22bSEric Biggers 	static const struct dm_arg _args[] = {
187dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid up interval"},
188dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid down interval"},
189dfd068b0SMike Snitzer 	};
1903407ef52SJosef Bacik 
191dfd068b0SMike Snitzer 	int r;
192dfd068b0SMike Snitzer 	struct flakey_c *fc;
193dfd068b0SMike Snitzer 	unsigned long long tmpll;
194dfd068b0SMike Snitzer 	struct dm_arg_set as;
195dfd068b0SMike Snitzer 	const char *devname;
19631998ef1SMikulas Patocka 	char dummy;
197dfd068b0SMike Snitzer 
198dfd068b0SMike Snitzer 	as.argc = argc;
199dfd068b0SMike Snitzer 	as.argv = argv;
200dfd068b0SMike Snitzer 
201dfd068b0SMike Snitzer 	if (argc < 4) {
202dfd068b0SMike Snitzer 		ti->error = "Invalid argument count";
2033407ef52SJosef Bacik 		return -EINVAL;
2043407ef52SJosef Bacik 	}
2053407ef52SJosef Bacik 
206b26f5e3dSMike Snitzer 	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
2073407ef52SJosef Bacik 	if (!fc) {
20875e3a0f5SAlasdair G Kergon 		ti->error = "Cannot allocate context";
2093407ef52SJosef Bacik 		return -ENOMEM;
2103407ef52SJosef Bacik 	}
2113407ef52SJosef Bacik 	fc->start_time = jiffies;
2123407ef52SJosef Bacik 
213dfd068b0SMike Snitzer 	devname = dm_shift_arg(&as);
2143407ef52SJosef Bacik 
215e80d1c80SVivek Goyal 	r = -EINVAL;
21631998ef1SMikulas Patocka 	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
217dfd068b0SMike Snitzer 		ti->error = "Invalid device sector";
2183407ef52SJosef Bacik 		goto bad;
2193407ef52SJosef Bacik 	}
220dfd068b0SMike Snitzer 	fc->start = tmpll;
2213407ef52SJosef Bacik 
222dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
223dfd068b0SMike Snitzer 	if (r)
2243407ef52SJosef Bacik 		goto bad;
225dfd068b0SMike Snitzer 
226dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
227dfd068b0SMike Snitzer 	if (r)
228dfd068b0SMike Snitzer 		goto bad;
2293407ef52SJosef Bacik 
2303407ef52SJosef Bacik 	if (!(fc->up_interval + fc->down_interval)) {
231dfd068b0SMike Snitzer 		ti->error = "Total (up + down) interval is zero";
232bff7e067SWei Yongjun 		r = -EINVAL;
2333407ef52SJosef Bacik 		goto bad;
2343407ef52SJosef Bacik 	}
2353407ef52SJosef Bacik 
2363407ef52SJosef Bacik 	if (fc->up_interval + fc->down_interval < fc->up_interval) {
237dfd068b0SMike Snitzer 		ti->error = "Interval overflow";
238bff7e067SWei Yongjun 		r = -EINVAL;
2393407ef52SJosef Bacik 		goto bad;
2403407ef52SJosef Bacik 	}
2413407ef52SJosef Bacik 
242b26f5e3dSMike Snitzer 	r = parse_features(&as, fc, ti);
243dfd068b0SMike Snitzer 	if (r)
244dfd068b0SMike Snitzer 		goto bad;
245dfd068b0SMike Snitzer 
246e80d1c80SVivek Goyal 	r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
247e80d1c80SVivek Goyal 	if (r) {
248dfd068b0SMike Snitzer 		ti->error = "Device lookup failed";
2493407ef52SJosef Bacik 		goto bad;
2503407ef52SJosef Bacik 	}
2513407ef52SJosef Bacik 
25255a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
25355a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
25430187e1dSMike Snitzer 	ti->per_io_data_size = sizeof(struct per_bio_data);
2553407ef52SJosef Bacik 	ti->private = fc;
2563407ef52SJosef Bacik 	return 0;
2573407ef52SJosef Bacik 
2583407ef52SJosef Bacik bad:
2593407ef52SJosef Bacik 	kfree(fc);
260e80d1c80SVivek Goyal 	return r;
2613407ef52SJosef Bacik }
2623407ef52SJosef Bacik 
2633407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti)
2643407ef52SJosef Bacik {
2653407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2663407ef52SJosef Bacik 
2673407ef52SJosef Bacik 	dm_put_device(ti, fc->dev);
2683407ef52SJosef Bacik 	kfree(fc);
2693407ef52SJosef Bacik }
2703407ef52SJosef Bacik 
2713407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
2723407ef52SJosef Bacik {
2733407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2743407ef52SJosef Bacik 
27530e4171bSMike Snitzer 	return fc->start + dm_target_offset(ti, bi_sector);
2763407ef52SJosef Bacik }
2773407ef52SJosef Bacik 
2783407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
2793407ef52SJosef Bacik {
2803407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2813407ef52SJosef Bacik 
28274d46992SChristoph Hellwig 	bio_set_dev(bio, fc->dev->bdev);
283124c4454SDamien Le Moal 	if (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET)
2844f024f37SKent Overstreet 		bio->bi_iter.bi_sector =
2854f024f37SKent Overstreet 			flakey_map_sector(ti, bio->bi_iter.bi_sector);
2863407ef52SJosef Bacik }
2873407ef52SJosef Bacik 
288a3998799SMike Snitzer static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
289a3998799SMike Snitzer {
290a3998799SMike Snitzer 	unsigned bio_bytes = bio_cur_bytes(bio);
291a3998799SMike Snitzer 	char *data = bio_data(bio);
292a3998799SMike Snitzer 
293a3998799SMike Snitzer 	/*
294a3998799SMike Snitzer 	 * Overwrite the Nth byte of the data returned.
295a3998799SMike Snitzer 	 */
296a3998799SMike Snitzer 	if (data && bio_bytes >= fc->corrupt_bio_byte) {
297a3998799SMike Snitzer 		data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
298a3998799SMike Snitzer 
299a3998799SMike Snitzer 		DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
3001eff9d32SJens Axboe 			"(rw=%c bi_opf=%u bi_sector=%llu cur_bytes=%u)\n",
301a3998799SMike Snitzer 			bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
3021eff9d32SJens Axboe 			(bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
3034f024f37SKent Overstreet 			(unsigned long long)bio->bi_iter.bi_sector, bio_bytes);
304a3998799SMike Snitzer 	}
305a3998799SMike Snitzer }
306a3998799SMike Snitzer 
3077de3ee57SMikulas Patocka static int flakey_map(struct dm_target *ti, struct bio *bio)
3083407ef52SJosef Bacik {
3093407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
3103407ef52SJosef Bacik 	unsigned elapsed;
311c7cfdf59SMikulas Patocka 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
312c7cfdf59SMikulas Patocka 	pb->bio_submitted = false;
3133407ef52SJosef Bacik 
314124c4454SDamien Le Moal 	/* Do not fail reset zone */
315124c4454SDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_RESET)
316124c4454SDamien Le Moal 		goto map_bio;
317124c4454SDamien Le Moal 
318124c4454SDamien Le Moal 	/* We need to remap reported zones, so remember the BIO iter */
319124c4454SDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_REPORT)
320124c4454SDamien Le Moal 		goto map_bio;
321124c4454SDamien Le Moal 
3223407ef52SJosef Bacik 	/* Are we alive ? */
3233407ef52SJosef Bacik 	elapsed = (jiffies - fc->start_time) / HZ;
324b26f5e3dSMike Snitzer 	if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
325a3998799SMike Snitzer 		/*
326a3998799SMike Snitzer 		 * Flag this bio as submitted while down.
327a3998799SMike Snitzer 		 */
328c7cfdf59SMikulas Patocka 		pb->bio_submitted = true;
3293407ef52SJosef Bacik 
330b26f5e3dSMike Snitzer 		/*
331ef548c55SMike Snitzer 		 * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
332299f6230SMike Snitzer 		 * Otherwise, flakey_end_io() will decide if the reads should be modified.
333a3998799SMike Snitzer 		 */
33499f3c90dSMike Snitzer 		if (bio_data_dir(bio) == READ) {
335ef548c55SMike Snitzer 			if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
336ef548c55SMike Snitzer 			    !test_bit(ERROR_WRITES, &fc->flags))
337846785e6SChristoph Hellwig 				return DM_MAPIO_KILL;
338299f6230SMike Snitzer 			goto map_bio;
33999f3c90dSMike Snitzer 		}
340a3998799SMike Snitzer 
341a3998799SMike Snitzer 		/*
342ef548c55SMike Snitzer 		 * Drop or error writes?
343b26f5e3dSMike Snitzer 		 */
344b26f5e3dSMike Snitzer 		if (test_bit(DROP_WRITES, &fc->flags)) {
3454246a0b6SChristoph Hellwig 			bio_endio(bio);
346b26f5e3dSMike Snitzer 			return DM_MAPIO_SUBMITTED;
347b26f5e3dSMike Snitzer 		}
348ef548c55SMike Snitzer 		else if (test_bit(ERROR_WRITES, &fc->flags)) {
349ef548c55SMike Snitzer 			bio_io_error(bio);
350ef548c55SMike Snitzer 			return DM_MAPIO_SUBMITTED;
351ef548c55SMike Snitzer 		}
352a3998799SMike Snitzer 
353a3998799SMike Snitzer 		/*
354a3998799SMike Snitzer 		 * Corrupt matching writes.
355a3998799SMike Snitzer 		 */
356a3998799SMike Snitzer 		if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
357a3998799SMike Snitzer 			if (all_corrupt_bio_flags_match(bio, fc))
358a3998799SMike Snitzer 				corrupt_bio_data(bio, fc);
359b26f5e3dSMike Snitzer 			goto map_bio;
360b26f5e3dSMike Snitzer 		}
361b26f5e3dSMike Snitzer 
362b26f5e3dSMike Snitzer 		/*
363a3998799SMike Snitzer 		 * By default, error all I/O.
364b26f5e3dSMike Snitzer 		 */
365846785e6SChristoph Hellwig 		return DM_MAPIO_KILL;
366b26f5e3dSMike Snitzer 	}
367b26f5e3dSMike Snitzer 
368b26f5e3dSMike Snitzer map_bio:
3693407ef52SJosef Bacik 	flakey_map_bio(ti, bio);
3703407ef52SJosef Bacik 
3713407ef52SJosef Bacik 	return DM_MAPIO_REMAPPED;
3723407ef52SJosef Bacik }
3733407ef52SJosef Bacik 
3744e4cbee9SChristoph Hellwig static int flakey_end_io(struct dm_target *ti, struct bio *bio,
3754e4cbee9SChristoph Hellwig 			 blk_status_t *error)
376a3998799SMike Snitzer {
377a3998799SMike Snitzer 	struct flakey_c *fc = ti->private;
378c7cfdf59SMikulas Patocka 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
379a3998799SMike Snitzer 
380124c4454SDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_RESET)
381124c4454SDamien Le Moal 		return DM_ENDIO_DONE;
382124c4454SDamien Le Moal 
383124c4454SDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_REPORT) {
384124c4454SDamien Le Moal 		dm_remap_zone_report(ti, bio, fc->start);
385124c4454SDamien Le Moal 		return DM_ENDIO_DONE;
386124c4454SDamien Le Moal 	}
387124c4454SDamien Le Moal 
3881be56909SChristoph Hellwig 	if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
389299f6230SMike Snitzer 		if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
390299f6230SMike Snitzer 		    all_corrupt_bio_flags_match(bio, fc)) {
391299f6230SMike Snitzer 			/*
392299f6230SMike Snitzer 			 * Corrupt successful matching READs while in down state.
393299f6230SMike Snitzer 			 */
394a3998799SMike Snitzer 			corrupt_bio_data(bio, fc);
395299f6230SMike Snitzer 
396ef548c55SMike Snitzer 		} else if (!test_bit(DROP_WRITES, &fc->flags) &&
397ef548c55SMike Snitzer 			   !test_bit(ERROR_WRITES, &fc->flags)) {
398299f6230SMike Snitzer 			/*
399299f6230SMike Snitzer 			 * Error read during the down_interval if drop_writes
400ef548c55SMike Snitzer 			 * and error_writes were not configured.
401299f6230SMike Snitzer 			 */
4024e4cbee9SChristoph Hellwig 			*error = BLK_STS_IOERR;
40399f3c90dSMike Snitzer 		}
404299f6230SMike Snitzer 	}
405a3998799SMike Snitzer 
4061be56909SChristoph Hellwig 	return DM_ENDIO_DONE;
407a3998799SMike Snitzer }
408a3998799SMike Snitzer 
409fd7c092eSMikulas Patocka static void flakey_status(struct dm_target *ti, status_type_t type,
4101f4e0ff0SAlasdair G Kergon 			  unsigned status_flags, char *result, unsigned maxlen)
4113407ef52SJosef Bacik {
412b26f5e3dSMike Snitzer 	unsigned sz = 0;
4133407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
414ef548c55SMike Snitzer 	unsigned drop_writes, error_writes;
4153407ef52SJosef Bacik 
4163407ef52SJosef Bacik 	switch (type) {
4173407ef52SJosef Bacik 	case STATUSTYPE_INFO:
4183407ef52SJosef Bacik 		result[0] = '\0';
4193407ef52SJosef Bacik 		break;
4203407ef52SJosef Bacik 
4213407ef52SJosef Bacik 	case STATUSTYPE_TABLE:
422b26f5e3dSMike Snitzer 		DMEMIT("%s %llu %u %u ", fc->dev->name,
4233407ef52SJosef Bacik 		       (unsigned long long)fc->start, fc->up_interval,
4243407ef52SJosef Bacik 		       fc->down_interval);
425b26f5e3dSMike Snitzer 
426b26f5e3dSMike Snitzer 		drop_writes = test_bit(DROP_WRITES, &fc->flags);
427ef548c55SMike Snitzer 		error_writes = test_bit(ERROR_WRITES, &fc->flags);
428ef548c55SMike Snitzer 		DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
429a3998799SMike Snitzer 
430b26f5e3dSMike Snitzer 		if (drop_writes)
431b26f5e3dSMike Snitzer 			DMEMIT("drop_writes ");
432ef548c55SMike Snitzer 		else if (error_writes)
433ef548c55SMike Snitzer 			DMEMIT("error_writes ");
434a3998799SMike Snitzer 
435a3998799SMike Snitzer 		if (fc->corrupt_bio_byte)
436a3998799SMike Snitzer 			DMEMIT("corrupt_bio_byte %u %c %u %u ",
437a3998799SMike Snitzer 			       fc->corrupt_bio_byte,
438a3998799SMike Snitzer 			       (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
439a3998799SMike Snitzer 			       fc->corrupt_bio_value, fc->corrupt_bio_flags);
440a3998799SMike Snitzer 
4413407ef52SJosef Bacik 		break;
4423407ef52SJosef Bacik 	}
4433407ef52SJosef Bacik }
4443407ef52SJosef Bacik 
445*5bd5e8d8SMike Snitzer static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
4463407ef52SJosef Bacik {
4473407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
448e56f81e0SChristoph Hellwig 
449e56f81e0SChristoph Hellwig 	*bdev = fc->dev->bdev;
4503407ef52SJosef Bacik 
451ec8013beSPaolo Bonzini 	/*
452ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
453ec8013beSPaolo Bonzini 	 */
454ec8013beSPaolo Bonzini 	if (fc->start ||
455e56f81e0SChristoph Hellwig 	    ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
456e56f81e0SChristoph Hellwig 		return 1;
457e56f81e0SChristoph Hellwig 	return 0;
4583407ef52SJosef Bacik }
4593407ef52SJosef Bacik 
4603407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
4613407ef52SJosef Bacik {
4623407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
4633407ef52SJosef Bacik 
4643407ef52SJosef Bacik 	return fn(ti, fc->dev, fc->start, ti->len, data);
4653407ef52SJosef Bacik }
4663407ef52SJosef Bacik 
4673407ef52SJosef Bacik static struct target_type flakey_target = {
4683407ef52SJosef Bacik 	.name   = "flakey",
469124c4454SDamien Le Moal 	.version = {1, 5, 0},
470124c4454SDamien Le Moal 	.features = DM_TARGET_ZONED_HM,
4713407ef52SJosef Bacik 	.module = THIS_MODULE,
4723407ef52SJosef Bacik 	.ctr    = flakey_ctr,
4733407ef52SJosef Bacik 	.dtr    = flakey_dtr,
4743407ef52SJosef Bacik 	.map    = flakey_map,
475a3998799SMike Snitzer 	.end_io = flakey_end_io,
4763407ef52SJosef Bacik 	.status = flakey_status,
477e56f81e0SChristoph Hellwig 	.prepare_ioctl = flakey_prepare_ioctl,
4783407ef52SJosef Bacik 	.iterate_devices = flakey_iterate_devices,
4793407ef52SJosef Bacik };
4803407ef52SJosef Bacik 
4813407ef52SJosef Bacik static int __init dm_flakey_init(void)
4823407ef52SJosef Bacik {
4833407ef52SJosef Bacik 	int r = dm_register_target(&flakey_target);
4843407ef52SJosef Bacik 
4853407ef52SJosef Bacik 	if (r < 0)
4863407ef52SJosef Bacik 		DMERR("register failed %d", r);
4873407ef52SJosef Bacik 
4883407ef52SJosef Bacik 	return r;
4893407ef52SJosef Bacik }
4903407ef52SJosef Bacik 
4913407ef52SJosef Bacik static void __exit dm_flakey_exit(void)
4923407ef52SJosef Bacik {
4933407ef52SJosef Bacik 	dm_unregister_target(&flakey_target);
4943407ef52SJosef Bacik }
4953407ef52SJosef Bacik 
4963407ef52SJosef Bacik /* Module hooks */
4973407ef52SJosef Bacik module_init(dm_flakey_init);
4983407ef52SJosef Bacik module_exit(dm_flakey_exit);
4993407ef52SJosef Bacik 
5003407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target");
5013407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
5023407ef52SJosef Bacik MODULE_LICENSE("GPL");
503