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