Some support for external metadata.
[thirdparty/mdadm.git] / restripe.c
CommitLineData
e86c9dd6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2006 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25#include "mdadm.h"
26
27/* To restripe, we read from old geometry to a buffer, and
28 * read from buffer to new geometry.
29 * When reading we don't worry about parity. When writing we do.
30 *
31 */
32
33static int geo_map(int block, unsigned long long stripe, int raid_disks, int level, int layout)
34{
48327135 35 /* On the given stripe, find which disk in the array will have
e86c9dd6 36 * block numbered 'block'.
48327135
NB
37 * '-1' means the parity block.
38 * '-2' means the Q syndrome.
e86c9dd6
NB
39 */
40 int pd;
41
42 switch(level*100 + layout) {
43 case 000:
44 case 400:
45 /* raid 4 isn't messed around by parity blocks */
46 if (block == -1)
47 return raid_disks-1; /* parity block */
48 return block;
49 case 500 + ALGORITHM_LEFT_ASYMMETRIC:
50 pd = (raid_disks-1) - stripe % raid_disks;
51 if (block == -1) return pd;
52 if (block >= pd)
53 block++;
54 return block;
55
56 case 500 + ALGORITHM_RIGHT_ASYMMETRIC:
57 pd = stripe % raid_disks;
58 if (block == -1) return pd;
59 if (block >= pd)
60 block++;
61 return block;
62
63 case 500 + ALGORITHM_LEFT_SYMMETRIC:
64 pd = (raid_disks - 1) - stripe % raid_disks;
65 if (block == -1) return pd;
66 return (pd + 1 + block) % raid_disks;
67
68 case 500 + ALGORITHM_RIGHT_SYMMETRIC:
69 pd = stripe % raid_disks;
70 if (block == -1) return pd;
71 return (pd + 1 + block) % raid_disks;
72
73 case 600 + ALGORITHM_LEFT_ASYMMETRIC:
74 pd = raid_disks - 1 - (stripe % raid_disks);
75 if (block == -1) return pd;
48327135 76 if (block == -2) return (pd+1) % raid_disks;
e86c9dd6
NB
77 if (pd == raid_disks - 1)
78 return block+1;
79 if (block >= pd)
80 return block+2;
81 return block;
82
83 case 600 + ALGORITHM_RIGHT_ASYMMETRIC:
84 pd = stripe % raid_disks;
85 if (block == -1) return pd;
48327135 86 if (block == -2) return (pd+1) % raid_disks;
e86c9dd6
NB
87 if (pd == raid_disks - 1)
88 return block+1;
89 if (block >= pd)
90 return block+2;
91 return block;
92
93 case 600 + ALGORITHM_LEFT_SYMMETRIC:
94 pd = raid_disks - 1 - (stripe % raid_disks);
95 if (block == -1) return pd;
48327135 96 if (block == -2) return (pd+1) % raid_disks;
e86c9dd6
NB
97 return (pd + 2 + block) % raid_disks;
98
99 case 600 + ALGORITHM_RIGHT_SYMMETRIC:
100 pd = stripe % raid_disks;
101 if (block == -1) return pd;
48327135 102 if (block == -2) return (pd+1) % raid_disks;
e86c9dd6
NB
103 return (pd + 2 + block) % raid_disks;
104 }
105 return -1;
106}
107
108
109static void xor_blocks(char *target, char **sources, int disks, int size)
110{
111 int i, j;
112 /* Amazingly inefficient... */
113 for (i=0; i<size; i++) {
114 char c = 0;
115 for (j=0 ; j<disks; j++)
116 c ^= sources[j][i];
117 target[i] = c;
118 }
119}
120
48327135
NB
121static void qsyndrome(char *p, char *q, char **sources, int disks, int size)
122{
123 int d, z;
124 char wq0, wp0, wd0, w10, w20;
125 for ( d = 0; d < size; d++) {
126 wq0 = wp0 = sources[disks-1][d];
127 for ( z = disks-2 ; z >= 0 ; z-- ) {
128 wd0 = sources[z][d];
129 wp0 ^= wd0;
130 w20 = (wq0&0x80) ? 0xff : 0x00;
131 w10 = (wq0 << 1) & 0xff;
132 w20 &= 0x1d;
133 w10 ^= w20;
134 wq0 = w10 ^ wd0;
135 }
136 p[d] = wp0;
137 q[d] = wq0;
138 }
139}
140
e86c9dd6
NB
141/* Save data:
142 * We are given:
143 * A list of 'fds' of the active disks. For now we require all to be present.
48327135 144 * A geometry: raid_disks, chunk_size, level, layout
e86c9dd6
NB
145 * A list of 'fds' for mirrored targets. They are already seeked to
146 * right (Write) location
147 * A start and length
148 */
149
150int save_stripes(int *source, unsigned long long *offsets,
151 int raid_disks, int chunk_size, int level, int layout,
152 int nwrites, int *dest,
153 unsigned long long start, unsigned long long length)
154{
155 char buf[8192];
156 int cpos = start % chunk_size; /* where in chunk we are up to */
157 int len;
158 int data_disks = raid_disks - (level == 0 ? 0 : level <=5 ? 1 : 2);
159 int disk;
160
161 while (length > 0) {
162 unsigned long long offset;
163 int i;
164 len = chunk_size - cpos;
165 if (len > sizeof(buf)) len = sizeof(buf);
166 if (len > length) len = length;
167 /* len bytes to be moved from one device */
168
169 offset = (start/chunk_size/data_disks)*chunk_size + cpos;
170 disk = start/chunk_size % data_disks;
171 disk = geo_map(disk, start/chunk_size/data_disks,
172 raid_disks, level, layout);
173 if (lseek64(source[disk], offsets[disk]+offset, 0) < 0)
174 return -1;
175 if (read(source[disk], buf, len) != len)
176 return -1;
177 for (i=0; i<nwrites; i++)
178 if (write(dest[i], buf, len) != len)
179 return -1;
180 length -= len;
181 start += len;
182 cpos += len;
183 while (cpos >= chunk_size) cpos -= chunk_size;
184 }
185 return 0;
186}
187
188/* Restore data:
189 * We are given:
190 * A list of 'fds' of the active disks. Some may be '-1' for not-available.
353632d9 191 * A geometry: raid_disks, chunk_size, level, layout
e86c9dd6
NB
192 * An 'fd' to read from. It is already seeked to the right (Read) location.
193 * A start and length.
194 * The length must be a multiple of the stripe size.
195 *
196 * We build a full stripe in memory and then write it out.
197 * We assume that there are enough working devices.
198 */
199int restore_stripes(int *dest, unsigned long long *offsets,
200 int raid_disks, int chunk_size, int level, int layout,
353632d9 201 int source, unsigned long long read_offset,
e86c9dd6
NB
202 unsigned long long start, unsigned long long length)
203{
204 char *stripe_buf = malloc(raid_disks * chunk_size);
205 char **stripes = malloc(raid_disks * sizeof(char*));
206 char **blocks = malloc(raid_disks * sizeof(char*));
207 int i;
208
209 int data_disks = raid_disks - (level == 0 ? 0 : level <=5 ? 1 : 2);
210
211 if (stripe_buf == NULL || stripes == NULL || blocks == NULL) {
212 free(stripe_buf);
213 free(stripes);
214 free(blocks);
215 return -2;
216 }
217 for (i=0; i<raid_disks; i++)
218 stripes[i] = stripe_buf + i * chunk_size;
219 while (length > 0) {
220 int len = data_disks * chunk_size;
221 unsigned long long offset;
48327135 222 int disk, qdisk;
e86c9dd6
NB
223 if (length < len)
224 return -3;
225 for (i=0; i < data_disks; i++) {
226 int disk = geo_map(i, start/chunk_size/data_disks,
227 raid_disks, level, layout);
228 blocks[i] = stripes[disk];
353632d9
NB
229 if (lseek64(source, read_offset, 0) != read_offset)
230 return -1;
e86c9dd6
NB
231 if (read(source, stripes[disk], chunk_size) != chunk_size)
232 return -1;
353632d9 233 read_offset += chunk_size;
e86c9dd6
NB
234 }
235 /* We have the data, now do the parity */
236 offset = (start/chunk_size/data_disks) * chunk_size;
48327135
NB
237 switch (level) {
238 case 4:
239 case 5:
240 disk = geo_map(-1, start/chunk_size/data_disks,
e86c9dd6
NB
241 raid_disks, level, layout);
242 xor_blocks(stripes[disk], blocks, data_disks, chunk_size);
48327135
NB
243 break;
244 case 6:
245 disk = geo_map(-1, start/chunk_size/data_disks,
246 raid_disks, level, layout);
247 qdisk = geo_map(-2, start/chunk_size/data_disks,
248 raid_disks, level, layout);
249
250 qsyndrome(stripes[disk], stripes[qdisk], blocks,
251 data_disks, chunk_size);
252 break;
e86c9dd6
NB
253 }
254 for (i=0; i < raid_disks ; i++)
255 if (dest[i] >= 0) {
256 if (lseek64(dest[i], offsets[i]+offset, 0) < 0)
257 return -1;
258 if (write(dest[i], stripes[i], chunk_size) != chunk_size)
259 return -1;
260 }
261 length -= len;
262 start += len;
263 }
264 return 0;
265}
266
267#ifdef MAIN
268
48327135
NB
269int test_stripes(int *source, unsigned long long *offsets,
270 int raid_disks, int chunk_size, int level, int layout,
271 unsigned long long start, unsigned long long length)
272{
273 /* ready the data and p (and q) blocks, and check we got them right */
274 char *stripe_buf = malloc(raid_disks * chunk_size);
275 char **stripes = malloc(raid_disks * sizeof(char*));
276 char **blocks = malloc(raid_disks * sizeof(char*));
277 char *p = malloc(chunk_size);
278 char *q = malloc(chunk_size);
279
280 int i;
281 int data_disks = raid_disks - (level == 5 ? 1: 2);
282 for ( i = 0 ; i < raid_disks ; i++)
283 stripes[i] = stripe_buf + i * chunk_size;
284
285 while (length > 0) {
286 int disk;
287
288 for (i = 0 ; i < raid_disks ; i++) {
289 lseek64(source[i], offsets[i]+start, 0);
290 read(source[i], stripes[i], chunk_size);
291 }
292 for (i = 0 ; i < data_disks ; i++) {
293 int disk = geo_map(i, start/chunk_size, raid_disks,
294 level, layout);
295 blocks[i] = stripes[disk];
296 printf("%d->%d\n", i, disk);
297 }
298 switch(level) {
299 case 6:
300 qsyndrome(p, q, blocks, data_disks, chunk_size);
301 disk = geo_map(-1, start/chunk_size, raid_disks,
302 level, layout);
303 if (memcmp(p, stripes[disk], chunk_size) != 0) {
304 printf("P(%d) wrong at %llu\n", disk,
305 start / chunk_size);
306 }
307 disk = geo_map(-2, start/chunk_size, raid_disks,
308 level, layout);
309 if (memcmp(q, stripes[disk], chunk_size) != 0) {
310 printf("Q(%d) wrong at %llu\n", disk,
311 start / chunk_size);
312 }
313 break;
314 }
315 length -= chunk_size;
316 start += chunk_size;
317 }
318 return 0;
319}
320
e86c9dd6
NB
321unsigned long long getnum(char *str, char **err)
322{
323 char *e;
324 unsigned long long rv = strtoull(str, &e, 10);
325 if (e==str || *e) {
326 *err = str;
327 return 0;
328 }
329 return rv;
330}
331
332main(int argc, char *argv[])
333{
334 /* save/restore file raid_disks chunk_size level layout start length devices...
335 */
336 int save;
337 int *fds;
338 char *file;
339 int storefd;
340 unsigned long long *offsets;
341 int raid_disks, chunk_size, level, layout;
342 unsigned long long start, length;
343 int i;
344
345 char *err = NULL;
346 if (argc < 10) {
347 fprintf(stderr, "Usage: test_stripe save/restore file raid_disks"
348 " chunk_size level layout start length devices...\n");
349 exit(1);
350 }
351 if (strcmp(argv[1], "save")==0)
352 save = 1;
353 else if (strcmp(argv[1], "restore") == 0)
354 save = 0;
48327135
NB
355 else if (strcmp(argv[1], "test") == 0)
356 save = 2;
e86c9dd6
NB
357 else {
358 fprintf(stderr, "test_stripe: must give 'save' or 'restore'.\n");
359 exit(2);
360 }
361
362 file = argv[2];
363 raid_disks = getnum(argv[3], &err);
364 chunk_size = getnum(argv[4], &err);
365 level = getnum(argv[5], &err);
366 layout = getnum(argv[6], &err);
367 start = getnum(argv[7], &err);
368 length = getnum(argv[8], &err);
369 if (err) {
370 fprintf(stderr, "test_stripe: Bad number: %s\n", err);
371 exit(2);
372 }
373 if (argc != raid_disks + 9) {
374 fprintf(stderr, "test_stripe: wrong number of devices: want %d found %d\n",
375 raid_disks, argc-9);
376 exit(2);
377 }
378 fds = malloc(raid_disks * sizeof(*fds));
379 offsets = malloc(raid_disks * sizeof(*offsets));
380 memset(offsets, 0, raid_disks * sizeof(*offsets));
381
382 storefd = open(file, O_RDWR);
383 if (storefd < 0) {
384 perror(file);
385 fprintf(stderr, "test_stripe: could not open %s.\n", file);
386 exit(3);
387 }
388 for (i=0; i<raid_disks; i++) {
389 fds[i] = open(argv[9+i], O_RDWR);
390 if (fds[i] < 0) {
391 perror(argv[9+i]);
392 fprintf(stderr,"test_stripe: cannot open %s.\n", argv[9+i]);
393 exit(3);
394 }
395 }
396
48327135 397 if (save == 1) {
e86c9dd6
NB
398 int rv = save_stripes(fds, offsets,
399 raid_disks, chunk_size, level, layout,
400 1, &storefd,
401 start, length);
402 if (rv != 0) {
48327135
NB
403 fprintf(stderr,
404 "test_stripe: save_stripes returned %d\n", rv);
405 exit(1);
406 }
407 } else if (save == 2) {
408 int rv = test_stripes(fds, offsets,
409 raid_disks, chunk_size, level, layout,
410 start, length);
411 if (rv != 0) {
412 fprintf(stderr,
413 "test_stripe: test_stripes returned %d\n", rv);
e86c9dd6
NB
414 exit(1);
415 }
416 } else {
417 int rv = restore_stripes(fds, offsets,
418 raid_disks, chunk_size, level, layout,
353632d9 419 storefd, 0ULL,
e86c9dd6
NB
420 start, length);
421 if (rv != 0) {
48327135
NB
422 fprintf(stderr,
423 "test_stripe: restore_stripes returned %d\n",
424 rv);
e86c9dd6
NB
425 exit(1);
426 }
427 }
428 exit(0);
429}
430
431#endif /* MAIN */