2 * faulty.c : Multiple Devices driver for Linux
4 * Copyright (C) 2004 Neil Brown
6 * fautly-device-simulator personality for md
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)
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.
21 * The "faulty" personality causes some requests to fail.
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
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.
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".
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.
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 */
55 #define ClearErrors 31
56 #define ClearFaults 30
58 #define AllPersist 100 /* internal use only */
65 #include <linux/blkdev.h>
66 #include <linux/module.h>
67 #include <linux/raid/md_u.h>
68 #include <linux/slab.h>
70 #include <linux/seq_file.h>
73 static void faulty_fail(struct bio
*bio
, int error
)
75 struct bio
*b
= bio
->bi_private
;
77 b
->bi_iter
.bi_size
= bio
->bi_iter
.bi_size
;
78 b
->bi_iter
.bi_sector
= bio
->bi_iter
.bi_sector
;
87 atomic_t counters
[Modes
];
88 sector_t faults
[MaxFault
];
94 static int check_mode(struct faulty_conf
*conf
, int mode
)
96 if (conf
->period
[mode
] == 0 &&
97 atomic_read(&conf
->counters
[mode
]) <= 0)
98 return 0; /* no failure, no decrement */
101 if (atomic_dec_and_test(&conf
->counters
[mode
])) {
102 if (conf
->period
[mode
])
103 atomic_set(&conf
->counters
[mode
], conf
->period
[mode
]);
109 static int check_sector(struct faulty_conf
*conf
, sector_t start
, sector_t end
, int dir
)
111 /* If we find a ReadFixable sector, we fix it ... */
113 for (i
=0; i
<conf
->nfaults
; i
++)
114 if (conf
->faults
[i
] >= start
&&
115 conf
->faults
[i
] < end
) {
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
;
124 case AllPersist
*2+READ
:
125 case AllPersist
*2+WRITE
: return 1;
133 static void add_sector(struct faulty_conf
*conf
, sector_t start
, int mode
)
136 int n
= conf
->nfaults
;
137 for (i
=0; i
<conf
->nfaults
; i
++)
138 if (conf
->faults
[i
] == start
) {
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
;
146 conf
->modes
[i
] = WritePersistent
;
149 if (conf
->modes
[i
] == WritePersistent
)
150 conf
->modes
[i
] = AllPersist
;
152 conf
->modes
[i
] = ReadPersistent
;
155 if (conf
->modes
[i
] == WritePersistent
||
156 conf
->modes
[i
] == ReadPersistent
)
157 conf
->modes
[i
] = AllPersist
;
159 conf
->modes
[i
] = ReadFixable
;
162 } else if (conf
->modes
[i
] == NoPersist
)
167 conf
->faults
[n
] = start
;
168 conf
->modes
[n
] = mode
;
169 if (conf
->nfaults
== n
)
173 static void make_request(struct mddev
*mddev
, struct bio
*bio
)
175 struct faulty_conf
*conf
= mddev
->private;
178 if (bio_data_dir(bio
) == WRITE
) {
180 if (atomic_read(&conf
->counters
[WriteAll
])) {
181 /* special case - don't decrement, don't generic_make_request,
182 * just fail immediately
184 bio_endio(bio
, -EIO
);
188 if (check_sector(conf
, bio
->bi_iter
.bi_sector
,
189 bio_end_sector(bio
), WRITE
))
191 if (check_mode(conf
, WritePersistent
)) {
192 add_sector(conf
, bio
->bi_iter
.bi_sector
,
196 if (check_mode(conf
, WriteTransient
))
200 if (check_sector(conf
, bio
->bi_iter
.bi_sector
,
201 bio_end_sector(bio
), READ
))
203 if (check_mode(conf
, ReadTransient
))
205 if (check_mode(conf
, ReadPersistent
)) {
206 add_sector(conf
, bio
->bi_iter
.bi_sector
,
210 if (check_mode(conf
, ReadFixable
)) {
211 add_sector(conf
, bio
->bi_iter
.bi_sector
,
217 struct bio
*b
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
219 b
->bi_bdev
= conf
->rdev
->bdev
;
221 b
->bi_end_io
= faulty_fail
;
224 bio
->bi_bdev
= conf
->rdev
->bdev
;
226 generic_make_request(bio
);
229 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
231 struct faulty_conf
*conf
= mddev
->private;
234 if ((n
=atomic_read(&conf
->counters
[WriteTransient
])) != 0)
235 seq_printf(seq
, " WriteTransient=%d(%d)",
236 n
, conf
->period
[WriteTransient
]);
238 if ((n
=atomic_read(&conf
->counters
[ReadTransient
])) != 0)
239 seq_printf(seq
, " ReadTransient=%d(%d)",
240 n
, conf
->period
[ReadTransient
]);
242 if ((n
=atomic_read(&conf
->counters
[WritePersistent
])) != 0)
243 seq_printf(seq
, " WritePersistent=%d(%d)",
244 n
, conf
->period
[WritePersistent
]);
246 if ((n
=atomic_read(&conf
->counters
[ReadPersistent
])) != 0)
247 seq_printf(seq
, " ReadPersistent=%d(%d)",
248 n
, conf
->period
[ReadPersistent
]);
251 if ((n
=atomic_read(&conf
->counters
[ReadFixable
])) != 0)
252 seq_printf(seq
, " ReadFixable=%d(%d)",
253 n
, conf
->period
[ReadFixable
]);
255 if ((n
=atomic_read(&conf
->counters
[WriteAll
])) != 0)
256 seq_printf(seq
, " WriteAll");
258 seq_printf(seq
, " nfaults=%d", conf
->nfaults
);
262 static int reshape(struct mddev
*mddev
)
264 int mode
= mddev
->new_layout
& ModeMask
;
265 int count
= mddev
->new_layout
>> ModeShift
;
266 struct faulty_conf
*conf
= mddev
->private;
268 if (mddev
->new_layout
< 0)
272 if (mode
== ClearFaults
)
274 else if (mode
== ClearErrors
) {
276 for (i
=0 ; i
< Modes
; i
++) {
278 atomic_set(&conf
->counters
[i
], 0);
280 } else if (mode
< Modes
) {
281 conf
->period
[mode
] = count
;
283 atomic_set(&conf
->counters
[mode
], count
);
286 mddev
->new_layout
= -1;
287 mddev
->layout
= -1; /* makes sure further changes come through */
291 static sector_t
faulty_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
293 WARN_ONCE(raid_disks
,
294 "%s does not support generic reshape\n", __func__
);
297 return mddev
->dev_sectors
;
302 static int run(struct mddev
*mddev
)
304 struct md_rdev
*rdev
;
306 struct faulty_conf
*conf
;
308 if (md_check_no_bitmap(mddev
))
311 conf
= kmalloc(sizeof(*conf
), GFP_KERNEL
);
315 for (i
=0; i
<Modes
; i
++) {
316 atomic_set(&conf
->counters
[i
], 0);
321 rdev_for_each(rdev
, mddev
) {
323 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
324 rdev
->data_offset
<< 9);
327 md_set_array_sectors(mddev
, faulty_size(mddev
, 0, 0));
328 mddev
->private = conf
;
335 static int stop(struct mddev
*mddev
)
337 struct faulty_conf
*conf
= mddev
->private;
340 mddev
->private = NULL
;
344 static struct md_personality faulty_personality
=
347 .level
= LEVEL_FAULTY
,
348 .owner
= THIS_MODULE
,
349 .make_request
= make_request
,
353 .check_reshape
= reshape
,
357 static int __init
raid_init(void)
359 return register_md_personality(&faulty_personality
);
362 static void raid_exit(void)
364 unregister_md_personality(&faulty_personality
);
367 module_init(raid_init
);
368 module_exit(raid_exit
);
369 MODULE_LICENSE("GPL");
370 MODULE_DESCRIPTION("Fault injection personality for MD");
371 MODULE_ALIAS("md-personality-10"); /* faulty */
372 MODULE_ALIAS("md-faulty");
373 MODULE_ALIAS("md-level--5");