raid6test.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/random.h>
  25. #undef pr
  26. #define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
  27. #define NDISKS 16 /* Including P and Q */
  28. static struct page *dataptrs[NDISKS];
  29. static addr_conv_t addr_conv[NDISKS];
  30. static struct page *data[NDISKS+3];
  31. static struct page *spare;
  32. static struct page *recovi;
  33. static struct page *recovj;
  34. static void callback(void *param)
  35. {
  36. struct completion *cmp = param;
  37. complete(cmp);
  38. }
  39. static void makedata(int disks)
  40. {
  41. int i, j;
  42. for (i = 0; i < disks; i++) {
  43. for (j = 0; j < PAGE_SIZE/sizeof(u32); j += sizeof(u32)) {
  44. u32 *p = page_address(data[i]) + j;
  45. *p = random32();
  46. }
  47. dataptrs[i] = data[i];
  48. }
  49. }
  50. static char disk_type(int d, int disks)
  51. {
  52. if (d == disks - 2)
  53. return 'P';
  54. else if (d == disks - 1)
  55. return 'Q';
  56. else
  57. return 'D';
  58. }
  59. /* Recover two failed blocks. */
  60. static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
  61. {
  62. struct async_submit_ctl submit;
  63. struct completion cmp;
  64. struct dma_async_tx_descriptor *tx = NULL;
  65. enum sum_check_flags result = ~0;
  66. if (faila > failb)
  67. swap(faila, failb);
  68. if (failb == disks-1) {
  69. if (faila == disks-2) {
  70. /* P+Q failure. Just rebuild the syndrome. */
  71. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  72. tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
  73. } else {
  74. struct page *blocks[disks];
  75. struct page *dest;
  76. int count = 0;
  77. int i;
  78. /* data+Q failure. Reconstruct data from P,
  79. * then rebuild syndrome
  80. */
  81. for (i = disks; i-- ; ) {
  82. if (i == faila || i == failb)
  83. continue;
  84. blocks[count++] = ptrs[i];
  85. }
  86. dest = ptrs[faila];
  87. init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
  88. NULL, NULL, addr_conv);
  89. tx = async_xor(dest, blocks, 0, count, bytes, &submit);
  90. init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
  91. tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
  92. }
  93. } else {
  94. if (failb == disks-2) {
  95. /* data+P failure. */
  96. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  97. tx = async_raid6_datap_recov(disks, bytes, faila, ptrs, &submit);
  98. } else {
  99. /* data+data failure. */
  100. init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
  101. tx = async_raid6_2data_recov(disks, bytes, faila, failb, ptrs, &submit);
  102. }
  103. }
  104. init_completion(&cmp);
  105. init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
  106. tx = async_syndrome_val(ptrs, 0, disks, bytes, &result, spare, &submit);
  107. async_tx_issue_pending(tx);
  108. if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
  109. pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
  110. __func__, faila, failb, disks);
  111. if (result != 0)
  112. pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
  113. __func__, faila, failb, result);
  114. }
  115. static int test_disks(int i, int j, int disks)
  116. {
  117. int erra, errb;
  118. memset(page_address(recovi), 0xf0, PAGE_SIZE);
  119. memset(page_address(recovj), 0xba, PAGE_SIZE);
  120. dataptrs[i] = recovi;
  121. dataptrs[j] = recovj;
  122. raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs);
  123. erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
  124. errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
  125. pr("%s(%d, %d): faila=%3d(%c) failb=%3d(%c) %s\n",
  126. __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
  127. (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
  128. dataptrs[i] = data[i];
  129. dataptrs[j] = data[j];
  130. return erra || errb;
  131. }
  132. static int test(int disks, int *tests)
  133. {
  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. /* the 11 and 12 disk cases are special for ioatdma (p-disabled
  182. * q-continuation without extended descriptor)
  183. */
  184. if (NDISKS > 12) {
  185. err += test(11, &tests);
  186. err += test(12, &tests);
  187. }
  188. err += test(NDISKS, &tests);
  189. pr("\n");
  190. pr("complete (%d tests, %d failure%s)\n",
  191. tests, err, err == 1 ? "" : "s");
  192. for (i = 0; i < NDISKS+3; i++)
  193. put_page(data[i]);
  194. return 0;
  195. }
  196. static void raid6_test_exit(void)
  197. {
  198. }
  199. /* when compiled-in wait for drivers to load first (assumes dma drivers
  200. * are also compliled-in)
  201. */
  202. late_initcall(raid6_test);
  203. module_exit(raid6_test_exit);
  204. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  205. MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
  206. MODULE_LICENSE("GPL");