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