1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy 22eda14cbcSMatt Macy /* 23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 242c48331dSMatt Macy * Copyright (c) 2012, 2020 by Delphix. All rights reserved. 25eda14cbcSMatt Macy * Copyright (c) 2016 Gvozden Nešković. All rights reserved. 26eda14cbcSMatt Macy */ 27eda14cbcSMatt Macy 28eda14cbcSMatt Macy #include <sys/zfs_context.h> 29eda14cbcSMatt Macy #include <sys/spa.h> 30eda14cbcSMatt Macy #include <sys/vdev_impl.h> 31eda14cbcSMatt Macy #include <sys/zio.h> 32eda14cbcSMatt Macy #include <sys/zio_checksum.h> 33eda14cbcSMatt Macy #include <sys/abd.h> 34eda14cbcSMatt Macy #include <sys/fs/zfs.h> 35eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h> 36eda14cbcSMatt Macy #include <sys/vdev_raidz.h> 37eda14cbcSMatt Macy #include <sys/vdev_raidz_impl.h> 387877fdebSMatt Macy #include <sys/vdev_draid.h> 39eda14cbcSMatt Macy 40eda14cbcSMatt Macy #ifdef ZFS_DEBUG 41eda14cbcSMatt Macy #include <sys/vdev.h> /* For vdev_xlate() in vdev_raidz_io_verify() */ 42eda14cbcSMatt Macy #endif 43eda14cbcSMatt Macy 44eda14cbcSMatt Macy /* 45eda14cbcSMatt Macy * Virtual device vector for RAID-Z. 46eda14cbcSMatt Macy * 47eda14cbcSMatt Macy * This vdev supports single, double, and triple parity. For single parity, 48eda14cbcSMatt Macy * we use a simple XOR of all the data columns. For double or triple parity, 49eda14cbcSMatt Macy * we use a special case of Reed-Solomon coding. This extends the 50eda14cbcSMatt Macy * technique described in "The mathematics of RAID-6" by H. Peter Anvin by 51eda14cbcSMatt Macy * drawing on the system described in "A Tutorial on Reed-Solomon Coding for 52eda14cbcSMatt Macy * Fault-Tolerance in RAID-like Systems" by James S. Plank on which the 53eda14cbcSMatt Macy * former is also based. The latter is designed to provide higher performance 54eda14cbcSMatt Macy * for writes. 55eda14cbcSMatt Macy * 56eda14cbcSMatt Macy * Note that the Plank paper claimed to support arbitrary N+M, but was then 57eda14cbcSMatt Macy * amended six years later identifying a critical flaw that invalidates its 58eda14cbcSMatt Macy * claims. Nevertheless, the technique can be adapted to work for up to 59eda14cbcSMatt Macy * triple parity. For additional parity, the amendment "Note: Correction to 60eda14cbcSMatt Macy * the 1997 Tutorial on Reed-Solomon Coding" by James S. Plank and Ying Ding 61eda14cbcSMatt Macy * is viable, but the additional complexity means that write performance will 62eda14cbcSMatt Macy * suffer. 63eda14cbcSMatt Macy * 64eda14cbcSMatt Macy * All of the methods above operate on a Galois field, defined over the 65eda14cbcSMatt Macy * integers mod 2^N. In our case we choose N=8 for GF(8) so that all elements 66eda14cbcSMatt Macy * can be expressed with a single byte. Briefly, the operations on the 67eda14cbcSMatt Macy * field are defined as follows: 68eda14cbcSMatt Macy * 69eda14cbcSMatt Macy * o addition (+) is represented by a bitwise XOR 70eda14cbcSMatt Macy * o subtraction (-) is therefore identical to addition: A + B = A - B 71eda14cbcSMatt Macy * o multiplication of A by 2 is defined by the following bitwise expression: 72eda14cbcSMatt Macy * 73eda14cbcSMatt Macy * (A * 2)_7 = A_6 74eda14cbcSMatt Macy * (A * 2)_6 = A_5 75eda14cbcSMatt Macy * (A * 2)_5 = A_4 76eda14cbcSMatt Macy * (A * 2)_4 = A_3 + A_7 77eda14cbcSMatt Macy * (A * 2)_3 = A_2 + A_7 78eda14cbcSMatt Macy * (A * 2)_2 = A_1 + A_7 79eda14cbcSMatt Macy * (A * 2)_1 = A_0 80eda14cbcSMatt Macy * (A * 2)_0 = A_7 81eda14cbcSMatt Macy * 82eda14cbcSMatt Macy * In C, multiplying by 2 is therefore ((a << 1) ^ ((a & 0x80) ? 0x1d : 0)). 83eda14cbcSMatt Macy * As an aside, this multiplication is derived from the error correcting 84eda14cbcSMatt Macy * primitive polynomial x^8 + x^4 + x^3 + x^2 + 1. 85eda14cbcSMatt Macy * 86eda14cbcSMatt Macy * Observe that any number in the field (except for 0) can be expressed as a 87eda14cbcSMatt Macy * power of 2 -- a generator for the field. We store a table of the powers of 88eda14cbcSMatt Macy * 2 and logs base 2 for quick look ups, and exploit the fact that A * B can 89eda14cbcSMatt Macy * be rewritten as 2^(log_2(A) + log_2(B)) (where '+' is normal addition rather 90eda14cbcSMatt Macy * than field addition). The inverse of a field element A (A^-1) is therefore 91eda14cbcSMatt Macy * A ^ (255 - 1) = A^254. 92eda14cbcSMatt Macy * 93eda14cbcSMatt Macy * The up-to-three parity columns, P, Q, R over several data columns, 94eda14cbcSMatt Macy * D_0, ... D_n-1, can be expressed by field operations: 95eda14cbcSMatt Macy * 96eda14cbcSMatt Macy * P = D_0 + D_1 + ... + D_n-2 + D_n-1 97eda14cbcSMatt Macy * Q = 2^n-1 * D_0 + 2^n-2 * D_1 + ... + 2^1 * D_n-2 + 2^0 * D_n-1 98eda14cbcSMatt Macy * = ((...((D_0) * 2 + D_1) * 2 + ...) * 2 + D_n-2) * 2 + D_n-1 99eda14cbcSMatt Macy * R = 4^n-1 * D_0 + 4^n-2 * D_1 + ... + 4^1 * D_n-2 + 4^0 * D_n-1 100eda14cbcSMatt Macy * = ((...((D_0) * 4 + D_1) * 4 + ...) * 4 + D_n-2) * 4 + D_n-1 101eda14cbcSMatt Macy * 102eda14cbcSMatt Macy * We chose 1, 2, and 4 as our generators because 1 corresponds to the trivial 103eda14cbcSMatt Macy * XOR operation, and 2 and 4 can be computed quickly and generate linearly- 104eda14cbcSMatt Macy * independent coefficients. (There are no additional coefficients that have 105eda14cbcSMatt Macy * this property which is why the uncorrected Plank method breaks down.) 106eda14cbcSMatt Macy * 107eda14cbcSMatt Macy * See the reconstruction code below for how P, Q and R can used individually 108eda14cbcSMatt Macy * or in concert to recover missing data columns. 109eda14cbcSMatt Macy */ 110eda14cbcSMatt Macy 111eda14cbcSMatt Macy #define VDEV_RAIDZ_P 0 112eda14cbcSMatt Macy #define VDEV_RAIDZ_Q 1 113eda14cbcSMatt Macy #define VDEV_RAIDZ_R 2 114eda14cbcSMatt Macy 115eda14cbcSMatt Macy #define VDEV_RAIDZ_MUL_2(x) (((x) << 1) ^ (((x) & 0x80) ? 0x1d : 0)) 116eda14cbcSMatt Macy #define VDEV_RAIDZ_MUL_4(x) (VDEV_RAIDZ_MUL_2(VDEV_RAIDZ_MUL_2(x))) 117eda14cbcSMatt Macy 118eda14cbcSMatt Macy /* 119eda14cbcSMatt Macy * We provide a mechanism to perform the field multiplication operation on a 120eda14cbcSMatt Macy * 64-bit value all at once rather than a byte at a time. This works by 121eda14cbcSMatt Macy * creating a mask from the top bit in each byte and using that to 122eda14cbcSMatt Macy * conditionally apply the XOR of 0x1d. 123eda14cbcSMatt Macy */ 124eda14cbcSMatt Macy #define VDEV_RAIDZ_64MUL_2(x, mask) \ 125eda14cbcSMatt Macy { \ 126eda14cbcSMatt Macy (mask) = (x) & 0x8080808080808080ULL; \ 127eda14cbcSMatt Macy (mask) = ((mask) << 1) - ((mask) >> 7); \ 128eda14cbcSMatt Macy (x) = (((x) << 1) & 0xfefefefefefefefeULL) ^ \ 129eda14cbcSMatt Macy ((mask) & 0x1d1d1d1d1d1d1d1dULL); \ 130eda14cbcSMatt Macy } 131eda14cbcSMatt Macy 132eda14cbcSMatt Macy #define VDEV_RAIDZ_64MUL_4(x, mask) \ 133eda14cbcSMatt Macy { \ 134eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2((x), mask); \ 135eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2((x), mask); \ 136eda14cbcSMatt Macy } 137eda14cbcSMatt Macy 1387877fdebSMatt Macy static void 1397877fdebSMatt Macy vdev_raidz_row_free(raidz_row_t *rr) 140eda14cbcSMatt Macy { 141184c1b94SMartin Matuska for (int c = 0; c < rr->rr_cols; c++) { 142184c1b94SMartin Matuska raidz_col_t *rc = &rr->rr_col[c]; 143eda14cbcSMatt Macy 144184c1b94SMartin Matuska if (rc->rc_size != 0) 145184c1b94SMartin Matuska abd_free(rc->rc_abd); 146184c1b94SMartin Matuska if (rc->rc_gdata != NULL) 147184c1b94SMartin Matuska abd_free(rc->rc_gdata); 148184c1b94SMartin Matuska if (rc->rc_orig_data != NULL) 149184c1b94SMartin Matuska zio_buf_free(rc->rc_orig_data, rc->rc_size); 150eda14cbcSMatt Macy } 151eda14cbcSMatt Macy 1527877fdebSMatt Macy if (rr->rr_abd_copy != NULL) 1537877fdebSMatt Macy abd_free(rr->rr_abd_copy); 154eda14cbcSMatt Macy 1557877fdebSMatt Macy if (rr->rr_abd_empty != NULL) 1567877fdebSMatt Macy abd_free(rr->rr_abd_empty); 157eda14cbcSMatt Macy 1587877fdebSMatt Macy kmem_free(rr, offsetof(raidz_row_t, rr_col[rr->rr_scols])); 1597877fdebSMatt Macy } 1607877fdebSMatt Macy 1617877fdebSMatt Macy void 1627877fdebSMatt Macy vdev_raidz_map_free(raidz_map_t *rm) 1637877fdebSMatt Macy { 1647877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) 1657877fdebSMatt Macy vdev_raidz_row_free(rm->rm_row[i]); 1667877fdebSMatt Macy 1677877fdebSMatt Macy kmem_free(rm, offsetof(raidz_map_t, rm_row[rm->rm_nrows])); 168eda14cbcSMatt Macy } 169eda14cbcSMatt Macy 170eda14cbcSMatt Macy static void 171eda14cbcSMatt Macy vdev_raidz_map_free_vsd(zio_t *zio) 172eda14cbcSMatt Macy { 173eda14cbcSMatt Macy raidz_map_t *rm = zio->io_vsd; 174eda14cbcSMatt Macy 175eda14cbcSMatt Macy ASSERT0(rm->rm_freed); 1767877fdebSMatt Macy rm->rm_freed = B_TRUE; 177eda14cbcSMatt Macy 1787877fdebSMatt Macy if (rm->rm_reports == 0) { 179eda14cbcSMatt Macy vdev_raidz_map_free(rm); 180eda14cbcSMatt Macy } 1817877fdebSMatt Macy } 182eda14cbcSMatt Macy 183eda14cbcSMatt Macy /*ARGSUSED*/ 184eda14cbcSMatt Macy static void 185eda14cbcSMatt Macy vdev_raidz_cksum_free(void *arg, size_t ignored) 186eda14cbcSMatt Macy { 187eda14cbcSMatt Macy raidz_map_t *rm = arg; 188eda14cbcSMatt Macy 189eda14cbcSMatt Macy ASSERT3U(rm->rm_reports, >, 0); 190eda14cbcSMatt Macy 1917877fdebSMatt Macy if (--rm->rm_reports == 0 && rm->rm_freed) 192eda14cbcSMatt Macy vdev_raidz_map_free(rm); 193eda14cbcSMatt Macy } 194eda14cbcSMatt Macy 195eda14cbcSMatt Macy static void 196eda14cbcSMatt Macy vdev_raidz_cksum_finish(zio_cksum_report_t *zcr, const abd_t *good_data) 197eda14cbcSMatt Macy { 198eda14cbcSMatt Macy raidz_map_t *rm = zcr->zcr_cbdata; 199eda14cbcSMatt Macy const size_t c = zcr->zcr_cbinfo; 200eda14cbcSMatt Macy size_t x, offset; 201eda14cbcSMatt Macy 202eda14cbcSMatt Macy if (good_data == NULL) { 203eda14cbcSMatt Macy zfs_ereport_finish_checksum(zcr, NULL, NULL, B_FALSE); 204eda14cbcSMatt Macy return; 205eda14cbcSMatt Macy } 206eda14cbcSMatt Macy 2077877fdebSMatt Macy ASSERT3U(rm->rm_nrows, ==, 1); 2087877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[0]; 2097877fdebSMatt Macy 2107877fdebSMatt Macy const abd_t *good = NULL; 2117877fdebSMatt Macy const abd_t *bad = rr->rr_col[c].rc_abd; 2127877fdebSMatt Macy 2137877fdebSMatt Macy if (c < rr->rr_firstdatacol) { 214eda14cbcSMatt Macy /* 215eda14cbcSMatt Macy * The first time through, calculate the parity blocks for 216eda14cbcSMatt Macy * the good data (this relies on the fact that the good 217eda14cbcSMatt Macy * data never changes for a given logical ZIO) 218eda14cbcSMatt Macy */ 2197877fdebSMatt Macy if (rr->rr_col[0].rc_gdata == NULL) { 220eda14cbcSMatt Macy abd_t *bad_parity[VDEV_RAIDZ_MAXPARITY]; 221eda14cbcSMatt Macy 222eda14cbcSMatt Macy /* 2237877fdebSMatt Macy * Set up the rr_col[]s to generate the parity for 224eda14cbcSMatt Macy * good_data, first saving the parity bufs and 225eda14cbcSMatt Macy * replacing them with buffers to hold the result. 226eda14cbcSMatt Macy */ 2277877fdebSMatt Macy for (x = 0; x < rr->rr_firstdatacol; x++) { 2287877fdebSMatt Macy bad_parity[x] = rr->rr_col[x].rc_abd; 2297877fdebSMatt Macy rr->rr_col[x].rc_abd = rr->rr_col[x].rc_gdata = 2307877fdebSMatt Macy abd_alloc_sametype(rr->rr_col[x].rc_abd, 2317877fdebSMatt Macy rr->rr_col[x].rc_size); 232eda14cbcSMatt Macy } 233eda14cbcSMatt Macy 234eda14cbcSMatt Macy /* fill in the data columns from good_data */ 235eda14cbcSMatt Macy offset = 0; 2367877fdebSMatt Macy for (; x < rr->rr_cols; x++) { 237184c1b94SMartin Matuska abd_free(rr->rr_col[x].rc_abd); 238eda14cbcSMatt Macy 2397877fdebSMatt Macy rr->rr_col[x].rc_abd = 240eda14cbcSMatt Macy abd_get_offset_size((abd_t *)good_data, 2417877fdebSMatt Macy offset, rr->rr_col[x].rc_size); 2427877fdebSMatt Macy offset += rr->rr_col[x].rc_size; 243eda14cbcSMatt Macy } 244eda14cbcSMatt Macy 245eda14cbcSMatt Macy /* 246eda14cbcSMatt Macy * Construct the parity from the good data. 247eda14cbcSMatt Macy */ 2487877fdebSMatt Macy vdev_raidz_generate_parity_row(rm, rr); 249eda14cbcSMatt Macy 250eda14cbcSMatt Macy /* restore everything back to its original state */ 2517877fdebSMatt Macy for (x = 0; x < rr->rr_firstdatacol; x++) 2527877fdebSMatt Macy rr->rr_col[x].rc_abd = bad_parity[x]; 253eda14cbcSMatt Macy 254eda14cbcSMatt Macy offset = 0; 2557877fdebSMatt Macy for (x = rr->rr_firstdatacol; x < rr->rr_cols; x++) { 256184c1b94SMartin Matuska abd_free(rr->rr_col[x].rc_abd); 2577877fdebSMatt Macy rr->rr_col[x].rc_abd = abd_get_offset_size( 2587877fdebSMatt Macy rr->rr_abd_copy, offset, 2597877fdebSMatt Macy rr->rr_col[x].rc_size); 2607877fdebSMatt Macy offset += rr->rr_col[x].rc_size; 261eda14cbcSMatt Macy } 262eda14cbcSMatt Macy } 263eda14cbcSMatt Macy 2647877fdebSMatt Macy ASSERT3P(rr->rr_col[c].rc_gdata, !=, NULL); 2657877fdebSMatt Macy good = abd_get_offset_size(rr->rr_col[c].rc_gdata, 0, 2667877fdebSMatt Macy rr->rr_col[c].rc_size); 267eda14cbcSMatt Macy } else { 268eda14cbcSMatt Macy /* adjust good_data to point at the start of our column */ 269eda14cbcSMatt Macy offset = 0; 2707877fdebSMatt Macy for (x = rr->rr_firstdatacol; x < c; x++) 2717877fdebSMatt Macy offset += rr->rr_col[x].rc_size; 272eda14cbcSMatt Macy 273eda14cbcSMatt Macy good = abd_get_offset_size((abd_t *)good_data, offset, 2747877fdebSMatt Macy rr->rr_col[c].rc_size); 275eda14cbcSMatt Macy } 276eda14cbcSMatt Macy 277eda14cbcSMatt Macy /* we drop the ereport if it ends up that the data was good */ 278eda14cbcSMatt Macy zfs_ereport_finish_checksum(zcr, good, bad, B_TRUE); 279184c1b94SMartin Matuska abd_free((abd_t *)good); 280eda14cbcSMatt Macy } 281eda14cbcSMatt Macy 282eda14cbcSMatt Macy /* 283eda14cbcSMatt Macy * Invoked indirectly by zfs_ereport_start_checksum(), called 284eda14cbcSMatt Macy * below when our read operation fails completely. The main point 285eda14cbcSMatt Macy * is to keep a copy of everything we read from disk, so that at 286eda14cbcSMatt Macy * vdev_raidz_cksum_finish() time we can compare it with the good data. 287eda14cbcSMatt Macy */ 288eda14cbcSMatt Macy static void 289eda14cbcSMatt Macy vdev_raidz_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *arg) 290eda14cbcSMatt Macy { 291eda14cbcSMatt Macy size_t c = (size_t)(uintptr_t)arg; 292eda14cbcSMatt Macy raidz_map_t *rm = zio->io_vsd; 293eda14cbcSMatt Macy 294eda14cbcSMatt Macy /* set up the report and bump the refcount */ 295eda14cbcSMatt Macy zcr->zcr_cbdata = rm; 296eda14cbcSMatt Macy zcr->zcr_cbinfo = c; 297eda14cbcSMatt Macy zcr->zcr_finish = vdev_raidz_cksum_finish; 298eda14cbcSMatt Macy zcr->zcr_free = vdev_raidz_cksum_free; 299eda14cbcSMatt Macy 300eda14cbcSMatt Macy rm->rm_reports++; 301eda14cbcSMatt Macy ASSERT3U(rm->rm_reports, >, 0); 3027877fdebSMatt Macy ASSERT3U(rm->rm_nrows, ==, 1); 303eda14cbcSMatt Macy 3047877fdebSMatt Macy if (rm->rm_row[0]->rr_abd_copy != NULL) 305eda14cbcSMatt Macy return; 306eda14cbcSMatt Macy 307eda14cbcSMatt Macy /* 308eda14cbcSMatt Macy * It's the first time we're called for this raidz_map_t, so we need 309eda14cbcSMatt Macy * to copy the data aside; there's no guarantee that our zio's buffer 310eda14cbcSMatt Macy * won't be re-used for something else. 311eda14cbcSMatt Macy * 312eda14cbcSMatt Macy * Our parity data is already in separate buffers, so there's no need 313eda14cbcSMatt Macy * to copy them. 314eda14cbcSMatt Macy */ 3157877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 3167877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 3177877fdebSMatt Macy size_t offset = 0; 3187877fdebSMatt Macy size_t size = 0; 319eda14cbcSMatt Macy 3207877fdebSMatt Macy for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) 3217877fdebSMatt Macy size += rr->rr_col[c].rc_size; 322eda14cbcSMatt Macy 3237877fdebSMatt Macy rr->rr_abd_copy = abd_alloc_for_io(size, B_FALSE); 324eda14cbcSMatt Macy 3257877fdebSMatt Macy for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 3267877fdebSMatt Macy raidz_col_t *col = &rr->rr_col[c]; 3277877fdebSMatt Macy abd_t *tmp = abd_get_offset_size(rr->rr_abd_copy, 3287877fdebSMatt Macy offset, col->rc_size); 329eda14cbcSMatt Macy 330eda14cbcSMatt Macy abd_copy(tmp, col->rc_abd, col->rc_size); 331eda14cbcSMatt Macy 332184c1b94SMartin Matuska abd_free(col->rc_abd); 333eda14cbcSMatt Macy col->rc_abd = tmp; 334eda14cbcSMatt Macy 335eda14cbcSMatt Macy offset += col->rc_size; 336eda14cbcSMatt Macy } 337eda14cbcSMatt Macy ASSERT3U(offset, ==, size); 338eda14cbcSMatt Macy } 3397877fdebSMatt Macy } 340eda14cbcSMatt Macy 341eda14cbcSMatt Macy static const zio_vsd_ops_t vdev_raidz_vsd_ops = { 342eda14cbcSMatt Macy .vsd_free = vdev_raidz_map_free_vsd, 343eda14cbcSMatt Macy .vsd_cksum_report = vdev_raidz_cksum_report 344eda14cbcSMatt Macy }; 345eda14cbcSMatt Macy 346eda14cbcSMatt Macy /* 347eda14cbcSMatt Macy * Divides the IO evenly across all child vdevs; usually, dcols is 348eda14cbcSMatt Macy * the number of children in the target vdev. 349eda14cbcSMatt Macy * 350eda14cbcSMatt Macy * Avoid inlining the function to keep vdev_raidz_io_start(), which 351eda14cbcSMatt Macy * is this functions only caller, as small as possible on the stack. 352eda14cbcSMatt Macy */ 353eda14cbcSMatt Macy noinline raidz_map_t * 354eda14cbcSMatt Macy vdev_raidz_map_alloc(zio_t *zio, uint64_t ashift, uint64_t dcols, 355eda14cbcSMatt Macy uint64_t nparity) 356eda14cbcSMatt Macy { 3577877fdebSMatt Macy raidz_row_t *rr; 358eda14cbcSMatt Macy /* The starting RAIDZ (parent) vdev sector of the block. */ 359eda14cbcSMatt Macy uint64_t b = zio->io_offset >> ashift; 360eda14cbcSMatt Macy /* The zio's size in units of the vdev's minimum sector size. */ 361eda14cbcSMatt Macy uint64_t s = zio->io_size >> ashift; 362eda14cbcSMatt Macy /* The first column for this stripe. */ 363eda14cbcSMatt Macy uint64_t f = b % dcols; 364eda14cbcSMatt Macy /* The starting byte offset on each child vdev. */ 365eda14cbcSMatt Macy uint64_t o = (b / dcols) << ashift; 366eda14cbcSMatt Macy uint64_t q, r, c, bc, col, acols, scols, coff, devidx, asize, tot; 367eda14cbcSMatt Macy 3687877fdebSMatt Macy raidz_map_t *rm = 3697877fdebSMatt Macy kmem_zalloc(offsetof(raidz_map_t, rm_row[1]), KM_SLEEP); 3707877fdebSMatt Macy rm->rm_nrows = 1; 3717877fdebSMatt Macy 372eda14cbcSMatt Macy /* 373eda14cbcSMatt Macy * "Quotient": The number of data sectors for this stripe on all but 374eda14cbcSMatt Macy * the "big column" child vdevs that also contain "remainder" data. 375eda14cbcSMatt Macy */ 376eda14cbcSMatt Macy q = s / (dcols - nparity); 377eda14cbcSMatt Macy 378eda14cbcSMatt Macy /* 379eda14cbcSMatt Macy * "Remainder": The number of partial stripe data sectors in this I/O. 380eda14cbcSMatt Macy * This will add a sector to some, but not all, child vdevs. 381eda14cbcSMatt Macy */ 382eda14cbcSMatt Macy r = s - q * (dcols - nparity); 383eda14cbcSMatt Macy 384eda14cbcSMatt Macy /* The number of "big columns" - those which contain remainder data. */ 385eda14cbcSMatt Macy bc = (r == 0 ? 0 : r + nparity); 386eda14cbcSMatt Macy 387eda14cbcSMatt Macy /* 388eda14cbcSMatt Macy * The total number of data and parity sectors associated with 389eda14cbcSMatt Macy * this I/O. 390eda14cbcSMatt Macy */ 391eda14cbcSMatt Macy tot = s + nparity * (q + (r == 0 ? 0 : 1)); 392eda14cbcSMatt Macy 3937877fdebSMatt Macy /* 3947877fdebSMatt Macy * acols: The columns that will be accessed. 3957877fdebSMatt Macy * scols: The columns that will be accessed or skipped. 3967877fdebSMatt Macy */ 397eda14cbcSMatt Macy if (q == 0) { 398eda14cbcSMatt Macy /* Our I/O request doesn't span all child vdevs. */ 399eda14cbcSMatt Macy acols = bc; 400eda14cbcSMatt Macy scols = MIN(dcols, roundup(bc, nparity + 1)); 401eda14cbcSMatt Macy } else { 402eda14cbcSMatt Macy acols = dcols; 403eda14cbcSMatt Macy scols = dcols; 404eda14cbcSMatt Macy } 405eda14cbcSMatt Macy 406eda14cbcSMatt Macy ASSERT3U(acols, <=, scols); 407eda14cbcSMatt Macy 4087877fdebSMatt Macy rr = kmem_alloc(offsetof(raidz_row_t, rr_col[scols]), KM_SLEEP); 4097877fdebSMatt Macy rm->rm_row[0] = rr; 410eda14cbcSMatt Macy 4117877fdebSMatt Macy rr->rr_cols = acols; 4127877fdebSMatt Macy rr->rr_scols = scols; 4137877fdebSMatt Macy rr->rr_bigcols = bc; 4147877fdebSMatt Macy rr->rr_missingdata = 0; 4157877fdebSMatt Macy rr->rr_missingparity = 0; 4167877fdebSMatt Macy rr->rr_firstdatacol = nparity; 4177877fdebSMatt Macy rr->rr_abd_copy = NULL; 4187877fdebSMatt Macy rr->rr_abd_empty = NULL; 4197877fdebSMatt Macy rr->rr_nempty = 0; 4207877fdebSMatt Macy #ifdef ZFS_DEBUG 4217877fdebSMatt Macy rr->rr_offset = zio->io_offset; 4227877fdebSMatt Macy rr->rr_size = zio->io_size; 4237877fdebSMatt Macy #endif 424eda14cbcSMatt Macy 425eda14cbcSMatt Macy asize = 0; 426eda14cbcSMatt Macy 427eda14cbcSMatt Macy for (c = 0; c < scols; c++) { 4287877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 429eda14cbcSMatt Macy col = f + c; 430eda14cbcSMatt Macy coff = o; 431eda14cbcSMatt Macy if (col >= dcols) { 432eda14cbcSMatt Macy col -= dcols; 433eda14cbcSMatt Macy coff += 1ULL << ashift; 434eda14cbcSMatt Macy } 4357877fdebSMatt Macy rc->rc_devidx = col; 4367877fdebSMatt Macy rc->rc_offset = coff; 4377877fdebSMatt Macy rc->rc_abd = NULL; 4387877fdebSMatt Macy rc->rc_gdata = NULL; 4397877fdebSMatt Macy rc->rc_orig_data = NULL; 4407877fdebSMatt Macy rc->rc_error = 0; 4417877fdebSMatt Macy rc->rc_tried = 0; 4427877fdebSMatt Macy rc->rc_skipped = 0; 4437877fdebSMatt Macy rc->rc_repair = 0; 4447877fdebSMatt Macy rc->rc_need_orig_restore = B_FALSE; 445eda14cbcSMatt Macy 446eda14cbcSMatt Macy if (c >= acols) 4477877fdebSMatt Macy rc->rc_size = 0; 448eda14cbcSMatt Macy else if (c < bc) 4497877fdebSMatt Macy rc->rc_size = (q + 1) << ashift; 450eda14cbcSMatt Macy else 4517877fdebSMatt Macy rc->rc_size = q << ashift; 452eda14cbcSMatt Macy 4537877fdebSMatt Macy asize += rc->rc_size; 454eda14cbcSMatt Macy } 455eda14cbcSMatt Macy 456eda14cbcSMatt Macy ASSERT3U(asize, ==, tot << ashift); 457eda14cbcSMatt Macy rm->rm_nskip = roundup(tot, nparity + 1) - tot; 4587877fdebSMatt Macy rm->rm_skipstart = bc; 459eda14cbcSMatt Macy 4607877fdebSMatt Macy for (c = 0; c < rr->rr_firstdatacol; c++) 4617877fdebSMatt Macy rr->rr_col[c].rc_abd = 4627877fdebSMatt Macy abd_alloc_linear(rr->rr_col[c].rc_size, B_FALSE); 463eda14cbcSMatt Macy 464184c1b94SMartin Matuska for (uint64_t off = 0; c < acols; c++) { 4657877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 466184c1b94SMartin Matuska rc->rc_abd = abd_get_offset_struct(&rc->rc_abdstruct, 467184c1b94SMartin Matuska zio->io_abd, off, rc->rc_size); 4687877fdebSMatt Macy off += rc->rc_size; 469eda14cbcSMatt Macy } 470eda14cbcSMatt Macy 471eda14cbcSMatt Macy /* 472eda14cbcSMatt Macy * If all data stored spans all columns, there's a danger that parity 473eda14cbcSMatt Macy * will always be on the same device and, since parity isn't read 474eda14cbcSMatt Macy * during normal operation, that device's I/O bandwidth won't be 475eda14cbcSMatt Macy * used effectively. We therefore switch the parity every 1MB. 476eda14cbcSMatt Macy * 477eda14cbcSMatt Macy * ... at least that was, ostensibly, the theory. As a practical 478eda14cbcSMatt Macy * matter unless we juggle the parity between all devices evenly, we 479eda14cbcSMatt Macy * won't see any benefit. Further, occasional writes that aren't a 480eda14cbcSMatt Macy * multiple of the LCM of the number of children and the minimum 481eda14cbcSMatt Macy * stripe width are sufficient to avoid pessimal behavior. 482eda14cbcSMatt Macy * Unfortunately, this decision created an implicit on-disk format 483eda14cbcSMatt Macy * requirement that we need to support for all eternity, but only 484eda14cbcSMatt Macy * for single-parity RAID-Z. 485eda14cbcSMatt Macy * 486eda14cbcSMatt Macy * If we intend to skip a sector in the zeroth column for padding 487eda14cbcSMatt Macy * we must make sure to note this swap. We will never intend to 488eda14cbcSMatt Macy * skip the first column since at least one data and one parity 489eda14cbcSMatt Macy * column must appear in each row. 490eda14cbcSMatt Macy */ 4917877fdebSMatt Macy ASSERT(rr->rr_cols >= 2); 4927877fdebSMatt Macy ASSERT(rr->rr_col[0].rc_size == rr->rr_col[1].rc_size); 493eda14cbcSMatt Macy 4947877fdebSMatt Macy if (rr->rr_firstdatacol == 1 && (zio->io_offset & (1ULL << 20))) { 4957877fdebSMatt Macy devidx = rr->rr_col[0].rc_devidx; 4967877fdebSMatt Macy o = rr->rr_col[0].rc_offset; 4977877fdebSMatt Macy rr->rr_col[0].rc_devidx = rr->rr_col[1].rc_devidx; 4987877fdebSMatt Macy rr->rr_col[0].rc_offset = rr->rr_col[1].rc_offset; 4997877fdebSMatt Macy rr->rr_col[1].rc_devidx = devidx; 5007877fdebSMatt Macy rr->rr_col[1].rc_offset = o; 501eda14cbcSMatt Macy 502eda14cbcSMatt Macy if (rm->rm_skipstart == 0) 503eda14cbcSMatt Macy rm->rm_skipstart = 1; 504eda14cbcSMatt Macy } 505eda14cbcSMatt Macy 506eda14cbcSMatt Macy /* init RAIDZ parity ops */ 507eda14cbcSMatt Macy rm->rm_ops = vdev_raidz_math_get_ops(); 508eda14cbcSMatt Macy 509eda14cbcSMatt Macy return (rm); 510eda14cbcSMatt Macy } 511eda14cbcSMatt Macy 512eda14cbcSMatt Macy struct pqr_struct { 513eda14cbcSMatt Macy uint64_t *p; 514eda14cbcSMatt Macy uint64_t *q; 515eda14cbcSMatt Macy uint64_t *r; 516eda14cbcSMatt Macy }; 517eda14cbcSMatt Macy 518eda14cbcSMatt Macy static int 519eda14cbcSMatt Macy vdev_raidz_p_func(void *buf, size_t size, void *private) 520eda14cbcSMatt Macy { 521eda14cbcSMatt Macy struct pqr_struct *pqr = private; 522eda14cbcSMatt Macy const uint64_t *src = buf; 523eda14cbcSMatt Macy int i, cnt = size / sizeof (src[0]); 524eda14cbcSMatt Macy 525eda14cbcSMatt Macy ASSERT(pqr->p && !pqr->q && !pqr->r); 526eda14cbcSMatt Macy 527eda14cbcSMatt Macy for (i = 0; i < cnt; i++, src++, pqr->p++) 528eda14cbcSMatt Macy *pqr->p ^= *src; 529eda14cbcSMatt Macy 530eda14cbcSMatt Macy return (0); 531eda14cbcSMatt Macy } 532eda14cbcSMatt Macy 533eda14cbcSMatt Macy static int 534eda14cbcSMatt Macy vdev_raidz_pq_func(void *buf, size_t size, void *private) 535eda14cbcSMatt Macy { 536eda14cbcSMatt Macy struct pqr_struct *pqr = private; 537eda14cbcSMatt Macy const uint64_t *src = buf; 538eda14cbcSMatt Macy uint64_t mask; 539eda14cbcSMatt Macy int i, cnt = size / sizeof (src[0]); 540eda14cbcSMatt Macy 541eda14cbcSMatt Macy ASSERT(pqr->p && pqr->q && !pqr->r); 542eda14cbcSMatt Macy 543eda14cbcSMatt Macy for (i = 0; i < cnt; i++, src++, pqr->p++, pqr->q++) { 544eda14cbcSMatt Macy *pqr->p ^= *src; 545eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2(*pqr->q, mask); 546eda14cbcSMatt Macy *pqr->q ^= *src; 547eda14cbcSMatt Macy } 548eda14cbcSMatt Macy 549eda14cbcSMatt Macy return (0); 550eda14cbcSMatt Macy } 551eda14cbcSMatt Macy 552eda14cbcSMatt Macy static int 553eda14cbcSMatt Macy vdev_raidz_pqr_func(void *buf, size_t size, void *private) 554eda14cbcSMatt Macy { 555eda14cbcSMatt Macy struct pqr_struct *pqr = private; 556eda14cbcSMatt Macy const uint64_t *src = buf; 557eda14cbcSMatt Macy uint64_t mask; 558eda14cbcSMatt Macy int i, cnt = size / sizeof (src[0]); 559eda14cbcSMatt Macy 560eda14cbcSMatt Macy ASSERT(pqr->p && pqr->q && pqr->r); 561eda14cbcSMatt Macy 562eda14cbcSMatt Macy for (i = 0; i < cnt; i++, src++, pqr->p++, pqr->q++, pqr->r++) { 563eda14cbcSMatt Macy *pqr->p ^= *src; 564eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2(*pqr->q, mask); 565eda14cbcSMatt Macy *pqr->q ^= *src; 566eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_4(*pqr->r, mask); 567eda14cbcSMatt Macy *pqr->r ^= *src; 568eda14cbcSMatt Macy } 569eda14cbcSMatt Macy 570eda14cbcSMatt Macy return (0); 571eda14cbcSMatt Macy } 572eda14cbcSMatt Macy 573eda14cbcSMatt Macy static void 5747877fdebSMatt Macy vdev_raidz_generate_parity_p(raidz_row_t *rr) 575eda14cbcSMatt Macy { 5767877fdebSMatt Macy uint64_t *p = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd); 577eda14cbcSMatt Macy 5787877fdebSMatt Macy for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 5797877fdebSMatt Macy abd_t *src = rr->rr_col[c].rc_abd; 580eda14cbcSMatt Macy 5817877fdebSMatt Macy if (c == rr->rr_firstdatacol) { 5827877fdebSMatt Macy abd_copy_to_buf(p, src, rr->rr_col[c].rc_size); 583eda14cbcSMatt Macy } else { 584eda14cbcSMatt Macy struct pqr_struct pqr = { p, NULL, NULL }; 5857877fdebSMatt Macy (void) abd_iterate_func(src, 0, rr->rr_col[c].rc_size, 586eda14cbcSMatt Macy vdev_raidz_p_func, &pqr); 587eda14cbcSMatt Macy } 588eda14cbcSMatt Macy } 589eda14cbcSMatt Macy } 590eda14cbcSMatt Macy 591eda14cbcSMatt Macy static void 5927877fdebSMatt Macy vdev_raidz_generate_parity_pq(raidz_row_t *rr) 593eda14cbcSMatt Macy { 5947877fdebSMatt Macy uint64_t *p = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd); 5957877fdebSMatt Macy uint64_t *q = abd_to_buf(rr->rr_col[VDEV_RAIDZ_Q].rc_abd); 5967877fdebSMatt Macy uint64_t pcnt = rr->rr_col[VDEV_RAIDZ_P].rc_size / sizeof (p[0]); 5977877fdebSMatt Macy ASSERT(rr->rr_col[VDEV_RAIDZ_P].rc_size == 5987877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_Q].rc_size); 599eda14cbcSMatt Macy 6007877fdebSMatt Macy for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 6017877fdebSMatt Macy abd_t *src = rr->rr_col[c].rc_abd; 602eda14cbcSMatt Macy 6037877fdebSMatt Macy uint64_t ccnt = rr->rr_col[c].rc_size / sizeof (p[0]); 604eda14cbcSMatt Macy 6057877fdebSMatt Macy if (c == rr->rr_firstdatacol) { 606eda14cbcSMatt Macy ASSERT(ccnt == pcnt || ccnt == 0); 6077877fdebSMatt Macy abd_copy_to_buf(p, src, rr->rr_col[c].rc_size); 6087877fdebSMatt Macy (void) memcpy(q, p, rr->rr_col[c].rc_size); 609eda14cbcSMatt Macy 6107877fdebSMatt Macy for (uint64_t i = ccnt; i < pcnt; i++) { 611eda14cbcSMatt Macy p[i] = 0; 612eda14cbcSMatt Macy q[i] = 0; 613eda14cbcSMatt Macy } 614eda14cbcSMatt Macy } else { 615eda14cbcSMatt Macy struct pqr_struct pqr = { p, q, NULL }; 616eda14cbcSMatt Macy 617eda14cbcSMatt Macy ASSERT(ccnt <= pcnt); 6187877fdebSMatt Macy (void) abd_iterate_func(src, 0, rr->rr_col[c].rc_size, 619eda14cbcSMatt Macy vdev_raidz_pq_func, &pqr); 620eda14cbcSMatt Macy 621eda14cbcSMatt Macy /* 622eda14cbcSMatt Macy * Treat short columns as though they are full of 0s. 623eda14cbcSMatt Macy * Note that there's therefore nothing needed for P. 624eda14cbcSMatt Macy */ 6257877fdebSMatt Macy uint64_t mask; 6267877fdebSMatt Macy for (uint64_t i = ccnt; i < pcnt; i++) { 627eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2(q[i], mask); 628eda14cbcSMatt Macy } 629eda14cbcSMatt Macy } 630eda14cbcSMatt Macy } 631eda14cbcSMatt Macy } 632eda14cbcSMatt Macy 633eda14cbcSMatt Macy static void 6347877fdebSMatt Macy vdev_raidz_generate_parity_pqr(raidz_row_t *rr) 635eda14cbcSMatt Macy { 6367877fdebSMatt Macy uint64_t *p = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd); 6377877fdebSMatt Macy uint64_t *q = abd_to_buf(rr->rr_col[VDEV_RAIDZ_Q].rc_abd); 6387877fdebSMatt Macy uint64_t *r = abd_to_buf(rr->rr_col[VDEV_RAIDZ_R].rc_abd); 6397877fdebSMatt Macy uint64_t pcnt = rr->rr_col[VDEV_RAIDZ_P].rc_size / sizeof (p[0]); 6407877fdebSMatt Macy ASSERT(rr->rr_col[VDEV_RAIDZ_P].rc_size == 6417877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_Q].rc_size); 6427877fdebSMatt Macy ASSERT(rr->rr_col[VDEV_RAIDZ_P].rc_size == 6437877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_R].rc_size); 644eda14cbcSMatt Macy 6457877fdebSMatt Macy for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 6467877fdebSMatt Macy abd_t *src = rr->rr_col[c].rc_abd; 647eda14cbcSMatt Macy 6487877fdebSMatt Macy uint64_t ccnt = rr->rr_col[c].rc_size / sizeof (p[0]); 649eda14cbcSMatt Macy 6507877fdebSMatt Macy if (c == rr->rr_firstdatacol) { 651eda14cbcSMatt Macy ASSERT(ccnt == pcnt || ccnt == 0); 6527877fdebSMatt Macy abd_copy_to_buf(p, src, rr->rr_col[c].rc_size); 6537877fdebSMatt Macy (void) memcpy(q, p, rr->rr_col[c].rc_size); 6547877fdebSMatt Macy (void) memcpy(r, p, rr->rr_col[c].rc_size); 655eda14cbcSMatt Macy 6567877fdebSMatt Macy for (uint64_t i = ccnt; i < pcnt; i++) { 657eda14cbcSMatt Macy p[i] = 0; 658eda14cbcSMatt Macy q[i] = 0; 659eda14cbcSMatt Macy r[i] = 0; 660eda14cbcSMatt Macy } 661eda14cbcSMatt Macy } else { 662eda14cbcSMatt Macy struct pqr_struct pqr = { p, q, r }; 663eda14cbcSMatt Macy 664eda14cbcSMatt Macy ASSERT(ccnt <= pcnt); 6657877fdebSMatt Macy (void) abd_iterate_func(src, 0, rr->rr_col[c].rc_size, 666eda14cbcSMatt Macy vdev_raidz_pqr_func, &pqr); 667eda14cbcSMatt Macy 668eda14cbcSMatt Macy /* 669eda14cbcSMatt Macy * Treat short columns as though they are full of 0s. 670eda14cbcSMatt Macy * Note that there's therefore nothing needed for P. 671eda14cbcSMatt Macy */ 6727877fdebSMatt Macy uint64_t mask; 6737877fdebSMatt Macy for (uint64_t i = ccnt; i < pcnt; i++) { 674eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2(q[i], mask); 675eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_4(r[i], mask); 676eda14cbcSMatt Macy } 677eda14cbcSMatt Macy } 678eda14cbcSMatt Macy } 679eda14cbcSMatt Macy } 680eda14cbcSMatt Macy 681eda14cbcSMatt Macy /* 682eda14cbcSMatt Macy * Generate RAID parity in the first virtual columns according to the number of 683eda14cbcSMatt Macy * parity columns available. 684eda14cbcSMatt Macy */ 685eda14cbcSMatt Macy void 6867877fdebSMatt Macy vdev_raidz_generate_parity_row(raidz_map_t *rm, raidz_row_t *rr) 687eda14cbcSMatt Macy { 6887877fdebSMatt Macy ASSERT3U(rr->rr_cols, !=, 0); 6897877fdebSMatt Macy 690eda14cbcSMatt Macy /* Generate using the new math implementation */ 6917877fdebSMatt Macy if (vdev_raidz_math_generate(rm, rr) != RAIDZ_ORIGINAL_IMPL) 692eda14cbcSMatt Macy return; 693eda14cbcSMatt Macy 6947877fdebSMatt Macy switch (rr->rr_firstdatacol) { 695eda14cbcSMatt Macy case 1: 6967877fdebSMatt Macy vdev_raidz_generate_parity_p(rr); 697eda14cbcSMatt Macy break; 698eda14cbcSMatt Macy case 2: 6997877fdebSMatt Macy vdev_raidz_generate_parity_pq(rr); 700eda14cbcSMatt Macy break; 701eda14cbcSMatt Macy case 3: 7027877fdebSMatt Macy vdev_raidz_generate_parity_pqr(rr); 703eda14cbcSMatt Macy break; 704eda14cbcSMatt Macy default: 705eda14cbcSMatt Macy cmn_err(CE_PANIC, "invalid RAID-Z configuration"); 706eda14cbcSMatt Macy } 707eda14cbcSMatt Macy } 708eda14cbcSMatt Macy 7097877fdebSMatt Macy void 7107877fdebSMatt Macy vdev_raidz_generate_parity(raidz_map_t *rm) 7117877fdebSMatt Macy { 7127877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 7137877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 7147877fdebSMatt Macy vdev_raidz_generate_parity_row(rm, rr); 7157877fdebSMatt Macy } 7167877fdebSMatt Macy } 7177877fdebSMatt Macy 718eda14cbcSMatt Macy /* ARGSUSED */ 719eda14cbcSMatt Macy static int 720eda14cbcSMatt Macy vdev_raidz_reconst_p_func(void *dbuf, void *sbuf, size_t size, void *private) 721eda14cbcSMatt Macy { 722eda14cbcSMatt Macy uint64_t *dst = dbuf; 723eda14cbcSMatt Macy uint64_t *src = sbuf; 724eda14cbcSMatt Macy int cnt = size / sizeof (src[0]); 725eda14cbcSMatt Macy 726eda14cbcSMatt Macy for (int i = 0; i < cnt; i++) { 727eda14cbcSMatt Macy dst[i] ^= src[i]; 728eda14cbcSMatt Macy } 729eda14cbcSMatt Macy 730eda14cbcSMatt Macy return (0); 731eda14cbcSMatt Macy } 732eda14cbcSMatt Macy 733eda14cbcSMatt Macy /* ARGSUSED */ 734eda14cbcSMatt Macy static int 735eda14cbcSMatt Macy vdev_raidz_reconst_q_pre_func(void *dbuf, void *sbuf, size_t size, 736eda14cbcSMatt Macy void *private) 737eda14cbcSMatt Macy { 738eda14cbcSMatt Macy uint64_t *dst = dbuf; 739eda14cbcSMatt Macy uint64_t *src = sbuf; 740eda14cbcSMatt Macy uint64_t mask; 741eda14cbcSMatt Macy int cnt = size / sizeof (dst[0]); 742eda14cbcSMatt Macy 743eda14cbcSMatt Macy for (int i = 0; i < cnt; i++, dst++, src++) { 744eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2(*dst, mask); 745eda14cbcSMatt Macy *dst ^= *src; 746eda14cbcSMatt Macy } 747eda14cbcSMatt Macy 748eda14cbcSMatt Macy return (0); 749eda14cbcSMatt Macy } 750eda14cbcSMatt Macy 751eda14cbcSMatt Macy /* ARGSUSED */ 752eda14cbcSMatt Macy static int 753eda14cbcSMatt Macy vdev_raidz_reconst_q_pre_tail_func(void *buf, size_t size, void *private) 754eda14cbcSMatt Macy { 755eda14cbcSMatt Macy uint64_t *dst = buf; 756eda14cbcSMatt Macy uint64_t mask; 757eda14cbcSMatt Macy int cnt = size / sizeof (dst[0]); 758eda14cbcSMatt Macy 759eda14cbcSMatt Macy for (int i = 0; i < cnt; i++, dst++) { 760eda14cbcSMatt Macy /* same operation as vdev_raidz_reconst_q_pre_func() on dst */ 761eda14cbcSMatt Macy VDEV_RAIDZ_64MUL_2(*dst, mask); 762eda14cbcSMatt Macy } 763eda14cbcSMatt Macy 764eda14cbcSMatt Macy return (0); 765eda14cbcSMatt Macy } 766eda14cbcSMatt Macy 767eda14cbcSMatt Macy struct reconst_q_struct { 768eda14cbcSMatt Macy uint64_t *q; 769eda14cbcSMatt Macy int exp; 770eda14cbcSMatt Macy }; 771eda14cbcSMatt Macy 772eda14cbcSMatt Macy static int 773eda14cbcSMatt Macy vdev_raidz_reconst_q_post_func(void *buf, size_t size, void *private) 774eda14cbcSMatt Macy { 775eda14cbcSMatt Macy struct reconst_q_struct *rq = private; 776eda14cbcSMatt Macy uint64_t *dst = buf; 777eda14cbcSMatt Macy int cnt = size / sizeof (dst[0]); 778eda14cbcSMatt Macy 779eda14cbcSMatt Macy for (int i = 0; i < cnt; i++, dst++, rq->q++) { 780eda14cbcSMatt Macy int j; 781eda14cbcSMatt Macy uint8_t *b; 782eda14cbcSMatt Macy 783eda14cbcSMatt Macy *dst ^= *rq->q; 784eda14cbcSMatt Macy for (j = 0, b = (uint8_t *)dst; j < 8; j++, b++) { 785eda14cbcSMatt Macy *b = vdev_raidz_exp2(*b, rq->exp); 786eda14cbcSMatt Macy } 787eda14cbcSMatt Macy } 788eda14cbcSMatt Macy 789eda14cbcSMatt Macy return (0); 790eda14cbcSMatt Macy } 791eda14cbcSMatt Macy 792eda14cbcSMatt Macy struct reconst_pq_struct { 793eda14cbcSMatt Macy uint8_t *p; 794eda14cbcSMatt Macy uint8_t *q; 795eda14cbcSMatt Macy uint8_t *pxy; 796eda14cbcSMatt Macy uint8_t *qxy; 797eda14cbcSMatt Macy int aexp; 798eda14cbcSMatt Macy int bexp; 799eda14cbcSMatt Macy }; 800eda14cbcSMatt Macy 801eda14cbcSMatt Macy static int 802eda14cbcSMatt Macy vdev_raidz_reconst_pq_func(void *xbuf, void *ybuf, size_t size, void *private) 803eda14cbcSMatt Macy { 804eda14cbcSMatt Macy struct reconst_pq_struct *rpq = private; 805eda14cbcSMatt Macy uint8_t *xd = xbuf; 806eda14cbcSMatt Macy uint8_t *yd = ybuf; 807eda14cbcSMatt Macy 808eda14cbcSMatt Macy for (int i = 0; i < size; 809eda14cbcSMatt Macy i++, rpq->p++, rpq->q++, rpq->pxy++, rpq->qxy++, xd++, yd++) { 810eda14cbcSMatt Macy *xd = vdev_raidz_exp2(*rpq->p ^ *rpq->pxy, rpq->aexp) ^ 811eda14cbcSMatt Macy vdev_raidz_exp2(*rpq->q ^ *rpq->qxy, rpq->bexp); 812eda14cbcSMatt Macy *yd = *rpq->p ^ *rpq->pxy ^ *xd; 813eda14cbcSMatt Macy } 814eda14cbcSMatt Macy 815eda14cbcSMatt Macy return (0); 816eda14cbcSMatt Macy } 817eda14cbcSMatt Macy 818eda14cbcSMatt Macy static int 819eda14cbcSMatt Macy vdev_raidz_reconst_pq_tail_func(void *xbuf, size_t size, void *private) 820eda14cbcSMatt Macy { 821eda14cbcSMatt Macy struct reconst_pq_struct *rpq = private; 822eda14cbcSMatt Macy uint8_t *xd = xbuf; 823eda14cbcSMatt Macy 824eda14cbcSMatt Macy for (int i = 0; i < size; 825eda14cbcSMatt Macy i++, rpq->p++, rpq->q++, rpq->pxy++, rpq->qxy++, xd++) { 826eda14cbcSMatt Macy /* same operation as vdev_raidz_reconst_pq_func() on xd */ 827eda14cbcSMatt Macy *xd = vdev_raidz_exp2(*rpq->p ^ *rpq->pxy, rpq->aexp) ^ 828eda14cbcSMatt Macy vdev_raidz_exp2(*rpq->q ^ *rpq->qxy, rpq->bexp); 829eda14cbcSMatt Macy } 830eda14cbcSMatt Macy 831eda14cbcSMatt Macy return (0); 832eda14cbcSMatt Macy } 833eda14cbcSMatt Macy 834eda14cbcSMatt Macy static int 8357877fdebSMatt Macy vdev_raidz_reconstruct_p(raidz_row_t *rr, int *tgts, int ntgts) 836eda14cbcSMatt Macy { 837eda14cbcSMatt Macy int x = tgts[0]; 838eda14cbcSMatt Macy abd_t *dst, *src; 839eda14cbcSMatt Macy 8407877fdebSMatt Macy ASSERT3U(ntgts, ==, 1); 8417877fdebSMatt Macy ASSERT3U(x, >=, rr->rr_firstdatacol); 8427877fdebSMatt Macy ASSERT3U(x, <, rr->rr_cols); 843eda14cbcSMatt Macy 8447877fdebSMatt Macy ASSERT3U(rr->rr_col[x].rc_size, <=, rr->rr_col[VDEV_RAIDZ_P].rc_size); 845eda14cbcSMatt Macy 8467877fdebSMatt Macy src = rr->rr_col[VDEV_RAIDZ_P].rc_abd; 8477877fdebSMatt Macy dst = rr->rr_col[x].rc_abd; 848eda14cbcSMatt Macy 8497877fdebSMatt Macy abd_copy_from_buf(dst, abd_to_buf(src), rr->rr_col[x].rc_size); 850eda14cbcSMatt Macy 8517877fdebSMatt Macy for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 8527877fdebSMatt Macy uint64_t size = MIN(rr->rr_col[x].rc_size, 8537877fdebSMatt Macy rr->rr_col[c].rc_size); 854eda14cbcSMatt Macy 8557877fdebSMatt Macy src = rr->rr_col[c].rc_abd; 856eda14cbcSMatt Macy 857eda14cbcSMatt Macy if (c == x) 858eda14cbcSMatt Macy continue; 859eda14cbcSMatt Macy 860eda14cbcSMatt Macy (void) abd_iterate_func2(dst, src, 0, 0, size, 861eda14cbcSMatt Macy vdev_raidz_reconst_p_func, NULL); 862eda14cbcSMatt Macy } 863eda14cbcSMatt Macy 864eda14cbcSMatt Macy return (1 << VDEV_RAIDZ_P); 865eda14cbcSMatt Macy } 866eda14cbcSMatt Macy 867eda14cbcSMatt Macy static int 8687877fdebSMatt Macy vdev_raidz_reconstruct_q(raidz_row_t *rr, int *tgts, int ntgts) 869eda14cbcSMatt Macy { 870eda14cbcSMatt Macy int x = tgts[0]; 871eda14cbcSMatt Macy int c, exp; 872eda14cbcSMatt Macy abd_t *dst, *src; 873eda14cbcSMatt Macy 874eda14cbcSMatt Macy ASSERT(ntgts == 1); 875eda14cbcSMatt Macy 8767877fdebSMatt Macy ASSERT(rr->rr_col[x].rc_size <= rr->rr_col[VDEV_RAIDZ_Q].rc_size); 877eda14cbcSMatt Macy 8787877fdebSMatt Macy for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 8797877fdebSMatt Macy uint64_t size = (c == x) ? 0 : MIN(rr->rr_col[x].rc_size, 8807877fdebSMatt Macy rr->rr_col[c].rc_size); 881eda14cbcSMatt Macy 8827877fdebSMatt Macy src = rr->rr_col[c].rc_abd; 8837877fdebSMatt Macy dst = rr->rr_col[x].rc_abd; 884eda14cbcSMatt Macy 8857877fdebSMatt Macy if (c == rr->rr_firstdatacol) { 886eda14cbcSMatt Macy abd_copy(dst, src, size); 8877877fdebSMatt Macy if (rr->rr_col[x].rc_size > size) { 888eda14cbcSMatt Macy abd_zero_off(dst, size, 8897877fdebSMatt Macy rr->rr_col[x].rc_size - size); 8907877fdebSMatt Macy } 891eda14cbcSMatt Macy } else { 8927877fdebSMatt Macy ASSERT3U(size, <=, rr->rr_col[x].rc_size); 893eda14cbcSMatt Macy (void) abd_iterate_func2(dst, src, 0, 0, size, 894eda14cbcSMatt Macy vdev_raidz_reconst_q_pre_func, NULL); 895eda14cbcSMatt Macy (void) abd_iterate_func(dst, 8967877fdebSMatt Macy size, rr->rr_col[x].rc_size - size, 897eda14cbcSMatt Macy vdev_raidz_reconst_q_pre_tail_func, NULL); 898eda14cbcSMatt Macy } 899eda14cbcSMatt Macy } 900eda14cbcSMatt Macy 9017877fdebSMatt Macy src = rr->rr_col[VDEV_RAIDZ_Q].rc_abd; 9027877fdebSMatt Macy dst = rr->rr_col[x].rc_abd; 9037877fdebSMatt Macy exp = 255 - (rr->rr_cols - 1 - x); 904eda14cbcSMatt Macy 905eda14cbcSMatt Macy struct reconst_q_struct rq = { abd_to_buf(src), exp }; 9067877fdebSMatt Macy (void) abd_iterate_func(dst, 0, rr->rr_col[x].rc_size, 907eda14cbcSMatt Macy vdev_raidz_reconst_q_post_func, &rq); 908eda14cbcSMatt Macy 909eda14cbcSMatt Macy return (1 << VDEV_RAIDZ_Q); 910eda14cbcSMatt Macy } 911eda14cbcSMatt Macy 912eda14cbcSMatt Macy static int 9137877fdebSMatt Macy vdev_raidz_reconstruct_pq(raidz_row_t *rr, int *tgts, int ntgts) 914eda14cbcSMatt Macy { 915eda14cbcSMatt Macy uint8_t *p, *q, *pxy, *qxy, tmp, a, b, aexp, bexp; 916eda14cbcSMatt Macy abd_t *pdata, *qdata; 917eda14cbcSMatt Macy uint64_t xsize, ysize; 918eda14cbcSMatt Macy int x = tgts[0]; 919eda14cbcSMatt Macy int y = tgts[1]; 920eda14cbcSMatt Macy abd_t *xd, *yd; 921eda14cbcSMatt Macy 922eda14cbcSMatt Macy ASSERT(ntgts == 2); 923eda14cbcSMatt Macy ASSERT(x < y); 9247877fdebSMatt Macy ASSERT(x >= rr->rr_firstdatacol); 9257877fdebSMatt Macy ASSERT(y < rr->rr_cols); 926eda14cbcSMatt Macy 9277877fdebSMatt Macy ASSERT(rr->rr_col[x].rc_size >= rr->rr_col[y].rc_size); 928eda14cbcSMatt Macy 929eda14cbcSMatt Macy /* 930eda14cbcSMatt Macy * Move the parity data aside -- we're going to compute parity as 931eda14cbcSMatt Macy * though columns x and y were full of zeros -- Pxy and Qxy. We want to 932eda14cbcSMatt Macy * reuse the parity generation mechanism without trashing the actual 933eda14cbcSMatt Macy * parity so we make those columns appear to be full of zeros by 934eda14cbcSMatt Macy * setting their lengths to zero. 935eda14cbcSMatt Macy */ 9367877fdebSMatt Macy pdata = rr->rr_col[VDEV_RAIDZ_P].rc_abd; 9377877fdebSMatt Macy qdata = rr->rr_col[VDEV_RAIDZ_Q].rc_abd; 9387877fdebSMatt Macy xsize = rr->rr_col[x].rc_size; 9397877fdebSMatt Macy ysize = rr->rr_col[y].rc_size; 940eda14cbcSMatt Macy 9417877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_P].rc_abd = 9427877fdebSMatt Macy abd_alloc_linear(rr->rr_col[VDEV_RAIDZ_P].rc_size, B_TRUE); 9437877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_Q].rc_abd = 9447877fdebSMatt Macy abd_alloc_linear(rr->rr_col[VDEV_RAIDZ_Q].rc_size, B_TRUE); 9457877fdebSMatt Macy rr->rr_col[x].rc_size = 0; 9467877fdebSMatt Macy rr->rr_col[y].rc_size = 0; 947eda14cbcSMatt Macy 9487877fdebSMatt Macy vdev_raidz_generate_parity_pq(rr); 949eda14cbcSMatt Macy 9507877fdebSMatt Macy rr->rr_col[x].rc_size = xsize; 9517877fdebSMatt Macy rr->rr_col[y].rc_size = ysize; 952eda14cbcSMatt Macy 953eda14cbcSMatt Macy p = abd_to_buf(pdata); 954eda14cbcSMatt Macy q = abd_to_buf(qdata); 9557877fdebSMatt Macy pxy = abd_to_buf(rr->rr_col[VDEV_RAIDZ_P].rc_abd); 9567877fdebSMatt Macy qxy = abd_to_buf(rr->rr_col[VDEV_RAIDZ_Q].rc_abd); 9577877fdebSMatt Macy xd = rr->rr_col[x].rc_abd; 9587877fdebSMatt Macy yd = rr->rr_col[y].rc_abd; 959eda14cbcSMatt Macy 960eda14cbcSMatt Macy /* 961eda14cbcSMatt Macy * We now have: 962eda14cbcSMatt Macy * Pxy = P + D_x + D_y 963eda14cbcSMatt Macy * Qxy = Q + 2^(ndevs - 1 - x) * D_x + 2^(ndevs - 1 - y) * D_y 964eda14cbcSMatt Macy * 965eda14cbcSMatt Macy * We can then solve for D_x: 966eda14cbcSMatt Macy * D_x = A * (P + Pxy) + B * (Q + Qxy) 967eda14cbcSMatt Macy * where 968eda14cbcSMatt Macy * A = 2^(x - y) * (2^(x - y) + 1)^-1 969eda14cbcSMatt Macy * B = 2^(ndevs - 1 - x) * (2^(x - y) + 1)^-1 970eda14cbcSMatt Macy * 971eda14cbcSMatt Macy * With D_x in hand, we can easily solve for D_y: 972eda14cbcSMatt Macy * D_y = P + Pxy + D_x 973eda14cbcSMatt Macy */ 974eda14cbcSMatt Macy 975eda14cbcSMatt Macy a = vdev_raidz_pow2[255 + x - y]; 9767877fdebSMatt Macy b = vdev_raidz_pow2[255 - (rr->rr_cols - 1 - x)]; 977eda14cbcSMatt Macy tmp = 255 - vdev_raidz_log2[a ^ 1]; 978eda14cbcSMatt Macy 979eda14cbcSMatt Macy aexp = vdev_raidz_log2[vdev_raidz_exp2(a, tmp)]; 980eda14cbcSMatt Macy bexp = vdev_raidz_log2[vdev_raidz_exp2(b, tmp)]; 981eda14cbcSMatt Macy 982eda14cbcSMatt Macy ASSERT3U(xsize, >=, ysize); 983eda14cbcSMatt Macy struct reconst_pq_struct rpq = { p, q, pxy, qxy, aexp, bexp }; 984eda14cbcSMatt Macy 985eda14cbcSMatt Macy (void) abd_iterate_func2(xd, yd, 0, 0, ysize, 986eda14cbcSMatt Macy vdev_raidz_reconst_pq_func, &rpq); 987eda14cbcSMatt Macy (void) abd_iterate_func(xd, ysize, xsize - ysize, 988eda14cbcSMatt Macy vdev_raidz_reconst_pq_tail_func, &rpq); 989eda14cbcSMatt Macy 9907877fdebSMatt Macy abd_free(rr->rr_col[VDEV_RAIDZ_P].rc_abd); 9917877fdebSMatt Macy abd_free(rr->rr_col[VDEV_RAIDZ_Q].rc_abd); 992eda14cbcSMatt Macy 993eda14cbcSMatt Macy /* 994eda14cbcSMatt Macy * Restore the saved parity data. 995eda14cbcSMatt Macy */ 9967877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_P].rc_abd = pdata; 9977877fdebSMatt Macy rr->rr_col[VDEV_RAIDZ_Q].rc_abd = qdata; 998eda14cbcSMatt Macy 999eda14cbcSMatt Macy return ((1 << VDEV_RAIDZ_P) | (1 << VDEV_RAIDZ_Q)); 1000eda14cbcSMatt Macy } 1001eda14cbcSMatt Macy 1002eda14cbcSMatt Macy /* BEGIN CSTYLED */ 1003eda14cbcSMatt Macy /* 1004eda14cbcSMatt Macy * In the general case of reconstruction, we must solve the system of linear 1005eda14cbcSMatt Macy * equations defined by the coefficients used to generate parity as well as 1006eda14cbcSMatt Macy * the contents of the data and parity disks. This can be expressed with 1007eda14cbcSMatt Macy * vectors for the original data (D) and the actual data (d) and parity (p) 1008eda14cbcSMatt Macy * and a matrix composed of the identity matrix (I) and a dispersal matrix (V): 1009eda14cbcSMatt Macy * 1010eda14cbcSMatt Macy * __ __ __ __ 1011eda14cbcSMatt Macy * | | __ __ | p_0 | 1012eda14cbcSMatt Macy * | V | | D_0 | | p_m-1 | 1013eda14cbcSMatt Macy * | | x | : | = | d_0 | 1014eda14cbcSMatt Macy * | I | | D_n-1 | | : | 1015eda14cbcSMatt Macy * | | ~~ ~~ | d_n-1 | 1016eda14cbcSMatt Macy * ~~ ~~ ~~ ~~ 1017eda14cbcSMatt Macy * 1018eda14cbcSMatt Macy * I is simply a square identity matrix of size n, and V is a vandermonde 1019eda14cbcSMatt Macy * matrix defined by the coefficients we chose for the various parity columns 1020eda14cbcSMatt Macy * (1, 2, 4). Note that these values were chosen both for simplicity, speedy 1021eda14cbcSMatt Macy * computation as well as linear separability. 1022eda14cbcSMatt Macy * 1023eda14cbcSMatt Macy * __ __ __ __ 1024eda14cbcSMatt Macy * | 1 .. 1 1 1 | | p_0 | 1025eda14cbcSMatt Macy * | 2^n-1 .. 4 2 1 | __ __ | : | 1026eda14cbcSMatt Macy * | 4^n-1 .. 16 4 1 | | D_0 | | p_m-1 | 1027eda14cbcSMatt Macy * | 1 .. 0 0 0 | | D_1 | | d_0 | 1028eda14cbcSMatt Macy * | 0 .. 0 0 0 | x | D_2 | = | d_1 | 1029eda14cbcSMatt Macy * | : : : : | | : | | d_2 | 1030eda14cbcSMatt Macy * | 0 .. 1 0 0 | | D_n-1 | | : | 1031eda14cbcSMatt Macy * | 0 .. 0 1 0 | ~~ ~~ | : | 1032eda14cbcSMatt Macy * | 0 .. 0 0 1 | | d_n-1 | 1033eda14cbcSMatt Macy * ~~ ~~ ~~ ~~ 1034eda14cbcSMatt Macy * 1035eda14cbcSMatt Macy * Note that I, V, d, and p are known. To compute D, we must invert the 1036eda14cbcSMatt Macy * matrix and use the known data and parity values to reconstruct the unknown 1037eda14cbcSMatt Macy * data values. We begin by removing the rows in V|I and d|p that correspond 1038eda14cbcSMatt Macy * to failed or missing columns; we then make V|I square (n x n) and d|p 1039eda14cbcSMatt Macy * sized n by removing rows corresponding to unused parity from the bottom up 1040eda14cbcSMatt Macy * to generate (V|I)' and (d|p)'. We can then generate the inverse of (V|I)' 1041eda14cbcSMatt Macy * using Gauss-Jordan elimination. In the example below we use m=3 parity 1042eda14cbcSMatt Macy * columns, n=8 data columns, with errors in d_1, d_2, and p_1: 1043eda14cbcSMatt Macy * __ __ 1044eda14cbcSMatt Macy * | 1 1 1 1 1 1 1 1 | 1045eda14cbcSMatt Macy * | 128 64 32 16 8 4 2 1 | <-----+-+-- missing disks 1046eda14cbcSMatt Macy * | 19 205 116 29 64 16 4 1 | / / 1047eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 | / / 1048eda14cbcSMatt Macy * | 0 1 0 0 0 0 0 0 | <--' / 1049eda14cbcSMatt Macy * (V|I) = | 0 0 1 0 0 0 0 0 | <---' 1050eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 | 1051eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 | 1052eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 | 1053eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 | 1054eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 | 1055eda14cbcSMatt Macy * ~~ ~~ 1056eda14cbcSMatt Macy * __ __ 1057eda14cbcSMatt Macy * | 1 1 1 1 1 1 1 1 | 1058eda14cbcSMatt Macy * | 128 64 32 16 8 4 2 1 | 1059eda14cbcSMatt Macy * | 19 205 116 29 64 16 4 1 | 1060eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 | 1061eda14cbcSMatt Macy * | 0 1 0 0 0 0 0 0 | 1062eda14cbcSMatt Macy * (V|I)' = | 0 0 1 0 0 0 0 0 | 1063eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 | 1064eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 | 1065eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 | 1066eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 | 1067eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 | 1068eda14cbcSMatt Macy * ~~ ~~ 1069eda14cbcSMatt Macy * 1070eda14cbcSMatt Macy * Here we employ Gauss-Jordan elimination to find the inverse of (V|I)'. We 1071eda14cbcSMatt Macy * have carefully chosen the seed values 1, 2, and 4 to ensure that this 1072eda14cbcSMatt Macy * matrix is not singular. 1073eda14cbcSMatt Macy * __ __ 1074eda14cbcSMatt Macy * | 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 | 1075eda14cbcSMatt Macy * | 19 205 116 29 64 16 4 1 0 1 0 0 0 0 0 0 | 1076eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1077eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 | 1078eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 | 1079eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 | 1080eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 | 1081eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 | 1082eda14cbcSMatt Macy * ~~ ~~ 1083eda14cbcSMatt Macy * __ __ 1084eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1085eda14cbcSMatt Macy * | 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 | 1086eda14cbcSMatt Macy * | 19 205 116 29 64 16 4 1 0 1 0 0 0 0 0 0 | 1087eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 | 1088eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 | 1089eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 | 1090eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 | 1091eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 | 1092eda14cbcSMatt Macy * ~~ ~~ 1093eda14cbcSMatt Macy * __ __ 1094eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1095eda14cbcSMatt Macy * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 | 1096eda14cbcSMatt Macy * | 0 205 116 0 0 0 0 0 0 1 19 29 64 16 4 1 | 1097eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 | 1098eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 | 1099eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 | 1100eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 | 1101eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 | 1102eda14cbcSMatt Macy * ~~ ~~ 1103eda14cbcSMatt Macy * __ __ 1104eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1105eda14cbcSMatt Macy * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 | 1106eda14cbcSMatt Macy * | 0 0 185 0 0 0 0 0 205 1 222 208 141 221 201 204 | 1107eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 | 1108eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 | 1109eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 | 1110eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 | 1111eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 | 1112eda14cbcSMatt Macy * ~~ ~~ 1113eda14cbcSMatt Macy * __ __ 1114eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1115eda14cbcSMatt Macy * | 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 | 1116eda14cbcSMatt Macy * | 0 0 1 0 0 0 0 0 166 100 4 40 158 168 216 209 | 1117eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 | 1118eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 | 1119eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 | 1120eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 | 1121eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 | 1122eda14cbcSMatt Macy * ~~ ~~ 1123eda14cbcSMatt Macy * __ __ 1124eda14cbcSMatt Macy * | 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1125eda14cbcSMatt Macy * | 0 1 0 0 0 0 0 0 167 100 5 41 159 169 217 208 | 1126eda14cbcSMatt Macy * | 0 0 1 0 0 0 0 0 166 100 4 40 158 168 216 209 | 1127eda14cbcSMatt Macy * | 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 | 1128eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 | 1129eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 | 1130eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 | 1131eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 | 1132eda14cbcSMatt Macy * ~~ ~~ 1133eda14cbcSMatt Macy * __ __ 1134eda14cbcSMatt Macy * | 0 0 1 0 0 0 0 0 | 1135eda14cbcSMatt Macy * | 167 100 5 41 159 169 217 208 | 1136eda14cbcSMatt Macy * | 166 100 4 40 158 168 216 209 | 1137eda14cbcSMatt Macy * (V|I)'^-1 = | 0 0 0 1 0 0 0 0 | 1138eda14cbcSMatt Macy * | 0 0 0 0 1 0 0 0 | 1139eda14cbcSMatt Macy * | 0 0 0 0 0 1 0 0 | 1140eda14cbcSMatt Macy * | 0 0 0 0 0 0 1 0 | 1141eda14cbcSMatt Macy * | 0 0 0 0 0 0 0 1 | 1142eda14cbcSMatt Macy * ~~ ~~ 1143eda14cbcSMatt Macy * 1144eda14cbcSMatt Macy * We can then simply compute D = (V|I)'^-1 x (d|p)' to discover the values 1145eda14cbcSMatt Macy * of the missing data. 1146eda14cbcSMatt Macy * 1147eda14cbcSMatt Macy * As is apparent from the example above, the only non-trivial rows in the 1148eda14cbcSMatt Macy * inverse matrix correspond to the data disks that we're trying to 1149eda14cbcSMatt Macy * reconstruct. Indeed, those are the only rows we need as the others would 1150eda14cbcSMatt Macy * only be useful for reconstructing data known or assumed to be valid. For 1151eda14cbcSMatt Macy * that reason, we only build the coefficients in the rows that correspond to 1152eda14cbcSMatt Macy * targeted columns. 1153eda14cbcSMatt Macy */ 1154eda14cbcSMatt Macy /* END CSTYLED */ 1155eda14cbcSMatt Macy 1156eda14cbcSMatt Macy static void 11577877fdebSMatt Macy vdev_raidz_matrix_init(raidz_row_t *rr, int n, int nmap, int *map, 1158eda14cbcSMatt Macy uint8_t **rows) 1159eda14cbcSMatt Macy { 1160eda14cbcSMatt Macy int i, j; 1161eda14cbcSMatt Macy int pow; 1162eda14cbcSMatt Macy 11637877fdebSMatt Macy ASSERT(n == rr->rr_cols - rr->rr_firstdatacol); 1164eda14cbcSMatt Macy 1165eda14cbcSMatt Macy /* 1166eda14cbcSMatt Macy * Fill in the missing rows of interest. 1167eda14cbcSMatt Macy */ 1168eda14cbcSMatt Macy for (i = 0; i < nmap; i++) { 1169eda14cbcSMatt Macy ASSERT3S(0, <=, map[i]); 1170eda14cbcSMatt Macy ASSERT3S(map[i], <=, 2); 1171eda14cbcSMatt Macy 1172eda14cbcSMatt Macy pow = map[i] * n; 1173eda14cbcSMatt Macy if (pow > 255) 1174eda14cbcSMatt Macy pow -= 255; 1175eda14cbcSMatt Macy ASSERT(pow <= 255); 1176eda14cbcSMatt Macy 1177eda14cbcSMatt Macy for (j = 0; j < n; j++) { 1178eda14cbcSMatt Macy pow -= map[i]; 1179eda14cbcSMatt Macy if (pow < 0) 1180eda14cbcSMatt Macy pow += 255; 1181eda14cbcSMatt Macy rows[i][j] = vdev_raidz_pow2[pow]; 1182eda14cbcSMatt Macy } 1183eda14cbcSMatt Macy } 1184eda14cbcSMatt Macy } 1185eda14cbcSMatt Macy 1186eda14cbcSMatt Macy static void 11877877fdebSMatt Macy vdev_raidz_matrix_invert(raidz_row_t *rr, int n, int nmissing, int *missing, 1188eda14cbcSMatt Macy uint8_t **rows, uint8_t **invrows, const uint8_t *used) 1189eda14cbcSMatt Macy { 1190eda14cbcSMatt Macy int i, j, ii, jj; 1191eda14cbcSMatt Macy uint8_t log; 1192eda14cbcSMatt Macy 1193eda14cbcSMatt Macy /* 1194eda14cbcSMatt Macy * Assert that the first nmissing entries from the array of used 1195eda14cbcSMatt Macy * columns correspond to parity columns and that subsequent entries 1196eda14cbcSMatt Macy * correspond to data columns. 1197eda14cbcSMatt Macy */ 1198eda14cbcSMatt Macy for (i = 0; i < nmissing; i++) { 11997877fdebSMatt Macy ASSERT3S(used[i], <, rr->rr_firstdatacol); 1200eda14cbcSMatt Macy } 1201eda14cbcSMatt Macy for (; i < n; i++) { 12027877fdebSMatt Macy ASSERT3S(used[i], >=, rr->rr_firstdatacol); 1203eda14cbcSMatt Macy } 1204eda14cbcSMatt Macy 1205eda14cbcSMatt Macy /* 1206eda14cbcSMatt Macy * First initialize the storage where we'll compute the inverse rows. 1207eda14cbcSMatt Macy */ 1208eda14cbcSMatt Macy for (i = 0; i < nmissing; i++) { 1209eda14cbcSMatt Macy for (j = 0; j < n; j++) { 1210eda14cbcSMatt Macy invrows[i][j] = (i == j) ? 1 : 0; 1211eda14cbcSMatt Macy } 1212eda14cbcSMatt Macy } 1213eda14cbcSMatt Macy 1214eda14cbcSMatt Macy /* 1215eda14cbcSMatt Macy * Subtract all trivial rows from the rows of consequence. 1216eda14cbcSMatt Macy */ 1217eda14cbcSMatt Macy for (i = 0; i < nmissing; i++) { 1218eda14cbcSMatt Macy for (j = nmissing; j < n; j++) { 12197877fdebSMatt Macy ASSERT3U(used[j], >=, rr->rr_firstdatacol); 12207877fdebSMatt Macy jj = used[j] - rr->rr_firstdatacol; 1221eda14cbcSMatt Macy ASSERT3S(jj, <, n); 1222eda14cbcSMatt Macy invrows[i][j] = rows[i][jj]; 1223eda14cbcSMatt Macy rows[i][jj] = 0; 1224eda14cbcSMatt Macy } 1225eda14cbcSMatt Macy } 1226eda14cbcSMatt Macy 1227eda14cbcSMatt Macy /* 1228eda14cbcSMatt Macy * For each of the rows of interest, we must normalize it and subtract 1229eda14cbcSMatt Macy * a multiple of it from the other rows. 1230eda14cbcSMatt Macy */ 1231eda14cbcSMatt Macy for (i = 0; i < nmissing; i++) { 1232eda14cbcSMatt Macy for (j = 0; j < missing[i]; j++) { 1233eda14cbcSMatt Macy ASSERT0(rows[i][j]); 1234eda14cbcSMatt Macy } 1235eda14cbcSMatt Macy ASSERT3U(rows[i][missing[i]], !=, 0); 1236eda14cbcSMatt Macy 1237eda14cbcSMatt Macy /* 1238eda14cbcSMatt Macy * Compute the inverse of the first element and multiply each 1239eda14cbcSMatt Macy * element in the row by that value. 1240eda14cbcSMatt Macy */ 1241eda14cbcSMatt Macy log = 255 - vdev_raidz_log2[rows[i][missing[i]]]; 1242eda14cbcSMatt Macy 1243eda14cbcSMatt Macy for (j = 0; j < n; j++) { 1244eda14cbcSMatt Macy rows[i][j] = vdev_raidz_exp2(rows[i][j], log); 1245eda14cbcSMatt Macy invrows[i][j] = vdev_raidz_exp2(invrows[i][j], log); 1246eda14cbcSMatt Macy } 1247eda14cbcSMatt Macy 1248eda14cbcSMatt Macy for (ii = 0; ii < nmissing; ii++) { 1249eda14cbcSMatt Macy if (i == ii) 1250eda14cbcSMatt Macy continue; 1251eda14cbcSMatt Macy 1252eda14cbcSMatt Macy ASSERT3U(rows[ii][missing[i]], !=, 0); 1253eda14cbcSMatt Macy 1254eda14cbcSMatt Macy log = vdev_raidz_log2[rows[ii][missing[i]]]; 1255eda14cbcSMatt Macy 1256eda14cbcSMatt Macy for (j = 0; j < n; j++) { 1257eda14cbcSMatt Macy rows[ii][j] ^= 1258eda14cbcSMatt Macy vdev_raidz_exp2(rows[i][j], log); 1259eda14cbcSMatt Macy invrows[ii][j] ^= 1260eda14cbcSMatt Macy vdev_raidz_exp2(invrows[i][j], log); 1261eda14cbcSMatt Macy } 1262eda14cbcSMatt Macy } 1263eda14cbcSMatt Macy } 1264eda14cbcSMatt Macy 1265eda14cbcSMatt Macy /* 1266eda14cbcSMatt Macy * Verify that the data that is left in the rows are properly part of 1267eda14cbcSMatt Macy * an identity matrix. 1268eda14cbcSMatt Macy */ 1269eda14cbcSMatt Macy for (i = 0; i < nmissing; i++) { 1270eda14cbcSMatt Macy for (j = 0; j < n; j++) { 1271eda14cbcSMatt Macy if (j == missing[i]) { 1272eda14cbcSMatt Macy ASSERT3U(rows[i][j], ==, 1); 1273eda14cbcSMatt Macy } else { 1274eda14cbcSMatt Macy ASSERT0(rows[i][j]); 1275eda14cbcSMatt Macy } 1276eda14cbcSMatt Macy } 1277eda14cbcSMatt Macy } 1278eda14cbcSMatt Macy } 1279eda14cbcSMatt Macy 1280eda14cbcSMatt Macy static void 12817877fdebSMatt Macy vdev_raidz_matrix_reconstruct(raidz_row_t *rr, int n, int nmissing, 1282eda14cbcSMatt Macy int *missing, uint8_t **invrows, const uint8_t *used) 1283eda14cbcSMatt Macy { 1284eda14cbcSMatt Macy int i, j, x, cc, c; 1285eda14cbcSMatt Macy uint8_t *src; 1286eda14cbcSMatt Macy uint64_t ccount; 1287eda14cbcSMatt Macy uint8_t *dst[VDEV_RAIDZ_MAXPARITY] = { NULL }; 1288eda14cbcSMatt Macy uint64_t dcount[VDEV_RAIDZ_MAXPARITY] = { 0 }; 1289eda14cbcSMatt Macy uint8_t log = 0; 1290eda14cbcSMatt Macy uint8_t val; 1291eda14cbcSMatt Macy int ll; 1292eda14cbcSMatt Macy uint8_t *invlog[VDEV_RAIDZ_MAXPARITY]; 1293eda14cbcSMatt Macy uint8_t *p, *pp; 1294eda14cbcSMatt Macy size_t psize; 1295eda14cbcSMatt Macy 1296eda14cbcSMatt Macy psize = sizeof (invlog[0][0]) * n * nmissing; 1297eda14cbcSMatt Macy p = kmem_alloc(psize, KM_SLEEP); 1298eda14cbcSMatt Macy 1299eda14cbcSMatt Macy for (pp = p, i = 0; i < nmissing; i++) { 1300eda14cbcSMatt Macy invlog[i] = pp; 1301eda14cbcSMatt Macy pp += n; 1302eda14cbcSMatt Macy } 1303eda14cbcSMatt Macy 1304eda14cbcSMatt Macy for (i = 0; i < nmissing; i++) { 1305eda14cbcSMatt Macy for (j = 0; j < n; j++) { 1306eda14cbcSMatt Macy ASSERT3U(invrows[i][j], !=, 0); 1307eda14cbcSMatt Macy invlog[i][j] = vdev_raidz_log2[invrows[i][j]]; 1308eda14cbcSMatt Macy } 1309eda14cbcSMatt Macy } 1310eda14cbcSMatt Macy 1311eda14cbcSMatt Macy for (i = 0; i < n; i++) { 1312eda14cbcSMatt Macy c = used[i]; 13137877fdebSMatt Macy ASSERT3U(c, <, rr->rr_cols); 1314eda14cbcSMatt Macy 13157877fdebSMatt Macy ccount = rr->rr_col[c].rc_size; 13167877fdebSMatt Macy ASSERT(ccount >= rr->rr_col[missing[0]].rc_size || i > 0); 13177877fdebSMatt Macy if (ccount == 0) 13187877fdebSMatt Macy continue; 13197877fdebSMatt Macy src = abd_to_buf(rr->rr_col[c].rc_abd); 1320eda14cbcSMatt Macy for (j = 0; j < nmissing; j++) { 13217877fdebSMatt Macy cc = missing[j] + rr->rr_firstdatacol; 13227877fdebSMatt Macy ASSERT3U(cc, >=, rr->rr_firstdatacol); 13237877fdebSMatt Macy ASSERT3U(cc, <, rr->rr_cols); 1324eda14cbcSMatt Macy ASSERT3U(cc, !=, c); 1325eda14cbcSMatt Macy 13267877fdebSMatt Macy dcount[j] = rr->rr_col[cc].rc_size; 13277877fdebSMatt Macy if (dcount[j] != 0) 13287877fdebSMatt Macy dst[j] = abd_to_buf(rr->rr_col[cc].rc_abd); 1329eda14cbcSMatt Macy } 1330eda14cbcSMatt Macy 1331eda14cbcSMatt Macy for (x = 0; x < ccount; x++, src++) { 1332eda14cbcSMatt Macy if (*src != 0) 1333eda14cbcSMatt Macy log = vdev_raidz_log2[*src]; 1334eda14cbcSMatt Macy 1335eda14cbcSMatt Macy for (cc = 0; cc < nmissing; cc++) { 1336eda14cbcSMatt Macy if (x >= dcount[cc]) 1337eda14cbcSMatt Macy continue; 1338eda14cbcSMatt Macy 1339eda14cbcSMatt Macy if (*src == 0) { 1340eda14cbcSMatt Macy val = 0; 1341eda14cbcSMatt Macy } else { 1342eda14cbcSMatt Macy if ((ll = log + invlog[cc][i]) >= 255) 1343eda14cbcSMatt Macy ll -= 255; 1344eda14cbcSMatt Macy val = vdev_raidz_pow2[ll]; 1345eda14cbcSMatt Macy } 1346eda14cbcSMatt Macy 1347eda14cbcSMatt Macy if (i == 0) 1348eda14cbcSMatt Macy dst[cc][x] = val; 1349eda14cbcSMatt Macy else 1350eda14cbcSMatt Macy dst[cc][x] ^= val; 1351eda14cbcSMatt Macy } 1352eda14cbcSMatt Macy } 1353eda14cbcSMatt Macy } 1354eda14cbcSMatt Macy 1355eda14cbcSMatt Macy kmem_free(p, psize); 1356eda14cbcSMatt Macy } 1357eda14cbcSMatt Macy 1358eda14cbcSMatt Macy static int 13597877fdebSMatt Macy vdev_raidz_reconstruct_general(raidz_row_t *rr, int *tgts, int ntgts) 1360eda14cbcSMatt Macy { 1361eda14cbcSMatt Macy int n, i, c, t, tt; 1362eda14cbcSMatt Macy int nmissing_rows; 1363eda14cbcSMatt Macy int missing_rows[VDEV_RAIDZ_MAXPARITY]; 1364eda14cbcSMatt Macy int parity_map[VDEV_RAIDZ_MAXPARITY]; 1365eda14cbcSMatt Macy uint8_t *p, *pp; 1366eda14cbcSMatt Macy size_t psize; 1367eda14cbcSMatt Macy uint8_t *rows[VDEV_RAIDZ_MAXPARITY]; 1368eda14cbcSMatt Macy uint8_t *invrows[VDEV_RAIDZ_MAXPARITY]; 1369eda14cbcSMatt Macy uint8_t *used; 1370eda14cbcSMatt Macy 1371eda14cbcSMatt Macy abd_t **bufs = NULL; 1372eda14cbcSMatt Macy 1373eda14cbcSMatt Macy int code = 0; 1374eda14cbcSMatt Macy 1375eda14cbcSMatt Macy /* 1376eda14cbcSMatt Macy * Matrix reconstruction can't use scatter ABDs yet, so we allocate 13777877fdebSMatt Macy * temporary linear ABDs if any non-linear ABDs are found. 1378eda14cbcSMatt Macy */ 13797877fdebSMatt Macy for (i = rr->rr_firstdatacol; i < rr->rr_cols; i++) { 13807877fdebSMatt Macy if (!abd_is_linear(rr->rr_col[i].rc_abd)) { 13817877fdebSMatt Macy bufs = kmem_alloc(rr->rr_cols * sizeof (abd_t *), 13827877fdebSMatt Macy KM_PUSHPAGE); 1383eda14cbcSMatt Macy 13847877fdebSMatt Macy for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 13857877fdebSMatt Macy raidz_col_t *col = &rr->rr_col[c]; 1386eda14cbcSMatt Macy 1387eda14cbcSMatt Macy bufs[c] = col->rc_abd; 13887877fdebSMatt Macy if (bufs[c] != NULL) { 13897877fdebSMatt Macy col->rc_abd = abd_alloc_linear( 13907877fdebSMatt Macy col->rc_size, B_TRUE); 13917877fdebSMatt Macy abd_copy(col->rc_abd, bufs[c], 13927877fdebSMatt Macy col->rc_size); 1393eda14cbcSMatt Macy } 1394eda14cbcSMatt Macy } 1395eda14cbcSMatt Macy 13967877fdebSMatt Macy break; 13977877fdebSMatt Macy } 13987877fdebSMatt Macy } 13997877fdebSMatt Macy 14007877fdebSMatt Macy n = rr->rr_cols - rr->rr_firstdatacol; 1401eda14cbcSMatt Macy 1402eda14cbcSMatt Macy /* 1403eda14cbcSMatt Macy * Figure out which data columns are missing. 1404eda14cbcSMatt Macy */ 1405eda14cbcSMatt Macy nmissing_rows = 0; 1406eda14cbcSMatt Macy for (t = 0; t < ntgts; t++) { 14077877fdebSMatt Macy if (tgts[t] >= rr->rr_firstdatacol) { 1408eda14cbcSMatt Macy missing_rows[nmissing_rows++] = 14097877fdebSMatt Macy tgts[t] - rr->rr_firstdatacol; 1410eda14cbcSMatt Macy } 1411eda14cbcSMatt Macy } 1412eda14cbcSMatt Macy 1413eda14cbcSMatt Macy /* 1414eda14cbcSMatt Macy * Figure out which parity columns to use to help generate the missing 1415eda14cbcSMatt Macy * data columns. 1416eda14cbcSMatt Macy */ 1417eda14cbcSMatt Macy for (tt = 0, c = 0, i = 0; i < nmissing_rows; c++) { 1418eda14cbcSMatt Macy ASSERT(tt < ntgts); 14197877fdebSMatt Macy ASSERT(c < rr->rr_firstdatacol); 1420eda14cbcSMatt Macy 1421eda14cbcSMatt Macy /* 1422eda14cbcSMatt Macy * Skip any targeted parity columns. 1423eda14cbcSMatt Macy */ 1424eda14cbcSMatt Macy if (c == tgts[tt]) { 1425eda14cbcSMatt Macy tt++; 1426eda14cbcSMatt Macy continue; 1427eda14cbcSMatt Macy } 1428eda14cbcSMatt Macy 1429eda14cbcSMatt Macy code |= 1 << c; 1430eda14cbcSMatt Macy 1431eda14cbcSMatt Macy parity_map[i] = c; 1432eda14cbcSMatt Macy i++; 1433eda14cbcSMatt Macy } 1434eda14cbcSMatt Macy 1435eda14cbcSMatt Macy ASSERT(code != 0); 1436eda14cbcSMatt Macy ASSERT3U(code, <, 1 << VDEV_RAIDZ_MAXPARITY); 1437eda14cbcSMatt Macy 1438eda14cbcSMatt Macy psize = (sizeof (rows[0][0]) + sizeof (invrows[0][0])) * 1439eda14cbcSMatt Macy nmissing_rows * n + sizeof (used[0]) * n; 1440eda14cbcSMatt Macy p = kmem_alloc(psize, KM_SLEEP); 1441eda14cbcSMatt Macy 1442eda14cbcSMatt Macy for (pp = p, i = 0; i < nmissing_rows; i++) { 1443eda14cbcSMatt Macy rows[i] = pp; 1444eda14cbcSMatt Macy pp += n; 1445eda14cbcSMatt Macy invrows[i] = pp; 1446eda14cbcSMatt Macy pp += n; 1447eda14cbcSMatt Macy } 1448eda14cbcSMatt Macy used = pp; 1449eda14cbcSMatt Macy 1450eda14cbcSMatt Macy for (i = 0; i < nmissing_rows; i++) { 1451eda14cbcSMatt Macy used[i] = parity_map[i]; 1452eda14cbcSMatt Macy } 1453eda14cbcSMatt Macy 14547877fdebSMatt Macy for (tt = 0, c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 1455eda14cbcSMatt Macy if (tt < nmissing_rows && 14567877fdebSMatt Macy c == missing_rows[tt] + rr->rr_firstdatacol) { 1457eda14cbcSMatt Macy tt++; 1458eda14cbcSMatt Macy continue; 1459eda14cbcSMatt Macy } 1460eda14cbcSMatt Macy 1461eda14cbcSMatt Macy ASSERT3S(i, <, n); 1462eda14cbcSMatt Macy used[i] = c; 1463eda14cbcSMatt Macy i++; 1464eda14cbcSMatt Macy } 1465eda14cbcSMatt Macy 1466eda14cbcSMatt Macy /* 1467eda14cbcSMatt Macy * Initialize the interesting rows of the matrix. 1468eda14cbcSMatt Macy */ 14697877fdebSMatt Macy vdev_raidz_matrix_init(rr, n, nmissing_rows, parity_map, rows); 1470eda14cbcSMatt Macy 1471eda14cbcSMatt Macy /* 1472eda14cbcSMatt Macy * Invert the matrix. 1473eda14cbcSMatt Macy */ 14747877fdebSMatt Macy vdev_raidz_matrix_invert(rr, n, nmissing_rows, missing_rows, rows, 1475eda14cbcSMatt Macy invrows, used); 1476eda14cbcSMatt Macy 1477eda14cbcSMatt Macy /* 1478eda14cbcSMatt Macy * Reconstruct the missing data using the generated matrix. 1479eda14cbcSMatt Macy */ 14807877fdebSMatt Macy vdev_raidz_matrix_reconstruct(rr, n, nmissing_rows, missing_rows, 1481eda14cbcSMatt Macy invrows, used); 1482eda14cbcSMatt Macy 1483eda14cbcSMatt Macy kmem_free(p, psize); 1484eda14cbcSMatt Macy 1485eda14cbcSMatt Macy /* 1486eda14cbcSMatt Macy * copy back from temporary linear abds and free them 1487eda14cbcSMatt Macy */ 1488eda14cbcSMatt Macy if (bufs) { 14897877fdebSMatt Macy for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 14907877fdebSMatt Macy raidz_col_t *col = &rr->rr_col[c]; 1491eda14cbcSMatt Macy 14927877fdebSMatt Macy if (bufs[c] != NULL) { 1493eda14cbcSMatt Macy abd_copy(bufs[c], col->rc_abd, col->rc_size); 1494eda14cbcSMatt Macy abd_free(col->rc_abd); 14957877fdebSMatt Macy } 1496eda14cbcSMatt Macy col->rc_abd = bufs[c]; 1497eda14cbcSMatt Macy } 14987877fdebSMatt Macy kmem_free(bufs, rr->rr_cols * sizeof (abd_t *)); 1499eda14cbcSMatt Macy } 1500eda14cbcSMatt Macy 1501eda14cbcSMatt Macy return (code); 1502eda14cbcSMatt Macy } 1503eda14cbcSMatt Macy 15047877fdebSMatt Macy static int 15057877fdebSMatt Macy vdev_raidz_reconstruct_row(raidz_map_t *rm, raidz_row_t *rr, 15067877fdebSMatt Macy const int *t, int nt) 1507eda14cbcSMatt Macy { 1508eda14cbcSMatt Macy int tgts[VDEV_RAIDZ_MAXPARITY], *dt; 1509eda14cbcSMatt Macy int ntgts; 1510eda14cbcSMatt Macy int i, c, ret; 1511eda14cbcSMatt Macy int code; 1512eda14cbcSMatt Macy int nbadparity, nbaddata; 1513eda14cbcSMatt Macy int parity_valid[VDEV_RAIDZ_MAXPARITY]; 1514eda14cbcSMatt Macy 15157877fdebSMatt Macy nbadparity = rr->rr_firstdatacol; 15167877fdebSMatt Macy nbaddata = rr->rr_cols - nbadparity; 1517eda14cbcSMatt Macy ntgts = 0; 15187877fdebSMatt Macy for (i = 0, c = 0; c < rr->rr_cols; c++) { 15197877fdebSMatt Macy if (c < rr->rr_firstdatacol) 1520eda14cbcSMatt Macy parity_valid[c] = B_FALSE; 1521eda14cbcSMatt Macy 1522eda14cbcSMatt Macy if (i < nt && c == t[i]) { 1523eda14cbcSMatt Macy tgts[ntgts++] = c; 1524eda14cbcSMatt Macy i++; 15257877fdebSMatt Macy } else if (rr->rr_col[c].rc_error != 0) { 1526eda14cbcSMatt Macy tgts[ntgts++] = c; 15277877fdebSMatt Macy } else if (c >= rr->rr_firstdatacol) { 1528eda14cbcSMatt Macy nbaddata--; 1529eda14cbcSMatt Macy } else { 1530eda14cbcSMatt Macy parity_valid[c] = B_TRUE; 1531eda14cbcSMatt Macy nbadparity--; 1532eda14cbcSMatt Macy } 1533eda14cbcSMatt Macy } 1534eda14cbcSMatt Macy 1535eda14cbcSMatt Macy ASSERT(ntgts >= nt); 1536eda14cbcSMatt Macy ASSERT(nbaddata >= 0); 1537eda14cbcSMatt Macy ASSERT(nbaddata + nbadparity == ntgts); 1538eda14cbcSMatt Macy 1539eda14cbcSMatt Macy dt = &tgts[nbadparity]; 1540eda14cbcSMatt Macy 1541eda14cbcSMatt Macy /* Reconstruct using the new math implementation */ 15427877fdebSMatt Macy ret = vdev_raidz_math_reconstruct(rm, rr, parity_valid, dt, nbaddata); 1543eda14cbcSMatt Macy if (ret != RAIDZ_ORIGINAL_IMPL) 1544eda14cbcSMatt Macy return (ret); 1545eda14cbcSMatt Macy 1546eda14cbcSMatt Macy /* 1547eda14cbcSMatt Macy * See if we can use any of our optimized reconstruction routines. 1548eda14cbcSMatt Macy */ 1549eda14cbcSMatt Macy switch (nbaddata) { 1550eda14cbcSMatt Macy case 1: 1551eda14cbcSMatt Macy if (parity_valid[VDEV_RAIDZ_P]) 15527877fdebSMatt Macy return (vdev_raidz_reconstruct_p(rr, dt, 1)); 1553eda14cbcSMatt Macy 15547877fdebSMatt Macy ASSERT(rr->rr_firstdatacol > 1); 1555eda14cbcSMatt Macy 1556eda14cbcSMatt Macy if (parity_valid[VDEV_RAIDZ_Q]) 15577877fdebSMatt Macy return (vdev_raidz_reconstruct_q(rr, dt, 1)); 1558eda14cbcSMatt Macy 15597877fdebSMatt Macy ASSERT(rr->rr_firstdatacol > 2); 1560eda14cbcSMatt Macy break; 1561eda14cbcSMatt Macy 1562eda14cbcSMatt Macy case 2: 15637877fdebSMatt Macy ASSERT(rr->rr_firstdatacol > 1); 1564eda14cbcSMatt Macy 1565eda14cbcSMatt Macy if (parity_valid[VDEV_RAIDZ_P] && 1566eda14cbcSMatt Macy parity_valid[VDEV_RAIDZ_Q]) 15677877fdebSMatt Macy return (vdev_raidz_reconstruct_pq(rr, dt, 2)); 1568eda14cbcSMatt Macy 15697877fdebSMatt Macy ASSERT(rr->rr_firstdatacol > 2); 1570eda14cbcSMatt Macy 1571eda14cbcSMatt Macy break; 1572eda14cbcSMatt Macy } 1573eda14cbcSMatt Macy 15747877fdebSMatt Macy code = vdev_raidz_reconstruct_general(rr, tgts, ntgts); 1575eda14cbcSMatt Macy ASSERT(code < (1 << VDEV_RAIDZ_MAXPARITY)); 1576eda14cbcSMatt Macy ASSERT(code > 0); 1577eda14cbcSMatt Macy return (code); 1578eda14cbcSMatt Macy } 1579eda14cbcSMatt Macy 1580eda14cbcSMatt Macy static int 1581eda14cbcSMatt Macy vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize, 1582eda14cbcSMatt Macy uint64_t *logical_ashift, uint64_t *physical_ashift) 1583eda14cbcSMatt Macy { 15847877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 15857877fdebSMatt Macy uint64_t nparity = vdrz->vd_nparity; 1586eda14cbcSMatt Macy int c; 1587eda14cbcSMatt Macy int lasterror = 0; 1588eda14cbcSMatt Macy int numerrors = 0; 1589eda14cbcSMatt Macy 1590eda14cbcSMatt Macy ASSERT(nparity > 0); 1591eda14cbcSMatt Macy 1592eda14cbcSMatt Macy if (nparity > VDEV_RAIDZ_MAXPARITY || 1593eda14cbcSMatt Macy vd->vdev_children < nparity + 1) { 1594eda14cbcSMatt Macy vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 1595eda14cbcSMatt Macy return (SET_ERROR(EINVAL)); 1596eda14cbcSMatt Macy } 1597eda14cbcSMatt Macy 1598eda14cbcSMatt Macy vdev_open_children(vd); 1599eda14cbcSMatt Macy 1600eda14cbcSMatt Macy for (c = 0; c < vd->vdev_children; c++) { 16017877fdebSMatt Macy vdev_t *cvd = vd->vdev_child[c]; 1602eda14cbcSMatt Macy 1603eda14cbcSMatt Macy if (cvd->vdev_open_error != 0) { 1604eda14cbcSMatt Macy lasterror = cvd->vdev_open_error; 1605eda14cbcSMatt Macy numerrors++; 1606eda14cbcSMatt Macy continue; 1607eda14cbcSMatt Macy } 1608eda14cbcSMatt Macy 1609eda14cbcSMatt Macy *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1; 1610eda14cbcSMatt Macy *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1; 1611eda14cbcSMatt Macy *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift); 1612eda14cbcSMatt Macy *physical_ashift = MAX(*physical_ashift, 1613eda14cbcSMatt Macy cvd->vdev_physical_ashift); 1614eda14cbcSMatt Macy } 1615eda14cbcSMatt Macy 1616eda14cbcSMatt Macy *asize *= vd->vdev_children; 1617eda14cbcSMatt Macy *max_asize *= vd->vdev_children; 1618eda14cbcSMatt Macy 1619eda14cbcSMatt Macy if (numerrors > nparity) { 1620eda14cbcSMatt Macy vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; 1621eda14cbcSMatt Macy return (lasterror); 1622eda14cbcSMatt Macy } 1623eda14cbcSMatt Macy 1624eda14cbcSMatt Macy return (0); 1625eda14cbcSMatt Macy } 1626eda14cbcSMatt Macy 1627eda14cbcSMatt Macy static void 1628eda14cbcSMatt Macy vdev_raidz_close(vdev_t *vd) 1629eda14cbcSMatt Macy { 16307877fdebSMatt Macy for (int c = 0; c < vd->vdev_children; c++) { 16317877fdebSMatt Macy if (vd->vdev_child[c] != NULL) 1632eda14cbcSMatt Macy vdev_close(vd->vdev_child[c]); 1633eda14cbcSMatt Macy } 16347877fdebSMatt Macy } 1635eda14cbcSMatt Macy 1636eda14cbcSMatt Macy static uint64_t 1637eda14cbcSMatt Macy vdev_raidz_asize(vdev_t *vd, uint64_t psize) 1638eda14cbcSMatt Macy { 16397877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 1640eda14cbcSMatt Macy uint64_t asize; 1641eda14cbcSMatt Macy uint64_t ashift = vd->vdev_top->vdev_ashift; 16427877fdebSMatt Macy uint64_t cols = vdrz->vd_logical_width; 16437877fdebSMatt Macy uint64_t nparity = vdrz->vd_nparity; 1644eda14cbcSMatt Macy 1645eda14cbcSMatt Macy asize = ((psize - 1) >> ashift) + 1; 1646eda14cbcSMatt Macy asize += nparity * ((asize + cols - nparity - 1) / (cols - nparity)); 1647eda14cbcSMatt Macy asize = roundup(asize, nparity + 1) << ashift; 1648eda14cbcSMatt Macy 1649eda14cbcSMatt Macy return (asize); 1650eda14cbcSMatt Macy } 1651eda14cbcSMatt Macy 16527877fdebSMatt Macy /* 16537877fdebSMatt Macy * The allocatable space for a raidz vdev is N * sizeof(smallest child) 16547877fdebSMatt Macy * so each child must provide at least 1/Nth of its asize. 16557877fdebSMatt Macy */ 16567877fdebSMatt Macy static uint64_t 16577877fdebSMatt Macy vdev_raidz_min_asize(vdev_t *vd) 16587877fdebSMatt Macy { 16597877fdebSMatt Macy return ((vd->vdev_min_asize + vd->vdev_children - 1) / 16607877fdebSMatt Macy vd->vdev_children); 16617877fdebSMatt Macy } 16627877fdebSMatt Macy 16637877fdebSMatt Macy void 1664eda14cbcSMatt Macy vdev_raidz_child_done(zio_t *zio) 1665eda14cbcSMatt Macy { 1666eda14cbcSMatt Macy raidz_col_t *rc = zio->io_private; 1667eda14cbcSMatt Macy 1668eda14cbcSMatt Macy rc->rc_error = zio->io_error; 1669eda14cbcSMatt Macy rc->rc_tried = 1; 1670eda14cbcSMatt Macy rc->rc_skipped = 0; 1671eda14cbcSMatt Macy } 1672eda14cbcSMatt Macy 1673eda14cbcSMatt Macy static void 16747877fdebSMatt Macy vdev_raidz_io_verify(vdev_t *vd, raidz_row_t *rr, int col) 1675eda14cbcSMatt Macy { 1676eda14cbcSMatt Macy #ifdef ZFS_DEBUG 1677eda14cbcSMatt Macy vdev_t *tvd = vd->vdev_top; 1678eda14cbcSMatt Macy 16797877fdebSMatt Macy range_seg64_t logical_rs, physical_rs, remain_rs; 16807877fdebSMatt Macy logical_rs.rs_start = rr->rr_offset; 1681eda14cbcSMatt Macy logical_rs.rs_end = logical_rs.rs_start + 16827877fdebSMatt Macy vdev_raidz_asize(vd, rr->rr_size); 1683eda14cbcSMatt Macy 16847877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[col]; 1685eda14cbcSMatt Macy vdev_t *cvd = vd->vdev_child[rc->rc_devidx]; 1686eda14cbcSMatt Macy 16877877fdebSMatt Macy vdev_xlate(cvd, &logical_rs, &physical_rs, &remain_rs); 16887877fdebSMatt Macy ASSERT(vdev_xlate_is_empty(&remain_rs)); 1689eda14cbcSMatt Macy ASSERT3U(rc->rc_offset, ==, physical_rs.rs_start); 1690eda14cbcSMatt Macy ASSERT3U(rc->rc_offset, <, physical_rs.rs_end); 1691eda14cbcSMatt Macy /* 1692eda14cbcSMatt Macy * It would be nice to assert that rs_end is equal 1693eda14cbcSMatt Macy * to rc_offset + rc_size but there might be an 1694eda14cbcSMatt Macy * optional I/O at the end that is not accounted in 1695eda14cbcSMatt Macy * rc_size. 1696eda14cbcSMatt Macy */ 1697eda14cbcSMatt Macy if (physical_rs.rs_end > rc->rc_offset + rc->rc_size) { 1698eda14cbcSMatt Macy ASSERT3U(physical_rs.rs_end, ==, rc->rc_offset + 1699eda14cbcSMatt Macy rc->rc_size + (1 << tvd->vdev_ashift)); 1700eda14cbcSMatt Macy } else { 1701eda14cbcSMatt Macy ASSERT3U(physical_rs.rs_end, ==, rc->rc_offset + rc->rc_size); 1702eda14cbcSMatt Macy } 1703eda14cbcSMatt Macy #endif 1704eda14cbcSMatt Macy } 1705eda14cbcSMatt Macy 17067877fdebSMatt Macy static void 17077877fdebSMatt Macy vdev_raidz_io_start_write(zio_t *zio, raidz_row_t *rr, uint64_t ashift) 17087877fdebSMatt Macy { 17097877fdebSMatt Macy vdev_t *vd = zio->io_vd; 17107877fdebSMatt Macy raidz_map_t *rm = zio->io_vsd; 17117877fdebSMatt Macy int c, i; 17127877fdebSMatt Macy 17137877fdebSMatt Macy vdev_raidz_generate_parity_row(rm, rr); 17147877fdebSMatt Macy 17157877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 17167877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 17177877fdebSMatt Macy if (rc->rc_size == 0) 17187877fdebSMatt Macy continue; 17197877fdebSMatt Macy 17207877fdebSMatt Macy /* Verify physical to logical translation */ 17217877fdebSMatt Macy vdev_raidz_io_verify(vd, rr, c); 17227877fdebSMatt Macy 17237877fdebSMatt Macy zio_nowait(zio_vdev_child_io(zio, NULL, 17247877fdebSMatt Macy vd->vdev_child[rc->rc_devidx], rc->rc_offset, 17257877fdebSMatt Macy rc->rc_abd, rc->rc_size, zio->io_type, zio->io_priority, 17267877fdebSMatt Macy 0, vdev_raidz_child_done, rc)); 17277877fdebSMatt Macy } 17287877fdebSMatt Macy 17297877fdebSMatt Macy /* 17307877fdebSMatt Macy * Generate optional I/Os for skip sectors to improve aggregation 17317877fdebSMatt Macy * contiguity. 17327877fdebSMatt Macy */ 17337877fdebSMatt Macy for (c = rm->rm_skipstart, i = 0; i < rm->rm_nskip; c++, i++) { 17347877fdebSMatt Macy ASSERT(c <= rr->rr_scols); 17357877fdebSMatt Macy if (c == rr->rr_scols) 17367877fdebSMatt Macy c = 0; 17377877fdebSMatt Macy 17387877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 17397877fdebSMatt Macy vdev_t *cvd = vd->vdev_child[rc->rc_devidx]; 17407877fdebSMatt Macy 17417877fdebSMatt Macy zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 17427877fdebSMatt Macy rc->rc_offset + rc->rc_size, NULL, 1ULL << ashift, 17437877fdebSMatt Macy zio->io_type, zio->io_priority, 17447877fdebSMatt Macy ZIO_FLAG_NODATA | ZIO_FLAG_OPTIONAL, NULL, NULL)); 17457877fdebSMatt Macy } 17467877fdebSMatt Macy } 17477877fdebSMatt Macy 17487877fdebSMatt Macy static void 17497877fdebSMatt Macy vdev_raidz_io_start_read(zio_t *zio, raidz_row_t *rr) 17507877fdebSMatt Macy { 17517877fdebSMatt Macy vdev_t *vd = zio->io_vd; 17527877fdebSMatt Macy 17537877fdebSMatt Macy /* 17547877fdebSMatt Macy * Iterate over the columns in reverse order so that we hit the parity 17557877fdebSMatt Macy * last -- any errors along the way will force us to read the parity. 17567877fdebSMatt Macy */ 17577877fdebSMatt Macy for (int c = rr->rr_cols - 1; c >= 0; c--) { 17587877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 17597877fdebSMatt Macy if (rc->rc_size == 0) 17607877fdebSMatt Macy continue; 17617877fdebSMatt Macy vdev_t *cvd = vd->vdev_child[rc->rc_devidx]; 17627877fdebSMatt Macy if (!vdev_readable(cvd)) { 17637877fdebSMatt Macy if (c >= rr->rr_firstdatacol) 17647877fdebSMatt Macy rr->rr_missingdata++; 17657877fdebSMatt Macy else 17667877fdebSMatt Macy rr->rr_missingparity++; 17677877fdebSMatt Macy rc->rc_error = SET_ERROR(ENXIO); 17687877fdebSMatt Macy rc->rc_tried = 1; /* don't even try */ 17697877fdebSMatt Macy rc->rc_skipped = 1; 17707877fdebSMatt Macy continue; 17717877fdebSMatt Macy } 17727877fdebSMatt Macy if (vdev_dtl_contains(cvd, DTL_MISSING, zio->io_txg, 1)) { 17737877fdebSMatt Macy if (c >= rr->rr_firstdatacol) 17747877fdebSMatt Macy rr->rr_missingdata++; 17757877fdebSMatt Macy else 17767877fdebSMatt Macy rr->rr_missingparity++; 17777877fdebSMatt Macy rc->rc_error = SET_ERROR(ESTALE); 17787877fdebSMatt Macy rc->rc_skipped = 1; 17797877fdebSMatt Macy continue; 17807877fdebSMatt Macy } 17817877fdebSMatt Macy if (c >= rr->rr_firstdatacol || rr->rr_missingdata > 0 || 17827877fdebSMatt Macy (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) { 17837877fdebSMatt Macy zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 17847877fdebSMatt Macy rc->rc_offset, rc->rc_abd, rc->rc_size, 17857877fdebSMatt Macy zio->io_type, zio->io_priority, 0, 17867877fdebSMatt Macy vdev_raidz_child_done, rc)); 17877877fdebSMatt Macy } 17887877fdebSMatt Macy } 17897877fdebSMatt Macy } 17907877fdebSMatt Macy 1791eda14cbcSMatt Macy /* 1792eda14cbcSMatt Macy * Start an IO operation on a RAIDZ VDev 1793eda14cbcSMatt Macy * 1794eda14cbcSMatt Macy * Outline: 1795eda14cbcSMatt Macy * - For write operations: 1796eda14cbcSMatt Macy * 1. Generate the parity data 1797eda14cbcSMatt Macy * 2. Create child zio write operations to each column's vdev, for both 1798eda14cbcSMatt Macy * data and parity. 1799eda14cbcSMatt Macy * 3. If the column skips any sectors for padding, create optional dummy 1800eda14cbcSMatt Macy * write zio children for those areas to improve aggregation continuity. 1801eda14cbcSMatt Macy * - For read operations: 1802eda14cbcSMatt Macy * 1. Create child zio read operations to each data column's vdev to read 1803eda14cbcSMatt Macy * the range of data required for zio. 1804eda14cbcSMatt Macy * 2. If this is a scrub or resilver operation, or if any of the data 1805eda14cbcSMatt Macy * vdevs have had errors, then create zio read operations to the parity 1806eda14cbcSMatt Macy * columns' VDevs as well. 1807eda14cbcSMatt Macy */ 1808eda14cbcSMatt Macy static void 1809eda14cbcSMatt Macy vdev_raidz_io_start(zio_t *zio) 1810eda14cbcSMatt Macy { 1811eda14cbcSMatt Macy vdev_t *vd = zio->io_vd; 1812eda14cbcSMatt Macy vdev_t *tvd = vd->vdev_top; 18137877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 1814eda14cbcSMatt Macy raidz_map_t *rm; 1815eda14cbcSMatt Macy 18167877fdebSMatt Macy rm = vdev_raidz_map_alloc(zio, tvd->vdev_ashift, 18177877fdebSMatt Macy vdrz->vd_logical_width, vdrz->vd_nparity); 1818eda14cbcSMatt Macy 18197877fdebSMatt Macy /* 18207877fdebSMatt Macy * Until raidz expansion is implemented all maps for a raidz vdev 18217877fdebSMatt Macy * contain a single row. 18227877fdebSMatt Macy */ 18237877fdebSMatt Macy ASSERT3U(rm->rm_nrows, ==, 1); 18247877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[0]; 18257877fdebSMatt Macy 18267877fdebSMatt Macy zio->io_vsd = rm; 18277877fdebSMatt Macy zio->io_vsd_ops = &vdev_raidz_vsd_ops; 1828eda14cbcSMatt Macy 1829eda14cbcSMatt Macy if (zio->io_type == ZIO_TYPE_WRITE) { 18307877fdebSMatt Macy vdev_raidz_io_start_write(zio, rr, tvd->vdev_ashift); 18317877fdebSMatt Macy } else { 1832eda14cbcSMatt Macy ASSERT(zio->io_type == ZIO_TYPE_READ); 18337877fdebSMatt Macy vdev_raidz_io_start_read(zio, rr); 1834eda14cbcSMatt Macy } 1835eda14cbcSMatt Macy 1836eda14cbcSMatt Macy zio_execute(zio); 1837eda14cbcSMatt Macy } 1838eda14cbcSMatt Macy 1839eda14cbcSMatt Macy /* 1840eda14cbcSMatt Macy * Report a checksum error for a child of a RAID-Z device. 1841eda14cbcSMatt Macy */ 1842eda14cbcSMatt Macy static void 1843eda14cbcSMatt Macy raidz_checksum_error(zio_t *zio, raidz_col_t *rc, abd_t *bad_data) 1844eda14cbcSMatt Macy { 1845eda14cbcSMatt Macy vdev_t *vd = zio->io_vd->vdev_child[rc->rc_devidx]; 1846eda14cbcSMatt Macy 18477877fdebSMatt Macy if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE) && 18487877fdebSMatt Macy zio->io_priority != ZIO_PRIORITY_REBUILD) { 1849eda14cbcSMatt Macy zio_bad_cksum_t zbc; 1850eda14cbcSMatt Macy raidz_map_t *rm = zio->io_vsd; 1851eda14cbcSMatt Macy 1852eda14cbcSMatt Macy zbc.zbc_has_cksum = 0; 1853eda14cbcSMatt Macy zbc.zbc_injected = rm->rm_ecksuminjected; 1854eda14cbcSMatt Macy 1855*ba27dd8bSMartin Matuska (void) zfs_ereport_post_checksum(zio->io_spa, vd, 1856eda14cbcSMatt Macy &zio->io_bookmark, zio, rc->rc_offset, rc->rc_size, 1857eda14cbcSMatt Macy rc->rc_abd, bad_data, &zbc); 18582c48331dSMatt Macy mutex_enter(&vd->vdev_stat_lock); 18592c48331dSMatt Macy vd->vdev_stat.vs_checksum_errors++; 18602c48331dSMatt Macy mutex_exit(&vd->vdev_stat_lock); 18612c48331dSMatt Macy } 1862eda14cbcSMatt Macy } 1863eda14cbcSMatt Macy 1864eda14cbcSMatt Macy /* 1865eda14cbcSMatt Macy * We keep track of whether or not there were any injected errors, so that 1866eda14cbcSMatt Macy * any ereports we generate can note it. 1867eda14cbcSMatt Macy */ 1868eda14cbcSMatt Macy static int 1869eda14cbcSMatt Macy raidz_checksum_verify(zio_t *zio) 1870eda14cbcSMatt Macy { 1871eda14cbcSMatt Macy zio_bad_cksum_t zbc; 1872eda14cbcSMatt Macy raidz_map_t *rm = zio->io_vsd; 1873eda14cbcSMatt Macy 1874eda14cbcSMatt Macy bzero(&zbc, sizeof (zio_bad_cksum_t)); 1875eda14cbcSMatt Macy 1876eda14cbcSMatt Macy int ret = zio_checksum_error(zio, &zbc); 1877eda14cbcSMatt Macy if (ret != 0 && zbc.zbc_injected != 0) 1878eda14cbcSMatt Macy rm->rm_ecksuminjected = 1; 1879eda14cbcSMatt Macy 1880eda14cbcSMatt Macy return (ret); 1881eda14cbcSMatt Macy } 1882eda14cbcSMatt Macy 1883eda14cbcSMatt Macy /* 1884eda14cbcSMatt Macy * Generate the parity from the data columns. If we tried and were able to 1885eda14cbcSMatt Macy * read the parity without error, verify that the generated parity matches the 1886eda14cbcSMatt Macy * data we read. If it doesn't, we fire off a checksum error. Return the 18877877fdebSMatt Macy * number of such failures. 1888eda14cbcSMatt Macy */ 1889eda14cbcSMatt Macy static int 18907877fdebSMatt Macy raidz_parity_verify(zio_t *zio, raidz_row_t *rr) 1891eda14cbcSMatt Macy { 1892eda14cbcSMatt Macy abd_t *orig[VDEV_RAIDZ_MAXPARITY]; 1893eda14cbcSMatt Macy int c, ret = 0; 18947877fdebSMatt Macy raidz_map_t *rm = zio->io_vsd; 1895eda14cbcSMatt Macy raidz_col_t *rc; 1896eda14cbcSMatt Macy 1897eda14cbcSMatt Macy blkptr_t *bp = zio->io_bp; 1898eda14cbcSMatt Macy enum zio_checksum checksum = (bp == NULL ? zio->io_prop.zp_checksum : 1899eda14cbcSMatt Macy (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp))); 1900eda14cbcSMatt Macy 1901eda14cbcSMatt Macy if (checksum == ZIO_CHECKSUM_NOPARITY) 1902eda14cbcSMatt Macy return (ret); 1903eda14cbcSMatt Macy 19047877fdebSMatt Macy for (c = 0; c < rr->rr_firstdatacol; c++) { 19057877fdebSMatt Macy rc = &rr->rr_col[c]; 1906eda14cbcSMatt Macy if (!rc->rc_tried || rc->rc_error != 0) 1907eda14cbcSMatt Macy continue; 1908eda14cbcSMatt Macy 1909eda14cbcSMatt Macy orig[c] = abd_alloc_sametype(rc->rc_abd, rc->rc_size); 1910eda14cbcSMatt Macy abd_copy(orig[c], rc->rc_abd, rc->rc_size); 1911eda14cbcSMatt Macy } 1912eda14cbcSMatt Macy 19137877fdebSMatt Macy /* 19147877fdebSMatt Macy * Regenerates parity even for !tried||rc_error!=0 columns. This 19157877fdebSMatt Macy * isn't harmful but it does have the side effect of fixing stuff 19167877fdebSMatt Macy * we didn't realize was necessary (i.e. even if we return 0). 19177877fdebSMatt Macy */ 19187877fdebSMatt Macy vdev_raidz_generate_parity_row(rm, rr); 1919eda14cbcSMatt Macy 19207877fdebSMatt Macy for (c = 0; c < rr->rr_firstdatacol; c++) { 19217877fdebSMatt Macy rc = &rr->rr_col[c]; 19227877fdebSMatt Macy 1923eda14cbcSMatt Macy if (!rc->rc_tried || rc->rc_error != 0) 1924eda14cbcSMatt Macy continue; 19257877fdebSMatt Macy 1926eda14cbcSMatt Macy if (abd_cmp(orig[c], rc->rc_abd) != 0) { 1927eda14cbcSMatt Macy raidz_checksum_error(zio, rc, orig[c]); 1928eda14cbcSMatt Macy rc->rc_error = SET_ERROR(ECKSUM); 1929eda14cbcSMatt Macy ret++; 1930eda14cbcSMatt Macy } 1931eda14cbcSMatt Macy abd_free(orig[c]); 1932eda14cbcSMatt Macy } 1933eda14cbcSMatt Macy 1934eda14cbcSMatt Macy return (ret); 1935eda14cbcSMatt Macy } 1936eda14cbcSMatt Macy 1937eda14cbcSMatt Macy static int 19387877fdebSMatt Macy vdev_raidz_worst_error(raidz_row_t *rr) 1939eda14cbcSMatt Macy { 1940eda14cbcSMatt Macy int error = 0; 1941eda14cbcSMatt Macy 19427877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) 19437877fdebSMatt Macy error = zio_worst_error(error, rr->rr_col[c].rc_error); 1944eda14cbcSMatt Macy 1945eda14cbcSMatt Macy return (error); 1946eda14cbcSMatt Macy } 1947eda14cbcSMatt Macy 1948eda14cbcSMatt Macy static void 19497877fdebSMatt Macy vdev_raidz_io_done_verified(zio_t *zio, raidz_row_t *rr) 1950eda14cbcSMatt Macy { 1951eda14cbcSMatt Macy int unexpected_errors = 0; 1952eda14cbcSMatt Macy int parity_errors = 0; 1953eda14cbcSMatt Macy int parity_untried = 0; 1954eda14cbcSMatt Macy int data_errors = 0; 1955eda14cbcSMatt Macy 19567877fdebSMatt Macy ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ); 1957eda14cbcSMatt Macy 19587877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 19597877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 1960eda14cbcSMatt Macy 1961eda14cbcSMatt Macy if (rc->rc_error) { 19627877fdebSMatt Macy if (c < rr->rr_firstdatacol) 1963eda14cbcSMatt Macy parity_errors++; 1964eda14cbcSMatt Macy else 1965eda14cbcSMatt Macy data_errors++; 1966eda14cbcSMatt Macy 1967eda14cbcSMatt Macy if (!rc->rc_skipped) 1968eda14cbcSMatt Macy unexpected_errors++; 19697877fdebSMatt Macy } else if (c < rr->rr_firstdatacol && !rc->rc_tried) { 1970eda14cbcSMatt Macy parity_untried++; 1971eda14cbcSMatt Macy } 1972eda14cbcSMatt Macy } 1973eda14cbcSMatt Macy 1974eda14cbcSMatt Macy /* 19757877fdebSMatt Macy * If we read more parity disks than were used for 19767877fdebSMatt Macy * reconstruction, confirm that the other parity disks produced 19777877fdebSMatt Macy * correct data. 19787877fdebSMatt Macy * 19797877fdebSMatt Macy * Note that we also regenerate parity when resilvering so we 19807877fdebSMatt Macy * can write it out to failed devices later. 19817877fdebSMatt Macy */ 19827877fdebSMatt Macy if (parity_errors + parity_untried < 19837877fdebSMatt Macy rr->rr_firstdatacol - data_errors || 19847877fdebSMatt Macy (zio->io_flags & ZIO_FLAG_RESILVER)) { 19857877fdebSMatt Macy int n = raidz_parity_verify(zio, rr); 19867877fdebSMatt Macy unexpected_errors += n; 19877877fdebSMatt Macy ASSERT3U(parity_errors + n, <=, rr->rr_firstdatacol); 19887877fdebSMatt Macy } 19897877fdebSMatt Macy 19907877fdebSMatt Macy if (zio->io_error == 0 && spa_writeable(zio->io_spa) && 19917877fdebSMatt Macy (unexpected_errors > 0 || (zio->io_flags & ZIO_FLAG_RESILVER))) { 19927877fdebSMatt Macy /* 19937877fdebSMatt Macy * Use the good data we have in hand to repair damaged children. 19947877fdebSMatt Macy */ 19957877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 19967877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 19977877fdebSMatt Macy vdev_t *vd = zio->io_vd; 19987877fdebSMatt Macy vdev_t *cvd = vd->vdev_child[rc->rc_devidx]; 19997877fdebSMatt Macy 20007877fdebSMatt Macy if ((rc->rc_error == 0 || rc->rc_size == 0) && 20017877fdebSMatt Macy (rc->rc_repair == 0)) { 20027877fdebSMatt Macy continue; 20037877fdebSMatt Macy } 20047877fdebSMatt Macy 20057877fdebSMatt Macy zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 20067877fdebSMatt Macy rc->rc_offset, rc->rc_abd, rc->rc_size, 20077877fdebSMatt Macy ZIO_TYPE_WRITE, 20087877fdebSMatt Macy zio->io_priority == ZIO_PRIORITY_REBUILD ? 20097877fdebSMatt Macy ZIO_PRIORITY_REBUILD : ZIO_PRIORITY_ASYNC_WRITE, 20107877fdebSMatt Macy ZIO_FLAG_IO_REPAIR | (unexpected_errors ? 20117877fdebSMatt Macy ZIO_FLAG_SELF_HEAL : 0), NULL, NULL)); 20127877fdebSMatt Macy } 20137877fdebSMatt Macy } 20147877fdebSMatt Macy } 20157877fdebSMatt Macy 20167877fdebSMatt Macy static void 20177877fdebSMatt Macy raidz_restore_orig_data(raidz_map_t *rm) 20187877fdebSMatt Macy { 20197877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 20207877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 20217877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 20227877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 20237877fdebSMatt Macy if (rc->rc_need_orig_restore) { 20247877fdebSMatt Macy abd_copy_from_buf(rc->rc_abd, 20257877fdebSMatt Macy rc->rc_orig_data, rc->rc_size); 20267877fdebSMatt Macy rc->rc_need_orig_restore = B_FALSE; 20277877fdebSMatt Macy } 20287877fdebSMatt Macy } 20297877fdebSMatt Macy } 20307877fdebSMatt Macy } 20317877fdebSMatt Macy 20327877fdebSMatt Macy /* 20337877fdebSMatt Macy * returns EINVAL if reconstruction of the block will not be possible 20347877fdebSMatt Macy * returns ECKSUM if this specific reconstruction failed 20357877fdebSMatt Macy * returns 0 on successful reconstruction 20367877fdebSMatt Macy */ 20377877fdebSMatt Macy static int 20387877fdebSMatt Macy raidz_reconstruct(zio_t *zio, int *ltgts, int ntgts, int nparity) 20397877fdebSMatt Macy { 20407877fdebSMatt Macy raidz_map_t *rm = zio->io_vsd; 20417877fdebSMatt Macy 20427877fdebSMatt Macy /* Reconstruct each row */ 20437877fdebSMatt Macy for (int r = 0; r < rm->rm_nrows; r++) { 20447877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[r]; 20457877fdebSMatt Macy int my_tgts[VDEV_RAIDZ_MAXPARITY]; /* value is child id */ 20467877fdebSMatt Macy int t = 0; 20477877fdebSMatt Macy int dead = 0; 20487877fdebSMatt Macy int dead_data = 0; 20497877fdebSMatt Macy 20507877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 20517877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 20527877fdebSMatt Macy ASSERT0(rc->rc_need_orig_restore); 20537877fdebSMatt Macy if (rc->rc_error != 0) { 20547877fdebSMatt Macy dead++; 20557877fdebSMatt Macy if (c >= nparity) 20567877fdebSMatt Macy dead_data++; 20577877fdebSMatt Macy continue; 20587877fdebSMatt Macy } 20597877fdebSMatt Macy if (rc->rc_size == 0) 20607877fdebSMatt Macy continue; 20617877fdebSMatt Macy for (int lt = 0; lt < ntgts; lt++) { 20627877fdebSMatt Macy if (rc->rc_devidx == ltgts[lt]) { 20637877fdebSMatt Macy if (rc->rc_orig_data == NULL) { 20647877fdebSMatt Macy rc->rc_orig_data = 20657877fdebSMatt Macy zio_buf_alloc(rc->rc_size); 20667877fdebSMatt Macy abd_copy_to_buf( 20677877fdebSMatt Macy rc->rc_orig_data, 20687877fdebSMatt Macy rc->rc_abd, rc->rc_size); 20697877fdebSMatt Macy } 20707877fdebSMatt Macy rc->rc_need_orig_restore = B_TRUE; 20717877fdebSMatt Macy 20727877fdebSMatt Macy dead++; 20737877fdebSMatt Macy if (c >= nparity) 20747877fdebSMatt Macy dead_data++; 20757877fdebSMatt Macy my_tgts[t++] = c; 20767877fdebSMatt Macy break; 20777877fdebSMatt Macy } 20787877fdebSMatt Macy } 20797877fdebSMatt Macy } 20807877fdebSMatt Macy if (dead > nparity) { 20817877fdebSMatt Macy /* reconstruction not possible */ 20827877fdebSMatt Macy raidz_restore_orig_data(rm); 20837877fdebSMatt Macy return (EINVAL); 20847877fdebSMatt Macy } 20857877fdebSMatt Macy rr->rr_code = 0; 20867877fdebSMatt Macy if (dead_data > 0) 20877877fdebSMatt Macy rr->rr_code = vdev_raidz_reconstruct_row(rm, rr, 20887877fdebSMatt Macy my_tgts, t); 20897877fdebSMatt Macy } 20907877fdebSMatt Macy 20917877fdebSMatt Macy /* Check for success */ 20927877fdebSMatt Macy if (raidz_checksum_verify(zio) == 0) { 20937877fdebSMatt Macy 20947877fdebSMatt Macy /* Reconstruction succeeded - report errors */ 20957877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 20967877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 20977877fdebSMatt Macy 20987877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 20997877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 21007877fdebSMatt Macy if (rc->rc_need_orig_restore) { 21017877fdebSMatt Macy /* 21027877fdebSMatt Macy * Note: if this is a parity column, 21037877fdebSMatt Macy * we don't really know if it's wrong. 21047877fdebSMatt Macy * We need to let 21057877fdebSMatt Macy * vdev_raidz_io_done_verified() check 21067877fdebSMatt Macy * it, and if we set rc_error, it will 21077877fdebSMatt Macy * think that it is a "known" error 21087877fdebSMatt Macy * that doesn't need to be checked 21097877fdebSMatt Macy * or corrected. 21107877fdebSMatt Macy */ 21117877fdebSMatt Macy if (rc->rc_error == 0 && 21127877fdebSMatt Macy c >= rr->rr_firstdatacol) { 21137877fdebSMatt Macy raidz_checksum_error(zio, 21147877fdebSMatt Macy rc, rc->rc_gdata); 21157877fdebSMatt Macy rc->rc_error = 21167877fdebSMatt Macy SET_ERROR(ECKSUM); 21177877fdebSMatt Macy } 21187877fdebSMatt Macy rc->rc_need_orig_restore = B_FALSE; 21197877fdebSMatt Macy } 21207877fdebSMatt Macy } 21217877fdebSMatt Macy 21227877fdebSMatt Macy vdev_raidz_io_done_verified(zio, rr); 21237877fdebSMatt Macy } 21247877fdebSMatt Macy 21257877fdebSMatt Macy zio_checksum_verified(zio); 21267877fdebSMatt Macy 21277877fdebSMatt Macy return (0); 21287877fdebSMatt Macy } 21297877fdebSMatt Macy 21307877fdebSMatt Macy /* Reconstruction failed - restore original data */ 21317877fdebSMatt Macy raidz_restore_orig_data(rm); 21327877fdebSMatt Macy return (ECKSUM); 21337877fdebSMatt Macy } 21347877fdebSMatt Macy 21357877fdebSMatt Macy /* 21367877fdebSMatt Macy * Iterate over all combinations of N bad vdevs and attempt a reconstruction. 21377877fdebSMatt Macy * Note that the algorithm below is non-optimal because it doesn't take into 21387877fdebSMatt Macy * account how reconstruction is actually performed. For example, with 21397877fdebSMatt Macy * triple-parity RAID-Z the reconstruction procedure is the same if column 4 21407877fdebSMatt Macy * is targeted as invalid as if columns 1 and 4 are targeted since in both 21417877fdebSMatt Macy * cases we'd only use parity information in column 0. 21427877fdebSMatt Macy * 21437877fdebSMatt Macy * The order that we find the various possible combinations of failed 21447877fdebSMatt Macy * disks is dictated by these rules: 21457877fdebSMatt Macy * - Examine each "slot" (the "i" in tgts[i]) 21467877fdebSMatt Macy * - Try to increment this slot (tgts[i] = tgts[i] + 1) 21477877fdebSMatt Macy * - if we can't increment because it runs into the next slot, 21487877fdebSMatt Macy * reset our slot to the minimum, and examine the next slot 21497877fdebSMatt Macy * 21507877fdebSMatt Macy * For example, with a 6-wide RAIDZ3, and no known errors (so we have to choose 21517877fdebSMatt Macy * 3 columns to reconstruct), we will generate the following sequence: 21527877fdebSMatt Macy * 21537877fdebSMatt Macy * STATE ACTION 21547877fdebSMatt Macy * 0 1 2 special case: skip since these are all parity 21557877fdebSMatt Macy * 0 1 3 first slot: reset to 0; middle slot: increment to 2 21567877fdebSMatt Macy * 0 2 3 first slot: increment to 1 21577877fdebSMatt Macy * 1 2 3 first: reset to 0; middle: reset to 1; last: increment to 4 21587877fdebSMatt Macy * 0 1 4 first: reset to 0; middle: increment to 2 21597877fdebSMatt Macy * 0 2 4 first: increment to 1 21607877fdebSMatt Macy * 1 2 4 first: reset to 0; middle: increment to 3 21617877fdebSMatt Macy * 0 3 4 first: increment to 1 21627877fdebSMatt Macy * 1 3 4 first: increment to 2 21637877fdebSMatt Macy * 2 3 4 first: reset to 0; middle: reset to 1; last: increment to 5 21647877fdebSMatt Macy * 0 1 5 first: reset to 0; middle: increment to 2 21657877fdebSMatt Macy * 0 2 5 first: increment to 1 21667877fdebSMatt Macy * 1 2 5 first: reset to 0; middle: increment to 3 21677877fdebSMatt Macy * 0 3 5 first: increment to 1 21687877fdebSMatt Macy * 1 3 5 first: increment to 2 21697877fdebSMatt Macy * 2 3 5 first: reset to 0; middle: increment to 4 21707877fdebSMatt Macy * 0 4 5 first: increment to 1 21717877fdebSMatt Macy * 1 4 5 first: increment to 2 21727877fdebSMatt Macy * 2 4 5 first: increment to 3 21737877fdebSMatt Macy * 3 4 5 done 21747877fdebSMatt Macy * 21757877fdebSMatt Macy * This strategy works for dRAID but is less effecient when there are a large 21767877fdebSMatt Macy * number of child vdevs and therefore permutations to check. Furthermore, 21777877fdebSMatt Macy * since the raidz_map_t rows likely do not overlap reconstruction would be 21787877fdebSMatt Macy * possible as long as there are no more than nparity data errors per row. 21797877fdebSMatt Macy * These additional permutations are not currently checked but could be as 21807877fdebSMatt Macy * a future improvement. 21817877fdebSMatt Macy */ 21827877fdebSMatt Macy static int 21837877fdebSMatt Macy vdev_raidz_combrec(zio_t *zio) 21847877fdebSMatt Macy { 21857877fdebSMatt Macy int nparity = vdev_get_nparity(zio->io_vd); 21867877fdebSMatt Macy raidz_map_t *rm = zio->io_vsd; 21877877fdebSMatt Macy 21887877fdebSMatt Macy /* Check if there's enough data to attempt reconstrution. */ 21897877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 21907877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 21917877fdebSMatt Macy int total_errors = 0; 21927877fdebSMatt Macy 21937877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 21947877fdebSMatt Macy if (rr->rr_col[c].rc_error) 21957877fdebSMatt Macy total_errors++; 21967877fdebSMatt Macy } 21977877fdebSMatt Macy 21987877fdebSMatt Macy if (total_errors > nparity) 21997877fdebSMatt Macy return (vdev_raidz_worst_error(rr)); 22007877fdebSMatt Macy } 22017877fdebSMatt Macy 22027877fdebSMatt Macy for (int num_failures = 1; num_failures <= nparity; num_failures++) { 22037877fdebSMatt Macy int tstore[VDEV_RAIDZ_MAXPARITY + 2]; 22047877fdebSMatt Macy int *ltgts = &tstore[1]; /* value is logical child ID */ 22057877fdebSMatt Macy 22067877fdebSMatt Macy /* Determine number of logical children, n */ 22077877fdebSMatt Macy int n = zio->io_vd->vdev_children; 22087877fdebSMatt Macy 22097877fdebSMatt Macy ASSERT3U(num_failures, <=, nparity); 22107877fdebSMatt Macy ASSERT3U(num_failures, <=, VDEV_RAIDZ_MAXPARITY); 22117877fdebSMatt Macy 22127877fdebSMatt Macy /* Handle corner cases in combrec logic */ 22137877fdebSMatt Macy ltgts[-1] = -1; 22147877fdebSMatt Macy for (int i = 0; i < num_failures; i++) { 22157877fdebSMatt Macy ltgts[i] = i; 22167877fdebSMatt Macy } 22177877fdebSMatt Macy ltgts[num_failures] = n; 22187877fdebSMatt Macy 22197877fdebSMatt Macy for (;;) { 22207877fdebSMatt Macy int err = raidz_reconstruct(zio, ltgts, num_failures, 22217877fdebSMatt Macy nparity); 22227877fdebSMatt Macy if (err == EINVAL) { 22237877fdebSMatt Macy /* 22247877fdebSMatt Macy * Reconstruction not possible with this # 22257877fdebSMatt Macy * failures; try more failures. 22267877fdebSMatt Macy */ 22277877fdebSMatt Macy break; 22287877fdebSMatt Macy } else if (err == 0) 22297877fdebSMatt Macy return (0); 22307877fdebSMatt Macy 22317877fdebSMatt Macy /* Compute next targets to try */ 22327877fdebSMatt Macy for (int t = 0; ; t++) { 22337877fdebSMatt Macy ASSERT3U(t, <, num_failures); 22347877fdebSMatt Macy ltgts[t]++; 22357877fdebSMatt Macy if (ltgts[t] == n) { 22367877fdebSMatt Macy /* try more failures */ 22377877fdebSMatt Macy ASSERT3U(t, ==, num_failures - 1); 22387877fdebSMatt Macy break; 22397877fdebSMatt Macy } 22407877fdebSMatt Macy 22417877fdebSMatt Macy ASSERT3U(ltgts[t], <, n); 22427877fdebSMatt Macy ASSERT3U(ltgts[t], <=, ltgts[t + 1]); 22437877fdebSMatt Macy 22447877fdebSMatt Macy /* 22457877fdebSMatt Macy * If that spot is available, we're done here. 22467877fdebSMatt Macy * Try the next combination. 22477877fdebSMatt Macy */ 22487877fdebSMatt Macy if (ltgts[t] != ltgts[t + 1]) 22497877fdebSMatt Macy break; 22507877fdebSMatt Macy 22517877fdebSMatt Macy /* 22527877fdebSMatt Macy * Otherwise, reset this tgt to the minimum, 22537877fdebSMatt Macy * and move on to the next tgt. 22547877fdebSMatt Macy */ 22557877fdebSMatt Macy ltgts[t] = ltgts[t - 1] + 1; 22567877fdebSMatt Macy ASSERT3U(ltgts[t], ==, t); 22577877fdebSMatt Macy } 22587877fdebSMatt Macy 22597877fdebSMatt Macy /* Increase the number of failures and keep trying. */ 22607877fdebSMatt Macy if (ltgts[num_failures - 1] == n) 22617877fdebSMatt Macy break; 22627877fdebSMatt Macy } 22637877fdebSMatt Macy } 22647877fdebSMatt Macy 22657877fdebSMatt Macy return (ECKSUM); 22667877fdebSMatt Macy } 22677877fdebSMatt Macy 22687877fdebSMatt Macy void 22697877fdebSMatt Macy vdev_raidz_reconstruct(raidz_map_t *rm, const int *t, int nt) 22707877fdebSMatt Macy { 22717877fdebSMatt Macy for (uint64_t row = 0; row < rm->rm_nrows; row++) { 22727877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[row]; 22737877fdebSMatt Macy vdev_raidz_reconstruct_row(rm, rr, t, nt); 22747877fdebSMatt Macy } 22757877fdebSMatt Macy } 22767877fdebSMatt Macy 22777877fdebSMatt Macy /* 22787877fdebSMatt Macy * Complete a write IO operation on a RAIDZ VDev 22797877fdebSMatt Macy * 22807877fdebSMatt Macy * Outline: 22817877fdebSMatt Macy * 1. Check for errors on the child IOs. 22827877fdebSMatt Macy * 2. Return, setting an error code if too few child VDevs were written 22837877fdebSMatt Macy * to reconstruct the data later. Note that partial writes are 22847877fdebSMatt Macy * considered successful if they can be reconstructed at all. 22857877fdebSMatt Macy */ 22867877fdebSMatt Macy static void 22877877fdebSMatt Macy vdev_raidz_io_done_write_impl(zio_t *zio, raidz_row_t *rr) 22887877fdebSMatt Macy { 22897877fdebSMatt Macy int total_errors = 0; 22907877fdebSMatt Macy 22917877fdebSMatt Macy ASSERT3U(rr->rr_missingparity, <=, rr->rr_firstdatacol); 22927877fdebSMatt Macy ASSERT3U(rr->rr_missingdata, <=, rr->rr_cols - rr->rr_firstdatacol); 22937877fdebSMatt Macy ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 22947877fdebSMatt Macy 22957877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 22967877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 22977877fdebSMatt Macy 22987877fdebSMatt Macy if (rc->rc_error) { 22997877fdebSMatt Macy ASSERT(rc->rc_error != ECKSUM); /* child has no bp */ 23007877fdebSMatt Macy 23017877fdebSMatt Macy total_errors++; 23027877fdebSMatt Macy } 23037877fdebSMatt Macy } 23047877fdebSMatt Macy 23057877fdebSMatt Macy /* 23067877fdebSMatt Macy * Treat partial writes as a success. If we couldn't write enough 23077877fdebSMatt Macy * columns to reconstruct the data, the I/O failed. Otherwise, 23087877fdebSMatt Macy * good enough. 2309eda14cbcSMatt Macy * 2310eda14cbcSMatt Macy * Now that we support write reallocation, it would be better 2311eda14cbcSMatt Macy * to treat partial failure as real failure unless there are 2312eda14cbcSMatt Macy * no non-degraded top-level vdevs left, and not update DTLs 2313eda14cbcSMatt Macy * if we intend to reallocate. 2314eda14cbcSMatt Macy */ 23157877fdebSMatt Macy if (total_errors > rr->rr_firstdatacol) { 23167877fdebSMatt Macy zio->io_error = zio_worst_error(zio->io_error, 23177877fdebSMatt Macy vdev_raidz_worst_error(rr)); 23187877fdebSMatt Macy } 2319eda14cbcSMatt Macy } 2320eda14cbcSMatt Macy 2321eda14cbcSMatt Macy /* 23227877fdebSMatt Macy * return 0 if no reconstruction occurred, otherwise the "code" from 23237877fdebSMatt Macy * vdev_raidz_reconstruct(). 2324eda14cbcSMatt Macy */ 23257877fdebSMatt Macy static int 23267877fdebSMatt Macy vdev_raidz_io_done_reconstruct_known_missing(zio_t *zio, raidz_map_t *rm, 23277877fdebSMatt Macy raidz_row_t *rr) 23287877fdebSMatt Macy { 23297877fdebSMatt Macy int parity_errors = 0; 23307877fdebSMatt Macy int parity_untried = 0; 23317877fdebSMatt Macy int data_errors = 0; 23327877fdebSMatt Macy int total_errors = 0; 23337877fdebSMatt Macy int code = 0; 23347877fdebSMatt Macy 23357877fdebSMatt Macy ASSERT3U(rr->rr_missingparity, <=, rr->rr_firstdatacol); 23367877fdebSMatt Macy ASSERT3U(rr->rr_missingdata, <=, rr->rr_cols - rr->rr_firstdatacol); 23377877fdebSMatt Macy ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ); 23387877fdebSMatt Macy 23397877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 23407877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 23417877fdebSMatt Macy 23427877fdebSMatt Macy if (rc->rc_error) { 23437877fdebSMatt Macy ASSERT(rc->rc_error != ECKSUM); /* child has no bp */ 23447877fdebSMatt Macy 23457877fdebSMatt Macy if (c < rr->rr_firstdatacol) 23467877fdebSMatt Macy parity_errors++; 23477877fdebSMatt Macy else 23487877fdebSMatt Macy data_errors++; 23497877fdebSMatt Macy 23507877fdebSMatt Macy total_errors++; 23517877fdebSMatt Macy } else if (c < rr->rr_firstdatacol && !rc->rc_tried) { 23527877fdebSMatt Macy parity_untried++; 23537877fdebSMatt Macy } 23547877fdebSMatt Macy } 2355eda14cbcSMatt Macy 2356eda14cbcSMatt Macy /* 23577877fdebSMatt Macy * If there were data errors and the number of errors we saw was 23587877fdebSMatt Macy * correctable -- less than or equal to the number of parity disks read 23597877fdebSMatt Macy * -- reconstruct based on the missing data. 2360eda14cbcSMatt Macy */ 23617877fdebSMatt Macy if (data_errors != 0 && 23627877fdebSMatt Macy total_errors <= rr->rr_firstdatacol - parity_untried) { 2363eda14cbcSMatt Macy /* 2364eda14cbcSMatt Macy * We either attempt to read all the parity columns or 2365eda14cbcSMatt Macy * none of them. If we didn't try to read parity, we 2366eda14cbcSMatt Macy * wouldn't be here in the correctable case. There must 2367eda14cbcSMatt Macy * also have been fewer parity errors than parity 2368eda14cbcSMatt Macy * columns or, again, we wouldn't be in this code path. 2369eda14cbcSMatt Macy */ 2370eda14cbcSMatt Macy ASSERT(parity_untried == 0); 23717877fdebSMatt Macy ASSERT(parity_errors < rr->rr_firstdatacol); 2372eda14cbcSMatt Macy 2373eda14cbcSMatt Macy /* 2374eda14cbcSMatt Macy * Identify the data columns that reported an error. 2375eda14cbcSMatt Macy */ 23767877fdebSMatt Macy int n = 0; 23777877fdebSMatt Macy int tgts[VDEV_RAIDZ_MAXPARITY]; 23787877fdebSMatt Macy for (int c = rr->rr_firstdatacol; c < rr->rr_cols; c++) { 23797877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 2380eda14cbcSMatt Macy if (rc->rc_error != 0) { 2381eda14cbcSMatt Macy ASSERT(n < VDEV_RAIDZ_MAXPARITY); 2382eda14cbcSMatt Macy tgts[n++] = c; 2383eda14cbcSMatt Macy } 2384eda14cbcSMatt Macy } 2385eda14cbcSMatt Macy 23867877fdebSMatt Macy ASSERT(rr->rr_firstdatacol >= n); 2387eda14cbcSMatt Macy 23887877fdebSMatt Macy code = vdev_raidz_reconstruct_row(rm, rr, tgts, n); 2389eda14cbcSMatt Macy } 2390eda14cbcSMatt Macy 23917877fdebSMatt Macy return (code); 2392eda14cbcSMatt Macy } 2393eda14cbcSMatt Macy 2394eda14cbcSMatt Macy /* 23957877fdebSMatt Macy * Return the number of reads issued. 2396eda14cbcSMatt Macy */ 23977877fdebSMatt Macy static int 23987877fdebSMatt Macy vdev_raidz_read_all(zio_t *zio, raidz_row_t *rr) 23997877fdebSMatt Macy { 24007877fdebSMatt Macy vdev_t *vd = zio->io_vd; 24017877fdebSMatt Macy int nread = 0; 2402eda14cbcSMatt Macy 24037877fdebSMatt Macy rr->rr_missingdata = 0; 24047877fdebSMatt Macy rr->rr_missingparity = 0; 24057877fdebSMatt Macy 24067877fdebSMatt Macy /* 24077877fdebSMatt Macy * If this rows contains empty sectors which are not required 24087877fdebSMatt Macy * for a normal read then allocate an ABD for them now so they 24097877fdebSMatt Macy * may be read, verified, and any needed repairs performed. 24107877fdebSMatt Macy */ 24117877fdebSMatt Macy if (rr->rr_nempty && rr->rr_abd_empty == NULL) 24127877fdebSMatt Macy vdev_draid_map_alloc_empty(zio, rr); 24137877fdebSMatt Macy 24147877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 24157877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 24167877fdebSMatt Macy if (rc->rc_tried || rc->rc_size == 0) 2417eda14cbcSMatt Macy continue; 2418eda14cbcSMatt Macy 2419eda14cbcSMatt Macy zio_nowait(zio_vdev_child_io(zio, NULL, 2420eda14cbcSMatt Macy vd->vdev_child[rc->rc_devidx], 2421eda14cbcSMatt Macy rc->rc_offset, rc->rc_abd, rc->rc_size, 2422eda14cbcSMatt Macy zio->io_type, zio->io_priority, 0, 2423eda14cbcSMatt Macy vdev_raidz_child_done, rc)); 24247877fdebSMatt Macy nread++; 24257877fdebSMatt Macy } 24267877fdebSMatt Macy return (nread); 2427eda14cbcSMatt Macy } 2428eda14cbcSMatt Macy 2429eda14cbcSMatt Macy /* 24307877fdebSMatt Macy * We're here because either there were too many errors to even attempt 24317877fdebSMatt Macy * reconstruction (total_errors == rm_first_datacol), or vdev_*_combrec() 24327877fdebSMatt Macy * failed. In either case, there is enough bad data to prevent reconstruction. 24337877fdebSMatt Macy * Start checksum ereports for all children which haven't failed. 2434eda14cbcSMatt Macy */ 24357877fdebSMatt Macy static void 24367877fdebSMatt Macy vdev_raidz_io_done_unrecoverable(zio_t *zio) 24377877fdebSMatt Macy { 24387877fdebSMatt Macy raidz_map_t *rm = zio->io_vsd; 2439eda14cbcSMatt Macy 24407877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 24417877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 2442eda14cbcSMatt Macy 24437877fdebSMatt Macy for (int c = 0; c < rr->rr_cols; c++) { 24447877fdebSMatt Macy raidz_col_t *rc = &rr->rr_col[c]; 24457877fdebSMatt Macy vdev_t *cvd = zio->io_vd->vdev_child[rc->rc_devidx]; 24467877fdebSMatt Macy 24472c48331dSMatt Macy if (rc->rc_error != 0) 24482c48331dSMatt Macy continue; 24492c48331dSMatt Macy 2450eda14cbcSMatt Macy zio_bad_cksum_t zbc; 2451eda14cbcSMatt Macy zbc.zbc_has_cksum = 0; 24522c48331dSMatt Macy zbc.zbc_injected = rm->rm_ecksuminjected; 2453eda14cbcSMatt Macy 2454*ba27dd8bSMartin Matuska (void) zfs_ereport_start_checksum(zio->io_spa, 24557877fdebSMatt Macy cvd, &zio->io_bookmark, zio, rc->rc_offset, 24567877fdebSMatt Macy rc->rc_size, (void *)(uintptr_t)c, &zbc); 2457eda14cbcSMatt Macy mutex_enter(&cvd->vdev_stat_lock); 2458eda14cbcSMatt Macy cvd->vdev_stat.vs_checksum_errors++; 2459eda14cbcSMatt Macy mutex_exit(&cvd->vdev_stat_lock); 2460eda14cbcSMatt Macy } 2461eda14cbcSMatt Macy } 2462eda14cbcSMatt Macy } 2463eda14cbcSMatt Macy 24647877fdebSMatt Macy void 24657877fdebSMatt Macy vdev_raidz_io_done(zio_t *zio) 24667877fdebSMatt Macy { 24677877fdebSMatt Macy raidz_map_t *rm = zio->io_vsd; 24687877fdebSMatt Macy 24697877fdebSMatt Macy if (zio->io_type == ZIO_TYPE_WRITE) { 24707877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 24717877fdebSMatt Macy vdev_raidz_io_done_write_impl(zio, rm->rm_row[i]); 24727877fdebSMatt Macy } 24737877fdebSMatt Macy } else { 24747877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 24757877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 24767877fdebSMatt Macy rr->rr_code = 24777877fdebSMatt Macy vdev_raidz_io_done_reconstruct_known_missing(zio, 24787877fdebSMatt Macy rm, rr); 24797877fdebSMatt Macy } 24807877fdebSMatt Macy 24817877fdebSMatt Macy if (raidz_checksum_verify(zio) == 0) { 24827877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 24837877fdebSMatt Macy raidz_row_t *rr = rm->rm_row[i]; 24847877fdebSMatt Macy vdev_raidz_io_done_verified(zio, rr); 24857877fdebSMatt Macy } 2486eda14cbcSMatt Macy zio_checksum_verified(zio); 24877877fdebSMatt Macy } else { 2488eda14cbcSMatt Macy /* 24897877fdebSMatt Macy * A sequential resilver has no checksum which makes 24907877fdebSMatt Macy * combinatoral reconstruction impossible. This code 24917877fdebSMatt Macy * path is unreachable since raidz_checksum_verify() 24927877fdebSMatt Macy * has no checksum to verify and must succeed. 2493eda14cbcSMatt Macy */ 24947877fdebSMatt Macy ASSERT3U(zio->io_priority, !=, ZIO_PRIORITY_REBUILD); 2495eda14cbcSMatt Macy 24967877fdebSMatt Macy /* 24977877fdebSMatt Macy * This isn't a typical situation -- either we got a 24987877fdebSMatt Macy * read error or a child silently returned bad data. 24997877fdebSMatt Macy * Read every block so we can try again with as much 25007877fdebSMatt Macy * data and parity as we can track down. If we've 25017877fdebSMatt Macy * already been through once before, all children will 25027877fdebSMatt Macy * be marked as tried so we'll proceed to combinatorial 25037877fdebSMatt Macy * reconstruction. 25047877fdebSMatt Macy */ 25057877fdebSMatt Macy int nread = 0; 25067877fdebSMatt Macy for (int i = 0; i < rm->rm_nrows; i++) { 25077877fdebSMatt Macy nread += vdev_raidz_read_all(zio, 25087877fdebSMatt Macy rm->rm_row[i]); 25097877fdebSMatt Macy } 25107877fdebSMatt Macy if (nread != 0) { 25117877fdebSMatt Macy /* 25127877fdebSMatt Macy * Normally our stage is VDEV_IO_DONE, but if 25137877fdebSMatt Macy * we've already called redone(), it will have 25147877fdebSMatt Macy * changed to VDEV_IO_START, in which case we 25157877fdebSMatt Macy * don't want to call redone() again. 25167877fdebSMatt Macy */ 25177877fdebSMatt Macy if (zio->io_stage != ZIO_STAGE_VDEV_IO_START) 25187877fdebSMatt Macy zio_vdev_io_redone(zio); 25197877fdebSMatt Macy return; 25207877fdebSMatt Macy } 2521eda14cbcSMatt Macy 25227877fdebSMatt Macy zio->io_error = vdev_raidz_combrec(zio); 25237877fdebSMatt Macy if (zio->io_error == ECKSUM && 25247877fdebSMatt Macy !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 25257877fdebSMatt Macy vdev_raidz_io_done_unrecoverable(zio); 25267877fdebSMatt Macy } 2527eda14cbcSMatt Macy } 2528eda14cbcSMatt Macy } 2529eda14cbcSMatt Macy } 2530eda14cbcSMatt Macy 2531eda14cbcSMatt Macy static void 2532eda14cbcSMatt Macy vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded) 2533eda14cbcSMatt Macy { 25347877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 25357877fdebSMatt Macy if (faulted > vdrz->vd_nparity) 2536eda14cbcSMatt Macy vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2537eda14cbcSMatt Macy VDEV_AUX_NO_REPLICAS); 2538eda14cbcSMatt Macy else if (degraded + faulted != 0) 2539eda14cbcSMatt Macy vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 2540eda14cbcSMatt Macy else 2541eda14cbcSMatt Macy vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 2542eda14cbcSMatt Macy } 2543eda14cbcSMatt Macy 2544eda14cbcSMatt Macy /* 2545eda14cbcSMatt Macy * Determine if any portion of the provided block resides on a child vdev 2546eda14cbcSMatt Macy * with a dirty DTL and therefore needs to be resilvered. The function 2547eda14cbcSMatt Macy * assumes that at least one DTL is dirty which implies that full stripe 2548eda14cbcSMatt Macy * width blocks must be resilvered. 2549eda14cbcSMatt Macy */ 2550eda14cbcSMatt Macy static boolean_t 25517877fdebSMatt Macy vdev_raidz_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize, 25527877fdebSMatt Macy uint64_t phys_birth) 2553eda14cbcSMatt Macy { 25547877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 2555eda14cbcSMatt Macy uint64_t dcols = vd->vdev_children; 25567877fdebSMatt Macy uint64_t nparity = vdrz->vd_nparity; 2557eda14cbcSMatt Macy uint64_t ashift = vd->vdev_top->vdev_ashift; 2558eda14cbcSMatt Macy /* The starting RAIDZ (parent) vdev sector of the block. */ 25597877fdebSMatt Macy uint64_t b = DVA_GET_OFFSET(dva) >> ashift; 2560eda14cbcSMatt Macy /* The zio's size in units of the vdev's minimum sector size. */ 2561eda14cbcSMatt Macy uint64_t s = ((psize - 1) >> ashift) + 1; 2562eda14cbcSMatt Macy /* The first column for this stripe. */ 2563eda14cbcSMatt Macy uint64_t f = b % dcols; 2564eda14cbcSMatt Macy 25657877fdebSMatt Macy /* Unreachable by sequential resilver. */ 25667877fdebSMatt Macy ASSERT3U(phys_birth, !=, TXG_UNKNOWN); 25677877fdebSMatt Macy 25687877fdebSMatt Macy if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1)) 25697877fdebSMatt Macy return (B_FALSE); 25707877fdebSMatt Macy 2571eda14cbcSMatt Macy if (s + nparity >= dcols) 2572eda14cbcSMatt Macy return (B_TRUE); 2573eda14cbcSMatt Macy 2574eda14cbcSMatt Macy for (uint64_t c = 0; c < s + nparity; c++) { 2575eda14cbcSMatt Macy uint64_t devidx = (f + c) % dcols; 2576eda14cbcSMatt Macy vdev_t *cvd = vd->vdev_child[devidx]; 2577eda14cbcSMatt Macy 2578eda14cbcSMatt Macy /* 2579eda14cbcSMatt Macy * dsl_scan_need_resilver() already checked vd with 2580eda14cbcSMatt Macy * vdev_dtl_contains(). So here just check cvd with 2581eda14cbcSMatt Macy * vdev_dtl_empty(), cheaper and a good approximation. 2582eda14cbcSMatt Macy */ 2583eda14cbcSMatt Macy if (!vdev_dtl_empty(cvd, DTL_PARTIAL)) 2584eda14cbcSMatt Macy return (B_TRUE); 2585eda14cbcSMatt Macy } 2586eda14cbcSMatt Macy 2587eda14cbcSMatt Macy return (B_FALSE); 2588eda14cbcSMatt Macy } 2589eda14cbcSMatt Macy 2590eda14cbcSMatt Macy static void 25917877fdebSMatt Macy vdev_raidz_xlate(vdev_t *cvd, const range_seg64_t *logical_rs, 25927877fdebSMatt Macy range_seg64_t *physical_rs, range_seg64_t *remain_rs) 2593eda14cbcSMatt Macy { 2594eda14cbcSMatt Macy vdev_t *raidvd = cvd->vdev_parent; 2595eda14cbcSMatt Macy ASSERT(raidvd->vdev_ops == &vdev_raidz_ops); 2596eda14cbcSMatt Macy 2597eda14cbcSMatt Macy uint64_t width = raidvd->vdev_children; 2598eda14cbcSMatt Macy uint64_t tgt_col = cvd->vdev_id; 2599eda14cbcSMatt Macy uint64_t ashift = raidvd->vdev_top->vdev_ashift; 2600eda14cbcSMatt Macy 2601eda14cbcSMatt Macy /* make sure the offsets are block-aligned */ 26027877fdebSMatt Macy ASSERT0(logical_rs->rs_start % (1 << ashift)); 26037877fdebSMatt Macy ASSERT0(logical_rs->rs_end % (1 << ashift)); 26047877fdebSMatt Macy uint64_t b_start = logical_rs->rs_start >> ashift; 26057877fdebSMatt Macy uint64_t b_end = logical_rs->rs_end >> ashift; 2606eda14cbcSMatt Macy 2607eda14cbcSMatt Macy uint64_t start_row = 0; 2608eda14cbcSMatt Macy if (b_start > tgt_col) /* avoid underflow */ 2609eda14cbcSMatt Macy start_row = ((b_start - tgt_col - 1) / width) + 1; 2610eda14cbcSMatt Macy 2611eda14cbcSMatt Macy uint64_t end_row = 0; 2612eda14cbcSMatt Macy if (b_end > tgt_col) 2613eda14cbcSMatt Macy end_row = ((b_end - tgt_col - 1) / width) + 1; 2614eda14cbcSMatt Macy 26157877fdebSMatt Macy physical_rs->rs_start = start_row << ashift; 26167877fdebSMatt Macy physical_rs->rs_end = end_row << ashift; 2617eda14cbcSMatt Macy 26187877fdebSMatt Macy ASSERT3U(physical_rs->rs_start, <=, logical_rs->rs_start); 26197877fdebSMatt Macy ASSERT3U(physical_rs->rs_end - physical_rs->rs_start, <=, 26207877fdebSMatt Macy logical_rs->rs_end - logical_rs->rs_start); 26217877fdebSMatt Macy } 26227877fdebSMatt Macy 26237877fdebSMatt Macy /* 26247877fdebSMatt Macy * Initialize private RAIDZ specific fields from the nvlist. 26257877fdebSMatt Macy */ 26267877fdebSMatt Macy static int 26277877fdebSMatt Macy vdev_raidz_init(spa_t *spa, nvlist_t *nv, void **tsd) 26287877fdebSMatt Macy { 26297877fdebSMatt Macy vdev_raidz_t *vdrz; 26307877fdebSMatt Macy uint64_t nparity; 26317877fdebSMatt Macy 26327877fdebSMatt Macy uint_t children; 26337877fdebSMatt Macy nvlist_t **child; 26347877fdebSMatt Macy int error = nvlist_lookup_nvlist_array(nv, 26357877fdebSMatt Macy ZPOOL_CONFIG_CHILDREN, &child, &children); 26367877fdebSMatt Macy if (error != 0) 26377877fdebSMatt Macy return (SET_ERROR(EINVAL)); 26387877fdebSMatt Macy 26397877fdebSMatt Macy if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, &nparity) == 0) { 26407877fdebSMatt Macy if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 26417877fdebSMatt Macy return (SET_ERROR(EINVAL)); 26427877fdebSMatt Macy 26437877fdebSMatt Macy /* 26447877fdebSMatt Macy * Previous versions could only support 1 or 2 parity 26457877fdebSMatt Macy * device. 26467877fdebSMatt Macy */ 26477877fdebSMatt Macy if (nparity > 1 && spa_version(spa) < SPA_VERSION_RAIDZ2) 26487877fdebSMatt Macy return (SET_ERROR(EINVAL)); 26497877fdebSMatt Macy else if (nparity > 2 && spa_version(spa) < SPA_VERSION_RAIDZ3) 26507877fdebSMatt Macy return (SET_ERROR(EINVAL)); 26517877fdebSMatt Macy } else { 26527877fdebSMatt Macy /* 26537877fdebSMatt Macy * We require the parity to be specified for SPAs that 26547877fdebSMatt Macy * support multiple parity levels. 26557877fdebSMatt Macy */ 26567877fdebSMatt Macy if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 26577877fdebSMatt Macy return (SET_ERROR(EINVAL)); 26587877fdebSMatt Macy 26597877fdebSMatt Macy /* 26607877fdebSMatt Macy * Otherwise, we default to 1 parity device for RAID-Z. 26617877fdebSMatt Macy */ 26627877fdebSMatt Macy nparity = 1; 26637877fdebSMatt Macy } 26647877fdebSMatt Macy 26657877fdebSMatt Macy vdrz = kmem_zalloc(sizeof (*vdrz), KM_SLEEP); 26667877fdebSMatt Macy vdrz->vd_logical_width = children; 26677877fdebSMatt Macy vdrz->vd_nparity = nparity; 26687877fdebSMatt Macy 26697877fdebSMatt Macy *tsd = vdrz; 26707877fdebSMatt Macy 26717877fdebSMatt Macy return (0); 26727877fdebSMatt Macy } 26737877fdebSMatt Macy 26747877fdebSMatt Macy static void 26757877fdebSMatt Macy vdev_raidz_fini(vdev_t *vd) 26767877fdebSMatt Macy { 26777877fdebSMatt Macy kmem_free(vd->vdev_tsd, sizeof (vdev_raidz_t)); 26787877fdebSMatt Macy } 26797877fdebSMatt Macy 26807877fdebSMatt Macy /* 26817877fdebSMatt Macy * Add RAIDZ specific fields to the config nvlist. 26827877fdebSMatt Macy */ 26837877fdebSMatt Macy static void 26847877fdebSMatt Macy vdev_raidz_config_generate(vdev_t *vd, nvlist_t *nv) 26857877fdebSMatt Macy { 26867877fdebSMatt Macy ASSERT3P(vd->vdev_ops, ==, &vdev_raidz_ops); 26877877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 26887877fdebSMatt Macy 26897877fdebSMatt Macy /* 26907877fdebSMatt Macy * Make sure someone hasn't managed to sneak a fancy new vdev 26917877fdebSMatt Macy * into a crufty old storage pool. 26927877fdebSMatt Macy */ 26937877fdebSMatt Macy ASSERT(vdrz->vd_nparity == 1 || 26947877fdebSMatt Macy (vdrz->vd_nparity <= 2 && 26957877fdebSMatt Macy spa_version(vd->vdev_spa) >= SPA_VERSION_RAIDZ2) || 26967877fdebSMatt Macy (vdrz->vd_nparity <= 3 && 26977877fdebSMatt Macy spa_version(vd->vdev_spa) >= SPA_VERSION_RAIDZ3)); 26987877fdebSMatt Macy 26997877fdebSMatt Macy /* 27007877fdebSMatt Macy * Note that we'll add these even on storage pools where they 27017877fdebSMatt Macy * aren't strictly required -- older software will just ignore 27027877fdebSMatt Macy * it. 27037877fdebSMatt Macy */ 27047877fdebSMatt Macy fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vdrz->vd_nparity); 27057877fdebSMatt Macy } 27067877fdebSMatt Macy 27077877fdebSMatt Macy static uint64_t 27087877fdebSMatt Macy vdev_raidz_nparity(vdev_t *vd) 27097877fdebSMatt Macy { 27107877fdebSMatt Macy vdev_raidz_t *vdrz = vd->vdev_tsd; 27117877fdebSMatt Macy return (vdrz->vd_nparity); 27127877fdebSMatt Macy } 27137877fdebSMatt Macy 27147877fdebSMatt Macy static uint64_t 27157877fdebSMatt Macy vdev_raidz_ndisks(vdev_t *vd) 27167877fdebSMatt Macy { 27177877fdebSMatt Macy return (vd->vdev_children); 2718eda14cbcSMatt Macy } 2719eda14cbcSMatt Macy 2720eda14cbcSMatt Macy vdev_ops_t vdev_raidz_ops = { 27217877fdebSMatt Macy .vdev_op_init = vdev_raidz_init, 27227877fdebSMatt Macy .vdev_op_fini = vdev_raidz_fini, 2723eda14cbcSMatt Macy .vdev_op_open = vdev_raidz_open, 2724eda14cbcSMatt Macy .vdev_op_close = vdev_raidz_close, 2725eda14cbcSMatt Macy .vdev_op_asize = vdev_raidz_asize, 27267877fdebSMatt Macy .vdev_op_min_asize = vdev_raidz_min_asize, 27277877fdebSMatt Macy .vdev_op_min_alloc = NULL, 2728eda14cbcSMatt Macy .vdev_op_io_start = vdev_raidz_io_start, 2729eda14cbcSMatt Macy .vdev_op_io_done = vdev_raidz_io_done, 2730eda14cbcSMatt Macy .vdev_op_state_change = vdev_raidz_state_change, 2731eda14cbcSMatt Macy .vdev_op_need_resilver = vdev_raidz_need_resilver, 2732eda14cbcSMatt Macy .vdev_op_hold = NULL, 2733eda14cbcSMatt Macy .vdev_op_rele = NULL, 2734eda14cbcSMatt Macy .vdev_op_remap = NULL, 2735eda14cbcSMatt Macy .vdev_op_xlate = vdev_raidz_xlate, 27367877fdebSMatt Macy .vdev_op_rebuild_asize = NULL, 27377877fdebSMatt Macy .vdev_op_metaslab_init = NULL, 27387877fdebSMatt Macy .vdev_op_config_generate = vdev_raidz_config_generate, 27397877fdebSMatt Macy .vdev_op_nparity = vdev_raidz_nparity, 27407877fdebSMatt Macy .vdev_op_ndisks = vdev_raidz_ndisks, 2741eda14cbcSMatt Macy .vdev_op_type = VDEV_TYPE_RAIDZ, /* name of this vdev type */ 2742eda14cbcSMatt Macy .vdev_op_leaf = B_FALSE /* not a leaf vdev */ 2743eda14cbcSMatt Macy }; 2744