1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12
13 /*
14 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
16 * Copyright (c) 2016 Gvozden Nešković. All rights reserved.
17 * Copyright (c) 2025, Klara, Inc.
18 * Copyright (c) 2026, Wasabi Technologies, Inc.
19 */
20
21 #include <sys/zfs_context.h>
22 #include <sys/spa.h>
23 #include <sys/spa_impl.h>
24 #include <sys/zap.h>
25 #include <sys/vdev_impl.h>
26 #include <sys/metaslab_impl.h>
27 #include <sys/zio.h>
28 #include <sys/zio_checksum.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/abd.h>
31 #include <sys/zfs_rlock.h>
32 #include <sys/fs/zfs.h>
33 #include <sys/fm/fs/zfs.h>
34 #include <sys/vdev_raidz.h>
35 #include <sys/vdev_raidz_impl.h>
36 #include <sys/vdev_draid.h>
37 #include <sys/uberblock_impl.h>
38 #include <sys/dsl_scan.h>
39
40 #ifdef ZFS_DEBUG
41 #include <sys/vdev.h> /* For vdev_xlate() in vdev_raidz_io_verify() */
42 #endif
43
44 /*
45 * Virtual device vector for RAID-Z.
46 *
47 * This vdev supports single, double, and triple parity. For single parity,
48 * we use a simple XOR of all the data columns. For double or triple parity,
49 * we use a special case of Reed-Solomon coding. This extends the
50 * technique described in "The mathematics of RAID-6" by H. Peter Anvin by
51 * drawing on the system described in "A Tutorial on Reed-Solomon Coding for
52 * Fault-Tolerance in RAID-like Systems" by James S. Plank on which the
53 * former is also based. The latter is designed to provide higher performance
54 * for writes.
55 *
56 * Note that the Plank paper claimed to support arbitrary N+M, but was then
57 * amended six years later identifying a critical flaw that invalidates its
58 * claims. Nevertheless, the technique can be adapted to work for up to
59 * triple parity. For additional parity, the amendment "Note: Correction to
60 * the 1997 Tutorial on Reed-Solomon Coding" by James S. Plank and Ying Ding
61 * is viable, but the additional complexity means that write performance will
62 * suffer.
63 *
64 * All of the methods above operate on a Galois field, defined over the
65 * integers mod 2^N. In our case we choose N=8 for GF(8) so that all elements
66 * can be expressed with a single byte. Briefly, the operations on the
67 * field are defined as follows:
68 *
69 * o addition (+) is represented by a bitwise XOR
70 * o subtraction (-) is therefore identical to addition: A + B = A - B
71 * o multiplication of A by 2 is defined by the following bitwise expression:
72 *
73 * (A * 2)_7 = A_6
74 * (A * 2)_6 = A_5
75 * (A * 2)_5 = A_4
76 * (A * 2)_4 = A_3 + A_7
77 * (A * 2)_3 = A_2 + A_7
78 * (A * 2)_2 = A_1 + A_7
79 * (A * 2)_1 = A_0
80 * (A * 2)_0 = A_7
81 *
82 * In C, multiplying by 2 is therefore ((a << 1) ^ ((a & 0x80) ? 0x1d : 0)).
83 * As an aside, this multiplication is derived from the error correcting
84 * primitive polynomial x^8 + x^4 + x^3 + x^2 + 1.
85 *
86 * Observe that any number in the field (except for 0) can be expressed as a
87 * power of 2 -- a generator for the field. We store a table of the powers of
88 * 2 and logs base 2 for quick look ups, and exploit the fact that A * B can
89 * be rewritten as 2^(log_2(A) + log_2(B)) (where '+' is normal addition rather
90 * than field addition). The inverse of a field element A (A^-1) is therefore
91 * A ^ (255 - 1) = A^254.
92 *
93 * The up-to-three parity columns, P, Q, R over several data columns,
94 * D_0, ... D_n-1, can be expressed by field operations:
95 *
96 * P = D_0 + D_1 + ... + D_n-2 + D_n-1
97 * Q = 2^n-1 * D_0 + 2^n-2 * D_1 + ... + 2^1 * D_n-2 + 2^0 * D_n-1
98 * = ((...((D_0) * 2 + D_1) * 2 + ...) * 2 + D_n-2) * 2 + D_n-1
99 * R = 4^n-1 * D_0 + 4^n-2 * D_1 + ... + 4^1 * D_n-2 + 4^0 * D_n-1
100 * = ((...((D_0) * 4 + D_1) * 4 + ...) * 4 + D_n-2) * 4 + D_n-1
101 *
102 * We chose 1, 2, and 4 as our generators because 1 corresponds to the trivial
103 * XOR operation, and 2 and 4 can be computed quickly and generate linearly-
104 * independent coefficients. (There are no additional coefficients that have
105 * this property which is why the uncorrected Plank method breaks down.)
106 *
107 * See the reconstruction code below for how P, Q and R can used individually
108 * or in concert to recover missing data columns.
109 */
110
111 #define VDEV_RAIDZ_P 0
112 #define VDEV_RAIDZ_Q 1
113 #define VDEV_RAIDZ_R 2
114
115 #define VDEV_RAIDZ_MUL_2(x) (((x) << 1) ^ (((x) & 0x80) ? 0x1d : 0))
116 #define VDEV_RAIDZ_MUL_4(x) (VDEV_RAIDZ_MUL_2(VDEV_RAIDZ_MUL_2(x)))
117
118 /*
119 * We provide a mechanism to perform the field multiplication operation on a
120 * 64-bit value all at once rather than a byte at a time. This works by
121 * creating a mask from the top bit in each byte and using that to
122 * conditionally apply the XOR of 0x1d.
123 */
124 #define VDEV_RAIDZ_64MUL_2(x, mask) \
125 { \
126 (mask) = (x) & 0x8080808080808080ULL; \
127 (mask) = ((mask) << 1) - ((mask) >> 7); \
128 (x) = (((x) << 1) & 0xfefefefefefefefeULL) ^ \
129 ((mask) & 0x1d1d1d1d1d1d1d1dULL); \
130 }
131
132 #define VDEV_RAIDZ_64MUL_4(x, mask) \
133 { \
134 VDEV_RAIDZ_64MUL_2((x), mask); \
135 VDEV_RAIDZ_64MUL_2((x), mask); \
136 }
137
138
139 /*
140 * Big Theory Statement for how a RAIDZ VDEV is expanded
141 *
142 * An existing RAIDZ VDEV can be expanded by attaching a new disk. Expansion
143 * works with all three RAIDZ parity choices, including RAIDZ1, 2, or 3. VDEVs
144 * that have been previously expanded can be expanded again.
145 *
146 * The RAIDZ VDEV must be healthy (must be able to write to all the drives in
147 * the VDEV) when an expansion starts. And the expansion will pause if any
148 * disk in the VDEV fails, and resume once the VDEV is healthy again. All other
149 * operations on the pool can continue while an expansion is in progress (e.g.
150 * read/write, snapshot, zpool add, etc). Except zpool checkpoint, zpool trim,
151 * and zpool initialize which can't be run during an expansion. Following a
152 * reboot or export/import, the expansion resumes where it left off.
153 *
154 * == Reflowing the Data ==
155 *
156 * The expansion involves reflowing (copying) the data from the current set
157 * of disks to spread it across the new set which now has one more disk. This
158 * reflow operation is similar to reflowing text when the column width of a
159 * text editor window is expanded. The text doesn’t change but the location of
160 * the text changes to accommodate the new width. An example reflow result for
161 * a 4-wide RAIDZ1 to a 5-wide is shown below.
162 *
163 * Reflow End State
164 * Each letter indicates a parity group (logical stripe)
165 *
166 * Before expansion After Expansion
167 * D1 D2 D3 D4 D1 D2 D3 D4 D5
168 * +------+------+------+------+ +------+------+------+------+------+
169 * | | | | | | | | | | |
170 * | A | A | A | A | | A | A | A | A | B |
171 * | 1| 2| 3| 4| | 1| 2| 3| 4| 5|
172 * +------+------+------+------+ +------+------+------+------+------+
173 * | | | | | | | | | | |
174 * | B | B | C | C | | B | C | C | C | C |
175 * | 5| 6| 7| 8| | 6| 7| 8| 9| 10|
176 * +------+------+------+------+ +------+------+------+------+------+
177 * | | | | | | | | | | |
178 * | C | C | D | D | | D | D | E | E | E |
179 * | 9| 10| 11| 12| | 11| 12| 13| 14| 15|
180 * +------+------+------+------+ +------+------+------+------+------+
181 * | | | | | | | | | | |
182 * | E | E | E | E | --> | E | F | F | G | G |
183 * | 13| 14| 15| 16| | 16| 17| 18|p 19| 20|
184 * +------+------+------+------+ +------+------+------+------+------+
185 * | | | | | | | | | | |
186 * | F | F | G | G | | G | G | H | H | H |
187 * | 17| 18| 19| 20| | 21| 22| 23| 24| 25|
188 * +------+------+------+------+ +------+------+------+------+------+
189 * | | | | | | | | | | |
190 * | G | G | H | H | | H | I | I | J | J |
191 * | 21| 22| 23| 24| | 26| 27| 28| 29| 30|
192 * +------+------+------+------+ +------+------+------+------+------+
193 * | | | | | | | | | | |
194 * | H | H | I | I | | J | J | | | K |
195 * | 25| 26| 27| 28| | 31| 32| 33| 34| 35|
196 * +------+------+------+------+ +------+------+------+------+------+
197 *
198 * This reflow approach has several advantages. There is no need to read or
199 * modify the block pointers or recompute any block checksums. The reflow
200 * doesn’t need to know where the parity sectors reside. We can read and write
201 * data sequentially and the copy can occur in a background thread in open
202 * context. The design also allows for fast discovery of what data to copy.
203 *
204 * The VDEV metaslabs are processed, one at a time, to copy the block data to
205 * have it flow across all the disks. The metaslab is disabled for allocations
206 * during the copy. As an optimization, we only copy the allocated data which
207 * can be determined by looking at the metaslab range tree. During the copy we
208 * must maintain the redundancy guarantees of the RAIDZ VDEV (i.e., we still
209 * need to be able to survive losing parity count disks). This means we
210 * cannot overwrite data during the reflow that would be needed if a disk is
211 * lost.
212 *
213 * After the reflow completes, all newly-written blocks will have the new
214 * layout, i.e., they will have the parity to data ratio implied by the new
215 * number of disks in the RAIDZ group. Even though the reflow copies all of
216 * the allocated space (data and parity), it is only rearranged, not changed.
217 *
218 * This act of reflowing the data has a few implications about blocks
219 * that were written before the reflow completes:
220 *
221 * - Old blocks will still use the same amount of space (i.e., they will have
222 * the parity to data ratio implied by the old number of disks in the RAIDZ
223 * group).
224 * - Reading old blocks will be slightly slower than before the reflow, for
225 * two reasons. First, we will have to read from all disks in the RAIDZ
226 * VDEV, rather than being able to skip the children that contain only
227 * parity of this block (because the data of a single block is now spread
228 * out across all the disks). Second, in most cases there will be an extra
229 * bcopy, needed to rearrange the data back to its original layout in memory.
230 *
231 * == Scratch Area ==
232 *
233 * As we copy the block data, we can only progress to the point that writes
234 * will not overlap with blocks whose progress has not yet been recorded on
235 * disk. Since partially-copied rows are always read from the old location,
236 * we need to stop one row before the sector-wise overlap, to prevent any
237 * row-wise overlap. For example, in the diagram above, when we reflow sector
238 * B6 it will overwite the original location for B5.
239 *
240 * To get around this, a scratch space is used so that we can start copying
241 * without risking data loss by overlapping the row. As an added benefit, it
242 * improves performance at the beginning of the reflow, but that small perf
243 * boost wouldn't be worth the complexity on its own.
244 *
245 * Ideally we want to copy at least 2 * (new_width)^2 so that we have a
246 * separation of 2*(new_width+1) and a chunk size of new_width+2. With the max
247 * RAIDZ width of 255 and 4K sectors this would be 2MB per disk. In practice
248 * the widths will likely be single digits so we can get a substantial chuck
249 * size using only a few MB of scratch per disk.
250 *
251 * The scratch area is persisted to disk which holds a large amount of reflowed
252 * state. We can always read the partially written stripes when a disk fails or
253 * the copy is interrupted (crash) during the initial copying phase and also
254 * get past a small chunk size restriction. At a minimum, the scratch space
255 * must be large enough to get us to the point that one row does not overlap
256 * itself when moved (i.e new_width^2). But going larger is even better. We
257 * use the 3.5 MiB reserved "boot" space that resides after the ZFS disk labels
258 * as our scratch space to handle overwriting the initial part of the VDEV.
259 *
260 * 0 256K 512K 4M
261 * +------+------+-----------------------+-----------------------------
262 * | VDEV | VDEV | Boot Block (3.5M) | Allocatable space ...
263 * | L0 | L1 | Reserved | (Metaslabs)
264 * +------+------+-----------------------+-------------------------------
265 * Scratch Area
266 *
267 * == Reflow Progress Updates ==
268 * After the initial scratch-based reflow, the expansion process works
269 * similarly to device removal. We create a new open context thread which
270 * reflows the data, and periodically kicks off sync tasks to update logical
271 * state. In this case, state is the committed progress (offset of next data
272 * to copy). We need to persist the completed offset on disk, so that if we
273 * crash we know which format each VDEV offset is in.
274 *
275 * == Time Dependent Geometry ==
276 *
277 * In non-expanded RAIDZ, blocks are read from disk in a column by column
278 * fashion. For a multi-row block, the second sector is in the first column
279 * not in the second column. This allows us to issue full reads for each
280 * column directly into the request buffer. The block data is thus laid out
281 * sequentially in a column-by-column fashion.
282 *
283 * For example, in the before expansion diagram above, one logical block might
284 * be sectors G19-H26. The parity is in G19,H23; and the data is in
285 * G20,H24,G21,H25,G22,H26.
286 *
287 * After a block is reflowed, the sectors that were all in the original column
288 * data can now reside in different columns. When reading from an expanded
289 * VDEV, we need to know the logical stripe width for each block so we can
290 * reconstitute the block’s data after the reads are completed. Likewise,
291 * when we perform the combinatorial reconstruction we need to know the
292 * original width so we can retry combinations from the past layouts.
293 *
294 * Time dependent geometry is what we call having blocks with different layouts
295 * (stripe widths) in the same VDEV. This time-dependent geometry uses the
296 * block’s birth time (+ the time expansion ended) to establish the correct
297 * width for a given block. After an expansion completes, we record the time
298 * for blocks written with a particular width (geometry).
299 *
300 * == On Disk Format Changes ==
301 *
302 * New pool feature flag, 'raidz_expansion' whose reference count is the number
303 * of RAIDZ VDEVs that have been expanded.
304 *
305 * The blocks on expanded RAIDZ VDEV can have different logical stripe widths.
306 *
307 * Since the uberblock can point to arbitrary blocks, which might be on the
308 * expanding RAIDZ, and might or might not have been expanded. We need to know
309 * which way a block is laid out before reading it. This info is the next
310 * offset that needs to be reflowed and we persist that in the uberblock, in
311 * the new ub_raidz_reflow_info field, as opposed to the MOS or the vdev label.
312 * After the expansion is complete, we then use the raidz_expand_txgs array
313 * (see below) to determine how to read a block and the ub_raidz_reflow_info
314 * field no longer required.
315 *
316 * The uberblock's ub_raidz_reflow_info field also holds the scratch space
317 * state (i.e., active or not) which is also required before reading a block
318 * during the initial phase of reflowing the data.
319 *
320 * The top-level RAIDZ VDEV has two new entries in the nvlist:
321 *
322 * 'raidz_expand_txgs' array: logical stripe widths by txg are recorded here
323 * and used after the expansion is complete to
324 * determine how to read a raidz block
325 * 'raidz_expanding' boolean: present during reflow and removed after completion
326 * used during a spa import to resume an unfinished
327 * expansion
328 *
329 * And finally the VDEVs top zap adds the following informational entries:
330 * VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE
331 * VDEV_TOP_ZAP_RAIDZ_EXPAND_START_TIME
332 * VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME
333 * VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED
334 */
335
336 /*
337 * For testing only: pause the raidz expansion after reflowing this amount.
338 * (accessed by ZTS and ztest)
339 */
340 #ifdef _KERNEL
341 static
342 #endif /* _KERNEL */
343 unsigned long raidz_expand_max_reflow_bytes = 0;
344
345 /*
346 * For testing only: pause the raidz expansion at a certain point.
347 */
348 uint_t raidz_expand_pause_point = 0;
349
350 /*
351 * This represents the duration for a slow drive read sit out.
352 */
353 static unsigned long vdev_read_sit_out_secs = 600;
354
355 /*
356 * How often each RAID-Z and dRAID vdev will check for slow disk outliers.
357 * Increasing this interval will reduce the sensitivity of detection (since all
358 * I/Os since the last check are included in the statistics), but will slow the
359 * response to a disk developing a problem.
360 *
361 * Defaults to once per second; setting extremely small values may cause
362 * negative performance effects.
363 */
364 static hrtime_t vdev_raidz_outlier_check_interval_ms = 1000;
365
366 /*
367 * When performing slow outlier checks for RAID-Z and dRAID vdevs, this value is
368 * used to determine how far out an outlier must be before it counts as an event
369 * worth consdering.
370 *
371 * Smaller values will result in more aggressive sitting out of disks that may
372 * have problems, but may significantly increase the rate of spurious sit-outs.
373 */
374 static uint32_t vdev_raidz_outlier_insensitivity = 50;
375
376 /*
377 * Maximum amount of copy io's outstanding at once.
378 */
379 #ifdef _ILP32
380 static unsigned long raidz_expand_max_copy_bytes = SPA_MAXBLOCKSIZE;
381 #else
382 static unsigned long raidz_expand_max_copy_bytes = 10 * SPA_MAXBLOCKSIZE;
383 #endif
384
385 /*
386 * Apply raidz map abds aggregation if the number of rows in the map is equal
387 * or greater than the value below.
388 */
389 static unsigned long raidz_io_aggregate_rows = 4;
390
391 /*
392 * Automatically start a pool scrub when a RAIDZ expansion completes in
393 * order to verify the checksums of all blocks which have been copied
394 * during the expansion. Automatic scrubbing is enabled by default and
395 * is strongly recommended.
396 */
397 static int zfs_scrub_after_expand = 1;
398
399 /*
400 * If there are errors when writing, but few enough that the data is
401 * recoverable, then ZFS used to silently move on, leaving the data not 100%
402 * redundant. If this tunable is set, we issue a read after that case occurs,
403 * allowing the normal error recovery process to handle it.
404 *
405 * NOTE: Currently applies only to raidz and draid.
406 */
407 static int zfs_scrub_partial_writes = 1;
408
409 static void
vdev_raidz_row_free(raidz_row_t * rr)410 vdev_raidz_row_free(raidz_row_t *rr)
411 {
412 abd_t *dabd = rr->rr_col[rr->rr_firstdatacol].rc_abd;
413 for (int c = 0; c < rr->rr_firstdatacol; c++) {
414 raidz_col_t *rc = &rr->rr_col[c];
415
416 if (rc->rc_size != 0 && rc->rc_abd != dabd)
417 abd_free(rc->rc_abd);
418 if (rc->rc_orig_data != NULL)
419 abd_free(rc->rc_orig_data);
420 }
421 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
422 raidz_col_t *rc = &rr->rr_col[c];
423
424 if (rc->rc_size != 0)
425 abd_free(rc->rc_abd);
426 if (rc->rc_orig_data != NULL)
427 abd_free(rc->rc_orig_data);
428 }
429
430 if (rr->rr_abd_empty != NULL)
431 abd_free(rr->rr_abd_empty);
432
433 kmem_free(rr, offsetof(raidz_row_t, rr_col[rr->rr_scols]));
434 }
435
436 void
vdev_raidz_map_free(raidz_map_t * rm)437 vdev_raidz_map_free(raidz_map_t *rm)
438 {
439 for (int i = 0; i < rm->rm_nrows; i++)
440 vdev_raidz_row_free(rm->rm_row[i]);
441
442 if (rm->rm_nphys_cols) {
443 for (int i = 0; i < rm->rm_nphys_cols; i++) {
444 if (rm->rm_phys_col[i].rc_abd != NULL)
445 abd_free(rm->rm_phys_col[i].rc_abd);
446 }
447
448 kmem_free(rm->rm_phys_col, sizeof (raidz_col_t) *
449 rm->rm_nphys_cols);
450 }
451
452 ASSERT0P(rm->rm_lr);
453 kmem_free(rm, offsetof(raidz_map_t, rm_row[rm->rm_nrows]));
454 }
455
456 static void
vdev_raidz_map_free_vsd(zio_t * zio)457 vdev_raidz_map_free_vsd(zio_t *zio)
458 {
459 raidz_map_t *rm = zio->io_vsd;
460
461 vdev_raidz_map_free(rm);
462 }
463
464 static int
vdev_raidz_reflow_compare(const void * x1,const void * x2)465 vdev_raidz_reflow_compare(const void *x1, const void *x2)
466 {
467 const reflow_node_t *l = x1;
468 const reflow_node_t *r = x2;
469
470 return (TREE_CMP(l->re_txg, r->re_txg));
471 }
472
473 const zio_vsd_ops_t vdev_raidz_vsd_ops = {
474 .vsd_free = vdev_raidz_map_free_vsd,
475 };
476
477 raidz_row_t *
vdev_raidz_row_alloc(int cols,zio_t * zio)478 vdev_raidz_row_alloc(int cols, zio_t *zio)
479 {
480 raidz_row_t *rr =
481 kmem_zalloc(offsetof(raidz_row_t, rr_col[cols]), KM_SLEEP);
482
483 rr->rr_cols = cols;
484 rr->rr_scols = cols;
485
486 for (int c = 0; c < cols; c++) {
487 raidz_col_t *rc = &rr->rr_col[c];
488 rc->rc_shadow_devidx = INT_MAX;
489 rc->rc_shadow_offset = UINT64_MAX;
490 /*
491 * We can not allow self healing to take place for Direct I/O
492 * reads. There is nothing that stops the buffer contents from
493 * being manipulated while the I/O is in flight. It is possible
494 * that the checksum could be verified on the buffer and then
495 * the contents of that buffer are manipulated afterwards. This
496 * could lead to bad data being written out during self
497 * healing.
498 */
499 if (!(zio->io_flags & ZIO_FLAG_DIO_READ))
500 rc->rc_allow_repair = 1;
501 }
502 return (rr);
503 }
504
505 static void
vdev_raidz_map_alloc_write(zio_t * zio,raidz_map_t * rm,uint64_t ashift)506 vdev_raidz_map_alloc_write(zio_t *zio, raidz_map_t *rm, uint64_t ashift)
507 {
508 int c;
509 int nwrapped = 0;
510 uint64_t off = 0;
511 raidz_row_t *rr = rm->rm_row[0];
512
513 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
514 ASSERT3U(rm->rm_nrows, ==, 1);
515
516 /*
517 * Pad any parity columns with additional space to account for skip
518 * sectors.
519 */
520 if (rm->rm_skipstart < rr->rr_firstdatacol) {
521 ASSERT0(rm->rm_skipstart);
522 nwrapped = rm->rm_nskip;
523 } else if (rr->rr_scols < (rm->rm_skipstart + rm->rm_nskip)) {
524 nwrapped =
525 (rm->rm_skipstart + rm->rm_nskip) % rr->rr_scols;
526 }
527
528 /*
529 * Optional single skip sectors (rc_size == 0) will be handled in
530 * vdev_raidz_io_start_write().
531 */
532 int skipped = rr->rr_scols - rr->rr_cols;
533
534 /*
535 * When there is only a single data column the parity is a copy of
536 * it, so point all parity columns at the data ABD directly to avoid
537 * allocating buffers and computing parity.
538 */
539 if (rr->rr_cols == rr->rr_firstdatacol + 1) {
540 ASSERT0(nwrapped);
541 ASSERT0(rm->rm_nskip);
542 raidz_col_t *dc = &rr->rr_col[rr->rr_firstdatacol];
543 dc->rc_abd = abd_get_offset_struct(&dc->rc_abdstruct,
544 zio->io_abd, 0, dc->rc_size);
545 for (c = 0; c < rr->rr_firstdatacol; c++)
546 rr->rr_col[c].rc_abd = dc->rc_abd;
547 return;
548 }
549
550 /* Allocate buffers for the parity columns */
551 for (c = 0; c < rr->rr_firstdatacol; c++) {
552 raidz_col_t *rc = &rr->rr_col[c];
553
554 /*
555 * Parity columns will pad out a linear ABD to account for
556 * the skip sector. A linear ABD is used here because
557 * parity calculations use the ABD buffer directly to calculate
558 * parity. This avoids doing a memcpy back to the ABD after the
559 * parity has been calculated. By issuing the parity column
560 * with the skip sector we can reduce contention on the child
561 * VDEV queue locks (vq_lock).
562 */
563 if (c < nwrapped) {
564 rc->rc_abd = abd_alloc_linear_struct(&rc->rc_abdstruct,
565 rc->rc_size + (1ULL << ashift), B_FALSE);
566 abd_zero_off(rc->rc_abd, rc->rc_size, 1ULL << ashift);
567 skipped++;
568 } else {
569 rc->rc_abd = abd_alloc_linear_struct(&rc->rc_abdstruct,
570 rc->rc_size, B_FALSE);
571 }
572 }
573
574 for (off = 0; c < rr->rr_cols; c++) {
575 raidz_col_t *rc = &rr->rr_col[c];
576 abd_t *abd = abd_get_offset_struct(&rc->rc_abdstruct,
577 zio->io_abd, off, rc->rc_size);
578
579 /*
580 * Generate I/O for skip sectors to improve aggregation
581 * continuity. We will use gang ABD's to reduce contention
582 * on the child VDEV queue locks (vq_lock) by issuing
583 * a single I/O that contains the data and skip sector.
584 *
585 * It is important to make sure that rc_size is not updated
586 * even though we are adding a skip sector to the ABD. When
587 * calculating the parity in vdev_raidz_generate_parity_row()
588 * the rc_size is used to iterate through the ABD's. We can
589 * not have zero'd out skip sectors used for calculating
590 * parity for raidz, because those same sectors are not used
591 * during reconstruction.
592 */
593 if (c >= rm->rm_skipstart && skipped < rm->rm_nskip) {
594 rc->rc_abd = abd_alloc_gang();
595 abd_gang_add(rc->rc_abd, abd, B_TRUE);
596 abd_gang_add(rc->rc_abd,
597 abd_get_zeros(1ULL << ashift), B_TRUE);
598 skipped++;
599 } else {
600 rc->rc_abd = abd;
601 }
602 off += rc->rc_size;
603 }
604
605 ASSERT3U(off, ==, zio->io_size);
606 ASSERT3S(skipped, ==, rm->rm_nskip);
607 }
608
609 static void
vdev_raidz_map_alloc_read(zio_t * zio,raidz_map_t * rm)610 vdev_raidz_map_alloc_read(zio_t *zio, raidz_map_t *rm)
611 {
612 int c;
613 raidz_row_t *rr = rm->rm_row[0];
614
615 ASSERT3U(rm->rm_nrows, ==, 1);
616
617 /* Allocate buffers for the parity columns */
618 for (c = 0; c < rr->rr_firstdatacol; c++) {
619 raidz_col_t *rc = &rr->rr_col[c];
620 rc->rc_abd = abd_alloc_linear_struct(&rc->rc_abdstruct,
621 rc->rc_size, B_FALSE);
622 }
623
624 for (uint64_t off = 0; c < rr->rr_cols; c++) {
625 raidz_col_t *rc = &rr->rr_col[c];
626 rc->rc_abd = abd_get_offset_struct(&rc->rc_abdstruct,
627 zio->io_abd, off, rc->rc_size);
628 off += rc->rc_size;
629 }
630 }
631
632 /*
633 * Divides the IO evenly across all child vdevs; usually, dcols is
634 * the number of children in the target vdev.
635 *
636 * Avoid inlining the function to keep vdev_raidz_io_start(), which
637 * is this functions only caller, as small as possible on the stack.
638 */
639 noinline raidz_map_t *
vdev_raidz_map_alloc(zio_t * zio,uint64_t ashift,uint64_t dcols,uint64_t nparity)640 vdev_raidz_map_alloc(zio_t *zio, uint64_t ashift, uint64_t dcols,
641 uint64_t nparity)
642 {
643 raidz_row_t *rr;
644 /* The starting RAIDZ (parent) vdev sector of the block. */
645 uint64_t b = zio->io_offset >> ashift;
646 /* The zio's size in units of the vdev's minimum sector size. */
647 uint64_t s = zio->io_size >> ashift;
648 /* The first column for this stripe. */
649 uint64_t f = b % dcols;
650 /* The starting byte offset on each child vdev. */
651 uint64_t o = (b / dcols) << ashift;
652 uint64_t acols, scols;
653
654 raidz_map_t *rm =
655 kmem_zalloc(offsetof(raidz_map_t, rm_row[1]), KM_SLEEP);
656 rm->rm_nrows = 1;
657
658 /*
659 * "Quotient": The number of data sectors for this stripe on all but
660 * the "big column" child vdevs that also contain "remainder" data.
661 */
662 uint64_t q = s / (dcols - nparity);
663
664 /*
665 * "Remainder": The number of partial stripe data sectors in this I/O.
666 * This will add a sector to some, but not all, child vdevs.
667 */
668 uint64_t r = s - q * (dcols - nparity);
669
670 /* The number of "big columns" - those which contain remainder data. */
671 uint64_t bc = (r == 0 ? 0 : r + nparity);
672
673 /*
674 * The total number of data and parity sectors associated with
675 * this I/O.
676 */
677 uint64_t tot = s + nparity * (q + (r == 0 ? 0 : 1));
678
679 /*
680 * acols: The columns that will be accessed.
681 * scols: The columns that will be accessed or skipped.
682 */
683 if (q == 0) {
684 /* Our I/O request doesn't span all child vdevs. */
685 acols = bc;
686 scols = MIN(dcols, roundup(bc, nparity + 1));
687 } else {
688 acols = dcols;
689 scols = dcols;
690 }
691
692 ASSERT3U(acols, <=, scols);
693 rr = vdev_raidz_row_alloc(scols, zio);
694 rm->rm_row[0] = rr;
695 rr->rr_cols = acols;
696 rr->rr_bigcols = bc;
697 rr->rr_firstdatacol = nparity;
698 #ifdef ZFS_DEBUG
699 rr->rr_offset = zio->io_offset;
700 rr->rr_size = zio->io_size;
701 #endif
702
703 uint64_t asize = 0;
704
705 for (uint64_t c = 0; c < scols; c++) {
706 raidz_col_t *rc = &rr->rr_col[c];
707 uint64_t col = f + c;
708 uint64_t coff = o;
709 if (col >= dcols) {
710 col -= dcols;
711 coff += 1ULL << ashift;
712 }
713 rc->rc_devidx = col;
714 rc->rc_offset = coff;
715
716 if (c >= acols)
717 rc->rc_size = 0;
718 else if (c < bc)
719 rc->rc_size = (q + 1) << ashift;
720 else
721 rc->rc_size = q << ashift;
722
723 asize += rc->rc_size;
724 }
725
726 ASSERT3U(asize, ==, tot << ashift);
727 rm->rm_nskip = roundup(tot, nparity + 1) - tot;
728 rm->rm_skipstart = bc;
729
730 /*
731 * If all data stored spans all columns, there's a danger that parity
732 * will always be on the same device and, since parity isn't read
733 * during normal operation, that device's I/O bandwidth won't be
734 * used effectively. We therefore switch the parity every 1MB.
735 *
736 * ... at least that was, ostensibly, the theory. As a practical
737 * matter unless we juggle the parity between all devices evenly, we
738 * won't see any benefit. Further, occasional writes that aren't a
739 * multiple of the LCM of the number of children and the minimum
740 * stripe width are sufficient to avoid pessimal behavior.
741 * Unfortunately, this decision created an implicit on-disk format
742 * requirement that we need to support for all eternity, but only
743 * for single-parity RAID-Z.
744 *
745 * If we intend to skip a sector in the zeroth column for padding
746 * we must make sure to note this swap. We will never intend to
747 * skip the first column since at least one data and one parity
748 * column must appear in each row.
749 */
750 ASSERT(rr->rr_cols >= 2);
751 ASSERT(rr->rr_col[0].rc_size == rr->rr_col[1].rc_size);
752
753 if (rr->rr_firstdatacol == 1 && (zio->io_offset & (1ULL << 20))) {
754 uint64_t devidx = rr->rr_col[0].rc_devidx;
755 o = rr->rr_col[0].rc_offset;
756 rr->rr_col[0].rc_devidx = rr->rr_col[1].rc_devidx;
757 rr->rr_col[0].rc_offset = rr->rr_col[1].rc_offset;
758 rr->rr_col[1].rc_devidx = devidx;
759 rr->rr_col[1].rc_offset = o;
760 if (rm->rm_skipstart == 0)
761 rm->rm_skipstart = 1;
762 }
763
764 if (zio->io_type == ZIO_TYPE_WRITE) {
765 vdev_raidz_map_alloc_write(zio, rm, ashift);
766 } else {
767 vdev_raidz_map_alloc_read(zio, rm);
768 }
769 /* init RAIDZ parity ops */
770 rm->rm_ops = vdev_raidz_math_get_ops();
771
772 return (rm);
773 }
774
775 /*
776 * Everything before reflow_offset_synced should have been moved to the new
777 * location (read and write completed). However, this may not yet be reflected
778 * in the on-disk format (e.g. raidz_reflow_sync() has been called but the
779 * uberblock has not yet been written). If reflow is not in progress,
780 * reflow_offset_synced should be UINT64_MAX. For each row, if the row is
781 * entirely before reflow_offset_synced, it will come from the new location.
782 * Otherwise this row will come from the old location. Therefore, rows that
783 * straddle the reflow_offset_synced will come from the old location.
784 *
785 * For writes, reflow_offset_next is the next offset to copy. If a sector has
786 * been copied, but not yet reflected in the on-disk progress
787 * (reflow_offset_synced), it will also be written to the new (already copied)
788 * offset.
789 */
790 noinline raidz_map_t *
vdev_raidz_map_alloc_expanded(zio_t * zio,uint64_t ashift,uint64_t physical_cols,uint64_t logical_cols,uint64_t nparity,uint64_t reflow_offset_synced,uint64_t reflow_offset_next,boolean_t use_scratch)791 vdev_raidz_map_alloc_expanded(zio_t *zio,
792 uint64_t ashift, uint64_t physical_cols, uint64_t logical_cols,
793 uint64_t nparity, uint64_t reflow_offset_synced,
794 uint64_t reflow_offset_next, boolean_t use_scratch)
795 {
796 abd_t *abd = zio->io_abd;
797 uint64_t offset = zio->io_offset;
798 uint64_t size = zio->io_size;
799
800 /* The zio's size in units of the vdev's minimum sector size. */
801 uint64_t s = size >> ashift;
802
803 /*
804 * "Quotient": The number of data sectors for this stripe on all but
805 * the "big column" child vdevs that also contain "remainder" data.
806 * AKA "full rows"
807 */
808 uint64_t q = s / (logical_cols - nparity);
809
810 /*
811 * "Remainder": The number of partial stripe data sectors in this I/O.
812 * This will add a sector to some, but not all, child vdevs.
813 */
814 uint64_t r = s - q * (logical_cols - nparity);
815
816 /* The number of "big columns" - those which contain remainder data. */
817 uint64_t bc = (r == 0 ? 0 : r + nparity);
818
819 /*
820 * The total number of data and parity sectors associated with
821 * this I/O.
822 */
823 uint64_t tot = s + nparity * (q + (r == 0 ? 0 : 1));
824
825 /* How many rows contain data (not skip) */
826 uint64_t rows = howmany(tot, logical_cols);
827 int cols = MIN(tot, logical_cols);
828
829 raidz_map_t *rm =
830 kmem_zalloc(offsetof(raidz_map_t, rm_row[rows]),
831 KM_SLEEP);
832 rm->rm_nrows = rows;
833 rm->rm_nskip = roundup(tot, nparity + 1) - tot;
834 rm->rm_skipstart = bc;
835 uint64_t asize = 0;
836
837 for (uint64_t row = 0; row < rows; row++) {
838 boolean_t row_use_scratch = B_FALSE;
839 raidz_row_t *rr = vdev_raidz_row_alloc(cols, zio);
840 rm->rm_row[row] = rr;
841
842 /* The starting RAIDZ (parent) vdev sector of the row. */
843 uint64_t b = (offset >> ashift) + row * logical_cols;
844
845 /*
846 * If we are in the middle of a reflow, and the copying has
847 * not yet completed for any part of this row, then use the
848 * old location of this row. Note that reflow_offset_synced
849 * reflects the i/o that's been completed, because it's
850 * updated by a synctask, after zio_wait(spa_txg_zio[]).
851 * This is sufficient for our check, even if that progress
852 * has not yet been recorded to disk (reflected in
853 * spa_ubsync). Also note that we consider the last row to
854 * be "full width" (`cols`-wide rather than `bc`-wide) for
855 * this calculation. This causes a tiny bit of unnecessary
856 * double-writes but is safe and simpler to calculate.
857 */
858 int row_phys_cols = physical_cols;
859 if (b + cols > reflow_offset_synced >> ashift)
860 row_phys_cols--;
861 else if (use_scratch)
862 row_use_scratch = B_TRUE;
863
864 /* starting child of this row */
865 uint64_t child_id = b % row_phys_cols;
866 /* The starting byte offset on each child vdev. */
867 uint64_t child_offset = (b / row_phys_cols) << ashift;
868
869 /*
870 * Note, rr_cols is the entire width of the block, even
871 * if this row is shorter. This is needed because parity
872 * generation (for Q and R) needs to know the entire width,
873 * because it treats the short row as though it was
874 * full-width (and the "phantom" sectors were zero-filled).
875 *
876 * Another approach to this would be to set cols shorter
877 * (to just the number of columns that we might do i/o to)
878 * and have another mechanism to tell the parity generation
879 * about the "entire width". Reconstruction (at least
880 * vdev_raidz_reconstruct_general()) would also need to
881 * know about the "entire width".
882 */
883 rr->rr_firstdatacol = nparity;
884 #ifdef ZFS_DEBUG
885 /*
886 * note: rr_size is PSIZE, not ASIZE
887 */
888 rr->rr_offset = b << ashift;
889 rr->rr_size = (rr->rr_cols - rr->rr_firstdatacol) << ashift;
890 #endif
891
892 for (int c = 0; c < rr->rr_cols; c++, child_id++) {
893 if (child_id >= row_phys_cols) {
894 child_id -= row_phys_cols;
895 child_offset += 1ULL << ashift;
896 }
897 raidz_col_t *rc = &rr->rr_col[c];
898 rc->rc_devidx = child_id;
899 rc->rc_offset = child_offset;
900
901 /*
902 * Get this from the scratch space if appropriate.
903 * This only happens if we crashed in the middle of
904 * raidz_reflow_scratch_sync() (while it's running,
905 * the rangelock prevents us from doing concurrent
906 * io), and even then only during zpool import or
907 * when the pool is imported readonly.
908 */
909 if (row_use_scratch)
910 rc->rc_offset -= VDEV_BOOT_SIZE;
911
912 uint64_t dc = c - rr->rr_firstdatacol;
913 if (c < rr->rr_firstdatacol) {
914 rc->rc_size = 1ULL << ashift;
915
916 /*
917 * Parity sectors' rc_abd's are set below
918 * after determining if this is an aggregation.
919 */
920 } else if (row == rows - 1 && bc != 0 && c >= bc) {
921 /*
922 * Past the end of the block (even including
923 * skip sectors). This sector is part of the
924 * map so that we have full rows for p/q parity
925 * generation.
926 */
927 rc->rc_size = 0;
928 rc->rc_abd = NULL;
929 } else {
930 /* "data column" (col excluding parity) */
931 uint64_t off;
932
933 if (c < bc || r == 0) {
934 off = dc * rows + row;
935 } else {
936 off = r * rows +
937 (dc - r) * (rows - 1) + row;
938 }
939 rc->rc_size = 1ULL << ashift;
940 rc->rc_abd = abd_get_offset_struct(
941 &rc->rc_abdstruct, abd, off << ashift,
942 rc->rc_size);
943 }
944
945 if (rc->rc_size == 0)
946 continue;
947
948 /*
949 * If any part of this row is in both old and new
950 * locations, the primary location is the old
951 * location. If this sector was already copied to the
952 * new location, we need to also write to the new,
953 * "shadow" location.
954 *
955 * Note, `row_phys_cols != physical_cols` indicates
956 * that the primary location is the old location.
957 * `b+c < reflow_offset_next` indicates that the copy
958 * to the new location has been initiated. We know
959 * that the copy has completed because we have the
960 * rangelock, which is held exclusively while the
961 * copy is in progress.
962 */
963 if (row_use_scratch ||
964 (row_phys_cols != physical_cols &&
965 b + c < reflow_offset_next >> ashift)) {
966 rc->rc_shadow_devidx = (b + c) % physical_cols;
967 rc->rc_shadow_offset =
968 ((b + c) / physical_cols) << ashift;
969 if (row_use_scratch)
970 rc->rc_shadow_offset -= VDEV_BOOT_SIZE;
971 }
972
973 asize += rc->rc_size;
974 }
975
976 /*
977 * See comment in vdev_raidz_map_alloc()
978 */
979 if (rr->rr_firstdatacol == 1 && rr->rr_cols > 1 &&
980 (offset & (1ULL << 20))) {
981 ASSERT(rr->rr_cols >= 2);
982 ASSERT(rr->rr_col[0].rc_size == rr->rr_col[1].rc_size);
983
984 int devidx0 = rr->rr_col[0].rc_devidx;
985 uint64_t offset0 = rr->rr_col[0].rc_offset;
986 int shadow_devidx0 = rr->rr_col[0].rc_shadow_devidx;
987 uint64_t shadow_offset0 =
988 rr->rr_col[0].rc_shadow_offset;
989
990 rr->rr_col[0].rc_devidx = rr->rr_col[1].rc_devidx;
991 rr->rr_col[0].rc_offset = rr->rr_col[1].rc_offset;
992 rr->rr_col[0].rc_shadow_devidx =
993 rr->rr_col[1].rc_shadow_devidx;
994 rr->rr_col[0].rc_shadow_offset =
995 rr->rr_col[1].rc_shadow_offset;
996
997 rr->rr_col[1].rc_devidx = devidx0;
998 rr->rr_col[1].rc_offset = offset0;
999 rr->rr_col[1].rc_shadow_devidx = shadow_devidx0;
1000 rr->rr_col[1].rc_shadow_offset = shadow_offset0;
1001 }
1002 }
1003 ASSERT3U(asize, ==, tot << ashift);
1004
1005 /*
1006 * Determine if the block is contiguous, in which case we can use
1007 * an aggregation.
1008 */
1009 if (rows >= raidz_io_aggregate_rows) {
1010 rm->rm_nphys_cols = physical_cols;
1011 rm->rm_phys_col =
1012 kmem_zalloc(sizeof (raidz_col_t) * rm->rm_nphys_cols,
1013 KM_SLEEP);
1014
1015 /*
1016 * Determine the aggregate io's offset and size, and check
1017 * that the io is contiguous.
1018 */
1019 for (int i = 0;
1020 i < rm->rm_nrows && rm->rm_phys_col != NULL; i++) {
1021 raidz_row_t *rr = rm->rm_row[i];
1022 for (int c = 0; c < rr->rr_cols; c++) {
1023 raidz_col_t *rc = &rr->rr_col[c];
1024 raidz_col_t *prc =
1025 &rm->rm_phys_col[rc->rc_devidx];
1026
1027 if (rc->rc_size == 0)
1028 continue;
1029
1030 if (prc->rc_size == 0) {
1031 ASSERT0(prc->rc_offset);
1032 prc->rc_offset = rc->rc_offset;
1033 } else if (prc->rc_offset + prc->rc_size !=
1034 rc->rc_offset) {
1035 /*
1036 * This block is not contiguous and
1037 * therefore can't be aggregated.
1038 * This is expected to be rare, so
1039 * the cost of allocating and then
1040 * freeing rm_phys_col is not
1041 * significant.
1042 */
1043 kmem_free(rm->rm_phys_col,
1044 sizeof (raidz_col_t) *
1045 rm->rm_nphys_cols);
1046 rm->rm_phys_col = NULL;
1047 rm->rm_nphys_cols = 0;
1048 break;
1049 }
1050 prc->rc_size += rc->rc_size;
1051 }
1052 }
1053 }
1054 if (rm->rm_phys_col != NULL) {
1055 /*
1056 * Allocate aggregate ABD's.
1057 */
1058 for (int i = 0; i < rm->rm_nphys_cols; i++) {
1059 raidz_col_t *prc = &rm->rm_phys_col[i];
1060
1061 prc->rc_devidx = i;
1062
1063 if (prc->rc_size == 0)
1064 continue;
1065
1066 prc->rc_abd =
1067 abd_alloc_linear_struct(&prc->rc_abdstruct,
1068 prc->rc_size, B_FALSE);
1069 }
1070
1071 /*
1072 * Point the parity abd's into the aggregate abd's.
1073 */
1074 for (int i = 0; i < rm->rm_nrows; i++) {
1075 raidz_row_t *rr = rm->rm_row[i];
1076 for (int c = 0; c < rr->rr_firstdatacol; c++) {
1077 raidz_col_t *rc = &rr->rr_col[c];
1078 raidz_col_t *prc =
1079 &rm->rm_phys_col[rc->rc_devidx];
1080 rc->rc_abd =
1081 abd_get_offset_struct(&rc->rc_abdstruct,
1082 prc->rc_abd,
1083 rc->rc_offset - prc->rc_offset,
1084 rc->rc_size);
1085 }
1086 }
1087 } else {
1088 /*
1089 * Allocate new abd's for the parity sectors.
1090 */
1091 for (int i = 0; i < rm->rm_nrows; i++) {
1092 raidz_row_t *rr = rm->rm_row[i];
1093 for (int c = 0; c < rr->rr_firstdatacol; c++) {
1094 raidz_col_t *rc = &rr->rr_col[c];
1095 rc->rc_abd =
1096 abd_alloc_linear_struct(&rc->rc_abdstruct,
1097 rc->rc_size, B_TRUE);
1098 }
1099 }
1100 }
1101 /* init RAIDZ parity ops */
1102 rm->rm_ops = vdev_raidz_math_get_ops();
1103
1104 return (rm);
1105 }
1106
1107 struct pqr_struct {
1108 uint64_t *p;
1109 uint64_t *q;
1110 uint64_t *r;
1111 };
1112
1113 static int
vdev_raidz_p_func(void * buf,size_t size,void * private)1114 vdev_raidz_p_func(void *buf, size_t size, void *private)
1115 {
1116 struct pqr_struct *pqr = private;
1117 const uint64_t *src = buf;
1118 int cnt = size / sizeof (src[0]);
1119
1120 ASSERT(pqr->p && !pqr->q && !pqr->r);
1121
1122 for (int i = 0; i < cnt; i++, src++, pqr->p++)
1123 *pqr->p ^= *src;
1124
1125 return (0);
1126 }
1127
1128 static int
vdev_raidz_pq_func(void * buf,size_t size,void * private)1129 vdev_raidz_pq_func(void *buf, size_t size, void *private)
1130 {
1131 struct pqr_struct *pqr = private;
1132 const uint64_t *src = buf;
1133 uint64_t mask;
1134 int cnt = size / sizeof (src[0]);
1135
1136 ASSERT(pqr->p && pqr->q && !pqr->r);
1137
1138 for (int i = 0; i < cnt; i++, src++, pqr->p++, pqr->q++) {
1139 *pqr->p ^= *src;
1140 VDEV_RAIDZ_64MUL_2(*pqr->q, mask);
1141 *pqr->q ^= *src;
1142 }
1143
1144 return (0);
1145 }
1146
1147 static int
vdev_raidz_pqr_func(void * buf,size_t size,void * private)1148 vdev_raidz_pqr_func(void *buf, size_t size, void *private)
1149 {
1150 struct pqr_struct *pqr = private;
1151 const uint64_t *src = buf;
1152 uint64_t mask;
1153 int cnt = size / sizeof (src[0]);
1154
1155 ASSERT(pqr->p && pqr->q && pqr->r);
1156
1157 for (int i = 0; i < cnt; i++, src++, pqr->p++, pqr->q++, pqr->r++) {
1158 *pqr->p ^= *src;
1159 VDEV_RAIDZ_64MUL_2(*pqr->q, mask);
1160 *pqr->q ^= *src;
1161 VDEV_RAIDZ_64MUL_4(*pqr->r, mask);
1162 *pqr->r ^= *src;
1163 }
1164
1165 return (0);
1166 }
1167
1168 static void
vdev_raidz_generate_parity_p(raidz_row_t * rr)1169 vdev_raidz_generate_parity_p(raidz_row_t *rr)
1170 {
1171 uint64_t *p = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd);
1172
1173 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1174 abd_t *src = rr->rr_col[c].rc_abd;
1175
1176 if (c == rr->rr_firstdatacol) {
1177 abd_copy_to_buf(p, src, rr->rr_col[c].rc_size);
1178 } else {
1179 struct pqr_struct pqr = { p, NULL, NULL };
1180 (void) abd_iterate_func(src, 0, rr->rr_col[c].rc_size,
1181 vdev_raidz_p_func, &pqr);
1182 }
1183 }
1184 }
1185
1186 static void
vdev_raidz_generate_parity_pq(raidz_row_t * rr)1187 vdev_raidz_generate_parity_pq(raidz_row_t *rr)
1188 {
1189 uint64_t *p = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd);
1190 uint64_t *q = abd_to_buf(rr->rr_col[VDEV_RAIDZ_Q].rc_abd);
1191 uint64_t pcnt = rr->rr_col[VDEV_RAIDZ_P].rc_size / sizeof (p[0]);
1192 ASSERT(rr->rr_col[VDEV_RAIDZ_P].rc_size ==
1193 rr->rr_col[VDEV_RAIDZ_Q].rc_size);
1194
1195 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1196 abd_t *src = rr->rr_col[c].rc_abd;
1197
1198 uint64_t ccnt = rr->rr_col[c].rc_size / sizeof (p[0]);
1199
1200 if (c == rr->rr_firstdatacol) {
1201 ASSERT(ccnt == pcnt || ccnt == 0);
1202 abd_copy_to_buf(p, src, rr->rr_col[c].rc_size);
1203 (void) memcpy(q, p, rr->rr_col[c].rc_size);
1204
1205 for (uint64_t i = ccnt; i < pcnt; i++) {
1206 p[i] = 0;
1207 q[i] = 0;
1208 }
1209 } else {
1210 struct pqr_struct pqr = { p, q, NULL };
1211
1212 ASSERT(ccnt <= pcnt);
1213 (void) abd_iterate_func(src, 0, rr->rr_col[c].rc_size,
1214 vdev_raidz_pq_func, &pqr);
1215
1216 /*
1217 * Treat short columns as though they are full of 0s.
1218 * Note that there's therefore nothing needed for P.
1219 */
1220 uint64_t mask;
1221 for (uint64_t i = ccnt; i < pcnt; i++) {
1222 VDEV_RAIDZ_64MUL_2(q[i], mask);
1223 }
1224 }
1225 }
1226 }
1227
1228 static void
vdev_raidz_generate_parity_pqr(raidz_row_t * rr)1229 vdev_raidz_generate_parity_pqr(raidz_row_t *rr)
1230 {
1231 uint64_t *p = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd);
1232 uint64_t *q = abd_to_buf(rr->rr_col[VDEV_RAIDZ_Q].rc_abd);
1233 uint64_t *r = abd_to_buf(rr->rr_col[VDEV_RAIDZ_R].rc_abd);
1234 uint64_t pcnt = rr->rr_col[VDEV_RAIDZ_P].rc_size / sizeof (p[0]);
1235 ASSERT(rr->rr_col[VDEV_RAIDZ_P].rc_size ==
1236 rr->rr_col[VDEV_RAIDZ_Q].rc_size);
1237 ASSERT(rr->rr_col[VDEV_RAIDZ_P].rc_size ==
1238 rr->rr_col[VDEV_RAIDZ_R].rc_size);
1239
1240 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1241 abd_t *src = rr->rr_col[c].rc_abd;
1242
1243 uint64_t ccnt = rr->rr_col[c].rc_size / sizeof (p[0]);
1244
1245 if (c == rr->rr_firstdatacol) {
1246 ASSERT(ccnt == pcnt || ccnt == 0);
1247 abd_copy_to_buf(p, src, rr->rr_col[c].rc_size);
1248 (void) memcpy(q, p, rr->rr_col[c].rc_size);
1249 (void) memcpy(r, p, rr->rr_col[c].rc_size);
1250
1251 for (uint64_t i = ccnt; i < pcnt; i++) {
1252 p[i] = 0;
1253 q[i] = 0;
1254 r[i] = 0;
1255 }
1256 } else {
1257 struct pqr_struct pqr = { p, q, r };
1258
1259 ASSERT(ccnt <= pcnt);
1260 (void) abd_iterate_func(src, 0, rr->rr_col[c].rc_size,
1261 vdev_raidz_pqr_func, &pqr);
1262
1263 /*
1264 * Treat short columns as though they are full of 0s.
1265 * Note that there's therefore nothing needed for P.
1266 */
1267 uint64_t mask;
1268 for (uint64_t i = ccnt; i < pcnt; i++) {
1269 VDEV_RAIDZ_64MUL_2(q[i], mask);
1270 VDEV_RAIDZ_64MUL_4(r[i], mask);
1271 }
1272 }
1273 }
1274 }
1275
1276 /*
1277 * Generate RAID parity in the first virtual columns according to the number of
1278 * parity columns available.
1279 */
1280 void
vdev_raidz_generate_parity_row(raidz_map_t * rm,raidz_row_t * rr)1281 vdev_raidz_generate_parity_row(raidz_map_t *rm, raidz_row_t *rr)
1282 {
1283 if (rr->rr_cols == 0) {
1284 /*
1285 * We are handling this block one row at a time (because
1286 * this block has a different logical vs physical width,
1287 * due to RAIDZ expansion), and this is a pad-only row,
1288 * which has no parity.
1289 */
1290 return;
1291 }
1292
1293 /*
1294 * Single data column: parity is the data itself.
1295 */
1296 if (rr->rr_col[VDEV_RAIDZ_P].rc_abd ==
1297 rr->rr_col[rr->rr_firstdatacol].rc_abd)
1298 return;
1299
1300 /* Generate using the new math implementation */
1301 if (vdev_raidz_math_generate(rm, rr) != RAIDZ_ORIGINAL_IMPL)
1302 return;
1303
1304 switch (rr->rr_firstdatacol) {
1305 case 1:
1306 vdev_raidz_generate_parity_p(rr);
1307 break;
1308 case 2:
1309 vdev_raidz_generate_parity_pq(rr);
1310 break;
1311 case 3:
1312 vdev_raidz_generate_parity_pqr(rr);
1313 break;
1314 default:
1315 cmn_err(CE_PANIC, "invalid RAID-Z configuration");
1316 }
1317 }
1318
1319 void
vdev_raidz_generate_parity(raidz_map_t * rm)1320 vdev_raidz_generate_parity(raidz_map_t *rm)
1321 {
1322 for (int i = 0; i < rm->rm_nrows; i++) {
1323 raidz_row_t *rr = rm->rm_row[i];
1324 vdev_raidz_generate_parity_row(rm, rr);
1325 }
1326 }
1327
1328 static int
vdev_raidz_reconst_p_func(void * dbuf,void * sbuf,size_t size,void * private)1329 vdev_raidz_reconst_p_func(void *dbuf, void *sbuf, size_t size, void *private)
1330 {
1331 (void) private;
1332 uint64_t *dst = dbuf;
1333 uint64_t *src = sbuf;
1334 int cnt = size / sizeof (src[0]);
1335
1336 for (int i = 0; i < cnt; i++) {
1337 dst[i] ^= src[i];
1338 }
1339
1340 return (0);
1341 }
1342
1343 static int
vdev_raidz_reconst_q_pre_func(void * dbuf,void * sbuf,size_t size,void * private)1344 vdev_raidz_reconst_q_pre_func(void *dbuf, void *sbuf, size_t size,
1345 void *private)
1346 {
1347 (void) private;
1348 uint64_t *dst = dbuf;
1349 uint64_t *src = sbuf;
1350 uint64_t mask;
1351 int cnt = size / sizeof (dst[0]);
1352
1353 for (int i = 0; i < cnt; i++, dst++, src++) {
1354 VDEV_RAIDZ_64MUL_2(*dst, mask);
1355 *dst ^= *src;
1356 }
1357
1358 return (0);
1359 }
1360
1361 static int
vdev_raidz_reconst_q_pre_tail_func(void * buf,size_t size,void * private)1362 vdev_raidz_reconst_q_pre_tail_func(void *buf, size_t size, void *private)
1363 {
1364 (void) private;
1365 uint64_t *dst = buf;
1366 uint64_t mask;
1367 int cnt = size / sizeof (dst[0]);
1368
1369 for (int i = 0; i < cnt; i++, dst++) {
1370 /* same operation as vdev_raidz_reconst_q_pre_func() on dst */
1371 VDEV_RAIDZ_64MUL_2(*dst, mask);
1372 }
1373
1374 return (0);
1375 }
1376
1377 struct reconst_q_struct {
1378 uint64_t *q;
1379 int exp;
1380 };
1381
1382 static int
vdev_raidz_reconst_q_post_func(void * buf,size_t size,void * private)1383 vdev_raidz_reconst_q_post_func(void *buf, size_t size, void *private)
1384 {
1385 struct reconst_q_struct *rq = private;
1386 uint64_t *dst = buf;
1387 int cnt = size / sizeof (dst[0]);
1388
1389 for (int i = 0; i < cnt; i++, dst++, rq->q++) {
1390 int j;
1391 uint8_t *b;
1392
1393 *dst ^= *rq->q;
1394 for (j = 0, b = (uint8_t *)dst; j < 8; j++, b++) {
1395 *b = vdev_raidz_exp2(*b, rq->exp);
1396 }
1397 }
1398
1399 return (0);
1400 }
1401
1402 struct reconst_pq_struct {
1403 uint8_t *p;
1404 uint8_t *q;
1405 uint8_t *pxy;
1406 uint8_t *qxy;
1407 int aexp;
1408 int bexp;
1409 };
1410
1411 static int
vdev_raidz_reconst_pq_func(void * xbuf,void * ybuf,size_t size,void * private)1412 vdev_raidz_reconst_pq_func(void *xbuf, void *ybuf, size_t size, void *private)
1413 {
1414 struct reconst_pq_struct *rpq = private;
1415 uint8_t *xd = xbuf;
1416 uint8_t *yd = ybuf;
1417
1418 for (int i = 0; i < size;
1419 i++, rpq->p++, rpq->q++, rpq->pxy++, rpq->qxy++, xd++, yd++) {
1420 *xd = vdev_raidz_exp2(*rpq->p ^ *rpq->pxy, rpq->aexp) ^
1421 vdev_raidz_exp2(*rpq->q ^ *rpq->qxy, rpq->bexp);
1422 *yd = *rpq->p ^ *rpq->pxy ^ *xd;
1423 }
1424
1425 return (0);
1426 }
1427
1428 static int
vdev_raidz_reconst_pq_tail_func(void * xbuf,size_t size,void * private)1429 vdev_raidz_reconst_pq_tail_func(void *xbuf, size_t size, void *private)
1430 {
1431 struct reconst_pq_struct *rpq = private;
1432 uint8_t *xd = xbuf;
1433
1434 for (int i = 0; i < size;
1435 i++, rpq->p++, rpq->q++, rpq->pxy++, rpq->qxy++, xd++) {
1436 /* same operation as vdev_raidz_reconst_pq_func() on xd */
1437 *xd = vdev_raidz_exp2(*rpq->p ^ *rpq->pxy, rpq->aexp) ^
1438 vdev_raidz_exp2(*rpq->q ^ *rpq->qxy, rpq->bexp);
1439 }
1440
1441 return (0);
1442 }
1443
1444 static void
vdev_raidz_reconstruct_p(raidz_row_t * rr,int * tgts,int ntgts)1445 vdev_raidz_reconstruct_p(raidz_row_t *rr, int *tgts, int ntgts)
1446 {
1447 int x = tgts[0];
1448 abd_t *dst, *src;
1449
1450 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT)
1451 zfs_dbgmsg("reconstruct_p(rm=%px x=%u)", rr, x);
1452
1453 ASSERT3U(ntgts, ==, 1);
1454 ASSERT3U(x, >=, rr->rr_firstdatacol);
1455 ASSERT3U(x, <, rr->rr_cols);
1456
1457 ASSERT3U(rr->rr_col[x].rc_size, <=, rr->rr_col[VDEV_RAIDZ_P].rc_size);
1458
1459 src = rr->rr_col[VDEV_RAIDZ_P].rc_abd;
1460 dst = rr->rr_col[x].rc_abd;
1461
1462 abd_copy_from_buf(dst, abd_to_buf(src), rr->rr_col[x].rc_size);
1463
1464 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1465 uint64_t size = MIN(rr->rr_col[x].rc_size,
1466 rr->rr_col[c].rc_size);
1467
1468 src = rr->rr_col[c].rc_abd;
1469
1470 if (c == x)
1471 continue;
1472
1473 (void) abd_iterate_func2(dst, src, 0, 0, size,
1474 vdev_raidz_reconst_p_func, NULL);
1475 }
1476 }
1477
1478 static void
vdev_raidz_reconstruct_q(raidz_row_t * rr,int * tgts,int ntgts)1479 vdev_raidz_reconstruct_q(raidz_row_t *rr, int *tgts, int ntgts)
1480 {
1481 int x = tgts[0];
1482 int c, exp;
1483 abd_t *dst, *src;
1484
1485 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT)
1486 zfs_dbgmsg("reconstruct_q(rm=%px x=%u)", rr, x);
1487
1488 ASSERT(ntgts == 1);
1489
1490 ASSERT(rr->rr_col[x].rc_size <= rr->rr_col[VDEV_RAIDZ_Q].rc_size);
1491
1492 for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1493 uint64_t size = (c == x) ? 0 : MIN(rr->rr_col[x].rc_size,
1494 rr->rr_col[c].rc_size);
1495
1496 src = rr->rr_col[c].rc_abd;
1497 dst = rr->rr_col[x].rc_abd;
1498
1499 if (c == rr->rr_firstdatacol) {
1500 abd_copy(dst, src, size);
1501 if (rr->rr_col[x].rc_size > size) {
1502 abd_zero_off(dst, size,
1503 rr->rr_col[x].rc_size - size);
1504 }
1505 } else {
1506 ASSERT3U(size, <=, rr->rr_col[x].rc_size);
1507 (void) abd_iterate_func2(dst, src, 0, 0, size,
1508 vdev_raidz_reconst_q_pre_func, NULL);
1509 (void) abd_iterate_func(dst,
1510 size, rr->rr_col[x].rc_size - size,
1511 vdev_raidz_reconst_q_pre_tail_func, NULL);
1512 }
1513 }
1514
1515 src = rr->rr_col[VDEV_RAIDZ_Q].rc_abd;
1516 dst = rr->rr_col[x].rc_abd;
1517 exp = 255 - (rr->rr_cols - 1 - x);
1518
1519 struct reconst_q_struct rq = { abd_to_buf(src), exp };
1520 (void) abd_iterate_func(dst, 0, rr->rr_col[x].rc_size,
1521 vdev_raidz_reconst_q_post_func, &rq);
1522 }
1523
1524 static void
vdev_raidz_reconstruct_pq(raidz_row_t * rr,int * tgts,int ntgts)1525 vdev_raidz_reconstruct_pq(raidz_row_t *rr, int *tgts, int ntgts)
1526 {
1527 uint8_t *p, *q, *pxy, *qxy, tmp, a, b, aexp, bexp;
1528 abd_t *pdata, *qdata;
1529 uint64_t xsize, ysize;
1530 int x = tgts[0];
1531 int y = tgts[1];
1532 abd_t *xd, *yd;
1533
1534 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT)
1535 zfs_dbgmsg("reconstruct_pq(rm=%px x=%u y=%u)", rr, x, y);
1536
1537 ASSERT(ntgts == 2);
1538 ASSERT(x < y);
1539 ASSERT(x >= rr->rr_firstdatacol);
1540 ASSERT(y < rr->rr_cols);
1541
1542 ASSERT(rr->rr_col[x].rc_size >= rr->rr_col[y].rc_size);
1543
1544 /*
1545 * Move the parity data aside -- we're going to compute parity as
1546 * though columns x and y were full of zeros -- Pxy and Qxy. We want to
1547 * reuse the parity generation mechanism without trashing the actual
1548 * parity so we make those columns appear to be full of zeros by
1549 * setting their lengths to zero.
1550 */
1551 pdata = rr->rr_col[VDEV_RAIDZ_P].rc_abd;
1552 qdata = rr->rr_col[VDEV_RAIDZ_Q].rc_abd;
1553 xsize = rr->rr_col[x].rc_size;
1554 ysize = rr->rr_col[y].rc_size;
1555
1556 rr->rr_col[VDEV_RAIDZ_P].rc_abd =
1557 abd_alloc_linear(rr->rr_col[VDEV_RAIDZ_P].rc_size, B_TRUE);
1558 rr->rr_col[VDEV_RAIDZ_Q].rc_abd =
1559 abd_alloc_linear(rr->rr_col[VDEV_RAIDZ_Q].rc_size, B_TRUE);
1560 rr->rr_col[x].rc_size = 0;
1561 rr->rr_col[y].rc_size = 0;
1562
1563 vdev_raidz_generate_parity_pq(rr);
1564
1565 rr->rr_col[x].rc_size = xsize;
1566 rr->rr_col[y].rc_size = ysize;
1567
1568 p = abd_to_buf(pdata);
1569 q = abd_to_buf(qdata);
1570 pxy = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd);
1571 qxy = abd_to_buf(rr->rr_col[VDEV_RAIDZ_Q].rc_abd);
1572 xd = rr->rr_col[x].rc_abd;
1573 yd = rr->rr_col[y].rc_abd;
1574
1575 /*
1576 * We now have:
1577 * Pxy = P + D_x + D_y
1578 * Qxy = Q + 2^(ndevs - 1 - x) * D_x + 2^(ndevs - 1 - y) * D_y
1579 *
1580 * We can then solve for D_x:
1581 * D_x = A * (P + Pxy) + B * (Q + Qxy)
1582 * where
1583 * A = 2^(x - y) * (2^(x - y) + 1)^-1
1584 * B = 2^(ndevs - 1 - x) * (2^(x - y) + 1)^-1
1585 *
1586 * With D_x in hand, we can easily solve for D_y:
1587 * D_y = P + Pxy + D_x
1588 */
1589
1590 a = vdev_raidz_pow2[255 + x - y];
1591 b = vdev_raidz_pow2[255 - (rr->rr_cols - 1 - x)];
1592 tmp = 255 - vdev_raidz_log2[a ^ 1];
1593
1594 aexp = vdev_raidz_log2[vdev_raidz_exp2(a, tmp)];
1595 bexp = vdev_raidz_log2[vdev_raidz_exp2(b, tmp)];
1596
1597 ASSERT3U(xsize, >=, ysize);
1598 struct reconst_pq_struct rpq = { p, q, pxy, qxy, aexp, bexp };
1599
1600 (void) abd_iterate_func2(xd, yd, 0, 0, ysize,
1601 vdev_raidz_reconst_pq_func, &rpq);
1602 (void) abd_iterate_func(xd, ysize, xsize - ysize,
1603 vdev_raidz_reconst_pq_tail_func, &rpq);
1604
1605 abd_free(rr->rr_col[VDEV_RAIDZ_P].rc_abd);
1606 abd_free(rr->rr_col[VDEV_RAIDZ_Q].rc_abd);
1607
1608 /*
1609 * Restore the saved parity data.
1610 */
1611 rr->rr_col[VDEV_RAIDZ_P].rc_abd = pdata;
1612 rr->rr_col[VDEV_RAIDZ_Q].rc_abd = qdata;
1613 }
1614
1615 /*
1616 * In the general case of reconstruction, we must solve the system of linear
1617 * equations defined by the coefficients used to generate parity as well as
1618 * the contents of the data and parity disks. This can be expressed with
1619 * vectors for the original data (D) and the actual data (d) and parity (p)
1620 * and a matrix composed of the identity matrix (I) and a dispersal matrix (V):
1621 *
1622 * __ __ __ __
1623 * | | __ __ | p_0 |
1624 * | V | | D_0 | | p_m-1 |
1625 * | | x | : | = | d_0 |
1626 * | I | | D_n-1 | | : |
1627 * | | ~~ ~~ | d_n-1 |
1628 * ~~ ~~ ~~ ~~
1629 *
1630 * I is simply a square identity matrix of size n, and V is a vandermonde
1631 * matrix defined by the coefficients we chose for the various parity columns
1632 * (1, 2, 4). Note that these values were chosen both for simplicity, speedy
1633 * computation as well as linear separability.
1634 *
1635 * __ __ __ __
1636 * | 1 .. 1 1 1 | | p_0 |
1637 * | 2^n-1 .. 4 2 1 | __ __ | : |
1638 * | 4^n-1 .. 16 4 1 | | D_0 | | p_m-1 |
1639 * | 1 .. 0 0 0 | | D_1 | | d_0 |
1640 * | 0 .. 0 0 0 | x | D_2 | = | d_1 |
1641 * | : : : : | | : | | d_2 |
1642 * | 0 .. 1 0 0 | | D_n-1 | | : |
1643 * | 0 .. 0 1 0 | ~~ ~~ | : |
1644 * | 0 .. 0 0 1 | | d_n-1 |
1645 * ~~ ~~ ~~ ~~
1646 *
1647 * Note that I, V, d, and p are known. To compute D, we must invert the
1648 * matrix and use the known data and parity values to reconstruct the unknown
1649 * data values. We begin by removing the rows in V|I and d|p that correspond
1650 * to failed or missing columns; we then make V|I square (n x n) and d|p
1651 * sized n by removing rows corresponding to unused parity from the bottom up
1652 * to generate (V|I)' and (d|p)'. We can then generate the inverse of (V|I)'
1653 * using Gauss-Jordan elimination. In the example below we use m=3 parity
1654 * columns, n=8 data columns, with errors in d_1, d_2, and p_1:
1655 * __ __
1656 * | 1 1 1 1 1 1 1 1 |
1657 * | 128 64 32 16 8 4 2 1 | <-----+-+-- missing disks
1658 * | 19 205 116 29 64 16 4 1 | / /
1659 * | 1 0 0 0 0 0 0 0 | / /
1660 * | 0 1 0 0 0 0 0 0 | <--' /
1661 * (V|I) = | 0 0 1 0 0 0 0 0 | <---'
1662 * | 0 0 0 1 0 0 0 0 |
1663 * | 0 0 0 0 1 0 0 0 |
1664 * | 0 0 0 0 0 1 0 0 |
1665 * | 0 0 0 0 0 0 1 0 |
1666 * | 0 0 0 0 0 0 0 1 |
1667 * ~~ ~~
1668 * __ __
1669 * | 1 1 1 1 1 1 1 1 |
1670 * | 128 64 32 16 8 4 2 1 |
1671 * | 19 205 116 29 64 16 4 1 |
1672 * | 1 0 0 0 0 0 0 0 |
1673 * | 0 1 0 0 0 0 0 0 |
1674 * (V|I)' = | 0 0 1 0 0 0 0 0 |
1675 * | 0 0 0 1 0 0 0 0 |
1676 * | 0 0 0 0 1 0 0 0 |
1677 * | 0 0 0 0 0 1 0 0 |
1678 * | 0 0 0 0 0 0 1 0 |
1679 * | 0 0 0 0 0 0 0 1 |
1680 * ~~ ~~
1681 *
1682 * Here we employ Gauss-Jordan elimination to find the inverse of (V|I)'. We
1683 * have carefully chosen the seed values 1, 2, and 4 to ensure that this
1684 * matrix is not singular.
1685 * __ __
1686 * | 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 |
1687 * | 19 205 116 29 64 16 4 1 0 1 0 0 0 0 0 0 |
1688 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
1689 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
1690 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
1691 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
1692 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
1693 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
1694 * ~~ ~~
1695 * __ __
1696 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
1697 * | 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 |
1698 * | 19 205 116 29 64 16 4 1 0 1 0 0 0 0 0 0 |
1699 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
1700 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
1701 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
1702 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
1703 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
1704 * ~~ ~~
1705 * __ __
1706 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
1707 * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 |
1708 * | 0 205 116 0 0 0 0 0 0 1 19 29 64 16 4 1 |
1709 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
1710 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
1711 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
1712 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
1713 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
1714 * ~~ ~~
1715 * __ __
1716 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
1717 * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 |
1718 * | 0 0 185 0 0 0 0 0 205 1 222 208 141 221 201 204 |
1719 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
1720 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
1721 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
1722 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
1723 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
1724 * ~~ ~~
1725 * __ __
1726 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
1727 * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 |
1728 * | 0 0 1 0 0 0 0 0 166 100 4 40 158 168 216 209 |
1729 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
1730 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
1731 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
1732 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
1733 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
1734 * ~~ ~~
1735 * __ __
1736 * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 |
1737 * | 0 1 0 0 0 0 0 0 167 100 5 41 159 169 217 208 |
1738 * | 0 0 1 0 0 0 0 0 166 100 4 40 158 168 216 209 |
1739 * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 |
1740 * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 |
1741 * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 |
1742 * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 |
1743 * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 |
1744 * ~~ ~~
1745 * __ __
1746 * | 0 0 1 0 0 0 0 0 |
1747 * | 167 100 5 41 159 169 217 208 |
1748 * | 166 100 4 40 158 168 216 209 |
1749 * (V|I)'^-1 = | 0 0 0 1 0 0 0 0 |
1750 * | 0 0 0 0 1 0 0 0 |
1751 * | 0 0 0 0 0 1 0 0 |
1752 * | 0 0 0 0 0 0 1 0 |
1753 * | 0 0 0 0 0 0 0 1 |
1754 * ~~ ~~
1755 *
1756 * We can then simply compute D = (V|I)'^-1 x (d|p)' to discover the values
1757 * of the missing data.
1758 *
1759 * As is apparent from the example above, the only non-trivial rows in the
1760 * inverse matrix correspond to the data disks that we're trying to
1761 * reconstruct. Indeed, those are the only rows we need as the others would
1762 * only be useful for reconstructing data known or assumed to be valid. For
1763 * that reason, we only build the coefficients in the rows that correspond to
1764 * targeted columns.
1765 */
1766
1767 static void
vdev_raidz_matrix_init(raidz_row_t * rr,int n,int nmap,int * map,uint8_t ** rows)1768 vdev_raidz_matrix_init(raidz_row_t *rr, int n, int nmap, int *map,
1769 uint8_t **rows)
1770 {
1771 int i, j;
1772 int pow;
1773
1774 ASSERT(n == rr->rr_cols - rr->rr_firstdatacol);
1775
1776 /*
1777 * Fill in the missing rows of interest.
1778 */
1779 for (i = 0; i < nmap; i++) {
1780 ASSERT3S(0, <=, map[i]);
1781 ASSERT3S(map[i], <=, 2);
1782
1783 pow = map[i] * n;
1784 if (pow > 255)
1785 pow -= 255;
1786 ASSERT(pow <= 255);
1787
1788 for (j = 0; j < n; j++) {
1789 pow -= map[i];
1790 if (pow < 0)
1791 pow += 255;
1792 rows[i][j] = vdev_raidz_pow2[pow];
1793 }
1794 }
1795 }
1796
1797 static void
vdev_raidz_matrix_invert(raidz_row_t * rr,int n,int nmissing,int * missing,uint8_t ** rows,uint8_t ** invrows,const uint8_t * used)1798 vdev_raidz_matrix_invert(raidz_row_t *rr, int n, int nmissing, int *missing,
1799 uint8_t **rows, uint8_t **invrows, const uint8_t *used)
1800 {
1801 int i, j, ii, jj;
1802 uint8_t log;
1803
1804 /*
1805 * Assert that the first nmissing entries from the array of used
1806 * columns correspond to parity columns and that subsequent entries
1807 * correspond to data columns.
1808 */
1809 for (i = 0; i < nmissing; i++) {
1810 ASSERT3S(used[i], <, rr->rr_firstdatacol);
1811 }
1812 for (; i < n; i++) {
1813 ASSERT3S(used[i], >=, rr->rr_firstdatacol);
1814 }
1815
1816 /*
1817 * First initialize the storage where we'll compute the inverse rows.
1818 */
1819 for (i = 0; i < nmissing; i++) {
1820 for (j = 0; j < n; j++) {
1821 invrows[i][j] = (i == j) ? 1 : 0;
1822 }
1823 }
1824
1825 /*
1826 * Subtract all trivial rows from the rows of consequence.
1827 */
1828 for (i = 0; i < nmissing; i++) {
1829 for (j = nmissing; j < n; j++) {
1830 ASSERT3U(used[j], >=, rr->rr_firstdatacol);
1831 jj = used[j] - rr->rr_firstdatacol;
1832 ASSERT3S(jj, <, n);
1833 invrows[i][j] = rows[i][jj];
1834 rows[i][jj] = 0;
1835 }
1836 }
1837
1838 /*
1839 * For each of the rows of interest, we must normalize it and subtract
1840 * a multiple of it from the other rows.
1841 */
1842 for (i = 0; i < nmissing; i++) {
1843 for (j = 0; j < missing[i]; j++) {
1844 ASSERT0(rows[i][j]);
1845 }
1846 ASSERT3U(rows[i][missing[i]], !=, 0);
1847
1848 /*
1849 * Compute the inverse of the first element and multiply each
1850 * element in the row by that value.
1851 */
1852 log = 255 - vdev_raidz_log2[rows[i][missing[i]]];
1853
1854 for (j = 0; j < n; j++) {
1855 rows[i][j] = vdev_raidz_exp2(rows[i][j], log);
1856 invrows[i][j] = vdev_raidz_exp2(invrows[i][j], log);
1857 }
1858
1859 for (ii = 0; ii < nmissing; ii++) {
1860 if (i == ii)
1861 continue;
1862
1863 ASSERT3U(rows[ii][missing[i]], !=, 0);
1864
1865 log = vdev_raidz_log2[rows[ii][missing[i]]];
1866
1867 for (j = 0; j < n; j++) {
1868 rows[ii][j] ^=
1869 vdev_raidz_exp2(rows[i][j], log);
1870 invrows[ii][j] ^=
1871 vdev_raidz_exp2(invrows[i][j], log);
1872 }
1873 }
1874 }
1875
1876 /*
1877 * Verify that the data that is left in the rows are properly part of
1878 * an identity matrix.
1879 */
1880 for (i = 0; i < nmissing; i++) {
1881 for (j = 0; j < n; j++) {
1882 if (j == missing[i]) {
1883 ASSERT3U(rows[i][j], ==, 1);
1884 } else {
1885 ASSERT0(rows[i][j]);
1886 }
1887 }
1888 }
1889 }
1890
1891 static void
vdev_raidz_matrix_reconstruct(raidz_row_t * rr,int n,int nmissing,int * missing,uint8_t ** invrows,const uint8_t * used)1892 vdev_raidz_matrix_reconstruct(raidz_row_t *rr, int n, int nmissing,
1893 int *missing, uint8_t **invrows, const uint8_t *used)
1894 {
1895 int i, j, x, cc, c;
1896 uint8_t *src;
1897 uint64_t ccount;
1898 uint8_t *dst[VDEV_RAIDZ_MAXPARITY] = { NULL };
1899 uint64_t dcount[VDEV_RAIDZ_MAXPARITY] = { 0 };
1900 uint8_t log = 0;
1901 uint8_t val;
1902 int ll;
1903 uint8_t *invlog[VDEV_RAIDZ_MAXPARITY];
1904 uint8_t *p, *pp;
1905 size_t psize;
1906
1907 psize = sizeof (invlog[0][0]) * n * nmissing;
1908 p = kmem_alloc(psize, KM_SLEEP);
1909
1910 for (pp = p, i = 0; i < nmissing; i++) {
1911 invlog[i] = pp;
1912 pp += n;
1913 }
1914
1915 for (i = 0; i < nmissing; i++) {
1916 for (j = 0; j < n; j++) {
1917 ASSERT3U(invrows[i][j], !=, 0);
1918 invlog[i][j] = vdev_raidz_log2[invrows[i][j]];
1919 }
1920 }
1921
1922 for (i = 0; i < n; i++) {
1923 c = used[i];
1924 ASSERT3U(c, <, rr->rr_cols);
1925
1926 ccount = rr->rr_col[c].rc_size;
1927 ASSERT(ccount >= rr->rr_col[missing[0]].rc_size || i > 0);
1928 if (ccount == 0)
1929 continue;
1930 src = abd_to_buf(rr->rr_col[c].rc_abd);
1931 for (j = 0; j < nmissing; j++) {
1932 cc = missing[j] + rr->rr_firstdatacol;
1933 ASSERT3U(cc, >=, rr->rr_firstdatacol);
1934 ASSERT3U(cc, <, rr->rr_cols);
1935 ASSERT3U(cc, !=, c);
1936
1937 dcount[j] = rr->rr_col[cc].rc_size;
1938 if (dcount[j] != 0)
1939 dst[j] = abd_to_buf(rr->rr_col[cc].rc_abd);
1940 }
1941
1942 for (x = 0; x < ccount; x++, src++) {
1943 if (*src != 0)
1944 log = vdev_raidz_log2[*src];
1945
1946 for (cc = 0; cc < nmissing; cc++) {
1947 if (x >= dcount[cc])
1948 continue;
1949
1950 if (*src == 0) {
1951 val = 0;
1952 } else {
1953 if ((ll = log + invlog[cc][i]) >= 255)
1954 ll -= 255;
1955 val = vdev_raidz_pow2[ll];
1956 }
1957
1958 if (i == 0)
1959 dst[cc][x] = val;
1960 else
1961 dst[cc][x] ^= val;
1962 }
1963 }
1964 }
1965
1966 kmem_free(p, psize);
1967 }
1968
1969 static void
vdev_raidz_reconstruct_general(raidz_row_t * rr,int * tgts,int ntgts)1970 vdev_raidz_reconstruct_general(raidz_row_t *rr, int *tgts, int ntgts)
1971 {
1972 int i, c, t, tt;
1973 unsigned int n;
1974 unsigned int nmissing_rows;
1975 int missing_rows[VDEV_RAIDZ_MAXPARITY];
1976 int parity_map[VDEV_RAIDZ_MAXPARITY];
1977 uint8_t *p, *pp;
1978 size_t psize;
1979 uint8_t *rows[VDEV_RAIDZ_MAXPARITY];
1980 uint8_t *invrows[VDEV_RAIDZ_MAXPARITY];
1981 uint8_t *used;
1982
1983 abd_t **bufs = NULL;
1984
1985 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT)
1986 zfs_dbgmsg("reconstruct_general(rm=%px ntgts=%u)", rr, ntgts);
1987 /*
1988 * Matrix reconstruction can't use scatter ABDs yet, so we allocate
1989 * temporary linear ABDs if any non-linear ABDs are found.
1990 */
1991 for (i = rr->rr_firstdatacol; i < rr->rr_cols; i++) {
1992 ASSERT(rr->rr_col[i].rc_abd != NULL);
1993 if (!abd_is_linear(rr->rr_col[i].rc_abd)) {
1994 bufs = kmem_alloc(rr->rr_cols * sizeof (abd_t *),
1995 KM_PUSHPAGE);
1996
1997 for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1998 raidz_col_t *col = &rr->rr_col[c];
1999
2000 bufs[c] = col->rc_abd;
2001 if (bufs[c] != NULL) {
2002 col->rc_abd = abd_alloc_linear(
2003 col->rc_size, B_TRUE);
2004 abd_copy(col->rc_abd, bufs[c],
2005 col->rc_size);
2006 }
2007 }
2008
2009 break;
2010 }
2011 }
2012
2013 n = rr->rr_cols - rr->rr_firstdatacol;
2014
2015 /*
2016 * Figure out which data columns are missing.
2017 */
2018 nmissing_rows = 0;
2019 for (t = 0; t < ntgts; t++) {
2020 if (tgts[t] >= rr->rr_firstdatacol) {
2021 missing_rows[nmissing_rows++] =
2022 tgts[t] - rr->rr_firstdatacol;
2023 }
2024 }
2025
2026 /*
2027 * Figure out which parity columns to use to help generate the missing
2028 * data columns.
2029 */
2030 for (tt = 0, c = 0, i = 0; i < nmissing_rows; c++) {
2031 ASSERT(tt < ntgts);
2032 ASSERT(c < rr->rr_firstdatacol);
2033
2034 /*
2035 * Skip any targeted parity columns.
2036 */
2037 if (c == tgts[tt]) {
2038 tt++;
2039 continue;
2040 }
2041
2042 parity_map[i] = c;
2043 i++;
2044 }
2045
2046 psize = (sizeof (rows[0][0]) + sizeof (invrows[0][0])) *
2047 nmissing_rows * n + sizeof (used[0]) * n;
2048 p = kmem_alloc(psize, KM_SLEEP);
2049
2050 for (pp = p, i = 0; i < nmissing_rows; i++) {
2051 rows[i] = pp;
2052 pp += n;
2053 invrows[i] = pp;
2054 pp += n;
2055 }
2056 used = pp;
2057
2058 for (i = 0; i < nmissing_rows; i++) {
2059 used[i] = parity_map[i];
2060 }
2061
2062 for (tt = 0, c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
2063 if (tt < nmissing_rows &&
2064 c == missing_rows[tt] + rr->rr_firstdatacol) {
2065 tt++;
2066 continue;
2067 }
2068
2069 ASSERT3S(i, <, n);
2070 used[i] = c;
2071 i++;
2072 }
2073
2074 /*
2075 * Initialize the interesting rows of the matrix.
2076 */
2077 vdev_raidz_matrix_init(rr, n, nmissing_rows, parity_map, rows);
2078
2079 /*
2080 * Invert the matrix.
2081 */
2082 vdev_raidz_matrix_invert(rr, n, nmissing_rows, missing_rows, rows,
2083 invrows, used);
2084
2085 /*
2086 * Reconstruct the missing data using the generated matrix.
2087 */
2088 vdev_raidz_matrix_reconstruct(rr, n, nmissing_rows, missing_rows,
2089 invrows, used);
2090
2091 kmem_free(p, psize);
2092
2093 /*
2094 * copy back from temporary linear abds and free them
2095 */
2096 if (bufs) {
2097 for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
2098 raidz_col_t *col = &rr->rr_col[c];
2099
2100 if (bufs[c] != NULL) {
2101 abd_copy(bufs[c], col->rc_abd, col->rc_size);
2102 abd_free(col->rc_abd);
2103 }
2104 col->rc_abd = bufs[c];
2105 }
2106 kmem_free(bufs, rr->rr_cols * sizeof (abd_t *));
2107 }
2108 }
2109
2110 static void
vdev_raidz_reconstruct_row(raidz_map_t * rm,raidz_row_t * rr,const int * t,int nt)2111 vdev_raidz_reconstruct_row(raidz_map_t *rm, raidz_row_t *rr,
2112 const int *t, int nt)
2113 {
2114 int tgts[VDEV_RAIDZ_MAXPARITY], *dt;
2115 int ntgts;
2116 int i, c, ret;
2117 int nbadparity, nbaddata;
2118 int parity_valid[VDEV_RAIDZ_MAXPARITY];
2119
2120 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT) {
2121 zfs_dbgmsg("reconstruct(rm=%px nt=%u cols=%u md=%u mp=%u)",
2122 rr, nt, (int)rr->rr_cols, (int)rr->rr_missingdata,
2123 (int)rr->rr_missingparity);
2124 }
2125
2126 nbadparity = rr->rr_firstdatacol;
2127 nbaddata = rr->rr_cols - nbadparity;
2128 ntgts = 0;
2129 for (i = 0, c = 0; c < rr->rr_cols; c++) {
2130 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT) {
2131 zfs_dbgmsg("reconstruct(rm=%px col=%u devid=%u "
2132 "offset=%llx error=%u)",
2133 rr, c, (int)rr->rr_col[c].rc_devidx,
2134 (long long)rr->rr_col[c].rc_offset,
2135 (int)rr->rr_col[c].rc_error);
2136 }
2137 if (c < rr->rr_firstdatacol)
2138 parity_valid[c] = B_FALSE;
2139
2140 if (i < nt && c == t[i]) {
2141 tgts[ntgts++] = c;
2142 i++;
2143 } else if (rr->rr_col[c].rc_error != 0) {
2144 tgts[ntgts++] = c;
2145 } else if (c >= rr->rr_firstdatacol) {
2146 nbaddata--;
2147 } else {
2148 parity_valid[c] = B_TRUE;
2149 nbadparity--;
2150 }
2151 }
2152
2153 ASSERT(ntgts >= nt);
2154 ASSERT(nbaddata >= 0);
2155 ASSERT(nbaddata + nbadparity == ntgts);
2156
2157 dt = &tgts[nbadparity];
2158
2159 /* Reconstruct using the new math implementation */
2160 ret = vdev_raidz_math_reconstruct(rm, rr, parity_valid, dt, nbaddata);
2161 if (ret != RAIDZ_ORIGINAL_IMPL)
2162 return;
2163
2164 /*
2165 * See if we can use any of our optimized reconstruction routines.
2166 */
2167 switch (nbaddata) {
2168 case 1:
2169 if (parity_valid[VDEV_RAIDZ_P]) {
2170 vdev_raidz_reconstruct_p(rr, dt, 1);
2171 return;
2172 }
2173
2174 ASSERT(rr->rr_firstdatacol > 1);
2175
2176 if (parity_valid[VDEV_RAIDZ_Q]) {
2177 vdev_raidz_reconstruct_q(rr, dt, 1);
2178 return;
2179 }
2180
2181 ASSERT(rr->rr_firstdatacol > 2);
2182 break;
2183
2184 case 2:
2185 ASSERT(rr->rr_firstdatacol > 1);
2186
2187 if (parity_valid[VDEV_RAIDZ_P] &&
2188 parity_valid[VDEV_RAIDZ_Q]) {
2189 vdev_raidz_reconstruct_pq(rr, dt, 2);
2190 return;
2191 }
2192
2193 ASSERT(rr->rr_firstdatacol > 2);
2194
2195 break;
2196 }
2197
2198 vdev_raidz_reconstruct_general(rr, tgts, ntgts);
2199 }
2200
2201 static int
vdev_raidz_open(vdev_t * vd,uint64_t * asize,uint64_t * max_asize,uint64_t * logical_ashift,uint64_t * physical_ashift,cred_t * cr)2202 vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
2203 uint64_t *logical_ashift, uint64_t *physical_ashift, cred_t *cr)
2204 {
2205 vdev_raidz_t *vdrz = vd->vdev_tsd;
2206 uint64_t nparity = vdrz->vd_nparity;
2207 int c;
2208 int lasterror = 0;
2209 int numerrors = 0;
2210
2211 ASSERT(nparity > 0);
2212
2213 if (nparity > VDEV_RAIDZ_MAXPARITY ||
2214 vd->vdev_children < nparity + 1) {
2215 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
2216 return (SET_ERROR(EINVAL));
2217 }
2218
2219 vdev_open_children(vd, cr);
2220
2221 for (c = 0; c < vd->vdev_children; c++) {
2222 vdev_t *cvd = vd->vdev_child[c];
2223
2224 if (cvd->vdev_open_error != 0) {
2225 lasterror = cvd->vdev_open_error;
2226 numerrors++;
2227 continue;
2228 }
2229
2230 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
2231 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
2232 *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
2233 }
2234 for (c = 0; c < vd->vdev_children; c++) {
2235 vdev_t *cvd = vd->vdev_child[c];
2236
2237 if (cvd->vdev_open_error != 0)
2238 continue;
2239 *physical_ashift = vdev_best_ashift(*logical_ashift,
2240 *physical_ashift, cvd->vdev_physical_ashift);
2241 }
2242
2243 if (vd->vdev_rz_expanding) {
2244 *asize *= vd->vdev_children - 1;
2245 *max_asize *= vd->vdev_children - 1;
2246
2247 vd->vdev_min_asize = *asize;
2248 } else {
2249 *asize *= vd->vdev_children;
2250 *max_asize *= vd->vdev_children;
2251 }
2252
2253 if (numerrors > nparity) {
2254 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
2255 return (lasterror);
2256 }
2257
2258 return (0);
2259 }
2260
2261 static void
vdev_raidz_close(vdev_t * vd)2262 vdev_raidz_close(vdev_t *vd)
2263 {
2264 for (int c = 0; c < vd->vdev_children; c++) {
2265 if (vd->vdev_child[c] != NULL)
2266 vdev_close(vd->vdev_child[c]);
2267 }
2268 }
2269
2270 /*
2271 * Return the logical width to use, given the txg in which the allocation
2272 * happened.
2273 */
2274 static uint64_t
vdev_raidz_get_logical_width(vdev_raidz_t * vdrz,uint64_t txg)2275 vdev_raidz_get_logical_width(vdev_raidz_t *vdrz, uint64_t txg)
2276 {
2277 reflow_node_t lookup = {
2278 .re_txg = txg,
2279 };
2280 avl_index_t where;
2281
2282 uint64_t width;
2283 mutex_enter(&vdrz->vd_expand_lock);
2284 reflow_node_t *re = avl_find(&vdrz->vd_expand_txgs, &lookup, &where);
2285 if (re != NULL) {
2286 width = re->re_logical_width;
2287 } else {
2288 re = avl_nearest(&vdrz->vd_expand_txgs, where, AVL_BEFORE);
2289 if (re != NULL)
2290 width = re->re_logical_width;
2291 else
2292 width = vdrz->vd_original_width;
2293 }
2294 mutex_exit(&vdrz->vd_expand_lock);
2295 return (width);
2296 }
2297 /*
2298 * This code converts an asize into the largest psize that can safely be written
2299 * to an allocation of that size for this vdev.
2300 *
2301 * Note that this function will not take into account the effect of gang
2302 * headers, which also modify the ASIZE of the DVAs. It is purely a reverse of
2303 * the psize_to_asize function.
2304 */
2305 static uint64_t
vdev_raidz_asize_to_psize(vdev_t * vd,uint64_t asize,uint64_t txg)2306 vdev_raidz_asize_to_psize(vdev_t *vd, uint64_t asize, uint64_t txg)
2307 {
2308 vdev_raidz_t *vdrz = vd->vdev_tsd;
2309 uint64_t psize;
2310 uint64_t ashift = vd->vdev_top->vdev_ashift;
2311 uint64_t nparity = vdrz->vd_nparity;
2312
2313 uint64_t cols = vdev_raidz_get_logical_width(vdrz, txg);
2314
2315 ASSERT0(asize % (1 << ashift));
2316
2317 psize = (asize >> ashift);
2318 /*
2319 * If the roundup to nparity + 1 caused us to spill into a new row, we
2320 * need to ignore that row entirely (since it can't store data or
2321 * parity).
2322 */
2323 uint64_t rows = psize / cols;
2324 psize = psize - (rows * cols) <= nparity ? rows * cols : psize;
2325 /* Subtract out parity sectors for each row storing data. */
2326 psize -= nparity * DIV_ROUND_UP(psize, cols);
2327 psize <<= ashift;
2328
2329 return (psize);
2330 }
2331
2332 /*
2333 * Note: If the RAIDZ vdev has been expanded, older BP's may have allocated
2334 * more space due to the lower data-to-parity ratio. In this case it's
2335 * important to pass in the correct txg. Note that vdev_gang_header_asize()
2336 * relies on a constant asize for psize=SPA_GANGBLOCKSIZE=SPA_MINBLOCKSIZE,
2337 * regardless of txg. This is assured because for a single data sector, we
2338 * allocate P+1 sectors regardless of width ("cols", which is at least P+1).
2339 */
2340 static uint64_t
vdev_raidz_psize_to_asize(vdev_t * vd,uint64_t psize,uint64_t txg)2341 vdev_raidz_psize_to_asize(vdev_t *vd, uint64_t psize, uint64_t txg)
2342 {
2343 vdev_raidz_t *vdrz = vd->vdev_tsd;
2344 uint64_t asize;
2345 uint64_t ashift = vd->vdev_top->vdev_ashift;
2346 uint64_t nparity = vdrz->vd_nparity;
2347
2348 uint64_t cols = vdev_raidz_get_logical_width(vdrz, txg);
2349
2350 asize = ((psize - 1) >> ashift) + 1;
2351 asize += nparity * ((asize + cols - nparity - 1) / (cols - nparity));
2352 asize = roundup(asize, nparity + 1) << ashift;
2353
2354 #ifdef ZFS_DEBUG
2355 uint64_t asize_new = ((psize - 1) >> ashift) + 1;
2356 uint64_t ncols_new = vdrz->vd_physical_width;
2357 asize_new += nparity * ((asize_new + ncols_new - nparity - 1) /
2358 (ncols_new - nparity));
2359 asize_new = roundup(asize_new, nparity + 1) << ashift;
2360 VERIFY3U(asize_new, <=, asize);
2361 #endif
2362
2363 return (asize);
2364 }
2365
2366 /*
2367 * The allocatable space for a raidz vdev is N * sizeof(smallest child)
2368 * so each child must provide at least 1/Nth of its asize.
2369 */
2370 static uint64_t
vdev_raidz_min_asize(vdev_t * vd)2371 vdev_raidz_min_asize(vdev_t *vd)
2372 {
2373 return ((vd->vdev_min_asize + vd->vdev_children - 1) /
2374 vd->vdev_children);
2375 }
2376
2377 /*
2378 * return B_TRUE if a read should be skipped due to being too slow.
2379 *
2380 * In vdev_child_slow_outlier() it looks for outliers based on disk
2381 * latency from the most recent child reads. Here we're checking if,
2382 * over time, a disk has has been an outlier too many times and is
2383 * now in a sit out period.
2384 */
2385 boolean_t
vdev_sit_out_reads(vdev_t * vd,zio_flag_t io_flags)2386 vdev_sit_out_reads(vdev_t *vd, zio_flag_t io_flags)
2387 {
2388 if (vdev_read_sit_out_secs == 0)
2389 return (B_FALSE);
2390
2391 /* Avoid skipping a data column read when scrubbing */
2392 if (io_flags & ZIO_FLAG_SCRUB)
2393 return (B_FALSE);
2394
2395 if (!vd->vdev_ops->vdev_op_leaf) {
2396 boolean_t sitting = B_FALSE;
2397 for (int c = 0; c < vd->vdev_children; c++) {
2398 sitting |= vdev_sit_out_reads(vd->vdev_child[c],
2399 io_flags);
2400 }
2401 return (sitting);
2402 }
2403
2404 if (vd->vdev_read_sit_out_expire >= gethrestime_sec())
2405 return (B_TRUE);
2406
2407 vd->vdev_read_sit_out_expire = 0;
2408
2409 return (B_FALSE);
2410 }
2411
2412 void
vdev_raidz_child_done(zio_t * zio)2413 vdev_raidz_child_done(zio_t *zio)
2414 {
2415 raidz_col_t *rc = zio->io_private;
2416
2417 ASSERT3P(rc->rc_abd, !=, NULL);
2418 rc->rc_error = zio->io_error;
2419 rc->rc_tried = 1;
2420 rc->rc_skipped = 0;
2421 }
2422
2423 static void
vdev_raidz_shadow_child_done(zio_t * zio)2424 vdev_raidz_shadow_child_done(zio_t *zio)
2425 {
2426 raidz_col_t *rc = zio->io_private;
2427
2428 rc->rc_shadow_error = zio->io_error;
2429 }
2430
2431 static void
vdev_raidz_io_verify(zio_t * zio,raidz_map_t * rm,raidz_row_t * rr,int col)2432 vdev_raidz_io_verify(zio_t *zio, raidz_map_t *rm, raidz_row_t *rr, int col)
2433 {
2434 (void) rm;
2435 #ifdef ZFS_DEBUG
2436 zfs_range_seg64_t logical_rs, physical_rs, remain_rs;
2437 logical_rs.rs_start = rr->rr_offset;
2438 logical_rs.rs_end = logical_rs.rs_start +
2439 vdev_raidz_psize_to_asize(zio->io_vd, rr->rr_size,
2440 BP_GET_PHYSICAL_BIRTH(zio->io_bp));
2441
2442 raidz_col_t *rc = &rr->rr_col[col];
2443 vdev_t *cvd = zio->io_vd->vdev_child[rc->rc_devidx];
2444
2445 vdev_xlate(cvd, &logical_rs, &physical_rs, &remain_rs);
2446 ASSERT(vdev_xlate_is_empty(&remain_rs));
2447 if (vdev_xlate_is_empty(&physical_rs)) {
2448 /*
2449 * If we are in the middle of expansion, the
2450 * physical->logical mapping is changing so vdev_xlate()
2451 * can't give us a reliable answer.
2452 */
2453 return;
2454 }
2455 ASSERT3U(rc->rc_offset, ==, physical_rs.rs_start);
2456 ASSERT3U(rc->rc_offset, <, physical_rs.rs_end);
2457 /*
2458 * It would be nice to assert that rs_end is equal
2459 * to rc_offset + rc_size but there might be an
2460 * optional I/O at the end that is not accounted in
2461 * rc_size.
2462 */
2463 if (physical_rs.rs_end > rc->rc_offset + rc->rc_size) {
2464 ASSERT3U(physical_rs.rs_end, ==, rc->rc_offset +
2465 rc->rc_size + (1 << zio->io_vd->vdev_top->vdev_ashift));
2466 } else {
2467 ASSERT3U(physical_rs.rs_end, ==, rc->rc_offset + rc->rc_size);
2468 }
2469 #endif
2470 }
2471
2472 static void
vdev_raidz_io_start_write(zio_t * zio,raidz_row_t * rr)2473 vdev_raidz_io_start_write(zio_t *zio, raidz_row_t *rr)
2474 {
2475 vdev_t *vd = zio->io_vd;
2476 raidz_map_t *rm = zio->io_vsd;
2477
2478 vdev_raidz_generate_parity_row(rm, rr);
2479
2480 for (int c = 0; c < rr->rr_scols; c++) {
2481 raidz_col_t *rc = &rr->rr_col[c];
2482 vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2483
2484 /* Verify physical to logical translation */
2485 vdev_raidz_io_verify(zio, rm, rr, c);
2486
2487 if (rc->rc_size == 0)
2488 continue;
2489
2490 ASSERT3U(rc->rc_offset + rc->rc_size, <,
2491 cvd->vdev_psize - VDEV_LABEL_END_SIZE);
2492
2493 ASSERT3P(rc->rc_abd, !=, NULL);
2494 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2495 rc->rc_offset, rc->rc_abd,
2496 abd_get_size(rc->rc_abd), zio->io_type,
2497 zio->io_priority, 0, vdev_raidz_child_done, rc));
2498
2499 if (rc->rc_shadow_devidx != INT_MAX) {
2500 vdev_t *cvd2 = vd->vdev_child[rc->rc_shadow_devidx];
2501
2502 ASSERT3U(
2503 rc->rc_shadow_offset + abd_get_size(rc->rc_abd), <,
2504 cvd2->vdev_psize - VDEV_LABEL_END_SIZE);
2505
2506 zio_nowait(zio_vdev_child_io(zio, NULL, cvd2,
2507 rc->rc_shadow_offset, rc->rc_abd,
2508 abd_get_size(rc->rc_abd),
2509 zio->io_type, zio->io_priority, 0,
2510 vdev_raidz_shadow_child_done, rc));
2511 }
2512 }
2513 }
2514
2515 /*
2516 * Generate optional I/Os for skip sectors to improve aggregation contiguity.
2517 * This only works for vdev_raidz_map_alloc() (not _expanded()).
2518 */
2519 static void
raidz_start_skip_writes(zio_t * zio)2520 raidz_start_skip_writes(zio_t *zio)
2521 {
2522 vdev_t *vd = zio->io_vd;
2523 uint64_t ashift = vd->vdev_top->vdev_ashift;
2524 raidz_map_t *rm = zio->io_vsd;
2525 ASSERT3U(rm->rm_nrows, ==, 1);
2526 raidz_row_t *rr = rm->rm_row[0];
2527 for (int c = 0; c < rr->rr_scols; c++) {
2528 raidz_col_t *rc = &rr->rr_col[c];
2529 vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2530 if (rc->rc_size != 0)
2531 continue;
2532 ASSERT0P(rc->rc_abd);
2533
2534 ASSERT3U(rc->rc_offset, <,
2535 cvd->vdev_psize - VDEV_LABEL_END_SIZE);
2536
2537 zio_nowait(zio_vdev_child_io(zio, NULL, cvd, rc->rc_offset,
2538 NULL, 1ULL << ashift, zio->io_type, zio->io_priority,
2539 ZIO_FLAG_NODATA | ZIO_FLAG_OPTIONAL, NULL, NULL));
2540 }
2541 }
2542
2543 static void
vdev_raidz_io_start_read_row(zio_t * zio,raidz_row_t * rr,boolean_t forceparity)2544 vdev_raidz_io_start_read_row(zio_t *zio, raidz_row_t *rr, boolean_t forceparity)
2545 {
2546 vdev_t *vd = zio->io_vd;
2547
2548 /*
2549 * Iterate over the columns in reverse order so that we hit the parity
2550 * last -- any errors along the way will force us to read the parity.
2551 */
2552 for (int c = rr->rr_cols - 1; c >= 0; c--) {
2553 raidz_col_t *rc = &rr->rr_col[c];
2554 if (rc->rc_size == 0)
2555 continue;
2556 vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2557 if (!vdev_readable(cvd)) {
2558 if (c >= rr->rr_firstdatacol)
2559 rr->rr_missingdata++;
2560 else
2561 rr->rr_missingparity++;
2562 rc->rc_error = SET_ERROR(ENXIO);
2563 rc->rc_tried = 1; /* don't even try */
2564 rc->rc_skipped = 1;
2565 continue;
2566 }
2567 if (vdev_dtl_contains(cvd, DTL_MISSING, zio->io_txg, 1)) {
2568 if (c >= rr->rr_firstdatacol)
2569 rr->rr_missingdata++;
2570 else
2571 rr->rr_missingparity++;
2572 rc->rc_error = SET_ERROR(ESTALE);
2573 rc->rc_skipped = 1;
2574 continue;
2575 }
2576
2577 if (vdev_sit_out_reads(cvd, zio->io_flags)) {
2578 rr->rr_outlier_cnt++;
2579 ASSERT0(rc->rc_latency_outlier);
2580 rc->rc_latency_outlier = 1;
2581 }
2582 }
2583
2584 /*
2585 * When the row contains a latency outlier and sufficient parity
2586 * exists to reconstruct the column data, then skip reading the
2587 * known slow child vdev as a performance optimization.
2588 */
2589 if (rr->rr_outlier_cnt > 0 &&
2590 (rr->rr_firstdatacol - rr->rr_missingparity) >=
2591 (rr->rr_missingdata + 1)) {
2592
2593 for (int c = rr->rr_cols - 1; c >= 0; c--) {
2594 raidz_col_t *rc = &rr->rr_col[c];
2595
2596 if (rc->rc_error == 0 && rc->rc_latency_outlier) {
2597 if (c >= rr->rr_firstdatacol)
2598 rr->rr_missingdata++;
2599 else
2600 rr->rr_missingparity++;
2601 rc->rc_error = SET_ERROR(EAGAIN);
2602 rc->rc_skipped = 1;
2603 break;
2604 }
2605 }
2606 }
2607
2608 for (int c = rr->rr_cols - 1; c >= 0; c--) {
2609 raidz_col_t *rc = &rr->rr_col[c];
2610 vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2611
2612 if (rc->rc_error || rc->rc_size == 0)
2613 continue;
2614
2615 if (forceparity ||
2616 c >= rr->rr_firstdatacol || rr->rr_missingdata > 0 ||
2617 (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
2618 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2619 rc->rc_offset, rc->rc_abd, rc->rc_size,
2620 zio->io_type, zio->io_priority, 0,
2621 vdev_raidz_child_done, rc));
2622 }
2623 }
2624 }
2625
2626 static void
vdev_raidz_io_start_read_phys_cols(zio_t * zio,raidz_map_t * rm)2627 vdev_raidz_io_start_read_phys_cols(zio_t *zio, raidz_map_t *rm)
2628 {
2629 vdev_t *vd = zio->io_vd;
2630
2631 for (int i = 0; i < rm->rm_nphys_cols; i++) {
2632 raidz_col_t *prc = &rm->rm_phys_col[i];
2633 if (prc->rc_size == 0)
2634 continue;
2635
2636 ASSERT3U(prc->rc_devidx, ==, i);
2637 vdev_t *cvd = vd->vdev_child[i];
2638
2639 if (!vdev_readable(cvd)) {
2640 prc->rc_error = SET_ERROR(ENXIO);
2641 prc->rc_tried = 1; /* don't even try */
2642 prc->rc_skipped = 1;
2643 continue;
2644 }
2645 if (vdev_dtl_contains(cvd, DTL_MISSING, zio->io_txg, 1)) {
2646 prc->rc_error = SET_ERROR(ESTALE);
2647 prc->rc_skipped = 1;
2648 continue;
2649 }
2650 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2651 prc->rc_offset, prc->rc_abd, prc->rc_size,
2652 zio->io_type, zio->io_priority, 0,
2653 vdev_raidz_child_done, prc));
2654 }
2655 }
2656
2657 static void
vdev_raidz_io_start_read(zio_t * zio,raidz_map_t * rm)2658 vdev_raidz_io_start_read(zio_t *zio, raidz_map_t *rm)
2659 {
2660 /*
2661 * If there are multiple rows, we will be hitting
2662 * all disks, so go ahead and read the parity so
2663 * that we are reading in decent size chunks.
2664 */
2665 boolean_t forceparity = rm->rm_nrows > 1;
2666
2667 if (rm->rm_phys_col) {
2668 vdev_raidz_io_start_read_phys_cols(zio, rm);
2669 } else {
2670 for (int i = 0; i < rm->rm_nrows; i++) {
2671 raidz_row_t *rr = rm->rm_row[i];
2672 vdev_raidz_io_start_read_row(zio, rr, forceparity);
2673 }
2674 }
2675 }
2676
2677 /*
2678 * Start an IO operation on a RAIDZ VDev
2679 *
2680 * Outline:
2681 * - For write operations:
2682 * 1. Generate the parity data
2683 * 2. Create child zio write operations to each column's vdev, for both
2684 * data and parity.
2685 * 3. If the column skips any sectors for padding, create optional dummy
2686 * write zio children for those areas to improve aggregation continuity.
2687 * - For read operations:
2688 * 1. Create child zio read operations to each data column's vdev to read
2689 * the range of data required for zio.
2690 * 2. If this is a scrub or resilver operation, or if any of the data
2691 * vdevs have had errors, then create zio read operations to the parity
2692 * columns' VDevs as well.
2693 */
2694 static void
vdev_raidz_io_start(zio_t * zio)2695 vdev_raidz_io_start(zio_t *zio)
2696 {
2697 vdev_t *vd = zio->io_vd;
2698 vdev_t *tvd = vd->vdev_top;
2699 vdev_raidz_t *vdrz = vd->vdev_tsd;
2700 raidz_map_t *rm;
2701
2702 uint64_t logical_width = vdev_raidz_get_logical_width(vdrz,
2703 BP_GET_PHYSICAL_BIRTH(zio->io_bp));
2704 if (logical_width != vdrz->vd_physical_width) {
2705 zfs_locked_range_t *lr = NULL;
2706 uint64_t synced_offset = UINT64_MAX;
2707 uint64_t next_offset = UINT64_MAX;
2708 boolean_t use_scratch = B_FALSE;
2709 /*
2710 * Note: when the expansion is completing, we set
2711 * vre_state=DSS_FINISHED (in raidz_reflow_complete_sync())
2712 * in a later txg than when we last update spa_ubsync's state
2713 * (see the end of spa_raidz_expand_thread()). Therefore we
2714 * may see vre_state!=SCANNING before
2715 * VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE=DSS_FINISHED is reflected
2716 * on disk, but the copying progress has been synced to disk
2717 * (and reflected in spa_ubsync). In this case it's fine to
2718 * treat the expansion as completed, since if we crash there's
2719 * no additional copying to do.
2720 */
2721 if (vdrz->vn_vre.vre_state == DSS_SCANNING) {
2722 ASSERT3P(vd->vdev_spa->spa_raidz_expand, ==,
2723 &vdrz->vn_vre);
2724 lr = zfs_rangelock_enter(&vdrz->vn_vre.vre_rangelock,
2725 zio->io_offset, zio->io_size, RL_READER);
2726 use_scratch =
2727 (RRSS_GET_STATE(&vd->vdev_spa->spa_ubsync) ==
2728 RRSS_SCRATCH_VALID);
2729 synced_offset =
2730 RRSS_GET_OFFSET(&vd->vdev_spa->spa_ubsync);
2731 next_offset = vdrz->vn_vre.vre_offset;
2732 /*
2733 * If we haven't resumed expanding since importing the
2734 * pool, vre_offset won't have been set yet. In
2735 * this case the next offset to be copied is the same
2736 * as what was synced.
2737 */
2738 if (next_offset == UINT64_MAX) {
2739 next_offset = synced_offset;
2740 }
2741 }
2742
2743 rm = vdev_raidz_map_alloc_expanded(zio,
2744 tvd->vdev_ashift, vdrz->vd_physical_width,
2745 logical_width, vdrz->vd_nparity,
2746 synced_offset, next_offset, use_scratch);
2747 rm->rm_lr = lr;
2748 } else {
2749 rm = vdev_raidz_map_alloc(zio,
2750 tvd->vdev_ashift, logical_width, vdrz->vd_nparity);
2751 }
2752 rm->rm_original_width = vdrz->vd_original_width;
2753
2754 zio->io_vsd = rm;
2755 zio->io_vsd_ops = &vdev_raidz_vsd_ops;
2756 if (zio->io_type == ZIO_TYPE_WRITE) {
2757 for (int i = 0; i < rm->rm_nrows; i++) {
2758 vdev_raidz_io_start_write(zio, rm->rm_row[i]);
2759 }
2760
2761 if (logical_width == vdrz->vd_physical_width) {
2762 raidz_start_skip_writes(zio);
2763 }
2764 } else {
2765 ASSERT(zio->io_type == ZIO_TYPE_READ);
2766 vdev_raidz_io_start_read(zio, rm);
2767 }
2768
2769 zio_execute(zio);
2770 }
2771
2772 /*
2773 * Report a checksum error for a child of a RAID-Z device.
2774 */
2775 void
vdev_raidz_checksum_error(zio_t * zio,raidz_col_t * rc,abd_t * bad_data)2776 vdev_raidz_checksum_error(zio_t *zio, raidz_col_t *rc, abd_t *bad_data)
2777 {
2778 vdev_t *vd = zio->io_vd->vdev_child[rc->rc_devidx];
2779
2780 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE) &&
2781 zio->io_priority != ZIO_PRIORITY_REBUILD) {
2782 zio_bad_cksum_t zbc;
2783 raidz_map_t *rm = zio->io_vsd;
2784
2785 zbc.zbc_has_cksum = 0;
2786 zbc.zbc_injected = rm->rm_ecksuminjected;
2787
2788 mutex_enter(&vd->vdev_stat_lock);
2789 vd->vdev_stat.vs_checksum_errors++;
2790 mutex_exit(&vd->vdev_stat_lock);
2791 (void) zfs_ereport_post_checksum(zio->io_spa, vd,
2792 &zio->io_bookmark, zio, rc->rc_offset, rc->rc_size,
2793 rc->rc_abd, bad_data, &zbc);
2794 }
2795 }
2796
2797 /*
2798 * We keep track of whether or not there were any injected errors, so that
2799 * any ereports we generate can note it.
2800 */
2801 static int
raidz_checksum_verify(zio_t * zio)2802 raidz_checksum_verify(zio_t *zio)
2803 {
2804 zio_bad_cksum_t zbc = {0};
2805 raidz_map_t *rm = zio->io_vsd;
2806
2807 int ret = zio_checksum_error(zio, &zbc);
2808 /*
2809 * Any Direct I/O read that has a checksum error must be treated as
2810 * suspicious as the contents of the buffer could be getting
2811 * manipulated while the I/O is taking place. The checksum verify error
2812 * will be reported to the top-level RAIDZ VDEV.
2813 */
2814 if (zio->io_flags & ZIO_FLAG_DIO_READ && ret == ECKSUM) {
2815 zio->io_error = ret;
2816 zio->io_post |= ZIO_POST_DIO_CHKSUM_ERR;
2817 zio_dio_chksum_verify_error_report(zio);
2818 zio_checksum_verified(zio);
2819 return (0);
2820 }
2821
2822 if (ret != 0 && zbc.zbc_injected != 0)
2823 rm->rm_ecksuminjected = 1;
2824
2825 return (ret);
2826 }
2827
2828 /*
2829 * Generate the parity from the data columns. If we tried and were able to
2830 * read the parity without error, verify that the generated parity matches the
2831 * data we read. If it doesn't, we fire off a checksum error. Return the
2832 * number of such failures.
2833 */
2834 static int
raidz_parity_verify(zio_t * zio,raidz_row_t * rr)2835 raidz_parity_verify(zio_t *zio, raidz_row_t *rr)
2836 {
2837 abd_t *orig[VDEV_RAIDZ_MAXPARITY];
2838 int c, ret = 0;
2839 raidz_map_t *rm = zio->io_vsd;
2840 raidz_col_t *rc;
2841
2842 blkptr_t *bp = zio->io_bp;
2843 enum zio_checksum checksum = (bp == NULL ? zio->io_prop.zp_checksum :
2844 (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
2845
2846 if (checksum == ZIO_CHECKSUM_NOPARITY)
2847 return (ret);
2848
2849 for (c = 0; c < rr->rr_firstdatacol; c++) {
2850 rc = &rr->rr_col[c];
2851 if (!rc->rc_tried || rc->rc_error != 0)
2852 continue;
2853
2854 orig[c] = rc->rc_abd;
2855 ASSERT3U(abd_get_size(rc->rc_abd), ==, rc->rc_size);
2856 rc->rc_abd = abd_alloc_linear(rc->rc_size, B_FALSE);
2857 }
2858
2859 /*
2860 * Verify any empty sectors are zero filled to ensure the parity
2861 * is calculated correctly even if these non-data sectors are damaged.
2862 */
2863 if (rr->rr_nempty && rr->rr_abd_empty != NULL)
2864 ret += vdev_draid_map_verify_empty(zio, rr);
2865
2866 /*
2867 * Regenerates parity even for !tried||rc_error!=0 columns. This
2868 * isn't harmful but it does have the side effect of fixing stuff
2869 * we didn't realize was necessary (i.e. even if we return 0).
2870 */
2871 vdev_raidz_generate_parity_row(rm, rr);
2872
2873 for (c = 0; c < rr->rr_firstdatacol; c++) {
2874 rc = &rr->rr_col[c];
2875
2876 if (!rc->rc_tried || rc->rc_error != 0)
2877 continue;
2878
2879 if (abd_cmp(orig[c], rc->rc_abd) != 0) {
2880 vdev_raidz_checksum_error(zio, rc, orig[c]);
2881 rc->rc_error = SET_ERROR(ECKSUM);
2882 ret++;
2883 }
2884 abd_free(orig[c]);
2885 }
2886
2887 return (ret);
2888 }
2889
2890 static int
vdev_raidz_worst_error(raidz_row_t * rr)2891 vdev_raidz_worst_error(raidz_row_t *rr)
2892 {
2893 int error = 0;
2894
2895 for (int c = 0; c < rr->rr_cols; c++) {
2896 error = zio_worst_error(error, rr->rr_col[c].rc_error);
2897 error = zio_worst_error(error, rr->rr_col[c].rc_shadow_error);
2898 }
2899
2900 return (error);
2901 }
2902
2903 /*
2904 * Find the median value from a set of n values
2905 */
2906 static uint64_t
latency_median_value(const uint64_t * data,size_t n)2907 latency_median_value(const uint64_t *data, size_t n)
2908 {
2909 uint64_t m;
2910
2911 if (n % 2 == 0)
2912 m = (data[(n >> 1) - 1] + data[n >> 1]) >> 1;
2913 else
2914 m = data[((n + 1) >> 1) - 1];
2915
2916 return (m);
2917 }
2918
2919 /*
2920 * Calculate the outlier fence from a set of n latency values
2921 *
2922 * fence = Q3 + vdev_raidz_outlier_insensitivity x (Q3 - Q1)
2923 */
2924 static uint64_t
latency_quartiles_fence(const uint64_t * data,size_t n,uint64_t * iqr)2925 latency_quartiles_fence(const uint64_t *data, size_t n, uint64_t *iqr)
2926 {
2927 uint64_t q1 = latency_median_value(&data[0], n >> 1);
2928 uint64_t q3 = latency_median_value(&data[(n + 1) >> 1], n >> 1);
2929
2930 /*
2931 * To avoid detecting false positive outliers when N is small and
2932 * and the latencies values are very close, make sure the IQR
2933 * is at least 25% larger than Q1.
2934 */
2935 *iqr = MAX(q3 - q1, q1 / 4);
2936
2937 return (q3 + (*iqr * vdev_raidz_outlier_insensitivity));
2938 }
2939 #define LAT_CHILDREN_MIN 5
2940 #define LAT_OUTLIER_LIMIT 20
2941
2942 static int
latency_compare(const void * arg1,const void * arg2)2943 latency_compare(const void *arg1, const void *arg2)
2944 {
2945 const uint64_t *l1 = (uint64_t *)arg1;
2946 const uint64_t *l2 = (uint64_t *)arg2;
2947
2948 return (TREE_CMP(*l1, *l2));
2949 }
2950
2951 void
vdev_raidz_sit_child(vdev_t * svd,uint64_t secs)2952 vdev_raidz_sit_child(vdev_t *svd, uint64_t secs)
2953 {
2954 for (int c = 0; c < svd->vdev_children; c++)
2955 vdev_raidz_sit_child(svd->vdev_child[c], secs);
2956
2957 if (!svd->vdev_ops->vdev_op_leaf)
2958 return;
2959
2960 /* Begin a sit out period for this slow drive */
2961 svd->vdev_read_sit_out_expire = gethrestime_sec() +
2962 secs;
2963
2964 /* Count each slow io period */
2965 mutex_enter(&svd->vdev_stat_lock);
2966 svd->vdev_stat.vs_slow_ios++;
2967 mutex_exit(&svd->vdev_stat_lock);
2968 }
2969
2970 void
vdev_raidz_unsit_child(vdev_t * vd)2971 vdev_raidz_unsit_child(vdev_t *vd)
2972 {
2973 for (int c = 0; c < vd->vdev_children; c++)
2974 vdev_raidz_unsit_child(vd->vdev_child[c]);
2975
2976 if (!vd->vdev_ops->vdev_op_leaf)
2977 return;
2978
2979 vd->vdev_read_sit_out_expire = 0;
2980 }
2981
2982 /*
2983 * Check for any latency outlier from latest set of child reads.
2984 *
2985 * Uses a Tukey's fence, with K = 50, for detecting extreme outliers. This
2986 * rule defines extreme outliers as data points outside the fence of the
2987 * third quartile plus fifty times the Interquartile Range (IQR). This range
2988 * is the distance between the first and third quartile.
2989 *
2990 * Fifty is an extremely large value for Tukey's fence, but the outliers we're
2991 * attempting to detect here are orders of magnitude times larger than the
2992 * median. This large value should capture any truly fault disk quickly,
2993 * without causing spurious sit-outs.
2994 *
2995 * To further avoid spurious sit-outs, vdevs must be detected multiple times
2996 * as an outlier before they are sat, and outlier counts will gradually decay.
2997 * Every nchildren times we have detected an outlier, we subtract 2 from the
2998 * outlier count of all children. If detected outliers are close to uniformly
2999 * distributed, this will result in the outlier count remaining close to 0
3000 * (in expectation; over long enough time-scales, spurious sit-outs are still
3001 * possible).
3002 */
3003 static void
vdev_child_slow_outlier(zio_t * zio)3004 vdev_child_slow_outlier(zio_t *zio)
3005 {
3006 vdev_t *vd = zio->io_vd;
3007 if (!vd->vdev_autosit || vdev_read_sit_out_secs == 0 ||
3008 vd->vdev_children < LAT_CHILDREN_MIN)
3009 return;
3010
3011 hrtime_t now = getlrtime();
3012 uint64_t last = atomic_load_64(&vd->vdev_last_latency_check);
3013
3014 if ((now - last) < MSEC2NSEC(vdev_raidz_outlier_check_interval_ms))
3015 return;
3016
3017 /* Allow a single winner when there are racing callers. */
3018 if (atomic_cas_64(&vd->vdev_last_latency_check, last, now) != last)
3019 return;
3020
3021 int children = vd->vdev_children;
3022 uint64_t *lat_data = kmem_alloc(sizeof (uint64_t) * children, KM_SLEEP);
3023
3024 for (int c = 0; c < children; c++) {
3025 vdev_t *cvd = vd->vdev_child[c];
3026 if (cvd->vdev_prev_histo == NULL) {
3027 mutex_enter(&cvd->vdev_stat_lock);
3028 size_t size =
3029 sizeof (cvd->vdev_stat_ex.vsx_disk_histo[0]);
3030 cvd->vdev_prev_histo = kmem_zalloc(size, KM_SLEEP);
3031 memcpy(cvd->vdev_prev_histo,
3032 cvd->vdev_stat_ex.vsx_disk_histo[ZIO_TYPE_READ],
3033 size);
3034 mutex_exit(&cvd->vdev_stat_lock);
3035 }
3036 }
3037 uint64_t max = 0;
3038 vdev_t *svd = NULL;
3039 uint_t sitouts = 0;
3040 boolean_t skip = B_FALSE, svd_sitting = B_FALSE;
3041 for (int c = 0; c < children; c++) {
3042 vdev_t *cvd = vd->vdev_child[c];
3043 boolean_t sitting = vdev_sit_out_reads(cvd, 0) ||
3044 cvd->vdev_state != VDEV_STATE_HEALTHY;
3045
3046 /* We can't sit out more disks than we have parity */
3047 if (sitting && ++sitouts >= vdev_get_nparity(vd))
3048 skip = B_TRUE;
3049
3050 mutex_enter(&cvd->vdev_stat_lock);
3051
3052 uint64_t *prev_histo = cvd->vdev_prev_histo;
3053 uint64_t *histo =
3054 cvd->vdev_stat_ex.vsx_disk_histo[ZIO_TYPE_READ];
3055 if (skip) {
3056 size_t size =
3057 sizeof (cvd->vdev_stat_ex.vsx_disk_histo[0]);
3058 memcpy(prev_histo, histo, size);
3059 mutex_exit(&cvd->vdev_stat_lock);
3060 continue;
3061 }
3062 uint64_t count = 0;
3063 lat_data[c] = 0;
3064 for (int i = 0; i < VDEV_L_HISTO_BUCKETS; i++) {
3065 uint64_t this_count = histo[i] - prev_histo[i];
3066 lat_data[c] += (1ULL << i) * this_count;
3067 count += this_count;
3068 }
3069 size_t size = sizeof (cvd->vdev_stat_ex.vsx_disk_histo[0]);
3070 memcpy(prev_histo, histo, size);
3071 mutex_exit(&cvd->vdev_stat_lock);
3072 lat_data[c] /= MAX(1, count);
3073
3074 /* Wait until all disks have been read from */
3075 if (lat_data[c] == 0 && !sitting) {
3076 skip = B_TRUE;
3077 continue;
3078 }
3079
3080 /* Keep track of the vdev with largest value */
3081 if (lat_data[c] > max) {
3082 max = lat_data[c];
3083 svd = cvd;
3084 svd_sitting = sitting;
3085 }
3086 }
3087
3088 if (skip) {
3089 kmem_free(lat_data, sizeof (uint64_t) * children);
3090 return;
3091 }
3092
3093 qsort((void *)lat_data, children, sizeof (uint64_t), latency_compare);
3094
3095 uint64_t iqr;
3096 uint64_t fence = latency_quartiles_fence(lat_data, children, &iqr);
3097
3098 ASSERT3U(lat_data[children - 1], ==, max);
3099 if (max > fence && !svd_sitting) {
3100 ASSERT3U(iqr, >, 0);
3101 uint64_t incr = MAX(1, MIN((max - fence) / iqr,
3102 LAT_OUTLIER_LIMIT / 4));
3103 vd->vdev_outlier_count += incr;
3104 if (vd->vdev_outlier_count >= children) {
3105 for (int c = 0; c < children; c++) {
3106 vdev_t *cvd = vd->vdev_child[c];
3107 cvd->vdev_outlier_count -= 2;
3108 cvd->vdev_outlier_count = MAX(0,
3109 cvd->vdev_outlier_count);
3110 }
3111 vd->vdev_outlier_count = 0;
3112 }
3113 /*
3114 * Keep track of how many times this child has had
3115 * an outlier read. A disk that persitently has a
3116 * higher than peers outlier count will be considered
3117 * a slow disk.
3118 */
3119 svd->vdev_outlier_count += incr;
3120 if (svd->vdev_outlier_count > LAT_OUTLIER_LIMIT) {
3121 ASSERT0(svd->vdev_read_sit_out_expire);
3122 vdev_raidz_sit_child(svd, vdev_read_sit_out_secs);
3123 (void) zfs_ereport_post(FM_EREPORT_ZFS_SITOUT,
3124 zio->io_spa, svd, NULL, NULL, 0);
3125 vdev_dbgmsg(svd, "begin read sit out for %d secs",
3126 (int)vdev_read_sit_out_secs);
3127
3128 for (int c = 0; c < vd->vdev_children; c++)
3129 vd->vdev_child[c]->vdev_outlier_count = 0;
3130 }
3131 }
3132
3133 kmem_free(lat_data, sizeof (uint64_t) * children);
3134 }
3135
3136 static void
vdev_raidz_io_done_verified(zio_t * zio,raidz_row_t * rr)3137 vdev_raidz_io_done_verified(zio_t *zio, raidz_row_t *rr)
3138 {
3139 int unexpected_errors = 0;
3140 int parity_errors = 0;
3141 int parity_untried = 0;
3142 int data_errors = 0;
3143 zio_flag_t add_flags = 0;
3144
3145 ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
3146 ASSERT0(zio->io_error);
3147
3148 for (int c = 0; c < rr->rr_cols; c++) {
3149 raidz_col_t *rc = &rr->rr_col[c];
3150
3151 if (rc->rc_error) {
3152 if (c < rr->rr_firstdatacol)
3153 parity_errors++;
3154 else
3155 data_errors++;
3156
3157 if (!rc->rc_skipped)
3158 unexpected_errors++;
3159 } else if (c < rr->rr_firstdatacol && !rc->rc_tried) {
3160 parity_untried++;
3161 }
3162
3163 if (rc->rc_force_repair)
3164 unexpected_errors++;
3165 }
3166
3167 /*
3168 * If we read more parity disks than were used for
3169 * reconstruction, confirm that the other parity disks produced
3170 * correct data.
3171 *
3172 * We also regenerate parity to write it back to any failed parity
3173 * columns. However, if all available parity was consumed by
3174 * reconstruction (parity_verify is false), regenerating parity is
3175 * a mathematical identity -- the result is guaranteed to equal the
3176 * input that was used for reconstruction, whether correct or
3177 * corrupted. In that case the only reason to regenerate is to
3178 * write back a failed parity column, so skip regeneration when no
3179 * parity column failed or the pool is read-only.
3180 */
3181 boolean_t parity_verify = (parity_errors + parity_untried) <
3182 (rr->rr_firstdatacol - data_errors);
3183 if (parity_verify || (parity_errors > 0 &&
3184 spa_writeable(zio->io_spa))) {
3185 int n = raidz_parity_verify(zio, rr);
3186 /*
3187 * In, Reed-Solomon encoding, if we have ndata+1 columns and
3188 * the parity doesn't match, it means the data integrity is
3189 * compromised. We shouldn't try to repair anything in this
3190 * case.
3191 */
3192 if (parity_verify && n > 0 &&
3193 zio->io_priority == ZIO_PRIORITY_REBUILD)
3194 return;
3195 /*
3196 * If we have only ndata columns, the data integrity will
3197 * be checked by the checksums normally, but not in case
3198 * of rebuild when we don't have checksums. In this case,
3199 * we add ZIO_FLAG_SPECULATIVE and try to not spread
3200 * unverified data. For example, when the target vdev happens
3201 * to be the mirroring spare vdev, we would repair only that
3202 * child in it which is being rebuilt.
3203 */
3204 if (!parity_verify && zio->io_priority == ZIO_PRIORITY_REBUILD)
3205 add_flags |= ZIO_FLAG_SPECULATIVE;
3206 unexpected_errors += n;
3207 }
3208
3209 if (spa_writeable(zio->io_spa) &&
3210 (unexpected_errors > 0 || (zio->io_flags & ZIO_FLAG_RESILVER))) {
3211 /*
3212 * Use the good data we have in hand to repair damaged children.
3213 */
3214 for (int c = 0; c < rr->rr_cols; c++) {
3215 raidz_col_t *rc = &rr->rr_col[c];
3216 vdev_t *vd = zio->io_vd;
3217 vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
3218
3219 if (!rc->rc_allow_repair) {
3220 continue;
3221 } else if (!rc->rc_force_repair &&
3222 (rc->rc_error == 0 || rc->rc_size == 0)) {
3223 continue;
3224 }
3225 /*
3226 * We do not allow self healing for Direct I/O reads.
3227 * See comment in vdev_raid_row_alloc().
3228 */
3229 ASSERT0(zio->io_flags & ZIO_FLAG_DIO_READ);
3230
3231 /*
3232 * When the target vdev is draid spare, we should clear
3233 * ZIO_FLAG_SPECULATIVE. First, if that draid spare maps
3234 * to another spare having an online/degraded disk, that
3235 * disk must be repaired also. Otherwise, the scrub will
3236 * detect a lot of cksum errors later. Second, since it
3237 * is draid spare, there is no harm in updating its
3238 * content on any vdev it maps to because the space is
3239 * reserved as a spare anyway.
3240 */
3241 zio_flag_t aflags = add_flags;
3242 if (rc->rc_tgt_is_dspare)
3243 aflags &= ~ZIO_FLAG_SPECULATIVE;
3244
3245 zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
3246 rc->rc_offset, rc->rc_abd, rc->rc_size,
3247 ZIO_TYPE_WRITE,
3248 zio->io_priority == ZIO_PRIORITY_REBUILD ?
3249 ZIO_PRIORITY_REBUILD : ZIO_PRIORITY_ASYNC_WRITE,
3250 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
3251 ZIO_FLAG_SELF_HEAL : 0) | aflags, NULL, NULL));
3252 }
3253 }
3254
3255 /*
3256 * Scrub or resilver i/o's: overwrite any shadow locations with the
3257 * good data. This ensures that if we've already copied this sector,
3258 * it will be corrected if it was damaged. This writes more than is
3259 * necessary, but since expansion is paused during scrub/resilver, at
3260 * most a single row will have a shadow location.
3261 */
3262 if (spa_writeable(zio->io_spa) &&
3263 (zio->io_flags & (ZIO_FLAG_RESILVER | ZIO_FLAG_SCRUB))) {
3264 for (int c = 0; c < rr->rr_cols; c++) {
3265 raidz_col_t *rc = &rr->rr_col[c];
3266 vdev_t *vd = zio->io_vd;
3267
3268 if (rc->rc_shadow_devidx == INT_MAX || rc->rc_size == 0)
3269 continue;
3270 vdev_t *cvd = vd->vdev_child[rc->rc_shadow_devidx];
3271
3272 /*
3273 * Note: We don't want to update the repair stats
3274 * because that would incorrectly indicate that there
3275 * was bad data to repair, which we aren't sure about.
3276 * By clearing the SCAN_THREAD flag, we prevent this
3277 * from happening, despite having the REPAIR flag set.
3278 * We need to set SELF_HEAL so that this i/o can't be
3279 * bypassed by zio_vdev_io_start().
3280 */
3281 zio_t *cio = zio_vdev_child_io(zio, NULL, cvd,
3282 rc->rc_shadow_offset, rc->rc_abd, rc->rc_size,
3283 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
3284 ZIO_FLAG_IO_REPAIR | ZIO_FLAG_SELF_HEAL,
3285 NULL, NULL);
3286 cio->io_flags &= ~ZIO_FLAG_SCAN_THREAD;
3287 zio_nowait(cio);
3288 }
3289 }
3290 }
3291
3292 static void
raidz_restore_orig_data(raidz_map_t * rm)3293 raidz_restore_orig_data(raidz_map_t *rm)
3294 {
3295 for (int i = 0; i < rm->rm_nrows; i++) {
3296 raidz_row_t *rr = rm->rm_row[i];
3297 for (int c = 0; c < rr->rr_cols; c++) {
3298 raidz_col_t *rc = &rr->rr_col[c];
3299 if (rc->rc_need_orig_restore) {
3300 abd_copy(rc->rc_abd,
3301 rc->rc_orig_data, rc->rc_size);
3302 rc->rc_need_orig_restore = B_FALSE;
3303 }
3304 }
3305 }
3306 }
3307
3308 /*
3309 * During raidz_reconstruct() for expanded VDEV, we need special consideration
3310 * failure simulations. See note in raidz_reconstruct() on simulating failure
3311 * of a pre-expansion device.
3312 *
3313 * Treating logical child i as failed, return TRUE if the given column should
3314 * be treated as failed. The idea of logical children allows us to imagine
3315 * that a disk silently failed before a RAIDZ expansion (reads from this disk
3316 * succeed but return the wrong data). Since the expansion doesn't verify
3317 * checksums, the incorrect data will be moved to new locations spread among
3318 * the children (going diagonally across them).
3319 *
3320 * Higher "logical child failures" (values of `i`) indicate these
3321 * "pre-expansion failures". The first physical_width values imagine that a
3322 * current child failed; the next physical_width-1 values imagine that a
3323 * child failed before the most recent expansion; the next physical_width-2
3324 * values imagine a child failed in the expansion before that, etc.
3325 */
3326 static boolean_t
raidz_simulate_failure(int physical_width,int original_width,int ashift,int i,raidz_col_t * rc)3327 raidz_simulate_failure(int physical_width, int original_width, int ashift,
3328 int i, raidz_col_t *rc)
3329 {
3330 uint64_t sector_id =
3331 physical_width * (rc->rc_offset >> ashift) +
3332 rc->rc_devidx;
3333
3334 for (int w = physical_width; w >= original_width; w--) {
3335 if (i < w) {
3336 return (sector_id % w == i);
3337 } else {
3338 i -= w;
3339 }
3340 }
3341 ASSERT(!"invalid logical child id");
3342 return (B_FALSE);
3343 }
3344
3345 /*
3346 * returns EINVAL if reconstruction of the block will not be possible
3347 * returns ECKSUM if this specific reconstruction failed
3348 * returns 0 on successful reconstruction
3349 */
3350 static int
raidz_reconstruct(zio_t * zio,int * ltgts,int ntgts,int nparity)3351 raidz_reconstruct(zio_t *zio, int *ltgts, int ntgts, int nparity)
3352 {
3353 vdev_t *vd = zio->io_vd;
3354 raidz_map_t *rm = zio->io_vsd;
3355 int physical_width = vd->vdev_children;
3356 int dbgmsg = zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT;
3357
3358 if (vd->vdev_ops == &vdev_draid_ops) {
3359 vdev_draid_config_t *vdc = vd->vdev_tsd;
3360 physical_width = vdc->vdc_children;
3361 }
3362
3363 int original_width = (rm->rm_original_width != 0) ?
3364 rm->rm_original_width : physical_width;
3365
3366 if (dbgmsg) {
3367 zfs_dbgmsg("raidz_reconstruct_expanded(zio=%px ltgts=%u,%u,%u "
3368 "ntgts=%u", zio, ltgts[0], ltgts[1], ltgts[2], ntgts);
3369 }
3370
3371 /* Reconstruct each row */
3372 for (int r = 0; r < rm->rm_nrows; r++) {
3373 raidz_row_t *rr = rm->rm_row[r];
3374 int my_tgts[VDEV_RAIDZ_MAXPARITY]; /* value is child id */
3375 int t = 0;
3376 int dead = 0;
3377 int dead_data = 0;
3378
3379 if (dbgmsg)
3380 zfs_dbgmsg("raidz_reconstruct_expanded(row=%u)", r);
3381
3382 for (int c = 0; c < rr->rr_cols; c++) {
3383 raidz_col_t *rc = &rr->rr_col[c];
3384 ASSERT0(rc->rc_need_orig_restore);
3385 if (rc->rc_error != 0) {
3386 dead++;
3387 if (c >= nparity)
3388 dead_data++;
3389 continue;
3390 }
3391 if (rc->rc_size == 0)
3392 continue;
3393 for (int lt = 0; lt < ntgts; lt++) {
3394 if (raidz_simulate_failure(physical_width,
3395 original_width,
3396 zio->io_vd->vdev_top->vdev_ashift,
3397 ltgts[lt], rc)) {
3398 if (rc->rc_orig_data == NULL) {
3399 rc->rc_orig_data =
3400 abd_alloc_linear(
3401 rc->rc_size, B_TRUE);
3402 abd_copy(rc->rc_orig_data,
3403 rc->rc_abd, rc->rc_size);
3404 }
3405 rc->rc_need_orig_restore = B_TRUE;
3406
3407 dead++;
3408 if (c >= nparity)
3409 dead_data++;
3410 /*
3411 * Note: simulating failure of a
3412 * pre-expansion device can hit more
3413 * than one column, in which case we
3414 * might try to simulate more failures
3415 * than can be reconstructed, which is
3416 * also more than the size of my_tgts.
3417 * This check prevents accessing past
3418 * the end of my_tgts. The "dead >
3419 * nparity" check below will fail this
3420 * reconstruction attempt.
3421 */
3422 if (t < VDEV_RAIDZ_MAXPARITY) {
3423 my_tgts[t++] = c;
3424 if (dbgmsg) {
3425 zfs_dbgmsg("simulating "
3426 "failure of col %u "
3427 "devidx %u", c,
3428 (int)rc->rc_devidx);
3429 }
3430 }
3431 break;
3432 }
3433 }
3434 }
3435 if (dead > nparity) {
3436 /* reconstruction not possible */
3437 if (dbgmsg) {
3438 zfs_dbgmsg("reconstruction not possible; "
3439 "too many failures");
3440 }
3441 raidz_restore_orig_data(rm);
3442 return (EINVAL);
3443 }
3444 if (dead_data > 0)
3445 vdev_raidz_reconstruct_row(rm, rr, my_tgts, t);
3446 }
3447
3448 /* Check for success */
3449 if (raidz_checksum_verify(zio) == 0) {
3450 if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)
3451 return (0);
3452
3453 /* Reconstruction succeeded - report errors */
3454 for (int i = 0; i < rm->rm_nrows; i++) {
3455 raidz_row_t *rr = rm->rm_row[i];
3456
3457 for (int c = 0; c < rr->rr_cols; c++) {
3458 raidz_col_t *rc = &rr->rr_col[c];
3459 if (rc->rc_need_orig_restore) {
3460 /*
3461 * Note: if this is a parity column,
3462 * we don't really know if it's wrong.
3463 * We need to let
3464 * vdev_raidz_io_done_verified() check
3465 * it, and if we set rc_error, it will
3466 * think that it is a "known" error
3467 * that doesn't need to be checked
3468 * or corrected.
3469 */
3470 if (rc->rc_error == 0 &&
3471 c >= rr->rr_firstdatacol) {
3472 vdev_raidz_checksum_error(zio,
3473 rc, rc->rc_orig_data);
3474 rc->rc_error =
3475 SET_ERROR(ECKSUM);
3476 }
3477 rc->rc_need_orig_restore = B_FALSE;
3478 }
3479 }
3480
3481 vdev_raidz_io_done_verified(zio, rr);
3482 }
3483
3484 zio_checksum_verified(zio);
3485
3486 if (dbgmsg) {
3487 zfs_dbgmsg("reconstruction successful "
3488 "(checksum verified)");
3489 }
3490 return (0);
3491 }
3492
3493 /* Reconstruction failed - restore original data */
3494 raidz_restore_orig_data(rm);
3495 if (dbgmsg) {
3496 zfs_dbgmsg("raidz_reconstruct_expanded(zio=%px) checksum "
3497 "failed", zio);
3498 }
3499 return (ECKSUM);
3500 }
3501
3502 /*
3503 * Iterate over all combinations of N bad vdevs and attempt a reconstruction.
3504 * Note that the algorithm below is non-optimal because it doesn't take into
3505 * account how reconstruction is actually performed. For example, with
3506 * triple-parity RAID-Z the reconstruction procedure is the same if column 4
3507 * is targeted as invalid as if columns 1 and 4 are targeted since in both
3508 * cases we'd only use parity information in column 0.
3509 *
3510 * The order that we find the various possible combinations of failed
3511 * disks is dictated by these rules:
3512 * - Examine each "slot" (the "i" in tgts[i])
3513 * - Try to increment this slot (tgts[i] += 1)
3514 * - if we can't increment because it runs into the next slot,
3515 * reset our slot to the minimum, and examine the next slot
3516 *
3517 * For example, with a 6-wide RAIDZ3, and no known errors (so we have to choose
3518 * 3 columns to reconstruct), we will generate the following sequence:
3519 *
3520 * STATE ACTION
3521 * 0 1 2 special case: skip since these are all parity
3522 * 0 1 3 first slot: reset to 0; middle slot: increment to 2
3523 * 0 2 3 first slot: increment to 1
3524 * 1 2 3 first: reset to 0; middle: reset to 1; last: increment to 4
3525 * 0 1 4 first: reset to 0; middle: increment to 2
3526 * 0 2 4 first: increment to 1
3527 * 1 2 4 first: reset to 0; middle: increment to 3
3528 * 0 3 4 first: increment to 1
3529 * 1 3 4 first: increment to 2
3530 * 2 3 4 first: reset to 0; middle: reset to 1; last: increment to 5
3531 * 0 1 5 first: reset to 0; middle: increment to 2
3532 * 0 2 5 first: increment to 1
3533 * 1 2 5 first: reset to 0; middle: increment to 3
3534 * 0 3 5 first: increment to 1
3535 * 1 3 5 first: increment to 2
3536 * 2 3 5 first: reset to 0; middle: increment to 4
3537 * 0 4 5 first: increment to 1
3538 * 1 4 5 first: increment to 2
3539 * 2 4 5 first: increment to 3
3540 * 3 4 5 done
3541 *
3542 * This strategy works for dRAID but is less efficient when there are a large
3543 * number of child vdevs and therefore permutations to check. Furthermore,
3544 * since the raidz_map_t rows likely do not overlap, reconstruction would be
3545 * possible as long as there are no more than nparity data errors per row.
3546 * These additional permutations are not currently checked but could be as
3547 * a future improvement.
3548 *
3549 * Returns 0 on success, ECKSUM on failure.
3550 */
3551 static int
vdev_raidz_combrec(zio_t * zio)3552 vdev_raidz_combrec(zio_t *zio)
3553 {
3554 vdev_t *vd = zio->io_vd;
3555 int nparity = vdev_get_nparity(vd);
3556 raidz_map_t *rm = zio->io_vsd;
3557 int physical_width = zio->io_vd->vdev_children;
3558
3559 if (vd->vdev_ops == &vdev_draid_ops) {
3560 vdev_draid_config_t *vdc = vd->vdev_tsd;
3561 nparity = vdc->vdc_nparity;
3562 physical_width = vdc->vdc_children;
3563 }
3564
3565 int original_width = (rm->rm_original_width != 0) ?
3566 rm->rm_original_width : physical_width;
3567
3568 for (int i = 0; i < rm->rm_nrows; i++) {
3569 raidz_row_t *rr = rm->rm_row[i];
3570 int total_errors = 0;
3571
3572 for (int c = 0; c < rr->rr_cols; c++) {
3573 if (rr->rr_col[c].rc_error)
3574 total_errors++;
3575 }
3576
3577 if (total_errors > nparity)
3578 return (vdev_raidz_worst_error(rr));
3579 }
3580
3581 for (int num_failures = 1; num_failures <= nparity; num_failures++) {
3582 int tstore[VDEV_RAIDZ_MAXPARITY + 2];
3583 int *ltgts = &tstore[1]; /* value is logical child ID */
3584
3585
3586 /*
3587 * Determine number of logical children, n. See comment
3588 * above raidz_simulate_failure().
3589 */
3590 int n = 0;
3591 for (int w = physical_width;
3592 w >= original_width; w--) {
3593 n += w;
3594 }
3595
3596 ASSERT3U(num_failures, <=, nparity);
3597 ASSERT3U(num_failures, <=, VDEV_RAIDZ_MAXPARITY);
3598
3599 /* Handle corner cases in combrec logic */
3600 ltgts[-1] = -1;
3601 for (int i = 0; i < num_failures; i++) {
3602 ltgts[i] = i;
3603 }
3604 ltgts[num_failures] = n;
3605
3606 for (;;) {
3607 int err = raidz_reconstruct(zio, ltgts, num_failures,
3608 nparity);
3609 if (err == EINVAL) {
3610 /*
3611 * Reconstruction not possible with this #
3612 * failures; try more failures.
3613 */
3614 break;
3615 } else if (err == 0)
3616 return (0);
3617
3618 /* Compute next targets to try */
3619 for (int t = 0; ; t++) {
3620 ASSERT3U(t, <, num_failures);
3621 ltgts[t]++;
3622 if (ltgts[t] == n) {
3623 /* try more failures */
3624 ASSERT3U(t, ==, num_failures - 1);
3625 if (zfs_flags &
3626 ZFS_DEBUG_RAIDZ_RECONSTRUCT) {
3627 zfs_dbgmsg("reconstruction "
3628 "failed for num_failures="
3629 "%u; tried all "
3630 "combinations",
3631 num_failures);
3632 }
3633 break;
3634 }
3635
3636 ASSERT3U(ltgts[t], <, n);
3637 ASSERT3U(ltgts[t], <=, ltgts[t + 1]);
3638
3639 /*
3640 * If that spot is available, we're done here.
3641 * Try the next combination.
3642 */
3643 if (ltgts[t] != ltgts[t + 1])
3644 break; // found next combination
3645
3646 /*
3647 * Otherwise, reset this tgt to the minimum,
3648 * and move on to the next tgt.
3649 */
3650 ltgts[t] = ltgts[t - 1] + 1;
3651 ASSERT3U(ltgts[t], ==, t);
3652 }
3653
3654 /* Increase the number of failures and keep trying. */
3655 if (ltgts[num_failures - 1] == n)
3656 break;
3657 }
3658 }
3659 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT)
3660 zfs_dbgmsg("reconstruction failed for all num_failures");
3661 return (ECKSUM);
3662 }
3663
3664 void
vdev_raidz_reconstruct(raidz_map_t * rm,const int * t,int nt)3665 vdev_raidz_reconstruct(raidz_map_t *rm, const int *t, int nt)
3666 {
3667 for (uint64_t row = 0; row < rm->rm_nrows; row++) {
3668 raidz_row_t *rr = rm->rm_row[row];
3669 vdev_raidz_reconstruct_row(rm, rr, t, nt);
3670 }
3671 }
3672
3673 /*
3674 * Complete a write IO operation on a RAIDZ VDev
3675 *
3676 * Outline:
3677 * 1. Check for errors on the child IOs.
3678 * 2. Return, setting an error code if too few child VDevs were written
3679 * to reconstruct the data later. Note that partial writes are
3680 * considered successful if they can be reconstructed at all.
3681 */
3682 static void
vdev_raidz_io_done_write_impl(zio_t * zio,raidz_row_t * rr)3683 vdev_raidz_io_done_write_impl(zio_t *zio, raidz_row_t *rr)
3684 {
3685 int normal_errors = 0;
3686 int shadow_errors = 0;
3687 int retryable_errors = 0;
3688
3689 ASSERT3U(rr->rr_missingparity, <=, rr->rr_firstdatacol);
3690 ASSERT3U(rr->rr_missingdata, <=, rr->rr_cols - rr->rr_firstdatacol);
3691 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
3692
3693 for (int c = 0; c < rr->rr_cols; c++) {
3694 raidz_col_t *rc = &rr->rr_col[c];
3695
3696 if (rc->rc_error != 0) {
3697 ASSERT(rc->rc_error != ECKSUM); /* child has no bp */
3698 normal_errors++;
3699 }
3700 if (rc->rc_shadow_error != 0) {
3701 ASSERT(rc->rc_shadow_error != ECKSUM);
3702 shadow_errors++;
3703 }
3704 if (rc->rc_error || rc->rc_shadow_error) {
3705 vdev_t *cvd = zio->io_vd->vdev_child[rc->rc_devidx];
3706 if (!(vdev_is_dead(cvd) || cvd->vdev_cant_write))
3707 retryable_errors++;
3708 }
3709 }
3710
3711 /*
3712 * Treat partial writes as a success. If we couldn't write enough
3713 * columns to reconstruct the data, the I/O failed. Otherwise, good
3714 * enough. Note that in the case of a shadow write (during raidz
3715 * expansion), depending on if we crash, either the normal (old) or
3716 * shadow (new) location may become the "real" version of the block,
3717 * so both locations must have sufficient redundancy.
3718 *
3719 * Now that we support write reallocation, it would be better
3720 * to treat partial failure as real failure unless there are
3721 * no non-degraded top-level vdevs left, and not update DTLs
3722 * if we intend to reallocate.
3723 */
3724 if (normal_errors > rr->rr_firstdatacol ||
3725 shadow_errors > rr->rr_firstdatacol) {
3726 zio->io_error = zio_worst_error(zio->io_error,
3727 vdev_raidz_worst_error(rr));
3728 } else if (retryable_errors && zfs_scrub_partial_writes) {
3729 zio->io_flags |= ZIO_FLAG_POSTREAD;
3730 }
3731 }
3732
3733 static void
vdev_raidz_io_done_reconstruct_known_missing(zio_t * zio,raidz_map_t * rm,raidz_row_t * rr)3734 vdev_raidz_io_done_reconstruct_known_missing(zio_t *zio, raidz_map_t *rm,
3735 raidz_row_t *rr)
3736 {
3737 int parity_errors = 0;
3738 int parity_untried = 0;
3739 int data_errors = 0;
3740 int total_errors = 0;
3741
3742 ASSERT3U(rr->rr_missingparity, <=, rr->rr_firstdatacol);
3743 ASSERT3U(rr->rr_missingdata, <=, rr->rr_cols - rr->rr_firstdatacol);
3744
3745 for (int c = 0; c < rr->rr_cols; c++) {
3746 raidz_col_t *rc = &rr->rr_col[c];
3747
3748 /*
3749 * If scrubbing and a replacing/sparing child vdev determined
3750 * that not all of its children have an identical copy of the
3751 * data, then clear the error so the column is treated like
3752 * any other read and force a repair to correct the damage.
3753 */
3754 if (rc->rc_error == ECKSUM) {
3755 ASSERT(zio->io_flags & ZIO_FLAG_SCRUB);
3756 vdev_raidz_checksum_error(zio, rc, rc->rc_abd);
3757 rc->rc_force_repair = 1;
3758 rc->rc_error = 0;
3759 }
3760
3761 if (rc->rc_error) {
3762 if (c < rr->rr_firstdatacol)
3763 parity_errors++;
3764 else
3765 data_errors++;
3766
3767 total_errors++;
3768 } else if (c < rr->rr_firstdatacol && !rc->rc_tried) {
3769 parity_untried++;
3770 }
3771 }
3772
3773 /*
3774 * If there were data errors and the number of errors we saw was
3775 * correctable -- less than or equal to the number of parity disks read
3776 * -- reconstruct based on the missing data.
3777 */
3778 if (data_errors != 0 &&
3779 total_errors <= rr->rr_firstdatacol - parity_untried) {
3780 /*
3781 * We either attempt to read all the parity columns or
3782 * none of them. If we didn't try to read parity, we
3783 * wouldn't be here in the correctable case. There must
3784 * also have been fewer parity errors than parity
3785 * columns or, again, we wouldn't be in this code path.
3786 */
3787 ASSERT0(parity_untried);
3788 ASSERT(parity_errors < rr->rr_firstdatacol);
3789
3790 /*
3791 * Identify the data columns that reported an error.
3792 */
3793 int n = 0;
3794 int tgts[VDEV_RAIDZ_MAXPARITY];
3795 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
3796 raidz_col_t *rc = &rr->rr_col[c];
3797 if (rc->rc_error != 0) {
3798 ASSERT(n < VDEV_RAIDZ_MAXPARITY);
3799 tgts[n++] = c;
3800 }
3801 }
3802
3803 ASSERT(rr->rr_firstdatacol >= n);
3804
3805 vdev_raidz_reconstruct_row(rm, rr, tgts, n);
3806 }
3807 }
3808
3809 /*
3810 * Return the number of reads issued.
3811 */
3812 static int
vdev_raidz_read_all(zio_t * zio,raidz_row_t * rr)3813 vdev_raidz_read_all(zio_t *zio, raidz_row_t *rr)
3814 {
3815 vdev_t *vd = zio->io_vd;
3816 int nread = 0;
3817
3818 rr->rr_missingdata = 0;
3819 rr->rr_missingparity = 0;
3820
3821 /*
3822 * If this rows contains empty sectors which are not required
3823 * for a normal read then allocate an ABD for them now so they
3824 * may be read, verified, and any needed repairs performed.
3825 */
3826 if (rr->rr_nempty != 0 && rr->rr_abd_empty == NULL)
3827 vdev_draid_map_alloc_empty(zio, rr);
3828
3829 for (int c = 0; c < rr->rr_cols; c++) {
3830 raidz_col_t *rc = &rr->rr_col[c];
3831 if (rc->rc_tried || rc->rc_size == 0)
3832 continue;
3833
3834 zio_nowait(zio_vdev_child_io(zio, NULL,
3835 vd->vdev_child[rc->rc_devidx],
3836 rc->rc_offset, rc->rc_abd, rc->rc_size,
3837 zio->io_type, zio->io_priority, 0,
3838 vdev_raidz_child_done, rc));
3839 nread++;
3840 }
3841 return (nread);
3842 }
3843
3844 /*
3845 * We're here because either there were too many errors to even attempt
3846 * reconstruction (total_errors == rm_first_datacol), or vdev_*_combrec()
3847 * failed. In either case, there is enough bad data to prevent reconstruction.
3848 * Start checksum ereports for all children which haven't failed.
3849 */
3850 static void
vdev_raidz_io_done_unrecoverable(zio_t * zio)3851 vdev_raidz_io_done_unrecoverable(zio_t *zio)
3852 {
3853 raidz_map_t *rm = zio->io_vsd;
3854
3855 for (int i = 0; i < rm->rm_nrows; i++) {
3856 raidz_row_t *rr = rm->rm_row[i];
3857
3858 for (int c = 0; c < rr->rr_cols; c++) {
3859 raidz_col_t *rc = &rr->rr_col[c];
3860 vdev_t *cvd = zio->io_vd->vdev_child[rc->rc_devidx];
3861
3862 if (rc->rc_error != 0)
3863 continue;
3864
3865 zio_bad_cksum_t zbc;
3866 zbc.zbc_has_cksum = 0;
3867 zbc.zbc_injected = rm->rm_ecksuminjected;
3868 mutex_enter(&cvd->vdev_stat_lock);
3869 cvd->vdev_stat.vs_checksum_errors++;
3870 mutex_exit(&cvd->vdev_stat_lock);
3871 (void) zfs_ereport_start_checksum(zio->io_spa,
3872 cvd, &zio->io_bookmark, zio, rc->rc_offset,
3873 rc->rc_size, &zbc);
3874 }
3875 }
3876 }
3877
3878 void
vdev_raidz_io_done(zio_t * zio)3879 vdev_raidz_io_done(zio_t *zio)
3880 {
3881 raidz_map_t *rm = zio->io_vsd;
3882
3883 ASSERT(zio->io_bp != NULL);
3884 if (zio->io_type == ZIO_TYPE_WRITE) {
3885 for (int i = 0; i < rm->rm_nrows; i++) {
3886 vdev_raidz_io_done_write_impl(zio, rm->rm_row[i]);
3887 }
3888 } else {
3889 if (rm->rm_phys_col) {
3890 /*
3891 * This is an aggregated read. Copy the data and status
3892 * from the aggregate abd's to the individual rows.
3893 */
3894 for (int i = 0; i < rm->rm_nrows; i++) {
3895 raidz_row_t *rr = rm->rm_row[i];
3896
3897 for (int c = 0; c < rr->rr_cols; c++) {
3898 raidz_col_t *rc = &rr->rr_col[c];
3899 if (rc->rc_tried || rc->rc_size == 0)
3900 continue;
3901
3902 raidz_col_t *prc =
3903 &rm->rm_phys_col[rc->rc_devidx];
3904 rc->rc_error = prc->rc_error;
3905 rc->rc_tried = prc->rc_tried;
3906 rc->rc_skipped = prc->rc_skipped;
3907 if (c >= rr->rr_firstdatacol) {
3908 /*
3909 * Note: this is slightly faster
3910 * than using abd_copy_off().
3911 */
3912 char *physbuf = abd_to_buf(
3913 prc->rc_abd);
3914 void *physloc = physbuf +
3915 rc->rc_offset -
3916 prc->rc_offset;
3917
3918 abd_copy_from_buf(rc->rc_abd,
3919 physloc, rc->rc_size);
3920 }
3921 }
3922 }
3923 }
3924
3925 for (int i = 0; i < rm->rm_nrows; i++) {
3926 raidz_row_t *rr = rm->rm_row[i];
3927 vdev_raidz_io_done_reconstruct_known_missing(zio,
3928 rm, rr);
3929 }
3930
3931 if (raidz_checksum_verify(zio) == 0) {
3932 if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)
3933 goto done;
3934
3935 for (int i = 0; i < rm->rm_nrows; i++) {
3936 raidz_row_t *rr = rm->rm_row[i];
3937 vdev_raidz_io_done_verified(zio, rr);
3938 }
3939 /* Periodically check for a read outlier */
3940 if (zio->io_type == ZIO_TYPE_READ)
3941 vdev_child_slow_outlier(zio);
3942 zio_checksum_verified(zio);
3943 } else {
3944 /*
3945 * A sequential resilver has no checksum which makes
3946 * combinatoral reconstruction impossible. This code
3947 * path is unreachable since raidz_checksum_verify()
3948 * has no checksum to verify and must succeed.
3949 */
3950 ASSERT3U(zio->io_priority, !=, ZIO_PRIORITY_REBUILD);
3951
3952 /*
3953 * This isn't a typical situation -- either we got a
3954 * read error or a child silently returned bad data.
3955 * Read every block so we can try again with as much
3956 * data and parity as we can track down. If we've
3957 * already been through once before, all children will
3958 * be marked as tried so we'll proceed to combinatorial
3959 * reconstruction.
3960 */
3961 int nread = 0;
3962 for (int i = 0; i < rm->rm_nrows; i++) {
3963 nread += vdev_raidz_read_all(zio,
3964 rm->rm_row[i]);
3965 }
3966 if (nread != 0) {
3967 /*
3968 * Normally our stage is VDEV_IO_DONE, but if
3969 * we've already called redone(), it will have
3970 * changed to VDEV_IO_START, in which case we
3971 * don't want to call redone() again.
3972 */
3973 if (zio->io_stage != ZIO_STAGE_VDEV_IO_START)
3974 zio_vdev_io_redone(zio);
3975 return;
3976 }
3977 /*
3978 * It would be too expensive to try every possible
3979 * combination of failed sectors in every row, so
3980 * instead we try every combination of failed current or
3981 * past physical disk. This means that if the incorrect
3982 * sectors were all on Nparity disks at any point in the
3983 * past, we will find the correct data. The only known
3984 * case where this is less durable than a non-expanded
3985 * RAIDZ, is if we have a silent failure during
3986 * expansion. In that case, one block could be
3987 * partially in the old format and partially in the
3988 * new format, so we'd lost some sectors from the old
3989 * format and some from the new format.
3990 *
3991 * e.g. logical_width=4 physical_width=6
3992 * the 15 (6+5+4) possible failed disks are:
3993 * width=6 child=0
3994 * width=6 child=1
3995 * width=6 child=2
3996 * width=6 child=3
3997 * width=6 child=4
3998 * width=6 child=5
3999 * width=5 child=0
4000 * width=5 child=1
4001 * width=5 child=2
4002 * width=5 child=3
4003 * width=5 child=4
4004 * width=4 child=0
4005 * width=4 child=1
4006 * width=4 child=2
4007 * width=4 child=3
4008 * And we will try every combination of Nparity of these
4009 * failing.
4010 *
4011 * As a first pass, we can generate every combo,
4012 * and try reconstructing, ignoring any known
4013 * failures. If any row has too many known + simulated
4014 * failures, then we bail on reconstructing with this
4015 * number of simulated failures. As an improvement,
4016 * we could detect the number of whole known failures
4017 * (i.e. we have known failures on these disks for
4018 * every row; the disks never succeeded), and
4019 * subtract that from the max # failures to simulate.
4020 * We could go even further like the current
4021 * combrec code, but that doesn't seem like it
4022 * gains us very much. If we simulate a failure
4023 * that is also a known failure, that's fine.
4024 */
4025 zio->io_error = vdev_raidz_combrec(zio);
4026 if (zio->io_error == ECKSUM &&
4027 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
4028 vdev_raidz_io_done_unrecoverable(zio);
4029 }
4030 }
4031 }
4032 done:
4033 if (rm->rm_lr != NULL) {
4034 zfs_rangelock_exit(rm->rm_lr);
4035 rm->rm_lr = NULL;
4036 }
4037 }
4038
4039 static void
vdev_raidz_state_change(vdev_t * vd,int faulted,int degraded)4040 vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded)
4041 {
4042 vdev_raidz_t *vdrz = vd->vdev_tsd;
4043 if (faulted > vdrz->vd_nparity)
4044 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
4045 VDEV_AUX_NO_REPLICAS);
4046 else if (degraded + faulted != 0)
4047 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
4048 else
4049 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
4050 }
4051
4052 /*
4053 * Determine if any portion of the provided block resides on a child vdev
4054 * with a dirty DTL and therefore needs to be resilvered. The function
4055 * assumes that at least one DTL is dirty which implies that full stripe
4056 * width blocks must be resilvered.
4057 */
4058 static boolean_t
vdev_raidz_need_resilver(vdev_t * vd,const dva_t * dva,size_t psize,uint64_t phys_birth)4059 vdev_raidz_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
4060 uint64_t phys_birth)
4061 {
4062 vdev_raidz_t *vdrz = vd->vdev_tsd;
4063
4064 /*
4065 * If we're in the middle of a RAIDZ expansion, this block may be in
4066 * the old and/or new location. For simplicity, always resilver it.
4067 */
4068 if (vdrz->vn_vre.vre_state == DSS_SCANNING)
4069 return (B_TRUE);
4070
4071 uint64_t dcols = vd->vdev_children;
4072 uint64_t nparity = vdrz->vd_nparity;
4073 uint64_t ashift = vd->vdev_top->vdev_ashift;
4074 /* The starting RAIDZ (parent) vdev sector of the block. */
4075 uint64_t b = DVA_GET_OFFSET(dva) >> ashift;
4076 /* The zio's size in units of the vdev's minimum sector size. */
4077 uint64_t s = ((psize - 1) >> ashift) + 1;
4078 /* The first column for this stripe. */
4079 uint64_t f = b % dcols;
4080
4081 /* Unreachable by sequential resilver. */
4082 ASSERT3U(phys_birth, !=, TXG_UNKNOWN);
4083
4084 if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1))
4085 return (B_FALSE);
4086
4087 if (s + nparity >= dcols)
4088 return (B_TRUE);
4089
4090 for (uint64_t c = 0; c < s + nparity; c++) {
4091 uint64_t devidx = (f + c) % dcols;
4092 vdev_t *cvd = vd->vdev_child[devidx];
4093
4094 /*
4095 * dsl_scan_need_resilver() already checked vd with
4096 * vdev_dtl_contains(). So here just check cvd with
4097 * vdev_dtl_empty(), cheaper and a good approximation.
4098 */
4099 if (!vdev_dtl_empty(cvd, DTL_PARTIAL))
4100 return (B_TRUE);
4101 }
4102
4103 return (B_FALSE);
4104 }
4105
4106 static void
vdev_raidz_xlate(vdev_t * cvd,const zfs_range_seg64_t * logical_rs,zfs_range_seg64_t * physical_rs,zfs_range_seg64_t * remain_rs)4107 vdev_raidz_xlate(vdev_t *cvd, const zfs_range_seg64_t *logical_rs,
4108 zfs_range_seg64_t *physical_rs, zfs_range_seg64_t *remain_rs)
4109 {
4110 (void) remain_rs;
4111
4112 vdev_t *raidvd = cvd->vdev_parent;
4113 ASSERT(raidvd->vdev_ops == &vdev_raidz_ops);
4114
4115 vdev_raidz_t *vdrz = raidvd->vdev_tsd;
4116
4117 if (vdrz->vn_vre.vre_state == DSS_SCANNING) {
4118 /*
4119 * We're in the middle of expansion, in which case the
4120 * translation is in flux. Any answer we give may be wrong
4121 * by the time we return, so it isn't safe for the caller to
4122 * act on it. Therefore we say that this range isn't present
4123 * on any children. The only consumers of this are "zpool
4124 * initialize" and trimming, both of which are "best effort"
4125 * anyway.
4126 */
4127 physical_rs->rs_start = physical_rs->rs_end = 0;
4128 remain_rs->rs_start = remain_rs->rs_end = 0;
4129 return;
4130 }
4131
4132 uint64_t width = vdrz->vd_physical_width;
4133 uint64_t tgt_col = cvd->vdev_id;
4134 uint64_t ashift = raidvd->vdev_top->vdev_ashift;
4135
4136 /* make sure the offsets are block-aligned */
4137 ASSERT0(logical_rs->rs_start % (1 << ashift));
4138 ASSERT0(logical_rs->rs_end % (1 << ashift));
4139 uint64_t b_start = logical_rs->rs_start >> ashift;
4140 uint64_t b_end = logical_rs->rs_end >> ashift;
4141
4142 uint64_t start_row = 0;
4143 if (b_start > tgt_col) /* avoid underflow */
4144 start_row = ((b_start - tgt_col - 1) / width) + 1;
4145
4146 uint64_t end_row = 0;
4147 if (b_end > tgt_col)
4148 end_row = ((b_end - tgt_col - 1) / width) + 1;
4149
4150 physical_rs->rs_start = start_row << ashift;
4151 physical_rs->rs_end = end_row << ashift;
4152
4153 ASSERT3U(physical_rs->rs_start, <=, logical_rs->rs_start);
4154 ASSERT3U(physical_rs->rs_end - physical_rs->rs_start, <=,
4155 logical_rs->rs_end - logical_rs->rs_start);
4156 }
4157
4158 static void
raidz_reflow_sync(void * arg,dmu_tx_t * tx)4159 raidz_reflow_sync(void *arg, dmu_tx_t *tx)
4160 {
4161 spa_t *spa = arg;
4162 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
4163 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
4164
4165 /*
4166 * Ensure there are no i/os to the range that is being committed.
4167 */
4168 uint64_t old_offset = RRSS_GET_OFFSET(&spa->spa_uberblock);
4169 ASSERT3U(vre->vre_offset_pertxg[txgoff], >=, old_offset);
4170
4171 mutex_enter(&vre->vre_lock);
4172 uint64_t new_offset =
4173 MIN(vre->vre_offset_pertxg[txgoff], vre->vre_failed_offset);
4174 /*
4175 * We should not have committed anything that failed.
4176 */
4177 VERIFY3U(vre->vre_failed_offset, >=, old_offset);
4178 mutex_exit(&vre->vre_lock);
4179
4180 zfs_locked_range_t *lr = zfs_rangelock_enter(&vre->vre_rangelock,
4181 old_offset, new_offset - old_offset,
4182 RL_WRITER);
4183
4184 /*
4185 * Update the uberblock that will be written when this txg completes.
4186 */
4187 RAIDZ_REFLOW_SET(&spa->spa_uberblock,
4188 RRSS_SCRATCH_INVALID_SYNCED_REFLOW, new_offset);
4189 vre->vre_offset_pertxg[txgoff] = 0;
4190 zfs_rangelock_exit(lr);
4191
4192 mutex_enter(&vre->vre_lock);
4193 vre->vre_bytes_copied += vre->vre_bytes_copied_pertxg[txgoff];
4194 vre->vre_bytes_copied_pertxg[txgoff] = 0;
4195 mutex_exit(&vre->vre_lock);
4196
4197 vdev_t *vd = vdev_lookup_top(spa, vre->vre_vdev_id);
4198 VERIFY0(zap_update(spa->spa_meta_objset,
4199 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED,
4200 sizeof (vre->vre_bytes_copied), 1, &vre->vre_bytes_copied, tx));
4201 }
4202
4203 static void
raidz_reflow_complete_sync(void * arg,dmu_tx_t * tx)4204 raidz_reflow_complete_sync(void *arg, dmu_tx_t *tx)
4205 {
4206 spa_t *spa = arg;
4207 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
4208 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
4209 vdev_raidz_t *vdrz = raidvd->vdev_tsd;
4210
4211 for (int i = 0; i < TXG_SIZE; i++)
4212 VERIFY0(vre->vre_offset_pertxg[i]);
4213
4214 reflow_node_t *re = kmem_zalloc(sizeof (*re), KM_SLEEP);
4215 re->re_txg = tx->tx_txg + TXG_CONCURRENT_STATES;
4216 re->re_logical_width = vdrz->vd_physical_width;
4217 mutex_enter(&vdrz->vd_expand_lock);
4218 avl_add(&vdrz->vd_expand_txgs, re);
4219 mutex_exit(&vdrz->vd_expand_lock);
4220
4221 vdev_t *vd = vdev_lookup_top(spa, vre->vre_vdev_id);
4222
4223 /*
4224 * Dirty the config so that the updated ZPOOL_CONFIG_RAIDZ_EXPAND_TXGS
4225 * will get written (based on vd_expand_txgs).
4226 */
4227 vdev_config_dirty(vd);
4228
4229 /*
4230 * Before we change vre_state, the on-disk state must reflect that we
4231 * have completed all copying, so that vdev_raidz_io_start() can use
4232 * vre_state to determine if the reflow is in progress. See also the
4233 * end of spa_raidz_expand_thread().
4234 */
4235 VERIFY3U(RRSS_GET_OFFSET(&spa->spa_ubsync), ==,
4236 raidvd->vdev_ms_count << raidvd->vdev_ms_shift);
4237
4238 vre->vre_end_time = gethrestime_sec();
4239 vre->vre_state = DSS_FINISHED;
4240
4241 uint64_t state = vre->vre_state;
4242 VERIFY0(zap_update(spa->spa_meta_objset,
4243 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE,
4244 sizeof (state), 1, &state, tx));
4245
4246 uint64_t end_time = vre->vre_end_time;
4247 VERIFY0(zap_update(spa->spa_meta_objset,
4248 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME,
4249 sizeof (end_time), 1, &end_time, tx));
4250
4251 spa->spa_uberblock.ub_raidz_reflow_info = 0;
4252
4253 spa_history_log_internal(spa, "raidz vdev expansion completed", tx,
4254 "%s vdev %llu new width %llu", spa_name(spa),
4255 (unsigned long long)vd->vdev_id,
4256 (unsigned long long)vd->vdev_children);
4257
4258 spa->spa_raidz_expand = NULL;
4259 raidvd->vdev_rz_expanding = B_FALSE;
4260
4261 spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
4262 spa_async_request(spa, SPA_ASYNC_TRIM_RESTART);
4263 spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART);
4264
4265 spa_notify_waiters(spa);
4266
4267 /*
4268 * While we're in syncing context take the opportunity to
4269 * setup a scrub. All the data has been sucessfully copied
4270 * but we have not validated any checksums.
4271 */
4272 setup_sync_arg_t setup_sync_arg = {
4273 .func = POOL_SCAN_SCRUB,
4274 .txgstart = 0,
4275 .txgend = 0,
4276 };
4277 if (zfs_scrub_after_expand &&
4278 dsl_scan_setup_check(&setup_sync_arg.func, tx) == 0) {
4279 dsl_scan_setup_sync(&setup_sync_arg, tx);
4280 }
4281 }
4282
4283 /*
4284 * State of one copy batch.
4285 */
4286 typedef struct raidz_reflow_arg {
4287 vdev_raidz_expand_t *rra_vre; /* Global expantion state. */
4288 zfs_locked_range_t *rra_lr; /* Range lock of this batch. */
4289 uint64_t rra_txg; /* TXG of this batch. */
4290 uint_t rra_ashift; /* Ashift of the vdev. */
4291 uint32_t rra_tbd; /* Number of in-flight ZIOs. */
4292 uint32_t rra_writes; /* Number of write ZIOs. */
4293 zio_t *rra_zio[]; /* Write ZIO pointers. */
4294 } raidz_reflow_arg_t;
4295
4296 /*
4297 * Write of the new location on one child is done. Once all of them are done
4298 * we can unlock and free everything.
4299 */
4300 static void
raidz_reflow_write_done(zio_t * zio)4301 raidz_reflow_write_done(zio_t *zio)
4302 {
4303 raidz_reflow_arg_t *rra = zio->io_private;
4304 vdev_raidz_expand_t *vre = rra->rra_vre;
4305
4306 abd_free(zio->io_abd);
4307
4308 mutex_enter(&vre->vre_lock);
4309 if (zio->io_error != 0) {
4310 /* Force a reflow pause on errors */
4311 vre->vre_failed_offset =
4312 MIN(vre->vre_failed_offset, rra->rra_lr->lr_offset);
4313 }
4314 ASSERT3U(vre->vre_outstanding_bytes, >=, zio->io_size);
4315 vre->vre_outstanding_bytes -= zio->io_size;
4316 if (rra->rra_lr->lr_offset + rra->rra_lr->lr_length <
4317 vre->vre_failed_offset) {
4318 vre->vre_bytes_copied_pertxg[rra->rra_txg & TXG_MASK] +=
4319 zio->io_size;
4320 }
4321 cv_signal(&vre->vre_cv);
4322 boolean_t done = (--rra->rra_tbd == 0);
4323 mutex_exit(&vre->vre_lock);
4324
4325 if (!done)
4326 return;
4327 spa_config_exit(zio->io_spa, SCL_STATE, zio->io_spa);
4328 zfs_rangelock_exit(rra->rra_lr);
4329 kmem_free(rra, sizeof (*rra) + sizeof (zio_t *) * rra->rra_writes);
4330 }
4331
4332 /*
4333 * Read of the old location on one child is done. Once all of them are done
4334 * writes should have all the data and we can issue them.
4335 */
4336 static void
raidz_reflow_read_done(zio_t * zio)4337 raidz_reflow_read_done(zio_t *zio)
4338 {
4339 raidz_reflow_arg_t *rra = zio->io_private;
4340 vdev_raidz_expand_t *vre = rra->rra_vre;
4341
4342 /* Reads of only one block use write ABDs. For bigger free gangs. */
4343 if (zio->io_size > (1 << rra->rra_ashift))
4344 abd_free(zio->io_abd);
4345
4346 /*
4347 * If the read failed, or if it was done on a vdev that is not fully
4348 * healthy (e.g. a child that has a resilver in progress), we may not
4349 * have the correct data. Note that it's OK if the write proceeds.
4350 * It may write garbage but the location is otherwise unused and we
4351 * will retry later due to vre_failed_offset.
4352 */
4353 if (zio->io_error != 0 || !vdev_dtl_empty(zio->io_vd, DTL_MISSING)) {
4354 zfs_dbgmsg("reflow read failed off=%llu size=%llu txg=%llu "
4355 "err=%u partial_dtl_empty=%u missing_dtl_empty=%u",
4356 (long long)rra->rra_lr->lr_offset,
4357 (long long)rra->rra_lr->lr_length,
4358 (long long)rra->rra_txg,
4359 zio->io_error,
4360 vdev_dtl_empty(zio->io_vd, DTL_PARTIAL),
4361 vdev_dtl_empty(zio->io_vd, DTL_MISSING));
4362 mutex_enter(&vre->vre_lock);
4363 /* Force a reflow pause on errors */
4364 vre->vre_failed_offset =
4365 MIN(vre->vre_failed_offset, rra->rra_lr->lr_offset);
4366 mutex_exit(&vre->vre_lock);
4367 }
4368
4369 if (atomic_dec_32_nv(&rra->rra_tbd) > 0)
4370 return;
4371 uint32_t writes = rra->rra_tbd = rra->rra_writes;
4372 for (uint64_t i = 0; i < writes; i++)
4373 zio_nowait(rra->rra_zio[i]);
4374 }
4375
4376 static void
raidz_reflow_record_progress(vdev_raidz_expand_t * vre,uint64_t offset,dmu_tx_t * tx)4377 raidz_reflow_record_progress(vdev_raidz_expand_t *vre, uint64_t offset,
4378 dmu_tx_t *tx)
4379 {
4380 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
4381 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4382
4383 if (offset == 0)
4384 return;
4385
4386 mutex_enter(&vre->vre_lock);
4387 ASSERT3U(vre->vre_offset, <=, offset);
4388 vre->vre_offset = offset;
4389 mutex_exit(&vre->vre_lock);
4390
4391 if (vre->vre_offset_pertxg[txgoff] == 0) {
4392 dsl_sync_task_nowait(dmu_tx_pool(tx), raidz_reflow_sync,
4393 spa, tx);
4394 }
4395 vre->vre_offset_pertxg[txgoff] = offset;
4396 }
4397
4398 static boolean_t
vdev_raidz_expand_child_replacing(vdev_t * raidz_vd)4399 vdev_raidz_expand_child_replacing(vdev_t *raidz_vd)
4400 {
4401 for (int i = 0; i < raidz_vd->vdev_children; i++) {
4402 /* Quick check if a child is being replaced */
4403 if (!raidz_vd->vdev_child[i]->vdev_ops->vdev_op_leaf)
4404 return (B_TRUE);
4405 }
4406 return (B_FALSE);
4407 }
4408
4409 static boolean_t
raidz_reflow_impl(vdev_t * vd,vdev_raidz_expand_t * vre,zfs_range_tree_t * rt,dmu_tx_t * tx)4410 raidz_reflow_impl(vdev_t *vd, vdev_raidz_expand_t *vre, zfs_range_tree_t *rt,
4411 dmu_tx_t *tx)
4412 {
4413 spa_t *spa = vd->vdev_spa;
4414 uint_t ashift = vd->vdev_top->vdev_ashift;
4415
4416 zfs_range_seg_t *rs = zfs_range_tree_first(rt);
4417 if (rt == NULL)
4418 return (B_FALSE);
4419 uint64_t offset = zfs_rs_get_start(rs, rt);
4420 ASSERT(IS_P2ALIGNED(offset, 1 << ashift));
4421 uint64_t size = zfs_rs_get_end(rs, rt) - offset;
4422 ASSERT3U(size, >=, 1 << ashift);
4423 ASSERT(IS_P2ALIGNED(size, 1 << ashift));
4424
4425 uint64_t blkid = offset >> ashift;
4426 uint_t old_children = vd->vdev_children - 1;
4427
4428 /*
4429 * We can only progress to the point that writes will not overlap
4430 * with blocks whose progress has not yet been recorded on disk.
4431 * Since partially-copied rows are still read from the old location,
4432 * we need to stop one row before the sector-wise overlap, to prevent
4433 * row-wise overlap.
4434 *
4435 * Note that even if we are skipping over a large unallocated region,
4436 * we can't move the on-disk progress to `offset`, because concurrent
4437 * writes/allocations could still use the currently-unallocated
4438 * region.
4439 */
4440 uint64_t ubsync_blkid =
4441 RRSS_GET_OFFSET(&spa->spa_ubsync) >> ashift;
4442 uint64_t next_overwrite_blkid = ubsync_blkid +
4443 ubsync_blkid / old_children - old_children;
4444 VERIFY3U(next_overwrite_blkid, >, ubsync_blkid);
4445 if (blkid >= next_overwrite_blkid) {
4446 raidz_reflow_record_progress(vre,
4447 next_overwrite_blkid << ashift, tx);
4448 return (B_TRUE);
4449 }
4450
4451 size = MIN(size, raidz_expand_max_copy_bytes);
4452 size = MIN(size, (uint64_t)old_children *
4453 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE));
4454 size = MAX(size, 1 << ashift);
4455 uint_t blocks = MIN(size >> ashift, next_overwrite_blkid - blkid);
4456 size = (uint64_t)blocks << ashift;
4457
4458 zfs_range_tree_remove(rt, offset, size);
4459
4460 uint_t reads = MIN(blocks, old_children);
4461 uint_t writes = MIN(blocks, vd->vdev_children);
4462 raidz_reflow_arg_t *rra = kmem_zalloc(sizeof (*rra) +
4463 sizeof (zio_t *) * writes, KM_SLEEP);
4464 rra->rra_vre = vre;
4465 rra->rra_lr = zfs_rangelock_enter(&vre->vre_rangelock,
4466 offset, size, RL_WRITER);
4467 rra->rra_txg = dmu_tx_get_txg(tx);
4468 rra->rra_ashift = ashift;
4469 rra->rra_tbd = reads;
4470 rra->rra_writes = writes;
4471
4472 raidz_reflow_record_progress(vre, offset + size, tx);
4473
4474 /*
4475 * SCL_STATE will be released when the read and write are done,
4476 * by raidz_reflow_write_done().
4477 */
4478 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
4479
4480 /* check if a replacing vdev was added, if so treat it as an error */
4481 if (vdev_raidz_expand_child_replacing(vd)) {
4482 zfs_dbgmsg("replacing vdev encountered, reflow paused at "
4483 "offset=%llu txg=%llu",
4484 (long long)rra->rra_lr->lr_offset,
4485 (long long)rra->rra_txg);
4486
4487 mutex_enter(&vre->vre_lock);
4488 vre->vre_failed_offset =
4489 MIN(vre->vre_failed_offset, rra->rra_lr->lr_offset);
4490 cv_signal(&vre->vre_cv);
4491 mutex_exit(&vre->vre_lock);
4492
4493 /* drop everything we acquired */
4494 spa_config_exit(spa, SCL_STATE, spa);
4495 zfs_rangelock_exit(rra->rra_lr);
4496 kmem_free(rra, sizeof (*rra) + sizeof (zio_t *) * writes);
4497 return (B_TRUE);
4498 }
4499
4500 mutex_enter(&vre->vre_lock);
4501 vre->vre_outstanding_bytes += size;
4502 mutex_exit(&vre->vre_lock);
4503
4504 /* Allocate ABD and ZIO for each child we write. */
4505 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
4506 zio_t *pio = spa->spa_txg_zio[txgoff];
4507 uint_t b = blocks / vd->vdev_children;
4508 uint_t bb = blocks % vd->vdev_children;
4509 for (uint_t i = 0; i < writes; i++) {
4510 uint_t n = b + (i < bb);
4511 abd_t *abd = abd_alloc_for_io(n << ashift, B_FALSE);
4512 rra->rra_zio[i] = zio_vdev_child_io(pio, NULL,
4513 vd->vdev_child[(blkid + i) % vd->vdev_children],
4514 ((blkid + i) / vd->vdev_children) << ashift,
4515 abd, n << ashift, ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL,
4516 ZIO_FLAG_CANFAIL, raidz_reflow_write_done, rra);
4517 }
4518
4519 /*
4520 * Allocate and issue ZIO for each child we read. For reads of only
4521 * one block we can use respective writer ABDs, since they will also
4522 * have only one block. For bigger reads create gang ABDs and fill
4523 * them with respective blocks from writer ABDs.
4524 */
4525 b = blocks / old_children;
4526 bb = blocks % old_children;
4527 for (uint_t i = 0; i < reads; i++) {
4528 uint_t n = b + (i < bb);
4529 abd_t *abd;
4530 if (n > 1) {
4531 abd = abd_alloc_gang();
4532 for (uint_t j = 0; j < n; j++) {
4533 uint_t b = j * old_children + i;
4534 abd_t *cabd = abd_get_offset_size(
4535 rra->rra_zio[b % vd->vdev_children]->io_abd,
4536 (b / vd->vdev_children) << ashift,
4537 1 << ashift);
4538 abd_gang_add(abd, cabd, B_TRUE);
4539 }
4540 } else {
4541 abd = rra->rra_zio[i]->io_abd;
4542 }
4543 zio_nowait(zio_vdev_child_io(pio, NULL,
4544 vd->vdev_child[(blkid + i) % old_children],
4545 ((blkid + i) / old_children) << ashift, abd,
4546 n << ashift, ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL,
4547 ZIO_FLAG_CANFAIL, raidz_reflow_read_done, rra));
4548 }
4549
4550 return (B_FALSE);
4551 }
4552
4553 /*
4554 * For testing (ztest specific)
4555 */
4556 static void
raidz_expand_pause(uint_t pause_point)4557 raidz_expand_pause(uint_t pause_point)
4558 {
4559 while (raidz_expand_pause_point != 0 &&
4560 raidz_expand_pause_point <= pause_point)
4561 delay(hz);
4562 }
4563
4564 static void
raidz_scratch_child_done(zio_t * zio)4565 raidz_scratch_child_done(zio_t *zio)
4566 {
4567 zio_t *pio = zio->io_private;
4568
4569 mutex_enter(&pio->io_lock);
4570 pio->io_error = zio_worst_error(pio->io_error, zio->io_error);
4571 mutex_exit(&pio->io_lock);
4572 }
4573
4574 /*
4575 * Reflow the beginning portion of the vdev into an intermediate scratch area
4576 * in memory and on disk. This operation must be persisted on disk before we
4577 * proceed to overwrite the beginning portion with the reflowed data.
4578 *
4579 * This multi-step task can fail to complete if disk errors are encountered
4580 * and we can return here after a pause (waiting for disk to become healthy).
4581 */
4582 static void
raidz_reflow_scratch_sync(void * arg,dmu_tx_t * tx)4583 raidz_reflow_scratch_sync(void *arg, dmu_tx_t *tx)
4584 {
4585 vdev_raidz_expand_t *vre = arg;
4586 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4587 zio_t *pio;
4588 int error;
4589
4590 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4591 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
4592 int ashift = raidvd->vdev_ashift;
4593 uint64_t write_size = P2ALIGN_TYPED(VDEV_BOOT_SIZE, 1 << ashift,
4594 uint64_t);
4595 uint64_t logical_size = write_size * raidvd->vdev_children;
4596 uint64_t read_size =
4597 P2ROUNDUP(DIV_ROUND_UP(logical_size, (raidvd->vdev_children - 1)),
4598 1 << ashift);
4599
4600 /*
4601 * The scratch space must be large enough to get us to the point
4602 * that one row does not overlap itself when moved. This is checked
4603 * by vdev_raidz_attach_check().
4604 */
4605 VERIFY3U(write_size, >=, raidvd->vdev_children << ashift);
4606 VERIFY3U(write_size, <=, VDEV_BOOT_SIZE);
4607 VERIFY3U(write_size, <=, read_size);
4608
4609 zfs_locked_range_t *lr = zfs_rangelock_enter(&vre->vre_rangelock,
4610 0, logical_size, RL_WRITER);
4611
4612 abd_t **abds = kmem_alloc(raidvd->vdev_children * sizeof (abd_t *),
4613 KM_SLEEP);
4614 for (int i = 0; i < raidvd->vdev_children; i++) {
4615 abds[i] = abd_alloc_linear(read_size, B_FALSE);
4616 }
4617
4618 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_PRE_SCRATCH_1);
4619
4620 /*
4621 * If we have already written the scratch area then we must read from
4622 * there, since new writes were redirected there while we were paused
4623 * or the original location may have been partially overwritten with
4624 * reflowed data.
4625 */
4626 if (RRSS_GET_STATE(&spa->spa_ubsync) == RRSS_SCRATCH_VALID) {
4627 VERIFY3U(RRSS_GET_OFFSET(&spa->spa_ubsync), ==, logical_size);
4628 /*
4629 * Read from scratch space.
4630 */
4631 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
4632 for (int i = 0; i < raidvd->vdev_children; i++) {
4633 /*
4634 * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE
4635 * to the offset to calculate the physical offset to
4636 * write to. Passing in a negative offset makes us
4637 * access the scratch area.
4638 */
4639 zio_nowait(zio_vdev_child_io(pio, NULL,
4640 raidvd->vdev_child[i],
4641 VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abds[i],
4642 write_size, ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL,
4643 ZIO_FLAG_CANFAIL, raidz_scratch_child_done, pio));
4644 }
4645 error = zio_wait(pio);
4646 if (error != 0) {
4647 zfs_dbgmsg("reflow: error %d reading scratch location",
4648 error);
4649 goto io_error_exit;
4650 }
4651 goto overwrite;
4652 }
4653
4654 /*
4655 * Read from original location.
4656 */
4657 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
4658 for (int i = 0; i < raidvd->vdev_children - 1; i++) {
4659 ASSERT0(vdev_is_dead(raidvd->vdev_child[i]));
4660 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i],
4661 0, abds[i], read_size, ZIO_TYPE_READ,
4662 ZIO_PRIORITY_REMOVAL, ZIO_FLAG_CANFAIL,
4663 raidz_scratch_child_done, pio));
4664 }
4665 error = zio_wait(pio);
4666 if (error != 0) {
4667 zfs_dbgmsg("reflow: error %d reading original location", error);
4668 io_error_exit:
4669 for (int i = 0; i < raidvd->vdev_children; i++)
4670 abd_free(abds[i]);
4671 kmem_free(abds, raidvd->vdev_children * sizeof (abd_t *));
4672 zfs_rangelock_exit(lr);
4673 spa_config_exit(spa, SCL_STATE, FTAG);
4674 return;
4675 }
4676
4677 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_PRE_SCRATCH_2);
4678
4679 /*
4680 * Reflow in memory.
4681 */
4682 uint64_t logical_sectors = logical_size >> ashift;
4683 for (int i = raidvd->vdev_children - 1; i < logical_sectors; i++) {
4684 int oldchild = i % (raidvd->vdev_children - 1);
4685 uint64_t oldoff = (i / (raidvd->vdev_children - 1)) << ashift;
4686
4687 int newchild = i % raidvd->vdev_children;
4688 uint64_t newoff = (i / raidvd->vdev_children) << ashift;
4689
4690 /* a single sector should not be copying over itself */
4691 ASSERT(!(newchild == oldchild && newoff == oldoff));
4692
4693 abd_copy_off(abds[newchild], abds[oldchild],
4694 newoff, oldoff, 1 << ashift);
4695 }
4696
4697 /*
4698 * Verify that we filled in everything we intended to (write_size on
4699 * each child).
4700 */
4701 VERIFY0(logical_sectors % raidvd->vdev_children);
4702 VERIFY3U((logical_sectors / raidvd->vdev_children) << ashift, ==,
4703 write_size);
4704
4705 /*
4706 * Write to scratch location (boot area).
4707 */
4708 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
4709 for (int i = 0; i < raidvd->vdev_children; i++) {
4710 /*
4711 * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE to
4712 * the offset to calculate the physical offset to write to.
4713 * Passing in a negative offset lets us access the boot area.
4714 */
4715 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i],
4716 VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abds[i],
4717 write_size, ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL,
4718 ZIO_FLAG_CANFAIL, raidz_scratch_child_done, pio));
4719 }
4720 error = zio_wait(pio);
4721 if (error != 0) {
4722 zfs_dbgmsg("reflow: error %d writing scratch location", error);
4723 goto io_error_exit;
4724 }
4725 pio = zio_root(spa, NULL, NULL, 0);
4726 zio_flush(pio, raidvd);
4727 zio_wait(pio);
4728
4729 zfs_dbgmsg("reflow: wrote %llu bytes (logical) to scratch area",
4730 (long long)logical_size);
4731
4732 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_PRE_SCRATCH_3);
4733
4734 /*
4735 * Update uberblock to indicate that scratch space is valid. This is
4736 * needed because after this point, the real location may be
4737 * overwritten. If we crash, we need to get the data from the
4738 * scratch space, rather than the real location.
4739 *
4740 * Note: ub_timestamp is bumped so that vdev_uberblock_compare()
4741 * will prefer this uberblock.
4742 */
4743 RAIDZ_REFLOW_SET(&spa->spa_ubsync, RRSS_SCRATCH_VALID, logical_size);
4744 spa->spa_ubsync.ub_timestamp++;
4745 ASSERT0(vdev_uberblock_sync_list(&spa->spa_root_vdev, 1,
4746 &spa->spa_ubsync, ZIO_FLAG_CONFIG_WRITER));
4747 if (spa_multihost(spa))
4748 mmp_update_uberblock(spa, &spa->spa_ubsync);
4749
4750 zfs_dbgmsg("reflow: uberblock updated "
4751 "(txg %llu, SCRATCH_VALID, size %llu, ts %llu)",
4752 (long long)spa->spa_ubsync.ub_txg,
4753 (long long)logical_size,
4754 (long long)spa->spa_ubsync.ub_timestamp);
4755
4756 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_VALID);
4757
4758 /*
4759 * Overwrite with reflow'ed data.
4760 */
4761 overwrite:
4762 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
4763 for (int i = 0; i < raidvd->vdev_children; i++) {
4764 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i],
4765 0, abds[i], write_size, ZIO_TYPE_WRITE,
4766 ZIO_PRIORITY_REMOVAL, ZIO_FLAG_CANFAIL,
4767 raidz_scratch_child_done, pio));
4768 }
4769 error = zio_wait(pio);
4770 if (error != 0) {
4771 /*
4772 * When we exit early here and drop the range lock, new
4773 * writes will go into the scratch area so we'll need to
4774 * read from there when we return after pausing.
4775 */
4776 zfs_dbgmsg("reflow: error %d writing real location", error);
4777 /*
4778 * Update the uberblock that is written when this txg completes.
4779 */
4780 RAIDZ_REFLOW_SET(&spa->spa_uberblock, RRSS_SCRATCH_VALID,
4781 logical_size);
4782 goto io_error_exit;
4783 }
4784 pio = zio_root(spa, NULL, NULL, 0);
4785 zio_flush(pio, raidvd);
4786 zio_wait(pio);
4787
4788 zfs_dbgmsg("reflow: overwrote %llu bytes (logical) to real location",
4789 (long long)logical_size);
4790 for (int i = 0; i < raidvd->vdev_children; i++)
4791 abd_free(abds[i]);
4792 kmem_free(abds, raidvd->vdev_children * sizeof (abd_t *));
4793
4794 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_REFLOWED);
4795
4796 /*
4797 * Update uberblock to indicate that the initial part has been
4798 * reflow'ed. This is needed because after this point (when we exit
4799 * the rangelock), we allow regular writes to this region, which will
4800 * be written to the new location only (because reflow_offset_next ==
4801 * reflow_offset_synced). If we crashed and re-copied from the
4802 * scratch space, we would lose the regular writes.
4803 */
4804 RAIDZ_REFLOW_SET(&spa->spa_ubsync, RRSS_SCRATCH_INVALID_SYNCED,
4805 logical_size);
4806 spa->spa_ubsync.ub_timestamp++;
4807 ASSERT0(vdev_uberblock_sync_list(&spa->spa_root_vdev, 1,
4808 &spa->spa_ubsync, ZIO_FLAG_CONFIG_WRITER));
4809 if (spa_multihost(spa))
4810 mmp_update_uberblock(spa, &spa->spa_ubsync);
4811
4812 zfs_dbgmsg("reflow: uberblock updated "
4813 "(txg %llu, SCRATCH_NOT_IN_USE, size %llu, ts %llu)",
4814 (long long)spa->spa_ubsync.ub_txg,
4815 (long long)logical_size,
4816 (long long)spa->spa_ubsync.ub_timestamp);
4817
4818 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_POST_REFLOW_1);
4819
4820 /*
4821 * Update progress.
4822 */
4823 vre->vre_offset = logical_size;
4824 zfs_rangelock_exit(lr);
4825 spa_config_exit(spa, SCL_STATE, FTAG);
4826
4827 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
4828 vre->vre_offset_pertxg[txgoff] = vre->vre_offset;
4829 vre->vre_bytes_copied_pertxg[txgoff] = vre->vre_bytes_copied;
4830 /*
4831 * Note - raidz_reflow_sync() will update the uberblock state to
4832 * RRSS_SCRATCH_INVALID_SYNCED_REFLOW
4833 */
4834 raidz_reflow_sync(spa, tx);
4835
4836 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_POST_REFLOW_2);
4837 }
4838
4839 /*
4840 * We crashed in the middle of raidz_reflow_scratch_sync(); complete its work
4841 * here. No other i/o can be in progress, so we don't need the vre_rangelock.
4842 */
4843 void
vdev_raidz_reflow_copy_scratch(spa_t * spa)4844 vdev_raidz_reflow_copy_scratch(spa_t *spa)
4845 {
4846 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
4847 uint64_t logical_size = RRSS_GET_OFFSET(&spa->spa_uberblock);
4848 ASSERT3U(RRSS_GET_STATE(&spa->spa_uberblock), ==, RRSS_SCRATCH_VALID);
4849
4850 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4851 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
4852 ASSERT0(logical_size % raidvd->vdev_children);
4853 uint64_t write_size = logical_size / raidvd->vdev_children;
4854
4855 zio_t *pio;
4856
4857 /*
4858 * Read from scratch space.
4859 */
4860 abd_t **abds = kmem_alloc(raidvd->vdev_children * sizeof (abd_t *),
4861 KM_SLEEP);
4862 for (int i = 0; i < raidvd->vdev_children; i++) {
4863 abds[i] = abd_alloc_linear(write_size, B_FALSE);
4864 }
4865
4866 pio = zio_root(spa, NULL, NULL, 0);
4867 for (int i = 0; i < raidvd->vdev_children; i++) {
4868 /*
4869 * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE to
4870 * the offset to calculate the physical offset to write to.
4871 * Passing in a negative offset lets us access the boot area.
4872 */
4873 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i],
4874 VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abds[i],
4875 write_size, ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL, 0,
4876 raidz_scratch_child_done, pio));
4877 }
4878 zio_wait(pio);
4879
4880 /*
4881 * Overwrite real location with reflow'ed data.
4882 */
4883 pio = zio_root(spa, NULL, NULL, 0);
4884 for (int i = 0; i < raidvd->vdev_children; i++) {
4885 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i],
4886 0, abds[i], write_size, ZIO_TYPE_WRITE,
4887 ZIO_PRIORITY_REMOVAL, 0,
4888 raidz_scratch_child_done, pio));
4889 }
4890 zio_wait(pio);
4891 pio = zio_root(spa, NULL, NULL, 0);
4892 zio_flush(pio, raidvd);
4893 zio_wait(pio);
4894
4895 zfs_dbgmsg("reflow recovery: overwrote %llu bytes (logical) "
4896 "to real location", (long long)logical_size);
4897
4898 for (int i = 0; i < raidvd->vdev_children; i++)
4899 abd_free(abds[i]);
4900 kmem_free(abds, raidvd->vdev_children * sizeof (abd_t *));
4901
4902 /*
4903 * Update uberblock.
4904 */
4905 RAIDZ_REFLOW_SET(&spa->spa_ubsync,
4906 RRSS_SCRATCH_INVALID_SYNCED_ON_IMPORT, logical_size);
4907 spa->spa_ubsync.ub_timestamp++;
4908 VERIFY0(vdev_uberblock_sync_list(&spa->spa_root_vdev, 1,
4909 &spa->spa_ubsync, ZIO_FLAG_CONFIG_WRITER));
4910 if (spa_multihost(spa))
4911 mmp_update_uberblock(spa, &spa->spa_ubsync);
4912
4913 zfs_dbgmsg("reflow recovery: uberblock updated "
4914 "(txg %llu, SCRATCH_NOT_IN_USE, size %llu, ts %llu)",
4915 (long long)spa->spa_ubsync.ub_txg,
4916 (long long)logical_size,
4917 (long long)spa->spa_ubsync.ub_timestamp);
4918
4919 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool,
4920 spa_first_txg(spa));
4921 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
4922 vre->vre_offset = logical_size;
4923 vre->vre_offset_pertxg[txgoff] = vre->vre_offset;
4924 vre->vre_bytes_copied_pertxg[txgoff] = vre->vre_bytes_copied;
4925 /*
4926 * Note that raidz_reflow_sync() will update the uberblock once more
4927 */
4928 raidz_reflow_sync(spa, tx);
4929
4930 dmu_tx_commit(tx);
4931
4932 spa_config_exit(spa, SCL_STATE, FTAG);
4933 }
4934
4935 static boolean_t
spa_raidz_expand_thread_check(void * arg,zthr_t * zthr)4936 spa_raidz_expand_thread_check(void *arg, zthr_t *zthr)
4937 {
4938 (void) zthr;
4939 spa_t *spa = arg;
4940
4941 return (spa->spa_raidz_expand != NULL &&
4942 !spa->spa_raidz_expand->vre_waiting_for_resilver);
4943 }
4944
4945 /*
4946 * RAIDZ expansion background thread
4947 *
4948 * Can be called multiple times if the reflow is paused
4949 */
4950 static void
spa_raidz_expand_thread(void * arg,zthr_t * zthr)4951 spa_raidz_expand_thread(void *arg, zthr_t *zthr)
4952 {
4953 spa_t *spa = arg;
4954 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
4955
4956 if (RRSS_GET_STATE(&spa->spa_ubsync) == RRSS_SCRATCH_VALID)
4957 vre->vre_offset = 0;
4958 else
4959 vre->vre_offset = RRSS_GET_OFFSET(&spa->spa_ubsync);
4960
4961 /* Reflow the beginning portion using the scratch area */
4962 if (vre->vre_offset == 0) {
4963 VERIFY0(dsl_sync_task(spa_name(spa),
4964 NULL, raidz_reflow_scratch_sync,
4965 vre, 0, ZFS_SPACE_CHECK_NONE));
4966
4967 /* if we encountered errors then pause */
4968 if (vre->vre_offset == 0) {
4969 mutex_enter(&vre->vre_lock);
4970 vre->vre_waiting_for_resilver = B_TRUE;
4971 mutex_exit(&vre->vre_lock);
4972 return;
4973 }
4974 }
4975
4976 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4977 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
4978
4979 uint64_t guid = raidvd->vdev_guid;
4980
4981 /* Iterate over all the remaining metaslabs */
4982 for (uint64_t i = vre->vre_offset >> raidvd->vdev_ms_shift;
4983 i < raidvd->vdev_ms_count &&
4984 !zthr_iscancelled(zthr) &&
4985 vre->vre_failed_offset == UINT64_MAX; i++) {
4986 metaslab_t *msp = raidvd->vdev_ms[i];
4987
4988 metaslab_disable(msp);
4989 mutex_enter(&msp->ms_lock);
4990
4991 /*
4992 * The metaslab may be newly created (for the expanded
4993 * space), in which case its trees won't exist yet,
4994 * so we need to bail out early.
4995 */
4996 if (msp->ms_new) {
4997 mutex_exit(&msp->ms_lock);
4998 metaslab_enable(msp, B_FALSE, B_FALSE);
4999 continue;
5000 }
5001
5002 VERIFY0(metaslab_load(msp));
5003
5004 /*
5005 * We want to copy everything except the free (allocatable)
5006 * space. Note that there may be a little bit more free
5007 * space (e.g. in ms_defer), and it's fine to copy that too.
5008 */
5009 uint64_t shift, start;
5010 zfs_range_seg_type_t type = metaslab_calculate_range_tree_type(
5011 raidvd, msp, &start, &shift);
5012 zfs_range_tree_t *rt = zfs_range_tree_create_flags(
5013 NULL, type, NULL, start, shift, ZFS_RT_F_DYN_NAME,
5014 metaslab_rt_name(msp->ms_group, msp,
5015 "spa_raidz_expand_thread:rt"));
5016 zfs_range_tree_add(rt, msp->ms_start, msp->ms_size);
5017 zfs_range_tree_walk(msp->ms_allocatable, zfs_range_tree_remove,
5018 rt);
5019 mutex_exit(&msp->ms_lock);
5020
5021 /*
5022 * Force the last sector of each metaslab to be copied. This
5023 * ensures that we advance the on-disk progress to the end of
5024 * this metaslab while the metaslab is disabled. Otherwise, we
5025 * could move past this metaslab without advancing the on-disk
5026 * progress, and then an allocation to this metaslab would not
5027 * be copied.
5028 */
5029 int sectorsz = 1 << raidvd->vdev_ashift;
5030 uint64_t ms_last_offset = msp->ms_start +
5031 msp->ms_size - sectorsz;
5032 if (!zfs_range_tree_contains(rt, ms_last_offset, sectorsz)) {
5033 zfs_range_tree_add(rt, ms_last_offset, sectorsz);
5034 }
5035
5036 /*
5037 * When we are resuming from a paused expansion (i.e.
5038 * when importing a pool with a expansion in progress),
5039 * discard any state that we have already processed.
5040 */
5041 if (vre->vre_offset > msp->ms_start) {
5042 zfs_range_tree_clear(rt, msp->ms_start,
5043 vre->vre_offset - msp->ms_start);
5044 }
5045
5046 while (!zthr_iscancelled(zthr) &&
5047 !zfs_range_tree_is_empty(rt) &&
5048 vre->vre_failed_offset == UINT64_MAX) {
5049
5050 /*
5051 * We need to periodically drop the config lock so that
5052 * writers can get in. Additionally, we can't wait
5053 * for a txg to sync while holding a config lock
5054 * (since a waiting writer could cause a 3-way deadlock
5055 * with the sync thread, which also gets a config
5056 * lock for reader). So we can't hold the config lock
5057 * while calling dmu_tx_assign().
5058 */
5059 spa_config_exit(spa, SCL_CONFIG, FTAG);
5060
5061 /*
5062 * If requested, pause the reflow when the amount
5063 * specified by raidz_expand_max_reflow_bytes is reached
5064 *
5065 * This pause is only used during testing or debugging.
5066 */
5067 while (raidz_expand_max_reflow_bytes != 0 &&
5068 raidz_expand_max_reflow_bytes <=
5069 vre->vre_bytes_copied && !zthr_iscancelled(zthr)) {
5070 delay(hz);
5071 }
5072
5073 mutex_enter(&vre->vre_lock);
5074 while (vre->vre_outstanding_bytes >
5075 raidz_expand_max_copy_bytes) {
5076 cv_wait(&vre->vre_cv, &vre->vre_lock);
5077 }
5078 mutex_exit(&vre->vre_lock);
5079
5080 dmu_tx_t *tx =
5081 dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5082
5083 VERIFY0(dmu_tx_assign(tx,
5084 DMU_TX_WAIT | DMU_TX_SUSPEND));
5085 uint64_t txg = dmu_tx_get_txg(tx);
5086
5087 /*
5088 * Reacquire the vdev_config lock. Theoretically, the
5089 * vdev_t that we're expanding may have changed.
5090 */
5091 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5092 raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
5093
5094 boolean_t needsync =
5095 raidz_reflow_impl(raidvd, vre, rt, tx);
5096
5097 dmu_tx_commit(tx);
5098
5099 if (needsync) {
5100 spa_config_exit(spa, SCL_CONFIG, FTAG);
5101 txg_wait_synced(spa->spa_dsl_pool, txg);
5102 spa_config_enter(spa, SCL_CONFIG, FTAG,
5103 RW_READER);
5104 }
5105 }
5106
5107 spa_config_exit(spa, SCL_CONFIG, FTAG);
5108
5109 metaslab_enable(msp, B_FALSE, B_FALSE);
5110 zfs_range_tree_vacate(rt, NULL, NULL);
5111 zfs_range_tree_destroy(rt);
5112
5113 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5114 raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
5115 }
5116
5117 spa_config_exit(spa, SCL_CONFIG, FTAG);
5118
5119 /*
5120 * The txg_wait_synced() here ensures that all reflow zio's have
5121 * completed, and vre_failed_offset has been set if necessary. It
5122 * also ensures that the progress of the last raidz_reflow_sync() is
5123 * written to disk before raidz_reflow_complete_sync() changes the
5124 * in-memory vre_state. vdev_raidz_io_start() uses vre_state to
5125 * determine if a reflow is in progress, in which case we may need to
5126 * write to both old and new locations. Therefore we can only change
5127 * vre_state once this is not necessary, which is once the on-disk
5128 * progress (in spa_ubsync) has been set past any possible writes (to
5129 * the end of the last metaslab).
5130 */
5131 txg_wait_synced(spa->spa_dsl_pool, 0);
5132
5133 if (!zthr_iscancelled(zthr) &&
5134 vre->vre_offset == raidvd->vdev_ms_count << raidvd->vdev_ms_shift) {
5135 /*
5136 * We are not being canceled or paused, so the reflow must be
5137 * complete. In that case also mark it as completed on disk.
5138 */
5139 ASSERT3U(vre->vre_failed_offset, ==, UINT64_MAX);
5140 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
5141 raidz_reflow_complete_sync, spa,
5142 0, ZFS_SPACE_CHECK_NONE));
5143 (void) vdev_online(spa, guid, ZFS_ONLINE_EXPAND, NULL);
5144 } else {
5145 /*
5146 * Wait for all copy zio's to complete and for all the
5147 * raidz_reflow_sync() synctasks to be run.
5148 */
5149 spa_history_log_internal(spa, "reflow pause",
5150 NULL, "offset=%llu failed_offset=%lld",
5151 (long long)vre->vre_offset,
5152 (long long)vre->vre_failed_offset);
5153 mutex_enter(&vre->vre_lock);
5154 if (vre->vre_failed_offset != UINT64_MAX) {
5155 /*
5156 * Reset progress so that we will retry everything
5157 * after the point that something failed.
5158 */
5159 vre->vre_offset = vre->vre_failed_offset;
5160 vre->vre_failed_offset = UINT64_MAX;
5161 vre->vre_waiting_for_resilver = B_TRUE;
5162 }
5163 mutex_exit(&vre->vre_lock);
5164 }
5165 }
5166
5167 void
spa_start_raidz_expansion_thread(spa_t * spa)5168 spa_start_raidz_expansion_thread(spa_t *spa)
5169 {
5170 ASSERT0P(spa->spa_raidz_expand_zthr);
5171 spa->spa_raidz_expand_zthr = zthr_create("raidz_expand",
5172 spa_raidz_expand_thread_check, spa_raidz_expand_thread,
5173 spa, defclsyspri);
5174 }
5175
5176 void
raidz_dtl_reassessed(vdev_t * vd)5177 raidz_dtl_reassessed(vdev_t *vd)
5178 {
5179 spa_t *spa = vd->vdev_spa;
5180 if (spa->spa_raidz_expand != NULL) {
5181 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
5182 /*
5183 * we get called often from vdev_dtl_reassess() so make
5184 * sure it's our vdev and any replacing is complete
5185 */
5186 if (vd->vdev_top->vdev_id == vre->vre_vdev_id &&
5187 !vdev_raidz_expand_child_replacing(vd->vdev_top)) {
5188 mutex_enter(&vre->vre_lock);
5189 if (vre->vre_waiting_for_resilver) {
5190 vdev_dbgmsg(vd, "DTL reassessed, "
5191 "continuing raidz expansion");
5192 vre->vre_waiting_for_resilver = B_FALSE;
5193 zthr_wakeup(spa->spa_raidz_expand_zthr);
5194 }
5195 mutex_exit(&vre->vre_lock);
5196 }
5197 }
5198 }
5199
5200 int
vdev_raidz_attach_check(vdev_t * new_child)5201 vdev_raidz_attach_check(vdev_t *new_child)
5202 {
5203 vdev_t *raidvd = new_child->vdev_parent;
5204 uint64_t new_children = raidvd->vdev_children;
5205
5206 /*
5207 * We use the "boot" space as scratch space to handle overwriting the
5208 * initial part of the vdev. If it is too small, then this expansion
5209 * is not allowed. This would be very unusual (e.g. ashift > 13 and
5210 * >200 children).
5211 */
5212 if (new_children << raidvd->vdev_ashift > VDEV_BOOT_SIZE) {
5213 return (EINVAL);
5214 }
5215 return (0);
5216 }
5217
5218 void
vdev_raidz_attach_sync(void * arg,dmu_tx_t * tx)5219 vdev_raidz_attach_sync(void *arg, dmu_tx_t *tx)
5220 {
5221 vdev_t *new_child = arg;
5222 spa_t *spa = new_child->vdev_spa;
5223 vdev_t *raidvd = new_child->vdev_parent;
5224 vdev_raidz_t *vdrz = raidvd->vdev_tsd;
5225 ASSERT3P(raidvd->vdev_ops, ==, &vdev_raidz_ops);
5226 ASSERT3P(raidvd->vdev_top, ==, raidvd);
5227 ASSERT3U(raidvd->vdev_children, >, vdrz->vd_original_width);
5228 ASSERT3U(raidvd->vdev_children, ==, vdrz->vd_physical_width + 1);
5229 ASSERT3P(raidvd->vdev_child[raidvd->vdev_children - 1], ==,
5230 new_child);
5231
5232 spa_feature_incr(spa, SPA_FEATURE_RAIDZ_EXPANSION, tx);
5233
5234 vdrz->vd_physical_width++;
5235
5236 VERIFY0(spa->spa_uberblock.ub_raidz_reflow_info);
5237 vdrz->vn_vre.vre_vdev_id = raidvd->vdev_id;
5238 vdrz->vn_vre.vre_offset = 0;
5239 vdrz->vn_vre.vre_failed_offset = UINT64_MAX;
5240 spa->spa_raidz_expand = &vdrz->vn_vre;
5241
5242 /*
5243 * Dirty the config so that ZPOOL_CONFIG_RAIDZ_EXPANDING will get
5244 * written to the config.
5245 */
5246 vdev_config_dirty(raidvd);
5247
5248 vdrz->vn_vre.vre_start_time = gethrestime_sec();
5249 vdrz->vn_vre.vre_end_time = 0;
5250 vdrz->vn_vre.vre_state = DSS_SCANNING;
5251 vdrz->vn_vre.vre_bytes_copied = 0;
5252
5253 uint64_t state = vdrz->vn_vre.vre_state;
5254 VERIFY0(zap_update(spa->spa_meta_objset,
5255 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE,
5256 sizeof (state), 1, &state, tx));
5257
5258 uint64_t start_time = vdrz->vn_vre.vre_start_time;
5259 VERIFY0(zap_update(spa->spa_meta_objset,
5260 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_START_TIME,
5261 sizeof (start_time), 1, &start_time, tx));
5262
5263 (void) zap_remove(spa->spa_meta_objset,
5264 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME, tx);
5265 (void) zap_remove(spa->spa_meta_objset,
5266 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED, tx);
5267
5268 spa_history_log_internal(spa, "raidz vdev expansion started", tx,
5269 "%s vdev %llu new width %llu", spa_name(spa),
5270 (unsigned long long)raidvd->vdev_id,
5271 (unsigned long long)raidvd->vdev_children);
5272 }
5273
5274 int
vdev_raidz_load(vdev_t * vd)5275 vdev_raidz_load(vdev_t *vd)
5276 {
5277 vdev_raidz_t *vdrz = vd->vdev_tsd;
5278 int err;
5279
5280 uint64_t state = DSS_NONE;
5281 uint64_t start_time = 0;
5282 uint64_t end_time = 0;
5283 uint64_t bytes_copied = 0;
5284
5285 if (vd->vdev_top_zap != 0) {
5286 err = zap_lookup(vd->vdev_spa->spa_meta_objset,
5287 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE,
5288 sizeof (state), 1, &state);
5289 if (err != 0 && err != ENOENT)
5290 return (err);
5291
5292 err = zap_lookup(vd->vdev_spa->spa_meta_objset,
5293 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_START_TIME,
5294 sizeof (start_time), 1, &start_time);
5295 if (err != 0 && err != ENOENT)
5296 return (err);
5297
5298 err = zap_lookup(vd->vdev_spa->spa_meta_objset,
5299 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME,
5300 sizeof (end_time), 1, &end_time);
5301 if (err != 0 && err != ENOENT)
5302 return (err);
5303
5304 err = zap_lookup(vd->vdev_spa->spa_meta_objset,
5305 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED,
5306 sizeof (bytes_copied), 1, &bytes_copied);
5307 if (err != 0 && err != ENOENT)
5308 return (err);
5309 }
5310
5311 /*
5312 * If we are in the middle of expansion, vre_state should have
5313 * already been set by vdev_raidz_init().
5314 */
5315 EQUIV(vdrz->vn_vre.vre_state == DSS_SCANNING, state == DSS_SCANNING);
5316 vdrz->vn_vre.vre_state = (dsl_scan_state_t)state;
5317 vdrz->vn_vre.vre_start_time = start_time;
5318 vdrz->vn_vre.vre_end_time = end_time;
5319 vdrz->vn_vre.vre_bytes_copied = bytes_copied;
5320
5321 return (0);
5322 }
5323
5324 int
spa_raidz_expand_get_stats(spa_t * spa,pool_raidz_expand_stat_t * pres)5325 spa_raidz_expand_get_stats(spa_t *spa, pool_raidz_expand_stat_t *pres)
5326 {
5327 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
5328
5329 if (vre == NULL) {
5330 /* no removal in progress; find most recent completed */
5331 for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) {
5332 vdev_t *vd = spa->spa_root_vdev->vdev_child[c];
5333 if (vd->vdev_ops == &vdev_raidz_ops) {
5334 vdev_raidz_t *vdrz = vd->vdev_tsd;
5335
5336 if (vdrz->vn_vre.vre_end_time != 0 &&
5337 (vre == NULL ||
5338 vdrz->vn_vre.vre_end_time >
5339 vre->vre_end_time)) {
5340 vre = &vdrz->vn_vre;
5341 }
5342 }
5343 }
5344 }
5345
5346 if (vre == NULL) {
5347 return (SET_ERROR(ENOENT));
5348 }
5349
5350 pres->pres_state = vre->vre_state;
5351 pres->pres_expanding_vdev = vre->vre_vdev_id;
5352
5353 vdev_t *vd = vdev_lookup_top(spa, vre->vre_vdev_id);
5354 pres->pres_to_reflow = vd->vdev_stat.vs_alloc;
5355
5356 mutex_enter(&vre->vre_lock);
5357 pres->pres_reflowed = vre->vre_bytes_copied;
5358 for (int i = 0; i < TXG_SIZE; i++)
5359 pres->pres_reflowed += vre->vre_bytes_copied_pertxg[i];
5360 mutex_exit(&vre->vre_lock);
5361
5362 pres->pres_start_time = vre->vre_start_time;
5363 pres->pres_end_time = vre->vre_end_time;
5364 pres->pres_waiting_for_resilver = vre->vre_waiting_for_resilver;
5365
5366 return (0);
5367 }
5368
5369 /*
5370 * Initialize private RAIDZ specific fields from the nvlist.
5371 */
5372 static int
vdev_raidz_init(spa_t * spa,nvlist_t * nv,void ** tsd)5373 vdev_raidz_init(spa_t *spa, nvlist_t *nv, void **tsd)
5374 {
5375 uint_t children;
5376 nvlist_t **child;
5377 int error = nvlist_lookup_nvlist_array(nv,
5378 ZPOOL_CONFIG_CHILDREN, &child, &children);
5379 if (error != 0)
5380 return (SET_ERROR(EINVAL));
5381
5382 uint64_t nparity;
5383 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, &nparity) == 0) {
5384 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
5385 return (SET_ERROR(EINVAL));
5386
5387 /*
5388 * Previous versions could only support 1 or 2 parity
5389 * device.
5390 */
5391 if (nparity > 1 && spa_version(spa) < SPA_VERSION_RAIDZ2)
5392 return (SET_ERROR(EINVAL));
5393 else if (nparity > 2 && spa_version(spa) < SPA_VERSION_RAIDZ3)
5394 return (SET_ERROR(EINVAL));
5395 } else {
5396 /*
5397 * We require the parity to be specified for SPAs that
5398 * support multiple parity levels.
5399 */
5400 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
5401 return (SET_ERROR(EINVAL));
5402
5403 /*
5404 * Otherwise, we default to 1 parity device for RAID-Z.
5405 */
5406 nparity = 1;
5407 }
5408
5409 vdev_raidz_t *vdrz = kmem_zalloc(sizeof (*vdrz), KM_SLEEP);
5410 vdrz->vn_vre.vre_vdev_id = -1;
5411 vdrz->vn_vre.vre_offset = UINT64_MAX;
5412 vdrz->vn_vre.vre_failed_offset = UINT64_MAX;
5413 mutex_init(&vdrz->vn_vre.vre_lock, NULL, MUTEX_DEFAULT, NULL);
5414 cv_init(&vdrz->vn_vre.vre_cv, NULL, CV_DEFAULT, NULL);
5415 zfs_rangelock_init(&vdrz->vn_vre.vre_rangelock, NULL, NULL);
5416 mutex_init(&vdrz->vd_expand_lock, NULL, MUTEX_DEFAULT, NULL);
5417 avl_create(&vdrz->vd_expand_txgs, vdev_raidz_reflow_compare,
5418 sizeof (reflow_node_t), offsetof(reflow_node_t, re_link));
5419
5420 vdrz->vd_physical_width = children;
5421 vdrz->vd_nparity = nparity;
5422
5423 /* note, the ID does not exist when creating a pool */
5424 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
5425 &vdrz->vn_vre.vre_vdev_id);
5426
5427 boolean_t reflow_in_progress =
5428 nvlist_exists(nv, ZPOOL_CONFIG_RAIDZ_EXPANDING);
5429 if (reflow_in_progress) {
5430 spa->spa_raidz_expand = &vdrz->vn_vre;
5431 vdrz->vn_vre.vre_state = DSS_SCANNING;
5432 }
5433
5434 vdrz->vd_original_width = children;
5435 uint64_t *txgs;
5436 unsigned int txgs_size = 0;
5437 error = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_RAIDZ_EXPAND_TXGS,
5438 &txgs, &txgs_size);
5439 if (error == 0) {
5440 for (int i = 0; i < txgs_size; i++) {
5441 reflow_node_t *re = kmem_zalloc(sizeof (*re), KM_SLEEP);
5442 re->re_txg = txgs[txgs_size - i - 1];
5443 re->re_logical_width = vdrz->vd_physical_width - i;
5444
5445 if (reflow_in_progress)
5446 re->re_logical_width--;
5447
5448 avl_add(&vdrz->vd_expand_txgs, re);
5449 }
5450
5451 vdrz->vd_original_width = vdrz->vd_physical_width - txgs_size;
5452 }
5453 if (reflow_in_progress) {
5454 vdrz->vd_original_width--;
5455 zfs_dbgmsg("reflow_in_progress, %u wide, %d prior expansions",
5456 children, txgs_size);
5457 }
5458
5459 *tsd = vdrz;
5460
5461 return (0);
5462 }
5463
5464 static void
vdev_raidz_fini(vdev_t * vd)5465 vdev_raidz_fini(vdev_t *vd)
5466 {
5467 vdev_raidz_t *vdrz = vd->vdev_tsd;
5468 if (vd->vdev_spa->spa_raidz_expand == &vdrz->vn_vre)
5469 vd->vdev_spa->spa_raidz_expand = NULL;
5470 reflow_node_t *re;
5471 void *cookie = NULL;
5472 avl_tree_t *tree = &vdrz->vd_expand_txgs;
5473 while ((re = avl_destroy_nodes(tree, &cookie)) != NULL)
5474 kmem_free(re, sizeof (*re));
5475 avl_destroy(&vdrz->vd_expand_txgs);
5476 mutex_destroy(&vdrz->vd_expand_lock);
5477 mutex_destroy(&vdrz->vn_vre.vre_lock);
5478 cv_destroy(&vdrz->vn_vre.vre_cv);
5479 zfs_rangelock_fini(&vdrz->vn_vre.vre_rangelock);
5480 kmem_free(vdrz, sizeof (*vdrz));
5481 }
5482
5483 /*
5484 * Add RAIDZ specific fields to the config nvlist.
5485 */
5486 static void
vdev_raidz_config_generate(vdev_t * vd,nvlist_t * nv)5487 vdev_raidz_config_generate(vdev_t *vd, nvlist_t *nv)
5488 {
5489 ASSERT3P(vd->vdev_ops, ==, &vdev_raidz_ops);
5490 vdev_raidz_t *vdrz = vd->vdev_tsd;
5491
5492 /*
5493 * Make sure someone hasn't managed to sneak a fancy new vdev
5494 * into a crufty old storage pool.
5495 */
5496 ASSERT(vdrz->vd_nparity == 1 ||
5497 (vdrz->vd_nparity <= 2 &&
5498 spa_version(vd->vdev_spa) >= SPA_VERSION_RAIDZ2) ||
5499 (vdrz->vd_nparity <= 3 &&
5500 spa_version(vd->vdev_spa) >= SPA_VERSION_RAIDZ3));
5501
5502 /*
5503 * Note that we'll add these even on storage pools where they
5504 * aren't strictly required -- older software will just ignore
5505 * it.
5506 */
5507 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vdrz->vd_nparity);
5508
5509 if (vdrz->vn_vre.vre_state == DSS_SCANNING) {
5510 fnvlist_add_boolean(nv, ZPOOL_CONFIG_RAIDZ_EXPANDING);
5511 }
5512
5513 mutex_enter(&vdrz->vd_expand_lock);
5514 if (!avl_is_empty(&vdrz->vd_expand_txgs)) {
5515 uint64_t count = avl_numnodes(&vdrz->vd_expand_txgs);
5516 uint64_t *txgs = kmem_alloc(sizeof (uint64_t) * count,
5517 KM_SLEEP);
5518 uint64_t i = 0;
5519
5520 for (reflow_node_t *re = avl_first(&vdrz->vd_expand_txgs);
5521 re != NULL; re = AVL_NEXT(&vdrz->vd_expand_txgs, re)) {
5522 txgs[i++] = re->re_txg;
5523 }
5524
5525 fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_RAIDZ_EXPAND_TXGS,
5526 txgs, count);
5527
5528 kmem_free(txgs, sizeof (uint64_t) * count);
5529 }
5530 mutex_exit(&vdrz->vd_expand_lock);
5531 }
5532
5533 static uint64_t
vdev_raidz_nparity(vdev_t * vd)5534 vdev_raidz_nparity(vdev_t *vd)
5535 {
5536 vdev_raidz_t *vdrz = vd->vdev_tsd;
5537 return (vdrz->vd_nparity);
5538 }
5539
5540 static uint64_t
vdev_raidz_ndisks(vdev_t * vd)5541 vdev_raidz_ndisks(vdev_t *vd)
5542 {
5543 return (vd->vdev_children);
5544 }
5545
5546 vdev_ops_t vdev_raidz_ops = {
5547 .vdev_op_init = vdev_raidz_init,
5548 .vdev_op_fini = vdev_raidz_fini,
5549 .vdev_op_open = vdev_raidz_open,
5550 .vdev_op_close = vdev_raidz_close,
5551 .vdev_op_psize_to_asize = vdev_raidz_psize_to_asize,
5552 .vdev_op_asize_to_psize = vdev_raidz_asize_to_psize,
5553 .vdev_op_min_asize = vdev_raidz_min_asize,
5554 .vdev_op_min_alloc = NULL,
5555 .vdev_op_io_start = vdev_raidz_io_start,
5556 .vdev_op_io_done = vdev_raidz_io_done,
5557 .vdev_op_state_change = vdev_raidz_state_change,
5558 .vdev_op_need_resilver = vdev_raidz_need_resilver,
5559 .vdev_op_hold = NULL,
5560 .vdev_op_rele = NULL,
5561 .vdev_op_remap = NULL,
5562 .vdev_op_xlate = vdev_raidz_xlate,
5563 .vdev_op_rebuild_asize = NULL,
5564 .vdev_op_metaslab_init = NULL,
5565 .vdev_op_config_generate = vdev_raidz_config_generate,
5566 .vdev_op_nparity = vdev_raidz_nparity,
5567 .vdev_op_ndisks = vdev_raidz_ndisks,
5568 .vdev_op_type = VDEV_TYPE_RAIDZ, /* name of this vdev type */
5569 .vdev_op_leaf = B_FALSE /* not a leaf vdev */
5570 };
5571
5572 ZFS_MODULE_PARAM(zfs_vdev, raidz_, expand_max_reflow_bytes, ULONG, ZMOD_RW,
5573 "For testing, pause RAIDZ expansion after reflowing this many bytes");
5574 ZFS_MODULE_PARAM(zfs_vdev, raidz_, expand_max_copy_bytes, ULONG, ZMOD_RW,
5575 "Max amount of concurrent i/o for RAIDZ expansion");
5576 ZFS_MODULE_PARAM(zfs_vdev, raidz_, io_aggregate_rows, ULONG, ZMOD_RW,
5577 "For expanded RAIDZ, aggregate reads that have more rows than this");
5578 ZFS_MODULE_PARAM(zfs, zfs_, scrub_after_expand, INT, ZMOD_RW,
5579 "For expanded RAIDZ, automatically start a pool scrub when expansion "
5580 "completes");
5581 ZFS_MODULE_PARAM(zfs, zfs_, scrub_partial_writes, INT, ZMOD_RW,
5582 "Issue reads after writes with recoverable failures to ensure "
5583 "integrity");
5584 ZFS_MODULE_PARAM(zfs_vdev, vdev_, read_sit_out_secs, ULONG, ZMOD_RW,
5585 "Raidz/draid slow disk sit out time period in seconds");
5586 ZFS_MODULE_PARAM(zfs_vdev, vdev_, raidz_outlier_check_interval_ms, U64,
5587 ZMOD_RW, "Interval to check for slow raidz/draid children");
5588 ZFS_MODULE_PARAM(zfs_vdev, vdev_, raidz_outlier_insensitivity, UINT,
5589 ZMOD_RW, "How insensitive the slow raidz/draid child check should be");
5590 /* END CSTYLED */
5591