faulty.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. * The "faulty" personality causes some requests to fail.
  20. *
  21. * Possible failure modes are:
  22. * reads fail "randomly" but succeed on retry
  23. * writes fail "randomly" but succeed on retry
  24. * reads for some address fail and then persist until a write
  25. * reads for some address fail and then persist irrespective of write
  26. * writes for some address fail and persist
  27. * all writes fail
  28. *
  29. * Different modes can be active at a time, but only
  30. * one can be set at array creation. Others can be added later.
  31. * A mode can be one-shot or recurrent with the recurrence being
  32. * once in every N requests.
  33. * The bottom 5 bits of the "layout" indicate the mode. The
  34. * remainder indicate a period, or 0 for one-shot.
  35. *
  36. * There is an implementation limit on the number of concurrently
  37. * persisting-faulty blocks. When a new fault is requested that would
  38. * exceed the limit, it is ignored.
  39. * All current faults can be clear using a layout of "0".
  40. *
  41. * Requests are always sent to the device. If they are to fail,
  42. * we clone the bio and insert a new b_end_io into the chain.
  43. */
  44. #define WriteTransient 0
  45. #define ReadTransient 1
  46. #define WritePersistent 2
  47. #define ReadPersistent 3
  48. #define WriteAll 4 /* doesn't go to device */
  49. #define ReadFixable 5
  50. #define Modes 6
  51. #define ClearErrors 31
  52. #define ClearFaults 30
  53. #define AllPersist 100 /* internal use only */
  54. #define NoPersist 101
  55. #define ModeMask 0x1f
  56. #define ModeShift 5
  57. #define MaxFault 50
  58. #include <linux/blkdev.h>
  59. #include <linux/raid/md_u.h>
  60. #include <linux/slab.h>
  61. #include "md.h"
  62. #include <linux/seq_file.h>
  63. static void faulty_fail(struct bio *bio, int error)
  64. {
  65. struct bio *b = bio->bi_private;
  66. b->bi_size = bio->bi_size;
  67. b->bi_sector = bio->bi_sector;
  68. bio_put(bio);
  69. bio_io_error(b);
  70. }
  71. typedef struct faulty_conf {
  72. int period[Modes];
  73. atomic_t counters[Modes];
  74. sector_t faults[MaxFault];
  75. int modes[MaxFault];
  76. int nfaults;
  77. mdk_rdev_t *rdev;
  78. } conf_t;
  79. static int check_mode(conf_t *conf, int mode)
  80. {
  81. if (conf->period[mode] == 0 &&
  82. atomic_read(&conf->counters[mode]) <= 0)
  83. return 0; /* no failure, no decrement */
  84. if (atomic_dec_and_test(&conf->counters[mode])) {
  85. if (conf->period[mode])
  86. atomic_set(&conf->counters[mode], conf->period[mode]);
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. static int check_sector(conf_t *conf, sector_t start, sector_t end, int dir)
  92. {
  93. /* If we find a ReadFixable sector, we fix it ... */
  94. int i;
  95. for (i=0; i<conf->nfaults; i++)
  96. if (conf->faults[i] >= start &&
  97. conf->faults[i] < end) {
  98. /* found it ... */
  99. switch (conf->modes[i] * 2 + dir) {
  100. case WritePersistent*2+WRITE: return 1;
  101. case ReadPersistent*2+READ: return 1;
  102. case ReadFixable*2+READ: return 1;
  103. case ReadFixable*2+WRITE:
  104. conf->modes[i] = NoPersist;
  105. return 0;
  106. case AllPersist*2+READ:
  107. case AllPersist*2+WRITE: return 1;
  108. default:
  109. return 0;
  110. }
  111. }
  112. return 0;
  113. }
  114. static void add_sector(conf_t *conf, sector_t start, int mode)
  115. {
  116. int i;
  117. int n = conf->nfaults;
  118. for (i=0; i<conf->nfaults; i++)
  119. if (conf->faults[i] == start) {
  120. switch(mode) {
  121. case NoPersist: conf->modes[i] = mode; return;
  122. case WritePersistent:
  123. if (conf->modes[i] == ReadPersistent ||
  124. conf->modes[i] == ReadFixable)
  125. conf->modes[i] = AllPersist;
  126. else
  127. conf->modes[i] = WritePersistent;
  128. return;
  129. case ReadPersistent:
  130. if (conf->modes[i] == WritePersistent)
  131. conf->modes[i] = AllPersist;
  132. else
  133. conf->modes[i] = ReadPersistent;
  134. return;
  135. case ReadFixable:
  136. if (conf->modes[i] == WritePersistent ||
  137. conf->modes[i] == ReadPersistent)
  138. conf->modes[i] = AllPersist;
  139. else
  140. conf->modes[i] = ReadFixable;
  141. return;
  142. }
  143. } else if (conf->modes[i] == NoPersist)
  144. n = i;
  145. if (n >= MaxFault)
  146. return;
  147. conf->faults[n] = start;
  148. conf->modes[n] = mode;
  149. if (conf->nfaults == n)
  150. conf->nfaults = n+1;
  151. }
  152. static int make_request(mddev_t *mddev, struct bio *bio)
  153. {
  154. conf_t *conf = mddev->private;
  155. int failit = 0;
  156. if (bio_data_dir(bio) == WRITE) {
  157. /* write request */
  158. if (atomic_read(&conf->counters[WriteAll])) {
  159. /* special case - don't decrement, don't generic_make_request,
  160. * just fail immediately
  161. */
  162. bio_endio(bio, -EIO);
  163. return 0;
  164. }
  165. if (check_sector(conf, bio->bi_sector, bio->bi_sector+(bio->bi_size>>9),
  166. WRITE))
  167. failit = 1;
  168. if (check_mode(conf, WritePersistent)) {
  169. add_sector(conf, bio->bi_sector, WritePersistent);
  170. failit = 1;
  171. }
  172. if (check_mode(conf, WriteTransient))
  173. failit = 1;
  174. } else {
  175. /* read request */
  176. if (check_sector(conf, bio->bi_sector, bio->bi_sector + (bio->bi_size>>9),
  177. READ))
  178. failit = 1;
  179. if (check_mode(conf, ReadTransient))
  180. failit = 1;
  181. if (check_mode(conf, ReadPersistent)) {
  182. add_sector(conf, bio->bi_sector, ReadPersistent);
  183. failit = 1;
  184. }
  185. if (check_mode(conf, ReadFixable)) {
  186. add_sector(conf, bio->bi_sector, ReadFixable);
  187. failit = 1;
  188. }
  189. }
  190. if (failit) {
  191. struct bio *b = bio_clone_mddev(bio, GFP_NOIO, mddev);
  192. b->bi_bdev = conf->rdev->bdev;
  193. b->bi_private = bio;
  194. b->bi_end_io = faulty_fail;
  195. generic_make_request(b);
  196. return 0;
  197. } else {
  198. bio->bi_bdev = conf->rdev->bdev;
  199. return 1;
  200. }
  201. }
  202. static void status(struct seq_file *seq, mddev_t *mddev)
  203. {
  204. conf_t *conf = mddev->private;
  205. int n;
  206. if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
  207. seq_printf(seq, " WriteTransient=%d(%d)",
  208. n, conf->period[WriteTransient]);
  209. if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
  210. seq_printf(seq, " ReadTransient=%d(%d)",
  211. n, conf->period[ReadTransient]);
  212. if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
  213. seq_printf(seq, " WritePersistent=%d(%d)",
  214. n, conf->period[WritePersistent]);
  215. if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
  216. seq_printf(seq, " ReadPersistent=%d(%d)",
  217. n, conf->period[ReadPersistent]);
  218. if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
  219. seq_printf(seq, " ReadFixable=%d(%d)",
  220. n, conf->period[ReadFixable]);
  221. if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
  222. seq_printf(seq, " WriteAll");
  223. seq_printf(seq, " nfaults=%d", conf->nfaults);
  224. }
  225. static int reshape(mddev_t *mddev)
  226. {
  227. int mode = mddev->new_layout & ModeMask;
  228. int count = mddev->new_layout >> ModeShift;
  229. conf_t *conf = mddev->private;
  230. if (mddev->new_layout < 0)
  231. return 0;
  232. /* new layout */
  233. if (mode == ClearFaults)
  234. conf->nfaults = 0;
  235. else if (mode == ClearErrors) {
  236. int i;
  237. for (i=0 ; i < Modes ; i++) {
  238. conf->period[i] = 0;
  239. atomic_set(&conf->counters[i], 0);
  240. }
  241. } else if (mode < Modes) {
  242. conf->period[mode] = count;
  243. if (!count) count++;
  244. atomic_set(&conf->counters[mode], count);
  245. } else
  246. return -EINVAL;
  247. mddev->new_layout = -1;
  248. mddev->layout = -1; /* makes sure further changes come through */
  249. return 0;
  250. }
  251. static sector_t faulty_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  252. {
  253. WARN_ONCE(raid_disks,
  254. "%s does not support generic reshape\n", __func__);
  255. if (sectors == 0)
  256. return mddev->dev_sectors;
  257. return sectors;
  258. }
  259. static int run(mddev_t *mddev)
  260. {
  261. mdk_rdev_t *rdev;
  262. int i;
  263. conf_t *conf;
  264. if (md_check_no_bitmap(mddev))
  265. return -EINVAL;
  266. conf = kmalloc(sizeof(*conf), GFP_KERNEL);
  267. if (!conf)
  268. return -ENOMEM;
  269. for (i=0; i<Modes; i++) {
  270. atomic_set(&conf->counters[i], 0);
  271. conf->period[i] = 0;
  272. }
  273. conf->nfaults = 0;
  274. list_for_each_entry(rdev, &mddev->disks, same_set)
  275. conf->rdev = rdev;
  276. md_set_array_sectors(mddev, faulty_size(mddev, 0, 0));
  277. mddev->private = conf;
  278. reshape(mddev);
  279. return 0;
  280. }
  281. static int stop(mddev_t *mddev)
  282. {
  283. conf_t *conf = mddev->private;
  284. kfree(conf);
  285. mddev->private = NULL;
  286. return 0;
  287. }
  288. static struct mdk_personality faulty_personality =
  289. {
  290. .name = "faulty",
  291. .level = LEVEL_FAULTY,
  292. .owner = THIS_MODULE,
  293. .make_request = make_request,
  294. .run = run,
  295. .stop = stop,
  296. .status = status,
  297. .check_reshape = reshape,
  298. .size = faulty_size,
  299. };
  300. static int __init raid_init(void)
  301. {
  302. return register_md_personality(&faulty_personality);
  303. }
  304. static void raid_exit(void)
  305. {
  306. unregister_md_personality(&faulty_personality);
  307. }
  308. module_init(raid_init);
  309. module_exit(raid_exit);
  310. MODULE_LICENSE("GPL");
  311. MODULE_DESCRIPTION("Fault injection personality for MD");
  312. MODULE_ALIAS("md-personality-10"); /* faulty */
  313. MODULE_ALIAS("md-faulty");
  314. MODULE_ALIAS("md-level--5");