xref: /titanic_41/usr/src/cmd/ztest/ztest.c (revision 62dcc6f36fd03042857eae7d26dc2dca0db70d0c)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * The objective of this program is to provide a DMU/ZAP/SPA stress test
28  * that runs entirely in userland, is easy to use, and easy to extend.
29  *
30  * The overall design of the ztest program is as follows:
31  *
32  * (1) For each major functional area (e.g. adding vdevs to a pool,
33  *     creating and destroying datasets, reading and writing objects, etc)
34  *     we have a simple routine to test that functionality.  These
35  *     individual routines do not have to do anything "stressful".
36  *
37  * (2) We turn these simple functionality tests into a stress test by
38  *     running them all in parallel, with as many threads as desired,
39  *     and spread across as many datasets, objects, and vdevs as desired.
40  *
41  * (3) While all this is happening, we inject faults into the pool to
42  *     verify that self-healing data really works.
43  *
44  * (4) Every time we open a dataset, we change its checksum and compression
45  *     functions.  Thus even individual objects vary from block to block
46  *     in which checksum they use and whether they're compressed.
47  *
48  * (5) To verify that we never lose on-disk consistency after a crash,
49  *     we run the entire test in a child of the main process.
50  *     At random times, the child self-immolates with a SIGKILL.
51  *     This is the software equivalent of pulling the power cord.
52  *     The parent then runs the test again, using the existing
53  *     storage pool, as many times as desired.
54  *
55  * (6) To verify that we don't have future leaks or temporal incursions,
56  *     many of the functional tests record the transaction group number
57  *     as part of their data.  When reading old data, they verify that
58  *     the transaction group number is less than the current, open txg.
59  *     If you add a new test, please do this if applicable.
60  *
61  * When run with no arguments, ztest runs for about five minutes and
62  * produces no output if successful.  To get a little bit of information,
63  * specify -V.  To get more information, specify -VV, and so on.
64  *
65  * To turn this into an overnight stress test, use -T to specify run time.
66  *
67  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
68  * to increase the pool capacity, fanout, and overall stress level.
69  *
70  * The -N(okill) option will suppress kills, so each child runs to completion.
71  * This can be useful when you're trying to distinguish temporal incursions
72  * from plain old race conditions.
73  */
74 
75 #include <sys/zfs_context.h>
76 #include <sys/spa.h>
77 #include <sys/dmu.h>
78 #include <sys/txg.h>
79 #include <sys/dbuf.h>
80 #include <sys/zap.h>
81 #include <sys/dmu_objset.h>
82 #include <sys/poll.h>
83 #include <sys/stat.h>
84 #include <sys/time.h>
85 #include <sys/wait.h>
86 #include <sys/mman.h>
87 #include <sys/resource.h>
88 #include <sys/zio.h>
89 #include <sys/zio_checksum.h>
90 #include <sys/zio_compress.h>
91 #include <sys/zil.h>
92 #include <sys/vdev_impl.h>
93 #include <sys/vdev_file.h>
94 #include <sys/spa_impl.h>
95 #include <sys/dsl_prop.h>
96 #include <sys/dsl_dataset.h>
97 #include <sys/refcount.h>
98 #include <stdio.h>
99 #include <stdio_ext.h>
100 #include <stdlib.h>
101 #include <unistd.h>
102 #include <signal.h>
103 #include <umem.h>
104 #include <dlfcn.h>
105 #include <ctype.h>
106 #include <math.h>
107 #include <sys/fs/zfs.h>
108 
109 static char cmdname[] = "ztest";
110 static char *zopt_pool = cmdname;
111 
112 static uint64_t zopt_vdevs = 5;
113 static uint64_t zopt_vdevtime;
114 static int zopt_ashift = SPA_MINBLOCKSHIFT;
115 static int zopt_mirrors = 2;
116 static int zopt_raidz = 4;
117 static int zopt_raidz_parity = 1;
118 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
119 static int zopt_datasets = 7;
120 static int zopt_threads = 23;
121 static uint64_t zopt_passtime = 60;	/* 60 seconds */
122 static uint64_t zopt_killrate = 70;	/* 70% kill rate */
123 static int zopt_verbose = 0;
124 static int zopt_init = 1;
125 static char *zopt_dir = "/tmp";
126 static uint64_t zopt_time = 300;	/* 5 minutes */
127 static int zopt_maxfaults;
128 
129 typedef struct ztest_block_tag {
130 	uint64_t	bt_objset;
131 	uint64_t	bt_object;
132 	uint64_t	bt_offset;
133 	uint64_t	bt_txg;
134 	uint64_t	bt_thread;
135 	uint64_t	bt_seq;
136 } ztest_block_tag_t;
137 
138 typedef struct ztest_args {
139 	char		za_pool[MAXNAMELEN];
140 	spa_t		*za_spa;
141 	objset_t	*za_os;
142 	zilog_t		*za_zilog;
143 	thread_t	za_thread;
144 	uint64_t	za_instance;
145 	uint64_t	za_random;
146 	uint64_t	za_diroff;
147 	uint64_t	za_diroff_shared;
148 	uint64_t	za_zil_seq;
149 	hrtime_t	za_start;
150 	hrtime_t	za_stop;
151 	hrtime_t	za_kill;
152 	/*
153 	 * Thread-local variables can go here to aid debugging.
154 	 */
155 	ztest_block_tag_t za_rbt;
156 	ztest_block_tag_t za_wbt;
157 	dmu_object_info_t za_doi;
158 	dmu_buf_t	*za_dbuf;
159 } ztest_args_t;
160 
161 typedef void ztest_func_t(ztest_args_t *);
162 
163 /*
164  * Note: these aren't static because we want dladdr() to work.
165  */
166 ztest_func_t ztest_dmu_read_write;
167 ztest_func_t ztest_dmu_read_write_zcopy;
168 ztest_func_t ztest_dmu_write_parallel;
169 ztest_func_t ztest_dmu_object_alloc_free;
170 ztest_func_t ztest_zap;
171 ztest_func_t ztest_fzap;
172 ztest_func_t ztest_zap_parallel;
173 ztest_func_t ztest_traverse;
174 ztest_func_t ztest_dsl_prop_get_set;
175 ztest_func_t ztest_dmu_objset_create_destroy;
176 ztest_func_t ztest_dmu_snapshot_create_destroy;
177 ztest_func_t ztest_dsl_dataset_promote_busy;
178 ztest_func_t ztest_spa_create_destroy;
179 ztest_func_t ztest_fault_inject;
180 ztest_func_t ztest_spa_rename;
181 ztest_func_t ztest_vdev_attach_detach;
182 ztest_func_t ztest_vdev_LUN_growth;
183 ztest_func_t ztest_vdev_add_remove;
184 ztest_func_t ztest_vdev_aux_add_remove;
185 ztest_func_t ztest_scrub;
186 
187 typedef struct ztest_info {
188 	ztest_func_t	*zi_func;	/* test function */
189 	uint64_t	zi_iters;	/* iterations per execution */
190 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
191 	uint64_t	zi_calls;	/* per-pass count */
192 	uint64_t	zi_call_time;	/* per-pass time */
193 	uint64_t	zi_call_total;	/* cumulative total */
194 	uint64_t	zi_call_target;	/* target cumulative total */
195 } ztest_info_t;
196 
197 uint64_t zopt_always = 0;		/* all the time */
198 uint64_t zopt_often = 1;		/* every second */
199 uint64_t zopt_sometimes = 10;		/* every 10 seconds */
200 uint64_t zopt_rarely = 60;		/* every 60 seconds */
201 
202 ztest_info_t ztest_info[] = {
203 	{ ztest_dmu_read_write,			1,	&zopt_always	},
204 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_always	},
205 	{ ztest_dmu_write_parallel,		30,	&zopt_always	},
206 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
207 	{ ztest_zap,				30,	&zopt_always	},
208 	{ ztest_fzap,				30,	&zopt_always	},
209 	{ ztest_zap_parallel,			100,	&zopt_always	},
210 	{ ztest_dsl_prop_get_set,		1,	&zopt_sometimes	},
211 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_sometimes },
212 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes },
213 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes },
214 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
215 	{ ztest_spa_rename,			1,	&zopt_rarely	},
216 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely	},
217 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
218 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
219 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime	},
220 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
221 	{ ztest_scrub,				1,	&zopt_vdevtime	},
222 };
223 
224 #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
225 
226 #define	ZTEST_SYNC_LOCKS	16
227 
228 /*
229  * Stuff we need to share writably between parent and child.
230  */
231 typedef struct ztest_shared {
232 	mutex_t		zs_vdev_lock;
233 	rwlock_t	zs_name_lock;
234 	uint64_t	zs_vdev_primaries;
235 	uint64_t	zs_vdev_aux;
236 	uint64_t	zs_enospc_count;
237 	hrtime_t	zs_start_time;
238 	hrtime_t	zs_stop_time;
239 	uint64_t	zs_alloc;
240 	uint64_t	zs_space;
241 	ztest_info_t	zs_info[ZTEST_FUNCS];
242 	mutex_t		zs_sync_lock[ZTEST_SYNC_LOCKS];
243 	uint64_t	zs_seq[ZTEST_SYNC_LOCKS];
244 } ztest_shared_t;
245 
246 static char ztest_dev_template[] = "%s/%s.%llua";
247 static char ztest_aux_template[] = "%s/%s.%s.%llu";
248 static ztest_shared_t *ztest_shared;
249 
250 static int ztest_random_fd;
251 static int ztest_dump_core = 1;
252 
253 static uint64_t metaslab_sz;
254 static boolean_t ztest_exiting;
255 
256 extern uint64_t metaslab_gang_bang;
257 extern uint64_t metaslab_df_alloc_threshold;
258 
259 #define	ZTEST_DIROBJ		1
260 #define	ZTEST_MICROZAP_OBJ	2
261 #define	ZTEST_FATZAP_OBJ	3
262 
263 #define	ZTEST_DIROBJ_BLOCKSIZE	(1 << 10)
264 #define	ZTEST_DIRSIZE		256
265 
266 static void usage(boolean_t) __NORETURN;
267 
268 /*
269  * These libumem hooks provide a reasonable set of defaults for the allocator's
270  * debugging facilities.
271  */
272 const char *
273 _umem_debug_init()
274 {
275 	return ("default,verbose"); /* $UMEM_DEBUG setting */
276 }
277 
278 const char *
279 _umem_logging_init(void)
280 {
281 	return ("fail,contents"); /* $UMEM_LOGGING setting */
282 }
283 
284 #define	FATAL_MSG_SZ	1024
285 
286 char *fatal_msg;
287 
288 static void
289 fatal(int do_perror, char *message, ...)
290 {
291 	va_list args;
292 	int save_errno = errno;
293 	char buf[FATAL_MSG_SZ];
294 
295 	(void) fflush(stdout);
296 
297 	va_start(args, message);
298 	(void) sprintf(buf, "ztest: ");
299 	/* LINTED */
300 	(void) vsprintf(buf + strlen(buf), message, args);
301 	va_end(args);
302 	if (do_perror) {
303 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
304 		    ": %s", strerror(save_errno));
305 	}
306 	(void) fprintf(stderr, "%s\n", buf);
307 	fatal_msg = buf;			/* to ease debugging */
308 	if (ztest_dump_core)
309 		abort();
310 	exit(3);
311 }
312 
313 static int
314 str2shift(const char *buf)
315 {
316 	const char *ends = "BKMGTPEZ";
317 	int i;
318 
319 	if (buf[0] == '\0')
320 		return (0);
321 	for (i = 0; i < strlen(ends); i++) {
322 		if (toupper(buf[0]) == ends[i])
323 			break;
324 	}
325 	if (i == strlen(ends)) {
326 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
327 		    buf);
328 		usage(B_FALSE);
329 	}
330 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
331 		return (10*i);
332 	}
333 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
334 	usage(B_FALSE);
335 	/* NOTREACHED */
336 }
337 
338 static uint64_t
339 nicenumtoull(const char *buf)
340 {
341 	char *end;
342 	uint64_t val;
343 
344 	val = strtoull(buf, &end, 0);
345 	if (end == buf) {
346 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
347 		usage(B_FALSE);
348 	} else if (end[0] == '.') {
349 		double fval = strtod(buf, &end);
350 		fval *= pow(2, str2shift(end));
351 		if (fval > UINT64_MAX) {
352 			(void) fprintf(stderr, "ztest: value too large: %s\n",
353 			    buf);
354 			usage(B_FALSE);
355 		}
356 		val = (uint64_t)fval;
357 	} else {
358 		int shift = str2shift(end);
359 		if (shift >= 64 || (val << shift) >> shift != val) {
360 			(void) fprintf(stderr, "ztest: value too large: %s\n",
361 			    buf);
362 			usage(B_FALSE);
363 		}
364 		val <<= shift;
365 	}
366 	return (val);
367 }
368 
369 static void
370 usage(boolean_t requested)
371 {
372 	char nice_vdev_size[10];
373 	char nice_gang_bang[10];
374 	FILE *fp = requested ? stdout : stderr;
375 
376 	nicenum(zopt_vdev_size, nice_vdev_size);
377 	nicenum(metaslab_gang_bang, nice_gang_bang);
378 
379 	(void) fprintf(fp, "Usage: %s\n"
380 	    "\t[-v vdevs (default: %llu)]\n"
381 	    "\t[-s size_of_each_vdev (default: %s)]\n"
382 	    "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
383 	    "\t[-m mirror_copies (default: %d)]\n"
384 	    "\t[-r raidz_disks (default: %d)]\n"
385 	    "\t[-R raidz_parity (default: %d)]\n"
386 	    "\t[-d datasets (default: %d)]\n"
387 	    "\t[-t threads (default: %d)]\n"
388 	    "\t[-g gang_block_threshold (default: %s)]\n"
389 	    "\t[-i initialize pool i times (default: %d)]\n"
390 	    "\t[-k kill percentage (default: %llu%%)]\n"
391 	    "\t[-p pool_name (default: %s)]\n"
392 	    "\t[-f file directory for vdev files (default: %s)]\n"
393 	    "\t[-V(erbose)] (use multiple times for ever more blather)\n"
394 	    "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
395 	    "\t[-T time] total run time (default: %llu sec)\n"
396 	    "\t[-P passtime] time per pass (default: %llu sec)\n"
397 	    "\t[-h] (print help)\n"
398 	    "",
399 	    cmdname,
400 	    (u_longlong_t)zopt_vdevs,			/* -v */
401 	    nice_vdev_size,				/* -s */
402 	    zopt_ashift,				/* -a */
403 	    zopt_mirrors,				/* -m */
404 	    zopt_raidz,					/* -r */
405 	    zopt_raidz_parity,				/* -R */
406 	    zopt_datasets,				/* -d */
407 	    zopt_threads,				/* -t */
408 	    nice_gang_bang,				/* -g */
409 	    zopt_init,					/* -i */
410 	    (u_longlong_t)zopt_killrate,		/* -k */
411 	    zopt_pool,					/* -p */
412 	    zopt_dir,					/* -f */
413 	    (u_longlong_t)zopt_time,			/* -T */
414 	    (u_longlong_t)zopt_passtime);		/* -P */
415 	exit(requested ? 0 : 1);
416 }
417 
418 static uint64_t
419 ztest_random(uint64_t range)
420 {
421 	uint64_t r;
422 
423 	if (range == 0)
424 		return (0);
425 
426 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
427 		fatal(1, "short read from /dev/urandom");
428 
429 	return (r % range);
430 }
431 
432 /* ARGSUSED */
433 static void
434 ztest_record_enospc(char *s)
435 {
436 	ztest_shared->zs_enospc_count++;
437 }
438 
439 static void
440 process_options(int argc, char **argv)
441 {
442 	int opt;
443 	uint64_t value;
444 
445 	/* By default, test gang blocks for blocks 32K and greater */
446 	metaslab_gang_bang = 32 << 10;
447 
448 	while ((opt = getopt(argc, argv,
449 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
450 		value = 0;
451 		switch (opt) {
452 		case 'v':
453 		case 's':
454 		case 'a':
455 		case 'm':
456 		case 'r':
457 		case 'R':
458 		case 'd':
459 		case 't':
460 		case 'g':
461 		case 'i':
462 		case 'k':
463 		case 'T':
464 		case 'P':
465 			value = nicenumtoull(optarg);
466 		}
467 		switch (opt) {
468 		case 'v':
469 			zopt_vdevs = value;
470 			break;
471 		case 's':
472 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
473 			break;
474 		case 'a':
475 			zopt_ashift = value;
476 			break;
477 		case 'm':
478 			zopt_mirrors = value;
479 			break;
480 		case 'r':
481 			zopt_raidz = MAX(1, value);
482 			break;
483 		case 'R':
484 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
485 			break;
486 		case 'd':
487 			zopt_datasets = MAX(1, value);
488 			break;
489 		case 't':
490 			zopt_threads = MAX(1, value);
491 			break;
492 		case 'g':
493 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
494 			break;
495 		case 'i':
496 			zopt_init = value;
497 			break;
498 		case 'k':
499 			zopt_killrate = value;
500 			break;
501 		case 'p':
502 			zopt_pool = strdup(optarg);
503 			break;
504 		case 'f':
505 			zopt_dir = strdup(optarg);
506 			break;
507 		case 'V':
508 			zopt_verbose++;
509 			break;
510 		case 'E':
511 			zopt_init = 0;
512 			break;
513 		case 'T':
514 			zopt_time = value;
515 			break;
516 		case 'P':
517 			zopt_passtime = MAX(1, value);
518 			break;
519 		case 'h':
520 			usage(B_TRUE);
521 			break;
522 		case '?':
523 		default:
524 			usage(B_FALSE);
525 			break;
526 		}
527 	}
528 
529 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
530 
531 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
532 	zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
533 }
534 
535 static uint64_t
536 ztest_get_ashift(void)
537 {
538 	if (zopt_ashift == 0)
539 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
540 	return (zopt_ashift);
541 }
542 
543 static nvlist_t *
544 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
545 {
546 	char pathbuf[MAXPATHLEN];
547 	uint64_t vdev;
548 	nvlist_t *file;
549 
550 	if (ashift == 0)
551 		ashift = ztest_get_ashift();
552 
553 	if (path == NULL) {
554 		path = pathbuf;
555 
556 		if (aux != NULL) {
557 			vdev = ztest_shared->zs_vdev_aux;
558 			(void) sprintf(path, ztest_aux_template,
559 			    zopt_dir, zopt_pool, aux, vdev);
560 		} else {
561 			vdev = ztest_shared->zs_vdev_primaries++;
562 			(void) sprintf(path, ztest_dev_template,
563 			    zopt_dir, zopt_pool, vdev);
564 		}
565 	}
566 
567 	if (size != 0) {
568 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
569 		if (fd == -1)
570 			fatal(1, "can't open %s", path);
571 		if (ftruncate(fd, size) != 0)
572 			fatal(1, "can't ftruncate %s", path);
573 		(void) close(fd);
574 	}
575 
576 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
577 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
578 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
579 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
580 
581 	return (file);
582 }
583 
584 static nvlist_t *
585 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
586 {
587 	nvlist_t *raidz, **child;
588 	int c;
589 
590 	if (r < 2)
591 		return (make_vdev_file(path, aux, size, ashift));
592 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
593 
594 	for (c = 0; c < r; c++)
595 		child[c] = make_vdev_file(path, aux, size, ashift);
596 
597 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
598 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
599 	    VDEV_TYPE_RAIDZ) == 0);
600 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
601 	    zopt_raidz_parity) == 0);
602 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
603 	    child, r) == 0);
604 
605 	for (c = 0; c < r; c++)
606 		nvlist_free(child[c]);
607 
608 	umem_free(child, r * sizeof (nvlist_t *));
609 
610 	return (raidz);
611 }
612 
613 static nvlist_t *
614 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
615 	int r, int m)
616 {
617 	nvlist_t *mirror, **child;
618 	int c;
619 
620 	if (m < 1)
621 		return (make_vdev_raidz(path, aux, size, ashift, r));
622 
623 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
624 
625 	for (c = 0; c < m; c++)
626 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
627 
628 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
629 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
630 	    VDEV_TYPE_MIRROR) == 0);
631 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
632 	    child, m) == 0);
633 
634 	for (c = 0; c < m; c++)
635 		nvlist_free(child[c]);
636 
637 	umem_free(child, m * sizeof (nvlist_t *));
638 
639 	return (mirror);
640 }
641 
642 static nvlist_t *
643 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
644 	int log, int r, int m, int t)
645 {
646 	nvlist_t *root, **child;
647 	int c;
648 
649 	ASSERT(t > 0);
650 
651 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
652 
653 	for (c = 0; c < t; c++) {
654 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
655 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
656 		    log) == 0);
657 	}
658 
659 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
660 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
661 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
662 	    child, t) == 0);
663 
664 	for (c = 0; c < t; c++)
665 		nvlist_free(child[c]);
666 
667 	umem_free(child, t * sizeof (nvlist_t *));
668 
669 	return (root);
670 }
671 
672 static void
673 ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
674 {
675 	int bs = SPA_MINBLOCKSHIFT +
676 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
677 	int ibs = DN_MIN_INDBLKSHIFT +
678 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
679 	int error;
680 
681 	error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
682 	if (error) {
683 		char osname[300];
684 		dmu_objset_name(os, osname);
685 		fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
686 		    osname, object, 1 << bs, ibs, error);
687 	}
688 }
689 
690 static uint8_t
691 ztest_random_checksum(void)
692 {
693 	uint8_t checksum;
694 
695 	do {
696 		checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
697 	} while (zio_checksum_table[checksum].ci_zbt);
698 
699 	if (checksum == ZIO_CHECKSUM_OFF)
700 		checksum = ZIO_CHECKSUM_ON;
701 
702 	return (checksum);
703 }
704 
705 static uint8_t
706 ztest_random_compress(void)
707 {
708 	return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
709 }
710 
711 static int
712 ztest_replay_create(objset_t *os, lr_create_t *lr, boolean_t byteswap)
713 {
714 	dmu_tx_t *tx;
715 	int error;
716 
717 	if (byteswap)
718 		byteswap_uint64_array(lr, sizeof (*lr));
719 
720 	tx = dmu_tx_create(os);
721 	dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
722 	error = dmu_tx_assign(tx, TXG_WAIT);
723 	if (error) {
724 		dmu_tx_abort(tx);
725 		return (error);
726 	}
727 
728 	error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
729 	    DMU_OT_NONE, 0, tx);
730 	ASSERT3U(error, ==, 0);
731 	dmu_tx_commit(tx);
732 
733 	if (zopt_verbose >= 5) {
734 		char osname[MAXNAMELEN];
735 		dmu_objset_name(os, osname);
736 		(void) printf("replay create of %s object %llu"
737 		    " in txg %llu = %d\n",
738 		    osname, (u_longlong_t)lr->lr_doid,
739 		    (u_longlong_t)dmu_tx_get_txg(tx), error);
740 	}
741 
742 	return (error);
743 }
744 
745 static int
746 ztest_replay_remove(objset_t *os, lr_remove_t *lr, boolean_t byteswap)
747 {
748 	dmu_tx_t *tx;
749 	int error;
750 
751 	if (byteswap)
752 		byteswap_uint64_array(lr, sizeof (*lr));
753 
754 	tx = dmu_tx_create(os);
755 	dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
756 	error = dmu_tx_assign(tx, TXG_WAIT);
757 	if (error) {
758 		dmu_tx_abort(tx);
759 		return (error);
760 	}
761 
762 	error = dmu_object_free(os, lr->lr_doid, tx);
763 	dmu_tx_commit(tx);
764 
765 	return (error);
766 }
767 
768 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
769 	NULL,			/* 0 no such transaction type */
770 	ztest_replay_create,	/* TX_CREATE */
771 	NULL,			/* TX_MKDIR */
772 	NULL,			/* TX_MKXATTR */
773 	NULL,			/* TX_SYMLINK */
774 	ztest_replay_remove,	/* TX_REMOVE */
775 	NULL,			/* TX_RMDIR */
776 	NULL,			/* TX_LINK */
777 	NULL,			/* TX_RENAME */
778 	NULL,			/* TX_WRITE */
779 	NULL,			/* TX_TRUNCATE */
780 	NULL,			/* TX_SETATTR */
781 	NULL,			/* TX_ACL */
782 };
783 
784 /*
785  * Verify that we can't destroy an active pool, create an existing pool,
786  * or create a pool with a bad vdev spec.
787  */
788 void
789 ztest_spa_create_destroy(ztest_args_t *za)
790 {
791 	int error;
792 	spa_t *spa;
793 	nvlist_t *nvroot;
794 
795 	/*
796 	 * Attempt to create using a bad file.
797 	 */
798 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
799 	error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
800 	nvlist_free(nvroot);
801 	if (error != ENOENT)
802 		fatal(0, "spa_create(bad_file) = %d", error);
803 
804 	/*
805 	 * Attempt to create using a bad mirror.
806 	 */
807 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
808 	error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
809 	nvlist_free(nvroot);
810 	if (error != ENOENT)
811 		fatal(0, "spa_create(bad_mirror) = %d", error);
812 
813 	/*
814 	 * Attempt to create an existing pool.  It shouldn't matter
815 	 * what's in the nvroot; we should fail with EEXIST.
816 	 */
817 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
818 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
819 	error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
820 	nvlist_free(nvroot);
821 	if (error != EEXIST)
822 		fatal(0, "spa_create(whatever) = %d", error);
823 
824 	error = spa_open(za->za_pool, &spa, FTAG);
825 	if (error)
826 		fatal(0, "spa_open() = %d", error);
827 
828 	error = spa_destroy(za->za_pool);
829 	if (error != EBUSY)
830 		fatal(0, "spa_destroy() = %d", error);
831 
832 	spa_close(spa, FTAG);
833 	(void) rw_unlock(&ztest_shared->zs_name_lock);
834 }
835 
836 static vdev_t *
837 vdev_lookup_by_path(vdev_t *vd, const char *path)
838 {
839 	vdev_t *mvd;
840 
841 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
842 		return (vd);
843 
844 	for (int c = 0; c < vd->vdev_children; c++)
845 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
846 		    NULL)
847 			return (mvd);
848 
849 	return (NULL);
850 }
851 
852 /*
853  * Verify that vdev_add() works as expected.
854  */
855 void
856 ztest_vdev_add_remove(ztest_args_t *za)
857 {
858 	spa_t *spa = za->za_spa;
859 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
860 	nvlist_t *nvroot;
861 	int error;
862 
863 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
864 
865 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
866 
867 	ztest_shared->zs_vdev_primaries =
868 	    spa->spa_root_vdev->vdev_children * leaves;
869 
870 	spa_config_exit(spa, SCL_VDEV, FTAG);
871 
872 	/*
873 	 * Make 1/4 of the devices be log devices.
874 	 */
875 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
876 	    ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
877 
878 	error = spa_vdev_add(spa, nvroot);
879 	nvlist_free(nvroot);
880 
881 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
882 
883 	if (error == ENOSPC)
884 		ztest_record_enospc("spa_vdev_add");
885 	else if (error != 0)
886 		fatal(0, "spa_vdev_add() = %d", error);
887 }
888 
889 /*
890  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
891  */
892 void
893 ztest_vdev_aux_add_remove(ztest_args_t *za)
894 {
895 	spa_t *spa = za->za_spa;
896 	vdev_t *rvd = spa->spa_root_vdev;
897 	spa_aux_vdev_t *sav;
898 	char *aux;
899 	uint64_t guid = 0;
900 	int error;
901 
902 	if (ztest_random(2) == 0) {
903 		sav = &spa->spa_spares;
904 		aux = ZPOOL_CONFIG_SPARES;
905 	} else {
906 		sav = &spa->spa_l2cache;
907 		aux = ZPOOL_CONFIG_L2CACHE;
908 	}
909 
910 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
911 
912 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
913 
914 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
915 		/*
916 		 * Pick a random device to remove.
917 		 */
918 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
919 	} else {
920 		/*
921 		 * Find an unused device we can add.
922 		 */
923 		ztest_shared->zs_vdev_aux = 0;
924 		for (;;) {
925 			char path[MAXPATHLEN];
926 			int c;
927 			(void) sprintf(path, ztest_aux_template, zopt_dir,
928 			    zopt_pool, aux, ztest_shared->zs_vdev_aux);
929 			for (c = 0; c < sav->sav_count; c++)
930 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
931 				    path) == 0)
932 					break;
933 			if (c == sav->sav_count &&
934 			    vdev_lookup_by_path(rvd, path) == NULL)
935 				break;
936 			ztest_shared->zs_vdev_aux++;
937 		}
938 	}
939 
940 	spa_config_exit(spa, SCL_VDEV, FTAG);
941 
942 	if (guid == 0) {
943 		/*
944 		 * Add a new device.
945 		 */
946 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
947 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
948 		error = spa_vdev_add(spa, nvroot);
949 		if (error != 0)
950 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
951 		nvlist_free(nvroot);
952 	} else {
953 		/*
954 		 * Remove an existing device.  Sometimes, dirty its
955 		 * vdev state first to make sure we handle removal
956 		 * of devices that have pending state changes.
957 		 */
958 		if (ztest_random(2) == 0)
959 			(void) vdev_online(spa, guid, 0, NULL);
960 
961 		error = spa_vdev_remove(spa, guid, B_FALSE);
962 		if (error != 0 && error != EBUSY)
963 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
964 	}
965 
966 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
967 }
968 
969 /*
970  * Verify that we can attach and detach devices.
971  */
972 void
973 ztest_vdev_attach_detach(ztest_args_t *za)
974 {
975 	spa_t *spa = za->za_spa;
976 	spa_aux_vdev_t *sav = &spa->spa_spares;
977 	vdev_t *rvd = spa->spa_root_vdev;
978 	vdev_t *oldvd, *newvd, *pvd;
979 	nvlist_t *root;
980 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
981 	uint64_t leaf, top;
982 	uint64_t ashift = ztest_get_ashift();
983 	uint64_t oldguid, pguid;
984 	size_t oldsize, newsize;
985 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
986 	int replacing;
987 	int oldvd_has_siblings = B_FALSE;
988 	int newvd_is_spare = B_FALSE;
989 	int oldvd_is_log;
990 	int error, expected_error;
991 
992 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
993 
994 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
995 
996 	/*
997 	 * Decide whether to do an attach or a replace.
998 	 */
999 	replacing = ztest_random(2);
1000 
1001 	/*
1002 	 * Pick a random top-level vdev.
1003 	 */
1004 	top = ztest_random(rvd->vdev_children);
1005 
1006 	/*
1007 	 * Pick a random leaf within it.
1008 	 */
1009 	leaf = ztest_random(leaves);
1010 
1011 	/*
1012 	 * Locate this vdev.
1013 	 */
1014 	oldvd = rvd->vdev_child[top];
1015 	if (zopt_mirrors >= 1) {
1016 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
1017 		ASSERT(oldvd->vdev_children >= zopt_mirrors);
1018 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
1019 	}
1020 	if (zopt_raidz > 1) {
1021 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
1022 		ASSERT(oldvd->vdev_children == zopt_raidz);
1023 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
1024 	}
1025 
1026 	/*
1027 	 * If we're already doing an attach or replace, oldvd may be a
1028 	 * mirror vdev -- in which case, pick a random child.
1029 	 */
1030 	while (oldvd->vdev_children != 0) {
1031 		oldvd_has_siblings = B_TRUE;
1032 		ASSERT(oldvd->vdev_children >= 2);
1033 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
1034 	}
1035 
1036 	oldguid = oldvd->vdev_guid;
1037 	oldsize = vdev_get_min_asize(oldvd);
1038 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
1039 	(void) strcpy(oldpath, oldvd->vdev_path);
1040 	pvd = oldvd->vdev_parent;
1041 	pguid = pvd->vdev_guid;
1042 
1043 	/*
1044 	 * If oldvd has siblings, then half of the time, detach it.
1045 	 */
1046 	if (oldvd_has_siblings && ztest_random(2) == 0) {
1047 		spa_config_exit(spa, SCL_VDEV, FTAG);
1048 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
1049 		if (error != 0 && error != ENODEV && error != EBUSY &&
1050 		    error != ENOTSUP)
1051 			fatal(0, "detach (%s) returned %d", oldpath, error);
1052 		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1053 		return;
1054 	}
1055 
1056 	/*
1057 	 * For the new vdev, choose with equal probability between the two
1058 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
1059 	 */
1060 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
1061 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
1062 		newvd_is_spare = B_TRUE;
1063 		(void) strcpy(newpath, newvd->vdev_path);
1064 	} else {
1065 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
1066 		    zopt_dir, zopt_pool, top * leaves + leaf);
1067 		if (ztest_random(2) == 0)
1068 			newpath[strlen(newpath) - 1] = 'b';
1069 		newvd = vdev_lookup_by_path(rvd, newpath);
1070 	}
1071 
1072 	if (newvd) {
1073 		newsize = vdev_get_min_asize(newvd);
1074 	} else {
1075 		/*
1076 		 * Make newsize a little bigger or smaller than oldsize.
1077 		 * If it's smaller, the attach should fail.
1078 		 * If it's larger, and we're doing a replace,
1079 		 * we should get dynamic LUN growth when we're done.
1080 		 */
1081 		newsize = 10 * oldsize / (9 + ztest_random(3));
1082 	}
1083 
1084 	/*
1085 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
1086 	 * unless it's a replace; in that case any non-replacing parent is OK.
1087 	 *
1088 	 * If newvd is already part of the pool, it should fail with EBUSY.
1089 	 *
1090 	 * If newvd is too small, it should fail with EOVERFLOW.
1091 	 */
1092 	if (pvd->vdev_ops != &vdev_mirror_ops &&
1093 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
1094 	    pvd->vdev_ops == &vdev_replacing_ops ||
1095 	    pvd->vdev_ops == &vdev_spare_ops))
1096 		expected_error = ENOTSUP;
1097 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
1098 		expected_error = ENOTSUP;
1099 	else if (newvd == oldvd)
1100 		expected_error = replacing ? 0 : EBUSY;
1101 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
1102 		expected_error = EBUSY;
1103 	else if (newsize < oldsize)
1104 		expected_error = EOVERFLOW;
1105 	else if (ashift > oldvd->vdev_top->vdev_ashift)
1106 		expected_error = EDOM;
1107 	else
1108 		expected_error = 0;
1109 
1110 	spa_config_exit(spa, SCL_VDEV, FTAG);
1111 
1112 	/*
1113 	 * Build the nvlist describing newpath.
1114 	 */
1115 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
1116 	    ashift, 0, 0, 0, 1);
1117 
1118 	error = spa_vdev_attach(spa, oldguid, root, replacing);
1119 
1120 	nvlist_free(root);
1121 
1122 	/*
1123 	 * If our parent was the replacing vdev, but the replace completed,
1124 	 * then instead of failing with ENOTSUP we may either succeed,
1125 	 * fail with ENODEV, or fail with EOVERFLOW.
1126 	 */
1127 	if (expected_error == ENOTSUP &&
1128 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
1129 		expected_error = error;
1130 
1131 	/*
1132 	 * If someone grew the LUN, the replacement may be too small.
1133 	 */
1134 	if (error == EOVERFLOW || error == EBUSY)
1135 		expected_error = error;
1136 
1137 	/* XXX workaround 6690467 */
1138 	if (error != expected_error && expected_error != EBUSY) {
1139 		fatal(0, "attach (%s %llu, %s %llu, %d) "
1140 		    "returned %d, expected %d",
1141 		    oldpath, (longlong_t)oldsize, newpath,
1142 		    (longlong_t)newsize, replacing, error, expected_error);
1143 	}
1144 
1145 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1146 }
1147 
1148 /*
1149  * Callback function which expands the physical size of the vdev.
1150  */
1151 vdev_t *
1152 grow_vdev(vdev_t *vd, void *arg)
1153 {
1154 	spa_t *spa = vd->vdev_spa;
1155 	size_t *newsize = arg;
1156 	size_t fsize;
1157 	int fd;
1158 
1159 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
1160 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1161 
1162 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
1163 		return (vd);
1164 
1165 	fsize = lseek(fd, 0, SEEK_END);
1166 	(void) ftruncate(fd, *newsize);
1167 
1168 	if (zopt_verbose >= 6) {
1169 		(void) printf("%s grew from %lu to %lu bytes\n",
1170 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
1171 	}
1172 	(void) close(fd);
1173 	return (NULL);
1174 }
1175 
1176 /*
1177  * Callback function which expands a given vdev by calling vdev_online().
1178  */
1179 /* ARGSUSED */
1180 vdev_t *
1181 online_vdev(vdev_t *vd, void *arg)
1182 {
1183 	spa_t *spa = vd->vdev_spa;
1184 	vdev_t *tvd = vd->vdev_top;
1185 	vdev_t *pvd = vd->vdev_parent;
1186 	uint64_t guid = vd->vdev_guid;
1187 
1188 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
1189 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1190 
1191 	/* Calling vdev_online will initialize the new metaslabs */
1192 	spa_config_exit(spa, SCL_STATE, spa);
1193 	(void) vdev_online(spa, guid, ZFS_ONLINE_EXPAND, NULL);
1194 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1195 
1196 	/*
1197 	 * Since we dropped the lock we need to ensure that we're
1198 	 * still talking to the original vdev. It's possible this
1199 	 * vdev may have been detached/replaced while we were
1200 	 * trying to online it.
1201 	 */
1202 	if (vd != vdev_lookup_by_guid(tvd, guid) || vd->vdev_parent != pvd) {
1203 		if (zopt_verbose >= 6) {
1204 			(void) printf("vdev %p has disappeared, was "
1205 			    "guid %llu\n", (void *)vd, (u_longlong_t)guid);
1206 		}
1207 		return (vd);
1208 	}
1209 	return (NULL);
1210 }
1211 
1212 /*
1213  * Traverse the vdev tree calling the supplied function.
1214  * We continue to walk the tree until we either have walked all
1215  * children or we receive a non-NULL return from the callback.
1216  * If a NULL callback is passed, then we just return back the first
1217  * leaf vdev we encounter.
1218  */
1219 vdev_t *
1220 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
1221 {
1222 	if (vd->vdev_ops->vdev_op_leaf) {
1223 		if (func == NULL)
1224 			return (vd);
1225 		else
1226 			return (func(vd, arg));
1227 	}
1228 
1229 	for (uint_t c = 0; c < vd->vdev_children; c++) {
1230 		vdev_t *cvd = vd->vdev_child[c];
1231 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
1232 			return (cvd);
1233 	}
1234 	return (NULL);
1235 }
1236 
1237 /*
1238  * Verify that dynamic LUN growth works as expected.
1239  */
1240 void
1241 ztest_vdev_LUN_growth(ztest_args_t *za)
1242 {
1243 	spa_t *spa = za->za_spa;
1244 	vdev_t *vd, *tvd = NULL;
1245 	size_t psize, newsize;
1246 	uint64_t spa_newsize, spa_cursize, ms_count;
1247 
1248 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
1249 	mutex_enter(&spa_namespace_lock);
1250 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1251 
1252 	while (tvd == NULL || tvd->vdev_islog) {
1253 		uint64_t vdev;
1254 
1255 		vdev = ztest_random(spa->spa_root_vdev->vdev_children);
1256 		tvd = spa->spa_root_vdev->vdev_child[vdev];
1257 	}
1258 
1259 	/*
1260 	 * Determine the size of the first leaf vdev associated with
1261 	 * our top-level device.
1262 	 */
1263 	vd = vdev_walk_tree(tvd, NULL, NULL);
1264 	ASSERT3P(vd, !=, NULL);
1265 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1266 
1267 	psize = vd->vdev_psize;
1268 
1269 	/*
1270 	 * We only try to expand the vdev if it's less than 4x its
1271 	 * original size and it has a valid psize.
1272 	 */
1273 	if (psize == 0 || psize >= 4 * zopt_vdev_size) {
1274 		spa_config_exit(spa, SCL_STATE, spa);
1275 		mutex_exit(&spa_namespace_lock);
1276 		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1277 		return;
1278 	}
1279 	ASSERT(psize > 0);
1280 	newsize = psize + psize / 8;
1281 	ASSERT3U(newsize, >, psize);
1282 
1283 	if (zopt_verbose >= 6) {
1284 		(void) printf("Expanding vdev %s from %lu to %lu\n",
1285 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
1286 	}
1287 
1288 	spa_cursize = spa_get_space(spa);
1289 	ms_count = tvd->vdev_ms_count;
1290 
1291 	/*
1292 	 * Growing the vdev is a two step process:
1293 	 *	1). expand the physical size (i.e. relabel)
1294 	 *	2). online the vdev to create the new metaslabs
1295 	 */
1296 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
1297 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
1298 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
1299 		if (zopt_verbose >= 5) {
1300 			(void) printf("Could not expand LUN because "
1301 			    "some vdevs were not healthy\n");
1302 		}
1303 		(void) spa_config_exit(spa, SCL_STATE, spa);
1304 		mutex_exit(&spa_namespace_lock);
1305 		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1306 		return;
1307 	}
1308 
1309 	(void) spa_config_exit(spa, SCL_STATE, spa);
1310 	mutex_exit(&spa_namespace_lock);
1311 
1312 	/*
1313 	 * Expanding the LUN will update the config asynchronously,
1314 	 * thus we must wait for the async thread to complete any
1315 	 * pending tasks before proceeding.
1316 	 */
1317 	mutex_enter(&spa->spa_async_lock);
1318 	while (spa->spa_async_thread != NULL || spa->spa_async_tasks)
1319 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
1320 	mutex_exit(&spa->spa_async_lock);
1321 
1322 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1323 	spa_newsize = spa_get_space(spa);
1324 
1325 	/*
1326 	 * Make sure we were able to grow the pool.
1327 	 */
1328 	if (ms_count >= tvd->vdev_ms_count ||
1329 	    spa_cursize >= spa_newsize) {
1330 		(void) printf("Top-level vdev metaslab count: "
1331 		    "before %llu, after %llu\n",
1332 		    (u_longlong_t)ms_count,
1333 		    (u_longlong_t)tvd->vdev_ms_count);
1334 		fatal(0, "LUN expansion failed: before %llu, "
1335 		    "after %llu\n", spa_cursize, spa_newsize);
1336 	} else if (zopt_verbose >= 5) {
1337 		char oldnumbuf[6], newnumbuf[6];
1338 
1339 		nicenum(spa_cursize, oldnumbuf);
1340 		nicenum(spa_newsize, newnumbuf);
1341 		(void) printf("%s grew from %s to %s\n",
1342 		    spa->spa_name, oldnumbuf, newnumbuf);
1343 	}
1344 	spa_config_exit(spa, SCL_STATE, spa);
1345 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1346 }
1347 
1348 /* ARGSUSED */
1349 static void
1350 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1351 {
1352 	/*
1353 	 * Create the directory object.
1354 	 */
1355 	VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1356 	    DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1357 	    DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1358 
1359 	VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1360 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1361 
1362 	VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1363 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1364 }
1365 
1366 static int
1367 ztest_destroy_cb(char *name, void *arg)
1368 {
1369 	ztest_args_t *za = arg;
1370 	objset_t *os;
1371 	dmu_object_info_t *doi = &za->za_doi;
1372 	int error;
1373 
1374 	/*
1375 	 * Verify that the dataset contains a directory object.
1376 	 */
1377 	error = dmu_objset_hold(name, FTAG, &os);
1378 	ASSERT3U(error, ==, 0);
1379 	error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1380 	if (error != ENOENT) {
1381 		/* We could have crashed in the middle of destroying it */
1382 		ASSERT3U(error, ==, 0);
1383 		ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1384 		ASSERT3S(doi->doi_physical_blks, >=, 0);
1385 	}
1386 	dmu_objset_rele(os, FTAG);
1387 
1388 	/*
1389 	 * Destroy the dataset.
1390 	 */
1391 	error = dmu_objset_destroy(name, B_FALSE);
1392 	if (error) {
1393 		(void) dmu_objset_hold(name, FTAG, &os);
1394 		fatal(0, "dmu_objset_destroy(os=%p) = %d\n", os, error);
1395 	}
1396 	return (0);
1397 }
1398 
1399 /*
1400  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1401  */
1402 static uint64_t
1403 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1404 {
1405 	itx_t *itx;
1406 	lr_create_t *lr;
1407 	size_t namesize;
1408 	char name[24];
1409 
1410 	(void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1411 	namesize = strlen(name) + 1;
1412 
1413 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1414 	    ztest_random(ZIL_MAX_BLKSZ));
1415 	lr = (lr_create_t *)&itx->itx_lr;
1416 	bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1417 	lr->lr_doid = object;
1418 	lr->lr_foid = 0;
1419 	lr->lr_mode = mode;
1420 	lr->lr_uid = 0;
1421 	lr->lr_gid = 0;
1422 	lr->lr_gen = dmu_tx_get_txg(tx);
1423 	lr->lr_crtime[0] = time(NULL);
1424 	lr->lr_crtime[1] = 0;
1425 	lr->lr_rdev = 0;
1426 	bcopy(name, (char *)(lr + 1), namesize);
1427 
1428 	return (zil_itx_assign(zilog, itx, tx));
1429 }
1430 
1431 void
1432 ztest_dmu_objset_create_destroy(ztest_args_t *za)
1433 {
1434 	int error;
1435 	objset_t *os, *os2;
1436 	char name[100];
1437 	zilog_t *zilog;
1438 	uint64_t seq;
1439 	uint64_t objects;
1440 
1441 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1442 	(void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1443 	    (u_longlong_t)za->za_instance);
1444 
1445 	/*
1446 	 * If this dataset exists from a previous run, process its replay log
1447 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
1448 	 * (invoked from ztest_destroy_cb() below) should just throw it away.
1449 	 */
1450 	if (ztest_random(2) == 0 &&
1451 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
1452 		zil_replay(os, os, ztest_replay_vector);
1453 		dmu_objset_disown(os, FTAG);
1454 	}
1455 
1456 	/*
1457 	 * There may be an old instance of the dataset we're about to
1458 	 * create lying around from a previous run.  If so, destroy it
1459 	 * and all of its snapshots.
1460 	 */
1461 	(void) dmu_objset_find(name, ztest_destroy_cb, za,
1462 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1463 
1464 	/*
1465 	 * Verify that the destroyed dataset is no longer in the namespace.
1466 	 */
1467 	error = dmu_objset_hold(name, FTAG, &os);
1468 	if (error != ENOENT)
1469 		fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1470 		    name, os);
1471 
1472 	/*
1473 	 * Verify that we can create a new dataset.
1474 	 */
1475 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
1476 	    ztest_create_cb, NULL);
1477 	if (error) {
1478 		if (error == ENOSPC) {
1479 			ztest_record_enospc("dmu_objset_create");
1480 			(void) rw_unlock(&ztest_shared->zs_name_lock);
1481 			return;
1482 		}
1483 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
1484 	}
1485 
1486 	error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os);
1487 	if (error) {
1488 		fatal(0, "dmu_objset_open(%s) = %d", name, error);
1489 	}
1490 
1491 	/*
1492 	 * Open the intent log for it.
1493 	 */
1494 	zilog = zil_open(os, NULL);
1495 
1496 	/*
1497 	 * Put a random number of objects in there.
1498 	 */
1499 	objects = ztest_random(20);
1500 	seq = 0;
1501 	while (objects-- != 0) {
1502 		uint64_t object;
1503 		dmu_tx_t *tx = dmu_tx_create(os);
1504 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1505 		error = dmu_tx_assign(tx, TXG_WAIT);
1506 		if (error) {
1507 			dmu_tx_abort(tx);
1508 		} else {
1509 			object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1510 			    DMU_OT_NONE, 0, tx);
1511 			ztest_set_random_blocksize(os, object, tx);
1512 			seq = ztest_log_create(zilog, tx, object,
1513 			    DMU_OT_UINT64_OTHER);
1514 			dmu_write(os, object, 0, sizeof (name), name, tx);
1515 			dmu_tx_commit(tx);
1516 		}
1517 		if (ztest_random(5) == 0) {
1518 			zil_commit(zilog, seq, object);
1519 		}
1520 		if (ztest_random(100) == 0) {
1521 			error = zil_suspend(zilog);
1522 			if (error == 0) {
1523 				zil_resume(zilog);
1524 			}
1525 		}
1526 	}
1527 
1528 	/*
1529 	 * Verify that we cannot create an existing dataset.
1530 	 */
1531 	error = dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL);
1532 	if (error != EEXIST)
1533 		fatal(0, "created existing dataset, error = %d", error);
1534 
1535 	/*
1536 	 * Verify that we can hold an objset that is also owned.
1537 	 */
1538 	error = dmu_objset_hold(name, FTAG, &os2);
1539 	if (error)
1540 		fatal(0, "dmu_objset_open('%s') = %d", name, error);
1541 	dmu_objset_rele(os2, FTAG);
1542 
1543 	/*
1544 	 * Verify that we can not own an objset that is already owned.
1545 	 */
1546 	error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2);
1547 	if (error != EBUSY)
1548 		fatal(0, "dmu_objset_open('%s') = %d, expected EBUSY",
1549 		    name, error);
1550 
1551 	zil_close(zilog);
1552 	dmu_objset_disown(os, FTAG);
1553 
1554 	error = dmu_objset_destroy(name, B_FALSE);
1555 	if (error)
1556 		fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1557 
1558 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1559 }
1560 
1561 /*
1562  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1563  */
1564 void
1565 ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1566 {
1567 	int error;
1568 	objset_t *os = za->za_os;
1569 	char snapname[100];
1570 	char osname[MAXNAMELEN];
1571 
1572 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1573 	dmu_objset_name(os, osname);
1574 	(void) snprintf(snapname, 100, "%s@%llu", osname,
1575 	    (u_longlong_t)za->za_instance);
1576 
1577 	error = dmu_objset_destroy(snapname, B_FALSE);
1578 	if (error != 0 && error != ENOENT)
1579 		fatal(0, "dmu_objset_destroy() = %d", error);
1580 	error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1,
1581 	    NULL, FALSE);
1582 	if (error == ENOSPC)
1583 		ztest_record_enospc("dmu_take_snapshot");
1584 	else if (error != 0 && error != EEXIST)
1585 		fatal(0, "dmu_take_snapshot() = %d", error);
1586 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1587 }
1588 
1589 /*
1590  * Cleanup non-standard snapshots and clones.
1591  */
1592 void
1593 ztest_dsl_dataset_cleanup(char *osname, uint64_t curval)
1594 {
1595 	char snap1name[100];
1596 	char clone1name[100];
1597 	char snap2name[100];
1598 	char clone2name[100];
1599 	char snap3name[100];
1600 	int error;
1601 
1602 	(void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval);
1603 	(void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval);
1604 	(void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval);
1605 	(void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval);
1606 	(void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval);
1607 
1608 	error = dmu_objset_destroy(clone2name, B_FALSE);
1609 	if (error && error != ENOENT)
1610 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
1611 	error = dmu_objset_destroy(snap3name, B_FALSE);
1612 	if (error && error != ENOENT)
1613 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
1614 	error = dmu_objset_destroy(snap2name, B_FALSE);
1615 	if (error && error != ENOENT)
1616 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
1617 	error = dmu_objset_destroy(clone1name, B_FALSE);
1618 	if (error && error != ENOENT)
1619 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
1620 	error = dmu_objset_destroy(snap1name, B_FALSE);
1621 	if (error && error != ENOENT)
1622 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
1623 }
1624 
1625 /*
1626  * Verify dsl_dataset_promote handles EBUSY
1627  */
1628 void
1629 ztest_dsl_dataset_promote_busy(ztest_args_t *za)
1630 {
1631 	int error;
1632 	objset_t *os = za->za_os;
1633 	objset_t *clone;
1634 	dsl_dataset_t *ds;
1635 	char snap1name[100];
1636 	char clone1name[100];
1637 	char snap2name[100];
1638 	char clone2name[100];
1639 	char snap3name[100];
1640 	char osname[MAXNAMELEN];
1641 	uint64_t curval = za->za_instance;
1642 
1643 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1644 
1645 	dmu_objset_name(os, osname);
1646 	ztest_dsl_dataset_cleanup(osname, curval);
1647 
1648 	(void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval);
1649 	(void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval);
1650 	(void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval);
1651 	(void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval);
1652 	(void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval);
1653 
1654 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
1655 	    NULL, FALSE);
1656 	if (error && error != EEXIST) {
1657 		if (error == ENOSPC) {
1658 			ztest_record_enospc("dmu_take_snapshot");
1659 			goto out;
1660 		}
1661 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
1662 	}
1663 
1664 	error = dmu_objset_hold(snap1name, FTAG, &clone);
1665 	if (error)
1666 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
1667 
1668 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
1669 	dmu_objset_rele(clone, FTAG);
1670 	if (error) {
1671 		if (error == ENOSPC) {
1672 			ztest_record_enospc("dmu_objset_create");
1673 			goto out;
1674 		}
1675 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
1676 	}
1677 
1678 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
1679 	    NULL, FALSE);
1680 	if (error && error != EEXIST) {
1681 		if (error == ENOSPC) {
1682 			ztest_record_enospc("dmu_take_snapshot");
1683 			goto out;
1684 		}
1685 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
1686 	}
1687 
1688 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
1689 	    NULL, FALSE);
1690 	if (error && error != EEXIST) {
1691 		if (error == ENOSPC) {
1692 			ztest_record_enospc("dmu_take_snapshot");
1693 			goto out;
1694 		}
1695 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
1696 	}
1697 
1698 	error = dmu_objset_hold(snap3name, FTAG, &clone);
1699 	if (error)
1700 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
1701 
1702 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
1703 	dmu_objset_rele(clone, FTAG);
1704 	if (error) {
1705 		if (error == ENOSPC) {
1706 			ztest_record_enospc("dmu_objset_create");
1707 			goto out;
1708 		}
1709 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
1710 	}
1711 
1712 	error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds);
1713 	if (error)
1714 		fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
1715 	error = dsl_dataset_promote(clone2name);
1716 	if (error != EBUSY)
1717 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
1718 		    error);
1719 	dsl_dataset_disown(ds, FTAG);
1720 
1721 out:
1722 	ztest_dsl_dataset_cleanup(osname, curval);
1723 
1724 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1725 }
1726 
1727 /*
1728  * Verify that dmu_object_{alloc,free} work as expected.
1729  */
1730 void
1731 ztest_dmu_object_alloc_free(ztest_args_t *za)
1732 {
1733 	objset_t *os = za->za_os;
1734 	dmu_buf_t *db;
1735 	dmu_tx_t *tx;
1736 	uint64_t batchobj, object, batchsize, endoff, temp;
1737 	int b, c, error, bonuslen;
1738 	dmu_object_info_t *doi = &za->za_doi;
1739 	char osname[MAXNAMELEN];
1740 
1741 	dmu_objset_name(os, osname);
1742 
1743 	endoff = -8ULL;
1744 	batchsize = 2;
1745 
1746 	/*
1747 	 * Create a batch object if necessary, and record it in the directory.
1748 	 */
1749 	VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1750 	    sizeof (uint64_t), &batchobj, DMU_READ_PREFETCH));
1751 	if (batchobj == 0) {
1752 		tx = dmu_tx_create(os);
1753 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1754 		    sizeof (uint64_t));
1755 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1756 		error = dmu_tx_assign(tx, TXG_WAIT);
1757 		if (error) {
1758 			ztest_record_enospc("create a batch object");
1759 			dmu_tx_abort(tx);
1760 			return;
1761 		}
1762 		batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1763 		    DMU_OT_NONE, 0, tx);
1764 		ztest_set_random_blocksize(os, batchobj, tx);
1765 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1766 		    sizeof (uint64_t), &batchobj, tx);
1767 		dmu_tx_commit(tx);
1768 	}
1769 
1770 	/*
1771 	 * Destroy the previous batch of objects.
1772 	 */
1773 	for (b = 0; b < batchsize; b++) {
1774 		VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
1775 		    sizeof (uint64_t), &object, DMU_READ_PREFETCH));
1776 		if (object == 0)
1777 			continue;
1778 		/*
1779 		 * Read and validate contents.
1780 		 * We expect the nth byte of the bonus buffer to be n.
1781 		 */
1782 		VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1783 		za->za_dbuf = db;
1784 
1785 		dmu_object_info_from_db(db, doi);
1786 		ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1787 		ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1788 		ASSERT3S(doi->doi_physical_blks, >=, 0);
1789 
1790 		bonuslen = doi->doi_bonus_size;
1791 
1792 		for (c = 0; c < bonuslen; c++) {
1793 			if (((uint8_t *)db->db_data)[c] !=
1794 			    (uint8_t)(c + bonuslen)) {
1795 				fatal(0,
1796 				    "bad bonus: %s, obj %llu, off %d: %u != %u",
1797 				    osname, object, c,
1798 				    ((uint8_t *)db->db_data)[c],
1799 				    (uint8_t)(c + bonuslen));
1800 			}
1801 		}
1802 
1803 		dmu_buf_rele(db, FTAG);
1804 		za->za_dbuf = NULL;
1805 
1806 		/*
1807 		 * We expect the word at endoff to be our object number.
1808 		 */
1809 		VERIFY(0 == dmu_read(os, object, endoff,
1810 		    sizeof (uint64_t), &temp, DMU_READ_PREFETCH));
1811 
1812 		if (temp != object) {
1813 			fatal(0, "bad data in %s, got %llu, expected %llu",
1814 			    osname, temp, object);
1815 		}
1816 
1817 		/*
1818 		 * Destroy old object and clear batch entry.
1819 		 */
1820 		tx = dmu_tx_create(os);
1821 		dmu_tx_hold_write(tx, batchobj,
1822 		    b * sizeof (uint64_t), sizeof (uint64_t));
1823 		dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1824 		error = dmu_tx_assign(tx, TXG_WAIT);
1825 		if (error) {
1826 			ztest_record_enospc("free object");
1827 			dmu_tx_abort(tx);
1828 			return;
1829 		}
1830 		error = dmu_object_free(os, object, tx);
1831 		if (error) {
1832 			fatal(0, "dmu_object_free('%s', %llu) = %d",
1833 			    osname, object, error);
1834 		}
1835 		object = 0;
1836 
1837 		dmu_object_set_checksum(os, batchobj,
1838 		    ztest_random_checksum(), tx);
1839 		dmu_object_set_compress(os, batchobj,
1840 		    ztest_random_compress(), tx);
1841 
1842 		dmu_write(os, batchobj, b * sizeof (uint64_t),
1843 		    sizeof (uint64_t), &object, tx);
1844 
1845 		dmu_tx_commit(tx);
1846 	}
1847 
1848 	/*
1849 	 * Before creating the new batch of objects, generate a bunch of churn.
1850 	 */
1851 	for (b = ztest_random(100); b > 0; b--) {
1852 		tx = dmu_tx_create(os);
1853 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1854 		error = dmu_tx_assign(tx, TXG_WAIT);
1855 		if (error) {
1856 			ztest_record_enospc("churn objects");
1857 			dmu_tx_abort(tx);
1858 			return;
1859 		}
1860 		object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1861 		    DMU_OT_NONE, 0, tx);
1862 		ztest_set_random_blocksize(os, object, tx);
1863 		error = dmu_object_free(os, object, tx);
1864 		if (error) {
1865 			fatal(0, "dmu_object_free('%s', %llu) = %d",
1866 			    osname, object, error);
1867 		}
1868 		dmu_tx_commit(tx);
1869 	}
1870 
1871 	/*
1872 	 * Create a new batch of objects with randomly chosen
1873 	 * blocksizes and record them in the batch directory.
1874 	 */
1875 	for (b = 0; b < batchsize; b++) {
1876 		uint32_t va_blksize;
1877 		u_longlong_t va_nblocks;
1878 
1879 		tx = dmu_tx_create(os);
1880 		dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1881 		    sizeof (uint64_t));
1882 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1883 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1884 		    sizeof (uint64_t));
1885 		error = dmu_tx_assign(tx, TXG_WAIT);
1886 		if (error) {
1887 			ztest_record_enospc("create batchobj");
1888 			dmu_tx_abort(tx);
1889 			return;
1890 		}
1891 		bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1892 
1893 		object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1894 		    DMU_OT_PLAIN_OTHER, bonuslen, tx);
1895 
1896 		ztest_set_random_blocksize(os, object, tx);
1897 
1898 		dmu_object_set_checksum(os, object,
1899 		    ztest_random_checksum(), tx);
1900 		dmu_object_set_compress(os, object,
1901 		    ztest_random_compress(), tx);
1902 
1903 		dmu_write(os, batchobj, b * sizeof (uint64_t),
1904 		    sizeof (uint64_t), &object, tx);
1905 
1906 		/*
1907 		 * Write to both the bonus buffer and the regular data.
1908 		 */
1909 		VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1910 		za->za_dbuf = db;
1911 		ASSERT3U(bonuslen, <=, db->db_size);
1912 
1913 		dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1914 		ASSERT3S(va_nblocks, >=, 0);
1915 
1916 		dmu_buf_will_dirty(db, tx);
1917 
1918 		/*
1919 		 * See comments above regarding the contents of
1920 		 * the bonus buffer and the word at endoff.
1921 		 */
1922 		for (c = 0; c < bonuslen; c++)
1923 			((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1924 
1925 		dmu_buf_rele(db, FTAG);
1926 		za->za_dbuf = NULL;
1927 
1928 		/*
1929 		 * Write to a large offset to increase indirection.
1930 		 */
1931 		dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1932 
1933 		dmu_tx_commit(tx);
1934 	}
1935 }
1936 
1937 /*
1938  * Verify that dmu_{read,write} work as expected.
1939  */
1940 typedef struct bufwad {
1941 	uint64_t	bw_index;
1942 	uint64_t	bw_txg;
1943 	uint64_t	bw_data;
1944 } bufwad_t;
1945 
1946 typedef struct dmu_read_write_dir {
1947 	uint64_t	dd_packobj;
1948 	uint64_t	dd_bigobj;
1949 	uint64_t	dd_chunk;
1950 } dmu_read_write_dir_t;
1951 
1952 void
1953 ztest_dmu_read_write(ztest_args_t *za)
1954 {
1955 	objset_t *os = za->za_os;
1956 	dmu_read_write_dir_t dd;
1957 	dmu_tx_t *tx;
1958 	int i, freeit, error;
1959 	uint64_t n, s, txg;
1960 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1961 	uint64_t packoff, packsize, bigoff, bigsize;
1962 	uint64_t regions = 997;
1963 	uint64_t stride = 123456789ULL;
1964 	uint64_t width = 40;
1965 	int free_percent = 5;
1966 
1967 	/*
1968 	 * This test uses two objects, packobj and bigobj, that are always
1969 	 * updated together (i.e. in the same tx) so that their contents are
1970 	 * in sync and can be compared.  Their contents relate to each other
1971 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
1972 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
1973 	 * for any index n, there are three bufwads that should be identical:
1974 	 *
1975 	 *	packobj, at offset n * sizeof (bufwad_t)
1976 	 *	bigobj, at the head of the nth chunk
1977 	 *	bigobj, at the tail of the nth chunk
1978 	 *
1979 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
1980 	 * and it doesn't have any relation to the object blocksize.
1981 	 * The only requirement is that it can hold at least two bufwads.
1982 	 *
1983 	 * Normally, we write the bufwad to each of these locations.
1984 	 * However, free_percent of the time we instead write zeroes to
1985 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
1986 	 * bigobj to packobj, we can verify that the DMU is correctly
1987 	 * tracking which parts of an object are allocated and free,
1988 	 * and that the contents of the allocated blocks are correct.
1989 	 */
1990 
1991 	/*
1992 	 * Read the directory info.  If it's the first time, set things up.
1993 	 */
1994 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1995 	    sizeof (dd), &dd, DMU_READ_PREFETCH));
1996 	if (dd.dd_chunk == 0) {
1997 		ASSERT(dd.dd_packobj == 0);
1998 		ASSERT(dd.dd_bigobj == 0);
1999 		tx = dmu_tx_create(os);
2000 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
2001 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2002 		error = dmu_tx_assign(tx, TXG_WAIT);
2003 		if (error) {
2004 			ztest_record_enospc("create r/w directory");
2005 			dmu_tx_abort(tx);
2006 			return;
2007 		}
2008 
2009 		dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
2010 		    DMU_OT_NONE, 0, tx);
2011 		dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
2012 		    DMU_OT_NONE, 0, tx);
2013 		dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
2014 
2015 		ztest_set_random_blocksize(os, dd.dd_packobj, tx);
2016 		ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
2017 
2018 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
2019 		    tx);
2020 		dmu_tx_commit(tx);
2021 	}
2022 
2023 	/*
2024 	 * Prefetch a random chunk of the big object.
2025 	 * Our aim here is to get some async reads in flight
2026 	 * for blocks that we may free below; the DMU should
2027 	 * handle this race correctly.
2028 	 */
2029 	n = ztest_random(regions) * stride + ztest_random(width);
2030 	s = 1 + ztest_random(2 * width - 1);
2031 	dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
2032 
2033 	/*
2034 	 * Pick a random index and compute the offsets into packobj and bigobj.
2035 	 */
2036 	n = ztest_random(regions) * stride + ztest_random(width);
2037 	s = 1 + ztest_random(width - 1);
2038 
2039 	packoff = n * sizeof (bufwad_t);
2040 	packsize = s * sizeof (bufwad_t);
2041 
2042 	bigoff = n * dd.dd_chunk;
2043 	bigsize = s * dd.dd_chunk;
2044 
2045 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
2046 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
2047 
2048 	/*
2049 	 * free_percent of the time, free a range of bigobj rather than
2050 	 * overwriting it.
2051 	 */
2052 	freeit = (ztest_random(100) < free_percent);
2053 
2054 	/*
2055 	 * Read the current contents of our objects.
2056 	 */
2057 	error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf,
2058 	    DMU_READ_PREFETCH);
2059 	ASSERT3U(error, ==, 0);
2060 	error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf,
2061 	    DMU_READ_PREFETCH);
2062 	ASSERT3U(error, ==, 0);
2063 
2064 	/*
2065 	 * Get a tx for the mods to both packobj and bigobj.
2066 	 */
2067 	tx = dmu_tx_create(os);
2068 
2069 	dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
2070 
2071 	if (freeit)
2072 		dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
2073 	else
2074 		dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
2075 
2076 	error = dmu_tx_assign(tx, TXG_WAIT);
2077 
2078 	if (error) {
2079 		ztest_record_enospc("dmu r/w range");
2080 		dmu_tx_abort(tx);
2081 		umem_free(packbuf, packsize);
2082 		umem_free(bigbuf, bigsize);
2083 		return;
2084 	}
2085 
2086 	txg = dmu_tx_get_txg(tx);
2087 
2088 	/*
2089 	 * For each index from n to n + s, verify that the existing bufwad
2090 	 * in packobj matches the bufwads at the head and tail of the
2091 	 * corresponding chunk in bigobj.  Then update all three bufwads
2092 	 * with the new values we want to write out.
2093 	 */
2094 	for (i = 0; i < s; i++) {
2095 		/* LINTED */
2096 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
2097 		/* LINTED */
2098 		bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
2099 		/* LINTED */
2100 		bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
2101 
2102 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
2103 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
2104 
2105 		if (pack->bw_txg > txg)
2106 			fatal(0, "future leak: got %llx, open txg is %llx",
2107 			    pack->bw_txg, txg);
2108 
2109 		if (pack->bw_data != 0 && pack->bw_index != n + i)
2110 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
2111 			    pack->bw_index, n, i);
2112 
2113 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
2114 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
2115 
2116 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
2117 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
2118 
2119 		if (freeit) {
2120 			bzero(pack, sizeof (bufwad_t));
2121 		} else {
2122 			pack->bw_index = n + i;
2123 			pack->bw_txg = txg;
2124 			pack->bw_data = 1 + ztest_random(-2ULL);
2125 		}
2126 		*bigH = *pack;
2127 		*bigT = *pack;
2128 	}
2129 
2130 	/*
2131 	 * We've verified all the old bufwads, and made new ones.
2132 	 * Now write them out.
2133 	 */
2134 	dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
2135 
2136 	if (freeit) {
2137 		if (zopt_verbose >= 6) {
2138 			(void) printf("freeing offset %llx size %llx"
2139 			    " txg %llx\n",
2140 			    (u_longlong_t)bigoff,
2141 			    (u_longlong_t)bigsize,
2142 			    (u_longlong_t)txg);
2143 		}
2144 		VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
2145 		    bigsize, tx));
2146 	} else {
2147 		if (zopt_verbose >= 6) {
2148 			(void) printf("writing offset %llx size %llx"
2149 			    " txg %llx\n",
2150 			    (u_longlong_t)bigoff,
2151 			    (u_longlong_t)bigsize,
2152 			    (u_longlong_t)txg);
2153 		}
2154 		dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
2155 	}
2156 
2157 	dmu_tx_commit(tx);
2158 
2159 	/*
2160 	 * Sanity check the stuff we just wrote.
2161 	 */
2162 	{
2163 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
2164 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
2165 
2166 		VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
2167 		    packsize, packcheck, DMU_READ_PREFETCH));
2168 		VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
2169 		    bigsize, bigcheck, DMU_READ_PREFETCH));
2170 
2171 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
2172 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
2173 
2174 		umem_free(packcheck, packsize);
2175 		umem_free(bigcheck, bigsize);
2176 	}
2177 
2178 	umem_free(packbuf, packsize);
2179 	umem_free(bigbuf, bigsize);
2180 }
2181 
2182 void
2183 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
2184     uint64_t bigsize, uint64_t n, dmu_read_write_dir_t dd, uint64_t txg)
2185 {
2186 	uint64_t i;
2187 	bufwad_t *pack;
2188 	bufwad_t *bigH;
2189 	bufwad_t *bigT;
2190 
2191 	/*
2192 	 * For each index from n to n + s, verify that the existing bufwad
2193 	 * in packobj matches the bufwads at the head and tail of the
2194 	 * corresponding chunk in bigobj.  Then update all three bufwads
2195 	 * with the new values we want to write out.
2196 	 */
2197 	for (i = 0; i < s; i++) {
2198 		/* LINTED */
2199 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
2200 		/* LINTED */
2201 		bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
2202 		/* LINTED */
2203 		bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
2204 
2205 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
2206 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
2207 
2208 		if (pack->bw_txg > txg)
2209 			fatal(0, "future leak: got %llx, open txg is %llx",
2210 			    pack->bw_txg, txg);
2211 
2212 		if (pack->bw_data != 0 && pack->bw_index != n + i)
2213 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
2214 			    pack->bw_index, n, i);
2215 
2216 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
2217 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
2218 
2219 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
2220 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
2221 
2222 		pack->bw_index = n + i;
2223 		pack->bw_txg = txg;
2224 		pack->bw_data = 1 + ztest_random(-2ULL);
2225 
2226 		*bigH = *pack;
2227 		*bigT = *pack;
2228 	}
2229 }
2230 
2231 void
2232 ztest_dmu_read_write_zcopy(ztest_args_t *za)
2233 {
2234 	objset_t *os = za->za_os;
2235 	dmu_read_write_dir_t dd;
2236 	dmu_tx_t *tx;
2237 	uint64_t i;
2238 	int error;
2239 	uint64_t n, s, txg;
2240 	bufwad_t *packbuf, *bigbuf;
2241 	uint64_t packoff, packsize, bigoff, bigsize;
2242 	uint64_t regions = 997;
2243 	uint64_t stride = 123456789ULL;
2244 	uint64_t width = 9;
2245 	dmu_buf_t *bonus_db;
2246 	arc_buf_t **bigbuf_arcbufs;
2247 	dmu_object_info_t *doi = &za->za_doi;
2248 
2249 	/*
2250 	 * This test uses two objects, packobj and bigobj, that are always
2251 	 * updated together (i.e. in the same tx) so that their contents are
2252 	 * in sync and can be compared.  Their contents relate to each other
2253 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
2254 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
2255 	 * for any index n, there are three bufwads that should be identical:
2256 	 *
2257 	 *	packobj, at offset n * sizeof (bufwad_t)
2258 	 *	bigobj, at the head of the nth chunk
2259 	 *	bigobj, at the tail of the nth chunk
2260 	 *
2261 	 * The chunk size is set equal to bigobj block size so that
2262 	 * dmu_assign_arcbuf() can be tested for object updates.
2263 	 */
2264 
2265 	/*
2266 	 * Read the directory info.  If it's the first time, set things up.
2267 	 */
2268 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2269 	    sizeof (dd), &dd, DMU_READ_PREFETCH));
2270 	if (dd.dd_chunk == 0) {
2271 		ASSERT(dd.dd_packobj == 0);
2272 		ASSERT(dd.dd_bigobj == 0);
2273 		tx = dmu_tx_create(os);
2274 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
2275 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2276 		error = dmu_tx_assign(tx, TXG_WAIT);
2277 		if (error) {
2278 			ztest_record_enospc("create r/w directory");
2279 			dmu_tx_abort(tx);
2280 			return;
2281 		}
2282 
2283 		dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
2284 		    DMU_OT_NONE, 0, tx);
2285 		dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
2286 		    DMU_OT_NONE, 0, tx);
2287 		ztest_set_random_blocksize(os, dd.dd_packobj, tx);
2288 		ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
2289 
2290 		VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0);
2291 		ASSERT(doi->doi_data_block_size >= 2 * sizeof (bufwad_t));
2292 		ASSERT(ISP2(doi->doi_data_block_size));
2293 		dd.dd_chunk = doi->doi_data_block_size;
2294 
2295 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
2296 		    tx);
2297 		dmu_tx_commit(tx);
2298 	} else {
2299 		VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0);
2300 		VERIFY(ISP2(doi->doi_data_block_size));
2301 		VERIFY(dd.dd_chunk == doi->doi_data_block_size);
2302 		VERIFY(dd.dd_chunk >= 2 * sizeof (bufwad_t));
2303 	}
2304 
2305 	/*
2306 	 * Pick a random index and compute the offsets into packobj and bigobj.
2307 	 */
2308 	n = ztest_random(regions) * stride + ztest_random(width);
2309 	s = 1 + ztest_random(width - 1);
2310 
2311 	packoff = n * sizeof (bufwad_t);
2312 	packsize = s * sizeof (bufwad_t);
2313 
2314 	bigoff = n * dd.dd_chunk;
2315 	bigsize = s * dd.dd_chunk;
2316 
2317 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
2318 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
2319 
2320 	VERIFY(dmu_bonus_hold(os, dd.dd_bigobj, FTAG, &bonus_db) == 0);
2321 
2322 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
2323 
2324 	/*
2325 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
2326 	 * Iteration 1 test zcopy to already referenced dbufs.
2327 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
2328 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
2329 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
2330 	 * Iteration 5 test zcopy when it can't be done.
2331 	 * Iteration 6 one more zcopy write.
2332 	 */
2333 	for (i = 0; i < 7; i++) {
2334 		uint64_t j;
2335 		uint64_t off;
2336 
2337 		/*
2338 		 * In iteration 5 (i == 5) use arcbufs
2339 		 * that don't match bigobj blksz to test
2340 		 * dmu_assign_arcbuf() when it can't directly
2341 		 * assign an arcbuf to a dbuf.
2342 		 */
2343 		for (j = 0; j < s; j++) {
2344 			if (i != 5) {
2345 				bigbuf_arcbufs[j] =
2346 				    dmu_request_arcbuf(bonus_db,
2347 				    dd.dd_chunk);
2348 			} else {
2349 				bigbuf_arcbufs[2 * j] =
2350 				    dmu_request_arcbuf(bonus_db,
2351 				    dd.dd_chunk / 2);
2352 				bigbuf_arcbufs[2 * j + 1] =
2353 				    dmu_request_arcbuf(bonus_db,
2354 				    dd.dd_chunk / 2);
2355 			}
2356 		}
2357 
2358 		/*
2359 		 * Get a tx for the mods to both packobj and bigobj.
2360 		 */
2361 		tx = dmu_tx_create(os);
2362 
2363 		dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
2364 		dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
2365 
2366 		if (ztest_random(100) == 0) {
2367 			error = -1;
2368 		} else {
2369 			error = dmu_tx_assign(tx, TXG_WAIT);
2370 		}
2371 
2372 		if (error) {
2373 			if (error != -1) {
2374 				ztest_record_enospc("dmu r/w range");
2375 			}
2376 			dmu_tx_abort(tx);
2377 			umem_free(packbuf, packsize);
2378 			umem_free(bigbuf, bigsize);
2379 			for (j = 0; j < s; j++) {
2380 				if (i != 5) {
2381 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
2382 				} else {
2383 					dmu_return_arcbuf(
2384 					    bigbuf_arcbufs[2 * j]);
2385 					dmu_return_arcbuf(
2386 					    bigbuf_arcbufs[2 * j + 1]);
2387 				}
2388 			}
2389 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
2390 			dmu_buf_rele(bonus_db, FTAG);
2391 			return;
2392 		}
2393 
2394 		txg = dmu_tx_get_txg(tx);
2395 
2396 		/*
2397 		 * 50% of the time don't read objects in the 1st iteration to
2398 		 * test dmu_assign_arcbuf() for the case when there're no
2399 		 * existing dbufs for the specified offsets.
2400 		 */
2401 		if (i != 0 || ztest_random(2) != 0) {
2402 			error = dmu_read(os, dd.dd_packobj, packoff,
2403 			    packsize, packbuf, DMU_READ_PREFETCH);
2404 			ASSERT3U(error, ==, 0);
2405 			error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize,
2406 			    bigbuf, DMU_READ_PREFETCH);
2407 			ASSERT3U(error, ==, 0);
2408 		}
2409 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
2410 		    n, dd, txg);
2411 
2412 		/*
2413 		 * We've verified all the old bufwads, and made new ones.
2414 		 * Now write them out.
2415 		 */
2416 		dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
2417 		if (zopt_verbose >= 6) {
2418 			(void) printf("writing offset %llx size %llx"
2419 			    " txg %llx\n",
2420 			    (u_longlong_t)bigoff,
2421 			    (u_longlong_t)bigsize,
2422 			    (u_longlong_t)txg);
2423 		}
2424 		for (off = bigoff, j = 0; j < s; j++, off += dd.dd_chunk) {
2425 			dmu_buf_t *dbt;
2426 			if (i != 5) {
2427 				bcopy((caddr_t)bigbuf + (off - bigoff),
2428 				    bigbuf_arcbufs[j]->b_data, dd.dd_chunk);
2429 			} else {
2430 				bcopy((caddr_t)bigbuf + (off - bigoff),
2431 				    bigbuf_arcbufs[2 * j]->b_data,
2432 				    dd.dd_chunk / 2);
2433 				bcopy((caddr_t)bigbuf + (off - bigoff) +
2434 				    dd.dd_chunk / 2,
2435 				    bigbuf_arcbufs[2 * j + 1]->b_data,
2436 				    dd.dd_chunk / 2);
2437 			}
2438 
2439 			if (i == 1) {
2440 				VERIFY(dmu_buf_hold(os, dd.dd_bigobj, off,
2441 				    FTAG, &dbt) == 0);
2442 			}
2443 			if (i != 5) {
2444 				dmu_assign_arcbuf(bonus_db, off,
2445 				    bigbuf_arcbufs[j], tx);
2446 			} else {
2447 				dmu_assign_arcbuf(bonus_db, off,
2448 				    bigbuf_arcbufs[2 * j], tx);
2449 				dmu_assign_arcbuf(bonus_db,
2450 				    off + dd.dd_chunk / 2,
2451 				    bigbuf_arcbufs[2 * j + 1], tx);
2452 			}
2453 			if (i == 1) {
2454 				dmu_buf_rele(dbt, FTAG);
2455 			}
2456 		}
2457 		dmu_tx_commit(tx);
2458 
2459 		/*
2460 		 * Sanity check the stuff we just wrote.
2461 		 */
2462 		{
2463 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
2464 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
2465 
2466 			VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
2467 			    packsize, packcheck, DMU_READ_PREFETCH));
2468 			VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
2469 			    bigsize, bigcheck, DMU_READ_PREFETCH));
2470 
2471 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
2472 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
2473 
2474 			umem_free(packcheck, packsize);
2475 			umem_free(bigcheck, bigsize);
2476 		}
2477 		if (i == 2) {
2478 			txg_wait_open(dmu_objset_pool(os), 0);
2479 		} else if (i == 3) {
2480 			txg_wait_synced(dmu_objset_pool(os), 0);
2481 		}
2482 	}
2483 
2484 	dmu_buf_rele(bonus_db, FTAG);
2485 	umem_free(packbuf, packsize);
2486 	umem_free(bigbuf, bigsize);
2487 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
2488 }
2489 
2490 void
2491 ztest_dmu_check_future_leak(ztest_args_t *za)
2492 {
2493 	objset_t *os = za->za_os;
2494 	dmu_buf_t *db;
2495 	ztest_block_tag_t *bt;
2496 	dmu_object_info_t *doi = &za->za_doi;
2497 
2498 	/*
2499 	 * Make sure that, if there is a write record in the bonus buffer
2500 	 * of the ZTEST_DIROBJ, that the txg for this record is <= the
2501 	 * last synced txg of the pool.
2502 	 */
2503 	VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2504 	za->za_dbuf = db;
2505 	VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
2506 	ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
2507 	ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2508 	ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
2509 	bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
2510 	if (bt->bt_objset != 0) {
2511 		ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
2512 		ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
2513 		ASSERT3U(bt->bt_offset, ==, -1ULL);
2514 		ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
2515 	}
2516 	dmu_buf_rele(db, FTAG);
2517 	za->za_dbuf = NULL;
2518 }
2519 
2520 void
2521 ztest_dmu_write_parallel(ztest_args_t *za)
2522 {
2523 	objset_t *os = za->za_os;
2524 	ztest_block_tag_t *rbt = &za->za_rbt;
2525 	ztest_block_tag_t *wbt = &za->za_wbt;
2526 	const size_t btsize = sizeof (ztest_block_tag_t);
2527 	dmu_buf_t *db;
2528 	int b, error;
2529 	int bs = ZTEST_DIROBJ_BLOCKSIZE;
2530 	int do_free = 0;
2531 	uint64_t off, txg, txg_how;
2532 	mutex_t *lp;
2533 	char osname[MAXNAMELEN];
2534 	char iobuf[SPA_MAXBLOCKSIZE];
2535 	blkptr_t blk = { 0 };
2536 	uint64_t blkoff;
2537 	zbookmark_t zb;
2538 	dmu_tx_t *tx = dmu_tx_create(os);
2539 	dmu_buf_t *bonus_db;
2540 	arc_buf_t *abuf = NULL;
2541 
2542 	dmu_objset_name(os, osname);
2543 
2544 	/*
2545 	 * Have multiple threads write to large offsets in ZTEST_DIROBJ
2546 	 * to verify that having multiple threads writing to the same object
2547 	 * in parallel doesn't cause any trouble.
2548 	 */
2549 	if (ztest_random(4) == 0) {
2550 		/*
2551 		 * Do the bonus buffer instead of a regular block.
2552 		 * We need a lock to serialize resize vs. others,
2553 		 * so we hash on the objset ID.
2554 		 */
2555 		b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
2556 		off = -1ULL;
2557 		dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
2558 	} else {
2559 		b = ztest_random(ZTEST_SYNC_LOCKS);
2560 		off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
2561 		if (ztest_random(4) == 0) {
2562 			do_free = 1;
2563 			dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
2564 		} else {
2565 			dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
2566 		}
2567 	}
2568 
2569 	if (off != -1ULL && P2PHASE(off, bs) == 0 && !do_free &&
2570 	    ztest_random(8) == 0) {
2571 		VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &bonus_db) == 0);
2572 		abuf = dmu_request_arcbuf(bonus_db, bs);
2573 	}
2574 
2575 	txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
2576 	error = dmu_tx_assign(tx, txg_how);
2577 	if (error) {
2578 		if (error == ERESTART) {
2579 			ASSERT(txg_how == TXG_NOWAIT);
2580 			dmu_tx_wait(tx);
2581 		} else {
2582 			ztest_record_enospc("dmu write parallel");
2583 		}
2584 		dmu_tx_abort(tx);
2585 		if (abuf != NULL) {
2586 			dmu_return_arcbuf(abuf);
2587 			dmu_buf_rele(bonus_db, FTAG);
2588 		}
2589 		return;
2590 	}
2591 	txg = dmu_tx_get_txg(tx);
2592 
2593 	lp = &ztest_shared->zs_sync_lock[b];
2594 	(void) mutex_lock(lp);
2595 
2596 	wbt->bt_objset = dmu_objset_id(os);
2597 	wbt->bt_object = ZTEST_DIROBJ;
2598 	wbt->bt_offset = off;
2599 	wbt->bt_txg = txg;
2600 	wbt->bt_thread = za->za_instance;
2601 	wbt->bt_seq = ztest_shared->zs_seq[b]++;	/* protected by lp */
2602 
2603 	/*
2604 	 * Occasionally, write an all-zero block to test the behavior
2605 	 * of blocks that compress into holes.
2606 	 */
2607 	if (off != -1ULL && ztest_random(8) == 0)
2608 		bzero(wbt, btsize);
2609 
2610 	if (off == -1ULL) {
2611 		dmu_object_info_t *doi = &za->za_doi;
2612 		char *dboff;
2613 
2614 		VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2615 		za->za_dbuf = db;
2616 		dmu_object_info_from_db(db, doi);
2617 		ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2618 		ASSERT3U(doi->doi_bonus_size, >=, btsize);
2619 		ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2620 		dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2621 		bcopy(dboff, rbt, btsize);
2622 		if (rbt->bt_objset != 0) {
2623 			ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2624 			ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2625 			ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2626 			ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2627 		}
2628 		if (ztest_random(10) == 0) {
2629 			int newsize = (ztest_random(db->db_size /
2630 			    btsize) + 1) * btsize;
2631 
2632 			ASSERT3U(newsize, >=, btsize);
2633 			ASSERT3U(newsize, <=, db->db_size);
2634 			VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2635 			dboff = (char *)db->db_data + newsize - btsize;
2636 		}
2637 		dmu_buf_will_dirty(db, tx);
2638 		bcopy(wbt, dboff, btsize);
2639 		dmu_buf_rele(db, FTAG);
2640 		za->za_dbuf = NULL;
2641 	} else if (do_free) {
2642 		VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
2643 	} else if (abuf == NULL) {
2644 		dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
2645 	} else {
2646 		bcopy(wbt, abuf->b_data, btsize);
2647 		dmu_assign_arcbuf(bonus_db, off, abuf, tx);
2648 		dmu_buf_rele(bonus_db, FTAG);
2649 	}
2650 
2651 	(void) mutex_unlock(lp);
2652 
2653 	if (ztest_random(1000) == 0)
2654 		(void) poll(NULL, 0, 1); /* open dn_notxholds window */
2655 
2656 	dmu_tx_commit(tx);
2657 
2658 	if (ztest_random(10000) == 0)
2659 		txg_wait_synced(dmu_objset_pool(os), txg);
2660 
2661 	if (off == -1ULL || do_free)
2662 		return;
2663 
2664 	if (ztest_random(2) != 0)
2665 		return;
2666 
2667 	/*
2668 	 * dmu_sync() the block we just wrote.
2669 	 */
2670 	(void) mutex_lock(lp);
2671 
2672 	blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2673 	error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2674 	za->za_dbuf = db;
2675 	if (error) {
2676 		(void) mutex_unlock(lp);
2677 		return;
2678 	}
2679 	blkoff = off - blkoff;
2680 	error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
2681 	dmu_buf_rele(db, FTAG);
2682 	za->za_dbuf = NULL;
2683 
2684 	if (error) {
2685 		(void) mutex_unlock(lp);
2686 		return;
2687 	}
2688 
2689 	if (blk.blk_birth == 0)	{	/* concurrent free */
2690 		(void) mutex_unlock(lp);
2691 		return;
2692 	}
2693 
2694 	txg_suspend(dmu_objset_pool(os));
2695 
2696 	(void) mutex_unlock(lp);
2697 
2698 	ASSERT(blk.blk_fill == 1);
2699 	ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2700 	ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2701 	ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2702 
2703 	/*
2704 	 * Read the block that dmu_sync() returned to make sure its contents
2705 	 * match what we wrote.  We do this while still txg_suspend()ed
2706 	 * to ensure that the block can't be reused before we read it.
2707 	 */
2708 	zb.zb_objset = dmu_objset_id(os);
2709 	zb.zb_object = ZTEST_DIROBJ;
2710 	zb.zb_level = 0;
2711 	zb.zb_blkid = off / bs;
2712 	error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2713 	    NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2714 	ASSERT3U(error, ==, 0);
2715 
2716 	txg_resume(dmu_objset_pool(os));
2717 
2718 	bcopy(&iobuf[blkoff], rbt, btsize);
2719 
2720 	if (rbt->bt_objset == 0)		/* concurrent free */
2721 		return;
2722 
2723 	if (wbt->bt_objset == 0)		/* all-zero overwrite */
2724 		return;
2725 
2726 	ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2727 	ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2728 	ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2729 
2730 	/*
2731 	 * The semantic of dmu_sync() is that we always push the most recent
2732 	 * version of the data, so in the face of concurrent updates we may
2733 	 * see a newer version of the block.  That's OK.
2734 	 */
2735 	ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2736 	if (rbt->bt_thread == wbt->bt_thread)
2737 		ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2738 	else
2739 		ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2740 }
2741 
2742 /*
2743  * Verify that zap_{create,destroy,add,remove,update} work as expected.
2744  */
2745 #define	ZTEST_ZAP_MIN_INTS	1
2746 #define	ZTEST_ZAP_MAX_INTS	4
2747 #define	ZTEST_ZAP_MAX_PROPS	1000
2748 
2749 void
2750 ztest_zap(ztest_args_t *za)
2751 {
2752 	objset_t *os = za->za_os;
2753 	uint64_t object;
2754 	uint64_t txg, last_txg;
2755 	uint64_t value[ZTEST_ZAP_MAX_INTS];
2756 	uint64_t zl_ints, zl_intsize, prop;
2757 	int i, ints;
2758 	dmu_tx_t *tx;
2759 	char propname[100], txgname[100];
2760 	int error;
2761 	char osname[MAXNAMELEN];
2762 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2763 
2764 	dmu_objset_name(os, osname);
2765 
2766 	/*
2767 	 * Create a new object if necessary, and record it in the directory.
2768 	 */
2769 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2770 	    sizeof (uint64_t), &object, DMU_READ_PREFETCH));
2771 
2772 	if (object == 0) {
2773 		tx = dmu_tx_create(os);
2774 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2775 		    sizeof (uint64_t));
2776 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2777 		error = dmu_tx_assign(tx, TXG_WAIT);
2778 		if (error) {
2779 			ztest_record_enospc("create zap test obj");
2780 			dmu_tx_abort(tx);
2781 			return;
2782 		}
2783 		object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2784 		if (error) {
2785 			fatal(0, "zap_create('%s', %llu) = %d",
2786 			    osname, object, error);
2787 		}
2788 		ASSERT(object != 0);
2789 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2790 		    sizeof (uint64_t), &object, tx);
2791 		/*
2792 		 * Generate a known hash collision, and verify that
2793 		 * we can lookup and remove both entries.
2794 		 */
2795 		for (i = 0; i < 2; i++) {
2796 			value[i] = i;
2797 			error = zap_add(os, object, hc[i], sizeof (uint64_t),
2798 			    1, &value[i], tx);
2799 			ASSERT3U(error, ==, 0);
2800 		}
2801 		for (i = 0; i < 2; i++) {
2802 			error = zap_add(os, object, hc[i], sizeof (uint64_t),
2803 			    1, &value[i], tx);
2804 			ASSERT3U(error, ==, EEXIST);
2805 			error = zap_length(os, object, hc[i],
2806 			    &zl_intsize, &zl_ints);
2807 			ASSERT3U(error, ==, 0);
2808 			ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2809 			ASSERT3U(zl_ints, ==, 1);
2810 		}
2811 		for (i = 0; i < 2; i++) {
2812 			error = zap_remove(os, object, hc[i], tx);
2813 			ASSERT3U(error, ==, 0);
2814 		}
2815 
2816 		dmu_tx_commit(tx);
2817 	}
2818 
2819 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2820 
2821 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2822 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2823 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2824 	bzero(value, sizeof (value));
2825 	last_txg = 0;
2826 
2827 	/*
2828 	 * If these zap entries already exist, validate their contents.
2829 	 */
2830 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2831 	if (error == 0) {
2832 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2833 		ASSERT3U(zl_ints, ==, 1);
2834 
2835 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2836 		    zl_ints, &last_txg) == 0);
2837 
2838 		VERIFY(zap_length(os, object, propname, &zl_intsize,
2839 		    &zl_ints) == 0);
2840 
2841 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2842 		ASSERT3U(zl_ints, ==, ints);
2843 
2844 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
2845 		    zl_ints, value) == 0);
2846 
2847 		for (i = 0; i < ints; i++) {
2848 			ASSERT3U(value[i], ==, last_txg + object + i);
2849 		}
2850 	} else {
2851 		ASSERT3U(error, ==, ENOENT);
2852 	}
2853 
2854 	/*
2855 	 * Atomically update two entries in our zap object.
2856 	 * The first is named txg_%llu, and contains the txg
2857 	 * in which the property was last updated.  The second
2858 	 * is named prop_%llu, and the nth element of its value
2859 	 * should be txg + object + n.
2860 	 */
2861 	tx = dmu_tx_create(os);
2862 	dmu_tx_hold_zap(tx, object, TRUE, NULL);
2863 	error = dmu_tx_assign(tx, TXG_WAIT);
2864 	if (error) {
2865 		ztest_record_enospc("create zap entry");
2866 		dmu_tx_abort(tx);
2867 		return;
2868 	}
2869 	txg = dmu_tx_get_txg(tx);
2870 
2871 	if (last_txg > txg)
2872 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2873 
2874 	for (i = 0; i < ints; i++)
2875 		value[i] = txg + object + i;
2876 
2877 	error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2878 	if (error)
2879 		fatal(0, "zap_update('%s', %llu, '%s') = %d",
2880 		    osname, object, txgname, error);
2881 
2882 	error = zap_update(os, object, propname, sizeof (uint64_t),
2883 	    ints, value, tx);
2884 	if (error)
2885 		fatal(0, "zap_update('%s', %llu, '%s') = %d",
2886 		    osname, object, propname, error);
2887 
2888 	dmu_tx_commit(tx);
2889 
2890 	/*
2891 	 * Remove a random pair of entries.
2892 	 */
2893 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2894 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2895 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2896 
2897 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2898 
2899 	if (error == ENOENT)
2900 		return;
2901 
2902 	ASSERT3U(error, ==, 0);
2903 
2904 	tx = dmu_tx_create(os);
2905 	dmu_tx_hold_zap(tx, object, TRUE, NULL);
2906 	error = dmu_tx_assign(tx, TXG_WAIT);
2907 	if (error) {
2908 		ztest_record_enospc("remove zap entry");
2909 		dmu_tx_abort(tx);
2910 		return;
2911 	}
2912 	error = zap_remove(os, object, txgname, tx);
2913 	if (error)
2914 		fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2915 		    osname, object, txgname, error);
2916 
2917 	error = zap_remove(os, object, propname, tx);
2918 	if (error)
2919 		fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2920 		    osname, object, propname, error);
2921 
2922 	dmu_tx_commit(tx);
2923 
2924 	/*
2925 	 * Once in a while, destroy the object.
2926 	 */
2927 	if (ztest_random(1000) != 0)
2928 		return;
2929 
2930 	tx = dmu_tx_create(os);
2931 	dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2932 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2933 	error = dmu_tx_assign(tx, TXG_WAIT);
2934 	if (error) {
2935 		ztest_record_enospc("destroy zap object");
2936 		dmu_tx_abort(tx);
2937 		return;
2938 	}
2939 	error = zap_destroy(os, object, tx);
2940 	if (error)
2941 		fatal(0, "zap_destroy('%s', %llu) = %d",
2942 		    osname, object, error);
2943 	object = 0;
2944 	dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2945 	    &object, tx);
2946 	dmu_tx_commit(tx);
2947 }
2948 
2949 /*
2950  * Testcase to test the upgrading of a microzap to fatzap.
2951  */
2952 void
2953 ztest_fzap(ztest_args_t *za)
2954 {
2955 	objset_t *os = za->za_os;
2956 	uint64_t object;
2957 	uint64_t value;
2958 	dmu_tx_t *tx;
2959 	int i, error;
2960 	char osname[MAXNAMELEN];
2961 	char *name = "aaa";
2962 	char entname[MAXNAMELEN];
2963 
2964 	dmu_objset_name(os, osname);
2965 
2966 	/*
2967 	 * Create a new object if necessary, and record it in the directory.
2968 	 */
2969 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2970 	    sizeof (uint64_t), &object, DMU_READ_PREFETCH));
2971 
2972 	if (object == 0) {
2973 		tx = dmu_tx_create(os);
2974 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2975 		    sizeof (uint64_t));
2976 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2977 		error = dmu_tx_assign(tx, TXG_WAIT);
2978 		if (error) {
2979 			ztest_record_enospc("create zap test obj");
2980 			dmu_tx_abort(tx);
2981 			return;
2982 		}
2983 		object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2984 		if (error) {
2985 			fatal(0, "zap_create('%s', %llu) = %d",
2986 			    osname, object, error);
2987 		}
2988 		ASSERT(object != 0);
2989 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2990 		    sizeof (uint64_t), &object, tx);
2991 		dmu_tx_commit(tx);
2992 	}
2993 
2994 	/*
2995 	 * Add entries to this ZAP amd make sure it spills over
2996 	 * and gets upgraded to a fatzap. Also, since we are adding
2997 	 * 2050 entries we should see ptrtbl growth and leaf-block
2998 	 * split.
2999 	 */
3000 	for (i = 0; i < 2050; i++) {
3001 		(void) snprintf(entname, sizeof (entname), "%s-%d", name, i);
3002 		value = i;
3003 
3004 		tx = dmu_tx_create(os);
3005 		dmu_tx_hold_zap(tx, object, TRUE, entname);
3006 		error = dmu_tx_assign(tx, TXG_WAIT);
3007 
3008 		if (error) {
3009 			ztest_record_enospc("create zap entry");
3010 			dmu_tx_abort(tx);
3011 			return;
3012 		}
3013 		error = zap_add(os, object, entname, sizeof (uint64_t),
3014 		    1, &value, tx);
3015 
3016 		ASSERT(error == 0 || error == EEXIST);
3017 		dmu_tx_commit(tx);
3018 	}
3019 
3020 	/*
3021 	 * Once in a while, destroy the object.
3022 	 */
3023 	if (ztest_random(1000) != 0)
3024 		return;
3025 
3026 	tx = dmu_tx_create(os);
3027 	dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
3028 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
3029 	error = dmu_tx_assign(tx, TXG_WAIT);
3030 	if (error) {
3031 		ztest_record_enospc("destroy zap object");
3032 		dmu_tx_abort(tx);
3033 		return;
3034 	}
3035 	error = zap_destroy(os, object, tx);
3036 	if (error)
3037 		fatal(0, "zap_destroy('%s', %llu) = %d",
3038 		    osname, object, error);
3039 	object = 0;
3040 	dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
3041 	    &object, tx);
3042 	dmu_tx_commit(tx);
3043 }
3044 
3045 void
3046 ztest_zap_parallel(ztest_args_t *za)
3047 {
3048 	objset_t *os = za->za_os;
3049 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3050 	dmu_tx_t *tx;
3051 	int i, namelen, error;
3052 	char name[20], string_value[20];
3053 	void *data;
3054 
3055 	/*
3056 	 * Generate a random name of the form 'xxx.....' where each
3057 	 * x is a random printable character and the dots are dots.
3058 	 * There are 94 such characters, and the name length goes from
3059 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
3060 	 */
3061 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
3062 
3063 	for (i = 0; i < 3; i++)
3064 		name[i] = '!' + ztest_random('~' - '!' + 1);
3065 	for (; i < namelen - 1; i++)
3066 		name[i] = '.';
3067 	name[i] = '\0';
3068 
3069 	if (ztest_random(2) == 0)
3070 		object = ZTEST_MICROZAP_OBJ;
3071 	else
3072 		object = ZTEST_FATZAP_OBJ;
3073 
3074 	if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
3075 		wsize = sizeof (txg);
3076 		wc = 1;
3077 		data = &txg;
3078 	} else {
3079 		wsize = 1;
3080 		wc = namelen;
3081 		data = string_value;
3082 	}
3083 
3084 	count = -1ULL;
3085 	VERIFY(zap_count(os, object, &count) == 0);
3086 	ASSERT(count != -1ULL);
3087 
3088 	/*
3089 	 * Select an operation: length, lookup, add, update, remove.
3090 	 */
3091 	i = ztest_random(5);
3092 
3093 	if (i >= 2) {
3094 		tx = dmu_tx_create(os);
3095 		dmu_tx_hold_zap(tx, object, TRUE, NULL);
3096 		error = dmu_tx_assign(tx, TXG_WAIT);
3097 		if (error) {
3098 			ztest_record_enospc("zap parallel");
3099 			dmu_tx_abort(tx);
3100 			return;
3101 		}
3102 		txg = dmu_tx_get_txg(tx);
3103 		bcopy(name, string_value, namelen);
3104 	} else {
3105 		tx = NULL;
3106 		txg = 0;
3107 		bzero(string_value, namelen);
3108 	}
3109 
3110 	switch (i) {
3111 
3112 	case 0:
3113 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
3114 		if (error == 0) {
3115 			ASSERT3U(wsize, ==, zl_wsize);
3116 			ASSERT3U(wc, ==, zl_wc);
3117 		} else {
3118 			ASSERT3U(error, ==, ENOENT);
3119 		}
3120 		break;
3121 
3122 	case 1:
3123 		error = zap_lookup(os, object, name, wsize, wc, data);
3124 		if (error == 0) {
3125 			if (data == string_value &&
3126 			    bcmp(name, data, namelen) != 0)
3127 				fatal(0, "name '%s' != val '%s' len %d",
3128 				    name, data, namelen);
3129 		} else {
3130 			ASSERT3U(error, ==, ENOENT);
3131 		}
3132 		break;
3133 
3134 	case 2:
3135 		error = zap_add(os, object, name, wsize, wc, data, tx);
3136 		ASSERT(error == 0 || error == EEXIST);
3137 		break;
3138 
3139 	case 3:
3140 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
3141 		break;
3142 
3143 	case 4:
3144 		error = zap_remove(os, object, name, tx);
3145 		ASSERT(error == 0 || error == ENOENT);
3146 		break;
3147 	}
3148 
3149 	if (tx != NULL)
3150 		dmu_tx_commit(tx);
3151 }
3152 
3153 void
3154 ztest_dsl_prop_get_set(ztest_args_t *za)
3155 {
3156 	objset_t *os = za->za_os;
3157 	int i, inherit;
3158 	uint64_t value;
3159 	const char *prop, *valname;
3160 	char setpoint[MAXPATHLEN];
3161 	char osname[MAXNAMELEN];
3162 	int error;
3163 
3164 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
3165 
3166 	dmu_objset_name(os, osname);
3167 
3168 	for (i = 0; i < 2; i++) {
3169 		if (i == 0) {
3170 			prop = "checksum";
3171 			value = ztest_random_checksum();
3172 			inherit = (value == ZIO_CHECKSUM_INHERIT);
3173 		} else {
3174 			prop = "compression";
3175 			value = ztest_random_compress();
3176 			inherit = (value == ZIO_COMPRESS_INHERIT);
3177 		}
3178 
3179 		error = dsl_prop_set(osname, prop, sizeof (value),
3180 		    !inherit, &value);
3181 
3182 		if (error == ENOSPC) {
3183 			ztest_record_enospc("dsl_prop_set");
3184 			break;
3185 		}
3186 
3187 		ASSERT3U(error, ==, 0);
3188 
3189 		VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
3190 		    1, &value, setpoint), ==, 0);
3191 
3192 		if (i == 0)
3193 			valname = zio_checksum_table[value].ci_name;
3194 		else
3195 			valname = zio_compress_table[value].ci_name;
3196 
3197 		if (zopt_verbose >= 6) {
3198 			(void) printf("%s %s = %s for '%s'\n",
3199 			    osname, prop, valname, setpoint);
3200 		}
3201 	}
3202 
3203 	(void) rw_unlock(&ztest_shared->zs_name_lock);
3204 }
3205 
3206 /*
3207  * Inject random faults into the on-disk data.
3208  */
3209 void
3210 ztest_fault_inject(ztest_args_t *za)
3211 {
3212 	int fd;
3213 	uint64_t offset;
3214 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
3215 	uint64_t bad = 0x1990c0ffeedecade;
3216 	uint64_t top, leaf;
3217 	char path0[MAXPATHLEN];
3218 	char pathrand[MAXPATHLEN];
3219 	size_t fsize;
3220 	spa_t *spa = za->za_spa;
3221 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
3222 	int iters = 1000;
3223 	int maxfaults = zopt_maxfaults;
3224 	vdev_t *vd0 = NULL;
3225 	uint64_t guid0 = 0;
3226 
3227 	ASSERT(leaves >= 1);
3228 
3229 	/*
3230 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
3231 	 */
3232 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3233 
3234 	if (ztest_random(2) == 0) {
3235 		/*
3236 		 * Inject errors on a normal data device.
3237 		 */
3238 		top = ztest_random(spa->spa_root_vdev->vdev_children);
3239 		leaf = ztest_random(leaves);
3240 
3241 		/*
3242 		 * Generate paths to the first leaf in this top-level vdev,
3243 		 * and to the random leaf we selected.  We'll induce transient
3244 		 * write failures and random online/offline activity on leaf 0,
3245 		 * and we'll write random garbage to the randomly chosen leaf.
3246 		 */
3247 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
3248 		    zopt_dir, zopt_pool, top * leaves + 0);
3249 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
3250 		    zopt_dir, zopt_pool, top * leaves + leaf);
3251 
3252 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
3253 		if (vd0 != NULL && maxfaults != 1) {
3254 			/*
3255 			 * Make vd0 explicitly claim to be unreadable,
3256 			 * or unwriteable, or reach behind its back
3257 			 * and close the underlying fd.  We can do this if
3258 			 * maxfaults == 0 because we'll fail and reexecute,
3259 			 * and we can do it if maxfaults >= 2 because we'll
3260 			 * have enough redundancy.  If maxfaults == 1, the
3261 			 * combination of this with injection of random data
3262 			 * corruption below exceeds the pool's fault tolerance.
3263 			 */
3264 			vdev_file_t *vf = vd0->vdev_tsd;
3265 
3266 			if (vf != NULL && ztest_random(3) == 0) {
3267 				(void) close(vf->vf_vnode->v_fd);
3268 				vf->vf_vnode->v_fd = -1;
3269 			} else if (ztest_random(2) == 0) {
3270 				vd0->vdev_cant_read = B_TRUE;
3271 			} else {
3272 				vd0->vdev_cant_write = B_TRUE;
3273 			}
3274 			guid0 = vd0->vdev_guid;
3275 		}
3276 	} else {
3277 		/*
3278 		 * Inject errors on an l2cache device.
3279 		 */
3280 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
3281 
3282 		if (sav->sav_count == 0) {
3283 			spa_config_exit(spa, SCL_STATE, FTAG);
3284 			return;
3285 		}
3286 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
3287 		guid0 = vd0->vdev_guid;
3288 		(void) strcpy(path0, vd0->vdev_path);
3289 		(void) strcpy(pathrand, vd0->vdev_path);
3290 
3291 		leaf = 0;
3292 		leaves = 1;
3293 		maxfaults = INT_MAX;	/* no limit on cache devices */
3294 	}
3295 
3296 	spa_config_exit(spa, SCL_STATE, FTAG);
3297 
3298 	if (maxfaults == 0)
3299 		return;
3300 
3301 	/*
3302 	 * If we can tolerate two or more faults, randomly online/offline vd0.
3303 	 */
3304 	if (maxfaults >= 2 && guid0 != 0) {
3305 		if (ztest_random(10) < 6) {
3306 			int flags = (ztest_random(2) == 0 ?
3307 			    ZFS_OFFLINE_TEMPORARY : 0);
3308 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
3309 		} else {
3310 			(void) vdev_online(spa, guid0, 0, NULL);
3311 		}
3312 	}
3313 
3314 	/*
3315 	 * We have at least single-fault tolerance, so inject data corruption.
3316 	 */
3317 	fd = open(pathrand, O_RDWR);
3318 
3319 	if (fd == -1)	/* we hit a gap in the device namespace */
3320 		return;
3321 
3322 	fsize = lseek(fd, 0, SEEK_END);
3323 
3324 	while (--iters != 0) {
3325 		offset = ztest_random(fsize / (leaves << bshift)) *
3326 		    (leaves << bshift) + (leaf << bshift) +
3327 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
3328 
3329 		if (offset >= fsize)
3330 			continue;
3331 
3332 		if (zopt_verbose >= 6)
3333 			(void) printf("injecting bad word into %s,"
3334 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
3335 
3336 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
3337 			fatal(1, "can't inject bad word at 0x%llx in %s",
3338 			    offset, pathrand);
3339 	}
3340 
3341 	(void) close(fd);
3342 }
3343 
3344 /*
3345  * Scrub the pool.
3346  */
3347 void
3348 ztest_scrub(ztest_args_t *za)
3349 {
3350 	spa_t *spa = za->za_spa;
3351 
3352 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
3353 	(void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
3354 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
3355 }
3356 
3357 /*
3358  * Rename the pool to a different name and then rename it back.
3359  */
3360 void
3361 ztest_spa_rename(ztest_args_t *za)
3362 {
3363 	char *oldname, *newname;
3364 	int error;
3365 	spa_t *spa;
3366 
3367 	(void) rw_wrlock(&ztest_shared->zs_name_lock);
3368 
3369 	oldname = za->za_pool;
3370 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
3371 	(void) strcpy(newname, oldname);
3372 	(void) strcat(newname, "_tmp");
3373 
3374 	/*
3375 	 * Do the rename
3376 	 */
3377 	error = spa_rename(oldname, newname);
3378 	if (error)
3379 		fatal(0, "spa_rename('%s', '%s') = %d", oldname,
3380 		    newname, error);
3381 
3382 	/*
3383 	 * Try to open it under the old name, which shouldn't exist
3384 	 */
3385 	error = spa_open(oldname, &spa, FTAG);
3386 	if (error != ENOENT)
3387 		fatal(0, "spa_open('%s') = %d", oldname, error);
3388 
3389 	/*
3390 	 * Open it under the new name and make sure it's still the same spa_t.
3391 	 */
3392 	error = spa_open(newname, &spa, FTAG);
3393 	if (error != 0)
3394 		fatal(0, "spa_open('%s') = %d", newname, error);
3395 
3396 	ASSERT(spa == za->za_spa);
3397 	spa_close(spa, FTAG);
3398 
3399 	/*
3400 	 * Rename it back to the original
3401 	 */
3402 	error = spa_rename(newname, oldname);
3403 	if (error)
3404 		fatal(0, "spa_rename('%s', '%s') = %d", newname,
3405 		    oldname, error);
3406 
3407 	/*
3408 	 * Make sure it can still be opened
3409 	 */
3410 	error = spa_open(oldname, &spa, FTAG);
3411 	if (error != 0)
3412 		fatal(0, "spa_open('%s') = %d", oldname, error);
3413 
3414 	ASSERT(spa == za->za_spa);
3415 	spa_close(spa, FTAG);
3416 
3417 	umem_free(newname, strlen(newname) + 1);
3418 
3419 	(void) rw_unlock(&ztest_shared->zs_name_lock);
3420 }
3421 
3422 
3423 /*
3424  * Completely obliterate one disk.
3425  */
3426 static void
3427 ztest_obliterate_one_disk(uint64_t vdev)
3428 {
3429 	int fd;
3430 	char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
3431 	size_t fsize;
3432 
3433 	if (zopt_maxfaults < 2)
3434 		return;
3435 
3436 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
3437 	(void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
3438 
3439 	fd = open(dev_name, O_RDWR);
3440 
3441 	if (fd == -1)
3442 		fatal(1, "can't open %s", dev_name);
3443 
3444 	/*
3445 	 * Determine the size.
3446 	 */
3447 	fsize = lseek(fd, 0, SEEK_END);
3448 
3449 	(void) close(fd);
3450 
3451 	/*
3452 	 * Rename the old device to dev_name.old (useful for debugging).
3453 	 */
3454 	VERIFY(rename(dev_name, copy_name) == 0);
3455 
3456 	/*
3457 	 * Create a new one.
3458 	 */
3459 	VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
3460 	VERIFY(ftruncate(fd, fsize) == 0);
3461 	(void) close(fd);
3462 }
3463 
3464 static void
3465 ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
3466 {
3467 	char dev_name[MAXPATHLEN];
3468 	nvlist_t *root;
3469 	int error;
3470 	uint64_t guid;
3471 	vdev_t *vd;
3472 
3473 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
3474 
3475 	/*
3476 	 * Build the nvlist describing dev_name.
3477 	 */
3478 	root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
3479 
3480 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3481 	if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
3482 		guid = 0;
3483 	else
3484 		guid = vd->vdev_guid;
3485 	spa_config_exit(spa, SCL_VDEV, FTAG);
3486 	error = spa_vdev_attach(spa, guid, root, B_TRUE);
3487 	if (error != 0 &&
3488 	    error != EBUSY &&
3489 	    error != ENOTSUP &&
3490 	    error != ENODEV &&
3491 	    error != EDOM)
3492 		fatal(0, "spa_vdev_attach(in-place) = %d", error);
3493 
3494 	nvlist_free(root);
3495 }
3496 
3497 static void
3498 ztest_verify_blocks(char *pool)
3499 {
3500 	int status;
3501 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
3502 	char zbuf[1024];
3503 	char *bin;
3504 	char *ztest;
3505 	char *isa;
3506 	int isalen;
3507 	FILE *fp;
3508 
3509 	(void) realpath(getexecname(), zdb);
3510 
3511 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
3512 	bin = strstr(zdb, "/usr/bin/");
3513 	ztest = strstr(bin, "/ztest");
3514 	isa = bin + 8;
3515 	isalen = ztest - isa;
3516 	isa = strdup(isa);
3517 	/* LINTED */
3518 	(void) sprintf(bin,
3519 	    "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s",
3520 	    isalen,
3521 	    isa,
3522 	    zopt_verbose >= 3 ? "s" : "",
3523 	    zopt_verbose >= 4 ? "v" : "",
3524 	    pool);
3525 	free(isa);
3526 
3527 	if (zopt_verbose >= 5)
3528 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
3529 
3530 	fp = popen(zdb, "r");
3531 
3532 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
3533 		if (zopt_verbose >= 3)
3534 			(void) printf("%s", zbuf);
3535 
3536 	status = pclose(fp);
3537 
3538 	if (status == 0)
3539 		return;
3540 
3541 	ztest_dump_core = 0;
3542 	if (WIFEXITED(status))
3543 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
3544 	else
3545 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
3546 }
3547 
3548 static void
3549 ztest_walk_pool_directory(char *header)
3550 {
3551 	spa_t *spa = NULL;
3552 
3553 	if (zopt_verbose >= 6)
3554 		(void) printf("%s\n", header);
3555 
3556 	mutex_enter(&spa_namespace_lock);
3557 	while ((spa = spa_next(spa)) != NULL)
3558 		if (zopt_verbose >= 6)
3559 			(void) printf("\t%s\n", spa_name(spa));
3560 	mutex_exit(&spa_namespace_lock);
3561 }
3562 
3563 static void
3564 ztest_spa_import_export(char *oldname, char *newname)
3565 {
3566 	nvlist_t *config, *newconfig;
3567 	uint64_t pool_guid;
3568 	spa_t *spa;
3569 	int error;
3570 
3571 	if (zopt_verbose >= 4) {
3572 		(void) printf("import/export: old = %s, new = %s\n",
3573 		    oldname, newname);
3574 	}
3575 
3576 	/*
3577 	 * Clean up from previous runs.
3578 	 */
3579 	(void) spa_destroy(newname);
3580 
3581 	/*
3582 	 * Get the pool's configuration and guid.
3583 	 */
3584 	error = spa_open(oldname, &spa, FTAG);
3585 	if (error)
3586 		fatal(0, "spa_open('%s') = %d", oldname, error);
3587 
3588 	/*
3589 	 * Kick off a scrub to tickle scrub/export races.
3590 	 */
3591 	if (ztest_random(2) == 0)
3592 		(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
3593 
3594 	pool_guid = spa_guid(spa);
3595 	spa_close(spa, FTAG);
3596 
3597 	ztest_walk_pool_directory("pools before export");
3598 
3599 	/*
3600 	 * Export it.
3601 	 */
3602 	error = spa_export(oldname, &config, B_FALSE, B_FALSE);
3603 	if (error)
3604 		fatal(0, "spa_export('%s') = %d", oldname, error);
3605 
3606 	ztest_walk_pool_directory("pools after export");
3607 
3608 	/*
3609 	 * Try to import it.
3610 	 */
3611 	newconfig = spa_tryimport(config);
3612 	ASSERT(newconfig != NULL);
3613 	nvlist_free(newconfig);
3614 
3615 	/*
3616 	 * Import it under the new name.
3617 	 */
3618 	error = spa_import(newname, config, NULL);
3619 	if (error)
3620 		fatal(0, "spa_import('%s') = %d", newname, error);
3621 
3622 	ztest_walk_pool_directory("pools after import");
3623 
3624 	/*
3625 	 * Try to import it again -- should fail with EEXIST.
3626 	 */
3627 	error = spa_import(newname, config, NULL);
3628 	if (error != EEXIST)
3629 		fatal(0, "spa_import('%s') twice", newname);
3630 
3631 	/*
3632 	 * Try to import it under a different name -- should fail with EEXIST.
3633 	 */
3634 	error = spa_import(oldname, config, NULL);
3635 	if (error != EEXIST)
3636 		fatal(0, "spa_import('%s') under multiple names", newname);
3637 
3638 	/*
3639 	 * Verify that the pool is no longer visible under the old name.
3640 	 */
3641 	error = spa_open(oldname, &spa, FTAG);
3642 	if (error != ENOENT)
3643 		fatal(0, "spa_open('%s') = %d", newname, error);
3644 
3645 	/*
3646 	 * Verify that we can open and close the pool using the new name.
3647 	 */
3648 	error = spa_open(newname, &spa, FTAG);
3649 	if (error)
3650 		fatal(0, "spa_open('%s') = %d", newname, error);
3651 	ASSERT(pool_guid == spa_guid(spa));
3652 	spa_close(spa, FTAG);
3653 
3654 	nvlist_free(config);
3655 }
3656 
3657 static void
3658 ztest_resume(spa_t *spa)
3659 {
3660 	if (spa_suspended(spa)) {
3661 		spa_vdev_state_enter(spa);
3662 		vdev_clear(spa, NULL);
3663 		(void) spa_vdev_state_exit(spa, NULL, 0);
3664 		(void) zio_resume(spa);
3665 	}
3666 }
3667 
3668 static void *
3669 ztest_resume_thread(void *arg)
3670 {
3671 	spa_t *spa = arg;
3672 
3673 	while (!ztest_exiting) {
3674 		(void) poll(NULL, 0, 1000);
3675 		ztest_resume(spa);
3676 	}
3677 	return (NULL);
3678 }
3679 
3680 static void *
3681 ztest_thread(void *arg)
3682 {
3683 	ztest_args_t *za = arg;
3684 	ztest_shared_t *zs = ztest_shared;
3685 	hrtime_t now, functime;
3686 	ztest_info_t *zi;
3687 	int f, i;
3688 
3689 	while ((now = gethrtime()) < za->za_stop) {
3690 		/*
3691 		 * See if it's time to force a crash.
3692 		 */
3693 		if (now > za->za_kill) {
3694 			zs->zs_alloc = spa_get_alloc(za->za_spa);
3695 			zs->zs_space = spa_get_space(za->za_spa);
3696 			(void) kill(getpid(), SIGKILL);
3697 		}
3698 
3699 		/*
3700 		 * Pick a random function.
3701 		 */
3702 		f = ztest_random(ZTEST_FUNCS);
3703 		zi = &zs->zs_info[f];
3704 
3705 		/*
3706 		 * Decide whether to call it, based on the requested frequency.
3707 		 */
3708 		if (zi->zi_call_target == 0 ||
3709 		    (double)zi->zi_call_total / zi->zi_call_target >
3710 		    (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
3711 			continue;
3712 
3713 		atomic_add_64(&zi->zi_calls, 1);
3714 		atomic_add_64(&zi->zi_call_total, 1);
3715 
3716 		za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3717 		    ZTEST_DIRSIZE;
3718 		za->za_diroff_shared = (1ULL << 63);
3719 
3720 		for (i = 0; i < zi->zi_iters; i++)
3721 			zi->zi_func(za);
3722 
3723 		functime = gethrtime() - now;
3724 
3725 		atomic_add_64(&zi->zi_call_time, functime);
3726 
3727 		if (zopt_verbose >= 4) {
3728 			Dl_info dli;
3729 			(void) dladdr((void *)zi->zi_func, &dli);
3730 			(void) printf("%6.2f sec in %s\n",
3731 			    (double)functime / NANOSEC, dli.dli_sname);
3732 		}
3733 
3734 		/*
3735 		 * If we're getting ENOSPC with some regularity, stop.
3736 		 */
3737 		if (zs->zs_enospc_count > 10)
3738 			break;
3739 	}
3740 
3741 	return (NULL);
3742 }
3743 
3744 /*
3745  * Kick off threads to run tests on all datasets in parallel.
3746  */
3747 static void
3748 ztest_run(char *pool)
3749 {
3750 	int t, d, error;
3751 	ztest_shared_t *zs = ztest_shared;
3752 	ztest_args_t *za;
3753 	spa_t *spa;
3754 	char name[100];
3755 	thread_t resume_tid;
3756 
3757 	ztest_exiting = B_FALSE;
3758 
3759 	(void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3760 	(void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3761 
3762 	for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3763 		(void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3764 
3765 	/*
3766 	 * Destroy one disk before we even start.
3767 	 * It's mirrored, so everything should work just fine.
3768 	 * This makes us exercise fault handling very early in spa_load().
3769 	 */
3770 	ztest_obliterate_one_disk(0);
3771 
3772 	/*
3773 	 * Verify that the sum of the sizes of all blocks in the pool
3774 	 * equals the SPA's allocated space total.
3775 	 */
3776 	ztest_verify_blocks(pool);
3777 
3778 	/*
3779 	 * Kick off a replacement of the disk we just obliterated.
3780 	 */
3781 	kernel_init(FREAD | FWRITE);
3782 	VERIFY(spa_open(pool, &spa, FTAG) == 0);
3783 	ztest_replace_one_disk(spa, 0);
3784 	if (zopt_verbose >= 5)
3785 		show_pool_stats(spa);
3786 	spa_close(spa, FTAG);
3787 	kernel_fini();
3788 
3789 	kernel_init(FREAD | FWRITE);
3790 
3791 	/*
3792 	 * Verify that we can export the pool and reimport it under a
3793 	 * different name.
3794 	 */
3795 	if (ztest_random(2) == 0) {
3796 		(void) snprintf(name, 100, "%s_import", pool);
3797 		ztest_spa_import_export(pool, name);
3798 		ztest_spa_import_export(name, pool);
3799 	}
3800 
3801 	/*
3802 	 * Verify that we can loop over all pools.
3803 	 */
3804 	mutex_enter(&spa_namespace_lock);
3805 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3806 		if (zopt_verbose > 3) {
3807 			(void) printf("spa_next: found %s\n", spa_name(spa));
3808 		}
3809 	}
3810 	mutex_exit(&spa_namespace_lock);
3811 
3812 	/*
3813 	 * Open our pool.
3814 	 */
3815 	VERIFY(spa_open(pool, &spa, FTAG) == 0);
3816 
3817 	/*
3818 	 * We don't expect the pool to suspend unless maxfaults == 0,
3819 	 * in which case ztest_fault_inject() temporarily takes away
3820 	 * the only valid replica.
3821 	 */
3822 	if (zopt_maxfaults == 0)
3823 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
3824 	else
3825 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
3826 
3827 	/*
3828 	 * Create a thread to periodically resume suspended I/O.
3829 	 */
3830 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
3831 	    &resume_tid) == 0);
3832 
3833 	/*
3834 	 * Verify that we can safely inquire about about any object,
3835 	 * whether it's allocated or not.  To make it interesting,
3836 	 * we probe a 5-wide window around each power of two.
3837 	 * This hits all edge cases, including zero and the max.
3838 	 */
3839 	for (t = 0; t < 64; t++) {
3840 		for (d = -5; d <= 5; d++) {
3841 			error = dmu_object_info(spa->spa_meta_objset,
3842 			    (1ULL << t) + d, NULL);
3843 			ASSERT(error == 0 || error == ENOENT ||
3844 			    error == EINVAL);
3845 		}
3846 	}
3847 
3848 	/*
3849 	 * Now kick off all the tests that run in parallel.
3850 	 */
3851 	zs->zs_enospc_count = 0;
3852 
3853 	za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3854 
3855 	if (zopt_verbose >= 4)
3856 		(void) printf("starting main threads...\n");
3857 
3858 	za[0].za_start = gethrtime();
3859 	za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3860 	za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3861 	za[0].za_kill = za[0].za_stop;
3862 	if (ztest_random(100) < zopt_killrate)
3863 		za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3864 
3865 	for (t = 0; t < zopt_threads; t++) {
3866 		d = t % zopt_datasets;
3867 
3868 		(void) strcpy(za[t].za_pool, pool);
3869 		za[t].za_os = za[d].za_os;
3870 		za[t].za_spa = spa;
3871 		za[t].za_zilog = za[d].za_zilog;
3872 		za[t].za_instance = t;
3873 		za[t].za_random = ztest_random(-1ULL);
3874 		za[t].za_start = za[0].za_start;
3875 		za[t].za_stop = za[0].za_stop;
3876 		za[t].za_kill = za[0].za_kill;
3877 
3878 		if (t < zopt_datasets) {
3879 			int test_future = FALSE;
3880 			(void) rw_rdlock(&ztest_shared->zs_name_lock);
3881 			(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3882 			error = dmu_objset_create(name, DMU_OST_OTHER, 0,
3883 			    ztest_create_cb, NULL);
3884 			if (error == EEXIST) {
3885 				test_future = TRUE;
3886 			} else if (error == ENOSPC) {
3887 				zs->zs_enospc_count++;
3888 				(void) rw_unlock(&ztest_shared->zs_name_lock);
3889 				break;
3890 			} else if (error != 0) {
3891 				fatal(0, "dmu_objset_create(%s) = %d",
3892 				    name, error);
3893 			}
3894 			error = dmu_objset_hold(name, FTAG, &za[d].za_os);
3895 			if (error)
3896 				fatal(0, "dmu_objset_open('%s') = %d",
3897 				    name, error);
3898 			(void) rw_unlock(&ztest_shared->zs_name_lock);
3899 			if (test_future)
3900 				ztest_dmu_check_future_leak(&za[t]);
3901 			zil_replay(za[d].za_os, za[d].za_os,
3902 			    ztest_replay_vector);
3903 			za[d].za_zilog = zil_open(za[d].za_os, NULL);
3904 		}
3905 
3906 		VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3907 		    &za[t].za_thread) == 0);
3908 	}
3909 
3910 	while (--t >= 0) {
3911 		VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
3912 		if (t < zopt_datasets) {
3913 			zil_close(za[t].za_zilog);
3914 			dmu_objset_rele(za[t].za_os, FTAG);
3915 		}
3916 	}
3917 
3918 	if (zopt_verbose >= 3)
3919 		show_pool_stats(spa);
3920 
3921 	txg_wait_synced(spa_get_dsl(spa), 0);
3922 
3923 	zs->zs_alloc = spa_get_alloc(spa);
3924 	zs->zs_space = spa_get_space(spa);
3925 
3926 	/*
3927 	 * If we had out-of-space errors, destroy a random objset.
3928 	 */
3929 	if (zs->zs_enospc_count != 0) {
3930 		(void) rw_rdlock(&ztest_shared->zs_name_lock);
3931 		d = (int)ztest_random(zopt_datasets);
3932 		(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3933 		if (zopt_verbose >= 3)
3934 			(void) printf("Destroying %s to free up space\n", name);
3935 
3936 		/* Cleanup any non-standard clones and snapshots */
3937 		ztest_dsl_dataset_cleanup(name, za[d].za_instance);
3938 
3939 		(void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
3940 		    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3941 		(void) rw_unlock(&ztest_shared->zs_name_lock);
3942 	}
3943 
3944 	txg_wait_synced(spa_get_dsl(spa), 0);
3945 
3946 	umem_free(za, zopt_threads * sizeof (ztest_args_t));
3947 
3948 	/* Kill the resume thread */
3949 	ztest_exiting = B_TRUE;
3950 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
3951 	ztest_resume(spa);
3952 
3953 	/*
3954 	 * Right before closing the pool, kick off a bunch of async I/O;
3955 	 * spa_close() should wait for it to complete.
3956 	 */
3957 	for (t = 1; t < 50; t++)
3958 		dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3959 
3960 	spa_close(spa, FTAG);
3961 
3962 	kernel_fini();
3963 }
3964 
3965 void
3966 print_time(hrtime_t t, char *timebuf)
3967 {
3968 	hrtime_t s = t / NANOSEC;
3969 	hrtime_t m = s / 60;
3970 	hrtime_t h = m / 60;
3971 	hrtime_t d = h / 24;
3972 
3973 	s -= m * 60;
3974 	m -= h * 60;
3975 	h -= d * 24;
3976 
3977 	timebuf[0] = '\0';
3978 
3979 	if (d)
3980 		(void) sprintf(timebuf,
3981 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
3982 	else if (h)
3983 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3984 	else if (m)
3985 		(void) sprintf(timebuf, "%llum%02llus", m, s);
3986 	else
3987 		(void) sprintf(timebuf, "%llus", s);
3988 }
3989 
3990 /*
3991  * Create a storage pool with the given name and initial vdev size.
3992  * Then create the specified number of datasets in the pool.
3993  */
3994 static void
3995 ztest_init(char *pool)
3996 {
3997 	spa_t *spa;
3998 	int error;
3999 	nvlist_t *nvroot;
4000 
4001 	kernel_init(FREAD | FWRITE);
4002 
4003 	/*
4004 	 * Create the storage pool.
4005 	 */
4006 	(void) spa_destroy(pool);
4007 	ztest_shared->zs_vdev_primaries = 0;
4008 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
4009 	    0, zopt_raidz, zopt_mirrors, 1);
4010 	error = spa_create(pool, nvroot, NULL, NULL, NULL);
4011 	nvlist_free(nvroot);
4012 
4013 	if (error)
4014 		fatal(0, "spa_create() = %d", error);
4015 	error = spa_open(pool, &spa, FTAG);
4016 	if (error)
4017 		fatal(0, "spa_open() = %d", error);
4018 
4019 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
4020 
4021 	if (zopt_verbose >= 3)
4022 		show_pool_stats(spa);
4023 
4024 	spa_close(spa, FTAG);
4025 
4026 	kernel_fini();
4027 }
4028 
4029 int
4030 main(int argc, char **argv)
4031 {
4032 	int kills = 0;
4033 	int iters = 0;
4034 	int i, f;
4035 	ztest_shared_t *zs;
4036 	ztest_info_t *zi;
4037 	char timebuf[100];
4038 	char numbuf[6];
4039 
4040 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
4041 
4042 	/* Override location of zpool.cache */
4043 	spa_config_path = "/tmp/zpool.cache";
4044 
4045 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
4046 
4047 	process_options(argc, argv);
4048 
4049 	/*
4050 	 * Blow away any existing copy of zpool.cache
4051 	 */
4052 	if (zopt_init != 0)
4053 		(void) remove("/tmp/zpool.cache");
4054 
4055 	zs = ztest_shared = (void *)mmap(0,
4056 	    P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
4057 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
4058 
4059 	if (zopt_verbose >= 1) {
4060 		(void) printf("%llu vdevs, %d datasets, %d threads,"
4061 		    " %llu seconds...\n",
4062 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
4063 		    (u_longlong_t)zopt_time);
4064 	}
4065 
4066 	/*
4067 	 * Create and initialize our storage pool.
4068 	 */
4069 	for (i = 1; i <= zopt_init; i++) {
4070 		bzero(zs, sizeof (ztest_shared_t));
4071 		if (zopt_verbose >= 3 && zopt_init != 1)
4072 			(void) printf("ztest_init(), pass %d\n", i);
4073 		ztest_init(zopt_pool);
4074 	}
4075 
4076 	/*
4077 	 * Initialize the call targets for each function.
4078 	 */
4079 	for (f = 0; f < ZTEST_FUNCS; f++) {
4080 		zi = &zs->zs_info[f];
4081 
4082 		*zi = ztest_info[f];
4083 
4084 		if (*zi->zi_interval == 0)
4085 			zi->zi_call_target = UINT64_MAX;
4086 		else
4087 			zi->zi_call_target = zopt_time / *zi->zi_interval;
4088 	}
4089 
4090 	zs->zs_start_time = gethrtime();
4091 	zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
4092 
4093 	/*
4094 	 * Run the tests in a loop.  These tests include fault injection
4095 	 * to verify that self-healing data works, and forced crashes
4096 	 * to verify that we never lose on-disk consistency.
4097 	 */
4098 	while (gethrtime() < zs->zs_stop_time) {
4099 		int status;
4100 		pid_t pid;
4101 		char *tmp;
4102 
4103 		/*
4104 		 * Initialize the workload counters for each function.
4105 		 */
4106 		for (f = 0; f < ZTEST_FUNCS; f++) {
4107 			zi = &zs->zs_info[f];
4108 			zi->zi_calls = 0;
4109 			zi->zi_call_time = 0;
4110 		}
4111 
4112 		/* Set the allocation switch size */
4113 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
4114 
4115 		pid = fork();
4116 
4117 		if (pid == -1)
4118 			fatal(1, "fork failed");
4119 
4120 		if (pid == 0) {	/* child */
4121 			struct rlimit rl = { 1024, 1024 };
4122 			(void) setrlimit(RLIMIT_NOFILE, &rl);
4123 			(void) enable_extended_FILE_stdio(-1, -1);
4124 			ztest_run(zopt_pool);
4125 			exit(0);
4126 		}
4127 
4128 		while (waitpid(pid, &status, 0) != pid)
4129 			continue;
4130 
4131 		if (WIFEXITED(status)) {
4132 			if (WEXITSTATUS(status) != 0) {
4133 				(void) fprintf(stderr,
4134 				    "child exited with code %d\n",
4135 				    WEXITSTATUS(status));
4136 				exit(2);
4137 			}
4138 		} else if (WIFSIGNALED(status)) {
4139 			if (WTERMSIG(status) != SIGKILL) {
4140 				(void) fprintf(stderr,
4141 				    "child died with signal %d\n",
4142 				    WTERMSIG(status));
4143 				exit(3);
4144 			}
4145 			kills++;
4146 		} else {
4147 			(void) fprintf(stderr, "something strange happened "
4148 			    "to child\n");
4149 			exit(4);
4150 		}
4151 
4152 		iters++;
4153 
4154 		if (zopt_verbose >= 1) {
4155 			hrtime_t now = gethrtime();
4156 
4157 			now = MIN(now, zs->zs_stop_time);
4158 			print_time(zs->zs_stop_time - now, timebuf);
4159 			nicenum(zs->zs_space, numbuf);
4160 
4161 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
4162 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
4163 			    iters,
4164 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
4165 			    (u_longlong_t)zs->zs_enospc_count,
4166 			    100.0 * zs->zs_alloc / zs->zs_space,
4167 			    numbuf,
4168 			    100.0 * (now - zs->zs_start_time) /
4169 			    (zopt_time * NANOSEC), timebuf);
4170 		}
4171 
4172 		if (zopt_verbose >= 2) {
4173 			(void) printf("\nWorkload summary:\n\n");
4174 			(void) printf("%7s %9s   %s\n",
4175 			    "Calls", "Time", "Function");
4176 			(void) printf("%7s %9s   %s\n",
4177 			    "-----", "----", "--------");
4178 			for (f = 0; f < ZTEST_FUNCS; f++) {
4179 				Dl_info dli;
4180 
4181 				zi = &zs->zs_info[f];
4182 				print_time(zi->zi_call_time, timebuf);
4183 				(void) dladdr((void *)zi->zi_func, &dli);
4184 				(void) printf("%7llu %9s   %s\n",
4185 				    (u_longlong_t)zi->zi_calls, timebuf,
4186 				    dli.dli_sname);
4187 			}
4188 			(void) printf("\n");
4189 		}
4190 
4191 		/*
4192 		 * It's possible that we killed a child during a rename test, in
4193 		 * which case we'll have a 'ztest_tmp' pool lying around instead
4194 		 * of 'ztest'.  Do a blind rename in case this happened.
4195 		 */
4196 		tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
4197 		(void) strcpy(tmp, zopt_pool);
4198 		(void) strcat(tmp, "_tmp");
4199 		kernel_init(FREAD | FWRITE);
4200 		(void) spa_rename(tmp, zopt_pool);
4201 		kernel_fini();
4202 		umem_free(tmp, strlen(tmp) + 1);
4203 	}
4204 
4205 	ztest_verify_blocks(zopt_pool);
4206 
4207 	if (zopt_verbose >= 1) {
4208 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
4209 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
4210 	}
4211 
4212 	return (0);
4213 }
4214