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