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