mdraid_linux.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /* Linux RAID on disk structures and constants,
  26. copied from include/linux/raid/md_p.h. */
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. #ifdef MODE_BIGENDIAN
  29. #define grub_md_to_cpu64 grub_be_to_cpu64
  30. #define grub_md_to_cpu32 grub_be_to_cpu32
  31. #define grub_md_to_cpu16 grub_be_to_cpu16
  32. #define grub_cpu_to_md64_compile_time grub_cpu_to_be64_compile_time
  33. #define grub_cpu_to_md32_compile_time grub_cpu_to_be32_compile_time
  34. #define grub_cpu_to_md16_compile_time grub_cpu_to_be16_compile_time
  35. #else
  36. #define grub_md_to_cpu64 grub_le_to_cpu64
  37. #define grub_md_to_cpu32 grub_le_to_cpu32
  38. #define grub_md_to_cpu16 grub_le_to_cpu16
  39. #define grub_cpu_to_md64_compile_time grub_cpu_to_le64_compile_time
  40. #define grub_cpu_to_md32_compile_time grub_cpu_to_le32_compile_time
  41. #define grub_cpu_to_md16_compile_time grub_cpu_to_le16_compile_time
  42. #endif
  43. #define RESERVED_BYTES (64 * 1024)
  44. #define RESERVED_SECTORS (RESERVED_BYTES / 512)
  45. #define NEW_SIZE_SECTORS(x) ((x & ~(RESERVED_SECTORS - 1)) \
  46. - RESERVED_SECTORS)
  47. #define SB_BYTES 4096
  48. #define SB_WORDS (SB_BYTES / 4)
  49. #define SB_SECTORS (SB_BYTES / 512)
  50. /*
  51. * The following are counted in 32-bit words
  52. */
  53. #define SB_GENERIC_OFFSET 0
  54. #define SB_PERSONALITY_OFFSET 64
  55. #define SB_DISKS_OFFSET 128
  56. #define SB_DESCRIPTOR_OFFSET 992
  57. #define SB_GENERIC_CONSTANT_WORDS 32
  58. #define SB_GENERIC_STATE_WORDS 32
  59. #define SB_GENERIC_WORDS (SB_GENERIC_CONSTANT_WORDS + \
  60. SB_GENERIC_STATE_WORDS)
  61. #define SB_PERSONALITY_WORDS 64
  62. #define SB_DESCRIPTOR_WORDS 32
  63. #define SB_DISKS 27
  64. #define SB_DISKS_WORDS (SB_DISKS * SB_DESCRIPTOR_WORDS)
  65. #define SB_RESERVED_WORDS (1024 \
  66. - SB_GENERIC_WORDS \
  67. - SB_PERSONALITY_WORDS \
  68. - SB_DISKS_WORDS \
  69. - SB_DESCRIPTOR_WORDS)
  70. #define SB_EQUAL_WORDS (SB_GENERIC_WORDS \
  71. + SB_PERSONALITY_WORDS \
  72. + SB_DISKS_WORDS)
  73. /*
  74. * Device "operational" state bits
  75. */
  76. #define DISK_FAULTY 0
  77. #define DISK_ACTIVE 1
  78. #define DISK_SYNC 2
  79. #define DISK_REMOVED 3
  80. #define DISK_WRITEMOSTLY 9
  81. #define SB_MAGIC 0xa92b4efc
  82. /*
  83. * Superblock state bits
  84. */
  85. #define SB_CLEAN 0
  86. #define SB_ERRORS 1
  87. #define SB_BITMAP_PRESENT 8
  88. struct grub_raid_disk_09
  89. {
  90. grub_uint32_t number; /* Device number in the entire set. */
  91. grub_uint32_t major; /* Device major number. */
  92. grub_uint32_t minor; /* Device minor number. */
  93. grub_uint32_t raid_disk; /* The role of the device in the raid set. */
  94. grub_uint32_t state; /* Operational state. */
  95. grub_uint32_t reserved[SB_DESCRIPTOR_WORDS - 5];
  96. };
  97. struct grub_raid_super_09
  98. {
  99. /*
  100. * Constant generic information
  101. */
  102. grub_uint32_t md_magic; /* MD identifier. */
  103. grub_uint32_t major_version; /* Major version. */
  104. grub_uint32_t minor_version; /* Minor version. */
  105. grub_uint32_t patch_version; /* Patchlevel version. */
  106. grub_uint32_t gvalid_words; /* Number of used words in this section. */
  107. grub_uint32_t set_uuid0; /* Raid set identifier. */
  108. grub_uint32_t ctime; /* Creation time. */
  109. grub_uint32_t level; /* Raid personality. */
  110. grub_uint32_t size; /* Apparent size of each individual disk. */
  111. grub_uint32_t nr_disks; /* Total disks in the raid set. */
  112. grub_uint32_t raid_disks; /* Disks in a fully functional raid set. */
  113. grub_uint32_t md_minor; /* Preferred MD minor device number. */
  114. grub_uint32_t not_persistent; /* Does it have a persistent superblock. */
  115. grub_uint32_t set_uuid1; /* Raid set identifier #2. */
  116. grub_uint32_t set_uuid2; /* Raid set identifier #3. */
  117. grub_uint32_t set_uuid3; /* Raid set identifier #4. */
  118. grub_uint32_t gstate_creserved[SB_GENERIC_CONSTANT_WORDS - 16];
  119. /*
  120. * Generic state information
  121. */
  122. grub_uint32_t utime; /* Superblock update time. */
  123. grub_uint32_t state; /* State bits (clean, ...). */
  124. grub_uint32_t active_disks; /* Number of currently active disks. */
  125. grub_uint32_t working_disks; /* Number of working disks. */
  126. grub_uint32_t failed_disks; /* Number of failed disks. */
  127. grub_uint32_t spare_disks; /* Number of spare disks. */
  128. grub_uint32_t sb_csum; /* Checksum of the whole superblock. */
  129. grub_uint64_t events; /* Superblock update count. */
  130. grub_uint64_t cp_events; /* Checkpoint update count. */
  131. grub_uint32_t recovery_cp; /* Recovery checkpoint sector count. */
  132. grub_uint32_t gstate_sreserved[SB_GENERIC_STATE_WORDS - 12];
  133. /*
  134. * Personality information
  135. */
  136. grub_uint32_t layout; /* The array's physical layout. */
  137. grub_uint32_t chunk_size; /* Chunk size in bytes. */
  138. grub_uint32_t root_pv; /* LV root PV. */
  139. grub_uint32_t root_block; /* LV root block. */
  140. grub_uint32_t pstate_reserved[SB_PERSONALITY_WORDS - 4];
  141. /*
  142. * Disks information
  143. */
  144. struct grub_raid_disk_09 disks[SB_DISKS];
  145. /*
  146. * Reserved
  147. */
  148. grub_uint32_t reserved[SB_RESERVED_WORDS];
  149. /*
  150. * Active descriptor
  151. */
  152. struct grub_raid_disk_09 this_disk;
  153. } GRUB_PACKED;
  154. static struct grub_diskfilter_vg *
  155. grub_mdraid_detect (grub_disk_t disk,
  156. struct grub_diskfilter_pv_id *id,
  157. grub_disk_addr_t *start_sector)
  158. {
  159. grub_disk_addr_t sector;
  160. grub_uint64_t size;
  161. struct grub_raid_super_09 *sb = NULL;
  162. grub_uint32_t *uuid;
  163. grub_uint32_t level;
  164. struct grub_diskfilter_vg *ret;
  165. /* The sector where the mdraid 0.90 superblock is stored, if available. */
  166. size = grub_disk_native_sectors (disk);
  167. if (size == GRUB_DISK_SIZE_UNKNOWN)
  168. /* not 0.9x raid. */
  169. return NULL;
  170. sector = NEW_SIZE_SECTORS (size);
  171. sb = grub_malloc (sizeof (*sb));
  172. if (!sb)
  173. return NULL;
  174. if (grub_disk_read (disk, sector, 0, SB_BYTES, sb))
  175. goto fail;
  176. /* Look whether there is a mdraid 0.90 superblock. */
  177. if (sb->md_magic != grub_cpu_to_md32_compile_time (SB_MAGIC))
  178. /* not 0.9x raid. */
  179. goto fail;
  180. if (sb->major_version != grub_cpu_to_md32_compile_time (0)
  181. || sb->minor_version != grub_cpu_to_md32_compile_time (90))
  182. /* Unsupported version. */
  183. goto fail;
  184. /* No need for explicit check that sb->size is 0 (unspecified) since
  185. 0 >= non-0 is false. */
  186. if (((grub_disk_addr_t) grub_md_to_cpu32 (sb->size)) * 2 >= size)
  187. goto fail;
  188. /* FIXME: Check the checksum. */
  189. level = grub_md_to_cpu32 (sb->level);
  190. /* Multipath. */
  191. if ((int) level == -4)
  192. level = 1;
  193. if (level != 0 && level != 1 && level != 4 &&
  194. level != 5 && level != 6 && level != 10)
  195. {
  196. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  197. "unsupported RAID level: %d", level);
  198. goto fail;
  199. }
  200. if (grub_md_to_cpu32 (sb->this_disk.number) == 0xffff
  201. || grub_md_to_cpu32 (sb->this_disk.number) == 0xfffe)
  202. /* Spares aren't implemented. */
  203. goto fail;
  204. uuid = grub_malloc (16);
  205. if (!uuid)
  206. goto fail;
  207. uuid[0] = grub_swap_bytes32 (sb->set_uuid0);
  208. uuid[1] = grub_swap_bytes32 (sb->set_uuid1);
  209. uuid[2] = grub_swap_bytes32 (sb->set_uuid2);
  210. uuid[3] = grub_swap_bytes32 (sb->set_uuid3);
  211. *start_sector = 0;
  212. id->uuidlen = 0;
  213. id->id = grub_md_to_cpu32 (sb->this_disk.number);
  214. char buf[32];
  215. grub_snprintf (buf, sizeof (buf), "md%d", grub_md_to_cpu32 (sb->md_minor));
  216. ret = grub_diskfilter_make_raid (16, (char *) uuid,
  217. grub_md_to_cpu32 (sb->raid_disks), buf,
  218. (sb->size) ? ((grub_disk_addr_t)
  219. grub_md_to_cpu32 (sb->size)) * 2
  220. : sector,
  221. grub_md_to_cpu32 (sb->chunk_size) >> 9,
  222. grub_md_to_cpu32 (sb->layout),
  223. level);
  224. grub_free (sb);
  225. return ret;
  226. fail:
  227. grub_free (sb);
  228. return NULL;
  229. }
  230. static struct grub_diskfilter grub_mdraid_dev = {
  231. #ifdef MODE_BIGENDIAN
  232. .name = "mdraid09_be",
  233. #else
  234. .name = "mdraid09",
  235. #endif
  236. .detect = grub_mdraid_detect,
  237. .next = 0
  238. };
  239. #ifdef MODE_BIGENDIAN
  240. GRUB_MOD_INIT (mdraid09_be)
  241. #else
  242. GRUB_MOD_INIT (mdraid09)
  243. #endif
  244. {
  245. grub_diskfilter_register_front (&grub_mdraid_dev);
  246. }
  247. #ifdef MODE_BIGENDIAN
  248. GRUB_MOD_FINI (mdraid09_be)
  249. #else
  250. GRUB_MOD_FINI (mdraid09)
  251. #endif
  252. {
  253. grub_diskfilter_unregister (&grub_mdraid_dev);
  254. }