dm-flakey.c 9.8 KB
Newer Older
J
Josef Bacik 已提交
1 2
/*
 * Copyright (C) 2003 Sistina Software (UK) Limited.
3
 * Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
J
Josef Bacik 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This file is released under the GPL.
 */

#include <linux/device-mapper.h>

#include <linux/module.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/slab.h>

#define DM_MSG_PREFIX "flakey"

18 19 20
#define all_corrupt_bio_flags_match(bio, fc)	\
	(((bio)->bi_rw & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)

J
Josef Bacik 已提交
21 22 23 24 25 26 27 28 29 30
/*
 * Flakey: Used for testing only, simulates intermittent,
 * catastrophic device failure.
 */
struct flakey_c {
	struct dm_dev *dev;
	unsigned long start_time;
	sector_t start;
	unsigned up_interval;
	unsigned down_interval;
M
Mike Snitzer 已提交
31
	unsigned long flags;
32 33 34 35
	unsigned corrupt_bio_byte;
	unsigned corrupt_bio_rw;
	unsigned corrupt_bio_value;
	unsigned corrupt_bio_flags;
J
Josef Bacik 已提交
36 37
};

M
Mike Snitzer 已提交
38 39 40 41 42 43
enum feature_flag_bits {
	DROP_WRITES
};

static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
			  struct dm_target *ti)
M
Mike Snitzer 已提交
44 45 46 47 48 49
{
	int r;
	unsigned argc;
	const char *arg_name;

	static struct dm_arg _args[] = {
50 51 52 53
		{0, 6, "Invalid number of feature args"},
		{1, UINT_MAX, "Invalid corrupt bio byte"},
		{0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
		{0, UINT_MAX, "Invalid corrupt bio flags mask"},
M
Mike Snitzer 已提交
54 55 56 57 58 59 60 61
	};

	/* No feature arguments supplied. */
	if (!as->argc)
		return 0;

	r = dm_read_arg_group(_args, as, &argc, &ti->error);
	if (r)
62
		return r;
M
Mike Snitzer 已提交
63

64
	while (argc) {
M
Mike Snitzer 已提交
65 66 67
		arg_name = dm_shift_arg(as);
		argc--;

M
Mike Snitzer 已提交
68 69 70 71 72 73 74 75 76 77 78 79
		/*
		 * drop_writes
		 */
		if (!strcasecmp(arg_name, "drop_writes")) {
			if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
				ti->error = "Feature drop_writes duplicated";
				return -EINVAL;
			}

			continue;
		}

80 81 82 83
		/*
		 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
		 */
		if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
84
			if (!argc) {
85
				ti->error = "Feature corrupt_bio_byte requires parameters";
86 87
				return -EINVAL;
			}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

			r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
			if (r)
				return r;
			argc--;

			/*
			 * Direction r or w?
			 */
			arg_name = dm_shift_arg(as);
			if (!strcasecmp(arg_name, "w"))
				fc->corrupt_bio_rw = WRITE;
			else if (!strcasecmp(arg_name, "r"))
				fc->corrupt_bio_rw = READ;
			else {
				ti->error = "Invalid corrupt bio direction (r or w)";
				return -EINVAL;
			}
			argc--;

			/*
			 * Value of byte (0-255) to write in place of correct one.
			 */
			r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
			if (r)
				return r;
			argc--;

			/*
			 * Only corrupt bios with these flags set.
			 */
			r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
			if (r)
				return r;
			argc--;

			continue;
		}

M
Mike Snitzer 已提交
127
		ti->error = "Unrecognised flakey feature requested";
128
		return -EINVAL;
M
Mike Snitzer 已提交
129 130
	}

131 132 133 134 135 136
	if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
		ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
		return -EINVAL;
	}

	return 0;
M
Mike Snitzer 已提交
137 138
}

J
Josef Bacik 已提交
139
/*
M
Mike Snitzer 已提交
140 141
 * Construct a flakey mapping:
 * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
M
Mike Snitzer 已提交
142 143 144
 *
 *   Feature args:
 *     [drop_writes]
145 146 147 148 149
 *     [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
 *
 *   Nth_byte starts from 1 for the first byte.
 *   Direction is r for READ or w for WRITE.
 *   bio_flags is ignored if 0.
J
Josef Bacik 已提交
150 151 152
 */
static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
M
Mike Snitzer 已提交
153 154 155 156 157 158
	static struct dm_arg _args[] = {
		{0, UINT_MAX, "Invalid up interval"},
		{0, UINT_MAX, "Invalid down interval"},
	};

	int r;
J
Josef Bacik 已提交
159
	struct flakey_c *fc;
M
Mike Snitzer 已提交
160 161 162
	unsigned long long tmpll;
	struct dm_arg_set as;
	const char *devname;
163
	char dummy;
J
Josef Bacik 已提交
164

M
Mike Snitzer 已提交
165 166 167 168 169
	as.argc = argc;
	as.argv = argv;

	if (argc < 4) {
		ti->error = "Invalid argument count";
J
Josef Bacik 已提交
170 171 172
		return -EINVAL;
	}

M
Mike Snitzer 已提交
173
	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
J
Josef Bacik 已提交
174
	if (!fc) {
M
Mike Snitzer 已提交
175
		ti->error = "Cannot allocate linear context";
J
Josef Bacik 已提交
176 177 178 179
		return -ENOMEM;
	}
	fc->start_time = jiffies;

M
Mike Snitzer 已提交
180 181
	devname = dm_shift_arg(&as);

182
	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
M
Mike Snitzer 已提交
183
		ti->error = "Invalid device sector";
J
Josef Bacik 已提交
184 185
		goto bad;
	}
M
Mike Snitzer 已提交
186
	fc->start = tmpll;
J
Josef Bacik 已提交
187

M
Mike Snitzer 已提交
188 189
	r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
	if (r)
J
Josef Bacik 已提交
190 191
		goto bad;

M
Mike Snitzer 已提交
192 193
	r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
	if (r)
J
Josef Bacik 已提交
194 195 196
		goto bad;

	if (!(fc->up_interval + fc->down_interval)) {
M
Mike Snitzer 已提交
197
		ti->error = "Total (up + down) interval is zero";
J
Josef Bacik 已提交
198 199 200 201
		goto bad;
	}

	if (fc->up_interval + fc->down_interval < fc->up_interval) {
M
Mike Snitzer 已提交
202
		ti->error = "Interval overflow";
J
Josef Bacik 已提交
203 204 205
		goto bad;
	}

M
Mike Snitzer 已提交
206
	r = parse_features(&as, fc, ti);
M
Mike Snitzer 已提交
207 208 209 210 211
	if (r)
		goto bad;

	if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev)) {
		ti->error = "Device lookup failed";
J
Josef Bacik 已提交
212 213 214 215
		goto bad;
	}

	ti->num_flush_requests = 1;
216
	ti->num_discard_requests = 1;
J
Josef Bacik 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	ti->private = fc;
	return 0;

bad:
	kfree(fc);
	return -EINVAL;
}

static void flakey_dtr(struct dm_target *ti)
{
	struct flakey_c *fc = ti->private;

	dm_put_device(ti, fc->dev);
	kfree(fc);
}

static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
{
	struct flakey_c *fc = ti->private;

237
	return fc->start + dm_target_offset(ti, bi_sector);
J
Josef Bacik 已提交
238 239 240 241 242 243 244 245 246 247 248
}

static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
{
	struct flakey_c *fc = ti->private;

	bio->bi_bdev = fc->dev->bdev;
	if (bio_sectors(bio))
		bio->bi_sector = flakey_map_sector(ti, bio->bi_sector);
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
{
	unsigned bio_bytes = bio_cur_bytes(bio);
	char *data = bio_data(bio);

	/*
	 * Overwrite the Nth byte of the data returned.
	 */
	if (data && bio_bytes >= fc->corrupt_bio_byte) {
		data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;

		DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
			"(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
			bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
			(bio_data_dir(bio) == WRITE) ? 'w' : 'r',
			bio->bi_rw, (unsigned long long)bio->bi_sector, bio_bytes);
	}
}

J
Josef Bacik 已提交
268 269 270 271 272 273 274 275
static int flakey_map(struct dm_target *ti, struct bio *bio,
		      union map_info *map_context)
{
	struct flakey_c *fc = ti->private;
	unsigned elapsed;

	/* Are we alive ? */
	elapsed = (jiffies - fc->start_time) / HZ;
M
Mike Snitzer 已提交
276
	if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
277 278 279 280 281 282 283 284 285 286
		/*
		 * Flag this bio as submitted while down.
		 */
		map_context->ll = 1;

		/*
		 * Map reads as normal.
		 */
		if (bio_data_dir(bio) == READ)
			goto map_bio;
M
Mike Snitzer 已提交
287 288

		/*
289
		 * Drop writes?
M
Mike Snitzer 已提交
290 291
		 */
		if (test_bit(DROP_WRITES, &fc->flags)) {
292 293 294 295 296 297 298 299 300 301
			bio_endio(bio, 0);
			return DM_MAPIO_SUBMITTED;
		}

		/*
		 * Corrupt matching writes.
		 */
		if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
			if (all_corrupt_bio_flags_match(bio, fc))
				corrupt_bio_data(bio, fc);
M
Mike Snitzer 已提交
302 303 304 305
			goto map_bio;
		}

		/*
306
		 * By default, error all I/O.
M
Mike Snitzer 已提交
307
		 */
J
Josef Bacik 已提交
308
		return -EIO;
M
Mike Snitzer 已提交
309
	}
J
Josef Bacik 已提交
310

M
Mike Snitzer 已提交
311
map_bio:
J
Josef Bacik 已提交
312 313 314 315 316
	flakey_map_bio(ti, bio);

	return DM_MAPIO_REMAPPED;
}

317 318 319 320 321 322 323 324 325 326
static int flakey_end_io(struct dm_target *ti, struct bio *bio,
			 int error, union map_info *map_context)
{
	struct flakey_c *fc = ti->private;
	unsigned bio_submitted_while_down = map_context->ll;

	/*
	 * Corrupt successful READs while in down state.
	 * If flags were specified, only corrupt those that match.
	 */
327
	if (fc->corrupt_bio_byte && !error && bio_submitted_while_down &&
328 329 330 331 332 333 334
	    (bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
	    all_corrupt_bio_flags_match(bio, fc))
		corrupt_bio_data(bio, fc);

	return error;
}

J
Josef Bacik 已提交
335 336 337
static int flakey_status(struct dm_target *ti, status_type_t type,
			 char *result, unsigned int maxlen)
{
M
Mike Snitzer 已提交
338
	unsigned sz = 0;
J
Josef Bacik 已提交
339
	struct flakey_c *fc = ti->private;
M
Mike Snitzer 已提交
340
	unsigned drop_writes;
J
Josef Bacik 已提交
341 342 343 344 345 346 347

	switch (type) {
	case STATUSTYPE_INFO:
		result[0] = '\0';
		break;

	case STATUSTYPE_TABLE:
M
Mike Snitzer 已提交
348 349 350 351 352
		DMEMIT("%s %llu %u %u ", fc->dev->name,
		       (unsigned long long)fc->start, fc->up_interval,
		       fc->down_interval);

		drop_writes = test_bit(DROP_WRITES, &fc->flags);
353 354
		DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);

M
Mike Snitzer 已提交
355 356
		if (drop_writes)
			DMEMIT("drop_writes ");
357 358 359 360 361 362 363

		if (fc->corrupt_bio_byte)
			DMEMIT("corrupt_bio_byte %u %c %u %u ",
			       fc->corrupt_bio_byte,
			       (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
			       fc->corrupt_bio_value, fc->corrupt_bio_flags);

J
Josef Bacik 已提交
364 365 366 367 368 369 370 371
		break;
	}
	return 0;
}

static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
{
	struct flakey_c *fc = ti->private;
372 373
	struct dm_dev *dev = fc->dev;
	int r = 0;
J
Josef Bacik 已提交
374

375 376 377 378 379 380 381 382
	/*
	 * Only pass ioctls through if the device sizes match exactly.
	 */
	if (fc->start ||
	    ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
		r = scsi_verify_blk_ioctl(NULL, cmd);

	return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg);
J
Josef Bacik 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
}

static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
			struct bio_vec *biovec, int max_size)
{
	struct flakey_c *fc = ti->private;
	struct request_queue *q = bdev_get_queue(fc->dev->bdev);

	if (!q->merge_bvec_fn)
		return max_size;

	bvm->bi_bdev = fc->dev->bdev;
	bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);

	return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
}

static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
{
	struct flakey_c *fc = ti->private;

	return fn(ti, fc->dev, fc->start, ti->len, data);
}

static struct target_type flakey_target = {
	.name   = "flakey",
M
Mike Snitzer 已提交
409
	.version = {1, 2, 0},
J
Josef Bacik 已提交
410 411 412 413
	.module = THIS_MODULE,
	.ctr    = flakey_ctr,
	.dtr    = flakey_dtr,
	.map    = flakey_map,
414
	.end_io = flakey_end_io,
J
Josef Bacik 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	.status = flakey_status,
	.ioctl	= flakey_ioctl,
	.merge	= flakey_merge,
	.iterate_devices = flakey_iterate_devices,
};

static int __init dm_flakey_init(void)
{
	int r = dm_register_target(&flakey_target);

	if (r < 0)
		DMERR("register failed %d", r);

	return r;
}

static void __exit dm_flakey_exit(void)
{
	dm_unregister_target(&flakey_target);
}

/* Module hooks */
module_init(dm_flakey_init);
module_exit(dm_flakey_exit);

MODULE_DESCRIPTION(DM_NAME " flakey target");
MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
MODULE_LICENSE("GPL");