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