1 /* mdraid_linux.c - module to handle linux softraid. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
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.
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.
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/>.
21 #include <grub/disk.h>
24 #include <grub/misc.h>
25 #include <grub/raid.h>
27 /* Linux RAID on disk structures and constants,
28 copied from include/linux/raid/md_p.h. */
30 #define RESERVED_BYTES (64 * 1024)
31 #define RESERVED_SECTORS (RESERVED_BYTES / 512)
33 #define NEW_SIZE_SECTORS(x) ((x & ~(RESERVED_SECTORS - 1)) \
37 #define SB_WORDS (SB_BYTES / 4)
38 #define SB_SECTORS (SB_BYTES / 512)
41 * The following are counted in 32-bit words
43 #define SB_GENERIC_OFFSET 0
45 #define SB_PERSONALITY_OFFSET 64
46 #define SB_DISKS_OFFSET 128
47 #define SB_DESCRIPTOR_OFFSET 992
49 #define SB_GENERIC_CONSTANT_WORDS 32
50 #define SB_GENERIC_STATE_WORDS 32
51 #define SB_GENERIC_WORDS (SB_GENERIC_CONSTANT_WORDS + \
52 SB_GENERIC_STATE_WORDS)
54 #define SB_PERSONALITY_WORDS 64
55 #define SB_DESCRIPTOR_WORDS 32
57 #define SB_DISKS_WORDS (SB_DISKS * SB_DESCRIPTOR_WORDS)
59 #define SB_RESERVED_WORDS (1024 \
61 - SB_PERSONALITY_WORDS \
63 - SB_DESCRIPTOR_WORDS)
65 #define SB_EQUAL_WORDS (SB_GENERIC_WORDS \
66 + SB_PERSONALITY_WORDS \
70 * Device "operational" state bits
75 #define DISK_REMOVED 3
77 #define DISK_WRITEMOSTLY 9
79 #define SB_MAGIC 0xa92b4efc
82 * Superblock state bits
87 #define SB_BITMAP_PRESENT 8
89 struct grub_raid_disk_09
91 grub_uint32_t number
; /* Device number in the entire set. */
92 grub_uint32_t major
; /* Device major number. */
93 grub_uint32_t minor
; /* Device minor number. */
94 grub_uint32_t raid_disk
; /* The role of the device in the raid set. */
95 grub_uint32_t state
; /* Operational state. */
96 grub_uint32_t reserved
[SB_DESCRIPTOR_WORDS
- 5];
99 struct grub_raid_super_09
102 * Constant generic information
104 grub_uint32_t md_magic
; /* MD identifier. */
105 grub_uint32_t major_version
; /* Major version. */
106 grub_uint32_t minor_version
; /* Minor version. */
107 grub_uint32_t patch_version
; /* Patchlevel version. */
108 grub_uint32_t gvalid_words
; /* Number of used words in this section. */
109 grub_uint32_t set_uuid0
; /* Raid set identifier. */
110 grub_uint32_t ctime
; /* Creation time. */
111 grub_uint32_t level
; /* Raid personality. */
112 grub_uint32_t size
; /* Apparent size of each individual disk. */
113 grub_uint32_t nr_disks
; /* Total disks in the raid set. */
114 grub_uint32_t raid_disks
; /* Disks in a fully functional raid set. */
115 grub_uint32_t md_minor
; /* Preferred MD minor device number. */
116 grub_uint32_t not_persistent
; /* Does it have a persistent superblock. */
117 grub_uint32_t set_uuid1
; /* Raid set identifier #2. */
118 grub_uint32_t set_uuid2
; /* Raid set identifier #3. */
119 grub_uint32_t set_uuid3
; /* Raid set identifier #4. */
120 grub_uint32_t gstate_creserved
[SB_GENERIC_CONSTANT_WORDS
- 16];
123 * Generic state information
125 grub_uint32_t utime
; /* Superblock update time. */
126 grub_uint32_t state
; /* State bits (clean, ...). */
127 grub_uint32_t active_disks
; /* Number of currently active disks. */
128 grub_uint32_t working_disks
; /* Number of working disks. */
129 grub_uint32_t failed_disks
; /* Number of failed disks. */
130 grub_uint32_t spare_disks
; /* Number of spare disks. */
131 grub_uint32_t sb_csum
; /* Checksum of the whole superblock. */
132 grub_uint64_t events
; /* Superblock update count. */
133 grub_uint64_t cp_events
; /* Checkpoint update count. */
134 grub_uint32_t recovery_cp
; /* Recovery checkpoint sector count. */
135 grub_uint32_t gstate_sreserved
[SB_GENERIC_STATE_WORDS
- 12];
138 * Personality information
140 grub_uint32_t layout
; /* The array's physical layout. */
141 grub_uint32_t chunk_size
; /* Chunk size in bytes. */
142 grub_uint32_t root_pv
; /* LV root PV. */
143 grub_uint32_t root_block
; /* LV root block. */
144 grub_uint32_t pstate_reserved
[SB_PERSONALITY_WORDS
- 4];
149 struct grub_raid_disk_09 disks
[SB_DISKS
];
154 grub_uint32_t reserved
[SB_RESERVED_WORDS
];
159 struct grub_raid_disk_09 this_disk
;
160 } __attribute__ ((packed
));
163 grub_mdraid_detect (grub_disk_t disk
, struct grub_raid_array
*array
)
165 grub_disk_addr_t sector
;
167 struct grub_raid_super_09 sb
;
170 /* The sector where the RAID superblock is stored, if available. */
171 size
= grub_disk_get_size (disk
);
172 sector
= NEW_SIZE_SECTORS (size
);
174 if (grub_disk_read (disk
, sector
, 0, SB_BYTES
, (char *) &sb
))
177 /* Look whether there is a RAID superblock. */
178 if (sb
.md_magic
!= SB_MAGIC
)
179 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "not raid");
181 /* FIXME: Also support version 1.0. */
182 if (sb
.major_version
!= 0 || sb
.minor_version
!= 90)
183 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
184 "Unsupported RAID version: %d.%d",
185 sb
.major_version
, sb
.minor_version
);
187 /* FIXME: Check the checksum. */
190 if ((int) sb
.level
== -4)
193 if (sb
.level
!= 0 && sb
.level
!= 1 && sb
.level
!= 4 &&
194 sb
.level
!= 5 && sb
.level
!= 6 && sb
.level
!= 10)
195 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
196 "Unsupported RAID level: %d", sb
.level
);
198 array
->number
= sb
.md_minor
;
199 array
->level
= sb
.level
;
200 array
->layout
= sb
.layout
;
201 array
->total_devs
= sb
.raid_disks
;
202 array
->disk_size
= (sb
.size
) ? sb
.size
* 2 : sector
;
203 array
->chunk_size
= sb
.chunk_size
>> 9;
204 array
->index
= sb
.this_disk
.number
;
205 array
->uuid_len
= 16;
206 array
->uuid
= grub_malloc (16);
210 uuid
= (grub_uint32_t
*) array
->uuid
;
211 uuid
[0] = sb
.set_uuid0
;
212 uuid
[1] = sb
.set_uuid1
;
213 uuid
[2] = sb
.set_uuid2
;
214 uuid
[3] = sb
.set_uuid3
;
219 static struct grub_raid grub_mdraid_dev
= {
221 .detect
= grub_mdraid_detect
,
225 GRUB_MOD_INIT (mdraid
)
227 grub_raid_register (&grub_mdraid_dev
);
230 GRUB_MOD_FINI (mdraid
)
232 grub_raid_register (&grub_mdraid_dev
);