mdraid_linux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* mdraid_linux.c - module to handle linux softraid. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/raid.h>
  25. /* Linux RAID on disk structures and constants,
  26. copied from include/linux/raid/md_p.h. */
  27. #define RESERVED_BYTES (64 * 1024)
  28. #define RESERVED_SECTORS (RESERVED_BYTES / 512)
  29. #define NEW_SIZE_SECTORS(x) ((x & ~(RESERVED_SECTORS - 1)) \
  30. - RESERVED_SECTORS)
  31. #define SB_BYTES 4096
  32. #define SB_WORDS (SB_BYTES / 4)
  33. #define SB_SECTORS (SB_BYTES / 512)
  34. /*
  35. * The following are counted in 32-bit words
  36. */
  37. #define SB_GENERIC_OFFSET 0
  38. #define SB_PERSONALITY_OFFSET 64
  39. #define SB_DISKS_OFFSET 128
  40. #define SB_DESCRIPTOR_OFFSET 992
  41. #define SB_GENERIC_CONSTANT_WORDS 32
  42. #define SB_GENERIC_STATE_WORDS 32
  43. #define SB_GENERIC_WORDS (SB_GENERIC_CONSTANT_WORDS + \
  44. SB_GENERIC_STATE_WORDS)
  45. #define SB_PERSONALITY_WORDS 64
  46. #define SB_DESCRIPTOR_WORDS 32
  47. #define SB_DISKS 27
  48. #define SB_DISKS_WORDS (SB_DISKS * SB_DESCRIPTOR_WORDS)
  49. #define SB_RESERVED_WORDS (1024 \
  50. - SB_GENERIC_WORDS \
  51. - SB_PERSONALITY_WORDS \
  52. - SB_DISKS_WORDS \
  53. - SB_DESCRIPTOR_WORDS)
  54. #define SB_EQUAL_WORDS (SB_GENERIC_WORDS \
  55. + SB_PERSONALITY_WORDS \
  56. + SB_DISKS_WORDS)
  57. /*
  58. * Device "operational" state bits
  59. */
  60. #define DISK_FAULTY 0
  61. #define DISK_ACTIVE 1
  62. #define DISK_SYNC 2
  63. #define DISK_REMOVED 3
  64. #define DISK_WRITEMOSTLY 9
  65. #define SB_MAGIC 0xa92b4efc
  66. /*
  67. * Superblock state bits
  68. */
  69. #define SB_CLEAN 0
  70. #define SB_ERRORS 1
  71. #define SB_BITMAP_PRESENT 8
  72. struct grub_raid_disk_09
  73. {
  74. grub_uint32_t number; /* Device number in the entire set. */
  75. grub_uint32_t major; /* Device major number. */
  76. grub_uint32_t minor; /* Device minor number. */
  77. grub_uint32_t raid_disk; /* The role of the device in the raid set. */
  78. grub_uint32_t state; /* Operational state. */
  79. grub_uint32_t reserved[SB_DESCRIPTOR_WORDS - 5];
  80. };
  81. struct grub_raid_super_09
  82. {
  83. /*
  84. * Constant generic information
  85. */
  86. grub_uint32_t md_magic; /* MD identifier. */
  87. grub_uint32_t major_version; /* Major version. */
  88. grub_uint32_t minor_version; /* Minor version. */
  89. grub_uint32_t patch_version; /* Patchlevel version. */
  90. grub_uint32_t gvalid_words; /* Number of used words in this section. */
  91. grub_uint32_t set_uuid0; /* Raid set identifier. */
  92. grub_uint32_t ctime; /* Creation time. */
  93. grub_uint32_t level; /* Raid personality. */
  94. grub_uint32_t size; /* Apparent size of each individual disk. */
  95. grub_uint32_t nr_disks; /* Total disks in the raid set. */
  96. grub_uint32_t raid_disks; /* Disks in a fully functional raid set. */
  97. grub_uint32_t md_minor; /* Preferred MD minor device number. */
  98. grub_uint32_t not_persistent; /* Does it have a persistent superblock. */
  99. grub_uint32_t set_uuid1; /* Raid set identifier #2. */
  100. grub_uint32_t set_uuid2; /* Raid set identifier #3. */
  101. grub_uint32_t set_uuid3; /* Raid set identifier #4. */
  102. grub_uint32_t gstate_creserved[SB_GENERIC_CONSTANT_WORDS - 16];
  103. /*
  104. * Generic state information
  105. */
  106. grub_uint32_t utime; /* Superblock update time. */
  107. grub_uint32_t state; /* State bits (clean, ...). */
  108. grub_uint32_t active_disks; /* Number of currently active disks. */
  109. grub_uint32_t working_disks; /* Number of working disks. */
  110. grub_uint32_t failed_disks; /* Number of failed disks. */
  111. grub_uint32_t spare_disks; /* Number of spare disks. */
  112. grub_uint32_t sb_csum; /* Checksum of the whole superblock. */
  113. grub_uint64_t events; /* Superblock update count. */
  114. grub_uint64_t cp_events; /* Checkpoint update count. */
  115. grub_uint32_t recovery_cp; /* Recovery checkpoint sector count. */
  116. grub_uint32_t gstate_sreserved[SB_GENERIC_STATE_WORDS - 12];
  117. /*
  118. * Personality information
  119. */
  120. grub_uint32_t layout; /* The array's physical layout. */
  121. grub_uint32_t chunk_size; /* Chunk size in bytes. */
  122. grub_uint32_t root_pv; /* LV root PV. */
  123. grub_uint32_t root_block; /* LV root block. */
  124. grub_uint32_t pstate_reserved[SB_PERSONALITY_WORDS - 4];
  125. /*
  126. * Disks information
  127. */
  128. struct grub_raid_disk_09 disks[SB_DISKS];
  129. /*
  130. * Reserved
  131. */
  132. grub_uint32_t reserved[SB_RESERVED_WORDS];
  133. /*
  134. * Active descriptor
  135. */
  136. struct grub_raid_disk_09 this_disk;
  137. } __attribute__ ((packed));
  138. struct grub_raid_super_1
  139. {
  140. grub_uint32_t md_magic; /* MD identifier. */
  141. grub_uint32_t major_version; /* Major version. */
  142. grub_uint32_t feature_map;
  143. grub_uint32_t pad0;
  144. grub_uint8_t set_uuid[16];
  145. char set_name[32];
  146. grub_uint64_t ctime; /* Creation time. low 40 bits are seconds, */
  147. /* top 24 are microseconds or 0. */
  148. grub_uint32_t level; /* Raid personality. */
  149. grub_uint32_t layout; /* Only for raid5 currently. */
  150. grub_uint64_t size; /* Apparent size of each individual disk. */
  151. grub_uint32_t chunk_size; /* In 512 byte sectors. */
  152. grub_uint32_t raid_disks; /* Disks in a fully functional raid set. */
  153. grub_uint32_t bitmap_offset;
  154. grub_uint32_t new_level; /* New level we are reshaping to. */
  155. grub_uint64_t reshape_position;
  156. grub_uint32_t delta_disks;
  157. grub_uint32_t new_layout;
  158. grub_uint32_t new_chunk;
  159. grub_uint8_t pad1[128 - 124];
  160. grub_uint64_t data_offset;
  161. grub_uint64_t data_size;
  162. grub_uint64_t super_offset;
  163. grub_uint64_t recovery_offset;
  164. grub_uint32_t dev_number;
  165. grub_uint32_t cnt_corrected_read;
  166. grub_uint8_t device_uuid[16];
  167. grub_uint8_t devflags;
  168. grub_uint8_t pad2[64 - 57];
  169. grub_uint64_t utime;
  170. grub_uint64_t events;
  171. grub_uint64_t rsync_offset;
  172. grub_uint32_t sb_csum;
  173. grub_uint32_t max_dev;
  174. grub_uint8_t pad3[64 - 32];
  175. grub_uint16_t dev_roles[0];
  176. } __attribute__ ((packed));
  177. static int
  178. detect_super_1 (grub_disk_t disk, grub_disk_addr_t sector,
  179. struct grub_raid_super_1 *sb)
  180. {
  181. if (grub_disk_read (disk, sector, 0, sizeof (*sb), sb))
  182. return 0;
  183. if (grub_le_to_cpu32 (sb->md_magic) != SB_MAGIC)
  184. return 0;
  185. if (grub_le_to_cpu32 (sb->major_version) != 1)
  186. return 0;
  187. if (grub_le_to_cpu64 (sb->super_offset) != sector)
  188. return 0;
  189. return 1;
  190. }
  191. static int
  192. grub_mdraid_detect_1 (grub_disk_t disk, struct grub_raid_array *array)
  193. {
  194. struct grub_raid_super_1 sb;
  195. grub_disk_addr_t sector, best_offset;
  196. grub_uint64_t best_ctime;
  197. int i, best_ver;
  198. sector = grub_disk_get_size (disk);
  199. if (sector < 24)
  200. return 0;
  201. best_offset = 0;
  202. best_ctime = 0;
  203. best_ver = -1;
  204. for (i = 0; i <= 2; i++)
  205. {
  206. grub_disk_addr_t offset;
  207. if (i == 0)
  208. offset = (sector - 16) & ~7ULL;
  209. else if (i == 1)
  210. offset = 0;
  211. else
  212. offset = 8;
  213. if (detect_super_1 (disk, offset, &sb))
  214. {
  215. if ((best_ver < 0) ||
  216. (best_ctime < grub_le_to_cpu64 (sb.ctime)))
  217. {
  218. best_ver = i;
  219. best_ctime = grub_le_to_cpu64 (sb.ctime);
  220. best_offset = offset;
  221. }
  222. }
  223. if (grub_errno)
  224. return 0;
  225. }
  226. if (best_ver < 0)
  227. return 0;
  228. if (grub_disk_read (disk, best_offset, 0, sizeof (sb), &sb))
  229. return 0;
  230. array->number = 0;
  231. array->level = grub_le_to_cpu32 (sb.level);
  232. array->layout = grub_le_to_cpu32 (sb.layout);
  233. array->total_devs = grub_le_to_cpu32 (sb.raid_disks);
  234. array->disk_size = grub_le_to_cpu64 (sb.data_size);
  235. array->disk_offset = grub_le_to_cpu64 (sb.data_offset);
  236. array->chunk_size = grub_le_to_cpu32 (sb.chunk_size);
  237. array->index = grub_le_to_cpu32 (sb.dev_number);
  238. array->uuid_len = sizeof (sb.set_uuid);
  239. array->uuid = grub_malloc (sizeof (sb.set_uuid));
  240. if (!array->uuid)
  241. return 0;
  242. grub_memcpy (array->uuid, &sb.set_uuid, sizeof (sb.set_uuid));
  243. return 1;
  244. }
  245. static grub_err_t
  246. grub_mdraid_detect_09 (grub_disk_t disk, struct grub_raid_array *array)
  247. {
  248. grub_disk_addr_t sector;
  249. grub_uint64_t size;
  250. struct grub_raid_super_09 sb;
  251. grub_uint32_t *uuid;
  252. /* The sector where the RAID superblock is stored, if available. */
  253. size = grub_disk_get_size (disk);
  254. sector = NEW_SIZE_SECTORS (size);
  255. if (grub_disk_read (disk, sector, 0, SB_BYTES, &sb))
  256. return grub_errno;
  257. /* Look whether there is a RAID superblock. */
  258. if (sb.md_magic != SB_MAGIC)
  259. return grub_error (GRUB_ERR_OUT_OF_RANGE, "not raid");
  260. /* FIXME: Also support version 1.0. */
  261. if (sb.major_version != 0 || sb.minor_version != 90)
  262. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  263. "unsupported RAID version: %d.%d",
  264. sb.major_version, sb.minor_version);
  265. /* FIXME: Check the checksum. */
  266. /* Multipath. */
  267. if ((int) sb.level == -4)
  268. sb.level = 1;
  269. if (sb.level != 0 && sb.level != 1 && sb.level != 4 &&
  270. sb.level != 5 && sb.level != 6 && sb.level != 10)
  271. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  272. "unsupported RAID level: %d", sb.level);
  273. array->number = sb.md_minor;
  274. array->level = sb.level;
  275. array->layout = sb.layout;
  276. array->total_devs = sb.raid_disks;
  277. array->disk_size = (sb.size) ? sb.size * 2 : sector;
  278. array->disk_offset = 0;
  279. array->chunk_size = sb.chunk_size >> 9;
  280. array->index = sb.this_disk.number;
  281. array->uuid_len = 16;
  282. array->uuid = grub_malloc (16);
  283. if (!array->uuid)
  284. return grub_errno;
  285. uuid = (grub_uint32_t *) array->uuid;
  286. uuid[0] = sb.set_uuid0;
  287. uuid[1] = sb.set_uuid1;
  288. uuid[2] = sb.set_uuid2;
  289. uuid[3] = sb.set_uuid3;
  290. return 0;
  291. }
  292. static grub_err_t
  293. grub_mdraid_detect (grub_disk_t disk, struct grub_raid_array *array)
  294. {
  295. if (grub_mdraid_detect_1 (disk, array))
  296. return 0;
  297. if (grub_errno)
  298. return grub_errno;
  299. return grub_mdraid_detect_09 (disk, array);
  300. }
  301. static struct grub_raid grub_mdraid_dev = {
  302. .name = "mdraid",
  303. .detect = grub_mdraid_detect,
  304. .next = 0
  305. };
  306. GRUB_MOD_INIT (mdraid)
  307. {
  308. grub_raid_register (&grub_mdraid_dev);
  309. }
  310. GRUB_MOD_FINI (mdraid)
  311. {
  312. grub_raid_unregister (&grub_mdraid_dev);
  313. }