raid6test.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * asynchronous raid6 recovery self test
  3. * Copyright (c) 2009, Intel Corporation.
  4. *
  5. * based on drivers/md/raid6test/test.c:
  6. * Copyright 2002-2007 H. Peter Anvin
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. */
  22. #include <linux/async_tx.h>
  23. #include <linux/gfp.h>
  24. #include <linux/mm.h>
  25. #include <linux/random.h>
  26. #include <linux/module.h>
  27. #undef pr
  28. #define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
  29. #define NDISKS 16 /* Including P and Q */
  30. static struct page *dataptrs[NDISKS];
  31. static addr_conv_t addr_conv[NDISKS];
  32. static struct page *data[NDISKS+3];
  33. static struct page *spare;
  34. static struct page *recovi;
  35. static struct page *recovj;
  36. static void callback(void *param)
  37. {
  38. struct completion *cmp = param;
  39. complete(cmp);
  40. }
  41. static void makedata(int disks)
  42. {
  43. int i, j;
  44. for (i = 0; i < disks; i++) {
  45. for (j = 0; j < PAGE_SIZE/sizeof(u32); j += sizeof(u32)) {
  46. u32 *p = page_address(data[i]) + j;
  47. *p = random32();
  48. }
  49. dataptrs[i] = data[i];
  50. }
  51. }
  52. static char disk_type(int d, int disks)
  53. {
  54. if (d == disks - 2)
  55. return 'P';
  56. else if (d == disks - 1)
  57. return 'Q';
  58. else
  59. return 'D';
  60. }
  61. /* Recover two failed blocks. */
  62. static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
  63. {
  64. struct async_submit_ctl submit;
  65. struct completion cmp;
  66. struct dma_async_tx_descriptor *tx = NULL;
  67. enum sum_check_flags result = ~0;
  68. if (faila > failb)
  69. swap(faila, failb);
  70. if (failb == disks-1) {
  71. if (faila == disks-2) {
  72. /* P+Q failure. Just rebuild the syndrome. */
  73. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  74. tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
  75. } else {
  76. struct page *blocks[disks];
  77. struct page *dest;
  78. int count = 0;
  79. int i;
  80. /* data+Q failure. Reconstruct data from P,
  81. * then rebuild syndrome
  82. */
  83. for (i = disks; i-- ; ) {
  84. if (i == faila || i == failb)
  85. continue;
  86. blocks[count++] = ptrs[i];
  87. }
  88. dest = ptrs[faila];
  89. init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
  90. NULL, NULL, addr_conv);
  91. tx = async_xor(dest, blocks, 0, count, bytes, &submit);
  92. init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
  93. tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
  94. }
  95. } else {
  96. if (failb == disks-2) {
  97. /* data+P failure. */
  98. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  99. tx = async_raid6_datap_recov(disks, bytes, faila, ptrs, &submit);
  100. } else {
  101. /* data+data failure. */
  102. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  103. tx = async_raid6_2data_recov(disks, bytes, faila, failb, ptrs, &submit);
  104. }
  105. }
  106. init_completion(&cmp);
  107. init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
  108. tx = async_syndrome_val(ptrs, 0, disks, bytes, &result, spare, &submit);
  109. async_tx_issue_pending(tx);
  110. if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
  111. pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
  112. __func__, faila, failb, disks);
  113. if (result != 0)
  114. pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
  115. __func__, faila, failb, result);
  116. }
  117. static int test_disks(int i, int j, int disks)
  118. {
  119. int erra, errb;
  120. memset(page_address(recovi), 0xf0, PAGE_SIZE);
  121. memset(page_address(recovj), 0xba, PAGE_SIZE);
  122. dataptrs[i] = recovi;
  123. dataptrs[j] = recovj;
  124. raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs);
  125. erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
  126. errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
  127. pr("%s(%d, %d): faila=%3d(%c) failb=%3d(%c) %s\n",
  128. __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
  129. (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
  130. dataptrs[i] = data[i];
  131. dataptrs[j] = data[j];
  132. return erra || errb;
  133. }
  134. static int test(int disks, int *tests)
  135. {
  136. struct dma_async_tx_descriptor *tx;
  137. struct async_submit_ctl submit;
  138. struct completion cmp;
  139. int err = 0;
  140. int i, j;
  141. recovi = data[disks];
  142. recovj = data[disks+1];
  143. spare = data[disks+2];
  144. makedata(disks);
  145. /* Nuke syndromes */
  146. memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
  147. memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
  148. /* Generate assumed good syndrome */
  149. init_completion(&cmp);
  150. init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
  151. tx = async_gen_syndrome(dataptrs, 0, disks, PAGE_SIZE, &submit);
  152. async_tx_issue_pending(tx);
  153. if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
  154. pr("error: initial gen_syndrome(%d) timed out\n", disks);
  155. return 1;
  156. }
  157. pr("testing the %d-disk case...\n", disks);
  158. for (i = 0; i < disks-1; i++)
  159. for (j = i+1; j < disks; j++) {
  160. (*tests)++;
  161. err += test_disks(i, j, disks);
  162. }
  163. return err;
  164. }
  165. static int raid6_test(void)
  166. {
  167. int err = 0;
  168. int tests = 0;
  169. int i;
  170. for (i = 0; i < NDISKS+3; i++) {
  171. data[i] = alloc_page(GFP_KERNEL);
  172. if (!data[i]) {
  173. while (i--)
  174. put_page(data[i]);
  175. return -ENOMEM;
  176. }
  177. }
  178. /* the 4-disk and 5-disk cases are special for the recovery code */
  179. if (NDISKS > 4)
  180. err += test(4, &tests);
  181. if (NDISKS > 5)
  182. err += test(5, &tests);
  183. /* the 11 and 12 disk cases are special for ioatdma (p-disabled
  184. * q-continuation without extended descriptor)
  185. */
  186. if (NDISKS > 12) {
  187. err += test(11, &tests);
  188. err += test(12, &tests);
  189. }
  190. err += test(NDISKS, &tests);
  191. pr("\n");
  192. pr("complete (%d tests, %d failure%s)\n",
  193. tests, err, err == 1 ? "" : "s");
  194. for (i = 0; i < NDISKS+3; i++)
  195. put_page(data[i]);
  196. return 0;
  197. }
  198. static void raid6_test_exit(void)
  199. {
  200. }
  201. /* when compiled-in wait for drivers to load first (assumes dma drivers
  202. * are also compliled-in)
  203. */
  204. late_initcall(raid6_test);
  205. module_exit(raid6_test_exit);
  206. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  207. MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
  208. MODULE_LICENSE("GPL");