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