~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to grub-core/disk/mdraid1x_linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-02-08 11:39:26 UTC
  • mfrom: (17.6.26 experimental)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 104.
  • Revision ID: james.westby@ubuntu.com-20110208113926-clfs90haboyk9zip
Tags: 1.99~rc1-2
* Merge 1.98+20100804-13 and 1.98+20100804-14, updating translations:
  - Kazakh (Baurzhan Muftakhidinov / Timur Birsh).
* mkconfig_skip_dmcrypt.patch: Refer to GRUB_PRELOAD_MODULES rather than
  suggesting people write a /etc/grub.d/01_modules script (thanks, Jordan
  Uggla).
* Handle empty dir passed to grub_find_root_device_from_mountinfo; fixes
  grub-mkrelpath on btrfs subvolumes (LP: #712029).
* Add rootflags=subvol=<name> if / is on a btrfs subvolume (LP: #712029).
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *  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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <grub/dl.h>
 
21
#include <grub/disk.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/err.h>
 
24
#include <grub/misc.h>
 
25
#include <grub/raid.h>
 
26
 
 
27
/* Linux RAID on disk structures and constants,
 
28
   copied from include/linux/raid/md_p.h.  */
 
29
 
 
30
#define SB_MAGIC                        0xa92b4efc
 
31
 
 
32
/*
 
33
 * The version-1 superblock :
 
34
 * All numeric fields are little-endian.
 
35
 *
 
36
 * Total size: 256 bytes plus 2 per device.
 
37
 * 1K allows 384 devices.
 
38
 */
 
39
 
 
40
struct grub_raid_super_1x
 
41
{
 
42
  /* Constant array information - 128 bytes.  */
 
43
  grub_uint32_t magic;          /* MD_SB_MAGIC: 0xa92b4efc - little endian.  */
 
44
  grub_uint32_t major_version;  /* 1.  */
 
45
  grub_uint32_t feature_map;    /* Bit 0 set if 'bitmap_offset' is meaningful.   */
 
46
  grub_uint32_t pad0;           /* Always set to 0 when writing.  */
 
47
 
 
48
  grub_uint8_t set_uuid[16];    /* User-space generated.  */
 
49
  char set_name[32];            /* Set and interpreted by user-space.  */
 
50
 
 
51
  grub_uint64_t ctime;          /* Lo 40 bits are seconds, top 24 are microseconds or 0.  */
 
52
  grub_uint32_t level;          /* -4 (multipath), -1 (linear), 0,1,4,5.  */
 
53
  grub_uint32_t layout;         /* only for raid5 and raid10 currently.  */
 
54
  grub_uint64_t size;           /* Used size of component devices, in 512byte sectors.  */
 
55
 
 
56
  grub_uint32_t chunksize;      /* In 512byte sectors.  */
 
57
  grub_uint32_t raid_disks;
 
58
  grub_uint32_t bitmap_offset;  /* Sectors after start of superblock that bitmap starts
 
59
                                 * NOTE: signed, so bitmap can be before superblock
 
60
                                 * only meaningful of feature_map[0] is set.
 
61
                                 */
 
62
 
 
63
  /* These are only valid with feature bit '4'.  */
 
64
  grub_uint32_t new_level;      /* New level we are reshaping to.  */
 
65
  grub_uint64_t reshape_position;       /* Next address in array-space for reshape.  */
 
66
  grub_uint32_t delta_disks;    /* Change in number of raid_disks.  */
 
67
  grub_uint32_t new_layout;     /* New layout.  */
 
68
  grub_uint32_t new_chunk;      /* New chunk size (512byte sectors).  */
 
69
  grub_uint8_t pad1[128 - 124]; /* Set to 0 when written.  */
 
70
 
 
71
  /* Constant this-device information - 64 bytes.  */
 
72
  grub_uint64_t data_offset;    /* Sector start of data, often 0.  */
 
73
  grub_uint64_t data_size;      /* Sectors in this device that can be used for data.  */
 
74
  grub_uint64_t super_offset;   /* Sector start of this superblock.  */
 
75
  grub_uint64_t recovery_offset;        /* Sectors before this offset (from data_offset) have been recovered.  */
 
76
  grub_uint32_t dev_number;     /* Permanent identifier of this  device - not role in raid.  */
 
77
  grub_uint32_t cnt_corrected_read;     /* Number of read errors that were corrected by re-writing.  */
 
78
  grub_uint8_t device_uuid[16]; /* User-space setable, ignored by kernel.  */
 
79
  grub_uint8_t devflags;        /* Per-device flags.  Only one defined...  */
 
80
  grub_uint8_t pad2[64 - 57];   /* Set to 0 when writing.  */
 
81
 
 
82
  /* Array state information - 64 bytes.  */
 
83
  grub_uint64_t utime;          /* 40 bits second, 24 btes microseconds.  */
 
84
  grub_uint64_t events;         /* Incremented when superblock updated.  */
 
85
  grub_uint64_t resync_offset;  /* Data before this offset (from data_offset) known to be in sync.  */
 
86
  grub_uint32_t sb_csum;        /* Checksum upto devs[max_dev].  */
 
87
  grub_uint32_t max_dev;        /* Size of devs[] array to consider.  */
 
88
  grub_uint8_t pad3[64 - 32];   /* Set to 0 when writing.  */
 
89
 
 
90
  /* Device state information. Indexed by dev_number.
 
91
   * 2 bytes per device.
 
92
   * Note there are no per-device state flags. State information is rolled
 
93
   * into the 'roles' value.  If a device is spare or faulty, then it doesn't
 
94
   * have a meaningful role.
 
95
   */
 
96
  grub_uint16_t dev_roles[0];   /* Role in array, or 0xffff for a spare, or 0xfffe for faulty.  */
 
97
};
 
98
/* Could be __attribute__ ((packed)), but since all members in this struct
 
99
   are already appropriately aligned, we can omit this and avoid suboptimal
 
100
   assembly in some cases.  */
 
101
 
 
102
#define WriteMostly1    1       /* Mask for writemostly flag in above devflags.  */
 
103
 
 
104
static grub_err_t
 
105
grub_mdraid_detect (grub_disk_t disk, struct grub_raid_array *array,
 
106
                    grub_disk_addr_t *start_sector)
 
107
{
 
108
  grub_disk_addr_t sector = 0;
 
109
  grub_uint64_t size;
 
110
  struct grub_raid_super_1x sb;
 
111
  grub_uint8_t minor_version;
 
112
 
 
113
  /* The sector where the mdraid 0.90 superblock is stored, if available.  */
 
114
  size = grub_disk_get_size (disk);
 
115
 
 
116
  /* Check for an 1.x superblock.
 
117
   * It's always aligned to a 4K boundary
 
118
   * and depending on the minor version it can be:
 
119
   * 0: At least 8K, but less than 12K, from end of device
 
120
   * 1: At start of device
 
121
   * 2: 4K from start of device.
 
122
   */
 
123
 
 
124
  for (minor_version = 0; minor_version < 3; ++minor_version)
 
125
    {
 
126
      if (size == GRUB_DISK_SIZE_UNKNOWN && minor_version == 0)
 
127
        continue;
 
128
        
 
129
      switch (minor_version)
 
130
        {
 
131
        case 0:
 
132
          sector = (size - 8 * 2) & ~(4 * 2 - 1);
 
133
          break;
 
134
        case 1:
 
135
          sector = 0;
 
136
          break;
 
137
        case 2:
 
138
          sector = 4 * 2;
 
139
          break;
 
140
        }
 
141
 
 
142
      if (grub_disk_read (disk, sector, 0, sizeof (struct grub_raid_super_1x),
 
143
                          &sb))
 
144
        return grub_errno;
 
145
 
 
146
      if (grub_le_to_cpu32 (sb.magic) != SB_MAGIC)
 
147
        continue;
 
148
 
 
149
      {
 
150
        grub_uint64_t sb_size;
 
151
        struct grub_raid_super_1x *real_sb;
 
152
        grub_uint32_t level;
 
153
 
 
154
        if (grub_le_to_cpu32 (sb.major_version) != 1)
 
155
          return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
156
                             "Unsupported RAID version: %d",
 
157
                             grub_le_to_cpu32 (sb.major_version));
 
158
 
 
159
        level = grub_le_to_cpu32 (sb.level);
 
160
 
 
161
        /* Multipath.  */
 
162
        if ((int) level == -4)
 
163
          level = 1;
 
164
 
 
165
        if (level != 0 && level != 1 && level != 4 &&
 
166
            level != 5 && level != 6 && level != 10)
 
167
          return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
168
                             "Unsupported RAID level: %d", sb.level);
 
169
 
 
170
        /* 1.x superblocks don't have a fixed size on disk.  So we have to
 
171
           read it again now that we now the max device count.  */
 
172
        sb_size = sizeof (struct grub_raid_super_1x) 
 
173
          + 2 * grub_le_to_cpu32 (sb.max_dev);
 
174
        real_sb = grub_malloc (sb_size);
 
175
        if (! real_sb)
 
176
          return grub_errno;
 
177
 
 
178
        if (grub_disk_read (disk, sector, 0, sb_size, real_sb))
 
179
          {
 
180
            grub_free (real_sb);
 
181
            return grub_errno;
 
182
          }
 
183
 
 
184
        array->name = grub_strdup (real_sb->set_name);
 
185
        if (! array->name)
 
186
          {
 
187
            grub_free (real_sb);
 
188
            return grub_errno;
 
189
          }
 
190
 
 
191
        array->number = 0;
 
192
        array->level = grub_le_to_cpu32 (real_sb->level);
 
193
        array->layout = grub_le_to_cpu32 (real_sb->layout);
 
194
        array->total_devs = grub_le_to_cpu32 (real_sb->raid_disks);
 
195
        array->disk_size = grub_le_to_cpu64 (real_sb->size);
 
196
        array->chunk_size = grub_le_to_cpu32 (real_sb->chunksize);
 
197
 
 
198
        if (grub_le_to_cpu32 (real_sb->dev_number) >=
 
199
            grub_le_to_cpu32 (real_sb->max_dev))
 
200
          return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
201
                             "spares aren't implemented");
 
202
 
 
203
        array->index = grub_le_to_cpu16
 
204
          (real_sb->dev_roles[grub_le_to_cpu32 (real_sb->dev_number)]);
 
205
        array->uuid_len = 16;
 
206
        array->uuid = grub_malloc (16);
 
207
        if (!array->uuid)
 
208
          {
 
209
            grub_free (real_sb);
 
210
            return grub_errno;
 
211
          }
 
212
 
 
213
        grub_memcpy (array->uuid, real_sb->set_uuid, 16);
 
214
        
 
215
        *start_sector = grub_le_to_cpu64 (real_sb->data_offset);
 
216
 
 
217
        grub_free (real_sb);
 
218
        return 0;
 
219
      }
 
220
    }
 
221
 
 
222
  return grub_error (GRUB_ERR_OUT_OF_RANGE, "not 1.x raid");
 
223
}
 
224
 
 
225
static struct grub_raid grub_mdraid_dev = {
 
226
  .name = "mdraid1x",
 
227
  .detect = grub_mdraid_detect,
 
228
  .next = 0
 
229
};
 
230
 
 
231
GRUB_MOD_INIT (mdraid1x)
 
232
{
 
233
  grub_raid_register (&grub_mdraid_dev);
 
234
}
 
235
 
 
236
GRUB_MOD_FINI (mdraid1x)
 
237
{
 
238
  grub_raid_unregister (&grub_mdraid_dev);
 
239
}