1
/* raid.c - module to read RAID arrays. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2006,2007,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
/* Linked list of RAID arrays. */
28
static struct grub_raid_array *array_list;
29
grub_raid5_recover_func_t grub_raid5_recover_func;
30
grub_raid6_recover_func_t grub_raid6_recover_func;
34
grub_is_array_readable (struct grub_raid_array *array)
39
if (array->nr_devs == array->total_devs)
44
if (array->nr_devs >= 1)
55
if (array->level == 10)
57
n = array->layout & 0xFF;
59
n = (array->layout >> 8) & 0xFF;
66
if (array->nr_devs >= array->total_devs - n)
77
grub_raid_iterate (int (*hook) (const char *name))
79
struct grub_raid_array *array;
81
for (array = array_list; array != NULL; array = array->next)
83
if (grub_is_array_readable (array))
84
if (hook (array->name))
92
static grub_disk_memberlist_t
93
grub_raid_memberlist (grub_disk_t disk)
95
struct grub_raid_array *array = disk->data;
96
grub_disk_memberlist_t list = NULL, tmp;
99
for (i = 0; i < array->total_devs; i++)
100
if (array->device[i])
102
tmp = grub_malloc (sizeof (*tmp));
103
tmp->disk = array->device[i];
113
grub_raid_open (const char *name, grub_disk_t disk)
115
struct grub_raid_array *array;
118
for (array = array_list; array != NULL; array = array->next)
120
if (!grub_strcmp (array->name, name))
121
if (grub_is_array_readable (array))
126
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "unknown RAID device %s",
129
disk->has_partitions = 1;
130
disk->id = array->number;
133
grub_dprintf ("raid", "%s: total_devs=%d, disk_size=%lld\n", name,
134
array->total_devs, (unsigned long long) array->disk_size);
136
switch (array->level)
139
disk->total_sectors = array->disk_size;
143
n = array->layout & 0xFF;
145
n = (array->layout >> 8) & 0xFF;
147
disk->total_sectors = grub_divmod64 (array->total_devs *
156
n = array->level / 3;
158
disk->total_sectors = (array->total_devs - n) * array->disk_size;
162
grub_dprintf ("raid", "%s: level=%d, total_sectors=%lld\n", name,
163
array->level, (unsigned long long) disk->total_sectors);
169
grub_raid_close (grub_disk_t disk __attribute ((unused)))
175
grub_raid_block_xor (char *buf1, const char *buf2, int size)
178
const grub_size_t *p2;
180
p1 = (grub_size_t *) buf1;
181
p2 = (const grub_size_t *) buf2;
182
size /= GRUB_CPU_SIZEOF_VOID_P;
192
grub_raid_read (grub_disk_t disk, grub_disk_addr_t sector,
193
grub_size_t size, char *buf)
195
struct grub_raid_array *array = disk->data;
198
switch (array->level)
204
grub_disk_addr_t read_sector, far_ofs;
205
grub_uint32_t disknr, b, near, far, ofs;
207
read_sector = grub_divmod64 (sector, array->chunk_size, &b);
208
far = ofs = near = 1;
211
if (array->level == 1)
212
near = array->total_devs;
213
else if (array->level == 10)
215
near = array->layout & 0xFF;
216
far = (array->layout >> 8) & 0xFF;
217
if (array->layout >> 16)
223
far_ofs = grub_divmod64 (array->disk_size,
224
far * array->chunk_size, 0);
226
far_ofs *= array->chunk_size;
229
read_sector = grub_divmod64 (read_sector * near, array->total_devs,
232
ofs *= array->chunk_size;
237
grub_size_t read_size;
240
read_size = array->chunk_size - b;
241
if (read_size > size)
244
for (i = 0; i < near; i++)
249
for (j = 0; j < far; j++)
251
if (array->device[k])
253
if (grub_errno == GRUB_ERR_READ_ERROR)
254
grub_errno = GRUB_ERR_NONE;
256
err = grub_disk_read (array->device[k],
257
array->start_sector[k] +
258
read_sector + j * far_ofs + b,
260
read_size << GRUB_DISK_SECTOR_BITS,
264
else if (err != GRUB_ERR_READ_ERROR)
268
err = grub_error (GRUB_ERR_READ_ERROR,
272
if (k == array->total_devs)
280
if (disknr == array->total_devs)
290
buf += read_size << GRUB_DISK_SECTOR_BITS;
296
disknr += (near - i);
297
while (disknr >= array->total_devs)
299
disknr -= array->total_devs;
310
grub_disk_addr_t read_sector;
311
grub_uint32_t b, p, n, disknr, e;
313
/* n = 1 for level 4 and 5, 2 for level 6. */
314
n = array->level / 3;
316
/* Find the first sector to read. */
317
read_sector = grub_divmod64 (sector, array->chunk_size, &b);
318
read_sector = grub_divmod64 (read_sector, array->total_devs - n,
320
if (array->level >= 5)
322
grub_divmod64 (read_sector, array->total_devs, &p);
324
if (! (array->layout & GRUB_RAID_LAYOUT_RIGHT_MASK))
325
p = array->total_devs - 1 - p;
327
if (array->layout & GRUB_RAID_LAYOUT_SYMMETRIC_MASK)
336
if (q >= array->total_devs)
337
q -= array->total_devs;
341
else if (disknr >= q)
345
if (disknr >= array->total_devs)
346
disknr -= array->total_devs;
349
p = array->total_devs - n;
351
read_sector *= array->chunk_size;
355
grub_size_t read_size;
358
read_size = array->chunk_size - b;
359
if (read_size > size)
363
if (array->device[disknr])
365
/* Reset read error. */
366
if (grub_errno == GRUB_ERR_READ_ERROR)
367
grub_errno = GRUB_ERR_NONE;
369
err = grub_disk_read (array->device[disknr],
370
array->start_sector[disknr] +
372
read_size << GRUB_DISK_SECTOR_BITS,
375
if ((err) && (err != GRUB_ERR_READ_ERROR))
380
err = GRUB_ERR_READ_ERROR;
384
if (array->nr_devs < array->total_devs - n + e)
387
grub_errno = GRUB_ERR_NONE;
388
if (array->level == 6)
390
err = ((grub_raid6_recover_func) ?
391
(*grub_raid6_recover_func) (array, disknr, p,
392
buf, read_sector + b,
394
grub_error (GRUB_ERR_BAD_DEVICE,
395
"raid6rec is not loaded"));
399
err = ((grub_raid5_recover_func) ?
400
(*grub_raid5_recover_func) (array, disknr,
401
buf, read_sector + b,
403
grub_error (GRUB_ERR_BAD_DEVICE,
404
"raid5rec is not loaded"));
411
buf += read_size << GRUB_DISK_SECTOR_BITS;
419
if (array->layout & GRUB_RAID_LAYOUT_SYMMETRIC_MASK)
421
if (disknr == array->total_devs)
424
next_level = (disknr == p);
431
next_level = (disknr >= array->total_devs);
436
read_sector += array->chunk_size;
438
if (array->level >= 5)
440
if (array->layout & GRUB_RAID_LAYOUT_RIGHT_MASK)
441
p = (p == array->total_devs - 1) ? 0 : p + 1;
443
p = (p == 0) ? array->total_devs - 1 : p - 1;
445
if (array->layout & GRUB_RAID_LAYOUT_SYMMETRIC_MASK)
448
if (disknr >= array->total_devs)
449
disknr -= array->total_devs;
453
disknr -= array->total_devs;
470
grub_raid_write (grub_disk_t disk __attribute ((unused)),
471
grub_disk_addr_t sector __attribute ((unused)),
472
grub_size_t size __attribute ((unused)),
473
const char *buf __attribute ((unused)))
475
return GRUB_ERR_NOT_IMPLEMENTED_YET;
479
insert_array (grub_disk_t disk, struct grub_raid_array *new_array,
480
grub_disk_addr_t start_sector, const char *scanner_name)
482
struct grub_raid_array *array = 0, *p;
484
/* See whether the device is part of an array we have already seen a
486
for (p = array_list; p != NULL; p = p->next)
487
if ((p->uuid_len == new_array->uuid_len) &&
488
(! grub_memcmp (p->uuid, new_array->uuid, p->uuid_len)))
490
grub_free (new_array->uuid);
493
/* Do some checks before adding the device to the array. */
495
/* FIXME: Check whether the update time of the superblocks are
498
if (array->total_devs == array->nr_devs)
499
/* We found more members of the array than the array
500
actually has according to its superblock. This shouldn't
502
grub_dprintf ("raid", "array->nr_devs > array->total_devs (%d)?!?",
505
if (array->device[new_array->index] != NULL)
506
/* We found multiple devices with the same number. Again,
507
this shouldn't happen. */
508
grub_dprintf ("raid", "Found two disks with the number %d?!?",
511
if (new_array->disk_size < array->disk_size)
512
array->disk_size = new_array->disk_size;
516
/* Add an array to the list if we didn't find any. */
519
array = grub_malloc (sizeof (*array));
522
grub_free (new_array->uuid);
528
grub_memset (&array->device, 0, sizeof (array->device));
529
grub_memset (&array->start_sector, 0, sizeof (array->start_sector));
533
for (p = array_list; p != NULL; p = p->next)
535
if (! p->name && p->number == array->number)
540
if (array->name || p)
542
/* The number is already in use, so we need to find a new one.
543
(Or, in the case of named arrays, the array doesn't have its
544
own number, but we need one that doesn't clash for use as a key
545
in the disk cache. */
546
int i = array->name ? 0x40000000 : 0;
550
for (p = array_list; p != NULL; p = p->next)
558
/* We found an unused number. */
567
/* mdraid 1.x superblocks have only a name stored not a number.
568
Use it directly as GRUB device. */
571
array->name = grub_xasprintf ("md%d", array->number);
574
grub_free (array->uuid);
582
/* Strip off the homehost if present. */
583
char *colon = grub_strchr (array->name, ':');
584
char *new_name = grub_xasprintf ("md/%s",
585
colon ? colon + 1 : array->name);
589
grub_free (array->uuid);
595
grub_free (array->name);
596
array->name = new_name;
599
grub_dprintf ("raid", "Found array %s (%s)\n", array->name,
602
/* Add our new array to the list. */
603
array->next = array_list;
606
/* RAID 1 doesn't use a chunksize but code assumes one so set
608
if (array->level == 1)
609
array->chunk_size = 64;
612
/* Add the device to the array. */
613
array->device[new_array->index] = disk;
614
array->start_sector[new_array->index] = start_sector;
620
static grub_raid_t grub_raid_list;
625
struct grub_raid_array *array;
630
struct grub_raid_array *p;
636
for (i = 0; i < GRUB_RAID_MAX_DEVICES; i++)
638
grub_disk_close (p->device[i]);
649
grub_raid_register (grub_raid_t raid)
651
auto int hook (const char *name);
652
int hook (const char *name)
655
struct grub_raid_array array;
656
grub_disk_addr_t start_sector;
658
grub_dprintf ("raid", "Scanning for RAID devices on disk %s\n", name);
660
disk = grub_disk_open (name);
664
if ((disk->total_sectors != GRUB_ULONG_MAX) &&
665
(! grub_raid_list->detect (disk, &array, &start_sector)) &&
666
(! insert_array (disk, &array, start_sector, grub_raid_list->name)))
669
/* This error usually means it's not raid, no need to display
671
if (grub_errno != GRUB_ERR_OUT_OF_RANGE)
674
grub_errno = GRUB_ERR_NONE;
676
grub_disk_close (disk);
681
raid->next = grub_raid_list;
682
grub_raid_list = raid;
683
grub_device_iterate (&hook);
687
grub_raid_unregister (grub_raid_t raid)
691
for (p = &grub_raid_list, q = *p; q; p = &(q->next), q = q->next)
699
static struct grub_disk_dev grub_raid_dev =
702
.id = GRUB_DISK_DEVICE_RAID_ID,
703
.iterate = grub_raid_iterate,
704
.open = grub_raid_open,
705
.close = grub_raid_close,
706
.read = grub_raid_read,
707
.write = grub_raid_write,
709
.memberlist = grub_raid_memberlist,
717
grub_disk_dev_register (&grub_raid_dev);
722
grub_disk_dev_unregister (&grub_raid_dev);