Splits the RAID engine in multiple files.

This commit is contained in:
Andrea Mazzoleni 2013-12-28 09:10:00 +01:00
parent b4939aa33a
commit abc05c612c
23 changed files with 1825 additions and 1630 deletions

435
raid/cpu.h Normal file
View file

@ -0,0 +1,435 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __RAID_CPU_H
#define __RAID_CPU_H
#if defined(__i386__) || defined(__x86_64__)
static __always_inline void raid_cpuid(uint32_t func_eax, uint32_t sub_ecx, uint32_t* reg)
{
asm volatile(
#if defined(__i386__) && defined(__PIC__)
/* allow compilation in PIC mode saving ebx */
"xchgl %%ebx, %1\n"
"cpuid\n"
"xchgl %%ebx, %1\n"
: "=a" (reg[0]), "=r" (reg[1]), "=c" (reg[2]), "=d" (reg[3])
: "0" (func_eax), "2" (sub_ecx)
#else
"cpuid\n"
: "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3])
: "0" (func_eax), "2" (sub_ecx)
#endif
);
}
#define CPU_VENDOR_MAX 13
static __always_inline void raid_cpu_info(char* vendor, unsigned* family, unsigned* model)
{
uint32_t reg[4];
unsigned f, ef, m, em;
raid_cpuid(0, 0, reg);
((uint32_t*)vendor)[0] = reg[1];
((uint32_t*)vendor)[1] = reg[3];
((uint32_t*)vendor)[2] = reg[2];
vendor[12] = 0;
raid_cpuid(1, 0, reg);
f = (reg[0] >> 8) & 0xF;
ef = (reg[0] >> 20) & 0xFF;
m = (reg[0] >> 4) & 0xF;
em = (reg[0] >> 16) & 0xF;
if (strcmp(vendor, "AuthenticAMD") == 0) {
if (f < 15) {
*family = f;
*model = m;
} else {
*family = f + ef;
*model = m + (em << 4);
}
} else {
*family = f + ef;
*model = m + (em << 4);
}
}
static __always_inline int raid_cpu_has_mmx(void)
{
uint32_t reg[4];
raid_cpuid(1, 0, reg);
return (reg[3] >> 23) & 1;
}
static __always_inline int raid_cpu_has_sse2(void)
{
uint32_t reg[4];
raid_cpuid(1, 0, reg);
return (reg[3] >> 26) & 1;
}
static __always_inline int raid_cpu_has_ssse3(void)
{
uint32_t reg[4];
raid_cpuid(1, 0, reg);
return (reg[2] >> 9) & 1;
}
static __always_inline int raid_cpu_has_sse42(void)
{
uint32_t reg[4];
raid_cpuid(1, 0, reg);
return (reg[2] >> 20) & 1;
}
static __always_inline int raid_cpu_has_avx(void)
{
uint32_t reg[4];
raid_cpuid(1, 0, reg);
return (reg[2] >> 28) & 1;
}
static __always_inline int raid_cpu_has_avx2(void)
{
uint32_t reg[4];
raid_cpuid(7, 0, reg);
return (reg[1] >> 5) & 1;
}
/**
* Check if the processor has a slow MULT implementation.
* If yes, it's better to use a hash not based on multiplication.
*/
static __always_inline int raid_cpu_has_slowmult(void)
{
char vendor[CPU_VENDOR_MAX];
unsigned family;
unsigned model;
raid_cpu_info(vendor, &family, &model);
if (strcmp(vendor, "GenuineIntel") == 0) {
/* Intel(R) Atom(TM) CPU D525 @ 1.80GHz (info from user)
* Compiler gcc 4.7.2
* CPU GenuineIntel, family 6, model 28, flags mmx sse2 slowmult
* Memory is little-endian 64-bit
* Speed test with 8 disk and 262144 buffer size...
* memset0 1849 [MB/s]
* HASH Murmur3 378 [MB/s] (extremely slower than Spooky2)
* HASH Spooky2 3413 [MB/s]
* PAR1 int32x2 707 [MB/s]
* PAR1 mmxx2 1264 [MB/s]
* PAR1 mmxx4 1910 [MB/s]
* PAR1 sse2x2 2204 [MB/s]
* PAR1 sse2x4 2980 [MB/s]
* PAR2 int32x2 296 [MB/s]
* PAR2 mmxx2 543 [MB/s]
* PAR2 sse2x2 1068 [MB/s]
* PAR2 sse2x4 1601 [MB/s]
*/
if (family == 6 && model == 28)
return 1;
}
return 0;
}
/**
* Check if the processor has a slow PSHUFB implementation.
* If yes, it's better to not use it.
*/
static __always_inline int raid_cpu_has_slowpshufb(void)
{
char vendor[CPU_VENDOR_MAX];
unsigned family;
unsigned model;
raid_cpu_info(vendor, &family, &model);
if (strcmp(vendor, "GenuineIntel") == 0) {
/* Intel ATOM (info from x264 -> full family/model 6/28) */
/* Intel(R) Atom(TM) CPU D525 @ 1.80GHz (info from user)
* Compiler gcc 4.7.2
* CPU GenuineIntel, family 6, model 28, flags mmx sse2 ssse3 slowmult
* Memory is little-endian 64-bit
* Support nanosecond timestamps with futimens()
*
* Speed test with 8 disk and 262144 buffer size, for a total of 2816 KiB...
* memset0 1850 [MB/s]
* CRC table 316 [MB/s]
* HASH Murmur3 378 [MB/s]
* HASH Spooky2 3389 [MB/s]
* PAR1 int32 1800 [MB/s]
* PAR1 int64 2938 [MB/s]
* PAR1 mmx 2946 [MB/s]
* PAR1 sse2 3429 [MB/s]
* PAR1 sse2ext 3447 [MB/s]
* PAR2 int32 519 [MB/s]
* PAR2 int64 944 [MB/s]
* PAR2 mmx 1160 [MB/s]
* PAR2 sse2 2082 [MB/s]
* PAR2 sse2ext 2481 [MB/s]
* PARz int32 236 [MB/s]
* PARz int64 456 [MB/s]
* PARz mmx 576 [MB/s]
* PARz sse2 1109 [MB/s]
* PARz sse2ext 1527 [MB/s]
* PARz ssse3 931 [MB/s] (slower than sse2ext)
* PARz ssse3ext 932 [MB/s] (slower than sse2ext)
* PAR4z int32 144 [MB/s]
* PAR4z int64 271 [MB/s]
* PAR4z mmx 431 [MB/s]
* PAR4z sse2 843 [MB/s]
* PAR4z sse2ext 1086 [MB/s]
* PAR4z ssse3 732 [MB/s] (slower than sse2ext)
* PAR4z ssse3ext 571 [MB/s] (slower than sse2ext)
*/
if (family == 6 && model == 28)
return 1;
}
if (strcmp(vendor, "AuthenticAMD") == 0) {
/* AMD Jaguar (info from x264 -> full family 0x16==21) */
if (family == 21)
return 1;
}
return 0;
}
/**
* Check if the processor has a slow extended set of SSE registers.
* If yes, it's better to unroll without using the second part of registers.
*/
static __always_inline int raid_cpu_has_slowextendedreg(void)
{
char vendor[CPU_VENDOR_MAX];
unsigned family;
unsigned model;
raid_cpu_info(vendor, &family, &model);
if (strcmp(vendor, "AuthenticAMD") == 0) {
/* AMD Bulldozer (from user)
* CPU AuthenticAMD, family 21, model 19, flags mmx sse2
* Memory is little-endian 64-bit
* Speed test with 8 disk and 262144 buffer size, for a total of 2560 KiB...
* memset0 5721 [MB/s]
* CRC table 1080 [MB/s]
* CRC intel-crc32 2845 [MB/s]
* HASH Murmur3 2970 [MB/s]
* HASH Spooky2 7503 [MB/s]
* PAR1 int32x2 4595 [MB/s]
* PAR1 mmxx2 5856 [MB/s]
* PAR1 mmxx4 6157 [MB/s]
* PAR1 sse2x2 7151 [MB/s]
* PAR1 sse2x4 8447 [MB/s]
* PAR1 sse2x8 8155 [MB/s] (slower than sse2x4)
* PAR2 int32x2 1892 [MB/s]
* PAR2 mmxx2 3744 [MB/s]
* PAR2 sse2x2 4922 [MB/s]
* PAR2 sse2x4 4464 [MB/s] (slower than sse2x2)
*/
if (family == 21)
return 1;
}
return 0;
}
/* other cases */
/* Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
* Compiler gcc 4.7.3
* CPU GenuineIntel, family 6, model 58, flags mmx sse2
* Memory is little-endian 32-bit
* memset0 34685 [MB/s]
* Murmur3 4170 [MB/s]
* Spooky2 2599 [MB/s]
* PAR1 int32x2 8919 [MB/s]
* PAR1 mmxx2 17170 [MB/s]
* PAR1 sse2x2 27478 [MB/s]
* PAR2 int32x2 1953 [MB/s]
* PAR2 mmxx2 7560 [MB/s]
* PAR2 sse2x2 13930 [MB/s]
*/
/* Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
* Compiler gcc 4.7.3
* CPU GenuineIntel, family 6, model 58, flags mmx sse2
* Memory is little-endian 64-bit
* memset0 26164 [MB/s]
* Murmur3 4469 [MB/s]
* Spooky2 12834 [MB/s]
* PAR1 int32x2 10775 [MB/s]
* PAR1 mmxx2 19644 [MB/s]
* PAR1 sse2x2 29168 [MB/s]
* PAR2 int32x2 3288 [MB/s]
* PAR2 mmxx2 8374 [MB/s]
* PAR2 sse2x2 15547 [MB/s]
*/
/* Intel(R) Core(TM) i5 CPU 650 @ 3.20GHz
* Compiler gcc 4.7.1
* CPU GenuineIntel, family 6, model 37, flags mmx sse2
* Memory is little-endian 32-bit
* memset0 25468 [MB/s]
* Murmur3 3357 [MB/s]
* Spooky2 1861 [MB/s]
* PAR1 int32x2 5043 [MB/s]
* PAR1 mmxx2 9864 [MB/s]
* PAR1 sse2x2 16896 [MB/s]
* PAR2 int32x2 1100 [MB/s]
* PAR2 mmxx2 5056 [MB/s]
* PAR2 sse2x2 8726 [MB/s]
*/
/* AMD Athlon(tm) 64 X2 Dual Core Processor 3600+ 1913.377 MHz
* Compiler gcc 4.7.3
* CPU AuthenticAMD, family 15, model 107, flags mmx sse2
* Memory is little-endian 32-bit
* memset 2414 [MB/s]
* Murmur3 1213 [MB/s]
* Spooky2 947 [MB/s]
* PAR1 int32x2 532 [MB/s]
* PAR1 mmxx2 1121 [MB/s]
* PAR1 sse2x2 2061 [MB/s]
* PAR2 int32x2 280 [MB/s]
* PAR2 mmxx2 632 [MB/s]
* PAR2 sse2x2 909 [MB/s]
*/
/* ARM Feroceon 88FR131 rev 1 (v5l)
* Compiler gcc 4.7.2
* CPU is not a x86/x64
* Memory is little-endian 32-bit
* memset 763 [MB/s]
* Murmur3 237 [MB/s]
* Spooky2 216 [MB/s]
* PAR1 int32x2 233 [MB/s]
* PAR2 int32x2 103 [MB/s]
*/
/* Intel(R) Xeon(R) CPU E3-1270 V2 @ 3.50GHz $
* Compiler gcc 4.8.1
* CPU GenuineIntel, family 6, model 58, flags mmx sse2
* Memory is little-endian 64-bit
* memset 27842 [MB/s]
* Murmur3 4884 [MB/s]
* Spooky2 14039 [MB/s]
* PAR1 int32x2 11038 [MB/s]
* PAR1 mmxx2 20055 [MB/s]
* PAR1 sse2x2 30703 [MB/s]
* PAR2 int32x2 3267 [MB/s]
* PAR2 mmxx2 8883 [MB/s]
* PAR2 sse2x2 16433 [MB/s]
*/
/* Bobcat/Zacade (info from x264 -> full family 20)
* AMD E-350 Processor (info from user)
* Compiler gcc 4.7.3
* CPU AuthenticAMD, family 20, model 1, flags mmx sse2
* Memory is little-endian 64-bit
* memset 2137 [MB/s]
* Murmur3 1140 [MB/s]
* Spooky2 2326 [MB/s]
* PAR1 int32x2 1853 [MB/s]
* PAR1 mmxx2 2019 [MB/s]
* PAR1 sse2x2 2908 [MB/s]
* PAR2 int32x2 884 [MB/s]
* PAR2 mmxx2 1502 [MB/s]
* PAR2 sse2x2 1168 [MB/s] (slow version with prefetchnta)
*/
/* AMD Turion(tm) II Neo N40L Dual-Core Processor (info from user)
* Compiler gcc 4.7.1
* CPU AuthenticAMD, family 16, model 6, flags mmx sse2
* Memory is little-endian 64-bit
* memset 4910 [MB/s]
* Murmur3 1160 [MB/s]
* Spooky2 3994 [MB/s]
* PAR1 int32x2 530 [MB/s]
* PAR1 mmxx2 1114 [MB/s]
* PAR1 sse2x1 2127 [MB/s]
* PAR1 sse2x2 2804 [MB/s]
* PAR2 int32x2 316 [MB/s]
* PAR2 mmxx2 655 [MB/s]
* PAR2 sse2x2 639 [MB/s] (slow version with prefetchnta)
*/
/* AMD Athlon(tm) II X4 620 (info from user)
* Compiler gcc 4.7.3
* CPU AuthenticAMD, family 16, model 5, flags mmx sse2
* Memory is little-endian 64-bit
* memset 2845 [MB/s]
* Murmur3 2262 [MB/s]
* Murmur3x64 3500 [MB/s]
* Spooky2 4362 [MB/s]
* Spooky2x86 4000 [MB/s]
* PAR1 int32x2 1064 [MB/s]
* PAR1 mmxx2 2064 [MB/s]
* PAR1 sse2x2 3289 [MB/s]
* PAR2 int32x2 639 [MB/s]
* PAR2 mmxx2 1325 [MB/s]
* PAR2 sse2x2 915 [MB/s] (slow version with prefetchnta)
*/
/* ARM @ 1.2GHz (info from user)
* /proc/cpuinfo
* model name : Feroceon 88FR131 rev 1 (v5l)
* BogoMIPS : 1196.85
* Features : swp half thumb fastmult edsp
* CPU implementer : 0x56
* CPU architecture: 5TE
* CPU variant : 0x2
* CPU part : 0x131
* CPU revision : 1
* Hardware : QNAP TS-41x
*
* Compiler gcc 4.7.2
* CPU is not a x86/x64
* Memory is little-endian 32-bit
* Speed test with 4 disk and 262144 buffer size...
* memset 763 [MB/s]
* Murmur3 237 [MB/s]
* Spooky2 216 [MB/s]
* Spooky2x86 418 [MB/s] (dropped experiment of a new hash)
* PAR1 int32x2 233 [MB/s]
* PAR2 int32x2 103 [MB/s]
*/
#endif
#endif

144
raid/gf.h Normal file
View file

@ -0,0 +1,144 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __RAID_GF_H
#define __RAID_GF_H
/*
* Galois field operations.
*
* Basic range checks are implemented using BUG_ON().
*/
/*
* Generic assert() with inverted meaning.
*/
#define BUG_ON(a) assert(!(a))
/*
* GF a*b.
*/
static __always_inline uint8_t mul(uint8_t a, uint8_t b)
{
return gfmul[a][b];
}
/*
* GF 1/a.
* Not defined for a == 0.
*/
static __always_inline uint8_t inv(uint8_t v)
{
BUG_ON(v == 0); /* division by zero */
return gfinv[v];
}
/*
* GF 2^a.
*/
static __always_inline uint8_t pow2(int v)
{
BUG_ON(v < 0 || v > 254); /* invalid exponent */
return gfexp[v];
}
/*
* Gets the multiplication table for a specified value.
*/
static __always_inline const uint8_t* table(uint8_t v)
{
return gfmul[v];
}
extern const uint8_t (*raid_gen)[256];
/*
* Gets the generator matrix coefficient for parity 'p' and disk 'd'.
*/
static __always_inline uint8_t A(int p, int d)
{
return raid_gen[p][d];
}
/*
* Dereference as uint8_t
*/
#define v_8(p) (*(uint8_t*)&(p))
/*
* Dereference as uint32_t
*/
#define v_32(p) (*(uint32_t*)&(p))
/*
* Dereference as uint64_t
*/
#define v_64(p) (*(uint64_t*)&(p))
/*
* Multiply each byte of a uint32 by 2 in the GF(2^8).
*/
static __always_inline uint32_t x2_32(uint32_t v)
{
uint32_t mask = v & 0x80808080U;
mask = (mask << 1) - (mask >> 7);
v = (v << 1) & 0xfefefefeU;
v ^= mask & 0x1d1d1d1dU;
return v;
}
/*
* Multiply each byte of a uint64 by 2 in the GF(2^8).
*/
static __always_inline uint64_t x2_64(uint64_t v)
{
uint64_t mask = v & 0x8080808080808080ULL;
mask = (mask << 1) - (mask >> 7);
v = (v << 1) & 0xfefefefefefefefeULL;
v ^= mask & 0x1d1d1d1d1d1d1d1dULL;
return v;
}
/*
* Divide each byte of a uint32 by 2 in the GF(2^8).
*/
static __always_inline uint32_t d2_32(uint32_t v)
{
uint32_t mask = v & 0x01010101U;
mask = (mask << 8) - mask;
v = (v >> 1) & 0x7f7f7f7fU;
v ^= mask & 0x8e8e8e8eU;
return v;
}
/*
* Divide each byte of a uint64 by 2 in the GF(2^8).
*/
static __always_inline uint64_t d2_64(uint64_t v)
{
uint64_t mask = v & 0x0101010101010101ULL;
mask = (mask << 8) - mask;
v = (v >> 1) & 0x7f7f7f7f7f7f7f7fULL;
v ^= mask & 0x8e8e8e8e8e8e8e8eULL;
return v;
}
#endif

570
raid/int.c Normal file
View file

@ -0,0 +1,570 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#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<size;i+=8) {
p0 = v_32(v[l][i]);
p1 = v_32(v[l][i+4]);
/* accessing disks in backward order because the buffers */
/* are also in backward order */
for(d=l-1;d>=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<size;i+=16) {
p0 = v_64(v[l][i]);
p1 = v_64(v[l][i+8]);
/* accessing disks in backward order because the buffers */
/* are also in backward order */
for(d=l-1;d>=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<size;i+=8) {
q0 = p0 = v_32(v[l][i]);
q1 = p1 = v_32(v[l][i+4]);
for(d=l-1;d>=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<size;i+=16) {
q0 = p0 = v_64(v[l][i]);
q1 = p1 = v_64(v[l][i+8]);
for(d=l-1;d>=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;i<size;i+=1) {
p0 = q0 = r0 = 0;
for(d=l;d>0;--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;i<size;i+=1) {
p0 = q0 = r0 = s0 = 0;
for(d=l;d>0;--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;i<size;i+=1) {
p0 = q0 = r0 = s0 = t0 = 0;
for(d=l;d>0;--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;i<size;i+=1) {
p0 = q0 = r0 = s0 = t0 = u0 = 0;
for(d=l;d>0;--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<size;++i) {
/* delta */
uint8_t Pd = p[i] ^ pa[i];
/* reconstruct */
pa[i] = T[Pd];
}
}
/*
* Recover failure of two data blocks at indexes id[0],id[1] using parity at
* indexes ip[0],ip[1] for any RAID level.
*
* Starting from the equations:
*
* Pd = A[ip[0],id[0]] * Dx + A[ip[0],id[1]] * Dy
* Qd = A[ip[1],id[0]] * Dx + A[ip[1],id[1]] * Dy
*
* we solve inverting the coefficients matrix.
*/
void raid_rec2_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;
uint8_t* q;
uint8_t* qa;
const int N = 2;
const uint8_t* T[N][N];
uint8_t G[N*N];
uint8_t V[N*N];
size_t i;
int j, k;
(void)nr; /* unused, it's always 2 */
/* if it's RAID6 uses the dedicated and faster function */
if (ip[0] == 0 && ip[1] == 1) {
raid_rec2_par2(id, ip, nd, size, vv);
return;
}
/* setup the coefficients matrix */
for(j=0;j<N;++j) {
for(k=0;k<N;++k) {
G[j*N+k] = A(ip[j],id[k]);
}
}
/* invert it to solve the system of linear equations */
raid_invert(G, V, N);
/* get multiplication tables */
for(j=0;j<N;++j) {
for(k=0;k<N;++k) {
T[j][k] = table( V[j*N+k] );
}
}
/* compute delta parity */
raid_delta_gen(2, id, ip, nd, size, vv);
p = v[nd+ip[0]];
q = v[nd+ip[1]];
pa = v[id[0]];
qa = v[id[1]];
for(i=0;i<size;++i) {
/* delta */
uint8_t Pd = p[i] ^ pa[i];
uint8_t Qd = q[i] ^ qa[i];
/* reconstruct */
pa[i] = T[0][0][Pd] ^ T[0][1][Qd];
qa[i] = T[1][0][Pd] ^ T[1][1][Qd];
}
}
/*
* Recover failure of N data blocks at indexes id[N] using parity at indexes
* ip[N] for any RAID level.
*
* Starting from the N equations, with 0<=i<N :
*
* PD[i] = sum(A[ip[i],id[j]] * D[i]) 0<=j<N
*
* we solve inverting the coefficients matrix.
*
* Note that referring at previous equations you have:
* PD[0] = Pd, PD[1] = Qd, PD[2] = Rd, ...
* D[0] = Dx, D[1] = Dy, D[2] = Dz, ...
*/
void raid_recX_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[RAID_PARITY_MAX];
uint8_t* pa[RAID_PARITY_MAX];
const uint8_t* T[RAID_PARITY_MAX][RAID_PARITY_MAX];
uint8_t G[RAID_PARITY_MAX*RAID_PARITY_MAX];
uint8_t V[RAID_PARITY_MAX*RAID_PARITY_MAX];
size_t i;
int j, k;
/* setup the coefficients matrix */
for(j=0;j<nr;++j) {
for(k=0;k<nr;++k) {
G[j*nr+k] = A(ip[j],id[k]);
}
}
/* invert it to solve the system of linear equations */
raid_invert(G, V, nr);
/* get multiplication tables */
for(j=0;j<nr;++j) {
for(k=0;k<nr;++k) {
T[j][k] = table( V[j*nr+k] );
}
}
/* compute delta parity */
raid_delta_gen(nr, id, ip, nd, size, vv);
for(j=0;j<nr;++j) {
p[j] = v[nd+ip[j]];
pa[j] = v[id[j]];
}
for(i=0;i<size;++i) {
uint8_t PD[RAID_PARITY_MAX];
/* delta */
for(j=0;j<nr;++j)
PD[j] = p[j][i] ^ pa[j][i];
/* reconstruct */
for(j=0;j<nr;++j) {
uint8_t b = 0;
for(k=0;k<nr;++k)
b ^= T[j][k][PD[k]];
pa[j][i] = b;
}
}
}

107
raid/internal.h Normal file
View file

@ -0,0 +1,107 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __RAID_INTERNAL_H
#define __RAID_INTERNAL_H
#include "../portable.h"
#include "raid.h"
/*
* Internal functions.
*
* These are intented to provide access for testing.
*/
void raid_invert(uint8_t* M, uint8_t* V, int n);
void raid_delta_gen(int nr, const int* id, const int* ip, int nd, size_t size, void** v);
void raid_rec1_par1(const int* id, int nd, size_t size, void** v);
void raid_rec2_par2(const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_par1_int32(int nd, size_t size, void** vv);
void raid_par1_int64(int nd, size_t size, void** vv);
void raid_par1_sse2(int nd, size_t size, void** vv);
void raid_par2_int32(int nd, size_t size, void** vv);
void raid_par2_int64(int nd, size_t size, void** vv);
void raid_par2_sse2(int nd, size_t size, void** vv);
void raid_par2_sse2ext(int nd, size_t size, void** vv);
void raid_parz_int32(int nd, size_t size, void** vv);
void raid_parz_int64(int nd, size_t size, void** vv);
void raid_parz_sse2(int nd, size_t size, void** vv);
void raid_parz_sse2ext(int nd, size_t size, void** vv);
void raid_par3_int8(int nd, size_t size, void** vv);
void raid_par3_ssse3(int nd, size_t size, void** vv);
void raid_par3_ssse3ext(int nd, size_t size, void** vv);
void raid_par4_int8(int nd, size_t size, void** vv);
void raid_par4_ssse3(int nd, size_t size, void** vv);
void raid_par4_ssse3ext(int nd, size_t size, void** vv);
void raid_par5_int8(int nd, size_t size, void** vv);
void raid_par5_ssse3(int nd, size_t size, void** vv);
void raid_par5_ssse3ext(int nd, size_t size, void** vv);
void raid_par6_int8(int nd, size_t size, void** vv);
void raid_par6_ssse3(int nd, size_t size, void** vv);
void raid_par6_ssse3ext(int nd, size_t size, void** vv);
void raid_rec1_int8(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_rec2_int8(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_recX_int8(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_rec1_ssse3(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_rec2_ssse3(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_recX_ssse3(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
/*
* Internal naming.
*
* These are intented to provide access for testing.
*/
const char* raid_par1_tag(void);
const char* raid_par2_tag(void);
const char* raid_parz_tag(void);
const char* raid_par3_tag(void);
const char* raid_par4_tag(void);
const char* raid_par5_tag(void);
const char* raid_par6_tag(void);
const char* raid_rec1_tag(void);
const char* raid_rec2_tag(void);
const char* raid_recX_tag(void);
/*
* Internal forwarders.
*/
extern void (*raid_par_ptr[RAID_PARITY_MAX])(int nd, size_t size, void** vv);
extern void (*raid_par3_ptr)(int nd, size_t size, void** vv);
extern void (*raid_parz_ptr)(int nd, size_t size, void** vv);
extern void (*raid_rec_ptr[RAID_PARITY_MAX])(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
/*
* Tables.
*/
extern const uint8_t raid_gfmul[256][256] __attribute__((aligned(256)));
extern const uint8_t raid_gfexp[256] __attribute__((aligned(256)));
extern const uint8_t raid_gfinv[256] __attribute__((aligned(256)));
extern const uint8_t raid_gfvandermonde[3][256] __attribute__((aligned(256)));
extern const uint8_t raid_gfcauchy[6][256] __attribute__((aligned(256)));
extern const uint8_t raid_gfcauchypshufb[251][4][2][16] __attribute__((aligned(256)));
extern const uint8_t raid_gfmulpshufb[256][2][16] __attribute__((aligned(256)));
#define gfmul raid_gfmul
#define gfexp raid_gfexp
#define gfinv raid_gfinv
#define gfvandermonde raid_gfvandermonde
#define gfcauchy raid_gfcauchy
#define gfcauchypshufb raid_gfcauchypshufb
#define gfmulpshufb raid_gfmulpshufb
#endif

122
raid/intz.c Normal file
View file

@ -0,0 +1,122 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include "gf.h"
/*
* PARz (triple parity with powers of 2^-1) 32bit C implementation
*/
void raid_parz_int32(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;
uint32_t d0, r0, q0, p0;
uint32_t d1, r1, q1, p1;
l = nd - 1;
p = v[nd];
q = v[nd+1];
r = v[nd+2];
for(i=0;i<size;i+=8) {
r0 = q0 = p0 = v_32(v[l][i]);
r1 = q1 = p1 = v_32(v[l][i+4]);
for(d=l-1;d>=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;
r0 = d2_32(r0);
r1 = d2_32(r1);
r0 ^= d0;
r1 ^= d1;
}
v_32(p[i]) = p0;
v_32(p[i+4]) = p1;
v_32(q[i]) = q0;
v_32(q[i+4]) = q1;
v_32(r[i]) = r0;
v_32(r[i+4]) = r1;
}
}
/*
* PARz (triple parity with powers of 2^-1) 64bit C implementation
*/
void raid_parz_int64(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;
uint64_t d0, r0, q0, p0;
uint64_t d1, r1, q1, p1;
l = nd - 1;
p = v[nd];
q = v[nd+1];
r = v[nd+2];
for(i=0;i<size;i+=16) {
r0 = q0 = p0 = v_64(v[l][i]);
r1 = q1 = p1 = v_64(v[l][i+8]);
for(d=l-1;d>=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;
r0 = d2_64(r0);
r1 = d2_64(r1);
r0 ^= d0;
r1 ^= d1;
}
v_64(p[i]) = p0;
v_64(p[i+8]) = p1;
v_64(q[i]) = q0;
v_64(q[i+8]) = q1;
v_64(r[i]) = r0;
v_64(r[i+8]) = r1;
}
}

389
raid/mktables.c Normal file
View file

@ -0,0 +1,389 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
/**
* Multiplication in GF(2^8).
*/
static uint8_t gfmul(uint8_t a, uint8_t b)
{
uint8_t v;
v = 0;
while (b) {
if ((b & 1) != 0)
v ^= a;
if ((a & 0x80) != 0) {
a <<= 1;
a ^= 0x1d;
} else {
a <<= 1;
}
b >>= 1;
}
return v;
}
/**
* Inversion table in GF(2^8).
*/
uint8_t gfinv[256];
/**
* Number of parity.
* This is the number of rows of the generation matrix.
*/
#define PARITY 6
/**
* Number of disks.
* This is the number of columns of the generation matrix.
*/
#define DISK (257-PARITY)
/**
* Setup the Cauchy matrix used to generate the parity.
*/
static void set_cauchy(uint8_t* matrix)
{
int i, j;
uint8_t inv_x, y;
/*
* First row is formed by all 1.
*
* This is an Extended Cauchy matrix built from a Cauchy matrix
* adding the first row of all 1.
*/
for(i=0;i<DISK;++i) {
matrix[0*DISK+i] = 1;
}
/*
* Second row is formed by power of 2^i.
*
* This is the first row of the Cauchy matrix.
*
* Each element of the Cauchy matrix is in the form 1/(xi+yj)
* where all xi, and yj must be different.
*
* Choosing xi = 2^-i and y0 = 0, we obtain for the first row:
*
* 1/(xi+y0) = 1/(2^-i + 0) = 2^i
*
* with 2^-i != 0 for any i
*/
inv_x = 1;
for(i=0;i<DISK;++i) {
matrix[1*DISK+i] = inv_x;
inv_x = gfmul(2, inv_x);
}
/*
* Next rows of the Cauchy matrix.
*
* Continue forming the Cauchy matrix with yj = 2^j obtaining :
*
* 1/(xi+yj) = 1/(2^-i + 2^j)
*
* with xi != yj for any i,j with i>=0,j>=1,i+j<255
*/
y = 2;
for(j=0;j<PARITY-2;++j) {
inv_x = 1;
for(i=0;i<DISK;++i) {
uint8_t x = gfinv[inv_x];
matrix[(j+2)*DISK+i] = gfinv[y ^ x];
inv_x = gfmul(2, inv_x);
}
y = gfmul(2, y);
}
/*
* Adjusts the matrix multipling each row for
* the inverse of the first element in the row.
*
* This operation doesn't invalidate the property that all the square
* submatrices are not singular.
*/
for(j=0;j<PARITY-2;++j) {
uint8_t f = gfinv[matrix[(j+2)*DISK]];
for(i=0;i<DISK;++i) {
matrix[(j+2)*DISK+i] = gfmul(matrix[(j+2)*DISK+i], f);
}
}
}
/**
* Setup the Power matrix used to generate the parity.
*/
static void set_power(uint8_t* matrix)
{
unsigned i;
uint8_t v;
v = 1;
for(i=0;i<DISK;++i) {
matrix[0*DISK+i] = v;
}
v = 1;
for(i=0;i<DISK;++i) {
matrix[1*DISK+i] = v;
v = gfmul(2, v);
}
v = 1;
for(i=0;i<DISK;++i) {
matrix[2*DISK+i] = v;
v = gfmul(0x8e, v);
}
}
/**
* Next power of 2.
*/
static unsigned np(unsigned v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
++v;
return v;
}
int main(void)
{
uint8_t v;
int i, j, k, p;
uint8_t matrix[PARITY * 256];
printf("/*\n");
printf(" * Copyright (C) 2013 Andrea Mazzoleni\n");
printf(" *\n");
printf(" * This program is free software: you can redistribute it and/or modify\n");
printf(" * it under the terms of the GNU General Public License as published by\n");
printf(" * the Free Software Foundation, either version 2 of the License, or\n");
printf(" * (at your option) any later version.\n");
printf(" *\n");
printf(" * This program is distributed in the hope that it will be useful,\n");
printf(" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
printf(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
printf(" * GNU General Public License for more details.\n");
printf(" *\n");
printf(" * You should have received a copy of the GNU General Public License\n");
printf(" * along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
printf(" */\n");
printf("\n");
printf("#include \"internal.h\"\n");
printf("\n");
/* a*b */
printf("const uint8_t __attribute__((aligned(256))) raid_gfmul[256][256] =\n");
printf("{\n");
for(i=0;i<256;++i) {
printf("\t{\n");
for(j=0;j<256;++j) {
if (j % 8 == 0)
printf("\t\t");
v = gfmul(i, j);
if (v == 1)
gfinv[i] = j;
printf("0x%02x,", (unsigned)v);
if (j % 8 == 7)
printf("\n");
else
printf(" ");
}
printf("\t},\n");
}
printf("};\n\n");
/* 2^a */
printf("const uint8_t __attribute__((aligned(256))) raid_gfexp[256] =\n");
printf("{\n");
v = 1;
for(i=0;i<256;++i) {
if (i % 8 == 0)
printf("\t");
printf("0x%02x,", v);
v = gfmul(v, 2);
if (i % 8 == 7)
printf("\n");
else
printf(" ");
}
printf("};\n\n");
/* 1/a */
printf("const uint8_t __attribute__((aligned(256))) raid_gfinv[256] =\n");
printf("{\n");
printf("\t/* note that the first element is not significative */\n");
for(i=0;i<256;++i) {
if (i % 8 == 0)
printf("\t");
if (i == 0)
v = 0;
else
v = gfinv[i];
printf("0x%02x,", v);
if (i % 8 == 7)
printf("\n");
else
printf(" ");
}
printf("};\n\n");
/* power matrix */
set_power(matrix);
printf("/**\n");
printf(" * Power matrix used to generate parity.\n");
printf(" * This matrix is valid for up to %u parity with %u data disks.\n", 3, DISK);
printf(" *\n");
for(p=0;p<3;++p) {
printf(" * ");
for(i=0;i<DISK;++i) {
printf("%02x ", matrix[p*DISK+i]);
}
printf("\n");
}
printf(" */\n");
printf("const uint8_t __attribute__((aligned(256))) raid_gfvandermonde[%u][256] =\n", 3);
printf("{\n");
for(p=0;p<3;++p) {
printf("\t{\n");
for(i=0;i<DISK;++i) {
if (i % 8 == 0)
printf("\t\t");
printf("0x%02x,", matrix[p*DISK+i]);
if (i % 8 == 7)
printf("\n");
else
printf(" ");
}
printf("\n\t},\n");
}
printf("};\n\n");
/* cauchy matrix */
set_cauchy(matrix);
printf("/**\n");
printf(" * Cauchy matrix used to generate parity.\n");
printf(" * This matrix is valid for up to %u parity with %u data disks.\n", PARITY, DISK);
printf(" *\n");
for(p=0;p<PARITY;++p) {
printf(" * ");
for(i=0;i<DISK;++i) {
printf("%02x ", matrix[p*DISK+i]);
}
printf("\n");
}
printf(" */\n");
printf("const uint8_t __attribute__((aligned(256))) raid_gfcauchy[%u][256] =\n", PARITY);
printf("{\n");
for(p=0;p<PARITY;++p) {
printf("\t{\n");
for(i=0;i<DISK;++i) {
if (i % 8 == 0)
printf("\t\t");
printf("0x%02x,", matrix[p*DISK+i]);
if (i % 8 == 7)
printf("\n");
else
printf(" ");
}
printf("\n\t},\n");
}
printf("};\n\n");
printf("#if defined(__i386__) || defined(__x86_64__)\n");
printf("/**\n");
printf(" * PSHUFB tables for the Cauchy matrix.\n");
printf(" *\n");
printf(" * Indexes are [DISK][PARITY - 2][LH].\n");
printf(" * Where DISK is from 0 to %u, PARITY from 2 to %u, LH from 0 to 1.\n", DISK - 1, PARITY - 1);
printf(" */\n");
printf("const uint8_t __attribute__((aligned(256))) raid_gfcauchypshufb[%u][%u][2][16] =\n", DISK, np(PARITY - 2));
printf("{\n");
for(i=0;i<DISK;++i) {
printf("\t{\n");
for(p=2;p<PARITY;++p) {
printf("\t\t{\n");
for(j=0;j<2;++j) {
printf("\t\t\t{ ");
for(k=0;k<16;++k) {
v = gfmul(matrix[p*DISK+i], k);
if (j == 1)
v = gfmul(v, 16);
printf("0x%02x", (unsigned)v);
if (k != 15)
printf(", ");
}
printf(" },\n");
}
printf("\t\t},\n");
}
printf("\t},\n");
}
printf("};\n");
printf("#endif\n\n");
printf("#if defined(__i386__) || defined(__x86_64__)\n");
printf("/**\n");
printf(" * PSHUFB tables for generic multiplication.\n");
printf(" *\n");
printf(" * Indexes are [MULTIPLER][LH].\n");
printf(" * Where MULTIPLER is from 0 to 255, LH from 0 to 1.\n");
printf(" */\n");
printf("const uint8_t __attribute__((aligned(256))) raid_gfmulpshufb[256][2][16] =\n");
printf("{\n");
for(i=0;i<256;++i) {
printf("\t{\n");
for(j=0;j<2;++j) {
printf("\t\t{ ");
for(k=0;k<16;++k) {
v = gfmul(i, k);
if (j == 1)
v = gfmul(v, 16);
printf("0x%02x", (unsigned)v);
if (k != 15)
printf(", ");
}
printf(" },\n");
}
printf("\t},\n");
}
printf("};\n");
printf("#endif\n\n");
return 0;
}

189
raid/module.c Normal file
View file

@ -0,0 +1,189 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include "cpu.h"
void raid_init(void)
{
raid_par3_ptr = raid_par3_int8;
raid_par_ptr[3] = raid_par4_int8;
raid_par_ptr[4] = raid_par5_int8;
raid_par_ptr[5] = raid_par6_int8;
if (sizeof(void*) == 4) {
raid_par_ptr[0] = raid_par1_int32;
raid_par_ptr[1] = raid_par2_int32;
raid_parz_ptr = raid_parz_int32;
} else {
raid_par_ptr[0] = raid_par1_int64;
raid_par_ptr[1] = raid_par2_int64;
raid_parz_ptr = raid_parz_int64;
}
raid_rec_ptr[0] = raid_rec1_int8;
raid_rec_ptr[1] = raid_rec2_int8;
raid_rec_ptr[2] = raid_recX_int8;
raid_rec_ptr[3] = raid_recX_int8;
raid_rec_ptr[4] = raid_recX_int8;
raid_rec_ptr[5] = raid_recX_int8;
#if defined(__i386__) || defined(__x86_64__)
if (raid_cpu_has_sse2()) {
raid_par_ptr[0] = raid_par1_sse2;
#if defined(__x86_64__)
if (raid_cpu_has_slowextendedreg()) {
raid_par_ptr[1] = raid_par2_sse2;
raid_parz_ptr = raid_parz_sse2;
} else {
raid_par_ptr[1] = raid_par2_sse2ext;
raid_parz_ptr = raid_parz_sse2ext;
}
#else
raid_par_ptr[1] = raid_par2_sse2;
raid_parz_ptr = raid_parz_sse2;
#endif
}
if (raid_cpu_has_ssse3()) {
#if defined(__x86_64__)
raid_par3_ptr = raid_par3_ssse3ext;
raid_par_ptr[3] = raid_par4_ssse3ext;
raid_par_ptr[4] = raid_par5_ssse3ext;
raid_par_ptr[5] = raid_par6_ssse3ext;
#else
raid_par3_ptr = raid_par3_ssse3;
raid_par_ptr[3] = raid_par4_ssse3;
raid_par_ptr[4] = raid_par5_ssse3;
raid_par_ptr[5] = raid_par6_ssse3;
#endif
raid_rec_ptr[0] = raid_rec1_ssse3;
raid_rec_ptr[1] = raid_rec2_ssse3;
raid_rec_ptr[2] = raid_recX_ssse3;
raid_rec_ptr[3] = raid_recX_ssse3;
raid_rec_ptr[4] = raid_recX_ssse3;
raid_rec_ptr[5] = raid_recX_ssse3;
}
#endif
/* set the default mode */
raid_mode(RAID_MODE_CAUCHY);
}
static struct raid_func {
const char* name;
void* p;
} RAID_FUNC[] = {
{ "int8", raid_par3_int8 },
{ "int8", raid_par4_int8 },
{ "int8", raid_par5_int8 },
{ "int8", raid_par6_int8 },
{ "int32", raid_par1_int32 },
{ "int64", raid_par1_int64 },
{ "int32", raid_par2_int32 },
{ "int64", raid_par2_int64 },
{ "int32", raid_parz_int32 },
{ "int64", raid_parz_int64 },
{ "int8", raid_rec1_int8 },
{ "int8", raid_rec2_int8 },
{ "int8", raid_recX_int8 },
#if defined(__i386__) || defined(__x86_64__)
{ "sse2", raid_par1_sse2 },
{ "sse2", raid_par2_sse2 },
{ "sse2", raid_parz_sse2 },
{ "ssse3", raid_par3_ssse3 },
{ "ssse3", raid_par4_ssse3 },
{ "ssse3", raid_par5_ssse3 },
{ "ssse3", raid_par6_ssse3 },
{ "ssse3", raid_rec1_ssse3 },
{ "ssse3", raid_rec2_ssse3 },
{ "ssse3", raid_recX_ssse3 },
#endif
#if defined(__x86_64__)
{ "sse2e", raid_par2_sse2ext },
{ "sse2e", raid_parz_sse2ext },
{ "ssse3e", raid_par3_ssse3ext },
{ "ssse3e", raid_par4_ssse3ext },
{ "ssse3e", raid_par5_ssse3ext },
{ "ssse3e", raid_par6_ssse3ext },
#endif
{ 0, 0 }
};
static const char* raid_tag(void* func)
{
struct raid_func* i = RAID_FUNC;
while (i->name != 0) {
if (i->p == func)
return i->name;
++i;
}
return "unknown";
}
const char* raid_par1_tag(void)
{
return raid_tag(raid_par_ptr[0]);
}
const char* raid_par2_tag(void)
{
return raid_tag(raid_par_ptr[1]);
}
const char* raid_parz_tag(void)
{
return raid_tag(raid_parz_ptr);
}
const char* raid_par3_tag(void)
{
return raid_tag(raid_par_ptr[2]);
}
const char* raid_par4_tag(void)
{
return raid_tag(raid_par_ptr[3]);
}
const char* raid_par5_tag(void)
{
return raid_tag(raid_par_ptr[4]);
}
const char* raid_par6_tag(void)
{
return raid_tag(raid_par_ptr[5]);
}
const char* raid_rec1_tag(void)
{
return raid_tag(raid_rec_ptr[0]);
}
const char* raid_rec2_tag(void)
{
return raid_tag(raid_rec_ptr[1]);
}
const char* raid_recX_tag(void)
{
return raid_tag(raid_rec_ptr[2]);
}

458
raid/raid.c Normal file
View file

@ -0,0 +1,458 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
/*
* Copyright notes:
*
* The RAID5 and RAID6 support was originally derived from the
* H. Peter Anvin paper "The mathematics of RAID-6" [1] and the
* libraid6 library, also by H. Peter Anvin, released with license
* "GPLv2 or any later version" inside the Linux Kernel 2.6.38.
*
* This support was later completely rewritten (many times), but
* the H. Peter Anvin's Copyright may still apply.
*
* The others RAID levels and the recovering based on matrix
* inversion is original work implemented from scratch.
*/
/*
* The RAID5 and RAID6 support is implemented using the Galois Field
* GF(2^8) with the primitive polynomial x^8 + x^4 + x^3 + x^2 + 1
* (285 decimal).
*
* The parity P and Q of a set of N disk Di with 0<=i<N, is computed
* using the equations:
*
* P = sum(Di)
* Q = sum(2^i * Di) with 0<=i<N
*
* This approach is the same used by the Linux Kernel RAID, and by ZFS
* RAIDZ2, better described in the H. Peter Anvin paper "The mathematics
* of RAID-6" [1].
*
* To support triple parity, it was first evaluated and then dropped, an
* extension of the same approach, with additional parity coefficients set
* as powers of 2^-1, with equations:
*
* P = sum(Di)
* Q = sum(2^i * Di)
* R = sum(2^-i * Di) with 0<=i<N
*
* This approach works well for triple parity and it's very efficient,
* because we can implement very fast parallel multiplications and
* divisions by 2 in GF(2^8).
*
* It's also similar at the approach used by ZFS RAIDZ3, with the
* difference that ZFS uses powers of 4 instead of 2^-1.
*
* Unfortunately it doesn't work beyond triple parity, because whatever
* value we choose to generate the power coefficients to compute other
* parities, the resulting equations are not solvable for some
* combinations of missing disks.
*
* This is expected, because the Vandermonde matrix used to compute the
* parity has no guarantee to have all submatrices not singular
* [2, Chap 11, Problem 7] and this is a requirement to have
* a MDS (Maximum Distance Separable) code [2, Chap 11, Theorem 8].
*
* To overcome this limitation, we use a Cauchy matrix [3][4] to compute
* the parity. A Cauchy matrix has the property to have all the square
* submatrices not singular, resulting in always solvable equations,
* for any combination of missing disks.
*
* The problem of this approach is that it requires the use of
* generic multiplications, and not only by 2 or 2^-1, potentially
* affecting badly the performance.
*
* Hopefully there is a method to implement parallel multiplications
* using SSSE3 instructions [1][5]. Method competitive with the
* computation of triple parity using power coefficients.
*
* Another important property of the Cauchy matrix is that we can setup
* the first two rows with coeffients equal at the RAID5 and RAID6 approach
* decribed, resulting in a compatible extension, and requiring SSSE3
* instructions only if triple parity or beyond is used.
*
* The matrix is also adjusted, multipling each row by a constant factor
* to make the first column of all 1, to optimize the computation for
* the first disk.
*
* This results in the matrix A[row,col] defined as:
*
* 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01...
* 01 02 04 08 10 20 40 80 1d 3a 74 e8 cd 87 13 26 4c 98 2d 5a b4 75...
* 01 f5 d2 c4 9a 71 f1 7f fc 87 c1 c6 19 2f 40 55 3d ba 53 04 9c 61...
* 01 bb a6 d7 c7 07 ce 82 4a 2f a5 9b b6 60 f1 ad e7 f4 06 d2 df 2e...
* 01 97 7f 9c 7c 18 bd a2 58 1a da 74 70 a3 e5 47 29 07 f5 80 23 e9...
* 01 2b 3f cf 73 2c d6 ed cb 74 15 78 8a c1 17 c9 89 68 21 ab 76 3b...
* (see tables.h for the matrix with all the columns)
*
* This matrix supports 6 level of parity, one for each row, for up to 251
* data disks, one for each column, with all the 377,342,351,231 square
* submatrices not singular, verified also with brute-force.
*
* This matrix can be extended to support any number of parities, just
* adding additional rows, and removing one column for each new row.
* (see mktables.c for more details in how the matrix is generated)
*
* In details, parity is computed as:
*
* P = sum(Di)
* Q = sum(2^i * Di)
* R = sum(A[2,i] * Di)
* S = sum(A[3,i] * Di)
* T = sum(A[4,i] * Di)
* U = sum(A[5,i] * Di) with 0<=i<N
*
* To recover from a failure of six disks at indexes x,y,z,h,v,w,
* with 0<=x<y<z<h<v<w<N, we compute the parity of the available N-6
* disks as:
*
* Pa = sum(Di)
* Qa = sum(2^i * Di)
* Ra = sum(A[2,i] * Di)
* Sa = sum(A[3,i] * Di)
* Ta = sum(A[4,i] * Di)
* Ua = sum(A[5,i] * Di) with 0<=i<N,i!=x,i!=y,i!=z,i!=h,i!=v,i!=w.
*
* And if we define:
*
* Pd = Pa + P
* Qd = Qa + Q
* Rd = Ra + R
* Sd = Sa + S
* Td = Ta + T
* Ud = Ua + U
*
* we can sum these two sets of equations, obtaining:
*
* Pd = Dx + Dy + Dz + Dh + Dv + Dw
* Qd = 2^x * Dx + 2^y * Dy + 2^z * Dz + 2^h * Dh + 2^v * Dv + 2^w * Dw
* Rd = A[2,x] * Dx + A[2,y] * Dy + A[2,z] * Dz + A[2,h] * Dh + A[2,v] * Dv + A[2,w] * Dw
* Sd = A[3,x] * Dx + A[3,y] * Dy + A[3,z] * Dz + A[3,h] * Dh + A[3,v] * Dv + A[3,w] * Dw
* Td = A[4,x] * Dx + A[4,y] * Dy + A[4,z] * Dz + A[4,h] * Dh + A[4,v] * Dv + A[4,w] * Dw
* Ud = A[5,x] * Dx + A[5,y] * Dy + A[5,z] * Dz + A[5,h] * Dh + A[5,v] * Dv + A[5,w] * Dw
*
* A linear system always solvable because the coefficients matrix is
* always not singular due the properties of the matrix A[].
*
* Resulting speed in x64, with 8 data disks, using a stripe of 256 KiB,
* for a Core i7-3740QM CPU @ 2.7GHz is:
*
* int8 int32 int64 sse2 sse2e ssse3 ssse3e
* par1 11927 22075 36004
* par2 3378 5874 18235 19164
* par3 844 8814 9419
* par4 665 6836 7415
* par5 537 5388 5686
* par6 449 4307 4789
*
* Values are in MiB/s of data processed, not counting generated parity.
*
* You can replicate these results in your machine using the "snapraid -T"
* command.
*
* For comparison, the triple parity computation using the power
* coeffients "1,2,2^-1" is only a little faster than the one based on
* the Cauchy matrix if SSSE3 is present.
*
* int8 int32 int64 sse2 sse2e ssse3 ssse3e
* parz 2112 3118 9589 10304
*
*
* In conclusion, the use of power coefficients, and specifically powers
* of 1,2,2^-1, is the best option to implement triple parity in CPUs
* without SSSE3.
* But if a modern CPU with SSSE3 (or similar) is available, the Cauchy
* matrix is the best option because it provides a fast and general
* approach working for any number of parities.
*
* References:
* [1] Anvin, "The mathematics of RAID-6", 2004
* [2] MacWilliams, Sloane, "The Theory of Error-Correcting Codes", 1977
* [3] Blomer, "An XOR-Based Erasure-Resilient Coding Scheme", 1995
* [4] Roth, "Introduction to Coding Theory", 2006
* [5] Plank, "Screaming Fast Galois Field Arithmetic Using Intel SIMD Instructions", 2013
*/
#include "internal.h"
#include "gf.h"
/**
* Generator matrix currently used.
*/
const uint8_t (*raid_gen)[256];
void raid_mode(int mode)
{
if (mode == RAID_MODE_VANDERMONDE) {
raid_par_ptr[2] = raid_parz_ptr;
raid_gen = gfvandermonde;
} else {
raid_par_ptr[2] = raid_par3_ptr;
raid_gen = gfcauchy;
}
}
/**
* Buffer filled with 0 used in recovering.
*/
static void* raid_empty_zero_block;
void raid_zero(void* zero)
{
raid_empty_zero_block = zero;
}
/* internal forwarder */
void (*raid_par_ptr[RAID_PARITY_MAX])(int nd, size_t size, void** vv);
void (*raid_par3_ptr)(int nd, size_t size, void** vv);
void (*raid_parz_ptr)(int nd, size_t size, void** vv);
void raid_par(int np, int nd, size_t size, void** v)
{
BUG_ON(np < 1 || np > RAID_PARITY_MAX);
raid_par_ptr[np - 1](nd, size, v);
}
/**
* Inverts the square matrix M of size nxn into V.
* We use Gauss elimination to invert.
*/
void raid_invert(uint8_t* M, uint8_t* V, int n)
{
int i,j,k;
/* set the identity matrix in V */
for(i=0;i<n;++i) {
for(j=0;j<n;++j) {
V[i*n+j] = i == j;
}
}
/* for each element in the diagonal */
for(k=0;k<n;++k) {
uint8_t f;
/* the diagonal element cannot be 0 because */
/* we are inverting matrices with all the square submatrices */
/* not singular */
BUG_ON(M[k*n+k] == 0);
/* make the diagonal element to be 1 */
f = inv(M[k*n+k]);
for(j=0;j<n;++j) {
M[k*n+j] = mul(f, M[k*n+j]);
V[k*n+j] = mul(f, V[k*n+j]);
}
/* make all the elements over and under the diagonal to be 0 */
for(i=0;i<n;++i) {
if (i == k)
continue;
f = M[i*n+k];
for(j=0;j<n;++j) {
M[i*n+j] ^= mul(f, M[k*n+j]);
V[i*n+j] ^= mul(f, V[k*n+j]);
}
}
}
}
/**
* Computes the parity without the missing data blocks
* and store it in the buffers of such data blocks.
*
* This is the parity expressed as Pa,Qa,Ra,Sa,Ta,Ua
* in the equations.
*
* Note that all the other parities not in the ip[] vector
* are destroyed.
*/
void raid_delta_gen(int nr, const int* id, const int* ip, int nd, size_t size, void** v)
{
void* p[RAID_PARITY_MAX];
void* pa[RAID_PARITY_MAX];
int i;
for(i=0;i<nr;++i) {
/* keep a copy of the parity buffer */
p[i] = v[nd+ip[i]];
/* buffer for missing data blocks */
pa[i] = v[id[i]];
/* set at zero the missing data blocks */
v[id[i]] = raid_empty_zero_block;
/* compute the parity over the missing data blocks */
v[nd+ip[i]] = pa[i];
}
/* recompute the minimal parity required */
raid_par(ip[nr - 1] + 1, nd, size, v);
for(i=0;i<nr;++i) {
/* restore disk buffers as before */
v[id[i]] = pa[i];
/* restore parity buffers as before */
v[nd+ip[i]] = p[i];
}
}
/**
* Recover failure of one data block for PAR1.
*
* Starting from the equation:
*
* Pd = Dx
*
* and solving we get:
*
* Dx = Pd
*/
void raid_rec1_par1(const int* id, int nd, size_t size, void** v)
{
void* p;
void* pa;
/* for PAR1 we can directly compute the missing block */
/* and we don't need to use the zero buffer */
p = v[nd];
pa = v[id[0]];
/* use the parity as missing data block */
v[id[0]] = p;
/* compute the parity over the missing data block */
v[nd] = pa;
/* compute */
raid_par(1, nd, size, v);
/* restore as before */
v[id[0]] = pa;
v[nd] = p;
}
/**
* Recover failure of two data blocks for PAR2.
*
* Starting from the equations:
*
* Pd = Dx + Dy
* Qd = 2^id[0] * Dx + 2^id[1] * Dy
*
* and solving we get:
*
* 1 2^(-id[0])
* Dy = ------------------- * Pd + ------------------- * Qd
* 2^(id[1]-id[0]) + 1 2^(id[1]-id[0]) + 1
*
* Dx = Dy + Pd
*
* with conditions:
*
* 2^id[0] != 0
* 2^(id[1]-id[0]) + 1 != 0
*
* That are always satisfied for any 0<=id[0]<id[1]<255.
*/
void raid_rec2_par2(const int* id, const int* ip, int nd, size_t size, void** vv)
{
uint8_t** v = (uint8_t**)vv;
size_t i;
uint8_t* p;
uint8_t* pa;
uint8_t* q;
uint8_t* qa;
const uint8_t* T[2];
/* get multiplication tables */
T[0] = table( inv(pow2(id[1]-id[0]) ^ 1) );
T[1] = table( inv(pow2(id[0]) ^ pow2(id[1])) );
/* compute delta parity */
raid_delta_gen(2, id, ip, nd, size, vv);
p = v[nd];
q = v[nd+1];
pa = v[id[0]];
qa = v[id[1]];
for(i=0;i<size;++i) {
/* delta */
uint8_t Pd = p[i] ^ pa[i];
uint8_t Qd = q[i] ^ qa[i];
/* reconstruct */
uint8_t Dy = T[0][Pd] ^ T[1][Qd];
uint8_t Dx = Pd ^ Dy;
/* set */
pa[i] = Dx;
qa[i] = Dy;
}
}
/* internal forwarder */
void (*raid_rec_ptr[RAID_PARITY_MAX])(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
void raid_rec(int nr, const int* id, const int* ip, int nd, size_t size, void** v)
{
BUG_ON(nr > nd);
/* if failed data is present */
if (nr != 0) {
raid_rec_ptr[nr - 1](nr, id, ip, nd, size, v);
}
}
void raid_recpar(int nrd, const int* id, int nrp, int* ip, int np, int nd, size_t size, void** v)
{
BUG_ON(nrd > nd);
BUG_ON(nrd + nrp > np);
/* if failed data is present */
if (nrd != 0) {
int iu[RAID_PARITY_MAX];
int i, j, k;
/* setup the vector of parities to use */
for(i=0,j=0,k=0;i<np;++i) {
if (ip[j] == i) {
++j;
} else {
iu[k] = i;
++k;
}
}
/* recover the data, and limit the parity use to needed ones */
raid_rec_ptr[nrd - 1](nrd, id, iu, nd, size, v);
}
/* recompute all the parities up to the last bad one */
if (nrp != 0) {
raid_par(ip[nrp - 1] + 1, nd, size, v);
}
}

134
raid/raid.h Normal file
View file

@ -0,0 +1,134 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __RAID_H
#define __RAID_H
/**
* RAID mode supporting up to 6 parities.
*
* It requires SSSE3 to get good performance with triple or more
* parities.
*
* This is the default mode used by SnapRAID.
*/
#define RAID_MODE_CAUCHY 0
/**
* RAID mode supporting up to 3 parities,
*
* It has a fast triple parity implementation even without SSSE3, but it
* cannot go beyond it.
* This is mostly intended for low end CPUs like ARM and AMD Athlon.
*/
#define RAID_MODE_VANDERMONDE 1
/**
* Max level of parity supported in the default mode RAID_MODE_CAUCHY.
*/
#define RAID_PARITY_MAX 6
/**
* Maximum number of data disks.
* Limit of the parity generator matrix used.
*/
#define RAID_DATA_MAX 251
/**
* Initializes the RAID system.
*/
void raid_init(void);
/**
* Sets the mode to use. One of RAID_MODE_*.
*
* You can change mode at any time, and it will affect next calls to raid_par(),
* raid_rec() and raid_recpar().
*
* The two modes are compatible for the first two levels of parity.
* The third one is different.
*/
void raid_mode(int mode);
/**
* Sets the zero buffer to use in recovering.
*
* Before calling raid_rec() and raid_recpar() you must provide a memory
* buffer filled with zero with the same size of the blocks to recover.
*/
void raid_zero(void* zero);
/**
* Computes the parity.
*
* \param np Number of parities to compute.
* \param nd Number of data disks.
* \param size Size of the blocks pointed by vv. It must be a multipler of 64.
* \param vv Vector of pointers to the blocks for disks and parities.
* It has (::nd + ::np) elements.
* Each element points to a buffer of ::size bytes.
*/
void raid_par(int np, int nd, size_t size, void** vv);
/**
* Recovers failures of data disks using the specified parities.
*
* Only the data blocks are recovered. The parity buffers are destroyed.
* If you want also to recompute parities, you can use raid_recpar().
*
* \param nr Number of failed data disks to recover.
* \param id[] Vector of ::nr indexes of the data disks to recover.
* The indexes start from 0. They must be in order.
* \param ip[] Vector of ::nr indexes of the parity disks to use for recovering.
* The indexes start from 0. They must be in order.
* \param nd Number of data disks.
* \param size Size of the blocks pointed by vv. It must be a multipler of 64.
* \param vv Vector of pointers to the blocks for disks and parities.
* It has (::nd + ::ip[::nr - 1] + 1) elements. The first elements are the blocks
* for data, following with the parity blocks. Each blocks has ::size bytes.
*/
void raid_rec(int nr, const int* id, const int* ip, int nd, size_t size, void** vv);
/**
* Recovers failures of data and parity disks.
*
* It's similar at raid_rec() but it also recovers parities.
*
* In case there are more parities available than needed, ones to use are
* automatically selected, excluding the ones specified in the ::ip vector.
*
* Ensure to have ::nrd + ::nrp <= ::np, otherwise recovering is not possible.
*
* \param nrd Number of failed data disks to recover.
* \param id[] Vector of ::nrd indexes of the data disks to recover.
* The indexes start from 0. They must be in order.
* \param nrp Number of failed parity disks to recover.
* \param ip[] Vector of ::nrp indexes of the parity disks to recover.
* The indexes start from 0. They must be in order.
* Note that as difference than raid_rec() these are the failed parities.
* All the parities not specified here are assumed correct, and they are
* not recomputed.
* \param np Number of parity disks.
* \param nd Number of data disks.
* \param size Size of the blocks pointed by vv. It must be a multipler of 64.
* \param vv Vector of pointers to the blocks for disks and parities.
* It has (::nd + ::np) elements. The first elements are the blocks
* for data, following with the parity blocks. Each blocks has ::size bytes.
*/
void raid_recpar(int nrd, const int* id, int nrp, int* ip, int np, int nd, size_t size, void** vv);
#endif

14699
raid/tables.c Normal file

File diff suppressed because it is too large Load diff

1540
raid/x86.c Normal file

File diff suppressed because it is too large Load diff

165
raid/x86z.c Normal file
View file

@ -0,0 +1,165 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "internal.h"
#if defined(__i386__) || defined(__x86_64__)
static const struct gfzconst16 {
uint8_t poly[16];
uint8_t half[16];
uint8_t low7[16];
} gfzconst16 __attribute__((aligned(64))) = {
{ 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d },
{ 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e },
{ 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f }
};
/*
* PARz (triple parity with powers of 2^-1) SSE2 implementation
*/
void raid_parz_sse2(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;
l = nd - 1;
p = v[nd];
q = v[nd+1];
r = v[nd+2];
asm volatile("movdqa %0,%%xmm7" : : "m" (gfzconst16.poly[0]));
asm volatile("movdqa %0,%%xmm3" : : "m" (gfzconst16.half[0]));
asm volatile("movdqa %0,%%xmm6" : : "m" (gfzconst16.low7[0]));
for(i=0;i<size;i+=16) {
asm volatile("movdqa %0,%%xmm0" : : "m" (v[l][i]));
asm volatile("movdqa %xmm0,%xmm1");
asm volatile("movdqa %xmm0,%xmm2");
for(d=l-1;d>=0;--d) {
asm volatile("pxor %xmm4,%xmm4");
asm volatile("pcmpgtb %xmm1,%xmm4");
asm volatile("paddb %xmm1,%xmm1");
asm volatile("pand %xmm7,%xmm4");
asm volatile("pxor %xmm4,%xmm1");
asm volatile("movdqa %xmm2,%xmm4");
asm volatile("pxor %xmm5,%xmm5");
asm volatile("psllw $7,%xmm4");
asm volatile("psrlw $1,%xmm2");
asm volatile("pcmpgtb %xmm4,%xmm5");
asm volatile("pand %xmm6,%xmm2");
asm volatile("pand %xmm3,%xmm5");
asm volatile("pxor %xmm5,%xmm2");
asm volatile("movdqa %0,%%xmm4" : : "m" (v[d][i]));
asm volatile("pxor %xmm4,%xmm0");
asm volatile("pxor %xmm4,%xmm1");
asm volatile("pxor %xmm4,%xmm2");
}
asm volatile("movntdq %%xmm0,%0" : "=m" (p[i]));
asm volatile("movntdq %%xmm1,%0" : "=m" (q[i]));
asm volatile("movntdq %%xmm2,%0" : "=m" (r[i]));
}
asm volatile("sfence" : : : "memory");
}
#endif
#if defined(__x86_64__)
/*
* PARz (triple parity with powers of 2^-1) SSE2 implementation
*
* Note that it uses 16 registers, meaning that x64 is required.
*/
void raid_parz_sse2ext(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;
l = nd - 1;
p = v[nd];
q = v[nd+1];
r = v[nd+2];
asm volatile("movdqa %0,%%xmm7" : : "m" (gfzconst16.poly[0]));
asm volatile("movdqa %0,%%xmm3" : : "m" (gfzconst16.half[0]));
asm volatile("movdqa %0,%%xmm11" : : "m" (gfzconst16.low7[0]));
for(i=0;i<size;i+=32) {
asm volatile("movdqa %0,%%xmm0" : : "m" (v[l][i]));
asm volatile("movdqa %0,%%xmm8" : : "m" (v[l][i+16]));
asm volatile("movdqa %xmm0,%xmm1");
asm volatile("movdqa %xmm8,%xmm9");
asm volatile("movdqa %xmm0,%xmm2");
asm volatile("movdqa %xmm8,%xmm10");
for(d=l-1;d>=0;--d) {
asm volatile("movdqa %xmm2,%xmm6");
asm volatile("movdqa %xmm10,%xmm14");
asm volatile("pxor %xmm4,%xmm4");
asm volatile("pxor %xmm12,%xmm12");
asm volatile("pxor %xmm5,%xmm5");
asm volatile("pxor %xmm13,%xmm13");
asm volatile("psllw $7,%xmm6");
asm volatile("psllw $7,%xmm14");
asm volatile("psrlw $1,%xmm2");
asm volatile("psrlw $1,%xmm10");
asm volatile("pcmpgtb %xmm1,%xmm4");
asm volatile("pcmpgtb %xmm9,%xmm12");
asm volatile("pcmpgtb %xmm6,%xmm5");
asm volatile("pcmpgtb %xmm14,%xmm13");
asm volatile("paddb %xmm1,%xmm1");
asm volatile("paddb %xmm9,%xmm9");
asm volatile("pand %xmm11,%xmm2");
asm volatile("pand %xmm11,%xmm10");
asm volatile("pand %xmm7,%xmm4");
asm volatile("pand %xmm7,%xmm12");
asm volatile("pand %xmm3,%xmm5");
asm volatile("pand %xmm3,%xmm13");
asm volatile("pxor %xmm4,%xmm1");
asm volatile("pxor %xmm12,%xmm9");
asm volatile("pxor %xmm5,%xmm2");
asm volatile("pxor %xmm13,%xmm10");
asm volatile("movdqa %0,%%xmm4" : : "m" (v[d][i]));
asm volatile("movdqa %0,%%xmm12" : : "m" (v[d][i+16]));
asm volatile("pxor %xmm4,%xmm0");
asm volatile("pxor %xmm4,%xmm1");
asm volatile("pxor %xmm4,%xmm2");
asm volatile("pxor %xmm12,%xmm8");
asm volatile("pxor %xmm12,%xmm9");
asm volatile("pxor %xmm12,%xmm10");
}
asm volatile("movntdq %%xmm0,%0" : "=m" (p[i]));
asm volatile("movntdq %%xmm8,%0" : "=m" (p[i+16]));
asm volatile("movntdq %%xmm1,%0" : "=m" (q[i]));
asm volatile("movntdq %%xmm9,%0" : "=m" (q[i+16]));
asm volatile("movntdq %%xmm2,%0" : "=m" (r[i]));
asm volatile("movntdq %%xmm10,%0" : "=m" (r[i+16]));
}
asm volatile("sfence" : : : "memory");
}
#endif