]> git.ipfire.org Git - thirdparty/mdadm.git/blob - restripe.c
f579a92c87440f85fb69cbf18a4dba45ae6df8fb
[thirdparty/mdadm.git] / restripe.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2006-2009 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 #include <stdint.h>
27
28 /* To restripe, we read from old geometry to a buffer, and
29 * read from buffer to new geometry.
30 * When reading, we might have missing devices and so could need
31 * to reconstruct.
32 * When writing, we need to create correct parity and Q.
33 *
34 */
35
36 int geo_map(int block, unsigned long long stripe, int raid_disks,
37 int level, int layout)
38 {
39 /* On the given stripe, find which disk in the array will have
40 * block numbered 'block'.
41 * '-1' means the parity block.
42 * '-2' means the Q syndrome.
43 */
44 int pd;
45
46 /* layout is not relevant for raid0 and raid4 */
47 if ((level == 0) ||
48 (level == 4))
49 layout = 0;
50
51 switch(level*100 + layout) {
52 case 000:
53 case 400:
54 case 500 + ALGORITHM_PARITY_N:
55 /* raid 4 isn't messed around by parity blocks */
56 if (block == -1)
57 return raid_disks-1; /* parity block */
58 return block;
59 case 500 + ALGORITHM_LEFT_ASYMMETRIC:
60 pd = (raid_disks-1) - stripe % raid_disks;
61 if (block == -1) return pd;
62 if (block >= pd)
63 block++;
64 return block;
65
66 case 500 + ALGORITHM_RIGHT_ASYMMETRIC:
67 pd = stripe % raid_disks;
68 if (block == -1) return pd;
69 if (block >= pd)
70 block++;
71 return block;
72
73 case 500 + ALGORITHM_LEFT_SYMMETRIC:
74 pd = (raid_disks - 1) - stripe % raid_disks;
75 if (block == -1) return pd;
76 return (pd + 1 + block) % raid_disks;
77
78 case 500 + ALGORITHM_RIGHT_SYMMETRIC:
79 pd = stripe % raid_disks;
80 if (block == -1) return pd;
81 return (pd + 1 + block) % raid_disks;
82
83 case 500 + ALGORITHM_PARITY_0:
84 return block + 1;
85
86
87 case 600 + ALGORITHM_PARITY_N_6:
88 if (block == -2)
89 return raid_disks - 1;
90 if (block == -1)
91 return raid_disks - 2; /* parity block */
92 return block;
93 case 600 + ALGORITHM_LEFT_ASYMMETRIC_6:
94 if (block == -2)
95 return raid_disks - 1;
96 raid_disks--;
97 pd = (raid_disks-1) - stripe % raid_disks;
98 if (block == -1) return pd;
99 if (block >= pd)
100 block++;
101 return block;
102
103 case 600 + ALGORITHM_RIGHT_ASYMMETRIC_6:
104 if (block == -2)
105 return raid_disks - 1;
106 raid_disks--;
107 pd = stripe % raid_disks;
108 if (block == -1) return pd;
109 if (block >= pd)
110 block++;
111 return block;
112
113 case 600 + ALGORITHM_LEFT_SYMMETRIC_6:
114 if (block == -2)
115 return raid_disks - 1;
116 raid_disks--;
117 pd = (raid_disks - 1) - stripe % raid_disks;
118 if (block == -1) return pd;
119 return (pd + 1 + block) % raid_disks;
120
121 case 600 + ALGORITHM_RIGHT_SYMMETRIC_6:
122 if (block == -2)
123 return raid_disks - 1;
124 raid_disks--;
125 pd = stripe % raid_disks;
126 if (block == -1) return pd;
127 return (pd + 1 + block) % raid_disks;
128
129 case 600 + ALGORITHM_PARITY_0_6:
130 if (block == -2)
131 return raid_disks - 1;
132 return block + 1;
133
134
135 case 600 + ALGORITHM_PARITY_0:
136 if (block == -1)
137 return 0;
138 if (block == -2)
139 return 1;
140 return block + 2;
141
142 case 600 + ALGORITHM_LEFT_ASYMMETRIC:
143 pd = raid_disks - 1 - (stripe % raid_disks);
144 if (block == -1) return pd;
145 if (block == -2) return (pd+1) % raid_disks;
146 if (pd == raid_disks - 1)
147 return block+1;
148 if (block >= pd)
149 return block+2;
150 return block;
151
152 case 600 + ALGORITHM_ROTATING_ZERO_RESTART:
153 /* Different order for calculating Q, otherwize same as ... */
154 case 600 + ALGORITHM_RIGHT_ASYMMETRIC:
155 pd = stripe % raid_disks;
156 if (block == -1) return pd;
157 if (block == -2) return (pd+1) % raid_disks;
158 if (pd == raid_disks - 1)
159 return block+1;
160 if (block >= pd)
161 return block+2;
162 return block;
163
164 case 600 + ALGORITHM_LEFT_SYMMETRIC:
165 pd = raid_disks - 1 - (stripe % raid_disks);
166 if (block == -1) return pd;
167 if (block == -2) return (pd+1) % raid_disks;
168 return (pd + 2 + block) % raid_disks;
169
170 case 600 + ALGORITHM_RIGHT_SYMMETRIC:
171 pd = stripe % raid_disks;
172 if (block == -1) return pd;
173 if (block == -2) return (pd+1) % raid_disks;
174 return (pd + 2 + block) % raid_disks;
175
176
177 case 600 + ALGORITHM_ROTATING_N_RESTART:
178 /* Same a left_asymmetric, by first stripe is
179 * D D D P Q rather than
180 * Q D D D P
181 */
182 pd = raid_disks - 1 - ((stripe + 1) % raid_disks);
183 if (block == -1) return pd;
184 if (block == -2) return (pd+1) % raid_disks;
185 if (pd == raid_disks - 1)
186 return block+1;
187 if (block >= pd)
188 return block+2;
189 return block;
190
191 case 600 + ALGORITHM_ROTATING_N_CONTINUE:
192 /* Same as left_symmetric but Q is before P */
193 pd = raid_disks - 1 - (stripe % raid_disks);
194 if (block == -1) return pd;
195 if (block == -2) return (pd+raid_disks-1) % raid_disks;
196 return (pd + 1 + block) % raid_disks;
197 }
198 return -1;
199 }
200 static int is_ddf(int layout)
201 {
202 switch (layout)
203 {
204 default:
205 return 0;
206 case ALGORITHM_ROTATING_N_CONTINUE:
207 case ALGORITHM_ROTATING_N_RESTART:
208 case ALGORITHM_ROTATING_ZERO_RESTART:
209 return 1;
210 }
211 }
212
213
214 static void xor_blocks(char *target, char **sources, int disks, int size)
215 {
216 int i, j;
217 /* Amazingly inefficient... */
218 for (i=0; i<size; i++) {
219 char c = 0;
220 for (j=0 ; j<disks; j++)
221 c ^= sources[j][i];
222 target[i] = c;
223 }
224 }
225
226 void qsyndrome(uint8_t *p, uint8_t *q, uint8_t **sources, int disks, int size)
227 {
228 int d, z;
229 uint8_t wq0, wp0, wd0, w10, w20;
230 for ( d = 0; d < size; d++) {
231 wq0 = wp0 = sources[disks-1][d];
232 for ( z = disks-2 ; z >= 0 ; z-- ) {
233 wd0 = sources[z][d];
234 wp0 ^= wd0;
235 w20 = (wq0&0x80) ? 0xff : 0x00;
236 w10 = (wq0 << 1) & 0xff;
237 w20 &= 0x1d;
238 w10 ^= w20;
239 wq0 = w10 ^ wd0;
240 }
241 p[d] = wp0;
242 q[d] = wq0;
243 }
244 }
245
246
247 /*
248 * The following was taken from linux/drivers/md/mktables.c, and modified
249 * to create in-memory tables rather than C code
250 */
251 static uint8_t gfmul(uint8_t a, uint8_t b)
252 {
253 uint8_t v = 0;
254
255 while (b) {
256 if (b & 1)
257 v ^= a;
258 a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
259 b >>= 1;
260 }
261
262 return v;
263 }
264
265 static uint8_t gfpow(uint8_t a, int b)
266 {
267 uint8_t v = 1;
268
269 b %= 255;
270 if (b < 0)
271 b += 255;
272
273 while (b) {
274 if (b & 1)
275 v = gfmul(v, a);
276 a = gfmul(a, a);
277 b >>= 1;
278 }
279
280 return v;
281 }
282
283 int tables_ready = 0;
284 uint8_t raid6_gfmul[256][256];
285 uint8_t raid6_gfexp[256];
286 uint8_t raid6_gfinv[256];
287 uint8_t raid6_gfexi[256];
288 uint8_t raid6_gflog[256];
289 uint8_t raid6_gfilog[256];
290 void make_tables(void)
291 {
292 int i, j;
293 uint8_t v;
294 uint32_t b, log;
295
296 /* Compute multiplication table */
297 for (i = 0; i < 256; i++)
298 for (j = 0; j < 256; j++)
299 raid6_gfmul[i][j] = gfmul(i, j);
300
301 /* Compute power-of-2 table (exponent) */
302 v = 1;
303 for (i = 0; i < 256; i++) {
304 raid6_gfexp[i] = v;
305 v = gfmul(v, 2);
306 if (v == 1)
307 v = 0; /* For entry 255, not a real entry */
308 }
309
310 /* Compute inverse table x^-1 == x^254 */
311 for (i = 0; i < 256; i++)
312 raid6_gfinv[i] = gfpow(i, 254);
313
314 /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
315 for (i = 0; i < 256; i ++)
316 raid6_gfexi[i] = raid6_gfinv[raid6_gfexp[i] ^ 1];
317
318 /* Compute log and inverse log */
319 /* Modified code from:
320 * http://web.eecs.utk.edu/~plank/plank/papers/CS-96-332.html
321 */
322 b = 1;
323 raid6_gflog[0] = 0;
324 raid6_gfilog[255] = 0;
325
326 for (log = 0; log < 255; log++) {
327 raid6_gflog[b] = (uint8_t) log;
328 raid6_gfilog[log] = (uint8_t) b;
329 b = b << 1;
330 if (b & 256) b = b ^ 0435;
331 }
332
333 tables_ready = 1;
334 }
335
336 uint8_t *zero;
337 /* Following was taken from linux/drivers/md/raid6recov.c */
338
339 /* Recover two failed data blocks. */
340 void raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
341 uint8_t **ptrs)
342 {
343 uint8_t *p, *q, *dp, *dq;
344 uint8_t px, qx, db;
345 const uint8_t *pbmul; /* P multiplier table for B data */
346 const uint8_t *qmul; /* Q multiplier table (for both) */
347
348 p = ptrs[disks-2];
349 q = ptrs[disks-1];
350
351 /* Compute syndrome with zero for the missing data pages
352 Use the dead data pages as temporary storage for
353 delta p and delta q */
354 dp = ptrs[faila];
355 ptrs[faila] = zero;
356 dq = ptrs[failb];
357 ptrs[failb] = zero;
358
359 qsyndrome(dp, dq, ptrs, disks-2, bytes);
360
361 /* Restore pointer table */
362 ptrs[faila] = dp;
363 ptrs[failb] = dq;
364
365 /* Now, pick the proper data tables */
366 pbmul = raid6_gfmul[raid6_gfexi[failb-faila]];
367 qmul = raid6_gfmul[raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]]];
368
369 /* Now do it... */
370 while ( bytes-- ) {
371 px = *p ^ *dp;
372 qx = qmul[*q ^ *dq];
373 *dq++ = db = pbmul[px] ^ qx; /* Reconstructed B */
374 *dp++ = db ^ px; /* Reconstructed A */
375 p++; q++;
376 }
377 }
378
379 /* Recover failure of one data block plus the P block */
380 void raid6_datap_recov(int disks, size_t bytes, int faila, uint8_t **ptrs)
381 {
382 uint8_t *p, *q, *dq;
383 const uint8_t *qmul; /* Q multiplier table */
384
385 p = ptrs[disks-2];
386 q = ptrs[disks-1];
387
388 /* Compute syndrome with zero for the missing data page
389 Use the dead data page as temporary storage for delta q */
390 dq = ptrs[faila];
391 ptrs[faila] = zero;
392
393 qsyndrome(p, dq, ptrs, disks-2, bytes);
394
395 /* Restore pointer table */
396 ptrs[faila] = dq;
397
398 /* Now, pick the proper data tables */
399 qmul = raid6_gfmul[raid6_gfinv[raid6_gfexp[faila]]];
400
401 /* Now do it... */
402 while ( bytes-- ) {
403 *p++ ^= *dq = qmul[*q ^ *dq];
404 q++; dq++;
405 }
406 }
407
408 /* Try to find out if a specific disk has a problem */
409 int raid6_check_disks(int data_disks, int start, int chunk_size,
410 int level, int layout, int diskP, int diskQ,
411 char *p, char *q, char **stripes)
412 {
413 int i;
414 int data_id, diskD;
415 uint8_t Px, Qx;
416 int curr_broken_disk = -1;
417 int prev_broken_disk = -1;
418 int broken_status = 0;
419
420 for(i = 0; i < chunk_size; i++) {
421 Px = (uint8_t)stripes[diskP][i] ^ (uint8_t)p[i];
422 Qx = (uint8_t)stripes[diskQ][i] ^ (uint8_t)q[i];
423
424 if((Px != 0) && (Qx == 0))
425 curr_broken_disk = diskP;
426
427
428 if((Px == 0) && (Qx != 0))
429 curr_broken_disk = diskQ;
430
431
432 if((Px != 0) && (Qx != 0)) {
433 data_id = (raid6_gflog[Qx] - raid6_gflog[Px]);
434 if(data_id < 0) data_id += 255;
435 diskD = geo_map(data_id, start/chunk_size,
436 data_disks + 2, level, layout);
437 curr_broken_disk = diskD;
438 }
439
440 if((Px == 0) && (Qx == 0))
441 curr_broken_disk = curr_broken_disk;
442
443 if(curr_broken_disk >= data_disks + 2)
444 broken_status = 2;
445
446 switch(broken_status) {
447 case 0:
448 if(curr_broken_disk != -1) {
449 prev_broken_disk = curr_broken_disk;
450 broken_status = 1;
451 }
452 break;
453
454 case 1:
455 if(curr_broken_disk != prev_broken_disk)
456 broken_status = 2;
457 break;
458
459 case 2:
460 default:
461 curr_broken_disk = prev_broken_disk = -2;
462 break;
463 }
464 }
465
466 return curr_broken_disk;
467 }
468
469 /* Save data:
470 * We are given:
471 * A list of 'fds' of the active disks. Some may be absent.
472 * A geometry: raid_disks, chunk_size, level, layout
473 * A list of 'fds' for mirrored targets. They are already seeked to
474 * right (Write) location
475 * A start and length which must be stripe-aligned
476 * 'buf' is large enough to hold one stripe, and is aligned
477 */
478
479 int save_stripes(int *source, unsigned long long *offsets,
480 int raid_disks, int chunk_size, int level, int layout,
481 int nwrites, int *dest,
482 unsigned long long start, unsigned long long length,
483 char *buf)
484 {
485 int len;
486 int data_disks = raid_disks - (level == 0 ? 0 : level <=5 ? 1 : 2);
487 int disk;
488 int i;
489
490 if (!tables_ready)
491 make_tables();
492
493 if (zero == NULL) {
494 zero = malloc(chunk_size);
495 memset(zero, 0, chunk_size);
496 }
497
498 len = data_disks * chunk_size;
499 while (length > 0) {
500 int failed = 0;
501 int fdisk[3], fblock[3];
502 for (disk = 0; disk < raid_disks ; disk++) {
503 unsigned long long offset;
504 int dnum;
505
506 offset = (start/chunk_size/data_disks)*chunk_size;
507 dnum = geo_map(disk < data_disks ? disk : data_disks - disk - 1,
508 start/chunk_size/data_disks,
509 raid_disks, level, layout);
510 if (dnum < 0) abort();
511 if (source[dnum] < 0 ||
512 lseek64(source[dnum], offsets[dnum]+offset, 0) < 0 ||
513 read(source[dnum], buf+disk * chunk_size, chunk_size)
514 != chunk_size)
515 if (failed <= 2) {
516 fdisk[failed] = dnum;
517 fblock[failed] = disk;
518 failed++;
519 }
520 }
521 if (failed == 0 || fblock[0] >= data_disks)
522 /* all data disks are good */
523 ;
524 else if (failed == 1 || fblock[1] >= data_disks+1) {
525 /* one failed data disk and good parity */
526 char *bufs[data_disks];
527 for (i=0; i < data_disks; i++)
528 if (fblock[0] == i)
529 bufs[i] = buf + data_disks*chunk_size;
530 else
531 bufs[i] = buf + i*chunk_size;
532
533 xor_blocks(buf + fblock[0]*chunk_size,
534 bufs, data_disks, chunk_size);
535 } else if (failed > 2 || level != 6)
536 /* too much failure */
537 return -1;
538 else {
539 /* RAID6 computations needed. */
540 uint8_t *bufs[data_disks+4];
541 int qdisk;
542 int syndrome_disks;
543 disk = geo_map(-1, start/chunk_size/data_disks,
544 raid_disks, level, layout);
545 qdisk = geo_map(-2, start/chunk_size/data_disks,
546 raid_disks, level, layout);
547 if (is_ddf(layout)) {
548 /* q over 'raid_disks' blocks, in device order.
549 * 'p' and 'q' get to be all zero
550 */
551 for (i = 0; i < raid_disks; i++)
552 bufs[i] = zero;
553 for (i = 0; i < data_disks; i++) {
554 int dnum = geo_map(i,
555 start/chunk_size/data_disks,
556 raid_disks, level, layout);
557 int snum;
558 /* i is the logical block number, so is index to 'buf'.
559 * dnum is physical disk number
560 * and thus the syndrome number.
561 */
562 snum = dnum;
563 bufs[snum] = (uint8_t*)buf + chunk_size * i;
564 }
565 syndrome_disks = raid_disks;
566 } else {
567 /* for md, q is over 'data_disks' blocks,
568 * starting immediately after 'q'
569 * Note that for the '_6' variety, the p block
570 * makes a hole that we need to be careful of.
571 */
572 int j;
573 int snum = 0;
574 for (j = 0; j < raid_disks; j++) {
575 int dnum = (qdisk + 1 + j) % raid_disks;
576 if (dnum == disk || dnum == qdisk)
577 continue;
578 for (i = 0; i < data_disks; i++)
579 if (geo_map(i,
580 start/chunk_size/data_disks,
581 raid_disks, level, layout) == dnum)
582 break;
583 /* i is the logical block number, so is index to 'buf'.
584 * dnum is physical disk number
585 * snum is syndrome disk for which 0 is immediately after Q
586 */
587 bufs[snum] = (uint8_t*)buf + chunk_size * i;
588
589 if (fblock[0] == i)
590 fdisk[0] = snum;
591 if (fblock[1] == i)
592 fdisk[1] = snum;
593 snum++;
594 }
595
596 syndrome_disks = data_disks;
597 }
598
599 /* Place P and Q blocks at end of bufs */
600 bufs[syndrome_disks] = (uint8_t*)buf + chunk_size * data_disks;
601 bufs[syndrome_disks+1] = (uint8_t*)buf + chunk_size * (data_disks+1);
602
603 if (fblock[1] == data_disks)
604 /* One data failed, and parity failed */
605 raid6_datap_recov(syndrome_disks+2, chunk_size,
606 fdisk[0], bufs);
607 else {
608 if (fdisk[0] > fdisk[1]) {
609 int t = fdisk[0];
610 fdisk[0] = fdisk[1];
611 fdisk[1] = t;
612 }
613 /* Two data blocks failed, P,Q OK */
614 raid6_2data_recov(syndrome_disks+2, chunk_size,
615 fdisk[0], fdisk[1], bufs);
616 }
617 }
618
619 for (i=0; i<nwrites; i++)
620 if (write(dest[i], buf, len) != len)
621 return -1;
622
623 length -= len;
624 start += len;
625 }
626 return 0;
627 }
628
629 /* Restore data:
630 * We are given:
631 * A list of 'fds' of the active disks. Some may be '-1' for not-available.
632 * A geometry: raid_disks, chunk_size, level, layout
633 * An 'fd' to read from. It is already seeked to the right (Read) location.
634 * A start and length.
635 * The length must be a multiple of the stripe size.
636 *
637 * We build a full stripe in memory and then write it out.
638 * We assume that there are enough working devices.
639 */
640 int restore_stripes(int *dest, unsigned long long *offsets,
641 int raid_disks, int chunk_size, int level, int layout,
642 int source, unsigned long long read_offset,
643 unsigned long long start, unsigned long long length)
644 {
645 char *stripe_buf;
646 char **stripes = malloc(raid_disks * sizeof(char*));
647 char **blocks = malloc(raid_disks * sizeof(char*));
648 int i;
649
650 int data_disks = raid_disks - (level == 0 ? 0 : level <= 5 ? 1 : 2);
651
652 if (posix_memalign((void**)&stripe_buf, 4096, raid_disks * chunk_size))
653 stripe_buf = NULL;
654 if (zero == NULL) {
655 zero = malloc(chunk_size);
656 if (zero)
657 memset(zero, 0, chunk_size);
658 }
659 if (stripe_buf == NULL || stripes == NULL || blocks == NULL
660 || zero == NULL) {
661 free(stripe_buf);
662 free(stripes);
663 free(blocks);
664 free(zero);
665 return -2;
666 }
667 for (i=0; i<raid_disks; i++)
668 stripes[i] = stripe_buf + i * chunk_size;
669 while (length > 0) {
670 unsigned int len = data_disks * chunk_size;
671 unsigned long long offset;
672 int disk, qdisk;
673 int syndrome_disks;
674 if (length < len)
675 return -3;
676 for (i=0; i < data_disks; i++) {
677 int disk = geo_map(i, start/chunk_size/data_disks,
678 raid_disks, level, layout);
679 if ((unsigned long long)lseek64(source, read_offset, 0)
680 != read_offset)
681 return -1;
682 if (read(source, stripes[disk],
683 chunk_size) != chunk_size)
684 return -1;
685 read_offset += chunk_size;
686 }
687 /* We have the data, now do the parity */
688 offset = (start/chunk_size/data_disks) * chunk_size;
689 switch (level) {
690 case 4:
691 case 5:
692 disk = geo_map(-1, start/chunk_size/data_disks,
693 raid_disks, level, layout);
694 for (i = 0; i < data_disks; i++)
695 blocks[i] = stripes[(disk+1+i) % raid_disks];
696 xor_blocks(stripes[disk], blocks, data_disks, chunk_size);
697 break;
698 case 6:
699 disk = geo_map(-1, start/chunk_size/data_disks,
700 raid_disks, level, layout);
701 qdisk = geo_map(-2, start/chunk_size/data_disks,
702 raid_disks, level, layout);
703 if (is_ddf(layout)) {
704 /* q over 'raid_disks' blocks, in device order.
705 * 'p' and 'q' get to be all zero
706 */
707 for (i = 0; i < raid_disks; i++)
708 if (i == disk || i == qdisk)
709 blocks[i] = (char*)zero;
710 else
711 blocks[i] = stripes[i];
712 syndrome_disks = raid_disks;
713 } else {
714 /* for md, q is over 'data_disks' blocks,
715 * starting immediately after 'q'
716 */
717 for (i = 0; i < data_disks; i++)
718 blocks[i] = stripes[(qdisk+1+i) % raid_disks];
719
720 syndrome_disks = data_disks;
721 }
722 qsyndrome((uint8_t*)stripes[disk],
723 (uint8_t*)stripes[qdisk],
724 (uint8_t**)blocks,
725 syndrome_disks, chunk_size);
726 break;
727 }
728 for (i=0; i < raid_disks ; i++)
729 if (dest[i] >= 0) {
730 if (lseek64(dest[i], offsets[i]+offset, 0) < 0)
731 return -1;
732 if (write(dest[i], stripes[i], chunk_size) != chunk_size)
733 return -1;
734 }
735 length -= len;
736 start += len;
737 }
738 return 0;
739 }
740
741 #ifdef MAIN
742
743 int test_stripes(int *source, unsigned long long *offsets,
744 int raid_disks, int chunk_size, int level, int layout,
745 unsigned long long start, unsigned long long length)
746 {
747 /* ready the data and p (and q) blocks, and check we got them right */
748 char *stripe_buf = malloc(raid_disks * chunk_size);
749 char **stripes = malloc(raid_disks * sizeof(char*));
750 char **blocks = malloc(raid_disks * sizeof(char*));
751 char *p = malloc(chunk_size);
752 char *q = malloc(chunk_size);
753
754 int i;
755 int diskP, diskQ;
756 int data_disks = raid_disks - (level == 5 ? 1: 2);
757
758 if (!tables_ready)
759 make_tables();
760
761 for ( i = 0 ; i < raid_disks ; i++)
762 stripes[i] = stripe_buf + i * chunk_size;
763
764 while (length > 0) {
765 int disk;
766
767 for (i = 0 ; i < raid_disks ; i++) {
768 lseek64(source[i], offsets[i]+start, 0);
769 read(source[i], stripes[i], chunk_size);
770 }
771 for (i = 0 ; i < data_disks ; i++) {
772 int disk = geo_map(i, start/chunk_size, raid_disks,
773 level, layout);
774 blocks[i] = stripes[disk];
775 printf("%d->%d\n", i, disk);
776 }
777 switch(level) {
778 case 6:
779 qsyndrome(p, q, (uint8_t**)blocks, data_disks, chunk_size);
780 diskP = geo_map(-1, start/chunk_size, raid_disks,
781 level, layout);
782 if (memcmp(p, stripes[diskP], chunk_size) != 0) {
783 printf("P(%d) wrong at %llu\n", diskP,
784 start / chunk_size);
785 }
786 diskQ = geo_map(-2, start/chunk_size, raid_disks,
787 level, layout);
788 if (memcmp(q, stripes[diskQ], chunk_size) != 0) {
789 printf("Q(%d) wrong at %llu\n", diskQ,
790 start / chunk_size);
791 }
792 disk = raid6_check_disks(data_disks, start, chunk_size,
793 level, layout, diskP, diskQ,
794 p, q, stripes);
795 if(disk >= 0) {
796 printf("Possible failed disk: %d\n", disk);
797 }
798 if(disk == -2) {
799 printf("Failure detected, but disk unknown\n");
800 }
801 break;
802 }
803 length -= chunk_size;
804 start += chunk_size;
805 }
806 return 0;
807 }
808
809 unsigned long long getnum(char *str, char **err)
810 {
811 char *e;
812 unsigned long long rv = strtoull(str, &e, 10);
813 if (e==str || *e) {
814 *err = str;
815 return 0;
816 }
817 return rv;
818 }
819
820 main(int argc, char *argv[])
821 {
822 /* save/restore file raid_disks chunk_size level layout start length devices...
823 */
824 int save;
825 int *fds;
826 char *file;
827 char *buf;
828 int storefd;
829 unsigned long long *offsets;
830 int raid_disks, chunk_size, level, layout;
831 unsigned long long start, length;
832 int i;
833
834 char *err = NULL;
835 if (argc < 10) {
836 fprintf(stderr, "Usage: test_stripe save/restore file raid_disks"
837 " chunk_size level layout start length devices...\n");
838 exit(1);
839 }
840 if (strcmp(argv[1], "save")==0)
841 save = 1;
842 else if (strcmp(argv[1], "restore") == 0)
843 save = 0;
844 else if (strcmp(argv[1], "test") == 0)
845 save = 2;
846 else {
847 fprintf(stderr, "test_stripe: must give 'save' or 'restore'.\n");
848 exit(2);
849 }
850
851 file = argv[2];
852 raid_disks = getnum(argv[3], &err);
853 chunk_size = getnum(argv[4], &err);
854 level = getnum(argv[5], &err);
855 layout = getnum(argv[6], &err);
856 start = getnum(argv[7], &err);
857 length = getnum(argv[8], &err);
858 if (err) {
859 fprintf(stderr, "test_stripe: Bad number: %s\n", err);
860 exit(2);
861 }
862 if (argc != raid_disks + 9) {
863 fprintf(stderr, "test_stripe: wrong number of devices: want %d found %d\n",
864 raid_disks, argc-9);
865 exit(2);
866 }
867 fds = malloc(raid_disks * sizeof(*fds));
868 offsets = malloc(raid_disks * sizeof(*offsets));
869 memset(offsets, 0, raid_disks * sizeof(*offsets));
870
871 storefd = open(file, O_RDWR);
872 if (storefd < 0) {
873 perror(file);
874 fprintf(stderr, "test_stripe: could not open %s.\n", file);
875 exit(3);
876 }
877 for (i=0; i<raid_disks; i++) {
878 fds[i] = open(argv[9+i], O_RDWR);
879 if (fds[i] < 0) {
880 perror(argv[9+i]);
881 fprintf(stderr,"test_stripe: cannot open %s.\n", argv[9+i]);
882 exit(3);
883 }
884 }
885
886 buf = malloc(raid_disks * chunk_size);
887
888 if (save == 1) {
889 int rv = save_stripes(fds, offsets,
890 raid_disks, chunk_size, level, layout,
891 1, &storefd,
892 start, length, buf);
893 if (rv != 0) {
894 fprintf(stderr,
895 "test_stripe: save_stripes returned %d\n", rv);
896 exit(1);
897 }
898 } else if (save == 2) {
899 int rv = test_stripes(fds, offsets,
900 raid_disks, chunk_size, level, layout,
901 start, length);
902 if (rv != 0) {
903 fprintf(stderr,
904 "test_stripe: test_stripes returned %d\n", rv);
905 exit(1);
906 }
907 } else {
908 int rv = restore_stripes(fds, offsets,
909 raid_disks, chunk_size, level, layout,
910 storefd, 0ULL,
911 start, length);
912 if (rv != 0) {
913 fprintf(stderr,
914 "test_stripe: restore_stripes returned %d\n",
915 rv);
916 exit(1);
917 }
918 }
919 exit(0);
920 }
921
922 #endif /* MAIN */