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

« back to all changes in this revision

Viewing changes to disk/raid6_recover.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
 
/* raid6_recover.c - module to recover from faulty RAID6 arrays.  */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2006,2007,2008,2009  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
 
static grub_uint8_t raid6_table1[256][256];
28
 
static grub_uint8_t raid6_table2[256][256];
29
 
 
30
 
static void
31
 
grub_raid_block_mul (grub_uint8_t mul, char *buf, int size)
32
 
{
33
 
  int i;
34
 
  grub_uint8_t *p;
35
 
 
36
 
  p = (grub_uint8_t *) buf;
37
 
  for (i = 0; i < size; i++, p++)
38
 
    *p = raid6_table1[mul][*p];
39
 
}
40
 
 
41
 
static void
42
 
grub_raid6_init_table (void)
43
 
{
44
 
  int i, j;
45
 
 
46
 
  for (i = 0; i < 256; i++)
47
 
    raid6_table1[i][1] = raid6_table1[1][i] = i;
48
 
 
49
 
  for (i = 2; i < 256; i++)
50
 
    for (j = i; j < 256; j++)
51
 
      {
52
 
        int n;
53
 
        grub_uint8_t c;
54
 
 
55
 
        n = i >> 1;
56
 
 
57
 
        c = raid6_table1[n][j];
58
 
        c = (c << 1) ^ ((c & 0x80) ? 0x1d : 0);
59
 
        if (i & 1)
60
 
          c ^= j;
61
 
 
62
 
        raid6_table1[j][i] = raid6_table1[i][j] = c;
63
 
      }
64
 
 
65
 
  raid6_table2[0][0] = 1;
66
 
  for (i = 1; i < 256; i++)
67
 
    raid6_table2[i][i] = raid6_table1[raid6_table2[i - 1][i - 1]][2];
68
 
 
69
 
  for (i = 0; i < 254; i++)
70
 
    for (j = 0; j < 254; j++)
71
 
      {
72
 
        grub_uint8_t c, n;
73
 
        int k;
74
 
 
75
 
        if (i == j)
76
 
          continue;
77
 
 
78
 
        k = i - j;
79
 
        if (k < 0)
80
 
          k += 255;
81
 
 
82
 
        c = n = raid6_table2[k][k] ^ 1;
83
 
        for (k = 0; k < 253; k++)
84
 
          c = raid6_table1[c][n];
85
 
 
86
 
        raid6_table2[i][j] = raid6_table1[raid6_table2[255 - j][255 - j]][c];
87
 
      }
88
 
}
89
 
 
90
 
static grub_err_t
91
 
grub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
92
 
                    char *buf, grub_disk_addr_t sector, int size)
93
 
{
94
 
  int i, q, pos;
95
 
  int bad1 = -1, bad2 = -1;
96
 
  char *pbuf = 0, *qbuf = 0;
97
 
 
98
 
  size <<= GRUB_DISK_SECTOR_BITS;
99
 
  pbuf = grub_zalloc (size);
100
 
  if (!pbuf)
101
 
    goto quit;
102
 
 
103
 
  qbuf = grub_zalloc (size);
104
 
  if (!qbuf)
105
 
    goto quit;
106
 
 
107
 
  q = p + 1;
108
 
  if (q == (int) array->total_devs)
109
 
    q = 0;
110
 
 
111
 
  pos = q + 1;
112
 
  if (pos == (int) array->total_devs)
113
 
    pos = 0;
114
 
 
115
 
  for (i = 0; i < (int) array->total_devs - 2; i++)
116
 
    {
117
 
      if (pos == disknr)
118
 
        bad1 = i;
119
 
      else
120
 
        {
121
 
          if ((array->device[pos]) &&
122
 
              (! grub_disk_read (array->device[pos], sector, 0, size, buf)))
123
 
            {
124
 
              grub_raid_block_xor (pbuf, buf, size);
125
 
              grub_raid_block_mul (raid6_table2[i][i], buf, size);
126
 
              grub_raid_block_xor (qbuf, buf, size);
127
 
            }
128
 
          else
129
 
            {
130
 
              /* Too many bad devices */
131
 
              if (bad2 >= 0)
132
 
                goto quit;
133
 
 
134
 
              bad2 = i;
135
 
              grub_errno = GRUB_ERR_NONE;
136
 
            }
137
 
        }
138
 
 
139
 
      pos++;
140
 
      if (pos == (int) array->total_devs)
141
 
        pos = 0;
142
 
    }
143
 
 
144
 
  /* Invalid disknr or p */
145
 
  if (bad1 < 0)
146
 
    goto quit;
147
 
 
148
 
  if (bad2 < 0)
149
 
    {
150
 
      /* One bad device */
151
 
      if ((array->device[p]) &&
152
 
          (! grub_disk_read (array->device[p], sector, 0, size, buf)))
153
 
        {
154
 
          grub_raid_block_xor (buf, pbuf, size);
155
 
          goto quit;
156
 
        }
157
 
 
158
 
      if (! array->device[q])
159
 
        {
160
 
          grub_error (GRUB_ERR_READ_ERROR, "not enough disk to restore");
161
 
          goto quit;
162
 
        }
163
 
 
164
 
      grub_errno = GRUB_ERR_NONE;
165
 
      if (grub_disk_read (array->device[q], sector, 0, size, buf))
166
 
        goto quit;
167
 
 
168
 
      grub_raid_block_xor (buf, qbuf, size);
169
 
      grub_raid_block_mul (raid6_table2[255 - bad1][255 - bad1], buf,
170
 
                           size);
171
 
    }
172
 
  else
173
 
    {
174
 
      /* Two bad devices */
175
 
      grub_uint8_t c;
176
 
 
177
 
      if ((! array->device[p]) || (! array->device[q]))
178
 
        {
179
 
          grub_error (GRUB_ERR_READ_ERROR, "not enough disk to restore");
180
 
          goto quit;
181
 
        }
182
 
 
183
 
      if (grub_disk_read (array->device[p], sector, 0, size, buf))
184
 
        goto quit;
185
 
 
186
 
      grub_raid_block_xor (pbuf, buf, size);
187
 
 
188
 
      if (grub_disk_read (array->device[q], sector, 0, size, buf))
189
 
        goto quit;
190
 
 
191
 
      grub_raid_block_xor (qbuf, buf, size);
192
 
 
193
 
      c = raid6_table2[bad2][bad1];
194
 
      grub_raid_block_mul (c, qbuf, size);
195
 
 
196
 
      c = raid6_table1[raid6_table2[bad2][bad2]][c];
197
 
      grub_raid_block_mul (c, pbuf, size);
198
 
 
199
 
      grub_raid_block_xor (pbuf, qbuf, size);
200
 
      grub_memcpy (buf, pbuf, size);
201
 
    }
202
 
 
203
 
quit:
204
 
  grub_free (pbuf);
205
 
  grub_free (qbuf);
206
 
 
207
 
  return grub_errno;
208
 
}
209
 
 
210
 
GRUB_MOD_INIT(raid6rec)
211
 
{
212
 
  grub_raid6_init_table ();
213
 
  grub_raid6_recover_func = grub_raid6_recover;
214
 
}
215
 
 
216
 
GRUB_MOD_FINI(raid6rec)
217
 
{
218
 
  grub_raid6_recover_func = 0;
219
 
}