/* * Copyright (C) 2013 Andrea Mazzoleni * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "internal.h" #include "gf.h" /* * PAR1 (RAID5 with xor) 32bit C implementation */ void raid_par1_int32(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; int d, l; size_t i; uint32_t p0; uint32_t p1; l = nd - 1; p = v[nd]; for(i=0;i=0;--d) { p0 ^= v_32(v[d][i]); p1 ^= v_32(v[d][i+4]); } v_32(p[i]) = p0; v_32(p[i+4]) = p1; } } /* * PAR1 (RAID5 with xor) 64bit C implementation */ void raid_par1_int64(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; int d, l; size_t i; uint64_t p0; uint64_t p1; l = nd - 1; p = v[nd]; for(i=0;i=0;--d) { p0 ^= v_64(v[d][i]); p1 ^= v_64(v[d][i+8]); } v_64(p[i]) = p0; v_64(p[i+8]) = p1; } } /* * PAR2 (RAID6 with powers of 2) 32bit C implementation */ void raid_par2_int32(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* q; int d, l; size_t i; uint32_t d0, q0, p0; uint32_t d1, q1, p1; l = nd - 1; p = v[nd]; q = v[nd+1]; for(i=0;i=0;--d) { d0 = v_32(v[d][i]); d1 = v_32(v[d][i+4]); p0 ^= d0; p1 ^= d1; q0 = x2_32(q0); q1 = x2_32(q1); q0 ^= d0; q1 ^= d1; } v_32(p[i]) = p0; v_32(p[i+4]) = p1; v_32(q[i]) = q0; v_32(q[i+4]) = q1; } } /* * PAR2 (RAID6 with powers of 2) 64bit C implementation */ void raid_par2_int64(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* q; int d, l; size_t i; uint64_t d0, q0, p0; uint64_t d1, q1, p1; l = nd - 1; p = v[nd]; q = v[nd+1]; for(i=0;i=0;--d) { d0 = v_64(v[d][i]); d1 = v_64(v[d][i+8]); p0 ^= d0; p1 ^= d1; q0 = x2_64(q0); q1 = x2_64(q1); q0 ^= d0; q1 ^= d1; } v_64(p[i]) = p0; v_64(p[i+8]) = p1; v_64(q[i]) = q0; v_64(q[i+8]) = q1; } } /* * PAR3 (triple parity with Cauchy matrix) 8bit C implementation * * Note that instead of a generic multiplicationt table, likely resulting * in multiple cache misses, a precomputed table could be used. * But this is only a kind of reference function, and we are not really * interested in speed. */ void raid_par3_int8(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* q; uint8_t* r; int d, l; size_t i; uint8_t d0, r0, q0, p0; l = nd - 1; p = v[nd]; q = v[nd+1]; r = v[nd+2]; for(i=0;i0;--d) { d0 = v_8(v[d][i]); p0 ^= d0; q0 ^= gfmul[d0][gfcauchy[1][d]]; r0 ^= gfmul[d0][gfcauchy[2][d]]; } /* first disk with all coefficients at 1 */ d0 = v_8(v[0][i]); p0 ^= d0; q0 ^= d0; r0 ^= d0; v_8(p[i]) = p0; v_8(q[i]) = q0; v_8(r[i]) = r0; } } /* * PAR4 (quad parity with Cauchy matrix) 8bit C implementation * * Note that instead of a generic multiplicationt table, likely resulting * in multiple cache misses, a precomputed table could be used. * But this is only a kind of reference function, and we are not really * interested in speed. */ void raid_par4_int8(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* q; uint8_t* r; uint8_t* s; int d, l; size_t i; uint8_t d0, s0, r0, q0, p0; l = nd - 1; p = v[nd]; q = v[nd+1]; r = v[nd+2]; s = v[nd+3]; for(i=0;i0;--d) { d0 = v_8(v[d][i]); p0 ^= d0; q0 ^= gfmul[d0][gfcauchy[1][d]]; r0 ^= gfmul[d0][gfcauchy[2][d]]; s0 ^= gfmul[d0][gfcauchy[3][d]]; } /* first disk with all coefficients at 1 */ d0 = v_8(v[0][i]); p0 ^= d0; q0 ^= d0; r0 ^= d0; s0 ^= d0; v_8(p[i]) = p0; v_8(q[i]) = q0; v_8(r[i]) = r0; v_8(s[i]) = s0; } } /* * PAR5 (penta parity with Cauchy matrix) 8bit C implementation * * Note that instead of a generic multiplicationt table, likely resulting * in multiple cache misses, a precomputed table could be used. * But this is only a kind of reference function, and we are not really * interested in speed. */ void raid_par5_int8(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* q; uint8_t* r; uint8_t* s; uint8_t* t; int d, l; size_t i; uint8_t d0, t0, s0, r0, q0, p0; l = nd - 1; p = v[nd]; q = v[nd+1]; r = v[nd+2]; s = v[nd+3]; t = v[nd+4]; for(i=0;i0;--d) { d0 = v_8(v[d][i]); p0 ^= d0; q0 ^= gfmul[d0][gfcauchy[1][d]]; r0 ^= gfmul[d0][gfcauchy[2][d]]; s0 ^= gfmul[d0][gfcauchy[3][d]]; t0 ^= gfmul[d0][gfcauchy[4][d]]; } /* first disk with all coefficients at 1 */ d0 = v_8(v[0][i]); p0 ^= d0; q0 ^= d0; r0 ^= d0; s0 ^= d0; t0 ^= d0; v_8(p[i]) = p0; v_8(q[i]) = q0; v_8(r[i]) = r0; v_8(s[i]) = s0; v_8(t[i]) = t0; } } /* * PAR6 (hexa parity with Cauchy matrix) 8bit C implementation * * Note that instead of a generic multiplicationt table, likely resulting * in multiple cache misses, a precomputed table could be used. * But this is only a kind of reference function, and we are not really * interested in speed. */ void raid_par6_int8(int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* q; uint8_t* r; uint8_t* s; uint8_t* t; uint8_t* u; int d, l; size_t i; uint8_t d0, u0, t0, s0, r0, q0, p0; l = nd - 1; p = v[nd]; q = v[nd+1]; r = v[nd+2]; s = v[nd+3]; t = v[nd+4]; u = v[nd+5]; for(i=0;i0;--d) { d0 = v_8(v[d][i]); p0 ^= d0; q0 ^= gfmul[d0][gfcauchy[1][d]]; r0 ^= gfmul[d0][gfcauchy[2][d]]; s0 ^= gfmul[d0][gfcauchy[3][d]]; t0 ^= gfmul[d0][gfcauchy[4][d]]; u0 ^= gfmul[d0][gfcauchy[5][d]]; } /* first disk with all coefficients at 1 */ d0 = v_8(v[0][i]); p0 ^= d0; q0 ^= d0; r0 ^= d0; s0 ^= d0; t0 ^= d0; u0 ^= d0; v_8(p[i]) = p0; v_8(q[i]) = q0; v_8(r[i]) = r0; v_8(s[i]) = s0; v_8(t[i]) = t0; v_8(u[i]) = u0; } } /* * Recover failure of one data block at index id[0] using parity at index * ip[0] for any RAID level. * * Starting from the equation: * * Pd = A[ip[0],id[0]] * Dx * * and solving we get: * * Dx = A[ip[0],id[0]]^-1 * Pd */ void raid_rec1_int8(int nr, const int* id, const int* ip, int nd, size_t size, void** vv) { uint8_t** v = (uint8_t**)vv; uint8_t* p; uint8_t* pa; const uint8_t* T; uint8_t G; uint8_t V; size_t i; (void)nr; /* unused, it's always 1 */ /* if it's RAID5 uses the dedicated and faster function */ if (ip[0] == 0) { raid_rec1_par1(id, nd, size, vv); return; } /* setup the coefficients matrix */ G = A(ip[0],id[0]); /* invert it to solve the system of linear equations */ V = inv(G); /* get multiplication tables */ T = table(V); /* compute delta parity */ raid_delta_gen(1, id, ip, nd, size, vv); p = v[nd+ip[0]]; pa = v[id[0]]; for(i=0;i