Release 4.12 drivers/md/dm-flakey.c
  
  
  
/*
 * Copyright (C) 2003 Sistina Software (UK) Limited.
 * Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
 *
 * 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"
#define all_corrupt_bio_flags_match(bio, fc)	\
	(((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
/*
 * 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;
	
unsigned long flags;
	
unsigned corrupt_bio_byte;
	
unsigned corrupt_bio_rw;
	
unsigned corrupt_bio_value;
	
unsigned corrupt_bio_flags;
};
enum feature_flag_bits {
	
DROP_WRITES,
	
ERROR_WRITES
};
struct per_bio_data {
	
bool bio_submitted;
};
static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
			  struct dm_target *ti)
{
	int r;
	unsigned argc;
	const char *arg_name;
	static struct dm_arg _args[] = {
		{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"},
        };
	/* No feature arguments supplied. */
	if (!as->argc)
		return 0;
	r = dm_read_arg_group(_args, as, &argc, &ti->error);
	if (r)
		return r;
	while (argc) {
		arg_name = dm_shift_arg(as);
		argc--;
		/*
                 * 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;
			} else if (test_bit(ERROR_WRITES, &fc->flags)) {
				ti->error = "Feature drop_writes conflicts with feature error_writes";
				return -EINVAL;
			}
			continue;
		}
		/*
                 * error_writes
                 */
		if (!strcasecmp(arg_name, "error_writes")) {
			if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
				ti->error = "Feature error_writes duplicated";
				return -EINVAL;
			} else if (test_bit(DROP_WRITES, &fc->flags)) {
				ti->error = "Feature error_writes conflicts with feature drop_writes";
				return -EINVAL;
			}
			continue;
		}
		/*
                 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
                 */
		if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
			if (!argc) {
				ti->error = "Feature corrupt_bio_byte requires parameters";
				return -EINVAL;
			}
			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;
		}
		ti->error = "Unrecognised flakey feature requested";
		return -EINVAL;
	}
	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;
	} else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
		ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
		return -EINVAL;
	}
	return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Mike Snitzer | 510 | 100.00% | 5 | 100.00% | 
| Total | 510 | 100.00% | 5 | 100.00% | 
/*
 * Construct a flakey mapping:
 * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
 *
 *   Feature args:
 *     [drop_writes]
 *     [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.
 */
static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
	static struct dm_arg _args[] = {
		{0, UINT_MAX, "Invalid up interval"},
		{0, UINT_MAX, "Invalid down interval"},
        };
	int r;
	struct flakey_c *fc;
	unsigned long long tmpll;
	struct dm_arg_set as;
	const char *devname;
	char dummy;
	as.argc = argc;
	as.argv = argv;
	if (argc < 4) {
		ti->error = "Invalid argument count";
		return -EINVAL;
	}
	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
	if (!fc) {
		ti->error = "Cannot allocate context";
		return -ENOMEM;
	}
	fc->start_time = jiffies;
	devname = dm_shift_arg(&as);
	r = -EINVAL;
	if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
		ti->error = "Invalid device sector";
		goto bad;
	}
	fc->start = tmpll;
	r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
	if (r)
		goto bad;
	r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
	if (r)
		goto bad;
	if (!(fc->up_interval + fc->down_interval)) {
		ti->error = "Total (up + down) interval is zero";
		r = -EINVAL;
		goto bad;
	}
	if (fc->up_interval + fc->down_interval < fc->up_interval) {
		ti->error = "Interval overflow";
		r = -EINVAL;
		goto bad;
	}
	r = parse_features(&as, fc, ti);
	if (r)
		goto bad;
	r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
	if (r) {
		ti->error = "Device lookup failed";
		goto bad;
	}
	ti->num_flush_bios = 1;
	ti->num_discard_bios = 1;
	ti->per_io_data_size = sizeof(struct per_bio_data);
	ti->private = fc;
	return 0;
bad:
	kfree(fc);
	return r;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 231 | 58.48% | 1 | 9.09% | 
| Mike Snitzer | 123 | 31.14% | 4 | 36.36% | 
| Mikulas Patocka | 16 | 4.05% | 2 | 18.18% | 
| Vivek Goyal | 12 | 3.04% | 1 | 9.09% | 
| Wei Yongjun | 10 | 2.53% | 1 | 9.09% | 
| Alasdair G. Kergon | 3 | 0.76% | 2 | 18.18% | 
| Total | 395 | 100.00% | 11 | 100.00% | 
static void flakey_dtr(struct dm_target *ti)
{
	struct flakey_c *fc = ti->private;
	dm_put_device(ti, fc->dev);
	kfree(fc);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 34 | 100.00% | 1 | 100.00% | 
| Total | 34 | 100.00% | 1 | 100.00% | 
static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
{
	struct flakey_c *fc = ti->private;
	return fc->start + dm_target_offset(ti, bi_sector);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 30 | 85.71% | 1 | 50.00% | 
| Mike Snitzer | 5 | 14.29% | 1 | 50.00% | 
| Total | 35 | 100.00% | 2 | 100.00% | 
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_iter.bi_sector =
			flakey_map_sector(ti, bio->bi_iter.bi_sector);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 55 | 93.22% | 1 | 50.00% | 
| Kent Overstreet | 4 | 6.78% | 1 | 50.00% | 
| Total | 59 | 100.00% | 2 | 100.00% | 
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_opf=%u 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_opf,
			(unsigned long long)bio->bi_iter.bi_sector, bio_bytes);
	}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Mike Snitzer | 101 | 96.19% | 1 | 33.33% | 
| Jens Axboe | 2 | 1.90% | 1 | 33.33% | 
| Kent Overstreet | 2 | 1.90% | 1 | 33.33% | 
| Total | 105 | 100.00% | 3 | 100.00% | 
static int flakey_map(struct dm_target *ti, struct bio *bio)
{
	struct flakey_c *fc = ti->private;
	unsigned elapsed;
	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
	pb->bio_submitted = false;
	/* Are we alive ? */
	elapsed = (jiffies - fc->start_time) / HZ;
	if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
		/*
                 * Flag this bio as submitted while down.
                 */
		pb->bio_submitted = true;
		/*
                 * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
                 * Otherwise, flakey_end_io() will decide if the reads should be modified.
                 */
		if (bio_data_dir(bio) == READ) {
			if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
			    !test_bit(ERROR_WRITES, &fc->flags))
				return -EIO;
			goto map_bio;
		}
		/*
                 * Drop or error writes?
                 */
		if (test_bit(DROP_WRITES, &fc->flags)) {
			bio_endio(bio);
			return DM_MAPIO_SUBMITTED;
		}
		else if (test_bit(ERROR_WRITES, &fc->flags)) {
			bio_io_error(bio);
			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);
			goto map_bio;
		}
		/*
                 * By default, error all I/O.
                 */
		return -EIO;
	}
map_bio:
	flakey_map_bio(ti, bio);
	return DM_MAPIO_REMAPPED;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Mike Snitzer | 139 | 58.65% | 5 | 71.43% | 
| Josef Bacik | 73 | 30.80% | 1 | 14.29% | 
| Mikulas Patocka | 25 | 10.55% | 1 | 14.29% | 
| Total | 237 | 100.00% | 7 | 100.00% | 
static int flakey_end_io(struct dm_target *ti, struct bio *bio, int error)
{
	struct flakey_c *fc = ti->private;
	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
	if (!error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
		if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
		    all_corrupt_bio_flags_match(bio, fc)) {
			/*
                         * Corrupt successful matching READs while in down state.
                         */
			corrupt_bio_data(bio, fc);
		} else if (!test_bit(DROP_WRITES, &fc->flags) &&
			   !test_bit(ERROR_WRITES, &fc->flags)) {
			/*
                         * Error read during the down_interval if drop_writes
                         * and error_writes were not configured.
                         */
			return -EIO;
		}
	}
	return error;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Mike Snitzer | 113 | 86.92% | 4 | 80.00% | 
| Mikulas Patocka | 17 | 13.08% | 1 | 20.00% | 
| Total | 130 | 100.00% | 5 | 100.00% | 
static void flakey_status(struct dm_target *ti, status_type_t type,
			  unsigned status_flags, char *result, unsigned maxlen)
{
	unsigned sz = 0;
	struct flakey_c *fc = ti->private;
	unsigned drop_writes, error_writes;
	switch (type) {
	case STATUSTYPE_INFO:
		result[0] = '\0';
		break;
	case STATUSTYPE_TABLE:
		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);
		error_writes = test_bit(ERROR_WRITES, &fc->flags);
		DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
		if (drop_writes)
			DMEMIT("drop_writes ");
		else if (error_writes)
			DMEMIT("error_writes ");
		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);
		break;
	}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Mike Snitzer | 109 | 57.67% | 3 | 50.00% | 
| Josef Bacik | 76 | 40.21% | 1 | 16.67% | 
| Alasdair G. Kergon | 3 | 1.59% | 1 | 16.67% | 
| Mikulas Patocka | 1 | 0.53% | 1 | 16.67% | 
| Total | 189 | 100.00% | 6 | 100.00% | 
static int flakey_prepare_ioctl(struct dm_target *ti,
		struct block_device **bdev, fmode_t *mode)
{
	struct flakey_c *fc = ti->private;
	*bdev = fc->dev->bdev;
	/*
         * Only pass ioctls through if the device sizes match exactly.
         */
	if (fc->start ||
	    ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
		return 1;
	return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 26 | 38.24% | 1 | 33.33% | 
| Christoph Hellwig | 22 | 32.35% | 1 | 33.33% | 
| Paolo Bonzini | 20 | 29.41% | 1 | 33.33% | 
| Total | 68 | 100.00% | 3 | 100.00% | 
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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 47 | 100.00% | 1 | 100.00% | 
| Total | 47 | 100.00% | 1 | 100.00% | 
static struct target_type flakey_target = {
	.name   = "flakey",
	.version = {1, 4, 0},
	.module = THIS_MODULE,
	.ctr    = flakey_ctr,
	.dtr    = flakey_dtr,
	.map    = flakey_map,
	.end_io = flakey_end_io,
	.status = flakey_status,
	.prepare_ioctl = flakey_prepare_ioctl,
	.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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 34 | 100.00% | 1 | 100.00% | 
| Total | 34 | 100.00% | 1 | 100.00% | 
static void __exit dm_flakey_exit(void)
{
	dm_unregister_target(&flakey_target);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Josef Bacik | 15 | 100.00% | 1 | 100.00% | 
| Total | 15 | 100.00% | 1 | 100.00% | 
/* 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");
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Mike Snitzer | 1142 | 56.04% | 9 | 40.91% | 
| Josef Bacik | 748 | 36.70% | 1 | 4.55% | 
| Mikulas Patocka | 67 | 3.29% | 3 | 13.64% | 
| Christoph Hellwig | 24 | 1.18% | 1 | 4.55% | 
| Paolo Bonzini | 20 | 0.98% | 1 | 4.55% | 
| Vivek Goyal | 12 | 0.59% | 1 | 4.55% | 
| Wei Yongjun | 10 | 0.49% | 1 | 4.55% | 
| Kent Overstreet | 6 | 0.29% | 1 | 4.55% | 
| Alasdair G. Kergon | 6 | 0.29% | 3 | 13.64% | 
| Jens Axboe | 3 | 0.15% | 1 | 4.55% | 
| Total | 2038 | 100.00% | 22 | 100.00% | 
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.