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