xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_raidz.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  * CDDL HEADER START
3*eda14cbcSMatt Macy  *
4*eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5*eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6*eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7*eda14cbcSMatt Macy  *
8*eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10*eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11*eda14cbcSMatt Macy  * and limitations under the License.
12*eda14cbcSMatt Macy  *
13*eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14*eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16*eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17*eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*eda14cbcSMatt Macy  *
19*eda14cbcSMatt Macy  * CDDL HEADER END
20*eda14cbcSMatt Macy  */
21*eda14cbcSMatt Macy 
22*eda14cbcSMatt Macy /*
23*eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*eda14cbcSMatt Macy  * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
25*eda14cbcSMatt Macy  * Copyright (c) 2016 Gvozden Nešković. All rights reserved.
26*eda14cbcSMatt Macy  */
27*eda14cbcSMatt Macy 
28*eda14cbcSMatt Macy #include <sys/zfs_context.h>
29*eda14cbcSMatt Macy #include <sys/spa.h>
30*eda14cbcSMatt Macy #include <sys/vdev_impl.h>
31*eda14cbcSMatt Macy #include <sys/zio.h>
32*eda14cbcSMatt Macy #include <sys/zio_checksum.h>
33*eda14cbcSMatt Macy #include <sys/abd.h>
34*eda14cbcSMatt Macy #include <sys/fs/zfs.h>
35*eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h>
36*eda14cbcSMatt Macy #include <sys/vdev_raidz.h>
37*eda14cbcSMatt Macy #include <sys/vdev_raidz_impl.h>
38*eda14cbcSMatt Macy 
39*eda14cbcSMatt Macy #ifdef ZFS_DEBUG
40*eda14cbcSMatt Macy #include <sys/vdev.h>	/* For vdev_xlate() in vdev_raidz_io_verify() */
41*eda14cbcSMatt Macy #endif
42*eda14cbcSMatt Macy 
43*eda14cbcSMatt Macy /*
44*eda14cbcSMatt Macy  * Virtual device vector for RAID-Z.
45*eda14cbcSMatt Macy  *
46*eda14cbcSMatt Macy  * This vdev supports single, double, and triple parity. For single parity,
47*eda14cbcSMatt Macy  * we use a simple XOR of all the data columns. For double or triple parity,
48*eda14cbcSMatt Macy  * we use a special case of Reed-Solomon coding. This extends the
49*eda14cbcSMatt Macy  * technique described in "The mathematics of RAID-6" by H. Peter Anvin by
50*eda14cbcSMatt Macy  * drawing on the system described in "A Tutorial on Reed-Solomon Coding for
51*eda14cbcSMatt Macy  * Fault-Tolerance in RAID-like Systems" by James S. Plank on which the
52*eda14cbcSMatt Macy  * former is also based. The latter is designed to provide higher performance
53*eda14cbcSMatt Macy  * for writes.
54*eda14cbcSMatt Macy  *
55*eda14cbcSMatt Macy  * Note that the Plank paper claimed to support arbitrary N+M, but was then
56*eda14cbcSMatt Macy  * amended six years later identifying a critical flaw that invalidates its
57*eda14cbcSMatt Macy  * claims. Nevertheless, the technique can be adapted to work for up to
58*eda14cbcSMatt Macy  * triple parity. For additional parity, the amendment "Note: Correction to
59*eda14cbcSMatt Macy  * the 1997 Tutorial on Reed-Solomon Coding" by James S. Plank and Ying Ding
60*eda14cbcSMatt Macy  * is viable, but the additional complexity means that write performance will
61*eda14cbcSMatt Macy  * suffer.
62*eda14cbcSMatt Macy  *
63*eda14cbcSMatt Macy  * All of the methods above operate on a Galois field, defined over the
64*eda14cbcSMatt Macy  * integers mod 2^N. In our case we choose N=8 for GF(8) so that all elements
65*eda14cbcSMatt Macy  * can be expressed with a single byte. Briefly, the operations on the
66*eda14cbcSMatt Macy  * field are defined as follows:
67*eda14cbcSMatt Macy  *
68*eda14cbcSMatt Macy  *   o addition (+) is represented by a bitwise XOR
69*eda14cbcSMatt Macy  *   o subtraction (-) is therefore identical to addition: A + B = A - B
70*eda14cbcSMatt Macy  *   o multiplication of A by 2 is defined by the following bitwise expression:
71*eda14cbcSMatt Macy  *
72*eda14cbcSMatt Macy  *	(A * 2)_7 = A_6
73*eda14cbcSMatt Macy  *	(A * 2)_6 = A_5
74*eda14cbcSMatt Macy  *	(A * 2)_5 = A_4
75*eda14cbcSMatt Macy  *	(A * 2)_4 = A_3 + A_7
76*eda14cbcSMatt Macy  *	(A * 2)_3 = A_2 + A_7
77*eda14cbcSMatt Macy  *	(A * 2)_2 = A_1 + A_7
78*eda14cbcSMatt Macy  *	(A * 2)_1 = A_0
79*eda14cbcSMatt Macy  *	(A * 2)_0 = A_7
80*eda14cbcSMatt Macy  *
81*eda14cbcSMatt Macy  * In C, multiplying by 2 is therefore ((a << 1) ^ ((a & 0x80) ? 0x1d : 0)).
82*eda14cbcSMatt Macy  * As an aside, this multiplication is derived from the error correcting
83*eda14cbcSMatt Macy  * primitive polynomial x^8 + x^4 + x^3 + x^2 + 1.
84*eda14cbcSMatt Macy  *
85*eda14cbcSMatt Macy  * Observe that any number in the field (except for 0) can be expressed as a
86*eda14cbcSMatt Macy  * power of 2 -- a generator for the field. We store a table of the powers of
87*eda14cbcSMatt Macy  * 2 and logs base 2 for quick look ups, and exploit the fact that A * B can
88*eda14cbcSMatt Macy  * be rewritten as 2^(log_2(A) + log_2(B)) (where '+' is normal addition rather
89*eda14cbcSMatt Macy  * than field addition). The inverse of a field element A (A^-1) is therefore
90*eda14cbcSMatt Macy  * A ^ (255 - 1) = A^254.
91*eda14cbcSMatt Macy  *
92*eda14cbcSMatt Macy  * The up-to-three parity columns, P, Q, R over several data columns,
93*eda14cbcSMatt Macy  * D_0, ... D_n-1, can be expressed by field operations:
94*eda14cbcSMatt Macy  *
95*eda14cbcSMatt Macy  *	P = D_0 + D_1 + ... + D_n-2 + D_n-1
96*eda14cbcSMatt Macy  *	Q = 2^n-1 * D_0 + 2^n-2 * D_1 + ... + 2^1 * D_n-2 + 2^0 * D_n-1
97*eda14cbcSMatt Macy  *	  = ((...((D_0) * 2 + D_1) * 2 + ...) * 2 + D_n-2) * 2 + D_n-1
98*eda14cbcSMatt Macy  *	R = 4^n-1 * D_0 + 4^n-2 * D_1 + ... + 4^1 * D_n-2 + 4^0 * D_n-1
99*eda14cbcSMatt Macy  *	  = ((...((D_0) * 4 + D_1) * 4 + ...) * 4 + D_n-2) * 4 + D_n-1
100*eda14cbcSMatt Macy  *
101*eda14cbcSMatt Macy  * We chose 1, 2, and 4 as our generators because 1 corresponds to the trivial
102*eda14cbcSMatt Macy  * XOR operation, and 2 and 4 can be computed quickly and generate linearly-
103*eda14cbcSMatt Macy  * independent coefficients. (There are no additional coefficients that have
104*eda14cbcSMatt Macy  * this property which is why the uncorrected Plank method breaks down.)
105*eda14cbcSMatt Macy  *
106*eda14cbcSMatt Macy  * See the reconstruction code below for how P, Q and R can used individually
107*eda14cbcSMatt Macy  * or in concert to recover missing data columns.
108*eda14cbcSMatt Macy  */
109*eda14cbcSMatt Macy 
110*eda14cbcSMatt Macy #define	VDEV_RAIDZ_P		0
111*eda14cbcSMatt Macy #define	VDEV_RAIDZ_Q		1
112*eda14cbcSMatt Macy #define	VDEV_RAIDZ_R		2
113*eda14cbcSMatt Macy 
114*eda14cbcSMatt Macy #define	VDEV_RAIDZ_MUL_2(x)	(((x) << 1) ^ (((x) & 0x80) ? 0x1d : 0))
115*eda14cbcSMatt Macy #define	VDEV_RAIDZ_MUL_4(x)	(VDEV_RAIDZ_MUL_2(VDEV_RAIDZ_MUL_2(x)))
116*eda14cbcSMatt Macy 
117*eda14cbcSMatt Macy /*
118*eda14cbcSMatt Macy  * We provide a mechanism to perform the field multiplication operation on a
119*eda14cbcSMatt Macy  * 64-bit value all at once rather than a byte at a time. This works by
120*eda14cbcSMatt Macy  * creating a mask from the top bit in each byte and using that to
121*eda14cbcSMatt Macy  * conditionally apply the XOR of 0x1d.
122*eda14cbcSMatt Macy  */
123*eda14cbcSMatt Macy #define	VDEV_RAIDZ_64MUL_2(x, mask) \
124*eda14cbcSMatt Macy { \
125*eda14cbcSMatt Macy 	(mask) = (x) & 0x8080808080808080ULL; \
126*eda14cbcSMatt Macy 	(mask) = ((mask) << 1) - ((mask) >> 7); \
127*eda14cbcSMatt Macy 	(x) = (((x) << 1) & 0xfefefefefefefefeULL) ^ \
128*eda14cbcSMatt Macy 	    ((mask) & 0x1d1d1d1d1d1d1d1dULL); \
129*eda14cbcSMatt Macy }
130*eda14cbcSMatt Macy 
131*eda14cbcSMatt Macy #define	VDEV_RAIDZ_64MUL_4(x, mask) \
132*eda14cbcSMatt Macy { \
133*eda14cbcSMatt Macy 	VDEV_RAIDZ_64MUL_2((x), mask); \
134*eda14cbcSMatt Macy 	VDEV_RAIDZ_64MUL_2((x), mask); \
135*eda14cbcSMatt Macy }
136*eda14cbcSMatt Macy 
137*eda14cbcSMatt Macy void
138*eda14cbcSMatt Macy vdev_raidz_map_free(raidz_map_t *rm)
139*eda14cbcSMatt Macy {
140*eda14cbcSMatt Macy 	int c;
141*eda14cbcSMatt Macy 
142*eda14cbcSMatt Macy 	for (c = 0; c < rm->rm_firstdatacol; c++) {
143*eda14cbcSMatt Macy 		abd_free(rm->rm_col[c].rc_abd);
144*eda14cbcSMatt Macy 
145*eda14cbcSMatt Macy 		if (rm->rm_col[c].rc_gdata != NULL)
146*eda14cbcSMatt Macy 			abd_free(rm->rm_col[c].rc_gdata);
147*eda14cbcSMatt Macy 	}
148*eda14cbcSMatt Macy 
149*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++)
150*eda14cbcSMatt Macy 		abd_put(rm->rm_col[c].rc_abd);
151*eda14cbcSMatt Macy 
152*eda14cbcSMatt Macy 	if (rm->rm_abd_copy != NULL)
153*eda14cbcSMatt Macy 		abd_free(rm->rm_abd_copy);
154*eda14cbcSMatt Macy 
155*eda14cbcSMatt Macy 	kmem_free(rm, offsetof(raidz_map_t, rm_col[rm->rm_scols]));
156*eda14cbcSMatt Macy }
157*eda14cbcSMatt Macy 
158*eda14cbcSMatt Macy static void
159*eda14cbcSMatt Macy vdev_raidz_map_free_vsd(zio_t *zio)
160*eda14cbcSMatt Macy {
161*eda14cbcSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
162*eda14cbcSMatt Macy 
163*eda14cbcSMatt Macy 	ASSERT0(rm->rm_freed);
164*eda14cbcSMatt Macy 	rm->rm_freed = 1;
165*eda14cbcSMatt Macy 
166*eda14cbcSMatt Macy 	if (rm->rm_reports == 0)
167*eda14cbcSMatt Macy 		vdev_raidz_map_free(rm);
168*eda14cbcSMatt Macy }
169*eda14cbcSMatt Macy 
170*eda14cbcSMatt Macy /*ARGSUSED*/
171*eda14cbcSMatt Macy static void
172*eda14cbcSMatt Macy vdev_raidz_cksum_free(void *arg, size_t ignored)
173*eda14cbcSMatt Macy {
174*eda14cbcSMatt Macy 	raidz_map_t *rm = arg;
175*eda14cbcSMatt Macy 
176*eda14cbcSMatt Macy 	ASSERT3U(rm->rm_reports, >, 0);
177*eda14cbcSMatt Macy 
178*eda14cbcSMatt Macy 	if (--rm->rm_reports == 0 && rm->rm_freed != 0)
179*eda14cbcSMatt Macy 		vdev_raidz_map_free(rm);
180*eda14cbcSMatt Macy }
181*eda14cbcSMatt Macy 
182*eda14cbcSMatt Macy static void
183*eda14cbcSMatt Macy vdev_raidz_cksum_finish(zio_cksum_report_t *zcr, const abd_t *good_data)
184*eda14cbcSMatt Macy {
185*eda14cbcSMatt Macy 	raidz_map_t *rm = zcr->zcr_cbdata;
186*eda14cbcSMatt Macy 	const size_t c = zcr->zcr_cbinfo;
187*eda14cbcSMatt Macy 	size_t x, offset;
188*eda14cbcSMatt Macy 
189*eda14cbcSMatt Macy 	const abd_t *good = NULL;
190*eda14cbcSMatt Macy 	const abd_t *bad = rm->rm_col[c].rc_abd;
191*eda14cbcSMatt Macy 
192*eda14cbcSMatt Macy 	if (good_data == NULL) {
193*eda14cbcSMatt Macy 		zfs_ereport_finish_checksum(zcr, NULL, NULL, B_FALSE);
194*eda14cbcSMatt Macy 		return;
195*eda14cbcSMatt Macy 	}
196*eda14cbcSMatt Macy 
197*eda14cbcSMatt Macy 	if (c < rm->rm_firstdatacol) {
198*eda14cbcSMatt Macy 		/*
199*eda14cbcSMatt Macy 		 * The first time through, calculate the parity blocks for
200*eda14cbcSMatt Macy 		 * the good data (this relies on the fact that the good
201*eda14cbcSMatt Macy 		 * data never changes for a given logical ZIO)
202*eda14cbcSMatt Macy 		 */
203*eda14cbcSMatt Macy 		if (rm->rm_col[0].rc_gdata == NULL) {
204*eda14cbcSMatt Macy 			abd_t *bad_parity[VDEV_RAIDZ_MAXPARITY];
205*eda14cbcSMatt Macy 
206*eda14cbcSMatt Macy 			/*
207*eda14cbcSMatt Macy 			 * Set up the rm_col[]s to generate the parity for
208*eda14cbcSMatt Macy 			 * good_data, first saving the parity bufs and
209*eda14cbcSMatt Macy 			 * replacing them with buffers to hold the result.
210*eda14cbcSMatt Macy 			 */
211*eda14cbcSMatt Macy 			for (x = 0; x < rm->rm_firstdatacol; x++) {
212*eda14cbcSMatt Macy 				bad_parity[x] = rm->rm_col[x].rc_abd;
213*eda14cbcSMatt Macy 				rm->rm_col[x].rc_abd =
214*eda14cbcSMatt Macy 				    rm->rm_col[x].rc_gdata =
215*eda14cbcSMatt Macy 				    abd_alloc_sametype(rm->rm_col[x].rc_abd,
216*eda14cbcSMatt Macy 				    rm->rm_col[x].rc_size);
217*eda14cbcSMatt Macy 			}
218*eda14cbcSMatt Macy 
219*eda14cbcSMatt Macy 			/* fill in the data columns from good_data */
220*eda14cbcSMatt Macy 			offset = 0;
221*eda14cbcSMatt Macy 			for (; x < rm->rm_cols; x++) {
222*eda14cbcSMatt Macy 				abd_put(rm->rm_col[x].rc_abd);
223*eda14cbcSMatt Macy 
224*eda14cbcSMatt Macy 				rm->rm_col[x].rc_abd =
225*eda14cbcSMatt Macy 				    abd_get_offset_size((abd_t *)good_data,
226*eda14cbcSMatt Macy 				    offset, rm->rm_col[x].rc_size);
227*eda14cbcSMatt Macy 				offset += rm->rm_col[x].rc_size;
228*eda14cbcSMatt Macy 			}
229*eda14cbcSMatt Macy 
230*eda14cbcSMatt Macy 			/*
231*eda14cbcSMatt Macy 			 * Construct the parity from the good data.
232*eda14cbcSMatt Macy 			 */
233*eda14cbcSMatt Macy 			vdev_raidz_generate_parity(rm);
234*eda14cbcSMatt Macy 
235*eda14cbcSMatt Macy 			/* restore everything back to its original state */
236*eda14cbcSMatt Macy 			for (x = 0; x < rm->rm_firstdatacol; x++)
237*eda14cbcSMatt Macy 				rm->rm_col[x].rc_abd = bad_parity[x];
238*eda14cbcSMatt Macy 
239*eda14cbcSMatt Macy 			offset = 0;
240*eda14cbcSMatt Macy 			for (x = rm->rm_firstdatacol; x < rm->rm_cols; x++) {
241*eda14cbcSMatt Macy 				abd_put(rm->rm_col[x].rc_abd);
242*eda14cbcSMatt Macy 				rm->rm_col[x].rc_abd = abd_get_offset_size(
243*eda14cbcSMatt Macy 				    rm->rm_abd_copy, offset,
244*eda14cbcSMatt Macy 				    rm->rm_col[x].rc_size);
245*eda14cbcSMatt Macy 				offset += rm->rm_col[x].rc_size;
246*eda14cbcSMatt Macy 			}
247*eda14cbcSMatt Macy 		}
248*eda14cbcSMatt Macy 
249*eda14cbcSMatt Macy 		ASSERT3P(rm->rm_col[c].rc_gdata, !=, NULL);
250*eda14cbcSMatt Macy 		good = abd_get_offset_size(rm->rm_col[c].rc_gdata, 0,
251*eda14cbcSMatt Macy 		    rm->rm_col[c].rc_size);
252*eda14cbcSMatt Macy 	} else {
253*eda14cbcSMatt Macy 		/* adjust good_data to point at the start of our column */
254*eda14cbcSMatt Macy 		offset = 0;
255*eda14cbcSMatt Macy 		for (x = rm->rm_firstdatacol; x < c; x++)
256*eda14cbcSMatt Macy 			offset += rm->rm_col[x].rc_size;
257*eda14cbcSMatt Macy 
258*eda14cbcSMatt Macy 		good = abd_get_offset_size((abd_t *)good_data, offset,
259*eda14cbcSMatt Macy 		    rm->rm_col[c].rc_size);
260*eda14cbcSMatt Macy 	}
261*eda14cbcSMatt Macy 
262*eda14cbcSMatt Macy 	/* we drop the ereport if it ends up that the data was good */
263*eda14cbcSMatt Macy 	zfs_ereport_finish_checksum(zcr, good, bad, B_TRUE);
264*eda14cbcSMatt Macy 	abd_put((abd_t *)good);
265*eda14cbcSMatt Macy }
266*eda14cbcSMatt Macy 
267*eda14cbcSMatt Macy /*
268*eda14cbcSMatt Macy  * Invoked indirectly by zfs_ereport_start_checksum(), called
269*eda14cbcSMatt Macy  * below when our read operation fails completely.  The main point
270*eda14cbcSMatt Macy  * is to keep a copy of everything we read from disk, so that at
271*eda14cbcSMatt Macy  * vdev_raidz_cksum_finish() time we can compare it with the good data.
272*eda14cbcSMatt Macy  */
273*eda14cbcSMatt Macy static void
274*eda14cbcSMatt Macy vdev_raidz_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *arg)
275*eda14cbcSMatt Macy {
276*eda14cbcSMatt Macy 	size_t c = (size_t)(uintptr_t)arg;
277*eda14cbcSMatt Macy 	size_t offset;
278*eda14cbcSMatt Macy 
279*eda14cbcSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
280*eda14cbcSMatt Macy 	size_t size;
281*eda14cbcSMatt Macy 
282*eda14cbcSMatt Macy 	/* set up the report and bump the refcount  */
283*eda14cbcSMatt Macy 	zcr->zcr_cbdata = rm;
284*eda14cbcSMatt Macy 	zcr->zcr_cbinfo = c;
285*eda14cbcSMatt Macy 	zcr->zcr_finish = vdev_raidz_cksum_finish;
286*eda14cbcSMatt Macy 	zcr->zcr_free = vdev_raidz_cksum_free;
287*eda14cbcSMatt Macy 
288*eda14cbcSMatt Macy 	rm->rm_reports++;
289*eda14cbcSMatt Macy 	ASSERT3U(rm->rm_reports, >, 0);
290*eda14cbcSMatt Macy 
291*eda14cbcSMatt Macy 	if (rm->rm_abd_copy != NULL)
292*eda14cbcSMatt Macy 		return;
293*eda14cbcSMatt Macy 
294*eda14cbcSMatt Macy 	/*
295*eda14cbcSMatt Macy 	 * It's the first time we're called for this raidz_map_t, so we need
296*eda14cbcSMatt Macy 	 * to copy the data aside; there's no guarantee that our zio's buffer
297*eda14cbcSMatt Macy 	 * won't be re-used for something else.
298*eda14cbcSMatt Macy 	 *
299*eda14cbcSMatt Macy 	 * Our parity data is already in separate buffers, so there's no need
300*eda14cbcSMatt Macy 	 * to copy them.
301*eda14cbcSMatt Macy 	 */
302*eda14cbcSMatt Macy 
303*eda14cbcSMatt Macy 	size = 0;
304*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++)
305*eda14cbcSMatt Macy 		size += rm->rm_col[c].rc_size;
306*eda14cbcSMatt Macy 
307*eda14cbcSMatt Macy 	rm->rm_abd_copy = abd_alloc_for_io(size, B_FALSE);
308*eda14cbcSMatt Macy 
309*eda14cbcSMatt Macy 	for (offset = 0, c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
310*eda14cbcSMatt Macy 		raidz_col_t *col = &rm->rm_col[c];
311*eda14cbcSMatt Macy 		abd_t *tmp = abd_get_offset_size(rm->rm_abd_copy, offset,
312*eda14cbcSMatt Macy 		    col->rc_size);
313*eda14cbcSMatt Macy 
314*eda14cbcSMatt Macy 		abd_copy(tmp, col->rc_abd, col->rc_size);
315*eda14cbcSMatt Macy 
316*eda14cbcSMatt Macy 		abd_put(col->rc_abd);
317*eda14cbcSMatt Macy 		col->rc_abd = tmp;
318*eda14cbcSMatt Macy 
319*eda14cbcSMatt Macy 		offset += col->rc_size;
320*eda14cbcSMatt Macy 	}
321*eda14cbcSMatt Macy 	ASSERT3U(offset, ==, size);
322*eda14cbcSMatt Macy }
323*eda14cbcSMatt Macy 
324*eda14cbcSMatt Macy static const zio_vsd_ops_t vdev_raidz_vsd_ops = {
325*eda14cbcSMatt Macy 	.vsd_free = vdev_raidz_map_free_vsd,
326*eda14cbcSMatt Macy 	.vsd_cksum_report = vdev_raidz_cksum_report
327*eda14cbcSMatt Macy };
328*eda14cbcSMatt Macy 
329*eda14cbcSMatt Macy /*
330*eda14cbcSMatt Macy  * Divides the IO evenly across all child vdevs; usually, dcols is
331*eda14cbcSMatt Macy  * the number of children in the target vdev.
332*eda14cbcSMatt Macy  *
333*eda14cbcSMatt Macy  * Avoid inlining the function to keep vdev_raidz_io_start(), which
334*eda14cbcSMatt Macy  * is this functions only caller, as small as possible on the stack.
335*eda14cbcSMatt Macy  */
336*eda14cbcSMatt Macy noinline raidz_map_t *
337*eda14cbcSMatt Macy vdev_raidz_map_alloc(zio_t *zio, uint64_t ashift, uint64_t dcols,
338*eda14cbcSMatt Macy     uint64_t nparity)
339*eda14cbcSMatt Macy {
340*eda14cbcSMatt Macy 	raidz_map_t *rm;
341*eda14cbcSMatt Macy 	/* The starting RAIDZ (parent) vdev sector of the block. */
342*eda14cbcSMatt Macy 	uint64_t b = zio->io_offset >> ashift;
343*eda14cbcSMatt Macy 	/* The zio's size in units of the vdev's minimum sector size. */
344*eda14cbcSMatt Macy 	uint64_t s = zio->io_size >> ashift;
345*eda14cbcSMatt Macy 	/* The first column for this stripe. */
346*eda14cbcSMatt Macy 	uint64_t f = b % dcols;
347*eda14cbcSMatt Macy 	/* The starting byte offset on each child vdev. */
348*eda14cbcSMatt Macy 	uint64_t o = (b / dcols) << ashift;
349*eda14cbcSMatt Macy 	uint64_t q, r, c, bc, col, acols, scols, coff, devidx, asize, tot;
350*eda14cbcSMatt Macy 	uint64_t off = 0;
351*eda14cbcSMatt Macy 
352*eda14cbcSMatt Macy 	/*
353*eda14cbcSMatt Macy 	 * "Quotient": The number of data sectors for this stripe on all but
354*eda14cbcSMatt Macy 	 * the "big column" child vdevs that also contain "remainder" data.
355*eda14cbcSMatt Macy 	 */
356*eda14cbcSMatt Macy 	q = s / (dcols - nparity);
357*eda14cbcSMatt Macy 
358*eda14cbcSMatt Macy 	/*
359*eda14cbcSMatt Macy 	 * "Remainder": The number of partial stripe data sectors in this I/O.
360*eda14cbcSMatt Macy 	 * This will add a sector to some, but not all, child vdevs.
361*eda14cbcSMatt Macy 	 */
362*eda14cbcSMatt Macy 	r = s - q * (dcols - nparity);
363*eda14cbcSMatt Macy 
364*eda14cbcSMatt Macy 	/* The number of "big columns" - those which contain remainder data. */
365*eda14cbcSMatt Macy 	bc = (r == 0 ? 0 : r + nparity);
366*eda14cbcSMatt Macy 
367*eda14cbcSMatt Macy 	/*
368*eda14cbcSMatt Macy 	 * The total number of data and parity sectors associated with
369*eda14cbcSMatt Macy 	 * this I/O.
370*eda14cbcSMatt Macy 	 */
371*eda14cbcSMatt Macy 	tot = s + nparity * (q + (r == 0 ? 0 : 1));
372*eda14cbcSMatt Macy 
373*eda14cbcSMatt Macy 	/* acols: The columns that will be accessed. */
374*eda14cbcSMatt Macy 	/* scols: The columns that will be accessed or skipped. */
375*eda14cbcSMatt Macy 	if (q == 0) {
376*eda14cbcSMatt Macy 		/* Our I/O request doesn't span all child vdevs. */
377*eda14cbcSMatt Macy 		acols = bc;
378*eda14cbcSMatt Macy 		scols = MIN(dcols, roundup(bc, nparity + 1));
379*eda14cbcSMatt Macy 	} else {
380*eda14cbcSMatt Macy 		acols = dcols;
381*eda14cbcSMatt Macy 		scols = dcols;
382*eda14cbcSMatt Macy 	}
383*eda14cbcSMatt Macy 
384*eda14cbcSMatt Macy 	ASSERT3U(acols, <=, scols);
385*eda14cbcSMatt Macy 
386*eda14cbcSMatt Macy 	rm = kmem_alloc(offsetof(raidz_map_t, rm_col[scols]), KM_SLEEP);
387*eda14cbcSMatt Macy 
388*eda14cbcSMatt Macy 	rm->rm_cols = acols;
389*eda14cbcSMatt Macy 	rm->rm_scols = scols;
390*eda14cbcSMatt Macy 	rm->rm_bigcols = bc;
391*eda14cbcSMatt Macy 	rm->rm_skipstart = bc;
392*eda14cbcSMatt Macy 	rm->rm_missingdata = 0;
393*eda14cbcSMatt Macy 	rm->rm_missingparity = 0;
394*eda14cbcSMatt Macy 	rm->rm_firstdatacol = nparity;
395*eda14cbcSMatt Macy 	rm->rm_abd_copy = NULL;
396*eda14cbcSMatt Macy 	rm->rm_reports = 0;
397*eda14cbcSMatt Macy 	rm->rm_freed = 0;
398*eda14cbcSMatt Macy 	rm->rm_ecksuminjected = 0;
399*eda14cbcSMatt Macy 
400*eda14cbcSMatt Macy 	asize = 0;
401*eda14cbcSMatt Macy 
402*eda14cbcSMatt Macy 	for (c = 0; c < scols; c++) {
403*eda14cbcSMatt Macy 		col = f + c;
404*eda14cbcSMatt Macy 		coff = o;
405*eda14cbcSMatt Macy 		if (col >= dcols) {
406*eda14cbcSMatt Macy 			col -= dcols;
407*eda14cbcSMatt Macy 			coff += 1ULL << ashift;
408*eda14cbcSMatt Macy 		}
409*eda14cbcSMatt Macy 		rm->rm_col[c].rc_devidx = col;
410*eda14cbcSMatt Macy 		rm->rm_col[c].rc_offset = coff;
411*eda14cbcSMatt Macy 		rm->rm_col[c].rc_abd = NULL;
412*eda14cbcSMatt Macy 		rm->rm_col[c].rc_gdata = NULL;
413*eda14cbcSMatt Macy 		rm->rm_col[c].rc_error = 0;
414*eda14cbcSMatt Macy 		rm->rm_col[c].rc_tried = 0;
415*eda14cbcSMatt Macy 		rm->rm_col[c].rc_skipped = 0;
416*eda14cbcSMatt Macy 
417*eda14cbcSMatt Macy 		if (c >= acols)
418*eda14cbcSMatt Macy 			rm->rm_col[c].rc_size = 0;
419*eda14cbcSMatt Macy 		else if (c < bc)
420*eda14cbcSMatt Macy 			rm->rm_col[c].rc_size = (q + 1) << ashift;
421*eda14cbcSMatt Macy 		else
422*eda14cbcSMatt Macy 			rm->rm_col[c].rc_size = q << ashift;
423*eda14cbcSMatt Macy 
424*eda14cbcSMatt Macy 		asize += rm->rm_col[c].rc_size;
425*eda14cbcSMatt Macy 	}
426*eda14cbcSMatt Macy 
427*eda14cbcSMatt Macy 	ASSERT3U(asize, ==, tot << ashift);
428*eda14cbcSMatt Macy 	rm->rm_asize = roundup(asize, (nparity + 1) << ashift);
429*eda14cbcSMatt Macy 	rm->rm_nskip = roundup(tot, nparity + 1) - tot;
430*eda14cbcSMatt Macy 	ASSERT3U(rm->rm_asize - asize, ==, rm->rm_nskip << ashift);
431*eda14cbcSMatt Macy 	ASSERT3U(rm->rm_nskip, <=, nparity);
432*eda14cbcSMatt Macy 
433*eda14cbcSMatt Macy 	for (c = 0; c < rm->rm_firstdatacol; c++)
434*eda14cbcSMatt Macy 		rm->rm_col[c].rc_abd =
435*eda14cbcSMatt Macy 		    abd_alloc_linear(rm->rm_col[c].rc_size, B_FALSE);
436*eda14cbcSMatt Macy 
437*eda14cbcSMatt Macy 	rm->rm_col[c].rc_abd = abd_get_offset_size(zio->io_abd, 0,
438*eda14cbcSMatt Macy 	    rm->rm_col[c].rc_size);
439*eda14cbcSMatt Macy 	off = rm->rm_col[c].rc_size;
440*eda14cbcSMatt Macy 
441*eda14cbcSMatt Macy 	for (c = c + 1; c < acols; c++) {
442*eda14cbcSMatt Macy 		rm->rm_col[c].rc_abd = abd_get_offset_size(zio->io_abd, off,
443*eda14cbcSMatt Macy 		    rm->rm_col[c].rc_size);
444*eda14cbcSMatt Macy 		off += rm->rm_col[c].rc_size;
445*eda14cbcSMatt Macy 	}
446*eda14cbcSMatt Macy 
447*eda14cbcSMatt Macy 	/*
448*eda14cbcSMatt Macy 	 * If all data stored spans all columns, there's a danger that parity
449*eda14cbcSMatt Macy 	 * will always be on the same device and, since parity isn't read
450*eda14cbcSMatt Macy 	 * during normal operation, that device's I/O bandwidth won't be
451*eda14cbcSMatt Macy 	 * used effectively. We therefore switch the parity every 1MB.
452*eda14cbcSMatt Macy 	 *
453*eda14cbcSMatt Macy 	 * ... at least that was, ostensibly, the theory. As a practical
454*eda14cbcSMatt Macy 	 * matter unless we juggle the parity between all devices evenly, we
455*eda14cbcSMatt Macy 	 * won't see any benefit. Further, occasional writes that aren't a
456*eda14cbcSMatt Macy 	 * multiple of the LCM of the number of children and the minimum
457*eda14cbcSMatt Macy 	 * stripe width are sufficient to avoid pessimal behavior.
458*eda14cbcSMatt Macy 	 * Unfortunately, this decision created an implicit on-disk format
459*eda14cbcSMatt Macy 	 * requirement that we need to support for all eternity, but only
460*eda14cbcSMatt Macy 	 * for single-parity RAID-Z.
461*eda14cbcSMatt Macy 	 *
462*eda14cbcSMatt Macy 	 * If we intend to skip a sector in the zeroth column for padding
463*eda14cbcSMatt Macy 	 * we must make sure to note this swap. We will never intend to
464*eda14cbcSMatt Macy 	 * skip the first column since at least one data and one parity
465*eda14cbcSMatt Macy 	 * column must appear in each row.
466*eda14cbcSMatt Macy 	 */
467*eda14cbcSMatt Macy 	ASSERT(rm->rm_cols >= 2);
468*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[0].rc_size == rm->rm_col[1].rc_size);
469*eda14cbcSMatt Macy 
470*eda14cbcSMatt Macy 	if (rm->rm_firstdatacol == 1 && (zio->io_offset & (1ULL << 20))) {
471*eda14cbcSMatt Macy 		devidx = rm->rm_col[0].rc_devidx;
472*eda14cbcSMatt Macy 		o = rm->rm_col[0].rc_offset;
473*eda14cbcSMatt Macy 		rm->rm_col[0].rc_devidx = rm->rm_col[1].rc_devidx;
474*eda14cbcSMatt Macy 		rm->rm_col[0].rc_offset = rm->rm_col[1].rc_offset;
475*eda14cbcSMatt Macy 		rm->rm_col[1].rc_devidx = devidx;
476*eda14cbcSMatt Macy 		rm->rm_col[1].rc_offset = o;
477*eda14cbcSMatt Macy 
478*eda14cbcSMatt Macy 		if (rm->rm_skipstart == 0)
479*eda14cbcSMatt Macy 			rm->rm_skipstart = 1;
480*eda14cbcSMatt Macy 	}
481*eda14cbcSMatt Macy 
482*eda14cbcSMatt Macy 	zio->io_vsd = rm;
483*eda14cbcSMatt Macy 	zio->io_vsd_ops = &vdev_raidz_vsd_ops;
484*eda14cbcSMatt Macy 
485*eda14cbcSMatt Macy 	/* init RAIDZ parity ops */
486*eda14cbcSMatt Macy 	rm->rm_ops = vdev_raidz_math_get_ops();
487*eda14cbcSMatt Macy 
488*eda14cbcSMatt Macy 	return (rm);
489*eda14cbcSMatt Macy }
490*eda14cbcSMatt Macy 
491*eda14cbcSMatt Macy struct pqr_struct {
492*eda14cbcSMatt Macy 	uint64_t *p;
493*eda14cbcSMatt Macy 	uint64_t *q;
494*eda14cbcSMatt Macy 	uint64_t *r;
495*eda14cbcSMatt Macy };
496*eda14cbcSMatt Macy 
497*eda14cbcSMatt Macy static int
498*eda14cbcSMatt Macy vdev_raidz_p_func(void *buf, size_t size, void *private)
499*eda14cbcSMatt Macy {
500*eda14cbcSMatt Macy 	struct pqr_struct *pqr = private;
501*eda14cbcSMatt Macy 	const uint64_t *src = buf;
502*eda14cbcSMatt Macy 	int i, cnt = size / sizeof (src[0]);
503*eda14cbcSMatt Macy 
504*eda14cbcSMatt Macy 	ASSERT(pqr->p && !pqr->q && !pqr->r);
505*eda14cbcSMatt Macy 
506*eda14cbcSMatt Macy 	for (i = 0; i < cnt; i++, src++, pqr->p++)
507*eda14cbcSMatt Macy 		*pqr->p ^= *src;
508*eda14cbcSMatt Macy 
509*eda14cbcSMatt Macy 	return (0);
510*eda14cbcSMatt Macy }
511*eda14cbcSMatt Macy 
512*eda14cbcSMatt Macy static int
513*eda14cbcSMatt Macy vdev_raidz_pq_func(void *buf, size_t size, void *private)
514*eda14cbcSMatt Macy {
515*eda14cbcSMatt Macy 	struct pqr_struct *pqr = private;
516*eda14cbcSMatt Macy 	const uint64_t *src = buf;
517*eda14cbcSMatt Macy 	uint64_t mask;
518*eda14cbcSMatt Macy 	int i, cnt = size / sizeof (src[0]);
519*eda14cbcSMatt Macy 
520*eda14cbcSMatt Macy 	ASSERT(pqr->p && pqr->q && !pqr->r);
521*eda14cbcSMatt Macy 
522*eda14cbcSMatt Macy 	for (i = 0; i < cnt; i++, src++, pqr->p++, pqr->q++) {
523*eda14cbcSMatt Macy 		*pqr->p ^= *src;
524*eda14cbcSMatt Macy 		VDEV_RAIDZ_64MUL_2(*pqr->q, mask);
525*eda14cbcSMatt Macy 		*pqr->q ^= *src;
526*eda14cbcSMatt Macy 	}
527*eda14cbcSMatt Macy 
528*eda14cbcSMatt Macy 	return (0);
529*eda14cbcSMatt Macy }
530*eda14cbcSMatt Macy 
531*eda14cbcSMatt Macy static int
532*eda14cbcSMatt Macy vdev_raidz_pqr_func(void *buf, size_t size, void *private)
533*eda14cbcSMatt Macy {
534*eda14cbcSMatt Macy 	struct pqr_struct *pqr = private;
535*eda14cbcSMatt Macy 	const uint64_t *src = buf;
536*eda14cbcSMatt Macy 	uint64_t mask;
537*eda14cbcSMatt Macy 	int i, cnt = size / sizeof (src[0]);
538*eda14cbcSMatt Macy 
539*eda14cbcSMatt Macy 	ASSERT(pqr->p && pqr->q && pqr->r);
540*eda14cbcSMatt Macy 
541*eda14cbcSMatt Macy 	for (i = 0; i < cnt; i++, src++, pqr->p++, pqr->q++, pqr->r++) {
542*eda14cbcSMatt Macy 		*pqr->p ^= *src;
543*eda14cbcSMatt Macy 		VDEV_RAIDZ_64MUL_2(*pqr->q, mask);
544*eda14cbcSMatt Macy 		*pqr->q ^= *src;
545*eda14cbcSMatt Macy 		VDEV_RAIDZ_64MUL_4(*pqr->r, mask);
546*eda14cbcSMatt Macy 		*pqr->r ^= *src;
547*eda14cbcSMatt Macy 	}
548*eda14cbcSMatt Macy 
549*eda14cbcSMatt Macy 	return (0);
550*eda14cbcSMatt Macy }
551*eda14cbcSMatt Macy 
552*eda14cbcSMatt Macy static void
553*eda14cbcSMatt Macy vdev_raidz_generate_parity_p(raidz_map_t *rm)
554*eda14cbcSMatt Macy {
555*eda14cbcSMatt Macy 	uint64_t *p;
556*eda14cbcSMatt Macy 	int c;
557*eda14cbcSMatt Macy 	abd_t *src;
558*eda14cbcSMatt Macy 
559*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
560*eda14cbcSMatt Macy 		src = rm->rm_col[c].rc_abd;
561*eda14cbcSMatt Macy 		p = abd_to_buf(rm->rm_col[VDEV_RAIDZ_P].rc_abd);
562*eda14cbcSMatt Macy 
563*eda14cbcSMatt Macy 		if (c == rm->rm_firstdatacol) {
564*eda14cbcSMatt Macy 			abd_copy_to_buf(p, src, rm->rm_col[c].rc_size);
565*eda14cbcSMatt Macy 		} else {
566*eda14cbcSMatt Macy 			struct pqr_struct pqr = { p, NULL, NULL };
567*eda14cbcSMatt Macy 			(void) abd_iterate_func(src, 0, rm->rm_col[c].rc_size,
568*eda14cbcSMatt Macy 			    vdev_raidz_p_func, &pqr);
569*eda14cbcSMatt Macy 		}
570*eda14cbcSMatt Macy 	}
571*eda14cbcSMatt Macy }
572*eda14cbcSMatt Macy 
573*eda14cbcSMatt Macy static void
574*eda14cbcSMatt Macy vdev_raidz_generate_parity_pq(raidz_map_t *rm)
575*eda14cbcSMatt Macy {
576*eda14cbcSMatt Macy 	uint64_t *p, *q, pcnt, ccnt, mask, i;
577*eda14cbcSMatt Macy 	int c;
578*eda14cbcSMatt Macy 	abd_t *src;
579*eda14cbcSMatt Macy 
580*eda14cbcSMatt Macy 	pcnt = rm->rm_col[VDEV_RAIDZ_P].rc_size / sizeof (p[0]);
581*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[VDEV_RAIDZ_P].rc_size ==
582*eda14cbcSMatt Macy 	    rm->rm_col[VDEV_RAIDZ_Q].rc_size);
583*eda14cbcSMatt Macy 
584*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
585*eda14cbcSMatt Macy 		src = rm->rm_col[c].rc_abd;
586*eda14cbcSMatt Macy 		p = abd_to_buf(rm->rm_col[VDEV_RAIDZ_P].rc_abd);
587*eda14cbcSMatt Macy 		q = abd_to_buf(rm->rm_col[VDEV_RAIDZ_Q].rc_abd);
588*eda14cbcSMatt Macy 
589*eda14cbcSMatt Macy 		ccnt = rm->rm_col[c].rc_size / sizeof (p[0]);
590*eda14cbcSMatt Macy 
591*eda14cbcSMatt Macy 		if (c == rm->rm_firstdatacol) {
592*eda14cbcSMatt Macy 			ASSERT(ccnt == pcnt || ccnt == 0);
593*eda14cbcSMatt Macy 			abd_copy_to_buf(p, src, rm->rm_col[c].rc_size);
594*eda14cbcSMatt Macy 			(void) memcpy(q, p, rm->rm_col[c].rc_size);
595*eda14cbcSMatt Macy 
596*eda14cbcSMatt Macy 			for (i = ccnt; i < pcnt; i++) {
597*eda14cbcSMatt Macy 				p[i] = 0;
598*eda14cbcSMatt Macy 				q[i] = 0;
599*eda14cbcSMatt Macy 			}
600*eda14cbcSMatt Macy 		} else {
601*eda14cbcSMatt Macy 			struct pqr_struct pqr = { p, q, NULL };
602*eda14cbcSMatt Macy 
603*eda14cbcSMatt Macy 			ASSERT(ccnt <= pcnt);
604*eda14cbcSMatt Macy 			(void) abd_iterate_func(src, 0, rm->rm_col[c].rc_size,
605*eda14cbcSMatt Macy 			    vdev_raidz_pq_func, &pqr);
606*eda14cbcSMatt Macy 
607*eda14cbcSMatt Macy 			/*
608*eda14cbcSMatt Macy 			 * Treat short columns as though they are full of 0s.
609*eda14cbcSMatt Macy 			 * Note that there's therefore nothing needed for P.
610*eda14cbcSMatt Macy 			 */
611*eda14cbcSMatt Macy 			for (i = ccnt; i < pcnt; i++) {
612*eda14cbcSMatt Macy 				VDEV_RAIDZ_64MUL_2(q[i], mask);
613*eda14cbcSMatt Macy 			}
614*eda14cbcSMatt Macy 		}
615*eda14cbcSMatt Macy 	}
616*eda14cbcSMatt Macy }
617*eda14cbcSMatt Macy 
618*eda14cbcSMatt Macy static void
619*eda14cbcSMatt Macy vdev_raidz_generate_parity_pqr(raidz_map_t *rm)
620*eda14cbcSMatt Macy {
621*eda14cbcSMatt Macy 	uint64_t *p, *q, *r, pcnt, ccnt, mask, i;
622*eda14cbcSMatt Macy 	int c;
623*eda14cbcSMatt Macy 	abd_t *src;
624*eda14cbcSMatt Macy 
625*eda14cbcSMatt Macy 	pcnt = rm->rm_col[VDEV_RAIDZ_P].rc_size / sizeof (p[0]);
626*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[VDEV_RAIDZ_P].rc_size ==
627*eda14cbcSMatt Macy 	    rm->rm_col[VDEV_RAIDZ_Q].rc_size);
628*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[VDEV_RAIDZ_P].rc_size ==
629*eda14cbcSMatt Macy 	    rm->rm_col[VDEV_RAIDZ_R].rc_size);
630*eda14cbcSMatt Macy 
631*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
632*eda14cbcSMatt Macy 		src = rm->rm_col[c].rc_abd;
633*eda14cbcSMatt Macy 		p = abd_to_buf(rm->rm_col[VDEV_RAIDZ_P].rc_abd);
634*eda14cbcSMatt Macy 		q = abd_to_buf(rm->rm_col[VDEV_RAIDZ_Q].rc_abd);
635*eda14cbcSMatt Macy 		r = abd_to_buf(rm->rm_col[VDEV_RAIDZ_R].rc_abd);
636*eda14cbcSMatt Macy 
637*eda14cbcSMatt Macy 		ccnt = rm->rm_col[c].rc_size / sizeof (p[0]);
638*eda14cbcSMatt Macy 
639*eda14cbcSMatt Macy 		if (c == rm->rm_firstdatacol) {
640*eda14cbcSMatt Macy 			ASSERT(ccnt == pcnt || ccnt == 0);
641*eda14cbcSMatt Macy 			abd_copy_to_buf(p, src, rm->rm_col[c].rc_size);
642*eda14cbcSMatt Macy 			(void) memcpy(q, p, rm->rm_col[c].rc_size);
643*eda14cbcSMatt Macy 			(void) memcpy(r, p, rm->rm_col[c].rc_size);
644*eda14cbcSMatt Macy 
645*eda14cbcSMatt Macy 			for (i = ccnt; i < pcnt; i++) {
646*eda14cbcSMatt Macy 				p[i] = 0;
647*eda14cbcSMatt Macy 				q[i] = 0;
648*eda14cbcSMatt Macy 				r[i] = 0;
649*eda14cbcSMatt Macy 			}
650*eda14cbcSMatt Macy 		} else {
651*eda14cbcSMatt Macy 			struct pqr_struct pqr = { p, q, r };
652*eda14cbcSMatt Macy 
653*eda14cbcSMatt Macy 			ASSERT(ccnt <= pcnt);
654*eda14cbcSMatt Macy 			(void) abd_iterate_func(src, 0, rm->rm_col[c].rc_size,
655*eda14cbcSMatt Macy 			    vdev_raidz_pqr_func, &pqr);
656*eda14cbcSMatt Macy 
657*eda14cbcSMatt Macy 			/*
658*eda14cbcSMatt Macy 			 * Treat short columns as though they are full of 0s.
659*eda14cbcSMatt Macy 			 * Note that there's therefore nothing needed for P.
660*eda14cbcSMatt Macy 			 */
661*eda14cbcSMatt Macy 			for (i = ccnt; i < pcnt; i++) {
662*eda14cbcSMatt Macy 				VDEV_RAIDZ_64MUL_2(q[i], mask);
663*eda14cbcSMatt Macy 				VDEV_RAIDZ_64MUL_4(r[i], mask);
664*eda14cbcSMatt Macy 			}
665*eda14cbcSMatt Macy 		}
666*eda14cbcSMatt Macy 	}
667*eda14cbcSMatt Macy }
668*eda14cbcSMatt Macy 
669*eda14cbcSMatt Macy /*
670*eda14cbcSMatt Macy  * Generate RAID parity in the first virtual columns according to the number of
671*eda14cbcSMatt Macy  * parity columns available.
672*eda14cbcSMatt Macy  */
673*eda14cbcSMatt Macy void
674*eda14cbcSMatt Macy vdev_raidz_generate_parity(raidz_map_t *rm)
675*eda14cbcSMatt Macy {
676*eda14cbcSMatt Macy 	/* Generate using the new math implementation */
677*eda14cbcSMatt Macy 	if (vdev_raidz_math_generate(rm) != RAIDZ_ORIGINAL_IMPL)
678*eda14cbcSMatt Macy 		return;
679*eda14cbcSMatt Macy 
680*eda14cbcSMatt Macy 	switch (rm->rm_firstdatacol) {
681*eda14cbcSMatt Macy 	case 1:
682*eda14cbcSMatt Macy 		vdev_raidz_generate_parity_p(rm);
683*eda14cbcSMatt Macy 		break;
684*eda14cbcSMatt Macy 	case 2:
685*eda14cbcSMatt Macy 		vdev_raidz_generate_parity_pq(rm);
686*eda14cbcSMatt Macy 		break;
687*eda14cbcSMatt Macy 	case 3:
688*eda14cbcSMatt Macy 		vdev_raidz_generate_parity_pqr(rm);
689*eda14cbcSMatt Macy 		break;
690*eda14cbcSMatt Macy 	default:
691*eda14cbcSMatt Macy 		cmn_err(CE_PANIC, "invalid RAID-Z configuration");
692*eda14cbcSMatt Macy 	}
693*eda14cbcSMatt Macy }
694*eda14cbcSMatt Macy 
695*eda14cbcSMatt Macy /* ARGSUSED */
696*eda14cbcSMatt Macy static int
697*eda14cbcSMatt Macy vdev_raidz_reconst_p_func(void *dbuf, void *sbuf, size_t size, void *private)
698*eda14cbcSMatt Macy {
699*eda14cbcSMatt Macy 	uint64_t *dst = dbuf;
700*eda14cbcSMatt Macy 	uint64_t *src = sbuf;
701*eda14cbcSMatt Macy 	int cnt = size / sizeof (src[0]);
702*eda14cbcSMatt Macy 
703*eda14cbcSMatt Macy 	for (int i = 0; i < cnt; i++) {
704*eda14cbcSMatt Macy 		dst[i] ^= src[i];
705*eda14cbcSMatt Macy 	}
706*eda14cbcSMatt Macy 
707*eda14cbcSMatt Macy 	return (0);
708*eda14cbcSMatt Macy }
709*eda14cbcSMatt Macy 
710*eda14cbcSMatt Macy /* ARGSUSED */
711*eda14cbcSMatt Macy static int
712*eda14cbcSMatt Macy vdev_raidz_reconst_q_pre_func(void *dbuf, void *sbuf, size_t size,
713*eda14cbcSMatt Macy     void *private)
714*eda14cbcSMatt Macy {
715*eda14cbcSMatt Macy 	uint64_t *dst = dbuf;
716*eda14cbcSMatt Macy 	uint64_t *src = sbuf;
717*eda14cbcSMatt Macy 	uint64_t mask;
718*eda14cbcSMatt Macy 	int cnt = size / sizeof (dst[0]);
719*eda14cbcSMatt Macy 
720*eda14cbcSMatt Macy 	for (int i = 0; i < cnt; i++, dst++, src++) {
721*eda14cbcSMatt Macy 		VDEV_RAIDZ_64MUL_2(*dst, mask);
722*eda14cbcSMatt Macy 		*dst ^= *src;
723*eda14cbcSMatt Macy 	}
724*eda14cbcSMatt Macy 
725*eda14cbcSMatt Macy 	return (0);
726*eda14cbcSMatt Macy }
727*eda14cbcSMatt Macy 
728*eda14cbcSMatt Macy /* ARGSUSED */
729*eda14cbcSMatt Macy static int
730*eda14cbcSMatt Macy vdev_raidz_reconst_q_pre_tail_func(void *buf, size_t size, void *private)
731*eda14cbcSMatt Macy {
732*eda14cbcSMatt Macy 	uint64_t *dst = buf;
733*eda14cbcSMatt Macy 	uint64_t mask;
734*eda14cbcSMatt Macy 	int cnt = size / sizeof (dst[0]);
735*eda14cbcSMatt Macy 
736*eda14cbcSMatt Macy 	for (int i = 0; i < cnt; i++, dst++) {
737*eda14cbcSMatt Macy 		/* same operation as vdev_raidz_reconst_q_pre_func() on dst */
738*eda14cbcSMatt Macy 		VDEV_RAIDZ_64MUL_2(*dst, mask);
739*eda14cbcSMatt Macy 	}
740*eda14cbcSMatt Macy 
741*eda14cbcSMatt Macy 	return (0);
742*eda14cbcSMatt Macy }
743*eda14cbcSMatt Macy 
744*eda14cbcSMatt Macy struct reconst_q_struct {
745*eda14cbcSMatt Macy 	uint64_t *q;
746*eda14cbcSMatt Macy 	int exp;
747*eda14cbcSMatt Macy };
748*eda14cbcSMatt Macy 
749*eda14cbcSMatt Macy static int
750*eda14cbcSMatt Macy vdev_raidz_reconst_q_post_func(void *buf, size_t size, void *private)
751*eda14cbcSMatt Macy {
752*eda14cbcSMatt Macy 	struct reconst_q_struct *rq = private;
753*eda14cbcSMatt Macy 	uint64_t *dst = buf;
754*eda14cbcSMatt Macy 	int cnt = size / sizeof (dst[0]);
755*eda14cbcSMatt Macy 
756*eda14cbcSMatt Macy 	for (int i = 0; i < cnt; i++, dst++, rq->q++) {
757*eda14cbcSMatt Macy 		int j;
758*eda14cbcSMatt Macy 		uint8_t *b;
759*eda14cbcSMatt Macy 
760*eda14cbcSMatt Macy 		*dst ^= *rq->q;
761*eda14cbcSMatt Macy 		for (j = 0, b = (uint8_t *)dst; j < 8; j++, b++) {
762*eda14cbcSMatt Macy 			*b = vdev_raidz_exp2(*b, rq->exp);
763*eda14cbcSMatt Macy 		}
764*eda14cbcSMatt Macy 	}
765*eda14cbcSMatt Macy 
766*eda14cbcSMatt Macy 	return (0);
767*eda14cbcSMatt Macy }
768*eda14cbcSMatt Macy 
769*eda14cbcSMatt Macy struct reconst_pq_struct {
770*eda14cbcSMatt Macy 	uint8_t *p;
771*eda14cbcSMatt Macy 	uint8_t *q;
772*eda14cbcSMatt Macy 	uint8_t *pxy;
773*eda14cbcSMatt Macy 	uint8_t *qxy;
774*eda14cbcSMatt Macy 	int aexp;
775*eda14cbcSMatt Macy 	int bexp;
776*eda14cbcSMatt Macy };
777*eda14cbcSMatt Macy 
778*eda14cbcSMatt Macy static int
779*eda14cbcSMatt Macy vdev_raidz_reconst_pq_func(void *xbuf, void *ybuf, size_t size, void *private)
780*eda14cbcSMatt Macy {
781*eda14cbcSMatt Macy 	struct reconst_pq_struct *rpq = private;
782*eda14cbcSMatt Macy 	uint8_t *xd = xbuf;
783*eda14cbcSMatt Macy 	uint8_t *yd = ybuf;
784*eda14cbcSMatt Macy 
785*eda14cbcSMatt Macy 	for (int i = 0; i < size;
786*eda14cbcSMatt Macy 	    i++, rpq->p++, rpq->q++, rpq->pxy++, rpq->qxy++, xd++, yd++) {
787*eda14cbcSMatt Macy 		*xd = vdev_raidz_exp2(*rpq->p ^ *rpq->pxy, rpq->aexp) ^
788*eda14cbcSMatt Macy 		    vdev_raidz_exp2(*rpq->q ^ *rpq->qxy, rpq->bexp);
789*eda14cbcSMatt Macy 		*yd = *rpq->p ^ *rpq->pxy ^ *xd;
790*eda14cbcSMatt Macy 	}
791*eda14cbcSMatt Macy 
792*eda14cbcSMatt Macy 	return (0);
793*eda14cbcSMatt Macy }
794*eda14cbcSMatt Macy 
795*eda14cbcSMatt Macy static int
796*eda14cbcSMatt Macy vdev_raidz_reconst_pq_tail_func(void *xbuf, size_t size, void *private)
797*eda14cbcSMatt Macy {
798*eda14cbcSMatt Macy 	struct reconst_pq_struct *rpq = private;
799*eda14cbcSMatt Macy 	uint8_t *xd = xbuf;
800*eda14cbcSMatt Macy 
801*eda14cbcSMatt Macy 	for (int i = 0; i < size;
802*eda14cbcSMatt Macy 	    i++, rpq->p++, rpq->q++, rpq->pxy++, rpq->qxy++, xd++) {
803*eda14cbcSMatt Macy 		/* same operation as vdev_raidz_reconst_pq_func() on xd */
804*eda14cbcSMatt Macy 		*xd = vdev_raidz_exp2(*rpq->p ^ *rpq->pxy, rpq->aexp) ^
805*eda14cbcSMatt Macy 		    vdev_raidz_exp2(*rpq->q ^ *rpq->qxy, rpq->bexp);
806*eda14cbcSMatt Macy 	}
807*eda14cbcSMatt Macy 
808*eda14cbcSMatt Macy 	return (0);
809*eda14cbcSMatt Macy }
810*eda14cbcSMatt Macy 
811*eda14cbcSMatt Macy static int
812*eda14cbcSMatt Macy vdev_raidz_reconstruct_p(raidz_map_t *rm, int *tgts, int ntgts)
813*eda14cbcSMatt Macy {
814*eda14cbcSMatt Macy 	int x = tgts[0];
815*eda14cbcSMatt Macy 	int c;
816*eda14cbcSMatt Macy 	abd_t *dst, *src;
817*eda14cbcSMatt Macy 
818*eda14cbcSMatt Macy 	ASSERT(ntgts == 1);
819*eda14cbcSMatt Macy 	ASSERT(x >= rm->rm_firstdatacol);
820*eda14cbcSMatt Macy 	ASSERT(x < rm->rm_cols);
821*eda14cbcSMatt Macy 
822*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[x].rc_size <= rm->rm_col[VDEV_RAIDZ_P].rc_size);
823*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[x].rc_size > 0);
824*eda14cbcSMatt Macy 
825*eda14cbcSMatt Macy 	src = rm->rm_col[VDEV_RAIDZ_P].rc_abd;
826*eda14cbcSMatt Macy 	dst = rm->rm_col[x].rc_abd;
827*eda14cbcSMatt Macy 
828*eda14cbcSMatt Macy 	abd_copy_from_buf(dst, abd_to_buf(src), rm->rm_col[x].rc_size);
829*eda14cbcSMatt Macy 
830*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
831*eda14cbcSMatt Macy 		uint64_t size = MIN(rm->rm_col[x].rc_size,
832*eda14cbcSMatt Macy 		    rm->rm_col[c].rc_size);
833*eda14cbcSMatt Macy 
834*eda14cbcSMatt Macy 		src = rm->rm_col[c].rc_abd;
835*eda14cbcSMatt Macy 		dst = rm->rm_col[x].rc_abd;
836*eda14cbcSMatt Macy 
837*eda14cbcSMatt Macy 		if (c == x)
838*eda14cbcSMatt Macy 			continue;
839*eda14cbcSMatt Macy 
840*eda14cbcSMatt Macy 		(void) abd_iterate_func2(dst, src, 0, 0, size,
841*eda14cbcSMatt Macy 		    vdev_raidz_reconst_p_func, NULL);
842*eda14cbcSMatt Macy 	}
843*eda14cbcSMatt Macy 
844*eda14cbcSMatt Macy 	return (1 << VDEV_RAIDZ_P);
845*eda14cbcSMatt Macy }
846*eda14cbcSMatt Macy 
847*eda14cbcSMatt Macy static int
848*eda14cbcSMatt Macy vdev_raidz_reconstruct_q(raidz_map_t *rm, int *tgts, int ntgts)
849*eda14cbcSMatt Macy {
850*eda14cbcSMatt Macy 	int x = tgts[0];
851*eda14cbcSMatt Macy 	int c, exp;
852*eda14cbcSMatt Macy 	abd_t *dst, *src;
853*eda14cbcSMatt Macy 
854*eda14cbcSMatt Macy 	ASSERT(ntgts == 1);
855*eda14cbcSMatt Macy 
856*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[x].rc_size <= rm->rm_col[VDEV_RAIDZ_Q].rc_size);
857*eda14cbcSMatt Macy 
858*eda14cbcSMatt Macy 	for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
859*eda14cbcSMatt Macy 		uint64_t size = (c == x) ? 0 : MIN(rm->rm_col[x].rc_size,
860*eda14cbcSMatt Macy 		    rm->rm_col[c].rc_size);
861*eda14cbcSMatt Macy 
862*eda14cbcSMatt Macy 		src = rm->rm_col[c].rc_abd;
863*eda14cbcSMatt Macy 		dst = rm->rm_col[x].rc_abd;
864*eda14cbcSMatt Macy 
865*eda14cbcSMatt Macy 		if (c == rm->rm_firstdatacol) {
866*eda14cbcSMatt Macy 			abd_copy(dst, src, size);
867*eda14cbcSMatt Macy 			if (rm->rm_col[x].rc_size > size)
868*eda14cbcSMatt Macy 				abd_zero_off(dst, size,
869*eda14cbcSMatt Macy 				    rm->rm_col[x].rc_size - size);
870*eda14cbcSMatt Macy 
871*eda14cbcSMatt Macy 		} else {
872*eda14cbcSMatt Macy 			ASSERT3U(size, <=, rm->rm_col[x].rc_size);
873*eda14cbcSMatt Macy 			(void) abd_iterate_func2(dst, src, 0, 0, size,
874*eda14cbcSMatt Macy 			    vdev_raidz_reconst_q_pre_func, NULL);
875*eda14cbcSMatt Macy 			(void) abd_iterate_func(dst,
876*eda14cbcSMatt Macy 			    size, rm->rm_col[x].rc_size - size,
877*eda14cbcSMatt Macy 			    vdev_raidz_reconst_q_pre_tail_func, NULL);
878*eda14cbcSMatt Macy 		}
879*eda14cbcSMatt Macy 	}
880*eda14cbcSMatt Macy 
881*eda14cbcSMatt Macy 	src = rm->rm_col[VDEV_RAIDZ_Q].rc_abd;
882*eda14cbcSMatt Macy 	dst = rm->rm_col[x].rc_abd;
883*eda14cbcSMatt Macy 	exp = 255 - (rm->rm_cols - 1 - x);
884*eda14cbcSMatt Macy 
885*eda14cbcSMatt Macy 	struct reconst_q_struct rq = { abd_to_buf(src), exp };
886*eda14cbcSMatt Macy 	(void) abd_iterate_func(dst, 0, rm->rm_col[x].rc_size,
887*eda14cbcSMatt Macy 	    vdev_raidz_reconst_q_post_func, &rq);
888*eda14cbcSMatt Macy 
889*eda14cbcSMatt Macy 	return (1 << VDEV_RAIDZ_Q);
890*eda14cbcSMatt Macy }
891*eda14cbcSMatt Macy 
892*eda14cbcSMatt Macy static int
893*eda14cbcSMatt Macy vdev_raidz_reconstruct_pq(raidz_map_t *rm, int *tgts, int ntgts)
894*eda14cbcSMatt Macy {
895*eda14cbcSMatt Macy 	uint8_t *p, *q, *pxy, *qxy, tmp, a, b, aexp, bexp;
896*eda14cbcSMatt Macy 	abd_t *pdata, *qdata;
897*eda14cbcSMatt Macy 	uint64_t xsize, ysize;
898*eda14cbcSMatt Macy 	int x = tgts[0];
899*eda14cbcSMatt Macy 	int y = tgts[1];
900*eda14cbcSMatt Macy 	abd_t *xd, *yd;
901*eda14cbcSMatt Macy 
902*eda14cbcSMatt Macy 	ASSERT(ntgts == 2);
903*eda14cbcSMatt Macy 	ASSERT(x < y);
904*eda14cbcSMatt Macy 	ASSERT(x >= rm->rm_firstdatacol);
905*eda14cbcSMatt Macy 	ASSERT(y < rm->rm_cols);
906*eda14cbcSMatt Macy 
907*eda14cbcSMatt Macy 	ASSERT(rm->rm_col[x].rc_size >= rm->rm_col[y].rc_size);
908*eda14cbcSMatt Macy 
909*eda14cbcSMatt Macy 	/*
910*eda14cbcSMatt Macy 	 * Move the parity data aside -- we're going to compute parity as
911*eda14cbcSMatt Macy 	 * though columns x and y were full of zeros -- Pxy and Qxy. We want to
912*eda14cbcSMatt Macy 	 * reuse the parity generation mechanism without trashing the actual
913*eda14cbcSMatt Macy 	 * parity so we make those columns appear to be full of zeros by
914*eda14cbcSMatt Macy 	 * setting their lengths to zero.
915*eda14cbcSMatt Macy 	 */
916*eda14cbcSMatt Macy 	pdata = rm->rm_col[VDEV_RAIDZ_P].rc_abd;
917*eda14cbcSMatt Macy 	qdata = rm->rm_col[VDEV_RAIDZ_Q].rc_abd;
918*eda14cbcSMatt Macy 	xsize = rm->rm_col[x].rc_size;
919*eda14cbcSMatt Macy 	ysize = rm->rm_col[y].rc_size;
920*eda14cbcSMatt Macy 
921*eda14cbcSMatt Macy 	rm->rm_col[VDEV_RAIDZ_P].rc_abd =
922*eda14cbcSMatt Macy 	    abd_alloc_linear(rm->rm_col[VDEV_RAIDZ_P].rc_size, B_TRUE);
923*eda14cbcSMatt Macy 	rm->rm_col[VDEV_RAIDZ_Q].rc_abd =
924*eda14cbcSMatt Macy 	    abd_alloc_linear(rm->rm_col[VDEV_RAIDZ_Q].rc_size, B_TRUE);
925*eda14cbcSMatt Macy 	rm->rm_col[x].rc_size = 0;
926*eda14cbcSMatt Macy 	rm->rm_col[y].rc_size = 0;
927*eda14cbcSMatt Macy 
928*eda14cbcSMatt Macy 	vdev_raidz_generate_parity_pq(rm);
929*eda14cbcSMatt Macy 
930*eda14cbcSMatt Macy 	rm->rm_col[x].rc_size = xsize;
931*eda14cbcSMatt Macy 	rm->rm_col[y].rc_size = ysize;
932*eda14cbcSMatt Macy 
933*eda14cbcSMatt Macy 	p = abd_to_buf(pdata);
934*eda14cbcSMatt Macy 	q = abd_to_buf(qdata);
935*eda14cbcSMatt Macy 	pxy = abd_to_buf(rm->rm_col[VDEV_RAIDZ_P].rc_abd);
936*eda14cbcSMatt Macy 	qxy = abd_to_buf(rm->rm_col[VDEV_RAIDZ_Q].rc_abd);
937*eda14cbcSMatt Macy 	xd = rm->rm_col[x].rc_abd;
938*eda14cbcSMatt Macy 	yd = rm->rm_col[y].rc_abd;
939*eda14cbcSMatt Macy 
940*eda14cbcSMatt Macy 	/*
941*eda14cbcSMatt Macy 	 * We now have:
942*eda14cbcSMatt Macy 	 *	Pxy = P + D_x + D_y
943*eda14cbcSMatt Macy 	 *	Qxy = Q + 2^(ndevs - 1 - x) * D_x + 2^(ndevs - 1 - y) * D_y
944*eda14cbcSMatt Macy 	 *
945*eda14cbcSMatt Macy 	 * We can then solve for D_x:
946*eda14cbcSMatt Macy 	 *	D_x = A * (P + Pxy) + B * (Q + Qxy)
947*eda14cbcSMatt Macy 	 * where
948*eda14cbcSMatt Macy 	 *	A = 2^(x - y) * (2^(x - y) + 1)^-1
949*eda14cbcSMatt Macy 	 *	B = 2^(ndevs - 1 - x) * (2^(x - y) + 1)^-1
950*eda14cbcSMatt Macy 	 *
951*eda14cbcSMatt Macy 	 * With D_x in hand, we can easily solve for D_y:
952*eda14cbcSMatt Macy 	 *	D_y = P + Pxy + D_x
953*eda14cbcSMatt Macy 	 */
954*eda14cbcSMatt Macy 
955*eda14cbcSMatt Macy 	a = vdev_raidz_pow2[255 + x - y];
956*eda14cbcSMatt Macy 	b = vdev_raidz_pow2[255 - (rm->rm_cols - 1 - x)];
957*eda14cbcSMatt Macy 	tmp = 255 - vdev_raidz_log2[a ^ 1];
958*eda14cbcSMatt Macy 
959*eda14cbcSMatt Macy 	aexp = vdev_raidz_log2[vdev_raidz_exp2(a, tmp)];
960*eda14cbcSMatt Macy 	bexp = vdev_raidz_log2[vdev_raidz_exp2(b, tmp)];
961*eda14cbcSMatt Macy 
962*eda14cbcSMatt Macy 	ASSERT3U(xsize, >=, ysize);
963*eda14cbcSMatt Macy 	struct reconst_pq_struct rpq = { p, q, pxy, qxy, aexp, bexp };
964*eda14cbcSMatt Macy 
965*eda14cbcSMatt Macy 	(void) abd_iterate_func2(xd, yd, 0, 0, ysize,
966*eda14cbcSMatt Macy 	    vdev_raidz_reconst_pq_func, &rpq);
967*eda14cbcSMatt Macy 	(void) abd_iterate_func(xd, ysize, xsize - ysize,
968*eda14cbcSMatt Macy 	    vdev_raidz_reconst_pq_tail_func, &rpq);
969*eda14cbcSMatt Macy 
970*eda14cbcSMatt Macy 	abd_free(rm->rm_col[VDEV_RAIDZ_P].rc_abd);
971*eda14cbcSMatt Macy 	abd_free(rm->rm_col[VDEV_RAIDZ_Q].rc_abd);
972*eda14cbcSMatt Macy 
973*eda14cbcSMatt Macy 	/*
974*eda14cbcSMatt Macy 	 * Restore the saved parity data.
975*eda14cbcSMatt Macy 	 */
976*eda14cbcSMatt Macy 	rm->rm_col[VDEV_RAIDZ_P].rc_abd = pdata;
977*eda14cbcSMatt Macy 	rm->rm_col[VDEV_RAIDZ_Q].rc_abd = qdata;
978*eda14cbcSMatt Macy 
979*eda14cbcSMatt Macy 	return ((1 << VDEV_RAIDZ_P) | (1 << VDEV_RAIDZ_Q));
980*eda14cbcSMatt Macy }
981*eda14cbcSMatt Macy 
982*eda14cbcSMatt Macy /* BEGIN CSTYLED */
983*eda14cbcSMatt Macy /*
984*eda14cbcSMatt Macy  * In the general case of reconstruction, we must solve the system of linear
985*eda14cbcSMatt Macy  * equations defined by the coefficients used to generate parity as well as
986*eda14cbcSMatt Macy  * the contents of the data and parity disks. This can be expressed with
987*eda14cbcSMatt Macy  * vectors for the original data (D) and the actual data (d) and parity (p)
988*eda14cbcSMatt Macy  * and a matrix composed of the identity matrix (I) and a dispersal matrix (V):
989*eda14cbcSMatt Macy  *
990*eda14cbcSMatt Macy  *            __   __                     __     __
991*eda14cbcSMatt Macy  *            |     |         __     __   |  p_0  |
992*eda14cbcSMatt Macy  *            |  V  |         |  D_0  |   | p_m-1 |
993*eda14cbcSMatt Macy  *            |     |    x    |   :   | = |  d_0  |
994*eda14cbcSMatt Macy  *            |  I  |         | D_n-1 |   |   :   |
995*eda14cbcSMatt Macy  *            |     |         ~~     ~~   | d_n-1 |
996*eda14cbcSMatt Macy  *            ~~   ~~                     ~~     ~~
997*eda14cbcSMatt Macy  *
998*eda14cbcSMatt Macy  * I is simply a square identity matrix of size n, and V is a vandermonde
999*eda14cbcSMatt Macy  * matrix defined by the coefficients we chose for the various parity columns
1000*eda14cbcSMatt Macy  * (1, 2, 4). Note that these values were chosen both for simplicity, speedy
1001*eda14cbcSMatt Macy  * computation as well as linear separability.
1002*eda14cbcSMatt Macy  *
1003*eda14cbcSMatt Macy  *      __               __               __     __
1004*eda14cbcSMatt Macy  *      |   1   ..  1 1 1 |               |  p_0  |
1005*eda14cbcSMatt Macy  *      | 2^n-1 ..  4 2 1 |   __     __   |   :   |
1006*eda14cbcSMatt Macy  *      | 4^n-1 .. 16 4 1 |   |  D_0  |   | p_m-1 |
1007*eda14cbcSMatt Macy  *      |   1   ..  0 0 0 |   |  D_1  |   |  d_0  |
1008*eda14cbcSMatt Macy  *      |   0   ..  0 0 0 | x |  D_2  | = |  d_1  |
1009*eda14cbcSMatt Macy  *      |   :       : : : |   |   :   |   |  d_2  |
1010*eda14cbcSMatt Macy  *      |   0   ..  1 0 0 |   | D_n-1 |   |   :   |
1011*eda14cbcSMatt Macy  *      |   0   ..  0 1 0 |   ~~     ~~   |   :   |
1012*eda14cbcSMatt Macy  *      |   0   ..  0 0 1 |               | d_n-1 |
1013*eda14cbcSMatt Macy  *      ~~               ~~               ~~     ~~
1014*eda14cbcSMatt Macy  *
1015*eda14cbcSMatt Macy  * Note that I, V, d, and p are known. To compute D, we must invert the
1016*eda14cbcSMatt Macy  * matrix and use the known data and parity values to reconstruct the unknown
1017*eda14cbcSMatt Macy  * data values. We begin by removing the rows in V|I and d|p that correspond
1018*eda14cbcSMatt Macy  * to failed or missing columns; we then make V|I square (n x n) and d|p
1019*eda14cbcSMatt Macy  * sized n by removing rows corresponding to unused parity from the bottom up
1020*eda14cbcSMatt Macy  * to generate (V|I)' and (d|p)'. We can then generate the inverse of (V|I)'
1021*eda14cbcSMatt Macy  * using Gauss-Jordan elimination. In the example below we use m=3 parity
1022*eda14cbcSMatt Macy  * columns, n=8 data columns, with errors in d_1, d_2, and p_1:
1023*eda14cbcSMatt Macy  *           __                               __
1024*eda14cbcSMatt Macy  *           |  1   1   1   1   1   1   1   1  |
1025*eda14cbcSMatt Macy  *           | 128  64  32  16  8   4   2   1  | <-----+-+-- missing disks
1026*eda14cbcSMatt Macy  *           |  19 205 116  29  64  16  4   1  |      / /
1027*eda14cbcSMatt Macy  *           |  1   0   0   0   0   0   0   0  |     / /
1028*eda14cbcSMatt Macy  *           |  0   1   0   0   0   0   0   0  | <--' /
1029*eda14cbcSMatt Macy  *  (V|I)  = |  0   0   1   0   0   0   0   0  | <---'
1030*eda14cbcSMatt Macy  *           |  0   0   0   1   0   0   0   0  |
1031*eda14cbcSMatt Macy  *           |  0   0   0   0   1   0   0   0  |
1032*eda14cbcSMatt Macy  *           |  0   0   0   0   0   1   0   0  |
1033*eda14cbcSMatt Macy  *           |  0   0   0   0   0   0   1   0  |
1034*eda14cbcSMatt Macy  *           |  0   0   0   0   0   0   0   1  |
1035*eda14cbcSMatt Macy  *           ~~                               ~~
1036*eda14cbcSMatt Macy  *           __                               __
1037*eda14cbcSMatt Macy  *           |  1   1   1   1   1   1   1   1  |
1038*eda14cbcSMatt Macy  *           | 128  64  32  16  8   4   2   1  |
1039*eda14cbcSMatt Macy  *           |  19 205 116  29  64  16  4   1  |
1040*eda14cbcSMatt Macy  *           |  1   0   0   0   0   0   0   0  |
1041*eda14cbcSMatt Macy  *           |  0   1   0   0   0   0   0   0  |
1042*eda14cbcSMatt Macy  *  (V|I)' = |  0   0   1   0   0   0   0   0  |
1043*eda14cbcSMatt Macy  *           |  0   0   0   1   0   0   0   0  |
1044*eda14cbcSMatt Macy  *           |  0   0   0   0   1   0   0   0  |
1045*eda14cbcSMatt Macy  *           |  0   0   0   0   0   1   0   0  |
1046*eda14cbcSMatt Macy  *           |  0   0   0   0   0   0   1   0  |
1047*eda14cbcSMatt Macy  *           |  0   0   0   0   0   0   0   1  |
1048*eda14cbcSMatt Macy  *           ~~                               ~~
1049*eda14cbcSMatt Macy  *
1050*eda14cbcSMatt Macy  * Here we employ Gauss-Jordan elimination to find the inverse of (V|I)'. We
1051*eda14cbcSMatt Macy  * have carefully chosen the seed values 1, 2, and 4 to ensure that this
1052*eda14cbcSMatt Macy  * matrix is not singular.
1053*eda14cbcSMatt Macy  * __                                                                 __
1054*eda14cbcSMatt Macy  * |  1   1   1   1   1   1   1   1     1   0   0   0   0   0   0   0  |
1055*eda14cbcSMatt Macy  * |  19 205 116  29  64  16  4   1     0   1   0   0   0   0   0   0  |
1056*eda14cbcSMatt Macy  * |  1   0   0   0   0   0   0   0     0   0   1   0   0   0   0   0  |
1057*eda14cbcSMatt Macy  * |  0   0   0   1   0   0   0   0     0   0   0   1   0   0   0   0  |
1058*eda14cbcSMatt Macy  * |  0   0   0   0   1   0   0   0     0   0   0   0   1   0   0   0  |
1059*eda14cbcSMatt Macy  * |  0   0   0   0   0   1   0   0     0   0   0   0   0   1   0   0  |
1060*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   1   0     0   0   0   0   0   0   1   0  |
1061*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   0   1     0   0   0   0   0   0   0   1  |
1062*eda14cbcSMatt Macy  * ~~                                                                 ~~
1063*eda14cbcSMatt Macy  * __                                                                 __
1064*eda14cbcSMatt Macy  * |  1   0   0   0   0   0   0   0     0   0   1   0   0   0   0   0  |
1065*eda14cbcSMatt Macy  * |  1   1   1   1   1   1   1   1     1   0   0   0   0   0   0   0  |
1066*eda14cbcSMatt Macy  * |  19 205 116  29  64  16  4   1     0   1   0   0   0   0   0   0  |
1067*eda14cbcSMatt Macy  * |  0   0   0   1   0   0   0   0     0   0   0   1   0   0   0   0  |
1068*eda14cbcSMatt Macy  * |  0   0   0   0   1   0   0   0     0   0   0   0   1   0   0   0  |
1069*eda14cbcSMatt Macy  * |  0   0   0   0   0   1   0   0     0   0   0   0   0   1   0   0  |
1070*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   1   0     0   0   0   0   0   0   1   0  |
1071*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   0   1     0   0   0   0   0   0   0   1  |
1072*eda14cbcSMatt Macy  * ~~                                                                 ~~
1073*eda14cbcSMatt Macy  * __                                                                 __
1074*eda14cbcSMatt Macy  * |  1   0   0   0   0   0   0   0     0   0   1   0   0   0   0   0  |
1075*eda14cbcSMatt Macy  * |  0   1   1   0   0   0   0   0     1   0   1   1   1   1   1   1  |
1076*eda14cbcSMatt Macy  * |  0  205 116  0   0   0   0   0     0   1   19  29  64  16  4   1  |
1077*eda14cbcSMatt Macy  * |  0   0   0   1   0   0   0   0     0   0   0   1   0   0   0   0  |
1078*eda14cbcSMatt Macy  * |  0   0   0   0   1   0   0   0     0   0   0   0   1   0   0   0  |
1079*eda14cbcSMatt Macy  * |  0   0   0   0   0   1   0   0     0   0   0   0   0   1   0   0  |
1080*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   1   0     0   0   0   0   0   0   1   0  |
1081*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   0   1     0   0   0   0   0   0   0   1  |
1082*eda14cbcSMatt Macy  * ~~                                                                 ~~
1083*eda14cbcSMatt Macy  * __                                                                 __
1084*eda14cbcSMatt Macy  * |  1   0   0   0   0   0   0   0     0   0   1   0   0   0   0   0  |
1085*eda14cbcSMatt Macy  * |  0   1   1   0   0   0   0   0     1   0   1   1   1   1   1   1  |
1086*eda14cbcSMatt Macy  * |  0   0  185  0   0   0   0   0    205  1  222 208 141 221 201 204 |
1087*eda14cbcSMatt Macy  * |  0   0   0   1   0   0   0   0     0   0   0   1   0   0   0   0  |
1088*eda14cbcSMatt Macy  * |  0   0   0   0   1   0   0   0     0   0   0   0   1   0   0   0  |
1089*eda14cbcSMatt Macy  * |  0   0   0   0   0   1   0   0     0   0   0   0   0   1   0   0  |
1090*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   1   0     0   0   0   0   0   0   1   0  |
1091*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   0   1     0   0   0   0   0   0   0   1  |
1092*eda14cbcSMatt Macy  * ~~                                                                 ~~
1093*eda14cbcSMatt Macy  * __                                                                 __
1094*eda14cbcSMatt Macy  * |  1   0   0   0   0   0   0   0     0   0   1   0   0   0   0   0  |
1095*eda14cbcSMatt Macy  * |  0   1   1   0   0   0   0   0     1   0   1   1   1   1   1   1  |
1096*eda14cbcSMatt Macy  * |  0   0   1   0   0   0   0   0    166 100  4   40 158 168 216 209 |
1097*eda14cbcSMatt Macy  * |  0   0   0   1   0   0   0   0     0   0   0   1   0   0   0   0  |
1098*eda14cbcSMatt Macy  * |  0   0   0   0   1   0   0   0     0   0   0   0   1   0   0   0  |
1099*eda14cbcSMatt Macy  * |  0   0   0   0   0   1   0   0     0   0   0   0   0   1   0   0  |
1100*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   1   0     0   0   0   0   0   0   1   0  |
1101*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   0   1     0   0   0   0   0   0   0   1  |
1102*eda14cbcSMatt Macy  * ~~                                                                 ~~
1103*eda14cbcSMatt Macy  * __                                                                 __
1104*eda14cbcSMatt Macy  * |  1   0   0   0   0   0   0   0     0   0   1   0   0   0   0   0  |
1105*eda14cbcSMatt Macy  * |  0   1   0   0   0   0   0   0    167 100  5   41 159 169 217 208 |
1106*eda14cbcSMatt Macy  * |  0   0   1   0   0   0   0   0    166 100  4   40 158 168 216 209 |
1107*eda14cbcSMatt Macy  * |  0   0   0   1   0   0   0   0     0   0   0   1   0   0   0   0  |
1108*eda14cbcSMatt Macy  * |  0   0   0   0   1   0   0   0     0   0   0   0   1   0   0   0  |
1109*eda14cbcSMatt Macy  * |  0   0   0   0   0   1   0   0     0   0   0   0   0   1   0   0  |
1110*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   1   0     0   0   0   0   0   0   1   0  |
1111*eda14cbcSMatt Macy  * |  0   0   0   0   0   0   0   1     0   0   0   0   0   0   0   1  |
1112*eda14cbcSMatt Macy  * ~~                                                                 ~~
1113*eda14cbcSMatt Macy  *                   __                               __
1114*eda14cbcSMatt Macy  *                   |  0   0   1   0   0   0   0   0  |
1115*eda14cbcSMatt Macy  *                   | 167 100  5   41 159 169 217 208 |
1116*eda14cbcSMatt Macy  *                   | 166 100  4   40 158 168 216 209 |
1117*eda14cbcSMatt Macy  *       (V|I)'^-1 = |  0   0   0   1   0   0   0   0  |
1118*eda14cbcSMatt Macy  *                   |  0   0   0   0   1   0   0   0  |
1119*eda14cbcSMatt Macy  *                   |  0   0   0   0   0   1   0   0  |
1120*eda14cbcSMatt Macy  *                   |  0   0   0   0   0   0   1   0  |
1121*eda14cbcSMatt Macy  *                   |  0   0   0   0   0   0   0   1  |
1122*eda14cbcSMatt Macy  *                   ~~                               ~~
1123*eda14cbcSMatt Macy  *
1124*eda14cbcSMatt Macy  * We can then simply compute D = (V|I)'^-1 x (d|p)' to discover the values
1125*eda14cbcSMatt Macy  * of the missing data.
1126*eda14cbcSMatt Macy  *
1127*eda14cbcSMatt Macy  * As is apparent from the example above, the only non-trivial rows in the
1128*eda14cbcSMatt Macy  * inverse matrix correspond to the data disks that we're trying to
1129*eda14cbcSMatt Macy  * reconstruct. Indeed, those are the only rows we need as the others would
1130*eda14cbcSMatt Macy  * only be useful for reconstructing data known or assumed to be valid. For
1131*eda14cbcSMatt Macy  * that reason, we only build the coefficients in the rows that correspond to
1132*eda14cbcSMatt Macy  * targeted columns.
1133*eda14cbcSMatt Macy  */
1134*eda14cbcSMatt Macy /* END CSTYLED */
1135*eda14cbcSMatt Macy 
1136*eda14cbcSMatt Macy static void
1137*eda14cbcSMatt Macy vdev_raidz_matrix_init(raidz_map_t *rm, int n, int nmap, int *map,
1138*eda14cbcSMatt Macy     uint8_t **rows)
1139*eda14cbcSMatt Macy {
1140*eda14cbcSMatt Macy 	int i, j;
1141*eda14cbcSMatt Macy 	int pow;
1142*eda14cbcSMatt Macy 
1143*eda14cbcSMatt Macy 	ASSERT(n == rm->rm_cols - rm->rm_firstdatacol);
1144*eda14cbcSMatt Macy 
1145*eda14cbcSMatt Macy 	/*
1146*eda14cbcSMatt Macy 	 * Fill in the missing rows of interest.
1147*eda14cbcSMatt Macy 	 */
1148*eda14cbcSMatt Macy 	for (i = 0; i < nmap; i++) {
1149*eda14cbcSMatt Macy 		ASSERT3S(0, <=, map[i]);
1150*eda14cbcSMatt Macy 		ASSERT3S(map[i], <=, 2);
1151*eda14cbcSMatt Macy 
1152*eda14cbcSMatt Macy 		pow = map[i] * n;
1153*eda14cbcSMatt Macy 		if (pow > 255)
1154*eda14cbcSMatt Macy 			pow -= 255;
1155*eda14cbcSMatt Macy 		ASSERT(pow <= 255);
1156*eda14cbcSMatt Macy 
1157*eda14cbcSMatt Macy 		for (j = 0; j < n; j++) {
1158*eda14cbcSMatt Macy 			pow -= map[i];
1159*eda14cbcSMatt Macy 			if (pow < 0)
1160*eda14cbcSMatt Macy 				pow += 255;
1161*eda14cbcSMatt Macy 			rows[i][j] = vdev_raidz_pow2[pow];
1162*eda14cbcSMatt Macy 		}
1163*eda14cbcSMatt Macy 	}
1164*eda14cbcSMatt Macy }
1165*eda14cbcSMatt Macy 
1166*eda14cbcSMatt Macy static void
1167*eda14cbcSMatt Macy vdev_raidz_matrix_invert(raidz_map_t *rm, int n, int nmissing, int *missing,
1168*eda14cbcSMatt Macy     uint8_t **rows, uint8_t **invrows, const uint8_t *used)
1169*eda14cbcSMatt Macy {
1170*eda14cbcSMatt Macy 	int i, j, ii, jj;
1171*eda14cbcSMatt Macy 	uint8_t log;
1172*eda14cbcSMatt Macy 
1173*eda14cbcSMatt Macy 	/*
1174*eda14cbcSMatt Macy 	 * Assert that the first nmissing entries from the array of used
1175*eda14cbcSMatt Macy 	 * columns correspond to parity columns and that subsequent entries
1176*eda14cbcSMatt Macy 	 * correspond to data columns.
1177*eda14cbcSMatt Macy 	 */
1178*eda14cbcSMatt Macy 	for (i = 0; i < nmissing; i++) {
1179*eda14cbcSMatt Macy 		ASSERT3S(used[i], <, rm->rm_firstdatacol);
1180*eda14cbcSMatt Macy 	}
1181*eda14cbcSMatt Macy 	for (; i < n; i++) {
1182*eda14cbcSMatt Macy 		ASSERT3S(used[i], >=, rm->rm_firstdatacol);
1183*eda14cbcSMatt Macy 	}
1184*eda14cbcSMatt Macy 
1185*eda14cbcSMatt Macy 	/*
1186*eda14cbcSMatt Macy 	 * First initialize the storage where we'll compute the inverse rows.
1187*eda14cbcSMatt Macy 	 */
1188*eda14cbcSMatt Macy 	for (i = 0; i < nmissing; i++) {
1189*eda14cbcSMatt Macy 		for (j = 0; j < n; j++) {
1190*eda14cbcSMatt Macy 			invrows[i][j] = (i == j) ? 1 : 0;
1191*eda14cbcSMatt Macy 		}
1192*eda14cbcSMatt Macy 	}
1193*eda14cbcSMatt Macy 
1194*eda14cbcSMatt Macy 	/*
1195*eda14cbcSMatt Macy 	 * Subtract all trivial rows from the rows of consequence.
1196*eda14cbcSMatt Macy 	 */
1197*eda14cbcSMatt Macy 	for (i = 0; i < nmissing; i++) {
1198*eda14cbcSMatt Macy 		for (j = nmissing; j < n; j++) {
1199*eda14cbcSMatt Macy 			ASSERT3U(used[j], >=, rm->rm_firstdatacol);
1200*eda14cbcSMatt Macy 			jj = used[j] - rm->rm_firstdatacol;
1201*eda14cbcSMatt Macy 			ASSERT3S(jj, <, n);
1202*eda14cbcSMatt Macy 			invrows[i][j] = rows[i][jj];
1203*eda14cbcSMatt Macy 			rows[i][jj] = 0;
1204*eda14cbcSMatt Macy 		}
1205*eda14cbcSMatt Macy 	}
1206*eda14cbcSMatt Macy 
1207*eda14cbcSMatt Macy 	/*
1208*eda14cbcSMatt Macy 	 * For each of the rows of interest, we must normalize it and subtract
1209*eda14cbcSMatt Macy 	 * a multiple of it from the other rows.
1210*eda14cbcSMatt Macy 	 */
1211*eda14cbcSMatt Macy 	for (i = 0; i < nmissing; i++) {
1212*eda14cbcSMatt Macy 		for (j = 0; j < missing[i]; j++) {
1213*eda14cbcSMatt Macy 			ASSERT0(rows[i][j]);
1214*eda14cbcSMatt Macy 		}
1215*eda14cbcSMatt Macy 		ASSERT3U(rows[i][missing[i]], !=, 0);
1216*eda14cbcSMatt Macy 
1217*eda14cbcSMatt Macy 		/*
1218*eda14cbcSMatt Macy 		 * Compute the inverse of the first element and multiply each
1219*eda14cbcSMatt Macy 		 * element in the row by that value.
1220*eda14cbcSMatt Macy 		 */
1221*eda14cbcSMatt Macy 		log = 255 - vdev_raidz_log2[rows[i][missing[i]]];
1222*eda14cbcSMatt Macy 
1223*eda14cbcSMatt Macy 		for (j = 0; j < n; j++) {
1224*eda14cbcSMatt Macy 			rows[i][j] = vdev_raidz_exp2(rows[i][j], log);
1225*eda14cbcSMatt Macy 			invrows[i][j] = vdev_raidz_exp2(invrows[i][j], log);
1226*eda14cbcSMatt Macy 		}
1227*eda14cbcSMatt Macy 
1228*eda14cbcSMatt Macy 		for (ii = 0; ii < nmissing; ii++) {
1229*eda14cbcSMatt Macy 			if (i == ii)
1230*eda14cbcSMatt Macy 				continue;
1231*eda14cbcSMatt Macy 
1232*eda14cbcSMatt Macy 			ASSERT3U(rows[ii][missing[i]], !=, 0);
1233*eda14cbcSMatt Macy 
1234*eda14cbcSMatt Macy 			log = vdev_raidz_log2[rows[ii][missing[i]]];
1235*eda14cbcSMatt Macy 
1236*eda14cbcSMatt Macy 			for (j = 0; j < n; j++) {
1237*eda14cbcSMatt Macy 				rows[ii][j] ^=
1238*eda14cbcSMatt Macy 				    vdev_raidz_exp2(rows[i][j], log);
1239*eda14cbcSMatt Macy 				invrows[ii][j] ^=
1240*eda14cbcSMatt Macy 				    vdev_raidz_exp2(invrows[i][j], log);
1241*eda14cbcSMatt Macy 			}
1242*eda14cbcSMatt Macy 		}
1243*eda14cbcSMatt Macy 	}
1244*eda14cbcSMatt Macy 
1245*eda14cbcSMatt Macy 	/*
1246*eda14cbcSMatt Macy 	 * Verify that the data that is left in the rows are properly part of
1247*eda14cbcSMatt Macy 	 * an identity matrix.
1248*eda14cbcSMatt Macy 	 */
1249*eda14cbcSMatt Macy 	for (i = 0; i < nmissing; i++) {
1250*eda14cbcSMatt Macy 		for (j = 0; j < n; j++) {
1251*eda14cbcSMatt Macy 			if (j == missing[i]) {
1252*eda14cbcSMatt Macy 				ASSERT3U(rows[i][j], ==, 1);
1253*eda14cbcSMatt Macy 			} else {
1254*eda14cbcSMatt Macy 				ASSERT0(rows[i][j]);
1255*eda14cbcSMatt Macy 			}
1256*eda14cbcSMatt Macy 		}
1257*eda14cbcSMatt Macy 	}
1258*eda14cbcSMatt Macy }
1259*eda14cbcSMatt Macy 
1260*eda14cbcSMatt Macy static void
1261*eda14cbcSMatt Macy vdev_raidz_matrix_reconstruct(raidz_map_t *rm, int n, int nmissing,
1262*eda14cbcSMatt Macy     int *missing, uint8_t **invrows, const uint8_t *used)
1263*eda14cbcSMatt Macy {
1264*eda14cbcSMatt Macy 	int i, j, x, cc, c;
1265*eda14cbcSMatt Macy 	uint8_t *src;
1266*eda14cbcSMatt Macy 	uint64_t ccount;
1267*eda14cbcSMatt Macy 	uint8_t *dst[VDEV_RAIDZ_MAXPARITY] = { NULL };
1268*eda14cbcSMatt Macy 	uint64_t dcount[VDEV_RAIDZ_MAXPARITY] = { 0 };
1269*eda14cbcSMatt Macy 	uint8_t log = 0;
1270*eda14cbcSMatt Macy 	uint8_t val;
1271*eda14cbcSMatt Macy 	int ll;
1272*eda14cbcSMatt Macy 	uint8_t *invlog[VDEV_RAIDZ_MAXPARITY];
1273*eda14cbcSMatt Macy 	uint8_t *p, *pp;
1274*eda14cbcSMatt Macy 	size_t psize;
1275*eda14cbcSMatt Macy 
1276*eda14cbcSMatt Macy 	psize = sizeof (invlog[0][0]) * n * nmissing;
1277*eda14cbcSMatt Macy 	p = kmem_alloc(psize, KM_SLEEP);
1278*eda14cbcSMatt Macy 
1279*eda14cbcSMatt Macy 	for (pp = p, i = 0; i < nmissing; i++) {
1280*eda14cbcSMatt Macy 		invlog[i] = pp;
1281*eda14cbcSMatt Macy 		pp += n;
1282*eda14cbcSMatt Macy 	}
1283*eda14cbcSMatt Macy 
1284*eda14cbcSMatt Macy 	for (i = 0; i < nmissing; i++) {
1285*eda14cbcSMatt Macy 		for (j = 0; j < n; j++) {
1286*eda14cbcSMatt Macy 			ASSERT3U(invrows[i][j], !=, 0);
1287*eda14cbcSMatt Macy 			invlog[i][j] = vdev_raidz_log2[invrows[i][j]];
1288*eda14cbcSMatt Macy 		}
1289*eda14cbcSMatt Macy 	}
1290*eda14cbcSMatt Macy 
1291*eda14cbcSMatt Macy 	for (i = 0; i < n; i++) {
1292*eda14cbcSMatt Macy 		c = used[i];
1293*eda14cbcSMatt Macy 		ASSERT3U(c, <, rm->rm_cols);
1294*eda14cbcSMatt Macy 
1295*eda14cbcSMatt Macy 		src = abd_to_buf(rm->rm_col[c].rc_abd);
1296*eda14cbcSMatt Macy 		ccount = rm->rm_col[c].rc_size;
1297*eda14cbcSMatt Macy 		for (j = 0; j < nmissing; j++) {
1298*eda14cbcSMatt Macy 			cc = missing[j] + rm->rm_firstdatacol;
1299*eda14cbcSMatt Macy 			ASSERT3U(cc, >=, rm->rm_firstdatacol);
1300*eda14cbcSMatt Macy 			ASSERT3U(cc, <, rm->rm_cols);
1301*eda14cbcSMatt Macy 			ASSERT3U(cc, !=, c);
1302*eda14cbcSMatt Macy 
1303*eda14cbcSMatt Macy 			dst[j] = abd_to_buf(rm->rm_col[cc].rc_abd);
1304*eda14cbcSMatt Macy 			dcount[j] = rm->rm_col[cc].rc_size;
1305*eda14cbcSMatt Macy 		}
1306*eda14cbcSMatt Macy 
1307*eda14cbcSMatt Macy 		ASSERT(ccount >= rm->rm_col[missing[0]].rc_size || i > 0);
1308*eda14cbcSMatt Macy 
1309*eda14cbcSMatt Macy 		for (x = 0; x < ccount; x++, src++) {
1310*eda14cbcSMatt Macy 			if (*src != 0)
1311*eda14cbcSMatt Macy 				log = vdev_raidz_log2[*src];
1312*eda14cbcSMatt Macy 
1313*eda14cbcSMatt Macy 			for (cc = 0; cc < nmissing; cc++) {
1314*eda14cbcSMatt Macy 				if (x >= dcount[cc])
1315*eda14cbcSMatt Macy 					continue;
1316*eda14cbcSMatt Macy 
1317*eda14cbcSMatt Macy 				if (*src == 0) {
1318*eda14cbcSMatt Macy 					val = 0;
1319*eda14cbcSMatt Macy 				} else {
1320*eda14cbcSMatt Macy 					if ((ll = log + invlog[cc][i]) >= 255)
1321*eda14cbcSMatt Macy 						ll -= 255;
1322*eda14cbcSMatt Macy 					val = vdev_raidz_pow2[ll];
1323*eda14cbcSMatt Macy 				}
1324*eda14cbcSMatt Macy 
1325*eda14cbcSMatt Macy 				if (i == 0)
1326*eda14cbcSMatt Macy 					dst[cc][x] = val;
1327*eda14cbcSMatt Macy 				else
1328*eda14cbcSMatt Macy 					dst[cc][x] ^= val;
1329*eda14cbcSMatt Macy 			}
1330*eda14cbcSMatt Macy 		}
1331*eda14cbcSMatt Macy 	}
1332*eda14cbcSMatt Macy 
1333*eda14cbcSMatt Macy 	kmem_free(p, psize);
1334*eda14cbcSMatt Macy }
1335*eda14cbcSMatt Macy 
1336*eda14cbcSMatt Macy static int
1337*eda14cbcSMatt Macy vdev_raidz_reconstruct_general(raidz_map_t *rm, int *tgts, int ntgts)
1338*eda14cbcSMatt Macy {
1339*eda14cbcSMatt Macy 	int n, i, c, t, tt;
1340*eda14cbcSMatt Macy 	int nmissing_rows;
1341*eda14cbcSMatt Macy 	int missing_rows[VDEV_RAIDZ_MAXPARITY];
1342*eda14cbcSMatt Macy 	int parity_map[VDEV_RAIDZ_MAXPARITY];
1343*eda14cbcSMatt Macy 
1344*eda14cbcSMatt Macy 	uint8_t *p, *pp;
1345*eda14cbcSMatt Macy 	size_t psize;
1346*eda14cbcSMatt Macy 
1347*eda14cbcSMatt Macy 	uint8_t *rows[VDEV_RAIDZ_MAXPARITY];
1348*eda14cbcSMatt Macy 	uint8_t *invrows[VDEV_RAIDZ_MAXPARITY];
1349*eda14cbcSMatt Macy 	uint8_t *used;
1350*eda14cbcSMatt Macy 
1351*eda14cbcSMatt Macy 	abd_t **bufs = NULL;
1352*eda14cbcSMatt Macy 
1353*eda14cbcSMatt Macy 	int code = 0;
1354*eda14cbcSMatt Macy 
1355*eda14cbcSMatt Macy 	/*
1356*eda14cbcSMatt Macy 	 * Matrix reconstruction can't use scatter ABDs yet, so we allocate
1357*eda14cbcSMatt Macy 	 * temporary linear ABDs.
1358*eda14cbcSMatt Macy 	 */
1359*eda14cbcSMatt Macy 	if (!abd_is_linear(rm->rm_col[rm->rm_firstdatacol].rc_abd)) {
1360*eda14cbcSMatt Macy 		bufs = kmem_alloc(rm->rm_cols * sizeof (abd_t *), KM_PUSHPAGE);
1361*eda14cbcSMatt Macy 
1362*eda14cbcSMatt Macy 		for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
1363*eda14cbcSMatt Macy 			raidz_col_t *col = &rm->rm_col[c];
1364*eda14cbcSMatt Macy 
1365*eda14cbcSMatt Macy 			bufs[c] = col->rc_abd;
1366*eda14cbcSMatt Macy 			col->rc_abd = abd_alloc_linear(col->rc_size, B_TRUE);
1367*eda14cbcSMatt Macy 			abd_copy(col->rc_abd, bufs[c], col->rc_size);
1368*eda14cbcSMatt Macy 		}
1369*eda14cbcSMatt Macy 	}
1370*eda14cbcSMatt Macy 
1371*eda14cbcSMatt Macy 	n = rm->rm_cols - rm->rm_firstdatacol;
1372*eda14cbcSMatt Macy 
1373*eda14cbcSMatt Macy 	/*
1374*eda14cbcSMatt Macy 	 * Figure out which data columns are missing.
1375*eda14cbcSMatt Macy 	 */
1376*eda14cbcSMatt Macy 	nmissing_rows = 0;
1377*eda14cbcSMatt Macy 	for (t = 0; t < ntgts; t++) {
1378*eda14cbcSMatt Macy 		if (tgts[t] >= rm->rm_firstdatacol) {
1379*eda14cbcSMatt Macy 			missing_rows[nmissing_rows++] =
1380*eda14cbcSMatt Macy 			    tgts[t] - rm->rm_firstdatacol;
1381*eda14cbcSMatt Macy 		}
1382*eda14cbcSMatt Macy 	}
1383*eda14cbcSMatt Macy 
1384*eda14cbcSMatt Macy 	/*
1385*eda14cbcSMatt Macy 	 * Figure out which parity columns to use to help generate the missing
1386*eda14cbcSMatt Macy 	 * data columns.
1387*eda14cbcSMatt Macy 	 */
1388*eda14cbcSMatt Macy 	for (tt = 0, c = 0, i = 0; i < nmissing_rows; c++) {
1389*eda14cbcSMatt Macy 		ASSERT(tt < ntgts);
1390*eda14cbcSMatt Macy 		ASSERT(c < rm->rm_firstdatacol);
1391*eda14cbcSMatt Macy 
1392*eda14cbcSMatt Macy 		/*
1393*eda14cbcSMatt Macy 		 * Skip any targeted parity columns.
1394*eda14cbcSMatt Macy 		 */
1395*eda14cbcSMatt Macy 		if (c == tgts[tt]) {
1396*eda14cbcSMatt Macy 			tt++;
1397*eda14cbcSMatt Macy 			continue;
1398*eda14cbcSMatt Macy 		}
1399*eda14cbcSMatt Macy 
1400*eda14cbcSMatt Macy 		code |= 1 << c;
1401*eda14cbcSMatt Macy 
1402*eda14cbcSMatt Macy 		parity_map[i] = c;
1403*eda14cbcSMatt Macy 		i++;
1404*eda14cbcSMatt Macy 	}
1405*eda14cbcSMatt Macy 
1406*eda14cbcSMatt Macy 	ASSERT(code != 0);
1407*eda14cbcSMatt Macy 	ASSERT3U(code, <, 1 << VDEV_RAIDZ_MAXPARITY);
1408*eda14cbcSMatt Macy 
1409*eda14cbcSMatt Macy 	psize = (sizeof (rows[0][0]) + sizeof (invrows[0][0])) *
1410*eda14cbcSMatt Macy 	    nmissing_rows * n + sizeof (used[0]) * n;
1411*eda14cbcSMatt Macy 	p = kmem_alloc(psize, KM_SLEEP);
1412*eda14cbcSMatt Macy 
1413*eda14cbcSMatt Macy 	for (pp = p, i = 0; i < nmissing_rows; i++) {
1414*eda14cbcSMatt Macy 		rows[i] = pp;
1415*eda14cbcSMatt Macy 		pp += n;
1416*eda14cbcSMatt Macy 		invrows[i] = pp;
1417*eda14cbcSMatt Macy 		pp += n;
1418*eda14cbcSMatt Macy 	}
1419*eda14cbcSMatt Macy 	used = pp;
1420*eda14cbcSMatt Macy 
1421*eda14cbcSMatt Macy 	for (i = 0; i < nmissing_rows; i++) {
1422*eda14cbcSMatt Macy 		used[i] = parity_map[i];
1423*eda14cbcSMatt Macy 	}
1424*eda14cbcSMatt Macy 
1425*eda14cbcSMatt Macy 	for (tt = 0, c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
1426*eda14cbcSMatt Macy 		if (tt < nmissing_rows &&
1427*eda14cbcSMatt Macy 		    c == missing_rows[tt] + rm->rm_firstdatacol) {
1428*eda14cbcSMatt Macy 			tt++;
1429*eda14cbcSMatt Macy 			continue;
1430*eda14cbcSMatt Macy 		}
1431*eda14cbcSMatt Macy 
1432*eda14cbcSMatt Macy 		ASSERT3S(i, <, n);
1433*eda14cbcSMatt Macy 		used[i] = c;
1434*eda14cbcSMatt Macy 		i++;
1435*eda14cbcSMatt Macy 	}
1436*eda14cbcSMatt Macy 
1437*eda14cbcSMatt Macy 	/*
1438*eda14cbcSMatt Macy 	 * Initialize the interesting rows of the matrix.
1439*eda14cbcSMatt Macy 	 */
1440*eda14cbcSMatt Macy 	vdev_raidz_matrix_init(rm, n, nmissing_rows, parity_map, rows);
1441*eda14cbcSMatt Macy 
1442*eda14cbcSMatt Macy 	/*
1443*eda14cbcSMatt Macy 	 * Invert the matrix.
1444*eda14cbcSMatt Macy 	 */
1445*eda14cbcSMatt Macy 	vdev_raidz_matrix_invert(rm, n, nmissing_rows, missing_rows, rows,
1446*eda14cbcSMatt Macy 	    invrows, used);
1447*eda14cbcSMatt Macy 
1448*eda14cbcSMatt Macy 	/*
1449*eda14cbcSMatt Macy 	 * Reconstruct the missing data using the generated matrix.
1450*eda14cbcSMatt Macy 	 */
1451*eda14cbcSMatt Macy 	vdev_raidz_matrix_reconstruct(rm, n, nmissing_rows, missing_rows,
1452*eda14cbcSMatt Macy 	    invrows, used);
1453*eda14cbcSMatt Macy 
1454*eda14cbcSMatt Macy 	kmem_free(p, psize);
1455*eda14cbcSMatt Macy 
1456*eda14cbcSMatt Macy 	/*
1457*eda14cbcSMatt Macy 	 * copy back from temporary linear abds and free them
1458*eda14cbcSMatt Macy 	 */
1459*eda14cbcSMatt Macy 	if (bufs) {
1460*eda14cbcSMatt Macy 		for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
1461*eda14cbcSMatt Macy 			raidz_col_t *col = &rm->rm_col[c];
1462*eda14cbcSMatt Macy 
1463*eda14cbcSMatt Macy 			abd_copy(bufs[c], col->rc_abd, col->rc_size);
1464*eda14cbcSMatt Macy 			abd_free(col->rc_abd);
1465*eda14cbcSMatt Macy 			col->rc_abd = bufs[c];
1466*eda14cbcSMatt Macy 		}
1467*eda14cbcSMatt Macy 		kmem_free(bufs, rm->rm_cols * sizeof (abd_t *));
1468*eda14cbcSMatt Macy 	}
1469*eda14cbcSMatt Macy 
1470*eda14cbcSMatt Macy 	return (code);
1471*eda14cbcSMatt Macy }
1472*eda14cbcSMatt Macy 
1473*eda14cbcSMatt Macy int
1474*eda14cbcSMatt Macy vdev_raidz_reconstruct(raidz_map_t *rm, const int *t, int nt)
1475*eda14cbcSMatt Macy {
1476*eda14cbcSMatt Macy 	int tgts[VDEV_RAIDZ_MAXPARITY], *dt;
1477*eda14cbcSMatt Macy 	int ntgts;
1478*eda14cbcSMatt Macy 	int i, c, ret;
1479*eda14cbcSMatt Macy 	int code;
1480*eda14cbcSMatt Macy 	int nbadparity, nbaddata;
1481*eda14cbcSMatt Macy 	int parity_valid[VDEV_RAIDZ_MAXPARITY];
1482*eda14cbcSMatt Macy 
1483*eda14cbcSMatt Macy 	/*
1484*eda14cbcSMatt Macy 	 * The tgts list must already be sorted.
1485*eda14cbcSMatt Macy 	 */
1486*eda14cbcSMatt Macy 	for (i = 1; i < nt; i++) {
1487*eda14cbcSMatt Macy 		ASSERT(t[i] > t[i - 1]);
1488*eda14cbcSMatt Macy 	}
1489*eda14cbcSMatt Macy 
1490*eda14cbcSMatt Macy 	nbadparity = rm->rm_firstdatacol;
1491*eda14cbcSMatt Macy 	nbaddata = rm->rm_cols - nbadparity;
1492*eda14cbcSMatt Macy 	ntgts = 0;
1493*eda14cbcSMatt Macy 	for (i = 0, c = 0; c < rm->rm_cols; c++) {
1494*eda14cbcSMatt Macy 		if (c < rm->rm_firstdatacol)
1495*eda14cbcSMatt Macy 			parity_valid[c] = B_FALSE;
1496*eda14cbcSMatt Macy 
1497*eda14cbcSMatt Macy 		if (i < nt && c == t[i]) {
1498*eda14cbcSMatt Macy 			tgts[ntgts++] = c;
1499*eda14cbcSMatt Macy 			i++;
1500*eda14cbcSMatt Macy 		} else if (rm->rm_col[c].rc_error != 0) {
1501*eda14cbcSMatt Macy 			tgts[ntgts++] = c;
1502*eda14cbcSMatt Macy 		} else if (c >= rm->rm_firstdatacol) {
1503*eda14cbcSMatt Macy 			nbaddata--;
1504*eda14cbcSMatt Macy 		} else {
1505*eda14cbcSMatt Macy 			parity_valid[c] = B_TRUE;
1506*eda14cbcSMatt Macy 			nbadparity--;
1507*eda14cbcSMatt Macy 		}
1508*eda14cbcSMatt Macy 	}
1509*eda14cbcSMatt Macy 
1510*eda14cbcSMatt Macy 	ASSERT(ntgts >= nt);
1511*eda14cbcSMatt Macy 	ASSERT(nbaddata >= 0);
1512*eda14cbcSMatt Macy 	ASSERT(nbaddata + nbadparity == ntgts);
1513*eda14cbcSMatt Macy 
1514*eda14cbcSMatt Macy 	dt = &tgts[nbadparity];
1515*eda14cbcSMatt Macy 
1516*eda14cbcSMatt Macy 	/* Reconstruct using the new math implementation */
1517*eda14cbcSMatt Macy 	ret = vdev_raidz_math_reconstruct(rm, parity_valid, dt, nbaddata);
1518*eda14cbcSMatt Macy 	if (ret != RAIDZ_ORIGINAL_IMPL)
1519*eda14cbcSMatt Macy 		return (ret);
1520*eda14cbcSMatt Macy 
1521*eda14cbcSMatt Macy 	/*
1522*eda14cbcSMatt Macy 	 * See if we can use any of our optimized reconstruction routines.
1523*eda14cbcSMatt Macy 	 */
1524*eda14cbcSMatt Macy 	switch (nbaddata) {
1525*eda14cbcSMatt Macy 	case 1:
1526*eda14cbcSMatt Macy 		if (parity_valid[VDEV_RAIDZ_P])
1527*eda14cbcSMatt Macy 			return (vdev_raidz_reconstruct_p(rm, dt, 1));
1528*eda14cbcSMatt Macy 
1529*eda14cbcSMatt Macy 		ASSERT(rm->rm_firstdatacol > 1);
1530*eda14cbcSMatt Macy 
1531*eda14cbcSMatt Macy 		if (parity_valid[VDEV_RAIDZ_Q])
1532*eda14cbcSMatt Macy 			return (vdev_raidz_reconstruct_q(rm, dt, 1));
1533*eda14cbcSMatt Macy 
1534*eda14cbcSMatt Macy 		ASSERT(rm->rm_firstdatacol > 2);
1535*eda14cbcSMatt Macy 		break;
1536*eda14cbcSMatt Macy 
1537*eda14cbcSMatt Macy 	case 2:
1538*eda14cbcSMatt Macy 		ASSERT(rm->rm_firstdatacol > 1);
1539*eda14cbcSMatt Macy 
1540*eda14cbcSMatt Macy 		if (parity_valid[VDEV_RAIDZ_P] &&
1541*eda14cbcSMatt Macy 		    parity_valid[VDEV_RAIDZ_Q])
1542*eda14cbcSMatt Macy 			return (vdev_raidz_reconstruct_pq(rm, dt, 2));
1543*eda14cbcSMatt Macy 
1544*eda14cbcSMatt Macy 		ASSERT(rm->rm_firstdatacol > 2);
1545*eda14cbcSMatt Macy 
1546*eda14cbcSMatt Macy 		break;
1547*eda14cbcSMatt Macy 	}
1548*eda14cbcSMatt Macy 
1549*eda14cbcSMatt Macy 	code = vdev_raidz_reconstruct_general(rm, tgts, ntgts);
1550*eda14cbcSMatt Macy 	ASSERT(code < (1 << VDEV_RAIDZ_MAXPARITY));
1551*eda14cbcSMatt Macy 	ASSERT(code > 0);
1552*eda14cbcSMatt Macy 	return (code);
1553*eda14cbcSMatt Macy }
1554*eda14cbcSMatt Macy 
1555*eda14cbcSMatt Macy static int
1556*eda14cbcSMatt Macy vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
1557*eda14cbcSMatt Macy     uint64_t *logical_ashift, uint64_t *physical_ashift)
1558*eda14cbcSMatt Macy {
1559*eda14cbcSMatt Macy 	vdev_t *cvd;
1560*eda14cbcSMatt Macy 	uint64_t nparity = vd->vdev_nparity;
1561*eda14cbcSMatt Macy 	int c;
1562*eda14cbcSMatt Macy 	int lasterror = 0;
1563*eda14cbcSMatt Macy 	int numerrors = 0;
1564*eda14cbcSMatt Macy 
1565*eda14cbcSMatt Macy 	ASSERT(nparity > 0);
1566*eda14cbcSMatt Macy 
1567*eda14cbcSMatt Macy 	if (nparity > VDEV_RAIDZ_MAXPARITY ||
1568*eda14cbcSMatt Macy 	    vd->vdev_children < nparity + 1) {
1569*eda14cbcSMatt Macy 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
1570*eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1571*eda14cbcSMatt Macy 	}
1572*eda14cbcSMatt Macy 
1573*eda14cbcSMatt Macy 	vdev_open_children(vd);
1574*eda14cbcSMatt Macy 
1575*eda14cbcSMatt Macy 	for (c = 0; c < vd->vdev_children; c++) {
1576*eda14cbcSMatt Macy 		cvd = vd->vdev_child[c];
1577*eda14cbcSMatt Macy 
1578*eda14cbcSMatt Macy 		if (cvd->vdev_open_error != 0) {
1579*eda14cbcSMatt Macy 			lasterror = cvd->vdev_open_error;
1580*eda14cbcSMatt Macy 			numerrors++;
1581*eda14cbcSMatt Macy 			continue;
1582*eda14cbcSMatt Macy 		}
1583*eda14cbcSMatt Macy 
1584*eda14cbcSMatt Macy 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
1585*eda14cbcSMatt Macy 		*max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
1586*eda14cbcSMatt Macy 		*logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
1587*eda14cbcSMatt Macy 		*physical_ashift = MAX(*physical_ashift,
1588*eda14cbcSMatt Macy 		    cvd->vdev_physical_ashift);
1589*eda14cbcSMatt Macy 	}
1590*eda14cbcSMatt Macy 
1591*eda14cbcSMatt Macy 	*asize *= vd->vdev_children;
1592*eda14cbcSMatt Macy 	*max_asize *= vd->vdev_children;
1593*eda14cbcSMatt Macy 
1594*eda14cbcSMatt Macy 	if (numerrors > nparity) {
1595*eda14cbcSMatt Macy 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
1596*eda14cbcSMatt Macy 		return (lasterror);
1597*eda14cbcSMatt Macy 	}
1598*eda14cbcSMatt Macy 
1599*eda14cbcSMatt Macy 	return (0);
1600*eda14cbcSMatt Macy }
1601*eda14cbcSMatt Macy 
1602*eda14cbcSMatt Macy static void
1603*eda14cbcSMatt Macy vdev_raidz_close(vdev_t *vd)
1604*eda14cbcSMatt Macy {
1605*eda14cbcSMatt Macy 	int c;
1606*eda14cbcSMatt Macy 
1607*eda14cbcSMatt Macy 	for (c = 0; c < vd->vdev_children; c++)
1608*eda14cbcSMatt Macy 		vdev_close(vd->vdev_child[c]);
1609*eda14cbcSMatt Macy }
1610*eda14cbcSMatt Macy 
1611*eda14cbcSMatt Macy static uint64_t
1612*eda14cbcSMatt Macy vdev_raidz_asize(vdev_t *vd, uint64_t psize)
1613*eda14cbcSMatt Macy {
1614*eda14cbcSMatt Macy 	uint64_t asize;
1615*eda14cbcSMatt Macy 	uint64_t ashift = vd->vdev_top->vdev_ashift;
1616*eda14cbcSMatt Macy 	uint64_t cols = vd->vdev_children;
1617*eda14cbcSMatt Macy 	uint64_t nparity = vd->vdev_nparity;
1618*eda14cbcSMatt Macy 
1619*eda14cbcSMatt Macy 	asize = ((psize - 1) >> ashift) + 1;
1620*eda14cbcSMatt Macy 	asize += nparity * ((asize + cols - nparity - 1) / (cols - nparity));
1621*eda14cbcSMatt Macy 	asize = roundup(asize, nparity + 1) << ashift;
1622*eda14cbcSMatt Macy 
1623*eda14cbcSMatt Macy 	return (asize);
1624*eda14cbcSMatt Macy }
1625*eda14cbcSMatt Macy 
1626*eda14cbcSMatt Macy static void
1627*eda14cbcSMatt Macy vdev_raidz_child_done(zio_t *zio)
1628*eda14cbcSMatt Macy {
1629*eda14cbcSMatt Macy 	raidz_col_t *rc = zio->io_private;
1630*eda14cbcSMatt Macy 
1631*eda14cbcSMatt Macy 	rc->rc_error = zio->io_error;
1632*eda14cbcSMatt Macy 	rc->rc_tried = 1;
1633*eda14cbcSMatt Macy 	rc->rc_skipped = 0;
1634*eda14cbcSMatt Macy }
1635*eda14cbcSMatt Macy 
1636*eda14cbcSMatt Macy static void
1637*eda14cbcSMatt Macy vdev_raidz_io_verify(zio_t *zio, raidz_map_t *rm, int col)
1638*eda14cbcSMatt Macy {
1639*eda14cbcSMatt Macy #ifdef ZFS_DEBUG
1640*eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
1641*eda14cbcSMatt Macy 	vdev_t *tvd = vd->vdev_top;
1642*eda14cbcSMatt Macy 
1643*eda14cbcSMatt Macy 	range_seg64_t logical_rs, physical_rs;
1644*eda14cbcSMatt Macy 	logical_rs.rs_start = zio->io_offset;
1645*eda14cbcSMatt Macy 	logical_rs.rs_end = logical_rs.rs_start +
1646*eda14cbcSMatt Macy 	    vdev_raidz_asize(zio->io_vd, zio->io_size);
1647*eda14cbcSMatt Macy 
1648*eda14cbcSMatt Macy 	raidz_col_t *rc = &rm->rm_col[col];
1649*eda14cbcSMatt Macy 	vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
1650*eda14cbcSMatt Macy 
1651*eda14cbcSMatt Macy 	vdev_xlate(cvd, &logical_rs, &physical_rs);
1652*eda14cbcSMatt Macy 	ASSERT3U(rc->rc_offset, ==, physical_rs.rs_start);
1653*eda14cbcSMatt Macy 	ASSERT3U(rc->rc_offset, <, physical_rs.rs_end);
1654*eda14cbcSMatt Macy 	/*
1655*eda14cbcSMatt Macy 	 * It would be nice to assert that rs_end is equal
1656*eda14cbcSMatt Macy 	 * to rc_offset + rc_size but there might be an
1657*eda14cbcSMatt Macy 	 * optional I/O at the end that is not accounted in
1658*eda14cbcSMatt Macy 	 * rc_size.
1659*eda14cbcSMatt Macy 	 */
1660*eda14cbcSMatt Macy 	if (physical_rs.rs_end > rc->rc_offset + rc->rc_size) {
1661*eda14cbcSMatt Macy 		ASSERT3U(physical_rs.rs_end, ==, rc->rc_offset +
1662*eda14cbcSMatt Macy 		    rc->rc_size + (1 << tvd->vdev_ashift));
1663*eda14cbcSMatt Macy 	} else {
1664*eda14cbcSMatt Macy 		ASSERT3U(physical_rs.rs_end, ==, rc->rc_offset + rc->rc_size);
1665*eda14cbcSMatt Macy 	}
1666*eda14cbcSMatt Macy #endif
1667*eda14cbcSMatt Macy }
1668*eda14cbcSMatt Macy 
1669*eda14cbcSMatt Macy /*
1670*eda14cbcSMatt Macy  * Start an IO operation on a RAIDZ VDev
1671*eda14cbcSMatt Macy  *
1672*eda14cbcSMatt Macy  * Outline:
1673*eda14cbcSMatt Macy  * - For write operations:
1674*eda14cbcSMatt Macy  *   1. Generate the parity data
1675*eda14cbcSMatt Macy  *   2. Create child zio write operations to each column's vdev, for both
1676*eda14cbcSMatt Macy  *      data and parity.
1677*eda14cbcSMatt Macy  *   3. If the column skips any sectors for padding, create optional dummy
1678*eda14cbcSMatt Macy  *      write zio children for those areas to improve aggregation continuity.
1679*eda14cbcSMatt Macy  * - For read operations:
1680*eda14cbcSMatt Macy  *   1. Create child zio read operations to each data column's vdev to read
1681*eda14cbcSMatt Macy  *      the range of data required for zio.
1682*eda14cbcSMatt Macy  *   2. If this is a scrub or resilver operation, or if any of the data
1683*eda14cbcSMatt Macy  *      vdevs have had errors, then create zio read operations to the parity
1684*eda14cbcSMatt Macy  *      columns' VDevs as well.
1685*eda14cbcSMatt Macy  */
1686*eda14cbcSMatt Macy static void
1687*eda14cbcSMatt Macy vdev_raidz_io_start(zio_t *zio)
1688*eda14cbcSMatt Macy {
1689*eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
1690*eda14cbcSMatt Macy 	vdev_t *tvd = vd->vdev_top;
1691*eda14cbcSMatt Macy 	vdev_t *cvd;
1692*eda14cbcSMatt Macy 	raidz_map_t *rm;
1693*eda14cbcSMatt Macy 	raidz_col_t *rc;
1694*eda14cbcSMatt Macy 	int c, i;
1695*eda14cbcSMatt Macy 
1696*eda14cbcSMatt Macy 	rm = vdev_raidz_map_alloc(zio, tvd->vdev_ashift, vd->vdev_children,
1697*eda14cbcSMatt Macy 	    vd->vdev_nparity);
1698*eda14cbcSMatt Macy 
1699*eda14cbcSMatt Macy 	ASSERT3U(rm->rm_asize, ==, vdev_psize_to_asize(vd, zio->io_size));
1700*eda14cbcSMatt Macy 
1701*eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
1702*eda14cbcSMatt Macy 		vdev_raidz_generate_parity(rm);
1703*eda14cbcSMatt Macy 
1704*eda14cbcSMatt Macy 		for (c = 0; c < rm->rm_cols; c++) {
1705*eda14cbcSMatt Macy 			rc = &rm->rm_col[c];
1706*eda14cbcSMatt Macy 			cvd = vd->vdev_child[rc->rc_devidx];
1707*eda14cbcSMatt Macy 
1708*eda14cbcSMatt Macy 			/*
1709*eda14cbcSMatt Macy 			 * Verify physical to logical translation.
1710*eda14cbcSMatt Macy 			 */
1711*eda14cbcSMatt Macy 			vdev_raidz_io_verify(zio, rm, c);
1712*eda14cbcSMatt Macy 
1713*eda14cbcSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
1714*eda14cbcSMatt Macy 			    rc->rc_offset, rc->rc_abd, rc->rc_size,
1715*eda14cbcSMatt Macy 			    zio->io_type, zio->io_priority, 0,
1716*eda14cbcSMatt Macy 			    vdev_raidz_child_done, rc));
1717*eda14cbcSMatt Macy 		}
1718*eda14cbcSMatt Macy 
1719*eda14cbcSMatt Macy 		/*
1720*eda14cbcSMatt Macy 		 * Generate optional I/Os for any skipped sectors to improve
1721*eda14cbcSMatt Macy 		 * aggregation contiguity.
1722*eda14cbcSMatt Macy 		 */
1723*eda14cbcSMatt Macy 		for (c = rm->rm_skipstart, i = 0; i < rm->rm_nskip; c++, i++) {
1724*eda14cbcSMatt Macy 			ASSERT(c <= rm->rm_scols);
1725*eda14cbcSMatt Macy 			if (c == rm->rm_scols)
1726*eda14cbcSMatt Macy 				c = 0;
1727*eda14cbcSMatt Macy 			rc = &rm->rm_col[c];
1728*eda14cbcSMatt Macy 			cvd = vd->vdev_child[rc->rc_devidx];
1729*eda14cbcSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
1730*eda14cbcSMatt Macy 			    rc->rc_offset + rc->rc_size, NULL,
1731*eda14cbcSMatt Macy 			    1 << tvd->vdev_ashift,
1732*eda14cbcSMatt Macy 			    zio->io_type, zio->io_priority,
1733*eda14cbcSMatt Macy 			    ZIO_FLAG_NODATA | ZIO_FLAG_OPTIONAL, NULL, NULL));
1734*eda14cbcSMatt Macy 		}
1735*eda14cbcSMatt Macy 
1736*eda14cbcSMatt Macy 		zio_execute(zio);
1737*eda14cbcSMatt Macy 		return;
1738*eda14cbcSMatt Macy 	}
1739*eda14cbcSMatt Macy 
1740*eda14cbcSMatt Macy 	ASSERT(zio->io_type == ZIO_TYPE_READ);
1741*eda14cbcSMatt Macy 
1742*eda14cbcSMatt Macy 	/*
1743*eda14cbcSMatt Macy 	 * Iterate over the columns in reverse order so that we hit the parity
1744*eda14cbcSMatt Macy 	 * last -- any errors along the way will force us to read the parity.
1745*eda14cbcSMatt Macy 	 */
1746*eda14cbcSMatt Macy 	for (c = rm->rm_cols - 1; c >= 0; c--) {
1747*eda14cbcSMatt Macy 		rc = &rm->rm_col[c];
1748*eda14cbcSMatt Macy 		cvd = vd->vdev_child[rc->rc_devidx];
1749*eda14cbcSMatt Macy 		if (!vdev_readable(cvd)) {
1750*eda14cbcSMatt Macy 			if (c >= rm->rm_firstdatacol)
1751*eda14cbcSMatt Macy 				rm->rm_missingdata++;
1752*eda14cbcSMatt Macy 			else
1753*eda14cbcSMatt Macy 				rm->rm_missingparity++;
1754*eda14cbcSMatt Macy 			rc->rc_error = SET_ERROR(ENXIO);
1755*eda14cbcSMatt Macy 			rc->rc_tried = 1;	/* don't even try */
1756*eda14cbcSMatt Macy 			rc->rc_skipped = 1;
1757*eda14cbcSMatt Macy 			continue;
1758*eda14cbcSMatt Macy 		}
1759*eda14cbcSMatt Macy 		if (vdev_dtl_contains(cvd, DTL_MISSING, zio->io_txg, 1)) {
1760*eda14cbcSMatt Macy 			if (c >= rm->rm_firstdatacol)
1761*eda14cbcSMatt Macy 				rm->rm_missingdata++;
1762*eda14cbcSMatt Macy 			else
1763*eda14cbcSMatt Macy 				rm->rm_missingparity++;
1764*eda14cbcSMatt Macy 			rc->rc_error = SET_ERROR(ESTALE);
1765*eda14cbcSMatt Macy 			rc->rc_skipped = 1;
1766*eda14cbcSMatt Macy 			continue;
1767*eda14cbcSMatt Macy 		}
1768*eda14cbcSMatt Macy 		if (c >= rm->rm_firstdatacol || rm->rm_missingdata > 0 ||
1769*eda14cbcSMatt Macy 		    (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
1770*eda14cbcSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
1771*eda14cbcSMatt Macy 			    rc->rc_offset, rc->rc_abd, rc->rc_size,
1772*eda14cbcSMatt Macy 			    zio->io_type, zio->io_priority, 0,
1773*eda14cbcSMatt Macy 			    vdev_raidz_child_done, rc));
1774*eda14cbcSMatt Macy 		}
1775*eda14cbcSMatt Macy 	}
1776*eda14cbcSMatt Macy 
1777*eda14cbcSMatt Macy 	zio_execute(zio);
1778*eda14cbcSMatt Macy }
1779*eda14cbcSMatt Macy 
1780*eda14cbcSMatt Macy 
1781*eda14cbcSMatt Macy /*
1782*eda14cbcSMatt Macy  * Report a checksum error for a child of a RAID-Z device.
1783*eda14cbcSMatt Macy  */
1784*eda14cbcSMatt Macy static void
1785*eda14cbcSMatt Macy raidz_checksum_error(zio_t *zio, raidz_col_t *rc, abd_t *bad_data)
1786*eda14cbcSMatt Macy {
1787*eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd->vdev_child[rc->rc_devidx];
1788*eda14cbcSMatt Macy 
1789*eda14cbcSMatt Macy 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
1790*eda14cbcSMatt Macy 		zio_bad_cksum_t zbc;
1791*eda14cbcSMatt Macy 		raidz_map_t *rm = zio->io_vsd;
1792*eda14cbcSMatt Macy 
1793*eda14cbcSMatt Macy 		mutex_enter(&vd->vdev_stat_lock);
1794*eda14cbcSMatt Macy 		vd->vdev_stat.vs_checksum_errors++;
1795*eda14cbcSMatt Macy 		mutex_exit(&vd->vdev_stat_lock);
1796*eda14cbcSMatt Macy 
1797*eda14cbcSMatt Macy 		zbc.zbc_has_cksum = 0;
1798*eda14cbcSMatt Macy 		zbc.zbc_injected = rm->rm_ecksuminjected;
1799*eda14cbcSMatt Macy 
1800*eda14cbcSMatt Macy 		zfs_ereport_post_checksum(zio->io_spa, vd,
1801*eda14cbcSMatt Macy 		    &zio->io_bookmark, zio, rc->rc_offset, rc->rc_size,
1802*eda14cbcSMatt Macy 		    rc->rc_abd, bad_data, &zbc);
1803*eda14cbcSMatt Macy 	}
1804*eda14cbcSMatt Macy }
1805*eda14cbcSMatt Macy 
1806*eda14cbcSMatt Macy /*
1807*eda14cbcSMatt Macy  * We keep track of whether or not there were any injected errors, so that
1808*eda14cbcSMatt Macy  * any ereports we generate can note it.
1809*eda14cbcSMatt Macy  */
1810*eda14cbcSMatt Macy static int
1811*eda14cbcSMatt Macy raidz_checksum_verify(zio_t *zio)
1812*eda14cbcSMatt Macy {
1813*eda14cbcSMatt Macy 	zio_bad_cksum_t zbc;
1814*eda14cbcSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
1815*eda14cbcSMatt Macy 
1816*eda14cbcSMatt Macy 	bzero(&zbc, sizeof (zio_bad_cksum_t));
1817*eda14cbcSMatt Macy 
1818*eda14cbcSMatt Macy 	int ret = zio_checksum_error(zio, &zbc);
1819*eda14cbcSMatt Macy 	if (ret != 0 && zbc.zbc_injected != 0)
1820*eda14cbcSMatt Macy 		rm->rm_ecksuminjected = 1;
1821*eda14cbcSMatt Macy 
1822*eda14cbcSMatt Macy 	return (ret);
1823*eda14cbcSMatt Macy }
1824*eda14cbcSMatt Macy 
1825*eda14cbcSMatt Macy /*
1826*eda14cbcSMatt Macy  * Generate the parity from the data columns. If we tried and were able to
1827*eda14cbcSMatt Macy  * read the parity without error, verify that the generated parity matches the
1828*eda14cbcSMatt Macy  * data we read. If it doesn't, we fire off a checksum error. Return the
1829*eda14cbcSMatt Macy  * number such failures.
1830*eda14cbcSMatt Macy  */
1831*eda14cbcSMatt Macy static int
1832*eda14cbcSMatt Macy raidz_parity_verify(zio_t *zio, raidz_map_t *rm)
1833*eda14cbcSMatt Macy {
1834*eda14cbcSMatt Macy 	abd_t *orig[VDEV_RAIDZ_MAXPARITY];
1835*eda14cbcSMatt Macy 	int c, ret = 0;
1836*eda14cbcSMatt Macy 	raidz_col_t *rc;
1837*eda14cbcSMatt Macy 
1838*eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
1839*eda14cbcSMatt Macy 	enum zio_checksum checksum = (bp == NULL ? zio->io_prop.zp_checksum :
1840*eda14cbcSMatt Macy 	    (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
1841*eda14cbcSMatt Macy 
1842*eda14cbcSMatt Macy 	if (checksum == ZIO_CHECKSUM_NOPARITY)
1843*eda14cbcSMatt Macy 		return (ret);
1844*eda14cbcSMatt Macy 
1845*eda14cbcSMatt Macy 	for (c = 0; c < rm->rm_firstdatacol; c++) {
1846*eda14cbcSMatt Macy 		rc = &rm->rm_col[c];
1847*eda14cbcSMatt Macy 		if (!rc->rc_tried || rc->rc_error != 0)
1848*eda14cbcSMatt Macy 			continue;
1849*eda14cbcSMatt Macy 
1850*eda14cbcSMatt Macy 		orig[c] = abd_alloc_sametype(rc->rc_abd, rc->rc_size);
1851*eda14cbcSMatt Macy 		abd_copy(orig[c], rc->rc_abd, rc->rc_size);
1852*eda14cbcSMatt Macy 	}
1853*eda14cbcSMatt Macy 
1854*eda14cbcSMatt Macy 	vdev_raidz_generate_parity(rm);
1855*eda14cbcSMatt Macy 
1856*eda14cbcSMatt Macy 	for (c = 0; c < rm->rm_firstdatacol; c++) {
1857*eda14cbcSMatt Macy 		rc = &rm->rm_col[c];
1858*eda14cbcSMatt Macy 		if (!rc->rc_tried || rc->rc_error != 0)
1859*eda14cbcSMatt Macy 			continue;
1860*eda14cbcSMatt Macy 		if (abd_cmp(orig[c], rc->rc_abd) != 0) {
1861*eda14cbcSMatt Macy 			raidz_checksum_error(zio, rc, orig[c]);
1862*eda14cbcSMatt Macy 			rc->rc_error = SET_ERROR(ECKSUM);
1863*eda14cbcSMatt Macy 			ret++;
1864*eda14cbcSMatt Macy 		}
1865*eda14cbcSMatt Macy 		abd_free(orig[c]);
1866*eda14cbcSMatt Macy 	}
1867*eda14cbcSMatt Macy 
1868*eda14cbcSMatt Macy 	return (ret);
1869*eda14cbcSMatt Macy }
1870*eda14cbcSMatt Macy 
1871*eda14cbcSMatt Macy static int
1872*eda14cbcSMatt Macy vdev_raidz_worst_error(raidz_map_t *rm)
1873*eda14cbcSMatt Macy {
1874*eda14cbcSMatt Macy 	int error = 0;
1875*eda14cbcSMatt Macy 
1876*eda14cbcSMatt Macy 	for (int c = 0; c < rm->rm_cols; c++)
1877*eda14cbcSMatt Macy 		error = zio_worst_error(error, rm->rm_col[c].rc_error);
1878*eda14cbcSMatt Macy 
1879*eda14cbcSMatt Macy 	return (error);
1880*eda14cbcSMatt Macy }
1881*eda14cbcSMatt Macy 
1882*eda14cbcSMatt Macy /*
1883*eda14cbcSMatt Macy  * Iterate over all combinations of bad data and attempt a reconstruction.
1884*eda14cbcSMatt Macy  * Note that the algorithm below is non-optimal because it doesn't take into
1885*eda14cbcSMatt Macy  * account how reconstruction is actually performed. For example, with
1886*eda14cbcSMatt Macy  * triple-parity RAID-Z the reconstruction procedure is the same if column 4
1887*eda14cbcSMatt Macy  * is targeted as invalid as if columns 1 and 4 are targeted since in both
1888*eda14cbcSMatt Macy  * cases we'd only use parity information in column 0.
1889*eda14cbcSMatt Macy  */
1890*eda14cbcSMatt Macy static int
1891*eda14cbcSMatt Macy vdev_raidz_combrec(zio_t *zio, int total_errors, int data_errors)
1892*eda14cbcSMatt Macy {
1893*eda14cbcSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
1894*eda14cbcSMatt Macy 	raidz_col_t *rc;
1895*eda14cbcSMatt Macy 	abd_t *orig[VDEV_RAIDZ_MAXPARITY];
1896*eda14cbcSMatt Macy 	int tstore[VDEV_RAIDZ_MAXPARITY + 2];
1897*eda14cbcSMatt Macy 	int *tgts = &tstore[1];
1898*eda14cbcSMatt Macy 	int curr, next, i, c, n;
1899*eda14cbcSMatt Macy 	int code, ret = 0;
1900*eda14cbcSMatt Macy 
1901*eda14cbcSMatt Macy 	ASSERT(total_errors < rm->rm_firstdatacol);
1902*eda14cbcSMatt Macy 
1903*eda14cbcSMatt Macy 	/*
1904*eda14cbcSMatt Macy 	 * This simplifies one edge condition.
1905*eda14cbcSMatt Macy 	 */
1906*eda14cbcSMatt Macy 	tgts[-1] = -1;
1907*eda14cbcSMatt Macy 
1908*eda14cbcSMatt Macy 	for (n = 1; n <= rm->rm_firstdatacol - total_errors; n++) {
1909*eda14cbcSMatt Macy 		/*
1910*eda14cbcSMatt Macy 		 * Initialize the targets array by finding the first n columns
1911*eda14cbcSMatt Macy 		 * that contain no error.
1912*eda14cbcSMatt Macy 		 *
1913*eda14cbcSMatt Macy 		 * If there were no data errors, we need to ensure that we're
1914*eda14cbcSMatt Macy 		 * always explicitly attempting to reconstruct at least one
1915*eda14cbcSMatt Macy 		 * data column. To do this, we simply push the highest target
1916*eda14cbcSMatt Macy 		 * up into the data columns.
1917*eda14cbcSMatt Macy 		 */
1918*eda14cbcSMatt Macy 		for (c = 0, i = 0; i < n; i++) {
1919*eda14cbcSMatt Macy 			if (i == n - 1 && data_errors == 0 &&
1920*eda14cbcSMatt Macy 			    c < rm->rm_firstdatacol) {
1921*eda14cbcSMatt Macy 				c = rm->rm_firstdatacol;
1922*eda14cbcSMatt Macy 			}
1923*eda14cbcSMatt Macy 
1924*eda14cbcSMatt Macy 			while (rm->rm_col[c].rc_error != 0) {
1925*eda14cbcSMatt Macy 				c++;
1926*eda14cbcSMatt Macy 				ASSERT3S(c, <, rm->rm_cols);
1927*eda14cbcSMatt Macy 			}
1928*eda14cbcSMatt Macy 
1929*eda14cbcSMatt Macy 			tgts[i] = c++;
1930*eda14cbcSMatt Macy 		}
1931*eda14cbcSMatt Macy 
1932*eda14cbcSMatt Macy 		/*
1933*eda14cbcSMatt Macy 		 * Setting tgts[n] simplifies the other edge condition.
1934*eda14cbcSMatt Macy 		 */
1935*eda14cbcSMatt Macy 		tgts[n] = rm->rm_cols;
1936*eda14cbcSMatt Macy 
1937*eda14cbcSMatt Macy 		/*
1938*eda14cbcSMatt Macy 		 * These buffers were allocated in previous iterations.
1939*eda14cbcSMatt Macy 		 */
1940*eda14cbcSMatt Macy 		for (i = 0; i < n - 1; i++) {
1941*eda14cbcSMatt Macy 			ASSERT(orig[i] != NULL);
1942*eda14cbcSMatt Macy 		}
1943*eda14cbcSMatt Macy 
1944*eda14cbcSMatt Macy 		orig[n - 1] = abd_alloc_sametype(rm->rm_col[0].rc_abd,
1945*eda14cbcSMatt Macy 		    rm->rm_col[0].rc_size);
1946*eda14cbcSMatt Macy 
1947*eda14cbcSMatt Macy 		curr = 0;
1948*eda14cbcSMatt Macy 		next = tgts[curr];
1949*eda14cbcSMatt Macy 
1950*eda14cbcSMatt Macy 		while (curr != n) {
1951*eda14cbcSMatt Macy 			tgts[curr] = next;
1952*eda14cbcSMatt Macy 			curr = 0;
1953*eda14cbcSMatt Macy 
1954*eda14cbcSMatt Macy 			/*
1955*eda14cbcSMatt Macy 			 * Save off the original data that we're going to
1956*eda14cbcSMatt Macy 			 * attempt to reconstruct.
1957*eda14cbcSMatt Macy 			 */
1958*eda14cbcSMatt Macy 			for (i = 0; i < n; i++) {
1959*eda14cbcSMatt Macy 				ASSERT(orig[i] != NULL);
1960*eda14cbcSMatt Macy 				c = tgts[i];
1961*eda14cbcSMatt Macy 				ASSERT3S(c, >=, 0);
1962*eda14cbcSMatt Macy 				ASSERT3S(c, <, rm->rm_cols);
1963*eda14cbcSMatt Macy 				rc = &rm->rm_col[c];
1964*eda14cbcSMatt Macy 				abd_copy(orig[i], rc->rc_abd, rc->rc_size);
1965*eda14cbcSMatt Macy 			}
1966*eda14cbcSMatt Macy 
1967*eda14cbcSMatt Macy 			/*
1968*eda14cbcSMatt Macy 			 * Attempt a reconstruction and exit the outer loop on
1969*eda14cbcSMatt Macy 			 * success.
1970*eda14cbcSMatt Macy 			 */
1971*eda14cbcSMatt Macy 			code = vdev_raidz_reconstruct(rm, tgts, n);
1972*eda14cbcSMatt Macy 			if (raidz_checksum_verify(zio) == 0) {
1973*eda14cbcSMatt Macy 
1974*eda14cbcSMatt Macy 				for (i = 0; i < n; i++) {
1975*eda14cbcSMatt Macy 					c = tgts[i];
1976*eda14cbcSMatt Macy 					rc = &rm->rm_col[c];
1977*eda14cbcSMatt Macy 					ASSERT(rc->rc_error == 0);
1978*eda14cbcSMatt Macy 					if (rc->rc_tried)
1979*eda14cbcSMatt Macy 						raidz_checksum_error(zio, rc,
1980*eda14cbcSMatt Macy 						    orig[i]);
1981*eda14cbcSMatt Macy 					rc->rc_error = SET_ERROR(ECKSUM);
1982*eda14cbcSMatt Macy 				}
1983*eda14cbcSMatt Macy 
1984*eda14cbcSMatt Macy 				ret = code;
1985*eda14cbcSMatt Macy 				goto done;
1986*eda14cbcSMatt Macy 			}
1987*eda14cbcSMatt Macy 
1988*eda14cbcSMatt Macy 			/*
1989*eda14cbcSMatt Macy 			 * Restore the original data.
1990*eda14cbcSMatt Macy 			 */
1991*eda14cbcSMatt Macy 			for (i = 0; i < n; i++) {
1992*eda14cbcSMatt Macy 				c = tgts[i];
1993*eda14cbcSMatt Macy 				rc = &rm->rm_col[c];
1994*eda14cbcSMatt Macy 				abd_copy(rc->rc_abd, orig[i], rc->rc_size);
1995*eda14cbcSMatt Macy 			}
1996*eda14cbcSMatt Macy 
1997*eda14cbcSMatt Macy 			do {
1998*eda14cbcSMatt Macy 				/*
1999*eda14cbcSMatt Macy 				 * Find the next valid column after the curr
2000*eda14cbcSMatt Macy 				 * position..
2001*eda14cbcSMatt Macy 				 */
2002*eda14cbcSMatt Macy 				for (next = tgts[curr] + 1;
2003*eda14cbcSMatt Macy 				    next < rm->rm_cols &&
2004*eda14cbcSMatt Macy 				    rm->rm_col[next].rc_error != 0; next++)
2005*eda14cbcSMatt Macy 					continue;
2006*eda14cbcSMatt Macy 
2007*eda14cbcSMatt Macy 				ASSERT(next <= tgts[curr + 1]);
2008*eda14cbcSMatt Macy 
2009*eda14cbcSMatt Macy 				/*
2010*eda14cbcSMatt Macy 				 * If that spot is available, we're done here.
2011*eda14cbcSMatt Macy 				 */
2012*eda14cbcSMatt Macy 				if (next != tgts[curr + 1])
2013*eda14cbcSMatt Macy 					break;
2014*eda14cbcSMatt Macy 
2015*eda14cbcSMatt Macy 				/*
2016*eda14cbcSMatt Macy 				 * Otherwise, find the next valid column after
2017*eda14cbcSMatt Macy 				 * the previous position.
2018*eda14cbcSMatt Macy 				 */
2019*eda14cbcSMatt Macy 				for (c = tgts[curr - 1] + 1;
2020*eda14cbcSMatt Macy 				    rm->rm_col[c].rc_error != 0; c++)
2021*eda14cbcSMatt Macy 					continue;
2022*eda14cbcSMatt Macy 
2023*eda14cbcSMatt Macy 				tgts[curr] = c;
2024*eda14cbcSMatt Macy 				curr++;
2025*eda14cbcSMatt Macy 
2026*eda14cbcSMatt Macy 			} while (curr != n);
2027*eda14cbcSMatt Macy 		}
2028*eda14cbcSMatt Macy 	}
2029*eda14cbcSMatt Macy 	n--;
2030*eda14cbcSMatt Macy done:
2031*eda14cbcSMatt Macy 	for (i = 0; i < n; i++)
2032*eda14cbcSMatt Macy 		abd_free(orig[i]);
2033*eda14cbcSMatt Macy 
2034*eda14cbcSMatt Macy 	return (ret);
2035*eda14cbcSMatt Macy }
2036*eda14cbcSMatt Macy 
2037*eda14cbcSMatt Macy /*
2038*eda14cbcSMatt Macy  * Complete an IO operation on a RAIDZ VDev
2039*eda14cbcSMatt Macy  *
2040*eda14cbcSMatt Macy  * Outline:
2041*eda14cbcSMatt Macy  * - For write operations:
2042*eda14cbcSMatt Macy  *   1. Check for errors on the child IOs.
2043*eda14cbcSMatt Macy  *   2. Return, setting an error code if too few child VDevs were written
2044*eda14cbcSMatt Macy  *      to reconstruct the data later.  Note that partial writes are
2045*eda14cbcSMatt Macy  *      considered successful if they can be reconstructed at all.
2046*eda14cbcSMatt Macy  * - For read operations:
2047*eda14cbcSMatt Macy  *   1. Check for errors on the child IOs.
2048*eda14cbcSMatt Macy  *   2. If data errors occurred:
2049*eda14cbcSMatt Macy  *      a. Try to reassemble the data from the parity available.
2050*eda14cbcSMatt Macy  *      b. If we haven't yet read the parity drives, read them now.
2051*eda14cbcSMatt Macy  *      c. If all parity drives have been read but the data still doesn't
2052*eda14cbcSMatt Macy  *         reassemble with a correct checksum, then try combinatorial
2053*eda14cbcSMatt Macy  *         reconstruction.
2054*eda14cbcSMatt Macy  *      d. If that doesn't work, return an error.
2055*eda14cbcSMatt Macy  *   3. If there were unexpected errors or this is a resilver operation,
2056*eda14cbcSMatt Macy  *      rewrite the vdevs that had errors.
2057*eda14cbcSMatt Macy  */
2058*eda14cbcSMatt Macy static void
2059*eda14cbcSMatt Macy vdev_raidz_io_done(zio_t *zio)
2060*eda14cbcSMatt Macy {
2061*eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
2062*eda14cbcSMatt Macy 	vdev_t *cvd;
2063*eda14cbcSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
2064*eda14cbcSMatt Macy 	raidz_col_t *rc = NULL;
2065*eda14cbcSMatt Macy 	int unexpected_errors = 0;
2066*eda14cbcSMatt Macy 	int parity_errors = 0;
2067*eda14cbcSMatt Macy 	int parity_untried = 0;
2068*eda14cbcSMatt Macy 	int data_errors = 0;
2069*eda14cbcSMatt Macy 	int total_errors = 0;
2070*eda14cbcSMatt Macy 	int n, c;
2071*eda14cbcSMatt Macy 	int tgts[VDEV_RAIDZ_MAXPARITY];
2072*eda14cbcSMatt Macy 	int code;
2073*eda14cbcSMatt Macy 
2074*eda14cbcSMatt Macy 	ASSERT(zio->io_bp != NULL);  /* XXX need to add code to enforce this */
2075*eda14cbcSMatt Macy 
2076*eda14cbcSMatt Macy 	ASSERT(rm->rm_missingparity <= rm->rm_firstdatacol);
2077*eda14cbcSMatt Macy 	ASSERT(rm->rm_missingdata <= rm->rm_cols - rm->rm_firstdatacol);
2078*eda14cbcSMatt Macy 
2079*eda14cbcSMatt Macy 	for (c = 0; c < rm->rm_cols; c++) {
2080*eda14cbcSMatt Macy 		rc = &rm->rm_col[c];
2081*eda14cbcSMatt Macy 
2082*eda14cbcSMatt Macy 		if (rc->rc_error) {
2083*eda14cbcSMatt Macy 			ASSERT(rc->rc_error != ECKSUM);	/* child has no bp */
2084*eda14cbcSMatt Macy 
2085*eda14cbcSMatt Macy 			if (c < rm->rm_firstdatacol)
2086*eda14cbcSMatt Macy 				parity_errors++;
2087*eda14cbcSMatt Macy 			else
2088*eda14cbcSMatt Macy 				data_errors++;
2089*eda14cbcSMatt Macy 
2090*eda14cbcSMatt Macy 			if (!rc->rc_skipped)
2091*eda14cbcSMatt Macy 				unexpected_errors++;
2092*eda14cbcSMatt Macy 
2093*eda14cbcSMatt Macy 			total_errors++;
2094*eda14cbcSMatt Macy 		} else if (c < rm->rm_firstdatacol && !rc->rc_tried) {
2095*eda14cbcSMatt Macy 			parity_untried++;
2096*eda14cbcSMatt Macy 		}
2097*eda14cbcSMatt Macy 	}
2098*eda14cbcSMatt Macy 
2099*eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
2100*eda14cbcSMatt Macy 		/*
2101*eda14cbcSMatt Macy 		 * XXX -- for now, treat partial writes as a success.
2102*eda14cbcSMatt Macy 		 * (If we couldn't write enough columns to reconstruct
2103*eda14cbcSMatt Macy 		 * the data, the I/O failed.  Otherwise, good enough.)
2104*eda14cbcSMatt Macy 		 *
2105*eda14cbcSMatt Macy 		 * Now that we support write reallocation, it would be better
2106*eda14cbcSMatt Macy 		 * to treat partial failure as real failure unless there are
2107*eda14cbcSMatt Macy 		 * no non-degraded top-level vdevs left, and not update DTLs
2108*eda14cbcSMatt Macy 		 * if we intend to reallocate.
2109*eda14cbcSMatt Macy 		 */
2110*eda14cbcSMatt Macy 		/* XXPOLICY */
2111*eda14cbcSMatt Macy 		if (total_errors > rm->rm_firstdatacol)
2112*eda14cbcSMatt Macy 			zio->io_error = vdev_raidz_worst_error(rm);
2113*eda14cbcSMatt Macy 
2114*eda14cbcSMatt Macy 		return;
2115*eda14cbcSMatt Macy 	}
2116*eda14cbcSMatt Macy 
2117*eda14cbcSMatt Macy 	ASSERT(zio->io_type == ZIO_TYPE_READ);
2118*eda14cbcSMatt Macy 	/*
2119*eda14cbcSMatt Macy 	 * There are three potential phases for a read:
2120*eda14cbcSMatt Macy 	 *	1. produce valid data from the columns read
2121*eda14cbcSMatt Macy 	 *	2. read all disks and try again
2122*eda14cbcSMatt Macy 	 *	3. perform combinatorial reconstruction
2123*eda14cbcSMatt Macy 	 *
2124*eda14cbcSMatt Macy 	 * Each phase is progressively both more expensive and less likely to
2125*eda14cbcSMatt Macy 	 * occur. If we encounter more errors than we can repair or all phases
2126*eda14cbcSMatt Macy 	 * fail, we have no choice but to return an error.
2127*eda14cbcSMatt Macy 	 */
2128*eda14cbcSMatt Macy 
2129*eda14cbcSMatt Macy 	/*
2130*eda14cbcSMatt Macy 	 * If the number of errors we saw was correctable -- less than or equal
2131*eda14cbcSMatt Macy 	 * to the number of parity disks read -- attempt to produce data that
2132*eda14cbcSMatt Macy 	 * has a valid checksum. Naturally, this case applies in the absence of
2133*eda14cbcSMatt Macy 	 * any errors.
2134*eda14cbcSMatt Macy 	 */
2135*eda14cbcSMatt Macy 	if (total_errors <= rm->rm_firstdatacol - parity_untried) {
2136*eda14cbcSMatt Macy 		if (data_errors == 0) {
2137*eda14cbcSMatt Macy 			if (raidz_checksum_verify(zio) == 0) {
2138*eda14cbcSMatt Macy 				/*
2139*eda14cbcSMatt Macy 				 * If we read parity information (unnecessarily
2140*eda14cbcSMatt Macy 				 * as it happens since no reconstruction was
2141*eda14cbcSMatt Macy 				 * needed) regenerate and verify the parity.
2142*eda14cbcSMatt Macy 				 * We also regenerate parity when resilvering
2143*eda14cbcSMatt Macy 				 * so we can write it out to the failed device
2144*eda14cbcSMatt Macy 				 * later.
2145*eda14cbcSMatt Macy 				 */
2146*eda14cbcSMatt Macy 				if (parity_errors + parity_untried <
2147*eda14cbcSMatt Macy 				    rm->rm_firstdatacol ||
2148*eda14cbcSMatt Macy 				    (zio->io_flags & ZIO_FLAG_RESILVER)) {
2149*eda14cbcSMatt Macy 					n = raidz_parity_verify(zio, rm);
2150*eda14cbcSMatt Macy 					unexpected_errors += n;
2151*eda14cbcSMatt Macy 					ASSERT(parity_errors + n <=
2152*eda14cbcSMatt Macy 					    rm->rm_firstdatacol);
2153*eda14cbcSMatt Macy 				}
2154*eda14cbcSMatt Macy 				goto done;
2155*eda14cbcSMatt Macy 			}
2156*eda14cbcSMatt Macy 		} else {
2157*eda14cbcSMatt Macy 			/*
2158*eda14cbcSMatt Macy 			 * We either attempt to read all the parity columns or
2159*eda14cbcSMatt Macy 			 * none of them. If we didn't try to read parity, we
2160*eda14cbcSMatt Macy 			 * wouldn't be here in the correctable case. There must
2161*eda14cbcSMatt Macy 			 * also have been fewer parity errors than parity
2162*eda14cbcSMatt Macy 			 * columns or, again, we wouldn't be in this code path.
2163*eda14cbcSMatt Macy 			 */
2164*eda14cbcSMatt Macy 			ASSERT(parity_untried == 0);
2165*eda14cbcSMatt Macy 			ASSERT(parity_errors < rm->rm_firstdatacol);
2166*eda14cbcSMatt Macy 
2167*eda14cbcSMatt Macy 			/*
2168*eda14cbcSMatt Macy 			 * Identify the data columns that reported an error.
2169*eda14cbcSMatt Macy 			 */
2170*eda14cbcSMatt Macy 			n = 0;
2171*eda14cbcSMatt Macy 			for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
2172*eda14cbcSMatt Macy 				rc = &rm->rm_col[c];
2173*eda14cbcSMatt Macy 				if (rc->rc_error != 0) {
2174*eda14cbcSMatt Macy 					ASSERT(n < VDEV_RAIDZ_MAXPARITY);
2175*eda14cbcSMatt Macy 					tgts[n++] = c;
2176*eda14cbcSMatt Macy 				}
2177*eda14cbcSMatt Macy 			}
2178*eda14cbcSMatt Macy 
2179*eda14cbcSMatt Macy 			ASSERT(rm->rm_firstdatacol >= n);
2180*eda14cbcSMatt Macy 
2181*eda14cbcSMatt Macy 			code = vdev_raidz_reconstruct(rm, tgts, n);
2182*eda14cbcSMatt Macy 
2183*eda14cbcSMatt Macy 			if (raidz_checksum_verify(zio) == 0) {
2184*eda14cbcSMatt Macy 				/*
2185*eda14cbcSMatt Macy 				 * If we read more parity disks than were used
2186*eda14cbcSMatt Macy 				 * for reconstruction, confirm that the other
2187*eda14cbcSMatt Macy 				 * parity disks produced correct data. This
2188*eda14cbcSMatt Macy 				 * routine is suboptimal in that it regenerates
2189*eda14cbcSMatt Macy 				 * the parity that we already used in addition
2190*eda14cbcSMatt Macy 				 * to the parity that we're attempting to
2191*eda14cbcSMatt Macy 				 * verify, but this should be a relatively
2192*eda14cbcSMatt Macy 				 * uncommon case, and can be optimized if it
2193*eda14cbcSMatt Macy 				 * becomes a problem. Note that we regenerate
2194*eda14cbcSMatt Macy 				 * parity when resilvering so we can write it
2195*eda14cbcSMatt Macy 				 * out to failed devices later.
2196*eda14cbcSMatt Macy 				 */
2197*eda14cbcSMatt Macy 				if (parity_errors < rm->rm_firstdatacol - n ||
2198*eda14cbcSMatt Macy 				    (zio->io_flags & ZIO_FLAG_RESILVER)) {
2199*eda14cbcSMatt Macy 					n = raidz_parity_verify(zio, rm);
2200*eda14cbcSMatt Macy 					unexpected_errors += n;
2201*eda14cbcSMatt Macy 					ASSERT(parity_errors + n <=
2202*eda14cbcSMatt Macy 					    rm->rm_firstdatacol);
2203*eda14cbcSMatt Macy 				}
2204*eda14cbcSMatt Macy 
2205*eda14cbcSMatt Macy 				goto done;
2206*eda14cbcSMatt Macy 			}
2207*eda14cbcSMatt Macy 		}
2208*eda14cbcSMatt Macy 	}
2209*eda14cbcSMatt Macy 
2210*eda14cbcSMatt Macy 	/*
2211*eda14cbcSMatt Macy 	 * This isn't a typical situation -- either we got a read error or
2212*eda14cbcSMatt Macy 	 * a child silently returned bad data. Read every block so we can
2213*eda14cbcSMatt Macy 	 * try again with as much data and parity as we can track down. If
2214*eda14cbcSMatt Macy 	 * we've already been through once before, all children will be marked
2215*eda14cbcSMatt Macy 	 * as tried so we'll proceed to combinatorial reconstruction.
2216*eda14cbcSMatt Macy 	 */
2217*eda14cbcSMatt Macy 	unexpected_errors = 1;
2218*eda14cbcSMatt Macy 	rm->rm_missingdata = 0;
2219*eda14cbcSMatt Macy 	rm->rm_missingparity = 0;
2220*eda14cbcSMatt Macy 
2221*eda14cbcSMatt Macy 	for (c = 0; c < rm->rm_cols; c++) {
2222*eda14cbcSMatt Macy 		if (rm->rm_col[c].rc_tried)
2223*eda14cbcSMatt Macy 			continue;
2224*eda14cbcSMatt Macy 
2225*eda14cbcSMatt Macy 		zio_vdev_io_redone(zio);
2226*eda14cbcSMatt Macy 		do {
2227*eda14cbcSMatt Macy 			rc = &rm->rm_col[c];
2228*eda14cbcSMatt Macy 			if (rc->rc_tried)
2229*eda14cbcSMatt Macy 				continue;
2230*eda14cbcSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL,
2231*eda14cbcSMatt Macy 			    vd->vdev_child[rc->rc_devidx],
2232*eda14cbcSMatt Macy 			    rc->rc_offset, rc->rc_abd, rc->rc_size,
2233*eda14cbcSMatt Macy 			    zio->io_type, zio->io_priority, 0,
2234*eda14cbcSMatt Macy 			    vdev_raidz_child_done, rc));
2235*eda14cbcSMatt Macy 		} while (++c < rm->rm_cols);
2236*eda14cbcSMatt Macy 
2237*eda14cbcSMatt Macy 		return;
2238*eda14cbcSMatt Macy 	}
2239*eda14cbcSMatt Macy 
2240*eda14cbcSMatt Macy 	/*
2241*eda14cbcSMatt Macy 	 * At this point we've attempted to reconstruct the data given the
2242*eda14cbcSMatt Macy 	 * errors we detected, and we've attempted to read all columns. There
2243*eda14cbcSMatt Macy 	 * must, therefore, be one or more additional problems -- silent errors
2244*eda14cbcSMatt Macy 	 * resulting in invalid data rather than explicit I/O errors resulting
2245*eda14cbcSMatt Macy 	 * in absent data. We check if there is enough additional data to
2246*eda14cbcSMatt Macy 	 * possibly reconstruct the data and then perform combinatorial
2247*eda14cbcSMatt Macy 	 * reconstruction over all possible combinations. If that fails,
2248*eda14cbcSMatt Macy 	 * we're cooked.
2249*eda14cbcSMatt Macy 	 */
2250*eda14cbcSMatt Macy 	if (total_errors > rm->rm_firstdatacol) {
2251*eda14cbcSMatt Macy 		zio->io_error = vdev_raidz_worst_error(rm);
2252*eda14cbcSMatt Macy 
2253*eda14cbcSMatt Macy 	} else if (total_errors < rm->rm_firstdatacol &&
2254*eda14cbcSMatt Macy 	    (code = vdev_raidz_combrec(zio, total_errors, data_errors)) != 0) {
2255*eda14cbcSMatt Macy 		/*
2256*eda14cbcSMatt Macy 		 * If we didn't use all the available parity for the
2257*eda14cbcSMatt Macy 		 * combinatorial reconstruction, verify that the remaining
2258*eda14cbcSMatt Macy 		 * parity is correct.
2259*eda14cbcSMatt Macy 		 */
2260*eda14cbcSMatt Macy 		if (code != (1 << rm->rm_firstdatacol) - 1)
2261*eda14cbcSMatt Macy 			(void) raidz_parity_verify(zio, rm);
2262*eda14cbcSMatt Macy 	} else {
2263*eda14cbcSMatt Macy 		/*
2264*eda14cbcSMatt Macy 		 * We're here because either:
2265*eda14cbcSMatt Macy 		 *
2266*eda14cbcSMatt Macy 		 *	total_errors == rm_first_datacol, or
2267*eda14cbcSMatt Macy 		 *	vdev_raidz_combrec() failed
2268*eda14cbcSMatt Macy 		 *
2269*eda14cbcSMatt Macy 		 * In either case, there is enough bad data to prevent
2270*eda14cbcSMatt Macy 		 * reconstruction.
2271*eda14cbcSMatt Macy 		 *
2272*eda14cbcSMatt Macy 		 * Start checksum ereports for all children which haven't
2273*eda14cbcSMatt Macy 		 * failed, and the IO wasn't speculative.
2274*eda14cbcSMatt Macy 		 */
2275*eda14cbcSMatt Macy 		zio->io_error = SET_ERROR(ECKSUM);
2276*eda14cbcSMatt Macy 
2277*eda14cbcSMatt Macy 		if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2278*eda14cbcSMatt Macy 			for (c = 0; c < rm->rm_cols; c++) {
2279*eda14cbcSMatt Macy 				vdev_t *cvd;
2280*eda14cbcSMatt Macy 				rc = &rm->rm_col[c];
2281*eda14cbcSMatt Macy 				cvd = vd->vdev_child[rc->rc_devidx];
2282*eda14cbcSMatt Macy 				if (rc->rc_error == 0) {
2283*eda14cbcSMatt Macy 					zio_bad_cksum_t zbc;
2284*eda14cbcSMatt Macy 					zbc.zbc_has_cksum = 0;
2285*eda14cbcSMatt Macy 					zbc.zbc_injected =
2286*eda14cbcSMatt Macy 					    rm->rm_ecksuminjected;
2287*eda14cbcSMatt Macy 
2288*eda14cbcSMatt Macy 					mutex_enter(&cvd->vdev_stat_lock);
2289*eda14cbcSMatt Macy 					cvd->vdev_stat.vs_checksum_errors++;
2290*eda14cbcSMatt Macy 					mutex_exit(&cvd->vdev_stat_lock);
2291*eda14cbcSMatt Macy 
2292*eda14cbcSMatt Macy 					zfs_ereport_start_checksum(
2293*eda14cbcSMatt Macy 					    zio->io_spa, cvd,
2294*eda14cbcSMatt Macy 					    &zio->io_bookmark, zio,
2295*eda14cbcSMatt Macy 					    rc->rc_offset, rc->rc_size,
2296*eda14cbcSMatt Macy 					    (void *)(uintptr_t)c, &zbc);
2297*eda14cbcSMatt Macy 				}
2298*eda14cbcSMatt Macy 			}
2299*eda14cbcSMatt Macy 		}
2300*eda14cbcSMatt Macy 	}
2301*eda14cbcSMatt Macy 
2302*eda14cbcSMatt Macy done:
2303*eda14cbcSMatt Macy 	zio_checksum_verified(zio);
2304*eda14cbcSMatt Macy 
2305*eda14cbcSMatt Macy 	if (zio->io_error == 0 && spa_writeable(zio->io_spa) &&
2306*eda14cbcSMatt Macy 	    (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) {
2307*eda14cbcSMatt Macy 		/*
2308*eda14cbcSMatt Macy 		 * Use the good data we have in hand to repair damaged children.
2309*eda14cbcSMatt Macy 		 */
2310*eda14cbcSMatt Macy 		for (c = 0; c < rm->rm_cols; c++) {
2311*eda14cbcSMatt Macy 			rc = &rm->rm_col[c];
2312*eda14cbcSMatt Macy 			cvd = vd->vdev_child[rc->rc_devidx];
2313*eda14cbcSMatt Macy 
2314*eda14cbcSMatt Macy 			if (rc->rc_error == 0)
2315*eda14cbcSMatt Macy 				continue;
2316*eda14cbcSMatt Macy 
2317*eda14cbcSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2318*eda14cbcSMatt Macy 			    rc->rc_offset, rc->rc_abd, rc->rc_size,
2319*eda14cbcSMatt Macy 			    ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
2320*eda14cbcSMatt Macy 			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
2321*eda14cbcSMatt Macy 			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
2322*eda14cbcSMatt Macy 		}
2323*eda14cbcSMatt Macy 	}
2324*eda14cbcSMatt Macy }
2325*eda14cbcSMatt Macy 
2326*eda14cbcSMatt Macy static void
2327*eda14cbcSMatt Macy vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded)
2328*eda14cbcSMatt Macy {
2329*eda14cbcSMatt Macy 	if (faulted > vd->vdev_nparity)
2330*eda14cbcSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2331*eda14cbcSMatt Macy 		    VDEV_AUX_NO_REPLICAS);
2332*eda14cbcSMatt Macy 	else if (degraded + faulted != 0)
2333*eda14cbcSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
2334*eda14cbcSMatt Macy 	else
2335*eda14cbcSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
2336*eda14cbcSMatt Macy }
2337*eda14cbcSMatt Macy 
2338*eda14cbcSMatt Macy /*
2339*eda14cbcSMatt Macy  * Determine if any portion of the provided block resides on a child vdev
2340*eda14cbcSMatt Macy  * with a dirty DTL and therefore needs to be resilvered.  The function
2341*eda14cbcSMatt Macy  * assumes that at least one DTL is dirty which implies that full stripe
2342*eda14cbcSMatt Macy  * width blocks must be resilvered.
2343*eda14cbcSMatt Macy  */
2344*eda14cbcSMatt Macy static boolean_t
2345*eda14cbcSMatt Macy vdev_raidz_need_resilver(vdev_t *vd, uint64_t offset, size_t psize)
2346*eda14cbcSMatt Macy {
2347*eda14cbcSMatt Macy 	uint64_t dcols = vd->vdev_children;
2348*eda14cbcSMatt Macy 	uint64_t nparity = vd->vdev_nparity;
2349*eda14cbcSMatt Macy 	uint64_t ashift = vd->vdev_top->vdev_ashift;
2350*eda14cbcSMatt Macy 	/* The starting RAIDZ (parent) vdev sector of the block. */
2351*eda14cbcSMatt Macy 	uint64_t b = offset >> ashift;
2352*eda14cbcSMatt Macy 	/* The zio's size in units of the vdev's minimum sector size. */
2353*eda14cbcSMatt Macy 	uint64_t s = ((psize - 1) >> ashift) + 1;
2354*eda14cbcSMatt Macy 	/* The first column for this stripe. */
2355*eda14cbcSMatt Macy 	uint64_t f = b % dcols;
2356*eda14cbcSMatt Macy 
2357*eda14cbcSMatt Macy 	if (s + nparity >= dcols)
2358*eda14cbcSMatt Macy 		return (B_TRUE);
2359*eda14cbcSMatt Macy 
2360*eda14cbcSMatt Macy 	for (uint64_t c = 0; c < s + nparity; c++) {
2361*eda14cbcSMatt Macy 		uint64_t devidx = (f + c) % dcols;
2362*eda14cbcSMatt Macy 		vdev_t *cvd = vd->vdev_child[devidx];
2363*eda14cbcSMatt Macy 
2364*eda14cbcSMatt Macy 		/*
2365*eda14cbcSMatt Macy 		 * dsl_scan_need_resilver() already checked vd with
2366*eda14cbcSMatt Macy 		 * vdev_dtl_contains(). So here just check cvd with
2367*eda14cbcSMatt Macy 		 * vdev_dtl_empty(), cheaper and a good approximation.
2368*eda14cbcSMatt Macy 		 */
2369*eda14cbcSMatt Macy 		if (!vdev_dtl_empty(cvd, DTL_PARTIAL))
2370*eda14cbcSMatt Macy 			return (B_TRUE);
2371*eda14cbcSMatt Macy 	}
2372*eda14cbcSMatt Macy 
2373*eda14cbcSMatt Macy 	return (B_FALSE);
2374*eda14cbcSMatt Macy }
2375*eda14cbcSMatt Macy 
2376*eda14cbcSMatt Macy static void
2377*eda14cbcSMatt Macy vdev_raidz_xlate(vdev_t *cvd, const range_seg64_t *in, range_seg64_t *res)
2378*eda14cbcSMatt Macy {
2379*eda14cbcSMatt Macy 	vdev_t *raidvd = cvd->vdev_parent;
2380*eda14cbcSMatt Macy 	ASSERT(raidvd->vdev_ops == &vdev_raidz_ops);
2381*eda14cbcSMatt Macy 
2382*eda14cbcSMatt Macy 	uint64_t width = raidvd->vdev_children;
2383*eda14cbcSMatt Macy 	uint64_t tgt_col = cvd->vdev_id;
2384*eda14cbcSMatt Macy 	uint64_t ashift = raidvd->vdev_top->vdev_ashift;
2385*eda14cbcSMatt Macy 
2386*eda14cbcSMatt Macy 	/* make sure the offsets are block-aligned */
2387*eda14cbcSMatt Macy 	ASSERT0(in->rs_start % (1 << ashift));
2388*eda14cbcSMatt Macy 	ASSERT0(in->rs_end % (1 << ashift));
2389*eda14cbcSMatt Macy 	uint64_t b_start = in->rs_start >> ashift;
2390*eda14cbcSMatt Macy 	uint64_t b_end = in->rs_end >> ashift;
2391*eda14cbcSMatt Macy 
2392*eda14cbcSMatt Macy 	uint64_t start_row = 0;
2393*eda14cbcSMatt Macy 	if (b_start > tgt_col) /* avoid underflow */
2394*eda14cbcSMatt Macy 		start_row = ((b_start - tgt_col - 1) / width) + 1;
2395*eda14cbcSMatt Macy 
2396*eda14cbcSMatt Macy 	uint64_t end_row = 0;
2397*eda14cbcSMatt Macy 	if (b_end > tgt_col)
2398*eda14cbcSMatt Macy 		end_row = ((b_end - tgt_col - 1) / width) + 1;
2399*eda14cbcSMatt Macy 
2400*eda14cbcSMatt Macy 	res->rs_start = start_row << ashift;
2401*eda14cbcSMatt Macy 	res->rs_end = end_row << ashift;
2402*eda14cbcSMatt Macy 
2403*eda14cbcSMatt Macy 	ASSERT3U(res->rs_start, <=, in->rs_start);
2404*eda14cbcSMatt Macy 	ASSERT3U(res->rs_end - res->rs_start, <=, in->rs_end - in->rs_start);
2405*eda14cbcSMatt Macy }
2406*eda14cbcSMatt Macy 
2407*eda14cbcSMatt Macy vdev_ops_t vdev_raidz_ops = {
2408*eda14cbcSMatt Macy 	.vdev_op_open = vdev_raidz_open,
2409*eda14cbcSMatt Macy 	.vdev_op_close = vdev_raidz_close,
2410*eda14cbcSMatt Macy 	.vdev_op_asize = vdev_raidz_asize,
2411*eda14cbcSMatt Macy 	.vdev_op_io_start = vdev_raidz_io_start,
2412*eda14cbcSMatt Macy 	.vdev_op_io_done = vdev_raidz_io_done,
2413*eda14cbcSMatt Macy 	.vdev_op_state_change = vdev_raidz_state_change,
2414*eda14cbcSMatt Macy 	.vdev_op_need_resilver = vdev_raidz_need_resilver,
2415*eda14cbcSMatt Macy 	.vdev_op_hold = NULL,
2416*eda14cbcSMatt Macy 	.vdev_op_rele = NULL,
2417*eda14cbcSMatt Macy 	.vdev_op_remap = NULL,
2418*eda14cbcSMatt Macy 	.vdev_op_xlate = vdev_raidz_xlate,
2419*eda14cbcSMatt Macy 	.vdev_op_type = VDEV_TYPE_RAIDZ,	/* name of this vdev type */
2420*eda14cbcSMatt Macy 	.vdev_op_leaf = B_FALSE			/* not a leaf vdev */
2421*eda14cbcSMatt Macy };
2422