faulty.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 recurrance 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/raid/md.h>
  59. static void faulty_fail(struct bio *bio, int error)
  60. {
  61. struct bio *b = bio->bi_private;
  62. b->bi_size = bio->bi_size;
  63. b->bi_sector = bio->bi_sector;
  64. bio_put(bio);
  65. bio_io_error(b);
  66. }
  67. typedef struct faulty_conf {
  68. int period[Modes];
  69. atomic_t counters[Modes];
  70. sector_t faults[MaxFault];
  71. int modes[MaxFault];
  72. int nfaults;
  73. mdk_rdev_t *rdev;
  74. } conf_t;
  75. static int check_mode(conf_t *conf, int mode)
  76. {
  77. if (conf->period[mode] == 0 &&
  78. atomic_read(&conf->counters[mode]) <= 0)
  79. return 0; /* no failure, no decrement */
  80. if (atomic_dec_and_test(&conf->counters[mode])) {
  81. if (conf->period[mode])
  82. atomic_set(&conf->counters[mode], conf->period[mode]);
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. static int check_sector(conf_t *conf, sector_t start, sector_t end, int dir)
  88. {
  89. /* If we find a ReadFixable sector, we fix it ... */
  90. int i;
  91. for (i=0; i<conf->nfaults; i++)
  92. if (conf->faults[i] >= start &&
  93. conf->faults[i] < end) {
  94. /* found it ... */
  95. switch (conf->modes[i] * 2 + dir) {
  96. case WritePersistent*2+WRITE: return 1;
  97. case ReadPersistent*2+READ: return 1;
  98. case ReadFixable*2+READ: return 1;
  99. case ReadFixable*2+WRITE:
  100. conf->modes[i] = NoPersist;
  101. return 0;
  102. case AllPersist*2+READ:
  103. case AllPersist*2+WRITE: return 1;
  104. default:
  105. return 0;
  106. }
  107. }
  108. return 0;
  109. }
  110. static void add_sector(conf_t *conf, sector_t start, int mode)
  111. {
  112. int i;
  113. int n = conf->nfaults;
  114. for (i=0; i<conf->nfaults; i++)
  115. if (conf->faults[i] == start) {
  116. switch(mode) {
  117. case NoPersist: conf->modes[i] = mode; return;
  118. case WritePersistent:
  119. if (conf->modes[i] == ReadPersistent ||
  120. conf->modes[i] == ReadFixable)
  121. conf->modes[i] = AllPersist;
  122. else
  123. conf->modes[i] = WritePersistent;
  124. return;
  125. case ReadPersistent:
  126. if (conf->modes[i] == WritePersistent)
  127. conf->modes[i] = AllPersist;
  128. else
  129. conf->modes[i] = ReadPersistent;
  130. return;
  131. case ReadFixable:
  132. if (conf->modes[i] == WritePersistent ||
  133. conf->modes[i] == ReadPersistent)
  134. conf->modes[i] = AllPersist;
  135. else
  136. conf->modes[i] = ReadFixable;
  137. return;
  138. }
  139. } else if (conf->modes[i] == NoPersist)
  140. n = i;
  141. if (n >= MaxFault)
  142. return;
  143. conf->faults[n] = start;
  144. conf->modes[n] = mode;
  145. if (conf->nfaults == n)
  146. conf->nfaults = n+1;
  147. }
  148. static int make_request(struct request_queue *q, struct bio *bio)
  149. {
  150. mddev_t *mddev = q->queuedata;
  151. conf_t *conf = (conf_t*)mddev->private;
  152. int failit = 0;
  153. if (bio_data_dir(bio) == WRITE) {
  154. /* write request */
  155. if (atomic_read(&conf->counters[WriteAll])) {
  156. /* special case - don't decrement, don't generic_make_request,
  157. * just fail immediately
  158. */
  159. bio_endio(bio, -EIO);
  160. return 0;
  161. }
  162. if (check_sector(conf, bio->bi_sector, bio->bi_sector+(bio->bi_size>>9),
  163. WRITE))
  164. failit = 1;
  165. if (check_mode(conf, WritePersistent)) {
  166. add_sector(conf, bio->bi_sector, WritePersistent);
  167. failit = 1;
  168. }
  169. if (check_mode(conf, WriteTransient))
  170. failit = 1;
  171. } else {
  172. /* read request */
  173. if (check_sector(conf, bio->bi_sector, bio->bi_sector + (bio->bi_size>>9),
  174. READ))
  175. failit = 1;
  176. if (check_mode(conf, ReadTransient))
  177. failit = 1;
  178. if (check_mode(conf, ReadPersistent)) {
  179. add_sector(conf, bio->bi_sector, ReadPersistent);
  180. failit = 1;
  181. }
  182. if (check_mode(conf, ReadFixable)) {
  183. add_sector(conf, bio->bi_sector, ReadFixable);
  184. failit = 1;
  185. }
  186. }
  187. if (failit) {
  188. struct bio *b = bio_clone(bio, GFP_NOIO);
  189. b->bi_bdev = conf->rdev->bdev;
  190. b->bi_private = bio;
  191. b->bi_end_io = faulty_fail;
  192. generic_make_request(b);
  193. return 0;
  194. } else {
  195. bio->bi_bdev = conf->rdev->bdev;
  196. return 1;
  197. }
  198. }
  199. static void status(struct seq_file *seq, mddev_t *mddev)
  200. {
  201. conf_t *conf = (conf_t*)mddev->private;
  202. int n;
  203. if ((n=atomic_read(&conf->counters[WriteTransient])) != 0)
  204. seq_printf(seq, " WriteTransient=%d(%d)",
  205. n, conf->period[WriteTransient]);
  206. if ((n=atomic_read(&conf->counters[ReadTransient])) != 0)
  207. seq_printf(seq, " ReadTransient=%d(%d)",
  208. n, conf->period[ReadTransient]);
  209. if ((n=atomic_read(&conf->counters[WritePersistent])) != 0)
  210. seq_printf(seq, " WritePersistent=%d(%d)",
  211. n, conf->period[WritePersistent]);
  212. if ((n=atomic_read(&conf->counters[ReadPersistent])) != 0)
  213. seq_printf(seq, " ReadPersistent=%d(%d)",
  214. n, conf->period[ReadPersistent]);
  215. if ((n=atomic_read(&conf->counters[ReadFixable])) != 0)
  216. seq_printf(seq, " ReadFixable=%d(%d)",
  217. n, conf->period[ReadFixable]);
  218. if ((n=atomic_read(&conf->counters[WriteAll])) != 0)
  219. seq_printf(seq, " WriteAll");
  220. seq_printf(seq, " nfaults=%d", conf->nfaults);
  221. }
  222. static int reconfig(mddev_t *mddev, int layout, int chunk_size)
  223. {
  224. int mode = layout & ModeMask;
  225. int count = layout >> ModeShift;
  226. conf_t *conf = mddev->private;
  227. if (chunk_size != -1)
  228. return -EINVAL;
  229. /* new layout */
  230. if (mode == ClearFaults)
  231. conf->nfaults = 0;
  232. else if (mode == ClearErrors) {
  233. int i;
  234. for (i=0 ; i < Modes ; i++) {
  235. conf->period[i] = 0;
  236. atomic_set(&conf->counters[i], 0);
  237. }
  238. } else if (mode < Modes) {
  239. conf->period[mode] = count;
  240. if (!count) count++;
  241. atomic_set(&conf->counters[mode], count);
  242. } else
  243. return -EINVAL;
  244. mddev->layout = -1; /* makes sure further changes come through */
  245. return 0;
  246. }
  247. static int run(mddev_t *mddev)
  248. {
  249. mdk_rdev_t *rdev;
  250. struct list_head *tmp;
  251. int i;
  252. conf_t *conf = kmalloc(sizeof(*conf), GFP_KERNEL);
  253. for (i=0; i<Modes; i++) {
  254. atomic_set(&conf->counters[i], 0);
  255. conf->period[i] = 0;
  256. }
  257. conf->nfaults = 0;
  258. rdev_for_each(rdev, tmp, mddev)
  259. conf->rdev = rdev;
  260. mddev->array_sectors = mddev->size * 2;
  261. mddev->private = conf;
  262. reconfig(mddev, mddev->layout, -1);
  263. return 0;
  264. }
  265. static int stop(mddev_t *mddev)
  266. {
  267. conf_t *conf = (conf_t *)mddev->private;
  268. kfree(conf);
  269. mddev->private = NULL;
  270. return 0;
  271. }
  272. static struct mdk_personality faulty_personality =
  273. {
  274. .name = "faulty",
  275. .level = LEVEL_FAULTY,
  276. .owner = THIS_MODULE,
  277. .make_request = make_request,
  278. .run = run,
  279. .stop = stop,
  280. .status = status,
  281. .reconfig = reconfig,
  282. };
  283. static int __init raid_init(void)
  284. {
  285. return register_md_personality(&faulty_personality);
  286. }
  287. static void raid_exit(void)
  288. {
  289. unregister_md_personality(&faulty_personality);
  290. }
  291. module_init(raid_init);
  292. module_exit(raid_exit);
  293. MODULE_LICENSE("GPL");
  294. MODULE_ALIAS("md-personality-10"); /* faulty */
  295. MODULE_ALIAS("md-faulty");
  296. MODULE_ALIAS("md-level--5");