xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_raidz_math.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
14  */
15 
16 #include <sys/simd.h>
17 #include <sys/zfs_context.h>
18 #include <sys/types.h>
19 #include <sys/zio.h>
20 #include <sys/debug.h>
21 #include <sys/zfs_debug.h>
22 #include <sys/vdev_raidz.h>
23 #include <sys/vdev_raidz_impl.h>
24 
25 /* Opaque implementation with NULL methods to represent original methods */
26 static const raidz_impl_ops_t vdev_raidz_original_impl = {
27 	.name = "original",
28 	.is_supported = raidz_will_scalar_work,
29 };
30 
31 /* RAIDZ parity op that contain the fastest methods */
32 static raidz_impl_ops_t vdev_raidz_fastest_impl = {
33 	.name = "fastest"
34 };
35 
36 /* All compiled in implementations */
37 static const raidz_impl_ops_t *const raidz_all_maths[] = {
38 	&vdev_raidz_original_impl,
39 	&vdev_raidz_scalar_impl,
40 #if defined(__x86_64) && HAVE_SIMD(SSE2)	/* only x86_64 for now */
41 	&vdev_raidz_sse2_impl,
42 #endif
43 #if defined(__x86_64) && HAVE_SIMD(SSSE3)	/* only x86_64 for now */
44 	&vdev_raidz_ssse3_impl,
45 #endif
46 #if defined(__x86_64) && HAVE_SIMD(AVX2)	/* only x86_64 for now */
47 	&vdev_raidz_avx2_impl,
48 #endif
49 #if defined(__x86_64) && HAVE_SIMD(AVX512F)	/* only x86_64 for now */
50 	&vdev_raidz_avx512f_impl,
51 #endif
52 #if defined(__x86_64) && HAVE_SIMD(AVX512BW)	/* only x86_64 for now */
53 	&vdev_raidz_avx512bw_impl,
54 #endif
55 #if defined(__aarch64__) && !defined(__FreeBSD__)
56 	&vdev_raidz_aarch64_neon_impl,
57 	&vdev_raidz_aarch64_neonx2_impl,
58 #endif
59 #if defined(__powerpc__) && defined(__altivec__)
60 	&vdev_raidz_powerpc_altivec_impl,
61 #endif
62 };
63 
64 /* Indicate that benchmark has been completed */
65 static boolean_t raidz_math_initialized = B_FALSE;
66 
67 /* Select raidz implementation */
68 #define	IMPL_FASTEST	(UINT32_MAX)
69 #define	IMPL_CYCLE	(UINT32_MAX - 1)
70 #define	IMPL_ORIGINAL	(0)
71 #define	IMPL_SCALAR	(1)
72 
73 #define	RAIDZ_IMPL_READ(i)	(*(volatile uint32_t *) &(i))
74 
75 uint32_t zfs_vdev_raidz_impl = IMPL_SCALAR;
76 static uint32_t user_sel_impl = IMPL_FASTEST;
77 
78 /* Hold all supported implementations */
79 static size_t raidz_supp_impl_cnt = 0;
80 static raidz_impl_ops_t *raidz_supp_impl[ARRAY_SIZE(raidz_all_maths)];
81 
82 #if defined(_KERNEL)
83 /*
84  * kstats values for supported implementations
85  * Values represent per disk throughput of 8 disk+parity raidz vdev [B/s]
86  */
87 static raidz_impl_kstat_t raidz_impl_kstats[ARRAY_SIZE(raidz_all_maths) + 1];
88 
89 /* kstat for benchmarked implementations */
90 static kstat_t *raidz_math_kstat = NULL;
91 #endif
92 
93 /*
94  * Returns the RAIDZ operations for raidz_map() parity calculations.   When
95  * a SIMD implementation is not allowed in the current context, then fallback
96  * to the fastest generic implementation.
97  */
98 const raidz_impl_ops_t *
vdev_raidz_math_get_ops(void)99 vdev_raidz_math_get_ops(void)
100 {
101 	if (!kfpu_allowed())
102 		return (&vdev_raidz_scalar_impl);
103 
104 	raidz_impl_ops_t *ops = NULL;
105 	const uint32_t impl = RAIDZ_IMPL_READ(zfs_vdev_raidz_impl);
106 
107 	switch (impl) {
108 	case IMPL_FASTEST:
109 		ASSERT(raidz_math_initialized);
110 		ops = &vdev_raidz_fastest_impl;
111 		break;
112 	case IMPL_CYCLE:
113 		/* Cycle through all supported implementations */
114 		ASSERT(raidz_math_initialized);
115 		ASSERT3U(raidz_supp_impl_cnt, >, 0);
116 		static size_t cycle_impl_idx = 0;
117 		size_t idx = (++cycle_impl_idx) % raidz_supp_impl_cnt;
118 		ops = raidz_supp_impl[idx];
119 		break;
120 	case IMPL_ORIGINAL:
121 		ops = (raidz_impl_ops_t *)&vdev_raidz_original_impl;
122 		break;
123 	case IMPL_SCALAR:
124 		ops = (raidz_impl_ops_t *)&vdev_raidz_scalar_impl;
125 		break;
126 	default:
127 		ASSERT3U(impl, <, raidz_supp_impl_cnt);
128 		ASSERT3U(raidz_supp_impl_cnt, >, 0);
129 		if (impl < ARRAY_SIZE(raidz_all_maths))
130 			ops = raidz_supp_impl[impl];
131 		break;
132 	}
133 
134 	ASSERT3P(ops, !=, NULL);
135 
136 	return (ops);
137 }
138 
139 /*
140  * Select parity generation method for raidz_map
141  */
142 int
vdev_raidz_math_generate(raidz_map_t * rm,raidz_row_t * rr)143 vdev_raidz_math_generate(raidz_map_t *rm, raidz_row_t *rr)
144 {
145 	raidz_gen_f gen_parity = NULL;
146 
147 	switch (raidz_parity(rm)) {
148 		case 1:
149 			gen_parity = rm->rm_ops->gen[RAIDZ_GEN_P];
150 			break;
151 		case 2:
152 			gen_parity = rm->rm_ops->gen[RAIDZ_GEN_PQ];
153 			break;
154 		case 3:
155 			gen_parity = rm->rm_ops->gen[RAIDZ_GEN_PQR];
156 			break;
157 		default:
158 			gen_parity = NULL;
159 			cmn_err(CE_PANIC, "invalid RAID-Z configuration %llu",
160 			    (u_longlong_t)raidz_parity(rm));
161 			break;
162 	}
163 
164 	/* if method is NULL execute the original implementation */
165 	if (gen_parity == NULL)
166 		return (RAIDZ_ORIGINAL_IMPL);
167 
168 	gen_parity(rr);
169 
170 	return (0);
171 }
172 
173 static raidz_rec_f
reconstruct_fun_p_sel(raidz_map_t * rm,const int * parity_valid,const int nbaddata)174 reconstruct_fun_p_sel(raidz_map_t *rm, const int *parity_valid,
175     const int nbaddata)
176 {
177 	if (nbaddata == 1 && parity_valid[CODE_P]) {
178 		return (rm->rm_ops->rec[RAIDZ_REC_P]);
179 	}
180 	return ((raidz_rec_f) NULL);
181 }
182 
183 static raidz_rec_f
reconstruct_fun_pq_sel(raidz_map_t * rm,const int * parity_valid,const int nbaddata)184 reconstruct_fun_pq_sel(raidz_map_t *rm, const int *parity_valid,
185     const int nbaddata)
186 {
187 	if (nbaddata == 1) {
188 		if (parity_valid[CODE_P]) {
189 			return (rm->rm_ops->rec[RAIDZ_REC_P]);
190 		} else if (parity_valid[CODE_Q]) {
191 			return (rm->rm_ops->rec[RAIDZ_REC_Q]);
192 		}
193 	} else if (nbaddata == 2 &&
194 	    parity_valid[CODE_P] && parity_valid[CODE_Q]) {
195 		return (rm->rm_ops->rec[RAIDZ_REC_PQ]);
196 	}
197 	return ((raidz_rec_f) NULL);
198 }
199 
200 static raidz_rec_f
reconstruct_fun_pqr_sel(raidz_map_t * rm,const int * parity_valid,const int nbaddata)201 reconstruct_fun_pqr_sel(raidz_map_t *rm, const int *parity_valid,
202     const int nbaddata)
203 {
204 	if (nbaddata == 1) {
205 		if (parity_valid[CODE_P]) {
206 			return (rm->rm_ops->rec[RAIDZ_REC_P]);
207 		} else if (parity_valid[CODE_Q]) {
208 			return (rm->rm_ops->rec[RAIDZ_REC_Q]);
209 		} else if (parity_valid[CODE_R]) {
210 			return (rm->rm_ops->rec[RAIDZ_REC_R]);
211 		}
212 	} else if (nbaddata == 2) {
213 		if (parity_valid[CODE_P] && parity_valid[CODE_Q]) {
214 			return (rm->rm_ops->rec[RAIDZ_REC_PQ]);
215 		} else if (parity_valid[CODE_P] && parity_valid[CODE_R]) {
216 			return (rm->rm_ops->rec[RAIDZ_REC_PR]);
217 		} else if (parity_valid[CODE_Q] && parity_valid[CODE_R]) {
218 			return (rm->rm_ops->rec[RAIDZ_REC_QR]);
219 		}
220 	} else if (nbaddata == 3 &&
221 	    parity_valid[CODE_P] && parity_valid[CODE_Q] &&
222 	    parity_valid[CODE_R]) {
223 		return (rm->rm_ops->rec[RAIDZ_REC_PQR]);
224 	}
225 	return ((raidz_rec_f) NULL);
226 }
227 
228 /*
229  * Select data reconstruction method for raidz_map
230  * @parity_valid - Parity validity flag
231  * @dt           - Failed data index array
232  * @nbaddata     - Number of failed data columns
233  */
234 int
vdev_raidz_math_reconstruct(raidz_map_t * rm,raidz_row_t * rr,const int * parity_valid,const int * dt,const int nbaddata)235 vdev_raidz_math_reconstruct(raidz_map_t *rm, raidz_row_t *rr,
236     const int *parity_valid, const int *dt, const int nbaddata)
237 {
238 	raidz_rec_f rec_fn = NULL;
239 
240 	switch (raidz_parity(rm)) {
241 	case PARITY_P:
242 		rec_fn = reconstruct_fun_p_sel(rm, parity_valid, nbaddata);
243 		break;
244 	case PARITY_PQ:
245 		rec_fn = reconstruct_fun_pq_sel(rm, parity_valid, nbaddata);
246 		break;
247 	case PARITY_PQR:
248 		rec_fn = reconstruct_fun_pqr_sel(rm, parity_valid, nbaddata);
249 		break;
250 	default:
251 		cmn_err(CE_PANIC, "invalid RAID-Z configuration %llu",
252 		    (u_longlong_t)raidz_parity(rm));
253 		break;
254 	}
255 
256 	if (rec_fn == NULL)
257 		return (RAIDZ_ORIGINAL_IMPL);
258 	else
259 		return (rec_fn(rr, dt));
260 }
261 
262 const char *const raidz_gen_name[] = {
263 	"gen_p", "gen_pq", "gen_pqr"
264 };
265 const char *const raidz_rec_name[] = {
266 	"rec_p", "rec_q", "rec_r",
267 	"rec_pq", "rec_pr", "rec_qr", "rec_pqr"
268 };
269 
270 #if defined(_KERNEL)
271 
272 #define	RAIDZ_KSTAT_LINE_LEN	(17 + 10*12 + 1)
273 
274 static int
raidz_math_kstat_headers(char * buf,size_t size)275 raidz_math_kstat_headers(char *buf, size_t size)
276 {
277 	ASSERT3U(size, >=, RAIDZ_KSTAT_LINE_LEN);
278 
279 	ssize_t off = kmem_scnprintf(buf, size, "%-17s", "implementation");
280 
281 	for (int i = 0; i < ARRAY_SIZE(raidz_gen_name); i++)
282 		off += kmem_scnprintf(buf + off, size - off, "%-16s",
283 		    raidz_gen_name[i]);
284 
285 	for (int i = 0; i < ARRAY_SIZE(raidz_rec_name); i++)
286 		off += kmem_scnprintf(buf + off, size - off, "%-16s",
287 		    raidz_rec_name[i]);
288 
289 	(void) kmem_scnprintf(buf + off, size - off, "\n");
290 
291 	return (0);
292 }
293 
294 static int
raidz_math_kstat_data(char * buf,size_t size,void * data)295 raidz_math_kstat_data(char *buf, size_t size, void *data)
296 {
297 	raidz_impl_kstat_t *fstat = &raidz_impl_kstats[raidz_supp_impl_cnt];
298 	raidz_impl_kstat_t *cstat = (raidz_impl_kstat_t *)data;
299 	ssize_t off = 0;
300 	int i;
301 
302 	ASSERT3U(size, >=, RAIDZ_KSTAT_LINE_LEN);
303 
304 	if (cstat == fstat) {
305 		off += kmem_scnprintf(buf + off, size - off, "%-17s",
306 		    "fastest");
307 
308 		for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++) {
309 			int id = fstat->gen[i];
310 			off += kmem_scnprintf(buf + off, size - off, "%-16s",
311 			    raidz_supp_impl[id]->name);
312 		}
313 		for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++) {
314 			int id = fstat->rec[i];
315 			off += kmem_scnprintf(buf + off, size - off, "%-16s",
316 			    raidz_supp_impl[id]->name);
317 		}
318 	} else {
319 		ptrdiff_t id = cstat - raidz_impl_kstats;
320 
321 		off += kmem_scnprintf(buf + off, size - off, "%-17s",
322 		    raidz_supp_impl[id]->name);
323 
324 		for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++)
325 			off += kmem_scnprintf(buf + off, size - off, "%-16llu",
326 			    (u_longlong_t)cstat->gen[i]);
327 
328 		for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++)
329 			off += kmem_scnprintf(buf + off, size - off, "%-16llu",
330 			    (u_longlong_t)cstat->rec[i]);
331 	}
332 
333 	(void) kmem_scnprintf(buf + off, size - off, "\n");
334 
335 	return (0);
336 }
337 
338 static void *
raidz_math_kstat_addr(kstat_t * ksp,loff_t n)339 raidz_math_kstat_addr(kstat_t *ksp, loff_t n)
340 {
341 	if (n <= raidz_supp_impl_cnt)
342 		ksp->ks_private = (void *) (raidz_impl_kstats + n);
343 	else
344 		ksp->ks_private = NULL;
345 
346 	return (ksp->ks_private);
347 }
348 
349 #define	BENCH_D_COLS	(8ULL)
350 #define	BENCH_COLS	(BENCH_D_COLS + PARITY_PQR)
351 #define	BENCH_ZIO_SIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)	/* 128 kiB */
352 #define	BENCH_NS	MSEC2NSEC(1)			/* 1ms */
353 
354 typedef void (*benchmark_fn)(raidz_map_t *rm, const int fn);
355 
356 static void
benchmark_gen_impl(raidz_map_t * rm,const int fn)357 benchmark_gen_impl(raidz_map_t *rm, const int fn)
358 {
359 	(void) fn;
360 	vdev_raidz_generate_parity(rm);
361 }
362 
363 static void
benchmark_rec_impl(raidz_map_t * rm,const int fn)364 benchmark_rec_impl(raidz_map_t *rm, const int fn)
365 {
366 	static const int rec_tgt[7][3] = {
367 		{1, 2, 3},	/* rec_p:   bad QR & D[0]	*/
368 		{0, 2, 3},	/* rec_q:   bad PR & D[0]	*/
369 		{0, 1, 3},	/* rec_r:   bad PQ & D[0]	*/
370 		{2, 3, 4},	/* rec_pq:  bad R  & D[0][1]	*/
371 		{1, 3, 4},	/* rec_pr:  bad Q  & D[0][1]	*/
372 		{0, 3, 4},	/* rec_qr:  bad P  & D[0][1]	*/
373 		{3, 4, 5}	/* rec_pqr: bad    & D[0][1][2] */
374 	};
375 
376 	vdev_raidz_reconstruct(rm, rec_tgt[fn], 3);
377 }
378 
379 /*
380  * Benchmarking of all supported implementations (raidz_supp_impl_cnt)
381  * is performed by setting the rm_ops pointer and calling the top level
382  * generate/reconstruct methods of bench_rm.
383  */
384 static void
benchmark_raidz_impl(raidz_map_t * bench_rm,const int fn,benchmark_fn bench_fn)385 benchmark_raidz_impl(raidz_map_t *bench_rm, const int fn, benchmark_fn bench_fn)
386 {
387 	uint64_t run_cnt, speed, best_speed = 0;
388 	hrtime_t t_start, t_diff;
389 	raidz_impl_ops_t *curr_impl;
390 	raidz_impl_kstat_t *fstat = &raidz_impl_kstats[raidz_supp_impl_cnt];
391 	int impl, i;
392 
393 	for (impl = 0; impl < raidz_supp_impl_cnt; impl++) {
394 		/* set an implementation to benchmark */
395 		curr_impl = raidz_supp_impl[impl];
396 		bench_rm->rm_ops = curr_impl;
397 
398 		run_cnt = 0;
399 		t_start = gethrtime();
400 
401 		do {
402 			for (i = 0; i < 5; i++, run_cnt++)
403 				bench_fn(bench_rm, fn);
404 
405 			t_diff = gethrtime() - t_start;
406 		} while (t_diff < BENCH_NS);
407 
408 		speed = run_cnt * BENCH_ZIO_SIZE * NANOSEC;
409 		speed /= (t_diff * BENCH_COLS);
410 
411 		if (bench_fn == benchmark_gen_impl)
412 			raidz_impl_kstats[impl].gen[fn] = speed;
413 		else
414 			raidz_impl_kstats[impl].rec[fn] = speed;
415 
416 		/* Update fastest implementation method */
417 		if (speed > best_speed) {
418 			best_speed = speed;
419 
420 			if (bench_fn == benchmark_gen_impl) {
421 				fstat->gen[fn] = impl;
422 				vdev_raidz_fastest_impl.gen[fn] =
423 				    curr_impl->gen[fn];
424 			} else {
425 				fstat->rec[fn] = impl;
426 				vdev_raidz_fastest_impl.rec[fn] =
427 				    curr_impl->rec[fn];
428 			}
429 		}
430 	}
431 }
432 #endif
433 
434 /*
435  * Initialize and benchmark all supported implementations.
436  */
437 static void
benchmark_raidz(void)438 benchmark_raidz(void)
439 {
440 	raidz_impl_ops_t *curr_impl;
441 	int i, c;
442 
443 	/* Move supported impl into raidz_supp_impl */
444 	for (i = 0, c = 0; i < ARRAY_SIZE(raidz_all_maths); i++) {
445 		curr_impl = (raidz_impl_ops_t *)raidz_all_maths[i];
446 
447 		if (curr_impl->init)
448 			curr_impl->init();
449 
450 		if (curr_impl->is_supported())
451 			raidz_supp_impl[c++] = (raidz_impl_ops_t *)curr_impl;
452 	}
453 	membar_producer();		/* complete raidz_supp_impl[] init */
454 	raidz_supp_impl_cnt = c;	/* number of supported impl */
455 
456 #if defined(_KERNEL)
457 	abd_t *pabd;
458 	zio_t *bench_zio = NULL;
459 	raidz_map_t *bench_rm = NULL;
460 	uint64_t bench_parity;
461 
462 	/* Fake a zio and run the benchmark on a warmed up buffer */
463 	bench_zio = kmem_zalloc(sizeof (zio_t), KM_SLEEP);
464 	bench_zio->io_offset = 0;
465 	bench_zio->io_size = BENCH_ZIO_SIZE; /* only data columns */
466 	bench_zio->io_abd = abd_alloc_linear(BENCH_ZIO_SIZE, B_TRUE);
467 	memset(abd_to_buf(bench_zio->io_abd), 0xAA, BENCH_ZIO_SIZE);
468 
469 	/* Benchmark parity generation methods */
470 	for (int fn = 0; fn < RAIDZ_GEN_NUM; fn++) {
471 		bench_parity = fn + 1;
472 		/* New raidz_map is needed for each generate_p/q/r */
473 		bench_rm = vdev_raidz_map_alloc(bench_zio, SPA_MINBLOCKSHIFT,
474 		    BENCH_D_COLS + bench_parity, bench_parity);
475 
476 		benchmark_raidz_impl(bench_rm, fn, benchmark_gen_impl);
477 
478 		vdev_raidz_map_free(bench_rm);
479 	}
480 
481 	/* Benchmark data reconstruction methods */
482 	bench_rm = vdev_raidz_map_alloc(bench_zio, SPA_MINBLOCKSHIFT,
483 	    BENCH_COLS, PARITY_PQR);
484 
485 	/* Ensure that fake parity blocks are initialized */
486 	for (c = 0; c < bench_rm->rm_row[0]->rr_firstdatacol; c++) {
487 		pabd = bench_rm->rm_row[0]->rr_col[c].rc_abd;
488 		memset(abd_to_buf(pabd), 0xAA, abd_get_size(pabd));
489 	}
490 
491 	for (int fn = 0; fn < RAIDZ_REC_NUM; fn++)
492 		benchmark_raidz_impl(bench_rm, fn, benchmark_rec_impl);
493 
494 	vdev_raidz_map_free(bench_rm);
495 
496 	/* cleanup the bench zio */
497 	abd_free(bench_zio->io_abd);
498 	kmem_free(bench_zio, sizeof (zio_t));
499 #else
500 	/*
501 	 * Skip the benchmark in user space to avoid impacting libzpool
502 	 * consumers (zdb, zhack, zinject, ztest).  The last implementation
503 	 * is assumed to be the fastest and used by default.
504 	 */
505 	memcpy(&vdev_raidz_fastest_impl,
506 	    raidz_supp_impl[raidz_supp_impl_cnt - 1],
507 	    sizeof (vdev_raidz_fastest_impl));
508 	strcpy(vdev_raidz_fastest_impl.name, "fastest");
509 #endif /* _KERNEL */
510 }
511 
512 void
vdev_raidz_math_init(void)513 vdev_raidz_math_init(void)
514 {
515 	/* Determine the fastest available implementation. */
516 	benchmark_raidz();
517 
518 #if defined(_KERNEL)
519 	/* Install kstats for all implementations */
520 	raidz_math_kstat = kstat_create("zfs", 0, "vdev_raidz_bench", "misc",
521 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
522 	if (raidz_math_kstat != NULL) {
523 		raidz_math_kstat->ks_data = NULL;
524 		raidz_math_kstat->ks_ndata = UINT32_MAX;
525 		kstat_set_raw_ops(raidz_math_kstat,
526 		    raidz_math_kstat_headers,
527 		    raidz_math_kstat_data,
528 		    raidz_math_kstat_addr);
529 		kstat_install(raidz_math_kstat);
530 	}
531 #endif
532 
533 	/* Finish initialization */
534 	atomic_swap_32(&zfs_vdev_raidz_impl, user_sel_impl);
535 	raidz_math_initialized = B_TRUE;
536 }
537 
538 void
vdev_raidz_math_fini(void)539 vdev_raidz_math_fini(void)
540 {
541 	raidz_impl_ops_t const *curr_impl;
542 
543 #if defined(_KERNEL)
544 	if (raidz_math_kstat != NULL) {
545 		kstat_delete(raidz_math_kstat);
546 		raidz_math_kstat = NULL;
547 	}
548 #endif
549 
550 	for (int i = 0; i < ARRAY_SIZE(raidz_all_maths); i++) {
551 		curr_impl = raidz_all_maths[i];
552 		if (curr_impl->fini)
553 			curr_impl->fini();
554 	}
555 }
556 
557 static const struct {
558 	const char *name;
559 	uint32_t sel;
560 } math_impl_opts[] = {
561 		{ "cycle",	IMPL_CYCLE },
562 		{ "fastest",	IMPL_FASTEST },
563 		{ "original",	IMPL_ORIGINAL },
564 		{ "scalar",	IMPL_SCALAR }
565 };
566 
567 /*
568  * Function sets desired raidz implementation.
569  *
570  * If we are called before init(), user preference will be saved in
571  * user_sel_impl, and applied in later init() call. This occurs when module
572  * parameter is specified on module load. Otherwise, directly update
573  * zfs_vdev_raidz_impl.
574  *
575  * @val		Name of raidz implementation to use
576  * @param	Unused.
577  */
578 int
vdev_raidz_impl_set(const char * val)579 vdev_raidz_impl_set(const char *val)
580 {
581 	int err = -EINVAL;
582 	char req_name[RAIDZ_IMPL_NAME_MAX];
583 	uint32_t impl = RAIDZ_IMPL_READ(user_sel_impl);
584 	size_t i;
585 
586 	/* sanitize input */
587 	i = strnlen(val, RAIDZ_IMPL_NAME_MAX);
588 	if (i == 0 || i == RAIDZ_IMPL_NAME_MAX)
589 		return (err);
590 
591 	strlcpy(req_name, val, RAIDZ_IMPL_NAME_MAX);
592 	while (i > 0 && !!isspace(req_name[i-1]))
593 		i--;
594 	req_name[i] = '\0';
595 
596 	/* Check mandatory options */
597 	for (i = 0; i < ARRAY_SIZE(math_impl_opts); i++) {
598 		if (strcmp(req_name, math_impl_opts[i].name) == 0) {
599 			impl = math_impl_opts[i].sel;
600 			err = 0;
601 			break;
602 		}
603 	}
604 
605 	/* check all supported impl if init() was already called */
606 	if (err != 0 && raidz_math_initialized) {
607 		/* check all supported implementations */
608 		for (i = 0; i < raidz_supp_impl_cnt; i++) {
609 			if (strcmp(req_name, raidz_supp_impl[i]->name) == 0) {
610 				impl = i;
611 				err = 0;
612 				break;
613 			}
614 		}
615 	}
616 
617 	if (err == 0) {
618 		if (raidz_math_initialized)
619 			atomic_swap_32(&zfs_vdev_raidz_impl, impl);
620 		else
621 			atomic_swap_32(&user_sel_impl, impl);
622 	}
623 
624 	return (err);
625 }
626 
627 #if defined(_KERNEL)
628 
629 int
vdev_raidz_impl_get(char * buffer,size_t size)630 vdev_raidz_impl_get(char *buffer, size_t size)
631 {
632 	int i, cnt = 0;
633 	char *fmt;
634 	const uint32_t impl = RAIDZ_IMPL_READ(zfs_vdev_raidz_impl);
635 
636 	/* list mandatory options */
637 	for (i = 0; i < ARRAY_SIZE(math_impl_opts) - 2; i++) {
638 		fmt = (impl == math_impl_opts[i].sel) ? "[%s] " : "%s ";
639 		cnt += kmem_scnprintf(buffer + cnt, size - cnt, fmt,
640 		    math_impl_opts[i].name);
641 	}
642 
643 	/* list all supported implementations */
644 	for (i = 0; i < raidz_supp_impl_cnt; i++) {
645 		fmt = (i == impl) ? "[%s] " : "%s ";
646 		cnt += kmem_scnprintf(buffer + cnt, size - cnt, fmt,
647 		    raidz_supp_impl[i]->name);
648 	}
649 
650 	return (cnt);
651 }
652 
653 #endif
654