/* mdraid_linux.c - module to handle Linux Software RAID. */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2008,2009,2010 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VasEBoot. If not, see . */ #include #include #include #include #include #include /* Linux RAID on disk structures and constants, copied from include/linux/raid/md_p.h. */ VasEBoot_MOD_LICENSE ("GPLv3+"); #ifdef MODE_BIGENDIAN #define VasEBoot_md_to_cpu64 VasEBoot_be_to_cpu64 #define VasEBoot_md_to_cpu32 VasEBoot_be_to_cpu32 #define VasEBoot_md_to_cpu16 VasEBoot_be_to_cpu16 #define VasEBoot_cpu_to_md64_compile_time VasEBoot_cpu_to_be64_compile_time #define VasEBoot_cpu_to_md32_compile_time VasEBoot_cpu_to_be32_compile_time #define VasEBoot_cpu_to_md16_compile_time VasEBoot_cpu_to_be16_compile_time #else #define VasEBoot_md_to_cpu64 VasEBoot_le_to_cpu64 #define VasEBoot_md_to_cpu32 VasEBoot_le_to_cpu32 #define VasEBoot_md_to_cpu16 VasEBoot_le_to_cpu16 #define VasEBoot_cpu_to_md64_compile_time VasEBoot_cpu_to_le64_compile_time #define VasEBoot_cpu_to_md32_compile_time VasEBoot_cpu_to_le32_compile_time #define VasEBoot_cpu_to_md16_compile_time VasEBoot_cpu_to_le16_compile_time #endif #define RESERVED_BYTES (64 * 1024) #define RESERVED_SECTORS (RESERVED_BYTES / 512) #define NEW_SIZE_SECTORS(x) ((x & ~(RESERVED_SECTORS - 1)) \ - RESERVED_SECTORS) #define SB_BYTES 4096 #define SB_WORDS (SB_BYTES / 4) #define SB_SECTORS (SB_BYTES / 512) /* * The following are counted in 32-bit words */ #define SB_GENERIC_OFFSET 0 #define SB_PERSONALITY_OFFSET 64 #define SB_DISKS_OFFSET 128 #define SB_DESCRIPTOR_OFFSET 992 #define SB_GENERIC_CONSTANT_WORDS 32 #define SB_GENERIC_STATE_WORDS 32 #define SB_GENERIC_WORDS (SB_GENERIC_CONSTANT_WORDS + \ SB_GENERIC_STATE_WORDS) #define SB_PERSONALITY_WORDS 64 #define SB_DESCRIPTOR_WORDS 32 #define SB_DISKS 27 #define SB_DISKS_WORDS (SB_DISKS * SB_DESCRIPTOR_WORDS) #define SB_RESERVED_WORDS (1024 \ - SB_GENERIC_WORDS \ - SB_PERSONALITY_WORDS \ - SB_DISKS_WORDS \ - SB_DESCRIPTOR_WORDS) #define SB_EQUAL_WORDS (SB_GENERIC_WORDS \ + SB_PERSONALITY_WORDS \ + SB_DISKS_WORDS) /* * Device "operational" state bits */ #define DISK_FAULTY 0 #define DISK_ACTIVE 1 #define DISK_SYNC 2 #define DISK_REMOVED 3 #define DISK_WRITEMOSTLY 9 #define SB_MAGIC 0xa92b4efc /* * Superblock state bits */ #define SB_CLEAN 0 #define SB_ERRORS 1 #define SB_BITMAP_PRESENT 8 struct VasEBoot_raid_disk_09 { VasEBoot_uint32_t number; /* Device number in the entire set. */ VasEBoot_uint32_t major; /* Device major number. */ VasEBoot_uint32_t minor; /* Device minor number. */ VasEBoot_uint32_t raid_disk; /* The role of the device in the raid set. */ VasEBoot_uint32_t state; /* Operational state. */ VasEBoot_uint32_t reserved[SB_DESCRIPTOR_WORDS - 5]; }; struct VasEBoot_raid_super_09 { /* * Constant generic information */ VasEBoot_uint32_t md_magic; /* MD identifier. */ VasEBoot_uint32_t major_version; /* Major version. */ VasEBoot_uint32_t minor_version; /* Minor version. */ VasEBoot_uint32_t patch_version; /* Patchlevel version. */ VasEBoot_uint32_t gvalid_words; /* Number of used words in this section. */ VasEBoot_uint32_t set_uuid0; /* Raid set identifier. */ VasEBoot_uint32_t ctime; /* Creation time. */ VasEBoot_uint32_t level; /* Raid personality. */ VasEBoot_uint32_t size; /* Apparent size of each individual disk. */ VasEBoot_uint32_t nr_disks; /* Total disks in the raid set. */ VasEBoot_uint32_t raid_disks; /* Disks in a fully functional raid set. */ VasEBoot_uint32_t md_minor; /* Preferred MD minor device number. */ VasEBoot_uint32_t not_persistent; /* Does it have a persistent superblock. */ VasEBoot_uint32_t set_uuid1; /* Raid set identifier #2. */ VasEBoot_uint32_t set_uuid2; /* Raid set identifier #3. */ VasEBoot_uint32_t set_uuid3; /* Raid set identifier #4. */ VasEBoot_uint32_t gstate_creserved[SB_GENERIC_CONSTANT_WORDS - 16]; /* * Generic state information */ VasEBoot_uint32_t utime; /* Superblock update time. */ VasEBoot_uint32_t state; /* State bits (clean, ...). */ VasEBoot_uint32_t active_disks; /* Number of currently active disks. */ VasEBoot_uint32_t working_disks; /* Number of working disks. */ VasEBoot_uint32_t failed_disks; /* Number of failed disks. */ VasEBoot_uint32_t spare_disks; /* Number of spare disks. */ VasEBoot_uint32_t sb_csum; /* Checksum of the whole superblock. */ VasEBoot_uint64_t events; /* Superblock update count. */ VasEBoot_uint64_t cp_events; /* Checkpoint update count. */ VasEBoot_uint32_t recovery_cp; /* Recovery checkpoint sector count. */ VasEBoot_uint32_t gstate_sreserved[SB_GENERIC_STATE_WORDS - 12]; /* * Personality information */ VasEBoot_uint32_t layout; /* The array's physical layout. */ VasEBoot_uint32_t chunk_size; /* Chunk size in bytes. */ VasEBoot_uint32_t root_pv; /* LV root PV. */ VasEBoot_uint32_t root_block; /* LV root block. */ VasEBoot_uint32_t pstate_reserved[SB_PERSONALITY_WORDS - 4]; /* * Disks information */ struct VasEBoot_raid_disk_09 disks[SB_DISKS]; /* * Reserved */ VasEBoot_uint32_t reserved[SB_RESERVED_WORDS]; /* * Active descriptor */ struct VasEBoot_raid_disk_09 this_disk; } VasEBoot_PACKED; static struct VasEBoot_diskfilter_vg * VasEBoot_mdraid_detect (VasEBoot_disk_t disk, struct VasEBoot_diskfilter_pv_id *id, VasEBoot_disk_addr_t *start_sector) { VasEBoot_disk_addr_t sector; VasEBoot_uint64_t size; struct VasEBoot_raid_super_09 *sb = NULL; VasEBoot_uint32_t *uuid; VasEBoot_uint32_t level; struct VasEBoot_diskfilter_vg *ret; /* The sector where the mdraid 0.90 superblock is stored, if available. */ size = VasEBoot_disk_get_size (disk); if (size == VasEBoot_DISK_SIZE_UNKNOWN) /* not 0.9x raid. */ return NULL; sector = NEW_SIZE_SECTORS (size); sb = VasEBoot_malloc (sizeof (*sb)); if (!sb) return NULL; if (VasEBoot_disk_read (disk, sector, 0, SB_BYTES, sb)) goto fail; /* Look whether there is a mdraid 0.90 superblock. */ if (sb->md_magic != VasEBoot_cpu_to_md32_compile_time (SB_MAGIC)) /* not 0.9x raid. */ goto fail; if (sb->major_version != VasEBoot_cpu_to_md32_compile_time (0) || sb->minor_version != VasEBoot_cpu_to_md32_compile_time (90)) /* Unsupported version. */ goto fail; /* No need for explicit check that sb->size is 0 (unspecified) since 0 >= non-0 is false. */ if (((VasEBoot_disk_addr_t) VasEBoot_md_to_cpu32 (sb->size)) * 2 >= size) goto fail; /* FIXME: Check the checksum. */ level = VasEBoot_md_to_cpu32 (sb->level); /* Multipath. */ if ((int) level == -4) level = 1; if (level != 0 && level != 1 && level != 4 && level != 5 && level != 6 && level != 10) { VasEBoot_error (VasEBoot_ERR_NOT_IMPLEMENTED_YET, "unsupported RAID level: %d", level); goto fail; } if (VasEBoot_md_to_cpu32 (sb->this_disk.number) == 0xffff || VasEBoot_md_to_cpu32 (sb->this_disk.number) == 0xfffe) /* Spares aren't implemented. */ goto fail; uuid = VasEBoot_malloc (16); if (!uuid) goto fail; uuid[0] = VasEBoot_swap_bytes32 (sb->set_uuid0); uuid[1] = VasEBoot_swap_bytes32 (sb->set_uuid1); uuid[2] = VasEBoot_swap_bytes32 (sb->set_uuid2); uuid[3] = VasEBoot_swap_bytes32 (sb->set_uuid3); *start_sector = 0; id->uuidlen = 0; id->id = VasEBoot_md_to_cpu32 (sb->this_disk.number); char buf[32]; VasEBoot_snprintf (buf, sizeof (buf), "md%d", VasEBoot_md_to_cpu32 (sb->md_minor)); ret = VasEBoot_diskfilter_make_raid (16, (char *) uuid, VasEBoot_md_to_cpu32 (sb->raid_disks), buf, (sb->size) ? ((VasEBoot_disk_addr_t) VasEBoot_md_to_cpu32 (sb->size)) * 2 : sector, VasEBoot_md_to_cpu32 (sb->chunk_size) >> 9, VasEBoot_md_to_cpu32 (sb->layout), level); VasEBoot_free (sb); return ret; fail: VasEBoot_free (sb); return NULL; } static struct VasEBoot_diskfilter VasEBoot_mdraid_dev = { #ifdef MODE_BIGENDIAN .name = "mdraid09_be", #else .name = "mdraid09", #endif .detect = VasEBoot_mdraid_detect, .next = 0 }; #ifdef MODE_BIGENDIAN VasEBoot_MOD_INIT (mdraid09_be) #else VasEBoot_MOD_INIT (mdraid09) #endif { VasEBoot_diskfilter_register_front (&VasEBoot_mdraid_dev); } #ifdef MODE_BIGENDIAN VasEBoot_MOD_FINI (mdraid09_be) #else VasEBoot_MOD_FINI (mdraid09) #endif { VasEBoot_diskfilter_unregister (&VasEBoot_mdraid_dev); }