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 #include <grub/util/misc.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 /* Linked list of RAID arrays. */
33 static struct grub_raid_array
*array_list
;
34 grub_raid5_recover_func_t grub_raid5_recover_func
;
35 grub_raid6_recover_func_t grub_raid6_recover_func
;
39 grub_is_array_readable (struct grub_raid_array
*array
)
44 if (array
->nr_devs
== array
->total_devs
)
49 if (array
->nr_devs
>= 1)
60 if (array
->level
== 10)
62 n
= array
->layout
& 0xFF;
64 n
= (array
->layout
>> 8) & 0xFF;
71 if (array
->nr_devs
>= array
->total_devs
- n
)
82 grub_raid_iterate (int (*hook
) (const char *name
))
84 struct grub_raid_array
*array
;
86 for (array
= array_list
; array
!= NULL
; array
= array
->next
)
88 if (grub_is_array_readable (array
))
89 if (hook (array
->name
))
97 static grub_disk_memberlist_t
98 grub_raid_memberlist (grub_disk_t disk
)
100 struct grub_raid_array
*array
= disk
->data
;
101 grub_disk_memberlist_t list
= NULL
, tmp
;
104 for (i
= 0; i
< array
->total_devs
; i
++)
105 if (array
->members
[i
].device
)
107 tmp
= grub_malloc (sizeof (*tmp
));
108 tmp
->disk
= array
->members
[i
].device
;
117 grub_raid_getname (struct grub_disk
*disk
)
119 struct grub_raid_array
*array
= disk
->data
;
121 return array
->driver
->name
;
126 grub_raid_open (const char *name
, grub_disk_t disk
)
128 struct grub_raid_array
*array
;
131 for (array
= array_list
; array
!= NULL
; array
= array
->next
)
133 if (!grub_strcmp (array
->name
, name
))
134 if (grub_is_array_readable (array
))
139 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "unknown RAID device %s",
142 disk
->id
= array
->number
;
145 grub_dprintf ("raid", "%s: total_devs=%d, disk_size=%lld\n", name
,
146 array
->total_devs
, (unsigned long long) array
->disk_size
);
148 switch (array
->level
)
151 disk
->total_sectors
= array
->disk_size
;
155 n
= array
->layout
& 0xFF;
157 n
= (array
->layout
>> 8) & 0xFF;
159 disk
->total_sectors
= grub_divmod64 (array
->total_devs
*
168 n
= array
->level
/ 3;
170 disk
->total_sectors
= (array
->total_devs
- n
) * array
->disk_size
;
174 grub_dprintf ("raid", "%s: level=%d, total_sectors=%lld\n", name
,
175 array
->level
, (unsigned long long) disk
->total_sectors
);
181 grub_raid_close (grub_disk_t disk
__attribute ((unused
)))
187 grub_raid_block_xor (char *buf1
, const char *buf2
, int size
)
190 const grub_size_t
*p2
;
192 p1
= (grub_size_t
*) buf1
;
193 p2
= (const grub_size_t
*) buf2
;
194 size
/= GRUB_CPU_SIZEOF_VOID_P
;
204 grub_raid_read (grub_disk_t disk
, grub_disk_addr_t sector
,
205 grub_size_t size
, char *buf
)
207 struct grub_raid_array
*array
= disk
->data
;
210 switch (array
->level
)
216 grub_disk_addr_t read_sector
, far_ofs
;
217 grub_uint32_t disknr
, b
, near
, far
, ofs
;
219 read_sector
= grub_divmod64 (sector
, array
->chunk_size
, &b
);
220 far
= ofs
= near
= 1;
223 if (array
->level
== 1)
224 near
= array
->total_devs
;
225 else if (array
->level
== 10)
227 near
= array
->layout
& 0xFF;
228 far
= (array
->layout
>> 8) & 0xFF;
229 if (array
->layout
>> 16)
235 far_ofs
= grub_divmod64 (array
->disk_size
,
236 far
* array
->chunk_size
, 0);
238 far_ofs
*= array
->chunk_size
;
241 read_sector
= grub_divmod64 (read_sector
* near
, array
->total_devs
,
244 ofs
*= array
->chunk_size
;
249 grub_size_t read_size
;
252 read_size
= array
->chunk_size
- b
;
253 if (read_size
> size
)
256 for (i
= 0; i
< near
; i
++)
261 for (j
= 0; j
< far
; j
++)
263 if (array
->members
[k
].device
)
265 if (grub_errno
== GRUB_ERR_READ_ERROR
)
266 grub_errno
= GRUB_ERR_NONE
;
268 err
= grub_disk_read (array
->members
[k
].device
,
269 array
->members
[k
].start_sector
+
270 read_sector
+ j
* far_ofs
+ b
,
272 read_size
<< GRUB_DISK_SECTOR_BITS
,
276 else if (err
!= GRUB_ERR_READ_ERROR
)
280 err
= grub_error (GRUB_ERR_READ_ERROR
,
284 if (k
== array
->total_devs
)
292 if (disknr
== array
->total_devs
)
302 buf
+= read_size
<< GRUB_DISK_SECTOR_BITS
;
308 disknr
+= (near
- i
);
309 while (disknr
>= array
->total_devs
)
311 disknr
-= array
->total_devs
;
322 grub_disk_addr_t read_sector
;
323 grub_uint32_t b
, p
, n
, disknr
, e
;
325 /* n = 1 for level 4 and 5, 2 for level 6. */
326 n
= array
->level
/ 3;
328 /* Find the first sector to read. */
329 read_sector
= grub_divmod64 (sector
, array
->chunk_size
, &b
);
330 read_sector
= grub_divmod64 (read_sector
, array
->total_devs
- n
,
332 if (array
->level
>= 5)
334 grub_divmod64 (read_sector
, array
->total_devs
, &p
);
336 if (! (array
->layout
& GRUB_RAID_LAYOUT_RIGHT_MASK
))
337 p
= array
->total_devs
- 1 - p
;
339 if (array
->layout
& GRUB_RAID_LAYOUT_SYMMETRIC_MASK
)
348 if (q
>= array
->total_devs
)
349 q
-= array
->total_devs
;
353 else if (disknr
>= q
)
357 if (disknr
>= array
->total_devs
)
358 disknr
-= array
->total_devs
;
361 p
= array
->total_devs
- n
;
363 read_sector
*= array
->chunk_size
;
367 grub_size_t read_size
;
370 read_size
= array
->chunk_size
- b
;
371 if (read_size
> size
)
375 if (array
->members
[disknr
].device
)
377 /* Reset read error. */
378 if (grub_errno
== GRUB_ERR_READ_ERROR
)
379 grub_errno
= GRUB_ERR_NONE
;
381 err
= grub_disk_read (array
->members
[disknr
].device
,
382 array
->members
[disknr
].start_sector
+
384 read_size
<< GRUB_DISK_SECTOR_BITS
,
387 if ((err
) && (err
!= GRUB_ERR_READ_ERROR
))
392 err
= GRUB_ERR_READ_ERROR
;
396 if (array
->nr_devs
< array
->total_devs
- n
+ e
)
399 grub_errno
= GRUB_ERR_NONE
;
400 if (array
->level
== 6)
402 err
= ((grub_raid6_recover_func
) ?
403 (*grub_raid6_recover_func
) (array
, disknr
, p
,
404 buf
, read_sector
+ b
,
406 grub_error (GRUB_ERR_BAD_DEVICE
,
407 "raid6rec is not loaded"));
411 err
= ((grub_raid5_recover_func
) ?
412 (*grub_raid5_recover_func
) (array
, disknr
,
413 buf
, read_sector
+ b
,
415 grub_error (GRUB_ERR_BAD_DEVICE
,
416 "raid5rec is not loaded"));
423 buf
+= read_size
<< GRUB_DISK_SECTOR_BITS
;
431 if (array
->layout
& GRUB_RAID_LAYOUT_SYMMETRIC_MASK
)
433 if (disknr
== array
->total_devs
)
436 next_level
= (disknr
== p
);
443 next_level
= (disknr
>= array
->total_devs
);
448 read_sector
+= array
->chunk_size
;
450 if (array
->level
>= 5)
452 if (array
->layout
& GRUB_RAID_LAYOUT_RIGHT_MASK
)
453 p
= (p
== array
->total_devs
- 1) ? 0 : p
+ 1;
455 p
= (p
== 0) ? array
->total_devs
- 1 : p
- 1;
457 if (array
->layout
& GRUB_RAID_LAYOUT_SYMMETRIC_MASK
)
460 if (disknr
>= array
->total_devs
)
461 disknr
-= array
->total_devs
;
465 disknr
-= array
->total_devs
;
482 grub_raid_write (grub_disk_t disk
__attribute ((unused
)),
483 grub_disk_addr_t sector
__attribute ((unused
)),
484 grub_size_t size
__attribute ((unused
)),
485 const char *buf
__attribute ((unused
)))
487 return GRUB_ERR_NOT_IMPLEMENTED_YET
;
491 insert_array (grub_disk_t disk
, struct grub_raid_array
*new_array
,
492 grub_disk_addr_t start_sector
, const char *scanner_name
,
493 grub_raid_t raid
__attribute__ ((unused
)))
495 struct grub_raid_array
*array
= 0, *p
;
497 /* See whether the device is part of an array we have already seen a
499 for (p
= array_list
; p
!= NULL
; p
= p
->next
)
500 if ((p
->uuid_len
== new_array
->uuid_len
) &&
501 (! grub_memcmp (p
->uuid
, new_array
->uuid
, p
->uuid_len
)))
503 grub_free (new_array
->uuid
);
506 /* Do some checks before adding the device to the array. */
508 if (new_array
->index
>= array
->allocated_devs
)
511 unsigned int newnum
= 2 * (new_array
->index
+ 1);
512 tmp
= grub_realloc (array
->members
, newnum
513 * sizeof (array
->members
[0]));
516 array
->members
= tmp
;
517 grub_memset (array
->members
+ array
->allocated_devs
,
518 0, (newnum
- array
->allocated_devs
)
519 * sizeof (array
->members
[0]));
520 array
->allocated_devs
= newnum
;
523 /* FIXME: Check whether the update time of the superblocks are
526 if (array
->total_devs
== array
->nr_devs
)
527 /* We found more members of the array than the array
528 actually has according to its superblock. This shouldn't
530 return grub_error (GRUB_ERR_BAD_DEVICE
,
531 "superfluous RAID member (%d found)",
534 if (array
->members
[new_array
->index
].device
!= NULL
)
535 /* We found multiple devices with the same number. Again,
536 this shouldn't happen. */
537 return grub_error (GRUB_ERR_BAD_DEVICE
,
538 "found two disks with the index %d for RAID %s",
539 new_array
->index
, array
->name
);
541 if (new_array
->disk_size
< array
->disk_size
)
542 array
->disk_size
= new_array
->disk_size
;
546 /* Add an array to the list if we didn't find any. */
549 array
= grub_malloc (sizeof (*array
));
552 grub_free (new_array
->uuid
);
559 array
->driver
= raid
;
561 array
->allocated_devs
= 32;
562 if (new_array
->index
>= array
->allocated_devs
)
563 array
->allocated_devs
= 2 * (new_array
->index
+ 1);
565 array
->members
= grub_zalloc (array
->allocated_devs
566 * sizeof (array
->members
[0]));
570 grub_free (new_array
->uuid
);
576 for (p
= array_list
; p
!= NULL
; p
= p
->next
)
578 if (p
->number
== array
->number
)
583 if (array
->name
|| p
)
585 /* The number is already in use, so we need to find a new one.
586 (Or, in the case of named arrays, the array doesn't have its
587 own number, but we need one that doesn't clash for use as a key
588 in the disk cache. */
589 int i
= array
->name
? 0x40000000 : 0;
593 for (p
= array_list
; p
!= NULL
; p
= p
->next
)
601 /* We found an unused number. */
610 /* mdraid 1.x superblocks have only a name stored not a number.
611 Use it directly as GRUB device. */
614 array
->name
= grub_xasprintf ("md%d", array
->number
);
617 grub_free (array
->members
);
618 grub_free (array
->uuid
);
626 /* Strip off the homehost if present. */
627 char *colon
= grub_strchr (array
->name
, ':');
628 char *new_name
= grub_xasprintf ("md/%s",
629 colon
? colon
+ 1 : array
->name
);
633 grub_free (array
->members
);
634 grub_free (array
->uuid
);
640 grub_free (array
->name
);
641 array
->name
= new_name
;
644 grub_dprintf ("raid", "Found array %s (%s)\n", array
->name
,
647 grub_util_info ("Found array %s (%s)", array
->name
,
651 /* Add our new array to the list. */
652 array
->next
= array_list
;
655 /* RAID 1 doesn't use a chunksize but code assumes one so set
657 if (array
->level
== 1)
658 array
->chunk_size
= 64;
661 /* Add the device to the array. */
662 array
->members
[new_array
->index
].device
= disk
;
663 array
->members
[new_array
->index
].start_sector
= start_sector
;
669 static grub_raid_t grub_raid_list
;
674 struct grub_raid_array
*array
;
679 struct grub_raid_array
*p
;
685 for (i
= 0; i
< p
->allocated_devs
; i
++)
686 if (p
->members
[i
].device
)
687 grub_disk_close (p
->members
[i
].device
);
688 grub_free (p
->members
);
699 grub_raid_register (grub_raid_t raid
)
701 auto int hook (const char *name
);
702 int hook (const char *name
)
705 struct grub_raid_array array
;
706 grub_disk_addr_t start_sector
;
708 grub_dprintf ("raid", "Scanning for %s RAID devices on disk %s\n",
709 grub_raid_list
->name
, name
);
711 grub_util_info ("Scanning for %s RAID devices on disk %s",
712 grub_raid_list
->name
, name
);
715 disk
= grub_disk_open (name
);
719 if ((disk
->total_sectors
!= GRUB_ULONG_MAX
) &&
720 (! grub_raid_list
->detect (disk
, &array
, &start_sector
)) &&
721 (! insert_array (disk
, &array
, start_sector
, grub_raid_list
->name
,
725 /* This error usually means it's not raid, no need to display
727 if (grub_errno
!= GRUB_ERR_OUT_OF_RANGE
)
730 grub_errno
= GRUB_ERR_NONE
;
732 grub_disk_close (disk
);
737 raid
->next
= grub_raid_list
;
738 grub_raid_list
= raid
;
739 grub_device_iterate (&hook
);
743 grub_raid_unregister (grub_raid_t raid
)
747 for (p
= &grub_raid_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
755 static struct grub_disk_dev grub_raid_dev
=
758 .id
= GRUB_DISK_DEVICE_RAID_ID
,
759 .iterate
= grub_raid_iterate
,
760 .open
= grub_raid_open
,
761 .close
= grub_raid_close
,
762 .read
= grub_raid_read
,
763 .write
= grub_raid_write
,
765 .memberlist
= grub_raid_memberlist
,
766 .raidname
= grub_raid_getname
,
774 grub_disk_dev_register (&grub_raid_dev
);
779 grub_disk_dev_unregister (&grub_raid_dev
);