4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
25 #ifndef _VDEV_RAIDZ_MATH_IMPL_H
26 #define _VDEV_RAIDZ_MATH_IMPL_H
28 #include <sys/types.h>
29 #include <sys/vdev_raidz_impl.h>
31 #define raidz_inline inline __attribute__((always_inline))
33 #define noinline __attribute__((noinline))
37 * Functions calculate multiplication constants for data reconstruction.
38 * Coefficients depend on RAIDZ geometry, indexes of failed child vdevs, and
39 * used parity columns for reconstruction.
41 * @tgtidx array of missing data indexes
42 * @coeff output array of coefficients. Array must be provided by
43 * user and must hold minimum MUL_CNT values.
46 raidz_rec_q_coeff(const raidz_row_t *rr, const int *tgtidx, unsigned *coeff)
48 const unsigned ncols = rr->rr_cols;
49 const unsigned x = tgtidx[TARGET_X];
51 coeff[MUL_Q_X] = gf_exp2(255 - (ncols - x - 1));
55 raidz_rec_r_coeff(const raidz_row_t *rr, const int *tgtidx, unsigned *coeff)
57 const unsigned ncols = rr->rr_cols;
58 const unsigned x = tgtidx[TARGET_X];
60 coeff[MUL_R_X] = gf_exp4(255 - (ncols - x - 1));
64 raidz_rec_pq_coeff(const raidz_row_t *rr, const int *tgtidx, unsigned *coeff)
66 const unsigned ncols = rr->rr_cols;
67 const unsigned x = tgtidx[TARGET_X];
68 const unsigned y = tgtidx[TARGET_Y];
71 a = gf_exp2(x + 255 - y);
72 b = gf_exp2(255 - (ncols - x - 1));
75 coeff[MUL_PQ_X] = gf_div(a, e);
76 coeff[MUL_PQ_Y] = gf_div(b, e);
80 raidz_rec_pr_coeff(const raidz_row_t *rr, const int *tgtidx, unsigned *coeff)
82 const unsigned ncols = rr->rr_cols;
83 const unsigned x = tgtidx[TARGET_X];
84 const unsigned y = tgtidx[TARGET_Y];
88 a = gf_exp4(x + 255 - y);
89 b = gf_exp4(255 - (ncols - x - 1));
92 coeff[MUL_PR_X] = gf_div(a, e);
93 coeff[MUL_PR_Y] = gf_div(b, e);
97 raidz_rec_qr_coeff(const raidz_row_t *rr, const int *tgtidx, unsigned *coeff)
99 const unsigned ncols = rr->rr_cols;
100 const unsigned x = tgtidx[TARGET_X];
101 const unsigned y = tgtidx[TARGET_Y];
103 gf_t nx, ny, nxxy, nxyy, d;
105 nx = gf_exp2(ncols - x - 1);
106 ny = gf_exp2(ncols - y - 1);
107 nxxy = gf_mul(gf_mul(nx, nx), ny);
108 nxyy = gf_mul(gf_mul(nx, ny), ny);
111 coeff[MUL_QR_XQ] = ny;
112 coeff[MUL_QR_X] = gf_div(ny, d);
113 coeff[MUL_QR_YQ] = nx;
114 coeff[MUL_QR_Y] = gf_div(nx, d);
118 raidz_rec_pqr_coeff(const raidz_row_t *rr, const int *tgtidx, unsigned *coeff)
120 const unsigned ncols = rr->rr_cols;
121 const unsigned x = tgtidx[TARGET_X];
122 const unsigned y = tgtidx[TARGET_Y];
123 const unsigned z = tgtidx[TARGET_Z];
125 gf_t nx, ny, nz, nxx, nyy, nzz, nyyz, nyzz, xd, yd;
127 nx = gf_exp2(ncols - x - 1);
128 ny = gf_exp2(ncols - y - 1);
129 nz = gf_exp2(ncols - z - 1);
131 nxx = gf_exp4(ncols - x - 1);
132 nyy = gf_exp4(ncols - y - 1);
133 nzz = gf_exp4(ncols - z - 1);
135 nyyz = gf_mul(gf_mul(ny, nz), ny);
136 nyzz = gf_mul(nzz, ny);
138 xd = gf_mul(nxx, ny) ^ gf_mul(nx, nyy) ^ nyyz ^
139 gf_mul(nxx, nz) ^ gf_mul(nzz, nx) ^ nyzz;
141 yd = gf_inv(ny ^ nz);
143 coeff[MUL_PQR_XP] = gf_div(nyyz ^ nyzz, xd);
144 coeff[MUL_PQR_XQ] = gf_div(nyy ^ nzz, xd);
145 coeff[MUL_PQR_XR] = gf_div(ny ^ nz, xd);
146 coeff[MUL_PQR_YU] = nx;
147 coeff[MUL_PQR_YP] = gf_mul(nz, yd);
148 coeff[MUL_PQR_YQ] = yd;
152 * Method for zeroing a buffer (can be implemented using SIMD).
153 * This method is used by multiple for gen/rec functions.
155 * @dc Destination buffer
156 * @dsize Destination buffer size
160 raidz_zero_abd_cb(void *dc, size_t dsize, void *private)
162 v_t *dst = (v_t *)dc;
167 (void) private; /* unused */
171 for (i = 0; i < dsize / sizeof (v_t); i += (2 * ZERO_STRIDE)) {
172 STORE(dst + i, ZERO_D);
173 STORE(dst + i + ZERO_STRIDE, ZERO_D);
179 #define raidz_zero(dabd, size) \
181 abd_iterate_func(dabd, 0, size, raidz_zero_abd_cb, NULL); \
185 * Method for copying two buffers (can be implemented using SIMD).
186 * This method is used by multiple for gen/rec functions.
188 * @dc Destination buffer
190 * @dsize Destination buffer size
191 * @ssize Source buffer size
195 raidz_copy_abd_cb(void *dc, void *sc, size_t size, void *private)
197 v_t *dst = (v_t *)dc;
198 const v_t *src = (v_t *)sc;
203 (void) private; /* unused */
205 for (i = 0; i < size / sizeof (v_t); i += (2 * COPY_STRIDE)) {
206 LOAD(src + i, COPY_D);
207 STORE(dst + i, COPY_D);
209 LOAD(src + i + COPY_STRIDE, COPY_D);
210 STORE(dst + i + COPY_STRIDE, COPY_D);
217 #define raidz_copy(dabd, sabd, size) \
219 abd_iterate_func2(dabd, sabd, 0, 0, size, raidz_copy_abd_cb, NULL);\
223 * Method for adding (XORing) two buffers.
224 * Source and destination are XORed together and result is stored in
225 * destination buffer. This method is used by multiple for gen/rec functions.
227 * @dc Destination buffer
229 * @dsize Destination buffer size
230 * @ssize Source buffer size
234 raidz_add_abd_cb(void *dc, void *sc, size_t size, void *private)
236 v_t *dst = (v_t *)dc;
237 const v_t *src = (v_t *)sc;
242 (void) private; /* unused */
244 for (i = 0; i < size / sizeof (v_t); i += (2 * ADD_STRIDE)) {
245 LOAD(dst + i, ADD_D);
246 XOR_ACC(src + i, ADD_D);
247 STORE(dst + i, ADD_D);
249 LOAD(dst + i + ADD_STRIDE, ADD_D);
250 XOR_ACC(src + i + ADD_STRIDE, ADD_D);
251 STORE(dst + i + ADD_STRIDE, ADD_D);
257 #define raidz_add(dabd, sabd, size) \
259 abd_iterate_func2(dabd, sabd, 0, 0, size, raidz_add_abd_cb, NULL);\
263 * Method for multiplying a buffer with a constant in GF(2^8).
264 * Symbols from buffer are multiplied by a constant and result is stored
265 * back in the same buffer.
267 * @dc In/Out data buffer.
268 * @size Size of the buffer
269 * @private pointer to the multiplication constant (unsigned)
272 raidz_mul_abd_cb(void *dc, size_t size, void *private)
274 const unsigned mul = *((unsigned *)private);
280 for (i = 0; i < size / sizeof (v_t); i += (2 * MUL_STRIDE)) {
285 LOAD(d + i + MUL_STRIDE, MUL_D);
287 STORE(d + i + MUL_STRIDE, MUL_D);
295 * Syndrome generation/update macros
297 * Require LOAD(), XOR(), STORE(), MUL2(), and MUL4() macros
299 #define P_D_SYNDROME(D, T, t) \
306 #define Q_D_SYNDROME(D, T, t) \
314 #define Q_SYNDROME(T, t) \
321 #define R_D_SYNDROME(D, T, t) \
329 #define R_SYNDROME(T, t) \
340 * Macros *_SYNDROME are used for parity/syndrome calculation.
341 * *_D_SYNDROME() macros are used to calculate syndrome between 0 and
342 * length of data column, and *_SYNDROME() macros are only for updating
343 * the parity/syndrome if data column is shorter.
345 * P parity is calculated using raidz_add_abd().
349 * Generate P parity (RAIDZ1)
353 static raidz_inline void
354 raidz_generate_p_impl(raidz_row_t * const rr)
357 const size_t ncols = rr->rr_cols;
358 const size_t psize = rr->rr_col[CODE_P].rc_size;
359 abd_t *pabd = rr->rr_col[CODE_P].rc_abd;
365 /* start with first data column */
366 raidz_copy(pabd, rr->rr_col[1].rc_abd, psize);
368 for (c = 2; c < ncols; c++) {
369 dabd = rr->rr_col[c].rc_abd;
370 size = rr->rr_col[c].rc_size;
372 /* add data column */
373 raidz_add(pabd, dabd, size);
381 * Generate PQ parity (RAIDZ2)
382 * The function is called per data column.
384 * @c array of pointers to parity (code) columns
385 * @dc pointer to data column
386 * @csize size of parity columns
387 * @dsize size of data column
390 raidz_gen_pq_add(void **c, const void *dc, const size_t csize,
393 v_t *p = (v_t *)c[0];
394 v_t *q = (v_t *)c[1];
395 const v_t *d = (const v_t *)dc;
396 const v_t * const dend = d + (dsize / sizeof (v_t));
397 const v_t * const qend = q + (csize / sizeof (v_t));
403 for (; d < dend; d += GEN_PQ_STRIDE, p += GEN_PQ_STRIDE,
404 q += GEN_PQ_STRIDE) {
406 P_D_SYNDROME(GEN_PQ_D, GEN_PQ_C, p);
407 Q_D_SYNDROME(GEN_PQ_D, GEN_PQ_C, q);
409 for (; q < qend; q += GEN_PQ_STRIDE) {
410 Q_SYNDROME(GEN_PQ_C, q);
416 * Generate PQ parity (RAIDZ2)
420 static raidz_inline void
421 raidz_generate_pq_impl(raidz_row_t * const rr)
424 const size_t ncols = rr->rr_cols;
425 const size_t csize = rr->rr_col[CODE_P].rc_size;
429 rr->rr_col[CODE_P].rc_abd,
430 rr->rr_col[CODE_Q].rc_abd
435 raidz_copy(cabds[CODE_P], rr->rr_col[2].rc_abd, csize);
436 raidz_copy(cabds[CODE_Q], rr->rr_col[2].rc_abd, csize);
438 for (c = 3; c < ncols; c++) {
439 dabd = rr->rr_col[c].rc_abd;
440 dsize = rr->rr_col[c].rc_size;
442 abd_raidz_gen_iterate(cabds, dabd, csize, dsize, 2,
451 * Generate PQR parity (RAIDZ3)
452 * The function is called per data column.
454 * @c array of pointers to parity (code) columns
455 * @dc pointer to data column
456 * @csize size of parity columns
457 * @dsize size of data column
460 raidz_gen_pqr_add(void **c, const void *dc, const size_t csize,
463 v_t *p = (v_t *)c[CODE_P];
464 v_t *q = (v_t *)c[CODE_Q];
465 v_t *r = (v_t *)c[CODE_R];
466 const v_t *d = (const v_t *)dc;
467 const v_t * const dend = d + (dsize / sizeof (v_t));
468 const v_t * const qend = q + (csize / sizeof (v_t));
474 for (; d < dend; d += GEN_PQR_STRIDE, p += GEN_PQR_STRIDE,
475 q += GEN_PQR_STRIDE, r += GEN_PQR_STRIDE) {
477 P_D_SYNDROME(GEN_PQR_D, GEN_PQR_C, p);
478 Q_D_SYNDROME(GEN_PQR_D, GEN_PQR_C, q);
479 R_D_SYNDROME(GEN_PQR_D, GEN_PQR_C, r);
481 for (; q < qend; q += GEN_PQR_STRIDE, r += GEN_PQR_STRIDE) {
482 Q_SYNDROME(GEN_PQR_C, q);
483 R_SYNDROME(GEN_PQR_C, r);
489 * Generate PQR parity (RAIDZ3)
493 static raidz_inline void
494 raidz_generate_pqr_impl(raidz_row_t * const rr)
497 const size_t ncols = rr->rr_cols;
498 const size_t csize = rr->rr_col[CODE_P].rc_size;
502 rr->rr_col[CODE_P].rc_abd,
503 rr->rr_col[CODE_Q].rc_abd,
504 rr->rr_col[CODE_R].rc_abd
509 raidz_copy(cabds[CODE_P], rr->rr_col[3].rc_abd, csize);
510 raidz_copy(cabds[CODE_Q], rr->rr_col[3].rc_abd, csize);
511 raidz_copy(cabds[CODE_R], rr->rr_col[3].rc_abd, csize);
513 for (c = 4; c < ncols; c++) {
514 dabd = rr->rr_col[c].rc_abd;
515 dsize = rr->rr_col[c].rc_size;
517 abd_raidz_gen_iterate(cabds, dabd, csize, dsize, 3,
526 * DATA RECONSTRUCTION
528 * Data reconstruction process consists of two phases:
529 * - Syndrome calculation
530 * - Data reconstruction
532 * Syndrome is calculated by generating parity using available data columns
533 * and zeros in places of erasure. Existing parity is added to corresponding
534 * syndrome value to obtain the [P|Q|R]syn values from equation:
535 * P = Psyn + Dx + Dy + Dz
536 * Q = Qsyn + 2^x * Dx + 2^y * Dy + 2^z * Dz
537 * R = Rsyn + 4^x * Dx + 4^y * Dy + 4^z * Dz
539 * For data reconstruction phase, the corresponding equations are solved
540 * for missing data (Dx, Dy, Dz). This generally involves multiplying known
541 * symbols by an coefficient and adding them together. The multiplication
542 * constant coefficients are calculated ahead of the operation in
543 * raidz_rec_[q|r|pq|pq|qr|pqr]_coeff() functions.
545 * IMPLEMENTATION NOTE: RAID-Z block can have complex geometry, with "big"
546 * and "short" columns.
547 * For this reason, reconstruction is performed in minimum of
548 * two steps. First, from offset 0 to short_size, then from short_size to
549 * short_size. Calculation functions REC_[*]_BLOCK() are implemented to work
550 * over both ranges. The split also enables removal of conditional expressions
551 * from loop bodies, improving throughput of SIMD implementations.
552 * For the best performance, all functions marked with raidz_inline attribute
553 * must be inlined by compiler.
557 * <----------> <------------------>
558 * x y <----+ missing columns (x, y)
560 * +---+---+---+---+-v-+---+-v-+---+ ^ 0
561 * | | | | | | | | | |
562 * | | | | | | | | | |
563 * | P | Q | R | D | D | D | D | D | |
564 * | | | | 0 | 1 | 2 | 3 | 4 | |
565 * | | | | | | | | | v
566 * | | | | | +---+---+---+ ^ short_size
568 * +---+---+---+---+---+ v big_size
569 * <------------------> <---------->
570 * big columns short columns
578 * Reconstruct single data column using P parity
580 * @syn_method raidz_add_abd()
581 * @rec_method not applicable
584 * @tgtidx array of missing data indexes
586 static raidz_inline int
587 raidz_reconstruct_p_impl(raidz_row_t *rr, const int *tgtidx)
590 const size_t firstdc = rr->rr_firstdatacol;
591 const size_t ncols = rr->rr_cols;
592 const size_t x = tgtidx[TARGET_X];
593 const size_t xsize = rr->rr_col[x].rc_size;
594 abd_t *xabd = rr->rr_col[x].rc_abd;
599 return (1 << CODE_P);
603 /* copy P into target */
604 raidz_copy(xabd, rr->rr_col[CODE_P].rc_abd, xsize);
606 /* generate p_syndrome */
607 for (c = firstdc; c < ncols; c++) {
611 dabd = rr->rr_col[c].rc_abd;
612 size = MIN(rr->rr_col[c].rc_size, xsize);
614 raidz_add(xabd, dabd, size);
619 return (1 << CODE_P);
624 * Generate Q syndrome (Qsyn)
626 * @xc array of pointers to syndrome columns
627 * @dc data column (NULL if missing)
628 * @xsize size of syndrome columns
629 * @dsize size of data column (0 if missing)
632 raidz_syn_q_abd(void **xc, const void *dc, const size_t xsize,
635 v_t *x = (v_t *)xc[TARGET_X];
636 const v_t *d = (const v_t *)dc;
637 const v_t * const dend = d + (dsize / sizeof (v_t));
638 const v_t * const xend = x + (xsize / sizeof (v_t));
644 for (; d < dend; d += SYN_STRIDE, x += SYN_STRIDE) {
646 Q_D_SYNDROME(SYN_Q_D, SYN_Q_X, x);
648 for (; x < xend; x += SYN_STRIDE) {
649 Q_SYNDROME(SYN_Q_X, x);
655 * Reconstruct single data column using Q parity
657 * @syn_method raidz_add_abd()
658 * @rec_method raidz_mul_abd_cb()
661 * @tgtidx array of missing data indexes
663 static raidz_inline int
664 raidz_reconstruct_q_impl(raidz_row_t *rr, const int *tgtidx)
669 const size_t firstdc = rr->rr_firstdatacol;
670 const size_t ncols = rr->rr_cols;
671 const size_t x = tgtidx[TARGET_X];
672 abd_t *xabd = rr->rr_col[x].rc_abd;
673 const size_t xsize = rr->rr_col[x].rc_size;
674 abd_t *tabds[] = { xabd };
677 return (1 << CODE_Q);
679 unsigned coeff[MUL_CNT];
680 raidz_rec_q_coeff(rr, tgtidx, coeff);
684 /* Start with first data column if present */
686 raidz_copy(xabd, rr->rr_col[firstdc].rc_abd, xsize);
688 raidz_zero(xabd, xsize);
691 /* generate q_syndrome */
692 for (c = firstdc+1; c < ncols; c++) {
697 dabd = rr->rr_col[c].rc_abd;
698 dsize = rr->rr_col[c].rc_size;
701 abd_raidz_gen_iterate(tabds, dabd, xsize, dsize, 1,
705 /* add Q to the syndrome */
706 raidz_add(xabd, rr->rr_col[CODE_Q].rc_abd, xsize);
708 /* transform the syndrome */
709 abd_iterate_func(xabd, 0, xsize, raidz_mul_abd_cb, (void*) coeff);
713 return (1 << CODE_Q);
718 * Generate R syndrome (Rsyn)
720 * @xc array of pointers to syndrome columns
721 * @dc data column (NULL if missing)
722 * @tsize size of syndrome columns
723 * @dsize size of data column (0 if missing)
726 raidz_syn_r_abd(void **xc, const void *dc, const size_t tsize,
729 v_t *x = (v_t *)xc[TARGET_X];
730 const v_t *d = (const v_t *)dc;
731 const v_t * const dend = d + (dsize / sizeof (v_t));
732 const v_t * const xend = x + (tsize / sizeof (v_t));
738 for (; d < dend; d += SYN_STRIDE, x += SYN_STRIDE) {
740 R_D_SYNDROME(SYN_R_D, SYN_R_X, x);
742 for (; x < xend; x += SYN_STRIDE) {
743 R_SYNDROME(SYN_R_X, x);
749 * Reconstruct single data column using R parity
751 * @syn_method raidz_add_abd()
752 * @rec_method raidz_mul_abd_cb()
755 * @tgtidx array of missing data indexes
757 static raidz_inline int
758 raidz_reconstruct_r_impl(raidz_row_t *rr, const int *tgtidx)
763 const size_t firstdc = rr->rr_firstdatacol;
764 const size_t ncols = rr->rr_cols;
765 const size_t x = tgtidx[TARGET_X];
766 const size_t xsize = rr->rr_col[x].rc_size;
767 abd_t *xabd = rr->rr_col[x].rc_abd;
768 abd_t *tabds[] = { xabd };
771 return (1 << CODE_R);
773 unsigned coeff[MUL_CNT];
774 raidz_rec_r_coeff(rr, tgtidx, coeff);
778 /* Start with first data column if present */
780 raidz_copy(xabd, rr->rr_col[firstdc].rc_abd, xsize);
782 raidz_zero(xabd, xsize);
786 /* generate q_syndrome */
787 for (c = firstdc+1; c < ncols; c++) {
792 dabd = rr->rr_col[c].rc_abd;
793 dsize = rr->rr_col[c].rc_size;
796 abd_raidz_gen_iterate(tabds, dabd, xsize, dsize, 1,
800 /* add R to the syndrome */
801 raidz_add(xabd, rr->rr_col[CODE_R].rc_abd, xsize);
803 /* transform the syndrome */
804 abd_iterate_func(xabd, 0, xsize, raidz_mul_abd_cb, (void *)coeff);
808 return (1 << CODE_R);
813 * Generate P and Q syndromes
815 * @xc array of pointers to syndrome columns
816 * @dc data column (NULL if missing)
817 * @tsize size of syndrome columns
818 * @dsize size of data column (0 if missing)
821 raidz_syn_pq_abd(void **tc, const void *dc, const size_t tsize,
824 v_t *x = (v_t *)tc[TARGET_X];
825 v_t *y = (v_t *)tc[TARGET_Y];
826 const v_t *d = (const v_t *)dc;
827 const v_t * const dend = d + (dsize / sizeof (v_t));
828 const v_t * const yend = y + (tsize / sizeof (v_t));
834 for (; d < dend; d += SYN_STRIDE, x += SYN_STRIDE, y += SYN_STRIDE) {
836 P_D_SYNDROME(SYN_PQ_D, SYN_PQ_X, x);
837 Q_D_SYNDROME(SYN_PQ_D, SYN_PQ_X, y);
839 for (; y < yend; y += SYN_STRIDE) {
840 Q_SYNDROME(SYN_PQ_X, y);
845 * Reconstruct data using PQ parity and PQ syndromes
847 * @tc syndrome/result columns
848 * @tsize size of syndrome/result columns
850 * @mul array of multiplication constants
853 raidz_rec_pq_abd(void **tc, const size_t tsize, void **c,
856 v_t *x = (v_t *)tc[TARGET_X];
857 v_t *y = (v_t *)tc[TARGET_Y];
858 const v_t * const xend = x + (tsize / sizeof (v_t));
859 const v_t *p = (v_t *)c[CODE_P];
860 const v_t *q = (v_t *)c[CODE_Q];
864 for (; x < xend; x += REC_PQ_STRIDE, y += REC_PQ_STRIDE,
865 p += REC_PQ_STRIDE, q += REC_PQ_STRIDE) {
869 XOR_ACC(p, REC_PQ_X);
870 XOR_ACC(q, REC_PQ_Y);
873 COPY(REC_PQ_X, REC_PQ_T);
876 MUL(mul[MUL_PQ_X], REC_PQ_X);
877 MUL(mul[MUL_PQ_Y], REC_PQ_Y);
878 XOR(REC_PQ_Y, REC_PQ_X);
882 XOR(REC_PQ_T, REC_PQ_X);
889 * Reconstruct two data columns using PQ parity
891 * @syn_method raidz_syn_pq_abd()
892 * @rec_method raidz_rec_pq_abd()
895 * @tgtidx array of missing data indexes
897 static raidz_inline int
898 raidz_reconstruct_pq_impl(raidz_row_t *rr, const int *tgtidx)
903 const size_t firstdc = rr->rr_firstdatacol;
904 const size_t ncols = rr->rr_cols;
905 const size_t x = tgtidx[TARGET_X];
906 const size_t y = tgtidx[TARGET_Y];
907 const size_t xsize = rr->rr_col[x].rc_size;
908 const size_t ysize = rr->rr_col[y].rc_size;
909 abd_t *xabd = rr->rr_col[x].rc_abd;
910 abd_t *yabd = rr->rr_col[y].rc_abd;
911 abd_t *tabds[2] = { xabd, yabd };
913 rr->rr_col[CODE_P].rc_abd,
914 rr->rr_col[CODE_Q].rc_abd
918 return ((1 << CODE_P) | (1 << CODE_Q));
920 unsigned coeff[MUL_CNT];
921 raidz_rec_pq_coeff(rr, tgtidx, coeff);
924 * Check if some of targets is shorter then others
925 * In this case, shorter target needs to be replaced with
926 * new buffer so that syndrome can be calculated.
929 yabd = abd_alloc(xsize, B_FALSE);
935 /* Start with first data column if present */
937 raidz_copy(xabd, rr->rr_col[firstdc].rc_abd, xsize);
938 raidz_copy(yabd, rr->rr_col[firstdc].rc_abd, xsize);
940 raidz_zero(xabd, xsize);
941 raidz_zero(yabd, xsize);
944 /* generate q_syndrome */
945 for (c = firstdc+1; c < ncols; c++) {
946 if (c == x || c == y) {
950 dabd = rr->rr_col[c].rc_abd;
951 dsize = rr->rr_col[c].rc_size;
954 abd_raidz_gen_iterate(tabds, dabd, xsize, dsize, 2,
958 abd_raidz_rec_iterate(cabds, tabds, xsize, 2, raidz_rec_pq_abd, coeff);
960 /* Copy shorter targets back to the original abd buffer */
962 raidz_copy(rr->rr_col[y].rc_abd, yabd, ysize);
969 return ((1 << CODE_P) | (1 << CODE_Q));
974 * Generate P and R syndromes
976 * @xc array of pointers to syndrome columns
977 * @dc data column (NULL if missing)
978 * @tsize size of syndrome columns
979 * @dsize size of data column (0 if missing)
982 raidz_syn_pr_abd(void **c, const void *dc, const size_t tsize,
985 v_t *x = (v_t *)c[TARGET_X];
986 v_t *y = (v_t *)c[TARGET_Y];
987 const v_t *d = (const v_t *)dc;
988 const v_t * const dend = d + (dsize / sizeof (v_t));
989 const v_t * const yend = y + (tsize / sizeof (v_t));
995 for (; d < dend; d += SYN_STRIDE, x += SYN_STRIDE, y += SYN_STRIDE) {
997 P_D_SYNDROME(SYN_PR_D, SYN_PR_X, x);
998 R_D_SYNDROME(SYN_PR_D, SYN_PR_X, y);
1000 for (; y < yend; y += SYN_STRIDE) {
1001 R_SYNDROME(SYN_PR_X, y);
1006 * Reconstruct data using PR parity and PR syndromes
1008 * @tc syndrome/result columns
1009 * @tsize size of syndrome/result columns
1011 * @mul array of multiplication constants
1014 raidz_rec_pr_abd(void **t, const size_t tsize, void **c,
1015 const unsigned *mul)
1017 v_t *x = (v_t *)t[TARGET_X];
1018 v_t *y = (v_t *)t[TARGET_Y];
1019 const v_t * const xend = x + (tsize / sizeof (v_t));
1020 const v_t *p = (v_t *)c[CODE_P];
1021 const v_t *q = (v_t *)c[CODE_Q];
1025 for (; x < xend; x += REC_PR_STRIDE, y += REC_PR_STRIDE,
1026 p += REC_PR_STRIDE, q += REC_PR_STRIDE) {
1029 XOR_ACC(p, REC_PR_X);
1030 XOR_ACC(q, REC_PR_Y);
1033 COPY(REC_PR_X, REC_PR_T);
1036 MUL(mul[MUL_PR_X], REC_PR_X);
1037 MUL(mul[MUL_PR_Y], REC_PR_Y);
1038 XOR(REC_PR_Y, REC_PR_X);
1042 XOR(REC_PR_T, REC_PR_X);
1049 * Reconstruct two data columns using PR parity
1051 * @syn_method raidz_syn_pr_abd()
1052 * @rec_method raidz_rec_pr_abd()
1055 * @tgtidx array of missing data indexes
1057 static raidz_inline int
1058 raidz_reconstruct_pr_impl(raidz_row_t *rr, const int *tgtidx)
1063 const size_t firstdc = rr->rr_firstdatacol;
1064 const size_t ncols = rr->rr_cols;
1065 const size_t x = tgtidx[0];
1066 const size_t y = tgtidx[1];
1067 const size_t xsize = rr->rr_col[x].rc_size;
1068 const size_t ysize = rr->rr_col[y].rc_size;
1069 abd_t *xabd = rr->rr_col[x].rc_abd;
1070 abd_t *yabd = rr->rr_col[y].rc_abd;
1071 abd_t *tabds[2] = { xabd, yabd };
1073 rr->rr_col[CODE_P].rc_abd,
1074 rr->rr_col[CODE_R].rc_abd
1078 return ((1 << CODE_P) | (1 << CODE_R));
1080 unsigned coeff[MUL_CNT];
1081 raidz_rec_pr_coeff(rr, tgtidx, coeff);
1084 * Check if some of targets are shorter then others.
1085 * They need to be replaced with a new buffer so that syndrome can
1086 * be calculated on full length.
1088 if (ysize < xsize) {
1089 yabd = abd_alloc(xsize, B_FALSE);
1095 /* Start with first data column if present */
1097 raidz_copy(xabd, rr->rr_col[firstdc].rc_abd, xsize);
1098 raidz_copy(yabd, rr->rr_col[firstdc].rc_abd, xsize);
1100 raidz_zero(xabd, xsize);
1101 raidz_zero(yabd, xsize);
1104 /* generate q_syndrome */
1105 for (c = firstdc+1; c < ncols; c++) {
1106 if (c == x || c == y) {
1110 dabd = rr->rr_col[c].rc_abd;
1111 dsize = rr->rr_col[c].rc_size;
1114 abd_raidz_gen_iterate(tabds, dabd, xsize, dsize, 2,
1118 abd_raidz_rec_iterate(cabds, tabds, xsize, 2, raidz_rec_pr_abd, coeff);
1121 * Copy shorter targets back to the original abd buffer
1124 raidz_copy(rr->rr_col[y].rc_abd, yabd, ysize);
1131 return ((1 << CODE_P) | (1 << CODE_R));
1136 * Generate Q and R syndromes
1138 * @xc array of pointers to syndrome columns
1139 * @dc data column (NULL if missing)
1140 * @tsize size of syndrome columns
1141 * @dsize size of data column (0 if missing)
1144 raidz_syn_qr_abd(void **c, const void *dc, const size_t tsize,
1147 v_t *x = (v_t *)c[TARGET_X];
1148 v_t *y = (v_t *)c[TARGET_Y];
1149 const v_t * const xend = x + (tsize / sizeof (v_t));
1150 const v_t *d = (const v_t *)dc;
1151 const v_t * const dend = d + (dsize / sizeof (v_t));
1157 for (; d < dend; d += SYN_STRIDE, x += SYN_STRIDE, y += SYN_STRIDE) {
1159 Q_D_SYNDROME(SYN_QR_D, SYN_QR_X, x);
1160 R_D_SYNDROME(SYN_QR_D, SYN_QR_X, y);
1162 for (; x < xend; x += SYN_STRIDE, y += SYN_STRIDE) {
1163 Q_SYNDROME(SYN_QR_X, x);
1164 R_SYNDROME(SYN_QR_X, y);
1170 * Reconstruct data using QR parity and QR syndromes
1172 * @tc syndrome/result columns
1173 * @tsize size of syndrome/result columns
1175 * @mul array of multiplication constants
1178 raidz_rec_qr_abd(void **t, const size_t tsize, void **c,
1179 const unsigned *mul)
1181 v_t *x = (v_t *)t[TARGET_X];
1182 v_t *y = (v_t *)t[TARGET_Y];
1183 const v_t * const xend = x + (tsize / sizeof (v_t));
1184 const v_t *p = (v_t *)c[CODE_P];
1185 const v_t *q = (v_t *)c[CODE_Q];
1189 for (; x < xend; x += REC_QR_STRIDE, y += REC_QR_STRIDE,
1190 p += REC_QR_STRIDE, q += REC_QR_STRIDE) {
1194 XOR_ACC(p, REC_QR_X);
1195 XOR_ACC(q, REC_QR_Y);
1198 COPY(REC_QR_X, REC_QR_T);
1201 MUL(mul[MUL_QR_XQ], REC_QR_X); /* X = Q * xqm */
1202 XOR(REC_QR_Y, REC_QR_X); /* X = R ^ X */
1203 MUL(mul[MUL_QR_X], REC_QR_X); /* X = X * xm */
1207 MUL(mul[MUL_QR_YQ], REC_QR_T); /* X = Q * xqm */
1208 XOR(REC_QR_Y, REC_QR_T); /* X = R ^ X */
1209 MUL(mul[MUL_QR_Y], REC_QR_T); /* X = X * xm */
1216 * Reconstruct two data columns using QR parity
1218 * @syn_method raidz_syn_qr_abd()
1219 * @rec_method raidz_rec_qr_abd()
1222 * @tgtidx array of missing data indexes
1224 static raidz_inline int
1225 raidz_reconstruct_qr_impl(raidz_row_t *rr, const int *tgtidx)
1230 const size_t firstdc = rr->rr_firstdatacol;
1231 const size_t ncols = rr->rr_cols;
1232 const size_t x = tgtidx[TARGET_X];
1233 const size_t y = tgtidx[TARGET_Y];
1234 const size_t xsize = rr->rr_col[x].rc_size;
1235 const size_t ysize = rr->rr_col[y].rc_size;
1236 abd_t *xabd = rr->rr_col[x].rc_abd;
1237 abd_t *yabd = rr->rr_col[y].rc_abd;
1238 abd_t *tabds[2] = { xabd, yabd };
1240 rr->rr_col[CODE_Q].rc_abd,
1241 rr->rr_col[CODE_R].rc_abd
1245 return ((1 << CODE_Q) | (1 << CODE_R));
1247 unsigned coeff[MUL_CNT];
1248 raidz_rec_qr_coeff(rr, tgtidx, coeff);
1251 * Check if some of targets is shorter then others
1252 * In this case, shorter target needs to be replaced with
1253 * new buffer so that syndrome can be calculated.
1255 if (ysize < xsize) {
1256 yabd = abd_alloc(xsize, B_FALSE);
1262 /* Start with first data column if present */
1264 raidz_copy(xabd, rr->rr_col[firstdc].rc_abd, xsize);
1265 raidz_copy(yabd, rr->rr_col[firstdc].rc_abd, xsize);
1267 raidz_zero(xabd, xsize);
1268 raidz_zero(yabd, xsize);
1271 /* generate q_syndrome */
1272 for (c = firstdc+1; c < ncols; c++) {
1273 if (c == x || c == y) {
1277 dabd = rr->rr_col[c].rc_abd;
1278 dsize = rr->rr_col[c].rc_size;
1281 abd_raidz_gen_iterate(tabds, dabd, xsize, dsize, 2,
1285 abd_raidz_rec_iterate(cabds, tabds, xsize, 2, raidz_rec_qr_abd, coeff);
1288 * Copy shorter targets back to the original abd buffer
1291 raidz_copy(rr->rr_col[y].rc_abd, yabd, ysize);
1299 return ((1 << CODE_Q) | (1 << CODE_R));
1304 * Generate P, Q, and R syndromes
1306 * @xc array of pointers to syndrome columns
1307 * @dc data column (NULL if missing)
1308 * @tsize size of syndrome columns
1309 * @dsize size of data column (0 if missing)
1312 raidz_syn_pqr_abd(void **c, const void *dc, const size_t tsize,
1315 v_t *x = (v_t *)c[TARGET_X];
1316 v_t *y = (v_t *)c[TARGET_Y];
1317 v_t *z = (v_t *)c[TARGET_Z];
1318 const v_t * const yend = y + (tsize / sizeof (v_t));
1319 const v_t *d = (const v_t *)dc;
1320 const v_t * const dend = d + (dsize / sizeof (v_t));
1326 for (; d < dend; d += SYN_STRIDE, x += SYN_STRIDE, y += SYN_STRIDE,
1329 P_D_SYNDROME(SYN_PQR_D, SYN_PQR_X, x)
1330 Q_D_SYNDROME(SYN_PQR_D, SYN_PQR_X, y);
1331 R_D_SYNDROME(SYN_PQR_D, SYN_PQR_X, z);
1333 for (; y < yend; y += SYN_STRIDE, z += SYN_STRIDE) {
1334 Q_SYNDROME(SYN_PQR_X, y);
1335 R_SYNDROME(SYN_PQR_X, z);
1341 * Reconstruct data using PRQ parity and PQR syndromes
1343 * @tc syndrome/result columns
1344 * @tsize size of syndrome/result columns
1346 * @mul array of multiplication constants
1349 raidz_rec_pqr_abd(void **t, const size_t tsize, void **c,
1350 const unsigned * const mul)
1352 v_t *x = (v_t *)t[TARGET_X];
1353 v_t *y = (v_t *)t[TARGET_Y];
1354 v_t *z = (v_t *)t[TARGET_Z];
1355 const v_t * const xend = x + (tsize / sizeof (v_t));
1356 const v_t *p = (v_t *)c[CODE_P];
1357 const v_t *q = (v_t *)c[CODE_Q];
1358 const v_t *r = (v_t *)c[CODE_R];
1362 for (; x < xend; x += REC_PQR_STRIDE, y += REC_PQR_STRIDE,
1363 z += REC_PQR_STRIDE, p += REC_PQR_STRIDE, q += REC_PQR_STRIDE,
1364 r += REC_PQR_STRIDE) {
1369 XOR_ACC(p, REC_PQR_X);
1370 XOR_ACC(q, REC_PQR_Y);
1371 XOR_ACC(r, REC_PQR_Z);
1373 /* Save Pxyz and Qxyz */
1374 COPY(REC_PQR_X, REC_PQR_XS);
1375 COPY(REC_PQR_Y, REC_PQR_YS);
1378 MUL(mul[MUL_PQR_XP], REC_PQR_X); /* Xp = Pxyz * xp */
1379 MUL(mul[MUL_PQR_XQ], REC_PQR_Y); /* Xq = Qxyz * xq */
1380 XOR(REC_PQR_Y, REC_PQR_X);
1381 MUL(mul[MUL_PQR_XR], REC_PQR_Z); /* Xr = Rxyz * xr */
1382 XOR(REC_PQR_Z, REC_PQR_X); /* X = Xp + Xq + Xr */
1383 STORE(x, REC_PQR_X);
1386 XOR(REC_PQR_X, REC_PQR_XS); /* Pyz = Pxyz + X */
1387 MUL(mul[MUL_PQR_YU], REC_PQR_X); /* Xq = X * upd_q */
1388 XOR(REC_PQR_X, REC_PQR_YS); /* Qyz = Qxyz + Xq */
1389 COPY(REC_PQR_XS, REC_PQR_X); /* restore Pyz */
1390 MUL(mul[MUL_PQR_YP], REC_PQR_X); /* Yp = Pyz * yp */
1391 MUL(mul[MUL_PQR_YQ], REC_PQR_YS); /* Yq = Qyz * yq */
1392 XOR(REC_PQR_X, REC_PQR_YS); /* Y = Yp + Yq */
1393 STORE(y, REC_PQR_YS);
1396 XOR(REC_PQR_XS, REC_PQR_YS); /* Z = Pz = Pyz + Y */
1397 STORE(z, REC_PQR_YS);
1403 * Reconstruct three data columns using PQR parity
1405 * @syn_method raidz_syn_pqr_abd()
1406 * @rec_method raidz_rec_pqr_abd()
1409 * @tgtidx array of missing data indexes
1411 static raidz_inline int
1412 raidz_reconstruct_pqr_impl(raidz_row_t *rr, const int *tgtidx)
1417 const size_t firstdc = rr->rr_firstdatacol;
1418 const size_t ncols = rr->rr_cols;
1419 const size_t x = tgtidx[TARGET_X];
1420 const size_t y = tgtidx[TARGET_Y];
1421 const size_t z = tgtidx[TARGET_Z];
1422 const size_t xsize = rr->rr_col[x].rc_size;
1423 const size_t ysize = rr->rr_col[y].rc_size;
1424 const size_t zsize = rr->rr_col[z].rc_size;
1425 abd_t *xabd = rr->rr_col[x].rc_abd;
1426 abd_t *yabd = rr->rr_col[y].rc_abd;
1427 abd_t *zabd = rr->rr_col[z].rc_abd;
1428 abd_t *tabds[] = { xabd, yabd, zabd };
1430 rr->rr_col[CODE_P].rc_abd,
1431 rr->rr_col[CODE_Q].rc_abd,
1432 rr->rr_col[CODE_R].rc_abd
1436 return ((1 << CODE_P) | (1 << CODE_Q) | (1 << CODE_R));
1438 unsigned coeff[MUL_CNT];
1439 raidz_rec_pqr_coeff(rr, tgtidx, coeff);
1442 * Check if some of targets is shorter then others
1443 * In this case, shorter target needs to be replaced with
1444 * new buffer so that syndrome can be calculated.
1446 if (ysize < xsize) {
1447 yabd = abd_alloc(xsize, B_FALSE);
1450 if (zsize < xsize) {
1451 zabd = abd_alloc(xsize, B_FALSE);
1457 /* Start with first data column if present */
1459 raidz_copy(xabd, rr->rr_col[firstdc].rc_abd, xsize);
1460 raidz_copy(yabd, rr->rr_col[firstdc].rc_abd, xsize);
1461 raidz_copy(zabd, rr->rr_col[firstdc].rc_abd, xsize);
1463 raidz_zero(xabd, xsize);
1464 raidz_zero(yabd, xsize);
1465 raidz_zero(zabd, xsize);
1468 /* generate q_syndrome */
1469 for (c = firstdc+1; c < ncols; c++) {
1470 if (c == x || c == y || c == z) {
1474 dabd = rr->rr_col[c].rc_abd;
1475 dsize = rr->rr_col[c].rc_size;
1478 abd_raidz_gen_iterate(tabds, dabd, xsize, dsize, 3,
1482 abd_raidz_rec_iterate(cabds, tabds, xsize, 3, raidz_rec_pqr_abd, coeff);
1485 * Copy shorter targets back to the original abd buffer
1488 raidz_copy(rr->rr_col[y].rc_abd, yabd, ysize);
1490 raidz_copy(rr->rr_col[z].rc_abd, zabd, zsize);
1499 return ((1 << CODE_P) | (1 << CODE_Q) | (1 << CODE_R));
1502 #endif /* _VDEV_RAIDZ_MATH_IMPL_H */