Line data Source code
1 : /*
2 : * Copyright (C) 2013 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : */
14 :
15 : #include "internal.h"
16 : #include "gf.h"
17 :
18 : /*
19 : * GEN1 (RAID5 with xor) 32bit C implementation
20 : */
21 34 : void raid_gen1_int32(int nd, size_t size, void **vv)
22 : {
23 34 : uint8_t **v = (uint8_t **)vv;
24 : uint8_t *p;
25 : int d, l;
26 : size_t i;
27 :
28 : uint32_t p0;
29 : uint32_t p1;
30 :
31 34 : l = nd - 1;
32 34 : p = v[nd];
33 :
34 1015938 : for (i = 0; i < size; i += 8) {
35 1015904 : p0 = v_32(v[l][i]);
36 1015904 : p1 = v_32(v[l][i + 4]);
37 8128544 : for (d = l - 1; d >= 0; --d) {
38 7112640 : p0 ^= v_32(v[d][i]);
39 7112640 : p1 ^= v_32(v[d][i + 4]);
40 : }
41 1015904 : v_32(p[i]) = p0;
42 1015904 : v_32(p[i + 4]) = p1;
43 : }
44 34 : }
45 :
46 : /*
47 : * GEN1 (RAID5 with xor) 64bit C implementation
48 : */
49 54 : void raid_gen1_int64(int nd, size_t size, void **vv)
50 : {
51 54 : uint8_t **v = (uint8_t **)vv;
52 : uint8_t *p;
53 : int d, l;
54 : size_t i;
55 :
56 : uint64_t p0;
57 : uint64_t p1;
58 :
59 54 : l = nd - 1;
60 54 : p = v[nd];
61 :
62 835686 : for (i = 0; i < size; i += 16) {
63 835632 : p0 = v_64(v[l][i]);
64 835632 : p1 = v_64(v[l][i + 8]);
65 6685712 : for (d = l - 1; d >= 0; --d) {
66 5850080 : p0 ^= v_64(v[d][i]);
67 5850080 : p1 ^= v_64(v[d][i + 8]);
68 : }
69 835632 : v_64(p[i]) = p0;
70 835632 : v_64(p[i + 8]) = p1;
71 : }
72 54 : }
73 :
74 : /*
75 : * GEN2 (RAID6 with powers of 2) 32bit C implementation
76 : */
77 15 : void raid_gen2_int32(int nd, size_t size, void **vv)
78 : {
79 15 : uint8_t **v = (uint8_t **)vv;
80 : uint8_t *p;
81 : uint8_t *q;
82 : int d, l;
83 : size_t i;
84 :
85 : uint32_t d0, q0, p0;
86 : uint32_t d1, q1, p1;
87 :
88 15 : l = nd - 1;
89 15 : p = v[nd];
90 15 : q = v[nd + 1];
91 :
92 393327 : for (i = 0; i < size; i += 8) {
93 393312 : q0 = p0 = v_32(v[l][i]);
94 393312 : q1 = p1 = v_32(v[l][i + 4]);
95 3147808 : for (d = l - 1; d >= 0; --d) {
96 2754496 : d0 = v_32(v[d][i]);
97 2754496 : d1 = v_32(v[d][i + 4]);
98 :
99 2754496 : p0 ^= d0;
100 2754496 : p1 ^= d1;
101 :
102 2754496 : q0 = x2_32(q0);
103 2754496 : q1 = x2_32(q1);
104 :
105 2754496 : q0 ^= d0;
106 2754496 : q1 ^= d1;
107 : }
108 393312 : v_32(p[i]) = p0;
109 393312 : v_32(p[i + 4]) = p1;
110 393312 : v_32(q[i]) = q0;
111 393312 : v_32(q[i + 4]) = q1;
112 : }
113 15 : }
114 :
115 : /*
116 : * GEN2 (RAID6 with powers of 2) 64bit C implementation
117 : */
118 17 : void raid_gen2_int64(int nd, size_t size, void **vv)
119 : {
120 17 : uint8_t **v = (uint8_t **)vv;
121 : uint8_t *p;
122 : uint8_t *q;
123 : int d, l;
124 : size_t i;
125 :
126 : uint64_t d0, q0, p0;
127 : uint64_t d1, q1, p1;
128 :
129 17 : l = nd - 1;
130 17 : p = v[nd];
131 17 : q = v[nd + 1];
132 :
133 229441 : for (i = 0; i < size; i += 16) {
134 229424 : q0 = p0 = v_64(v[l][i]);
135 229424 : q1 = p1 = v_64(v[l][i + 8]);
136 1836048 : for (d = l - 1; d >= 0; --d) {
137 1606624 : d0 = v_64(v[d][i]);
138 1606624 : d1 = v_64(v[d][i + 8]);
139 :
140 1606624 : p0 ^= d0;
141 1606624 : p1 ^= d1;
142 :
143 1606624 : q0 = x2_64(q0);
144 1606624 : q1 = x2_64(q1);
145 :
146 1606624 : q0 ^= d0;
147 1606624 : q1 ^= d1;
148 : }
149 229424 : v_64(p[i]) = p0;
150 229424 : v_64(p[i + 8]) = p1;
151 229424 : v_64(q[i]) = q0;
152 229424 : v_64(q[i + 8]) = q1;
153 : }
154 17 : }
155 :
156 : /*
157 : * GEN3 (triple parity with Cauchy matrix) 8bit C implementation
158 : *
159 : * Note that instead of a generic multiplication table, likely resulting
160 : * in multiple cache misses, a precomputed table could be used.
161 : * But this is only a kind of reference function, and we are not really
162 : * interested in speed.
163 : */
164 5 : void raid_gen3_int8(int nd, size_t size, void **vv)
165 : {
166 5 : uint8_t **v = (uint8_t **)vv;
167 : uint8_t *p;
168 : uint8_t *q;
169 : uint8_t *r;
170 : int d, l;
171 : size_t i;
172 :
173 : uint8_t d0, r0, q0, p0;
174 :
175 5 : l = nd - 1;
176 5 : p = v[nd];
177 5 : q = v[nd + 1];
178 5 : r = v[nd + 2];
179 :
180 786949 : for (i = 0; i < size; i += 1) {
181 786944 : p0 = q0 = r0 = 0;
182 6299904 : for (d = l; d > 0; --d) {
183 5512960 : d0 = v_8(v[d][i]);
184 :
185 5512960 : p0 ^= d0;
186 5512960 : q0 ^= gfmul[d0][gfgen[1][d]];
187 5512960 : r0 ^= gfmul[d0][gfgen[2][d]];
188 : }
189 :
190 : /* first disk with all coefficients at 1 */
191 786944 : d0 = v_8(v[0][i]);
192 :
193 786944 : p0 ^= d0;
194 786944 : q0 ^= d0;
195 786944 : r0 ^= d0;
196 :
197 786944 : v_8(p[i]) = p0;
198 786944 : v_8(q[i]) = q0;
199 786944 : v_8(r[i]) = r0;
200 : }
201 5 : }
202 :
203 : /*
204 : * GEN4 (quad parity with Cauchy matrix) 8bit C implementation
205 : *
206 : * Note that instead of a generic multiplication table, likely resulting
207 : * in multiple cache misses, a precomputed table could be used.
208 : * But this is only a kind of reference function, and we are not really
209 : * interested in speed.
210 : */
211 5 : void raid_gen4_int8(int nd, size_t size, void **vv)
212 : {
213 5 : uint8_t **v = (uint8_t **)vv;
214 : uint8_t *p;
215 : uint8_t *q;
216 : uint8_t *r;
217 : uint8_t *s;
218 : int d, l;
219 : size_t i;
220 :
221 : uint8_t d0, s0, r0, q0, p0;
222 :
223 5 : l = nd - 1;
224 5 : p = v[nd];
225 5 : q = v[nd + 1];
226 5 : r = v[nd + 2];
227 5 : s = v[nd + 3];
228 :
229 786949 : for (i = 0; i < size; i += 1) {
230 786944 : p0 = q0 = r0 = s0 = 0;
231 6299904 : for (d = l; d > 0; --d) {
232 5512960 : d0 = v_8(v[d][i]);
233 :
234 5512960 : p0 ^= d0;
235 5512960 : q0 ^= gfmul[d0][gfgen[1][d]];
236 5512960 : r0 ^= gfmul[d0][gfgen[2][d]];
237 5512960 : s0 ^= gfmul[d0][gfgen[3][d]];
238 : }
239 :
240 : /* first disk with all coefficients at 1 */
241 786944 : d0 = v_8(v[0][i]);
242 :
243 786944 : p0 ^= d0;
244 786944 : q0 ^= d0;
245 786944 : r0 ^= d0;
246 786944 : s0 ^= d0;
247 :
248 786944 : v_8(p[i]) = p0;
249 786944 : v_8(q[i]) = q0;
250 786944 : v_8(r[i]) = r0;
251 786944 : v_8(s[i]) = s0;
252 : }
253 5 : }
254 :
255 : /*
256 : * GEN5 (penta parity with Cauchy matrix) 8bit C implementation
257 : *
258 : * Note that instead of a generic multiplication table, likely resulting
259 : * in multiple cache misses, a precomputed table could be used.
260 : * But this is only a kind of reference function, and we are not really
261 : * interested in speed.
262 : */
263 4 : void raid_gen5_int8(int nd, size_t size, void **vv)
264 : {
265 4 : uint8_t **v = (uint8_t **)vv;
266 : uint8_t *p;
267 : uint8_t *q;
268 : uint8_t *r;
269 : uint8_t *s;
270 : uint8_t *t;
271 : int d, l;
272 : size_t i;
273 :
274 : uint8_t d0, t0, s0, r0, q0, p0;
275 :
276 4 : l = nd - 1;
277 4 : p = v[nd];
278 4 : q = v[nd + 1];
279 4 : r = v[nd + 2];
280 4 : s = v[nd + 3];
281 4 : t = v[nd + 4];
282 :
283 524804 : for (i = 0; i < size; i += 1) {
284 524800 : p0 = q0 = r0 = s0 = t0 = 0;
285 4202752 : for (d = l; d > 0; --d) {
286 3677952 : d0 = v_8(v[d][i]);
287 :
288 3677952 : p0 ^= d0;
289 3677952 : q0 ^= gfmul[d0][gfgen[1][d]];
290 3677952 : r0 ^= gfmul[d0][gfgen[2][d]];
291 3677952 : s0 ^= gfmul[d0][gfgen[3][d]];
292 3677952 : t0 ^= gfmul[d0][gfgen[4][d]];
293 : }
294 :
295 : /* first disk with all coefficients at 1 */
296 524800 : d0 = v_8(v[0][i]);
297 :
298 524800 : p0 ^= d0;
299 524800 : q0 ^= d0;
300 524800 : r0 ^= d0;
301 524800 : s0 ^= d0;
302 524800 : t0 ^= d0;
303 :
304 524800 : v_8(p[i]) = p0;
305 524800 : v_8(q[i]) = q0;
306 524800 : v_8(r[i]) = r0;
307 524800 : v_8(s[i]) = s0;
308 524800 : v_8(t[i]) = t0;
309 : }
310 4 : }
311 :
312 : /*
313 : * GEN6 (hexa parity with Cauchy matrix) 8bit C implementation
314 : *
315 : * Note that instead of a generic multiplication table, likely resulting
316 : * in multiple cache misses, a precomputed table could be used.
317 : * But this is only a kind of reference function, and we are not really
318 : * interested in speed.
319 : */
320 4 : void raid_gen6_int8(int nd, size_t size, void **vv)
321 : {
322 4 : uint8_t **v = (uint8_t **)vv;
323 : uint8_t *p;
324 : uint8_t *q;
325 : uint8_t *r;
326 : uint8_t *s;
327 : uint8_t *t;
328 : uint8_t *u;
329 : int d, l;
330 : size_t i;
331 :
332 : uint8_t d0, u0, t0, s0, r0, q0, p0;
333 :
334 4 : l = nd - 1;
335 4 : p = v[nd];
336 4 : q = v[nd + 1];
337 4 : r = v[nd + 2];
338 4 : s = v[nd + 3];
339 4 : t = v[nd + 4];
340 4 : u = v[nd + 5];
341 :
342 524804 : for (i = 0; i < size; i += 1) {
343 524800 : p0 = q0 = r0 = s0 = t0 = u0 = 0;
344 4202752 : for (d = l; d > 0; --d) {
345 3677952 : d0 = v_8(v[d][i]);
346 :
347 3677952 : p0 ^= d0;
348 3677952 : q0 ^= gfmul[d0][gfgen[1][d]];
349 3677952 : r0 ^= gfmul[d0][gfgen[2][d]];
350 3677952 : s0 ^= gfmul[d0][gfgen[3][d]];
351 3677952 : t0 ^= gfmul[d0][gfgen[4][d]];
352 3677952 : u0 ^= gfmul[d0][gfgen[5][d]];
353 : }
354 :
355 : /* first disk with all coefficients at 1 */
356 524800 : d0 = v_8(v[0][i]);
357 :
358 524800 : p0 ^= d0;
359 524800 : q0 ^= d0;
360 524800 : r0 ^= d0;
361 524800 : s0 ^= d0;
362 524800 : t0 ^= d0;
363 524800 : u0 ^= d0;
364 :
365 524800 : v_8(p[i]) = p0;
366 524800 : v_8(q[i]) = q0;
367 524800 : v_8(r[i]) = r0;
368 524800 : v_8(s[i]) = s0;
369 524800 : v_8(t[i]) = t0;
370 524800 : v_8(u[i]) = u0;
371 : }
372 4 : }
373 :
374 : /*
375 : * Recover failure of one data block at index id[0] using parity at index
376 : * ip[0] for any RAID level.
377 : *
378 : * Starting from the equation:
379 : *
380 : * Pd = A[ip[0],id[0]] * Dx
381 : *
382 : * and solving we get:
383 : *
384 : * Dx = A[ip[0],id[0]]^-1 * Pd
385 : */
386 124 : void raid_rec1_int8(int nr, int *id, int *ip, int nd, size_t size, void **vv)
387 : {
388 124 : uint8_t **v = (uint8_t **)vv;
389 : uint8_t *p;
390 : uint8_t *pa;
391 : const uint8_t *T;
392 : uint8_t G;
393 : uint8_t V;
394 : size_t i;
395 :
396 : (void)nr; /* unused, it's always 1 */
397 :
398 : /* if it's RAID5 uses the faster function */
399 124 : if (ip[0] == 0) {
400 24 : raid_rec1of1(id, nd, size, vv);
401 148 : return;
402 : }
403 :
404 : /* setup the coefficients matrix */
405 200 : G = A(ip[0], id[0]);
406 :
407 : /* invert it to solve the system of linear equations */
408 200 : V = inv(G);
409 :
410 : /* get multiplication tables */
411 200 : T = table(V);
412 :
413 : /* compute delta parity */
414 100 : raid_delta_gen(1, id, ip, nd, size, vv);
415 :
416 100 : p = v[nd + ip[0]];
417 100 : pa = v[id[0]];
418 :
419 4215908 : for (i = 0; i < size; ++i) {
420 : /* delta */
421 4215808 : uint8_t Pd = p[i] ^ pa[i];
422 :
423 : /* reconstruct */
424 4215808 : pa[i] = T[Pd];
425 : }
426 : }
427 :
428 : /*
429 : * Recover failure of two data blocks at indexes id[0],id[1] using parity at
430 : * indexes ip[0],ip[1] for any RAID level.
431 : *
432 : * Starting from the equations:
433 : *
434 : * Pd = A[ip[0],id[0]] * Dx + A[ip[0],id[1]] * Dy
435 : * Qd = A[ip[1],id[0]] * Dx + A[ip[1],id[1]] * Dy
436 : *
437 : * we solve inverting the coefficients matrix.
438 : */
439 1204 : void raid_rec2_int8(int nr, int *id, int *ip, int nd, size_t size, void **vv)
440 : {
441 1204 : uint8_t **v = (uint8_t **)vv;
442 : uint8_t *p;
443 : uint8_t *pa;
444 : uint8_t *q;
445 : uint8_t *qa;
446 1204 : const int N = 2;
447 1204 : const uint8_t *T[N][N];
448 1204 : uint8_t G[N * N];
449 1204 : uint8_t V[N * N];
450 : size_t i;
451 : int j, k;
452 :
453 : (void)nr; /* unused, it's always 2 */
454 :
455 : /* if it's RAID6 recovering with P and Q uses the faster function */
456 1204 : if (ip[0] == 0 && ip[1] == 1) {
457 132 : raid_rec2of2_int8(id, ip, nd, size, vv);
458 1336 : return;
459 : }
460 :
461 : /* setup the coefficients matrix */
462 3216 : for (j = 0; j < N; ++j)
463 6432 : for (k = 0; k < N; ++k)
464 8576 : G[j * N + k] = A(ip[j], id[k]);
465 :
466 : /* invert it to solve the system of linear equations */
467 1072 : raid_invert(G, V, N);
468 :
469 : /* get multiplication tables */
470 3216 : for (j = 0; j < N; ++j)
471 6432 : for (k = 0; k < N; ++k)
472 8576 : T[j][k] = table(V[j * N + k]);
473 :
474 : /* compute delta parity */
475 1072 : raid_delta_gen(2, id, ip, nd, size, vv);
476 :
477 1072 : p = v[nd + ip[0]];
478 1072 : q = v[nd + ip[1]];
479 1072 : pa = v[id[0]];
480 1072 : qa = v[id[1]];
481 :
482 4465712 : for (i = 0; i < size; ++i) {
483 : /* delta */
484 4464640 : uint8_t Pd = p[i] ^ pa[i];
485 4464640 : uint8_t Qd = q[i] ^ qa[i];
486 :
487 : /* reconstruct */
488 4464640 : pa[i] = T[0][0][Pd] ^ T[0][1][Qd];
489 4464640 : qa[i] = T[1][0][Pd] ^ T[1][1][Qd];
490 : }
491 : }
492 :
493 : /*
494 : * Recover failure of N data blocks at indexes id[N] using parity at indexes
495 : * ip[N] for any RAID level.
496 : *
497 : * Starting from the N equations, with 0<=i<N :
498 : *
499 : * PD[i] = sum(A[ip[i],id[j]] * D[i]) 0<=j<N
500 : *
501 : * we solve inverting the coefficients matrix.
502 : *
503 : * Note that referring at previous equations you have:
504 : * PD[0] = Pd, PD[1] = Qd, PD[2] = Rd, ...
505 : * D[0] = Dx, D[1] = Dy, D[2] = Dz, ...
506 : */
507 17753 : void raid_recX_int8(int nr, int *id, int *ip, int nd, size_t size, void **vv)
508 : {
509 17753 : uint8_t **v = (uint8_t **)vv;
510 : uint8_t *p[RAID_PARITY_MAX];
511 : uint8_t *pa[RAID_PARITY_MAX];
512 : const uint8_t *T[RAID_PARITY_MAX][RAID_PARITY_MAX];
513 : uint8_t G[RAID_PARITY_MAX * RAID_PARITY_MAX];
514 : uint8_t V[RAID_PARITY_MAX * RAID_PARITY_MAX];
515 : size_t i;
516 : int j, k;
517 :
518 : /* setup the coefficients matrix */
519 90761 : for (j = 0; j < nr; ++j)
520 386140 : for (k = 0; k < nr; ++k)
521 626264 : G[j * nr + k] = A(ip[j], id[k]);
522 :
523 : /* invert it to solve the system of linear equations */
524 17753 : raid_invert(G, V, nr);
525 :
526 : /* get multiplication tables */
527 90761 : for (j = 0; j < nr; ++j)
528 386140 : for (k = 0; k < nr; ++k)
529 626264 : T[j][k] = table(V[j * nr + k]);
530 :
531 : /* compute delta parity */
532 17753 : raid_delta_gen(nr, id, ip, nd, size, vv);
533 :
534 90761 : for (j = 0; j < nr; ++j) {
535 73008 : p[j] = v[nd + ip[j]];
536 73008 : pa[j] = v[id[j]];
537 : }
538 :
539 12942937 : for (i = 0; i < size; ++i) {
540 : uint8_t PD[RAID_PARITY_MAX];
541 :
542 : /* delta */
543 69327104 : for (j = 0; j < nr; ++j)
544 56401920 : PD[j] = p[j][i] ^ pa[j][i];
545 :
546 : /* reconstruct */
547 69327104 : for (j = 0; j < nr; ++j) {
548 56401920 : uint8_t b = 0;
549 :
550 316742656 : for (k = 0; k < nr; ++k)
551 260340736 : b ^= T[j][k][PD[k]];
552 56401920 : pa[j][i] = b;
553 : }
554 : }
555 17753 : }
556 :
|