mdraid1x_linux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* mdraid_linux.c - module to handle Linux Software RAID. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009,2010 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. GRUB_MOD_LICENSE ("GPLv3+");
  26. /* Linux RAID on disk structures and constants,
  27. copied from include/linux/raid/md_p.h. */
  28. #define SB_MAGIC 0xa92b4efc
  29. /*
  30. * The version-1 superblock :
  31. * All numeric fields are little-endian.
  32. *
  33. * Total size: 256 bytes plus 2 per device.
  34. * 1K allows 384 devices.
  35. */
  36. struct grub_raid_super_1x
  37. {
  38. /* Constant array information - 128 bytes. */
  39. grub_uint32_t magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian. */
  40. grub_uint32_t major_version; /* 1. */
  41. grub_uint32_t feature_map; /* Bit 0 set if 'bitmap_offset' is meaningful. */
  42. grub_uint32_t pad0; /* Always set to 0 when writing. */
  43. grub_uint8_t set_uuid[16]; /* User-space generated. */
  44. char set_name[32]; /* Set and interpreted by user-space. */
  45. grub_uint64_t ctime; /* Lo 40 bits are seconds, top 24 are microseconds or 0. */
  46. grub_uint32_t level; /* -4 (multipath), -1 (linear), 0,1,4,5. */
  47. grub_uint32_t layout; /* only for raid5 and raid10 currently. */
  48. grub_uint64_t size; /* Used size of component devices, in 512byte sectors. */
  49. grub_uint32_t chunksize; /* In 512byte sectors. */
  50. grub_uint32_t raid_disks;
  51. grub_uint32_t bitmap_offset; /* Sectors after start of superblock that bitmap starts
  52. * NOTE: signed, so bitmap can be before superblock
  53. * only meaningful of feature_map[0] is set.
  54. */
  55. /* These are only valid with feature bit '4'. */
  56. grub_uint32_t new_level; /* New level we are reshaping to. */
  57. grub_uint64_t reshape_position; /* Next address in array-space for reshape. */
  58. grub_uint32_t delta_disks; /* Change in number of raid_disks. */
  59. grub_uint32_t new_layout; /* New layout. */
  60. grub_uint32_t new_chunk; /* New chunk size (512byte sectors). */
  61. grub_uint8_t pad1[128 - 124]; /* Set to 0 when written. */
  62. /* Constant this-device information - 64 bytes. */
  63. grub_uint64_t data_offset; /* Sector start of data, often 0. */
  64. grub_uint64_t data_size; /* Sectors in this device that can be used for data. */
  65. grub_uint64_t super_offset; /* Sector start of this superblock. */
  66. grub_uint64_t recovery_offset; /* Sectors before this offset (from data_offset) have been recovered. */
  67. grub_uint32_t dev_number; /* Permanent identifier of this device - not role in raid. */
  68. grub_uint32_t cnt_corrected_read; /* Number of read errors that were corrected by re-writing. */
  69. grub_uint8_t device_uuid[16]; /* User-space setable, ignored by kernel. */
  70. grub_uint8_t devflags; /* Per-device flags. Only one defined... */
  71. grub_uint8_t pad2[64 - 57]; /* Set to 0 when writing. */
  72. /* Array state information - 64 bytes. */
  73. grub_uint64_t utime; /* 40 bits second, 24 btes microseconds. */
  74. grub_uint64_t events; /* Incremented when superblock updated. */
  75. grub_uint64_t resync_offset; /* Data before this offset (from data_offset) known to be in sync. */
  76. grub_uint32_t sb_csum; /* Checksum upto devs[max_dev]. */
  77. grub_uint32_t max_dev; /* Size of devs[] array to consider. */
  78. grub_uint8_t pad3[64 - 32]; /* Set to 0 when writing. */
  79. /* Device state information. Indexed by dev_number.
  80. * 2 bytes per device.
  81. * Note there are no per-device state flags. State information is rolled
  82. * into the 'roles' value. If a device is spare or faulty, then it doesn't
  83. * have a meaningful role.
  84. */
  85. grub_uint16_t dev_roles[0]; /* Role in array, or 0xffff for a spare, or 0xfffe for faulty. */
  86. };
  87. /* Could be GRUB_PACKED, but since all members in this struct
  88. are already appropriately aligned, we can omit this and avoid suboptimal
  89. assembly in some cases. */
  90. #define WriteMostly1 1 /* Mask for writemostly flag in above devflags. */
  91. static struct grub_diskfilter_vg *
  92. grub_mdraid_detect (grub_disk_t disk,
  93. struct grub_diskfilter_pv_id *id,
  94. grub_disk_addr_t *start_sector)
  95. {
  96. grub_uint64_t size;
  97. grub_uint8_t minor_version;
  98. size = grub_disk_native_sectors (disk);
  99. /* Check for an 1.x superblock.
  100. * It's always aligned to a 4K boundary
  101. * and depending on the minor version it can be:
  102. * 0: At least 8K, but less than 12K, from end of device
  103. * 1: At start of device
  104. * 2: 4K from start of device.
  105. */
  106. for (minor_version = 0; minor_version < 3; ++minor_version)
  107. {
  108. grub_disk_addr_t sector = 0;
  109. struct grub_raid_super_1x sb;
  110. grub_uint16_t role;
  111. grub_uint32_t level;
  112. struct grub_diskfilter_vg *array;
  113. char *uuid;
  114. if (size == GRUB_DISK_SIZE_UNKNOWN && minor_version == 0)
  115. continue;
  116. switch (minor_version)
  117. {
  118. case 0:
  119. sector = (size - 8 * 2) & ~(4 * 2 - 1);
  120. break;
  121. case 1:
  122. sector = 0;
  123. break;
  124. case 2:
  125. sector = 4 * 2;
  126. break;
  127. }
  128. if (grub_disk_read (disk, sector, 0, sizeof (struct grub_raid_super_1x),
  129. &sb))
  130. return NULL;
  131. if (sb.magic != grub_cpu_to_le32_compile_time (SB_MAGIC)
  132. || grub_le_to_cpu64 (sb.super_offset) != sector)
  133. continue;
  134. if (sb.major_version != grub_cpu_to_le32_compile_time (1))
  135. /* Unsupported version. */
  136. return NULL;
  137. level = grub_le_to_cpu32 (sb.level);
  138. /* Multipath. */
  139. if ((int) level == -4)
  140. level = 1;
  141. if (level != 0 && level != 1 && level != 4 &&
  142. level != 5 && level != 6 && level != 10)
  143. {
  144. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  145. "Unsupported RAID level: %d", sb.level);
  146. return NULL;
  147. }
  148. if (grub_le_to_cpu32 (sb.dev_number) >=
  149. grub_le_to_cpu32 (sb.max_dev))
  150. /* Spares aren't implemented. */
  151. return NULL;
  152. if (grub_disk_read (disk, sector,
  153. (char *) (sb.dev_roles + grub_le_to_cpu32 (sb.dev_number))
  154. - (char *) &sb,
  155. sizeof (role), &role))
  156. return NULL;
  157. if (grub_le_to_cpu16 (role)
  158. >= grub_le_to_cpu32 (sb.raid_disks))
  159. /* Spares aren't implemented. */
  160. return NULL;
  161. id->uuidlen = 0;
  162. id->id = grub_le_to_cpu16 (role);
  163. uuid = grub_malloc (16);
  164. if (!uuid)
  165. return NULL;
  166. grub_memcpy (uuid, sb.set_uuid, 16);
  167. *start_sector = grub_le_to_cpu64 (sb.data_offset);
  168. array = grub_diskfilter_make_raid (16, uuid,
  169. grub_le_to_cpu32 (sb.raid_disks),
  170. sb.set_name,
  171. (sb.size)
  172. ? grub_le_to_cpu64 (sb.size)
  173. : grub_le_to_cpu64 (sb.data_size),
  174. grub_le_to_cpu32 (sb.chunksize),
  175. grub_le_to_cpu32 (sb.layout),
  176. grub_le_to_cpu32 (sb.level));
  177. return array;
  178. }
  179. /* not 1.x raid. */
  180. return NULL;
  181. }
  182. static struct grub_diskfilter grub_mdraid_dev = {
  183. .name = "mdraid1x",
  184. .detect = grub_mdraid_detect,
  185. .next = 0
  186. };
  187. GRUB_MOD_INIT (mdraid1x)
  188. {
  189. grub_diskfilter_register_front (&grub_mdraid_dev);
  190. }
  191. GRUB_MOD_FINI (mdraid1x)
  192. {
  193. grub_diskfilter_unregister (&grub_mdraid_dev);
  194. }