blob: 3193aefe982b7b42badf4eba4adc36f89439d70c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030033 * A mode can be one-shot or recurrent with the recurrence being
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 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
NeilBrownbff61972009-03-31 14:33:13 +110065#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040066#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110067#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090068#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110069#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110070#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72
NeilBrown6712ecf2007-09-27 12:47:43 +020073static void faulty_fail(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 struct bio *b = bio->bi_private;
76
77 b->bi_size = bio->bi_size;
78 b->bi_sector = bio->bi_sector;
79
NeilBrown6712ecf2007-09-27 12:47:43 +020080 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
NeilBrown6712ecf2007-09-27 12:47:43 +020082 bio_io_error(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
NeilBrown8f1ae432011-10-11 16:48:52 +110085struct faulty_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int period[Modes];
87 atomic_t counters[Modes];
88 sector_t faults[MaxFault];
89 int modes[MaxFault];
90 int nfaults;
NeilBrown3cb03002011-10-11 16:45:26 +110091 struct md_rdev *rdev;
NeilBrown8f1ae432011-10-11 16:48:52 +110092};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
NeilBrown8f1ae432011-10-11 16:48:52 +110094static int check_mode(struct faulty_conf *conf, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
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
NeilBrown8f1ae432011-10-11 16:48:52 +1100109static int check_sector(struct faulty_conf *conf, sector_t start, sector_t end, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
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
NeilBrown8f1ae432011-10-11 16:48:52 +1100133static void add_sector(struct faulty_conf *conf, sector_t start, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
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
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700173static void make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
NeilBrown8f1ae432011-10-11 16:48:52 +1100175 struct faulty_conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int failit = 0;
177
NeilBrown802ba062006-12-13 00:34:13 -0800178 if (bio_data_dir(bio) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* 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 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200184 bio_endio(bio, -EIO);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
Kent Overstreetf73a1c72012-09-25 15:05:12 -0700188 if (check_sector(conf, bio->bi_sector, bio_end_sector(bio), WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 failit = 1;
190 if (check_mode(conf, WritePersistent)) {
191 add_sector(conf, bio->bi_sector, WritePersistent);
192 failit = 1;
193 }
194 if (check_mode(conf, WriteTransient))
195 failit = 1;
196 } else {
197 /* read request */
Kent Overstreetf73a1c72012-09-25 15:05:12 -0700198 if (check_sector(conf, bio->bi_sector, bio_end_sector(bio), READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 failit = 1;
200 if (check_mode(conf, ReadTransient))
201 failit = 1;
202 if (check_mode(conf, ReadPersistent)) {
203 add_sector(conf, bio->bi_sector, ReadPersistent);
204 failit = 1;
205 }
206 if (check_mode(conf, ReadFixable)) {
207 add_sector(conf, bio->bi_sector, ReadFixable);
208 failit = 1;
209 }
210 }
211 if (failit) {
NeilBrowna167f662010-10-26 18:31:13 +1100212 struct bio *b = bio_clone_mddev(bio, GFP_NOIO, mddev);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 b->bi_bdev = conf->rdev->bdev;
215 b->bi_private = bio;
216 b->bi_end_io = faulty_fail;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200217 bio = b;
218 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 bio->bi_bdev = conf->rdev->bdev;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200220
221 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
NeilBrownfd01b882011-10-11 16:47:53 +1100224static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
NeilBrown8f1ae432011-10-11 16:48:52 +1100226 struct faulty_conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int n;
228
229 if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
230 seq_printf(seq, " WriteTransient=%d(%d)",
231 n, conf->period[WriteTransient]);
232
233 if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
234 seq_printf(seq, " ReadTransient=%d(%d)",
235 n, conf->period[ReadTransient]);
236
237 if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
238 seq_printf(seq, " WritePersistent=%d(%d)",
239 n, conf->period[WritePersistent]);
240
241 if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
242 seq_printf(seq, " ReadPersistent=%d(%d)",
243 n, conf->period[ReadPersistent]);
244
245
246 if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
247 seq_printf(seq, " ReadFixable=%d(%d)",
248 n, conf->period[ReadFixable]);
249
250 if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
251 seq_printf(seq, " WriteAll");
252
253 seq_printf(seq, " nfaults=%d", conf->nfaults);
254}
255
256
NeilBrownfd01b882011-10-11 16:47:53 +1100257static int reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
NeilBrown597a7112009-06-18 08:47:42 +1000259 int mode = mddev->new_layout & ModeMask;
260 int count = mddev->new_layout >> ModeShift;
NeilBrown8f1ae432011-10-11 16:48:52 +1100261 struct faulty_conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
NeilBrown597a7112009-06-18 08:47:42 +1000263 if (mddev->new_layout < 0)
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* new layout */
267 if (mode == ClearFaults)
268 conf->nfaults = 0;
269 else if (mode == ClearErrors) {
270 int i;
271 for (i=0 ; i < Modes ; i++) {
272 conf->period[i] = 0;
273 atomic_set(&conf->counters[i], 0);
274 }
275 } else if (mode < Modes) {
276 conf->period[mode] = count;
277 if (!count) count++;
278 atomic_set(&conf->counters[mode], count);
279 } else
280 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +1000281 mddev->new_layout = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 mddev->layout = -1; /* makes sure further changes come through */
283 return 0;
284}
285
NeilBrownfd01b882011-10-11 16:47:53 +1100286static sector_t faulty_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700287{
288 WARN_ONCE(raid_disks,
289 "%s does not support generic reshape\n", __func__);
290
291 if (sectors == 0)
292 return mddev->dev_sectors;
293
294 return sectors;
295}
296
NeilBrownfd01b882011-10-11 16:47:53 +1100297static int run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
NeilBrown3cb03002011-10-11 16:45:26 +1100299 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int i;
NeilBrown8f1ae432011-10-11 16:48:52 +1100301 struct faulty_conf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Andre Noll0894cc32009-06-18 08:49:23 +1000303 if (md_check_no_bitmap(mddev))
304 return -EINVAL;
305
306 conf = kmalloc(sizeof(*conf), GFP_KERNEL);
Sven Wegener08ff39f2008-10-16 14:16:53 +1100307 if (!conf)
308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 for (i=0; i<Modes; i++) {
311 atomic_set(&conf->counters[i], 0);
312 conf->period[i] = 0;
313 }
314 conf->nfaults = 0;
315
Eric Sandeen0be1fec2012-10-22 10:44:55 +1100316 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 conf->rdev = rdev;
Eric Sandeen0be1fec2012-10-22 10:44:55 +1100318 disk_stack_limits(mddev->gendisk, rdev->bdev,
319 rdev->data_offset << 9);
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dan Williams1f403622009-03-31 14:59:03 +1100322 md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 mddev->private = conf;
324
NeilBrown50ac1682009-06-18 08:47:55 +1000325 reshape(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return 0;
328}
329
NeilBrownfd01b882011-10-11 16:47:53 +1100330static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
NeilBrown8f1ae432011-10-11 16:48:52 +1100332 struct faulty_conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 kfree(conf);
335 mddev->private = NULL;
336 return 0;
337}
338
NeilBrown84fc4b52011-10-11 16:49:58 +1100339static struct md_personality faulty_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 .name = "faulty",
NeilBrown2604b702006-01-06 00:20:36 -0800342 .level = LEVEL_FAULTY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .owner = THIS_MODULE,
344 .make_request = make_request,
345 .run = run,
346 .stop = stop,
347 .status = status,
NeilBrown50ac1682009-06-18 08:47:55 +1000348 .check_reshape = reshape,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700349 .size = faulty_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
352static int __init raid_init(void)
353{
NeilBrown2604b702006-01-06 00:20:36 -0800354 return register_md_personality(&faulty_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void raid_exit(void)
358{
NeilBrown2604b702006-01-06 00:20:36 -0800359 unregister_md_personality(&faulty_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362module_init(raid_init);
363module_exit(raid_exit);
364MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100365MODULE_DESCRIPTION("Fault injection personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366MODULE_ALIAS("md-personality-10"); /* faulty */
NeilBrownd9d166c2006-01-06 00:20:51 -0800367MODULE_ALIAS("md-faulty");
NeilBrown2604b702006-01-06 00:20:36 -0800368MODULE_ALIAS("md-level--5");