xref: /titanic_51/usr/src/cmd/ztest/ztest.c (revision 26f3cdf03f1adcc98f6d3d99843ee71e9229a8c0)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 by Delphix. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 /*
28  * The objective of this program is to provide a DMU/ZAP/SPA stress test
29  * that runs entirely in userland, is easy to use, and easy to extend.
30  *
31  * The overall design of the ztest program is as follows:
32  *
33  * (1) For each major functional area (e.g. adding vdevs to a pool,
34  *     creating and destroying datasets, reading and writing objects, etc)
35  *     we have a simple routine to test that functionality.  These
36  *     individual routines do not have to do anything "stressful".
37  *
38  * (2) We turn these simple functionality tests into a stress test by
39  *     running them all in parallel, with as many threads as desired,
40  *     and spread across as many datasets, objects, and vdevs as desired.
41  *
42  * (3) While all this is happening, we inject faults into the pool to
43  *     verify that self-healing data really works.
44  *
45  * (4) Every time we open a dataset, we change its checksum and compression
46  *     functions.  Thus even individual objects vary from block to block
47  *     in which checksum they use and whether they're compressed.
48  *
49  * (5) To verify that we never lose on-disk consistency after a crash,
50  *     we run the entire test in a child of the main process.
51  *     At random times, the child self-immolates with a SIGKILL.
52  *     This is the software equivalent of pulling the power cord.
53  *     The parent then runs the test again, using the existing
54  *     storage pool, as many times as desired.
55  *
56  * (6) To verify that we don't have future leaks or temporal incursions,
57  *     many of the functional tests record the transaction group number
58  *     as part of their data.  When reading old data, they verify that
59  *     the transaction group number is less than the current, open txg.
60  *     If you add a new test, please do this if applicable.
61  *
62  * When run with no arguments, ztest runs for about five minutes and
63  * produces no output if successful.  To get a little bit of information,
64  * specify -V.  To get more information, specify -VV, and so on.
65  *
66  * To turn this into an overnight stress test, use -T to specify run time.
67  *
68  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
69  * to increase the pool capacity, fanout, and overall stress level.
70  *
71  * The -N(okill) option will suppress kills, so each child runs to completion.
72  * This can be useful when you're trying to distinguish temporal incursions
73  * from plain old race conditions.
74  */
75 
76 #include <sys/zfs_context.h>
77 #include <sys/spa.h>
78 #include <sys/dmu.h>
79 #include <sys/txg.h>
80 #include <sys/dbuf.h>
81 #include <sys/zap.h>
82 #include <sys/dmu_objset.h>
83 #include <sys/poll.h>
84 #include <sys/stat.h>
85 #include <sys/time.h>
86 #include <sys/wait.h>
87 #include <sys/mman.h>
88 #include <sys/resource.h>
89 #include <sys/zio.h>
90 #include <sys/zil.h>
91 #include <sys/zil_impl.h>
92 #include <sys/vdev_impl.h>
93 #include <sys/vdev_file.h>
94 #include <sys/spa_impl.h>
95 #include <sys/metaslab_impl.h>
96 #include <sys/dsl_prop.h>
97 #include <sys/dsl_dataset.h>
98 #include <sys/dsl_scan.h>
99 #include <sys/zio_checksum.h>
100 #include <sys/refcount.h>
101 #include <stdio.h>
102 #include <stdio_ext.h>
103 #include <stdlib.h>
104 #include <unistd.h>
105 #include <signal.h>
106 #include <umem.h>
107 #include <dlfcn.h>
108 #include <ctype.h>
109 #include <math.h>
110 #include <sys/fs/zfs.h>
111 #include <libnvpair.h>
112 
113 static char cmdname[] = "ztest";
114 static char *zopt_pool = cmdname;
115 
116 static uint64_t zopt_vdevs = 5;
117 static uint64_t zopt_vdevtime;
118 static int zopt_ashift = SPA_MINBLOCKSHIFT;
119 static int zopt_mirrors = 2;
120 static int zopt_raidz = 4;
121 static int zopt_raidz_parity = 1;
122 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
123 static int zopt_datasets = 7;
124 static int zopt_threads = 23;
125 static uint64_t zopt_passtime = 60;	/* 60 seconds */
126 static uint64_t zopt_killrate = 70;	/* 70% kill rate */
127 static int zopt_verbose = 0;
128 static int zopt_init = 1;
129 static char *zopt_dir = "/tmp";
130 static uint64_t zopt_time = 300;	/* 5 minutes */
131 static uint64_t zopt_maxloops = 50;	/* max loops during spa_freeze() */
132 
133 #define	BT_MAGIC	0x123456789abcdefULL
134 #define	MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
135 
136 enum ztest_io_type {
137 	ZTEST_IO_WRITE_TAG,
138 	ZTEST_IO_WRITE_PATTERN,
139 	ZTEST_IO_WRITE_ZEROES,
140 	ZTEST_IO_TRUNCATE,
141 	ZTEST_IO_SETATTR,
142 	ZTEST_IO_TYPES
143 };
144 
145 typedef struct ztest_block_tag {
146 	uint64_t	bt_magic;
147 	uint64_t	bt_objset;
148 	uint64_t	bt_object;
149 	uint64_t	bt_offset;
150 	uint64_t	bt_gen;
151 	uint64_t	bt_txg;
152 	uint64_t	bt_crtxg;
153 } ztest_block_tag_t;
154 
155 typedef struct bufwad {
156 	uint64_t	bw_index;
157 	uint64_t	bw_txg;
158 	uint64_t	bw_data;
159 } bufwad_t;
160 
161 /*
162  * XXX -- fix zfs range locks to be generic so we can use them here.
163  */
164 typedef enum {
165 	RL_READER,
166 	RL_WRITER,
167 	RL_APPEND
168 } rl_type_t;
169 
170 typedef struct rll {
171 	void		*rll_writer;
172 	int		rll_readers;
173 	mutex_t		rll_lock;
174 	cond_t		rll_cv;
175 } rll_t;
176 
177 typedef struct rl {
178 	uint64_t	rl_object;
179 	uint64_t	rl_offset;
180 	uint64_t	rl_size;
181 	rll_t		*rl_lock;
182 } rl_t;
183 
184 #define	ZTEST_RANGE_LOCKS	64
185 #define	ZTEST_OBJECT_LOCKS	64
186 
187 /*
188  * Object descriptor.  Used as a template for object lookup/create/remove.
189  */
190 typedef struct ztest_od {
191 	uint64_t	od_dir;
192 	uint64_t	od_object;
193 	dmu_object_type_t od_type;
194 	dmu_object_type_t od_crtype;
195 	uint64_t	od_blocksize;
196 	uint64_t	od_crblocksize;
197 	uint64_t	od_gen;
198 	uint64_t	od_crgen;
199 	char		od_name[MAXNAMELEN];
200 } ztest_od_t;
201 
202 /*
203  * Per-dataset state.
204  */
205 typedef struct ztest_ds {
206 	objset_t	*zd_os;
207 	rwlock_t	zd_zilog_lock;
208 	zilog_t		*zd_zilog;
209 	uint64_t	zd_seq;
210 	ztest_od_t	*zd_od;		/* debugging aid */
211 	char		zd_name[MAXNAMELEN];
212 	mutex_t		zd_dirobj_lock;
213 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
214 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
215 } ztest_ds_t;
216 
217 /*
218  * Per-iteration state.
219  */
220 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
221 
222 typedef struct ztest_info {
223 	ztest_func_t	*zi_func;	/* test function */
224 	uint64_t	zi_iters;	/* iterations per execution */
225 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
226 	uint64_t	zi_call_count;	/* per-pass count */
227 	uint64_t	zi_call_time;	/* per-pass time */
228 	uint64_t	zi_call_next;	/* next time to call this function */
229 } ztest_info_t;
230 
231 /*
232  * Note: these aren't static because we want dladdr() to work.
233  */
234 ztest_func_t ztest_dmu_read_write;
235 ztest_func_t ztest_dmu_write_parallel;
236 ztest_func_t ztest_dmu_object_alloc_free;
237 ztest_func_t ztest_dmu_commit_callbacks;
238 ztest_func_t ztest_zap;
239 ztest_func_t ztest_zap_parallel;
240 ztest_func_t ztest_zil_commit;
241 ztest_func_t ztest_zil_remount;
242 ztest_func_t ztest_dmu_read_write_zcopy;
243 ztest_func_t ztest_dmu_objset_create_destroy;
244 ztest_func_t ztest_dmu_prealloc;
245 ztest_func_t ztest_fzap;
246 ztest_func_t ztest_dmu_snapshot_create_destroy;
247 ztest_func_t ztest_dsl_prop_get_set;
248 ztest_func_t ztest_spa_prop_get_set;
249 ztest_func_t ztest_spa_create_destroy;
250 ztest_func_t ztest_fault_inject;
251 ztest_func_t ztest_ddt_repair;
252 ztest_func_t ztest_dmu_snapshot_hold;
253 ztest_func_t ztest_spa_rename;
254 ztest_func_t ztest_scrub;
255 ztest_func_t ztest_dsl_dataset_promote_busy;
256 ztest_func_t ztest_vdev_attach_detach;
257 ztest_func_t ztest_vdev_LUN_growth;
258 ztest_func_t ztest_vdev_add_remove;
259 ztest_func_t ztest_vdev_aux_add_remove;
260 ztest_func_t ztest_split_pool;
261 ztest_func_t ztest_reguid;
262 
263 uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
264 uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
265 uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
266 uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
267 uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
268 
269 ztest_info_t ztest_info[] = {
270 	{ ztest_dmu_read_write,			1,	&zopt_always	},
271 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
272 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
273 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
274 	{ ztest_zap,				30,	&zopt_always	},
275 	{ ztest_zap_parallel,			100,	&zopt_always	},
276 	{ ztest_split_pool,			1,	&zopt_always	},
277 	{ ztest_zil_commit,			1,	&zopt_incessant	},
278 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
279 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
280 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
281 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
282 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
283 #if 0
284 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
285 #endif
286 	{ ztest_fzap,				1,	&zopt_sometimes	},
287 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
288 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
289 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
290 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
291 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
292 	{ ztest_reguid,				1,	&zopt_sometimes },
293 	{ ztest_spa_rename,			1,	&zopt_rarely	},
294 	{ ztest_scrub,				1,	&zopt_rarely	},
295 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
296 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely },
297 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
298 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime },
299 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
300 };
301 
302 #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
303 
304 /*
305  * The following struct is used to hold a list of uncalled commit callbacks.
306  * The callbacks are ordered by txg number.
307  */
308 typedef struct ztest_cb_list {
309 	mutex_t	zcl_callbacks_lock;
310 	list_t	zcl_callbacks;
311 } ztest_cb_list_t;
312 
313 /*
314  * Stuff we need to share writably between parent and child.
315  */
316 typedef struct ztest_shared {
317 	char		*zs_pool;
318 	spa_t		*zs_spa;
319 	hrtime_t	zs_proc_start;
320 	hrtime_t	zs_proc_stop;
321 	hrtime_t	zs_thread_start;
322 	hrtime_t	zs_thread_stop;
323 	hrtime_t	zs_thread_kill;
324 	uint64_t	zs_enospc_count;
325 	uint64_t	zs_vdev_next_leaf;
326 	uint64_t	zs_vdev_aux;
327 	uint64_t	zs_alloc;
328 	uint64_t	zs_space;
329 	uint64_t	zs_guid;
330 	mutex_t		zs_vdev_lock;
331 	rwlock_t	zs_name_lock;
332 	ztest_info_t	zs_info[ZTEST_FUNCS];
333 	uint64_t	zs_splits;
334 	uint64_t	zs_mirrors;
335 	ztest_ds_t	zs_zd[];
336 } ztest_shared_t;
337 
338 #define	ID_PARALLEL	-1ULL
339 
340 static char ztest_dev_template[] = "%s/%s.%llua";
341 static char ztest_aux_template[] = "%s/%s.%s.%llu";
342 ztest_shared_t *ztest_shared;
343 uint64_t *ztest_seq;
344 
345 static int ztest_random_fd;
346 static int ztest_dump_core = 1;
347 
348 static boolean_t ztest_exiting;
349 
350 /* Global commit callback list */
351 static ztest_cb_list_t zcl;
352 
353 extern uint64_t metaslab_gang_bang;
354 extern uint64_t metaslab_df_alloc_threshold;
355 static uint64_t metaslab_sz;
356 
357 enum ztest_object {
358 	ZTEST_META_DNODE = 0,
359 	ZTEST_DIROBJ,
360 	ZTEST_OBJECTS
361 };
362 
363 static void usage(boolean_t) __NORETURN;
364 
365 /*
366  * These libumem hooks provide a reasonable set of defaults for the allocator's
367  * debugging facilities.
368  */
369 const char *
370 _umem_debug_init()
371 {
372 	return ("default,verbose"); /* $UMEM_DEBUG setting */
373 }
374 
375 const char *
376 _umem_logging_init(void)
377 {
378 	return ("fail,contents"); /* $UMEM_LOGGING setting */
379 }
380 
381 #define	FATAL_MSG_SZ	1024
382 
383 char *fatal_msg;
384 
385 static void
386 fatal(int do_perror, char *message, ...)
387 {
388 	va_list args;
389 	int save_errno = errno;
390 	char buf[FATAL_MSG_SZ];
391 
392 	(void) fflush(stdout);
393 
394 	va_start(args, message);
395 	(void) sprintf(buf, "ztest: ");
396 	/* LINTED */
397 	(void) vsprintf(buf + strlen(buf), message, args);
398 	va_end(args);
399 	if (do_perror) {
400 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
401 		    ": %s", strerror(save_errno));
402 	}
403 	(void) fprintf(stderr, "%s\n", buf);
404 	fatal_msg = buf;			/* to ease debugging */
405 	if (ztest_dump_core)
406 		abort();
407 	exit(3);
408 }
409 
410 static int
411 str2shift(const char *buf)
412 {
413 	const char *ends = "BKMGTPEZ";
414 	int i;
415 
416 	if (buf[0] == '\0')
417 		return (0);
418 	for (i = 0; i < strlen(ends); i++) {
419 		if (toupper(buf[0]) == ends[i])
420 			break;
421 	}
422 	if (i == strlen(ends)) {
423 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
424 		    buf);
425 		usage(B_FALSE);
426 	}
427 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
428 		return (10*i);
429 	}
430 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
431 	usage(B_FALSE);
432 	/* NOTREACHED */
433 }
434 
435 static uint64_t
436 nicenumtoull(const char *buf)
437 {
438 	char *end;
439 	uint64_t val;
440 
441 	val = strtoull(buf, &end, 0);
442 	if (end == buf) {
443 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
444 		usage(B_FALSE);
445 	} else if (end[0] == '.') {
446 		double fval = strtod(buf, &end);
447 		fval *= pow(2, str2shift(end));
448 		if (fval > UINT64_MAX) {
449 			(void) fprintf(stderr, "ztest: value too large: %s\n",
450 			    buf);
451 			usage(B_FALSE);
452 		}
453 		val = (uint64_t)fval;
454 	} else {
455 		int shift = str2shift(end);
456 		if (shift >= 64 || (val << shift) >> shift != val) {
457 			(void) fprintf(stderr, "ztest: value too large: %s\n",
458 			    buf);
459 			usage(B_FALSE);
460 		}
461 		val <<= shift;
462 	}
463 	return (val);
464 }
465 
466 static void
467 usage(boolean_t requested)
468 {
469 	char nice_vdev_size[10];
470 	char nice_gang_bang[10];
471 	FILE *fp = requested ? stdout : stderr;
472 
473 	nicenum(zopt_vdev_size, nice_vdev_size);
474 	nicenum(metaslab_gang_bang, nice_gang_bang);
475 
476 	(void) fprintf(fp, "Usage: %s\n"
477 	    "\t[-v vdevs (default: %llu)]\n"
478 	    "\t[-s size_of_each_vdev (default: %s)]\n"
479 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
480 	    "\t[-m mirror_copies (default: %d)]\n"
481 	    "\t[-r raidz_disks (default: %d)]\n"
482 	    "\t[-R raidz_parity (default: %d)]\n"
483 	    "\t[-d datasets (default: %d)]\n"
484 	    "\t[-t threads (default: %d)]\n"
485 	    "\t[-g gang_block_threshold (default: %s)]\n"
486 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
487 	    "\t[-k kill_percentage (default: %llu%%)]\n"
488 	    "\t[-p pool_name (default: %s)]\n"
489 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
490 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
491 	    "\t[-E] use existing pool instead of creating new one\n"
492 	    "\t[-T time (default: %llu sec)] total run time\n"
493 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
494 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
495 	    "\t[-h] (print help)\n"
496 	    "",
497 	    cmdname,
498 	    (u_longlong_t)zopt_vdevs,			/* -v */
499 	    nice_vdev_size,				/* -s */
500 	    zopt_ashift,				/* -a */
501 	    zopt_mirrors,				/* -m */
502 	    zopt_raidz,					/* -r */
503 	    zopt_raidz_parity,				/* -R */
504 	    zopt_datasets,				/* -d */
505 	    zopt_threads,				/* -t */
506 	    nice_gang_bang,				/* -g */
507 	    zopt_init,					/* -i */
508 	    (u_longlong_t)zopt_killrate,		/* -k */
509 	    zopt_pool,					/* -p */
510 	    zopt_dir,					/* -f */
511 	    (u_longlong_t)zopt_time,			/* -T */
512 	    (u_longlong_t)zopt_maxloops,		/* -F */
513 	    (u_longlong_t)zopt_passtime);		/* -P */
514 	exit(requested ? 0 : 1);
515 }
516 
517 static void
518 process_options(int argc, char **argv)
519 {
520 	int opt;
521 	uint64_t value;
522 
523 	/* By default, test gang blocks for blocks 32K and greater */
524 	metaslab_gang_bang = 32 << 10;
525 
526 	while ((opt = getopt(argc, argv,
527 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
528 		value = 0;
529 		switch (opt) {
530 		case 'v':
531 		case 's':
532 		case 'a':
533 		case 'm':
534 		case 'r':
535 		case 'R':
536 		case 'd':
537 		case 't':
538 		case 'g':
539 		case 'i':
540 		case 'k':
541 		case 'T':
542 		case 'P':
543 		case 'F':
544 			value = nicenumtoull(optarg);
545 		}
546 		switch (opt) {
547 		case 'v':
548 			zopt_vdevs = value;
549 			break;
550 		case 's':
551 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
552 			break;
553 		case 'a':
554 			zopt_ashift = value;
555 			break;
556 		case 'm':
557 			zopt_mirrors = value;
558 			break;
559 		case 'r':
560 			zopt_raidz = MAX(1, value);
561 			break;
562 		case 'R':
563 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
564 			break;
565 		case 'd':
566 			zopt_datasets = MAX(1, value);
567 			break;
568 		case 't':
569 			zopt_threads = MAX(1, value);
570 			break;
571 		case 'g':
572 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
573 			break;
574 		case 'i':
575 			zopt_init = value;
576 			break;
577 		case 'k':
578 			zopt_killrate = value;
579 			break;
580 		case 'p':
581 			zopt_pool = strdup(optarg);
582 			break;
583 		case 'f':
584 			zopt_dir = strdup(optarg);
585 			break;
586 		case 'V':
587 			zopt_verbose++;
588 			break;
589 		case 'E':
590 			zopt_init = 0;
591 			break;
592 		case 'T':
593 			zopt_time = value;
594 			break;
595 		case 'P':
596 			zopt_passtime = MAX(1, value);
597 			break;
598 		case 'F':
599 			zopt_maxloops = MAX(1, value);
600 			break;
601 		case 'h':
602 			usage(B_TRUE);
603 			break;
604 		case '?':
605 		default:
606 			usage(B_FALSE);
607 			break;
608 		}
609 	}
610 
611 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
612 
613 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
614 	    UINT64_MAX >> 2);
615 }
616 
617 static void
618 ztest_kill(ztest_shared_t *zs)
619 {
620 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
621 	zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
622 	(void) kill(getpid(), SIGKILL);
623 }
624 
625 static uint64_t
626 ztest_random(uint64_t range)
627 {
628 	uint64_t r;
629 
630 	if (range == 0)
631 		return (0);
632 
633 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
634 		fatal(1, "short read from /dev/urandom");
635 
636 	return (r % range);
637 }
638 
639 /* ARGSUSED */
640 static void
641 ztest_record_enospc(const char *s)
642 {
643 	ztest_shared->zs_enospc_count++;
644 }
645 
646 static uint64_t
647 ztest_get_ashift(void)
648 {
649 	if (zopt_ashift == 0)
650 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
651 	return (zopt_ashift);
652 }
653 
654 static nvlist_t *
655 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
656 {
657 	char pathbuf[MAXPATHLEN];
658 	uint64_t vdev;
659 	nvlist_t *file;
660 
661 	if (ashift == 0)
662 		ashift = ztest_get_ashift();
663 
664 	if (path == NULL) {
665 		path = pathbuf;
666 
667 		if (aux != NULL) {
668 			vdev = ztest_shared->zs_vdev_aux;
669 			(void) sprintf(path, ztest_aux_template,
670 			    zopt_dir, zopt_pool, aux, vdev);
671 		} else {
672 			vdev = ztest_shared->zs_vdev_next_leaf++;
673 			(void) sprintf(path, ztest_dev_template,
674 			    zopt_dir, zopt_pool, vdev);
675 		}
676 	}
677 
678 	if (size != 0) {
679 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
680 		if (fd == -1)
681 			fatal(1, "can't open %s", path);
682 		if (ftruncate(fd, size) != 0)
683 			fatal(1, "can't ftruncate %s", path);
684 		(void) close(fd);
685 	}
686 
687 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
688 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
689 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
690 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
691 
692 	return (file);
693 }
694 
695 static nvlist_t *
696 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
697 {
698 	nvlist_t *raidz, **child;
699 	int c;
700 
701 	if (r < 2)
702 		return (make_vdev_file(path, aux, size, ashift));
703 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
704 
705 	for (c = 0; c < r; c++)
706 		child[c] = make_vdev_file(path, aux, size, ashift);
707 
708 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
709 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
710 	    VDEV_TYPE_RAIDZ) == 0);
711 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
712 	    zopt_raidz_parity) == 0);
713 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
714 	    child, r) == 0);
715 
716 	for (c = 0; c < r; c++)
717 		nvlist_free(child[c]);
718 
719 	umem_free(child, r * sizeof (nvlist_t *));
720 
721 	return (raidz);
722 }
723 
724 static nvlist_t *
725 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
726 	int r, int m)
727 {
728 	nvlist_t *mirror, **child;
729 	int c;
730 
731 	if (m < 1)
732 		return (make_vdev_raidz(path, aux, size, ashift, r));
733 
734 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
735 
736 	for (c = 0; c < m; c++)
737 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
738 
739 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
740 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
741 	    VDEV_TYPE_MIRROR) == 0);
742 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
743 	    child, m) == 0);
744 
745 	for (c = 0; c < m; c++)
746 		nvlist_free(child[c]);
747 
748 	umem_free(child, m * sizeof (nvlist_t *));
749 
750 	return (mirror);
751 }
752 
753 static nvlist_t *
754 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
755 	int log, int r, int m, int t)
756 {
757 	nvlist_t *root, **child;
758 	int c;
759 
760 	ASSERT(t > 0);
761 
762 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
763 
764 	for (c = 0; c < t; c++) {
765 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
766 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
767 		    log) == 0);
768 	}
769 
770 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
771 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
772 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
773 	    child, t) == 0);
774 
775 	for (c = 0; c < t; c++)
776 		nvlist_free(child[c]);
777 
778 	umem_free(child, t * sizeof (nvlist_t *));
779 
780 	return (root);
781 }
782 
783 static int
784 ztest_random_blocksize(void)
785 {
786 	return (1 << (SPA_MINBLOCKSHIFT +
787 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
788 }
789 
790 static int
791 ztest_random_ibshift(void)
792 {
793 	return (DN_MIN_INDBLKSHIFT +
794 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
795 }
796 
797 static uint64_t
798 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
799 {
800 	uint64_t top;
801 	vdev_t *rvd = spa->spa_root_vdev;
802 	vdev_t *tvd;
803 
804 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
805 
806 	do {
807 		top = ztest_random(rvd->vdev_children);
808 		tvd = rvd->vdev_child[top];
809 	} while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
810 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
811 
812 	return (top);
813 }
814 
815 static uint64_t
816 ztest_random_dsl_prop(zfs_prop_t prop)
817 {
818 	uint64_t value;
819 
820 	do {
821 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
822 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
823 
824 	return (value);
825 }
826 
827 static int
828 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
829     boolean_t inherit)
830 {
831 	const char *propname = zfs_prop_to_name(prop);
832 	const char *valname;
833 	char setpoint[MAXPATHLEN];
834 	uint64_t curval;
835 	int error;
836 
837 	error = dsl_prop_set(osname, propname,
838 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
839 	    sizeof (value), 1, &value);
840 
841 	if (error == ENOSPC) {
842 		ztest_record_enospc(FTAG);
843 		return (error);
844 	}
845 	ASSERT3U(error, ==, 0);
846 
847 	VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
848 	    1, &curval, setpoint), ==, 0);
849 
850 	if (zopt_verbose >= 6) {
851 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
852 		(void) printf("%s %s = %s at '%s'\n",
853 		    osname, propname, valname, setpoint);
854 	}
855 
856 	return (error);
857 }
858 
859 static int
860 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
861 {
862 	spa_t *spa = zs->zs_spa;
863 	nvlist_t *props = NULL;
864 	int error;
865 
866 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
867 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
868 
869 	error = spa_prop_set(spa, props);
870 
871 	nvlist_free(props);
872 
873 	if (error == ENOSPC) {
874 		ztest_record_enospc(FTAG);
875 		return (error);
876 	}
877 	ASSERT3U(error, ==, 0);
878 
879 	return (error);
880 }
881 
882 static void
883 ztest_rll_init(rll_t *rll)
884 {
885 	rll->rll_writer = NULL;
886 	rll->rll_readers = 0;
887 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
888 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
889 }
890 
891 static void
892 ztest_rll_destroy(rll_t *rll)
893 {
894 	ASSERT(rll->rll_writer == NULL);
895 	ASSERT(rll->rll_readers == 0);
896 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
897 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
898 }
899 
900 static void
901 ztest_rll_lock(rll_t *rll, rl_type_t type)
902 {
903 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
904 
905 	if (type == RL_READER) {
906 		while (rll->rll_writer != NULL)
907 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
908 		rll->rll_readers++;
909 	} else {
910 		while (rll->rll_writer != NULL || rll->rll_readers)
911 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
912 		rll->rll_writer = curthread;
913 	}
914 
915 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
916 }
917 
918 static void
919 ztest_rll_unlock(rll_t *rll)
920 {
921 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
922 
923 	if (rll->rll_writer) {
924 		ASSERT(rll->rll_readers == 0);
925 		rll->rll_writer = NULL;
926 	} else {
927 		ASSERT(rll->rll_readers != 0);
928 		ASSERT(rll->rll_writer == NULL);
929 		rll->rll_readers--;
930 	}
931 
932 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
933 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
934 
935 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
936 }
937 
938 static void
939 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
940 {
941 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
942 
943 	ztest_rll_lock(rll, type);
944 }
945 
946 static void
947 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
948 {
949 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
950 
951 	ztest_rll_unlock(rll);
952 }
953 
954 static rl_t *
955 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
956     uint64_t size, rl_type_t type)
957 {
958 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
959 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
960 	rl_t *rl;
961 
962 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
963 	rl->rl_object = object;
964 	rl->rl_offset = offset;
965 	rl->rl_size = size;
966 	rl->rl_lock = rll;
967 
968 	ztest_rll_lock(rll, type);
969 
970 	return (rl);
971 }
972 
973 static void
974 ztest_range_unlock(rl_t *rl)
975 {
976 	rll_t *rll = rl->rl_lock;
977 
978 	ztest_rll_unlock(rll);
979 
980 	umem_free(rl, sizeof (*rl));
981 }
982 
983 static void
984 ztest_zd_init(ztest_ds_t *zd, objset_t *os)
985 {
986 	zd->zd_os = os;
987 	zd->zd_zilog = dmu_objset_zil(os);
988 	zd->zd_seq = 0;
989 	dmu_objset_name(os, zd->zd_name);
990 
991 	VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
992 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
993 
994 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
995 		ztest_rll_init(&zd->zd_object_lock[l]);
996 
997 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
998 		ztest_rll_init(&zd->zd_range_lock[l]);
999 }
1000 
1001 static void
1002 ztest_zd_fini(ztest_ds_t *zd)
1003 {
1004 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
1005 
1006 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1007 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1008 
1009 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1010 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1011 }
1012 
1013 #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1014 
1015 static uint64_t
1016 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1017 {
1018 	uint64_t txg;
1019 	int error;
1020 
1021 	/*
1022 	 * Attempt to assign tx to some transaction group.
1023 	 */
1024 	error = dmu_tx_assign(tx, txg_how);
1025 	if (error) {
1026 		if (error == ERESTART) {
1027 			ASSERT(txg_how == TXG_NOWAIT);
1028 			dmu_tx_wait(tx);
1029 		} else {
1030 			ASSERT3U(error, ==, ENOSPC);
1031 			ztest_record_enospc(tag);
1032 		}
1033 		dmu_tx_abort(tx);
1034 		return (0);
1035 	}
1036 	txg = dmu_tx_get_txg(tx);
1037 	ASSERT(txg != 0);
1038 	return (txg);
1039 }
1040 
1041 static void
1042 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1043 {
1044 	uint64_t *ip = buf;
1045 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1046 
1047 	while (ip < ip_end)
1048 		*ip++ = value;
1049 }
1050 
1051 static boolean_t
1052 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1053 {
1054 	uint64_t *ip = buf;
1055 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1056 	uint64_t diff = 0;
1057 
1058 	while (ip < ip_end)
1059 		diff |= (value - *ip++);
1060 
1061 	return (diff == 0);
1062 }
1063 
1064 static void
1065 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1066     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1067 {
1068 	bt->bt_magic = BT_MAGIC;
1069 	bt->bt_objset = dmu_objset_id(os);
1070 	bt->bt_object = object;
1071 	bt->bt_offset = offset;
1072 	bt->bt_gen = gen;
1073 	bt->bt_txg = txg;
1074 	bt->bt_crtxg = crtxg;
1075 }
1076 
1077 static void
1078 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1079     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1080 {
1081 	ASSERT(bt->bt_magic == BT_MAGIC);
1082 	ASSERT(bt->bt_objset == dmu_objset_id(os));
1083 	ASSERT(bt->bt_object == object);
1084 	ASSERT(bt->bt_offset == offset);
1085 	ASSERT(bt->bt_gen <= gen);
1086 	ASSERT(bt->bt_txg <= txg);
1087 	ASSERT(bt->bt_crtxg == crtxg);
1088 }
1089 
1090 static ztest_block_tag_t *
1091 ztest_bt_bonus(dmu_buf_t *db)
1092 {
1093 	dmu_object_info_t doi;
1094 	ztest_block_tag_t *bt;
1095 
1096 	dmu_object_info_from_db(db, &doi);
1097 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1098 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1099 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1100 
1101 	return (bt);
1102 }
1103 
1104 /*
1105  * ZIL logging ops
1106  */
1107 
1108 #define	lrz_type	lr_mode
1109 #define	lrz_blocksize	lr_uid
1110 #define	lrz_ibshift	lr_gid
1111 #define	lrz_bonustype	lr_rdev
1112 #define	lrz_bonuslen	lr_crtime[1]
1113 
1114 static void
1115 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1116 {
1117 	char *name = (void *)(lr + 1);		/* name follows lr */
1118 	size_t namesize = strlen(name) + 1;
1119 	itx_t *itx;
1120 
1121 	if (zil_replaying(zd->zd_zilog, tx))
1122 		return;
1123 
1124 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1125 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1126 	    sizeof (*lr) + namesize - sizeof (lr_t));
1127 
1128 	zil_itx_assign(zd->zd_zilog, itx, tx);
1129 }
1130 
1131 static void
1132 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1133 {
1134 	char *name = (void *)(lr + 1);		/* name follows lr */
1135 	size_t namesize = strlen(name) + 1;
1136 	itx_t *itx;
1137 
1138 	if (zil_replaying(zd->zd_zilog, tx))
1139 		return;
1140 
1141 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1142 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1143 	    sizeof (*lr) + namesize - sizeof (lr_t));
1144 
1145 	itx->itx_oid = object;
1146 	zil_itx_assign(zd->zd_zilog, itx, tx);
1147 }
1148 
1149 static void
1150 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1151 {
1152 	itx_t *itx;
1153 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1154 
1155 	if (zil_replaying(zd->zd_zilog, tx))
1156 		return;
1157 
1158 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1159 		write_state = WR_INDIRECT;
1160 
1161 	itx = zil_itx_create(TX_WRITE,
1162 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1163 
1164 	if (write_state == WR_COPIED &&
1165 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1166 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1167 		zil_itx_destroy(itx);
1168 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1169 		write_state = WR_NEED_COPY;
1170 	}
1171 	itx->itx_private = zd;
1172 	itx->itx_wr_state = write_state;
1173 	itx->itx_sync = (ztest_random(8) == 0);
1174 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1175 
1176 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1177 	    sizeof (*lr) - sizeof (lr_t));
1178 
1179 	zil_itx_assign(zd->zd_zilog, itx, tx);
1180 }
1181 
1182 static void
1183 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1184 {
1185 	itx_t *itx;
1186 
1187 	if (zil_replaying(zd->zd_zilog, tx))
1188 		return;
1189 
1190 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1191 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1192 	    sizeof (*lr) - sizeof (lr_t));
1193 
1194 	itx->itx_sync = B_FALSE;
1195 	zil_itx_assign(zd->zd_zilog, itx, tx);
1196 }
1197 
1198 static void
1199 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1200 {
1201 	itx_t *itx;
1202 
1203 	if (zil_replaying(zd->zd_zilog, tx))
1204 		return;
1205 
1206 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1207 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1208 	    sizeof (*lr) - sizeof (lr_t));
1209 
1210 	itx->itx_sync = B_FALSE;
1211 	zil_itx_assign(zd->zd_zilog, itx, tx);
1212 }
1213 
1214 /*
1215  * ZIL replay ops
1216  */
1217 static int
1218 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1219 {
1220 	char *name = (void *)(lr + 1);		/* name follows lr */
1221 	objset_t *os = zd->zd_os;
1222 	ztest_block_tag_t *bbt;
1223 	dmu_buf_t *db;
1224 	dmu_tx_t *tx;
1225 	uint64_t txg;
1226 	int error = 0;
1227 
1228 	if (byteswap)
1229 		byteswap_uint64_array(lr, sizeof (*lr));
1230 
1231 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1232 	ASSERT(name[0] != '\0');
1233 
1234 	tx = dmu_tx_create(os);
1235 
1236 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1237 
1238 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1239 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1240 	} else {
1241 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1242 	}
1243 
1244 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1245 	if (txg == 0)
1246 		return (ENOSPC);
1247 
1248 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1249 
1250 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1251 		if (lr->lr_foid == 0) {
1252 			lr->lr_foid = zap_create(os,
1253 			    lr->lrz_type, lr->lrz_bonustype,
1254 			    lr->lrz_bonuslen, tx);
1255 		} else {
1256 			error = zap_create_claim(os, lr->lr_foid,
1257 			    lr->lrz_type, lr->lrz_bonustype,
1258 			    lr->lrz_bonuslen, tx);
1259 		}
1260 	} else {
1261 		if (lr->lr_foid == 0) {
1262 			lr->lr_foid = dmu_object_alloc(os,
1263 			    lr->lrz_type, 0, lr->lrz_bonustype,
1264 			    lr->lrz_bonuslen, tx);
1265 		} else {
1266 			error = dmu_object_claim(os, lr->lr_foid,
1267 			    lr->lrz_type, 0, lr->lrz_bonustype,
1268 			    lr->lrz_bonuslen, tx);
1269 		}
1270 	}
1271 
1272 	if (error) {
1273 		ASSERT3U(error, ==, EEXIST);
1274 		ASSERT(zd->zd_zilog->zl_replay);
1275 		dmu_tx_commit(tx);
1276 		return (error);
1277 	}
1278 
1279 	ASSERT(lr->lr_foid != 0);
1280 
1281 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1282 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1283 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1284 
1285 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1286 	bbt = ztest_bt_bonus(db);
1287 	dmu_buf_will_dirty(db, tx);
1288 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1289 	dmu_buf_rele(db, FTAG);
1290 
1291 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1292 	    &lr->lr_foid, tx));
1293 
1294 	(void) ztest_log_create(zd, tx, lr);
1295 
1296 	dmu_tx_commit(tx);
1297 
1298 	return (0);
1299 }
1300 
1301 static int
1302 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1303 {
1304 	char *name = (void *)(lr + 1);		/* name follows lr */
1305 	objset_t *os = zd->zd_os;
1306 	dmu_object_info_t doi;
1307 	dmu_tx_t *tx;
1308 	uint64_t object, txg;
1309 
1310 	if (byteswap)
1311 		byteswap_uint64_array(lr, sizeof (*lr));
1312 
1313 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1314 	ASSERT(name[0] != '\0');
1315 
1316 	VERIFY3U(0, ==,
1317 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1318 	ASSERT(object != 0);
1319 
1320 	ztest_object_lock(zd, object, RL_WRITER);
1321 
1322 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1323 
1324 	tx = dmu_tx_create(os);
1325 
1326 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1327 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1328 
1329 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1330 	if (txg == 0) {
1331 		ztest_object_unlock(zd, object);
1332 		return (ENOSPC);
1333 	}
1334 
1335 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1336 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1337 	} else {
1338 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1339 	}
1340 
1341 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1342 
1343 	(void) ztest_log_remove(zd, tx, lr, object);
1344 
1345 	dmu_tx_commit(tx);
1346 
1347 	ztest_object_unlock(zd, object);
1348 
1349 	return (0);
1350 }
1351 
1352 static int
1353 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1354 {
1355 	objset_t *os = zd->zd_os;
1356 	void *data = lr + 1;			/* data follows lr */
1357 	uint64_t offset, length;
1358 	ztest_block_tag_t *bt = data;
1359 	ztest_block_tag_t *bbt;
1360 	uint64_t gen, txg, lrtxg, crtxg;
1361 	dmu_object_info_t doi;
1362 	dmu_tx_t *tx;
1363 	dmu_buf_t *db;
1364 	arc_buf_t *abuf = NULL;
1365 	rl_t *rl;
1366 
1367 	if (byteswap)
1368 		byteswap_uint64_array(lr, sizeof (*lr));
1369 
1370 	offset = lr->lr_offset;
1371 	length = lr->lr_length;
1372 
1373 	/* If it's a dmu_sync() block, write the whole block */
1374 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1375 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1376 		if (length < blocksize) {
1377 			offset -= offset % blocksize;
1378 			length = blocksize;
1379 		}
1380 	}
1381 
1382 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1383 		byteswap_uint64_array(bt, sizeof (*bt));
1384 
1385 	if (bt->bt_magic != BT_MAGIC)
1386 		bt = NULL;
1387 
1388 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1389 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1390 
1391 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1392 
1393 	dmu_object_info_from_db(db, &doi);
1394 
1395 	bbt = ztest_bt_bonus(db);
1396 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1397 	gen = bbt->bt_gen;
1398 	crtxg = bbt->bt_crtxg;
1399 	lrtxg = lr->lr_common.lrc_txg;
1400 
1401 	tx = dmu_tx_create(os);
1402 
1403 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1404 
1405 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1406 	    P2PHASE(offset, length) == 0)
1407 		abuf = dmu_request_arcbuf(db, length);
1408 
1409 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1410 	if (txg == 0) {
1411 		if (abuf != NULL)
1412 			dmu_return_arcbuf(abuf);
1413 		dmu_buf_rele(db, FTAG);
1414 		ztest_range_unlock(rl);
1415 		ztest_object_unlock(zd, lr->lr_foid);
1416 		return (ENOSPC);
1417 	}
1418 
1419 	if (bt != NULL) {
1420 		/*
1421 		 * Usually, verify the old data before writing new data --
1422 		 * but not always, because we also want to verify correct
1423 		 * behavior when the data was not recently read into cache.
1424 		 */
1425 		ASSERT(offset % doi.doi_data_block_size == 0);
1426 		if (ztest_random(4) != 0) {
1427 			int prefetch = ztest_random(2) ?
1428 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1429 			ztest_block_tag_t rbt;
1430 
1431 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1432 			    sizeof (rbt), &rbt, prefetch) == 0);
1433 			if (rbt.bt_magic == BT_MAGIC) {
1434 				ztest_bt_verify(&rbt, os, lr->lr_foid,
1435 				    offset, gen, txg, crtxg);
1436 			}
1437 		}
1438 
1439 		/*
1440 		 * Writes can appear to be newer than the bonus buffer because
1441 		 * the ztest_get_data() callback does a dmu_read() of the
1442 		 * open-context data, which may be different than the data
1443 		 * as it was when the write was generated.
1444 		 */
1445 		if (zd->zd_zilog->zl_replay) {
1446 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
1447 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1448 			    bt->bt_crtxg);
1449 		}
1450 
1451 		/*
1452 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1453 		 * so that all of the usual ASSERTs will work.
1454 		 */
1455 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1456 	}
1457 
1458 	if (abuf == NULL) {
1459 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1460 	} else {
1461 		bcopy(data, abuf->b_data, length);
1462 		dmu_assign_arcbuf(db, offset, abuf, tx);
1463 	}
1464 
1465 	(void) ztest_log_write(zd, tx, lr);
1466 
1467 	dmu_buf_rele(db, FTAG);
1468 
1469 	dmu_tx_commit(tx);
1470 
1471 	ztest_range_unlock(rl);
1472 	ztest_object_unlock(zd, lr->lr_foid);
1473 
1474 	return (0);
1475 }
1476 
1477 static int
1478 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1479 {
1480 	objset_t *os = zd->zd_os;
1481 	dmu_tx_t *tx;
1482 	uint64_t txg;
1483 	rl_t *rl;
1484 
1485 	if (byteswap)
1486 		byteswap_uint64_array(lr, sizeof (*lr));
1487 
1488 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1489 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1490 	    RL_WRITER);
1491 
1492 	tx = dmu_tx_create(os);
1493 
1494 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1495 
1496 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1497 	if (txg == 0) {
1498 		ztest_range_unlock(rl);
1499 		ztest_object_unlock(zd, lr->lr_foid);
1500 		return (ENOSPC);
1501 	}
1502 
1503 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1504 	    lr->lr_length, tx) == 0);
1505 
1506 	(void) ztest_log_truncate(zd, tx, lr);
1507 
1508 	dmu_tx_commit(tx);
1509 
1510 	ztest_range_unlock(rl);
1511 	ztest_object_unlock(zd, lr->lr_foid);
1512 
1513 	return (0);
1514 }
1515 
1516 static int
1517 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1518 {
1519 	objset_t *os = zd->zd_os;
1520 	dmu_tx_t *tx;
1521 	dmu_buf_t *db;
1522 	ztest_block_tag_t *bbt;
1523 	uint64_t txg, lrtxg, crtxg;
1524 
1525 	if (byteswap)
1526 		byteswap_uint64_array(lr, sizeof (*lr));
1527 
1528 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1529 
1530 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1531 
1532 	tx = dmu_tx_create(os);
1533 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1534 
1535 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1536 	if (txg == 0) {
1537 		dmu_buf_rele(db, FTAG);
1538 		ztest_object_unlock(zd, lr->lr_foid);
1539 		return (ENOSPC);
1540 	}
1541 
1542 	bbt = ztest_bt_bonus(db);
1543 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1544 	crtxg = bbt->bt_crtxg;
1545 	lrtxg = lr->lr_common.lrc_txg;
1546 
1547 	if (zd->zd_zilog->zl_replay) {
1548 		ASSERT(lr->lr_size != 0);
1549 		ASSERT(lr->lr_mode != 0);
1550 		ASSERT(lrtxg != 0);
1551 	} else {
1552 		/*
1553 		 * Randomly change the size and increment the generation.
1554 		 */
1555 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1556 		    sizeof (*bbt);
1557 		lr->lr_mode = bbt->bt_gen + 1;
1558 		ASSERT(lrtxg == 0);
1559 	}
1560 
1561 	/*
1562 	 * Verify that the current bonus buffer is not newer than our txg.
1563 	 */
1564 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1565 	    MAX(txg, lrtxg), crtxg);
1566 
1567 	dmu_buf_will_dirty(db, tx);
1568 
1569 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1570 	ASSERT3U(lr->lr_size, <=, db->db_size);
1571 	VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1572 	bbt = ztest_bt_bonus(db);
1573 
1574 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1575 
1576 	dmu_buf_rele(db, FTAG);
1577 
1578 	(void) ztest_log_setattr(zd, tx, lr);
1579 
1580 	dmu_tx_commit(tx);
1581 
1582 	ztest_object_unlock(zd, lr->lr_foid);
1583 
1584 	return (0);
1585 }
1586 
1587 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1588 	NULL,			/* 0 no such transaction type */
1589 	ztest_replay_create,	/* TX_CREATE */
1590 	NULL,			/* TX_MKDIR */
1591 	NULL,			/* TX_MKXATTR */
1592 	NULL,			/* TX_SYMLINK */
1593 	ztest_replay_remove,	/* TX_REMOVE */
1594 	NULL,			/* TX_RMDIR */
1595 	NULL,			/* TX_LINK */
1596 	NULL,			/* TX_RENAME */
1597 	ztest_replay_write,	/* TX_WRITE */
1598 	ztest_replay_truncate,	/* TX_TRUNCATE */
1599 	ztest_replay_setattr,	/* TX_SETATTR */
1600 	NULL,			/* TX_ACL */
1601 	NULL,			/* TX_CREATE_ACL */
1602 	NULL,			/* TX_CREATE_ATTR */
1603 	NULL,			/* TX_CREATE_ACL_ATTR */
1604 	NULL,			/* TX_MKDIR_ACL */
1605 	NULL,			/* TX_MKDIR_ATTR */
1606 	NULL,			/* TX_MKDIR_ACL_ATTR */
1607 	NULL,			/* TX_WRITE2 */
1608 };
1609 
1610 /*
1611  * ZIL get_data callbacks
1612  */
1613 
1614 static void
1615 ztest_get_done(zgd_t *zgd, int error)
1616 {
1617 	ztest_ds_t *zd = zgd->zgd_private;
1618 	uint64_t object = zgd->zgd_rl->rl_object;
1619 
1620 	if (zgd->zgd_db)
1621 		dmu_buf_rele(zgd->zgd_db, zgd);
1622 
1623 	ztest_range_unlock(zgd->zgd_rl);
1624 	ztest_object_unlock(zd, object);
1625 
1626 	if (error == 0 && zgd->zgd_bp)
1627 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1628 
1629 	umem_free(zgd, sizeof (*zgd));
1630 }
1631 
1632 static int
1633 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1634 {
1635 	ztest_ds_t *zd = arg;
1636 	objset_t *os = zd->zd_os;
1637 	uint64_t object = lr->lr_foid;
1638 	uint64_t offset = lr->lr_offset;
1639 	uint64_t size = lr->lr_length;
1640 	blkptr_t *bp = &lr->lr_blkptr;
1641 	uint64_t txg = lr->lr_common.lrc_txg;
1642 	uint64_t crtxg;
1643 	dmu_object_info_t doi;
1644 	dmu_buf_t *db;
1645 	zgd_t *zgd;
1646 	int error;
1647 
1648 	ztest_object_lock(zd, object, RL_READER);
1649 	error = dmu_bonus_hold(os, object, FTAG, &db);
1650 	if (error) {
1651 		ztest_object_unlock(zd, object);
1652 		return (error);
1653 	}
1654 
1655 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
1656 
1657 	if (crtxg == 0 || crtxg > txg) {
1658 		dmu_buf_rele(db, FTAG);
1659 		ztest_object_unlock(zd, object);
1660 		return (ENOENT);
1661 	}
1662 
1663 	dmu_object_info_from_db(db, &doi);
1664 	dmu_buf_rele(db, FTAG);
1665 	db = NULL;
1666 
1667 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1668 	zgd->zgd_zilog = zd->zd_zilog;
1669 	zgd->zgd_private = zd;
1670 
1671 	if (buf != NULL) {	/* immediate write */
1672 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1673 		    RL_READER);
1674 
1675 		error = dmu_read(os, object, offset, size, buf,
1676 		    DMU_READ_NO_PREFETCH);
1677 		ASSERT(error == 0);
1678 	} else {
1679 		size = doi.doi_data_block_size;
1680 		if (ISP2(size)) {
1681 			offset = P2ALIGN(offset, size);
1682 		} else {
1683 			ASSERT(offset < size);
1684 			offset = 0;
1685 		}
1686 
1687 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1688 		    RL_READER);
1689 
1690 		error = dmu_buf_hold(os, object, offset, zgd, &db,
1691 		    DMU_READ_NO_PREFETCH);
1692 
1693 		if (error == 0) {
1694 			zgd->zgd_db = db;
1695 			zgd->zgd_bp = bp;
1696 
1697 			ASSERT(db->db_offset == offset);
1698 			ASSERT(db->db_size == size);
1699 
1700 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1701 			    ztest_get_done, zgd);
1702 
1703 			if (error == 0)
1704 				return (0);
1705 		}
1706 	}
1707 
1708 	ztest_get_done(zgd, error);
1709 
1710 	return (error);
1711 }
1712 
1713 static void *
1714 ztest_lr_alloc(size_t lrsize, char *name)
1715 {
1716 	char *lr;
1717 	size_t namesize = name ? strlen(name) + 1 : 0;
1718 
1719 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1720 
1721 	if (name)
1722 		bcopy(name, lr + lrsize, namesize);
1723 
1724 	return (lr);
1725 }
1726 
1727 void
1728 ztest_lr_free(void *lr, size_t lrsize, char *name)
1729 {
1730 	size_t namesize = name ? strlen(name) + 1 : 0;
1731 
1732 	umem_free(lr, lrsize + namesize);
1733 }
1734 
1735 /*
1736  * Lookup a bunch of objects.  Returns the number of objects not found.
1737  */
1738 static int
1739 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1740 {
1741 	int missing = 0;
1742 	int error;
1743 
1744 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1745 
1746 	for (int i = 0; i < count; i++, od++) {
1747 		od->od_object = 0;
1748 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1749 		    sizeof (uint64_t), 1, &od->od_object);
1750 		if (error) {
1751 			ASSERT(error == ENOENT);
1752 			ASSERT(od->od_object == 0);
1753 			missing++;
1754 		} else {
1755 			dmu_buf_t *db;
1756 			ztest_block_tag_t *bbt;
1757 			dmu_object_info_t doi;
1758 
1759 			ASSERT(od->od_object != 0);
1760 			ASSERT(missing == 0);	/* there should be no gaps */
1761 
1762 			ztest_object_lock(zd, od->od_object, RL_READER);
1763 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1764 			    od->od_object, FTAG, &db));
1765 			dmu_object_info_from_db(db, &doi);
1766 			bbt = ztest_bt_bonus(db);
1767 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1768 			od->od_type = doi.doi_type;
1769 			od->od_blocksize = doi.doi_data_block_size;
1770 			od->od_gen = bbt->bt_gen;
1771 			dmu_buf_rele(db, FTAG);
1772 			ztest_object_unlock(zd, od->od_object);
1773 		}
1774 	}
1775 
1776 	return (missing);
1777 }
1778 
1779 static int
1780 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1781 {
1782 	int missing = 0;
1783 
1784 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1785 
1786 	for (int i = 0; i < count; i++, od++) {
1787 		if (missing) {
1788 			od->od_object = 0;
1789 			missing++;
1790 			continue;
1791 		}
1792 
1793 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1794 
1795 		lr->lr_doid = od->od_dir;
1796 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
1797 		lr->lrz_type = od->od_crtype;
1798 		lr->lrz_blocksize = od->od_crblocksize;
1799 		lr->lrz_ibshift = ztest_random_ibshift();
1800 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1801 		lr->lrz_bonuslen = dmu_bonus_max();
1802 		lr->lr_gen = od->od_crgen;
1803 		lr->lr_crtime[0] = time(NULL);
1804 
1805 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1806 			ASSERT(missing == 0);
1807 			od->od_object = 0;
1808 			missing++;
1809 		} else {
1810 			od->od_object = lr->lr_foid;
1811 			od->od_type = od->od_crtype;
1812 			od->od_blocksize = od->od_crblocksize;
1813 			od->od_gen = od->od_crgen;
1814 			ASSERT(od->od_object != 0);
1815 		}
1816 
1817 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
1818 	}
1819 
1820 	return (missing);
1821 }
1822 
1823 static int
1824 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1825 {
1826 	int missing = 0;
1827 	int error;
1828 
1829 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1830 
1831 	od += count - 1;
1832 
1833 	for (int i = count - 1; i >= 0; i--, od--) {
1834 		if (missing) {
1835 			missing++;
1836 			continue;
1837 		}
1838 
1839 		if (od->od_object == 0)
1840 			continue;
1841 
1842 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1843 
1844 		lr->lr_doid = od->od_dir;
1845 
1846 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1847 			ASSERT3U(error, ==, ENOSPC);
1848 			missing++;
1849 		} else {
1850 			od->od_object = 0;
1851 		}
1852 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
1853 	}
1854 
1855 	return (missing);
1856 }
1857 
1858 static int
1859 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1860     void *data)
1861 {
1862 	lr_write_t *lr;
1863 	int error;
1864 
1865 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1866 
1867 	lr->lr_foid = object;
1868 	lr->lr_offset = offset;
1869 	lr->lr_length = size;
1870 	lr->lr_blkoff = 0;
1871 	BP_ZERO(&lr->lr_blkptr);
1872 
1873 	bcopy(data, lr + 1, size);
1874 
1875 	error = ztest_replay_write(zd, lr, B_FALSE);
1876 
1877 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1878 
1879 	return (error);
1880 }
1881 
1882 static int
1883 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1884 {
1885 	lr_truncate_t *lr;
1886 	int error;
1887 
1888 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
1889 
1890 	lr->lr_foid = object;
1891 	lr->lr_offset = offset;
1892 	lr->lr_length = size;
1893 
1894 	error = ztest_replay_truncate(zd, lr, B_FALSE);
1895 
1896 	ztest_lr_free(lr, sizeof (*lr), NULL);
1897 
1898 	return (error);
1899 }
1900 
1901 static int
1902 ztest_setattr(ztest_ds_t *zd, uint64_t object)
1903 {
1904 	lr_setattr_t *lr;
1905 	int error;
1906 
1907 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
1908 
1909 	lr->lr_foid = object;
1910 	lr->lr_size = 0;
1911 	lr->lr_mode = 0;
1912 
1913 	error = ztest_replay_setattr(zd, lr, B_FALSE);
1914 
1915 	ztest_lr_free(lr, sizeof (*lr), NULL);
1916 
1917 	return (error);
1918 }
1919 
1920 static void
1921 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1922 {
1923 	objset_t *os = zd->zd_os;
1924 	dmu_tx_t *tx;
1925 	uint64_t txg;
1926 	rl_t *rl;
1927 
1928 	txg_wait_synced(dmu_objset_pool(os), 0);
1929 
1930 	ztest_object_lock(zd, object, RL_READER);
1931 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1932 
1933 	tx = dmu_tx_create(os);
1934 
1935 	dmu_tx_hold_write(tx, object, offset, size);
1936 
1937 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1938 
1939 	if (txg != 0) {
1940 		dmu_prealloc(os, object, offset, size, tx);
1941 		dmu_tx_commit(tx);
1942 		txg_wait_synced(dmu_objset_pool(os), txg);
1943 	} else {
1944 		(void) dmu_free_long_range(os, object, offset, size);
1945 	}
1946 
1947 	ztest_range_unlock(rl);
1948 	ztest_object_unlock(zd, object);
1949 }
1950 
1951 static void
1952 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1953 {
1954 	ztest_block_tag_t wbt;
1955 	dmu_object_info_t doi;
1956 	enum ztest_io_type io_type;
1957 	uint64_t blocksize;
1958 	void *data;
1959 
1960 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1961 	blocksize = doi.doi_data_block_size;
1962 	data = umem_alloc(blocksize, UMEM_NOFAIL);
1963 
1964 	/*
1965 	 * Pick an i/o type at random, biased toward writing block tags.
1966 	 */
1967 	io_type = ztest_random(ZTEST_IO_TYPES);
1968 	if (ztest_random(2) == 0)
1969 		io_type = ZTEST_IO_WRITE_TAG;
1970 
1971 	(void) rw_rdlock(&zd->zd_zilog_lock);
1972 
1973 	switch (io_type) {
1974 
1975 	case ZTEST_IO_WRITE_TAG:
1976 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
1977 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
1978 		break;
1979 
1980 	case ZTEST_IO_WRITE_PATTERN:
1981 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
1982 		if (ztest_random(2) == 0) {
1983 			/*
1984 			 * Induce fletcher2 collisions to ensure that
1985 			 * zio_ddt_collision() detects and resolves them
1986 			 * when using fletcher2-verify for deduplication.
1987 			 */
1988 			((uint64_t *)data)[0] ^= 1ULL << 63;
1989 			((uint64_t *)data)[4] ^= 1ULL << 63;
1990 		}
1991 		(void) ztest_write(zd, object, offset, blocksize, data);
1992 		break;
1993 
1994 	case ZTEST_IO_WRITE_ZEROES:
1995 		bzero(data, blocksize);
1996 		(void) ztest_write(zd, object, offset, blocksize, data);
1997 		break;
1998 
1999 	case ZTEST_IO_TRUNCATE:
2000 		(void) ztest_truncate(zd, object, offset, blocksize);
2001 		break;
2002 
2003 	case ZTEST_IO_SETATTR:
2004 		(void) ztest_setattr(zd, object);
2005 		break;
2006 	}
2007 
2008 	(void) rw_unlock(&zd->zd_zilog_lock);
2009 
2010 	umem_free(data, blocksize);
2011 }
2012 
2013 /*
2014  * Initialize an object description template.
2015  */
2016 static void
2017 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2018     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2019 {
2020 	od->od_dir = ZTEST_DIROBJ;
2021 	od->od_object = 0;
2022 
2023 	od->od_crtype = type;
2024 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2025 	od->od_crgen = gen;
2026 
2027 	od->od_type = DMU_OT_NONE;
2028 	od->od_blocksize = 0;
2029 	od->od_gen = 0;
2030 
2031 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2032 	    tag, (int64_t)id, index);
2033 }
2034 
2035 /*
2036  * Lookup or create the objects for a test using the od template.
2037  * If the objects do not all exist, or if 'remove' is specified,
2038  * remove any existing objects and create new ones.  Otherwise,
2039  * use the existing objects.
2040  */
2041 static int
2042 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2043 {
2044 	int count = size / sizeof (*od);
2045 	int rv = 0;
2046 
2047 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2048 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2049 	    (ztest_remove(zd, od, count) != 0 ||
2050 	    ztest_create(zd, od, count) != 0))
2051 		rv = -1;
2052 	zd->zd_od = od;
2053 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2054 
2055 	return (rv);
2056 }
2057 
2058 /* ARGSUSED */
2059 void
2060 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2061 {
2062 	zilog_t *zilog = zd->zd_zilog;
2063 
2064 	(void) rw_rdlock(&zd->zd_zilog_lock);
2065 
2066 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2067 
2068 	/*
2069 	 * Remember the committed values in zd, which is in parent/child
2070 	 * shared memory.  If we die, the next iteration of ztest_run()
2071 	 * will verify that the log really does contain this record.
2072 	 */
2073 	mutex_enter(&zilog->zl_lock);
2074 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2075 	zd->zd_seq = zilog->zl_commit_lr_seq;
2076 	mutex_exit(&zilog->zl_lock);
2077 
2078 	(void) rw_unlock(&zd->zd_zilog_lock);
2079 }
2080 
2081 /*
2082  * This function is designed to simulate the operations that occur during a
2083  * mount/unmount operation.  We hold the dataset across these operations in an
2084  * attempt to expose any implicit assumptions about ZIL management.
2085  */
2086 /* ARGSUSED */
2087 void
2088 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2089 {
2090 	objset_t *os = zd->zd_os;
2091 
2092 	(void) rw_wrlock(&zd->zd_zilog_lock);
2093 
2094 	/* zfsvfs_teardown() */
2095 	zil_close(zd->zd_zilog);
2096 
2097 	/* zfsvfs_setup() */
2098 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2099 	zil_replay(os, zd, ztest_replay_vector);
2100 
2101 	(void) rw_unlock(&zd->zd_zilog_lock);
2102 }
2103 
2104 /*
2105  * Verify that we can't destroy an active pool, create an existing pool,
2106  * or create a pool with a bad vdev spec.
2107  */
2108 /* ARGSUSED */
2109 void
2110 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2111 {
2112 	ztest_shared_t *zs = ztest_shared;
2113 	spa_t *spa;
2114 	nvlist_t *nvroot;
2115 
2116 	/*
2117 	 * Attempt to create using a bad file.
2118 	 */
2119 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2120 	VERIFY3U(ENOENT, ==,
2121 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2122 	nvlist_free(nvroot);
2123 
2124 	/*
2125 	 * Attempt to create using a bad mirror.
2126 	 */
2127 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2128 	VERIFY3U(ENOENT, ==,
2129 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2130 	nvlist_free(nvroot);
2131 
2132 	/*
2133 	 * Attempt to create an existing pool.  It shouldn't matter
2134 	 * what's in the nvroot; we should fail with EEXIST.
2135 	 */
2136 	(void) rw_rdlock(&zs->zs_name_lock);
2137 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2138 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2139 	nvlist_free(nvroot);
2140 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2141 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2142 	spa_close(spa, FTAG);
2143 
2144 	(void) rw_unlock(&zs->zs_name_lock);
2145 }
2146 
2147 static vdev_t *
2148 vdev_lookup_by_path(vdev_t *vd, const char *path)
2149 {
2150 	vdev_t *mvd;
2151 
2152 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2153 		return (vd);
2154 
2155 	for (int c = 0; c < vd->vdev_children; c++)
2156 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2157 		    NULL)
2158 			return (mvd);
2159 
2160 	return (NULL);
2161 }
2162 
2163 /*
2164  * Find the first available hole which can be used as a top-level.
2165  */
2166 int
2167 find_vdev_hole(spa_t *spa)
2168 {
2169 	vdev_t *rvd = spa->spa_root_vdev;
2170 	int c;
2171 
2172 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2173 
2174 	for (c = 0; c < rvd->vdev_children; c++) {
2175 		vdev_t *cvd = rvd->vdev_child[c];
2176 
2177 		if (cvd->vdev_ishole)
2178 			break;
2179 	}
2180 	return (c);
2181 }
2182 
2183 /*
2184  * Verify that vdev_add() works as expected.
2185  */
2186 /* ARGSUSED */
2187 void
2188 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2189 {
2190 	ztest_shared_t *zs = ztest_shared;
2191 	spa_t *spa = zs->zs_spa;
2192 	uint64_t leaves;
2193 	uint64_t guid;
2194 	nvlist_t *nvroot;
2195 	int error;
2196 
2197 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2198 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2199 
2200 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2201 
2202 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2203 
2204 	/*
2205 	 * If we have slogs then remove them 1/4 of the time.
2206 	 */
2207 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2208 		/*
2209 		 * Grab the guid from the head of the log class rotor.
2210 		 */
2211 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2212 
2213 		spa_config_exit(spa, SCL_VDEV, FTAG);
2214 
2215 		/*
2216 		 * We have to grab the zs_name_lock as writer to
2217 		 * prevent a race between removing a slog (dmu_objset_find)
2218 		 * and destroying a dataset. Removing the slog will
2219 		 * grab a reference on the dataset which may cause
2220 		 * dmu_objset_destroy() to fail with EBUSY thus
2221 		 * leaving the dataset in an inconsistent state.
2222 		 */
2223 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
2224 		error = spa_vdev_remove(spa, guid, B_FALSE);
2225 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
2226 
2227 		if (error && error != EEXIST)
2228 			fatal(0, "spa_vdev_remove() = %d", error);
2229 	} else {
2230 		spa_config_exit(spa, SCL_VDEV, FTAG);
2231 
2232 		/*
2233 		 * Make 1/4 of the devices be log devices.
2234 		 */
2235 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2236 		    ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2237 
2238 		error = spa_vdev_add(spa, nvroot);
2239 		nvlist_free(nvroot);
2240 
2241 		if (error == ENOSPC)
2242 			ztest_record_enospc("spa_vdev_add");
2243 		else if (error != 0)
2244 			fatal(0, "spa_vdev_add() = %d", error);
2245 	}
2246 
2247 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
2248 }
2249 
2250 /*
2251  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2252  */
2253 /* ARGSUSED */
2254 void
2255 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2256 {
2257 	ztest_shared_t *zs = ztest_shared;
2258 	spa_t *spa = zs->zs_spa;
2259 	vdev_t *rvd = spa->spa_root_vdev;
2260 	spa_aux_vdev_t *sav;
2261 	char *aux;
2262 	uint64_t guid = 0;
2263 	int error;
2264 
2265 	if (ztest_random(2) == 0) {
2266 		sav = &spa->spa_spares;
2267 		aux = ZPOOL_CONFIG_SPARES;
2268 	} else {
2269 		sav = &spa->spa_l2cache;
2270 		aux = ZPOOL_CONFIG_L2CACHE;
2271 	}
2272 
2273 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2274 
2275 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2276 
2277 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
2278 		/*
2279 		 * Pick a random device to remove.
2280 		 */
2281 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2282 	} else {
2283 		/*
2284 		 * Find an unused device we can add.
2285 		 */
2286 		zs->zs_vdev_aux = 0;
2287 		for (;;) {
2288 			char path[MAXPATHLEN];
2289 			int c;
2290 			(void) sprintf(path, ztest_aux_template, zopt_dir,
2291 			    zopt_pool, aux, zs->zs_vdev_aux);
2292 			for (c = 0; c < sav->sav_count; c++)
2293 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
2294 				    path) == 0)
2295 					break;
2296 			if (c == sav->sav_count &&
2297 			    vdev_lookup_by_path(rvd, path) == NULL)
2298 				break;
2299 			zs->zs_vdev_aux++;
2300 		}
2301 	}
2302 
2303 	spa_config_exit(spa, SCL_VDEV, FTAG);
2304 
2305 	if (guid == 0) {
2306 		/*
2307 		 * Add a new device.
2308 		 */
2309 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
2310 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2311 		error = spa_vdev_add(spa, nvroot);
2312 		if (error != 0)
2313 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2314 		nvlist_free(nvroot);
2315 	} else {
2316 		/*
2317 		 * Remove an existing device.  Sometimes, dirty its
2318 		 * vdev state first to make sure we handle removal
2319 		 * of devices that have pending state changes.
2320 		 */
2321 		if (ztest_random(2) == 0)
2322 			(void) vdev_online(spa, guid, 0, NULL);
2323 
2324 		error = spa_vdev_remove(spa, guid, B_FALSE);
2325 		if (error != 0 && error != EBUSY)
2326 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2327 	}
2328 
2329 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2330 }
2331 
2332 /*
2333  * split a pool if it has mirror tlvdevs
2334  */
2335 /* ARGSUSED */
2336 void
2337 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2338 {
2339 	ztest_shared_t *zs = ztest_shared;
2340 	spa_t *spa = zs->zs_spa;
2341 	vdev_t *rvd = spa->spa_root_vdev;
2342 	nvlist_t *tree, **child, *config, *split, **schild;
2343 	uint_t c, children, schildren = 0, lastlogid = 0;
2344 	int error = 0;
2345 
2346 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2347 
2348 	/* ensure we have a useable config; mirrors of raidz aren't supported */
2349 	if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2350 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2351 		return;
2352 	}
2353 
2354 	/* clean up the old pool, if any */
2355 	(void) spa_destroy("splitp");
2356 
2357 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2358 
2359 	/* generate a config from the existing config */
2360 	mutex_enter(&spa->spa_props_lock);
2361 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2362 	    &tree) == 0);
2363 	mutex_exit(&spa->spa_props_lock);
2364 
2365 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2366 	    &children) == 0);
2367 
2368 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2369 	for (c = 0; c < children; c++) {
2370 		vdev_t *tvd = rvd->vdev_child[c];
2371 		nvlist_t **mchild;
2372 		uint_t mchildren;
2373 
2374 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2375 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2376 			    0) == 0);
2377 			VERIFY(nvlist_add_string(schild[schildren],
2378 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2379 			VERIFY(nvlist_add_uint64(schild[schildren],
2380 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2381 			if (lastlogid == 0)
2382 				lastlogid = schildren;
2383 			++schildren;
2384 			continue;
2385 		}
2386 		lastlogid = 0;
2387 		VERIFY(nvlist_lookup_nvlist_array(child[c],
2388 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2389 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2390 	}
2391 
2392 	/* OK, create a config that can be used to split */
2393 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2394 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2395 	    VDEV_TYPE_ROOT) == 0);
2396 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2397 	    lastlogid != 0 ? lastlogid : schildren) == 0);
2398 
2399 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2400 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2401 
2402 	for (c = 0; c < schildren; c++)
2403 		nvlist_free(schild[c]);
2404 	free(schild);
2405 	nvlist_free(split);
2406 
2407 	spa_config_exit(spa, SCL_VDEV, FTAG);
2408 
2409 	(void) rw_wrlock(&zs->zs_name_lock);
2410 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2411 	(void) rw_unlock(&zs->zs_name_lock);
2412 
2413 	nvlist_free(config);
2414 
2415 	if (error == 0) {
2416 		(void) printf("successful split - results:\n");
2417 		mutex_enter(&spa_namespace_lock);
2418 		show_pool_stats(spa);
2419 		show_pool_stats(spa_lookup("splitp"));
2420 		mutex_exit(&spa_namespace_lock);
2421 		++zs->zs_splits;
2422 		--zs->zs_mirrors;
2423 	}
2424 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2425 
2426 }
2427 
2428 /*
2429  * Verify that we can attach and detach devices.
2430  */
2431 /* ARGSUSED */
2432 void
2433 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2434 {
2435 	ztest_shared_t *zs = ztest_shared;
2436 	spa_t *spa = zs->zs_spa;
2437 	spa_aux_vdev_t *sav = &spa->spa_spares;
2438 	vdev_t *rvd = spa->spa_root_vdev;
2439 	vdev_t *oldvd, *newvd, *pvd;
2440 	nvlist_t *root;
2441 	uint64_t leaves;
2442 	uint64_t leaf, top;
2443 	uint64_t ashift = ztest_get_ashift();
2444 	uint64_t oldguid, pguid;
2445 	size_t oldsize, newsize;
2446 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2447 	int replacing;
2448 	int oldvd_has_siblings = B_FALSE;
2449 	int newvd_is_spare = B_FALSE;
2450 	int oldvd_is_log;
2451 	int error, expected_error;
2452 
2453 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2454 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2455 
2456 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2457 
2458 	/*
2459 	 * Decide whether to do an attach or a replace.
2460 	 */
2461 	replacing = ztest_random(2);
2462 
2463 	/*
2464 	 * Pick a random top-level vdev.
2465 	 */
2466 	top = ztest_random_vdev_top(spa, B_TRUE);
2467 
2468 	/*
2469 	 * Pick a random leaf within it.
2470 	 */
2471 	leaf = ztest_random(leaves);
2472 
2473 	/*
2474 	 * Locate this vdev.
2475 	 */
2476 	oldvd = rvd->vdev_child[top];
2477 	if (zs->zs_mirrors >= 1) {
2478 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2479 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2480 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
2481 	}
2482 	if (zopt_raidz > 1) {
2483 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2484 		ASSERT(oldvd->vdev_children == zopt_raidz);
2485 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
2486 	}
2487 
2488 	/*
2489 	 * If we're already doing an attach or replace, oldvd may be a
2490 	 * mirror vdev -- in which case, pick a random child.
2491 	 */
2492 	while (oldvd->vdev_children != 0) {
2493 		oldvd_has_siblings = B_TRUE;
2494 		ASSERT(oldvd->vdev_children >= 2);
2495 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2496 	}
2497 
2498 	oldguid = oldvd->vdev_guid;
2499 	oldsize = vdev_get_min_asize(oldvd);
2500 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
2501 	(void) strcpy(oldpath, oldvd->vdev_path);
2502 	pvd = oldvd->vdev_parent;
2503 	pguid = pvd->vdev_guid;
2504 
2505 	/*
2506 	 * If oldvd has siblings, then half of the time, detach it.
2507 	 */
2508 	if (oldvd_has_siblings && ztest_random(2) == 0) {
2509 		spa_config_exit(spa, SCL_VDEV, FTAG);
2510 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2511 		if (error != 0 && error != ENODEV && error != EBUSY &&
2512 		    error != ENOTSUP)
2513 			fatal(0, "detach (%s) returned %d", oldpath, error);
2514 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2515 		return;
2516 	}
2517 
2518 	/*
2519 	 * For the new vdev, choose with equal probability between the two
2520 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2521 	 */
2522 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
2523 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2524 		newvd_is_spare = B_TRUE;
2525 		(void) strcpy(newpath, newvd->vdev_path);
2526 	} else {
2527 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2528 		    zopt_dir, zopt_pool, top * leaves + leaf);
2529 		if (ztest_random(2) == 0)
2530 			newpath[strlen(newpath) - 1] = 'b';
2531 		newvd = vdev_lookup_by_path(rvd, newpath);
2532 	}
2533 
2534 	if (newvd) {
2535 		newsize = vdev_get_min_asize(newvd);
2536 	} else {
2537 		/*
2538 		 * Make newsize a little bigger or smaller than oldsize.
2539 		 * If it's smaller, the attach should fail.
2540 		 * If it's larger, and we're doing a replace,
2541 		 * we should get dynamic LUN growth when we're done.
2542 		 */
2543 		newsize = 10 * oldsize / (9 + ztest_random(3));
2544 	}
2545 
2546 	/*
2547 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2548 	 * unless it's a replace; in that case any non-replacing parent is OK.
2549 	 *
2550 	 * If newvd is already part of the pool, it should fail with EBUSY.
2551 	 *
2552 	 * If newvd is too small, it should fail with EOVERFLOW.
2553 	 */
2554 	if (pvd->vdev_ops != &vdev_mirror_ops &&
2555 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2556 	    pvd->vdev_ops == &vdev_replacing_ops ||
2557 	    pvd->vdev_ops == &vdev_spare_ops))
2558 		expected_error = ENOTSUP;
2559 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
2560 		expected_error = ENOTSUP;
2561 	else if (newvd == oldvd)
2562 		expected_error = replacing ? 0 : EBUSY;
2563 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2564 		expected_error = EBUSY;
2565 	else if (newsize < oldsize)
2566 		expected_error = EOVERFLOW;
2567 	else if (ashift > oldvd->vdev_top->vdev_ashift)
2568 		expected_error = EDOM;
2569 	else
2570 		expected_error = 0;
2571 
2572 	spa_config_exit(spa, SCL_VDEV, FTAG);
2573 
2574 	/*
2575 	 * Build the nvlist describing newpath.
2576 	 */
2577 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2578 	    ashift, 0, 0, 0, 1);
2579 
2580 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2581 
2582 	nvlist_free(root);
2583 
2584 	/*
2585 	 * If our parent was the replacing vdev, but the replace completed,
2586 	 * then instead of failing with ENOTSUP we may either succeed,
2587 	 * fail with ENODEV, or fail with EOVERFLOW.
2588 	 */
2589 	if (expected_error == ENOTSUP &&
2590 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2591 		expected_error = error;
2592 
2593 	/*
2594 	 * If someone grew the LUN, the replacement may be too small.
2595 	 */
2596 	if (error == EOVERFLOW || error == EBUSY)
2597 		expected_error = error;
2598 
2599 	/* XXX workaround 6690467 */
2600 	if (error != expected_error && expected_error != EBUSY) {
2601 		fatal(0, "attach (%s %llu, %s %llu, %d) "
2602 		    "returned %d, expected %d",
2603 		    oldpath, (longlong_t)oldsize, newpath,
2604 		    (longlong_t)newsize, replacing, error, expected_error);
2605 	}
2606 
2607 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2608 }
2609 
2610 /*
2611  * Callback function which expands the physical size of the vdev.
2612  */
2613 vdev_t *
2614 grow_vdev(vdev_t *vd, void *arg)
2615 {
2616 	spa_t *spa = vd->vdev_spa;
2617 	size_t *newsize = arg;
2618 	size_t fsize;
2619 	int fd;
2620 
2621 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2622 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2623 
2624 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2625 		return (vd);
2626 
2627 	fsize = lseek(fd, 0, SEEK_END);
2628 	(void) ftruncate(fd, *newsize);
2629 
2630 	if (zopt_verbose >= 6) {
2631 		(void) printf("%s grew from %lu to %lu bytes\n",
2632 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2633 	}
2634 	(void) close(fd);
2635 	return (NULL);
2636 }
2637 
2638 /*
2639  * Callback function which expands a given vdev by calling vdev_online().
2640  */
2641 /* ARGSUSED */
2642 vdev_t *
2643 online_vdev(vdev_t *vd, void *arg)
2644 {
2645 	spa_t *spa = vd->vdev_spa;
2646 	vdev_t *tvd = vd->vdev_top;
2647 	uint64_t guid = vd->vdev_guid;
2648 	uint64_t generation = spa->spa_config_generation + 1;
2649 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2650 	int error;
2651 
2652 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2653 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2654 
2655 	/* Calling vdev_online will initialize the new metaslabs */
2656 	spa_config_exit(spa, SCL_STATE, spa);
2657 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2658 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2659 
2660 	/*
2661 	 * If vdev_online returned an error or the underlying vdev_open
2662 	 * failed then we abort the expand. The only way to know that
2663 	 * vdev_open fails is by checking the returned newstate.
2664 	 */
2665 	if (error || newstate != VDEV_STATE_HEALTHY) {
2666 		if (zopt_verbose >= 5) {
2667 			(void) printf("Unable to expand vdev, state %llu, "
2668 			    "error %d\n", (u_longlong_t)newstate, error);
2669 		}
2670 		return (vd);
2671 	}
2672 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2673 
2674 	/*
2675 	 * Since we dropped the lock we need to ensure that we're
2676 	 * still talking to the original vdev. It's possible this
2677 	 * vdev may have been detached/replaced while we were
2678 	 * trying to online it.
2679 	 */
2680 	if (generation != spa->spa_config_generation) {
2681 		if (zopt_verbose >= 5) {
2682 			(void) printf("vdev configuration has changed, "
2683 			    "guid %llu, state %llu, expected gen %llu, "
2684 			    "got gen %llu\n",
2685 			    (u_longlong_t)guid,
2686 			    (u_longlong_t)tvd->vdev_state,
2687 			    (u_longlong_t)generation,
2688 			    (u_longlong_t)spa->spa_config_generation);
2689 		}
2690 		return (vd);
2691 	}
2692 	return (NULL);
2693 }
2694 
2695 /*
2696  * Traverse the vdev tree calling the supplied function.
2697  * We continue to walk the tree until we either have walked all
2698  * children or we receive a non-NULL return from the callback.
2699  * If a NULL callback is passed, then we just return back the first
2700  * leaf vdev we encounter.
2701  */
2702 vdev_t *
2703 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2704 {
2705 	if (vd->vdev_ops->vdev_op_leaf) {
2706 		if (func == NULL)
2707 			return (vd);
2708 		else
2709 			return (func(vd, arg));
2710 	}
2711 
2712 	for (uint_t c = 0; c < vd->vdev_children; c++) {
2713 		vdev_t *cvd = vd->vdev_child[c];
2714 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2715 			return (cvd);
2716 	}
2717 	return (NULL);
2718 }
2719 
2720 /*
2721  * Verify that dynamic LUN growth works as expected.
2722  */
2723 /* ARGSUSED */
2724 void
2725 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2726 {
2727 	ztest_shared_t *zs = ztest_shared;
2728 	spa_t *spa = zs->zs_spa;
2729 	vdev_t *vd, *tvd;
2730 	metaslab_class_t *mc;
2731 	metaslab_group_t *mg;
2732 	size_t psize, newsize;
2733 	uint64_t top;
2734 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2735 
2736 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2737 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2738 
2739 	top = ztest_random_vdev_top(spa, B_TRUE);
2740 
2741 	tvd = spa->spa_root_vdev->vdev_child[top];
2742 	mg = tvd->vdev_mg;
2743 	mc = mg->mg_class;
2744 	old_ms_count = tvd->vdev_ms_count;
2745 	old_class_space = metaslab_class_get_space(mc);
2746 
2747 	/*
2748 	 * Determine the size of the first leaf vdev associated with
2749 	 * our top-level device.
2750 	 */
2751 	vd = vdev_walk_tree(tvd, NULL, NULL);
2752 	ASSERT3P(vd, !=, NULL);
2753 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2754 
2755 	psize = vd->vdev_psize;
2756 
2757 	/*
2758 	 * We only try to expand the vdev if it's healthy, less than 4x its
2759 	 * original size, and it has a valid psize.
2760 	 */
2761 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2762 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
2763 		spa_config_exit(spa, SCL_STATE, spa);
2764 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2765 		return;
2766 	}
2767 	ASSERT(psize > 0);
2768 	newsize = psize + psize / 8;
2769 	ASSERT3U(newsize, >, psize);
2770 
2771 	if (zopt_verbose >= 6) {
2772 		(void) printf("Expanding LUN %s from %lu to %lu\n",
2773 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2774 	}
2775 
2776 	/*
2777 	 * Growing the vdev is a two step process:
2778 	 *	1). expand the physical size (i.e. relabel)
2779 	 *	2). online the vdev to create the new metaslabs
2780 	 */
2781 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2782 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2783 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
2784 		if (zopt_verbose >= 5) {
2785 			(void) printf("Could not expand LUN because "
2786 			    "the vdev configuration changed.\n");
2787 		}
2788 		spa_config_exit(spa, SCL_STATE, spa);
2789 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2790 		return;
2791 	}
2792 
2793 	spa_config_exit(spa, SCL_STATE, spa);
2794 
2795 	/*
2796 	 * Expanding the LUN will update the config asynchronously,
2797 	 * thus we must wait for the async thread to complete any
2798 	 * pending tasks before proceeding.
2799 	 */
2800 	for (;;) {
2801 		boolean_t done;
2802 		mutex_enter(&spa->spa_async_lock);
2803 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2804 		mutex_exit(&spa->spa_async_lock);
2805 		if (done)
2806 			break;
2807 		txg_wait_synced(spa_get_dsl(spa), 0);
2808 		(void) poll(NULL, 0, 100);
2809 	}
2810 
2811 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2812 
2813 	tvd = spa->spa_root_vdev->vdev_child[top];
2814 	new_ms_count = tvd->vdev_ms_count;
2815 	new_class_space = metaslab_class_get_space(mc);
2816 
2817 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2818 		if (zopt_verbose >= 5) {
2819 			(void) printf("Could not verify LUN expansion due to "
2820 			    "intervening vdev offline or remove.\n");
2821 		}
2822 		spa_config_exit(spa, SCL_STATE, spa);
2823 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2824 		return;
2825 	}
2826 
2827 	/*
2828 	 * Make sure we were able to grow the vdev.
2829 	 */
2830 	if (new_ms_count <= old_ms_count)
2831 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2832 		    old_ms_count, new_ms_count);
2833 
2834 	/*
2835 	 * Make sure we were able to grow the pool.
2836 	 */
2837 	if (new_class_space <= old_class_space)
2838 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2839 		    old_class_space, new_class_space);
2840 
2841 	if (zopt_verbose >= 5) {
2842 		char oldnumbuf[6], newnumbuf[6];
2843 
2844 		nicenum(old_class_space, oldnumbuf);
2845 		nicenum(new_class_space, newnumbuf);
2846 		(void) printf("%s grew from %s to %s\n",
2847 		    spa->spa_name, oldnumbuf, newnumbuf);
2848 	}
2849 
2850 	spa_config_exit(spa, SCL_STATE, spa);
2851 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2852 }
2853 
2854 /*
2855  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2856  */
2857 /* ARGSUSED */
2858 static void
2859 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2860 {
2861 	/*
2862 	 * Create the objects common to all ztest datasets.
2863 	 */
2864 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2865 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2866 }
2867 
2868 static int
2869 ztest_dataset_create(char *dsname)
2870 {
2871 	uint64_t zilset = ztest_random(100);
2872 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2873 	    ztest_objset_create_cb, NULL);
2874 
2875 	if (err || zilset < 80)
2876 		return (err);
2877 
2878 	(void) printf("Setting dataset %s to sync always\n", dsname);
2879 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2880 	    ZFS_SYNC_ALWAYS, B_FALSE));
2881 }
2882 
2883 /* ARGSUSED */
2884 static int
2885 ztest_objset_destroy_cb(const char *name, void *arg)
2886 {
2887 	objset_t *os;
2888 	dmu_object_info_t doi;
2889 	int error;
2890 
2891 	/*
2892 	 * Verify that the dataset contains a directory object.
2893 	 */
2894 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2895 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
2896 	if (error != ENOENT) {
2897 		/* We could have crashed in the middle of destroying it */
2898 		ASSERT3U(error, ==, 0);
2899 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2900 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
2901 	}
2902 	dmu_objset_rele(os, FTAG);
2903 
2904 	/*
2905 	 * Destroy the dataset.
2906 	 */
2907 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
2908 	return (0);
2909 }
2910 
2911 static boolean_t
2912 ztest_snapshot_create(char *osname, uint64_t id)
2913 {
2914 	char snapname[MAXNAMELEN];
2915 	int error;
2916 
2917 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2918 	    (u_longlong_t)id);
2919 
2920 	error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2921 	    NULL, NULL, B_FALSE, B_FALSE, -1);
2922 	if (error == ENOSPC) {
2923 		ztest_record_enospc(FTAG);
2924 		return (B_FALSE);
2925 	}
2926 	if (error != 0 && error != EEXIST)
2927 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2928 	return (B_TRUE);
2929 }
2930 
2931 static boolean_t
2932 ztest_snapshot_destroy(char *osname, uint64_t id)
2933 {
2934 	char snapname[MAXNAMELEN];
2935 	int error;
2936 
2937 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2938 	    (u_longlong_t)id);
2939 
2940 	error = dmu_objset_destroy(snapname, B_FALSE);
2941 	if (error != 0 && error != ENOENT)
2942 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2943 	return (B_TRUE);
2944 }
2945 
2946 /* ARGSUSED */
2947 void
2948 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2949 {
2950 	ztest_shared_t *zs = ztest_shared;
2951 	ztest_ds_t zdtmp;
2952 	int iters;
2953 	int error;
2954 	objset_t *os, *os2;
2955 	char name[MAXNAMELEN];
2956 	zilog_t *zilog;
2957 
2958 	(void) rw_rdlock(&zs->zs_name_lock);
2959 
2960 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2961 	    zs->zs_pool, (u_longlong_t)id);
2962 
2963 	/*
2964 	 * If this dataset exists from a previous run, process its replay log
2965 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
2966 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2967 	 */
2968 	if (ztest_random(2) == 0 &&
2969 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2970 		ztest_zd_init(&zdtmp, os);
2971 		zil_replay(os, &zdtmp, ztest_replay_vector);
2972 		ztest_zd_fini(&zdtmp);
2973 		dmu_objset_disown(os, FTAG);
2974 	}
2975 
2976 	/*
2977 	 * There may be an old instance of the dataset we're about to
2978 	 * create lying around from a previous run.  If so, destroy it
2979 	 * and all of its snapshots.
2980 	 */
2981 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
2982 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2983 
2984 	/*
2985 	 * Verify that the destroyed dataset is no longer in the namespace.
2986 	 */
2987 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2988 
2989 	/*
2990 	 * Verify that we can create a new dataset.
2991 	 */
2992 	error = ztest_dataset_create(name);
2993 	if (error) {
2994 		if (error == ENOSPC) {
2995 			ztest_record_enospc(FTAG);
2996 			(void) rw_unlock(&zs->zs_name_lock);
2997 			return;
2998 		}
2999 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3000 	}
3001 
3002 	VERIFY3U(0, ==,
3003 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3004 
3005 	ztest_zd_init(&zdtmp, os);
3006 
3007 	/*
3008 	 * Open the intent log for it.
3009 	 */
3010 	zilog = zil_open(os, ztest_get_data);
3011 
3012 	/*
3013 	 * Put some objects in there, do a little I/O to them,
3014 	 * and randomly take a couple of snapshots along the way.
3015 	 */
3016 	iters = ztest_random(5);
3017 	for (int i = 0; i < iters; i++) {
3018 		ztest_dmu_object_alloc_free(&zdtmp, id);
3019 		if (ztest_random(iters) == 0)
3020 			(void) ztest_snapshot_create(name, i);
3021 	}
3022 
3023 	/*
3024 	 * Verify that we cannot create an existing dataset.
3025 	 */
3026 	VERIFY3U(EEXIST, ==,
3027 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3028 
3029 	/*
3030 	 * Verify that we can hold an objset that is also owned.
3031 	 */
3032 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3033 	dmu_objset_rele(os2, FTAG);
3034 
3035 	/*
3036 	 * Verify that we cannot own an objset that is already owned.
3037 	 */
3038 	VERIFY3U(EBUSY, ==,
3039 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3040 
3041 	zil_close(zilog);
3042 	dmu_objset_disown(os, FTAG);
3043 	ztest_zd_fini(&zdtmp);
3044 
3045 	(void) rw_unlock(&zs->zs_name_lock);
3046 }
3047 
3048 /*
3049  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3050  */
3051 void
3052 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3053 {
3054 	ztest_shared_t *zs = ztest_shared;
3055 
3056 	(void) rw_rdlock(&zs->zs_name_lock);
3057 	(void) ztest_snapshot_destroy(zd->zd_name, id);
3058 	(void) ztest_snapshot_create(zd->zd_name, id);
3059 	(void) rw_unlock(&zs->zs_name_lock);
3060 }
3061 
3062 /*
3063  * Cleanup non-standard snapshots and clones.
3064  */
3065 void
3066 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3067 {
3068 	char snap1name[MAXNAMELEN];
3069 	char clone1name[MAXNAMELEN];
3070 	char snap2name[MAXNAMELEN];
3071 	char clone2name[MAXNAMELEN];
3072 	char snap3name[MAXNAMELEN];
3073 	int error;
3074 
3075 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
3076 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
3077 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
3078 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
3079 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
3080 
3081 	error = dmu_objset_destroy(clone2name, B_FALSE);
3082 	if (error && error != ENOENT)
3083 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3084 	error = dmu_objset_destroy(snap3name, B_FALSE);
3085 	if (error && error != ENOENT)
3086 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3087 	error = dmu_objset_destroy(snap2name, B_FALSE);
3088 	if (error && error != ENOENT)
3089 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3090 	error = dmu_objset_destroy(clone1name, B_FALSE);
3091 	if (error && error != ENOENT)
3092 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3093 	error = dmu_objset_destroy(snap1name, B_FALSE);
3094 	if (error && error != ENOENT)
3095 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3096 }
3097 
3098 /*
3099  * Verify dsl_dataset_promote handles EBUSY
3100  */
3101 void
3102 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3103 {
3104 	ztest_shared_t *zs = ztest_shared;
3105 	objset_t *clone;
3106 	dsl_dataset_t *ds;
3107 	char snap1name[MAXNAMELEN];
3108 	char clone1name[MAXNAMELEN];
3109 	char snap2name[MAXNAMELEN];
3110 	char clone2name[MAXNAMELEN];
3111 	char snap3name[MAXNAMELEN];
3112 	char *osname = zd->zd_name;
3113 	int error;
3114 
3115 	(void) rw_rdlock(&zs->zs_name_lock);
3116 
3117 	ztest_dsl_dataset_cleanup(osname, id);
3118 
3119 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
3120 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
3121 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
3122 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
3123 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
3124 
3125 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3126 	    NULL, NULL, B_FALSE, B_FALSE, -1);
3127 	if (error && error != EEXIST) {
3128 		if (error == ENOSPC) {
3129 			ztest_record_enospc(FTAG);
3130 			goto out;
3131 		}
3132 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3133 	}
3134 
3135 	error = dmu_objset_hold(snap1name, FTAG, &clone);
3136 	if (error)
3137 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3138 
3139 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3140 	dmu_objset_rele(clone, FTAG);
3141 	if (error) {
3142 		if (error == ENOSPC) {
3143 			ztest_record_enospc(FTAG);
3144 			goto out;
3145 		}
3146 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3147 	}
3148 
3149 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3150 	    NULL, NULL, B_FALSE, B_FALSE, -1);
3151 	if (error && error != EEXIST) {
3152 		if (error == ENOSPC) {
3153 			ztest_record_enospc(FTAG);
3154 			goto out;
3155 		}
3156 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3157 	}
3158 
3159 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3160 	    NULL, NULL, B_FALSE, B_FALSE, -1);
3161 	if (error && error != EEXIST) {
3162 		if (error == ENOSPC) {
3163 			ztest_record_enospc(FTAG);
3164 			goto out;
3165 		}
3166 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3167 	}
3168 
3169 	error = dmu_objset_hold(snap3name, FTAG, &clone);
3170 	if (error)
3171 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3172 
3173 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3174 	dmu_objset_rele(clone, FTAG);
3175 	if (error) {
3176 		if (error == ENOSPC) {
3177 			ztest_record_enospc(FTAG);
3178 			goto out;
3179 		}
3180 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3181 	}
3182 
3183 	error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3184 	if (error)
3185 		fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3186 	error = dsl_dataset_promote(clone2name, NULL);
3187 	if (error != EBUSY)
3188 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3189 		    error);
3190 	dsl_dataset_disown(ds, FTAG);
3191 
3192 out:
3193 	ztest_dsl_dataset_cleanup(osname, id);
3194 
3195 	(void) rw_unlock(&zs->zs_name_lock);
3196 }
3197 
3198 /*
3199  * Verify that dmu_object_{alloc,free} work as expected.
3200  */
3201 void
3202 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3203 {
3204 	ztest_od_t od[4];
3205 	int batchsize = sizeof (od) / sizeof (od[0]);
3206 
3207 	for (int b = 0; b < batchsize; b++)
3208 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3209 
3210 	/*
3211 	 * Destroy the previous batch of objects, create a new batch,
3212 	 * and do some I/O on the new objects.
3213 	 */
3214 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3215 		return;
3216 
3217 	while (ztest_random(4 * batchsize) != 0)
3218 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
3219 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3220 }
3221 
3222 /*
3223  * Verify that dmu_{read,write} work as expected.
3224  */
3225 void
3226 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3227 {
3228 	objset_t *os = zd->zd_os;
3229 	ztest_od_t od[2];
3230 	dmu_tx_t *tx;
3231 	int i, freeit, error;
3232 	uint64_t n, s, txg;
3233 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3234 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3235 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3236 	uint64_t regions = 997;
3237 	uint64_t stride = 123456789ULL;
3238 	uint64_t width = 40;
3239 	int free_percent = 5;
3240 
3241 	/*
3242 	 * This test uses two objects, packobj and bigobj, that are always
3243 	 * updated together (i.e. in the same tx) so that their contents are
3244 	 * in sync and can be compared.  Their contents relate to each other
3245 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3246 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3247 	 * for any index n, there are three bufwads that should be identical:
3248 	 *
3249 	 *	packobj, at offset n * sizeof (bufwad_t)
3250 	 *	bigobj, at the head of the nth chunk
3251 	 *	bigobj, at the tail of the nth chunk
3252 	 *
3253 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3254 	 * and it doesn't have any relation to the object blocksize.
3255 	 * The only requirement is that it can hold at least two bufwads.
3256 	 *
3257 	 * Normally, we write the bufwad to each of these locations.
3258 	 * However, free_percent of the time we instead write zeroes to
3259 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3260 	 * bigobj to packobj, we can verify that the DMU is correctly
3261 	 * tracking which parts of an object are allocated and free,
3262 	 * and that the contents of the allocated blocks are correct.
3263 	 */
3264 
3265 	/*
3266 	 * Read the directory info.  If it's the first time, set things up.
3267 	 */
3268 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3269 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3270 
3271 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3272 		return;
3273 
3274 	bigobj = od[0].od_object;
3275 	packobj = od[1].od_object;
3276 	chunksize = od[0].od_gen;
3277 	ASSERT(chunksize == od[1].od_gen);
3278 
3279 	/*
3280 	 * Prefetch a random chunk of the big object.
3281 	 * Our aim here is to get some async reads in flight
3282 	 * for blocks that we may free below; the DMU should
3283 	 * handle this race correctly.
3284 	 */
3285 	n = ztest_random(regions) * stride + ztest_random(width);
3286 	s = 1 + ztest_random(2 * width - 1);
3287 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3288 
3289 	/*
3290 	 * Pick a random index and compute the offsets into packobj and bigobj.
3291 	 */
3292 	n = ztest_random(regions) * stride + ztest_random(width);
3293 	s = 1 + ztest_random(width - 1);
3294 
3295 	packoff = n * sizeof (bufwad_t);
3296 	packsize = s * sizeof (bufwad_t);
3297 
3298 	bigoff = n * chunksize;
3299 	bigsize = s * chunksize;
3300 
3301 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3302 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3303 
3304 	/*
3305 	 * free_percent of the time, free a range of bigobj rather than
3306 	 * overwriting it.
3307 	 */
3308 	freeit = (ztest_random(100) < free_percent);
3309 
3310 	/*
3311 	 * Read the current contents of our objects.
3312 	 */
3313 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
3314 	    DMU_READ_PREFETCH);
3315 	ASSERT3U(error, ==, 0);
3316 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3317 	    DMU_READ_PREFETCH);
3318 	ASSERT3U(error, ==, 0);
3319 
3320 	/*
3321 	 * Get a tx for the mods to both packobj and bigobj.
3322 	 */
3323 	tx = dmu_tx_create(os);
3324 
3325 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3326 
3327 	if (freeit)
3328 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3329 	else
3330 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3331 
3332 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3333 	if (txg == 0) {
3334 		umem_free(packbuf, packsize);
3335 		umem_free(bigbuf, bigsize);
3336 		return;
3337 	}
3338 
3339 	dmu_object_set_checksum(os, bigobj,
3340 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3341 
3342 	dmu_object_set_compress(os, bigobj,
3343 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3344 
3345 	/*
3346 	 * For each index from n to n + s, verify that the existing bufwad
3347 	 * in packobj matches the bufwads at the head and tail of the
3348 	 * corresponding chunk in bigobj.  Then update all three bufwads
3349 	 * with the new values we want to write out.
3350 	 */
3351 	for (i = 0; i < s; i++) {
3352 		/* LINTED */
3353 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3354 		/* LINTED */
3355 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3356 		/* LINTED */
3357 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3358 
3359 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3360 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3361 
3362 		if (pack->bw_txg > txg)
3363 			fatal(0, "future leak: got %llx, open txg is %llx",
3364 			    pack->bw_txg, txg);
3365 
3366 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3367 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3368 			    pack->bw_index, n, i);
3369 
3370 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3371 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3372 
3373 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3374 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3375 
3376 		if (freeit) {
3377 			bzero(pack, sizeof (bufwad_t));
3378 		} else {
3379 			pack->bw_index = n + i;
3380 			pack->bw_txg = txg;
3381 			pack->bw_data = 1 + ztest_random(-2ULL);
3382 		}
3383 		*bigH = *pack;
3384 		*bigT = *pack;
3385 	}
3386 
3387 	/*
3388 	 * We've verified all the old bufwads, and made new ones.
3389 	 * Now write them out.
3390 	 */
3391 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3392 
3393 	if (freeit) {
3394 		if (zopt_verbose >= 7) {
3395 			(void) printf("freeing offset %llx size %llx"
3396 			    " txg %llx\n",
3397 			    (u_longlong_t)bigoff,
3398 			    (u_longlong_t)bigsize,
3399 			    (u_longlong_t)txg);
3400 		}
3401 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3402 	} else {
3403 		if (zopt_verbose >= 7) {
3404 			(void) printf("writing offset %llx size %llx"
3405 			    " txg %llx\n",
3406 			    (u_longlong_t)bigoff,
3407 			    (u_longlong_t)bigsize,
3408 			    (u_longlong_t)txg);
3409 		}
3410 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3411 	}
3412 
3413 	dmu_tx_commit(tx);
3414 
3415 	/*
3416 	 * Sanity check the stuff we just wrote.
3417 	 */
3418 	{
3419 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3420 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3421 
3422 		VERIFY(0 == dmu_read(os, packobj, packoff,
3423 		    packsize, packcheck, DMU_READ_PREFETCH));
3424 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
3425 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3426 
3427 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3428 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3429 
3430 		umem_free(packcheck, packsize);
3431 		umem_free(bigcheck, bigsize);
3432 	}
3433 
3434 	umem_free(packbuf, packsize);
3435 	umem_free(bigbuf, bigsize);
3436 }
3437 
3438 void
3439 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3440     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3441 {
3442 	uint64_t i;
3443 	bufwad_t *pack;
3444 	bufwad_t *bigH;
3445 	bufwad_t *bigT;
3446 
3447 	/*
3448 	 * For each index from n to n + s, verify that the existing bufwad
3449 	 * in packobj matches the bufwads at the head and tail of the
3450 	 * corresponding chunk in bigobj.  Then update all three bufwads
3451 	 * with the new values we want to write out.
3452 	 */
3453 	for (i = 0; i < s; i++) {
3454 		/* LINTED */
3455 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3456 		/* LINTED */
3457 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3458 		/* LINTED */
3459 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3460 
3461 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3462 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3463 
3464 		if (pack->bw_txg > txg)
3465 			fatal(0, "future leak: got %llx, open txg is %llx",
3466 			    pack->bw_txg, txg);
3467 
3468 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3469 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3470 			    pack->bw_index, n, i);
3471 
3472 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3473 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3474 
3475 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3476 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3477 
3478 		pack->bw_index = n + i;
3479 		pack->bw_txg = txg;
3480 		pack->bw_data = 1 + ztest_random(-2ULL);
3481 
3482 		*bigH = *pack;
3483 		*bigT = *pack;
3484 	}
3485 }
3486 
3487 void
3488 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3489 {
3490 	objset_t *os = zd->zd_os;
3491 	ztest_od_t od[2];
3492 	dmu_tx_t *tx;
3493 	uint64_t i;
3494 	int error;
3495 	uint64_t n, s, txg;
3496 	bufwad_t *packbuf, *bigbuf;
3497 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3498 	uint64_t blocksize = ztest_random_blocksize();
3499 	uint64_t chunksize = blocksize;
3500 	uint64_t regions = 997;
3501 	uint64_t stride = 123456789ULL;
3502 	uint64_t width = 9;
3503 	dmu_buf_t *bonus_db;
3504 	arc_buf_t **bigbuf_arcbufs;
3505 	dmu_object_info_t doi;
3506 
3507 	/*
3508 	 * This test uses two objects, packobj and bigobj, that are always
3509 	 * updated together (i.e. in the same tx) so that their contents are
3510 	 * in sync and can be compared.  Their contents relate to each other
3511 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3512 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3513 	 * for any index n, there are three bufwads that should be identical:
3514 	 *
3515 	 *	packobj, at offset n * sizeof (bufwad_t)
3516 	 *	bigobj, at the head of the nth chunk
3517 	 *	bigobj, at the tail of the nth chunk
3518 	 *
3519 	 * The chunk size is set equal to bigobj block size so that
3520 	 * dmu_assign_arcbuf() can be tested for object updates.
3521 	 */
3522 
3523 	/*
3524 	 * Read the directory info.  If it's the first time, set things up.
3525 	 */
3526 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3527 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3528 
3529 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3530 		return;
3531 
3532 	bigobj = od[0].od_object;
3533 	packobj = od[1].od_object;
3534 	blocksize = od[0].od_blocksize;
3535 	chunksize = blocksize;
3536 	ASSERT(chunksize == od[1].od_gen);
3537 
3538 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3539 	VERIFY(ISP2(doi.doi_data_block_size));
3540 	VERIFY(chunksize == doi.doi_data_block_size);
3541 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3542 
3543 	/*
3544 	 * Pick a random index and compute the offsets into packobj and bigobj.
3545 	 */
3546 	n = ztest_random(regions) * stride + ztest_random(width);
3547 	s = 1 + ztest_random(width - 1);
3548 
3549 	packoff = n * sizeof (bufwad_t);
3550 	packsize = s * sizeof (bufwad_t);
3551 
3552 	bigoff = n * chunksize;
3553 	bigsize = s * chunksize;
3554 
3555 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3556 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3557 
3558 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3559 
3560 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3561 
3562 	/*
3563 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3564 	 * Iteration 1 test zcopy to already referenced dbufs.
3565 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3566 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3567 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
3568 	 * Iteration 5 test zcopy when it can't be done.
3569 	 * Iteration 6 one more zcopy write.
3570 	 */
3571 	for (i = 0; i < 7; i++) {
3572 		uint64_t j;
3573 		uint64_t off;
3574 
3575 		/*
3576 		 * In iteration 5 (i == 5) use arcbufs
3577 		 * that don't match bigobj blksz to test
3578 		 * dmu_assign_arcbuf() when it can't directly
3579 		 * assign an arcbuf to a dbuf.
3580 		 */
3581 		for (j = 0; j < s; j++) {
3582 			if (i != 5) {
3583 				bigbuf_arcbufs[j] =
3584 				    dmu_request_arcbuf(bonus_db, chunksize);
3585 			} else {
3586 				bigbuf_arcbufs[2 * j] =
3587 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
3588 				bigbuf_arcbufs[2 * j + 1] =
3589 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
3590 			}
3591 		}
3592 
3593 		/*
3594 		 * Get a tx for the mods to both packobj and bigobj.
3595 		 */
3596 		tx = dmu_tx_create(os);
3597 
3598 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
3599 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3600 
3601 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3602 		if (txg == 0) {
3603 			umem_free(packbuf, packsize);
3604 			umem_free(bigbuf, bigsize);
3605 			for (j = 0; j < s; j++) {
3606 				if (i != 5) {
3607 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
3608 				} else {
3609 					dmu_return_arcbuf(
3610 					    bigbuf_arcbufs[2 * j]);
3611 					dmu_return_arcbuf(
3612 					    bigbuf_arcbufs[2 * j + 1]);
3613 				}
3614 			}
3615 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3616 			dmu_buf_rele(bonus_db, FTAG);
3617 			return;
3618 		}
3619 
3620 		/*
3621 		 * 50% of the time don't read objects in the 1st iteration to
3622 		 * test dmu_assign_arcbuf() for the case when there're no
3623 		 * existing dbufs for the specified offsets.
3624 		 */
3625 		if (i != 0 || ztest_random(2) != 0) {
3626 			error = dmu_read(os, packobj, packoff,
3627 			    packsize, packbuf, DMU_READ_PREFETCH);
3628 			ASSERT3U(error, ==, 0);
3629 			error = dmu_read(os, bigobj, bigoff, bigsize,
3630 			    bigbuf, DMU_READ_PREFETCH);
3631 			ASSERT3U(error, ==, 0);
3632 		}
3633 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3634 		    n, chunksize, txg);
3635 
3636 		/*
3637 		 * We've verified all the old bufwads, and made new ones.
3638 		 * Now write them out.
3639 		 */
3640 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3641 		if (zopt_verbose >= 7) {
3642 			(void) printf("writing offset %llx size %llx"
3643 			    " txg %llx\n",
3644 			    (u_longlong_t)bigoff,
3645 			    (u_longlong_t)bigsize,
3646 			    (u_longlong_t)txg);
3647 		}
3648 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3649 			dmu_buf_t *dbt;
3650 			if (i != 5) {
3651 				bcopy((caddr_t)bigbuf + (off - bigoff),
3652 				    bigbuf_arcbufs[j]->b_data, chunksize);
3653 			} else {
3654 				bcopy((caddr_t)bigbuf + (off - bigoff),
3655 				    bigbuf_arcbufs[2 * j]->b_data,
3656 				    chunksize / 2);
3657 				bcopy((caddr_t)bigbuf + (off - bigoff) +
3658 				    chunksize / 2,
3659 				    bigbuf_arcbufs[2 * j + 1]->b_data,
3660 				    chunksize / 2);
3661 			}
3662 
3663 			if (i == 1) {
3664 				VERIFY(dmu_buf_hold(os, bigobj, off,
3665 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3666 			}
3667 			if (i != 5) {
3668 				dmu_assign_arcbuf(bonus_db, off,
3669 				    bigbuf_arcbufs[j], tx);
3670 			} else {
3671 				dmu_assign_arcbuf(bonus_db, off,
3672 				    bigbuf_arcbufs[2 * j], tx);
3673 				dmu_assign_arcbuf(bonus_db,
3674 				    off + chunksize / 2,
3675 				    bigbuf_arcbufs[2 * j + 1], tx);
3676 			}
3677 			if (i == 1) {
3678 				dmu_buf_rele(dbt, FTAG);
3679 			}
3680 		}
3681 		dmu_tx_commit(tx);
3682 
3683 		/*
3684 		 * Sanity check the stuff we just wrote.
3685 		 */
3686 		{
3687 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3688 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3689 
3690 			VERIFY(0 == dmu_read(os, packobj, packoff,
3691 			    packsize, packcheck, DMU_READ_PREFETCH));
3692 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
3693 			    bigsize, bigcheck, DMU_READ_PREFETCH));
3694 
3695 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3696 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3697 
3698 			umem_free(packcheck, packsize);
3699 			umem_free(bigcheck, bigsize);
3700 		}
3701 		if (i == 2) {
3702 			txg_wait_open(dmu_objset_pool(os), 0);
3703 		} else if (i == 3) {
3704 			txg_wait_synced(dmu_objset_pool(os), 0);
3705 		}
3706 	}
3707 
3708 	dmu_buf_rele(bonus_db, FTAG);
3709 	umem_free(packbuf, packsize);
3710 	umem_free(bigbuf, bigsize);
3711 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3712 }
3713 
3714 /* ARGSUSED */
3715 void
3716 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3717 {
3718 	ztest_od_t od[1];
3719 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3720 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3721 
3722 	/*
3723 	 * Have multiple threads write to large offsets in an object
3724 	 * to verify that parallel writes to an object -- even to the
3725 	 * same blocks within the object -- doesn't cause any trouble.
3726 	 */
3727 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3728 
3729 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3730 		return;
3731 
3732 	while (ztest_random(10) != 0)
3733 		ztest_io(zd, od[0].od_object, offset);
3734 }
3735 
3736 void
3737 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3738 {
3739 	ztest_od_t od[1];
3740 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3741 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3742 	uint64_t count = ztest_random(20) + 1;
3743 	uint64_t blocksize = ztest_random_blocksize();
3744 	void *data;
3745 
3746 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3747 
3748 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3749 		return;
3750 
3751 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
3752 		return;
3753 
3754 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
3755 
3756 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
3757 
3758 	while (ztest_random(count) != 0) {
3759 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
3760 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
3761 		    data) != 0)
3762 			break;
3763 		while (ztest_random(4) != 0)
3764 			ztest_io(zd, od[0].od_object, randoff);
3765 	}
3766 
3767 	umem_free(data, blocksize);
3768 }
3769 
3770 /*
3771  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3772  */
3773 #define	ZTEST_ZAP_MIN_INTS	1
3774 #define	ZTEST_ZAP_MAX_INTS	4
3775 #define	ZTEST_ZAP_MAX_PROPS	1000
3776 
3777 void
3778 ztest_zap(ztest_ds_t *zd, uint64_t id)
3779 {
3780 	objset_t *os = zd->zd_os;
3781 	ztest_od_t od[1];
3782 	uint64_t object;
3783 	uint64_t txg, last_txg;
3784 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3785 	uint64_t zl_ints, zl_intsize, prop;
3786 	int i, ints;
3787 	dmu_tx_t *tx;
3788 	char propname[100], txgname[100];
3789 	int error;
3790 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3791 
3792 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3793 
3794 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3795 		return;
3796 
3797 	object = od[0].od_object;
3798 
3799 	/*
3800 	 * Generate a known hash collision, and verify that
3801 	 * we can lookup and remove both entries.
3802 	 */
3803 	tx = dmu_tx_create(os);
3804 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3805 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3806 	if (txg == 0)
3807 		return;
3808 	for (i = 0; i < 2; i++) {
3809 		value[i] = i;
3810 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3811 		    1, &value[i], tx));
3812 	}
3813 	for (i = 0; i < 2; i++) {
3814 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3815 		    sizeof (uint64_t), 1, &value[i], tx));
3816 		VERIFY3U(0, ==,
3817 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3818 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3819 		ASSERT3U(zl_ints, ==, 1);
3820 	}
3821 	for (i = 0; i < 2; i++) {
3822 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3823 	}
3824 	dmu_tx_commit(tx);
3825 
3826 	/*
3827 	 * Generate a buch of random entries.
3828 	 */
3829 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3830 
3831 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3832 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3833 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3834 	bzero(value, sizeof (value));
3835 	last_txg = 0;
3836 
3837 	/*
3838 	 * If these zap entries already exist, validate their contents.
3839 	 */
3840 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3841 	if (error == 0) {
3842 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3843 		ASSERT3U(zl_ints, ==, 1);
3844 
3845 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3846 		    zl_ints, &last_txg) == 0);
3847 
3848 		VERIFY(zap_length(os, object, propname, &zl_intsize,
3849 		    &zl_ints) == 0);
3850 
3851 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3852 		ASSERT3U(zl_ints, ==, ints);
3853 
3854 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
3855 		    zl_ints, value) == 0);
3856 
3857 		for (i = 0; i < ints; i++) {
3858 			ASSERT3U(value[i], ==, last_txg + object + i);
3859 		}
3860 	} else {
3861 		ASSERT3U(error, ==, ENOENT);
3862 	}
3863 
3864 	/*
3865 	 * Atomically update two entries in our zap object.
3866 	 * The first is named txg_%llu, and contains the txg
3867 	 * in which the property was last updated.  The second
3868 	 * is named prop_%llu, and the nth element of its value
3869 	 * should be txg + object + n.
3870 	 */
3871 	tx = dmu_tx_create(os);
3872 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3873 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3874 	if (txg == 0)
3875 		return;
3876 
3877 	if (last_txg > txg)
3878 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
3879 
3880 	for (i = 0; i < ints; i++)
3881 		value[i] = txg + object + i;
3882 
3883 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3884 	    1, &txg, tx));
3885 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3886 	    ints, value, tx));
3887 
3888 	dmu_tx_commit(tx);
3889 
3890 	/*
3891 	 * Remove a random pair of entries.
3892 	 */
3893 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3894 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3895 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3896 
3897 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3898 
3899 	if (error == ENOENT)
3900 		return;
3901 
3902 	ASSERT3U(error, ==, 0);
3903 
3904 	tx = dmu_tx_create(os);
3905 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3906 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3907 	if (txg == 0)
3908 		return;
3909 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
3910 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3911 	dmu_tx_commit(tx);
3912 }
3913 
3914 /*
3915  * Testcase to test the upgrading of a microzap to fatzap.
3916  */
3917 void
3918 ztest_fzap(ztest_ds_t *zd, uint64_t id)
3919 {
3920 	objset_t *os = zd->zd_os;
3921 	ztest_od_t od[1];
3922 	uint64_t object, txg;
3923 
3924 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3925 
3926 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3927 		return;
3928 
3929 	object = od[0].od_object;
3930 
3931 	/*
3932 	 * Add entries to this ZAP and make sure it spills over
3933 	 * and gets upgraded to a fatzap. Also, since we are adding
3934 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
3935 	 */
3936 	for (int i = 0; i < 2050; i++) {
3937 		char name[MAXNAMELEN];
3938 		uint64_t value = i;
3939 		dmu_tx_t *tx;
3940 		int error;
3941 
3942 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
3943 		    id, value);
3944 
3945 		tx = dmu_tx_create(os);
3946 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
3947 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3948 		if (txg == 0)
3949 			return;
3950 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
3951 		    &value, tx);
3952 		ASSERT(error == 0 || error == EEXIST);
3953 		dmu_tx_commit(tx);
3954 	}
3955 }
3956 
3957 /* ARGSUSED */
3958 void
3959 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3960 {
3961 	objset_t *os = zd->zd_os;
3962 	ztest_od_t od[1];
3963 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3964 	dmu_tx_t *tx;
3965 	int i, namelen, error;
3966 	int micro = ztest_random(2);
3967 	char name[20], string_value[20];
3968 	void *data;
3969 
3970 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
3971 
3972 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3973 		return;
3974 
3975 	object = od[0].od_object;
3976 
3977 	/*
3978 	 * Generate a random name of the form 'xxx.....' where each
3979 	 * x is a random printable character and the dots are dots.
3980 	 * There are 94 such characters, and the name length goes from
3981 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
3982 	 */
3983 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
3984 
3985 	for (i = 0; i < 3; i++)
3986 		name[i] = '!' + ztest_random('~' - '!' + 1);
3987 	for (; i < namelen - 1; i++)
3988 		name[i] = '.';
3989 	name[i] = '\0';
3990 
3991 	if ((namelen & 1) || micro) {
3992 		wsize = sizeof (txg);
3993 		wc = 1;
3994 		data = &txg;
3995 	} else {
3996 		wsize = 1;
3997 		wc = namelen;
3998 		data = string_value;
3999 	}
4000 
4001 	count = -1ULL;
4002 	VERIFY(zap_count(os, object, &count) == 0);
4003 	ASSERT(count != -1ULL);
4004 
4005 	/*
4006 	 * Select an operation: length, lookup, add, update, remove.
4007 	 */
4008 	i = ztest_random(5);
4009 
4010 	if (i >= 2) {
4011 		tx = dmu_tx_create(os);
4012 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4013 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4014 		if (txg == 0)
4015 			return;
4016 		bcopy(name, string_value, namelen);
4017 	} else {
4018 		tx = NULL;
4019 		txg = 0;
4020 		bzero(string_value, namelen);
4021 	}
4022 
4023 	switch (i) {
4024 
4025 	case 0:
4026 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4027 		if (error == 0) {
4028 			ASSERT3U(wsize, ==, zl_wsize);
4029 			ASSERT3U(wc, ==, zl_wc);
4030 		} else {
4031 			ASSERT3U(error, ==, ENOENT);
4032 		}
4033 		break;
4034 
4035 	case 1:
4036 		error = zap_lookup(os, object, name, wsize, wc, data);
4037 		if (error == 0) {
4038 			if (data == string_value &&
4039 			    bcmp(name, data, namelen) != 0)
4040 				fatal(0, "name '%s' != val '%s' len %d",
4041 				    name, data, namelen);
4042 		} else {
4043 			ASSERT3U(error, ==, ENOENT);
4044 		}
4045 		break;
4046 
4047 	case 2:
4048 		error = zap_add(os, object, name, wsize, wc, data, tx);
4049 		ASSERT(error == 0 || error == EEXIST);
4050 		break;
4051 
4052 	case 3:
4053 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4054 		break;
4055 
4056 	case 4:
4057 		error = zap_remove(os, object, name, tx);
4058 		ASSERT(error == 0 || error == ENOENT);
4059 		break;
4060 	}
4061 
4062 	if (tx != NULL)
4063 		dmu_tx_commit(tx);
4064 }
4065 
4066 /*
4067  * Commit callback data.
4068  */
4069 typedef struct ztest_cb_data {
4070 	list_node_t		zcd_node;
4071 	uint64_t		zcd_txg;
4072 	int			zcd_expected_err;
4073 	boolean_t		zcd_added;
4074 	boolean_t		zcd_called;
4075 	spa_t			*zcd_spa;
4076 } ztest_cb_data_t;
4077 
4078 /* This is the actual commit callback function */
4079 static void
4080 ztest_commit_callback(void *arg, int error)
4081 {
4082 	ztest_cb_data_t *data = arg;
4083 	uint64_t synced_txg;
4084 
4085 	VERIFY(data != NULL);
4086 	VERIFY3S(data->zcd_expected_err, ==, error);
4087 	VERIFY(!data->zcd_called);
4088 
4089 	synced_txg = spa_last_synced_txg(data->zcd_spa);
4090 	if (data->zcd_txg > synced_txg)
4091 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4092 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4093 		    synced_txg);
4094 
4095 	data->zcd_called = B_TRUE;
4096 
4097 	if (error == ECANCELED) {
4098 		ASSERT3U(data->zcd_txg, ==, 0);
4099 		ASSERT(!data->zcd_added);
4100 
4101 		/*
4102 		 * The private callback data should be destroyed here, but
4103 		 * since we are going to check the zcd_called field after
4104 		 * dmu_tx_abort(), we will destroy it there.
4105 		 */
4106 		return;
4107 	}
4108 
4109 	/* Was this callback added to the global callback list? */
4110 	if (!data->zcd_added)
4111 		goto out;
4112 
4113 	ASSERT3U(data->zcd_txg, !=, 0);
4114 
4115 	/* Remove our callback from the list */
4116 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
4117 	list_remove(&zcl.zcl_callbacks, data);
4118 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
4119 
4120 out:
4121 	umem_free(data, sizeof (ztest_cb_data_t));
4122 }
4123 
4124 /* Allocate and initialize callback data structure */
4125 static ztest_cb_data_t *
4126 ztest_create_cb_data(objset_t *os, uint64_t txg)
4127 {
4128 	ztest_cb_data_t *cb_data;
4129 
4130 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4131 
4132 	cb_data->zcd_txg = txg;
4133 	cb_data->zcd_spa = dmu_objset_spa(os);
4134 
4135 	return (cb_data);
4136 }
4137 
4138 /*
4139  * If a number of txgs equal to this threshold have been created after a commit
4140  * callback has been registered but not called, then we assume there is an
4141  * implementation bug.
4142  */
4143 #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
4144 
4145 /*
4146  * Commit callback test.
4147  */
4148 void
4149 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4150 {
4151 	objset_t *os = zd->zd_os;
4152 	ztest_od_t od[1];
4153 	dmu_tx_t *tx;
4154 	ztest_cb_data_t *cb_data[3], *tmp_cb;
4155 	uint64_t old_txg, txg;
4156 	int i, error;
4157 
4158 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4159 
4160 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4161 		return;
4162 
4163 	tx = dmu_tx_create(os);
4164 
4165 	cb_data[0] = ztest_create_cb_data(os, 0);
4166 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4167 
4168 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4169 
4170 	/* Every once in a while, abort the transaction on purpose */
4171 	if (ztest_random(100) == 0)
4172 		error = -1;
4173 
4174 	if (!error)
4175 		error = dmu_tx_assign(tx, TXG_NOWAIT);
4176 
4177 	txg = error ? 0 : dmu_tx_get_txg(tx);
4178 
4179 	cb_data[0]->zcd_txg = txg;
4180 	cb_data[1] = ztest_create_cb_data(os, txg);
4181 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4182 
4183 	if (error) {
4184 		/*
4185 		 * It's not a strict requirement to call the registered
4186 		 * callbacks from inside dmu_tx_abort(), but that's what
4187 		 * it's supposed to happen in the current implementation
4188 		 * so we will check for that.
4189 		 */
4190 		for (i = 0; i < 2; i++) {
4191 			cb_data[i]->zcd_expected_err = ECANCELED;
4192 			VERIFY(!cb_data[i]->zcd_called);
4193 		}
4194 
4195 		dmu_tx_abort(tx);
4196 
4197 		for (i = 0; i < 2; i++) {
4198 			VERIFY(cb_data[i]->zcd_called);
4199 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4200 		}
4201 
4202 		return;
4203 	}
4204 
4205 	cb_data[2] = ztest_create_cb_data(os, txg);
4206 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4207 
4208 	/*
4209 	 * Read existing data to make sure there isn't a future leak.
4210 	 */
4211 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4212 	    &old_txg, DMU_READ_PREFETCH));
4213 
4214 	if (old_txg > txg)
4215 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4216 		    old_txg, txg);
4217 
4218 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4219 
4220 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
4221 
4222 	/*
4223 	 * Since commit callbacks don't have any ordering requirement and since
4224 	 * it is theoretically possible for a commit callback to be called
4225 	 * after an arbitrary amount of time has elapsed since its txg has been
4226 	 * synced, it is difficult to reliably determine whether a commit
4227 	 * callback hasn't been called due to high load or due to a flawed
4228 	 * implementation.
4229 	 *
4230 	 * In practice, we will assume that if after a certain number of txgs a
4231 	 * commit callback hasn't been called, then most likely there's an
4232 	 * implementation bug..
4233 	 */
4234 	tmp_cb = list_head(&zcl.zcl_callbacks);
4235 	if (tmp_cb != NULL &&
4236 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
4237 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4238 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4239 	}
4240 
4241 	/*
4242 	 * Let's find the place to insert our callbacks.
4243 	 *
4244 	 * Even though the list is ordered by txg, it is possible for the
4245 	 * insertion point to not be the end because our txg may already be
4246 	 * quiescing at this point and other callbacks in the open txg
4247 	 * (from other objsets) may have sneaked in.
4248 	 */
4249 	tmp_cb = list_tail(&zcl.zcl_callbacks);
4250 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4251 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4252 
4253 	/* Add the 3 callbacks to the list */
4254 	for (i = 0; i < 3; i++) {
4255 		if (tmp_cb == NULL)
4256 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4257 		else
4258 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4259 			    cb_data[i]);
4260 
4261 		cb_data[i]->zcd_added = B_TRUE;
4262 		VERIFY(!cb_data[i]->zcd_called);
4263 
4264 		tmp_cb = cb_data[i];
4265 	}
4266 
4267 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
4268 
4269 	dmu_tx_commit(tx);
4270 }
4271 
4272 /* ARGSUSED */
4273 void
4274 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4275 {
4276 	zfs_prop_t proplist[] = {
4277 		ZFS_PROP_CHECKSUM,
4278 		ZFS_PROP_COMPRESSION,
4279 		ZFS_PROP_COPIES,
4280 		ZFS_PROP_DEDUP
4281 	};
4282 	ztest_shared_t *zs = ztest_shared;
4283 
4284 	(void) rw_rdlock(&zs->zs_name_lock);
4285 
4286 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4287 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4288 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4289 
4290 	(void) rw_unlock(&zs->zs_name_lock);
4291 }
4292 
4293 /* ARGSUSED */
4294 void
4295 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4296 {
4297 	ztest_shared_t *zs = ztest_shared;
4298 	nvlist_t *props = NULL;
4299 
4300 	(void) rw_rdlock(&zs->zs_name_lock);
4301 
4302 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4303 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4304 
4305 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4306 
4307 	if (zopt_verbose >= 6)
4308 		dump_nvlist(props, 4);
4309 
4310 	nvlist_free(props);
4311 
4312 	(void) rw_unlock(&zs->zs_name_lock);
4313 }
4314 
4315 /*
4316  * Test snapshot hold/release and deferred destroy.
4317  */
4318 void
4319 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4320 {
4321 	int error;
4322 	objset_t *os = zd->zd_os;
4323 	objset_t *origin;
4324 	char snapname[100];
4325 	char fullname[100];
4326 	char clonename[100];
4327 	char tag[100];
4328 	char osname[MAXNAMELEN];
4329 
4330 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
4331 
4332 	dmu_objset_name(os, osname);
4333 
4334 	(void) snprintf(snapname, 100, "sh1_%llu", id);
4335 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4336 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
4337 	(void) snprintf(tag, 100, "%tag_%llu", id);
4338 
4339 	/*
4340 	 * Clean up from any previous run.
4341 	 */
4342 	(void) dmu_objset_destroy(clonename, B_FALSE);
4343 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4344 	(void) dmu_objset_destroy(fullname, B_FALSE);
4345 
4346 	/*
4347 	 * Create snapshot, clone it, mark snap for deferred destroy,
4348 	 * destroy clone, verify snap was also destroyed.
4349 	 */
4350 	error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4351 	    FALSE, -1);
4352 	if (error) {
4353 		if (error == ENOSPC) {
4354 			ztest_record_enospc("dmu_objset_snapshot");
4355 			goto out;
4356 		}
4357 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4358 	}
4359 
4360 	error = dmu_objset_hold(fullname, FTAG, &origin);
4361 	if (error)
4362 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4363 
4364 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4365 	dmu_objset_rele(origin, FTAG);
4366 	if (error) {
4367 		if (error == ENOSPC) {
4368 			ztest_record_enospc("dmu_objset_clone");
4369 			goto out;
4370 		}
4371 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4372 	}
4373 
4374 	error = dmu_objset_destroy(fullname, B_TRUE);
4375 	if (error) {
4376 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4377 		    fullname, error);
4378 	}
4379 
4380 	error = dmu_objset_destroy(clonename, B_FALSE);
4381 	if (error)
4382 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4383 
4384 	error = dmu_objset_hold(fullname, FTAG, &origin);
4385 	if (error != ENOENT)
4386 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4387 
4388 	/*
4389 	 * Create snapshot, add temporary hold, verify that we can't
4390 	 * destroy a held snapshot, mark for deferred destroy,
4391 	 * release hold, verify snapshot was destroyed.
4392 	 */
4393 	error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4394 	    FALSE, -1);
4395 	if (error) {
4396 		if (error == ENOSPC) {
4397 			ztest_record_enospc("dmu_objset_snapshot");
4398 			goto out;
4399 		}
4400 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4401 	}
4402 
4403 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4404 	    B_TRUE, -1);
4405 	if (error)
4406 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4407 
4408 	error = dmu_objset_destroy(fullname, B_FALSE);
4409 	if (error != EBUSY) {
4410 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4411 		    fullname, error);
4412 	}
4413 
4414 	error = dmu_objset_destroy(fullname, B_TRUE);
4415 	if (error) {
4416 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4417 		    fullname, error);
4418 	}
4419 
4420 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4421 	if (error)
4422 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4423 
4424 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4425 
4426 out:
4427 	(void) rw_unlock(&ztest_shared->zs_name_lock);
4428 }
4429 
4430 /*
4431  * Inject random faults into the on-disk data.
4432  */
4433 /* ARGSUSED */
4434 void
4435 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4436 {
4437 	ztest_shared_t *zs = ztest_shared;
4438 	spa_t *spa = zs->zs_spa;
4439 	int fd;
4440 	uint64_t offset;
4441 	uint64_t leaves;
4442 	uint64_t bad = 0x1990c0ffeedecade;
4443 	uint64_t top, leaf;
4444 	char path0[MAXPATHLEN];
4445 	char pathrand[MAXPATHLEN];
4446 	size_t fsize;
4447 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4448 	int iters = 1000;
4449 	int maxfaults;
4450 	int mirror_save;
4451 	vdev_t *vd0 = NULL;
4452 	uint64_t guid0 = 0;
4453 	boolean_t islog = B_FALSE;
4454 
4455 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4456 	maxfaults = MAXFAULTS();
4457 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4458 	mirror_save = zs->zs_mirrors;
4459 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4460 
4461 	ASSERT(leaves >= 1);
4462 
4463 	/*
4464 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4465 	 */
4466 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4467 
4468 	if (ztest_random(2) == 0) {
4469 		/*
4470 		 * Inject errors on a normal data device or slog device.
4471 		 */
4472 		top = ztest_random_vdev_top(spa, B_TRUE);
4473 		leaf = ztest_random(leaves) + zs->zs_splits;
4474 
4475 		/*
4476 		 * Generate paths to the first leaf in this top-level vdev,
4477 		 * and to the random leaf we selected.  We'll induce transient
4478 		 * write failures and random online/offline activity on leaf 0,
4479 		 * and we'll write random garbage to the randomly chosen leaf.
4480 		 */
4481 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
4482 		    zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
4483 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4484 		    zopt_dir, zopt_pool, top * leaves + leaf);
4485 
4486 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4487 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4488 			islog = B_TRUE;
4489 
4490 		if (vd0 != NULL && maxfaults != 1) {
4491 			/*
4492 			 * Make vd0 explicitly claim to be unreadable,
4493 			 * or unwriteable, or reach behind its back
4494 			 * and close the underlying fd.  We can do this if
4495 			 * maxfaults == 0 because we'll fail and reexecute,
4496 			 * and we can do it if maxfaults >= 2 because we'll
4497 			 * have enough redundancy.  If maxfaults == 1, the
4498 			 * combination of this with injection of random data
4499 			 * corruption below exceeds the pool's fault tolerance.
4500 			 */
4501 			vdev_file_t *vf = vd0->vdev_tsd;
4502 
4503 			if (vf != NULL && ztest_random(3) == 0) {
4504 				(void) close(vf->vf_vnode->v_fd);
4505 				vf->vf_vnode->v_fd = -1;
4506 			} else if (ztest_random(2) == 0) {
4507 				vd0->vdev_cant_read = B_TRUE;
4508 			} else {
4509 				vd0->vdev_cant_write = B_TRUE;
4510 			}
4511 			guid0 = vd0->vdev_guid;
4512 		}
4513 	} else {
4514 		/*
4515 		 * Inject errors on an l2cache device.
4516 		 */
4517 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
4518 
4519 		if (sav->sav_count == 0) {
4520 			spa_config_exit(spa, SCL_STATE, FTAG);
4521 			return;
4522 		}
4523 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4524 		guid0 = vd0->vdev_guid;
4525 		(void) strcpy(path0, vd0->vdev_path);
4526 		(void) strcpy(pathrand, vd0->vdev_path);
4527 
4528 		leaf = 0;
4529 		leaves = 1;
4530 		maxfaults = INT_MAX;	/* no limit on cache devices */
4531 	}
4532 
4533 	spa_config_exit(spa, SCL_STATE, FTAG);
4534 
4535 	/*
4536 	 * If we can tolerate two or more faults, or we're dealing
4537 	 * with a slog, randomly online/offline vd0.
4538 	 */
4539 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
4540 		if (ztest_random(10) < 6) {
4541 			int flags = (ztest_random(2) == 0 ?
4542 			    ZFS_OFFLINE_TEMPORARY : 0);
4543 
4544 			/*
4545 			 * We have to grab the zs_name_lock as writer to
4546 			 * prevent a race between offlining a slog and
4547 			 * destroying a dataset. Offlining the slog will
4548 			 * grab a reference on the dataset which may cause
4549 			 * dmu_objset_destroy() to fail with EBUSY thus
4550 			 * leaving the dataset in an inconsistent state.
4551 			 */
4552 			if (islog)
4553 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
4554 
4555 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4556 
4557 			if (islog)
4558 				(void) rw_unlock(&ztest_shared->zs_name_lock);
4559 		} else {
4560 			(void) vdev_online(spa, guid0, 0, NULL);
4561 		}
4562 	}
4563 
4564 	if (maxfaults == 0)
4565 		return;
4566 
4567 	/*
4568 	 * We have at least single-fault tolerance, so inject data corruption.
4569 	 */
4570 	fd = open(pathrand, O_RDWR);
4571 
4572 	if (fd == -1)	/* we hit a gap in the device namespace */
4573 		return;
4574 
4575 	fsize = lseek(fd, 0, SEEK_END);
4576 
4577 	while (--iters != 0) {
4578 		offset = ztest_random(fsize / (leaves << bshift)) *
4579 		    (leaves << bshift) + (leaf << bshift) +
4580 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4581 
4582 		if (offset >= fsize)
4583 			continue;
4584 
4585 		VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4586 		if (mirror_save != zs->zs_mirrors) {
4587 			VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4588 			(void) close(fd);
4589 			return;
4590 		}
4591 
4592 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4593 			fatal(1, "can't inject bad word at 0x%llx in %s",
4594 			    offset, pathrand);
4595 
4596 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4597 
4598 		if (zopt_verbose >= 7)
4599 			(void) printf("injected bad word into %s,"
4600 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4601 	}
4602 
4603 	(void) close(fd);
4604 }
4605 
4606 /*
4607  * Verify that DDT repair works as expected.
4608  */
4609 void
4610 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4611 {
4612 	ztest_shared_t *zs = ztest_shared;
4613 	spa_t *spa = zs->zs_spa;
4614 	objset_t *os = zd->zd_os;
4615 	ztest_od_t od[1];
4616 	uint64_t object, blocksize, txg, pattern, psize;
4617 	enum zio_checksum checksum = spa_dedup_checksum(spa);
4618 	dmu_buf_t *db;
4619 	dmu_tx_t *tx;
4620 	void *buf;
4621 	blkptr_t blk;
4622 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
4623 
4624 	blocksize = ztest_random_blocksize();
4625 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
4626 
4627 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4628 
4629 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4630 		return;
4631 
4632 	/*
4633 	 * Take the name lock as writer to prevent anyone else from changing
4634 	 * the pool and dataset properies we need to maintain during this test.
4635 	 */
4636 	(void) rw_wrlock(&zs->zs_name_lock);
4637 
4638 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4639 	    B_FALSE) != 0 ||
4640 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4641 	    B_FALSE) != 0) {
4642 		(void) rw_unlock(&zs->zs_name_lock);
4643 		return;
4644 	}
4645 
4646 	object = od[0].od_object;
4647 	blocksize = od[0].od_blocksize;
4648 	pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
4649 
4650 	ASSERT(object != 0);
4651 
4652 	tx = dmu_tx_create(os);
4653 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4654 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4655 	if (txg == 0) {
4656 		(void) rw_unlock(&zs->zs_name_lock);
4657 		return;
4658 	}
4659 
4660 	/*
4661 	 * Write all the copies of our block.
4662 	 */
4663 	for (int i = 0; i < copies; i++) {
4664 		uint64_t offset = i * blocksize;
4665 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4666 		    DMU_READ_NO_PREFETCH) == 0);
4667 		ASSERT(db->db_offset == offset);
4668 		ASSERT(db->db_size == blocksize);
4669 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4670 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4671 		dmu_buf_will_fill(db, tx);
4672 		ztest_pattern_set(db->db_data, db->db_size, pattern);
4673 		dmu_buf_rele(db, FTAG);
4674 	}
4675 
4676 	dmu_tx_commit(tx);
4677 	txg_wait_synced(spa_get_dsl(spa), txg);
4678 
4679 	/*
4680 	 * Find out what block we got.
4681 	 */
4682 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4683 	    DMU_READ_NO_PREFETCH) == 0);
4684 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4685 	dmu_buf_rele(db, FTAG);
4686 
4687 	/*
4688 	 * Damage the block.  Dedup-ditto will save us when we read it later.
4689 	 */
4690 	psize = BP_GET_PSIZE(&blk);
4691 	buf = zio_buf_alloc(psize);
4692 	ztest_pattern_set(buf, psize, ~pattern);
4693 
4694 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4695 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4696 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4697 
4698 	zio_buf_free(buf, psize);
4699 
4700 	(void) rw_unlock(&zs->zs_name_lock);
4701 }
4702 
4703 /*
4704  * Scrub the pool.
4705  */
4706 /* ARGSUSED */
4707 void
4708 ztest_scrub(ztest_ds_t *zd, uint64_t id)
4709 {
4710 	ztest_shared_t *zs = ztest_shared;
4711 	spa_t *spa = zs->zs_spa;
4712 
4713 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
4714 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4715 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
4716 }
4717 
4718 /*
4719  * Change the guid for the pool.
4720  */
4721 /* ARGSUSED */
4722 void
4723 ztest_reguid(ztest_ds_t *zd, uint64_t id)
4724 {
4725 	ztest_shared_t *zs = ztest_shared;
4726 	spa_t *spa = zs->zs_spa;
4727 	uint64_t orig, load;
4728 
4729 	orig = spa_guid(spa);
4730 	load = spa_load_guid(spa);
4731 	if (spa_change_guid(spa) != 0)
4732 		return;
4733 
4734 	if (zopt_verbose >= 3) {
4735 		(void) printf("Changed guid old %llu -> %llu\n",
4736 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
4737 	}
4738 
4739 	VERIFY3U(orig, !=, spa_guid(spa));
4740 	VERIFY3U(load, ==, spa_load_guid(spa));
4741 }
4742 
4743 /*
4744  * Rename the pool to a different name and then rename it back.
4745  */
4746 /* ARGSUSED */
4747 void
4748 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4749 {
4750 	ztest_shared_t *zs = ztest_shared;
4751 	char *oldname, *newname;
4752 	spa_t *spa;
4753 
4754 	(void) rw_wrlock(&zs->zs_name_lock);
4755 
4756 	oldname = zs->zs_pool;
4757 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4758 	(void) strcpy(newname, oldname);
4759 	(void) strcat(newname, "_tmp");
4760 
4761 	/*
4762 	 * Do the rename
4763 	 */
4764 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4765 
4766 	/*
4767 	 * Try to open it under the old name, which shouldn't exist
4768 	 */
4769 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4770 
4771 	/*
4772 	 * Open it under the new name and make sure it's still the same spa_t.
4773 	 */
4774 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4775 
4776 	ASSERT(spa == zs->zs_spa);
4777 	spa_close(spa, FTAG);
4778 
4779 	/*
4780 	 * Rename it back to the original
4781 	 */
4782 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4783 
4784 	/*
4785 	 * Make sure it can still be opened
4786 	 */
4787 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4788 
4789 	ASSERT(spa == zs->zs_spa);
4790 	spa_close(spa, FTAG);
4791 
4792 	umem_free(newname, strlen(newname) + 1);
4793 
4794 	(void) rw_unlock(&zs->zs_name_lock);
4795 }
4796 
4797 /*
4798  * Verify pool integrity by running zdb.
4799  */
4800 static void
4801 ztest_run_zdb(char *pool)
4802 {
4803 	int status;
4804 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4805 	char zbuf[1024];
4806 	char *bin;
4807 	char *ztest;
4808 	char *isa;
4809 	int isalen;
4810 	FILE *fp;
4811 
4812 	(void) realpath(getexecname(), zdb);
4813 
4814 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4815 	bin = strstr(zdb, "/usr/bin/");
4816 	ztest = strstr(bin, "/ztest");
4817 	isa = bin + 8;
4818 	isalen = ztest - isa;
4819 	isa = strdup(isa);
4820 	/* LINTED */
4821 	(void) sprintf(bin,
4822 	    "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
4823 	    isalen,
4824 	    isa,
4825 	    zopt_verbose >= 3 ? "s" : "",
4826 	    zopt_verbose >= 4 ? "v" : "",
4827 	    spa_config_path,
4828 	    pool);
4829 	free(isa);
4830 
4831 	if (zopt_verbose >= 5)
4832 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4833 
4834 	fp = popen(zdb, "r");
4835 
4836 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4837 		if (zopt_verbose >= 3)
4838 			(void) printf("%s", zbuf);
4839 
4840 	status = pclose(fp);
4841 
4842 	if (status == 0)
4843 		return;
4844 
4845 	ztest_dump_core = 0;
4846 	if (WIFEXITED(status))
4847 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4848 	else
4849 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4850 }
4851 
4852 static void
4853 ztest_walk_pool_directory(char *header)
4854 {
4855 	spa_t *spa = NULL;
4856 
4857 	if (zopt_verbose >= 6)
4858 		(void) printf("%s\n", header);
4859 
4860 	mutex_enter(&spa_namespace_lock);
4861 	while ((spa = spa_next(spa)) != NULL)
4862 		if (zopt_verbose >= 6)
4863 			(void) printf("\t%s\n", spa_name(spa));
4864 	mutex_exit(&spa_namespace_lock);
4865 }
4866 
4867 static void
4868 ztest_spa_import_export(char *oldname, char *newname)
4869 {
4870 	nvlist_t *config, *newconfig;
4871 	uint64_t pool_guid;
4872 	spa_t *spa;
4873 
4874 	if (zopt_verbose >= 4) {
4875 		(void) printf("import/export: old = %s, new = %s\n",
4876 		    oldname, newname);
4877 	}
4878 
4879 	/*
4880 	 * Clean up from previous runs.
4881 	 */
4882 	(void) spa_destroy(newname);
4883 
4884 	/*
4885 	 * Get the pool's configuration and guid.
4886 	 */
4887 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4888 
4889 	/*
4890 	 * Kick off a scrub to tickle scrub/export races.
4891 	 */
4892 	if (ztest_random(2) == 0)
4893 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
4894 
4895 	pool_guid = spa_guid(spa);
4896 	spa_close(spa, FTAG);
4897 
4898 	ztest_walk_pool_directory("pools before export");
4899 
4900 	/*
4901 	 * Export it.
4902 	 */
4903 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4904 
4905 	ztest_walk_pool_directory("pools after export");
4906 
4907 	/*
4908 	 * Try to import it.
4909 	 */
4910 	newconfig = spa_tryimport(config);
4911 	ASSERT(newconfig != NULL);
4912 	nvlist_free(newconfig);
4913 
4914 	/*
4915 	 * Import it under the new name.
4916 	 */
4917 	VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
4918 
4919 	ztest_walk_pool_directory("pools after import");
4920 
4921 	/*
4922 	 * Try to import it again -- should fail with EEXIST.
4923 	 */
4924 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
4925 
4926 	/*
4927 	 * Try to import it under a different name -- should fail with EEXIST.
4928 	 */
4929 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
4930 
4931 	/*
4932 	 * Verify that the pool is no longer visible under the old name.
4933 	 */
4934 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4935 
4936 	/*
4937 	 * Verify that we can open and close the pool using the new name.
4938 	 */
4939 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4940 	ASSERT(pool_guid == spa_guid(spa));
4941 	spa_close(spa, FTAG);
4942 
4943 	nvlist_free(config);
4944 }
4945 
4946 static void
4947 ztest_resume(spa_t *spa)
4948 {
4949 	if (spa_suspended(spa) && zopt_verbose >= 6)
4950 		(void) printf("resuming from suspended state\n");
4951 	spa_vdev_state_enter(spa, SCL_NONE);
4952 	vdev_clear(spa, NULL);
4953 	(void) spa_vdev_state_exit(spa, NULL, 0);
4954 	(void) zio_resume(spa);
4955 }
4956 
4957 static void *
4958 ztest_resume_thread(void *arg)
4959 {
4960 	spa_t *spa = arg;
4961 
4962 	while (!ztest_exiting) {
4963 		if (spa_suspended(spa))
4964 			ztest_resume(spa);
4965 		(void) poll(NULL, 0, 100);
4966 	}
4967 	return (NULL);
4968 }
4969 
4970 static void *
4971 ztest_deadman_thread(void *arg)
4972 {
4973 	ztest_shared_t *zs = arg;
4974 	int grace = 300;
4975 	hrtime_t delta;
4976 
4977 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
4978 
4979 	(void) poll(NULL, 0, (int)(1000 * delta));
4980 
4981 	fatal(0, "failed to complete within %d seconds of deadline", grace);
4982 
4983 	return (NULL);
4984 }
4985 
4986 static void
4987 ztest_execute(ztest_info_t *zi, uint64_t id)
4988 {
4989 	ztest_shared_t *zs = ztest_shared;
4990 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
4991 	hrtime_t functime = gethrtime();
4992 
4993 	for (int i = 0; i < zi->zi_iters; i++)
4994 		zi->zi_func(zd, id);
4995 
4996 	functime = gethrtime() - functime;
4997 
4998 	atomic_add_64(&zi->zi_call_count, 1);
4999 	atomic_add_64(&zi->zi_call_time, functime);
5000 
5001 	if (zopt_verbose >= 4) {
5002 		Dl_info dli;
5003 		(void) dladdr((void *)zi->zi_func, &dli);
5004 		(void) printf("%6.2f sec in %s\n",
5005 		    (double)functime / NANOSEC, dli.dli_sname);
5006 	}
5007 }
5008 
5009 static void *
5010 ztest_thread(void *arg)
5011 {
5012 	uint64_t id = (uintptr_t)arg;
5013 	ztest_shared_t *zs = ztest_shared;
5014 	uint64_t call_next;
5015 	hrtime_t now;
5016 	ztest_info_t *zi;
5017 
5018 	while ((now = gethrtime()) < zs->zs_thread_stop) {
5019 		/*
5020 		 * See if it's time to force a crash.
5021 		 */
5022 		if (now > zs->zs_thread_kill)
5023 			ztest_kill(zs);
5024 
5025 		/*
5026 		 * If we're getting ENOSPC with some regularity, stop.
5027 		 */
5028 		if (zs->zs_enospc_count > 10)
5029 			break;
5030 
5031 		/*
5032 		 * Pick a random function to execute.
5033 		 */
5034 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
5035 		call_next = zi->zi_call_next;
5036 
5037 		if (now >= call_next &&
5038 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
5039 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
5040 			ztest_execute(zi, id);
5041 	}
5042 
5043 	return (NULL);
5044 }
5045 
5046 static void
5047 ztest_dataset_name(char *dsname, char *pool, int d)
5048 {
5049 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5050 }
5051 
5052 static void
5053 ztest_dataset_destroy(ztest_shared_t *zs, int d)
5054 {
5055 	char name[MAXNAMELEN];
5056 
5057 	ztest_dataset_name(name, zs->zs_pool, d);
5058 
5059 	if (zopt_verbose >= 3)
5060 		(void) printf("Destroying %s to free up space\n", name);
5061 
5062 	/*
5063 	 * Cleanup any non-standard clones and snapshots.  In general,
5064 	 * ztest thread t operates on dataset (t % zopt_datasets),
5065 	 * so there may be more than one thing to clean up.
5066 	 */
5067 	for (int t = d; t < zopt_threads; t += zopt_datasets)
5068 		ztest_dsl_dataset_cleanup(name, t);
5069 
5070 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5071 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5072 }
5073 
5074 static void
5075 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5076 {
5077 	uint64_t usedobjs, dirobjs, scratch;
5078 
5079 	/*
5080 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
5081 	 * Therefore, the number of objects in use should equal the
5082 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5083 	 * If not, we have an object leak.
5084 	 *
5085 	 * Note that we can only check this in ztest_dataset_open(),
5086 	 * when the open-context and syncing-context values agree.
5087 	 * That's because zap_count() returns the open-context value,
5088 	 * while dmu_objset_space() returns the rootbp fill count.
5089 	 */
5090 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5091 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5092 	ASSERT3U(dirobjs + 1, ==, usedobjs);
5093 }
5094 
5095 static int
5096 ztest_dataset_open(ztest_shared_t *zs, int d)
5097 {
5098 	ztest_ds_t *zd = &zs->zs_zd[d];
5099 	uint64_t committed_seq = zd->zd_seq;
5100 	objset_t *os;
5101 	zilog_t *zilog;
5102 	char name[MAXNAMELEN];
5103 	int error;
5104 
5105 	ztest_dataset_name(name, zs->zs_pool, d);
5106 
5107 	(void) rw_rdlock(&zs->zs_name_lock);
5108 
5109 	error = ztest_dataset_create(name);
5110 	if (error == ENOSPC) {
5111 		(void) rw_unlock(&zs->zs_name_lock);
5112 		ztest_record_enospc(FTAG);
5113 		return (error);
5114 	}
5115 	ASSERT(error == 0 || error == EEXIST);
5116 
5117 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5118 	(void) rw_unlock(&zs->zs_name_lock);
5119 
5120 	ztest_zd_init(zd, os);
5121 
5122 	zilog = zd->zd_zilog;
5123 
5124 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5125 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
5126 		fatal(0, "missing log records: claimed %llu < committed %llu",
5127 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
5128 
5129 	ztest_dataset_dirobj_verify(zd);
5130 
5131 	zil_replay(os, zd, ztest_replay_vector);
5132 
5133 	ztest_dataset_dirobj_verify(zd);
5134 
5135 	if (zopt_verbose >= 6)
5136 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5137 		    zd->zd_name,
5138 		    (u_longlong_t)zilog->zl_parse_blk_count,
5139 		    (u_longlong_t)zilog->zl_parse_lr_count,
5140 		    (u_longlong_t)zilog->zl_replaying_seq);
5141 
5142 	zilog = zil_open(os, ztest_get_data);
5143 
5144 	if (zilog->zl_replaying_seq != 0 &&
5145 	    zilog->zl_replaying_seq < committed_seq)
5146 		fatal(0, "missing log records: replayed %llu < committed %llu",
5147 		    zilog->zl_replaying_seq, committed_seq);
5148 
5149 	return (0);
5150 }
5151 
5152 static void
5153 ztest_dataset_close(ztest_shared_t *zs, int d)
5154 {
5155 	ztest_ds_t *zd = &zs->zs_zd[d];
5156 
5157 	zil_close(zd->zd_zilog);
5158 	dmu_objset_rele(zd->zd_os, zd);
5159 
5160 	ztest_zd_fini(zd);
5161 }
5162 
5163 /*
5164  * Kick off threads to run tests on all datasets in parallel.
5165  */
5166 static void
5167 ztest_run(ztest_shared_t *zs)
5168 {
5169 	thread_t *tid;
5170 	spa_t *spa;
5171 	objset_t *os;
5172 	thread_t resume_tid;
5173 	int error;
5174 
5175 	ztest_exiting = B_FALSE;
5176 
5177 	/*
5178 	 * Initialize parent/child shared state.
5179 	 */
5180 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5181 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5182 
5183 	zs->zs_thread_start = gethrtime();
5184 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5185 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5186 	zs->zs_thread_kill = zs->zs_thread_stop;
5187 	if (ztest_random(100) < zopt_killrate)
5188 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
5189 
5190 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
5191 
5192 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5193 	    offsetof(ztest_cb_data_t, zcd_node));
5194 
5195 	/*
5196 	 * Open our pool.
5197 	 */
5198 	kernel_init(FREAD | FWRITE);
5199 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5200 	spa->spa_debug = B_TRUE;
5201 	zs->zs_spa = spa;
5202 
5203 	VERIFY3U(0, ==, dmu_objset_hold(zs->zs_pool, FTAG, &os));
5204 	zs->zs_guid = dmu_objset_fsid_guid(os);
5205 	dmu_objset_rele(os, FTAG);
5206 
5207 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5208 
5209 	/*
5210 	 * We don't expect the pool to suspend unless maxfaults == 0,
5211 	 * in which case ztest_fault_inject() temporarily takes away
5212 	 * the only valid replica.
5213 	 */
5214 	if (MAXFAULTS() == 0)
5215 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5216 	else
5217 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5218 
5219 	/*
5220 	 * Create a thread to periodically resume suspended I/O.
5221 	 */
5222 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
5223 	    &resume_tid) == 0);
5224 
5225 	/*
5226 	 * Create a deadman thread to abort() if we hang.
5227 	 */
5228 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5229 	    NULL) == 0);
5230 
5231 	/*
5232 	 * Verify that we can safely inquire about about any object,
5233 	 * whether it's allocated or not.  To make it interesting,
5234 	 * we probe a 5-wide window around each power of two.
5235 	 * This hits all edge cases, including zero and the max.
5236 	 */
5237 	for (int t = 0; t < 64; t++) {
5238 		for (int d = -5; d <= 5; d++) {
5239 			error = dmu_object_info(spa->spa_meta_objset,
5240 			    (1ULL << t) + d, NULL);
5241 			ASSERT(error == 0 || error == ENOENT ||
5242 			    error == EINVAL);
5243 		}
5244 	}
5245 
5246 	/*
5247 	 * If we got any ENOSPC errors on the previous run, destroy something.
5248 	 */
5249 	if (zs->zs_enospc_count != 0) {
5250 		int d = ztest_random(zopt_datasets);
5251 		ztest_dataset_destroy(zs, d);
5252 	}
5253 	zs->zs_enospc_count = 0;
5254 
5255 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5256 
5257 	if (zopt_verbose >= 4)
5258 		(void) printf("starting main threads...\n");
5259 
5260 	/*
5261 	 * Kick off all the tests that run in parallel.
5262 	 */
5263 	for (int t = 0; t < zopt_threads; t++) {
5264 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5265 			return;
5266 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5267 		    THR_BOUND, &tid[t]) == 0);
5268 	}
5269 
5270 	/*
5271 	 * Wait for all of the tests to complete.  We go in reverse order
5272 	 * so we don't close datasets while threads are still using them.
5273 	 */
5274 	for (int t = zopt_threads - 1; t >= 0; t--) {
5275 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5276 		if (t < zopt_datasets)
5277 			ztest_dataset_close(zs, t);
5278 	}
5279 
5280 	txg_wait_synced(spa_get_dsl(spa), 0);
5281 
5282 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5283 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5284 
5285 	umem_free(tid, zopt_threads * sizeof (thread_t));
5286 
5287 	/* Kill the resume thread */
5288 	ztest_exiting = B_TRUE;
5289 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5290 	ztest_resume(spa);
5291 
5292 	/*
5293 	 * Right before closing the pool, kick off a bunch of async I/O;
5294 	 * spa_close() should wait for it to complete.
5295 	 */
5296 	for (uint64_t object = 1; object < 50; object++)
5297 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5298 
5299 	spa_close(spa, FTAG);
5300 
5301 	/*
5302 	 * Verify that we can loop over all pools.
5303 	 */
5304 	mutex_enter(&spa_namespace_lock);
5305 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5306 		if (zopt_verbose > 3)
5307 			(void) printf("spa_next: found %s\n", spa_name(spa));
5308 	mutex_exit(&spa_namespace_lock);
5309 
5310 	/*
5311 	 * Verify that we can export the pool and reimport it under a
5312 	 * different name.
5313 	 */
5314 	if (ztest_random(2) == 0) {
5315 		char name[MAXNAMELEN];
5316 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5317 		ztest_spa_import_export(zs->zs_pool, name);
5318 		ztest_spa_import_export(name, zs->zs_pool);
5319 	}
5320 
5321 	kernel_fini();
5322 
5323 	list_destroy(&zcl.zcl_callbacks);
5324 
5325 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
5326 
5327 	(void) rwlock_destroy(&zs->zs_name_lock);
5328 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5329 }
5330 
5331 static void
5332 ztest_freeze(ztest_shared_t *zs)
5333 {
5334 	ztest_ds_t *zd = &zs->zs_zd[0];
5335 	spa_t *spa;
5336 	int numloops = 0;
5337 
5338 	if (zopt_verbose >= 3)
5339 		(void) printf("testing spa_freeze()...\n");
5340 
5341 	kernel_init(FREAD | FWRITE);
5342 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5343 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5344 
5345 	/*
5346 	 * Force the first log block to be transactionally allocated.
5347 	 * We have to do this before we freeze the pool -- otherwise
5348 	 * the log chain won't be anchored.
5349 	 */
5350 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5351 		ztest_dmu_object_alloc_free(zd, 0);
5352 		zil_commit(zd->zd_zilog, 0);
5353 	}
5354 
5355 	txg_wait_synced(spa_get_dsl(spa), 0);
5356 
5357 	/*
5358 	 * Freeze the pool.  This stops spa_sync() from doing anything,
5359 	 * so that the only way to record changes from now on is the ZIL.
5360 	 */
5361 	spa_freeze(spa);
5362 
5363 	/*
5364 	 * Run tests that generate log records but don't alter the pool config
5365 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5366 	 * We do a txg_wait_synced() after each iteration to force the txg
5367 	 * to increase well beyond the last synced value in the uberblock.
5368 	 * The ZIL should be OK with that.
5369 	 */
5370 	while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5371 		ztest_dmu_write_parallel(zd, 0);
5372 		ztest_dmu_object_alloc_free(zd, 0);
5373 		txg_wait_synced(spa_get_dsl(spa), 0);
5374 	}
5375 
5376 	/*
5377 	 * Commit all of the changes we just generated.
5378 	 */
5379 	zil_commit(zd->zd_zilog, 0);
5380 	txg_wait_synced(spa_get_dsl(spa), 0);
5381 
5382 	/*
5383 	 * Close our dataset and close the pool.
5384 	 */
5385 	ztest_dataset_close(zs, 0);
5386 	spa_close(spa, FTAG);
5387 	kernel_fini();
5388 
5389 	/*
5390 	 * Open and close the pool and dataset to induce log replay.
5391 	 */
5392 	kernel_init(FREAD | FWRITE);
5393 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5394 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5395 	ztest_dataset_close(zs, 0);
5396 	spa_close(spa, FTAG);
5397 	kernel_fini();
5398 }
5399 
5400 void
5401 print_time(hrtime_t t, char *timebuf)
5402 {
5403 	hrtime_t s = t / NANOSEC;
5404 	hrtime_t m = s / 60;
5405 	hrtime_t h = m / 60;
5406 	hrtime_t d = h / 24;
5407 
5408 	s -= m * 60;
5409 	m -= h * 60;
5410 	h -= d * 24;
5411 
5412 	timebuf[0] = '\0';
5413 
5414 	if (d)
5415 		(void) sprintf(timebuf,
5416 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5417 	else if (h)
5418 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5419 	else if (m)
5420 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5421 	else
5422 		(void) sprintf(timebuf, "%llus", s);
5423 }
5424 
5425 static nvlist_t *
5426 make_random_props()
5427 {
5428 	nvlist_t *props;
5429 
5430 	if (ztest_random(2) == 0)
5431 		return (NULL);
5432 
5433 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5434 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5435 
5436 	(void) printf("props:\n");
5437 	dump_nvlist(props, 4);
5438 
5439 	return (props);
5440 }
5441 
5442 /*
5443  * Create a storage pool with the given name and initial vdev size.
5444  * Then test spa_freeze() functionality.
5445  */
5446 static void
5447 ztest_init(ztest_shared_t *zs)
5448 {
5449 	spa_t *spa;
5450 	nvlist_t *nvroot, *props;
5451 
5452 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5453 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5454 
5455 	kernel_init(FREAD | FWRITE);
5456 
5457 	/*
5458 	 * Create the storage pool.
5459 	 */
5460 	(void) spa_destroy(zs->zs_pool);
5461 	ztest_shared->zs_vdev_next_leaf = 0;
5462 	zs->zs_splits = 0;
5463 	zs->zs_mirrors = zopt_mirrors;
5464 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
5465 	    0, zopt_raidz, zs->zs_mirrors, 1);
5466 	props = make_random_props();
5467 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5468 	nvlist_free(nvroot);
5469 
5470 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5471 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5472 	spa_close(spa, FTAG);
5473 
5474 	kernel_fini();
5475 
5476 	ztest_run_zdb(zs->zs_pool);
5477 
5478 	ztest_freeze(zs);
5479 
5480 	ztest_run_zdb(zs->zs_pool);
5481 
5482 	(void) rwlock_destroy(&zs->zs_name_lock);
5483 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5484 }
5485 
5486 int
5487 main(int argc, char **argv)
5488 {
5489 	int kills = 0;
5490 	int iters = 0;
5491 	ztest_shared_t *zs;
5492 	size_t shared_size;
5493 	ztest_info_t *zi;
5494 	char timebuf[100];
5495 	char numbuf[6];
5496 	spa_t *spa;
5497 
5498 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5499 
5500 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5501 
5502 	process_options(argc, argv);
5503 
5504 	/* Override location of zpool.cache */
5505 	(void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
5506 
5507 	/*
5508 	 * Blow away any existing copy of zpool.cache
5509 	 */
5510 	if (zopt_init != 0)
5511 		(void) remove(spa_config_path);
5512 
5513 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5514 
5515 	zs = ztest_shared = (void *)mmap(0,
5516 	    P2ROUNDUP(shared_size, getpagesize()),
5517 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5518 
5519 	if (zopt_verbose >= 1) {
5520 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5521 		    " %llu seconds...\n",
5522 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5523 		    (u_longlong_t)zopt_time);
5524 	}
5525 
5526 	/*
5527 	 * Create and initialize our storage pool.
5528 	 */
5529 	for (int i = 1; i <= zopt_init; i++) {
5530 		bzero(zs, sizeof (ztest_shared_t));
5531 		if (zopt_verbose >= 3 && zopt_init != 1)
5532 			(void) printf("ztest_init(), pass %d\n", i);
5533 		zs->zs_pool = zopt_pool;
5534 		ztest_init(zs);
5535 	}
5536 
5537 	zs->zs_pool = zopt_pool;
5538 	zs->zs_proc_start = gethrtime();
5539 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5540 
5541 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5542 		zi = &zs->zs_info[f];
5543 		*zi = ztest_info[f];
5544 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5545 			zi->zi_call_next = UINT64_MAX;
5546 		else
5547 			zi->zi_call_next = zs->zs_proc_start +
5548 			    ztest_random(2 * zi->zi_interval[0] + 1);
5549 	}
5550 
5551 	/*
5552 	 * Run the tests in a loop.  These tests include fault injection
5553 	 * to verify that self-healing data works, and forced crashes
5554 	 * to verify that we never lose on-disk consistency.
5555 	 */
5556 	while (gethrtime() < zs->zs_proc_stop) {
5557 		int status;
5558 		pid_t pid;
5559 
5560 		/*
5561 		 * Initialize the workload counters for each function.
5562 		 */
5563 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5564 			zi = &zs->zs_info[f];
5565 			zi->zi_call_count = 0;
5566 			zi->zi_call_time = 0;
5567 		}
5568 
5569 		/* Set the allocation switch size */
5570 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5571 
5572 		pid = fork();
5573 
5574 		if (pid == -1)
5575 			fatal(1, "fork failed");
5576 
5577 		if (pid == 0) {	/* child */
5578 			struct rlimit rl = { 1024, 1024 };
5579 			(void) setrlimit(RLIMIT_NOFILE, &rl);
5580 			(void) enable_extended_FILE_stdio(-1, -1);
5581 			ztest_run(zs);
5582 			exit(0);
5583 		}
5584 
5585 		while (waitpid(pid, &status, 0) != pid)
5586 			continue;
5587 
5588 		if (WIFEXITED(status)) {
5589 			if (WEXITSTATUS(status) != 0) {
5590 				(void) fprintf(stderr,
5591 				    "child exited with code %d\n",
5592 				    WEXITSTATUS(status));
5593 				exit(2);
5594 			}
5595 		} else if (WIFSIGNALED(status)) {
5596 			if (WTERMSIG(status) != SIGKILL) {
5597 				(void) fprintf(stderr,
5598 				    "child died with signal %d\n",
5599 				    WTERMSIG(status));
5600 				exit(3);
5601 			}
5602 			kills++;
5603 		} else {
5604 			(void) fprintf(stderr, "something strange happened "
5605 			    "to child\n");
5606 			exit(4);
5607 		}
5608 
5609 		iters++;
5610 
5611 		if (zopt_verbose >= 1) {
5612 			hrtime_t now = gethrtime();
5613 
5614 			now = MIN(now, zs->zs_proc_stop);
5615 			print_time(zs->zs_proc_stop - now, timebuf);
5616 			nicenum(zs->zs_space, numbuf);
5617 
5618 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5619 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5620 			    iters,
5621 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5622 			    (u_longlong_t)zs->zs_enospc_count,
5623 			    100.0 * zs->zs_alloc / zs->zs_space,
5624 			    numbuf,
5625 			    100.0 * (now - zs->zs_proc_start) /
5626 			    (zopt_time * NANOSEC), timebuf);
5627 		}
5628 
5629 		if (zopt_verbose >= 2) {
5630 			(void) printf("\nWorkload summary:\n\n");
5631 			(void) printf("%7s %9s   %s\n",
5632 			    "Calls", "Time", "Function");
5633 			(void) printf("%7s %9s   %s\n",
5634 			    "-----", "----", "--------");
5635 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5636 				Dl_info dli;
5637 
5638 				zi = &zs->zs_info[f];
5639 				print_time(zi->zi_call_time, timebuf);
5640 				(void) dladdr((void *)zi->zi_func, &dli);
5641 				(void) printf("%7llu %9s   %s\n",
5642 				    (u_longlong_t)zi->zi_call_count, timebuf,
5643 				    dli.dli_sname);
5644 			}
5645 			(void) printf("\n");
5646 		}
5647 
5648 		/*
5649 		 * It's possible that we killed a child during a rename test,
5650 		 * in which case we'll have a 'ztest_tmp' pool lying around
5651 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5652 		 */
5653 		kernel_init(FREAD);
5654 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5655 			spa_close(spa, FTAG);
5656 		} else {
5657 			char tmpname[MAXNAMELEN];
5658 			kernel_fini();
5659 			kernel_init(FREAD | FWRITE);
5660 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5661 			    zopt_pool);
5662 			(void) spa_rename(tmpname, zopt_pool);
5663 		}
5664 		kernel_fini();
5665 
5666 		ztest_run_zdb(zopt_pool);
5667 	}
5668 
5669 	if (zopt_verbose >= 1) {
5670 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5671 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5672 	}
5673 
5674 	return (0);
5675 }
5676