xref: /openbmc/linux/drivers/md/dm-flakey.c (revision 7de3ee57da4b717050e79c9313a9bf66ccc72519)
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 
42c7cfdf59SMikulas Patocka struct per_bio_data {
43c7cfdf59SMikulas Patocka 	bool bio_submitted;
44c7cfdf59SMikulas Patocka };
45c7cfdf59SMikulas Patocka 
46b26f5e3dSMike Snitzer static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
47b26f5e3dSMike Snitzer 			  struct dm_target *ti)
48dfd068b0SMike Snitzer {
49dfd068b0SMike Snitzer 	int r;
50dfd068b0SMike Snitzer 	unsigned argc;
51dfd068b0SMike Snitzer 	const char *arg_name;
52dfd068b0SMike Snitzer 
53dfd068b0SMike Snitzer 	static struct dm_arg _args[] = {
54a3998799SMike Snitzer 		{0, 6, "Invalid number of feature args"},
55a3998799SMike Snitzer 		{1, UINT_MAX, "Invalid corrupt bio byte"},
56a3998799SMike Snitzer 		{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
57a3998799SMike Snitzer 		{0, UINT_MAX, "Invalid corrupt bio flags mask"},
58dfd068b0SMike Snitzer 	};
59dfd068b0SMike Snitzer 
60dfd068b0SMike Snitzer 	/* No feature arguments supplied. */
61dfd068b0SMike Snitzer 	if (!as->argc)
62dfd068b0SMike Snitzer 		return 0;
63dfd068b0SMike Snitzer 
64dfd068b0SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
65dfd068b0SMike Snitzer 	if (r)
66a3998799SMike Snitzer 		return r;
67dfd068b0SMike Snitzer 
68a3998799SMike Snitzer 	while (argc) {
69dfd068b0SMike Snitzer 		arg_name = dm_shift_arg(as);
70dfd068b0SMike Snitzer 		argc--;
71dfd068b0SMike Snitzer 
72b26f5e3dSMike Snitzer 		/*
73b26f5e3dSMike Snitzer 		 * drop_writes
74b26f5e3dSMike Snitzer 		 */
75b26f5e3dSMike Snitzer 		if (!strcasecmp(arg_name, "drop_writes")) {
76b26f5e3dSMike Snitzer 			if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
77b26f5e3dSMike Snitzer 				ti->error = "Feature drop_writes duplicated";
78b26f5e3dSMike Snitzer 				return -EINVAL;
79b26f5e3dSMike Snitzer 			}
80b26f5e3dSMike Snitzer 
81b26f5e3dSMike Snitzer 			continue;
82b26f5e3dSMike Snitzer 		}
83b26f5e3dSMike Snitzer 
84a3998799SMike Snitzer 		/*
85a3998799SMike Snitzer 		 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
86a3998799SMike Snitzer 		 */
87a3998799SMike Snitzer 		if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
8868e58a29SMike Snitzer 			if (!argc) {
89a3998799SMike Snitzer 				ti->error = "Feature corrupt_bio_byte requires parameters";
9068e58a29SMike Snitzer 				return -EINVAL;
9168e58a29SMike Snitzer 			}
92a3998799SMike Snitzer 
93a3998799SMike Snitzer 			r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
94a3998799SMike Snitzer 			if (r)
95a3998799SMike Snitzer 				return r;
96a3998799SMike Snitzer 			argc--;
97a3998799SMike Snitzer 
98a3998799SMike Snitzer 			/*
99a3998799SMike Snitzer 			 * Direction r or w?
100a3998799SMike Snitzer 			 */
101a3998799SMike Snitzer 			arg_name = dm_shift_arg(as);
102a3998799SMike Snitzer 			if (!strcasecmp(arg_name, "w"))
103a3998799SMike Snitzer 				fc->corrupt_bio_rw = WRITE;
104a3998799SMike Snitzer 			else if (!strcasecmp(arg_name, "r"))
105a3998799SMike Snitzer 				fc->corrupt_bio_rw = READ;
106a3998799SMike Snitzer 			else {
107a3998799SMike Snitzer 				ti->error = "Invalid corrupt bio direction (r or w)";
108a3998799SMike Snitzer 				return -EINVAL;
109a3998799SMike Snitzer 			}
110a3998799SMike Snitzer 			argc--;
111a3998799SMike Snitzer 
112a3998799SMike Snitzer 			/*
113a3998799SMike Snitzer 			 * Value of byte (0-255) to write in place of correct one.
114a3998799SMike Snitzer 			 */
115a3998799SMike Snitzer 			r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
116a3998799SMike Snitzer 			if (r)
117a3998799SMike Snitzer 				return r;
118a3998799SMike Snitzer 			argc--;
119a3998799SMike Snitzer 
120a3998799SMike Snitzer 			/*
121a3998799SMike Snitzer 			 * Only corrupt bios with these flags set.
122a3998799SMike Snitzer 			 */
123a3998799SMike Snitzer 			r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
124a3998799SMike Snitzer 			if (r)
125a3998799SMike Snitzer 				return r;
126a3998799SMike Snitzer 			argc--;
127a3998799SMike Snitzer 
128a3998799SMike Snitzer 			continue;
129dfd068b0SMike Snitzer 		}
130dfd068b0SMike Snitzer 
131a3998799SMike Snitzer 		ti->error = "Unrecognised flakey feature requested";
132a3998799SMike Snitzer 		return -EINVAL;
133a3998799SMike Snitzer 	}
134a3998799SMike Snitzer 
135a3998799SMike Snitzer 	if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
136a3998799SMike Snitzer 		ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
137a3998799SMike Snitzer 		return -EINVAL;
138a3998799SMike Snitzer 	}
139a3998799SMike Snitzer 
140a3998799SMike Snitzer 	return 0;
141dfd068b0SMike Snitzer }
142dfd068b0SMike Snitzer 
1433407ef52SJosef Bacik /*
144dfd068b0SMike Snitzer  * Construct a flakey mapping:
145dfd068b0SMike Snitzer  * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
146b26f5e3dSMike Snitzer  *
147b26f5e3dSMike Snitzer  *   Feature args:
148b26f5e3dSMike Snitzer  *     [drop_writes]
149a3998799SMike Snitzer  *     [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
150a3998799SMike Snitzer  *
151a3998799SMike Snitzer  *   Nth_byte starts from 1 for the first byte.
152a3998799SMike Snitzer  *   Direction is r for READ or w for WRITE.
153a3998799SMike Snitzer  *   bio_flags is ignored if 0.
1543407ef52SJosef Bacik  */
1553407ef52SJosef Bacik static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1563407ef52SJosef Bacik {
157dfd068b0SMike Snitzer 	static struct dm_arg _args[] = {
158dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid up interval"},
159dfd068b0SMike Snitzer 		{0, UINT_MAX, "Invalid down interval"},
160dfd068b0SMike Snitzer 	};
1613407ef52SJosef Bacik 
162dfd068b0SMike Snitzer 	int r;
163dfd068b0SMike Snitzer 	struct flakey_c *fc;
164dfd068b0SMike Snitzer 	unsigned long long tmpll;
165dfd068b0SMike Snitzer 	struct dm_arg_set as;
166dfd068b0SMike Snitzer 	const char *devname;
16731998ef1SMikulas Patocka 	char dummy;
168dfd068b0SMike Snitzer 
169dfd068b0SMike Snitzer 	as.argc = argc;
170dfd068b0SMike Snitzer 	as.argv = argv;
171dfd068b0SMike Snitzer 
172dfd068b0SMike Snitzer 	if (argc < 4) {
173dfd068b0SMike Snitzer 		ti->error = "Invalid argument count";
1743407ef52SJosef Bacik 		return -EINVAL;
1753407ef52SJosef Bacik 	}
1763407ef52SJosef Bacik 
177b26f5e3dSMike Snitzer 	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
1783407ef52SJosef Bacik 	if (!fc) {
179dfd068b0SMike Snitzer 		ti->error = "Cannot allocate linear context";
1803407ef52SJosef Bacik 		return -ENOMEM;
1813407ef52SJosef Bacik 	}
1823407ef52SJosef Bacik 	fc->start_time = jiffies;
1833407ef52SJosef Bacik 
184dfd068b0SMike Snitzer 	devname = dm_shift_arg(&as);
1853407ef52SJosef Bacik 
18631998ef1SMikulas Patocka 	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
187dfd068b0SMike Snitzer 		ti->error = "Invalid device sector";
1883407ef52SJosef Bacik 		goto bad;
1893407ef52SJosef Bacik 	}
190dfd068b0SMike Snitzer 	fc->start = tmpll;
1913407ef52SJosef Bacik 
192dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
193dfd068b0SMike Snitzer 	if (r)
1943407ef52SJosef Bacik 		goto bad;
195dfd068b0SMike Snitzer 
196dfd068b0SMike Snitzer 	r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
197dfd068b0SMike Snitzer 	if (r)
198dfd068b0SMike Snitzer 		goto bad;
1993407ef52SJosef Bacik 
2003407ef52SJosef Bacik 	if (!(fc->up_interval + fc->down_interval)) {
201dfd068b0SMike Snitzer 		ti->error = "Total (up + down) interval is zero";
2023407ef52SJosef Bacik 		goto bad;
2033407ef52SJosef Bacik 	}
2043407ef52SJosef Bacik 
2053407ef52SJosef Bacik 	if (fc->up_interval + fc->down_interval < fc->up_interval) {
206dfd068b0SMike Snitzer 		ti->error = "Interval overflow";
2073407ef52SJosef Bacik 		goto bad;
2083407ef52SJosef Bacik 	}
2093407ef52SJosef Bacik 
210b26f5e3dSMike Snitzer 	r = parse_features(&as, fc, ti);
211dfd068b0SMike Snitzer 	if (r)
212dfd068b0SMike Snitzer 		goto bad;
213dfd068b0SMike Snitzer 
214dfd068b0SMike Snitzer 	if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev)) {
215dfd068b0SMike Snitzer 		ti->error = "Device lookup failed";
2163407ef52SJosef Bacik 		goto bad;
2173407ef52SJosef Bacik 	}
2183407ef52SJosef Bacik 
2193407ef52SJosef Bacik 	ti->num_flush_requests = 1;
22030e4171bSMike Snitzer 	ti->num_discard_requests = 1;
221c7cfdf59SMikulas Patocka 	ti->per_bio_data_size = sizeof(struct per_bio_data);
2223407ef52SJosef Bacik 	ti->private = fc;
2233407ef52SJosef Bacik 	return 0;
2243407ef52SJosef Bacik 
2253407ef52SJosef Bacik bad:
2263407ef52SJosef Bacik 	kfree(fc);
2273407ef52SJosef Bacik 	return -EINVAL;
2283407ef52SJosef Bacik }
2293407ef52SJosef Bacik 
2303407ef52SJosef Bacik static void flakey_dtr(struct dm_target *ti)
2313407ef52SJosef Bacik {
2323407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2333407ef52SJosef Bacik 
2343407ef52SJosef Bacik 	dm_put_device(ti, fc->dev);
2353407ef52SJosef Bacik 	kfree(fc);
2363407ef52SJosef Bacik }
2373407ef52SJosef Bacik 
2383407ef52SJosef Bacik static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
2393407ef52SJosef Bacik {
2403407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2413407ef52SJosef Bacik 
24230e4171bSMike Snitzer 	return fc->start + dm_target_offset(ti, bi_sector);
2433407ef52SJosef Bacik }
2443407ef52SJosef Bacik 
2453407ef52SJosef Bacik static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
2463407ef52SJosef Bacik {
2473407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2483407ef52SJosef Bacik 
2493407ef52SJosef Bacik 	bio->bi_bdev = fc->dev->bdev;
2503407ef52SJosef Bacik 	if (bio_sectors(bio))
2513407ef52SJosef Bacik 		bio->bi_sector = flakey_map_sector(ti, bio->bi_sector);
2523407ef52SJosef Bacik }
2533407ef52SJosef Bacik 
254a3998799SMike Snitzer static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
255a3998799SMike Snitzer {
256a3998799SMike Snitzer 	unsigned bio_bytes = bio_cur_bytes(bio);
257a3998799SMike Snitzer 	char *data = bio_data(bio);
258a3998799SMike Snitzer 
259a3998799SMike Snitzer 	/*
260a3998799SMike Snitzer 	 * Overwrite the Nth byte of the data returned.
261a3998799SMike Snitzer 	 */
262a3998799SMike Snitzer 	if (data && bio_bytes >= fc->corrupt_bio_byte) {
263a3998799SMike Snitzer 		data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
264a3998799SMike Snitzer 
265a3998799SMike Snitzer 		DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
266a3998799SMike Snitzer 			"(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
267a3998799SMike Snitzer 			bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
268a3998799SMike Snitzer 			(bio_data_dir(bio) == WRITE) ? 'w' : 'r',
269a3998799SMike Snitzer 			bio->bi_rw, (unsigned long long)bio->bi_sector, bio_bytes);
270a3998799SMike Snitzer 	}
271a3998799SMike Snitzer }
272a3998799SMike Snitzer 
273*7de3ee57SMikulas Patocka static int flakey_map(struct dm_target *ti, struct bio *bio)
2743407ef52SJosef Bacik {
2753407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
2763407ef52SJosef Bacik 	unsigned elapsed;
277c7cfdf59SMikulas Patocka 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
278c7cfdf59SMikulas Patocka 	pb->bio_submitted = false;
2793407ef52SJosef Bacik 
2803407ef52SJosef Bacik 	/* Are we alive ? */
2813407ef52SJosef Bacik 	elapsed = (jiffies - fc->start_time) / HZ;
282b26f5e3dSMike Snitzer 	if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
283a3998799SMike Snitzer 		/*
284a3998799SMike Snitzer 		 * Flag this bio as submitted while down.
285a3998799SMike Snitzer 		 */
286c7cfdf59SMikulas Patocka 		pb->bio_submitted = true;
2873407ef52SJosef Bacik 
288b26f5e3dSMike Snitzer 		/*
289a3998799SMike Snitzer 		 * Map reads as normal.
290a3998799SMike Snitzer 		 */
291a3998799SMike Snitzer 		if (bio_data_dir(bio) == READ)
292a3998799SMike Snitzer 			goto map_bio;
293a3998799SMike Snitzer 
294a3998799SMike Snitzer 		/*
295a3998799SMike Snitzer 		 * Drop writes?
296b26f5e3dSMike Snitzer 		 */
297b26f5e3dSMike Snitzer 		if (test_bit(DROP_WRITES, &fc->flags)) {
298b26f5e3dSMike Snitzer 			bio_endio(bio, 0);
299b26f5e3dSMike Snitzer 			return DM_MAPIO_SUBMITTED;
300b26f5e3dSMike Snitzer 		}
301a3998799SMike Snitzer 
302a3998799SMike Snitzer 		/*
303a3998799SMike Snitzer 		 * Corrupt matching writes.
304a3998799SMike Snitzer 		 */
305a3998799SMike Snitzer 		if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
306a3998799SMike Snitzer 			if (all_corrupt_bio_flags_match(bio, fc))
307a3998799SMike Snitzer 				corrupt_bio_data(bio, fc);
308b26f5e3dSMike Snitzer 			goto map_bio;
309b26f5e3dSMike Snitzer 		}
310b26f5e3dSMike Snitzer 
311b26f5e3dSMike Snitzer 		/*
312a3998799SMike Snitzer 		 * By default, error all I/O.
313b26f5e3dSMike Snitzer 		 */
314b26f5e3dSMike Snitzer 		return -EIO;
315b26f5e3dSMike Snitzer 	}
316b26f5e3dSMike Snitzer 
317b26f5e3dSMike Snitzer map_bio:
3183407ef52SJosef Bacik 	flakey_map_bio(ti, bio);
3193407ef52SJosef Bacik 
3203407ef52SJosef Bacik 	return DM_MAPIO_REMAPPED;
3213407ef52SJosef Bacik }
3223407ef52SJosef Bacik 
323*7de3ee57SMikulas Patocka static int flakey_end_io(struct dm_target *ti, struct bio *bio, int error)
324a3998799SMike Snitzer {
325a3998799SMike Snitzer 	struct flakey_c *fc = ti->private;
326c7cfdf59SMikulas Patocka 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
327a3998799SMike Snitzer 
328a3998799SMike Snitzer 	/*
329a3998799SMike Snitzer 	 * Corrupt successful READs while in down state.
330a3998799SMike Snitzer 	 * If flags were specified, only corrupt those that match.
331a3998799SMike Snitzer 	 */
332c7cfdf59SMikulas Patocka 	if (fc->corrupt_bio_byte && !error && pb->bio_submitted &&
333a3998799SMike Snitzer 	    (bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
334a3998799SMike Snitzer 	    all_corrupt_bio_flags_match(bio, fc))
335a3998799SMike Snitzer 		corrupt_bio_data(bio, fc);
336a3998799SMike Snitzer 
337a3998799SMike Snitzer 	return error;
338a3998799SMike Snitzer }
339a3998799SMike Snitzer 
3403407ef52SJosef Bacik static int flakey_status(struct dm_target *ti, status_type_t type,
3411f4e0ff0SAlasdair G Kergon 			 unsigned status_flags, char *result, unsigned maxlen)
3423407ef52SJosef Bacik {
343b26f5e3dSMike Snitzer 	unsigned sz = 0;
3443407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
345b26f5e3dSMike Snitzer 	unsigned drop_writes;
3463407ef52SJosef Bacik 
3473407ef52SJosef Bacik 	switch (type) {
3483407ef52SJosef Bacik 	case STATUSTYPE_INFO:
3493407ef52SJosef Bacik 		result[0] = '\0';
3503407ef52SJosef Bacik 		break;
3513407ef52SJosef Bacik 
3523407ef52SJosef Bacik 	case STATUSTYPE_TABLE:
353b26f5e3dSMike Snitzer 		DMEMIT("%s %llu %u %u ", fc->dev->name,
3543407ef52SJosef Bacik 		       (unsigned long long)fc->start, fc->up_interval,
3553407ef52SJosef Bacik 		       fc->down_interval);
356b26f5e3dSMike Snitzer 
357b26f5e3dSMike Snitzer 		drop_writes = test_bit(DROP_WRITES, &fc->flags);
358a3998799SMike Snitzer 		DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
359a3998799SMike Snitzer 
360b26f5e3dSMike Snitzer 		if (drop_writes)
361b26f5e3dSMike Snitzer 			DMEMIT("drop_writes ");
362a3998799SMike Snitzer 
363a3998799SMike Snitzer 		if (fc->corrupt_bio_byte)
364a3998799SMike Snitzer 			DMEMIT("corrupt_bio_byte %u %c %u %u ",
365a3998799SMike Snitzer 			       fc->corrupt_bio_byte,
366a3998799SMike Snitzer 			       (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
367a3998799SMike Snitzer 			       fc->corrupt_bio_value, fc->corrupt_bio_flags);
368a3998799SMike Snitzer 
3693407ef52SJosef Bacik 		break;
3703407ef52SJosef Bacik 	}
3713407ef52SJosef Bacik 	return 0;
3723407ef52SJosef Bacik }
3733407ef52SJosef Bacik 
3743407ef52SJosef Bacik static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
3753407ef52SJosef Bacik {
3763407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
377ec8013beSPaolo Bonzini 	struct dm_dev *dev = fc->dev;
378ec8013beSPaolo Bonzini 	int r = 0;
3793407ef52SJosef Bacik 
380ec8013beSPaolo Bonzini 	/*
381ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
382ec8013beSPaolo Bonzini 	 */
383ec8013beSPaolo Bonzini 	if (fc->start ||
384ec8013beSPaolo Bonzini 	    ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
385ec8013beSPaolo Bonzini 		r = scsi_verify_blk_ioctl(NULL, cmd);
386ec8013beSPaolo Bonzini 
387ec8013beSPaolo Bonzini 	return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg);
3883407ef52SJosef Bacik }
3893407ef52SJosef Bacik 
3903407ef52SJosef Bacik static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3913407ef52SJosef Bacik 			struct bio_vec *biovec, int max_size)
3923407ef52SJosef Bacik {
3933407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
3943407ef52SJosef Bacik 	struct request_queue *q = bdev_get_queue(fc->dev->bdev);
3953407ef52SJosef Bacik 
3963407ef52SJosef Bacik 	if (!q->merge_bvec_fn)
3973407ef52SJosef Bacik 		return max_size;
3983407ef52SJosef Bacik 
3993407ef52SJosef Bacik 	bvm->bi_bdev = fc->dev->bdev;
4003407ef52SJosef Bacik 	bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);
4013407ef52SJosef Bacik 
4023407ef52SJosef Bacik 	return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4033407ef52SJosef Bacik }
4043407ef52SJosef Bacik 
4053407ef52SJosef Bacik static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
4063407ef52SJosef Bacik {
4073407ef52SJosef Bacik 	struct flakey_c *fc = ti->private;
4083407ef52SJosef Bacik 
4093407ef52SJosef Bacik 	return fn(ti, fc->dev, fc->start, ti->len, data);
4103407ef52SJosef Bacik }
4113407ef52SJosef Bacik 
4123407ef52SJosef Bacik static struct target_type flakey_target = {
4133407ef52SJosef Bacik 	.name   = "flakey",
414c7cfdf59SMikulas Patocka 	.version = {1, 3, 0},
4153407ef52SJosef Bacik 	.module = THIS_MODULE,
4163407ef52SJosef Bacik 	.ctr    = flakey_ctr,
4173407ef52SJosef Bacik 	.dtr    = flakey_dtr,
4183407ef52SJosef Bacik 	.map    = flakey_map,
419a3998799SMike Snitzer 	.end_io = flakey_end_io,
4203407ef52SJosef Bacik 	.status = flakey_status,
4213407ef52SJosef Bacik 	.ioctl	= flakey_ioctl,
4223407ef52SJosef Bacik 	.merge	= flakey_merge,
4233407ef52SJosef Bacik 	.iterate_devices = flakey_iterate_devices,
4243407ef52SJosef Bacik };
4253407ef52SJosef Bacik 
4263407ef52SJosef Bacik static int __init dm_flakey_init(void)
4273407ef52SJosef Bacik {
4283407ef52SJosef Bacik 	int r = dm_register_target(&flakey_target);
4293407ef52SJosef Bacik 
4303407ef52SJosef Bacik 	if (r < 0)
4313407ef52SJosef Bacik 		DMERR("register failed %d", r);
4323407ef52SJosef Bacik 
4333407ef52SJosef Bacik 	return r;
4343407ef52SJosef Bacik }
4353407ef52SJosef Bacik 
4363407ef52SJosef Bacik static void __exit dm_flakey_exit(void)
4373407ef52SJosef Bacik {
4383407ef52SJosef Bacik 	dm_unregister_target(&flakey_target);
4393407ef52SJosef Bacik }
4403407ef52SJosef Bacik 
4413407ef52SJosef Bacik /* Module hooks */
4423407ef52SJosef Bacik module_init(dm_flakey_init);
4433407ef52SJosef Bacik module_exit(dm_flakey_exit);
4443407ef52SJosef Bacik 
4453407ef52SJosef Bacik MODULE_DESCRIPTION(DM_NAME " flakey target");
4463407ef52SJosef Bacik MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4473407ef52SJosef Bacik MODULE_LICENSE("GPL");
448