]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/md/faulty.c
block: prep work for batch completion
[karo-tx-linux.git] / 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 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.
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/module.h>
67 #include <linux/raid/md_u.h>
68 #include <linux/slab.h>
69 #include "md.h"
70 #include <linux/seq_file.h>
71
72
73 static void faulty_fail(struct bio *bio, int error,
74                         struct batch_complete *batch)
75 {
76         struct bio *b = bio->bi_private;
77
78         b->bi_size = bio->bi_size;
79         b->bi_sector = bio->bi_sector;
80
81         bio_put(bio);
82
83         bio_io_error(b);
84 }
85
86 struct faulty_conf {
87         int period[Modes];
88         atomic_t counters[Modes];
89         sector_t faults[MaxFault];
90         int     modes[MaxFault];
91         int nfaults;
92         struct md_rdev *rdev;
93 };
94
95 static int check_mode(struct faulty_conf *conf, int mode)
96 {
97         if (conf->period[mode] == 0 &&
98             atomic_read(&conf->counters[mode]) <= 0)
99                 return 0; /* no failure, no decrement */
100
101
102         if (atomic_dec_and_test(&conf->counters[mode])) {
103                 if (conf->period[mode])
104                         atomic_set(&conf->counters[mode], conf->period[mode]);
105                 return 1;
106         }
107         return 0;
108 }
109
110 static int check_sector(struct faulty_conf *conf, sector_t start, sector_t end, int dir)
111 {
112         /* If we find a ReadFixable sector, we fix it ... */
113         int i;
114         for (i=0; i<conf->nfaults; i++)
115                 if (conf->faults[i] >= start &&
116                     conf->faults[i] < end) {
117                         /* found it ... */
118                         switch (conf->modes[i] * 2 + dir) {
119                         case WritePersistent*2+WRITE: return 1;
120                         case ReadPersistent*2+READ: return 1;
121                         case ReadFixable*2+READ: return 1;
122                         case ReadFixable*2+WRITE:
123                                 conf->modes[i] = NoPersist;
124                                 return 0;
125                         case AllPersist*2+READ:
126                         case AllPersist*2+WRITE: return 1;
127                         default:
128                                 return 0;
129                         }
130                 }
131         return 0;
132 }
133
134 static void add_sector(struct faulty_conf *conf, sector_t start, int mode)
135 {
136         int i;
137         int n = conf->nfaults;
138         for (i=0; i<conf->nfaults; i++)
139                 if (conf->faults[i] == start) {
140                         switch(mode) {
141                         case NoPersist: conf->modes[i] = mode; return;
142                         case WritePersistent:
143                                 if (conf->modes[i] == ReadPersistent ||
144                                     conf->modes[i] == ReadFixable)
145                                         conf->modes[i] = AllPersist;
146                                 else
147                                         conf->modes[i] = WritePersistent;
148                                 return;
149                         case ReadPersistent:
150                                 if (conf->modes[i] == WritePersistent)
151                                         conf->modes[i] = AllPersist;
152                                 else
153                                         conf->modes[i] = ReadPersistent;
154                                 return;
155                         case ReadFixable:
156                                 if (conf->modes[i] == WritePersistent ||
157                                     conf->modes[i] == ReadPersistent)
158                                         conf->modes[i] = AllPersist;
159                                 else
160                                         conf->modes[i] = ReadFixable;
161                                 return;
162                         }
163                 } else if (conf->modes[i] == NoPersist)
164                         n = i;
165
166         if (n >= MaxFault)
167                 return;
168         conf->faults[n] = start;
169         conf->modes[n] = mode;
170         if (conf->nfaults == n)
171                 conf->nfaults = n+1;
172 }
173
174 static void make_request(struct mddev *mddev, struct bio *bio)
175 {
176         struct faulty_conf *conf = mddev->private;
177         int failit = 0;
178
179         if (bio_data_dir(bio) == WRITE) {
180                 /* write request */
181                 if (atomic_read(&conf->counters[WriteAll])) {
182                         /* special case - don't decrement, don't generic_make_request,
183                          * just fail immediately
184                          */
185                         bio_endio(bio, -EIO);
186                         return;
187                 }
188
189                 if (check_sector(conf, bio->bi_sector, bio_end_sector(bio), WRITE))
190                         failit = 1;
191                 if (check_mode(conf, WritePersistent)) {
192                         add_sector(conf, bio->bi_sector, WritePersistent);
193                         failit = 1;
194                 }
195                 if (check_mode(conf, WriteTransient))
196                         failit = 1;
197         } else {
198                 /* read request */
199                 if (check_sector(conf, bio->bi_sector, bio_end_sector(bio), READ))
200                         failit = 1;
201                 if (check_mode(conf, ReadTransient))
202                         failit = 1;
203                 if (check_mode(conf, ReadPersistent)) {
204                         add_sector(conf, bio->bi_sector, ReadPersistent);
205                         failit = 1;
206                 }
207                 if (check_mode(conf, ReadFixable)) {
208                         add_sector(conf, bio->bi_sector, ReadFixable);
209                         failit = 1;
210                 }
211         }
212         if (failit) {
213                 struct bio *b = bio_clone_mddev(bio, GFP_NOIO, mddev);
214
215                 b->bi_bdev = conf->rdev->bdev;
216                 b->bi_private = bio;
217                 b->bi_end_io = faulty_fail;
218                 bio = b;
219         } else
220                 bio->bi_bdev = conf->rdev->bdev;
221
222         generic_make_request(bio);
223 }
224
225 static void status(struct seq_file *seq, struct mddev *mddev)
226 {
227         struct faulty_conf *conf = mddev->private;
228         int n;
229
230         if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
231                 seq_printf(seq, " WriteTransient=%d(%d)",
232                            n, conf->period[WriteTransient]);
233
234         if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
235                 seq_printf(seq, " ReadTransient=%d(%d)",
236                            n, conf->period[ReadTransient]);
237
238         if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
239                 seq_printf(seq, " WritePersistent=%d(%d)",
240                            n, conf->period[WritePersistent]);
241
242         if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
243                 seq_printf(seq, " ReadPersistent=%d(%d)",
244                            n, conf->period[ReadPersistent]);
245
246
247         if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
248                 seq_printf(seq, " ReadFixable=%d(%d)",
249                            n, conf->period[ReadFixable]);
250
251         if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
252                 seq_printf(seq, " WriteAll");
253
254         seq_printf(seq, " nfaults=%d", conf->nfaults);
255 }
256
257
258 static int reshape(struct mddev *mddev)
259 {
260         int mode = mddev->new_layout & ModeMask;
261         int count = mddev->new_layout >> ModeShift;
262         struct faulty_conf *conf = mddev->private;
263
264         if (mddev->new_layout < 0)
265                 return 0;
266
267         /* new layout */
268         if (mode == ClearFaults)
269                 conf->nfaults = 0;
270         else if (mode == ClearErrors) {
271                 int i;
272                 for (i=0 ; i < Modes ; i++) {
273                         conf->period[i] = 0;
274                         atomic_set(&conf->counters[i], 0);
275                 }
276         } else if (mode < Modes) {
277                 conf->period[mode] = count;
278                 if (!count) count++;
279                 atomic_set(&conf->counters[mode], count);
280         } else
281                 return -EINVAL;
282         mddev->new_layout = -1;
283         mddev->layout = -1; /* makes sure further changes come through */
284         return 0;
285 }
286
287 static sector_t faulty_size(struct mddev *mddev, sector_t sectors, int raid_disks)
288 {
289         WARN_ONCE(raid_disks,
290                   "%s does not support generic reshape\n", __func__);
291
292         if (sectors == 0)
293                 return mddev->dev_sectors;
294
295         return sectors;
296 }
297
298 static int run(struct mddev *mddev)
299 {
300         struct md_rdev *rdev;
301         int i;
302         struct faulty_conf *conf;
303
304         if (md_check_no_bitmap(mddev))
305                 return -EINVAL;
306
307         conf = kmalloc(sizeof(*conf), GFP_KERNEL);
308         if (!conf)
309                 return -ENOMEM;
310
311         for (i=0; i<Modes; i++) {
312                 atomic_set(&conf->counters[i], 0);
313                 conf->period[i] = 0;
314         }
315         conf->nfaults = 0;
316
317         rdev_for_each(rdev, mddev) {
318                 conf->rdev = rdev;
319                 disk_stack_limits(mddev->gendisk, rdev->bdev,
320                                   rdev->data_offset << 9);
321         }
322
323         md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
324         mddev->private = conf;
325
326         reshape(mddev);
327
328         return 0;
329 }
330
331 static int stop(struct mddev *mddev)
332 {
333         struct faulty_conf *conf = mddev->private;
334
335         kfree(conf);
336         mddev->private = NULL;
337         return 0;
338 }
339
340 static struct md_personality faulty_personality =
341 {
342         .name           = "faulty",
343         .level          = LEVEL_FAULTY,
344         .owner          = THIS_MODULE,
345         .make_request   = make_request,
346         .run            = run,
347         .stop           = stop,
348         .status         = status,
349         .check_reshape  = reshape,
350         .size           = faulty_size,
351 };
352
353 static int __init raid_init(void)
354 {
355         return register_md_personality(&faulty_personality);
356 }
357
358 static void raid_exit(void)
359 {
360         unregister_md_personality(&faulty_personality);
361 }
362
363 module_init(raid_init);
364 module_exit(raid_exit);
365 MODULE_LICENSE("GPL");
366 MODULE_DESCRIPTION("Fault injection personality for MD");
367 MODULE_ALIAS("md-personality-10"); /* faulty */
368 MODULE_ALIAS("md-faulty");
369 MODULE_ALIAS("md-level--5");