mdraid_linux.c 8.87 KB
Newer Older
1
/* mdraid_linux.c - module to handle Linux Software RAID.  */
2 3
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2008,2009,2010  Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  GRUB 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.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/dl.h>
#include <grub/disk.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/misc.h>
25
#include <grub/diskfilter.h>
26 27 28 29

/* Linux RAID on disk structures and constants,
   copied from include/linux/raid/md_p.h.  */

30 31
GRUB_MOD_LICENSE ("GPLv3+");

32 33 34 35
#ifdef MODE_BIGENDIAN
#define grub_md_to_cpu64 grub_be_to_cpu64
#define grub_md_to_cpu32 grub_be_to_cpu32
#define grub_md_to_cpu16 grub_be_to_cpu16
36 37 38
#define grub_cpu_to_md64_compile_time grub_cpu_to_be64_compile_time
#define grub_cpu_to_md32_compile_time grub_cpu_to_be32_compile_time
#define grub_cpu_to_md16_compile_time grub_cpu_to_be16_compile_time
39 40 41 42
#else
#define grub_md_to_cpu64 grub_le_to_cpu64
#define grub_md_to_cpu32 grub_le_to_cpu32
#define grub_md_to_cpu16 grub_le_to_cpu16
43 44 45
#define grub_cpu_to_md64_compile_time grub_cpu_to_le64_compile_time
#define grub_cpu_to_md32_compile_time grub_cpu_to_le32_compile_time
#define grub_cpu_to_md16_compile_time grub_cpu_to_le16_compile_time
46 47
#endif

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
#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 grub_raid_disk_09
{
  grub_uint32_t number;		/* Device number in the entire set.  */
  grub_uint32_t major;		/* Device major number.  */
  grub_uint32_t minor;		/* Device minor number.  */
  grub_uint32_t raid_disk;	/* The role of the device in the raid set.  */
  grub_uint32_t state;		/* Operational state.  */
  grub_uint32_t reserved[SB_DESCRIPTOR_WORDS - 5];
};

struct grub_raid_super_09
{
  /*
   * Constant generic information
   */
  grub_uint32_t md_magic;	/* MD identifier.  */
  grub_uint32_t major_version;	/* Major version.  */
  grub_uint32_t minor_version;	/* Minor version.  */
  grub_uint32_t patch_version;	/* Patchlevel version.  */
  grub_uint32_t gvalid_words;	/* Number of used words in this section.  */
  grub_uint32_t set_uuid0;	/* Raid set identifier.  */
  grub_uint32_t ctime;		/* Creation time.  */
  grub_uint32_t level;		/* Raid personality.  */
  grub_uint32_t size;		/* Apparent size of each individual disk.  */
  grub_uint32_t nr_disks;	/* Total disks in the raid set.  */
  grub_uint32_t raid_disks;	/* Disks in a fully functional raid set.  */
  grub_uint32_t md_minor;	/* Preferred MD minor device number.  */
  grub_uint32_t not_persistent;	/* Does it have a persistent superblock.  */
  grub_uint32_t set_uuid1;	/* Raid set identifier #2.  */
  grub_uint32_t set_uuid2;	/* Raid set identifier #3.  */
  grub_uint32_t set_uuid3;	/* Raid set identifier #4.  */
  grub_uint32_t gstate_creserved[SB_GENERIC_CONSTANT_WORDS - 16];

  /*
   * Generic state information
   */
  grub_uint32_t utime;		/* Superblock update time.  */
  grub_uint32_t state;		/* State bits (clean, ...).  */
  grub_uint32_t active_disks;	/* Number of currently active disks.  */
  grub_uint32_t working_disks;	/* Number of working disks.  */
  grub_uint32_t failed_disks;	/* Number of failed disks.  */
  grub_uint32_t spare_disks;	/* Number of spare disks.  */
  grub_uint32_t sb_csum;	/* Checksum of the whole superblock.  */
  grub_uint64_t events;		/* Superblock update count.  */
  grub_uint64_t cp_events;	/* Checkpoint update count.  */
  grub_uint32_t recovery_cp;	/* Recovery checkpoint sector count.  */
  grub_uint32_t gstate_sreserved[SB_GENERIC_STATE_WORDS - 12];

  /*
   * Personality information
   */
  grub_uint32_t layout;		/* The array's physical layout.  */
  grub_uint32_t chunk_size;	/* Chunk size in bytes.  */
  grub_uint32_t root_pv;	/* LV root PV.  */
  grub_uint32_t root_block;	/* LV root block.  */
  grub_uint32_t pstate_reserved[SB_PERSONALITY_WORDS - 4];

  /*
   * Disks information
   */
  struct grub_raid_disk_09 disks[SB_DISKS];

  /*
   * Reserved
   */
  grub_uint32_t reserved[SB_RESERVED_WORDS];

  /*
   * Active descriptor
   */
  struct grub_raid_disk_09 this_disk;
178
} GRUB_PACKED;
179

180 181 182
static struct grub_diskfilter_vg *
grub_mdraid_detect (grub_disk_t disk,
		    struct grub_diskfilter_pv_id *id,
183
		    grub_disk_addr_t *start_sector)
184
{
185 186
  grub_disk_addr_t sector;
  grub_uint64_t size;
187
  struct grub_raid_super_09 *sb = NULL;
188
  grub_uint32_t *uuid;
189
  grub_uint32_t level;
190
  struct grub_diskfilter_vg *ret;
191

192 193
  /* The sector where the mdraid 0.90 superblock is stored, if available.  */
  size = grub_disk_get_size (disk);
194
  if (size == GRUB_DISK_SIZE_UNKNOWN)
195 196
    /* not 0.9x raid.  */
    return NULL;
197 198
  sector = NEW_SIZE_SECTORS (size);

199 200
  sb = grub_malloc (sizeof (*sb));
  if (!sb)
201
    return NULL;
202

203 204 205
  if (grub_disk_read (disk, sector, 0, SB_BYTES, sb))
    goto fail;

206
  /* Look whether there is a mdraid 0.90 superblock.  */
207
  if (sb->md_magic != grub_cpu_to_md32_compile_time (SB_MAGIC))
208
    /* not 0.9x raid.  */
209
    goto fail;
210

211 212
  if (sb->major_version != grub_cpu_to_md32_compile_time (0)
      || sb->minor_version != grub_cpu_to_md32_compile_time (90))
213
    /* Unsupported version.  */
214
    goto fail;
215

216
  /* No need for explicit check that sb->size is 0 (unspecified) since
217
     0 >= non-0 is false.  */
218 219
  if (((grub_disk_addr_t) grub_md_to_cpu32 (sb->size)) * 2 >= size)
    goto fail;
220

221
  /* FIXME: Check the checksum.  */
222

223
  level = grub_md_to_cpu32 (sb->level);
224
  /* Multipath.  */
225 226
  if ((int) level == -4)
    level = 1;
227

228 229
  if (level != 0 && level != 1 && level != 4 &&
      level != 5 && level != 6 && level != 10)
230 231 232
    {
      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
		  "unsupported RAID level: %d", level);
233
      goto fail;
234
    }
235 236
  if (grub_md_to_cpu32 (sb->this_disk.number) == 0xffff
      || grub_md_to_cpu32 (sb->this_disk.number) == 0xfffe)
237
    /* Spares aren't implemented.  */
238
    goto fail;
239

240
  uuid = grub_malloc (16);
241
  if (!uuid)
242
    goto fail;
243

244 245 246 247
  uuid[0] = grub_swap_bytes32 (sb->set_uuid0);
  uuid[1] = grub_swap_bytes32 (sb->set_uuid1);
  uuid[2] = grub_swap_bytes32 (sb->set_uuid2);
  uuid[3] = grub_swap_bytes32 (sb->set_uuid3);
248

249 250
  *start_sector = 0;

251
  id->uuidlen = 0;
252
  id->id = grub_md_to_cpu32 (sb->this_disk.number);
253 254

  char buf[32];
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  grub_snprintf (buf, sizeof (buf), "md%d", grub_md_to_cpu32 (sb->md_minor));
  ret = grub_diskfilter_make_raid (16, (char *) uuid,
				   grub_md_to_cpu32 (sb->raid_disks), buf,
				   (sb->size) ? ((grub_disk_addr_t)
						 grub_md_to_cpu32 (sb->size)) * 2
				   : sector,
				   grub_md_to_cpu32 (sb->chunk_size) >> 9,
				   grub_md_to_cpu32 (sb->layout),
				   level);
  grub_free (sb);
  return ret;

 fail:
  grub_free (sb);
  return NULL;
270
}
271

272
static struct grub_diskfilter grub_mdraid_dev = {
273 274 275
#ifdef MODE_BIGENDIAN
  .name = "mdraid09_be",
#else
276
  .name = "mdraid09",
277
#endif
278 279 280 281
  .detect = grub_mdraid_detect,
  .next = 0
};

282 283 284
#ifdef MODE_BIGENDIAN
GRUB_MOD_INIT (mdraid09_be)
#else
285
GRUB_MOD_INIT (mdraid09)
286
#endif
287
{
288
  grub_diskfilter_register_front (&grub_mdraid_dev);
289 290
}

291 292 293
#ifdef MODE_BIGENDIAN
GRUB_MOD_FINI (mdraid09_be)
#else
294
GRUB_MOD_FINI (mdraid09)
295
#endif
296
{
297
  grub_diskfilter_unregister (&grub_mdraid_dev);
298
}