mdraid1x_linux.c 9.6 KB

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