1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
24 */
25
26 #ifndef _VDEV_RAIDZ_H
27 #define _VDEV_RAIDZ_H
28
29 #include <sys/types.h>
30 #include <sys/debug.h>
31 #include <sys/kstat.h>
32 #include <sys/abd.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/abd_impl.h>
35 #include <sys/zfs_rlock.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #define CODE_P (0U)
42 #define CODE_Q (1U)
43 #define CODE_R (2U)
44
45 #define PARITY_P (1U)
46 #define PARITY_PQ (2U)
47 #define PARITY_PQR (3U)
48
49 #define TARGET_X (0U)
50 #define TARGET_Y (1U)
51 #define TARGET_Z (2U)
52
53 /*
54 * Parity generation methods indexes
55 */
56 enum raidz_math_gen_op {
57 RAIDZ_GEN_P = 0,
58 RAIDZ_GEN_PQ,
59 RAIDZ_GEN_PQR,
60 RAIDZ_GEN_NUM = 3
61 };
62 /*
63 * Data reconstruction methods indexes
64 */
65 enum raidz_rec_op {
66 RAIDZ_REC_P = 0,
67 RAIDZ_REC_Q,
68 RAIDZ_REC_R,
69 RAIDZ_REC_PQ,
70 RAIDZ_REC_PR,
71 RAIDZ_REC_QR,
72 RAIDZ_REC_PQR,
73 RAIDZ_REC_NUM = 7
74 };
75
76 extern const char *const raidz_gen_name[RAIDZ_GEN_NUM];
77 extern const char *const raidz_rec_name[RAIDZ_REC_NUM];
78
79 /*
80 * Methods used to define raidz implementation
81 *
82 * @raidz_gen_f Parity generation function
83 * @par1 pointer to raidz_map
84 * @raidz_rec_f Data reconstruction function
85 * @par1 pointer to raidz_map
86 * @par2 array of reconstruction targets
87 * @will_work_f Function returns TRUE if impl. is supported on the system
88 * @init_impl_f Function is called once on init
89 * @fini_impl_f Function is called once on fini
90 */
91 typedef void (*raidz_gen_f)(void *);
92 typedef int (*raidz_rec_f)(void *, const int *);
93 typedef boolean_t (*will_work_f)(void);
94 typedef void (*init_impl_f)(void);
95 typedef void (*fini_impl_f)(void);
96
97 #define RAIDZ_IMPL_NAME_MAX (20)
98
99 typedef struct raidz_impl_ops {
100 init_impl_f init;
101 fini_impl_f fini;
102 raidz_gen_f gen[RAIDZ_GEN_NUM]; /* Parity generate functions */
103 raidz_rec_f rec[RAIDZ_REC_NUM]; /* Data reconstruction functions */
104 will_work_f is_supported; /* Support check function */
105 char name[RAIDZ_IMPL_NAME_MAX]; /* Name of the implementation */
106 } raidz_impl_ops_t;
107
108
109 typedef struct raidz_col {
110 int rc_devidx; /* child device index for I/O */
111 uint32_t rc_size; /* I/O size */
112 uint64_t rc_offset; /* device offset */
113 abd_t rc_abdstruct; /* rc_abd probably points here */
114 abd_t *rc_abd; /* I/O data */
115 abd_t *rc_orig_data; /* pre-reconstruction */
116 int rc_error; /* I/O error for this device */
117 uint8_t rc_tried:1; /* Did we attempt this I/O column? */
118 uint8_t rc_skipped:1; /* Did we skip this I/O column? */
119 uint8_t rc_need_orig_restore:1; /* need to restore from orig_data? */
120 uint8_t rc_force_repair:1; /* Write good data to this column */
121 uint8_t rc_allow_repair:1; /* Allow repair I/O to this column */
122 int rc_shadow_devidx; /* for double write during expansion */
123 int rc_shadow_error; /* for double write during expansion */
124 uint64_t rc_shadow_offset; /* for double write during expansion */
125 } raidz_col_t;
126
127 typedef struct raidz_row {
128 int rr_cols; /* Regular column count */
129 int rr_scols; /* Count including skipped columns */
130 int rr_bigcols; /* Remainder data column count */
131 int rr_missingdata; /* Count of missing data devices */
132 int rr_missingparity; /* Count of missing parity devices */
133 int rr_firstdatacol; /* First data column/parity count */
134 abd_t *rr_abd_empty; /* dRAID empty sector buffer */
135 int rr_nempty; /* empty sectors included in parity */
136 #ifdef ZFS_DEBUG
137 uint64_t rr_offset; /* Logical offset for *_io_verify() */
138 uint64_t rr_size; /* Physical size for *_io_verify() */
139 #endif
140 raidz_col_t rr_col[]; /* Flexible array of I/O columns */
141 } raidz_row_t;
142
143 typedef struct raidz_map {
144 boolean_t rm_ecksuminjected; /* checksum error was injected */
145 int rm_nrows; /* Regular row count */
146 int rm_nskip; /* RAIDZ sectors skipped for padding */
147 int rm_skipstart; /* Column index of padding start */
148 int rm_original_width; /* pre-expansion width of raidz vdev */
149 int rm_nphys_cols; /* num entries in rm_phys_col[] */
150 zfs_locked_range_t *rm_lr;
151 const raidz_impl_ops_t *rm_ops; /* RAIDZ math operations */
152 raidz_col_t *rm_phys_col; /* if non-NULL, read i/o aggregation */
153 raidz_row_t *rm_row[]; /* flexible array of rows */
154 } raidz_map_t;
155
156 /*
157 * Nodes in vdev_raidz_t:vd_expand_txgs.
158 * Blocks with physical birth time of re_txg or later have the specified
159 * logical width (until the next node).
160 */
161 typedef struct reflow_node {
162 uint64_t re_txg;
163 uint64_t re_logical_width;
164 avl_node_t re_link;
165 } reflow_node_t;
166
167
168 #define RAIDZ_ORIGINAL_IMPL (INT_MAX)
169
170 extern const raidz_impl_ops_t vdev_raidz_scalar_impl;
171 extern boolean_t raidz_will_scalar_work(void);
172
173 #if defined(__x86_64) && defined(HAVE_SSE2) /* only x86_64 for now */
174 extern const raidz_impl_ops_t vdev_raidz_sse2_impl;
175 #endif
176 #if defined(__x86_64) && defined(HAVE_SSSE3) /* only x86_64 for now */
177 extern const raidz_impl_ops_t vdev_raidz_ssse3_impl;
178 #endif
179 #if defined(__x86_64) && defined(HAVE_AVX2) /* only x86_64 for now */
180 extern const raidz_impl_ops_t vdev_raidz_avx2_impl;
181 #endif
182 #if defined(__x86_64) && defined(HAVE_AVX512F) /* only x86_64 for now */
183 extern const raidz_impl_ops_t vdev_raidz_avx512f_impl;
184 #endif
185 #if defined(__x86_64) && defined(HAVE_AVX512BW) /* only x86_64 for now */
186 extern const raidz_impl_ops_t vdev_raidz_avx512bw_impl;
187 #endif
188 #if defined(__aarch64__)
189 extern const raidz_impl_ops_t vdev_raidz_aarch64_neon_impl;
190 extern const raidz_impl_ops_t vdev_raidz_aarch64_neonx2_impl;
191 #endif
192 #if defined(__powerpc__)
193 extern const raidz_impl_ops_t vdev_raidz_powerpc_altivec_impl;
194 #endif
195
196 /*
197 * Commonly used raidz_map helpers
198 *
199 * raidz_parity Returns parity of the RAIDZ block
200 * raidz_ncols Returns number of columns the block spans
201 * Note, all rows have the same number of columns.
202 * raidz_nbigcols Returns number of big columns
203 * raidz_col_p Returns pointer to a column
204 * raidz_col_size Returns size of a column
205 * raidz_big_size Returns size of big columns
206 * raidz_short_size Returns size of short columns
207 */
208 #define raidz_parity(rm) ((rm)->rm_row[0]->rr_firstdatacol)
209 #define raidz_ncols(rm) ((rm)->rm_row[0]->rr_cols)
210 #define raidz_nbigcols(rm) ((rm)->rm_bigcols)
211 #define raidz_col_p(rm, c) ((rm)->rm_col + (c))
212 #define raidz_col_size(rm, c) ((rm)->rm_col[c].rc_size)
213 #define raidz_big_size(rm) (raidz_col_size(rm, CODE_P))
214 #define raidz_short_size(rm) (raidz_col_size(rm, raidz_ncols(rm)-1))
215
216 /*
217 * Macro defines an RAIDZ parity generation method
218 *
219 * @code parity the function produce
220 * @impl name of the implementation
221 */
222 #define _RAIDZ_GEN_WRAP(code, impl) \
223 static void \
224 impl ## _gen_ ## code(void *rrp) \
225 { \
226 raidz_row_t *rr = (raidz_row_t *)rrp; \
227 raidz_generate_## code ## _impl(rr); \
228 }
229
230 /*
231 * Macro defines an RAIDZ data reconstruction method
232 *
233 * @code parity the function produce
234 * @impl name of the implementation
235 */
236 #define _RAIDZ_REC_WRAP(code, impl) \
237 static int \
238 impl ## _rec_ ## code(void *rrp, const int *tgtidx) \
239 { \
240 raidz_row_t *rr = (raidz_row_t *)rrp; \
241 return (raidz_reconstruct_## code ## _impl(rr, tgtidx)); \
242 }
243
244 /*
245 * Define all gen methods for an implementation
246 *
247 * @impl name of the implementation
248 */
249 #define DEFINE_GEN_METHODS(impl) \
250 _RAIDZ_GEN_WRAP(p, impl); \
251 _RAIDZ_GEN_WRAP(pq, impl); \
252 _RAIDZ_GEN_WRAP(pqr, impl)
253
254 /*
255 * Define all rec functions for an implementation
256 *
257 * @impl name of the implementation
258 */
259 #define DEFINE_REC_METHODS(impl) \
260 _RAIDZ_REC_WRAP(p, impl); \
261 _RAIDZ_REC_WRAP(q, impl); \
262 _RAIDZ_REC_WRAP(r, impl); \
263 _RAIDZ_REC_WRAP(pq, impl); \
264 _RAIDZ_REC_WRAP(pr, impl); \
265 _RAIDZ_REC_WRAP(qr, impl); \
266 _RAIDZ_REC_WRAP(pqr, impl)
267
268 #define RAIDZ_GEN_METHODS(impl) \
269 { \
270 [RAIDZ_GEN_P] = & impl ## _gen_p, \
271 [RAIDZ_GEN_PQ] = & impl ## _gen_pq, \
272 [RAIDZ_GEN_PQR] = & impl ## _gen_pqr \
273 }
274
275 #define RAIDZ_REC_METHODS(impl) \
276 { \
277 [RAIDZ_REC_P] = & impl ## _rec_p, \
278 [RAIDZ_REC_Q] = & impl ## _rec_q, \
279 [RAIDZ_REC_R] = & impl ## _rec_r, \
280 [RAIDZ_REC_PQ] = & impl ## _rec_pq, \
281 [RAIDZ_REC_PR] = & impl ## _rec_pr, \
282 [RAIDZ_REC_QR] = & impl ## _rec_qr, \
283 [RAIDZ_REC_PQR] = & impl ## _rec_pqr \
284 }
285
286
287 typedef struct raidz_impl_kstat {
288 uint64_t gen[RAIDZ_GEN_NUM]; /* gen method speed B/s */
289 uint64_t rec[RAIDZ_REC_NUM]; /* rec method speed B/s */
290 } raidz_impl_kstat_t;
291
292 /*
293 * Enumerate various multiplication constants
294 * used in reconstruction methods
295 */
296 typedef enum raidz_mul_info {
297 /* Reconstruct Q */
298 MUL_Q_X = 0,
299 /* Reconstruct R */
300 MUL_R_X = 0,
301 /* Reconstruct PQ */
302 MUL_PQ_X = 0,
303 MUL_PQ_Y = 1,
304 /* Reconstruct PR */
305 MUL_PR_X = 0,
306 MUL_PR_Y = 1,
307 /* Reconstruct QR */
308 MUL_QR_XQ = 0,
309 MUL_QR_X = 1,
310 MUL_QR_YQ = 2,
311 MUL_QR_Y = 3,
312 /* Reconstruct PQR */
313 MUL_PQR_XP = 0,
314 MUL_PQR_XQ = 1,
315 MUL_PQR_XR = 2,
316 MUL_PQR_YU = 3,
317 MUL_PQR_YP = 4,
318 MUL_PQR_YQ = 5,
319
320 MUL_CNT = 6
321 } raidz_mul_info_t;
322
323 /*
324 * Powers of 2 in the Galois field.
325 */
326 extern const uint8_t vdev_raidz_pow2[256] __attribute__((aligned(256)));
327 /* Logs of 2 in the Galois field defined above. */
328 extern const uint8_t vdev_raidz_log2[256] __attribute__((aligned(256)));
329
330 /*
331 * Multiply a given number by 2 raised to the given power.
332 */
333 static inline uint8_t
vdev_raidz_exp2(const uint8_t a,const unsigned exp)334 vdev_raidz_exp2(const uint8_t a, const unsigned exp)
335 {
336 if (a == 0)
337 return (0);
338
339 return (vdev_raidz_pow2[(exp + (unsigned)vdev_raidz_log2[a]) % 255]);
340 }
341
342 /*
343 * Galois Field operations.
344 *
345 * gf_exp2 - computes 2 raised to the given power
346 * gf_exp4 - computes 4 raised to the given power
347 * gf_mul - multiplication
348 * gf_div - division
349 * gf_inv - multiplicative inverse
350 */
351 typedef unsigned gf_t;
352 typedef unsigned gf_log_t;
353
354 static inline gf_t
gf_mul(const gf_t a,const gf_t b)355 gf_mul(const gf_t a, const gf_t b)
356 {
357 gf_log_t logsum;
358
359 if (a == 0 || b == 0)
360 return (0);
361
362 logsum = (gf_log_t)vdev_raidz_log2[a] + (gf_log_t)vdev_raidz_log2[b];
363
364 return ((gf_t)vdev_raidz_pow2[logsum % 255]);
365 }
366
367 static inline gf_t
gf_div(const gf_t a,const gf_t b)368 gf_div(const gf_t a, const gf_t b)
369 {
370 gf_log_t logsum;
371
372 ASSERT3U(b, >, 0);
373 if (a == 0)
374 return (0);
375
376 logsum = (gf_log_t)255 + (gf_log_t)vdev_raidz_log2[a] -
377 (gf_log_t)vdev_raidz_log2[b];
378
379 return ((gf_t)vdev_raidz_pow2[logsum % 255]);
380 }
381
382 static inline gf_t
gf_inv(const gf_t a)383 gf_inv(const gf_t a)
384 {
385 gf_log_t logsum;
386
387 ASSERT3U(a, >, 0);
388
389 logsum = (gf_log_t)255 - (gf_log_t)vdev_raidz_log2[a];
390
391 return ((gf_t)vdev_raidz_pow2[logsum]);
392 }
393
394 static inline gf_t
gf_exp2(gf_log_t exp)395 gf_exp2(gf_log_t exp)
396 {
397 return (vdev_raidz_pow2[exp % 255]);
398 }
399
400 static inline gf_t
gf_exp4(gf_log_t exp)401 gf_exp4(gf_log_t exp)
402 {
403 ASSERT3U(exp, <=, 255);
404 return ((gf_t)vdev_raidz_pow2[(2 * exp) % 255]);
405 }
406
407 #ifdef __cplusplus
408 }
409 #endif
410
411 #endif /* _VDEV_RAIDZ_H */
412