xref: /freebsd/sys/contrib/openzfs/cmd/raidz_test/raidz_test.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/time.h>
28 #include <sys/wait.h>
29 #include <sys/zio.h>
30 #include <umem.h>
31 #include <sys/vdev_raidz.h>
32 #include <sys/vdev_raidz_impl.h>
33 #include <assert.h>
34 #include <stdio.h>
35 #include "raidz_test.h"
36 
37 static int *rand_data;
38 raidz_test_opts_t rto_opts;
39 
40 static char gdb[256];
41 static const char gdb_tmpl[] = "gdb -ex \"set pagination 0\" -p %d";
42 
43 static void sig_handler(int signo)
44 {
45 	struct sigaction action;
46 	/*
47 	 * Restore default action and re-raise signal so SIGSEGV and
48 	 * SIGABRT can trigger a core dump.
49 	 */
50 	action.sa_handler = SIG_DFL;
51 	sigemptyset(&action.sa_mask);
52 	action.sa_flags = 0;
53 	(void) sigaction(signo, &action, NULL);
54 
55 	if (rto_opts.rto_gdb)
56 		if (system(gdb)) { }
57 
58 	raise(signo);
59 }
60 
61 static void print_opts(raidz_test_opts_t *opts, boolean_t force)
62 {
63 	char *verbose;
64 	switch (opts->rto_v) {
65 		case 0:
66 			verbose = "no";
67 			break;
68 		case 1:
69 			verbose = "info";
70 			break;
71 		default:
72 			verbose = "debug";
73 			break;
74 	}
75 
76 	if (force || opts->rto_v >= D_INFO) {
77 		(void) fprintf(stdout, DBLSEP "Running with options:\n"
78 		    "  (-a) zio ashift                   : %zu\n"
79 		    "  (-o) zio offset                   : 1 << %zu\n"
80 		    "  (-e) expanded map                 : %s\n"
81 		    "  (-r) reflow offset                : %llx\n"
82 		    "  (-d) number of raidz data columns : %zu\n"
83 		    "  (-s) size of DATA                 : 1 << %zu\n"
84 		    "  (-S) sweep parameters             : %s \n"
85 		    "  (-v) verbose                      : %s \n\n",
86 		    opts->rto_ashift,				/* -a */
87 		    ilog2(opts->rto_offset),			/* -o */
88 		    opts->rto_expand ? "yes" : "no",		/* -e */
89 		    (u_longlong_t)opts->rto_expand_offset,	/* -r */
90 		    opts->rto_dcols,				/* -d */
91 		    ilog2(opts->rto_dsize),			/* -s */
92 		    opts->rto_sweep ? "yes" : "no",		/* -S */
93 		    verbose);					/* -v */
94 	}
95 }
96 
97 static void usage(boolean_t requested)
98 {
99 	const raidz_test_opts_t *o = &rto_opts_defaults;
100 
101 	FILE *fp = requested ? stdout : stderr;
102 
103 	(void) fprintf(fp, "Usage:\n"
104 	    "\t[-a zio ashift (default: %zu)]\n"
105 	    "\t[-o zio offset, exponent radix 2 (default: %zu)]\n"
106 	    "\t[-d number of raidz data columns (default: %zu)]\n"
107 	    "\t[-s zio size, exponent radix 2 (default: %zu)]\n"
108 	    "\t[-S parameter sweep (default: %s)]\n"
109 	    "\t[-t timeout for parameter sweep test]\n"
110 	    "\t[-B benchmark all raidz implementations]\n"
111 	    "\t[-e use expanded raidz map (default: %s)]\n"
112 	    "\t[-r expanded raidz map reflow offset (default: %llx)]\n"
113 	    "\t[-v increase verbosity (default: %zu)]\n"
114 	    "\t[-h (print help)]\n"
115 	    "\t[-T test the test, see if failure would be detected]\n"
116 	    "\t[-D debug (attach gdb on SIGSEGV)]\n"
117 	    "",
118 	    o->rto_ashift,				/* -a */
119 	    ilog2(o->rto_offset),			/* -o */
120 	    o->rto_dcols,				/* -d */
121 	    ilog2(o->rto_dsize),			/* -s */
122 	    rto_opts.rto_sweep ? "yes" : "no",		/* -S */
123 	    rto_opts.rto_expand ? "yes" : "no",		/* -e */
124 	    (u_longlong_t)o->rto_expand_offset,		/* -r */
125 	    o->rto_v);					/* -d */
126 
127 	exit(requested ? 0 : 1);
128 }
129 
130 static void process_options(int argc, char **argv)
131 {
132 	size_t value;
133 	int opt;
134 
135 	raidz_test_opts_t *o = &rto_opts;
136 
137 	bcopy(&rto_opts_defaults, o, sizeof (*o));
138 
139 	while ((opt = getopt(argc, argv, "TDBSvha:er:o:d:s:t:")) != -1) {
140 		value = 0;
141 
142 		switch (opt) {
143 		case 'a':
144 			value = strtoull(optarg, NULL, 0);
145 			o->rto_ashift = MIN(13, MAX(9, value));
146 			break;
147 		case 'e':
148 			o->rto_expand = 1;
149 			break;
150 		case 'r':
151 			o->rto_expand_offset = strtoull(optarg, NULL, 0);
152 			break;
153 		case 'o':
154 			value = strtoull(optarg, NULL, 0);
155 			o->rto_offset = ((1ULL << MIN(12, value)) >> 9) << 9;
156 			break;
157 		case 'd':
158 			value = strtoull(optarg, NULL, 0);
159 			o->rto_dcols = MIN(255, MAX(1, value));
160 			break;
161 		case 's':
162 			value = strtoull(optarg, NULL, 0);
163 			o->rto_dsize = 1ULL <<  MIN(SPA_MAXBLOCKSHIFT,
164 			    MAX(SPA_MINBLOCKSHIFT, value));
165 			break;
166 		case 't':
167 			value = strtoull(optarg, NULL, 0);
168 			o->rto_sweep_timeout = value;
169 			break;
170 		case 'v':
171 			o->rto_v++;
172 			break;
173 		case 'S':
174 			o->rto_sweep = 1;
175 			break;
176 		case 'B':
177 			o->rto_benchmark = 1;
178 			break;
179 		case 'D':
180 			o->rto_gdb = 1;
181 			break;
182 		case 'T':
183 			o->rto_sanity = 1;
184 			break;
185 		case 'h':
186 			usage(B_TRUE);
187 			break;
188 		case '?':
189 		default:
190 			usage(B_FALSE);
191 			break;
192 		}
193 	}
194 }
195 
196 #define	DATA_COL(rr, i) ((rr)->rr_col[rr->rr_firstdatacol + (i)].rc_abd)
197 #define	DATA_COL_SIZE(rr, i) ((rr)->rr_col[rr->rr_firstdatacol + (i)].rc_size)
198 
199 #define	CODE_COL(rr, i) ((rr)->rr_col[(i)].rc_abd)
200 #define	CODE_COL_SIZE(rr, i) ((rr)->rr_col[(i)].rc_size)
201 
202 static int
203 cmp_code(raidz_test_opts_t *opts, const raidz_map_t *rm, const int parity)
204 {
205 	int r, i, ret = 0;
206 
207 	VERIFY(parity >= 1 && parity <= 3);
208 
209 	for (r = 0; r < rm->rm_nrows; r++) {
210 		raidz_row_t * const rr = rm->rm_row[r];
211 		raidz_row_t * const rrg = opts->rm_golden->rm_row[r];
212 		for (i = 0; i < parity; i++) {
213 			if (CODE_COL_SIZE(rrg, i) == 0) {
214 				VERIFY0(CODE_COL_SIZE(rr, i));
215 				continue;
216 			}
217 
218 			if (abd_cmp(CODE_COL(rr, i),
219 			    CODE_COL(rrg, i)) != 0) {
220 				ret++;
221 				LOG_OPT(D_DEBUG, opts,
222 				    "\nParity block [%d] different!\n", i);
223 			}
224 		}
225 	}
226 	return (ret);
227 }
228 
229 static int
230 cmp_data(raidz_test_opts_t *opts, raidz_map_t *rm)
231 {
232 	int r, i, dcols, ret = 0;
233 
234 	for (r = 0; r < rm->rm_nrows; r++) {
235 		raidz_row_t *rr = rm->rm_row[r];
236 		raidz_row_t *rrg = opts->rm_golden->rm_row[r];
237 		dcols = opts->rm_golden->rm_row[0]->rr_cols -
238 		    raidz_parity(opts->rm_golden);
239 		for (i = 0; i < dcols; i++) {
240 			if (DATA_COL_SIZE(rrg, i) == 0) {
241 				VERIFY0(DATA_COL_SIZE(rr, i));
242 				continue;
243 			}
244 
245 			if (abd_cmp(DATA_COL(rrg, i),
246 			    DATA_COL(rr, i)) != 0) {
247 				ret++;
248 
249 				LOG_OPT(D_DEBUG, opts,
250 				    "\nData block [%d] different!\n", i);
251 			}
252 		}
253 	}
254 	return (ret);
255 }
256 
257 static int
258 init_rand(void *data, size_t size, void *private)
259 {
260 	int i;
261 	int *dst = (int *)data;
262 
263 	for (i = 0; i < size / sizeof (int); i++)
264 		dst[i] = rand_data[i];
265 
266 	return (0);
267 }
268 
269 static void
270 corrupt_colums(raidz_map_t *rm, const int *tgts, const int cnt)
271 {
272 	for (int r = 0; r < rm->rm_nrows; r++) {
273 		raidz_row_t *rr = rm->rm_row[r];
274 		for (int i = 0; i < cnt; i++) {
275 			raidz_col_t *col = &rr->rr_col[tgts[i]];
276 			abd_iterate_func(col->rc_abd, 0, col->rc_size,
277 			    init_rand, NULL);
278 		}
279 	}
280 }
281 
282 void
283 init_zio_abd(zio_t *zio)
284 {
285 	abd_iterate_func(zio->io_abd, 0, zio->io_size, init_rand, NULL);
286 }
287 
288 static void
289 fini_raidz_map(zio_t **zio, raidz_map_t **rm)
290 {
291 	vdev_raidz_map_free(*rm);
292 	raidz_free((*zio)->io_abd, (*zio)->io_size);
293 	umem_free(*zio, sizeof (zio_t));
294 
295 	*zio = NULL;
296 	*rm = NULL;
297 }
298 
299 static int
300 init_raidz_golden_map(raidz_test_opts_t *opts, const int parity)
301 {
302 	int err = 0;
303 	zio_t *zio_test;
304 	raidz_map_t *rm_test;
305 	const size_t total_ncols = opts->rto_dcols + parity;
306 
307 	if (opts->rm_golden) {
308 		fini_raidz_map(&opts->zio_golden, &opts->rm_golden);
309 	}
310 
311 	opts->zio_golden = umem_zalloc(sizeof (zio_t), UMEM_NOFAIL);
312 	zio_test = umem_zalloc(sizeof (zio_t), UMEM_NOFAIL);
313 
314 	opts->zio_golden->io_offset = zio_test->io_offset = opts->rto_offset;
315 	opts->zio_golden->io_size = zio_test->io_size = opts->rto_dsize;
316 
317 	opts->zio_golden->io_abd = raidz_alloc(opts->rto_dsize);
318 	zio_test->io_abd = raidz_alloc(opts->rto_dsize);
319 
320 	init_zio_abd(opts->zio_golden);
321 	init_zio_abd(zio_test);
322 
323 	VERIFY0(vdev_raidz_impl_set("original"));
324 
325 	if (opts->rto_expand) {
326 		opts->rm_golden =
327 		    vdev_raidz_map_alloc_expanded(opts->zio_golden->io_abd,
328 		    opts->zio_golden->io_size, opts->zio_golden->io_offset,
329 		    opts->rto_ashift, total_ncols+1, total_ncols,
330 		    parity, opts->rto_expand_offset);
331 		rm_test = vdev_raidz_map_alloc_expanded(zio_test->io_abd,
332 		    zio_test->io_size, zio_test->io_offset,
333 		    opts->rto_ashift, total_ncols+1, total_ncols,
334 		    parity, opts->rto_expand_offset);
335 	} else {
336 		opts->rm_golden = vdev_raidz_map_alloc(opts->zio_golden,
337 		    opts->rto_ashift, total_ncols, parity);
338 		rm_test = vdev_raidz_map_alloc(zio_test,
339 		    opts->rto_ashift, total_ncols, parity);
340 	}
341 
342 	VERIFY(opts->zio_golden);
343 	VERIFY(opts->rm_golden);
344 
345 	vdev_raidz_generate_parity(opts->rm_golden);
346 	vdev_raidz_generate_parity(rm_test);
347 
348 	/* sanity check */
349 	err |= cmp_data(opts, rm_test);
350 	err |= cmp_code(opts, rm_test, parity);
351 
352 	if (err)
353 		ERR("initializing the golden copy ... [FAIL]!\n");
354 
355 	/* tear down raidz_map of test zio */
356 	fini_raidz_map(&zio_test, &rm_test);
357 
358 	return (err);
359 }
360 
361 /*
362  * If reflow is not in progress, reflow_offset should be UINT64_MAX.
363  * For each row, if the row is entirely before reflow_offset, it will
364  * come from the new location.  Otherwise this row will come from the
365  * old location.  Therefore, rows that straddle the reflow_offset will
366  * come from the old location.
367  *
368  * NOTE: Until raidz expansion is implemented this function is only
369  * needed by raidz_test.c to the multi-row raid_map_t functionality.
370  */
371 raidz_map_t *
372 vdev_raidz_map_alloc_expanded(abd_t *abd, uint64_t size, uint64_t offset,
373     uint64_t ashift, uint64_t physical_cols, uint64_t logical_cols,
374     uint64_t nparity, uint64_t reflow_offset)
375 {
376 	/* The zio's size in units of the vdev's minimum sector size. */
377 	uint64_t s = size >> ashift;
378 	uint64_t q, r, bc, devidx, asize = 0, tot;
379 
380 	/*
381 	 * "Quotient": The number of data sectors for this stripe on all but
382 	 * the "big column" child vdevs that also contain "remainder" data.
383 	 * AKA "full rows"
384 	 */
385 	q = s / (logical_cols - nparity);
386 
387 	/*
388 	 * "Remainder": The number of partial stripe data sectors in this I/O.
389 	 * This will add a sector to some, but not all, child vdevs.
390 	 */
391 	r = s - q * (logical_cols - nparity);
392 
393 	/* The number of "big columns" - those which contain remainder data. */
394 	bc = (r == 0 ? 0 : r + nparity);
395 
396 	/*
397 	 * The total number of data and parity sectors associated with
398 	 * this I/O.
399 	 */
400 	tot = s + nparity * (q + (r == 0 ? 0 : 1));
401 
402 	/* How many rows contain data (not skip) */
403 	uint64_t rows = howmany(tot, logical_cols);
404 	int cols = MIN(tot, logical_cols);
405 
406 	raidz_map_t *rm = kmem_zalloc(offsetof(raidz_map_t, rm_row[rows]),
407 	    KM_SLEEP);
408 	rm->rm_nrows = rows;
409 
410 	for (uint64_t row = 0; row < rows; row++) {
411 		raidz_row_t *rr = kmem_alloc(offsetof(raidz_row_t,
412 		    rr_col[cols]), KM_SLEEP);
413 		rm->rm_row[row] = rr;
414 
415 		/* The starting RAIDZ (parent) vdev sector of the row. */
416 		uint64_t b = (offset >> ashift) + row * logical_cols;
417 
418 		/*
419 		 * If we are in the middle of a reflow, and any part of this
420 		 * row has not been copied, then use the old location of
421 		 * this row.
422 		 */
423 		int row_phys_cols = physical_cols;
424 		if (b + (logical_cols - nparity) > reflow_offset >> ashift)
425 			row_phys_cols--;
426 
427 		/* starting child of this row */
428 		uint64_t child_id = b % row_phys_cols;
429 		/* The starting byte offset on each child vdev. */
430 		uint64_t child_offset = (b / row_phys_cols) << ashift;
431 
432 		/*
433 		 * We set cols to the entire width of the block, even
434 		 * if this row is shorter.  This is needed because parity
435 		 * generation (for Q and R) needs to know the entire width,
436 		 * because it treats the short row as though it was
437 		 * full-width (and the "phantom" sectors were zero-filled).
438 		 *
439 		 * Another approach to this would be to set cols shorter
440 		 * (to just the number of columns that we might do i/o to)
441 		 * and have another mechanism to tell the parity generation
442 		 * about the "entire width".  Reconstruction (at least
443 		 * vdev_raidz_reconstruct_general()) would also need to
444 		 * know about the "entire width".
445 		 */
446 		rr->rr_cols = cols;
447 		rr->rr_bigcols = bc;
448 		rr->rr_missingdata = 0;
449 		rr->rr_missingparity = 0;
450 		rr->rr_firstdatacol = nparity;
451 		rr->rr_abd_empty = NULL;
452 		rr->rr_nempty = 0;
453 
454 		for (int c = 0; c < rr->rr_cols; c++, child_id++) {
455 			if (child_id >= row_phys_cols) {
456 				child_id -= row_phys_cols;
457 				child_offset += 1ULL << ashift;
458 			}
459 			rr->rr_col[c].rc_devidx = child_id;
460 			rr->rr_col[c].rc_offset = child_offset;
461 			rr->rr_col[c].rc_orig_data = NULL;
462 			rr->rr_col[c].rc_error = 0;
463 			rr->rr_col[c].rc_tried = 0;
464 			rr->rr_col[c].rc_skipped = 0;
465 			rr->rr_col[c].rc_need_orig_restore = B_FALSE;
466 
467 			uint64_t dc = c - rr->rr_firstdatacol;
468 			if (c < rr->rr_firstdatacol) {
469 				rr->rr_col[c].rc_size = 1ULL << ashift;
470 				rr->rr_col[c].rc_abd =
471 				    abd_alloc_linear(rr->rr_col[c].rc_size,
472 				    B_TRUE);
473 			} else if (row == rows - 1 && bc != 0 && c >= bc) {
474 				/*
475 				 * Past the end, this for parity generation.
476 				 */
477 				rr->rr_col[c].rc_size = 0;
478 				rr->rr_col[c].rc_abd = NULL;
479 			} else {
480 				/*
481 				 * "data column" (col excluding parity)
482 				 * Add an ASCII art diagram here
483 				 */
484 				uint64_t off;
485 
486 				if (c < bc || r == 0) {
487 					off = dc * rows + row;
488 				} else {
489 					off = r * rows +
490 					    (dc - r) * (rows - 1) + row;
491 				}
492 				rr->rr_col[c].rc_size = 1ULL << ashift;
493 				rr->rr_col[c].rc_abd = abd_get_offset_struct(
494 				    &rr->rr_col[c].rc_abdstruct,
495 				    abd, off << ashift, 1 << ashift);
496 			}
497 
498 			asize += rr->rr_col[c].rc_size;
499 		}
500 		/*
501 		 * If all data stored spans all columns, there's a danger that
502 		 * parity will always be on the same device and, since parity
503 		 * isn't read during normal operation, that that device's I/O
504 		 * bandwidth won't be used effectively. We therefore switch
505 		 * the parity every 1MB.
506 		 *
507 		 * ...at least that was, ostensibly, the theory. As a practical
508 		 * matter unless we juggle the parity between all devices
509 		 * evenly, we won't see any benefit. Further, occasional writes
510 		 * that aren't a multiple of the LCM of the number of children
511 		 * and the minimum stripe width are sufficient to avoid pessimal
512 		 * behavior. Unfortunately, this decision created an implicit
513 		 * on-disk format requirement that we need to support for all
514 		 * eternity, but only for single-parity RAID-Z.
515 		 *
516 		 * If we intend to skip a sector in the zeroth column for
517 		 * padding we must make sure to note this swap. We will never
518 		 * intend to skip the first column since at least one data and
519 		 * one parity column must appear in each row.
520 		 */
521 		if (rr->rr_firstdatacol == 1 && rr->rr_cols > 1 &&
522 		    (offset & (1ULL << 20))) {
523 			ASSERT(rr->rr_cols >= 2);
524 			ASSERT(rr->rr_col[0].rc_size == rr->rr_col[1].rc_size);
525 			devidx = rr->rr_col[0].rc_devidx;
526 			uint64_t o = rr->rr_col[0].rc_offset;
527 			rr->rr_col[0].rc_devidx = rr->rr_col[1].rc_devidx;
528 			rr->rr_col[0].rc_offset = rr->rr_col[1].rc_offset;
529 			rr->rr_col[1].rc_devidx = devidx;
530 			rr->rr_col[1].rc_offset = o;
531 		}
532 
533 	}
534 	ASSERT3U(asize, ==, tot << ashift);
535 
536 	/* init RAIDZ parity ops */
537 	rm->rm_ops = vdev_raidz_math_get_ops();
538 
539 	return (rm);
540 }
541 
542 static raidz_map_t *
543 init_raidz_map(raidz_test_opts_t *opts, zio_t **zio, const int parity)
544 {
545 	raidz_map_t *rm = NULL;
546 	const size_t alloc_dsize = opts->rto_dsize;
547 	const size_t total_ncols = opts->rto_dcols + parity;
548 	const int ccols[] = { 0, 1, 2 };
549 
550 	VERIFY(zio);
551 	VERIFY(parity <= 3 && parity >= 1);
552 
553 	*zio = umem_zalloc(sizeof (zio_t), UMEM_NOFAIL);
554 
555 	(*zio)->io_offset = 0;
556 	(*zio)->io_size = alloc_dsize;
557 	(*zio)->io_abd = raidz_alloc(alloc_dsize);
558 	init_zio_abd(*zio);
559 
560 	if (opts->rto_expand) {
561 		rm = vdev_raidz_map_alloc_expanded((*zio)->io_abd,
562 		    (*zio)->io_size, (*zio)->io_offset,
563 		    opts->rto_ashift, total_ncols+1, total_ncols,
564 		    parity, opts->rto_expand_offset);
565 	} else {
566 		rm = vdev_raidz_map_alloc(*zio, opts->rto_ashift,
567 		    total_ncols, parity);
568 	}
569 	VERIFY(rm);
570 
571 	/* Make sure code columns are destroyed */
572 	corrupt_colums(rm, ccols, parity);
573 
574 	return (rm);
575 }
576 
577 static int
578 run_gen_check(raidz_test_opts_t *opts)
579 {
580 	char **impl_name;
581 	int fn, err = 0;
582 	zio_t *zio_test;
583 	raidz_map_t *rm_test;
584 
585 	err = init_raidz_golden_map(opts, PARITY_PQR);
586 	if (0 != err)
587 		return (err);
588 
589 	LOG(D_INFO, DBLSEP);
590 	LOG(D_INFO, "Testing parity generation...\n");
591 
592 	for (impl_name = (char **)raidz_impl_names+1; *impl_name != NULL;
593 	    impl_name++) {
594 
595 		LOG(D_INFO, SEP);
596 		LOG(D_INFO, "\tTesting [%s] implementation...", *impl_name);
597 
598 		if (0 != vdev_raidz_impl_set(*impl_name)) {
599 			LOG(D_INFO, "[SKIP]\n");
600 			continue;
601 		} else {
602 			LOG(D_INFO, "[SUPPORTED]\n");
603 		}
604 
605 		for (fn = 0; fn < RAIDZ_GEN_NUM; fn++) {
606 
607 			/* Check if should stop */
608 			if (rto_opts.rto_should_stop)
609 				return (err);
610 
611 			/* create suitable raidz_map */
612 			rm_test = init_raidz_map(opts, &zio_test, fn+1);
613 			VERIFY(rm_test);
614 
615 			LOG(D_INFO, "\t\tTesting method [%s] ...",
616 			    raidz_gen_name[fn]);
617 
618 			if (!opts->rto_sanity)
619 				vdev_raidz_generate_parity(rm_test);
620 
621 			if (cmp_code(opts, rm_test, fn+1) != 0) {
622 				LOG(D_INFO, "[FAIL]\n");
623 				err++;
624 			} else
625 				LOG(D_INFO, "[PASS]\n");
626 
627 			fini_raidz_map(&zio_test, &rm_test);
628 		}
629 	}
630 
631 	fini_raidz_map(&opts->zio_golden, &opts->rm_golden);
632 
633 	return (err);
634 }
635 
636 static int
637 run_rec_check_impl(raidz_test_opts_t *opts, raidz_map_t *rm, const int fn)
638 {
639 	int x0, x1, x2;
640 	int tgtidx[3];
641 	int err = 0;
642 	static const int rec_tgts[7][3] = {
643 		{1, 2, 3},	/* rec_p:   bad QR & D[0]	*/
644 		{0, 2, 3},	/* rec_q:   bad PR & D[0]	*/
645 		{0, 1, 3},	/* rec_r:   bad PQ & D[0]	*/
646 		{2, 3, 4},	/* rec_pq:  bad R  & D[0][1]	*/
647 		{1, 3, 4},	/* rec_pr:  bad Q  & D[0][1]	*/
648 		{0, 3, 4},	/* rec_qr:  bad P  & D[0][1]	*/
649 		{3, 4, 5}	/* rec_pqr: bad    & D[0][1][2] */
650 	};
651 
652 	memcpy(tgtidx, rec_tgts[fn], sizeof (tgtidx));
653 
654 	if (fn < RAIDZ_REC_PQ) {
655 		/* can reconstruct 1 failed data disk */
656 		for (x0 = 0; x0 < opts->rto_dcols; x0++) {
657 			if (x0 >= rm->rm_row[0]->rr_cols - raidz_parity(rm))
658 				continue;
659 
660 			/* Check if should stop */
661 			if (rto_opts.rto_should_stop)
662 				return (err);
663 
664 			LOG(D_DEBUG, "[%d] ", x0);
665 
666 			tgtidx[2] = x0 + raidz_parity(rm);
667 
668 			corrupt_colums(rm, tgtidx+2, 1);
669 
670 			if (!opts->rto_sanity)
671 				vdev_raidz_reconstruct(rm, tgtidx, 3);
672 
673 			if (cmp_data(opts, rm) != 0) {
674 				err++;
675 				LOG(D_DEBUG, "\nREC D[%d]... [FAIL]\n", x0);
676 			}
677 		}
678 
679 	} else if (fn < RAIDZ_REC_PQR) {
680 		/* can reconstruct 2 failed data disk */
681 		for (x0 = 0; x0 < opts->rto_dcols; x0++) {
682 			if (x0 >= rm->rm_row[0]->rr_cols - raidz_parity(rm))
683 				continue;
684 			for (x1 = x0 + 1; x1 < opts->rto_dcols; x1++) {
685 				if (x1 >= rm->rm_row[0]->rr_cols -
686 				    raidz_parity(rm))
687 					continue;
688 
689 				/* Check if should stop */
690 				if (rto_opts.rto_should_stop)
691 					return (err);
692 
693 				LOG(D_DEBUG, "[%d %d] ", x0, x1);
694 
695 				tgtidx[1] = x0 + raidz_parity(rm);
696 				tgtidx[2] = x1 + raidz_parity(rm);
697 
698 				corrupt_colums(rm, tgtidx+1, 2);
699 
700 				if (!opts->rto_sanity)
701 					vdev_raidz_reconstruct(rm, tgtidx, 3);
702 
703 				if (cmp_data(opts, rm) != 0) {
704 					err++;
705 					LOG(D_DEBUG, "\nREC D[%d %d]... "
706 					    "[FAIL]\n", x0, x1);
707 				}
708 			}
709 		}
710 	} else {
711 		/* can reconstruct 3 failed data disk */
712 		for (x0 = 0; x0 < opts->rto_dcols; x0++) {
713 			if (x0 >= rm->rm_row[0]->rr_cols - raidz_parity(rm))
714 				continue;
715 			for (x1 = x0 + 1; x1 < opts->rto_dcols; x1++) {
716 				if (x1 >= rm->rm_row[0]->rr_cols -
717 				    raidz_parity(rm))
718 					continue;
719 				for (x2 = x1 + 1; x2 < opts->rto_dcols; x2++) {
720 					if (x2 >= rm->rm_row[0]->rr_cols -
721 					    raidz_parity(rm))
722 						continue;
723 
724 					/* Check if should stop */
725 					if (rto_opts.rto_should_stop)
726 						return (err);
727 
728 					LOG(D_DEBUG, "[%d %d %d]", x0, x1, x2);
729 
730 					tgtidx[0] = x0 + raidz_parity(rm);
731 					tgtidx[1] = x1 + raidz_parity(rm);
732 					tgtidx[2] = x2 + raidz_parity(rm);
733 
734 					corrupt_colums(rm, tgtidx, 3);
735 
736 					if (!opts->rto_sanity)
737 						vdev_raidz_reconstruct(rm,
738 						    tgtidx, 3);
739 
740 					if (cmp_data(opts, rm) != 0) {
741 						err++;
742 						LOG(D_DEBUG,
743 						    "\nREC D[%d %d %d]... "
744 						    "[FAIL]\n", x0, x1, x2);
745 					}
746 				}
747 			}
748 		}
749 	}
750 	return (err);
751 }
752 
753 static int
754 run_rec_check(raidz_test_opts_t *opts)
755 {
756 	char **impl_name;
757 	unsigned fn, err = 0;
758 	zio_t *zio_test;
759 	raidz_map_t *rm_test;
760 
761 	err = init_raidz_golden_map(opts, PARITY_PQR);
762 	if (0 != err)
763 		return (err);
764 
765 	LOG(D_INFO, DBLSEP);
766 	LOG(D_INFO, "Testing data reconstruction...\n");
767 
768 	for (impl_name = (char **)raidz_impl_names+1; *impl_name != NULL;
769 	    impl_name++) {
770 
771 		LOG(D_INFO, SEP);
772 		LOG(D_INFO, "\tTesting [%s] implementation...", *impl_name);
773 
774 		if (vdev_raidz_impl_set(*impl_name) != 0) {
775 			LOG(D_INFO, "[SKIP]\n");
776 			continue;
777 		} else
778 			LOG(D_INFO, "[SUPPORTED]\n");
779 
780 
781 		/* create suitable raidz_map */
782 		rm_test = init_raidz_map(opts, &zio_test, PARITY_PQR);
783 		/* generate parity */
784 		vdev_raidz_generate_parity(rm_test);
785 
786 		for (fn = 0; fn < RAIDZ_REC_NUM; fn++) {
787 
788 			LOG(D_INFO, "\t\tTesting method [%s] ...",
789 			    raidz_rec_name[fn]);
790 
791 			if (run_rec_check_impl(opts, rm_test, fn) != 0) {
792 				LOG(D_INFO, "[FAIL]\n");
793 				err++;
794 
795 			} else
796 				LOG(D_INFO, "[PASS]\n");
797 
798 		}
799 		/* tear down test raidz_map */
800 		fini_raidz_map(&zio_test, &rm_test);
801 	}
802 
803 	fini_raidz_map(&opts->zio_golden, &opts->rm_golden);
804 
805 	return (err);
806 }
807 
808 static int
809 run_test(raidz_test_opts_t *opts)
810 {
811 	int err = 0;
812 
813 	if (opts == NULL)
814 		opts = &rto_opts;
815 
816 	print_opts(opts, B_FALSE);
817 
818 	err |= run_gen_check(opts);
819 	err |= run_rec_check(opts);
820 
821 	return (err);
822 }
823 
824 #define	SWEEP_RUNNING	0
825 #define	SWEEP_FINISHED	1
826 #define	SWEEP_ERROR	2
827 #define	SWEEP_TIMEOUT	3
828 
829 static int sweep_state = 0;
830 static raidz_test_opts_t failed_opts;
831 
832 static kmutex_t sem_mtx;
833 static kcondvar_t sem_cv;
834 static int max_free_slots;
835 static int free_slots;
836 
837 static void
838 sweep_thread(void *arg)
839 {
840 	int err = 0;
841 	raidz_test_opts_t *opts = (raidz_test_opts_t *)arg;
842 	VERIFY(opts != NULL);
843 
844 	err = run_test(opts);
845 
846 	if (rto_opts.rto_sanity) {
847 		/* 25% chance that a sweep test fails */
848 		if (rand() < (RAND_MAX/4))
849 			err = 1;
850 	}
851 
852 	if (0 != err) {
853 		mutex_enter(&sem_mtx);
854 		memcpy(&failed_opts, opts, sizeof (raidz_test_opts_t));
855 		sweep_state = SWEEP_ERROR;
856 		mutex_exit(&sem_mtx);
857 	}
858 
859 	umem_free(opts, sizeof (raidz_test_opts_t));
860 
861 	/* signal the next thread */
862 	mutex_enter(&sem_mtx);
863 	free_slots++;
864 	cv_signal(&sem_cv);
865 	mutex_exit(&sem_mtx);
866 
867 	thread_exit();
868 }
869 
870 static int
871 run_sweep(void)
872 {
873 	static const size_t dcols_v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 15, 16 };
874 	static const size_t ashift_v[] = { 9, 12, 14 };
875 	static const size_t size_v[] = { 1 << 9, 21 * (1 << 9), 13 * (1 << 12),
876 		1 << 17, (1 << 20) - (1 << 12), SPA_MAXBLOCKSIZE };
877 
878 	(void) setvbuf(stdout, NULL, _IONBF, 0);
879 
880 	ulong_t total_comb = ARRAY_SIZE(size_v) * ARRAY_SIZE(ashift_v) *
881 	    ARRAY_SIZE(dcols_v);
882 	ulong_t tried_comb = 0;
883 	hrtime_t time_diff, start_time = gethrtime();
884 	raidz_test_opts_t *opts;
885 	int a, d, s;
886 
887 	max_free_slots = free_slots = MAX(2, boot_ncpus);
888 
889 	mutex_init(&sem_mtx, NULL, MUTEX_DEFAULT, NULL);
890 	cv_init(&sem_cv, NULL, CV_DEFAULT, NULL);
891 
892 	for (s = 0; s < ARRAY_SIZE(size_v); s++)
893 	for (a = 0; a < ARRAY_SIZE(ashift_v); a++)
894 	for (d = 0; d < ARRAY_SIZE(dcols_v); d++) {
895 
896 		if (size_v[s] < (1 << ashift_v[a])) {
897 			total_comb--;
898 			continue;
899 		}
900 
901 		if (++tried_comb % 20 == 0)
902 			LOG(D_ALL, "%lu/%lu... ", tried_comb, total_comb);
903 
904 		/* wait for signal to start new thread */
905 		mutex_enter(&sem_mtx);
906 		while (cv_timedwait_sig(&sem_cv, &sem_mtx,
907 		    ddi_get_lbolt() + hz)) {
908 
909 			/* check if should stop the test (timeout) */
910 			time_diff = (gethrtime() - start_time) / NANOSEC;
911 			if (rto_opts.rto_sweep_timeout > 0 &&
912 			    time_diff >= rto_opts.rto_sweep_timeout) {
913 				sweep_state = SWEEP_TIMEOUT;
914 				rto_opts.rto_should_stop = B_TRUE;
915 				mutex_exit(&sem_mtx);
916 				goto exit;
917 			}
918 
919 			/* check if should stop the test (error) */
920 			if (sweep_state != SWEEP_RUNNING) {
921 				mutex_exit(&sem_mtx);
922 				goto exit;
923 			}
924 
925 			/* exit loop if a slot is available */
926 			if (free_slots > 0) {
927 				break;
928 			}
929 		}
930 
931 		free_slots--;
932 		mutex_exit(&sem_mtx);
933 
934 		opts = umem_zalloc(sizeof (raidz_test_opts_t), UMEM_NOFAIL);
935 		opts->rto_ashift = ashift_v[a];
936 		opts->rto_dcols = dcols_v[d];
937 		opts->rto_offset = (1 << ashift_v[a]) * rand();
938 		opts->rto_dsize = size_v[s];
939 		opts->rto_expand = rto_opts.rto_expand;
940 		opts->rto_expand_offset = rto_opts.rto_expand_offset;
941 		opts->rto_v = 0; /* be quiet */
942 
943 		VERIFY3P(thread_create(NULL, 0, sweep_thread, (void *) opts,
944 		    0, NULL, TS_RUN, defclsyspri), !=, NULL);
945 	}
946 
947 exit:
948 	LOG(D_ALL, "\nWaiting for test threads to finish...\n");
949 	mutex_enter(&sem_mtx);
950 	VERIFY(free_slots <= max_free_slots);
951 	while (free_slots < max_free_slots) {
952 		(void) cv_wait(&sem_cv, &sem_mtx);
953 	}
954 	mutex_exit(&sem_mtx);
955 
956 	if (sweep_state == SWEEP_ERROR) {
957 		ERR("Sweep test failed! Failed option: \n");
958 		print_opts(&failed_opts, B_TRUE);
959 	} else {
960 		if (sweep_state == SWEEP_TIMEOUT)
961 			LOG(D_ALL, "Test timeout (%lus). Stopping...\n",
962 			    (ulong_t)rto_opts.rto_sweep_timeout);
963 
964 		LOG(D_ALL, "Sweep test succeeded on %lu raidz maps!\n",
965 		    (ulong_t)tried_comb);
966 	}
967 
968 	mutex_destroy(&sem_mtx);
969 
970 	return (sweep_state == SWEEP_ERROR ? SWEEP_ERROR : 0);
971 }
972 
973 
974 int
975 main(int argc, char **argv)
976 {
977 	size_t i;
978 	struct sigaction action;
979 	int err = 0;
980 
981 	/* init gdb string early */
982 	(void) sprintf(gdb, gdb_tmpl, getpid());
983 
984 	action.sa_handler = sig_handler;
985 	sigemptyset(&action.sa_mask);
986 	action.sa_flags = 0;
987 
988 	if (sigaction(SIGSEGV, &action, NULL) < 0) {
989 		ERR("raidz_test: cannot catch SIGSEGV: %s.\n", strerror(errno));
990 		exit(EXIT_FAILURE);
991 	}
992 
993 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
994 
995 	dprintf_setup(&argc, argv);
996 
997 	process_options(argc, argv);
998 
999 	kernel_init(SPA_MODE_READ);
1000 
1001 	/* setup random data because rand() is not reentrant */
1002 	rand_data = (int *)umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
1003 	srand((unsigned)time(NULL) * getpid());
1004 	for (i = 0; i < SPA_MAXBLOCKSIZE / sizeof (int); i++)
1005 		rand_data[i] = rand();
1006 
1007 	mprotect(rand_data, SPA_MAXBLOCKSIZE, PROT_READ);
1008 
1009 	if (rto_opts.rto_benchmark) {
1010 		run_raidz_benchmark();
1011 	} else if (rto_opts.rto_sweep) {
1012 		err = run_sweep();
1013 	} else {
1014 		err = run_test(NULL);
1015 	}
1016 
1017 	umem_free(rand_data, SPA_MAXBLOCKSIZE);
1018 	kernel_fini();
1019 
1020 	return (err);
1021 }
1022