raid6_recover.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* raid6_recover.c - module to recover from faulty RAID6 arrays. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/disk.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/misc.h>
  24. #include <grub/diskfilter.h>
  25. #include <grub/crypto.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. /* x**y. */
  28. static grub_uint8_t powx[255 * 2];
  29. /* Such an s that x**s = y */
  30. static unsigned powx_inv[256];
  31. static const grub_uint8_t poly = 0x1d;
  32. static void
  33. grub_raid_block_mulx (unsigned mul, char *buf, grub_size_t size)
  34. {
  35. grub_size_t i;
  36. grub_uint8_t *p;
  37. p = (grub_uint8_t *) buf;
  38. for (i = 0; i < size; i++, p++)
  39. if (*p)
  40. *p = powx[mul + powx_inv[*p]];
  41. }
  42. static void
  43. grub_raid6_init_table (void)
  44. {
  45. unsigned i;
  46. grub_uint8_t cur = 1;
  47. for (i = 0; i < 255; i++)
  48. {
  49. powx[i] = cur;
  50. powx[i + 255] = cur;
  51. powx_inv[cur] = i;
  52. if (cur & 0x80)
  53. cur = (cur << 1) ^ poly;
  54. else
  55. cur <<= 1;
  56. }
  57. }
  58. static unsigned
  59. mod_255 (unsigned x)
  60. {
  61. while (x > 0xff)
  62. x = (x >> 8) + (x & 0xff);
  63. if (x == 0xff)
  64. return 0;
  65. return x;
  66. }
  67. static grub_err_t
  68. raid6_recover_read_node (void *data, int disknr,
  69. grub_uint64_t sector,
  70. void *buf, grub_size_t size)
  71. {
  72. struct grub_diskfilter_segment *array = data;
  73. return grub_diskfilter_read_node (&array->nodes[disknr],
  74. (grub_disk_addr_t)sector,
  75. size >> GRUB_DISK_SECTOR_BITS, buf);
  76. }
  77. grub_err_t
  78. grub_raid6_recover_gen (void *data, grub_uint64_t nstripes, int disknr, int p,
  79. char *buf, grub_uint64_t sector, grub_size_t size,
  80. int layout, raid_recover_read_t read_func)
  81. {
  82. int i, q, pos;
  83. int bad1 = -1, bad2 = -1;
  84. char *pbuf = 0, *qbuf = 0;
  85. pbuf = grub_zalloc (size);
  86. if (!pbuf)
  87. goto quit;
  88. qbuf = grub_zalloc (size);
  89. if (!qbuf)
  90. goto quit;
  91. q = p + 1;
  92. if (q == (int) nstripes)
  93. q = 0;
  94. pos = q + 1;
  95. if (pos == (int) nstripes)
  96. pos = 0;
  97. for (i = 0; i < (int) nstripes - 2; i++)
  98. {
  99. int c;
  100. if (layout & GRUB_RAID_LAYOUT_MUL_FROM_POS)
  101. c = pos;
  102. else
  103. c = i;
  104. if (pos == disknr)
  105. bad1 = c;
  106. else
  107. {
  108. if (!read_func (data, pos, sector, buf, size))
  109. {
  110. grub_crypto_xor (pbuf, pbuf, buf, size);
  111. grub_raid_block_mulx (c, buf, size);
  112. grub_crypto_xor (qbuf, qbuf, buf, size);
  113. }
  114. else
  115. {
  116. /* Too many bad devices */
  117. if (bad2 >= 0)
  118. goto quit;
  119. bad2 = c;
  120. grub_errno = GRUB_ERR_NONE;
  121. }
  122. }
  123. pos++;
  124. if (pos == (int) nstripes)
  125. pos = 0;
  126. }
  127. /* Invalid disknr or p */
  128. if (bad1 < 0)
  129. goto quit;
  130. if (bad2 < 0)
  131. {
  132. /* One bad device */
  133. if (!read_func (data, p, sector, buf, size))
  134. {
  135. grub_crypto_xor (buf, buf, pbuf, size);
  136. goto quit;
  137. }
  138. grub_errno = GRUB_ERR_NONE;
  139. if (read_func (data, q, sector, buf, size))
  140. goto quit;
  141. grub_crypto_xor (buf, buf, qbuf, size);
  142. grub_raid_block_mulx (255 - bad1, buf,
  143. size);
  144. }
  145. else
  146. {
  147. /* Two bad devices */
  148. unsigned c;
  149. if (read_func (data, p, sector, buf, size))
  150. goto quit;
  151. grub_crypto_xor (pbuf, pbuf, buf, size);
  152. if (read_func (data, q, sector, buf, size))
  153. goto quit;
  154. grub_crypto_xor (qbuf, qbuf, buf, size);
  155. c = mod_255((255 ^ bad1)
  156. + (255 ^ powx_inv[(powx[bad2 + (bad1 ^ 255)] ^ 1)]));
  157. grub_raid_block_mulx (c, qbuf, size);
  158. c = mod_255((unsigned) bad2 + c);
  159. grub_raid_block_mulx (c, pbuf, size);
  160. grub_crypto_xor (pbuf, pbuf, qbuf, size);
  161. grub_memcpy (buf, pbuf, size);
  162. }
  163. quit:
  164. grub_free (pbuf);
  165. grub_free (qbuf);
  166. return grub_errno;
  167. }
  168. static grub_err_t
  169. grub_raid6_recover (struct grub_diskfilter_segment *array, int disknr, int p,
  170. char *buf, grub_disk_addr_t sector, grub_size_t size)
  171. {
  172. return grub_raid6_recover_gen (array, array->node_count, disknr, p, buf,
  173. sector, size << GRUB_DISK_SECTOR_BITS,
  174. array->layout, raid6_recover_read_node);
  175. }
  176. GRUB_MOD_INIT(raid6rec)
  177. {
  178. grub_raid6_init_table ();
  179. grub_raid6_recover_func = grub_raid6_recover;
  180. }
  181. GRUB_MOD_FINI(raid6rec)
  182. {
  183. grub_raid6_recover_func = 0;
  184. }