Whamcloud - gitweb
LU-12189 ec: code to add support for M to N parity
[fs/lustre-release.git] / lustre / ec / ec_base.c
1 // SPDX-License-Identifier: BSD-2-Clause
2 /**********************************************************************
3  * Copyright(c) 2011-2015 Intel Corporation All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *    Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *    Neither the name of Intel Corporation nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <linux/limits.h>
32 #include <linux/string.h>       /* for memset */
33 #include <libcfs/libcfs.h>
34 #include "erasure_code.h"
35
36 /* Global GF(256) tables */
37 static const unsigned char gff_base[] = {
38         0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a,
39         0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26, 0x4c, 0x98, 0x2d, 0x5a,
40         0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30,
41         0x60, 0xc0, 0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35,
42         0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23, 0x46, 0x8c,
43         0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2,
44         0xb9, 0x6f, 0xde, 0xa1, 0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f,
45         0x5e, 0xbc, 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0,
46         0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1,
47         0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2, 0xd9, 0xaf, 0x43, 0x86,
48         0x11, 0x22, 0x44, 0x88, 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd,
49         0x67, 0xce, 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93,
50         0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, 0x85, 0x17,
51         0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42,
52         0x84, 0x15, 0x2a, 0x54, 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4,
53         0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73,
54         0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5,
55         0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 0xe3, 0xdb, 0xab, 0x4b,
56         0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57,
57         0xae, 0x41, 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e,
58         0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, 0x51, 0xa2,
59         0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56,
60         0xac, 0x45, 0x8a, 0x09, 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a,
61         0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
62         0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36,
63         0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01
64 };
65
66 static const unsigned char gflog_base[] = {
67         0x00, 0xff, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf,
68         0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, 0x04, 0x64, 0xe0, 0x0e,
69         0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08,
70         0x4c, 0x71, 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21,
71         0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, 0x1d, 0xb5,
72         0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78,
73         0x4d, 0xe4, 0x72, 0xa6, 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd,
74         0x30, 0xfd, 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88,
75         0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2,
76         0x13, 0x5c, 0x83, 0x38, 0x46, 0x40, 0x1e, 0x42, 0xb6, 0xa3,
77         0xc3, 0x48, 0x7e, 0x6e, 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85,
78         0xba, 0x3d, 0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b,
79         0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57, 0x07, 0x70,
80         0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, 0x67, 0x4a, 0xde, 0xed,
81         0x31, 0xc5, 0xfe, 0x18, 0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8,
82         0xb4, 0x7c, 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e,
83         0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, 0x90, 0x87,
84         0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61, 0xf2, 0x56, 0xd3, 0xab,
85         0x14, 0x2a, 0x5d, 0x9e, 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d,
86         0x41, 0xa2, 0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76,
87         0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6, 0x6c, 0xa1,
88         0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, 0xfb, 0x60, 0x86, 0xb1,
89         0xbb, 0xcc, 0x3e, 0x5a, 0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9,
90         0xa0, 0x51, 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7,
91         0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 0x74, 0xd6,
92         0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
93 };
94
95 void ec_init_tables(int k, int rows, unsigned char *a, unsigned char *g_tbls)
96 {
97         int i, j;
98
99         for (i = 0; i < rows; i++) {
100                 for (j = 0; j < k; j++) {
101                         gf_vect_mul_init(*a++, g_tbls);
102                         g_tbls += 32;
103                 }
104         }
105 }
106 EXPORT_SYMBOL(ec_init_tables);
107
108 unsigned char gf_mul(unsigned char a, unsigned char b)
109 {
110         int i;
111
112         if ((a == 0) || (b == 0))
113                 return 0;
114
115         i = gflog_base[a] + gflog_base[b];
116         return gff_base[i > 254 ? i - 255 : i];
117 }
118
119 unsigned char gf_inv(unsigned char a)
120 {
121         if (a == 0)
122                 return 0;
123
124         return gff_base[255 - gflog_base[a]];
125 }
126
127 void gf_gen_cauchy1_matrix(unsigned char *a, int m, int k)
128 {
129         int i, j;
130         unsigned char *p;
131
132         /* Identity matrix in high position */
133         memset(a, 0, k * m);
134         for (i = 0; i < k; i++)
135                 a[k * i + i] = 1;
136
137         /* For the rest choose 1/(i + j) | i != j */
138         p = &a[k * k];
139         for (i = k; i < m; i++)
140                 for (j = 0; j < k; j++)
141                         *p++ = gf_inv(i ^ j);
142
143 }
144 EXPORT_SYMBOL(gf_gen_cauchy1_matrix);
145
146 int gf_invert_matrix(unsigned char *in_mat, unsigned char *out_mat, const int n)
147 {
148         int i, j, k;
149         unsigned char temp;
150
151         /* Set out_mat[] to the identity matrix */
152         for (i = 0; i < n * n; i++)     /* memset(out_mat, 0, n*n) */
153                 out_mat[i] = 0;
154
155         for (i = 0; i < n; i++)
156                 out_mat[i * n + i] = 1;
157
158         /* Inverse */
159         for (i = 0; i < n; i++) {
160                 /* Check for 0 in pivot element */
161                 if (in_mat[i * n + i] == 0) {
162                         /* Find a row with non-zero in current column and swap */
163                         for (j = i + 1; j < n; j++)
164                                 if (in_mat[j * n + i])
165                                         break;
166
167                         if (j == n)     /* Couldn't find means it's singular */
168                                 return -1;
169
170                         for (k = 0; k < n; k++) {       /* Swap rows i,j */
171                                 temp = in_mat[i * n + k];
172                                 in_mat[i * n + k] = in_mat[j * n + k];
173                                 in_mat[j * n + k] = temp;
174
175                                 temp = out_mat[i * n + k];
176                                 out_mat[i * n + k] = out_mat[j * n + k];
177                                 out_mat[j * n + k] = temp;
178                         }
179                 }
180
181                 temp = gf_inv(in_mat[i * n + i]);       /* 1/pivot */
182                 for (j = 0; j < n; j++) {       /* Scale row i by 1/pivot */
183                         in_mat[i * n + j] = gf_mul(in_mat[i * n + j], temp);
184                         out_mat[i * n + j] = gf_mul(out_mat[i * n + j], temp);
185                 }
186
187                 for (j = 0; j < n; j++) {
188                         if (j == i)
189                                 continue;
190
191                         temp = in_mat[j * n + i];
192                         for (k = 0; k < n; k++) {
193                                 out_mat[j * n + k] ^= gf_mul(temp, out_mat[i * n + k]);
194                                 in_mat[j * n + k] ^= gf_mul(temp, in_mat[i * n + k]);
195                         }
196                 }
197         }
198         return 0;
199 }
200 EXPORT_SYMBOL(gf_invert_matrix);
201
202 /* Calculates const table gftbl in GF(2^8) from single input A
203  * gftbl(A) = {A{00}, A{01}, A{02}, ... , A{0f} }, {A{00}, A{10}, A{20}, ... ,
204  * A{f0} }
205  */
206 void gf_vect_mul_init(unsigned char c, unsigned char *tbl)
207 {
208         unsigned char c2 = (c << 1) ^ ((c & 0x80) ? 0x1d : 0);  /* Mult by
209                                                                  * GF{2}
210                                                                  */
211         unsigned char c4 = (c2 << 1) ^ ((c2 & 0x80) ? 0x1d : 0); /* Mult by
212                                                                   * GF{2}
213                                                                   */
214         unsigned char c8 = (c4 << 1) ^ ((c4 & 0x80) ? 0x1d : 0); /* Mult by
215                                                                   * GF{2}
216                                                                   */
217 #if BITS_PER_LONG == 64
218         unsigned long long v1, v2, v4, v8, *t;
219         unsigned long long v10, v20, v40, v80;
220         unsigned char c17, c18, c20, c24;
221
222         t = (unsigned long long *)tbl;
223
224         v1 = c * 0x0100010001000100ull;
225         v2 = c2 * 0x0101000001010000ull;
226         v4 = c4 * 0x0101010100000000ull;
227         v8 = c8 * 0x0101010101010101ull;
228
229         v4 = v1 ^ v2 ^ v4;
230         t[0] = v4;
231         t[1] = v8 ^ v4;
232
233         c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0);     //Mult by GF{2}
234         c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0);   //Mult by GF{2}
235         c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0);   //Mult by GF{2}
236         c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0);   //Mult by GF{2}
237
238         v10 = c17 * 0x0100010001000100ull;
239         v20 = c18 * 0x0101000001010000ull;
240         v40 = c20 * 0x0101010100000000ull;
241         v80 = c24 * 0x0101010101010101ull;
242
243         v40 = v10 ^ v20 ^ v40;
244         t[2] = v40;
245         t[3] = v80 ^ v40;
246
247 #else /* 32-bit or other */
248         unsigned char c3, c5, c6, c7, c9, c10, c11, c12, c13, c14, c15;
249         unsigned char c17, c18, c19, c20, c21, c22, c23, c24, c25, c26;
250         unsigned char c27, c28, c29, c30, c31;
251
252         c3 = c2 ^ c;
253         c5 = c4 ^ c;
254         c6 = c4 ^ c2;
255         c7 = c4 ^ c3;
256
257         c9 = c8 ^ c;
258         c10 = c8 ^ c2;
259         c11 = c8 ^ c3;
260         c12 = c8 ^ c4;
261         c13 = c8 ^ c5;
262         c14 = c8 ^ c6;
263         c15 = c8 ^ c7;
264
265         tbl[0] = 0;
266         tbl[1] = c;
267         tbl[2] = c2;
268         tbl[3] = c3;
269         tbl[4] = c4;
270         tbl[5] = c5;
271         tbl[6] = c6;
272         tbl[7] = c7;
273         tbl[8] = c8;
274         tbl[9] = c9;
275         tbl[10] = c10;
276         tbl[11] = c11;
277         tbl[12] = c12;
278         tbl[13] = c13;
279         tbl[14] = c14;
280         tbl[15] = c15;
281
282         c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0);     /* Mult by GF{2} */
283         c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0);   /* Mult by GF{2} */
284         c19 = c18 ^ c17;
285         c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0);   /* Mult by GF{2} */
286         c21 = c20 ^ c17;
287         c22 = c20 ^ c18;
288         c23 = c20 ^ c19;
289         c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0);   /* Mult by GF{2} */
290         c25 = c24 ^ c17;
291         c26 = c24 ^ c18;
292         c27 = c24 ^ c19;
293         c28 = c24 ^ c20;
294         c29 = c24 ^ c21;
295         c30 = c24 ^ c22;
296         c31 = c24 ^ c23;
297
298         tbl[16] = 0;
299         tbl[17] = c17;
300         tbl[18] = c18;
301         tbl[19] = c19;
302         tbl[20] = c20;
303         tbl[21] = c21;
304         tbl[22] = c22;
305         tbl[23] = c23;
306         tbl[24] = c24;
307         tbl[25] = c25;
308         tbl[26] = c26;
309         tbl[27] = c27;
310         tbl[28] = c28;
311         tbl[29] = c29;
312         tbl[30] = c30;
313         tbl[31] = c31;
314
315 #endif /* BITS_PER_LONG == 64 */
316 }
317
318 void ec_encode_data(int len, int srcs, int dests, unsigned char *v,
319                     unsigned char **src, unsigned char **dest)
320 {
321         int i, j, l;
322         unsigned char s;
323
324         for (l = 0; l < dests; l++) {
325                 for (i = 0; i < len; i++) {
326                         s = 0;
327                         for (j = 0; j < srcs; j++)
328                                 s ^= gf_mul(src[j][i], v[j * 32 + l * srcs * 32 + 1]);
329
330                         dest[l][i] = s;
331                 }
332         }
333 }
334 EXPORT_SYMBOL(ec_encode_data);
335
336 static int __init ec_init(void)
337 {
338         return 0;
339 }
340
341 static void __exit ec_exit(void)
342 {
343 }
344
345 MODULE_AUTHOR("Intel Corporation");
346 MODULE_DESCRIPTION("M to N erasure code handling");
347 MODULE_VERSION("1.0.0");
348 MODULE_LICENSE("Dual BSD/GPL");
349
350 module_init(ec_init);
351 module_exit(ec_exit);