]>
Commit | Line | Data |
---|---|---|
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 | 132 | void |
428870ff | 133 | vdev_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 |
156 | static void |
157 | vdev_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*/ | |
169 | static void | |
170 | vdev_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 | ||
180 | static void | |
181 | vdev_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 | */ | |
259 | static void | |
260 | vdev_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 | ||
306 | static 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 | 318 | noinline raidz_map_t * |
34dc7c2f BB |
319 | vdev_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 |
467 | static void |
468 | vdev_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 | ||
494 | static void | |
495 | vdev_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 | ||
546 | static void | |
547 | vdev_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 | 611 | void |
45d1cae3 BB |
612 | vdev_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 | ||
633 | static int | |
634 | vdev_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 |
672 | static int |
673 | vdev_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 |
730 | static int |
731 | vdev_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 | ||
975 | static void | |
976 | vdev_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 | ||
1005 | static void | |
1006 | vdev_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 | ||
1099 | static void | |
1100 | vdev_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; | |
689f093e GN |
1106 | uint8_t *dst[VDEV_RAIDZ_MAXPARITY] = { NULL }; |
1107 | uint64_t dcount[VDEV_RAIDZ_MAXPARITY] = { 0 }; | |
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 | ||
1175 | static int | |
1176 | vdev_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 |
1281 | int |
1282 | vdev_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 | |
1364 | static int | |
1bd201e7 CS |
1365 | vdev_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 | ||
1409 | static void | |
1410 | vdev_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 | ||
1418 | static uint64_t | |
1419 | vdev_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 | ||
1433 | static void | |
1434 | vdev_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 | 1460 | static void |
34dc7c2f BB |
1461 | vdev_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 | */ | |
1552 | static void | |
428870ff | 1553 | raidz_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 | */ | |
1578 | static int | |
1579 | raidz_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 | */ | |
1600 | static int | |
1601 | raidz_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 | ||
3c67d83a TH |
1607 | blkptr_t *bp = zio->io_bp; |
1608 | enum zio_checksum checksum = (bp == NULL ? zio->io_prop.zp_checksum : | |
1609 | (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp))); | |
1610 | ||
1611 | if (checksum == ZIO_CHECKSUM_NOPARITY) | |
1612 | return (ret); | |
1613 | ||
34dc7c2f BB |
1614 | for (c = 0; c < rm->rm_firstdatacol; c++) { |
1615 | rc = &rm->rm_col[c]; | |
1616 | if (!rc->rc_tried || rc->rc_error != 0) | |
1617 | continue; | |
1618 | orig[c] = zio_buf_alloc(rc->rc_size); | |
1619 | bcopy(rc->rc_data, orig[c], rc->rc_size); | |
1620 | } | |
1621 | ||
45d1cae3 | 1622 | vdev_raidz_generate_parity(rm); |
34dc7c2f BB |
1623 | |
1624 | for (c = 0; c < rm->rm_firstdatacol; c++) { | |
1625 | rc = &rm->rm_col[c]; | |
1626 | if (!rc->rc_tried || rc->rc_error != 0) | |
1627 | continue; | |
1628 | if (bcmp(orig[c], rc->rc_data, rc->rc_size) != 0) { | |
428870ff | 1629 | raidz_checksum_error(zio, rc, orig[c]); |
2e528b49 | 1630 | rc->rc_error = SET_ERROR(ECKSUM); |
34dc7c2f BB |
1631 | ret++; |
1632 | } | |
1633 | zio_buf_free(orig[c], rc->rc_size); | |
1634 | } | |
1635 | ||
1636 | return (ret); | |
1637 | } | |
1638 | ||
34dc7c2f | 1639 | static int |
b128c09f BB |
1640 | vdev_raidz_worst_error(raidz_map_t *rm) |
1641 | { | |
d6320ddb | 1642 | int c, error = 0; |
b128c09f | 1643 | |
d6320ddb | 1644 | for (c = 0; c < rm->rm_cols; c++) |
b128c09f BB |
1645 | error = zio_worst_error(error, rm->rm_col[c].rc_error); |
1646 | ||
1647 | return (error); | |
1648 | } | |
1649 | ||
45d1cae3 BB |
1650 | /* |
1651 | * Iterate over all combinations of bad data and attempt a reconstruction. | |
1652 | * Note that the algorithm below is non-optimal because it doesn't take into | |
1653 | * account how reconstruction is actually performed. For example, with | |
1654 | * triple-parity RAID-Z the reconstruction procedure is the same if column 4 | |
1655 | * is targeted as invalid as if columns 1 and 4 are targeted since in both | |
1656 | * cases we'd only use parity information in column 0. | |
1657 | */ | |
1658 | static int | |
1659 | vdev_raidz_combrec(zio_t *zio, int total_errors, int data_errors) | |
1660 | { | |
1661 | raidz_map_t *rm = zio->io_vsd; | |
1662 | raidz_col_t *rc; | |
1663 | void *orig[VDEV_RAIDZ_MAXPARITY]; | |
1664 | int tstore[VDEV_RAIDZ_MAXPARITY + 2]; | |
1665 | int *tgts = &tstore[1]; | |
5631c038 | 1666 | int curr, next, i, c, n; |
45d1cae3 BB |
1667 | int code, ret = 0; |
1668 | ||
1669 | ASSERT(total_errors < rm->rm_firstdatacol); | |
1670 | ||
1671 | /* | |
1672 | * This simplifies one edge condition. | |
1673 | */ | |
1674 | tgts[-1] = -1; | |
1675 | ||
1676 | for (n = 1; n <= rm->rm_firstdatacol - total_errors; n++) { | |
1677 | /* | |
1678 | * Initialize the targets array by finding the first n columns | |
1679 | * that contain no error. | |
1680 | * | |
1681 | * If there were no data errors, we need to ensure that we're | |
1682 | * always explicitly attempting to reconstruct at least one | |
1683 | * data column. To do this, we simply push the highest target | |
1684 | * up into the data columns. | |
1685 | */ | |
1686 | for (c = 0, i = 0; i < n; i++) { | |
1687 | if (i == n - 1 && data_errors == 0 && | |
1688 | c < rm->rm_firstdatacol) { | |
1689 | c = rm->rm_firstdatacol; | |
1690 | } | |
1691 | ||
1692 | while (rm->rm_col[c].rc_error != 0) { | |
1693 | c++; | |
1694 | ASSERT3S(c, <, rm->rm_cols); | |
1695 | } | |
1696 | ||
1697 | tgts[i] = c++; | |
1698 | } | |
1699 | ||
1700 | /* | |
1701 | * Setting tgts[n] simplifies the other edge condition. | |
1702 | */ | |
1703 | tgts[n] = rm->rm_cols; | |
1704 | ||
1705 | /* | |
1706 | * These buffers were allocated in previous iterations. | |
1707 | */ | |
1708 | for (i = 0; i < n - 1; i++) { | |
1709 | ASSERT(orig[i] != NULL); | |
1710 | } | |
1711 | ||
1712 | orig[n - 1] = zio_buf_alloc(rm->rm_col[0].rc_size); | |
1713 | ||
5631c038 BB |
1714 | curr = 0; |
1715 | next = tgts[curr]; | |
45d1cae3 | 1716 | |
5631c038 BB |
1717 | while (curr != n) { |
1718 | tgts[curr] = next; | |
1719 | curr = 0; | |
45d1cae3 BB |
1720 | |
1721 | /* | |
1722 | * Save off the original data that we're going to | |
1723 | * attempt to reconstruct. | |
1724 | */ | |
1725 | for (i = 0; i < n; i++) { | |
1726 | ASSERT(orig[i] != NULL); | |
1727 | c = tgts[i]; | |
1728 | ASSERT3S(c, >=, 0); | |
1729 | ASSERT3S(c, <, rm->rm_cols); | |
1730 | rc = &rm->rm_col[c]; | |
1731 | bcopy(rc->rc_data, orig[i], rc->rc_size); | |
1732 | } | |
1733 | ||
1734 | /* | |
1735 | * Attempt a reconstruction and exit the outer loop on | |
1736 | * success. | |
1737 | */ | |
1738 | code = vdev_raidz_reconstruct(rm, tgts, n); | |
428870ff | 1739 | if (raidz_checksum_verify(zio) == 0) { |
45d1cae3 BB |
1740 | |
1741 | for (i = 0; i < n; i++) { | |
1742 | c = tgts[i]; | |
1743 | rc = &rm->rm_col[c]; | |
1744 | ASSERT(rc->rc_error == 0); | |
1745 | if (rc->rc_tried) | |
428870ff BB |
1746 | raidz_checksum_error(zio, rc, |
1747 | orig[i]); | |
2e528b49 | 1748 | rc->rc_error = SET_ERROR(ECKSUM); |
45d1cae3 BB |
1749 | } |
1750 | ||
1751 | ret = code; | |
1752 | goto done; | |
1753 | } | |
1754 | ||
1755 | /* | |
1756 | * Restore the original data. | |
1757 | */ | |
1758 | for (i = 0; i < n; i++) { | |
1759 | c = tgts[i]; | |
1760 | rc = &rm->rm_col[c]; | |
1761 | bcopy(orig[i], rc->rc_data, rc->rc_size); | |
1762 | } | |
1763 | ||
1764 | do { | |
1765 | /* | |
5631c038 | 1766 | * Find the next valid column after the curr |
45d1cae3 BB |
1767 | * position.. |
1768 | */ | |
5631c038 | 1769 | for (next = tgts[curr] + 1; |
45d1cae3 BB |
1770 | next < rm->rm_cols && |
1771 | rm->rm_col[next].rc_error != 0; next++) | |
1772 | continue; | |
1773 | ||
5631c038 | 1774 | ASSERT(next <= tgts[curr + 1]); |
45d1cae3 BB |
1775 | |
1776 | /* | |
1777 | * If that spot is available, we're done here. | |
1778 | */ | |
5631c038 | 1779 | if (next != tgts[curr + 1]) |
45d1cae3 BB |
1780 | break; |
1781 | ||
1782 | /* | |
1783 | * Otherwise, find the next valid column after | |
1784 | * the previous position. | |
1785 | */ | |
5631c038 | 1786 | for (c = tgts[curr - 1] + 1; |
45d1cae3 BB |
1787 | rm->rm_col[c].rc_error != 0; c++) |
1788 | continue; | |
1789 | ||
5631c038 BB |
1790 | tgts[curr] = c; |
1791 | curr++; | |
45d1cae3 | 1792 | |
5631c038 | 1793 | } while (curr != n); |
45d1cae3 BB |
1794 | } |
1795 | } | |
1796 | n--; | |
1797 | done: | |
1798 | for (i = 0; i < n; i++) { | |
1799 | zio_buf_free(orig[i], rm->rm_col[0].rc_size); | |
1800 | } | |
1801 | ||
1802 | return (ret); | |
1803 | } | |
1804 | ||
e49f1e20 WA |
1805 | /* |
1806 | * Complete an IO operation on a RAIDZ VDev | |
1807 | * | |
1808 | * Outline: | |
1809 | * - For write operations: | |
1810 | * 1. Check for errors on the child IOs. | |
1811 | * 2. Return, setting an error code if too few child VDevs were written | |
1812 | * to reconstruct the data later. Note that partial writes are | |
1813 | * considered successful if they can be reconstructed at all. | |
1814 | * - For read operations: | |
1815 | * 1. Check for errors on the child IOs. | |
1816 | * 2. If data errors occurred: | |
1817 | * a. Try to reassemble the data from the parity available. | |
1818 | * b. If we haven't yet read the parity drives, read them now. | |
1819 | * c. If all parity drives have been read but the data still doesn't | |
1820 | * reassemble with a correct checksum, then try combinatorial | |
1821 | * reconstruction. | |
1822 | * d. If that doesn't work, return an error. | |
1823 | * 3. If there were unexpected errors or this is a resilver operation, | |
1824 | * rewrite the vdevs that had errors. | |
1825 | */ | |
b128c09f | 1826 | static void |
34dc7c2f BB |
1827 | vdev_raidz_io_done(zio_t *zio) |
1828 | { | |
1829 | vdev_t *vd = zio->io_vd; | |
1830 | vdev_t *cvd; | |
1831 | raidz_map_t *rm = zio->io_vsd; | |
d4ed6673 | 1832 | raidz_col_t *rc = NULL; |
34dc7c2f BB |
1833 | int unexpected_errors = 0; |
1834 | int parity_errors = 0; | |
1835 | int parity_untried = 0; | |
1836 | int data_errors = 0; | |
b128c09f | 1837 | int total_errors = 0; |
45d1cae3 BB |
1838 | int n, c; |
1839 | int tgts[VDEV_RAIDZ_MAXPARITY]; | |
1840 | int code; | |
34dc7c2f BB |
1841 | |
1842 | ASSERT(zio->io_bp != NULL); /* XXX need to add code to enforce this */ | |
1843 | ||
34dc7c2f BB |
1844 | ASSERT(rm->rm_missingparity <= rm->rm_firstdatacol); |
1845 | ASSERT(rm->rm_missingdata <= rm->rm_cols - rm->rm_firstdatacol); | |
1846 | ||
1847 | for (c = 0; c < rm->rm_cols; c++) { | |
1848 | rc = &rm->rm_col[c]; | |
1849 | ||
34dc7c2f | 1850 | if (rc->rc_error) { |
b128c09f | 1851 | ASSERT(rc->rc_error != ECKSUM); /* child has no bp */ |
34dc7c2f BB |
1852 | |
1853 | if (c < rm->rm_firstdatacol) | |
1854 | parity_errors++; | |
1855 | else | |
1856 | data_errors++; | |
1857 | ||
1858 | if (!rc->rc_skipped) | |
1859 | unexpected_errors++; | |
1860 | ||
b128c09f | 1861 | total_errors++; |
34dc7c2f BB |
1862 | } else if (c < rm->rm_firstdatacol && !rc->rc_tried) { |
1863 | parity_untried++; | |
1864 | } | |
1865 | } | |
1866 | ||
1867 | if (zio->io_type == ZIO_TYPE_WRITE) { | |
1868 | /* | |
b128c09f BB |
1869 | * XXX -- for now, treat partial writes as a success. |
1870 | * (If we couldn't write enough columns to reconstruct | |
1871 | * the data, the I/O failed. Otherwise, good enough.) | |
1872 | * | |
1873 | * Now that we support write reallocation, it would be better | |
1874 | * to treat partial failure as real failure unless there are | |
1875 | * no non-degraded top-level vdevs left, and not update DTLs | |
1876 | * if we intend to reallocate. | |
34dc7c2f BB |
1877 | */ |
1878 | /* XXPOLICY */ | |
b128c09f BB |
1879 | if (total_errors > rm->rm_firstdatacol) |
1880 | zio->io_error = vdev_raidz_worst_error(rm); | |
34dc7c2f | 1881 | |
b128c09f | 1882 | return; |
34dc7c2f BB |
1883 | } |
1884 | ||
1885 | ASSERT(zio->io_type == ZIO_TYPE_READ); | |
1886 | /* | |
1887 | * There are three potential phases for a read: | |
1888 | * 1. produce valid data from the columns read | |
1889 | * 2. read all disks and try again | |
1890 | * 3. perform combinatorial reconstruction | |
1891 | * | |
1892 | * Each phase is progressively both more expensive and less likely to | |
1893 | * occur. If we encounter more errors than we can repair or all phases | |
1894 | * fail, we have no choice but to return an error. | |
1895 | */ | |
1896 | ||
1897 | /* | |
1898 | * If the number of errors we saw was correctable -- less than or equal | |
1899 | * to the number of parity disks read -- attempt to produce data that | |
1900 | * has a valid checksum. Naturally, this case applies in the absence of | |
1901 | * any errors. | |
1902 | */ | |
b128c09f | 1903 | if (total_errors <= rm->rm_firstdatacol - parity_untried) { |
45d1cae3 | 1904 | if (data_errors == 0) { |
428870ff | 1905 | if (raidz_checksum_verify(zio) == 0) { |
34dc7c2f BB |
1906 | /* |
1907 | * If we read parity information (unnecessarily | |
1908 | * as it happens since no reconstruction was | |
1909 | * needed) regenerate and verify the parity. | |
1910 | * We also regenerate parity when resilvering | |
1911 | * so we can write it out to the failed device | |
1912 | * later. | |
1913 | */ | |
1914 | if (parity_errors + parity_untried < | |
1915 | rm->rm_firstdatacol || | |
1916 | (zio->io_flags & ZIO_FLAG_RESILVER)) { | |
1917 | n = raidz_parity_verify(zio, rm); | |
1918 | unexpected_errors += n; | |
1919 | ASSERT(parity_errors + n <= | |
1920 | rm->rm_firstdatacol); | |
1921 | } | |
1922 | goto done; | |
1923 | } | |
45d1cae3 | 1924 | } else { |
34dc7c2f BB |
1925 | /* |
1926 | * We either attempt to read all the parity columns or | |
1927 | * none of them. If we didn't try to read parity, we | |
1928 | * wouldn't be here in the correctable case. There must | |
1929 | * also have been fewer parity errors than parity | |
1930 | * columns or, again, we wouldn't be in this code path. | |
1931 | */ | |
1932 | ASSERT(parity_untried == 0); | |
1933 | ASSERT(parity_errors < rm->rm_firstdatacol); | |
1934 | ||
1935 | /* | |
45d1cae3 | 1936 | * Identify the data columns that reported an error. |
34dc7c2f | 1937 | */ |
45d1cae3 | 1938 | n = 0; |
34dc7c2f BB |
1939 | for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) { |
1940 | rc = &rm->rm_col[c]; | |
45d1cae3 BB |
1941 | if (rc->rc_error != 0) { |
1942 | ASSERT(n < VDEV_RAIDZ_MAXPARITY); | |
1943 | tgts[n++] = c; | |
1944 | } | |
34dc7c2f | 1945 | } |
34dc7c2f | 1946 | |
45d1cae3 BB |
1947 | ASSERT(rm->rm_firstdatacol >= n); |
1948 | ||
1949 | code = vdev_raidz_reconstruct(rm, tgts, n); | |
34dc7c2f | 1950 | |
428870ff | 1951 | if (raidz_checksum_verify(zio) == 0) { |
34dc7c2f | 1952 | /* |
45d1cae3 BB |
1953 | * If we read more parity disks than were used |
1954 | * for reconstruction, confirm that the other | |
1955 | * parity disks produced correct data. This | |
1956 | * routine is suboptimal in that it regenerates | |
1957 | * the parity that we already used in addition | |
1958 | * to the parity that we're attempting to | |
1959 | * verify, but this should be a relatively | |
1960 | * uncommon case, and can be optimized if it | |
1961 | * becomes a problem. Note that we regenerate | |
1962 | * parity when resilvering so we can write it | |
1963 | * out to failed devices later. | |
34dc7c2f | 1964 | */ |
45d1cae3 | 1965 | if (parity_errors < rm->rm_firstdatacol - n || |
34dc7c2f BB |
1966 | (zio->io_flags & ZIO_FLAG_RESILVER)) { |
1967 | n = raidz_parity_verify(zio, rm); | |
1968 | unexpected_errors += n; | |
1969 | ASSERT(parity_errors + n <= | |
1970 | rm->rm_firstdatacol); | |
1971 | } | |
1972 | ||
1973 | goto done; | |
1974 | } | |
34dc7c2f BB |
1975 | } |
1976 | } | |
1977 | ||
1978 | /* | |
1979 | * This isn't a typical situation -- either we got a read error or | |
1980 | * a child silently returned bad data. Read every block so we can | |
1981 | * try again with as much data and parity as we can track down. If | |
1982 | * we've already been through once before, all children will be marked | |
1983 | * as tried so we'll proceed to combinatorial reconstruction. | |
1984 | */ | |
1985 | unexpected_errors = 1; | |
1986 | rm->rm_missingdata = 0; | |
1987 | rm->rm_missingparity = 0; | |
1988 | ||
1989 | for (c = 0; c < rm->rm_cols; c++) { | |
1990 | if (rm->rm_col[c].rc_tried) | |
1991 | continue; | |
1992 | ||
34dc7c2f BB |
1993 | zio_vdev_io_redone(zio); |
1994 | do { | |
1995 | rc = &rm->rm_col[c]; | |
1996 | if (rc->rc_tried) | |
1997 | continue; | |
1998 | zio_nowait(zio_vdev_child_io(zio, NULL, | |
1999 | vd->vdev_child[rc->rc_devidx], | |
2000 | rc->rc_offset, rc->rc_data, rc->rc_size, | |
b128c09f | 2001 | zio->io_type, zio->io_priority, 0, |
34dc7c2f BB |
2002 | vdev_raidz_child_done, rc)); |
2003 | } while (++c < rm->rm_cols); | |
34dc7c2f | 2004 | |
b128c09f | 2005 | return; |
34dc7c2f BB |
2006 | } |
2007 | ||
2008 | /* | |
2009 | * At this point we've attempted to reconstruct the data given the | |
2010 | * errors we detected, and we've attempted to read all columns. There | |
2011 | * must, therefore, be one or more additional problems -- silent errors | |
2012 | * resulting in invalid data rather than explicit I/O errors resulting | |
45d1cae3 BB |
2013 | * in absent data. We check if there is enough additional data to |
2014 | * possibly reconstruct the data and then perform combinatorial | |
2015 | * reconstruction over all possible combinations. If that fails, | |
2016 | * we're cooked. | |
34dc7c2f | 2017 | */ |
428870ff | 2018 | if (total_errors > rm->rm_firstdatacol) { |
b128c09f | 2019 | zio->io_error = vdev_raidz_worst_error(rm); |
34dc7c2f | 2020 | |
428870ff BB |
2021 | } else if (total_errors < rm->rm_firstdatacol && |
2022 | (code = vdev_raidz_combrec(zio, total_errors, data_errors)) != 0) { | |
34dc7c2f | 2023 | /* |
45d1cae3 BB |
2024 | * If we didn't use all the available parity for the |
2025 | * combinatorial reconstruction, verify that the remaining | |
2026 | * parity is correct. | |
34dc7c2f | 2027 | */ |
45d1cae3 BB |
2028 | if (code != (1 << rm->rm_firstdatacol) - 1) |
2029 | (void) raidz_parity_verify(zio, rm); | |
2030 | } else { | |
34dc7c2f | 2031 | /* |
428870ff BB |
2032 | * We're here because either: |
2033 | * | |
2034 | * total_errors == rm_first_datacol, or | |
2035 | * vdev_raidz_combrec() failed | |
2036 | * | |
2037 | * In either case, there is enough bad data to prevent | |
2038 | * reconstruction. | |
2039 | * | |
2040 | * Start checksum ereports for all children which haven't | |
2041 | * failed, and the IO wasn't speculative. | |
34dc7c2f | 2042 | */ |
2e528b49 | 2043 | zio->io_error = SET_ERROR(ECKSUM); |
34dc7c2f | 2044 | |
45d1cae3 BB |
2045 | if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { |
2046 | for (c = 0; c < rm->rm_cols; c++) { | |
2047 | rc = &rm->rm_col[c]; | |
428870ff BB |
2048 | if (rc->rc_error == 0) { |
2049 | zio_bad_cksum_t zbc; | |
2050 | zbc.zbc_has_cksum = 0; | |
2051 | zbc.zbc_injected = | |
2052 | rm->rm_ecksuminjected; | |
2053 | ||
2054 | zfs_ereport_start_checksum( | |
2055 | zio->io_spa, | |
2056 | vd->vdev_child[rc->rc_devidx], | |
2057 | zio, rc->rc_offset, rc->rc_size, | |
2058 | (void *)(uintptr_t)c, &zbc); | |
2059 | } | |
34dc7c2f | 2060 | } |
34dc7c2f BB |
2061 | } |
2062 | } | |
2063 | ||
2064 | done: | |
2065 | zio_checksum_verified(zio); | |
2066 | ||
fb5f0bc8 | 2067 | if (zio->io_error == 0 && spa_writeable(zio->io_spa) && |
34dc7c2f | 2068 | (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) { |
34dc7c2f BB |
2069 | /* |
2070 | * Use the good data we have in hand to repair damaged children. | |
34dc7c2f | 2071 | */ |
34dc7c2f BB |
2072 | for (c = 0; c < rm->rm_cols; c++) { |
2073 | rc = &rm->rm_col[c]; | |
2074 | cvd = vd->vdev_child[rc->rc_devidx]; | |
2075 | ||
2076 | if (rc->rc_error == 0) | |
2077 | continue; | |
2078 | ||
b128c09f | 2079 | zio_nowait(zio_vdev_child_io(zio, NULL, cvd, |
34dc7c2f | 2080 | rc->rc_offset, rc->rc_data, rc->rc_size, |
e8b96c60 | 2081 | ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE, |
fb5f0bc8 BB |
2082 | ZIO_FLAG_IO_REPAIR | (unexpected_errors ? |
2083 | ZIO_FLAG_SELF_HEAL : 0), NULL, NULL)); | |
34dc7c2f | 2084 | } |
34dc7c2f | 2085 | } |
34dc7c2f BB |
2086 | } |
2087 | ||
2088 | static void | |
2089 | vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded) | |
2090 | { | |
2091 | if (faulted > vd->vdev_nparity) | |
2092 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
2093 | VDEV_AUX_NO_REPLICAS); | |
2094 | else if (degraded + faulted != 0) | |
2095 | vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); | |
2096 | else | |
2097 | vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); | |
2098 | } | |
2099 | ||
2100 | vdev_ops_t vdev_raidz_ops = { | |
2101 | vdev_raidz_open, | |
2102 | vdev_raidz_close, | |
34dc7c2f BB |
2103 | vdev_raidz_asize, |
2104 | vdev_raidz_io_start, | |
2105 | vdev_raidz_io_done, | |
2106 | vdev_raidz_state_change, | |
428870ff BB |
2107 | NULL, |
2108 | NULL, | |
34dc7c2f BB |
2109 | VDEV_TYPE_RAIDZ, /* name of this vdev type */ |
2110 | B_FALSE /* not a leaf vdev */ | |
2111 | }; |