raid6test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 64 /* 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;
  44. for (i = 0; i < disks; i++) {
  45. prandom_bytes(page_address(data[i]), PAGE_SIZE);
  46. dataptrs[i] = data[i];
  47. }
  48. }
  49. static char disk_type(int d, int disks)
  50. {
  51. if (d == disks - 2)
  52. return 'P';
  53. else if (d == disks - 1)
  54. return 'Q';
  55. else
  56. return 'D';
  57. }
  58. /* Recover two failed blocks. */
  59. static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
  60. {
  61. struct async_submit_ctl submit;
  62. struct completion cmp;
  63. struct dma_async_tx_descriptor *tx = NULL;
  64. enum sum_check_flags result = ~0;
  65. if (faila > failb)
  66. swap(faila, failb);
  67. if (failb == disks-1) {
  68. if (faila == disks-2) {
  69. /* P+Q failure. Just rebuild the syndrome. */
  70. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  71. tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
  72. } else {
  73. struct page *blocks[disks];
  74. struct page *dest;
  75. int count = 0;
  76. int i;
  77. /* data+Q failure. Reconstruct data from P,
  78. * then rebuild syndrome
  79. */
  80. for (i = disks; i-- ; ) {
  81. if (i == faila || i == failb)
  82. continue;
  83. blocks[count++] = ptrs[i];
  84. }
  85. dest = ptrs[faila];
  86. init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
  87. NULL, NULL, addr_conv);
  88. tx = async_xor(dest, blocks, 0, count, bytes, &submit);
  89. init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
  90. tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
  91. }
  92. } else {
  93. if (failb == disks-2) {
  94. /* data+P failure. */
  95. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  96. tx = async_raid6_datap_recov(disks, bytes, faila, ptrs, &submit);
  97. } else {
  98. /* data+data failure. */
  99. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  100. tx = async_raid6_2data_recov(disks, bytes, faila, failb, ptrs, &submit);
  101. }
  102. }
  103. init_completion(&cmp);
  104. init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
  105. tx = async_syndrome_val(ptrs, 0, disks, bytes, &result, spare, &submit);
  106. async_tx_issue_pending(tx);
  107. if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
  108. pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
  109. __func__, faila, failb, disks);
  110. if (result != 0)
  111. pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
  112. __func__, faila, failb, result);
  113. }
  114. static int test_disks(int i, int j, int disks)
  115. {
  116. int erra, errb;
  117. memset(page_address(recovi), 0xf0, PAGE_SIZE);
  118. memset(page_address(recovj), 0xba, PAGE_SIZE);
  119. dataptrs[i] = recovi;
  120. dataptrs[j] = recovj;
  121. raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs);
  122. erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
  123. errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
  124. pr("%s(%d, %d): faila=%3d(%c) failb=%3d(%c) %s\n",
  125. __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
  126. (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
  127. dataptrs[i] = data[i];
  128. dataptrs[j] = data[j];
  129. return erra || errb;
  130. }
  131. static int test(int disks, int *tests)
  132. {
  133. struct dma_async_tx_descriptor *tx;
  134. struct async_submit_ctl submit;
  135. struct completion cmp;
  136. int err = 0;
  137. int i, j;
  138. recovi = data[disks];
  139. recovj = data[disks+1];
  140. spare = data[disks+2];
  141. makedata(disks);
  142. /* Nuke syndromes */
  143. memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
  144. memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
  145. /* Generate assumed good syndrome */
  146. init_completion(&cmp);
  147. init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
  148. tx = async_gen_syndrome(dataptrs, 0, disks, PAGE_SIZE, &submit);
  149. async_tx_issue_pending(tx);
  150. if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
  151. pr("error: initial gen_syndrome(%d) timed out\n", disks);
  152. return 1;
  153. }
  154. pr("testing the %d-disk case...\n", disks);
  155. for (i = 0; i < disks-1; i++)
  156. for (j = i+1; j < disks; j++) {
  157. (*tests)++;
  158. err += test_disks(i, j, disks);
  159. }
  160. return err;
  161. }
  162. static int raid6_test(void)
  163. {
  164. int err = 0;
  165. int tests = 0;
  166. int i;
  167. for (i = 0; i < NDISKS+3; i++) {
  168. data[i] = alloc_page(GFP_KERNEL);
  169. if (!data[i]) {
  170. while (i--)
  171. put_page(data[i]);
  172. return -ENOMEM;
  173. }
  174. }
  175. /* the 4-disk and 5-disk cases are special for the recovery code */
  176. if (NDISKS > 4)
  177. err += test(4, &tests);
  178. if (NDISKS > 5)
  179. err += test(5, &tests);
  180. /* the 11 and 12 disk cases are special for ioatdma (p-disabled
  181. * q-continuation without extended descriptor)
  182. */
  183. if (NDISKS > 12) {
  184. err += test(11, &tests);
  185. err += test(12, &tests);
  186. }
  187. /* the 24 disk case is special for ioatdma as it is the boudary point
  188. * at which it needs to switch from 8-source ops to 16-source
  189. * ops for continuation (assumes DMA_HAS_PQ_CONTINUE is not set)
  190. */
  191. if (NDISKS > 24)
  192. err += test(24, &tests);
  193. err += test(NDISKS, &tests);
  194. pr("\n");
  195. pr("complete (%d tests, %d failure%s)\n",
  196. tests, err, err == 1 ? "" : "s");
  197. for (i = 0; i < NDISKS+3; i++)
  198. put_page(data[i]);
  199. return 0;
  200. }
  201. static void raid6_test_exit(void)
  202. {
  203. }
  204. /* when compiled-in wait for drivers to load first (assumes dma drivers
  205. * are also compliled-in)
  206. */
  207. late_initcall(raid6_test);
  208. module_exit(raid6_test_exit);
  209. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  210. MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
  211. MODULE_LICENSE("GPL");