Linux 3.12.39
[linux/fpc-iii.git] / drivers / md / faulty.c
blob3193aefe982b7b42badf4eba4adc36f89439d70c
1 /*
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)
12 * any later version.
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
29 * all writes fail
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 */
52 #define ReadFixable 5
53 #define Modes 6
55 #define ClearErrors 31
56 #define ClearFaults 30
58 #define AllPersist 100 /* internal use only */
59 #define NoPersist 101
61 #define ModeMask 0x1f
62 #define ModeShift 5
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>
73 static void faulty_fail(struct bio *bio, int error)
75 struct bio *b = bio->bi_private;
77 b->bi_size = bio->bi_size;
78 b->bi_sector = bio->bi_sector;
80 bio_put(bio);
82 bio_io_error(b);
85 struct 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;
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]);
104 return 1;
106 return 0;
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 ... */
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;
130 return 0;
133 static void add_sector(struct faulty_conf *conf, sector_t start, int mode)
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;
162 } else if (conf->modes[i] == NoPersist)
163 n = i;
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;
173 static void make_request(struct mddev *mddev, struct bio *bio)
175 struct faulty_conf *conf = mddev->private;
176 int failit = 0;
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
184 bio_endio(bio, -EIO);
185 return;
188 if (check_sector(conf, bio->bi_sector, bio_end_sector(bio), WRITE))
189 failit = 1;
190 if (check_mode(conf, WritePersistent)) {
191 add_sector(conf, bio->bi_sector, WritePersistent);
192 failit = 1;
194 if (check_mode(conf, WriteTransient))
195 failit = 1;
196 } else {
197 /* read request */
198 if (check_sector(conf, bio->bi_sector, bio_end_sector(bio), READ))
199 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;
206 if (check_mode(conf, ReadFixable)) {
207 add_sector(conf, bio->bi_sector, ReadFixable);
208 failit = 1;
211 if (failit) {
212 struct bio *b = bio_clone_mddev(bio, GFP_NOIO, mddev);
214 b->bi_bdev = conf->rdev->bdev;
215 b->bi_private = bio;
216 b->bi_end_io = faulty_fail;
217 bio = b;
218 } else
219 bio->bi_bdev = conf->rdev->bdev;
221 generic_make_request(bio);
224 static void status(struct seq_file *seq, struct mddev *mddev)
226 struct faulty_conf *conf = mddev->private;
227 int n;
229 if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
230 seq_printf(seq, " WriteTransient=%d(%d)",
231 n, conf->period[WriteTransient]);
233 if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
234 seq_printf(seq, " ReadTransient=%d(%d)",
235 n, conf->period[ReadTransient]);
237 if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
238 seq_printf(seq, " WritePersistent=%d(%d)",
239 n, conf->period[WritePersistent]);
241 if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
242 seq_printf(seq, " ReadPersistent=%d(%d)",
243 n, conf->period[ReadPersistent]);
246 if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
247 seq_printf(seq, " ReadFixable=%d(%d)",
248 n, conf->period[ReadFixable]);
250 if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
251 seq_printf(seq, " WriteAll");
253 seq_printf(seq, " nfaults=%d", conf->nfaults);
257 static int reshape(struct mddev *mddev)
259 int mode = mddev->new_layout & ModeMask;
260 int count = mddev->new_layout >> ModeShift;
261 struct faulty_conf *conf = mddev->private;
263 if (mddev->new_layout < 0)
264 return 0;
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);
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;
281 mddev->new_layout = -1;
282 mddev->layout = -1; /* makes sure further changes come through */
283 return 0;
286 static sector_t faulty_size(struct mddev *mddev, sector_t sectors, int raid_disks)
288 WARN_ONCE(raid_disks,
289 "%s does not support generic reshape\n", __func__);
291 if (sectors == 0)
292 return mddev->dev_sectors;
294 return sectors;
297 static int run(struct mddev *mddev)
299 struct md_rdev *rdev;
300 int i;
301 struct faulty_conf *conf;
303 if (md_check_no_bitmap(mddev))
304 return -EINVAL;
306 conf = kmalloc(sizeof(*conf), GFP_KERNEL);
307 if (!conf)
308 return -ENOMEM;
310 for (i=0; i<Modes; i++) {
311 atomic_set(&conf->counters[i], 0);
312 conf->period[i] = 0;
314 conf->nfaults = 0;
316 rdev_for_each(rdev, mddev) {
317 conf->rdev = rdev;
318 disk_stack_limits(mddev->gendisk, rdev->bdev,
319 rdev->data_offset << 9);
322 md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
323 mddev->private = conf;
325 reshape(mddev);
327 return 0;
330 static int stop(struct mddev *mddev)
332 struct faulty_conf *conf = mddev->private;
334 kfree(conf);
335 mddev->private = NULL;
336 return 0;
339 static struct md_personality faulty_personality =
341 .name = "faulty",
342 .level = LEVEL_FAULTY,
343 .owner = THIS_MODULE,
344 .make_request = make_request,
345 .run = run,
346 .stop = stop,
347 .status = status,
348 .check_reshape = reshape,
349 .size = faulty_size,
352 static int __init raid_init(void)
354 return register_md_personality(&faulty_personality);
357 static void raid_exit(void)
359 unregister_md_personality(&faulty_personality);
362 module_init(raid_init);
363 module_exit(raid_exit);
364 MODULE_LICENSE("GPL");
365 MODULE_DESCRIPTION("Fault injection personality for MD");
366 MODULE_ALIAS("md-personality-10"); /* faulty */
367 MODULE_ALIAS("md-faulty");
368 MODULE_ALIAS("md-level--5");