]> Pileus Git - ~andy/linux/blob - drivers/md/faulty.c
md: pass mddev to make_request functions rather than request_queue
[~andy/linux] / drivers / md / faulty.c
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 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.
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/raid/md_u.h>
67 #include "md.h"
68 #include <linux/seq_file.h>
69
70
71 static void faulty_fail(struct bio *bio, int error)
72 {
73         struct bio *b = bio->bi_private;
74
75         b->bi_size = bio->bi_size;
76         b->bi_sector = bio->bi_sector;
77
78         bio_put(bio);
79
80         bio_io_error(b);
81 }
82
83 typedef struct faulty_conf {
84         int period[Modes];
85         atomic_t counters[Modes];
86         sector_t faults[MaxFault];
87         int     modes[MaxFault];
88         int nfaults;
89         mdk_rdev_t *rdev;
90 } conf_t;
91
92 static int check_mode(conf_t *conf, int mode)
93 {
94         if (conf->period[mode] == 0 &&
95             atomic_read(&conf->counters[mode]) <= 0)
96                 return 0; /* no failure, no decrement */
97
98
99         if (atomic_dec_and_test(&conf->counters[mode])) {
100                 if (conf->period[mode])
101                         atomic_set(&conf->counters[mode], conf->period[mode]);
102                 return 1;
103         }
104         return 0;
105 }
106
107 static int check_sector(conf_t *conf, sector_t start, sector_t end, int dir)
108 {
109         /* If we find a ReadFixable sector, we fix it ... */
110         int i;
111         for (i=0; i<conf->nfaults; i++)
112                 if (conf->faults[i] >= start &&
113                     conf->faults[i] < end) {
114                         /* found it ... */
115                         switch (conf->modes[i] * 2 + dir) {
116                         case WritePersistent*2+WRITE: return 1;
117                         case ReadPersistent*2+READ: return 1;
118                         case ReadFixable*2+READ: return 1;
119                         case ReadFixable*2+WRITE:
120                                 conf->modes[i] = NoPersist;
121                                 return 0;
122                         case AllPersist*2+READ:
123                         case AllPersist*2+WRITE: return 1;
124                         default:
125                                 return 0;
126                         }
127                 }
128         return 0;
129 }
130
131 static void add_sector(conf_t *conf, sector_t start, int mode)
132 {
133         int i;
134         int n = conf->nfaults;
135         for (i=0; i<conf->nfaults; i++)
136                 if (conf->faults[i] == start) {
137                         switch(mode) {
138                         case NoPersist: conf->modes[i] = mode; return;
139                         case WritePersistent:
140                                 if (conf->modes[i] == ReadPersistent ||
141                                     conf->modes[i] == ReadFixable)
142                                         conf->modes[i] = AllPersist;
143                                 else
144                                         conf->modes[i] = WritePersistent;
145                                 return;
146                         case ReadPersistent:
147                                 if (conf->modes[i] == WritePersistent)
148                                         conf->modes[i] = AllPersist;
149                                 else
150                                         conf->modes[i] = ReadPersistent;
151                                 return;
152                         case ReadFixable:
153                                 if (conf->modes[i] == WritePersistent ||
154                                     conf->modes[i] == ReadPersistent)
155                                         conf->modes[i] = AllPersist;
156                                 else
157                                         conf->modes[i] = ReadFixable;
158                                 return;
159                         }
160                 } else if (conf->modes[i] == NoPersist)
161                         n = i;
162
163         if (n >= MaxFault)
164                 return;
165         conf->faults[n] = start;
166         conf->modes[n] = mode;
167         if (conf->nfaults == n)
168                 conf->nfaults = n+1;
169 }
170
171 static int make_request(mddev_t *mddev, struct bio *bio)
172 {
173         conf_t *conf = mddev->private;
174         int failit = 0;
175
176         if (bio_data_dir(bio) == WRITE) {
177                 /* write request */
178                 if (atomic_read(&conf->counters[WriteAll])) {
179                         /* special case - don't decrement, don't generic_make_request,
180                          * just fail immediately
181                          */
182                         bio_endio(bio, -EIO);
183                         return 0;
184                 }
185
186                 if (check_sector(conf, bio->bi_sector, bio->bi_sector+(bio->bi_size>>9),
187                                  WRITE))
188                         failit = 1;
189                 if (check_mode(conf, WritePersistent)) {
190                         add_sector(conf, bio->bi_sector, WritePersistent);
191                         failit = 1;
192                 }
193                 if (check_mode(conf, WriteTransient))
194                         failit = 1;
195         } else {
196                 /* read request */
197                 if (check_sector(conf, bio->bi_sector, bio->bi_sector + (bio->bi_size>>9),
198                                  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;
205                 }
206                 if (check_mode(conf, ReadFixable)) {
207                         add_sector(conf, bio->bi_sector, ReadFixable);
208                         failit = 1;
209                 }
210         }
211         if (failit) {
212                 struct bio *b = bio_clone(bio, GFP_NOIO);
213                 b->bi_bdev = conf->rdev->bdev;
214                 b->bi_private = bio;
215                 b->bi_end_io = faulty_fail;
216                 generic_make_request(b);
217                 return 0;
218         } else {
219                 bio->bi_bdev = conf->rdev->bdev;
220                 return 1;
221         }
222 }
223
224 static void status(struct seq_file *seq, mddev_t *mddev)
225 {
226         conf_t *conf = mddev->private;
227         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
257 static int reshape(mddev_t *mddev)
258 {
259         int mode = mddev->new_layout & ModeMask;
260         int count = mddev->new_layout >> ModeShift;
261         conf_t *conf = mddev->private;
262
263         if (mddev->new_layout < 0)
264                 return 0;
265
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;
281         mddev->new_layout = -1;
282         mddev->layout = -1; /* makes sure further changes come through */
283         return 0;
284 }
285
286 static sector_t faulty_size(mddev_t *mddev, sector_t sectors, int raid_disks)
287 {
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
297 static int run(mddev_t *mddev)
298 {
299         mdk_rdev_t *rdev;
300         int i;
301         conf_t *conf;
302
303         if (md_check_no_bitmap(mddev))
304                 return -EINVAL;
305
306         conf = kmalloc(sizeof(*conf), GFP_KERNEL);
307         if (!conf)
308                 return -ENOMEM;
309
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
316         list_for_each_entry(rdev, &mddev->disks, same_set)
317                 conf->rdev = rdev;
318
319         md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
320         mddev->private = conf;
321
322         reshape(mddev);
323
324         return 0;
325 }
326
327 static int stop(mddev_t *mddev)
328 {
329         conf_t *conf = mddev->private;
330
331         kfree(conf);
332         mddev->private = NULL;
333         return 0;
334 }
335
336 static struct mdk_personality faulty_personality =
337 {
338         .name           = "faulty",
339         .level          = LEVEL_FAULTY,
340         .owner          = THIS_MODULE,
341         .make_request   = make_request,
342         .run            = run,
343         .stop           = stop,
344         .status         = status,
345         .check_reshape  = reshape,
346         .size           = faulty_size,
347 };
348
349 static int __init raid_init(void)
350 {
351         return register_md_personality(&faulty_personality);
352 }
353
354 static void raid_exit(void)
355 {
356         unregister_md_personality(&faulty_personality);
357 }
358
359 module_init(raid_init);
360 module_exit(raid_exit);
361 MODULE_LICENSE("GPL");
362 MODULE_DESCRIPTION("Fault injection personality for MD");
363 MODULE_ALIAS("md-personality-10"); /* faulty */
364 MODULE_ALIAS("md-faulty");
365 MODULE_ALIAS("md-level--5");