]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_raidz.c
Fixes and enhancements of SIMD raidz parity
[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 460
c9187d86
GN
461 /* init RAIDZ parity ops */
462 rm->rm_ops = vdev_raidz_math_get_ops();
ab9f4b0b 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{
c9187d86
GN
614 /* Generate using the new math implementation */
615 if (vdev_raidz_math_generate(rm) != RAIDZ_ORIGINAL_IMPL)
ab9f4b0b 616 return;
ab9f4b0b 617
45d1cae3
BB
618 switch (rm->rm_firstdatacol) {
619 case 1:
620 vdev_raidz_generate_parity_p(rm);
621 break;
622 case 2:
623 vdev_raidz_generate_parity_pq(rm);
624 break;
625 case 3:
626 vdev_raidz_generate_parity_pqr(rm);
627 break;
628 default:
629 cmn_err(CE_PANIC, "invalid RAID-Z configuration");
630 }
631}
632
633static int
634vdev_raidz_reconstruct_p(raidz_map_t *rm, int *tgts, int ntgts)
34dc7c2f
BB
635{
636 uint64_t *dst, *src, xcount, ccount, count, i;
45d1cae3 637 int x = tgts[0];
34dc7c2f
BB
638 int c;
639
45d1cae3
BB
640 ASSERT(ntgts == 1);
641 ASSERT(x >= rm->rm_firstdatacol);
642 ASSERT(x < rm->rm_cols);
643
34dc7c2f
BB
644 xcount = rm->rm_col[x].rc_size / sizeof (src[0]);
645 ASSERT(xcount <= rm->rm_col[VDEV_RAIDZ_P].rc_size / sizeof (src[0]));
646 ASSERT(xcount > 0);
647
648 src = rm->rm_col[VDEV_RAIDZ_P].rc_data;
649 dst = rm->rm_col[x].rc_data;
650 for (i = 0; i < xcount; i++, dst++, src++) {
651 *dst = *src;
652 }
653
654 for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
655 src = rm->rm_col[c].rc_data;
656 dst = rm->rm_col[x].rc_data;
657
658 if (c == x)
659 continue;
660
661 ccount = rm->rm_col[c].rc_size / sizeof (src[0]);
662 count = MIN(ccount, xcount);
663
664 for (i = 0; i < count; i++, dst++, src++) {
665 *dst ^= *src;
666 }
667 }
45d1cae3
BB
668
669 return (1 << VDEV_RAIDZ_P);
34dc7c2f
BB
670}
671
45d1cae3
BB
672static int
673vdev_raidz_reconstruct_q(raidz_map_t *rm, int *tgts, int ntgts)
34dc7c2f
BB
674{
675 uint64_t *dst, *src, xcount, ccount, count, mask, i;
676 uint8_t *b;
45d1cae3 677 int x = tgts[0];
34dc7c2f
BB
678 int c, j, exp;
679
45d1cae3
BB
680 ASSERT(ntgts == 1);
681
34dc7c2f
BB
682 xcount = rm->rm_col[x].rc_size / sizeof (src[0]);
683 ASSERT(xcount <= rm->rm_col[VDEV_RAIDZ_Q].rc_size / sizeof (src[0]));
684
685 for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
686 src = rm->rm_col[c].rc_data;
687 dst = rm->rm_col[x].rc_data;
688
689 if (c == x)
690 ccount = 0;
691 else
692 ccount = rm->rm_col[c].rc_size / sizeof (src[0]);
693
694 count = MIN(ccount, xcount);
695
696 if (c == rm->rm_firstdatacol) {
697 for (i = 0; i < count; i++, dst++, src++) {
698 *dst = *src;
699 }
700 for (; i < xcount; i++, dst++) {
701 *dst = 0;
702 }
703
704 } else {
34dc7c2f 705 for (i = 0; i < count; i++, dst++, src++) {
45d1cae3 706 VDEV_RAIDZ_64MUL_2(*dst, mask);
34dc7c2f
BB
707 *dst ^= *src;
708 }
709
710 for (; i < xcount; i++, dst++) {
45d1cae3 711 VDEV_RAIDZ_64MUL_2(*dst, mask);
34dc7c2f
BB
712 }
713 }
714 }
715
716 src = rm->rm_col[VDEV_RAIDZ_Q].rc_data;
717 dst = rm->rm_col[x].rc_data;
718 exp = 255 - (rm->rm_cols - 1 - x);
719
720 for (i = 0; i < xcount; i++, dst++, src++) {
721 *dst ^= *src;
722 for (j = 0, b = (uint8_t *)dst; j < 8; j++, b++) {
723 *b = vdev_raidz_exp2(*b, exp);
724 }
725 }
45d1cae3
BB
726
727 return (1 << VDEV_RAIDZ_Q);
34dc7c2f
BB
728}
729
45d1cae3
BB
730static int
731vdev_raidz_reconstruct_pq(raidz_map_t *rm, int *tgts, int ntgts)
34dc7c2f
BB
732{
733 uint8_t *p, *q, *pxy, *qxy, *xd, *yd, tmp, a, b, aexp, bexp;
734 void *pdata, *qdata;
735 uint64_t xsize, ysize, i;
45d1cae3
BB
736 int x = tgts[0];
737 int y = tgts[1];
34dc7c2f 738
45d1cae3 739 ASSERT(ntgts == 2);
34dc7c2f
BB
740 ASSERT(x < y);
741 ASSERT(x >= rm->rm_firstdatacol);
742 ASSERT(y < rm->rm_cols);
743
744 ASSERT(rm->rm_col[x].rc_size >= rm->rm_col[y].rc_size);
745
746 /*
747 * Move the parity data aside -- we're going to compute parity as
748 * though columns x and y were full of zeros -- Pxy and Qxy. We want to
749 * reuse the parity generation mechanism without trashing the actual
750 * parity so we make those columns appear to be full of zeros by
751 * setting their lengths to zero.
752 */
753 pdata = rm->rm_col[VDEV_RAIDZ_P].rc_data;
754 qdata = rm->rm_col[VDEV_RAIDZ_Q].rc_data;
755 xsize = rm->rm_col[x].rc_size;
756 ysize = rm->rm_col[y].rc_size;
757
758 rm->rm_col[VDEV_RAIDZ_P].rc_data =
759 zio_buf_alloc(rm->rm_col[VDEV_RAIDZ_P].rc_size);
760 rm->rm_col[VDEV_RAIDZ_Q].rc_data =
761 zio_buf_alloc(rm->rm_col[VDEV_RAIDZ_Q].rc_size);
762 rm->rm_col[x].rc_size = 0;
763 rm->rm_col[y].rc_size = 0;
764
765 vdev_raidz_generate_parity_pq(rm);
766
767 rm->rm_col[x].rc_size = xsize;
768 rm->rm_col[y].rc_size = ysize;
769
770 p = pdata;
771 q = qdata;
772 pxy = rm->rm_col[VDEV_RAIDZ_P].rc_data;
773 qxy = rm->rm_col[VDEV_RAIDZ_Q].rc_data;
774 xd = rm->rm_col[x].rc_data;
775 yd = rm->rm_col[y].rc_data;
776
777 /*
778 * We now have:
779 * Pxy = P + D_x + D_y
780 * Qxy = Q + 2^(ndevs - 1 - x) * D_x + 2^(ndevs - 1 - y) * D_y
781 *
782 * We can then solve for D_x:
783 * D_x = A * (P + Pxy) + B * (Q + Qxy)
784 * where
785 * A = 2^(x - y) * (2^(x - y) + 1)^-1
786 * B = 2^(ndevs - 1 - x) * (2^(x - y) + 1)^-1
787 *
788 * With D_x in hand, we can easily solve for D_y:
789 * D_y = P + Pxy + D_x
790 */
791
792 a = vdev_raidz_pow2[255 + x - y];
793 b = vdev_raidz_pow2[255 - (rm->rm_cols - 1 - x)];
794 tmp = 255 - vdev_raidz_log2[a ^ 1];
795
796 aexp = vdev_raidz_log2[vdev_raidz_exp2(a, tmp)];
797 bexp = vdev_raidz_log2[vdev_raidz_exp2(b, tmp)];
798
799 for (i = 0; i < xsize; i++, p++, q++, pxy++, qxy++, xd++, yd++) {
800 *xd = vdev_raidz_exp2(*p ^ *pxy, aexp) ^
801 vdev_raidz_exp2(*q ^ *qxy, bexp);
802
803 if (i < ysize)
804 *yd = *p ^ *pxy ^ *xd;
805 }
806
807 zio_buf_free(rm->rm_col[VDEV_RAIDZ_P].rc_data,
808 rm->rm_col[VDEV_RAIDZ_P].rc_size);
809 zio_buf_free(rm->rm_col[VDEV_RAIDZ_Q].rc_data,
810 rm->rm_col[VDEV_RAIDZ_Q].rc_size);
811
812 /*
813 * Restore the saved parity data.
814 */
815 rm->rm_col[VDEV_RAIDZ_P].rc_data = pdata;
816 rm->rm_col[VDEV_RAIDZ_Q].rc_data = qdata;
45d1cae3
BB
817
818 return ((1 << VDEV_RAIDZ_P) | (1 << VDEV_RAIDZ_Q));
819}
820
821/* BEGIN CSTYLED */
822/*
823 * In the general case of reconstruction, we must solve the system of linear
824 * equations defined by the coeffecients used to generate parity as well as
825 * the contents of the data and parity disks. This can be expressed with
826 * vectors for the original data (D) and the actual data (d) and parity (p)
827 * and a matrix composed of the identity matrix (I) and a dispersal matrix (V):
828 *
829 * __ __ __ __
830 * | | __ __ | p_0 |
831 * | V | | D_0 | | p_m-1 |
832 * | | x | : | = | d_0 |
833 * | I | | D_n-1 | | : |
834 * | | ~~ ~~ | d_n-1 |
835 * ~~ ~~ ~~ ~~
836 *
837 * I is simply a square identity matrix of size n, and V is a vandermonde
838 * matrix defined by the coeffecients we chose for the various parity columns
839 * (1, 2, 4). Note that these values were chosen both for simplicity, speedy
840 * computation as well as linear separability.
841 *
842 * __ __ __ __
843 * | 1 .. 1 1 1 | | p_0 |
844 * | 2^n-1 .. 4 2 1 | __ __ | : |
845 * | 4^n-1 .. 16 4 1 | | D_0 | | p_m-1 |
846 * | 1 .. 0 0 0 | | D_1 | | d_0 |
847 * | 0 .. 0 0 0 | x | D_2 | = | d_1 |
848 * | : : : : | | : | | d_2 |
849 * | 0 .. 1 0 0 | | D_n-1 | | : |
850 * | 0 .. 0 1 0 | ~~ ~~ | : |
851 * | 0 .. 0 0 1 | | d_n-1 |
852 * ~~ ~~ ~~ ~~
853 *
854 * Note that I, V, d, and p are known. To compute D, we must invert the
855 * matrix and use the known data and parity values to reconstruct the unknown
856 * data values. We begin by removing the rows in V|I and d|p that correspond
857 * to failed or missing columns; we then make V|I square (n x n) and d|p
858 * sized n by removing rows corresponding to unused parity from the bottom up
859 * to generate (V|I)' and (d|p)'. We can then generate the inverse of (V|I)'
860 * using Gauss-Jordan elimination. In the example below we use m=3 parity
861 * columns, n=8 data columns, with errors in d_1, d_2, and p_1:
862 * __ __
863 * | 1 1 1 1 1 1 1 1 |
864 * | 128 64 32 16 8 4 2 1 | <-----+-+-- missing disks
865 * | 19 205 116 29 64 16 4 1 | / /
866 * | 1 0 0 0 0 0 0 0 | / /
867 * | 0 1 0 0 0 0 0 0 | <--' /
868 * (V|I) = | 0 0 1 0 0 0 0 0 | <---'
869 * | 0 0 0 1 0 0 0 0 |
870 * | 0 0 0 0 1 0 0 0 |
871 * | 0 0 0 0 0 1 0 0 |
872 * | 0 0 0 0 0 0 1 0 |
873 * | 0 0 0 0 0 0 0 1 |
874 * ~~ ~~
875 * __ __
876 * | 1 1 1 1 1 1 1 1 |
877 * | 128 64 32 16 8 4 2 1 |
878 * | 19 205 116 29 64 16 4 1 |
879 * | 1 0 0 0 0 0 0 0 |
880 * | 0 1 0 0 0 0 0 0 |
881 * (V|I)' = | 0 0 1 0 0 0 0 0 |
882 * | 0 0 0 1 0 0 0 0 |
883 * | 0 0 0 0 1 0 0 0 |
884 * | 0 0 0 0 0 1 0 0 |
885 * | 0 0 0 0 0 0 1 0 |
886 * | 0 0 0 0 0 0 0 1 |
887 * ~~ ~~
888 *
889 * Here we employ Gauss-Jordan elimination to find the inverse of (V|I)'. We
890 * have carefully chosen the seed values 1, 2, and 4 to ensure that this
891 * matrix is not singular.
892 * __ __
893 * | 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 |
894 * | 19 205 116 29 64 16 4 1 0 1 0 0 0 0 0 0 |
895 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
896 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
897 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
898 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
899 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
900 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
901 * ~~ ~~
902 * __ __
903 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
904 * | 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 |
905 * | 19 205 116 29 64 16 4 1 0 1 0 0 0 0 0 0 |
906 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
907 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
908 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
909 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
910 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
911 * ~~ ~~
912 * __ __
913 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
914 * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 |
915 * | 0 205 116 0 0 0 0 0 0 1 19 29 64 16 4 1 |
916 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
917 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
918 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
919 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
920 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
921 * ~~ ~~
922 * __ __
923 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
924 * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 |
925 * | 0 0 185 0 0 0 0 0 205 1 222 208 141 221 201 204 |
926 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
927 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
928 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
929 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
930 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
931 * ~~ ~~
932 * __ __
933 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
934 * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 |
935 * | 0 0 1 0 0 0 0 0 166 100 4 40 158 168 216 209 |
936 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
937 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
938 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
939 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
940 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
941 * ~~ ~~
942 * __ __
943 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
944 * | 0 1 0 0 0 0 0 0 167 100 5 41 159 169 217 208 |
945 * | 0 0 1 0 0 0 0 0 166 100 4 40 158 168 216 209 |
946 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
947 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
948 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
949 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
950 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
951 * ~~ ~~
952 * __ __
953 * | 0 0 1 0 0 0 0 0 |
954 * | 167 100 5 41 159 169 217 208 |
955 * | 166 100 4 40 158 168 216 209 |
956 * (V|I)'^-1 = | 0 0 0 1 0 0 0 0 |
957 * | 0 0 0 0 1 0 0 0 |
958 * | 0 0 0 0 0 1 0 0 |
959 * | 0 0 0 0 0 0 1 0 |
960 * | 0 0 0 0 0 0 0 1 |
961 * ~~ ~~
962 *
963 * We can then simply compute D = (V|I)'^-1 x (d|p)' to discover the values
964 * of the missing data.
965 *
966 * As is apparent from the example above, the only non-trivial rows in the
967 * inverse matrix correspond to the data disks that we're trying to
968 * reconstruct. Indeed, those are the only rows we need as the others would
969 * only be useful for reconstructing data known or assumed to be valid. For
970 * that reason, we only build the coefficients in the rows that correspond to
971 * targeted columns.
972 */
973/* END CSTYLED */
974
975static void
976vdev_raidz_matrix_init(raidz_map_t *rm, int n, int nmap, int *map,
977 uint8_t **rows)
978{
979 int i, j;
980 int pow;
981
982 ASSERT(n == rm->rm_cols - rm->rm_firstdatacol);
983
984 /*
985 * Fill in the missing rows of interest.
986 */
987 for (i = 0; i < nmap; i++) {
988 ASSERT3S(0, <=, map[i]);
989 ASSERT3S(map[i], <=, 2);
990
991 pow = map[i] * n;
992 if (pow > 255)
993 pow -= 255;
994 ASSERT(pow <= 255);
995
996 for (j = 0; j < n; j++) {
997 pow -= map[i];
998 if (pow < 0)
999 pow += 255;
1000 rows[i][j] = vdev_raidz_pow2[pow];
1001 }
1002 }
1003}
1004
1005static void
1006vdev_raidz_matrix_invert(raidz_map_t *rm, int n, int nmissing, int *missing,
1007 uint8_t **rows, uint8_t **invrows, const uint8_t *used)
1008{
1009 int i, j, ii, jj;
1010 uint8_t log;
1011
1012 /*
1013 * Assert that the first nmissing entries from the array of used
1014 * columns correspond to parity columns and that subsequent entries
1015 * correspond to data columns.
1016 */
1017 for (i = 0; i < nmissing; i++) {
1018 ASSERT3S(used[i], <, rm->rm_firstdatacol);
1019 }
1020 for (; i < n; i++) {
1021 ASSERT3S(used[i], >=, rm->rm_firstdatacol);
1022 }
1023
1024 /*
1025 * First initialize the storage where we'll compute the inverse rows.
1026 */
1027 for (i = 0; i < nmissing; i++) {
1028 for (j = 0; j < n; j++) {
1029 invrows[i][j] = (i == j) ? 1 : 0;
1030 }
1031 }
1032
1033 /*
1034 * Subtract all trivial rows from the rows of consequence.
1035 */
1036 for (i = 0; i < nmissing; i++) {
1037 for (j = nmissing; j < n; j++) {
1038 ASSERT3U(used[j], >=, rm->rm_firstdatacol);
1039 jj = used[j] - rm->rm_firstdatacol;
1040 ASSERT3S(jj, <, n);
1041 invrows[i][j] = rows[i][jj];
1042 rows[i][jj] = 0;
1043 }
1044 }
1045
1046 /*
1047 * For each of the rows of interest, we must normalize it and subtract
1048 * a multiple of it from the other rows.
1049 */
1050 for (i = 0; i < nmissing; i++) {
1051 for (j = 0; j < missing[i]; j++) {
c99c9001 1052 ASSERT0(rows[i][j]);
45d1cae3
BB
1053 }
1054 ASSERT3U(rows[i][missing[i]], !=, 0);
1055
1056 /*
1057 * Compute the inverse of the first element and multiply each
1058 * element in the row by that value.
1059 */
1060 log = 255 - vdev_raidz_log2[rows[i][missing[i]]];
1061
1062 for (j = 0; j < n; j++) {
1063 rows[i][j] = vdev_raidz_exp2(rows[i][j], log);
1064 invrows[i][j] = vdev_raidz_exp2(invrows[i][j], log);
1065 }
1066
1067 for (ii = 0; ii < nmissing; ii++) {
1068 if (i == ii)
1069 continue;
1070
1071 ASSERT3U(rows[ii][missing[i]], !=, 0);
1072
1073 log = vdev_raidz_log2[rows[ii][missing[i]]];
1074
1075 for (j = 0; j < n; j++) {
1076 rows[ii][j] ^=
1077 vdev_raidz_exp2(rows[i][j], log);
1078 invrows[ii][j] ^=
1079 vdev_raidz_exp2(invrows[i][j], log);
1080 }
1081 }
1082 }
1083
1084 /*
1085 * Verify that the data that is left in the rows are properly part of
1086 * an identity matrix.
1087 */
1088 for (i = 0; i < nmissing; i++) {
1089 for (j = 0; j < n; j++) {
1090 if (j == missing[i]) {
1091 ASSERT3U(rows[i][j], ==, 1);
1092 } else {
c99c9001 1093 ASSERT0(rows[i][j]);
45d1cae3
BB
1094 }
1095 }
1096 }
1097}
1098
1099static void
1100vdev_raidz_matrix_reconstruct(raidz_map_t *rm, int n, int nmissing,
1101 int *missing, uint8_t **invrows, const uint8_t *used)
1102{
1103 int i, j, x, cc, c;
1104 uint8_t *src;
1105 uint64_t ccount;
1106 uint8_t *dst[VDEV_RAIDZ_MAXPARITY];
1107 uint64_t dcount[VDEV_RAIDZ_MAXPARITY];
a117a6d6
GW
1108 uint8_t log = 0;
1109 uint8_t val;
45d1cae3
BB
1110 int ll;
1111 uint8_t *invlog[VDEV_RAIDZ_MAXPARITY];
1112 uint8_t *p, *pp;
1113 size_t psize;
1114
1115 psize = sizeof (invlog[0][0]) * n * nmissing;
79c76d5b 1116 p = kmem_alloc(psize, KM_SLEEP);
45d1cae3
BB
1117
1118 for (pp = p, i = 0; i < nmissing; i++) {
1119 invlog[i] = pp;
1120 pp += n;
1121 }
1122
1123 for (i = 0; i < nmissing; i++) {
1124 for (j = 0; j < n; j++) {
1125 ASSERT3U(invrows[i][j], !=, 0);
1126 invlog[i][j] = vdev_raidz_log2[invrows[i][j]];
1127 }
1128 }
1129
1130 for (i = 0; i < n; i++) {
1131 c = used[i];
1132 ASSERT3U(c, <, rm->rm_cols);
1133
1134 src = rm->rm_col[c].rc_data;
1135 ccount = rm->rm_col[c].rc_size;
1136 for (j = 0; j < nmissing; j++) {
1137 cc = missing[j] + rm->rm_firstdatacol;
1138 ASSERT3U(cc, >=, rm->rm_firstdatacol);
1139 ASSERT3U(cc, <, rm->rm_cols);
1140 ASSERT3U(cc, !=, c);
1141
1142 dst[j] = rm->rm_col[cc].rc_data;
1143 dcount[j] = rm->rm_col[cc].rc_size;
1144 }
1145
1146 ASSERT(ccount >= rm->rm_col[missing[0]].rc_size || i > 0);
1147
1148 for (x = 0; x < ccount; x++, src++) {
1149 if (*src != 0)
1150 log = vdev_raidz_log2[*src];
1151
1152 for (cc = 0; cc < nmissing; cc++) {
1153 if (x >= dcount[cc])
1154 continue;
1155
1156 if (*src == 0) {
1157 val = 0;
1158 } else {
1159 if ((ll = log + invlog[cc][i]) >= 255)
1160 ll -= 255;
1161 val = vdev_raidz_pow2[ll];
1162 }
1163
1164 if (i == 0)
1165 dst[cc][x] = val;
1166 else
1167 dst[cc][x] ^= val;
1168 }
1169 }
1170 }
1171
1172 kmem_free(p, psize);
1173}
1174
1175static int
1176vdev_raidz_reconstruct_general(raidz_map_t *rm, int *tgts, int ntgts)
1177{
1178 int n, i, c, t, tt;
1179 int nmissing_rows;
1180 int missing_rows[VDEV_RAIDZ_MAXPARITY];
1181 int parity_map[VDEV_RAIDZ_MAXPARITY];
1182
1183 uint8_t *p, *pp;
1184 size_t psize;
1185
1186 uint8_t *rows[VDEV_RAIDZ_MAXPARITY];
1187 uint8_t *invrows[VDEV_RAIDZ_MAXPARITY];
1188 uint8_t *used;
1189
1190 int code = 0;
1191
1192
1193 n = rm->rm_cols - rm->rm_firstdatacol;
1194
1195 /*
1196 * Figure out which data columns are missing.
1197 */
1198 nmissing_rows = 0;
1199 for (t = 0; t < ntgts; t++) {
1200 if (tgts[t] >= rm->rm_firstdatacol) {
1201 missing_rows[nmissing_rows++] =
1202 tgts[t] - rm->rm_firstdatacol;
1203 }
1204 }
1205
1206 /*
1207 * Figure out which parity columns to use to help generate the missing
1208 * data columns.
1209 */
1210 for (tt = 0, c = 0, i = 0; i < nmissing_rows; c++) {
1211 ASSERT(tt < ntgts);
1212 ASSERT(c < rm->rm_firstdatacol);
1213
1214 /*
1215 * Skip any targeted parity columns.
1216 */
1217 if (c == tgts[tt]) {
1218 tt++;
1219 continue;
1220 }
1221
1222 code |= 1 << c;
1223
1224 parity_map[i] = c;
1225 i++;
1226 }
1227
1228 ASSERT(code != 0);
1229 ASSERT3U(code, <, 1 << VDEV_RAIDZ_MAXPARITY);
1230
1231 psize = (sizeof (rows[0][0]) + sizeof (invrows[0][0])) *
1232 nmissing_rows * n + sizeof (used[0]) * n;
79c76d5b 1233 p = kmem_alloc(psize, KM_SLEEP);
45d1cae3
BB
1234
1235 for (pp = p, i = 0; i < nmissing_rows; i++) {
1236 rows[i] = pp;
1237 pp += n;
1238 invrows[i] = pp;
1239 pp += n;
1240 }
1241 used = pp;
1242
1243 for (i = 0; i < nmissing_rows; i++) {
1244 used[i] = parity_map[i];
1245 }
1246
1247 for (tt = 0, c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
1248 if (tt < nmissing_rows &&
1249 c == missing_rows[tt] + rm->rm_firstdatacol) {
1250 tt++;
1251 continue;
1252 }
1253
1254 ASSERT3S(i, <, n);
1255 used[i] = c;
1256 i++;
1257 }
1258
1259 /*
1260 * Initialize the interesting rows of the matrix.
1261 */
1262 vdev_raidz_matrix_init(rm, n, nmissing_rows, parity_map, rows);
1263
1264 /*
1265 * Invert the matrix.
1266 */
1267 vdev_raidz_matrix_invert(rm, n, nmissing_rows, missing_rows, rows,
1268 invrows, used);
1269
1270 /*
1271 * Reconstruct the missing data using the generated matrix.
1272 */
1273 vdev_raidz_matrix_reconstruct(rm, n, nmissing_rows, missing_rows,
1274 invrows, used);
1275
1276 kmem_free(p, psize);
1277
1278 return (code);
34dc7c2f
BB
1279}
1280
ab9f4b0b
GN
1281int
1282vdev_raidz_reconstruct(raidz_map_t *rm, const int *t, int nt)
45d1cae3
BB
1283{
1284 int tgts[VDEV_RAIDZ_MAXPARITY], *dt;
1285 int ntgts;
c9187d86 1286 int i, c, ret;
45d1cae3
BB
1287 int code;
1288 int nbadparity, nbaddata;
1289 int parity_valid[VDEV_RAIDZ_MAXPARITY];
1290
1291 /*
1292 * The tgts list must already be sorted.
1293 */
1294 for (i = 1; i < nt; i++) {
1295 ASSERT(t[i] > t[i - 1]);
1296 }
1297
1298 nbadparity = rm->rm_firstdatacol;
1299 nbaddata = rm->rm_cols - nbadparity;
1300 ntgts = 0;
1301 for (i = 0, c = 0; c < rm->rm_cols; c++) {
1302 if (c < rm->rm_firstdatacol)
1303 parity_valid[c] = B_FALSE;
1304
1305 if (i < nt && c == t[i]) {
1306 tgts[ntgts++] = c;
1307 i++;
1308 } else if (rm->rm_col[c].rc_error != 0) {
1309 tgts[ntgts++] = c;
1310 } else if (c >= rm->rm_firstdatacol) {
1311 nbaddata--;
1312 } else {
1313 parity_valid[c] = B_TRUE;
1314 nbadparity--;
1315 }
1316 }
1317
1318 ASSERT(ntgts >= nt);
1319 ASSERT(nbaddata >= 0);
1320 ASSERT(nbaddata + nbadparity == ntgts);
1321
1322 dt = &tgts[nbadparity];
1323
c9187d86
GN
1324
1325 /* Reconstruct using the new math implementation */
1326 ret = vdev_raidz_math_reconstruct(rm, parity_valid, dt, nbaddata);
1327 if (ret != RAIDZ_ORIGINAL_IMPL)
1328 return (ret);
ab9f4b0b 1329
45d1cae3
BB
1330 /*
1331 * See if we can use any of our optimized reconstruction routines.
1332 */
ab9f4b0b
GN
1333 switch (nbaddata) {
1334 case 1:
1335 if (parity_valid[VDEV_RAIDZ_P])
1336 return (vdev_raidz_reconstruct_p(rm, dt, 1));
45d1cae3 1337
ab9f4b0b 1338 ASSERT(rm->rm_firstdatacol > 1);
45d1cae3 1339
ab9f4b0b
GN
1340 if (parity_valid[VDEV_RAIDZ_Q])
1341 return (vdev_raidz_reconstruct_q(rm, dt, 1));
45d1cae3 1342
ab9f4b0b
GN
1343 ASSERT(rm->rm_firstdatacol > 2);
1344 break;
45d1cae3 1345
ab9f4b0b
GN
1346 case 2:
1347 ASSERT(rm->rm_firstdatacol > 1);
45d1cae3 1348
ab9f4b0b
GN
1349 if (parity_valid[VDEV_RAIDZ_P] &&
1350 parity_valid[VDEV_RAIDZ_Q])
1351 return (vdev_raidz_reconstruct_pq(rm, dt, 2));
45d1cae3 1352
ab9f4b0b 1353 ASSERT(rm->rm_firstdatacol > 2);
45d1cae3 1354
ab9f4b0b 1355 break;
45d1cae3
BB
1356 }
1357
1358 code = vdev_raidz_reconstruct_general(rm, tgts, ntgts);
1359 ASSERT(code < (1 << VDEV_RAIDZ_MAXPARITY));
1360 ASSERT(code > 0);
1361 return (code);
1362}
34dc7c2f
BB
1363
1364static int
1bd201e7
CS
1365vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
1366 uint64_t *ashift)
34dc7c2f
BB
1367{
1368 vdev_t *cvd;
1369 uint64_t nparity = vd->vdev_nparity;
45d1cae3 1370 int c;
34dc7c2f
BB
1371 int lasterror = 0;
1372 int numerrors = 0;
1373
1374 ASSERT(nparity > 0);
1375
1376 if (nparity > VDEV_RAIDZ_MAXPARITY ||
1377 vd->vdev_children < nparity + 1) {
1378 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
2e528b49 1379 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1380 }
1381
45d1cae3
BB
1382 vdev_open_children(vd);
1383
34dc7c2f
BB
1384 for (c = 0; c < vd->vdev_children; c++) {
1385 cvd = vd->vdev_child[c];
1386
45d1cae3
BB
1387 if (cvd->vdev_open_error != 0) {
1388 lasterror = cvd->vdev_open_error;
34dc7c2f
BB
1389 numerrors++;
1390 continue;
1391 }
1392
1393 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
1bd201e7 1394 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
34dc7c2f
BB
1395 *ashift = MAX(*ashift, cvd->vdev_ashift);
1396 }
1397
1398 *asize *= vd->vdev_children;
1bd201e7 1399 *max_asize *= vd->vdev_children;
34dc7c2f
BB
1400
1401 if (numerrors > nparity) {
1402 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
1403 return (lasterror);
1404 }
1405
1406 return (0);
1407}
1408
1409static void
1410vdev_raidz_close(vdev_t *vd)
1411{
1412 int c;
1413
1414 for (c = 0; c < vd->vdev_children; c++)
1415 vdev_close(vd->vdev_child[c]);
1416}
1417
1418static uint64_t
1419vdev_raidz_asize(vdev_t *vd, uint64_t psize)
1420{
1421 uint64_t asize;
1422 uint64_t ashift = vd->vdev_top->vdev_ashift;
1423 uint64_t cols = vd->vdev_children;
1424 uint64_t nparity = vd->vdev_nparity;
1425
1426 asize = ((psize - 1) >> ashift) + 1;
1427 asize += nparity * ((asize + cols - nparity - 1) / (cols - nparity));
1428 asize = roundup(asize, nparity + 1) << ashift;
1429
1430 return (asize);
1431}
1432
1433static void
1434vdev_raidz_child_done(zio_t *zio)
1435{
1436 raidz_col_t *rc = zio->io_private;
1437
1438 rc->rc_error = zio->io_error;
1439 rc->rc_tried = 1;
1440 rc->rc_skipped = 0;
1441}
1442
e49f1e20
WA
1443/*
1444 * Start an IO operation on a RAIDZ VDev
1445 *
1446 * Outline:
1447 * - For write operations:
1448 * 1. Generate the parity data
1449 * 2. Create child zio write operations to each column's vdev, for both
1450 * data and parity.
1451 * 3. If the column skips any sectors for padding, create optional dummy
1452 * write zio children for those areas to improve aggregation continuity.
1453 * - For read operations:
1454 * 1. Create child zio read operations to each data column's vdev to read
1455 * the range of data required for zio.
1456 * 2. If this is a scrub or resilver operation, or if any of the data
1457 * vdevs have had errors, then create zio read operations to the parity
1458 * columns' VDevs as well.
1459 */
98b25418 1460static void
34dc7c2f
BB
1461vdev_raidz_io_start(zio_t *zio)
1462{
1463 vdev_t *vd = zio->io_vd;
1464 vdev_t *tvd = vd->vdev_top;
1465 vdev_t *cvd;
34dc7c2f
BB
1466 raidz_map_t *rm;
1467 raidz_col_t *rc;
45d1cae3 1468 int c, i;
34dc7c2f
BB
1469
1470 rm = vdev_raidz_map_alloc(zio, tvd->vdev_ashift, vd->vdev_children,
1471 vd->vdev_nparity);
1472
1473 ASSERT3U(rm->rm_asize, ==, vdev_psize_to_asize(vd, zio->io_size));
1474
1475 if (zio->io_type == ZIO_TYPE_WRITE) {
45d1cae3 1476 vdev_raidz_generate_parity(rm);
34dc7c2f
BB
1477
1478 for (c = 0; c < rm->rm_cols; c++) {
1479 rc = &rm->rm_col[c];
1480 cvd = vd->vdev_child[rc->rc_devidx];
1481 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
1482 rc->rc_offset, rc->rc_data, rc->rc_size,
b128c09f 1483 zio->io_type, zio->io_priority, 0,
34dc7c2f
BB
1484 vdev_raidz_child_done, rc));
1485 }
1486
45d1cae3
BB
1487 /*
1488 * Generate optional I/Os for any skipped sectors to improve
1489 * aggregation contiguity.
1490 */
428870ff 1491 for (c = rm->rm_skipstart, i = 0; i < rm->rm_nskip; c++, i++) {
45d1cae3
BB
1492 ASSERT(c <= rm->rm_scols);
1493 if (c == rm->rm_scols)
1494 c = 0;
1495 rc = &rm->rm_col[c];
1496 cvd = vd->vdev_child[rc->rc_devidx];
1497 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
1498 rc->rc_offset + rc->rc_size, NULL,
1499 1 << tvd->vdev_ashift,
1500 zio->io_type, zio->io_priority,
1501 ZIO_FLAG_NODATA | ZIO_FLAG_OPTIONAL, NULL, NULL));
1502 }
1503
98b25418
GW
1504 zio_execute(zio);
1505 return;
34dc7c2f
BB
1506 }
1507
1508 ASSERT(zio->io_type == ZIO_TYPE_READ);
1509
1510 /*
1511 * Iterate over the columns in reverse order so that we hit the parity
45d1cae3 1512 * last -- any errors along the way will force us to read the parity.
34dc7c2f
BB
1513 */
1514 for (c = rm->rm_cols - 1; c >= 0; c--) {
1515 rc = &rm->rm_col[c];
1516 cvd = vd->vdev_child[rc->rc_devidx];
1517 if (!vdev_readable(cvd)) {
1518 if (c >= rm->rm_firstdatacol)
1519 rm->rm_missingdata++;
1520 else
1521 rm->rm_missingparity++;
2e528b49 1522 rc->rc_error = SET_ERROR(ENXIO);
34dc7c2f
BB
1523 rc->rc_tried = 1; /* don't even try */
1524 rc->rc_skipped = 1;
1525 continue;
1526 }
428870ff 1527 if (vdev_dtl_contains(cvd, DTL_MISSING, zio->io_txg, 1)) {
34dc7c2f
BB
1528 if (c >= rm->rm_firstdatacol)
1529 rm->rm_missingdata++;
1530 else
1531 rm->rm_missingparity++;
2e528b49 1532 rc->rc_error = SET_ERROR(ESTALE);
34dc7c2f
BB
1533 rc->rc_skipped = 1;
1534 continue;
1535 }
1536 if (c >= rm->rm_firstdatacol || rm->rm_missingdata > 0 ||
9babb374 1537 (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
34dc7c2f
BB
1538 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
1539 rc->rc_offset, rc->rc_data, rc->rc_size,
b128c09f 1540 zio->io_type, zio->io_priority, 0,
34dc7c2f
BB
1541 vdev_raidz_child_done, rc));
1542 }
1543 }
1544
98b25418 1545 zio_execute(zio);
34dc7c2f
BB
1546}
1547
428870ff 1548
34dc7c2f
BB
1549/*
1550 * Report a checksum error for a child of a RAID-Z device.
1551 */
1552static void
428870ff 1553raidz_checksum_error(zio_t *zio, raidz_col_t *rc, void *bad_data)
34dc7c2f
BB
1554{
1555 vdev_t *vd = zio->io_vd->vdev_child[rc->rc_devidx];
34dc7c2f
BB
1556
1557 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
428870ff
BB
1558 zio_bad_cksum_t zbc;
1559 raidz_map_t *rm = zio->io_vsd;
1560
34dc7c2f
BB
1561 mutex_enter(&vd->vdev_stat_lock);
1562 vd->vdev_stat.vs_checksum_errors++;
1563 mutex_exit(&vd->vdev_stat_lock);
428870ff
BB
1564
1565 zbc.zbc_has_cksum = 0;
1566 zbc.zbc_injected = rm->rm_ecksuminjected;
1567
1568 zfs_ereport_post_checksum(zio->io_spa, vd, zio,
1569 rc->rc_offset, rc->rc_size, rc->rc_data, bad_data,
1570 &zbc);
34dc7c2f 1571 }
428870ff
BB
1572}
1573
1574/*
1575 * We keep track of whether or not there were any injected errors, so that
1576 * any ereports we generate can note it.
1577 */
1578static int
1579raidz_checksum_verify(zio_t *zio)
1580{
1581 zio_bad_cksum_t zbc;
1582 raidz_map_t *rm = zio->io_vsd;
d4ed6673 1583 int ret;
428870ff 1584
d4ed6673
BB
1585 bzero(&zbc, sizeof (zio_bad_cksum_t));
1586
1587 ret = zio_checksum_error(zio, &zbc);
428870ff
BB
1588 if (ret != 0 && zbc.zbc_injected != 0)
1589 rm->rm_ecksuminjected = 1;
34dc7c2f 1590
428870ff 1591 return (ret);
34dc7c2f
BB
1592}
1593
1594/*
1595 * Generate the parity from the data columns. If we tried and were able to
1596 * read the parity without error, verify that the generated parity matches the
1597 * data we read. If it doesn't, we fire off a checksum error. Return the
1598 * number such failures.
1599 */
1600static int
1601raidz_parity_verify(zio_t *zio, raidz_map_t *rm)
1602{
1603 void *orig[VDEV_RAIDZ_MAXPARITY];
1604 int c, ret = 0;
1605 raidz_col_t *rc;
1606
1607 for (c = 0; c < rm->rm_firstdatacol; c++) {
1608 rc = &rm->rm_col[c];
1609 if (!rc->rc_tried || rc->rc_error != 0)
1610 continue;
1611 orig[c] = zio_buf_alloc(rc->rc_size);
1612 bcopy(rc->rc_data, orig[c], rc->rc_size);
1613 }
1614
45d1cae3 1615 vdev_raidz_generate_parity(rm);
34dc7c2f
BB
1616
1617 for (c = 0; c < rm->rm_firstdatacol; c++) {
1618 rc = &rm->rm_col[c];
1619 if (!rc->rc_tried || rc->rc_error != 0)
1620 continue;
1621 if (bcmp(orig[c], rc->rc_data, rc->rc_size) != 0) {
428870ff 1622 raidz_checksum_error(zio, rc, orig[c]);
2e528b49 1623 rc->rc_error = SET_ERROR(ECKSUM);
34dc7c2f
BB
1624 ret++;
1625 }
1626 zio_buf_free(orig[c], rc->rc_size);
1627 }
1628
1629 return (ret);
1630}
1631
34dc7c2f 1632static int
b128c09f
BB
1633vdev_raidz_worst_error(raidz_map_t *rm)
1634{
d6320ddb 1635 int c, error = 0;
b128c09f 1636
d6320ddb 1637 for (c = 0; c < rm->rm_cols; c++)
b128c09f
BB
1638 error = zio_worst_error(error, rm->rm_col[c].rc_error);
1639
1640 return (error);
1641}
1642
45d1cae3
BB
1643/*
1644 * Iterate over all combinations of bad data and attempt a reconstruction.
1645 * Note that the algorithm below is non-optimal because it doesn't take into
1646 * account how reconstruction is actually performed. For example, with
1647 * triple-parity RAID-Z the reconstruction procedure is the same if column 4
1648 * is targeted as invalid as if columns 1 and 4 are targeted since in both
1649 * cases we'd only use parity information in column 0.
1650 */
1651static int
1652vdev_raidz_combrec(zio_t *zio, int total_errors, int data_errors)
1653{
1654 raidz_map_t *rm = zio->io_vsd;
1655 raidz_col_t *rc;
1656 void *orig[VDEV_RAIDZ_MAXPARITY];
1657 int tstore[VDEV_RAIDZ_MAXPARITY + 2];
1658 int *tgts = &tstore[1];
5631c038 1659 int curr, next, i, c, n;
45d1cae3
BB
1660 int code, ret = 0;
1661
1662 ASSERT(total_errors < rm->rm_firstdatacol);
1663
1664 /*
1665 * This simplifies one edge condition.
1666 */
1667 tgts[-1] = -1;
1668
1669 for (n = 1; n <= rm->rm_firstdatacol - total_errors; n++) {
1670 /*
1671 * Initialize the targets array by finding the first n columns
1672 * that contain no error.
1673 *
1674 * If there were no data errors, we need to ensure that we're
1675 * always explicitly attempting to reconstruct at least one
1676 * data column. To do this, we simply push the highest target
1677 * up into the data columns.
1678 */
1679 for (c = 0, i = 0; i < n; i++) {
1680 if (i == n - 1 && data_errors == 0 &&
1681 c < rm->rm_firstdatacol) {
1682 c = rm->rm_firstdatacol;
1683 }
1684
1685 while (rm->rm_col[c].rc_error != 0) {
1686 c++;
1687 ASSERT3S(c, <, rm->rm_cols);
1688 }
1689
1690 tgts[i] = c++;
1691 }
1692
1693 /*
1694 * Setting tgts[n] simplifies the other edge condition.
1695 */
1696 tgts[n] = rm->rm_cols;
1697
1698 /*
1699 * These buffers were allocated in previous iterations.
1700 */
1701 for (i = 0; i < n - 1; i++) {
1702 ASSERT(orig[i] != NULL);
1703 }
1704
1705 orig[n - 1] = zio_buf_alloc(rm->rm_col[0].rc_size);
1706
5631c038
BB
1707 curr = 0;
1708 next = tgts[curr];
45d1cae3 1709
5631c038
BB
1710 while (curr != n) {
1711 tgts[curr] = next;
1712 curr = 0;
45d1cae3
BB
1713
1714 /*
1715 * Save off the original data that we're going to
1716 * attempt to reconstruct.
1717 */
1718 for (i = 0; i < n; i++) {
1719 ASSERT(orig[i] != NULL);
1720 c = tgts[i];
1721 ASSERT3S(c, >=, 0);
1722 ASSERT3S(c, <, rm->rm_cols);
1723 rc = &rm->rm_col[c];
1724 bcopy(rc->rc_data, orig[i], rc->rc_size);
1725 }
1726
1727 /*
1728 * Attempt a reconstruction and exit the outer loop on
1729 * success.
1730 */
1731 code = vdev_raidz_reconstruct(rm, tgts, n);
428870ff 1732 if (raidz_checksum_verify(zio) == 0) {
45d1cae3
BB
1733
1734 for (i = 0; i < n; i++) {
1735 c = tgts[i];
1736 rc = &rm->rm_col[c];
1737 ASSERT(rc->rc_error == 0);
1738 if (rc->rc_tried)
428870ff
BB
1739 raidz_checksum_error(zio, rc,
1740 orig[i]);
2e528b49 1741 rc->rc_error = SET_ERROR(ECKSUM);
45d1cae3
BB
1742 }
1743
1744 ret = code;
1745 goto done;
1746 }
1747
1748 /*
1749 * Restore the original data.
1750 */
1751 for (i = 0; i < n; i++) {
1752 c = tgts[i];
1753 rc = &rm->rm_col[c];
1754 bcopy(orig[i], rc->rc_data, rc->rc_size);
1755 }
1756
1757 do {
1758 /*
5631c038 1759 * Find the next valid column after the curr
45d1cae3
BB
1760 * position..
1761 */
5631c038 1762 for (next = tgts[curr] + 1;
45d1cae3
BB
1763 next < rm->rm_cols &&
1764 rm->rm_col[next].rc_error != 0; next++)
1765 continue;
1766
5631c038 1767 ASSERT(next <= tgts[curr + 1]);
45d1cae3
BB
1768
1769 /*
1770 * If that spot is available, we're done here.
1771 */
5631c038 1772 if (next != tgts[curr + 1])
45d1cae3
BB
1773 break;
1774
1775 /*
1776 * Otherwise, find the next valid column after
1777 * the previous position.
1778 */
5631c038 1779 for (c = tgts[curr - 1] + 1;
45d1cae3
BB
1780 rm->rm_col[c].rc_error != 0; c++)
1781 continue;
1782
5631c038
BB
1783 tgts[curr] = c;
1784 curr++;
45d1cae3 1785
5631c038 1786 } while (curr != n);
45d1cae3
BB
1787 }
1788 }
1789 n--;
1790done:
1791 for (i = 0; i < n; i++) {
1792 zio_buf_free(orig[i], rm->rm_col[0].rc_size);
1793 }
1794
1795 return (ret);
1796}
1797
e49f1e20
WA
1798/*
1799 * Complete an IO operation on a RAIDZ VDev
1800 *
1801 * Outline:
1802 * - For write operations:
1803 * 1. Check for errors on the child IOs.
1804 * 2. Return, setting an error code if too few child VDevs were written
1805 * to reconstruct the data later. Note that partial writes are
1806 * considered successful if they can be reconstructed at all.
1807 * - For read operations:
1808 * 1. Check for errors on the child IOs.
1809 * 2. If data errors occurred:
1810 * a. Try to reassemble the data from the parity available.
1811 * b. If we haven't yet read the parity drives, read them now.
1812 * c. If all parity drives have been read but the data still doesn't
1813 * reassemble with a correct checksum, then try combinatorial
1814 * reconstruction.
1815 * d. If that doesn't work, return an error.
1816 * 3. If there were unexpected errors or this is a resilver operation,
1817 * rewrite the vdevs that had errors.
1818 */
b128c09f 1819static void
34dc7c2f
BB
1820vdev_raidz_io_done(zio_t *zio)
1821{
1822 vdev_t *vd = zio->io_vd;
1823 vdev_t *cvd;
1824 raidz_map_t *rm = zio->io_vsd;
d4ed6673 1825 raidz_col_t *rc = NULL;
34dc7c2f
BB
1826 int unexpected_errors = 0;
1827 int parity_errors = 0;
1828 int parity_untried = 0;
1829 int data_errors = 0;
b128c09f 1830 int total_errors = 0;
45d1cae3
BB
1831 int n, c;
1832 int tgts[VDEV_RAIDZ_MAXPARITY];
1833 int code;
34dc7c2f
BB
1834
1835 ASSERT(zio->io_bp != NULL); /* XXX need to add code to enforce this */
1836
34dc7c2f
BB
1837 ASSERT(rm->rm_missingparity <= rm->rm_firstdatacol);
1838 ASSERT(rm->rm_missingdata <= rm->rm_cols - rm->rm_firstdatacol);
1839
1840 for (c = 0; c < rm->rm_cols; c++) {
1841 rc = &rm->rm_col[c];
1842
34dc7c2f 1843 if (rc->rc_error) {
b128c09f 1844 ASSERT(rc->rc_error != ECKSUM); /* child has no bp */
34dc7c2f
BB
1845
1846 if (c < rm->rm_firstdatacol)
1847 parity_errors++;
1848 else
1849 data_errors++;
1850
1851 if (!rc->rc_skipped)
1852 unexpected_errors++;
1853
b128c09f 1854 total_errors++;
34dc7c2f
BB
1855 } else if (c < rm->rm_firstdatacol && !rc->rc_tried) {
1856 parity_untried++;
1857 }
1858 }
1859
1860 if (zio->io_type == ZIO_TYPE_WRITE) {
1861 /*
b128c09f
BB
1862 * XXX -- for now, treat partial writes as a success.
1863 * (If we couldn't write enough columns to reconstruct
1864 * the data, the I/O failed. Otherwise, good enough.)
1865 *
1866 * Now that we support write reallocation, it would be better
1867 * to treat partial failure as real failure unless there are
1868 * no non-degraded top-level vdevs left, and not update DTLs
1869 * if we intend to reallocate.
34dc7c2f
BB
1870 */
1871 /* XXPOLICY */
b128c09f
BB
1872 if (total_errors > rm->rm_firstdatacol)
1873 zio->io_error = vdev_raidz_worst_error(rm);
34dc7c2f 1874
b128c09f 1875 return;
34dc7c2f
BB
1876 }
1877
1878 ASSERT(zio->io_type == ZIO_TYPE_READ);
1879 /*
1880 * There are three potential phases for a read:
1881 * 1. produce valid data from the columns read
1882 * 2. read all disks and try again
1883 * 3. perform combinatorial reconstruction
1884 *
1885 * Each phase is progressively both more expensive and less likely to
1886 * occur. If we encounter more errors than we can repair or all phases
1887 * fail, we have no choice but to return an error.
1888 */
1889
1890 /*
1891 * If the number of errors we saw was correctable -- less than or equal
1892 * to the number of parity disks read -- attempt to produce data that
1893 * has a valid checksum. Naturally, this case applies in the absence of
1894 * any errors.
1895 */
b128c09f 1896 if (total_errors <= rm->rm_firstdatacol - parity_untried) {
45d1cae3 1897 if (data_errors == 0) {
428870ff 1898 if (raidz_checksum_verify(zio) == 0) {
34dc7c2f
BB
1899 /*
1900 * If we read parity information (unnecessarily
1901 * as it happens since no reconstruction was
1902 * needed) regenerate and verify the parity.
1903 * We also regenerate parity when resilvering
1904 * so we can write it out to the failed device
1905 * later.
1906 */
1907 if (parity_errors + parity_untried <
1908 rm->rm_firstdatacol ||
1909 (zio->io_flags & ZIO_FLAG_RESILVER)) {
1910 n = raidz_parity_verify(zio, rm);
1911 unexpected_errors += n;
1912 ASSERT(parity_errors + n <=
1913 rm->rm_firstdatacol);
1914 }
1915 goto done;
1916 }
45d1cae3 1917 } else {
34dc7c2f
BB
1918 /*
1919 * We either attempt to read all the parity columns or
1920 * none of them. If we didn't try to read parity, we
1921 * wouldn't be here in the correctable case. There must
1922 * also have been fewer parity errors than parity
1923 * columns or, again, we wouldn't be in this code path.
1924 */
1925 ASSERT(parity_untried == 0);
1926 ASSERT(parity_errors < rm->rm_firstdatacol);
1927
1928 /*
45d1cae3 1929 * Identify the data columns that reported an error.
34dc7c2f 1930 */
45d1cae3 1931 n = 0;
34dc7c2f
BB
1932 for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) {
1933 rc = &rm->rm_col[c];
45d1cae3
BB
1934 if (rc->rc_error != 0) {
1935 ASSERT(n < VDEV_RAIDZ_MAXPARITY);
1936 tgts[n++] = c;
1937 }
34dc7c2f 1938 }
34dc7c2f 1939
45d1cae3
BB
1940 ASSERT(rm->rm_firstdatacol >= n);
1941
1942 code = vdev_raidz_reconstruct(rm, tgts, n);
34dc7c2f 1943
428870ff 1944 if (raidz_checksum_verify(zio) == 0) {
34dc7c2f 1945 /*
45d1cae3
BB
1946 * If we read more parity disks than were used
1947 * for reconstruction, confirm that the other
1948 * parity disks produced correct data. This
1949 * routine is suboptimal in that it regenerates
1950 * the parity that we already used in addition
1951 * to the parity that we're attempting to
1952 * verify, but this should be a relatively
1953 * uncommon case, and can be optimized if it
1954 * becomes a problem. Note that we regenerate
1955 * parity when resilvering so we can write it
1956 * out to failed devices later.
34dc7c2f 1957 */
45d1cae3 1958 if (parity_errors < rm->rm_firstdatacol - n ||
34dc7c2f
BB
1959 (zio->io_flags & ZIO_FLAG_RESILVER)) {
1960 n = raidz_parity_verify(zio, rm);
1961 unexpected_errors += n;
1962 ASSERT(parity_errors + n <=
1963 rm->rm_firstdatacol);
1964 }
1965
1966 goto done;
1967 }
34dc7c2f
BB
1968 }
1969 }
1970
1971 /*
1972 * This isn't a typical situation -- either we got a read error or
1973 * a child silently returned bad data. Read every block so we can
1974 * try again with as much data and parity as we can track down. If
1975 * we've already been through once before, all children will be marked
1976 * as tried so we'll proceed to combinatorial reconstruction.
1977 */
1978 unexpected_errors = 1;
1979 rm->rm_missingdata = 0;
1980 rm->rm_missingparity = 0;
1981
1982 for (c = 0; c < rm->rm_cols; c++) {
1983 if (rm->rm_col[c].rc_tried)
1984 continue;
1985
34dc7c2f
BB
1986 zio_vdev_io_redone(zio);
1987 do {
1988 rc = &rm->rm_col[c];
1989 if (rc->rc_tried)
1990 continue;
1991 zio_nowait(zio_vdev_child_io(zio, NULL,
1992 vd->vdev_child[rc->rc_devidx],
1993 rc->rc_offset, rc->rc_data, rc->rc_size,
b128c09f 1994 zio->io_type, zio->io_priority, 0,
34dc7c2f
BB
1995 vdev_raidz_child_done, rc));
1996 } while (++c < rm->rm_cols);
34dc7c2f 1997
b128c09f 1998 return;
34dc7c2f
BB
1999 }
2000
2001 /*
2002 * At this point we've attempted to reconstruct the data given the
2003 * errors we detected, and we've attempted to read all columns. There
2004 * must, therefore, be one or more additional problems -- silent errors
2005 * resulting in invalid data rather than explicit I/O errors resulting
45d1cae3
BB
2006 * in absent data. We check if there is enough additional data to
2007 * possibly reconstruct the data and then perform combinatorial
2008 * reconstruction over all possible combinations. If that fails,
2009 * we're cooked.
34dc7c2f 2010 */
428870ff 2011 if (total_errors > rm->rm_firstdatacol) {
b128c09f 2012 zio->io_error = vdev_raidz_worst_error(rm);
34dc7c2f 2013
428870ff
BB
2014 } else if (total_errors < rm->rm_firstdatacol &&
2015 (code = vdev_raidz_combrec(zio, total_errors, data_errors)) != 0) {
34dc7c2f 2016 /*
45d1cae3
BB
2017 * If we didn't use all the available parity for the
2018 * combinatorial reconstruction, verify that the remaining
2019 * parity is correct.
34dc7c2f 2020 */
45d1cae3
BB
2021 if (code != (1 << rm->rm_firstdatacol) - 1)
2022 (void) raidz_parity_verify(zio, rm);
2023 } else {
34dc7c2f 2024 /*
428870ff
BB
2025 * We're here because either:
2026 *
2027 * total_errors == rm_first_datacol, or
2028 * vdev_raidz_combrec() failed
2029 *
2030 * In either case, there is enough bad data to prevent
2031 * reconstruction.
2032 *
2033 * Start checksum ereports for all children which haven't
2034 * failed, and the IO wasn't speculative.
34dc7c2f 2035 */
2e528b49 2036 zio->io_error = SET_ERROR(ECKSUM);
34dc7c2f 2037
45d1cae3
BB
2038 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2039 for (c = 0; c < rm->rm_cols; c++) {
2040 rc = &rm->rm_col[c];
428870ff
BB
2041 if (rc->rc_error == 0) {
2042 zio_bad_cksum_t zbc;
2043 zbc.zbc_has_cksum = 0;
2044 zbc.zbc_injected =
2045 rm->rm_ecksuminjected;
2046
2047 zfs_ereport_start_checksum(
2048 zio->io_spa,
2049 vd->vdev_child[rc->rc_devidx],
2050 zio, rc->rc_offset, rc->rc_size,
2051 (void *)(uintptr_t)c, &zbc);
2052 }
34dc7c2f 2053 }
34dc7c2f
BB
2054 }
2055 }
2056
2057done:
2058 zio_checksum_verified(zio);
2059
fb5f0bc8 2060 if (zio->io_error == 0 && spa_writeable(zio->io_spa) &&
34dc7c2f 2061 (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) {
34dc7c2f
BB
2062 /*
2063 * Use the good data we have in hand to repair damaged children.
34dc7c2f 2064 */
34dc7c2f
BB
2065 for (c = 0; c < rm->rm_cols; c++) {
2066 rc = &rm->rm_col[c];
2067 cvd = vd->vdev_child[rc->rc_devidx];
2068
2069 if (rc->rc_error == 0)
2070 continue;
2071
b128c09f 2072 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
34dc7c2f 2073 rc->rc_offset, rc->rc_data, rc->rc_size,
e8b96c60 2074 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
fb5f0bc8
BB
2075 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
2076 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
34dc7c2f 2077 }
34dc7c2f 2078 }
34dc7c2f
BB
2079}
2080
2081static void
2082vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded)
2083{
2084 if (faulted > vd->vdev_nparity)
2085 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2086 VDEV_AUX_NO_REPLICAS);
2087 else if (degraded + faulted != 0)
2088 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
2089 else
2090 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
2091}
2092
2093vdev_ops_t vdev_raidz_ops = {
2094 vdev_raidz_open,
2095 vdev_raidz_close,
34dc7c2f
BB
2096 vdev_raidz_asize,
2097 vdev_raidz_io_start,
2098 vdev_raidz_io_done,
2099 vdev_raidz_state_change,
428870ff
BB
2100 NULL,
2101 NULL,
34dc7c2f
BB
2102 VDEV_TYPE_RAIDZ, /* name of this vdev type */
2103 B_FALSE /* not a leaf vdev */
2104};