~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

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 (sb.magic != SB_MAGIC)
 
147
        continue;
 
148
 
 
149
      {
 
150
        grub_uint64_t sb_size;
 
151
        struct grub_raid_super_1x *real_sb;
 
152
 
 
153
        if (sb.major_version != 1)
 
154
          return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
155
                             "Unsupported RAID version: %d",
 
156
                             sb.major_version);
 
157
 
 
158
        /* Multipath.  */
 
159
        if ((int) sb.level == -4)
 
160
          sb.level = 1;
 
161
 
 
162
        if (sb.level != 0 && sb.level != 1 && sb.level != 4 &&
 
163
            sb.level != 5 && sb.level != 6 && sb.level != 10)
 
164
          return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
165
                             "Unsupported RAID level: %d", sb.level);
 
166
 
 
167
        /* 1.x superblocks don't have a fixed size on disk.  So we have to
 
168
           read it again now that we now the max device count.  */
 
169
        sb_size = sizeof (struct grub_raid_super_1x) 
 
170
          + 2 * grub_le_to_cpu32 (sb.max_dev);
 
171
        real_sb = grub_malloc (sb_size);
 
172
        if (! real_sb)
 
173
          return grub_errno;
 
174
 
 
175
        if (grub_disk_read (disk, sector, 0, sb_size, real_sb))
 
176
          {
 
177
            grub_free (real_sb);
 
178
            return grub_errno;
 
179
          }
 
180
 
 
181
        array->name = grub_strdup (real_sb->set_name);
 
182
        if (! array->name)
 
183
          {
 
184
            grub_free (real_sb);
 
185
            return grub_errno;
 
186
          }
 
187
 
 
188
        array->number = 0;
 
189
        array->level = grub_le_to_cpu32 (real_sb->level);
 
190
        array->layout = grub_le_to_cpu32 (real_sb->layout);
 
191
        array->total_devs = grub_le_to_cpu32 (real_sb->raid_disks);
 
192
        array->disk_size = grub_le_to_cpu64 (real_sb->size);
 
193
        array->chunk_size = grub_le_to_cpu32 (real_sb->chunksize);
 
194
 
 
195
        if (grub_le_to_cpu32 (real_sb->dev_number) >=
 
196
            grub_le_to_cpu32 (real_sb->max_dev))
 
197
          return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
198
                             "spares aren't implemented");
 
199
 
 
200
        array->index = grub_le_to_cpu16
 
201
          (real_sb->dev_roles[grub_le_to_cpu32 (real_sb->dev_number)]);
 
202
        array->uuid_len = 16;
 
203
        array->uuid = grub_malloc (16);
 
204
        if (!array->uuid)
 
205
          {
 
206
            grub_free (real_sb);
 
207
            return grub_errno;
 
208
          }
 
209
 
 
210
        grub_memcpy (array->uuid, real_sb->set_uuid, 16);
 
211
        
 
212
        *start_sector = real_sb->data_offset;
 
213
 
 
214
        grub_free (real_sb);
 
215
        return 0;
 
216
      }
 
217
    }
 
218
 
 
219
  return grub_error (GRUB_ERR_OUT_OF_RANGE, "not 1.x raid");
 
220
}
 
221
 
 
222
static struct grub_raid grub_mdraid_dev = {
 
223
  .name = "mdraid1x",
 
224
  .detect = grub_mdraid_detect,
 
225
  .next = 0
 
226
};
 
227
 
 
228
GRUB_MOD_INIT (mdraid1x)
 
229
{
 
230
  grub_raid_register (&grub_mdraid_dev);
 
231
}
 
232
 
 
233
GRUB_MOD_FINI (mdraid1x)
 
234
{
 
235
  grub_raid_unregister (&grub_mdraid_dev);
 
236
}