xref: /openbmc/linux/drivers/md/dm-flakey.c (revision 68e58a294fb26f692697179e3f3ecf88dd8cb97c)
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)	\
19a3998799SMike Snitzer 	(((bio)->bi_rw & (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 {
39b26f5e3dSMike Snitzer 	DROP_WRITES
40b26f5e3dSMike Snitzer };
41b26f5e3dSMike Snitzer 
42b26f5e3dSMike Snitzer static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
43b26f5e3dSMike Snitzer 			  struct dm_target *ti)
44dfd068b0SMike Snitzer {
45dfd068b0SMike Snitzer 	int r;
46dfd068b0SMike Snitzer 	unsigned argc;
47dfd068b0SMike Snitzer 	const char *arg_name;
48dfd068b0SMike Snitzer 
49dfd068b0SMike Snitzer 	static struct dm_arg _args[] = {
50a3998799SMike Snitzer 		{0, 6, "Invalid number of feature args"},
51a3998799SMike Snitzer 		{1, UINT_MAX, "Invalid corrupt bio byte"},
52a3998799SMike Snitzer 		{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
53a3998799SMike Snitzer 		{0, UINT_MAX, "Invalid corrupt bio flags mask"},
54dfd068b0SMike Snitzer 	};
55dfd068b0SMike Snitzer 
56dfd068b0SMike Snitzer 	/* No feature arguments supplied. */
57dfd068b0SMike Snitzer 	if (!as->argc)
58dfd068b0SMike Snitzer 		return 0;
59dfd068b0SMike Snitzer 
60dfd068b0SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
61dfd068b0SMike Snitzer 	if (r)
62a3998799SMike Snitzer 		return r;
63dfd068b0SMike Snitzer 
64a3998799SMike Snitzer 	while (argc) {
65dfd068b0SMike Snitzer 		arg_name = dm_shift_arg(as);
66dfd068b0SMike Snitzer 		argc--;
67dfd068b0SMike Snitzer 
68b26f5e3dSMike Snitzer 		/*
69b26f5e3dSMike Snitzer 		 * drop_writes
70b26f5e3dSMike Snitzer 		 */
71b26f5e3dSMike Snitzer 		if (!strcasecmp(arg_name, "drop_writes")) {
72b26f5e3dSMike Snitzer 			if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
73b26f5e3dSMike Snitzer 				ti->error = "Feature drop_writes duplicated";
74b26f5e3dSMike Snitzer 				return -EINVAL;
75b26f5e3dSMike Snitzer 			}
76b26f5e3dSMike Snitzer 
77b26f5e3dSMike Snitzer 			continue;
78b26f5e3dSMike Snitzer 		}
79b26f5e3dSMike Snitzer 
80a3998799SMike Snitzer 		/*
81a3998799SMike Snitzer 		 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
82a3998799SMike Snitzer 		 */
83a3998799SMike Snitzer 		if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
84*68e58a29SMike Snitzer 			if (!argc) {
85a3998799SMike Snitzer 				ti->error = "Feature corrupt_bio_byte requires parameters";
86*68e58a29SMike Snitzer 				return -EINVAL;
87*68e58a29SMike Snitzer 			}
88a3998799SMike Snitzer 
89a3998799SMike Snitzer 			r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
90a3998799SMike Snitzer 			if (r)
91a3998799SMike Snitzer 				return r;
92a3998799SMike Snitzer 			argc--;
93a3998799SMike Snitzer 
94a3998799SMike Snitzer 			/*
95a3998799SMike Snitzer 			 * Direction r or w?
96a3998799SMike Snitzer 			 */
97a3998799SMike Snitzer 			arg_name = dm_shift_arg(as);
98a3998799SMike Snitzer 			if (!strcasecmp(arg_name, "w"))
99a3998799SMike Snitzer 				fc->corrupt_bio_rw = WRITE;
100a3998799SMike Snitzer 			else if (!strcasecmp(arg_name, "r"))
101a3998799SMike Snitzer 				fc->corrupt_bio_rw = READ;
102a3998799SMike Snitzer 			else {
103a3998799SMike Snitzer 				ti->error = "Invalid corrupt bio direction (r or w)";
104a3998799SMike Snitzer 				return -EINVAL;
105a3998799SMike Snitzer 			}
106a3998799SMike Snitzer 			argc--;
107a3998799SMike Snitzer 
108a3998799SMike Snitzer 			/*
109a3998799SMike Snitzer 			 * Value of byte (0-255) to write in place of correct one.
110a3998799SMike Snitzer 			 */
111a3998799SMike Snitzer 			r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
112a3998799SMike Snitzer 			if (r)
113a3998799SMike Snitzer 				return r;
114a3998799SMike Snitzer 			argc--;
115a3998799SMike Snitzer 
116a3998799SMike Snitzer 			/*
117a3998799SMike Snitzer 			 * Only corrupt bios with these flags set.
118a3998799SMike Snitzer 			 */
119a3998799SMike Snitzer 			r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
120a3998799SMike Snitzer 			if (r)
121a3998799SMike Snitzer 				return r;
122a3998799SMike Snitzer 			argc--;
123a3998799SMike Snitzer 
124a3998799SMike Snitzer 			continue;
125dfd068b0SMike Snitzer 		}
126dfd068b0SMike Snitzer 
127a3998799SMike Snitzer 		ti->error = "Unrecognised flakey feature requested";
128a3998799SMike Snitzer 		return -EINVAL;
129a3998799SMike Snitzer 	}
130a3998799SMike Snitzer 
131a3998799SMike Snitzer 	if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
132a3998799SMike Snitzer 		ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
133a3998799SMike Snitzer 		return -EINVAL;
134a3998799SMike Snitzer 	}
135a3998799SMike Snitzer 
136a3998799SMike Snitzer 	return 0;
137dfd068b0SMike Snitzer }
138dfd068b0SMike Snitzer 
1393407ef52SJosef Bacik /*
140dfd068b0SMike Snitzer  * Construct a flakey mapping:
141dfd068b0SMike Snitzer  * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
142b26f5e3dSMike Snitzer  *
143b26f5e3dSMike Snitzer  *   Feature args:
144b26f5e3dSMike Snitzer  *     [drop_writes]
145a3998799SMike Snitzer  *     [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
146a3998799SMike Snitzer  *
147a3998799SMike Snitzer  *   Nth_byte starts from 1 for the first byte.
148a3998799SMike Snitzer  *   Direction is r for READ or w for WRITE.
149a3998799SMike Snitzer  *   bio_flags is ignored if 0.
1503407ef52SJosef Bacik  */
1513407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1523407ef52SJosef Bacik {
153dfd068b0SMike Snitzer 	static struct dm_arg _args[] = {
154dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid up interval"},
155dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid down interval"},
156dfd068b0SMike Snitzer 	};
1573407ef52SJosef Bacik 
158dfd068b0SMike Snitzer 	int r;
159dfd068b0SMike Snitzer 	struct flakey_c *fc;
160dfd068b0SMike Snitzer 	unsigned long long tmpll;
161dfd068b0SMike Snitzer 	struct dm_arg_set as;
162dfd068b0SMike Snitzer 	const char *devname;
163dfd068b0SMike Snitzer 
164dfd068b0SMike Snitzer 	as.argc = argc;
165dfd068b0SMike Snitzer 	as.argv = argv;
166dfd068b0SMike Snitzer 
167dfd068b0SMike Snitzer 	if (argc < 4) {
168dfd068b0SMike Snitzer 		ti->error = "Invalid argument count";
1693407ef52SJosef Bacik 		return -EINVAL;
1703407ef52SJosef Bacik 	}
1713407ef52SJosef Bacik 
172b26f5e3dSMike Snitzer 	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
1733407ef52SJosef Bacik 	if (!fc) {
174dfd068b0SMike Snitzer 		ti->error = "Cannot allocate linear context";
1753407ef52SJosef Bacik 		return -ENOMEM;
1763407ef52SJosef Bacik 	}
1773407ef52SJosef Bacik 	fc->start_time = jiffies;
1783407ef52SJosef Bacik 
179dfd068b0SMike Snitzer 	devname = dm_shift_arg(&as);
1803407ef52SJosef Bacik 
181dfd068b0SMike Snitzer 	if (sscanf(dm_shift_arg(&as), "%llu", &tmpll) != 1) {
182dfd068b0SMike Snitzer 		ti->error = "Invalid device sector";
1833407ef52SJosef Bacik 		goto bad;
1843407ef52SJosef Bacik 	}
185dfd068b0SMike Snitzer 	fc->start = tmpll;
1863407ef52SJosef Bacik 
187dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
188dfd068b0SMike Snitzer 	if (r)
1893407ef52SJosef Bacik 		goto bad;
190dfd068b0SMike Snitzer 
191dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
192dfd068b0SMike Snitzer 	if (r)
193dfd068b0SMike Snitzer 		goto bad;
1943407ef52SJosef Bacik 
1953407ef52SJosef Bacik 	if (!(fc->up_interval + fc->down_interval)) {
196dfd068b0SMike Snitzer 		ti->error = "Total (up + down) interval is zero";
1973407ef52SJosef Bacik 		goto bad;
1983407ef52SJosef Bacik 	}
1993407ef52SJosef Bacik 
2003407ef52SJosef Bacik 	if (fc->up_interval + fc->down_interval < fc->up_interval) {
201dfd068b0SMike Snitzer 		ti->error = "Interval overflow";
2023407ef52SJosef Bacik 		goto bad;
2033407ef52SJosef Bacik 	}
2043407ef52SJosef Bacik 
205b26f5e3dSMike Snitzer 	r = parse_features(&as, fc, ti);
206dfd068b0SMike Snitzer 	if (r)
207dfd068b0SMike Snitzer 		goto bad;
208dfd068b0SMike Snitzer 
209dfd068b0SMike Snitzer 	if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev)) {
210dfd068b0SMike Snitzer 		ti->error = "Device lookup failed";
2113407ef52SJosef Bacik 		goto bad;
2123407ef52SJosef Bacik 	}
2133407ef52SJosef Bacik 
2143407ef52SJosef Bacik 	ti->num_flush_requests = 1;
21530e4171bSMike Snitzer 	ti->num_discard_requests = 1;
2163407ef52SJosef Bacik 	ti->private = fc;
2173407ef52SJosef Bacik 	return 0;
2183407ef52SJosef Bacik 
2193407ef52SJosef Bacik bad:
2203407ef52SJosef Bacik 	kfree(fc);
2213407ef52SJosef Bacik 	return -EINVAL;
2223407ef52SJosef Bacik }
2233407ef52SJosef Bacik 
2243407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti)
2253407ef52SJosef Bacik {
2263407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2273407ef52SJosef Bacik 
2283407ef52SJosef Bacik 	dm_put_device(ti, fc->dev);
2293407ef52SJosef Bacik 	kfree(fc);
2303407ef52SJosef Bacik }
2313407ef52SJosef Bacik 
2323407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
2333407ef52SJosef Bacik {
2343407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2353407ef52SJosef Bacik 
23630e4171bSMike Snitzer 	return fc->start + dm_target_offset(ti, bi_sector);
2373407ef52SJosef Bacik }
2383407ef52SJosef Bacik 
2393407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
2403407ef52SJosef Bacik {
2413407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2423407ef52SJosef Bacik 
2433407ef52SJosef Bacik 	bio->bi_bdev = fc->dev->bdev;
2443407ef52SJosef Bacik 	if (bio_sectors(bio))
2453407ef52SJosef Bacik 		bio->bi_sector = flakey_map_sector(ti, bio->bi_sector);
2463407ef52SJosef Bacik }
2473407ef52SJosef Bacik 
248a3998799SMike Snitzer static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
249a3998799SMike Snitzer {
250a3998799SMike Snitzer 	unsigned bio_bytes = bio_cur_bytes(bio);
251a3998799SMike Snitzer 	char *data = bio_data(bio);
252a3998799SMike Snitzer 
253a3998799SMike Snitzer 	/*
254a3998799SMike Snitzer 	 * Overwrite the Nth byte of the data returned.
255a3998799SMike Snitzer 	 */
256a3998799SMike Snitzer 	if (data && bio_bytes >= fc->corrupt_bio_byte) {
257a3998799SMike Snitzer 		data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
258a3998799SMike Snitzer 
259a3998799SMike Snitzer 		DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
260a3998799SMike Snitzer 			"(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
261a3998799SMike Snitzer 			bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
262a3998799SMike Snitzer 			(bio_data_dir(bio) == WRITE) ? 'w' : 'r',
263a3998799SMike Snitzer 			bio->bi_rw, (unsigned long long)bio->bi_sector, bio_bytes);
264a3998799SMike Snitzer 	}
265a3998799SMike Snitzer }
266a3998799SMike Snitzer 
2673407ef52SJosef Bacik static int flakey_map(struct dm_target *ti, struct bio *bio,
2683407ef52SJosef Bacik 		      union map_info *map_context)
2693407ef52SJosef Bacik {
2703407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2713407ef52SJosef Bacik 	unsigned elapsed;
2723407ef52SJosef Bacik 
2733407ef52SJosef Bacik 	/* Are we alive ? */
2743407ef52SJosef Bacik 	elapsed = (jiffies - fc->start_time) / HZ;
275b26f5e3dSMike Snitzer 	if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
276a3998799SMike Snitzer 		/*
277a3998799SMike Snitzer 		 * Flag this bio as submitted while down.
278a3998799SMike Snitzer 		 */
279a3998799SMike Snitzer 		map_context->ll = 1;
2803407ef52SJosef Bacik 
281b26f5e3dSMike Snitzer 		/*
282a3998799SMike Snitzer 		 * Map reads as normal.
283a3998799SMike Snitzer 		 */
284a3998799SMike Snitzer 		if (bio_data_dir(bio) == READ)
285a3998799SMike Snitzer 			goto map_bio;
286a3998799SMike Snitzer 
287a3998799SMike Snitzer 		/*
288a3998799SMike Snitzer 		 * Drop writes?
289b26f5e3dSMike Snitzer 		 */
290b26f5e3dSMike Snitzer 		if (test_bit(DROP_WRITES, &fc->flags)) {
291b26f5e3dSMike Snitzer 			bio_endio(bio, 0);
292b26f5e3dSMike Snitzer 			return DM_MAPIO_SUBMITTED;
293b26f5e3dSMike Snitzer 		}
294a3998799SMike Snitzer 
295a3998799SMike Snitzer 		/*
296a3998799SMike Snitzer 		 * Corrupt matching writes.
297a3998799SMike Snitzer 		 */
298a3998799SMike Snitzer 		if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
299a3998799SMike Snitzer 			if (all_corrupt_bio_flags_match(bio, fc))
300a3998799SMike Snitzer 				corrupt_bio_data(bio, fc);
301b26f5e3dSMike Snitzer 			goto map_bio;
302b26f5e3dSMike Snitzer 		}
303b26f5e3dSMike Snitzer 
304b26f5e3dSMike Snitzer 		/*
305a3998799SMike Snitzer 		 * By default, error all I/O.
306b26f5e3dSMike Snitzer 		 */
307b26f5e3dSMike Snitzer 		return -EIO;
308b26f5e3dSMike Snitzer 	}
309b26f5e3dSMike Snitzer 
310b26f5e3dSMike Snitzer map_bio:
3113407ef52SJosef Bacik 	flakey_map_bio(ti, bio);
3123407ef52SJosef Bacik 
3133407ef52SJosef Bacik 	return DM_MAPIO_REMAPPED;
3143407ef52SJosef Bacik }
3153407ef52SJosef Bacik 
316a3998799SMike Snitzer static int flakey_end_io(struct dm_target *ti, struct bio *bio,
317a3998799SMike Snitzer 			 int error, union map_info *map_context)
318a3998799SMike Snitzer {
319a3998799SMike Snitzer 	struct flakey_c *fc = ti->private;
320a3998799SMike Snitzer 	unsigned bio_submitted_while_down = map_context->ll;
321a3998799SMike Snitzer 
322a3998799SMike Snitzer 	/*
323a3998799SMike Snitzer 	 * Corrupt successful READs while in down state.
324a3998799SMike Snitzer 	 * If flags were specified, only corrupt those that match.
325a3998799SMike Snitzer 	 */
326a3998799SMike Snitzer 	if (!error && bio_submitted_while_down &&
327a3998799SMike Snitzer 	    (bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
328a3998799SMike Snitzer 	    all_corrupt_bio_flags_match(bio, fc))
329a3998799SMike Snitzer 		corrupt_bio_data(bio, fc);
330a3998799SMike Snitzer 
331a3998799SMike Snitzer 	return error;
332a3998799SMike Snitzer }
333a3998799SMike Snitzer 
3343407ef52SJosef Bacik static int flakey_status(struct dm_target *ti, status_type_t type,
3353407ef52SJosef Bacik 			 char *result, unsigned int maxlen)
3363407ef52SJosef Bacik {
337b26f5e3dSMike Snitzer 	unsigned sz = 0;
3383407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
339b26f5e3dSMike Snitzer 	unsigned drop_writes;
3403407ef52SJosef Bacik 
3413407ef52SJosef Bacik 	switch (type) {
3423407ef52SJosef Bacik 	case STATUSTYPE_INFO:
3433407ef52SJosef Bacik 		result[0] = '\0';
3443407ef52SJosef Bacik 		break;
3453407ef52SJosef Bacik 
3463407ef52SJosef Bacik 	case STATUSTYPE_TABLE:
347b26f5e3dSMike Snitzer 		DMEMIT("%s %llu %u %u ", fc->dev->name,
3483407ef52SJosef Bacik 		       (unsigned long long)fc->start, fc->up_interval,
3493407ef52SJosef Bacik 		       fc->down_interval);
350b26f5e3dSMike Snitzer 
351b26f5e3dSMike Snitzer 		drop_writes = test_bit(DROP_WRITES, &fc->flags);
352a3998799SMike Snitzer 		DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
353a3998799SMike Snitzer 
354b26f5e3dSMike Snitzer 		if (drop_writes)
355b26f5e3dSMike Snitzer 			DMEMIT("drop_writes ");
356a3998799SMike Snitzer 
357a3998799SMike Snitzer 		if (fc->corrupt_bio_byte)
358a3998799SMike Snitzer 			DMEMIT("corrupt_bio_byte %u %c %u %u ",
359a3998799SMike Snitzer 			       fc->corrupt_bio_byte,
360a3998799SMike Snitzer 			       (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
361a3998799SMike Snitzer 			       fc->corrupt_bio_value, fc->corrupt_bio_flags);
362a3998799SMike Snitzer 
3633407ef52SJosef Bacik 		break;
3643407ef52SJosef Bacik 	}
3653407ef52SJosef Bacik 	return 0;
3663407ef52SJosef Bacik }
3673407ef52SJosef Bacik 
3683407ef52SJosef Bacik static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
3693407ef52SJosef Bacik {
3703407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
3713407ef52SJosef Bacik 
3723407ef52SJosef Bacik 	return __blkdev_driver_ioctl(fc->dev->bdev, fc->dev->mode, cmd, arg);
3733407ef52SJosef Bacik }
3743407ef52SJosef Bacik 
3753407ef52SJosef Bacik static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3763407ef52SJosef Bacik 			struct bio_vec *biovec, int max_size)
3773407ef52SJosef Bacik {
3783407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
3793407ef52SJosef Bacik 	struct request_queue *q = bdev_get_queue(fc->dev->bdev);
3803407ef52SJosef Bacik 
3813407ef52SJosef Bacik 	if (!q->merge_bvec_fn)
3823407ef52SJosef Bacik 		return max_size;
3833407ef52SJosef Bacik 
3843407ef52SJosef Bacik 	bvm->bi_bdev = fc->dev->bdev;
3853407ef52SJosef Bacik 	bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);
3863407ef52SJosef Bacik 
3873407ef52SJosef Bacik 	return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3883407ef52SJosef Bacik }
3893407ef52SJosef Bacik 
3903407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
3913407ef52SJosef Bacik {
3923407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
3933407ef52SJosef Bacik 
3943407ef52SJosef Bacik 	return fn(ti, fc->dev, fc->start, ti->len, data);
3953407ef52SJosef Bacik }
3963407ef52SJosef Bacik 
3973407ef52SJosef Bacik static struct target_type flakey_target = {
3983407ef52SJosef Bacik 	.name   = "flakey",
399dfd068b0SMike Snitzer 	.version = {1, 2, 0},
4003407ef52SJosef Bacik 	.module = THIS_MODULE,
4013407ef52SJosef Bacik 	.ctr    = flakey_ctr,
4023407ef52SJosef Bacik 	.dtr    = flakey_dtr,
4033407ef52SJosef Bacik 	.map    = flakey_map,
404a3998799SMike Snitzer 	.end_io = flakey_end_io,
4053407ef52SJosef Bacik 	.status = flakey_status,
4063407ef52SJosef Bacik 	.ioctl	= flakey_ioctl,
4073407ef52SJosef Bacik 	.merge	= flakey_merge,
4083407ef52SJosef Bacik 	.iterate_devices = flakey_iterate_devices,
4093407ef52SJosef Bacik };
4103407ef52SJosef Bacik 
4113407ef52SJosef Bacik static int __init dm_flakey_init(void)
4123407ef52SJosef Bacik {
4133407ef52SJosef Bacik 	int r = dm_register_target(&flakey_target);
4143407ef52SJosef Bacik 
4153407ef52SJosef Bacik 	if (r < 0)
4163407ef52SJosef Bacik 		DMERR("register failed %d", r);
4173407ef52SJosef Bacik 
4183407ef52SJosef Bacik 	return r;
4193407ef52SJosef Bacik }
4203407ef52SJosef Bacik 
4213407ef52SJosef Bacik static void __exit dm_flakey_exit(void)
4223407ef52SJosef Bacik {
4233407ef52SJosef Bacik 	dm_unregister_target(&flakey_target);
4243407ef52SJosef Bacik }
4253407ef52SJosef Bacik 
4263407ef52SJosef Bacik /* Module hooks */
4273407ef52SJosef Bacik module_init(dm_flakey_init);
4283407ef52SJosef Bacik module_exit(dm_flakey_exit);
4293407ef52SJosef Bacik 
4303407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target");
4313407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4323407ef52SJosef Bacik MODULE_LICENSE("GPL");
433