Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * faulty.c : Multiple Devices driver for Linux
  4 *
  5 * Copyright (C) 2004 Neil Brown
  6 *
  7 * fautly-device-simulator personality for md
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10
 11/*
 12 * The "faulty" personality causes some requests to fail.
 13 *
 14 * Possible failure modes are:
 15 *   reads fail "randomly" but succeed on retry
 16 *   writes fail "randomly" but succeed on retry
 17 *   reads for some address fail and then persist until a write
 18 *   reads for some address fail and then persist irrespective of write
 19 *   writes for some address fail and persist
 20 *   all writes fail
 21 *
 22 * Different modes can be active at a time, but only
 23 * one can be set at array creation.  Others can be added later.
 24 * A mode can be one-shot or recurrent with the recurrence being
 25 * once in every N requests.
 26 * The bottom 5 bits of the "layout" indicate the mode.  The
 27 * remainder indicate a period, or 0 for one-shot.
 28 *
 29 * There is an implementation limit on the number of concurrently
 30 * persisting-faulty blocks. When a new fault is requested that would
 31 * exceed the limit, it is ignored.
 32 * All current faults can be clear using a layout of "0".
 33 *
 34 * Requests are always sent to the device.  If they are to fail,
 35 * we clone the bio and insert a new b_end_io into the chain.
 36 */
 37
 38#define	WriteTransient	0
 39#define	ReadTransient	1
 40#define	WritePersistent	2
 41#define	ReadPersistent	3
 42#define	WriteAll	4 /* doesn't go to device */
 43#define	ReadFixable	5
 44#define	Modes	6
 45
 46#define	ClearErrors	31
 47#define	ClearFaults	30
 48
 49#define AllPersist	100 /* internal use only */
 50#define	NoPersist	101
 51
 52#define	ModeMask	0x1f
 53#define	ModeShift	5
 54
 55#define MaxFault	50
 56#include <linux/blkdev.h>
 57#include <linux/module.h>
 58#include <linux/raid/md_u.h>
 59#include <linux/slab.h>
 60#include "md.h"
 61#include <linux/seq_file.h>
 62
 63
 64static void faulty_fail(struct bio *bio)
 65{
 66	struct bio *b = bio->bi_private;
 67
 68	b->bi_iter.bi_size = bio->bi_iter.bi_size;
 69	b->bi_iter.bi_sector = bio->bi_iter.bi_sector;
 70
 71	bio_put(bio);
 72
 73	bio_io_error(b);
 74}
 75
 76struct faulty_conf {
 77	int period[Modes];
 78	atomic_t counters[Modes];
 79	sector_t faults[MaxFault];
 80	int	modes[MaxFault];
 81	int nfaults;
 82	struct md_rdev *rdev;
 83};
 84
 85static int check_mode(struct faulty_conf *conf, int mode)
 86{
 87	if (conf->period[mode] == 0 &&
 88	    atomic_read(&conf->counters[mode]) <= 0)
 89		return 0; /* no failure, no decrement */
 90
 91
 92	if (atomic_dec_and_test(&conf->counters[mode])) {
 93		if (conf->period[mode])
 94			atomic_set(&conf->counters[mode], conf->period[mode]);
 95		return 1;
 96	}
 97	return 0;
 98}
 99
100static int check_sector(struct faulty_conf *conf, sector_t start, sector_t end, int dir)
101{
102	/* If we find a ReadFixable sector, we fix it ... */
103	int i;
104	for (i=0; i<conf->nfaults; i++)
105		if (conf->faults[i] >= start &&
106		    conf->faults[i] < end) {
107			/* found it ... */
108			switch (conf->modes[i] * 2 + dir) {
109			case WritePersistent*2+WRITE: return 1;
110			case ReadPersistent*2+READ: return 1;
111			case ReadFixable*2+READ: return 1;
112			case ReadFixable*2+WRITE:
113				conf->modes[i] = NoPersist;
114				return 0;
115			case AllPersist*2+READ:
116			case AllPersist*2+WRITE: return 1;
117			default:
118				return 0;
119			}
120		}
121	return 0;
122}
123
124static void add_sector(struct faulty_conf *conf, sector_t start, int mode)
125{
126	int i;
127	int n = conf->nfaults;
128	for (i=0; i<conf->nfaults; i++)
129		if (conf->faults[i] == start) {
130			switch(mode) {
131			case NoPersist: conf->modes[i] = mode; return;
132			case WritePersistent:
133				if (conf->modes[i] == ReadPersistent ||
134				    conf->modes[i] == ReadFixable)
135					conf->modes[i] = AllPersist;
136				else
137					conf->modes[i] = WritePersistent;
138				return;
139			case ReadPersistent:
140				if (conf->modes[i] == WritePersistent)
141					conf->modes[i] = AllPersist;
142				else
143					conf->modes[i] = ReadPersistent;
144				return;
145			case ReadFixable:
146				if (conf->modes[i] == WritePersistent ||
147				    conf->modes[i] == ReadPersistent)
148					conf->modes[i] = AllPersist;
149				else
150					conf->modes[i] = ReadFixable;
151				return;
152			}
153		} else if (conf->modes[i] == NoPersist)
154			n = i;
155
156	if (n >= MaxFault)
157		return;
158	conf->faults[n] = start;
159	conf->modes[n] = mode;
160	if (conf->nfaults == n)
161		conf->nfaults = n+1;
162}
163
164static bool faulty_make_request(struct mddev *mddev, struct bio *bio)
165{
166	struct faulty_conf *conf = mddev->private;
167	int failit = 0;
168
169	if (bio_data_dir(bio) == WRITE) {
170		/* write request */
171		if (atomic_read(&conf->counters[WriteAll])) {
172			/* special case - don't decrement, don't submit_bio_noacct,
173			 * just fail immediately
174			 */
175			bio_io_error(bio);
176			return true;
177		}
178
179		if (check_sector(conf, bio->bi_iter.bi_sector,
180				 bio_end_sector(bio), WRITE))
181			failit = 1;
182		if (check_mode(conf, WritePersistent)) {
183			add_sector(conf, bio->bi_iter.bi_sector,
184				   WritePersistent);
185			failit = 1;
186		}
187		if (check_mode(conf, WriteTransient))
188			failit = 1;
189	} else {
190		/* read request */
191		if (check_sector(conf, bio->bi_iter.bi_sector,
192				 bio_end_sector(bio), READ))
193			failit = 1;
194		if (check_mode(conf, ReadTransient))
195			failit = 1;
196		if (check_mode(conf, ReadPersistent)) {
197			add_sector(conf, bio->bi_iter.bi_sector,
198				   ReadPersistent);
199			failit = 1;
200		}
201		if (check_mode(conf, ReadFixable)) {
202			add_sector(conf, bio->bi_iter.bi_sector,
203				   ReadFixable);
204			failit = 1;
205		}
206	}
207	if (failit) {
208		struct bio *b = bio_alloc_clone(conf->rdev->bdev, bio, GFP_NOIO,
209						&mddev->bio_set);
210
 
211		b->bi_private = bio;
212		b->bi_end_io = faulty_fail;
213		bio = b;
214	} else
215		bio_set_dev(bio, conf->rdev->bdev);
216
217	submit_bio_noacct(bio);
218	return true;
219}
220
221static void faulty_status(struct seq_file *seq, struct mddev *mddev)
222{
223	struct faulty_conf *conf = mddev->private;
224	int n;
225
226	if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
227		seq_printf(seq, " WriteTransient=%d(%d)",
228			   n, conf->period[WriteTransient]);
229
230	if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
231		seq_printf(seq, " ReadTransient=%d(%d)",
232			   n, conf->period[ReadTransient]);
233
234	if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
235		seq_printf(seq, " WritePersistent=%d(%d)",
236			   n, conf->period[WritePersistent]);
237
238	if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
239		seq_printf(seq, " ReadPersistent=%d(%d)",
240			   n, conf->period[ReadPersistent]);
241
242
243	if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
244		seq_printf(seq, " ReadFixable=%d(%d)",
245			   n, conf->period[ReadFixable]);
246
247	if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
248		seq_printf(seq, " WriteAll");
249
250	seq_printf(seq, " nfaults=%d", conf->nfaults);
251}
252
253
254static int faulty_reshape(struct mddev *mddev)
255{
256	int mode = mddev->new_layout & ModeMask;
257	int count = mddev->new_layout >> ModeShift;
258	struct faulty_conf *conf = mddev->private;
259
260	if (mddev->new_layout < 0)
261		return 0;
262
263	/* new layout */
264	if (mode == ClearFaults)
265		conf->nfaults = 0;
266	else if (mode == ClearErrors) {
267		int i;
268		for (i=0 ; i < Modes ; i++) {
269			conf->period[i] = 0;
270			atomic_set(&conf->counters[i], 0);
271		}
272	} else if (mode < Modes) {
273		conf->period[mode] = count;
274		if (!count) count++;
275		atomic_set(&conf->counters[mode], count);
276	} else
277		return -EINVAL;
278	mddev->new_layout = -1;
279	mddev->layout = -1; /* makes sure further changes come through */
280	return 0;
281}
282
283static sector_t faulty_size(struct mddev *mddev, sector_t sectors, int raid_disks)
284{
285	WARN_ONCE(raid_disks,
286		  "%s does not support generic reshape\n", __func__);
287
288	if (sectors == 0)
289		return mddev->dev_sectors;
290
291	return sectors;
292}
293
294static int faulty_run(struct mddev *mddev)
295{
296	struct md_rdev *rdev;
297	int i;
298	struct faulty_conf *conf;
299
300	if (md_check_no_bitmap(mddev))
301		return -EINVAL;
302
303	conf = kmalloc(sizeof(*conf), GFP_KERNEL);
304	if (!conf)
305		return -ENOMEM;
306
307	for (i=0; i<Modes; i++) {
308		atomic_set(&conf->counters[i], 0);
309		conf->period[i] = 0;
310	}
311	conf->nfaults = 0;
312
313	rdev_for_each(rdev, mddev) {
314		conf->rdev = rdev;
315		disk_stack_limits(mddev->gendisk, rdev->bdev,
316				  rdev->data_offset << 9);
317	}
318
319	md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
320	mddev->private = conf;
321
322	faulty_reshape(mddev);
323
324	return 0;
325}
326
327static void faulty_free(struct mddev *mddev, void *priv)
328{
329	struct faulty_conf *conf = priv;
330
331	kfree(conf);
332}
333
334static struct md_personality faulty_personality =
335{
336	.name		= "faulty",
337	.level		= LEVEL_FAULTY,
338	.owner		= THIS_MODULE,
339	.make_request	= faulty_make_request,
340	.run		= faulty_run,
341	.free		= faulty_free,
342	.status		= faulty_status,
343	.check_reshape	= faulty_reshape,
344	.size		= faulty_size,
345};
346
347static int __init raid_init(void)
348{
349	return register_md_personality(&faulty_personality);
350}
351
352static void raid_exit(void)
353{
354	unregister_md_personality(&faulty_personality);
355}
356
357module_init(raid_init);
358module_exit(raid_exit);
359MODULE_LICENSE("GPL");
360MODULE_DESCRIPTION("Fault injection personality for MD (deprecated)");
361MODULE_ALIAS("md-personality-10"); /* faulty */
362MODULE_ALIAS("md-faulty");
363MODULE_ALIAS("md-level--5");
v4.17
 
  1/*
  2 * faulty.c : Multiple Devices driver for Linux
  3 *
  4 * Copyright (C) 2004 Neil Brown
  5 *
  6 * fautly-device-simulator personality for md
  7 *
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation; either version 2, or (at your option)
 12 * any later version.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * (for example /usr/src/linux/COPYING); if not, write to the Free
 16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 17 */
 18
 19
 20/*
 21 * The "faulty" personality causes some requests to fail.
 22 *
 23 * Possible failure modes are:
 24 *   reads fail "randomly" but succeed on retry
 25 *   writes fail "randomly" but succeed on retry
 26 *   reads for some address fail and then persist until a write
 27 *   reads for some address fail and then persist irrespective of write
 28 *   writes for some address fail and persist
 29 *   all writes fail
 30 *
 31 * Different modes can be active at a time, but only
 32 * one can be set at array creation.  Others can be added later.
 33 * A mode can be one-shot or recurrent with the recurrence being
 34 * once in every N requests.
 35 * The bottom 5 bits of the "layout" indicate the mode.  The
 36 * remainder indicate a period, or 0 for one-shot.
 37 *
 38 * There is an implementation limit on the number of concurrently
 39 * persisting-faulty blocks. When a new fault is requested that would
 40 * exceed the limit, it is ignored.
 41 * All current faults can be clear using a layout of "0".
 42 *
 43 * Requests are always sent to the device.  If they are to fail,
 44 * we clone the bio and insert a new b_end_io into the chain.
 45 */
 46
 47#define	WriteTransient	0
 48#define	ReadTransient	1
 49#define	WritePersistent	2
 50#define	ReadPersistent	3
 51#define	WriteAll	4 /* doesn't go to device */
 52#define	ReadFixable	5
 53#define	Modes	6
 54
 55#define	ClearErrors	31
 56#define	ClearFaults	30
 57
 58#define AllPersist	100 /* internal use only */
 59#define	NoPersist	101
 60
 61#define	ModeMask	0x1f
 62#define	ModeShift	5
 63
 64#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>
 71
 72
 73static void faulty_fail(struct bio *bio)
 74{
 75	struct bio *b = bio->bi_private;
 76
 77	b->bi_iter.bi_size = bio->bi_iter.bi_size;
 78	b->bi_iter.bi_sector = bio->bi_iter.bi_sector;
 79
 80	bio_put(bio);
 81
 82	bio_io_error(b);
 83}
 84
 85struct faulty_conf {
 86	int period[Modes];
 87	atomic_t counters[Modes];
 88	sector_t faults[MaxFault];
 89	int	modes[MaxFault];
 90	int nfaults;
 91	struct md_rdev *rdev;
 92};
 93
 94static int check_mode(struct faulty_conf *conf, int mode)
 95{
 96	if (conf->period[mode] == 0 &&
 97	    atomic_read(&conf->counters[mode]) <= 0)
 98		return 0; /* no failure, no decrement */
 99
100
101	if (atomic_dec_and_test(&conf->counters[mode])) {
102		if (conf->period[mode])
103			atomic_set(&conf->counters[mode], conf->period[mode]);
104		return 1;
105	}
106	return 0;
107}
108
109static int check_sector(struct faulty_conf *conf, sector_t start, sector_t end, int dir)
110{
111	/* If we find a ReadFixable sector, we fix it ... */
112	int i;
113	for (i=0; i<conf->nfaults; i++)
114		if (conf->faults[i] >= start &&
115		    conf->faults[i] < end) {
116			/* found it ... */
117			switch (conf->modes[i] * 2 + dir) {
118			case WritePersistent*2+WRITE: return 1;
119			case ReadPersistent*2+READ: return 1;
120			case ReadFixable*2+READ: return 1;
121			case ReadFixable*2+WRITE:
122				conf->modes[i] = NoPersist;
123				return 0;
124			case AllPersist*2+READ:
125			case AllPersist*2+WRITE: return 1;
126			default:
127				return 0;
128			}
129		}
130	return 0;
131}
132
133static void add_sector(struct faulty_conf *conf, sector_t start, int mode)
134{
135	int i;
136	int n = conf->nfaults;
137	for (i=0; i<conf->nfaults; i++)
138		if (conf->faults[i] == start) {
139			switch(mode) {
140			case NoPersist: conf->modes[i] = mode; return;
141			case WritePersistent:
142				if (conf->modes[i] == ReadPersistent ||
143				    conf->modes[i] == ReadFixable)
144					conf->modes[i] = AllPersist;
145				else
146					conf->modes[i] = WritePersistent;
147				return;
148			case ReadPersistent:
149				if (conf->modes[i] == WritePersistent)
150					conf->modes[i] = AllPersist;
151				else
152					conf->modes[i] = ReadPersistent;
153				return;
154			case ReadFixable:
155				if (conf->modes[i] == WritePersistent ||
156				    conf->modes[i] == ReadPersistent)
157					conf->modes[i] = AllPersist;
158				else
159					conf->modes[i] = ReadFixable;
160				return;
161			}
162		} else if (conf->modes[i] == NoPersist)
163			n = i;
164
165	if (n >= MaxFault)
166		return;
167	conf->faults[n] = start;
168	conf->modes[n] = mode;
169	if (conf->nfaults == n)
170		conf->nfaults = n+1;
171}
172
173static bool faulty_make_request(struct mddev *mddev, struct bio *bio)
174{
175	struct faulty_conf *conf = mddev->private;
176	int failit = 0;
177
178	if (bio_data_dir(bio) == WRITE) {
179		/* write request */
180		if (atomic_read(&conf->counters[WriteAll])) {
181			/* special case - don't decrement, don't generic_make_request,
182			 * just fail immediately
183			 */
184			bio_io_error(bio);
185			return true;
186		}
187
188		if (check_sector(conf, bio->bi_iter.bi_sector,
189				 bio_end_sector(bio), WRITE))
190			failit = 1;
191		if (check_mode(conf, WritePersistent)) {
192			add_sector(conf, bio->bi_iter.bi_sector,
193				   WritePersistent);
194			failit = 1;
195		}
196		if (check_mode(conf, WriteTransient))
197			failit = 1;
198	} else {
199		/* read request */
200		if (check_sector(conf, bio->bi_iter.bi_sector,
201				 bio_end_sector(bio), READ))
202			failit = 1;
203		if (check_mode(conf, ReadTransient))
204			failit = 1;
205		if (check_mode(conf, ReadPersistent)) {
206			add_sector(conf, bio->bi_iter.bi_sector,
207				   ReadPersistent);
208			failit = 1;
209		}
210		if (check_mode(conf, ReadFixable)) {
211			add_sector(conf, bio->bi_iter.bi_sector,
212				   ReadFixable);
213			failit = 1;
214		}
215	}
216	if (failit) {
217		struct bio *b = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
 
218
219		bio_set_dev(b, conf->rdev->bdev);
220		b->bi_private = bio;
221		b->bi_end_io = faulty_fail;
222		bio = b;
223	} else
224		bio_set_dev(bio, conf->rdev->bdev);
225
226	generic_make_request(bio);
227	return true;
228}
229
230static void faulty_status(struct seq_file *seq, struct mddev *mddev)
231{
232	struct faulty_conf *conf = mddev->private;
233	int n;
234
235	if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
236		seq_printf(seq, " WriteTransient=%d(%d)",
237			   n, conf->period[WriteTransient]);
238
239	if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
240		seq_printf(seq, " ReadTransient=%d(%d)",
241			   n, conf->period[ReadTransient]);
242
243	if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
244		seq_printf(seq, " WritePersistent=%d(%d)",
245			   n, conf->period[WritePersistent]);
246
247	if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
248		seq_printf(seq, " ReadPersistent=%d(%d)",
249			   n, conf->period[ReadPersistent]);
250
251
252	if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
253		seq_printf(seq, " ReadFixable=%d(%d)",
254			   n, conf->period[ReadFixable]);
255
256	if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
257		seq_printf(seq, " WriteAll");
258
259	seq_printf(seq, " nfaults=%d", conf->nfaults);
260}
261
262
263static int faulty_reshape(struct mddev *mddev)
264{
265	int mode = mddev->new_layout & ModeMask;
266	int count = mddev->new_layout >> ModeShift;
267	struct faulty_conf *conf = mddev->private;
268
269	if (mddev->new_layout < 0)
270		return 0;
271
272	/* new layout */
273	if (mode == ClearFaults)
274		conf->nfaults = 0;
275	else if (mode == ClearErrors) {
276		int i;
277		for (i=0 ; i < Modes ; i++) {
278			conf->period[i] = 0;
279			atomic_set(&conf->counters[i], 0);
280		}
281	} else if (mode < Modes) {
282		conf->period[mode] = count;
283		if (!count) count++;
284		atomic_set(&conf->counters[mode], count);
285	} else
286		return -EINVAL;
287	mddev->new_layout = -1;
288	mddev->layout = -1; /* makes sure further changes come through */
289	return 0;
290}
291
292static sector_t faulty_size(struct mddev *mddev, sector_t sectors, int raid_disks)
293{
294	WARN_ONCE(raid_disks,
295		  "%s does not support generic reshape\n", __func__);
296
297	if (sectors == 0)
298		return mddev->dev_sectors;
299
300	return sectors;
301}
302
303static int faulty_run(struct mddev *mddev)
304{
305	struct md_rdev *rdev;
306	int i;
307	struct faulty_conf *conf;
308
309	if (md_check_no_bitmap(mddev))
310		return -EINVAL;
311
312	conf = kmalloc(sizeof(*conf), GFP_KERNEL);
313	if (!conf)
314		return -ENOMEM;
315
316	for (i=0; i<Modes; i++) {
317		atomic_set(&conf->counters[i], 0);
318		conf->period[i] = 0;
319	}
320	conf->nfaults = 0;
321
322	rdev_for_each(rdev, mddev) {
323		conf->rdev = rdev;
324		disk_stack_limits(mddev->gendisk, rdev->bdev,
325				  rdev->data_offset << 9);
326	}
327
328	md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
329	mddev->private = conf;
330
331	faulty_reshape(mddev);
332
333	return 0;
334}
335
336static void faulty_free(struct mddev *mddev, void *priv)
337{
338	struct faulty_conf *conf = priv;
339
340	kfree(conf);
341}
342
343static struct md_personality faulty_personality =
344{
345	.name		= "faulty",
346	.level		= LEVEL_FAULTY,
347	.owner		= THIS_MODULE,
348	.make_request	= faulty_make_request,
349	.run		= faulty_run,
350	.free		= faulty_free,
351	.status		= faulty_status,
352	.check_reshape	= faulty_reshape,
353	.size		= faulty_size,
354};
355
356static int __init raid_init(void)
357{
358	return register_md_personality(&faulty_personality);
359}
360
361static void raid_exit(void)
362{
363	unregister_md_personality(&faulty_personality);
364}
365
366module_init(raid_init);
367module_exit(raid_exit);
368MODULE_LICENSE("GPL");
369MODULE_DESCRIPTION("Fault injection personality for MD");
370MODULE_ALIAS("md-personality-10"); /* faulty */
371MODULE_ALIAS("md-faulty");
372MODULE_ALIAS("md-level--5");