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