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 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 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 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 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 * 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 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 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 * 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 * 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 zio_batch_create(zio); 2757 2758 if (zio->io_type == ZIO_TYPE_WRITE) { 2759 for (int i = 0; i < rm->rm_nrows; i++) { 2760 vdev_raidz_io_start_write(zio, rm->rm_row[i]); 2761 } 2762 2763 if (logical_width == vdrz->vd_physical_width) { 2764 raidz_start_skip_writes(zio); 2765 } 2766 } else { 2767 ASSERT(zio->io_type == ZIO_TYPE_READ); 2768 vdev_raidz_io_start_read(zio, rm); 2769 } 2770 2771 zio_execute(zio_batch_rele(zio)); 2772 } 2773 2774 /* 2775 * Report a checksum error for a child of a RAID-Z device. 2776 */ 2777 void 2778 vdev_raidz_checksum_error(zio_t *zio, raidz_col_t *rc, abd_t *bad_data) 2779 { 2780 vdev_t *vd = zio->io_vd->vdev_child[rc->rc_devidx]; 2781 2782 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE) && 2783 zio->io_priority != ZIO_PRIORITY_REBUILD) { 2784 zio_bad_cksum_t zbc; 2785 raidz_map_t *rm = zio->io_vsd; 2786 2787 zbc.zbc_has_cksum = 0; 2788 zbc.zbc_injected = rm->rm_ecksuminjected; 2789 2790 mutex_enter(&vd->vdev_stat_lock); 2791 vd->vdev_stat.vs_checksum_errors++; 2792 mutex_exit(&vd->vdev_stat_lock); 2793 (void) zfs_ereport_post_checksum(zio->io_spa, vd, 2794 &zio->io_bookmark, zio, rc->rc_offset, rc->rc_size, 2795 rc->rc_abd, bad_data, &zbc); 2796 } 2797 } 2798 2799 /* 2800 * We keep track of whether or not there were any injected errors, so that 2801 * any ereports we generate can note it. 2802 */ 2803 static int 2804 raidz_checksum_verify(zio_t *zio) 2805 { 2806 zio_bad_cksum_t zbc = {0}; 2807 raidz_map_t *rm = zio->io_vsd; 2808 2809 int ret = zio_checksum_error(zio, &zbc); 2810 /* 2811 * Any Direct I/O read that has a checksum error must be treated as 2812 * suspicious as the contents of the buffer could be getting 2813 * manipulated while the I/O is taking place. The checksum verify error 2814 * will be reported to the top-level RAIDZ VDEV. 2815 */ 2816 if (zio->io_flags & ZIO_FLAG_DIO_READ && ret == ECKSUM) { 2817 zio->io_error = ret; 2818 zio->io_post |= ZIO_POST_DIO_CHKSUM_ERR; 2819 zio_dio_chksum_verify_error_report(zio); 2820 zio_checksum_verified(zio); 2821 return (0); 2822 } 2823 2824 if (ret != 0 && zbc.zbc_injected != 0) 2825 rm->rm_ecksuminjected = 1; 2826 2827 return (ret); 2828 } 2829 2830 /* 2831 * Generate the parity from the data columns. If we tried and were able to 2832 * read the parity without error, verify that the generated parity matches the 2833 * data we read. If it doesn't, we fire off a checksum error. Return the 2834 * number of such failures. 2835 */ 2836 static int 2837 raidz_parity_verify(zio_t *zio, raidz_row_t *rr) 2838 { 2839 abd_t *orig[VDEV_RAIDZ_MAXPARITY]; 2840 int c, ret = 0; 2841 raidz_map_t *rm = zio->io_vsd; 2842 raidz_col_t *rc; 2843 2844 blkptr_t *bp = zio->io_bp; 2845 enum zio_checksum checksum = (bp == NULL ? zio->io_prop.zp_checksum : 2846 (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp))); 2847 2848 if (checksum == ZIO_CHECKSUM_NOPARITY) 2849 return (ret); 2850 2851 for (c = 0; c < rr->rr_firstdatacol; c++) { 2852 rc = &rr->rr_col[c]; 2853 if (!rc->rc_tried || rc->rc_error != 0) 2854 continue; 2855 2856 orig[c] = rc->rc_abd; 2857 ASSERT3U(abd_get_size(rc->rc_abd), ==, rc->rc_size); 2858 rc->rc_abd = abd_alloc_linear(rc->rc_size, B_FALSE); 2859 } 2860 2861 /* 2862 * Verify any empty sectors are zero filled to ensure the parity 2863 * is calculated correctly even if these non-data sectors are damaged. 2864 */ 2865 if (rr->rr_nempty && rr->rr_abd_empty != NULL) 2866 ret += vdev_draid_map_verify_empty(zio, rr); 2867 2868 /* 2869 * Regenerates parity even for !tried||rc_error!=0 columns. This 2870 * isn't harmful but it does have the side effect of fixing stuff 2871 * we didn't realize was necessary (i.e. even if we return 0). 2872 */ 2873 vdev_raidz_generate_parity_row(rm, rr); 2874 2875 for (c = 0; c < rr->rr_firstdatacol; c++) { 2876 rc = &rr->rr_col[c]; 2877 2878 if (!rc->rc_tried || rc->rc_error != 0) 2879 continue; 2880 2881 if (abd_cmp(orig[c], rc->rc_abd) != 0) { 2882 vdev_raidz_checksum_error(zio, rc, orig[c]); 2883 rc->rc_error = SET_ERROR(ECKSUM); 2884 ret++; 2885 } 2886 abd_free(orig[c]); 2887 } 2888 2889 return (ret); 2890 } 2891 2892 static int 2893 vdev_raidz_worst_error(raidz_row_t *rr) 2894 { 2895 int error = 0; 2896 2897 for (int c = 0; c < rr->rr_cols; c++) { 2898 error = zio_worst_error(error, rr->rr_col[c].rc_error); 2899 error = zio_worst_error(error, rr->rr_col[c].rc_shadow_error); 2900 } 2901 2902 return (error); 2903 } 2904 2905 /* 2906 * Find the median value from a set of n values 2907 */ 2908 static uint64_t 2909 latency_median_value(const uint64_t *data, size_t n) 2910 { 2911 uint64_t m; 2912 2913 if (n % 2 == 0) 2914 m = (data[(n >> 1) - 1] + data[n >> 1]) >> 1; 2915 else 2916 m = data[((n + 1) >> 1) - 1]; 2917 2918 return (m); 2919 } 2920 2921 /* 2922 * Calculate the outlier fence from a set of n latency values 2923 * 2924 * fence = Q3 + vdev_raidz_outlier_insensitivity x (Q3 - Q1) 2925 */ 2926 static uint64_t 2927 latency_quartiles_fence(const uint64_t *data, size_t n, uint64_t *iqr) 2928 { 2929 uint64_t q1 = latency_median_value(&data[0], n >> 1); 2930 uint64_t q3 = latency_median_value(&data[(n + 1) >> 1], n >> 1); 2931 2932 /* 2933 * To avoid detecting false positive outliers when N is small and 2934 * and the latencies values are very close, make sure the IQR 2935 * is at least 25% larger than Q1. 2936 */ 2937 *iqr = MAX(q3 - q1, q1 / 4); 2938 2939 return (q3 + (*iqr * vdev_raidz_outlier_insensitivity)); 2940 } 2941 #define LAT_CHILDREN_MIN 5 2942 #define LAT_OUTLIER_LIMIT 20 2943 2944 static int 2945 latency_compare(const void *arg1, const void *arg2) 2946 { 2947 const uint64_t *l1 = (uint64_t *)arg1; 2948 const uint64_t *l2 = (uint64_t *)arg2; 2949 2950 return (TREE_CMP(*l1, *l2)); 2951 } 2952 2953 void 2954 vdev_raidz_sit_child(vdev_t *svd, uint64_t secs) 2955 { 2956 for (int c = 0; c < svd->vdev_children; c++) 2957 vdev_raidz_sit_child(svd->vdev_child[c], secs); 2958 2959 if (!svd->vdev_ops->vdev_op_leaf) 2960 return; 2961 2962 /* Begin a sit out period for this slow drive */ 2963 svd->vdev_read_sit_out_expire = gethrestime_sec() + 2964 secs; 2965 2966 /* Count each slow io period */ 2967 mutex_enter(&svd->vdev_stat_lock); 2968 svd->vdev_stat.vs_slow_ios++; 2969 mutex_exit(&svd->vdev_stat_lock); 2970 } 2971 2972 void 2973 vdev_raidz_unsit_child(vdev_t *vd) 2974 { 2975 for (int c = 0; c < vd->vdev_children; c++) 2976 vdev_raidz_unsit_child(vd->vdev_child[c]); 2977 2978 if (!vd->vdev_ops->vdev_op_leaf) 2979 return; 2980 2981 vd->vdev_read_sit_out_expire = 0; 2982 } 2983 2984 /* 2985 * Check for any latency outlier from latest set of child reads. 2986 * 2987 * Uses a Tukey's fence, with K = 50, for detecting extreme outliers. This 2988 * rule defines extreme outliers as data points outside the fence of the 2989 * third quartile plus fifty times the Interquartile Range (IQR). This range 2990 * is the distance between the first and third quartile. 2991 * 2992 * Fifty is an extremely large value for Tukey's fence, but the outliers we're 2993 * attempting to detect here are orders of magnitude times larger than the 2994 * median. This large value should capture any truly fault disk quickly, 2995 * without causing spurious sit-outs. 2996 * 2997 * To further avoid spurious sit-outs, vdevs must be detected multiple times 2998 * as an outlier before they are sat, and outlier counts will gradually decay. 2999 * Every nchildren times we have detected an outlier, we subtract 2 from the 3000 * outlier count of all children. If detected outliers are close to uniformly 3001 * distributed, this will result in the outlier count remaining close to 0 3002 * (in expectation; over long enough time-scales, spurious sit-outs are still 3003 * possible). 3004 */ 3005 static void 3006 vdev_child_slow_outlier(zio_t *zio) 3007 { 3008 vdev_t *vd = zio->io_vd; 3009 if (!vd->vdev_autosit || vdev_read_sit_out_secs == 0 || 3010 vd->vdev_children < LAT_CHILDREN_MIN) 3011 return; 3012 3013 hrtime_t now = getlrtime(); 3014 uint64_t last = atomic_load_64(&vd->vdev_last_latency_check); 3015 3016 if ((now - last) < MSEC2NSEC(vdev_raidz_outlier_check_interval_ms)) 3017 return; 3018 3019 /* Allow a single winner when there are racing callers. */ 3020 if (atomic_cas_64(&vd->vdev_last_latency_check, last, now) != last) 3021 return; 3022 3023 int children = vd->vdev_children; 3024 uint64_t *lat_data = kmem_alloc(sizeof (uint64_t) * children, KM_SLEEP); 3025 3026 for (int c = 0; c < children; c++) { 3027 vdev_t *cvd = vd->vdev_child[c]; 3028 if (cvd->vdev_prev_histo == NULL) { 3029 mutex_enter(&cvd->vdev_stat_lock); 3030 size_t size = 3031 sizeof (cvd->vdev_stat_ex.vsx_disk_histo[0]); 3032 cvd->vdev_prev_histo = kmem_zalloc(size, KM_SLEEP); 3033 memcpy(cvd->vdev_prev_histo, 3034 cvd->vdev_stat_ex.vsx_disk_histo[ZIO_TYPE_READ], 3035 size); 3036 mutex_exit(&cvd->vdev_stat_lock); 3037 } 3038 } 3039 uint64_t max = 0; 3040 vdev_t *svd = NULL; 3041 uint_t sitouts = 0; 3042 boolean_t skip = B_FALSE, svd_sitting = B_FALSE; 3043 for (int c = 0; c < children; c++) { 3044 vdev_t *cvd = vd->vdev_child[c]; 3045 boolean_t sitting = vdev_sit_out_reads(cvd, 0) || 3046 cvd->vdev_state != VDEV_STATE_HEALTHY; 3047 3048 /* We can't sit out more disks than we have parity */ 3049 if (sitting && ++sitouts >= vdev_get_nparity(vd)) 3050 skip = B_TRUE; 3051 3052 mutex_enter(&cvd->vdev_stat_lock); 3053 3054 uint64_t *prev_histo = cvd->vdev_prev_histo; 3055 uint64_t *histo = 3056 cvd->vdev_stat_ex.vsx_disk_histo[ZIO_TYPE_READ]; 3057 if (skip) { 3058 size_t size = 3059 sizeof (cvd->vdev_stat_ex.vsx_disk_histo[0]); 3060 memcpy(prev_histo, histo, size); 3061 mutex_exit(&cvd->vdev_stat_lock); 3062 continue; 3063 } 3064 uint64_t count = 0; 3065 lat_data[c] = 0; 3066 for (int i = 0; i < VDEV_L_HISTO_BUCKETS; i++) { 3067 uint64_t this_count = histo[i] - prev_histo[i]; 3068 lat_data[c] += (1ULL << i) * this_count; 3069 count += this_count; 3070 } 3071 size_t size = sizeof (cvd->vdev_stat_ex.vsx_disk_histo[0]); 3072 memcpy(prev_histo, histo, size); 3073 mutex_exit(&cvd->vdev_stat_lock); 3074 lat_data[c] /= MAX(1, count); 3075 3076 /* Wait until all disks have been read from */ 3077 if (lat_data[c] == 0 && !sitting) { 3078 skip = B_TRUE; 3079 continue; 3080 } 3081 3082 /* Keep track of the vdev with largest value */ 3083 if (lat_data[c] > max) { 3084 max = lat_data[c]; 3085 svd = cvd; 3086 svd_sitting = sitting; 3087 } 3088 } 3089 3090 if (skip) { 3091 kmem_free(lat_data, sizeof (uint64_t) * children); 3092 return; 3093 } 3094 3095 qsort((void *)lat_data, children, sizeof (uint64_t), latency_compare); 3096 3097 uint64_t iqr; 3098 uint64_t fence = latency_quartiles_fence(lat_data, children, &iqr); 3099 3100 ASSERT3U(lat_data[children - 1], ==, max); 3101 if (max > fence && !svd_sitting) { 3102 ASSERT3U(iqr, >, 0); 3103 uint64_t incr = MAX(1, MIN((max - fence) / iqr, 3104 LAT_OUTLIER_LIMIT / 4)); 3105 vd->vdev_outlier_count += incr; 3106 if (vd->vdev_outlier_count >= children) { 3107 for (int c = 0; c < children; c++) { 3108 vdev_t *cvd = vd->vdev_child[c]; 3109 cvd->vdev_outlier_count -= 2; 3110 cvd->vdev_outlier_count = MAX(0, 3111 cvd->vdev_outlier_count); 3112 } 3113 vd->vdev_outlier_count = 0; 3114 } 3115 /* 3116 * Keep track of how many times this child has had 3117 * an outlier read. A disk that persitently has a 3118 * higher than peers outlier count will be considered 3119 * a slow disk. 3120 */ 3121 svd->vdev_outlier_count += incr; 3122 if (svd->vdev_outlier_count > LAT_OUTLIER_LIMIT) { 3123 ASSERT0(svd->vdev_read_sit_out_expire); 3124 vdev_raidz_sit_child(svd, vdev_read_sit_out_secs); 3125 (void) zfs_ereport_post(FM_EREPORT_ZFS_SITOUT, 3126 zio->io_spa, svd, NULL, NULL, 0); 3127 vdev_dbgmsg(svd, "begin read sit out for %d secs", 3128 (int)vdev_read_sit_out_secs); 3129 3130 for (int c = 0; c < vd->vdev_children; c++) 3131 vd->vdev_child[c]->vdev_outlier_count = 0; 3132 } 3133 } 3134 3135 kmem_free(lat_data, sizeof (uint64_t) * children); 3136 } 3137 3138 static void 3139 vdev_raidz_io_done_verified(zio_t *zio, raidz_row_t *rr) 3140 { 3141 int unexpected_errors = 0; 3142 int parity_errors = 0; 3143 int parity_untried = 0; 3144 int data_errors = 0; 3145 zio_flag_t add_flags = 0; 3146 3147 ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ); 3148 ASSERT0(zio->io_error); 3149 3150 for (int c = 0; c < rr->rr_cols; c++) { 3151 raidz_col_t *rc = &rr->rr_col[c]; 3152 3153 if (rc->rc_error) { 3154 if (c < rr->rr_firstdatacol) 3155 parity_errors++; 3156 else 3157 data_errors++; 3158 3159 if (!rc->rc_skipped) 3160 unexpected_errors++; 3161 } else if (c < rr->rr_firstdatacol && !rc->rc_tried) { 3162 parity_untried++; 3163 } 3164 3165 if (rc->rc_force_repair) 3166 unexpected_errors++; 3167 } 3168 3169 /* 3170 * If we read more parity disks than were used for 3171 * reconstruction, confirm that the other parity disks produced 3172 * correct data. 3173 * 3174 * We also regenerate parity to write it back to any failed parity 3175 * columns. However, if all available parity was consumed by 3176 * reconstruction (parity_verify is false), regenerating parity is 3177 * a mathematical identity -- the result is guaranteed to equal the 3178 * input that was used for reconstruction, whether correct or 3179 * corrupted. In that case the only reason to regenerate is to 3180 * write back a failed parity column, so skip regeneration when no 3181 * parity column failed or the pool is read-only. 3182 */ 3183 boolean_t parity_verify = (parity_errors + parity_untried) < 3184 (rr->rr_firstdatacol - data_errors); 3185 if (parity_verify || (parity_errors > 0 && 3186 spa_writeable(zio->io_spa))) { 3187 int n = raidz_parity_verify(zio, rr); 3188 /* 3189 * In, Reed-Solomon encoding, if we have ndata+1 columns and 3190 * the parity doesn't match, it means the data integrity is 3191 * compromised. We shouldn't try to repair anything in this 3192 * case. 3193 */ 3194 if (parity_verify && n > 0 && 3195 zio->io_priority == ZIO_PRIORITY_REBUILD) 3196 return; 3197 /* 3198 * If we have only ndata columns, the data integrity will 3199 * be checked by the checksums normally, but not in case 3200 * of rebuild when we don't have checksums. In this case, 3201 * we add ZIO_FLAG_SPECULATIVE and try to not spread 3202 * unverified data. For example, when the target vdev happens 3203 * to be the mirroring spare vdev, we would repair only that 3204 * child in it which is being rebuilt. 3205 */ 3206 if (!parity_verify && zio->io_priority == ZIO_PRIORITY_REBUILD) 3207 add_flags |= ZIO_FLAG_SPECULATIVE; 3208 unexpected_errors += n; 3209 } 3210 3211 if (spa_writeable(zio->io_spa) && 3212 (unexpected_errors > 0 || (zio->io_flags & ZIO_FLAG_RESILVER))) { 3213 /* 3214 * Use the good data we have in hand to repair damaged children. 3215 */ 3216 for (int c = 0; c < rr->rr_cols; c++) { 3217 raidz_col_t *rc = &rr->rr_col[c]; 3218 vdev_t *vd = zio->io_vd; 3219 vdev_t *cvd = vd->vdev_child[rc->rc_devidx]; 3220 3221 if (!rc->rc_allow_repair) { 3222 continue; 3223 } else if (!rc->rc_force_repair && 3224 (rc->rc_error == 0 || rc->rc_size == 0)) { 3225 continue; 3226 } 3227 /* 3228 * We do not allow self healing for Direct I/O reads. 3229 * See comment in vdev_raid_row_alloc(). 3230 */ 3231 ASSERT0(zio->io_flags & ZIO_FLAG_DIO_READ); 3232 3233 /* 3234 * When the target vdev is draid spare, we should clear 3235 * ZIO_FLAG_SPECULATIVE. First, if that draid spare maps 3236 * to another spare having an online/degraded disk, that 3237 * disk must be repaired also. Otherwise, the scrub will 3238 * detect a lot of cksum errors later. Second, since it 3239 * is draid spare, there is no harm in updating its 3240 * content on any vdev it maps to because the space is 3241 * reserved as a spare anyway. 3242 */ 3243 zio_flag_t aflags = add_flags; 3244 if (rc->rc_tgt_is_dspare) 3245 aflags &= ~ZIO_FLAG_SPECULATIVE; 3246 3247 zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 3248 rc->rc_offset, rc->rc_abd, rc->rc_size, 3249 ZIO_TYPE_WRITE, 3250 zio->io_priority == ZIO_PRIORITY_REBUILD ? 3251 ZIO_PRIORITY_REBUILD : ZIO_PRIORITY_ASYNC_WRITE, 3252 ZIO_FLAG_IO_REPAIR | (unexpected_errors ? 3253 ZIO_FLAG_SELF_HEAL : 0) | aflags, NULL, NULL)); 3254 } 3255 } 3256 3257 /* 3258 * Scrub or resilver i/o's: overwrite any shadow locations with the 3259 * good data. This ensures that if we've already copied this sector, 3260 * it will be corrected if it was damaged. This writes more than is 3261 * necessary, but since expansion is paused during scrub/resilver, at 3262 * most a single row will have a shadow location. 3263 */ 3264 if (spa_writeable(zio->io_spa) && 3265 (zio->io_flags & (ZIO_FLAG_RESILVER | ZIO_FLAG_SCRUB))) { 3266 for (int c = 0; c < rr->rr_cols; c++) { 3267 raidz_col_t *rc = &rr->rr_col[c]; 3268 vdev_t *vd = zio->io_vd; 3269 3270 if (rc->rc_shadow_devidx == INT_MAX || rc->rc_size == 0) 3271 continue; 3272 vdev_t *cvd = vd->vdev_child[rc->rc_shadow_devidx]; 3273 3274 /* 3275 * Note: We don't want to update the repair stats 3276 * because that would incorrectly indicate that there 3277 * was bad data to repair, which we aren't sure about. 3278 * By clearing the SCAN_THREAD flag, we prevent this 3279 * from happening, despite having the REPAIR flag set. 3280 * We need to set SELF_HEAL so that this i/o can't be 3281 * bypassed by zio_vdev_io_start(). 3282 */ 3283 zio_t *cio = zio_vdev_child_io(zio, NULL, cvd, 3284 rc->rc_shadow_offset, rc->rc_abd, rc->rc_size, 3285 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE, 3286 ZIO_FLAG_IO_REPAIR | ZIO_FLAG_SELF_HEAL, 3287 NULL, NULL); 3288 cio->io_flags &= ~ZIO_FLAG_SCAN_THREAD; 3289 zio_nowait(cio); 3290 } 3291 } 3292 } 3293 3294 static void 3295 raidz_restore_orig_data(raidz_map_t *rm) 3296 { 3297 for (int i = 0; i < rm->rm_nrows; i++) { 3298 raidz_row_t *rr = rm->rm_row[i]; 3299 for (int c = 0; c < rr->rr_cols; c++) { 3300 raidz_col_t *rc = &rr->rr_col[c]; 3301 if (rc->rc_need_orig_restore) { 3302 abd_copy(rc->rc_abd, 3303 rc->rc_orig_data, rc->rc_size); 3304 rc->rc_need_orig_restore = B_FALSE; 3305 } 3306 } 3307 } 3308 } 3309 3310 /* 3311 * During raidz_reconstruct() for expanded VDEV, we need special consideration 3312 * failure simulations. See note in raidz_reconstruct() on simulating failure 3313 * of a pre-expansion device. 3314 * 3315 * Treating logical child i as failed, return TRUE if the given column should 3316 * be treated as failed. The idea of logical children allows us to imagine 3317 * that a disk silently failed before a RAIDZ expansion (reads from this disk 3318 * succeed but return the wrong data). Since the expansion doesn't verify 3319 * checksums, the incorrect data will be moved to new locations spread among 3320 * the children (going diagonally across them). 3321 * 3322 * Higher "logical child failures" (values of `i`) indicate these 3323 * "pre-expansion failures". The first physical_width values imagine that a 3324 * current child failed; the next physical_width-1 values imagine that a 3325 * child failed before the most recent expansion; the next physical_width-2 3326 * values imagine a child failed in the expansion before that, etc. 3327 */ 3328 static boolean_t 3329 raidz_simulate_failure(int physical_width, int original_width, int ashift, 3330 int i, raidz_col_t *rc) 3331 { 3332 uint64_t sector_id = 3333 physical_width * (rc->rc_offset >> ashift) + 3334 rc->rc_devidx; 3335 3336 for (int w = physical_width; w >= original_width; w--) { 3337 if (i < w) { 3338 return (sector_id % w == i); 3339 } else { 3340 i -= w; 3341 } 3342 } 3343 ASSERT(!"invalid logical child id"); 3344 return (B_FALSE); 3345 } 3346 3347 /* 3348 * returns EINVAL if reconstruction of the block will not be possible 3349 * returns ECKSUM if this specific reconstruction failed 3350 * returns 0 on successful reconstruction 3351 */ 3352 static int 3353 raidz_reconstruct(zio_t *zio, int *ltgts, int ntgts, int nparity) 3354 { 3355 vdev_t *vd = zio->io_vd; 3356 raidz_map_t *rm = zio->io_vsd; 3357 int physical_width = vd->vdev_children; 3358 int dbgmsg = zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT; 3359 3360 if (vd->vdev_ops == &vdev_draid_ops) { 3361 vdev_draid_config_t *vdc = vd->vdev_tsd; 3362 physical_width = vdc->vdc_children; 3363 } 3364 3365 int original_width = (rm->rm_original_width != 0) ? 3366 rm->rm_original_width : physical_width; 3367 3368 if (dbgmsg) { 3369 zfs_dbgmsg("raidz_reconstruct_expanded(zio=%px ltgts=%u,%u,%u " 3370 "ntgts=%u", zio, ltgts[0], ltgts[1], ltgts[2], ntgts); 3371 } 3372 3373 /* Reconstruct each row */ 3374 for (int r = 0; r < rm->rm_nrows; r++) { 3375 raidz_row_t *rr = rm->rm_row[r]; 3376 int my_tgts[VDEV_RAIDZ_MAXPARITY]; /* value is child id */ 3377 int t = 0; 3378 int dead = 0; 3379 int dead_data = 0; 3380 3381 if (dbgmsg) 3382 zfs_dbgmsg("raidz_reconstruct_expanded(row=%u)", r); 3383 3384 for (int c = 0; c < rr->rr_cols; c++) { 3385 raidz_col_t *rc = &rr->rr_col[c]; 3386 ASSERT0(rc->rc_need_orig_restore); 3387 if (rc->rc_error != 0) { 3388 dead++; 3389 if (c >= nparity) 3390 dead_data++; 3391 continue; 3392 } 3393 if (rc->rc_size == 0) 3394 continue; 3395 for (int lt = 0; lt < ntgts; lt++) { 3396 if (raidz_simulate_failure(physical_width, 3397 original_width, 3398 zio->io_vd->vdev_top->vdev_ashift, 3399 ltgts[lt], rc)) { 3400 if (rc->rc_orig_data == NULL) { 3401 rc->rc_orig_data = 3402 abd_alloc_linear( 3403 rc->rc_size, B_TRUE); 3404 abd_copy(rc->rc_orig_data, 3405 rc->rc_abd, rc->rc_size); 3406 } 3407 rc->rc_need_orig_restore = B_TRUE; 3408 3409 dead++; 3410 if (c >= nparity) 3411 dead_data++; 3412 /* 3413 * Note: simulating failure of a 3414 * pre-expansion device can hit more 3415 * than one column, in which case we 3416 * might try to simulate more failures 3417 * than can be reconstructed, which is 3418 * also more than the size of my_tgts. 3419 * This check prevents accessing past 3420 * the end of my_tgts. The "dead > 3421 * nparity" check below will fail this 3422 * reconstruction attempt. 3423 */ 3424 if (t < VDEV_RAIDZ_MAXPARITY) { 3425 my_tgts[t++] = c; 3426 if (dbgmsg) { 3427 zfs_dbgmsg("simulating " 3428 "failure of col %u " 3429 "devidx %u", c, 3430 (int)rc->rc_devidx); 3431 } 3432 } 3433 break; 3434 } 3435 } 3436 } 3437 if (dead > nparity) { 3438 /* reconstruction not possible */ 3439 if (dbgmsg) { 3440 zfs_dbgmsg("reconstruction not possible; " 3441 "too many failures"); 3442 } 3443 raidz_restore_orig_data(rm); 3444 return (EINVAL); 3445 } 3446 if (dead_data > 0) 3447 vdev_raidz_reconstruct_row(rm, rr, my_tgts, t); 3448 } 3449 3450 /* Check for success */ 3451 if (raidz_checksum_verify(zio) == 0) { 3452 if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR) 3453 return (0); 3454 3455 /* Reconstruction succeeded - report errors */ 3456 for (int i = 0; i < rm->rm_nrows; i++) { 3457 raidz_row_t *rr = rm->rm_row[i]; 3458 3459 for (int c = 0; c < rr->rr_cols; c++) { 3460 raidz_col_t *rc = &rr->rr_col[c]; 3461 if (rc->rc_need_orig_restore) { 3462 /* 3463 * Note: if this is a parity column, 3464 * we don't really know if it's wrong. 3465 * We need to let 3466 * vdev_raidz_io_done_verified() check 3467 * it, and if we set rc_error, it will 3468 * think that it is a "known" error 3469 * that doesn't need to be checked 3470 * or corrected. 3471 */ 3472 if (rc->rc_error == 0 && 3473 c >= rr->rr_firstdatacol) { 3474 vdev_raidz_checksum_error(zio, 3475 rc, rc->rc_orig_data); 3476 rc->rc_error = 3477 SET_ERROR(ECKSUM); 3478 } 3479 rc->rc_need_orig_restore = B_FALSE; 3480 } 3481 } 3482 3483 vdev_raidz_io_done_verified(zio, rr); 3484 } 3485 3486 zio_checksum_verified(zio); 3487 3488 if (dbgmsg) { 3489 zfs_dbgmsg("reconstruction successful " 3490 "(checksum verified)"); 3491 } 3492 return (0); 3493 } 3494 3495 /* Reconstruction failed - restore original data */ 3496 raidz_restore_orig_data(rm); 3497 if (dbgmsg) { 3498 zfs_dbgmsg("raidz_reconstruct_expanded(zio=%px) checksum " 3499 "failed", zio); 3500 } 3501 return (ECKSUM); 3502 } 3503 3504 /* 3505 * Iterate over all combinations of N bad vdevs and attempt a reconstruction. 3506 * Note that the algorithm below is non-optimal because it doesn't take into 3507 * account how reconstruction is actually performed. For example, with 3508 * triple-parity RAID-Z the reconstruction procedure is the same if column 4 3509 * is targeted as invalid as if columns 1 and 4 are targeted since in both 3510 * cases we'd only use parity information in column 0. 3511 * 3512 * The order that we find the various possible combinations of failed 3513 * disks is dictated by these rules: 3514 * - Examine each "slot" (the "i" in tgts[i]) 3515 * - Try to increment this slot (tgts[i] += 1) 3516 * - if we can't increment because it runs into the next slot, 3517 * reset our slot to the minimum, and examine the next slot 3518 * 3519 * For example, with a 6-wide RAIDZ3, and no known errors (so we have to choose 3520 * 3 columns to reconstruct), we will generate the following sequence: 3521 * 3522 * STATE ACTION 3523 * 0 1 2 special case: skip since these are all parity 3524 * 0 1 3 first slot: reset to 0; middle slot: increment to 2 3525 * 0 2 3 first slot: increment to 1 3526 * 1 2 3 first: reset to 0; middle: reset to 1; last: increment to 4 3527 * 0 1 4 first: reset to 0; middle: increment to 2 3528 * 0 2 4 first: increment to 1 3529 * 1 2 4 first: reset to 0; middle: increment to 3 3530 * 0 3 4 first: increment to 1 3531 * 1 3 4 first: increment to 2 3532 * 2 3 4 first: reset to 0; middle: reset to 1; last: increment to 5 3533 * 0 1 5 first: reset to 0; middle: increment to 2 3534 * 0 2 5 first: increment to 1 3535 * 1 2 5 first: reset to 0; middle: increment to 3 3536 * 0 3 5 first: increment to 1 3537 * 1 3 5 first: increment to 2 3538 * 2 3 5 first: reset to 0; middle: increment to 4 3539 * 0 4 5 first: increment to 1 3540 * 1 4 5 first: increment to 2 3541 * 2 4 5 first: increment to 3 3542 * 3 4 5 done 3543 * 3544 * This strategy works for dRAID but is less efficient when there are a large 3545 * number of child vdevs and therefore permutations to check. Furthermore, 3546 * since the raidz_map_t rows likely do not overlap, reconstruction would be 3547 * possible as long as there are no more than nparity data errors per row. 3548 * These additional permutations are not currently checked but could be as 3549 * a future improvement. 3550 * 3551 * Returns 0 on success, ECKSUM on failure. 3552 */ 3553 static int 3554 vdev_raidz_combrec(zio_t *zio) 3555 { 3556 vdev_t *vd = zio->io_vd; 3557 int nparity = vdev_get_nparity(vd); 3558 raidz_map_t *rm = zio->io_vsd; 3559 int physical_width = zio->io_vd->vdev_children; 3560 3561 if (vd->vdev_ops == &vdev_draid_ops) { 3562 vdev_draid_config_t *vdc = vd->vdev_tsd; 3563 nparity = vdc->vdc_nparity; 3564 physical_width = vdc->vdc_children; 3565 } 3566 3567 int original_width = (rm->rm_original_width != 0) ? 3568 rm->rm_original_width : physical_width; 3569 3570 for (int i = 0; i < rm->rm_nrows; i++) { 3571 raidz_row_t *rr = rm->rm_row[i]; 3572 int total_errors = 0; 3573 3574 for (int c = 0; c < rr->rr_cols; c++) { 3575 if (rr->rr_col[c].rc_error) 3576 total_errors++; 3577 } 3578 3579 if (total_errors > nparity) 3580 return (vdev_raidz_worst_error(rr)); 3581 } 3582 3583 for (int num_failures = 1; num_failures <= nparity; num_failures++) { 3584 int tstore[VDEV_RAIDZ_MAXPARITY + 2]; 3585 int *ltgts = &tstore[1]; /* value is logical child ID */ 3586 3587 3588 /* 3589 * Determine number of logical children, n. See comment 3590 * above raidz_simulate_failure(). 3591 */ 3592 int n = 0; 3593 for (int w = physical_width; 3594 w >= original_width; w--) { 3595 n += w; 3596 } 3597 3598 ASSERT3U(num_failures, <=, nparity); 3599 ASSERT3U(num_failures, <=, VDEV_RAIDZ_MAXPARITY); 3600 3601 /* Handle corner cases in combrec logic */ 3602 ltgts[-1] = -1; 3603 for (int i = 0; i < num_failures; i++) { 3604 ltgts[i] = i; 3605 } 3606 ltgts[num_failures] = n; 3607 3608 for (;;) { 3609 int err = raidz_reconstruct(zio, ltgts, num_failures, 3610 nparity); 3611 if (err == EINVAL) { 3612 /* 3613 * Reconstruction not possible with this # 3614 * failures; try more failures. 3615 */ 3616 break; 3617 } else if (err == 0) 3618 return (0); 3619 3620 /* Compute next targets to try */ 3621 for (int t = 0; ; t++) { 3622 ASSERT3U(t, <, num_failures); 3623 ltgts[t]++; 3624 if (ltgts[t] == n) { 3625 /* try more failures */ 3626 ASSERT3U(t, ==, num_failures - 1); 3627 if (zfs_flags & 3628 ZFS_DEBUG_RAIDZ_RECONSTRUCT) { 3629 zfs_dbgmsg("reconstruction " 3630 "failed for num_failures=" 3631 "%u; tried all " 3632 "combinations", 3633 num_failures); 3634 } 3635 break; 3636 } 3637 3638 ASSERT3U(ltgts[t], <, n); 3639 ASSERT3U(ltgts[t], <=, ltgts[t + 1]); 3640 3641 /* 3642 * If that spot is available, we're done here. 3643 * Try the next combination. 3644 */ 3645 if (ltgts[t] != ltgts[t + 1]) 3646 break; // found next combination 3647 3648 /* 3649 * Otherwise, reset this tgt to the minimum, 3650 * and move on to the next tgt. 3651 */ 3652 ltgts[t] = ltgts[t - 1] + 1; 3653 ASSERT3U(ltgts[t], ==, t); 3654 } 3655 3656 /* Increase the number of failures and keep trying. */ 3657 if (ltgts[num_failures - 1] == n) 3658 break; 3659 } 3660 } 3661 if (zfs_flags & ZFS_DEBUG_RAIDZ_RECONSTRUCT) 3662 zfs_dbgmsg("reconstruction failed for all num_failures"); 3663 return (ECKSUM); 3664 } 3665 3666 void 3667 vdev_raidz_reconstruct(raidz_map_t *rm, const int *t, int nt) 3668 { 3669 for (uint64_t row = 0; row < rm->rm_nrows; row++) { 3670 raidz_row_t *rr = rm->rm_row[row]; 3671 vdev_raidz_reconstruct_row(rm, rr, t, nt); 3672 } 3673 } 3674 3675 /* 3676 * Complete a write IO operation on a RAIDZ VDev 3677 * 3678 * Outline: 3679 * 1. Check for errors on the child IOs. 3680 * 2. Return, setting an error code if too few child VDevs were written 3681 * to reconstruct the data later. Note that partial writes are 3682 * considered successful if they can be reconstructed at all. 3683 */ 3684 static void 3685 vdev_raidz_io_done_write_impl(zio_t *zio, raidz_row_t *rr) 3686 { 3687 int normal_errors = 0; 3688 int shadow_errors = 0; 3689 int retryable_errors = 0; 3690 3691 ASSERT3U(rr->rr_missingparity, <=, rr->rr_firstdatacol); 3692 ASSERT3U(rr->rr_missingdata, <=, rr->rr_cols - rr->rr_firstdatacol); 3693 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 3694 3695 for (int c = 0; c < rr->rr_cols; c++) { 3696 raidz_col_t *rc = &rr->rr_col[c]; 3697 3698 if (rc->rc_error != 0) { 3699 ASSERT(rc->rc_error != ECKSUM); /* child has no bp */ 3700 normal_errors++; 3701 } 3702 if (rc->rc_shadow_error != 0) { 3703 ASSERT(rc->rc_shadow_error != ECKSUM); 3704 shadow_errors++; 3705 } 3706 if (rc->rc_error || rc->rc_shadow_error) { 3707 vdev_t *cvd = zio->io_vd->vdev_child[rc->rc_devidx]; 3708 if (!(vdev_is_dead(cvd) || cvd->vdev_cant_write)) 3709 retryable_errors++; 3710 } 3711 } 3712 3713 /* 3714 * Treat partial writes as a success. If we couldn't write enough 3715 * columns to reconstruct the data, the I/O failed. Otherwise, good 3716 * enough. Note that in the case of a shadow write (during raidz 3717 * expansion), depending on if we crash, either the normal (old) or 3718 * shadow (new) location may become the "real" version of the block, 3719 * so both locations must have sufficient redundancy. 3720 * 3721 * Now that we support write reallocation, it would be better 3722 * to treat partial failure as real failure unless there are 3723 * no non-degraded top-level vdevs left, and not update DTLs 3724 * if we intend to reallocate. 3725 */ 3726 if (normal_errors > rr->rr_firstdatacol || 3727 shadow_errors > rr->rr_firstdatacol) { 3728 zio->io_error = zio_worst_error(zio->io_error, 3729 vdev_raidz_worst_error(rr)); 3730 } else if (retryable_errors && zfs_scrub_partial_writes) { 3731 zio->io_flags |= ZIO_FLAG_POSTREAD; 3732 } 3733 } 3734 3735 static void 3736 vdev_raidz_io_done_reconstruct_known_missing(zio_t *zio, raidz_map_t *rm, 3737 raidz_row_t *rr) 3738 { 3739 int parity_errors = 0; 3740 int parity_untried = 0; 3741 int data_errors = 0; 3742 int total_errors = 0; 3743 3744 ASSERT3U(rr->rr_missingparity, <=, rr->rr_firstdatacol); 3745 ASSERT3U(rr->rr_missingdata, <=, rr->rr_cols - rr->rr_firstdatacol); 3746 3747 for (int c = 0; c < rr->rr_cols; c++) { 3748 raidz_col_t *rc = &rr->rr_col[c]; 3749 3750 /* 3751 * If scrubbing and a replacing/sparing child vdev determined 3752 * that not all of its children have an identical copy of the 3753 * data, then clear the error so the column is treated like 3754 * any other read and force a repair to correct the damage. 3755 */ 3756 if (rc->rc_error == ECKSUM) { 3757 ASSERT(zio->io_flags & ZIO_FLAG_SCRUB); 3758 vdev_raidz_checksum_error(zio, rc, rc->rc_abd); 3759 rc->rc_force_repair = 1; 3760 rc->rc_error = 0; 3761 } 3762 3763 if (rc->rc_error) { 3764 if (c < rr->rr_firstdatacol) 3765 parity_errors++; 3766 else 3767 data_errors++; 3768 3769 total_errors++; 3770 } else if (c < rr->rr_firstdatacol && !rc->rc_tried) { 3771 parity_untried++; 3772 } 3773 } 3774 3775 /* 3776 * If there were data errors and the number of errors we saw was 3777 * correctable -- less than or equal to the number of parity disks read 3778 * -- reconstruct based on the missing data. 3779 */ 3780 if (data_errors != 0 && 3781 total_errors <= rr->rr_firstdatacol - parity_untried) { 3782 /* 3783 * We either attempt to read all the parity columns or 3784 * none of them. If we didn't try to read parity, we 3785 * wouldn't be here in the correctable case. There must 3786 * also have been fewer parity errors than parity 3787 * columns or, again, we wouldn't be in this code path. 3788 */ 3789 ASSERT0(parity_untried); 3790 ASSERT(parity_errors < rr->rr_firstdatacol); 3791 3792 /* 3793 * Identify the data columns that reported an error. 3794 */ 3795 int n = 0; 3796 int tgts[VDEV_RAIDZ_MAXPARITY]; 3797 for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 3798 raidz_col_t *rc = &rr->rr_col[c]; 3799 if (rc->rc_error != 0) { 3800 ASSERT(n < VDEV_RAIDZ_MAXPARITY); 3801 tgts[n++] = c; 3802 } 3803 } 3804 3805 ASSERT(rr->rr_firstdatacol >= n); 3806 3807 vdev_raidz_reconstruct_row(rm, rr, tgts, n); 3808 } 3809 } 3810 3811 /* 3812 * Return the number of reads issued. 3813 */ 3814 static int 3815 vdev_raidz_read_all(zio_t *zio, raidz_row_t *rr) 3816 { 3817 vdev_t *vd = zio->io_vd; 3818 int nread = 0; 3819 3820 rr->rr_missingdata = 0; 3821 rr->rr_missingparity = 0; 3822 3823 /* 3824 * If this rows contains empty sectors which are not required 3825 * for a normal read then allocate an ABD for them now so they 3826 * may be read, verified, and any needed repairs performed. 3827 */ 3828 if (rr->rr_nempty != 0 && rr->rr_abd_empty == NULL) 3829 vdev_draid_map_alloc_empty(zio, rr); 3830 3831 for (int c = 0; c < rr->rr_cols; c++) { 3832 raidz_col_t *rc = &rr->rr_col[c]; 3833 if (rc->rc_tried || rc->rc_size == 0) 3834 continue; 3835 3836 zio_nowait(zio_vdev_child_io(zio, NULL, 3837 vd->vdev_child[rc->rc_devidx], 3838 rc->rc_offset, rc->rc_abd, rc->rc_size, 3839 zio->io_type, zio->io_priority, 0, 3840 vdev_raidz_child_done, rc)); 3841 nread++; 3842 } 3843 return (nread); 3844 } 3845 3846 /* 3847 * We're here because either there were too many errors to even attempt 3848 * reconstruction (total_errors == rm_first_datacol), or vdev_*_combrec() 3849 * failed. In either case, there is enough bad data to prevent reconstruction. 3850 * Start checksum ereports for all children which haven't failed. 3851 */ 3852 static void 3853 vdev_raidz_io_done_unrecoverable(zio_t *zio) 3854 { 3855 raidz_map_t *rm = zio->io_vsd; 3856 3857 for (int i = 0; i < rm->rm_nrows; i++) { 3858 raidz_row_t *rr = rm->rm_row[i]; 3859 3860 for (int c = 0; c < rr->rr_cols; c++) { 3861 raidz_col_t *rc = &rr->rr_col[c]; 3862 vdev_t *cvd = zio->io_vd->vdev_child[rc->rc_devidx]; 3863 3864 if (rc->rc_error != 0) 3865 continue; 3866 3867 zio_bad_cksum_t zbc; 3868 zbc.zbc_has_cksum = 0; 3869 zbc.zbc_injected = rm->rm_ecksuminjected; 3870 mutex_enter(&cvd->vdev_stat_lock); 3871 cvd->vdev_stat.vs_checksum_errors++; 3872 mutex_exit(&cvd->vdev_stat_lock); 3873 (void) zfs_ereport_start_checksum(zio->io_spa, 3874 cvd, &zio->io_bookmark, zio, rc->rc_offset, 3875 rc->rc_size, &zbc); 3876 } 3877 } 3878 } 3879 3880 void 3881 vdev_raidz_io_done(zio_t *zio) 3882 { 3883 raidz_map_t *rm = zio->io_vsd; 3884 3885 ASSERT(zio->io_bp != NULL); 3886 if (zio->io_type == ZIO_TYPE_WRITE) { 3887 for (int i = 0; i < rm->rm_nrows; i++) { 3888 vdev_raidz_io_done_write_impl(zio, rm->rm_row[i]); 3889 } 3890 } else { 3891 if (rm->rm_phys_col) { 3892 /* 3893 * This is an aggregated read. Copy the data and status 3894 * from the aggregate abd's to the individual rows. 3895 */ 3896 for (int i = 0; i < rm->rm_nrows; i++) { 3897 raidz_row_t *rr = rm->rm_row[i]; 3898 3899 for (int c = 0; c < rr->rr_cols; c++) { 3900 raidz_col_t *rc = &rr->rr_col[c]; 3901 if (rc->rc_tried || rc->rc_size == 0) 3902 continue; 3903 3904 raidz_col_t *prc = 3905 &rm->rm_phys_col[rc->rc_devidx]; 3906 rc->rc_error = prc->rc_error; 3907 rc->rc_tried = prc->rc_tried; 3908 rc->rc_skipped = prc->rc_skipped; 3909 if (c >= rr->rr_firstdatacol) { 3910 /* 3911 * Note: this is slightly faster 3912 * than using abd_copy_off(). 3913 */ 3914 char *physbuf = abd_to_buf( 3915 prc->rc_abd); 3916 void *physloc = physbuf + 3917 rc->rc_offset - 3918 prc->rc_offset; 3919 3920 abd_copy_from_buf(rc->rc_abd, 3921 physloc, rc->rc_size); 3922 } 3923 } 3924 } 3925 } 3926 3927 for (int i = 0; i < rm->rm_nrows; i++) { 3928 raidz_row_t *rr = rm->rm_row[i]; 3929 vdev_raidz_io_done_reconstruct_known_missing(zio, 3930 rm, rr); 3931 } 3932 3933 if (raidz_checksum_verify(zio) == 0) { 3934 if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR) 3935 goto done; 3936 3937 for (int i = 0; i < rm->rm_nrows; i++) { 3938 raidz_row_t *rr = rm->rm_row[i]; 3939 vdev_raidz_io_done_verified(zio, rr); 3940 } 3941 /* Periodically check for a read outlier */ 3942 if (zio->io_type == ZIO_TYPE_READ) 3943 vdev_child_slow_outlier(zio); 3944 zio_checksum_verified(zio); 3945 } else { 3946 /* 3947 * A sequential resilver has no checksum which makes 3948 * combinatoral reconstruction impossible. This code 3949 * path is unreachable since raidz_checksum_verify() 3950 * has no checksum to verify and must succeed. 3951 */ 3952 ASSERT3U(zio->io_priority, !=, ZIO_PRIORITY_REBUILD); 3953 3954 /* 3955 * This isn't a typical situation -- either we got a 3956 * read error or a child silently returned bad data. 3957 * Read every block so we can try again with as much 3958 * data and parity as we can track down. If we've 3959 * already been through once before, all children will 3960 * be marked as tried so we'll proceed to combinatorial 3961 * reconstruction. 3962 */ 3963 int nread = 0; 3964 for (int i = 0; i < rm->rm_nrows; i++) { 3965 nread += vdev_raidz_read_all(zio, 3966 rm->rm_row[i]); 3967 } 3968 if (nread != 0) { 3969 /* 3970 * Normally our stage is VDEV_IO_DONE, but if 3971 * we've already called redone(), it will have 3972 * changed to VDEV_IO_START, in which case we 3973 * don't want to call redone() again. 3974 */ 3975 if (zio->io_stage != ZIO_STAGE_VDEV_IO_START) 3976 zio_vdev_io_redone(zio); 3977 return; 3978 } 3979 /* 3980 * It would be too expensive to try every possible 3981 * combination of failed sectors in every row, so 3982 * instead we try every combination of failed current or 3983 * past physical disk. This means that if the incorrect 3984 * sectors were all on Nparity disks at any point in the 3985 * past, we will find the correct data. The only known 3986 * case where this is less durable than a non-expanded 3987 * RAIDZ, is if we have a silent failure during 3988 * expansion. In that case, one block could be 3989 * partially in the old format and partially in the 3990 * new format, so we'd lost some sectors from the old 3991 * format and some from the new format. 3992 * 3993 * e.g. logical_width=4 physical_width=6 3994 * the 15 (6+5+4) possible failed disks are: 3995 * width=6 child=0 3996 * width=6 child=1 3997 * width=6 child=2 3998 * width=6 child=3 3999 * width=6 child=4 4000 * width=6 child=5 4001 * width=5 child=0 4002 * width=5 child=1 4003 * width=5 child=2 4004 * width=5 child=3 4005 * width=5 child=4 4006 * width=4 child=0 4007 * width=4 child=1 4008 * width=4 child=2 4009 * width=4 child=3 4010 * And we will try every combination of Nparity of these 4011 * failing. 4012 * 4013 * As a first pass, we can generate every combo, 4014 * and try reconstructing, ignoring any known 4015 * failures. If any row has too many known + simulated 4016 * failures, then we bail on reconstructing with this 4017 * number of simulated failures. As an improvement, 4018 * we could detect the number of whole known failures 4019 * (i.e. we have known failures on these disks for 4020 * every row; the disks never succeeded), and 4021 * subtract that from the max # failures to simulate. 4022 * We could go even further like the current 4023 * combrec code, but that doesn't seem like it 4024 * gains us very much. If we simulate a failure 4025 * that is also a known failure, that's fine. 4026 */ 4027 zio->io_error = vdev_raidz_combrec(zio); 4028 if (zio->io_error == ECKSUM && 4029 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 4030 vdev_raidz_io_done_unrecoverable(zio); 4031 } 4032 } 4033 } 4034 done: 4035 if (rm->rm_lr != NULL) { 4036 zfs_rangelock_exit(rm->rm_lr); 4037 rm->rm_lr = NULL; 4038 } 4039 } 4040 4041 static void 4042 vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded) 4043 { 4044 vdev_raidz_t *vdrz = vd->vdev_tsd; 4045 if (faulted > vdrz->vd_nparity) 4046 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 4047 VDEV_AUX_NO_REPLICAS); 4048 else if (degraded + faulted != 0) 4049 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 4050 else 4051 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 4052 } 4053 4054 /* 4055 * Determine if any portion of the provided block resides on a child vdev 4056 * with a dirty DTL and therefore needs to be resilvered. The function 4057 * assumes that at least one DTL is dirty which implies that full stripe 4058 * width blocks must be resilvered. 4059 */ 4060 static boolean_t 4061 vdev_raidz_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize, 4062 uint64_t phys_birth) 4063 { 4064 vdev_raidz_t *vdrz = vd->vdev_tsd; 4065 4066 /* 4067 * If we're in the middle of a RAIDZ expansion, this block may be in 4068 * the old and/or new location. For simplicity, always resilver it. 4069 */ 4070 if (vdrz->vn_vre.vre_state == DSS_SCANNING) 4071 return (B_TRUE); 4072 4073 uint64_t dcols = vd->vdev_children; 4074 uint64_t nparity = vdrz->vd_nparity; 4075 uint64_t ashift = vd->vdev_top->vdev_ashift; 4076 /* The starting RAIDZ (parent) vdev sector of the block. */ 4077 uint64_t b = DVA_GET_OFFSET(dva) >> ashift; 4078 /* The zio's size in units of the vdev's minimum sector size. */ 4079 uint64_t s = ((psize - 1) >> ashift) + 1; 4080 /* The first column for this stripe. */ 4081 uint64_t f = b % dcols; 4082 4083 /* Unreachable by sequential resilver. */ 4084 ASSERT3U(phys_birth, !=, TXG_UNKNOWN); 4085 4086 if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1)) 4087 return (B_FALSE); 4088 4089 if (s + nparity >= dcols) 4090 return (B_TRUE); 4091 4092 for (uint64_t c = 0; c < s + nparity; c++) { 4093 uint64_t devidx = (f + c) % dcols; 4094 vdev_t *cvd = vd->vdev_child[devidx]; 4095 4096 /* 4097 * dsl_scan_need_resilver() already checked vd with 4098 * vdev_dtl_contains(). So here just check cvd with 4099 * vdev_dtl_empty(), cheaper and a good approximation. 4100 */ 4101 if (!vdev_dtl_empty(cvd, DTL_PARTIAL)) 4102 return (B_TRUE); 4103 } 4104 4105 return (B_FALSE); 4106 } 4107 4108 static void 4109 vdev_raidz_xlate(vdev_t *cvd, const zfs_range_seg64_t *logical_rs, 4110 zfs_range_seg64_t *physical_rs, zfs_range_seg64_t *remain_rs) 4111 { 4112 (void) remain_rs; 4113 4114 vdev_t *raidvd = cvd->vdev_parent; 4115 ASSERT(raidvd->vdev_ops == &vdev_raidz_ops); 4116 4117 vdev_raidz_t *vdrz = raidvd->vdev_tsd; 4118 4119 if (vdrz->vn_vre.vre_state == DSS_SCANNING) { 4120 /* 4121 * We're in the middle of expansion, in which case the 4122 * translation is in flux. Any answer we give may be wrong 4123 * by the time we return, so it isn't safe for the caller to 4124 * act on it. Therefore we say that this range isn't present 4125 * on any children. The only consumers of this are "zpool 4126 * initialize" and trimming, both of which are "best effort" 4127 * anyway. 4128 */ 4129 physical_rs->rs_start = physical_rs->rs_end = 0; 4130 remain_rs->rs_start = remain_rs->rs_end = 0; 4131 return; 4132 } 4133 4134 uint64_t width = vdrz->vd_physical_width; 4135 uint64_t tgt_col = cvd->vdev_id; 4136 uint64_t ashift = raidvd->vdev_top->vdev_ashift; 4137 4138 /* make sure the offsets are block-aligned */ 4139 ASSERT0(logical_rs->rs_start % (1 << ashift)); 4140 ASSERT0(logical_rs->rs_end % (1 << ashift)); 4141 uint64_t b_start = logical_rs->rs_start >> ashift; 4142 uint64_t b_end = logical_rs->rs_end >> ashift; 4143 4144 uint64_t start_row = 0; 4145 if (b_start > tgt_col) /* avoid underflow */ 4146 start_row = ((b_start - tgt_col - 1) / width) + 1; 4147 4148 uint64_t end_row = 0; 4149 if (b_end > tgt_col) 4150 end_row = ((b_end - tgt_col - 1) / width) + 1; 4151 4152 physical_rs->rs_start = start_row << ashift; 4153 physical_rs->rs_end = end_row << ashift; 4154 4155 ASSERT3U(physical_rs->rs_start, <=, logical_rs->rs_start); 4156 ASSERT3U(physical_rs->rs_end - physical_rs->rs_start, <=, 4157 logical_rs->rs_end - logical_rs->rs_start); 4158 } 4159 4160 static void 4161 raidz_reflow_sync(void *arg, dmu_tx_t *tx) 4162 { 4163 spa_t *spa = arg; 4164 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; 4165 vdev_raidz_expand_t *vre = spa->spa_raidz_expand; 4166 4167 /* 4168 * Ensure there are no i/os to the range that is being committed. 4169 */ 4170 uint64_t old_offset = RRSS_GET_OFFSET(&spa->spa_uberblock); 4171 ASSERT3U(vre->vre_offset_pertxg[txgoff], >=, old_offset); 4172 4173 mutex_enter(&vre->vre_lock); 4174 uint64_t new_offset = 4175 MIN(vre->vre_offset_pertxg[txgoff], vre->vre_failed_offset); 4176 /* 4177 * We should not have committed anything that failed. 4178 */ 4179 VERIFY3U(vre->vre_failed_offset, >=, old_offset); 4180 mutex_exit(&vre->vre_lock); 4181 4182 zfs_locked_range_t *lr = zfs_rangelock_enter(&vre->vre_rangelock, 4183 old_offset, new_offset - old_offset, 4184 RL_WRITER); 4185 4186 /* 4187 * Update the uberblock that will be written when this txg completes. 4188 */ 4189 RAIDZ_REFLOW_SET(&spa->spa_uberblock, 4190 RRSS_SCRATCH_INVALID_SYNCED_REFLOW, new_offset); 4191 vre->vre_offset_pertxg[txgoff] = 0; 4192 zfs_rangelock_exit(lr); 4193 4194 mutex_enter(&vre->vre_lock); 4195 vre->vre_bytes_copied += vre->vre_bytes_copied_pertxg[txgoff]; 4196 vre->vre_bytes_copied_pertxg[txgoff] = 0; 4197 mutex_exit(&vre->vre_lock); 4198 4199 vdev_t *vd = vdev_lookup_top(spa, vre->vre_vdev_id); 4200 VERIFY0(zap_update(spa->spa_meta_objset, 4201 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED, 4202 sizeof (vre->vre_bytes_copied), 1, &vre->vre_bytes_copied, tx)); 4203 } 4204 4205 static void 4206 raidz_reflow_complete_sync(void *arg, dmu_tx_t *tx) 4207 { 4208 spa_t *spa = arg; 4209 vdev_raidz_expand_t *vre = spa->spa_raidz_expand; 4210 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id); 4211 vdev_raidz_t *vdrz = raidvd->vdev_tsd; 4212 4213 for (int i = 0; i < TXG_SIZE; i++) 4214 VERIFY0(vre->vre_offset_pertxg[i]); 4215 4216 reflow_node_t *re = kmem_zalloc(sizeof (*re), KM_SLEEP); 4217 re->re_txg = tx->tx_txg + TXG_CONCURRENT_STATES; 4218 re->re_logical_width = vdrz->vd_physical_width; 4219 mutex_enter(&vdrz->vd_expand_lock); 4220 avl_add(&vdrz->vd_expand_txgs, re); 4221 mutex_exit(&vdrz->vd_expand_lock); 4222 4223 vdev_t *vd = vdev_lookup_top(spa, vre->vre_vdev_id); 4224 4225 /* 4226 * Dirty the config so that the updated ZPOOL_CONFIG_RAIDZ_EXPAND_TXGS 4227 * will get written (based on vd_expand_txgs). 4228 */ 4229 vdev_config_dirty(vd); 4230 4231 /* 4232 * Before we change vre_state, the on-disk state must reflect that we 4233 * have completed all copying, so that vdev_raidz_io_start() can use 4234 * vre_state to determine if the reflow is in progress. See also the 4235 * end of spa_raidz_expand_thread(). 4236 */ 4237 VERIFY3U(RRSS_GET_OFFSET(&spa->spa_ubsync), ==, 4238 raidvd->vdev_ms_count << raidvd->vdev_ms_shift); 4239 4240 vre->vre_end_time = gethrestime_sec(); 4241 vre->vre_state = DSS_FINISHED; 4242 4243 uint64_t state = vre->vre_state; 4244 VERIFY0(zap_update(spa->spa_meta_objset, 4245 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE, 4246 sizeof (state), 1, &state, tx)); 4247 4248 uint64_t end_time = vre->vre_end_time; 4249 VERIFY0(zap_update(spa->spa_meta_objset, 4250 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME, 4251 sizeof (end_time), 1, &end_time, tx)); 4252 4253 spa->spa_uberblock.ub_raidz_reflow_info = 0; 4254 4255 spa_history_log_internal(spa, "raidz vdev expansion completed", tx, 4256 "%s vdev %llu new width %llu", spa_name(spa), 4257 (unsigned long long)vd->vdev_id, 4258 (unsigned long long)vd->vdev_children); 4259 4260 spa->spa_raidz_expand = NULL; 4261 raidvd->vdev_rz_expanding = B_FALSE; 4262 4263 spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); 4264 spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); 4265 spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); 4266 4267 spa_notify_waiters(spa); 4268 4269 /* 4270 * While we're in syncing context take the opportunity to 4271 * setup a scrub. All the data has been sucessfully copied 4272 * but we have not validated any checksums. 4273 */ 4274 setup_sync_arg_t setup_sync_arg = { 4275 .func = POOL_SCAN_SCRUB, 4276 .txgstart = 0, 4277 .txgend = 0, 4278 }; 4279 if (zfs_scrub_after_expand && 4280 dsl_scan_setup_check(&setup_sync_arg.func, tx) == 0) { 4281 dsl_scan_setup_sync(&setup_sync_arg, tx); 4282 } 4283 } 4284 4285 /* 4286 * State of one copy batch. 4287 */ 4288 typedef struct raidz_reflow_arg { 4289 vdev_raidz_expand_t *rra_vre; /* Global expantion state. */ 4290 zfs_locked_range_t *rra_lr; /* Range lock of this batch. */ 4291 uint64_t rra_txg; /* TXG of this batch. */ 4292 uint_t rra_ashift; /* Ashift of the vdev. */ 4293 uint32_t rra_tbd; /* Number of in-flight ZIOs. */ 4294 uint32_t rra_writes; /* Number of write ZIOs. */ 4295 zio_t *rra_zio[]; /* Write ZIO pointers. */ 4296 } raidz_reflow_arg_t; 4297 4298 /* 4299 * Write of the new location on one child is done. Once all of them are done 4300 * we can unlock and free everything. 4301 */ 4302 static void 4303 raidz_reflow_write_done(zio_t *zio) 4304 { 4305 raidz_reflow_arg_t *rra = zio->io_private; 4306 vdev_raidz_expand_t *vre = rra->rra_vre; 4307 4308 abd_free(zio->io_abd); 4309 4310 mutex_enter(&vre->vre_lock); 4311 if (zio->io_error != 0) { 4312 /* Force a reflow pause on errors */ 4313 vre->vre_failed_offset = 4314 MIN(vre->vre_failed_offset, rra->rra_lr->lr_offset); 4315 } 4316 ASSERT3U(vre->vre_outstanding_bytes, >=, zio->io_size); 4317 vre->vre_outstanding_bytes -= zio->io_size; 4318 if (rra->rra_lr->lr_offset + rra->rra_lr->lr_length < 4319 vre->vre_failed_offset) { 4320 vre->vre_bytes_copied_pertxg[rra->rra_txg & TXG_MASK] += 4321 zio->io_size; 4322 } 4323 cv_signal(&vre->vre_cv); 4324 boolean_t done = (--rra->rra_tbd == 0); 4325 mutex_exit(&vre->vre_lock); 4326 4327 if (!done) 4328 return; 4329 spa_config_exit(zio->io_spa, SCL_STATE, zio->io_spa); 4330 zfs_rangelock_exit(rra->rra_lr); 4331 kmem_free(rra, sizeof (*rra) + sizeof (zio_t *) * rra->rra_writes); 4332 } 4333 4334 /* 4335 * Read of the old location on one child is done. Once all of them are done 4336 * writes should have all the data and we can issue them. 4337 */ 4338 static void 4339 raidz_reflow_read_done(zio_t *zio) 4340 { 4341 raidz_reflow_arg_t *rra = zio->io_private; 4342 vdev_raidz_expand_t *vre = rra->rra_vre; 4343 4344 /* Reads of only one block use write ABDs. For bigger free gangs. */ 4345 if (zio->io_size > (1 << rra->rra_ashift)) 4346 abd_free(zio->io_abd); 4347 4348 /* 4349 * If the read failed, or if it was done on a vdev that is not fully 4350 * healthy (e.g. a child that has a resilver in progress), we may not 4351 * have the correct data. Note that it's OK if the write proceeds. 4352 * It may write garbage but the location is otherwise unused and we 4353 * will retry later due to vre_failed_offset. 4354 */ 4355 if (zio->io_error != 0 || !vdev_dtl_empty(zio->io_vd, DTL_MISSING)) { 4356 zfs_dbgmsg("reflow read failed off=%llu size=%llu txg=%llu " 4357 "err=%u partial_dtl_empty=%u missing_dtl_empty=%u", 4358 (long long)rra->rra_lr->lr_offset, 4359 (long long)rra->rra_lr->lr_length, 4360 (long long)rra->rra_txg, 4361 zio->io_error, 4362 vdev_dtl_empty(zio->io_vd, DTL_PARTIAL), 4363 vdev_dtl_empty(zio->io_vd, DTL_MISSING)); 4364 mutex_enter(&vre->vre_lock); 4365 /* Force a reflow pause on errors */ 4366 vre->vre_failed_offset = 4367 MIN(vre->vre_failed_offset, rra->rra_lr->lr_offset); 4368 mutex_exit(&vre->vre_lock); 4369 } 4370 4371 if (atomic_dec_32_nv(&rra->rra_tbd) > 0) 4372 return; 4373 uint32_t writes = rra->rra_tbd = rra->rra_writes; 4374 for (uint64_t i = 0; i < writes; i++) 4375 zio_nowait(rra->rra_zio[i]); 4376 } 4377 4378 static void 4379 raidz_reflow_record_progress(vdev_raidz_expand_t *vre, uint64_t offset, 4380 dmu_tx_t *tx) 4381 { 4382 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; 4383 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4384 4385 if (offset == 0) 4386 return; 4387 4388 mutex_enter(&vre->vre_lock); 4389 ASSERT3U(vre->vre_offset, <=, offset); 4390 vre->vre_offset = offset; 4391 mutex_exit(&vre->vre_lock); 4392 4393 if (vre->vre_offset_pertxg[txgoff] == 0) { 4394 dsl_sync_task_nowait(dmu_tx_pool(tx), raidz_reflow_sync, 4395 spa, tx); 4396 } 4397 vre->vre_offset_pertxg[txgoff] = offset; 4398 } 4399 4400 static boolean_t 4401 vdev_raidz_expand_child_replacing(vdev_t *raidz_vd) 4402 { 4403 for (int i = 0; i < raidz_vd->vdev_children; i++) { 4404 /* Quick check if a child is being replaced */ 4405 if (!raidz_vd->vdev_child[i]->vdev_ops->vdev_op_leaf) 4406 return (B_TRUE); 4407 } 4408 return (B_FALSE); 4409 } 4410 4411 static boolean_t 4412 raidz_reflow_impl(vdev_t *vd, vdev_raidz_expand_t *vre, zfs_range_tree_t *rt, 4413 dmu_tx_t *tx) 4414 { 4415 spa_t *spa = vd->vdev_spa; 4416 uint_t ashift = vd->vdev_top->vdev_ashift; 4417 4418 zfs_range_seg_t *rs = zfs_range_tree_first(rt); 4419 if (rt == NULL) 4420 return (B_FALSE); 4421 uint64_t offset = zfs_rs_get_start(rs, rt); 4422 ASSERT(IS_P2ALIGNED(offset, 1 << ashift)); 4423 uint64_t size = zfs_rs_get_end(rs, rt) - offset; 4424 ASSERT3U(size, >=, 1 << ashift); 4425 ASSERT(IS_P2ALIGNED(size, 1 << ashift)); 4426 4427 uint64_t blkid = offset >> ashift; 4428 uint_t old_children = vd->vdev_children - 1; 4429 4430 /* 4431 * We can only progress to the point that writes will not overlap 4432 * with blocks whose progress has not yet been recorded on disk. 4433 * Since partially-copied rows are still read from the old location, 4434 * we need to stop one row before the sector-wise overlap, to prevent 4435 * row-wise overlap. 4436 * 4437 * Note that even if we are skipping over a large unallocated region, 4438 * we can't move the on-disk progress to `offset`, because concurrent 4439 * writes/allocations could still use the currently-unallocated 4440 * region. 4441 */ 4442 uint64_t ubsync_blkid = 4443 RRSS_GET_OFFSET(&spa->spa_ubsync) >> ashift; 4444 uint64_t next_overwrite_blkid = ubsync_blkid + 4445 ubsync_blkid / old_children - old_children; 4446 VERIFY3U(next_overwrite_blkid, >, ubsync_blkid); 4447 if (blkid >= next_overwrite_blkid) { 4448 raidz_reflow_record_progress(vre, 4449 next_overwrite_blkid << ashift, tx); 4450 return (B_TRUE); 4451 } 4452 4453 size = MIN(size, raidz_expand_max_copy_bytes); 4454 size = MIN(size, (uint64_t)old_children * 4455 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE)); 4456 size = MAX(size, 1 << ashift); 4457 uint_t blocks = MIN(size >> ashift, next_overwrite_blkid - blkid); 4458 size = (uint64_t)blocks << ashift; 4459 4460 zfs_range_tree_remove(rt, offset, size); 4461 4462 uint_t reads = MIN(blocks, old_children); 4463 uint_t writes = MIN(blocks, vd->vdev_children); 4464 raidz_reflow_arg_t *rra = kmem_zalloc(sizeof (*rra) + 4465 sizeof (zio_t *) * writes, KM_SLEEP); 4466 rra->rra_vre = vre; 4467 rra->rra_lr = zfs_rangelock_enter(&vre->vre_rangelock, 4468 offset, size, RL_WRITER); 4469 rra->rra_txg = dmu_tx_get_txg(tx); 4470 rra->rra_ashift = ashift; 4471 rra->rra_tbd = reads; 4472 rra->rra_writes = writes; 4473 4474 raidz_reflow_record_progress(vre, offset + size, tx); 4475 4476 /* 4477 * SCL_STATE will be released when the read and write are done, 4478 * by raidz_reflow_write_done(). 4479 */ 4480 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 4481 4482 /* check if a replacing vdev was added, if so treat it as an error */ 4483 if (vdev_raidz_expand_child_replacing(vd)) { 4484 zfs_dbgmsg("replacing vdev encountered, reflow paused at " 4485 "offset=%llu txg=%llu", 4486 (long long)rra->rra_lr->lr_offset, 4487 (long long)rra->rra_txg); 4488 4489 mutex_enter(&vre->vre_lock); 4490 vre->vre_failed_offset = 4491 MIN(vre->vre_failed_offset, rra->rra_lr->lr_offset); 4492 cv_signal(&vre->vre_cv); 4493 mutex_exit(&vre->vre_lock); 4494 4495 /* drop everything we acquired */ 4496 spa_config_exit(spa, SCL_STATE, spa); 4497 zfs_rangelock_exit(rra->rra_lr); 4498 kmem_free(rra, sizeof (*rra) + sizeof (zio_t *) * writes); 4499 return (B_TRUE); 4500 } 4501 4502 mutex_enter(&vre->vre_lock); 4503 vre->vre_outstanding_bytes += size; 4504 mutex_exit(&vre->vre_lock); 4505 4506 /* Allocate ABD and ZIO for each child we write. */ 4507 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; 4508 zio_t *pio = spa->spa_txg_zio[txgoff]; 4509 uint_t b = blocks / vd->vdev_children; 4510 uint_t bb = blocks % vd->vdev_children; 4511 for (uint_t i = 0; i < writes; i++) { 4512 uint_t n = b + (i < bb); 4513 abd_t *abd = abd_alloc_for_io(n << ashift, B_FALSE); 4514 rra->rra_zio[i] = zio_vdev_child_io(pio, NULL, 4515 vd->vdev_child[(blkid + i) % vd->vdev_children], 4516 ((blkid + i) / vd->vdev_children) << ashift, 4517 abd, n << ashift, ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL, 4518 ZIO_FLAG_CANFAIL, raidz_reflow_write_done, rra); 4519 } 4520 4521 /* 4522 * Allocate and issue ZIO for each child we read. For reads of only 4523 * one block we can use respective writer ABDs, since they will also 4524 * have only one block. For bigger reads create gang ABDs and fill 4525 * them with respective blocks from writer ABDs. 4526 */ 4527 b = blocks / old_children; 4528 bb = blocks % old_children; 4529 for (uint_t i = 0; i < reads; i++) { 4530 uint_t n = b + (i < bb); 4531 abd_t *abd; 4532 if (n > 1) { 4533 abd = abd_alloc_gang(); 4534 for (uint_t j = 0; j < n; j++) { 4535 uint_t b = j * old_children + i; 4536 abd_t *cabd = abd_get_offset_size( 4537 rra->rra_zio[b % vd->vdev_children]->io_abd, 4538 (b / vd->vdev_children) << ashift, 4539 1 << ashift); 4540 abd_gang_add(abd, cabd, B_TRUE); 4541 } 4542 } else { 4543 abd = rra->rra_zio[i]->io_abd; 4544 } 4545 zio_nowait(zio_vdev_child_io(pio, NULL, 4546 vd->vdev_child[(blkid + i) % old_children], 4547 ((blkid + i) / old_children) << ashift, abd, 4548 n << ashift, ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL, 4549 ZIO_FLAG_CANFAIL, raidz_reflow_read_done, rra)); 4550 } 4551 4552 return (B_FALSE); 4553 } 4554 4555 /* 4556 * For testing (ztest specific) 4557 */ 4558 static void 4559 raidz_expand_pause(uint_t pause_point) 4560 { 4561 while (raidz_expand_pause_point != 0 && 4562 raidz_expand_pause_point <= pause_point) 4563 delay(hz); 4564 } 4565 4566 static void 4567 raidz_scratch_child_done(zio_t *zio) 4568 { 4569 zio_t *pio = zio->io_private; 4570 4571 mutex_enter(&pio->io_lock); 4572 pio->io_error = zio_worst_error(pio->io_error, zio->io_error); 4573 mutex_exit(&pio->io_lock); 4574 } 4575 4576 /* 4577 * Reflow the beginning portion of the vdev into an intermediate scratch area 4578 * in memory and on disk. This operation must be persisted on disk before we 4579 * proceed to overwrite the beginning portion with the reflowed data. 4580 * 4581 * This multi-step task can fail to complete if disk errors are encountered 4582 * and we can return here after a pause (waiting for disk to become healthy). 4583 */ 4584 static void 4585 raidz_reflow_scratch_sync(void *arg, dmu_tx_t *tx) 4586 { 4587 vdev_raidz_expand_t *vre = arg; 4588 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4589 zio_t *pio; 4590 int error; 4591 4592 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4593 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id); 4594 int ashift = raidvd->vdev_ashift; 4595 uint64_t write_size = P2ALIGN_TYPED(VDEV_BOOT_SIZE, 1 << ashift, 4596 uint64_t); 4597 uint64_t logical_size = write_size * raidvd->vdev_children; 4598 uint64_t read_size = 4599 P2ROUNDUP(DIV_ROUND_UP(logical_size, (raidvd->vdev_children - 1)), 4600 1 << ashift); 4601 4602 /* 4603 * The scratch space must be large enough to get us to the point 4604 * that one row does not overlap itself when moved. This is checked 4605 * by vdev_raidz_attach_check(). 4606 */ 4607 VERIFY3U(write_size, >=, raidvd->vdev_children << ashift); 4608 VERIFY3U(write_size, <=, VDEV_BOOT_SIZE); 4609 VERIFY3U(write_size, <=, read_size); 4610 4611 zfs_locked_range_t *lr = zfs_rangelock_enter(&vre->vre_rangelock, 4612 0, logical_size, RL_WRITER); 4613 4614 abd_t **abds = kmem_alloc(raidvd->vdev_children * sizeof (abd_t *), 4615 KM_SLEEP); 4616 for (int i = 0; i < raidvd->vdev_children; i++) { 4617 abds[i] = abd_alloc_linear(read_size, B_FALSE); 4618 } 4619 4620 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_PRE_SCRATCH_1); 4621 4622 /* 4623 * If we have already written the scratch area then we must read from 4624 * there, since new writes were redirected there while we were paused 4625 * or the original location may have been partially overwritten with 4626 * reflowed data. 4627 */ 4628 if (RRSS_GET_STATE(&spa->spa_ubsync) == RRSS_SCRATCH_VALID) { 4629 VERIFY3U(RRSS_GET_OFFSET(&spa->spa_ubsync), ==, logical_size); 4630 /* 4631 * Read from scratch space. 4632 */ 4633 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 4634 for (int i = 0; i < raidvd->vdev_children; i++) { 4635 /* 4636 * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE 4637 * to the offset to calculate the physical offset to 4638 * write to. Passing in a negative offset makes us 4639 * access the scratch area. 4640 */ 4641 zio_nowait(zio_vdev_child_io(pio, NULL, 4642 raidvd->vdev_child[i], 4643 VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abds[i], 4644 write_size, ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL, 4645 ZIO_FLAG_CANFAIL, raidz_scratch_child_done, pio)); 4646 } 4647 error = zio_wait(pio); 4648 if (error != 0) { 4649 zfs_dbgmsg("reflow: error %d reading scratch location", 4650 error); 4651 goto io_error_exit; 4652 } 4653 goto overwrite; 4654 } 4655 4656 /* 4657 * Read from original location. 4658 */ 4659 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 4660 for (int i = 0; i < raidvd->vdev_children - 1; i++) { 4661 ASSERT0(vdev_is_dead(raidvd->vdev_child[i])); 4662 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i], 4663 0, abds[i], read_size, ZIO_TYPE_READ, 4664 ZIO_PRIORITY_REMOVAL, ZIO_FLAG_CANFAIL, 4665 raidz_scratch_child_done, pio)); 4666 } 4667 error = zio_wait(pio); 4668 if (error != 0) { 4669 zfs_dbgmsg("reflow: error %d reading original location", error); 4670 io_error_exit: 4671 for (int i = 0; i < raidvd->vdev_children; i++) 4672 abd_free(abds[i]); 4673 kmem_free(abds, raidvd->vdev_children * sizeof (abd_t *)); 4674 zfs_rangelock_exit(lr); 4675 spa_config_exit(spa, SCL_STATE, FTAG); 4676 return; 4677 } 4678 4679 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_PRE_SCRATCH_2); 4680 4681 /* 4682 * Reflow in memory. 4683 */ 4684 uint64_t logical_sectors = logical_size >> ashift; 4685 for (int i = raidvd->vdev_children - 1; i < logical_sectors; i++) { 4686 int oldchild = i % (raidvd->vdev_children - 1); 4687 uint64_t oldoff = (i / (raidvd->vdev_children - 1)) << ashift; 4688 4689 int newchild = i % raidvd->vdev_children; 4690 uint64_t newoff = (i / raidvd->vdev_children) << ashift; 4691 4692 /* a single sector should not be copying over itself */ 4693 ASSERT(!(newchild == oldchild && newoff == oldoff)); 4694 4695 abd_copy_off(abds[newchild], abds[oldchild], 4696 newoff, oldoff, 1 << ashift); 4697 } 4698 4699 /* 4700 * Verify that we filled in everything we intended to (write_size on 4701 * each child). 4702 */ 4703 VERIFY0(logical_sectors % raidvd->vdev_children); 4704 VERIFY3U((logical_sectors / raidvd->vdev_children) << ashift, ==, 4705 write_size); 4706 4707 /* 4708 * Write to scratch location (boot area). 4709 */ 4710 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 4711 for (int i = 0; i < raidvd->vdev_children; i++) { 4712 /* 4713 * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE to 4714 * the offset to calculate the physical offset to write to. 4715 * Passing in a negative offset lets us access the boot area. 4716 */ 4717 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i], 4718 VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abds[i], 4719 write_size, ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL, 4720 ZIO_FLAG_CANFAIL, raidz_scratch_child_done, pio)); 4721 } 4722 error = zio_wait(pio); 4723 if (error != 0) { 4724 zfs_dbgmsg("reflow: error %d writing scratch location", error); 4725 goto io_error_exit; 4726 } 4727 pio = zio_root(spa, NULL, NULL, 0); 4728 zio_flush(pio, raidvd); 4729 zio_wait(pio); 4730 4731 zfs_dbgmsg("reflow: wrote %llu bytes (logical) to scratch area", 4732 (long long)logical_size); 4733 4734 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_PRE_SCRATCH_3); 4735 4736 /* 4737 * Update uberblock to indicate that scratch space is valid. This is 4738 * needed because after this point, the real location may be 4739 * overwritten. If we crash, we need to get the data from the 4740 * scratch space, rather than the real location. 4741 * 4742 * Note: ub_timestamp is bumped so that vdev_uberblock_compare() 4743 * will prefer this uberblock. 4744 */ 4745 RAIDZ_REFLOW_SET(&spa->spa_ubsync, RRSS_SCRATCH_VALID, logical_size); 4746 spa->spa_ubsync.ub_timestamp++; 4747 ASSERT0(vdev_uberblock_sync_list(spa, &spa->spa_root_vdev, 1, 4748 &spa->spa_ubsync, ZIO_FLAG_CONFIG_WRITER)); 4749 if (spa_multihost(spa)) 4750 mmp_update_uberblock(spa, &spa->spa_ubsync); 4751 4752 zfs_dbgmsg("reflow: uberblock updated " 4753 "(txg %llu, SCRATCH_VALID, size %llu, ts %llu)", 4754 (long long)spa->spa_ubsync.ub_txg, 4755 (long long)logical_size, 4756 (long long)spa->spa_ubsync.ub_timestamp); 4757 4758 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_VALID); 4759 4760 /* 4761 * Overwrite with reflow'ed data. 4762 */ 4763 overwrite: 4764 pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 4765 for (int i = 0; i < raidvd->vdev_children; i++) { 4766 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i], 4767 0, abds[i], write_size, ZIO_TYPE_WRITE, 4768 ZIO_PRIORITY_REMOVAL, ZIO_FLAG_CANFAIL, 4769 raidz_scratch_child_done, pio)); 4770 } 4771 error = zio_wait(pio); 4772 if (error != 0) { 4773 /* 4774 * When we exit early here and drop the range lock, new 4775 * writes will go into the scratch area so we'll need to 4776 * read from there when we return after pausing. 4777 */ 4778 zfs_dbgmsg("reflow: error %d writing real location", error); 4779 /* 4780 * Update the uberblock that is written when this txg completes. 4781 */ 4782 RAIDZ_REFLOW_SET(&spa->spa_uberblock, RRSS_SCRATCH_VALID, 4783 logical_size); 4784 goto io_error_exit; 4785 } 4786 pio = zio_root(spa, NULL, NULL, 0); 4787 zio_flush(pio, raidvd); 4788 zio_wait(pio); 4789 4790 zfs_dbgmsg("reflow: overwrote %llu bytes (logical) to real location", 4791 (long long)logical_size); 4792 for (int i = 0; i < raidvd->vdev_children; i++) 4793 abd_free(abds[i]); 4794 kmem_free(abds, raidvd->vdev_children * sizeof (abd_t *)); 4795 4796 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_REFLOWED); 4797 4798 /* 4799 * Update uberblock to indicate that the initial part has been 4800 * reflow'ed. This is needed because after this point (when we exit 4801 * the rangelock), we allow regular writes to this region, which will 4802 * be written to the new location only (because reflow_offset_next == 4803 * reflow_offset_synced). If we crashed and re-copied from the 4804 * scratch space, we would lose the regular writes. 4805 */ 4806 RAIDZ_REFLOW_SET(&spa->spa_ubsync, RRSS_SCRATCH_INVALID_SYNCED, 4807 logical_size); 4808 spa->spa_ubsync.ub_timestamp++; 4809 ASSERT0(vdev_uberblock_sync_list(spa, &spa->spa_root_vdev, 1, 4810 &spa->spa_ubsync, ZIO_FLAG_CONFIG_WRITER)); 4811 if (spa_multihost(spa)) 4812 mmp_update_uberblock(spa, &spa->spa_ubsync); 4813 4814 zfs_dbgmsg("reflow: uberblock updated " 4815 "(txg %llu, SCRATCH_NOT_IN_USE, size %llu, ts %llu)", 4816 (long long)spa->spa_ubsync.ub_txg, 4817 (long long)logical_size, 4818 (long long)spa->spa_ubsync.ub_timestamp); 4819 4820 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_POST_REFLOW_1); 4821 4822 /* 4823 * Update progress. 4824 */ 4825 vre->vre_offset = logical_size; 4826 zfs_rangelock_exit(lr); 4827 spa_config_exit(spa, SCL_STATE, FTAG); 4828 4829 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; 4830 vre->vre_offset_pertxg[txgoff] = vre->vre_offset; 4831 vre->vre_bytes_copied_pertxg[txgoff] = vre->vre_bytes_copied; 4832 /* 4833 * Note - raidz_reflow_sync() will update the uberblock state to 4834 * RRSS_SCRATCH_INVALID_SYNCED_REFLOW 4835 */ 4836 raidz_reflow_sync(spa, tx); 4837 4838 raidz_expand_pause(RAIDZ_EXPAND_PAUSE_SCRATCH_POST_REFLOW_2); 4839 } 4840 4841 /* 4842 * We crashed in the middle of raidz_reflow_scratch_sync(); complete its work 4843 * here. No other i/o can be in progress, so we don't need the vre_rangelock. 4844 */ 4845 void 4846 vdev_raidz_reflow_copy_scratch(spa_t *spa) 4847 { 4848 vdev_raidz_expand_t *vre = spa->spa_raidz_expand; 4849 uint64_t logical_size = RRSS_GET_OFFSET(&spa->spa_uberblock); 4850 ASSERT3U(RRSS_GET_STATE(&spa->spa_uberblock), ==, RRSS_SCRATCH_VALID); 4851 4852 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4853 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id); 4854 ASSERT0(logical_size % raidvd->vdev_children); 4855 uint64_t write_size = logical_size / raidvd->vdev_children; 4856 4857 zio_t *pio; 4858 4859 /* 4860 * Read from scratch space. 4861 */ 4862 abd_t **abds = kmem_alloc(raidvd->vdev_children * sizeof (abd_t *), 4863 KM_SLEEP); 4864 for (int i = 0; i < raidvd->vdev_children; i++) { 4865 abds[i] = abd_alloc_linear(write_size, B_FALSE); 4866 } 4867 4868 pio = zio_root(spa, NULL, NULL, 0); 4869 for (int i = 0; i < raidvd->vdev_children; i++) { 4870 /* 4871 * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE to 4872 * the offset to calculate the physical offset to write to. 4873 * Passing in a negative offset lets us access the boot area. 4874 */ 4875 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i], 4876 VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abds[i], 4877 write_size, ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL, 0, 4878 raidz_scratch_child_done, pio)); 4879 } 4880 zio_wait(pio); 4881 4882 /* 4883 * Overwrite real location with reflow'ed data. 4884 */ 4885 pio = zio_root(spa, NULL, NULL, 0); 4886 for (int i = 0; i < raidvd->vdev_children; i++) { 4887 zio_nowait(zio_vdev_child_io(pio, NULL, raidvd->vdev_child[i], 4888 0, abds[i], write_size, ZIO_TYPE_WRITE, 4889 ZIO_PRIORITY_REMOVAL, 0, 4890 raidz_scratch_child_done, pio)); 4891 } 4892 zio_wait(pio); 4893 pio = zio_root(spa, NULL, NULL, 0); 4894 zio_flush(pio, raidvd); 4895 zio_wait(pio); 4896 4897 zfs_dbgmsg("reflow recovery: overwrote %llu bytes (logical) " 4898 "to real location", (long long)logical_size); 4899 4900 for (int i = 0; i < raidvd->vdev_children; i++) 4901 abd_free(abds[i]); 4902 kmem_free(abds, raidvd->vdev_children * sizeof (abd_t *)); 4903 4904 /* 4905 * Update uberblock. 4906 */ 4907 RAIDZ_REFLOW_SET(&spa->spa_ubsync, 4908 RRSS_SCRATCH_INVALID_SYNCED_ON_IMPORT, logical_size); 4909 spa->spa_ubsync.ub_timestamp++; 4910 VERIFY0(vdev_uberblock_sync_list(spa, &spa->spa_root_vdev, 1, 4911 &spa->spa_ubsync, ZIO_FLAG_CONFIG_WRITER)); 4912 if (spa_multihost(spa)) 4913 mmp_update_uberblock(spa, &spa->spa_ubsync); 4914 4915 zfs_dbgmsg("reflow recovery: uberblock updated " 4916 "(txg %llu, SCRATCH_NOT_IN_USE, size %llu, ts %llu)", 4917 (long long)spa->spa_ubsync.ub_txg, 4918 (long long)logical_size, 4919 (long long)spa->spa_ubsync.ub_timestamp); 4920 4921 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, 4922 spa_first_txg(spa)); 4923 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; 4924 vre->vre_offset = logical_size; 4925 vre->vre_offset_pertxg[txgoff] = vre->vre_offset; 4926 vre->vre_bytes_copied_pertxg[txgoff] = vre->vre_bytes_copied; 4927 /* 4928 * Note that raidz_reflow_sync() will update the uberblock once more 4929 */ 4930 raidz_reflow_sync(spa, tx); 4931 4932 dmu_tx_commit(tx); 4933 4934 spa_config_exit(spa, SCL_STATE, FTAG); 4935 } 4936 4937 static boolean_t 4938 spa_raidz_expand_thread_check(void *arg, zthr_t *zthr) 4939 { 4940 (void) zthr; 4941 spa_t *spa = arg; 4942 4943 return (spa->spa_raidz_expand != NULL && 4944 !spa->spa_raidz_expand->vre_waiting_for_resilver); 4945 } 4946 4947 /* 4948 * RAIDZ expansion background thread 4949 * 4950 * Can be called multiple times if the reflow is paused 4951 */ 4952 static void 4953 spa_raidz_expand_thread(void *arg, zthr_t *zthr) 4954 { 4955 spa_t *spa = arg; 4956 vdev_raidz_expand_t *vre = spa->spa_raidz_expand; 4957 4958 if (RRSS_GET_STATE(&spa->spa_ubsync) == RRSS_SCRATCH_VALID) 4959 vre->vre_offset = 0; 4960 else 4961 vre->vre_offset = RRSS_GET_OFFSET(&spa->spa_ubsync); 4962 4963 /* Reflow the beginning portion using the scratch area */ 4964 if (vre->vre_offset == 0) { 4965 VERIFY0(dsl_sync_task(spa_name(spa), 4966 NULL, raidz_reflow_scratch_sync, 4967 vre, 0, ZFS_SPACE_CHECK_NONE)); 4968 4969 /* if we encountered errors then pause */ 4970 if (vre->vre_offset == 0) { 4971 mutex_enter(&vre->vre_lock); 4972 vre->vre_waiting_for_resilver = B_TRUE; 4973 mutex_exit(&vre->vre_lock); 4974 return; 4975 } 4976 } 4977 4978 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4979 vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id); 4980 4981 uint64_t guid = raidvd->vdev_guid; 4982 4983 /* Iterate over all the remaining metaslabs */ 4984 for (uint64_t i = vre->vre_offset >> raidvd->vdev_ms_shift; 4985 i < raidvd->vdev_ms_count && 4986 !zthr_iscancelled(zthr) && 4987 vre->vre_failed_offset == UINT64_MAX; i++) { 4988 metaslab_t *msp = raidvd->vdev_ms[i]; 4989 4990 metaslab_disable(msp); 4991 mutex_enter(&msp->ms_lock); 4992 4993 /* 4994 * The metaslab may be newly created (for the expanded 4995 * space), in which case its trees won't exist yet, 4996 * so we need to bail out early. 4997 */ 4998 if (msp->ms_new) { 4999 mutex_exit(&msp->ms_lock); 5000 metaslab_enable(msp, B_FALSE, B_FALSE); 5001 continue; 5002 } 5003 5004 VERIFY0(metaslab_load(msp)); 5005 5006 /* 5007 * We want to copy everything except the free (allocatable) 5008 * space. Note that there may be a little bit more free 5009 * space (e.g. in ms_defer), and it's fine to copy that too. 5010 */ 5011 uint64_t shift, start; 5012 zfs_range_seg_type_t type = metaslab_calculate_range_tree_type( 5013 raidvd, msp, &start, &shift); 5014 zfs_range_tree_t *rt = zfs_range_tree_create_flags( 5015 NULL, type, NULL, start, shift, ZFS_RT_F_DYN_NAME, 5016 metaslab_rt_name(msp->ms_group, msp, 5017 "spa_raidz_expand_thread:rt")); 5018 zfs_range_tree_add(rt, msp->ms_start, msp->ms_size); 5019 zfs_range_tree_walk(msp->ms_allocatable, zfs_range_tree_remove, 5020 rt); 5021 mutex_exit(&msp->ms_lock); 5022 5023 /* 5024 * Force the last sector of each metaslab to be copied. This 5025 * ensures that we advance the on-disk progress to the end of 5026 * this metaslab while the metaslab is disabled. Otherwise, we 5027 * could move past this metaslab without advancing the on-disk 5028 * progress, and then an allocation to this metaslab would not 5029 * be copied. 5030 */ 5031 int sectorsz = 1 << raidvd->vdev_ashift; 5032 uint64_t ms_last_offset = msp->ms_start + 5033 msp->ms_size - sectorsz; 5034 if (!zfs_range_tree_contains(rt, ms_last_offset, sectorsz)) { 5035 zfs_range_tree_add(rt, ms_last_offset, sectorsz); 5036 } 5037 5038 /* 5039 * When we are resuming from a paused expansion (i.e. 5040 * when importing a pool with a expansion in progress), 5041 * discard any state that we have already processed. 5042 */ 5043 if (vre->vre_offset > msp->ms_start) { 5044 zfs_range_tree_clear(rt, msp->ms_start, 5045 vre->vre_offset - msp->ms_start); 5046 } 5047 5048 while (!zthr_iscancelled(zthr) && 5049 !zfs_range_tree_is_empty(rt) && 5050 vre->vre_failed_offset == UINT64_MAX) { 5051 5052 /* 5053 * We need to periodically drop the config lock so that 5054 * writers can get in. Additionally, we can't wait 5055 * for a txg to sync while holding a config lock 5056 * (since a waiting writer could cause a 3-way deadlock 5057 * with the sync thread, which also gets a config 5058 * lock for reader). So we can't hold the config lock 5059 * while calling dmu_tx_assign(). 5060 */ 5061 spa_config_exit(spa, SCL_CONFIG, FTAG); 5062 5063 /* 5064 * If requested, pause the reflow when the amount 5065 * specified by raidz_expand_max_reflow_bytes is reached 5066 * 5067 * This pause is only used during testing or debugging. 5068 */ 5069 while (raidz_expand_max_reflow_bytes != 0 && 5070 raidz_expand_max_reflow_bytes <= 5071 vre->vre_bytes_copied && !zthr_iscancelled(zthr)) { 5072 delay(hz); 5073 } 5074 5075 mutex_enter(&vre->vre_lock); 5076 while (vre->vre_outstanding_bytes > 5077 raidz_expand_max_copy_bytes) { 5078 cv_wait(&vre->vre_cv, &vre->vre_lock); 5079 } 5080 mutex_exit(&vre->vre_lock); 5081 5082 dmu_tx_t *tx = 5083 dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 5084 5085 VERIFY0(dmu_tx_assign(tx, 5086 DMU_TX_WAIT | DMU_TX_SUSPEND)); 5087 uint64_t txg = dmu_tx_get_txg(tx); 5088 5089 /* 5090 * Reacquire the vdev_config lock. Theoretically, the 5091 * vdev_t that we're expanding may have changed. 5092 */ 5093 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5094 raidvd = vdev_lookup_top(spa, vre->vre_vdev_id); 5095 5096 boolean_t needsync = 5097 raidz_reflow_impl(raidvd, vre, rt, tx); 5098 5099 dmu_tx_commit(tx); 5100 5101 if (needsync) { 5102 spa_config_exit(spa, SCL_CONFIG, FTAG); 5103 txg_wait_synced(spa->spa_dsl_pool, txg); 5104 spa_config_enter(spa, SCL_CONFIG, FTAG, 5105 RW_READER); 5106 } 5107 } 5108 5109 spa_config_exit(spa, SCL_CONFIG, FTAG); 5110 5111 metaslab_enable(msp, B_FALSE, B_FALSE); 5112 zfs_range_tree_vacate(rt, NULL, NULL); 5113 zfs_range_tree_destroy(rt); 5114 5115 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5116 raidvd = vdev_lookup_top(spa, vre->vre_vdev_id); 5117 } 5118 5119 spa_config_exit(spa, SCL_CONFIG, FTAG); 5120 5121 /* 5122 * The txg_wait_synced() here ensures that all reflow zio's have 5123 * completed, and vre_failed_offset has been set if necessary. It 5124 * also ensures that the progress of the last raidz_reflow_sync() is 5125 * written to disk before raidz_reflow_complete_sync() changes the 5126 * in-memory vre_state. vdev_raidz_io_start() uses vre_state to 5127 * determine if a reflow is in progress, in which case we may need to 5128 * write to both old and new locations. Therefore we can only change 5129 * vre_state once this is not necessary, which is once the on-disk 5130 * progress (in spa_ubsync) has been set past any possible writes (to 5131 * the end of the last metaslab). 5132 */ 5133 txg_wait_synced(spa->spa_dsl_pool, 0); 5134 5135 if (!zthr_iscancelled(zthr) && 5136 vre->vre_offset == raidvd->vdev_ms_count << raidvd->vdev_ms_shift) { 5137 /* 5138 * We are not being canceled or paused, so the reflow must be 5139 * complete. In that case also mark it as completed on disk. 5140 */ 5141 ASSERT3U(vre->vre_failed_offset, ==, UINT64_MAX); 5142 VERIFY0(dsl_sync_task(spa_name(spa), NULL, 5143 raidz_reflow_complete_sync, spa, 5144 0, ZFS_SPACE_CHECK_NONE)); 5145 (void) vdev_online(spa, guid, ZFS_ONLINE_EXPAND, NULL); 5146 } else { 5147 /* 5148 * Wait for all copy zio's to complete and for all the 5149 * raidz_reflow_sync() synctasks to be run. 5150 */ 5151 spa_history_log_internal(spa, "reflow pause", 5152 NULL, "offset=%llu failed_offset=%lld", 5153 (long long)vre->vre_offset, 5154 (long long)vre->vre_failed_offset); 5155 mutex_enter(&vre->vre_lock); 5156 if (vre->vre_failed_offset != UINT64_MAX) { 5157 /* 5158 * Reset progress so that we will retry everything 5159 * after the point that something failed. 5160 */ 5161 vre->vre_offset = vre->vre_failed_offset; 5162 vre->vre_failed_offset = UINT64_MAX; 5163 vre->vre_waiting_for_resilver = B_TRUE; 5164 } 5165 mutex_exit(&vre->vre_lock); 5166 } 5167 } 5168 5169 void 5170 spa_start_raidz_expansion_thread(spa_t *spa) 5171 { 5172 ASSERT0P(spa->spa_raidz_expand_zthr); 5173 spa->spa_raidz_expand_zthr = zthr_create("raidz_expand", 5174 spa_raidz_expand_thread_check, spa_raidz_expand_thread, 5175 spa, defclsyspri); 5176 } 5177 5178 void 5179 raidz_dtl_reassessed(vdev_t *vd) 5180 { 5181 spa_t *spa = vd->vdev_spa; 5182 if (spa->spa_raidz_expand != NULL) { 5183 vdev_raidz_expand_t *vre = spa->spa_raidz_expand; 5184 /* 5185 * we get called often from vdev_dtl_reassess() so make 5186 * sure it's our vdev and any replacing is complete 5187 */ 5188 if (vd->vdev_top->vdev_id == vre->vre_vdev_id && 5189 !vdev_raidz_expand_child_replacing(vd->vdev_top)) { 5190 mutex_enter(&vre->vre_lock); 5191 if (vre->vre_waiting_for_resilver) { 5192 vdev_dbgmsg(vd, "DTL reassessed, " 5193 "continuing raidz expansion"); 5194 vre->vre_waiting_for_resilver = B_FALSE; 5195 zthr_wakeup(spa->spa_raidz_expand_zthr); 5196 } 5197 mutex_exit(&vre->vre_lock); 5198 } 5199 } 5200 } 5201 5202 int 5203 vdev_raidz_attach_check(vdev_t *new_child) 5204 { 5205 vdev_t *raidvd = new_child->vdev_parent; 5206 uint64_t new_children = raidvd->vdev_children; 5207 5208 /* 5209 * We use the "boot" space as scratch space to handle overwriting the 5210 * initial part of the vdev. If it is too small, then this expansion 5211 * is not allowed. This would be very unusual (e.g. ashift > 13 and 5212 * >200 children). 5213 */ 5214 if (new_children << raidvd->vdev_ashift > VDEV_BOOT_SIZE) { 5215 return (EINVAL); 5216 } 5217 return (0); 5218 } 5219 5220 void 5221 vdev_raidz_attach_sync(void *arg, dmu_tx_t *tx) 5222 { 5223 vdev_t *new_child = arg; 5224 spa_t *spa = new_child->vdev_spa; 5225 vdev_t *raidvd = new_child->vdev_parent; 5226 vdev_raidz_t *vdrz = raidvd->vdev_tsd; 5227 ASSERT3P(raidvd->vdev_ops, ==, &vdev_raidz_ops); 5228 ASSERT3P(raidvd->vdev_top, ==, raidvd); 5229 ASSERT3U(raidvd->vdev_children, >, vdrz->vd_original_width); 5230 ASSERT3U(raidvd->vdev_children, ==, vdrz->vd_physical_width + 1); 5231 ASSERT3P(raidvd->vdev_child[raidvd->vdev_children - 1], ==, 5232 new_child); 5233 5234 spa_feature_incr(spa, SPA_FEATURE_RAIDZ_EXPANSION, tx); 5235 5236 vdrz->vd_physical_width++; 5237 5238 VERIFY0(spa->spa_uberblock.ub_raidz_reflow_info); 5239 vdrz->vn_vre.vre_vdev_id = raidvd->vdev_id; 5240 vdrz->vn_vre.vre_offset = 0; 5241 vdrz->vn_vre.vre_failed_offset = UINT64_MAX; 5242 spa->spa_raidz_expand = &vdrz->vn_vre; 5243 5244 /* 5245 * Dirty the config so that ZPOOL_CONFIG_RAIDZ_EXPANDING will get 5246 * written to the config. 5247 */ 5248 vdev_config_dirty(raidvd); 5249 5250 vdrz->vn_vre.vre_start_time = gethrestime_sec(); 5251 vdrz->vn_vre.vre_end_time = 0; 5252 vdrz->vn_vre.vre_state = DSS_SCANNING; 5253 vdrz->vn_vre.vre_bytes_copied = 0; 5254 5255 uint64_t state = vdrz->vn_vre.vre_state; 5256 VERIFY0(zap_update(spa->spa_meta_objset, 5257 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE, 5258 sizeof (state), 1, &state, tx)); 5259 5260 uint64_t start_time = vdrz->vn_vre.vre_start_time; 5261 VERIFY0(zap_update(spa->spa_meta_objset, 5262 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_START_TIME, 5263 sizeof (start_time), 1, &start_time, tx)); 5264 5265 (void) zap_remove(spa->spa_meta_objset, 5266 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME, tx); 5267 (void) zap_remove(spa->spa_meta_objset, 5268 raidvd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED, tx); 5269 5270 spa_history_log_internal(spa, "raidz vdev expansion started", tx, 5271 "%s vdev %llu new width %llu", spa_name(spa), 5272 (unsigned long long)raidvd->vdev_id, 5273 (unsigned long long)raidvd->vdev_children); 5274 } 5275 5276 int 5277 vdev_raidz_load(vdev_t *vd) 5278 { 5279 vdev_raidz_t *vdrz = vd->vdev_tsd; 5280 int err; 5281 5282 uint64_t state = DSS_NONE; 5283 uint64_t start_time = 0; 5284 uint64_t end_time = 0; 5285 uint64_t bytes_copied = 0; 5286 5287 if (vd->vdev_top_zap != 0) { 5288 err = zap_lookup(vd->vdev_spa->spa_meta_objset, 5289 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_STATE, 5290 sizeof (state), 1, &state); 5291 if (err != 0 && err != ENOENT) 5292 return (err); 5293 5294 err = zap_lookup(vd->vdev_spa->spa_meta_objset, 5295 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_START_TIME, 5296 sizeof (start_time), 1, &start_time); 5297 if (err != 0 && err != ENOENT) 5298 return (err); 5299 5300 err = zap_lookup(vd->vdev_spa->spa_meta_objset, 5301 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_END_TIME, 5302 sizeof (end_time), 1, &end_time); 5303 if (err != 0 && err != ENOENT) 5304 return (err); 5305 5306 err = zap_lookup(vd->vdev_spa->spa_meta_objset, 5307 vd->vdev_top_zap, VDEV_TOP_ZAP_RAIDZ_EXPAND_BYTES_COPIED, 5308 sizeof (bytes_copied), 1, &bytes_copied); 5309 if (err != 0 && err != ENOENT) 5310 return (err); 5311 } 5312 5313 /* 5314 * If we are in the middle of expansion, vre_state should have 5315 * already been set by vdev_raidz_init(). 5316 */ 5317 EQUIV(vdrz->vn_vre.vre_state == DSS_SCANNING, state == DSS_SCANNING); 5318 vdrz->vn_vre.vre_state = (dsl_scan_state_t)state; 5319 vdrz->vn_vre.vre_start_time = start_time; 5320 vdrz->vn_vre.vre_end_time = end_time; 5321 vdrz->vn_vre.vre_bytes_copied = bytes_copied; 5322 5323 return (0); 5324 } 5325 5326 int 5327 spa_raidz_expand_get_stats(spa_t *spa, pool_raidz_expand_stat_t *pres) 5328 { 5329 vdev_raidz_expand_t *vre = spa->spa_raidz_expand; 5330 5331 if (vre == NULL) { 5332 /* no removal in progress; find most recent completed */ 5333 for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { 5334 vdev_t *vd = spa->spa_root_vdev->vdev_child[c]; 5335 if (vd->vdev_ops == &vdev_raidz_ops) { 5336 vdev_raidz_t *vdrz = vd->vdev_tsd; 5337 5338 if (vdrz->vn_vre.vre_end_time != 0 && 5339 (vre == NULL || 5340 vdrz->vn_vre.vre_end_time > 5341 vre->vre_end_time)) { 5342 vre = &vdrz->vn_vre; 5343 } 5344 } 5345 } 5346 } 5347 5348 if (vre == NULL) { 5349 return (SET_ERROR(ENOENT)); 5350 } 5351 5352 pres->pres_state = vre->vre_state; 5353 pres->pres_expanding_vdev = vre->vre_vdev_id; 5354 5355 vdev_t *vd = vdev_lookup_top(spa, vre->vre_vdev_id); 5356 pres->pres_to_reflow = vd->vdev_stat.vs_alloc; 5357 5358 mutex_enter(&vre->vre_lock); 5359 pres->pres_reflowed = vre->vre_bytes_copied; 5360 for (int i = 0; i < TXG_SIZE; i++) 5361 pres->pres_reflowed += vre->vre_bytes_copied_pertxg[i]; 5362 mutex_exit(&vre->vre_lock); 5363 5364 pres->pres_start_time = vre->vre_start_time; 5365 pres->pres_end_time = vre->vre_end_time; 5366 pres->pres_waiting_for_resilver = vre->vre_waiting_for_resilver; 5367 5368 return (0); 5369 } 5370 5371 /* 5372 * Initialize private RAIDZ specific fields from the nvlist. 5373 */ 5374 static int 5375 vdev_raidz_init(spa_t *spa, nvlist_t *nv, void **tsd) 5376 { 5377 uint_t children; 5378 nvlist_t **child; 5379 int error = nvlist_lookup_nvlist_array(nv, 5380 ZPOOL_CONFIG_CHILDREN, &child, &children); 5381 if (error != 0) 5382 return (SET_ERROR(EINVAL)); 5383 5384 uint64_t nparity; 5385 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, &nparity) == 0) { 5386 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 5387 return (SET_ERROR(EINVAL)); 5388 5389 /* 5390 * Previous versions could only support 1 or 2 parity 5391 * device. 5392 */ 5393 if (nparity > 1 && spa_version(spa) < SPA_VERSION_RAIDZ2) 5394 return (SET_ERROR(EINVAL)); 5395 else if (nparity > 2 && spa_version(spa) < SPA_VERSION_RAIDZ3) 5396 return (SET_ERROR(EINVAL)); 5397 } else { 5398 /* 5399 * We require the parity to be specified for SPAs that 5400 * support multiple parity levels. 5401 */ 5402 if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 5403 return (SET_ERROR(EINVAL)); 5404 5405 /* 5406 * Otherwise, we default to 1 parity device for RAID-Z. 5407 */ 5408 nparity = 1; 5409 } 5410 5411 vdev_raidz_t *vdrz = kmem_zalloc(sizeof (*vdrz), KM_SLEEP); 5412 vdrz->vn_vre.vre_vdev_id = -1; 5413 vdrz->vn_vre.vre_offset = UINT64_MAX; 5414 vdrz->vn_vre.vre_failed_offset = UINT64_MAX; 5415 mutex_init(&vdrz->vn_vre.vre_lock, NULL, MUTEX_DEFAULT, NULL); 5416 cv_init(&vdrz->vn_vre.vre_cv, NULL, CV_DEFAULT, NULL); 5417 zfs_rangelock_init(&vdrz->vn_vre.vre_rangelock, NULL, NULL); 5418 mutex_init(&vdrz->vd_expand_lock, NULL, MUTEX_DEFAULT, NULL); 5419 avl_create(&vdrz->vd_expand_txgs, vdev_raidz_reflow_compare, 5420 sizeof (reflow_node_t), offsetof(reflow_node_t, re_link)); 5421 5422 vdrz->vd_physical_width = children; 5423 vdrz->vd_nparity = nparity; 5424 5425 /* note, the ID does not exist when creating a pool */ 5426 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 5427 &vdrz->vn_vre.vre_vdev_id); 5428 5429 boolean_t reflow_in_progress = 5430 nvlist_exists(nv, ZPOOL_CONFIG_RAIDZ_EXPANDING); 5431 if (reflow_in_progress) { 5432 spa->spa_raidz_expand = &vdrz->vn_vre; 5433 vdrz->vn_vre.vre_state = DSS_SCANNING; 5434 } 5435 5436 vdrz->vd_original_width = children; 5437 uint64_t *txgs; 5438 unsigned int txgs_size = 0; 5439 error = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_RAIDZ_EXPAND_TXGS, 5440 &txgs, &txgs_size); 5441 if (error == 0) { 5442 for (int i = 0; i < txgs_size; i++) { 5443 reflow_node_t *re = kmem_zalloc(sizeof (*re), KM_SLEEP); 5444 re->re_txg = txgs[txgs_size - i - 1]; 5445 re->re_logical_width = vdrz->vd_physical_width - i; 5446 5447 if (reflow_in_progress) 5448 re->re_logical_width--; 5449 5450 avl_add(&vdrz->vd_expand_txgs, re); 5451 } 5452 5453 vdrz->vd_original_width = vdrz->vd_physical_width - txgs_size; 5454 } 5455 if (reflow_in_progress) { 5456 vdrz->vd_original_width--; 5457 zfs_dbgmsg("reflow_in_progress, %u wide, %d prior expansions", 5458 children, txgs_size); 5459 } 5460 5461 *tsd = vdrz; 5462 5463 return (0); 5464 } 5465 5466 static void 5467 vdev_raidz_fini(vdev_t *vd) 5468 { 5469 vdev_raidz_t *vdrz = vd->vdev_tsd; 5470 if (vd->vdev_spa->spa_raidz_expand == &vdrz->vn_vre) 5471 vd->vdev_spa->spa_raidz_expand = NULL; 5472 reflow_node_t *re; 5473 void *cookie = NULL; 5474 avl_tree_t *tree = &vdrz->vd_expand_txgs; 5475 while ((re = avl_destroy_nodes(tree, &cookie)) != NULL) 5476 kmem_free(re, sizeof (*re)); 5477 avl_destroy(&vdrz->vd_expand_txgs); 5478 mutex_destroy(&vdrz->vd_expand_lock); 5479 mutex_destroy(&vdrz->vn_vre.vre_lock); 5480 cv_destroy(&vdrz->vn_vre.vre_cv); 5481 zfs_rangelock_fini(&vdrz->vn_vre.vre_rangelock); 5482 kmem_free(vdrz, sizeof (*vdrz)); 5483 } 5484 5485 /* 5486 * Add RAIDZ specific fields to the config nvlist. 5487 */ 5488 static void 5489 vdev_raidz_config_generate(vdev_t *vd, nvlist_t *nv) 5490 { 5491 ASSERT3P(vd->vdev_ops, ==, &vdev_raidz_ops); 5492 vdev_raidz_t *vdrz = vd->vdev_tsd; 5493 5494 /* 5495 * Make sure someone hasn't managed to sneak a fancy new vdev 5496 * into a crufty old storage pool. 5497 */ 5498 ASSERT(vdrz->vd_nparity == 1 || 5499 (vdrz->vd_nparity <= 2 && 5500 spa_version(vd->vdev_spa) >= SPA_VERSION_RAIDZ2) || 5501 (vdrz->vd_nparity <= 3 && 5502 spa_version(vd->vdev_spa) >= SPA_VERSION_RAIDZ3)); 5503 5504 /* 5505 * Note that we'll add these even on storage pools where they 5506 * aren't strictly required -- older software will just ignore 5507 * it. 5508 */ 5509 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vdrz->vd_nparity); 5510 5511 if (vdrz->vn_vre.vre_state == DSS_SCANNING) { 5512 fnvlist_add_boolean(nv, ZPOOL_CONFIG_RAIDZ_EXPANDING); 5513 } 5514 5515 mutex_enter(&vdrz->vd_expand_lock); 5516 if (!avl_is_empty(&vdrz->vd_expand_txgs)) { 5517 uint64_t count = avl_numnodes(&vdrz->vd_expand_txgs); 5518 uint64_t *txgs = kmem_alloc(sizeof (uint64_t) * count, 5519 KM_SLEEP); 5520 uint64_t i = 0; 5521 5522 for (reflow_node_t *re = avl_first(&vdrz->vd_expand_txgs); 5523 re != NULL; re = AVL_NEXT(&vdrz->vd_expand_txgs, re)) { 5524 txgs[i++] = re->re_txg; 5525 } 5526 5527 fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_RAIDZ_EXPAND_TXGS, 5528 txgs, count); 5529 5530 kmem_free(txgs, sizeof (uint64_t) * count); 5531 } 5532 mutex_exit(&vdrz->vd_expand_lock); 5533 } 5534 5535 static uint64_t 5536 vdev_raidz_nparity(vdev_t *vd) 5537 { 5538 vdev_raidz_t *vdrz = vd->vdev_tsd; 5539 return (vdrz->vd_nparity); 5540 } 5541 5542 static uint64_t 5543 vdev_raidz_ndisks(vdev_t *vd) 5544 { 5545 return (vd->vdev_children); 5546 } 5547 5548 vdev_ops_t vdev_raidz_ops = { 5549 .vdev_op_init = vdev_raidz_init, 5550 .vdev_op_fini = vdev_raidz_fini, 5551 .vdev_op_open = vdev_raidz_open, 5552 .vdev_op_close = vdev_raidz_close, 5553 .vdev_op_psize_to_asize = vdev_raidz_psize_to_asize, 5554 .vdev_op_asize_to_psize = vdev_raidz_asize_to_psize, 5555 .vdev_op_min_asize = vdev_raidz_min_asize, 5556 .vdev_op_min_alloc = NULL, 5557 .vdev_op_io_start = vdev_raidz_io_start, 5558 .vdev_op_io_done = vdev_raidz_io_done, 5559 .vdev_op_state_change = vdev_raidz_state_change, 5560 .vdev_op_need_resilver = vdev_raidz_need_resilver, 5561 .vdev_op_hold = NULL, 5562 .vdev_op_rele = NULL, 5563 .vdev_op_remap = NULL, 5564 .vdev_op_xlate = vdev_raidz_xlate, 5565 .vdev_op_rebuild_asize = NULL, 5566 .vdev_op_metaslab_init = NULL, 5567 .vdev_op_config_generate = vdev_raidz_config_generate, 5568 .vdev_op_nparity = vdev_raidz_nparity, 5569 .vdev_op_ndisks = vdev_raidz_ndisks, 5570 .vdev_op_type = VDEV_TYPE_RAIDZ, /* name of this vdev type */ 5571 .vdev_op_leaf = B_FALSE /* not a leaf vdev */ 5572 }; 5573 5574 ZFS_MODULE_PARAM(zfs_vdev, raidz_, expand_max_reflow_bytes, ULONG, ZMOD_RW, 5575 "For testing, pause RAIDZ expansion after reflowing this many bytes"); 5576 ZFS_MODULE_PARAM(zfs_vdev, raidz_, expand_max_copy_bytes, ULONG, ZMOD_RW, 5577 "Max amount of concurrent i/o for RAIDZ expansion"); 5578 ZFS_MODULE_PARAM(zfs_vdev, raidz_, io_aggregate_rows, ULONG, ZMOD_RW, 5579 "For expanded RAIDZ, aggregate reads that have more rows than this"); 5580 ZFS_MODULE_PARAM(zfs, zfs_, scrub_after_expand, INT, ZMOD_RW, 5581 "For expanded RAIDZ, automatically start a pool scrub when expansion " 5582 "completes"); 5583 ZFS_MODULE_PARAM(zfs, zfs_, scrub_partial_writes, INT, ZMOD_RW, 5584 "Issue reads after writes with recoverable failures to ensure " 5585 "integrity"); 5586 ZFS_MODULE_PARAM(zfs_vdev, vdev_, read_sit_out_secs, ULONG, ZMOD_RW, 5587 "Raidz/draid slow disk sit out time period in seconds"); 5588 ZFS_MODULE_PARAM(zfs_vdev, vdev_, raidz_outlier_check_interval_ms, U64, 5589 ZMOD_RW, "Interval to check for slow raidz/draid children"); 5590 ZFS_MODULE_PARAM(zfs_vdev, vdev_, raidz_outlier_insensitivity, UINT, 5591 ZMOD_RW, "How insensitive the slow raidz/draid child check should be"); 5592 /* END CSTYLED */ 5593