iwlwifi: add missing rcu_read_lock
[linux-2.6/kvm.git] / drivers / md / faulty.c
blob8e3850b98cca508f2bb60de9aa0c843c3328779b
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 recurrance 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/raid/md_u.h>
67 #include <linux/slab.h>
68 #include "md.h"
69 #include <linux/seq_file.h>
72 static void faulty_fail(struct bio *bio, int error)
74 struct bio *b = bio->bi_private;
76 b->bi_size = bio->bi_size;
77 b->bi_sector = bio->bi_sector;
79 bio_put(bio);
81 bio_io_error(b);
84 typedef struct faulty_conf {
85 int period[Modes];
86 atomic_t counters[Modes];
87 sector_t faults[MaxFault];
88 int modes[MaxFault];
89 int nfaults;
90 mdk_rdev_t *rdev;
91 } conf_t;
93 static int check_mode(conf_t *conf, int mode)
95 if (conf->period[mode] == 0 &&
96 atomic_read(&conf->counters[mode]) <= 0)
97 return 0; /* no failure, no decrement */
100 if (atomic_dec_and_test(&conf->counters[mode])) {
101 if (conf->period[mode])
102 atomic_set(&conf->counters[mode], conf->period[mode]);
103 return 1;
105 return 0;
108 static int check_sector(conf_t *conf, sector_t start, sector_t end, int dir)
110 /* If we find a ReadFixable sector, we fix it ... */
111 int i;
112 for (i=0; i<conf->nfaults; i++)
113 if (conf->faults[i] >= start &&
114 conf->faults[i] < end) {
115 /* found it ... */
116 switch (conf->modes[i] * 2 + dir) {
117 case WritePersistent*2+WRITE: return 1;
118 case ReadPersistent*2+READ: return 1;
119 case ReadFixable*2+READ: return 1;
120 case ReadFixable*2+WRITE:
121 conf->modes[i] = NoPersist;
122 return 0;
123 case AllPersist*2+READ:
124 case AllPersist*2+WRITE: return 1;
125 default:
126 return 0;
129 return 0;
132 static void add_sector(conf_t *conf, sector_t start, int mode)
134 int i;
135 int n = conf->nfaults;
136 for (i=0; i<conf->nfaults; i++)
137 if (conf->faults[i] == start) {
138 switch(mode) {
139 case NoPersist: conf->modes[i] = mode; return;
140 case WritePersistent:
141 if (conf->modes[i] == ReadPersistent ||
142 conf->modes[i] == ReadFixable)
143 conf->modes[i] = AllPersist;
144 else
145 conf->modes[i] = WritePersistent;
146 return;
147 case ReadPersistent:
148 if (conf->modes[i] == WritePersistent)
149 conf->modes[i] = AllPersist;
150 else
151 conf->modes[i] = ReadPersistent;
152 return;
153 case ReadFixable:
154 if (conf->modes[i] == WritePersistent ||
155 conf->modes[i] == ReadPersistent)
156 conf->modes[i] = AllPersist;
157 else
158 conf->modes[i] = ReadFixable;
159 return;
161 } else if (conf->modes[i] == NoPersist)
162 n = i;
164 if (n >= MaxFault)
165 return;
166 conf->faults[n] = start;
167 conf->modes[n] = mode;
168 if (conf->nfaults == n)
169 conf->nfaults = n+1;
172 static int make_request(struct request_queue *q, struct bio *bio)
174 mddev_t *mddev = q->queuedata;
175 conf_t *conf = (conf_t*)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 0;
188 if (check_sector(conf, bio->bi_sector, bio->bi_sector+(bio->bi_size>>9),
189 WRITE))
190 failit = 1;
191 if (check_mode(conf, WritePersistent)) {
192 add_sector(conf, bio->bi_sector, WritePersistent);
193 failit = 1;
195 if (check_mode(conf, WriteTransient))
196 failit = 1;
197 } else {
198 /* read request */
199 if (check_sector(conf, bio->bi_sector, bio->bi_sector + (bio->bi_size>>9),
200 READ))
201 failit = 1;
202 if (check_mode(conf, ReadTransient))
203 failit = 1;
204 if (check_mode(conf, ReadPersistent)) {
205 add_sector(conf, bio->bi_sector, ReadPersistent);
206 failit = 1;
208 if (check_mode(conf, ReadFixable)) {
209 add_sector(conf, bio->bi_sector, ReadFixable);
210 failit = 1;
213 if (failit) {
214 struct bio *b = bio_clone(bio, GFP_NOIO);
215 b->bi_bdev = conf->rdev->bdev;
216 b->bi_private = bio;
217 b->bi_end_io = faulty_fail;
218 generic_make_request(b);
219 return 0;
220 } else {
221 bio->bi_bdev = conf->rdev->bdev;
222 return 1;
226 static void status(struct seq_file *seq, mddev_t *mddev)
228 conf_t *conf = (conf_t*)mddev->private;
229 int n;
231 if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
232 seq_printf(seq, " WriteTransient=%d(%d)",
233 n, conf->period[WriteTransient]);
235 if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
236 seq_printf(seq, " ReadTransient=%d(%d)",
237 n, conf->period[ReadTransient]);
239 if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
240 seq_printf(seq, " WritePersistent=%d(%d)",
241 n, conf->period[WritePersistent]);
243 if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
244 seq_printf(seq, " ReadPersistent=%d(%d)",
245 n, conf->period[ReadPersistent]);
248 if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
249 seq_printf(seq, " ReadFixable=%d(%d)",
250 n, conf->period[ReadFixable]);
252 if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
253 seq_printf(seq, " WriteAll");
255 seq_printf(seq, " nfaults=%d", conf->nfaults);
259 static int reshape(mddev_t *mddev)
261 int mode = mddev->new_layout & ModeMask;
262 int count = mddev->new_layout >> ModeShift;
263 conf_t *conf = mddev->private;
265 if (mddev->new_layout < 0)
266 return 0;
268 /* new layout */
269 if (mode == ClearFaults)
270 conf->nfaults = 0;
271 else if (mode == ClearErrors) {
272 int i;
273 for (i=0 ; i < Modes ; i++) {
274 conf->period[i] = 0;
275 atomic_set(&conf->counters[i], 0);
277 } else if (mode < Modes) {
278 conf->period[mode] = count;
279 if (!count) count++;
280 atomic_set(&conf->counters[mode], count);
281 } else
282 return -EINVAL;
283 mddev->new_layout = -1;
284 mddev->layout = -1; /* makes sure further changes come through */
285 return 0;
288 static sector_t faulty_size(mddev_t *mddev, sector_t sectors, int raid_disks)
290 WARN_ONCE(raid_disks,
291 "%s does not support generic reshape\n", __func__);
293 if (sectors == 0)
294 return mddev->dev_sectors;
296 return sectors;
299 static int run(mddev_t *mddev)
301 mdk_rdev_t *rdev;
302 int i;
303 conf_t *conf;
305 if (md_check_no_bitmap(mddev))
306 return -EINVAL;
308 conf = kmalloc(sizeof(*conf), GFP_KERNEL);
309 if (!conf)
310 return -ENOMEM;
312 for (i=0; i<Modes; i++) {
313 atomic_set(&conf->counters[i], 0);
314 conf->period[i] = 0;
316 conf->nfaults = 0;
318 list_for_each_entry(rdev, &mddev->disks, same_set)
319 conf->rdev = rdev;
321 md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
322 mddev->private = conf;
324 reshape(mddev);
326 return 0;
329 static int stop(mddev_t *mddev)
331 conf_t *conf = (conf_t *)mddev->private;
333 kfree(conf);
334 mddev->private = NULL;
335 return 0;
338 static struct mdk_personality faulty_personality =
340 .name = "faulty",
341 .level = LEVEL_FAULTY,
342 .owner = THIS_MODULE,
343 .make_request = make_request,
344 .run = run,
345 .stop = stop,
346 .status = status,
347 .check_reshape = reshape,
348 .size = faulty_size,
351 static int __init raid_init(void)
353 return register_md_personality(&faulty_personality);
356 static void raid_exit(void)
358 unregister_md_personality(&faulty_personality);
361 module_init(raid_init);
362 module_exit(raid_exit);
363 MODULE_LICENSE("GPL");
364 MODULE_DESCRIPTION("Fault injection personality for MD");
365 MODULE_ALIAS("md-personality-10"); /* faulty */
366 MODULE_ALIAS("md-faulty");
367 MODULE_ALIAS("md-level--5");