faulty.c 9.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * faulty.c : Multiple Devices driver for Linux
 *
 * Copyright (C) 2004 Neil Brown
 *
 * fautly-device-simulator personality for md
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * You should have received a copy of the GNU General Public License
 * (for example /usr/src/linux/COPYING); if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/*
 * The "faulty" personality causes some requests to fail.
 *
 * Possible failure modes are:
 *   reads fail "randomly" but succeed on retry
 *   writes fail "randomly" but succeed on retry
 *   reads for some address fail and then persist until a write
 *   reads for some address fail and then persist irrespective of write
 *   writes for some address fail and persist
 *   all writes fail
 *
 * Different modes can be active at a time, but only
 * one can be set at array creation.  Others can be added later.
L
Lucas De Marchi 已提交
33
 * A mode can be one-shot or recurrent with the recurrence being
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * once in every N requests.
 * The bottom 5 bits of the "layout" indicate the mode.  The
 * remainder indicate a period, or 0 for one-shot.
 *
 * There is an implementation limit on the number of concurrently
 * persisting-faulty blocks. When a new fault is requested that would
 * exceed the limit, it is ignored.
 * All current faults can be clear using a layout of "0".
 *
 * Requests are always sent to the device.  If they are to fail,
 * we clone the bio and insert a new b_end_io into the chain.
 */

#define	WriteTransient	0
#define	ReadTransient	1
#define	WritePersistent	2
#define	ReadPersistent	3
#define	WriteAll	4 /* doesn't go to device */
#define	ReadFixable	5
#define	Modes	6

#define	ClearErrors	31
#define	ClearFaults	30

#define AllPersist	100 /* internal use only */
#define	NoPersist	101

#define	ModeMask	0x1f
#define	ModeShift	5

#define MaxFault	50
65
#include <linux/blkdev.h>
66
#include <linux/module.h>
67
#include <linux/raid/md_u.h>
68
#include <linux/slab.h>
69
#include "md.h"
70
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
71 72


73
static void faulty_fail(struct bio *bio)
L
Linus Torvalds 已提交
74 75 76
{
	struct bio *b = bio->bi_private;

77 78
	b->bi_iter.bi_size = bio->bi_iter.bi_size;
	b->bi_iter.bi_sector = bio->bi_iter.bi_sector;
L
Linus Torvalds 已提交
79

80
	bio_put(bio);
L
Linus Torvalds 已提交
81

82
	bio_io_error(b);
L
Linus Torvalds 已提交
83 84
}

85
struct faulty_conf {
L
Linus Torvalds 已提交
86 87 88 89 90
	int period[Modes];
	atomic_t counters[Modes];
	sector_t faults[MaxFault];
	int	modes[MaxFault];
	int nfaults;
91
	struct md_rdev *rdev;
92
};
L
Linus Torvalds 已提交
93

94
static int check_mode(struct faulty_conf *conf, int mode)
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
	if (conf->period[mode] == 0 &&
	    atomic_read(&conf->counters[mode]) <= 0)
		return 0; /* no failure, no decrement */


	if (atomic_dec_and_test(&conf->counters[mode])) {
		if (conf->period[mode])
			atomic_set(&conf->counters[mode], conf->period[mode]);
		return 1;
	}
	return 0;
}

109
static int check_sector(struct faulty_conf *conf, sector_t start, sector_t end, int dir)
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
	/* If we find a ReadFixable sector, we fix it ... */
	int i;
	for (i=0; i<conf->nfaults; i++)
		if (conf->faults[i] >= start &&
		    conf->faults[i] < end) {
			/* found it ... */
			switch (conf->modes[i] * 2 + dir) {
			case WritePersistent*2+WRITE: return 1;
			case ReadPersistent*2+READ: return 1;
			case ReadFixable*2+READ: return 1;
			case ReadFixable*2+WRITE:
				conf->modes[i] = NoPersist;
				return 0;
			case AllPersist*2+READ:
			case AllPersist*2+WRITE: return 1;
			default:
				return 0;
			}
		}
	return 0;
}

133
static void add_sector(struct faulty_conf *conf, sector_t start, int mode)
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
{
	int i;
	int n = conf->nfaults;
	for (i=0; i<conf->nfaults; i++)
		if (conf->faults[i] == start) {
			switch(mode) {
			case NoPersist: conf->modes[i] = mode; return;
			case WritePersistent:
				if (conf->modes[i] == ReadPersistent ||
				    conf->modes[i] == ReadFixable)
					conf->modes[i] = AllPersist;
				else
					conf->modes[i] = WritePersistent;
				return;
			case ReadPersistent:
				if (conf->modes[i] == WritePersistent)
					conf->modes[i] = AllPersist;
				else
					conf->modes[i] = ReadPersistent;
				return;
			case ReadFixable:
				if (conf->modes[i] == WritePersistent ||
				    conf->modes[i] == ReadPersistent)
					conf->modes[i] = AllPersist;
				else
					conf->modes[i] = ReadFixable;
				return;
			}
		} else if (conf->modes[i] == NoPersist)
			n = i;

	if (n >= MaxFault)
		return;
	conf->faults[n] = start;
	conf->modes[n] = mode;
	if (conf->nfaults == n)
		conf->nfaults = n+1;
}

S
Shaohua Li 已提交
173
static void faulty_make_request(struct mddev *mddev, struct bio *bio)
L
Linus Torvalds 已提交
174
{
175
	struct faulty_conf *conf = mddev->private;
L
Linus Torvalds 已提交
176 177
	int failit = 0;

178
	if (bio_data_dir(bio) == WRITE) {
L
Linus Torvalds 已提交
179 180 181 182 183
		/* write request */
		if (atomic_read(&conf->counters[WriteAll])) {
			/* special case - don't decrement, don't generic_make_request,
			 * just fail immediately
			 */
184
			bio_io_error(bio);
185
			return;
L
Linus Torvalds 已提交
186 187
		}

188 189
		if (check_sector(conf, bio->bi_iter.bi_sector,
				 bio_end_sector(bio), WRITE))
L
Linus Torvalds 已提交
190 191
			failit = 1;
		if (check_mode(conf, WritePersistent)) {
192 193
			add_sector(conf, bio->bi_iter.bi_sector,
				   WritePersistent);
L
Linus Torvalds 已提交
194 195 196 197 198 199
			failit = 1;
		}
		if (check_mode(conf, WriteTransient))
			failit = 1;
	} else {
		/* read request */
200 201
		if (check_sector(conf, bio->bi_iter.bi_sector,
				 bio_end_sector(bio), READ))
L
Linus Torvalds 已提交
202 203 204 205
			failit = 1;
		if (check_mode(conf, ReadTransient))
			failit = 1;
		if (check_mode(conf, ReadPersistent)) {
206 207
			add_sector(conf, bio->bi_iter.bi_sector,
				   ReadPersistent);
L
Linus Torvalds 已提交
208 209 210
			failit = 1;
		}
		if (check_mode(conf, ReadFixable)) {
211 212
			add_sector(conf, bio->bi_iter.bi_sector,
				   ReadFixable);
L
Linus Torvalds 已提交
213 214 215 216
			failit = 1;
		}
	}
	if (failit) {
217
		struct bio *b = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
218

L
Linus Torvalds 已提交
219 220 221
		b->bi_bdev = conf->rdev->bdev;
		b->bi_private = bio;
		b->bi_end_io = faulty_fail;
222 223
		bio = b;
	} else
L
Linus Torvalds 已提交
224
		bio->bi_bdev = conf->rdev->bdev;
225 226

	generic_make_request(bio);
L
Linus Torvalds 已提交
227 228
}

S
Shaohua Li 已提交
229
static void faulty_status(struct seq_file *seq, struct mddev *mddev)
L
Linus Torvalds 已提交
230
{
231
	struct faulty_conf *conf = mddev->private;
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	int n;

	if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
		seq_printf(seq, " WriteTransient=%d(%d)",
			   n, conf->period[WriteTransient]);

	if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
		seq_printf(seq, " ReadTransient=%d(%d)",
			   n, conf->period[ReadTransient]);

	if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
		seq_printf(seq, " WritePersistent=%d(%d)",
			   n, conf->period[WritePersistent]);

	if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
		seq_printf(seq, " ReadPersistent=%d(%d)",
			   n, conf->period[ReadPersistent]);


	if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
		seq_printf(seq, " ReadFixable=%d(%d)",
			   n, conf->period[ReadFixable]);

	if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
		seq_printf(seq, " WriteAll");

	seq_printf(seq, " nfaults=%d", conf->nfaults);
}


S
Shaohua Li 已提交
262
static int faulty_reshape(struct mddev *mddev)
L
Linus Torvalds 已提交
263
{
264 265
	int mode = mddev->new_layout & ModeMask;
	int count = mddev->new_layout >> ModeShift;
266
	struct faulty_conf *conf = mddev->private;
L
Linus Torvalds 已提交
267

268 269
	if (mddev->new_layout < 0)
		return 0;
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

	/* new layout */
	if (mode == ClearFaults)
		conf->nfaults = 0;
	else if (mode == ClearErrors) {
		int i;
		for (i=0 ; i < Modes ; i++) {
			conf->period[i] = 0;
			atomic_set(&conf->counters[i], 0);
		}
	} else if (mode < Modes) {
		conf->period[mode] = count;
		if (!count) count++;
		atomic_set(&conf->counters[mode], count);
	} else
		return -EINVAL;
286
	mddev->new_layout = -1;
L
Linus Torvalds 已提交
287 288 289 290
	mddev->layout = -1; /* makes sure further changes come through */
	return 0;
}

291
static sector_t faulty_size(struct mddev *mddev, sector_t sectors, int raid_disks)
292 293 294 295 296 297 298 299 300 301
{
	WARN_ONCE(raid_disks,
		  "%s does not support generic reshape\n", __func__);

	if (sectors == 0)
		return mddev->dev_sectors;

	return sectors;
}

S
Shaohua Li 已提交
302
static int faulty_run(struct mddev *mddev)
L
Linus Torvalds 已提交
303
{
304
	struct md_rdev *rdev;
L
Linus Torvalds 已提交
305
	int i;
306
	struct faulty_conf *conf;
L
Linus Torvalds 已提交
307

308 309 310 311
	if (md_check_no_bitmap(mddev))
		return -EINVAL;

	conf = kmalloc(sizeof(*conf), GFP_KERNEL);
312 313
	if (!conf)
		return -ENOMEM;
L
Linus Torvalds 已提交
314 315 316 317 318 319 320

	for (i=0; i<Modes; i++) {
		atomic_set(&conf->counters[i], 0);
		conf->period[i] = 0;
	}
	conf->nfaults = 0;

E
Eric Sandeen 已提交
321
	rdev_for_each(rdev, mddev) {
L
Linus Torvalds 已提交
322
		conf->rdev = rdev;
E
Eric Sandeen 已提交
323 324 325
		disk_stack_limits(mddev->gendisk, rdev->bdev,
				  rdev->data_offset << 9);
	}
L
Linus Torvalds 已提交
326

327
	md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
L
Linus Torvalds 已提交
328 329
	mddev->private = conf;

S
Shaohua Li 已提交
330
	faulty_reshape(mddev);
L
Linus Torvalds 已提交
331 332 333 334

	return 0;
}

N
NeilBrown 已提交
335
static void faulty_free(struct mddev *mddev, void *priv)
L
Linus Torvalds 已提交
336
{
N
NeilBrown 已提交
337
	struct faulty_conf *conf = priv;
L
Linus Torvalds 已提交
338 339 340 341

	kfree(conf);
}

342
static struct md_personality faulty_personality =
L
Linus Torvalds 已提交
343 344
{
	.name		= "faulty",
345
	.level		= LEVEL_FAULTY,
L
Linus Torvalds 已提交
346
	.owner		= THIS_MODULE,
S
Shaohua Li 已提交
347 348
	.make_request	= faulty_make_request,
	.run		= faulty_run,
N
NeilBrown 已提交
349
	.free		= faulty_free,
S
Shaohua Li 已提交
350 351
	.status		= faulty_status,
	.check_reshape	= faulty_reshape,
352
	.size		= faulty_size,
L
Linus Torvalds 已提交
353 354 355 356
};

static int __init raid_init(void)
{
357
	return register_md_personality(&faulty_personality);
L
Linus Torvalds 已提交
358 359 360 361
}

static void raid_exit(void)
{
362
	unregister_md_personality(&faulty_personality);
L
Linus Torvalds 已提交
363 364 365 366 367
}

module_init(raid_init);
module_exit(raid_exit);
MODULE_LICENSE("GPL");
368
MODULE_DESCRIPTION("Fault injection personality for MD");
L
Linus Torvalds 已提交
369
MODULE_ALIAS("md-personality-10"); /* faulty */
370
MODULE_ALIAS("md-faulty");
371
MODULE_ALIAS("md-level--5");