xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_draid.c (revision 071ab5a1f3cbfd29c8fbec27f7e619418adaf074)
161145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
27877fdebSMatt Macy /*
37877fdebSMatt Macy  * CDDL HEADER START
47877fdebSMatt Macy  *
57877fdebSMatt Macy  * The contents of this file are subject to the terms of the
67877fdebSMatt Macy  * Common Development and Distribution License (the "License").
77877fdebSMatt Macy  * You may not use this file except in compliance with the License.
87877fdebSMatt Macy  *
97877fdebSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
117877fdebSMatt Macy  * See the License for the specific language governing permissions
127877fdebSMatt Macy  * and limitations under the License.
137877fdebSMatt Macy  *
147877fdebSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
157877fdebSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167877fdebSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
177877fdebSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
187877fdebSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
197877fdebSMatt Macy  *
207877fdebSMatt Macy  * CDDL HEADER END
217877fdebSMatt Macy  */
227877fdebSMatt Macy /*
237877fdebSMatt Macy  * Copyright (c) 2018 Intel Corporation.
247877fdebSMatt Macy  * Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
257877fdebSMatt Macy  */
267877fdebSMatt Macy 
277877fdebSMatt Macy #include <sys/zfs_context.h>
287877fdebSMatt Macy #include <sys/spa.h>
297877fdebSMatt Macy #include <sys/spa_impl.h>
307877fdebSMatt Macy #include <sys/vdev_impl.h>
317877fdebSMatt Macy #include <sys/vdev_draid.h>
327877fdebSMatt Macy #include <sys/vdev_raidz.h>
337877fdebSMatt Macy #include <sys/vdev_rebuild.h>
347877fdebSMatt Macy #include <sys/abd.h>
357877fdebSMatt Macy #include <sys/zio.h>
367877fdebSMatt Macy #include <sys/nvpair.h>
377877fdebSMatt Macy #include <sys/zio_checksum.h>
387877fdebSMatt Macy #include <sys/fs/zfs.h>
397877fdebSMatt Macy #include <sys/fm/fs/zfs.h>
407877fdebSMatt Macy #include <zfs_fletcher.h>
417877fdebSMatt Macy 
427877fdebSMatt Macy #ifdef ZFS_DEBUG
437877fdebSMatt Macy #include <sys/vdev.h>	/* For vdev_xlate() in vdev_draid_io_verify() */
447877fdebSMatt Macy #endif
457877fdebSMatt Macy 
467877fdebSMatt Macy /*
477877fdebSMatt Macy  * dRAID is a distributed spare implementation for ZFS. A dRAID vdev is
487877fdebSMatt Macy  * comprised of multiple raidz redundancy groups which are spread over the
497877fdebSMatt Macy  * dRAID children. To ensure an even distribution, and avoid hot spots, a
507877fdebSMatt Macy  * permutation mapping is applied to the order of the dRAID children.
517877fdebSMatt Macy  * This mixing effectively distributes the parity columns evenly over all
527877fdebSMatt Macy  * of the disks in the dRAID.
537877fdebSMatt Macy  *
547877fdebSMatt Macy  * This is beneficial because it means when resilvering all of the disks
557877fdebSMatt Macy  * can participate thereby increasing the available IOPs and bandwidth.
567877fdebSMatt Macy  * Furthermore, by reserving a small fraction of each child's total capacity
577877fdebSMatt Macy  * virtual distributed spare disks can be created. These spares similarly
587877fdebSMatt Macy  * benefit from the performance gains of spanning all of the children. The
597877fdebSMatt Macy  * consequence of which is that resilvering to a distributed spare can
607877fdebSMatt Macy  * substantially reduce the time required to restore full parity to pool
617877fdebSMatt Macy  * with a failed disks.
627877fdebSMatt Macy  *
637877fdebSMatt Macy  * === dRAID group layout ===
647877fdebSMatt Macy  *
657877fdebSMatt Macy  * First, let's define a "row" in the configuration to be a 16M chunk from
667877fdebSMatt Macy  * each physical drive at the same offset. This is the minimum allowable
677877fdebSMatt Macy  * size since it must be possible to store a full 16M block when there is
687877fdebSMatt Macy  * only a single data column. Next, we define a "group" to be a set of
697877fdebSMatt Macy  * sequential disks containing both the parity and data columns. We allow
707877fdebSMatt Macy  * groups to span multiple rows in order to align any group size to any
717877fdebSMatt Macy  * number of physical drives. Finally, a "slice" is comprised of the rows
727877fdebSMatt Macy  * which contain the target number of groups. The permutation mappings
737877fdebSMatt Macy  * are applied in a round robin fashion to each slice.
747877fdebSMatt Macy  *
757877fdebSMatt Macy  * Given D+P drives in a group (including parity drives) and C-S physical
767877fdebSMatt Macy  * drives (not including the spare drives), we can distribute the groups
777877fdebSMatt Macy  * across R rows without remainder by selecting the least common multiple
787877fdebSMatt Macy  * of D+P and C-S as the number of groups; i.e. ngroups = LCM(D+P, C-S).
797877fdebSMatt Macy  *
807877fdebSMatt Macy  * In the example below, there are C=14 physical drives in the configuration
817877fdebSMatt Macy  * with S=2 drives worth of spare capacity. Each group has a width of 9
827877fdebSMatt Macy  * which includes D=8 data and P=1 parity drive. There are 4 groups and
837877fdebSMatt Macy  * 3 rows per slice.  Each group has a size of 144M (16M * 9) and a slice
847877fdebSMatt Macy  * size is 576M (144M * 4). When allocating from a dRAID each group is
857877fdebSMatt Macy  * filled before moving on to the next as show in slice0 below.
867877fdebSMatt Macy  *
877877fdebSMatt Macy  *             data disks (8 data + 1 parity)          spares (2)
887877fdebSMatt Macy  *     +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
897877fdebSMatt Macy  *  ^  | 2 | 6 | 1 | 11| 4 | 0 | 7 | 10| 8 | 9 | 13| 5 | 12| 3 | device map 0
907877fdebSMatt Macy  *  |  +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
917877fdebSMatt Macy  *  |  |              group 0              |  group 1..|       |
927877fdebSMatt Macy  *  |  +-----------------------------------+-----------+-------|
937877fdebSMatt Macy  *  |  | 0   1   2   3   4   5   6   7   8 | 36  37  38|       |  r
947877fdebSMatt Macy  *  |  | 9   10  11  12  13  14  15  16  17| 45  46  47|       |  o
957877fdebSMatt Macy  *  |  | 18  19  20  21  22  23  24  25  26| 54  55  56|       |  w
967877fdebSMatt Macy  *     | 27  28  29  30  31  32  33  34  35| 63  64  65|       |  0
977877fdebSMatt Macy  *  s  +-----------------------+-----------------------+-------+
987877fdebSMatt Macy  *  l  |       ..group 1       |        group 2..      |       |
997877fdebSMatt Macy  *  i  +-----------------------+-----------------------+-------+
1007877fdebSMatt Macy  *  c  | 39  40  41  42  43  44| 72  73  74  75  76  77|       |  r
1017877fdebSMatt Macy  *  e  | 48  49  50  51  52  53| 81  82  83  84  85  86|       |  o
1027877fdebSMatt Macy  *  0  | 57  58  59  60  61  62| 90  91  92  93  94  95|       |  w
1037877fdebSMatt Macy  *     | 66  67  68  69  70  71| 99 100 101 102 103 104|       |  1
1047877fdebSMatt Macy  *  |  +-----------+-----------+-----------------------+-------+
1057877fdebSMatt Macy  *  |  |..group 2  |            group 3                |       |
1067877fdebSMatt Macy  *  |  +-----------+-----------+-----------------------+-------+
1077877fdebSMatt Macy  *  |  | 78  79  80|108 109 110 111 112 113 114 115 116|       |  r
1087877fdebSMatt Macy  *  |  | 87  88  89|117 118 119 120 121 122 123 124 125|       |  o
1097877fdebSMatt Macy  *  |  | 96  97  98|126 127 128 129 130 131 132 133 134|       |  w
1107877fdebSMatt Macy  *  v  |105 106 107|135 136 137 138 139 140 141 142 143|       |  2
1117877fdebSMatt Macy  *     +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
1127877fdebSMatt Macy  *     | 9 | 11| 12| 2 | 4 | 1 | 3 | 0 | 10| 13| 8 | 5 | 6 | 7 | device map 1
1137877fdebSMatt Macy  *  s  +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
1147877fdebSMatt Macy  *  l  |              group 4              |  group 5..|       | row 3
1157877fdebSMatt Macy  *  i  +-----------------------+-----------+-----------+-------|
1167877fdebSMatt Macy  *  c  |       ..group 5       |        group 6..      |       | row 4
1177877fdebSMatt Macy  *  e  +-----------+-----------+-----------------------+-------+
1187877fdebSMatt Macy  *  1  |..group 6  |            group 7                |       | row 5
1197877fdebSMatt Macy  *     +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
1207877fdebSMatt Macy  *     | 3 | 5 | 10| 8 | 6 | 11| 12| 0 | 2 | 4 | 7 | 1 | 9 | 13| device map 2
1217877fdebSMatt Macy  *  s  +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
1227877fdebSMatt Macy  *  l  |              group 8              |  group 9..|       | row 6
1237877fdebSMatt Macy  *  i  +-----------------------------------------------+-------|
1247877fdebSMatt Macy  *  c  |       ..group 9       |        group 10..     |       | row 7
1257877fdebSMatt Macy  *  e  +-----------------------+-----------------------+-------+
1267877fdebSMatt Macy  *  2  |..group 10 |            group 11               |       | row 8
1277877fdebSMatt Macy  *     +-----------+-----------------------------------+-------+
1287877fdebSMatt Macy  *
1297877fdebSMatt Macy  * This layout has several advantages over requiring that each row contain
1307877fdebSMatt Macy  * a whole number of groups.
1317877fdebSMatt Macy  *
1327877fdebSMatt Macy  * 1. The group count is not a relevant parameter when defining a dRAID
1337877fdebSMatt Macy  *    layout. Only the group width is needed, and *all* groups will have
1347877fdebSMatt Macy  *    the desired size.
1357877fdebSMatt Macy  *
1367877fdebSMatt Macy  * 2. All possible group widths (<= physical disk count) can be supported.
1377877fdebSMatt Macy  *
1387877fdebSMatt Macy  * 3. The logic within vdev_draid.c is simplified when the group width is
1397877fdebSMatt Macy  *    the same for all groups (although some of the logic around computing
1407877fdebSMatt Macy  *    permutation numbers and drive offsets is more complicated).
1417877fdebSMatt Macy  *
1427877fdebSMatt Macy  * N.B. The following array describes all valid dRAID permutation maps.
1437877fdebSMatt Macy  * Each row is used to generate a permutation map for a different number
1447877fdebSMatt Macy  * of children from a unique seed. The seeds were generated and carefully
1457877fdebSMatt Macy  * evaluated by the 'draid' utility in order to provide balanced mappings.
1467877fdebSMatt Macy  * In addition to the seed a checksum of the in-memory mapping is stored
1477877fdebSMatt Macy  * for verification.
1487877fdebSMatt Macy  *
1497877fdebSMatt Macy  * The imbalance ratio of a given failure (e.g. 5 disks wide, child 3 failed,
1507877fdebSMatt Macy  * with a given permutation map) is the ratio of the amounts of I/O that will
1517877fdebSMatt Macy  * be sent to the least and most busy disks when resilvering. The average
1527877fdebSMatt Macy  * imbalance ratio (of a given number of disks and permutation map) is the
1537877fdebSMatt Macy  * average of the ratios of all possible single and double disk failures.
1547877fdebSMatt Macy  *
1557877fdebSMatt Macy  * In order to achieve a low imbalance ratio the number of permutations in
1567877fdebSMatt Macy  * the mapping must be significantly larger than the number of children.
1577877fdebSMatt Macy  * For dRAID the number of permutations has been limited to 512 to minimize
1587877fdebSMatt Macy  * the map size. This does result in a gradually increasing imbalance ratio
1597877fdebSMatt Macy  * as seen in the table below. Increasing the number of permutations for
1607877fdebSMatt Macy  * larger child counts would reduce the imbalance ratio. However, in practice
1617877fdebSMatt Macy  * when there are a large number of children each child is responsible for
1627877fdebSMatt Macy  * fewer total IOs so it's less of a concern.
1637877fdebSMatt Macy  *
1647877fdebSMatt Macy  * Note these values are hard coded and must never be changed.  Existing
1657877fdebSMatt Macy  * pools depend on the same mapping always being generated in order to
1667877fdebSMatt Macy  * read and write from the correct locations.  Any change would make
1677877fdebSMatt Macy  * existing pools completely inaccessible.
1687877fdebSMatt Macy  */
1697877fdebSMatt Macy static const draid_map_t draid_maps[VDEV_DRAID_MAX_MAPS] = {
1707877fdebSMatt Macy 	{   2, 256, 0x89ef3dabbcc7de37, 0x00000000433d433d },	/* 1.000 */
1717877fdebSMatt Macy 	{   3, 256, 0x89a57f3de98121b4, 0x00000000bcd8b7b5 },	/* 1.000 */
1727877fdebSMatt Macy 	{   4, 256, 0xc9ea9ec82340c885, 0x00000001819d7c69 },	/* 1.000 */
1737877fdebSMatt Macy 	{   5, 256, 0xf46733b7f4d47dfd, 0x00000002a1648d74 },	/* 1.010 */
1747877fdebSMatt Macy 	{   6, 256, 0x88c3c62d8585b362, 0x00000003d3b0c2c4 },	/* 1.031 */
1757877fdebSMatt Macy 	{   7, 256, 0x3a65d809b4d1b9d5, 0x000000055c4183ee },	/* 1.043 */
1767877fdebSMatt Macy 	{   8, 256, 0xe98930e3c5d2e90a, 0x00000006edfb0329 },	/* 1.059 */
1777877fdebSMatt Macy 	{   9, 256, 0x5a5430036b982ccb, 0x00000008ceaf6934 },	/* 1.056 */
1787877fdebSMatt Macy 	{  10, 256, 0x92bf389e9eadac74, 0x0000000b26668c09 },	/* 1.072 */
1797877fdebSMatt Macy 	{  11, 256, 0x74ccebf1dcf3ae80, 0x0000000dd691358c },	/* 1.083 */
1807877fdebSMatt Macy 	{  12, 256, 0x8847e41a1a9f5671, 0x00000010a0c63c8e },	/* 1.097 */
1817877fdebSMatt Macy 	{  13, 256, 0x7481b56debf0e637, 0x0000001424121fe4 },	/* 1.100 */
1827877fdebSMatt Macy 	{  14, 256, 0x559b8c44065f8967, 0x00000016ab2ff079 },	/* 1.121 */
1837877fdebSMatt Macy 	{  15, 256, 0x34c49545a2ee7f01, 0x0000001a6028efd6 },	/* 1.103 */
1847877fdebSMatt Macy 	{  16, 256, 0xb85f4fa81a7698f7, 0x0000001e95ff5e66 },	/* 1.111 */
1857877fdebSMatt Macy 	{  17, 256, 0x6353e47b7e47aba0, 0x00000021a81fa0fe },	/* 1.133 */
1867877fdebSMatt Macy 	{  18, 256, 0xaa549746b1cbb81c, 0x00000026f02494c9 },	/* 1.131 */
1877877fdebSMatt Macy 	{  19, 256, 0x892e343f2f31d690, 0x00000029eb392835 },	/* 1.130 */
1887877fdebSMatt Macy 	{  20, 256, 0x76914824db98cc3f, 0x0000003004f31a7c },	/* 1.141 */
1897877fdebSMatt Macy 	{  21, 256, 0x4b3cbabf9cfb1d0f, 0x00000036363a2408 },	/* 1.139 */
1907877fdebSMatt Macy 	{  22, 256, 0xf45c77abb4f035d4, 0x00000038dd0f3e84 },	/* 1.150 */
1917877fdebSMatt Macy 	{  23, 256, 0x5e18bd7f3fd4baf4, 0x0000003f0660391f },	/* 1.174 */
1927877fdebSMatt Macy 	{  24, 256, 0xa7b3a4d285d6503b, 0x000000443dfc9ff6 },	/* 1.168 */
1937877fdebSMatt Macy 	{  25, 256, 0x56ac7dd967521f5a, 0x0000004b03a87eb7 },	/* 1.180 */
1947877fdebSMatt Macy 	{  26, 256, 0x3a42dfda4eb880f7, 0x000000522c719bba },	/* 1.226 */
1957877fdebSMatt Macy 	{  27, 256, 0xd200d2fc6b54bf60, 0x0000005760b4fdf5 },	/* 1.228 */
1967877fdebSMatt Macy 	{  28, 256, 0xc52605bbd486c546, 0x0000005e00d8f74c },	/* 1.217 */
1977877fdebSMatt Macy 	{  29, 256, 0xc761779e63cd762f, 0x00000067be3cd85c },	/* 1.239 */
1987877fdebSMatt Macy 	{  30, 256, 0xca577b1e07f85ca5, 0x0000006f5517f3e4 },	/* 1.238 */
1997877fdebSMatt Macy 	{  31, 256, 0xfd50a593c518b3d4, 0x0000007370e7778f },	/* 1.273 */
2007877fdebSMatt Macy 	{  32, 512, 0xc6c87ba5b042650b, 0x000000f7eb08a156 },	/* 1.191 */
2017877fdebSMatt Macy 	{  33, 512, 0xc3880d0c9d458304, 0x0000010734b5d160 },	/* 1.199 */
2027877fdebSMatt Macy 	{  34, 512, 0xe920927e4d8b2c97, 0x00000118c1edbce0 },	/* 1.195 */
2037877fdebSMatt Macy 	{  35, 512, 0x8da7fcda87bde316, 0x0000012a3e9f9110 },	/* 1.201 */
2047877fdebSMatt Macy 	{  36, 512, 0xcf09937491514a29, 0x0000013bd6a24bef },	/* 1.194 */
2057877fdebSMatt Macy 	{  37, 512, 0x9b5abbf345cbd7cc, 0x0000014b9d90fac3 },	/* 1.237 */
2067877fdebSMatt Macy 	{  38, 512, 0x506312a44668d6a9, 0x0000015e1b5f6148 },	/* 1.242 */
2077877fdebSMatt Macy 	{  39, 512, 0x71659ede62b4755f, 0x00000173ef029bcd },	/* 1.231 */
2087877fdebSMatt Macy 	{  40, 512, 0xa7fde73fb74cf2d7, 0x000001866fb72748 },	/* 1.233 */
2097877fdebSMatt Macy 	{  41, 512, 0x19e8b461a1dea1d3, 0x000001a046f76b23 },	/* 1.271 */
2107877fdebSMatt Macy 	{  42, 512, 0x031c9b868cc3e976, 0x000001afa64c49d3 },	/* 1.263 */
2117877fdebSMatt Macy 	{  43, 512, 0xbaa5125faa781854, 0x000001c76789e278 },	/* 1.270 */
2127877fdebSMatt Macy 	{  44, 512, 0x4ed55052550d721b, 0x000001d800ccd8eb },	/* 1.281 */
2137877fdebSMatt Macy 	{  45, 512, 0x0fd63ddbdff90677, 0x000001f08ad59ed2 },	/* 1.282 */
2147877fdebSMatt Macy 	{  46, 512, 0x36d66546de7fdd6f, 0x000002016f09574b },	/* 1.286 */
2157877fdebSMatt Macy 	{  47, 512, 0x99f997e7eafb69d7, 0x0000021e42e47cb6 },	/* 1.329 */
2167877fdebSMatt Macy 	{  48, 512, 0xbecd9c2571312c5d, 0x000002320fe2872b },	/* 1.286 */
2177877fdebSMatt Macy 	{  49, 512, 0xd97371329e488a32, 0x0000024cd73f2ca7 },	/* 1.322 */
2187877fdebSMatt Macy 	{  50, 512, 0x30e9b136670749ee, 0x000002681c83b0e0 },	/* 1.335 */
2197877fdebSMatt Macy 	{  51, 512, 0x11ad6bc8f47aaeb4, 0x0000027e9261b5d5 },	/* 1.305 */
2207877fdebSMatt Macy 	{  52, 512, 0x68e445300af432c1, 0x0000029aa0eb7dbf },	/* 1.330 */
2217877fdebSMatt Macy 	{  53, 512, 0x910fb561657ea98c, 0x000002b3dca04853 },	/* 1.365 */
2227877fdebSMatt Macy 	{  54, 512, 0xd619693d8ce5e7a5, 0x000002cc280e9c97 },	/* 1.334 */
2237877fdebSMatt Macy 	{  55, 512, 0x24e281f564dbb60a, 0x000002e9fa842713 },	/* 1.364 */
2247877fdebSMatt Macy 	{  56, 512, 0x947a7d3bdaab44c5, 0x000003046680f72e },	/* 1.374 */
2257877fdebSMatt Macy 	{  57, 512, 0x2d44fec9c093e0de, 0x00000324198ba810 },	/* 1.363 */
2267877fdebSMatt Macy 	{  58, 512, 0x87743c272d29bb4c, 0x0000033ec48c9ac9 },	/* 1.401 */
2277877fdebSMatt Macy 	{  59, 512, 0x96aa3b6f67f5d923, 0x0000034faead902c },	/* 1.392 */
2287877fdebSMatt Macy 	{  60, 512, 0x94a4f1faf520b0d3, 0x0000037d713ab005 },	/* 1.360 */
2297877fdebSMatt Macy 	{  61, 512, 0xb13ed3a272f711a2, 0x00000397368f3cbd },	/* 1.396 */
2307877fdebSMatt Macy 	{  62, 512, 0x3b1b11805fa4a64a, 0x000003b8a5e2840c },	/* 1.453 */
2317877fdebSMatt Macy 	{  63, 512, 0x4c74caad9172ba71, 0x000003d4be280290 },	/* 1.437 */
2327877fdebSMatt Macy 	{  64, 512, 0x035ff643923dd29e, 0x000003fad6c355e1 },	/* 1.402 */
2337877fdebSMatt Macy 	{  65, 512, 0x768e9171b11abd3c, 0x0000040eb07fed20 },	/* 1.459 */
2347877fdebSMatt Macy 	{  66, 512, 0x75880e6f78a13ddd, 0x000004433d6acf14 },	/* 1.423 */
2357877fdebSMatt Macy 	{  67, 512, 0x910b9714f698a877, 0x00000451ea65d5db },	/* 1.447 */
2367877fdebSMatt Macy 	{  68, 512, 0x87f5db6f9fdcf5c7, 0x000004732169e3f7 },	/* 1.450 */
2377877fdebSMatt Macy 	{  69, 512, 0x836d4968fbaa3706, 0x000004954068a380 },	/* 1.455 */
2387877fdebSMatt Macy 	{  70, 512, 0xc567d73a036421ab, 0x000004bd7cb7bd3d },	/* 1.463 */
2397877fdebSMatt Macy 	{  71, 512, 0x619df40f240b8fed, 0x000004e376c2e972 },	/* 1.463 */
2407877fdebSMatt Macy 	{  72, 512, 0x42763a680d5bed8e, 0x000005084275c680 },	/* 1.452 */
2417877fdebSMatt Macy 	{  73, 512, 0x5866f064b3230431, 0x0000052906f2c9ab },	/* 1.498 */
2427877fdebSMatt Macy 	{  74, 512, 0x9fa08548b1621a44, 0x0000054708019247 },	/* 1.526 */
2437877fdebSMatt Macy 	{  75, 512, 0xb6053078ce0fc303, 0x00000572cc5c72b0 },	/* 1.491 */
2447877fdebSMatt Macy 	{  76, 512, 0x4a7aad7bf3890923, 0x0000058e987bc8e9 },	/* 1.470 */
2457877fdebSMatt Macy 	{  77, 512, 0xe165613fd75b5a53, 0x000005c20473a211 },	/* 1.527 */
2467877fdebSMatt Macy 	{  78, 512, 0x3ff154ac878163a6, 0x000005d659194bf3 },	/* 1.509 */
2477877fdebSMatt Macy 	{  79, 512, 0x24b93ade0aa8a532, 0x0000060a201c4f8e },	/* 1.569 */
2487877fdebSMatt Macy 	{  80, 512, 0xc18e2d14cd9bb554, 0x0000062c55cfe48c },	/* 1.555 */
2497877fdebSMatt Macy 	{  81, 512, 0x98cc78302feb58b6, 0x0000066656a07194 },	/* 1.509 */
2507877fdebSMatt Macy 	{  82, 512, 0xc6c5fd5a2abc0543, 0x0000067cff94fbf8 },	/* 1.596 */
2517877fdebSMatt Macy 	{  83, 512, 0xa7962f514acbba21, 0x000006ab7b5afa2e },	/* 1.568 */
2527877fdebSMatt Macy 	{  84, 512, 0xba02545069ddc6dc, 0x000006d19861364f },	/* 1.541 */
2537877fdebSMatt Macy 	{  85, 512, 0x447c73192c35073e, 0x000006fce315ce35 },	/* 1.623 */
2547877fdebSMatt Macy 	{  86, 512, 0x48beef9e2d42b0c2, 0x00000720a8e38b6b },	/* 1.620 */
2557877fdebSMatt Macy 	{  87, 512, 0x4874cf98541a35e0, 0x00000758382a2273 },	/* 1.597 */
2567877fdebSMatt Macy 	{  88, 512, 0xad4cf8333a31127a, 0x00000781e1651b1b },	/* 1.575 */
2577877fdebSMatt Macy 	{  89, 512, 0x47ae4859d57888c1, 0x000007b27edbe5bc },	/* 1.627 */
2587877fdebSMatt Macy 	{  90, 512, 0x06f7723cfe5d1891, 0x000007dc2a96d8eb },	/* 1.596 */
2597877fdebSMatt Macy 	{  91, 512, 0xd4e44218d660576d, 0x0000080ac46f02d5 },	/* 1.622 */
2607877fdebSMatt Macy 	{  92, 512, 0x7066702b0d5be1f2, 0x00000832c96d154e },	/* 1.695 */
2617877fdebSMatt Macy 	{  93, 512, 0x011209b4f9e11fb9, 0x0000085eefda104c },	/* 1.605 */
2627877fdebSMatt Macy 	{  94, 512, 0x47ffba30a0b35708, 0x00000899badc32dc },	/* 1.625 */
2637877fdebSMatt Macy 	{  95, 512, 0x1a95a6ac4538aaa8, 0x000008b6b69a42b2 },	/* 1.687 */
2647877fdebSMatt Macy 	{  96, 512, 0xbda2b239bb2008eb, 0x000008f22d2de38a },	/* 1.621 */
2657877fdebSMatt Macy 	{  97, 512, 0x7ffa0bea90355c6c, 0x0000092e5b23b816 },	/* 1.699 */
2667877fdebSMatt Macy 	{  98, 512, 0x1d56ba34be426795, 0x0000094f482e5d1b },	/* 1.688 */
2677877fdebSMatt Macy 	{  99, 512, 0x0aa89d45c502e93d, 0x00000977d94a98ce },	/* 1.642 */
2687877fdebSMatt Macy 	{ 100, 512, 0x54369449f6857774, 0x000009c06c9b34cc },	/* 1.683 */
2697877fdebSMatt Macy 	{ 101, 512, 0xf7d4dd8445b46765, 0x000009e5dc542259 },	/* 1.755 */
2707877fdebSMatt Macy 	{ 102, 512, 0xfa8866312f169469, 0x00000a16b54eae93 },	/* 1.692 */
2717877fdebSMatt Macy 	{ 103, 512, 0xd8a5aea08aef3ff9, 0x00000a381d2cbfe7 },	/* 1.747 */
2727877fdebSMatt Macy 	{ 104, 512, 0x66bcd2c3d5f9ef0e, 0x00000a8191817be7 },	/* 1.751 */
2737877fdebSMatt Macy 	{ 105, 512, 0x3fb13a47a012ec81, 0x00000ab562b9a254 },	/* 1.751 */
2747877fdebSMatt Macy 	{ 106, 512, 0x43100f01c9e5e3ca, 0x00000aeee84c185f },	/* 1.726 */
2757877fdebSMatt Macy 	{ 107, 512, 0xca09c50ccee2d054, 0x00000b1c359c047d },	/* 1.788 */
2767877fdebSMatt Macy 	{ 108, 512, 0xd7176732ac503f9b, 0x00000b578bc52a73 },	/* 1.740 */
2777877fdebSMatt Macy 	{ 109, 512, 0xed206e51f8d9422d, 0x00000b8083e0d960 },	/* 1.780 */
2787877fdebSMatt Macy 	{ 110, 512, 0x17ead5dc6ba0dcd6, 0x00000bcfb1a32ca8 },	/* 1.836 */
2797877fdebSMatt Macy 	{ 111, 512, 0x5f1dc21e38a969eb, 0x00000c0171becdd6 },	/* 1.778 */
2807877fdebSMatt Macy 	{ 112, 512, 0xddaa973de33ec528, 0x00000c3edaba4b95 },	/* 1.831 */
2817877fdebSMatt Macy 	{ 113, 512, 0x2a5eccd7735a3630, 0x00000c630664e7df },	/* 1.825 */
2827877fdebSMatt Macy 	{ 114, 512, 0xafcccee5c0b71446, 0x00000cb65392f6e4 },	/* 1.826 */
2837877fdebSMatt Macy 	{ 115, 512, 0x8fa30c5e7b147e27, 0x00000cd4db391e55 },	/* 1.843 */
2847877fdebSMatt Macy 	{ 116, 512, 0x5afe0711fdfafd82, 0x00000d08cb4ec35d },	/* 1.826 */
2857877fdebSMatt Macy 	{ 117, 512, 0x533a6090238afd4c, 0x00000d336f115d1b },	/* 1.803 */
2867877fdebSMatt Macy 	{ 118, 512, 0x90cf11b595e39a84, 0x00000d8e041c2048 },	/* 1.857 */
2877877fdebSMatt Macy 	{ 119, 512, 0x0d61a3b809444009, 0x00000dcb798afe35 },	/* 1.877 */
2887877fdebSMatt Macy 	{ 120, 512, 0x7f34da0f54b0d114, 0x00000df3922664e1 },	/* 1.849 */
2897877fdebSMatt Macy 	{ 121, 512, 0xa52258d5b72f6551, 0x00000e4d37a9872d },	/* 1.867 */
2907877fdebSMatt Macy 	{ 122, 512, 0xc1de54d7672878db, 0x00000e6583a94cf6 },	/* 1.978 */
2917877fdebSMatt Macy 	{ 123, 512, 0x1d03354316a414ab, 0x00000ebffc50308d },	/* 1.947 */
2927877fdebSMatt Macy 	{ 124, 512, 0xcebdcc377665412c, 0x00000edee1997cea },	/* 1.865 */
2937877fdebSMatt Macy 	{ 125, 512, 0x4ddd4c04b1a12344, 0x00000f21d64b373f },	/* 1.881 */
2947877fdebSMatt Macy 	{ 126, 512, 0x64fc8f94e3973658, 0x00000f8f87a8896b },	/* 1.882 */
2957877fdebSMatt Macy 	{ 127, 512, 0x68765f78034a334e, 0x00000fb8fe62197e },	/* 1.867 */
2967877fdebSMatt Macy 	{ 128, 512, 0xaf36b871a303e816, 0x00000fec6f3afb1e },	/* 1.972 */
2977877fdebSMatt Macy 	{ 129, 512, 0x2a4cbf73866c3a28, 0x00001027febfe4e5 },	/* 1.896 */
2987877fdebSMatt Macy 	{ 130, 512, 0x9cb128aacdcd3b2f, 0x0000106aa8ac569d },	/* 1.965 */
2997877fdebSMatt Macy 	{ 131, 512, 0x5511d41c55869124, 0x000010bbd755ddf1 },	/* 1.963 */
3007877fdebSMatt Macy 	{ 132, 512, 0x42f92461937f284a, 0x000010fb8bceb3b5 },	/* 1.925 */
3017877fdebSMatt Macy 	{ 133, 512, 0xe2d89a1cf6f1f287, 0x0000114cf5331e34 },	/* 1.862 */
3027877fdebSMatt Macy 	{ 134, 512, 0xdc631a038956200e, 0x0000116428d2adc5 },	/* 2.042 */
3037877fdebSMatt Macy 	{ 135, 512, 0xb2e5ac222cd236be, 0x000011ca88e4d4d2 },	/* 1.935 */
3047877fdebSMatt Macy 	{ 136, 512, 0xbc7d8236655d88e7, 0x000011e39cb94e66 },	/* 2.005 */
3057877fdebSMatt Macy 	{ 137, 512, 0x073e02d88d2d8e75, 0x0000123136c7933c },	/* 2.041 */
3067877fdebSMatt Macy 	{ 138, 512, 0x3ddb9c3873166be0, 0x00001280e4ec6d52 },	/* 1.997 */
3077877fdebSMatt Macy 	{ 139, 512, 0x7d3b1a845420e1b5, 0x000012c2e7cd6a44 },	/* 1.996 */
3087877fdebSMatt Macy 	{ 140, 512, 0x60102308aa7b2a6c, 0x000012fc490e6c7d },	/* 2.053 */
3097877fdebSMatt Macy 	{ 141, 512, 0xdb22bb2f9eb894aa, 0x00001343f5a85a1a },	/* 1.971 */
3107877fdebSMatt Macy 	{ 142, 512, 0xd853f879a13b1606, 0x000013bb7d5f9048 },	/* 2.018 */
3117877fdebSMatt Macy 	{ 143, 512, 0x001620a03f804b1d, 0x000013e74cc794fd },	/* 1.961 */
3127877fdebSMatt Macy 	{ 144, 512, 0xfdb52dda76fbf667, 0x00001442d2f22480 },	/* 2.046 */
3137877fdebSMatt Macy 	{ 145, 512, 0xa9160110f66e24ff, 0x0000144b899f9dbb },	/* 1.968 */
3147877fdebSMatt Macy 	{ 146, 512, 0x77306a30379ae03b, 0x000014cb98eb1f81 },	/* 2.143 */
3157877fdebSMatt Macy 	{ 147, 512, 0x14f5985d2752319d, 0x000014feab821fc9 },	/* 2.064 */
3167877fdebSMatt Macy 	{ 148, 512, 0xa4b8ff11de7863f8, 0x0000154a0e60b9c9 },	/* 2.023 */
3177877fdebSMatt Macy 	{ 149, 512, 0x44b345426455c1b3, 0x000015999c3c569c },	/* 2.136 */
3187877fdebSMatt Macy 	{ 150, 512, 0x272677826049b46c, 0x000015c9697f4b92 },	/* 2.063 */
3197877fdebSMatt Macy 	{ 151, 512, 0x2f9216e2cd74fe40, 0x0000162b1f7bbd39 },	/* 1.974 */
3207877fdebSMatt Macy 	{ 152, 512, 0x706ae3e763ad8771, 0x00001661371c55e1 },	/* 2.210 */
3217877fdebSMatt Macy 	{ 153, 512, 0xf7fd345307c2480e, 0x000016e251f28b6a },	/* 2.006 */
3227877fdebSMatt Macy 	{ 154, 512, 0x6e94e3d26b3139eb, 0x000016f2429bb8c6 },	/* 2.193 */
3237877fdebSMatt Macy 	{ 155, 512, 0x5458bbfbb781fcba, 0x0000173efdeca1b9 },	/* 2.163 */
3247877fdebSMatt Macy 	{ 156, 512, 0xa80e2afeccd93b33, 0x000017bfdcb78adc },	/* 2.046 */
3257877fdebSMatt Macy 	{ 157, 512, 0x1e4ccbb22796cf9d, 0x00001826fdcc39c9 },	/* 2.084 */
3267877fdebSMatt Macy 	{ 158, 512, 0x8fba4b676aaa3663, 0x00001841a1379480 },	/* 2.264 */
3277877fdebSMatt Macy 	{ 159, 512, 0xf82b843814b315fa, 0x000018886e19b8a3 },	/* 2.074 */
3287877fdebSMatt Macy 	{ 160, 512, 0x7f21e920ecf753a3, 0x0000191812ca0ea7 },	/* 2.282 */
3297877fdebSMatt Macy 	{ 161, 512, 0x48bb8ea2c4caa620, 0x0000192f310faccf },	/* 2.148 */
3307877fdebSMatt Macy 	{ 162, 512, 0x5cdb652b4952c91b, 0x0000199e1d7437c7 },	/* 2.355 */
3317877fdebSMatt Macy 	{ 163, 512, 0x6ac1ba6f78c06cd4, 0x000019cd11f82c70 },	/* 2.164 */
3327877fdebSMatt Macy 	{ 164, 512, 0x9faf5f9ca2669a56, 0x00001a18d5431f6a },	/* 2.393 */
3337877fdebSMatt Macy 	{ 165, 512, 0xaa57e9383eb01194, 0x00001a9e7d253d85 },	/* 2.178 */
3347877fdebSMatt Macy 	{ 166, 512, 0x896967bf495c34d2, 0x00001afb8319b9fc },	/* 2.334 */
3357877fdebSMatt Macy 	{ 167, 512, 0xdfad5f05de225f1b, 0x00001b3a59c3093b },	/* 2.266 */
3367877fdebSMatt Macy 	{ 168, 512, 0xfd299a99f9f2abdd, 0x00001bb6f1a10799 },	/* 2.304 */
3377877fdebSMatt Macy 	{ 169, 512, 0xdda239e798fe9fd4, 0x00001bfae0c9692d },	/* 2.218 */
3387877fdebSMatt Macy 	{ 170, 512, 0x5fca670414a32c3e, 0x00001c22129dbcff },	/* 2.377 */
3397877fdebSMatt Macy 	{ 171, 512, 0x1bb8934314b087de, 0x00001c955db36cd0 },	/* 2.155 */
3407877fdebSMatt Macy 	{ 172, 512, 0xd96394b4b082200d, 0x00001cfc8619b7e6 },	/* 2.404 */
3417877fdebSMatt Macy 	{ 173, 512, 0xb612a7735b1c8cbc, 0x00001d303acdd585 },	/* 2.205 */
3427877fdebSMatt Macy 	{ 174, 512, 0x28e7430fe5875fe1, 0x00001d7ed5b3697d },	/* 2.359 */
3437877fdebSMatt Macy 	{ 175, 512, 0x5038e89efdd981b9, 0x00001dc40ec35c59 },	/* 2.158 */
3447877fdebSMatt Macy 	{ 176, 512, 0x075fd78f1d14db7c, 0x00001e31c83b4a2b },	/* 2.614 */
3457877fdebSMatt Macy 	{ 177, 512, 0xc50fafdb5021be15, 0x00001e7cdac82fbc },	/* 2.239 */
3467877fdebSMatt Macy 	{ 178, 512, 0xe6dc7572ce7b91c7, 0x00001edd8bb454fc },	/* 2.493 */
3477877fdebSMatt Macy 	{ 179, 512, 0x21f7843e7beda537, 0x00001f3a8e019d6c },	/* 2.327 */
3487877fdebSMatt Macy 	{ 180, 512, 0xc83385e20b43ec82, 0x00001f70735ec137 },	/* 2.231 */
3497877fdebSMatt Macy 	{ 181, 512, 0xca818217dddb21fd, 0x0000201ca44c5a3c },	/* 2.237 */
3507877fdebSMatt Macy 	{ 182, 512, 0xe6035defea48f933, 0x00002038e3346658 },	/* 2.691 */
3517877fdebSMatt Macy 	{ 183, 512, 0x47262a4f953dac5a, 0x000020c2e554314e },	/* 2.170 */
3527877fdebSMatt Macy 	{ 184, 512, 0xe24c7246260873ea, 0x000021197e618d64 },	/* 2.600 */
3537877fdebSMatt Macy 	{ 185, 512, 0xeef6b57c9b58e9e1, 0x0000217ea48ecddc },	/* 2.391 */
3547877fdebSMatt Macy 	{ 186, 512, 0x2becd3346e386142, 0x000021c496d4a5f9 },	/* 2.677 */
3557877fdebSMatt Macy 	{ 187, 512, 0x63c6207bdf3b40a3, 0x0000220e0f2eec0c },	/* 2.410 */
3567877fdebSMatt Macy 	{ 188, 512, 0x3056ce8989767d4b, 0x0000228eb76cd137 },	/* 2.776 */
3577877fdebSMatt Macy 	{ 189, 512, 0x91af61c307cee780, 0x000022e17e2ea501 },	/* 2.266 */
3587877fdebSMatt Macy 	{ 190, 512, 0xda359da225f6d54f, 0x00002358a2debc19 },	/* 2.717 */
3597877fdebSMatt Macy 	{ 191, 512, 0x0a5f7a2a55607ba0, 0x0000238a79dac18c },	/* 2.474 */
3607877fdebSMatt Macy 	{ 192, 512, 0x27bb75bf5224638a, 0x00002403a58e2351 },	/* 2.673 */
3617877fdebSMatt Macy 	{ 193, 512, 0x1ebfdb94630f5d0f, 0x00002492a10cb339 },	/* 2.420 */
3627877fdebSMatt Macy 	{ 194, 512, 0x6eae5e51d9c5f6fb, 0x000024ce4bf98715 },	/* 2.898 */
3637877fdebSMatt Macy 	{ 195, 512, 0x08d903b4daedc2e0, 0x0000250d1e15886c },	/* 2.363 */
3647877fdebSMatt Macy 	{ 196, 512, 0xc722a2f7fa7cd686, 0x0000258a99ed0c9e },	/* 2.747 */
3657877fdebSMatt Macy 	{ 197, 512, 0x8f71faf0e54e361d, 0x000025dee11976f5 },	/* 2.531 */
3667877fdebSMatt Macy 	{ 198, 512, 0x87f64695c91a54e7, 0x0000264e00a43da0 },	/* 2.707 */
3677877fdebSMatt Macy 	{ 199, 512, 0xc719cbac2c336b92, 0x000026d327277ac1 },	/* 2.315 */
3687877fdebSMatt Macy 	{ 200, 512, 0xe7e647afaf771ade, 0x000027523a5c44bf },	/* 3.012 */
3697877fdebSMatt Macy 	{ 201, 512, 0x12d4b5c38ce8c946, 0x0000273898432545 },	/* 2.378 */
3707877fdebSMatt Macy 	{ 202, 512, 0xf2e0cd4067bdc94a, 0x000027e47bb2c935 },	/* 2.969 */
3717877fdebSMatt Macy 	{ 203, 512, 0x21b79f14d6d947d3, 0x0000281e64977f0d },	/* 2.594 */
3727877fdebSMatt Macy 	{ 204, 512, 0x515093f952f18cd6, 0x0000289691a473fd },	/* 2.763 */
3737877fdebSMatt Macy 	{ 205, 512, 0xd47b160a1b1022c8, 0x00002903e8b52411 },	/* 2.457 */
3747877fdebSMatt Macy 	{ 206, 512, 0xc02fc96684715a16, 0x0000297515608601 },	/* 3.057 */
3757877fdebSMatt Macy 	{ 207, 512, 0xef51e68efba72ed0, 0x000029ef73604804 },	/* 2.590 */
3767877fdebSMatt Macy 	{ 208, 512, 0x9e3be6e5448b4f33, 0x00002a2846ed074b },	/* 3.047 */
3777877fdebSMatt Macy 	{ 209, 512, 0x81d446c6d5fec063, 0x00002a92ca693455 },	/* 2.676 */
3787877fdebSMatt Macy 	{ 210, 512, 0xff215de8224e57d5, 0x00002b2271fe3729 },	/* 2.993 */
3797877fdebSMatt Macy 	{ 211, 512, 0xe2524d9ba8f69796, 0x00002b64b99c3ba2 },	/* 2.457 */
3807877fdebSMatt Macy 	{ 212, 512, 0xf6b28e26097b7e4b, 0x00002bd768b6e068 },	/* 3.182 */
3817877fdebSMatt Macy 	{ 213, 512, 0x893a487f30ce1644, 0x00002c67f722b4b2 },	/* 2.563 */
3827877fdebSMatt Macy 	{ 214, 512, 0x386566c3fc9871df, 0x00002cc1cf8b4037 },	/* 3.025 */
3837877fdebSMatt Macy 	{ 215, 512, 0x1e0ed78edf1f558a, 0x00002d3948d36c7f },	/* 2.730 */
3847877fdebSMatt Macy 	{ 216, 512, 0xe3bc20c31e61f113, 0x00002d6d6b12e025 },	/* 3.036 */
3857877fdebSMatt Macy 	{ 217, 512, 0xd6c3ad2e23021882, 0x00002deff7572241 },	/* 2.722 */
3867877fdebSMatt Macy 	{ 218, 512, 0xb4a9f95cf0f69c5a, 0x00002e67d537aa36 },	/* 3.356 */
3877877fdebSMatt Macy 	{ 219, 512, 0x6e98ed6f6c38e82f, 0x00002e9720626789 },	/* 2.697 */
3887877fdebSMatt Macy 	{ 220, 512, 0x2e01edba33fddac7, 0x00002f407c6b0198 },	/* 2.979 */
3897877fdebSMatt Macy 	{ 221, 512, 0x559d02e1f5f57ccc, 0x00002fb6a5ab4f24 },	/* 2.858 */
3907877fdebSMatt Macy 	{ 222, 512, 0xac18f5a916adcd8e, 0x0000304ae1c5c57e },	/* 3.258 */
3917877fdebSMatt Macy 	{ 223, 512, 0x15789fbaddb86f4b, 0x0000306f6e019c78 },	/* 2.693 */
3927877fdebSMatt Macy 	{ 224, 512, 0xf4a9c36d5bc4c408, 0x000030da40434213 },	/* 3.259 */
3937877fdebSMatt Macy 	{ 225, 512, 0xf640f90fd2727f44, 0x00003189ed37b90c },	/* 2.733 */
3947877fdebSMatt Macy 	{ 226, 512, 0xb5313d390d61884a, 0x000031e152616b37 },	/* 3.235 */
3957877fdebSMatt Macy 	{ 227, 512, 0x4bae6b3ce9160939, 0x0000321f40aeac42 },	/* 2.983 */
3967877fdebSMatt Macy 	{ 228, 512, 0x838c34480f1a66a1, 0x000032f389c0f78e },	/* 3.308 */
3977877fdebSMatt Macy 	{ 229, 512, 0xb1c4a52c8e3d6060, 0x0000330062a40284 },	/* 2.715 */
3987877fdebSMatt Macy 	{ 230, 512, 0xe0f1110c6d0ed822, 0x0000338be435644f },	/* 3.540 */
3997877fdebSMatt Macy 	{ 231, 512, 0x9f1a8ccdcea68d4b, 0x000034045a4e97e1 },	/* 2.779 */
4007877fdebSMatt Macy 	{ 232, 512, 0x3261ed62223f3099, 0x000034702cfc401c },	/* 3.084 */
4017877fdebSMatt Macy 	{ 233, 512, 0xf2191e2311022d65, 0x00003509dd19c9fc },	/* 2.987 */
4027877fdebSMatt Macy 	{ 234, 512, 0xf102a395c2033abc, 0x000035654dc96fae },	/* 3.341 */
4037877fdebSMatt Macy 	{ 235, 512, 0x11fe378f027906b6, 0x000035b5193b0264 },	/* 2.793 */
4047877fdebSMatt Macy 	{ 236, 512, 0xf777f2c026b337aa, 0x000036704f5d9297 },	/* 3.518 */
4057877fdebSMatt Macy 	{ 237, 512, 0x1b04e9c2ee143f32, 0x000036dfbb7af218 },	/* 2.962 */
4067877fdebSMatt Macy 	{ 238, 512, 0x2fcec95266f9352c, 0x00003785c8df24a9 },	/* 3.196 */
4077877fdebSMatt Macy 	{ 239, 512, 0xfe2b0e47e427dd85, 0x000037cbdf5da729 },	/* 2.914 */
4087877fdebSMatt Macy 	{ 240, 512, 0x72b49bf2225f6c6d, 0x0000382227c15855 },	/* 3.408 */
4097877fdebSMatt Macy 	{ 241, 512, 0x50486b43df7df9c7, 0x0000389b88be6453 },	/* 2.903 */
4107877fdebSMatt Macy 	{ 242, 512, 0x5192a3e53181c8ab, 0x000038ddf3d67263 },	/* 3.778 */
4117877fdebSMatt Macy 	{ 243, 512, 0xe9f5d8365296fd5e, 0x0000399f1c6c9e9c },	/* 3.026 */
4127877fdebSMatt Macy 	{ 244, 512, 0xc740263f0301efa8, 0x00003a147146512d },	/* 3.347 */
4137877fdebSMatt Macy 	{ 245, 512, 0x23cd0f2b5671e67d, 0x00003ab10bcc0d9d },	/* 3.212 */
4147877fdebSMatt Macy 	{ 246, 512, 0x002ccc7e5cd41390, 0x00003ad6cd14a6c0 },	/* 3.482 */
4157877fdebSMatt Macy 	{ 247, 512, 0x9aafb3c02544b31b, 0x00003b8cb8779fb0 },	/* 3.146 */
4167877fdebSMatt Macy 	{ 248, 512, 0x72ba07a78b121999, 0x00003c24142a5a3f },	/* 3.626 */
4177877fdebSMatt Macy 	{ 249, 512, 0x3d784aa58edfc7b4, 0x00003cd084817d99 },	/* 2.952 */
4187877fdebSMatt Macy 	{ 250, 512, 0xaab750424d8004af, 0x00003d506a8e098e },	/* 3.463 */
4197877fdebSMatt Macy 	{ 251, 512, 0x84403fcf8e6b5ca2, 0x00003d4c54c2aec4 },	/* 3.131 */
4207877fdebSMatt Macy 	{ 252, 512, 0x71eb7455ec98e207, 0x00003e655715cf2c },	/* 3.538 */
4217877fdebSMatt Macy 	{ 253, 512, 0xd752b4f19301595b, 0x00003ecd7b2ca5ac },	/* 2.974 */
4227877fdebSMatt Macy 	{ 254, 512, 0xc4674129750499de, 0x00003e99e86d3e95 },	/* 3.843 */
4237877fdebSMatt Macy 	{ 255, 512, 0x9772baff5cd12ef5, 0x00003f895c019841 },	/* 3.088 */
4247877fdebSMatt Macy };
4257877fdebSMatt Macy 
4267877fdebSMatt Macy /*
4277877fdebSMatt Macy  * Verify the map is valid. Each device index must appear exactly
4287877fdebSMatt Macy  * once in every row, and the permutation array checksum must match.
4297877fdebSMatt Macy  */
4307877fdebSMatt Macy static int
verify_perms(uint8_t * perms,uint64_t children,uint64_t nperms,uint64_t checksum)4317877fdebSMatt Macy verify_perms(uint8_t *perms, uint64_t children, uint64_t nperms,
4327877fdebSMatt Macy     uint64_t checksum)
4337877fdebSMatt Macy {
4347877fdebSMatt Macy 	int countssz = sizeof (uint16_t) * children;
4357877fdebSMatt Macy 	uint16_t *counts = kmem_zalloc(countssz, KM_SLEEP);
4367877fdebSMatt Macy 
4377877fdebSMatt Macy 	for (int i = 0; i < nperms; i++) {
4387877fdebSMatt Macy 		for (int j = 0; j < children; j++) {
4397877fdebSMatt Macy 			uint8_t val = perms[(i * children) + j];
4407877fdebSMatt Macy 
4417877fdebSMatt Macy 			if (val >= children || counts[val] != i) {
4427877fdebSMatt Macy 				kmem_free(counts, countssz);
4437877fdebSMatt Macy 				return (EINVAL);
4447877fdebSMatt Macy 			}
4457877fdebSMatt Macy 
4467877fdebSMatt Macy 			counts[val]++;
4477877fdebSMatt Macy 		}
4487877fdebSMatt Macy 	}
4497877fdebSMatt Macy 
4507877fdebSMatt Macy 	if (checksum != 0) {
4517877fdebSMatt Macy 		int permssz = sizeof (uint8_t) * children * nperms;
4527877fdebSMatt Macy 		zio_cksum_t cksum;
4537877fdebSMatt Macy 
4547877fdebSMatt Macy 		fletcher_4_native_varsize(perms, permssz, &cksum);
4557877fdebSMatt Macy 
4567877fdebSMatt Macy 		if (checksum != cksum.zc_word[0]) {
4577877fdebSMatt Macy 			kmem_free(counts, countssz);
4587877fdebSMatt Macy 			return (ECKSUM);
4597877fdebSMatt Macy 		}
4607877fdebSMatt Macy 	}
4617877fdebSMatt Macy 
4627877fdebSMatt Macy 	kmem_free(counts, countssz);
4637877fdebSMatt Macy 
4647877fdebSMatt Macy 	return (0);
4657877fdebSMatt Macy }
4667877fdebSMatt Macy 
4677877fdebSMatt Macy /*
4687877fdebSMatt Macy  * Generate the permutation array for the draid_map_t.  These maps control
4697877fdebSMatt Macy  * the placement of all data in a dRAID.  Therefore it's critical that the
4707877fdebSMatt Macy  * seed always generates the same mapping.  We provide our own pseudo-random
4717877fdebSMatt Macy  * number generator for this purpose.
4727877fdebSMatt Macy  */
4737877fdebSMatt Macy int
vdev_draid_generate_perms(const draid_map_t * map,uint8_t ** permsp)4747877fdebSMatt Macy vdev_draid_generate_perms(const draid_map_t *map, uint8_t **permsp)
4757877fdebSMatt Macy {
4767877fdebSMatt Macy 	VERIFY3U(map->dm_children, >=, VDEV_DRAID_MIN_CHILDREN);
4777877fdebSMatt Macy 	VERIFY3U(map->dm_children, <=, VDEV_DRAID_MAX_CHILDREN);
4787877fdebSMatt Macy 	VERIFY3U(map->dm_seed, !=, 0);
4797877fdebSMatt Macy 	VERIFY3U(map->dm_nperms, !=, 0);
4807877fdebSMatt Macy 	VERIFY3P(map->dm_perms, ==, NULL);
4817877fdebSMatt Macy 
4827877fdebSMatt Macy #ifdef _KERNEL
4837877fdebSMatt Macy 	/*
4847877fdebSMatt Macy 	 * The kernel code always provides both a map_seed and checksum.
4857877fdebSMatt Macy 	 * Only the tests/zfs-tests/cmd/draid/draid.c utility will provide
4867877fdebSMatt Macy 	 * a zero checksum when generating new candidate maps.
4877877fdebSMatt Macy 	 */
4887877fdebSMatt Macy 	VERIFY3U(map->dm_checksum, !=, 0);
4897877fdebSMatt Macy #endif
4907877fdebSMatt Macy 	uint64_t children = map->dm_children;
4917877fdebSMatt Macy 	uint64_t nperms = map->dm_nperms;
4927877fdebSMatt Macy 	int rowsz = sizeof (uint8_t) * children;
4937877fdebSMatt Macy 	int permssz = rowsz * nperms;
4947877fdebSMatt Macy 	uint8_t *perms;
4957877fdebSMatt Macy 
4967877fdebSMatt Macy 	/* Allocate the permutation array */
4977877fdebSMatt Macy 	perms = vmem_alloc(permssz, KM_SLEEP);
4987877fdebSMatt Macy 
4997877fdebSMatt Macy 	/* Setup an initial row with a known pattern */
5007877fdebSMatt Macy 	uint8_t *initial_row = kmem_alloc(rowsz, KM_SLEEP);
5017877fdebSMatt Macy 	for (int i = 0; i < children; i++)
5027877fdebSMatt Macy 		initial_row[i] = i;
5037877fdebSMatt Macy 
5047877fdebSMatt Macy 	uint64_t draid_seed[2] = { VDEV_DRAID_SEED, map->dm_seed };
5057877fdebSMatt Macy 	uint8_t *current_row, *previous_row = initial_row;
5067877fdebSMatt Macy 
5077877fdebSMatt Macy 	/*
5087877fdebSMatt Macy 	 * Perform a Fisher-Yates shuffle of each row using the previous
5097877fdebSMatt Macy 	 * row as the starting point.  An initial_row with known pattern
5107877fdebSMatt Macy 	 * is used as the input for the first row.
5117877fdebSMatt Macy 	 */
5127877fdebSMatt Macy 	for (int i = 0; i < nperms; i++) {
5137877fdebSMatt Macy 		current_row = &perms[i * children];
5147877fdebSMatt Macy 		memcpy(current_row, previous_row, rowsz);
5157877fdebSMatt Macy 
5167877fdebSMatt Macy 		for (int j = children - 1; j > 0; j--) {
5177877fdebSMatt Macy 			uint64_t k = vdev_draid_rand(draid_seed) % (j + 1);
5187877fdebSMatt Macy 			uint8_t val = current_row[j];
5197877fdebSMatt Macy 			current_row[j] = current_row[k];
5207877fdebSMatt Macy 			current_row[k] = val;
5217877fdebSMatt Macy 		}
5227877fdebSMatt Macy 
5237877fdebSMatt Macy 		previous_row = current_row;
5247877fdebSMatt Macy 	}
5257877fdebSMatt Macy 
5267877fdebSMatt Macy 	kmem_free(initial_row, rowsz);
5277877fdebSMatt Macy 
5287877fdebSMatt Macy 	int error = verify_perms(perms, children, nperms, map->dm_checksum);
5297877fdebSMatt Macy 	if (error) {
5307877fdebSMatt Macy 		vmem_free(perms, permssz);
5317877fdebSMatt Macy 		return (error);
5327877fdebSMatt Macy 	}
5337877fdebSMatt Macy 
5347877fdebSMatt Macy 	*permsp = perms;
5357877fdebSMatt Macy 
5367877fdebSMatt Macy 	return (0);
5377877fdebSMatt Macy }
5387877fdebSMatt Macy 
5397877fdebSMatt Macy /*
5407877fdebSMatt Macy  * Lookup the fixed draid_map_t for the requested number of children.
5417877fdebSMatt Macy  */
5427877fdebSMatt Macy int
vdev_draid_lookup_map(uint64_t children,const draid_map_t ** mapp)5437877fdebSMatt Macy vdev_draid_lookup_map(uint64_t children, const draid_map_t **mapp)
5447877fdebSMatt Macy {
545c7046f76SMartin Matuska 	for (int i = 0; i < VDEV_DRAID_MAX_MAPS; i++) {
5467877fdebSMatt Macy 		if (draid_maps[i].dm_children == children) {
5477877fdebSMatt Macy 			*mapp = &draid_maps[i];
5487877fdebSMatt Macy 			return (0);
5497877fdebSMatt Macy 		}
5507877fdebSMatt Macy 	}
5517877fdebSMatt Macy 
5527877fdebSMatt Macy 	return (ENOENT);
5537877fdebSMatt Macy }
5547877fdebSMatt Macy 
5557877fdebSMatt Macy /*
5567877fdebSMatt Macy  * Lookup the permutation array and iteration id for the provided offset.
5577877fdebSMatt Macy  */
5587877fdebSMatt Macy static void
vdev_draid_get_perm(vdev_draid_config_t * vdc,uint64_t pindex,uint8_t ** base,uint64_t * iter)5597877fdebSMatt Macy vdev_draid_get_perm(vdev_draid_config_t *vdc, uint64_t pindex,
5607877fdebSMatt Macy     uint8_t **base, uint64_t *iter)
5617877fdebSMatt Macy {
5627877fdebSMatt Macy 	uint64_t ncols = vdc->vdc_children;
5637877fdebSMatt Macy 	uint64_t poff = pindex % (vdc->vdc_nperms * ncols);
5647877fdebSMatt Macy 
5657877fdebSMatt Macy 	*base = vdc->vdc_perms + (poff / ncols) * ncols;
5667877fdebSMatt Macy 	*iter = poff % ncols;
5677877fdebSMatt Macy }
5687877fdebSMatt Macy 
5697877fdebSMatt Macy static inline uint64_t
vdev_draid_permute_id(vdev_draid_config_t * vdc,uint8_t * base,uint64_t iter,uint64_t index)5707877fdebSMatt Macy vdev_draid_permute_id(vdev_draid_config_t *vdc,
5717877fdebSMatt Macy     uint8_t *base, uint64_t iter, uint64_t index)
5727877fdebSMatt Macy {
5737877fdebSMatt Macy 	return ((base[index] + iter) % vdc->vdc_children);
5747877fdebSMatt Macy }
5757877fdebSMatt Macy 
5767877fdebSMatt Macy /*
5777877fdebSMatt Macy  * Return the asize which is the psize rounded up to a full group width.
5787877fdebSMatt Macy  * i.e. vdev_draid_psize_to_asize().
5797877fdebSMatt Macy  */
5807877fdebSMatt Macy static uint64_t
vdev_draid_psize_to_asize(vdev_t * vd,uint64_t psize,uint64_t txg)581*071ab5a1SMartin Matuska vdev_draid_psize_to_asize(vdev_t *vd, uint64_t psize, uint64_t txg)
5827877fdebSMatt Macy {
583e716630dSMartin Matuska 	(void) txg;
5847877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
5857877fdebSMatt Macy 	uint64_t ashift = vd->vdev_ashift;
5867877fdebSMatt Macy 
5877877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
5887877fdebSMatt Macy 
5897877fdebSMatt Macy 	uint64_t rows = ((psize - 1) / (vdc->vdc_ndata << ashift)) + 1;
5907877fdebSMatt Macy 	uint64_t asize = (rows * vdc->vdc_groupwidth) << ashift;
5917877fdebSMatt Macy 
5927877fdebSMatt Macy 	ASSERT3U(asize, !=, 0);
5937877fdebSMatt Macy 	ASSERT3U(asize % (vdc->vdc_groupwidth), ==, 0);
5947877fdebSMatt Macy 
5957877fdebSMatt Macy 	return (asize);
5967877fdebSMatt Macy }
5977877fdebSMatt Macy 
5987877fdebSMatt Macy /*
5997877fdebSMatt Macy  * Deflate the asize to the psize, this includes stripping parity.
6007877fdebSMatt Macy  */
6017877fdebSMatt Macy uint64_t
vdev_draid_asize_to_psize(vdev_t * vd,uint64_t asize,uint64_t txg)602*071ab5a1SMartin Matuska vdev_draid_asize_to_psize(vdev_t *vd, uint64_t asize, uint64_t txg)
6037877fdebSMatt Macy {
604*071ab5a1SMartin Matuska 	(void) txg;
6057877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
6067877fdebSMatt Macy 
6077877fdebSMatt Macy 	ASSERT0(asize % vdc->vdc_groupwidth);
6087877fdebSMatt Macy 
6097877fdebSMatt Macy 	return ((asize / vdc->vdc_groupwidth) * vdc->vdc_ndata);
6107877fdebSMatt Macy }
6117877fdebSMatt Macy 
6127877fdebSMatt Macy /*
6137877fdebSMatt Macy  * Convert a logical offset to the corresponding group number.
6147877fdebSMatt Macy  */
6157877fdebSMatt Macy static uint64_t
vdev_draid_offset_to_group(vdev_t * vd,uint64_t offset)6167877fdebSMatt Macy vdev_draid_offset_to_group(vdev_t *vd, uint64_t offset)
6177877fdebSMatt Macy {
6187877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
6197877fdebSMatt Macy 
6207877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
6217877fdebSMatt Macy 
6227877fdebSMatt Macy 	return (offset / vdc->vdc_groupsz);
6237877fdebSMatt Macy }
6247877fdebSMatt Macy 
6257877fdebSMatt Macy /*
6267877fdebSMatt Macy  * Convert a group number to the logical starting offset for that group.
6277877fdebSMatt Macy  */
6287877fdebSMatt Macy static uint64_t
vdev_draid_group_to_offset(vdev_t * vd,uint64_t group)6297877fdebSMatt Macy vdev_draid_group_to_offset(vdev_t *vd, uint64_t group)
6307877fdebSMatt Macy {
6317877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
6327877fdebSMatt Macy 
6337877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
6347877fdebSMatt Macy 
6357877fdebSMatt Macy 	return (group * vdc->vdc_groupsz);
6367877fdebSMatt Macy }
6377877fdebSMatt Macy 
6387877fdebSMatt Macy /*
6397877fdebSMatt Macy  * Full stripe writes.  When writing, all columns (D+P) are required.  Parity
6407877fdebSMatt Macy  * is calculated over all the columns, including empty zero filled sectors,
6417877fdebSMatt Macy  * and each is written to disk.  While only the data columns are needed for
6427877fdebSMatt Macy  * a normal read, all of the columns are required for reconstruction when
6437877fdebSMatt Macy  * performing a sequential resilver.
6447877fdebSMatt Macy  *
6457877fdebSMatt Macy  * For "big columns" it's sufficient to map the correct range of the zio ABD.
6467877fdebSMatt Macy  * Partial columns require allocating a gang ABD in order to zero fill the
6477877fdebSMatt Macy  * empty sectors.  When the column is empty a zero filled sector must be
6487877fdebSMatt Macy  * mapped.  In all cases the data ABDs must be the same size as the parity
6497877fdebSMatt Macy  * ABDs (e.g. rc->rc_size == parity_size).
6507877fdebSMatt Macy  */
6517877fdebSMatt Macy static void
vdev_draid_map_alloc_write(zio_t * zio,uint64_t abd_offset,raidz_row_t * rr)6527877fdebSMatt Macy vdev_draid_map_alloc_write(zio_t *zio, uint64_t abd_offset, raidz_row_t *rr)
6537877fdebSMatt Macy {
6547877fdebSMatt Macy 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
6557877fdebSMatt Macy 	uint64_t parity_size = rr->rr_col[0].rc_size;
6567877fdebSMatt Macy 	uint64_t abd_off = abd_offset;
6577877fdebSMatt Macy 
6587877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
6597877fdebSMatt Macy 	ASSERT3U(parity_size, ==, abd_get_size(rr->rr_col[0].rc_abd));
6607877fdebSMatt Macy 
6617877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
6627877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
6637877fdebSMatt Macy 
6647877fdebSMatt Macy 		if (rc->rc_size == 0) {
6657877fdebSMatt Macy 			/* empty data column (small write), add a skip sector */
6667877fdebSMatt Macy 			ASSERT3U(skip_size, ==, parity_size);
6677877fdebSMatt Macy 			rc->rc_abd = abd_get_zeros(skip_size);
6687877fdebSMatt Macy 		} else if (rc->rc_size == parity_size) {
6697877fdebSMatt Macy 			/* this is a "big column" */
670184c1b94SMartin Matuska 			rc->rc_abd = abd_get_offset_struct(&rc->rc_abdstruct,
671184c1b94SMartin Matuska 			    zio->io_abd, abd_off, rc->rc_size);
6727877fdebSMatt Macy 		} else {
6737877fdebSMatt Macy 			/* short data column, add a skip sector */
6747877fdebSMatt Macy 			ASSERT3U(rc->rc_size + skip_size, ==, parity_size);
675184c1b94SMartin Matuska 			rc->rc_abd = abd_alloc_gang();
6767877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
6777877fdebSMatt Macy 			    zio->io_abd, abd_off, rc->rc_size), B_TRUE);
6787877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_zeros(skip_size),
6797877fdebSMatt Macy 			    B_TRUE);
6807877fdebSMatt Macy 		}
6817877fdebSMatt Macy 
6827877fdebSMatt Macy 		ASSERT3U(abd_get_size(rc->rc_abd), ==, parity_size);
6837877fdebSMatt Macy 
6847877fdebSMatt Macy 		abd_off += rc->rc_size;
6857877fdebSMatt Macy 		rc->rc_size = parity_size;
6867877fdebSMatt Macy 	}
6877877fdebSMatt Macy 
6887877fdebSMatt Macy 	IMPLY(abd_offset != 0, abd_off == zio->io_size);
6897877fdebSMatt Macy }
6907877fdebSMatt Macy 
6917877fdebSMatt Macy /*
6927877fdebSMatt Macy  * Scrub/resilver reads.  In order to store the contents of the skip sectors
6937877fdebSMatt Macy  * an additional ABD is allocated.  The columns are handled in the same way
6947877fdebSMatt Macy  * as a full stripe write except instead of using the zero ABD the newly
6957877fdebSMatt Macy  * allocated skip ABD is used to back the skip sectors.  In all cases the
6967877fdebSMatt Macy  * data ABD must be the same size as the parity ABDs.
6977877fdebSMatt Macy  */
6987877fdebSMatt Macy static void
vdev_draid_map_alloc_scrub(zio_t * zio,uint64_t abd_offset,raidz_row_t * rr)6997877fdebSMatt Macy vdev_draid_map_alloc_scrub(zio_t *zio, uint64_t abd_offset, raidz_row_t *rr)
7007877fdebSMatt Macy {
7017877fdebSMatt Macy 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
7027877fdebSMatt Macy 	uint64_t parity_size = rr->rr_col[0].rc_size;
7037877fdebSMatt Macy 	uint64_t abd_off = abd_offset;
7047877fdebSMatt Macy 	uint64_t skip_off = 0;
7057877fdebSMatt Macy 
7067877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
7077877fdebSMatt Macy 	ASSERT3P(rr->rr_abd_empty, ==, NULL);
7087877fdebSMatt Macy 
7097877fdebSMatt Macy 	if (rr->rr_nempty > 0) {
7107877fdebSMatt Macy 		rr->rr_abd_empty = abd_alloc_linear(rr->rr_nempty * skip_size,
7117877fdebSMatt Macy 		    B_FALSE);
7127877fdebSMatt Macy 	}
7137877fdebSMatt Macy 
7147877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
7157877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
7167877fdebSMatt Macy 
7177877fdebSMatt Macy 		if (rc->rc_size == 0) {
7187877fdebSMatt Macy 			/* empty data column (small read), add a skip sector */
7197877fdebSMatt Macy 			ASSERT3U(skip_size, ==, parity_size);
7207877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
7217877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(rr->rr_abd_empty,
7227877fdebSMatt Macy 			    skip_off, skip_size);
7237877fdebSMatt Macy 			skip_off += skip_size;
7247877fdebSMatt Macy 		} else if (rc->rc_size == parity_size) {
7257877fdebSMatt Macy 			/* this is a "big column" */
726184c1b94SMartin Matuska 			rc->rc_abd = abd_get_offset_struct(&rc->rc_abdstruct,
727184c1b94SMartin Matuska 			    zio->io_abd, abd_off, rc->rc_size);
7287877fdebSMatt Macy 		} else {
7297877fdebSMatt Macy 			/* short data column, add a skip sector */
7307877fdebSMatt Macy 			ASSERT3U(rc->rc_size + skip_size, ==, parity_size);
7317877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
732184c1b94SMartin Matuska 			rc->rc_abd = abd_alloc_gang();
7337877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
7347877fdebSMatt Macy 			    zio->io_abd, abd_off, rc->rc_size), B_TRUE);
7357877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
7367877fdebSMatt Macy 			    rr->rr_abd_empty, skip_off, skip_size), B_TRUE);
7377877fdebSMatt Macy 			skip_off += skip_size;
7387877fdebSMatt Macy 		}
7397877fdebSMatt Macy 
7407877fdebSMatt Macy 		uint64_t abd_size = abd_get_size(rc->rc_abd);
7417877fdebSMatt Macy 		ASSERT3U(abd_size, ==, abd_get_size(rr->rr_col[0].rc_abd));
7427877fdebSMatt Macy 
7437877fdebSMatt Macy 		/*
7447877fdebSMatt Macy 		 * Increase rc_size so the skip ABD is included in subsequent
7457877fdebSMatt Macy 		 * parity calculations.
7467877fdebSMatt Macy 		 */
7477877fdebSMatt Macy 		abd_off += rc->rc_size;
7487877fdebSMatt Macy 		rc->rc_size = abd_size;
7497877fdebSMatt Macy 	}
7507877fdebSMatt Macy 
7517877fdebSMatt Macy 	IMPLY(abd_offset != 0, abd_off == zio->io_size);
7527877fdebSMatt Macy 	ASSERT3U(skip_off, ==, rr->rr_nempty * skip_size);
7537877fdebSMatt Macy }
7547877fdebSMatt Macy 
7557877fdebSMatt Macy /*
7567877fdebSMatt Macy  * Normal reads.  In this common case only the columns containing data
7577877fdebSMatt Macy  * are read in to the zio ABDs.  Neither the parity columns or empty skip
7587877fdebSMatt Macy  * sectors are read unless the checksum fails verification.  In which case
7597877fdebSMatt Macy  * vdev_raidz_read_all() will call vdev_draid_map_alloc_empty() to expand
7607877fdebSMatt Macy  * the raid map in order to allow reconstruction using the parity data and
7617877fdebSMatt Macy  * skip sectors.
7627877fdebSMatt Macy  */
7637877fdebSMatt Macy static void
vdev_draid_map_alloc_read(zio_t * zio,uint64_t abd_offset,raidz_row_t * rr)7647877fdebSMatt Macy vdev_draid_map_alloc_read(zio_t *zio, uint64_t abd_offset, raidz_row_t *rr)
7657877fdebSMatt Macy {
7667877fdebSMatt Macy 	uint64_t abd_off = abd_offset;
7677877fdebSMatt Macy 
7687877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
7697877fdebSMatt Macy 
7707877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
7717877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
7727877fdebSMatt Macy 
7737877fdebSMatt Macy 		if (rc->rc_size > 0) {
774184c1b94SMartin Matuska 			rc->rc_abd = abd_get_offset_struct(&rc->rc_abdstruct,
775184c1b94SMartin Matuska 			    zio->io_abd, abd_off, rc->rc_size);
7767877fdebSMatt Macy 			abd_off += rc->rc_size;
7777877fdebSMatt Macy 		}
7787877fdebSMatt Macy 	}
7797877fdebSMatt Macy 
7807877fdebSMatt Macy 	IMPLY(abd_offset != 0, abd_off == zio->io_size);
7817877fdebSMatt Macy }
7827877fdebSMatt Macy 
7837877fdebSMatt Macy /*
7847877fdebSMatt Macy  * Converts a normal "read" raidz_row_t to a "scrub" raidz_row_t. The key
7857877fdebSMatt Macy  * difference is that an ABD is allocated to back skip sectors so they may
7867877fdebSMatt Macy  * be read in to memory, verified, and repaired if needed.
7877877fdebSMatt Macy  */
7887877fdebSMatt Macy void
vdev_draid_map_alloc_empty(zio_t * zio,raidz_row_t * rr)7897877fdebSMatt Macy vdev_draid_map_alloc_empty(zio_t *zio, raidz_row_t *rr)
7907877fdebSMatt Macy {
7917877fdebSMatt Macy 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
7927877fdebSMatt Macy 	uint64_t parity_size = rr->rr_col[0].rc_size;
7937877fdebSMatt Macy 	uint64_t skip_off = 0;
7947877fdebSMatt Macy 
7957877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
7967877fdebSMatt Macy 	ASSERT3P(rr->rr_abd_empty, ==, NULL);
7977877fdebSMatt Macy 
7987877fdebSMatt Macy 	if (rr->rr_nempty > 0) {
7997877fdebSMatt Macy 		rr->rr_abd_empty = abd_alloc_linear(rr->rr_nempty * skip_size,
8007877fdebSMatt Macy 		    B_FALSE);
8017877fdebSMatt Macy 	}
8027877fdebSMatt Macy 
8037877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
8047877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
8057877fdebSMatt Macy 
8067877fdebSMatt Macy 		if (rc->rc_size == 0) {
8077877fdebSMatt Macy 			/* empty data column (small read), add a skip sector */
8087877fdebSMatt Macy 			ASSERT3U(skip_size, ==, parity_size);
8097877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
8107877fdebSMatt Macy 			ASSERT3P(rc->rc_abd, ==, NULL);
8117877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(rr->rr_abd_empty,
8127877fdebSMatt Macy 			    skip_off, skip_size);
8137877fdebSMatt Macy 			skip_off += skip_size;
8147877fdebSMatt Macy 		} else if (rc->rc_size == parity_size) {
8157877fdebSMatt Macy 			/* this is a "big column", nothing to add */
8167877fdebSMatt Macy 			ASSERT3P(rc->rc_abd, !=, NULL);
8177877fdebSMatt Macy 		} else {
81816038816SMartin Matuska 			/*
81916038816SMartin Matuska 			 * short data column, add a skip sector and clear
82016038816SMartin Matuska 			 * rc_tried to force the entire column to be re-read
82116038816SMartin Matuska 			 * thereby including the missing skip sector data
82216038816SMartin Matuska 			 * which is needed for reconstruction.
82316038816SMartin Matuska 			 */
8247877fdebSMatt Macy 			ASSERT3U(rc->rc_size + skip_size, ==, parity_size);
8257877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
8267877fdebSMatt Macy 			ASSERT3P(rc->rc_abd, !=, NULL);
8277877fdebSMatt Macy 			ASSERT(!abd_is_gang(rc->rc_abd));
8287877fdebSMatt Macy 			abd_t *read_abd = rc->rc_abd;
829184c1b94SMartin Matuska 			rc->rc_abd = abd_alloc_gang();
8307877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, read_abd, B_TRUE);
8317877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
8327877fdebSMatt Macy 			    rr->rr_abd_empty, skip_off, skip_size), B_TRUE);
8337877fdebSMatt Macy 			skip_off += skip_size;
83416038816SMartin Matuska 			rc->rc_tried = 0;
8357877fdebSMatt Macy 		}
8367877fdebSMatt Macy 
8377877fdebSMatt Macy 		/*
8387877fdebSMatt Macy 		 * Increase rc_size so the empty ABD is included in subsequent
8397877fdebSMatt Macy 		 * parity calculations.
8407877fdebSMatt Macy 		 */
8417877fdebSMatt Macy 		rc->rc_size = parity_size;
8427877fdebSMatt Macy 	}
8437877fdebSMatt Macy 
8447877fdebSMatt Macy 	ASSERT3U(skip_off, ==, rr->rr_nempty * skip_size);
8457877fdebSMatt Macy }
8467877fdebSMatt Macy 
8477877fdebSMatt Macy /*
848e92ffd9bSMartin Matuska  * Verify that all empty sectors are zero filled before using them to
849e92ffd9bSMartin Matuska  * calculate parity.  Otherwise, silent corruption in an empty sector will
850e92ffd9bSMartin Matuska  * result in bad parity being generated.  That bad parity will then be
851e92ffd9bSMartin Matuska  * considered authoritative and overwrite the good parity on disk.  This
852e92ffd9bSMartin Matuska  * is possible because the checksum is only calculated over the data,
853e92ffd9bSMartin Matuska  * thus it cannot be used to detect damage in empty sectors.
854e92ffd9bSMartin Matuska  */
855e92ffd9bSMartin Matuska int
vdev_draid_map_verify_empty(zio_t * zio,raidz_row_t * rr)856e92ffd9bSMartin Matuska vdev_draid_map_verify_empty(zio_t *zio, raidz_row_t *rr)
857e92ffd9bSMartin Matuska {
858e92ffd9bSMartin Matuska 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
859e92ffd9bSMartin Matuska 	uint64_t parity_size = rr->rr_col[0].rc_size;
860e92ffd9bSMartin Matuska 	uint64_t skip_off = parity_size - skip_size;
861e92ffd9bSMartin Matuska 	uint64_t empty_off = 0;
862e92ffd9bSMartin Matuska 	int ret = 0;
863e92ffd9bSMartin Matuska 
864e92ffd9bSMartin Matuska 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
865e92ffd9bSMartin Matuska 	ASSERT3P(rr->rr_abd_empty, !=, NULL);
866e92ffd9bSMartin Matuska 	ASSERT3U(rr->rr_bigcols, >, 0);
867e92ffd9bSMartin Matuska 
868e92ffd9bSMartin Matuska 	void *zero_buf = kmem_zalloc(skip_size, KM_SLEEP);
869e92ffd9bSMartin Matuska 
870e92ffd9bSMartin Matuska 	for (int c = rr->rr_bigcols; c < rr->rr_cols; c++) {
871e92ffd9bSMartin Matuska 		raidz_col_t *rc = &rr->rr_col[c];
872e92ffd9bSMartin Matuska 
873e92ffd9bSMartin Matuska 		ASSERT3P(rc->rc_abd, !=, NULL);
874e92ffd9bSMartin Matuska 		ASSERT3U(rc->rc_size, ==, parity_size);
875e92ffd9bSMartin Matuska 
876e92ffd9bSMartin Matuska 		if (abd_cmp_buf_off(rc->rc_abd, zero_buf, skip_off,
877e92ffd9bSMartin Matuska 		    skip_size) != 0) {
878e92ffd9bSMartin Matuska 			vdev_raidz_checksum_error(zio, rc, rc->rc_abd);
879e92ffd9bSMartin Matuska 			abd_zero_off(rc->rc_abd, skip_off, skip_size);
880e92ffd9bSMartin Matuska 			rc->rc_error = SET_ERROR(ECKSUM);
881e92ffd9bSMartin Matuska 			ret++;
882e92ffd9bSMartin Matuska 		}
883e92ffd9bSMartin Matuska 
884e92ffd9bSMartin Matuska 		empty_off += skip_size;
885e92ffd9bSMartin Matuska 	}
886e92ffd9bSMartin Matuska 
887e92ffd9bSMartin Matuska 	ASSERT3U(empty_off, ==, abd_get_size(rr->rr_abd_empty));
888e92ffd9bSMartin Matuska 
889e92ffd9bSMartin Matuska 	kmem_free(zero_buf, skip_size);
890e92ffd9bSMartin Matuska 
891e92ffd9bSMartin Matuska 	return (ret);
892e92ffd9bSMartin Matuska }
893e92ffd9bSMartin Matuska 
894e92ffd9bSMartin Matuska /*
8957877fdebSMatt Macy  * Given a logical address within a dRAID configuration, return the physical
8967877fdebSMatt Macy  * address on the first drive in the group that this address maps to
8977877fdebSMatt Macy  * (at position 'start' in permutation number 'perm').
8987877fdebSMatt Macy  */
8997877fdebSMatt Macy static uint64_t
vdev_draid_logical_to_physical(vdev_t * vd,uint64_t logical_offset,uint64_t * perm,uint64_t * start)9007877fdebSMatt Macy vdev_draid_logical_to_physical(vdev_t *vd, uint64_t logical_offset,
9017877fdebSMatt Macy     uint64_t *perm, uint64_t *start)
9027877fdebSMatt Macy {
9037877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
9047877fdebSMatt Macy 
9057877fdebSMatt Macy 	/* b is the dRAID (parent) sector offset. */
9067877fdebSMatt Macy 	uint64_t ashift = vd->vdev_top->vdev_ashift;
9077877fdebSMatt Macy 	uint64_t b_offset = logical_offset >> ashift;
9087877fdebSMatt Macy 
9097877fdebSMatt Macy 	/*
9107877fdebSMatt Macy 	 * The height of a row in units of the vdev's minimum sector size.
9117877fdebSMatt Macy 	 * This is the amount of data written to each disk of each group
9127877fdebSMatt Macy 	 * in a given permutation.
9137877fdebSMatt Macy 	 */
9147877fdebSMatt Macy 	uint64_t rowheight_sectors = VDEV_DRAID_ROWHEIGHT >> ashift;
9157877fdebSMatt Macy 
9167877fdebSMatt Macy 	/*
9177877fdebSMatt Macy 	 * We cycle through a disk permutation every groupsz * ngroups chunk
9187877fdebSMatt Macy 	 * of address space. Note that ngroups * groupsz must be a multiple
9197877fdebSMatt Macy 	 * of the number of data drives (ndisks) in order to guarantee
9207877fdebSMatt Macy 	 * alignment. So, for example, if our row height is 16MB, our group
9217877fdebSMatt Macy 	 * size is 10, and there are 13 data drives in the draid, then ngroups
9227877fdebSMatt Macy 	 * will be 13, we will change permutation every 2.08GB and each
9237877fdebSMatt Macy 	 * disk will have 160MB of data per chunk.
9247877fdebSMatt Macy 	 */
9257877fdebSMatt Macy 	uint64_t groupwidth = vdc->vdc_groupwidth;
9267877fdebSMatt Macy 	uint64_t ngroups = vdc->vdc_ngroups;
9277877fdebSMatt Macy 	uint64_t ndisks = vdc->vdc_ndisks;
9287877fdebSMatt Macy 
9297877fdebSMatt Macy 	/*
9307877fdebSMatt Macy 	 * groupstart is where the group this IO will land in "starts" in
9317877fdebSMatt Macy 	 * the permutation array.
9327877fdebSMatt Macy 	 */
9337877fdebSMatt Macy 	uint64_t group = logical_offset / vdc->vdc_groupsz;
9347877fdebSMatt Macy 	uint64_t groupstart = (group * groupwidth) % ndisks;
9357877fdebSMatt Macy 	ASSERT3U(groupstart + groupwidth, <=, ndisks + groupstart);
9367877fdebSMatt Macy 	*start = groupstart;
9377877fdebSMatt Macy 
9387877fdebSMatt Macy 	/* b_offset is the sector offset within a group chunk */
9397877fdebSMatt Macy 	b_offset = b_offset % (rowheight_sectors * groupwidth);
9407877fdebSMatt Macy 	ASSERT0(b_offset % groupwidth);
9417877fdebSMatt Macy 
9427877fdebSMatt Macy 	/*
9437877fdebSMatt Macy 	 * Find the starting byte offset on each child vdev:
9447877fdebSMatt Macy 	 * - within a permutation there are ngroups groups spread over the
9457877fdebSMatt Macy 	 *   rows, where each row covers a slice portion of the disk
9467877fdebSMatt Macy 	 * - each permutation has (groupwidth * ngroups) / ndisks rows
9477877fdebSMatt Macy 	 * - so each permutation covers rows * slice portion of the disk
9487877fdebSMatt Macy 	 * - so we need to find the row where this IO group target begins
9497877fdebSMatt Macy 	 */
9507877fdebSMatt Macy 	*perm = group / ngroups;
9517877fdebSMatt Macy 	uint64_t row = (*perm * ((groupwidth * ngroups) / ndisks)) +
9527877fdebSMatt Macy 	    (((group % ngroups) * groupwidth) / ndisks);
9537877fdebSMatt Macy 
9547877fdebSMatt Macy 	return (((rowheight_sectors * row) +
9557877fdebSMatt Macy 	    (b_offset / groupwidth)) << ashift);
9567877fdebSMatt Macy }
9577877fdebSMatt Macy 
9587877fdebSMatt Macy static uint64_t
vdev_draid_map_alloc_row(zio_t * zio,raidz_row_t ** rrp,uint64_t io_offset,uint64_t abd_offset,uint64_t abd_size)9597877fdebSMatt Macy vdev_draid_map_alloc_row(zio_t *zio, raidz_row_t **rrp, uint64_t io_offset,
9607877fdebSMatt Macy     uint64_t abd_offset, uint64_t abd_size)
9617877fdebSMatt Macy {
9627877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
9637877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
9647877fdebSMatt Macy 	uint64_t ashift = vd->vdev_top->vdev_ashift;
9657877fdebSMatt Macy 	uint64_t io_size = abd_size;
966*071ab5a1SMartin Matuska 	uint64_t io_asize = vdev_draid_psize_to_asize(vd, io_size, 0);
9677877fdebSMatt Macy 	uint64_t group = vdev_draid_offset_to_group(vd, io_offset);
9687877fdebSMatt Macy 	uint64_t start_offset = vdev_draid_group_to_offset(vd, group + 1);
9697877fdebSMatt Macy 
9707877fdebSMatt Macy 	/*
9717877fdebSMatt Macy 	 * Limit the io_size to the space remaining in the group.  A second
9727877fdebSMatt Macy 	 * row in the raidz_map_t is created for the remainder.
9737877fdebSMatt Macy 	 */
9747877fdebSMatt Macy 	if (io_offset + io_asize > start_offset) {
9757877fdebSMatt Macy 		io_size = vdev_draid_asize_to_psize(vd,
976*071ab5a1SMartin Matuska 		    start_offset - io_offset, 0);
9777877fdebSMatt Macy 	}
9787877fdebSMatt Macy 
9797877fdebSMatt Macy 	/*
9807877fdebSMatt Macy 	 * At most a block may span the logical end of one group and the start
9817877fdebSMatt Macy 	 * of the next group. Therefore, at the end of a group the io_size must
9827877fdebSMatt Macy 	 * span the group width evenly and the remainder must be aligned to the
9837877fdebSMatt Macy 	 * start of the next group.
9847877fdebSMatt Macy 	 */
9857877fdebSMatt Macy 	IMPLY(abd_offset == 0 && io_size < zio->io_size,
9867877fdebSMatt Macy 	    (io_asize >> ashift) % vdc->vdc_groupwidth == 0);
9877877fdebSMatt Macy 	IMPLY(abd_offset != 0,
9887877fdebSMatt Macy 	    vdev_draid_group_to_offset(vd, group) == io_offset);
9897877fdebSMatt Macy 
9907877fdebSMatt Macy 	/* Lookup starting byte offset on each child vdev */
9917877fdebSMatt Macy 	uint64_t groupstart, perm;
9927877fdebSMatt Macy 	uint64_t physical_offset = vdev_draid_logical_to_physical(vd,
9937877fdebSMatt Macy 	    io_offset, &perm, &groupstart);
9947877fdebSMatt Macy 
9957877fdebSMatt Macy 	/*
9967877fdebSMatt Macy 	 * If there is less than groupwidth drives available after the group
9977877fdebSMatt Macy 	 * start, the group is going to wrap onto the next row. 'wrap' is the
9987877fdebSMatt Macy 	 * group disk number that starts on the next row.
9997877fdebSMatt Macy 	 */
10007877fdebSMatt Macy 	uint64_t ndisks = vdc->vdc_ndisks;
10017877fdebSMatt Macy 	uint64_t groupwidth = vdc->vdc_groupwidth;
10027877fdebSMatt Macy 	uint64_t wrap = groupwidth;
10037877fdebSMatt Macy 
10047877fdebSMatt Macy 	if (groupstart + groupwidth > ndisks)
10057877fdebSMatt Macy 		wrap = ndisks - groupstart;
10067877fdebSMatt Macy 
10077877fdebSMatt Macy 	/* The io size in units of the vdev's minimum sector size. */
10087877fdebSMatt Macy 	const uint64_t psize = io_size >> ashift;
10097877fdebSMatt Macy 
10107877fdebSMatt Macy 	/*
10117877fdebSMatt Macy 	 * "Quotient": The number of data sectors for this stripe on all but
10127877fdebSMatt Macy 	 * the "big column" child vdevs that also contain "remainder" data.
10137877fdebSMatt Macy 	 */
10147877fdebSMatt Macy 	uint64_t q = psize / vdc->vdc_ndata;
10157877fdebSMatt Macy 
10167877fdebSMatt Macy 	/*
10177877fdebSMatt Macy 	 * "Remainder": The number of partial stripe data sectors in this I/O.
10187877fdebSMatt Macy 	 * This will add a sector to some, but not all, child vdevs.
10197877fdebSMatt Macy 	 */
10207877fdebSMatt Macy 	uint64_t r = psize - q * vdc->vdc_ndata;
10217877fdebSMatt Macy 
10227877fdebSMatt Macy 	/* The number of "big columns" - those which contain remainder data. */
10237877fdebSMatt Macy 	uint64_t bc = (r == 0 ? 0 : r + vdc->vdc_nparity);
10247877fdebSMatt Macy 	ASSERT3U(bc, <, groupwidth);
10257877fdebSMatt Macy 
10267877fdebSMatt Macy 	/* The total number of data and parity sectors for this I/O. */
10277877fdebSMatt Macy 	uint64_t tot = psize + (vdc->vdc_nparity * (q + (r == 0 ? 0 : 1)));
10287877fdebSMatt Macy 
10292a58b312SMartin Matuska 	ASSERT3U(vdc->vdc_nparity, >, 0);
10302a58b312SMartin Matuska 
103187bf66d4SMartin Matuska 	raidz_row_t *rr = vdev_raidz_row_alloc(groupwidth, zio);
10327877fdebSMatt Macy 	rr->rr_bigcols = bc;
10337877fdebSMatt Macy 	rr->rr_firstdatacol = vdc->vdc_nparity;
10347877fdebSMatt Macy #ifdef ZFS_DEBUG
10357877fdebSMatt Macy 	rr->rr_offset = io_offset;
10367877fdebSMatt Macy 	rr->rr_size = io_size;
10377877fdebSMatt Macy #endif
10387877fdebSMatt Macy 	*rrp = rr;
10397877fdebSMatt Macy 
10407877fdebSMatt Macy 	uint8_t *base;
10417877fdebSMatt Macy 	uint64_t iter, asize = 0;
10427877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
10437877fdebSMatt Macy 	for (uint64_t i = 0; i < groupwidth; i++) {
10447877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[i];
10457877fdebSMatt Macy 		uint64_t c = (groupstart + i) % ndisks;
10467877fdebSMatt Macy 
10477877fdebSMatt Macy 		/* increment the offset if we wrap to the next row */
10487877fdebSMatt Macy 		if (i == wrap)
10497877fdebSMatt Macy 			physical_offset += VDEV_DRAID_ROWHEIGHT;
10507877fdebSMatt Macy 
10517877fdebSMatt Macy 		rc->rc_devidx = vdev_draid_permute_id(vdc, base, iter, c);
10527877fdebSMatt Macy 		rc->rc_offset = physical_offset;
10537877fdebSMatt Macy 
10547877fdebSMatt Macy 		if (q == 0 && i >= bc)
10557877fdebSMatt Macy 			rc->rc_size = 0;
10567877fdebSMatt Macy 		else if (i < bc)
10577877fdebSMatt Macy 			rc->rc_size = (q + 1) << ashift;
10587877fdebSMatt Macy 		else
10597877fdebSMatt Macy 			rc->rc_size = q << ashift;
10607877fdebSMatt Macy 
10617877fdebSMatt Macy 		asize += rc->rc_size;
10627877fdebSMatt Macy 	}
10637877fdebSMatt Macy 
10647877fdebSMatt Macy 	ASSERT3U(asize, ==, tot << ashift);
10657877fdebSMatt Macy 	rr->rr_nempty = roundup(tot, groupwidth) - tot;
10667877fdebSMatt Macy 	IMPLY(bc > 0, rr->rr_nempty == groupwidth - bc);
10677877fdebSMatt Macy 
10687877fdebSMatt Macy 	/* Allocate buffers for the parity columns */
10697877fdebSMatt Macy 	for (uint64_t c = 0; c < rr->rr_firstdatacol; c++) {
10707877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
10717877fdebSMatt Macy 		rc->rc_abd = abd_alloc_linear(rc->rc_size, B_FALSE);
10727877fdebSMatt Macy 	}
10737877fdebSMatt Macy 
10747877fdebSMatt Macy 	/*
10757877fdebSMatt Macy 	 * Map buffers for data columns and allocate/map buffers for skip
10767877fdebSMatt Macy 	 * sectors.  There are three distinct cases for dRAID which are
10777877fdebSMatt Macy 	 * required to support sequential rebuild.
10787877fdebSMatt Macy 	 */
10797877fdebSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
10807877fdebSMatt Macy 		vdev_draid_map_alloc_write(zio, abd_offset, rr);
10817877fdebSMatt Macy 	} else if ((rr->rr_nempty > 0) &&
10827877fdebSMatt Macy 	    (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
10837877fdebSMatt Macy 		vdev_draid_map_alloc_scrub(zio, abd_offset, rr);
10847877fdebSMatt Macy 	} else {
10857877fdebSMatt Macy 		ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
10867877fdebSMatt Macy 		vdev_draid_map_alloc_read(zio, abd_offset, rr);
10877877fdebSMatt Macy 	}
10887877fdebSMatt Macy 
10897877fdebSMatt Macy 	return (io_size);
10907877fdebSMatt Macy }
10917877fdebSMatt Macy 
10927877fdebSMatt Macy /*
10937877fdebSMatt Macy  * Allocate the raidz mapping to be applied to the dRAID I/O.  The parity
10947877fdebSMatt Macy  * calculations for dRAID are identical to raidz however there are a few
10957877fdebSMatt Macy  * differences in the layout.
10967877fdebSMatt Macy  *
10977877fdebSMatt Macy  * - dRAID always allocates a full stripe width. Any extra sectors due
10987877fdebSMatt Macy  *   this padding are zero filled and written to disk. They will be read
10997877fdebSMatt Macy  *   back during a scrub or repair operation since they are included in
11007877fdebSMatt Macy  *   the parity calculation. This property enables sequential resilvering.
11017877fdebSMatt Macy  *
11027877fdebSMatt Macy  * - When the block at the logical offset spans redundancy groups then two
11037877fdebSMatt Macy  *   rows are allocated in the raidz_map_t. One row resides at the end of
11047877fdebSMatt Macy  *   the first group and the other at the start of the following group.
11057877fdebSMatt Macy  */
11067877fdebSMatt Macy static raidz_map_t *
vdev_draid_map_alloc(zio_t * zio)11077877fdebSMatt Macy vdev_draid_map_alloc(zio_t *zio)
11087877fdebSMatt Macy {
11097877fdebSMatt Macy 	raidz_row_t *rr[2];
11107877fdebSMatt Macy 	uint64_t abd_offset = 0;
11117877fdebSMatt Macy 	uint64_t abd_size = zio->io_size;
11127877fdebSMatt Macy 	uint64_t io_offset = zio->io_offset;
11137877fdebSMatt Macy 	uint64_t size;
11147877fdebSMatt Macy 	int nrows = 1;
11157877fdebSMatt Macy 
11167877fdebSMatt Macy 	size = vdev_draid_map_alloc_row(zio, &rr[0], io_offset,
11177877fdebSMatt Macy 	    abd_offset, abd_size);
11187877fdebSMatt Macy 	if (size < abd_size) {
11197877fdebSMatt Macy 		vdev_t *vd = zio->io_vd;
11207877fdebSMatt Macy 
1121*071ab5a1SMartin Matuska 		io_offset += vdev_draid_psize_to_asize(vd, size, 0);
11227877fdebSMatt Macy 		abd_offset += size;
11237877fdebSMatt Macy 		abd_size -= size;
11247877fdebSMatt Macy 		nrows++;
11257877fdebSMatt Macy 
11267877fdebSMatt Macy 		ASSERT3U(io_offset, ==, vdev_draid_group_to_offset(
11277877fdebSMatt Macy 		    vd, vdev_draid_offset_to_group(vd, io_offset)));
11287877fdebSMatt Macy 		ASSERT3U(abd_offset, <, zio->io_size);
11297877fdebSMatt Macy 		ASSERT3U(abd_size, !=, 0);
11307877fdebSMatt Macy 
11317877fdebSMatt Macy 		size = vdev_draid_map_alloc_row(zio, &rr[1],
11327877fdebSMatt Macy 		    io_offset, abd_offset, abd_size);
11337877fdebSMatt Macy 		VERIFY3U(size, ==, abd_size);
11347877fdebSMatt Macy 	}
11357877fdebSMatt Macy 
11367877fdebSMatt Macy 	raidz_map_t *rm;
11377877fdebSMatt Macy 	rm = kmem_zalloc(offsetof(raidz_map_t, rm_row[nrows]), KM_SLEEP);
11387877fdebSMatt Macy 	rm->rm_ops = vdev_raidz_math_get_ops();
11397877fdebSMatt Macy 	rm->rm_nrows = nrows;
11407877fdebSMatt Macy 	rm->rm_row[0] = rr[0];
11417877fdebSMatt Macy 	if (nrows == 2)
11427877fdebSMatt Macy 		rm->rm_row[1] = rr[1];
11437877fdebSMatt Macy 	return (rm);
11447877fdebSMatt Macy }
11457877fdebSMatt Macy 
11467877fdebSMatt Macy /*
11477877fdebSMatt Macy  * Given an offset into a dRAID return the next group width aligned offset
11487877fdebSMatt Macy  * which can be used to start an allocation.
11497877fdebSMatt Macy  */
11507877fdebSMatt Macy static uint64_t
vdev_draid_get_astart(vdev_t * vd,const uint64_t start)11517877fdebSMatt Macy vdev_draid_get_astart(vdev_t *vd, const uint64_t start)
11527877fdebSMatt Macy {
11537877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
11547877fdebSMatt Macy 
11557877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
11567877fdebSMatt Macy 
11577877fdebSMatt Macy 	return (roundup(start, vdc->vdc_groupwidth << vd->vdev_ashift));
11587877fdebSMatt Macy }
11597877fdebSMatt Macy 
11607877fdebSMatt Macy /*
11617877fdebSMatt Macy  * Allocatable space for dRAID is (children - nspares) * sizeof(smallest child)
11627877fdebSMatt Macy  * rounded down to the last full slice.  So each child must provide at least
11637877fdebSMatt Macy  * 1 / (children - nspares) of its asize.
11647877fdebSMatt Macy  */
11657877fdebSMatt Macy static uint64_t
vdev_draid_min_asize(vdev_t * vd)11667877fdebSMatt Macy vdev_draid_min_asize(vdev_t *vd)
11677877fdebSMatt Macy {
11687877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
11697877fdebSMatt Macy 
11707877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
11717877fdebSMatt Macy 
11720d8fe237SMartin Matuska 	return (VDEV_DRAID_REFLOW_RESERVE +
11730d8fe237SMartin Matuska 	    (vd->vdev_min_asize + vdc->vdc_ndisks - 1) / (vdc->vdc_ndisks));
11747877fdebSMatt Macy }
11757877fdebSMatt Macy 
11767877fdebSMatt Macy /*
11777877fdebSMatt Macy  * When using dRAID the minimum allocation size is determined by the number
11787877fdebSMatt Macy  * of data disks in the redundancy group.  Full stripes are always used.
11797877fdebSMatt Macy  */
11807877fdebSMatt Macy static uint64_t
vdev_draid_min_alloc(vdev_t * vd)11817877fdebSMatt Macy vdev_draid_min_alloc(vdev_t *vd)
11827877fdebSMatt Macy {
11837877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
11847877fdebSMatt Macy 
11857877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
11867877fdebSMatt Macy 
11877877fdebSMatt Macy 	return (vdc->vdc_ndata << vd->vdev_ashift);
11887877fdebSMatt Macy }
11897877fdebSMatt Macy 
11907877fdebSMatt Macy /*
11917877fdebSMatt Macy  * Returns true if the txg range does not exist on any leaf vdev.
11927877fdebSMatt Macy  *
11937877fdebSMatt Macy  * A dRAID spare does not fit into the DTL model. While it has child vdevs
11947877fdebSMatt Macy  * there is no redundancy among them, and the effective child vdev is
11957877fdebSMatt Macy  * determined by offset. Essentially we do a vdev_dtl_reassess() on the
11967877fdebSMatt Macy  * fly by replacing a dRAID spare with the child vdev under the offset.
11977877fdebSMatt Macy  * Note that it is a recursive process because the child vdev can be
11987877fdebSMatt Macy  * another dRAID spare and so on.
11997877fdebSMatt Macy  */
12007877fdebSMatt Macy boolean_t
vdev_draid_missing(vdev_t * vd,uint64_t physical_offset,uint64_t txg,uint64_t size)12017877fdebSMatt Macy vdev_draid_missing(vdev_t *vd, uint64_t physical_offset, uint64_t txg,
12027877fdebSMatt Macy     uint64_t size)
12037877fdebSMatt Macy {
12047877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_spare_ops ||
12057877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops) {
12067877fdebSMatt Macy 		/*
12077877fdebSMatt Macy 		 * Check all of the readable children, if any child
12087877fdebSMatt Macy 		 * contains the txg range the data it is not missing.
12097877fdebSMatt Macy 		 */
12107877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
12117877fdebSMatt Macy 			vdev_t *cvd = vd->vdev_child[c];
12127877fdebSMatt Macy 
12137877fdebSMatt Macy 			if (!vdev_readable(cvd))
12147877fdebSMatt Macy 				continue;
12157877fdebSMatt Macy 
12167877fdebSMatt Macy 			if (!vdev_draid_missing(cvd, physical_offset,
12177877fdebSMatt Macy 			    txg, size))
12187877fdebSMatt Macy 				return (B_FALSE);
12197877fdebSMatt Macy 		}
12207877fdebSMatt Macy 
12217877fdebSMatt Macy 		return (B_TRUE);
12227877fdebSMatt Macy 	}
12237877fdebSMatt Macy 
12247877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
12257877fdebSMatt Macy 		/*
12267877fdebSMatt Macy 		 * When sequentially resilvering we don't have a proper
12277877fdebSMatt Macy 		 * txg range so instead we must presume all txgs are
12287877fdebSMatt Macy 		 * missing on this vdev until the resilver completes.
12297877fdebSMatt Macy 		 */
12307877fdebSMatt Macy 		if (vd->vdev_rebuild_txg != 0)
12317877fdebSMatt Macy 			return (B_TRUE);
12327877fdebSMatt Macy 
12337877fdebSMatt Macy 		/*
12347877fdebSMatt Macy 		 * DTL_MISSING is set for all prior txgs when a resilver
12357877fdebSMatt Macy 		 * is started in spa_vdev_attach().
12367877fdebSMatt Macy 		 */
12377877fdebSMatt Macy 		if (vdev_dtl_contains(vd, DTL_MISSING, txg, size))
12387877fdebSMatt Macy 			return (B_TRUE);
12397877fdebSMatt Macy 
12407877fdebSMatt Macy 		/*
12417877fdebSMatt Macy 		 * Consult the DTL on the relevant vdev. Either a vdev
12427877fdebSMatt Macy 		 * leaf or spare/replace mirror child may be returned so
12437877fdebSMatt Macy 		 * we must recursively call vdev_draid_missing_impl().
12447877fdebSMatt Macy 		 */
12457877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
12467877fdebSMatt Macy 		if (vd == NULL)
12477877fdebSMatt Macy 			return (B_TRUE);
12487877fdebSMatt Macy 
12497877fdebSMatt Macy 		return (vdev_draid_missing(vd, physical_offset,
12507877fdebSMatt Macy 		    txg, size));
12517877fdebSMatt Macy 	}
12527877fdebSMatt Macy 
12537877fdebSMatt Macy 	return (vdev_dtl_contains(vd, DTL_MISSING, txg, size));
12547877fdebSMatt Macy }
12557877fdebSMatt Macy 
12567877fdebSMatt Macy /*
12577877fdebSMatt Macy  * Returns true if the txg is only partially replicated on the leaf vdevs.
12587877fdebSMatt Macy  */
12597877fdebSMatt Macy static boolean_t
vdev_draid_partial(vdev_t * vd,uint64_t physical_offset,uint64_t txg,uint64_t size)12607877fdebSMatt Macy vdev_draid_partial(vdev_t *vd, uint64_t physical_offset, uint64_t txg,
12617877fdebSMatt Macy     uint64_t size)
12627877fdebSMatt Macy {
12637877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_spare_ops ||
12647877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops) {
12657877fdebSMatt Macy 		/*
12667877fdebSMatt Macy 		 * Check all of the readable children, if any child is
12677877fdebSMatt Macy 		 * missing the txg range then it is partially replicated.
12687877fdebSMatt Macy 		 */
12697877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
12707877fdebSMatt Macy 			vdev_t *cvd = vd->vdev_child[c];
12717877fdebSMatt Macy 
12727877fdebSMatt Macy 			if (!vdev_readable(cvd))
12737877fdebSMatt Macy 				continue;
12747877fdebSMatt Macy 
12757877fdebSMatt Macy 			if (vdev_draid_partial(cvd, physical_offset, txg, size))
12767877fdebSMatt Macy 				return (B_TRUE);
12777877fdebSMatt Macy 		}
12787877fdebSMatt Macy 
12797877fdebSMatt Macy 		return (B_FALSE);
12807877fdebSMatt Macy 	}
12817877fdebSMatt Macy 
12827877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
12837877fdebSMatt Macy 		/*
12847877fdebSMatt Macy 		 * When sequentially resilvering we don't have a proper
12857877fdebSMatt Macy 		 * txg range so instead we must presume all txgs are
12867877fdebSMatt Macy 		 * missing on this vdev until the resilver completes.
12877877fdebSMatt Macy 		 */
12887877fdebSMatt Macy 		if (vd->vdev_rebuild_txg != 0)
12897877fdebSMatt Macy 			return (B_TRUE);
12907877fdebSMatt Macy 
12917877fdebSMatt Macy 		/*
12927877fdebSMatt Macy 		 * DTL_MISSING is set for all prior txgs when a resilver
12937877fdebSMatt Macy 		 * is started in spa_vdev_attach().
12947877fdebSMatt Macy 		 */
12957877fdebSMatt Macy 		if (vdev_dtl_contains(vd, DTL_MISSING, txg, size))
12967877fdebSMatt Macy 			return (B_TRUE);
12977877fdebSMatt Macy 
12987877fdebSMatt Macy 		/*
12997877fdebSMatt Macy 		 * Consult the DTL on the relevant vdev. Either a vdev
13007877fdebSMatt Macy 		 * leaf or spare/replace mirror child may be returned so
13017877fdebSMatt Macy 		 * we must recursively call vdev_draid_missing_impl().
13027877fdebSMatt Macy 		 */
13037877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
13047877fdebSMatt Macy 		if (vd == NULL)
13057877fdebSMatt Macy 			return (B_TRUE);
13067877fdebSMatt Macy 
13077877fdebSMatt Macy 		return (vdev_draid_partial(vd, physical_offset, txg, size));
13087877fdebSMatt Macy 	}
13097877fdebSMatt Macy 
13107877fdebSMatt Macy 	return (vdev_dtl_contains(vd, DTL_MISSING, txg, size));
13117877fdebSMatt Macy }
13127877fdebSMatt Macy 
13137877fdebSMatt Macy /*
13147877fdebSMatt Macy  * Determine if the vdev is readable at the given offset.
13157877fdebSMatt Macy  */
13167877fdebSMatt Macy boolean_t
vdev_draid_readable(vdev_t * vd,uint64_t physical_offset)13177877fdebSMatt Macy vdev_draid_readable(vdev_t *vd, uint64_t physical_offset)
13187877fdebSMatt Macy {
13197877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
13207877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
13217877fdebSMatt Macy 		if (vd == NULL)
13227877fdebSMatt Macy 			return (B_FALSE);
13237877fdebSMatt Macy 	}
13247877fdebSMatt Macy 
13257877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_spare_ops ||
13267877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops) {
13277877fdebSMatt Macy 
13287877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
13297877fdebSMatt Macy 			vdev_t *cvd = vd->vdev_child[c];
13307877fdebSMatt Macy 
13317877fdebSMatt Macy 			if (!vdev_readable(cvd))
13327877fdebSMatt Macy 				continue;
13337877fdebSMatt Macy 
13347877fdebSMatt Macy 			if (vdev_draid_readable(cvd, physical_offset))
13357877fdebSMatt Macy 				return (B_TRUE);
13367877fdebSMatt Macy 		}
13377877fdebSMatt Macy 
13387877fdebSMatt Macy 		return (B_FALSE);
13397877fdebSMatt Macy 	}
13407877fdebSMatt Macy 
13417877fdebSMatt Macy 	return (vdev_readable(vd));
13427877fdebSMatt Macy }
13437877fdebSMatt Macy 
13447877fdebSMatt Macy /*
13457877fdebSMatt Macy  * Returns the first distributed spare found under the provided vdev tree.
13467877fdebSMatt Macy  */
13477877fdebSMatt Macy static vdev_t *
vdev_draid_find_spare(vdev_t * vd)13487877fdebSMatt Macy vdev_draid_find_spare(vdev_t *vd)
13497877fdebSMatt Macy {
13507877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops)
13517877fdebSMatt Macy 		return (vd);
13527877fdebSMatt Macy 
13537877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
13547877fdebSMatt Macy 		vdev_t *svd = vdev_draid_find_spare(vd->vdev_child[c]);
13557877fdebSMatt Macy 		if (svd != NULL)
13567877fdebSMatt Macy 			return (svd);
13577877fdebSMatt Macy 	}
13587877fdebSMatt Macy 
13597877fdebSMatt Macy 	return (NULL);
13607877fdebSMatt Macy }
13617877fdebSMatt Macy 
13627877fdebSMatt Macy /*
13637877fdebSMatt Macy  * Returns B_TRUE if the passed in vdev is currently "faulted".
13647877fdebSMatt Macy  * Faulted, in this context, means that the vdev represents a
13657877fdebSMatt Macy  * replacing or sparing vdev tree.
13667877fdebSMatt Macy  */
13677877fdebSMatt Macy static boolean_t
vdev_draid_faulted(vdev_t * vd,uint64_t physical_offset)13687877fdebSMatt Macy vdev_draid_faulted(vdev_t *vd, uint64_t physical_offset)
13697877fdebSMatt Macy {
13707877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
13717877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
13727877fdebSMatt Macy 		if (vd == NULL)
13737877fdebSMatt Macy 			return (B_FALSE);
13747877fdebSMatt Macy 
13757877fdebSMatt Macy 		/*
13767877fdebSMatt Macy 		 * After resolving the distributed spare to a leaf vdev
13777877fdebSMatt Macy 		 * check the parent to determine if it's "faulted".
13787877fdebSMatt Macy 		 */
13797877fdebSMatt Macy 		vd = vd->vdev_parent;
13807877fdebSMatt Macy 	}
13817877fdebSMatt Macy 
13827877fdebSMatt Macy 	return (vd->vdev_ops == &vdev_replacing_ops ||
13837877fdebSMatt Macy 	    vd->vdev_ops == &vdev_spare_ops);
13847877fdebSMatt Macy }
13857877fdebSMatt Macy 
13867877fdebSMatt Macy /*
13877877fdebSMatt Macy  * Determine if the dRAID block at the logical offset is degraded.
13887877fdebSMatt Macy  * Used by sequential resilver.
13897877fdebSMatt Macy  */
13907877fdebSMatt Macy static boolean_t
vdev_draid_group_degraded(vdev_t * vd,uint64_t offset)13917877fdebSMatt Macy vdev_draid_group_degraded(vdev_t *vd, uint64_t offset)
13927877fdebSMatt Macy {
13937877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
13947877fdebSMatt Macy 
13957877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
13967877fdebSMatt Macy 	ASSERT3U(vdev_draid_get_astart(vd, offset), ==, offset);
13977877fdebSMatt Macy 
13987877fdebSMatt Macy 	uint64_t groupstart, perm;
13997877fdebSMatt Macy 	uint64_t physical_offset = vdev_draid_logical_to_physical(vd,
14007877fdebSMatt Macy 	    offset, &perm, &groupstart);
14017877fdebSMatt Macy 
14027877fdebSMatt Macy 	uint8_t *base;
14037877fdebSMatt Macy 	uint64_t iter;
14047877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
14057877fdebSMatt Macy 
14067877fdebSMatt Macy 	for (uint64_t i = 0; i < vdc->vdc_groupwidth; i++) {
14077877fdebSMatt Macy 		uint64_t c = (groupstart + i) % vdc->vdc_ndisks;
14087877fdebSMatt Macy 		uint64_t cid = vdev_draid_permute_id(vdc, base, iter, c);
14097877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[cid];
14107877fdebSMatt Macy 
14117877fdebSMatt Macy 		/* Group contains a faulted vdev. */
14127877fdebSMatt Macy 		if (vdev_draid_faulted(cvd, physical_offset))
14137877fdebSMatt Macy 			return (B_TRUE);
14147877fdebSMatt Macy 
14157877fdebSMatt Macy 		/*
14167877fdebSMatt Macy 		 * Always check groups with active distributed spares
14177877fdebSMatt Macy 		 * because any vdev failure in the pool will affect them.
14187877fdebSMatt Macy 		 */
14197877fdebSMatt Macy 		if (vdev_draid_find_spare(cvd) != NULL)
14207877fdebSMatt Macy 			return (B_TRUE);
14217877fdebSMatt Macy 	}
14227877fdebSMatt Macy 
14237877fdebSMatt Macy 	return (B_FALSE);
14247877fdebSMatt Macy }
14257877fdebSMatt Macy 
14267877fdebSMatt Macy /*
14277877fdebSMatt Macy  * Determine if the txg is missing.  Used by healing resilver.
14287877fdebSMatt Macy  */
14297877fdebSMatt Macy static boolean_t
vdev_draid_group_missing(vdev_t * vd,uint64_t offset,uint64_t txg,uint64_t size)14307877fdebSMatt Macy vdev_draid_group_missing(vdev_t *vd, uint64_t offset, uint64_t txg,
14317877fdebSMatt Macy     uint64_t size)
14327877fdebSMatt Macy {
14337877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
14347877fdebSMatt Macy 
14357877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
14367877fdebSMatt Macy 	ASSERT3U(vdev_draid_get_astart(vd, offset), ==, offset);
14377877fdebSMatt Macy 
14387877fdebSMatt Macy 	uint64_t groupstart, perm;
14397877fdebSMatt Macy 	uint64_t physical_offset = vdev_draid_logical_to_physical(vd,
14407877fdebSMatt Macy 	    offset, &perm, &groupstart);
14417877fdebSMatt Macy 
14427877fdebSMatt Macy 	uint8_t *base;
14437877fdebSMatt Macy 	uint64_t iter;
14447877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
14457877fdebSMatt Macy 
14467877fdebSMatt Macy 	for (uint64_t i = 0; i < vdc->vdc_groupwidth; i++) {
14477877fdebSMatt Macy 		uint64_t c = (groupstart + i) % vdc->vdc_ndisks;
14487877fdebSMatt Macy 		uint64_t cid = vdev_draid_permute_id(vdc, base, iter, c);
14497877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[cid];
14507877fdebSMatt Macy 
14517877fdebSMatt Macy 		/* Transaction group is known to be partially replicated. */
14527877fdebSMatt Macy 		if (vdev_draid_partial(cvd, physical_offset, txg, size))
14537877fdebSMatt Macy 			return (B_TRUE);
14547877fdebSMatt Macy 
14557877fdebSMatt Macy 		/*
14567877fdebSMatt Macy 		 * Always check groups with active distributed spares
14577877fdebSMatt Macy 		 * because any vdev failure in the pool will affect them.
14587877fdebSMatt Macy 		 */
14597877fdebSMatt Macy 		if (vdev_draid_find_spare(cvd) != NULL)
14607877fdebSMatt Macy 			return (B_TRUE);
14617877fdebSMatt Macy 	}
14627877fdebSMatt Macy 
14637877fdebSMatt Macy 	return (B_FALSE);
14647877fdebSMatt Macy }
14657877fdebSMatt Macy 
14667877fdebSMatt Macy /*
14677877fdebSMatt Macy  * Find the smallest child asize and largest sector size to calculate the
14687877fdebSMatt Macy  * available capacity.  Distributed spares are ignored since their capacity
14697877fdebSMatt Macy  * is also based of the minimum child size in the top-level dRAID.
14707877fdebSMatt Macy  */
14717877fdebSMatt Macy static void
vdev_draid_calculate_asize(vdev_t * vd,uint64_t * asizep,uint64_t * max_asizep,uint64_t * logical_ashiftp,uint64_t * physical_ashiftp)14727877fdebSMatt Macy vdev_draid_calculate_asize(vdev_t *vd, uint64_t *asizep, uint64_t *max_asizep,
14737877fdebSMatt Macy     uint64_t *logical_ashiftp, uint64_t *physical_ashiftp)
14747877fdebSMatt Macy {
14757877fdebSMatt Macy 	uint64_t logical_ashift = 0, physical_ashift = 0;
14767877fdebSMatt Macy 	uint64_t asize = 0, max_asize = 0;
14777877fdebSMatt Macy 
14787877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
14797877fdebSMatt Macy 
14807877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
14817877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[c];
14827877fdebSMatt Macy 
14837877fdebSMatt Macy 		if (cvd->vdev_ops == &vdev_draid_spare_ops)
14847877fdebSMatt Macy 			continue;
14857877fdebSMatt Macy 
14867877fdebSMatt Macy 		asize = MIN(asize - 1, cvd->vdev_asize - 1) + 1;
14877877fdebSMatt Macy 		max_asize = MIN(max_asize - 1, cvd->vdev_max_asize - 1) + 1;
14887877fdebSMatt Macy 		logical_ashift = MAX(logical_ashift, cvd->vdev_ashift);
1489c7046f76SMartin Matuska 	}
1490c7046f76SMartin Matuska 	for (int c = 0; c < vd->vdev_children; c++) {
1491c7046f76SMartin Matuska 		vdev_t *cvd = vd->vdev_child[c];
1492c7046f76SMartin Matuska 
1493c7046f76SMartin Matuska 		if (cvd->vdev_ops == &vdev_draid_spare_ops)
1494c7046f76SMartin Matuska 			continue;
1495c7046f76SMartin Matuska 		physical_ashift = vdev_best_ashift(logical_ashift,
1496c7046f76SMartin Matuska 		    physical_ashift, cvd->vdev_physical_ashift);
14977877fdebSMatt Macy 	}
14987877fdebSMatt Macy 
14997877fdebSMatt Macy 	*asizep = asize;
15007877fdebSMatt Macy 	*max_asizep = max_asize;
15017877fdebSMatt Macy 	*logical_ashiftp = logical_ashift;
15027877fdebSMatt Macy 	*physical_ashiftp = physical_ashift;
15037877fdebSMatt Macy }
15047877fdebSMatt Macy 
15057877fdebSMatt Macy /*
15067877fdebSMatt Macy  * Open spare vdevs.
15077877fdebSMatt Macy  */
15087877fdebSMatt Macy static boolean_t
vdev_draid_open_spares(vdev_t * vd)15097877fdebSMatt Macy vdev_draid_open_spares(vdev_t *vd)
15107877fdebSMatt Macy {
15117877fdebSMatt Macy 	return (vd->vdev_ops == &vdev_draid_spare_ops ||
15127877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops ||
15137877fdebSMatt Macy 	    vd->vdev_ops == &vdev_spare_ops);
15147877fdebSMatt Macy }
15157877fdebSMatt Macy 
15167877fdebSMatt Macy /*
15177877fdebSMatt Macy  * Open all children, excluding spares.
15187877fdebSMatt Macy  */
15197877fdebSMatt Macy static boolean_t
vdev_draid_open_children(vdev_t * vd)15207877fdebSMatt Macy vdev_draid_open_children(vdev_t *vd)
15217877fdebSMatt Macy {
15227877fdebSMatt Macy 	return (!vdev_draid_open_spares(vd));
15237877fdebSMatt Macy }
15247877fdebSMatt Macy 
15257877fdebSMatt Macy /*
15267877fdebSMatt Macy  * Open a top-level dRAID vdev.
15277877fdebSMatt Macy  */
15287877fdebSMatt Macy static int
vdev_draid_open(vdev_t * vd,uint64_t * asize,uint64_t * max_asize,uint64_t * logical_ashift,uint64_t * physical_ashift)15297877fdebSMatt Macy vdev_draid_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
15307877fdebSMatt Macy     uint64_t *logical_ashift, uint64_t *physical_ashift)
15317877fdebSMatt Macy {
15327877fdebSMatt Macy 	vdev_draid_config_t *vdc =  vd->vdev_tsd;
15337877fdebSMatt Macy 	uint64_t nparity = vdc->vdc_nparity;
15347877fdebSMatt Macy 	int open_errors = 0;
15357877fdebSMatt Macy 
15367877fdebSMatt Macy 	if (nparity > VDEV_DRAID_MAXPARITY ||
15377877fdebSMatt Macy 	    vd->vdev_children < nparity + 1) {
15387877fdebSMatt Macy 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
15397877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
15407877fdebSMatt Macy 	}
15417877fdebSMatt Macy 
15427877fdebSMatt Macy 	/*
15437877fdebSMatt Macy 	 * First open the normal children then the distributed spares.  This
15447877fdebSMatt Macy 	 * ordering is important to ensure the distributed spares calculate
15457877fdebSMatt Macy 	 * the correct psize in the event that the dRAID vdevs were expanded.
15467877fdebSMatt Macy 	 */
15477877fdebSMatt Macy 	vdev_open_children_subset(vd, vdev_draid_open_children);
15487877fdebSMatt Macy 	vdev_open_children_subset(vd, vdev_draid_open_spares);
15497877fdebSMatt Macy 
15507877fdebSMatt Macy 	/* Verify enough of the children are available to continue. */
15517877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
15527877fdebSMatt Macy 		if (vd->vdev_child[c]->vdev_open_error != 0) {
15537877fdebSMatt Macy 			if ((++open_errors) > nparity) {
15547877fdebSMatt Macy 				vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
15557877fdebSMatt Macy 				return (SET_ERROR(ENXIO));
15567877fdebSMatt Macy 			}
15577877fdebSMatt Macy 		}
15587877fdebSMatt Macy 	}
15597877fdebSMatt Macy 
15607877fdebSMatt Macy 	/*
15617877fdebSMatt Macy 	 * Allocatable capacity is the sum of the space on all children less
15627877fdebSMatt Macy 	 * the number of distributed spares rounded down to last full row
15637877fdebSMatt Macy 	 * and then to the last full group. An additional 32MB of scratch
15647877fdebSMatt Macy 	 * space is reserved at the end of each child for use by the dRAID
15657877fdebSMatt Macy 	 * expansion feature.
15667877fdebSMatt Macy 	 */
15677877fdebSMatt Macy 	uint64_t child_asize, child_max_asize;
15687877fdebSMatt Macy 	vdev_draid_calculate_asize(vd, &child_asize, &child_max_asize,
15697877fdebSMatt Macy 	    logical_ashift, physical_ashift);
15707877fdebSMatt Macy 
15717877fdebSMatt Macy 	/*
15727877fdebSMatt Macy 	 * Should be unreachable since the minimum child size is 64MB, but
15737877fdebSMatt Macy 	 * we want to make sure an underflow absolutely cannot occur here.
15747877fdebSMatt Macy 	 */
15757877fdebSMatt Macy 	if (child_asize < VDEV_DRAID_REFLOW_RESERVE ||
15767877fdebSMatt Macy 	    child_max_asize < VDEV_DRAID_REFLOW_RESERVE) {
15777877fdebSMatt Macy 		return (SET_ERROR(ENXIO));
15787877fdebSMatt Macy 	}
15797877fdebSMatt Macy 
15807877fdebSMatt Macy 	child_asize = ((child_asize - VDEV_DRAID_REFLOW_RESERVE) /
15817877fdebSMatt Macy 	    VDEV_DRAID_ROWHEIGHT) * VDEV_DRAID_ROWHEIGHT;
15827877fdebSMatt Macy 	child_max_asize = ((child_max_asize - VDEV_DRAID_REFLOW_RESERVE) /
15837877fdebSMatt Macy 	    VDEV_DRAID_ROWHEIGHT) * VDEV_DRAID_ROWHEIGHT;
15847877fdebSMatt Macy 
15857877fdebSMatt Macy 	*asize = (((child_asize * vdc->vdc_ndisks) / vdc->vdc_groupsz) *
15867877fdebSMatt Macy 	    vdc->vdc_groupsz);
15877877fdebSMatt Macy 	*max_asize = (((child_max_asize * vdc->vdc_ndisks) / vdc->vdc_groupsz) *
15887877fdebSMatt Macy 	    vdc->vdc_groupsz);
15897877fdebSMatt Macy 
15907877fdebSMatt Macy 	return (0);
15917877fdebSMatt Macy }
15927877fdebSMatt Macy 
15937877fdebSMatt Macy /*
15947877fdebSMatt Macy  * Close a top-level dRAID vdev.
15957877fdebSMatt Macy  */
15967877fdebSMatt Macy static void
vdev_draid_close(vdev_t * vd)15977877fdebSMatt Macy vdev_draid_close(vdev_t *vd)
15987877fdebSMatt Macy {
15997877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
16007877fdebSMatt Macy 		if (vd->vdev_child[c] != NULL)
16017877fdebSMatt Macy 			vdev_close(vd->vdev_child[c]);
16027877fdebSMatt Macy 	}
16037877fdebSMatt Macy }
16047877fdebSMatt Macy 
16057877fdebSMatt Macy /*
16067877fdebSMatt Macy  * Return the maximum asize for a rebuild zio in the provided range
16077877fdebSMatt Macy  * given the following constraints.  A dRAID chunks may not:
16087877fdebSMatt Macy  *
16097877fdebSMatt Macy  * - Exceed the maximum allowed block size (SPA_MAXBLOCKSIZE), or
16107877fdebSMatt Macy  * - Span dRAID redundancy groups.
16117877fdebSMatt Macy  */
16127877fdebSMatt Macy static uint64_t
vdev_draid_rebuild_asize(vdev_t * vd,uint64_t start,uint64_t asize,uint64_t max_segment)16137877fdebSMatt Macy vdev_draid_rebuild_asize(vdev_t *vd, uint64_t start, uint64_t asize,
16147877fdebSMatt Macy     uint64_t max_segment)
16157877fdebSMatt Macy {
16167877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
16177877fdebSMatt Macy 
16187877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
16197877fdebSMatt Macy 
16207877fdebSMatt Macy 	uint64_t ashift = vd->vdev_ashift;
16217877fdebSMatt Macy 	uint64_t ndata = vdc->vdc_ndata;
16227877fdebSMatt Macy 	uint64_t psize = MIN(P2ROUNDUP(max_segment * ndata, 1 << ashift),
16237877fdebSMatt Macy 	    SPA_MAXBLOCKSIZE);
16247877fdebSMatt Macy 
16257877fdebSMatt Macy 	ASSERT3U(vdev_draid_get_astart(vd, start), ==, start);
16267877fdebSMatt Macy 	ASSERT3U(asize % (vdc->vdc_groupwidth << ashift), ==, 0);
16277877fdebSMatt Macy 
16287877fdebSMatt Macy 	/* Chunks must evenly span all data columns in the group. */
16297877fdebSMatt Macy 	psize = (((psize >> ashift) / ndata) * ndata) << ashift;
16307877fdebSMatt Macy 	uint64_t chunk_size = MIN(asize, vdev_psize_to_asize(vd, psize));
16317877fdebSMatt Macy 
16327877fdebSMatt Macy 	/* Reduce the chunk size to the group space remaining. */
16337877fdebSMatt Macy 	uint64_t group = vdev_draid_offset_to_group(vd, start);
16347877fdebSMatt Macy 	uint64_t left = vdev_draid_group_to_offset(vd, group + 1) - start;
16357877fdebSMatt Macy 	chunk_size = MIN(chunk_size, left);
16367877fdebSMatt Macy 
16377877fdebSMatt Macy 	ASSERT3U(chunk_size % (vdc->vdc_groupwidth << ashift), ==, 0);
16387877fdebSMatt Macy 	ASSERT3U(vdev_draid_offset_to_group(vd, start), ==,
16397877fdebSMatt Macy 	    vdev_draid_offset_to_group(vd, start + chunk_size - 1));
16407877fdebSMatt Macy 
16417877fdebSMatt Macy 	return (chunk_size);
16427877fdebSMatt Macy }
16437877fdebSMatt Macy 
16447877fdebSMatt Macy /*
16457877fdebSMatt Macy  * Align the start of the metaslab to the group width and slightly reduce
16467877fdebSMatt Macy  * its size to a multiple of the group width.  Since full stripe writes are
16477877fdebSMatt Macy  * required by dRAID this space is unallocable.  Furthermore, aligning the
16487877fdebSMatt Macy  * metaslab start is important for vdev initialize and TRIM which both operate
16497877fdebSMatt Macy  * on metaslab boundaries which vdev_xlate() expects to be aligned.
16507877fdebSMatt Macy  */
16517877fdebSMatt Macy static void
vdev_draid_metaslab_init(vdev_t * vd,uint64_t * ms_start,uint64_t * ms_size)16527877fdebSMatt Macy vdev_draid_metaslab_init(vdev_t *vd, uint64_t *ms_start, uint64_t *ms_size)
16537877fdebSMatt Macy {
16547877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
16557877fdebSMatt Macy 
16567877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
16577877fdebSMatt Macy 
16587877fdebSMatt Macy 	uint64_t sz = vdc->vdc_groupwidth << vd->vdev_ashift;
16597877fdebSMatt Macy 	uint64_t astart = vdev_draid_get_astart(vd, *ms_start);
16607877fdebSMatt Macy 	uint64_t asize = ((*ms_size - (astart - *ms_start)) / sz) * sz;
16617877fdebSMatt Macy 
16627877fdebSMatt Macy 	*ms_start = astart;
16637877fdebSMatt Macy 	*ms_size = asize;
16647877fdebSMatt Macy 
16657877fdebSMatt Macy 	ASSERT0(*ms_start % sz);
16667877fdebSMatt Macy 	ASSERT0(*ms_size % sz);
16677877fdebSMatt Macy }
16687877fdebSMatt Macy 
16697877fdebSMatt Macy /*
16707877fdebSMatt Macy  * Add virtual dRAID spares to the list of valid spares. In order to accomplish
16717877fdebSMatt Macy  * this the existing array must be freed and reallocated with the additional
16727877fdebSMatt Macy  * entries.
16737877fdebSMatt Macy  */
16747877fdebSMatt Macy int
vdev_draid_spare_create(nvlist_t * nvroot,vdev_t * vd,uint64_t * ndraidp,uint64_t next_vdev_id)16757877fdebSMatt Macy vdev_draid_spare_create(nvlist_t *nvroot, vdev_t *vd, uint64_t *ndraidp,
16767877fdebSMatt Macy     uint64_t next_vdev_id)
16777877fdebSMatt Macy {
16787877fdebSMatt Macy 	uint64_t draid_nspares = 0;
16797877fdebSMatt Macy 	uint64_t ndraid = 0;
16807877fdebSMatt Macy 	int error;
16817877fdebSMatt Macy 
16827877fdebSMatt Macy 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
16837877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[i];
16847877fdebSMatt Macy 
16857877fdebSMatt Macy 		if (cvd->vdev_ops == &vdev_draid_ops) {
16867877fdebSMatt Macy 			vdev_draid_config_t *vdc = cvd->vdev_tsd;
16877877fdebSMatt Macy 			draid_nspares += vdc->vdc_nspares;
16887877fdebSMatt Macy 			ndraid++;
16897877fdebSMatt Macy 		}
16907877fdebSMatt Macy 	}
16917877fdebSMatt Macy 
16927877fdebSMatt Macy 	if (draid_nspares == 0) {
16937877fdebSMatt Macy 		*ndraidp = ndraid;
16947877fdebSMatt Macy 		return (0);
16957877fdebSMatt Macy 	}
16967877fdebSMatt Macy 
16977877fdebSMatt Macy 	nvlist_t **old_spares, **new_spares;
16987877fdebSMatt Macy 	uint_t old_nspares;
16997877fdebSMatt Macy 	error = nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
17007877fdebSMatt Macy 	    &old_spares, &old_nspares);
17017877fdebSMatt Macy 	if (error)
17027877fdebSMatt Macy 		old_nspares = 0;
17037877fdebSMatt Macy 
17047877fdebSMatt Macy 	/* Allocate memory and copy of the existing spares. */
17057877fdebSMatt Macy 	new_spares = kmem_alloc(sizeof (nvlist_t *) *
17067877fdebSMatt Macy 	    (draid_nspares + old_nspares), KM_SLEEP);
17077877fdebSMatt Macy 	for (uint_t i = 0; i < old_nspares; i++)
17087877fdebSMatt Macy 		new_spares[i] = fnvlist_dup(old_spares[i]);
17097877fdebSMatt Macy 
17107877fdebSMatt Macy 	/* Add new distributed spares to ZPOOL_CONFIG_SPARES. */
17117877fdebSMatt Macy 	uint64_t n = old_nspares;
17127877fdebSMatt Macy 	for (uint64_t vdev_id = 0; vdev_id < vd->vdev_children; vdev_id++) {
17137877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[vdev_id];
17147877fdebSMatt Macy 		char path[64];
17157877fdebSMatt Macy 
17167877fdebSMatt Macy 		if (cvd->vdev_ops != &vdev_draid_ops)
17177877fdebSMatt Macy 			continue;
17187877fdebSMatt Macy 
17197877fdebSMatt Macy 		vdev_draid_config_t *vdc = cvd->vdev_tsd;
17207877fdebSMatt Macy 		uint64_t nspares = vdc->vdc_nspares;
17217877fdebSMatt Macy 		uint64_t nparity = vdc->vdc_nparity;
17227877fdebSMatt Macy 
17237877fdebSMatt Macy 		for (uint64_t spare_id = 0; spare_id < nspares; spare_id++) {
1724da5137abSMartin Matuska 			memset(path, 0, sizeof (path));
17257877fdebSMatt Macy 			(void) snprintf(path, sizeof (path) - 1,
17267877fdebSMatt Macy 			    "%s%llu-%llu-%llu", VDEV_TYPE_DRAID,
17277877fdebSMatt Macy 			    (u_longlong_t)nparity,
17287877fdebSMatt Macy 			    (u_longlong_t)next_vdev_id + vdev_id,
17297877fdebSMatt Macy 			    (u_longlong_t)spare_id);
17307877fdebSMatt Macy 
17317877fdebSMatt Macy 			nvlist_t *spare = fnvlist_alloc();
17327877fdebSMatt Macy 			fnvlist_add_string(spare, ZPOOL_CONFIG_PATH, path);
17337877fdebSMatt Macy 			fnvlist_add_string(spare, ZPOOL_CONFIG_TYPE,
17347877fdebSMatt Macy 			    VDEV_TYPE_DRAID_SPARE);
17357877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_TOP_GUID,
17367877fdebSMatt Macy 			    cvd->vdev_guid);
17377877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_SPARE_ID,
17387877fdebSMatt Macy 			    spare_id);
17397877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_IS_LOG, 0);
17407877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_IS_SPARE, 1);
17417877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_WHOLE_DISK, 1);
17427877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_ASHIFT,
17437877fdebSMatt Macy 			    cvd->vdev_ashift);
17447877fdebSMatt Macy 
17457877fdebSMatt Macy 			new_spares[n] = spare;
17467877fdebSMatt Macy 			n++;
17477877fdebSMatt Macy 		}
17487877fdebSMatt Macy 	}
17497877fdebSMatt Macy 
17507877fdebSMatt Macy 	if (n > 0) {
17517877fdebSMatt Macy 		(void) nvlist_remove_all(nvroot, ZPOOL_CONFIG_SPARES);
17527877fdebSMatt Macy 		fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1753681ce946SMartin Matuska 		    (const nvlist_t **)new_spares, n);
17547877fdebSMatt Macy 	}
17557877fdebSMatt Macy 
17567877fdebSMatt Macy 	for (int i = 0; i < n; i++)
17577877fdebSMatt Macy 		nvlist_free(new_spares[i]);
17587877fdebSMatt Macy 
17597877fdebSMatt Macy 	kmem_free(new_spares, sizeof (*new_spares) * n);
17607877fdebSMatt Macy 	*ndraidp = ndraid;
17617877fdebSMatt Macy 
17627877fdebSMatt Macy 	return (0);
17637877fdebSMatt Macy }
17647877fdebSMatt Macy 
17657877fdebSMatt Macy /*
17667877fdebSMatt Macy  * Determine if any portion of the provided block resides on a child vdev
17677877fdebSMatt Macy  * with a dirty DTL and therefore needs to be resilvered.
17687877fdebSMatt Macy  */
17697877fdebSMatt Macy static boolean_t
vdev_draid_need_resilver(vdev_t * vd,const dva_t * dva,size_t psize,uint64_t phys_birth)17707877fdebSMatt Macy vdev_draid_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
17717877fdebSMatt Macy     uint64_t phys_birth)
17727877fdebSMatt Macy {
17737877fdebSMatt Macy 	uint64_t offset = DVA_GET_OFFSET(dva);
1774*071ab5a1SMartin Matuska 	uint64_t asize = vdev_draid_psize_to_asize(vd, psize, 0);
17757877fdebSMatt Macy 
17767877fdebSMatt Macy 	if (phys_birth == TXG_UNKNOWN) {
17777877fdebSMatt Macy 		/*
17787877fdebSMatt Macy 		 * Sequential resilver.  There is no meaningful phys_birth
17797877fdebSMatt Macy 		 * for this block, we can only determine if block resides
17807877fdebSMatt Macy 		 * in a degraded group in which case it must be resilvered.
17817877fdebSMatt Macy 		 */
17827877fdebSMatt Macy 		ASSERT3U(vdev_draid_offset_to_group(vd, offset), ==,
17837877fdebSMatt Macy 		    vdev_draid_offset_to_group(vd, offset + asize - 1));
17847877fdebSMatt Macy 
17857877fdebSMatt Macy 		return (vdev_draid_group_degraded(vd, offset));
17867877fdebSMatt Macy 	} else {
17877877fdebSMatt Macy 		/*
17887877fdebSMatt Macy 		 * Healing resilver.  TXGs not in DTL_PARTIAL are intact,
17897877fdebSMatt Macy 		 * as are blocks in non-degraded groups.
17907877fdebSMatt Macy 		 */
17917877fdebSMatt Macy 		if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1))
17927877fdebSMatt Macy 			return (B_FALSE);
17937877fdebSMatt Macy 
17947877fdebSMatt Macy 		if (vdev_draid_group_missing(vd, offset, phys_birth, 1))
17957877fdebSMatt Macy 			return (B_TRUE);
17967877fdebSMatt Macy 
17977877fdebSMatt Macy 		/* The block may span groups in which case check both. */
17987877fdebSMatt Macy 		if (vdev_draid_offset_to_group(vd, offset) !=
17997877fdebSMatt Macy 		    vdev_draid_offset_to_group(vd, offset + asize - 1)) {
18007877fdebSMatt Macy 			if (vdev_draid_group_missing(vd,
18017877fdebSMatt Macy 			    offset + asize, phys_birth, 1))
18027877fdebSMatt Macy 				return (B_TRUE);
18037877fdebSMatt Macy 		}
18047877fdebSMatt Macy 
18057877fdebSMatt Macy 		return (B_FALSE);
18067877fdebSMatt Macy 	}
18077877fdebSMatt Macy }
18087877fdebSMatt Macy 
18097877fdebSMatt Macy static boolean_t
vdev_draid_rebuilding(vdev_t * vd)18107877fdebSMatt Macy vdev_draid_rebuilding(vdev_t *vd)
18117877fdebSMatt Macy {
18127877fdebSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_rebuild_txg)
18137877fdebSMatt Macy 		return (B_TRUE);
18147877fdebSMatt Macy 
18157877fdebSMatt Macy 	for (int i = 0; i < vd->vdev_children; i++) {
18167877fdebSMatt Macy 		if (vdev_draid_rebuilding(vd->vdev_child[i])) {
18177877fdebSMatt Macy 			return (B_TRUE);
18187877fdebSMatt Macy 		}
18197877fdebSMatt Macy 	}
18207877fdebSMatt Macy 
18217877fdebSMatt Macy 	return (B_FALSE);
18227877fdebSMatt Macy }
18237877fdebSMatt Macy 
18247877fdebSMatt Macy static void
vdev_draid_io_verify(vdev_t * vd,raidz_row_t * rr,int col)18257877fdebSMatt Macy vdev_draid_io_verify(vdev_t *vd, raidz_row_t *rr, int col)
18267877fdebSMatt Macy {
18277877fdebSMatt Macy #ifdef ZFS_DEBUG
1828b59a0cdeSMartin Matuska 	zfs_range_seg64_t logical_rs, physical_rs, remain_rs;
18297877fdebSMatt Macy 	logical_rs.rs_start = rr->rr_offset;
18307877fdebSMatt Macy 	logical_rs.rs_end = logical_rs.rs_start +
1831*071ab5a1SMartin Matuska 	    vdev_draid_psize_to_asize(vd, rr->rr_size, 0);
18327877fdebSMatt Macy 
18337877fdebSMatt Macy 	raidz_col_t *rc = &rr->rr_col[col];
18347877fdebSMatt Macy 	vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
18357877fdebSMatt Macy 
18367877fdebSMatt Macy 	vdev_xlate(cvd, &logical_rs, &physical_rs, &remain_rs);
18377877fdebSMatt Macy 	ASSERT(vdev_xlate_is_empty(&remain_rs));
18387877fdebSMatt Macy 	ASSERT3U(rc->rc_offset, ==, physical_rs.rs_start);
18397877fdebSMatt Macy 	ASSERT3U(rc->rc_offset, <, physical_rs.rs_end);
18407877fdebSMatt Macy 	ASSERT3U(rc->rc_offset + rc->rc_size, ==, physical_rs.rs_end);
18417877fdebSMatt Macy #endif
18427877fdebSMatt Macy }
18437877fdebSMatt Macy 
18447877fdebSMatt Macy /*
18457877fdebSMatt Macy  * For write operations:
18467877fdebSMatt Macy  * 1. Generate the parity data
18477877fdebSMatt Macy  * 2. Create child zio write operations to each column's vdev, for both
18487877fdebSMatt Macy  *    data and parity.  A gang ABD is allocated by vdev_draid_map_alloc()
18497877fdebSMatt Macy  *    if a skip sector needs to be added to a column.
18507877fdebSMatt Macy  */
18517877fdebSMatt Macy static void
vdev_draid_io_start_write(zio_t * zio,raidz_row_t * rr)18527877fdebSMatt Macy vdev_draid_io_start_write(zio_t *zio, raidz_row_t *rr)
18537877fdebSMatt Macy {
18547877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
18557877fdebSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
18567877fdebSMatt Macy 
18577877fdebSMatt Macy 	vdev_raidz_generate_parity_row(rm, rr);
18587877fdebSMatt Macy 
18597877fdebSMatt Macy 	for (int c = 0; c < rr->rr_cols; c++) {
18607877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
18617877fdebSMatt Macy 
18627877fdebSMatt Macy 		/*
18637877fdebSMatt Macy 		 * Empty columns are zero filled and included in the parity
18647877fdebSMatt Macy 		 * calculation and therefore must be written.
18657877fdebSMatt Macy 		 */
18667877fdebSMatt Macy 		ASSERT3U(rc->rc_size, !=, 0);
18677877fdebSMatt Macy 
18687877fdebSMatt Macy 		/* Verify physical to logical translation */
18697877fdebSMatt Macy 		vdev_draid_io_verify(vd, rr, c);
18707877fdebSMatt Macy 
18717877fdebSMatt Macy 		zio_nowait(zio_vdev_child_io(zio, NULL,
18727877fdebSMatt Macy 		    vd->vdev_child[rc->rc_devidx], rc->rc_offset,
18737877fdebSMatt Macy 		    rc->rc_abd, rc->rc_size, zio->io_type, zio->io_priority,
18747877fdebSMatt Macy 		    0, vdev_raidz_child_done, rc));
18757877fdebSMatt Macy 	}
18767877fdebSMatt Macy }
18777877fdebSMatt Macy 
18787877fdebSMatt Macy /*
18797877fdebSMatt Macy  * For read operations:
18807877fdebSMatt Macy  * 1. The vdev_draid_map_alloc() function will create a minimal raidz
18817877fdebSMatt Macy  *    mapping for the read based on the zio->io_flags.  There are two
18827877fdebSMatt Macy  *    possible mappings either 1) a normal read, or 2) a scrub/resilver.
18837877fdebSMatt Macy  * 2. Create the zio read operations.  This will include all parity
18847877fdebSMatt Macy  *    columns and skip sectors for a scrub/resilver.
18857877fdebSMatt Macy  */
18867877fdebSMatt Macy static void
vdev_draid_io_start_read(zio_t * zio,raidz_row_t * rr)18877877fdebSMatt Macy vdev_draid_io_start_read(zio_t *zio, raidz_row_t *rr)
18887877fdebSMatt Macy {
18897877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
18907877fdebSMatt Macy 
18917877fdebSMatt Macy 	/* Sequential rebuild must do IO at redundancy group boundary. */
18927877fdebSMatt Macy 	IMPLY(zio->io_priority == ZIO_PRIORITY_REBUILD, rr->rr_nempty == 0);
18937877fdebSMatt Macy 
18947877fdebSMatt Macy 	/*
18957877fdebSMatt Macy 	 * Iterate over the columns in reverse order so that we hit the parity
18967877fdebSMatt Macy 	 * last.  Any errors along the way will force us to read the parity.
18977877fdebSMatt Macy 	 * For scrub/resilver IOs which verify skip sectors, a gang ABD will
18987877fdebSMatt Macy 	 * have been allocated to store them and rc->rc_size is increased.
18997877fdebSMatt Macy 	 */
19007877fdebSMatt Macy 	for (int c = rr->rr_cols - 1; c >= 0; c--) {
19017877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
19027877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
19037877fdebSMatt Macy 
19047877fdebSMatt Macy 		if (!vdev_draid_readable(cvd, rc->rc_offset)) {
19057877fdebSMatt Macy 			if (c >= rr->rr_firstdatacol)
19067877fdebSMatt Macy 				rr->rr_missingdata++;
19077877fdebSMatt Macy 			else
19087877fdebSMatt Macy 				rr->rr_missingparity++;
19097877fdebSMatt Macy 			rc->rc_error = SET_ERROR(ENXIO);
19107877fdebSMatt Macy 			rc->rc_tried = 1;
19117877fdebSMatt Macy 			rc->rc_skipped = 1;
19127877fdebSMatt Macy 			continue;
19137877fdebSMatt Macy 		}
19147877fdebSMatt Macy 
19157877fdebSMatt Macy 		if (vdev_draid_missing(cvd, rc->rc_offset, zio->io_txg, 1)) {
19167877fdebSMatt Macy 			if (c >= rr->rr_firstdatacol)
19177877fdebSMatt Macy 				rr->rr_missingdata++;
19187877fdebSMatt Macy 			else
19197877fdebSMatt Macy 				rr->rr_missingparity++;
19207877fdebSMatt Macy 			rc->rc_error = SET_ERROR(ESTALE);
19217877fdebSMatt Macy 			rc->rc_skipped = 1;
19227877fdebSMatt Macy 			continue;
19237877fdebSMatt Macy 		}
19247877fdebSMatt Macy 
19257877fdebSMatt Macy 		/*
19267877fdebSMatt Macy 		 * Empty columns may be read during vdev_draid_io_done().
19277877fdebSMatt Macy 		 * Only skip them after the readable and missing checks
19287877fdebSMatt Macy 		 * verify they are available.
19297877fdebSMatt Macy 		 */
19307877fdebSMatt Macy 		if (rc->rc_size == 0) {
19317877fdebSMatt Macy 			rc->rc_skipped = 1;
19327877fdebSMatt Macy 			continue;
19337877fdebSMatt Macy 		}
19347877fdebSMatt Macy 
19357877fdebSMatt Macy 		if (zio->io_flags & ZIO_FLAG_RESILVER) {
19367877fdebSMatt Macy 			vdev_t *svd;
19377877fdebSMatt Macy 
19387877fdebSMatt Macy 			/*
193916038816SMartin Matuska 			 * Sequential rebuilds need to always consider the data
194016038816SMartin Matuska 			 * on the child being rebuilt to be stale.  This is
194116038816SMartin Matuska 			 * important when all columns are available to aid
194216038816SMartin Matuska 			 * known reconstruction in identifing which columns
194316038816SMartin Matuska 			 * contain incorrect data.
194416038816SMartin Matuska 			 *
194516038816SMartin Matuska 			 * Furthermore, all repairs need to be constrained to
194616038816SMartin Matuska 			 * the devices being rebuilt because without a checksum
194716038816SMartin Matuska 			 * we cannot verify the data is actually correct and
194816038816SMartin Matuska 			 * performing an incorrect repair could result in
194916038816SMartin Matuska 			 * locking in damage and making the data unrecoverable.
195016038816SMartin Matuska 			 */
195116038816SMartin Matuska 			if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
195216038816SMartin Matuska 				if (vdev_draid_rebuilding(cvd)) {
195316038816SMartin Matuska 					if (c >= rr->rr_firstdatacol)
195416038816SMartin Matuska 						rr->rr_missingdata++;
195516038816SMartin Matuska 					else
195616038816SMartin Matuska 						rr->rr_missingparity++;
195716038816SMartin Matuska 					rc->rc_error = SET_ERROR(ESTALE);
195816038816SMartin Matuska 					rc->rc_skipped = 1;
195916038816SMartin Matuska 					rc->rc_allow_repair = 1;
196016038816SMartin Matuska 					continue;
196116038816SMartin Matuska 				} else {
196216038816SMartin Matuska 					rc->rc_allow_repair = 0;
196316038816SMartin Matuska 				}
196416038816SMartin Matuska 			} else {
196516038816SMartin Matuska 				rc->rc_allow_repair = 1;
196616038816SMartin Matuska 			}
196716038816SMartin Matuska 
196816038816SMartin Matuska 			/*
19697877fdebSMatt Macy 			 * If this child is a distributed spare then the
19707877fdebSMatt Macy 			 * offset might reside on the vdev being replaced.
19717877fdebSMatt Macy 			 * In which case this data must be written to the
19727877fdebSMatt Macy 			 * new device.  Failure to do so would result in
19737877fdebSMatt Macy 			 * checksum errors when the old device is detached
19747877fdebSMatt Macy 			 * and the pool is scrubbed.
19757877fdebSMatt Macy 			 */
19767877fdebSMatt Macy 			if ((svd = vdev_draid_find_spare(cvd)) != NULL) {
19777877fdebSMatt Macy 				svd = vdev_draid_spare_get_child(svd,
19787877fdebSMatt Macy 				    rc->rc_offset);
19797877fdebSMatt Macy 				if (svd && (svd->vdev_ops == &vdev_spare_ops ||
19807877fdebSMatt Macy 				    svd->vdev_ops == &vdev_replacing_ops)) {
198116038816SMartin Matuska 					rc->rc_force_repair = 1;
198216038816SMartin Matuska 
198316038816SMartin Matuska 					if (vdev_draid_rebuilding(svd))
198416038816SMartin Matuska 						rc->rc_allow_repair = 1;
19857877fdebSMatt Macy 				}
19867877fdebSMatt Macy 			}
19877877fdebSMatt Macy 
19887877fdebSMatt Macy 			/*
19897877fdebSMatt Macy 			 * Always issue a repair IO to this child when its
19907877fdebSMatt Macy 			 * a spare or replacing vdev with an active rebuild.
19917877fdebSMatt Macy 			 */
19927877fdebSMatt Macy 			if ((cvd->vdev_ops == &vdev_spare_ops ||
19937877fdebSMatt Macy 			    cvd->vdev_ops == &vdev_replacing_ops) &&
19947877fdebSMatt Macy 			    vdev_draid_rebuilding(cvd)) {
199516038816SMartin Matuska 				rc->rc_force_repair = 1;
199616038816SMartin Matuska 				rc->rc_allow_repair = 1;
19977877fdebSMatt Macy 			}
19987877fdebSMatt Macy 		}
19997877fdebSMatt Macy 	}
20007877fdebSMatt Macy 
20017877fdebSMatt Macy 	/*
20027877fdebSMatt Macy 	 * Either a parity or data column is missing this means a repair
20037877fdebSMatt Macy 	 * may be attempted by vdev_draid_io_done().  Expand the raid map
20047877fdebSMatt Macy 	 * to read in empty columns which are needed along with the parity
20057877fdebSMatt Macy 	 * during reconstruction.
20067877fdebSMatt Macy 	 */
20077877fdebSMatt Macy 	if ((rr->rr_missingdata > 0 || rr->rr_missingparity > 0) &&
20087877fdebSMatt Macy 	    rr->rr_nempty > 0 && rr->rr_abd_empty == NULL) {
20097877fdebSMatt Macy 		vdev_draid_map_alloc_empty(zio, rr);
20107877fdebSMatt Macy 	}
20117877fdebSMatt Macy 
20127877fdebSMatt Macy 	for (int c = rr->rr_cols - 1; c >= 0; c--) {
20137877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
20147877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
20157877fdebSMatt Macy 
20167877fdebSMatt Macy 		if (rc->rc_error || rc->rc_size == 0)
20177877fdebSMatt Macy 			continue;
20187877fdebSMatt Macy 
20197877fdebSMatt Macy 		if (c >= rr->rr_firstdatacol || rr->rr_missingdata > 0 ||
20207877fdebSMatt Macy 		    (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
20217877fdebSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
20227877fdebSMatt Macy 			    rc->rc_offset, rc->rc_abd, rc->rc_size,
20237877fdebSMatt Macy 			    zio->io_type, zio->io_priority, 0,
20247877fdebSMatt Macy 			    vdev_raidz_child_done, rc));
20257877fdebSMatt Macy 		}
20267877fdebSMatt Macy 	}
20277877fdebSMatt Macy }
20287877fdebSMatt Macy 
20297877fdebSMatt Macy /*
20307877fdebSMatt Macy  * Start an IO operation to a dRAID vdev.
20317877fdebSMatt Macy  */
20327877fdebSMatt Macy static void
vdev_draid_io_start(zio_t * zio)20337877fdebSMatt Macy vdev_draid_io_start(zio_t *zio)
20347877fdebSMatt Macy {
20357877fdebSMatt Macy 	vdev_t *vd __maybe_unused = zio->io_vd;
20367877fdebSMatt Macy 
20377877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
20387877fdebSMatt Macy 	ASSERT3U(zio->io_offset, ==, vdev_draid_get_astart(vd, zio->io_offset));
20397877fdebSMatt Macy 
2040f9693befSMartin Matuska 	raidz_map_t *rm = vdev_draid_map_alloc(zio);
2041f9693befSMartin Matuska 	zio->io_vsd = rm;
2042f9693befSMartin Matuska 	zio->io_vsd_ops = &vdev_raidz_vsd_ops;
20437877fdebSMatt Macy 
20447877fdebSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
20457877fdebSMatt Macy 		for (int i = 0; i < rm->rm_nrows; i++) {
20467877fdebSMatt Macy 			vdev_draid_io_start_write(zio, rm->rm_row[i]);
20477877fdebSMatt Macy 		}
20487877fdebSMatt Macy 	} else {
20497877fdebSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_READ);
20507877fdebSMatt Macy 
20517877fdebSMatt Macy 		for (int i = 0; i < rm->rm_nrows; i++) {
20527877fdebSMatt Macy 			vdev_draid_io_start_read(zio, rm->rm_row[i]);
20537877fdebSMatt Macy 		}
20547877fdebSMatt Macy 	}
20557877fdebSMatt Macy 
20567877fdebSMatt Macy 	zio_execute(zio);
20577877fdebSMatt Macy }
20587877fdebSMatt Macy 
20597877fdebSMatt Macy /*
20607877fdebSMatt Macy  * Complete an IO operation on a dRAID vdev.  The raidz logic can be applied
20617877fdebSMatt Macy  * to dRAID since the layout is fully described by the raidz_map_t.
20627877fdebSMatt Macy  */
20637877fdebSMatt Macy static void
vdev_draid_io_done(zio_t * zio)20647877fdebSMatt Macy vdev_draid_io_done(zio_t *zio)
20657877fdebSMatt Macy {
20667877fdebSMatt Macy 	vdev_raidz_io_done(zio);
20677877fdebSMatt Macy }
20687877fdebSMatt Macy 
20697877fdebSMatt Macy static void
vdev_draid_state_change(vdev_t * vd,int faulted,int degraded)20707877fdebSMatt Macy vdev_draid_state_change(vdev_t *vd, int faulted, int degraded)
20717877fdebSMatt Macy {
20727877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
20737877fdebSMatt Macy 	ASSERT(vd->vdev_ops == &vdev_draid_ops);
20747877fdebSMatt Macy 
20757877fdebSMatt Macy 	if (faulted > vdc->vdc_nparity)
20767877fdebSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
20777877fdebSMatt Macy 		    VDEV_AUX_NO_REPLICAS);
20787877fdebSMatt Macy 	else if (degraded + faulted != 0)
20797877fdebSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
20807877fdebSMatt Macy 	else
20817877fdebSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
20827877fdebSMatt Macy }
20837877fdebSMatt Macy 
20847877fdebSMatt Macy static void
vdev_draid_xlate(vdev_t * cvd,const zfs_range_seg64_t * logical_rs,zfs_range_seg64_t * physical_rs,zfs_range_seg64_t * remain_rs)2085b59a0cdeSMartin Matuska vdev_draid_xlate(vdev_t *cvd, const zfs_range_seg64_t *logical_rs,
2086b59a0cdeSMartin Matuska     zfs_range_seg64_t *physical_rs, zfs_range_seg64_t *remain_rs)
20877877fdebSMatt Macy {
20887877fdebSMatt Macy 	vdev_t *raidvd = cvd->vdev_parent;
20897877fdebSMatt Macy 	ASSERT(raidvd->vdev_ops == &vdev_draid_ops);
20907877fdebSMatt Macy 
20917877fdebSMatt Macy 	vdev_draid_config_t *vdc = raidvd->vdev_tsd;
20927877fdebSMatt Macy 	uint64_t ashift = raidvd->vdev_top->vdev_ashift;
20937877fdebSMatt Macy 
20947877fdebSMatt Macy 	/* Make sure the offsets are block-aligned */
20957877fdebSMatt Macy 	ASSERT0(logical_rs->rs_start % (1 << ashift));
20967877fdebSMatt Macy 	ASSERT0(logical_rs->rs_end % (1 << ashift));
20977877fdebSMatt Macy 
20987877fdebSMatt Macy 	uint64_t logical_start = logical_rs->rs_start;
20997877fdebSMatt Macy 	uint64_t logical_end = logical_rs->rs_end;
21007877fdebSMatt Macy 
21017877fdebSMatt Macy 	/*
21027877fdebSMatt Macy 	 * Unaligned ranges must be skipped. All metaslabs are correctly
21037877fdebSMatt Macy 	 * aligned so this should not happen, but this case is handled in
21047877fdebSMatt Macy 	 * case it's needed by future callers.
21057877fdebSMatt Macy 	 */
21067877fdebSMatt Macy 	uint64_t astart = vdev_draid_get_astart(raidvd, logical_start);
21077877fdebSMatt Macy 	if (astart != logical_start) {
21087877fdebSMatt Macy 		physical_rs->rs_start = logical_start;
21097877fdebSMatt Macy 		physical_rs->rs_end = logical_start;
21107877fdebSMatt Macy 		remain_rs->rs_start = MIN(astart, logical_end);
21117877fdebSMatt Macy 		remain_rs->rs_end = logical_end;
21127877fdebSMatt Macy 		return;
21137877fdebSMatt Macy 	}
21147877fdebSMatt Macy 
21157877fdebSMatt Macy 	/*
21167877fdebSMatt Macy 	 * Unlike with mirrors and raidz a dRAID logical range can map
21177877fdebSMatt Macy 	 * to multiple non-contiguous physical ranges. This is handled by
21187877fdebSMatt Macy 	 * limiting the size of the logical range to a single group and
21197877fdebSMatt Macy 	 * setting the remain argument such that it describes the remaining
21207877fdebSMatt Macy 	 * unmapped logical range. This is stricter than absolutely
21217877fdebSMatt Macy 	 * necessary but helps simplify the logic below.
21227877fdebSMatt Macy 	 */
21237877fdebSMatt Macy 	uint64_t group = vdev_draid_offset_to_group(raidvd, logical_start);
21247877fdebSMatt Macy 	uint64_t nextstart = vdev_draid_group_to_offset(raidvd, group + 1);
21257877fdebSMatt Macy 	if (logical_end > nextstart)
21267877fdebSMatt Macy 		logical_end = nextstart;
21277877fdebSMatt Macy 
21287877fdebSMatt Macy 	/* Find the starting offset for each vdev in the group */
21297877fdebSMatt Macy 	uint64_t perm, groupstart;
21307877fdebSMatt Macy 	uint64_t start = vdev_draid_logical_to_physical(raidvd,
21317877fdebSMatt Macy 	    logical_start, &perm, &groupstart);
21327877fdebSMatt Macy 	uint64_t end = start;
21337877fdebSMatt Macy 
21347877fdebSMatt Macy 	uint8_t *base;
21357877fdebSMatt Macy 	uint64_t iter, id;
21367877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
21377877fdebSMatt Macy 
21387877fdebSMatt Macy 	/*
21397877fdebSMatt Macy 	 * Check if the passed child falls within the group.  If it does
21407877fdebSMatt Macy 	 * update the start and end to reflect the physical range.
21417877fdebSMatt Macy 	 * Otherwise, leave them unmodified which will result in an empty
21427877fdebSMatt Macy 	 * (zero-length) physical range being returned.
21437877fdebSMatt Macy 	 */
21447877fdebSMatt Macy 	for (uint64_t i = 0; i < vdc->vdc_groupwidth; i++) {
21457877fdebSMatt Macy 		uint64_t c = (groupstart + i) % vdc->vdc_ndisks;
21467877fdebSMatt Macy 
21477877fdebSMatt Macy 		if (c == 0 && i != 0) {
21487877fdebSMatt Macy 			/* the group wrapped, increment the start */
21497877fdebSMatt Macy 			start += VDEV_DRAID_ROWHEIGHT;
21507877fdebSMatt Macy 			end = start;
21517877fdebSMatt Macy 		}
21527877fdebSMatt Macy 
21537877fdebSMatt Macy 		id = vdev_draid_permute_id(vdc, base, iter, c);
21547877fdebSMatt Macy 		if (id == cvd->vdev_id) {
21557877fdebSMatt Macy 			uint64_t b_size = (logical_end >> ashift) -
21567877fdebSMatt Macy 			    (logical_start >> ashift);
21577877fdebSMatt Macy 			ASSERT3U(b_size, >, 0);
21587877fdebSMatt Macy 			end = start + ((((b_size - 1) /
21597877fdebSMatt Macy 			    vdc->vdc_groupwidth) + 1) << ashift);
21607877fdebSMatt Macy 			break;
21617877fdebSMatt Macy 		}
21627877fdebSMatt Macy 	}
21637877fdebSMatt Macy 	physical_rs->rs_start = start;
21647877fdebSMatt Macy 	physical_rs->rs_end = end;
21657877fdebSMatt Macy 
21667877fdebSMatt Macy 	/*
21677877fdebSMatt Macy 	 * Only top-level vdevs are allowed to set remain_rs because
21687877fdebSMatt Macy 	 * when .vdev_op_xlate() is called for their children the full
21697877fdebSMatt Macy 	 * logical range is not provided by vdev_xlate().
21707877fdebSMatt Macy 	 */
21717877fdebSMatt Macy 	remain_rs->rs_start = logical_end;
21727877fdebSMatt Macy 	remain_rs->rs_end = logical_rs->rs_end;
21737877fdebSMatt Macy 
21747877fdebSMatt Macy 	ASSERT3U(physical_rs->rs_start, <=, logical_start);
21757877fdebSMatt Macy 	ASSERT3U(physical_rs->rs_end - physical_rs->rs_start, <=,
21767877fdebSMatt Macy 	    logical_end - logical_start);
21777877fdebSMatt Macy }
21787877fdebSMatt Macy 
21797877fdebSMatt Macy /*
21807877fdebSMatt Macy  * Add dRAID specific fields to the config nvlist.
21817877fdebSMatt Macy  */
21827877fdebSMatt Macy static void
vdev_draid_config_generate(vdev_t * vd,nvlist_t * nv)21837877fdebSMatt Macy vdev_draid_config_generate(vdev_t *vd, nvlist_t *nv)
21847877fdebSMatt Macy {
21857877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
21867877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
21877877fdebSMatt Macy 
21887877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vdc->vdc_nparity);
21897877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, vdc->vdc_ndata);
21907877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, vdc->vdc_nspares);
21917877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, vdc->vdc_ngroups);
21927877fdebSMatt Macy }
21937877fdebSMatt Macy 
21947877fdebSMatt Macy /*
21957877fdebSMatt Macy  * Initialize private dRAID specific fields from the nvlist.
21967877fdebSMatt Macy  */
21977877fdebSMatt Macy static int
vdev_draid_init(spa_t * spa,nvlist_t * nv,void ** tsd)21987877fdebSMatt Macy vdev_draid_init(spa_t *spa, nvlist_t *nv, void **tsd)
21997877fdebSMatt Macy {
2200e92ffd9bSMartin Matuska 	(void) spa;
22017877fdebSMatt Macy 	uint64_t ndata, nparity, nspares, ngroups;
22027877fdebSMatt Macy 	int error;
22037877fdebSMatt Macy 
22047877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, &ndata))
22057877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22067877fdebSMatt Macy 
22077877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, &nparity) ||
22087877fdebSMatt Macy 	    nparity == 0 || nparity > VDEV_DRAID_MAXPARITY) {
22097877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22107877fdebSMatt Macy 	}
22117877fdebSMatt Macy 
22127877fdebSMatt Macy 	uint_t children;
22137877fdebSMatt Macy 	nvlist_t **child;
22147877fdebSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
22157877fdebSMatt Macy 	    &child, &children) != 0 || children == 0 ||
22167877fdebSMatt Macy 	    children > VDEV_DRAID_MAX_CHILDREN) {
22177877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22187877fdebSMatt Macy 	}
22197877fdebSMatt Macy 
22207877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, &nspares) ||
22217877fdebSMatt Macy 	    nspares > 100 || nspares > (children - (ndata + nparity))) {
22227877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22237877fdebSMatt Macy 	}
22247877fdebSMatt Macy 
22257877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, &ngroups) ||
22267877fdebSMatt Macy 	    ngroups == 0 || ngroups > VDEV_DRAID_MAX_CHILDREN) {
22277877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22287877fdebSMatt Macy 	}
22297877fdebSMatt Macy 
22307877fdebSMatt Macy 	/*
22317877fdebSMatt Macy 	 * Validate the minimum number of children exist per group for the
22327877fdebSMatt Macy 	 * specified parity level (draid1 >= 2, draid2 >= 3, draid3 >= 4).
22337877fdebSMatt Macy 	 */
22347877fdebSMatt Macy 	if (children < (ndata + nparity + nspares))
22357877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22367877fdebSMatt Macy 
22377877fdebSMatt Macy 	/*
22387877fdebSMatt Macy 	 * Create the dRAID configuration using the pool nvlist configuration
22397877fdebSMatt Macy 	 * and the fixed mapping for the correct number of children.
22407877fdebSMatt Macy 	 */
22417877fdebSMatt Macy 	vdev_draid_config_t *vdc;
22427877fdebSMatt Macy 	const draid_map_t *map;
22437877fdebSMatt Macy 
22447877fdebSMatt Macy 	error = vdev_draid_lookup_map(children, &map);
22457877fdebSMatt Macy 	if (error)
22467877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22477877fdebSMatt Macy 
22487877fdebSMatt Macy 	vdc = kmem_zalloc(sizeof (*vdc), KM_SLEEP);
22497877fdebSMatt Macy 	vdc->vdc_ndata = ndata;
22507877fdebSMatt Macy 	vdc->vdc_nparity = nparity;
22517877fdebSMatt Macy 	vdc->vdc_nspares = nspares;
22527877fdebSMatt Macy 	vdc->vdc_children = children;
22537877fdebSMatt Macy 	vdc->vdc_ngroups = ngroups;
22547877fdebSMatt Macy 	vdc->vdc_nperms = map->dm_nperms;
22557877fdebSMatt Macy 
22567877fdebSMatt Macy 	error = vdev_draid_generate_perms(map, &vdc->vdc_perms);
22577877fdebSMatt Macy 	if (error) {
22587877fdebSMatt Macy 		kmem_free(vdc, sizeof (*vdc));
22597877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
22607877fdebSMatt Macy 	}
22617877fdebSMatt Macy 
22627877fdebSMatt Macy 	/*
22637877fdebSMatt Macy 	 * Derived constants.
22647877fdebSMatt Macy 	 */
22657877fdebSMatt Macy 	vdc->vdc_groupwidth = vdc->vdc_ndata + vdc->vdc_nparity;
22667877fdebSMatt Macy 	vdc->vdc_ndisks = vdc->vdc_children - vdc->vdc_nspares;
22677877fdebSMatt Macy 	vdc->vdc_groupsz = vdc->vdc_groupwidth * VDEV_DRAID_ROWHEIGHT;
22687877fdebSMatt Macy 	vdc->vdc_devslicesz = (vdc->vdc_groupsz * vdc->vdc_ngroups) /
22697877fdebSMatt Macy 	    vdc->vdc_ndisks;
22707877fdebSMatt Macy 
22717877fdebSMatt Macy 	ASSERT3U(vdc->vdc_groupwidth, >=, 2);
22727877fdebSMatt Macy 	ASSERT3U(vdc->vdc_groupwidth, <=, vdc->vdc_ndisks);
22737877fdebSMatt Macy 	ASSERT3U(vdc->vdc_groupsz, >=, 2 * VDEV_DRAID_ROWHEIGHT);
22747877fdebSMatt Macy 	ASSERT3U(vdc->vdc_devslicesz, >=, VDEV_DRAID_ROWHEIGHT);
22757877fdebSMatt Macy 	ASSERT3U(vdc->vdc_devslicesz % VDEV_DRAID_ROWHEIGHT, ==, 0);
22767877fdebSMatt Macy 	ASSERT3U((vdc->vdc_groupwidth * vdc->vdc_ngroups) %
22777877fdebSMatt Macy 	    vdc->vdc_ndisks, ==, 0);
22787877fdebSMatt Macy 
22797877fdebSMatt Macy 	*tsd = vdc;
22807877fdebSMatt Macy 
22817877fdebSMatt Macy 	return (0);
22827877fdebSMatt Macy }
22837877fdebSMatt Macy 
22847877fdebSMatt Macy static void
vdev_draid_fini(vdev_t * vd)22857877fdebSMatt Macy vdev_draid_fini(vdev_t *vd)
22867877fdebSMatt Macy {
22877877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
22887877fdebSMatt Macy 
22897877fdebSMatt Macy 	vmem_free(vdc->vdc_perms, sizeof (uint8_t) *
22907877fdebSMatt Macy 	    vdc->vdc_children * vdc->vdc_nperms);
22917877fdebSMatt Macy 	kmem_free(vdc, sizeof (*vdc));
22927877fdebSMatt Macy }
22937877fdebSMatt Macy 
22947877fdebSMatt Macy static uint64_t
vdev_draid_nparity(vdev_t * vd)22957877fdebSMatt Macy vdev_draid_nparity(vdev_t *vd)
22967877fdebSMatt Macy {
22977877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
22987877fdebSMatt Macy 
22997877fdebSMatt Macy 	return (vdc->vdc_nparity);
23007877fdebSMatt Macy }
23017877fdebSMatt Macy 
23027877fdebSMatt Macy static uint64_t
vdev_draid_ndisks(vdev_t * vd)23037877fdebSMatt Macy vdev_draid_ndisks(vdev_t *vd)
23047877fdebSMatt Macy {
23057877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
23067877fdebSMatt Macy 
23077877fdebSMatt Macy 	return (vdc->vdc_ndisks);
23087877fdebSMatt Macy }
23097877fdebSMatt Macy 
23107877fdebSMatt Macy vdev_ops_t vdev_draid_ops = {
23117877fdebSMatt Macy 	.vdev_op_init = vdev_draid_init,
23127877fdebSMatt Macy 	.vdev_op_fini = vdev_draid_fini,
23137877fdebSMatt Macy 	.vdev_op_open = vdev_draid_open,
23147877fdebSMatt Macy 	.vdev_op_close = vdev_draid_close,
2315*071ab5a1SMartin Matuska 	.vdev_op_psize_to_asize = vdev_draid_psize_to_asize,
2316*071ab5a1SMartin Matuska 	.vdev_op_asize_to_psize = vdev_draid_asize_to_psize,
23177877fdebSMatt Macy 	.vdev_op_min_asize = vdev_draid_min_asize,
23187877fdebSMatt Macy 	.vdev_op_min_alloc = vdev_draid_min_alloc,
23197877fdebSMatt Macy 	.vdev_op_io_start = vdev_draid_io_start,
23207877fdebSMatt Macy 	.vdev_op_io_done = vdev_draid_io_done,
23217877fdebSMatt Macy 	.vdev_op_state_change = vdev_draid_state_change,
23227877fdebSMatt Macy 	.vdev_op_need_resilver = vdev_draid_need_resilver,
23237877fdebSMatt Macy 	.vdev_op_hold = NULL,
23247877fdebSMatt Macy 	.vdev_op_rele = NULL,
23257877fdebSMatt Macy 	.vdev_op_remap = NULL,
23267877fdebSMatt Macy 	.vdev_op_xlate = vdev_draid_xlate,
23277877fdebSMatt Macy 	.vdev_op_rebuild_asize = vdev_draid_rebuild_asize,
23287877fdebSMatt Macy 	.vdev_op_metaslab_init = vdev_draid_metaslab_init,
23297877fdebSMatt Macy 	.vdev_op_config_generate = vdev_draid_config_generate,
23307877fdebSMatt Macy 	.vdev_op_nparity = vdev_draid_nparity,
23317877fdebSMatt Macy 	.vdev_op_ndisks = vdev_draid_ndisks,
23327877fdebSMatt Macy 	.vdev_op_type = VDEV_TYPE_DRAID,
23337877fdebSMatt Macy 	.vdev_op_leaf = B_FALSE,
23347877fdebSMatt Macy };
23357877fdebSMatt Macy 
23367877fdebSMatt Macy 
23377877fdebSMatt Macy /*
23387877fdebSMatt Macy  * A dRAID distributed spare is a virtual leaf vdev which is included in the
23397877fdebSMatt Macy  * parent dRAID configuration.  The last N columns of the dRAID permutation
23407877fdebSMatt Macy  * table are used to determine on which dRAID children a specific offset
23417877fdebSMatt Macy  * should be written.  These spare leaf vdevs can only be used to replace
23427877fdebSMatt Macy  * faulted children in the same dRAID configuration.
23437877fdebSMatt Macy  */
23447877fdebSMatt Macy 
23457877fdebSMatt Macy /*
23467877fdebSMatt Macy  * Distributed spare state.  All fields are set when the distributed spare is
23477877fdebSMatt Macy  * first opened and are immutable.
23487877fdebSMatt Macy  */
23497877fdebSMatt Macy typedef struct {
23507877fdebSMatt Macy 	vdev_t *vds_draid_vdev;		/* top-level parent dRAID vdev */
23517877fdebSMatt Macy 	uint64_t vds_top_guid;		/* top-level parent dRAID guid */
23527877fdebSMatt Macy 	uint64_t vds_spare_id;		/* spare id (0 - vdc->vdc_nspares-1) */
23537877fdebSMatt Macy } vdev_draid_spare_t;
23547877fdebSMatt Macy 
23557877fdebSMatt Macy /*
23567877fdebSMatt Macy  * Returns the parent dRAID vdev to which the distributed spare belongs.
23577877fdebSMatt Macy  * This may be safely called even when the vdev is not open.
23587877fdebSMatt Macy  */
23597877fdebSMatt Macy vdev_t *
vdev_draid_spare_get_parent(vdev_t * vd)23607877fdebSMatt Macy vdev_draid_spare_get_parent(vdev_t *vd)
23617877fdebSMatt Macy {
23627877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
23637877fdebSMatt Macy 
23647877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_spare_ops);
23657877fdebSMatt Macy 
23667877fdebSMatt Macy 	if (vds->vds_draid_vdev != NULL)
23677877fdebSMatt Macy 		return (vds->vds_draid_vdev);
23687877fdebSMatt Macy 
23697877fdebSMatt Macy 	return (vdev_lookup_by_guid(vd->vdev_spa->spa_root_vdev,
23707877fdebSMatt Macy 	    vds->vds_top_guid));
23717877fdebSMatt Macy }
23727877fdebSMatt Macy 
23737877fdebSMatt Macy /*
23747877fdebSMatt Macy  * A dRAID space is active when it's the child of a vdev using the
23757877fdebSMatt Macy  * vdev_spare_ops, vdev_replacing_ops or vdev_draid_ops.
23767877fdebSMatt Macy  */
23777877fdebSMatt Macy static boolean_t
vdev_draid_spare_is_active(vdev_t * vd)23787877fdebSMatt Macy vdev_draid_spare_is_active(vdev_t *vd)
23797877fdebSMatt Macy {
23807877fdebSMatt Macy 	vdev_t *pvd = vd->vdev_parent;
23817877fdebSMatt Macy 
23827877fdebSMatt Macy 	if (pvd != NULL && (pvd->vdev_ops == &vdev_spare_ops ||
23837877fdebSMatt Macy 	    pvd->vdev_ops == &vdev_replacing_ops ||
23847877fdebSMatt Macy 	    pvd->vdev_ops == &vdev_draid_ops)) {
23857877fdebSMatt Macy 		return (B_TRUE);
23867877fdebSMatt Macy 	} else {
23877877fdebSMatt Macy 		return (B_FALSE);
23887877fdebSMatt Macy 	}
23897877fdebSMatt Macy }
23907877fdebSMatt Macy 
23917877fdebSMatt Macy /*
23927877fdebSMatt Macy  * Given a dRAID distribute spare vdev, returns the physical child vdev
23937877fdebSMatt Macy  * on which the provided offset resides.  This may involve recursing through
23947877fdebSMatt Macy  * multiple layers of distributed spares.  Note that offset is relative to
23957877fdebSMatt Macy  * this vdev.
23967877fdebSMatt Macy  */
23977877fdebSMatt Macy vdev_t *
vdev_draid_spare_get_child(vdev_t * vd,uint64_t physical_offset)23987877fdebSMatt Macy vdev_draid_spare_get_child(vdev_t *vd, uint64_t physical_offset)
23997877fdebSMatt Macy {
24007877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
24017877fdebSMatt Macy 
24027877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_spare_ops);
24037877fdebSMatt Macy 
24047877fdebSMatt Macy 	/* The vdev is closed */
24057877fdebSMatt Macy 	if (vds->vds_draid_vdev == NULL)
24067877fdebSMatt Macy 		return (NULL);
24077877fdebSMatt Macy 
24087877fdebSMatt Macy 	vdev_t *tvd = vds->vds_draid_vdev;
24097877fdebSMatt Macy 	vdev_draid_config_t *vdc = tvd->vdev_tsd;
24107877fdebSMatt Macy 
24117877fdebSMatt Macy 	ASSERT3P(tvd->vdev_ops, ==, &vdev_draid_ops);
24127877fdebSMatt Macy 	ASSERT3U(vds->vds_spare_id, <, vdc->vdc_nspares);
24137877fdebSMatt Macy 
24147877fdebSMatt Macy 	uint8_t *base;
24157877fdebSMatt Macy 	uint64_t iter;
24167877fdebSMatt Macy 	uint64_t perm = physical_offset / vdc->vdc_devslicesz;
24177877fdebSMatt Macy 
24187877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
24197877fdebSMatt Macy 
24207877fdebSMatt Macy 	uint64_t cid = vdev_draid_permute_id(vdc, base, iter,
24217877fdebSMatt Macy 	    (tvd->vdev_children - 1) - vds->vds_spare_id);
24227877fdebSMatt Macy 	vdev_t *cvd = tvd->vdev_child[cid];
24237877fdebSMatt Macy 
24247877fdebSMatt Macy 	if (cvd->vdev_ops == &vdev_draid_spare_ops)
24257877fdebSMatt Macy 		return (vdev_draid_spare_get_child(cvd, physical_offset));
24267877fdebSMatt Macy 
24277877fdebSMatt Macy 	return (cvd);
24287877fdebSMatt Macy }
24297877fdebSMatt Macy 
24307877fdebSMatt Macy static void
vdev_draid_spare_close(vdev_t * vd)24317877fdebSMatt Macy vdev_draid_spare_close(vdev_t *vd)
24327877fdebSMatt Macy {
24337877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
24347877fdebSMatt Macy 	vds->vds_draid_vdev = NULL;
24357877fdebSMatt Macy }
24367877fdebSMatt Macy 
24377877fdebSMatt Macy /*
24387877fdebSMatt Macy  * Opening a dRAID spare device is done by looking up the associated dRAID
24397877fdebSMatt Macy  * top-level vdev guid from the spare configuration.
24407877fdebSMatt Macy  */
24417877fdebSMatt Macy static int
vdev_draid_spare_open(vdev_t * vd,uint64_t * psize,uint64_t * max_psize,uint64_t * logical_ashift,uint64_t * physical_ashift)24427877fdebSMatt Macy vdev_draid_spare_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
24437877fdebSMatt Macy     uint64_t *logical_ashift, uint64_t *physical_ashift)
24447877fdebSMatt Macy {
24457877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
24467877fdebSMatt Macy 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
24477877fdebSMatt Macy 	uint64_t asize, max_asize;
24487877fdebSMatt Macy 
24497877fdebSMatt Macy 	vdev_t *tvd = vdev_lookup_by_guid(rvd, vds->vds_top_guid);
24507877fdebSMatt Macy 	if (tvd == NULL) {
24517877fdebSMatt Macy 		/*
24527877fdebSMatt Macy 		 * When spa_vdev_add() is labeling new spares the
24537877fdebSMatt Macy 		 * associated dRAID is not attached to the root vdev
24547877fdebSMatt Macy 		 * nor does this spare have a parent.  Simulate a valid
24557877fdebSMatt Macy 		 * device in order to allow the label to be initialized
24567877fdebSMatt Macy 		 * and the distributed spare added to the configuration.
24577877fdebSMatt Macy 		 */
24587877fdebSMatt Macy 		if (vd->vdev_parent == NULL) {
24597877fdebSMatt Macy 			*psize = *max_psize = SPA_MINDEVSIZE;
24607877fdebSMatt Macy 			*logical_ashift = *physical_ashift = ASHIFT_MIN;
24617877fdebSMatt Macy 			return (0);
24627877fdebSMatt Macy 		}
24637877fdebSMatt Macy 
24647877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
24657877fdebSMatt Macy 	}
24667877fdebSMatt Macy 
24677877fdebSMatt Macy 	vdev_draid_config_t *vdc = tvd->vdev_tsd;
24687877fdebSMatt Macy 	if (tvd->vdev_ops != &vdev_draid_ops || vdc == NULL)
24697877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
24707877fdebSMatt Macy 
24717877fdebSMatt Macy 	if (vds->vds_spare_id >= vdc->vdc_nspares)
24727877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
24737877fdebSMatt Macy 
24747877fdebSMatt Macy 	/*
24757877fdebSMatt Macy 	 * Neither tvd->vdev_asize or tvd->vdev_max_asize can be used here
24767877fdebSMatt Macy 	 * because the caller may be vdev_draid_open() in which case the
24777877fdebSMatt Macy 	 * values are stale as they haven't yet been updated by vdev_open().
24787877fdebSMatt Macy 	 * To avoid this always recalculate the dRAID asize and max_asize.
24797877fdebSMatt Macy 	 */
24807877fdebSMatt Macy 	vdev_draid_calculate_asize(tvd, &asize, &max_asize,
24817877fdebSMatt Macy 	    logical_ashift, physical_ashift);
24827877fdebSMatt Macy 
24837877fdebSMatt Macy 	*psize = asize + VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
24847877fdebSMatt Macy 	*max_psize = max_asize + VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
24857877fdebSMatt Macy 
24867877fdebSMatt Macy 	vds->vds_draid_vdev = tvd;
24877877fdebSMatt Macy 
24887877fdebSMatt Macy 	return (0);
24897877fdebSMatt Macy }
24907877fdebSMatt Macy 
24917877fdebSMatt Macy /*
24927877fdebSMatt Macy  * Completed distributed spare IO.  Store the result in the parent zio
24937877fdebSMatt Macy  * as if it had performed the operation itself.  Only the first error is
24947877fdebSMatt Macy  * preserved if there are multiple errors.
24957877fdebSMatt Macy  */
24967877fdebSMatt Macy static void
vdev_draid_spare_child_done(zio_t * zio)24977877fdebSMatt Macy vdev_draid_spare_child_done(zio_t *zio)
24987877fdebSMatt Macy {
24997877fdebSMatt Macy 	zio_t *pio = zio->io_private;
25007877fdebSMatt Macy 
25017877fdebSMatt Macy 	/*
25027877fdebSMatt Macy 	 * IOs are issued to non-writable vdevs in order to keep their
25037877fdebSMatt Macy 	 * DTLs accurate.  However, we don't want to propagate the
25047877fdebSMatt Macy 	 * error in to the distributed spare's DTL.  When resilvering
25057877fdebSMatt Macy 	 * vdev_draid_need_resilver() will consult the relevant DTL
25067877fdebSMatt Macy 	 * to determine if the data is missing and must be repaired.
25077877fdebSMatt Macy 	 */
25087877fdebSMatt Macy 	if (!vdev_writeable(zio->io_vd))
25097877fdebSMatt Macy 		return;
25107877fdebSMatt Macy 
25117877fdebSMatt Macy 	if (pio->io_error == 0)
25127877fdebSMatt Macy 		pio->io_error = zio->io_error;
25137877fdebSMatt Macy }
25147877fdebSMatt Macy 
25157877fdebSMatt Macy /*
25167877fdebSMatt Macy  * Returns a valid label nvlist for the distributed spare vdev.  This is
25177877fdebSMatt Macy  * used to bypass the IO pipeline to avoid the complexity of constructing
25187877fdebSMatt Macy  * a complete label with valid checksum to return when read.
25197877fdebSMatt Macy  */
25207877fdebSMatt Macy nvlist_t *
vdev_draid_read_config_spare(vdev_t * vd)25217877fdebSMatt Macy vdev_draid_read_config_spare(vdev_t *vd)
25227877fdebSMatt Macy {
25237877fdebSMatt Macy 	spa_t *spa = vd->vdev_spa;
25247877fdebSMatt Macy 	spa_aux_vdev_t *sav = &spa->spa_spares;
25257877fdebSMatt Macy 	uint64_t guid = vd->vdev_guid;
25267877fdebSMatt Macy 
25277877fdebSMatt Macy 	nvlist_t *nv = fnvlist_alloc();
25287877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
25297877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
25307877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_VERSION, spa_version(spa));
25317877fdebSMatt Macy 	fnvlist_add_string(nv, ZPOOL_CONFIG_POOL_NAME, spa_name(spa));
25327877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_POOL_GUID, spa_guid(spa));
25337877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg);
25347877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_TOP_GUID, vd->vdev_top->vdev_guid);
25357877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_POOL_STATE,
25367877fdebSMatt Macy 	    vdev_draid_spare_is_active(vd) ?
25377877fdebSMatt Macy 	    POOL_STATE_ACTIVE : POOL_STATE_SPARE);
25387877fdebSMatt Macy 
25397877fdebSMatt Macy 	/* Set the vdev guid based on the vdev list in sav_count. */
25407877fdebSMatt Macy 	for (int i = 0; i < sav->sav_count; i++) {
25417877fdebSMatt Macy 		if (sav->sav_vdevs[i]->vdev_ops == &vdev_draid_spare_ops &&
25427877fdebSMatt Macy 		    strcmp(sav->sav_vdevs[i]->vdev_path, vd->vdev_path) == 0) {
25437877fdebSMatt Macy 			guid = sav->sav_vdevs[i]->vdev_guid;
25447877fdebSMatt Macy 			break;
25457877fdebSMatt Macy 		}
25467877fdebSMatt Macy 	}
25477877fdebSMatt Macy 
25487877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, guid);
25497877fdebSMatt Macy 
25507877fdebSMatt Macy 	return (nv);
25517877fdebSMatt Macy }
25527877fdebSMatt Macy 
25537877fdebSMatt Macy /*
25541719886fSMartin Matuska  * Handle any flush requested of the distributed spare. All children must be
25551719886fSMartin Matuska  * flushed.
25567877fdebSMatt Macy  */
25577877fdebSMatt Macy static int
vdev_draid_spare_flush(zio_t * zio)25581719886fSMartin Matuska vdev_draid_spare_flush(zio_t *zio)
25597877fdebSMatt Macy {
25607877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
25617877fdebSMatt Macy 	int error = 0;
25627877fdebSMatt Macy 
25637877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
25647877fdebSMatt Macy 		zio_nowait(zio_vdev_child_io(zio, NULL,
25657877fdebSMatt Macy 		    vd->vdev_child[c], zio->io_offset, zio->io_abd,
25667877fdebSMatt Macy 		    zio->io_size, zio->io_type, zio->io_priority, 0,
25677877fdebSMatt Macy 		    vdev_draid_spare_child_done, zio));
25687877fdebSMatt Macy 	}
25697877fdebSMatt Macy 
25707877fdebSMatt Macy 	return (error);
25717877fdebSMatt Macy }
25727877fdebSMatt Macy 
25737877fdebSMatt Macy /*
25747877fdebSMatt Macy  * Initiate an IO to the distributed spare.  For normal IOs this entails using
25757877fdebSMatt Macy  * the zio->io_offset and permutation table to calculate which child dRAID vdev
25767877fdebSMatt Macy  * is responsible for the data.  Then passing along the zio to that child to
25777877fdebSMatt Macy  * perform the actual IO.  The label ranges are not stored on disk and require
25787877fdebSMatt Macy  * some special handling which is described below.
25797877fdebSMatt Macy  */
25807877fdebSMatt Macy static void
vdev_draid_spare_io_start(zio_t * zio)25817877fdebSMatt Macy vdev_draid_spare_io_start(zio_t *zio)
25827877fdebSMatt Macy {
25837877fdebSMatt Macy 	vdev_t *cvd = NULL, *vd = zio->io_vd;
25847877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
25857877fdebSMatt Macy 	uint64_t offset = zio->io_offset - VDEV_LABEL_START_SIZE;
25867877fdebSMatt Macy 
25877877fdebSMatt Macy 	/*
25887877fdebSMatt Macy 	 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
25897877fdebSMatt Macy 	 * Nothing to be done here but return failure.
25907877fdebSMatt Macy 	 */
25917877fdebSMatt Macy 	if (vds == NULL) {
25927877fdebSMatt Macy 		zio->io_error = ENXIO;
25937877fdebSMatt Macy 		zio_interrupt(zio);
25947877fdebSMatt Macy 		return;
25957877fdebSMatt Macy 	}
25967877fdebSMatt Macy 
25977877fdebSMatt Macy 	switch (zio->io_type) {
25981719886fSMartin Matuska 	case ZIO_TYPE_FLUSH:
25991719886fSMartin Matuska 		zio->io_error = vdev_draid_spare_flush(zio);
26007877fdebSMatt Macy 		break;
26017877fdebSMatt Macy 
26027877fdebSMatt Macy 	case ZIO_TYPE_WRITE:
26037877fdebSMatt Macy 		if (VDEV_OFFSET_IS_LABEL(vd, zio->io_offset)) {
26047877fdebSMatt Macy 			/*
26057877fdebSMatt Macy 			 * Accept probe IOs and config writers to simulate the
26067877fdebSMatt Macy 			 * existence of an on disk label.  vdev_label_sync(),
26077877fdebSMatt Macy 			 * vdev_uberblock_sync() and vdev_copy_uberblocks()
26087877fdebSMatt Macy 			 * skip the distributed spares.  This only leaves
26097877fdebSMatt Macy 			 * vdev_label_init() which is allowed to succeed to
26107877fdebSMatt Macy 			 * avoid adding special cases the function.
26117877fdebSMatt Macy 			 */
26127877fdebSMatt Macy 			if (zio->io_flags & ZIO_FLAG_PROBE ||
26137877fdebSMatt Macy 			    zio->io_flags & ZIO_FLAG_CONFIG_WRITER) {
26147877fdebSMatt Macy 				zio->io_error = 0;
26157877fdebSMatt Macy 			} else {
26167877fdebSMatt Macy 				zio->io_error = SET_ERROR(EIO);
26177877fdebSMatt Macy 			}
26187877fdebSMatt Macy 		} else {
26197877fdebSMatt Macy 			cvd = vdev_draid_spare_get_child(vd, offset);
26207877fdebSMatt Macy 
26217877fdebSMatt Macy 			if (cvd == NULL) {
26227877fdebSMatt Macy 				zio->io_error = SET_ERROR(ENXIO);
26237877fdebSMatt Macy 			} else {
26247877fdebSMatt Macy 				zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
26257877fdebSMatt Macy 				    offset, zio->io_abd, zio->io_size,
26267877fdebSMatt Macy 				    zio->io_type, zio->io_priority, 0,
26277877fdebSMatt Macy 				    vdev_draid_spare_child_done, zio));
26287877fdebSMatt Macy 			}
26297877fdebSMatt Macy 		}
26307877fdebSMatt Macy 		break;
26317877fdebSMatt Macy 
26327877fdebSMatt Macy 	case ZIO_TYPE_READ:
26337877fdebSMatt Macy 		if (VDEV_OFFSET_IS_LABEL(vd, zio->io_offset)) {
26347877fdebSMatt Macy 			/*
26357877fdebSMatt Macy 			 * Accept probe IOs to simulate the existence of a
26367877fdebSMatt Macy 			 * label.  vdev_label_read_config() bypasses the
26377877fdebSMatt Macy 			 * pipeline to read the label configuration and
26387877fdebSMatt Macy 			 * vdev_uberblock_load() skips distributed spares
26397877fdebSMatt Macy 			 * when attempting to locate the best uberblock.
26407877fdebSMatt Macy 			 */
26417877fdebSMatt Macy 			if (zio->io_flags & ZIO_FLAG_PROBE) {
26427877fdebSMatt Macy 				zio->io_error = 0;
26437877fdebSMatt Macy 			} else {
26447877fdebSMatt Macy 				zio->io_error = SET_ERROR(EIO);
26457877fdebSMatt Macy 			}
26467877fdebSMatt Macy 		} else {
26477877fdebSMatt Macy 			cvd = vdev_draid_spare_get_child(vd, offset);
26487877fdebSMatt Macy 
26497877fdebSMatt Macy 			if (cvd == NULL || !vdev_readable(cvd)) {
26507877fdebSMatt Macy 				zio->io_error = SET_ERROR(ENXIO);
26517877fdebSMatt Macy 			} else {
26527877fdebSMatt Macy 				zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
26537877fdebSMatt Macy 				    offset, zio->io_abd, zio->io_size,
26547877fdebSMatt Macy 				    zio->io_type, zio->io_priority, 0,
26557877fdebSMatt Macy 				    vdev_draid_spare_child_done, zio));
26567877fdebSMatt Macy 			}
26577877fdebSMatt Macy 		}
26587877fdebSMatt Macy 		break;
26597877fdebSMatt Macy 
26607877fdebSMatt Macy 	case ZIO_TYPE_TRIM:
26617877fdebSMatt Macy 		/* The vdev label ranges are never trimmed */
26627877fdebSMatt Macy 		ASSERT0(VDEV_OFFSET_IS_LABEL(vd, zio->io_offset));
26637877fdebSMatt Macy 
26647877fdebSMatt Macy 		cvd = vdev_draid_spare_get_child(vd, offset);
26657877fdebSMatt Macy 
26667877fdebSMatt Macy 		if (cvd == NULL || !cvd->vdev_has_trim) {
26677877fdebSMatt Macy 			zio->io_error = SET_ERROR(ENXIO);
26687877fdebSMatt Macy 		} else {
26697877fdebSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
26707877fdebSMatt Macy 			    offset, zio->io_abd, zio->io_size,
26717877fdebSMatt Macy 			    zio->io_type, zio->io_priority, 0,
26727877fdebSMatt Macy 			    vdev_draid_spare_child_done, zio));
26737877fdebSMatt Macy 		}
26747877fdebSMatt Macy 		break;
26757877fdebSMatt Macy 
26767877fdebSMatt Macy 	default:
26777877fdebSMatt Macy 		zio->io_error = SET_ERROR(ENOTSUP);
26787877fdebSMatt Macy 		break;
26797877fdebSMatt Macy 	}
26807877fdebSMatt Macy 
26817877fdebSMatt Macy 	zio_execute(zio);
26827877fdebSMatt Macy }
26837877fdebSMatt Macy 
26847877fdebSMatt Macy static void
vdev_draid_spare_io_done(zio_t * zio)26857877fdebSMatt Macy vdev_draid_spare_io_done(zio_t *zio)
26867877fdebSMatt Macy {
2687e92ffd9bSMartin Matuska 	(void) zio;
26887877fdebSMatt Macy }
26897877fdebSMatt Macy 
26907877fdebSMatt Macy /*
26917877fdebSMatt Macy  * Lookup the full spare config in spa->spa_spares.sav_config and
26927877fdebSMatt Macy  * return the top_guid and spare_id for the named spare.
26937877fdebSMatt Macy  */
26947877fdebSMatt Macy static int
vdev_draid_spare_lookup(spa_t * spa,nvlist_t * nv,uint64_t * top_guidp,uint64_t * spare_idp)26957877fdebSMatt Macy vdev_draid_spare_lookup(spa_t *spa, nvlist_t *nv, uint64_t *top_guidp,
26967877fdebSMatt Macy     uint64_t *spare_idp)
26977877fdebSMatt Macy {
26987877fdebSMatt Macy 	nvlist_t **spares;
26997877fdebSMatt Macy 	uint_t nspares;
27007877fdebSMatt Macy 	int error;
27017877fdebSMatt Macy 
27027877fdebSMatt Macy 	if ((spa->spa_spares.sav_config == NULL) ||
27037877fdebSMatt Macy 	    (nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
27047877fdebSMatt Macy 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0)) {
27057877fdebSMatt Macy 		return (SET_ERROR(ENOENT));
27067877fdebSMatt Macy 	}
27077877fdebSMatt Macy 
27082a58b312SMartin Matuska 	const char *spare_name;
27097877fdebSMatt Macy 	error = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &spare_name);
27107877fdebSMatt Macy 	if (error != 0)
27117877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
27127877fdebSMatt Macy 
27137877fdebSMatt Macy 	for (int i = 0; i < nspares; i++) {
27147877fdebSMatt Macy 		nvlist_t *spare = spares[i];
27157877fdebSMatt Macy 		uint64_t top_guid, spare_id;
27162a58b312SMartin Matuska 		const char *type, *path;
27177877fdebSMatt Macy 
27187877fdebSMatt Macy 		/* Skip non-distributed spares */
27197877fdebSMatt Macy 		error = nvlist_lookup_string(spare, ZPOOL_CONFIG_TYPE, &type);
27207877fdebSMatt Macy 		if (error != 0 || strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0)
27217877fdebSMatt Macy 			continue;
27227877fdebSMatt Macy 
27237877fdebSMatt Macy 		/* Skip spares with the wrong name */
27247877fdebSMatt Macy 		error = nvlist_lookup_string(spare, ZPOOL_CONFIG_PATH, &path);
27257877fdebSMatt Macy 		if (error != 0 || strcmp(path, spare_name) != 0)
27267877fdebSMatt Macy 			continue;
27277877fdebSMatt Macy 
27287877fdebSMatt Macy 		/* Found the matching spare */
27297877fdebSMatt Macy 		error = nvlist_lookup_uint64(spare,
27307877fdebSMatt Macy 		    ZPOOL_CONFIG_TOP_GUID, &top_guid);
27317877fdebSMatt Macy 		if (error == 0) {
27327877fdebSMatt Macy 			error = nvlist_lookup_uint64(spare,
27337877fdebSMatt Macy 			    ZPOOL_CONFIG_SPARE_ID, &spare_id);
27347877fdebSMatt Macy 		}
27357877fdebSMatt Macy 
27367877fdebSMatt Macy 		if (error != 0) {
27377877fdebSMatt Macy 			return (SET_ERROR(EINVAL));
27387877fdebSMatt Macy 		} else {
27397877fdebSMatt Macy 			*top_guidp = top_guid;
27407877fdebSMatt Macy 			*spare_idp = spare_id;
27417877fdebSMatt Macy 			return (0);
27427877fdebSMatt Macy 		}
27437877fdebSMatt Macy 	}
27447877fdebSMatt Macy 
27457877fdebSMatt Macy 	return (SET_ERROR(ENOENT));
27467877fdebSMatt Macy }
27477877fdebSMatt Macy 
27487877fdebSMatt Macy /*
27497877fdebSMatt Macy  * Initialize private dRAID spare specific fields from the nvlist.
27507877fdebSMatt Macy  */
27517877fdebSMatt Macy static int
vdev_draid_spare_init(spa_t * spa,nvlist_t * nv,void ** tsd)27527877fdebSMatt Macy vdev_draid_spare_init(spa_t *spa, nvlist_t *nv, void **tsd)
27537877fdebSMatt Macy {
27547877fdebSMatt Macy 	vdev_draid_spare_t *vds;
27557877fdebSMatt Macy 	uint64_t top_guid = 0;
27567877fdebSMatt Macy 	uint64_t spare_id;
27577877fdebSMatt Macy 
27587877fdebSMatt Macy 	/*
27597877fdebSMatt Macy 	 * In the normal case check the list of spares stored in the spa
27607877fdebSMatt Macy 	 * to lookup the top_guid and spare_id for provided spare config.
27617877fdebSMatt Macy 	 * When creating a new pool or adding vdevs the spare list is not
27627877fdebSMatt Macy 	 * yet populated and the values are provided in the passed config.
27637877fdebSMatt Macy 	 */
27647877fdebSMatt Macy 	if (vdev_draid_spare_lookup(spa, nv, &top_guid, &spare_id) != 0) {
27657877fdebSMatt Macy 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_TOP_GUID,
27667877fdebSMatt Macy 		    &top_guid) != 0)
27677877fdebSMatt Macy 			return (SET_ERROR(EINVAL));
27687877fdebSMatt Macy 
27697877fdebSMatt Macy 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_SPARE_ID,
27707877fdebSMatt Macy 		    &spare_id) != 0)
27717877fdebSMatt Macy 			return (SET_ERROR(EINVAL));
27727877fdebSMatt Macy 	}
27737877fdebSMatt Macy 
27747877fdebSMatt Macy 	vds = kmem_alloc(sizeof (vdev_draid_spare_t), KM_SLEEP);
27757877fdebSMatt Macy 	vds->vds_draid_vdev = NULL;
27767877fdebSMatt Macy 	vds->vds_top_guid = top_guid;
27777877fdebSMatt Macy 	vds->vds_spare_id = spare_id;
27787877fdebSMatt Macy 
27797877fdebSMatt Macy 	*tsd = vds;
27807877fdebSMatt Macy 
27817877fdebSMatt Macy 	return (0);
27827877fdebSMatt Macy }
27837877fdebSMatt Macy 
27847877fdebSMatt Macy static void
vdev_draid_spare_fini(vdev_t * vd)27857877fdebSMatt Macy vdev_draid_spare_fini(vdev_t *vd)
27867877fdebSMatt Macy {
27877877fdebSMatt Macy 	kmem_free(vd->vdev_tsd, sizeof (vdev_draid_spare_t));
27887877fdebSMatt Macy }
27897877fdebSMatt Macy 
27907877fdebSMatt Macy static void
vdev_draid_spare_config_generate(vdev_t * vd,nvlist_t * nv)27917877fdebSMatt Macy vdev_draid_spare_config_generate(vdev_t *vd, nvlist_t *nv)
27927877fdebSMatt Macy {
27937877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
27947877fdebSMatt Macy 
27957877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_spare_ops);
27967877fdebSMatt Macy 
27977877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_TOP_GUID, vds->vds_top_guid);
27987877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_SPARE_ID, vds->vds_spare_id);
27997877fdebSMatt Macy }
28007877fdebSMatt Macy 
28017877fdebSMatt Macy vdev_ops_t vdev_draid_spare_ops = {
28027877fdebSMatt Macy 	.vdev_op_init = vdev_draid_spare_init,
28037877fdebSMatt Macy 	.vdev_op_fini = vdev_draid_spare_fini,
28047877fdebSMatt Macy 	.vdev_op_open = vdev_draid_spare_open,
28057877fdebSMatt Macy 	.vdev_op_close = vdev_draid_spare_close,
2806*071ab5a1SMartin Matuska 	.vdev_op_psize_to_asize = vdev_default_asize,
2807*071ab5a1SMartin Matuska 	.vdev_op_asize_to_psize = vdev_default_psize,
28087877fdebSMatt Macy 	.vdev_op_min_asize = vdev_default_min_asize,
28097877fdebSMatt Macy 	.vdev_op_min_alloc = NULL,
28107877fdebSMatt Macy 	.vdev_op_io_start = vdev_draid_spare_io_start,
28117877fdebSMatt Macy 	.vdev_op_io_done = vdev_draid_spare_io_done,
28127877fdebSMatt Macy 	.vdev_op_state_change = NULL,
28137877fdebSMatt Macy 	.vdev_op_need_resilver = NULL,
28147877fdebSMatt Macy 	.vdev_op_hold = NULL,
28157877fdebSMatt Macy 	.vdev_op_rele = NULL,
28167877fdebSMatt Macy 	.vdev_op_remap = NULL,
28177877fdebSMatt Macy 	.vdev_op_xlate = vdev_default_xlate,
28187877fdebSMatt Macy 	.vdev_op_rebuild_asize = NULL,
28197877fdebSMatt Macy 	.vdev_op_metaslab_init = NULL,
28207877fdebSMatt Macy 	.vdev_op_config_generate = vdev_draid_spare_config_generate,
28217877fdebSMatt Macy 	.vdev_op_nparity = NULL,
28227877fdebSMatt Macy 	.vdev_op_ndisks = NULL,
28237877fdebSMatt Macy 	.vdev_op_type = VDEV_TYPE_DRAID_SPARE,
28247877fdebSMatt Macy 	.vdev_op_leaf = B_TRUE,
28257877fdebSMatt Macy };
2826