raid5_recover.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* raid5_recover.c - module to recover from faulty RAID4/5 arrays. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008 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. static grub_err_t
  28. grub_raid5_recover (struct grub_diskfilter_segment *array, int disknr,
  29. char *buf, grub_disk_addr_t sector, grub_size_t size)
  30. {
  31. char *buf2;
  32. int i;
  33. size <<= GRUB_DISK_SECTOR_BITS;
  34. buf2 = grub_malloc (size);
  35. if (!buf2)
  36. return grub_errno;
  37. grub_memset (buf, 0, size);
  38. for (i = 0; i < (int) array->node_count; i++)
  39. {
  40. grub_err_t err;
  41. if (i == disknr)
  42. continue;
  43. err = grub_diskfilter_read_node (&array->nodes[i], sector,
  44. size >> GRUB_DISK_SECTOR_BITS, buf2);
  45. if (err)
  46. {
  47. grub_free (buf2);
  48. return err;
  49. }
  50. grub_crypto_xor (buf, buf, buf2, size);
  51. }
  52. grub_free (buf2);
  53. return GRUB_ERR_NONE;
  54. }
  55. GRUB_MOD_INIT(raid5rec)
  56. {
  57. grub_raid5_recover_func = grub_raid5_recover;
  58. }
  59. GRUB_MOD_FINI(raid5rec)
  60. {
  61. grub_raid5_recover_func = 0;
  62. }