xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_draid.c (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
1*7877fdebSMatt Macy /*
2*7877fdebSMatt Macy  * CDDL HEADER START
3*7877fdebSMatt Macy  *
4*7877fdebSMatt Macy  * The contents of this file are subject to the terms of the
5*7877fdebSMatt Macy  * Common Development and Distribution License (the "License").
6*7877fdebSMatt Macy  * You may not use this file except in compliance with the License.
7*7877fdebSMatt Macy  *
8*7877fdebSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7877fdebSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10*7877fdebSMatt Macy  * See the License for the specific language governing permissions
11*7877fdebSMatt Macy  * and limitations under the License.
12*7877fdebSMatt Macy  *
13*7877fdebSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14*7877fdebSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7877fdebSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16*7877fdebSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17*7877fdebSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7877fdebSMatt Macy  *
19*7877fdebSMatt Macy  * CDDL HEADER END
20*7877fdebSMatt Macy  */
21*7877fdebSMatt Macy /*
22*7877fdebSMatt Macy  * Copyright (c) 2018 Intel Corporation.
23*7877fdebSMatt Macy  * Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
24*7877fdebSMatt Macy  */
25*7877fdebSMatt Macy 
26*7877fdebSMatt Macy #include <sys/zfs_context.h>
27*7877fdebSMatt Macy #include <sys/spa.h>
28*7877fdebSMatt Macy #include <sys/spa_impl.h>
29*7877fdebSMatt Macy #include <sys/vdev_impl.h>
30*7877fdebSMatt Macy #include <sys/vdev_draid.h>
31*7877fdebSMatt Macy #include <sys/vdev_raidz.h>
32*7877fdebSMatt Macy #include <sys/vdev_rebuild.h>
33*7877fdebSMatt Macy #include <sys/abd.h>
34*7877fdebSMatt Macy #include <sys/zio.h>
35*7877fdebSMatt Macy #include <sys/nvpair.h>
36*7877fdebSMatt Macy #include <sys/zio_checksum.h>
37*7877fdebSMatt Macy #include <sys/fs/zfs.h>
38*7877fdebSMatt Macy #include <sys/fm/fs/zfs.h>
39*7877fdebSMatt Macy #include <zfs_fletcher.h>
40*7877fdebSMatt Macy 
41*7877fdebSMatt Macy #ifdef ZFS_DEBUG
42*7877fdebSMatt Macy #include <sys/vdev.h>	/* For vdev_xlate() in vdev_draid_io_verify() */
43*7877fdebSMatt Macy #endif
44*7877fdebSMatt Macy 
45*7877fdebSMatt Macy /*
46*7877fdebSMatt Macy  * dRAID is a distributed spare implementation for ZFS. A dRAID vdev is
47*7877fdebSMatt Macy  * comprised of multiple raidz redundancy groups which are spread over the
48*7877fdebSMatt Macy  * dRAID children. To ensure an even distribution, and avoid hot spots, a
49*7877fdebSMatt Macy  * permutation mapping is applied to the order of the dRAID children.
50*7877fdebSMatt Macy  * This mixing effectively distributes the parity columns evenly over all
51*7877fdebSMatt Macy  * of the disks in the dRAID.
52*7877fdebSMatt Macy  *
53*7877fdebSMatt Macy  * This is beneficial because it means when resilvering all of the disks
54*7877fdebSMatt Macy  * can participate thereby increasing the available IOPs and bandwidth.
55*7877fdebSMatt Macy  * Furthermore, by reserving a small fraction of each child's total capacity
56*7877fdebSMatt Macy  * virtual distributed spare disks can be created. These spares similarly
57*7877fdebSMatt Macy  * benefit from the performance gains of spanning all of the children. The
58*7877fdebSMatt Macy  * consequence of which is that resilvering to a distributed spare can
59*7877fdebSMatt Macy  * substantially reduce the time required to restore full parity to pool
60*7877fdebSMatt Macy  * with a failed disks.
61*7877fdebSMatt Macy  *
62*7877fdebSMatt Macy  * === dRAID group layout ===
63*7877fdebSMatt Macy  *
64*7877fdebSMatt Macy  * First, let's define a "row" in the configuration to be a 16M chunk from
65*7877fdebSMatt Macy  * each physical drive at the same offset. This is the minimum allowable
66*7877fdebSMatt Macy  * size since it must be possible to store a full 16M block when there is
67*7877fdebSMatt Macy  * only a single data column. Next, we define a "group" to be a set of
68*7877fdebSMatt Macy  * sequential disks containing both the parity and data columns. We allow
69*7877fdebSMatt Macy  * groups to span multiple rows in order to align any group size to any
70*7877fdebSMatt Macy  * number of physical drives. Finally, a "slice" is comprised of the rows
71*7877fdebSMatt Macy  * which contain the target number of groups. The permutation mappings
72*7877fdebSMatt Macy  * are applied in a round robin fashion to each slice.
73*7877fdebSMatt Macy  *
74*7877fdebSMatt Macy  * Given D+P drives in a group (including parity drives) and C-S physical
75*7877fdebSMatt Macy  * drives (not including the spare drives), we can distribute the groups
76*7877fdebSMatt Macy  * across R rows without remainder by selecting the least common multiple
77*7877fdebSMatt Macy  * of D+P and C-S as the number of groups; i.e. ngroups = LCM(D+P, C-S).
78*7877fdebSMatt Macy  *
79*7877fdebSMatt Macy  * In the example below, there are C=14 physical drives in the configuration
80*7877fdebSMatt Macy  * with S=2 drives worth of spare capacity. Each group has a width of 9
81*7877fdebSMatt Macy  * which includes D=8 data and P=1 parity drive. There are 4 groups and
82*7877fdebSMatt Macy  * 3 rows per slice.  Each group has a size of 144M (16M * 9) and a slice
83*7877fdebSMatt Macy  * size is 576M (144M * 4). When allocating from a dRAID each group is
84*7877fdebSMatt Macy  * filled before moving on to the next as show in slice0 below.
85*7877fdebSMatt Macy  *
86*7877fdebSMatt Macy  *             data disks (8 data + 1 parity)          spares (2)
87*7877fdebSMatt Macy  *     +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
88*7877fdebSMatt Macy  *  ^  | 2 | 6 | 1 | 11| 4 | 0 | 7 | 10| 8 | 9 | 13| 5 | 12| 3 | device map 0
89*7877fdebSMatt Macy  *  |  +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
90*7877fdebSMatt Macy  *  |  |              group 0              |  group 1..|       |
91*7877fdebSMatt Macy  *  |  +-----------------------------------+-----------+-------|
92*7877fdebSMatt Macy  *  |  | 0   1   2   3   4   5   6   7   8 | 36  37  38|       |  r
93*7877fdebSMatt Macy  *  |  | 9   10  11  12  13  14  15  16  17| 45  46  47|       |  o
94*7877fdebSMatt Macy  *  |  | 18  19  20  21  22  23  24  25  26| 54  55  56|       |  w
95*7877fdebSMatt Macy  *     | 27  28  29  30  31  32  33  34  35| 63  64  65|       |  0
96*7877fdebSMatt Macy  *  s  +-----------------------+-----------------------+-------+
97*7877fdebSMatt Macy  *  l  |       ..group 1       |        group 2..      |       |
98*7877fdebSMatt Macy  *  i  +-----------------------+-----------------------+-------+
99*7877fdebSMatt Macy  *  c  | 39  40  41  42  43  44| 72  73  74  75  76  77|       |  r
100*7877fdebSMatt Macy  *  e  | 48  49  50  51  52  53| 81  82  83  84  85  86|       |  o
101*7877fdebSMatt Macy  *  0  | 57  58  59  60  61  62| 90  91  92  93  94  95|       |  w
102*7877fdebSMatt Macy  *     | 66  67  68  69  70  71| 99 100 101 102 103 104|       |  1
103*7877fdebSMatt Macy  *  |  +-----------+-----------+-----------------------+-------+
104*7877fdebSMatt Macy  *  |  |..group 2  |            group 3                |       |
105*7877fdebSMatt Macy  *  |  +-----------+-----------+-----------------------+-------+
106*7877fdebSMatt Macy  *  |  | 78  79  80|108 109 110 111 112 113 114 115 116|       |  r
107*7877fdebSMatt Macy  *  |  | 87  88  89|117 118 119 120 121 122 123 124 125|       |  o
108*7877fdebSMatt Macy  *  |  | 96  97  98|126 127 128 129 130 131 132 133 134|       |  w
109*7877fdebSMatt Macy  *  v  |105 106 107|135 136 137 138 139 140 141 142 143|       |  2
110*7877fdebSMatt Macy  *     +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
111*7877fdebSMatt Macy  *     | 9 | 11| 12| 2 | 4 | 1 | 3 | 0 | 10| 13| 8 | 5 | 6 | 7 | device map 1
112*7877fdebSMatt Macy  *  s  +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
113*7877fdebSMatt Macy  *  l  |              group 4              |  group 5..|       | row 3
114*7877fdebSMatt Macy  *  i  +-----------------------+-----------+-----------+-------|
115*7877fdebSMatt Macy  *  c  |       ..group 5       |        group 6..      |       | row 4
116*7877fdebSMatt Macy  *  e  +-----------+-----------+-----------------------+-------+
117*7877fdebSMatt Macy  *  1  |..group 6  |            group 7                |       | row 5
118*7877fdebSMatt Macy  *     +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
119*7877fdebSMatt Macy  *     | 3 | 5 | 10| 8 | 6 | 11| 12| 0 | 2 | 4 | 7 | 1 | 9 | 13| device map 2
120*7877fdebSMatt Macy  *  s  +===+===+===+===+===+===+===+===+===+===+===+===+===+===+
121*7877fdebSMatt Macy  *  l  |              group 8              |  group 9..|       | row 6
122*7877fdebSMatt Macy  *  i  +-----------------------------------------------+-------|
123*7877fdebSMatt Macy  *  c  |       ..group 9       |        group 10..     |       | row 7
124*7877fdebSMatt Macy  *  e  +-----------------------+-----------------------+-------+
125*7877fdebSMatt Macy  *  2  |..group 10 |            group 11               |       | row 8
126*7877fdebSMatt Macy  *     +-----------+-----------------------------------+-------+
127*7877fdebSMatt Macy  *
128*7877fdebSMatt Macy  * This layout has several advantages over requiring that each row contain
129*7877fdebSMatt Macy  * a whole number of groups.
130*7877fdebSMatt Macy  *
131*7877fdebSMatt Macy  * 1. The group count is not a relevant parameter when defining a dRAID
132*7877fdebSMatt Macy  *    layout. Only the group width is needed, and *all* groups will have
133*7877fdebSMatt Macy  *    the desired size.
134*7877fdebSMatt Macy  *
135*7877fdebSMatt Macy  * 2. All possible group widths (<= physical disk count) can be supported.
136*7877fdebSMatt Macy  *
137*7877fdebSMatt Macy  * 3. The logic within vdev_draid.c is simplified when the group width is
138*7877fdebSMatt Macy  *    the same for all groups (although some of the logic around computing
139*7877fdebSMatt Macy  *    permutation numbers and drive offsets is more complicated).
140*7877fdebSMatt Macy  *
141*7877fdebSMatt Macy  * N.B. The following array describes all valid dRAID permutation maps.
142*7877fdebSMatt Macy  * Each row is used to generate a permutation map for a different number
143*7877fdebSMatt Macy  * of children from a unique seed. The seeds were generated and carefully
144*7877fdebSMatt Macy  * evaluated by the 'draid' utility in order to provide balanced mappings.
145*7877fdebSMatt Macy  * In addition to the seed a checksum of the in-memory mapping is stored
146*7877fdebSMatt Macy  * for verification.
147*7877fdebSMatt Macy  *
148*7877fdebSMatt Macy  * The imbalance ratio of a given failure (e.g. 5 disks wide, child 3 failed,
149*7877fdebSMatt Macy  * with a given permutation map) is the ratio of the amounts of I/O that will
150*7877fdebSMatt Macy  * be sent to the least and most busy disks when resilvering. The average
151*7877fdebSMatt Macy  * imbalance ratio (of a given number of disks and permutation map) is the
152*7877fdebSMatt Macy  * average of the ratios of all possible single and double disk failures.
153*7877fdebSMatt Macy  *
154*7877fdebSMatt Macy  * In order to achieve a low imbalance ratio the number of permutations in
155*7877fdebSMatt Macy  * the mapping must be significantly larger than the number of children.
156*7877fdebSMatt Macy  * For dRAID the number of permutations has been limited to 512 to minimize
157*7877fdebSMatt Macy  * the map size. This does result in a gradually increasing imbalance ratio
158*7877fdebSMatt Macy  * as seen in the table below. Increasing the number of permutations for
159*7877fdebSMatt Macy  * larger child counts would reduce the imbalance ratio. However, in practice
160*7877fdebSMatt Macy  * when there are a large number of children each child is responsible for
161*7877fdebSMatt Macy  * fewer total IOs so it's less of a concern.
162*7877fdebSMatt Macy  *
163*7877fdebSMatt Macy  * Note these values are hard coded and must never be changed.  Existing
164*7877fdebSMatt Macy  * pools depend on the same mapping always being generated in order to
165*7877fdebSMatt Macy  * read and write from the correct locations.  Any change would make
166*7877fdebSMatt Macy  * existing pools completely inaccessible.
167*7877fdebSMatt Macy  */
168*7877fdebSMatt Macy static const draid_map_t draid_maps[VDEV_DRAID_MAX_MAPS] = {
169*7877fdebSMatt Macy 	{   2, 256, 0x89ef3dabbcc7de37, 0x00000000433d433d },	/* 1.000 */
170*7877fdebSMatt Macy 	{   3, 256, 0x89a57f3de98121b4, 0x00000000bcd8b7b5 },	/* 1.000 */
171*7877fdebSMatt Macy 	{   4, 256, 0xc9ea9ec82340c885, 0x00000001819d7c69 },	/* 1.000 */
172*7877fdebSMatt Macy 	{   5, 256, 0xf46733b7f4d47dfd, 0x00000002a1648d74 },	/* 1.010 */
173*7877fdebSMatt Macy 	{   6, 256, 0x88c3c62d8585b362, 0x00000003d3b0c2c4 },	/* 1.031 */
174*7877fdebSMatt Macy 	{   7, 256, 0x3a65d809b4d1b9d5, 0x000000055c4183ee },	/* 1.043 */
175*7877fdebSMatt Macy 	{   8, 256, 0xe98930e3c5d2e90a, 0x00000006edfb0329 },	/* 1.059 */
176*7877fdebSMatt Macy 	{   9, 256, 0x5a5430036b982ccb, 0x00000008ceaf6934 },	/* 1.056 */
177*7877fdebSMatt Macy 	{  10, 256, 0x92bf389e9eadac74, 0x0000000b26668c09 },	/* 1.072 */
178*7877fdebSMatt Macy 	{  11, 256, 0x74ccebf1dcf3ae80, 0x0000000dd691358c },	/* 1.083 */
179*7877fdebSMatt Macy 	{  12, 256, 0x8847e41a1a9f5671, 0x00000010a0c63c8e },	/* 1.097 */
180*7877fdebSMatt Macy 	{  13, 256, 0x7481b56debf0e637, 0x0000001424121fe4 },	/* 1.100 */
181*7877fdebSMatt Macy 	{  14, 256, 0x559b8c44065f8967, 0x00000016ab2ff079 },	/* 1.121 */
182*7877fdebSMatt Macy 	{  15, 256, 0x34c49545a2ee7f01, 0x0000001a6028efd6 },	/* 1.103 */
183*7877fdebSMatt Macy 	{  16, 256, 0xb85f4fa81a7698f7, 0x0000001e95ff5e66 },	/* 1.111 */
184*7877fdebSMatt Macy 	{  17, 256, 0x6353e47b7e47aba0, 0x00000021a81fa0fe },	/* 1.133 */
185*7877fdebSMatt Macy 	{  18, 256, 0xaa549746b1cbb81c, 0x00000026f02494c9 },	/* 1.131 */
186*7877fdebSMatt Macy 	{  19, 256, 0x892e343f2f31d690, 0x00000029eb392835 },	/* 1.130 */
187*7877fdebSMatt Macy 	{  20, 256, 0x76914824db98cc3f, 0x0000003004f31a7c },	/* 1.141 */
188*7877fdebSMatt Macy 	{  21, 256, 0x4b3cbabf9cfb1d0f, 0x00000036363a2408 },	/* 1.139 */
189*7877fdebSMatt Macy 	{  22, 256, 0xf45c77abb4f035d4, 0x00000038dd0f3e84 },	/* 1.150 */
190*7877fdebSMatt Macy 	{  23, 256, 0x5e18bd7f3fd4baf4, 0x0000003f0660391f },	/* 1.174 */
191*7877fdebSMatt Macy 	{  24, 256, 0xa7b3a4d285d6503b, 0x000000443dfc9ff6 },	/* 1.168 */
192*7877fdebSMatt Macy 	{  25, 256, 0x56ac7dd967521f5a, 0x0000004b03a87eb7 },	/* 1.180 */
193*7877fdebSMatt Macy 	{  26, 256, 0x3a42dfda4eb880f7, 0x000000522c719bba },	/* 1.226 */
194*7877fdebSMatt Macy 	{  27, 256, 0xd200d2fc6b54bf60, 0x0000005760b4fdf5 },	/* 1.228 */
195*7877fdebSMatt Macy 	{  28, 256, 0xc52605bbd486c546, 0x0000005e00d8f74c },	/* 1.217 */
196*7877fdebSMatt Macy 	{  29, 256, 0xc761779e63cd762f, 0x00000067be3cd85c },	/* 1.239 */
197*7877fdebSMatt Macy 	{  30, 256, 0xca577b1e07f85ca5, 0x0000006f5517f3e4 },	/* 1.238 */
198*7877fdebSMatt Macy 	{  31, 256, 0xfd50a593c518b3d4, 0x0000007370e7778f },	/* 1.273 */
199*7877fdebSMatt Macy 	{  32, 512, 0xc6c87ba5b042650b, 0x000000f7eb08a156 },	/* 1.191 */
200*7877fdebSMatt Macy 	{  33, 512, 0xc3880d0c9d458304, 0x0000010734b5d160 },	/* 1.199 */
201*7877fdebSMatt Macy 	{  34, 512, 0xe920927e4d8b2c97, 0x00000118c1edbce0 },	/* 1.195 */
202*7877fdebSMatt Macy 	{  35, 512, 0x8da7fcda87bde316, 0x0000012a3e9f9110 },	/* 1.201 */
203*7877fdebSMatt Macy 	{  36, 512, 0xcf09937491514a29, 0x0000013bd6a24bef },	/* 1.194 */
204*7877fdebSMatt Macy 	{  37, 512, 0x9b5abbf345cbd7cc, 0x0000014b9d90fac3 },	/* 1.237 */
205*7877fdebSMatt Macy 	{  38, 512, 0x506312a44668d6a9, 0x0000015e1b5f6148 },	/* 1.242 */
206*7877fdebSMatt Macy 	{  39, 512, 0x71659ede62b4755f, 0x00000173ef029bcd },	/* 1.231 */
207*7877fdebSMatt Macy 	{  40, 512, 0xa7fde73fb74cf2d7, 0x000001866fb72748 },	/* 1.233 */
208*7877fdebSMatt Macy 	{  41, 512, 0x19e8b461a1dea1d3, 0x000001a046f76b23 },	/* 1.271 */
209*7877fdebSMatt Macy 	{  42, 512, 0x031c9b868cc3e976, 0x000001afa64c49d3 },	/* 1.263 */
210*7877fdebSMatt Macy 	{  43, 512, 0xbaa5125faa781854, 0x000001c76789e278 },	/* 1.270 */
211*7877fdebSMatt Macy 	{  44, 512, 0x4ed55052550d721b, 0x000001d800ccd8eb },	/* 1.281 */
212*7877fdebSMatt Macy 	{  45, 512, 0x0fd63ddbdff90677, 0x000001f08ad59ed2 },	/* 1.282 */
213*7877fdebSMatt Macy 	{  46, 512, 0x36d66546de7fdd6f, 0x000002016f09574b },	/* 1.286 */
214*7877fdebSMatt Macy 	{  47, 512, 0x99f997e7eafb69d7, 0x0000021e42e47cb6 },	/* 1.329 */
215*7877fdebSMatt Macy 	{  48, 512, 0xbecd9c2571312c5d, 0x000002320fe2872b },	/* 1.286 */
216*7877fdebSMatt Macy 	{  49, 512, 0xd97371329e488a32, 0x0000024cd73f2ca7 },	/* 1.322 */
217*7877fdebSMatt Macy 	{  50, 512, 0x30e9b136670749ee, 0x000002681c83b0e0 },	/* 1.335 */
218*7877fdebSMatt Macy 	{  51, 512, 0x11ad6bc8f47aaeb4, 0x0000027e9261b5d5 },	/* 1.305 */
219*7877fdebSMatt Macy 	{  52, 512, 0x68e445300af432c1, 0x0000029aa0eb7dbf },	/* 1.330 */
220*7877fdebSMatt Macy 	{  53, 512, 0x910fb561657ea98c, 0x000002b3dca04853 },	/* 1.365 */
221*7877fdebSMatt Macy 	{  54, 512, 0xd619693d8ce5e7a5, 0x000002cc280e9c97 },	/* 1.334 */
222*7877fdebSMatt Macy 	{  55, 512, 0x24e281f564dbb60a, 0x000002e9fa842713 },	/* 1.364 */
223*7877fdebSMatt Macy 	{  56, 512, 0x947a7d3bdaab44c5, 0x000003046680f72e },	/* 1.374 */
224*7877fdebSMatt Macy 	{  57, 512, 0x2d44fec9c093e0de, 0x00000324198ba810 },	/* 1.363 */
225*7877fdebSMatt Macy 	{  58, 512, 0x87743c272d29bb4c, 0x0000033ec48c9ac9 },	/* 1.401 */
226*7877fdebSMatt Macy 	{  59, 512, 0x96aa3b6f67f5d923, 0x0000034faead902c },	/* 1.392 */
227*7877fdebSMatt Macy 	{  60, 512, 0x94a4f1faf520b0d3, 0x0000037d713ab005 },	/* 1.360 */
228*7877fdebSMatt Macy 	{  61, 512, 0xb13ed3a272f711a2, 0x00000397368f3cbd },	/* 1.396 */
229*7877fdebSMatt Macy 	{  62, 512, 0x3b1b11805fa4a64a, 0x000003b8a5e2840c },	/* 1.453 */
230*7877fdebSMatt Macy 	{  63, 512, 0x4c74caad9172ba71, 0x000003d4be280290 },	/* 1.437 */
231*7877fdebSMatt Macy 	{  64, 512, 0x035ff643923dd29e, 0x000003fad6c355e1 },	/* 1.402 */
232*7877fdebSMatt Macy 	{  65, 512, 0x768e9171b11abd3c, 0x0000040eb07fed20 },	/* 1.459 */
233*7877fdebSMatt Macy 	{  66, 512, 0x75880e6f78a13ddd, 0x000004433d6acf14 },	/* 1.423 */
234*7877fdebSMatt Macy 	{  67, 512, 0x910b9714f698a877, 0x00000451ea65d5db },	/* 1.447 */
235*7877fdebSMatt Macy 	{  68, 512, 0x87f5db6f9fdcf5c7, 0x000004732169e3f7 },	/* 1.450 */
236*7877fdebSMatt Macy 	{  69, 512, 0x836d4968fbaa3706, 0x000004954068a380 },	/* 1.455 */
237*7877fdebSMatt Macy 	{  70, 512, 0xc567d73a036421ab, 0x000004bd7cb7bd3d },	/* 1.463 */
238*7877fdebSMatt Macy 	{  71, 512, 0x619df40f240b8fed, 0x000004e376c2e972 },	/* 1.463 */
239*7877fdebSMatt Macy 	{  72, 512, 0x42763a680d5bed8e, 0x000005084275c680 },	/* 1.452 */
240*7877fdebSMatt Macy 	{  73, 512, 0x5866f064b3230431, 0x0000052906f2c9ab },	/* 1.498 */
241*7877fdebSMatt Macy 	{  74, 512, 0x9fa08548b1621a44, 0x0000054708019247 },	/* 1.526 */
242*7877fdebSMatt Macy 	{  75, 512, 0xb6053078ce0fc303, 0x00000572cc5c72b0 },	/* 1.491 */
243*7877fdebSMatt Macy 	{  76, 512, 0x4a7aad7bf3890923, 0x0000058e987bc8e9 },	/* 1.470 */
244*7877fdebSMatt Macy 	{  77, 512, 0xe165613fd75b5a53, 0x000005c20473a211 },	/* 1.527 */
245*7877fdebSMatt Macy 	{  78, 512, 0x3ff154ac878163a6, 0x000005d659194bf3 },	/* 1.509 */
246*7877fdebSMatt Macy 	{  79, 512, 0x24b93ade0aa8a532, 0x0000060a201c4f8e },	/* 1.569 */
247*7877fdebSMatt Macy 	{  80, 512, 0xc18e2d14cd9bb554, 0x0000062c55cfe48c },	/* 1.555 */
248*7877fdebSMatt Macy 	{  81, 512, 0x98cc78302feb58b6, 0x0000066656a07194 },	/* 1.509 */
249*7877fdebSMatt Macy 	{  82, 512, 0xc6c5fd5a2abc0543, 0x0000067cff94fbf8 },	/* 1.596 */
250*7877fdebSMatt Macy 	{  83, 512, 0xa7962f514acbba21, 0x000006ab7b5afa2e },	/* 1.568 */
251*7877fdebSMatt Macy 	{  84, 512, 0xba02545069ddc6dc, 0x000006d19861364f },	/* 1.541 */
252*7877fdebSMatt Macy 	{  85, 512, 0x447c73192c35073e, 0x000006fce315ce35 },	/* 1.623 */
253*7877fdebSMatt Macy 	{  86, 512, 0x48beef9e2d42b0c2, 0x00000720a8e38b6b },	/* 1.620 */
254*7877fdebSMatt Macy 	{  87, 512, 0x4874cf98541a35e0, 0x00000758382a2273 },	/* 1.597 */
255*7877fdebSMatt Macy 	{  88, 512, 0xad4cf8333a31127a, 0x00000781e1651b1b },	/* 1.575 */
256*7877fdebSMatt Macy 	{  89, 512, 0x47ae4859d57888c1, 0x000007b27edbe5bc },	/* 1.627 */
257*7877fdebSMatt Macy 	{  90, 512, 0x06f7723cfe5d1891, 0x000007dc2a96d8eb },	/* 1.596 */
258*7877fdebSMatt Macy 	{  91, 512, 0xd4e44218d660576d, 0x0000080ac46f02d5 },	/* 1.622 */
259*7877fdebSMatt Macy 	{  92, 512, 0x7066702b0d5be1f2, 0x00000832c96d154e },	/* 1.695 */
260*7877fdebSMatt Macy 	{  93, 512, 0x011209b4f9e11fb9, 0x0000085eefda104c },	/* 1.605 */
261*7877fdebSMatt Macy 	{  94, 512, 0x47ffba30a0b35708, 0x00000899badc32dc },	/* 1.625 */
262*7877fdebSMatt Macy 	{  95, 512, 0x1a95a6ac4538aaa8, 0x000008b6b69a42b2 },	/* 1.687 */
263*7877fdebSMatt Macy 	{  96, 512, 0xbda2b239bb2008eb, 0x000008f22d2de38a },	/* 1.621 */
264*7877fdebSMatt Macy 	{  97, 512, 0x7ffa0bea90355c6c, 0x0000092e5b23b816 },	/* 1.699 */
265*7877fdebSMatt Macy 	{  98, 512, 0x1d56ba34be426795, 0x0000094f482e5d1b },	/* 1.688 */
266*7877fdebSMatt Macy 	{  99, 512, 0x0aa89d45c502e93d, 0x00000977d94a98ce },	/* 1.642 */
267*7877fdebSMatt Macy 	{ 100, 512, 0x54369449f6857774, 0x000009c06c9b34cc },	/* 1.683 */
268*7877fdebSMatt Macy 	{ 101, 512, 0xf7d4dd8445b46765, 0x000009e5dc542259 },	/* 1.755 */
269*7877fdebSMatt Macy 	{ 102, 512, 0xfa8866312f169469, 0x00000a16b54eae93 },	/* 1.692 */
270*7877fdebSMatt Macy 	{ 103, 512, 0xd8a5aea08aef3ff9, 0x00000a381d2cbfe7 },	/* 1.747 */
271*7877fdebSMatt Macy 	{ 104, 512, 0x66bcd2c3d5f9ef0e, 0x00000a8191817be7 },	/* 1.751 */
272*7877fdebSMatt Macy 	{ 105, 512, 0x3fb13a47a012ec81, 0x00000ab562b9a254 },	/* 1.751 */
273*7877fdebSMatt Macy 	{ 106, 512, 0x43100f01c9e5e3ca, 0x00000aeee84c185f },	/* 1.726 */
274*7877fdebSMatt Macy 	{ 107, 512, 0xca09c50ccee2d054, 0x00000b1c359c047d },	/* 1.788 */
275*7877fdebSMatt Macy 	{ 108, 512, 0xd7176732ac503f9b, 0x00000b578bc52a73 },	/* 1.740 */
276*7877fdebSMatt Macy 	{ 109, 512, 0xed206e51f8d9422d, 0x00000b8083e0d960 },	/* 1.780 */
277*7877fdebSMatt Macy 	{ 110, 512, 0x17ead5dc6ba0dcd6, 0x00000bcfb1a32ca8 },	/* 1.836 */
278*7877fdebSMatt Macy 	{ 111, 512, 0x5f1dc21e38a969eb, 0x00000c0171becdd6 },	/* 1.778 */
279*7877fdebSMatt Macy 	{ 112, 512, 0xddaa973de33ec528, 0x00000c3edaba4b95 },	/* 1.831 */
280*7877fdebSMatt Macy 	{ 113, 512, 0x2a5eccd7735a3630, 0x00000c630664e7df },	/* 1.825 */
281*7877fdebSMatt Macy 	{ 114, 512, 0xafcccee5c0b71446, 0x00000cb65392f6e4 },	/* 1.826 */
282*7877fdebSMatt Macy 	{ 115, 512, 0x8fa30c5e7b147e27, 0x00000cd4db391e55 },	/* 1.843 */
283*7877fdebSMatt Macy 	{ 116, 512, 0x5afe0711fdfafd82, 0x00000d08cb4ec35d },	/* 1.826 */
284*7877fdebSMatt Macy 	{ 117, 512, 0x533a6090238afd4c, 0x00000d336f115d1b },	/* 1.803 */
285*7877fdebSMatt Macy 	{ 118, 512, 0x90cf11b595e39a84, 0x00000d8e041c2048 },	/* 1.857 */
286*7877fdebSMatt Macy 	{ 119, 512, 0x0d61a3b809444009, 0x00000dcb798afe35 },	/* 1.877 */
287*7877fdebSMatt Macy 	{ 120, 512, 0x7f34da0f54b0d114, 0x00000df3922664e1 },	/* 1.849 */
288*7877fdebSMatt Macy 	{ 121, 512, 0xa52258d5b72f6551, 0x00000e4d37a9872d },	/* 1.867 */
289*7877fdebSMatt Macy 	{ 122, 512, 0xc1de54d7672878db, 0x00000e6583a94cf6 },	/* 1.978 */
290*7877fdebSMatt Macy 	{ 123, 512, 0x1d03354316a414ab, 0x00000ebffc50308d },	/* 1.947 */
291*7877fdebSMatt Macy 	{ 124, 512, 0xcebdcc377665412c, 0x00000edee1997cea },	/* 1.865 */
292*7877fdebSMatt Macy 	{ 125, 512, 0x4ddd4c04b1a12344, 0x00000f21d64b373f },	/* 1.881 */
293*7877fdebSMatt Macy 	{ 126, 512, 0x64fc8f94e3973658, 0x00000f8f87a8896b },	/* 1.882 */
294*7877fdebSMatt Macy 	{ 127, 512, 0x68765f78034a334e, 0x00000fb8fe62197e },	/* 1.867 */
295*7877fdebSMatt Macy 	{ 128, 512, 0xaf36b871a303e816, 0x00000fec6f3afb1e },	/* 1.972 */
296*7877fdebSMatt Macy 	{ 129, 512, 0x2a4cbf73866c3a28, 0x00001027febfe4e5 },	/* 1.896 */
297*7877fdebSMatt Macy 	{ 130, 512, 0x9cb128aacdcd3b2f, 0x0000106aa8ac569d },	/* 1.965 */
298*7877fdebSMatt Macy 	{ 131, 512, 0x5511d41c55869124, 0x000010bbd755ddf1 },	/* 1.963 */
299*7877fdebSMatt Macy 	{ 132, 512, 0x42f92461937f284a, 0x000010fb8bceb3b5 },	/* 1.925 */
300*7877fdebSMatt Macy 	{ 133, 512, 0xe2d89a1cf6f1f287, 0x0000114cf5331e34 },	/* 1.862 */
301*7877fdebSMatt Macy 	{ 134, 512, 0xdc631a038956200e, 0x0000116428d2adc5 },	/* 2.042 */
302*7877fdebSMatt Macy 	{ 135, 512, 0xb2e5ac222cd236be, 0x000011ca88e4d4d2 },	/* 1.935 */
303*7877fdebSMatt Macy 	{ 136, 512, 0xbc7d8236655d88e7, 0x000011e39cb94e66 },	/* 2.005 */
304*7877fdebSMatt Macy 	{ 137, 512, 0x073e02d88d2d8e75, 0x0000123136c7933c },	/* 2.041 */
305*7877fdebSMatt Macy 	{ 138, 512, 0x3ddb9c3873166be0, 0x00001280e4ec6d52 },	/* 1.997 */
306*7877fdebSMatt Macy 	{ 139, 512, 0x7d3b1a845420e1b5, 0x000012c2e7cd6a44 },	/* 1.996 */
307*7877fdebSMatt Macy 	{ 140, 512, 0x60102308aa7b2a6c, 0x000012fc490e6c7d },	/* 2.053 */
308*7877fdebSMatt Macy 	{ 141, 512, 0xdb22bb2f9eb894aa, 0x00001343f5a85a1a },	/* 1.971 */
309*7877fdebSMatt Macy 	{ 142, 512, 0xd853f879a13b1606, 0x000013bb7d5f9048 },	/* 2.018 */
310*7877fdebSMatt Macy 	{ 143, 512, 0x001620a03f804b1d, 0x000013e74cc794fd },	/* 1.961 */
311*7877fdebSMatt Macy 	{ 144, 512, 0xfdb52dda76fbf667, 0x00001442d2f22480 },	/* 2.046 */
312*7877fdebSMatt Macy 	{ 145, 512, 0xa9160110f66e24ff, 0x0000144b899f9dbb },	/* 1.968 */
313*7877fdebSMatt Macy 	{ 146, 512, 0x77306a30379ae03b, 0x000014cb98eb1f81 },	/* 2.143 */
314*7877fdebSMatt Macy 	{ 147, 512, 0x14f5985d2752319d, 0x000014feab821fc9 },	/* 2.064 */
315*7877fdebSMatt Macy 	{ 148, 512, 0xa4b8ff11de7863f8, 0x0000154a0e60b9c9 },	/* 2.023 */
316*7877fdebSMatt Macy 	{ 149, 512, 0x44b345426455c1b3, 0x000015999c3c569c },	/* 2.136 */
317*7877fdebSMatt Macy 	{ 150, 512, 0x272677826049b46c, 0x000015c9697f4b92 },	/* 2.063 */
318*7877fdebSMatt Macy 	{ 151, 512, 0x2f9216e2cd74fe40, 0x0000162b1f7bbd39 },	/* 1.974 */
319*7877fdebSMatt Macy 	{ 152, 512, 0x706ae3e763ad8771, 0x00001661371c55e1 },	/* 2.210 */
320*7877fdebSMatt Macy 	{ 153, 512, 0xf7fd345307c2480e, 0x000016e251f28b6a },	/* 2.006 */
321*7877fdebSMatt Macy 	{ 154, 512, 0x6e94e3d26b3139eb, 0x000016f2429bb8c6 },	/* 2.193 */
322*7877fdebSMatt Macy 	{ 155, 512, 0x5458bbfbb781fcba, 0x0000173efdeca1b9 },	/* 2.163 */
323*7877fdebSMatt Macy 	{ 156, 512, 0xa80e2afeccd93b33, 0x000017bfdcb78adc },	/* 2.046 */
324*7877fdebSMatt Macy 	{ 157, 512, 0x1e4ccbb22796cf9d, 0x00001826fdcc39c9 },	/* 2.084 */
325*7877fdebSMatt Macy 	{ 158, 512, 0x8fba4b676aaa3663, 0x00001841a1379480 },	/* 2.264 */
326*7877fdebSMatt Macy 	{ 159, 512, 0xf82b843814b315fa, 0x000018886e19b8a3 },	/* 2.074 */
327*7877fdebSMatt Macy 	{ 160, 512, 0x7f21e920ecf753a3, 0x0000191812ca0ea7 },	/* 2.282 */
328*7877fdebSMatt Macy 	{ 161, 512, 0x48bb8ea2c4caa620, 0x0000192f310faccf },	/* 2.148 */
329*7877fdebSMatt Macy 	{ 162, 512, 0x5cdb652b4952c91b, 0x0000199e1d7437c7 },	/* 2.355 */
330*7877fdebSMatt Macy 	{ 163, 512, 0x6ac1ba6f78c06cd4, 0x000019cd11f82c70 },	/* 2.164 */
331*7877fdebSMatt Macy 	{ 164, 512, 0x9faf5f9ca2669a56, 0x00001a18d5431f6a },	/* 2.393 */
332*7877fdebSMatt Macy 	{ 165, 512, 0xaa57e9383eb01194, 0x00001a9e7d253d85 },	/* 2.178 */
333*7877fdebSMatt Macy 	{ 166, 512, 0x896967bf495c34d2, 0x00001afb8319b9fc },	/* 2.334 */
334*7877fdebSMatt Macy 	{ 167, 512, 0xdfad5f05de225f1b, 0x00001b3a59c3093b },	/* 2.266 */
335*7877fdebSMatt Macy 	{ 168, 512, 0xfd299a99f9f2abdd, 0x00001bb6f1a10799 },	/* 2.304 */
336*7877fdebSMatt Macy 	{ 169, 512, 0xdda239e798fe9fd4, 0x00001bfae0c9692d },	/* 2.218 */
337*7877fdebSMatt Macy 	{ 170, 512, 0x5fca670414a32c3e, 0x00001c22129dbcff },	/* 2.377 */
338*7877fdebSMatt Macy 	{ 171, 512, 0x1bb8934314b087de, 0x00001c955db36cd0 },	/* 2.155 */
339*7877fdebSMatt Macy 	{ 172, 512, 0xd96394b4b082200d, 0x00001cfc8619b7e6 },	/* 2.404 */
340*7877fdebSMatt Macy 	{ 173, 512, 0xb612a7735b1c8cbc, 0x00001d303acdd585 },	/* 2.205 */
341*7877fdebSMatt Macy 	{ 174, 512, 0x28e7430fe5875fe1, 0x00001d7ed5b3697d },	/* 2.359 */
342*7877fdebSMatt Macy 	{ 175, 512, 0x5038e89efdd981b9, 0x00001dc40ec35c59 },	/* 2.158 */
343*7877fdebSMatt Macy 	{ 176, 512, 0x075fd78f1d14db7c, 0x00001e31c83b4a2b },	/* 2.614 */
344*7877fdebSMatt Macy 	{ 177, 512, 0xc50fafdb5021be15, 0x00001e7cdac82fbc },	/* 2.239 */
345*7877fdebSMatt Macy 	{ 178, 512, 0xe6dc7572ce7b91c7, 0x00001edd8bb454fc },	/* 2.493 */
346*7877fdebSMatt Macy 	{ 179, 512, 0x21f7843e7beda537, 0x00001f3a8e019d6c },	/* 2.327 */
347*7877fdebSMatt Macy 	{ 180, 512, 0xc83385e20b43ec82, 0x00001f70735ec137 },	/* 2.231 */
348*7877fdebSMatt Macy 	{ 181, 512, 0xca818217dddb21fd, 0x0000201ca44c5a3c },	/* 2.237 */
349*7877fdebSMatt Macy 	{ 182, 512, 0xe6035defea48f933, 0x00002038e3346658 },	/* 2.691 */
350*7877fdebSMatt Macy 	{ 183, 512, 0x47262a4f953dac5a, 0x000020c2e554314e },	/* 2.170 */
351*7877fdebSMatt Macy 	{ 184, 512, 0xe24c7246260873ea, 0x000021197e618d64 },	/* 2.600 */
352*7877fdebSMatt Macy 	{ 185, 512, 0xeef6b57c9b58e9e1, 0x0000217ea48ecddc },	/* 2.391 */
353*7877fdebSMatt Macy 	{ 186, 512, 0x2becd3346e386142, 0x000021c496d4a5f9 },	/* 2.677 */
354*7877fdebSMatt Macy 	{ 187, 512, 0x63c6207bdf3b40a3, 0x0000220e0f2eec0c },	/* 2.410 */
355*7877fdebSMatt Macy 	{ 188, 512, 0x3056ce8989767d4b, 0x0000228eb76cd137 },	/* 2.776 */
356*7877fdebSMatt Macy 	{ 189, 512, 0x91af61c307cee780, 0x000022e17e2ea501 },	/* 2.266 */
357*7877fdebSMatt Macy 	{ 190, 512, 0xda359da225f6d54f, 0x00002358a2debc19 },	/* 2.717 */
358*7877fdebSMatt Macy 	{ 191, 512, 0x0a5f7a2a55607ba0, 0x0000238a79dac18c },	/* 2.474 */
359*7877fdebSMatt Macy 	{ 192, 512, 0x27bb75bf5224638a, 0x00002403a58e2351 },	/* 2.673 */
360*7877fdebSMatt Macy 	{ 193, 512, 0x1ebfdb94630f5d0f, 0x00002492a10cb339 },	/* 2.420 */
361*7877fdebSMatt Macy 	{ 194, 512, 0x6eae5e51d9c5f6fb, 0x000024ce4bf98715 },	/* 2.898 */
362*7877fdebSMatt Macy 	{ 195, 512, 0x08d903b4daedc2e0, 0x0000250d1e15886c },	/* 2.363 */
363*7877fdebSMatt Macy 	{ 196, 512, 0xc722a2f7fa7cd686, 0x0000258a99ed0c9e },	/* 2.747 */
364*7877fdebSMatt Macy 	{ 197, 512, 0x8f71faf0e54e361d, 0x000025dee11976f5 },	/* 2.531 */
365*7877fdebSMatt Macy 	{ 198, 512, 0x87f64695c91a54e7, 0x0000264e00a43da0 },	/* 2.707 */
366*7877fdebSMatt Macy 	{ 199, 512, 0xc719cbac2c336b92, 0x000026d327277ac1 },	/* 2.315 */
367*7877fdebSMatt Macy 	{ 200, 512, 0xe7e647afaf771ade, 0x000027523a5c44bf },	/* 3.012 */
368*7877fdebSMatt Macy 	{ 201, 512, 0x12d4b5c38ce8c946, 0x0000273898432545 },	/* 2.378 */
369*7877fdebSMatt Macy 	{ 202, 512, 0xf2e0cd4067bdc94a, 0x000027e47bb2c935 },	/* 2.969 */
370*7877fdebSMatt Macy 	{ 203, 512, 0x21b79f14d6d947d3, 0x0000281e64977f0d },	/* 2.594 */
371*7877fdebSMatt Macy 	{ 204, 512, 0x515093f952f18cd6, 0x0000289691a473fd },	/* 2.763 */
372*7877fdebSMatt Macy 	{ 205, 512, 0xd47b160a1b1022c8, 0x00002903e8b52411 },	/* 2.457 */
373*7877fdebSMatt Macy 	{ 206, 512, 0xc02fc96684715a16, 0x0000297515608601 },	/* 3.057 */
374*7877fdebSMatt Macy 	{ 207, 512, 0xef51e68efba72ed0, 0x000029ef73604804 },	/* 2.590 */
375*7877fdebSMatt Macy 	{ 208, 512, 0x9e3be6e5448b4f33, 0x00002a2846ed074b },	/* 3.047 */
376*7877fdebSMatt Macy 	{ 209, 512, 0x81d446c6d5fec063, 0x00002a92ca693455 },	/* 2.676 */
377*7877fdebSMatt Macy 	{ 210, 512, 0xff215de8224e57d5, 0x00002b2271fe3729 },	/* 2.993 */
378*7877fdebSMatt Macy 	{ 211, 512, 0xe2524d9ba8f69796, 0x00002b64b99c3ba2 },	/* 2.457 */
379*7877fdebSMatt Macy 	{ 212, 512, 0xf6b28e26097b7e4b, 0x00002bd768b6e068 },	/* 3.182 */
380*7877fdebSMatt Macy 	{ 213, 512, 0x893a487f30ce1644, 0x00002c67f722b4b2 },	/* 2.563 */
381*7877fdebSMatt Macy 	{ 214, 512, 0x386566c3fc9871df, 0x00002cc1cf8b4037 },	/* 3.025 */
382*7877fdebSMatt Macy 	{ 215, 512, 0x1e0ed78edf1f558a, 0x00002d3948d36c7f },	/* 2.730 */
383*7877fdebSMatt Macy 	{ 216, 512, 0xe3bc20c31e61f113, 0x00002d6d6b12e025 },	/* 3.036 */
384*7877fdebSMatt Macy 	{ 217, 512, 0xd6c3ad2e23021882, 0x00002deff7572241 },	/* 2.722 */
385*7877fdebSMatt Macy 	{ 218, 512, 0xb4a9f95cf0f69c5a, 0x00002e67d537aa36 },	/* 3.356 */
386*7877fdebSMatt Macy 	{ 219, 512, 0x6e98ed6f6c38e82f, 0x00002e9720626789 },	/* 2.697 */
387*7877fdebSMatt Macy 	{ 220, 512, 0x2e01edba33fddac7, 0x00002f407c6b0198 },	/* 2.979 */
388*7877fdebSMatt Macy 	{ 221, 512, 0x559d02e1f5f57ccc, 0x00002fb6a5ab4f24 },	/* 2.858 */
389*7877fdebSMatt Macy 	{ 222, 512, 0xac18f5a916adcd8e, 0x0000304ae1c5c57e },	/* 3.258 */
390*7877fdebSMatt Macy 	{ 223, 512, 0x15789fbaddb86f4b, 0x0000306f6e019c78 },	/* 2.693 */
391*7877fdebSMatt Macy 	{ 224, 512, 0xf4a9c36d5bc4c408, 0x000030da40434213 },	/* 3.259 */
392*7877fdebSMatt Macy 	{ 225, 512, 0xf640f90fd2727f44, 0x00003189ed37b90c },	/* 2.733 */
393*7877fdebSMatt Macy 	{ 226, 512, 0xb5313d390d61884a, 0x000031e152616b37 },	/* 3.235 */
394*7877fdebSMatt Macy 	{ 227, 512, 0x4bae6b3ce9160939, 0x0000321f40aeac42 },	/* 2.983 */
395*7877fdebSMatt Macy 	{ 228, 512, 0x838c34480f1a66a1, 0x000032f389c0f78e },	/* 3.308 */
396*7877fdebSMatt Macy 	{ 229, 512, 0xb1c4a52c8e3d6060, 0x0000330062a40284 },	/* 2.715 */
397*7877fdebSMatt Macy 	{ 230, 512, 0xe0f1110c6d0ed822, 0x0000338be435644f },	/* 3.540 */
398*7877fdebSMatt Macy 	{ 231, 512, 0x9f1a8ccdcea68d4b, 0x000034045a4e97e1 },	/* 2.779 */
399*7877fdebSMatt Macy 	{ 232, 512, 0x3261ed62223f3099, 0x000034702cfc401c },	/* 3.084 */
400*7877fdebSMatt Macy 	{ 233, 512, 0xf2191e2311022d65, 0x00003509dd19c9fc },	/* 2.987 */
401*7877fdebSMatt Macy 	{ 234, 512, 0xf102a395c2033abc, 0x000035654dc96fae },	/* 3.341 */
402*7877fdebSMatt Macy 	{ 235, 512, 0x11fe378f027906b6, 0x000035b5193b0264 },	/* 2.793 */
403*7877fdebSMatt Macy 	{ 236, 512, 0xf777f2c026b337aa, 0x000036704f5d9297 },	/* 3.518 */
404*7877fdebSMatt Macy 	{ 237, 512, 0x1b04e9c2ee143f32, 0x000036dfbb7af218 },	/* 2.962 */
405*7877fdebSMatt Macy 	{ 238, 512, 0x2fcec95266f9352c, 0x00003785c8df24a9 },	/* 3.196 */
406*7877fdebSMatt Macy 	{ 239, 512, 0xfe2b0e47e427dd85, 0x000037cbdf5da729 },	/* 2.914 */
407*7877fdebSMatt Macy 	{ 240, 512, 0x72b49bf2225f6c6d, 0x0000382227c15855 },	/* 3.408 */
408*7877fdebSMatt Macy 	{ 241, 512, 0x50486b43df7df9c7, 0x0000389b88be6453 },	/* 2.903 */
409*7877fdebSMatt Macy 	{ 242, 512, 0x5192a3e53181c8ab, 0x000038ddf3d67263 },	/* 3.778 */
410*7877fdebSMatt Macy 	{ 243, 512, 0xe9f5d8365296fd5e, 0x0000399f1c6c9e9c },	/* 3.026 */
411*7877fdebSMatt Macy 	{ 244, 512, 0xc740263f0301efa8, 0x00003a147146512d },	/* 3.347 */
412*7877fdebSMatt Macy 	{ 245, 512, 0x23cd0f2b5671e67d, 0x00003ab10bcc0d9d },	/* 3.212 */
413*7877fdebSMatt Macy 	{ 246, 512, 0x002ccc7e5cd41390, 0x00003ad6cd14a6c0 },	/* 3.482 */
414*7877fdebSMatt Macy 	{ 247, 512, 0x9aafb3c02544b31b, 0x00003b8cb8779fb0 },	/* 3.146 */
415*7877fdebSMatt Macy 	{ 248, 512, 0x72ba07a78b121999, 0x00003c24142a5a3f },	/* 3.626 */
416*7877fdebSMatt Macy 	{ 249, 512, 0x3d784aa58edfc7b4, 0x00003cd084817d99 },	/* 2.952 */
417*7877fdebSMatt Macy 	{ 250, 512, 0xaab750424d8004af, 0x00003d506a8e098e },	/* 3.463 */
418*7877fdebSMatt Macy 	{ 251, 512, 0x84403fcf8e6b5ca2, 0x00003d4c54c2aec4 },	/* 3.131 */
419*7877fdebSMatt Macy 	{ 252, 512, 0x71eb7455ec98e207, 0x00003e655715cf2c },	/* 3.538 */
420*7877fdebSMatt Macy 	{ 253, 512, 0xd752b4f19301595b, 0x00003ecd7b2ca5ac },	/* 2.974 */
421*7877fdebSMatt Macy 	{ 254, 512, 0xc4674129750499de, 0x00003e99e86d3e95 },	/* 3.843 */
422*7877fdebSMatt Macy 	{ 255, 512, 0x9772baff5cd12ef5, 0x00003f895c019841 },	/* 3.088 */
423*7877fdebSMatt Macy };
424*7877fdebSMatt Macy 
425*7877fdebSMatt Macy /*
426*7877fdebSMatt Macy  * Verify the map is valid. Each device index must appear exactly
427*7877fdebSMatt Macy  * once in every row, and the permutation array checksum must match.
428*7877fdebSMatt Macy  */
429*7877fdebSMatt Macy static int
430*7877fdebSMatt Macy verify_perms(uint8_t *perms, uint64_t children, uint64_t nperms,
431*7877fdebSMatt Macy     uint64_t checksum)
432*7877fdebSMatt Macy {
433*7877fdebSMatt Macy 	int countssz = sizeof (uint16_t) * children;
434*7877fdebSMatt Macy 	uint16_t *counts = kmem_zalloc(countssz, KM_SLEEP);
435*7877fdebSMatt Macy 
436*7877fdebSMatt Macy 	for (int i = 0; i < nperms; i++) {
437*7877fdebSMatt Macy 		for (int j = 0; j < children; j++) {
438*7877fdebSMatt Macy 			uint8_t val = perms[(i * children) + j];
439*7877fdebSMatt Macy 
440*7877fdebSMatt Macy 			if (val >= children || counts[val] != i) {
441*7877fdebSMatt Macy 				kmem_free(counts, countssz);
442*7877fdebSMatt Macy 				return (EINVAL);
443*7877fdebSMatt Macy 			}
444*7877fdebSMatt Macy 
445*7877fdebSMatt Macy 			counts[val]++;
446*7877fdebSMatt Macy 		}
447*7877fdebSMatt Macy 	}
448*7877fdebSMatt Macy 
449*7877fdebSMatt Macy 	if (checksum != 0) {
450*7877fdebSMatt Macy 		int permssz = sizeof (uint8_t) * children * nperms;
451*7877fdebSMatt Macy 		zio_cksum_t cksum;
452*7877fdebSMatt Macy 
453*7877fdebSMatt Macy 		fletcher_4_native_varsize(perms, permssz, &cksum);
454*7877fdebSMatt Macy 
455*7877fdebSMatt Macy 		if (checksum != cksum.zc_word[0]) {
456*7877fdebSMatt Macy 			kmem_free(counts, countssz);
457*7877fdebSMatt Macy 			return (ECKSUM);
458*7877fdebSMatt Macy 		}
459*7877fdebSMatt Macy 	}
460*7877fdebSMatt Macy 
461*7877fdebSMatt Macy 	kmem_free(counts, countssz);
462*7877fdebSMatt Macy 
463*7877fdebSMatt Macy 	return (0);
464*7877fdebSMatt Macy }
465*7877fdebSMatt Macy 
466*7877fdebSMatt Macy /*
467*7877fdebSMatt Macy  * Generate the permutation array for the draid_map_t.  These maps control
468*7877fdebSMatt Macy  * the placement of all data in a dRAID.  Therefore it's critical that the
469*7877fdebSMatt Macy  * seed always generates the same mapping.  We provide our own pseudo-random
470*7877fdebSMatt Macy  * number generator for this purpose.
471*7877fdebSMatt Macy  */
472*7877fdebSMatt Macy int
473*7877fdebSMatt Macy vdev_draid_generate_perms(const draid_map_t *map, uint8_t **permsp)
474*7877fdebSMatt Macy {
475*7877fdebSMatt Macy 	VERIFY3U(map->dm_children, >=, VDEV_DRAID_MIN_CHILDREN);
476*7877fdebSMatt Macy 	VERIFY3U(map->dm_children, <=, VDEV_DRAID_MAX_CHILDREN);
477*7877fdebSMatt Macy 	VERIFY3U(map->dm_seed, !=, 0);
478*7877fdebSMatt Macy 	VERIFY3U(map->dm_nperms, !=, 0);
479*7877fdebSMatt Macy 	VERIFY3P(map->dm_perms, ==, NULL);
480*7877fdebSMatt Macy 
481*7877fdebSMatt Macy #ifdef _KERNEL
482*7877fdebSMatt Macy 	/*
483*7877fdebSMatt Macy 	 * The kernel code always provides both a map_seed and checksum.
484*7877fdebSMatt Macy 	 * Only the tests/zfs-tests/cmd/draid/draid.c utility will provide
485*7877fdebSMatt Macy 	 * a zero checksum when generating new candidate maps.
486*7877fdebSMatt Macy 	 */
487*7877fdebSMatt Macy 	VERIFY3U(map->dm_checksum, !=, 0);
488*7877fdebSMatt Macy #endif
489*7877fdebSMatt Macy 	uint64_t children = map->dm_children;
490*7877fdebSMatt Macy 	uint64_t nperms = map->dm_nperms;
491*7877fdebSMatt Macy 	int rowsz = sizeof (uint8_t) * children;
492*7877fdebSMatt Macy 	int permssz = rowsz * nperms;
493*7877fdebSMatt Macy 	uint8_t *perms;
494*7877fdebSMatt Macy 
495*7877fdebSMatt Macy 	/* Allocate the permutation array */
496*7877fdebSMatt Macy 	perms = vmem_alloc(permssz, KM_SLEEP);
497*7877fdebSMatt Macy 
498*7877fdebSMatt Macy 	/* Setup an initial row with a known pattern */
499*7877fdebSMatt Macy 	uint8_t *initial_row = kmem_alloc(rowsz, KM_SLEEP);
500*7877fdebSMatt Macy 	for (int i = 0; i < children; i++)
501*7877fdebSMatt Macy 		initial_row[i] = i;
502*7877fdebSMatt Macy 
503*7877fdebSMatt Macy 	uint64_t draid_seed[2] = { VDEV_DRAID_SEED, map->dm_seed };
504*7877fdebSMatt Macy 	uint8_t *current_row, *previous_row = initial_row;
505*7877fdebSMatt Macy 
506*7877fdebSMatt Macy 	/*
507*7877fdebSMatt Macy 	 * Perform a Fisher-Yates shuffle of each row using the previous
508*7877fdebSMatt Macy 	 * row as the starting point.  An initial_row with known pattern
509*7877fdebSMatt Macy 	 * is used as the input for the first row.
510*7877fdebSMatt Macy 	 */
511*7877fdebSMatt Macy 	for (int i = 0; i < nperms; i++) {
512*7877fdebSMatt Macy 		current_row = &perms[i * children];
513*7877fdebSMatt Macy 		memcpy(current_row, previous_row, rowsz);
514*7877fdebSMatt Macy 
515*7877fdebSMatt Macy 		for (int j = children - 1; j > 0; j--) {
516*7877fdebSMatt Macy 			uint64_t k = vdev_draid_rand(draid_seed) % (j + 1);
517*7877fdebSMatt Macy 			uint8_t val = current_row[j];
518*7877fdebSMatt Macy 			current_row[j] = current_row[k];
519*7877fdebSMatt Macy 			current_row[k] = val;
520*7877fdebSMatt Macy 		}
521*7877fdebSMatt Macy 
522*7877fdebSMatt Macy 		previous_row = current_row;
523*7877fdebSMatt Macy 	}
524*7877fdebSMatt Macy 
525*7877fdebSMatt Macy 	kmem_free(initial_row, rowsz);
526*7877fdebSMatt Macy 
527*7877fdebSMatt Macy 	int error = verify_perms(perms, children, nperms, map->dm_checksum);
528*7877fdebSMatt Macy 	if (error) {
529*7877fdebSMatt Macy 		vmem_free(perms, permssz);
530*7877fdebSMatt Macy 		return (error);
531*7877fdebSMatt Macy 	}
532*7877fdebSMatt Macy 
533*7877fdebSMatt Macy 	*permsp = perms;
534*7877fdebSMatt Macy 
535*7877fdebSMatt Macy 	return (0);
536*7877fdebSMatt Macy }
537*7877fdebSMatt Macy 
538*7877fdebSMatt Macy /*
539*7877fdebSMatt Macy  * Lookup the fixed draid_map_t for the requested number of children.
540*7877fdebSMatt Macy  */
541*7877fdebSMatt Macy int
542*7877fdebSMatt Macy vdev_draid_lookup_map(uint64_t children, const draid_map_t **mapp)
543*7877fdebSMatt Macy {
544*7877fdebSMatt Macy 	for (int i = 0; i <= VDEV_DRAID_MAX_MAPS; i++) {
545*7877fdebSMatt Macy 		if (draid_maps[i].dm_children == children) {
546*7877fdebSMatt Macy 			*mapp = &draid_maps[i];
547*7877fdebSMatt Macy 			return (0);
548*7877fdebSMatt Macy 		}
549*7877fdebSMatt Macy 	}
550*7877fdebSMatt Macy 
551*7877fdebSMatt Macy 	return (ENOENT);
552*7877fdebSMatt Macy }
553*7877fdebSMatt Macy 
554*7877fdebSMatt Macy /*
555*7877fdebSMatt Macy  * Lookup the permutation array and iteration id for the provided offset.
556*7877fdebSMatt Macy  */
557*7877fdebSMatt Macy static void
558*7877fdebSMatt Macy vdev_draid_get_perm(vdev_draid_config_t *vdc, uint64_t pindex,
559*7877fdebSMatt Macy     uint8_t **base, uint64_t *iter)
560*7877fdebSMatt Macy {
561*7877fdebSMatt Macy 	uint64_t ncols = vdc->vdc_children;
562*7877fdebSMatt Macy 	uint64_t poff = pindex % (vdc->vdc_nperms * ncols);
563*7877fdebSMatt Macy 
564*7877fdebSMatt Macy 	*base = vdc->vdc_perms + (poff / ncols) * ncols;
565*7877fdebSMatt Macy 	*iter = poff % ncols;
566*7877fdebSMatt Macy }
567*7877fdebSMatt Macy 
568*7877fdebSMatt Macy static inline uint64_t
569*7877fdebSMatt Macy vdev_draid_permute_id(vdev_draid_config_t *vdc,
570*7877fdebSMatt Macy     uint8_t *base, uint64_t iter, uint64_t index)
571*7877fdebSMatt Macy {
572*7877fdebSMatt Macy 	return ((base[index] + iter) % vdc->vdc_children);
573*7877fdebSMatt Macy }
574*7877fdebSMatt Macy 
575*7877fdebSMatt Macy /*
576*7877fdebSMatt Macy  * Return the asize which is the psize rounded up to a full group width.
577*7877fdebSMatt Macy  * i.e. vdev_draid_psize_to_asize().
578*7877fdebSMatt Macy  */
579*7877fdebSMatt Macy static uint64_t
580*7877fdebSMatt Macy vdev_draid_asize(vdev_t *vd, uint64_t psize)
581*7877fdebSMatt Macy {
582*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
583*7877fdebSMatt Macy 	uint64_t ashift = vd->vdev_ashift;
584*7877fdebSMatt Macy 
585*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
586*7877fdebSMatt Macy 
587*7877fdebSMatt Macy 	uint64_t rows = ((psize - 1) / (vdc->vdc_ndata << ashift)) + 1;
588*7877fdebSMatt Macy 	uint64_t asize = (rows * vdc->vdc_groupwidth) << ashift;
589*7877fdebSMatt Macy 
590*7877fdebSMatt Macy 	ASSERT3U(asize, !=, 0);
591*7877fdebSMatt Macy 	ASSERT3U(asize % (vdc->vdc_groupwidth), ==, 0);
592*7877fdebSMatt Macy 
593*7877fdebSMatt Macy 	return (asize);
594*7877fdebSMatt Macy }
595*7877fdebSMatt Macy 
596*7877fdebSMatt Macy /*
597*7877fdebSMatt Macy  * Deflate the asize to the psize, this includes stripping parity.
598*7877fdebSMatt Macy  */
599*7877fdebSMatt Macy uint64_t
600*7877fdebSMatt Macy vdev_draid_asize_to_psize(vdev_t *vd, uint64_t asize)
601*7877fdebSMatt Macy {
602*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
603*7877fdebSMatt Macy 
604*7877fdebSMatt Macy 	ASSERT0(asize % vdc->vdc_groupwidth);
605*7877fdebSMatt Macy 
606*7877fdebSMatt Macy 	return ((asize / vdc->vdc_groupwidth) * vdc->vdc_ndata);
607*7877fdebSMatt Macy }
608*7877fdebSMatt Macy 
609*7877fdebSMatt Macy /*
610*7877fdebSMatt Macy  * Convert a logical offset to the corresponding group number.
611*7877fdebSMatt Macy  */
612*7877fdebSMatt Macy static uint64_t
613*7877fdebSMatt Macy vdev_draid_offset_to_group(vdev_t *vd, uint64_t offset)
614*7877fdebSMatt Macy {
615*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
616*7877fdebSMatt Macy 
617*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
618*7877fdebSMatt Macy 
619*7877fdebSMatt Macy 	return (offset / vdc->vdc_groupsz);
620*7877fdebSMatt Macy }
621*7877fdebSMatt Macy 
622*7877fdebSMatt Macy /*
623*7877fdebSMatt Macy  * Convert a group number to the logical starting offset for that group.
624*7877fdebSMatt Macy  */
625*7877fdebSMatt Macy static uint64_t
626*7877fdebSMatt Macy vdev_draid_group_to_offset(vdev_t *vd, uint64_t group)
627*7877fdebSMatt Macy {
628*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
629*7877fdebSMatt Macy 
630*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
631*7877fdebSMatt Macy 
632*7877fdebSMatt Macy 	return (group * vdc->vdc_groupsz);
633*7877fdebSMatt Macy }
634*7877fdebSMatt Macy 
635*7877fdebSMatt Macy 
636*7877fdebSMatt Macy static void
637*7877fdebSMatt Macy vdev_draid_map_free_vsd(zio_t *zio)
638*7877fdebSMatt Macy {
639*7877fdebSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
640*7877fdebSMatt Macy 
641*7877fdebSMatt Macy 	ASSERT0(rm->rm_freed);
642*7877fdebSMatt Macy 	rm->rm_freed = B_TRUE;
643*7877fdebSMatt Macy 
644*7877fdebSMatt Macy 	if (rm->rm_reports == 0) {
645*7877fdebSMatt Macy 		vdev_raidz_map_free(rm);
646*7877fdebSMatt Macy 	}
647*7877fdebSMatt Macy }
648*7877fdebSMatt Macy 
649*7877fdebSMatt Macy /*ARGSUSED*/
650*7877fdebSMatt Macy static void
651*7877fdebSMatt Macy vdev_draid_cksum_free(void *arg, size_t ignored)
652*7877fdebSMatt Macy {
653*7877fdebSMatt Macy 	raidz_map_t *rm = arg;
654*7877fdebSMatt Macy 
655*7877fdebSMatt Macy 	ASSERT3U(rm->rm_reports, >, 0);
656*7877fdebSMatt Macy 
657*7877fdebSMatt Macy 	if (--rm->rm_reports == 0 && rm->rm_freed)
658*7877fdebSMatt Macy 		vdev_raidz_map_free(rm);
659*7877fdebSMatt Macy }
660*7877fdebSMatt Macy 
661*7877fdebSMatt Macy static void
662*7877fdebSMatt Macy vdev_draid_cksum_finish(zio_cksum_report_t *zcr, const abd_t *good_data)
663*7877fdebSMatt Macy {
664*7877fdebSMatt Macy 	raidz_map_t *rm = zcr->zcr_cbdata;
665*7877fdebSMatt Macy 	const size_t c = zcr->zcr_cbinfo;
666*7877fdebSMatt Macy 	uint64_t skip_size = zcr->zcr_sector;
667*7877fdebSMatt Macy 	uint64_t parity_size;
668*7877fdebSMatt Macy 	size_t x, offset, size;
669*7877fdebSMatt Macy 
670*7877fdebSMatt Macy 	if (good_data == NULL) {
671*7877fdebSMatt Macy 		zfs_ereport_finish_checksum(zcr, NULL, NULL, B_FALSE);
672*7877fdebSMatt Macy 		return;
673*7877fdebSMatt Macy 	}
674*7877fdebSMatt Macy 
675*7877fdebSMatt Macy 	/*
676*7877fdebSMatt Macy 	 * Detailed cksum reporting is currently only supported for single
677*7877fdebSMatt Macy 	 * row draid mappings, this covers the vast majority of zios. Only
678*7877fdebSMatt Macy 	 * a dRAID zio which spans groups will have multiple rows.
679*7877fdebSMatt Macy 	 */
680*7877fdebSMatt Macy 	if (rm->rm_nrows != 1) {
681*7877fdebSMatt Macy 		zfs_ereport_finish_checksum(zcr, NULL, NULL, B_FALSE);
682*7877fdebSMatt Macy 		return;
683*7877fdebSMatt Macy 	}
684*7877fdebSMatt Macy 
685*7877fdebSMatt Macy 	raidz_row_t *rr = rm->rm_row[0];
686*7877fdebSMatt Macy 	const abd_t *good = NULL;
687*7877fdebSMatt Macy 	const abd_t *bad = rr->rr_col[c].rc_abd;
688*7877fdebSMatt Macy 
689*7877fdebSMatt Macy 	if (c < rr->rr_firstdatacol) {
690*7877fdebSMatt Macy 		/*
691*7877fdebSMatt Macy 		 * The first time through, calculate the parity blocks for
692*7877fdebSMatt Macy 		 * the good data (this relies on the fact that the good
693*7877fdebSMatt Macy 		 * data never changes for a given logical zio)
694*7877fdebSMatt Macy 		 */
695*7877fdebSMatt Macy 		if (rr->rr_col[0].rc_gdata == NULL) {
696*7877fdebSMatt Macy 			abd_t *bad_parity[VDEV_DRAID_MAXPARITY];
697*7877fdebSMatt Macy 
698*7877fdebSMatt Macy 			/*
699*7877fdebSMatt Macy 			 * Set up the rr_col[]s to generate the parity for
700*7877fdebSMatt Macy 			 * good_data, first saving the parity bufs and
701*7877fdebSMatt Macy 			 * replacing them with buffers to hold the result.
702*7877fdebSMatt Macy 			 */
703*7877fdebSMatt Macy 			for (x = 0; x < rr->rr_firstdatacol; x++) {
704*7877fdebSMatt Macy 				bad_parity[x] = rr->rr_col[x].rc_abd;
705*7877fdebSMatt Macy 				rr->rr_col[x].rc_abd = rr->rr_col[x].rc_gdata =
706*7877fdebSMatt Macy 				    abd_alloc_sametype(rr->rr_col[x].rc_abd,
707*7877fdebSMatt Macy 				    rr->rr_col[x].rc_size);
708*7877fdebSMatt Macy 			}
709*7877fdebSMatt Macy 
710*7877fdebSMatt Macy 			/*
711*7877fdebSMatt Macy 			 * Fill in the data columns from good_data being
712*7877fdebSMatt Macy 			 * careful to pad short columns and empty columns
713*7877fdebSMatt Macy 			 * with a skip sector.
714*7877fdebSMatt Macy 			 */
715*7877fdebSMatt Macy 			uint64_t good_size = abd_get_size((abd_t *)good_data);
716*7877fdebSMatt Macy 
717*7877fdebSMatt Macy 			offset = 0;
718*7877fdebSMatt Macy 			for (; x < rr->rr_cols; x++) {
719*7877fdebSMatt Macy 				abd_put(rr->rr_col[x].rc_abd);
720*7877fdebSMatt Macy 
721*7877fdebSMatt Macy 				if (offset == good_size) {
722*7877fdebSMatt Macy 					/* empty data column (small write) */
723*7877fdebSMatt Macy 					rr->rr_col[x].rc_abd =
724*7877fdebSMatt Macy 					    abd_get_zeros(skip_size);
725*7877fdebSMatt Macy 				} else if (x < rr->rr_bigcols) {
726*7877fdebSMatt Macy 					/* this is a "big column" */
727*7877fdebSMatt Macy 					size = rr->rr_col[x].rc_size;
728*7877fdebSMatt Macy 					rr->rr_col[x].rc_abd =
729*7877fdebSMatt Macy 					    abd_get_offset_size(
730*7877fdebSMatt Macy 					    (abd_t *)good_data, offset, size);
731*7877fdebSMatt Macy 					offset += size;
732*7877fdebSMatt Macy 				} else {
733*7877fdebSMatt Macy 					/* short data column, add skip sector */
734*7877fdebSMatt Macy 					size = rr->rr_col[x].rc_size -skip_size;
735*7877fdebSMatt Macy 					rr->rr_col[x].rc_abd = abd_alloc(
736*7877fdebSMatt Macy 					    rr->rr_col[x].rc_size, B_TRUE);
737*7877fdebSMatt Macy 					abd_copy_off(rr->rr_col[x].rc_abd,
738*7877fdebSMatt Macy 					    (abd_t *)good_data, 0, offset,
739*7877fdebSMatt Macy 					    size);
740*7877fdebSMatt Macy 					abd_zero_off(rr->rr_col[x].rc_abd,
741*7877fdebSMatt Macy 					    size, skip_size);
742*7877fdebSMatt Macy 					offset += size;
743*7877fdebSMatt Macy 				}
744*7877fdebSMatt Macy 			}
745*7877fdebSMatt Macy 
746*7877fdebSMatt Macy 			/*
747*7877fdebSMatt Macy 			 * Construct the parity from the good data.
748*7877fdebSMatt Macy 			 */
749*7877fdebSMatt Macy 			vdev_raidz_generate_parity_row(rm, rr);
750*7877fdebSMatt Macy 
751*7877fdebSMatt Macy 			/* restore everything back to its original state */
752*7877fdebSMatt Macy 			for (x = 0; x < rr->rr_firstdatacol; x++)
753*7877fdebSMatt Macy 				rr->rr_col[x].rc_abd = bad_parity[x];
754*7877fdebSMatt Macy 
755*7877fdebSMatt Macy 			offset = 0;
756*7877fdebSMatt Macy 			for (x = rr->rr_firstdatacol; x < rr->rr_cols; x++) {
757*7877fdebSMatt Macy 				if (offset == good_size || x < rr->rr_bigcols)
758*7877fdebSMatt Macy 					abd_put(rr->rr_col[x].rc_abd);
759*7877fdebSMatt Macy 				else
760*7877fdebSMatt Macy 					abd_free(rr->rr_col[x].rc_abd);
761*7877fdebSMatt Macy 
762*7877fdebSMatt Macy 				rr->rr_col[x].rc_abd = abd_get_offset_size(
763*7877fdebSMatt Macy 				    rr->rr_abd_copy, offset,
764*7877fdebSMatt Macy 				    rr->rr_col[x].rc_size);
765*7877fdebSMatt Macy 				offset += rr->rr_col[x].rc_size;
766*7877fdebSMatt Macy 			}
767*7877fdebSMatt Macy 		}
768*7877fdebSMatt Macy 
769*7877fdebSMatt Macy 		ASSERT3P(rr->rr_col[c].rc_gdata, !=, NULL);
770*7877fdebSMatt Macy 		good = abd_get_offset_size(rr->rr_col[c].rc_gdata, 0,
771*7877fdebSMatt Macy 		    rr->rr_col[c].rc_size);
772*7877fdebSMatt Macy 	} else {
773*7877fdebSMatt Macy 		/* adjust good_data to point at the start of our column */
774*7877fdebSMatt Macy 		parity_size = size = rr->rr_col[0].rc_size;
775*7877fdebSMatt Macy 		if (c >= rr->rr_bigcols) {
776*7877fdebSMatt Macy 			size -= skip_size;
777*7877fdebSMatt Macy 			zcr->zcr_length = size;
778*7877fdebSMatt Macy 		}
779*7877fdebSMatt Macy 
780*7877fdebSMatt Macy 		/* empty column */
781*7877fdebSMatt Macy 		if (size == 0) {
782*7877fdebSMatt Macy 			zfs_ereport_finish_checksum(zcr, NULL, NULL, B_TRUE);
783*7877fdebSMatt Macy 			return;
784*7877fdebSMatt Macy 		}
785*7877fdebSMatt Macy 
786*7877fdebSMatt Macy 		offset = 0;
787*7877fdebSMatt Macy 		for (x = rr->rr_firstdatacol; x < c; x++) {
788*7877fdebSMatt Macy 			if (x < rr->rr_bigcols) {
789*7877fdebSMatt Macy 				offset += parity_size;
790*7877fdebSMatt Macy 			} else {
791*7877fdebSMatt Macy 				offset += parity_size - skip_size;
792*7877fdebSMatt Macy 			}
793*7877fdebSMatt Macy 		}
794*7877fdebSMatt Macy 
795*7877fdebSMatt Macy 		good = abd_get_offset_size((abd_t *)good_data, offset, size);
796*7877fdebSMatt Macy 	}
797*7877fdebSMatt Macy 
798*7877fdebSMatt Macy 	/* we drop the ereport if it ends up that the data was good */
799*7877fdebSMatt Macy 	zfs_ereport_finish_checksum(zcr, good, bad, B_TRUE);
800*7877fdebSMatt Macy 	abd_put((abd_t *)good);
801*7877fdebSMatt Macy }
802*7877fdebSMatt Macy 
803*7877fdebSMatt Macy /*
804*7877fdebSMatt Macy  * Invoked indirectly by zfs_ereport_start_checksum(), called
805*7877fdebSMatt Macy  * below when our read operation fails completely.  The main point
806*7877fdebSMatt Macy  * is to keep a copy of everything we read from disk, so that at
807*7877fdebSMatt Macy  * vdev_draid_cksum_finish() time we can compare it with the good data.
808*7877fdebSMatt Macy  */
809*7877fdebSMatt Macy static void
810*7877fdebSMatt Macy vdev_draid_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *arg)
811*7877fdebSMatt Macy {
812*7877fdebSMatt Macy 	size_t c = (size_t)(uintptr_t)arg;
813*7877fdebSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
814*7877fdebSMatt Macy 
815*7877fdebSMatt Macy 	/* set up the report and bump the refcount  */
816*7877fdebSMatt Macy 	zcr->zcr_cbdata = rm;
817*7877fdebSMatt Macy 	zcr->zcr_cbinfo = c;
818*7877fdebSMatt Macy 	zcr->zcr_finish = vdev_draid_cksum_finish;
819*7877fdebSMatt Macy 	zcr->zcr_free = vdev_draid_cksum_free;
820*7877fdebSMatt Macy 
821*7877fdebSMatt Macy 	rm->rm_reports++;
822*7877fdebSMatt Macy 	ASSERT3U(rm->rm_reports, >, 0);
823*7877fdebSMatt Macy 
824*7877fdebSMatt Macy 	if (rm->rm_row[0]->rr_abd_copy != NULL)
825*7877fdebSMatt Macy 		return;
826*7877fdebSMatt Macy 
827*7877fdebSMatt Macy 	/*
828*7877fdebSMatt Macy 	 * It's the first time we're called for this raidz_map_t, so we need
829*7877fdebSMatt Macy 	 * to copy the data aside; there's no guarantee that our zio's buffer
830*7877fdebSMatt Macy 	 * won't be re-used for something else.
831*7877fdebSMatt Macy 	 *
832*7877fdebSMatt Macy 	 * Our parity data is already in separate buffers, so there's no need
833*7877fdebSMatt Macy 	 * to copy them.  Furthermore, all columns should have been expanded
834*7877fdebSMatt Macy 	 * by vdev_draid_map_alloc_empty() when attempting reconstruction.
835*7877fdebSMatt Macy 	 */
836*7877fdebSMatt Macy 	for (int i = 0; i < rm->rm_nrows; i++) {
837*7877fdebSMatt Macy 		raidz_row_t *rr = rm->rm_row[i];
838*7877fdebSMatt Macy 		size_t offset = 0;
839*7877fdebSMatt Macy 		size_t size = 0;
840*7877fdebSMatt Macy 
841*7877fdebSMatt Macy 		for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
842*7877fdebSMatt Macy 			ASSERT3U(rr->rr_col[c].rc_size, ==,
843*7877fdebSMatt Macy 			    rr->rr_col[0].rc_size);
844*7877fdebSMatt Macy 			size += rr->rr_col[c].rc_size;
845*7877fdebSMatt Macy 		}
846*7877fdebSMatt Macy 
847*7877fdebSMatt Macy 		rr->rr_abd_copy = abd_alloc_for_io(size, B_FALSE);
848*7877fdebSMatt Macy 
849*7877fdebSMatt Macy 		for (c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
850*7877fdebSMatt Macy 			raidz_col_t *col = &rr->rr_col[c];
851*7877fdebSMatt Macy 			abd_t *tmp = abd_get_offset_size(rr->rr_abd_copy,
852*7877fdebSMatt Macy 			    offset, col->rc_size);
853*7877fdebSMatt Macy 
854*7877fdebSMatt Macy 			abd_copy(tmp, col->rc_abd, col->rc_size);
855*7877fdebSMatt Macy 
856*7877fdebSMatt Macy 			if (abd_is_gang(col->rc_abd))
857*7877fdebSMatt Macy 				abd_free(col->rc_abd);
858*7877fdebSMatt Macy 			else
859*7877fdebSMatt Macy 				abd_put(col->rc_abd);
860*7877fdebSMatt Macy 
861*7877fdebSMatt Macy 			col->rc_abd = tmp;
862*7877fdebSMatt Macy 			offset += col->rc_size;
863*7877fdebSMatt Macy 		}
864*7877fdebSMatt Macy 		ASSERT3U(offset, ==, size);
865*7877fdebSMatt Macy 	}
866*7877fdebSMatt Macy }
867*7877fdebSMatt Macy 
868*7877fdebSMatt Macy const zio_vsd_ops_t vdev_draid_vsd_ops = {
869*7877fdebSMatt Macy 	.vsd_free = vdev_draid_map_free_vsd,
870*7877fdebSMatt Macy 	.vsd_cksum_report = vdev_draid_cksum_report
871*7877fdebSMatt Macy };
872*7877fdebSMatt Macy 
873*7877fdebSMatt Macy /*
874*7877fdebSMatt Macy  * Full stripe writes.  When writing, all columns (D+P) are required.  Parity
875*7877fdebSMatt Macy  * is calculated over all the columns, including empty zero filled sectors,
876*7877fdebSMatt Macy  * and each is written to disk.  While only the data columns are needed for
877*7877fdebSMatt Macy  * a normal read, all of the columns are required for reconstruction when
878*7877fdebSMatt Macy  * performing a sequential resilver.
879*7877fdebSMatt Macy  *
880*7877fdebSMatt Macy  * For "big columns" it's sufficient to map the correct range of the zio ABD.
881*7877fdebSMatt Macy  * Partial columns require allocating a gang ABD in order to zero fill the
882*7877fdebSMatt Macy  * empty sectors.  When the column is empty a zero filled sector must be
883*7877fdebSMatt Macy  * mapped.  In all cases the data ABDs must be the same size as the parity
884*7877fdebSMatt Macy  * ABDs (e.g. rc->rc_size == parity_size).
885*7877fdebSMatt Macy  */
886*7877fdebSMatt Macy static void
887*7877fdebSMatt Macy vdev_draid_map_alloc_write(zio_t *zio, uint64_t abd_offset, raidz_row_t *rr)
888*7877fdebSMatt Macy {
889*7877fdebSMatt Macy 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
890*7877fdebSMatt Macy 	uint64_t parity_size = rr->rr_col[0].rc_size;
891*7877fdebSMatt Macy 	uint64_t abd_off = abd_offset;
892*7877fdebSMatt Macy 
893*7877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
894*7877fdebSMatt Macy 	ASSERT3U(parity_size, ==, abd_get_size(rr->rr_col[0].rc_abd));
895*7877fdebSMatt Macy 
896*7877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
897*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
898*7877fdebSMatt Macy 
899*7877fdebSMatt Macy 		if (rc->rc_size == 0) {
900*7877fdebSMatt Macy 			/* empty data column (small write), add a skip sector */
901*7877fdebSMatt Macy 			ASSERT3U(skip_size, ==, parity_size);
902*7877fdebSMatt Macy 			rc->rc_abd = abd_get_zeros(skip_size);
903*7877fdebSMatt Macy 		} else if (rc->rc_size == parity_size) {
904*7877fdebSMatt Macy 			/* this is a "big column" */
905*7877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(zio->io_abd,
906*7877fdebSMatt Macy 			    abd_off, rc->rc_size);
907*7877fdebSMatt Macy 		} else {
908*7877fdebSMatt Macy 			/* short data column, add a skip sector */
909*7877fdebSMatt Macy 			ASSERT3U(rc->rc_size + skip_size, ==, parity_size);
910*7877fdebSMatt Macy 			rc->rc_abd = abd_alloc_gang_abd();
911*7877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
912*7877fdebSMatt Macy 			    zio->io_abd, abd_off, rc->rc_size), B_TRUE);
913*7877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_zeros(skip_size),
914*7877fdebSMatt Macy 			    B_TRUE);
915*7877fdebSMatt Macy 		}
916*7877fdebSMatt Macy 
917*7877fdebSMatt Macy 		ASSERT3U(abd_get_size(rc->rc_abd), ==, parity_size);
918*7877fdebSMatt Macy 
919*7877fdebSMatt Macy 		abd_off += rc->rc_size;
920*7877fdebSMatt Macy 		rc->rc_size = parity_size;
921*7877fdebSMatt Macy 	}
922*7877fdebSMatt Macy 
923*7877fdebSMatt Macy 	IMPLY(abd_offset != 0, abd_off == zio->io_size);
924*7877fdebSMatt Macy }
925*7877fdebSMatt Macy 
926*7877fdebSMatt Macy /*
927*7877fdebSMatt Macy  * Scrub/resilver reads.  In order to store the contents of the skip sectors
928*7877fdebSMatt Macy  * an additional ABD is allocated.  The columns are handled in the same way
929*7877fdebSMatt Macy  * as a full stripe write except instead of using the zero ABD the newly
930*7877fdebSMatt Macy  * allocated skip ABD is used to back the skip sectors.  In all cases the
931*7877fdebSMatt Macy  * data ABD must be the same size as the parity ABDs.
932*7877fdebSMatt Macy  */
933*7877fdebSMatt Macy static void
934*7877fdebSMatt Macy vdev_draid_map_alloc_scrub(zio_t *zio, uint64_t abd_offset, raidz_row_t *rr)
935*7877fdebSMatt Macy {
936*7877fdebSMatt Macy 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
937*7877fdebSMatt Macy 	uint64_t parity_size = rr->rr_col[0].rc_size;
938*7877fdebSMatt Macy 	uint64_t abd_off = abd_offset;
939*7877fdebSMatt Macy 	uint64_t skip_off = 0;
940*7877fdebSMatt Macy 
941*7877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
942*7877fdebSMatt Macy 	ASSERT3P(rr->rr_abd_empty, ==, NULL);
943*7877fdebSMatt Macy 
944*7877fdebSMatt Macy 	if (rr->rr_nempty > 0) {
945*7877fdebSMatt Macy 		rr->rr_abd_empty = abd_alloc_linear(rr->rr_nempty * skip_size,
946*7877fdebSMatt Macy 		    B_FALSE);
947*7877fdebSMatt Macy 	}
948*7877fdebSMatt Macy 
949*7877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
950*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
951*7877fdebSMatt Macy 
952*7877fdebSMatt Macy 		if (rc->rc_size == 0) {
953*7877fdebSMatt Macy 			/* empty data column (small read), add a skip sector */
954*7877fdebSMatt Macy 			ASSERT3U(skip_size, ==, parity_size);
955*7877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
956*7877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(rr->rr_abd_empty,
957*7877fdebSMatt Macy 			    skip_off, skip_size);
958*7877fdebSMatt Macy 			skip_off += skip_size;
959*7877fdebSMatt Macy 		} else if (rc->rc_size == parity_size) {
960*7877fdebSMatt Macy 			/* this is a "big column" */
961*7877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(zio->io_abd,
962*7877fdebSMatt Macy 			    abd_off, rc->rc_size);
963*7877fdebSMatt Macy 		} else {
964*7877fdebSMatt Macy 			/* short data column, add a skip sector */
965*7877fdebSMatt Macy 			ASSERT3U(rc->rc_size + skip_size, ==, parity_size);
966*7877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
967*7877fdebSMatt Macy 			rc->rc_abd = abd_alloc_gang_abd();
968*7877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
969*7877fdebSMatt Macy 			    zio->io_abd, abd_off, rc->rc_size), B_TRUE);
970*7877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
971*7877fdebSMatt Macy 			    rr->rr_abd_empty, skip_off, skip_size), B_TRUE);
972*7877fdebSMatt Macy 			skip_off += skip_size;
973*7877fdebSMatt Macy 		}
974*7877fdebSMatt Macy 
975*7877fdebSMatt Macy 		uint64_t abd_size = abd_get_size(rc->rc_abd);
976*7877fdebSMatt Macy 		ASSERT3U(abd_size, ==, abd_get_size(rr->rr_col[0].rc_abd));
977*7877fdebSMatt Macy 
978*7877fdebSMatt Macy 		/*
979*7877fdebSMatt Macy 		 * Increase rc_size so the skip ABD is included in subsequent
980*7877fdebSMatt Macy 		 * parity calculations.
981*7877fdebSMatt Macy 		 */
982*7877fdebSMatt Macy 		abd_off += rc->rc_size;
983*7877fdebSMatt Macy 		rc->rc_size = abd_size;
984*7877fdebSMatt Macy 	}
985*7877fdebSMatt Macy 
986*7877fdebSMatt Macy 	IMPLY(abd_offset != 0, abd_off == zio->io_size);
987*7877fdebSMatt Macy 	ASSERT3U(skip_off, ==, rr->rr_nempty * skip_size);
988*7877fdebSMatt Macy }
989*7877fdebSMatt Macy 
990*7877fdebSMatt Macy /*
991*7877fdebSMatt Macy  * Normal reads.  In this common case only the columns containing data
992*7877fdebSMatt Macy  * are read in to the zio ABDs.  Neither the parity columns or empty skip
993*7877fdebSMatt Macy  * sectors are read unless the checksum fails verification.  In which case
994*7877fdebSMatt Macy  * vdev_raidz_read_all() will call vdev_draid_map_alloc_empty() to expand
995*7877fdebSMatt Macy  * the raid map in order to allow reconstruction using the parity data and
996*7877fdebSMatt Macy  * skip sectors.
997*7877fdebSMatt Macy  */
998*7877fdebSMatt Macy static void
999*7877fdebSMatt Macy vdev_draid_map_alloc_read(zio_t *zio, uint64_t abd_offset, raidz_row_t *rr)
1000*7877fdebSMatt Macy {
1001*7877fdebSMatt Macy 	uint64_t abd_off = abd_offset;
1002*7877fdebSMatt Macy 
1003*7877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
1004*7877fdebSMatt Macy 
1005*7877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1006*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
1007*7877fdebSMatt Macy 
1008*7877fdebSMatt Macy 		if (rc->rc_size > 0) {
1009*7877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(zio->io_abd,
1010*7877fdebSMatt Macy 			    abd_off, rc->rc_size);
1011*7877fdebSMatt Macy 			abd_off += rc->rc_size;
1012*7877fdebSMatt Macy 		}
1013*7877fdebSMatt Macy 	}
1014*7877fdebSMatt Macy 
1015*7877fdebSMatt Macy 	IMPLY(abd_offset != 0, abd_off == zio->io_size);
1016*7877fdebSMatt Macy }
1017*7877fdebSMatt Macy 
1018*7877fdebSMatt Macy /*
1019*7877fdebSMatt Macy  * Converts a normal "read" raidz_row_t to a "scrub" raidz_row_t. The key
1020*7877fdebSMatt Macy  * difference is that an ABD is allocated to back skip sectors so they may
1021*7877fdebSMatt Macy  * be read in to memory, verified, and repaired if needed.
1022*7877fdebSMatt Macy  */
1023*7877fdebSMatt Macy void
1024*7877fdebSMatt Macy vdev_draid_map_alloc_empty(zio_t *zio, raidz_row_t *rr)
1025*7877fdebSMatt Macy {
1026*7877fdebSMatt Macy 	uint64_t skip_size = 1ULL << zio->io_vd->vdev_top->vdev_ashift;
1027*7877fdebSMatt Macy 	uint64_t parity_size = rr->rr_col[0].rc_size;
1028*7877fdebSMatt Macy 	uint64_t skip_off = 0;
1029*7877fdebSMatt Macy 
1030*7877fdebSMatt Macy 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
1031*7877fdebSMatt Macy 	ASSERT3P(rr->rr_abd_empty, ==, NULL);
1032*7877fdebSMatt Macy 
1033*7877fdebSMatt Macy 	if (rr->rr_nempty > 0) {
1034*7877fdebSMatt Macy 		rr->rr_abd_empty = abd_alloc_linear(rr->rr_nempty * skip_size,
1035*7877fdebSMatt Macy 		    B_FALSE);
1036*7877fdebSMatt Macy 	}
1037*7877fdebSMatt Macy 
1038*7877fdebSMatt Macy 	for (uint64_t c = rr->rr_firstdatacol; c < rr->rr_cols; c++) {
1039*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
1040*7877fdebSMatt Macy 
1041*7877fdebSMatt Macy 		if (rc->rc_size == 0) {
1042*7877fdebSMatt Macy 			/* empty data column (small read), add a skip sector */
1043*7877fdebSMatt Macy 			ASSERT3U(skip_size, ==, parity_size);
1044*7877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
1045*7877fdebSMatt Macy 			ASSERT3P(rc->rc_abd, ==, NULL);
1046*7877fdebSMatt Macy 			rc->rc_abd = abd_get_offset_size(rr->rr_abd_empty,
1047*7877fdebSMatt Macy 			    skip_off, skip_size);
1048*7877fdebSMatt Macy 			skip_off += skip_size;
1049*7877fdebSMatt Macy 		} else if (rc->rc_size == parity_size) {
1050*7877fdebSMatt Macy 			/* this is a "big column", nothing to add */
1051*7877fdebSMatt Macy 			ASSERT3P(rc->rc_abd, !=, NULL);
1052*7877fdebSMatt Macy 		} else {
1053*7877fdebSMatt Macy 			/* short data column, add a skip sector */
1054*7877fdebSMatt Macy 			ASSERT3U(rc->rc_size + skip_size, ==, parity_size);
1055*7877fdebSMatt Macy 			ASSERT3U(rr->rr_nempty, !=, 0);
1056*7877fdebSMatt Macy 			ASSERT3P(rc->rc_abd, !=, NULL);
1057*7877fdebSMatt Macy 			ASSERT(!abd_is_gang(rc->rc_abd));
1058*7877fdebSMatt Macy 			abd_t *read_abd = rc->rc_abd;
1059*7877fdebSMatt Macy 			rc->rc_abd = abd_alloc_gang_abd();
1060*7877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, read_abd, B_TRUE);
1061*7877fdebSMatt Macy 			abd_gang_add(rc->rc_abd, abd_get_offset_size(
1062*7877fdebSMatt Macy 			    rr->rr_abd_empty, skip_off, skip_size), B_TRUE);
1063*7877fdebSMatt Macy 			skip_off += skip_size;
1064*7877fdebSMatt Macy 		}
1065*7877fdebSMatt Macy 
1066*7877fdebSMatt Macy 		/*
1067*7877fdebSMatt Macy 		 * Increase rc_size so the empty ABD is included in subsequent
1068*7877fdebSMatt Macy 		 * parity calculations.
1069*7877fdebSMatt Macy 		 */
1070*7877fdebSMatt Macy 		rc->rc_size = parity_size;
1071*7877fdebSMatt Macy 	}
1072*7877fdebSMatt Macy 
1073*7877fdebSMatt Macy 	ASSERT3U(skip_off, ==, rr->rr_nempty * skip_size);
1074*7877fdebSMatt Macy }
1075*7877fdebSMatt Macy 
1076*7877fdebSMatt Macy /*
1077*7877fdebSMatt Macy  * Given a logical address within a dRAID configuration, return the physical
1078*7877fdebSMatt Macy  * address on the first drive in the group that this address maps to
1079*7877fdebSMatt Macy  * (at position 'start' in permutation number 'perm').
1080*7877fdebSMatt Macy  */
1081*7877fdebSMatt Macy static uint64_t
1082*7877fdebSMatt Macy vdev_draid_logical_to_physical(vdev_t *vd, uint64_t logical_offset,
1083*7877fdebSMatt Macy     uint64_t *perm, uint64_t *start)
1084*7877fdebSMatt Macy {
1085*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1086*7877fdebSMatt Macy 
1087*7877fdebSMatt Macy 	/* b is the dRAID (parent) sector offset. */
1088*7877fdebSMatt Macy 	uint64_t ashift = vd->vdev_top->vdev_ashift;
1089*7877fdebSMatt Macy 	uint64_t b_offset = logical_offset >> ashift;
1090*7877fdebSMatt Macy 
1091*7877fdebSMatt Macy 	/*
1092*7877fdebSMatt Macy 	 * The height of a row in units of the vdev's minimum sector size.
1093*7877fdebSMatt Macy 	 * This is the amount of data written to each disk of each group
1094*7877fdebSMatt Macy 	 * in a given permutation.
1095*7877fdebSMatt Macy 	 */
1096*7877fdebSMatt Macy 	uint64_t rowheight_sectors = VDEV_DRAID_ROWHEIGHT >> ashift;
1097*7877fdebSMatt Macy 
1098*7877fdebSMatt Macy 	/*
1099*7877fdebSMatt Macy 	 * We cycle through a disk permutation every groupsz * ngroups chunk
1100*7877fdebSMatt Macy 	 * of address space. Note that ngroups * groupsz must be a multiple
1101*7877fdebSMatt Macy 	 * of the number of data drives (ndisks) in order to guarantee
1102*7877fdebSMatt Macy 	 * alignment. So, for example, if our row height is 16MB, our group
1103*7877fdebSMatt Macy 	 * size is 10, and there are 13 data drives in the draid, then ngroups
1104*7877fdebSMatt Macy 	 * will be 13, we will change permutation every 2.08GB and each
1105*7877fdebSMatt Macy 	 * disk will have 160MB of data per chunk.
1106*7877fdebSMatt Macy 	 */
1107*7877fdebSMatt Macy 	uint64_t groupwidth = vdc->vdc_groupwidth;
1108*7877fdebSMatt Macy 	uint64_t ngroups = vdc->vdc_ngroups;
1109*7877fdebSMatt Macy 	uint64_t ndisks = vdc->vdc_ndisks;
1110*7877fdebSMatt Macy 
1111*7877fdebSMatt Macy 	/*
1112*7877fdebSMatt Macy 	 * groupstart is where the group this IO will land in "starts" in
1113*7877fdebSMatt Macy 	 * the permutation array.
1114*7877fdebSMatt Macy 	 */
1115*7877fdebSMatt Macy 	uint64_t group = logical_offset / vdc->vdc_groupsz;
1116*7877fdebSMatt Macy 	uint64_t groupstart = (group * groupwidth) % ndisks;
1117*7877fdebSMatt Macy 	ASSERT3U(groupstart + groupwidth, <=, ndisks + groupstart);
1118*7877fdebSMatt Macy 	*start = groupstart;
1119*7877fdebSMatt Macy 
1120*7877fdebSMatt Macy 	/* b_offset is the sector offset within a group chunk */
1121*7877fdebSMatt Macy 	b_offset = b_offset % (rowheight_sectors * groupwidth);
1122*7877fdebSMatt Macy 	ASSERT0(b_offset % groupwidth);
1123*7877fdebSMatt Macy 
1124*7877fdebSMatt Macy 	/*
1125*7877fdebSMatt Macy 	 * Find the starting byte offset on each child vdev:
1126*7877fdebSMatt Macy 	 * - within a permutation there are ngroups groups spread over the
1127*7877fdebSMatt Macy 	 *   rows, where each row covers a slice portion of the disk
1128*7877fdebSMatt Macy 	 * - each permutation has (groupwidth * ngroups) / ndisks rows
1129*7877fdebSMatt Macy 	 * - so each permutation covers rows * slice portion of the disk
1130*7877fdebSMatt Macy 	 * - so we need to find the row where this IO group target begins
1131*7877fdebSMatt Macy 	 */
1132*7877fdebSMatt Macy 	*perm = group / ngroups;
1133*7877fdebSMatt Macy 	uint64_t row = (*perm * ((groupwidth * ngroups) / ndisks)) +
1134*7877fdebSMatt Macy 	    (((group % ngroups) * groupwidth) / ndisks);
1135*7877fdebSMatt Macy 
1136*7877fdebSMatt Macy 	return (((rowheight_sectors * row) +
1137*7877fdebSMatt Macy 	    (b_offset / groupwidth)) << ashift);
1138*7877fdebSMatt Macy }
1139*7877fdebSMatt Macy 
1140*7877fdebSMatt Macy static uint64_t
1141*7877fdebSMatt Macy vdev_draid_map_alloc_row(zio_t *zio, raidz_row_t **rrp, uint64_t io_offset,
1142*7877fdebSMatt Macy     uint64_t abd_offset, uint64_t abd_size)
1143*7877fdebSMatt Macy {
1144*7877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
1145*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1146*7877fdebSMatt Macy 	uint64_t ashift = vd->vdev_top->vdev_ashift;
1147*7877fdebSMatt Macy 	uint64_t io_size = abd_size;
1148*7877fdebSMatt Macy 	uint64_t io_asize = vdev_draid_asize(vd, io_size);
1149*7877fdebSMatt Macy 	uint64_t group = vdev_draid_offset_to_group(vd, io_offset);
1150*7877fdebSMatt Macy 	uint64_t start_offset = vdev_draid_group_to_offset(vd, group + 1);
1151*7877fdebSMatt Macy 
1152*7877fdebSMatt Macy 	/*
1153*7877fdebSMatt Macy 	 * Limit the io_size to the space remaining in the group.  A second
1154*7877fdebSMatt Macy 	 * row in the raidz_map_t is created for the remainder.
1155*7877fdebSMatt Macy 	 */
1156*7877fdebSMatt Macy 	if (io_offset + io_asize > start_offset) {
1157*7877fdebSMatt Macy 		io_size = vdev_draid_asize_to_psize(vd,
1158*7877fdebSMatt Macy 		    start_offset - io_offset);
1159*7877fdebSMatt Macy 	}
1160*7877fdebSMatt Macy 
1161*7877fdebSMatt Macy 	/*
1162*7877fdebSMatt Macy 	 * At most a block may span the logical end of one group and the start
1163*7877fdebSMatt Macy 	 * of the next group. Therefore, at the end of a group the io_size must
1164*7877fdebSMatt Macy 	 * span the group width evenly and the remainder must be aligned to the
1165*7877fdebSMatt Macy 	 * start of the next group.
1166*7877fdebSMatt Macy 	 */
1167*7877fdebSMatt Macy 	IMPLY(abd_offset == 0 && io_size < zio->io_size,
1168*7877fdebSMatt Macy 	    (io_asize >> ashift) % vdc->vdc_groupwidth == 0);
1169*7877fdebSMatt Macy 	IMPLY(abd_offset != 0,
1170*7877fdebSMatt Macy 	    vdev_draid_group_to_offset(vd, group) == io_offset);
1171*7877fdebSMatt Macy 
1172*7877fdebSMatt Macy 	/* Lookup starting byte offset on each child vdev */
1173*7877fdebSMatt Macy 	uint64_t groupstart, perm;
1174*7877fdebSMatt Macy 	uint64_t physical_offset = vdev_draid_logical_to_physical(vd,
1175*7877fdebSMatt Macy 	    io_offset, &perm, &groupstart);
1176*7877fdebSMatt Macy 
1177*7877fdebSMatt Macy 	/*
1178*7877fdebSMatt Macy 	 * If there is less than groupwidth drives available after the group
1179*7877fdebSMatt Macy 	 * start, the group is going to wrap onto the next row. 'wrap' is the
1180*7877fdebSMatt Macy 	 * group disk number that starts on the next row.
1181*7877fdebSMatt Macy 	 */
1182*7877fdebSMatt Macy 	uint64_t ndisks = vdc->vdc_ndisks;
1183*7877fdebSMatt Macy 	uint64_t groupwidth = vdc->vdc_groupwidth;
1184*7877fdebSMatt Macy 	uint64_t wrap = groupwidth;
1185*7877fdebSMatt Macy 
1186*7877fdebSMatt Macy 	if (groupstart + groupwidth > ndisks)
1187*7877fdebSMatt Macy 		wrap = ndisks - groupstart;
1188*7877fdebSMatt Macy 
1189*7877fdebSMatt Macy 	/* The io size in units of the vdev's minimum sector size. */
1190*7877fdebSMatt Macy 	const uint64_t psize = io_size >> ashift;
1191*7877fdebSMatt Macy 
1192*7877fdebSMatt Macy 	/*
1193*7877fdebSMatt Macy 	 * "Quotient": The number of data sectors for this stripe on all but
1194*7877fdebSMatt Macy 	 * the "big column" child vdevs that also contain "remainder" data.
1195*7877fdebSMatt Macy 	 */
1196*7877fdebSMatt Macy 	uint64_t q = psize / vdc->vdc_ndata;
1197*7877fdebSMatt Macy 
1198*7877fdebSMatt Macy 	/*
1199*7877fdebSMatt Macy 	 * "Remainder": The number of partial stripe data sectors in this I/O.
1200*7877fdebSMatt Macy 	 * This will add a sector to some, but not all, child vdevs.
1201*7877fdebSMatt Macy 	 */
1202*7877fdebSMatt Macy 	uint64_t r = psize - q * vdc->vdc_ndata;
1203*7877fdebSMatt Macy 
1204*7877fdebSMatt Macy 	/* The number of "big columns" - those which contain remainder data. */
1205*7877fdebSMatt Macy 	uint64_t bc = (r == 0 ? 0 : r + vdc->vdc_nparity);
1206*7877fdebSMatt Macy 	ASSERT3U(bc, <, groupwidth);
1207*7877fdebSMatt Macy 
1208*7877fdebSMatt Macy 	/* The total number of data and parity sectors for this I/O. */
1209*7877fdebSMatt Macy 	uint64_t tot = psize + (vdc->vdc_nparity * (q + (r == 0 ? 0 : 1)));
1210*7877fdebSMatt Macy 
1211*7877fdebSMatt Macy 	raidz_row_t *rr;
1212*7877fdebSMatt Macy 	rr = kmem_alloc(offsetof(raidz_row_t, rr_col[groupwidth]), KM_SLEEP);
1213*7877fdebSMatt Macy 	rr->rr_cols = groupwidth;
1214*7877fdebSMatt Macy 	rr->rr_scols = groupwidth;
1215*7877fdebSMatt Macy 	rr->rr_bigcols = bc;
1216*7877fdebSMatt Macy 	rr->rr_missingdata = 0;
1217*7877fdebSMatt Macy 	rr->rr_missingparity = 0;
1218*7877fdebSMatt Macy 	rr->rr_firstdatacol = vdc->vdc_nparity;
1219*7877fdebSMatt Macy 	rr->rr_abd_copy = NULL;
1220*7877fdebSMatt Macy 	rr->rr_abd_empty = NULL;
1221*7877fdebSMatt Macy #ifdef ZFS_DEBUG
1222*7877fdebSMatt Macy 	rr->rr_offset = io_offset;
1223*7877fdebSMatt Macy 	rr->rr_size = io_size;
1224*7877fdebSMatt Macy #endif
1225*7877fdebSMatt Macy 	*rrp = rr;
1226*7877fdebSMatt Macy 
1227*7877fdebSMatt Macy 	uint8_t *base;
1228*7877fdebSMatt Macy 	uint64_t iter, asize = 0;
1229*7877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
1230*7877fdebSMatt Macy 	for (uint64_t i = 0; i < groupwidth; i++) {
1231*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[i];
1232*7877fdebSMatt Macy 		uint64_t c = (groupstart + i) % ndisks;
1233*7877fdebSMatt Macy 
1234*7877fdebSMatt Macy 		/* increment the offset if we wrap to the next row */
1235*7877fdebSMatt Macy 		if (i == wrap)
1236*7877fdebSMatt Macy 			physical_offset += VDEV_DRAID_ROWHEIGHT;
1237*7877fdebSMatt Macy 
1238*7877fdebSMatt Macy 		rc->rc_devidx = vdev_draid_permute_id(vdc, base, iter, c);
1239*7877fdebSMatt Macy 		rc->rc_offset = physical_offset;
1240*7877fdebSMatt Macy 		rc->rc_abd = NULL;
1241*7877fdebSMatt Macy 		rc->rc_gdata = NULL;
1242*7877fdebSMatt Macy 		rc->rc_orig_data = NULL;
1243*7877fdebSMatt Macy 		rc->rc_error = 0;
1244*7877fdebSMatt Macy 		rc->rc_tried = 0;
1245*7877fdebSMatt Macy 		rc->rc_skipped = 0;
1246*7877fdebSMatt Macy 		rc->rc_repair = 0;
1247*7877fdebSMatt Macy 		rc->rc_need_orig_restore = B_FALSE;
1248*7877fdebSMatt Macy 
1249*7877fdebSMatt Macy 		if (q == 0 && i >= bc)
1250*7877fdebSMatt Macy 			rc->rc_size = 0;
1251*7877fdebSMatt Macy 		else if (i < bc)
1252*7877fdebSMatt Macy 			rc->rc_size = (q + 1) << ashift;
1253*7877fdebSMatt Macy 		else
1254*7877fdebSMatt Macy 			rc->rc_size = q << ashift;
1255*7877fdebSMatt Macy 
1256*7877fdebSMatt Macy 		asize += rc->rc_size;
1257*7877fdebSMatt Macy 	}
1258*7877fdebSMatt Macy 
1259*7877fdebSMatt Macy 	ASSERT3U(asize, ==, tot << ashift);
1260*7877fdebSMatt Macy 	rr->rr_nempty = roundup(tot, groupwidth) - tot;
1261*7877fdebSMatt Macy 	IMPLY(bc > 0, rr->rr_nempty == groupwidth - bc);
1262*7877fdebSMatt Macy 
1263*7877fdebSMatt Macy 	/* Allocate buffers for the parity columns */
1264*7877fdebSMatt Macy 	for (uint64_t c = 0; c < rr->rr_firstdatacol; c++) {
1265*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
1266*7877fdebSMatt Macy 		rc->rc_abd = abd_alloc_linear(rc->rc_size, B_FALSE);
1267*7877fdebSMatt Macy 	}
1268*7877fdebSMatt Macy 
1269*7877fdebSMatt Macy 	/*
1270*7877fdebSMatt Macy 	 * Map buffers for data columns and allocate/map buffers for skip
1271*7877fdebSMatt Macy 	 * sectors.  There are three distinct cases for dRAID which are
1272*7877fdebSMatt Macy 	 * required to support sequential rebuild.
1273*7877fdebSMatt Macy 	 */
1274*7877fdebSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
1275*7877fdebSMatt Macy 		vdev_draid_map_alloc_write(zio, abd_offset, rr);
1276*7877fdebSMatt Macy 	} else if ((rr->rr_nempty > 0) &&
1277*7877fdebSMatt Macy 	    (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
1278*7877fdebSMatt Macy 		vdev_draid_map_alloc_scrub(zio, abd_offset, rr);
1279*7877fdebSMatt Macy 	} else {
1280*7877fdebSMatt Macy 		ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
1281*7877fdebSMatt Macy 		vdev_draid_map_alloc_read(zio, abd_offset, rr);
1282*7877fdebSMatt Macy 	}
1283*7877fdebSMatt Macy 
1284*7877fdebSMatt Macy 	return (io_size);
1285*7877fdebSMatt Macy }
1286*7877fdebSMatt Macy 
1287*7877fdebSMatt Macy /*
1288*7877fdebSMatt Macy  * Allocate the raidz mapping to be applied to the dRAID I/O.  The parity
1289*7877fdebSMatt Macy  * calculations for dRAID are identical to raidz however there are a few
1290*7877fdebSMatt Macy  * differences in the layout.
1291*7877fdebSMatt Macy  *
1292*7877fdebSMatt Macy  * - dRAID always allocates a full stripe width. Any extra sectors due
1293*7877fdebSMatt Macy  *   this padding are zero filled and written to disk. They will be read
1294*7877fdebSMatt Macy  *   back during a scrub or repair operation since they are included in
1295*7877fdebSMatt Macy  *   the parity calculation. This property enables sequential resilvering.
1296*7877fdebSMatt Macy  *
1297*7877fdebSMatt Macy  * - When the block at the logical offset spans redundancy groups then two
1298*7877fdebSMatt Macy  *   rows are allocated in the raidz_map_t. One row resides at the end of
1299*7877fdebSMatt Macy  *   the first group and the other at the start of the following group.
1300*7877fdebSMatt Macy  */
1301*7877fdebSMatt Macy static raidz_map_t *
1302*7877fdebSMatt Macy vdev_draid_map_alloc(zio_t *zio)
1303*7877fdebSMatt Macy {
1304*7877fdebSMatt Macy 	raidz_row_t *rr[2];
1305*7877fdebSMatt Macy 	uint64_t abd_offset = 0;
1306*7877fdebSMatt Macy 	uint64_t abd_size = zio->io_size;
1307*7877fdebSMatt Macy 	uint64_t io_offset = zio->io_offset;
1308*7877fdebSMatt Macy 	uint64_t size;
1309*7877fdebSMatt Macy 	int nrows = 1;
1310*7877fdebSMatt Macy 
1311*7877fdebSMatt Macy 	size = vdev_draid_map_alloc_row(zio, &rr[0], io_offset,
1312*7877fdebSMatt Macy 	    abd_offset, abd_size);
1313*7877fdebSMatt Macy 	if (size < abd_size) {
1314*7877fdebSMatt Macy 		vdev_t *vd = zio->io_vd;
1315*7877fdebSMatt Macy 
1316*7877fdebSMatt Macy 		io_offset += vdev_draid_asize(vd, size);
1317*7877fdebSMatt Macy 		abd_offset += size;
1318*7877fdebSMatt Macy 		abd_size -= size;
1319*7877fdebSMatt Macy 		nrows++;
1320*7877fdebSMatt Macy 
1321*7877fdebSMatt Macy 		ASSERT3U(io_offset, ==, vdev_draid_group_to_offset(
1322*7877fdebSMatt Macy 		    vd, vdev_draid_offset_to_group(vd, io_offset)));
1323*7877fdebSMatt Macy 		ASSERT3U(abd_offset, <, zio->io_size);
1324*7877fdebSMatt Macy 		ASSERT3U(abd_size, !=, 0);
1325*7877fdebSMatt Macy 
1326*7877fdebSMatt Macy 		size = vdev_draid_map_alloc_row(zio, &rr[1],
1327*7877fdebSMatt Macy 		    io_offset, abd_offset, abd_size);
1328*7877fdebSMatt Macy 		VERIFY3U(size, ==, abd_size);
1329*7877fdebSMatt Macy 	}
1330*7877fdebSMatt Macy 
1331*7877fdebSMatt Macy 	raidz_map_t *rm;
1332*7877fdebSMatt Macy 	rm = kmem_zalloc(offsetof(raidz_map_t, rm_row[nrows]), KM_SLEEP);
1333*7877fdebSMatt Macy 	rm->rm_ops = vdev_raidz_math_get_ops();
1334*7877fdebSMatt Macy 	rm->rm_nrows = nrows;
1335*7877fdebSMatt Macy 	rm->rm_row[0] = rr[0];
1336*7877fdebSMatt Macy 	if (nrows == 2)
1337*7877fdebSMatt Macy 		rm->rm_row[1] = rr[1];
1338*7877fdebSMatt Macy 
1339*7877fdebSMatt Macy 	zio->io_vsd = rm;
1340*7877fdebSMatt Macy 	zio->io_vsd_ops = &vdev_draid_vsd_ops;
1341*7877fdebSMatt Macy 
1342*7877fdebSMatt Macy 	return (rm);
1343*7877fdebSMatt Macy }
1344*7877fdebSMatt Macy 
1345*7877fdebSMatt Macy /*
1346*7877fdebSMatt Macy  * Given an offset into a dRAID return the next group width aligned offset
1347*7877fdebSMatt Macy  * which can be used to start an allocation.
1348*7877fdebSMatt Macy  */
1349*7877fdebSMatt Macy static uint64_t
1350*7877fdebSMatt Macy vdev_draid_get_astart(vdev_t *vd, const uint64_t start)
1351*7877fdebSMatt Macy {
1352*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1353*7877fdebSMatt Macy 
1354*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1355*7877fdebSMatt Macy 
1356*7877fdebSMatt Macy 	return (roundup(start, vdc->vdc_groupwidth << vd->vdev_ashift));
1357*7877fdebSMatt Macy }
1358*7877fdebSMatt Macy 
1359*7877fdebSMatt Macy /*
1360*7877fdebSMatt Macy  * Allocatable space for dRAID is (children - nspares) * sizeof(smallest child)
1361*7877fdebSMatt Macy  * rounded down to the last full slice.  So each child must provide at least
1362*7877fdebSMatt Macy  * 1 / (children - nspares) of its asize.
1363*7877fdebSMatt Macy  */
1364*7877fdebSMatt Macy static uint64_t
1365*7877fdebSMatt Macy vdev_draid_min_asize(vdev_t *vd)
1366*7877fdebSMatt Macy {
1367*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1368*7877fdebSMatt Macy 
1369*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1370*7877fdebSMatt Macy 
1371*7877fdebSMatt Macy 	return ((vd->vdev_min_asize + vdc->vdc_ndisks - 1) / (vdc->vdc_ndisks));
1372*7877fdebSMatt Macy }
1373*7877fdebSMatt Macy 
1374*7877fdebSMatt Macy /*
1375*7877fdebSMatt Macy  * When using dRAID the minimum allocation size is determined by the number
1376*7877fdebSMatt Macy  * of data disks in the redundancy group.  Full stripes are always used.
1377*7877fdebSMatt Macy  */
1378*7877fdebSMatt Macy static uint64_t
1379*7877fdebSMatt Macy vdev_draid_min_alloc(vdev_t *vd)
1380*7877fdebSMatt Macy {
1381*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1382*7877fdebSMatt Macy 
1383*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1384*7877fdebSMatt Macy 
1385*7877fdebSMatt Macy 	return (vdc->vdc_ndata << vd->vdev_ashift);
1386*7877fdebSMatt Macy }
1387*7877fdebSMatt Macy 
1388*7877fdebSMatt Macy /*
1389*7877fdebSMatt Macy  * Returns true if the txg range does not exist on any leaf vdev.
1390*7877fdebSMatt Macy  *
1391*7877fdebSMatt Macy  * A dRAID spare does not fit into the DTL model. While it has child vdevs
1392*7877fdebSMatt Macy  * there is no redundancy among them, and the effective child vdev is
1393*7877fdebSMatt Macy  * determined by offset. Essentially we do a vdev_dtl_reassess() on the
1394*7877fdebSMatt Macy  * fly by replacing a dRAID spare with the child vdev under the offset.
1395*7877fdebSMatt Macy  * Note that it is a recursive process because the child vdev can be
1396*7877fdebSMatt Macy  * another dRAID spare and so on.
1397*7877fdebSMatt Macy  */
1398*7877fdebSMatt Macy boolean_t
1399*7877fdebSMatt Macy vdev_draid_missing(vdev_t *vd, uint64_t physical_offset, uint64_t txg,
1400*7877fdebSMatt Macy     uint64_t size)
1401*7877fdebSMatt Macy {
1402*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_spare_ops ||
1403*7877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops) {
1404*7877fdebSMatt Macy 		/*
1405*7877fdebSMatt Macy 		 * Check all of the readable children, if any child
1406*7877fdebSMatt Macy 		 * contains the txg range the data it is not missing.
1407*7877fdebSMatt Macy 		 */
1408*7877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
1409*7877fdebSMatt Macy 			vdev_t *cvd = vd->vdev_child[c];
1410*7877fdebSMatt Macy 
1411*7877fdebSMatt Macy 			if (!vdev_readable(cvd))
1412*7877fdebSMatt Macy 				continue;
1413*7877fdebSMatt Macy 
1414*7877fdebSMatt Macy 			if (!vdev_draid_missing(cvd, physical_offset,
1415*7877fdebSMatt Macy 			    txg, size))
1416*7877fdebSMatt Macy 				return (B_FALSE);
1417*7877fdebSMatt Macy 		}
1418*7877fdebSMatt Macy 
1419*7877fdebSMatt Macy 		return (B_TRUE);
1420*7877fdebSMatt Macy 	}
1421*7877fdebSMatt Macy 
1422*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
1423*7877fdebSMatt Macy 		/*
1424*7877fdebSMatt Macy 		 * When sequentially resilvering we don't have a proper
1425*7877fdebSMatt Macy 		 * txg range so instead we must presume all txgs are
1426*7877fdebSMatt Macy 		 * missing on this vdev until the resilver completes.
1427*7877fdebSMatt Macy 		 */
1428*7877fdebSMatt Macy 		if (vd->vdev_rebuild_txg != 0)
1429*7877fdebSMatt Macy 			return (B_TRUE);
1430*7877fdebSMatt Macy 
1431*7877fdebSMatt Macy 		/*
1432*7877fdebSMatt Macy 		 * DTL_MISSING is set for all prior txgs when a resilver
1433*7877fdebSMatt Macy 		 * is started in spa_vdev_attach().
1434*7877fdebSMatt Macy 		 */
1435*7877fdebSMatt Macy 		if (vdev_dtl_contains(vd, DTL_MISSING, txg, size))
1436*7877fdebSMatt Macy 			return (B_TRUE);
1437*7877fdebSMatt Macy 
1438*7877fdebSMatt Macy 		/*
1439*7877fdebSMatt Macy 		 * Consult the DTL on the relevant vdev. Either a vdev
1440*7877fdebSMatt Macy 		 * leaf or spare/replace mirror child may be returned so
1441*7877fdebSMatt Macy 		 * we must recursively call vdev_draid_missing_impl().
1442*7877fdebSMatt Macy 		 */
1443*7877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
1444*7877fdebSMatt Macy 		if (vd == NULL)
1445*7877fdebSMatt Macy 			return (B_TRUE);
1446*7877fdebSMatt Macy 
1447*7877fdebSMatt Macy 		return (vdev_draid_missing(vd, physical_offset,
1448*7877fdebSMatt Macy 		    txg, size));
1449*7877fdebSMatt Macy 	}
1450*7877fdebSMatt Macy 
1451*7877fdebSMatt Macy 	return (vdev_dtl_contains(vd, DTL_MISSING, txg, size));
1452*7877fdebSMatt Macy }
1453*7877fdebSMatt Macy 
1454*7877fdebSMatt Macy /*
1455*7877fdebSMatt Macy  * Returns true if the txg is only partially replicated on the leaf vdevs.
1456*7877fdebSMatt Macy  */
1457*7877fdebSMatt Macy static boolean_t
1458*7877fdebSMatt Macy vdev_draid_partial(vdev_t *vd, uint64_t physical_offset, uint64_t txg,
1459*7877fdebSMatt Macy     uint64_t size)
1460*7877fdebSMatt Macy {
1461*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_spare_ops ||
1462*7877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops) {
1463*7877fdebSMatt Macy 		/*
1464*7877fdebSMatt Macy 		 * Check all of the readable children, if any child is
1465*7877fdebSMatt Macy 		 * missing the txg range then it is partially replicated.
1466*7877fdebSMatt Macy 		 */
1467*7877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
1468*7877fdebSMatt Macy 			vdev_t *cvd = vd->vdev_child[c];
1469*7877fdebSMatt Macy 
1470*7877fdebSMatt Macy 			if (!vdev_readable(cvd))
1471*7877fdebSMatt Macy 				continue;
1472*7877fdebSMatt Macy 
1473*7877fdebSMatt Macy 			if (vdev_draid_partial(cvd, physical_offset, txg, size))
1474*7877fdebSMatt Macy 				return (B_TRUE);
1475*7877fdebSMatt Macy 		}
1476*7877fdebSMatt Macy 
1477*7877fdebSMatt Macy 		return (B_FALSE);
1478*7877fdebSMatt Macy 	}
1479*7877fdebSMatt Macy 
1480*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
1481*7877fdebSMatt Macy 		/*
1482*7877fdebSMatt Macy 		 * When sequentially resilvering we don't have a proper
1483*7877fdebSMatt Macy 		 * txg range so instead we must presume all txgs are
1484*7877fdebSMatt Macy 		 * missing on this vdev until the resilver completes.
1485*7877fdebSMatt Macy 		 */
1486*7877fdebSMatt Macy 		if (vd->vdev_rebuild_txg != 0)
1487*7877fdebSMatt Macy 			return (B_TRUE);
1488*7877fdebSMatt Macy 
1489*7877fdebSMatt Macy 		/*
1490*7877fdebSMatt Macy 		 * DTL_MISSING is set for all prior txgs when a resilver
1491*7877fdebSMatt Macy 		 * is started in spa_vdev_attach().
1492*7877fdebSMatt Macy 		 */
1493*7877fdebSMatt Macy 		if (vdev_dtl_contains(vd, DTL_MISSING, txg, size))
1494*7877fdebSMatt Macy 			return (B_TRUE);
1495*7877fdebSMatt Macy 
1496*7877fdebSMatt Macy 		/*
1497*7877fdebSMatt Macy 		 * Consult the DTL on the relevant vdev. Either a vdev
1498*7877fdebSMatt Macy 		 * leaf or spare/replace mirror child may be returned so
1499*7877fdebSMatt Macy 		 * we must recursively call vdev_draid_missing_impl().
1500*7877fdebSMatt Macy 		 */
1501*7877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
1502*7877fdebSMatt Macy 		if (vd == NULL)
1503*7877fdebSMatt Macy 			return (B_TRUE);
1504*7877fdebSMatt Macy 
1505*7877fdebSMatt Macy 		return (vdev_draid_partial(vd, physical_offset, txg, size));
1506*7877fdebSMatt Macy 	}
1507*7877fdebSMatt Macy 
1508*7877fdebSMatt Macy 	return (vdev_dtl_contains(vd, DTL_MISSING, txg, size));
1509*7877fdebSMatt Macy }
1510*7877fdebSMatt Macy 
1511*7877fdebSMatt Macy /*
1512*7877fdebSMatt Macy  * Determine if the vdev is readable at the given offset.
1513*7877fdebSMatt Macy  */
1514*7877fdebSMatt Macy boolean_t
1515*7877fdebSMatt Macy vdev_draid_readable(vdev_t *vd, uint64_t physical_offset)
1516*7877fdebSMatt Macy {
1517*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
1518*7877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
1519*7877fdebSMatt Macy 		if (vd == NULL)
1520*7877fdebSMatt Macy 			return (B_FALSE);
1521*7877fdebSMatt Macy 	}
1522*7877fdebSMatt Macy 
1523*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_spare_ops ||
1524*7877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops) {
1525*7877fdebSMatt Macy 
1526*7877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
1527*7877fdebSMatt Macy 			vdev_t *cvd = vd->vdev_child[c];
1528*7877fdebSMatt Macy 
1529*7877fdebSMatt Macy 			if (!vdev_readable(cvd))
1530*7877fdebSMatt Macy 				continue;
1531*7877fdebSMatt Macy 
1532*7877fdebSMatt Macy 			if (vdev_draid_readable(cvd, physical_offset))
1533*7877fdebSMatt Macy 				return (B_TRUE);
1534*7877fdebSMatt Macy 		}
1535*7877fdebSMatt Macy 
1536*7877fdebSMatt Macy 		return (B_FALSE);
1537*7877fdebSMatt Macy 	}
1538*7877fdebSMatt Macy 
1539*7877fdebSMatt Macy 	return (vdev_readable(vd));
1540*7877fdebSMatt Macy }
1541*7877fdebSMatt Macy 
1542*7877fdebSMatt Macy /*
1543*7877fdebSMatt Macy  * Returns the first distributed spare found under the provided vdev tree.
1544*7877fdebSMatt Macy  */
1545*7877fdebSMatt Macy static vdev_t *
1546*7877fdebSMatt Macy vdev_draid_find_spare(vdev_t *vd)
1547*7877fdebSMatt Macy {
1548*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops)
1549*7877fdebSMatt Macy 		return (vd);
1550*7877fdebSMatt Macy 
1551*7877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
1552*7877fdebSMatt Macy 		vdev_t *svd = vdev_draid_find_spare(vd->vdev_child[c]);
1553*7877fdebSMatt Macy 		if (svd != NULL)
1554*7877fdebSMatt Macy 			return (svd);
1555*7877fdebSMatt Macy 	}
1556*7877fdebSMatt Macy 
1557*7877fdebSMatt Macy 	return (NULL);
1558*7877fdebSMatt Macy }
1559*7877fdebSMatt Macy 
1560*7877fdebSMatt Macy /*
1561*7877fdebSMatt Macy  * Returns B_TRUE if the passed in vdev is currently "faulted".
1562*7877fdebSMatt Macy  * Faulted, in this context, means that the vdev represents a
1563*7877fdebSMatt Macy  * replacing or sparing vdev tree.
1564*7877fdebSMatt Macy  */
1565*7877fdebSMatt Macy static boolean_t
1566*7877fdebSMatt Macy vdev_draid_faulted(vdev_t *vd, uint64_t physical_offset)
1567*7877fdebSMatt Macy {
1568*7877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops) {
1569*7877fdebSMatt Macy 		vd = vdev_draid_spare_get_child(vd, physical_offset);
1570*7877fdebSMatt Macy 		if (vd == NULL)
1571*7877fdebSMatt Macy 			return (B_FALSE);
1572*7877fdebSMatt Macy 
1573*7877fdebSMatt Macy 		/*
1574*7877fdebSMatt Macy 		 * After resolving the distributed spare to a leaf vdev
1575*7877fdebSMatt Macy 		 * check the parent to determine if it's "faulted".
1576*7877fdebSMatt Macy 		 */
1577*7877fdebSMatt Macy 		vd = vd->vdev_parent;
1578*7877fdebSMatt Macy 	}
1579*7877fdebSMatt Macy 
1580*7877fdebSMatt Macy 	return (vd->vdev_ops == &vdev_replacing_ops ||
1581*7877fdebSMatt Macy 	    vd->vdev_ops == &vdev_spare_ops);
1582*7877fdebSMatt Macy }
1583*7877fdebSMatt Macy 
1584*7877fdebSMatt Macy /*
1585*7877fdebSMatt Macy  * Determine if the dRAID block at the logical offset is degraded.
1586*7877fdebSMatt Macy  * Used by sequential resilver.
1587*7877fdebSMatt Macy  */
1588*7877fdebSMatt Macy static boolean_t
1589*7877fdebSMatt Macy vdev_draid_group_degraded(vdev_t *vd, uint64_t offset)
1590*7877fdebSMatt Macy {
1591*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1592*7877fdebSMatt Macy 
1593*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1594*7877fdebSMatt Macy 	ASSERT3U(vdev_draid_get_astart(vd, offset), ==, offset);
1595*7877fdebSMatt Macy 
1596*7877fdebSMatt Macy 	uint64_t groupstart, perm;
1597*7877fdebSMatt Macy 	uint64_t physical_offset = vdev_draid_logical_to_physical(vd,
1598*7877fdebSMatt Macy 	    offset, &perm, &groupstart);
1599*7877fdebSMatt Macy 
1600*7877fdebSMatt Macy 	uint8_t *base;
1601*7877fdebSMatt Macy 	uint64_t iter;
1602*7877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
1603*7877fdebSMatt Macy 
1604*7877fdebSMatt Macy 	for (uint64_t i = 0; i < vdc->vdc_groupwidth; i++) {
1605*7877fdebSMatt Macy 		uint64_t c = (groupstart + i) % vdc->vdc_ndisks;
1606*7877fdebSMatt Macy 		uint64_t cid = vdev_draid_permute_id(vdc, base, iter, c);
1607*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[cid];
1608*7877fdebSMatt Macy 
1609*7877fdebSMatt Macy 		/* Group contains a faulted vdev. */
1610*7877fdebSMatt Macy 		if (vdev_draid_faulted(cvd, physical_offset))
1611*7877fdebSMatt Macy 			return (B_TRUE);
1612*7877fdebSMatt Macy 
1613*7877fdebSMatt Macy 		/*
1614*7877fdebSMatt Macy 		 * Always check groups with active distributed spares
1615*7877fdebSMatt Macy 		 * because any vdev failure in the pool will affect them.
1616*7877fdebSMatt Macy 		 */
1617*7877fdebSMatt Macy 		if (vdev_draid_find_spare(cvd) != NULL)
1618*7877fdebSMatt Macy 			return (B_TRUE);
1619*7877fdebSMatt Macy 	}
1620*7877fdebSMatt Macy 
1621*7877fdebSMatt Macy 	return (B_FALSE);
1622*7877fdebSMatt Macy }
1623*7877fdebSMatt Macy 
1624*7877fdebSMatt Macy /*
1625*7877fdebSMatt Macy  * Determine if the txg is missing.  Used by healing resilver.
1626*7877fdebSMatt Macy  */
1627*7877fdebSMatt Macy static boolean_t
1628*7877fdebSMatt Macy vdev_draid_group_missing(vdev_t *vd, uint64_t offset, uint64_t txg,
1629*7877fdebSMatt Macy     uint64_t size)
1630*7877fdebSMatt Macy {
1631*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1632*7877fdebSMatt Macy 
1633*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1634*7877fdebSMatt Macy 	ASSERT3U(vdev_draid_get_astart(vd, offset), ==, offset);
1635*7877fdebSMatt Macy 
1636*7877fdebSMatt Macy 	uint64_t groupstart, perm;
1637*7877fdebSMatt Macy 	uint64_t physical_offset = vdev_draid_logical_to_physical(vd,
1638*7877fdebSMatt Macy 	    offset, &perm, &groupstart);
1639*7877fdebSMatt Macy 
1640*7877fdebSMatt Macy 	uint8_t *base;
1641*7877fdebSMatt Macy 	uint64_t iter;
1642*7877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
1643*7877fdebSMatt Macy 
1644*7877fdebSMatt Macy 	for (uint64_t i = 0; i < vdc->vdc_groupwidth; i++) {
1645*7877fdebSMatt Macy 		uint64_t c = (groupstart + i) % vdc->vdc_ndisks;
1646*7877fdebSMatt Macy 		uint64_t cid = vdev_draid_permute_id(vdc, base, iter, c);
1647*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[cid];
1648*7877fdebSMatt Macy 
1649*7877fdebSMatt Macy 		/* Transaction group is known to be partially replicated. */
1650*7877fdebSMatt Macy 		if (vdev_draid_partial(cvd, physical_offset, txg, size))
1651*7877fdebSMatt Macy 			return (B_TRUE);
1652*7877fdebSMatt Macy 
1653*7877fdebSMatt Macy 		/*
1654*7877fdebSMatt Macy 		 * Always check groups with active distributed spares
1655*7877fdebSMatt Macy 		 * because any vdev failure in the pool will affect them.
1656*7877fdebSMatt Macy 		 */
1657*7877fdebSMatt Macy 		if (vdev_draid_find_spare(cvd) != NULL)
1658*7877fdebSMatt Macy 			return (B_TRUE);
1659*7877fdebSMatt Macy 	}
1660*7877fdebSMatt Macy 
1661*7877fdebSMatt Macy 	return (B_FALSE);
1662*7877fdebSMatt Macy }
1663*7877fdebSMatt Macy 
1664*7877fdebSMatt Macy /*
1665*7877fdebSMatt Macy  * Find the smallest child asize and largest sector size to calculate the
1666*7877fdebSMatt Macy  * available capacity.  Distributed spares are ignored since their capacity
1667*7877fdebSMatt Macy  * is also based of the minimum child size in the top-level dRAID.
1668*7877fdebSMatt Macy  */
1669*7877fdebSMatt Macy static void
1670*7877fdebSMatt Macy vdev_draid_calculate_asize(vdev_t *vd, uint64_t *asizep, uint64_t *max_asizep,
1671*7877fdebSMatt Macy     uint64_t *logical_ashiftp, uint64_t *physical_ashiftp)
1672*7877fdebSMatt Macy {
1673*7877fdebSMatt Macy 	uint64_t logical_ashift = 0, physical_ashift = 0;
1674*7877fdebSMatt Macy 	uint64_t asize = 0, max_asize = 0;
1675*7877fdebSMatt Macy 
1676*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1677*7877fdebSMatt Macy 
1678*7877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
1679*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[c];
1680*7877fdebSMatt Macy 
1681*7877fdebSMatt Macy 		if (cvd->vdev_ops == &vdev_draid_spare_ops)
1682*7877fdebSMatt Macy 			continue;
1683*7877fdebSMatt Macy 
1684*7877fdebSMatt Macy 		asize = MIN(asize - 1, cvd->vdev_asize - 1) + 1;
1685*7877fdebSMatt Macy 		max_asize = MIN(max_asize - 1, cvd->vdev_max_asize - 1) + 1;
1686*7877fdebSMatt Macy 		logical_ashift = MAX(logical_ashift, cvd->vdev_ashift);
1687*7877fdebSMatt Macy 		physical_ashift = MAX(physical_ashift,
1688*7877fdebSMatt Macy 		    cvd->vdev_physical_ashift);
1689*7877fdebSMatt Macy 	}
1690*7877fdebSMatt Macy 
1691*7877fdebSMatt Macy 	*asizep = asize;
1692*7877fdebSMatt Macy 	*max_asizep = max_asize;
1693*7877fdebSMatt Macy 	*logical_ashiftp = logical_ashift;
1694*7877fdebSMatt Macy 	*physical_ashiftp = physical_ashift;
1695*7877fdebSMatt Macy }
1696*7877fdebSMatt Macy 
1697*7877fdebSMatt Macy /*
1698*7877fdebSMatt Macy  * Open spare vdevs.
1699*7877fdebSMatt Macy  */
1700*7877fdebSMatt Macy static boolean_t
1701*7877fdebSMatt Macy vdev_draid_open_spares(vdev_t *vd)
1702*7877fdebSMatt Macy {
1703*7877fdebSMatt Macy 	return (vd->vdev_ops == &vdev_draid_spare_ops ||
1704*7877fdebSMatt Macy 	    vd->vdev_ops == &vdev_replacing_ops ||
1705*7877fdebSMatt Macy 	    vd->vdev_ops == &vdev_spare_ops);
1706*7877fdebSMatt Macy }
1707*7877fdebSMatt Macy 
1708*7877fdebSMatt Macy /*
1709*7877fdebSMatt Macy  * Open all children, excluding spares.
1710*7877fdebSMatt Macy  */
1711*7877fdebSMatt Macy static boolean_t
1712*7877fdebSMatt Macy vdev_draid_open_children(vdev_t *vd)
1713*7877fdebSMatt Macy {
1714*7877fdebSMatt Macy 	return (!vdev_draid_open_spares(vd));
1715*7877fdebSMatt Macy }
1716*7877fdebSMatt Macy 
1717*7877fdebSMatt Macy /*
1718*7877fdebSMatt Macy  * Open a top-level dRAID vdev.
1719*7877fdebSMatt Macy  */
1720*7877fdebSMatt Macy static int
1721*7877fdebSMatt Macy vdev_draid_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
1722*7877fdebSMatt Macy     uint64_t *logical_ashift, uint64_t *physical_ashift)
1723*7877fdebSMatt Macy {
1724*7877fdebSMatt Macy 	vdev_draid_config_t *vdc =  vd->vdev_tsd;
1725*7877fdebSMatt Macy 	uint64_t nparity = vdc->vdc_nparity;
1726*7877fdebSMatt Macy 	int open_errors = 0;
1727*7877fdebSMatt Macy 
1728*7877fdebSMatt Macy 	if (nparity > VDEV_DRAID_MAXPARITY ||
1729*7877fdebSMatt Macy 	    vd->vdev_children < nparity + 1) {
1730*7877fdebSMatt Macy 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
1731*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
1732*7877fdebSMatt Macy 	}
1733*7877fdebSMatt Macy 
1734*7877fdebSMatt Macy 	/*
1735*7877fdebSMatt Macy 	 * First open the normal children then the distributed spares.  This
1736*7877fdebSMatt Macy 	 * ordering is important to ensure the distributed spares calculate
1737*7877fdebSMatt Macy 	 * the correct psize in the event that the dRAID vdevs were expanded.
1738*7877fdebSMatt Macy 	 */
1739*7877fdebSMatt Macy 	vdev_open_children_subset(vd, vdev_draid_open_children);
1740*7877fdebSMatt Macy 	vdev_open_children_subset(vd, vdev_draid_open_spares);
1741*7877fdebSMatt Macy 
1742*7877fdebSMatt Macy 	/* Verify enough of the children are available to continue. */
1743*7877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
1744*7877fdebSMatt Macy 		if (vd->vdev_child[c]->vdev_open_error != 0) {
1745*7877fdebSMatt Macy 			if ((++open_errors) > nparity) {
1746*7877fdebSMatt Macy 				vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
1747*7877fdebSMatt Macy 				return (SET_ERROR(ENXIO));
1748*7877fdebSMatt Macy 			}
1749*7877fdebSMatt Macy 		}
1750*7877fdebSMatt Macy 	}
1751*7877fdebSMatt Macy 
1752*7877fdebSMatt Macy 	/*
1753*7877fdebSMatt Macy 	 * Allocatable capacity is the sum of the space on all children less
1754*7877fdebSMatt Macy 	 * the number of distributed spares rounded down to last full row
1755*7877fdebSMatt Macy 	 * and then to the last full group. An additional 32MB of scratch
1756*7877fdebSMatt Macy 	 * space is reserved at the end of each child for use by the dRAID
1757*7877fdebSMatt Macy 	 * expansion feature.
1758*7877fdebSMatt Macy 	 */
1759*7877fdebSMatt Macy 	uint64_t child_asize, child_max_asize;
1760*7877fdebSMatt Macy 	vdev_draid_calculate_asize(vd, &child_asize, &child_max_asize,
1761*7877fdebSMatt Macy 	    logical_ashift, physical_ashift);
1762*7877fdebSMatt Macy 
1763*7877fdebSMatt Macy 	/*
1764*7877fdebSMatt Macy 	 * Should be unreachable since the minimum child size is 64MB, but
1765*7877fdebSMatt Macy 	 * we want to make sure an underflow absolutely cannot occur here.
1766*7877fdebSMatt Macy 	 */
1767*7877fdebSMatt Macy 	if (child_asize < VDEV_DRAID_REFLOW_RESERVE ||
1768*7877fdebSMatt Macy 	    child_max_asize < VDEV_DRAID_REFLOW_RESERVE) {
1769*7877fdebSMatt Macy 		return (SET_ERROR(ENXIO));
1770*7877fdebSMatt Macy 	}
1771*7877fdebSMatt Macy 
1772*7877fdebSMatt Macy 	child_asize = ((child_asize - VDEV_DRAID_REFLOW_RESERVE) /
1773*7877fdebSMatt Macy 	    VDEV_DRAID_ROWHEIGHT) * VDEV_DRAID_ROWHEIGHT;
1774*7877fdebSMatt Macy 	child_max_asize = ((child_max_asize - VDEV_DRAID_REFLOW_RESERVE) /
1775*7877fdebSMatt Macy 	    VDEV_DRAID_ROWHEIGHT) * VDEV_DRAID_ROWHEIGHT;
1776*7877fdebSMatt Macy 
1777*7877fdebSMatt Macy 	*asize = (((child_asize * vdc->vdc_ndisks) / vdc->vdc_groupsz) *
1778*7877fdebSMatt Macy 	    vdc->vdc_groupsz);
1779*7877fdebSMatt Macy 	*max_asize = (((child_max_asize * vdc->vdc_ndisks) / vdc->vdc_groupsz) *
1780*7877fdebSMatt Macy 	    vdc->vdc_groupsz);
1781*7877fdebSMatt Macy 
1782*7877fdebSMatt Macy 	return (0);
1783*7877fdebSMatt Macy }
1784*7877fdebSMatt Macy 
1785*7877fdebSMatt Macy /*
1786*7877fdebSMatt Macy  * Close a top-level dRAID vdev.
1787*7877fdebSMatt Macy  */
1788*7877fdebSMatt Macy static void
1789*7877fdebSMatt Macy vdev_draid_close(vdev_t *vd)
1790*7877fdebSMatt Macy {
1791*7877fdebSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
1792*7877fdebSMatt Macy 		if (vd->vdev_child[c] != NULL)
1793*7877fdebSMatt Macy 			vdev_close(vd->vdev_child[c]);
1794*7877fdebSMatt Macy 	}
1795*7877fdebSMatt Macy }
1796*7877fdebSMatt Macy 
1797*7877fdebSMatt Macy /*
1798*7877fdebSMatt Macy  * Return the maximum asize for a rebuild zio in the provided range
1799*7877fdebSMatt Macy  * given the following constraints.  A dRAID chunks may not:
1800*7877fdebSMatt Macy  *
1801*7877fdebSMatt Macy  * - Exceed the maximum allowed block size (SPA_MAXBLOCKSIZE), or
1802*7877fdebSMatt Macy  * - Span dRAID redundancy groups.
1803*7877fdebSMatt Macy  */
1804*7877fdebSMatt Macy static uint64_t
1805*7877fdebSMatt Macy vdev_draid_rebuild_asize(vdev_t *vd, uint64_t start, uint64_t asize,
1806*7877fdebSMatt Macy     uint64_t max_segment)
1807*7877fdebSMatt Macy {
1808*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1809*7877fdebSMatt Macy 
1810*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1811*7877fdebSMatt Macy 
1812*7877fdebSMatt Macy 	uint64_t ashift = vd->vdev_ashift;
1813*7877fdebSMatt Macy 	uint64_t ndata = vdc->vdc_ndata;
1814*7877fdebSMatt Macy 	uint64_t psize = MIN(P2ROUNDUP(max_segment * ndata, 1 << ashift),
1815*7877fdebSMatt Macy 	    SPA_MAXBLOCKSIZE);
1816*7877fdebSMatt Macy 
1817*7877fdebSMatt Macy 	ASSERT3U(vdev_draid_get_astart(vd, start), ==, start);
1818*7877fdebSMatt Macy 	ASSERT3U(asize % (vdc->vdc_groupwidth << ashift), ==, 0);
1819*7877fdebSMatt Macy 
1820*7877fdebSMatt Macy 	/* Chunks must evenly span all data columns in the group. */
1821*7877fdebSMatt Macy 	psize = (((psize >> ashift) / ndata) * ndata) << ashift;
1822*7877fdebSMatt Macy 	uint64_t chunk_size = MIN(asize, vdev_psize_to_asize(vd, psize));
1823*7877fdebSMatt Macy 
1824*7877fdebSMatt Macy 	/* Reduce the chunk size to the group space remaining. */
1825*7877fdebSMatt Macy 	uint64_t group = vdev_draid_offset_to_group(vd, start);
1826*7877fdebSMatt Macy 	uint64_t left = vdev_draid_group_to_offset(vd, group + 1) - start;
1827*7877fdebSMatt Macy 	chunk_size = MIN(chunk_size, left);
1828*7877fdebSMatt Macy 
1829*7877fdebSMatt Macy 	ASSERT3U(chunk_size % (vdc->vdc_groupwidth << ashift), ==, 0);
1830*7877fdebSMatt Macy 	ASSERT3U(vdev_draid_offset_to_group(vd, start), ==,
1831*7877fdebSMatt Macy 	    vdev_draid_offset_to_group(vd, start + chunk_size - 1));
1832*7877fdebSMatt Macy 
1833*7877fdebSMatt Macy 	return (chunk_size);
1834*7877fdebSMatt Macy }
1835*7877fdebSMatt Macy 
1836*7877fdebSMatt Macy /*
1837*7877fdebSMatt Macy  * Align the start of the metaslab to the group width and slightly reduce
1838*7877fdebSMatt Macy  * its size to a multiple of the group width.  Since full stripe writes are
1839*7877fdebSMatt Macy  * required by dRAID this space is unallocable.  Furthermore, aligning the
1840*7877fdebSMatt Macy  * metaslab start is important for vdev initialize and TRIM which both operate
1841*7877fdebSMatt Macy  * on metaslab boundaries which vdev_xlate() expects to be aligned.
1842*7877fdebSMatt Macy  */
1843*7877fdebSMatt Macy static void
1844*7877fdebSMatt Macy vdev_draid_metaslab_init(vdev_t *vd, uint64_t *ms_start, uint64_t *ms_size)
1845*7877fdebSMatt Macy {
1846*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
1847*7877fdebSMatt Macy 
1848*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
1849*7877fdebSMatt Macy 
1850*7877fdebSMatt Macy 	uint64_t sz = vdc->vdc_groupwidth << vd->vdev_ashift;
1851*7877fdebSMatt Macy 	uint64_t astart = vdev_draid_get_astart(vd, *ms_start);
1852*7877fdebSMatt Macy 	uint64_t asize = ((*ms_size - (astart - *ms_start)) / sz) * sz;
1853*7877fdebSMatt Macy 
1854*7877fdebSMatt Macy 	*ms_start = astart;
1855*7877fdebSMatt Macy 	*ms_size = asize;
1856*7877fdebSMatt Macy 
1857*7877fdebSMatt Macy 	ASSERT0(*ms_start % sz);
1858*7877fdebSMatt Macy 	ASSERT0(*ms_size % sz);
1859*7877fdebSMatt Macy }
1860*7877fdebSMatt Macy 
1861*7877fdebSMatt Macy /*
1862*7877fdebSMatt Macy  * Add virtual dRAID spares to the list of valid spares. In order to accomplish
1863*7877fdebSMatt Macy  * this the existing array must be freed and reallocated with the additional
1864*7877fdebSMatt Macy  * entries.
1865*7877fdebSMatt Macy  */
1866*7877fdebSMatt Macy int
1867*7877fdebSMatt Macy vdev_draid_spare_create(nvlist_t *nvroot, vdev_t *vd, uint64_t *ndraidp,
1868*7877fdebSMatt Macy     uint64_t next_vdev_id)
1869*7877fdebSMatt Macy {
1870*7877fdebSMatt Macy 	uint64_t draid_nspares = 0;
1871*7877fdebSMatt Macy 	uint64_t ndraid = 0;
1872*7877fdebSMatt Macy 	int error;
1873*7877fdebSMatt Macy 
1874*7877fdebSMatt Macy 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
1875*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[i];
1876*7877fdebSMatt Macy 
1877*7877fdebSMatt Macy 		if (cvd->vdev_ops == &vdev_draid_ops) {
1878*7877fdebSMatt Macy 			vdev_draid_config_t *vdc = cvd->vdev_tsd;
1879*7877fdebSMatt Macy 			draid_nspares += vdc->vdc_nspares;
1880*7877fdebSMatt Macy 			ndraid++;
1881*7877fdebSMatt Macy 		}
1882*7877fdebSMatt Macy 	}
1883*7877fdebSMatt Macy 
1884*7877fdebSMatt Macy 	if (draid_nspares == 0) {
1885*7877fdebSMatt Macy 		*ndraidp = ndraid;
1886*7877fdebSMatt Macy 		return (0);
1887*7877fdebSMatt Macy 	}
1888*7877fdebSMatt Macy 
1889*7877fdebSMatt Macy 	nvlist_t **old_spares, **new_spares;
1890*7877fdebSMatt Macy 	uint_t old_nspares;
1891*7877fdebSMatt Macy 	error = nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1892*7877fdebSMatt Macy 	    &old_spares, &old_nspares);
1893*7877fdebSMatt Macy 	if (error)
1894*7877fdebSMatt Macy 		old_nspares = 0;
1895*7877fdebSMatt Macy 
1896*7877fdebSMatt Macy 	/* Allocate memory and copy of the existing spares. */
1897*7877fdebSMatt Macy 	new_spares = kmem_alloc(sizeof (nvlist_t *) *
1898*7877fdebSMatt Macy 	    (draid_nspares + old_nspares), KM_SLEEP);
1899*7877fdebSMatt Macy 	for (uint_t i = 0; i < old_nspares; i++)
1900*7877fdebSMatt Macy 		new_spares[i] = fnvlist_dup(old_spares[i]);
1901*7877fdebSMatt Macy 
1902*7877fdebSMatt Macy 	/* Add new distributed spares to ZPOOL_CONFIG_SPARES. */
1903*7877fdebSMatt Macy 	uint64_t n = old_nspares;
1904*7877fdebSMatt Macy 	for (uint64_t vdev_id = 0; vdev_id < vd->vdev_children; vdev_id++) {
1905*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[vdev_id];
1906*7877fdebSMatt Macy 		char path[64];
1907*7877fdebSMatt Macy 
1908*7877fdebSMatt Macy 		if (cvd->vdev_ops != &vdev_draid_ops)
1909*7877fdebSMatt Macy 			continue;
1910*7877fdebSMatt Macy 
1911*7877fdebSMatt Macy 		vdev_draid_config_t *vdc = cvd->vdev_tsd;
1912*7877fdebSMatt Macy 		uint64_t nspares = vdc->vdc_nspares;
1913*7877fdebSMatt Macy 		uint64_t nparity = vdc->vdc_nparity;
1914*7877fdebSMatt Macy 
1915*7877fdebSMatt Macy 		for (uint64_t spare_id = 0; spare_id < nspares; spare_id++) {
1916*7877fdebSMatt Macy 			bzero(path, sizeof (path));
1917*7877fdebSMatt Macy 			(void) snprintf(path, sizeof (path) - 1,
1918*7877fdebSMatt Macy 			    "%s%llu-%llu-%llu", VDEV_TYPE_DRAID,
1919*7877fdebSMatt Macy 			    (u_longlong_t)nparity,
1920*7877fdebSMatt Macy 			    (u_longlong_t)next_vdev_id + vdev_id,
1921*7877fdebSMatt Macy 			    (u_longlong_t)spare_id);
1922*7877fdebSMatt Macy 
1923*7877fdebSMatt Macy 			nvlist_t *spare = fnvlist_alloc();
1924*7877fdebSMatt Macy 			fnvlist_add_string(spare, ZPOOL_CONFIG_PATH, path);
1925*7877fdebSMatt Macy 			fnvlist_add_string(spare, ZPOOL_CONFIG_TYPE,
1926*7877fdebSMatt Macy 			    VDEV_TYPE_DRAID_SPARE);
1927*7877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_TOP_GUID,
1928*7877fdebSMatt Macy 			    cvd->vdev_guid);
1929*7877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_SPARE_ID,
1930*7877fdebSMatt Macy 			    spare_id);
1931*7877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_IS_LOG, 0);
1932*7877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_IS_SPARE, 1);
1933*7877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_WHOLE_DISK, 1);
1934*7877fdebSMatt Macy 			fnvlist_add_uint64(spare, ZPOOL_CONFIG_ASHIFT,
1935*7877fdebSMatt Macy 			    cvd->vdev_ashift);
1936*7877fdebSMatt Macy 
1937*7877fdebSMatt Macy 			new_spares[n] = spare;
1938*7877fdebSMatt Macy 			n++;
1939*7877fdebSMatt Macy 		}
1940*7877fdebSMatt Macy 	}
1941*7877fdebSMatt Macy 
1942*7877fdebSMatt Macy 	if (n > 0) {
1943*7877fdebSMatt Macy 		(void) nvlist_remove_all(nvroot, ZPOOL_CONFIG_SPARES);
1944*7877fdebSMatt Macy 		fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1945*7877fdebSMatt Macy 		    new_spares, n);
1946*7877fdebSMatt Macy 	}
1947*7877fdebSMatt Macy 
1948*7877fdebSMatt Macy 	for (int i = 0; i < n; i++)
1949*7877fdebSMatt Macy 		nvlist_free(new_spares[i]);
1950*7877fdebSMatt Macy 
1951*7877fdebSMatt Macy 	kmem_free(new_spares, sizeof (*new_spares) * n);
1952*7877fdebSMatt Macy 	*ndraidp = ndraid;
1953*7877fdebSMatt Macy 
1954*7877fdebSMatt Macy 	return (0);
1955*7877fdebSMatt Macy }
1956*7877fdebSMatt Macy 
1957*7877fdebSMatt Macy /*
1958*7877fdebSMatt Macy  * Determine if any portion of the provided block resides on a child vdev
1959*7877fdebSMatt Macy  * with a dirty DTL and therefore needs to be resilvered.
1960*7877fdebSMatt Macy  */
1961*7877fdebSMatt Macy static boolean_t
1962*7877fdebSMatt Macy vdev_draid_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
1963*7877fdebSMatt Macy     uint64_t phys_birth)
1964*7877fdebSMatt Macy {
1965*7877fdebSMatt Macy 	uint64_t offset = DVA_GET_OFFSET(dva);
1966*7877fdebSMatt Macy 	uint64_t asize = vdev_draid_asize(vd, psize);
1967*7877fdebSMatt Macy 
1968*7877fdebSMatt Macy 	if (phys_birth == TXG_UNKNOWN) {
1969*7877fdebSMatt Macy 		/*
1970*7877fdebSMatt Macy 		 * Sequential resilver.  There is no meaningful phys_birth
1971*7877fdebSMatt Macy 		 * for this block, we can only determine if block resides
1972*7877fdebSMatt Macy 		 * in a degraded group in which case it must be resilvered.
1973*7877fdebSMatt Macy 		 */
1974*7877fdebSMatt Macy 		ASSERT3U(vdev_draid_offset_to_group(vd, offset), ==,
1975*7877fdebSMatt Macy 		    vdev_draid_offset_to_group(vd, offset + asize - 1));
1976*7877fdebSMatt Macy 
1977*7877fdebSMatt Macy 		return (vdev_draid_group_degraded(vd, offset));
1978*7877fdebSMatt Macy 	} else {
1979*7877fdebSMatt Macy 		/*
1980*7877fdebSMatt Macy 		 * Healing resilver.  TXGs not in DTL_PARTIAL are intact,
1981*7877fdebSMatt Macy 		 * as are blocks in non-degraded groups.
1982*7877fdebSMatt Macy 		 */
1983*7877fdebSMatt Macy 		if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1))
1984*7877fdebSMatt Macy 			return (B_FALSE);
1985*7877fdebSMatt Macy 
1986*7877fdebSMatt Macy 		if (vdev_draid_group_missing(vd, offset, phys_birth, 1))
1987*7877fdebSMatt Macy 			return (B_TRUE);
1988*7877fdebSMatt Macy 
1989*7877fdebSMatt Macy 		/* The block may span groups in which case check both. */
1990*7877fdebSMatt Macy 		if (vdev_draid_offset_to_group(vd, offset) !=
1991*7877fdebSMatt Macy 		    vdev_draid_offset_to_group(vd, offset + asize - 1)) {
1992*7877fdebSMatt Macy 			if (vdev_draid_group_missing(vd,
1993*7877fdebSMatt Macy 			    offset + asize, phys_birth, 1))
1994*7877fdebSMatt Macy 				return (B_TRUE);
1995*7877fdebSMatt Macy 		}
1996*7877fdebSMatt Macy 
1997*7877fdebSMatt Macy 		return (B_FALSE);
1998*7877fdebSMatt Macy 	}
1999*7877fdebSMatt Macy }
2000*7877fdebSMatt Macy 
2001*7877fdebSMatt Macy static boolean_t
2002*7877fdebSMatt Macy vdev_draid_rebuilding(vdev_t *vd)
2003*7877fdebSMatt Macy {
2004*7877fdebSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_rebuild_txg)
2005*7877fdebSMatt Macy 		return (B_TRUE);
2006*7877fdebSMatt Macy 
2007*7877fdebSMatt Macy 	for (int i = 0; i < vd->vdev_children; i++) {
2008*7877fdebSMatt Macy 		if (vdev_draid_rebuilding(vd->vdev_child[i])) {
2009*7877fdebSMatt Macy 			return (B_TRUE);
2010*7877fdebSMatt Macy 		}
2011*7877fdebSMatt Macy 	}
2012*7877fdebSMatt Macy 
2013*7877fdebSMatt Macy 	return (B_FALSE);
2014*7877fdebSMatt Macy }
2015*7877fdebSMatt Macy 
2016*7877fdebSMatt Macy static void
2017*7877fdebSMatt Macy vdev_draid_io_verify(vdev_t *vd, raidz_row_t *rr, int col)
2018*7877fdebSMatt Macy {
2019*7877fdebSMatt Macy #ifdef ZFS_DEBUG
2020*7877fdebSMatt Macy 	range_seg64_t logical_rs, physical_rs, remain_rs;
2021*7877fdebSMatt Macy 	logical_rs.rs_start = rr->rr_offset;
2022*7877fdebSMatt Macy 	logical_rs.rs_end = logical_rs.rs_start +
2023*7877fdebSMatt Macy 	    vdev_draid_asize(vd, rr->rr_size);
2024*7877fdebSMatt Macy 
2025*7877fdebSMatt Macy 	raidz_col_t *rc = &rr->rr_col[col];
2026*7877fdebSMatt Macy 	vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2027*7877fdebSMatt Macy 
2028*7877fdebSMatt Macy 	vdev_xlate(cvd, &logical_rs, &physical_rs, &remain_rs);
2029*7877fdebSMatt Macy 	ASSERT(vdev_xlate_is_empty(&remain_rs));
2030*7877fdebSMatt Macy 	ASSERT3U(rc->rc_offset, ==, physical_rs.rs_start);
2031*7877fdebSMatt Macy 	ASSERT3U(rc->rc_offset, <, physical_rs.rs_end);
2032*7877fdebSMatt Macy 	ASSERT3U(rc->rc_offset + rc->rc_size, ==, physical_rs.rs_end);
2033*7877fdebSMatt Macy #endif
2034*7877fdebSMatt Macy }
2035*7877fdebSMatt Macy 
2036*7877fdebSMatt Macy /*
2037*7877fdebSMatt Macy  * For write operations:
2038*7877fdebSMatt Macy  * 1. Generate the parity data
2039*7877fdebSMatt Macy  * 2. Create child zio write operations to each column's vdev, for both
2040*7877fdebSMatt Macy  *    data and parity.  A gang ABD is allocated by vdev_draid_map_alloc()
2041*7877fdebSMatt Macy  *    if a skip sector needs to be added to a column.
2042*7877fdebSMatt Macy  */
2043*7877fdebSMatt Macy static void
2044*7877fdebSMatt Macy vdev_draid_io_start_write(zio_t *zio, raidz_row_t *rr)
2045*7877fdebSMatt Macy {
2046*7877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
2047*7877fdebSMatt Macy 	raidz_map_t *rm = zio->io_vsd;
2048*7877fdebSMatt Macy 
2049*7877fdebSMatt Macy 	vdev_raidz_generate_parity_row(rm, rr);
2050*7877fdebSMatt Macy 
2051*7877fdebSMatt Macy 	for (int c = 0; c < rr->rr_cols; c++) {
2052*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
2053*7877fdebSMatt Macy 
2054*7877fdebSMatt Macy 		/*
2055*7877fdebSMatt Macy 		 * Empty columns are zero filled and included in the parity
2056*7877fdebSMatt Macy 		 * calculation and therefore must be written.
2057*7877fdebSMatt Macy 		 */
2058*7877fdebSMatt Macy 		ASSERT3U(rc->rc_size, !=, 0);
2059*7877fdebSMatt Macy 
2060*7877fdebSMatt Macy 		/* Verify physical to logical translation */
2061*7877fdebSMatt Macy 		vdev_draid_io_verify(vd, rr, c);
2062*7877fdebSMatt Macy 
2063*7877fdebSMatt Macy 		zio_nowait(zio_vdev_child_io(zio, NULL,
2064*7877fdebSMatt Macy 		    vd->vdev_child[rc->rc_devidx], rc->rc_offset,
2065*7877fdebSMatt Macy 		    rc->rc_abd, rc->rc_size, zio->io_type, zio->io_priority,
2066*7877fdebSMatt Macy 		    0, vdev_raidz_child_done, rc));
2067*7877fdebSMatt Macy 	}
2068*7877fdebSMatt Macy }
2069*7877fdebSMatt Macy 
2070*7877fdebSMatt Macy /*
2071*7877fdebSMatt Macy  * For read operations:
2072*7877fdebSMatt Macy  * 1. The vdev_draid_map_alloc() function will create a minimal raidz
2073*7877fdebSMatt Macy  *    mapping for the read based on the zio->io_flags.  There are two
2074*7877fdebSMatt Macy  *    possible mappings either 1) a normal read, or 2) a scrub/resilver.
2075*7877fdebSMatt Macy  * 2. Create the zio read operations.  This will include all parity
2076*7877fdebSMatt Macy  *    columns and skip sectors for a scrub/resilver.
2077*7877fdebSMatt Macy  */
2078*7877fdebSMatt Macy static void
2079*7877fdebSMatt Macy vdev_draid_io_start_read(zio_t *zio, raidz_row_t *rr)
2080*7877fdebSMatt Macy {
2081*7877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
2082*7877fdebSMatt Macy 
2083*7877fdebSMatt Macy 	/* Sequential rebuild must do IO at redundancy group boundary. */
2084*7877fdebSMatt Macy 	IMPLY(zio->io_priority == ZIO_PRIORITY_REBUILD, rr->rr_nempty == 0);
2085*7877fdebSMatt Macy 
2086*7877fdebSMatt Macy 	/*
2087*7877fdebSMatt Macy 	 * Iterate over the columns in reverse order so that we hit the parity
2088*7877fdebSMatt Macy 	 * last.  Any errors along the way will force us to read the parity.
2089*7877fdebSMatt Macy 	 * For scrub/resilver IOs which verify skip sectors, a gang ABD will
2090*7877fdebSMatt Macy 	 * have been allocated to store them and rc->rc_size is increased.
2091*7877fdebSMatt Macy 	 */
2092*7877fdebSMatt Macy 	for (int c = rr->rr_cols - 1; c >= 0; c--) {
2093*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
2094*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2095*7877fdebSMatt Macy 
2096*7877fdebSMatt Macy 		if (!vdev_draid_readable(cvd, rc->rc_offset)) {
2097*7877fdebSMatt Macy 			if (c >= rr->rr_firstdatacol)
2098*7877fdebSMatt Macy 				rr->rr_missingdata++;
2099*7877fdebSMatt Macy 			else
2100*7877fdebSMatt Macy 				rr->rr_missingparity++;
2101*7877fdebSMatt Macy 			rc->rc_error = SET_ERROR(ENXIO);
2102*7877fdebSMatt Macy 			rc->rc_tried = 1;
2103*7877fdebSMatt Macy 			rc->rc_skipped = 1;
2104*7877fdebSMatt Macy 			continue;
2105*7877fdebSMatt Macy 		}
2106*7877fdebSMatt Macy 
2107*7877fdebSMatt Macy 		if (vdev_draid_missing(cvd, rc->rc_offset, zio->io_txg, 1)) {
2108*7877fdebSMatt Macy 			if (c >= rr->rr_firstdatacol)
2109*7877fdebSMatt Macy 				rr->rr_missingdata++;
2110*7877fdebSMatt Macy 			else
2111*7877fdebSMatt Macy 				rr->rr_missingparity++;
2112*7877fdebSMatt Macy 			rc->rc_error = SET_ERROR(ESTALE);
2113*7877fdebSMatt Macy 			rc->rc_skipped = 1;
2114*7877fdebSMatt Macy 			continue;
2115*7877fdebSMatt Macy 		}
2116*7877fdebSMatt Macy 
2117*7877fdebSMatt Macy 		/*
2118*7877fdebSMatt Macy 		 * Empty columns may be read during vdev_draid_io_done().
2119*7877fdebSMatt Macy 		 * Only skip them after the readable and missing checks
2120*7877fdebSMatt Macy 		 * verify they are available.
2121*7877fdebSMatt Macy 		 */
2122*7877fdebSMatt Macy 		if (rc->rc_size == 0) {
2123*7877fdebSMatt Macy 			rc->rc_skipped = 1;
2124*7877fdebSMatt Macy 			continue;
2125*7877fdebSMatt Macy 		}
2126*7877fdebSMatt Macy 
2127*7877fdebSMatt Macy 		if (zio->io_flags & ZIO_FLAG_RESILVER) {
2128*7877fdebSMatt Macy 			vdev_t *svd;
2129*7877fdebSMatt Macy 
2130*7877fdebSMatt Macy 			/*
2131*7877fdebSMatt Macy 			 * If this child is a distributed spare then the
2132*7877fdebSMatt Macy 			 * offset might reside on the vdev being replaced.
2133*7877fdebSMatt Macy 			 * In which case this data must be written to the
2134*7877fdebSMatt Macy 			 * new device.  Failure to do so would result in
2135*7877fdebSMatt Macy 			 * checksum errors when the old device is detached
2136*7877fdebSMatt Macy 			 * and the pool is scrubbed.
2137*7877fdebSMatt Macy 			 */
2138*7877fdebSMatt Macy 			if ((svd = vdev_draid_find_spare(cvd)) != NULL) {
2139*7877fdebSMatt Macy 				svd = vdev_draid_spare_get_child(svd,
2140*7877fdebSMatt Macy 				    rc->rc_offset);
2141*7877fdebSMatt Macy 				if (svd && (svd->vdev_ops == &vdev_spare_ops ||
2142*7877fdebSMatt Macy 				    svd->vdev_ops == &vdev_replacing_ops)) {
2143*7877fdebSMatt Macy 					rc->rc_repair = 1;
2144*7877fdebSMatt Macy 				}
2145*7877fdebSMatt Macy 			}
2146*7877fdebSMatt Macy 
2147*7877fdebSMatt Macy 			/*
2148*7877fdebSMatt Macy 			 * Always issue a repair IO to this child when its
2149*7877fdebSMatt Macy 			 * a spare or replacing vdev with an active rebuild.
2150*7877fdebSMatt Macy 			 */
2151*7877fdebSMatt Macy 			if ((cvd->vdev_ops == &vdev_spare_ops ||
2152*7877fdebSMatt Macy 			    cvd->vdev_ops == &vdev_replacing_ops) &&
2153*7877fdebSMatt Macy 			    vdev_draid_rebuilding(cvd)) {
2154*7877fdebSMatt Macy 				rc->rc_repair = 1;
2155*7877fdebSMatt Macy 			}
2156*7877fdebSMatt Macy 		}
2157*7877fdebSMatt Macy 	}
2158*7877fdebSMatt Macy 
2159*7877fdebSMatt Macy 	/*
2160*7877fdebSMatt Macy 	 * Either a parity or data column is missing this means a repair
2161*7877fdebSMatt Macy 	 * may be attempted by vdev_draid_io_done().  Expand the raid map
2162*7877fdebSMatt Macy 	 * to read in empty columns which are needed along with the parity
2163*7877fdebSMatt Macy 	 * during reconstruction.
2164*7877fdebSMatt Macy 	 */
2165*7877fdebSMatt Macy 	if ((rr->rr_missingdata > 0 || rr->rr_missingparity > 0) &&
2166*7877fdebSMatt Macy 	    rr->rr_nempty > 0 && rr->rr_abd_empty == NULL) {
2167*7877fdebSMatt Macy 		vdev_draid_map_alloc_empty(zio, rr);
2168*7877fdebSMatt Macy 	}
2169*7877fdebSMatt Macy 
2170*7877fdebSMatt Macy 	for (int c = rr->rr_cols - 1; c >= 0; c--) {
2171*7877fdebSMatt Macy 		raidz_col_t *rc = &rr->rr_col[c];
2172*7877fdebSMatt Macy 		vdev_t *cvd = vd->vdev_child[rc->rc_devidx];
2173*7877fdebSMatt Macy 
2174*7877fdebSMatt Macy 		if (rc->rc_error || rc->rc_size == 0)
2175*7877fdebSMatt Macy 			continue;
2176*7877fdebSMatt Macy 
2177*7877fdebSMatt Macy 		if (c >= rr->rr_firstdatacol || rr->rr_missingdata > 0 ||
2178*7877fdebSMatt Macy 		    (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
2179*7877fdebSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2180*7877fdebSMatt Macy 			    rc->rc_offset, rc->rc_abd, rc->rc_size,
2181*7877fdebSMatt Macy 			    zio->io_type, zio->io_priority, 0,
2182*7877fdebSMatt Macy 			    vdev_raidz_child_done, rc));
2183*7877fdebSMatt Macy 		}
2184*7877fdebSMatt Macy 	}
2185*7877fdebSMatt Macy }
2186*7877fdebSMatt Macy 
2187*7877fdebSMatt Macy /*
2188*7877fdebSMatt Macy  * Start an IO operation to a dRAID vdev.
2189*7877fdebSMatt Macy  */
2190*7877fdebSMatt Macy static void
2191*7877fdebSMatt Macy vdev_draid_io_start(zio_t *zio)
2192*7877fdebSMatt Macy {
2193*7877fdebSMatt Macy 	vdev_t *vd __maybe_unused = zio->io_vd;
2194*7877fdebSMatt Macy 	raidz_map_t *rm;
2195*7877fdebSMatt Macy 
2196*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
2197*7877fdebSMatt Macy 	ASSERT3U(zio->io_offset, ==, vdev_draid_get_astart(vd, zio->io_offset));
2198*7877fdebSMatt Macy 
2199*7877fdebSMatt Macy 	rm = vdev_draid_map_alloc(zio);
2200*7877fdebSMatt Macy 
2201*7877fdebSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE) {
2202*7877fdebSMatt Macy 		for (int i = 0; i < rm->rm_nrows; i++) {
2203*7877fdebSMatt Macy 			vdev_draid_io_start_write(zio, rm->rm_row[i]);
2204*7877fdebSMatt Macy 		}
2205*7877fdebSMatt Macy 	} else {
2206*7877fdebSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_READ);
2207*7877fdebSMatt Macy 
2208*7877fdebSMatt Macy 		for (int i = 0; i < rm->rm_nrows; i++) {
2209*7877fdebSMatt Macy 			vdev_draid_io_start_read(zio, rm->rm_row[i]);
2210*7877fdebSMatt Macy 		}
2211*7877fdebSMatt Macy 	}
2212*7877fdebSMatt Macy 
2213*7877fdebSMatt Macy 	zio_execute(zio);
2214*7877fdebSMatt Macy }
2215*7877fdebSMatt Macy 
2216*7877fdebSMatt Macy /*
2217*7877fdebSMatt Macy  * Complete an IO operation on a dRAID vdev.  The raidz logic can be applied
2218*7877fdebSMatt Macy  * to dRAID since the layout is fully described by the raidz_map_t.
2219*7877fdebSMatt Macy  */
2220*7877fdebSMatt Macy static void
2221*7877fdebSMatt Macy vdev_draid_io_done(zio_t *zio)
2222*7877fdebSMatt Macy {
2223*7877fdebSMatt Macy 	vdev_raidz_io_done(zio);
2224*7877fdebSMatt Macy }
2225*7877fdebSMatt Macy 
2226*7877fdebSMatt Macy static void
2227*7877fdebSMatt Macy vdev_draid_state_change(vdev_t *vd, int faulted, int degraded)
2228*7877fdebSMatt Macy {
2229*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
2230*7877fdebSMatt Macy 	ASSERT(vd->vdev_ops == &vdev_draid_ops);
2231*7877fdebSMatt Macy 
2232*7877fdebSMatt Macy 	if (faulted > vdc->vdc_nparity)
2233*7877fdebSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2234*7877fdebSMatt Macy 		    VDEV_AUX_NO_REPLICAS);
2235*7877fdebSMatt Macy 	else if (degraded + faulted != 0)
2236*7877fdebSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
2237*7877fdebSMatt Macy 	else
2238*7877fdebSMatt Macy 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
2239*7877fdebSMatt Macy }
2240*7877fdebSMatt Macy 
2241*7877fdebSMatt Macy static void
2242*7877fdebSMatt Macy vdev_draid_xlate(vdev_t *cvd, const range_seg64_t *logical_rs,
2243*7877fdebSMatt Macy     range_seg64_t *physical_rs, range_seg64_t *remain_rs)
2244*7877fdebSMatt Macy {
2245*7877fdebSMatt Macy 	vdev_t *raidvd = cvd->vdev_parent;
2246*7877fdebSMatt Macy 	ASSERT(raidvd->vdev_ops == &vdev_draid_ops);
2247*7877fdebSMatt Macy 
2248*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = raidvd->vdev_tsd;
2249*7877fdebSMatt Macy 	uint64_t ashift = raidvd->vdev_top->vdev_ashift;
2250*7877fdebSMatt Macy 
2251*7877fdebSMatt Macy 	/* Make sure the offsets are block-aligned */
2252*7877fdebSMatt Macy 	ASSERT0(logical_rs->rs_start % (1 << ashift));
2253*7877fdebSMatt Macy 	ASSERT0(logical_rs->rs_end % (1 << ashift));
2254*7877fdebSMatt Macy 
2255*7877fdebSMatt Macy 	uint64_t logical_start = logical_rs->rs_start;
2256*7877fdebSMatt Macy 	uint64_t logical_end = logical_rs->rs_end;
2257*7877fdebSMatt Macy 
2258*7877fdebSMatt Macy 	/*
2259*7877fdebSMatt Macy 	 * Unaligned ranges must be skipped. All metaslabs are correctly
2260*7877fdebSMatt Macy 	 * aligned so this should not happen, but this case is handled in
2261*7877fdebSMatt Macy 	 * case it's needed by future callers.
2262*7877fdebSMatt Macy 	 */
2263*7877fdebSMatt Macy 	uint64_t astart = vdev_draid_get_astart(raidvd, logical_start);
2264*7877fdebSMatt Macy 	if (astart != logical_start) {
2265*7877fdebSMatt Macy 		physical_rs->rs_start = logical_start;
2266*7877fdebSMatt Macy 		physical_rs->rs_end = logical_start;
2267*7877fdebSMatt Macy 		remain_rs->rs_start = MIN(astart, logical_end);
2268*7877fdebSMatt Macy 		remain_rs->rs_end = logical_end;
2269*7877fdebSMatt Macy 		return;
2270*7877fdebSMatt Macy 	}
2271*7877fdebSMatt Macy 
2272*7877fdebSMatt Macy 	/*
2273*7877fdebSMatt Macy 	 * Unlike with mirrors and raidz a dRAID logical range can map
2274*7877fdebSMatt Macy 	 * to multiple non-contiguous physical ranges. This is handled by
2275*7877fdebSMatt Macy 	 * limiting the size of the logical range to a single group and
2276*7877fdebSMatt Macy 	 * setting the remain argument such that it describes the remaining
2277*7877fdebSMatt Macy 	 * unmapped logical range. This is stricter than absolutely
2278*7877fdebSMatt Macy 	 * necessary but helps simplify the logic below.
2279*7877fdebSMatt Macy 	 */
2280*7877fdebSMatt Macy 	uint64_t group = vdev_draid_offset_to_group(raidvd, logical_start);
2281*7877fdebSMatt Macy 	uint64_t nextstart = vdev_draid_group_to_offset(raidvd, group + 1);
2282*7877fdebSMatt Macy 	if (logical_end > nextstart)
2283*7877fdebSMatt Macy 		logical_end = nextstart;
2284*7877fdebSMatt Macy 
2285*7877fdebSMatt Macy 	/* Find the starting offset for each vdev in the group */
2286*7877fdebSMatt Macy 	uint64_t perm, groupstart;
2287*7877fdebSMatt Macy 	uint64_t start = vdev_draid_logical_to_physical(raidvd,
2288*7877fdebSMatt Macy 	    logical_start, &perm, &groupstart);
2289*7877fdebSMatt Macy 	uint64_t end = start;
2290*7877fdebSMatt Macy 
2291*7877fdebSMatt Macy 	uint8_t *base;
2292*7877fdebSMatt Macy 	uint64_t iter, id;
2293*7877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
2294*7877fdebSMatt Macy 
2295*7877fdebSMatt Macy 	/*
2296*7877fdebSMatt Macy 	 * Check if the passed child falls within the group.  If it does
2297*7877fdebSMatt Macy 	 * update the start and end to reflect the physical range.
2298*7877fdebSMatt Macy 	 * Otherwise, leave them unmodified which will result in an empty
2299*7877fdebSMatt Macy 	 * (zero-length) physical range being returned.
2300*7877fdebSMatt Macy 	 */
2301*7877fdebSMatt Macy 	for (uint64_t i = 0; i < vdc->vdc_groupwidth; i++) {
2302*7877fdebSMatt Macy 		uint64_t c = (groupstart + i) % vdc->vdc_ndisks;
2303*7877fdebSMatt Macy 
2304*7877fdebSMatt Macy 		if (c == 0 && i != 0) {
2305*7877fdebSMatt Macy 			/* the group wrapped, increment the start */
2306*7877fdebSMatt Macy 			start += VDEV_DRAID_ROWHEIGHT;
2307*7877fdebSMatt Macy 			end = start;
2308*7877fdebSMatt Macy 		}
2309*7877fdebSMatt Macy 
2310*7877fdebSMatt Macy 		id = vdev_draid_permute_id(vdc, base, iter, c);
2311*7877fdebSMatt Macy 		if (id == cvd->vdev_id) {
2312*7877fdebSMatt Macy 			uint64_t b_size = (logical_end >> ashift) -
2313*7877fdebSMatt Macy 			    (logical_start >> ashift);
2314*7877fdebSMatt Macy 			ASSERT3U(b_size, >, 0);
2315*7877fdebSMatt Macy 			end = start + ((((b_size - 1) /
2316*7877fdebSMatt Macy 			    vdc->vdc_groupwidth) + 1) << ashift);
2317*7877fdebSMatt Macy 			break;
2318*7877fdebSMatt Macy 		}
2319*7877fdebSMatt Macy 	}
2320*7877fdebSMatt Macy 	physical_rs->rs_start = start;
2321*7877fdebSMatt Macy 	physical_rs->rs_end = end;
2322*7877fdebSMatt Macy 
2323*7877fdebSMatt Macy 	/*
2324*7877fdebSMatt Macy 	 * Only top-level vdevs are allowed to set remain_rs because
2325*7877fdebSMatt Macy 	 * when .vdev_op_xlate() is called for their children the full
2326*7877fdebSMatt Macy 	 * logical range is not provided by vdev_xlate().
2327*7877fdebSMatt Macy 	 */
2328*7877fdebSMatt Macy 	remain_rs->rs_start = logical_end;
2329*7877fdebSMatt Macy 	remain_rs->rs_end = logical_rs->rs_end;
2330*7877fdebSMatt Macy 
2331*7877fdebSMatt Macy 	ASSERT3U(physical_rs->rs_start, <=, logical_start);
2332*7877fdebSMatt Macy 	ASSERT3U(physical_rs->rs_end - physical_rs->rs_start, <=,
2333*7877fdebSMatt Macy 	    logical_end - logical_start);
2334*7877fdebSMatt Macy }
2335*7877fdebSMatt Macy 
2336*7877fdebSMatt Macy /*
2337*7877fdebSMatt Macy  * Add dRAID specific fields to the config nvlist.
2338*7877fdebSMatt Macy  */
2339*7877fdebSMatt Macy static void
2340*7877fdebSMatt Macy vdev_draid_config_generate(vdev_t *vd, nvlist_t *nv)
2341*7877fdebSMatt Macy {
2342*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_ops);
2343*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
2344*7877fdebSMatt Macy 
2345*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vdc->vdc_nparity);
2346*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, vdc->vdc_ndata);
2347*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, vdc->vdc_nspares);
2348*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, vdc->vdc_ngroups);
2349*7877fdebSMatt Macy }
2350*7877fdebSMatt Macy 
2351*7877fdebSMatt Macy /*
2352*7877fdebSMatt Macy  * Initialize private dRAID specific fields from the nvlist.
2353*7877fdebSMatt Macy  */
2354*7877fdebSMatt Macy static int
2355*7877fdebSMatt Macy vdev_draid_init(spa_t *spa, nvlist_t *nv, void **tsd)
2356*7877fdebSMatt Macy {
2357*7877fdebSMatt Macy 	uint64_t ndata, nparity, nspares, ngroups;
2358*7877fdebSMatt Macy 	int error;
2359*7877fdebSMatt Macy 
2360*7877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, &ndata))
2361*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2362*7877fdebSMatt Macy 
2363*7877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, &nparity) ||
2364*7877fdebSMatt Macy 	    nparity == 0 || nparity > VDEV_DRAID_MAXPARITY) {
2365*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2366*7877fdebSMatt Macy 	}
2367*7877fdebSMatt Macy 
2368*7877fdebSMatt Macy 	uint_t children;
2369*7877fdebSMatt Macy 	nvlist_t **child;
2370*7877fdebSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2371*7877fdebSMatt Macy 	    &child, &children) != 0 || children == 0 ||
2372*7877fdebSMatt Macy 	    children > VDEV_DRAID_MAX_CHILDREN) {
2373*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2374*7877fdebSMatt Macy 	}
2375*7877fdebSMatt Macy 
2376*7877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, &nspares) ||
2377*7877fdebSMatt Macy 	    nspares > 100 || nspares > (children - (ndata + nparity))) {
2378*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2379*7877fdebSMatt Macy 	}
2380*7877fdebSMatt Macy 
2381*7877fdebSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, &ngroups) ||
2382*7877fdebSMatt Macy 	    ngroups == 0 || ngroups > VDEV_DRAID_MAX_CHILDREN) {
2383*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2384*7877fdebSMatt Macy 	}
2385*7877fdebSMatt Macy 
2386*7877fdebSMatt Macy 	/*
2387*7877fdebSMatt Macy 	 * Validate the minimum number of children exist per group for the
2388*7877fdebSMatt Macy 	 * specified parity level (draid1 >= 2, draid2 >= 3, draid3 >= 4).
2389*7877fdebSMatt Macy 	 */
2390*7877fdebSMatt Macy 	if (children < (ndata + nparity + nspares))
2391*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2392*7877fdebSMatt Macy 
2393*7877fdebSMatt Macy 	/*
2394*7877fdebSMatt Macy 	 * Create the dRAID configuration using the pool nvlist configuration
2395*7877fdebSMatt Macy 	 * and the fixed mapping for the correct number of children.
2396*7877fdebSMatt Macy 	 */
2397*7877fdebSMatt Macy 	vdev_draid_config_t *vdc;
2398*7877fdebSMatt Macy 	const draid_map_t *map;
2399*7877fdebSMatt Macy 
2400*7877fdebSMatt Macy 	error = vdev_draid_lookup_map(children, &map);
2401*7877fdebSMatt Macy 	if (error)
2402*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2403*7877fdebSMatt Macy 
2404*7877fdebSMatt Macy 	vdc = kmem_zalloc(sizeof (*vdc), KM_SLEEP);
2405*7877fdebSMatt Macy 	vdc->vdc_ndata = ndata;
2406*7877fdebSMatt Macy 	vdc->vdc_nparity = nparity;
2407*7877fdebSMatt Macy 	vdc->vdc_nspares = nspares;
2408*7877fdebSMatt Macy 	vdc->vdc_children = children;
2409*7877fdebSMatt Macy 	vdc->vdc_ngroups = ngroups;
2410*7877fdebSMatt Macy 	vdc->vdc_nperms = map->dm_nperms;
2411*7877fdebSMatt Macy 
2412*7877fdebSMatt Macy 	error = vdev_draid_generate_perms(map, &vdc->vdc_perms);
2413*7877fdebSMatt Macy 	if (error) {
2414*7877fdebSMatt Macy 		kmem_free(vdc, sizeof (*vdc));
2415*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2416*7877fdebSMatt Macy 	}
2417*7877fdebSMatt Macy 
2418*7877fdebSMatt Macy 	/*
2419*7877fdebSMatt Macy 	 * Derived constants.
2420*7877fdebSMatt Macy 	 */
2421*7877fdebSMatt Macy 	vdc->vdc_groupwidth = vdc->vdc_ndata + vdc->vdc_nparity;
2422*7877fdebSMatt Macy 	vdc->vdc_ndisks = vdc->vdc_children - vdc->vdc_nspares;
2423*7877fdebSMatt Macy 	vdc->vdc_groupsz = vdc->vdc_groupwidth * VDEV_DRAID_ROWHEIGHT;
2424*7877fdebSMatt Macy 	vdc->vdc_devslicesz = (vdc->vdc_groupsz * vdc->vdc_ngroups) /
2425*7877fdebSMatt Macy 	    vdc->vdc_ndisks;
2426*7877fdebSMatt Macy 
2427*7877fdebSMatt Macy 	ASSERT3U(vdc->vdc_groupwidth, >=, 2);
2428*7877fdebSMatt Macy 	ASSERT3U(vdc->vdc_groupwidth, <=, vdc->vdc_ndisks);
2429*7877fdebSMatt Macy 	ASSERT3U(vdc->vdc_groupsz, >=, 2 * VDEV_DRAID_ROWHEIGHT);
2430*7877fdebSMatt Macy 	ASSERT3U(vdc->vdc_devslicesz, >=, VDEV_DRAID_ROWHEIGHT);
2431*7877fdebSMatt Macy 	ASSERT3U(vdc->vdc_devslicesz % VDEV_DRAID_ROWHEIGHT, ==, 0);
2432*7877fdebSMatt Macy 	ASSERT3U((vdc->vdc_groupwidth * vdc->vdc_ngroups) %
2433*7877fdebSMatt Macy 	    vdc->vdc_ndisks, ==, 0);
2434*7877fdebSMatt Macy 
2435*7877fdebSMatt Macy 	*tsd = vdc;
2436*7877fdebSMatt Macy 
2437*7877fdebSMatt Macy 	return (0);
2438*7877fdebSMatt Macy }
2439*7877fdebSMatt Macy 
2440*7877fdebSMatt Macy static void
2441*7877fdebSMatt Macy vdev_draid_fini(vdev_t *vd)
2442*7877fdebSMatt Macy {
2443*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
2444*7877fdebSMatt Macy 
2445*7877fdebSMatt Macy 	vmem_free(vdc->vdc_perms, sizeof (uint8_t) *
2446*7877fdebSMatt Macy 	    vdc->vdc_children * vdc->vdc_nperms);
2447*7877fdebSMatt Macy 	kmem_free(vdc, sizeof (*vdc));
2448*7877fdebSMatt Macy }
2449*7877fdebSMatt Macy 
2450*7877fdebSMatt Macy static uint64_t
2451*7877fdebSMatt Macy vdev_draid_nparity(vdev_t *vd)
2452*7877fdebSMatt Macy {
2453*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
2454*7877fdebSMatt Macy 
2455*7877fdebSMatt Macy 	return (vdc->vdc_nparity);
2456*7877fdebSMatt Macy }
2457*7877fdebSMatt Macy 
2458*7877fdebSMatt Macy static uint64_t
2459*7877fdebSMatt Macy vdev_draid_ndisks(vdev_t *vd)
2460*7877fdebSMatt Macy {
2461*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = vd->vdev_tsd;
2462*7877fdebSMatt Macy 
2463*7877fdebSMatt Macy 	return (vdc->vdc_ndisks);
2464*7877fdebSMatt Macy }
2465*7877fdebSMatt Macy 
2466*7877fdebSMatt Macy vdev_ops_t vdev_draid_ops = {
2467*7877fdebSMatt Macy 	.vdev_op_init = vdev_draid_init,
2468*7877fdebSMatt Macy 	.vdev_op_fini = vdev_draid_fini,
2469*7877fdebSMatt Macy 	.vdev_op_open = vdev_draid_open,
2470*7877fdebSMatt Macy 	.vdev_op_close = vdev_draid_close,
2471*7877fdebSMatt Macy 	.vdev_op_asize = vdev_draid_asize,
2472*7877fdebSMatt Macy 	.vdev_op_min_asize = vdev_draid_min_asize,
2473*7877fdebSMatt Macy 	.vdev_op_min_alloc = vdev_draid_min_alloc,
2474*7877fdebSMatt Macy 	.vdev_op_io_start = vdev_draid_io_start,
2475*7877fdebSMatt Macy 	.vdev_op_io_done = vdev_draid_io_done,
2476*7877fdebSMatt Macy 	.vdev_op_state_change = vdev_draid_state_change,
2477*7877fdebSMatt Macy 	.vdev_op_need_resilver = vdev_draid_need_resilver,
2478*7877fdebSMatt Macy 	.vdev_op_hold = NULL,
2479*7877fdebSMatt Macy 	.vdev_op_rele = NULL,
2480*7877fdebSMatt Macy 	.vdev_op_remap = NULL,
2481*7877fdebSMatt Macy 	.vdev_op_xlate = vdev_draid_xlate,
2482*7877fdebSMatt Macy 	.vdev_op_rebuild_asize = vdev_draid_rebuild_asize,
2483*7877fdebSMatt Macy 	.vdev_op_metaslab_init = vdev_draid_metaslab_init,
2484*7877fdebSMatt Macy 	.vdev_op_config_generate = vdev_draid_config_generate,
2485*7877fdebSMatt Macy 	.vdev_op_nparity = vdev_draid_nparity,
2486*7877fdebSMatt Macy 	.vdev_op_ndisks = vdev_draid_ndisks,
2487*7877fdebSMatt Macy 	.vdev_op_type = VDEV_TYPE_DRAID,
2488*7877fdebSMatt Macy 	.vdev_op_leaf = B_FALSE,
2489*7877fdebSMatt Macy };
2490*7877fdebSMatt Macy 
2491*7877fdebSMatt Macy 
2492*7877fdebSMatt Macy /*
2493*7877fdebSMatt Macy  * A dRAID distributed spare is a virtual leaf vdev which is included in the
2494*7877fdebSMatt Macy  * parent dRAID configuration.  The last N columns of the dRAID permutation
2495*7877fdebSMatt Macy  * table are used to determine on which dRAID children a specific offset
2496*7877fdebSMatt Macy  * should be written.  These spare leaf vdevs can only be used to replace
2497*7877fdebSMatt Macy  * faulted children in the same dRAID configuration.
2498*7877fdebSMatt Macy  */
2499*7877fdebSMatt Macy 
2500*7877fdebSMatt Macy /*
2501*7877fdebSMatt Macy  * Distributed spare state.  All fields are set when the distributed spare is
2502*7877fdebSMatt Macy  * first opened and are immutable.
2503*7877fdebSMatt Macy  */
2504*7877fdebSMatt Macy typedef struct {
2505*7877fdebSMatt Macy 	vdev_t *vds_draid_vdev;		/* top-level parent dRAID vdev */
2506*7877fdebSMatt Macy 	uint64_t vds_top_guid;		/* top-level parent dRAID guid */
2507*7877fdebSMatt Macy 	uint64_t vds_spare_id;		/* spare id (0 - vdc->vdc_nspares-1) */
2508*7877fdebSMatt Macy } vdev_draid_spare_t;
2509*7877fdebSMatt Macy 
2510*7877fdebSMatt Macy /*
2511*7877fdebSMatt Macy  * Returns the parent dRAID vdev to which the distributed spare belongs.
2512*7877fdebSMatt Macy  * This may be safely called even when the vdev is not open.
2513*7877fdebSMatt Macy  */
2514*7877fdebSMatt Macy vdev_t *
2515*7877fdebSMatt Macy vdev_draid_spare_get_parent(vdev_t *vd)
2516*7877fdebSMatt Macy {
2517*7877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
2518*7877fdebSMatt Macy 
2519*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_spare_ops);
2520*7877fdebSMatt Macy 
2521*7877fdebSMatt Macy 	if (vds->vds_draid_vdev != NULL)
2522*7877fdebSMatt Macy 		return (vds->vds_draid_vdev);
2523*7877fdebSMatt Macy 
2524*7877fdebSMatt Macy 	return (vdev_lookup_by_guid(vd->vdev_spa->spa_root_vdev,
2525*7877fdebSMatt Macy 	    vds->vds_top_guid));
2526*7877fdebSMatt Macy }
2527*7877fdebSMatt Macy 
2528*7877fdebSMatt Macy /*
2529*7877fdebSMatt Macy  * A dRAID space is active when it's the child of a vdev using the
2530*7877fdebSMatt Macy  * vdev_spare_ops, vdev_replacing_ops or vdev_draid_ops.
2531*7877fdebSMatt Macy  */
2532*7877fdebSMatt Macy static boolean_t
2533*7877fdebSMatt Macy vdev_draid_spare_is_active(vdev_t *vd)
2534*7877fdebSMatt Macy {
2535*7877fdebSMatt Macy 	vdev_t *pvd = vd->vdev_parent;
2536*7877fdebSMatt Macy 
2537*7877fdebSMatt Macy 	if (pvd != NULL && (pvd->vdev_ops == &vdev_spare_ops ||
2538*7877fdebSMatt Macy 	    pvd->vdev_ops == &vdev_replacing_ops ||
2539*7877fdebSMatt Macy 	    pvd->vdev_ops == &vdev_draid_ops)) {
2540*7877fdebSMatt Macy 		return (B_TRUE);
2541*7877fdebSMatt Macy 	} else {
2542*7877fdebSMatt Macy 		return (B_FALSE);
2543*7877fdebSMatt Macy 	}
2544*7877fdebSMatt Macy }
2545*7877fdebSMatt Macy 
2546*7877fdebSMatt Macy /*
2547*7877fdebSMatt Macy  * Given a dRAID distribute spare vdev, returns the physical child vdev
2548*7877fdebSMatt Macy  * on which the provided offset resides.  This may involve recursing through
2549*7877fdebSMatt Macy  * multiple layers of distributed spares.  Note that offset is relative to
2550*7877fdebSMatt Macy  * this vdev.
2551*7877fdebSMatt Macy  */
2552*7877fdebSMatt Macy vdev_t *
2553*7877fdebSMatt Macy vdev_draid_spare_get_child(vdev_t *vd, uint64_t physical_offset)
2554*7877fdebSMatt Macy {
2555*7877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
2556*7877fdebSMatt Macy 
2557*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_spare_ops);
2558*7877fdebSMatt Macy 
2559*7877fdebSMatt Macy 	/* The vdev is closed */
2560*7877fdebSMatt Macy 	if (vds->vds_draid_vdev == NULL)
2561*7877fdebSMatt Macy 		return (NULL);
2562*7877fdebSMatt Macy 
2563*7877fdebSMatt Macy 	vdev_t *tvd = vds->vds_draid_vdev;
2564*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = tvd->vdev_tsd;
2565*7877fdebSMatt Macy 
2566*7877fdebSMatt Macy 	ASSERT3P(tvd->vdev_ops, ==, &vdev_draid_ops);
2567*7877fdebSMatt Macy 	ASSERT3U(vds->vds_spare_id, <, vdc->vdc_nspares);
2568*7877fdebSMatt Macy 
2569*7877fdebSMatt Macy 	uint8_t *base;
2570*7877fdebSMatt Macy 	uint64_t iter;
2571*7877fdebSMatt Macy 	uint64_t perm = physical_offset / vdc->vdc_devslicesz;
2572*7877fdebSMatt Macy 
2573*7877fdebSMatt Macy 	vdev_draid_get_perm(vdc, perm, &base, &iter);
2574*7877fdebSMatt Macy 
2575*7877fdebSMatt Macy 	uint64_t cid = vdev_draid_permute_id(vdc, base, iter,
2576*7877fdebSMatt Macy 	    (tvd->vdev_children - 1) - vds->vds_spare_id);
2577*7877fdebSMatt Macy 	vdev_t *cvd = tvd->vdev_child[cid];
2578*7877fdebSMatt Macy 
2579*7877fdebSMatt Macy 	if (cvd->vdev_ops == &vdev_draid_spare_ops)
2580*7877fdebSMatt Macy 		return (vdev_draid_spare_get_child(cvd, physical_offset));
2581*7877fdebSMatt Macy 
2582*7877fdebSMatt Macy 	return (cvd);
2583*7877fdebSMatt Macy }
2584*7877fdebSMatt Macy 
2585*7877fdebSMatt Macy /* ARGSUSED */
2586*7877fdebSMatt Macy static void
2587*7877fdebSMatt Macy vdev_draid_spare_close(vdev_t *vd)
2588*7877fdebSMatt Macy {
2589*7877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
2590*7877fdebSMatt Macy 	vds->vds_draid_vdev = NULL;
2591*7877fdebSMatt Macy }
2592*7877fdebSMatt Macy 
2593*7877fdebSMatt Macy /*
2594*7877fdebSMatt Macy  * Opening a dRAID spare device is done by looking up the associated dRAID
2595*7877fdebSMatt Macy  * top-level vdev guid from the spare configuration.
2596*7877fdebSMatt Macy  */
2597*7877fdebSMatt Macy static int
2598*7877fdebSMatt Macy vdev_draid_spare_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
2599*7877fdebSMatt Macy     uint64_t *logical_ashift, uint64_t *physical_ashift)
2600*7877fdebSMatt Macy {
2601*7877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
2602*7877fdebSMatt Macy 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2603*7877fdebSMatt Macy 	uint64_t asize, max_asize;
2604*7877fdebSMatt Macy 
2605*7877fdebSMatt Macy 	vdev_t *tvd = vdev_lookup_by_guid(rvd, vds->vds_top_guid);
2606*7877fdebSMatt Macy 	if (tvd == NULL) {
2607*7877fdebSMatt Macy 		/*
2608*7877fdebSMatt Macy 		 * When spa_vdev_add() is labeling new spares the
2609*7877fdebSMatt Macy 		 * associated dRAID is not attached to the root vdev
2610*7877fdebSMatt Macy 		 * nor does this spare have a parent.  Simulate a valid
2611*7877fdebSMatt Macy 		 * device in order to allow the label to be initialized
2612*7877fdebSMatt Macy 		 * and the distributed spare added to the configuration.
2613*7877fdebSMatt Macy 		 */
2614*7877fdebSMatt Macy 		if (vd->vdev_parent == NULL) {
2615*7877fdebSMatt Macy 			*psize = *max_psize = SPA_MINDEVSIZE;
2616*7877fdebSMatt Macy 			*logical_ashift = *physical_ashift = ASHIFT_MIN;
2617*7877fdebSMatt Macy 			return (0);
2618*7877fdebSMatt Macy 		}
2619*7877fdebSMatt Macy 
2620*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2621*7877fdebSMatt Macy 	}
2622*7877fdebSMatt Macy 
2623*7877fdebSMatt Macy 	vdev_draid_config_t *vdc = tvd->vdev_tsd;
2624*7877fdebSMatt Macy 	if (tvd->vdev_ops != &vdev_draid_ops || vdc == NULL)
2625*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2626*7877fdebSMatt Macy 
2627*7877fdebSMatt Macy 	if (vds->vds_spare_id >= vdc->vdc_nspares)
2628*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2629*7877fdebSMatt Macy 
2630*7877fdebSMatt Macy 	/*
2631*7877fdebSMatt Macy 	 * Neither tvd->vdev_asize or tvd->vdev_max_asize can be used here
2632*7877fdebSMatt Macy 	 * because the caller may be vdev_draid_open() in which case the
2633*7877fdebSMatt Macy 	 * values are stale as they haven't yet been updated by vdev_open().
2634*7877fdebSMatt Macy 	 * To avoid this always recalculate the dRAID asize and max_asize.
2635*7877fdebSMatt Macy 	 */
2636*7877fdebSMatt Macy 	vdev_draid_calculate_asize(tvd, &asize, &max_asize,
2637*7877fdebSMatt Macy 	    logical_ashift, physical_ashift);
2638*7877fdebSMatt Macy 
2639*7877fdebSMatt Macy 	*psize = asize + VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2640*7877fdebSMatt Macy 	*max_psize = max_asize + VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2641*7877fdebSMatt Macy 
2642*7877fdebSMatt Macy 	vds->vds_draid_vdev = tvd;
2643*7877fdebSMatt Macy 
2644*7877fdebSMatt Macy 	return (0);
2645*7877fdebSMatt Macy }
2646*7877fdebSMatt Macy 
2647*7877fdebSMatt Macy /*
2648*7877fdebSMatt Macy  * Completed distributed spare IO.  Store the result in the parent zio
2649*7877fdebSMatt Macy  * as if it had performed the operation itself.  Only the first error is
2650*7877fdebSMatt Macy  * preserved if there are multiple errors.
2651*7877fdebSMatt Macy  */
2652*7877fdebSMatt Macy static void
2653*7877fdebSMatt Macy vdev_draid_spare_child_done(zio_t *zio)
2654*7877fdebSMatt Macy {
2655*7877fdebSMatt Macy 	zio_t *pio = zio->io_private;
2656*7877fdebSMatt Macy 
2657*7877fdebSMatt Macy 	/*
2658*7877fdebSMatt Macy 	 * IOs are issued to non-writable vdevs in order to keep their
2659*7877fdebSMatt Macy 	 * DTLs accurate.  However, we don't want to propagate the
2660*7877fdebSMatt Macy 	 * error in to the distributed spare's DTL.  When resilvering
2661*7877fdebSMatt Macy 	 * vdev_draid_need_resilver() will consult the relevant DTL
2662*7877fdebSMatt Macy 	 * to determine if the data is missing and must be repaired.
2663*7877fdebSMatt Macy 	 */
2664*7877fdebSMatt Macy 	if (!vdev_writeable(zio->io_vd))
2665*7877fdebSMatt Macy 		return;
2666*7877fdebSMatt Macy 
2667*7877fdebSMatt Macy 	if (pio->io_error == 0)
2668*7877fdebSMatt Macy 		pio->io_error = zio->io_error;
2669*7877fdebSMatt Macy }
2670*7877fdebSMatt Macy 
2671*7877fdebSMatt Macy /*
2672*7877fdebSMatt Macy  * Returns a valid label nvlist for the distributed spare vdev.  This is
2673*7877fdebSMatt Macy  * used to bypass the IO pipeline to avoid the complexity of constructing
2674*7877fdebSMatt Macy  * a complete label with valid checksum to return when read.
2675*7877fdebSMatt Macy  */
2676*7877fdebSMatt Macy nvlist_t *
2677*7877fdebSMatt Macy vdev_draid_read_config_spare(vdev_t *vd)
2678*7877fdebSMatt Macy {
2679*7877fdebSMatt Macy 	spa_t *spa = vd->vdev_spa;
2680*7877fdebSMatt Macy 	spa_aux_vdev_t *sav = &spa->spa_spares;
2681*7877fdebSMatt Macy 	uint64_t guid = vd->vdev_guid;
2682*7877fdebSMatt Macy 
2683*7877fdebSMatt Macy 	nvlist_t *nv = fnvlist_alloc();
2684*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
2685*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
2686*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_VERSION, spa_version(spa));
2687*7877fdebSMatt Macy 	fnvlist_add_string(nv, ZPOOL_CONFIG_POOL_NAME, spa_name(spa));
2688*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_POOL_GUID, spa_guid(spa));
2689*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg);
2690*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_TOP_GUID, vd->vdev_top->vdev_guid);
2691*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_POOL_STATE,
2692*7877fdebSMatt Macy 	    vdev_draid_spare_is_active(vd) ?
2693*7877fdebSMatt Macy 	    POOL_STATE_ACTIVE : POOL_STATE_SPARE);
2694*7877fdebSMatt Macy 
2695*7877fdebSMatt Macy 	/* Set the vdev guid based on the vdev list in sav_count. */
2696*7877fdebSMatt Macy 	for (int i = 0; i < sav->sav_count; i++) {
2697*7877fdebSMatt Macy 		if (sav->sav_vdevs[i]->vdev_ops == &vdev_draid_spare_ops &&
2698*7877fdebSMatt Macy 		    strcmp(sav->sav_vdevs[i]->vdev_path, vd->vdev_path) == 0) {
2699*7877fdebSMatt Macy 			guid = sav->sav_vdevs[i]->vdev_guid;
2700*7877fdebSMatt Macy 			break;
2701*7877fdebSMatt Macy 		}
2702*7877fdebSMatt Macy 	}
2703*7877fdebSMatt Macy 
2704*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, guid);
2705*7877fdebSMatt Macy 
2706*7877fdebSMatt Macy 	return (nv);
2707*7877fdebSMatt Macy }
2708*7877fdebSMatt Macy 
2709*7877fdebSMatt Macy /*
2710*7877fdebSMatt Macy  * Handle any ioctl requested of the distributed spare.  Only flushes
2711*7877fdebSMatt Macy  * are supported in which case all children must be flushed.
2712*7877fdebSMatt Macy  */
2713*7877fdebSMatt Macy static int
2714*7877fdebSMatt Macy vdev_draid_spare_ioctl(zio_t *zio)
2715*7877fdebSMatt Macy {
2716*7877fdebSMatt Macy 	vdev_t *vd = zio->io_vd;
2717*7877fdebSMatt Macy 	int error = 0;
2718*7877fdebSMatt Macy 
2719*7877fdebSMatt Macy 	if (zio->io_cmd == DKIOCFLUSHWRITECACHE) {
2720*7877fdebSMatt Macy 		for (int c = 0; c < vd->vdev_children; c++) {
2721*7877fdebSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL,
2722*7877fdebSMatt Macy 			    vd->vdev_child[c], zio->io_offset, zio->io_abd,
2723*7877fdebSMatt Macy 			    zio->io_size, zio->io_type, zio->io_priority, 0,
2724*7877fdebSMatt Macy 			    vdev_draid_spare_child_done, zio));
2725*7877fdebSMatt Macy 		}
2726*7877fdebSMatt Macy 	} else {
2727*7877fdebSMatt Macy 		error = SET_ERROR(ENOTSUP);
2728*7877fdebSMatt Macy 	}
2729*7877fdebSMatt Macy 
2730*7877fdebSMatt Macy 	return (error);
2731*7877fdebSMatt Macy }
2732*7877fdebSMatt Macy 
2733*7877fdebSMatt Macy /*
2734*7877fdebSMatt Macy  * Initiate an IO to the distributed spare.  For normal IOs this entails using
2735*7877fdebSMatt Macy  * the zio->io_offset and permutation table to calculate which child dRAID vdev
2736*7877fdebSMatt Macy  * is responsible for the data.  Then passing along the zio to that child to
2737*7877fdebSMatt Macy  * perform the actual IO.  The label ranges are not stored on disk and require
2738*7877fdebSMatt Macy  * some special handling which is described below.
2739*7877fdebSMatt Macy  */
2740*7877fdebSMatt Macy static void
2741*7877fdebSMatt Macy vdev_draid_spare_io_start(zio_t *zio)
2742*7877fdebSMatt Macy {
2743*7877fdebSMatt Macy 	vdev_t *cvd = NULL, *vd = zio->io_vd;
2744*7877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
2745*7877fdebSMatt Macy 	uint64_t offset = zio->io_offset - VDEV_LABEL_START_SIZE;
2746*7877fdebSMatt Macy 
2747*7877fdebSMatt Macy 	/*
2748*7877fdebSMatt Macy 	 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
2749*7877fdebSMatt Macy 	 * Nothing to be done here but return failure.
2750*7877fdebSMatt Macy 	 */
2751*7877fdebSMatt Macy 	if (vds == NULL) {
2752*7877fdebSMatt Macy 		zio->io_error = ENXIO;
2753*7877fdebSMatt Macy 		zio_interrupt(zio);
2754*7877fdebSMatt Macy 		return;
2755*7877fdebSMatt Macy 	}
2756*7877fdebSMatt Macy 
2757*7877fdebSMatt Macy 	switch (zio->io_type) {
2758*7877fdebSMatt Macy 	case ZIO_TYPE_IOCTL:
2759*7877fdebSMatt Macy 		zio->io_error = vdev_draid_spare_ioctl(zio);
2760*7877fdebSMatt Macy 		break;
2761*7877fdebSMatt Macy 
2762*7877fdebSMatt Macy 	case ZIO_TYPE_WRITE:
2763*7877fdebSMatt Macy 		if (VDEV_OFFSET_IS_LABEL(vd, zio->io_offset)) {
2764*7877fdebSMatt Macy 			/*
2765*7877fdebSMatt Macy 			 * Accept probe IOs and config writers to simulate the
2766*7877fdebSMatt Macy 			 * existence of an on disk label.  vdev_label_sync(),
2767*7877fdebSMatt Macy 			 * vdev_uberblock_sync() and vdev_copy_uberblocks()
2768*7877fdebSMatt Macy 			 * skip the distributed spares.  This only leaves
2769*7877fdebSMatt Macy 			 * vdev_label_init() which is allowed to succeed to
2770*7877fdebSMatt Macy 			 * avoid adding special cases the function.
2771*7877fdebSMatt Macy 			 */
2772*7877fdebSMatt Macy 			if (zio->io_flags & ZIO_FLAG_PROBE ||
2773*7877fdebSMatt Macy 			    zio->io_flags & ZIO_FLAG_CONFIG_WRITER) {
2774*7877fdebSMatt Macy 				zio->io_error = 0;
2775*7877fdebSMatt Macy 			} else {
2776*7877fdebSMatt Macy 				zio->io_error = SET_ERROR(EIO);
2777*7877fdebSMatt Macy 			}
2778*7877fdebSMatt Macy 		} else {
2779*7877fdebSMatt Macy 			cvd = vdev_draid_spare_get_child(vd, offset);
2780*7877fdebSMatt Macy 
2781*7877fdebSMatt Macy 			if (cvd == NULL) {
2782*7877fdebSMatt Macy 				zio->io_error = SET_ERROR(ENXIO);
2783*7877fdebSMatt Macy 			} else {
2784*7877fdebSMatt Macy 				zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2785*7877fdebSMatt Macy 				    offset, zio->io_abd, zio->io_size,
2786*7877fdebSMatt Macy 				    zio->io_type, zio->io_priority, 0,
2787*7877fdebSMatt Macy 				    vdev_draid_spare_child_done, zio));
2788*7877fdebSMatt Macy 			}
2789*7877fdebSMatt Macy 		}
2790*7877fdebSMatt Macy 		break;
2791*7877fdebSMatt Macy 
2792*7877fdebSMatt Macy 	case ZIO_TYPE_READ:
2793*7877fdebSMatt Macy 		if (VDEV_OFFSET_IS_LABEL(vd, zio->io_offset)) {
2794*7877fdebSMatt Macy 			/*
2795*7877fdebSMatt Macy 			 * Accept probe IOs to simulate the existence of a
2796*7877fdebSMatt Macy 			 * label.  vdev_label_read_config() bypasses the
2797*7877fdebSMatt Macy 			 * pipeline to read the label configuration and
2798*7877fdebSMatt Macy 			 * vdev_uberblock_load() skips distributed spares
2799*7877fdebSMatt Macy 			 * when attempting to locate the best uberblock.
2800*7877fdebSMatt Macy 			 */
2801*7877fdebSMatt Macy 			if (zio->io_flags & ZIO_FLAG_PROBE) {
2802*7877fdebSMatt Macy 				zio->io_error = 0;
2803*7877fdebSMatt Macy 			} else {
2804*7877fdebSMatt Macy 				zio->io_error = SET_ERROR(EIO);
2805*7877fdebSMatt Macy 			}
2806*7877fdebSMatt Macy 		} else {
2807*7877fdebSMatt Macy 			cvd = vdev_draid_spare_get_child(vd, offset);
2808*7877fdebSMatt Macy 
2809*7877fdebSMatt Macy 			if (cvd == NULL || !vdev_readable(cvd)) {
2810*7877fdebSMatt Macy 				zio->io_error = SET_ERROR(ENXIO);
2811*7877fdebSMatt Macy 			} else {
2812*7877fdebSMatt Macy 				zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2813*7877fdebSMatt Macy 				    offset, zio->io_abd, zio->io_size,
2814*7877fdebSMatt Macy 				    zio->io_type, zio->io_priority, 0,
2815*7877fdebSMatt Macy 				    vdev_draid_spare_child_done, zio));
2816*7877fdebSMatt Macy 			}
2817*7877fdebSMatt Macy 		}
2818*7877fdebSMatt Macy 		break;
2819*7877fdebSMatt Macy 
2820*7877fdebSMatt Macy 	case ZIO_TYPE_TRIM:
2821*7877fdebSMatt Macy 		/* The vdev label ranges are never trimmed */
2822*7877fdebSMatt Macy 		ASSERT0(VDEV_OFFSET_IS_LABEL(vd, zio->io_offset));
2823*7877fdebSMatt Macy 
2824*7877fdebSMatt Macy 		cvd = vdev_draid_spare_get_child(vd, offset);
2825*7877fdebSMatt Macy 
2826*7877fdebSMatt Macy 		if (cvd == NULL || !cvd->vdev_has_trim) {
2827*7877fdebSMatt Macy 			zio->io_error = SET_ERROR(ENXIO);
2828*7877fdebSMatt Macy 		} else {
2829*7877fdebSMatt Macy 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
2830*7877fdebSMatt Macy 			    offset, zio->io_abd, zio->io_size,
2831*7877fdebSMatt Macy 			    zio->io_type, zio->io_priority, 0,
2832*7877fdebSMatt Macy 			    vdev_draid_spare_child_done, zio));
2833*7877fdebSMatt Macy 		}
2834*7877fdebSMatt Macy 		break;
2835*7877fdebSMatt Macy 
2836*7877fdebSMatt Macy 	default:
2837*7877fdebSMatt Macy 		zio->io_error = SET_ERROR(ENOTSUP);
2838*7877fdebSMatt Macy 		break;
2839*7877fdebSMatt Macy 	}
2840*7877fdebSMatt Macy 
2841*7877fdebSMatt Macy 	zio_execute(zio);
2842*7877fdebSMatt Macy }
2843*7877fdebSMatt Macy 
2844*7877fdebSMatt Macy /* ARGSUSED */
2845*7877fdebSMatt Macy static void
2846*7877fdebSMatt Macy vdev_draid_spare_io_done(zio_t *zio)
2847*7877fdebSMatt Macy {
2848*7877fdebSMatt Macy }
2849*7877fdebSMatt Macy 
2850*7877fdebSMatt Macy /*
2851*7877fdebSMatt Macy  * Lookup the full spare config in spa->spa_spares.sav_config and
2852*7877fdebSMatt Macy  * return the top_guid and spare_id for the named spare.
2853*7877fdebSMatt Macy  */
2854*7877fdebSMatt Macy static int
2855*7877fdebSMatt Macy vdev_draid_spare_lookup(spa_t *spa, nvlist_t *nv, uint64_t *top_guidp,
2856*7877fdebSMatt Macy     uint64_t *spare_idp)
2857*7877fdebSMatt Macy {
2858*7877fdebSMatt Macy 	nvlist_t **spares;
2859*7877fdebSMatt Macy 	uint_t nspares;
2860*7877fdebSMatt Macy 	int error;
2861*7877fdebSMatt Macy 
2862*7877fdebSMatt Macy 	if ((spa->spa_spares.sav_config == NULL) ||
2863*7877fdebSMatt Macy 	    (nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2864*7877fdebSMatt Macy 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0)) {
2865*7877fdebSMatt Macy 		return (SET_ERROR(ENOENT));
2866*7877fdebSMatt Macy 	}
2867*7877fdebSMatt Macy 
2868*7877fdebSMatt Macy 	char *spare_name;
2869*7877fdebSMatt Macy 	error = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &spare_name);
2870*7877fdebSMatt Macy 	if (error != 0)
2871*7877fdebSMatt Macy 		return (SET_ERROR(EINVAL));
2872*7877fdebSMatt Macy 
2873*7877fdebSMatt Macy 	for (int i = 0; i < nspares; i++) {
2874*7877fdebSMatt Macy 		nvlist_t *spare = spares[i];
2875*7877fdebSMatt Macy 		uint64_t top_guid, spare_id;
2876*7877fdebSMatt Macy 		char *type, *path;
2877*7877fdebSMatt Macy 
2878*7877fdebSMatt Macy 		/* Skip non-distributed spares */
2879*7877fdebSMatt Macy 		error = nvlist_lookup_string(spare, ZPOOL_CONFIG_TYPE, &type);
2880*7877fdebSMatt Macy 		if (error != 0 || strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0)
2881*7877fdebSMatt Macy 			continue;
2882*7877fdebSMatt Macy 
2883*7877fdebSMatt Macy 		/* Skip spares with the wrong name */
2884*7877fdebSMatt Macy 		error = nvlist_lookup_string(spare, ZPOOL_CONFIG_PATH, &path);
2885*7877fdebSMatt Macy 		if (error != 0 || strcmp(path, spare_name) != 0)
2886*7877fdebSMatt Macy 			continue;
2887*7877fdebSMatt Macy 
2888*7877fdebSMatt Macy 		/* Found the matching spare */
2889*7877fdebSMatt Macy 		error = nvlist_lookup_uint64(spare,
2890*7877fdebSMatt Macy 		    ZPOOL_CONFIG_TOP_GUID, &top_guid);
2891*7877fdebSMatt Macy 		if (error == 0) {
2892*7877fdebSMatt Macy 			error = nvlist_lookup_uint64(spare,
2893*7877fdebSMatt Macy 			    ZPOOL_CONFIG_SPARE_ID, &spare_id);
2894*7877fdebSMatt Macy 		}
2895*7877fdebSMatt Macy 
2896*7877fdebSMatt Macy 		if (error != 0) {
2897*7877fdebSMatt Macy 			return (SET_ERROR(EINVAL));
2898*7877fdebSMatt Macy 		} else {
2899*7877fdebSMatt Macy 			*top_guidp = top_guid;
2900*7877fdebSMatt Macy 			*spare_idp = spare_id;
2901*7877fdebSMatt Macy 			return (0);
2902*7877fdebSMatt Macy 		}
2903*7877fdebSMatt Macy 	}
2904*7877fdebSMatt Macy 
2905*7877fdebSMatt Macy 	return (SET_ERROR(ENOENT));
2906*7877fdebSMatt Macy }
2907*7877fdebSMatt Macy 
2908*7877fdebSMatt Macy /*
2909*7877fdebSMatt Macy  * Initialize private dRAID spare specific fields from the nvlist.
2910*7877fdebSMatt Macy  */
2911*7877fdebSMatt Macy static int
2912*7877fdebSMatt Macy vdev_draid_spare_init(spa_t *spa, nvlist_t *nv, void **tsd)
2913*7877fdebSMatt Macy {
2914*7877fdebSMatt Macy 	vdev_draid_spare_t *vds;
2915*7877fdebSMatt Macy 	uint64_t top_guid = 0;
2916*7877fdebSMatt Macy 	uint64_t spare_id;
2917*7877fdebSMatt Macy 
2918*7877fdebSMatt Macy 	/*
2919*7877fdebSMatt Macy 	 * In the normal case check the list of spares stored in the spa
2920*7877fdebSMatt Macy 	 * to lookup the top_guid and spare_id for provided spare config.
2921*7877fdebSMatt Macy 	 * When creating a new pool or adding vdevs the spare list is not
2922*7877fdebSMatt Macy 	 * yet populated and the values are provided in the passed config.
2923*7877fdebSMatt Macy 	 */
2924*7877fdebSMatt Macy 	if (vdev_draid_spare_lookup(spa, nv, &top_guid, &spare_id) != 0) {
2925*7877fdebSMatt Macy 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_TOP_GUID,
2926*7877fdebSMatt Macy 		    &top_guid) != 0)
2927*7877fdebSMatt Macy 			return (SET_ERROR(EINVAL));
2928*7877fdebSMatt Macy 
2929*7877fdebSMatt Macy 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_SPARE_ID,
2930*7877fdebSMatt Macy 		    &spare_id) != 0)
2931*7877fdebSMatt Macy 			return (SET_ERROR(EINVAL));
2932*7877fdebSMatt Macy 	}
2933*7877fdebSMatt Macy 
2934*7877fdebSMatt Macy 	vds = kmem_alloc(sizeof (vdev_draid_spare_t), KM_SLEEP);
2935*7877fdebSMatt Macy 	vds->vds_draid_vdev = NULL;
2936*7877fdebSMatt Macy 	vds->vds_top_guid = top_guid;
2937*7877fdebSMatt Macy 	vds->vds_spare_id = spare_id;
2938*7877fdebSMatt Macy 
2939*7877fdebSMatt Macy 	*tsd = vds;
2940*7877fdebSMatt Macy 
2941*7877fdebSMatt Macy 	return (0);
2942*7877fdebSMatt Macy }
2943*7877fdebSMatt Macy 
2944*7877fdebSMatt Macy static void
2945*7877fdebSMatt Macy vdev_draid_spare_fini(vdev_t *vd)
2946*7877fdebSMatt Macy {
2947*7877fdebSMatt Macy 	kmem_free(vd->vdev_tsd, sizeof (vdev_draid_spare_t));
2948*7877fdebSMatt Macy }
2949*7877fdebSMatt Macy 
2950*7877fdebSMatt Macy static void
2951*7877fdebSMatt Macy vdev_draid_spare_config_generate(vdev_t *vd, nvlist_t *nv)
2952*7877fdebSMatt Macy {
2953*7877fdebSMatt Macy 	vdev_draid_spare_t *vds = vd->vdev_tsd;
2954*7877fdebSMatt Macy 
2955*7877fdebSMatt Macy 	ASSERT3P(vd->vdev_ops, ==, &vdev_draid_spare_ops);
2956*7877fdebSMatt Macy 
2957*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_TOP_GUID, vds->vds_top_guid);
2958*7877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_SPARE_ID, vds->vds_spare_id);
2959*7877fdebSMatt Macy }
2960*7877fdebSMatt Macy 
2961*7877fdebSMatt Macy vdev_ops_t vdev_draid_spare_ops = {
2962*7877fdebSMatt Macy 	.vdev_op_init = vdev_draid_spare_init,
2963*7877fdebSMatt Macy 	.vdev_op_fini = vdev_draid_spare_fini,
2964*7877fdebSMatt Macy 	.vdev_op_open = vdev_draid_spare_open,
2965*7877fdebSMatt Macy 	.vdev_op_close = vdev_draid_spare_close,
2966*7877fdebSMatt Macy 	.vdev_op_asize = vdev_default_asize,
2967*7877fdebSMatt Macy 	.vdev_op_min_asize = vdev_default_min_asize,
2968*7877fdebSMatt Macy 	.vdev_op_min_alloc = NULL,
2969*7877fdebSMatt Macy 	.vdev_op_io_start = vdev_draid_spare_io_start,
2970*7877fdebSMatt Macy 	.vdev_op_io_done = vdev_draid_spare_io_done,
2971*7877fdebSMatt Macy 	.vdev_op_state_change = NULL,
2972*7877fdebSMatt Macy 	.vdev_op_need_resilver = NULL,
2973*7877fdebSMatt Macy 	.vdev_op_hold = NULL,
2974*7877fdebSMatt Macy 	.vdev_op_rele = NULL,
2975*7877fdebSMatt Macy 	.vdev_op_remap = NULL,
2976*7877fdebSMatt Macy 	.vdev_op_xlate = vdev_default_xlate,
2977*7877fdebSMatt Macy 	.vdev_op_rebuild_asize = NULL,
2978*7877fdebSMatt Macy 	.vdev_op_metaslab_init = NULL,
2979*7877fdebSMatt Macy 	.vdev_op_config_generate = vdev_draid_spare_config_generate,
2980*7877fdebSMatt Macy 	.vdev_op_nparity = NULL,
2981*7877fdebSMatt Macy 	.vdev_op_ndisks = NULL,
2982*7877fdebSMatt Macy 	.vdev_op_type = VDEV_TYPE_DRAID_SPARE,
2983*7877fdebSMatt Macy 	.vdev_op_leaf = B_TRUE,
2984*7877fdebSMatt Macy };
2985