raid6test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/random.h>
  24. #undef pr
  25. #define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
  26. #define NDISKS 16 /* Including P and Q */
  27. static struct page *dataptrs[NDISKS];
  28. static struct page *data[NDISKS+3];
  29. static struct page *spare;
  30. static struct page *recovi;
  31. static struct page *recovj;
  32. static void callback(void *param)
  33. {
  34. struct completion *cmp = param;
  35. complete(cmp);
  36. }
  37. static void makedata(int disks)
  38. {
  39. int i, j;
  40. for (i = 0; i < disks; i++) {
  41. for (j = 0; j < PAGE_SIZE/sizeof(u32); j += sizeof(u32)) {
  42. u32 *p = page_address(data[i]) + j;
  43. *p = random32();
  44. }
  45. dataptrs[i] = data[i];
  46. }
  47. }
  48. static char disk_type(int d, int disks)
  49. {
  50. if (d == disks - 2)
  51. return 'P';
  52. else if (d == disks - 1)
  53. return 'Q';
  54. else
  55. return 'D';
  56. }
  57. /* Recover two failed blocks. */
  58. static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
  59. {
  60. struct async_submit_ctl submit;
  61. addr_conv_t addr_conv[disks];
  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. addr_conv_t addr_conv[disks];
  134. struct dma_async_tx_descriptor *tx;
  135. struct async_submit_ctl submit;
  136. struct completion cmp;
  137. int err = 0;
  138. int i, j;
  139. recovi = data[disks];
  140. recovj = data[disks+1];
  141. spare = data[disks+2];
  142. makedata(disks);
  143. /* Nuke syndromes */
  144. memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
  145. memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
  146. /* Generate assumed good syndrome */
  147. init_completion(&cmp);
  148. init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
  149. tx = async_gen_syndrome(dataptrs, 0, disks, PAGE_SIZE, &submit);
  150. async_tx_issue_pending(tx);
  151. if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
  152. pr("error: initial gen_syndrome(%d) timed out\n", disks);
  153. return 1;
  154. }
  155. pr("testing the %d-disk case...\n", disks);
  156. for (i = 0; i < disks-1; i++)
  157. for (j = i+1; j < disks; j++) {
  158. (*tests)++;
  159. err += test_disks(i, j, disks);
  160. }
  161. return err;
  162. }
  163. static int raid6_test(void)
  164. {
  165. int err = 0;
  166. int tests = 0;
  167. int i;
  168. for (i = 0; i < NDISKS+3; i++) {
  169. data[i] = alloc_page(GFP_KERNEL);
  170. if (!data[i]) {
  171. while (i--)
  172. put_page(data[i]);
  173. return -ENOMEM;
  174. }
  175. }
  176. /* the 4-disk and 5-disk cases are special for the recovery code */
  177. if (NDISKS > 4)
  178. err += test(4, &tests);
  179. if (NDISKS > 5)
  180. err += test(5, &tests);
  181. err += test(NDISKS, &tests);
  182. pr("\n");
  183. pr("complete (%d tests, %d failure%s)\n",
  184. tests, err, err == 1 ? "" : "s");
  185. for (i = 0; i < NDISKS+3; i++)
  186. put_page(data[i]);
  187. return 0;
  188. }
  189. static void raid6_test_exit(void)
  190. {
  191. }
  192. /* when compiled-in wait for drivers to load first (assumes dma drivers
  193. * are also compliled-in)
  194. */
  195. late_initcall(raid6_test);
  196. module_exit(raid6_test_exit);
  197. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  198. MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
  199. MODULE_LICENSE("GPL");