1
/* mdraid_linux.c - module to handle Linux Software RAID. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008,2009,2010 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,
164
grub_disk_addr_t *start_sector)
166
grub_disk_addr_t sector;
168
struct grub_raid_super_09 sb;
171
/* The sector where the mdraid 0.90 superblock is stored, if available. */
172
size = grub_disk_get_size (disk);
173
if (size == GRUB_DISK_SIZE_UNKNOWN)
174
return grub_error (GRUB_ERR_OUT_OF_RANGE, "not 0.9x raid");
175
sector = NEW_SIZE_SECTORS (size);
177
if (grub_disk_read (disk, sector, 0, SB_BYTES, &sb))
180
/* Look whether there is a mdraid 0.90 superblock. */
181
if (sb.md_magic != SB_MAGIC)
182
return grub_error (GRUB_ERR_OUT_OF_RANGE, "not 0.9x raid");
184
if (sb.major_version != 0 || sb.minor_version != 90)
185
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
186
"unsupported RAID version: %d.%d",
187
sb.major_version, sb.minor_version);
189
/* FIXME: Check the checksum. */
192
if ((int) sb.level == -4)
195
if (sb.level != 0 && sb.level != 1 && sb.level != 4 &&
196
sb.level != 5 && sb.level != 6 && sb.level != 10)
197
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
198
"unsupported RAID level: %d", sb.level);
199
if (sb.this_disk.number == 0xffff || sb.this_disk.number == 0xfffe)
200
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
201
"spares aren't implemented");
204
array->number = sb.md_minor;
205
array->level = sb.level;
206
array->layout = sb.layout;
207
array->total_devs = sb.raid_disks;
208
array->disk_size = (sb.size) ? sb.size * 2 : sector;
209
array->chunk_size = sb.chunk_size >> 9;
210
array->index = sb.this_disk.number;
211
array->uuid_len = 16;
212
array->uuid = grub_malloc (16);
216
uuid = (grub_uint32_t *) array->uuid;
217
uuid[0] = sb.set_uuid0;
218
uuid[1] = sb.set_uuid1;
219
uuid[2] = sb.set_uuid2;
220
uuid[3] = sb.set_uuid3;
227
static struct grub_raid grub_mdraid_dev = {
229
.detect = grub_mdraid_detect,
233
GRUB_MOD_INIT (mdraid09)
235
grub_raid_register (&grub_mdraid_dev);
238
GRUB_MOD_FINI (mdraid09)
240
grub_raid_unregister (&grub_mdraid_dev);